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 "windows.h" /* for suspend_win(), append_win(), reset_pad_offset(),
8 * resize_active_win(), init_win(), free_win(),
11 #include "yx_uint16.h" /* for yx_uint16 struct */
12 #include "main.h" /* for Wins struct */
13 #include "misc.h" /* for textfile_sizes() */
14 #include "rexit.h" /* for exit_err() */
15 #include "main.h" /* for World, Wins structs */
16 #include "draw_wins.h" /* for draw_keys_win(), draw_info_win(), draw_log_win(),
22 /* Initialize Winconf of "id" from appropriate config file.*/
23 static void init_winconf_from_file(struct World * world, char id);
25 /* Wrapper around init_win() called with values from appropriate Winconf. */
26 static void init_win_from_winconf(struct World * world, char id);
30 static void init_winconf_from_file(struct World * world, char id)
32 char * err_m = "Trouble in init_win_from_file() with malloc().";
33 char * prefix = "config/windows/Win_";
34 uint8_t size = strlen(prefix) + 2;
35 char * path = malloc(size);
36 exit_err(NULL == path, world, err_m);
37 sprintf(path, "%s_", prefix);
39 char * err = "Trouble in init_win_from_file() with fopen().";
40 FILE * file = fopen(path, "r");
41 exit_err(NULL == file, world, err);
44 struct WinConf * winconf = get_winconf_by_id(world, id);
46 err = "Trouble in init_win_from_file() with textfile_sizes().";
47 exit_err(textfile_sizes(file, &linemax, NULL), world, err);
48 char * line = malloc(linemax);
49 exit_err(NULL == line, world, err_m);
50 err = "Trouble in init_win_from_file() with fgets().";
52 exit_err(NULL == fgets(line, linemax, file), world, err);
53 winconf->title = malloc(strlen(line));
54 exit_err(NULL == winconf->title, world, err_m);
55 memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */
56 winconf->title[strlen(line) - 1] = '\0'; /* char at end of string. */
57 exit_err(NULL == fgets(line, linemax, file), world, err);
58 winconf->height = atoi(line);
59 exit_err(NULL == fgets(line, linemax, file), world, err);
60 winconf->width = atoi(line);
63 err = "Trouble in init_win_from_file() with fclose().";
64 exit_err(fclose(file), world, err);
69 static void init_win_from_winconf(struct World * world, char id)
71 struct WinConf * winconf = get_winconf_by_id(world, id);
72 char * err = "Trouble in init_win_from_file() with init_win().";
73 exit_err(init_win(world->wmeta, &winconf->win, winconf->title,
74 winconf->height, winconf->width, world, winconf->draw),
80 extern void create_winconfs(struct World * world)
82 char * err = "Trouble with malloc() in init_winconfs().";
83 struct WinConf * winconfs = malloc(4 * sizeof(struct WinConf));
84 exit_err(NULL == winconfs, world, err);
86 winconfs[0].draw = draw_info_win;
88 winconfs[1].draw = draw_keys_win;
90 winconfs[2].draw = draw_log_win;
92 winconfs[3].draw = draw_map_win;
93 world->winconfs = winconfs;
98 extern void init_winconfs(struct World * world)
100 init_winconf_from_file(world, 'i');
101 init_winconf_from_file(world, 'k');
102 init_winconf_from_file(world, 'l');
103 init_winconf_from_file(world, 'm');
108 extern void free_winconf(struct World * world, char id)
110 struct WinConf * wc = get_winconf_by_id(world, id);
116 extern void free_winconfs(struct World * world)
118 free_winconf(world, 'i');
119 free_winconf(world, 'k');
120 free_winconf(world, 'l');
121 free_winconf(world, 'm');
122 free(world->winconfs);
127 extern void init_wins(struct World * world)
129 init_win_from_winconf(world, 'i');
130 init_win_from_winconf(world, 'k');
131 init_win_from_winconf(world, 'l');
132 init_win_from_winconf(world, 'm');
137 extern void free_wins(struct World * world)
139 free_win(get_win_by_id(world, 'i'));
140 free_win(get_win_by_id(world, 'k'));
141 free_win(get_win_by_id(world, 'l'));
142 free_win(get_win_by_id(world, 'm'));
147 extern void reload_win_config(struct World * world)
149 while (0 != world->wmeta->active)
151 suspend_win(world->wmeta, world->wmeta->active);
154 free_winconfs(world);
155 create_winconfs(world);
156 init_winconfs(world);
158 sorted_wintoggle(world);
163 extern struct WinConf * get_winconf_by_id(struct World * world, char id)
168 if (id == world->winconfs[i].id)
170 return &world->winconfs[i];
178 extern struct Win * get_win_by_id(struct World * world, char id)
180 struct WinConf * wc = get_winconf_by_id(world, id);
186 extern void sorted_wintoggle(struct World * world)
188 char * err = "Trouble in sorted_wintoggle() with fopen().";
189 FILE * file = fopen("config/windows/toggle_order", "r");
190 exit_err(NULL == file, world, err);
192 err = "Trouble in sorted_wintoggle() with textfile_sizes().";
193 exit_err(textfile_sizes(file, &linemax, NULL), world, err);
194 char win_order[linemax];
195 err = "Trouble in sorted_wintoggle() with fgets().";
196 exit_err(NULL == fgets(win_order, linemax, file), world, err);
197 err = "Trouble in sorted_wintoggle() with fclose().";
198 exit_err(fclose(file), world, err);
200 for (; i < linemax - 2; i++)
202 toggle_window(world->wmeta, get_win_by_id(world, win_order[i]));
209 extern uint8_t toggle_window(struct WinMeta * win_meta, struct Win * win)
211 if (0 != win->frame.curses_win)
213 return suspend_win(win_meta, win);
217 return append_win(win_meta, win);
223 extern void scroll_pad(struct WinMeta * win_meta, char dir)
227 reset_pad_offset(win_meta, win_meta->pad_offset + 1);
231 reset_pad_offset(win_meta, win_meta->pad_offset - 1);
237 extern uint8_t growshrink_active_window(struct WinMeta * win_meta, char change)
239 if (0 != win_meta->active)
241 struct yx_uint16 size = win_meta->active->frame.size;
246 else if (change == '+')
250 else if (change == '_')
254 else if (change == '*')
258 return resize_active_win (win_meta, size);