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 <stddef.h> /* NULL */
7 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
8 #include <stdio.h> /* FILE */
9 #include <stdlib.h> /* free(), atoi() */
10 #include <string.h> /* strlen(), strchr(), strstr(), memcpy() */
11 #include "../common/readwrite.h" /* try_fopen(), textfile_sizes(), try_fgets(),
12 * textfile_sizes(), try_fwrite(), try_fgetc(),
13 * try_fclose_unlink_rename(), try_fputc()
15 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
16 #include "../common/try_malloc.h" /* try_malloc() */
17 #include "../common/yx_uint16.h" /* struct yx_uint16 */
18 #include "draw_wins.h" /* draw_win_map(), draw_win_info(), draw_win_log(),
19 * draw_win_available_keybindings(),
20 * draw_win_inventory(), draw_win_keybindings_global(),
21 * draw_win_keybindings_winconf_geometry(),
22 * draw_win_keybindings_winconf_keybindings(),
23 * draw_winconf_geometry(), draw_winconf_keybindings()
25 #include "keybindings.h" /* struct KeyBinding, free_keybindings() */
26 #include "windows.h" /* struct Win, resize_active_win(), reset_pad_offset(),
27 * append_win(), suspend_win(), init_win(), free_win()
29 #include "world.h" /* global world */
33 /* Return string "prefix" + "id"; malloc()'s string, remember to call free()! */
34 static char * string_prefixed_id(char * prefix, char id);
36 /* Initialize Winconf of "id" from appropriate config file.*/
37 static void init_winconf_from_file(char id, struct WinConf * winconf);
39 /* Wrapper around init_win() called with values from Winconf of "id". */
40 static void init_win_from_winconf(char id);
42 /* Save title, draw function, size of window identified by "id" to conffile. */
43 static void save_win_config(char id);
45 /* Free data pointed to inside individual WinConf struct of "id". */
46 static void free_winconf_data(char id);
48 /* Write geometry of a window to its WinConf, as positive or negative values
49 * (dependent on state ofWinConf->height_type / WinConf->width_type).
51 static void set_winconf_geometry(char id);
53 /* Get WinConf by "id"; get id of WinConf mothering "win". */
54 static struct WinConf * get_winconf_by_id(char id);
56 /* Get (Win->draw) function identified by "c"; NULL if c not mapped to one. */
57 static void * get_drawfunc_by_char(char c);
61 static char * string_prefixed_id(char * prefix, char id)
63 uint8_t size = strlen(prefix) + 2;
64 char * path = try_malloc(size, "string_prefixed_id()");
65 sprintf(path, "%s_", prefix);
72 static void init_winconf_from_file(char id, struct WinConf * winconf)
74 /* Assign WinConf id to filename path, error message context, winconf->id.*/
75 char * tmp = "init_winconf_from_file() on window id '_'";
76 char * context = try_malloc(strlen(tmp) + 1, "init_winconf_from_file()");
77 memcpy(context, tmp, strlen(tmp) + 1);
78 context[strlen(tmp) - 2] = id;
79 char * path = string_prefixed_id("confclient/windows/Win_", id);
82 /* Prepare reading in file line by line into "line" array. */
83 FILE * file = try_fopen(path, "r", context);
85 uint32_t linemax = textfile_sizes(file, NULL);
86 char line[linemax + 1];
88 /* Read/determine winconf->title, ->draw, ->height(_type),->width(_type). */
89 try_fgets(line, linemax + 1, file, context);
90 winconf->title = try_malloc(strlen(line), context);
91 memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */
92 winconf->title[strlen(line) - 1] = '\0'; /* char at end of string. */
93 try_fgets(line, linemax + 1, file, context);
94 winconf->draw = line[0];
95 try_fgets(line, linemax + 1, file, context);
96 winconf->height = atoi(line);
97 winconf->height_type = (0 >= winconf->height);
98 try_fgets(line, linemax + 1, file, context);
99 winconf->width = atoi(line);
100 winconf->width_type = (0 >= winconf->width);
102 /* Read in window-specific keybindings (winconf->kb). */
103 char command[linemax + 1];
105 struct KeyBinding ** loc_last_ptr = &winconf->kb.kbs;
107 while (try_fgets(command, linemax + 1, file, context))
109 if ('\n' == command[0] || 0 == command[0])
113 * loc_last_ptr = try_malloc(sizeof(struct KeyBinding), context);
114 struct KeyBinding * kb_p = * loc_last_ptr;
116 kb_p->key = atoi(command);
117 cmdptr = strchr(command, ' ') + 1;
118 cmdptr[strlen(cmdptr) - 1] = '\0';
119 kb_p->command = get_command(cmdptr);
120 loc_last_ptr = & kb_p->next;
123 /* Init remaining values to zero and cleaning up. */
125 winconf->kb.edit = 0;
126 winconf->kb.select = 0;
127 try_fclose(file, context);
133 static void init_win_from_winconf(char id)
135 char * err = "get_drawfunc_by_char() returns NULL to init_win_from_file().";
136 struct WinConf * winconf = get_winconf_by_id(id);
137 void * f = get_drawfunc_by_char(winconf->draw);
138 exit_err(NULL == f, err);
139 init_win(&winconf->win, winconf->title, winconf->height, winconf->width, f);
144 static void save_win_config(char id)
146 char * f_name = "save_win_config()";
148 /* Prepare atomic file saving. */
149 char * path_tmp = string_prefixed_id("confclient/windows/Win_tmp_", id);
150 FILE * file = try_fopen(path_tmp, "w", f_name);
152 /* Save, line by line, ->title, ->draw, ->height and ->width. */
153 struct WinConf * wc = get_winconf_by_id(id);
154 uint8_t size = strlen(wc->title) + 2;
155 if (size < 7) /* Ensure that at least 5 + 2 char fit into line so that */
156 { /* the digit representation of any uint16_t may be stored. */
160 sprintf(line, "%s\n", wc->title);
161 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
162 sprintf(line, "%c\n", wc->draw);
163 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
164 sprintf(line, "%d\n", wc->height);
165 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
166 sprintf(line, "%d\n", wc->width);
167 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
169 /* Save window-specific keybindings (->kb.kbs). */
170 uint16_t linemax = 0;
171 struct KeyBinding * kb_p = wc->kb.kbs;
174 if (strlen(kb_p->command->dsc_short) > linemax)
176 linemax = strlen(kb_p->command->dsc_short);
180 linemax = linemax + 6; /* + 6: + 3 digits + whitespace + \n + \0 */
181 char kb_line[linemax];
185 sprintf(kb_line, "%d %s\n", kb_p->key, kb_p->command->dsc_short);
186 try_fwrite(kb_line, sizeof(char), strlen(kb_line), file, f_name);
190 /* Finish atomic file saving and clean up. */
191 char * path = string_prefixed_id("confclient/windows/Win_", id);
192 try_fclose_unlink_rename(file, path_tmp, path, f_name);
199 static void free_winconf_data(char id)
201 struct WinConf * wc = get_winconf_by_id(id);
203 free_keybindings(wc->kb.kbs);
209 static void set_winconf_geometry(char id)
211 struct WinConf * wcp = get_winconf_by_id(id);
212 if (0 == wcp->height_type)
214 wcp->height = wcp->win->framesize.y;
216 else if (1 == wcp->height_type)
218 wcp->height = wcp->win->framesize.y - world.wmeta.padsize.y + 1;
220 if (0 == wcp->width_type)
222 wcp->width = wcp->win->framesize.x;
224 else if (1 == wcp->width_type)
226 wcp->width = wcp->win->framesize.x - world.wmeta.padsize.x;
232 static struct WinConf * get_winconf_by_id(char id)
237 if (id == world.winconf_db.winconfs[i].id)
239 return &world.winconf_db.winconfs[i];
247 static void * get_drawfunc_by_char(char c)
252 return draw_win_inventory;
256 return draw_win_info;
264 return draw_win_available_keybindings;
272 return draw_win_keybindings_global;
276 return draw_win_keybindings_winconf_geometry;
280 return draw_win_keybindings_winconf_keybindings;
287 extern struct WinConf * get_winconf_by_win(struct Win * win)
292 if (win == world.winconf_db.winconfs[i].win)
294 return &world.winconf_db.winconfs[i];
302 extern struct Win * get_win_by_id(char id)
304 struct WinConf * wc = get_winconf_by_id(id);
310 extern void init_winconfs()
312 char * f_name = "init_winconfs()";
314 /* Fill world.winconf_db.winconf_ids with confclient/windows/Win_*
315 * filenames' end chars.
317 uint8_t max_wins = 255; /* Maximum number of window ids to store. */
318 DIR * dp = opendir("confclient/windows");
319 exit_trouble(NULL == dp, f_name, "opendir()");
322 char * winconf_ids = try_malloc(max_wins + 1, f_name);
325 while (NULL != (fn = readdir(dp)) && i < max_wins)
327 if (5 == strlen(fn->d_name) && fn->d_name == strstr(fn->d_name, "Win_"))
334 winconf_ids[i] = '\0';
335 exit_trouble(errno, f_name, "readdir()");
336 exit_trouble(closedir(dp), f_name, "closedir()");
337 world.winconf_db.winconf_ids = try_malloc(strlen(winconf_ids) + 1, f_name);
338 memcpy(world.winconf_db.winconf_ids, winconf_ids, strlen(winconf_ids) + 1);
341 /* Initialize world.winconf_db.winconfs from Win_* files named in
342 * world.winconf_db.winconf_ids.
344 size_t size = strlen(world.winconf_db.winconf_ids) * sizeof(struct WinConf);
345 world.winconf_db.winconfs = try_malloc(size, f_name);
347 while (0 != (id = get_next_winconf_id()))
349 init_winconf_from_file(id, &world.winconf_db.winconfs[i]);
356 extern void free_winconfs()
359 while (0 != (id = get_next_winconf_id()))
361 free_winconf_data(id);
363 free(world.winconf_db.winconf_ids);
364 free(world.winconf_db.winconfs);
369 extern void init_wins()
372 while (0 != (id = get_next_winconf_id()))
374 init_win_from_winconf(id);
380 extern void sorted_wintoggle_and_activate()
382 char * f_name = "sorted_wintoggle_and_activate()";
384 /* Read from file order of windows to be toggled + active win selection. */
385 char * path = "confclient/windows/toggle_order_and_active";
386 FILE * file = try_fopen(path, "r", f_name);
387 uint32_t linemax = textfile_sizes(file, NULL);
388 char win_order[linemax + 1];
389 try_fgets(win_order, linemax + 1, file, f_name);
390 int char_or_eof = try_fgetc(file, f_name);
391 exit_trouble(EOF==char_or_eof, f_name, "fgetc() unexpectedly hitting EOF");
392 uint8_t a = (uint8_t) char_or_eof;
393 try_fclose(file, f_name);
395 /* Toggle windows and set active window selection. */
397 for (; i < strlen(win_order) - 1; i++)
399 if (NULL == strchr(world.winconf_db.winconf_ids, win_order[i]))
403 toggle_window(win_order[i]);
404 if (a == (uint8_t) win_order[i])
406 world.wmeta.active = get_win_by_id(win_order[i]);
413 extern char get_next_winconf_id()
415 static uint8_t i = 0;
416 char c = world.winconf_db.winconf_ids[i];
428 extern void save_win_configs()
430 char * f_name = "save_win_configs()";
432 /* Save individual world.winconf_db.winconfs to their proper files. */
433 uint8_t max_wins = 255; /* n of WinConf fitting into world.winconf_db */
435 while (0 != (id = get_next_winconf_id()))
440 /* Save order of windows to toggle on start / which to select as active. */
441 char * path = "confclient/windows/toggle_order_and_active";
442 char * path_tmp = "confclient/windows/toggle_order_and_active_tmp";
443 FILE * file = try_fopen(path_tmp, "w", f_name);
444 char line[max_wins + 2];
445 struct Win * w_p = world.wmeta.chain_start;
447 while (0 != w_p && i < max_wins)
449 struct WinConf * wc = get_winconf_by_win(w_p);
456 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
457 if (0 != world.wmeta.active)
459 struct WinConf * wc = get_winconf_by_win(world.wmeta.active);
460 try_fputc(wc->id, file, f_name);
462 try_fclose_unlink_rename(file, path_tmp, path, f_name);
467 extern void toggle_window(char id)
469 struct Win * win = get_win_by_id(id);
470 if (0 == win->prev && world.wmeta.chain_start != win) /* Win struct is */
471 { /* outside chain? */
482 extern void toggle_winconfig()
484 struct Win * win = world.wmeta.active;
485 struct WinConf * wcp = get_winconf_by_win(win);
489 win->draw = draw_winconf_geometry;
490 wcp->center = win->center;
494 else if (1 == wcp->view)
497 win->draw = draw_winconf_keybindings;
503 win->draw = get_drawfunc_by_char(wcp->draw);
504 win->center = wcp->center;
510 extern void toggle_win_size_type(char axis)
512 struct Win * win = world.wmeta.active;
513 struct WinConf * wcp = get_winconf_by_win(win);
516 wcp->height_type = (0 == wcp->height_type);
517 set_winconf_geometry(wcp->id);
520 wcp->width_type = ( 0 == wcp->width_type
521 && win->framesize.x <= world.wmeta.padsize.x);
522 set_winconf_geometry(wcp->id);
527 extern void scroll_pad(char dir)
531 reset_pad_offset(world.wmeta.pad_offset + 1);
535 reset_pad_offset(world.wmeta.pad_offset - 1);
541 extern void growshrink_active_window(char change)
543 if (0 != world.wmeta.active)
545 struct yx_uint16 size = world.wmeta.active->framesize;
550 else if (change == '+')
554 else if (change == '_')
558 else if (change == '*')
562 resize_active_win(size);
563 struct WinConf * wcp = get_winconf_by_win(world.wmeta.active);
564 if ( 1 == wcp->width_type
565 && world.wmeta.active->framesize.x > world.wmeta.padsize.x)
569 set_winconf_geometry(wcp->id);