licensing fix
[libguac-client-rdp.git] / tools / ini2dot_h_unimap.py
1 #!/usr/bin/env python
2
3 # ***** BEGIN LICENSE BLOCK *****
4 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 #
6 # The contents of this file are subject to the Mozilla Public License Version
7 # 1.1 (the "License"); you may not use this file except in compliance with
8 # the License. You may obtain a copy of the License at
9 # http://www.mozilla.org/MPL/
10 #
11 # Software distributed under the License is distributed on an "AS IS" basis,
12 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 # for the specific language governing rights and limitations under the
14 # License.
15 #
16 # The Original Code is libguac-client-rdp.
17 #
18 # The Initial Developer of the Original Code is
19 # Jocelyn DELALANDE <j.delalande@ulteo.com> Ulteo SAS - http://www.ulteo.com
20 #
21 # Portions created by the Initial Developer are Copyright (C) 2012 Ulteo SAS.
22 #
23 # Contributor(s): 
24 #
25 # Alternatively, the contents of this file may be used under the terms of
26 # either the GNU General Public License Version 2 or later (the "GPL"), or
27 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 # in which case the provisions of the GPL or the LGPL are applicable instead
29 # of those above. If you wish to allow use of your version of this file only
30 # under the terms of either the GPL or the LGPL, and not to allow others to
31 # use your version of this file under the terms of the MPL, indicate your
32 # decision by deleting the provisions above and replace them with the notice
33 # and other provisions required by the GPL or the LGPL. If you do not delete
34 # the provisions above, a recipient may use your version of this file under
35 # the terms of any one of the MPL, the GPL or the LGPL.
36 #
37 # ***** END LICENSE BLOCK ***** 
38
39 # Converts a .ini file defining unicode exceptions to a dot_h file
40 # The dot_h file defines an array of keysim->unicode
41 #
42 # Used to extract the keysym<->unicode mapping exceptions from 
43 # unicode_exception.ini (can be found in Ulteo patched version of xrdp)
44 #
45 # Such an ini file can be found at
46 # http://www.ulteo.com/home/en/download/sourcecode (xrdp folder)
47 #
48
49 import sys
50 import ConfigParser
51
52
53 class KeysymMaps:
54     def __init__(self):
55         # 4 digits keysyms
56         self.base = []
57         # Extented keysym starting with 0x1000
58         self.ext0 = []
59         # Extented keysym starting with 0x1001
60         self.ext1 = []
61         # Extented keysym starting with 0x1002
62         self.ext2 = []
63
64     def insert(self, keysym, uni):
65         # Main keysym table is 4-digit hexa keysyms
66         # (most of 'em are 3-digits but they lack the leading zero)
67         if len(keysym) <= 6:
68             self.base.append((keysym[2:], uni))
69
70         elif keysym.startswith('0x100'):
71             if keysym[:6] == '0x1000':
72                 self.ext0.append((keysym[6:], uni))
73             elif keysym[:6] == '0x1001':
74                 self.ext1.append((keysym[6:], uni))
75             elif keysym[:6] == '0x1002':
76                 self.ext2.append((keysym[6:], uni))
77
78             else:
79                 raise ValueError("Unexpected keysym : %s" % keysym)
80
81         else:
82             raise ValueError("Unexpected keysym : %s" % keysym)
83
84     def get_h_content(self, base_name, ext0_name, ext1_name, ext2_name):
85         out = ''
86         maps = ((base_name, self.base), 
87                 (ext0_name, self.ext0), 
88                 (ext1_name, self.ext1), 
89                 (ext2_name, self.ext2))
90
91         for var_name, kmap in maps:
92             for keysym, uni in kmap:
93                 out += '%s[0x%s] = %s;\n' %(var_name, keysym, uni)
94             out += '\n\n'
95         return out
96
97
98 if __name__ == '__main__':
99     if len(sys.argv) < 2:
100         print "ini2dot_h_unimap.py <ini_file>"
101         exit(2)
102
103     inifile = sys.argv[1]
104     
105     print "uni2keysym_map[]"
106
107     ini = ConfigParser.ConfigParser()
108     ini.read([inifile])
109     mapping = ini.items('unicode_exception')
110     
111     maps = KeysymMaps()
112
113
114     for uni, keysym in mapping:
115         maps.insert(keysym, uni)
116
117     dot_h_content = \
118 """
119 int keysm2uni_base[4096];
120 int keysm2uni_ext0[4096];
121 int keysm2uni_ext1[4096];
122 int keysm2uni_ext2[4096];
123
124
125 """
126     dot_h_content += maps.get_h_content('keysm2uni_base', 'keysm2uni_ext0', 
127                                         'keysm2uni_ext1', 'keysm2uni_ext2');
128
129     print dot_h_content