home · contact · privacy
Optimized WinConf / WinConfDB structure, removed redundant .draw.
[plomrogue] / src / client / wincontrol.c
1 /* src/client/wincontrol.c */
2
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(),
11                                   * try_fputc()
12                                   */
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()
22                         */
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()
26                       */
27 #include "world.h" /* global world */
28
29
30
31 /* Wrapper around init_win() called with values from Winconf of "id". */
32 static void init_win_from_winconf(char id);
33
34 /* Free data pointed to inside individual WinConf struct of "id". */
35 static void free_winconf_data(char id);
36
37 /* Write geometry of a window to its WinConf, as positive or negative values
38  * (dependent on state ofWinConf->height_type / WinConf->width_type).
39  */
40 static void set_winconf_geometry(char id);
41
42 /* Get WinConf by "id". */
43 static struct WinConf * get_winconf_by_id(char id);
44
45 /* Get (Win->draw) function identified by "c"; NULL if c not mapped to one. */
46 static void * get_drawfunc_by_char(char c);
47
48
49
50 static void free_winconf_data(char id)
51 {
52     struct WinConf * wc = get_winconf_by_id(id);
53     free(wc->title);
54     free_keybindings(wc->kb.kbs);
55     free_win(wc->win);
56 }
57
58
59
60 static void set_winconf_geometry(char id)
61 {
62     struct WinConf * wcp = get_winconf_by_id(id);
63     if      (0 == wcp->height_type)
64     {
65         wcp->height = wcp->win->framesize.y;
66     }
67     else if (1 == wcp->height_type)
68     {
69         wcp->height = wcp->win->framesize.y - world.wmeta.padsize.y + 1;
70     }
71     if      (0 == wcp->width_type)
72     {
73         wcp->width = wcp->win->framesize.x;
74     }
75     else if (1 == wcp->width_type)
76     {
77         wcp->width = wcp->win->framesize.x - world.wmeta.padsize.x;
78     }
79 }
80
81
82
83 static void * get_drawfunc_by_char(char c)
84 {
85
86     if      ('c' == c)
87     {
88         return draw_win_inventory;
89     }
90     else if ('i' == c)
91     {
92         return draw_win_info;
93     }
94     else if ('l' == c)
95     {
96         return draw_win_log;
97     }
98     else if ('k' == c)
99     {
100         return draw_win_available_keybindings;
101     }
102     else if ('m' == c)
103     {
104         return draw_win_map;
105     }
106     else if ('0' == c)
107     {
108         return draw_win_keybindings_global;
109     }
110     else if ('1' == c)
111     {
112         return draw_win_keybindings_winconf_geometry;
113     }
114     else if ('2' == c)
115     {
116         return draw_win_keybindings_winconf_keybindings;
117     }
118     return NULL;
119 }
120
121
122
123 extern void init_win_from_winconf(char id)
124 {
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);
130 }
131
132
133
134 static struct WinConf * get_winconf_by_id(char id)
135 {
136     uint8_t i = 0;
137     while (1)
138     {
139         if (id == world.winconf_db.winconfs[i].id)
140         {
141             return &world.winconf_db.winconfs[i];
142         }
143         i++;
144     }
145 }
146
147
148
149 extern struct WinConf * get_winconf_by_win(struct Win * win)
150 {
151     uint8_t i = 0;
152     while (1)
153     {
154         if (win == world.winconf_db.winconfs[i].win)
155         {
156             return &world.winconf_db.winconfs[i];
157         }
158         i++;
159     }
160 }
161
162
163
164 extern struct Win * get_win_by_id(char id)
165 {
166     struct WinConf * wc = get_winconf_by_id(id);
167     return wc->win;
168 }
169
170
171
172 extern uint8_t read_winconf_from_file(char * line, uint32_t linemax, FILE * file)
173 {
174     char * f_name = "read_winconf_from_file()";
175     int test = try_fgetc(file, f_name);
176     if (EOF == test)
177     {
178         return 0;
179     }
180     struct WinConf winconf;
181     winconf.id = (char) test;
182     try_fgetc(file, f_name);
183     try_fgets(line, linemax + 1, file, f_name);
184     winconf.title = try_malloc(strlen(line), f_name);
185     memcpy(winconf.title, line, strlen(line) - 1);  /* Eliminate newline char */
186     winconf.title[strlen(line) - 1] = '\0';         /* char at end of string. */
187     try_fgets(line, linemax + 1, file, f_name);
188     winconf.height = atoi(line);
189     winconf.height_type = (0 >= winconf.height);
190     try_fgets(line, linemax + 1, file, f_name);
191     winconf.width = atoi(line);
192     winconf.width_type = (0 >= winconf.width);
193     read_keybindings_from_file(line, linemax, file, &winconf.kb);
194     winconf.win = NULL;
195     winconf.view = 0;
196     winconf.center.y = 0;
197     winconf.center.x = 0;
198     if (world.winconf_db.ids)
199     {
200         uint8_t old_ids_size = strlen(world.winconf_db.ids);
201         char * new_ids = try_malloc(old_ids_size + 1 + 1, f_name);
202         sprintf(new_ids, "%s%c", world.winconf_db.ids, winconf.id);
203         free(world.winconf_db.ids);
204         world.winconf_db.ids = new_ids;
205         uint16_t old_winconfs_size = old_ids_size * sizeof(struct WinConf);
206         uint16_t new_winconfs_size = old_winconfs_size + sizeof(struct WinConf);
207         struct WinConf * new_winconfs = try_malloc(new_winconfs_size, f_name);
208         memcpy(new_winconfs, world.winconf_db.winconfs, old_winconfs_size);
209         new_winconfs[old_ids_size] = winconf;
210         free(world.winconf_db.winconfs);
211         world.winconf_db.winconfs = new_winconfs;
212     }
213     else
214     {
215         world.winconf_db.ids = try_malloc(2, f_name);
216         sprintf(world.winconf_db.ids, "%c", winconf.id);
217         world.winconf_db.winconfs = try_malloc(sizeof(struct WinConf), f_name);
218         *world.winconf_db.winconfs = winconf;
219     }
220     return 1;
221 }
222
223
224
225 extern void write_winconf_of_id_to_file(FILE * file, char c, char * delim)
226 {
227     char * f_name = "write_winconf_of_id_to_file()";
228     struct WinConf * wc = get_winconf_by_id(c);
229     uint8_t size = strlen(wc->title) + 2;
230     if (size < 7)  /* Ensure that at least 5 + 2 char fit into line so that   */
231     {              /* the digit representation of any uint16_t may be stored. */
232         size = 7;
233     }
234     char line[size];
235     sprintf(line, "%c\n", wc->id);
236     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
237     sprintf(line, "%s\n", wc->title);
238     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
239     sprintf(line, "%d\n", wc->height);
240     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
241     sprintf(line, "%d\n", wc->width);
242     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
243     write_keybindings_to_file(file, &wc->kb, delim);
244 }
245
246
247
248 extern void read_order_wins_visible_active(char * line, uint32_t linemax, FILE * file)
249 {
250     char * f_name = "read_order_wins_visible_active()";
251     char win_order[linemax + 1];
252     try_fgets(win_order, linemax + 1, file, f_name);
253     world.winconf_db.order = try_malloc(linemax, f_name);
254     win_order[strlen(win_order) - 1] = '\0';
255     sprintf(world.winconf_db.order, "%s", win_order);
256     int char_or_eof = try_fgetc(file, f_name);
257     char * err_eof = "fgetc() unexpectedly hitting EOF";
258     exit_trouble(EOF == char_or_eof, f_name, err_eof);
259     world.winconf_db.active = (uint8_t) char_or_eof;
260     exit_trouble(EOF == try_fgetc(file, f_name), f_name, err_eof);
261     try_fgets(line, linemax + 1, file, f_name);
262 }
263
264
265
266 extern void write_order_wins_visible_active(FILE * file, char * delim)
267 {
268     char * f_name = "write_order_wins_visible_active()";
269     char line[strlen(world.winconf_db.ids) + 2];
270     struct Win * w_p = world.wmeta.chain_start;
271     char active = ' ';
272     uint8_t i;
273     for (; NULL != w_p; w_p = w_p->next, i++)
274     {
275         struct WinConf * wc_p = get_winconf_by_win(w_p);
276         line[i] = wc_p->id;
277         if (w_p == world.wmeta.active)
278         {
279             active = wc_p->id;
280         }
281     }
282     line[i] = '\n';
283     line[i + 1] = '\0';
284     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
285     try_fputc(active, file, f_name);
286     try_fputc('\n', file, f_name);
287     try_fwrite(delim, strlen(delim), 1, file, f_name);
288 }
289
290
291
292 extern void free_winconfs()
293 {
294     char id;
295     while (0 != (id = get_next_winconf_id()))
296     {
297         free_winconf_data(id);
298     }
299     free(world.winconf_db.ids);
300     world.winconf_db.ids = NULL;
301     free(world.winconf_db.winconfs);
302     world.winconf_db.winconfs = NULL;
303     free(world.winconf_db.order);
304     world.winconf_db.order = NULL;
305 }
306
307
308
309 extern void init_wins()
310 {
311     char id;
312     while (0 != (id = get_next_winconf_id()))
313     {
314         init_win_from_winconf(id);
315     }
316 }
317
318
319
320 extern void sorted_win_toggle_and_activate()
321 {
322     uint8_t i = 0;
323     for (; i < strlen(world.winconf_db.order); i++)
324     {
325         if (NULL == strchr(world.winconf_db.ids, world.winconf_db.order[i]))
326         {
327             continue;
328         }
329         toggle_window(world.winconf_db.order[i]);
330         if (world.winconf_db.active == (uint8_t) world.winconf_db.order[i])
331         {
332             world.wmeta.active = get_win_by_id(world.winconf_db.order[i]);
333         }
334     }
335 }
336
337
338 extern char get_next_winconf_id()
339 {
340     static uint8_t i = 0;
341     char c = world.winconf_db.ids[i];
342     if (0 == c)
343     {
344         i = 0;
345         return c;
346     }
347     i++;
348     return c;
349 }
350
351
352
353 extern void toggle_window(char id)
354 {
355     struct Win * win = get_win_by_id(id);
356     if (0 == win->prev && world.wmeta.chain_start != win)   /* Win struct is  */
357     {                                                       /* outside chain? */
358         append_win(win);
359     }
360     else
361     {
362         suspend_win(win);
363     }
364 }
365
366
367
368 extern void toggle_winconfig()
369 {
370    struct Win * win = world.wmeta.active;
371    struct WinConf * wcp = get_winconf_by_win(win);
372     if      (0 == wcp->view)
373     {
374         wcp->view     = 1;
375         win->draw     = draw_winconf_geometry;
376         wcp->center   = win->center;
377         win->center.y = 0;
378         win->center.x = 0;
379     }
380     else if (1 == wcp->view)
381     {
382         wcp->view     = 2;
383         win->draw     = draw_winconf_keybindings;
384         win->center.x = 0;
385     }
386     else
387     {
388         wcp->view     = 0;
389         win->draw     = get_drawfunc_by_char(wcp->id);
390         win->center   = wcp->center;
391     }
392 }
393
394
395
396 extern void toggle_win_size_type(char axis)
397 {
398     struct Win * win = world.wmeta.active;
399     struct WinConf * wcp = get_winconf_by_win(win);
400     if ('y' == axis)
401     {
402         wcp->height_type = (0 == wcp->height_type);
403         set_winconf_geometry(wcp->id);
404         return;
405     }
406     wcp->width_type = (   0 == wcp->width_type
407                        && win->framesize.x <= world.wmeta.padsize.x);
408     set_winconf_geometry(wcp->id);
409 }
410
411
412
413 extern void scroll_pad(char dir)
414 {
415     if      ('+' == dir)
416     {
417         reset_pad_offset(world.wmeta.pad_offset + 1);
418     }
419     else if ('-' == dir)
420     {
421         reset_pad_offset(world.wmeta.pad_offset - 1);
422     }
423 }
424
425
426
427 extern void growshrink_active_window(char change)
428 {
429     if (0 != world.wmeta.active)
430     {
431         struct yx_uint16 size = world.wmeta.active->framesize;
432         if      (change == '-')
433         {
434             size.y--;
435         }
436         else if (change == '+')
437         {
438             size.y++;
439         }
440         else if (change == '_')
441         {
442             size.x--;
443         }
444         else if (change == '*')
445         {
446             size.x++;
447         }
448         resize_active_win(size);
449         struct WinConf * wcp = get_winconf_by_win(world.wmeta.active);
450         if (   1 == wcp->width_type
451             && world.wmeta.active->framesize.x > world.wmeta.padsize.x)
452         {
453             wcp->width_type = 0;
454         }
455         set_winconf_geometry(wcp->id);
456     }
457 }