op_power: handle screensaver too
[openpandora.oe.git] / recipes / pandora-system / pandora-scripts / op_nubmode.py
1 #!/usr/bin/python
2
3 import os
4 import re
5 import sys
6 import gtk
7 import time
8 import optparse
9
10 # ================================================================
11
12 GUI_DESCRIPTION = 'nubmode.glade'
13 PROFILES = '/etc/pandora/conf/nub_profiles.conf'
14
15 # Shell command to reset nub: 3-0066 = left-nub, 3-0067 = right-nub
16 # apparently they are linked and resetting one resets both.
17 #RESET_CMD = 'echo %i > /sys/bus/i2c/drivers/vsense/3-00%i/reset'
18 RESET_CMD = "sudo /usr/pandora/scripts/reset_nubs.sh"
19
20 # Valid values for mode setting
21 MODES = ("mouse", "mbuttons", "scroll", "absolute")
22
23 # Paths for reading and writing different configuration options %i -> 0 | 1
24 SETTINGS = dict(
25     mode='/proc/pandora/nub%s/mode',
26     mouse='/proc/pandora/nub%s/mouse_sensitivity',
27     button='/proc/pandora/nub%s/mbutton_threshold',
28     rate='/proc/pandora/nub%s/scroll_rate',
29     scrollx='/proc/pandora/nub%s/scrollx_sensitivity',
30     scrolly='/proc/pandora/nub%s/scrolly_sensitivity',
31 )
32
33 # format for saving/loading
34 FILE_ORDER = ("mode", "mouse", "button", "rate", "scrollx", "scrolly")
35
36 # Default configuration
37 DEFAULT_PROFILENAME = "Default"
38 DEFAULT_DICT = dict(mode0='mouse', mode1='mbuttons', mouse0='150', mouse1='150',
39                     button0='20', button1='20', rate0='20', rate1='20',
40                     scrollx0='7', scrollx1='7', scrolly0='7', scrolly1='7')
41
42 # Types of model changes for view updates 
43 (MODEL_PROFILE_CHANGE, MODEL_VALUE_CHANGE) = range(2)
44
45 # Documentation for commandline options
46 HELP_RESET = "Reset specified nub(s). Format: left,right"
47 HELP_LEFT_NUB = ("Configure left nub. Include -a flag to activate. Format: %s. E.g. %s" %
48                  (' '.join(FILE_ORDER),
49                   ' '.join(DEFAULT_DICT[k+'0'] for k in FILE_ORDER)))
50 HELP_RIGHT_NUB = ("Configure right nub. Include -a flag to activate. Format: %s. E.g. %s" %
51                   (' '.join(FILE_ORDER),
52                    ' '.join(DEFAULT_DICT[k+'1'] for k in FILE_ORDER)))
53 HELP_SAVE = "Store current configuration as specified profile (no spaces allowed)"
54 HELP_APPLY = "Write currently loaded configuration to nubs."
55 HELP_LOAD = "Load and apply specified nub configuration profile"
56 HELP_DEL = "Delete specified nub configuration profile"
57
58 # Commandline input type checking
59 TYPECHECK = dict(mode='(%s)' % '|'.join(MODES), 
60                  mouse='(\d+)', button='(\d+)',
61                  rate='(\d+)', scrollx='(\d+)', scrolly='(\d+)')
62 RE_FORMAT = ','.join(TYPECHECK[k] for k in FILE_ORDER)
63 BOUNDS = dict(mouse=(50, 300), button=(1, 40), rate=(1, 40),
64               scrollx=(-32,32), scrolly=(-32,32))
65
66 # ================================================================
67 # There is a bug in setting scrollx/scrolly sensitivity in the
68 # firmware (to be fixed in hotfix 6). 
69 # Detect and set FIX_SCROLLXY_BUG to use a workaround.
70
71 def ReadWriteTest(value=None):
72     with open(SETTINGS['scrollx'] % 0, 'w' if value else 'r') as f:
73         return f.write('%s\n' % value) if value else f.readline().rstrip()
74
75 tmp = int(ReadWriteTest())                       # backup original
76 ReadWriteTest(tmp + (-1 if tmp < 0 else 1))      # write corrected value
77 FIX_SCROLLXY_BUG = int(ReadWriteTest()) == tmp   # fix bug if equal to original
78 ReadWriteTest(tmp + ((-1 if tmp < 0 else 1) if FIX_SCROLLXY_BUG else 0)) # restore
79
80 # ================================================================
81
82 def ReadProc():
83     config = {}
84     for key, value in SETTINGS.iteritems():
85         for c in '01':
86             with open(value % c, 'r') as f:
87                 config[key+c] = f.readline().strip()
88     return config
89
90 def StoreProc(dictionary):
91     # fix for value decrement/increment after written
92     if FIX_SCROLLXY_BUG:
93         dictionary = dictionary.copy()
94         for key in ('scrollx0', 'scrolly0', 'scrollx1', 'scrolly1'):
95             value = int(dictionary[key])
96             value += -1 if value < 0 else 1
97             dictionary[key] = str(value)
98     for key, value in SETTINGS.iteritems():
99         for c in '01':
100             with open(value % c, 'w') as f:
101                 f.write('%s\n' % dictionary[key+c])
102
103 def ProfileToString(dictionary):
104     return ' '.join(dictionary[k+c] for c in '01' for k in FILE_ORDER)
105
106 def StringToProfile(line):
107     return dict(zip((k+c for c in '01' for k in FILE_ORDER), line.split(' ')))
108
109 def Validate(value):
110     mo = re.match(RE_FORMAT, value)
111     if mo: # verify bounds
112         values = dict(zip(FILE_ORDER, mo.groups()))
113         if all(BOUNDS[k][0] <= int(values[k]) <= BOUNDS[k][1] for k in BOUNDS):
114             return values
115     return {}
116
117 # ================================================================
118
119 class NubModel(object):
120     def __init__(self, view=None):
121         self.views = [view] if view else []
122         self.settings = ReadProc()
123         self.profiles = {}
124         with open(PROFILES) as f:
125             name = None
126             for line in f:
127                 if name is None:
128                     name = line.rstrip()
129                 else:
130                     self.profiles[name] = StringToProfile(line.rstrip())
131                     name = None
132
133     def notify(self, reason, *args):
134         for v in self.views:
135             v.update_view(reason, *args)
136
137     def set_profile(self, name, dictionary):
138         notify = name not in self.profiles
139         self.profiles.setdefault(name, {}).update(dictionary)
140         if notify:
141             self.notify(MODEL_PROFILE_CHANGE, name, self.profiles)
142
143     def delete_profile(self, name):
144         notify = name in self.profiles
145         del self.profiles[name]
146         if notify:
147             self.notify(MODEL_PROFILE_CHANGE, '', self.profiles)
148
149     def load_profile(self, settings):
150         self.settings.update(settings)
151         self.notify(MODEL_VALUE_CHANGE, self.settings)
152
153     def load_named_profile(self, name):
154         notify = True
155         if name == DEFAULT_PROFILENAME:
156             self.settings.update(DEFAULT_DICT)
157         elif name in self.profiles:
158             self.settings.update(self.profiles[name])
159         else:
160             notify = False
161         if notify:
162             self.notify(MODEL_VALUE_CHANGE, self.settings)
163
164     def store_profiles(self, filename):
165         with open(filename, 'w') as f:
166             for name in sorted(self.profiles.keys()):
167                 f.write('%s\n%s\n' % (name, ProfileToString(self.profiles[name])))
168
169
170 class NubConfig(object):
171     """GUI application for modifying the pandora nub configuration"""
172     def __init__(self):
173         builder = gtk.Builder()
174         builder.add_from_file(
175             os.path.join(os.path.dirname(__file__), GUI_DESCRIPTION))
176         builder.connect_signals(self)
177
178         # slider widgets, more specifically: their Adjustment objects
179         self.widgets = {}
180         for s in DEFAULT_DICT:
181             w = builder.get_object(s)
182             if w is not None:
183                 w.connect('value-changed', self.on_slider_changed, s)
184                 self.widgets[s] = w
185
186         # radio buttons
187         for c in '01':
188             group = []
189             for m in MODES:
190                 w = builder.get_object('R%s%s' % (m,c))
191                 w.connect('clicked', self.on_radio_changed, 'mode'+c, m)
192                 group.append(w)
193             self.widgets['mode'+c] = group
194
195         self.statusbar = builder.get_object('statusbar')
196         self.contextid = self.statusbar.get_context_id('')
197
198         self.model = NubModel(self)
199
200         self.profiles = gtk.ListStore(str)
201         self.profiles.append([DEFAULT_PROFILENAME])
202         for name in self.model.profiles:
203             self.profiles.append([name])
204
205         self.comboentry = builder.get_object('ProfileComboEntry')
206         self.comboentry.set_model(self.profiles)
207         self.comboentry.set_text_column(0)
208         self.comboentry.connect('changed', self.on_combo_changed)
209
210         self.entry = self.comboentry.get_child()
211         self.entry.connect('activate', self.on_LoadProfile_clicked)
212
213         # we need a reference to change their sensitive
214         # setting according to the profile
215         self.delete = builder.get_object('DeleteProfile')
216         self.save = builder.get_object('SaveProfile')
217
218         # this also changes sensitive of self.save & self.load
219         self.comboentry.set_active(0)
220
221         # read current config
222         self.model.load_profile(ReadProc())
223
224         self.window = builder.get_object("window")
225         self.window.show_all()
226
227         accelgrp = gtk.AccelGroup()
228         key, mod = gtk.accelerator_parse('<Control>Q')
229         accelgrp.connect_group(key, mod, 0, self.on_window_destroy)
230         self.window.add_accel_group(accelgrp)
231
232     def _update_profile_list(self, new, profiles):
233         self.profiles.clear()
234         self.profiles.append([DEFAULT_PROFILENAME])
235         activate = 0
236         for i, key in enumerate(sorted(profiles.keys())):
237             self.profiles.append([key])
238             if key == new:
239                 activate = i
240         self.comboentry.set_active(activate)
241             
242     def _update_widgets(self, settings):
243         for key, value in settings.iteritems():
244             if 'mode' in key:
245                 for w, m in zip(self.widgets[key], MODES):
246                     w.set_active(m == value)
247             else:
248                 self.widgets[key].value = int(value)
249
250     def update_view(self, reason, *data):
251         if reason == MODEL_PROFILE_CHANGE:
252             self._update_profile_list(*data)
253         elif reason == MODEL_VALUE_CHANGE:
254             self._update_widgets(*data)
255
256     def _lose_active(self):
257         # Forces the comboboxentry to lose its active selection
258         # such that it also generates a changed signal when we
259         # select the same value.
260         temp = self.entry.get_text()
261         self.entry.set_text('')
262         self.entry.set_text(temp)
263
264     def Notify(self, message):
265         self.statusbar.pop(self.contextid)
266         self.statusbar.push(self.contextid, message)
267
268     def on_slider_changed(self, widget, key):
269         self.model.settings[key] = str(int(widget.value))
270
271     def on_radio_changed(self, widget, key, mode):
272         self.model.settings[key] = mode
273
274     def on_combo_changed(self, widget, *data):
275         name = self.entry.get_text()
276         self.delete.set_sensitive(name in self.model.profiles)
277         self.save.set_sensitive(name != '' and name != DEFAULT_PROFILENAME)
278         if self.comboentry.get_active() != -1:
279             self.on_LoadProfile_clicked(None)
280
281     def on_ResetNubs_clicked(self, widget, *data):
282         self.Notify("Resetting the nubs...")
283         os.system(RESET_CMD)
284         self.Notify("Nubs reset.")
285
286     def on_ReadNubConfig_clicked(self, widget, *data):
287         self.model.load_profile(ReadProc())
288         self.Notify("Active nub configuration loaded.")
289
290     def on_WriteNubConfig_clicked(self, widget, *data):
291         StoreProc(self.model.settings)
292         self.Notify("Nub configuration updated.")
293
294     def on_SaveProfile_clicked(self, widget, *data):
295         name = self.entry.get_text()
296         if name == '' or name == DEFAULT_PROFILENAME:
297             self.Notify("Invalid profile name")
298         else:
299             self.model.set_profile(name, self.model.settings)
300             self.Notify("Profile saved as: %s" % name)
301
302     def on_LoadProfile_clicked(self, widget, *data):
303         self._lose_active()          # force widget to always emit changed signals,
304         name = self.entry.get_text() # ok since we always lookup by text value anyway
305         if not name:
306             return
307         elif name not in self.model.profiles and name != DEFAULT_PROFILENAME:
308             self.Notify("Cannot load profile, please select an existing profile.")
309         else:
310             self.model.load_named_profile(name)
311             self.Notify("Profile loaded, hit 'Write nub settings' to make it active")
312
313     def on_DeleteProfile_clicked(self, widget, *data):
314         name = self.entry.get_text()
315         if name not in self.model.profiles or name == DEFAULT_PROFILENAME:
316             self.model("Cannot remove profile, please select an existing non-default profile.")
317         else:
318             dialog = gtk.MessageDialog(flags=gtk.DIALOG_MODAL, type=gtk.MESSAGE_WARNING,
319                  buttons=gtk.BUTTONS_YES_NO,
320                  message_format="Are you sure you want to delete profile %s?" % name)
321             if dialog.run() == gtk.RESPONSE_YES:
322                 self.model.delete_profile(name)
323             dialog.destroy()
324         
325     def on_window_destroy(self, widget, *data):
326         self.Notify("Storing profiles...")
327         self.model.store_profiles(PROFILES)
328         gtk.main_quit()
329
330 # ================================================================
331
332 if __name__ == '__main__':
333     parser = optparse.OptionParser()
334     parser.add_option('--reset', default=False, action='store_true', help=HELP_RESET)
335     parser.add_option('-l', '--left_nub', default='', help=HELP_LEFT_NUB)
336     parser.add_option('-r', '--right_nub', default='', help=HELP_RIGHT_NUB)
337     parser.add_option('-s', '--save_profile', default='', help=HELP_SAVE)
338     parser.add_option('-a', '--apply', default=False, action='store_true', help=HELP_APPLY)
339     parser.add_option('-p', '--load_profile', default='', help=HELP_LOAD)
340     parser.add_option('-d', '--remove_profile', default='', help=HELP_DEL)
341     options, args = parser.parse_args()
342
343     if len(sys.argv) == 1: # no params: run gui app
344         app = NubConfig()
345         gtk.main()
346     else: # run command line app
347         model = NubModel()
348
349         if options.reset:
350             os.system(RESET_CMD)
351         for key, value in Validate(options.left_nub).iteritems():
352             model.settings[key+'0'] = value
353         for key, value in Validate(options.right_nub).iteritems():
354             model.settings[key+'1'] = value
355         if options.save_profile and options.save_profile != DEFAULT_PROFILENAME:
356             model.set_profile(options.save_profile, model.settings)
357         if options.apply:
358             StoreProc(model.settings)
359         if options.load_profile:
360             model.load_named_profile(options.load_profile)
361             StoreProc(model.settings)
362         if options.remove_profile:
363             model.delete_profile(options.remove_profile)
364         if options.save_profile or options.remove_profile:
365             model.store_profiles(PROFILES)