fixes bug #291
[enigma2.git] / lib / python / Screens / ServiceInfo.py
1 from Components.HTMLComponent import HTMLComponent
2 from Components.GUIComponent import GUIComponent
3 from Screen import Screen
4 from Components.ActionMap import ActionMap
5 from Components.Label import Label
6 from ServiceReference import ServiceReference
7 from enigma import eListboxPythonMultiContent, eListbox, gFont, iServiceInformation, eServiceCenter
8 from Tools.Transponder import ConvertToHumanReadable
9
10 RT_HALIGN_LEFT = 0
11
12 TYPE_TEXT = 0
13 TYPE_VALUE_HEX = 1
14 TYPE_VALUE_DEC = 2
15 TYPE_VALUE_HEX_DEC = 3
16 TYPE_SLIDER = 4
17
18 def to_unsigned(x):
19         return x & 0xFFFFFFFF
20
21 def ServiceInfoListEntry(a, b, valueType=TYPE_TEXT, param=4):
22         print "b:", b
23         if not isinstance(b, str):
24                 if valueType == TYPE_VALUE_HEX:
25                         b = ("0x%0" + str(param) + "x") % to_unsigned(b)
26                 elif valueType == TYPE_VALUE_DEC:
27                         b = str(b)
28                 elif valueType == TYPE_VALUE_HEX_DEC:
29                         b = ("0x%0" + str(param) + "x (%dd)") % (to_unsigned(b), b)
30                 else:
31                         b = str(b)
32
33         return [
34                 #PyObject *type, *px, *py, *pwidth, *pheight, *pfnt, *pstring, *pflags;
35                 (eListboxPythonMultiContent.TYPE_TEXT, 0, 0, 200, 30, 0, RT_HALIGN_LEFT, ""),
36                 (eListboxPythonMultiContent.TYPE_TEXT, 0, 0, 200, 25, 0, RT_HALIGN_LEFT, a),
37                 (eListboxPythonMultiContent.TYPE_TEXT, 220, 0, 350, 25, 0, RT_HALIGN_LEFT, b)
38         ]
39
40 class ServiceInfoList(HTMLComponent, GUIComponent):
41         def __init__(self, source):
42                 GUIComponent.__init__(self)
43                 self.l = eListboxPythonMultiContent()
44                 self.list = source
45                 self.l.setList(self.list)
46                 self.l.setFont(0, gFont("Regular", 23))
47                 self.l.setItemHeight(25)
48
49         GUI_WIDGET = eListbox
50
51         def postWidgetCreate(self, instance):
52                 self.instance.setContent(self.l)
53
54 TYPE_SERVICE_INFO = 1
55 TYPE_TRANSPONDER_INFO = 2
56
57 class ServiceInfo(Screen):
58         def __init__(self, session, serviceref=None):
59                 Screen.__init__(self, session)
60                 
61                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
62                 {
63                         "ok": self.close,
64                         "cancel": self.close,
65                         "red": self.information,
66                         "green": self.pids,
67                         "yellow": self.transponder,
68                         "blue": self.tuner
69                 }, -1)
70
71                 if serviceref:
72                         self.type = TYPE_TRANSPONDER_INFO
73                         self["red"] = Label()
74                         self["green"] = Label()
75                         self["yellow"] = Label()
76                         self["blue"] = Label()
77                         info = eServiceCenter.getInstance().info(serviceref)
78                         self.transponder_info = info.getInfoObject(serviceref, iServiceInformation.sTransponderData)
79                         # info is a iStaticServiceInformation, not a iServiceInformation
80                         self.info = None
81                         self.feinfo = None
82                 else:
83                         self.type = TYPE_SERVICE_INFO
84                         self["red"] = Label(_("Serviceinfo"))
85                         self["green"] = Label(_("PIDs"))
86                         self["yellow"] = Label(_("Transponder"))
87                         self["blue"] = Label(_("Tuner status"))
88                         service = session.nav.getCurrentService()
89                         if service is not None:
90                                 self.info = service.info()
91                                 self.feinfo = service.frontendInfo()
92                                 print self.info.getInfoObject(iServiceInformation.sCAIDs);
93                         else:
94                                 self.info = None
95                                 self.feinfo = None
96
97                 tlist = [ ]
98
99                 self["infolist"] = ServiceInfoList(tlist)
100                 self.onShown.append(self.information)
101
102         def information(self):
103                 if self.type == TYPE_SERVICE_INFO:
104                         if self.session.nav.getCurrentlyPlayingServiceReference():
105                                 name = ServiceReference(self.session.nav.getCurrentlyPlayingServiceReference()).getServiceName()
106                                 refstr = self.session.nav.getCurrentlyPlayingServiceReference().toString()
107                         else:
108                                 name = "N/A"
109                                 refstr = "N/A"
110                         aspect = self.getServiceInfoValue(iServiceInformation.sAspect)
111                         if aspect in ( 1, 2, 5, 6, 9, 0xA, 0xD, 0xE ):
112                                 aspect = "4:3"
113                         else:
114                                 aspect = "16:9"
115                         width = self.info and self.info.getInfo(iServiceInformation.sVideoWidth) or -1
116                         height = self.info and self.info.getInfo(iServiceInformation.sVideoHeight) or -1
117                         if width != -1 and height != -1:
118                                 Labels = ( ("Name", name, TYPE_TEXT),
119                                                    ("Provider", self.getServiceInfoValue(iServiceInformation.sProvider), TYPE_TEXT),
120                                                    ("Videoformat", aspect, TYPE_TEXT),
121                                                    ("Videosize", "%dx%d" %(width, height), TYPE_TEXT),
122                                                    ("Namespace", self.getServiceInfoValue(iServiceInformation.sNamespace), TYPE_VALUE_HEX, 8),
123                                                    ("Service Reference", refstr, TYPE_TEXT))
124                         else:
125                                 Labels = ( ("Name", name, TYPE_TEXT),
126                                                    ("Provider", self.getServiceInfoValue(iServiceInformation.sProvider), TYPE_TEXT),
127                                                    ("Videoformat", aspect, TYPE_TEXT),
128                                                    ("Namespace", self.getServiceInfoValue(iServiceInformation.sNamespace), TYPE_VALUE_HEX, 8),
129                                                    ("Service Reference", refstr, TYPE_TEXT))
130                         self.fillList(Labels)
131                 else:
132                         if self.transponder_info:
133                                 tp_info = ConvertToHumanReadable(self.transponder_info)
134                                 conv = { "tuner_type"           : _("Transponder Type"),
135                                                  "system"                       : _("System"),
136                                                  "modulation"           : _("Modulation"),
137                                                  "orbital_position" : _("Orbital Position"),
138                                                  "frequency"            : _("Frequency"),
139                                                  "symbol_rate"          : _("Symbolrate"),
140                                                  "bandwidth"            : _("Bandwidth"),
141                                                  "polarization"         : _("Polarization"),
142                                                  "inversion"            : _("Inversion"),
143                                                  "pilot"                        : _("Pilot"),
144                                                  "rolloff"                      : _("Rolloff"),
145                                                  "fec_inner"            : _("FEC"),
146                                                  "code_rate_lp"         : _("Coderate LP"),
147                                                  "code_rate_hp"         : _("Coderate HP"),
148                                                  "constellation"        : _("Constellation"),
149                                                  "transmission_mode": _("Transmission Mode"),
150                                                  "guard_interval"       : _("Guard Interval"),
151                                                  "hierarchy_information": _("Hierarchy Information") }
152                                 Labels = [(conv[i], tp_info[i], TYPE_VALUE_DEC) for i in tp_info.keys()]
153                                 self.fillList(Labels)
154
155         def pids(self):
156                 if self.type == TYPE_SERVICE_INFO:
157                         Labels = ( ("VideoPID", self.getServiceInfoValue(iServiceInformation.sVideoPID), TYPE_VALUE_HEX_DEC, 4),
158                                            ("AudioPID", self.getServiceInfoValue(iServiceInformation.sAudioPID), TYPE_VALUE_HEX_DEC, 4),
159                                            ("PCRPID", self.getServiceInfoValue(iServiceInformation.sPCRPID), TYPE_VALUE_HEX_DEC, 4),
160                                            ("PMTPID", self.getServiceInfoValue(iServiceInformation.sPMTPID), TYPE_VALUE_HEX_DEC, 4),
161                                            ("TXTPID", self.getServiceInfoValue(iServiceInformation.sTXTPID), TYPE_VALUE_HEX_DEC, 4),
162                                            ("TSID", self.getServiceInfoValue(iServiceInformation.sTSID), TYPE_VALUE_HEX_DEC, 4),
163                                            ("ONID", self.getServiceInfoValue(iServiceInformation.sONID), TYPE_VALUE_HEX_DEC, 4),
164                                            ("SID", self.getServiceInfoValue(iServiceInformation.sSID), TYPE_VALUE_HEX_DEC, 4))
165                         self.fillList(Labels)
166         
167         def showFrontendData(self, real):
168                 if self.type == TYPE_SERVICE_INFO:
169                         frontendData = self.feinfo and self.feinfo.getAll(real)
170                         Labels = self.getFEData(frontendData)
171                         self.fillList(Labels)
172         
173         def transponder(self):
174                 if self.type == TYPE_SERVICE_INFO:
175                         self.showFrontendData(True)
176                 
177         def tuner(self):
178                 if self.type == TYPE_SERVICE_INFO:
179                         self.showFrontendData(False)
180
181         def getFEData(self, frontendDataOrg):
182                 if frontendDataOrg and len(frontendDataOrg):
183                         frontendData = ConvertToHumanReadable(frontendDataOrg)
184                         if frontendDataOrg["tuner_type"] == "DVB-S":
185                                 return (("NIM", ('A', 'B', 'C', 'D')[frontendData["tuner_number"]], TYPE_TEXT),
186                                                         ("Type", frontendData["system"], TYPE_TEXT),
187                                                         ("Modulation", frontendData["modulation"], TYPE_TEXT),
188                                                         ("Orbital position", frontendData["orbital_position"], TYPE_VALUE_DEC),
189                                                         ("Frequency", frontendData["frequency"], TYPE_VALUE_DEC),
190                                                         ("Symbolrate", frontendData["symbol_rate"], TYPE_VALUE_DEC),
191                                                         ("Polarization", frontendData["polarization"], TYPE_TEXT),
192                                                         ("Inversion", frontendData["inversion"], TYPE_TEXT),
193                                                         ("FEC inner", frontendData["fec_inner"], TYPE_TEXT),
194                                                         ("Pilot", frontendData.get("pilot", None), TYPE_TEXT),
195                                                         ("Rolloff", frontendData.get("rolloff", None), TYPE_TEXT))
196                         elif frontendDataOrg["tuner_type"] == "DVB-C":
197                                 return (("NIM", ('A', 'B', 'C', 'D')[frontendData["tuner_number"]], TYPE_TEXT),
198                                                 ("Type", frontendData["tuner_type"], TYPE_TEXT),
199                                                 ("Frequency", frontendData["frequency"], TYPE_VALUE_DEC),
200                                                 ("Symbolrate", frontendData["symbol_rate"], TYPE_VALUE_DEC),
201                                                 ("Modulation", frontendData["modulation"], TYPE_TEXT),
202                                                 ("Inversion", frontendData["inversion"], TYPE_TEXT),
203                                                 ("FEC inner", frontendData["fec_inner"], TYPE_TEXT))
204                         elif frontendDataOrg["tuner_type"] == "DVB-T":
205                                 return (("NIM", ('A', 'B', 'C', 'D')[frontendData["tuner_number"]], TYPE_TEXT),
206                                                 ("Type", frontendData["tuner_type"], TYPE_TEXT),
207                                                 ("Frequency", frontendData["frequency"], TYPE_VALUE_DEC),
208                                                 ("Inversion", frontendData["inversion"], TYPE_TEXT),
209                                                 ("Bandwidth", frontendData["bandwidth"], TYPE_VALUE_DEC),
210                                                 ("CodeRateLP", frontendData["code_rate_lp"], TYPE_TEXT),
211                                                 ("CodeRateHP", frontendData["code_rate_hp"], TYPE_TEXT),
212                                                 ("Constellation", frontendData["constellation"], TYPE_TEXT),
213                                                 ("Transmission Mode", frontendData["transmission_mode"], TYPE_TEXT),
214                                                 ("Guard Interval", frontendData["guard_interval"], TYPE_TEXT),
215                                                 ("Hierarchy Inform.", frontendData["hierarchy_information"], TYPE_TEXT))
216                 return [ ]
217
218         def fillList(self, Labels):
219                 tlist = [ ]
220
221                 for item in Labels:
222                         if item[1] is None:
223                                 continue;
224                         value = item[1]
225                         if len(item) < 4:
226                                 tlist.append(ServiceInfoListEntry(item[0]+":", value, item[2]))
227                         else:
228                                 tlist.append(ServiceInfoListEntry(item[0]+":", value, item[2], item[3]))
229
230                 self["infolist"].l.setList(tlist)
231
232         def getServiceInfoValue(self, what):
233                 if self.info is None:
234                         return ""
235                 
236                 v = self.info.getInfo(what)
237                 if v == -2:
238                         v = self.info.getInfoString(what)
239                 elif v == -1:
240                         v = "N/A"
241
242                 return v