restart service after leaving the media player
[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         print "language set to", language.getLanguage()
9         eServiceEvent.setEPGLanguage(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 from Screens.Standby import Standby
315
316 class PowerKey:
317         """ PowerKey stuff - handles the powerkey press and powerkey release actions"""
318         
319         def __init__(self, session):
320                 self.session = session
321                 self.powerKeyTimer = eTimer()
322                 self.powerKeyTimer.timeout.get().append(self.powertimer)
323                 globalActionMap.actions["powerdown"]=self.powerdown
324                 globalActionMap.actions["powerup"]=self.powerup
325                 
326 #               self["PowerKeyActions"] = HelpableActionMap(self, "PowerKeyActions",
327                         #{
328                                 #"powerdown": self.powerdown,
329                                 #"powerup": self.powerup,
330                                 #"discreteStandby": (self.standby, "Go standby"),
331                                 #"discretePowerOff": (self.quit, "Go to deep standby"),
332                         #})
333
334         def powertimer(self):   
335                 print "PowerOff - Now!"
336                 self.quit()
337         
338         def powerdown(self):
339                 self.standbyblocked = 0
340                 self.powerKeyTimer.start(3000, True)
341
342         def powerup(self):
343                 self.powerKeyTimer.stop()
344                 if self.standbyblocked == 0:
345                         self.standbyblocked = 1
346                         self.standby()
347
348         def standby(self):
349                 self.session.open(Standby, self)
350
351         def quit(self):
352                 # halt
353                 quitMainloop(1)
354
355 def runScreenTest():
356         plugins.readPluginList(resolveFilename(SCOPE_PLUGINS))
357
358         session = Session(desktop = getDesktop(0), summary_desktop = getDesktop(1), navigation = Navigation())
359         
360         screensToRun = [ ]
361         
362         for p in plugins.getPlugins(PluginDescriptor.WHERE_WIZARD):
363                 screensToRun.append(p.__call__)
364         
365         screensToRun += wizardManager.getWizards()
366         
367         screensToRun.append(Screens.InfoBar.InfoBar)
368
369         def runNextScreen(session, screensToRun, *result):
370                 if result:
371                         quitMainloop(*result)
372                         return
373         
374                 screen = screensToRun[0]
375                 
376                 if len(screensToRun):
377                         session.openWithCallback(boundFunction(runNextScreen, session, screensToRun[1:]), screen)
378                 else:
379                         session.open(screen)
380         
381         runNextScreen(session, screensToRun)
382         
383         CONNECT(keyPressedSignal(), session.keyEvent)
384         
385         vol = VolumeControl(session)
386         power = PowerKey(session)
387         
388         runReactor()
389         
390         configfile.save()
391         
392         from Tools.DreamboxHardware import setFPWakeuptime
393         from time import time
394         nextRecordingTime = session.nav.RecordTimer.getNextRecordingTime()
395         if nextRecordingTime != -1:
396                 if (nextRecordingTime - time() < 330): # no time to switch box back on
397                         setFPWakeuptime(time() + 30) # so switch back on in 30 seconds
398                 else:
399                         setFPWakeuptime(nextRecordingTime - (300))
400         
401         session.nav.shutdown()
402         
403         return 0
404
405 import keymapparser
406 keymapparser.readKeymap()
407 import skin
408 skin.loadSkinData(getDesktop(0))
409
410 import Components.InputDevice
411 Components.InputDevice.InitInputDevices()
412
413 import Components.AVSwitch
414 Components.AVSwitch.InitAVSwitch()
415
416 import Components.RecordingConfig
417 Components.RecordingConfig.InitRecordingConfig()
418
419 import Components.UsageConfig
420 Components.UsageConfig.InitUsageConfig()
421
422 import Components.Network
423 Components.Network.InitNetwork()
424
425 import Components.Lcd
426 Components.Lcd.InitLcd()
427
428 import Components.SetupDevices
429 Components.SetupDevices.InitSetupDevices()
430
431 import Components.RFmod
432 Components.RFmod.InitRFmod()
433
434 import Components.NimManager
435
436 # first, setup a screen
437 try:
438         runScreenTest()
439
440         plugins.shutdown()
441 except:
442         print 'EXCEPTION IN PYTHON STARTUP CODE:'
443         print '-'*60
444         traceback.print_exc(file=sys.stdout)
445         quitMainloop(5)
446         print '-'*60