1 /* src/client/windows.c */
3 #define _POSIX_C_SOURCE 200809L /* strnlen() */
5 #include <ncurses.h> /* chtype, getmaxx(), getmaxy(), erase(), werase(),
6 * endwin(), delwin(), wnoutrefresh(), pnoutrefresh(),
7 * doupdate(), refresh(), delwin(), newpad(), mvwaddch(),
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/try_malloc.h" /* try_malloc() */
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()
24 #include "wincontrol.h" /* toggle_window() */
25 #include "world.h" /* world */
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.
36 static void init_win_size_from_winconf_and_v_screen_size(char id);
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.
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)) ();
44 /* Iterate over chars of world.winDB.ids array / string. Restart after \0.*/
45 static char get_next_win_id();
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.
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);
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.
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);
68 /* Draw contents of all windows in window chain from window "w" onwards. */
69 static void draw_wins(struct Win * w);
73 static void init_win_size_from_winconf_and_v_screen_size(char id)
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)
80 w->frame_size.y = w->target_height;
82 else if ( 0 > w->target_height
83 && world.winDB.v_screen_size.y + (w->target_height - 1) > 0)
85 w->frame_size.y = world.winDB.v_screen_size.y + (w->target_height - 1);
87 w->frame_size.x = world.winDB.v_screen_size.x;
88 if (0 < w->target_width)
90 w->frame_size.x = w->target_width;
92 else if ( 0 > w->target_width
93 && world.winDB.v_screen_size.x + w->target_width > 0)
95 w->frame_size.x = world.winDB.v_screen_size.x + w->target_width;
101 static uint8_t match_func(char c, void (** f) (), char c_m, void (* f_m) ())
113 static void (* get_drawfunc_by_char(char c)) ()
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))
132 static char get_next_win_id()
134 static uint8_t i = 0;
135 char c = world.winDB.ids[i];
147 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
148 char * unit, struct yx_uint16 start)
150 char * f_name = "sprintf()";
152 /* Decide on alignment (vertical/horizontal?), thereby hint text space. */
153 char * more = "more";
154 uint16_t dsc_space = fsize.x;
155 if ('<' == dir || '>' == dir)
158 } /* vv-- 10 = max strlen for uint16_t */
159 uint16_t size = 1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1;
160 char * scrolldsc = try_malloc(size, f_name);
161 int test = sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
162 exit_trouble(test < 0, f_name, "sprintf()");
164 /* Decide on offset of the description text inside the scroll hint line. */
165 uint16_t dsc_offset = 1;
166 if (dsc_space > strlen(scrolldsc) + 1)
168 dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
171 /* Draw scroll hint line as dir symbols bracketing description text. */
172 uint16_t draw_offset = 0;
175 draw_offset = fsize.x - 1;
179 draw_offset = fsize.y - 1;
182 for (; q < dsc_space; q++)
184 chtype c = dir | A_REVERSE;
185 if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
187 c = scrolldsc[q - dsc_offset] | A_REVERSE;
189 if ('<' == dir || '>' == dir)
191 mvwaddch(world.winDB.v_screen, start.y+q, start.x+draw_offset, c);
194 mvwaddch(world.winDB.v_screen, start.y + draw_offset, start.x + q, c);
201 static void winscroll_hint(struct Win * w, char dir, uint16_t dist)
203 char * unit = "lines";
204 if ('<' == dir || '>' == dir)
208 struct yx_uint16 start = w->start;
209 scroll_hint(w->frame_size, dir, dist, unit, start);
214 static void draw_win_borderlines(struct Win * w)
216 /* Draw vertical and horizontal border lines. */
218 for (y = w->start.y; y <= w->start.y + w->frame_size.y; y++)
220 mvwaddch(world.winDB.v_screen, y, w->start.x - 1, '|');
221 mvwaddch(world.winDB.v_screen, y, w->start.x + w->frame_size.x, '|');
223 for (x = w->start.x; x <= w->start.x + w->frame_size.x; x++)
225 mvwaddch(world.winDB.v_screen, w->start.y - 1, x, '-');
226 mvwaddch(world.winDB.v_screen, w->start.y + w->frame_size.y, x, '-');
229 /* Draw as much as possible of the title into center of top border line. */
230 uint8_t min_title_length_visible = 3;/* min. 1 char +2 padding/decoration*/
231 if (w->frame_size.x >= min_title_length_visible)
234 if (w->frame_size.x > strlen(w->title) + 2)
236 offset = (w->frame_size.x - (strlen(w->title) + 2)) / 2;
237 } /* +2 is for padding/decoration */
238 uint16_t length_visible = strnlen(w->title, w->frame_size.x - 2);
239 char * title = try_malloc(length_visible + 3, "draw_win_borderlines()");
240 char decoration = ' ';
241 if (w->id == world.winDB.active)
245 memcpy(title + 1, w->title, length_visible);
246 title[0] = title[length_visible + 1] = decoration;
247 title[length_visible + 2] = '\0';
248 mvwaddstr(world.winDB.v_screen, w->start.y-1, w->start.x+offset, title);
255 static void draw_wins_borderlines(struct Win * w)
257 draw_win_borderlines(w);
258 struct Win * next = get_win_after(w->id);
261 draw_wins_borderlines(next);
267 static void draw_wins_bordercorners(struct Win * w)
269 mvwaddch(world.winDB.v_screen,
270 w->start.y - 1, w->start.x - 1, '+');
271 mvwaddch(world.winDB.v_screen,
272 w->start.y - 1, w->start.x + w->frame_size.x, '+');
273 mvwaddch(world.winDB.v_screen,
274 w->start.y + w->frame_size.y, w->start.x - 1, '+');
275 mvwaddch(world.winDB.v_screen, w->start.y + w->frame_size.y,
276 w->start.x + w->frame_size.x, '+');
277 struct Win * next = get_win_after(w->id);
280 draw_wins_bordercorners(next);
286 static void draw_wins(struct Win * w)
288 void (* drawfunc) (struct Win *) = get_drawfunc_by_char(w->id);
291 drawfunc = draw_winconf_geometry;
293 else if (2 == w->view)
295 drawfunc = draw_winconf_keybindings;
298 uint16_t size_y = w->winmap_size.y;
299 uint16_t size_x = w->winmap_size.x;
300 uint16_t offset_y = center_offset(w->center.y, size_y, w->frame_size.y);
301 uint16_t offset_x = center_offset(w->center.x, size_x, w->frame_size.x);
303 for (y = offset_y; y < w->frame_size.y + offset_y && y < size_y; y++)
305 for (x = offset_x; x < w->frame_size.x + offset_x && x < size_x; x++)
307 chtype ch = w->winmap[(y * w->winmap_size.x) + x];
308 mvwaddch(world.winDB.v_screen, w->start.y + (y - offset_y),
309 w->start.x + (x - offset_x), ch);
312 free(w->winmap); /* NULL so draw_wins.c's try_resize_winmap() may always */
313 w->winmap = NULL;/* free() it before (re-)allocation, even the first time.*/
314 memset(&w->winmap_size, 0, sizeof(struct yx_uint16));
317 winscroll_hint(w, '^', offset_y + 1);
319 if (size_y > offset_y + w->frame_size.y)
321 winscroll_hint(w, 'v', size_y - ((offset_y + w->frame_size.y) - 1));
325 winscroll_hint(w, '<', offset_x + 1);
327 if (size_x > offset_x + w->frame_size.x)
329 winscroll_hint(w, '>', size_x - ((offset_x + w->frame_size.x) - 1));
331 struct Win * next = get_win_after(w->id);
340 extern uint8_t get_win_pos_in_order(char c)
343 for (i = 0; c != world.winDB.order[i]; i++);
349 extern struct Win * get_win_after(char c)
351 return get_win_by_id(world.winDB.order[get_win_pos_in_order(c) + 1]);
356 extern uint16_t center_offset(uint16_t position, uint32_t mapsize,
360 if (mapsize > frame_size)
362 if (position > frame_size / 2)
364 if (position < mapsize - (frame_size / 2))
366 offset = position - (frame_size / 2);
370 offset = mapsize - frame_size;
379 extern struct Win * get_win_by_id(char id)
382 while ('\0' != world.winDB.ids[i])
384 if (id == world.winDB.ids[i])
386 return &world.winDB.wins[i];
395 extern void make_v_screen_and_init_win_sizes()
397 char * f_name = "make_v_screen_and_init_win_sizes()";
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, f_name, 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(NULL == world.winDB.v_screen, f_name, err_m);
408 while (0 != (id = get_next_win_id()))
410 init_win_size_from_winconf_and_v_screen_size(id);
416 extern void free_winDB()
419 while (0 != (id = get_next_win_id()))
421 struct Win * wc = get_win_by_id(id);
425 free(world.winDB.ids);
426 free(world.winDB.wins);
427 free(world.winDB.order);
432 extern void winch_called()
439 extern void reset_windows_on_winch()
441 char * f_name = "reset_windows_on_winch()";
442 endwin(); /* "[S]tandard way" to recalibrate ncurses post SIGWINCH, says */
443 refresh(); /* <http://invisible-island.net/ncurses/ncurses-intro.html>. */
444 char * tmp_order = try_malloc(strlen(world.winDB.order) + 1, f_name);
445 int test = sprintf(tmp_order, "%s", world.winDB.order);
446 exit_trouble(test < 0, f_name, "sprintf()");
448 char tmp_active = world.winDB.active;
449 for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
450 delwin(world.winDB.v_screen);
451 make_v_screen_and_init_win_sizes();
452 for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
454 world.winDB.active = tmp_active;
459 extern void draw_all_wins()
461 /* Empty everything before filling it a-new. */
463 wnoutrefresh(world.winDB.t_screen);
464 werase(world.winDB.v_screen);
465 if (world.winDB.active)
468 /* Draw borders, wins. Order matters: corners should overwrite lines. */
469 draw_wins_borderlines(get_win_by_id(world.winDB.order[0]));
470 draw_wins_bordercorners(get_win_by_id(world.winDB.order[0]));
471 draw_wins(get_win_by_id(world.winDB.order[0]));
473 /* Draw .v_screen scroll hints. */
474 struct yx_uint16 start;
476 start.x = world.winDB.v_screen_offset;
477 char * cols_string = "columns";
478 if (world.winDB.v_screen_offset > 0)
480 scroll_hint(world.winDB.v_screen_size, '<',
481 world.winDB.v_screen_offset + 1, cols_string, start);
483 uint16_t size_x = getmaxx(world.winDB.v_screen);
484 uint16_t right_edge = world.winDB.v_screen_offset
485 + world.winDB.v_screen_size.x;
486 if (right_edge < size_x - 1)
488 scroll_hint(world.winDB.v_screen_size, '>',
489 size_x - right_edge, cols_string, start);
492 /* Put .v_screen segment to be shown on .t_screen to .t_screen buffer.*/
493 pnoutrefresh(world.winDB.v_screen, 0, world.winDB.v_screen_offset, 0, 0,
494 world.winDB.v_screen_size.y,
495 world.winDB.v_screen_size.x - 1);
498 /* Only at the end write accumulated changes to .t_screen. */