1 /* src/client/windows.c */
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
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 */
24 /* Make virtual screen just wide enough to contain all visible windows. */
25 static void refit_pad();
27 /* Update geometry (sizes, positions) of window "w" and its successors in the
28 * window chain. Use place_win() for the positioning algorithm.
30 static void update_wins(struct Win * w);
31 static void place_win(struct Win * w);
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.
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);
45 /* Draw contents of all windows in window chain from window "w" onwards. */
46 static void draw_wins(struct Win * w);
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.
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);
58 /* Shift active window forwards / backwards in window chain. */
59 static void shift_win_forward();
60 static void shift_win_backward();
64 static void refit_pad()
66 /* Determine rightmost window column. */
67 uint32_t lastwcol = 0;
68 struct Win * wp = world.wmeta.chain_start;
71 if ((uint32_t) wp->start.x + (uint32_t) wp->framesize.x > lastwcol + 1)
73 lastwcol = (uint32_t) wp->start.x + (uint32_t) wp->framesize.x - 1;
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)
83 uint8_t t = (lastwcol + 2 > UINT16_MAX);
85 t = wresize(world.wmeta.pad, getmaxy(world.wmeta.pad), lastwcol + 2);
92 static void update_wins(struct Win * w)
104 static void place_win(struct Win * w)
106 /* If w is first window, it goes into the top left corner. */
108 w->start.y = 1; /* Leave space for title bar. */
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)
118 w->start.x = w_top->start.x + w_top->framesize.x + 1;
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)
125 w->start.x = w->prev->start.x;
126 w->start.y = w_prev_maxy + 1;
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.
137 struct Win * w_test = w->prev;
139 while (w_test != w_top)
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)
149 w->start.x = w_test->start.x + w_test->framesize.x + 1;
150 w->start.y = w_thr_bottom + 1;
161 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
162 char * unit, struct yx_uint16 start)
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)
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);
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)
178 dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
181 /* Draw scroll hint line as dir symbols bracketing description text. */
182 uint16_t draw_offset = 0;
185 draw_offset = fsize.x - 1;
189 draw_offset = fsize.y - 1;
192 for (; q < dsc_space; q++)
194 chtype c = dir | A_REVERSE;
195 if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
197 c = scrolldsc[q - dsc_offset] | A_REVERSE;
199 if ('<' == dir || '>' == dir)
201 mvwaddch(world.wmeta.pad, start.y + q, start.x + draw_offset, c);
204 mvwaddch(world.wmeta.pad, start.y + draw_offset, start.x + q, c);
209 static void padscroll_hint(char dir, uint16_t dist)
211 struct yx_uint16 start;
213 start.x = world.wmeta.pad_offset;
214 scroll_hint(world.wmeta.padsize, dir, dist, "columns", start);
219 static void winscroll_hint(struct Win * w, char dir, uint16_t dist)
221 char * unit = "lines";
222 if ('<' == dir || '>' == dir)
226 struct yx_uint16 start = w->start;
227 scroll_hint(w->framesize, dir, dist, unit, start);
232 static void draw_wins(struct Win * 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);
240 for (y = offset_y; y < w->framesize.y + offset_y && y < size_y; y++)
242 for (x = offset_x; x < w->framesize.x + offset_x && x < size_x; x++)
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);
255 winscroll_hint(w, '^', offset_y + 1);
257 if (size_y > offset_y + w->framesize.y)
259 winscroll_hint(w, 'v', size_y - ((offset_y + w->framesize.y) - 1));
263 winscroll_hint(w, '<', offset_x + 1);
265 if (size_x > offset_x + w->framesize.x)
267 winscroll_hint(w, '>', size_x - ((offset_x + w->framesize.x) - 1));
271 return draw_wins(w->next);
277 static void draw_win_borderlines(struct Win * w)
279 /* Draw vertical and horizontal border lines. */
281 for (y = w->start.y; y <= w->start.y + w->framesize.y; y++)
283 mvwaddch(world.wmeta.pad, y, w->start.x - 1, '|');
284 mvwaddch(world.wmeta.pad, y, w->start.x + w->framesize.x, '|');
286 for (x = w->start.x; x <= w->start.x + w->framesize.x; x++)
288 mvwaddch(world.wmeta.pad, w->start.y - 1, x, '-');
289 mvwaddch(world.wmeta.pad, w->start.y + w->framesize.y, x, '-');
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)
296 uint16_t title_offset = 0;
297 if (w->framesize.x > strlen(w->title) + 2)
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)
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);
318 static void draw_wins_borderlines(struct Win * w)
320 draw_win_borderlines(w);
323 draw_wins_borderlines(w->next);
329 static void draw_wins_bordercorners(struct Win * w)
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, '+');
338 draw_wins_bordercorners(w->next);
344 static void shift_win_forward()
346 if (world.wmeta.active == world.wmeta.chain_end)
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;
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)
361 world.wmeta.chain_end = world.wmeta.active;
362 world.wmeta.active->next = 0;
366 world.wmeta.active->next = old_next->next;
367 world.wmeta.active->next->prev = world.wmeta.active;
369 if (world.wmeta.chain_start == world.wmeta.active)
371 world.wmeta.chain_start = old_next;
375 old_prev->next = old_next;
377 old_next->prev = old_prev;
378 old_next->next = world.wmeta.active;
379 world.wmeta.active->prev = old_next;
385 static void shift_win_backward()
387 if (world.wmeta.active == world.wmeta.chain_start)
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;
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)
402 world.wmeta.chain_start = world.wmeta.active;
403 world.wmeta.active->prev = 0;
407 world.wmeta.active->prev = old_prev->prev;
408 world.wmeta.active->prev->next = world.wmeta.active;
410 if (world.wmeta.chain_end == world.wmeta.active)
412 world.wmeta.chain_end = old_prev;
416 old_next->prev = old_prev;
418 old_prev->next = old_next;
419 old_prev->prev = world.wmeta.active;
420 world.wmeta.active->next = old_prev;
426 extern void init_win_meta()
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);
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;
449 extern void init_win(struct Win ** wp, char * title, int16_t height,
450 int16_t width, void * func)
452 char * f_name = "init_win()";
453 struct Win * w = try_malloc(sizeof(struct Win), f_name);
459 w->title = try_malloc(strlen(title) + 1, f_name);
460 sprintf(w->title, "%s", title);
466 w->framesize.x = width;
470 w->framesize.x = world.wmeta.padsize.x + width;
472 if (0 < height && height <= world.wmeta.padsize.y - 1)
474 w->framesize.y = height;
476 else if (0 >= height && world.wmeta.padsize.y + (height - 1) > 0)
478 w->framesize.y = world.wmeta.padsize.y + (height - 1);
485 extern void free_winmeta_and_endwin()
487 delwin(world.wmeta.pad);
493 extern void free_win(struct Win * win)
501 extern void append_win(struct Win * w)
503 if (0 != world.wmeta.chain_start)
505 w->prev = world.wmeta.chain_end;
506 world.wmeta.chain_end->next = w;
510 world.wmeta.active = w;
511 world.wmeta.chain_start = w;
513 world.wmeta.chain_end = w;
519 extern void suspend_win(struct Win * w)
521 if (world.wmeta.chain_start != w)
523 w->prev->next = w->next;
527 world.wmeta.chain_start = w->next;
529 uint8_t pad_refitted = 0;
530 if (world.wmeta.chain_end != w)
532 w->next->prev = w->prev;
533 if (world.wmeta.active == w)
535 world.wmeta.active = w->next;
537 update_wins(w->next); /* Positioning of successor windows may be */
538 pad_refitted = 1; /* affected / need correction. Note that */
539 } /* update_wins() already refits the pad, */
540 else /* voiding later need for that. */
542 world.wmeta.chain_end = w->prev;
543 if (world.wmeta.active == w)
545 world.wmeta.active = w->prev;
550 if (0 == pad_refitted)
558 extern void reset_pad_offset(uint16_t new_offset)
561 && (new_offset < world.wmeta.pad_offset
562 || new_offset + world.wmeta.padsize.x < getmaxx(world.wmeta.pad)))
564 world.wmeta.pad_offset = new_offset;
570 extern void resize_active_win(struct yx_uint16 size)
572 if (0 != world.wmeta.active
573 && size.x > 0 && size.y > 0 && size.y < world.wmeta.padsize.y)
575 world.wmeta.active->framesize = size;
576 update_wins(world.wmeta.active); /* Positioning of following */
577 } /* windows may be affected. */
582 extern void cycle_active_win(char dir)
584 if (0 != world.wmeta.active)
588 if (world.wmeta.active->next != 0)
590 world.wmeta.active = world.wmeta.active->next;
594 world.wmeta.active = world.wmeta.chain_start;
599 if (world.wmeta.active->prev != 0)
601 world.wmeta.active = world.wmeta.active->prev;
605 world.wmeta.active = world.wmeta.chain_end;
613 extern void shift_active_win(char dir)
615 if ( 0 == world.wmeta.active /* No shifting with < 2 windows visible. */
616 || world.wmeta.chain_start == world.wmeta.chain_end)
623 update_wins(world.wmeta.chain_start);
626 shift_win_backward();
627 update_wins(world.wmeta.chain_start);
632 extern void draw_all_wins()
634 /* Empty everything before filling it a-new. */
636 wnoutrefresh(world.wmeta.screen);
637 werase(world.wmeta.pad);
638 if (world.wmeta.chain_start)
641 /* Draw windows' borders first, then windows. */
642 draw_wins_borderlines(world.wmeta.chain_start);
643 draw_wins_bordercorners(world.wmeta.chain_start);
644 draw_wins(world.wmeta.chain_start);
646 /* Draw virtual screen scroll hints. */
647 if (world.wmeta.pad_offset > 0)
649 padscroll_hint('<', world.wmeta.pad_offset + 1);
651 uint16_t size_x = getmaxx(world.wmeta.pad);
652 uint16_t right_edge = world.wmeta.pad_offset + world.wmeta.padsize.x;
653 if (right_edge < size_x - 1)
655 padscroll_hint('>', size_x - right_edge);
658 /* Write pad segment to be shown on physical screen to screen buffer. */
659 pnoutrefresh(world.wmeta.pad, 0, world.wmeta.pad_offset, 0, 0,
660 world.wmeta.padsize.y, world.wmeta.padsize.x - 1);
663 /* Only at the end write accumulated changes to the physical screen. */