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.wmeta.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.wmeta.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->draw);
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.winconf_db.winconfs[i].id)
141 return &world.winconf_db.winconfs[i];
149 extern struct WinConf * get_winconf_by_win(struct Win * win)
154 if (win == world.winconf_db.winconfs[i].win)
156 return &world.winconf_db.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, FILE * file)
174 char * f_name = "read_winconf_from_file()";
175 int test = try_fgetc(file, f_name);
180 struct WinConf winconf;
181 winconf.id = (char) test;
182 winconf.draw = winconf.id;
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;
199 if (world.winconf_db.ids)
201 uint8_t old_ids_size = strlen(world.winconf_db.ids);
202 char * new_ids = try_malloc(old_ids_size + 1 + 1, f_name);
203 sprintf(new_ids, "%s%c", world.winconf_db.ids, winconf.id);
204 free(world.winconf_db.ids);
205 world.winconf_db.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.winconf_db.winconfs, old_winconfs_size);
210 new_winconfs[old_ids_size] = winconf;
211 free(world.winconf_db.winconfs);
212 world.winconf_db.winconfs = new_winconfs;
216 world.winconf_db.ids = try_malloc(2, f_name);
217 sprintf(world.winconf_db.ids, "%c", winconf.id);
218 world.winconf_db.winconfs = try_malloc(sizeof(struct WinConf), f_name);
219 *world.winconf_db.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->draw);
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, FILE * file)
251 char * f_name = "read_order_wins_visible_active()";
252 char win_order[linemax + 1];
253 try_fgets(win_order, linemax + 1, file, f_name);
254 world.winconf_db.order = try_malloc(linemax, f_name);
255 win_order[strlen(win_order) - 1] = '\0';
256 sprintf(world.winconf_db.order, "%s", win_order);
257 int char_or_eof = try_fgetc(file, f_name);
258 char * err_eof = "fgetc() unexpectedly hitting EOF";
259 exit_trouble(EOF == char_or_eof, f_name, err_eof);
260 world.winconf_db.active = (uint8_t) char_or_eof;
261 exit_trouble(EOF == try_fgetc(file, f_name), f_name, err_eof);
262 try_fgets(line, linemax + 1, file, f_name);
267 extern void write_order_wins_visible_active(FILE * file, char * delim)
269 char * f_name = "write_order_wins_visible_active()";
270 char line[strlen(world.winconf_db.ids) + 2];
271 struct Win * w_p = world.wmeta.chain_start;
274 for (; NULL != w_p; w_p = w_p->next, i++)
276 struct WinConf * wc_p = get_winconf_by_win(w_p);
278 if (w_p == world.wmeta.active)
285 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
286 try_fputc(active, file, f_name);
287 try_fputc('\n', file, f_name);
288 try_fwrite(delim, strlen(delim), 1, file, f_name);
293 extern void free_winconfs()
296 while (0 != (id = get_next_winconf_id()))
298 free_winconf_data(id);
300 free(world.winconf_db.ids);
301 world.winconf_db.ids = NULL;
302 free(world.winconf_db.winconfs);
303 world.winconf_db.winconfs = NULL;
304 free(world.winconf_db.order);
305 world.winconf_db.order = NULL;
310 extern void init_wins()
313 while (0 != (id = get_next_winconf_id()))
315 init_win_from_winconf(id);
321 extern void sorted_win_toggle_and_activate()
324 for (; i < strlen(world.winconf_db.order); i++)
326 if (NULL == strchr(world.winconf_db.ids, world.winconf_db.order[i]))
330 toggle_window(world.winconf_db.order[i]);
331 if (world.winconf_db.active == (uint8_t) world.winconf_db.order[i])
333 world.wmeta.active = get_win_by_id(world.winconf_db.order[i]);
339 extern char get_next_winconf_id()
341 static uint8_t i = 0;
342 char c = world.winconf_db.ids[i];
354 extern void toggle_window(char id)
356 struct Win * win = get_win_by_id(id);
357 if (0 == win->prev && world.wmeta.chain_start != win) /* Win struct is */
358 { /* outside chain? */
369 extern void toggle_winconfig()
371 struct Win * win = world.wmeta.active;
372 struct WinConf * wcp = get_winconf_by_win(win);
376 win->draw = draw_winconf_geometry;
377 wcp->center = win->center;
381 else if (1 == wcp->view)
384 win->draw = draw_winconf_keybindings;
390 win->draw = get_drawfunc_by_char(wcp->draw);
391 win->center = wcp->center;
397 extern void toggle_win_size_type(char axis)
399 struct Win * win = world.wmeta.active;
400 struct WinConf * wcp = get_winconf_by_win(win);
403 wcp->height_type = (0 == wcp->height_type);
404 set_winconf_geometry(wcp->id);
407 wcp->width_type = ( 0 == wcp->width_type
408 && win->framesize.x <= world.wmeta.padsize.x);
409 set_winconf_geometry(wcp->id);
414 extern void scroll_pad(char dir)
418 reset_pad_offset(world.wmeta.pad_offset + 1);
422 reset_pad_offset(world.wmeta.pad_offset - 1);
428 extern void growshrink_active_window(char change)
430 if (0 != world.wmeta.active)
432 struct yx_uint16 size = world.wmeta.active->framesize;
437 else if (change == '+')
441 else if (change == '_')
445 else if (change == '*')
449 resize_active_win(size);
450 struct WinConf * wcp = get_winconf_by_win(world.wmeta.active);
451 if ( 1 == wcp->width_type
452 && world.wmeta.active->framesize.x > world.wmeta.padsize.x)
456 set_winconf_geometry(wcp->id);