OSDN Git Service

plugins/sixaxis: Add initial code for udev handling
authorSzymon Janc <szymon.janc@gmail.com>
Mon, 25 Nov 2013 22:15:49 +0000 (22:15 +0000)
committerJohan Hedberg <johan.hedberg@intel.com>
Wed, 27 Nov 2013 09:29:28 +0000 (11:29 +0200)
When new device is added plugin will be notified about it.

plugins/sixaxis.c

index bf66bef..7a5c6c2 100644 (file)
 #include <config.h>
 #endif
 
+#include <stddef.h>
+#include <errno.h>
+#include <glib.h>
+#include <libudev.h>
+
 #include "plugin.h"
 #include "log.h"
 
+static struct udev *ctx = NULL;
+static struct udev_monitor *monitor = NULL;
+static guint watch_id = 0;
+
+static void device_added(struct udev_device *udevice)
+{
+       DBG("");
+}
+
+static gboolean monitor_watch(GIOChannel *source, GIOCondition condition,
+                                                       gpointer data)
+{
+       struct udev_device *udevice;
+
+       udevice = udev_monitor_receive_device(monitor);
+       if (!udevice)
+               return TRUE;
+
+       if (!g_strcmp0(udev_device_get_action(udevice), "add"))
+               device_added(udevice);
+
+       udev_device_unref(udevice);
+
+       return TRUE;
+}
+
 static int sixaxis_init(void)
 {
+       GIOChannel *channel;
+
        DBG("");
 
+       ctx = udev_new();
+       if (!ctx)
+               return -EIO;
+
+       monitor = udev_monitor_new_from_netlink(ctx, "udev");
+       if (!monitor) {
+               udev_unref(ctx);
+               ctx = NULL;
+
+               return -EIO;
+       }
+
+       /* Listen for newly connected hidraw interfaces */
+       udev_monitor_filter_add_match_subsystem_devtype(monitor, "hidraw",
+                                                                       NULL);
+       udev_monitor_enable_receiving(monitor);
+
+       channel = g_io_channel_unix_new(udev_monitor_get_fd(monitor));
+       watch_id = g_io_add_watch(channel, G_IO_IN, monitor_watch, NULL);
+       g_io_channel_unref(channel);
+
        return 0;
 }
 
 static void sixaxis_exit(void)
 {
        DBG("");
+
+       g_source_remove(watch_id);
+       watch_id = 0;
+
+       udev_monitor_unref(monitor);
+       monitor = NULL;
+
+       udev_unref(ctx);
+       ctx = NULL;
 }
 
 BLUETOOTH_PLUGIN_DEFINE(sixaxis, VERSION, BLUETOOTH_PLUGIN_PRIORITY_LOW,