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