d19386b21ef7d9b46df2eb2479b3d9d374e4ffe2
[vuplus_dvbapp] / lib / python / Screens / Ci.py
1 from Screen import Screen
2 from Components.ActionMap import ActionMap
3 from Components.ActionMap import NumberActionMap
4 from Components.Label import Label
5
6 from Components.config import config, ConfigSubsection, ConfigSelection, ConfigSubList, getConfigListEntry, KEY_LEFT, KEY_RIGHT, KEY_0, ConfigNothing, ConfigPIN
7 from Components.ConfigList import ConfigList
8
9 from enigma import eTimer, eDVBCI_UI
10
11 MAX_NUM_CI = 4
12
13 def InitCiConfig():
14         config.ci = ConfigSubList()
15         for slot in range(MAX_NUM_CI):
16                 config.ci.append(ConfigSubsection())
17                 config.ci[slot].canDescrambleMultipleServices = ConfigSelection(choices = [("auto", _("Auto")), ("no", _("No")), ("yes", _("Yes"))], default = "auto")
18
19 class MMIDialog(Screen):
20         def __init__(self, session, slotid, action, handler = eDVBCI_UI.getInstance(), wait_text = _("wait for ci...") ):
21                 Screen.__init__(self, session)
22
23                 print "MMIDialog with action" + str(action)
24
25                 self.mmiclosed = False
26                 self.tag = None
27                 self.slotid = slotid
28
29                 self.timer = eTimer()
30                 self.timer.timeout.get().append(self.keyCancel)
31
32                 #else the skins fails
33                 self["title"] = Label("")
34                 self["subtitle"] = Label("")
35                 self["bottom"] = Label("")
36                 self["entries"] = ConfigList([ ])
37
38                 self["actions"] = NumberActionMap(["SetupActions"],
39                         {
40                                 "ok": self.okbuttonClick,
41                                 "cancel": self.keyCancel,
42                                 #for PIN
43                                 "left": self.keyLeft,
44                                 "right": self.keyRight,
45                                 "1": self.keyNumberGlobal,
46                                 "2": self.keyNumberGlobal,
47                                 "3": self.keyNumberGlobal,
48                                 "4": self.keyNumberGlobal,
49                                 "5": self.keyNumberGlobal,
50                                 "6": self.keyNumberGlobal,
51                                 "7": self.keyNumberGlobal,
52                                 "8": self.keyNumberGlobal,
53                                 "9": self.keyNumberGlobal,
54                                 "0": self.keyNumberGlobal
55                         }, -1)
56
57                 self.action = action
58
59                 self.handler = handler
60                 self.wait_text = wait_text
61
62                 if action == 2:         #start MMI
63                         handler.startMMI(self.slotid)
64                         self.showWait()
65                 elif action == 3:               #mmi already there (called from infobar)
66                         self.showScreen()
67
68         def addEntry(self, list, entry):
69                 if entry[0] == "TEXT":          #handle every item (text / pin only?)
70                         list.append( (entry[1], ConfigNothing(), entry[2]) )
71                 if entry[0] == "PIN":
72                         pinlength = entry[1]
73                         if entry[3] == 1:
74                                 # masked pins:
75                                 x = ConfigPIN(0, len = pinlength, censor = "*")
76                         else:
77                                 # unmasked pins:
78                                 x = ConfigPIN(0, len = pinlength)
79                         self["subtitle"].setText(entry[2])
80                         list.append( getConfigListEntry("", x) )
81                         self["bottom"].setText(_("please press OK when ready"))
82
83         def okbuttonClick(self):
84                 self.timer.stop()
85                 if not self.tag:
86                         return
87                 if self.tag == "WAIT":
88                         print "do nothing - wait"
89                 elif self.tag == "MENU":
90                         print "answer MENU"
91                         cur = self["entries"].getCurrent()
92                         if cur:
93                                 self.handler.answerMenu(self.slotid, cur[2])
94                         else:
95                                 self.handler.answerMenu(self.slotid, 0)
96                         self.showWait()
97                 elif self.tag == "LIST":
98                         print "answer LIST"
99                         self.handler.answerMenu(self.slotid, 0)
100                         self.showWait()
101                 elif self.tag == "ENQ":
102                         cur = self["entries"].getCurrent()
103                         answer = str(cur[1].value)
104                         length = len(answer)
105                         while length < cur[1].getLength():
106                                 answer = '0'+answer
107                                 length+=1
108                         self.handler.answerEnq(self.slotid, answer)
109                         self.showWait()
110
111         def closeMmi(self):
112                 self.timer.stop()
113                 self.close(self.slotid)
114
115         def keyCancel(self):
116                 self.timer.stop()
117                 if not self.tag or self.mmiclosed:
118                         self.closeMmi()
119                 elif self.tag == "WAIT":
120                         self.handler.stopMMI(self.slotid)
121                         self.closeMmi()
122                 elif self.tag in [ "MENU", "LIST" ]:
123                         print "cancel list"
124                         self.handler.answerMenu(self.slotid, 0)
125                         self.showWait()
126                 elif self.tag == "ENQ":
127                         print "cancel enq"
128                         self.handler.cancelEnq(self.slotid)
129                         self.showWait()
130                 else:
131                         print "give cancel action to ci"
132
133         def keyConfigEntry(self, key):
134                 self.timer.stop()
135                 try:
136                         self["entries"].handleKey(key)
137                 except:
138                         pass
139
140         def keyNumberGlobal(self, number):
141                 self.timer.stop()
142                 self.keyConfigEntry(KEY_0 + number)
143
144         def keyLeft(self):
145                 self.timer.stop()
146                 self.keyConfigEntry(KEY_LEFT)
147
148         def keyRight(self):
149                 self.timer.stop()
150                 self.keyConfigEntry(KEY_RIGHT)
151
152         def updateList(self, list):
153                 List = self["entries"]
154                 try:
155                         List.instance.moveSelectionTo(0)
156                 except:
157                         pass
158                 List.l.setList(list)
159
160         def showWait(self):
161                 self.tag = "WAIT"
162                 self["title"].setText("")
163                 self["subtitle"].setText("")
164                 self["bottom"].setText("")
165                 list = [ ]
166                 list.append( (self.wait_text, ConfigNothing()) )
167                 self.updateList(list)
168
169         def showScreen(self):
170                 screen = self.handler.getMMIScreen(self.slotid)
171
172                 list = [ ]
173
174                 self.timer.stop()
175                 if len(screen) > 0 and screen[0][0] == "CLOSE":
176                         timeout = screen[0][1]
177                         self.mmiclosed = True
178                         if timeout > 0:
179                                 self.timer.start(timeout*1000, True)
180                         else:
181                                 self.keyCancel()
182                 else:
183                         self.mmiclosed = False
184                         self.tag = screen[0][0]
185                         for entry in screen:
186                                 if entry[0] == "PIN":
187                                         self.addEntry(list, entry)
188                                 else:
189                                         if entry[0] == "TITLE":
190                                                 self["title"].setText(entry[1])
191                                         elif entry[0] == "SUBTITLE":
192                                                 self["subtitle"].setText(entry[1])
193                                         elif entry[0] == "BOTTOM":
194                                                 self["bottom"].setText(entry[1])
195                                         elif entry[0] == "TEXT":
196                                                 self.addEntry(list, entry)
197                         self.updateList(list)
198
199         def ciStateChanged(self):
200                 do_close = False
201                 if self.action == 0:                    #reset
202                         do_close = True
203                 if self.action == 1:                    #init
204                         do_close = True
205
206                 #module still there ?
207                 if self.handler.getState(self.slotid) != 2:
208                         do_close = True
209
210                 #mmi session still active ?
211                 if self.handler.getMMIState(self.slotid) != 1:
212                         do_close = True
213
214                 if do_close:
215                         self.closeMmi()
216                 elif self.action > 1 and self.handler.availableMMI(self.slotid) == 1:
217                         self.showScreen()
218
219                 #FIXME: check for mmi-session closed
220
221 class CiMessageHandler:
222         def __init__(self):
223                 self.session = None
224                 self.ci = { }
225                 self.dlgs = { }
226                 eDVBCI_UI.getInstance().ciStateChanged.get().append(self.ciStateChanged)
227
228         def setSession(self, session):
229                 self.session = session
230
231         def ciStateChanged(self, slot):
232                 if slot in self.ci:
233                         self.ci[slot](slot)
234                 else:
235                         if slot in self.dlgs:
236                                 self.dlgs[slot].ciStateChanged()
237                         elif eDVBCI_UI.getInstance().availableMMI(slot) == 1:
238                                 if self.session:
239                                         self.dlgs[slot] = self.session.openWithCallback(self.dlgClosed, MMIDialog, slot, 3)
240
241         def dlgClosed(self, slot):
242                 if slot in self.dlgs:
243                         del self.dlgs[slot]
244
245         def registerCIMessageHandler(self, slot, func):
246                 self.unregisterCIMessageHandler(slot)
247                 self.ci[slot] = func
248
249         def unregisterCIMessageHandler(self, slot):
250                 if slot in self.ci:
251                         del self.ci[slot]
252
253 CiHandler = CiMessageHandler()
254
255 class CiSelection(Screen):
256         def __init__(self, session):
257                 Screen.__init__(self, session)
258                 self["actions"] = ActionMap(["OkCancelActions", "CiSelectionActions"],
259                         {
260                                 "left": self.keyLeft,
261                                 "right": self.keyLeft,
262                                 "ok": self.okbuttonClick,
263                                 "cancel": self.cancel
264                         },-1)
265
266                 self.dlg = None
267                 self.state = { }
268                 self.list = [ ]
269
270                 for slot in range(MAX_NUM_CI):
271                         state = eDVBCI_UI.getInstance().getState(slot)
272                         if state != -1:
273                                 self.appendEntries(slot, state)
274                                 CiHandler.registerCIMessageHandler(slot, self.ciStateChanged)
275
276                 menuList = ConfigList(self.list)
277                 menuList.list = self.list
278                 menuList.l.setList(self.list)
279                 self["entries"] = menuList
280                 self["entries"].onSelectionChanged.append(self.selectionChanged)
281                 self["text"] = Label(_("Slot %d")%(1))
282
283         def selectionChanged(self):
284                 cur_idx = self["entries"].getCurrentIndex()
285                 self["text"].setText(_("Slot %d")%((cur_idx / 4)+1))
286
287         def keyConfigEntry(self, key):
288                 try:
289                         self["entries"].handleKey(key)
290                         self["entries"].getCurrent()[1].save()
291                 except:
292                         pass
293
294         def keyLeft(self):
295                 self.keyConfigEntry(KEY_LEFT)
296
297         def keyRight(self):
298                 self.keyConfigEntry(KEY_RIGHT)
299
300         def appendEntries(self, slot, state):
301                 self.state[slot] = state
302                 self.list.append( (_("Reset"), ConfigNothing(), 0, slot) )
303                 self.list.append( (_("Init"), ConfigNothing(), 1, slot) )
304
305                 if self.state[slot] == 0:                       #no module
306                         self.list.append( (_("no module found"), ConfigNothing(), 2, slot) )
307                 elif self.state[slot] == 1:             #module in init
308                         self.list.append( (_("init module"), ConfigNothing(), 2, slot) )
309                 elif self.state[slot] == 2:             #module ready
310                         #get appname
311                         appname = eDVBCI_UI.getInstance().getAppName(slot)
312                         self.list.append( (appname, ConfigNothing(), 2, slot) )
313
314                 self.list.append(getConfigListEntry(_("Multiple service support"), config.ci[slot].canDescrambleMultipleServices))
315
316         def updateState(self, slot):
317                 state = eDVBCI_UI.getInstance().getState(slot)
318                 self.state[slot] = state
319
320                 slotidx=0
321                 while len(self.list[slotidx]) < 3 or self.list[slotidx][3] != slot:
322                         slotidx += 1
323
324                 slotidx += 1 # do not change Reset
325                 slotidx += 1 # do not change Init
326
327                 if state == 0:                  #no module
328                         self.list[slotidx] = (_("no module found"), ConfigNothing(), 2, slot)
329                 elif state == 1:                #module in init
330                         self.list[slotidx] = (_("init module"), ConfigNothing(), 2, slot)
331                 elif state == 2:                #module ready
332                         #get appname
333                         appname = eDVBCI_UI.getInstance().getAppName(slot)
334                         self.list[slotidx] = (appname, ConfigNothing(), 2, slot)
335
336                 lst = self["entries"]
337                 lst.list = self.list
338                 lst.l.setList(self.list)
339
340         def ciStateChanged(self, slot):
341                 if self.dlg:
342                         self.dlg.ciStateChanged()
343                 else:
344                         state = eDVBCI_UI.getInstance().getState(slot)
345                         if self.state[slot] != state:
346                                 #print "something happens"
347                                 self.state[slot] = state
348                                 self.updateState(slot)
349
350         def dlgClosed(self, slot):
351                 self.dlg = None
352
353         def okbuttonClick(self):
354                 cur = self["entries"].getCurrent()
355                 if cur and len(cur) > 2:
356                         action = cur[2]
357                         slot = cur[3]
358                         if action == 0:         #reset
359                                 eDVBCI_UI.getInstance().setReset(slot)
360                         elif action == 1:               #init
361                                 eDVBCI_UI.getInstance().setInit(slot)
362                         elif self.state[slot] == 2:
363                                 self.dlg = self.session.openWithCallback(self.dlgClosed, MMIDialog, slot, action)
364
365         def cancel(self):
366                 for slot in range(MAX_NUM_CI):
367                         state = eDVBCI_UI.getInstance().getState(slot)
368                         if state != -1:
369                                 CiHandler.unregisterCIMessageHandler(slot)
370                 self.close()