1 /* src/client/draw_wins.c */
4 #include <ncurses.h> /* typedefs attr_t, chtype, define A_REVERSE */
5 #include <stddef.h> /* NULL */
6 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX */
7 #include <stdio.h> /* sprintf() */
8 #include <stdlib.h> /* free() */
9 #include <string.h> /* memset(), strchr(), strdup/(), strlen() */
10 #include "../common/rexit.h" /* exit_err() */
11 #include "../common/try_malloc.h" /* try_malloc() */
12 #include "../common/yx_uint16.h" /* struct yx_uint16 */
13 #include "keybindings.h" /* struct KeyBindingDB, get_keyname_to_keycode() */
14 #include "windows.h" /* struct Win, get_win_by_id() */
15 #include "world.h" /* global world */
19 /* Apply to the winmap of Win "w" the new sizes "new_size_y" and "new_size_x"
20 * to the degree that they extend it. Re-shape the window content accordingly.
22 static void try_resize_winmap(struct Win * w, int new_size_y, int new_size_x);
24 /* In Win "w", write "ch" to coordinate "y"/"x". */
25 static void set_ch_on_yx(struct Win * w, int y, int x, chtype ch);
27 /* Add "line" into window "w" with "attri" set on all chars. add_line() selects
28 * one of the other three functions based on the "win"->linebreak mode.
30 * "wide" writes lines to full width without in-between breaks. "long" breaks
31 * lines on the right window border. "compact" replaces newlines with printable
32 * separator characters. "offset" and "last" are only relevant to the "compact"
33 * mode. "offset" reports the horizontal offset at which the line is drawn from
34 * the lowest existing winmap line on; it is updated to the next offset after
35 * the new line, to be used by the next add_line_compact() call. "last" marks
36 * the line to add as the last in the window, so no further separator is added.
38 static void add_line(struct Win * win, char * line, attr_t attri,
39 uint16_t * offset, uint8_t last);
40 static void add_line_wide(struct Win * win, char * line, attr_t attri);
41 static void add_line_long(struct Win * win, char * line, attr_t attri);
42 static void add_line_compact(struct Win * win, char * line, attr_t attri,
43 uint16_t * offset, uint8_t last);
45 /* Add linebreaks-containing "text" to "win", formatted as per "win"->linebreak.
47 * draw_text_from_bottom() prepends empty lines to "text" to push its last line
48 * to the low frame edge when the frame is of greater height than the winmap
49 * would be otherwise; else, set "win"->center.y to the winmap's lowest line.
51 static void add_text_with_linebreaks(struct Win * win, char * text);
52 static void draw_text_from_bottom(struct Win * win, char * text);
54 /* Return a properly formatted keybinding list line for "kb". */
55 static char * get_kb_line(struct KeyBinding * kb, uint8_t linebreak_type);
57 /* Draw from line "start" on config view for keybindings defined at "kb". */
58 static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
61 /* Draw into window "w" from line "start" on a "title" followed by an empty
62 * line followed by a list of all keybindings starting in "kbdb".
64 static void draw_titled_keybinding_list(char * title, struct Win * win,
65 uint16_t * offset, uint8_t last,
66 struct KeyBindingDB * kbdb);
70 static void try_resize_winmap(struct Win * win, int new_size_y, int new_size_x)
72 char * f_name = "try_resize_winmap()";
73 if (win->winmap_size.y >= new_size_y && win->winmap_size.x >= new_size_x)
77 if (win->winmap_size.y > new_size_y)
79 new_size_y = win->winmap_size.y;
81 else if (win->winmap_size.x > new_size_x)
83 new_size_x = win->winmap_size.x;
85 chtype * old_winmap = win->winmap;
86 uint32_t new_size = sizeof(chtype) * new_size_y * new_size_x;
87 win->winmap = try_malloc(new_size, f_name);
89 for (y = 0; y < new_size_y; y++)
91 for (x = 0; y < win->winmap_size.y && x < win->winmap_size.x; x++)
93 chtype ch = old_winmap[(y * win->winmap_size.x) + x];
94 win->winmap[(y * new_size_x) + x] = ch;
96 for (; x < new_size_x; x++)
98 win->winmap[(y * new_size_x) + x] = ' ';
102 win->winmap_size.y = new_size_y;
103 win->winmap_size.x = new_size_x;
108 static void set_ch_on_yx(struct Win * win, int y, int x, chtype ch)
110 win->winmap[(y * win->winmap_size.x) + x] = ch;
115 static void add_line(struct Win * win, char * line, attr_t attri,
116 uint16_t * offset, uint8_t last)
118 exit_err(strlen(line) > UINT16_MAX, "Line in add_line() too big.");
119 if (0 == win->linebreak)
121 add_line_long(win, line, attri);
123 else if (1 == win->linebreak)
125 add_line_wide(win, line, attri);
127 else if (2 == win->linebreak)
129 add_line_compact(win, line, attri, offset, last);
135 static void add_line_wide(struct Win * win, char * line, attr_t attri)
137 uint16_t y_start = win->winmap_size.y;
138 uint16_t len_line = strlen(line);
139 try_resize_winmap(win, y_start + 1, win->frame_size.x);
140 try_resize_winmap(win, y_start + 1, len_line);
142 for (x = 0; x < len_line; x++)
144 set_ch_on_yx(win, y_start, x, line[x] | attri);
148 for (; x < win->frame_size.x; x++)
150 set_ch_on_yx(win, y_start, x, ' ' | attri);
157 static void add_line_long(struct Win * win, char * line, attr_t attri)
159 uint16_t y_start = win->winmap_size.y;
160 uint16_t len_line = strlen(line);
161 try_resize_winmap(win, y_start + 1, win->frame_size.x);
163 for (z = 0, y = y_start; z < len_line; y++)
165 for (x = 0; x < win->winmap_size.x && z < len_line; x++, z++)
167 set_ch_on_yx(win, y, x, line[z] | attri);
171 try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
178 static void add_line_compact(struct Win * win, char * line, attr_t attri,
179 uint16_t * offset, uint8_t last)
181 uint16_t y_start = win->winmap_size.y - (win->winmap_size.y > 0);
182 try_resize_winmap(win, y_start + 1, win->frame_size.x);
183 uint16_t len_line = strlen(line);
184 char * separator = last ? "" : " / ";
185 uint32_t len_line_new = len_line + strlen(separator);
186 char line_new[len_line_new];
187 sprintf(line_new, "%s%s", line, separator);
190 for (z = 0, y = y_start; z < len_line_new; y++)
192 for (x = * offset; x < win->winmap_size.x && z < len_line_new; x++, z++)
194 set_ch_on_yx(win, y, x, line_new[z] | attri);
195 if (z + 1 == (uint32_t) len_line)
201 if (z < len_line_new)
203 try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
211 static void add_text_with_linebreaks(struct Win * win, char * text)
215 char * text_copy = strdup(text);
216 char * start = text_copy;
217 while (NULL != (limit = strchr(start, '\n')))
220 add_line(win, start, 0, &offset, 0);
223 add_line(win, start, 0, &offset, 1);
229 static void draw_text_from_bottom(struct Win * win, char * text)
231 add_text_with_linebreaks(win, text);
232 if (win->winmap_size.y > win->frame_size.y)
234 win->center.y = win->winmap_size.y - 1;
236 else if (win->winmap_size.y < win->frame_size.y)
238 uint16_t new_y_start = win->frame_size.y - win->winmap_size.y;
239 memset(&win->winmap_size, 0, sizeof(struct yx_uint16));
244 add_line_wide(win, " ", 0);
246 while (new_y_start > win->winmap_size.y);
247 if (2 == win->linebreak) /* add_text_with_linebreaks() will start not */
248 { /* not after, but within the last line then. */
249 add_line_wide(win, " ", 0);
251 add_text_with_linebreaks(win, text);
257 static char * get_kb_line(struct KeyBinding * kb, uint8_t linebreak_type)
259 char * f_name = "get_kb_line()";
260 char * keyname = get_keyname_to_keycode(kb->keycode);
261 char * format = "%-9s %s";
262 uint16_t first_size = 9;
263 if (1 != linebreak_type)
266 first_size = strlen(keyname) + 1;
268 uint16_t size = first_size + 1 + strlen(kb->command->dsc_long) + 1;
269 char * kb_line = try_malloc(size, f_name);
270 sprintf(kb_line, format, keyname, kb->command->dsc_long);
277 static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
280 if (0 == kbdb->n_of_kbs)
282 add_line(win, "(none)", 0, &offset, 0);
286 for (kb_n = 0; kb_n < kbdb->n_of_kbs; kb_n++)
289 if (kb_n == kbdb->select)
294 attri = attri | A_BLINK;
296 win->center.y = win->winmap_size.y;
298 char * kb_line = get_kb_line(&kbdb->kbs[kb_n], win->linebreak);
299 add_line(win, kb_line, attri, &offset, (kbdb->n_of_kbs == kb_n + 1));
306 static void draw_titled_keybinding_list(char * title, struct Win * win,
307 uint16_t * offset, uint8_t last,
308 struct KeyBindingDB * kbdb)
312 while (0 == state || kb_n < kbdb->n_of_kbs)
316 add_line(win, title, 0, offset, 0);
317 add_line(win, " ", 0, offset, 0);
318 state = 1 + (0 == kbdb->n_of_kbs);
321 char * kb_line = get_kb_line(&kbdb->kbs[kb_n], win->linebreak);
322 add_line(win, kb_line, 0, offset, (last * kbdb->n_of_kbs == kb_n + 1));
328 add_line(win, "(none)", 0, offset, last);
334 extern void draw_win_log(struct Win * win)
340 draw_text_from_bottom(win, world.log);
345 extern void draw_win_map(struct Win * win)
347 try_resize_winmap(win, world.map.size.y, world.map.size.x);
350 for (y = 0; y < world.map.size.y; y++)
352 for (x = 0; x < world.map.size.x; x++)
354 set_ch_on_yx(win, y, x, world.map.cells[z]);
362 extern void draw_win_info(struct Win * win)
364 char * dsc_turn = "Turn: ";
365 char * dsc_hitpoints = "\nHitpoints: ";
366 uint16_t maxl = strlen(dsc_turn) + 5 + strlen(dsc_hitpoints) + 3;
368 sprintf(text, "%s%d%s%d",
369 dsc_turn, world.turn, dsc_hitpoints, world.player_lifepoints);
370 add_text_with_linebreaks(win, text);
375 extern void draw_win_inventory(struct Win * win)
378 char * text_copy = strdup(world.player_inventory);
379 char * start = text_copy;
381 uint8_t found_selection = 0;
383 while (NULL != (limit = strchr(start, '\n')))
387 if (i == world.player_inventory_select)
391 win->center.y = win->winmap_size.y;
393 add_line(win, start, attri, &offset, 0);
397 win->center.y = !found_selection ? win->winmap_size.y : win->center.y;
398 add_line(win, start, !found_selection * A_REVERSE, &offset, 1);
404 extern void draw_win_available_keybindings(struct Win * win)
406 char * title = "Active window's keybindings:";
407 struct KeyBindingDB * kbdb;
408 struct Win * win_active = get_win_by_id(world.winDB.active);
409 if (0 == win_active->view)
411 kbdb = &win_active->kb;
413 else if (1 == win_active->view)
415 kbdb = &world.kb_wingeom;
417 else if (2 == win_active->view)
419 kbdb = &world.kb_winkeys;
422 draw_titled_keybinding_list(title, win, &offset, 0, kbdb);
423 add_line(win, " ", 0, &offset, 0);
424 title = "Global keybindings:";
425 draw_titled_keybinding_list(title, win, &offset, 1, &world.kb_global);
431 extern void draw_win_keybindings_global(struct Win * win)
433 win->center.y = world.kb_global.select;
434 draw_keybinding_config(win, &world.kb_global, 0);
439 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
441 win->center.y = world.kb_wingeom.select;
442 draw_keybinding_config(win, &world.kb_wingeom, 0);
447 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
449 win->center.y = world.kb_winkeys.select;
450 draw_keybinding_config(win, &world.kb_winkeys, 0);
455 extern void draw_winconf_keybindings(struct Win * win)
457 char * title = "Window's keybindings:";
459 add_line(win, title, 0, &offset, 0);
460 add_line(win, " ", 0, &offset, 0);
461 draw_keybinding_config(win, &win->kb, offset);
466 extern void draw_winconf_geometry(struct Win * win)
468 char * title = "Window's geometry:\n\n";
469 char * h_title = "Height to save: ";
470 char h_value[6 + 1]; /* 6: int16_t value max strlen */
471 sprintf(h_value, "%d", win->target_height);
472 char * h_plus = " (width in cells)\n\n";
473 char * h_minus = " (negative diff: cells to screen width)\n\n";
474 char * h_type = (1 == win->target_height_type) ? h_minus : h_plus;
475 char * w_title = "Width to save: ";
477 sprintf(w_value, "%d", win->target_width);
478 char * w_plus = "(height in cells)\n\n";
479 char * w_minus = " (negative diff: cells to screen height)\n\n";
480 char * w_type = (1 == win->target_width_type) ? w_minus : w_plus;
481 char * breaks_title = "Linebreak type: ";
482 char * breaks_type = (1 == win->linebreak) ? "wide" : "long";
483 breaks_type = (2 == win->linebreak) ? "compact" : breaks_type;
484 uint16_t text_size = strlen(title)
485 + strlen(h_title) + strlen(h_value) + strlen(h_type)
486 + strlen(w_title) + strlen(w_value) + strlen(w_type)
487 + strlen(breaks_title) + strlen(breaks_type);
488 char text[text_size + 1];
489 sprintf(text, "%s%s%s%s%s%s%s%s%s", title, h_title, h_value, h_type,
490 w_title, w_value, w_type, breaks_title, breaks_type);
491 add_text_with_linebreaks(win, text);