blob: 33445439c97179d589d12203111a6f588ad24282 [file] [log] [blame]
brians0ab60bb2013-01-31 02:21:51 +00001#!/usr/bin/python
2
3global DeviceName, AddAction, RemoveAction
4DeviceName = 'usb_device_46d_c408_noserial' # Logitech Trackball
5AddAction = 'xmodmap -e "pointer = 3 2 1 4 5 6 7 9 8"'
6RemoveAction = 'xmodmap -e "pointer = 1 2 3 4 5 6 7 8 9"'
7
8import dbus # needed to do anything
9import dbus.decorators # needed to receive messages
10import dbus.glib # needed to receive messages
11import gobject # needed to loop & monitor
12import os # needed to
13
14def add_device(*args, **keywords):
15 Path = args[0].split('/')
16 if Path[-1] == DeviceName: # Device found
17 os.system(AddAction)
18
19def remove_device(*args, **keywords):
20 Path = args[0].split('/')
21 if Path[-1] == DeviceName: # Device found
22 os.system(RemoveAction)
23
24bus = dbus.SystemBus() # connect to system bus
25hal_manager_obj = bus.get_object('org.freedesktop.Hal', '/org/freedesktop/Hal/Manager')
26hal_manager = dbus.Interface(hal_manager_obj, 'org.freedesktop.Hal.Manager')
27
28# Add listeners for all devices being added or removed
29bus.add_signal_receiver(add_device, 'DeviceAdded', 'org.freedesktop.Hal.Manager',
30 'org.freedesktop.Hal', '/org/freedesktop/Hal/Manager')
31bus.add_signal_receiver(remove_device, 'DeviceRemoved', 'org.freedesktop.Hal.Manager',
32 'org.freedesktop.Hal', '/org/freedesktop/Hal/Manager')
33
34# get list of all devices, determine if device is connected
35device_names = hal_manager.GetAllDevices()
36for name in device_names:
37 Path = name.split('/')
38 if Path[-1] == DeviceName: # Device found
39 os.system(AddAction)
40 break # no need to keep looking
41
42# monitor
43loop = gobject.MainLoop()
44loop.run()