home · contact · privacy
Refactored similar array append activities into array_append().
[plomrogue] / src / client / windows.c
1 /* src/client/windows.c */
2
3 #include "windows.h"
4 #include <ncurses.h> /* chtype, getmaxx(), getmaxy(), erase(), werase(),
5                       * endwin(), delwin(), wnoutrefresh(), pnoutrefresh(),
6                       * doupdate(), refresh(), delwin(), newpad(), mvwaddch(),
7                       * mvwaddstr(), wresize()
8                       */
9 #include <stddef.h> /* NULL */
10 #include <stdlib.h> /* free(), atoi() */
11 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX */
12 #include <stdio.h> /* sprintf() */
13 #include <string.h> /* memcpy(), strlen(), strnlen(), strchr(), memset() */
14 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
15 #include "../common/readwrite.h" /* try_fputc(), try_write(), try_fgets(),
16                                   * try_fgetc()
17                                   */
18 #include "../common/try_malloc.h" /* try_malloc() */
19 #include "../common/yx_uint16.h" /* struct yx_uint16 */
20 #include "draw_wins.h" /* draw_winconf_geometry(), draw_winconf_keybindings(),
21                         * draw_win_inventory(), draw_win_info(), draw_win_log(),
22                         * draw_win_available_keybindings(), draw_win_map(),
23                         * draw_win_keybindings_winconf_keybindings(),
24                         * draw_win_keybindings_winconf_geometry(),
25                         * draw_win_keybindings_global()
26                         */
27 #include "keybindings.h" /* free_keybindings(), write_keybidings_to_file(),
28                           * read_keybindings_from_file()
29                           */
30 #include "misc.h" /* array_append() */
31 #include "world.h" /* global world */
32
33
34
35 /* Get position of id "c" in world.winDB.order or return Win before/after (or
36  * NULL if there is no window before/after).
37  */
38 static uint8_t get_pos_in_order(char c);
39 static struct Win * get_win_after(char c);
40 static struct Win * get_win_before(char c);
41
42 /* Calculate "id"'s window's size from v_screen size and .target_(height/width).
43  * A .target_width == 0 makes the window as wide as .t_screen. .target_height ==
44  * 0 sets the maximum allowed height: one cell smaller than that of .v_screen.
45  * Negative values make width/height so many cells smaller than what 0 would
46  * set. Values that would reduce the window height or width to less than 1 cell
47  * according to the aforementioned rules set the height/width as if they were 0.
48  */
49 static void init_win_size_from_winconf_and_v_screen_size(char id);
50
51 /* Get (Win->draw) function identified by "c"; NULL if c not mapped to one.
52  * match_func() is just a little helper to it.
53  */
54 static uint8_t match_func(char c, void (** f) (), char c_m, void (* f_m) ());
55 static void * get_drawfunc_by_char(char c);
56
57 /* Write "win"'s size back to .target_(height/width) as per .target_*_type. */
58 static void set_win_target_size(struct Win * win);
59
60 /* Iterate over chars of world.winDB.ids array / string. Restart after \0.*/
61 static char get_next_win_id();
62
63 /* Make .v_screen just wide enough to contain all visible windows. */
64 static void refit_v_screen();
65
66 /* Update geometry (sizes, positions) of window "w" and its successors in the
67  * window chain. Use place_win() for the positioning algorithm.
68  */
69 static void update_wins(struct Win * w);
70 static void place_win(struct Win * w);
71
72 /* Draw scroll hint (a line saying that there are "dist" more elements of "unit"
73  * further into the direction symbolized by "dir") into .v_screen, onto an
74  * appropriate edge of a window or .v_screen; the left/right edge if "dir" is
75  * "<"/">", or the top/bottom edge if it is "^"/"v". Use "start" as the start
76  * coordinate of the frame that is to contain the scroll hints. winscroll_hint()
77  * is a wrapper that uses the border of window "w" as this frame.
78  */
79 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
80                         char * unit, struct yx_uint16 start);
81 static void winscroll_hint(struct Win * w, char dir, uint16_t dist);
82
83 /* draw_win_borderlines() draws vertical/horizontal borders of window "w" sans
84  * corners into .v_screen. It draws the top border line as the windows' title
85  * bar (highlighted if the window is selected as active). It is called
86  * recursively by draw_wins_borderlines() on all windows from "w" on.
87  * draw_wins_bordercorners() draws the border corners of "w" and its successors.
88  */
89 static void draw_win_borderlines(struct Win * w);
90 static void draw_wins_borderlines(struct Win * w);
91 static void draw_wins_bordercorners(struct Win * w);
92
93 /* Draw contents of all windows in window chain from window "w" onwards. */
94 static void draw_wins(struct Win * w);
95
96 /* Append/suspend window "w" to/from chain of visible windows. Appended windows
97  * will become active. Suspended active windows will move the active window
98  * selection to their successor in the window chain or, failing that, their
99  * predecessor, or, failing that, to 0 (no window active).
100  */
101 static void append_win(struct Win * w);
102 static void suspend_win(struct Win * w);
103
104
105
106 static uint8_t get_pos_in_order(char c)
107 {
108     uint8_t i;
109     for (i = 0; c != world.winDB.order[i]; i++);
110     return i;
111 }
112
113
114
115 static struct Win * get_win_after(char c)
116 {
117     return get_win_by_id(world.winDB.order[get_pos_in_order(c) + 1]);
118 }
119
120
121
122 static struct Win * get_win_before(char c)
123 {
124     uint8_t i = get_pos_in_order(c);
125     if (i > 0)
126     {
127         return get_win_by_id(world.winDB.order[i - 1]);
128     }
129     return NULL;
130 }
131
132
133
134 static void init_win_size_from_winconf_and_v_screen_size(char id)
135 {
136     struct Win * w = get_win_by_id(id);
137     w->frame_size.y  = world.winDB.v_screen_size.y - 1;
138     if      (   0 < w->target_height
139              && w->target_height <= world.winDB.v_screen_size.y - 1)
140     {
141         w->frame_size.y = w->target_height;
142     }
143     else if (   0 > w->target_height
144              && world.winDB.v_screen_size.y + (w->target_height - 1) > 0)
145     {
146         w->frame_size.y = world.winDB.v_screen_size.y + (w->target_height - 1);
147     }
148     w->frame_size.x  = world.winDB.v_screen_size.x;
149     if      (0 < w->target_width)
150     {
151         w->frame_size.x = w->target_width;
152     }
153     else if (   0 > w->target_width
154              && world.winDB.v_screen_size.x + w->target_width > 0)
155     {
156         w->frame_size.x = world.winDB.v_screen_size.x + w->target_width;
157     }
158 }
159
160
161
162 static uint8_t match_func(char c, void (** f) (), char c_m, void (* f_m) ())
163 {
164     if (c == c_m)
165     {
166         * f = f_m;
167         return 1;
168     }
169     return 0;
170 }
171
172
173
174 static void * get_drawfunc_by_char(char c)
175 {
176     void (* f) (struct Win *) = NULL;
177     if (   match_func(c, &f, 'c', draw_win_inventory)
178         || match_func(c, &f, 'i', draw_win_info)
179         || match_func(c, &f, 'l', draw_win_log)
180         || match_func(c, &f, 'k', draw_win_available_keybindings)
181         || match_func(c, &f, 'm', draw_win_map)
182         || match_func(c, &f, '0', draw_win_keybindings_global)
183         || match_func(c, &f, '1', draw_win_keybindings_winconf_geometry)
184         || match_func(c, &f, '2', draw_win_keybindings_winconf_keybindings));
185     return f;
186 }
187
188
189
190 static void set_win_target_size(struct Win * wcp)
191 {
192     if      (0 == wcp->target_height_type)
193     {
194         wcp->target_height = wcp->frame_size.y;
195     }
196     else if (1 == wcp->target_height_type)
197     {
198         wcp->target_height = wcp->frame_size.y - world.winDB.v_screen_size.y +1;
199     }
200     if      (0 == wcp->target_width_type)
201     {
202         wcp->target_width = wcp->frame_size.x;
203     }
204     else if (1 == wcp->target_width_type)
205     {
206         wcp->target_width = wcp->frame_size.x - world.winDB.v_screen_size.x;
207     }
208 }
209
210
211
212 static char get_next_win_id()
213 {
214     static uint8_t i = 0;
215     char c = world.winDB.ids[i];
216     if (0 == c)
217     {
218         i = 0;
219         return c;
220     }
221     i++;
222     return c;
223 }
224
225
226
227 static void refit_v_screen()
228 {
229     /* Determine rightmost window column. */
230     uint32_t lastwcol = 0;
231     struct Win * wp = get_win_by_id(world.winDB.order[0]);
232     while (wp != 0)
233     {
234         if ((uint32_t) wp->start.x + (uint32_t) wp->frame_size.x > lastwcol + 1)
235         {
236             lastwcol = (uint32_t) wp->start.x + (uint32_t) wp->frame_size.x - 1;
237         }
238         wp = get_win_after(wp->id);
239     }
240
241     /* Only resize .v_screen if the rightmost window column has changed. */
242     char * err_s = "refit_v_screen() grows virtual screen beyond legal sizes.";
243     char * err_m = "refit_v_screen() triggers memory alloc error in wresize().";
244     if (getmaxx(world.winDB.v_screen) + 1 != lastwcol)
245     {
246         uint8_t t = (lastwcol + 2 > UINT16_MAX);
247         exit_err(t, err_s);
248         t = wresize(world.winDB.v_screen, getmaxy(world.winDB.v_screen),
249                     lastwcol + 2);
250         exit_err(t, err_m);
251     }
252 }
253
254
255
256 static void update_wins(struct Win * w)
257 {
258     place_win(w);
259     refit_v_screen();
260     struct Win * next = get_win_after(w->id);
261     if (next)
262     {
263         update_wins(next);
264     }
265 }
266
267
268
269 static void place_win(struct Win * w)
270 {
271     /* If w is first window, it goes into the top left corner. */
272     w->start.x = 0;
273     w->start.y = 1;                             /* Leave space for title bar. */
274     struct Win * w_prev = get_win_before(w->id);
275     if (w_prev)
276     {
277
278         /* If not, fit w's top left to top right of last top predecessor. */
279         struct Win * w_top = w_prev;
280         for (;
281              w_top->start.y != 1;
282              w_top = get_win_before(w_top->id));
283         w->start.x = w_top->start.x + w_top->frame_size.x + 1;
284
285         /* Fit w's top left to bottom left of its ->prev if enough space. */
286         uint16_t w_prev_maxy = w_prev->start.y + w_prev->frame_size.y;
287         if (   w->frame_size.x <= w_prev->frame_size.x
288             && w->frame_size.y <  world.winDB.v_screen_size.y - w_prev_maxy)
289         {
290             w->start.x = w_prev->start.x;
291             w->start.y = w_prev_maxy + 1;
292             return;
293         }
294
295         /* Failing that, try to fit w' top left to the top right of the last
296          * predecessor w_test 1) not followed by windows with a left corner
297          * further rightwards than its own 2) with enough space rightwards for w
298          * until the bottom right of w_thr directly throning over it 3) and with
299          * this same space extending far enough to the bottom for fitting in w.
300          */
301         struct Win * w_test = w_prev;
302         struct Win * w_thr;
303         while (w_test != w_top)
304         {
305             for (w_thr = get_win_before(w_test->id);
306                  w_test->start.y <= w_thr->start.y;
307                  w_thr = get_win_before(w_thr->id));
308             uint16_t w_thr_bottom = w_thr->start.y + w_thr->frame_size.y;
309             uint16_t free_width =   (w_thr->start.x + w_thr->frame_size.x)
310                                   - (w_test->start.x + w_test->frame_size.x);
311             if (   w->frame_size.y < world.winDB.v_screen_size.y - w_thr_bottom
312                 && w->frame_size.x < free_width)
313             {
314                 w->start.x = w_test->start.x + w_test->frame_size.x + 1;
315                 w->start.y = w_thr_bottom + 1;
316                 break;
317             }
318             w_test = w_thr;
319         }
320     }
321 }
322
323
324
325 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
326                         char * unit, struct yx_uint16 start)
327 {
328     /* Decide on alignment (vertical/horizontal?), thereby hint text space. */
329     char * more = "more";
330     uint16_t dsc_space = fsize.x;
331     if ('<' == dir || '>' == dir)
332     {
333         dsc_space = fsize.y;
334     }                                  /* vv-- 10 = max strlen for uint16_t */
335     char scrolldsc[1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1];
336     sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
337
338     /* Decide on offset of the description text inside the scroll hint line. */
339     uint16_t dsc_offset = 1;
340     if (dsc_space > strlen(scrolldsc) + 1)
341     {
342         dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
343     }
344
345     /* Draw scroll hint line as dir symbols bracketing description text. */
346     uint16_t draw_offset = 0;
347     if      ('>' == dir)
348     {
349         draw_offset = fsize.x - 1;
350     }
351     else if ('v' == dir)
352     {
353         draw_offset = fsize.y - 1;
354     }
355     uint16_t q = 0;
356     for (; q < dsc_space; q++)
357     {
358         chtype c = dir | A_REVERSE;
359         if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
360         {
361             c = scrolldsc[q - dsc_offset] | A_REVERSE;
362         }
363         if ('<' == dir || '>' == dir)
364         {
365             mvwaddch(world.winDB.v_screen, start.y+q, start.x+draw_offset, c);
366             continue;
367         }
368         mvwaddch(world.winDB.v_screen, start.y + draw_offset, start.x + q, c);
369     }
370 }
371
372
373
374 static void winscroll_hint(struct Win * w, char dir, uint16_t dist)
375 {
376     char * unit = "lines";
377     if ('<' == dir || '>' == dir)
378     {
379         unit = "columns";
380     }
381     struct yx_uint16 start = w->start;
382     scroll_hint(w->frame_size, dir, dist, unit, start);
383 }
384
385
386
387 static void draw_win_borderlines(struct Win * w)
388 {
389     /* Draw vertical and horizontal border lines. */
390     uint16_t y, x;
391     for (y = w->start.y; y <= w->start.y + w->frame_size.y; y++)
392     {
393         mvwaddch(world.winDB.v_screen, y, w->start.x - 1,              '|');
394         mvwaddch(world.winDB.v_screen, y, w->start.x + w->frame_size.x, '|');
395     }
396     for (x = w->start.x; x <= w->start.x + w->frame_size.x; x++)
397     {
398         mvwaddch(world.winDB.v_screen, w->start.y - 1,              x, '-');
399         mvwaddch(world.winDB.v_screen, w->start.y + w->frame_size.y, x, '-');
400     }
401
402     /* Draw as much as possible of the title into center of top border line. */
403     uint8_t min_title_length_visible = 3;/* min. 1 char +2 padding/decoration*/
404     if (w->frame_size.x >= min_title_length_visible)
405     {
406         uint16_t offset = 0;
407         if (w->frame_size.x > strlen(w->title) + 2)
408         {
409             offset = (w->frame_size.x - (strlen(w->title) + 2)) / 2;
410         }                                    /* +2 is for padding/decoration */
411         uint16_t length_visible = strnlen(w->title, w->frame_size.x - 2);
412         char title[length_visible + 3];
413         char decoration = ' ';
414         if (w->id == world.winDB.active)
415         {
416             decoration = '$';
417         }
418         memcpy(title + 1, w->title, length_visible);
419         title[0] = title[length_visible + 1] = decoration;
420         title[length_visible + 2] = '\0';
421         mvwaddstr(world.winDB.v_screen, w->start.y-1, w->start.x+offset, title);
422     }
423 }
424
425
426
427 static void draw_wins_borderlines(struct Win * w)
428 {
429     draw_win_borderlines(w);
430     struct Win * next = get_win_after(w->id);
431     if (next)
432     {
433         draw_wins_borderlines(next);
434     }
435 }
436
437
438
439 static void draw_wins_bordercorners(struct Win * w)
440 {
441     mvwaddch(world.winDB.v_screen,
442              w->start.y - 1, w->start.x - 1,              '+');
443     mvwaddch(world.winDB.v_screen,
444              w->start.y - 1, w->start.x + w->frame_size.x, '+');
445     mvwaddch(world.winDB.v_screen,
446              w->start.y + w->frame_size.y, w->start.x - 1, '+');
447     mvwaddch(world.winDB.v_screen, w->start.y + w->frame_size.y,
448              w->start.x + w->frame_size.x,                 '+');
449     struct Win * next = get_win_after(w->id);
450     if (next)
451     {
452         draw_wins_bordercorners(next);
453     }
454 }
455
456
457
458 static void draw_wins(struct Win * w)
459 {
460     void (* drawfunc) (struct Win *) = get_drawfunc_by_char(w->id);
461     if      (1 == w->view)
462     {
463         drawfunc = draw_winconf_geometry;
464     }
465     else if (2 == w->view)
466     {
467         drawfunc = draw_winconf_keybindings;
468     }
469     drawfunc(w);
470     uint16_t size_y = w->winmap_size.y;
471     uint16_t size_x = w->winmap_size.x;
472     uint16_t offset_y = center_offset(w->center.y, size_y, w->frame_size.y);
473     uint16_t offset_x = center_offset(w->center.x, size_x, w->frame_size.x);
474     uint16_t y, x;
475     for (y = offset_y; y < w->frame_size.y + offset_y && y < size_y; y++)
476     {
477         for (x = offset_x; x < w->frame_size.x + offset_x && x < size_x; x++)
478         {
479             chtype ch = w->winmap[(y * w->winmap_size.x) + x];
480             mvwaddch(world.winDB.v_screen, w->start.y + (y - offset_y),
481                                       w->start.x + (x - offset_x), ch);
482         }
483     }
484     free(w->winmap); /* NULL so draw_wins.c's try_resize_winmap() may always  */
485     w->winmap = NULL;/* free() it before (re-)allocation, even the first time.*/
486     memset(&w->winmap_size, 0, sizeof(struct yx_uint16));
487     if (offset_y > 0)
488     {
489         winscroll_hint(w, '^', offset_y + 1);
490     }
491     if (size_y > offset_y + w->frame_size.y)
492     {
493         winscroll_hint(w, 'v', size_y - ((offset_y + w->frame_size.y) - 1));
494     }
495     if (offset_x > 0)
496     {
497         winscroll_hint(w, '<', offset_x + 1);
498     }
499     if (size_x > offset_x + w->frame_size.x)
500     {
501         winscroll_hint(w, '>', size_x - ((offset_x + w->frame_size.x) - 1));
502     }
503     struct Win * next = get_win_after(w->id);
504     if (next)
505     {
506         return draw_wins(next);
507     }
508 }
509
510
511
512 static void append_win(struct Win * w)
513 {
514     char * f_name = "append_win()";
515     uint8_t old_size = strlen(world.winDB.order) + 1;
516     char * new_order = try_malloc(old_size + 1, f_name);
517     memcpy(new_order, world.winDB.order, old_size - 1);
518     new_order[old_size - 1] = w->id;
519     new_order[old_size] = '\0';
520     free(world.winDB.order);
521     world.winDB.order = new_order;
522     world.winDB.active = w->id;
523     update_wins(w);
524 }
525
526
527
528 static void suspend_win(struct Win * w)
529 {
530     char * f_name = "suspend_win()";
531     uint8_t new_size = strlen(world.winDB.order);
532     char * new_order = try_malloc(new_size, f_name);
533     uint8_t i = get_pos_in_order(w->id);
534     char next_char = world.winDB.order[i + 1];
535     world.winDB.order[i] = '\0';
536     char * second_part = &world.winDB.order[i + 1];
537     sprintf(new_order, "%s%s", world.winDB.order, second_part);
538     free(world.winDB.order);
539     world.winDB.order = new_order;
540     world.winDB.active = world.winDB.order[i];
541     if (!world.winDB.order[i] && 0 < i)
542     {
543         world.winDB.active = world.winDB.order[i - 1];
544     }
545     if (world.winDB.order[i])
546     {
547         update_wins(get_win_by_id(next_char)); /* Already calls               */
548         return;                                /* refit_v_screen(), so leave. */
549     }
550     refit_v_screen();
551 }
552
553
554
555 extern uint16_t center_offset(uint16_t position, uint16_t mapsize,
556                               uint16_t frame_size)
557 {
558     uint16_t offset = 0;
559     if (mapsize > frame_size)
560     {
561         if (position > frame_size / 2)
562         {
563             if (position < mapsize - (frame_size / 2))
564             {
565                 offset = position - (frame_size / 2);
566             }
567             else
568             {
569                 offset = mapsize - frame_size;
570             }
571         }
572     }
573     return offset;
574 }
575
576
577
578 extern struct Win * get_win_by_id(char id)
579 {
580     uint8_t i = 0;
581     while ('\0' != world.winDB.ids[i])
582     {
583         if (id == world.winDB.ids[i])
584         {
585             return &world.winDB.wins[i];
586         }
587         i++;
588     }
589     return NULL;
590 }
591
592
593
594 extern uint8_t read_winconf_from_file(char * line, uint32_t linemax,
595                                       FILE * file)
596 {
597     char * f_name = "read_winconf_from_file()";
598     int test = try_fgetc(file, f_name);
599     if (EOF == test || '\n' == test)
600     {
601         return 0;
602     }
603     struct Win win;
604     memset(&win, 0, sizeof(struct Win));
605     win.id = (char) test;
606     try_fgetc(file, f_name);
607     try_fgets(line, linemax + 1, file, f_name);
608     win.title = try_malloc(strlen(line), f_name);
609     memcpy(win.title, line, strlen(line) - 1);  /* Eliminate newline char */
610     win.title[strlen(line) - 1] = '\0';         /* char at end of string. */
611     try_fgets(line, linemax + 1, file, f_name);
612     win.target_height = atoi(line);
613     win.target_height_type = (0 >= win.target_height);
614     try_fgets(line, linemax + 1, file, f_name);
615     win.target_width = atoi(line);
616     win.target_width_type = (0 >= win.target_width);
617     read_keybindings_from_file(line, linemax, file, &win.kb);
618     if (world.winDB.ids)
619     {
620         uint8_t old_ids_size = strlen(world.winDB.ids);
621         char * new_ids = try_malloc(old_ids_size + 1 + 1, f_name);
622         sprintf(new_ids, "%s%c", world.winDB.ids, win.id);
623         free(world.winDB.ids);
624         world.winDB.ids = new_ids;
625         array_append(old_ids_size, sizeof(struct Win), (void *) &win,
626                      (void **) &world.winDB.wins);
627         return 1;
628     }
629     world.winDB.ids = try_malloc(2, f_name);
630     sprintf(world.winDB.ids, "%c", win.id);
631     world.winDB.wins = try_malloc(sizeof(struct Win), f_name);
632     world.winDB.wins[0] = win;
633     return 1;
634 }
635
636
637
638 extern void write_winconf_of_id_to_file(FILE * file, char c, char * delim)
639 {
640     char * f_name = "write_winconf_of_id_to_file()";
641     struct Win * wc = get_win_by_id(c);
642     uint8_t size = strlen(wc->title) + 2;
643     if (size < 7)  /* Ensure that at least 5 + 2 char fit into line so that   */
644     {              /* the digit representation of any uint16_t may be stored. */
645         size = 7;
646     }
647     char line[size];
648     sprintf(line, "%c\n", wc->id);
649     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
650     sprintf(line, "%s\n", wc->title);
651     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
652     sprintf(line, "%d\n", wc->target_height);
653     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
654     sprintf(line, "%d\n", wc->target_width);
655     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
656     write_keybindings_to_file(file, &wc->kb, delim);
657 }
658
659
660
661 extern void read_order_wins_visible_active(char * line, uint32_t linemax,
662                                            FILE * file)
663 {
664     char * f_name = "read_order_wins_visible_active()";
665     char win_order[linemax + 1];
666     try_fgets(win_order, linemax + 1, file, f_name);
667     win_order[strlen(win_order) - 1] = '\0';
668     world.winDB.order = try_malloc(strlen(win_order) + 1, f_name);
669     sprintf(world.winDB.order, "%s", win_order);
670     int char_or_eof = try_fgetc(file, f_name);
671     char * err_eof = "fgetc() unexpectedly hitting EOF";
672     exit_trouble(EOF == char_or_eof, f_name, err_eof);
673     world.winDB.active = (uint8_t) char_or_eof;
674     exit_trouble(EOF == try_fgetc(file, f_name), f_name, err_eof);
675     try_fgets(line, linemax + 1, file, f_name);
676 }
677
678
679
680 extern void write_order_wins_visible_active(FILE * file, char * delim)
681 {
682     char * f_name = "write_order_wins_visible_active()";
683     try_fwrite(world.winDB.order, strlen(world.winDB.order), 1, file, f_name);
684     try_fputc('\n', file, f_name);
685     try_fputc(world.winDB.active, file, f_name);
686     try_fputc('\n', file, f_name);
687     try_fwrite(delim, strlen(delim), 1, file, f_name);
688 }
689
690
691
692 extern void make_v_screen_and_init_win_sizes()
693 {
694     char * f_name = "make_v_screen_and_init_win_sizes()";
695     char * err_s = "creating an illegaly large virtual screen";
696     char * err_m = "triggering a memory allocation error via newpad()";
697     uint32_t maxy_test = getmaxy(world.winDB.t_screen);
698     uint32_t maxx_test = getmaxx(world.winDB.t_screen);
699     exit_trouble(maxy_test>UINT16_MAX || maxx_test>UINT16_MAX, f_name, err_s);
700     world.winDB.v_screen_size.y = maxy_test;
701     world.winDB.v_screen_size.x = maxx_test;
702     world.winDB.v_screen = newpad(world.winDB.v_screen_size.y, 1);
703     exit_trouble(NULL == world.winDB.v_screen, f_name, err_m);
704     char id;
705     while (0 != (id = get_next_win_id()))
706     {
707         init_win_size_from_winconf_and_v_screen_size(id);
708     }
709 }
710
711
712
713 extern void free_winDB()
714 {
715     char id;
716     while (0 != (id = get_next_win_id()))
717     {
718         struct Win * wc = get_win_by_id(id);
719         free(wc->title);
720         free_keybindings(wc->kb.kbs);
721     }
722     free(world.winDB.ids);  /* NULL this too since add_win_to_winDB() checks  */
723     world.winDB.ids = NULL; /* for it to detect its first post-DB-purge round.*/
724     free(world.winDB.wins);
725     free(world.winDB.order);
726 }
727
728
729
730 extern void winch_called(int signal)
731 {
732     world.winch = 1;
733 }
734
735
736
737 extern void reset_windows_on_winch()
738 {
739     endwin();  /* "[S]tandard way" to recalibrate ncurses post SIGWINCH, says */
740     refresh(); /* <http://invisible-island.net/ncurses/ncurses-intro.html>.   */
741     char tmp_order[strlen(world.winDB.order) + 1];
742     sprintf(tmp_order, "%s", world.winDB.order);
743     uint8_t i;
744     char tmp_active = world.winDB.active;
745     for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
746     delwin(world.winDB.v_screen);
747     make_v_screen_and_init_win_sizes();
748     for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
749     world.winDB.active = tmp_active;
750 }
751
752
753
754 extern void draw_all_wins()
755 {
756     /* Empty everything before filling it a-new. */
757     erase();
758     wnoutrefresh(world.winDB.t_screen);
759     werase(world.winDB.v_screen);
760     if (world.winDB.active)
761     {
762
763         /* Draw borders, wins. Order matters: corners should overwrite lines. */
764         draw_wins_borderlines(get_win_by_id(world.winDB.order[0]));
765         draw_wins_bordercorners(get_win_by_id(world.winDB.order[0]));
766         draw_wins(get_win_by_id(world.winDB.order[0]));
767
768         /* Draw .v_screen scroll hints. */
769         struct yx_uint16 start;
770         start.y = 0;
771         start.x = world.winDB.v_screen_offset;
772         char * cols_string = "columns";
773         if (world.winDB.v_screen_offset > 0)
774         {
775             scroll_hint(world.winDB.v_screen_size, '<',
776                         world.winDB.v_screen_offset + 1, cols_string, start);
777         }
778         uint16_t size_x = getmaxx(world.winDB.v_screen);
779         uint16_t right_edge =   world.winDB.v_screen_offset
780                               + world.winDB.v_screen_size.x;
781         if (right_edge < size_x - 1)
782         {
783             scroll_hint(world.winDB.v_screen_size, '>',
784                         size_x - right_edge, cols_string, start);
785         }
786
787         /* Put .v_screen segment to be shown on .t_screen to .t_screen buffer.*/
788         pnoutrefresh(world.winDB.v_screen, 0, world.winDB.v_screen_offset, 0, 0,
789                      world.winDB.v_screen_size.y,
790                      world.winDB.v_screen_size.x - 1);
791     }
792
793     /* Only at the end write accumulated changes to .t_screen. */
794     doupdate();
795 }
796
797
798
799 extern void toggle_window(char id)
800 {
801     struct Win * win = get_win_by_id(id);
802     if (NULL == strchr(world.winDB.order, id))
803     {
804         append_win(win);
805         return;
806     }
807     suspend_win(win);
808 }
809
810
811
812 extern void toggle_winconfig()
813 {
814     if (!world.winDB.active)
815     {
816         return;
817     }
818     struct Win * w = get_win_by_id(world.winDB.active);
819     if      (0 == w->view)
820     {
821         w->view          = 1;
822         w->target_center = w->center;
823         memset(&w->center, 0, sizeof(struct yx_uint16));
824         return;
825     }
826     else if (1 == w->view)
827     {
828         w->view          = 2;
829         w->center.x      = 0;
830         return;
831     }
832     w->view          = 0;
833     w->center        = w->target_center;
834 }
835
836
837
838 extern void toggle_win_size_type(char axis)
839 {
840     struct Win * w = get_win_by_id(world.winDB.active);
841     if ('y' == axis)
842     {
843         w->target_height_type = (0 == w->target_height_type);
844         set_win_target_size(w);
845         return;
846     }
847     w->target_width_type = (   0 == w->target_width_type
848                             && w->frame_size.x <= world.winDB.v_screen_size.x);
849     set_win_target_size(w);
850 }
851
852
853
854 extern void resize_active_win(char change)
855 {
856     if (world.winDB.active)
857     {
858         struct Win * w = get_win_by_id(world.winDB.active);
859         if      (change == '-' && w->frame_size.y > 1)
860         {
861             w->frame_size.y--;
862         }
863         else if (change == '_' && w->frame_size.y > 1)
864         {
865             w->frame_size.x--;
866         }
867         else if (   change == '+'
868                  && w->frame_size.y < world.winDB.v_screen_size.y - 1)
869         {
870             w->frame_size.y++;
871         }
872         else if (change == '*' && w->frame_size.y < UINT16_MAX)
873         {
874             w->frame_size.x++;
875         }
876         if (   1 == w->target_width_type
877             && w->frame_size.x > world.winDB.v_screen_size.x)
878         {
879             w->target_width_type = 0;
880         }
881         set_win_target_size(w);
882         update_wins(w);
883     }
884 }
885
886
887
888 extern void shift_active_win(char dir)
889 {
890     uint8_t len_order = strlen(world.winDB.order);
891     if (1 < len_order)
892     {
893         char tmp[len_order + 1];
894         tmp[len_order] = '\0';
895         uint8_t pos = get_pos_in_order(world.winDB.active);
896         if ('f' == dir)
897         {
898             if (pos == len_order - 1)
899             {
900                 memcpy(tmp + 1, world.winDB.order, len_order - 1);
901                 tmp[0] = world.winDB.active;
902                 memcpy(world.winDB.order, tmp, len_order + 1);
903             }
904             else
905             {
906                 world.winDB.order[pos] = world.winDB.order[pos + 1];
907                 world.winDB.order[pos + 1] = world.winDB.active;
908             }
909         }
910         else
911         {
912             if (pos == 0)
913             {
914                 memcpy(tmp, world.winDB.order + 1, len_order - 1);
915                 tmp[len_order - 1] = world.winDB.active;
916                 memcpy(world.winDB.order, tmp, len_order + 1);
917             }
918             else
919             {
920                 world.winDB.order[pos] = world.winDB.order[pos - 1];
921                 world.winDB.order[pos - 1] = world.winDB.active;
922             }
923         }
924         update_wins(get_win_by_id(world.winDB.order[0]));
925     }
926 }
927
928
929
930 extern void scroll_v_screen(char dir)
931 {
932     if      (   '+' == dir
933              &&   world.winDB.v_screen_offset + world.winDB.v_screen_size.x + 1
934                 < getmaxx(world.winDB.v_screen))
935     {
936         world.winDB.v_screen_offset++;
937     }
938     else if (   '-' == dir
939              && world.winDB.v_screen_offset > 0)
940     {
941         world.winDB.v_screen_offset--;
942     }
943 }
944
945
946
947 extern void cycle_active_win(char dir)
948 {
949     uint8_t len_order = strlen(world.winDB.order);
950     if (1 < len_order)
951     {
952         uint8_t pos = get_pos_in_order(world.winDB.active);
953         if ('f' == dir)
954         {
955             world.winDB.active = world.winDB.order[pos + 1];
956             if ('\0' == world.winDB.active)
957             {
958                 world.winDB.active = world.winDB.order[0];
959             }
960             return;
961         }
962         if (pos > 0)
963         {
964             world.winDB.active = world.winDB.order[pos - 1];
965             return;
966         }
967         world.winDB.active = world.winDB.order[len_order - 1];
968     }
969 }