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 char * f_name = "try_keycode()";
54 if (keycode_given == keycode_match)
56 int test = sprintf(keyname, "%s", keyname_match);
57 exit_trouble(test < 0, f_name, "sprintf()");
65 extern struct Command * get_command_to_keycode(struct KeyBindingDB * kbdb,
69 for (n_kb = 0; n_kb < kbdb->n_of_kbs; n_kb++)
71 if (keycode == kbdb->kbs[n_kb].keycode)
73 return kbdb->kbs[n_kb].command;
81 extern char * get_keyname_to_keycode(uint16_t keycode)
83 char * f_name = "get_name_to_keycode()";
84 char * keyname = try_malloc(10, f_name); /* max keyname length + 1 */
85 if (32 < keycode && keycode < 127)
87 exit_trouble(sprintf(keyname, "%c", keycode) < 0, f_name, "sprintf()");
89 else if (keycode >= KEY_F0 && keycode <= KEY_F(63))
91 uint16_t f = keycode - KEY_F0;
92 exit_trouble(sprintf(keyname, "F%d", f) < 0, f_name, "sprintf()");;
94 else if ( try_keycode(keycode, keyname, 9, "TAB")
95 || try_keycode(keycode, keyname, 10, "RETURN")
96 || try_keycode(keycode, keyname, 27, "ESCAPE")
97 || try_keycode(keycode, keyname, 32, "SPACE")
98 || try_keycode(keycode, keyname, KEY_UP, "UP")
99 || try_keycode(keycode, keyname, KEY_DOWN, "DOWN")
100 || try_keycode(keycode, keyname, KEY_LEFT, "LEFT")
101 || try_keycode(keycode, keyname, KEY_RIGHT, "RIGHT")
102 || try_keycode(keycode, keyname, KEY_HOME, "HOME")
103 || try_keycode(keycode, keyname, KEY_BACKSPACE, "BACKSPACE")
104 || try_keycode(keycode, keyname, KEY_DC, "DELETE")
105 || try_keycode(keycode, keyname, KEY_IC, "INSERT")
106 || try_keycode(keycode, keyname, KEY_NPAGE, "NEXT PAGE")
107 || try_keycode(keycode, keyname, KEY_PPAGE, "PREV PAGE")
108 || try_keycode(keycode, keyname, KEY_END, "END"))
114 exit_trouble(sprintf(keyname, "(unknown)") < 0, f_name, "sprintf()");
121 extern void mod_selected_keyb(char kb_c)
123 struct KeyBindingDB * kbdb = char_selected_kb_db(kb_c);
127 int keycode = getch();
128 halfdelay(world.halfdelay);
131 kbdb->kbs[kbdb->select].keycode = keycode;
138 extern void move_keyb_selection(char kb_c, char dir)
140 struct KeyBindingDB * kbdb = char_selected_kb_db(kb_c);
141 if ('u' == dir && kbdb->select > 0)
145 else if ('d' == dir && kbdb->select < kbdb->n_of_kbs - 1)