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 */
16 extern void init_keybindings(struct World * world)
18 FILE * file = fopen("keybindings", "r");
19 uint16_t lines, linemax;
20 textfile_sizes(file, &linemax, &lines);
21 struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
22 char * command = malloc(linemax);
23 uint16_t commcount = 0;
25 while (fgets(command, linemax, file))
27 keybindings[commcount].key = atoi(command);
28 cmdptr = strchr(command, ' ') + 1;
29 keybindings[commcount].name = malloc(strlen(cmdptr));
30 memcpy(keybindings[commcount].name, cmdptr, strlen(cmdptr) - 1);
31 keybindings[commcount].name[strlen(cmdptr) - 1] = '\0';
36 struct KeysWinData * keyswindata = malloc(sizeof(struct KeysWinData));
37 keyswindata->max = lines - 1;
38 keyswindata->select = 0;
39 keyswindata->edit = 0;
40 world->keybindings = keybindings;
41 world->keyswindata = keyswindata;
46 extern void save_keybindings(struct World * world)
48 struct KeysWinData * keyswindata = (struct KeysWinData *)
50 struct KeyBinding * keybindings = world->keybindings;
51 FILE * file = fopen("keybindings", "w");
54 for (i = 0; i <= keyswindata->max; i++)
56 if (strlen(keybindings[i].name) > linemax)
58 linemax = strlen(keybindings[i].name);
61 linemax = linemax + 6; /* + 6 = + 3 digits + whitespace + \n + \0 */
62 char * line = malloc(linemax);
63 for (i = 0; i <= keyswindata->max; i++)
65 snprintf(line, linemax,
66 "%d %s\n", keybindings[i].key, keybindings[i].name);
67 fwrite(line, sizeof(char), strlen(line), file);
75 extern uint16_t get_action_key(struct KeyBinding * keybindings, char * name)
78 while (strcmp(keybindings[i].name, name) )
82 return keybindings[i].key;
87 extern char * get_keyname(uint16_t keycode)
91 if (32 < keycode && keycode < 127)
93 sprintf(keyname, "%c", keycode);
95 else if (keycode == 9) /* TODO: Find a way more elegant than */
96 { /* hardcoding all of this? */
97 sprintf(keyname, "TAB");
99 else if (keycode == 10)
101 sprintf(keyname, "RETURN");
103 else if (keycode == 27)
105 sprintf(keyname, "ESCAPE");
107 else if (keycode == 32)
109 sprintf(keyname, "SPACE");
111 else if (keycode == KEY_UP)
113 sprintf(keyname, "UP");
115 else if (keycode == KEY_DOWN)
117 sprintf(keyname, "DOWN");
119 else if (keycode == KEY_LEFT)
121 sprintf(keyname, "LEFT");
123 else if (keycode == KEY_RIGHT)
125 sprintf(keyname, "RIGHT");
127 else if (keycode == KEY_HOME)
129 sprintf(keyname, "HOME");
131 else if (keycode == KEY_BACKSPACE)
133 sprintf(keyname, "BACKSPACE");
135 else if (keycode >= KEY_F0 && keycode <= KEY_F(63))
137 uint16_t f = keycode - KEY_F0;
138 sprintf(keyname, "F%d", f);
140 else if (keycode == KEY_DC)
142 sprintf(keyname, "DELETE");
144 else if (keycode == KEY_IC)
146 sprintf(keyname, "INSERT");
148 else if (keycode == KEY_NPAGE)
150 sprintf(keyname, "NEXT PAGE");
152 else if (keycode == KEY_PPAGE)
154 sprintf(keyname, "PREV PAGE");
156 else if (keycode == KEY_END)
158 sprintf(keyname, "END");
162 sprintf(keyname, "(unknown)");
169 extern void keyswin_mod_key(struct World * world, struct WinMeta * win_meta)
171 world->keyswindata->edit = 1;
172 draw_all_wins(win_meta);
176 world->keybindings[world->keyswindata->select].key = key;
178 world->keyswindata->edit = 0;
183 extern void keyswin_move_selection(struct World * world, char dir)
185 if ('u' == dir && world->keyswindata->select > 0)
187 world->keyswindata->select--;
189 else if ('d' == dir && world->keyswindata->select < world->keyswindata->max)
191 world->keyswindata->select++;