add 'weight' for menu entries (including plugins), so they can be sorted, patch by...
[enigma2.git] / lib / python / Plugins / SystemPlugins / PositionerSetup / plugin.py
index 6cb0c087f0b9012d99b65e4afe23dfff964e62ce..fccd2625b6d024ceb922d3e3f66c2fb90679878a 100644 (file)
@@ -1,4 +1,7 @@
-from enigma import eTimer, eDVBSatelliteEquipmentControl, eDVBResourceManager, eDVBDiseqcCommand, eDVBResourceManagerPtr, iDVBChannelPtr, iDVBFrontendPtr, iDVBFrontend, eDVBFrontendParametersSatellite, eDVBFrontendParameters
+from enigma import eTimer, eDVBSatelliteEquipmentControl, eDVBResourceManager, \
+       eDVBDiseqcCommand, eDVBFrontendParametersSatellite, eDVBFrontendParameters,\
+       iDVBFrontend
+
 from Screens.Screen import Screen
 from Screens.ScanSetup import ScanSetup
 from Screens.MessageBox import MessageBox
@@ -10,7 +13,9 @@ from Components.TunerInfo import TunerInfo
 from Components.ActionMap import ActionMap
 from Components.NimManager import nimmanager
 from Components.MenuList import MenuList
-from Components.config import ConfigDummy, ConfigSelection
+from Components.config import ConfigSatlist, ConfigNothing, ConfigSelection, ConfigSubsection, KEY_LEFT, KEY_RIGHT, getConfigListEntry
+
+from time import sleep
 
 class PositionerSetup(Screen):
        skin = """
@@ -44,16 +49,24 @@ class PositionerSetup(Screen):
        def __init__(self, session, feid):
                self.skin = PositionerSetup.skin
                Screen.__init__(self, session)
-               
-               self.session.nav.stopService()
-               
                self.feid = feid
+               self.oldref = None
                
-               self.diseqc = Diseqc(self.feid)
-               self.tuner = Tuner(self.diseqc.getFrontend())
-               self.tuner.tune((0,0,0,0,0,0,0,0,0))
-               
-               #self.session.nav.stopService()
+               if not self.openFrontend():
+                       self.oldref = session.nav.getCurrentlyPlayingServiceReference()
+                       session.nav.stopService() # try to disable foreground service
+                       if not self.openFrontend():
+                               if session.pipshown: # try to disable pip
+                                       session.pipshown = False
+                                       del session.pip
+                                       if not self.openFrontend():
+                                               self.frontend = None # in normal case this should not happen
+
+               self.frontendStatus = { }
+
+               self.diseqc = Diseqc(self.frontend)
+               self.tuner = Tuner(self.frontend)
+               self.tuner.tune((0,0,0,0,0,0))
                
                self.createConfig()
                
@@ -77,13 +90,13 @@ class PositionerSetup(Screen):
                self["agc"] = Label()
                self["ber"] = Label()
                self["lock"] = Label()
-               self["snr_percentage"] = TunerInfo(TunerInfo.SNR_PERCENTAGE, frontendfkt = self.diseqc.getFrontend)
-               self["agc_percentage"] = TunerInfo(TunerInfo.AGC_PERCENTAGE, frontendfkt = self.diseqc.getFrontend)
-               self["ber_value"] = TunerInfo(TunerInfo.BER_VALUE, frontendfkt = self.diseqc.getFrontend)
-               self["snr_bar"] = TunerInfo(TunerInfo.SNR_BAR, frontendfkt = self.diseqc.getFrontend)
-               self["agc_bar"] = TunerInfo(TunerInfo.AGC_BAR, frontendfkt = self.diseqc.getFrontend)
-               self["ber_bar"] = TunerInfo(TunerInfo.BER_BAR, frontendfkt = self.diseqc.getFrontend)
-               self["lock_state"] = TunerInfo(TunerInfo.LOCK_STATE, frontendfkt = self.diseqc.getFrontend)
+               self["snr_percentage"] = TunerInfo(TunerInfo.SNR_PERCENTAGE, statusDict = self.frontendStatus)
+               self["agc_percentage"] = TunerInfo(TunerInfo.AGC_PERCENTAGE, statusDict = self.frontendStatus)
+               self["ber_value"] = TunerInfo(TunerInfo.BER_VALUE, statusDict = self.frontendStatus)
+               self["snr_bar"] = TunerInfo(TunerInfo.SNR_BAR, statusDict = self.frontendStatus)
+               self["agc_bar"] = TunerInfo(TunerInfo.AGC_BAR, statusDict = self.frontendStatus)
+               self["ber_bar"] = TunerInfo(TunerInfo.BER_BAR, statusDict = self.frontendStatus)
+               self["lock_state"] = TunerInfo(TunerInfo.LOCK_STATE, statusDict = self.frontendStatus)
 
                self["frequency"] = Label()
                self["symbolrate"] = Label()
@@ -96,7 +109,7 @@ class PositionerSetup(Screen):
                self["actions"] = ActionMap(["DirectionActions", "OkCancelActions", "ColorActions"],
                {
                        "ok": self.go,
-                       "cancel": self.close,
+                       "cancel": self.keyCancel,
                        "up": self.up,
                        "down": self.down,
                        "left": self.left,
@@ -112,49 +125,79 @@ class PositionerSetup(Screen):
                self.statusTimer = eTimer()
                self.statusTimer.timeout.get().append(self.updateStatus)
                self.statusTimer.start(50, False)
-               
+
+       def restartPrevService(self, yesno):
+               if yesno:
+                       if self.frontend:
+                               self.frontend = None
+                               del self.raw_channel
+                       self.session.nav.playService(self.oldref)
+               self.close(None)
+       
+       def keyCancel(self):
+               if self.oldref:
+                       self.session.openWithCallback(self.restartPrevService, MessageBox, _("Zap back to service before positioner setup?"), MessageBox.TYPE_YESNO)
+               else:
+                       self.restartPrevService(False)
+
+       def openFrontend(self):
+               res_mgr = eDVBResourceManager.getInstance()
+               if res_mgr:
+                       self.raw_channel = res_mgr.allocateRawChannel(self.feid)
+                       if self.raw_channel:
+                               self.frontend = self.raw_channel.getFrontend()
+                               if self.frontend:
+                                       return True
+                               else:
+                                       print "getFrontend failed"
+                       else:
+                               print "getRawChannel failed"
+               else:
+                       print "getResourceManager instance failed"
+               return False
+
        def createConfig(self):
-               self.positioner_tune = ConfigDummy()
-               self.positioner_move = ConfigDummy()
-               self.positioner_finemove = ConfigDummy()
-               self.positioner_limits = ConfigDummy()
-               self.positioner_goto0 = ConfigDummy()
+               self.positioner_tune = ConfigNothing()
+               self.positioner_move = ConfigNothing()
+               self.positioner_finemove = ConfigNothing()
+               self.positioner_limits = ConfigNothing()
+               self.positioner_goto0 = ConfigNothing()
                storepos = []
                for x in range(1,255):
                        storepos.append(str(x))
                self.positioner_storage = ConfigSelection(choices = storepos)
-       
+
        def createSetup(self):
-               self.list.append(getConfigListEntry(_("Tune"), self.positioner_tune))
-               self.list.append(getConfigListEntry(_("Positioner movement"), self.positioner_move))
-               self.list.append(getConfigListEntry(_("Positioner fine movement"), self.positioner_finemove))
-               self.list.append(getConfigListEntry(_("Set limits"), self.positioner_limits))
-               self.list.append(getConfigListEntry(_("Positioner storage"), self.positioner_storage))
-               self.list.append(getConfigListEntry(_("Goto 0"), self.positioner_goto0))
+               self.list.append((_("Tune"), self.positioner_tune, "tune"))
+               self.list.append((_("Positioner movement"), self.positioner_move, "move"))
+               self.list.append((_("Positioner fine movement"), self.positioner_finemove, "finemove"))
+               self.list.append((_("Set limits"), self.positioner_limits, "limits"))
+               self.list.append((_("Positioner storage"), self.positioner_storage, "storage"))
+               self.list.append((_("Goto 0"), self.positioner_goto0, "goto0"))
                self["list"].l.setList(self.list)
-               
+
        def go(self):
                pass
-       
+
        def getCurrentConfigPath(self):
-               return self["list"].getCurrent()[1].parent.configPath
-       
+               return self["list"].getCurrent()[2]
+
        def up(self):
                if not self.isMoving:
                        self["list"].instance.moveSelection(self["list"].instance.moveUp)
                        self.updateColors(self.getCurrentConfigPath())
-       
+
        def down(self):
                if not self.isMoving:
                        self["list"].instance.moveSelection(self["list"].instance.moveDown)
                        self.updateColors(self.getCurrentConfigPath())
-       
+
        def left(self):
-               self["list"].handleKey(config.key["prevElement"])
-       
+               self["list"].handleKey(KEY_LEFT)
+
        def right(self):
-               self["list"].handleKey(config.key["nextElement"])
-       
+               self["list"].handleKey(KEY_RIGHT)
+
        def updateColors(self, entry):
                if entry == "tune":
                        self.red.setText(_("Tune"))
@@ -197,7 +240,7 @@ class PositionerSetup(Screen):
                        self.green.setText("")
                        self.yellow.setText("")
                        self.blue.setText("")
-       
+
        def redKey(self):
                entry = self.getCurrentConfigPath()
                if entry == "move":
@@ -233,11 +276,11 @@ class PositionerSetup(Screen):
                        print "stepping west"
                        self.diseqccommand("moveWest", 0xFF) # one step
                elif entry == "storage":
-                       print "store at position", (self.positioner_storage.value + 1)
-                       self.diseqccommand("store", self.positioner_storage.value + 1)
+                       print "store at position", int(self.positioner_storage.value)
+                       self.diseqccommand("store", int(self.positioner_storage.value))
                elif entry == "limits":
                        self.diseqccommand("limitWest")
-       
+
        def yellowKey(self):
                entry = self.getCurrentConfigPath()
                if entry == "move":
@@ -254,11 +297,11 @@ class PositionerSetup(Screen):
                        print "stepping east"
                        self.diseqccommand("moveEast", 0xFF) # one step
                elif entry == "storage":
-                       print "move to position", (self.positioner_storage.value + 1)
-                       self.diseqccommand("moveTo", self.positioner_storage.value + 1)
+                       print "move to position", int(self.positioner_storage.value)
+                       self.diseqccommand("moveTo", int(self.positioner_storage.value))
                elif entry == "limits":
                        self.diseqccommand("limitEast")
-#      
+
        def blueKey(self):
                entry = self.getCurrentConfigPath()
                if entry == "move":
@@ -279,6 +322,8 @@ class PositionerSetup(Screen):
                self.tuner.retune()
 
        def updateStatus(self):
+               if self.frontend:
+                       self.frontend.getFrontendStatus(self.frontendStatus)
                self["snr_percentage"].update()
                self["agc_percentage"].update()
                self["ber_value"].update()
@@ -287,10 +332,10 @@ class PositionerSetup(Screen):
                self["ber_bar"].update()
                self["lock_state"].update()
                transponderdata = self.tuner.getTransponderData()
-               self["frequency_value"].setText(str(transponderdata["frequency"]))
-               self["symbolrate_value"].setText(str(transponderdata["symbol_rate"]))
-               self["fec_value"].setText(str(transponderdata["fec_inner"]))
-               if transponderdata["tuner_locked"] == 1 and self.isMoving and self.stopOnLock:
+               self["frequency_value"].setText(str(transponderdata.get("frequency")))
+               self["symbolrate_value"].setText(str(transponderdata.get("symbol_rate")))
+               self["fec_value"].setText(str(transponderdata.get("fec_inner")))
+               if self.frontendStatus.get("tuner_locked", 0) == 1 and self.isMoving and self.stopOnLock:
                        self.diseqccommand("stop")
                        self.isMoving = False
                        self.stopOnLock = False
@@ -299,30 +344,13 @@ class PositionerSetup(Screen):
        def tune(self, transponder):
                if transponder is not None:
                        self.tuner.tune(transponder)
-                       
+
 class Diseqc:
-       def __init__(self, feid = 0):
-               self.ready = False
-               self.feid = feid
-               res_mgr = eDVBResourceManagerPtr()
-               if eDVBResourceManager.getInstance(res_mgr) == 0:
-                       self.raw_channel = iDVBChannelPtr()
-                       if res_mgr.allocateRawChannel(self.raw_channel, self.feid) == 0:
-                               self.frontend = iDVBFrontendPtr()
-                               if self.raw_channel.getFrontend(self.frontend) == 0:
-                                       self.ready = True
-                               else:
-                                       print "getFrontend failed"
-                       else:
-                               print "getRawChannel failed"
-               else:
-                       print "getResourceManager instance failed"
-       
-       def getFrontend(self):
-               return self.frontend
-               
+       def __init__(self, frontend):
+               self.frontend = frontend
+
        def command(self, what, param = 0):
-               if self.ready:
+               if self.frontend:
                        cmd = eDVBDiseqcCommand()
                        if what == "moveWest":
                                string = 'e03169' + ("%02x" % param)
@@ -342,16 +370,21 @@ class Diseqc:
                                string = 'e03167'
                        else:
                                string = 'e03160' #positioner stop
+                       
                        print "diseqc command:",
                        print string
-                       
                        cmd.setCommandString(string)
+                       self.frontend.setTone(iDVBFrontend.toneOff)
+                       sleep(0.015) # wait 15msec after disable tone
                        self.frontend.sendDiseqc(cmd)
-                       
+                       if string == 'e03160': #positioner stop
+                               sleep(0.05)
+                               self.frontend.sendDiseqc(cmd) # send 2nd time
+
 class Tuner:
        def __init__(self, frontend):
                self.frontend = frontend
-               
+
        def tune(self, transponder):
                print "tuning to transponder with data", transponder
                parm = eDVBFrontendParametersSatellite()
@@ -362,17 +395,24 @@ class Tuner:
                parm.inversion = transponder[4]
                parm.orbital_position = transponder[5]
                parm.system = 0  # FIXMEE !! HARDCODED DVB-S (add support for DVB-S2)
-               parm.modulation = 1 # FIXMEE !! HARDCODED QPSK 
+               parm.modulation = 1 # FIXMEE !! HARDCODED QPSK
                feparm = eDVBFrontendParameters()
                feparm.setDVBS(parm, True)
                self.lastparm = feparm
-               self.frontend.tune(feparm)
-       
+               if self.frontend:
+                       self.frontend.tune(feparm)
+
        def retune(self):
-               self.frontend.tune(self.lastparm)
-       
+               if self.frontend:
+                       self.frontend.tune(self.lastparm)
+
        def getTransponderData(self):
-               return self.frontend.readTransponderData(True)
+               ret = { }
+               if self.frontend:
+                       self.frontend.getTransponderData(ret, True)
+               return ret
+
+tuning = None
 
 class TunerScreen(ScanSetup):
        skin = """
@@ -384,26 +424,24 @@ class TunerScreen(ScanSetup):
        def __init__(self, session, feid):
                self.feid = feid
                ScanSetup.__init__(self, session)
-
                self["introduction"].setText("")
-               
+
        def createSetup(self):
                self.typeOfTuningEntry = None
                self.satEntry = None
-
                self.list = []
-               self.typeOfTuningEntry = getConfigListEntry(_('Tune'), config.tuning.type)
+               self.typeOfTuningEntry = getConfigListEntry(_('Tune'), tuning.type)
                self.list.append(self.typeOfTuningEntry)
-               self.satEntry = getConfigListEntry(_('Satellite'), config.tuning.sat)
+               self.satEntry = getConfigListEntry(_('Satellite'), tuning.sat)
                self.list.append(self.satEntry)
-               if currentConfigSelectionElement(config.tuning.type) == "manual_transponder":
-                       self.list.append(getConfigListEntry(_('Frequency'), config.scan.sat.frequency))
-                       self.list.append(getConfigListEntry(_('Inversion'), config.scan.sat.inversion))
-                       self.list.append(getConfigListEntry(_('Symbol Rate'), config.scan.sat.symbolrate))
-                       self.list.append(getConfigListEntry(_("Polarity"), config.scan.sat.polarization))
-                       self.list.append(getConfigListEntry(_("FEC"), config.scan.sat.fec))
-               elif currentConfigSelectionElement(config.tuning.type) == "predefined_transponder":
-                       self.list.append(getConfigListEntry(_("Transponder"), config.tuning.transponder))
+               if tuning.type.value == "manual_transponder":
+                       self.list.append(getConfigListEntry(_('Frequency'), self.scan_sat.frequency))
+                       self.list.append(getConfigListEntry(_('Inversion'), self.scan_sat.inversion))
+                       self.list.append(getConfigListEntry(_('Symbol Rate'), self.scan_sat.symbolrate))
+                       self.list.append(getConfigListEntry(_("Polarity"), self.scan_sat.polarization))
+                       self.list.append(getConfigListEntry(_("FEC"), self.scan_sat.fec))
+               elif tuning.type.value == "predefined_transponder":
+                       self.list.append(getConfigListEntry(_("Transponder"), tuning.transponder))
                self["config"].list = self.list
                self["config"].l.setList(self.list)
 
@@ -411,24 +449,30 @@ class TunerScreen(ScanSetup):
                if self["config"].getCurrent() == self.typeOfTuningEntry:
                        self.createSetup()
                elif self["config"].getCurrent() == self.satEntry:
-                       self.updateSats()
                        self.createSetup()
 
        def createConfig(self, foo):
-               config.tuning = ConfigSubsection()
-               
-               config.tuning.type = configElement_nonSave("config.tuning.type", configSelection, 0, (("manual_transponder", _("Manual transponder")), ("predefined_transponder", _("Predefined satellite"))))
-               
-               config.tuning.sat = configElement_nonSave("config.tuning.sat", configSatlist, 192, nimmanager.getRotorSatListForNim(self.feid))
+               global tuning
+               if not tuning:
+                       tuning = ConfigSubsection()
+                       tuning.type = ConfigSelection(
+                               default = "manual_transponder",
+                               choices = { "manual_transponder" : _("Manual transponder"),
+                                                       "predefined_transponder" : _("Predefined transponder") } )
+                       tuning.sat = ConfigSatlist(list=nimmanager.getRotorSatListForNim(self.feid))
+                       tuning.sat.addNotifier(self.tuningSatChanged)
+                       self.updateTransponders()
+                       TunerScreenConfigCreated = True
                ScanSetup.createConfig(self, None)
-               self.updateSats()
-               
-       def updateSats(self):
-               satnum = config.tuning.sat.value
-               satlist = config.tuning.sat.vals
-               if len(satlist):
-                       transponderlist = nimmanager.getTransponders(satlist[satnum][1])
-                       list = []
+
+       def tuningSatChanged(self, *parm):
+               self.updateTransponders()
+
+       def updateTransponders(self):
+               if len(tuning.sat.choices):
+                       transponderlist = nimmanager.getTransponders(int(tuning.sat.value))
+                       tps = []
+                       cnt=0
                        for x in transponderlist:
                                if x[3] == 0:
                                        pol = "H"
@@ -454,17 +498,23 @@ class TunerScreen(ScanSetup):
                                        fec = "FEC_8_9"
                                elif x[4] == 6:
                                        fec = "FEC_None"
-                               list.append(str(x[1]) + "," + str(x[2]) + "," + pol + "," + fec)
-                       config.tuning.transponder = configElement_nonSave("config.tuning.transponder", configSelection, 0, list)
-       
+                               tps.append(str(x[1]) + "," + str(x[2]) + "," + pol + "," + fec)
+                       tuning.transponder = ConfigSelection(choices=tps)
+
        def keyGo(self):
-               returnvalue = (0, 0, 0, 0, 0, 0, 0)
-               satpos = config.tuning.sat.vals[config.tuning.sat.value][1]
-               if currentConfigSelectionElement(config.tuning.type) == "manual_transponder":
-                       returnvalue = (config.scan.sat.frequency.value[0], config.scan.sat.symbolrate.value[0], config.scan.sat.polarization.value, config.scan.sat.fec.value, config.scan.sat.inversion.value, satpos)
-               elif currentConfigSelectionElement(config.tuning.type) == "predefined_transponder":
-                       transponder = nimmanager.getTransponders(config.tuning.sat.vals[config.tuning.sat.value][1])[config.tuning.transponder.value]
-                       returnvalue = (int(transponder[1] / 1000), int(transponder[2] / 1000), transponder[3], transponder[4], 2, config.tuning.sat.vals[config.tuning.sat.value][1], satpos)
+               returnvalue = (0, 0, 0, 0, 0, 0)
+               satpos = int(tuning.sat.value)
+               if tuning.type.value == "manual_transponder":
+                       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)
+               elif tuning.type.value == "predefined_transponder":
+                       transponder = nimmanager.getTransponders(satpos)[tuning.transponder.index]
+                       returnvalue = (int(transponder[1] / 1000), int(transponder[2] / 1000), transponder[3], transponder[4], 2, satpos)
                self.close(returnvalue)
 
        def keyCancel(self):
@@ -475,14 +525,15 @@ class NimSelection(Screen):
                <screen position="140,165" size="400,100" title="select Slot">
                        <widget name="nimlist" position="20,10" size="360,75" />
                </screen>"""
-               
+
        def __init__(self, session):
                Screen.__init__(self, session)
 
                nimlist = nimmanager.getNimListOfType(nimmanager.nimType["DVB-S"])
                nimMenuList = []
                for x in nimlist:
-                       nimMenuList.append((_("NIM ") + (["A", "B", "C", "D"][x]) + ": " + nimmanager.getNimName(x) + " (" + nimmanager.getNimTypeName(x) + ")", x))
+                       n = nimmanager.nim_slots[x]
+                       nimMenuList.append((n.friendly_full_name, x))
                
                self["nimlist"] = MenuList(nimMenuList)
 
@@ -497,7 +548,7 @@ class NimSelection(Screen):
                self.session.open(PositionerSetup, selection[1])
 
 def PositionerMain(session, **kwargs):
-       nimList = nimmanager.getNimListOfType(nimmanager.nimType["DVB-S"])
+       nimList = nimmanager.getNimListOfType("DVB-S")
        if len(nimList) == 0:
                session.open(MessageBox, _("No positioner capable frontend found."), MessageBox.TYPE_ERROR)
        else:
@@ -516,5 +567,14 @@ def PositionerMain(session, **kwargs):
                        else:
                                session.open(MessageBox, _("No tuner is configured for use with a diseqc positioner!"), MessageBox.TYPE_ERROR)
 
+def PositionerSetupStart(menuid, **kwargs):
+       if menuid == "scan":
+               return [(_("Positioner setup"), PositionerMain, "positioner_setup", None)]
+       else:
+               return []
+
 def Plugins(**kwargs):
-       return PluginDescriptor(name="Positioner setup", description="Setup your positioner", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=PositionerMain)
+       if (nimmanager.hasNimType("DVB-S")):
+               return PluginDescriptor(name=_("Positioner setup"), description="Setup your positioner", where = PluginDescriptor.WHERE_MENU, fnc=PositionerSetupStart)
+       else:
+               return []