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