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 /* Decide on alignment (vertical/horizontal?), thereby hint text space. */
151 char * more = "more";
152 uint16_t dsc_space = fsize.x;
153 if ('<' == dir || '>' == dir)
156 } /* vv-- 10 = max strlen for uint16_t */
157 uint16_t size = 1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1;
158 char * scrolldsc = try_malloc(size, __func__);
159 int test = sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
160 exit_trouble(test < 0, __func__, "sprintf");
162 /* Decide on offset of the description text inside the scroll hint line. */
163 uint16_t dsc_offset = 1;
164 if (dsc_space > strlen(scrolldsc) + 1)
166 dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
169 /* Draw scroll hint line as dir symbols bracketing description text. */
170 uint16_t draw_offset = 0;
173 draw_offset = fsize.x - 1;
177 draw_offset = fsize.y - 1;
180 for (; q < dsc_space; q++)
182 chtype c = dir | A_REVERSE;
183 if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
185 c = scrolldsc[q - dsc_offset] | A_REVERSE;
187 if ('<' == dir || '>' == dir)
189 mvwaddch(world.winDB.v_screen, start.y+q, start.x+draw_offset, c);
192 mvwaddch(world.winDB.v_screen, start.y + draw_offset, start.x + q, c);
199 static void winscroll_hint(struct Win * w, char dir, uint16_t dist)
201 char * unit = "lines";
202 if ('<' == dir || '>' == dir)
206 struct yx_uint16 start = w->start;
207 scroll_hint(w->frame_size, dir, dist, unit, start);
212 static void draw_win_borderlines(struct Win * w)
214 /* Draw vertical and horizontal border lines. */
216 for (y = w->start.y; y <= w->start.y + w->frame_size.y; y++)
218 mvwaddch(world.winDB.v_screen, y, w->start.x - 1, '|');
219 mvwaddch(world.winDB.v_screen, y, w->start.x + w->frame_size.x, '|');
221 for (x = w->start.x; x <= w->start.x + w->frame_size.x; x++)
223 mvwaddch(world.winDB.v_screen, w->start.y - 1, x, '-');
224 mvwaddch(world.winDB.v_screen, w->start.y + w->frame_size.y, x, '-');
227 /* Draw as much as possible of the title into center of top border line. */
228 uint8_t min_title_length_visible = 3;/* min. 1 char +2 padding/decoration*/
229 if (w->frame_size.x >= min_title_length_visible)
232 if (w->frame_size.x > strlen(w->title) + 2)
234 offset = (w->frame_size.x - (strlen(w->title) + 2)) / 2;
235 } /* +2 is for padding/decoration */
236 uint16_t length_visible = strnlen(w->title, w->frame_size.x - 2);
237 char * title = try_malloc(length_visible + 3, "draw_win_borderlines");
238 char decoration = ' ';
239 if (w->id == world.winDB.active)
243 memcpy(title + 1, w->title, length_visible);
244 title[0] = title[length_visible + 1] = decoration;
245 title[length_visible + 2] = '\0';
246 mvwaddstr(world.winDB.v_screen, w->start.y-1, w->start.x+offset, title);
253 static void draw_wins_borderlines(struct Win * w)
255 draw_win_borderlines(w);
256 struct Win * next = get_win_after(w->id);
259 draw_wins_borderlines(next);
265 static void draw_wins_bordercorners(struct Win * w)
267 mvwaddch(world.winDB.v_screen,
268 w->start.y - 1, w->start.x - 1, '+');
269 mvwaddch(world.winDB.v_screen,
270 w->start.y - 1, w->start.x + w->frame_size.x, '+');
271 mvwaddch(world.winDB.v_screen,
272 w->start.y + w->frame_size.y, w->start.x - 1, '+');
273 mvwaddch(world.winDB.v_screen, w->start.y + w->frame_size.y,
274 w->start.x + w->frame_size.x, '+');
275 struct Win * next = get_win_after(w->id);
278 draw_wins_bordercorners(next);
284 static void draw_wins(struct Win * w)
286 void (* drawfunc) (struct Win *) = get_drawfunc_by_char(w->id);
289 drawfunc = draw_winconf_geometry;
291 else if (2 == w->view)
293 drawfunc = draw_winconf_keybindings;
296 uint16_t size_y = w->winmap_size.y;
297 uint16_t size_x = w->winmap_size.x;
298 uint16_t offset_y = center_offset(w->center.y, size_y, w->frame_size.y);
299 uint16_t offset_x = center_offset(w->center.x, size_x, w->frame_size.x);
301 for (y = offset_y; y < w->frame_size.y + offset_y && y < size_y; y++)
303 for (x = offset_x; x < w->frame_size.x + offset_x && x < size_x; x++)
305 chtype ch = w->winmap[(y * w->winmap_size.x) + x];
306 mvwaddch(world.winDB.v_screen, w->start.y + (y - offset_y),
307 w->start.x + (x - offset_x), ch);
310 free(w->winmap); /* NULL so draw_wins.c's try_resize_winmap() may always */
311 w->winmap = NULL;/* free() it before (re-)allocation, even the first time.*/
312 memset(&w->winmap_size, 0, sizeof(struct yx_uint16));
315 winscroll_hint(w, '^', offset_y + 1);
317 if (size_y > offset_y + w->frame_size.y)
319 winscroll_hint(w, 'v', size_y - ((offset_y + w->frame_size.y) - 1));
323 winscroll_hint(w, '<', offset_x + 1);
325 if (size_x > offset_x + w->frame_size.x)
327 winscroll_hint(w, '>', size_x - ((offset_x + w->frame_size.x) - 1));
329 struct Win * next = get_win_after(w->id);
338 extern uint8_t get_win_pos_in_order(char c)
341 for (i = 0; c != world.winDB.order[i]; i++);
347 extern struct Win * get_win_after(char c)
349 return get_win_by_id(world.winDB.order[get_win_pos_in_order(c) + 1]);
354 extern uint16_t center_offset(uint16_t position, uint32_t mapsize,
358 if (mapsize > frame_size)
360 if (position > frame_size / 2)
362 if (position < mapsize - (frame_size / 2))
364 offset = position - (frame_size / 2);
368 offset = mapsize - frame_size;
377 extern struct Win * get_win_by_id(char id)
380 while ('\0' != world.winDB.ids[i])
382 if (id == world.winDB.ids[i])
384 return &world.winDB.wins[i];
393 extern void make_v_screen_and_init_win_sizes()
395 char * err_s = "creating an illegaly large virtual screen";
396 char * err_m = "triggering a memory allocation error via newpad";
397 uint32_t maxy_test = getmaxy(world.winDB.t_screen);
398 uint32_t maxx_test = getmaxx(world.winDB.t_screen);
399 exit_trouble(maxy_test>UINT16_MAX || maxx_test>UINT16_MAX, __func__, err_s);
400 world.winDB.v_screen_size.y = maxy_test;
401 world.winDB.v_screen_size.x = maxx_test;
402 world.winDB.v_screen = newpad(world.winDB.v_screen_size.y, 1);
403 exit_trouble(!world.winDB.v_screen, __func__, err_m);
405 while (0 != (id = get_next_win_id()))
407 init_win_size_from_winconf_and_v_screen_size(id);
413 extern void free_winDB()
416 while (0 != (id = get_next_win_id()))
418 struct Win * wc = get_win_by_id(id);
422 free(world.winDB.ids);
423 free(world.winDB.wins);
424 free(world.winDB.order);
429 extern void winch_called()
436 extern void reset_windows_on_winch()
438 endwin(); /* "[S]tandard way" to recalibrate ncurses post SIGWINCH, says */
439 refresh(); /* <http://invisible-island.net/ncurses/ncurses-intro.html>. */
440 char * tmp_order = try_malloc(strlen(world.winDB.order) + 1, __func__);
441 int test = sprintf(tmp_order, "%s", world.winDB.order);
442 exit_trouble(test < 0, __func__, "sprintf");
444 char tmp_active = world.winDB.active;
445 for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
446 delwin(world.winDB.v_screen);
447 make_v_screen_and_init_win_sizes();
448 for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
450 world.winDB.active = tmp_active;
455 extern void draw_all_wins()
457 /* Empty everything before filling it a-new. */
459 wnoutrefresh(world.winDB.t_screen);
460 werase(world.winDB.v_screen);
461 if (world.winDB.active)
464 /* Draw borders, wins. Order matters: corners should overwrite lines. */
465 draw_wins_borderlines(get_win_by_id(world.winDB.order[0]));
466 draw_wins_bordercorners(get_win_by_id(world.winDB.order[0]));
467 draw_wins(get_win_by_id(world.winDB.order[0]));
469 /* Draw .v_screen scroll hints. */
470 struct yx_uint16 start;
472 start.x = world.winDB.v_screen_offset;
473 char * cols_string = "columns";
474 if (world.winDB.v_screen_offset > 0)
476 scroll_hint(world.winDB.v_screen_size, '<',
477 world.winDB.v_screen_offset + 1, cols_string, start);
479 uint16_t size_x = getmaxx(world.winDB.v_screen);
480 uint16_t right_edge = world.winDB.v_screen_offset
481 + world.winDB.v_screen_size.x;
482 if (right_edge < size_x - 1)
484 scroll_hint(world.winDB.v_screen_size, '>',
485 size_x - right_edge, cols_string, start);
488 /* Put .v_screen segment to be shown on .t_screen to .t_screen buffer.*/
489 pnoutrefresh(world.winDB.v_screen, 0, world.winDB.v_screen_offset, 0, 0,
490 world.winDB.v_screen_size.y,
491 world.winDB.v_screen_size.x - 1);
494 /* Only at the end write accumulated changes to .t_screen. */