1 /* src/client/keybindings.c */
3 #include "keybindings.h"
4 #include <ncurses.h> /* keycode defines, cbreak(), halfdelay(), getch() */
5 #include <stdio.h> /* FILE, sprintf(), snprintf() */
6 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
7 #include <stdlib.h> /* free(), atoi() */
8 #include <string.h> /* strlen(), strchr(), strcmp(), memcpy() */
9 #include "../common/readwrite.h" /* try_fopen(), textfile_sizes(), try_fgets(),
10 * try_fclose(), try_fclose_unlink_rename(),
13 #include "../common/try_malloc.h" /* for try_malloc() */
14 #include "windows.h" /* draw_all_wins() */
15 #include "world.h" /* global world */
19 /* Return "n"-th keybinding in keybindings chain from "kb_p" on. */
20 static struct KeyBinding * get_keyb_of_n(struct KeyBinding * kb_p, uint16_t n);
22 /* Return number of keybindings in keybindings chain from "kb_p" on. */
23 static uint16_t get_n_of_keybs(struct KeyBinding * kb_p);
25 /* If "keycode_given" equals "keycode_match", copy "keyname_match" to "keyname"
26 * and return 1; otherwise return 0.
28 static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
29 uint16_t keycode_match, char * keyname_match);
33 static struct KeyBinding * get_keyb_of_n(struct KeyBinding * kb_p, uint16_t n)
50 static uint16_t get_n_of_keybs(struct KeyBinding * kb_p)
67 static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
68 uint16_t keycode_match, char * keyname_match)
70 if (keycode_given == keycode_match)
72 sprintf(keyname, keyname_match);
80 extern char * get_command_to_keycode(struct KeyBinding * kb_p, uint16_t key)
95 extern uint16_t get_keycode_to_command(struct KeyBinding * kb_p, char * command)
99 if (0 == strcmp(kb_p->command, command))
110 extern char * get_keyname_to_keycode(uint16_t keycode)
112 char * f_name = "get_name_to_keycode()";
113 char * keyname = try_malloc(15, f_name); /* FIXME: Why 15? */
114 if (32 < keycode && keycode < 127)
116 sprintf(keyname, "%c", keycode);
118 else if (keycode >= KEY_F0 && keycode <= KEY_F(63))
120 uint16_t f = keycode - KEY_F0;
121 sprintf(keyname, "F%d", f);
123 else if ( try_keycode(keycode, keyname, 9, "TAB")
124 || try_keycode(keycode, keyname, 10, "RETURN")
125 || try_keycode(keycode, keyname, 27, "ESCAPE")
126 || try_keycode(keycode, keyname, 32, "SPACE")
127 || try_keycode(keycode, keyname, KEY_UP, "UP")
128 || try_keycode(keycode, keyname, KEY_DOWN, "DOWN")
129 || try_keycode(keycode, keyname, KEY_LEFT, "LEFT")
130 || try_keycode(keycode, keyname, KEY_RIGHT, "RIGHT")
131 || try_keycode(keycode, keyname, KEY_HOME, "HOME")
132 || try_keycode(keycode, keyname, KEY_BACKSPACE, "BACKSPACE")
133 || try_keycode(keycode, keyname, KEY_DC, "DELETE")
134 || try_keycode(keycode, keyname, KEY_IC, "INSERT")
135 || try_keycode(keycode, keyname, KEY_NPAGE, "NEXT PAGE")
136 || try_keycode(keycode, keyname, KEY_PPAGE, "PREV PAGE")
137 || try_keycode(keycode, keyname, KEY_END, "END"))
143 sprintf(keyname, "(unknown)");
150 extern void init_keybindings(char * path, struct KeyBindingDB * kbd)
152 char * f_name = "init_keybindings()";
153 FILE * file = try_fopen(path, "r", f_name);
155 uint32_t linemax = textfile_sizes(file, &lines);
156 char command[linemax + 1];
158 struct KeyBinding ** loc_last_ptr = &kbd->kbs;
160 while (try_fgets(command, linemax + 1, file, f_name))
162 if ('\n' == command[0] || 0 == command[0])
166 * loc_last_ptr = try_malloc(sizeof(struct KeyBinding), f_name);
167 struct KeyBinding * kb_p = * loc_last_ptr;
169 kb_p->key = atoi(command);
170 cmdptr = strchr(command, ' ') + 1;
171 kb_p->command = try_malloc(strlen(cmdptr), f_name);
172 memcpy(kb_p->command, cmdptr, strlen(cmdptr) - 1);
173 kb_p->command[strlen(cmdptr) - 1] = '\0';
174 loc_last_ptr = & kb_p->next;
176 try_fclose(file, f_name);
183 extern void save_keybindings(char * path, struct KeyBindingDB * kbd)
185 char * f_name = "save_keybindings()";
186 char path_tmp[strlen(path) + 4 + 1];
187 sprintf(path_tmp, "%s_tmp", path);
188 FILE * file = try_fopen(path_tmp, "w", f_name);
189 uint16_t linemax = 0;
190 struct KeyBinding * kb_p = kbd->kbs;
193 if (strlen(kb_p->command) > linemax)
195 linemax = strlen(kb_p->command);
199 linemax = linemax + 6; /* + 6 = + 3 digits + whitespace + \n + \0 */
204 snprintf(line, linemax, "%d %s\n", kb_p->key, kb_p->command);
205 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
208 try_fclose_unlink_rename(file, path_tmp, path, f_name);
213 extern void free_keybindings(struct KeyBinding * kb_start)
219 struct KeyBinding * kb_p = kb_start->next;
222 free_keybindings(kb_p);
224 free(kb_start->command);
230 extern void mod_selected_keyb(struct KeyBindingDB * kbd)
236 halfdelay(world.halfdelay);
239 struct KeyBinding * kb_p = get_keyb_of_n(kbd->kbs, kbd->select);
247 extern void move_keyb_mod_selection(struct KeyBindingDB * kbd, char dir)
249 if ('u' == dir && kbd->select > 0)
253 else if ('d' == dir && kbd->select < get_n_of_keybs(kbd->kbs) - 1)