implement often wished feature :)
[enigma2.git] / keymapparser.py
1 import xml.dom.minidom
2 import enigma
3
4 from keyids import KEYIDS;
5
6 # these are only informational (for help)...
7 from Tools.KeyBindings import addKeyBinding
8
9 def readKeymap():
10
11         p = enigma.eActionMapPtr()
12         enigma.eActionMap.getInstance(p)
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         try:    
32                 keymap = dom.getElementsByTagName("keymap")[0]
33         except:
34                 raise "no keymap defined."
35         
36         maps = keymap.getElementsByTagName("map")
37         
38         for cmap in maps:
39                 context = str(cmap.getAttribute("context"))
40                 assert context != "", "map must have context"
41         
42                 def parseKeys(device, keys):
43                         for x in keys.getElementsByTagName("key"):
44                                 mapto = str(x.getAttribute("mapto"))
45                                 id = x.getAttribute("id")
46                                 flags = x.getAttribute("flags")
47                                 
48                                 flag_ascii_to_id = lambda x: {'m':1,'b':2,'r':4}[x]
49                                 
50 #                               try:
51                                 flags = sum(map(flag_ascii_to_id, flags))
52 #                               print "-> " + str(flags)
53 #                               except:
54 #                                       raise str("%s: illegal flags '%s' specificed in context %s, id '%s'" % (filename, flags, context, id))
55                                 
56                                 assert mapto != "", "%s: must specify mapto in context %s, id '%s'" % (filename, context, id)
57                                 assert id != "", "%s: must specify id in context %s, mapto '%s'" % (filename, context, mapto)
58                                 assert flags != 0, "%s: must specify at least one flag in context %s, id '%s'" % (filename, context, id)
59                                 
60                                 if len(id) == 1:
61                                         keyid = ord(id) | 0x8000
62                                 elif id[0] == '\\':
63                                         if id[1] == 'x':
64                                                 keyid = int(id[2:], 0x10) | 0x8000
65                                         elif id[1] == 'd':
66                                                 keyid = int(id[2:]) | 0x8000
67                                         else:
68                                                 raise "key id '" + str(id) + "' is neither hex nor dec"
69                                 else:
70                                         try:
71                                                 keyid = KEYIDS[id]
72                                         except:
73                                                 raise "key id '" + str(id) + "' is illegal"
74
75 #                               print context + "::" + mapto + " -> " + device + "." + hex(keyid)
76                                 p.bindKey(device, keyid, flags, context, mapto)
77                                 addKeyBinding(keyid, context, mapto)
78                 
79                 parseKeys("generic", cmap)
80                 
81                 for device in cmap.getElementsByTagName("device"):
82                         parseKeys(str(device.getAttribute("name")), device)
83