1 /* src/client/wincontrol.c */
3 #include "wincontrol.h"
4 #include <errno.h> /* global errno */
5 #include <stddef.h> /* NULL */
6 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
7 #include <stdio.h> /* FILE, sprintf() */
8 #include <stdlib.h> /* free(), atoi() */
9 #include <string.h> /* strlen(), strchr(), memcpy() */
10 #include "../common/readwrite.h" /* try_fgets(), try_fwrite(), try_fgetc(),
13 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
14 #include "../common/try_malloc.h" /* try_malloc() */
15 #include "../common/yx_uint16.h" /* struct yx_uint16 */
16 #include "draw_wins.h" /* draw_win_map(), draw_win_info(), draw_win_log(),
17 * draw_win_available_keybindings(),
18 * draw_win_inventory(), draw_win_keybindings_global(),
19 * draw_win_keybindings_winconf_geometry(),
20 * draw_win_keybindings_winconf_keybindings(),
21 * draw_winconf_geometry(), draw_winconf_keybindings()
23 #include "keybindings.h" /* struct KeyBinding, free_keybindings() */
24 #include "windows.h" /* struct Win, resize_active_win(), reset_pad_offset(),
25 * append_win(), suspend_win(), init_win(), free_win()
27 #include "world.h" /* global world */
31 /* Wrapper around init_win() called with values from Winconf of "id". */
32 static void init_win_from_winconf(char id);
34 /* Free data pointed to inside individual WinConf struct of "id". */
35 static void free_winconf_data(char id);
37 /* Write geometry of a window to its WinConf, as positive or negative values
38 * (dependent on state ofWinConf->height_type / WinConf->width_type).
40 static void set_winconf_geometry(char id);
42 /* Get WinConf by "id". */
43 static struct WinConf * get_winconf_by_id(char id);
45 /* Get (Win->draw) function identified by "c"; NULL if c not mapped to one. */
46 static void * get_drawfunc_by_char(char c);
50 static void free_winconf_data(char id)
52 struct WinConf * wc = get_winconf_by_id(id);
54 free_keybindings(wc->kb.kbs);
60 static void set_winconf_geometry(char id)
62 struct WinConf * wcp = get_winconf_by_id(id);
63 if (0 == wcp->height_type)
65 wcp->height = wcp->win->framesize.y;
67 else if (1 == wcp->height_type)
69 wcp->height = wcp->win->framesize.y - world.wins.padsize.y + 1;
71 if (0 == wcp->width_type)
73 wcp->width = wcp->win->framesize.x;
75 else if (1 == wcp->width_type)
77 wcp->width = wcp->win->framesize.x - world.wins.padsize.x;
83 static void * get_drawfunc_by_char(char c)
88 return draw_win_inventory;
100 return draw_win_available_keybindings;
108 return draw_win_keybindings_global;
112 return draw_win_keybindings_winconf_geometry;
116 return draw_win_keybindings_winconf_keybindings;
123 extern void init_win_from_winconf(char id)
125 char * err = "get_drawfunc_by_char() returns NULL to init_win_from_file().";
126 struct WinConf * winconf = get_winconf_by_id(id);
127 void * f = get_drawfunc_by_char(winconf->id);
128 exit_err(NULL == f, err);
129 init_win(&winconf->win, winconf->title, winconf->height, winconf->width, f);
134 static struct WinConf * get_winconf_by_id(char id)
139 if (id == world.wins.winconfs[i].id)
141 return &world.wins.winconfs[i];
149 extern struct WinConf * get_winconf_by_win(struct Win * win)
154 if (win == world.wins.winconfs[i].win)
156 return &world.wins.winconfs[i];
164 extern struct Win * get_win_by_id(char id)
166 struct WinConf * wc = get_winconf_by_id(id);
172 extern uint8_t read_winconf_from_file(char * line, uint32_t linemax,
175 char * f_name = "read_winconf_from_file()";
176 int test = try_fgetc(file, f_name);
181 struct WinConf winconf;
182 winconf.id = (char) test;
183 try_fgetc(file, f_name);
184 try_fgets(line, linemax + 1, file, f_name);
185 winconf.title = try_malloc(strlen(line), f_name);
186 memcpy(winconf.title, line, strlen(line) - 1); /* Eliminate newline char */
187 winconf.title[strlen(line) - 1] = '\0'; /* char at end of string. */
188 try_fgets(line, linemax + 1, file, f_name);
189 winconf.height = atoi(line);
190 winconf.height_type = (0 >= winconf.height);
191 try_fgets(line, linemax + 1, file, f_name);
192 winconf.width = atoi(line);
193 winconf.width_type = (0 >= winconf.width);
194 read_keybindings_from_file(line, linemax, file, &winconf.kb);
197 winconf.center.y = 0;
198 winconf.center.x = 0;
201 uint8_t old_ids_size = strlen(world.wins.ids);
202 char * new_ids = try_malloc(old_ids_size + 1 + 1, f_name);
203 sprintf(new_ids, "%s%c", world.wins.ids, winconf.id);
204 free(world.wins.ids);
205 world.wins.ids = new_ids;
206 uint16_t old_winconfs_size = old_ids_size * sizeof(struct WinConf);
207 uint16_t new_winconfs_size = old_winconfs_size + sizeof(struct WinConf);
208 struct WinConf * new_winconfs = try_malloc(new_winconfs_size, f_name);
209 memcpy(new_winconfs, world.wins.winconfs, old_winconfs_size);
210 new_winconfs[old_ids_size] = winconf;
211 free(world.wins.winconfs);
212 world.wins.winconfs = new_winconfs;
216 world.wins.ids = try_malloc(2, f_name);
217 sprintf(world.wins.ids, "%c", winconf.id);
218 world.wins.winconfs = try_malloc(sizeof(struct WinConf), f_name);
219 *world.wins.winconfs = winconf;
226 extern void write_winconf_of_id_to_file(FILE * file, char c, char * delim)
228 char * f_name = "write_winconf_of_id_to_file()";
229 struct WinConf * wc = get_winconf_by_id(c);
230 uint8_t size = strlen(wc->title) + 2;
231 if (size < 7) /* Ensure that at least 5 + 2 char fit into line so that */
232 { /* the digit representation of any uint16_t may be stored. */
236 sprintf(line, "%c\n", wc->id);
237 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
238 sprintf(line, "%s\n", wc->title);
239 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
240 sprintf(line, "%d\n", wc->height);
241 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
242 sprintf(line, "%d\n", wc->width);
243 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
244 write_keybindings_to_file(file, &wc->kb, delim);
249 extern void read_order_wins_visible_active(char * line, uint32_t linemax,
252 char * f_name = "read_order_wins_visible_active()";
253 char win_order[linemax + 1];
254 try_fgets(win_order, linemax + 1, file, f_name);
255 world.wins.order = try_malloc(linemax, f_name);
256 win_order[strlen(win_order) - 1] = '\0';
257 sprintf(world.wins.order, "%s", win_order);
258 int char_or_eof = try_fgetc(file, f_name);
259 char * err_eof = "fgetc() unexpectedly hitting EOF";
260 exit_trouble(EOF == char_or_eof, f_name, err_eof);
261 world.wins.id_active = (uint8_t) char_or_eof;
262 exit_trouble(EOF == try_fgetc(file, f_name), f_name, err_eof);
263 try_fgets(line, linemax + 1, file, f_name);
268 extern void write_order_wins_visible_active(FILE * file, char * delim)
270 char * f_name = "write_order_wins_visible_active()";
271 char line[strlen(world.wins.ids) + 2];
272 struct Win * w_p = world.wins.chain_start;
275 for (; NULL != w_p; w_p = w_p->next, i++)
277 struct WinConf * wc_p = get_winconf_by_win(w_p);
279 if (w_p == world.wins.win_active)
286 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
287 try_fputc(active, file, f_name);
288 try_fputc('\n', file, f_name);
289 try_fwrite(delim, strlen(delim), 1, file, f_name);
294 extern void free_winconfs()
297 while (0 != (id = get_next_winconf_id()))
299 free_winconf_data(id);
301 free(world.wins.ids);
302 world.wins.ids = NULL;
303 free(world.wins.winconfs);
304 world.wins.winconfs = NULL;
305 free(world.wins.order);
306 world.wins.order = NULL;
311 extern void init_wins()
314 while (0 != (id = get_next_winconf_id()))
316 init_win_from_winconf(id);
322 extern void sorted_win_toggle_and_activate()
325 for (; i < strlen(world.wins.order); i++)
327 if (NULL == strchr(world.wins.ids, world.wins.order[i]))
331 toggle_window(world.wins.order[i]);
332 if (world.wins.id_active == (uint8_t) world.wins.order[i])
334 world.wins.win_active = get_win_by_id(world.wins.order[i]);
340 extern char get_next_winconf_id()
342 static uint8_t i = 0;
343 char c = world.wins.ids[i];
355 extern void toggle_window(char id)
357 struct Win * win = get_win_by_id(id);
358 if (0 == win->prev && world.wins.chain_start != win) /* Win struct is */
359 { /* outside chain? */
370 extern void toggle_winconfig()
372 struct Win * win = world.wins.win_active;
373 struct WinConf * wcp = get_winconf_by_win(win);
377 win->draw = draw_winconf_geometry;
378 wcp->center = win->center;
382 else if (1 == wcp->view)
385 win->draw = draw_winconf_keybindings;
391 win->draw = get_drawfunc_by_char(wcp->id);
392 win->center = wcp->center;
398 extern void toggle_win_size_type(char axis)
400 struct Win * win = world.wins.win_active;
401 struct WinConf * wcp = get_winconf_by_win(win);
404 wcp->height_type = (0 == wcp->height_type);
405 set_winconf_geometry(wcp->id);
408 wcp->width_type = ( 0 == wcp->width_type
409 && win->framesize.x <= world.wins.padsize.x);
410 set_winconf_geometry(wcp->id);
415 extern void scroll_pad(char dir)
419 reset_pad_offset(world.wins.pad_offset + 1);
423 reset_pad_offset(world.wins.pad_offset - 1);
429 extern void growshrink_active_window(char change)
431 if (0 != world.wins.win_active)
433 struct yx_uint16 size = world.wins.win_active->framesize;
438 else if (change == '+')
442 else if (change == '_')
446 else if (change == '*')
450 resize_active_win(size);
451 struct WinConf * wcp = get_winconf_by_win(world.wins.win_active);
452 if ( 1 == wcp->width_type
453 && world.wins.win_active->framesize.x > world.wins.padsize.x)
457 set_winconf_geometry(wcp->id);