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