home · contact · privacy
Handle SIGWINCH signals via reset_windows().
[plomrogue] / src / client / windows.c
1 /* src/client/windows.c */
2
3 #include "windows.h"
4 #include <stddef.h> /* NULL */
5 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX */
6 #include <stdio.h> /* sprintf() */
7 #include <stdlib.h> /* free() */
8 #include <string.h> /* strlen(), memcpy(), strnlen() */
9 #include <ncurses.h> /* chtype, pnoutrefresh(), doupdate(), werase(), erase(),
10                       * wnoutrefresh(), getmaxx(), getmaxy(), initscr(),
11                       * noecho(), curs_set(), newpad(), mvwaddch(), mvwaddstr(),
12                       * wresize()
13                       */
14 #include "../common/rexit.h" /* for exit_err() */
15 #include "../common/try_malloc.h" /* for try_malloc() */
16 #include "../common/yx_uint16.h" /* for struct yx_uint16 */
17 #include "cleanup.h" /* for set_cleanup_flag() */
18 #include "misc.h" /* for center_offset() */
19 #include "world.h" /* for world global */
20
21
22
23
24 /* Make virtual screen just wide enough to contain all visible windows. */
25 static void refit_pad();
26
27 /* Update geometry (sizes, positions) of window "w" and its successors in the
28  * window chain. Use place_win() for the positioning algorithm.
29  */
30 static void update_wins(struct Win * w);
31 static void place_win(struct Win * w);
32
33 /* Draw scroll hint (a line saying that there are "dist" more elements of "unit"
34  * further into the direction symbolized by "dir") into virtual screen, onto an
35  * appropriate edge of a window or the screen; the left/right edge if "dir" is
36  * "<"/">", or the top/bottom edge if it is "^"/"v". "start" be either the start
37  * coordinate of a window's frame, or .y=0, .x=wm->pad_offset for the virtual
38  * screen. winscroll_hint() and padscroll_hint() are wrappers to both cases.
39  */
40 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
41                         char * unit, struct yx_uint16 start);
42 static void winscroll_hint(struct Win * w, char dir, uint16_t dist);
43 static void padscroll_hint(char dir, uint16_t dist);
44
45 /* Draw contents of all windows in window chain from window "w" onwards. */
46 static void draw_wins(struct Win * w);
47
48 /* draw_win_borderlines() draws vertical/horizontal borders of window "w" sans
49  * corners into the virtual screen. It draws the top border line as the windows'
50  * title bar (highlighted if the window is selected as active). It is called
51  * recursively by draw_wins_borderlines() on all windows from "w" on.
52  * draw_wins_bordercorners() draws the border corners of "w" and its successors.
53  */
54 static void draw_win_borderlines(struct Win * w);
55 static void draw_wins_borderlines(struct Win * w);
56 static void draw_wins_bordercorners(struct Win * w);
57
58 /* Shift active window forwards / backwards in window chain. */
59 static void shift_win_forward();
60 static void shift_win_backward();
61
62
63
64 static void refit_pad()
65 {
66     /* Determine rightmost window column. */
67     uint32_t lastwcol = 0;
68     struct Win * wp = world.wmeta.chain_start;
69     while (wp != 0)
70     {
71         if ((uint32_t) wp->start.x + (uint32_t) wp->framesize.x > lastwcol + 1)
72         {
73             lastwcol = (uint32_t) wp->start.x + (uint32_t) wp->framesize.x - 1;
74         }
75         wp = wp->next;
76     }
77
78     /* Only resize the pad if the rightmost window column has changed. */
79     char * err_s = "refit_pad() extends virtual screen beyond legal sizes.";
80     char * err_m = "refit_pad() triggers memory alloc error via wresize().";
81     if (getmaxx(world.wmeta.pad) + 1 != lastwcol)
82     {
83         uint8_t t = (lastwcol + 2 > UINT16_MAX);
84         exit_err(t, err_s);
85         t = wresize(world.wmeta.pad, getmaxy(world.wmeta.pad), lastwcol + 2);
86         exit_err(t, err_m);
87     }
88 }
89
90
91
92 static void update_wins(struct Win * w)
93 {
94     place_win(w);
95     refit_pad();
96     if (0 != w->next)
97     {
98         update_wins(w->next);
99     }
100 }
101
102
103
104 static void place_win(struct Win * w)
105 {
106     /* If w is first window, it goes into the top left corner. */
107     w->start.x = 0;
108     w->start.y = 1;                             /* Leave space for title bar. */
109     if (0 != w->prev)
110     {
111
112         /* If not, fit w's top left to top right of last top predecessor. */
113         struct Win * w_top = w->prev;
114         while (w_top->start.y != 1)
115         {
116             w_top = w_top->prev;
117         }
118         w->start.x = w_top->start.x + w_top->framesize.x + 1;
119
120         /* Fit w's top left to bottom left of its ->prev if enough space. */
121         uint16_t w_prev_maxy = w->prev->start.y + w->prev->framesize.y;
122         if (   w->framesize.x <= w->prev->framesize.x
123             && w->framesize.y <  world.wmeta.padsize.y - w_prev_maxy)
124         {
125             w->start.x = w->prev->start.x;
126             w->start.y = w_prev_maxy + 1;
127         }
128
129         /* Failing that, try to fit w' top left to the top right of the last
130          * predecessor w_test 1) not followed by windows with a left corner
131          * further rightwards than its own 2) with enough space rightwards for w
132          * until the bottom right of w_thr directly throning over it 3) and with
133          * this same space extending far enough to the bottom for fitting in w.
134          */
135         else
136         {
137             struct Win * w_test = w->prev;
138             struct Win * w_thr;
139             while (w_test != w_top)
140             {
141                 w_thr = w_test->prev;
142                 for (; w_test->start.y <= w_thr->start.y; w_thr = w_thr->prev);
143                 uint16_t w_thr_bottom = w_thr->start.y + w_thr->framesize.y;
144                 uint16_t free_width = (w_thr->start.x + w_thr->framesize.x)
145                                       - (w_test->start.x + w_test->framesize.x);
146                 if (   w->framesize.y < world.wmeta.padsize.y - w_thr_bottom
147                     && w->framesize.x < free_width)
148                 {
149                     w->start.x = w_test->start.x + w_test->framesize.x + 1;
150                     w->start.y = w_thr_bottom + 1;
151                     break;
152                 }
153                 w_test = w_thr;
154             }
155         }
156     }
157 }
158
159
160
161 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
162                         char * unit, struct yx_uint16 start)
163 {
164     /* Decide on alignment (vertical/horizontal?), thereby hint text space. */
165     char * more = "more";
166     uint16_t dsc_space = fsize.x;
167     if ('<' == dir || '>' == dir)
168     {
169         dsc_space = fsize.y;
170     }                                  /* vv-- 10 = max strlen for uint16_t */
171     char scrolldsc[1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1];
172     sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
173
174     /* Decide on offset of the description text inside the scroll hint line. */
175     uint16_t dsc_offset = 1;
176     if (dsc_space > strlen(scrolldsc) + 1)
177     {
178         dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
179     }
180
181     /* Draw scroll hint line as dir symbols bracketing description text. */
182     uint16_t draw_offset = 0;
183     if      ('>' == dir)
184     {
185         draw_offset = fsize.x - 1;
186     }
187     else if ('v' == dir)
188     {
189         draw_offset = fsize.y - 1;
190     }
191     uint16_t q = 0;
192     for (; q < dsc_space; q++)
193     {
194         chtype c = dir | A_REVERSE;
195         if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
196         {
197             c = scrolldsc[q - dsc_offset] | A_REVERSE;
198         }
199         if ('<' == dir || '>' == dir)
200         {
201             mvwaddch(world.wmeta.pad, start.y + q, start.x + draw_offset, c);
202             continue;
203         }
204         mvwaddch(world.wmeta.pad, start.y + draw_offset, start.x + q, c);
205     }
206 }
207
208
209 static void padscroll_hint(char dir, uint16_t dist)
210 {
211     struct yx_uint16 start;
212     start.y = 0;
213     start.x = world.wmeta.pad_offset;
214     scroll_hint(world.wmeta.padsize, dir, dist, "columns", start);
215 }
216
217
218
219 static void winscroll_hint(struct Win * w, char dir, uint16_t dist)
220 {
221     char * unit = "lines";
222     if ('<' == dir || '>' == dir)
223     {
224         unit = "columns";
225     }
226     struct yx_uint16 start = w->start;
227     scroll_hint(w->framesize, dir, dist, unit, start);
228 }
229
230
231
232 static void draw_wins(struct Win * w)
233 {
234     w->draw(w);
235     uint16_t size_y = w->winmapsize.y;
236     uint16_t size_x = w->winmapsize.x;
237     uint16_t offset_y = center_offset(w->center.y, size_y, w->framesize.y);
238     uint16_t offset_x = center_offset(w->center.x, size_x, w->framesize.x);
239     uint16_t y, x;
240     for (y = offset_y; y < w->framesize.y + offset_y && y < size_y; y++)
241     {
242         for (x = offset_x; x < w->framesize.x + offset_x && x < size_x; x++)
243         {
244             chtype ch = w->winmap[(y * w->winmapsize.x) + x];
245             mvwaddch(world.wmeta.pad, w->start.y + (y - offset_y),
246                                        w->start.x + (x - offset_x), ch);
247         }
248     }
249     free(w->winmap);
250     w->winmap = NULL;
251     w->winmapsize.y = 0;
252     w->winmapsize.x = 0;
253     if (offset_y > 0)
254     {
255         winscroll_hint(w, '^', offset_y + 1);
256     }
257     if (size_y > offset_y + w->framesize.y)
258     {
259         winscroll_hint(w, 'v', size_y - ((offset_y + w->framesize.y) - 1));
260     }
261     if (offset_x > 0)
262     {
263         winscroll_hint(w, '<', offset_x + 1);
264     }
265     if (size_x > offset_x + w->framesize.x)
266     {
267         winscroll_hint(w, '>', size_x - ((offset_x + w->framesize.x) - 1));
268     }
269     if (0 != w->next)
270     {
271         return draw_wins(w->next);
272     }
273 }
274
275
276
277 static void draw_win_borderlines(struct Win * w)
278 {
279     /* Draw vertical and horizontal border lines. */
280     uint16_t y, x;
281     for (y = w->start.y; y <= w->start.y + w->framesize.y; y++)
282     {
283         mvwaddch(world.wmeta.pad, y, w->start.x - 1,              '|');
284         mvwaddch(world.wmeta.pad, y, w->start.x + w->framesize.x, '|');
285     }
286     for (x = w->start.x; x <= w->start.x + w->framesize.x; x++)
287     {
288         mvwaddch(world.wmeta.pad, w->start.y - 1,              x, '-');
289         mvwaddch(world.wmeta.pad, w->start.y + w->framesize.y, x, '-');
290     }
291
292     /* Draw as much as possible of the title into center of top border line. */
293     char min_title_length_visible = 3;  /* min. 1 char + 2 padding/decoration */
294     if (w->framesize.x >= min_title_length_visible)
295     {
296         uint16_t title_offset = 0;
297         if (w->framesize.x > strlen(w->title) + 2)
298         {
299             title_offset = (w->framesize.x - (strlen(w->title) + 2)) / 2;
300         }                                    /* +2 is for padding/decoration */
301         uint16_t length_visible = strnlen(w->title, w->framesize.x - 2);
302         char title[length_visible + 3];
303         char decoration = ' ';
304         if (w == world.wmeta.active)
305         {
306             decoration = '$';
307         }
308         memcpy(title + 1, w->title, length_visible);
309         title[0] = title[length_visible + 1] = decoration;
310         title[length_visible + 2] = '\0';
311         mvwaddstr(world.wmeta.pad,
312                   w->start.y - 1, w->start.x + title_offset, title);
313     }
314 }
315
316
317
318 static void draw_wins_borderlines(struct Win * w)
319 {
320     draw_win_borderlines(w);
321     if (0 != w->next)
322     {
323         draw_wins_borderlines(w->next);
324     }
325 }
326
327
328
329 static void draw_wins_bordercorners(struct Win * w)
330 {
331     mvwaddch(world.wmeta.pad, w->start.y - 1, w->start.x - 1, '+');
332     mvwaddch(world.wmeta.pad, w->start.y - 1, w->start.x + w->framesize.x,'+');
333     mvwaddch(world.wmeta.pad, w->start.y + w->framesize.y, w->start.x - 1,'+');
334     mvwaddch(world.wmeta.pad, w->start.y + w->framesize.y,
335              w->start.x + w->framesize.x, '+');
336     if (0 != w->next)
337     {
338         draw_wins_bordercorners(w->next);
339     }
340 }
341
342
343
344 static void shift_win_forward()
345 {
346     if (world.wmeta.active == world.wmeta.chain_end)
347     {
348         world.wmeta.chain_end = world.wmeta.active->prev;
349         world.wmeta.chain_end->next = 0;
350         world.wmeta.active->next = world.wmeta.chain_start;
351         world.wmeta.active->next->prev = world.wmeta.active;
352         world.wmeta.chain_start = world.wmeta.active;
353         world.wmeta.chain_start->prev = 0;
354     }
355     else
356     {
357         struct Win * old_prev = world.wmeta.active->prev;
358         struct Win * old_next = world.wmeta.active->next;
359         if (world.wmeta.chain_end == world.wmeta.active->next)
360         {
361             world.wmeta.chain_end = world.wmeta.active;
362             world.wmeta.active->next = 0;
363         }
364         else
365         {
366             world.wmeta.active->next = old_next->next;
367             world.wmeta.active->next->prev = world.wmeta.active;
368         }
369         if (world.wmeta.chain_start == world.wmeta.active)
370         {
371             world.wmeta.chain_start = old_next;
372         }
373         else
374         {
375             old_prev->next = old_next;
376         }
377         old_next->prev = old_prev;
378         old_next->next = world.wmeta.active;
379         world.wmeta.active->prev = old_next;
380     }
381 }
382
383
384
385 static void shift_win_backward()
386 {
387     if (world.wmeta.active == world.wmeta.chain_start)
388     {
389         world.wmeta.chain_start = world.wmeta.active->next;
390         world.wmeta.chain_start->prev = 0;
391         world.wmeta.active->prev = world.wmeta.chain_end;
392         world.wmeta.active->prev->next = world.wmeta.active;
393         world.wmeta.chain_end = world.wmeta.active;
394         world.wmeta.chain_end->next = 0;
395     }
396     else
397     {
398         struct Win * old_prev = world.wmeta.active->prev;
399         struct Win * old_next = world.wmeta.active->next;
400         if (world.wmeta.chain_start == world.wmeta.active->prev)
401         {
402             world.wmeta.chain_start = world.wmeta.active;
403             world.wmeta.active->prev = 0;
404         }
405         else
406         {
407             world.wmeta.active->prev = old_prev->prev;
408             world.wmeta.active->prev->next = world.wmeta.active;
409         }
410         if (world.wmeta.chain_end == world.wmeta.active)
411         {
412             world.wmeta.chain_end = old_prev;
413         }
414         else
415         {
416             old_next->prev = old_prev;
417         }
418         old_prev->next = old_next;
419         old_prev->prev = world.wmeta.active;
420         world.wmeta.active->next = old_prev;
421     }
422 }
423
424
425
426 extern void init_wmeta_and_ncurses()
427 {
428     world.wmeta.screen = initscr();
429     set_cleanup_flag(CLEANUP_NCURSES);
430     noecho();
431     curs_set(0);
432     world.wmeta.padsize.y   = 0;
433     world.wmeta.padsize.x   = 0;
434     world.wmeta.chain_start = 0;
435     world.wmeta.chain_end   = 0;
436     world.wmeta.pad_offset  = 0;
437     world.wmeta.pad         = NULL;
438     world.wmeta.active      = 0;
439 }
440
441
442
443 extern void make_pad()
444 {
445     char * err_s = "make_pad() creates an illegaly large virtual screen.";
446     char * err_m = "make_pad() triggers memory allocation error via newpad().";
447     uint32_t maxy_test = getmaxy(world.wmeta.screen);
448     uint32_t maxx_test = getmaxx(world.wmeta.screen);
449     exit_err(maxy_test > UINT16_MAX || maxx_test > UINT16_MAX, err_s);
450     world.wmeta.padsize.y = maxy_test;
451     world.wmeta.padsize.x = maxx_test;
452     world.wmeta.pad = newpad(world.wmeta.padsize.y, 1);
453     exit_err(NULL == world.wmeta.pad, err_m);
454 }
455
456
457
458 extern void init_win(struct Win ** wp, char * title, int16_t height,
459                      int16_t width, void * func)
460 {
461     char * f_name = "init_win()";
462     struct Win * w  = try_malloc(sizeof(struct Win), f_name);
463     w->prev         = 0;
464     w->next         = 0;
465     w->winmapsize.y = 0;
466     w->winmapsize.x = 0;
467     w->winmap       = NULL;
468     w->title        = try_malloc(strlen(title) + 1, f_name);
469     sprintf(w->title, "%s", title);
470     w->draw         = func;
471     w->center.y     = 0;
472     w->center.x     = 0;
473     w->framesize.y  = world.wmeta.padsize.y - 1;
474     if      (0 < height && height <= world.wmeta.padsize.y - 1)
475     {
476         w->framesize.y = height;
477     }
478     else if (0 > height && world.wmeta.padsize.y + (height - 1) > 0)
479     {
480         w->framesize.y = world.wmeta.padsize.y + (height - 1);
481     }
482     w->framesize.x  = world.wmeta.padsize.x;
483     if      (0 < width)
484     {
485         w->framesize.x = width;
486     }
487     else if (0 > width && world.wmeta.padsize.x + width > 0)
488     {
489         w->framesize.x = world.wmeta.padsize.x + width;
490     }
491     *wp = w;
492 }
493
494
495
496 extern void free_win(struct Win * win)
497 {
498     free(win->title);
499     free(win);
500 }
501
502
503
504 extern void append_win(struct Win * w)
505 {
506     if (0 != world.wmeta.chain_start)
507     {
508         w->prev = world.wmeta.chain_end;
509         world.wmeta.chain_end->next = w;
510     }
511     else
512     {
513         world.wmeta.active = w;
514         world.wmeta.chain_start = w;
515     }
516     world.wmeta.chain_end = w;
517     update_wins(w);
518 }
519
520
521
522 extern void suspend_win(struct Win * w)
523 {
524     if (world.wmeta.chain_start != w)
525     {
526         w->prev->next = w->next;
527     }
528     else
529     {
530         world.wmeta.chain_start = w->next;
531     }
532     uint8_t pad_refitted = 0;
533     if (world.wmeta.chain_end != w)
534     {
535         w->next->prev = w->prev;
536         if (world.wmeta.active == w)
537         {
538             world.wmeta.active = w->next;
539         }
540         update_wins(w->next);      /* Positioning of successor windows may be */
541         pad_refitted = 1;          /* affected / need correction. Note that   */
542     }                              /* update_wins() already refits the pad,   */
543     else                           /* voiding later need for that.            */
544     {
545         world.wmeta.chain_end = w->prev;
546         if (world.wmeta.active == w)
547         {
548             world.wmeta.active = w->prev;
549         }
550     }
551     w->prev = 0;
552     w->next = 0;
553     if (0 == pad_refitted)
554     {
555         refit_pad();
556     }
557 }
558
559
560
561 extern void reset_pad_offset(uint16_t new_offset)
562 {
563     if (new_offset >= 0
564         && (new_offset < world.wmeta.pad_offset
565             || new_offset + world.wmeta.padsize.x < getmaxx(world.wmeta.pad)))
566     {
567         world.wmeta.pad_offset = new_offset;
568     }
569 }
570
571
572
573 extern void resize_active_win(struct yx_uint16 size)
574 {
575     if (0 != world.wmeta.active
576         && size.x > 0 && size.y > 0 && size.y < world.wmeta.padsize.y)
577     {
578         world.wmeta.active->framesize = size;
579         update_wins(world.wmeta.active);          /* Positioning of following */
580     }                                             /* windows may be affected. */
581 }
582
583
584
585 extern void cycle_active_win(char dir)
586 {
587     if (0 != world.wmeta.active)
588     {
589         if ('f' == dir)
590         {
591             if (world.wmeta.active->next != 0)
592             {
593                 world.wmeta.active = world.wmeta.active->next;
594             }
595             else
596             {
597                 world.wmeta.active = world.wmeta.chain_start;
598             }
599         }
600         else
601         {
602             if (world.wmeta.active->prev != 0)
603             {
604                 world.wmeta.active = world.wmeta.active->prev;
605             }
606             else
607             {
608                 world.wmeta.active = world.wmeta.chain_end;
609             }
610         }
611     }
612 }
613
614
615
616 extern void shift_active_win(char dir)
617 {
618     if (   0 == world.wmeta.active  /* No shifting with < 2 windows visible. */
619         || world.wmeta.chain_start == world.wmeta.chain_end)
620     {
621         return;
622     }
623     if ('f' == dir)
624     {
625         shift_win_forward();
626         update_wins(world.wmeta.chain_start);
627         return;
628     }
629     shift_win_backward();
630     update_wins(world.wmeta.chain_start);
631 }
632
633
634
635 extern void draw_all_wins()
636 {
637     /* Empty everything before filling it a-new. */
638     erase();
639     wnoutrefresh(world.wmeta.screen);
640     werase(world.wmeta.pad);
641     if (world.wmeta.chain_start)
642     {
643
644         /* Draw windows' borders first, then windows. */
645         draw_wins_borderlines(world.wmeta.chain_start);
646         draw_wins_bordercorners(world.wmeta.chain_start);
647         draw_wins(world.wmeta.chain_start);
648
649         /* Draw virtual screen scroll hints. */
650         if (world.wmeta.pad_offset > 0)
651         {
652             padscroll_hint('<', world.wmeta.pad_offset + 1);
653         }
654         uint16_t size_x = getmaxx(world.wmeta.pad);
655         uint16_t right_edge = world.wmeta.pad_offset + world.wmeta.padsize.x;
656         if (right_edge < size_x - 1)
657         {
658             padscroll_hint('>', size_x - right_edge);
659         }
660
661         /* Write pad segment to be shown on physical screen to screen buffer. */
662         pnoutrefresh(world.wmeta.pad, 0, world.wmeta.pad_offset, 0, 0,
663                      world.wmeta.padsize.y, world.wmeta.padsize.x - 1);
664     }
665
666     /* Only at the end write accumulated changes to the physical screen. */
667     doupdate();
668 }