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