home · contact · privacy
In the client, different modes of treating linebreaks are avaiable for
[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" /* 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     {
185         ;
186     }
187     return f;
188 }
189
190
191
192 static void set_win_target_size(struct Win * wcp)
193 {
194     if      (0 == wcp->target_height_type)
195     {
196         wcp->target_height = wcp->frame_size.y;
197     }
198     else if (1 == wcp->target_height_type)
199     {
200         wcp->target_height = wcp->frame_size.y - world.winDB.v_screen_size.y +1;
201     }
202     if      (0 == wcp->target_width_type)
203     {
204         wcp->target_width = wcp->frame_size.x;
205     }
206     else if (1 == wcp->target_width_type)
207     {
208         wcp->target_width = wcp->frame_size.x - world.winDB.v_screen_size.x;
209     }
210 }
211
212
213
214 static char get_next_win_id()
215 {
216     static uint8_t i = 0;
217     char c = world.winDB.ids[i];
218     if (0 == c)
219     {
220         i = 0;
221         return c;
222     }
223     i++;
224     return c;
225 }
226
227
228
229 static void refit_v_screen()
230 {
231     /* Determine rightmost window column. */
232     uint32_t lastwcol = 0;
233     struct Win * wp = get_win_by_id(world.winDB.order[0]);
234     while (wp != 0)
235     {
236         if ((uint32_t) wp->start.x + (uint32_t) wp->frame_size.x > lastwcol + 1)
237         {
238             lastwcol = (uint32_t) wp->start.x + (uint32_t) wp->frame_size.x - 1;
239         }
240         wp = get_win_after(wp->id);
241     }
242
243     /* Only resize .v_screen if the rightmost window column has changed. */
244     char * err_s = "refit_v_screen() grows virtual screen beyond legal sizes.";
245     char * err_m = "refit_v_screen() triggers memory alloc error in wresize().";
246     if ((uint32_t) getmaxx(world.winDB.v_screen) + 1 != lastwcol)
247     {
248         uint8_t t = (lastwcol + 2 > UINT16_MAX);
249         exit_err(t, err_s);
250         t = wresize(world.winDB.v_screen, getmaxy(world.winDB.v_screen),
251                     lastwcol + 2);
252         exit_err(t, err_m);
253     }
254 }
255
256
257
258 static void update_wins(struct Win * w)
259 {
260     place_win(w);
261     refit_v_screen();
262     struct Win * next = get_win_after(w->id);
263     if (next)
264     {
265         update_wins(next);
266     }
267 }
268
269
270
271 static void place_win(struct Win * w)
272 {
273     /* If w is first window, it goes into the top left corner. */
274     w->start.x = 0;
275     w->start.y = 1;                             /* Leave space for title bar. */
276     struct Win * w_prev = get_win_before(w->id);
277     if (w_prev)
278     {
279
280         /* If not, fit w's top left to top right of last top predecessor. */
281         struct Win * w_top = w_prev;
282         for (;
283              w_top->start.y != 1;
284              w_top = get_win_before(w_top->id));
285         w->start.x = w_top->start.x + w_top->frame_size.x + 1;
286
287         /* Fit w's top left to bottom left of its ->prev if enough space. */
288         uint16_t w_prev_maxy = w_prev->start.y + w_prev->frame_size.y;
289         if (   w->frame_size.x <= w_prev->frame_size.x
290             && w->frame_size.y <  world.winDB.v_screen_size.y - w_prev_maxy)
291         {
292             w->start.x = w_prev->start.x;
293             w->start.y = w_prev_maxy + 1;
294             return;
295         }
296
297         /* Failing that, try to fit w' top left to the top right of the last
298          * predecessor w_test 1) not followed by windows with a left corner
299          * further rightwards than its own 2) with enough space rightwards for w
300          * until the bottom right of w_thr directly throning over it 3) and with
301          * this same space extending far enough to the bottom for fitting in w.
302          */
303         struct Win * w_test = w_prev;
304         struct Win * w_thr;
305         while (w_test != w_top)
306         {
307             for (w_thr = get_win_before(w_test->id);
308                  w_test->start.y <= w_thr->start.y;
309                  w_thr = get_win_before(w_thr->id));
310             uint16_t w_thr_bottom = w_thr->start.y + w_thr->frame_size.y;
311             uint16_t free_width =   (w_thr->start.x + w_thr->frame_size.x)
312                                   - (w_test->start.x + w_test->frame_size.x);
313             if (   w->frame_size.y < world.winDB.v_screen_size.y - w_thr_bottom
314                 && w->frame_size.x < free_width)
315             {
316                 w->start.x = w_test->start.x + w_test->frame_size.x + 1;
317                 w->start.y = w_thr_bottom + 1;
318                 break;
319             }
320             w_test = w_thr;
321         }
322     }
323 }
324
325
326
327 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
328                         char * unit, struct yx_uint16 start)
329 {
330     /* Decide on alignment (vertical/horizontal?), thereby hint text space. */
331     char * more = "more";
332     uint16_t dsc_space = fsize.x;
333     if ('<' == dir || '>' == dir)
334     {
335         dsc_space = fsize.y;
336     }                                  /* vv-- 10 = max strlen for uint16_t */
337     char scrolldsc[1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1];
338     sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
339
340     /* Decide on offset of the description text inside the scroll hint line. */
341     uint16_t dsc_offset = 1;
342     if (dsc_space > strlen(scrolldsc) + 1)
343     {
344         dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
345     }
346
347     /* Draw scroll hint line as dir symbols bracketing description text. */
348     uint16_t draw_offset = 0;
349     if      ('>' == dir)
350     {
351         draw_offset = fsize.x - 1;
352     }
353     else if ('v' == dir)
354     {
355         draw_offset = fsize.y - 1;
356     }
357     uint16_t q = 0;
358     for (; q < dsc_space; q++)
359     {
360         chtype c = dir | A_REVERSE;
361         if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
362         {
363             c = scrolldsc[q - dsc_offset] | A_REVERSE;
364         }
365         if ('<' == dir || '>' == dir)
366         {
367             mvwaddch(world.winDB.v_screen, start.y+q, start.x+draw_offset, c);
368             continue;
369         }
370         mvwaddch(world.winDB.v_screen, start.y + draw_offset, start.x + q, c);
371     }
372 }
373
374
375
376 static void winscroll_hint(struct Win * w, char dir, uint16_t dist)
377 {
378     char * unit = "lines";
379     if ('<' == dir || '>' == dir)
380     {
381         unit = "columns";
382     }
383     struct yx_uint16 start = w->start;
384     scroll_hint(w->frame_size, dir, dist, unit, start);
385 }
386
387
388
389 static void draw_win_borderlines(struct Win * w)
390 {
391     /* Draw vertical and horizontal border lines. */
392     uint16_t y, x;
393     for (y = w->start.y; y <= w->start.y + w->frame_size.y; y++)
394     {
395         mvwaddch(world.winDB.v_screen, y, w->start.x - 1,              '|');
396         mvwaddch(world.winDB.v_screen, y, w->start.x + w->frame_size.x, '|');
397     }
398     for (x = w->start.x; x <= w->start.x + w->frame_size.x; x++)
399     {
400         mvwaddch(world.winDB.v_screen, w->start.y - 1,              x, '-');
401         mvwaddch(world.winDB.v_screen, w->start.y + w->frame_size.y, x, '-');
402     }
403
404     /* Draw as much as possible of the title into center of top border line. */
405     uint8_t min_title_length_visible = 3;/* min. 1 char +2 padding/decoration*/
406     if (w->frame_size.x >= min_title_length_visible)
407     {
408         uint16_t offset = 0;
409         if (w->frame_size.x > strlen(w->title) + 2)
410         {
411             offset = (w->frame_size.x - (strlen(w->title) + 2)) / 2;
412         }                                    /* +2 is for padding/decoration */
413         uint16_t length_visible = strnlen(w->title, w->frame_size.x - 2);
414         char title[length_visible + 3];
415         char decoration = ' ';
416         if (w->id == world.winDB.active)
417         {
418             decoration = '$';
419         }
420         memcpy(title + 1, w->title, length_visible);
421         title[0] = title[length_visible + 1] = decoration;
422         title[length_visible + 2] = '\0';
423         mvwaddstr(world.winDB.v_screen, w->start.y-1, w->start.x+offset, title);
424     }
425 }
426
427
428
429 static void draw_wins_borderlines(struct Win * w)
430 {
431     draw_win_borderlines(w);
432     struct Win * next = get_win_after(w->id);
433     if (next)
434     {
435         draw_wins_borderlines(next);
436     }
437 }
438
439
440
441 static void draw_wins_bordercorners(struct Win * w)
442 {
443     mvwaddch(world.winDB.v_screen,
444              w->start.y - 1, w->start.x - 1,              '+');
445     mvwaddch(world.winDB.v_screen,
446              w->start.y - 1, w->start.x + w->frame_size.x, '+');
447     mvwaddch(world.winDB.v_screen,
448              w->start.y + w->frame_size.y, w->start.x - 1, '+');
449     mvwaddch(world.winDB.v_screen, w->start.y + w->frame_size.y,
450              w->start.x + w->frame_size.x,                 '+');
451     struct Win * next = get_win_after(w->id);
452     if (next)
453     {
454         draw_wins_bordercorners(next);
455     }
456 }
457
458
459
460 static void draw_wins(struct Win * w)
461 {
462     void (* drawfunc) (struct Win *) = get_drawfunc_by_char(w->id);
463     if      (1 == w->view)
464     {
465         drawfunc = draw_winconf_geometry;
466     }
467     else if (2 == w->view)
468     {
469         drawfunc = draw_winconf_keybindings;
470     }
471     drawfunc(w);
472     uint16_t size_y = w->winmap_size.y;
473     uint16_t size_x = w->winmap_size.x;
474     uint16_t offset_y = center_offset(w->center.y, size_y, w->frame_size.y);
475     uint16_t offset_x = center_offset(w->center.x, size_x, w->frame_size.x);
476     uint16_t y, x;
477     for (y = offset_y; y < w->frame_size.y + offset_y && y < size_y; y++)
478     {
479         for (x = offset_x; x < w->frame_size.x + offset_x && x < size_x; x++)
480         {
481             chtype ch = w->winmap[(y * w->winmap_size.x) + x];
482             mvwaddch(world.winDB.v_screen, w->start.y + (y - offset_y),
483                                       w->start.x + (x - offset_x), ch);
484         }
485     }
486     free(w->winmap); /* NULL so draw_wins.c's try_resize_winmap() may always  */
487     w->winmap = NULL;/* free() it before (re-)allocation, even the first time.*/
488     memset(&w->winmap_size, 0, sizeof(struct yx_uint16));
489     if (offset_y > 0)
490     {
491         winscroll_hint(w, '^', offset_y + 1);
492     }
493     if (size_y > offset_y + w->frame_size.y)
494     {
495         winscroll_hint(w, 'v', size_y - ((offset_y + w->frame_size.y) - 1));
496     }
497     if (offset_x > 0)
498     {
499         winscroll_hint(w, '<', offset_x + 1);
500     }
501     if (size_x > offset_x + w->frame_size.x)
502     {
503         winscroll_hint(w, '>', size_x - ((offset_x + w->frame_size.x) - 1));
504     }
505     struct Win * next = get_win_after(w->id);
506     if (next)
507     {
508         draw_wins(next);
509     }
510 }
511
512
513
514 static void append_win(struct Win * w)
515 {
516     char * f_name = "append_win()";
517     uint8_t old_size = strlen(world.winDB.order) + 1;
518     char * new_order = try_malloc(old_size + 1, f_name);
519     memcpy(new_order, world.winDB.order, old_size - 1);
520     new_order[old_size - 1] = w->id;
521     new_order[old_size] = '\0';
522     free(world.winDB.order);
523     world.winDB.order = new_order;
524     world.winDB.active = w->id;
525     update_wins(w);
526 }
527
528
529
530 static void suspend_win(struct Win * w)
531 {
532     char * f_name = "suspend_win()";
533     uint8_t new_size = strlen(world.winDB.order);
534     char * new_order = try_malloc(new_size, f_name);
535     uint8_t i = get_pos_in_order(w->id);
536     char next_char = world.winDB.order[i + 1];
537     world.winDB.order[i] = '\0';
538     char * second_part = &world.winDB.order[i + 1];
539     sprintf(new_order, "%s%s", world.winDB.order, second_part);
540     free(world.winDB.order);
541     world.winDB.order = new_order;
542     world.winDB.active = world.winDB.order[i];
543     if (!world.winDB.order[i] && 0 < i)
544     {
545         world.winDB.active = world.winDB.order[i - 1];
546     }
547     if (world.winDB.order[i])
548     {
549         update_wins(get_win_by_id(next_char)); /* Already calls               */
550         return;                                /* refit_v_screen(), so leave. */
551     }
552     refit_v_screen();
553 }
554
555
556
557 extern uint16_t center_offset(uint16_t position, uint32_t mapsize,
558                               uint32_t frame_size)
559 {
560     uint16_t offset = 0;
561     if (mapsize > frame_size)
562     {
563         if (position > frame_size / 2)
564         {
565             if (position < mapsize - (frame_size / 2))
566             {
567                 offset = position - (frame_size / 2);
568             }
569             else
570             {
571                 offset = mapsize - frame_size;
572             }
573         }
574     }
575     return offset;
576 }
577
578
579
580 extern struct Win * get_win_by_id(char id)
581 {
582     uint8_t i = 0;
583     while ('\0' != world.winDB.ids[i])
584     {
585         if (id == world.winDB.ids[i])
586         {
587             return &world.winDB.wins[i];
588         }
589         i++;
590     }
591     return NULL;
592 }
593
594
595
596 extern uint8_t read_winconf_from_file(char * line, uint32_t linemax,
597                                       FILE * file)
598 {
599     char * f_name = "read_winconf_from_file()";
600     char * context = "Failed reading individual window's configuration from "
601                      "interface config file. ";
602     char * err_id  = "Illegal ID(s) selected.";
603     char * err_2   = "Double-initialized window.";
604     char * err_brk = "Illegal line break type index.";
605     int test_for_end = try_fgetc(file, f_name);
606     if (EOF == test_for_end || '\n' == test_for_end)
607     {
608         return 0;
609     }
610     exit_trouble(EOF == ungetc(test_for_end, file), f_name, "ungetc()");
611     err_try_fgets(line, linemax, file, context, "ns");
612     err_line(NULL == strchr(world.winDB.legal_ids, line[0]), line, context,
613              err_id);
614     exit_err(world.winDB.ids && NULL!=strchr(world.winDB.ids, line[0]), err_2);
615     struct Win win;
616     memset(&win, 0, sizeof(struct Win));
617     win.id = (char) line[0];
618     err_try_fgets(line, linemax, file, context, "0n");
619     win.title = try_malloc(strlen(line), f_name);
620     memcpy(win.title, line, strlen(line) - 1);      /* Eliminate newline char */
621     win.title[strlen(line) - 1] = '\0';             /* char at end of string. */
622     err_try_fgets(line, linemax, file, context, "0nsi");
623     err_line(atoi(line) > 2, line, context, err_brk);
624     win.linebreak = atoi(line);
625     err_try_fgets(line, linemax, file, context, "0ni");
626     win.target_height = atoi(line);
627     win.target_height_type = (0 >= win.target_height);
628     err_try_fgets(line, linemax, file, context, "0ni");
629     win.target_width = atoi(line);
630     win.target_width_type = (0 >= win.target_width);
631     read_keybindings_from_file(line, linemax, file, &win.kb);
632     if (world.winDB.ids)
633     {
634         uint8_t old_ids_size = strlen(world.winDB.ids);
635         char * new_ids = try_malloc(old_ids_size + 1 + 1, f_name);
636         sprintf(new_ids, "%s%c", world.winDB.ids, win.id);
637         free(world.winDB.ids);
638         world.winDB.ids = new_ids;
639         array_append(old_ids_size, sizeof(struct Win), (void *) &win,
640                      (void **) &world.winDB.wins);
641         return 1;
642     }
643     world.winDB.ids = try_malloc(2, f_name);
644     sprintf(world.winDB.ids, "%c", win.id);
645     world.winDB.wins = try_malloc(sizeof(struct Win), f_name);
646     world.winDB.wins[0] = win;
647     return 1;
648 }
649
650
651
652 extern void write_winconf_of_id_to_file(FILE * file, char c)
653 {
654     char * f_name = "write_winconf_of_id_to_file()";
655     struct Win * wc = get_win_by_id(c);
656     uint8_t size = strlen(wc->title) + 2;
657     if (size < 7)  /* Ensure that at least 5 + 2 char fit into line so that   */
658     {              /* the digit representation of any uint16_t may be stored. */
659         size = 7;
660     }
661     char line[size];
662     sprintf(line, "%c\n", wc->id);
663     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
664     sprintf(line, "%s\n", wc->title);
665     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
666     sprintf(line, "%d\n", wc->linebreak);
667     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
668     sprintf(line, "%d\n", wc->target_height);
669     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
670     sprintf(line, "%d\n", wc->target_width);
671     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
672     write_keybindings_to_file(file, &wc->kb);
673 }
674
675
676
677 extern void read_order_wins_visible_active(char * line, uint32_t linemax,
678                                            FILE * file, char ** tmp_order,
679                                            char * tmp_active)
680 {
681     char * f_name = "read_order_wins_visible_active()";
682     char * context   = "Failed reading order and activation of visible windows "
683                        "from interface config file. ";
684     char * err_id    = "Illegal ID(s) selected.";
685     err_try_fgets(line, linemax, file, context, "01");
686     uint32_t i = 0;
687     for (; i < strlen(line) - 1; i++)
688     {
689         char * test = strchr(world.winDB.legal_ids, line[i]);
690         err_line(!test, line, context, err_id);
691     }
692     line[strlen(line) - 1] = '\0';
693     *tmp_order = try_malloc(strlen(line) + 1, f_name);
694     sprintf(*tmp_order, "%s", line);
695     if (*tmp_order[0])
696     {
697         err_try_fgets(line, linemax, file, context, "0nfs");
698         err_line(NULL == strchr(*tmp_order, line[0]), line, context, err_id);
699         *tmp_active = line[0];
700     }
701     else
702     {
703         err_try_fgets(line, linemax, file, context, "0ne");
704         *tmp_active = '\0';
705     }
706     err_try_fgets(line, linemax, file, context, "d");
707 }
708
709
710
711 extern void write_order_wins_visible_active(FILE * file)
712 {
713     char * f_name = "write_order_wins_visible_active()";
714     try_fwrite(world.winDB.order, strlen(world.winDB.order), 1, file, f_name);
715     try_fputc('\n', file, f_name);
716     try_fputc(world.winDB.active, file, f_name);
717     try_fputc('\n', file, f_name);
718     try_fwrite(world.delim, strlen(world.delim), 1, file, f_name);
719 }
720
721
722
723 extern void make_v_screen_and_init_win_sizes()
724 {
725     char * f_name = "make_v_screen_and_init_win_sizes()";
726     char * err_s = "creating an illegaly large virtual screen";
727     char * err_m = "triggering a memory allocation error via newpad()";
728     uint32_t maxy_test = getmaxy(world.winDB.t_screen);
729     uint32_t maxx_test = getmaxx(world.winDB.t_screen);
730     exit_trouble(maxy_test>UINT16_MAX || maxx_test>UINT16_MAX, f_name, err_s);
731     world.winDB.v_screen_size.y = maxy_test;
732     world.winDB.v_screen_size.x = maxx_test;
733     world.winDB.v_screen = newpad(world.winDB.v_screen_size.y, 1);
734     exit_trouble(NULL == world.winDB.v_screen, f_name, err_m);
735     char id;
736     while (0 != (id = get_next_win_id()))
737     {
738         init_win_size_from_winconf_and_v_screen_size(id);
739     }
740 }
741
742
743
744 extern void free_winDB()
745 {
746     char id;
747     while (0 != (id = get_next_win_id()))
748     {
749         struct Win * wc = get_win_by_id(id);
750         free(wc->title);
751         free(wc->kb.kbs);
752         wc->kb.kbs = NULL;
753     }
754     free(world.winDB.ids);  /* NULL this too since add_win_to_winDB() checks  */
755     world.winDB.ids = NULL; /* for it to detect its first post-DB-purge round.*/
756     free(world.winDB.wins);
757     free(world.winDB.order);
758 }
759
760
761
762 extern void winch_called()
763 {
764     world.winch = 1;
765 }
766
767
768
769 extern void reset_windows_on_winch()
770 {
771     endwin();  /* "[S]tandard way" to recalibrate ncurses post SIGWINCH, says */
772     refresh(); /* <http://invisible-island.net/ncurses/ncurses-intro.html>.   */
773     char tmp_order[strlen(world.winDB.order) + 1];
774     sprintf(tmp_order, "%s", world.winDB.order);
775     uint8_t i;
776     char tmp_active = world.winDB.active;
777     for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
778     delwin(world.winDB.v_screen);
779     make_v_screen_and_init_win_sizes();
780     for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
781     world.winDB.active = tmp_active;
782 }
783
784
785
786 extern void draw_all_wins()
787 {
788     /* Empty everything before filling it a-new. */
789     erase();
790     wnoutrefresh(world.winDB.t_screen);
791     werase(world.winDB.v_screen);
792     if (world.winDB.active)
793     {
794
795         /* Draw borders, wins. Order matters: corners should overwrite lines. */
796         draw_wins_borderlines(get_win_by_id(world.winDB.order[0]));
797         draw_wins_bordercorners(get_win_by_id(world.winDB.order[0]));
798         draw_wins(get_win_by_id(world.winDB.order[0]));
799
800         /* Draw .v_screen scroll hints. */
801         struct yx_uint16 start;
802         start.y = 0;
803         start.x = world.winDB.v_screen_offset;
804         char * cols_string = "columns";
805         if (world.winDB.v_screen_offset > 0)
806         {
807             scroll_hint(world.winDB.v_screen_size, '<',
808                         world.winDB.v_screen_offset + 1, cols_string, start);
809         }
810         uint16_t size_x = getmaxx(world.winDB.v_screen);
811         uint16_t right_edge =   world.winDB.v_screen_offset
812                               + world.winDB.v_screen_size.x;
813         if (right_edge < size_x - 1)
814         {
815             scroll_hint(world.winDB.v_screen_size, '>',
816                         size_x - right_edge, cols_string, start);
817         }
818
819         /* Put .v_screen segment to be shown on .t_screen to .t_screen buffer.*/
820         pnoutrefresh(world.winDB.v_screen, 0, world.winDB.v_screen_offset, 0, 0,
821                      world.winDB.v_screen_size.y,
822                      world.winDB.v_screen_size.x - 1);
823     }
824
825     /* Only at the end write accumulated changes to .t_screen. */
826     doupdate();
827 }
828
829
830
831 extern void toggle_window(char id)
832 {
833     struct Win * win = get_win_by_id(id);
834     if (NULL == strchr(world.winDB.order, id))
835     {
836         append_win(win);
837         return;
838     }
839     suspend_win(win);
840 }
841
842
843
844 extern void toggle_winconfig()
845 {
846     if (!world.winDB.active)
847     {
848         return;
849     }
850     struct Win * w = get_win_by_id(world.winDB.active);
851     if      (0 == w->view)
852     {
853         w->view          = 1;
854         w->target_center = w->center;
855         memset(&w->center, 0, sizeof(struct yx_uint16));
856         return;
857     }
858     else if (1 == w->view)
859     {
860         w->view          = 2;
861         w->center.x      = 0;
862         return;
863     }
864     w->view          = 0;
865     w->center        = w->target_center;
866 }
867
868
869
870 extern void toggle_win_size_type(char axis)
871 {
872     struct Win * w = get_win_by_id(world.winDB.active);
873     if ('y' == axis)
874     {
875         w->target_height_type = (0 == w->target_height_type);
876         set_win_target_size(w);
877         return;
878     }
879     w->target_width_type = (   0 == w->target_width_type
880                             && w->frame_size.x <= world.winDB.v_screen_size.x);
881     set_win_target_size(w);
882 }
883
884
885
886 extern void toggle_linebreak_type()
887 {
888     struct Win * w = get_win_by_id(world.winDB.active);
889     if      (0 == w->linebreak)
890     {
891         w->linebreak = 1;
892     }
893     else if (1 == w->linebreak)
894     {
895         w->linebreak = 2;
896     }
897     else if (2 == w->linebreak)
898     {
899         w->linebreak = 0;
900     }
901 }
902
903
904
905 extern void resize_active_win(char change)
906 {
907     if (world.winDB.active)
908     {
909         struct Win * w = get_win_by_id(world.winDB.active);
910         if      (change == '-' && w->frame_size.y > 1)
911         {
912             w->frame_size.y--;
913         }
914         else if (change == '_' && w->frame_size.y > 1)
915         {
916             w->frame_size.x--;
917         }
918         else if (   change == '+'
919                  && w->frame_size.y < world.winDB.v_screen_size.y - 1)
920         {
921             w->frame_size.y++;
922         }
923         else if (change == '*' && w->frame_size.y < UINT16_MAX)
924         {
925             w->frame_size.x++;
926         }
927         if (   1 == w->target_width_type
928             && w->frame_size.x > world.winDB.v_screen_size.x)
929         {
930             w->target_width_type = 0;
931         }
932         set_win_target_size(w);
933         update_wins(w);
934     }
935 }
936
937
938
939 extern void shift_active_win(char dir)
940 {
941     uint8_t len_order = strlen(world.winDB.order);
942     if (1 < len_order)
943     {
944         char tmp[len_order + 1];
945         tmp[len_order] = '\0';
946         uint8_t pos = get_pos_in_order(world.winDB.active);
947         if ('f' == dir)
948         {
949             if (pos == len_order - 1)
950             {
951                 memcpy(tmp + 1, world.winDB.order, len_order - 1);
952                 tmp[0] = world.winDB.active;
953                 memcpy(world.winDB.order, tmp, len_order + 1);
954             }
955             else
956             {
957                 world.winDB.order[pos] = world.winDB.order[pos + 1];
958                 world.winDB.order[pos + 1] = world.winDB.active;
959             }
960         }
961         else
962         {
963             if (pos == 0)
964             {
965                 memcpy(tmp, world.winDB.order + 1, len_order - 1);
966                 tmp[len_order - 1] = world.winDB.active;
967                 memcpy(world.winDB.order, tmp, len_order + 1);
968             }
969             else
970             {
971                 world.winDB.order[pos] = world.winDB.order[pos - 1];
972                 world.winDB.order[pos - 1] = world.winDB.active;
973             }
974         }
975         update_wins(get_win_by_id(world.winDB.order[0]));
976     }
977 }
978
979
980
981 extern void scroll_v_screen(char dir)
982 {
983     if      (   '+' == dir
984              &&   world.winDB.v_screen_offset + world.winDB.v_screen_size.x + 1
985                 < getmaxx(world.winDB.v_screen))
986     {
987         world.winDB.v_screen_offset++;
988     }
989     else if (   '-' == dir
990              && world.winDB.v_screen_offset > 0)
991     {
992         world.winDB.v_screen_offset--;
993     }
994 }
995
996
997
998 extern void cycle_active_win(char dir)
999 {
1000     uint8_t len_order = strlen(world.winDB.order);
1001     if (1 < len_order)
1002     {
1003         uint8_t pos = get_pos_in_order(world.winDB.active);
1004         if ('f' == dir)
1005         {
1006             world.winDB.active = world.winDB.order[pos + 1];
1007             if ('\0' == world.winDB.active)
1008             {
1009                 world.winDB.active = world.winDB.order[0];
1010             }
1011             return;
1012         }
1013         if (pos > 0)
1014         {
1015             world.winDB.active = world.winDB.order[pos - 1];
1016             return;
1017         }
1018         world.winDB.active = world.winDB.order[len_order - 1];
1019     }
1020 }