home · contact · privacy
Client: Simplify display of available keybindings.
[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_global_keys(),draw_win_active_windows_keys(),
25                         * draw_win_keybindings_winconf_keybindings(),
26                         * draw_win_keybindings_winconf_geometry(),
27                         * draw_win_keybindings_global(), draw_win_map()
28                         */
29 #include "wincontrol.h" /* toggle_window() */
30 #include "world.h" /* world */
31
32
33
34 /* Calculate "id"'s window's size from v_screen size and .target_(height/width).
35  * A .target_width == 0 makes the window as wide as .t_screen. .target_height ==
36  * 0 sets the maximum allowed height: one cell smaller than that of .v_screen.
37  * Negative values make width/height so many cells smaller than what 0 would
38  * set. Values that would reduce the window height or width to less than 1 cell
39  * according to the aforementioned rules set the height/width as if they were 0.
40  */
41 static void init_win_size_from_winconf_and_v_screen_size(char id);
42
43 /* Get (Win->draw) function identified by "c"; NULL if c not mapped to one.
44  * match_func() is just a little helper to it.
45  */
46 static uint8_t match_func(char c, void (** f) (), char c_m, void (* f_m) ());
47 static void (* get_drawfunc_by_char(char c)) ();
48
49 /* Iterate over chars of world.winDB.ids array / string. Restart after \0.*/
50 static char get_next_win_id();
51
52 /* Draw scroll hint (a line saying that there are "dist" more elements of "unit"
53  * further into the direction symbolized by "dir") into .v_screen, onto an
54  * appropriate edge of a window or .v_screen; the left/right edge if "dir" is
55  * "<"/">", or the top/bottom edge if it is "^"/"v". Use "start" as the start
56  * coordinate of the frame that is to contain the scroll hints. winscroll_hint()
57  * is a wrapper that uses the border of window "w" as this frame.
58  */
59 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
60                         char * unit, struct yx_uint16 start);
61 static void winscroll_hint(struct Win * w, char dir, uint16_t dist);
62
63 /* draw_win_borderlines() draws vertical/horizontal borders of window "w" sans
64  * corners into .v_screen. It draws the top border line as the windows' title
65  * bar (highlighted if the window is selected as active). It is called
66  * recursively by draw_wins_borderlines() on all windows from "w" on.
67  * draw_wins_bordercorners() draws the border corners of "w" and its successors.
68  */
69 static void draw_win_borderlines(struct Win * w);
70 static void draw_wins_borderlines(struct Win * w);
71 static void draw_wins_bordercorners(struct Win * w);
72
73 /* Draw contents of all windows in window chain from window "w" onwards. */
74 static void draw_wins(struct Win * w);
75
76
77
78 static void init_win_size_from_winconf_and_v_screen_size(char id)
79 {
80     struct Win * w = get_win_by_id(id);
81     w->frame_size.y  = world.winDB.v_screen_size.y - 1;
82     if      (   0 < w->target_height
83              && w->target_height <= world.winDB.v_screen_size.y - 1)
84     {
85         w->frame_size.y = w->target_height;
86     }
87     else if (   0 > w->target_height
88              && world.winDB.v_screen_size.y + (w->target_height - 1) > 0)
89     {
90         w->frame_size.y = world.winDB.v_screen_size.y + (w->target_height - 1);
91     }
92     w->frame_size.x  = world.winDB.v_screen_size.x;
93     if      (0 < w->target_width)
94     {
95         w->frame_size.x = w->target_width;
96     }
97     else if (   0 > w->target_width
98              && world.winDB.v_screen_size.x + w->target_width > 0)
99     {
100         w->frame_size.x = world.winDB.v_screen_size.x + w->target_width;
101     }
102 }
103
104
105
106 static uint8_t match_func(char c, void (** f) (), char c_m, void (* f_m) ())
107 {
108     if (c == c_m)
109     {
110         * f = f_m;
111         return 1;
112     }
113     return 0;
114 }
115
116
117
118 static void (* get_drawfunc_by_char(char c)) ()
119 {
120     void (* f) (struct Win *) = NULL;
121     if (   match_func(c, &f, 'c', draw_win_inventory)
122         || match_func(c, &f, 'i', draw_win_info)
123         || match_func(c, &f, 'l', draw_win_log)
124         || match_func(c, &f, 'g', draw_win_global_keys)
125         || match_func(c, &f, 'a', draw_win_active_windows_keys)
126         || match_func(c, &f, 'm', draw_win_map)
127         || match_func(c, &f, '0', draw_win_keybindings_global)
128         || match_func(c, &f, '1', draw_win_keybindings_winconf_geometry)
129         || match_func(c, &f, '2', draw_win_keybindings_winconf_keybindings))
130     {
131         ;
132     }
133     return f;
134 }
135
136
137
138 static char get_next_win_id()
139 {
140     static uint8_t i = 0;
141     char c = world.winDB.ids[i];
142     if (0 == c)
143     {
144         i = 0;
145         return c;
146     }
147     i++;
148     return c;
149 }
150
151
152
153 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
154                         char * unit, struct yx_uint16 start)
155 {
156     /* Decide on alignment (vertical/horizontal?), thereby hint text space. */
157     char * more = "more";
158     uint16_t dsc_space = fsize.x;
159     if ('<' == dir || '>' == dir)
160     {
161         dsc_space = fsize.y;
162     }                                  /* vv-- 10 = max strlen for uint16_t */
163     uint16_t size = 1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1;
164     char * scrolldsc = try_malloc(size, __func__);
165     int test = sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
166     exit_trouble(test < 0, __func__, "sprintf");
167
168     /* Decide on offset of the description text inside the scroll hint line. */
169     uint16_t dsc_offset = 1;
170     if (dsc_space > strlen(scrolldsc) + 1)
171     {
172         dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
173     }
174
175     /* Draw scroll hint line as dir symbols bracketing description text. */
176     uint16_t draw_offset = 0;
177     if      ('>' == dir)
178     {
179         draw_offset = fsize.x - 1;
180     }
181     else if ('v' == dir)
182     {
183         draw_offset = fsize.y - 1;
184     }
185     uint16_t q = 0;
186     for (; q < dsc_space; q++)
187     {
188         chtype c = dir | A_REVERSE;
189         if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
190         {
191             c = scrolldsc[q - dsc_offset] | A_REVERSE;
192         }
193         if ('<' == dir || '>' == dir)
194         {
195             mvwaddch(world.winDB.v_screen, start.y+q, start.x+draw_offset, c);
196             continue;
197         }
198         mvwaddch(world.winDB.v_screen, start.y + draw_offset, start.x + q, c);
199     }
200     free(scrolldsc);
201 }
202
203
204
205 static void winscroll_hint(struct Win * w, char dir, uint16_t dist)
206 {
207     char * unit = "lines";
208     if ('<' == dir || '>' == dir)
209     {
210         unit = "columns";
211     }
212     struct yx_uint16 start = w->start;
213     scroll_hint(w->frame_size, dir, dist, unit, start);
214 }
215
216
217
218 static void draw_win_borderlines(struct Win * w)
219 {
220     /* Draw vertical and horizontal border lines. */
221     uint16_t y, x;
222     for (y = w->start.y; y <= w->start.y + w->frame_size.y; y++)
223     {
224         mvwaddch(world.winDB.v_screen, y, w->start.x - 1,              '|');
225         mvwaddch(world.winDB.v_screen, y, w->start.x + w->frame_size.x, '|');
226     }
227     for (x = w->start.x; x <= w->start.x + w->frame_size.x; x++)
228     {
229         mvwaddch(world.winDB.v_screen, w->start.y - 1,              x, '-');
230         mvwaddch(world.winDB.v_screen, w->start.y + w->frame_size.y, x, '-');
231     }
232
233     /* Draw as much as possible of the title into center of top border line. */
234     uint8_t min_title_length_visible = 3;/* min. 1 char +2 padding/decoration*/
235     if (w->frame_size.x >= min_title_length_visible)
236     {
237         uint16_t offset = 0;
238         if (w->frame_size.x > strlen(w->title) + 2)
239         {
240             offset = (w->frame_size.x - (strlen(w->title) + 2)) / 2;
241         }                                    /* +2 is for padding/decoration */
242         uint16_t length_visible = strnlen(w->title, w->frame_size.x - 2);
243         char * title = try_malloc(length_visible + 3, "draw_win_borderlines");
244         char decoration = ' ';
245         if (w->id == world.winDB.active)
246         {
247             decoration = '$';
248         }
249         memcpy(title + 1, w->title, length_visible);
250         title[0] = title[length_visible + 1] = decoration;
251         title[length_visible + 2] = '\0';
252         mvwaddstr(world.winDB.v_screen, w->start.y-1, w->start.x+offset, title);
253         free(title);
254     }
255 }
256
257
258
259 static void draw_wins_borderlines(struct Win * w)
260 {
261     draw_win_borderlines(w);
262     struct Win * next = get_win_after(w->id);
263     if (next)
264     {
265         draw_wins_borderlines(next);
266     }
267 }
268
269
270
271 static void draw_wins_bordercorners(struct Win * w)
272 {
273     mvwaddch(world.winDB.v_screen,
274              w->start.y - 1, w->start.x - 1,              '+');
275     mvwaddch(world.winDB.v_screen,
276              w->start.y - 1, w->start.x + w->frame_size.x, '+');
277     mvwaddch(world.winDB.v_screen,
278              w->start.y + w->frame_size.y, w->start.x - 1, '+');
279     mvwaddch(world.winDB.v_screen, w->start.y + w->frame_size.y,
280              w->start.x + w->frame_size.x,                 '+');
281     struct Win * next = get_win_after(w->id);
282     if (next)
283     {
284         draw_wins_bordercorners(next);
285     }
286 }
287
288
289
290 static void draw_wins(struct Win * w)
291 {
292     void (* drawfunc) (struct Win *) = get_drawfunc_by_char(w->id);
293     if      (1 == w->view)
294     {
295         drawfunc = draw_winconf_geometry;
296     }
297     else if (2 == w->view)
298     {
299         drawfunc = draw_winconf_keybindings;
300     }
301     drawfunc(w);
302     uint16_t size_y = w->winmap_size.y;
303     uint16_t size_x = w->winmap_size.x;
304     uint16_t offset_y = center_offset(w->center.y, size_y, w->frame_size.y);
305     uint16_t offset_x = center_offset(w->center.x, size_x, w->frame_size.x);
306     uint16_t y, x;
307     for (y = offset_y; y < w->frame_size.y + offset_y && y < size_y; y++)
308     {
309         for (x = offset_x; x < w->frame_size.x + offset_x && x < size_x; x++)
310         {
311             chtype ch = w->winmap[(y * w->winmap_size.x) + x];
312             mvwaddch(world.winDB.v_screen, w->start.y + (y - offset_y),
313                                       w->start.x + (x - offset_x), ch);
314         }
315     }
316     free(w->winmap); /* NULL so draw_wins.c's try_resize_winmap() may always  */
317     w->winmap = NULL;/* free() it before (re-)allocation, even the first time.*/
318     memset(&w->winmap_size, 0, sizeof(struct yx_uint16));
319     if (offset_y > 0)
320     {
321         winscroll_hint(w, '^', offset_y + 1);
322     }
323     if (size_y > offset_y + w->frame_size.y)
324     {
325         winscroll_hint(w, 'v', size_y - ((offset_y + w->frame_size.y) - 1));
326     }
327     if (offset_x > 0)
328     {
329         winscroll_hint(w, '<', offset_x + 1);
330     }
331     if (size_x > offset_x + w->frame_size.x)
332     {
333         winscroll_hint(w, '>', size_x - ((offset_x + w->frame_size.x) - 1));
334     }
335     struct Win * next = get_win_after(w->id);
336     if (next)
337     {
338         draw_wins(next);
339     }
340 }
341
342
343
344 extern uint8_t get_win_pos_in_order(char c)
345 {
346     uint8_t i;
347     for (i = 0; c != world.winDB.order[i]; i++);
348     return i;
349 }
350
351
352
353 extern struct Win * get_win_after(char c)
354 {
355     return get_win_by_id(world.winDB.order[get_win_pos_in_order(c) + 1]);
356 }
357
358
359
360 extern uint16_t center_offset(uint16_t position, uint32_t mapsize,
361                               uint32_t frame_size)
362 {
363     uint16_t offset = 0;
364     if (mapsize > frame_size)
365     {
366         if (position > frame_size / 2)
367         {
368             if (position < mapsize - (frame_size / 2))
369             {
370                 offset = position - (frame_size / 2);
371             }
372             else
373             {
374                 offset = mapsize - frame_size;
375             }
376         }
377     }
378     return offset;
379 }
380
381
382
383 extern struct Win * get_win_by_id(char id)
384 {
385     uint8_t i = 0;
386     while ('\0' != world.winDB.ids[i])
387     {
388         if (id == world.winDB.ids[i])
389         {
390             return &world.winDB.wins[i];
391         }
392         i++;
393     }
394     return NULL;
395 }
396
397
398
399 extern void make_v_screen_and_init_win_sizes()
400 {
401     char * err_s = "creating an illegaly large virtual screen";
402     char * err_m = "triggering a memory allocation error via newpad";
403     uint32_t maxy_test = getmaxy(world.winDB.t_screen);
404     uint32_t maxx_test = getmaxx(world.winDB.t_screen);
405     exit_trouble(maxy_test>UINT16_MAX || maxx_test>UINT16_MAX, __func__, err_s);
406     world.winDB.v_screen_size.y = maxy_test;
407     world.winDB.v_screen_size.x = maxx_test;
408     world.winDB.v_screen = newpad(world.winDB.v_screen_size.y, 1);
409     exit_trouble(!world.winDB.v_screen, __func__, err_m);
410     char id;
411     while (0 != (id = get_next_win_id()))
412     {
413         init_win_size_from_winconf_and_v_screen_size(id);
414     }
415 }
416
417
418
419 extern void free_winDB()
420 {
421     char id;
422     while (0 != (id = get_next_win_id()))
423     {
424         struct Win * wc = get_win_by_id(id);
425         free(wc->title);
426         free(wc->kb.kbs);
427     }
428     free(world.winDB.ids);
429     free(world.winDB.wins);
430     free(world.winDB.order);
431 }
432
433
434
435 extern void winch_called()
436 {
437     world.winch = 1;
438 }
439
440
441
442 extern void reset_windows_on_winch()
443 {
444     endwin();  /* "[S]tandard way" to recalibrate ncurses post SIGWINCH, says */
445     refresh(); /* <http://invisible-island.net/ncurses/ncurses-intro.html>.   */
446     char * tmp_order = try_malloc(strlen(world.winDB.order) + 1, __func__);
447     int test = sprintf(tmp_order, "%s", world.winDB.order);
448     exit_trouble(test < 0, __func__, "sprintf");
449     uint8_t i;
450     char tmp_active = world.winDB.active;
451     for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
452     delwin(world.winDB.v_screen);
453     make_v_screen_and_init_win_sizes();
454     for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
455     free(tmp_order);
456     world.winDB.active = tmp_active;
457 }
458
459
460
461 extern void draw_all_wins()
462 {
463     /* Empty everything before filling it a-new. */
464     erase();
465     wnoutrefresh(world.winDB.t_screen);
466     werase(world.winDB.v_screen);
467     if (world.winDB.active)
468     {
469
470         /* Draw borders, wins. Order matters: corners should overwrite lines. */
471         draw_wins_borderlines(get_win_by_id(world.winDB.order[0]));
472         draw_wins_bordercorners(get_win_by_id(world.winDB.order[0]));
473         draw_wins(get_win_by_id(world.winDB.order[0]));
474
475         /* Draw .v_screen scroll hints. */
476         struct yx_uint16 start;
477         start.y = 0;
478         start.x = world.winDB.v_screen_offset;
479         char * cols_string = "columns";
480         if (world.winDB.v_screen_offset > 0)
481         {
482             scroll_hint(world.winDB.v_screen_size, '<',
483                         world.winDB.v_screen_offset + 1, cols_string, start);
484         }
485         uint16_t size_x = getmaxx(world.winDB.v_screen);
486         uint16_t right_edge =   world.winDB.v_screen_offset
487                               + world.winDB.v_screen_size.x;
488         if (right_edge < size_x - 1)
489         {
490             scroll_hint(world.winDB.v_screen_size, '>',
491                         size_x - right_edge, cols_string, start);
492         }
493
494         /* Put .v_screen segment to be shown on .t_screen to .t_screen buffer.*/
495         pnoutrefresh(world.winDB.v_screen, 0, world.winDB.v_screen_offset, 0, 0,
496                      world.winDB.v_screen_size.y,
497                      world.winDB.v_screen_size.x - 1);
498     }
499
500     /* Only at the end write accumulated changes to .t_screen. */
501     doupdate();
502 }