f8aa07f49f4892f352eb7eb6f5c7f363e0ff242a
[openblackhole/openblackhole-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.eActionMap.getInstance()
13         assert p
14         
15         filename1 = "data/keymap.xml"
16         filename2 = "/usr/share/enigma2/keymap.xml"
17                 
18         try:
19                 source = open(filename1)
20                 filename = filename1
21         except:
22                 source = open(filename2)
23                 filename = filename2
24 #               raise "couldn't open keymap.xml!"
25         
26         try:
27                 dom = xml.dom.minidom.parse(source)
28         except:
29                 raise "keymap not well-formed."
30         
31         keymap = dom.childNodes[0]
32         
33         maps = elementsWithTag(keymap.childNodes, "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 elementsWithTag(keys.childNodes, "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                                 addKeyBinding(keyid, context, mapto)
75                 
76                 parseKeys("generic", cmap)
77                 
78                 for device in elementsWithTag(cmap.childNodes, "device"):
79                         parseKeys(str(device.getAttribute("name")), device)
80