[wiican/haldeprecation] Halsectomy performed by gudev replacement



commit c13e41b625b6772410581308f03a73a72d961b5d
Author: J. Félix Ontañón <fontanon emergya es>
Date:   Sun Dec 12 14:49:29 2010 +0100

    Halsectomy performed by gudev replacement

 wiican/service/wiican_dbus.py |   40 +++++---------
 wiican/service/wiifinder.py   |  124 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 138 insertions(+), 26 deletions(-)
---
diff --git a/wiican/service/wiican_dbus.py b/wiican/service/wiican_dbus.py
index 037791b..2f1d5fc 100644
--- a/wiican/service/wiican_dbus.py
+++ b/wiican/service/wiican_dbus.py
@@ -21,8 +21,11 @@
 ###
 
 import os
+import gudev
 import dbus, dbus.service, dbus.exceptions
+
 from wminput import WMInputLauncher
+from wiifinder import WiimoteFinder
 
 from wiican.mapping import MappingValidator
 
@@ -32,18 +35,10 @@ WIICAN_PATH = '/org/gnome/Wiican'
 DBUS_URI = 'org.freedesktop.DBus'
 DBUS_PATH = '/org/freedesktop/DBus'
 
-HAL_URI = 'org.freedesktop.Hal'
-HAL_DEVICE_IFACE = 'org.freedesktop.Hal.Device'
-
 BLUEZ_PATH = '/'
 BLUEZ_URI = 'org.bluez'
 BLUEZMANAGER_IFACE = 'org.bluez.Manager'
 
-HAL_URI = 'org.freedesktop.Hal'
-HAL_DEVICE_IFACE = 'org.freedesktop.Hal.Device'
-HAL_MANAGER_URI = 'org.freedesktop.Hal.Manager'
-HAL_MANAGER_PATH = '/org/freedesktop/Hal/Manager'
-
 WC_DISABLED = 0
 WC_BLUEZ_PRESENT = 1
 WC_UINPUT_PRESENT = 2
@@ -60,7 +55,7 @@ class WiicanDBus(dbus.service.Object):
         self.bus = dbus.SessionBus()
         self.mainloop = loop
         self.status = 0
-        self.wiimote_udi = 0
+        self.wiimote_device = None
         self.wminput = None
         
         dbus.service.Object.__init__(self, dbus.service.BusName(WIICAN_URI, 
@@ -86,11 +81,10 @@ class WiicanDBus(dbus.service.Object):
         # Check for uinput module
         self.__check_uinput_present()
 
-        # Setup HAL
-        hal_manager_obj = bus.get_object(HAL_URI, HAL_MANAGER_PATH)
-        hal_manager = dbus.Interface(hal_manager_obj, HAL_MANAGER_URI)
-        hal_manager.connect_to_signal('DeviceAdded', self.__plug_cb)
-        hal_manager.connect_to_signal('DeviceRemoved', self.__unplug_cb)
+        # Setup Wiimote finder
+        self.wiimote_finder = WiimoteFinder()
+        self.wiimote_finder.connect('connected', self.__plug_cb)
+        self.wiimote_finder.connect('disconnected', self.__unplug_cb)
 
     def __wait_for_bluez(self, names, old_owner, new_owner):
         if BLUEZ_URI in names:
@@ -132,19 +126,13 @@ class WiicanDBus(dbus.service.Object):
             self.StatusChanged(self.status)
         return False
 
-    def __plug_cb(self, udi):
-        bus = dbus.SystemBus()
-        device_dbus_obj = bus.get_object(HAL_URI, udi)
-        properties = device_dbus_obj.GetAllProperties(dbus_interface=HAL_DEVICE_IFACE)
-
-        if properties.has_key('input.product') and 'Nintendo Wiimote' in \
-                properties['input.product']:
-            self.wiimote_udi = udi
-            self.status = self.status | WC_WIIMOTE_DISCOVERING
-            self.StatusChanged(self.status)
+    def __plug_cb(self, finder, wiimote):
+        self.wiimote_device = wiimote
+        self.status = self.status | WC_WIIMOTE_DISCOVERING
+        self.StatusChanged(self.status)
 
-    def __unplug_cb(self, udi):
-        if self.wiimote_udi == udi and (self.status & WC_WIIMOTE_DISCOVERING):
+    def __unplug_cb(self, finder, wiimote):
+        if self.wiimote_device.path == wiimote.path and (self.status & WC_WIIMOTE_DISCOVERING):
             self.status = self.status ^ WC_WIIMOTE_DISCOVERING
             self.StatusChanged(self.status)
 
diff --git a/wiican/service/wiifinder.py b/wiican/service/wiifinder.py
new file mode 100644
index 0000000..76350d1
--- /dev/null
+++ b/wiican/service/wiifinder.py
@@ -0,0 +1,124 @@
+# -*- coding: utf-8 -*-
+# vim: ts=4 
+###
+#
+# Copyright (c) 2010 J. Félix Ontañón
+#
+# Almost based on arista.inputs module:
+# Copyright 2008 - 2010 Daniel G. Taylor <dan programmer-art org>
+# 
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3 as
+# published by the Free Software Foundation
+#
+# 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, see <http://www.gnu.org/licenses/>.
+#
+# Authors : J. Félix Ontañón <fontanon emergya es>
+# 
+
+import gobject
+import gudev
+
+WIIMOTE_DEVICE_NAME = '"Nintendo Wiimote"'
+
+class WiimoteDevice(object):
+    """A simple object representing a Wiimote device."""
+    
+    def __init__(self, device):
+        """Create a new input Wiimote device
+            
+        @type device: gudev.Device
+        @param device: The device that we are using as wiimote
+        """
+        self.device = device
+
+    @property
+    def nice_label(self):
+        return self.device.get_sysfs_attr('name')
+    
+    @property
+    def path(self):
+        """Get the sysfs_path for this device
+            
+        @rtype: string
+        @return: The sysfs path
+        """
+        return self.device.get_sysfs_path()
+
+class WiimoteFinder(gobject.GObject):
+    """
+    An object that will find and monitor Wiimote devices on your 
+    machine and emit signals when are connected / disconnected
+    """
+    
+    __gsignals__ = {
+        'connected': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, 
+            (gobject.TYPE_PYOBJECT,)),
+        'disconnected': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, 
+            (gobject.TYPE_PYOBJECT,)),
+    }
+    
+    def __init__(self):
+        """
+        Create a new WiimoteFinder and attach to the udev system to 
+        listen for events.
+        """
+        self.__gobject_init__()
+
+        self.client = gudev.Client(['input'])
+        self.wiimotes = {}
+
+        for device in self.client.query_by_subsystem('input'):
+            if device.get_property('NAME') == WIIMOTE_DEVICE_NAME:
+                path = device.get_sysfs_path()
+                self.wiimotes[path] = WiimoteDevice(device)
+
+        self.client.connect('uevent', self.event)
+
+    def event(self, client, action, device):
+        """Handle a udev event"""
+
+        return {
+            "add": self.device_added,
+            "remove": self.device_removed,
+        }.get(action, lambda x,y: None)(device)
+    
+    def device_added(self, device):
+        """Called when a device has been added to the system"""
+        
+        if device.get_property('NAME') == WIIMOTE_DEVICE_NAME:
+            path = device.get_sysfs_path()
+            self.wiimotes[path] = WiimoteDevice(device)
+            self.emit('connected', self.wiimotes[path])
+
+    def device_removed(self, device):
+        """Called when a device has been removed from the system"""
+        
+        if device.get_property('NAME') == WIIMOTE_DEVICE_NAME:
+            path = device.get_sysfs_path()
+            self.emit('disconnected', self.wiimotes[path])
+            del(self.wiimotes[path])
+
+gobject.type_register(WiimoteFinder)
+
+if __name__ == "__main__":
+    import gobject
+    
+    def found(finder, device):
+        print device.path + ": " + device.nice_label
+    
+    def lost(finder, device):
+        print device.path + ": " + device.nice_label
+    
+    finder = WiimoteFinder()
+    finder.connect('connected', found)
+    finder.connect('disconnected', lost)
+    
+    loop = gobject.MainLoop()
+    loop.run()



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