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