- add timer, RecordTimer
[enigma2.git] / mytest.py
1 from enigma import *
2 from tools import *
3
4 import RecordTimer
5
6 import sys
7 import time
8
9 from screens import *
10 from skin import applyGUIskin
11
12 # A screen is a function which instanciates all components of a screen into a temporary component.
13 # Thus, the global stuff is a screen, too.
14 # In a screen, components can either be instanciated from the class-tree, cloned (copied) or
15 # "linked" from the instance tree.
16 # A screen itself lives as the container of the components, so a screen is a component, too.
17
18 # we thus have one (static) hierarchy of screens (classes, not instances)
19 # and one with the instanciated components itself (both global and dynamic)
20
21 had = dict()
22
23 def dump(dir, p = ""):
24         if isinstance(dir, dict):
25                 for (entry, val) in dir.items():
26                         dump(val, p + "(dict)/" + entry)
27         if hasattr(dir, "__dict__"):
28                 for name, value in dir.__dict__.items():
29                         if not had.has_key(str(value)):
30                                 had[str(value)] = 1
31                                 dump(value, p + "/" + str(name))
32                         else:
33                                 print p + "/" + str(name) + ":" + str(dir.__class__) + "(cycle)"
34         else:
35                 print p + ":" + str(dir)
36
37 # + ":" + str(dir.__class__)
38
39 # defined components
40 components = {}
41
42 # do global
43 doGlobal(components)
44
45 # display
46
47 class OutputDevice:
48         def create(self, screen): pass
49
50 # display: HTML
51
52 class HTMLOutputDevice(OutputDevice):
53         def create(self, comp):
54                 print comp.produceHTML()
55
56 html = HTMLOutputDevice()
57
58 class GUIOutputDevice(OutputDevice):
59         parent = None
60         def create(self, comp):
61                 comp.createGUIScreen(self.parent)
62
63 class Session:
64         def __init__(self):
65                 self.desktop = None
66                 self.delayTimer = eTimer()
67                 self.delayTimer.timeout.get().append(self.processDelay)
68                 
69                 self.currentDialog = None
70                 
71                 self.dialogStack = [ ]
72         
73         def processDelay(self):
74                 self.execEnd()
75                 
76                 if self.currentDialog.isTmp:
77                         self.currentDialog.doClose()
78                 
79                         print sys.getrefcount(self.currentDialog)
80                         del self.currentDialog.instance
81                         dump(self.currentDialog)
82                         del self.currentDialog
83                 
84                 self.popCurrent()
85                         
86         def execBegin(self):
87                         self.currentDialog.execBegin()
88                         self.currentDialog.instance.show()
89                 
90         def execEnd(self):
91                         self.currentDialog.execEnd()
92                         self.currentDialog.instance.hide()
93         
94         def create(self, screen, arguments):
95                 # creates an instance of 'screen' (which is a class)
96                 return screen(self, *arguments)
97         
98         def instantiateDialog(self, screen, *arguments):
99                 dlg = self.create(screen, arguments)
100                 assert self.desktop != None
101                 dlg.instance = eWindow(self.desktop)
102
103                 gui = GUIOutputDevice()
104                 gui.parent = dlg.instance
105                 gui.create(dlg)
106
107                 applyGUIskin(dlg, None, dlg.skinName, self.desktop)
108                 
109                 return dlg
110          
111         def pushCurrent(self):
112                 if self.currentDialog:
113                         self.dialogStack.append(self.currentDialog)
114                         self.execEnd()
115         
116         def popCurrent(self):
117                 if len(self.dialogStack):
118                         self.currentDialog = self.dialogStack.pop()
119                         self.execBegin()
120         
121         def execDialog(self, dialog):
122                 self.pushCurrent()
123                 self.currentDialog = dialog
124                 self.currentDialog.isTmp = False
125                 self.execBegin()
126
127         def open(self, screen, *arguments):
128                 self.pushCurrent()
129                 self.currentDialog = self.instantiateDialog(screen, *arguments)
130                 self.currentDialog.isTmp = True
131                 self.execBegin()
132
133         def keyEvent(self, code):
134 #               print "code " + str(code)
135                 if code == 32:
136                         self.currentDialog["okbutton"].instance.push()
137
138                 if code == 33:
139                         self.currentDialog["channelSwitcher"].instance.push()
140                 
141                 if code >= 0x30 and code <= 0x39:
142                         try:
143                                 self.currentDialog["menu"].instance.moveSelection(code - 0x31)
144                         except:
145                                 self.currentDialog["list"].instance.moveSelection(code - 0x31)
146
147         def close(self):
148                 self.delayTimer.start(0, 1)
149
150 # TODO: remove pNavgation, eNavigation and rewrite this stuff in python.
151 class Navigation:
152         def __init__(self):
153                 self.pnav = pNavigation()
154                 self.pnav.m_event.get().append(self.callEvent)
155                 self.event = [ ]
156                 self.currentlyPlayingService = None
157                 
158                 self.RecordTimer = RecordTimer.RecordTimer()
159
160         def callEvent(self, i):
161                 for x in self.event:
162                         x(i)
163         
164         def playService(self, ref):
165                 self.currentlyPlayingServiceReference = None
166                 if not self.pnav.playService(ref):
167                         self.currentlyPlayingServiceReference = ref
168                         return 0
169                 return 1
170         
171         def getCurrentlyPlayingServiceReference(self):
172                 return self.currentlyPlayingServiceReference
173         
174         def recordService(self, ref):
175                 service = iRecordableServicePtr()
176                 print "recording service: %s" % (str(ref))
177                 if self.pnav.recordService(ref, service):
178                         print "record returned non-zero"
179                         return None
180                 else:
181                         print "ok, recordService didn't fail"
182                         return service
183         
184         def enqueueService(self, ref):
185                 return self.pnav.enqueueService(ref)
186         
187         def getCurrentService(self):
188                 service = iPlayableServicePtr()
189                 if self.pnav.getCurrentService(service):
190                         return None
191                 return service
192         
193         def getPlaylist(self):
194                 playlist = ePlaylistPtr()
195                 if self.pnav.getPlaylist(playlist):
196                         return None
197                 return playlist
198         
199         def pause(self, p):
200                 return self.pnav.pause(p)
201         
202         def recordWithTimer(self, begin, end, ref, epg):
203                 entry = RecordTimer.RecordTimerEntry(begin, end, self, ref, epg)
204                 self.RecordTimer.record(entry)
205                 return entry
206
207 def runScreenTest():
208         session = Session()
209         session.desktop = getDesktop()
210         
211         session.nav = Navigation()
212         
213         session.open(infoBar)
214
215         CONNECT(keyPressedSignal(), session.keyEvent)
216         
217         runMainloop()
218         
219         return 0
220
221 import keymapparser
222 keymapparser.readKeymap()
223 import skin
224 skin.loadSkin()
225
226 # first, setup a screen
227 runScreenTest()
228
229 # now, run the mainloop
230
231 #pt = eDebugClassPtr()
232 #eDebugClass.getDebug(pt, 12)
233 #p = pt.__deref__()
234 #print pt.x
235 #print p.x
236 #print "removing ptr..."
237 #pt = 0
238 #print "now"
239 #print "p is " + str(p)
240 #print p.x
241 #p = 0
242 #
243 #bla = eDebugClass()
244 #bla = eDebugClass(2)
245 #
246