home · contact · privacy
Merged world.wmeta and world.winconf_db into world.wins.
[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.wins.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.wins.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.wins.winconfs[i].id)
140         {
141             return &world.wins.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.wins.winconfs[i].win)
155         {
156             return &world.wins.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,
173                                       FILE * file)
174 {
175     char * f_name = "read_winconf_from_file()";
176     int test = try_fgetc(file, f_name);
177     if (EOF == test)
178     {
179         return 0;
180     }
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);
195     winconf.win = NULL;
196     winconf.view = 0;
197     winconf.center.y = 0;
198     winconf.center.x = 0;
199     if (world.wins.ids)
200     {
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;
213     }
214     else
215     {
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;
220     }
221     return 1;
222 }
223
224
225
226 extern void write_winconf_of_id_to_file(FILE * file, char c, char * delim)
227 {
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. */
233         size = 7;
234     }
235     char line[size];
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);
245 }
246
247
248
249 extern void read_order_wins_visible_active(char * line, uint32_t linemax,
250                                            FILE * file)
251 {
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);
264 }
265
266
267
268 extern void write_order_wins_visible_active(FILE * file, char * delim)
269 {
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;
273     char active = ' ';
274     uint8_t i;
275     for (; NULL != w_p; w_p = w_p->next, i++)
276     {
277         struct WinConf * wc_p = get_winconf_by_win(w_p);
278         line[i] = wc_p->id;
279         if (w_p == world.wins.win_active)
280         {
281             active = wc_p->id;
282         }
283     }
284     line[i] = '\n';
285     line[i + 1] = '\0';
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);
290 }
291
292
293
294 extern void free_winconfs()
295 {
296     char id;
297     while (0 != (id = get_next_winconf_id()))
298     {
299         free_winconf_data(id);
300     }
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;
307 }
308
309
310
311 extern void init_wins()
312 {
313     char id;
314     while (0 != (id = get_next_winconf_id()))
315     {
316         init_win_from_winconf(id);
317     }
318 }
319
320
321
322 extern void sorted_win_toggle_and_activate()
323 {
324     uint8_t i = 0;
325     for (; i < strlen(world.wins.order); i++)
326     {
327         if (NULL == strchr(world.wins.ids, world.wins.order[i]))
328         {
329             continue;
330         }
331         toggle_window(world.wins.order[i]);
332         if (world.wins.id_active == (uint8_t) world.wins.order[i])
333         {
334             world.wins.win_active = get_win_by_id(world.wins.order[i]);
335         }
336     }
337 }
338
339
340 extern char get_next_winconf_id()
341 {
342     static uint8_t i = 0;
343     char c = world.wins.ids[i];
344     if (0 == c)
345     {
346         i = 0;
347         return c;
348     }
349     i++;
350     return c;
351 }
352
353
354
355 extern void toggle_window(char id)
356 {
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? */
360         append_win(win);
361     }
362     else
363     {
364         suspend_win(win);
365     }
366 }
367
368
369
370 extern void toggle_winconfig()
371 {
372    struct Win * win = world.wins.win_active;
373    struct WinConf * wcp = get_winconf_by_win(win);
374     if      (0 == wcp->view)
375     {
376         wcp->view     = 1;
377         win->draw     = draw_winconf_geometry;
378         wcp->center   = win->center;
379         win->center.y = 0;
380         win->center.x = 0;
381     }
382     else if (1 == wcp->view)
383     {
384         wcp->view     = 2;
385         win->draw     = draw_winconf_keybindings;
386         win->center.x = 0;
387     }
388     else
389     {
390         wcp->view     = 0;
391         win->draw     = get_drawfunc_by_char(wcp->id);
392         win->center   = wcp->center;
393     }
394 }
395
396
397
398 extern void toggle_win_size_type(char axis)
399 {
400     struct Win * win = world.wins.win_active;
401     struct WinConf * wcp = get_winconf_by_win(win);
402     if ('y' == axis)
403     {
404         wcp->height_type = (0 == wcp->height_type);
405         set_winconf_geometry(wcp->id);
406         return;
407     }
408     wcp->width_type = (   0 == wcp->width_type
409                        && win->framesize.x <= world.wins.padsize.x);
410     set_winconf_geometry(wcp->id);
411 }
412
413
414
415 extern void scroll_pad(char dir)
416 {
417     if      ('+' == dir)
418     {
419         reset_pad_offset(world.wins.pad_offset + 1);
420     }
421     else if ('-' == dir)
422     {
423         reset_pad_offset(world.wins.pad_offset - 1);
424     }
425 }
426
427
428
429 extern void growshrink_active_window(char change)
430 {
431     if (0 != world.wins.win_active)
432     {
433         struct yx_uint16 size = world.wins.win_active->framesize;
434         if      (change == '-')
435         {
436             size.y--;
437         }
438         else if (change == '+')
439         {
440             size.y++;
441         }
442         else if (change == '_')
443         {
444             size.x--;
445         }
446         else if (change == '*')
447         {
448             size.x++;
449         }
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)
454         {
455             wcp->width_type = 0;
456         }
457         set_winconf_geometry(wcp->id);
458     }
459 }