1 #include "keybindings.h"
9 void init_keybindings(struct World * world) {
10 // Initialize keybindings from file "keybindings".
11 FILE * file = fopen("keybindings", "r");
20 if (c_count > linemax)
21 linemax = c_count + 1;
24 struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
25 fseek(file, 0, SEEK_SET);
26 char * command = malloc(linemax);
27 uint16_t commcount = 0;
29 while (fgets(command, linemax, file)) {
30 keybindings[commcount].key = atoi(command);
31 cmdptr = strchr(command, ' ') + 1;
32 keybindings[commcount].name = malloc(strlen(cmdptr));
33 memcpy(keybindings[commcount].name, cmdptr, strlen(cmdptr) - 1);
34 keybindings[commcount].name[strlen(cmdptr) - 1] = '\0';
38 struct KeysWinData * keyswindata = malloc(sizeof(struct KeysWinData));
39 keyswindata->max = lines - 1;
40 keyswindata->select = 0;
41 keyswindata->edit = 0;
42 world->keybindings = keybindings;
43 world->keyswindata = keyswindata; }
45 void save_keybindings(struct World * world) {
46 // Write keybindings to keybindings file.
47 struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
48 struct KeyBinding * keybindings = world->keybindings;
49 FILE * file = fopen("keybindings", "w");
52 for (i = 0; i <= keyswindata->max; i++)
53 if (strlen(keybindings[i].name) > linemax)
54 linemax = strlen(keybindings[i].name);
55 linemax = linemax + 6; // + 6 = + 3 digits + whitespace + newline + null byte
56 char * line = malloc(linemax);
57 for (i = 0; i <= keyswindata->max; i++) {
58 snprintf(line, linemax, "%d %s\n", keybindings[i].key, keybindings[i].name);
59 fwrite(line, sizeof(char), strlen(line), file); }
63 uint16_t get_action_key (struct KeyBinding * keybindings, char * name) {
64 // Return key matching name in keybindings.
66 while (strcmp(keybindings[i].name, name) )
68 return keybindings[i].key; }
70 char * get_keyname(uint16_t keycode) {
71 // Translate some keycodes to readable names of max 9 chars.
74 if (32 < keycode && keycode < 127)
75 sprintf(keyname, "%c", keycode);
76 else if (keycode == 9)
77 sprintf(keyname, "TAB");
78 else if (keycode == 10)
79 sprintf(keyname, "RETURN");
80 else if (keycode == 27)
81 sprintf(keyname, "ESCAPE");
82 else if (keycode == 32)
83 sprintf(keyname, "SPACE");
84 else if (keycode == KEY_UP)
85 sprintf(keyname, "UP");
86 else if (keycode == KEY_DOWN)
87 sprintf(keyname, "DOWN");
88 else if (keycode == KEY_LEFT)
89 sprintf(keyname, "LEFT");
90 else if (keycode == KEY_RIGHT)
91 sprintf(keyname, "RIGHT");
92 else if (keycode == KEY_HOME)
93 sprintf(keyname, "HOME");
94 else if (keycode == KEY_BACKSPACE)
95 sprintf(keyname, "BACKSPACE");
96 else if (keycode >= KEY_F0 && keycode <= KEY_F(63)) {
97 uint16_t f = keycode - KEY_F0;
98 sprintf(keyname, "F%d", f); }
99 else if (keycode == KEY_DC)
100 sprintf(keyname, "DELETE");
101 else if (keycode == KEY_IC)
102 sprintf(keyname, "INSERT");
103 else if (keycode == KEY_NPAGE)
104 sprintf(keyname, "NEXT PAGE");
105 else if (keycode == KEY_PPAGE)
106 sprintf(keyname, "PREV PAGE");
107 else if (keycode == KEY_END)
108 sprintf(keyname, "END");
110 sprintf(keyname, "(unknown)");
113 void keyswin_mod_key (struct World * world, struct WinMeta * win_meta) {
114 // In keybindings window, mark selection modifiable, modify key. Ensure max of three digits in key code field.
115 world->keyswindata->edit = 1;
116 draw_all_wins (win_meta);
119 world->keybindings[world->keyswindata->select].key = key;
120 world->keyswindata->edit = 0; }
122 void keyswin_move_selection (struct World * world, char dir) {
123 // In keybindings window, move selection upwards or downwards (if within limits of list length).
124 if ('u' == dir && world->keyswindata->select > 0)
125 world->keyswindata->select--;
126 else if ('d' == dir && world->keyswindata->select < world->keyswindata->max)
127 world->keyswindata->select++; }