home · contact · privacy
Use not f_name variable but __func__, standardize function name writing.
[plomrogue] / src / client / keybindings.c
1 /* src/client/keybindings.c */
2
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 */
12 struct Command;
13
14
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").
17  */
18 static struct KeyBindingDB * char_selected_kb_db(char c);
19
20 /* If "keycode_given" equals "keycode_match", copy "keyname_match" to "keyname"
21  * and return 1; otherwise return 0.
22  */
23 static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
24                            uint16_t keycode_match, char * keyname_match);
25
26
27
28 static struct KeyBindingDB * char_selected_kb_db(char c)
29 {
30     struct KeyBindingDB * kbdb;
31     kbdb = &world.kb_global;
32     if      ('g' == c)
33     {
34         kbdb = &world.kb_wingeom;
35     }
36     else if ('k' == c)
37     {
38         kbdb = &world.kb_winkeys;
39     }
40     else if ('w' == c)
41     {
42         struct Win * w = get_win_by_id(world.winDB.active);
43         kbdb = &w->kb;
44     }
45     return kbdb;
46 }
47
48
49
50 static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
51                            uint16_t keycode_match, char * keyname_match)
52 {
53     if (keycode_given == keycode_match)
54     {
55         int test = sprintf(keyname, "%s", keyname_match);
56         exit_trouble(test < 0, __func__, "sprintf");
57         return 1;
58     }
59     return 0;
60 }
61
62
63
64 extern struct Command * get_command_to_keycode(struct KeyBindingDB * kbdb,
65                                                uint16_t keycode)
66 {
67     uint8_t n_kb;
68     for (n_kb = 0; n_kb < kbdb->n_of_kbs; n_kb++)
69     {
70         if (keycode == kbdb->kbs[n_kb].keycode)
71         {
72             return kbdb->kbs[n_kb].command;
73         }
74     }
75     return NULL;
76 }
77
78
79
80 extern char * get_keyname_to_keycode(uint16_t keycode)
81 {
82     char * keyname = try_malloc(10, __func__);      /* max keyname length + 1 */
83     if (32 < keycode && keycode < 127)
84     {
85         exit_trouble(sprintf(keyname, "%c", keycode) < 0, __func__, "sprintf");
86     }
87     else if (keycode >= KEY_F0 && keycode <= KEY_F(63))
88     {
89         uint16_t f = keycode - KEY_F0;
90         exit_trouble(sprintf(keyname, "F%d", f) < 0, __func__, "sprintf");;
91     }
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"))
107     {
108         ;
109     }
110     else
111     {
112         exit_trouble(sprintf(keyname, "(unknown)") < 0, __func__, "sprintf");
113     }
114     return keyname;
115 }
116
117
118
119 extern void mod_selected_keyb(char kb_c)
120 {
121     struct KeyBindingDB * kbdb = char_selected_kb_db(kb_c);
122     kbdb->edit = 1;
123     draw_all_wins();
124     cbreak();
125     int keycode = getch();
126     halfdelay(world.halfdelay);
127     if (keycode < 1000)
128     {
129         kbdb->kbs[kbdb->select].keycode = keycode;
130     }
131     kbdb->edit = 0;
132 }
133
134
135
136 extern void move_keyb_selection(char kb_c, char dir)
137 {
138     struct KeyBindingDB * kbdb = char_selected_kb_db(kb_c);
139     if      ('u' == dir && kbdb->select > 0)
140     {
141         kbdb->select--;
142     }
143     else if ('d' == dir && kbdb->select < kbdb->n_of_kbs - 1)
144     {
145         kbdb->select++;
146     }
147 }