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("keybindings", "r");
20 uint16_t lines, linemax;
21 textfile_sizes(file, &linemax, &lines);
22 struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
23 char * command = malloc(linemax);
24 uint16_t commcount = 0;
26 while (fgets(command, linemax, file))
28 keybindings[commcount].key = atoi(command);
29 cmdptr = strchr(command, ' ') + 1;
30 keybindings[commcount].name = malloc(strlen(cmdptr));
31 memcpy(keybindings[commcount].name, cmdptr, strlen(cmdptr) - 1);
32 keybindings[commcount].name[strlen(cmdptr) - 1] = '\0';
37 struct KeysWinData * keyswindata = malloc(sizeof(struct KeysWinData));
38 keyswindata->max = lines - 1;
39 keyswindata->select = 0;
40 keyswindata->edit = 0;
41 world->keybindings = keybindings;
42 world->keyswindata = keyswindata;
47 extern void save_keybindings(struct World * world)
49 struct KeysWinData * keyswindata = (struct KeysWinData *)
51 struct KeyBinding * keybindings = world->keybindings;
52 FILE * file = fopen("keybindings", "w");
55 for (i = 0; i <= keyswindata->max; i++)
57 if (strlen(keybindings[i].name) > linemax)
59 linemax = strlen(keybindings[i].name);
62 linemax = linemax + 6; /* + 6 = + 3 digits + whitespace + \n + \0 */
63 char * line = malloc(linemax);
64 for (i = 0; i <= keyswindata->max; i++)
66 snprintf(line, linemax,
67 "%d %s\n", keybindings[i].key, keybindings[i].name);
68 fwrite(line, sizeof(char), strlen(line), file);
76 extern uint16_t get_action_key(struct KeyBinding * keybindings, char * name)
79 while (strcmp(keybindings[i].name, name) )
83 return keybindings[i].key;
88 extern char * get_keyname(uint16_t keycode)
92 if (32 < keycode && keycode < 127)
94 sprintf(keyname, "%c", keycode);
96 else if (keycode == 9) /* TODO: Find a way more elegant than */
97 { /* hardcoding all of this? */
98 sprintf(keyname, "TAB");
100 else if (keycode == 10)
102 sprintf(keyname, "RETURN");
104 else if (keycode == 27)
106 sprintf(keyname, "ESCAPE");
108 else if (keycode == 32)
110 sprintf(keyname, "SPACE");
112 else if (keycode == KEY_UP)
114 sprintf(keyname, "UP");
116 else if (keycode == KEY_DOWN)
118 sprintf(keyname, "DOWN");
120 else if (keycode == KEY_LEFT)
122 sprintf(keyname, "LEFT");
124 else if (keycode == KEY_RIGHT)
126 sprintf(keyname, "RIGHT");
128 else if (keycode == KEY_HOME)
130 sprintf(keyname, "HOME");
132 else if (keycode == KEY_BACKSPACE)
134 sprintf(keyname, "BACKSPACE");
136 else if (keycode >= KEY_F0 && keycode <= KEY_F(63))
138 uint16_t f = keycode - KEY_F0;
139 sprintf(keyname, "F%d", f);
141 else if (keycode == KEY_DC)
143 sprintf(keyname, "DELETE");
145 else if (keycode == KEY_IC)
147 sprintf(keyname, "INSERT");
149 else if (keycode == KEY_NPAGE)
151 sprintf(keyname, "NEXT PAGE");
153 else if (keycode == KEY_PPAGE)
155 sprintf(keyname, "PREV PAGE");
157 else if (keycode == KEY_END)
159 sprintf(keyname, "END");
163 sprintf(keyname, "(unknown)");
170 extern void keyswin_mod_key(struct World * world, struct WinMeta * win_meta)
172 world->keyswindata->edit = 1;
173 exit_err(draw_all_wins(win_meta), world, "Trouble with draw_all_wins() in "
174 "keyswin_mod_key().");
178 world->keybindings[world->keyswindata->select].key = key;
180 world->keyswindata->edit = 0;
185 extern void keyswin_move_selection(struct World * world, char dir)
187 if ('u' == dir && world->keyswindata->select > 0)
189 world->keyswindata->select--;
191 else if ('d' == dir && world->keyswindata->select < world->keyswindata->max)
193 world->keyswindata->select++;