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() */
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(), exit_trouble() */
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 /* Initializes Winconf: .view/.height_type/.width_type to 0, .id to "id". */
34 static void create_winconf(char id, struct WinConf * wcp);
36 /* Initialize Winconf of "id" from appropriate config file.*/
37 static void init_winconf_from_file(char id);
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);
59 /* Iterate over chars of world.winconf_ids array. Re-start after null byte. */
60 static char get_next_winconf_id();
64 static char * string_prefixed_id(char * prefix, char id)
66 uint8_t size = strlen(prefix) + 2;
67 char * path = try_malloc(size, "string_prefixed_id()");
68 sprintf(path, "%s_", prefix);
75 static void create_winconf(char id, struct WinConf * wcp)
87 static void init_winconf_from_file(char id)
89 char * tmp = "init_winconf_from_file() on window id '_'";
90 char * context = try_malloc(strlen(tmp) + 1, "init_winconf_from_file()");
91 memcpy(context, tmp, strlen(tmp) + 1);
92 context[strlen(tmp) - 2] = id;
94 char * path = string_prefixed_id("config/windows/Win_", id);
95 FILE * file = try_fopen(path, "r", context);
97 uint16_t linemax = get_linemax(file, context);
98 char line[linemax + 1];
100 struct WinConf * winconf = get_winconf_by_id(id);
101 try_fgets(line, linemax + 1, file, context);
102 winconf->title = try_malloc(strlen(line), context);
103 memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */
104 winconf->title[strlen(line) - 1] = '\0'; /* char at end of string. */
106 try_fgets(line, linemax + 1, file, context);
107 winconf->draw = line[0];
109 try_fgets(line, linemax + 1, file, context);
110 winconf->height = atoi(line);
111 if (0 >= winconf->height)
113 winconf->height_type = 1;
115 try_fgets(line, linemax + 1, file, context);
116 winconf->width = atoi(line);
117 if (0 >= winconf->width)
119 winconf->width_type = 1;
122 char command[linemax + 1];
124 struct KeyBinding ** loc_last_ptr = &winconf->kb.kbs;
126 while (fgets(command, linemax + 1, file))
128 if ('\n' == command[0] || 0 == command[0])
132 * loc_last_ptr = try_malloc(sizeof(struct KeyBinding), context);
133 struct KeyBinding * kb_p = * loc_last_ptr;
135 kb_p->key = atoi(command);
136 cmdptr = strchr(command, ' ') + 1;
137 kb_p->name = try_malloc(strlen(cmdptr), context);
138 memcpy(kb_p->name, cmdptr, strlen(cmdptr) - 1);
139 kb_p->name[strlen(cmdptr) - 1] = '\0';
140 loc_last_ptr = & kb_p->next;
143 try_fclose(file, context);
149 static void init_win_from_winconf(char id)
151 char * err = "get_drawfunc_by_char() returns NULL to init_win_from_file().";
152 struct WinConf * winconf = get_winconf_by_id(id);
153 void * f = get_drawfunc_by_char(winconf->draw);
154 exit_err(NULL == f, err);
155 init_win(&winconf->win, winconf->title, winconf->height, winconf->width, f);
160 static void save_win_config(char id)
162 char * f_name = "save_win_config()";
164 char * path_tmp = string_prefixed_id("config/windows/Win_tmp_", id);
165 FILE * file = try_fopen(path_tmp, "w", f_name);
167 struct WinConf * wc = get_winconf_by_id(id);
168 uint8_t size = strlen(wc->title) + 2;
174 sprintf(line, "%s\n", wc->title);
175 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
176 sprintf(line, "%c\n", wc->draw);
177 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
178 sprintf(line, "%d\n", wc->height);
179 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
180 sprintf(line, "%d\n", wc->width);
181 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
183 uint16_t linemax = 0;
184 struct KeyBinding * kb_p = wc->kb.kbs;
187 if (strlen(kb_p->name) > linemax)
189 linemax = strlen(kb_p->name);
193 linemax = linemax + 6; /* + 6 = + 3 digits + whitespace + \n + \0 */
195 char kb_line[linemax];
199 snprintf(kb_line, linemax, "%d %s\n", kb_p->key, kb_p->name);
200 try_fwrite(kb_line, sizeof(char), strlen(kb_line), file, f_name);
204 char * path = string_prefixed_id("config/windows/Win_", id);
205 try_fclose_unlink_rename(file, path_tmp, path, f_name);
212 static void free_winconf_data(char id)
214 struct WinConf * wc = get_winconf_by_id(id);
216 free_keybindings(wc->kb.kbs);
222 static void set_winconf_geometry(char id)
224 struct WinConf * wcp = get_winconf_by_id(id);
225 if (0 == wcp->height_type)
227 wcp->height = wcp->win->framesize.y;
229 else if (1 == wcp->height_type)
231 wcp->height = wcp->win->framesize.y - world.wmeta->padsize.y + 1;
233 if (0 == wcp->width_type)
235 wcp->width = wcp->win->framesize.x;
237 else if (1 == wcp->width_type)
239 wcp->width = wcp->win->framesize.x - world.wmeta->padsize.x;
245 static struct WinConf * get_winconf_by_id(char id)
250 if (id == world.winconfs[i].id)
252 return &world.winconfs[i];
260 static void * get_drawfunc_by_char(char c)
264 return draw_win_inventory;
268 return draw_win_info;
276 return draw_win_available_keybindings;
284 return draw_win_keybindings_global;
288 return draw_win_keybindings_winconf_geometry;
292 return draw_win_keybindings_winconf_keybindings;
299 static char get_next_winconf_id()
301 static uint8_t i = 0;
302 char c = world.winconf_ids[i];
314 extern struct WinConf * get_winconf_by_win(struct Win * win)
319 if (win == world.winconfs[i].win)
321 return &world.winconfs[i];
329 extern struct Win * get_win_by_id(char id)
331 struct WinConf * wc = get_winconf_by_id(id);
337 extern void init_winconfs()
339 char * f_name = "init_winconfs()";
341 DIR * dp = opendir("config/windows");
342 exit_trouble(NULL == dp, f_name, "opendir()");
345 char * winconf_ids = try_malloc(256, f_name);
348 while (NULL != (fn = readdir(dp)))
350 if (5 == strlen(fn->d_name) && fn->d_name == strstr(fn->d_name, "Win_"))
357 winconf_ids[i] = '\0';
358 exit_trouble(errno, f_name, "readdir()");
359 exit_trouble(closedir(dp), f_name, "closedir()");
360 world.winconf_ids = try_malloc(strlen(winconf_ids) + 1, f_name);
361 memcpy(world.winconf_ids, winconf_ids, strlen(winconf_ids) + 1);
364 struct WinConf * winconfs;
365 winconfs = try_malloc(strlen(world.winconf_ids) * sizeof(struct WinConf),
368 while (0 != (id = get_next_winconf_id()))
370 create_winconf(id, &winconfs[i]);
373 world.winconfs = winconfs;
374 while (0 != (id = get_next_winconf_id()))
376 init_winconf_from_file(id);
383 extern void free_winconfs()
386 while (0 != (id = get_next_winconf_id()))
388 free_winconf_data(id);
390 free(world.winconf_ids);
391 free(world.winconfs);
396 extern void init_wins()
399 while (0 != (id = get_next_winconf_id()))
401 init_win_from_winconf(id);
407 extern void sorted_wintoggle_and_activate()
409 char * f_name = "sorted_wintoggle_and_activate()";
411 char * path = "config/windows/toggle_order_and_active";
412 FILE * file = try_fopen(path, "r", f_name);
413 uint16_t linemax = get_linemax(file, f_name);
415 char win_order[linemax + 1];
416 try_fgets(win_order, linemax + 1, file, f_name);
419 exit_trouble(read_uint8(file, &a), f_name, "read_uint8()");
421 try_fclose(file, f_name);
424 for (; i < linemax - 1; i++)
426 if (NULL == strchr(world.winconf_ids, win_order[i]))
430 toggle_window(win_order[i]);
432 if (a == (uint8_t) win_order[i])
434 world.wmeta->active = get_win_by_id(win_order[i]);
441 extern void save_win_configs()
443 char * f_name = "save_win_configs()";
446 while (0 != (id = get_next_winconf_id()))
451 char * path = "config/windows/toggle_order_and_active";
452 char * path_tmp = "config/windows/toggle_order_and_active_tmp";
453 FILE * file = try_fopen(path_tmp, "w", f_name);
456 struct Win * w_p = world.wmeta->chain_start;
460 struct WinConf * wc = get_winconf_by_win(w_p);
466 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
467 if (0 != world.wmeta->active)
469 struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
470 write_uint8(wc->id, file);
473 try_fclose_unlink_rename(file, path_tmp, path, f_name);
478 extern void toggle_window(char id)
480 struct Win * win = get_win_by_id(id);
481 if (0 == win->prev && world.wmeta->chain_start != win) /* Win struct is */
482 { /* outside chain. */
493 extern void toggle_winconfig()
495 struct Win * win = world.wmeta->active;
496 struct WinConf * wcp = get_winconf_by_win(win);
499 win->draw = draw_winconf_geometry;
501 wcp->center = win->center;
505 else if (1 == wcp->view)
507 win->draw = draw_winconf_keybindings;
513 win->draw = get_drawfunc_by_char(wcp->draw);
514 win->center = wcp->center;
521 extern void toggle_win_height_type()
523 struct Win * win = world.wmeta->active;
524 struct WinConf * wcp = get_winconf_by_win(win);
525 if (0 == wcp->height_type)
527 wcp->height_type = 1;
531 wcp->height_type = 0;
533 set_winconf_geometry(wcp->id);
538 extern void toggle_win_width_type()
540 struct Win * win = world.wmeta->active;
541 struct WinConf * wcp = get_winconf_by_win(win);
542 if (0 == wcp->width_type && win->framesize.x <= world.wmeta->padsize.x)
550 set_winconf_geometry(wcp->id);
555 extern void scroll_pad(char dir)
559 reset_pad_offset(world.wmeta->pad_offset + 1);
563 reset_pad_offset(world.wmeta->pad_offset - 1);
569 extern void growshrink_active_window(char change)
571 if (0 != world.wmeta->active)
573 struct yx_uint16 size = world.wmeta->active->framesize;
578 else if (change == '+')
582 else if (change == '_')
586 else if (change == '*')
590 resize_active_win(size);
591 struct WinConf * wcp = get_winconf_by_win(world.wmeta->active);
592 if ( 1 == wcp->width_type
593 && world.wmeta->active->framesize.x > world.wmeta->padsize.x)
597 set_winconf_geometry(wcp->id);