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 KeyBinding * kb_p)
133 extern struct KeyBinding * get_keyb_of_n(struct KeyBinding * kb_p, uint16_t n)
150 extern void init_keybindings(struct World * world, char * path,
151 struct KeyBiData * kbd)
153 char * f_name = "init_keybindings()";
155 FILE * file = try_fopen(path, "r", world, f_name);
156 uint16_t lines, linemax;
157 char * err = "textfile_sizes() in init_keybindings() returns error.";
158 exit_err(textfile_sizes(file, &linemax, &lines), world, err);
160 char command[linemax + 1];
162 struct KeyBinding ** loc_last_ptr = &kbd->kbs;
164 while (fgets(command, linemax + 1, file))
166 if ('\n' == command[0] || 0 == command[0])
170 * loc_last_ptr = try_malloc(sizeof(struct KeyBinding), world, f_name);
171 struct KeyBinding * kb_p = * loc_last_ptr;
173 kb_p->key = atoi(command);
174 cmdptr = strchr(command, ' ') + 1;
175 kb_p->name = try_malloc(strlen(cmdptr), world, f_name);
176 memcpy(kb_p->name, cmdptr, strlen(cmdptr) - 1);
177 kb_p->name[strlen(cmdptr) - 1] = '\0';
178 loc_last_ptr = & kb_p->next;
181 try_fclose(file, world, f_name);
189 extern void save_keybindings(struct World * world, char * path,
190 struct KeyBiData * kbd)
192 char * f_name = "save_keybindings()";
194 char path_tmp[strlen(path) + 4 + 1];
195 sprintf(path_tmp, "%s_tmp", path);
196 FILE * file = try_fopen(path_tmp, "w", world, f_name);
198 uint16_t linemax = 0;
199 struct KeyBinding * kb_p = kbd->kbs;
202 if (strlen(kb_p->name) > linemax)
204 linemax = strlen(kb_p->name);
208 linemax = linemax + 6; /* + 6 = + 3 digits + whitespace + \n + \0 */
214 snprintf(line, linemax, "%d %s\n", kb_p->key, kb_p->name);
215 try_fwrite(line, sizeof(char), strlen(line), file, world, f_name);
219 try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
224 extern void free_keybindings(struct KeyBinding * kb_start)
230 struct KeyBinding * kb_p = kb_start->next;
233 free_keybindings(kb_p);
235 free(kb_start->name);
241 extern void mod_selected_keyb(struct World * world, struct KeyBiData * kbd)
244 exit_err(draw_all_wins(world->wmeta), world,
245 "Trouble with draw_all_wins() in mod_selected_keyb().");
249 struct KeyBinding * kb_p = get_keyb_of_n(kbd->kbs, kbd->select);
257 extern void move_keyb_mod_selection(struct KeyBiData * kbd, char dir)
259 if ('u' == dir && kbd->select > 0)
263 else if ('d' == dir && kbd->select < get_n_of_keybs(kbd->kbs) - 1)