make keymap configurable with config.usage.keymap
[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(filename):
11         p = enigma.eActionMap.getInstance()
12         assert p
13
14         source = open(filename)
15
16         try:
17                 dom = xml.dom.minidom.parse(source)
18         except:
19                 raise "keymap %s not well-formed." % filename
20
21         keymap = dom.childNodes[0]
22
23         maps = elementsWithTag(keymap.childNodes, "map")
24
25         for cmap in maps:
26                 context = str(cmap.getAttribute("context"))
27                 assert context != "", "map must have context"
28
29                 def parseKeys(device, keys):
30                         for x in elementsWithTag(keys.childNodes, "key"):
31                                 mapto = str(x.getAttribute("mapto"))
32                                 id = x.getAttribute("id")
33                                 flags = x.getAttribute("flags")
34
35                                 flag_ascii_to_id = lambda x: {'m':1,'b':2,'r':4,'l':8}[x]
36
37 #                               try:
38                                 flags = sum(map(flag_ascii_to_id, flags))
39 #                               print "-> " + str(flags)
40 #                               except:
41 #                                       raise str("%s: illegal flags '%s' specificed in context %s, id '%s'" % (filename, flags, context, id))
42
43                                 assert mapto != "", "%s: must specify mapto in context %s, id '%s'" % (filename, context, id)
44                                 assert id != "", "%s: must specify id in context %s, mapto '%s'" % (filename, context, mapto)
45                                 assert flags != 0, "%s: must specify at least one flag in context %s, id '%s'" % (filename, context, id)
46
47                                 if len(id) == 1:
48                                         keyid = ord(id) | 0x8000
49                                 elif id[0] == '\\':
50                                         if id[1] == 'x':
51                                                 keyid = int(id[2:], 0x10) | 0x8000
52                                         elif id[1] == 'd':
53                                                 keyid = int(id[2:]) | 0x8000
54                                         else:
55                                                 raise "key id '" + str(id) + "' is neither hex nor dec"
56                                 else:
57                                         try:
58                                                 keyid = KEYIDS[id]
59                                         except:
60                                                 raise "key id '" + str(id) + "' is illegal"
61
62 #                               print context + "::" + mapto + " -> " + device + "." + hex(keyid)
63                                 p.bindKey(filename, device, keyid, flags, context, mapto)
64                                 addKeyBinding(filename, keyid, context, mapto, flags)
65
66                 parseKeys("generic", cmap)
67
68                 for device in elementsWithTag(cmap.childNodes, "device"):
69                         parseKeys(str(device.getAttribute("name")), device)
70
71 def removeKeymap(filename):
72         p = enigma.eActionMap.getInstance()
73         p.unbindKeyDomain(filename)