home · contact · privacy
Client: Clear up place_win() code, improve its comments.
[plomrogue] / src / client / wincontrol.c
1 /* src/client/wincontrol.c */
2
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 */
15
16
17
18 /* Get Win before window identified by "c" or NULL if there is none. */
19 static struct Win * get_win_before(char c);
20
21 /* Make .v_screen just wide enough to contain all visible windows. */
22 static void refit_v_screen();
23
24 /* Update geometry (sizes, positions) of window "w" and its successors in the
25  * window chain. Use place_win() for the positioning algorithm.
26  */
27 static void update_wins(struct Win * w);
28 static void place_win(struct Win * w);
29
30 /* Write "win"'s size back to .target_(height/width) as per .target_*_type. */
31 static void set_win_target_size(struct Win * win);
32
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).
37  */
38 static void append_win(struct Win * w);
39 static void suspend_win(struct Win * w);
40
41
42
43 extern struct Win * get_win_before(char c)
44 {
45     uint8_t i = get_win_pos_in_order(c);
46     if (i > 0)
47     {
48         return get_win_by_id(world.winDB.order[i - 1]);
49     }
50     return NULL;
51 }
52
53
54
55 static void refit_v_screen()
56 {
57     /* Determine rightmost window column. */
58     uint32_t lastwcol = 0;
59     struct Win * wp = get_win_by_id(world.winDB.order[0]);
60     while (wp != 0)
61     {
62         if ((uint32_t) wp->start.x + (uint32_t) wp->frame_size.x > lastwcol + 1)
63         {
64             lastwcol = (uint32_t) wp->start.x + (uint32_t) wp->frame_size.x - 1;
65         }
66         wp = get_win_after(wp->id);
67     }
68
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)
73     {
74         uint8_t t = (lastwcol + 2 > UINT16_MAX);
75         exit_err(t, err_s);
76         t = wresize(world.winDB.v_screen, getmaxy(world.winDB.v_screen),
77                     lastwcol + 2);
78         exit_err(t, err_m);
79     }
80 }
81
82
83
84 static void update_wins(struct Win * w)
85 {
86     place_win(w);
87     refit_v_screen();
88     struct Win * next = get_win_after(w->id);
89     if (next)
90     {
91         update_wins(next);
92     }
93 }
94
95
96
97 static void place_win(struct Win * w)
98 {
99     uint8_t sep = 1;         /* Width of inter-window borders and title bars. */
100
101     /* If w is first window, it goes into the top left corner. */
102     w->start.x = 0;
103     w->start.y = 0 + sep;
104     struct Win * w_prev = get_win_before(w->id);
105     if (w_prev)
106     {
107
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.
110          */
111         struct Win * w_top = w_prev;
112         for (;
113              w_top->start.y != 0 + sep;
114              w_top = get_win_before(w_top->id));
115         w->start.x = w_top->start.x + w_top->frame_size.x + sep;
116
117         /* If enough space is found below w's predecessor, fit w's top left
118          * corner to that predecessor's bottom left corner.
119          */
120         uint16_t next_free_y = w_prev->start.y + w_prev->frame_size.y + sep;
121         if (   w->frame_size.x <= w_prev->frame_size.x
122             && w->frame_size.y <= world.winDB.v_screen_size.y - next_free_y)
123         {
124             w->start.x = w_prev->start.x;
125             w->start.y = next_free_y;
126             return;
127         }
128
129         /* If that fails, try to fit w's top left corner to the top right corner
130          * of its next predecessor w_test 1) below w_top (w's next predecessor
131          * starting a new stack on the screen top) 2) and the most rightward
132          * lower neighbor of a window w_high, itself throning over enough free
133          * space for w to fit below it, rightwards of its lower neighbor w_test.
134          */
135         struct Win * w_test = w_prev;
136         struct Win * w_high;
137         while (w_test != w_top)
138         {
139             for (w_high = get_win_before(w_test->id);  /* Walk down chain     */
140                  w_test->start.y <= w_high->start.y;   /* until w_high starts */
141                  w_high = get_win_before(w_high->id)); /* higher than w_test. */
142             next_free_y = w_high->start.y + w_high->frame_size.y + sep;
143             uint16_t first_free_x = w_test->start.x + w_test->frame_size.x +sep;
144             uint16_t last_free_x = w_high->start.x + w_high->frame_size.x;
145             if (   w->frame_size.y <= world.winDB.v_screen_size.y - next_free_y
146                 && w->frame_size.x <= last_free_x - first_free_x)
147             {
148                 w->start.x = first_free_x;
149                 w->start.y = next_free_y;
150                 break;
151             }
152             w_test = w_high;
153         }
154     }
155 }
156
157
158
159 static void set_win_target_size(struct Win * w)
160 {
161     if      (0 == w->target_height_type)
162     {
163         w->target_height = w->frame_size.y;
164     }
165     else if (1 == w->target_height_type)
166     {
167         w->target_height = w->frame_size.y - world.winDB.v_screen_size.y +1;
168     }
169     if      (0 == w->target_width_type)
170     {
171         w->target_width = w->frame_size.x;
172     }
173     else if (1 == w->target_width_type)
174     {
175         w->target_width = w->frame_size.x - world.winDB.v_screen_size.x;
176     }
177 }
178
179
180
181 static void append_win(struct Win * w)
182 {
183     char * f_name = "append_win()";
184     uint8_t old_size = strlen(world.winDB.order) + 1;
185     char * new_order = try_malloc(old_size + 1, f_name);
186     memcpy(new_order, world.winDB.order, old_size - 1);
187     new_order[old_size - 1] = w->id;
188     new_order[old_size] = '\0';
189     free(world.winDB.order);
190     world.winDB.order = new_order;
191     world.winDB.active = w->id;
192     update_wins(w);
193 }
194
195
196
197 static void suspend_win(struct Win * w)
198 {
199     char * f_name = "suspend_win()";
200     uint8_t new_size = strlen(world.winDB.order);
201     char * new_order = try_malloc(new_size, f_name);
202     uint8_t i = get_win_pos_in_order(w->id);
203     char next_char = world.winDB.order[i + 1];
204     world.winDB.order[i] = '\0';
205     char * second_part = &world.winDB.order[i + 1];
206     sprintf(new_order, "%s%s", world.winDB.order, second_part);
207     free(world.winDB.order);
208     world.winDB.order = new_order;
209     world.winDB.active = world.winDB.order[i];
210     if (!world.winDB.order[i] && 0 < i)
211     {
212         world.winDB.active = world.winDB.order[i - 1];
213     }
214     if (world.winDB.order[i])
215     {
216         update_wins(get_win_by_id(next_char)); /* Already calls               */
217         return;                                /* refit_v_screen(), so leave. */
218     }
219     refit_v_screen();
220 }
221
222
223
224 extern void toggle_window(char id)
225 {
226     struct Win * win = get_win_by_id(id);
227     if (NULL == strchr(world.winDB.order, id))
228     {
229         append_win(win);
230         return;
231     }
232     suspend_win(win);
233 }
234
235
236
237 extern void toggle_winconfig()
238 {
239     if (!world.winDB.active)
240     {
241         return;
242     }
243     struct Win * w = get_win_by_id(world.winDB.active);
244     if      (0 == w->view)
245     {
246         w->view          = 1;
247         w->target_center = w->center;
248         memset(&w->center, 0, sizeof(struct yx_uint16));
249         return;
250     }
251     else if (1 == w->view)
252     {
253         w->view          = 2;
254         w->center.x      = 0;
255         return;
256     }
257     w->view          = 0;
258     w->center        = w->target_center;
259 }
260
261
262
263 extern void toggle_win_size_type(char axis)
264 {
265     struct Win * w = get_win_by_id(world.winDB.active);
266     if ('y' == axis)
267     {
268         w->target_height_type = (0 == w->target_height_type);
269         set_win_target_size(w);
270         return;
271     }
272     w->target_width_type = (   0 == w->target_width_type
273                             && w->frame_size.x <= world.winDB.v_screen_size.x);
274     set_win_target_size(w);
275 }
276
277
278
279 extern void toggle_linebreak_type()
280 {
281     struct Win * w = get_win_by_id(world.winDB.active);
282     if      (0 == w->linebreak)
283     {
284         w->linebreak = 1;
285     }
286     else if (1 == w->linebreak)
287     {
288         w->linebreak = 2;
289     }
290     else if (2 == w->linebreak)
291     {
292         w->linebreak = 0;
293     }
294 }
295
296
297
298 extern void resize_active_win(char change)
299 {
300     if (world.winDB.active)
301     {
302         struct Win * w = get_win_by_id(world.winDB.active);
303         if      (change == '-' && w->frame_size.y > 1)
304         {
305             w->frame_size.y--;
306         }
307         else if (change == '_' && w->frame_size.x > 1)
308         {
309             w->frame_size.x--;
310         }
311         else if (   change == '+'
312                  && w->frame_size.y < world.winDB.v_screen_size.y - 1)
313         {
314             w->frame_size.y++;
315         }
316         else if (change == '*' && w->frame_size.y < UINT16_MAX)
317         {
318             w->frame_size.x++;
319         }
320         if (   1 == w->target_width_type
321             && w->frame_size.x > world.winDB.v_screen_size.x)
322         {
323             w->target_width_type = 0;
324         }
325         set_win_target_size(w);
326         update_wins(w);
327     }
328 }
329
330
331
332 extern void shift_active_win(char dir)
333 {
334     uint8_t len_order = strlen(world.winDB.order);
335     if (1 < len_order)
336     {
337         char tmp[len_order + 1];
338         tmp[len_order] = '\0';
339         uint8_t pos = get_win_pos_in_order(world.winDB.active);
340         if ('f' == dir)
341         {
342             if (pos == len_order - 1)
343             {
344                 memcpy(tmp + 1, world.winDB.order, len_order - 1);
345                 tmp[0] = world.winDB.active;
346                 memcpy(world.winDB.order, tmp, len_order + 1);
347             }
348             else
349             {
350                 world.winDB.order[pos] = world.winDB.order[pos + 1];
351                 world.winDB.order[pos + 1] = world.winDB.active;
352             }
353         }
354         else
355         {
356             if (pos == 0)
357             {
358                 memcpy(tmp, world.winDB.order + 1, len_order - 1);
359                 tmp[len_order - 1] = world.winDB.active;
360                 memcpy(world.winDB.order, tmp, len_order + 1);
361             }
362             else
363             {
364                 world.winDB.order[pos] = world.winDB.order[pos - 1];
365                 world.winDB.order[pos - 1] = world.winDB.active;
366             }
367         }
368         update_wins(get_win_by_id(world.winDB.order[0]));
369     }
370 }
371
372
373
374 extern void scroll_v_screen(char dir)
375 {
376     if      (   '+' == dir
377              &&   world.winDB.v_screen_offset + world.winDB.v_screen_size.x + 1
378                 < getmaxx(world.winDB.v_screen))
379     {
380         world.winDB.v_screen_offset++;
381     }
382     else if (   '-' == dir
383              && world.winDB.v_screen_offset > 0)
384     {
385         world.winDB.v_screen_offset--;
386     }
387 }
388
389
390
391 extern void cycle_active_win(char dir)
392 {
393     uint8_t len_order = strlen(world.winDB.order);
394     if (1 < len_order)
395     {
396         uint8_t pos = get_win_pos_in_order(world.winDB.active);
397         if ('f' == dir)
398         {
399             world.winDB.active = world.winDB.order[pos + 1];
400             if ('\0' == world.winDB.active)
401             {
402                 world.winDB.active = world.winDB.order[0];
403             }
404             return;
405         }
406         if (pos > 0)
407         {
408             world.winDB.active = world.winDB.order[pos - 1];
409             return;
410         }
411         world.winDB.active = world.winDB.order[len_order - 1];
412     }
413 }