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 "../common/try_malloc.h" /* try_malloc() */
9 #include "windows.h" /* draw_all_wins() */
10 #include "world.h" /* global world */
14 /* Return pointer to global keybindings or to keybindings for wingeometry config
15 * (c = "g") or winkeys config (c = "k") or active window's keybindings ("w").
17 static struct KeyBindingDB * char_selected_kb_db(char c);
19 /* If "keycode_given" equals "keycode_match", copy "keyname_match" to "keyname"
20 * and return 1; otherwise return 0.
22 static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
23 uint16_t keycode_match, char * keyname_match);
27 static struct KeyBindingDB * char_selected_kb_db(char c)
29 struct KeyBindingDB * kbdb;
30 kbdb = &world.kb_global;
33 kbdb = &world.kb_wingeom;
37 kbdb = &world.kb_winkeys;
41 struct Win * w = get_win_by_id(world.winDB.active);
49 static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
50 uint16_t keycode_match, char * keyname_match)
52 if (keycode_given == keycode_match)
54 sprintf(keyname, "%s", keyname_match);
62 extern struct Command * get_command_to_keycode(struct KeyBindingDB * kbdb,
66 for (n_kb = 0; n_kb < kbdb->n_of_kbs; n_kb++)
68 if (keycode == kbdb->kbs[n_kb].keycode)
70 return kbdb->kbs[n_kb].command;
78 extern char * get_keyname_to_keycode(uint16_t keycode)
80 char * f_name = "get_name_to_keycode()";
81 char * keyname = try_malloc(10, f_name); /* max keyname length + 1 */
82 if (32 < keycode && keycode < 127)
84 sprintf(keyname, "%c", keycode);
86 else if (keycode >= KEY_F0 && keycode <= KEY_F(63))
88 uint16_t f = keycode - KEY_F0;
89 sprintf(keyname, "F%d", f);
91 else if ( try_keycode(keycode, keyname, 9, "TAB")
92 || try_keycode(keycode, keyname, 10, "RETURN")
93 || try_keycode(keycode, keyname, 27, "ESCAPE")
94 || try_keycode(keycode, keyname, 32, "SPACE")
95 || try_keycode(keycode, keyname, KEY_UP, "UP")
96 || try_keycode(keycode, keyname, KEY_DOWN, "DOWN")
97 || try_keycode(keycode, keyname, KEY_LEFT, "LEFT")
98 || try_keycode(keycode, keyname, KEY_RIGHT, "RIGHT")
99 || try_keycode(keycode, keyname, KEY_HOME, "HOME")
100 || try_keycode(keycode, keyname, KEY_BACKSPACE, "BACKSPACE")
101 || try_keycode(keycode, keyname, KEY_DC, "DELETE")
102 || try_keycode(keycode, keyname, KEY_IC, "INSERT")
103 || try_keycode(keycode, keyname, KEY_NPAGE, "NEXT PAGE")
104 || try_keycode(keycode, keyname, KEY_PPAGE, "PREV PAGE")
105 || try_keycode(keycode, keyname, KEY_END, "END"))
111 sprintf(keyname, "(unknown)");
118 extern void mod_selected_keyb(char kb_c)
120 struct KeyBindingDB * kbdb = char_selected_kb_db(kb_c);
124 int keycode = getch();
125 halfdelay(world.halfdelay);
128 kbdb->kbs[kbdb->select].keycode = keycode;
135 extern void move_keyb_selection(char kb_c, char dir)
137 struct KeyBindingDB * kbdb = char_selected_kb_db(kb_c);
138 if ('u' == dir && kbdb->select > 0)
142 else if ('d' == dir && kbdb->select < kbdb->n_of_kbs - 1)