3 #include "wincontrol.h"
4 #include <stdlib.h> /* for free() */
5 #include <string.h> /* for strlen(), strchr(), strstr() */
6 #include <stdint.h> /* for uint8_t, uint16_t */
7 #include "windows.h" /* for suspend_win(), append_win(), reset_pad_offset(),
8 * resize_active_win(), init_win(), free_win(), struct Win
10 #include "yx_uint16.h" /* for yx_uint16 struct */
11 #include "main.h" /* for world global */
12 #include "readwrite.h" /* for get_linemax(), try_fopen(), try_fclose(),
13 * try_fgets(), try_fclose_unlink_rename(), try_fwrite()
15 #include "rexit.h" /* for exit_err(), exit_trouble() */
16 #include "draw_wins.h" /* for draw_win_map(), draw_win_info(), draw_win_log(),
17 * draw_win_available_keybindings(),
18 * draw_win_inventory(), draw_win_keybindings_global(),
19 * draw_win_keybindings_winconf_geometry(),
20 * draw_win_keybindings_winconf_keybindings(),
21 * draw_winconf_geometry(), draw_winconf_keybindings()
23 #include "misc.h" /* for try_malloc() */
24 #include "dirent.h" /* for opendir(), closedir(), readdir() */
25 #include "errno.h" /* for errno */
26 #include "keybindings.h" /* for KeyBinding struct, free_keybindings() */
30 /* Return string "prefix" + "id"; malloc()'s string, remember to call free()! */
31 static char * string_prefixed_id(char * prefix, char id);
33 /* Initialize Winconf of "id" from appropriate config file.*/
34 static void init_winconf_from_file(char id, struct WinConf * winconf);
36 /* Wrapper around init_win() called with values from Winconf of "id". */
37 static void init_win_from_winconf(char id);
39 /* Save title, draw function, size of window identified by "id" to conffile. */
40 static void save_win_config(char id);
42 /* Free data pointed to inside individual WinConf struct of "id". */
43 static void free_winconf_data(char id);
45 /* Write geometry of a window to its WinConf, as positive or negative values
46 * (dependent on state ofWinConf->height_type / WinConf->width_type).
48 static void set_winconf_geometry(char id);
50 /* Get WinConf by "id"; get id of WinConf mothering "win". */
51 static struct WinConf * get_winconf_by_id(char id);
53 /* Get (Win->draw) function identified by "c"; NULL if c not mapped to one. */
54 static void * get_drawfunc_by_char(char c);
56 /* Iterate over chars of world.winconf_ids array. Re-start after null byte. */
57 static char get_next_winconf_id();
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("config/windows/Win_", id);
82 /* Prepare reading in file line by line into "line" array. */
83 FILE * file = try_fopen(path, "r", context);
85 uint16_t linemax = get_linemax(file, context);
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 (fgets(command, linemax + 1, file))
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 kb_p->name = try_malloc(strlen(cmdptr), context);
119 memcpy(kb_p->name, cmdptr, strlen(cmdptr) - 1);
120 kb_p->name[strlen(cmdptr) - 1] = '\0';
121 loc_last_ptr = & kb_p->next;
124 /* Init remaining values to zero and cleaning up. */
126 winconf->kb.edit = 0;
127 winconf->kb.select = 0;
128 try_fclose(file, context);
134 static void init_win_from_winconf(char id)
136 char * err = "get_drawfunc_by_char() returns NULL to init_win_from_file().";
137 struct WinConf * winconf = get_winconf_by_id(id);
138 void * f = get_drawfunc_by_char(winconf->draw);
139 exit_err(NULL == f, err);
140 init_win(&winconf->win, winconf->title, winconf->height, winconf->width, f);
145 static void save_win_config(char id)
147 char * f_name = "save_win_config()";
149 /* Prepare atomic file saving. */
150 char * path_tmp = string_prefixed_id("config/windows/Win_tmp_", id);
151 FILE * file = try_fopen(path_tmp, "w", f_name);
153 /* Save, line by line, ->title, ->draw, ->height and ->width. */
154 struct WinConf * wc = get_winconf_by_id(id);
155 uint8_t size = strlen(wc->title) + 2;
156 if (size < 7) /* Ensure that at least 5 + 2 char fit into line so that */
157 { /* the digit representation of any uint16_t may be stored. */
161 sprintf(line, "%s\n", wc->title);
162 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
163 sprintf(line, "%c\n", wc->draw);
164 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
165 sprintf(line, "%d\n", wc->height);
166 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
167 sprintf(line, "%d\n", wc->width);
168 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
170 /* Save window-specific keybindings (->kb.kbs). */
171 uint16_t linemax = 0;
172 struct KeyBinding * kb_p = wc->kb.kbs;
175 if (strlen(kb_p->name) > linemax)
177 linemax = strlen(kb_p->name);
181 linemax = linemax + 6; /* + 6: + 3 digits + whitespace + \n + \0 */
182 char kb_line[linemax];
186 sprintf(kb_line, "%d %s\n", kb_p->key, kb_p->name);
187 try_fwrite(kb_line, sizeof(char), strlen(kb_line), file, f_name);
191 /* Finish atomic file saving and clean up. */
192 char * path = string_prefixed_id("config/windows/Win_", id);
193 try_fclose_unlink_rename(file, path_tmp, path, f_name);
200 static void free_winconf_data(char id)
202 struct WinConf * wc = get_winconf_by_id(id);
204 free_keybindings(wc->kb.kbs);
210 static void set_winconf_geometry(char id)
212 struct WinConf * wcp = get_winconf_by_id(id);
213 if (0 == wcp->height_type)
215 wcp->height = wcp->win->framesize.y;
217 else if (1 == wcp->height_type)
219 wcp->height = wcp->win->framesize.y - world.wmeta->padsize.y + 1;
221 if (0 == wcp->width_type)
223 wcp->width = wcp->win->framesize.x;
225 else if (1 == wcp->width_type)
227 wcp->width = wcp->win->framesize.x - world.wmeta->padsize.x;
233 static struct WinConf * get_winconf_by_id(char id)
238 if (id == world.winconfs[i].id)
240 return &world.winconfs[i];
248 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 static char get_next_winconf_id()
289 static uint8_t i = 0;
290 char c = world.winconf_ids[i];
302 extern struct WinConf * get_winconf_by_win(struct Win * win)
307 if (win == world.winconfs[i].win)
309 return &world.winconfs[i];
317 extern struct Win * get_win_by_id(char id)
319 struct WinConf * wc = get_winconf_by_id(id);
325 extern void init_winconfs()
327 char * f_name = "init_winconfs()";
329 /* Fill world.winconf_ids with config/windows/Win_* filenames' end chars. */
330 uint8_t max_wins = 255; /* Maximum number of window ids to store. */
331 DIR * dp = opendir("config/windows");
332 exit_trouble(NULL == dp, f_name, "opendir()");
335 char * winconf_ids = try_malloc(max_wins + 1, f_name);
338 while (NULL != (fn = readdir(dp)) && i < max_wins)
340 if (5 == strlen(fn->d_name) && fn->d_name == strstr(fn->d_name, "Win_"))
347 winconf_ids[i] = '\0';
348 exit_trouble(errno, f_name, "readdir()");
349 exit_trouble(closedir(dp), f_name, "closedir()");
350 world.winconf_ids = try_malloc(strlen(winconf_ids) + 1, f_name);
351 memcpy(world.winconf_ids, winconf_ids, strlen(winconf_ids) + 1);
354 /* Initialize world.winconfs from Win_* files named in world.winconf_ids. */
355 size_t size = strlen(world.winconf_ids) * sizeof(struct WinConf);
356 world.winconfs = try_malloc(size, f_name);
358 while (0 != (id = get_next_winconf_id()))
360 init_winconf_from_file(id, &world.winconfs[i]);
367 extern void free_winconfs()
370 while (0 != (id = get_next_winconf_id()))
372 free_winconf_data(id);
374 free(world.winconf_ids);
375 free(world.winconfs);
380 extern void init_wins()
383 while (0 != (id = get_next_winconf_id()))
385 init_win_from_winconf(id);
391 extern void sorted_wintoggle_and_activate()
393 char * f_name = "sorted_wintoggle_and_activate()";
395 /* Read from file order of windows to be toggled + active win selection. */
396 char * path = "config/windows/toggle_order_and_active";
397 FILE * file = try_fopen(path, "r", f_name);
398 uint16_t linemax = get_linemax(file, f_name);
399 char win_order[linemax + 1];
400 try_fgets(win_order, linemax + 1, file, f_name);
402 exit_trouble(read_uint8(file, &a), f_name, "read_uint8()");
403 try_fclose(file, f_name);
405 /* Toggle windows and set active window selection. */
407 for (; i < strlen(win_order) - 1; i++)
409 if (NULL == strchr(world.winconf_ids, win_order[i]))
413 toggle_window(win_order[i]);
414 if (a == (uint8_t) win_order[i])
416 world.wmeta->active = get_win_by_id(win_order[i]);
423 extern void save_win_configs()
425 char * f_name = "save_win_configs()";
427 /* Save individual world.winconfs to their proper files. */
428 uint8_t max_wins = 255; /* So many winconf ids fit into world.winconf_ids.*/
430 while (0 != (id = get_next_winconf_id()))
435 /* Save order of windows to toggle on start / which to select as active. */
436 char * path = "config/windows/toggle_order_and_active";
437 char * path_tmp = "config/windows/toggle_order_and_active_tmp";
438 FILE * file = try_fopen(path_tmp, "w", f_name);
439 char line[max_wins + 2];
440 struct Win * w_p = world.wmeta->chain_start;
442 while (0 != w_p && i < max_wins)
444 struct WinConf * wc = get_winconf_by_win(w_p);
451 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
452 if (0 != world.wmeta->active)
454 struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
455 write_uint8(wc->id, file);
457 try_fclose_unlink_rename(file, path_tmp, path, f_name);
462 extern void toggle_window(char id)
464 struct Win * win = get_win_by_id(id);
465 if (0 == win->prev && world.wmeta->chain_start != win) /* Win struct is */
466 { /* outside chain? */
477 extern void toggle_winconfig()
479 struct Win * win = world.wmeta->active;
480 struct WinConf * wcp = get_winconf_by_win(win);
484 win->draw = draw_winconf_geometry;
485 wcp->center = win->center;
489 else if (1 == wcp->view)
492 win->draw = draw_winconf_keybindings;
498 win->draw = get_drawfunc_by_char(wcp->draw);
499 win->center = wcp->center;
505 extern void toggle_win_size_type(char axis)
507 struct Win * win = world.wmeta->active;
508 struct WinConf * wcp = get_winconf_by_win(win);
511 wcp->height_type = (0 == wcp->height_type);
512 set_winconf_geometry(wcp->id);
515 wcp->width_type = ( 0 == wcp->width_type
516 && win->framesize.x <= world.wmeta->padsize.x);
517 set_winconf_geometry(wcp->id);
522 extern void scroll_pad(char dir)
526 reset_pad_offset(world.wmeta->pad_offset + 1);
530 reset_pad_offset(world.wmeta->pad_offset - 1);
536 extern void growshrink_active_window(char change)
538 if (0 != world.wmeta->active)
540 struct yx_uint16 size = world.wmeta->active->framesize;
545 else if (change == '+')
549 else if (change == '_')
553 else if (change == '*')
557 resize_active_win(size);
558 struct WinConf * wcp = get_winconf_by_win(world.wmeta->active);
559 if ( 1 == wcp->width_type
560 && world.wmeta->active->framesize.x > world.wmeta->padsize.x)
564 set_winconf_geometry(wcp->id);