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