3 #include "wincontrol.h"
4 #include <stdlib.h> /* for malloc(), free() */
5 #include <string.h> /* for strlen() */
6 #include <stdint.h> /* for uint8_t, uint16_t */
7 #include <stdio.h> /* for fopen(), fclose(), 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 "misc.h" /* for textfile_sizes() */
16 #include "rexit.h" /* for exit_err() */
17 #include "main.h" /* for World, Wins structs */
18 #include "draw_wins.h" /* for draw_keys_win(), draw_info_win(), draw_log_win(),
24 /* Return string "prefix" + "id"; malloc()'s string, remember to call free()! */
25 static char * string_prefixed_id(struct World * world, char * prefix, char id);
29 /* Create Winconf, initialize ->iew/height_type/width_type to 0, ->id to "id"
32 static void create_winconf(char id, struct WinConf * wcp,
33 void (* f) (struct Win *));
35 /* Initialize Winconf of "id" from appropriate config file.*/
36 static void init_winconf_from_file(struct World * world, char id);
38 /* Wrapper around init_win() called with values from Winconf of "id". */
39 static void init_win_from_winconf(struct World * world, char id);
41 /* Save title, size of window identified by "id" to its configuration file. */
42 static void save_win_config(struct World * world, char id);
46 /* Write size of a window to its WinConf, as positive or negative values
47 * (dependent on state ofWinConf->height_type / WinConf->width_type).
49 static void set_winconf(struct World * world, char id);
53 /* Get WinConf by "id"; get id of WinConf mothering "win". */
54 static struct WinConf * get_winconf_by_id(struct World * world, char id);
55 static char get_id_by_win(struct World * world, struct Win * win);
59 static char * string_prefixed_id(struct World * world, char * prefix, char id)
61 char * err = "Trouble in string_prefixed_id() with malloc().";
62 uint8_t size = strlen(prefix) + 2;
63 char * path = malloc(size);
64 exit_err(NULL == path, world, err);
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 * err_m = "Trouble in init_win_from_file() with malloc().";
87 char * path = string_prefixed_id(world, "config/windows/Win_", id);
88 char * err = "Trouble in init_win_from_file() with fopen().";
89 FILE * file = fopen(path, "r");
90 exit_err(NULL == file, world, err);
93 err = "Trouble in init_win_from_file() with textfile_sizes().";
94 struct WinConf * winconf = get_winconf_by_id(world, id);
96 exit_err(textfile_sizes(file, &linemax, NULL), world, err);
97 char * line = malloc(linemax);
98 exit_err(NULL == line, world, err_m);
100 err = "Trouble in init_win_from_file() with fgets().";
101 exit_err(NULL == fgets(line, linemax, file), world, err);
102 winconf->title = malloc(strlen(line));
103 exit_err(NULL == winconf->title, world, err_m);
104 memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */
105 winconf->title[strlen(line) - 1] = '\0'; /* char at end of string. */
106 exit_err(NULL == fgets(line, linemax, file), world, err);
107 winconf->height = atoi(line);
108 if (0 >= winconf->height)
110 winconf->height_type = 1;
112 exit_err(NULL == fgets(line, linemax, file), world, err);
113 winconf->width = atoi(line);
114 if (0 >= winconf->width)
116 winconf->width_type = 1;
119 err = "Trouble in init_win_from_file() with fclose().";
120 exit_err(fclose(file), world, err);
125 static void init_win_from_winconf(struct World * world, char id)
127 struct WinConf * winconf = get_winconf_by_id(world, id);
128 char * err = "Trouble in init_win_from_file() with init_win().";
129 exit_err(init_win(world->wmeta, &winconf->win, winconf->title,
130 winconf->height, winconf->width, world, winconf->draw),
136 extern void save_win_config(struct World * world, char id)
138 char * err_o = "Trouble in save_win_config() with fopen().";
139 char * err_m = "Trouble in save_win_config() with malloc().";
140 char * err_c = "Trouble in save_win_config() with fclose().";
141 char * err_u = "Trouble in save_win_config() with unlink().";
142 char * err_r = "Trouble in save_win_config() with rename().";
144 char * path_tmp = string_prefixed_id(world, "config/windows/Win_tmp_", id);
145 FILE * file = fopen(path_tmp, "w");
146 exit_err(NULL == file, world, err_o);
148 struct WinConf * wc = get_winconf_by_id(world, id);
149 uint8_t size = strlen(wc->title) + 2;
154 char * line = malloc(size);
155 exit_err(NULL == line, world, err_m);
156 sprintf(line, "%s\n", wc->title);
157 fwrite(line, sizeof(char), strlen(line), file);
158 sprintf(line, "%d\n", wc->height);
159 fwrite(line, sizeof(char), strlen(line), file);
160 sprintf(line, "%d\n", wc->width);
161 fwrite(line, sizeof(char), strlen(line), file);
163 exit_err(fclose(file), world, err_c);
164 char * path = string_prefixed_id(world, "config/windows/Win_", id);
165 if (!access(path, F_OK))
167 exit_err(unlink(path), world, err_u);
169 exit_err(rename(path_tmp, path), world, err_r);
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 char get_id_by_win(struct World * world, struct Win * win)
217 struct WinConf * wc = get_winconf_by_win(world, win);
223 extern struct WinConf * get_winconf_by_win(struct World * world,
229 if (win == world->winconfs[i].win)
231 return &world->winconfs[i];
239 extern struct Win * get_win_by_id(struct World * world, char id)
241 struct WinConf * wc = get_winconf_by_id(world, id);
247 extern void create_winconfs(struct World * world)
249 char * err = "Trouble with malloc() in init_winconfs().";
250 struct WinConf * winconfs = malloc(4 * sizeof(struct WinConf));
251 exit_err(NULL == winconfs, world, err);
252 create_winconf('i', &winconfs[0], draw_info_win);
253 create_winconf('k', &winconfs[1], draw_keys_win);
254 create_winconf('l', &winconfs[2], draw_log_win);
255 create_winconf('m', &winconfs[3], draw_map_win);
256 world->winconfs = winconfs;
261 extern void init_winconfs(struct World * world)
263 init_winconf_from_file(world, 'i');
264 init_winconf_from_file(world, 'k');
265 init_winconf_from_file(world, 'l');
266 init_winconf_from_file(world, 'm');
271 extern void free_winconf(struct World * world, char id)
273 struct WinConf * wc = get_winconf_by_id(world, id);
279 extern void free_winconfs(struct World * world)
281 free_winconf(world, 'i');
282 free_winconf(world, 'k');
283 free_winconf(world, 'l');
284 free_winconf(world, 'm');
285 free(world->winconfs);
290 extern void init_wins(struct World * world)
292 init_win_from_winconf(world, 'i');
293 init_win_from_winconf(world, 'k');
294 init_win_from_winconf(world, 'l');
295 init_win_from_winconf(world, 'm');
300 extern void free_wins(struct World * world)
302 free_win(get_win_by_id(world, 'i'));
303 free_win(get_win_by_id(world, 'k'));
304 free_win(get_win_by_id(world, 'l'));
305 free_win(get_win_by_id(world, 'm'));
310 extern void sorted_wintoggle(struct World * world)
312 char * err = "Trouble in sorted_wintoggle() with fopen().";
313 FILE * file = fopen("config/windows/toggle_order", "r");
314 exit_err(NULL == file, world, err);
316 err = "Trouble in sorted_wintoggle() with textfile_sizes().";
317 exit_err(textfile_sizes(file, &linemax, NULL), world, err);
318 char win_order[linemax];
319 err = "Trouble in sorted_wintoggle() with fgets().";
320 exit_err(NULL == fgets(win_order, linemax, file), world, err);
321 err = "Trouble in sorted_wintoggle() with fclose().";
322 exit_err(fclose(file), world, err);
324 for (; i < linemax - 2; i++)
326 toggle_window(world->wmeta, get_win_by_id(world, win_order[i]));
332 extern void reload_win_config(struct World * world)
334 while (0 != world->wmeta->active)
336 suspend_win(world->wmeta, world->wmeta->active);
339 free_winconfs(world);
340 create_winconfs(world);
341 init_winconfs(world);
343 sorted_wintoggle(world);
348 extern void save_win_configs(struct World * world)
350 save_win_config(world, 'i');
351 save_win_config(world, 'k');
352 save_win_config(world, 'l');
353 save_win_config(world, 'm');
355 char * err_o = "Trouble in save_win_configs() with fopen().";
356 char * err_m = "Trouble in save_win_configs() with calloc().";
357 char * err_c = "Trouble in save_win_configs() with fclose().";
358 char * err_u = "Trouble in save_win_configs() with unlink().";
359 char * err_r = "Trouble in save_win_configs() with rename().";
361 char * path = "config/windows/toggle_order";
362 char * path_tmp = "config/windows/toggle_order_tmp";
363 FILE * file = fopen(path_tmp, "w");
364 exit_err(NULL == file, world, err_o);
366 char * line = calloc(6, sizeof(char));
367 exit_err(NULL == line, world, err_m);
368 struct Win * w_p = world->wmeta->_chain_start;
372 line[i] = get_id_by_win(world, w_p);
377 fwrite(line, sizeof(char), strlen(line), file);
379 exit_err(fclose(file), world, err_c);
380 if (!access(path, F_OK))
382 exit_err(unlink(path), world, err_u);
384 exit_err(rename(path_tmp, path), world, err_r);
389 extern uint8_t toggle_window(struct WinMeta * win_meta, struct Win * win)
391 if (0 != win->frame.curses_win)
393 return suspend_win(win_meta, win);
397 return append_win(win_meta, win);
403 extern void toggle_winconfig(struct World * world, struct Win * win)
405 struct WinConf * wcp = get_winconf_by_win(world, win);
408 win->_draw = draw_winconf;
413 win->_draw = wcp->draw;
420 extern void toggle_win_height_type(struct World * world, struct Win * win)
422 struct WinConf * wcp = get_winconf_by_win(world, win);
423 if (0 == wcp->height_type)
425 wcp->height_type = 1;
429 wcp->height_type = 0;
431 set_winconf(world, wcp->id);
436 extern void toggle_win_width_type(struct World * world, struct Win * win)
438 struct WinConf * wcp = get_winconf_by_win(world, win);
439 if ( 0 == wcp->width_type
440 && win->frame.size.x <= world->wmeta->padframe.size.x)
448 set_winconf(world, wcp->id);
453 extern void scroll_pad(struct WinMeta * win_meta, char dir)
457 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
461 reset_pad_offset(win_meta, win_meta->pad_offset - 1);
467 extern uint8_t growshrink_active_window(struct World * world, char change)
469 if (0 != world->wmeta->active)
471 struct yx_uint16 size = world->wmeta->active->frame.size;
476 else if (change == '+')
480 else if (change == '_')
484 else if (change == '*')
488 uint8_t x = resize_active_win(world->wmeta, size);
489 struct WinConf * wcp = get_winconf_by_win(world, world->wmeta->active);
490 if ( 1 == wcp->width_type
491 && world->wmeta->active->frame.size.x
492 > world->wmeta->padframe.size.x)
496 set_winconf(world, wcp->id);