Initialize Tizen 2.3
[framework/uifw/ecore.git] / mobile / src / util / makekeys.c
1 /* Portions of this code are Copyright 1990, 1998 The Open Group
2
3 Permission to use, copy, modify, distribute, and sell this software and its
4 documentation for any purpose is hereby granted without fee, provided that
5 the above copyright notice appear in all copies and that both that
6 copyright notice and this permission notice appear in supporting
7 documentation.
8
9 The above copyright notice and this permission notice shall be included
10 in all copies or substantial portions of the Software.
11
12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
13 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
15 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
16 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
17 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
18 OTHER DEALINGS IN THE SOFTWARE.
19
20 Except as contained in this notice, the name of The Open Group shall
21 not be used in advertising or otherwise to promote the sale, use or
22 other dealings in this Software without prior written authorization
23 from The Open Group.
24
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <X11/keysymdef.h>
31
32 #define TBLNUM 4000
33 #define MIN_REHASH 15
34 #define MATCHES 10
35
36 typedef struct _Info Info;
37 static struct _Info 
38 {
39    char *name;
40    long unsigned int val;
41 } info[TBLNUM];
42
43 /* local function prototypes */
44 static int _parseline(const char *buf, char *key, long unsigned int *val, char *prefix);
45
46 /* local variables */
47 static char tab[TBLNUM];
48 static unsigned short offsets[TBLNUM];
49 static unsigned short indexes[TBLNUM];
50 static long unsigned int values[TBLNUM];
51 static char buf[1024];
52 static int ksnum = 0;
53
54 int 
55 main(int argc, char **argv) 
56 {
57    int max_rehash = 0;
58    unsigned long sig;
59    int i = 0, j = 0, k = 0, l = 0, z = 0;
60    FILE *fptr;
61    char *name = NULL, c;
62    int first = 0, num_found = 0;
63    int best_max_rehash = 0, best_z = 0;
64    long unsigned int val;
65    char key[128], prefix[128];
66
67    for (l = 1; l < argc; l++) 
68      {
69         if (!(fptr = fopen(argv[l], "r"))) 
70           {
71              fprintf(stderr, "Could not open %s\n", argv[l]);
72              continue;
73           }
74
75         while (fgets(buf, sizeof(buf), fptr)) 
76           {
77              if (!_parseline(buf, key, &val, prefix))
78                continue;
79
80              if (val == XK_VoidSymbol) val = 0;
81              if (val > 0x1fffffff) 
82                {
83                   fprintf(stderr, "Ignoring illegal keysym (%s %lx)\n", 
84                           key, val);
85                   continue;
86                }
87
88              if (!(name = malloc(strlen(prefix) + strlen(key) + 1))) 
89                {
90                   fprintf(stderr, "Makekeys: Out Of Memory !!\n");
91                   exit(EXIT_FAILURE);
92                }
93
94              sprintf(name, "%s%s", prefix, key);
95              info[ksnum].name = name;
96              info[ksnum].val = val;
97              ksnum++;
98              if (ksnum == TBLNUM) 
99                {
100                   fprintf(stderr, "Makekeys: Too Many Keysyms!!\n");
101                   exit(EXIT_FAILURE);
102                }
103           }
104
105         fclose(fptr);
106      }
107
108    printf("/* This file is generated from keysymdef.h. */\n");
109    printf("/* Do Not Edit !! */\n\n");
110
111    best_max_rehash = ksnum;
112    num_found = 0;
113    for (z = ksnum; z < TBLNUM; z++) 
114      {
115         max_rehash = 0;
116         for (name = tab, i = z; --i >= 0;) 
117           *name++ = 0;
118         for (i = 0; i < ksnum; i++) 
119           {
120              name = info[i].name;
121              sig = 0;
122              while ((c = *name++))
123                sig = (sig << 1) + c;
124              first = j = sig % z;
125              for (k = 0; tab[j]; k++) 
126                {
127                   j += (first + 1);
128                   if (j >= z) j -= z;
129                   if (j == first) goto next1;
130                }
131              tab[j] = 1;
132              if (k > max_rehash) max_rehash = k;
133           }
134         if (max_rehash < MIN_REHASH) 
135           {
136              if (max_rehash < best_max_rehash) 
137                {
138                   best_max_rehash = max_rehash;
139                   best_z = z;
140                }
141              num_found++;
142              if (num_found >= MATCHES)
143                break;
144           }
145 next1: ;
146      }
147
148    z = best_z;
149    if (z == 0) 
150      {
151         fprintf(stderr, "Makekeys: Failed to find small enough hash !!\n"
152                 "Try increasing TBLNUM in makekeys.c\n");
153         exit(EXIT_FAILURE);
154      }
155
156    printf("#ifdef NEED_KEYSYM_TABLE\n");
157    printf("const unsigned char _ecore_xcb_keytable[] = {\n");
158    printf("0,\n");
159    k = 1;
160    for (i = 0; i < ksnum; i++) 
161      {
162         name = info[i].name;
163         sig = 0;
164         while ((c = *name++))
165           sig = (sig << 1) + c;
166         first = j = sig % z;
167         while (offsets[j]) 
168           {
169              j += (first + 1);
170              if (j >= z) j -= z;
171           }
172         offsets[j] = k;
173         indexes[i] = k;
174         val = info[i].val;
175         printf("0x%.2lx, 0x%.2lx, 0x%.2lx, 0x%.2lx, 0x%.2lx, 0x%.2lx, ",
176                (sig >> 8) & 0xff, sig & 0xff,
177                (val >> 24) & 0xff, (val >> 16) & 0xff,
178                (val >> 8) & 0xff, val & 0xff);
179         for (name = info[i].name, k += 7; (c = *name++); k++)
180           printf("'%c',", c);
181         printf((i == (ksnum - 1)) ? "0\n" : "0,\n");
182      }
183
184    printf("};\n\n");
185    printf("#define KTABLESIZE %d\n", z);
186    printf("#define KMAXHASH %d\n", (best_max_rehash + 1));
187    printf("\n");
188    printf("static const unsigned short hashString[KTABLESIZE] = {\n");
189
190    for (i = 0; i < z;) 
191      {
192         printf("0x%.4x", offsets[i]);
193         i++;
194         if (i == z) break;
195         printf((i & 7) ? ", " : ",\n");
196      }
197
198    printf("\n");
199    printf("};\n");
200    printf("#endif\n");
201
202    best_max_rehash = ksnum;
203    num_found = 0;
204    for (z = ksnum; z < TBLNUM; z++) 
205      {
206         max_rehash = 0;
207         for (name = tab, i = z; --i >= 0;) 
208           *name++ = 0;
209         for (i = 0; i < ksnum; i++) 
210           {
211              val = info[i].val;
212              first = j = val % z;
213              for (k = 0; tab[j]; k++) 
214                {
215                   if (values[j] == val) goto skip1;
216                   j += (first + 1);
217                   if (j >= z) j -= z;
218                   if (j == first) goto next2;
219                }
220              tab[j] = 1;
221              values[j] = val;
222              if (k > max_rehash) max_rehash = k;
223 skip1: ;
224           }
225         if (max_rehash < MIN_REHASH) 
226           {
227              if (max_rehash < best_max_rehash) 
228                {
229                   best_max_rehash = max_rehash;
230                   best_z = z;
231                }
232              num_found++;
233              if (num_found >= MATCHES) break;
234           }
235 next2: ;
236      }
237
238    z = best_z;
239    if (z == 0) 
240      {
241         fprintf(stderr, "Makekeys: Failed to find small enough hash !!\n"
242                 "Try increasing TBLNUM in makekeys.c\n");
243         exit(EXIT_FAILURE);
244      }
245    for (i = z; --i >= 0;)
246      offsets[i] = 0;
247
248    for (i = 0; i < ksnum; i++) 
249      {
250         val = info[i].val;
251         first = j = val % z;
252         while (offsets[j]) 
253           {
254              if (values[j] == val) goto skip2;
255              j += (first + 1);
256              if (j >= z) j -= z;
257           }
258         offsets[j] = indexes[i] + 2;
259         values[j] = val;
260 skip2: ;
261      }
262
263    printf("\n");
264    printf("#ifdef NEED_VTABLE\n");
265    printf("#define VTABLESIZE %d\n", z);
266    printf("#define VMAXHASH %d\n", best_max_rehash + 1);
267    printf("\n");
268    printf("static const unsigned short hashKeysym[VTABLESIZE] = {\n");
269    for (i = 0; i < z;) 
270      {
271         printf("0x%.4x", offsets[i]);
272         i++;
273         if (i == z) break;
274         printf((i & 7) ? ", " : ",\n");
275      }
276    printf("\n");
277    printf("};\n");
278    printf("#endif\n");
279
280    return 0;
281 }
282
283 /* local functions */
284 static int 
285 _parseline(const char *buf, char *key, long unsigned int *val, char *prefix) 
286 {
287    int i = 0;
288    char alias[128];
289    char *tmp = NULL, *tmpa = NULL;
290
291    /* try to match XK_foo first */
292    i = sscanf(buf, "#define %127s 0x%lx", key, val);
293    if ((i == 2) && (tmp = strstr(key, "XK_")))
294      {
295         memcpy(prefix, key, (tmp - key));
296         prefix[tmp - key] = '\0';
297         tmp += 3;
298         memmove(key, tmp, strlen(tmp) + 1);
299         return 1;
300      }
301
302    /* try to match an alias */
303    i = sscanf(buf, "#define %127s %127s", key, alias);
304    if (((i == 2) && (tmp = strstr(key, "XK_"))) && 
305        (tmpa = strstr(alias, "XK_"))) 
306      {
307         memcpy(prefix, key, (tmp - key));
308         prefix[tmp - key] = '\0';
309         tmp += 3;
310         memmove(key, tmp, strlen(tmp) + 1);
311         memmove(tmpa, tmpa + 3, strlen(tmpa + 3) + 1);
312
313         for (i = ksnum - 1; i >= 0; i--) 
314           {
315              if (!strcmp(info[i].name, alias)) 
316                {
317                   *val = info[i].val;
318                   return 1;
319                }
320           }
321         fprintf(stderr, "Cannot find matching definition %s for keysym %s%s\n", 
322                 alias, prefix, key);
323      }
324
325    return 0;
326 }