fix movieplayer
[enigma2.git] / lib / python / Components / NimManager.py
1 from config import config       #global config instance
2
3 from config import configElement
4 from config import ConfigSubsection
5 from config import ConfigSlider
6 from config import configSelection
7 from config import configSequence
8 from config import configsequencearg
9 from config import configSatlist
10
11 from enigma import *
12
13 import xml.dom.minidom
14 from xml.dom import EMPTY_NAMESPACE
15 from skin import elementsWithTag
16 from Tools import XMLTools
17
18 from xml.sax import make_parser
19 from xml.sax.handler import ContentHandler
20
21 from Tools.BoundFunction import boundFunction
22
23 def tryOpen(filename):
24         try:
25                 procFile = open(filename)
26         except IOError:
27                 return ""
28         return procFile
29
30 class SecConfigure:
31         def addLNBSimple(self, slotid, diseqcmode, toneburstmode = 0, diseqcpos = 0, orbpos = 0, longitude = 0, latitude = 0, loDirection = 0, laDirection = 0):
32                 #simple defaults
33                 sec = eDVBSatelliteEquipmentControl.getInstance()
34                 sec.addLNB()
35                 sec.setLNBTunerMask(1 << slotid)
36                 sec.setLNBLOFL(9750000)
37                 sec.setLNBLOFH(10600000)
38                 sec.setLNBThreshold(11750000)
39                 sec.setRepeats(0)
40                 sec.setFastDiSEqC(0)
41                 sec.setSeqRepeat(0)
42                 sec.setVoltageMode(0) #HV
43                 sec.setToneMode(0)              #HILO
44                 sec.setCommandOrder(0)
45                 #user values
46                 sec.setDiSEqCMode(diseqcmode)
47                 sec.setToneburst(toneburstmode)
48                 sec.setCommittedCommand(diseqcpos)
49                 #print "set orbpos to:" + str(orbpos)
50
51                 if (0 <= diseqcmode < 3):
52                         sec.addSatellite(orbpos)
53                         self.satList.append(orbpos)
54                 elif (diseqcmode == 3): # diseqc 1.2
55                         sec.setLatitude(latitude)
56                         sec.setLaDirection(laDirection)
57                         sec.setLongitude(longitude)
58                         sec.setLoDirection(loDirection)
59                         sec.setUseInputpower(True)
60                         sec.setInputpowerDelta(50)
61                         
62                         for x in self.NimManager.satList:
63                                 print "Add sat " + str(x[1])
64                                 sec.addSatellite(int(x[1]))
65                                 sec.setVoltageMode(0)
66                                 sec.setToneMode(0)
67                                 self.satList.append(int(x[1]))
68                                 
69
70         def linkNIMs(self, nim1, nim2):
71                 eDVBSatelliteEquipmentControl.getInstance().setTunerLinked(nim1, nim2)
72                 
73         def getSatList(self):
74                 return self.satList
75
76         def update(self):
77                 eDVBSatelliteEquipmentControl.getInstance().clear()
78                 
79                 self.satList = []
80
81                 for slot in self.NimManager.nimslots:
82                         x = slot.slotid
83                         nim = config.Nims[x]
84                         if slot.nimType == self.NimManager.nimType["DVB-S"]:
85                                 print "slot: " + str(x) + " configmode: " + str(nim.configMode.value)
86                                 if nim.configMode.value == 2:
87                                         self.linkNIMs(x, nim.linkedTo.value)
88                                         nim = config.Nims[nim.linkedTo.value]
89                                 if nim.configMode.value == 0:           #simple config
90                                         if nim.diseqcMode.value == 0:                   #single
91                                                 self.addLNBSimple(slotid = x, orbpos = int(nim.diseqcA.vals[nim.diseqcA.value][1]), toneburstmode = 0, diseqcmode = 0, diseqcpos = 4)
92                                         elif nim.diseqcMode.value == 1:         #Toneburst A/B
93                                                 self.addLNBSimple(slotid = x, orbpos = int(nim.diseqcA.vals[nim.diseqcA.value][1]), toneburstmode = 1, diseqcmode = 0, diseqcpos = 4)
94                                                 self.addLNBSimple(slotid = x, orbpos = int(nim.diseqcB.vals[nim.diseqcB.value][1]), toneburstmode = 1, diseqcmode = 0, diseqcpos = 4)
95                                         elif nim.diseqcMode.value == 2:         #DiSEqC A/B
96                                                 self.addLNBSimple(slotid = x, orbpos = int(nim.diseqcA.vals[nim.diseqcA.value][1]), toneburstmode = 0, diseqcmode = 1, diseqcpos = 0)
97                                                 self.addLNBSimple(slotid = x, orbpos = int(nim.diseqcB.vals[nim.diseqcB.value][1]), toneburstmode = 0, diseqcmode = 1, diseqcpos = 1)
98                                         elif nim.diseqcMode.value == 3:         #DiSEqC A/B/C/D
99                                                 self.addLNBSimple(slotid = x, orbpos = int(nim.diseqcA.vals[nim.diseqcA.value][1]), toneburstmode = 0, diseqcmode = 1, diseqcpos = 0)
100                                                 self.addLNBSimple(slotid = x, orbpos = int(nim.diseqcB.vals[nim.diseqcB.value][1]), toneburstmode = 0, diseqcmode = 1, diseqcpos = 1)
101                                                 self.addLNBSimple(slotid = x, orbpos = int(nim.diseqcC.vals[nim.diseqcC.value][1]), toneburstmode = 0, diseqcmode = 1, diseqcpos = 2)
102                                                 self.addLNBSimple(slotid = x, orbpos = int(nim.diseqcD.vals[nim.diseqcD.value][1]), toneburstmode = 0, diseqcmode = 1, diseqcpos = 3)
103                                         elif nim.diseqcMode.value == 4:         #Positioner
104                                                 self.addLNBSimple(slotid = x, diseqcmode = 3, longitude = float(str(nim.longitude.value[0]) + "." + str(nim.longitude.value[1])), loDirection = nim.longitudeOrientation.value - 2, latitude = float(str(nim.latitude.value[0]) + "." + str(nim.latitude.value[1])), laDirection = nim.latitudeOrientation.value)
105                                         pass
106                                 else:                                                                                                                                   #advanced config
107                                         print "FIXME add support for advanced config"
108                 
109         def __init__(self, nimmgr):
110                 self.NimManager = nimmgr
111                 self.update()
112                 
113 class nimSlot:
114         def __init__(self, slotid, nimtype, name):
115                 self.slotid = slotid
116                 self.nimType = nimtype
117                 self.name = name
118
119 class NimManager:
120         class parseSats(ContentHandler):
121                 def __init__(self, satList, satellites, transponders):
122                         self.isPointsElement, self.isReboundsElement = 0, 0
123                         self.satList = satList
124                         self.satellites = satellites
125                         self.transponders = transponders
126         
127                 def startElement(self, name, attrs):
128                         if (name == "sat"):
129                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
130                                 tpos = int(attrs.get('position',""))
131                                 if tpos < 0:
132                                         tpos = 3600 + tpos
133                                 tname = attrs.get('name',"")
134                                 self.satellites[tpos] = tname
135                                 self.satList.append( (tname, tpos) )
136                                 self.parsedSat = int(tpos)
137                         elif (name == "transponder"):
138                                 freq = int(attrs.get('frequency',""))
139                                 sr = int(attrs.get('symbol_rate',""))
140                                 pol = int(attrs.get('polarization',""))
141                                 fec = int(attrs.get('fec_inner',""))
142                                 if self.parsedSat in self.transponders:
143                                         pass
144                                 else:
145                                         self.transponders[self.parsedSat] = [ ]
146
147                                 self.transponders[self.parsedSat].append((0, freq, sr, pol, fec))
148
149         class parseCables(ContentHandler):
150                 def __init__(self, cablesList, transponders):
151                         self.isPointsElement, self.isReboundsElement = 0, 0
152                         self.cablesList = cablesList
153                         self.transponders = transponders
154         
155                 def startElement(self, name, attrs):
156                         if (name == "cable"):
157                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
158                                 tname = attrs.get('name',"")
159                                 self.cablesList.append(str(tname))
160                                 self.parsedCab = str(tname)
161                         elif (name == "transponder"):
162                                 freq = int(attrs.get('frequency',""))
163                                 sr = int(attrs.get('symbol_rate',""))
164                                 mod = int(attrs.get('modulation',""))
165                                 fec = int(attrs.get('fec_inner',""))
166                                 if self.parsedCab in self.transponders:
167                                         pass
168                                 else:
169                                         self.transponders[self.parsedCab] = [ ]
170
171                                 self.transponders[self.parsedCab].append((1, freq, sr, mod, fec))
172
173         class parseTerrestrials(ContentHandler):
174                 def __init__(self, terrestrialsList, transponders):
175                         self.isPointsElement, self.isReboundsElement = 0, 0
176                         self.terrestrialsList = terrestrialsList
177                         self.transponders = transponders
178         
179                 def startElement(self, name, attrs):
180                         if (name == "terrestrial"):
181                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
182                                 tname = attrs.get('name',"")
183                                 tflags = attrs.get('flags',"")
184                                 self.terrestrialsList.append((tname, tflags))
185                                 self.parsedTer = str(tname)
186                         elif (name == "transponder"):
187                                 # TODO finish this!
188                                 freq = int(attrs.get('centre_frequency',""))
189                                 bw = int(attrs.get('bandwidth',""))
190                                 const = int(attrs.get('constellation',""))
191                                 crh = int(attrs.get('code_rate_hp',""))
192                                 crl = int(attrs.get('code_rate_lp',""))
193                                 guard = int(attrs.get('guard_interval',""))
194                                 transm = int(attrs.get('transmission_mode',""))
195                                 hierarchy = int(attrs.get('hierarchy_information',""))
196                                 inv = int(attrs.get('inversion',""))
197                                 if self.parsedTer in self.transponders:
198                                         pass
199                                 else:
200                                         self.transponders[self.parsedTer] = [ ]
201
202                                 self.transponders[self.parsedTer].append((2, freq, bw, const, crh, crl, guard, transm, hierarchy, inv))
203
204         def getTransponders(self, pos):
205                 return self.transponders[pos]
206
207         def getConfiguredSats(self):
208                 return self.sec.getSatList()
209
210         def getSatDescription(self, pos):
211                 return self.satellites[pos]
212
213         def readSatsfromFile(self):
214                 self.satellites = { }
215                 self.transponders = { }
216                 self.transponderscable = { }
217                 self.transpondersterrestrial = { }              
218                 
219                 parser = make_parser()
220                 if (self.hasNimType(self.nimType["DVB-S"])):
221                         print "Reading satellites.xml"
222                         satHandler = self.parseSats(self.satList, self.satellites, self.transponders)
223                         parser.setContentHandler(satHandler)
224                         parser.parse('/etc/tuxbox/satellites.xml')
225                 if (self.hasNimType(self.nimType["DVB-C"])):
226                         print "Reading cables.xml"
227                         cabHandler = self.parseCables(self.cablesList, self.transponderscable)
228                         parser.setContentHandler(cabHandler)
229                         parser.parse('/etc/tuxbox/cables.xml')
230
231                 if (self.hasNimType(self.nimType["DVB-T"])):
232                         print "Reading terrestrial.xml"
233                         terHandler = self.parseTerrestrials(self.terrestrialsList, self.transpondersterrestrial)
234                         parser.setContentHandler(terHandler)
235                         parser.parse('/etc/tuxbox/terrestrial.xml')
236                 
237         def parseProc(self):
238                 self.nimTypes = {}
239                 self.nimNames = {}              
240                 self.nimSocketCount = 0
241                 nimfile = tryOpen("/proc/bus/nim_sockets")
242
243                 if nimfile == "":
244                                 return self.nimType["empty/unknown"]
245                         
246                 lastsocket = -1
247
248                 while 1:                
249                         line = nimfile.readline()
250                         if line == "":
251                                 break
252                         if line.strip().startswith("NIM Socket"):
253                                 parts = line.strip().split(" ")
254                                 id = int(parts[2][:1])
255                                 lastsocket = int(id)
256                                 self.nimSocketCount += 1
257                         elif line.strip().startswith("Type:"):
258                                 self.nimTypes[lastsocket] = str(line.strip()[6:])
259                         elif line.strip().startswith("Name:"):
260                                 self.nimNames[lastsocket] = str(line.strip()[6:])
261                         elif line.strip().startswith("empty"):
262                                 self.nimNames[lastsocket] = _("N/A")
263                                 self.nimTypes[lastsocket] = "empty/unknown"
264
265                 nimfile.close()
266
267         def getNimType(self, slotID):
268                 if slotID >= self.nimCount:
269                         return "empty/unknown"
270                 else:   
271                         return self.nimType[self.nimTypes[slotID]]
272                         
273         def getNimName(self, slotID):
274                 return self.nimNames[slotID]
275
276         def getNimSocketCount(self):
277                 return self.nimSocketCount
278         
279         def hasNimType(self, chktype):
280                 for id, type in self.nimTypes.items():
281                         if (chktype == self.nimType[str(type)]):
282                                 return True
283                 return False
284         
285         def getNimListOfType(self, type, exception = -1):
286                 list = []
287                 for x in self.nimslots:
288                         if ((x.nimType == type) and (x.slotid != exception)):
289                                 list.append(x.slotid)
290                 return list
291
292         def getConfigPrefix(self, slotid):
293                 return "config.Nim" + ("A","B","C","D")[slotid] + "."
294                         
295         def __init__(self):
296                 #use as enum
297                 self.nimType = {                "empty/unknown": -1,
298                                                                                                 "DVB-S": 0,
299                                                                                                 "DVB-C": 1,
300                                                                                                 "DVB-T": 2}
301                 self.satList = [ ]
302                 self.cablesList = []
303                 self.terrestrialsList = []
304                                                                                                 
305                 self.parseProc()
306
307                 self.readSatsfromFile()                                                 
308                 
309                 self.nimCount = self.getNimSocketCount()
310                 
311                 self.nimslots = [ ]
312                 x = 0
313                 while x < self.nimCount:
314                         tType = self.getNimType(x)
315                         tName = self.getNimName(x)
316                         tNim = nimSlot(x, tType, tName)
317                         self.nimslots.append(tNim)
318                         x += 1
319                 
320                 InitNimManager(self)    #init config stuff
321
322         def nimList(self):
323                 list = [ ]
324                 for slot in self.nimslots:
325                         nimText = _("Socket ") + ("A", "B", "C", "D")[slot.slotid] + ": "
326                         if slot.nimType == -1:
327                                 nimText += _("empty/unknown")
328                         else:
329                                 nimText += slot.name + " ("     
330                                 nimText += ("DVB-S", "DVB-C", "DVB-T")[slot.nimType] + ")"
331                         list.append((nimText, slot))
332                 return list
333         
334         def getSatListForNim(self, slotid):
335                 list = []
336                 if (self.getNimType(slotid) != self.nimType["empty/unknown"]):
337                         #print "slotid:", slotid
338                         
339                         #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
340                         #print "diseqcA:", config.Nims[slotid].diseqcA.value
341                         if (config.Nims[slotid].diseqcMode.value <= 3):
342                                 list.append(self.satList[config.Nims[slotid].diseqcA.value])
343                         if (0 < config.Nims[slotid].diseqcMode.value <= 3):
344                                 list.append(self.satList[config.Nims[slotid].diseqcB.value])
345                         if (config.Nims[slotid].diseqcMode.value == 3):
346                                 list.append(self.satList[config.Nims[slotid].diseqcC.value])
347                                 list.append(self.satList[config.Nims[slotid].diseqcD.value])
348                         if (config.Nims[slotid].diseqcMode.value == 4):
349                                 for x in self.satList:
350                                         list.append(x)
351                 return list
352
353         #callbacks for c++ config
354         def nimConfigModeChanged(self, slotid, mode):
355                 if (mode != 2): # not linked
356                         print "Unlinking slot " + str(slotid)
357                         # TODO call c++ to unlink nim in slot slotid
358                 if (mode == 2): # linked
359                         pass
360                         #FIXME!!!
361                         #if (len(self.getNimListOfType(self.nimType["DVB-S"], slotid)) > 0):
362                         #       print "Linking slot " + str(slotid) + " to " + str(nimmgr.getConfigPrefix(slotid).value)
363                         # TODO call c++ to link nim in slot slotid with nim in slot nimmgr.getConfigPrefix(slotid).value
364         def nimLinkedToChanged(self, slotid, val):
365                 print "Linking slot " + str(slotid) + " to " + str(val)
366
367         def nimDiseqcModeChanged(self, slotid, mode):
368                 #print "nimDiseqcModeChanged set to " + str(mode)
369                 pass
370         def nimPortAChanged(self, slotid, val):
371                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
372                 pass
373         def nimPortBChanged(self, slotid, val):
374                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
375                 #print "nimDiseqcB set to " + str(val)
376                 pass
377         def nimPortCChanged(self, slotid, val):
378                 #print "nimDiseqcC set to " + str(val)
379                 pass
380         def nimPortDChanged(self, slotid, val):
381                 #print "nimDiseqcD set to " + str(val)
382                 pass
383
384 def InitNimManager(nimmgr):
385         config.Nims = []
386         for x in range(nimmgr.nimCount):
387                 config.Nims.append(ConfigSubsection())
388                 
389         def nimConfigModeChanged(slotid, configElement):
390                 nimmgr.nimConfigModeChanged(slotid, configElement.value)
391         def nimLinkedToChanged(slotid, configElement):
392                 nimmgr.nimLinkedToChanged(slotid, configElement.value)
393         def nimDiseqcModeChanged(slotid, configElement):
394                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
395                 
396         def nimPortAChanged(slotid, configElement):
397                 nimmgr.nimPortAChanged(slotid, configElement.vals[configElement.value][1])
398         def nimPortBChanged(slotid, configElement):
399                 nimmgr.nimPortBChanged(slotid, configElement.vals[configElement.value][1])
400         def nimPortCChanged(slotid, configElement):
401                 nimmgr.nimPortCChanged(slotid, configElement.vals[configElement.value][1])
402         def nimPortDChanged(slotid, configElement):
403                 nimmgr.nimPortDChanged(slotid, configElement.vals[configElement.value][1])
404
405         for slot in nimmgr.nimslots:
406                 x = slot.slotid
407                 cname = nimmgr.getConfigPrefix(x)
408                 nim = config.Nims[x]
409                 
410                 if slot.nimType == nimmgr.nimType["DVB-S"]:
411                         nim.configMode = configElement(cname + "configMode", configSelection, 0, (_("Simple"), _("Nothing connected"), _("Loopthrough to Socket A"))) # "Advanced"));
412                         
413                         #important - check if just the 2nd one is LT only and the first one is DVB-S
414                         if nim.configMode.value == 2: #linked
415                                 if x == 0:                                                                              #first one can never be linked to anything
416                                         nim.configMode.value = 0                #reset to simple
417                                         nim.configMode.save()
418                                 else:
419                                         #FIXME: make it better
420                                         for y in nimmgr.nimslots:
421                                                 if y.slotid == 0:
422                                                         if y.nimType != nimmgr.nimType["DVB-S"]:
423                                                                 nim.configMode.value = 0                #reset to simple
424                                                                 nim.configMode.save()
425
426                         nim.diseqcMode = configElement(cname + "diseqcMode", configSelection, 2, (_("Single"), _("Toneburst A/B"), _("DiSEqC A/B"), _("DiSEqC A/B/C/D"), _("Positioner")));
427                         nim.diseqcA = configElement(cname + "diseqcA", configSatlist, 192, nimmgr.satList);
428                         nim.diseqcB = configElement(cname + "diseqcB", configSatlist, 130, nimmgr.satList);
429                         nim.diseqcC = configElement(cname + "diseqcC", configSatlist, 0, nimmgr.satList);
430                         nim.diseqcD = configElement(cname + "diseqcD", configSatlist, 0, nimmgr.satList);
431                         nim.positionerMode = configElement(cname + "positionerMode", configSelection, 0, (_("USALS"), _("manual")));
432                         nim.longitude = configElement(cname + "longitude", configSequence, [5,100], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
433                         nim.longitudeOrientation = configElement(cname + "longitudeOrientation", configSelection, 0, (_("East"), _("West")))
434                         nim.latitude = configElement(cname + "latitude", configSequence, [50,767], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
435                         nim.latitudeOrientation = configElement(cname + "latitudeOrientation", configSelection, 0, (_("North"), _("South")))
436                         satNimList = nimmgr.getNimListOfType(nimmgr.nimType["DVB-S"], slot.slotid)
437                         satNimListNames = []
438                         for x in satNimList:
439                                 satNimListNames.append(_("Slot ") + ("A", "B", "C", "D")[x] + ": " + nimmgr.getNimName(x))
440                         nim.linkedTo = configElement(cname + "linkedTo", configSelection, 0, satNimListNames);
441                         
442                         #perhaps the instance of the slot is more useful?
443                         nim.configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
444                         nim.diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
445                         nim.diseqcA.addNotifier(boundFunction(nimPortAChanged,int(x)))
446                         nim.diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
447                         nim.diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
448                         nim.diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
449                         nim.linkedTo.addNotifier(boundFunction(nimLinkedToChanged,x))
450                 elif slot.nimType == nimmgr.nimType["DVB-C"]:
451                         nim.cable = configElement(cname + "cable", configSelection, 0, nimmgr.cablesList);
452                 elif slot.nimType == nimmgr.nimType["DVB-T"]:
453                         nim.cable = configElement(cname + "terrestrial", configSelection, 0, nimmgr.terrestrialsList);
454                 else:
455                         print "pls add support for this frontend type!"         
456
457         nimmgr.sec = SecConfigure(nimmgr)
458
459 nimmanager = NimManager()