[gnome-bluetooth] lib: Add RfkillGlib object to do the dirty work



commit a5f6638dd8dd2df7f25848d859140d78cb6eac66
Author: Bastien Nocera <hadess hadess net>
Date:   Fri Nov 9 09:52:44 2012 +0100

    lib: Add RfkillGlib object to do the dirty work
    
    Opening /dev/rfkill, setting up fd monitoring, passing on the
    events from the rfkill sub-system to code that cares about
    specific types of killswitches.

 lib/Makefile.am   |    4 +-
 lib/rfkill-glib.c |  297 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/rfkill-glib.h |   65 ++++++++++++
 3 files changed, 365 insertions(+), 1 deletions(-)
---
diff --git a/lib/Makefile.am b/lib/Makefile.am
index a9f5d10..b5faec6 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -17,7 +17,8 @@ libgnome_bluetooth_c_sources =		\
 	bluetooth-chooser-button.c	\
 	bluetooth-chooser-combo.c	\
 	bluetooth-client-glue.c		\
-	bluetooth-plugin-manager.c
+	bluetooth-plugin-manager.c	\
+	rfkill-glib.c
 
 libgnome_bluetooth_private_headers =	\
 	bluetooth-client-private.h	\
@@ -25,6 +26,7 @@ libgnome_bluetooth_private_headers =	\
 	gnome-bluetooth-enum-types.h	\
 	bluetooth-chooser-private.h	\
 	bluetooth-client-glue.h		\
+	rfkill-glib.h			\
 	rfkill.h
 
 # public headers don't need to be listed, are handled by _HEADERS
diff --git a/lib/rfkill-glib.c b/lib/rfkill-glib.c
new file mode 100644
index 0000000..97c8b0f
--- /dev/null
+++ b/lib/rfkill-glib.c
@@ -0,0 +1,297 @@
+/*
+ *
+ *  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 "rfkill-glib.h"
+
+enum {
+	CHANGED,
+	LAST_SIGNAL
+};
+
+static int signals[LAST_SIGNAL] = { 0 };
+
+#define RFKILL_GLIB_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
+				RFKILL_TYPE_GLIB, RfkillGlibPrivate))
+
+struct RfkillGlibPrivate {
+	int fd;
+	GIOChannel *channel;
+	guint watch_id;
+};
+
+G_DEFINE_TYPE(RfkillGlib, rfkill_glib, G_TYPE_OBJECT)
+
+int
+rfkill_glib_send_event (RfkillGlib *rfkill, struct rfkill_event *event)
+{
+	g_return_val_if_fail (RFKILL_IS_GLIB (rfkill), -1);
+	g_return_val_if_fail (rfkill->priv->fd > 0, -1);
+
+	return write (rfkill->priv->fd, event, sizeof(struct rfkill_event));
+}
+
+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:
+		g_assert_not_reached ();
+	}
+}
+
+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 (RfkillGlib *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,
+	  RfkillGlib   *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);
+		}
+	} else {
+		g_debug ("something else happened");
+		return FALSE;
+	}
+
+	emit_changed_signal_and_free (rfkill, events);
+
+	return TRUE;
+}
+
+static void
+rfkill_glib_init (RfkillGlib *rfkill)
+{
+	RfkillGlibPrivate *priv;
+
+	priv = RFKILL_GLIB_GET_PRIVATE (rfkill);
+	rfkill->priv = priv;
+	rfkill->priv->fd = -1;
+}
+
+int
+rfkill_glib_open (RfkillGlib *rfkill)
+{
+	RfkillGlibPrivate *priv;
+	int fd;
+	int ret;
+	GList *events;
+
+	g_return_val_if_fail (RFKILL_IS_GLIB (rfkill), -1);
+	g_return_val_if_fail (rfkill->priv->fd == -1, -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->fd = fd;
+	priv->channel = g_io_channel_unix_new (priv->fd);
+	priv->watch_id = g_io_add_watch (priv->channel,
+					 G_IO_IN | G_IO_HUP | G_IO_ERR,
+					 (GIOFunc) event_cb,
+					 rfkill);
+
+	emit_changed_signal_and_free (rfkill, events);
+
+	return fd;
+}
+
+static void
+rfkill_glib_finalize (GObject *object)
+{
+	RfkillGlib *rfkill;
+	RfkillGlibPrivate *priv;
+
+	rfkill = 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);
+	}
+	close(priv->fd);
+	priv->fd = -1;
+
+	G_OBJECT_CLASS(rfkill_glib_parent_class)->finalize(object);
+}
+
+static void
+rfkill_glib_class_init(RfkillGlibClass *klass)
+{
+	GObjectClass *object_class = (GObjectClass *) klass;
+
+	g_type_class_add_private(klass, sizeof(RfkillGlibPrivate));
+	object_class->finalize = rfkill_glib_finalize;
+
+	signals[CHANGED] =
+		g_signal_new ("changed",
+			      G_TYPE_FROM_CLASS (klass),
+			      G_SIGNAL_RUN_LAST,
+			      G_STRUCT_OFFSET (RfkillGlibClass, changed),
+			      NULL, NULL,
+			      NULL,
+			      G_TYPE_NONE, 1, G_TYPE_POINTER);
+
+}
+
+RfkillGlib *
+rfkill_glib_new (void)
+{
+	return RFKILL_GLIB (g_object_new (RFKILL_TYPE_GLIB, NULL));
+}
diff --git a/lib/rfkill-glib.h b/lib/rfkill-glib.h
new file mode 100644
index 0000000..23f3df6
--- /dev/null
+++ b/lib/rfkill-glib.h
@@ -0,0 +1,65 @@
+/*
+ *
+ *  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 __RFKILL_GLIB_H
+#define __RFKILL_GLIB_H
+
+#include <glib-object.h>
+#include "rfkill.h"
+
+G_BEGIN_DECLS
+
+#define RFKILL_TYPE_GLIB (rfkill_glib_get_type())
+#define RFKILL_GLIB(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \
+		RFKILL_TYPE_GLIB, RfkillGlib))
+#define RFKILL_GLIB_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), \
+		RFKILL_TYPE_GLIB, RfkillGlibClass))
+#define RFKILL_IS_GLIB(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), \
+		RFKILL_TYPE_GLIB))
+#define RFKILL_IS_GLIB_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), \
+		RFKILL_TYPE_GLIB))
+#define RFKILL_GET_GLIB_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), \
+		RFKILL_TYPE_GLIB, RfkillGlibClass))
+
+typedef struct RfkillGlibPrivate RfkillGlibPrivate;
+
+typedef struct _RfkillGlib {
+	GObject parent;
+	RfkillGlibPrivate *priv;
+} RfkillGlib;
+
+typedef struct _RfkillGlibClass {
+	GObjectClass parent_class;
+
+	void (*changed) (RfkillGlib *rfkill, GList *events);
+} RfkillGlibClass;
+
+GType rfkill_glib_get_type(void);
+
+RfkillGlib *rfkill_glib_new (void);
+int rfkill_glib_open (RfkillGlib *rfkill);
+int rfkill_glib_send_event (RfkillGlib *rfkill, struct rfkill_event *event);
+
+G_END_DECLS
+
+#endif /* __RFKILL_GLIB_H */



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