home · contact · privacy
17d80ae7f961d04db75e4c280f7f006182b1e10f
[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     /* If w is first window, it goes into the top left corner. */
100     w->start.x = 0;
101     w->start.y = 1;                             /* Leave space for title bar. */
102     struct Win * w_prev = get_win_before(w->id);
103     if (w_prev)
104     {
105
106         /* If not, fit w's top left to top right of last top predecessor. */
107         struct Win * w_top = w_prev;
108         for (;
109              w_top->start.y != 1;
110              w_top = get_win_before(w_top->id));
111         w->start.x = w_top->start.x + w_top->frame_size.x + 1;
112
113         /* Fit w's top left to bottom left of its ->prev if enough space. */
114         uint16_t w_prev_maxy = w_prev->start.y + w_prev->frame_size.y;
115         if (   w->frame_size.x <= w_prev->frame_size.x
116             && w->frame_size.y <  world.winDB.v_screen_size.y - w_prev_maxy)
117         {
118             w->start.x = w_prev->start.x;
119             w->start.y = w_prev_maxy + 1;
120             return;
121         }
122
123         /* Failing that, try to fit w' top left to the top right of the last
124          * predecessor w_test 1) not followed by windows with a left corner
125          * further rightwards than its own 2) with enough space rightwards for w
126          * until the bottom right of w_thr directly throning over it 3) and with
127          * this same space extending far enough to the bottom for fitting in w.
128          */
129         struct Win * w_test = w_prev;
130         struct Win * w_thr;
131         while (w_test != w_top)
132         {
133             for (w_thr = get_win_before(w_test->id);
134                  w_test->start.y <= w_thr->start.y;
135                  w_thr = get_win_before(w_thr->id));
136             uint16_t w_thr_bottom = w_thr->start.y + w_thr->frame_size.y;
137             uint16_t free_width =   (w_thr->start.x + w_thr->frame_size.x)
138                                   - (w_test->start.x + w_test->frame_size.x);
139             if (   w->frame_size.y < world.winDB.v_screen_size.y - w_thr_bottom
140                 && w->frame_size.x < free_width)
141             {
142                 w->start.x = w_test->start.x + w_test->frame_size.x + 1;
143                 w->start.y = w_thr_bottom + 1;
144                 break;
145             }
146             w_test = w_thr;
147         }
148     }
149 }
150
151
152
153 static void set_win_target_size(struct Win * w)
154 {
155     if      (0 == w->target_height_type)
156     {
157         w->target_height = w->frame_size.y;
158     }
159     else if (1 == w->target_height_type)
160     {
161         w->target_height = w->frame_size.y - world.winDB.v_screen_size.y +1;
162     }
163     if      (0 == w->target_width_type)
164     {
165         w->target_width = w->frame_size.x;
166     }
167     else if (1 == w->target_width_type)
168     {
169         w->target_width = w->frame_size.x - world.winDB.v_screen_size.x;
170     }
171 }
172
173
174
175 static void append_win(struct Win * w)
176 {
177     char * f_name = "append_win()";
178     uint8_t old_size = strlen(world.winDB.order) + 1;
179     char * new_order = try_malloc(old_size + 1, f_name);
180     memcpy(new_order, world.winDB.order, old_size - 1);
181     new_order[old_size - 1] = w->id;
182     new_order[old_size] = '\0';
183     free(world.winDB.order);
184     world.winDB.order = new_order;
185     world.winDB.active = w->id;
186     update_wins(w);
187 }
188
189
190
191 static void suspend_win(struct Win * w)
192 {
193     char * f_name = "suspend_win()";
194     uint8_t new_size = strlen(world.winDB.order);
195     char * new_order = try_malloc(new_size, f_name);
196     uint8_t i = get_win_pos_in_order(w->id);
197     char next_char = world.winDB.order[i + 1];
198     world.winDB.order[i] = '\0';
199     char * second_part = &world.winDB.order[i + 1];
200     sprintf(new_order, "%s%s", world.winDB.order, second_part);
201     free(world.winDB.order);
202     world.winDB.order = new_order;
203     world.winDB.active = world.winDB.order[i];
204     if (!world.winDB.order[i] && 0 < i)
205     {
206         world.winDB.active = world.winDB.order[i - 1];
207     }
208     if (world.winDB.order[i])
209     {
210         update_wins(get_win_by_id(next_char)); /* Already calls               */
211         return;                                /* refit_v_screen(), so leave. */
212     }
213     refit_v_screen();
214 }
215
216
217
218 extern void toggle_window(char id)
219 {
220     struct Win * win = get_win_by_id(id);
221     if (NULL == strchr(world.winDB.order, id))
222     {
223         append_win(win);
224         return;
225     }
226     suspend_win(win);
227 }
228
229
230
231 extern void toggle_winconfig()
232 {
233     if (!world.winDB.active)
234     {
235         return;
236     }
237     struct Win * w = get_win_by_id(world.winDB.active);
238     if      (0 == w->view)
239     {
240         w->view          = 1;
241         w->target_center = w->center;
242         memset(&w->center, 0, sizeof(struct yx_uint16));
243         return;
244     }
245     else if (1 == w->view)
246     {
247         w->view          = 2;
248         w->center.x      = 0;
249         return;
250     }
251     w->view          = 0;
252     w->center        = w->target_center;
253 }
254
255
256
257 extern void toggle_win_size_type(char axis)
258 {
259     struct Win * w = get_win_by_id(world.winDB.active);
260     if ('y' == axis)
261     {
262         w->target_height_type = (0 == w->target_height_type);
263         set_win_target_size(w);
264         return;
265     }
266     w->target_width_type = (   0 == w->target_width_type
267                             && w->frame_size.x <= world.winDB.v_screen_size.x);
268     set_win_target_size(w);
269 }
270
271
272
273 extern void toggle_linebreak_type()
274 {
275     struct Win * w = get_win_by_id(world.winDB.active);
276     if      (0 == w->linebreak)
277     {
278         w->linebreak = 1;
279     }
280     else if (1 == w->linebreak)
281     {
282         w->linebreak = 2;
283     }
284     else if (2 == w->linebreak)
285     {
286         w->linebreak = 0;
287     }
288 }
289
290
291
292 extern void resize_active_win(char change)
293 {
294     if (world.winDB.active)
295     {
296         struct Win * w = get_win_by_id(world.winDB.active);
297         if      (change == '-' && w->frame_size.y > 1)
298         {
299             w->frame_size.y--;
300         }
301         else if (change == '_' && w->frame_size.x > 1)
302         {
303             w->frame_size.x--;
304         }
305         else if (   change == '+'
306                  && w->frame_size.y < world.winDB.v_screen_size.y - 1)
307         {
308             w->frame_size.y++;
309         }
310         else if (change == '*' && w->frame_size.y < UINT16_MAX)
311         {
312             w->frame_size.x++;
313         }
314         if (   1 == w->target_width_type
315             && w->frame_size.x > world.winDB.v_screen_size.x)
316         {
317             w->target_width_type = 0;
318         }
319         set_win_target_size(w);
320         update_wins(w);
321     }
322 }
323
324
325
326 extern void shift_active_win(char dir)
327 {
328     uint8_t len_order = strlen(world.winDB.order);
329     if (1 < len_order)
330     {
331         char tmp[len_order + 1];
332         tmp[len_order] = '\0';
333         uint8_t pos = get_win_pos_in_order(world.winDB.active);
334         if ('f' == dir)
335         {
336             if (pos == len_order - 1)
337             {
338                 memcpy(tmp + 1, world.winDB.order, len_order - 1);
339                 tmp[0] = world.winDB.active;
340                 memcpy(world.winDB.order, tmp, len_order + 1);
341             }
342             else
343             {
344                 world.winDB.order[pos] = world.winDB.order[pos + 1];
345                 world.winDB.order[pos + 1] = world.winDB.active;
346             }
347         }
348         else
349         {
350             if (pos == 0)
351             {
352                 memcpy(tmp, world.winDB.order + 1, len_order - 1);
353                 tmp[len_order - 1] = world.winDB.active;
354                 memcpy(world.winDB.order, tmp, len_order + 1);
355             }
356             else
357             {
358                 world.winDB.order[pos] = world.winDB.order[pos - 1];
359                 world.winDB.order[pos - 1] = world.winDB.active;
360             }
361         }
362         update_wins(get_win_by_id(world.winDB.order[0]));
363     }
364 }
365
366
367
368 extern void scroll_v_screen(char dir)
369 {
370     if      (   '+' == dir
371              &&   world.winDB.v_screen_offset + world.winDB.v_screen_size.x + 1
372                 < getmaxx(world.winDB.v_screen))
373     {
374         world.winDB.v_screen_offset++;
375     }
376     else if (   '-' == dir
377              && world.winDB.v_screen_offset > 0)
378     {
379         world.winDB.v_screen_offset--;
380     }
381 }
382
383
384
385 extern void cycle_active_win(char dir)
386 {
387     uint8_t len_order = strlen(world.winDB.order);
388     if (1 < len_order)
389     {
390         uint8_t pos = get_win_pos_in_order(world.winDB.active);
391         if ('f' == dir)
392         {
393             world.winDB.active = world.winDB.order[pos + 1];
394             if ('\0' == world.winDB.active)
395             {
396                 world.winDB.active = world.winDB.order[0];
397             }
398             return;
399         }
400         if (pos > 0)
401         {
402             world.winDB.active = world.winDB.order[pos - 1];
403             return;
404         }
405         world.winDB.active = world.winDB.order[len_order - 1];
406     }
407 }