X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fclient%2Fkeybindings.c;h=b0b7c7a06fc496ee5cb9baa5cd5af8c70632d14a;hb=891ba8fbca53d920f6b3704827fa6b8aee737de4;hp=f148afa3371744a0c804ce84266bfb1858589f7d;hpb=0907037fc188c28471805286a67b786264ba3e2f;p=plomrogue diff --git a/src/client/keybindings.c b/src/client/keybindings.c index f148afa..b0b7c7a 100644 --- a/src/client/keybindings.c +++ b/src/client/keybindings.c @@ -5,16 +5,11 @@ #include /* NULL */ #include /* uint8_t, uint16_t, uint32_t */ #include /* FILE, sprintf() */ -#include /* atoi() */ -#include /* strlen(), strchr(), strcmp() */ -#include "../common/err_try_fgets.h" /* err_try_fgets(), err_line() */ -#include "../common/readwrite.h" /* try_fwrite()*/ +#include "../common/rexit.h" /* exit_trouble() */ #include "../common/try_malloc.h" /* try_malloc() */ -#include "command_db.h" /* get_command() */ -#include "misc.h" /* array_append() */ #include "windows.h" /* draw_all_wins() */ #include "world.h" /* global world */ - +struct Command; /* Return pointer to global keybindings or to keybindings for wingeometry config @@ -55,9 +50,11 @@ static struct KeyBindingDB * char_selected_kb_db(char c) static uint8_t try_keycode(uint16_t keycode_given, char * keyname, uint16_t keycode_match, char * keyname_match) { + char * f_name = "try_keycode()"; if (keycode_given == keycode_match) { - sprintf(keyname, keyname_match); + int test = sprintf(keyname, "%s", keyname_match); + exit_trouble(test < 0, f_name, "sprintf()"); return 1; } return 0; @@ -68,7 +65,7 @@ static uint8_t try_keycode(uint16_t keycode_given, char * keyname, extern struct Command * get_command_to_keycode(struct KeyBindingDB * kbdb, uint16_t keycode) { - uint16_t n_kb; + uint8_t n_kb; for (n_kb = 0; n_kb < kbdb->n_of_kbs; n_kb++) { if (keycode == kbdb->kbs[n_kb].keycode) @@ -84,15 +81,15 @@ extern struct Command * get_command_to_keycode(struct KeyBindingDB * kbdb, extern char * get_keyname_to_keycode(uint16_t keycode) { char * f_name = "get_name_to_keycode()"; - char * keyname = try_malloc(15, f_name); /* FIXME: Why 15? */ + char * keyname = try_malloc(10, f_name); /* max keyname length + 1 */ if (32 < keycode && keycode < 127) { - sprintf(keyname, "%c", keycode); + exit_trouble(sprintf(keyname, "%c", keycode) < 0, f_name, "sprintf()"); } else if (keycode >= KEY_F0 && keycode <= KEY_F(63)) { uint16_t f = keycode - KEY_F0; - sprintf(keyname, "F%d", f); + exit_trouble(sprintf(keyname, "F%d", f) < 0, f_name, "sprintf()");; } else if ( try_keycode(keycode, keyname, 9, "TAB") || try_keycode(keycode, keyname, 10, "RETURN") @@ -114,79 +111,13 @@ extern char * get_keyname_to_keycode(uint16_t keycode) } else { - sprintf(keyname, "(unknown)"); + exit_trouble(sprintf(keyname, "(unknown)") < 0, f_name, "sprintf()"); } return keyname; } -extern void write_keybindings_to_file(FILE * file, struct KeyBindingDB * kbd) -{ - char * f_name = "write_keybindings_to_file()"; - uint16_t linemax = 0; - uint16_t n_kb; - for (n_kb = 0; n_kb < kbd->n_of_kbs; n_kb++) - { - if (strlen(kbd->kbs[n_kb].command->dsc_short) > linemax) - { - linemax = strlen(kbd->kbs[n_kb].command->dsc_short); - } - } - linemax = linemax + 6; /* + 6 = + 3 digits + ' ' + '\n' + '\0' */ - char line[linemax]; - for (n_kb = 0; n_kb < kbd->n_of_kbs; n_kb++) - { - sprintf(line, "%d %s\n", - kbd->kbs[n_kb].keycode, kbd->kbs[n_kb].command->dsc_short); - try_fwrite(line, sizeof(char), strlen(line), file, f_name); - } - try_fwrite(world.delim, strlen(world.delim), 1, file, f_name); -} - - - -extern void read_keybindings_from_file(char * line, uint32_t linemax, - FILE * file, struct KeyBindingDB * kbdb) -{ - char * context = "Failed reading keybindings from interface config file. "; - char * err_space = "Line illegally ends in whitespace."; - char * err_nospace = "No whitespace found in line."; - char * err_int = "Line starts not with a decimal number in digits."; - char * err_toolarge = "Keycode number too large, must be below 1000."; - char * err_cmd = "No such command in command DB."; - kbdb->n_of_kbs = 0; - while (1) - { - err_try_fgets(line, linemax, file, context, "0nf"); - if (!strcmp(world.delim, line)) - { - break; - } - err_line(' ' == line[strlen(line) - 2], line, context, err_space); - char * ptr_space; - err_line(!(ptr_space = strchr(line, ' ')), line, context, err_nospace); - uint8_t i = 0; - err_line(0 == (ptr_space - line), line, context, err_int); - for (; i < (ptr_space - line); i++) - { - err_line(line[i] < '0' || '9' < line[i], line, context, err_int); - } - err_line(i > 3, line, context, err_toolarge); - - struct KeyBinding kb; - line[strlen(line) - 1] = '\0'; - kb.command = get_command(ptr_space + 1); - err_line(!(kb.command), line, context, err_cmd); - kb.keycode = atoi(line); - array_append(kbdb->n_of_kbs, sizeof(struct KeyBinding), (void *) &kb, - (void **) kbdb); - kbdb->n_of_kbs++; - } -} - - - extern void mod_selected_keyb(char kb_c) { struct KeyBindingDB * kbdb = char_selected_kb_db(kb_c);