39extern int pedigree_load_keymap(
char* buffer,
size_t len);
58#define SPECIAL 0x80000000
65#define TABLE_IDX(combinator, modifiers, scancode) \
66 (((combinator & 0xFF) << 11) | ((modifiers & 0xF) << 7) | (scancode & 0x7F))
67#define TABLE_MAX TABLE_IDX(0xFF, 0xF, 0x7F)
72#define SPARSE_DATA_FLAG 0x8000
82 if (sparse_pos + sz > sparse_buffsz) {
83 if (sparse_pos + sz >= 32768) {
84 fprintf(stderr,
"Too many sparse nodes.\n");
87 sparse_t* new_sparse_buff = realloc(sparse_buff, sparse_buffsz + sz +
sizeof(
sparse_t));
88 if (!new_sparse_buff) {
89 fprintf(stderr,
"Couldn't allocate buffer for new sparse buffer: %s.\n", strerror(errno));
92 sparse_buff = new_sparse_buff;
93 sparse_buffsz += sz +
sizeof(
sparse_t);
102 int sz = (
unsigned long)end - (
unsigned long)start;
103 if (data_pos + sz > data_buffsz) {
104 if (data_pos + sz >= 32768) {
105 fprintf(stderr,
"Too much data.\n");
108 char* new_data_buff = realloc(data_buff, data_buffsz + sz +
sizeof(
sparse_t));
109 if (!new_data_buff) {
110 fprintf(stderr,
"Couldn't allocate buffer for new data buffer: %s.\n", strerror(errno));
113 data_buff = new_data_buff;
114 data_buffsz += sz +
sizeof(
sparse_t);
117 memcpy(&data_buff[data_pos], (
char*)start, sz);
124 for (; start < end; start++) {
125 if (start->flags == 0 && start->val == 0)
132 for (; start < end; start++) {
133 if (start->flags != 0 || start->val != 0)
144int yyerror(
const char* str) {
145 printf(
"Syntax error: %s\n", str);
151 "keymap: Compile or install a keyboard mapping.\n\
152Usage: keymap [compile|install] file \n");
155void parse(
char* filename) {
156 FILE* stream = fopen(filename,
"r");
158 fprintf(stderr,
"Unable to open file `%s'\n", filename);
167 for (i = 0; i < n_cmds; i++) {
168 int comb = cmds[i]->combinators & 0xFF;
169 int modifiers = cmds[i]->modifiers & 0xF;
171 unsigned int idx = TABLE_IDX(comb, modifiers, cmds[i]->scancode);
172 table[idx].flags = cmds[i]->set_modifiers;
173 if (cmds[i]->unicode_point != 0) {
174 table[idx].val = cmds[i]->unicode_point;
175 }
else if (cmds[i]->val != 0) {
176 table[idx].flags |= SPECIAL;
177 memcpy((
char*)&(table[idx].val), cmds[i]->val, strlen(cmds[i]->val));
182int sparse(
int idx,
int bisect_size) {
183 int byte_offset = sparse_add();
184 sparse_t* me = &sparse_buff[byte_offset /
sizeof(*sparse_buff)];
187 if (all_clear(&table[idx - bisect_size], &table[idx]) > 0) {
189 me->left = SPARSE_NULL;
190 }
else if ((all_set(&table[idx - bisect_size], &table[idx]) > 0) || (bisect_size % 2)) {
193 me->left = data_add(&table[idx - bisect_size], &table[idx]) |
196 uint16_t tmp = sparse(idx - bisect_size / 2, bisect_size / 2);
198 me = &sparse_buff[byte_offset /
sizeof(*sparse_buff)];
203 if (all_clear(&table[idx], &table[idx + bisect_size]) > 0) {
205 me->right = SPARSE_NULL;
206 }
else if ((all_set(&table[idx], &table[idx + bisect_size]) > 0) || (bisect_size % 2)) {
209 me->right = data_add(&table[idx], &table[idx + bisect_size]) |
212 uint16_t tmp = sparse(idx + bisect_size / 2, bisect_size / 2);
214 me = &sparse_buff[byte_offset /
sizeof(*sparse_buff)];
221void compile(
char* filename) {
225 sparse((TABLE_MAX + 1) / 2, (TABLE_MAX + 1) / 2);
228 char* fname = (
char*)malloc(strlen(filename) + 5);
229 strcpy(fname, filename);
230 strcat(fname,
".kmc");
233 FILE* stream = fopen(fname,
"w");
235 fprintf(stderr,
"Unable to open output file `%s'\n", fname);
239 int idx = 2 *
sizeof(int);
240 fwrite(&idx,
sizeof(idx), 1, stream);
241 idx = sparse_buffsz + 2 *
sizeof(int);
243 fwrite(&idx,
sizeof(idx), 1, stream);
244 fwrite(sparse_buff, 1, sparse_buffsz, stream);
245 fwrite(data_buff, 1, data_buffsz, stream);
248 printf(
"Compiled keymap file written to `%s'.\n", fname);
251 memset(fname, 0, strlen(filename) + 5);
252 strcpy(fname, filename);
256 stream = fopen(fname,
"w");
258 fprintf(stderr,
"Unable to open output file `%s'\n", fname);
263 char* header_guard = strdup(fname);
265 for (i = 0; i < (int)strlen(fname); i++) {
266 if (fname[i] ==
'.' || fname[i] ==
'/')
267 header_guard[i] =
'_';
269 header_guard[i] = toupper(fname[i]);
274 * Copyright (c) 2010 James Molloy, Burtescu Eduard\n\
276 * Permission to use, copy, modify, and distribute this software for any\n\
277 * purpose with or without fee is hereby granted, provided that the above\n\
278 * copyright notice and this permission notice appear in all copies.\n\
280 * THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES\n\
281 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF\n\
282 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR\n\
283 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\n\
284 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN\n\
285 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF\n\
286 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n\
292static char sparseBuff[%d] =\n\"",
293 header_guard, header_guard, sparse_buffsz + 1);
295 for (i = 0; i < sparse_buffsz; i++) {
296 fprintf(stream,
"\\x%02x", (
unsigned char)((
char*)sparse_buff)[i]);
297 if ((i % 20) == 0 && i != 0)
298 fprintf(stream,
"\\\n");
300 fprintf(stream,
"\";\n\nstatic char dataBuff[%d] =\n\"", data_buffsz + 1);
301 for (i = 0; i < data_buffsz; i++) {
302 fprintf(stream,
"\\x%02x", (
unsigned char)data_buff[i]);
303 if ((i % 20) == 0 && i != 0)
304 fprintf(stream,
"\\\n");
306 fprintf(stream,
"\";\n\n#endif\n");
312 printf(
"Compiled keymap header file written to `%s'.\n", fname);
315void install(
char* filename) {
317 fprintf(stderr,
"Unable to install a keymap on non-pedigree OS.\n");
322 int len = strlen(filename);
323 if (len < 4 || filename[len - 3] !=
'k' || filename[len - 2] !=
'm' || filename[len - 1] !=
'c') {
325 "Warning: Filename does not end in '.kmc' - are you sure this "
326 "is a compiled keymap file?\n");
328 "*** If you load an invalid keymap your system will be rendered "
329 "unusable without a manual reboot. ***\n");
330 printf(
"\nContinue? [no] ");
333 if ((scanf(
"%63s", input) != 1) || (strcmp(input,
"yes") != 0)) {
334 printf(
"Aborted, no change took place.\n");
340 FILE* stream = fopen(filename,
"r");
342 fprintf(stderr,
"Error opening file `%s'.\n", filename);
345 fseek(stream, 0, SEEK_END);
347 fseek(stream, 0, SEEK_SET);
349 char* buffer = (
char*)malloc(len);
350 fread(buffer, 1, len, stream);
353 if (pedigree_load_keymap(buffer, len))
354 fprintf(stderr,
"Error loading keymap.\n");
356 printf(
"Keymap loaded.\n");
361int main(
int argc,
char** argv) {
367 if (!strcmp(argv[1],
"compile"))
369 else if (!strcmp(argv[1],
"install"))