1 /* src/client/interface_conf.c */
3 #define _POSIX_C_SOURCE 200809L /* getopt(), optarg, strdup() */
4 #include "interface_conf.h"
5 #include <ncurses.h> /* delwin() */
6 #include <stddef.h> /* NULL, size_t */
7 #include <stdint.h> /* UINT8_MAX, uint8_t, uint32_t */
8 #include <stdlib.h> /* EXIT_SUCCESS, atoi(), exit(), free() */
9 #include <stdio.h> /* FILE, sprintf() */
10 #include <string.h> /* strchr(), strcmp(), strdup(), strlen() */
11 #include <unistd.h> /* optarg, getopt() */
12 #include "../common/parse_file.h" /* EDIT_STARTED, parse_file(),parse_flagval(),
13 * token_from_line(), parsetest_singlechar(),
14 * parse_and_reduce_to_readyflag(),
15 * parsetest_defcontext(),parse_unknown_arg(),
16 * parsetest_too_many_values(),
17 * parse_id_uniq(), parse_init_entry()
19 #include "../common/readwrite.h" /* atomic_write_start(), atomic_write_finish(),
20 * detect_atomic_leftover(), try_fwrite()
22 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
23 #include "../common/try_malloc.h" /* try_malloc() */
24 #include "array_append.h" /* array_append() */
25 #include "cleanup.h" /* set_cleanup_flag() */
26 #include "command_db.h" /* get_command() */
27 #include "keybindings.h" /* KeyBinding, KeyBindingDB, get_command_to_keycode()*/
28 #include "map.h" /* map_center() */
29 #include "wincontrol.h" /* toggle_window() */
30 #include "windows.h" /* Win, free_winDB(), make_v_screen_and_init_win_sizes() */
31 #include "world.h" /* global world */
35 /* Editing state flags set / checked in tokens_into_entries(). */
43 READY_WIN = NAME_SET | WIDTH_SET | HEIGHT_SET | BREAK_SET,
50 /* Used in load_interface_conf() to temporarily store designated values for
51 * world.winDB.order and world.winDB.active. If those were set right away
52 * without all windows having already been initialized, orderly exits on error
53 * would be impossible, for windows are cleaned out by working toggling them off
54 * piece by peice following these values in unload_interfac_conf().
56 static char * tmp_order;
57 static char tmp_active;
61 /* Write into "file" keybindings in "kbdb" in config file format. */
62 static void write_keybindings(FILE * file, struct KeyBindingDB * kbdb);
64 /* Write into file "val" interpreted as pointing either to string (type = 's'),
65 * int16 ('i') or char ('c'), prefixed with "prefix" and put into single quotes
68 static void write_def(FILE * file, char * prefix, uint8_t quotes, char * val,
71 /* Read in "token0" and "token1" as interface config definition lines, apply
72 * definitions to WinDB, KeyBindingsDBs and tmp_order/tmp_active. If argument is
73 * "KEY", get third token via token_from_line() for complete keybinding.
75 static void tokens_into_entries(char * token0, char * token1);
77 /* If "win" points to non-NULL Win struct, append to it to WinDB.wins array and
78 * add its ID to world.winDB.ids.
80 static void write_if_win(struct Win ** win);
82 /* Start block setting data for window "token1" id if "token0" == "str_win".*/
83 static uint8_t start_win(char * token0, char * token1, char * str_win,
84 uint8_t * win_flags, struct Win ** win);
86 /* Start block setting window order and focus if "token0" = "str_ord".
87 * "tmp_order" stores the designated world.winDB.order string (only later to
88 * be realized in load_interface_conf() when all windows have been initialized),
89 * as does "tmp_active" for the designated world.winDB.active char, defaulting
90 * to the first char in the window order string if not otherwise specified.
92 static uint8_t start_ord(char * token0, char * token1, char * str_ord,
95 /* Start block setting non-window-specific keybindings if "token0" == "str_kbd".
96 * "token1" must be "global", "wingeom" or "winkeys" to set KeyBindingDB to edit
97 * ("kbdb") to world.kb_global, world.kb_wingeom or world.kb_winkeys.
99 static uint8_t start_kbd(char * token0, char * token1, char * str_kbd,
100 uint8_t * kbd_flags, struct KeyBindingDB ** kbdb);
102 /* Helper to tokens_into_entries(), sets specific entry member data. "token0"
103 * and the state of flags determine what entry member to edit; "win" and "kbdb"
104 * are entries to write "token1" data into (as is the global "tmp_active"). If
105 * "token0" is "str_key", a keybinding is defined and "token2" is read/must not
106 * be NULL. In that case, the line read is checked against having a 4th token.
108 static uint8_t set_members(char * token0, char * token1, uint8_t * win_flags,
109 uint8_t * ord_flags,uint8_t kbd_flags,char * str_key,
110 struct Win * win, struct KeyBindingDB * kbdb);
112 /* Add keybinding defined in "token1" as keycode and "token2" as command to
113 * "kbdb" if "flags" are set to EDIT_STARTED.
115 static void set_keybindings(char * token1, uint8_t flags,
116 struct KeyBindingDB * kbdb);
120 static void write_keybindings(FILE * file, struct KeyBindingDB * kbdb)
122 char * f_name = "write_keybindings()";
126 for (i_kb = 0; i_kb < kbdb->n_of_kbs; i_kb++)
128 size_t size = strlen(tok0) + strlen(sep) + 3 + strlen(sep)
129 + strlen(kbdb->kbs[i_kb].command->dsc_short) + 1 + 1;
130 char * line = try_malloc(size, f_name);
131 int test = snprintf(line, size, "%s%s%d%s%s\n",
132 tok0, sep, kbdb->kbs[i_kb].keycode, sep,
133 kbdb->kbs[i_kb].command->dsc_short);
134 exit_trouble(test < 0, f_name, "snprintf()");
135 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
142 static void write_def(FILE * file, char * prefix, uint8_t quotes, char * val,
145 char * f_name = "write_def()";
146 char * val_str = NULL;
147 int test_val_str = 1;
150 val_str = strdup(val);
154 size_t size_val_str = 6 + 1;
155 val_str = try_malloc(size_val_str, f_name);
156 test_val_str = snprintf(val_str, size_val_str, "%d", (int16_t) *val);
158 else if ('c' == type)
160 size_t size_val_str = 1 + 1;
161 val_str = try_malloc(size_val_str, f_name);
162 test_val_str = snprintf(val_str, size_val_str, "%c", * val);
164 exit_trouble(test_val_str < 0, f_name, "snprintf()");
165 char * quote = quotes ? "'": "";
167 size_t size = strlen(prefix) + strlen(val_str) + (2 * strlen(quote))
169 char * line = try_malloc(size, f_name);
170 int test = snprintf(line, size, "%s%s%s%s%s",
171 prefix, quote, val_str, quote, affix);
172 exit_trouble(test < 0, f_name, "snprintf()");
174 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
180 static void tokens_into_entries(char * token0, char * token1)
182 char * str_win = "WINDOW";
183 char * str_ord = "WIN_ORDER";
184 char * str_kbd = "KEYBINDINGS";
185 char * str_key = "KEY";
186 static uint8_t win_flags = READY_WIN;
187 static uint8_t ord_flags = READY_ORD;
188 static uint8_t kbd_flags = READY_KBD;
189 static struct Win * win = NULL;
190 static struct KeyBindingDB * kbdb = NULL;
191 if (!token0 || !strcmp(token0, str_win) || !strcmp(token0, str_ord)
192 || !strcmp(token0, str_kbd))
194 parse_and_reduce_to_readyflag(&win_flags, READY_WIN);
195 parse_and_reduce_to_readyflag(&ord_flags, READY_ORD);
196 parse_and_reduce_to_readyflag(&kbd_flags, READY_KBD);
201 if (strcmp(token0, str_key))
203 parsetest_too_many_values();
205 if (!( start_win(token0, token1, str_win, &win_flags, &win)
206 || start_ord(token0, token1, str_ord, &ord_flags)
207 || start_kbd(token0, token1, str_kbd, &kbd_flags, &kbdb)
208 || set_members(token0, token1, &win_flags, &ord_flags, kbd_flags,
209 str_key, win, kbdb)))
218 static void write_if_win(struct Win ** win)
220 char * f_name = "write_if_win()";
223 (*win)->target_height_type = (0 >= (*win)->target_height);
224 (*win)->target_width_type = (0 >= (*win)->target_width);;
225 size_t old_ids_size = strlen(world.winDB.ids);
226 size_t new_size = old_ids_size + 1 + 1;
227 char * new_ids = try_malloc(new_size, f_name);
228 int test = snprintf(new_ids,new_size,"%s%c",world.winDB.ids,(*win)->id);
229 exit_trouble(test < 0, f_name, "snprintf()");
230 free(world.winDB.ids);
231 world.winDB.ids = new_ids;
232 array_append(old_ids_size, sizeof(struct Win), *win,
233 (void **) &world.winDB.wins);
241 static uint8_t start_win(char * token0, char * token1, char * str_win,
242 uint8_t * win_flags, struct Win ** win)
244 if (strcmp(token0, str_win))
248 *win = (struct Win *) parse_init_entry(win_flags, sizeof(struct Win));
249 parsetest_singlechar(token1);
250 parse_id_uniq(world.winDB.ids && (NULL!=strchr(world.winDB.ids,token1[0])));
251 (*win)->id = token1[0];
257 static uint8_t start_ord(char * token0, char * token1, char * str_ord,
260 if (strcmp(token0, str_ord))
264 *ord_flags = EDIT_STARTED;
266 for (; i < strlen(token1); i++)
268 err_line(!strchr(world.winDB.legal_ids, token1[i]), "Illegal ID(s).");
271 tmp_order = strdup(token1);
272 if (0 < strlen(tmp_order))
274 tmp_active = tmp_order[0];
281 static uint8_t start_kbd(char * token0, char * token1, char * str_kbd,
282 uint8_t * kbd_flags, struct KeyBindingDB ** kbdb)
284 if (strcmp(token0, str_kbd))
288 *kbd_flags = EDIT_STARTED;
289 if (!strcmp(token1, "global"))
291 *kbdb = &world.kb_global;
293 else if (!strcmp(token1, "wingeom"))
295 *kbdb = &world.kb_wingeom;
297 else if (!strcmp(token1, "winkeys"))
299 *kbdb = &world.kb_winkeys;
303 err_line(1, "Value must be 'global', 'wingeom' or 'winkeys'.");
310 static uint8_t set_members(char * token0, char * token1, uint8_t * win_flags,
311 uint8_t * ord_flags,uint8_t kbd_flags,char * str_key,
312 struct Win * win, struct KeyBindingDB * kbdb)
314 if ( parse_flagval(token0, token1, "NAME", win_flags,
315 NAME_SET, 's', (char *) &win->title)
316 || parse_flagval(token0, token1, "WIDTH", win_flags,
317 WIDTH_SET, 'i', (char *) &win->target_width)
318 || parse_flagval(token0, token1, "HEIGHT", win_flags,
319 HEIGHT_SET, 'i', (char *) &win->target_height));
320 else if (parse_flagval(token0, token1, "BREAK", win_flags,
321 BREAK_SET, '8', (char *) &win->linebreak))
323 err_line(2 < win->linebreak, "Value must be 0, 1 or 2.");
325 else if (parse_flagval(token0, token1, "WIN_FOCUS", ord_flags,
326 FOCUS_SET, 'c', &tmp_active))
328 char * err_null = "Value not empty as it should be.";
329 char * err_outside = "ID not found in WIN_ORDER ID series.";
330 err_line(!strlen(tmp_order) && tmp_active, err_null);
331 err_line(!strchr(tmp_order, tmp_active), err_outside);
333 else if (!strcmp(token0, str_key))
335 if (*win_flags & EDIT_STARTED)
337 set_keybindings(token1, *win_flags, &win->kb);
341 set_keybindings(token1, kbd_flags, kbdb);
353 static void set_keybindings(char * token1, uint8_t flags,
354 struct KeyBindingDB * kbdb)
356 char * token2 = token_from_line(NULL);
357 err_line(!token2, "No binding to key given.");
358 parsetest_too_many_values();
359 char * err_code = "Invalid keycode. Must be >= 0 and < 1000.";
360 parsetest_defcontext(flags);
361 char * err_many = "No more than 255 keybindings allowed in one section.";
362 err_line(UINT8_MAX == kbdb->n_of_kbs, err_many);
363 struct KeyBinding kb;
364 uint8_t test = strlen(token1);
365 err_line(!test, err_code);
367 for (i = 0; '\0' != token1[i]; i++)
369 test= i > 2 || '0' > token1[i] || '9' < token1[i];
370 err_line(test, err_code);
372 kb.keycode = atoi(token1);
373 char * err_uniq = "Binding to key already defined.";
374 err_line(NULL != get_command_to_keycode(kbdb, kb.keycode), err_uniq);
375 kb.command = get_command(token2);
376 err_line(!(kb.command), "No such command in command DB.");
377 array_append(kbdb->n_of_kbs, sizeof(struct KeyBinding), (void *) &kb,
384 extern void obey_argv(int argc, char * argv[])
387 while (-1 != (opt = getopt(argc, argv, "i:")))
391 world.path_interface = optarg;
402 extern void save_interface_conf()
405 FILE * file = atomic_write_start(world.path_interface, &path_tmp);
406 char * str_keybs = "\nKEYBINDINGS ";
407 write_def(file, str_keybs, 1, "global", 's');
408 write_keybindings(file, &world.kb_global);
409 write_def(file, str_keybs, 1, "wingeom", 's');
410 write_keybindings(file, &world.kb_wingeom);
411 write_def(file, str_keybs, 1, "winkeys", 's');
412 write_keybindings(file, &world.kb_winkeys);
413 write_def(file, "\nWIN_ORDER ", 1, world.winDB.order, 's');
414 if (world.winDB.active)
416 write_def(file, "WIN_FOCUS ", 1, &world.winDB.active, 'c');
419 for (i = 0; i < strlen(world.winDB.ids); i++)
421 write_def(file, "\nWINDOW ", 0, &world.winDB.ids[i], 'c');
422 struct Win * win = get_win_by_id(world.winDB.ids[i]);
423 write_def(file, "NAME ", 1, win->title, 's');
424 write_def(file, "BREAK ", 0, (char *) &win->linebreak, 'i');
425 write_def(file, "WIDTH ", 0, (char *) &win->target_width, 'i');
426 write_def(file, "HEIGHT ", 0, (char *) &win->target_height, 'i');
427 write_keybindings(file, &win->kb);
429 atomic_write_finish(file, world.path_interface, path_tmp);
434 extern void load_interface_conf()
436 char * f_name = "load_interface_conf()";
437 world.winDB.ids = try_malloc(1, f_name);
438 world.winDB.ids[0] = '\0';
439 world.winDB.wins = NULL;
440 tmp_order = try_malloc(1, f_name);
443 detect_atomic_leftover(world.path_interface);
444 parse_file(world.path_interface, tokens_into_entries);
445 char * err = "Not all expected windows defined in config file.";
446 exit_err(strlen(world.winDB.legal_ids) != strlen(world.winDB.ids), err);
447 make_v_screen_and_init_win_sizes();
448 world.winDB.order = try_malloc(1, f_name);
449 world.winDB.order[0] = '\0';
451 for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
452 world.winDB.active = tmp_active;
454 set_cleanup_flag(CLEANUP_INTERFACE);
459 extern void unload_interface_conf()
461 free(world.kb_global.kbs);
462 world.kb_global.kbs = NULL;
463 world.kb_global.n_of_kbs = 0;
464 free(world.kb_wingeom.kbs);
465 world.kb_wingeom.kbs = NULL;
466 world.kb_wingeom.n_of_kbs = 0;
467 free(world.kb_winkeys.kbs);
468 world.kb_winkeys.kbs = NULL;
469 world.kb_winkeys.n_of_kbs = 0;
470 while ('\0' != world.winDB.active)
472 toggle_window(world.winDB.active);
475 delwin(world.winDB.v_screen);
480 extern void reload_interface_conf()
482 unload_interface_conf();
483 load_interface_conf();
485 world.winDB.v_screen_offset = 0;