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();
23 /* Update geometry (sizes, positions) of window "w" and its successors in the
24 * window chain. For the positioning algorithm place_win() is used.
26 static void update_wins(struct Win * w);
27 static void place_win(struct Win * w);
29 /* Draw scroll hint (a line saying that there are "dist" more elements of
30 * "unit" further into the direction symbolized by the "dir" char) into virtual
31 * screen pad, onto an appropriate edge of either a window or the screen; the
32 * left or right edge if "dir" is "<" or ">", or the upper or lower edge if it
33 * is "^" or "v". "start" should be either the start coordinate of a window's
34 * frame or .y=0, .x=wm->pad_offset if it describes the virtual screen pad.
35 * winscroll_hint() and padscroll_hint() are wrappers to simplify these uses.
37 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
38 char * unit, struct yx_uint16 start);
39 static void winscroll_hint(struct Win * w, char dir, uint16_t dist);
40 static void padscroll_hint(char dir, uint16_t dist);
42 /* Draw contents of all windows in window chain from window "w" onwards. */
43 static void draw_wins(struct Win * w);
45 /* draw_win_borderlines() draws the vertical and horizontal borders of window
46 * "w" sans corners into the virtual screen "pad", and draws the top border
47 * line as the windows' title bar (highlighted if the window is described
48 * active by "active" being == 1).
50 * draw_wins_borderlines() calls draw_win_borderlines() recursively on all
51 * windows from "w" on. "w_active" is a pointer to the one window that
52 * draw_win_borderlines() is supposed to handle as the active window.
54 * Finally, draw_wins_bordercorners() draws into "pad" the borders of window "w"
55 * and all its successors.
57 static void draw_win_borderlines(struct Win * w, char active, WINDOW * pad);
58 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
60 static void draw_wins_bordercorners(struct Win * w, WINDOW * pad);
62 /* Shift active window forwards / backwards in window chain. */
63 static void shift_win_forward();
64 static void shift_win_backward();
68 static void refit_pad()
70 /* Determine rightmost window column. */
71 uint32_t lastwcol = 0;
72 struct Win * wp = world.wmeta->chain_start;
75 if ((uint32_t) wp->start.x + (uint32_t) wp->framesize.x > lastwcol + 1)
77 lastwcol = (uint32_t) wp->start.x + (uint32_t) wp->framesize.x - 1;
82 /* Only resize the pad if the rightmost window column has changed. */
83 char * err_s = "refit_pad() extends virtual screen beyond legal sizes.";
84 char * err_m = "refit_pad() triggers memory alloc error via wresize().";
85 if (getmaxx(world.wmeta->pad) + 1 != lastwcol)
87 uint8_t t = (lastwcol + 2 > UINT16_MAX);
89 t = wresize(world.wmeta->pad, getmaxy(world.wmeta->pad), lastwcol + 2);
96 static void update_wins(struct Win * w)
102 update_wins(w->next);
108 static void place_win(struct Win * w)
110 /* If w is first window, it goes into the upper-left corner. */
112 w->start.y = 1; /* Leave space for title bar. */
116 /* If w is not first winddow, fit its top left corner to the top right
117 * corner of w_top, the last predecessor window starting at the top.
119 struct Win * w_top = w->prev;
120 while (w_top->start.y != 1)
124 w->start.x = w_top->start.x + w_top->framesize.x + 1;
126 /* Fit w's top left corner to bottom left corner of its immediate
127 * predecessor window if enough empty space is found there.
129 uint16_t w_prev_maxy = w->prev->start.y + w->prev->framesize.y;
130 if ( w->framesize.x <= w->prev->framesize.x
131 && w->framesize.y < world.wmeta->padsize.y - w_prev_maxy)
133 w->start.x = w->prev->start.x;
134 w->start.y = w_prev_maxy + 1;
137 /* Failing that, try to fit the top left corner to the top right corner
138 * of the last predecessor w_test 1) not followed by top windows or any
139 * other windows with a left corner further rightwards than its own left
140 * corner 2) with enough space rightwards for w until the bottom right
141 * corner of w_thro directly throning over it 3) and with this same
142 * space extending for enough to the bottom for fitting in w.
146 struct Win * w_test = w->prev;
148 while (w_test != w_top)
150 w_thro = w_test->prev;
151 while (w_test->start.y <= w_thro->start.y)
153 w_thro = w_thro->prev;
155 uint16_t w_thro_bottom = w_thro->start.y + w_thro->framesize.y;
156 uint16_t free_width = (w_thro->start.x + w_thro->framesize.x)
157 - (w_test->start.x + w_test->framesize.x);
158 if ( w->framesize.y < world.wmeta->padsize.y - w_thro_bottom
159 && w->framesize.x < free_width)
161 w->start.x = w_test->start.x + w_test->framesize.x + 1;
162 w->start.y = w_thro_bottom + 1;
173 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
174 char * unit, struct yx_uint16 start)
176 /* Decide on alignment (vertical/horizontal?), thereby hint text space. */
177 char * more = "more";
178 uint16_t dsc_space = fsize.x;
179 if ('<' == dir || '>' == dir)
182 } /* vv-- 10 = max strlen for uint16_t */
183 char scrolldsc[1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1];
184 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
186 /* Decide on offset of the description text inside the scroll hint line. */
187 uint16_t dsc_offset = 1;
188 if (dsc_space > strlen(scrolldsc) + 1)
190 dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
193 /* Draw scroll hint line as dir symbols bracketing description text. */
194 uint16_t draw_offset = 0;
197 draw_offset = fsize.x - 1;
201 draw_offset = fsize.y - 1;
204 for (; q < dsc_space; q++)
206 chtype c = dir | A_REVERSE;
207 if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
209 c = scrolldsc[q - dsc_offset] | A_REVERSE;
211 if ('<' == dir || '>' == dir)
213 mvwaddch(world.wmeta->pad, start.y + q, start.x + draw_offset, c);
217 mvwaddch(world.wmeta->pad, start.y + draw_offset, start.x + q, c);
223 static void padscroll_hint(char dir, uint16_t dist)
225 struct yx_uint16 start;
227 start.x = world.wmeta->pad_offset;
228 scroll_hint(world.wmeta->padsize, dir, dist, "columns", start);
233 static void winscroll_hint(struct Win * w, char dir, uint16_t dist)
235 char * unit = "lines";
236 if ('<' == dir || '>' == dir)
240 struct yx_uint16 start = w->start;
241 scroll_hint(w->framesize, dir, dist, unit, start);
246 static void draw_wins(struct Win * w)
249 uint16_t y, x, size_y, size_x;
250 size_y = w->winmapsize.y;
251 size_x = w->winmapsize.x;
252 uint16_t offset_y = center_offset(w->center.y, size_y, w->framesize.y);
253 uint16_t offset_x = center_offset(w->center.x, size_x, w->framesize.x);
254 for (y = offset_y; y < w->framesize.y + offset_y && y < size_y; y++)
256 for (x = offset_x; x < w->framesize.x + offset_x && x < size_x; x++)
258 chtype ch = w->winmap[(y * w->winmapsize.x) + x];
259 mvwaddch(world.wmeta->pad, w->start.y + (y - offset_y),
260 w->start.x + (x - offset_x), ch);
269 winscroll_hint(w, '^', offset_y + 1);
271 if (size_y > offset_y + w->framesize.y)
273 winscroll_hint(w, 'v', size_y - ((offset_y + w->framesize.y) - 1));
277 winscroll_hint(w, '<', offset_x + 1);
279 if (size_x > offset_x + w->framesize.x)
281 winscroll_hint(w, '>', size_x - ((offset_x + w->framesize.x) - 1));
285 return draw_wins(w->next);
291 static void draw_win_borderlines(struct Win * w, char active, WINDOW * pad)
293 /* Draw vertical and horizontal border lines. */
295 for (y = w->start.y; y <= w->start.y + w->framesize.y; y++)
297 mvwaddch(pad, y, w->start.x - 1, '|');
298 mvwaddch(pad, y, w->start.x + w->framesize.x, '|');
300 for (x = w->start.x; x <= w->start.x + w->framesize.x; x++)
302 mvwaddch(pad, w->start.y - 1, x, '-');
303 mvwaddch(pad, w->start.y + w->framesize.y, x, '-');
306 /* Draw as much as possible of the title into center of top border line. */
307 char min_title_length_visible = 3; /* min. 1 char + 2 padding/decoration */
308 if (w->framesize.x >= min_title_length_visible)
310 uint16_t title_offset = 0;
311 if (w->framesize.x > strlen(w->title) + 2)
313 title_offset = (w->framesize.x - (strlen(w->title) + 2)) / 2;
314 } /* +2 is for padding/decoration */
315 uint16_t length_visible = strnlen(w->title, w->framesize.x - 2);
316 char title[length_visible + 3];
317 char decoration = ' ';
322 memcpy(title + 1, w->title, length_visible);
323 title[0] = title[length_visible + 1] = decoration;
324 title[length_visible + 2] = '\0';
325 mvwaddstr(pad, w->start.y - 1, w->start.x + title_offset, title);
331 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
339 draw_win_borderlines(w, active, pad);
342 draw_wins_borderlines(w->next, w_active, pad);
348 static void draw_wins_bordercorners(struct Win * w, WINDOW * pad)
350 mvwaddch(pad, w->start.y - 1, w->start.x - 1, '+');
351 mvwaddch(pad, w->start.y - 1, w->start.x + w->framesize.x, '+');
352 mvwaddch(pad, w->start.y + w->framesize.y, w->start.x - 1, '+');
353 mvwaddch(pad, w->start.y + w->framesize.y, w->start.x + w->framesize.x,'+');
356 draw_wins_bordercorners(w->next, pad);
362 static void shift_win_forward()
364 if (world.wmeta->active == world.wmeta->chain_end)
366 world.wmeta->chain_end = world.wmeta->active->prev;
367 world.wmeta->chain_end->next = 0;
368 world.wmeta->active->next = world.wmeta->chain_start;
369 world.wmeta->active->next->prev = world.wmeta->active;
370 world.wmeta->chain_start = world.wmeta->active;
371 world.wmeta->chain_start->prev = 0;
375 struct Win * old_prev = world.wmeta->active->prev;
376 struct Win * old_next = world.wmeta->active->next;
377 if (world.wmeta->chain_end == world.wmeta->active->next)
379 world.wmeta->chain_end = world.wmeta->active;
380 world.wmeta->active->next = 0;
384 world.wmeta->active->next = old_next->next;
385 world.wmeta->active->next->prev = world.wmeta->active;
387 if (world.wmeta->chain_start == world.wmeta->active)
389 world.wmeta->chain_start = old_next;
393 old_prev->next = old_next;
395 old_next->prev = old_prev;
396 old_next->next = world.wmeta->active;
397 world.wmeta->active->prev = old_next;
403 static void shift_win_backward()
405 if (world.wmeta->active == world.wmeta->chain_start)
407 world.wmeta->chain_start = world.wmeta->active->next;
408 world.wmeta->chain_start->prev = 0;
409 world.wmeta->active->prev = world.wmeta->chain_end;
410 world.wmeta->active->prev->next = world.wmeta->active;
411 world.wmeta->chain_end = world.wmeta->active;
412 world.wmeta->chain_end->next = 0;
416 struct Win * old_prev = world.wmeta->active->prev;
417 struct Win * old_next = world.wmeta->active->next;
418 if (world.wmeta->chain_start == world.wmeta->active->prev)
420 world.wmeta->chain_start = world.wmeta->active;
421 world.wmeta->active->prev = 0;
425 world.wmeta->active->prev = old_prev->prev;
426 world.wmeta->active->prev->next = world.wmeta->active;
428 if (world.wmeta->chain_end == world.wmeta->active)
430 world.wmeta->chain_end = old_prev;
434 old_next->prev = old_prev;
436 old_prev->next = old_next;
437 old_prev->prev = world.wmeta->active;
438 world.wmeta->active->next = old_prev;
444 extern void init_win_meta(WINDOW * screen)
446 char * f_name = "init_win_meta()";
447 char * err_s = "init_win_meta() creates virtual screen beyond legal size.";
448 char * err_m = "init_win_meta() triggers memory alloc error via newpad().";
449 world.wmeta = try_malloc(sizeof(struct WinMeta), f_name);
450 world.wmeta->screen = screen;
451 uint32_t maxy_test = getmaxy(screen);
452 uint32_t maxx_test = getmaxx(screen);
453 uint8_t test = (maxy_test > UINT16_MAX || maxx_test > UINT16_MAX);
454 exit_err(test, err_s);
455 world.wmeta->padsize.y = maxy_test;
456 world.wmeta->padsize.x = maxx_test;
457 world.wmeta->chain_start = 0;
458 world.wmeta->chain_end = 0;
459 world.wmeta->pad_offset = 0;
460 world.wmeta->pad = newpad(world.wmeta->padsize.y, 1);
461 exit_err(NULL == world.wmeta->pad, err_m);
462 world.wmeta->active = 0;
467 extern void init_win(struct Win ** wp, char * title, int16_t height,
468 int16_t width, void * func)
470 char * f_name = "init_win()";
471 struct Win * w = try_malloc(sizeof(struct Win), f_name);
477 w->title = try_malloc(strlen(title) + 1, f_name);
478 sprintf(w->title, "%s", title);
484 w->framesize.x = width;
488 w->framesize.x = world.wmeta->padsize.x + width;
490 if (0 < height && height <= world.wmeta->padsize.y - 1)
492 w->framesize.y = height;
494 else if (0 >= height && world.wmeta->padsize.y + (height - 1) > 0)
496 w->framesize.y = world.wmeta->padsize.y + (height - 1);
503 extern void free_winmeta()
505 delwin(world.wmeta->pad);
511 extern void free_win(struct Win * win)
519 extern void append_win(struct Win * w)
521 if (0 != world.wmeta->chain_start)
523 w->prev = world.wmeta->chain_end;
524 world.wmeta->chain_end->next = w;
528 world.wmeta->active = w;
529 world.wmeta->chain_start = w;
531 world.wmeta->chain_end = w;
537 extern void suspend_win(struct Win * w)
539 if (world.wmeta->chain_start != w)
541 w->prev->next = w->next;
545 world.wmeta->chain_start = w->next;
547 char pad_refitted = 0;
548 if (world.wmeta->chain_end != w)
550 w->next->prev = w->prev;
551 if (world.wmeta->active == w)
553 world.wmeta->active = w->next;
555 update_wins(w->next); /* Positioning of successor windows may be */
556 pad_refitted = 1; /* affected / need correction. Note that */
557 } /* update_wins() already refits the pad, */
558 else /* voiding later need for that. */
560 world.wmeta->chain_end = w->prev;
561 if (world.wmeta->active == w)
563 world.wmeta->active = w->prev;
570 if (0 == pad_refitted)
578 extern void reset_pad_offset(uint16_t new_offset)
581 && (new_offset < world.wmeta->pad_offset
582 || new_offset + world.wmeta->padsize.x < getmaxx(world.wmeta->pad)))
584 world.wmeta->pad_offset = new_offset;
590 extern void resize_active_win(struct yx_uint16 size)
592 if (0 != world.wmeta->active
593 && size.x > 0 && size.y > 0 && size.y < world.wmeta->padsize.y)
595 world.wmeta->active->framesize = size;
596 update_wins(world.wmeta->active); /* Positioning of following */
597 } /* windows may be affected. */
602 extern void cycle_active_win(char dir)
604 if (0 != world.wmeta->active)
608 if (world.wmeta->active->next != 0)
610 world.wmeta->active = world.wmeta->active->next;
614 world.wmeta->active = world.wmeta->chain_start;
619 if (world.wmeta->active->prev != 0)
621 world.wmeta->active = world.wmeta->active->prev;
625 world.wmeta->active = world.wmeta->chain_end;
633 extern void shift_active_win(char dir)
635 if ( 0 == world.wmeta->active /* No shifting with < 2 windows visible. */
636 || world.wmeta->chain_start == world.wmeta->chain_end)
646 shift_win_backward();
648 update_wins(world.wmeta->chain_start);
653 extern void draw_all_wins()
655 /* Empty everything before filling it a-new. */
657 wnoutrefresh(world.wmeta->screen);
658 werase(world.wmeta->pad);
659 if (world.wmeta->chain_start)
662 /* Draw windows' borders first, then windows. */
663 draw_wins_borderlines(world.wmeta->chain_start, world.wmeta->active,
665 draw_wins_bordercorners(world.wmeta->chain_start, world.wmeta->pad);
666 draw_wins(world.wmeta->chain_start);
668 /* Draw virtual screen scroll hints. */
669 if (world.wmeta->pad_offset > 0)
671 padscroll_hint('<', world.wmeta->pad_offset + 1);
673 uint16_t size_x = getmaxx(world.wmeta->pad);
674 uint16_t right_edge = world.wmeta->pad_offset + world.wmeta->padsize.x;
675 if (right_edge < size_x - 1)
677 padscroll_hint('>', size_x - right_edge);
680 /* Write pad segment to be shown on physical screen to screen buffer. */
681 pnoutrefresh(world.wmeta->pad, 0, world.wmeta->pad_offset, 0, 0,
682 world.wmeta->padsize.y, world.wmeta->padsize.x - 1);
685 /* Only at the end write accumulated changes to the physical screen. */