5 #include "keybindings.h"
6 #include <stdlib.h> /* for malloc(), free(), atoi() */
7 #include <stdint.h> /* for uint16_t */
8 #include <ncurses.h> /* for keycode defines in get_keyname() */
9 #include <string.h> /* for strchr(), strlen(), strcmp(), memcpy()*/
10 #include "windows.h" /* for draw_all_wins() and WinMeta struct */
11 #include "misc.h" /* for texfile_sizes() */
12 #include "main.h" /* for World struct */
13 #include "rexit.h" /* for err_exit() */
17 extern void init_keybindings(struct World * world)
19 FILE * file = fopen("config/keybindings", "r");
20 uint16_t lines, linemax;
21 char * err = "textfile_sizes() in init_keybindings() returns error.";
22 exit_err(textfile_sizes(file, &linemax, &lines), world, err);
23 struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
24 char * command = malloc(linemax);
25 uint16_t commcount = 0;
27 while (fgets(command, linemax, file))
29 keybindings[commcount].key = atoi(command);
30 cmdptr = strchr(command, ' ') + 1;
31 keybindings[commcount].name = malloc(strlen(cmdptr));
32 memcpy(keybindings[commcount].name, cmdptr, strlen(cmdptr) - 1);
33 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;
48 extern void save_keybindings(struct World * world)
50 struct KeysWinData * keyswindata = (struct KeysWinData *)
52 struct KeyBinding * keybindings = world->keybindings;
53 FILE * file = fopen("config/keybindings", "w");
56 for (i = 0; i <= keyswindata->max; i++)
58 if (strlen(keybindings[i].name) > linemax)
60 linemax = strlen(keybindings[i].name);
63 linemax = linemax + 6; /* + 6 = + 3 digits + whitespace + \n + \0 */
64 char * line = malloc(linemax);
65 for (i = 0; i <= keyswindata->max; i++)
67 snprintf(line, linemax,
68 "%d %s\n", keybindings[i].key, keybindings[i].name);
69 fwrite(line, sizeof(char), strlen(line), file);
77 extern uint16_t get_action_key(struct KeyBinding * keybindings, char * name)
80 while (strcmp(keybindings[i].name, name) )
84 return keybindings[i].key;
89 extern char * get_keyname(uint16_t keycode)
93 if (32 < keycode && keycode < 127)
95 sprintf(keyname, "%c", keycode);
97 else if (keycode == 9) /* TODO: Find a way more elegant than */
98 { /* hardcoding all of this? */
99 sprintf(keyname, "TAB");
101 else if (keycode == 10)
103 sprintf(keyname, "RETURN");
105 else if (keycode == 27)
107 sprintf(keyname, "ESCAPE");
109 else if (keycode == 32)
111 sprintf(keyname, "SPACE");
113 else if (keycode == KEY_UP)
115 sprintf(keyname, "UP");
117 else if (keycode == KEY_DOWN)
119 sprintf(keyname, "DOWN");
121 else if (keycode == KEY_LEFT)
123 sprintf(keyname, "LEFT");
125 else if (keycode == KEY_RIGHT)
127 sprintf(keyname, "RIGHT");
129 else if (keycode == KEY_HOME)
131 sprintf(keyname, "HOME");
133 else if (keycode == KEY_BACKSPACE)
135 sprintf(keyname, "BACKSPACE");
137 else if (keycode >= KEY_F0 && keycode <= KEY_F(63))
139 uint16_t f = keycode - KEY_F0;
140 sprintf(keyname, "F%d", f);
142 else if (keycode == KEY_DC)
144 sprintf(keyname, "DELETE");
146 else if (keycode == KEY_IC)
148 sprintf(keyname, "INSERT");
150 else if (keycode == KEY_NPAGE)
152 sprintf(keyname, "NEXT PAGE");
154 else if (keycode == KEY_PPAGE)
156 sprintf(keyname, "PREV PAGE");
158 else if (keycode == KEY_END)
160 sprintf(keyname, "END");
164 sprintf(keyname, "(unknown)");
171 extern void keyswin_mod_key(struct World * world, struct WinMeta * win_meta)
173 world->keyswindata->edit = 1;
174 exit_err(draw_all_wins(win_meta), world, "Trouble with draw_all_wins() in "
175 "keyswin_mod_key().");
179 world->keybindings[world->keyswindata->select].key = key;
181 world->keyswindata->edit = 0;
186 extern void keyswin_move_selection(struct World * world, char dir)
188 if ('u' == dir && world->keyswindata->select > 0)
190 world->keyswindata->select--;
192 else if ('d' == dir && world->keyswindata->select < world->keyswindata->max)
194 world->keyswindata->select++;