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 "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()
23 #include "wincontrol.h" /* toggle_window() */
24 #include "world.h" /* world */
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.
35 static void init_win_size_from_winconf_and_v_screen_size(char id);
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.
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)) ();
43 /* Iterate over chars of world.winDB.ids array / string. Restart after \0.*/
44 static char get_next_win_id();
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.
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);
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.
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);
67 /* Draw contents of all windows in window chain from window "w" onwards. */
68 static void draw_wins(struct Win * w);
72 static void init_win_size_from_winconf_and_v_screen_size(char id)
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)
79 w->frame_size.y = w->target_height;
81 else if ( 0 > w->target_height
82 && world.winDB.v_screen_size.y + (w->target_height - 1) > 0)
84 w->frame_size.y = world.winDB.v_screen_size.y + (w->target_height - 1);
86 w->frame_size.x = world.winDB.v_screen_size.x;
87 if (0 < w->target_width)
89 w->frame_size.x = w->target_width;
91 else if ( 0 > w->target_width
92 && world.winDB.v_screen_size.x + w->target_width > 0)
94 w->frame_size.x = world.winDB.v_screen_size.x + w->target_width;
100 static uint8_t match_func(char c, void (** f) (), char c_m, void (* f_m) ())
112 static void (* get_drawfunc_by_char(char c)) ()
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))
131 static char get_next_win_id()
133 static uint8_t i = 0;
134 char c = world.winDB.ids[i];
146 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
147 char * unit, struct yx_uint16 start)
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)
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);
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)
163 dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
166 /* Draw scroll hint line as dir symbols bracketing description text. */
167 uint16_t draw_offset = 0;
170 draw_offset = fsize.x - 1;
174 draw_offset = fsize.y - 1;
177 for (; q < dsc_space; q++)
179 chtype c = dir | A_REVERSE;
180 if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
182 c = scrolldsc[q - dsc_offset] | A_REVERSE;
184 if ('<' == dir || '>' == dir)
186 mvwaddch(world.winDB.v_screen, start.y+q, start.x+draw_offset, c);
189 mvwaddch(world.winDB.v_screen, start.y + draw_offset, start.x + q, c);
195 static void winscroll_hint(struct Win * w, char dir, uint16_t dist)
197 char * unit = "lines";
198 if ('<' == dir || '>' == dir)
202 struct yx_uint16 start = w->start;
203 scroll_hint(w->frame_size, dir, dist, unit, start);
208 static void draw_win_borderlines(struct Win * w)
210 /* Draw vertical and horizontal border lines. */
212 for (y = w->start.y; y <= w->start.y + w->frame_size.y; y++)
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, '|');
217 for (x = w->start.x; x <= w->start.x + w->frame_size.x; x++)
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, '-');
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)
228 if (w->frame_size.x > strlen(w->title) + 2)
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)
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);
248 static void draw_wins_borderlines(struct Win * w)
250 draw_win_borderlines(w);
251 struct Win * next = get_win_after(w->id);
254 draw_wins_borderlines(next);
260 static void draw_wins_bordercorners(struct Win * w)
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);
273 draw_wins_bordercorners(next);
279 static void draw_wins(struct Win * w)
281 void (* drawfunc) (struct Win *) = get_drawfunc_by_char(w->id);
284 drawfunc = draw_winconf_geometry;
286 else if (2 == w->view)
288 drawfunc = draw_winconf_keybindings;
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);
296 for (y = offset_y; y < w->frame_size.y + offset_y && y < size_y; y++)
298 for (x = offset_x; x < w->frame_size.x + offset_x && x < size_x; x++)
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);
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));
310 winscroll_hint(w, '^', offset_y + 1);
312 if (size_y > offset_y + w->frame_size.y)
314 winscroll_hint(w, 'v', size_y - ((offset_y + w->frame_size.y) - 1));
318 winscroll_hint(w, '<', offset_x + 1);
320 if (size_x > offset_x + w->frame_size.x)
322 winscroll_hint(w, '>', size_x - ((offset_x + w->frame_size.x) - 1));
324 struct Win * next = get_win_after(w->id);
333 extern uint8_t get_win_pos_in_order(char c)
336 for (i = 0; c != world.winDB.order[i]; i++);
342 extern struct Win * get_win_after(char c)
344 return get_win_by_id(world.winDB.order[get_win_pos_in_order(c) + 1]);
349 extern uint16_t center_offset(uint16_t position, uint32_t mapsize,
353 if (mapsize > frame_size)
355 if (position > frame_size / 2)
357 if (position < mapsize - (frame_size / 2))
359 offset = position - (frame_size / 2);
363 offset = mapsize - frame_size;
372 extern struct Win * get_win_by_id(char id)
375 while ('\0' != world.winDB.ids[i])
377 if (id == world.winDB.ids[i])
379 return &world.winDB.wins[i];
388 extern void make_v_screen_and_init_win_sizes()
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);
401 while (0 != (id = get_next_win_id()))
403 init_win_size_from_winconf_and_v_screen_size(id);
409 extern void free_winDB()
412 while (0 != (id = get_next_win_id()))
414 struct Win * wc = get_win_by_id(id);
418 free(world.winDB.ids);
419 free(world.winDB.wins);
420 free(world.winDB.order);
425 extern void winch_called()
432 extern void reset_windows_on_winch()
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);
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;
449 extern void draw_all_wins()
451 /* Empty everything before filling it a-new. */
453 wnoutrefresh(world.winDB.t_screen);
454 werase(world.winDB.v_screen);
455 if (world.winDB.active)
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]));
463 /* Draw .v_screen scroll hints. */
464 struct yx_uint16 start;
466 start.x = world.winDB.v_screen_offset;
467 char * cols_string = "columns";
468 if (world.winDB.v_screen_offset > 0)
470 scroll_hint(world.winDB.v_screen_size, '<',
471 world.winDB.v_screen_offset + 1, cols_string, start);
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)
478 scroll_hint(world.winDB.v_screen_size, '>',
479 size_x - right_edge, cols_string, start);
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);
488 /* Only at the end write accumulated changes to .t_screen. */