4.5.0r6
[enigma2.git] / usr / lib / enigma2 / python / keymapparser.py
1 import enigma
2 import xml.etree.cElementTree
3
4 from keyids import KEYIDS
5
6 # these are only informational (for help)...
7 from Tools.KeyBindings import addKeyBinding
8 from Tools.Log import Log
9 from six.moves import map
10
11 class KeymapError(Exception):
12         def __init__(self, message):
13                 self.msg = message
14
15         def __str__(self):
16                 return self.msg
17
18 def parseKeys(context, filename, actionmap, device, keys):
19         devices = [_f for _f in [x.strip() for x in device.split(";")] if _f]
20         for x in keys.findall("key"):
21                 get_attr = x.attrib.get
22                 mapto = get_attr("mapto")
23                 id = get_attr("id")
24                 flags = get_attr("flags")
25
26                 flag_ascii_to_id = lambda x: {'m':1,'b':2,'r':4,'l':8}[x]
27
28                 flags = sum(map(flag_ascii_to_id, flags))
29
30                 assert mapto, "%s: must specify mapto in context %s, id '%s'" % (filename, context, id)
31                 assert id, "%s: must specify id in context %s, mapto '%s'" % (filename, context, mapto)
32                 assert flags, "%s: must specify at least one flag in context %s, id '%s'" % (filename, context, id)
33
34                 if len(id) == 1:
35                         keyid = ord(id) | 0x8000
36                 elif id[0] == '\\':
37                         if id[1] == 'x':
38                                 keyid = int(id[2:], 0x10) | 0x8000
39                         elif id[1] == 'd':
40                                 keyid = int(id[2:]) | 0x8000
41                         else:
42                                 raise KeymapError("key id '" + str(id) + "' is neither hex nor dec")
43                 else:
44                         try:
45                                 keyid = KEYIDS[id]
46                         except:
47                                 raise KeymapError("key id '" + str(id) + "' is illegal")
48                 for device in devices:
49                         actionmap.bindKey(filename, device, keyid, flags, context, mapto)
50                 addKeyBinding(filename, keyid, context, mapto, flags)
51
52 def readKeymap(filename):
53         p = enigma.eActionMap.getInstance()
54         assert p
55
56         source = open(filename, 'r')
57
58         try:
59                 dom = xml.etree.cElementTree.parse(source)
60         except:
61                 raise KeymapError("keymap %s not well-formed." % filename)
62
63         keymap = dom.getroot()
64
65         for cmap in keymap.findall("map"):
66                 context = cmap.attrib.get("context")
67                 assert context, "map must have context"
68
69                 advanced = None
70                 hasBle = False
71                 for device in cmap.findall("device"):
72                         devices = device.attrib.get("name")
73                         parseKeys(context, filename, p, devices, device)
74                         if devices.find("dreambox advanced remote control (native)") >= 0:
75                                 advanced = device
76                         if devices.find("dreambox remote control (bluetooth le)") >= 0:
77                                 hasBle = True
78                 if not hasBle and advanced:
79                         Log.w("BLE Keymap fallback to advanced remote  for context %s" %(context,))
80                         parseKeys(context, filename, p, "dreambox remote control (bluetooth le)", advanced)
81
82                 parseKeys(context, filename, p, "generic", cmap)
83
84 def removeKeymap(filename):
85         p = enigma.eActionMap.getInstance()
86         p.unbindKeyDomain(filename)