brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | global DeviceName, AddAction, RemoveAction |
| 4 | DeviceName = 'usb_device_46d_c408_noserial' # Logitech Trackball |
| 5 | AddAction = 'xmodmap -e "pointer = 3 2 1 4 5 6 7 9 8"' |
| 6 | RemoveAction = 'xmodmap -e "pointer = 1 2 3 4 5 6 7 8 9"' |
| 7 | |
| 8 | import dbus # needed to do anything |
| 9 | import dbus.decorators # needed to receive messages |
| 10 | import dbus.glib # needed to receive messages |
| 11 | import gobject # needed to loop & monitor |
| 12 | import os # needed to |
| 13 | |
| 14 | def add_device(*args, **keywords): |
| 15 | Path = args[0].split('/') |
| 16 | if Path[-1] == DeviceName: # Device found |
| 17 | os.system(AddAction) |
| 18 | |
| 19 | def remove_device(*args, **keywords): |
| 20 | Path = args[0].split('/') |
| 21 | if Path[-1] == DeviceName: # Device found |
| 22 | os.system(RemoveAction) |
| 23 | |
| 24 | bus = dbus.SystemBus() # connect to system bus |
| 25 | hal_manager_obj = bus.get_object('org.freedesktop.Hal', '/org/freedesktop/Hal/Manager') |
| 26 | hal_manager = dbus.Interface(hal_manager_obj, 'org.freedesktop.Hal.Manager') |
| 27 | |
| 28 | # Add listeners for all devices being added or removed |
| 29 | bus.add_signal_receiver(add_device, 'DeviceAdded', 'org.freedesktop.Hal.Manager', |
| 30 | 'org.freedesktop.Hal', '/org/freedesktop/Hal/Manager') |
| 31 | bus.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 |
| 35 | device_names = hal_manager.GetAllDevices() |
| 36 | for 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 |
| 43 | loop = gobject.MainLoop() |
| 44 | loop.run() |