updated on Thu Jan 26 16:09:46 UTC 2012
[aur-mirror.git] / buttons-eee901 / buttons-eee901.py
blob313c4272e0a1f24c3306b61b1a85eeb8708e1d58
1 #!/usr/bin/env python
3 ##### HELPERS ################################################################
5 # !SET YOUR CONFIGURATION HERE!
7 # power button
8 def key_power(arg):
9 hbus = dbus.SystemBus()
10 hobj = hbus.get_object('org.freedesktop.Hal', '/org/freedesktop/Hal/devices/computer')
11 hifc = dbus.Interface(hobj, 'org.freedesktop.Hal.Device.SystemPowerManagement')
12 hifc.Shutdown()
14 # sleep button
15 def key_sleep(arg):
16 hbus = dbus.SystemBus()
17 hobj = hbus.get_object('org.freedesktop.Hal', '/org/freedesktop/Hal/devices/computer')
18 hifc = dbus.Interface(hobj, 'org.freedesktop.Hal.Device.SystemPowerManagement')
19 hifc.Suspend(0)
21 # lid
22 def key_lid(arg):
23 ec = os.popen('cat /proc/acpi/button/lid/LID/state','r').read().split(' ')[-1].split('\n')[0]
24 if ec == 'closed':
25 hbus = dbus.SystemBus()
26 hobj = hbus.get_object('org.freedesktop.Hal', '/org/freedesktop/Hal/devices/computer')
27 hifc = dbus.Interface(hobj, 'org.freedesktop.Hal.Device.SystemPowerManagement')
28 hifc.Suspend(0)
30 # wlan button
31 def key_wlan(arg):
32 # toggle bluetooth switch
33 hbus = dbus.SystemBus()
34 hobj = hbus.get_object('org.freedesktop.Hal', '/org/freedesktop/Hal/devices/computer_rfkill_eeepc_wlan_wlan')
35 hifc = dbus.Interface(hobj, 'org.freedesktop.Hal.Device.KillSwitch')
36 cs = hifc.GetPower()
37 if cs == 1:
38 ec = os.system(arg + ' start')
39 elif cs == 0:
40 ec = os.system(arg + ' stop')
42 # switch video mode
43 def key_swvid(arg):
44 global state_videomode
45 if state_videomode == 0:
46 # was in the internal mode, have to switch to external:
47 ec = os.system('xrandr --output LVDS --off --output VGA --auto &')
48 state_videomode = 1
49 elif state_videomode == 1:
50 # was in the external mode, have to switch to internal & external:
51 ec = os.system('xrandr --output VGA --auto --output LVDS --auto &')
52 state_videomode = 2
53 elif state_videomode == 2:
54 # was in the internal & external mode, have to switch to internal:
55 ec = os.system('xrandr --output LVDS --auto --output VGA --off &')
56 state_videomode = 0
58 # first silver button
59 def key_coffee(arg):
60 ec = os.system(arg)
62 # A/P button
63 def key_prog1(arg):
64 ec = os.system(arg)
66 # third silver button
67 def key_prog2(arg):
68 ec = os.system(arg)
70 # fourth silver button
71 def key_prog3(arg):
72 ec = os.system(arg)
74 # second silver button
75 def key_prog4(arg):
76 # toggle bluetooth switch
77 hbus = dbus.SystemBus()
78 hobj = hbus.get_object('org.freedesktop.Hal', '/org/freedesktop/Hal/devices/computer_rfkill_eeepc_bluetooth_bluetooth')
79 hifc = dbus.Interface(hobj, 'org.freedesktop.Hal.Device.KillSwitch')
80 cs = hifc.GetPower()
81 # now toggle bluetooth
82 hifc.SetPower((cs + 1) % 2)
84 # mute button
85 def key_mute(arg):
86 ec = os.system(arg)
88 # volume down
89 def key_voldn(arg):
90 ec = os.system(arg)
92 # volume up
93 def key_volup(arg):
94 ec = os.system(arg)
96 ##### END HELPERS ############################################################
98 ##### CALLBACKS ##############################################################
100 #def parse(s):
101 # # Parse a string into a dictionary.
102 # # Fetch a *copy* of the default dictionary.
103 # ret = DEFAULTDICT.copy()
104 # # Split lines.
105 # lines = s.split('\n')
106 # # Loop through lines.
107 # for line in lines:
108 # # Strip whitespace.
109 # line = line.strip()
110 # # Skip comment and blank lines.
111 # if line and line[0] != '#':
112 # # Split the line in the pair key, value
113 # values = line.split()
114 # # Fill dictionary.
115 # ret[values[0]] = int(values[1])
116 # # Return dictionary.
117 # return ret
119 def callback(kind, key):
120 """Handles Condition events from org.freedesktop.Hal.Device"""
121 if kind == 'ButtonPressed':
122 key = key.lower()
123 try:
124 KEYMAP[key][0](KEYMAP[key][1])
125 print 'Executed event for key: %s' % key
126 except KeyError:
127 print 'No event defined for key: %s' % key
129 ##### END CALLBACKS ##########################################################
131 import os, dbus, gobject
132 from dbus.mainloop.glib import DBusGMainLoop
133 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
135 ## Open file: filename: ~/.buttons-eee901-rc
136 #if os.path.exists('~/.buttons-eee901-rc') == True:
137 # f = file('~/.buttons-eee901-rc')
138 # try:
139 # # Read whole file as one string.
140 # data = f.read()
141 # finally:
142 # # Close file
143 # f.close()
144 # # Parse data string.
145 # config = parse(data)
147 # definition of keys
148 KEYMAP = {
149 'power' : (key_power, ('')),
150 'sleep' : (key_sleep, ('')),
151 'lid' : (key_lid, ('')),
152 'wlan' : (key_wlan, ('wifi')),
153 'switch-videomode' : (key_swvid, ('')),
154 'coffee' : (key_coffee, ('xset dpms force off &')),
155 'prog1' : (key_prog1, ('xterm -e top &')),
156 'prog2' : (key_prog2, ('firefox &')),
157 'prog3' : (key_prog3, ('sylpheed &')),
158 'prog4' : (key_prog4, ('')),
159 'mute' : (key_mute, ('amixer -q -c 0 set LineOut toggle &')),
160 'volume-down' : (key_volup, ('amixer -q -c 0 set LineOut 5%- &')),
161 'volume-up' : (key_voldn, ('amixer -q -c 0 set LineOut 5%+ &')),
164 # connect to the message bus
165 hal_1 = 'org.freedesktop.Hal'
166 hal_2 = '/org/freedesktop/Hal'
168 # video mode:
169 # 0: internal
170 # 1: external
171 # 2: internal & external
172 state_videomode = 0
174 # Connect to DBUS System Bus
175 hbus = dbus.SystemBus()
177 # Add an event handler: Condition
178 hbus.add_signal_receiver(callback,
179 dbus_interface=hal_1+".Device",
180 signal_name="Condition")
182 # Add an event handler: PropertyModified
183 #hbus.add_signal_receiver(button_pressed_cb,
184 # dbus_interface=hal_1+".Device",
185 # signal_name="PropertyModified")
187 # Add an event handler: DeviceAdded
188 #hbus.add_signal_receiver(button_pressed_cb,
189 # dbus_interface=hal_1+".Device",
190 # signal_name="DeviceAdded")
192 # Add an event handler: DeviceRemoved
193 #hbus.add_signal_receiver(button_pressed_cb,
194 # dbus_interface=hal_1+".Device",
195 # signal_name="DeviceRemoved")
197 # other interesting stuff
199 #hbus = dbus.SystemBus()
200 #hmgr = hbus.get_object(hal_1, hal_2+'/Manager')
201 #hifc1 = dbus.Interface(hmgr, hal_1+'.Manager')
202 #hudi = hifc1.FindDeviceByCapability('laptop_panel')
203 #hobj = hbus.get_object('org.freedesktop.Hal', hudi[0])
204 #hifc2 = dbus.Interface(hobj, 'org.freedesktop.Hal.Device.LaptopPanel')
205 #hifc3 = dbus.Interface(hobj, 'org.freedesktop.Hal.Device')
206 #print 100 * hifc2.GetBrightness() / (hifc3.GetProperty('laptop_panel.num_levels') - 1)
208 loop = gobject.MainLoop()
209 loop.run()