home · contact · privacy
Test return values of _all_ *printf() calls.
[plomrogue] / src / client / draw_wins.c
1 /* src/client/draw_wins.c */
2
3 #define _POSIX_C_SOURCE 200809L /* strdup() */
4 #include "draw_wins.h"
5 #include <ncurses.h> /* typedefs attr_t, chtype, define A_REVERSE */
6 #include <stddef.h> /* NULL */
7 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX */
8 #include <stdio.h> /* sprintf() */
9 #include <stdlib.h> /* free() */
10 #include <string.h> /* memset(), strchr(), strdup/(), strlen() */
11 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
12 #include "../common/try_malloc.h" /* try_malloc() */
13 #include "keybindings.h" /* struct KeyBindingDB, get_keyname_to_keycode() */
14 #include "windows.h" /* yx_uint16, Win, get_win_by_id() */
15 #include "world.h" /* global world */
16
17
18
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.
21  */
22 static void try_resize_winmap(struct Win * w, int new_size_y, int new_size_x);
23
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);
26
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.
29  *
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.
37  */
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);
44
45 /* Add linebreaks-containing "text" to "win", formatted as per "win"->linebreak.
46  *
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.
50  */
51 static void add_text_with_linebreaks(struct Win * win, char * text);
52 static void draw_text_from_bottom(struct Win * win, char * text);
53
54 /* Return a properly formatted keybinding list line for "kb". */
55 static char * get_kb_line(struct KeyBinding * kb);
56
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,
59                                    uint16_t offset);
60
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".
63  */
64 static void draw_titled_keybinding_list(char * title, struct Win * win,
65                                         uint16_t * offset, uint8_t last,
66                                         struct KeyBindingDB * kbdb);
67
68
69
70 static void try_resize_winmap(struct Win * win, int new_size_y, int new_size_x)
71 {
72     char * f_name = "try_resize_winmap()";
73     if (win->winmap_size.y >= new_size_y && win->winmap_size.x >= new_size_x)
74     {
75         return;
76     }
77     if      (win->winmap_size.y > new_size_y)
78     {
79         new_size_y = win->winmap_size.y;
80     }
81     else if (win->winmap_size.x > new_size_x)
82     {
83         new_size_x = win->winmap_size.x;
84     }
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);
88     uint16_t y, x;
89     for (y = 0; y < new_size_y; y++)
90     {
91         for (x = 0; y < win->winmap_size.y && x < win->winmap_size.x; x++)
92         {
93             chtype ch = old_winmap[(y * win->winmap_size.x) + x];
94             win->winmap[(y * new_size_x) + x] = ch;
95         }
96         for (; x < new_size_x; x++)
97         {
98             win->winmap[(y * new_size_x) + x] = ' ';
99         }
100     }
101     free(old_winmap);
102     win->winmap_size.y = new_size_y;
103     win->winmap_size.x = new_size_x;
104 }
105
106
107
108 static void set_ch_on_yx(struct Win * win, int y, int x, chtype ch)
109 {
110     win->winmap[(y * win->winmap_size.x) + x] = ch;
111 }
112
113
114
115 static void add_line(struct Win * win, char * line, attr_t attri,
116                      uint16_t * offset, uint8_t last)
117 {
118     exit_err(strlen(line) > UINT16_MAX, "Line in add_line() too big.");
119     if      (0 == win->linebreak)
120     {
121         add_line_long(win, line, attri);
122     }
123     else if (1 == win->linebreak)
124     {
125         add_line_wide(win, line, attri);
126     }
127     else if (2 == win->linebreak)
128     {
129         add_line_compact(win, line, attri, offset, last);
130     }
131 }
132
133
134
135 static void add_line_wide(struct Win * win, char * line, attr_t attri)
136 {
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);
141     uint16_t x;
142     for (x = 0; x < len_line; x++)
143     {
144         set_ch_on_yx(win, y_start, x, line[x] | attri);
145     }
146     if (attri)
147     {
148         for (; x < win->frame_size.x; x++)
149         {
150             set_ch_on_yx(win, y_start, x, ' ' | attri);
151         }
152     }
153 }
154
155
156
157 static void add_line_long(struct Win * win, char * line, attr_t attri)
158 {
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);
162     uint16_t x, y, z;
163     for (z = 0, y = y_start; z < len_line; y++)
164     {
165         for (x = 0; x < win->winmap_size.x && z < len_line; x++, z++)
166         {
167             set_ch_on_yx(win, y, x, line[z] | attri);
168         }
169         if (z < len_line)
170         {
171             try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
172         }
173     }
174 }
175
176
177
178 static void add_line_compact(struct Win * win, char * line, attr_t attri,
179                              uint16_t * offset, uint8_t last)
180 {
181     char * f_name = "add_line_compact()";
182     uint16_t y_start = win->winmap_size.y - (win->winmap_size.y > 0);
183     try_resize_winmap(win, y_start + 1, win->frame_size.x);
184     uint16_t len_line = strlen(line);
185     char * separator = last ? "" : " / ";
186     uint32_t len_line_new = len_line + strlen(separator);
187     char * line_new = try_malloc(len_line_new, f_name);
188     int test = sprintf(line_new, "%s%s", line, separator);
189     exit_trouble(test < 0, f_name, "sprintf()");
190     uint16_t x = 0;
191     uint16_t y;
192     uint32_t z;
193     for (z = 0, y = y_start; z < len_line_new; y++)
194     {
195         for (x = * offset; x < win->winmap_size.x && z < len_line_new; x++, z++)
196         {
197             set_ch_on_yx(win, y, x, line_new[z] | attri);
198             if (z + 1 == (uint32_t) len_line)
199             {
200                attri = 0;
201             }
202         }
203         * offset = 0;
204         if (z < len_line_new)
205         {
206             try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
207         }
208     }
209     free(line_new);
210     *offset = x;
211 }
212
213
214
215 static void add_text_with_linebreaks(struct Win * win, char * text)
216 {
217     char * limit;
218     uint16_t offset = 0;
219     char * text_copy = strdup(text);
220     char * start = text_copy;
221     while (NULL != (limit = strchr(start, '\n')))
222     {
223         (* limit) = '\0';
224         add_line(win, start, 0, &offset, 0);
225         start = limit + 1;
226     }
227     add_line(win, start, 0, &offset, 1);
228     free(text_copy);
229 }
230
231
232
233 static void draw_text_from_bottom(struct Win * win, char * text)
234 {
235     add_text_with_linebreaks(win, text);
236     if (win->winmap_size.y > win->frame_size.y)
237     {
238         win->center.y = win->winmap_size.y - 1;
239     }
240     else if (win->winmap_size.y < win->frame_size.y)
241     {
242         uint16_t new_y_start = win->frame_size.y - win->winmap_size.y;
243         memset(&win->winmap_size, 0, sizeof(struct yx_uint16));
244         free(win->winmap);
245         win->winmap = NULL;
246         do
247         {
248             add_line_wide(win, " ", 0);
249         }
250         while (new_y_start > win->winmap_size.y);
251         if (2 == win->linebreak) /* add_text_with_linebreaks() will start not */
252         {                        /* not after, but within the last line then. */
253             add_line_wide(win, " ", 0);
254         }
255         add_text_with_linebreaks(win, text);
256     }
257 }
258
259
260
261 static char * get_kb_line(struct KeyBinding * kb)
262 {
263     char * f_name = "get_kb_line()";
264     char * keyname = get_keyname_to_keycode(kb->keycode);
265     uint16_t size = strlen(keyname) + 3 + strlen(kb->command->dsc_long) + 1;
266     char * kb_line = try_malloc(size, f_name);
267     int test = sprintf(kb_line, "%s - %s", keyname, kb->command->dsc_long);
268     exit_trouble(test < 0, f_name, "sprintf()");
269     free(keyname);
270     return kb_line;
271 }
272
273
274
275 static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
276                                    uint16_t offset)
277 {
278     if (0 == kbdb->n_of_kbs)
279     {
280         add_line(win, "(none)", 0, &offset, 0);
281         return;
282     }
283     uint8_t kb_n;
284     for (kb_n = 0; kb_n < kbdb->n_of_kbs; kb_n++)
285     {
286         attr_t attri = 0;
287         if (kb_n == kbdb->select)
288         {
289             attri = A_REVERSE;
290             if (1 == kbdb->edit)
291             {
292                 attri = attri | A_BLINK;
293             }
294             win->center.y = win->winmap_size.y;
295         }
296         char * kb_line = get_kb_line(&kbdb->kbs[kb_n]);
297         add_line(win, kb_line, attri, &offset, (kbdb->n_of_kbs == kb_n + 1));
298         free(kb_line);
299     }
300 }
301
302
303
304 static void draw_titled_keybinding_list(char * title, struct Win * win,
305                                         uint16_t * offset, uint8_t last,
306                                         struct KeyBindingDB * kbdb)
307 {
308     uint8_t state = 0;
309     uint8_t kb_n = 0;
310     while (0 == state || kb_n < kbdb->n_of_kbs)
311     {
312         if (0 == state)
313         {
314             add_line(win, title, 0, offset, 0);
315             add_line(win, " ", 0, offset, 0);
316             state = 1 + (0 == kbdb->n_of_kbs);
317             continue;
318         }
319         char * kb_line = get_kb_line(&kbdb->kbs[kb_n]);
320         add_line(win, kb_line, 0, offset, (last * kbdb->n_of_kbs == kb_n + 1));
321         free(kb_line);
322         kb_n++;
323     }
324     if (2 == state)
325     {
326         add_line(win, "(none)", 0, offset, last);
327     }
328 }
329
330
331
332 extern void draw_win_log(struct Win * win)
333 {
334     if (!world.log)
335     {
336         return;
337     }
338     draw_text_from_bottom(win, world.log);
339 }
340
341
342
343 extern void draw_win_map(struct Win * win)
344 {
345     try_resize_winmap(win, world.map.length, world.map.length * 2);
346     uint16_t z = 0;
347     uint16_t x, y;
348     for (y = 0; y < world.map.length; y++)
349     {
350         for (x = 0; x < world.map.length; x++)
351         {
352             set_ch_on_yx(win, y, x * 2 + (y % 2), world.map.cells[z]);
353             z++;
354         }
355     }
356 }
357
358
359
360 extern void draw_win_info(struct Win * win)
361 {
362     char * f_name = "draw_win_info()";
363     char * dsc_turn      = "Turn: ";
364     char * dsc_hitpoints = "\nHitpoints: ";
365     uint16_t maxl = strlen(dsc_turn) + 5 + strlen(dsc_hitpoints) + 3;
366     char * text = try_malloc(maxl + 1, f_name);
367     int test = sprintf(text, "%s%d%s%d", dsc_turn, world.turn, dsc_hitpoints,
368                                          world.player_lifepoints);
369     exit_trouble(test < 0, f_name, "sprintf()");
370     add_text_with_linebreaks(win, text);
371     free(text);
372 }
373
374
375
376 extern void draw_win_inventory(struct Win * win)
377 {
378     char * limit;
379     char * text_copy = strdup(world.player_inventory);
380     char * start = text_copy;
381     uint8_t i = 0;
382     uint8_t found_selection = 0;
383     uint16_t offset = 0;
384     while (NULL != (limit = strchr(start, '\n')))
385     {
386         (* limit) = '\0';
387         attr_t attri = 0;
388         if (i == world.player_inventory_select)
389         {
390             found_selection = 1;
391             attri = A_REVERSE;
392             win->center.y = win->winmap_size.y;
393         }
394         add_line(win, start, attri, &offset, 0);
395         start = limit + 1;
396         i++;
397     }
398     win->center.y = !found_selection ? win->winmap_size.y : win->center.y;
399     add_line(win, start, !found_selection * A_REVERSE, &offset, 1);
400     free(text_copy);
401 }
402
403
404
405 extern void draw_win_available_keybindings(struct Win * win)
406 {
407     char * title = "Active window's keys:";
408     struct Win * win_active = get_win_by_id(world.winDB.active);
409     struct KeyBindingDB * kbdb = &win_active->kb;
410     if      (1 == win_active->view)
411     {
412         kbdb = &world.kb_wingeom;
413     }
414     else if (2 == win_active->view)
415     {
416         kbdb = &world.kb_winkeys;
417     }
418     uint16_t offset = 0;
419     draw_titled_keybinding_list(title, win, &offset, 0, kbdb);
420     add_line(win, " ", 0, &offset, 0);
421     title = "Global keys:";
422     draw_titled_keybinding_list(title, win, &offset, 1, &world.kb_global);
423 }
424
425
426
427
428 extern void draw_win_keybindings_global(struct Win * win)
429 {
430     win->center.y = world.kb_global.select;
431     draw_keybinding_config(win, &world.kb_global, 0);
432 }
433
434
435
436 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
437 {
438     win->center.y = world.kb_wingeom.select;
439     draw_keybinding_config(win, &world.kb_wingeom, 0);
440 }
441
442
443
444 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
445 {
446     win->center.y = world.kb_winkeys.select;
447     draw_keybinding_config(win, &world.kb_winkeys, 0);
448 }
449
450
451
452 extern void draw_winconf_keybindings(struct Win * win)
453 {
454     char * title = "Window's keys:";
455     uint16_t offset = 0;
456     add_line(win, title, 0, &offset, 0);
457     add_line(win, " ", 0, &offset, 0);
458     draw_keybinding_config(win, &win->kb, offset);
459 }
460
461
462
463 extern void draw_winconf_geometry(struct Win * win)
464 {
465     char * f_name = "draw_winconf_geometry()";
466     char * title = "Window's geometry:\n\n";
467     char * h_title = "Height to save: ";
468     char h_value[6 + 1];                       /* 6: int16_t value max strlen */
469     int test = sprintf(h_value, "%d", win->target_height);
470     exit_trouble(test < 0, f_name, "sprintf()");
471     char * h_plus = " (width in cells)\n\n";
472     char * h_minus = " (negative diff: cells to screen width)\n\n";
473     char * h_type = (1 == win->target_height_type) ? h_minus : h_plus;
474     char * w_title = "Width to save: ";
475     char w_value[6 + 1];
476     test = sprintf(w_value, "%d", win->target_width);
477     exit_trouble(test < 0, f_name, "sprintf()");
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 = try_malloc(text_size + 1, f_name);
489     test = 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     exit_trouble(test < 0, f_name, "sprintf()");
492     add_text_with_linebreaks(win, text);
493     free(text);
494 }