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 <stdio.h> /* for fwrite() */
8 #include <unistd.h> /* for access(), unlink() */
9 #include "windows.h" /* for suspend_win(), append_win(), reset_pad_offset(),
10 * resize_active_win(), init_win(), free_win(),
11 * structs Win, WinMeta
13 #include "yx_uint16.h" /* for yx_uint16 struct */
14 #include "main.h" /* for Wins struct */
15 #include "readwrite.h" /* for get_linemax(), try_fopen(), try_fclose(),
16 * try_fgets(), try_fclose_unlink_rename()
18 #include "rexit.h" /* for exit_err() */
19 #include "main.h" /* for World, Wins structs */
20 #include "draw_wins.h" /* for draw_keys_win(), draw_info_win(), draw_log_win(),
23 #include "misc.h" /* for try_malloc() */
24 #include "dirent.h" /* for opendir(), closedir(), readdir() */
25 #include "errno.h" /* for errno */
29 /* Return string "prefix" + "id"; malloc()'s string, remember to call free()! */
30 static char * string_prefixed_id(struct World * world, char * prefix, char id);
34 /* Create Winconf, init ->view/height_type/width_type to 0, ->id to "id". */
35 static void create_winconf(char id, struct WinConf * wcp);
37 /* Initialize Winconf of "id" from appropriate config file.*/
38 static void init_winconf_from_file(struct World * world, char id);
40 /* Wrapper around init_win() called with values from Winconf of "id". */
41 static void init_win_from_winconf(struct World * world, char id);
43 /* Save title, draw function, size of window identified by "id" to conffile. */
44 static void save_win_config(struct World * world, char id);
48 /* Write size 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(struct World * world, char id);
55 /* Get WinConf by "id"; get id of WinConf mothering "win". */
56 static struct WinConf * get_winconf_by_id(struct World * world, char id);
58 /* Get (Win->draw) function identified by "c"; NULL if c not mapped to one. */
59 static void * get_drawfunc_by_char(char c);
61 /* Iterate over bytes of world->winconf_ids array. Re-start after null byte. */
62 static char get_next_winconf_id(struct World * world);
66 static char * string_prefixed_id(struct World * world, char * prefix, char id)
68 uint8_t size = strlen(prefix) + 2;
69 char * path = try_malloc(size, world, "string_prefixed_id()");
70 sprintf(path, "%s_", prefix);
77 static void create_winconf(char id, struct WinConf * wcp)
87 static void init_winconf_from_file(struct World * world, char id)
89 char * tmp = "init_winconf_from_file() on window id '_'";
90 char * context = try_malloc(strlen(tmp) + 1, world,
91 "init_winconf_from_file()");
92 memcpy(context, tmp, strlen(tmp) + 1);
93 context[strlen(tmp) - 2] = id;
95 char * path = string_prefixed_id(world, "config/windows/Win_", id);
96 FILE * file = try_fopen(path, "r", world, context);
98 uint16_t linemax = get_linemax(file, world, context);
99 char line[linemax + 1];
101 struct WinConf * winconf = get_winconf_by_id(world, id);
102 try_fgets(line, linemax + 1, file, world, context);
103 winconf->title = try_malloc(strlen(line), world, context);
104 memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */
105 winconf->title[strlen(line) - 1] = '\0'; /* char at end of string. */
107 try_fgets(line, linemax + 1, file, world, context);
108 winconf->draw = line[0];
110 try_fgets(line, linemax + 1, file, world, context);
111 winconf->height = atoi(line);
112 if (0 >= winconf->height)
114 winconf->height_type = 1;
116 try_fgets(line, linemax + 1, file, world, context);
117 winconf->width = atoi(line);
118 if (0 >= winconf->width)
120 winconf->width_type = 1;
123 try_fclose(file, world, context);
129 static void init_win_from_winconf(struct World * world, char id)
131 char * tmp = "Trouble in init_win_from_file() with init_win() (win id: _).";
132 char * err = try_malloc(strlen(tmp) + 1, world, "init_win_from_file()");
133 memcpy(err, tmp, strlen(tmp) + 1);
134 err[strlen(tmp) - 3] = id;
135 struct WinConf * winconf = get_winconf_by_id(world, id);
136 void * f = get_drawfunc_by_char(winconf->draw);
137 exit_err(NULL == f, world, err);
138 exit_err(init_win(world->wmeta, &winconf->win, winconf->title,
139 winconf->height, winconf->width, world, f),
146 extern void save_win_config(struct World * world, char id)
148 char * f_name = "save_win_config()";
150 char * path_tmp = string_prefixed_id(world, "config/windows/Win_tmp_", id);
151 FILE * file = try_fopen(path_tmp, "w", world, f_name);
153 struct WinConf * wc = get_winconf_by_id(world, id);
154 uint8_t size = strlen(wc->title) + 2;
160 sprintf(line, "%s\n", wc->title);
161 fwrite(line, sizeof(char), strlen(line), file);
162 sprintf(line, "%c\n", wc->draw);
163 fwrite(line, sizeof(char), strlen(line), file);
164 sprintf(line, "%d\n", wc->height);
165 fwrite(line, sizeof(char), strlen(line), file);
166 sprintf(line, "%d\n", wc->width);
167 fwrite(line, sizeof(char), strlen(line), file);
169 char * path = string_prefixed_id(world, "config/windows/Win_", id);
170 try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
177 static void set_winconf(struct World * world, char id)
179 struct WinConf * wcp = get_winconf_by_id(world, id);
180 if (0 == wcp->height_type)
182 wcp->height = wcp->win->frame.size.y;
184 else if (1 == wcp->height_type)
186 wcp->height = wcp->win->frame.size.y - world->wmeta->padframe.size.y
189 if (0 == wcp->width_type)
191 wcp->width = wcp->win->frame.size.x;
193 else if (1 == wcp->width_type)
195 wcp->width = wcp->win->frame.size.x - world->wmeta->padframe.size.x;
201 static struct WinConf * get_winconf_by_id(struct World * world, char id)
206 if (id == world->winconfs[i].id)
208 return &world->winconfs[i];
216 static void * get_drawfunc_by_char(char c)
220 return draw_info_win;
224 return draw_keys_win;
239 static char get_next_winconf_id(struct World * world)
241 static uint8_t i = 0;
242 char c = world->winconf_ids[i];
256 extern struct WinConf * get_winconf_by_win(struct World * world,
262 if (win == world->winconfs[i].win)
264 return &world->winconfs[i];
272 extern struct Win * get_win_by_id(struct World * world, char id)
274 struct WinConf * wc = get_winconf_by_id(world, id);
280 extern void init_winconfs(struct World * world)
282 char * f_name = "init_winconfs()";
283 char * err_o = "Trouble in init_winconfs() with opendir().";
284 char * err_r = "Trouble in init_winconfs() with readdir().";
285 char * err_c = "Trouble in init_winconfs() with closedir().";
287 DIR * dp = opendir("config/windows");
288 exit_err(NULL == dp, world, err_o);
291 char * winconf_ids = try_malloc(256, world, f_name);
294 while (NULL != (fn = readdir(dp)))
296 if ( 5 == strlen(fn->d_name)
297 && fn->d_name == strstr(fn->d_name, "Win_"))
304 winconf_ids[i] = '\0';
305 exit_err(errno, world, err_r);
306 exit_err(closedir(dp), world, err_c);
307 world->winconf_ids = try_malloc(strlen(winconf_ids + 1), world, f_name);
308 memcpy(world->winconf_ids, winconf_ids, strlen(winconf_ids) + 1);
311 struct WinConf * winconfs;
312 winconfs = try_malloc(strlen(world->winconf_ids) * sizeof(struct WinConf),
315 while (0 != (id = get_next_winconf_id(world)))
317 create_winconf(id, &winconfs[i]);
320 world->winconfs = winconfs;
321 while (0 != (id = get_next_winconf_id(world)))
323 init_winconf_from_file(world, id);
330 extern void free_winconf(struct World * world, char id)
332 struct WinConf * wc = get_winconf_by_id(world, id);
338 extern void free_winconfs(struct World * world)
341 while (0 != (id = get_next_winconf_id(world)))
343 free_winconf(world, id);
345 free(world->winconf_ids);
346 free(world->winconfs);
351 extern void init_wins(struct World * world)
354 while (0 != (id = get_next_winconf_id(world)))
356 init_win_from_winconf(world, id);
362 extern void free_wins(struct World * world)
365 while (0 != (id = get_next_winconf_id(world)))
367 free_win(get_win_by_id(world, id));
373 extern void sorted_wintoggle(struct World * world)
375 char * f_name = "sorted_wintoggle()";
376 char * path = "config/windows/toggle_order";
377 FILE * file = try_fopen(path, "r", world, f_name);
378 uint16_t linemax = get_linemax(file, world, f_name);
379 char win_order[linemax + 1];
380 try_fgets(win_order, linemax + 1, file, world, f_name);
381 try_fclose(file, world, f_name);
383 for (; i < linemax - 1; i++)
385 if (NULL == strchr(world->winconf_ids, win_order[i]))
389 toggle_window(world->wmeta, get_win_by_id(world, win_order[i]));
395 extern void reload_win_config(struct World * world)
397 while (0 != world->wmeta->active)
399 suspend_win(world->wmeta, world->wmeta->active);
402 free_winconfs(world);
403 init_winconfs(world);
405 sorted_wintoggle(world);
410 extern void save_win_configs(struct World * world)
412 char * f_name = "save_win_configs()";
415 while (0 != (id = get_next_winconf_id(world)))
417 save_win_config(world, id);
420 char * path = "config/windows/toggle_order";
421 char * path_tmp = "config/windows/toggle_order_tmp";
422 FILE * file = try_fopen(path_tmp, "w", world, f_name);
425 struct Win * w_p = world->wmeta->chain_start;
429 struct WinConf * wc = get_winconf_by_win(world, w_p);
435 fwrite(line, sizeof(char), strlen(line), file);
437 try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
442 extern uint8_t toggle_window(struct WinMeta * win_meta, struct Win * win)
444 if (0 != win->frame.curses_win)
446 return suspend_win(win_meta, win);
450 return append_win(win_meta, win);
456 extern void toggle_winconfig(struct World * world, struct Win * win)
458 struct WinConf * wcp = get_winconf_by_win(world, win);
461 win->draw = draw_winconf;
466 win->draw = get_drawfunc_by_char(wcp->draw);
473 extern void toggle_win_height_type(struct World * world, struct Win * win)
475 struct WinConf * wcp = get_winconf_by_win(world, win);
476 if (0 == wcp->height_type)
478 wcp->height_type = 1;
482 wcp->height_type = 0;
484 set_winconf(world, wcp->id);
489 extern void toggle_win_width_type(struct World * world, struct Win * win)
491 struct WinConf * wcp = get_winconf_by_win(world, win);
492 if ( 0 == wcp->width_type
493 && win->frame.size.x <= world->wmeta->padframe.size.x)
501 set_winconf(world, wcp->id);
506 extern void scroll_pad(struct WinMeta * win_meta, char dir)
510 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
514 reset_pad_offset(win_meta, win_meta->pad_offset - 1);
520 extern uint8_t growshrink_active_window(struct World * world, char change)
522 if (0 != world->wmeta->active)
524 struct yx_uint16 size = world->wmeta->active->frame.size;
529 else if (change == '+')
533 else if (change == '_')
537 else if (change == '*')
541 uint8_t x = resize_active_win(world->wmeta, size);
542 struct WinConf * wcp = get_winconf_by_win(world, world->wmeta->active);
543 if ( 1 == wcp->width_type
544 && world->wmeta->active->frame.size.x
545 > world->wmeta->padframe.size.x)
549 set_winconf(world, wcp->id);