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 Wins struct */
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 struct */
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(struct World * world, char * prefix, char id);
38 /* Create Winconf, init ->view/height_type/width_type to 0, ->id to "id". */
39 static void create_winconf(struct World * world, char id, struct WinConf * wcp);
41 /* Initialize Winconf of "id" from appropriate config file.*/
42 static void init_winconf_from_file(struct World * world, char id);
44 /* Wrapper around init_win() called with values from Winconf of "id". */
45 static void init_win_from_winconf(struct World * world, char id);
47 /* Save title, draw function, size of window identified by "id" to conffile. */
48 static void save_win_config(struct World * world, char id);
50 /* Free data pointed to inside WinConf struct. */
51 static void free_winconf_data(struct World * world, 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(struct World * world, char id);
62 /* Get WinConf by "id"; get id of WinConf mothering "win". */
63 static struct WinConf * get_winconf_by_id(struct World * world, 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(struct World * world);
73 static char * string_prefixed_id(struct World * world, char * prefix, char id)
75 uint8_t size = strlen(prefix) + 2;
76 char * path = try_malloc(size, world, "string_prefixed_id()");
77 sprintf(path, "%s_", prefix);
84 static void create_winconf(struct World * world, char id, struct WinConf * wcp)
96 static void init_winconf_from_file(struct World * world, char id)
98 char * tmp = "init_winconf_from_file() on window id '_'";
99 char * context = try_malloc(strlen(tmp) + 1, world,
100 "init_winconf_from_file()");
101 memcpy(context, tmp, strlen(tmp) + 1);
102 context[strlen(tmp) - 2] = id;
104 char * path = string_prefixed_id(world, "config/windows/Win_", id);
105 FILE * file = try_fopen(path, "r", world, context);
107 uint16_t linemax = get_linemax(file, world, context);
108 char line[linemax + 1];
110 struct WinConf * winconf = get_winconf_by_id(world, id);
111 try_fgets(line, linemax + 1, file, world, context);
112 winconf->title = try_malloc(strlen(line), world, context);
113 memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */
114 winconf->title[strlen(line) - 1] = '\0'; /* char at end of string. */
116 try_fgets(line, linemax + 1, file, world, context);
117 winconf->draw = line[0];
119 try_fgets(line, linemax + 1, file, world, context);
120 winconf->height = atoi(line);
121 if (0 >= winconf->height)
123 winconf->height_type = 1;
125 try_fgets(line, linemax + 1, file, world, context);
126 winconf->width = atoi(line);
127 if (0 >= winconf->width)
129 winconf->width_type = 1;
132 char command[linemax + 1];
134 struct KeyBinding ** loc_last_ptr = &winconf->kb.kbs;
136 while (fgets(command, linemax + 1, file))
138 if ('\n' == command[0] || 0 == command[0])
142 * loc_last_ptr = try_malloc(sizeof(struct KeyBinding), world, context);
143 struct KeyBinding * kb_p = * loc_last_ptr;
145 kb_p->key = atoi(command);
146 cmdptr = strchr(command, ' ') + 1;
147 kb_p->name = try_malloc(strlen(cmdptr), world, context);
148 memcpy(kb_p->name, cmdptr, strlen(cmdptr) - 1);
149 kb_p->name[strlen(cmdptr) - 1] = '\0';
150 loc_last_ptr = & kb_p->next;
153 try_fclose(file, world, context);
159 static void init_win_from_winconf(struct World * world, char id)
161 char * tmp = "Trouble in init_win_from_file() with init_win() (win id: _).";
162 char * err = try_malloc(strlen(tmp) + 1, world, "init_win_from_file()");
163 memcpy(err, tmp, strlen(tmp) + 1);
164 err[strlen(tmp) - 3] = id;
165 struct WinConf * winconf = get_winconf_by_id(world, id);
166 void * f = get_drawfunc_by_char(winconf->draw);
167 exit_err(NULL == f, world, err);
168 exit_err(init_win(world->wmeta, &winconf->win, winconf->title,
169 winconf->height, winconf->width, world, f),
176 static void save_win_config(struct World * world, char id)
178 char * f_name = "save_win_config()";
180 char * path_tmp = string_prefixed_id(world, "config/windows/Win_tmp_", id);
181 FILE * file = try_fopen(path_tmp, "w", world, f_name);
183 struct WinConf * wc = get_winconf_by_id(world, id);
184 uint8_t size = strlen(wc->title) + 2;
190 sprintf(line, "%s\n", wc->title);
191 try_fwrite(line, sizeof(char), strlen(line), file, world, f_name);
192 sprintf(line, "%c\n", wc->draw);
193 try_fwrite(line, sizeof(char), strlen(line), file, world, f_name);
194 sprintf(line, "%d\n", wc->height);
195 try_fwrite(line, sizeof(char), strlen(line), file, world, f_name);
196 sprintf(line, "%d\n", wc->width);
197 try_fwrite(line, sizeof(char), strlen(line), file, world, f_name);
199 uint16_t linemax = 0;
200 struct KeyBinding * kb_p = wc->kb.kbs;
203 if (strlen(kb_p->name) > linemax)
205 linemax = strlen(kb_p->name);
209 linemax = linemax + 6; /* + 6 = + 3 digits + whitespace + \n + \0 */
211 char kb_line[linemax];
215 snprintf(kb_line, linemax, "%d %s\n", kb_p->key, kb_p->name);
216 try_fwrite(kb_line, sizeof(char), strlen(kb_line), file, world, f_name);
220 char * path = string_prefixed_id(world, "config/windows/Win_", id);
221 try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
228 static void free_winconf_data(struct World * world, char id)
230 struct WinConf * wc = get_winconf_by_id(world, id);
232 free_keybindings(wc->kb.kbs);
238 static void set_winconf_geometry(struct World * world, char id)
240 struct WinConf * wcp = get_winconf_by_id(world, id);
241 if (0 == wcp->height_type)
243 wcp->height = wcp->win->framesize.y;
245 else if (1 == wcp->height_type)
247 wcp->height = wcp->win->framesize.y - world->wmeta->padsize.y + 1;
249 if (0 == wcp->width_type)
251 wcp->width = wcp->win->framesize.x;
253 else if (1 == wcp->width_type)
255 wcp->width = wcp->win->framesize.x - world->wmeta->padsize.x;
261 static struct WinConf * get_winconf_by_id(struct World * world, char id)
266 if (id == world->winconfs[i].id)
268 return &world->winconfs[i];
276 static void * get_drawfunc_by_char(char c)
280 return draw_win_inventory;
284 return draw_win_info;
292 return draw_win_available_keybindings;
300 return draw_win_keybindings_global;
304 return draw_win_keybindings_winconf_geometry;
308 return draw_win_keybindings_winconf_keybindings;
315 static char get_next_winconf_id(struct World * world)
317 static uint8_t i = 0;
318 char c = world->winconf_ids[i];
332 extern struct WinConf * get_winconf_by_win(struct World * world,
338 if (win == world->winconfs[i].win)
340 return &world->winconfs[i];
348 extern struct Win * get_win_by_id(struct World * world, char id)
350 struct WinConf * wc = get_winconf_by_id(world, id);
356 extern void init_winconfs(struct World * world)
358 char * f_name = "init_winconfs()";
359 char * err_o = "Trouble in init_winconfs() with opendir().";
360 char * err_r = "Trouble in init_winconfs() with readdir().";
361 char * err_c = "Trouble in init_winconfs() with closedir().";
363 DIR * dp = opendir("config/windows");
364 exit_err(NULL == dp, world, err_o);
367 char * winconf_ids = try_malloc(256, world, f_name);
370 while (NULL != (fn = readdir(dp)))
372 if ( 5 == strlen(fn->d_name)
373 && fn->d_name == strstr(fn->d_name, "Win_"))
380 winconf_ids[i] = '\0';
381 exit_err(errno, world, err_r);
382 exit_err(closedir(dp), world, err_c);
383 world->winconf_ids = try_malloc(strlen(winconf_ids) + 1, world, f_name);
384 memcpy(world->winconf_ids, winconf_ids, strlen(winconf_ids) + 1);
387 struct WinConf * winconfs;
388 winconfs = try_malloc(strlen(world->winconf_ids) * sizeof(struct WinConf),
391 while (0 != (id = get_next_winconf_id(world)))
393 create_winconf(world, id, &winconfs[i]);
396 world->winconfs = winconfs;
397 while (0 != (id = get_next_winconf_id(world)))
399 init_winconf_from_file(world, id);
406 extern void free_winconfs(struct World * world)
409 while (0 != (id = get_next_winconf_id(world)))
411 free_winconf_data(world, id);
413 free(world->winconf_ids);
414 free(world->winconfs);
419 extern void init_wins(struct World * world)
422 while (0 != (id = get_next_winconf_id(world)))
424 init_win_from_winconf(world, id);
430 extern void sorted_wintoggle_and_activate(struct World * world)
432 char * f_name = "sorted_wintoggle_and_activate()";
434 char * path = "config/windows/toggle_order_and_active";
435 FILE * file = try_fopen(path, "r", world, f_name);
436 uint16_t linemax = get_linemax(file, world, f_name);
438 char win_order[linemax + 1];
439 try_fgets(win_order, linemax + 1, file, world, f_name);
442 char * err = trouble_msg(world, f_name, "read_uint8()");
443 exit_err(read_uint8(file, &a), world, err);
446 try_fclose(file, world, f_name);
449 for (; i < linemax - 1; i++)
451 if (NULL == strchr(world->winconf_ids, win_order[i]))
455 struct Win * win = get_win_by_id(world, win_order[i]);
456 toggle_window(world->wmeta, win);
458 if (a == (uint8_t) win_order[i])
460 world->wmeta->active = win;
467 extern void save_win_configs(struct World * world)
469 char * f_name = "save_win_configs()";
472 while (0 != (id = get_next_winconf_id(world)))
474 save_win_config(world, id);
477 char * path = "config/windows/toggle_order_and_active";
478 char * path_tmp = "config/windows/toggle_order_and_active_tmp";
479 FILE * file = try_fopen(path_tmp, "w", world, f_name);
482 struct Win * w_p = world->wmeta->chain_start;
486 struct WinConf * wc = get_winconf_by_win(world, w_p);
492 try_fwrite(line, sizeof(char), strlen(line), file, world, f_name);
493 if (0 != world->wmeta->active)
495 struct WinConf * wc = get_winconf_by_win(world, world->wmeta->active);
496 write_uint8(wc->id, file);
499 try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
504 extern uint8_t toggle_window(struct WinMeta * win_meta, struct Win * win)
506 if (0 == win->prev && win_meta->chain_start != win) /* Win outside chain. */
508 return append_win(win_meta, win);
512 return suspend_win(win_meta, win);
518 extern void toggle_winconfig(struct World * world, struct Win * win)
520 struct WinConf * wcp = get_winconf_by_win(world, win);
523 win->draw = draw_winconf_geometry;
525 wcp->center = win->center;
529 else if (1 == wcp->view)
531 win->draw = draw_winconf_keybindings;
537 win->draw = get_drawfunc_by_char(wcp->draw);
538 win->center = wcp->center;
545 extern void toggle_win_height_type(struct World * world, struct Win * win)
547 struct WinConf * wcp = get_winconf_by_win(world, win);
548 if (0 == wcp->height_type)
550 wcp->height_type = 1;
554 wcp->height_type = 0;
556 set_winconf_geometry(world, wcp->id);
561 extern void toggle_win_width_type(struct World * world, struct Win * win)
563 struct WinConf * wcp = get_winconf_by_win(world, win);
564 if (0 == wcp->width_type && win->framesize.x <= world->wmeta->padsize.x)
572 set_winconf_geometry(world, wcp->id);
577 extern void scroll_pad(struct WinMeta * win_meta, char dir)
581 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
585 reset_pad_offset(win_meta, win_meta->pad_offset - 1);
591 extern uint8_t growshrink_active_window(struct World * world, char change)
593 if (0 != world->wmeta->active)
595 struct yx_uint16 size = world->wmeta->active->framesize;
600 else if (change == '+')
604 else if (change == '_')
608 else if (change == '*')
612 uint8_t x = resize_active_win(world->wmeta, size);
613 struct WinConf * wcp = get_winconf_by_win(world, world->wmeta->active);
614 if ( 1 == wcp->width_type
615 && world->wmeta->active->framesize.x > world->wmeta->padsize.x)
619 set_winconf_geometry(world, wcp->id);