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() */
26 /* Return string "prefix" + "id"; malloc()'s string, remember to call free()! */
27 static char * string_prefixed_id(struct World * world, char * prefix, char id);
31 /* Create Winconf, initialize ->view/height_type/width_type to 0, ->id to "id"
34 static void create_winconf(char id, struct WinConf * wcp,
35 void (* f) (struct Win *));
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, size of window identified by "id" to its configuration file. */
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);
57 static char get_id_by_win(struct World * world, struct Win * win);
61 static char * string_prefixed_id(struct World * world, char * prefix, char id)
63 uint8_t size = strlen(prefix) + 2;
64 char * path = try_malloc(size, world, "string_prefixed_id()");
65 sprintf(path, "%s_", prefix);
72 static void create_winconf(char id, struct WinConf * wcp,
73 void (* f) (struct Win *))
84 static void init_winconf_from_file(struct World * world, char id)
86 char * f_name = "init_winconf_from_file()";
88 char * path = string_prefixed_id(world, "config/windows/Win_", id);
89 FILE * file = try_fopen(path, "r", world, f_name);
91 uint16_t linemax = get_linemax(file, world, f_name);
92 char line[linemax + 1];
94 struct WinConf * winconf = get_winconf_by_id(world, id);
95 try_fgets(line, linemax + 1, file, world, f_name);
96 winconf->title = try_malloc(strlen(line), world, f_name);
97 memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */
98 winconf->title[strlen(line) - 1] = '\0'; /* char at end of string. */
99 try_fgets(line, linemax + 1, file, world, f_name);
100 winconf->height = atoi(line);
101 if (0 >= winconf->height)
103 winconf->height_type = 1;
105 try_fgets(line, linemax + 1, file, world, f_name);
106 winconf->width = atoi(line);
107 if (0 >= winconf->width)
109 winconf->width_type = 1;
112 try_fclose(file, world, f_name);
117 static void init_win_from_winconf(struct World * world, char id)
119 char * err = "Trouble in init_win_from_file() with init_win().";
120 struct WinConf * winconf = get_winconf_by_id(world, id);
121 exit_err(init_win(world->wmeta, &winconf->win, winconf->title,
122 winconf->height, winconf->width, world, winconf->draw),
128 extern void save_win_config(struct World * world, char id)
130 char * f_name = "save_win_config()";
132 char * path_tmp = string_prefixed_id(world, "config/windows/Win_tmp_", id);
133 FILE * file = try_fopen(path_tmp, "w", world, f_name);
135 struct WinConf * wc = get_winconf_by_id(world, id);
136 uint8_t size = strlen(wc->title) + 2;
142 sprintf(line, "%s\n", wc->title);
143 fwrite(line, sizeof(char), strlen(line), file);
144 sprintf(line, "%d\n", wc->height);
145 fwrite(line, sizeof(char), strlen(line), file);
146 sprintf(line, "%d\n", wc->width);
147 fwrite(line, sizeof(char), strlen(line), file);
149 char * path = string_prefixed_id(world, "config/windows/Win_", id);
150 try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
157 static void set_winconf(struct World * world, char id)
159 struct WinConf * wcp = get_winconf_by_id(world, id);
160 if (0 == wcp->height_type)
162 wcp->height = wcp->win->frame.size.y;
164 else if (1 == wcp->height_type)
166 wcp->height = wcp->win->frame.size.y - world->wmeta->padframe.size.y
169 if (0 == wcp->width_type)
171 wcp->width = wcp->win->frame.size.x;
173 else if (1 == wcp->width_type)
175 wcp->width = wcp->win->frame.size.x - world->wmeta->padframe.size.x;
181 static struct WinConf * get_winconf_by_id(struct World * world, char id)
186 if (id == world->winconfs[i].id)
188 return &world->winconfs[i];
196 static char get_id_by_win(struct World * world, struct Win * win)
198 struct WinConf * wc = get_winconf_by_win(world, win);
204 extern struct WinConf * get_winconf_by_win(struct World * world,
210 if (win == world->winconfs[i].win)
212 return &world->winconfs[i];
220 extern struct Win * get_win_by_id(struct World * world, char id)
222 struct WinConf * wc = get_winconf_by_id(world, id);
228 extern void init_winconfs(struct World * world)
230 char * f_name = "init_winconfs()";
231 struct WinConf * winconfs = try_malloc(4 * sizeof(struct WinConf),
233 create_winconf('i', &winconfs[0], draw_info_win);
234 create_winconf('k', &winconfs[1], draw_keys_win);
235 create_winconf('l', &winconfs[2], draw_log_win);
236 create_winconf('m', &winconfs[3], draw_map_win);
237 world->winconfs = winconfs;
238 init_winconf_from_file(world, 'i');
239 init_winconf_from_file(world, 'k');
240 init_winconf_from_file(world, 'l');
241 init_winconf_from_file(world, 'm');
246 extern void free_winconf(struct World * world, char id)
248 struct WinConf * wc = get_winconf_by_id(world, id);
254 extern void free_winconfs(struct World * world)
256 free_winconf(world, 'i');
257 free_winconf(world, 'k');
258 free_winconf(world, 'l');
259 free_winconf(world, 'm');
260 free(world->winconfs);
265 extern void init_wins(struct World * world)
267 init_win_from_winconf(world, 'i');
268 init_win_from_winconf(world, 'k');
269 init_win_from_winconf(world, 'l');
270 init_win_from_winconf(world, 'm');
275 extern void free_wins(struct World * world)
277 free_win(get_win_by_id(world, 'i'));
278 free_win(get_win_by_id(world, 'k'));
279 free_win(get_win_by_id(world, 'l'));
280 free_win(get_win_by_id(world, 'm'));
285 extern void sorted_wintoggle(struct World * world)
287 char * f_name = "sorted_wintoggle()";
288 char * path = "config/windows/toggle_order";
289 FILE * file = try_fopen(path, "r", world, f_name);
290 uint16_t linemax = get_linemax(file, world, f_name);
291 char win_order[linemax + 1];
292 try_fgets(win_order, linemax + 1, file, world, f_name);
293 try_fclose(file, world, f_name);
295 for (; i < linemax - 1; i++)
297 toggle_window(world->wmeta, get_win_by_id(world, win_order[i]));
303 extern void reload_win_config(struct World * world)
305 while (0 != world->wmeta->active)
307 suspend_win(world->wmeta, world->wmeta->active);
310 free_winconfs(world);
311 init_winconfs(world);
313 sorted_wintoggle(world);
318 extern void save_win_configs(struct World * world)
320 char * f_name = "save_win_configs()";
322 save_win_config(world, 'i');
323 save_win_config(world, 'k');
324 save_win_config(world, 'l');
325 save_win_config(world, 'm');
327 char * path = "config/windows/toggle_order";
328 char * path_tmp = "config/windows/toggle_order_tmp";
329 FILE * file = try_fopen(path_tmp, "w", world, f_name);
332 struct Win * w_p = world->wmeta->_chain_start;
336 line[i] = get_id_by_win(world, w_p);
341 fwrite(line, sizeof(char), strlen(line), file);
343 try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
348 extern uint8_t toggle_window(struct WinMeta * win_meta, struct Win * win)
350 if (0 != win->frame.curses_win)
352 return suspend_win(win_meta, win);
356 return append_win(win_meta, win);
362 extern void toggle_winconfig(struct World * world, struct Win * win)
364 struct WinConf * wcp = get_winconf_by_win(world, win);
367 win->_draw = draw_winconf;
372 win->_draw = wcp->draw;
379 extern void toggle_win_height_type(struct World * world, struct Win * win)
381 struct WinConf * wcp = get_winconf_by_win(world, win);
382 if (0 == wcp->height_type)
384 wcp->height_type = 1;
388 wcp->height_type = 0;
390 set_winconf(world, wcp->id);
395 extern void toggle_win_width_type(struct World * world, struct Win * win)
397 struct WinConf * wcp = get_winconf_by_win(world, win);
398 if ( 0 == wcp->width_type
399 && win->frame.size.x <= world->wmeta->padframe.size.x)
407 set_winconf(world, wcp->id);
412 extern void scroll_pad(struct WinMeta * win_meta, char dir)
416 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
420 reset_pad_offset(win_meta, win_meta->pad_offset - 1);
426 extern uint8_t growshrink_active_window(struct World * world, char change)
428 if (0 != world->wmeta->active)
430 struct yx_uint16 size = world->wmeta->active->frame.size;
435 else if (change == '+')
439 else if (change == '_')
443 else if (change == '*')
447 uint8_t x = resize_active_win(world->wmeta, size);
448 struct WinConf * wcp = get_winconf_by_win(world, world->wmeta->active);
449 if ( 1 == wcp->width_type
450 && world->wmeta->active->frame.size.x
451 > world->wmeta->padframe.size.x)
455 set_winconf(world, wcp->id);