1 /* src/client/wincontrol.c */
3 #include "wincontrol.h"
4 #include <errno.h> /* global errno */
5 #include <dirent.h> /* DIR, struct dirent, opendir(), closedir(), readdir() */
6 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
7 #include <stdio.h> /* FILE */
8 #include <stdlib.h> /* free(), atoi() */
9 #include <string.h> /* strlen(), strchr(), strstr(), memcpy() */
10 #include "../common/readwrite.h" /* try_fopen(), textfile_sizes(), try_fgets(),
11 * textfile_sizes(), try_fwrite(), try_fgetc(),
12 * try_fclose_unlink_rename(), try_fputc()
14 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
15 #include "../common/try_malloc.h" /* try_malloc() */
16 #include "../common/yx_uint16.h" /* struct yx_uint16 */
17 #include "draw_wins.h" /* draw_win_map(), draw_win_info(), draw_win_log(),
18 * draw_win_available_keybindings(),
19 * draw_win_inventory(), draw_win_keybindings_global(),
20 * draw_win_keybindings_winconf_geometry(),
21 * draw_win_keybindings_winconf_keybindings(),
22 * draw_winconf_geometry(), draw_winconf_keybindings()
24 #include "keybindings.h" /* struct KeyBinding, free_keybindings() */
25 #include "windows.h" /* struct Win, resize_active_win(), reset_pad_offset(),
26 * append_win(), suspend_win(), init_win(), free_win()
28 #include "world.h" /* global world */
32 /* Return string "prefix" + "id"; malloc()'s string, remember to call free()! */
33 static char * string_prefixed_id(char * prefix, char id);
35 /* Initialize Winconf of "id" from appropriate config file.*/
36 static void init_winconf_from_file(char id, struct WinConf * winconf);
38 /* Wrapper around init_win() called with values from Winconf of "id". */
39 static void init_win_from_winconf(char id);
41 /* Save title, draw function, size of window identified by "id" to conffile. */
42 static void save_win_config(char id);
44 /* Free data pointed to inside individual WinConf struct of "id". */
45 static void free_winconf_data(char id);
47 /* Write geometry of a window to its WinConf, as positive or negative values
48 * (dependent on state ofWinConf->height_type / WinConf->width_type).
50 static void set_winconf_geometry(char id);
52 /* Get WinConf by "id"; get id of WinConf mothering "win". */
53 static struct WinConf * get_winconf_by_id(char id);
55 /* Get (Win->draw) function identified by "c"; NULL if c not mapped to one. */
56 static void * get_drawfunc_by_char(char c);
58 /* Iterate over chars of world.winconf_db.winconf_ids array. Restart after \0.*/
59 static char get_next_winconf_id();
63 static char * string_prefixed_id(char * prefix, char id)
65 uint8_t size = strlen(prefix) + 2;
66 char * path = try_malloc(size, "string_prefixed_id()");
67 sprintf(path, "%s_", prefix);
74 static void init_winconf_from_file(char id, struct WinConf * winconf)
76 /* Assign WinConf id to filename path, error message context, winconf->id.*/
77 char * tmp = "init_winconf_from_file() on window id '_'";
78 char * context = try_malloc(strlen(tmp) + 1, "init_winconf_from_file()");
79 memcpy(context, tmp, strlen(tmp) + 1);
80 context[strlen(tmp) - 2] = id;
81 char * path = string_prefixed_id("confclient/windows/Win_", id);
84 /* Prepare reading in file line by line into "line" array. */
85 FILE * file = try_fopen(path, "r", context);
87 uint32_t linemax = textfile_sizes(file, NULL);
88 char line[linemax + 1];
90 /* Read/determine winconf->title, ->draw, ->height(_type),->width(_type). */
91 try_fgets(line, linemax + 1, file, context);
92 winconf->title = try_malloc(strlen(line), context);
93 memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */
94 winconf->title[strlen(line) - 1] = '\0'; /* char at end of string. */
95 try_fgets(line, linemax + 1, file, context);
96 winconf->draw = line[0];
97 try_fgets(line, linemax + 1, file, context);
98 winconf->height = atoi(line);
99 winconf->height_type = (0 >= winconf->height);
100 try_fgets(line, linemax + 1, file, context);
101 winconf->width = atoi(line);
102 winconf->width_type = (0 >= winconf->width);
104 /* Read in window-specific keybindings (winconf->kb). */
105 char command[linemax + 1];
107 struct KeyBinding ** loc_last_ptr = &winconf->kb.kbs;
109 while (try_fgets(command, linemax + 1, file, context))
111 if ('\n' == command[0] || 0 == command[0])
115 * loc_last_ptr = try_malloc(sizeof(struct KeyBinding), context);
116 struct KeyBinding * kb_p = * loc_last_ptr;
118 kb_p->key = atoi(command);
119 cmdptr = strchr(command, ' ') + 1;
120 cmdptr[strlen(cmdptr) - 1] = '\0';
121 kb_p->command = get_command(cmdptr);
122 loc_last_ptr = & kb_p->next;
125 /* Init remaining values to zero and cleaning up. */
127 winconf->kb.edit = 0;
128 winconf->kb.select = 0;
129 try_fclose(file, context);
135 static void init_win_from_winconf(char id)
137 char * err = "get_drawfunc_by_char() returns NULL to init_win_from_file().";
138 struct WinConf * winconf = get_winconf_by_id(id);
139 void * f = get_drawfunc_by_char(winconf->draw);
140 exit_err(NULL == f, err);
141 init_win(&winconf->win, winconf->title, winconf->height, winconf->width, f);
146 static void save_win_config(char id)
148 char * f_name = "save_win_config()";
150 /* Prepare atomic file saving. */
151 char * path_tmp = string_prefixed_id("confclient/windows/Win_tmp_", id);
152 FILE * file = try_fopen(path_tmp, "w", f_name);
154 /* Save, line by line, ->title, ->draw, ->height and ->width. */
155 struct WinConf * wc = get_winconf_by_id(id);
156 uint8_t size = strlen(wc->title) + 2;
157 if (size < 7) /* Ensure that at least 5 + 2 char fit into line so that */
158 { /* the digit representation of any uint16_t may be stored. */
162 sprintf(line, "%s\n", wc->title);
163 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
164 sprintf(line, "%c\n", wc->draw);
165 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
166 sprintf(line, "%d\n", wc->height);
167 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
168 sprintf(line, "%d\n", wc->width);
169 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
171 /* Save window-specific keybindings (->kb.kbs). */
172 uint16_t linemax = 0;
173 struct KeyBinding * kb_p = wc->kb.kbs;
176 if (strlen(kb_p->command->dsc_short) > linemax)
178 linemax = strlen(kb_p->command->dsc_short);
182 linemax = linemax + 6; /* + 6: + 3 digits + whitespace + \n + \0 */
183 char kb_line[linemax];
187 sprintf(kb_line, "%d %s\n", kb_p->key, kb_p->command->dsc_short);
188 try_fwrite(kb_line, sizeof(char), strlen(kb_line), file, f_name);
192 /* Finish atomic file saving and clean up. */
193 char * path = string_prefixed_id("confclient/windows/Win_", id);
194 try_fclose_unlink_rename(file, path_tmp, path, f_name);
201 static void free_winconf_data(char id)
203 struct WinConf * wc = get_winconf_by_id(id);
205 free_keybindings(wc->kb.kbs);
211 static void set_winconf_geometry(char id)
213 struct WinConf * wcp = get_winconf_by_id(id);
214 if (0 == wcp->height_type)
216 wcp->height = wcp->win->framesize.y;
218 else if (1 == wcp->height_type)
220 wcp->height = wcp->win->framesize.y - world.wmeta.padsize.y + 1;
222 if (0 == wcp->width_type)
224 wcp->width = wcp->win->framesize.x;
226 else if (1 == wcp->width_type)
228 wcp->width = wcp->win->framesize.x - world.wmeta.padsize.x;
234 static struct WinConf * get_winconf_by_id(char id)
239 if (id == world.winconf_db.winconfs[i].id)
241 return &world.winconf_db.winconfs[i];
249 static void * get_drawfunc_by_char(char c)
254 return draw_win_inventory;
258 return draw_win_info;
266 return draw_win_available_keybindings;
274 return draw_win_keybindings_global;
278 return draw_win_keybindings_winconf_geometry;
282 return draw_win_keybindings_winconf_keybindings;
289 static char get_next_winconf_id()
291 static uint8_t i = 0;
292 char c = world.winconf_db.winconf_ids[i];
304 extern struct WinConf * get_winconf_by_win(struct Win * win)
309 if (win == world.winconf_db.winconfs[i].win)
311 return &world.winconf_db.winconfs[i];
319 extern struct Win * get_win_by_id(char id)
321 struct WinConf * wc = get_winconf_by_id(id);
327 extern void init_winconfs()
329 char * f_name = "init_winconfs()";
331 /* Fill world.winconf_db.winconf_ids with confclient/windows/Win_*
332 * filenames' end chars.
334 uint8_t max_wins = 255; /* Maximum number of window ids to store. */
335 DIR * dp = opendir("confclient/windows");
336 exit_trouble(NULL == dp, f_name, "opendir()");
339 char * winconf_ids = try_malloc(max_wins + 1, f_name);
342 while (NULL != (fn = readdir(dp)) && i < max_wins)
344 if (5 == strlen(fn->d_name) && fn->d_name == strstr(fn->d_name, "Win_"))
351 winconf_ids[i] = '\0';
352 exit_trouble(errno, f_name, "readdir()");
353 exit_trouble(closedir(dp), f_name, "closedir()");
354 world.winconf_db.winconf_ids = try_malloc(strlen(winconf_ids) + 1, f_name);
355 memcpy(world.winconf_db.winconf_ids, winconf_ids, strlen(winconf_ids) + 1);
358 /* Initialize world.winconf_db.winconfs from Win_* files named in
359 * world.winconf_db.winconf_ids.
361 size_t size = strlen(world.winconf_db.winconf_ids) * sizeof(struct WinConf);
362 world.winconf_db.winconfs = try_malloc(size, f_name);
364 while (0 != (id = get_next_winconf_id()))
366 init_winconf_from_file(id, &world.winconf_db.winconfs[i]);
373 extern void free_winconfs()
376 while (0 != (id = get_next_winconf_id()))
378 free_winconf_data(id);
380 free(world.winconf_db.winconf_ids);
381 free(world.winconf_db.winconfs);
386 extern void init_wins()
389 while (0 != (id = get_next_winconf_id()))
391 init_win_from_winconf(id);
397 extern void sorted_wintoggle_and_activate()
399 char * f_name = "sorted_wintoggle_and_activate()";
401 /* Read from file order of windows to be toggled + active win selection. */
402 char * path = "confclient/windows/toggle_order_and_active";
403 FILE * file = try_fopen(path, "r", f_name);
404 uint32_t linemax = textfile_sizes(file, NULL);
405 char win_order[linemax + 1];
406 try_fgets(win_order, linemax + 1, file, f_name);
407 int char_or_eof = try_fgetc(file, f_name);
408 exit_trouble(EOF==char_or_eof, f_name, "fgetc() unexpectedly hitting EOF");
409 uint8_t a = (uint8_t) char_or_eof;
410 try_fclose(file, f_name);
412 /* Toggle windows and set active window selection. */
414 for (; i < strlen(win_order) - 1; i++)
416 if (NULL == strchr(world.winconf_db.winconf_ids, win_order[i]))
420 toggle_window(win_order[i]);
421 if (a == (uint8_t) win_order[i])
423 world.wmeta.active = get_win_by_id(win_order[i]);
430 extern void save_win_configs()
432 char * f_name = "save_win_configs()";
434 /* Save individual world.winconf_db.winconfs to their proper files. */
435 uint8_t max_wins = 255; /* n of WinConf fitting into world.winconf_db */
437 while (0 != (id = get_next_winconf_id()))
442 /* Save order of windows to toggle on start / which to select as active. */
443 char * path = "confclient/windows/toggle_order_and_active";
444 char * path_tmp = "confclient/windows/toggle_order_and_active_tmp";
445 FILE * file = try_fopen(path_tmp, "w", f_name);
446 char line[max_wins + 2];
447 struct Win * w_p = world.wmeta.chain_start;
449 while (0 != w_p && i < max_wins)
451 struct WinConf * wc = get_winconf_by_win(w_p);
458 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
459 if (0 != world.wmeta.active)
461 struct WinConf * wc = get_winconf_by_win(world.wmeta.active);
462 try_fputc(wc->id, file, f_name);
464 try_fclose_unlink_rename(file, path_tmp, path, f_name);
469 extern void toggle_window(char id)
471 struct Win * win = get_win_by_id(id);
472 if (0 == win->prev && world.wmeta.chain_start != win) /* Win struct is */
473 { /* outside chain? */
484 extern void toggle_winconfig()
486 struct Win * win = world.wmeta.active;
487 struct WinConf * wcp = get_winconf_by_win(win);
491 win->draw = draw_winconf_geometry;
492 wcp->center = win->center;
496 else if (1 == wcp->view)
499 win->draw = draw_winconf_keybindings;
505 win->draw = get_drawfunc_by_char(wcp->draw);
506 win->center = wcp->center;
512 extern void toggle_win_size_type(char axis)
514 struct Win * win = world.wmeta.active;
515 struct WinConf * wcp = get_winconf_by_win(win);
518 wcp->height_type = (0 == wcp->height_type);
519 set_winconf_geometry(wcp->id);
522 wcp->width_type = ( 0 == wcp->width_type
523 && win->framesize.x <= world.wmeta.padsize.x);
524 set_winconf_geometry(wcp->id);
529 extern void scroll_pad(char dir)
533 reset_pad_offset(world.wmeta.pad_offset + 1);
537 reset_pad_offset(world.wmeta.pad_offset - 1);
543 extern void growshrink_active_window(char change)
545 if (0 != world.wmeta.active)
547 struct yx_uint16 size = world.wmeta.active->framesize;
552 else if (change == '+')
556 else if (change == '_')
560 else if (change == '*')
564 resize_active_win(size);
565 struct WinConf * wcp = get_winconf_by_win(world.wmeta.active);
566 if ( 1 == wcp->width_type
567 && world.wmeta.active->framesize.x > world.wmeta.padsize.x)
571 set_winconf_geometry(wcp->id);