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() */
27 /* Return string "prefix" + "id"; malloc()'s string, remember to call free()! */
28 static char * string_prefixed_id(struct World * world, char * prefix, char id);
32 /* Create Winconf, initialize ->view/height_type/width_type to 0, ->id to "id"
35 static void create_winconf(char id, struct WinConf * wcp,
36 void (* f) (struct Win *));
38 /* Initialize Winconf of "id" from appropriate config file.*/
39 static void init_winconf_from_file(struct World * world, char id);
41 /* Wrapper around init_win() called with values from Winconf of "id". */
42 static void init_win_from_winconf(struct World * world, char id);
44 /* Save title, size of window identified by "id" to its configuration file. */
45 static void save_win_config(struct World * world, char id);
49 /* Write size of a window to its WinConf, as positive or negative values
50 * (dependent on state ofWinConf->height_type / WinConf->width_type).
52 static void set_winconf(struct World * world, char id);
56 /* Get WinConf by "id"; get id of WinConf mothering "win". */
57 static struct WinConf * get_winconf_by_id(struct World * world, char id);
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 extern struct WinConf * get_winconf_by_win(struct World * world,
202 if (win == world->winconfs[i].win)
204 return &world->winconfs[i];
212 extern struct Win * get_win_by_id(struct World * world, char id)
214 struct WinConf * wc = get_winconf_by_id(world, id);
220 extern void init_winconfs(struct World * world)
222 char * f_name = "init_winconfs()";
223 struct WinConf * winconfs = try_malloc(4 * sizeof(struct WinConf),
225 create_winconf('i', &winconfs[0], draw_info_win);
226 create_winconf('k', &winconfs[1], draw_keys_win);
227 create_winconf('l', &winconfs[2], draw_log_win);
228 create_winconf('m', &winconfs[3], draw_map_win);
229 world->winconfs = winconfs;
230 init_winconf_from_file(world, 'i');
231 init_winconf_from_file(world, 'k');
232 init_winconf_from_file(world, 'l');
233 init_winconf_from_file(world, 'm');
238 extern void free_winconf(struct World * world, char id)
240 struct WinConf * wc = get_winconf_by_id(world, id);
246 extern void free_winconfs(struct World * world)
248 free_winconf(world, 'i');
249 free_winconf(world, 'k');
250 free_winconf(world, 'l');
251 free_winconf(world, 'm');
252 free(world->winconfs);
257 extern void init_wins(struct World * world)
259 init_win_from_winconf(world, 'i');
260 init_win_from_winconf(world, 'k');
261 init_win_from_winconf(world, 'l');
262 init_win_from_winconf(world, 'm');
267 extern void free_wins(struct World * world)
269 free_win(get_win_by_id(world, 'i'));
270 free_win(get_win_by_id(world, 'k'));
271 free_win(get_win_by_id(world, 'l'));
272 free_win(get_win_by_id(world, 'm'));
277 extern void sorted_wintoggle(struct World * world)
279 char * f_name = "sorted_wintoggle()";
280 char * path = "config/windows/toggle_order";
281 FILE * file = try_fopen(path, "r", world, f_name);
282 uint16_t linemax = get_linemax(file, world, f_name);
283 char win_order[linemax + 1];
284 try_fgets(win_order, linemax + 1, file, world, f_name);
285 try_fclose(file, world, f_name);
287 for (; i < linemax - 1; i++)
289 toggle_window(world->wmeta, get_win_by_id(world, win_order[i]));
295 extern void reload_win_config(struct World * world)
297 while (0 != world->wmeta->active)
299 suspend_win(world->wmeta, world->wmeta->active);
302 free_winconfs(world);
303 init_winconfs(world);
305 sorted_wintoggle(world);
310 extern void save_win_configs(struct World * world)
312 char * f_name = "save_win_configs()";
314 save_win_config(world, 'i');
315 save_win_config(world, 'k');
316 save_win_config(world, 'l');
317 save_win_config(world, 'm');
319 char * path = "config/windows/toggle_order";
320 char * path_tmp = "config/windows/toggle_order_tmp";
321 FILE * file = try_fopen(path_tmp, "w", world, f_name);
324 struct Win * w_p = world->wmeta->_chain_start;
328 struct WinConf * wc = get_winconf_by_win(world, w_p);
334 fwrite(line, sizeof(char), strlen(line), file);
336 try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
341 extern uint8_t toggle_window(struct WinMeta * win_meta, struct Win * win)
343 if (0 != win->frame.curses_win)
345 return suspend_win(win_meta, win);
349 return append_win(win_meta, win);
355 extern void toggle_winconfig(struct World * world, struct Win * win)
357 struct WinConf * wcp = get_winconf_by_win(world, win);
360 win->_draw = draw_winconf;
365 win->_draw = wcp->draw;
372 extern void toggle_win_height_type(struct World * world, struct Win * win)
374 struct WinConf * wcp = get_winconf_by_win(world, win);
375 if (0 == wcp->height_type)
377 wcp->height_type = 1;
381 wcp->height_type = 0;
383 set_winconf(world, wcp->id);
388 extern void toggle_win_width_type(struct World * world, struct Win * win)
390 struct WinConf * wcp = get_winconf_by_win(world, win);
391 if ( 0 == wcp->width_type
392 && win->frame.size.x <= world->wmeta->padframe.size.x)
400 set_winconf(world, wcp->id);
405 extern void scroll_pad(struct WinMeta * win_meta, char dir)
409 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
413 reset_pad_offset(win_meta, win_meta->pad_offset - 1);
419 extern uint8_t growshrink_active_window(struct World * world, char change)
421 if (0 != world->wmeta->active)
423 struct yx_uint16 size = world->wmeta->active->frame.size;
428 else if (change == '+')
432 else if (change == '_')
436 else if (change == '*')
440 uint8_t x = resize_active_win(world->wmeta, size);
441 struct WinConf * wcp = get_winconf_by_win(world, world->wmeta->active);
442 if ( 1 == wcp->width_type
443 && world->wmeta->active->frame.size.x
444 > world->wmeta->padframe.size.x)
448 set_winconf(world, wcp->id);