home · contact · privacy
Enforce C11 via Makefile, explicate POSIX dependencies in source files.
[plomrogue] / src / client / windows.c
1 /* src/client/windows.c */
2
3 #define _POSIX_C_SOURCE 200809L /* strnlen() */
4 #include "windows.h"
5 #include <ncurses.h> /* chtype, getmaxx(), getmaxy(), erase(), werase(),
6                       * endwin(), delwin(), wnoutrefresh(), pnoutrefresh(),
7                       * doupdate(), refresh(), delwin(), newpad(), mvwaddch(),
8                       * mvwaddstr()
9                       */
10 #include <stddef.h> /* NULL */
11 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX */
12 #include <stdio.h> /* sprintf() */
13 #include <stdlib.h> /* free(), atoi() */
14 #include <string.h> /* memcpy(), strlen(), strnlen(), strchr(), memset() */
15 #include "../common/err_try_fgets.h" /* err_try_fgets(), err_line() */
16 #include "../common/readwrite.h" /* try_fputc(), try_write(), try_fgetc() */
17 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
18 #include "../common/try_malloc.h" /* try_malloc() */
19 #include "../common/yx_uint16.h" /* struct yx_uint16 */
20 #include "array_append.h" /* array_append() */
21 #include "draw_wins.h" /* draw_winconf_geometry(), draw_winconf_keybindings(),
22                         * draw_win_inventory(), draw_win_info(), draw_win_log(),
23                         * draw_win_available_keybindings(), draw_win_map(),
24                         * draw_win_keybindings_winconf_keybindings(),
25                         * draw_win_keybindings_winconf_geometry(),
26                         * draw_win_keybindings_global()
27                         */
28 #include "keybindings.h" /* write_keybidings_to_file(),
29                           * read_keybindings_from_file()
30                           */
31 #include "wincontrol.h" /* toggle_window() */
32 #include "world.h" /* global world */
33
34
35
36 /* Calculate "id"'s window's size from v_screen size and .target_(height/width).
37  * A .target_width == 0 makes the window as wide as .t_screen. .target_height ==
38  * 0 sets the maximum allowed height: one cell smaller than that of .v_screen.
39  * Negative values make width/height so many cells smaller than what 0 would
40  * set. Values that would reduce the window height or width to less than 1 cell
41  * according to the aforementioned rules set the height/width as if they were 0.
42  */
43 static void init_win_size_from_winconf_and_v_screen_size(char id);
44
45 /* Get (Win->draw) function identified by "c"; NULL if c not mapped to one.
46  * match_func() is just a little helper to it.
47  */
48 static uint8_t match_func(char c, void (** f) (), char c_m, void (* f_m) ());
49 static void (* get_drawfunc_by_char(char c)) ();
50
51 /* Iterate over chars of world.winDB.ids array / string. Restart after \0.*/
52 static char get_next_win_id();
53
54 /* Draw scroll hint (a line saying that there are "dist" more elements of "unit"
55  * further into the direction symbolized by "dir") into .v_screen, onto an
56  * appropriate edge of a window or .v_screen; the left/right edge if "dir" is
57  * "<"/">", or the top/bottom edge if it is "^"/"v". Use "start" as the start
58  * coordinate of the frame that is to contain the scroll hints. winscroll_hint()
59  * is a wrapper that uses the border of window "w" as this frame.
60  */
61 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
62                         char * unit, struct yx_uint16 start);
63 static void winscroll_hint(struct Win * w, char dir, uint16_t dist);
64
65 /* draw_win_borderlines() draws vertical/horizontal borders of window "w" sans
66  * corners into .v_screen. It draws the top border line as the windows' title
67  * bar (highlighted if the window is selected as active). It is called
68  * recursively by draw_wins_borderlines() on all windows from "w" on.
69  * draw_wins_bordercorners() draws the border corners of "w" and its successors.
70  */
71 static void draw_win_borderlines(struct Win * w);
72 static void draw_wins_borderlines(struct Win * w);
73 static void draw_wins_bordercorners(struct Win * w);
74
75 /* Draw contents of all windows in window chain from window "w" onwards. */
76 static void draw_wins(struct Win * w);
77
78
79
80 static void init_win_size_from_winconf_and_v_screen_size(char id)
81 {
82     struct Win * w = get_win_by_id(id);
83     w->frame_size.y  = world.winDB.v_screen_size.y - 1;
84     if      (   0 < w->target_height
85              && w->target_height <= world.winDB.v_screen_size.y - 1)
86     {
87         w->frame_size.y = w->target_height;
88     }
89     else if (   0 > w->target_height
90              && world.winDB.v_screen_size.y + (w->target_height - 1) > 0)
91     {
92         w->frame_size.y = world.winDB.v_screen_size.y + (w->target_height - 1);
93     }
94     w->frame_size.x  = world.winDB.v_screen_size.x;
95     if      (0 < w->target_width)
96     {
97         w->frame_size.x = w->target_width;
98     }
99     else if (   0 > w->target_width
100              && world.winDB.v_screen_size.x + w->target_width > 0)
101     {
102         w->frame_size.x = world.winDB.v_screen_size.x + w->target_width;
103     }
104 }
105
106
107
108 static uint8_t match_func(char c, void (** f) (), char c_m, void (* f_m) ())
109 {
110     if (c == c_m)
111     {
112         * f = f_m;
113         return 1;
114     }
115     return 0;
116 }
117
118
119
120 static void (* get_drawfunc_by_char(char c)) ()
121 {
122     void (* f) (struct Win *) = NULL;
123     if (   match_func(c, &f, 'c', draw_win_inventory)
124         || match_func(c, &f, 'i', draw_win_info)
125         || match_func(c, &f, 'l', draw_win_log)
126         || match_func(c, &f, 'k', draw_win_available_keybindings)
127         || match_func(c, &f, 'm', draw_win_map)
128         || match_func(c, &f, '0', draw_win_keybindings_global)
129         || match_func(c, &f, '1', draw_win_keybindings_winconf_geometry)
130         || match_func(c, &f, '2', draw_win_keybindings_winconf_keybindings))
131     {
132         ;
133     }
134     return f;
135 }
136
137
138
139 static char get_next_win_id()
140 {
141     static uint8_t i = 0;
142     char c = world.winDB.ids[i];
143     if (0 == c)
144     {
145         i = 0;
146         return c;
147     }
148     i++;
149     return c;
150 }
151
152
153
154 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
155                         char * unit, struct yx_uint16 start)
156 {
157     /* Decide on alignment (vertical/horizontal?), thereby hint text space. */
158     char * more = "more";
159     uint16_t dsc_space = fsize.x;
160     if ('<' == dir || '>' == dir)
161     {
162         dsc_space = fsize.y;
163     }                                  /* vv-- 10 = max strlen for uint16_t */
164     char scrolldsc[1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1];
165     sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
166
167     /* Decide on offset of the description text inside the scroll hint line. */
168     uint16_t dsc_offset = 1;
169     if (dsc_space > strlen(scrolldsc) + 1)
170     {
171         dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
172     }
173
174     /* Draw scroll hint line as dir symbols bracketing description text. */
175     uint16_t draw_offset = 0;
176     if      ('>' == dir)
177     {
178         draw_offset = fsize.x - 1;
179     }
180     else if ('v' == dir)
181     {
182         draw_offset = fsize.y - 1;
183     }
184     uint16_t q = 0;
185     for (; q < dsc_space; q++)
186     {
187         chtype c = dir | A_REVERSE;
188         if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
189         {
190             c = scrolldsc[q - dsc_offset] | A_REVERSE;
191         }
192         if ('<' == dir || '>' == dir)
193         {
194             mvwaddch(world.winDB.v_screen, start.y+q, start.x+draw_offset, c);
195             continue;
196         }
197         mvwaddch(world.winDB.v_screen, start.y + draw_offset, start.x + q, c);
198     }
199 }
200
201
202
203 static void winscroll_hint(struct Win * w, char dir, uint16_t dist)
204 {
205     char * unit = "lines";
206     if ('<' == dir || '>' == dir)
207     {
208         unit = "columns";
209     }
210     struct yx_uint16 start = w->start;
211     scroll_hint(w->frame_size, dir, dist, unit, start);
212 }
213
214
215
216 static void draw_win_borderlines(struct Win * w)
217 {
218     /* Draw vertical and horizontal border lines. */
219     uint16_t y, x;
220     for (y = w->start.y; y <= w->start.y + w->frame_size.y; y++)
221     {
222         mvwaddch(world.winDB.v_screen, y, w->start.x - 1,              '|');
223         mvwaddch(world.winDB.v_screen, y, w->start.x + w->frame_size.x, '|');
224     }
225     for (x = w->start.x; x <= w->start.x + w->frame_size.x; x++)
226     {
227         mvwaddch(world.winDB.v_screen, w->start.y - 1,              x, '-');
228         mvwaddch(world.winDB.v_screen, w->start.y + w->frame_size.y, x, '-');
229     }
230
231     /* Draw as much as possible of the title into center of top border line. */
232     uint8_t min_title_length_visible = 3;/* min. 1 char +2 padding/decoration*/
233     if (w->frame_size.x >= min_title_length_visible)
234     {
235         uint16_t offset = 0;
236         if (w->frame_size.x > strlen(w->title) + 2)
237         {
238             offset = (w->frame_size.x - (strlen(w->title) + 2)) / 2;
239         }                                    /* +2 is for padding/decoration */
240         uint16_t length_visible = strnlen(w->title, w->frame_size.x - 2);
241         char title[length_visible + 3];
242         char decoration = ' ';
243         if (w->id == world.winDB.active)
244         {
245             decoration = '$';
246         }
247         memcpy(title + 1, w->title, length_visible);
248         title[0] = title[length_visible + 1] = decoration;
249         title[length_visible + 2] = '\0';
250         mvwaddstr(world.winDB.v_screen, w->start.y-1, w->start.x+offset, title);
251     }
252 }
253
254
255
256 static void draw_wins_borderlines(struct Win * w)
257 {
258     draw_win_borderlines(w);
259     struct Win * next = get_win_after(w->id);
260     if (next)
261     {
262         draw_wins_borderlines(next);
263     }
264 }
265
266
267
268 static void draw_wins_bordercorners(struct Win * w)
269 {
270     mvwaddch(world.winDB.v_screen,
271              w->start.y - 1, w->start.x - 1,              '+');
272     mvwaddch(world.winDB.v_screen,
273              w->start.y - 1, w->start.x + w->frame_size.x, '+');
274     mvwaddch(world.winDB.v_screen,
275              w->start.y + w->frame_size.y, w->start.x - 1, '+');
276     mvwaddch(world.winDB.v_screen, w->start.y + w->frame_size.y,
277              w->start.x + w->frame_size.x,                 '+');
278     struct Win * next = get_win_after(w->id);
279     if (next)
280     {
281         draw_wins_bordercorners(next);
282     }
283 }
284
285
286
287 static void draw_wins(struct Win * w)
288 {
289     void (* drawfunc) (struct Win *) = get_drawfunc_by_char(w->id);
290     if      (1 == w->view)
291     {
292         drawfunc = draw_winconf_geometry;
293     }
294     else if (2 == w->view)
295     {
296         drawfunc = draw_winconf_keybindings;
297     }
298     drawfunc(w);
299     uint16_t size_y = w->winmap_size.y;
300     uint16_t size_x = w->winmap_size.x;
301     uint16_t offset_y = center_offset(w->center.y, size_y, w->frame_size.y);
302     uint16_t offset_x = center_offset(w->center.x, size_x, w->frame_size.x);
303     uint16_t y, x;
304     for (y = offset_y; y < w->frame_size.y + offset_y && y < size_y; y++)
305     {
306         for (x = offset_x; x < w->frame_size.x + offset_x && x < size_x; x++)
307         {
308             chtype ch = w->winmap[(y * w->winmap_size.x) + x];
309             mvwaddch(world.winDB.v_screen, w->start.y + (y - offset_y),
310                                       w->start.x + (x - offset_x), ch);
311         }
312     }
313     free(w->winmap); /* NULL so draw_wins.c's try_resize_winmap() may always  */
314     w->winmap = NULL;/* free() it before (re-)allocation, even the first time.*/
315     memset(&w->winmap_size, 0, sizeof(struct yx_uint16));
316     if (offset_y > 0)
317     {
318         winscroll_hint(w, '^', offset_y + 1);
319     }
320     if (size_y > offset_y + w->frame_size.y)
321     {
322         winscroll_hint(w, 'v', size_y - ((offset_y + w->frame_size.y) - 1));
323     }
324     if (offset_x > 0)
325     {
326         winscroll_hint(w, '<', offset_x + 1);
327     }
328     if (size_x > offset_x + w->frame_size.x)
329     {
330         winscroll_hint(w, '>', size_x - ((offset_x + w->frame_size.x) - 1));
331     }
332     struct Win * next = get_win_after(w->id);
333     if (next)
334     {
335         draw_wins(next);
336     }
337 }
338
339
340
341 extern uint8_t get_win_pos_in_order(char c)
342 {
343     uint8_t i;
344     for (i = 0; c != world.winDB.order[i]; i++);
345     return i;
346 }
347
348
349
350 extern struct Win * get_win_after(char c)
351 {
352     return get_win_by_id(world.winDB.order[get_win_pos_in_order(c) + 1]);
353 }
354
355
356
357 extern uint16_t center_offset(uint16_t position, uint32_t mapsize,
358                               uint32_t frame_size)
359 {
360     uint16_t offset = 0;
361     if (mapsize > frame_size)
362     {
363         if (position > frame_size / 2)
364         {
365             if (position < mapsize - (frame_size / 2))
366             {
367                 offset = position - (frame_size / 2);
368             }
369             else
370             {
371                 offset = mapsize - frame_size;
372             }
373         }
374     }
375     return offset;
376 }
377
378
379
380 extern struct Win * get_win_by_id(char id)
381 {
382     uint8_t i = 0;
383     while ('\0' != world.winDB.ids[i])
384     {
385         if (id == world.winDB.ids[i])
386         {
387             return &world.winDB.wins[i];
388         }
389         i++;
390     }
391     return NULL;
392 }
393
394
395
396 extern uint8_t read_winconf_from_file(char * line, uint32_t linemax,
397                                       FILE * file)
398 {
399     char * f_name = "read_winconf_from_file()";
400     char * context = "Failed reading individual window's configuration from "
401                      "interface config file. ";
402     char * err_id  = "Illegal ID(s) selected.";
403     char * err_2   = "Double-initialized window.";
404     char * err_brk = "Illegal line break type index.";
405     int test_for_end = try_fgetc(file, f_name);
406     if (EOF == test_for_end || '\n' == test_for_end)
407     {
408         return 0;
409     }
410     exit_trouble(EOF == ungetc(test_for_end, file), f_name, "ungetc()");
411     err_try_fgets(line, linemax, file, context, "ns");
412     err_line(NULL == strchr(world.winDB.legal_ids, line[0]), line, context,
413              err_id);
414     exit_err(world.winDB.ids && NULL!=strchr(world.winDB.ids, line[0]), err_2);
415     struct Win win;
416     memset(&win, 0, sizeof(struct Win));
417     win.id = (char) line[0];
418     err_try_fgets(line, linemax, file, context, "0n");
419     win.title = try_malloc(strlen(line), f_name);
420     memcpy(win.title, line, strlen(line) - 1);      /* Eliminate newline char */
421     win.title[strlen(line) - 1] = '\0';             /* char at end of string. */
422     err_try_fgets(line, linemax, file, context, "0nsi");
423     err_line(atoi(line) > 2, line, context, err_brk);
424     win.linebreak = atoi(line);
425     err_try_fgets(line, linemax, file, context, "0ni");
426     win.target_height = atoi(line);
427     win.target_height_type = (0 >= win.target_height);
428     err_try_fgets(line, linemax, file, context, "0ni");
429     win.target_width = atoi(line);
430     win.target_width_type = (0 >= win.target_width);
431     read_keybindings_from_file(line, linemax, file, &win.kb);
432     if (world.winDB.ids)
433     {
434         uint8_t old_ids_size = strlen(world.winDB.ids);
435         char * new_ids = try_malloc(old_ids_size + 1 + 1, f_name);
436         sprintf(new_ids, "%s%c", world.winDB.ids, win.id);
437         free(world.winDB.ids);
438         world.winDB.ids = new_ids;
439         array_append(old_ids_size, sizeof(struct Win), (void *) &win,
440                      (void **) &world.winDB.wins);
441         return 1;
442     }
443     world.winDB.ids = try_malloc(2, f_name);
444     sprintf(world.winDB.ids, "%c", win.id);
445     world.winDB.wins = try_malloc(sizeof(struct Win), f_name);
446     world.winDB.wins[0] = win;
447     return 1;
448 }
449
450
451
452 extern void write_winconf_of_id_to_file(FILE * file, char c)
453 {
454     char * f_name = "write_winconf_of_id_to_file()";
455     struct Win * wc = get_win_by_id(c);
456     uint8_t size = strlen(wc->title) + 2;
457     if (size < 7)  /* Ensure that at least 5 + 2 char fit into line so that   */
458     {              /* the digit representation of any uint16_t may be stored. */
459         size = 7;
460     }
461     char line[size];
462     sprintf(line, "%c\n", wc->id);
463     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
464     sprintf(line, "%s\n", wc->title);
465     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
466     sprintf(line, "%d\n", wc->linebreak);
467     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
468     sprintf(line, "%d\n", wc->target_height);
469     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
470     sprintf(line, "%d\n", wc->target_width);
471     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
472     write_keybindings_to_file(file, &wc->kb);
473 }
474
475
476
477 extern void read_order_wins_visible_active(char * line, uint32_t linemax,
478                                            FILE * file, char ** tmp_order,
479                                            char * tmp_active)
480 {
481     char * f_name = "read_order_wins_visible_active()";
482     char * context   = "Failed reading order and activation of visible windows "
483                        "from interface config file. ";
484     char * err_id    = "Illegal ID(s) selected.";
485     err_try_fgets(line, linemax, file, context, "01");
486     uint32_t i = 0;
487     for (; i < strlen(line) - 1; i++)
488     {
489         char * test = strchr(world.winDB.legal_ids, line[i]);
490         err_line(!test, line, context, err_id);
491     }
492     line[strlen(line) - 1] = '\0';
493     *tmp_order = try_malloc(strlen(line) + 1, f_name);
494     sprintf(*tmp_order, "%s", line);
495     if (*tmp_order[0])
496     {
497         err_try_fgets(line, linemax, file, context, "0nfs");
498         err_line(NULL == strchr(*tmp_order, line[0]), line, context, err_id);
499         *tmp_active = line[0];
500     }
501     else
502     {
503         err_try_fgets(line, linemax, file, context, "0ne");
504         *tmp_active = '\0';
505     }
506     err_try_fgets(line, linemax, file, context, "d");
507 }
508
509
510
511 extern void write_order_wins_visible_active(FILE * file)
512 {
513     char * f_name = "write_order_wins_visible_active()";
514     try_fwrite(world.winDB.order, strlen(world.winDB.order), 1, file, f_name);
515     try_fputc('\n', file, f_name);
516     try_fputc(world.winDB.active, file, f_name);
517     try_fputc('\n', file, f_name);
518     try_fwrite(world.delim, strlen(world.delim), 1, file, f_name);
519 }
520
521
522
523 extern void make_v_screen_and_init_win_sizes()
524 {
525     char * f_name = "make_v_screen_and_init_win_sizes()";
526     char * err_s = "creating an illegaly large virtual screen";
527     char * err_m = "triggering a memory allocation error via newpad()";
528     uint32_t maxy_test = getmaxy(world.winDB.t_screen);
529     uint32_t maxx_test = getmaxx(world.winDB.t_screen);
530     exit_trouble(maxy_test>UINT16_MAX || maxx_test>UINT16_MAX, f_name, err_s);
531     world.winDB.v_screen_size.y = maxy_test;
532     world.winDB.v_screen_size.x = maxx_test;
533     world.winDB.v_screen = newpad(world.winDB.v_screen_size.y, 1);
534     exit_trouble(NULL == world.winDB.v_screen, f_name, err_m);
535     char id;
536     while (0 != (id = get_next_win_id()))
537     {
538         init_win_size_from_winconf_and_v_screen_size(id);
539     }
540 }
541
542
543
544 extern void free_winDB()
545 {
546     char id;
547     while (0 != (id = get_next_win_id()))
548     {
549         struct Win * wc = get_win_by_id(id);
550         free(wc->title);
551         free(wc->kb.kbs);
552         wc->kb.kbs = NULL;
553     }
554     free(world.winDB.ids); /* NULL this since read_winconf_from_file() checks */
555     world.winDB.ids = NULL;/* for it to detect its first post-DB-purge round. */
556     free(world.winDB.wins);
557     free(world.winDB.order);
558 }
559
560
561
562 extern void winch_called()
563 {
564     world.winch = 1;
565 }
566
567
568
569 extern void reset_windows_on_winch()
570 {
571     endwin();  /* "[S]tandard way" to recalibrate ncurses post SIGWINCH, says */
572     refresh(); /* <http://invisible-island.net/ncurses/ncurses-intro.html>.   */
573     char tmp_order[strlen(world.winDB.order) + 1];
574     sprintf(tmp_order, "%s", world.winDB.order);
575     uint8_t i;
576     char tmp_active = world.winDB.active;
577     for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
578     delwin(world.winDB.v_screen);
579     make_v_screen_and_init_win_sizes();
580     for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
581     world.winDB.active = tmp_active;
582 }
583
584
585
586 extern void draw_all_wins()
587 {
588     /* Empty everything before filling it a-new. */
589     erase();
590     wnoutrefresh(world.winDB.t_screen);
591     werase(world.winDB.v_screen);
592     if (world.winDB.active)
593     {
594
595         /* Draw borders, wins. Order matters: corners should overwrite lines. */
596         draw_wins_borderlines(get_win_by_id(world.winDB.order[0]));
597         draw_wins_bordercorners(get_win_by_id(world.winDB.order[0]));
598         draw_wins(get_win_by_id(world.winDB.order[0]));
599
600         /* Draw .v_screen scroll hints. */
601         struct yx_uint16 start;
602         start.y = 0;
603         start.x = world.winDB.v_screen_offset;
604         char * cols_string = "columns";
605         if (world.winDB.v_screen_offset > 0)
606         {
607             scroll_hint(world.winDB.v_screen_size, '<',
608                         world.winDB.v_screen_offset + 1, cols_string, start);
609         }
610         uint16_t size_x = getmaxx(world.winDB.v_screen);
611         uint16_t right_edge =   world.winDB.v_screen_offset
612                               + world.winDB.v_screen_size.x;
613         if (right_edge < size_x - 1)
614         {
615             scroll_hint(world.winDB.v_screen_size, '>',
616                         size_x - right_edge, cols_string, start);
617         }
618
619         /* Put .v_screen segment to be shown on .t_screen to .t_screen buffer.*/
620         pnoutrefresh(world.winDB.v_screen, 0, world.winDB.v_screen_offset, 0, 0,
621                      world.winDB.v_screen_size.y,
622                      world.winDB.v_screen_size.x - 1);
623     }
624
625     /* Only at the end write accumulated changes to .t_screen. */
626     doupdate();
627 }