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