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