1 /* src/client/keybindings.c
3 * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4 * or any later version. For details on its copyright, license, and warranties,
5 * see the file NOTICE in the root directory of the PlomRogue source package.
8 #include "keybindings.h"
9 #include <ncurses.h> /* keycode defines, cbreak(), halfdelay(), getch() */
10 #include <stddef.h> /* NULL */
11 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
12 #include <stdio.h> /* FILE, sprintf() */
13 #include "../common/rexit.h" /* exit_trouble() */
14 #include "../common/try_malloc.h" /* try_malloc() */
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 int test = sprintf(keyname, "%s", keyname_match);
61 exit_trouble(test < 0, __func__, "sprintf");
69 extern struct Command * get_command_to_keycode(struct KeyBindingDB * kbdb,
73 for (n_kb = 0; n_kb < kbdb->n_of_kbs; n_kb++)
75 if (keycode == kbdb->kbs[n_kb].keycode)
77 return kbdb->kbs[n_kb].command;
85 extern char * get_keyname_to_keycode(uint16_t keycode)
87 char * keyname = try_malloc(10, __func__); /* max keyname length + 1 */
88 if (32 < keycode && keycode < 127)
90 exit_trouble(sprintf(keyname, "%c", keycode) < 0, __func__, "sprintf");
92 else if (keycode >= KEY_F0 && keycode <= KEY_F(63))
94 uint16_t f = keycode - KEY_F0;
95 exit_trouble(sprintf(keyname, "F%d", f) < 0, __func__, "sprintf");;
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 exit_trouble(sprintf(keyname, "(unknown)") < 0, __func__, "sprintf");
124 extern void mod_selected_keyb(char kb_c)
126 struct KeyBindingDB * kbdb = char_selected_kb_db(kb_c);
130 int keycode = getch();
131 halfdelay(world.halfdelay);
134 kbdb->kbs[kbdb->select].keycode = keycode;
141 extern void move_keyb_selection(char kb_c, char dir)
143 struct KeyBindingDB * kbdb = char_selected_kb_db(kb_c);
144 if ('u' == dir && kbdb->select > 0)
148 else if ('d' == dir && kbdb->select < kbdb->n_of_kbs - 1)