cosmetic and comments for guacamole integration
[libguac-client-rdp.git] / tools / ini2dot_h_unimap.py
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2012 Ulteo SAS
4 # http://www.ulteo.com
5 # Author Jocelyn DELALANDE <j.delalande@ulteo.com> 2012
6 #
7 # Converts a .ini file defining unicode exceptions to a dot_h file
8 # The dot_h file defines an array of keysim->unicode
9 #
10 # Used to extract the keysym<->unicode mapping exceptions from 
11 # unicode_exception.ini (can be found in Ulteo patched version of xrdp)
12 #
13 # Such an ini file can be found at
14 # http://www.ulteo.com/home/en/download/sourcecode (xrdp folder)
15 #
16
17 import sys
18 import ConfigParser
19
20
21 class KeysymMaps:
22     def __init__(self):
23         # 4 digits keysyms
24         self.base = []
25         # Extented keysym starting with 0x1000
26         self.ext0 = []
27         # Extented keysym starting with 0x1001
28         self.ext1 = []
29         # Extented keysym starting with 0x1002
30         self.ext2 = []
31
32     def insert(self, keysym, uni):
33         # Main keysym table is 4-digit hexa keysyms
34         # (most of 'em are 3-digits but they lack the leading zero)
35         if len(keysym) <= 6:
36             self.base.append((keysym[2:], uni))
37
38         elif keysym.startswith('0x100'):
39             if keysym[:6] == '0x1000':
40                 self.ext0.append((keysym[6:], uni))
41             elif keysym[:6] == '0x1001':
42                 self.ext1.append((keysym[6:], uni))
43             elif keysym[:6] == '0x1002':
44                 self.ext2.append((keysym[6:], uni))
45
46             else:
47                 raise ValueError("Unexpected keysym : %s" % keysym)
48
49         else:
50             raise ValueError("Unexpected keysym : %s" % keysym)
51
52     def get_h_content(self, base_name, ext0_name, ext1_name, ext2_name):
53         out = ''
54         maps = ((base_name, self.base), 
55                 (ext0_name, self.ext0), 
56                 (ext1_name, self.ext1), 
57                 (ext2_name, self.ext2))
58
59         for var_name, kmap in maps:
60             for keysym, uni in kmap:
61                 out += '%s[0x%s] = %s;\n' %(var_name, keysym, uni)
62             out += '\n\n'
63         return out
64
65
66 if __name__ == '__main__':
67     if len(sys.argv) < 2:
68         print "ini2dot_h_unimap.py <ini_file>"
69         exit(2)
70
71     inifile = sys.argv[1]
72     
73     print "uni2keysym_map[]"
74
75     ini = ConfigParser.ConfigParser()
76     ini.read([inifile])
77     mapping = ini.items('unicode_exception')
78     
79     maps = KeysymMaps()
80
81
82     for uni, keysym in mapping:
83         maps.insert(keysym, uni)
84
85     dot_h_content = \
86 """
87 int keysm2uni_base[4096];
88 int keysm2uni_ext0[4096];
89 int keysm2uni_ext1[4096];
90 int keysm2uni_ext2[4096];
91
92
93 """
94     dot_h_content += maps.get_h_content('keysm2uni_base', 'keysm2uni_ext0', 
95                                         'keysm2uni_ext1', 'keysm2uni_ext2');
96
97     print dot_h_content