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