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/rexit.h" /* exit_trouble() */
9 #include "../common/try_malloc.h" /* try_malloc() */
10 #include "windows.h" /* draw_all_wins() */
11 #include "world.h" /* global world */
15 /* Return pointer to global keybindings or to keybindings for wingeometry config
16 * (c = "g") or winkeys config (c = "k") or active window's keybindings ("w").
18 static struct KeyBindingDB * char_selected_kb_db(char c);
20 /* If "keycode_given" equals "keycode_match", copy "keyname_match" to "keyname"
21 * and return 1; otherwise return 0.
23 static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
24 uint16_t keycode_match, char * keyname_match);
28 static struct KeyBindingDB * char_selected_kb_db(char c)
30 struct KeyBindingDB * kbdb;
31 kbdb = &world.kb_global;
34 kbdb = &world.kb_wingeom;
38 kbdb = &world.kb_winkeys;
42 struct Win * w = get_win_by_id(world.winDB.active);
50 static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
51 uint16_t keycode_match, char * keyname_match)
53 if (keycode_given == keycode_match)
55 int test = sprintf(keyname, "%s", keyname_match);
56 exit_trouble(test < 0, __func__, "sprintf");
64 extern struct Command * get_command_to_keycode(struct KeyBindingDB * kbdb,
68 for (n_kb = 0; n_kb < kbdb->n_of_kbs; n_kb++)
70 if (keycode == kbdb->kbs[n_kb].keycode)
72 return kbdb->kbs[n_kb].command;
80 extern char * get_keyname_to_keycode(uint16_t keycode)
82 char * keyname = try_malloc(10, __func__); /* max keyname length + 1 */
83 if (32 < keycode && keycode < 127)
85 exit_trouble(sprintf(keyname, "%c", keycode) < 0, __func__, "sprintf");
87 else if (keycode >= KEY_F0 && keycode <= KEY_F(63))
89 uint16_t f = keycode - KEY_F0;
90 exit_trouble(sprintf(keyname, "F%d", f) < 0, __func__, "sprintf");;
92 else if ( try_keycode(keycode, keyname, 9, "TAB")
93 || try_keycode(keycode, keyname, 10, "RETURN")
94 || try_keycode(keycode, keyname, 27, "ESCAPE")
95 || try_keycode(keycode, keyname, 32, "SPACE")
96 || try_keycode(keycode, keyname, KEY_UP, "UP")
97 || try_keycode(keycode, keyname, KEY_DOWN, "DOWN")
98 || try_keycode(keycode, keyname, KEY_LEFT, "LEFT")
99 || try_keycode(keycode, keyname, KEY_RIGHT, "RIGHT")
100 || try_keycode(keycode, keyname, KEY_HOME, "HOME")
101 || try_keycode(keycode, keyname, KEY_BACKSPACE, "BACKSPACE")
102 || try_keycode(keycode, keyname, KEY_DC, "DELETE")
103 || try_keycode(keycode, keyname, KEY_IC, "INSERT")
104 || try_keycode(keycode, keyname, KEY_NPAGE, "NEXT PAGE")
105 || try_keycode(keycode, keyname, KEY_PPAGE, "PREV PAGE")
106 || try_keycode(keycode, keyname, KEY_END, "END"))
112 exit_trouble(sprintf(keyname, "(unknown)") < 0, __func__, "sprintf");
119 extern void mod_selected_keyb(char kb_c)
121 struct KeyBindingDB * kbdb = char_selected_kb_db(kb_c);
125 int keycode = getch();
126 halfdelay(world.halfdelay);
129 kbdb->kbs[kbdb->select].keycode = keycode;
136 extern void move_keyb_selection(char kb_c, char dir)
138 struct KeyBindingDB * kbdb = char_selected_kb_db(kb_c);
139 if ('u' == dir && kbdb->select > 0)
143 else if ('d' == dir && kbdb->select < kbdb->n_of_kbs - 1)