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