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> /* chtype, pnoutrefresh(), doupdate(), werase(), erase(),
10 * wnoutrefresh(), getmaxx(), getmaxy(), mvwaddstr(),
11 * mvwaddch(), mvwaddstr(), wresize()
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 "misc.h" /* for center_offset() */
17 #include "world.h" /* for world global */
22 /* Make virtual screen just wide enough to contain all visible windows. */
23 static void refit_pad();
25 /* Update geometry (sizes, positions) of window "w" and its successors in the
26 * window chain. Use place_win() for the positioning algorithm.
28 static void update_wins(struct Win * w);
29 static void place_win(struct Win * w);
31 /* Draw scroll hint (a line saying that there are "dist" more elements of "unit"
32 * further into the direction symbolized by "dir") into virtual screen, onto an
33 * appropriate edge of a window or the screen; the left/right edge if "dir" is
34 * "<"/">", or the top/bottom edge if it is "^"/"v". "start" be either the start
35 * coordinate of a window's frame, or .y=0, .x=wm->pad_offset for the virtual
36 * screen. winscroll_hint() and padscroll_hint() are wrappers to both cases.
38 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
39 char * unit, struct yx_uint16 start);
40 static void winscroll_hint(struct Win * w, char dir, uint16_t dist);
41 static void padscroll_hint(char dir, uint16_t dist);
43 /* Draw contents of all windows in window chain from window "w" onwards. */
44 static void draw_wins(struct Win * w);
46 /* draw_win_borderlines() draws vertical/horizontal borders of window "w" sans
47 * corners into the virtual screen. It draws the top border line as the windows'
48 * title bar (highlighted if the window is selected as active). It is called
49 * recursively by draw_wins_borderlines() on all windows from "w" on.
50 * draw_wins_bordercorners() draws the border corners of "w" and its successors.
52 static void draw_win_borderlines(struct Win * w);
53 static void draw_wins_borderlines(struct Win * w);
54 static void draw_wins_bordercorners(struct Win * w);
56 /* Shift active window forwards / backwards in window chain. */
57 static void shift_win_forward();
58 static void shift_win_backward();
62 static void refit_pad()
64 /* Determine rightmost window column. */
65 uint32_t lastwcol = 0;
66 struct Win * wp = world.wins.chain_start;
69 if ((uint32_t) wp->start.x + (uint32_t) wp->framesize.x > lastwcol + 1)
71 lastwcol = (uint32_t) wp->start.x + (uint32_t) wp->framesize.x - 1;
76 /* Only resize the pad if the rightmost window column has changed. */
77 char * err_s = "refit_pad() extends virtual screen beyond legal sizes.";
78 char * err_m = "refit_pad() triggers memory alloc error via wresize().";
79 if (getmaxx(world.wins.pad) + 1 != lastwcol)
81 uint8_t t = (lastwcol + 2 > UINT16_MAX);
83 t = wresize(world.wins.pad, getmaxy(world.wins.pad), lastwcol + 2);
90 static void update_wins(struct Win * w)
102 static void place_win(struct Win * w)
104 /* If w is first window, it goes into the top left corner. */
106 w->start.y = 1; /* Leave space for title bar. */
110 /* If not, fit w's top left to top right of last top predecessor. */
111 struct Win * w_top = w->prev;
112 while (w_top->start.y != 1)
116 w->start.x = w_top->start.x + w_top->framesize.x + 1;
118 /* Fit w's top left to bottom left of its ->prev if enough space. */
119 uint16_t w_prev_maxy = w->prev->start.y + w->prev->framesize.y;
120 if ( w->framesize.x <= w->prev->framesize.x
121 && w->framesize.y < world.wins.padsize.y - w_prev_maxy)
123 w->start.x = w->prev->start.x;
124 w->start.y = w_prev_maxy + 1;
127 /* Failing that, try to fit w' top left to the top right of the last
128 * predecessor w_test 1) not followed by windows with a left corner
129 * further rightwards than its own 2) with enough space rightwards for w
130 * until the bottom right of w_thr directly throning over it 3) and with
131 * this same space extending far enough to the bottom for fitting in w.
135 struct Win * w_test = w->prev;
137 while (w_test != w_top)
139 w_thr = w_test->prev;
140 for (; w_test->start.y <= w_thr->start.y; w_thr = w_thr->prev);
141 uint16_t w_thr_bottom = w_thr->start.y + w_thr->framesize.y;
142 uint16_t free_width = (w_thr->start.x + w_thr->framesize.x)
143 - (w_test->start.x + w_test->framesize.x);
144 if ( w->framesize.y < world.wins.padsize.y - w_thr_bottom
145 && w->framesize.x < free_width)
147 w->start.x = w_test->start.x + w_test->framesize.x + 1;
148 w->start.y = w_thr_bottom + 1;
159 static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
160 char * unit, struct yx_uint16 start)
162 /* Decide on alignment (vertical/horizontal?), thereby hint text space. */
163 char * more = "more";
164 uint16_t dsc_space = fsize.x;
165 if ('<' == dir || '>' == dir)
168 } /* vv-- 10 = max strlen for uint16_t */
169 char scrolldsc[1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1];
170 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
172 /* Decide on offset of the description text inside the scroll hint line. */
173 uint16_t dsc_offset = 1;
174 if (dsc_space > strlen(scrolldsc) + 1)
176 dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
179 /* Draw scroll hint line as dir symbols bracketing description text. */
180 uint16_t draw_offset = 0;
183 draw_offset = fsize.x - 1;
187 draw_offset = fsize.y - 1;
190 for (; q < dsc_space; q++)
192 chtype c = dir | A_REVERSE;
193 if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
195 c = scrolldsc[q - dsc_offset] | A_REVERSE;
197 if ('<' == dir || '>' == dir)
199 mvwaddch(world.wins.pad, start.y + q, start.x + draw_offset, c);
202 mvwaddch(world.wins.pad, start.y + draw_offset, start.x + q, c);
207 static void padscroll_hint(char dir, uint16_t dist)
209 struct yx_uint16 start;
211 start.x = world.wins.pad_offset;
212 scroll_hint(world.wins.padsize, dir, dist, "columns", start);
217 static void winscroll_hint(struct Win * w, char dir, uint16_t dist)
219 char * unit = "lines";
220 if ('<' == dir || '>' == dir)
224 struct yx_uint16 start = w->start;
225 scroll_hint(w->framesize, dir, dist, unit, start);
230 static void draw_wins(struct Win * w)
233 uint16_t size_y = w->winmapsize.y;
234 uint16_t size_x = w->winmapsize.x;
235 uint16_t offset_y = center_offset(w->center.y, size_y, w->framesize.y);
236 uint16_t offset_x = center_offset(w->center.x, size_x, w->framesize.x);
238 for (y = offset_y; y < w->framesize.y + offset_y && y < size_y; y++)
240 for (x = offset_x; x < w->framesize.x + offset_x && x < size_x; x++)
242 chtype ch = w->winmap[(y * w->winmapsize.x) + x];
243 mvwaddch(world.wins.pad, w->start.y + (y - offset_y),
244 w->start.x + (x - offset_x), ch);
253 winscroll_hint(w, '^', offset_y + 1);
255 if (size_y > offset_y + w->framesize.y)
257 winscroll_hint(w, 'v', size_y - ((offset_y + w->framesize.y) - 1));
261 winscroll_hint(w, '<', offset_x + 1);
263 if (size_x > offset_x + w->framesize.x)
265 winscroll_hint(w, '>', size_x - ((offset_x + w->framesize.x) - 1));
269 return draw_wins(w->next);
275 static void draw_win_borderlines(struct Win * w)
277 /* Draw vertical and horizontal border lines. */
279 for (y = w->start.y; y <= w->start.y + w->framesize.y; y++)
281 mvwaddch(world.wins.pad, y, w->start.x - 1, '|');
282 mvwaddch(world.wins.pad, y, w->start.x + w->framesize.x, '|');
284 for (x = w->start.x; x <= w->start.x + w->framesize.x; x++)
286 mvwaddch(world.wins.pad, w->start.y - 1, x, '-');
287 mvwaddch(world.wins.pad, w->start.y + w->framesize.y, x, '-');
290 /* Draw as much as possible of the title into center of top border line. */
291 char min_title_length_visible = 3; /* min. 1 char + 2 padding/decoration */
292 if (w->framesize.x >= min_title_length_visible)
294 uint16_t title_offset = 0;
295 if (w->framesize.x > strlen(w->title) + 2)
297 title_offset = (w->framesize.x - (strlen(w->title) + 2)) / 2;
298 } /* +2 is for padding/decoration */
299 uint16_t length_visible = strnlen(w->title, w->framesize.x - 2);
300 char title[length_visible + 3];
301 char decoration = ' ';
302 if (w == world.wins.win_active)
306 memcpy(title + 1, w->title, length_visible);
307 title[0] = title[length_visible + 1] = decoration;
308 title[length_visible + 2] = '\0';
309 mvwaddstr(world.wins.pad,
310 w->start.y - 1, w->start.x + title_offset, title);
316 static void draw_wins_borderlines(struct Win * w)
318 draw_win_borderlines(w);
321 draw_wins_borderlines(w->next);
327 static void draw_wins_bordercorners(struct Win * w)
329 mvwaddch(world.wins.pad, w->start.y - 1, w->start.x - 1, '+');
330 mvwaddch(world.wins.pad, w->start.y - 1, w->start.x + w->framesize.x, '+');
331 mvwaddch(world.wins.pad, w->start.y + w->framesize.y, w->start.x - 1, '+');
332 mvwaddch(world.wins.pad, w->start.y + w->framesize.y,
333 w->start.x + w->framesize.x, '+');
336 draw_wins_bordercorners(w->next);
342 static void shift_win_forward()
344 if (world.wins.win_active == world.wins.chain_end)
346 world.wins.chain_end = world.wins.win_active->prev;
347 world.wins.chain_end->next = 0;
348 world.wins.win_active->next = world.wins.chain_start;
349 world.wins.win_active->next->prev = world.wins.win_active;
350 world.wins.chain_start = world.wins.win_active;
351 world.wins.chain_start->prev = 0;
355 struct Win * old_prev = world.wins.win_active->prev;
356 struct Win * old_next = world.wins.win_active->next;
357 if (world.wins.chain_end == world.wins.win_active->next)
359 world.wins.chain_end = world.wins.win_active;
360 world.wins.win_active->next = 0;
364 world.wins.win_active->next = old_next->next;
365 world.wins.win_active->next->prev = world.wins.win_active;
367 if (world.wins.chain_start == world.wins.win_active)
369 world.wins.chain_start = old_next;
373 old_prev->next = old_next;
375 old_next->prev = old_prev;
376 old_next->next = world.wins.win_active;
377 world.wins.win_active->prev = old_next;
383 static void shift_win_backward()
385 if (world.wins.win_active == world.wins.chain_start)
387 world.wins.chain_start = world.wins.win_active->next;
388 world.wins.chain_start->prev = 0;
389 world.wins.win_active->prev = world.wins.chain_end;
390 world.wins.win_active->prev->next = world.wins.win_active;
391 world.wins.chain_end = world.wins.win_active;
392 world.wins.chain_end->next = 0;
396 struct Win * old_prev = world.wins.win_active->prev;
397 struct Win * old_next = world.wins.win_active->next;
398 if (world.wins.chain_start == world.wins.win_active->prev)
400 world.wins.chain_start = world.wins.win_active;
401 world.wins.win_active->prev = 0;
405 world.wins.win_active->prev = old_prev->prev;
406 world.wins.win_active->prev->next = world.wins.win_active;
408 if (world.wins.chain_end == world.wins.win_active)
410 world.wins.chain_end = old_prev;
414 old_next->prev = old_prev;
416 old_prev->next = old_next;
417 old_prev->prev = world.wins.win_active;
418 world.wins.win_active->next = old_prev;
424 extern void make_pad()
426 char * err_s = "make_pad() creates an illegaly large virtual screen.";
427 char * err_m = "make_pad() triggers memory allocation error via newpad().";
428 uint32_t maxy_test = getmaxy(world.wins.screen);
429 uint32_t maxx_test = getmaxx(world.wins.screen);
430 exit_err(maxy_test > UINT16_MAX || maxx_test > UINT16_MAX, err_s);
431 world.wins.padsize.y = maxy_test;
432 world.wins.padsize.x = maxx_test;
433 world.wins.pad = newpad(world.wins.padsize.y, 1);
434 exit_err(NULL == world.wins.pad, err_m);
439 extern void init_win(struct Win ** wp, char * title, int16_t height,
440 int16_t width, void * func)
442 char * f_name = "init_win()";
443 struct Win * w = try_malloc(sizeof(struct Win), f_name);
449 w->title = try_malloc(strlen(title) + 1, f_name);
450 sprintf(w->title, "%s", title);
454 w->framesize.y = world.wins.padsize.y - 1;
455 if (0 < height && height <= world.wins.padsize.y - 1)
457 w->framesize.y = height;
459 else if (0 > height && world.wins.padsize.y + (height - 1) > 0)
461 w->framesize.y = world.wins.padsize.y + (height - 1);
463 w->framesize.x = world.wins.padsize.x;
466 w->framesize.x = width;
468 else if (0 > width && world.wins.padsize.x + width > 0)
470 w->framesize.x = world.wins.padsize.x + width;
477 extern void free_win(struct Win * win)
485 extern void append_win(struct Win * w)
487 if (0 != world.wins.chain_start)
489 w->prev = world.wins.chain_end;
490 world.wins.chain_end->next = w;
494 world.wins.win_active = w;
495 world.wins.chain_start = w;
497 world.wins.chain_end = w;
503 extern void suspend_win(struct Win * w)
505 if (world.wins.chain_start != w)
507 w->prev->next = w->next;
511 world.wins.chain_start = w->next;
513 uint8_t pad_refitted = 0;
514 if (world.wins.chain_end != w)
516 w->next->prev = w->prev;
517 if (world.wins.win_active == w)
519 world.wins.win_active = w->next;
521 update_wins(w->next); /* Positioning of successor windows may be */
522 pad_refitted = 1; /* affected / need correction. Note that */
523 } /* update_wins() already refits the pad, */
524 else /* voiding later need for that. */
526 world.wins.chain_end = w->prev;
527 if (world.wins.win_active == w)
529 world.wins.win_active = w->prev;
534 if (0 == pad_refitted)
542 extern void reset_pad_offset(uint16_t new_offset)
545 && (new_offset < world.wins.pad_offset
546 || new_offset + world.wins.padsize.x < getmaxx(world.wins.pad)))
548 world.wins.pad_offset = new_offset;
554 extern void resize_active_win(struct yx_uint16 size)
556 if (0 != world.wins.win_active
557 && size.x > 0 && size.y > 0 && size.y < world.wins.padsize.y)
559 world.wins.win_active->framesize = size;
560 update_wins(world.wins.win_active); /* Positioning of following */
561 } /* windows may be affected. */
566 extern void cycle_active_win(char dir)
568 if (0 != world.wins.win_active)
572 if (world.wins.win_active->next != 0)
574 world.wins.win_active = world.wins.win_active->next;
578 world.wins.win_active = world.wins.chain_start;
583 if (world.wins.win_active->prev != 0)
585 world.wins.win_active = world.wins.win_active->prev;
589 world.wins.win_active = world.wins.chain_end;
597 extern void shift_active_win(char dir)
599 if ( 0 == world.wins.win_active /* No shifting with <2 windows visible. */
600 || world.wins.chain_start == world.wins.chain_end)
607 update_wins(world.wins.chain_start);
610 shift_win_backward();
611 update_wins(world.wins.chain_start);
616 extern void draw_all_wins()
618 /* Empty everything before filling it a-new. */
620 wnoutrefresh(world.wins.screen);
621 werase(world.wins.pad);
622 if (world.wins.chain_start)
625 /* Draw windows' borders first, then windows. */
626 draw_wins_borderlines(world.wins.chain_start);
627 draw_wins_bordercorners(world.wins.chain_start);
628 draw_wins(world.wins.chain_start);
630 /* Draw virtual screen scroll hints. */
631 if (world.wins.pad_offset > 0)
633 padscroll_hint('<', world.wins.pad_offset + 1);
635 uint16_t size_x = getmaxx(world.wins.pad);
636 uint16_t right_edge = world.wins.pad_offset + world.wins.padsize.x;
637 if (right_edge < size_x - 1)
639 padscroll_hint('>', size_x - right_edge);
642 /* Write pad segment to be shown on physical screen to screen buffer. */
643 pnoutrefresh(world.wins.pad, 0, world.wins.pad_offset, 0, 0,
644 world.wins.padsize.y, world.wins.padsize.x - 1);
647 /* Only at the end write accumulated changes to the physical screen. */