fixes to language activation callbacks and using the callbacks in mytest for setting...
[enigma2.git] / mytest.py
1 from Tools import RedirectOutput
2 from enigma import *
3 from tools import *
4
5 from Components.Language import language
6
7 def setEPGLanguage():
8         #eServiceEvent.setEPGLanguage(language.getLanguage())
9         print "language set to", language.getLanguage()
10         
11 language.addCallback(setEPGLanguage)
12
13 import traceback
14 import Screens.InfoBar
15 from Screens.SimpleSummary import SimpleSummary
16
17 import sys
18 import time
19
20 import ServiceReference
21
22 from Navigation import Navigation
23
24 from skin import readSkin, applyAllAttributes
25
26 from Components.config import configfile
27 from Tools.Directories import InitFallbackFiles, resolveFilename, SCOPE_PLUGINS
28 InitFallbackFiles()
29 eDVBDB.getInstance().reloadBouquets()
30
31 try:
32         import e2reactor
33         e2reactor.install()
34         
35         from twisted.internet import reactor
36         
37         def runReactor():
38                 reactor.run()
39 except ImportError:
40         print "twisted not available"
41         def runReactor():
42                 runMainloop()
43
44 # initialize autorun plugins and plugin menu entries
45 from Components.PluginComponent import plugins
46
47 from Screens.Wizard import wizardManager
48 from Screens.ImageWizard import *
49 from Screens.StartWizard import *
50 from Screens.TutorialWizard import *
51 from Tools.BoundFunction import boundFunction
52 from Plugins.Plugin import PluginDescriptor
53
54 had = dict()
55
56 def dump(dir, p = ""):
57         if isinstance(dir, dict):
58                 for (entry, val) in dir.items():
59                         dump(val, p + "(dict)/" + entry)
60         if hasattr(dir, "__dict__"):
61                 for name, value in dir.__dict__.items():
62                         if not had.has_key(str(value)):
63                                 had[str(value)] = 1
64                                 dump(value, p + "/" + str(name))
65                         else:
66                                 print p + "/" + str(name) + ":" + str(dir.__class__) + "(cycle)"
67         else:
68                 print p + ":" + str(dir)
69
70 # + ":" + str(dir.__class__)
71
72 # display
73
74 class OutputDevice:
75         def create(self, screen): pass
76
77 # display: HTML
78
79 class HTMLOutputDevice(OutputDevice):
80         def create(self, comp):
81                 print comp.produceHTML()
82
83 html = HTMLOutputDevice()
84
85 class GUIOutputDevice(OutputDevice):
86         parent = None
87         def create(self, comp, desktop):
88                 comp.createGUIScreen(self.parent, desktop)
89
90 class Session:
91         def __init__(self, desktop = None, summary_desktop = None, navigation = None):
92                 self.desktop = desktop
93                 self.summary_desktop = summary_desktop
94                 self.nav = navigation
95                 self.delay_timer = eTimer()
96                 self.delay_timer.timeout.get().append(self.processDelay)
97                 
98                 self.current_dialog = None
99                 
100                 self.dialog_stack = [ ]
101                 self.summary_stack = [ ]
102                 self.summary = None
103                 
104                 for p in plugins.getPlugins(PluginDescriptor.WHERE_SESSIONSTART):
105                         p(reason=0, session=self)
106         
107         def processDelay(self):
108                 self.execEnd()
109                 
110                 callback = self.current_dialog.callback
111
112                 retval = self.current_dialog.returnValue
113
114                 if self.current_dialog.isTmp:
115                         self.current_dialog.doClose()
116 #                       dump(self.current_dialog)
117                         del self.current_dialog
118                 else:
119                         del self.current_dialog.callback
120                 
121                 self.popCurrent()
122                 if callback is not None:
123                         callback(*retval)
124
125         def execBegin(self):
126                 c = self.current_dialog
127                 
128                 self.pushSummary()
129
130                 summary = c.createSummary() or SimpleSummary
131                 self.summary = self.instantiateSummaryDialog(summary, c)
132                 self.summary.show()
133
134                 c.addSummary(self.summary)
135                 c.execBegin()
136
137                 # when execBegin opened a new dialog, don't bother showing the old one.
138                 if c == self.current_dialog:
139                         c.show()
140                 
141         def execEnd(self):
142                 self.current_dialog.execEnd()
143                 self.current_dialog.hide()
144                 self.current_dialog.removeSummary(self.summary)
145                 self.popSummary()
146         
147         def create(self, screen, arguments, **kwargs):
148                 # creates an instance of 'screen' (which is a class)
149                 try:
150                         return screen(self, *arguments, **kwargs)
151                 except:
152                         errstr = "Screen %s(%s, %s): %s" % (str(screen), str(arguments), str(kwargs), sys.exc_info()[0])
153                         print errstr
154                         traceback.print_exc(file=sys.stdout)
155                         quitMainloop(5)
156         
157         def instantiateDialog(self, screen, *arguments, **kwargs):
158                 return self.doInstantiateDialog(screen, arguments, kwargs, self.desktop)
159         
160         def instantiateSummaryDialog(self, screen, *arguments, **kwargs):
161                 return self.doInstantiateDialog(screen, arguments, kwargs, self.summary_desktop)
162         
163         def doInstantiateDialog(self, screen, arguments, kwargs, desktop):
164                 # create dialog
165                 
166                 try:
167                         dlg = self.create(screen, arguments, **kwargs)
168                 except:
169                         print 'EXCEPTION IN DIALOG INIT CODE, ABORTING:'
170                         print '-'*60
171                         traceback.print_exc(file=sys.stdout)
172                         quitMainloop(5)
173                         print '-'*60
174                 
175                 if dlg is None:
176                         return
177
178                 # read skin data
179                 readSkin(dlg, None, dlg.skinName, desktop)
180
181                 # create GUI view of this dialog
182                 assert desktop is not None
183                 
184                 z = 0
185                 title = ""
186                 for (key, value) in dlg.skinAttributes:
187                         if key == "zPosition":
188                                 z = int(value)
189                         elif key == "title":
190                                 title = value
191                 
192                 dlg.instance = eWindow(desktop, z)
193                 dlg.title = title
194                 applyAllAttributes(dlg.instance, desktop, dlg.skinAttributes)
195                 gui = GUIOutputDevice()
196                 gui.parent = dlg.instance
197                 gui.create(dlg, desktop)
198                 
199                 return dlg
200          
201         def pushCurrent(self):
202                 if self.current_dialog:
203                         self.dialog_stack.append(self.current_dialog)
204                         self.execEnd()
205         
206         def popCurrent(self):
207                 if len(self.dialog_stack):
208                         self.current_dialog = self.dialog_stack.pop()
209                         self.execBegin()
210                 else:
211                         self.current_dialog = None
212
213         def execDialog(self, dialog):
214                 self.pushCurrent()
215                 self.current_dialog = dialog
216                 self.current_dialog.isTmp = False
217                 self.current_dialog.callback = None # would cause re-entrancy problems.
218                 self.execBegin()
219
220         def openWithCallback(self, callback, screen, *arguments, **kwargs):
221                 dlg = self.open(screen, *arguments, **kwargs)
222                 dlg.callback = callback
223
224         def open(self, screen, *arguments, **kwargs):
225                 self.pushCurrent()
226                 dlg = self.current_dialog = self.instantiateDialog(screen, *arguments, **kwargs)
227                 dlg.isTmp = True
228                 dlg.callback = None
229                 self.execBegin()
230                 return dlg
231
232         def keyEvent(self, code):
233                 print "code " + str(code)
234
235         def close(self, *retval):
236                 self.current_dialog.returnValue = retval
237                 self.delay_timer.start(0, 1)
238
239         def pushSummary(self):
240                 if self.summary is not None:
241                         self.summary.hide()
242                 self.summary_stack.append(self.summary)
243                 self.summary = None
244
245         def popSummary(self):
246                 if self.summary is not None:
247                         self.summary.doClose()
248                 self.summary = self.summary_stack.pop()
249                 if self.summary is not None:
250                         self.summary.show()
251
252 from Screens.Volume import Volume
253 from Screens.Mute import Mute
254 from GlobalActions import globalActionMap
255 from Components.config import ConfigSubsection, configSequence, configElement, configsequencearg
256
257 #TODO .. move this to a own .py file
258 class VolumeControl:
259         """Volume control, handles volUp, volDown, volMute actions and display
260         a corresponding dialog"""
261         def __init__(self, session):
262                 global globalActionMap
263                 globalActionMap.actions["volumeUp"]=self.volUp
264                 globalActionMap.actions["volumeDown"]=self.volDown
265                 globalActionMap.actions["volumeMute"]=self.volMute
266
267                 config.audio = ConfigSubsection()
268                 config.audio.volume = configElement("config.audio.volume", configSequence, [100], configsequencearg.get("INTEGER", (0, 100)))
269
270                 self.volumeDialog = session.instantiateDialog(Volume)
271                 self.muteDialog = session.instantiateDialog(Mute)
272
273                 self.hideVolTimer = eTimer()
274                 self.hideVolTimer.timeout.get().append(self.volHide)
275
276                 vol = config.audio.volume.value[0]
277                 self.volumeDialog.setValue(vol)
278                 eDVBVolumecontrol.getInstance().setVolume(vol, vol)
279
280         def volSave(self):
281                 config.audio.volume.value = eDVBVolumecontrol.getInstance().getVolume()
282                 config.audio.volume.save()
283
284         def     volUp(self):
285                 if (eDVBVolumecontrol.getInstance().isMuted()):
286                         self.volMute()
287                 eDVBVolumecontrol.getInstance().volumeUp()
288                 self.volumeDialog.show()
289                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
290                 self.volSave()
291                 self.hideVolTimer.start(3000, True)
292
293         def     volDown(self):
294                 if (eDVBVolumecontrol.getInstance().isMuted()):
295                         self.volMute()
296                 eDVBVolumecontrol.getInstance().volumeDown()
297                 self.volumeDialog.show()
298                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
299                 self.volSave()
300                 self.hideVolTimer.start(3000, True)
301
302         def volHide(self):
303                 self.volumeDialog.hide()
304
305         def     volMute(self):
306                 eDVBVolumecontrol.getInstance().volumeToggleMute()
307                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
308
309                 if (eDVBVolumecontrol.getInstance().isMuted()):
310                         self.muteDialog.show()
311                 else:
312                         self.muteDialog.hide()
313
314 def runScreenTest():
315         plugins.readPluginList(resolveFilename(SCOPE_PLUGINS))
316
317         session = Session(desktop = getDesktop(0), summary_desktop = getDesktop(1), navigation = Navigation())
318         
319         screensToRun = [ ]
320         
321         for p in plugins.getPlugins(PluginDescriptor.WHERE_WIZARD):
322                 screensToRun.append(p.__call__)
323         
324         screensToRun += wizardManager.getWizards()
325         
326         screensToRun.append(Screens.InfoBar.InfoBar)
327
328         def runNextScreen(session, screensToRun, *result):
329                 if result:
330                         quitMainloop(*result)
331                         return
332         
333                 screen = screensToRun[0]
334                 
335                 if len(screensToRun):
336                         session.openWithCallback(boundFunction(runNextScreen, session, screensToRun[1:]), screen)
337                 else:
338                         session.open(screen)
339         
340         runNextScreen(session, screensToRun)
341         
342         CONNECT(keyPressedSignal(), session.keyEvent)
343         
344         vol = VolumeControl(session)
345         
346         runReactor()
347         
348         configfile.save()
349         
350         from Tools.DreamboxHardware import setFPWakeuptime
351         from time import time
352         nextRecordingTime = session.nav.RecordTimer.getNextRecordingTime()
353         if nextRecordingTime != -1:
354                 if (nextRecordingTime - time() < 330): # no time to switch box back on
355                         setFPWakeuptime(time() + 30) # so switch back on in 30 seconds
356                 else:
357                         setFPWakeuptime(nextRecordingTime - (300))
358         
359         session.nav.shutdown()
360         
361         return 0
362
363 import keymapparser
364 keymapparser.readKeymap()
365 import skin
366 skin.loadSkin(getDesktop(0))
367
368 import Components.InputDevice
369 Components.InputDevice.InitInputDevices()
370
371 import Components.AVSwitch
372 Components.AVSwitch.InitAVSwitch()
373
374 import Components.RecordingConfig
375 Components.RecordingConfig.InitRecordingConfig()
376
377 import Components.UsageConfig
378 Components.UsageConfig.InitUsageConfig()
379
380 import Components.Network
381 Components.Network.InitNetwork()
382
383 import Components.Lcd
384 Components.Lcd.InitLcd()
385
386 import Components.SetupDevices
387 Components.SetupDevices.InitSetupDevices()
388
389 import Components.RFmod
390 Components.RFmod.InitRFmod()
391
392 import Components.NimManager
393
394 # first, setup a screen
395 try:
396         runScreenTest()
397
398         plugins.shutdown()
399 except:
400         print 'EXCEPTION IN PYTHON STARTUP CODE:'
401         print '-'*60
402         traceback.print_exc(file=sys.stdout)
403         quitMainloop(5)
404         print '-'*60