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