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