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 <unistd.h> /* for access(), unlink() */
8 #include "windows.h" /* for suspend_win(), append_win(), reset_pad_offset(),
9 * resize_active_win(), init_win(), free_win(),
10 * structs Win, WinMeta
12 #include "yx_uint16.h" /* for yx_uint16 struct */
13 #include "main.h" /* for world global */
14 #include "readwrite.h" /* for get_linemax(), try_fopen(), try_fclose(),
15 * try_fgets(), try_fclose_unlink_rename(), try_fwrite()
17 #include "rexit.h" /* for exit_err() */
18 #include "main.h" /* for world global */
19 #include "draw_wins.h" /* for draw_win_map(), draw_win_info(), draw_win_og(),
20 * draw_win_available_keybindings(),
21 * draw_win_keybindings_global(), draw_win_inventory(),
22 * draw_win_keybindings_winconf_geometry(),
23 * draw_win_keybindings_winconf_keybindings(),
24 * draw_winconf_geometry(), draw_winconf_keybindings()
26 #include "misc.h" /* for try_malloc(), trouble_msg() */
27 #include "dirent.h" /* for opendir(), closedir(), readdir() */
28 #include "errno.h" /* for errno */
29 #include "keybindings.h" /* for KeyBinding struct, free_keybindings() */
33 /* Return string "prefix" + "id"; malloc()'s string, remember to call free()! */
34 static char * string_prefixed_id(char * prefix, char id);
38 /* Create Winconf, init ->view/height_type/width_type to 0, ->id to "id". */
39 static void create_winconf(char id, struct WinConf * wcp);
41 /* Initialize Winconf of "id" from appropriate config file.*/
42 static void init_winconf_from_file(char id);
44 /* Wrapper around init_win() called with values from Winconf of "id". */
45 static void init_win_from_winconf(char id);
47 /* Save title, draw function, size of window identified by "id" to conffile. */
48 static void save_win_config(char id);
50 /* Free data pointed to inside WinConf struct. */
51 static void free_winconf_data(char id);
55 /* Write geometry of a window to its WinConf, as positive or negative values
56 * (dependent on state ofWinConf->height_type / WinConf->width_type).
58 static void set_winconf_geometry(char id);
62 /* Get WinConf by "id"; get id of WinConf mothering "win". */
63 static struct WinConf * get_winconf_by_id(char id);
65 /* Get (Win->draw) function identified by "c"; NULL if c not mapped to one. */
66 static void * get_drawfunc_by_char(char c);
68 /* Iterate over bytes of world.winconf_ids array. Re-start after null byte. */
69 static char get_next_winconf_id();
73 static char * string_prefixed_id(char * prefix, char id)
75 uint8_t size = strlen(prefix) + 2;
76 char * path = try_malloc(size, "string_prefixed_id()");
77 sprintf(path, "%s_", prefix);
84 static void create_winconf(char id, struct WinConf * wcp)
96 static void init_winconf_from_file(char id)
98 char * tmp = "init_winconf_from_file() on window id '_'";
99 char * context = try_malloc(strlen(tmp) + 1, "init_winconf_from_file()");
100 memcpy(context, tmp, strlen(tmp) + 1);
101 context[strlen(tmp) - 2] = id;
103 char * path = string_prefixed_id("config/windows/Win_", id);
104 FILE * file = try_fopen(path, "r", context);
106 uint16_t linemax = get_linemax(file, context);
107 char line[linemax + 1];
109 struct WinConf * winconf = get_winconf_by_id(id);
110 try_fgets(line, linemax + 1, file, context);
111 winconf->title = try_malloc(strlen(line), context);
112 memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */
113 winconf->title[strlen(line) - 1] = '\0'; /* char at end of string. */
115 try_fgets(line, linemax + 1, file, context);
116 winconf->draw = line[0];
118 try_fgets(line, linemax + 1, file, context);
119 winconf->height = atoi(line);
120 if (0 >= winconf->height)
122 winconf->height_type = 1;
124 try_fgets(line, linemax + 1, file, context);
125 winconf->width = atoi(line);
126 if (0 >= winconf->width)
128 winconf->width_type = 1;
131 char command[linemax + 1];
133 struct KeyBinding ** loc_last_ptr = &winconf->kb.kbs;
135 while (fgets(command, linemax + 1, file))
137 if ('\n' == command[0] || 0 == command[0])
141 * loc_last_ptr = try_malloc(sizeof(struct KeyBinding), context);
142 struct KeyBinding * kb_p = * loc_last_ptr;
144 kb_p->key = atoi(command);
145 cmdptr = strchr(command, ' ') + 1;
146 kb_p->name = try_malloc(strlen(cmdptr), context);
147 memcpy(kb_p->name, cmdptr, strlen(cmdptr) - 1);
148 kb_p->name[strlen(cmdptr) - 1] = '\0';
149 loc_last_ptr = & kb_p->next;
152 try_fclose(file, context);
158 static void init_win_from_winconf(char id)
160 char * err = "get_drawfunc_by_char() returns NULL to init_win_from_file().";
161 struct WinConf * winconf = get_winconf_by_id(id);
162 void * f = get_drawfunc_by_char(winconf->draw);
163 exit_err(NULL == f, err);
164 init_win(&winconf->win, winconf->title, winconf->height, winconf->width, f);
169 static void save_win_config(char id)
171 char * f_name = "save_win_config()";
173 char * path_tmp = string_prefixed_id("config/windows/Win_tmp_", id);
174 FILE * file = try_fopen(path_tmp, "w", f_name);
176 struct WinConf * wc = get_winconf_by_id(id);
177 uint8_t size = strlen(wc->title) + 2;
183 sprintf(line, "%s\n", wc->title);
184 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
185 sprintf(line, "%c\n", wc->draw);
186 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
187 sprintf(line, "%d\n", wc->height);
188 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
189 sprintf(line, "%d\n", wc->width);
190 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
192 uint16_t linemax = 0;
193 struct KeyBinding * kb_p = wc->kb.kbs;
196 if (strlen(kb_p->name) > linemax)
198 linemax = strlen(kb_p->name);
202 linemax = linemax + 6; /* + 6 = + 3 digits + whitespace + \n + \0 */
204 char kb_line[linemax];
208 snprintf(kb_line, linemax, "%d %s\n", kb_p->key, kb_p->name);
209 try_fwrite(kb_line, sizeof(char), strlen(kb_line), file, f_name);
213 char * path = string_prefixed_id("config/windows/Win_", id);
214 try_fclose_unlink_rename(file, path_tmp, path, f_name);
221 static void free_winconf_data(char id)
223 struct WinConf * wc = get_winconf_by_id(id);
225 free_keybindings(wc->kb.kbs);
231 static void set_winconf_geometry(char id)
233 struct WinConf * wcp = get_winconf_by_id(id);
234 if (0 == wcp->height_type)
236 wcp->height = wcp->win->framesize.y;
238 else if (1 == wcp->height_type)
240 wcp->height = wcp->win->framesize.y - world.wmeta->padsize.y + 1;
242 if (0 == wcp->width_type)
244 wcp->width = wcp->win->framesize.x;
246 else if (1 == wcp->width_type)
248 wcp->width = wcp->win->framesize.x - world.wmeta->padsize.x;
254 static struct WinConf * get_winconf_by_id(char id)
259 if (id == world.winconfs[i].id)
261 return &world.winconfs[i];
269 static void * get_drawfunc_by_char(char c)
273 return draw_win_inventory;
277 return draw_win_info;
285 return draw_win_available_keybindings;
293 return draw_win_keybindings_global;
297 return draw_win_keybindings_winconf_geometry;
301 return draw_win_keybindings_winconf_keybindings;
308 static char get_next_winconf_id()
310 static uint8_t i = 0;
311 char c = world.winconf_ids[i];
325 extern struct WinConf * get_winconf_by_win(struct Win * win)
330 if (win == world.winconfs[i].win)
332 return &world.winconfs[i];
340 extern struct Win * get_win_by_id(char id)
342 struct WinConf * wc = get_winconf_by_id(id);
348 extern void init_winconfs()
350 char * f_name = "init_winconfs()";
351 char * err_o = "Trouble in init_winconfs() with opendir().";
352 char * err_r = "Trouble in init_winconfs() with readdir().";
353 char * err_c = "Trouble in init_winconfs() with closedir().";
355 DIR * dp = opendir("config/windows");
356 exit_err(NULL == dp, err_o);
359 char * winconf_ids = try_malloc(256, f_name);
362 while (NULL != (fn = readdir(dp)))
364 if (5 == strlen(fn->d_name) && fn->d_name == strstr(fn->d_name, "Win_"))
371 winconf_ids[i] = '\0';
372 exit_err(errno, err_r);
373 exit_err(closedir(dp), err_c);
374 world.winconf_ids = try_malloc(strlen(winconf_ids) + 1, f_name);
375 memcpy(world.winconf_ids, winconf_ids, strlen(winconf_ids) + 1);
378 struct WinConf * winconfs;
379 winconfs = try_malloc(strlen(world.winconf_ids) * sizeof(struct WinConf),
382 while (0 != (id = get_next_winconf_id()))
384 create_winconf(id, &winconfs[i]);
387 world.winconfs = winconfs;
388 while (0 != (id = get_next_winconf_id()))
390 init_winconf_from_file(id);
397 extern void free_winconfs()
400 while (0 != (id = get_next_winconf_id()))
402 free_winconf_data(id);
404 free(world.winconf_ids);
405 free(world.winconfs);
410 extern void init_wins()
413 while (0 != (id = get_next_winconf_id()))
415 init_win_from_winconf(id);
421 extern void sorted_wintoggle_and_activate()
423 char * f_name = "sorted_wintoggle_and_activate()";
425 char * path = "config/windows/toggle_order_and_active";
426 FILE * file = try_fopen(path, "r", f_name);
427 uint16_t linemax = get_linemax(file, f_name);
429 char win_order[linemax + 1];
430 try_fgets(win_order, linemax + 1, file, f_name);
433 char * err = trouble_msg(f_name, "read_uint8()");
434 exit_err(read_uint8(file, &a), err);
437 try_fclose(file, f_name);
440 for (; i < linemax - 1; i++)
442 if (NULL == strchr(world.winconf_ids, win_order[i]))
446 toggle_window(win_order[i]);
448 if (a == (uint8_t) win_order[i])
450 world.wmeta->active = get_win_by_id(win_order[i]);
457 extern void save_win_configs()
459 char * f_name = "save_win_configs()";
462 while (0 != (id = get_next_winconf_id()))
467 char * path = "config/windows/toggle_order_and_active";
468 char * path_tmp = "config/windows/toggle_order_and_active_tmp";
469 FILE * file = try_fopen(path_tmp, "w", f_name);
472 struct Win * w_p = world.wmeta->chain_start;
476 struct WinConf * wc = get_winconf_by_win(w_p);
482 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
483 if (0 != world.wmeta->active)
485 struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
486 write_uint8(wc->id, file);
489 try_fclose_unlink_rename(file, path_tmp, path, f_name);
494 extern void toggle_window(char id)
496 struct Win * win = get_win_by_id(id);
497 if (0 == win->prev && world.wmeta->chain_start != win) /* Win struct is */
498 { /* outside chain. */
509 extern void toggle_winconfig()
511 struct Win * win = world.wmeta->active;
512 struct WinConf * wcp = get_winconf_by_win(win);
515 win->draw = draw_winconf_geometry;
517 wcp->center = win->center;
521 else if (1 == wcp->view)
523 win->draw = draw_winconf_keybindings;
529 win->draw = get_drawfunc_by_char(wcp->draw);
530 win->center = wcp->center;
537 extern void toggle_win_height_type()
539 struct Win * win = world.wmeta->active;
540 struct WinConf * wcp = get_winconf_by_win(win);
541 if (0 == wcp->height_type)
543 wcp->height_type = 1;
547 wcp->height_type = 0;
549 set_winconf_geometry(wcp->id);
554 extern void toggle_win_width_type()
556 struct Win * win = world.wmeta->active;
557 struct WinConf * wcp = get_winconf_by_win(win);
558 if (0 == wcp->width_type && win->framesize.x <= world.wmeta->padsize.x)
566 set_winconf_geometry(wcp->id);
571 extern void scroll_pad(char dir)
575 reset_pad_offset(world.wmeta->pad_offset + 1);
579 reset_pad_offset(world.wmeta->pad_offset - 1);
585 extern void growshrink_active_window(char change)
587 if (0 != world.wmeta->active)
589 struct yx_uint16 size = world.wmeta->active->framesize;
594 else if (change == '+')
598 else if (change == '_')
602 else if (change == '*')
606 resize_active_win(size);
607 struct WinConf * wcp = get_winconf_by_win(world.wmeta->active);
608 if ( 1 == wcp->width_type
609 && world.wmeta->active->framesize.x > world.wmeta->padsize.x)
613 set_winconf_geometry(wcp->id);