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