3 #include "keybindings.h"
4 #include <stdio.h> /* for FILE typedef*/
5 #include <stdlib.h> /* for free(), atoi() */
6 #include <stdint.h> /* for uint16_t */
7 #include <ncurses.h> /* for keycode defines in get_name_to_keycode() */
8 #include <string.h> /* for strchr(), strlen(), strcmp(), memcpy()*/
9 #include "windows.h" /* for draw_all_wins() and WinMeta struct */
10 #include "readwrite.h" /* for texfile_sizes(), try_fopen(), try_fclose()
11 * try_fclose_unlink_rename(), try_fwrite()
13 #include "main.h" /* for World struct */
14 #include "rexit.h" /* for err_exit() */
15 #include "misc.h" /* for try_malloc() */
19 extern uint16_t get_keycode_to_action(struct KeyBinding * kb_p, char * name)
23 if (0 == strcmp(kb_p->name, name))
34 extern char * get_name_to_keycode(struct World * world, uint16_t keycode)
36 char * f_name = "get_name_to_keycode()";
37 char * keyname = try_malloc(15, world, f_name);
38 if (32 < keycode && keycode < 127)
40 sprintf(keyname, "%c", keycode);
42 else if (keycode == 9) /* TODO: Find a way more elegant than */
43 { /* hardcoding all of this? */
44 sprintf(keyname, "TAB");
46 else if (keycode == 10)
48 sprintf(keyname, "RETURN");
50 else if (keycode == 27)
52 sprintf(keyname, "ESCAPE");
54 else if (keycode == 32)
56 sprintf(keyname, "SPACE");
58 else if (keycode == KEY_UP)
60 sprintf(keyname, "UP");
62 else if (keycode == KEY_DOWN)
64 sprintf(keyname, "DOWN");
66 else if (keycode == KEY_LEFT)
68 sprintf(keyname, "LEFT");
70 else if (keycode == KEY_RIGHT)
72 sprintf(keyname, "RIGHT");
74 else if (keycode == KEY_HOME)
76 sprintf(keyname, "HOME");
78 else if (keycode == KEY_BACKSPACE)
80 sprintf(keyname, "BACKSPACE");
82 else if (keycode >= KEY_F0 && keycode <= KEY_F(63))
84 uint16_t f = keycode - KEY_F0;
85 sprintf(keyname, "F%d", f);
87 else if (keycode == KEY_DC)
89 sprintf(keyname, "DELETE");
91 else if (keycode == KEY_IC)
93 sprintf(keyname, "INSERT");
95 else if (keycode == KEY_NPAGE)
97 sprintf(keyname, "NEXT PAGE");
99 else if (keycode == KEY_PPAGE)
101 sprintf(keyname, "PREV PAGE");
103 else if (keycode == KEY_END)
105 sprintf(keyname, "END");
109 sprintf(keyname, "(unknown)");
116 extern uint16_t get_n_of_keybs(struct World * world)
119 struct KeyBinding * kb_p = world->keybindings;
134 extern struct KeyBinding * get_keyb_of_n(struct World * world, uint16_t n)
137 struct KeyBinding * kb_p = world->keybindings;
152 extern void init_keybindings(struct World * world)
154 char * f_name = "init_keybindings()";
156 char * path = "config/keybindings";
157 FILE * file = try_fopen(path, "r", world, f_name);
158 uint16_t lines, linemax;
159 char * err = "textfile_sizes() in init_keybindings() returns error.";
160 exit_err(textfile_sizes(file, &linemax, &lines), world, err);
162 char command[linemax + 1];
164 struct KeyBinding ** loc_last_ptr = &world->keybindings;
165 while (fgets(command, linemax + 1, file))
167 * loc_last_ptr = try_malloc(sizeof(struct KeyBinding), world, f_name);
168 struct KeyBinding * kb_p = * loc_last_ptr;
170 kb_p->key = atoi(command);
171 cmdptr = strchr(command, ' ') + 1;
172 kb_p->name = try_malloc(strlen(cmdptr), world, f_name);
173 memcpy(kb_p->name, cmdptr, strlen(cmdptr) - 1);
174 kb_p->name[strlen(cmdptr) - 1] = '\0';
175 loc_last_ptr = & kb_p->next;
178 try_fclose(file, world, f_name);
180 struct KeysWinData * keyswindata;
181 keyswindata = try_malloc(sizeof(struct KeysWinData), world, f_name);
182 keyswindata->select = 0;
183 keyswindata->edit = 0;
184 world->keyswindata = keyswindata;
189 extern void save_keybindings(struct World * world)
191 char * f_name = "save_keybindings()";
193 char * path = "config/keybindings";
194 char * path_tmp = "config/keybindings_tmp";
195 FILE * file = try_fopen(path_tmp, "w", world, f_name);
197 uint16_t linemax = 0;
198 struct KeyBinding * kb_p = world->keybindings;
201 if (strlen(kb_p->name) > linemax)
203 linemax = strlen(kb_p->name);
207 linemax = linemax + 6; /* + 6 = + 3 digits + whitespace + \n + \0 */
210 kb_p = world->keybindings;
213 snprintf(line, linemax, "%d %s\n", kb_p->key, kb_p->name);
214 try_fwrite(line, sizeof(char), strlen(line), file, world, f_name);
218 try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
223 extern void free_keybindings(struct KeyBinding * kb_start)
225 struct KeyBinding * kb_p = kb_start->next;
228 free_keybindings(kb_p);
235 extern void keyswin_mod_key(struct World * world, struct WinMeta * win_meta)
237 world->keyswindata->edit = 1;
238 exit_err(draw_all_wins(win_meta), world, "Trouble with draw_all_wins() in "
239 "keyswin_mod_key().");
243 struct KeyBinding * kb_p = get_keyb_of_n(world,
244 world->keyswindata->select);
247 world->keyswindata->edit = 0;
252 extern void move_keys_mod_selection(struct World * world, char dir)
255 && world->keyswindata->select > 0)
257 world->keyswindata->select--;
260 && world->keyswindata->select < get_n_of_keybs(world) - 1)
262 world->keyswindata->select++;