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 kb_p->command = try_malloc(strlen(cmdptr), context);
121 memcpy(kb_p->command, cmdptr, strlen(cmdptr) - 1);
122 kb_p->command[strlen(cmdptr) - 1] = '\0';
123 loc_last_ptr = & kb_p->next;
126 /* Init remaining values to zero and cleaning up. */
128 winconf->kb.edit = 0;
129 winconf->kb.select = 0;
130 try_fclose(file, context);
136 static void init_win_from_winconf(char id)
138 char * err = "get_drawfunc_by_char() returns NULL to init_win_from_file().";
139 struct WinConf * winconf = get_winconf_by_id(id);
140 void * f = get_drawfunc_by_char(winconf->draw);
141 exit_err(NULL == f, err);
142 init_win(&winconf->win, winconf->title, winconf->height, winconf->width, f);
147 static void save_win_config(char id)
149 char * f_name = "save_win_config()";
151 /* Prepare atomic file saving. */
152 char * path_tmp = string_prefixed_id("confclient/windows/Win_tmp_", id);
153 FILE * file = try_fopen(path_tmp, "w", f_name);
155 /* Save, line by line, ->title, ->draw, ->height and ->width. */
156 struct WinConf * wc = get_winconf_by_id(id);
157 uint8_t size = strlen(wc->title) + 2;
158 if (size < 7) /* Ensure that at least 5 + 2 char fit into line so that */
159 { /* the digit representation of any uint16_t may be stored. */
163 sprintf(line, "%s\n", wc->title);
164 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
165 sprintf(line, "%c\n", wc->draw);
166 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
167 sprintf(line, "%d\n", wc->height);
168 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
169 sprintf(line, "%d\n", wc->width);
170 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
172 /* Save window-specific keybindings (->kb.kbs). */
173 uint16_t linemax = 0;
174 struct KeyBinding * kb_p = wc->kb.kbs;
177 if (strlen(kb_p->command) > linemax)
179 linemax = strlen(kb_p->command);
183 linemax = linemax + 6; /* + 6: + 3 digits + whitespace + \n + \0 */
184 char kb_line[linemax];
188 sprintf(kb_line, "%d %s\n", kb_p->key, kb_p->command);
189 try_fwrite(kb_line, sizeof(char), strlen(kb_line), file, f_name);
193 /* Finish atomic file saving and clean up. */
194 char * path = string_prefixed_id("confclient/windows/Win_", id);
195 try_fclose_unlink_rename(file, path_tmp, path, f_name);
202 static void free_winconf_data(char id)
204 struct WinConf * wc = get_winconf_by_id(id);
206 free_keybindings(wc->kb.kbs);
212 static void set_winconf_geometry(char id)
214 struct WinConf * wcp = get_winconf_by_id(id);
215 if (0 == wcp->height_type)
217 wcp->height = wcp->win->framesize.y;
219 else if (1 == wcp->height_type)
221 wcp->height = wcp->win->framesize.y - world.wmeta.padsize.y + 1;
223 if (0 == wcp->width_type)
225 wcp->width = wcp->win->framesize.x;
227 else if (1 == wcp->width_type)
229 wcp->width = wcp->win->framesize.x - world.wmeta.padsize.x;
235 static struct WinConf * get_winconf_by_id(char id)
240 if (id == world.winconf_db.winconfs[i].id)
242 return &world.winconf_db.winconfs[i];
250 static void * get_drawfunc_by_char(char c)
255 return draw_win_inventory;
259 return draw_win_info;
267 return draw_win_available_keybindings;
275 return draw_win_keybindings_global;
279 return draw_win_keybindings_winconf_geometry;
283 return draw_win_keybindings_winconf_keybindings;
290 static char get_next_winconf_id()
292 static uint8_t i = 0;
293 char c = world.winconf_db.winconf_ids[i];
305 extern struct WinConf * get_winconf_by_win(struct Win * win)
310 if (win == world.winconf_db.winconfs[i].win)
312 return &world.winconf_db.winconfs[i];
320 extern struct Win * get_win_by_id(char id)
322 struct WinConf * wc = get_winconf_by_id(id);
328 extern void init_winconfs()
330 char * f_name = "init_winconfs()";
332 /* Fill world.winconf_db.winconf_ids with confclient/windows/Win_*
333 * filenames' end chars.
335 uint8_t max_wins = 255; /* Maximum number of window ids to store. */
336 DIR * dp = opendir("confclient/windows");
337 exit_trouble(NULL == dp, f_name, "opendir()");
340 char * winconf_ids = try_malloc(max_wins + 1, f_name);
343 while (NULL != (fn = readdir(dp)) && i < max_wins)
345 if (5 == strlen(fn->d_name) && fn->d_name == strstr(fn->d_name, "Win_"))
352 winconf_ids[i] = '\0';
353 exit_trouble(errno, f_name, "readdir()");
354 exit_trouble(closedir(dp), f_name, "closedir()");
355 world.winconf_db.winconf_ids = try_malloc(strlen(winconf_ids) + 1, f_name);
356 memcpy(world.winconf_db.winconf_ids, winconf_ids, strlen(winconf_ids) + 1);
359 /* Initialize world.winconf_db.winconfs from Win_* files named in
360 * world.winconf_db.winconf_ids.
362 size_t size = strlen(world.winconf_db.winconf_ids) * sizeof(struct WinConf);
363 world.winconf_db.winconfs = try_malloc(size, f_name);
365 while (0 != (id = get_next_winconf_id()))
367 init_winconf_from_file(id, &world.winconf_db.winconfs[i]);
374 extern void free_winconfs()
377 while (0 != (id = get_next_winconf_id()))
379 free_winconf_data(id);
381 free(world.winconf_db.winconf_ids);
382 free(world.winconf_db.winconfs);
387 extern void init_wins()
390 while (0 != (id = get_next_winconf_id()))
392 init_win_from_winconf(id);
398 extern void sorted_wintoggle_and_activate()
400 char * f_name = "sorted_wintoggle_and_activate()";
402 /* Read from file order of windows to be toggled + active win selection. */
403 char * path = "confclient/windows/toggle_order_and_active";
404 FILE * file = try_fopen(path, "r", f_name);
405 uint32_t linemax = textfile_sizes(file, NULL);
406 char win_order[linemax + 1];
407 try_fgets(win_order, linemax + 1, file, f_name);
408 int char_or_eof = try_fgetc(file, f_name);
409 exit_trouble(EOF==char_or_eof, f_name, "fgetc() unexpectedly hitting EOF");
410 uint8_t a = (uint8_t) char_or_eof;
411 try_fclose(file, f_name);
413 /* Toggle windows and set active window selection. */
415 for (; i < strlen(win_order) - 1; i++)
417 if (NULL == strchr(world.winconf_db.winconf_ids, win_order[i]))
421 toggle_window(win_order[i]);
422 if (a == (uint8_t) win_order[i])
424 world.wmeta.active = get_win_by_id(win_order[i]);
431 extern void save_win_configs()
433 char * f_name = "save_win_configs()";
435 /* Save individual world.winconf_db.winconfs to their proper files. */
436 uint8_t max_wins = 255; /* n of WinConf fitting into world.winconf_db */
438 while (0 != (id = get_next_winconf_id()))
443 /* Save order of windows to toggle on start / which to select as active. */
444 char * path = "confclient/windows/toggle_order_and_active";
445 char * path_tmp = "confclient/windows/toggle_order_and_active_tmp";
446 FILE * file = try_fopen(path_tmp, "w", f_name);
447 char line[max_wins + 2];
448 struct Win * w_p = world.wmeta.chain_start;
450 while (0 != w_p && i < max_wins)
452 struct WinConf * wc = get_winconf_by_win(w_p);
459 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
460 if (0 != world.wmeta.active)
462 struct WinConf * wc = get_winconf_by_win(world.wmeta.active);
463 try_fputc(wc->id, file, f_name);
465 try_fclose_unlink_rename(file, path_tmp, path, f_name);
470 extern void toggle_window(char id)
472 struct Win * win = get_win_by_id(id);
473 if (0 == win->prev && world.wmeta.chain_start != win) /* Win struct is */
474 { /* outside chain? */
485 extern void toggle_winconfig()
487 struct Win * win = world.wmeta.active;
488 struct WinConf * wcp = get_winconf_by_win(win);
492 win->draw = draw_winconf_geometry;
493 wcp->center = win->center;
497 else if (1 == wcp->view)
500 win->draw = draw_winconf_keybindings;
506 win->draw = get_drawfunc_by_char(wcp->draw);
507 win->center = wcp->center;
513 extern void toggle_win_size_type(char axis)
515 struct Win * win = world.wmeta.active;
516 struct WinConf * wcp = get_winconf_by_win(win);
519 wcp->height_type = (0 == wcp->height_type);
520 set_winconf_geometry(wcp->id);
523 wcp->width_type = ( 0 == wcp->width_type
524 && win->framesize.x <= world.wmeta.padsize.x);
525 set_winconf_geometry(wcp->id);
530 extern void scroll_pad(char dir)
534 reset_pad_offset(world.wmeta.pad_offset + 1);
538 reset_pad_offset(world.wmeta.pad_offset - 1);
544 extern void growshrink_active_window(char change)
546 if (0 != world.wmeta.active)
548 struct yx_uint16 size = world.wmeta.active->framesize;
553 else if (change == '+')
557 else if (change == '_')
561 else if (change == '*')
565 resize_active_win(size);
566 struct WinConf * wcp = get_winconf_by_win(world.wmeta.active);
567 if ( 1 == wcp->width_type
568 && world.wmeta.active->framesize.x > world.wmeta.padsize.x)
572 set_winconf_geometry(wcp->id);