1 /* src/client/wincontrol.c */
3 #include "wincontrol.h"
4 #include <ncurses.h> /* getmaxx(), getmaxy(), wresize() */
5 #include <stddef.h> /* NULL */
6 #include <stdint.h> /* uint8_t, uint32_t, UINT16_MAX */
7 #include <stdio.h> /* sprintf() */
8 #include <stdlib.h> /* free() */
9 #include <string.h> /* memcpy(), memset(), strchr(), strlen() */
10 #include "../common/rexit.h" /* exit_err() */
11 #include "../common/try_malloc.h" /* try_malloc() */
12 #include "../common/yx_uint16.h" /* struct yx_uint16 */
13 #include "windows.h" /* struct Win, get_win_by_id(), get_win_pos_in_order() */
14 #include "world.h" /* global world */
18 /* Get Win before window identified by "c" or NULL if there is none. */
19 static struct Win * get_win_before(char c);
21 /* Make .v_screen just wide enough to contain all visible windows. */
22 static void refit_v_screen();
24 /* Update geometry (sizes, positions) of window "w" and its successors in the
25 * window chain. Use place_win() for the positioning algorithm.
27 static void update_wins(struct Win * w);
28 static void place_win(struct Win * w);
30 /* Write "win"'s size back to .target_(height/width) as per .target_*_type. */
31 static void set_win_target_size(struct Win * win);
33 /* Append/suspend window "w" to/from chain of visible windows. Appended windows
34 * will become active. Suspended active windows will move the active window
35 * selection to their successor in the window chain or, failing that, their
36 * predecessor, or, failing that, to 0 (no window active).
38 static void append_win(struct Win * w);
39 static void suspend_win(struct Win * w);
43 extern struct Win * get_win_before(char c)
45 uint8_t i = get_win_pos_in_order(c);
48 return get_win_by_id(world.winDB.order[i - 1]);
55 static void refit_v_screen()
57 /* Determine rightmost window column. */
58 uint32_t lastwcol = 0;
59 struct Win * wp = get_win_by_id(world.winDB.order[0]);
62 if ((uint32_t) wp->start.x + (uint32_t) wp->frame_size.x > lastwcol + 1)
64 lastwcol = (uint32_t) wp->start.x + (uint32_t) wp->frame_size.x - 1;
66 wp = get_win_after(wp->id);
69 /* Only resize .v_screen if the rightmost window column has changed. */
70 char * err_s = "refit_v_screen() grows virtual screen beyond legal sizes.";
71 char * err_m = "refit_v_screen() triggers memory alloc error in wresize().";
72 if ((uint32_t) getmaxx(world.winDB.v_screen) + 1 != lastwcol)
74 uint8_t t = (lastwcol + 2 > UINT16_MAX);
76 t = wresize(world.winDB.v_screen, getmaxy(world.winDB.v_screen),
84 static void update_wins(struct Win * w)
88 struct Win * next = get_win_after(w->id);
97 static void place_win(struct Win * w)
99 uint8_t sep = 1; /* Width of inter-window borders and title bars. */
101 /* If w is first window, it goes into the top left corner. */
103 w->start.y = 0 + sep;
104 struct Win * w_prev = get_win_before(w->id);
108 /* If not, get w's next predecessor starting a new stack on the screen
109 * top, fit w's top left corner to that predecessor's top right corner.
111 struct Win * w_top = w_prev;
112 for (; w_top->start.y != 0 + sep; w_top = get_win_before(w_top->id));
113 w->start.x = w_top->start.x + w_top->frame_size.x + sep;
115 /* If enough space is found below w's predecessor, fit w's top left
116 * corner to that predecessor's bottom left corner.
118 uint16_t next_free_y = w_prev->start.y + w_prev->frame_size.y + sep;
119 if ( w->frame_size.x <= w_prev->frame_size.x
120 && w->frame_size.y <= world.winDB.v_screen_size.y - next_free_y)
122 w->start.x = w_prev->start.x;
123 w->start.y = next_free_y;
127 /* If that fails, try to fit w's top left corner to the top right corner
128 * of its next predecessor w_test 1) below w_top (w's next predecessor
129 * starting a new stack on the screen top) 2) and the most rightward
130 * lower neighbor of a window w_high, itself throning over enough free
131 * space for w to fit below it, rightwards of its lower neighbor w_test.
133 struct Win * w_test = w_prev;
135 while (w_test != w_top)
137 for (w_high = get_win_before(w_test->id); /* Walk down chain */
138 w_test->start.y <= w_high->start.y; /* until w_high starts */
139 w_high = get_win_before(w_high->id)); /* higher than w_test. */
140 next_free_y = w_high->start.y + w_high->frame_size.y + sep;
141 uint16_t first_free_x = w_test->start.x + w_test->frame_size.x +sep;
142 uint16_t last_free_x = w_high->start.x + w_high->frame_size.x;
143 if ( w->frame_size.y <= world.winDB.v_screen_size.y - next_free_y
144 && w->frame_size.x <= last_free_x - first_free_x)
146 w->start.x = first_free_x;
147 w->start.y = next_free_y;
157 static void set_win_target_size(struct Win * w)
159 if (0 == w->target_height_type)
161 w->target_height = w->frame_size.y;
163 else if (1 == w->target_height_type)
165 w->target_height = w->frame_size.y - world.winDB.v_screen_size.y +1;
167 if (0 == w->target_width_type)
169 w->target_width = w->frame_size.x;
171 else if (1 == w->target_width_type)
173 w->target_width = w->frame_size.x - world.winDB.v_screen_size.x;
179 static void append_win(struct Win * w)
181 char * f_name = "append_win()";
182 uint8_t old_size = strlen(world.winDB.order) + 1;
183 char * new_order = try_malloc(old_size + 1, f_name);
184 memcpy(new_order, world.winDB.order, old_size - 1);
185 new_order[old_size - 1] = w->id;
186 new_order[old_size] = '\0';
187 free(world.winDB.order);
188 world.winDB.order = new_order;
189 world.winDB.active = w->id;
195 static void suspend_win(struct Win * w)
197 char * f_name = "suspend_win()";
198 uint8_t new_size = strlen(world.winDB.order);
199 char * new_order = try_malloc(new_size, f_name);
200 uint8_t i = get_win_pos_in_order(w->id);
201 char next_char = world.winDB.order[i + 1];
202 world.winDB.order[i] = '\0';
203 char * second_part = &world.winDB.order[i + 1];
204 sprintf(new_order, "%s%s", world.winDB.order, second_part);
205 free(world.winDB.order);
206 world.winDB.order = new_order;
207 world.winDB.active = world.winDB.order[i];
208 if (!world.winDB.order[i] && 0 < i)
210 world.winDB.active = world.winDB.order[i - 1];
212 if (world.winDB.order[i])
214 update_wins(get_win_by_id(next_char)); /* Already calls */
215 return; /* refit_v_screen(), so leave. */
222 extern void toggle_window(char id)
224 struct Win * win = get_win_by_id(id);
225 if (NULL == strchr(world.winDB.order, id))
235 extern void toggle_winconfig()
237 if (!world.winDB.active)
241 struct Win * w = get_win_by_id(world.winDB.active);
245 w->target_center = w->center;
246 memset(&w->center, 0, sizeof(struct yx_uint16));
249 else if (1 == w->view)
256 w->center = w->target_center;
261 extern void toggle_win_size_type(char axis)
263 struct Win * w = get_win_by_id(world.winDB.active);
266 w->target_height_type = (0 == w->target_height_type);
267 set_win_target_size(w);
270 w->target_width_type = ( 0 == w->target_width_type
271 && w->frame_size.x <= world.winDB.v_screen_size.x);
272 set_win_target_size(w);
277 extern void toggle_linebreak_type()
279 struct Win * w = get_win_by_id(world.winDB.active);
280 if (0 == w->linebreak)
284 else if (1 == w->linebreak)
288 else if (2 == w->linebreak)
296 extern void resize_active_win(char change)
298 if (world.winDB.active)
300 struct Win * w = get_win_by_id(world.winDB.active);
301 if (change == '-' && w->frame_size.y > 1)
305 else if (change == '_' && w->frame_size.x > 1)
309 else if ( change == '+'
310 && w->frame_size.y < world.winDB.v_screen_size.y - 1)
314 else if (change == '*' && w->frame_size.y < UINT16_MAX)
318 if ( 1 == w->target_width_type
319 && w->frame_size.x > world.winDB.v_screen_size.x)
321 w->target_width_type = 0;
323 set_win_target_size(w);
330 extern void shift_active_win(char dir)
332 uint8_t len_order = strlen(world.winDB.order);
335 char tmp[len_order + 1];
336 tmp[len_order] = '\0';
337 uint8_t pos = get_win_pos_in_order(world.winDB.active);
340 if (pos == len_order - 1)
342 memcpy(tmp + 1, world.winDB.order, len_order - 1);
343 tmp[0] = world.winDB.active;
344 memcpy(world.winDB.order, tmp, len_order + 1);
348 world.winDB.order[pos] = world.winDB.order[pos + 1];
349 world.winDB.order[pos + 1] = world.winDB.active;
356 memcpy(tmp, world.winDB.order + 1, len_order - 1);
357 tmp[len_order - 1] = world.winDB.active;
358 memcpy(world.winDB.order, tmp, len_order + 1);
362 world.winDB.order[pos] = world.winDB.order[pos - 1];
363 world.winDB.order[pos - 1] = world.winDB.active;
366 update_wins(get_win_by_id(world.winDB.order[0]));
372 extern void scroll_v_screen(char dir)
375 && world.winDB.v_screen_offset + world.winDB.v_screen_size.x + 1
376 < getmaxx(world.winDB.v_screen))
378 world.winDB.v_screen_offset++;
381 && world.winDB.v_screen_offset > 0)
383 world.winDB.v_screen_offset--;
389 extern void cycle_active_win(char dir)
391 uint8_t len_order = strlen(world.winDB.order);
394 uint8_t pos = get_win_pos_in_order(world.winDB.active);
397 world.winDB.active = world.winDB.order[pos + 1];
398 if ('\0' == world.winDB.active)
400 world.winDB.active = world.winDB.order[0];
406 world.winDB.active = world.winDB.order[pos - 1];
409 world.winDB.active = world.winDB.order[len_order - 1];