34 extern char **environ;
38 extern int pedigree_load_keymap(
char *buffer,
size_t len);
41 cmd_t *cmds[MAX_CMDS];
45 int sparse_buffsz = 0;
46 char *sparse_buff = 0;
55 #define SPECIAL 0x80000000 64 #define TABLE_IDX(combinator, modifiers, scancode) \ 65 (((combinator & 0xFF) << 11) | ((modifiers & 0xF) << 7) | (scancode & 0x7F)) 66 #define TABLE_MAX TABLE_IDX(0xFF, 0xF, 0x7F) 71 #define SPARSE_DATA_FLAG 0x8000 83 if (sparse_pos + sz > sparse_buffsz)
85 if (sparse_pos + sz >= 32768)
87 fprintf(stderr,
"Too many sparse nodes.\n");
90 char *new_sparse_buff = realloc(sparse_buff, sparse_buffsz + sz + 32);
94 stderr,
"Couldn't allocate buffer for new sparse buffer: %s.\n",
98 sparse_buff = new_sparse_buff;
99 sparse_buffsz += sz + 32;
109 int sz = (
unsigned long) end - (
unsigned long) start;
110 if (data_pos + sz > data_buffsz)
112 if (data_pos + sz >= 32768)
114 fprintf(stderr,
"Too much data.\n");
117 char *new_data_buff = realloc(data_buff, data_buffsz + sz + 32);
121 stderr,
"Couldn't allocate buffer for new data buffer: %s.\n",
125 data_buff = new_data_buff;
126 data_buffsz += sz + 32;
129 memcpy(&data_buff[data_pos], (
char *) start, sz);
137 for (; start < end; start++)
139 if (start->flags == 0 && start->val == 0)
147 for (; start < end; start++)
149 if (start->flags != 0 || start->val != 0)
161 int yyerror(
const char *str)
163 printf(
"Syntax error: %s\n", str);
169 printf(
"keymap: Compile or install a keyboard mapping.\n\ 170 Usage: keymap [compile|install] file \n");
173 void parse(
char *filename)
175 FILE *stream = fopen(filename,
"r");
178 fprintf(stderr,
"Unable to open file `%s'\n", filename);
187 for (i = 0; i < n_cmds; i++)
189 int comb = cmds[i]->combinators & 0xFF;
190 int modifiers = cmds[i]->modifiers & 0xF;
192 unsigned int idx = TABLE_IDX(comb, modifiers, cmds[i]->scancode);
193 table[idx].flags = cmds[i]->set_modifiers;
194 if (cmds[i]->unicode_point != 0)
196 table[idx].val = cmds[i]->unicode_point;
198 else if (cmds[i]->val != 0)
200 table[idx].flags |= SPECIAL;
202 (
char *) &(table[idx].val), cmds[i]->val, strlen(cmds[i]->val));
207 int sparse(
int idx,
int bisect_size)
209 int ime = sparse_add();
213 if (all_clear(&table[idx - bisect_size], &table[idx]) > 0)
216 me->left = SPARSE_NULL;
219 (all_set(&table[idx - bisect_size], &table[idx]) > 0) ||
225 data_add(&table[idx - bisect_size], &table[idx]) |
230 uint16_t tmp = sparse(idx - bisect_size / 2, bisect_size / 2);
232 me = (
sparse_t *) (&sparse_buff[ime]);
237 if (all_clear(&table[idx], &table[idx + bisect_size]) > 0)
240 me->right = SPARSE_NULL;
243 (all_set(&table[idx], &table[idx + bisect_size]) > 0) ||
249 data_add(&table[idx], &table[idx + bisect_size]) |
254 uint16_t tmp = sparse(idx + bisect_size / 2, bisect_size / 2);
256 me = (
sparse_t *) (&sparse_buff[ime]);
263 void compile(
char *filename)
268 sparse((TABLE_MAX + 1) / 2, (TABLE_MAX + 1) / 2);
271 char *fname = (
char *) malloc(strlen(filename) + 5);
272 strcpy(fname, filename);
273 strcat(fname,
".kmc");
276 FILE *stream = fopen(fname,
"w");
279 fprintf(stderr,
"Unable to open output file `%s'\n", fname);
283 int idx = 2 *
sizeof(int);
284 fwrite(&idx,
sizeof(idx), 1, stream);
285 idx = sparse_buffsz + 2 *
sizeof(int);
287 fwrite(&idx,
sizeof(idx), 1, stream);
288 fwrite(sparse_buff, 1, sparse_buffsz, stream);
289 fwrite(data_buff, 1, data_buffsz, stream);
292 printf(
"Compiled keymap file written to `%s'.\n", fname);
295 memset(fname, 0, strlen(filename) + 5);
296 strcpy(fname, filename);
300 stream = fopen(fname,
"w");
303 fprintf(stderr,
"Unable to open output file `%s'\n", fname);
308 char *header_guard = strdup(fname);
310 for (i = 0; i < (int) strlen(fname); i++)
312 if (fname[i] ==
'.' || fname[i] ==
'/')
313 header_guard[i] =
'_';
315 header_guard[i] = toupper(fname[i]);
320 * Copyright (c) 2010 James Molloy, Burtescu Eduard\n\ 322 * Permission to use, copy, modify, and distribute this software for any\n\ 323 * purpose with or without fee is hereby granted, provided that the above\n\ 324 * copyright notice and this permission notice appear in all copies.\n\ 326 * THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES\n\ 327 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF\n\ 328 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR\n\ 329 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\n\ 330 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN\n\ 331 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF\n\ 332 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n\ 338 static char sparseBuff[%d] =\n\"",
339 header_guard, header_guard, sparse_buffsz + 1);
341 for (i = 0; i < sparse_buffsz; i++)
343 fprintf(stream,
"\\x%02x", (
unsigned char) sparse_buff[i]);
344 if ((i % 20) == 0 && i != 0)
345 fprintf(stream,
"\\\n");
347 fprintf(stream,
"\";\n\nstatic char dataBuff[%d] =\n\"", data_buffsz + 1);
348 for (i = 0; i < data_buffsz; i++)
350 fprintf(stream,
"\\x%02x", (
unsigned char) data_buff[i]);
351 if ((i % 20) == 0 && i != 0)
352 fprintf(stream,
"\\\n");
354 fprintf(stream,
"\";\n\n#endif\n");
360 printf(
"Compiled keymap header file written to `%s'.\n", fname);
363 void install(
char *filename)
366 fprintf(stderr,
"Unable to install a keymap on non-pedigree OS.\n");
371 int len = strlen(filename);
372 if (len < 4 || filename[len - 3] !=
'k' || filename[len - 2] !=
'm' ||
373 filename[len - 1] !=
'c')
375 printf(
"Warning: Filename does not end in '.kmc' - are you sure this " 376 "is a compiled keymap file?\n");
377 printf(
"*** If you load an invalid keymap your system will be rendered " 378 "unusable without a manual reboot. ***\n");
379 printf(
"\nContinue? [no] ");
383 if (strcmp(input,
"yes"))
385 printf(
"Aborted, no change took place.\n");
391 FILE *stream = fopen(filename,
"r");
394 fprintf(stderr,
"Error opening file `%s'.\n", filename);
397 fseek(stream, 0, SEEK_END);
399 fseek(stream, 0, SEEK_SET);
401 char *buffer = (
char *) malloc(len);
402 fread(buffer, 1, len, stream);
405 if (pedigree_load_keymap(buffer, len))
406 fprintf(stderr,
"Error loading keymap.\n");
408 printf(
"Keymap loaded.\n");
413 int main(
int argc,
char **argv)
421 if (!strcmp(argv[1],
"compile"))
423 else if (!strcmp(argv[1],
"install"))