home · contact · privacy
Read interface config from one file (which can be set as command line argument)
[plomrogue] / src / client / wincontrol.c
1 /* src/client/wincontrol.c */
2
3 #include "wincontrol.h"
4 #include <errno.h> /* global errno */
5 #include <stddef.h> /* NULL */
6 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
7 #include <stdio.h> /* FILE, sprintf() */
8 #include <stdlib.h> /* free(), atoi() */
9 #include <string.h> /* strlen(), strchr(), memcpy() */
10 #include "../common/readwrite.h" /* try_fgets(), try_fwrite(), try_fgetc(),
11                                   * try_fputc()
12                                   */
13 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
14 #include "../common/try_malloc.h" /* try_malloc() */
15 #include "../common/yx_uint16.h" /* struct yx_uint16 */
16 #include "draw_wins.h" /* draw_win_map(), draw_win_info(), draw_win_log(),
17                         * draw_win_available_keybindings(),
18                         * draw_win_inventory(), draw_win_keybindings_global(),
19                         * draw_win_keybindings_winconf_geometry(),
20                         * draw_win_keybindings_winconf_keybindings(),
21                         * draw_winconf_geometry(), draw_winconf_keybindings()
22                         */
23 #include "keybindings.h" /* struct KeyBinding, free_keybindings() */
24 #include "windows.h" /* struct Win, resize_active_win(), reset_pad_offset(),
25                       * append_win(), suspend_win(), init_win(), free_win()
26                       */
27 #include "world.h" /* global world */
28
29
30
31 /* Wrapper around init_win() called with values from Winconf of "id". */
32 static void init_win_from_winconf(char id);
33
34 /* Free data pointed to inside individual WinConf struct of "id". */
35 static void free_winconf_data(char id);
36
37 /* Write geometry of a window to its WinConf, as positive or negative values
38  * (dependent on state ofWinConf->height_type / WinConf->width_type).
39  */
40 static void set_winconf_geometry(char id);
41
42 /* Get WinConf by "id". */
43 static struct WinConf * get_winconf_by_id(char id);
44
45 /* Get (Win->draw) function identified by "c"; NULL if c not mapped to one. */
46 static void * get_drawfunc_by_char(char c);
47
48
49
50 static void free_winconf_data(char id)
51 {
52     struct WinConf * wc = get_winconf_by_id(id);
53     free(wc->title);
54     free_keybindings(wc->kb.kbs);
55     free_win(wc->win);
56 }
57
58
59
60 static void set_winconf_geometry(char id)
61 {
62     struct WinConf * wcp = get_winconf_by_id(id);
63     if      (0 == wcp->height_type)
64     {
65         wcp->height = wcp->win->framesize.y;
66     }
67     else if (1 == wcp->height_type)
68     {
69         wcp->height = wcp->win->framesize.y - world.wmeta.padsize.y + 1;
70     }
71     if      (0 == wcp->width_type)
72     {
73         wcp->width = wcp->win->framesize.x;
74     }
75     else if (1 == wcp->width_type)
76     {
77         wcp->width = wcp->win->framesize.x - world.wmeta.padsize.x;
78     }
79 }
80
81
82
83 static void * get_drawfunc_by_char(char c)
84 {
85
86     if      ('c' == c)
87     {
88         return draw_win_inventory;
89     }
90     else if ('i' == c)
91     {
92         return draw_win_info;
93     }
94     else if ('l' == c)
95     {
96         return draw_win_log;
97     }
98     else if ('k' == c)
99     {
100         return draw_win_available_keybindings;
101     }
102     else if ('m' == c)
103     {
104         return draw_win_map;
105     }
106     else if ('0' == c)
107     {
108         return draw_win_keybindings_global;
109     }
110     else if ('1' == c)
111     {
112         return draw_win_keybindings_winconf_geometry;
113     }
114     else if ('2' == c)
115     {
116         return draw_win_keybindings_winconf_keybindings;
117     }
118     return NULL;
119 }
120
121
122
123 extern void init_win_from_winconf(char id)
124 {
125     char * err = "get_drawfunc_by_char() returns NULL to init_win_from_file().";
126     struct WinConf * winconf = get_winconf_by_id(id);
127     void * f = get_drawfunc_by_char(winconf->draw);
128     exit_err(NULL == f, err);
129     init_win(&winconf->win, winconf->title, winconf->height, winconf->width, f);
130 }
131
132
133
134 static struct WinConf * get_winconf_by_id(char id)
135 {
136     uint8_t i = 0;
137     while (1)
138     {
139         if (id == world.winconf_db.winconfs[i].id)
140         {
141             return &world.winconf_db.winconfs[i];
142         }
143         i++;
144     }
145 }
146
147
148
149 extern struct WinConf * get_winconf_by_win(struct Win * win)
150 {
151     uint8_t i = 0;
152     while (1)
153     {
154         if (win == world.winconf_db.winconfs[i].win)
155         {
156             return &world.winconf_db.winconfs[i];
157         }
158         i++;
159     }
160 }
161
162
163
164 extern struct Win * get_win_by_id(char id)
165 {
166     struct WinConf * wc = get_winconf_by_id(id);
167     return wc->win;
168 }
169
170
171
172 extern uint8_t read_winconf_from_file(char * line, uint32_t linemax, FILE * file)
173 {
174     char * f_name = "read_winconf_from_file()";
175     int test = try_fgetc(file, f_name);
176     if (EOF == test)
177     {
178         return 0;
179     }
180     struct WinConf winconf;
181     winconf.id = (char) test;
182     winconf.draw = winconf.id;
183     try_fgetc(file, f_name);
184     try_fgets(line, linemax + 1, file, f_name);
185     winconf.title = try_malloc(strlen(line), f_name);
186     memcpy(winconf.title, line, strlen(line) - 1);  /* Eliminate newline char */
187     winconf.title[strlen(line) - 1] = '\0';         /* char at end of string. */
188     try_fgets(line, linemax + 1, file, f_name);
189     winconf.height = atoi(line);
190     winconf.height_type = (0 >= winconf.height);
191     try_fgets(line, linemax + 1, file, f_name);
192     winconf.width = atoi(line);
193     winconf.width_type = (0 >= winconf.width);
194     read_keybindings_from_file(line, linemax, file, &winconf.kb);
195     winconf.win = NULL;
196     winconf.view = 0;
197     winconf.center.y = 0;
198     winconf.center.x = 0;
199     if (world.winconf_db.ids)
200     {
201         uint8_t old_ids_size = strlen(world.winconf_db.ids);
202         char * new_ids = try_malloc(old_ids_size + 1 + 1, f_name);
203         sprintf(new_ids, "%s%c", world.winconf_db.ids, winconf.id);
204         free(world.winconf_db.ids);
205         world.winconf_db.ids = new_ids;
206         uint16_t old_winconfs_size = old_ids_size * sizeof(struct WinConf);
207         uint16_t new_winconfs_size = old_winconfs_size + sizeof(struct WinConf);
208         struct WinConf * new_winconfs = try_malloc(new_winconfs_size, f_name);
209         memcpy(new_winconfs, world.winconf_db.winconfs, old_winconfs_size);
210         new_winconfs[old_ids_size] = winconf;
211         free(world.winconf_db.winconfs);
212         world.winconf_db.winconfs = new_winconfs;
213     }
214     else
215     {
216         world.winconf_db.ids = try_malloc(2, f_name);
217         sprintf(world.winconf_db.ids, "%c", winconf.id);
218         world.winconf_db.winconfs = try_malloc(sizeof(struct WinConf), f_name);
219         *world.winconf_db.winconfs = winconf;
220     }
221     return 1;
222 }
223
224
225
226 extern void write_winconf_of_id_to_file(FILE * file, char c, char * delim)
227 {
228     char * f_name = "write_winconf_of_id_to_file()";
229     struct WinConf * wc = get_winconf_by_id(c);
230     uint8_t size = strlen(wc->title) + 2;
231     if (size < 7)  /* Ensure that at least 5 + 2 char fit into line so that   */
232     {              /* the digit representation of any uint16_t may be stored. */
233         size = 7;
234     }
235     char line[size];
236     sprintf(line, "%c\n", wc->draw);
237     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
238     sprintf(line, "%s\n", wc->title);
239     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
240     sprintf(line, "%d\n", wc->height);
241     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
242     sprintf(line, "%d\n", wc->width);
243     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
244     write_keybindings_to_file(file, &wc->kb, delim);
245 }
246
247
248
249 extern void read_order_wins_visible_active(char * line, uint32_t linemax, FILE * file)
250 {
251     char * f_name = "read_order_wins_visible_active()";
252     char win_order[linemax + 1];
253     try_fgets(win_order, linemax + 1, file, f_name);
254     world.winconf_db.order = try_malloc(linemax, f_name);
255     win_order[strlen(win_order) - 1] = '\0';
256     sprintf(world.winconf_db.order, "%s", win_order);
257     int char_or_eof = try_fgetc(file, f_name);
258     char * err_eof = "fgetc() unexpectedly hitting EOF";
259     exit_trouble(EOF == char_or_eof, f_name, err_eof);
260     world.winconf_db.active = (uint8_t) char_or_eof;
261     exit_trouble(EOF == try_fgetc(file, f_name), f_name, err_eof);
262     try_fgets(line, linemax + 1, file, f_name);
263 }
264
265
266
267 extern void write_order_wins_visible_active(FILE * file, char * delim)
268 {
269     char * f_name = "write_order_wins_visible_active()";
270     char line[strlen(world.winconf_db.ids) + 2];
271     struct Win * w_p = world.wmeta.chain_start;
272     char active = ' ';
273     uint8_t i;
274     for (; NULL != w_p; w_p = w_p->next, i++)
275     {
276         struct WinConf * wc_p = get_winconf_by_win(w_p);
277         line[i] = wc_p->id;
278         if (w_p == world.wmeta.active)
279         {
280             active = wc_p->id;
281         }
282     }
283     line[i] = '\n';
284     line[i + 1] = '\0';
285     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
286     try_fputc(active, file, f_name);
287     try_fputc('\n', file, f_name);
288     try_fwrite(delim, strlen(delim), 1, file, f_name);
289 }
290
291
292
293 extern void free_winconfs()
294 {
295     char id;
296     while (0 != (id = get_next_winconf_id()))
297     {
298         free_winconf_data(id);
299     }
300     free(world.winconf_db.ids);
301     world.winconf_db.ids = NULL;
302     free(world.winconf_db.winconfs);
303     world.winconf_db.winconfs = NULL;
304     free(world.winconf_db.order);
305     world.winconf_db.order = NULL;
306 }
307
308
309
310 extern void init_wins()
311 {
312     char id;
313     while (0 != (id = get_next_winconf_id()))
314     {
315         init_win_from_winconf(id);
316     }
317 }
318
319
320
321 extern void sorted_win_toggle_and_activate()
322 {
323     uint8_t i = 0;
324     for (; i < strlen(world.winconf_db.order); i++)
325     {
326         if (NULL == strchr(world.winconf_db.ids, world.winconf_db.order[i]))
327         {
328             continue;
329         }
330         toggle_window(world.winconf_db.order[i]);
331         if (world.winconf_db.active == (uint8_t) world.winconf_db.order[i])
332         {
333             world.wmeta.active = get_win_by_id(world.winconf_db.order[i]);
334         }
335     }
336 }
337
338
339 extern char get_next_winconf_id()
340 {
341     static uint8_t i = 0;
342     char c = world.winconf_db.ids[i];
343     if (0 == c)
344     {
345         i = 0;
346         return c;
347     }
348     i++;
349     return c;
350 }
351
352
353
354 extern void toggle_window(char id)
355 {
356     struct Win * win = get_win_by_id(id);
357     if (0 == win->prev && world.wmeta.chain_start != win)   /* Win struct is  */
358     {                                                       /* outside chain? */
359         append_win(win);
360     }
361     else
362     {
363         suspend_win(win);
364     }
365 }
366
367
368
369 extern void toggle_winconfig()
370 {
371    struct Win * win = world.wmeta.active;
372    struct WinConf * wcp = get_winconf_by_win(win);
373     if      (0 == wcp->view)
374     {
375         wcp->view     = 1;
376         win->draw     = draw_winconf_geometry;
377         wcp->center   = win->center;
378         win->center.y = 0;
379         win->center.x = 0;
380     }
381     else if (1 == wcp->view)
382     {
383         wcp->view     = 2;
384         win->draw     = draw_winconf_keybindings;
385         win->center.x = 0;
386     }
387     else
388     {
389         wcp->view     = 0;
390         win->draw     = get_drawfunc_by_char(wcp->draw);
391         win->center   = wcp->center;
392     }
393 }
394
395
396
397 extern void toggle_win_size_type(char axis)
398 {
399     struct Win * win = world.wmeta.active;
400     struct WinConf * wcp = get_winconf_by_win(win);
401     if ('y' == axis)
402     {
403         wcp->height_type = (0 == wcp->height_type);
404         set_winconf_geometry(wcp->id);
405         return;
406     }
407     wcp->width_type = (   0 == wcp->width_type
408                        && win->framesize.x <= world.wmeta.padsize.x);
409     set_winconf_geometry(wcp->id);
410 }
411
412
413
414 extern void scroll_pad(char dir)
415 {
416     if      ('+' == dir)
417     {
418         reset_pad_offset(world.wmeta.pad_offset + 1);
419     }
420     else if ('-' == dir)
421     {
422         reset_pad_offset(world.wmeta.pad_offset - 1);
423     }
424 }
425
426
427
428 extern void growshrink_active_window(char change)
429 {
430     if (0 != world.wmeta.active)
431     {
432         struct yx_uint16 size = world.wmeta.active->framesize;
433         if      (change == '-')
434         {
435             size.y--;
436         }
437         else if (change == '+')
438         {
439             size.y++;
440         }
441         else if (change == '_')
442         {
443             size.x--;
444         }
445         else if (change == '*')
446         {
447             size.x++;
448         }
449         resize_active_win(size);
450         struct WinConf * wcp = get_winconf_by_win(world.wmeta.active);
451         if (   1 == wcp->width_type
452             && world.wmeta.active->framesize.x > world.wmeta.padsize.x)
453         {
454             wcp->width_type = 0;
455         }
456         set_winconf_geometry(wcp->id);
457     }
458 }