3 #include "wincontrol.h"
4 #include <stdlib.h> /* for free() */
5 #include <string.h> /* for strlen() */
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 * f_name = "init_winconf_from_file()";
91 char * path = string_prefixed_id(world, "config/windows/Win_", id);
92 FILE * file = try_fopen(path, "r", world, f_name);
94 uint16_t linemax = get_linemax(file, world, f_name);
95 char line[linemax + 1];
97 struct WinConf * winconf = get_winconf_by_id(world, id);
98 try_fgets(line, linemax + 1, file, world, f_name);
99 winconf->title = try_malloc(strlen(line), world, f_name);
100 memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */
101 winconf->title[strlen(line) - 1] = '\0'; /* char at end of string. */
103 try_fgets(line, linemax + 1, file, world, f_name);
104 winconf->draw = line[0];
106 try_fgets(line, linemax + 1, file, world, f_name);
107 winconf->height = atoi(line);
108 if (0 >= winconf->height)
110 winconf->height_type = 1;
112 try_fgets(line, linemax + 1, file, world, f_name);
113 winconf->width = atoi(line);
114 if (0 >= winconf->width)
116 winconf->width_type = 1;
119 try_fclose(file, world, f_name);
124 static void init_win_from_winconf(struct World * world, char id)
126 char * tmp = "Trouble in init_win_from_file() with init_win() (win id: _).";
127 char * err = try_malloc(strlen(tmp) + 1, world, "init_win_from_file()");
128 memcpy(err, tmp, strlen(tmp) + 1);
129 err[strlen(tmp) - 3] = id;
130 struct WinConf * winconf = get_winconf_by_id(world, id);
131 void * f = get_drawfunc_by_char(winconf->draw);
132 exit_err(NULL == f, world, err);
133 exit_err(init_win(world->wmeta, &winconf->win, winconf->title,
134 winconf->height, winconf->width, world, f),
141 extern void save_win_config(struct World * world, char id)
143 char * f_name = "save_win_config()";
145 char * path_tmp = string_prefixed_id(world, "config/windows/Win_tmp_", id);
146 FILE * file = try_fopen(path_tmp, "w", world, f_name);
148 struct WinConf * wc = get_winconf_by_id(world, id);
149 uint8_t size = strlen(wc->title) + 2;
155 sprintf(line, "%s\n", wc->title);
156 fwrite(line, sizeof(char), strlen(line), file);
157 sprintf(line, "%c\n", wc->draw);
158 fwrite(line, sizeof(char), strlen(line), file);
159 sprintf(line, "%d\n", wc->height);
160 fwrite(line, sizeof(char), strlen(line), file);
161 sprintf(line, "%d\n", wc->width);
162 fwrite(line, sizeof(char), strlen(line), file);
164 char * path = string_prefixed_id(world, "config/windows/Win_", id);
165 try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
172 static void set_winconf(struct World * world, char id)
174 struct WinConf * wcp = get_winconf_by_id(world, id);
175 if (0 == wcp->height_type)
177 wcp->height = wcp->win->frame.size.y;
179 else if (1 == wcp->height_type)
181 wcp->height = wcp->win->frame.size.y - world->wmeta->padframe.size.y
184 if (0 == wcp->width_type)
186 wcp->width = wcp->win->frame.size.x;
188 else if (1 == wcp->width_type)
190 wcp->width = wcp->win->frame.size.x - world->wmeta->padframe.size.x;
196 static struct WinConf * get_winconf_by_id(struct World * world, char id)
201 if (id == world->winconfs[i].id)
203 return &world->winconfs[i];
211 static void * get_drawfunc_by_char(char c)
215 return draw_info_win;
219 return draw_keys_win;
234 static char get_next_winconf_id(struct World * world)
236 static uint8_t i = 0;
237 char c = world->winconf_ids[i];
251 extern struct WinConf * get_winconf_by_win(struct World * world,
257 if (win == world->winconfs[i].win)
259 return &world->winconfs[i];
267 extern struct Win * get_win_by_id(struct World * world, char id)
269 struct WinConf * wc = get_winconf_by_id(world, id);
275 extern void init_winconfs(struct World * world)
277 char * f_name = "init_winconfs()";
278 char * err_o = "Trouble in init_winconfs() with opendir().";
279 char * err_r = "Trouble in init_winconfs() with readdir().";
280 char * err_c = "Trouble in init_winconfs() with closedir().";
282 DIR * dp = opendir("config/windows");
283 exit_err(NULL == dp, world, err_o);
286 char * winconf_ids = try_malloc(256, world, f_name);
289 while (NULL != (fn = readdir(dp)))
291 if ( 5 == strlen(fn->d_name)
292 && fn->d_name == strstr(fn->d_name, "Win_"))
299 winconf_ids[i] = '\0';
300 exit_err(errno, world, err_r);
301 exit_err(closedir(dp), world, err_c);
302 world->winconf_ids = try_malloc(strlen(winconf_ids + 1), world, f_name);
303 memcpy(world->winconf_ids, winconf_ids, strlen(winconf_ids) + 1);
306 struct WinConf * winconfs;
307 winconfs = try_malloc(strlen(world->winconf_ids) * sizeof(struct WinConf),
310 while (0 != (id = get_next_winconf_id(world)))
312 create_winconf(id, &winconfs[i]);
315 world->winconfs = winconfs;
316 while (0 != (id = get_next_winconf_id(world)))
318 init_winconf_from_file(world, id);
325 extern void free_winconf(struct World * world, char id)
327 struct WinConf * wc = get_winconf_by_id(world, id);
333 extern void free_winconfs(struct World * world)
336 while (0 != (id = get_next_winconf_id(world)))
338 free_winconf(world, id);
340 free(world->winconf_ids);
341 free(world->winconfs);
346 extern void init_wins(struct World * world)
349 while (0 != (id = get_next_winconf_id(world)))
351 init_win_from_winconf(world, id);
357 extern void free_wins(struct World * world)
360 while (0 != (id = get_next_winconf_id(world)))
362 free_win(get_win_by_id(world, id));
368 extern void sorted_wintoggle(struct World * world)
370 char * f_name = "sorted_wintoggle()";
371 char * path = "config/windows/toggle_order";
372 FILE * file = try_fopen(path, "r", world, f_name);
373 uint16_t linemax = get_linemax(file, world, f_name);
374 char win_order[linemax + 1];
375 try_fgets(win_order, linemax + 1, file, world, f_name);
376 try_fclose(file, world, f_name);
378 for (; i < linemax - 1; i++)
380 toggle_window(world->wmeta, get_win_by_id(world, win_order[i]));
386 extern void reload_win_config(struct World * world)
388 while (0 != world->wmeta->active)
390 suspend_win(world->wmeta, world->wmeta->active);
393 free_winconfs(world);
394 init_winconfs(world);
396 sorted_wintoggle(world);
401 extern void save_win_configs(struct World * world)
403 char * f_name = "save_win_configs()";
406 while (0 != (id = get_next_winconf_id(world)))
408 save_win_config(world, id);
411 char * path = "config/windows/toggle_order";
412 char * path_tmp = "config/windows/toggle_order_tmp";
413 FILE * file = try_fopen(path_tmp, "w", world, f_name);
416 struct Win * w_p = world->wmeta->_chain_start;
420 struct WinConf * wc = get_winconf_by_win(world, w_p);
426 fwrite(line, sizeof(char), strlen(line), file);
428 try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
433 extern uint8_t toggle_window(struct WinMeta * win_meta, struct Win * win)
435 if (0 != win->frame.curses_win)
437 return suspend_win(win_meta, win);
441 return append_win(win_meta, win);
447 extern void toggle_winconfig(struct World * world, struct Win * win)
449 struct WinConf * wcp = get_winconf_by_win(world, win);
452 win->_draw = draw_winconf;
457 win->_draw = get_drawfunc_by_char(wcp->draw);
464 extern void toggle_win_height_type(struct World * world, struct Win * win)
466 struct WinConf * wcp = get_winconf_by_win(world, win);
467 if (0 == wcp->height_type)
469 wcp->height_type = 1;
473 wcp->height_type = 0;
475 set_winconf(world, wcp->id);
480 extern void toggle_win_width_type(struct World * world, struct Win * win)
482 struct WinConf * wcp = get_winconf_by_win(world, win);
483 if ( 0 == wcp->width_type
484 && win->frame.size.x <= world->wmeta->padframe.size.x)
492 set_winconf(world, wcp->id);
497 extern void scroll_pad(struct WinMeta * win_meta, char dir)
501 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
505 reset_pad_offset(win_meta, win_meta->pad_offset - 1);
511 extern uint8_t growshrink_active_window(struct World * world, char change)
513 if (0 != world->wmeta->active)
515 struct yx_uint16 size = world->wmeta->active->frame.size;
520 else if (change == '+')
524 else if (change == '_')
528 else if (change == '*')
532 uint8_t x = resize_active_win(world->wmeta, size);
533 struct WinConf * wcp = get_winconf_by_win(world, world->wmeta->active);
534 if ( 1 == wcp->width_type
535 && world->wmeta->active->frame.size.x
536 > world->wmeta->padframe.size.x)
540 set_winconf(world, wcp->id);