Merge branch 'master' into dvd_scale_test
[enigma2.git] / lib / python / Plugins / SystemPlugins / Satfinder / plugin.py
1 from enigma import eDVBResourceManager,\
2         eDVBFrontendParametersSatellite, eDVBFrontendParameters
3
4 from Screens.Screen import Screen
5 from Screens.ScanSetup import ScanSetup
6 from Screens.MessageBox import MessageBox
7 from Plugins.Plugin import PluginDescriptor
8
9 from Components.Label import Label
10 from Components.Sources.FrontendStatus import FrontendStatus
11 from Components.ActionMap import ActionMap
12 from Components.NimManager import nimmanager, getConfigSatlist
13 from Components.MenuList import MenuList
14 from Components.config import ConfigSelection, getConfigListEntry
15
16 class Tuner:
17         def __init__(self, frontend):
18                 self.frontend = frontend
19                 
20         def tune(self, transponder):
21                 if self.frontend:
22                         print "tuning to transponder with data", transponder
23                         parm = eDVBFrontendParametersSatellite()
24                         parm.frequency = transponder[0] * 1000
25                         parm.symbol_rate = transponder[1] * 1000
26                         parm.polarisation = transponder[2]
27                         parm.fec = transponder[3]
28                         parm.inversion = transponder[4]
29                         parm.orbital_position = transponder[5]
30                         parm.system = 0  # FIXMEE !! HARDCODED DVB-S (add support for DVB-S2)
31                         parm.modulation = 1 # FIXMEE !! HARDCODED QPSK 
32                         feparm = eDVBFrontendParameters()
33                         feparm.setDVBS(parm)
34                         self.lastparm = feparm
35                         self.frontend.tune(feparm)
36         
37         def retune(self):
38                 if self.frontend:
39                         self.frontend.tune(self.lastparm)
40
41 class Satfinder(ScanSetup):
42         def openFrontend(self):
43                 res_mgr = eDVBResourceManager.getInstance()
44                 if res_mgr:
45                         self.raw_channel = res_mgr.allocateRawChannel(self.feid)
46                         if self.raw_channel:
47                                 self.frontend = self.raw_channel.getFrontend()
48                                 if self.frontend:
49                                         return True
50                                 else:
51                                         print "getFrontend failed"
52                         else:
53                                 print "getRawChannel failed"
54                 else:
55                         print "getResourceManager instance failed"
56                 return False
57
58         def __init__(self, session, feid):
59                 self.initcomplete = False
60                 self.feid = feid
61                 self.oldref = None
62
63                 if not self.openFrontend():
64                         self.oldref = session.nav.getCurrentlyPlayingServiceReference()
65                         session.nav.stopService() # try to disable foreground service
66                         if not self.openFrontend():
67                                 if session.pipshown: # try to disable pip
68                                         session.pipshown = False
69                                         del session.pip
70                                         if not self.openFrontend():
71                                                 self.frontend = None # in normal case this should not happen
72
73                 ScanSetup.__init__(self, session)
74                 self.tuner = Tuner(self.frontend)
75                 self["introduction"].setText("")
76                 self["Frontend"] = FrontendStatus(frontend_source = lambda : self.frontend, update_interval = 100)
77                 self.initcomplete = True
78                 self.onClose.append(self.__onClose)
79
80         def __onClose(self):
81                 self.session.nav.playService(self.oldref)
82
83         def createSetup(self):
84                 self.typeOfTuningEntry = None
85                 self.satEntry = None
86                 
87                 self.list = []
88                 self.typeOfTuningEntry = getConfigListEntry(_('Tune'), self.tuning_type)
89                 self.list.append(self.typeOfTuningEntry)
90                 self.satEntry = getConfigListEntry(_('Satellite'), self.tuning_sat)
91                 self.list.append(self.satEntry)
92                 if self.tuning_type.value == "manual_transponder":
93                         self.list.append(getConfigListEntry(_('Frequency'), self.scan_sat.frequency))
94                         self.list.append(getConfigListEntry(_('Inversion'), self.scan_sat.inversion))
95                         self.list.append(getConfigListEntry(_('Symbol Rate'), self.scan_sat.symbolrate))
96                         self.list.append(getConfigListEntry(_("Polarity"), self.scan_sat.polarization))
97                         self.list.append(getConfigListEntry(_("FEC"), self.scan_sat.fec))
98                 elif self.tuning_transponder and self.tuning_type.value == "predefined_transponder":
99                         self.list.append(getConfigListEntry(_("Transponder"), self.tuning_transponder))
100                 self["config"].list = self.list
101                 self["config"].l.setList(self.list)
102
103         def newConfig(self):
104                 if self["config"].getCurrent() == self.typeOfTuningEntry:
105                         self.createSetup()
106                 elif self["config"].getCurrent() == self.satEntry:
107                         self.updateSats()
108                         self.createSetup()
109
110         def sat_changed(self, config_element):
111                 self.newConfig()
112                 self.retune(config_element)
113
114         def retune(self, configElement):
115                 returnvalue = (0, 0, 0, 0, 0, 0, 0)
116                 satpos = self.tuning_sat.orbital_position
117                 
118                 if satpos is not None:
119                         if self.tuning_type.value == "manual_transponder":
120                                 returnvalue = (self.scan_sat.frequency.value, self.scan_sat.symbolrate.value, self.scan_sat.polarization.index, self.scan_sat.fec.index, self.scan_sat.inversion.index, satpos)
121                                 self.tune(returnvalue)
122                         elif self.tuning_type.value == "predefined_transponder":
123                                 tps = nimmanager.getTransponders(satpos)
124                                 l = len(tps)
125                                 if l > self.tuning_transponder.index:
126                                         transponder = nimmanager.getTransponders(satpos)[self.tuning_transponder.index]
127                                         returnvalue = (int(transponder[1] / 1000), int(transponder[2] / 1000), transponder[3], transponder[4], 2, satpos)
128                                         self.tune(returnvalue)
129
130         def createConfig(self, foo):
131                 self.tuning_transponder = None
132                 self.tuning_type = ConfigSelection(choices = [("manual_transponder", _("Manual transponder")), ("predefined_transponder", _("Predefined transponder"))])
133                 self.tuning_sat = getConfigSatlist(192, nimmanager.getSatListForNim(self.feid))
134                 ScanSetup.createConfig(self, None)
135                 
136                 self.updateSats()
137                 
138                 self.tuning_type.addNotifier(self.retune, initial_call = False)
139                 self.tuning_sat.addNotifier(self.sat_changed, initial_call = False)
140                 self.scan_sat.frequency.addNotifier(self.retune, initial_call = False)
141                 self.scan_sat.inversion.addNotifier(self.retune, initial_call = False)
142                 self.scan_sat.symbolrate.addNotifier(self.retune, initial_call = False)
143                 self.scan_sat.polarization.addNotifier(self.retune, initial_call = False)
144                 self.scan_sat.fec.addNotifier(self.retune, initial_call = False)
145
146         def updateSats(self):
147                 orb_pos = self.tuning_sat.orbital_position
148                 if orb_pos is not None:
149                         transponderlist = nimmanager.getTransponders(orb_pos)
150                         list = []
151                         default = None
152                         for x in transponderlist:
153                                 if x[3] == 0:
154                                         pol = "H"
155                                 elif x[3] == 1:
156                                         pol = "V"
157                                 elif x[3] == 2:
158                                         pol = "CL"
159                                 elif x[3] == 3:
160                                         pol = "CR"
161                                 else:
162                                         pol = "??"
163                                 if x[4] == 0:
164                                         fec = "FEC_AUTO"
165                                 elif x[4] == 1:
166                                         fec = "FEC_1_2"
167                                 elif x[4] == 2:
168                                         fec = "FEC_2_3"
169                                 elif x[4] == 3:
170                                         fec = "FEC_3_4"
171                                 elif x[4] == 4:
172                                         fec = "FEC_5_6"
173                                 elif x[4] == 5:
174                                         fec = "FEC_7_8"
175                                 elif x[4] == 6:
176                                         fec = "FEC_8_9"
177                                 elif x[4] == 7:
178                                         fec = "FEC_3_5"
179                                 elif x[4] == 8:
180                                         fec = "FEC_4_5"
181                                 elif x[4] == 9:
182                                         fec = "FEC_9_10"
183                                 elif x[4] == 15:
184                                         fec = "FEC_None"
185                                 else:
186                                         fec = "FEC_Unknown"
187                                 e = str(x[1]) + "," + str(x[2]) + "," + pol + "," + fec
188                                 if default is None:
189                                         default = e
190                                 list.append(e)
191                         self.tuning_transponder = ConfigSelection(choices = list, default = default)
192                         self.tuning_transponder.addNotifier(self.retune, initial_call = False)
193
194         def keyGo(self):
195                 self.retune(self.tuning_type)
196
197         def restartPrevService(self, yesno):
198                 if yesno:
199                         if self.frontend:
200                                 self.frontend = None
201                                 del self.raw_channel
202                 else:
203                         self.oldref = None
204                 self.close(None)
205
206         def keyCancel(self):
207                 if self.oldref:
208                         self.session.openWithCallback(self.restartPrevService, MessageBox, _("Zap back to service before satfinder?"), MessageBox.TYPE_YESNO)
209                 else:
210                         self.restartPrevService(False)
211
212         def tune(self, transponder):
213                 if self.initcomplete:
214                         if transponder is not None:
215                                 self.tuner.tune(transponder)
216
217 class SatNimSelection(Screen):
218         skin = """
219                 <screen position="140,165" size="400,100" title="select Slot">
220                         <widget name="nimlist" position="20,10" size="360,75" />
221                 </screen>"""
222                 
223         def __init__(self, session):
224                 Screen.__init__(self, session)
225
226                 nimlist = nimmanager.getNimListOfType("DVB-S")
227                 nimMenuList = []
228                 for x in nimlist:
229                         nimMenuList.append((nimmanager.nim_slots[x].friendly_full_description, x))
230
231                 self["nimlist"] = MenuList(nimMenuList)
232
233                 self["actions"] = ActionMap(["OkCancelActions"],
234                 {
235                         "ok": self.okbuttonClick ,
236                         "cancel": self.close
237                 }, -1)
238
239         def okbuttonClick(self):
240                 selection = self["nimlist"].getCurrent()[1]
241                 self.session.open(Satfinder, selection)
242
243 def SatfinderMain(session, **kwargs):
244         nimList = nimmanager.getNimListOfType("DVB-S")
245         if len(nimList) == 0:
246                 session.open(MessageBox, _("No satellite frontend found!!"), MessageBox.TYPE_ERROR)
247         else:
248                 if session.nav.RecordTimer.isRecording():
249                         session.open(MessageBox, _("A recording is currently running. Please stop the recording before trying to start the satfinder."), MessageBox.TYPE_ERROR)
250                 else:
251                         if len(nimList) == 1:
252                                 session.open(Satfinder, nimList[0])
253                         else:
254                                 session.open(SatNimSelection)
255
256 def SatfinderStart(menuid, **kwargs):
257         if menuid == "scan":
258                 return [(_("Satfinder"), SatfinderMain, "satfinder", None)]
259         else:
260                 return []
261
262 def Plugins(**kwargs):
263         if (nimmanager.hasNimType("DVB-S")):
264                 return PluginDescriptor(name=_("Satfinder"), description="Helps setting up your dish", where = PluginDescriptor.WHERE_MENU, fnc=SatfinderStart)
265         else:
266                 return []