4 #include <stdint.h> /* for uint8_t, uint16_t, uint32_t, UINT16_MAX */
5 #include <ncurses.h> /* for typedefs WINDOW, chtype, wresize(), getmaxx(), */
6 /* getmaxy(), delwin(), mvwaddch(), mvwaddstr(), */
7 /* newpad(), wnoutrefres(), erase(), werase(), */
8 /* pnoutrefresh(), doupdate(), getmaxyx() */
9 #include <stdlib.h> /* for malloc(), 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() */
18 /* Fit virtual screen's width to minimum width demanded by current windows'
21 static void refit_pad();
25 /* Update geometry (sizes, positions) of window "w" and its successors in the
26 * window chain. For the positioning algorithm place_win() is used.
28 static void update_wins(struct Win * w);
29 static void place_win(struct Win * w);
33 /* Draw scroll hint (a line saying that there are "dist" more elements of
34 * "unit" further into the direction symbolized by the "dir" char) into virtual
35 * screen pad, onto an appropriate edge of either a window or the screen; the
36 * left or right edge if "dir" is "<" or ">", or the upper or lower edge if it
37 * is "^" or "v". "start" should be either the start coordinate of a window's
38 * frame or .y=0, .x=wm->pad_offset if it describes the virtual screen pad.
39 * winscroll_hint() and padscroll_hint() are wrappers to simplify these uses.
41 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
42 char * unit, struct yx_uint16 start);
43 static void winscroll_hint(struct Win * w, char dir, uint16_t dist);
44 static void padscroll_hint(char dir, uint16_t dist);
48 /* Draw contents of all windows in window chain from window "w" onwards. */
49 static void draw_wins(struct Win * w);
53 /* draw_win_borderlines() draws the vertical and horizontal borders of window
54 * "w" sans corners into the virtual screen "pad", and draws the top border
55 * line as the windows' title bar (highlighted if the window is described
56 * active by "active" being == 1).
58 * draw_wins_borderlines() calls draw_win_borderlines() recursively on all
59 * windows from "w" on. "w_active" is a pointer to the one window that
60 * draw_win_borderlines() is supposed to handle as the active window.
62 * Finally, draw_wins_bordercorners() draws into "pad" the borders of window "w"
63 * and all its successors.
65 static void draw_win_borderlines(struct Win * w, char active, WINDOW * pad);
66 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
68 static void draw_wins_bordercorners(struct Win * w, WINDOW * pad);
72 /* Shift active window forwards / backwards in window chain. */
73 static void shift_win_forward();
74 static void shift_win_backward();
78 static void refit_pad()
80 /* Determine rightmost window column. */
81 uint32_t lastwcol = 0;
82 struct Win * wp = world.wmeta->chain_start;
85 if ((uint32_t) wp->start.x + (uint32_t) wp->framesize.x > lastwcol + 1)
87 lastwcol = (uint32_t) wp->start.x + (uint32_t) wp->framesize.x - 1;
92 /* Only resize the pad if the rightmost window column has changed. */
93 char * err_s = "refit_pad() extends virtual screen beyond legal sizes.";
94 char * err_m = "refit_pad() triggers memory alloc error via wresize().";
95 if (getmaxx(world.wmeta->pad) + 1 != lastwcol)
97 uint8_t t = (lastwcol + 2 > UINT16_MAX);
99 t = wresize(world.wmeta->pad, getmaxy(world.wmeta->pad), lastwcol + 2);
106 static void update_wins(struct Win * w)
112 update_wins(w->next);
118 static void place_win(struct Win * w)
120 /* First window goes into the upper-left corner. */
122 w->start.y = 1; /* Leave space for title bar. */
126 /* Non-first window fallbacks to: fit rightwards of rightmost border. */
127 struct Win * w_top = w->prev;
128 while (w_top->start.y != 1)
132 w->start.x = w_top->start.x + w_top->framesize.x + 1;
134 /* Fit window below its predecessor if that one directly thrones over
135 * empty space wide and high enough.
137 uint16_t w_prev_maxy = w->prev->start.y + w->prev->framesize.y;
138 if ( w->framesize.x <= w->prev->framesize.x
139 && w->framesize.y < world.wmeta->padsize.y - w_prev_maxy)
141 w->start.x = w->prev->start.x;
142 w->start.y = w_prev_maxy + 1;
145 /* Failing that, try to open a new sub column below the nearest
146 * predecessor window that thrones over enough empty space.
150 struct Win * w_up = w->prev;
151 struct Win * w_upup = w_up;
153 while (w_up != w_top)
158 if (w_up->start.y != w_upup->start.y)
162 w_upup = w_upup->prev;
164 w_prev_maxy = w_upup->start.y + w_upup->framesize.y;
165 widthdiff = (w_upup->start.x + w_upup->framesize.x)
166 - (w_up->start.x + w_up->framesize.x);
167 if ( w->framesize.y < world.wmeta->padsize.y - w_prev_maxy
168 && w->framesize.x < widthdiff)
170 w->start.x = w_up->start.x + w_up->framesize.x + 1 ;
171 w->start.y = w_prev_maxy + 1;
182 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
183 char * unit, struct yx_uint16 start)
185 /* Decide on alignment (vertical/horizontal?), thereby hint text space. */
186 char * more = "more";
187 uint16_t dsc_space = fsize.x;
188 if ('<' == dir || '>' == dir)
191 } /* vv-- 10 = max strlen for uint16_t */
192 char scrolldsc[1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1];
193 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
195 /* Decide on offset of the description text inside the scroll hint line. */
196 uint16_t dsc_offset = 1;
197 if (dsc_space > strlen(scrolldsc) + 1)
199 dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
202 /* Draw scroll hint line as dir symbols bracketing description text. */
203 uint16_t draw_offset = 0;
206 draw_offset = fsize.x - 1;
210 draw_offset = fsize.y - 1;
213 for (; q < dsc_space; q++)
215 chtype c = dir | A_REVERSE;
216 if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
218 c = scrolldsc[q - dsc_offset] | A_REVERSE;
220 if ('<' == dir || '>' == dir)
222 mvwaddch(world.wmeta->pad, start.y + q, start.x + draw_offset, c);
226 mvwaddch(world.wmeta->pad, start.y + draw_offset, start.x + q, c);
232 static void padscroll_hint(char dir, uint16_t dist)
234 struct yx_uint16 start;
236 start.x = world.wmeta->pad_offset;
237 scroll_hint(world.wmeta->padsize, dir, dist, "columns", start);
242 static void winscroll_hint(struct Win * w, char dir, uint16_t dist)
244 char * unit = "lines";
245 if ('<' == dir || '>' == dir)
249 struct yx_uint16 start = w->start;
250 scroll_hint(w->framesize, dir, dist, unit, start);
255 static void draw_wins(struct Win * w)
258 uint16_t y, x, size_y, size_x;
259 size_y = w->winmapsize.y;
260 size_x = w->winmapsize.x;
261 uint16_t offset_y = center_offset(w->center.y, size_y, w->framesize.y);
262 uint16_t offset_x = center_offset(w->center.x, size_x, w->framesize.x);
263 for (y = offset_y; y < w->framesize.y + offset_y && y < size_y; y++)
265 for (x = offset_x; x < w->framesize.x + offset_x && x < size_x; x++)
267 chtype ch = w->winmap[(y * w->winmapsize.x) + x];
268 mvwaddch(world.wmeta->pad, w->start.y + (y - offset_y),
269 w->start.x + (x - offset_x), ch);
278 winscroll_hint(w, '^', offset_y + 1);
280 if (size_y > offset_y + w->framesize.y)
282 winscroll_hint(w, 'v', size_y - ((offset_y + w->framesize.y) - 1));
286 winscroll_hint(w, '<', offset_x + 1);
288 if (size_x > offset_x + w->framesize.x)
290 winscroll_hint(w, '>', size_x - ((offset_x + w->framesize.x) - 1));
294 return draw_wins(w->next);
300 static void draw_win_borderlines(struct Win * w, char active, WINDOW * pad)
302 /* Draw vertical and horizontal border lines. */
304 for (y = w->start.y; y <= w->start.y + w->framesize.y; y++)
306 mvwaddch(pad, y, w->start.x - 1, '|');
307 mvwaddch(pad, y, w->start.x + w->framesize.x, '|');
309 for (x = w->start.x; x <= w->start.x + w->framesize.x; x++)
311 mvwaddch(pad, w->start.y - 1, x, '-');
312 mvwaddch(pad, w->start.y + w->framesize.y, x, '-');
315 /* Draw as much as possible of the title into center of top border line. */
316 char min_title_length_visible = 3; /* min. 1 char + 2 padding/decoration */
317 if (w->framesize.x >= min_title_length_visible)
319 uint16_t title_offset = 0;
320 if (w->framesize.x > strlen(w->title) + 2)
322 title_offset = (w->framesize.x - (strlen(w->title) + 2)) / 2;
323 } /* +2 is for padding/decoration */
324 uint16_t length_visible = strnlen(w->title, w->framesize.x - 2);
325 char title[length_visible + 3];
326 char decoration = ' ';
331 memcpy(title + 1, w->title, length_visible);
332 title[0] = title[length_visible + 1] = decoration;
333 title[length_visible + 2] = '\0';
334 mvwaddstr(pad, w->start.y - 1, w->start.x + title_offset, title);
340 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
348 draw_win_borderlines(w, active, pad);
351 draw_wins_borderlines(w->next, w_active, pad);
357 static void draw_wins_bordercorners(struct Win * w, WINDOW * pad)
359 mvwaddch(pad, w->start.y - 1, w->start.x - 1, '+');
360 mvwaddch(pad, w->start.y - 1, w->start.x + w->framesize.x, '+');
361 mvwaddch(pad, w->start.y + w->framesize.y, w->start.x - 1, '+');
362 mvwaddch(pad, w->start.y + w->framesize.y, w->start.x + w->framesize.x,'+');
365 draw_wins_bordercorners(w->next, pad);
371 static void shift_win_forward()
373 if (world.wmeta->active == world.wmeta->chain_end)
375 world.wmeta->chain_end = world.wmeta->active->prev;
376 world.wmeta->chain_end->next = 0;
377 world.wmeta->active->next = world.wmeta->chain_start;
378 world.wmeta->active->next->prev = world.wmeta->active;
379 world.wmeta->chain_start = world.wmeta->active;
380 world.wmeta->chain_start->prev = 0;
384 struct Win * old_prev = world.wmeta->active->prev;
385 struct Win * old_next = world.wmeta->active->next;
386 if (world.wmeta->chain_end == world.wmeta->active->next)
388 world.wmeta->chain_end = world.wmeta->active;
389 world.wmeta->active->next = 0;
393 world.wmeta->active->next = old_next->next;
394 world.wmeta->active->next->prev = world.wmeta->active;
396 if (world.wmeta->chain_start == world.wmeta->active)
398 world.wmeta->chain_start = old_next;
402 old_prev->next = old_next;
404 old_next->prev = old_prev;
405 old_next->next = world.wmeta->active;
406 world.wmeta->active->prev = old_next;
412 static void shift_win_backward()
414 if (world.wmeta->active == world.wmeta->chain_start)
416 world.wmeta->chain_start = world.wmeta->active->next;
417 world.wmeta->chain_start->prev = 0;
418 world.wmeta->active->prev = world.wmeta->chain_end;
419 world.wmeta->active->prev->next = world.wmeta->active;
420 world.wmeta->chain_end = world.wmeta->active;
421 world.wmeta->chain_end->next = 0;
425 struct Win * old_prev = world.wmeta->active->prev;
426 struct Win * old_next = world.wmeta->active->next;
427 if (world.wmeta->chain_start == world.wmeta->active->prev)
429 world.wmeta->chain_start = world.wmeta->active;
430 world.wmeta->active->prev = 0;
434 world.wmeta->active->prev = old_prev->prev;
435 world.wmeta->active->prev->next = world.wmeta->active;
437 if (world.wmeta->chain_end == world.wmeta->active)
439 world.wmeta->chain_end = old_prev;
443 old_next->prev = old_prev;
445 old_prev->next = old_next;
446 old_prev->prev = world.wmeta->active;
447 world.wmeta->active->next = old_prev;
453 extern void init_win_meta(WINDOW * screen)
455 char * f_name = "init_win_meta()";
456 char * err_s = "init_win_meta() creates virtual screen beyond legal size.";
457 char * err_m = "init_win_meta() triggers memory alloc error via newpad().";
458 world.wmeta = try_malloc(sizeof(struct WinMeta), f_name);
459 world.wmeta->screen = screen;
460 uint32_t maxy_test = getmaxy(screen);
461 uint32_t maxx_test = getmaxx(screen);
462 uint8_t test = (maxy_test > UINT16_MAX || maxx_test > UINT16_MAX);
463 exit_err(test, err_s);
464 world.wmeta->padsize.y = maxy_test;
465 world.wmeta->padsize.x = maxx_test;
466 world.wmeta->chain_start = 0;
467 world.wmeta->chain_end = 0;
468 world.wmeta->pad_offset = 0;
469 world.wmeta->pad = newpad(world.wmeta->padsize.y, 1);
470 exit_err(NULL == world.wmeta->pad, err_m);
471 world.wmeta->active = 0;
476 extern void init_win(struct Win ** wp, char * title, int16_t height,
477 int16_t width, void * func)
479 char * f_name = "init_win()";
480 struct Win * w = try_malloc(sizeof(struct Win), f_name);
486 w->title = try_malloc(strlen(title) + 1, f_name);
487 sprintf(w->title, "%s", title);
493 w->framesize.x = width;
497 w->framesize.x = world.wmeta->padsize.x + width;
499 if (0 < height && height <= world.wmeta->padsize.y - 1)
501 w->framesize.y = height;
503 else if (0 >= height && world.wmeta->padsize.y + (height - 1) > 0)
505 w->framesize.y = world.wmeta->padsize.y + (height - 1);
512 extern void free_winmeta()
514 delwin(world.wmeta->pad);
520 extern void free_win(struct Win * win)
528 extern void append_win(struct Win * w)
530 if (0 != world.wmeta->chain_start)
532 w->prev = world.wmeta->chain_end;
533 world.wmeta->chain_end->next = w;
537 world.wmeta->active = w;
538 world.wmeta->chain_start = w;
540 world.wmeta->chain_end = w;
546 extern void suspend_win(struct Win * w)
548 if (world.wmeta->chain_start != w)
550 w->prev->next = w->next;
554 world.wmeta->chain_start = w->next;
556 char pad_refitted = 0;
557 if (world.wmeta->chain_end != w)
559 w->next->prev = w->prev;
560 if (world.wmeta->active == w)
562 world.wmeta->active = w->next;
564 update_wins(w->next); /* Positioning of successor windows may be */
565 pad_refitted = 1; /* affected / need correction. Note that */
566 } /* update_wins() already refits the pad, */
567 else /* voiding later need for that. */
569 world.wmeta->chain_end = w->prev;
570 if (world.wmeta->active == w)
572 world.wmeta->active = w->prev;
579 if (0 == pad_refitted)
587 extern void reset_pad_offset(uint16_t new_offset)
590 && (new_offset < world.wmeta->pad_offset
591 || new_offset + world.wmeta->padsize.x < getmaxx(world.wmeta->pad)))
593 world.wmeta->pad_offset = new_offset;
599 extern void resize_active_win(struct yx_uint16 size)
601 if (0 != world.wmeta->active
602 && size.x > 0 && size.y > 0 && size.y < world.wmeta->padsize.y)
604 world.wmeta->active->framesize = size;
605 update_wins(world.wmeta->active); /* Positioning of following */
606 } /* windows may be affected. */
611 extern void cycle_active_win(char dir)
613 if (0 != world.wmeta->active)
617 if (world.wmeta->active->next != 0)
619 world.wmeta->active = world.wmeta->active->next;
623 world.wmeta->active = world.wmeta->chain_start;
628 if (world.wmeta->active->prev != 0)
630 world.wmeta->active = world.wmeta->active->prev;
634 world.wmeta->active = world.wmeta->chain_end;
642 extern void shift_active_win(char dir)
644 if ( 0 == world.wmeta->active /* No shifting with < 2 windows visible. */
645 || world.wmeta->chain_start == world.wmeta->chain_end)
655 shift_win_backward();
657 update_wins(world.wmeta->chain_start);
662 extern void draw_all_wins()
664 /* Empty everything before filling it a-new. */
666 wnoutrefresh(world.wmeta->screen);
667 werase(world.wmeta->pad);
668 if (world.wmeta->chain_start)
671 /* Draw windows' borders first, then windows. */
672 draw_wins_borderlines(world.wmeta->chain_start, world.wmeta->active,
674 draw_wins_bordercorners(world.wmeta->chain_start, world.wmeta->pad);
675 draw_wins(world.wmeta->chain_start);
677 /* Draw virtual screen scroll hints. */
678 if (world.wmeta->pad_offset > 0)
680 padscroll_hint('<', world.wmeta->pad_offset + 1);
682 uint16_t size_x = getmaxx(world.wmeta->pad);
683 uint16_t right_edge = world.wmeta->pad_offset + world.wmeta->padsize.x;
684 if (right_edge < size_x - 1)
686 padscroll_hint('>', size_x - right_edge);
689 /* Write pad segment to be shown on physical screen to screen buffer. */
690 pnoutrefresh(world.wmeta->pad, 0, world.wmeta->pad_offset, 0, 0,
691 world.wmeta->padsize.y, world.wmeta->padsize.x - 1);
694 /* Only at the end write accumulated changes to the physical screen. */