aboutsummaryrefslogtreecommitdiff
path: root/keymapparser.py
blob: f8aa07f49f4892f352eb7eb6f5c7f363e0ff242a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import xml.dom.minidom
import enigma
from Tools.XMLTools import elementsWithTag

from keyids import KEYIDS;

# these are only informational (for help)...
from Tools.KeyBindings import addKeyBinding

def readKeymap():

	p = enigma.eActionMap.getInstance()
	assert p
	
	filename1 = "data/keymap.xml"
	filename2 = "/usr/share/enigma2/keymap.xml"
		
	try:
		source = open(filename1)
		filename = filename1
	except:
		source = open(filename2)
		filename = filename2
#		raise "couldn't open keymap.xml!"
	
	try:
		dom = xml.dom.minidom.parse(source)
	except:
		raise "keymap not well-formed."
	
	keymap = dom.childNodes[0]
	
	maps = elementsWithTag(keymap.childNodes, "map")
	
	for cmap in maps:
		context = str(cmap.getAttribute("context"))
		assert context != "", "map must have context"
	
		def parseKeys(device, keys):
			for x in elementsWithTag(keys.childNodes, "key"):
				mapto = str(x.getAttribute("mapto"))
				id = x.getAttribute("id")
				flags = x.getAttribute("flags")
				
				flag_ascii_to_id = lambda x: {'m':1,'b':2,'r':4}[x]
				
#				try:
				flags = sum(map(flag_ascii_to_id, flags))
#				print "-> " + str(flags)
#				except:
#					raise str("%s: illegal flags '%s' specificed in context %s, id '%s'" % (filename, flags, context, id))
				
				assert mapto != "", "%s: must specify mapto in context %s, id '%s'" % (filename, context, id)
				assert id != "", "%s: must specify id in context %s, mapto '%s'" % (filename, context, mapto)
				assert flags != 0, "%s: must specify at least one flag in context %s, id '%s'" % (filename, context, id)
				
				if len(id) == 1:
					keyid = ord(id) | 0x8000
				elif id[0] == '\\':
					if id[1] == 'x':
						keyid = int(id[2:], 0x10) | 0x8000
					elif id[1] == 'd':
						keyid = int(id[2:]) | 0x8000
					else:
						raise "key id '" + str(id) + "' is neither hex nor dec"
				else:
					try:
						keyid = KEYIDS[id]
					except:
						raise "key id '" + str(id) + "' is illegal"

#				print context + "::" + mapto + " -> " + device + "." + hex(keyid)
				p.bindKey(device, keyid, flags, context, mapto)
				addKeyBinding(keyid, context, mapto)
		
		parseKeys("generic", cmap)
		
		for device in elementsWithTag(cmap.childNodes, "device"):
			parseKeys(str(device.getAttribute("name")), device)