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