1 /* src/client/windows.c */
4 #include <ncurses.h> /* chtype, getmaxx(), getmaxy(), erase(), werase(),
5 * endwin(), delwin(), wnoutrefresh(), pnoutrefresh(),
6 * doupdate(), refresh(), delwin(), newpad(), mvwaddch(),
9 #include <stddef.h> /* NULL */
10 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX */
11 #include <stdio.h> /* sprintf() */
12 #include <stdlib.h> /* free(), atoi() */
13 #include <string.h> /* memcpy(), strlen(), strnlen(), strchr(), memset() */
14 #include "../common/err_try_fgets.h" /* err_try_fgets(), err_line() */
15 #include "../common/readwrite.h" /* try_fputc(), try_write(), try_fgetc() */
16 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
17 #include "../common/try_malloc.h" /* try_malloc() */
18 #include "../common/yx_uint16.h" /* struct yx_uint16 */
19 #include "draw_wins.h" /* draw_winconf_geometry(), draw_winconf_keybindings(),
20 * draw_win_inventory(), draw_win_info(), draw_win_log(),
21 * draw_win_available_keybindings(), draw_win_map(),
22 * draw_win_keybindings_winconf_keybindings(),
23 * draw_win_keybindings_winconf_geometry(),
24 * draw_win_keybindings_global()
26 #include "keybindings.h" /* write_keybidings_to_file(),
27 * read_keybindings_from_file()
29 #include "misc.h" /* array_append() */
30 #include "wincontrol.h" /* toggle_window() */
31 #include "world.h" /* global world */
35 /* Calculate "id"'s window's size from v_screen size and .target_(height/width).
36 * A .target_width == 0 makes the window as wide as .t_screen. .target_height ==
37 * 0 sets the maximum allowed height: one cell smaller than that of .v_screen.
38 * Negative values make width/height so many cells smaller than what 0 would
39 * set. Values that would reduce the window height or width to less than 1 cell
40 * according to the aforementioned rules set the height/width as if they were 0.
42 static void init_win_size_from_winconf_and_v_screen_size(char id);
44 /* Get (Win->draw) function identified by "c"; NULL if c not mapped to one.
45 * match_func() is just a little helper to it.
47 static uint8_t match_func(char c, void (** f) (), char c_m, void (* f_m) ());
48 static void (* get_drawfunc_by_char(char c)) ();
50 /* Iterate over chars of world.winDB.ids array / string. Restart after \0.*/
51 static char get_next_win_id();
53 /* Draw scroll hint (a line saying that there are "dist" more elements of "unit"
54 * further into the direction symbolized by "dir") into .v_screen, onto an
55 * appropriate edge of a window or .v_screen; the left/right edge if "dir" is
56 * "<"/">", or the top/bottom edge if it is "^"/"v". Use "start" as the start
57 * coordinate of the frame that is to contain the scroll hints. winscroll_hint()
58 * is a wrapper that uses the border of window "w" as this frame.
60 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
61 char * unit, struct yx_uint16 start);
62 static void winscroll_hint(struct Win * w, char dir, uint16_t dist);
64 /* draw_win_borderlines() draws vertical/horizontal borders of window "w" sans
65 * corners into .v_screen. It draws the top border line as the windows' title
66 * bar (highlighted if the window is selected as active). It is called
67 * recursively by draw_wins_borderlines() on all windows from "w" on.
68 * draw_wins_bordercorners() draws the border corners of "w" and its successors.
70 static void draw_win_borderlines(struct Win * w);
71 static void draw_wins_borderlines(struct Win * w);
72 static void draw_wins_bordercorners(struct Win * w);
74 /* Draw contents of all windows in window chain from window "w" onwards. */
75 static void draw_wins(struct Win * w);
79 static void init_win_size_from_winconf_and_v_screen_size(char id)
81 struct Win * w = get_win_by_id(id);
82 w->frame_size.y = world.winDB.v_screen_size.y - 1;
83 if ( 0 < w->target_height
84 && w->target_height <= world.winDB.v_screen_size.y - 1)
86 w->frame_size.y = w->target_height;
88 else if ( 0 > w->target_height
89 && world.winDB.v_screen_size.y + (w->target_height - 1) > 0)
91 w->frame_size.y = world.winDB.v_screen_size.y + (w->target_height - 1);
93 w->frame_size.x = world.winDB.v_screen_size.x;
94 if (0 < w->target_width)
96 w->frame_size.x = w->target_width;
98 else if ( 0 > w->target_width
99 && world.winDB.v_screen_size.x + w->target_width > 0)
101 w->frame_size.x = world.winDB.v_screen_size.x + w->target_width;
107 static uint8_t match_func(char c, void (** f) (), char c_m, void (* f_m) ())
119 static void (* get_drawfunc_by_char(char c)) ()
121 void (* f) (struct Win *) = NULL;
122 if ( match_func(c, &f, 'c', draw_win_inventory)
123 || match_func(c, &f, 'i', draw_win_info)
124 || match_func(c, &f, 'l', draw_win_log)
125 || match_func(c, &f, 'k', draw_win_available_keybindings)
126 || match_func(c, &f, 'm', draw_win_map)
127 || match_func(c, &f, '0', draw_win_keybindings_global)
128 || match_func(c, &f, '1', draw_win_keybindings_winconf_geometry)
129 || match_func(c, &f, '2', draw_win_keybindings_winconf_keybindings))
138 static char get_next_win_id()
140 static uint8_t i = 0;
141 char c = world.winDB.ids[i];
153 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
154 char * unit, struct yx_uint16 start)
156 /* Decide on alignment (vertical/horizontal?), thereby hint text space. */
157 char * more = "more";
158 uint16_t dsc_space = fsize.x;
159 if ('<' == dir || '>' == dir)
162 } /* vv-- 10 = max strlen for uint16_t */
163 char scrolldsc[1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1];
164 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
166 /* Decide on offset of the description text inside the scroll hint line. */
167 uint16_t dsc_offset = 1;
168 if (dsc_space > strlen(scrolldsc) + 1)
170 dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
173 /* Draw scroll hint line as dir symbols bracketing description text. */
174 uint16_t draw_offset = 0;
177 draw_offset = fsize.x - 1;
181 draw_offset = fsize.y - 1;
184 for (; q < dsc_space; q++)
186 chtype c = dir | A_REVERSE;
187 if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
189 c = scrolldsc[q - dsc_offset] | A_REVERSE;
191 if ('<' == dir || '>' == dir)
193 mvwaddch(world.winDB.v_screen, start.y+q, start.x+draw_offset, c);
196 mvwaddch(world.winDB.v_screen, start.y + draw_offset, start.x + q, c);
202 static void winscroll_hint(struct Win * w, char dir, uint16_t dist)
204 char * unit = "lines";
205 if ('<' == dir || '>' == dir)
209 struct yx_uint16 start = w->start;
210 scroll_hint(w->frame_size, dir, dist, unit, start);
215 static void draw_win_borderlines(struct Win * w)
217 /* Draw vertical and horizontal border lines. */
219 for (y = w->start.y; y <= w->start.y + w->frame_size.y; y++)
221 mvwaddch(world.winDB.v_screen, y, w->start.x - 1, '|');
222 mvwaddch(world.winDB.v_screen, y, w->start.x + w->frame_size.x, '|');
224 for (x = w->start.x; x <= w->start.x + w->frame_size.x; x++)
226 mvwaddch(world.winDB.v_screen, w->start.y - 1, x, '-');
227 mvwaddch(world.winDB.v_screen, w->start.y + w->frame_size.y, x, '-');
230 /* Draw as much as possible of the title into center of top border line. */
231 uint8_t min_title_length_visible = 3;/* min. 1 char +2 padding/decoration*/
232 if (w->frame_size.x >= min_title_length_visible)
235 if (w->frame_size.x > strlen(w->title) + 2)
237 offset = (w->frame_size.x - (strlen(w->title) + 2)) / 2;
238 } /* +2 is for padding/decoration */
239 uint16_t length_visible = strnlen(w->title, w->frame_size.x - 2);
240 char title[length_visible + 3];
241 char decoration = ' ';
242 if (w->id == world.winDB.active)
246 memcpy(title + 1, w->title, length_visible);
247 title[0] = title[length_visible + 1] = decoration;
248 title[length_visible + 2] = '\0';
249 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 uint8_t read_winconf_from_file(char * line, uint32_t linemax,
398 char * f_name = "read_winconf_from_file()";
399 char * context = "Failed reading individual window's configuration from "
400 "interface config file. ";
401 char * err_id = "Illegal ID(s) selected.";
402 char * err_2 = "Double-initialized window.";
403 char * err_brk = "Illegal line break type index.";
404 int test_for_end = try_fgetc(file, f_name);
405 if (EOF == test_for_end || '\n' == test_for_end)
409 exit_trouble(EOF == ungetc(test_for_end, file), f_name, "ungetc()");
410 err_try_fgets(line, linemax, file, context, "ns");
411 err_line(NULL == strchr(world.winDB.legal_ids, line[0]), line, context,
413 exit_err(world.winDB.ids && NULL!=strchr(world.winDB.ids, line[0]), err_2);
415 memset(&win, 0, sizeof(struct Win));
416 win.id = (char) line[0];
417 err_try_fgets(line, linemax, file, context, "0n");
418 win.title = try_malloc(strlen(line), f_name);
419 memcpy(win.title, line, strlen(line) - 1); /* Eliminate newline char */
420 win.title[strlen(line) - 1] = '\0'; /* char at end of string. */
421 err_try_fgets(line, linemax, file, context, "0nsi");
422 err_line(atoi(line) > 2, line, context, err_brk);
423 win.linebreak = atoi(line);
424 err_try_fgets(line, linemax, file, context, "0ni");
425 win.target_height = atoi(line);
426 win.target_height_type = (0 >= win.target_height);
427 err_try_fgets(line, linemax, file, context, "0ni");
428 win.target_width = atoi(line);
429 win.target_width_type = (0 >= win.target_width);
430 read_keybindings_from_file(line, linemax, file, &win.kb);
433 uint8_t old_ids_size = strlen(world.winDB.ids);
434 char * new_ids = try_malloc(old_ids_size + 1 + 1, f_name);
435 sprintf(new_ids, "%s%c", world.winDB.ids, win.id);
436 free(world.winDB.ids);
437 world.winDB.ids = new_ids;
438 array_append(old_ids_size, sizeof(struct Win), (void *) &win,
439 (void **) &world.winDB.wins);
442 world.winDB.ids = try_malloc(2, f_name);
443 sprintf(world.winDB.ids, "%c", win.id);
444 world.winDB.wins = try_malloc(sizeof(struct Win), f_name);
445 world.winDB.wins[0] = win;
451 extern void write_winconf_of_id_to_file(FILE * file, char c)
453 char * f_name = "write_winconf_of_id_to_file()";
454 struct Win * wc = get_win_by_id(c);
455 uint8_t size = strlen(wc->title) + 2;
456 if (size < 7) /* Ensure that at least 5 + 2 char fit into line so that */
457 { /* the digit representation of any uint16_t may be stored. */
461 sprintf(line, "%c\n", wc->id);
462 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
463 sprintf(line, "%s\n", wc->title);
464 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
465 sprintf(line, "%d\n", wc->linebreak);
466 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
467 sprintf(line, "%d\n", wc->target_height);
468 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
469 sprintf(line, "%d\n", wc->target_width);
470 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
471 write_keybindings_to_file(file, &wc->kb);
476 extern void read_order_wins_visible_active(char * line, uint32_t linemax,
477 FILE * file, char ** tmp_order,
480 char * f_name = "read_order_wins_visible_active()";
481 char * context = "Failed reading order and activation of visible windows "
482 "from interface config file. ";
483 char * err_id = "Illegal ID(s) selected.";
484 err_try_fgets(line, linemax, file, context, "01");
486 for (; i < strlen(line) - 1; i++)
488 char * test = strchr(world.winDB.legal_ids, line[i]);
489 err_line(!test, line, context, err_id);
491 line[strlen(line) - 1] = '\0';
492 *tmp_order = try_malloc(strlen(line) + 1, f_name);
493 sprintf(*tmp_order, "%s", line);
496 err_try_fgets(line, linemax, file, context, "0nfs");
497 err_line(NULL == strchr(*tmp_order, line[0]), line, context, err_id);
498 *tmp_active = line[0];
502 err_try_fgets(line, linemax, file, context, "0ne");
505 err_try_fgets(line, linemax, file, context, "d");
510 extern void write_order_wins_visible_active(FILE * file)
512 char * f_name = "write_order_wins_visible_active()";
513 try_fwrite(world.winDB.order, strlen(world.winDB.order), 1, file, f_name);
514 try_fputc('\n', file, f_name);
515 try_fputc(world.winDB.active, file, f_name);
516 try_fputc('\n', file, f_name);
517 try_fwrite(world.delim, strlen(world.delim), 1, file, f_name);
522 extern void make_v_screen_and_init_win_sizes()
524 char * f_name = "make_v_screen_and_init_win_sizes()";
525 char * err_s = "creating an illegaly large virtual screen";
526 char * err_m = "triggering a memory allocation error via newpad()";
527 uint32_t maxy_test = getmaxy(world.winDB.t_screen);
528 uint32_t maxx_test = getmaxx(world.winDB.t_screen);
529 exit_trouble(maxy_test>UINT16_MAX || maxx_test>UINT16_MAX, f_name, err_s);
530 world.winDB.v_screen_size.y = maxy_test;
531 world.winDB.v_screen_size.x = maxx_test;
532 world.winDB.v_screen = newpad(world.winDB.v_screen_size.y, 1);
533 exit_trouble(NULL == world.winDB.v_screen, f_name, err_m);
535 while (0 != (id = get_next_win_id()))
537 init_win_size_from_winconf_and_v_screen_size(id);
543 extern void free_winDB()
546 while (0 != (id = get_next_win_id()))
548 struct Win * wc = get_win_by_id(id);
553 free(world.winDB.ids); /* NULL this too since add_win_to_winDB() checks */
554 world.winDB.ids = NULL; /* for it to detect its first post-DB-purge round.*/
555 free(world.winDB.wins);
556 free(world.winDB.order);
561 extern void winch_called()
568 extern void reset_windows_on_winch()
570 endwin(); /* "[S]tandard way" to recalibrate ncurses post SIGWINCH, says */
571 refresh(); /* <http://invisible-island.net/ncurses/ncurses-intro.html>. */
572 char tmp_order[strlen(world.winDB.order) + 1];
573 sprintf(tmp_order, "%s", world.winDB.order);
575 char tmp_active = world.winDB.active;
576 for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
577 delwin(world.winDB.v_screen);
578 make_v_screen_and_init_win_sizes();
579 for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
580 world.winDB.active = tmp_active;
585 extern void draw_all_wins()
587 /* Empty everything before filling it a-new. */
589 wnoutrefresh(world.winDB.t_screen);
590 werase(world.winDB.v_screen);
591 if (world.winDB.active)
594 /* Draw borders, wins. Order matters: corners should overwrite lines. */
595 draw_wins_borderlines(get_win_by_id(world.winDB.order[0]));
596 draw_wins_bordercorners(get_win_by_id(world.winDB.order[0]));
597 draw_wins(get_win_by_id(world.winDB.order[0]));
599 /* Draw .v_screen scroll hints. */
600 struct yx_uint16 start;
602 start.x = world.winDB.v_screen_offset;
603 char * cols_string = "columns";
604 if (world.winDB.v_screen_offset > 0)
606 scroll_hint(world.winDB.v_screen_size, '<',
607 world.winDB.v_screen_offset + 1, cols_string, start);
609 uint16_t size_x = getmaxx(world.winDB.v_screen);
610 uint16_t right_edge = world.winDB.v_screen_offset
611 + world.winDB.v_screen_size.x;
612 if (right_edge < size_x - 1)
614 scroll_hint(world.winDB.v_screen_size, '>',
615 size_x - right_edge, cols_string, start);
618 /* Put .v_screen segment to be shown on .t_screen to .t_screen buffer.*/
619 pnoutrefresh(world.winDB.v_screen, 0, world.winDB.v_screen_offset, 0, 0,
620 world.winDB.v_screen_size.y,
621 world.winDB.v_screen_size.x - 1);
624 /* Only at the end write accumulated changes to .t_screen. */