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