add support for fluendo ts/ps demux (preferred above mpegdemux), add support for...
[enigma2.git] / keymapparser.py
1 import xml.dom.minidom
2 import enigma
3 from Tools.XMLTools import elementsWithTag
4
5 from keyids import KEYIDS;
6
7 # these are only informational (for help)...
8 from Tools.KeyBindings import addKeyBinding
9
10 def readKeymap():
11
12         p = enigma.eActionMapPtr()
13         enigma.eActionMap.getInstance(p)
14         assert p
15         
16         filename1 = "data/keymap.xml"
17         filename2 = "/usr/share/enigma2/keymap.xml"
18                 
19         try:
20                 source = open(filename1)
21                 filename = filename1
22         except:
23                 source = open(filename2)
24                 filename = filename2
25 #               raise "couldn't open keymap.xml!"
26         
27         try:
28                 dom = xml.dom.minidom.parse(source)
29         except:
30                 raise "keymap not well-formed."
31         
32         keymap = dom.childNodes[0]
33         
34         maps = elementsWithTag(keymap.childNodes, "map")
35         
36         for cmap in maps:
37                 context = str(cmap.getAttribute("context"))
38                 assert context != "", "map must have context"
39         
40                 def parseKeys(device, keys):
41                         for x in elementsWithTag(keys.childNodes, "key"):
42                                 mapto = str(x.getAttribute("mapto"))
43                                 id = x.getAttribute("id")
44                                 flags = x.getAttribute("flags")
45                                 
46                                 flag_ascii_to_id = lambda x: {'m':1,'b':2,'r':4}[x]
47                                 
48 #                               try:
49                                 flags = sum(map(flag_ascii_to_id, flags))
50 #                               print "-> " + str(flags)
51 #                               except:
52 #                                       raise str("%s: illegal flags '%s' specificed in context %s, id '%s'" % (filename, flags, context, id))
53                                 
54                                 assert mapto != "", "%s: must specify mapto in context %s, id '%s'" % (filename, context, id)
55                                 assert id != "", "%s: must specify id in context %s, mapto '%s'" % (filename, context, mapto)
56                                 assert flags != 0, "%s: must specify at least one flag in context %s, id '%s'" % (filename, context, id)
57                                 
58                                 if len(id) == 1:
59                                         keyid = ord(id) | 0x8000
60                                 elif id[0] == '\\':
61                                         if id[1] == 'x':
62                                                 keyid = int(id[2:], 0x10) | 0x8000
63                                         elif id[1] == 'd':
64                                                 keyid = int(id[2:]) | 0x8000
65                                         else:
66                                                 raise "key id '" + str(id) + "' is neither hex nor dec"
67                                 else:
68                                         try:
69                                                 keyid = KEYIDS[id]
70                                         except:
71                                                 raise "key id '" + str(id) + "' is illegal"
72
73 #                               print context + "::" + mapto + " -> " + device + "." + hex(keyid)
74                                 p.bindKey(device, keyid, flags, context, mapto)
75                                 addKeyBinding(keyid, context, mapto)
76                 
77                 parseKeys("generic", cmap)
78                 
79                 for device in elementsWithTag(cmap.childNodes, "device"):
80                         parseKeys(str(device.getAttribute("name")), device)
81