Merge branch 'translations' into experimental
[enigma2.git] / lib / python / Screens / RdsDisplay.py
1 from enigma import iPlayableService, iRdsDecoder
2 from Screens.Screen import Screen
3 from Components.ActionMap import NumberActionMap
4 from Components.ServiceEventTracker import ServiceEventTracker
5 from Components.Pixmap import Pixmap
6 from Components.Label import Label
7 from Tools.Directories import resolveFilename, SCOPE_CURRENT_SKIN
8 from Tools.LoadPixmap import LoadPixmap
9
10 class RdsInfoDisplay(Screen):
11         ALLOW_SUSPEND = True
12         
13         def __init__(self, session):
14                 Screen.__init__(self, session)
15
16                 self.__event_tracker = ServiceEventTracker(screen=self, eventmap=
17                         {
18                                 iPlayableService.evEnd: self.__serviceStopped,
19                                 iPlayableService.evUpdatedRadioText: self.RadioTextChanged,
20                                 iPlayableService.evUpdatedRtpText: self.RtpTextChanged,
21                                 iPlayableService.evUpdatedRassInteractivePicMask: self.RassInteractivePicMaskChanged,
22                         })
23
24                 self["RadioText"] = Label()
25                 self["RtpText"] = Label()
26                 self["RassLogo"] = Pixmap()
27
28                 self.onLayoutFinish.append(self.hideWidgets)
29                 self.rassInteractivePossible=False
30                 self.onRassInteractivePossibilityChanged = [ ]
31
32         def hideWidgets(self):
33                 for x in (self["RadioText"],self["RtpText"],self["RassLogo"]):
34                         x.hide()
35
36         def RadioTextChanged(self):
37                 service = self.session.nav.getCurrentService()
38                 decoder = service and service.rdsDecoder()
39                 rdsText = decoder and decoder.getText(iRdsDecoder.RadioText)
40                 if rdsText and len(rdsText):
41                         self["RadioText"].setText(rdsText)
42                         self["RadioText"].show()
43                 else:
44                         self["RadioText"].hide()
45
46         def RtpTextChanged(self):
47                 service = self.session.nav.getCurrentService()
48                 decoder = service and service.rdsDecoder()
49                 rtpText = decoder and decoder.getText(iRdsDecoder.RtpText)
50                 if rtpText and len(rtpText):
51                         self["RtpText"].setText(rtpText)
52                         self["RtpText"].show()
53                 else:
54                         self["RtpText"].hide()
55
56         def RassInteractivePicMaskChanged(self):
57                 if not self.rassInteractivePossible:
58                         service = self.session.nav.getCurrentService()
59                         decoder = service and service.rdsDecoder()
60                         mask = decoder and decoder.getRassInteractiveMask()
61                         if mask[0] & 1: #rass interactive index page available
62                                 self["RassLogo"].show()
63                                 self.rassInteractivePossible = True
64                                 for x in self.onRassInteractivePossibilityChanged:
65                                         x(True)
66
67         def __serviceStopped(self):
68                 self.hideWidgets()
69                 if self.rassInteractivePossible:
70                         self.rassInteractivePossible = False
71                         for x in self.onRassInteractivePossibilityChanged:
72                                 x(False)
73
74 class RassInteractive(Screen):
75         def __init__(self, session):
76                 Screen.__init__(self, session)
77
78                 self["actions"] = NumberActionMap( [ "NumberActions", "RassInteractiveActions" ],
79                         {
80                                 "exit": self.close,
81                                 "0": lambda x : self.numPressed(0),
82                                 "1": lambda x : self.numPressed(1),
83                                 "2": lambda x : self.numPressed(2),
84                                 "3": lambda x : self.numPressed(3),
85                                 "4": lambda x : self.numPressed(4),
86                                 "5": lambda x : self.numPressed(5),
87                                 "6": lambda x : self.numPressed(6),
88                                 "7": lambda x : self.numPressed(7),
89                                 "8": lambda x : self.numPressed(8),
90                                 "9": lambda x : self.numPressed(9),
91                                 "nextPage": self.nextPage,
92                                 "prevPage": self.prevPage,
93                                 "nextSubPage": self.nextSubPage,
94                                 "prevSubPage": self.prevSubPage
95                         })
96
97                 self.__event_tracker = ServiceEventTracker(screen=self, eventmap=
98                         {
99                                 iPlayableService.evUpdatedRassInteractivePicMask: self.recvRassInteractivePicMaskChanged
100                         })
101
102                 self["subpages_1"] = Pixmap()
103                 self["subpages_2"] = Pixmap()
104                 self["subpages_3"] = Pixmap()
105                 self["subpages_4"] = Pixmap()
106                 self["subpages_5"] = Pixmap()
107                 self["subpages_6"] = Pixmap()
108                 self["subpages_7"] = Pixmap()
109                 self["subpages_8"] = Pixmap()
110                 self["subpages_9"] = Pixmap()
111                 self["Marker"] = Label(">")
112
113                 self.subpage = {
114                         1 : self["subpages_1"],
115                         2 : self["subpages_2"],
116                         3 : self["subpages_3"],
117                         4 : self["subpages_4"],
118                         5 : self["subpages_5"],
119                         6 : self["subpages_6"],
120                         7 : self["subpages_7"],
121                         8 : self["subpages_8"],
122                         9 : self["subpages_9"] }
123
124                 self.subpage_png = {
125                         1 : LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/rass_page1.png")),
126                         2 : LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/rass_page2.png")),
127                         3 : LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/rass_page3.png")),
128                         4 : LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/rass_page4.png")) }
129
130                 self.current_page=0;
131                 self.current_subpage=0;
132                 self.showRassPage(0,0)
133                 self.onLayoutFinish.append(self.updateSubPagePixmaps)
134
135         def updateSubPagePixmaps(self):
136                 service = self.session.nav.getCurrentService()
137                 decoder = service and service.rdsDecoder()
138                 if not decoder: # this should never happen
139                         print "NO RDS DECODER in showRassPage"
140                 else:
141                         mask = decoder.getRassInteractiveMask()
142                         page = 1
143                         while page < 10:
144                                 subpage_cnt = self.countAvailSubpages(page, mask)
145                                 subpage = self.subpage[page]
146                                 if subpage_cnt > 0:
147                                         if subpage.instance:
148                                                 png = self.subpage_png[subpage_cnt]
149                                                 if png:
150                                                         subpage.instance.setPixmap(png)
151                                                         subpage.show()
152                                                 else:
153                                                         print "rass png missing"
154                                 else:
155                                         subpage.hide()
156                                 page += 1
157
158         def recvRassInteractivePicMaskChanged(self):
159                 self.updateSubPagePixmaps()
160
161         def showRassPage(self, page, subpage):
162                 service = self.session.nav.getCurrentService()
163                 decoder = service and service.rdsDecoder()
164                 if not decoder: # this should never happen
165                         print "NO RDS DECODER in showRassPage"
166                 else:
167                         decoder.showRassInteractivePic(page, subpage)
168                         page_diff = page - self.current_page
169                         self.current_page = page
170                         if page_diff:
171                                 current_pos = self["Marker"].getPosition()
172                                 y = current_pos[1]
173                                 y += page_diff * 25
174                                 self["Marker"].setPosition(current_pos[0],y)
175
176         def getMaskForPage(self, page, masks=None):
177                 if not masks:
178                         service = self.session.nav.getCurrentService()
179                         decoder = service and service.rdsDecoder()
180                         if not decoder: # this should never happen
181                                 print "NO RDS DECODER in getMaskForPage"
182                         masks = decoder.getRassInteractiveMask()
183                 if masks:
184                         mask = masks[(page*4)/8]
185                         if page % 2:
186                                 mask >>= 4
187                         else:
188                                 mask &= 0xF
189                 return mask
190
191         def countAvailSubpages(self, page, masks):
192                 mask = self.getMaskForPage(page, masks)
193                 cnt = 0
194                 while mask:
195                         if mask & 1:
196                                 cnt += 1
197                         mask >>= 1
198                 return cnt
199
200         def nextPage(self):
201                 mask = 0
202                 page = self.current_page
203                 while mask == 0:
204                         page += 1
205                         if page > 9:
206                                 page = 0
207                         mask = self.getMaskForPage(page)
208                 self.numPressed(page)
209
210         def prevPage(self):
211                 mask = 0
212                 page = self.current_page
213                 while mask == 0:
214                         if page > 0:
215                                 page -= 1
216                         else:
217                                 page = 9
218                         mask = self.getMaskForPage(page)
219                 self.numPressed(page)
220
221         def nextSubPage(self):
222                 self.numPressed(self.current_page)
223
224         def prevSubPage(self):
225                 num = self.current_page
226                 mask = self.getMaskForPage(num)
227                 cur_bit = 1 << self.current_subpage
228                 tmp = cur_bit
229                 while True:
230                         if tmp == 1:
231                                 tmp = 8
232                         else:
233                                 tmp >>= 1
234                         if tmp == cur_bit: # no other subpage avail
235                                 return
236                         if mask & tmp: # next subpage found
237                                 subpage = 0
238                                 while tmp > 1: # convert bit to subpage
239                                         subpage += 1
240                                         tmp >>= 1
241                                 self.current_subpage = subpage
242                                 self.showRassPage(num, subpage)
243                                 return
244
245         def numPressed(self, num):
246                 mask = self.getMaskForPage(num)
247                 if self.current_page == num:
248                         self.skip = 0
249                         cur_bit = 1 << self.current_subpage
250                         tmp = cur_bit
251                 else:
252                         self.skip = 1
253                         cur_bit = 16
254                         tmp = 1
255                 while True:
256                         if not self.skip:
257                                 if tmp == 8 and cur_bit < 16:
258                                         tmp = 1
259                                 else:
260                                         tmp <<= 1
261                         else:
262                                 self.skip = 0
263                         if tmp == cur_bit: # no other subpage avail
264                                 return
265                         if mask & tmp: # next subpage found
266                                 subpage = 0
267                                 while tmp > 1: # convert bit to subpage
268                                         subpage += 1
269                                         tmp >>= 1
270                                 self.current_subpage = subpage
271                                 self.showRassPage(num, subpage)
272                                 return