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, "scroll_hint()");
159 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
161 /* Decide on offset of the description text inside the scroll hint line. */
162 uint16_t dsc_offset = 1;
163 if (dsc_space > strlen(scrolldsc) + 1)
165 dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
168 /* Draw scroll hint line as dir symbols bracketing description text. */
169 uint16_t draw_offset = 0;
172 draw_offset = fsize.x - 1;
176 draw_offset = fsize.y - 1;
179 for (; q < dsc_space; q++)
181 chtype c = dir | A_REVERSE;
182 if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
184 c = scrolldsc[q - dsc_offset] | A_REVERSE;
186 if ('<' == dir || '>' == dir)
188 mvwaddch(world.winDB.v_screen, start.y+q, start.x+draw_offset, c);
191 mvwaddch(world.winDB.v_screen, start.y + draw_offset, start.x + q, c);
198 static void winscroll_hint(struct Win * w, char dir, uint16_t dist)
200 char * unit = "lines";
201 if ('<' == dir || '>' == dir)
205 struct yx_uint16 start = w->start;
206 scroll_hint(w->frame_size, dir, dist, unit, start);
211 static void draw_win_borderlines(struct Win * w)
213 char * f_name = "draw_win_borderlines()";
215 /* Draw vertical and horizontal border lines. */
217 for (y = w->start.y; y <= w->start.y + w->frame_size.y; y++)
219 mvwaddch(world.winDB.v_screen, y, w->start.x - 1, '|');
220 mvwaddch(world.winDB.v_screen, y, w->start.x + w->frame_size.x, '|');
222 for (x = w->start.x; x <= w->start.x + w->frame_size.x; x++)
224 mvwaddch(world.winDB.v_screen, w->start.y - 1, x, '-');
225 mvwaddch(world.winDB.v_screen, w->start.y + w->frame_size.y, x, '-');
228 /* Draw as much as possible of the title into center of top border line. */
229 uint8_t min_title_length_visible = 3;/* min. 1 char +2 padding/decoration*/
230 if (w->frame_size.x >= min_title_length_visible)
233 if (w->frame_size.x > strlen(w->title) + 2)
235 offset = (w->frame_size.x - (strlen(w->title) + 2)) / 2;
236 } /* +2 is for padding/decoration */
237 uint16_t length_visible = strnlen(w->title, w->frame_size.x - 2);
238 char * title = try_malloc(length_visible + 3, f_name);
239 char decoration = ' ';
240 if (w->id == world.winDB.active)
244 memcpy(title + 1, w->title, length_visible);
245 title[0] = title[length_visible + 1] = decoration;
246 title[length_visible + 2] = '\0';
247 mvwaddstr(world.winDB.v_screen, w->start.y-1, w->start.x+offset, title);
254 static void draw_wins_borderlines(struct Win * w)
256 draw_win_borderlines(w);
257 struct Win * next = get_win_after(w->id);
260 draw_wins_borderlines(next);
266 static void draw_wins_bordercorners(struct Win * w)
268 mvwaddch(world.winDB.v_screen,
269 w->start.y - 1, w->start.x - 1, '+');
270 mvwaddch(world.winDB.v_screen,
271 w->start.y - 1, w->start.x + w->frame_size.x, '+');
272 mvwaddch(world.winDB.v_screen,
273 w->start.y + w->frame_size.y, w->start.x - 1, '+');
274 mvwaddch(world.winDB.v_screen, w->start.y + w->frame_size.y,
275 w->start.x + w->frame_size.x, '+');
276 struct Win * next = get_win_after(w->id);
279 draw_wins_bordercorners(next);
285 static void draw_wins(struct Win * w)
287 void (* drawfunc) (struct Win *) = get_drawfunc_by_char(w->id);
290 drawfunc = draw_winconf_geometry;
292 else if (2 == w->view)
294 drawfunc = draw_winconf_keybindings;
297 uint16_t size_y = w->winmap_size.y;
298 uint16_t size_x = w->winmap_size.x;
299 uint16_t offset_y = center_offset(w->center.y, size_y, w->frame_size.y);
300 uint16_t offset_x = center_offset(w->center.x, size_x, w->frame_size.x);
302 for (y = offset_y; y < w->frame_size.y + offset_y && y < size_y; y++)
304 for (x = offset_x; x < w->frame_size.x + offset_x && x < size_x; x++)
306 chtype ch = w->winmap[(y * w->winmap_size.x) + x];
307 mvwaddch(world.winDB.v_screen, w->start.y + (y - offset_y),
308 w->start.x + (x - offset_x), ch);
311 free(w->winmap); /* NULL so draw_wins.c's try_resize_winmap() may always */
312 w->winmap = NULL;/* free() it before (re-)allocation, even the first time.*/
313 memset(&w->winmap_size, 0, sizeof(struct yx_uint16));
316 winscroll_hint(w, '^', offset_y + 1);
318 if (size_y > offset_y + w->frame_size.y)
320 winscroll_hint(w, 'v', size_y - ((offset_y + w->frame_size.y) - 1));
324 winscroll_hint(w, '<', offset_x + 1);
326 if (size_x > offset_x + w->frame_size.x)
328 winscroll_hint(w, '>', size_x - ((offset_x + w->frame_size.x) - 1));
330 struct Win * next = get_win_after(w->id);
339 extern uint8_t get_win_pos_in_order(char c)
342 for (i = 0; c != world.winDB.order[i]; i++);
348 extern struct Win * get_win_after(char c)
350 return get_win_by_id(world.winDB.order[get_win_pos_in_order(c) + 1]);
355 extern uint16_t center_offset(uint16_t position, uint32_t mapsize,
359 if (mapsize > frame_size)
361 if (position > frame_size / 2)
363 if (position < mapsize - (frame_size / 2))
365 offset = position - (frame_size / 2);
369 offset = mapsize - frame_size;
378 extern struct Win * get_win_by_id(char id)
381 while ('\0' != world.winDB.ids[i])
383 if (id == world.winDB.ids[i])
385 return &world.winDB.wins[i];
394 extern void make_v_screen_and_init_win_sizes()
396 char * f_name = "make_v_screen_and_init_win_sizes()";
397 char * err_s = "creating an illegaly large virtual screen";
398 char * err_m = "triggering a memory allocation error via newpad()";
399 uint32_t maxy_test = getmaxy(world.winDB.t_screen);
400 uint32_t maxx_test = getmaxx(world.winDB.t_screen);
401 exit_trouble(maxy_test>UINT16_MAX || maxx_test>UINT16_MAX, f_name, err_s);
402 world.winDB.v_screen_size.y = maxy_test;
403 world.winDB.v_screen_size.x = maxx_test;
404 world.winDB.v_screen = newpad(world.winDB.v_screen_size.y, 1);
405 exit_trouble(NULL == world.winDB.v_screen, f_name, err_m);
407 while (0 != (id = get_next_win_id()))
409 init_win_size_from_winconf_and_v_screen_size(id);
415 extern void free_winDB()
418 while (0 != (id = get_next_win_id()))
420 struct Win * wc = get_win_by_id(id);
424 free(world.winDB.ids);
425 free(world.winDB.wins);
426 free(world.winDB.order);
431 extern void winch_called()
438 extern void reset_windows_on_winch()
440 char * f_name = "reset_windows_on_winch()";
441 endwin(); /* "[S]tandard way" to recalibrate ncurses post SIGWINCH, says */
442 refresh(); /* <http://invisible-island.net/ncurses/ncurses-intro.html>. */
443 char * tmp_order = try_malloc(strlen(world.winDB.order) + 1, f_name);
444 sprintf(tmp_order, "%s", world.winDB.order);
446 char tmp_active = world.winDB.active;
447 for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
448 delwin(world.winDB.v_screen);
449 make_v_screen_and_init_win_sizes();
450 for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
452 world.winDB.active = tmp_active;
457 extern void draw_all_wins()
459 /* Empty everything before filling it a-new. */
461 wnoutrefresh(world.winDB.t_screen);
462 werase(world.winDB.v_screen);
463 if (world.winDB.active)
466 /* Draw borders, wins. Order matters: corners should overwrite lines. */
467 draw_wins_borderlines(get_win_by_id(world.winDB.order[0]));
468 draw_wins_bordercorners(get_win_by_id(world.winDB.order[0]));
469 draw_wins(get_win_by_id(world.winDB.order[0]));
471 /* Draw .v_screen scroll hints. */
472 struct yx_uint16 start;
474 start.x = world.winDB.v_screen_offset;
475 char * cols_string = "columns";
476 if (world.winDB.v_screen_offset > 0)
478 scroll_hint(world.winDB.v_screen_size, '<',
479 world.winDB.v_screen_offset + 1, cols_string, start);
481 uint16_t size_x = getmaxx(world.winDB.v_screen);
482 uint16_t right_edge = world.winDB.v_screen_offset
483 + world.winDB.v_screen_size.x;
484 if (right_edge < size_x - 1)
486 scroll_hint(world.winDB.v_screen_size, '>',
487 size_x - right_edge, cols_string, start);
490 /* Put .v_screen segment to be shown on .t_screen to .t_screen buffer.*/
491 pnoutrefresh(world.winDB.v_screen, 0, world.winDB.v_screen_offset, 0, 0,
492 world.winDB.v_screen_size.y,
493 world.winDB.v_screen_size.x - 1);
496 /* Only at the end write accumulated changes to .t_screen. */