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);
58 static char get_id_by_win(struct World * world, struct Win * win);
62 static char * string_prefixed_id(struct World * world, char * prefix, char id)
64 uint8_t size = strlen(prefix) + 2;
65 char * path = try_malloc(size, world, "string_prefixed_id()");
66 sprintf(path, "%s_", prefix);
73 static void create_winconf(char id, struct WinConf * wcp,
74 void (* f) (struct Win *))
85 static void init_winconf_from_file(struct World * world, char id)
87 char * f_name = "init_winconf_from_file()";
89 char * path = string_prefixed_id(world, "config/windows/Win_", id);
90 FILE * file = try_fopen(path, "r", world, f_name);
92 uint16_t linemax = get_linemax(file, world, f_name);
93 char line[linemax + 1];
95 struct WinConf * winconf = get_winconf_by_id(world, id);
96 try_fgets(line, linemax + 1, file, world, f_name);
97 winconf->title = try_malloc(strlen(line), world, f_name);
98 memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */
99 winconf->title[strlen(line) - 1] = '\0'; /* char at end of string. */
100 try_fgets(line, linemax + 1, file, world, f_name);
101 winconf->height = atoi(line);
102 if (0 >= winconf->height)
104 winconf->height_type = 1;
106 try_fgets(line, linemax + 1, file, world, f_name);
107 winconf->width = atoi(line);
108 if (0 >= winconf->width)
110 winconf->width_type = 1;
113 try_fclose(file, world, f_name);
118 static void init_win_from_winconf(struct World * world, char id)
120 char * err = "Trouble in init_win_from_file() with init_win().";
121 struct WinConf * winconf = get_winconf_by_id(world, id);
122 exit_err(init_win(world->wmeta, &winconf->win, winconf->title,
123 winconf->height, winconf->width, world, winconf->draw),
129 extern void save_win_config(struct World * world, char id)
131 char * f_name = "save_win_config()";
133 char * path_tmp = string_prefixed_id(world, "config/windows/Win_tmp_", id);
134 FILE * file = try_fopen(path_tmp, "w", world, f_name);
136 struct WinConf * wc = get_winconf_by_id(world, id);
137 uint8_t size = strlen(wc->title) + 2;
143 sprintf(line, "%s\n", wc->title);
144 fwrite(line, sizeof(char), strlen(line), file);
145 sprintf(line, "%d\n", wc->height);
146 fwrite(line, sizeof(char), strlen(line), file);
147 sprintf(line, "%d\n", wc->width);
148 fwrite(line, sizeof(char), strlen(line), file);
150 char * path = string_prefixed_id(world, "config/windows/Win_", id);
151 try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
158 static void set_winconf(struct World * world, char id)
160 struct WinConf * wcp = get_winconf_by_id(world, id);
161 if (0 == wcp->height_type)
163 wcp->height = wcp->win->frame.size.y;
165 else if (1 == wcp->height_type)
167 wcp->height = wcp->win->frame.size.y - world->wmeta->padframe.size.y
170 if (0 == wcp->width_type)
172 wcp->width = wcp->win->frame.size.x;
174 else if (1 == wcp->width_type)
176 wcp->width = wcp->win->frame.size.x - world->wmeta->padframe.size.x;
182 static struct WinConf * get_winconf_by_id(struct World * world, char id)
187 if (id == world->winconfs[i].id)
189 return &world->winconfs[i];
197 static char get_id_by_win(struct World * world, struct Win * win)
199 struct WinConf * wc = get_winconf_by_win(world, win);
205 extern struct WinConf * get_winconf_by_win(struct World * world,
211 if (win == world->winconfs[i].win)
213 return &world->winconfs[i];
221 extern struct Win * get_win_by_id(struct World * world, char id)
223 struct WinConf * wc = get_winconf_by_id(world, id);
229 extern void init_winconfs(struct World * world)
231 char * f_name = "init_winconfs()";
232 struct WinConf * winconfs = try_malloc(4 * sizeof(struct WinConf),
234 create_winconf('i', &winconfs[0], draw_info_win);
235 create_winconf('k', &winconfs[1], draw_keys_win);
236 create_winconf('l', &winconfs[2], draw_log_win);
237 create_winconf('m', &winconfs[3], draw_map_win);
238 world->winconfs = winconfs;
239 init_winconf_from_file(world, 'i');
240 init_winconf_from_file(world, 'k');
241 init_winconf_from_file(world, 'l');
242 init_winconf_from_file(world, 'm');
247 extern void free_winconf(struct World * world, char id)
249 struct WinConf * wc = get_winconf_by_id(world, id);
255 extern void free_winconfs(struct World * world)
257 free_winconf(world, 'i');
258 free_winconf(world, 'k');
259 free_winconf(world, 'l');
260 free_winconf(world, 'm');
261 free(world->winconfs);
266 extern void init_wins(struct World * world)
268 init_win_from_winconf(world, 'i');
269 init_win_from_winconf(world, 'k');
270 init_win_from_winconf(world, 'l');
271 init_win_from_winconf(world, 'm');
276 extern void free_wins(struct World * world)
278 free_win(get_win_by_id(world, 'i'));
279 free_win(get_win_by_id(world, 'k'));
280 free_win(get_win_by_id(world, 'l'));
281 free_win(get_win_by_id(world, 'm'));
286 extern void sorted_wintoggle(struct World * world)
288 char * f_name = "sorted_wintoggle()";
289 char * path = "config/windows/toggle_order";
290 FILE * file = try_fopen(path, "r", world, f_name);
291 uint16_t linemax = get_linemax(file, world, f_name);
292 char win_order[linemax + 1];
293 try_fgets(win_order, linemax + 1, file, world, f_name);
294 try_fclose(file, world, f_name);
296 for (; i < linemax - 1; i++)
298 toggle_window(world->wmeta, get_win_by_id(world, win_order[i]));
304 extern void reload_win_config(struct World * world)
306 while (0 != world->wmeta->active)
308 suspend_win(world->wmeta, world->wmeta->active);
311 free_winconfs(world);
312 init_winconfs(world);
314 sorted_wintoggle(world);
319 extern void save_win_configs(struct World * world)
321 char * f_name = "save_win_configs()";
323 save_win_config(world, 'i');
324 save_win_config(world, 'k');
325 save_win_config(world, 'l');
326 save_win_config(world, 'm');
328 char * path = "config/windows/toggle_order";
329 char * path_tmp = "config/windows/toggle_order_tmp";
330 FILE * file = try_fopen(path_tmp, "w", world, f_name);
333 struct Win * w_p = world->wmeta->_chain_start;
337 line[i] = get_id_by_win(world, w_p);
342 fwrite(line, sizeof(char), strlen(line), file);
344 try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
349 extern uint8_t toggle_window(struct WinMeta * win_meta, struct Win * win)
351 if (0 != win->frame.curses_win)
353 return suspend_win(win_meta, win);
357 return append_win(win_meta, win);
363 extern void toggle_winconfig(struct World * world, struct Win * win)
365 struct WinConf * wcp = get_winconf_by_win(world, win);
368 win->_draw = draw_winconf;
373 win->_draw = wcp->draw;
380 extern void toggle_win_height_type(struct World * world, struct Win * win)
382 struct WinConf * wcp = get_winconf_by_win(world, win);
383 if (0 == wcp->height_type)
385 wcp->height_type = 1;
389 wcp->height_type = 0;
391 set_winconf(world, wcp->id);
396 extern void toggle_win_width_type(struct World * world, struct Win * win)
398 struct WinConf * wcp = get_winconf_by_win(world, win);
399 if ( 0 == wcp->width_type
400 && win->frame.size.x <= world->wmeta->padframe.size.x)
408 set_winconf(world, wcp->id);
413 extern void scroll_pad(struct WinMeta * win_meta, char dir)
417 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
421 reset_pad_offset(win_meta, win_meta->pad_offset - 1);
427 extern uint8_t growshrink_active_window(struct World * world, char change)
429 if (0 != world->wmeta->active)
431 struct yx_uint16 size = world->wmeta->active->frame.size;
436 else if (change == '+')
440 else if (change == '_')
444 else if (change == '*')
448 uint8_t x = resize_active_win(world->wmeta, size);
449 struct WinConf * wcp = get_winconf_by_win(world, world->wmeta->active);
450 if ( 1 == wcp->width_type
451 && world->wmeta->active->frame.size.x
452 > world->wmeta->padframe.size.x)
456 set_winconf(world, wcp->id);