home · contact · privacy
In client: Fix FIXME in get_keyname_to_keycode().
[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 <stdlib.h> /* atoi() */
9 #include <string.h> /* strlen(), strchr(), strcmp() */
10 #include "../common/err_try_fgets.h" /* err_try_fgets(), err_line() */
11 #include "../common/readwrite.h" /* try_fwrite()*/
12 #include "../common/try_malloc.h" /* try_malloc() */
13 #include "command_db.h" /* get_command() */
14 #include "misc.h" /* array_append() */
15 #include "windows.h" /* draw_all_wins() */
16 #include "world.h" /* global world */
17
18
19
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").
22  */
23 static struct KeyBindingDB * char_selected_kb_db(char c);
24
25 /* If "keycode_given" equals "keycode_match", copy "keyname_match" to "keyname"
26  * and return 1; otherwise return 0.
27  */
28 static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
29                            uint16_t keycode_match, char * keyname_match);
30
31
32
33 static struct KeyBindingDB * char_selected_kb_db(char c)
34 {
35     struct KeyBindingDB * kbdb;
36     kbdb = &world.kb_global;
37     if      ('g' == c)
38     {
39         kbdb = &world.kb_wingeom;
40     }
41     else if ('k' == c)
42     {
43         kbdb = &world.kb_winkeys;
44     }
45     else if ('w' == c)
46     {
47         struct Win * w = get_win_by_id(world.winDB.active);
48         kbdb = &w->kb;
49     }
50     return kbdb;
51 }
52
53
54
55 static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
56                            uint16_t keycode_match, char * keyname_match)
57 {
58     if (keycode_given == keycode_match)
59     {
60         sprintf(keyname, "%s", keyname_match);
61         return 1;
62     }
63     return 0;
64 }
65
66
67
68 extern struct Command * get_command_to_keycode(struct KeyBindingDB * kbdb,
69                                                uint16_t keycode)
70 {
71     uint16_t n_kb;
72     for (n_kb = 0; n_kb < kbdb->n_of_kbs; n_kb++)
73     {
74         if (keycode == kbdb->kbs[n_kb].keycode)
75         {
76             return kbdb->kbs[n_kb].command;
77         }
78     }
79     return NULL;
80 }
81
82
83
84 extern char * get_keyname_to_keycode(uint16_t keycode)
85 {
86     char * f_name = "get_name_to_keycode()";
87     char * keyname = try_malloc(10, f_name);        /* max keyname length + 1 */
88     if (32 < keycode && keycode < 127)
89     {
90         sprintf(keyname, "%c", keycode);
91     }
92     else if (keycode >= KEY_F0 && keycode <= KEY_F(63))
93     {
94         uint16_t f = keycode - KEY_F0;
95         sprintf(keyname, "F%d", f);
96     }
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"))
112     {
113         ;
114     }
115     else
116     {
117         sprintf(keyname, "(unknown)");
118     }
119     return keyname;
120 }
121
122
123
124 extern void write_keybindings_to_file(FILE * file, struct KeyBindingDB * kbd)
125 {
126     char * f_name = "write_keybindings_to_file()";
127     uint16_t linemax = 0;
128     uint16_t n_kb;
129     for (n_kb = 0; n_kb < kbd->n_of_kbs; n_kb++)
130     {
131         if (strlen(kbd->kbs[n_kb].command->dsc_short) > linemax)
132         {
133             linemax = strlen(kbd->kbs[n_kb].command->dsc_short);
134         }
135     }
136     linemax = linemax + 6;            /* + 6 = + 3 digits + ' ' + '\n' + '\0' */
137     char line[linemax];
138     for (n_kb = 0; n_kb < kbd->n_of_kbs; n_kb++)
139     {
140         sprintf(line, "%d %s\n",
141                 kbd->kbs[n_kb].keycode, kbd->kbs[n_kb].command->dsc_short);
142         try_fwrite(line, sizeof(char), strlen(line), file, f_name);
143     }
144     try_fwrite(world.delim, strlen(world.delim), 1, file, f_name);
145 }
146
147
148
149 extern void read_keybindings_from_file(char * line, uint32_t linemax,
150                                        FILE * file, struct KeyBindingDB * kbdb)
151 {
152     char * context = "Failed reading keybindings from interface config file. ";
153     char * err_space    = "Line illegally ends in whitespace.";
154     char * err_nospace  = "No whitespace found in line.";
155     char * err_int      = "Line starts not with a decimal number in digits.";
156     char * err_toolarge = "Keycode number too large, must be below 1000.";
157     char * err_cmd      = "No such command in command DB.";
158     kbdb->n_of_kbs = 0;
159     while (1)
160     {
161         err_try_fgets(line, linemax, file, context, "0nf");
162         if (!strcmp(world.delim, line))
163         {
164             break;
165         }
166         err_line(' ' == line[strlen(line) - 2], line, context, err_space);
167         char * ptr_space;
168         err_line(!(ptr_space = strchr(line, ' ')), line, context, err_nospace);
169         uint8_t i = 0;
170         err_line(0 == (ptr_space - line), line, context, err_int);
171         for (; i < (ptr_space - line); i++)
172         {
173             err_line(line[i] < '0' || '9' < line[i], line, context, err_int);
174         }
175         err_line(i > 3, line, context, err_toolarge);
176
177         struct KeyBinding kb;
178         line[strlen(line) - 1] = '\0';
179         kb.command = get_command(ptr_space + 1);
180         err_line(!(kb.command), line, context, err_cmd);
181         kb.keycode = atoi(line);
182         array_append(kbdb->n_of_kbs, sizeof(struct KeyBinding), (void *) &kb,
183                      (void **) kbdb);
184         kbdb->n_of_kbs++;
185     }
186 }
187
188
189
190 extern void mod_selected_keyb(char kb_c)
191 {
192     struct KeyBindingDB * kbdb = char_selected_kb_db(kb_c);
193     kbdb->edit = 1;
194     draw_all_wins();
195     cbreak();
196     int keycode = getch();
197     halfdelay(world.halfdelay);
198     if (keycode < 1000)
199     {
200         kbdb->kbs[kbdb->select].keycode = keycode;
201     }
202     kbdb->edit = 0;
203 }
204
205
206
207 extern void move_keyb_selection(char kb_c, char dir)
208 {
209     struct KeyBindingDB * kbdb = char_selected_kb_db(kb_c);
210     if      ('u' == dir && kbdb->select > 0)
211     {
212         kbdb->select--;
213     }
214     else if ('d' == dir && kbdb->select < kbdb->n_of_kbs - 1)
215     {
216         kbdb->select++;
217     }
218 }