- fix serious problems in widget code. fixup buffered mode. add animation support...
[openblackhole/openblackhole-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         except:
18                 source = open(filename2)
19 #               raise "couldn't open keymap.xml!"
20         
21         try:
22                 dom = xml.dom.minidom.parse(source)
23         except:
24                 raise "keymap not well-formed."
25         
26         try:    
27                 keymap = dom.getElementsByTagName("keymap")[0]
28         except:
29                 raise "no keymap defined."
30         
31         maps = keymap.getElementsByTagName("map")
32         
33         for cmap in maps:
34                 context = str(cmap.getAttribute("context"))
35                 assert context != "", "map must have context"
36         
37                 def parseKeys(device, keys):
38                         for x in keys.getElementsByTagName("key"):
39                                 mapto = str(x.getAttribute("mapto"))
40                                 id = x.getAttribute("id")
41                                 flags = x.getAttribute("flags")
42                                 
43                                 flag_ascii_to_id = lambda x: {'m':1,'b':2,'r':4}[x]
44                                 
45 #                               try:
46                                 flags = sum(map(flag_ascii_to_id, flags))
47 #                               print "-> " + str(flags)
48 #                               except:
49 #                                       raise str("%s: illegal flags '%s' specificed in context %s, id '%s'" % (filename, flags, context, id))
50                                 
51                                 assert mapto != "", "%s: must specify mapto in context %s, id '%s'" % (filename, context, id)
52                                 assert id != "", "%s: must specify id in context %s, mapto '%s'" % (filename, context, mapto)
53                                 assert flags != 0, "%s: must specify at least one flag in context %s, id '%s'" % (filename, context, id)
54                                 
55                                 if len(id) == 1:
56                                         keyid = ord(id) | 0x8000
57                                 elif id[0] == '\\':
58                                         if id[1] == 'x':
59                                                 keyid = int(id[2:], 0x10) | 0x8000
60                                         elif id[1] == 'd':
61                                                 keyid = int(id[2:]) | 0x8000
62                                         else:
63                                                 raise "key id '" + str(id) + "' is neither hex nor dec"
64                                 else:
65                                         try:
66                                                 keyid = KEYIDS[id]
67                                         except:
68                                                 raise "key id '" + str(id) + "' is illegal"
69
70 #                               print context + "::" + mapto + " -> " + device + "." + hex(keyid)
71                                 p.bindKey(device, keyid, flags, context, mapto)
72                 
73                 parseKeys("generic", cmap)
74                 
75                 for device in cmap.getElementsByTagName("device"):
76                         parseKeys(str(device.getAttribute("name")), device)
77