home · contact · privacy
74bd4a202b5a96f73973e4f399985d5b37ec75ca
[plomrogue] / src / wincontrol.c
1 /* wincontrol.c */
2
3 #include "wincontrol.h"
4 #include <stdlib.h> /* for free() */
5 #include <string.h> /* for strlen(), strchr(), strstr() */
6 #include <stdint.h> /* for uint8_t, uint16_t */
7 #include "windows.h" /* for suspend_win(), append_win(), reset_pad_offset(),
8                       * resize_active_win(), init_win(), free_win(), struct Win
9                       */
10 #include "yx_uint16.h" /* for yx_uint16 struct */
11 #include "main.h" /* for world global */
12 #include "readwrite.h" /* for textfile_sizes(), try_fopen(), try_fclose(),
13                         * try_fgets(), try_fclose_unlink_rename(), try_fwrite()
14                         * try_fgetc_noeof()
15                         */
16 #include "rexit.h" /* for exit_err(), exit_trouble() */
17 #include "draw_wins.h" /* for draw_win_map(), draw_win_info(), draw_win_log(),
18                         * draw_win_available_keybindings(),
19                         * draw_win_inventory(), draw_win_keybindings_global(),
20                         * draw_win_keybindings_winconf_geometry(),
21                         * draw_win_keybindings_winconf_keybindings(),
22                         * draw_winconf_geometry(), draw_winconf_keybindings()
23                         */
24 #include "misc.h" /* for try_malloc() */
25 #include "dirent.h" /* for opendir(), closedir(), readdir() */
26 #include "errno.h" /* for errno */
27 #include "keybindings.h" /* for KeyBinding struct, free_keybindings() */
28
29
30
31 /* Return string "prefix" + "id"; malloc()'s string, remember to call free()! */
32 static char * string_prefixed_id(char * prefix, char id);
33
34 /* Initialize Winconf of "id" from appropriate config file.*/
35 static void init_winconf_from_file(char id, struct WinConf * winconf);
36
37 /* Wrapper around init_win() called with values from Winconf of "id". */
38 static void init_win_from_winconf(char id);
39
40 /* Save title, draw function, size of window identified by "id" to conffile. */
41 static void save_win_config(char id);
42
43 /* Free data pointed to inside individual WinConf struct of "id". */
44 static void free_winconf_data(char id);
45
46 /* Write geometry of a window to its WinConf, as positive or negative values
47  * (dependent on state ofWinConf->height_type / WinConf->width_type).
48  */
49 static void set_winconf_geometry(char id);
50
51 /* Get WinConf by "id"; get id of WinConf mothering "win". */
52 static struct WinConf * get_winconf_by_id(char id);
53
54 /* Get (Win->draw) function identified by "c"; NULL if c not mapped to one. */
55 static void * get_drawfunc_by_char(char c);
56
57 /* Iterate over chars of world.winconf_ids array. Re-start after null byte. */
58 static char get_next_winconf_id();
59
60
61
62 static char * string_prefixed_id(char * prefix, char id)
63 {
64     uint8_t size = strlen(prefix) + 2;
65     char * path = try_malloc(size, "string_prefixed_id()");
66     sprintf(path, "%s_", prefix);
67     path[size - 2] = id;
68     return path;
69 }
70
71
72
73 static void init_winconf_from_file(char id, struct WinConf * winconf)
74 {
75     /* Assign WinConf id to filename path, error message context, winconf->id.*/
76     char * tmp = "init_winconf_from_file() on window id '_'";
77     char * context = try_malloc(strlen(tmp) + 1, "init_winconf_from_file()");
78     memcpy(context, tmp, strlen(tmp) + 1);
79     context[strlen(tmp) - 2] = id;
80     char * path = string_prefixed_id("config/windows/Win_", id);
81     winconf->id = id;
82
83     /* Prepare reading in file line by line into "line" array. */
84     FILE * file = try_fopen(path, "r", context);
85     free(path);
86     uint16_t linemax = textfile_sizes(file, NULL/*, context*/);
87     char line[linemax + 1];
88
89     /* Read/determine winconf->title, ->draw, ->height(_type),->width(_type). */
90     try_fgets(line, linemax + 1, file, context);
91     winconf->title = try_malloc(strlen(line), context);
92     memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */
93     winconf->title[strlen(line) - 1] = '\0';        /* char at end of string. */
94     try_fgets(line, linemax + 1, file, context);
95     winconf->draw = line[0];
96     try_fgets(line, linemax + 1, file, context);
97     winconf->height = atoi(line);
98     winconf->height_type = (0 >= winconf->height);
99     try_fgets(line, linemax + 1, file, context);
100     winconf->width = atoi(line);
101     winconf->width_type = (0 >= winconf->width);
102
103     /* Read in window-specific keybindings (winconf->kb). */
104     char command[linemax + 1];
105     char * cmdptr;
106     struct KeyBinding ** loc_last_ptr = &winconf->kb.kbs;
107     * loc_last_ptr = 0;
108     while (fgets(command, linemax + 1, file))
109     {
110         if ('\n' == command[0] || 0 == command[0])
111         {
112             break;
113         }
114         * loc_last_ptr = try_malloc(sizeof(struct KeyBinding), context);
115         struct KeyBinding * kb_p = * loc_last_ptr;
116         kb_p->next = 0;
117         kb_p->key = atoi(command);
118         cmdptr = strchr(command, ' ') + 1;
119         kb_p->name = try_malloc(strlen(cmdptr), context);
120         memcpy(kb_p->name, cmdptr, strlen(cmdptr) - 1);
121         kb_p->name[strlen(cmdptr) - 1] = '\0';
122         loc_last_ptr = & kb_p->next;
123     }
124
125     /* Init remaining values to zero and cleaning up. */
126     winconf->view = 0;
127     winconf->kb.edit = 0;
128     winconf->kb.select = 0;
129     try_fclose(file, context);
130     free(context);
131 }
132
133
134
135 static void init_win_from_winconf(char id)
136 {
137     char * err = "get_drawfunc_by_char() returns NULL to init_win_from_file().";
138     struct WinConf * winconf = get_winconf_by_id(id);
139     void * f = get_drawfunc_by_char(winconf->draw);
140     exit_err(NULL == f, err);
141     init_win(&winconf->win, winconf->title, winconf->height, winconf->width, f);
142 }
143
144
145
146 static void save_win_config(char id)
147 {
148     char * f_name = "save_win_config()";
149
150     /* Prepare atomic file saving. */
151     char * path_tmp = string_prefixed_id("config/windows/Win_tmp_", id);
152     FILE * file = try_fopen(path_tmp, "w", f_name);
153
154     /* Save, line by line, ->title, ->draw, ->height and ->width. */
155     struct WinConf * wc = get_winconf_by_id(id);
156     uint8_t size = strlen(wc->title) + 2;
157     if (size < 7)  /* Ensure that at least 5 + 2 char fit into line so that   */
158     {              /* the digit representation of any uint16_t may be stored. */
159         size = 7;
160     }
161     char line[size];
162     sprintf(line, "%s\n", wc->title);
163     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
164     sprintf(line, "%c\n", wc->draw);
165     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
166     sprintf(line, "%d\n", wc->height);
167     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
168     sprintf(line, "%d\n", wc->width);
169     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
170
171     /* Save window-specific keybindings (->kb.kbs). */
172     uint16_t linemax = 0;
173     struct KeyBinding * kb_p = wc->kb.kbs;
174     while (0 != kb_p)
175     {
176         if (strlen(kb_p->name) > linemax)
177         {
178             linemax = strlen(kb_p->name);
179         }
180         kb_p = kb_p->next;
181     }
182     linemax = linemax + 6;          /* + 6: + 3 digits + whitespace + \n + \0 */
183     char kb_line[linemax];
184     kb_p = wc->kb.kbs;
185     while (0 != kb_p)
186     {
187         sprintf(kb_line, "%d %s\n", kb_p->key, kb_p->name);
188         try_fwrite(kb_line, sizeof(char), strlen(kb_line), file, f_name);
189         kb_p = kb_p->next;
190     }
191
192     /* Finish atomic file saving and clean up. */
193     char * path = string_prefixed_id("config/windows/Win_", id);
194     try_fclose_unlink_rename(file, path_tmp, path, f_name);
195     free(path);
196     free(path_tmp);
197 }
198
199
200
201 static void free_winconf_data(char id)
202 {
203     struct WinConf * wc = get_winconf_by_id(id);
204     free(wc->title);
205     free_keybindings(wc->kb.kbs);
206     free_win(wc->win);
207 }
208
209
210
211 static void set_winconf_geometry(char id)
212 {
213     struct WinConf * wcp = get_winconf_by_id(id);
214     if      (0 == wcp->height_type)
215     {
216         wcp->height = wcp->win->framesize.y;
217     }
218     else if (1 == wcp->height_type)
219     {
220         wcp->height = wcp->win->framesize.y - world.wmeta->padsize.y + 1;
221     }
222     if      (0 == wcp->width_type)
223     {
224         wcp->width = wcp->win->framesize.x;
225     }
226     else if (1 == wcp->width_type)
227     {
228         wcp->width = wcp->win->framesize.x - world.wmeta->padsize.x;
229     }
230 }
231
232
233
234 static struct WinConf * get_winconf_by_id(char id)
235 {
236     uint8_t i = 0;
237     while (1)
238     {
239         if (id == world.winconfs[i].id)
240         {
241             return &world.winconfs[i];
242         }
243         i++;
244     }
245 }
246
247
248
249 static void * get_drawfunc_by_char(char c)
250 {
251     if      ('c' == c)
252     {
253         return draw_win_inventory;
254     }
255     else if ('i' == c)
256     {
257         return draw_win_info;
258     }
259     else if ('l' == c)
260     {
261         return draw_win_log;
262     }
263     else if ('k' == c)
264     {
265         return draw_win_available_keybindings;
266     }
267     else if ('m' == c)
268     {
269         return draw_win_map;
270     }
271     else if ('0' == c)
272     {
273         return draw_win_keybindings_global;
274     }
275     else if ('1' == c)
276     {
277         return draw_win_keybindings_winconf_geometry;
278     }
279     else if ('2' == c)
280     {
281         return draw_win_keybindings_winconf_keybindings;
282     }
283     return NULL;
284 }
285
286
287
288 static char get_next_winconf_id()
289 {
290     static uint8_t i = 0;
291     char c = world.winconf_ids[i];
292     if (0 == c)
293     {
294         i = 0;
295         return c;
296     }
297     i++;
298     return c;
299 }
300
301
302
303 extern struct WinConf * get_winconf_by_win(struct Win * win)
304 {
305     uint8_t i = 0;
306     while (1)
307     {
308         if (win == world.winconfs[i].win)
309         {
310             return &world.winconfs[i];
311         }
312         i++;
313     }
314 }
315
316
317
318 extern struct Win * get_win_by_id(char id)
319 {
320     struct WinConf * wc = get_winconf_by_id(id);
321     return wc->win;
322 }
323
324
325
326 extern void init_winconfs()
327 {
328     char * f_name = "init_winconfs()";
329
330     /* Fill world.winconf_ids with config/windows/Win_* filenames' end chars. */
331     uint8_t max_wins = 255;         /* Maximum number of window ids to store. */
332     DIR * dp = opendir("config/windows");
333     exit_trouble(NULL == dp, f_name, "opendir()");
334     struct dirent * fn;
335     errno = 0;
336     char * winconf_ids = try_malloc(max_wins + 1, f_name);
337     uint8_t i = 0;
338     char id;
339     while (NULL != (fn = readdir(dp)) && i < max_wins)
340     {
341         if (5 == strlen(fn->d_name) && fn->d_name == strstr(fn->d_name, "Win_"))
342         {
343             id = fn->d_name[4];
344             winconf_ids[i] = id;
345             i++;
346         }
347     }
348     winconf_ids[i] = '\0';
349     exit_trouble(errno, f_name, "readdir()");
350     exit_trouble(closedir(dp), f_name, "closedir()");
351     world.winconf_ids = try_malloc(strlen(winconf_ids) + 1, f_name);
352     memcpy(world.winconf_ids, winconf_ids, strlen(winconf_ids) + 1);
353     free(winconf_ids);
354
355     /* Initialize world.winconfs from Win_* files named in world.winconf_ids. */
356     size_t size = strlen(world.winconf_ids) * sizeof(struct WinConf);
357     world.winconfs = try_malloc(size, f_name);
358     i = 0;
359     while (0 != (id = get_next_winconf_id()))
360     {
361         init_winconf_from_file(id, &world.winconfs[i]);
362         i++;
363     }
364 }
365
366
367
368 extern void free_winconfs()
369 {
370     char id;
371     while (0 != (id = get_next_winconf_id()))
372     {
373         free_winconf_data(id);
374     }
375     free(world.winconf_ids);
376     free(world.winconfs);
377 }
378
379
380
381 extern void init_wins()
382 {
383     char id;
384     while (0 != (id = get_next_winconf_id()))
385     {
386         init_win_from_winconf(id);
387     }
388 }
389
390
391
392 extern void sorted_wintoggle_and_activate()
393 {
394     char * f_name = "sorted_wintoggle_and_activate()";
395
396     /* Read from file order of windows to be toggled + active win selection. */
397     char * path = "config/windows/toggle_order_and_active";
398     FILE * file = try_fopen(path, "r", f_name);
399     uint16_t linemax = textfile_sizes(file, NULL);
400     char win_order[linemax + 1];
401     try_fgets(win_order, linemax + 1, file, f_name);
402     uint8_t a = try_fgetc_noeof(file, f_name);
403     try_fclose(file, f_name);
404
405     /* Toggle windows and set active window selection. */
406     uint8_t i = 0;
407     for (; i < strlen(win_order) - 1; i++)
408     {
409         if (NULL == strchr(world.winconf_ids, win_order[i]))
410         {
411             continue;
412         }
413         toggle_window(win_order[i]);
414         if (a == (uint8_t) win_order[i])
415         {
416             world.wmeta->active = get_win_by_id(win_order[i]);
417         }
418     }
419 }
420
421
422
423 extern void save_win_configs()
424 {
425     char * f_name = "save_win_configs()";
426
427     /* Save individual world.winconfs to their proper files. */
428     uint8_t max_wins = 255; /* So many winconf ids fit into world.winconf_ids.*/
429     char id;
430     while (0 != (id = get_next_winconf_id()))
431     {
432         save_win_config(id);
433     }
434
435     /* Save order of windows to toggle on start / which to select as active. */
436     char * path     = "config/windows/toggle_order_and_active";
437     char * path_tmp = "config/windows/toggle_order_and_active_tmp";
438     FILE * file = try_fopen(path_tmp, "w", f_name);
439     char line[max_wins + 2];
440     struct Win * w_p = world.wmeta->chain_start;
441     uint8_t i = 0;
442     while (0 != w_p && i < max_wins)
443     {
444         struct WinConf * wc = get_winconf_by_win(w_p);
445         line[i] = wc->id;
446         w_p = w_p->next;
447         i++;
448     }
449     line[i] = '\n';
450     line[i + 1] = '\0';
451     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
452     if (0 != world.wmeta->active)
453     {
454         struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
455         try_fputc(wc->id, file, f_name);
456     }
457     try_fclose_unlink_rename(file, path_tmp, path, f_name);
458 }
459
460
461
462 extern void toggle_window(char id)
463 {
464     struct Win * win = get_win_by_id(id);
465     if (0 == win->prev && world.wmeta->chain_start != win)  /* Win struct is  */
466     {                                                       /* outside chain? */
467         append_win(win);
468     }
469     else
470     {
471         suspend_win(win);
472     }
473 }
474
475
476
477 extern void toggle_winconfig()
478 {
479    struct Win * win = world.wmeta->active;
480    struct WinConf * wcp = get_winconf_by_win(win);
481     if      (0 == wcp->view)
482     {
483         wcp->view     = 1;
484         win->draw     = draw_winconf_geometry;
485         wcp->center   = win->center;
486         win->center.y = 0;
487         win->center.x = 0;
488     }
489     else if (1 == wcp->view)
490     {
491         wcp->view     = 2;
492         win->draw     = draw_winconf_keybindings;
493         win->center.x = 0;
494     }
495     else
496     {
497         wcp->view     = 0;
498         win->draw     = get_drawfunc_by_char(wcp->draw);
499         win->center   = wcp->center;
500     }
501 }
502
503
504
505 extern void toggle_win_size_type(char axis)
506 {
507     struct Win * win = world.wmeta->active;
508     struct WinConf * wcp = get_winconf_by_win(win);
509     if ('y' == axis)
510     {
511         wcp->height_type = (0 == wcp->height_type);
512         set_winconf_geometry(wcp->id);
513         return;
514     }
515     wcp->width_type = (   0 == wcp->width_type
516                        && win->framesize.x <= world.wmeta->padsize.x);
517     set_winconf_geometry(wcp->id);
518 }
519
520
521
522 extern void scroll_pad(char dir)
523 {
524     if      ('+' == dir)
525     {
526         reset_pad_offset(world.wmeta->pad_offset + 1);
527     }
528     else if ('-' == dir)
529     {
530         reset_pad_offset(world.wmeta->pad_offset - 1);
531     }
532 }
533
534
535
536 extern void growshrink_active_window(char change)
537 {
538     if (0 != world.wmeta->active)
539     {
540         struct yx_uint16 size = world.wmeta->active->framesize;
541         if      (change == '-')
542         {
543             size.y--;
544         }
545         else if (change == '+')
546         {
547             size.y++;
548         }
549         else if (change == '_')
550         {
551             size.x--;
552         }
553         else if (change == '*')
554         {
555             size.x++;
556         }
557         resize_active_win(size);
558         struct WinConf * wcp = get_winconf_by_win(world.wmeta->active);
559         if (   1 == wcp->width_type
560             && world.wmeta->active->framesize.x > world.wmeta->padsize.x)
561         {
562             wcp->width_type = 0;
563         }
564         set_winconf_geometry(wcp->id);
565     }
566 }