fix vc1 streamtype detection
[enigma2.git] / mytest.py
1 import eConsoleImpl
2 import eBaseImpl
3 import enigma
4 enigma.eTimer = eBaseImpl.eTimer
5 enigma.eSocketNotifier = eBaseImpl.eSocketNotifier
6 enigma.eConsoleAppContainer = eConsoleImpl.eConsoleAppContainer
7
8 from Tools.Profile import profile, profile_final
9
10 profile("PYTHON_START")
11
12 from enigma import runMainloop, eDVBDB, eTimer, quitMainloop, \
13         getDesktop, ePythonConfigQuery, eAVSwitch, eServiceEvent
14 from tools import *
15
16 profile("LANGUAGE")
17
18 from Components.Language import language
19
20 def setEPGLanguage():
21         print "language set to", language.getLanguage()
22         eServiceEvent.setEPGLanguage(language.getLanguage())
23
24 language.addCallback(setEPGLanguage)
25
26 from traceback import print_exc
27 profile("LOAD:InfoBar")
28 import Screens.InfoBar
29 from Screens.SimpleSummary import SimpleSummary
30
31 from sys import stdout, exc_info
32
33 profile("ParentalControl")
34 from Components.ParentalControl import InitParentalControl
35 InitParentalControl()
36
37 profile("LOAD:Navigation")
38 from Navigation import Navigation
39
40 profile("LOAD:skin")
41 from skin import readSkin
42
43 profile("LOAD:Tools")
44 from Tools.Directories import InitFallbackFiles, resolveFilename, SCOPE_PLUGINS, SCOPE_SKIN_IMAGE
45 from Components.config import config, configfile, ConfigText, ConfigYesNo
46 InitFallbackFiles()
47
48 profile("ReloadProfiles")
49 eDVBDB.getInstance().reloadBouquets()
50
51 config.misc.radiopic = ConfigText(default = resolveFilename(SCOPE_SKIN_IMAGE)+"radio.mvi")
52 config.misc.isNextRecordTimerAfterEventActionAuto = ConfigYesNo(default=False)
53 config.misc.useTransponderTime = ConfigYesNo(default=True)
54
55 def useTransponderTimeChanged(configElement):
56         enigma.eDVBLocalTimeHandler.getInstance().setUseDVBTime(configElement.value)
57 config.misc.useTransponderTime.addNotifier(useTransponderTimeChanged)
58
59 profile("Twisted")
60 try:
61         import twisted.python.runtime
62         twisted.python.runtime.platform.supportsThreads = lambda: False
63
64         import e2reactor
65         e2reactor.install()
66
67         from twisted.internet import reactor
68
69         def runReactor():
70                 reactor.run(installSignalHandlers=False)
71 except ImportError:
72         print "twisted not available"
73         def runReactor():
74                 runMainloop()
75
76 profile("LOAD:Plugin")
77
78 # initialize autorun plugins and plugin menu entries
79 from Components.PluginComponent import plugins
80
81 profile("LOAD:Wizard")
82 from Screens.Wizard import wizardManager
83 from Screens.DefaultWizard import *
84 from Screens.StartWizard import *
85 from Screens.TutorialWizard import *
86 import Screens.Rc
87 from Tools.BoundFunction import boundFunction
88 from Plugins.Plugin import PluginDescriptor
89
90 profile("misc")
91 had = dict()
92
93 def dump(dir, p = ""):
94         if isinstance(dir, dict):
95                 for (entry, val) in dir.items():
96                         dump(val, p + "(dict)/" + entry)
97         if hasattr(dir, "__dict__"):
98                 for name, value in dir.__dict__.items():
99                         if not had.has_key(str(value)):
100                                 had[str(value)] = 1
101                                 dump(value, p + "/" + str(name))
102                         else:
103                                 print p + "/" + str(name) + ":" + str(dir.__class__) + "(cycle)"
104         else:
105                 print p + ":" + str(dir)
106
107 # + ":" + str(dir.__class__)
108
109 # display
110
111 profile("LOAD:ScreenGlobals")
112 from Screens.Globals import Globals
113 from Screens.SessionGlobals import SessionGlobals
114 from Screens.Screen import Screen
115
116 profile("Screen")
117 Screen.global_screen = Globals()
118
119 # Session.open:
120 # * push current active dialog ('current_dialog') onto stack
121 # * call execEnd for this dialog
122 #   * clear in_exec flag
123 #   * hide screen
124 # * instantiate new dialog into 'current_dialog'
125 #   * create screens, components
126 #   * read, apply skin
127 #   * create GUI for screen
128 # * call execBegin for new dialog
129 #   * set in_exec
130 #   * show gui screen
131 #   * call components' / screen's onExecBegin
132 # ... screen is active, until it calls 'close'...
133 # Session.close:
134 # * assert in_exec
135 # * save return value
136 # * start deferred close handler ('onClose')
137 # * execEnd
138 #   * clear in_exec
139 #   * hide screen
140 # .. a moment later:
141 # Session.doClose:
142 # * destroy screen
143
144 class Session:
145         def __init__(self, desktop = None, summary_desktop = None, navigation = None):
146                 self.desktop = desktop
147                 self.summary_desktop = summary_desktop
148                 self.nav = navigation
149                 self.delay_timer = eTimer()
150                 self.delay_timer.callback.append(self.processDelay)
151
152                 self.current_dialog = None
153
154                 self.dialog_stack = [ ]
155                 self.summary_stack = [ ]
156                 self.summary = None
157
158                 self.in_exec = False
159
160                 self.screen = SessionGlobals(self)
161
162                 for p in plugins.getPlugins(PluginDescriptor.WHERE_SESSIONSTART):
163                         p(reason=0, session=self)
164
165         def processDelay(self):
166                 callback = self.current_dialog.callback
167
168                 retval = self.current_dialog.returnValue
169
170                 if self.current_dialog.isTmp:
171                         self.current_dialog.doClose()
172 #                       dump(self.current_dialog)
173                         del self.current_dialog
174                 else:
175                         del self.current_dialog.callback
176
177                 self.popCurrent()
178                 if callback is not None:
179                         callback(*retval)
180
181         def execBegin(self, first=True, do_show = True):
182                 assert not self.in_exec 
183                 self.in_exec = True
184                 c = self.current_dialog
185
186                 # when this is an execbegin after a execend of a "higher" dialog,
187                 # popSummary already did the right thing.
188                 if first:
189                         self.pushSummary()
190                         summary = c.createSummary() or SimpleSummary
191                         self.summary = self.instantiateSummaryDialog(summary, c)
192                         self.summary.show()
193                         c.addSummary(self.summary)
194
195                 c.execBegin()
196
197                 # when execBegin opened a new dialog, don't bother showing the old one.
198                 if c == self.current_dialog and do_show:
199                         c.show()
200
201         def execEnd(self, last=True):
202                 assert self.in_exec
203                 self.in_exec = False
204
205                 self.current_dialog.execEnd()
206                 self.current_dialog.hide()
207
208                 if last:
209                         self.current_dialog.removeSummary(self.summary)
210                         self.popSummary()
211
212         def create(self, screen, arguments, **kwargs):
213                 # creates an instance of 'screen' (which is a class)
214                 try:
215                         return screen(self, *arguments, **kwargs)
216                 except:
217                         errstr = "Screen %s(%s, %s): %s" % (str(screen), str(arguments), str(kwargs), exc_info()[0])
218                         print errstr
219                         print_exc(file=stdout)
220                         quitMainloop(5)
221
222         def instantiateDialog(self, screen, *arguments, **kwargs):
223                 return self.doInstantiateDialog(screen, arguments, kwargs, self.desktop)
224
225         def deleteDialog(self, screen):
226                 screen.hide()
227                 screen.doClose()
228
229         def instantiateSummaryDialog(self, screen, *arguments, **kwargs):
230                 return self.doInstantiateDialog(screen, arguments, kwargs, self.summary_desktop)
231
232         def doInstantiateDialog(self, screen, arguments, kwargs, desktop):
233                 # create dialog
234
235                 try:
236                         dlg = self.create(screen, arguments, **kwargs)
237                 except:
238                         print 'EXCEPTION IN DIALOG INIT CODE, ABORTING:'
239                         print '-'*60
240                         print_exc(file=stdout)
241                         quitMainloop(5)
242                         print '-'*60
243
244                 if dlg is None:
245                         return
246
247                 # read skin data
248                 readSkin(dlg, None, dlg.skinName, desktop)
249
250                 # create GUI view of this dialog
251                 assert desktop is not None
252
253                 dlg.setDesktop(desktop)
254                 dlg.applySkin()
255
256                 return dlg
257
258         def pushCurrent(self):
259                 if self.current_dialog is not None:
260                         self.dialog_stack.append((self.current_dialog, self.current_dialog.shown))
261                         self.execEnd(last=False)
262
263         def popCurrent(self):
264                 if self.dialog_stack:
265                         (self.current_dialog, do_show) = self.dialog_stack.pop()
266                         self.execBegin(first=False, do_show=do_show)
267                 else:
268                         self.current_dialog = None
269
270         def execDialog(self, dialog):
271                 self.pushCurrent()
272                 self.current_dialog = dialog
273                 self.current_dialog.isTmp = False
274                 self.current_dialog.callback = None # would cause re-entrancy problems.
275                 self.execBegin()
276
277         def openWithCallback(self, callback, screen, *arguments, **kwargs):
278                 dlg = self.open(screen, *arguments, **kwargs)
279                 dlg.callback = callback
280                 return dlg
281
282         def open(self, screen, *arguments, **kwargs):
283                 if self.dialog_stack and not self.in_exec:
284                         raise RuntimeError("modal open are allowed only from a screen which is modal!")
285                         # ...unless it's the very first screen.
286
287                 self.pushCurrent()
288                 dlg = self.current_dialog = self.instantiateDialog(screen, *arguments, **kwargs)
289                 dlg.isTmp = True
290                 dlg.callback = None
291                 self.execBegin()
292                 return dlg
293
294         def close(self, screen, *retval):
295                 if not self.in_exec:
296                         print "close after exec!"
297                         return
298
299                 # be sure that the close is for the right dialog!
300                 # if it's not, you probably closed after another dialog
301                 # was opened. this can happen if you open a dialog
302                 # onExecBegin, and forget to do this only once.
303                 # after close of the top dialog, the underlying will
304                 # gain focus again (for a short time), thus triggering
305                 # the onExec, which opens the dialog again, closing the loop.
306                 assert screen == self.current_dialog
307
308                 self.current_dialog.returnValue = retval
309                 self.delay_timer.start(0, 1)
310                 self.execEnd()
311
312         def pushSummary(self):
313                 if self.summary is not None:
314                         self.summary.hide()
315                 self.summary_stack.append(self.summary)
316                 self.summary = None
317
318         def popSummary(self):
319                 if self.summary is not None:
320                         self.summary.doClose()
321                 self.summary = self.summary_stack.pop()
322                 if self.summary is not None:
323                         self.summary.show()
324
325 profile("Standby,PowerKey")
326 import Screens.Standby
327 from Screens.Menu import MainMenu, mdom
328 from GlobalActions import globalActionMap
329
330 class PowerKey:
331         """ PowerKey stuff - handles the powerkey press and powerkey release actions"""
332
333         def __init__(self, session):
334                 self.session = session
335                 globalActionMap.actions["power_down"]=self.powerdown
336                 globalActionMap.actions["power_up"]=self.powerup
337                 globalActionMap.actions["power_long"]=self.powerlong
338                 globalActionMap.actions["deepstandby"]=self.shutdown # frontpanel long power button press
339                 self.standbyblocked = 1
340
341         def MenuClosed(self, *val):
342                 self.session.infobar = None
343
344         def shutdown(self):
345                 print "PowerOff - Now!"
346                 if not Screens.Standby.inTryQuitMainloop and self.session.current_dialog and self.session.current_dialog.ALLOW_SUSPEND:
347                         self.session.open(Screens.Standby.TryQuitMainloop, 1)
348
349         def powerlong(self):
350                 if Screens.Standby.inTryQuitMainloop or (self.session.current_dialog and not self.session.current_dialog.ALLOW_SUSPEND):
351                         return
352
353                 self.standbyblocked = 1
354                 action = config.usage.on_long_powerpress.value
355                 if action == "shutdown":
356                         self.shutdown()
357                 elif action == "show_menu":
358                         print "Show shutdown Menu"
359                         root = mdom.getroot()
360                         for x in root.findall("menu"):
361                                 y = x.find("id")
362                                 if y is not None:
363                                         id = y.get("val")
364                                         if id and id == "shutdown":
365                                                 self.session.infobar = self
366                                                 menu_screen = self.session.openWithCallback(self.MenuClosed, MainMenu, x)
367                                                 menu_screen.setTitle(_("Standby / Restart"))
368                                                 return
369
370         def powerdown(self):
371                 self.standbyblocked = 0
372
373         def powerup(self):
374                 if self.standbyblocked == 0:
375                         self.standbyblocked = 1
376                         self.standby()
377
378         def standby(self):
379                 if not Screens.Standby.inStandby and self.session.current_dialog and self.session.current_dialog.ALLOW_SUSPEND and self.session.in_exec:
380                         self.session.open(Screens.Standby.Standby)
381
382 profile("Scart")
383 from Screens.Scart import Scart
384
385 class AutoScartControl:
386         def __init__(self, session):
387                 self.force = False
388                 self.current_vcr_sb = eAVSwitch.getInstance().getVCRSlowBlanking()
389                 if self.current_vcr_sb and config.av.vcrswitch.value:
390                         self.scartDialog = session.instantiateDialog(Scart, True)
391                 else:
392                         self.scartDialog = session.instantiateDialog(Scart, False)
393                 config.av.vcrswitch.addNotifier(self.recheckVCRSb)
394                 eAVSwitch.getInstance().vcr_sb_notifier.get().append(self.VCRSbChanged)
395
396         def recheckVCRSb(self, configElement):
397                 self.VCRSbChanged(self.current_vcr_sb)
398
399         def VCRSbChanged(self, value):
400                 #print "vcr sb changed to", value
401                 self.current_vcr_sb = value
402                 if config.av.vcrswitch.value or value > 2:
403                         if value:
404                                 self.scartDialog.showMessageBox()
405                         else:
406                                 self.scartDialog.switchToTV()
407
408 profile("Load:CI")
409 from enigma import eDVBCIInterfaces
410 from Screens.Ci import CiHandler
411
412 profile("Load:VolumeControl")
413 from Components.VolumeControl import VolumeControl
414
415 def runScreenTest():
416         profile("readPluginList")
417         plugins.readPluginList(resolveFilename(SCOPE_PLUGINS))
418
419         profile("Init:Session")
420         nav = Navigation(config.misc.isNextRecordTimerAfterEventActionAuto.value)
421         session = Session(desktop = getDesktop(0), summary_desktop = getDesktop(1), navigation = nav)
422
423         CiHandler.setSession(session)
424
425         screensToRun = [ p.__call__ for p in plugins.getPlugins(PluginDescriptor.WHERE_WIZARD) ]
426
427         profile("wizards")
428         screensToRun += wizardManager.getWizards()
429
430         screensToRun.append((100, Screens.InfoBar.InfoBar))
431
432         screensToRun.sort()
433
434         ePythonConfigQuery.setQueryFunc(configfile.getResolvedKey)
435
436 #       eDVBCIInterfaces.getInstance().setDescrambleRules(0 # Slot Number
437 #               ,(      ["1:0:1:24:4:85:C00000:0:0:0:"], #service_list
438 #                       ["PREMIERE"], #provider_list,
439 #                       [] #caid_list
440 #               ));
441
442         def runNextScreen(session, screensToRun, *result):
443                 if result:
444                         quitMainloop(*result)
445                         return
446
447                 screen = screensToRun[0][1]
448                 args = screensToRun[0][2:]
449
450                 if screensToRun:
451                         session.openWithCallback(boundFunction(runNextScreen, session, screensToRun[1:]), screen, *args)
452                 else:
453                         session.open(screen, *args)
454
455         runNextScreen(session, screensToRun)
456
457         profile("Init:VolumeControl")
458         vol = VolumeControl(session)
459         profile("Init:PowerKey")
460         power = PowerKey(session)
461
462         # we need session.scart to access it from within menu.xml
463         session.scart = AutoScartControl(session)
464
465         profile("RunReactor")
466         profile_final()
467         runReactor()
468
469         profile("wakeup")
470         from time import time, strftime, localtime
471         from Tools.DreamboxHardware import setFPWakeuptime, getFPWakeuptime, setRTCtime
472         #get currentTime
473         nowTime = time()
474         wakeupList = [
475                 x for x in ((session.nav.RecordTimer.getNextRecordingTime(), 0, session.nav.RecordTimer.isNextRecordAfterEventActionAuto()),
476                                         (session.nav.RecordTimer.getNextZapTime(), 1),
477                                         (plugins.getNextWakeupTime(), 2))
478                 if x[0] != -1
479         ]
480         wakeupList.sort()
481         recordTimerWakeupAuto = False
482         if wakeupList:
483                 from time import strftime
484                 startTime = wakeupList[0]
485                 if (startTime[0] - nowTime) < 330: # no time to switch box back on
486                         wptime = nowTime + 30  # so switch back on in 30 seconds
487                 else:
488                         wptime = startTime[0] - 300
489                 if not config.misc.useTransponderTime.value:
490                         print "dvb time sync disabled... so set RTC now to current linux time!", strftime("%Y/%m/%d %H:%M", localtime(nowTime))
491                         setRTCtime(nowTime)
492                 print "set wakeup time to", strftime("%Y/%m/%d %H:%M", localtime(wptime))
493                 setFPWakeuptime(wptime)
494                 recordTimerWakeupAuto = startTime[1] == 0 and startTime[2]
495         config.misc.isNextRecordTimerAfterEventActionAuto.value = recordTimerWakeupAuto
496         config.misc.isNextRecordTimerAfterEventActionAuto.save()
497
498         profile("stopService")
499         session.nav.stopService()
500         profile("nav shutdown")
501         session.nav.shutdown()
502
503         profile("configfile.save")
504         configfile.save()
505
506         return 0
507
508 profile("Init:skin")
509 import skin
510 skin.loadSkinData(getDesktop(0))
511
512 profile("InputDevice")
513 import Components.InputDevice
514 Components.InputDevice.InitInputDevices()
515
516 profile("AVSwitch")
517 import Components.AVSwitch
518 Components.AVSwitch.InitAVSwitch()
519
520 profile("RecordingConfig")
521 import Components.RecordingConfig
522 Components.RecordingConfig.InitRecordingConfig()
523
524 profile("UsageConfig")
525 import Components.UsageConfig
526 Components.UsageConfig.InitUsageConfig()
527
528 profile("keymapparser")
529 import keymapparser
530 keymapparser.readKeymap(config.usage.keymap.value)
531
532 profile("Network")
533 import Components.Network
534 Components.Network.InitNetwork()
535
536 profile("LCD")
537 import Components.Lcd
538 Components.Lcd.InitLcd()
539
540 profile("SetupDevices")
541 import Components.SetupDevices
542 Components.SetupDevices.InitSetupDevices()
543
544 profile("RFMod")
545 import Components.RFmod
546 Components.RFmod.InitRFmod()
547
548 profile("Init:CI")
549 import Screens.Ci
550 Screens.Ci.InitCiConfig()
551
552 #from enigma import dump_malloc_stats
553 #t = eTimer()
554 #t.callback.append(dump_malloc_stats)
555 #t.start(1000)
556
557 # first, setup a screen
558 try:
559         runScreenTest()
560
561         plugins.shutdown()
562
563         from Components.ParentalControl import parentalControl
564         parentalControl.save()
565 except:
566         print 'EXCEPTION IN PYTHON STARTUP CODE:'
567         print '-'*60
568         print_exc(file=stdout)
569         quitMainloop(5)
570         print '-'*60