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() */
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 struct Command * get_command_to_keycode(struct KeyBinding * kb_p,
96 extern char * get_keyname_to_keycode(uint16_t keycode)
98 char * f_name = "get_name_to_keycode()";
99 char * keyname = try_malloc(15, f_name); /* FIXME: Why 15? */
100 if (32 < keycode && keycode < 127)
102 sprintf(keyname, "%c", keycode);
104 else if (keycode >= KEY_F0 && keycode <= KEY_F(63))
106 uint16_t f = keycode - KEY_F0;
107 sprintf(keyname, "F%d", f);
109 else if ( try_keycode(keycode, keyname, 9, "TAB")
110 || try_keycode(keycode, keyname, 10, "RETURN")
111 || try_keycode(keycode, keyname, 27, "ESCAPE")
112 || try_keycode(keycode, keyname, 32, "SPACE")
113 || try_keycode(keycode, keyname, KEY_UP, "UP")
114 || try_keycode(keycode, keyname, KEY_DOWN, "DOWN")
115 || try_keycode(keycode, keyname, KEY_LEFT, "LEFT")
116 || try_keycode(keycode, keyname, KEY_RIGHT, "RIGHT")
117 || try_keycode(keycode, keyname, KEY_HOME, "HOME")
118 || try_keycode(keycode, keyname, KEY_BACKSPACE, "BACKSPACE")
119 || try_keycode(keycode, keyname, KEY_DC, "DELETE")
120 || try_keycode(keycode, keyname, KEY_IC, "INSERT")
121 || try_keycode(keycode, keyname, KEY_NPAGE, "NEXT PAGE")
122 || try_keycode(keycode, keyname, KEY_PPAGE, "PREV PAGE")
123 || try_keycode(keycode, keyname, KEY_END, "END"))
129 sprintf(keyname, "(unknown)");
136 extern void init_keybindings(char * path, struct KeyBindingDB * kbd)
138 char * f_name = "init_keybindings()";
139 FILE * file = try_fopen(path, "r", f_name);
141 uint32_t linemax = textfile_sizes(file, &lines);
142 char command[linemax + 1];
144 struct KeyBinding ** loc_last_ptr = &kbd->kbs;
146 while (try_fgets(command, linemax + 1, file, f_name))
148 if ('\n' == command[0] || 0 == command[0])
152 * loc_last_ptr = try_malloc(sizeof(struct KeyBinding), f_name);
153 struct KeyBinding * kb_p = * loc_last_ptr;
155 kb_p->key = atoi(command);
156 cmdptr = strchr(command, ' ') + 1;
157 cmdptr[strlen(cmdptr) - 1] = '\0';
158 kb_p->command = get_command(cmdptr);
159 loc_last_ptr = & kb_p->next;
161 try_fclose(file, f_name);
168 extern void save_keybindings(char * path, struct KeyBindingDB * kbd)
170 char * f_name = "save_keybindings()";
171 char path_tmp[strlen(path) + 4 + 1];
172 sprintf(path_tmp, "%s_tmp", path);
173 FILE * file = try_fopen(path_tmp, "w", f_name);
174 uint16_t linemax = 0;
175 struct KeyBinding * kb_p = kbd->kbs;
178 if (strlen(kb_p->command->dsc_short) > linemax)
180 linemax = strlen(kb_p->command->dsc_short);
184 linemax = linemax + 6; /* + 6 = + 3 digits + whitespace + \n + \0 */
189 snprintf(line, linemax, "%d %s\n", kb_p->key, kb_p->command->dsc_short);
190 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
193 try_fclose_unlink_rename(file, path_tmp, path, f_name);
198 extern void free_keybindings(struct KeyBinding * kb_start)
204 struct KeyBinding * kb_p = kb_start->next;
207 free_keybindings(kb_p);
214 extern void mod_selected_keyb(struct KeyBindingDB * kbd)
220 halfdelay(world.halfdelay);
223 struct KeyBinding * kb_p = get_keyb_of_n(kbd->kbs, kbd->select);
231 extern void move_keyb_mod_selection(struct KeyBindingDB * kbd, char dir)
233 if ('u' == dir && kbd->select > 0)
237 else if ('d' == dir && kbd->select < get_n_of_keybs(kbd->kbs) - 1)