1 /* src/client/keybindings.c */
3 #include "keybindings.h"
4 #include <ncurses.h> /* keycode defines, cbreak(), halfdelay(), getch() */
5 #include <stddef.h> /* NULL */
6 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
7 #include <stdio.h> /* FILE, sprintf() */
8 #include <stdlib.h> /* atoi() */
9 #include <string.h> /* strlen(), strchr(), strcmp() */
10 #include "../common/err_try_fgets.h" /* err_try_fgets(), err_line() */
11 #include "../common/readwrite.h" /* try_fwrite()*/
12 #include "../common/try_malloc.h" /* try_malloc() */
13 #include "command_db.h" /* get_command() */
14 #include "array_append.h" /* array_append() */
15 #include "windows.h" /* draw_all_wins() */
16 #include "world.h" /* global world */
20 /* Return pointer to global keybindings or to keybindings for wingeometry config
21 * (c = "g") or winkeys config (c = "k") or active window's keybindings ("w").
23 static struct KeyBindingDB * char_selected_kb_db(char c);
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 KeyBindingDB * char_selected_kb_db(char c)
35 struct KeyBindingDB * kbdb;
36 kbdb = &world.kb_global;
39 kbdb = &world.kb_wingeom;
43 kbdb = &world.kb_winkeys;
47 struct Win * w = get_win_by_id(world.winDB.active);
55 static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
56 uint16_t keycode_match, char * keyname_match)
58 if (keycode_given == keycode_match)
60 sprintf(keyname, "%s", keyname_match);
68 extern struct Command * get_command_to_keycode(struct KeyBindingDB * kbdb,
72 for (n_kb = 0; n_kb < kbdb->n_of_kbs; n_kb++)
74 if (keycode == kbdb->kbs[n_kb].keycode)
76 return kbdb->kbs[n_kb].command;
84 extern char * get_keyname_to_keycode(uint16_t keycode)
86 char * f_name = "get_name_to_keycode()";
87 char * keyname = try_malloc(10, f_name); /* max keyname length + 1 */
88 if (32 < keycode && keycode < 127)
90 sprintf(keyname, "%c", keycode);
92 else if (keycode >= KEY_F0 && keycode <= KEY_F(63))
94 uint16_t f = keycode - KEY_F0;
95 sprintf(keyname, "F%d", f);
97 else if ( try_keycode(keycode, keyname, 9, "TAB")
98 || try_keycode(keycode, keyname, 10, "RETURN")
99 || try_keycode(keycode, keyname, 27, "ESCAPE")
100 || try_keycode(keycode, keyname, 32, "SPACE")
101 || try_keycode(keycode, keyname, KEY_UP, "UP")
102 || try_keycode(keycode, keyname, KEY_DOWN, "DOWN")
103 || try_keycode(keycode, keyname, KEY_LEFT, "LEFT")
104 || try_keycode(keycode, keyname, KEY_RIGHT, "RIGHT")
105 || try_keycode(keycode, keyname, KEY_HOME, "HOME")
106 || try_keycode(keycode, keyname, KEY_BACKSPACE, "BACKSPACE")
107 || try_keycode(keycode, keyname, KEY_DC, "DELETE")
108 || try_keycode(keycode, keyname, KEY_IC, "INSERT")
109 || try_keycode(keycode, keyname, KEY_NPAGE, "NEXT PAGE")
110 || try_keycode(keycode, keyname, KEY_PPAGE, "PREV PAGE")
111 || try_keycode(keycode, keyname, KEY_END, "END"))
117 sprintf(keyname, "(unknown)");
124 extern void write_keybindings_to_file(FILE * file, struct KeyBindingDB * kbd)
126 char * f_name = "write_keybindings_to_file()";
127 uint16_t linemax = 0;
129 for (n_kb = 0; n_kb < kbd->n_of_kbs; n_kb++)
131 if (strlen(kbd->kbs[n_kb].command->dsc_short) > linemax)
133 linemax = strlen(kbd->kbs[n_kb].command->dsc_short);
136 linemax = linemax + 6; /* + 6 = + 3 digits + ' ' + '\n' + '\0' */
138 for (n_kb = 0; n_kb < kbd->n_of_kbs; n_kb++)
140 sprintf(line, "%d %s\n",
141 kbd->kbs[n_kb].keycode, kbd->kbs[n_kb].command->dsc_short);
142 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
144 try_fwrite(world.delim, strlen(world.delim), 1, file, f_name);
149 extern void read_keybindings_from_file(char * line, uint32_t linemax,
150 FILE * file, struct KeyBindingDB * kbdb)
152 char * context = "Failed reading keybindings from interface config file. ";
153 char * err_space = "Line illegally ends in whitespace.";
154 char * err_nospace = "No whitespace found in line.";
155 char * err_int = "Line starts not with a decimal number in digits.";
156 char * err_toolarge = "Keycode number too large, must be below 1000.";
157 char * err_cmd = "No such command in command DB.";
161 err_try_fgets(line, linemax, file, context, "0nf");
162 if (!strcmp(world.delim, line))
166 err_line(' ' == line[strlen(line) - 2], line, context, err_space);
168 err_line(!(ptr_space = strchr(line, ' ')), line, context, err_nospace);
170 err_line(0 == (ptr_space - line), line, context, err_int);
171 for (; i < (ptr_space - line); i++)
173 err_line(line[i] < '0' || '9' < line[i], line, context, err_int);
175 err_line(i > 3, line, context, err_toolarge);
177 struct KeyBinding kb;
178 line[strlen(line) - 1] = '\0';
179 kb.command = get_command(ptr_space + 1);
180 err_line(!(kb.command), line, context, err_cmd);
181 kb.keycode = atoi(line);
182 array_append(kbdb->n_of_kbs, sizeof(struct KeyBinding), (void *) &kb,
190 extern void mod_selected_keyb(char kb_c)
192 struct KeyBindingDB * kbdb = char_selected_kb_db(kb_c);
196 int keycode = getch();
197 halfdelay(world.halfdelay);
200 kbdb->kbs[kbdb->select].keycode = keycode;
207 extern void move_keyb_selection(char kb_c, char dir)
209 struct KeyBindingDB * kbdb = char_selected_kb_db(kb_c);
210 if ('u' == dir && kbdb->select > 0)
214 else if ('d' == dir && kbdb->select < kbdb->n_of_kbs - 1)