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, Wins structs */
19 #include "draw_wins.h" /* for draw_keys_win(), draw_info_win(), draw_log_win(),
22 #include "misc.h" /* for try_malloc() */
23 #include "dirent.h" /* for opendir(), closedir(), readdir() */
24 #include "errno.h" /* for errno */
28 /* Return string "prefix" + "id"; malloc()'s string, remember to call free()! */
29 static char * string_prefixed_id(struct World * world, char * prefix, char id);
33 /* Create Winconf, init ->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(struct World * world, char id);
39 /* Wrapper around init_win() called with values from Winconf of "id". */
40 static void init_win_from_winconf(struct World * world, char id);
42 /* Save title, draw function, size of window identified by "id" to conffile. */
43 static void save_win_config(struct World * world, char id);
47 /* Write size of a window to its WinConf, as positive or negative values
48 * (dependent on state ofWinConf->height_type / WinConf->width_type).
50 static void set_winconf(struct World * world, char id);
54 /* Get WinConf by "id"; get id of WinConf mothering "win". */
55 static struct WinConf * get_winconf_by_id(struct World * world, char id);
57 /* Get (Win->draw) function identified by "c"; NULL if c not mapped to one. */
58 static void * get_drawfunc_by_char(char c);
60 /* Iterate over bytes of world->winconf_ids array. Re-start after null byte. */
61 static char get_next_winconf_id(struct World * world);
65 static char * string_prefixed_id(struct World * world, char * prefix, char id)
67 uint8_t size = strlen(prefix) + 2;
68 char * path = try_malloc(size, world, "string_prefixed_id()");
69 sprintf(path, "%s_", prefix);
76 static void create_winconf(char id, struct WinConf * wcp)
86 static void init_winconf_from_file(struct World * world, char id)
88 char * tmp = "init_winconf_from_file() on window id '_'";
89 char * context = try_malloc(strlen(tmp) + 1, world,
90 "init_winconf_from_file()");
91 memcpy(context, tmp, strlen(tmp) + 1);
92 context[strlen(tmp) - 2] = id;
94 char * path = string_prefixed_id(world, "config/windows/Win_", id);
95 FILE * file = try_fopen(path, "r", world, context);
97 uint16_t linemax = get_linemax(file, world, context);
98 char line[linemax + 1];
100 struct WinConf * winconf = get_winconf_by_id(world, id);
101 try_fgets(line, linemax + 1, file, world, context);
102 winconf->title = try_malloc(strlen(line), world, 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, world, context);
107 winconf->draw = line[0];
109 try_fgets(line, linemax + 1, file, world, context);
110 winconf->height = atoi(line);
111 if (0 >= winconf->height)
113 winconf->height_type = 1;
115 try_fgets(line, linemax + 1, file, world, context);
116 winconf->width = atoi(line);
117 if (0 >= winconf->width)
119 winconf->width_type = 1;
122 try_fclose(file, world, context);
128 static void init_win_from_winconf(struct World * world, char id)
130 char * tmp = "Trouble in init_win_from_file() with init_win() (win id: _).";
131 char * err = try_malloc(strlen(tmp) + 1, world, "init_win_from_file()");
132 memcpy(err, tmp, strlen(tmp) + 1);
133 err[strlen(tmp) - 3] = id;
134 struct WinConf * winconf = get_winconf_by_id(world, id);
135 void * f = get_drawfunc_by_char(winconf->draw);
136 exit_err(NULL == f, world, err);
137 exit_err(init_win(world->wmeta, &winconf->win, winconf->title,
138 winconf->height, winconf->width, world, f),
145 extern void save_win_config(struct World * world, char id)
147 char * f_name = "save_win_config()";
149 char * path_tmp = string_prefixed_id(world, "config/windows/Win_tmp_", id);
150 FILE * file = try_fopen(path_tmp, "w", world, f_name);
152 struct WinConf * wc = get_winconf_by_id(world, id);
153 uint8_t size = strlen(wc->title) + 2;
159 sprintf(line, "%s\n", wc->title);
160 try_fwrite(line, sizeof(char), strlen(line), file, world, f_name);
161 sprintf(line, "%c\n", wc->draw);
162 try_fwrite(line, sizeof(char), strlen(line), file, world, f_name);
163 sprintf(line, "%d\n", wc->height);
164 try_fwrite(line, sizeof(char), strlen(line), file, world, f_name);
165 sprintf(line, "%d\n", wc->width);
166 try_fwrite(line, sizeof(char), strlen(line), file, world, f_name);
168 char * path = string_prefixed_id(world, "config/windows/Win_", id);
169 try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
176 static void set_winconf(struct World * world, char id)
178 struct WinConf * wcp = get_winconf_by_id(world, id);
179 if (0 == wcp->height_type)
181 wcp->height = wcp->win->frame.size.y;
183 else if (1 == wcp->height_type)
185 wcp->height = wcp->win->frame.size.y - world->wmeta->padframe.size.y
188 if (0 == wcp->width_type)
190 wcp->width = wcp->win->frame.size.x;
192 else if (1 == wcp->width_type)
194 wcp->width = wcp->win->frame.size.x - world->wmeta->padframe.size.x;
200 static struct WinConf * get_winconf_by_id(struct World * world, char id)
205 if (id == world->winconfs[i].id)
207 return &world->winconfs[i];
215 static void * get_drawfunc_by_char(char c)
219 return draw_info_win;
223 return draw_keys_win;
238 static char get_next_winconf_id(struct World * world)
240 static uint8_t i = 0;
241 char c = world->winconf_ids[i];
255 extern struct WinConf * get_winconf_by_win(struct World * world,
261 if (win == world->winconfs[i].win)
263 return &world->winconfs[i];
271 extern struct Win * get_win_by_id(struct World * world, char id)
273 struct WinConf * wc = get_winconf_by_id(world, id);
279 extern void init_winconfs(struct World * world)
281 char * f_name = "init_winconfs()";
282 char * err_o = "Trouble in init_winconfs() with opendir().";
283 char * err_r = "Trouble in init_winconfs() with readdir().";
284 char * err_c = "Trouble in init_winconfs() with closedir().";
286 DIR * dp = opendir("config/windows");
287 exit_err(NULL == dp, world, err_o);
290 char * winconf_ids = try_malloc(256, world, f_name);
293 while (NULL != (fn = readdir(dp)))
295 if ( 5 == strlen(fn->d_name)
296 && fn->d_name == strstr(fn->d_name, "Win_"))
303 winconf_ids[i] = '\0';
304 exit_err(errno, world, err_r);
305 exit_err(closedir(dp), world, err_c);
306 world->winconf_ids = try_malloc(strlen(winconf_ids) + 1, world, f_name);
307 memcpy(world->winconf_ids, winconf_ids, strlen(winconf_ids) + 1);
310 struct WinConf * winconfs;
311 winconfs = try_malloc(strlen(world->winconf_ids) * sizeof(struct WinConf),
314 while (0 != (id = get_next_winconf_id(world)))
316 create_winconf(id, &winconfs[i]);
319 world->winconfs = winconfs;
320 while (0 != (id = get_next_winconf_id(world)))
322 init_winconf_from_file(world, id);
329 extern void free_winconf(struct World * world, char id)
331 struct WinConf * wc = get_winconf_by_id(world, id);
337 extern void free_winconfs(struct World * world)
340 while (0 != (id = get_next_winconf_id(world)))
342 free_winconf(world, id);
344 free(world->winconf_ids);
345 free(world->winconfs);
350 extern void init_wins(struct World * world)
353 while (0 != (id = get_next_winconf_id(world)))
355 init_win_from_winconf(world, id);
361 extern void free_wins(struct World * world)
364 while (0 != (id = get_next_winconf_id(world)))
366 free_win(get_win_by_id(world, id));
372 extern void sorted_wintoggle(struct World * world)
374 char * f_name = "sorted_wintoggle()";
375 char * path = "config/windows/toggle_order";
376 FILE * file = try_fopen(path, "r", world, f_name);
377 uint16_t linemax = get_linemax(file, world, f_name);
378 char win_order[linemax + 1];
379 try_fgets(win_order, linemax + 1, file, world, f_name);
380 try_fclose(file, world, f_name);
382 for (; i < linemax - 1; i++)
384 if (NULL == strchr(world->winconf_ids, win_order[i]))
388 toggle_window(world->wmeta, get_win_by_id(world, win_order[i]));
394 extern void reload_win_config(struct World * world)
396 while (0 != world->wmeta->active)
398 suspend_win(world->wmeta, world->wmeta->active);
401 free_winconfs(world);
402 init_winconfs(world);
404 sorted_wintoggle(world);
409 extern void save_win_configs(struct World * world)
411 char * f_name = "save_win_configs()";
414 while (0 != (id = get_next_winconf_id(world)))
416 save_win_config(world, id);
419 char * path = "config/windows/toggle_order";
420 char * path_tmp = "config/windows/toggle_order_tmp";
421 FILE * file = try_fopen(path_tmp, "w", world, f_name);
424 struct Win * w_p = world->wmeta->chain_start;
428 struct WinConf * wc = get_winconf_by_win(world, w_p);
434 try_fwrite(line, sizeof(char), strlen(line), file, world, f_name);
436 try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
441 extern uint8_t toggle_window(struct WinMeta * win_meta, struct Win * win)
443 if (0 != win->frame.curses_win)
445 return suspend_win(win_meta, win);
449 return append_win(win_meta, win);
455 extern void toggle_winconfig(struct World * world, struct Win * win)
457 struct WinConf * wcp = get_winconf_by_win(world, win);
460 win->draw = draw_winconf;
465 win->draw = get_drawfunc_by_char(wcp->draw);
472 extern void toggle_win_height_type(struct World * world, struct Win * win)
474 struct WinConf * wcp = get_winconf_by_win(world, win);
475 if (0 == wcp->height_type)
477 wcp->height_type = 1;
481 wcp->height_type = 0;
483 set_winconf(world, wcp->id);
488 extern void toggle_win_width_type(struct World * world, struct Win * win)
490 struct WinConf * wcp = get_winconf_by_win(world, win);
491 if ( 0 == wcp->width_type
492 && win->frame.size.x <= world->wmeta->padframe.size.x)
500 set_winconf(world, wcp->id);
505 extern void scroll_pad(struct WinMeta * win_meta, char dir)
509 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
513 reset_pad_offset(win_meta, win_meta->pad_offset - 1);
519 extern uint8_t growshrink_active_window(struct World * world, char change)
521 if (0 != world->wmeta->active)
523 struct yx_uint16 size = world->wmeta->active->frame.size;
528 else if (change == '+')
532 else if (change == '_')
536 else if (change == '*')
540 uint8_t x = resize_active_win(world->wmeta, size);
541 struct WinConf * wcp = get_winconf_by_win(world, world->wmeta->active);
542 if ( 1 == wcp->width_type
543 && world->wmeta->active->frame.size.x
544 > world->wmeta->padframe.size.x)
548 set_winconf(world, wcp->id);