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