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