[gnome-bluetooth] lib: Use RfkillGlib in BluetoothKillswitch
- From: Bastien Nocera <hadess src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-bluetooth] lib: Use RfkillGlib in BluetoothKillswitch
- Date: Fri, 9 Nov 2012 09:11:32 +0000 (UTC)
commit 54c28d9eeb6461f9c15c048bb4ad2ff4c46eaf23
Author: Bastien Nocera <hadess hadess net>
Date: Fri Nov 9 09:57:08 2012 +0100
lib: Use RfkillGlib in BluetoothKillswitch
lib/bluetooth-killswitch.c | 232 +++++++++++---------------------------------
1 files changed, 59 insertions(+), 173 deletions(-)
---
diff --git a/lib/bluetooth-killswitch.c b/lib/bluetooth-killswitch.c
index 4ce63f5..5628a7a 100644
--- a/lib/bluetooth-killswitch.c
+++ b/lib/bluetooth-killswitch.c
@@ -36,17 +36,12 @@
#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.h"
-
#include "bluetooth-killswitch.h"
+#include "rfkill-glib.h"
enum {
STATE_CHANGED,
@@ -65,9 +60,8 @@ struct _BluetoothIndKillswitch {
};
struct _BluetoothKillswitchPrivate {
- int fd;
- GIOChannel *channel;
- guint watch_id;
+ RfkillGlib *rfkill;
+ gboolean in_init;
GList *killswitches; /* a GList of BluetoothIndKillswitch */
BluetoothKillswitchPrivate *priv;
};
@@ -167,7 +161,7 @@ bluetooth_killswitch_set_state (BluetoothKillswitch *killswitch,
else
g_assert_not_reached ();
- len = write (priv->fd, &event, sizeof(event));
+ len = rfkill_glib_send_event (priv->rfkill, &event);
if (len < 0)
g_warning ("Failed to change RFKILL state: %s",
g_strerror (errno));
@@ -262,61 +256,37 @@ add_killswitch (BluetoothKillswitch *killswitch,
priv->killswitches = g_list_append (priv->killswitches, ind);
}
-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 "BLUETOOTH";
- 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 gboolean
-event_cb (GIOChannel *source,
- GIOCondition condition,
- BluetoothKillswitch *killswitch)
+killswitch_changed (RfkillGlib *rfkill,
+ GList *events,
+ BluetoothKillswitch *killswitch)
{
BluetoothKillswitchState state;
gboolean changed;
+ GList *l;
+
+ if (killswitch->priv->in_init) {
+ for (l = events; l != NULL; l = l->next) {
+ struct rfkill_event *event = l->data;
+
+ if (event->op != RFKILL_OP_ADD)
+ continue;
+ if (event->type != RFKILL_TYPE_BLUETOOTH)
+ continue;
+
+ state = event_to_state (event->soft, event->hard);
+
+ g_debug ("Read killswitch (idx=%d): %s",
+ event->idx, bluetooth_killswitch_state_to_string (state));
+
+ add_killswitch (killswitch, event->idx, state);
+ }
+
+ g_signal_emit (G_OBJECT (killswitch),
+ signals[STATE_CHANGED],
+ 0, bluetooth_killswitch_get_state (killswitch));
+ return;
+ }
changed = FALSE;
@@ -324,48 +294,26 @@ event_cb (GIOChannel *source,
* see what really changed */
state = bluetooth_killswitch_get_state (killswitch);
- 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)) {
- if (event.type != RFKILL_TYPE_BLUETOOTH &&
- event.type != RFKILL_TYPE_ALL)
- goto carry_on;
-
- print_event (&event);
-
- if (event.op == RFKILL_OP_CHANGE) {
- update_killswitch (killswitch, event.idx, event.soft, event.hard);
- /* No changed here because update_killswitch
- * handles sending the state-changed signal */
- } else if (event.op == RFKILL_OP_DEL) {
- remove_killswitch (killswitch, event.idx);
- changed = TRUE;
- } else if (event.op == RFKILL_OP_ADD) {
- BluetoothKillswitchState state;
- state = event_to_state (event.soft, event.hard);
- add_killswitch (killswitch, event.idx, state);
- changed = TRUE;
- }
+ for (l = events; l != NULL; l = l->next) {
+ struct rfkill_event *event = l->data;
-carry_on:
- status = g_io_channel_read_chars (source,
- (char *) &event,
- sizeof(event),
- &read,
- NULL);
+ if (event->type != RFKILL_TYPE_BLUETOOTH &&
+ event->type != RFKILL_TYPE_ALL)
+ continue;
+
+ if (event->op == RFKILL_OP_CHANGE) {
+ update_killswitch (killswitch, event->idx, event->soft, event->hard);
+ /* No changed here because update_killswitch
+ * handles sending the state-changed signal */
+ } else if (event->op == RFKILL_OP_DEL) {
+ remove_killswitch (killswitch, event->idx);
+ changed = TRUE;
+ } else if (event->op == RFKILL_OP_ADD) {
+ BluetoothKillswitchState state;
+ state = event_to_state (event->soft, event->hard);
+ add_killswitch (killswitch, event->idx, state);
+ changed = TRUE;
}
- } else {
- g_debug ("something else happened");
- return FALSE;
}
if (changed) {
@@ -378,98 +326,37 @@ carry_on:
0, bluetooth_killswitch_get_state (killswitch));
}
}
-
- return TRUE;
}
static void
bluetooth_killswitch_init (BluetoothKillswitch *killswitch)
{
BluetoothKillswitchPrivate *priv;
- struct rfkill_event event;
- int fd;
priv = BLUETOOTH_KILLSWITCH_GET_PRIVATE (killswitch);
killswitch->priv = 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;
- }
+ priv->rfkill = rfkill_glib_new ();
+ g_signal_connect (priv->rfkill, "changed",
+ G_CALLBACK (killswitch_changed), killswitch);
- if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
- g_debug ("Can't set RFKILL control device to non-blocking");
- close(fd);
+ priv->in_init = TRUE;
+ if (rfkill_glib_open (priv->rfkill) < 0)
return;
- }
-
- while (1) {
- BluetoothKillswitchState state;
- 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;
- state = event_to_state (event.soft, event.hard);
-
- g_debug ("Read killswitch of type '%s' (idx=%d): %s",
- type_to_string (event.type),
- event.idx, bluetooth_killswitch_state_to_string (state));
-
- if (event.type != RFKILL_TYPE_BLUETOOTH)
- continue;
-
- add_killswitch (killswitch, event.idx, state);
- }
-
- /* 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,
- killswitch);
-
- g_signal_emit (G_OBJECT (killswitch),
- signals[STATE_CHANGED],
- 0, bluetooth_killswitch_get_state (killswitch));
+ priv->in_init = FALSE;
}
static void
bluetooth_killswitch_finalize (GObject *object)
{
BluetoothKillswitch *killswitch;
- BluetoothKillswitchPrivate *priv;
killswitch = BLUETOOTH_KILLSWITCH (object);
- priv = killswitch->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);
+ g_clear_object (&killswitch->priv->rfkill);
- g_list_foreach (priv->killswitches, (GFunc) g_free, NULL);
- g_list_free (priv->killswitches);
- priv->killswitches = NULL;
+ g_list_free_full (killswitch->priv->killswitches, g_free);
+ killswitch->priv->killswitches = NULL;
G_OBJECT_CLASS(bluetooth_killswitch_parent_class)->finalize(object);
}
@@ -496,6 +383,5 @@ bluetooth_killswitch_class_init(BluetoothKillswitchClass *klass)
BluetoothKillswitch *
bluetooth_killswitch_new (void)
{
- return BLUETOOTH_KILLSWITCH(g_object_new (BLUETOOTH_TYPE_KILLSWITCH, NULL));
+ return BLUETOOTH_KILLSWITCH (g_object_new (BLUETOOTH_TYPE_KILLSWITCH, NULL));
}
-
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]