home · contact · privacy
Client: Changed window sizing algorithm to fix bug of too large negative window
[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     w->framesize.y  = world.wmeta.padsize.y - 1;
465     if      (0 < height && height <= world.wmeta.padsize.y - 1)
466     {
467         w->framesize.y = height;
468     }
469     else if (0 > height && world.wmeta.padsize.y + (height - 1) > 0)
470     {
471         w->framesize.y = world.wmeta.padsize.y + (height - 1);
472     }
473     w->framesize.x  = world.wmeta.padsize.x;
474     if      (0 < width)
475     {
476         w->framesize.x = width;
477     }
478     else if (0 > width && world.wmeta.padsize.x + width > 0)
479     {
480         w->framesize.x = world.wmeta.padsize.x + width;
481     }
482     *wp = w;
483 }
484
485
486
487 extern void free_winmeta_and_endwin()
488 {
489     delwin(world.wmeta.pad);
490     endwin();
491 }
492
493
494
495 extern void free_win(struct Win * win)
496 {
497     free(win->title);
498     free(win);
499 }
500
501
502
503 extern void append_win(struct Win * w)
504 {
505     if (0 != world.wmeta.chain_start)
506     {
507         w->prev = world.wmeta.chain_end;
508         world.wmeta.chain_end->next = w;
509     }
510     else
511     {
512         world.wmeta.active = w;
513         world.wmeta.chain_start = w;
514     }
515     world.wmeta.chain_end = w;
516     update_wins(w);
517 }
518
519
520
521 extern void suspend_win(struct Win * w)
522 {
523     if (world.wmeta.chain_start != w)
524     {
525         w->prev->next = w->next;
526     }
527     else
528     {
529         world.wmeta.chain_start = w->next;
530     }
531     uint8_t pad_refitted = 0;
532     if (world.wmeta.chain_end != w)
533     {
534         w->next->prev = w->prev;
535         if (world.wmeta.active == w)
536         {
537             world.wmeta.active = w->next;
538         }
539         update_wins(w->next);      /* Positioning of successor windows may be */
540         pad_refitted = 1;          /* affected / need correction. Note that   */
541     }                              /* update_wins() already refits the pad,   */
542     else                           /* voiding later need for that.            */
543     {
544         world.wmeta.chain_end = w->prev;
545         if (world.wmeta.active == w)
546         {
547             world.wmeta.active = w->prev;
548         }
549     }
550     w->prev = 0;
551     w->next = 0;
552     if (0 == pad_refitted)
553     {
554         refit_pad();
555     }
556 }
557
558
559
560 extern void reset_pad_offset(uint16_t new_offset)
561 {
562     if (new_offset >= 0
563         && (new_offset < world.wmeta.pad_offset
564             || new_offset + world.wmeta.padsize.x < getmaxx(world.wmeta.pad)))
565     {
566         world.wmeta.pad_offset = new_offset;
567     }
568 }
569
570
571
572 extern void resize_active_win(struct yx_uint16 size)
573 {
574     if (0 != world.wmeta.active
575         && size.x > 0 && size.y > 0 && size.y < world.wmeta.padsize.y)
576     {
577         world.wmeta.active->framesize = size;
578         update_wins(world.wmeta.active);          /* Positioning of following */
579     }                                             /* windows may be affected. */
580 }
581
582
583
584 extern void cycle_active_win(char dir)
585 {
586     if (0 != world.wmeta.active)
587     {
588         if ('f' == dir)
589         {
590             if (world.wmeta.active->next != 0)
591             {
592                 world.wmeta.active = world.wmeta.active->next;
593             }
594             else
595             {
596                 world.wmeta.active = world.wmeta.chain_start;
597             }
598         }
599         else
600         {
601             if (world.wmeta.active->prev != 0)
602             {
603                 world.wmeta.active = world.wmeta.active->prev;
604             }
605             else
606             {
607                 world.wmeta.active = world.wmeta.chain_end;
608             }
609         }
610     }
611 }
612
613
614
615 extern void shift_active_win(char dir)
616 {
617     if (   0 == world.wmeta.active  /* No shifting with < 2 windows visible. */
618         || world.wmeta.chain_start == world.wmeta.chain_end)
619     {
620         return;
621     }
622     if ('f' == dir)
623     {
624         shift_win_forward();
625         update_wins(world.wmeta.chain_start);
626         return;
627     }
628     shift_win_backward();
629     update_wins(world.wmeta.chain_start);
630 }
631
632
633
634 extern void draw_all_wins()
635 {
636     /* Empty everything before filling it a-new. */
637     erase();
638     wnoutrefresh(world.wmeta.screen);
639     werase(world.wmeta.pad);
640     if (world.wmeta.chain_start)
641     {
642
643         /* Draw windows' borders first, then windows. */
644         draw_wins_borderlines(world.wmeta.chain_start);
645         draw_wins_bordercorners(world.wmeta.chain_start);
646         draw_wins(world.wmeta.chain_start);
647
648         /* Draw virtual screen scroll hints. */
649         if (world.wmeta.pad_offset > 0)
650         {
651             padscroll_hint('<', world.wmeta.pad_offset + 1);
652         }
653         uint16_t size_x = getmaxx(world.wmeta.pad);
654         uint16_t right_edge = world.wmeta.pad_offset + world.wmeta.padsize.x;
655         if (right_edge < size_x - 1)
656         {
657             padscroll_hint('>', size_x - right_edge);
658         }
659
660         /* Write pad segment to be shown on physical screen to screen buffer. */
661         pnoutrefresh(world.wmeta.pad, 0, world.wmeta.pad_offset, 0, 0,
662                      world.wmeta.padsize.y, world.wmeta.padsize.x - 1);
663     }
664
665     /* Only at the end write accumulated changes to the physical screen. */
666     doupdate();
667 }