This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 148
/
gen-charmap.py
190 lines (165 loc) · 4.59 KB
/
gen-charmap.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/python
#
# a python script used to generate some C constant tables from a key charmap file
#
# usage:
# progname file.kcm > charmap-tab.h
#
import sys, os, string, re
header = """\
#include "android_charmap.h"
/* the following is automatically generated by the 'gen-charmap.py' script
* do not touch. the generation command was:
* gen-charmap.py\
"""
header2 = """
*/
"""
kmap_header = """\
static const AKeyEntry _%(name)s_keys[] =
{
/* keycode base caps fn caps+fn number */
"""
kmap_footer = """\
};
static const AKeyCharmap _%(name)s_charmap =
{
_%(name)s_keys,
%(count)d,
"%(name)s"
};
"""
re_mapname = re.compile( r".*/(\w+).kcm" )
re_start = re.compile( r"(\w+)\s*(.*)" )
re_char = re.compile( r"('.')\s*(.*)" )
re_hex = re.compile( r"(0x\w+)\s*(.*)" )
specials = { 'COMMA': 'Comma',
'PERIOD': 'Period',
'AT': 'At',
'LEFT_BRACKET': 'LeftBracket',
'RIGHT_BRACKET': 'RightBracket',
'SLASH': 'Slash',
'BACKSLASH': 'Backslash',
'GRAVE': 'Grave',
'MINUS': 'Minus',
'EQUALS': 'Equals',
'SEMICOLON': 'Semicolon',
'APOSTROPHE': 'Apostrophe',
'SPACE': 'Space',
'ENTER': 'Newline',
'TAB': 'Tab',
'STAR': 'Star',
'POUND': 'Pound',
'PLUS': 'Plus',
'DEL': 'Del'
}
entries = []
def match_char_or_hex(line):
m = re_char.match(line)
if not m:
m = re_hex.match(line)
return m
def quote(s):
if s == "'''":
s = "'\\''"
elif s == "'\\'":
s = "'\\\\'"
return s
def process_line(line,result):
m = re_start.match(line)
if not m:
print "bad bad line: " + line
return -1
keycode = m.group(1)
line = m.group(2)
m = match_char_or_hex(line)
if not m:
print "character expected in: " + line
return -1
disp = quote(m.group(1))
line = m.group(2)
m = match_char_or_hex(line)
if not m:
print "character expected in: " + line
return -1
number = quote(m.group(1))
line = m.group(2)
m = match_char_or_hex(line)
if not m:
print "character expected in: " + line
return -1
base = quote(m.group(1))
line = m.group(2)
m = match_char_or_hex(line)
if not m:
print "character expected in: " + line
return -1
caps = quote(m.group(1))
line = m.group(2)
m = match_char_or_hex(line)
if not m:
print "character expected in: " + line
return -1
fn = quote(m.group(1))
line = m.group(2)
m = match_char_or_hex(line)
if not m:
print "character expected in: " + line
return -1
caps_fn = quote(m.group(1))
if specials.has_key(keycode):
keycode = specials[keycode]
keycode = "kKeyCode" + keycode
result.append( (keycode,base,caps,fn,caps_fn,number) )
return 0
def process_file( file ):
result = []
fp = open(file,"rb")
for line in fp.xreadlines():
line = line.strip()
if not line: # skip empty lines
continue
if line[0] == '#' or line[0] == '[': # skip
continue
if process_line(line,result) < 0:
break
fp.close()
return result
class KMap:
def __init__(self,name,results):
self.name = name
self.results = results
def dump(self):
t = { 'name': self.name, 'count':len(self.results) }
print kmap_header % t
for item in self.results:
print " { %-22s, %5s, %5s, %5s, %6s, %5s }," % item
print kmap_footer % t
kmaps = []
if len(sys.argv) < 2:
print "usage: progname charmap.kcm [charmap2.kcm ...] > charmap-tab.h"
else:
genline = ""
for filepath in sys.argv[1:]:
m = re_mapname.match(filepath)
if not m:
print "%s is not a keyboard charmap name" % filepath
os.exit(1)
mapname = m.group(1)
genline = genline + " " + mapname + ".kcm"
for filepath in sys.argv[1:]:
m = re_mapname.match(filepath)
mapname = m.group(1)
result = process_file( filepath )
kmap = KMap(mapname,result)
kmaps.append(kmap)
print header + genline + header2
for kmap in kmaps:
kmap.dump()
print "const AKeyCharmap* android_charmaps[%d] = {" % len(kmaps),
comma = ""
for kmap in kmaps:
print "%s&_%s_charmap" % (comma, kmap.name),
comma = ", "
print "};"
print "const int android_charmap_count = %d;" % len(kmaps)