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