home · contact · privacy
576cd56e81ba4897c4494783b1ee368c21d6d223
[plomrogue] / src / client / windows.c
1 /* src/client/windows.c
2  *
3  * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4  * or any later version. For details on its copyright, license, and warranties,
5  * see the file NOTICE in the root directory of the PlomRogue source package.
6  */
7
8 #define _POSIX_C_SOURCE 200809L /* strnlen() */
9 #include "windows.h"
10 #include <ncurses.h> /* chtype, getmaxx(), getmaxy(), erase(), werase(),
11                       * endwin(), delwin(), wnoutrefresh(), pnoutrefresh(),
12                       * doupdate(), refresh(), delwin(), newpad(), mvwaddch(),
13                       * mvwaddstr()
14                       */
15 #include <stddef.h> /* NULL */
16 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX */
17 #include <stdio.h> /* sprintf() */
18 #include <stdlib.h> /* free() */
19 #include <string.h> /* memcpy(), strlen(), strnlen(), memset() */
20 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
21 #include "../common/try_malloc.h" /* try_malloc() */
22 #include "draw_wins.h" /* draw_winconf_geometry(), draw_winconf_keybindings(),
23                         * draw_win_inventory(), draw_win_info(), draw_win_log(),
24                         * draw_win_keybindings_winconf_keybindings(),
25                         * draw_win_keybindings_winconf_geometry(),
26                         * draw_win_keybindings_global(), draw_win_map()
27                         */
28 #include "wincontrol.h" /* toggle_window() */
29 #include "world.h" /* world */
30
31
32
33 /* Calculate "id"'s window's size from v_screen size and .target_(height/width).
34  * A .target_width == 0 makes the window as wide as .t_screen. .target_height ==
35  * 0 sets the maximum allowed height: one cell smaller than that of .v_screen.
36  * Negative values make width/height so many cells smaller than what 0 would
37  * set. Values that would reduce the window height or width to less than 1 cell
38  * according to the aforementioned rules set the height/width as if they were 0.
39  */
40 static void init_win_size_from_winconf_and_v_screen_size(char id);
41
42 /* Get (Win->draw) function identified by "c"; NULL if c not mapped to one.
43  * match_func() is just a little helper to it.
44  */
45 static uint8_t match_func(char c, void (** f) (), char c_m, void (* f_m) ());
46 static void (* get_drawfunc_by_char(char c)) ();
47
48 /* Iterate over chars of world.winDB.ids array / string. Restart after \0.*/
49 static char get_next_win_id();
50
51 /* Draw scroll hint (a line saying that there are "dist" more elements of "unit"
52  * further into the direction symbolized by "dir") into .v_screen, onto an
53  * appropriate edge of a window or .v_screen; the left/right edge if "dir" is
54  * "<"/">", or the top/bottom edge if it is "^"/"v". Use "start" as the start
55  * coordinate of the frame that is to contain the scroll hints. winscroll_hint()
56  * is a wrapper that uses the border of window "w" as this frame.
57  */
58 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
59                         char * unit, struct yx_uint16 start);
60 static void winscroll_hint(struct Win * w, char dir, uint16_t dist);
61
62 /* draw_win_borderlines() draws vertical/horizontal borders of window "w" sans
63  * corners into .v_screen. It draws the top border line as the windows' title
64  * bar (highlighted if the window is selected as active). It is called
65  * recursively by draw_wins_borderlines() on all windows from "w" on.
66  * draw_wins_bordercorners() draws the border corners of "w" and its successors.
67  */
68 static void draw_win_borderlines(struct Win * w);
69 static void draw_wins_borderlines(struct Win * w);
70 static void draw_wins_bordercorners(struct Win * w);
71
72 /* Draw contents of all windows in window chain from window "w" onwards. */
73 static void draw_wins(struct Win * w);
74
75
76
77 static void init_win_size_from_winconf_and_v_screen_size(char id)
78 {
79     struct Win * w = get_win_by_id(id);
80     w->frame_size.y  = world.winDB.v_screen_size.y - 1;
81     if      (   0 < w->target_height
82              && w->target_height <= world.winDB.v_screen_size.y - 1)
83     {
84         w->frame_size.y = w->target_height;
85     }
86     else if (   0 > w->target_height
87              && world.winDB.v_screen_size.y + (w->target_height - 1) > 0)
88     {
89         w->frame_size.y = world.winDB.v_screen_size.y + (w->target_height - 1);
90     }
91     w->frame_size.x  = world.winDB.v_screen_size.x;
92     if      (0 < w->target_width)
93     {
94         w->frame_size.x = w->target_width;
95     }
96     else if (   0 > w->target_width
97              && world.winDB.v_screen_size.x + w->target_width > 0)
98     {
99         w->frame_size.x = world.winDB.v_screen_size.x + w->target_width;
100     }
101 }
102
103
104
105 static uint8_t match_func(char c, void (** f) (), char c_m, void (* f_m) ())
106 {
107     if (c == c_m)
108     {
109         * f = f_m;
110         return 1;
111     }
112     return 0;
113 }
114
115
116
117 static void (* get_drawfunc_by_char(char c)) ()
118 {
119     void (* f) (struct Win *) = NULL;
120     if (   match_func(c, &f, 'c', draw_win_inventory)
121         || match_func(c, &f, 'i', draw_win_info)
122         || match_func(c, &f, 'l', draw_win_log)
123         || match_func(c, &f, 'm', draw_win_map)
124         || match_func(c, &f, '0', draw_win_keybindings_global)
125         || match_func(c, &f, '1', draw_win_keybindings_winconf_geometry)
126         || match_func(c, &f, '2', draw_win_keybindings_winconf_keybindings))
127     {
128         ;
129     }
130     return f;
131 }
132
133
134
135 static char get_next_win_id()
136 {
137     static uint8_t i = 0;
138     char c = world.winDB.ids[i];
139     if (0 == c)
140     {
141         i = 0;
142         return c;
143     }
144     i++;
145     return c;
146 }
147
148
149
150 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
151                         char * unit, struct yx_uint16 start)
152 {
153     /* Decide on alignment (vertical/horizontal?), thereby hint text space. */
154     char * more = "more";
155     uint16_t dsc_space = fsize.x;
156     if ('<' == dir || '>' == dir)
157     {
158         dsc_space = fsize.y;
159     }                                  /* vv-- 10 = max strlen for uint16_t */
160     uint16_t size = 1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1;
161     char * scrolldsc = try_malloc(size, __func__);
162     int test = sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
163     exit_trouble(test < 0, __func__, "sprintf");
164
165     /* Decide on offset of the description text inside the scroll hint line. */
166     uint16_t dsc_offset = 1;
167     if (dsc_space > strlen(scrolldsc) + 1)
168     {
169         dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
170     }
171
172     /* Draw scroll hint line as dir symbols bracketing description text. */
173     uint16_t draw_offset = 0;
174     if      ('>' == dir)
175     {
176         draw_offset = fsize.x - 1;
177     }
178     else if ('v' == dir)
179     {
180         draw_offset = fsize.y - 1;
181     }
182     uint16_t q = 0;
183     for (; q < dsc_space; q++)
184     {
185         chtype c = dir | A_REVERSE;
186         if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
187         {
188             c = scrolldsc[q - dsc_offset] | A_REVERSE;
189         }
190         if ('<' == dir || '>' == dir)
191         {
192             mvwaddch(world.winDB.v_screen, start.y+q, start.x+draw_offset, c);
193             continue;
194         }
195         mvwaddch(world.winDB.v_screen, start.y + draw_offset, start.x + q, c);
196     }
197     free(scrolldsc);
198 }
199
200
201
202 static void winscroll_hint(struct Win * w, char dir, uint16_t dist)
203 {
204     char * unit = "lines";
205     if ('<' == dir || '>' == dir)
206     {
207         unit = "columns";
208     }
209     struct yx_uint16 start = w->start;
210     scroll_hint(w->frame_size, dir, dist, unit, start);
211 }
212
213
214
215 static void draw_win_borderlines(struct Win * w)
216 {
217     /* Draw vertical and horizontal border lines. */
218     uint16_t y, x;
219     for (y = w->start.y; y <= w->start.y + w->frame_size.y; y++)
220     {
221         mvwaddch(world.winDB.v_screen, y, w->start.x - 1,              '|');
222         mvwaddch(world.winDB.v_screen, y, w->start.x + w->frame_size.x, '|');
223     }
224     for (x = w->start.x; x <= w->start.x + w->frame_size.x; x++)
225     {
226         mvwaddch(world.winDB.v_screen, w->start.y - 1,              x, '-');
227         mvwaddch(world.winDB.v_screen, w->start.y + w->frame_size.y, x, '-');
228     }
229
230     /* Draw as much as possible of the title into center of top border line. */
231     uint8_t min_title_length_visible = 3;/* min. 1 char +2 padding/decoration*/
232     if (w->frame_size.x >= min_title_length_visible)
233     {
234         uint16_t offset = 0;
235         if (w->frame_size.x > strlen(w->title) + 2)
236         {
237             offset = (w->frame_size.x - (strlen(w->title) + 2)) / 2;
238         }                                    /* +2 is for padding/decoration */
239         uint16_t length_visible = strnlen(w->title, w->frame_size.x - 2);
240         char * title = try_malloc(length_visible + 3, "draw_win_borderlines");
241         char decoration = ' ';
242         if (w->id == world.winDB.active)
243         {
244             decoration = '$';
245         }
246         memcpy(title + 1, w->title, length_visible);
247         title[0] = title[length_visible + 1] = decoration;
248         title[length_visible + 2] = '\0';
249         mvwaddstr(world.winDB.v_screen, w->start.y-1, w->start.x+offset, title);
250         free(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 void make_v_screen_and_init_win_sizes()
397 {
398     char * err_s = "creating an illegaly large virtual screen";
399     char * err_m = "triggering a memory allocation error via newpad";
400     uint32_t maxy_test = getmaxy(world.winDB.t_screen);
401     uint32_t maxx_test = getmaxx(world.winDB.t_screen);
402     exit_trouble(maxy_test>UINT16_MAX || maxx_test>UINT16_MAX, __func__, err_s);
403     world.winDB.v_screen_size.y = maxy_test;
404     world.winDB.v_screen_size.x = maxx_test;
405     world.winDB.v_screen = newpad(world.winDB.v_screen_size.y, 1);
406     exit_trouble(!world.winDB.v_screen, __func__, err_m);
407     char id;
408     while (0 != (id = get_next_win_id()))
409     {
410         init_win_size_from_winconf_and_v_screen_size(id);
411     }
412 }
413
414
415
416 extern void free_winDB()
417 {
418     char id;
419     while (0 != (id = get_next_win_id()))
420     {
421         struct Win * wc = get_win_by_id(id);
422         free(wc->title);
423         free(wc->kb.kbs);
424     }
425     free(world.winDB.ids);
426     free(world.winDB.wins);
427     free(world.winDB.order);
428 }
429
430
431
432 extern void winch_called()
433 {
434     world.winch = 1;
435 }
436
437
438
439 extern void reset_windows_on_winch()
440 {
441     endwin();  /* "[S]tandard way" to recalibrate ncurses post SIGWINCH, says */
442     refresh(); /* <http://invisible-island.net/ncurses/ncurses-intro.html>.   */
443     char * tmp_order = try_malloc(strlen(world.winDB.order) + 1, __func__);
444     int test = sprintf(tmp_order, "%s", world.winDB.order);
445     exit_trouble(test < 0, __func__, "sprintf");
446     uint8_t i;
447     char tmp_active = world.winDB.active;
448     for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
449     delwin(world.winDB.v_screen);
450     make_v_screen_and_init_win_sizes();
451     for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
452     free(tmp_order);
453     world.winDB.active = tmp_active;
454 }
455
456
457
458 extern void draw_all_wins()
459 {
460     /* Empty everything before filling it a-new. */
461     erase();
462     wnoutrefresh(world.winDB.t_screen);
463     werase(world.winDB.v_screen);
464     if (world.winDB.active)
465     {
466
467         /* Draw borders, wins. Order matters: corners should overwrite lines. */
468         draw_wins_borderlines(get_win_by_id(world.winDB.order[0]));
469         draw_wins_bordercorners(get_win_by_id(world.winDB.order[0]));
470         draw_wins(get_win_by_id(world.winDB.order[0]));
471
472         /* Draw .v_screen scroll hints. */
473         struct yx_uint16 start;
474         start.y = 0;
475         start.x = world.winDB.v_screen_offset;
476         char * cols_string = "columns";
477         if (world.winDB.v_screen_offset > 0)
478         {
479             scroll_hint(world.winDB.v_screen_size, '<',
480                         world.winDB.v_screen_offset + 1, cols_string, start);
481         }
482         uint16_t size_x = getmaxx(world.winDB.v_screen);
483         uint16_t right_edge =   world.winDB.v_screen_offset
484                               + world.winDB.v_screen_size.x;
485         if (right_edge < size_x - 1)
486         {
487             scroll_hint(world.winDB.v_screen_size, '>',
488                         size_x - right_edge, cols_string, start);
489         }
490
491         /* Put .v_screen segment to be shown on .t_screen to .t_screen buffer.*/
492         pnoutrefresh(world.winDB.v_screen, 0, world.winDB.v_screen_offset, 0, 0,
493                      world.winDB.v_screen_size.y,
494                      world.winDB.v_screen_size.x - 1);
495     }
496
497     /* Only at the end write accumulated changes to .t_screen. */
498     doupdate();
499 }