home · contact · privacy
Included <stddef.h> were NULL was used.
[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> /* pnoutrefresh(), doupdate(), werase(), wnoutrefresh(),
10                       * erase(), getmaxx(), getmaxy(), delwin(), endwin(),
11                       * initscr(), noecho(), curs_set(), newpad(), mvwaddch(),
12                       * mvwaddstr(), wresize(), chtype
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_win_meta()
427 {
428     char * err_s = "init_win_meta() creates virtual screen beyond legal size.";
429     char * err_m = "init_win_meta() triggers memory alloc error via newpad().";
430     world.wmeta.screen = initscr();
431     set_cleanup_flag(CLEANUP_NCURSES);
432     noecho();
433     curs_set(0);
434     uint32_t maxy_test  = getmaxy(world.wmeta.screen);
435     uint32_t maxx_test  = getmaxx(world.wmeta.screen);
436     exit_err(maxy_test > UINT16_MAX || maxx_test > UINT16_MAX, err_s);
437     world.wmeta.padsize.y   = maxy_test;
438     world.wmeta.padsize.x   = maxx_test;
439     world.wmeta.chain_start = 0;
440     world.wmeta.chain_end   = 0;
441     world.wmeta.pad_offset  = 0;
442     world.wmeta.pad         = newpad(world.wmeta.padsize.y, 1);
443     exit_err(NULL == world.wmeta.pad, err_m);
444     world.wmeta.active      = 0;
445 }
446
447
448
449 extern void init_win(struct Win ** wp, char * title, int16_t height,
450                      int16_t width, void * func)
451 {
452     char * f_name = "init_win()";
453     struct Win * w  = try_malloc(sizeof(struct Win), f_name);
454     w->prev         = 0;
455     w->next         = 0;
456     w->winmapsize.y = 0;
457     w->winmapsize.x = 0;
458     w->winmap       = NULL;
459     w->title        = try_malloc(strlen(title) + 1, f_name);
460     sprintf(w->title, "%s", title);
461     w->draw         = func;
462     w->center.y     = 0;
463     w->center.x     = 0;
464     if      (0 < width)
465     {
466         w->framesize.x = width;
467     }
468     else if (0 >= width)
469     {
470         w->framesize.x = world.wmeta.padsize.x + width;
471     }
472     if      (0 < height && height <= world.wmeta.padsize.y - 1)
473     {
474         w->framesize.y = height;
475     }
476     else if (0 >= height && world.wmeta.padsize.y + (height - 1) > 0)
477     {
478         w->framesize.y = world.wmeta.padsize.y + (height - 1);
479     }
480     *wp = w;
481 }
482
483
484
485 extern void free_winmeta_and_endwin()
486 {
487     delwin(world.wmeta.pad);
488     endwin();
489 }
490
491
492
493 extern void free_win(struct Win * win)
494 {
495     free(win->title);
496     free(win);
497 }
498
499
500
501 extern void append_win(struct Win * w)
502 {
503     if (0 != world.wmeta.chain_start)
504     {
505         w->prev = world.wmeta.chain_end;
506         world.wmeta.chain_end->next = w;
507     }
508     else
509     {
510         world.wmeta.active = w;
511         world.wmeta.chain_start = w;
512     }
513     world.wmeta.chain_end = w;
514     update_wins(w);
515 }
516
517
518
519 extern void suspend_win(struct Win * w)
520 {
521     if (world.wmeta.chain_start != w)
522     {
523         w->prev->next = w->next;
524     }
525     else
526     {
527         world.wmeta.chain_start = w->next;
528     }
529     uint8_t pad_refitted = 0;
530     if (world.wmeta.chain_end != w)
531     {
532         w->next->prev = w->prev;
533         if (world.wmeta.active == w)
534         {
535             world.wmeta.active = w->next;
536         }
537         update_wins(w->next);      /* Positioning of successor windows may be */
538         pad_refitted = 1;          /* affected / need correction. Note that   */
539     }                              /* update_wins() already refits the pad,   */
540     else                           /* voiding later need for that.            */
541     {
542         world.wmeta.chain_end = w->prev;
543         if (world.wmeta.active == w)
544         {
545             world.wmeta.active = w->prev;
546         }
547     }
548     w->prev = 0;
549     w->next = 0;
550     if (0 == pad_refitted)
551     {
552         refit_pad();
553     }
554 }
555
556
557
558 extern void reset_pad_offset(uint16_t new_offset)
559 {
560     if (new_offset >= 0
561         && (new_offset < world.wmeta.pad_offset
562             || new_offset + world.wmeta.padsize.x < getmaxx(world.wmeta.pad)))
563     {
564         world.wmeta.pad_offset = new_offset;
565     }
566 }
567
568
569
570 extern void resize_active_win(struct yx_uint16 size)
571 {
572     if (0 != world.wmeta.active
573         && size.x > 0 && size.y > 0 && size.y < world.wmeta.padsize.y)
574     {
575         world.wmeta.active->framesize = size;
576         update_wins(world.wmeta.active);          /* Positioning of following */
577     }                                             /* windows may be affected. */
578 }
579
580
581
582 extern void cycle_active_win(char dir)
583 {
584     if (0 != world.wmeta.active)
585     {
586         if ('f' == dir)
587         {
588             if (world.wmeta.active->next != 0)
589             {
590                 world.wmeta.active = world.wmeta.active->next;
591             }
592             else
593             {
594                 world.wmeta.active = world.wmeta.chain_start;
595             }
596         }
597         else
598         {
599             if (world.wmeta.active->prev != 0)
600             {
601                 world.wmeta.active = world.wmeta.active->prev;
602             }
603             else
604             {
605                 world.wmeta.active = world.wmeta.chain_end;
606             }
607         }
608     }
609 }
610
611
612
613 extern void shift_active_win(char dir)
614 {
615     if (   0 == world.wmeta.active  /* No shifting with < 2 windows visible. */
616         || world.wmeta.chain_start == world.wmeta.chain_end)
617     {
618         return;
619     }
620     if ('f' == dir)
621     {
622         shift_win_forward();
623         update_wins(world.wmeta.chain_start);
624         return;
625     }
626     shift_win_backward();
627     update_wins(world.wmeta.chain_start);
628 }
629
630
631
632 extern void draw_all_wins()
633 {
634     /* Empty everything before filling it a-new. */
635     erase();
636     wnoutrefresh(world.wmeta.screen);
637     werase(world.wmeta.pad);
638     if (world.wmeta.chain_start)
639     {
640
641         /* Draw windows' borders first, then windows. */
642         draw_wins_borderlines(world.wmeta.chain_start);
643         draw_wins_bordercorners(world.wmeta.chain_start);
644         draw_wins(world.wmeta.chain_start);
645
646         /* Draw virtual screen scroll hints. */
647         if (world.wmeta.pad_offset > 0)
648         {
649             padscroll_hint('<', world.wmeta.pad_offset + 1);
650         }
651         uint16_t size_x = getmaxx(world.wmeta.pad);
652         uint16_t right_edge = world.wmeta.pad_offset + world.wmeta.padsize.x;
653         if (right_edge < size_x - 1)
654         {
655             padscroll_hint('>', size_x - right_edge);
656         }
657
658         /* Write pad segment to be shown on physical screen to screen buffer. */
659         pnoutrefresh(world.wmeta.pad, 0, world.wmeta.pad_offset, 0, 0,
660                      world.wmeta.padsize.y, world.wmeta.padsize.x - 1);
661     }
662
663     /* Only at the end write accumulated changes to the physical screen. */
664     doupdate();
665 }