[gnome-settings-daemon] rfkill: Add RFKill support plugin on Linux systems



commit 444af32e343a79ca3db0f957bf73687a0f0df9ec
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Jul 4 15:33:43 2013 +0200

    rfkill: Add RFKill support plugin on Linux systems
    
    Based on a patch by Jasper St. Pierre <jstpierre mecheye net>
    
    https://bugzilla.gnome.org/show_bug.cgi?id=703410

 configure.ac                                       |   18 +-
 ...gnome.settings-daemon.plugins.gschema.xml.in.in |   12 +
 plugins/Makefile.am                                |    6 +
 plugins/rfkill/Makefile.am                         |   64 ++++
 plugins/rfkill/gsd-rfkill-manager.c                |  351 ++++++++++++++++++++
 plugins/rfkill/gsd-rfkill-manager.h                |   58 ++++
 plugins/rfkill/gsd-rfkill-plugin.c                 |   30 ++
 plugins/rfkill/rfkill-glib.c                       |  350 +++++++++++++++++++
 plugins/rfkill/rfkill-glib.h                       |   74 ++++
 plugins/rfkill/rfkill.gnome-settings-plugin.in     |   10 +
 plugins/rfkill/rfkill.h                            |  107 ++++++
 plugins/rfkill/test-rfkill.c                       |    7 +
 po/POTFILES.in                                     |    1 +
 13 files changed, 1087 insertions(+), 1 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 41ab4df..0679bac 100644
--- a/configure.ac
+++ b/configure.ac
@@ -388,6 +388,21 @@ fi
 AM_CONDITIONAL(BUILD_PRINT_NOTIFICATIONS, [test x"$enable_cups" = x"yes"])
 
 # ---------------------------------------------------------------------------
+# Rfkill
+# ---------------------------------------------------------------------------
+
+AC_ARG_ENABLE(rfkill,
+              AS_HELP_STRING([--disable-rfkill], [disable rfkill support (default: enabled)]),,
+              enable_rfkill=yes, enabled_rfkill=no)
+
+if test x"$enable_rfkill" != x"no" ; then
+  AC_CHECK_HEADERS([linux/rfkill.h],,
+                   AC_MSG_ERROR([RFKill headers not found but rfkill support requested]))
+fi
+
+AM_CONDITIONAL(BUILD_RFKILL, [test x"$enable_rfkill" = x"yes"])
+
+# ---------------------------------------------------------------------------
 # Enable Profiling
 # ---------------------------------------------------------------------------
 AC_ARG_ENABLE(profiling,
@@ -398,7 +413,6 @@ if test "x$enable_profiling" = "xyes"; then
     AC_DEFINE(ENABLE_PROFILING,1,[enable profiling])
 fi
 
-
 # ---------------------------------------------------------------------------
 # Plugins
 # ---------------------------------------------------------------------------
@@ -505,6 +519,7 @@ plugins/mouse/Makefile
 plugins/orientation/Makefile
 plugins/print-notifications/Makefile
 plugins/remote-display/Makefile
+plugins/rfkill/Makefile
 plugins/screensaver-proxy/Makefile
 plugins/smartcard/Makefile
 plugins/sound/Makefile
@@ -564,6 +579,7 @@ echo "
         Smartcard support:        ${have_smartcard_support}
         Cups support:             ${enable_cups}
         Wacom support:            ${have_wacom}
+        RFKill support:           ${enable_rfkill}
 ${NSS_DATABASE:+\
         System nssdb:             ${NSS_DATABASE}
 }\
diff --git a/data/org.gnome.settings-daemon.plugins.gschema.xml.in.in 
b/data/org.gnome.settings-daemon.plugins.gschema.xml.in.in
index 4150199..bea64f3 100644
--- a/data/org.gnome.settings-daemon.plugins.gschema.xml.in.in
+++ b/data/org.gnome.settings-daemon.plugins.gschema.xml.in.in
@@ -102,6 +102,18 @@
       <_description>Priority to use for this plugin in gnome-settings-daemon startup queue</_description>
     </key>
   </schema>
+  <schema gettext-domain="@GETTEXT_PACKAGE@" id="org.gnome.settings-daemon.plugins.rfkill" 
path="/org/gnome/settings-daemon/plugins/rfkill/">
+    <key name="active" type="b">
+      <default>true</default>
+      <_summary>Activation of this plugin</_summary>
+      <_description>Whether this plugin would be activated by gnome-settings-daemon or not</_description>
+    </key>
+    <key name="priority" type="i">
+      <default>0</default>
+      <_summary>Priority to use for this plugin</_summary>
+      <_description>Priority to use for this plugin in gnome-settings-daemon startup queue</_description>
+    </key>
+  </schema>
   <schema gettext-domain="@GETTEXT_PACKAGE@" id="org.gnome.settings-daemon.plugins.screensaver-proxy" 
path="/org/gnome/settings-daemon/plugins/screensaver-proxy/">
     <key name="active" type="b">
       <default>true</default>
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index b3aed9e..787fd14 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -51,5 +51,11 @@ else
 disabled_plugins += print-notifications
 endif
 
+if BUILD_RFKILL
+enabled_plugins += rfkill
+else
+disabled_plugins += rfkill
+endif
+
 SUBDIRS = common $(enabled_plugins)
 DIST_SUBDIRS = $(SUBDIRS) $(disabled_plugins)
diff --git a/plugins/rfkill/Makefile.am b/plugins/rfkill/Makefile.am
new file mode 100644
index 0000000..73bf107
--- /dev/null
+++ b/plugins/rfkill/Makefile.am
@@ -0,0 +1,64 @@
+plugin_name = rfkill
+
+libexec_PROGRAMS = gsd-test-rfkill
+
+gsd_test_rfkill_SOURCES =              \
+       gsd-rfkill-manager.h    \
+       gsd-rfkill-manager.c    \
+       rfkill-glib.c           \
+       rfkill-glib.h           \
+       rfkill.h                \
+       test-rfkill.c
+
+gsd_test_rfkill_CFLAGS =                                       \
+       -I$(top_srcdir)/gnome-settings-daemon                   \
+       -I$(top_srcdir)/plugins/common                          \
+       -DGNOME_SETTINGS_LOCALEDIR=\""$(datadir)/locale"\"      \
+       $(PLUGIN_CFLAGS)                                        \
+       $(SETTINGS_PLUGIN_CFLAGS)                               \
+       $(AM_CFLAGS)
+
+gsd_test_rfkill_LDADD =                                        \
+       $(top_builddir)/gnome-settings-daemon/libgsd.la         \
+       $(top_builddir)/plugins/common/libcommon.la             \
+       $(SETTINGS_PLUGIN_LIBS)
+
+plugin_LTLIBRARIES = librfkill.la
+
+librfkill_la_SOURCES =                 \
+       gsd-rfkill-plugin.c     \
+       gsd-rfkill-manager.h    \
+       gsd-rfkill-manager.c    \
+       rfkill-glib.c           \
+       rfkill-glib.h           \
+       rfkill.h
+
+librfkill_la_CPPFLAGS = \
+       -I$(top_srcdir)/gnome-settings-daemon           \
+       -I$(top_srcdir)/data/                           \
+       -DGNOME_SETTINGS_LOCALEDIR=\""$(datadir)/locale"\" \
+       -DLIBEXECDIR=\""$(libexecdir)"\"                \
+       $(AM_CPPFLAGS)
+
+librfkill_la_CFLAGS = \
+       $(PLUGIN_CFLAGS)                \
+       $(RFKILL_CFLAGS)                \
+       $(SETTINGS_PLUGIN_CFLAGS)       \
+       $(AM_CFLAGS)
+
+librfkill_la_LDFLAGS =                 \
+       $(GSD_PLUGIN_LDFLAGS)
+
+librfkill_la_LIBADD  =                                         \
+       $(RFKILL_LIBS)                                          \
+       $(SETTINGS_PLUGIN_LIBS)
+
+plugin_in_files = rfkill.gnome-settings-plugin.in
+
+plugin_DATA = $(plugin_in_files:.gnome-settings-plugin.in=.gnome-settings-plugin)
+
+EXTRA_DIST = $(plugin_in_files)
+CLEANFILES = $(plugin_DATA)
+DISTCLEANFILES = $(plugin_DATA)
+
+ GSD_INTLTOOL_PLUGIN_RULE@
diff --git a/plugins/rfkill/gsd-rfkill-manager.c b/plugins/rfkill/gsd-rfkill-manager.c
new file mode 100644
index 0000000..80c61c2
--- /dev/null
+++ b/plugins/rfkill/gsd-rfkill-manager.c
@@ -0,0 +1,351 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2007 William Jon McCann <mccann jhu edu>
+ * Copyright (C) 2010,2011 Red Hat, Inc.
+ *
+ * Author: Bastien Nocera <hadess hadess net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#include "config.h"
+
+#include <gio/gio.h>
+#include <string.h>
+
+#include "gnome-settings-plugin.h"
+#include "gnome-settings-profile.h"
+#include "gsd-rfkill-manager.h"
+#include "rfkill-glib.h"
+
+#define GSD_RFKILL_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GSD_TYPE_RFKILL_MANAGER, 
GsdRfkillManagerPrivate))
+
+struct GsdRfkillManagerPrivate
+{
+        GDBusNodeInfo           *introspection_data;
+        guint                    name_id;
+        GDBusConnection         *connection;
+
+        CcRfkillGlib            *rfkill;
+        GHashTable              *killswitches;
+};
+
+#define GSD_RFKILL_DBUS_NAME GSD_DBUS_NAME ".Rfkill"
+#define GSD_RFKILL_DBUS_PATH GSD_DBUS_PATH "/Rfkill"
+
+static const gchar introspection_xml[] =
+"<node>"
+"  <interface name='org.gnome.SettingsDaemon.Rfkill'>"
+"    <annotation name='org.freedesktop.DBus.GLib.CSymbol' value='gsd_rfkill_manager'/>"
+"      <property name='AirplaneMode' type='b' access='readwrite'/>"
+"  </interface>"
+"</node>";
+
+static void     gsd_rfkill_manager_class_init  (GsdRfkillManagerClass *klass);
+static void     gsd_rfkill_manager_init        (GsdRfkillManager      *rfkill_manager);
+static void     gsd_rfkill_manager_finalize    (GObject                    *object);
+
+G_DEFINE_TYPE (GsdRfkillManager, gsd_rfkill_manager, G_TYPE_OBJECT)
+
+static gpointer manager_object = NULL;
+
+static GObject *
+gsd_rfkill_manager_constructor (GType                     type,
+                               guint                      n_construct_properties,
+                               GObjectConstructParam     *construct_properties)
+{
+        GsdRfkillManager      *rfkill_manager;
+
+        rfkill_manager = GSD_RFKILL_MANAGER (G_OBJECT_CLASS (gsd_rfkill_manager_parent_class)->constructor 
(type,
+                                                                                                         
n_construct_properties,
+                                                                                                         
construct_properties));
+
+        return G_OBJECT (rfkill_manager);
+}
+
+static void
+gsd_rfkill_manager_class_init (GsdRfkillManagerClass *klass)
+{
+        GObjectClass   *object_class = G_OBJECT_CLASS (klass);
+
+        object_class->constructor = gsd_rfkill_manager_constructor;
+        object_class->finalize = gsd_rfkill_manager_finalize;
+
+        g_type_class_add_private (klass, sizeof (GsdRfkillManagerPrivate));
+}
+
+static void
+gsd_rfkill_manager_init (GsdRfkillManager *manager)
+{
+        manager->priv = GSD_RFKILL_MANAGER_GET_PRIVATE (manager);
+}
+
+static gboolean
+engine_get_airplane_mode (GsdRfkillManager *manager)
+{
+       gboolean enabled;
+       GHashTableIter iter;
+       gpointer key, value;
+
+       enabled = TRUE;
+
+       g_hash_table_iter_init (&iter, manager->priv->killswitches);
+       while (g_hash_table_iter_next (&iter, &key, &value)) {
+               int idx, state;
+
+               idx = GPOINTER_TO_INT (key);
+               state = GPOINTER_TO_INT (value);
+               g_debug ("Killswitch %d is %s", idx, state ? "enabled" : "disabled");
+
+               /* A single device that's enabled? airplane mode is off */
+               if (state == FALSE) {
+                       enabled = FALSE;
+                       break;
+               }
+       }
+
+        return enabled;
+}
+
+static void
+rfkill_changed (CcRfkillGlib     *rfkill,
+               GList            *events,
+               GsdRfkillManager  *manager)
+{
+       GList *l;
+        GVariant *params;
+
+       for (l = events; l != NULL; l = l->next) {
+               struct rfkill_event *event = l->data;
+
+                switch (event->op) {
+                case RFKILL_OP_ADD:
+                case RFKILL_OP_CHANGE:
+                       g_hash_table_insert (manager->priv->killswitches,
+                                            GINT_TO_POINTER (event->idx),
+                                            GINT_TO_POINTER (event->soft || event->hard));
+                        break;
+                case RFKILL_OP_DEL:
+                       g_hash_table_remove (manager->priv->killswitches,
+                                            GINT_TO_POINTER (event->idx));
+                        break;
+                }
+       }
+
+        /* not yet connected to the bus */
+        if (manager->priv->connection == NULL)
+                return;
+
+        params = g_variant_new_parsed ("('" GSD_RFKILL_DBUS_NAME "', [{'AirplaneMode', %v}], @as [])",
+                                       g_variant_new_boolean (engine_get_airplane_mode (manager)));
+
+        g_dbus_connection_emit_signal (manager->priv->connection,
+                                       NULL,
+                                       GSD_RFKILL_DBUS_PATH,
+                                       "org.freedesktop.DBus.Properties",
+                                       "PropertiesChanged",
+                                       params, NULL);
+}
+
+static void
+rfkill_set_cb (GObject      *source_object,
+              GAsyncResult *res,
+              gpointer      user_data)
+{
+       gboolean ret;
+       GError *error = NULL;
+
+       ret = cc_rfkill_glib_send_event_finish (CC_RFKILL_GLIB (source_object), res, &error);
+       if (!ret) {
+               g_warning ("Failed to set RFKill: %s", error->message);
+               g_error_free (error);
+       }
+}
+
+static gboolean
+engine_set_airplane_mode (GsdRfkillManager *manager,
+                          gboolean          enable)
+{
+       struct rfkill_event event;
+
+       memset (&event, 0, sizeof(event));
+       event.op = RFKILL_OP_CHANGE_ALL;
+       event.type = RFKILL_TYPE_ALL;
+       event.soft = enable ? 1 : 0;
+       cc_rfkill_glib_send_event (manager->priv->rfkill, &event, NULL, rfkill_set_cb, manager);
+
+       return TRUE;
+}
+
+static gboolean
+handle_set_property (GDBusConnection *connection,
+                     const gchar     *sender,
+                     const gchar     *object_path,
+                     const gchar     *interface_name,
+                     const gchar     *property_name,
+                     GVariant        *value,
+                     GError         **error,
+                     gpointer         user_data)
+{
+        GsdRfkillManager *manager = GSD_RFKILL_MANAGER (user_data);
+
+        if (g_strcmp0 (property_name, "AirplaneMode") == 0) {
+                gboolean airplane_mode;
+                g_variant_get (value, "b", &airplane_mode);
+                return engine_set_airplane_mode (manager, airplane_mode);
+        }
+
+        return FALSE;
+}
+
+static GVariant *
+handle_get_property (GDBusConnection *connection,
+                     const gchar     *sender,
+                     const gchar     *object_path,
+                     const gchar     *interface_name,
+                     const gchar     *property_name,
+                     GError         **error,
+                     gpointer         user_data)
+{
+        GsdRfkillManager *manager = GSD_RFKILL_MANAGER (user_data);
+
+        /* Check session pointer as a proxy for whether the manager is in the
+           start or stop state */
+        if (manager->priv->connection == NULL) {
+                return NULL;
+        }
+
+        if (g_strcmp0 (property_name, "AirplaneMode") == 0) {
+                gboolean airplane_mode;
+                airplane_mode = engine_get_airplane_mode (manager);
+                return g_variant_new_boolean (airplane_mode);
+        }
+
+        return NULL;
+}
+
+static const GDBusInterfaceVTable interface_vtable =
+{
+        NULL,
+        handle_get_property,
+        handle_set_property
+};
+
+static void
+on_bus_gotten (GObject               *source_object,
+               GAsyncResult          *res,
+               GsdRfkillManager *manager)
+{
+        GDBusConnection *connection;
+        GError *error = NULL;
+
+        connection = g_bus_get_finish (res, &error);
+        if (connection == NULL) {
+                g_warning ("Could not get session bus: %s", error->message);
+                g_error_free (error);
+                return;
+        }
+        manager->priv->connection = connection;
+
+        g_dbus_connection_register_object (connection,
+                                           GSD_RFKILL_DBUS_PATH,
+                                           manager->priv->introspection_data->interfaces[0],
+                                           &interface_vtable,
+                                           manager,
+                                           NULL,
+                                           NULL);
+
+        manager->priv->name_id = g_bus_own_name_on_connection (connection,
+                                                               GSD_RFKILL_DBUS_NAME,
+                                                               G_BUS_NAME_OWNER_FLAGS_NONE,
+                                                               NULL,
+                                                               NULL,
+                                                               NULL,
+                                                               NULL);
+}
+
+gboolean
+gsd_rfkill_manager_start (GsdRfkillManager *manager,
+                         GError         **error)
+{
+        gnome_settings_profile_start (NULL);
+
+        manager->priv->introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
+        g_assert (manager->priv->introspection_data != NULL);
+
+        manager->priv->killswitches = g_hash_table_new (g_direct_hash, g_direct_equal);
+        manager->priv->rfkill = cc_rfkill_glib_new ();
+        g_signal_connect (G_OBJECT (manager->priv->rfkill), "changed",
+                          G_CALLBACK (rfkill_changed), manager);
+        cc_rfkill_glib_open (manager->priv->rfkill);
+
+        /* Start process of owning a D-Bus name */
+        g_bus_get (G_BUS_TYPE_SESSION,
+                   NULL,
+                   (GAsyncReadyCallback) on_bus_gotten,
+                   manager);
+
+        gnome_settings_profile_end (NULL);
+
+        return TRUE;
+}
+
+void
+gsd_rfkill_manager_stop (GsdRfkillManager *manager)
+{
+        GsdRfkillManagerPrivate *p = manager->priv;
+
+        g_debug ("Stopping rfkill manager");
+
+        if (manager->priv->name_id != 0)
+                g_bus_unown_name (manager->priv->name_id);
+
+        g_clear_pointer (&p->introspection_data, g_dbus_node_info_unref);
+        g_clear_object (&p->connection);
+        g_clear_object (&p->rfkill);
+        g_clear_pointer (&p->killswitches, g_hash_table_destroy);
+}
+
+static void
+gsd_rfkill_manager_finalize (GObject *object)
+{
+        GsdRfkillManager *manager;
+
+        g_return_if_fail (object != NULL);
+        g_return_if_fail (GSD_IS_RFKILL_MANAGER (object));
+
+        manager = GSD_RFKILL_MANAGER (object);
+
+        g_return_if_fail (manager->priv != NULL);
+
+        gsd_rfkill_manager_stop (manager);
+
+        G_OBJECT_CLASS (gsd_rfkill_manager_parent_class)->finalize (object);
+}
+
+GsdRfkillManager *
+gsd_rfkill_manager_new (void)
+{
+        if (manager_object != NULL) {
+                g_object_ref (manager_object);
+        } else {
+                manager_object = g_object_new (GSD_TYPE_RFKILL_MANAGER, NULL);
+                g_object_add_weak_pointer (manager_object,
+                                           (gpointer *) &manager_object);
+        }
+
+        return GSD_RFKILL_MANAGER (manager_object);
+}
diff --git a/plugins/rfkill/gsd-rfkill-manager.h b/plugins/rfkill/gsd-rfkill-manager.h
new file mode 100644
index 0000000..c047c39
--- /dev/null
+++ b/plugins/rfkill/gsd-rfkill-manager.h
@@ -0,0 +1,58 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2007 William Jon McCann <mccann jhu edu>
+ * Copyright (C) 2010 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#ifndef __GSD_RFKILL_MANAGER_H
+#define __GSD_RFKILL_MANAGER_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define GSD_TYPE_RFKILL_MANAGER         (gsd_rfkill_manager_get_type ())
+#define GSD_RFKILL_MANAGER(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GSD_TYPE_RFKILL_MANAGER, 
GsdRfkillManager))
+#define GSD_RFKILL_MANAGER_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), GSD_TYPE_RFKILL_MANAGER, 
GsdRfkillManagerClass))
+#define GSD_IS_RFKILL_MANAGER(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GSD_TYPE_RFKILL_MANAGER))
+#define GSD_IS_RFKILL_MANAGER_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), GSD_TYPE_RFKILL_MANAGER))
+#define GSD_RFKILL_MANAGER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GSD_TYPE_RFKILL_MANAGER, 
GsdRfkillManagerClass))
+
+typedef struct GsdRfkillManagerPrivate GsdRfkillManagerPrivate;
+
+typedef struct
+{
+        GObject                     parent;
+        GsdRfkillManagerPrivate *priv;
+} GsdRfkillManager;
+
+typedef struct
+{
+        GObjectClass   parent_class;
+} GsdRfkillManagerClass;
+
+GType                   gsd_rfkill_manager_get_type            (void);
+
+GsdRfkillManager *       gsd_rfkill_manager_new                 (void);
+gboolean                gsd_rfkill_manager_start               (GsdRfkillManager *manager,
+                                                               GError         **error);
+void                    gsd_rfkill_manager_stop                (GsdRfkillManager *manager);
+
+G_END_DECLS
+
+#endif /* __GSD_RFKILL_MANAGER_H */
diff --git a/plugins/rfkill/gsd-rfkill-plugin.c b/plugins/rfkill/gsd-rfkill-plugin.c
new file mode 100644
index 0000000..0a16db4
--- /dev/null
+++ b/plugins/rfkill/gsd-rfkill-plugin.c
@@ -0,0 +1,30 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2007 William Jon McCann <mccann jhu edu>
+ * Copyright (C) 2010 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#include "config.h"
+
+#include <glib/gi18n-lib.h>
+#include <gmodule.h>
+
+#include "gnome-settings-plugin.h"
+#include "gsd-rfkill-manager.h"
+
+GNOME_SETTINGS_PLUGIN_REGISTER (GsdRfkill, gsd_rfkill)
diff --git a/plugins/rfkill/rfkill-glib.c b/plugins/rfkill/rfkill-glib.c
new file mode 100644
index 0000000..aabce67
--- /dev/null
+++ b/plugins/rfkill/rfkill-glib.c
@@ -0,0 +1,350 @@
+/*
+ *
+ *  gnome-bluetooth - Bluetooth integration for GNOME
+ *
+ *  Copyright (C) 2012  Bastien Nocera <hadess hadess net>
+ *
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <errno.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+
+#include <glib.h>
+#include <gio/gio.h>
+#include <gio/gunixoutputstream.h>
+
+#include "rfkill-glib.h"
+
+enum {
+       CHANGED,
+       LAST_SIGNAL
+};
+
+static int signals[LAST_SIGNAL] = { 0 };
+
+#define CC_RFKILL_GLIB_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
+                               CC_RFKILL_TYPE_GLIB, CcRfkillGlibPrivate))
+
+struct CcRfkillGlibPrivate {
+       GOutputStream *stream;
+       GIOChannel *channel;
+       guint watch_id;
+};
+
+G_DEFINE_TYPE(CcRfkillGlib, cc_rfkill_glib, G_TYPE_OBJECT)
+
+gboolean
+cc_rfkill_glib_send_event_finish (CcRfkillGlib  *rfkill,
+                                 GAsyncResult  *res,
+                                 GError       **error)
+{
+       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
+
+       g_return_val_if_fail (RFKILL_IS_GLIB (rfkill), FALSE);
+       g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == cc_rfkill_glib_send_event);
+
+       if (g_simple_async_result_propagate_error (simple, error))
+               return FALSE;
+
+       return (g_simple_async_result_get_op_res_gssize (simple) >= 0);
+}
+
+static void
+write_done_cb (GObject      *source_object,
+              GAsyncResult *res,
+              gpointer      user_data)
+{
+       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
+       GError *error = NULL;
+       gssize ret;
+
+       ret = g_output_stream_write_finish (G_OUTPUT_STREAM (source_object), res, &error);
+       if (ret < 0)
+               g_simple_async_result_take_error (simple, error);
+       else
+               g_simple_async_result_set_op_res_gssize (simple, ret);
+
+       g_simple_async_result_complete_in_idle (simple);
+       g_object_unref (simple);
+}
+
+void
+cc_rfkill_glib_send_event (CcRfkillGlib        *rfkill,
+                          struct rfkill_event *event,
+                          GCancellable        *cancellable,
+                          GAsyncReadyCallback  callback,
+                          gpointer             user_data)
+{
+       GSimpleAsyncResult *simple;
+
+       g_return_if_fail (RFKILL_IS_GLIB (rfkill));
+       g_return_if_fail (rfkill->priv->stream);
+
+       simple = g_simple_async_result_new (G_OBJECT (rfkill),
+                                           callback,
+                                           user_data,
+                                           cc_rfkill_glib_send_event);
+
+       g_output_stream_write_async (rfkill->priv->stream,
+                                    event, sizeof(struct rfkill_event),
+                                    G_PRIORITY_DEFAULT,
+                                    cancellable, write_done_cb, simple);
+}
+
+static const char *
+type_to_string (unsigned int type)
+{
+       switch (type) {
+       case RFKILL_TYPE_ALL:
+               return "ALL";
+       case RFKILL_TYPE_WLAN:
+               return "WLAN";
+       case RFKILL_TYPE_BLUETOOTH:
+               return "RFKILL";
+       case RFKILL_TYPE_UWB:
+               return "UWB";
+       case RFKILL_TYPE_WIMAX:
+               return "WIMAX";
+       case RFKILL_TYPE_WWAN:
+               return "WWAN";
+       default:
+               return "UNKNOWN";
+       }
+}
+
+static const char *
+op_to_string (unsigned int op)
+{
+       switch (op) {
+       case RFKILL_OP_ADD:
+               return "ADD";
+       case RFKILL_OP_DEL:
+               return "DEL";
+       case RFKILL_OP_CHANGE:
+               return "CHANGE";
+       case RFKILL_OP_CHANGE_ALL:
+               return "CHANGE_ALL";
+       default:
+               g_assert_not_reached ();
+       }
+}
+
+static void
+print_event (struct rfkill_event *event)
+{
+       g_debug ("RFKILL event: idx %u type %u (%s) op %u (%s) soft %u hard %u",
+                event->idx,
+                event->type, type_to_string (event->type),
+                event->op, op_to_string (event->op),
+                event->soft, event->hard);
+}
+
+static void
+emit_changed_signal_and_free (CcRfkillGlib *rfkill,
+                             GList      *events)
+{
+       if (events == NULL)
+               return;
+
+       g_signal_emit (G_OBJECT (rfkill),
+                      signals[CHANGED],
+                      0, events);
+       g_list_free_full (events, g_free);
+}
+
+static gboolean
+event_cb (GIOChannel   *source,
+         GIOCondition  condition,
+         CcRfkillGlib   *rfkill)
+{
+       GList *events;
+
+       events = NULL;
+
+       if (condition & G_IO_IN) {
+               GIOStatus status;
+               struct rfkill_event event;
+               gsize read;
+
+               status = g_io_channel_read_chars (source,
+                                                 (char *) &event,
+                                                 sizeof(event),
+                                                 &read,
+                                                 NULL);
+
+               while (status == G_IO_STATUS_NORMAL && read == sizeof(event)) {
+                       struct rfkill_event *event_ptr;
+
+                       print_event (&event);
+
+                       event_ptr = g_memdup (&event, sizeof(event));
+                       events = g_list_prepend (events, event_ptr);
+
+                       status = g_io_channel_read_chars (source,
+                                                         (char *) &event,
+                                                         sizeof(event),
+                                                         &read,
+                                                         NULL);
+               }
+               events = g_list_reverse (events);
+       } else {
+               g_debug ("something else happened");
+               return FALSE;
+       }
+
+       emit_changed_signal_and_free (rfkill, events);
+
+       return TRUE;
+}
+
+static void
+cc_rfkill_glib_init (CcRfkillGlib *rfkill)
+{
+       CcRfkillGlibPrivate *priv;
+
+       priv = CC_RFKILL_GLIB_GET_PRIVATE (rfkill);
+       rfkill->priv = priv;
+}
+
+int
+cc_rfkill_glib_open (CcRfkillGlib *rfkill)
+{
+       CcRfkillGlibPrivate *priv;
+       int fd;
+       int ret;
+       GList *events;
+
+       g_return_val_if_fail (RFKILL_IS_GLIB (rfkill), -1);
+       g_return_val_if_fail (rfkill->priv->stream, -1);
+
+       priv = rfkill->priv;
+
+       fd = open("/dev/rfkill", O_RDWR);
+       if (fd < 0) {
+               if (errno == EACCES)
+                       g_warning ("Could not open RFKILL control device, please verify your installation");
+               return fd;
+       }
+
+       ret = fcntl(fd, F_SETFL, O_NONBLOCK);
+       if (ret < 0) {
+               g_debug ("Can't set RFKILL control device to non-blocking");
+               close(fd);
+               return ret;
+       }
+
+       events = NULL;
+
+       while (1) {
+               struct rfkill_event event;
+               struct rfkill_event *event_ptr;
+               ssize_t len;
+
+               len = read(fd, &event, sizeof(event));
+               if (len < 0) {
+                       if (errno == EAGAIN)
+                               break;
+                       g_debug ("Reading of RFKILL events failed");
+                       break;
+               }
+
+               if (len != RFKILL_EVENT_SIZE_V1) {
+                       g_warning ("Wrong size of RFKILL event\n");
+                       continue;
+               }
+
+               if (event.op != RFKILL_OP_ADD)
+                       continue;
+
+               g_debug ("Read killswitch of type '%s' (idx=%d): soft %d hard %d",
+                        type_to_string (event.type),
+                        event.idx, event.soft, event.hard);
+
+               event_ptr = g_memdup (&event, sizeof(event));
+               events = g_list_prepend (events, event_ptr);
+       }
+
+       /* Setup monitoring */
+       priv->channel = g_io_channel_unix_new (fd);
+       priv->watch_id = g_io_add_watch (priv->channel,
+                                        G_IO_IN | G_IO_HUP | G_IO_ERR,
+                                        (GIOFunc) event_cb,
+                                        rfkill);
+
+       events = g_list_reverse (events);
+       emit_changed_signal_and_free (rfkill, events);
+
+       /* Setup write stream */
+       priv->stream = g_unix_output_stream_new (fd, TRUE);
+
+       return fd;
+}
+
+static void
+cc_rfkill_glib_finalize (GObject *object)
+{
+       CcRfkillGlib *rfkill;
+       CcRfkillGlibPrivate *priv;
+
+       rfkill = CC_RFKILL_GLIB (object);
+       priv = rfkill->priv;
+
+       /* cleanup monitoring */
+       if (priv->watch_id > 0) {
+               g_source_remove (priv->watch_id);
+               priv->watch_id = 0;
+               g_io_channel_shutdown (priv->channel, FALSE, NULL);
+               g_io_channel_unref (priv->channel);
+       }
+       g_clear_object (&priv->stream);
+
+       G_OBJECT_CLASS(cc_rfkill_glib_parent_class)->finalize(object);
+}
+
+static void
+cc_rfkill_glib_class_init(CcRfkillGlibClass *klass)
+{
+       GObjectClass *object_class = (GObjectClass *) klass;
+
+       g_type_class_add_private(klass, sizeof(CcRfkillGlibPrivate));
+       object_class->finalize = cc_rfkill_glib_finalize;
+
+       signals[CHANGED] =
+               g_signal_new ("changed",
+                             G_TYPE_FROM_CLASS (klass),
+                             G_SIGNAL_RUN_LAST,
+                             G_STRUCT_OFFSET (CcRfkillGlibClass, changed),
+                             NULL, NULL,
+                             NULL,
+                             G_TYPE_NONE, 1, G_TYPE_POINTER);
+
+}
+
+CcRfkillGlib *
+cc_rfkill_glib_new (void)
+{
+       return CC_RFKILL_GLIB (g_object_new (CC_RFKILL_TYPE_GLIB, NULL));
+}
diff --git a/plugins/rfkill/rfkill-glib.h b/plugins/rfkill/rfkill-glib.h
new file mode 100644
index 0000000..4ac8b68
--- /dev/null
+++ b/plugins/rfkill/rfkill-glib.h
@@ -0,0 +1,74 @@
+/*
+ *
+ *  gnome-bluetooth - Bluetooth integration for GNOME
+ *
+ *  Copyright (C) 2012  Bastien Nocera <hadess hadess net>
+ *
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef __CC_RFKILL_GLIB_H
+#define __CC_RFKILL_GLIB_H
+
+#include <glib-object.h>
+#include <gio/gio.h>
+#include "rfkill.h"
+
+G_BEGIN_DECLS
+
+#define CC_RFKILL_TYPE_GLIB (cc_rfkill_glib_get_type())
+#define CC_RFKILL_GLIB(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \
+               CC_RFKILL_TYPE_GLIB, CcRfkillGlib))
+#define CC_RFKILL_GLIB_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), \
+               CC_RFKILL_TYPE_GLIB, CcRfkillGlibClass))
+#define RFKILL_IS_GLIB(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), \
+               CC_RFKILL_TYPE_GLIB))
+#define RFKILL_IS_GLIB_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), \
+               CC_RFKILL_TYPE_GLIB))
+#define RFKILL_GET_GLIB_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), \
+               CC_RFKILL_TYPE_GLIB, CcRfkillGlibClass))
+
+typedef struct CcRfkillGlibPrivate CcRfkillGlibPrivate;
+
+typedef struct _CcRfkillGlib {
+       GObject parent;
+       CcRfkillGlibPrivate *priv;
+} CcRfkillGlib;
+
+typedef struct _CcRfkillGlibClass {
+       GObjectClass parent_class;
+
+       void (*changed) (CcRfkillGlib *rfkill, GList *events);
+} CcRfkillGlibClass;
+
+GType         cc_rfkill_glib_get_type          (void);
+CcRfkillGlib *cc_rfkill_glib_new               (void);
+int           cc_rfkill_glib_open              (CcRfkillGlib *rfkill);
+
+void          cc_rfkill_glib_send_event        (CcRfkillGlib        *rfkill,
+                                               struct rfkill_event *event,
+                                               GCancellable        *cancellable,
+                                               GAsyncReadyCallback  callback,
+                                               gpointer             user_data);
+
+gboolean      cc_rfkill_glib_send_event_finish (CcRfkillGlib        *rfkill,
+                                               GAsyncResult        *res,
+                                               GError             **error);
+
+G_END_DECLS
+
+#endif /* __CC_RFKILL_GLIB_H */
diff --git a/plugins/rfkill/rfkill.gnome-settings-plugin.in b/plugins/rfkill/rfkill.gnome-settings-plugin.in
new file mode 100644
index 0000000..27412a2
--- /dev/null
+++ b/plugins/rfkill/rfkill.gnome-settings-plugin.in
@@ -0,0 +1,10 @@
+[GNOME Settings Plugin]
+Module=rfkill
+IAge=0
+# Default Priority
+# Priority=100
+_Name=Rfkill
+_Description=Rfkill plugin
+Authors=Bastien Nocera
+Copyright=Copyright © 2013
+Website=
diff --git a/plugins/rfkill/rfkill.h b/plugins/rfkill/rfkill.h
new file mode 100644
index 0000000..abb2c66
--- /dev/null
+++ b/plugins/rfkill/rfkill.h
@@ -0,0 +1,107 @@
+#ifndef __RFKILL_H
+#define __RFKILL_H
+
+/*
+ * Copyright (C) 2006 - 2007 Ivo van Doorn
+ * Copyright (C) 2007 Dmitry Torokhov
+ * Copyright 2009 Johannes Berg <johannes sipsolutions net>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <linux/types.h>
+
+/* define userspace visible states */
+#define RFKILL_STATE_SOFT_BLOCKED      0
+#define RFKILL_STATE_UNBLOCKED         1
+#define RFKILL_STATE_HARD_BLOCKED      2
+
+/**
+ * enum rfkill_type - type of rfkill switch.
+ *
+ * @RFKILL_TYPE_ALL: toggles all switches (requests only - not a switch type)
+ * @RFKILL_TYPE_WLAN: switch is on a 802.11 wireless network device.
+ * @RFKILL_TYPE_BLUETOOTH: switch is on a bluetooth device.
+ * @RFKILL_TYPE_UWB: switch is on a ultra wideband device.
+ * @RFKILL_TYPE_WIMAX: switch is on a WiMAX device.
+ * @RFKILL_TYPE_WWAN: switch is on a wireless WAN device.
+ * @RFKILL_TYPE_GPS: switch is on a GPS device.
+ * @RFKILL_TYPE_FM: switch is on a FM radio device.
+ * @NUM_RFKILL_TYPES: number of defined rfkill types
+ */
+enum rfkill_type {
+       RFKILL_TYPE_ALL = 0,
+       RFKILL_TYPE_WLAN,
+       RFKILL_TYPE_BLUETOOTH,
+       RFKILL_TYPE_UWB,
+       RFKILL_TYPE_WIMAX,
+       RFKILL_TYPE_WWAN,
+       RFKILL_TYPE_GPS,
+       RFKILL_TYPE_FM,
+       NUM_RFKILL_TYPES,
+};
+
+/**
+ * enum rfkill_operation - operation types
+ * @RFKILL_OP_ADD: a device was added
+ * @RFKILL_OP_DEL: a device was removed
+ * @RFKILL_OP_CHANGE: a device's state changed -- userspace changes one device
+ * @RFKILL_OP_CHANGE_ALL: userspace changes all devices (of a type, or all)
+ */
+enum rfkill_operation {
+       RFKILL_OP_ADD = 0,
+       RFKILL_OP_DEL,
+       RFKILL_OP_CHANGE,
+       RFKILL_OP_CHANGE_ALL,
+};
+
+/**
+ * struct rfkill_event - events for userspace on /dev/rfkill
+ * @idx: index of dev rfkill
+ * @type: type of the rfkill struct
+ * @op: operation code
+ * @hard: hard state (0/1)
+ * @soft: soft state (0/1)
+ *
+ * Structure used for userspace communication on /dev/rfkill,
+ * used for events from the kernel and control to the kernel.
+ */
+struct rfkill_event {
+       __u32 idx;
+       __u8  type;
+       __u8  op;
+       __u8  soft, hard;
+} __attribute__((packed));
+
+/*
+ * We are planning to be backward and forward compatible with changes
+ * to the event struct, by adding new, optional, members at the end.
+ * When reading an event (whether the kernel from userspace or vice
+ * versa) we need to accept anything that's at least as large as the
+ * version 1 event size, but might be able to accept other sizes in
+ * the future.
+ *
+ * One exception is the kernel -- we already have two event sizes in
+ * that we've made the 'hard' member optional since our only option
+ * is to ignore it anyway.
+ */
+#define RFKILL_EVENT_SIZE_V1   8
+
+/* ioctl for turning off rfkill-input (if present) */
+#define RFKILL_IOC_MAGIC       'R'
+#define RFKILL_IOC_NOINPUT     1
+#define RFKILL_IOCTL_NOINPUT   _IO(RFKILL_IOC_MAGIC, RFKILL_IOC_NOINPUT)
+
+/* and that's all userspace gets */
+
+#endif /* RFKILL_H */
diff --git a/plugins/rfkill/test-rfkill.c b/plugins/rfkill/test-rfkill.c
new file mode 100644
index 0000000..251b313
--- /dev/null
+++ b/plugins/rfkill/test-rfkill.c
@@ -0,0 +1,7 @@
+#define NEW gsd_rfkill_manager_new
+#define START gsd_rfkill_manager_start
+#define STOP gsd_rfkill_manager_stop
+#define MANAGER GsdRfkillManager
+#include "gsd-rfkill-manager.h"
+
+#include "test-plugin.h"
diff --git a/po/POTFILES.in b/po/POTFILES.in
index eb87766..2a2b0de 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -45,6 +45,7 @@ plugins/print-notifications/gsd-printer.c
 plugins/print-notifications/gsd-print-notifications-manager.c
 [type: gettext/ini]plugins/print-notifications/print-notifications.gnome-settings-plugin.in
 [type: gettext/ini]plugins/remote-display/remote-display.gnome-settings-plugin.in
+[type: gettext/ini]plugins/rfkill/rfkill.gnome-settings-plugin.in
 [type: gettext/ini]plugins/screensaver-proxy/screensaver-proxy.gnome-settings-plugin.in
 plugins/smartcard/gsd-smartcard.c
 plugins/smartcard/gsd-smartcard-manager.c



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]