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(), atoi() */
14 #include <string.h> /* memcpy(), strlen(), strnlen(), strchr(), memset() */
15 #include "../common/err_try_fgets.h" /* err_try_fgets(), err_line() */
16 #include "../common/readwrite.h" /* try_fputc(), try_write(), try_fgetc() */
17 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
18 #include "../common/try_malloc.h" /* try_malloc() */
19 #include "../common/yx_uint16.h" /* struct yx_uint16 */
20 #include "array_append.h" /* array_append() */
21 #include "draw_wins.h" /* draw_winconf_geometry(), draw_winconf_keybindings(),
22 * draw_win_inventory(), draw_win_info(), draw_win_log(),
23 * draw_win_available_keybindings(), draw_win_map(),
24 * draw_win_keybindings_winconf_keybindings(),
25 * draw_win_keybindings_winconf_geometry(),
26 * draw_win_keybindings_global()
28 #include "keybindings.h" /* write_keybidings_to_file(),
29 * read_keybindings_from_file()
31 #include "wincontrol.h" /* toggle_window() */
32 #include "world.h" /* global world */
36 /* Calculate "id"'s window's size from v_screen size and .target_(height/width).
37 * A .target_width == 0 makes the window as wide as .t_screen. .target_height ==
38 * 0 sets the maximum allowed height: one cell smaller than that of .v_screen.
39 * Negative values make width/height so many cells smaller than what 0 would
40 * set. Values that would reduce the window height or width to less than 1 cell
41 * according to the aforementioned rules set the height/width as if they were 0.
43 static void init_win_size_from_winconf_and_v_screen_size(char id);
45 /* Get (Win->draw) function identified by "c"; NULL if c not mapped to one.
46 * match_func() is just a little helper to it.
48 static uint8_t match_func(char c, void (** f) (), char c_m, void (* f_m) ());
49 static void (* get_drawfunc_by_char(char c)) ();
51 /* Iterate over chars of world.winDB.ids array / string. Restart after \0.*/
52 static char get_next_win_id();
54 /* Draw scroll hint (a line saying that there are "dist" more elements of "unit"
55 * further into the direction symbolized by "dir") into .v_screen, onto an
56 * appropriate edge of a window or .v_screen; the left/right edge if "dir" is
57 * "<"/">", or the top/bottom edge if it is "^"/"v". Use "start" as the start
58 * coordinate of the frame that is to contain the scroll hints. winscroll_hint()
59 * is a wrapper that uses the border of window "w" as this frame.
61 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
62 char * unit, struct yx_uint16 start);
63 static void winscroll_hint(struct Win * w, char dir, uint16_t dist);
65 /* draw_win_borderlines() draws vertical/horizontal borders of window "w" sans
66 * corners into .v_screen. It draws the top border line as the windows' title
67 * bar (highlighted if the window is selected as active). It is called
68 * recursively by draw_wins_borderlines() on all windows from "w" on.
69 * draw_wins_bordercorners() draws the border corners of "w" and its successors.
71 static void draw_win_borderlines(struct Win * w);
72 static void draw_wins_borderlines(struct Win * w);
73 static void draw_wins_bordercorners(struct Win * w);
75 /* Draw contents of all windows in window chain from window "w" onwards. */
76 static void draw_wins(struct Win * w);
80 static void init_win_size_from_winconf_and_v_screen_size(char id)
82 struct Win * w = get_win_by_id(id);
83 w->frame_size.y = world.winDB.v_screen_size.y - 1;
84 if ( 0 < w->target_height
85 && w->target_height <= world.winDB.v_screen_size.y - 1)
87 w->frame_size.y = w->target_height;
89 else if ( 0 > w->target_height
90 && world.winDB.v_screen_size.y + (w->target_height - 1) > 0)
92 w->frame_size.y = world.winDB.v_screen_size.y + (w->target_height - 1);
94 w->frame_size.x = world.winDB.v_screen_size.x;
95 if (0 < w->target_width)
97 w->frame_size.x = w->target_width;
99 else if ( 0 > w->target_width
100 && world.winDB.v_screen_size.x + w->target_width > 0)
102 w->frame_size.x = world.winDB.v_screen_size.x + w->target_width;
108 static uint8_t match_func(char c, void (** f) (), char c_m, void (* f_m) ())
120 static void (* get_drawfunc_by_char(char c)) ()
122 void (* f) (struct Win *) = NULL;
123 if ( match_func(c, &f, 'c', draw_win_inventory)
124 || match_func(c, &f, 'i', draw_win_info)
125 || match_func(c, &f, 'l', draw_win_log)
126 || match_func(c, &f, 'k', draw_win_available_keybindings)
127 || match_func(c, &f, 'm', draw_win_map)
128 || match_func(c, &f, '0', draw_win_keybindings_global)
129 || match_func(c, &f, '1', draw_win_keybindings_winconf_geometry)
130 || match_func(c, &f, '2', draw_win_keybindings_winconf_keybindings))
139 static char get_next_win_id()
141 static uint8_t i = 0;
142 char c = world.winDB.ids[i];
154 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
155 char * unit, struct yx_uint16 start)
157 /* Decide on alignment (vertical/horizontal?), thereby hint text space. */
158 char * more = "more";
159 uint16_t dsc_space = fsize.x;
160 if ('<' == dir || '>' == dir)
163 } /* vv-- 10 = max strlen for uint16_t */
164 char scrolldsc[1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1];
165 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
167 /* Decide on offset of the description text inside the scroll hint line. */
168 uint16_t dsc_offset = 1;
169 if (dsc_space > strlen(scrolldsc) + 1)
171 dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
174 /* Draw scroll hint line as dir symbols bracketing description text. */
175 uint16_t draw_offset = 0;
178 draw_offset = fsize.x - 1;
182 draw_offset = fsize.y - 1;
185 for (; q < dsc_space; q++)
187 chtype c = dir | A_REVERSE;
188 if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
190 c = scrolldsc[q - dsc_offset] | A_REVERSE;
192 if ('<' == dir || '>' == dir)
194 mvwaddch(world.winDB.v_screen, start.y+q, start.x+draw_offset, c);
197 mvwaddch(world.winDB.v_screen, start.y + draw_offset, start.x + q, c);
203 static void winscroll_hint(struct Win * w, char dir, uint16_t dist)
205 char * unit = "lines";
206 if ('<' == dir || '>' == dir)
210 struct yx_uint16 start = w->start;
211 scroll_hint(w->frame_size, dir, dist, unit, start);
216 static void draw_win_borderlines(struct Win * w)
218 /* Draw vertical and horizontal border lines. */
220 for (y = w->start.y; y <= w->start.y + w->frame_size.y; y++)
222 mvwaddch(world.winDB.v_screen, y, w->start.x - 1, '|');
223 mvwaddch(world.winDB.v_screen, y, w->start.x + w->frame_size.x, '|');
225 for (x = w->start.x; x <= w->start.x + w->frame_size.x; x++)
227 mvwaddch(world.winDB.v_screen, w->start.y - 1, x, '-');
228 mvwaddch(world.winDB.v_screen, w->start.y + w->frame_size.y, x, '-');
231 /* Draw as much as possible of the title into center of top border line. */
232 uint8_t min_title_length_visible = 3;/* min. 1 char +2 padding/decoration*/
233 if (w->frame_size.x >= min_title_length_visible)
236 if (w->frame_size.x > strlen(w->title) + 2)
238 offset = (w->frame_size.x - (strlen(w->title) + 2)) / 2;
239 } /* +2 is for padding/decoration */
240 uint16_t length_visible = strnlen(w->title, w->frame_size.x - 2);
241 char title[length_visible + 3];
242 char decoration = ' ';
243 if (w->id == world.winDB.active)
247 memcpy(title + 1, w->title, length_visible);
248 title[0] = title[length_visible + 1] = decoration;
249 title[length_visible + 2] = '\0';
250 mvwaddstr(world.winDB.v_screen, w->start.y-1, w->start.x+offset, title);
256 static void draw_wins_borderlines(struct Win * w)
258 draw_win_borderlines(w);
259 struct Win * next = get_win_after(w->id);
262 draw_wins_borderlines(next);
268 static void draw_wins_bordercorners(struct Win * w)
270 mvwaddch(world.winDB.v_screen,
271 w->start.y - 1, w->start.x - 1, '+');
272 mvwaddch(world.winDB.v_screen,
273 w->start.y - 1, w->start.x + w->frame_size.x, '+');
274 mvwaddch(world.winDB.v_screen,
275 w->start.y + w->frame_size.y, w->start.x - 1, '+');
276 mvwaddch(world.winDB.v_screen, w->start.y + w->frame_size.y,
277 w->start.x + w->frame_size.x, '+');
278 struct Win * next = get_win_after(w->id);
281 draw_wins_bordercorners(next);
287 static void draw_wins(struct Win * w)
289 void (* drawfunc) (struct Win *) = get_drawfunc_by_char(w->id);
292 drawfunc = draw_winconf_geometry;
294 else if (2 == w->view)
296 drawfunc = draw_winconf_keybindings;
299 uint16_t size_y = w->winmap_size.y;
300 uint16_t size_x = w->winmap_size.x;
301 uint16_t offset_y = center_offset(w->center.y, size_y, w->frame_size.y);
302 uint16_t offset_x = center_offset(w->center.x, size_x, w->frame_size.x);
304 for (y = offset_y; y < w->frame_size.y + offset_y && y < size_y; y++)
306 for (x = offset_x; x < w->frame_size.x + offset_x && x < size_x; x++)
308 chtype ch = w->winmap[(y * w->winmap_size.x) + x];
309 mvwaddch(world.winDB.v_screen, w->start.y + (y - offset_y),
310 w->start.x + (x - offset_x), ch);
313 free(w->winmap); /* NULL so draw_wins.c's try_resize_winmap() may always */
314 w->winmap = NULL;/* free() it before (re-)allocation, even the first time.*/
315 memset(&w->winmap_size, 0, sizeof(struct yx_uint16));
318 winscroll_hint(w, '^', offset_y + 1);
320 if (size_y > offset_y + w->frame_size.y)
322 winscroll_hint(w, 'v', size_y - ((offset_y + w->frame_size.y) - 1));
326 winscroll_hint(w, '<', offset_x + 1);
328 if (size_x > offset_x + w->frame_size.x)
330 winscroll_hint(w, '>', size_x - ((offset_x + w->frame_size.x) - 1));
332 struct Win * next = get_win_after(w->id);
341 extern uint8_t get_win_pos_in_order(char c)
344 for (i = 0; c != world.winDB.order[i]; i++);
350 extern struct Win * get_win_after(char c)
352 return get_win_by_id(world.winDB.order[get_win_pos_in_order(c) + 1]);
357 extern uint16_t center_offset(uint16_t position, uint32_t mapsize,
361 if (mapsize > frame_size)
363 if (position > frame_size / 2)
365 if (position < mapsize - (frame_size / 2))
367 offset = position - (frame_size / 2);
371 offset = mapsize - frame_size;
380 extern struct Win * get_win_by_id(char id)
383 while ('\0' != world.winDB.ids[i])
385 if (id == world.winDB.ids[i])
387 return &world.winDB.wins[i];
396 extern uint8_t read_winconf_from_file(char * line, uint32_t linemax,
399 char * f_name = "read_winconf_from_file()";
400 char * context = "Failed reading individual window's configuration from "
401 "interface config file. ";
402 char * err_id = "Illegal ID(s) selected.";
403 char * err_2 = "Double-initialized window.";
404 char * err_brk = "Illegal line break type index.";
405 int test_for_end = try_fgetc(file, f_name);
406 if (EOF == test_for_end || '\n' == test_for_end)
410 exit_trouble(EOF == ungetc(test_for_end, file), f_name, "ungetc()");
411 err_try_fgets(line, linemax, file, context, "ns");
412 err_line(NULL == strchr(world.winDB.legal_ids, line[0]), line, context,
414 exit_err(world.winDB.ids && NULL!=strchr(world.winDB.ids, line[0]), err_2);
416 memset(&win, 0, sizeof(struct Win));
417 win.id = (char) line[0];
418 err_try_fgets(line, linemax, file, context, "0n");
419 win.title = try_malloc(strlen(line), f_name);
420 memcpy(win.title, line, strlen(line) - 1); /* Eliminate newline char */
421 win.title[strlen(line) - 1] = '\0'; /* char at end of string. */
422 err_try_fgets(line, linemax, file, context, "0nsi");
423 err_line(atoi(line) > 2, line, context, err_brk);
424 win.linebreak = atoi(line);
425 err_try_fgets(line, linemax, file, context, "0ni");
426 win.target_height = atoi(line);
427 win.target_height_type = (0 >= win.target_height);
428 err_try_fgets(line, linemax, file, context, "0ni");
429 win.target_width = atoi(line);
430 win.target_width_type = (0 >= win.target_width);
431 read_keybindings_from_file(line, linemax, file, &win.kb);
434 uint8_t old_ids_size = strlen(world.winDB.ids);
435 char * new_ids = try_malloc(old_ids_size + 1 + 1, f_name);
436 sprintf(new_ids, "%s%c", world.winDB.ids, win.id);
437 free(world.winDB.ids);
438 world.winDB.ids = new_ids;
439 array_append(old_ids_size, sizeof(struct Win), (void *) &win,
440 (void **) &world.winDB.wins);
443 world.winDB.ids = try_malloc(2, f_name);
444 sprintf(world.winDB.ids, "%c", win.id);
445 world.winDB.wins = try_malloc(sizeof(struct Win), f_name);
446 world.winDB.wins[0] = win;
452 extern void write_winconf_of_id_to_file(FILE * file, char c)
454 char * f_name = "write_winconf_of_id_to_file()";
455 struct Win * wc = get_win_by_id(c);
456 uint8_t size = strlen(wc->title) + 2;
457 if (size < 7) /* Ensure that at least 5 + 2 char fit into line so that */
458 { /* the digit representation of any uint16_t may be stored. */
462 sprintf(line, "%c\n", wc->id);
463 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
464 sprintf(line, "%s\n", wc->title);
465 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
466 sprintf(line, "%d\n", wc->linebreak);
467 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
468 sprintf(line, "%d\n", wc->target_height);
469 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
470 sprintf(line, "%d\n", wc->target_width);
471 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
472 write_keybindings_to_file(file, &wc->kb);
477 extern void read_order_wins_visible_active(char * line, uint32_t linemax,
478 FILE * file, char ** tmp_order,
481 char * f_name = "read_order_wins_visible_active()";
482 char * context = "Failed reading order and activation of visible windows "
483 "from interface config file. ";
484 char * err_id = "Illegal ID(s) selected.";
485 err_try_fgets(line, linemax, file, context, "01");
487 for (; i < strlen(line) - 1; i++)
489 char * test = strchr(world.winDB.legal_ids, line[i]);
490 err_line(!test, line, context, err_id);
492 line[strlen(line) - 1] = '\0';
493 *tmp_order = try_malloc(strlen(line) + 1, f_name);
494 sprintf(*tmp_order, "%s", line);
497 err_try_fgets(line, linemax, file, context, "0nfs");
498 err_line(NULL == strchr(*tmp_order, line[0]), line, context, err_id);
499 *tmp_active = line[0];
503 err_try_fgets(line, linemax, file, context, "0ne");
506 err_try_fgets(line, linemax, file, context, "d");
511 extern void write_order_wins_visible_active(FILE * file)
513 char * f_name = "write_order_wins_visible_active()";
514 try_fwrite(world.winDB.order, strlen(world.winDB.order), 1, file, f_name);
515 try_fputc('\n', file, f_name);
516 try_fputc(world.winDB.active, file, f_name);
517 try_fputc('\n', file, f_name);
518 try_fwrite(world.delim, strlen(world.delim), 1, file, f_name);
523 extern void make_v_screen_and_init_win_sizes()
525 char * f_name = "make_v_screen_and_init_win_sizes()";
526 char * err_s = "creating an illegaly large virtual screen";
527 char * err_m = "triggering a memory allocation error via newpad()";
528 uint32_t maxy_test = getmaxy(world.winDB.t_screen);
529 uint32_t maxx_test = getmaxx(world.winDB.t_screen);
530 exit_trouble(maxy_test>UINT16_MAX || maxx_test>UINT16_MAX, f_name, err_s);
531 world.winDB.v_screen_size.y = maxy_test;
532 world.winDB.v_screen_size.x = maxx_test;
533 world.winDB.v_screen = newpad(world.winDB.v_screen_size.y, 1);
534 exit_trouble(NULL == world.winDB.v_screen, f_name, err_m);
536 while (0 != (id = get_next_win_id()))
538 init_win_size_from_winconf_and_v_screen_size(id);
544 extern void free_winDB()
547 while (0 != (id = get_next_win_id()))
549 struct Win * wc = get_win_by_id(id);
554 free(world.winDB.ids); /* NULL this since read_winconf_from_file() checks */
555 world.winDB.ids = NULL;/* for it to detect its first post-DB-purge round. */
556 free(world.winDB.wins);
557 free(world.winDB.order);
562 extern void winch_called()
569 extern void reset_windows_on_winch()
571 endwin(); /* "[S]tandard way" to recalibrate ncurses post SIGWINCH, says */
572 refresh(); /* <http://invisible-island.net/ncurses/ncurses-intro.html>. */
573 char tmp_order[strlen(world.winDB.order) + 1];
574 sprintf(tmp_order, "%s", world.winDB.order);
576 char tmp_active = world.winDB.active;
577 for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
578 delwin(world.winDB.v_screen);
579 make_v_screen_and_init_win_sizes();
580 for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
581 world.winDB.active = tmp_active;
586 extern void draw_all_wins()
588 /* Empty everything before filling it a-new. */
590 wnoutrefresh(world.winDB.t_screen);
591 werase(world.winDB.v_screen);
592 if (world.winDB.active)
595 /* Draw borders, wins. Order matters: corners should overwrite lines. */
596 draw_wins_borderlines(get_win_by_id(world.winDB.order[0]));
597 draw_wins_bordercorners(get_win_by_id(world.winDB.order[0]));
598 draw_wins(get_win_by_id(world.winDB.order[0]));
600 /* Draw .v_screen scroll hints. */
601 struct yx_uint16 start;
603 start.x = world.winDB.v_screen_offset;
604 char * cols_string = "columns";
605 if (world.winDB.v_screen_offset > 0)
607 scroll_hint(world.winDB.v_screen_size, '<',
608 world.winDB.v_screen_offset + 1, cols_string, start);
610 uint16_t size_x = getmaxx(world.winDB.v_screen);
611 uint16_t right_edge = world.winDB.v_screen_offset
612 + world.winDB.v_screen_size.x;
613 if (right_edge < size_x - 1)
615 scroll_hint(world.winDB.v_screen_size, '>',
616 size_x - right_edge, cols_string, start);
619 /* Put .v_screen segment to be shown on .t_screen to .t_screen buffer.*/
620 pnoutrefresh(world.winDB.v_screen, 0, world.winDB.v_screen_offset, 0, 0,
621 world.winDB.v_screen_size.y,
622 world.winDB.v_screen_size.x - 1);
625 /* Only at the end write accumulated changes to .t_screen. */