1 /* src/client/windows.c */
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
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 */
23 /* Make virtual screen just wide enough to contain all visible windows. */
24 static void refit_pad();
26 /* Update geometry (sizes, positions) of window "w" and its successors in the
27 * window chain. Use place_win() for the positioning algorithm.
29 static void update_wins(struct Win * w);
30 static void place_win(struct Win * w);
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.
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);
44 /* Draw contents of all windows in window chain from window "w" onwards. */
45 static void draw_wins(struct Win * w);
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.
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);
57 /* Shift active window forwards / backwards in window chain. */
58 static void shift_win_forward();
59 static void shift_win_backward();
63 static void refit_pad()
65 /* Determine rightmost window column. */
66 uint32_t lastwcol = 0;
67 struct Win * wp = world.wmeta.chain_start;
70 if ((uint32_t) wp->start.x + (uint32_t) wp->framesize.x > lastwcol + 1)
72 lastwcol = (uint32_t) wp->start.x + (uint32_t) wp->framesize.x - 1;
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)
82 uint8_t t = (lastwcol + 2 > UINT16_MAX);
84 t = wresize(world.wmeta.pad, getmaxy(world.wmeta.pad), lastwcol + 2);
91 static void update_wins(struct Win * w)
103 static void place_win(struct Win * w)
105 /* If w is first window, it goes into the top left corner. */
107 w->start.y = 1; /* Leave space for title bar. */
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)
117 w->start.x = w_top->start.x + w_top->framesize.x + 1;
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)
124 w->start.x = w->prev->start.x;
125 w->start.y = w_prev_maxy + 1;
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.
136 struct Win * w_test = w->prev;
138 while (w_test != w_top)
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)
148 w->start.x = w_test->start.x + w_test->framesize.x + 1;
149 w->start.y = w_thr_bottom + 1;
160 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
161 char * unit, struct yx_uint16 start)
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)
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);
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)
177 dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
180 /* Draw scroll hint line as dir symbols bracketing description text. */
181 uint16_t draw_offset = 0;
184 draw_offset = fsize.x - 1;
188 draw_offset = fsize.y - 1;
191 for (; q < dsc_space; q++)
193 chtype c = dir | A_REVERSE;
194 if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
196 c = scrolldsc[q - dsc_offset] | A_REVERSE;
198 if ('<' == dir || '>' == dir)
200 mvwaddch(world.wmeta.pad, start.y + q, start.x + draw_offset, c);
203 mvwaddch(world.wmeta.pad, start.y + draw_offset, start.x + q, c);
208 static void padscroll_hint(char dir, uint16_t dist)
210 struct yx_uint16 start;
212 start.x = world.wmeta.pad_offset;
213 scroll_hint(world.wmeta.padsize, dir, dist, "columns", start);
218 static void winscroll_hint(struct Win * w, char dir, uint16_t dist)
220 char * unit = "lines";
221 if ('<' == dir || '>' == dir)
225 struct yx_uint16 start = w->start;
226 scroll_hint(w->framesize, dir, dist, unit, start);
231 static void draw_wins(struct Win * 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);
239 for (y = offset_y; y < w->framesize.y + offset_y && y < size_y; y++)
241 for (x = offset_x; x < w->framesize.x + offset_x && x < size_x; x++)
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);
254 winscroll_hint(w, '^', offset_y + 1);
256 if (size_y > offset_y + w->framesize.y)
258 winscroll_hint(w, 'v', size_y - ((offset_y + w->framesize.y) - 1));
262 winscroll_hint(w, '<', offset_x + 1);
264 if (size_x > offset_x + w->framesize.x)
266 winscroll_hint(w, '>', size_x - ((offset_x + w->framesize.x) - 1));
270 return draw_wins(w->next);
276 static void draw_win_borderlines(struct Win * w)
278 /* Draw vertical and horizontal border lines. */
280 for (y = w->start.y; y <= w->start.y + w->framesize.y; y++)
282 mvwaddch(world.wmeta.pad, y, w->start.x - 1, '|');
283 mvwaddch(world.wmeta.pad, y, w->start.x + w->framesize.x, '|');
285 for (x = w->start.x; x <= w->start.x + w->framesize.x; x++)
287 mvwaddch(world.wmeta.pad, w->start.y - 1, x, '-');
288 mvwaddch(world.wmeta.pad, w->start.y + w->framesize.y, x, '-');
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)
295 uint16_t title_offset = 0;
296 if (w->framesize.x > strlen(w->title) + 2)
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)
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);
317 static void draw_wins_borderlines(struct Win * w)
319 draw_win_borderlines(w);
322 draw_wins_borderlines(w->next);
328 static void draw_wins_bordercorners(struct Win * w)
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, '+');
337 draw_wins_bordercorners(w->next);
343 static void shift_win_forward()
345 if (world.wmeta.active == world.wmeta.chain_end)
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;
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)
360 world.wmeta.chain_end = world.wmeta.active;
361 world.wmeta.active->next = 0;
365 world.wmeta.active->next = old_next->next;
366 world.wmeta.active->next->prev = world.wmeta.active;
368 if (world.wmeta.chain_start == world.wmeta.active)
370 world.wmeta.chain_start = old_next;
374 old_prev->next = old_next;
376 old_next->prev = old_prev;
377 old_next->next = world.wmeta.active;
378 world.wmeta.active->prev = old_next;
384 static void shift_win_backward()
386 if (world.wmeta.active == world.wmeta.chain_start)
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;
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)
401 world.wmeta.chain_start = world.wmeta.active;
402 world.wmeta.active->prev = 0;
406 world.wmeta.active->prev = old_prev->prev;
407 world.wmeta.active->prev->next = world.wmeta.active;
409 if (world.wmeta.chain_end == world.wmeta.active)
411 world.wmeta.chain_end = old_prev;
415 old_next->prev = old_prev;
417 old_prev->next = old_next;
418 old_prev->prev = world.wmeta.active;
419 world.wmeta.active->next = old_prev;
425 extern void init_win_meta()
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);
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;
448 extern void init_win(struct Win ** wp, char * title, int16_t height,
449 int16_t width, void * func)
451 char * f_name = "init_win()";
452 struct Win * w = try_malloc(sizeof(struct Win), f_name);
458 w->title = try_malloc(strlen(title) + 1, f_name);
459 sprintf(w->title, "%s", title);
465 w->framesize.x = width;
469 w->framesize.x = world.wmeta.padsize.x + width;
471 if (0 < height && height <= world.wmeta.padsize.y - 1)
473 w->framesize.y = height;
475 else if (0 >= height && world.wmeta.padsize.y + (height - 1) > 0)
477 w->framesize.y = world.wmeta.padsize.y + (height - 1);
484 extern void free_winmeta_and_endwin()
486 delwin(world.wmeta.pad);
492 extern void free_win(struct Win * win)
500 extern void append_win(struct Win * w)
502 if (0 != world.wmeta.chain_start)
504 w->prev = world.wmeta.chain_end;
505 world.wmeta.chain_end->next = w;
509 world.wmeta.active = w;
510 world.wmeta.chain_start = w;
512 world.wmeta.chain_end = w;
518 extern void suspend_win(struct Win * w)
520 if (world.wmeta.chain_start != w)
522 w->prev->next = w->next;
526 world.wmeta.chain_start = w->next;
528 uint8_t pad_refitted = 0;
529 if (world.wmeta.chain_end != w)
531 w->next->prev = w->prev;
532 if (world.wmeta.active == w)
534 world.wmeta.active = w->next;
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. */
541 world.wmeta.chain_end = w->prev;
542 if (world.wmeta.active == w)
544 world.wmeta.active = w->prev;
549 if (0 == pad_refitted)
557 extern void reset_pad_offset(uint16_t new_offset)
560 && (new_offset < world.wmeta.pad_offset
561 || new_offset + world.wmeta.padsize.x < getmaxx(world.wmeta.pad)))
563 world.wmeta.pad_offset = new_offset;
569 extern void resize_active_win(struct yx_uint16 size)
571 if (0 != world.wmeta.active
572 && size.x > 0 && size.y > 0 && size.y < world.wmeta.padsize.y)
574 world.wmeta.active->framesize = size;
575 update_wins(world.wmeta.active); /* Positioning of following */
576 } /* windows may be affected. */
581 extern void cycle_active_win(char dir)
583 if (0 != world.wmeta.active)
587 if (world.wmeta.active->next != 0)
589 world.wmeta.active = world.wmeta.active->next;
593 world.wmeta.active = world.wmeta.chain_start;
598 if (world.wmeta.active->prev != 0)
600 world.wmeta.active = world.wmeta.active->prev;
604 world.wmeta.active = world.wmeta.chain_end;
612 extern void shift_active_win(char dir)
614 if ( 0 == world.wmeta.active /* No shifting with < 2 windows visible. */
615 || world.wmeta.chain_start == world.wmeta.chain_end)
622 update_wins(world.wmeta.chain_start);
625 shift_win_backward();
626 update_wins(world.wmeta.chain_start);
631 extern void draw_all_wins()
633 /* Empty everything before filling it a-new. */
635 wnoutrefresh(world.wmeta.screen);
636 werase(world.wmeta.pad);
637 if (world.wmeta.chain_start)
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);
645 /* Draw virtual screen scroll hints. */
646 if (world.wmeta.pad_offset > 0)
648 padscroll_hint('<', world.wmeta.pad_offset + 1);
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)
654 padscroll_hint('>', size_x - right_edge);
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);
662 /* Only at the end write accumulated changes to the physical screen. */