home · contact · privacy
Transformed trouble_msg() into exit_err() wrapper exit_trouble(), eliminating some...
[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 /* 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
341     DIR * dp = opendir("config/windows");
342     exit_trouble(NULL == dp, f_name, "opendir()");
343     struct dirent * fn;
344     errno = 0;
345     char * winconf_ids = try_malloc(256, f_name);
346     uint8_t i = 0;
347     char id;
348     while (NULL != (fn = readdir(dp)))
349     {
350         if (5 == strlen(fn->d_name) && fn->d_name == strstr(fn->d_name, "Win_"))
351         {
352             id = fn->d_name[4];
353             winconf_ids[i] = id;
354             i++;
355         }
356     }
357     winconf_ids[i] = '\0';
358     exit_trouble(errno, f_name, "readdir()");
359     exit_trouble(closedir(dp), f_name, "closedir()");
360     world.winconf_ids = try_malloc(strlen(winconf_ids) + 1, f_name);
361     memcpy(world.winconf_ids, winconf_ids, strlen(winconf_ids) + 1);
362     free(winconf_ids);
363
364     struct WinConf * winconfs;
365     winconfs = try_malloc(strlen(world.winconf_ids) * sizeof(struct WinConf),
366                           f_name);
367     i = 0;
368     while (0 != (id = get_next_winconf_id()))
369     {
370         create_winconf(id, &winconfs[i]);
371         i++;
372     }
373     world.winconfs = winconfs;
374     while (0 != (id = get_next_winconf_id()))
375     {
376         init_winconf_from_file(id);
377         i++;
378     }
379 }
380
381
382
383 extern void free_winconfs()
384 {
385     char id;
386     while (0 != (id = get_next_winconf_id()))
387     {
388         free_winconf_data(id);
389     }
390     free(world.winconf_ids);
391     free(world.winconfs);
392 }
393
394
395
396 extern void init_wins()
397 {
398     char id;
399     while (0 != (id = get_next_winconf_id()))
400     {
401         init_win_from_winconf(id);
402     }
403 }
404
405
406
407 extern void sorted_wintoggle_and_activate()
408 {
409     char * f_name = "sorted_wintoggle_and_activate()";
410
411     char * path = "config/windows/toggle_order_and_active";
412     FILE * file = try_fopen(path, "r", f_name);
413     uint16_t linemax = get_linemax(file, f_name);
414
415     char win_order[linemax + 1];
416     try_fgets(win_order, linemax + 1, file, f_name);
417
418     uint8_t a = 0;
419     exit_trouble(read_uint8(file, &a), f_name, "read_uint8()");
420
421     try_fclose(file, f_name);
422
423     uint8_t i = 0;
424     for (; i < linemax - 1; i++)
425     {
426         if (NULL == strchr(world.winconf_ids, win_order[i]))
427         {
428             continue;
429         }
430         toggle_window(win_order[i]);
431
432         if (a == (uint8_t) win_order[i])
433         {
434             world.wmeta->active = get_win_by_id(win_order[i]);
435         }
436     }
437 }
438
439
440
441 extern void save_win_configs()
442 {
443     char * f_name = "save_win_configs()";
444
445     char id;
446     while (0 != (id = get_next_winconf_id()))
447     {
448         save_win_config(id);
449     }
450
451     char * path     = "config/windows/toggle_order_and_active";
452     char * path_tmp = "config/windows/toggle_order_and_active_tmp";
453     FILE * file = try_fopen(path_tmp, "w", f_name);
454
455     char line[6];
456     struct Win * w_p = world.wmeta->chain_start;
457     uint8_t i = 0;
458     while (0 != w_p)
459     {
460         struct WinConf * wc = get_winconf_by_win(w_p);
461         line[i] = wc->id;
462         w_p = w_p->next;
463         i++;
464     }
465     line[i] = '\n';
466     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
467     if (0 != world.wmeta->active)
468     {
469         struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
470         write_uint8(wc->id, file);
471     }
472
473     try_fclose_unlink_rename(file, path_tmp, path, f_name);
474 }
475
476
477
478 extern void toggle_window(char id)
479 {
480     struct Win * win = get_win_by_id(id);
481     if (0 == win->prev && world.wmeta->chain_start != win)  /* Win struct is  */
482     {                                                       /* outside chain. */
483         append_win(win);
484     }
485     else
486     {
487         suspend_win(win);
488     }
489 }
490
491
492
493 extern void toggle_winconfig()
494 {
495    struct Win * win = world.wmeta->active;
496    struct WinConf * wcp = get_winconf_by_win(win);
497     if      (0 == wcp->view)
498     {
499         win->draw = draw_winconf_geometry;
500         wcp->view = 1;
501         wcp->center = win->center;
502         win->center.y = 0;
503         win->center.x = 0;
504     }
505     else if (1 == wcp->view)
506     {
507         win->draw = draw_winconf_keybindings;
508         wcp->view = 2;
509         win->center.x = 0;
510     }
511     else
512     {
513         win->draw = get_drawfunc_by_char(wcp->draw);
514         win->center = wcp->center;
515         wcp->view = 0;
516     }
517 }
518
519
520
521 extern void toggle_win_height_type()
522 {
523     struct Win * win = world.wmeta->active;
524     struct WinConf * wcp = get_winconf_by_win(win);
525     if (0 == wcp->height_type)
526     {
527         wcp->height_type = 1;
528     }
529     else
530     {
531         wcp->height_type = 0;
532     }
533     set_winconf_geometry(wcp->id);
534 }
535
536
537
538 extern void toggle_win_width_type()
539 {
540     struct Win * win = world.wmeta->active;
541     struct WinConf * wcp = get_winconf_by_win(win);
542     if (0 == wcp->width_type && win->framesize.x <= world.wmeta->padsize.x)
543     {
544         wcp->width_type = 1;
545     }
546     else
547     {
548         wcp->width_type = 0;
549     }
550     set_winconf_geometry(wcp->id);
551 }
552
553
554
555 extern void scroll_pad(char dir)
556 {
557     if      ('+' == dir)
558     {
559         reset_pad_offset(world.wmeta->pad_offset + 1);
560     }
561     else if ('-' == dir)
562     {
563         reset_pad_offset(world.wmeta->pad_offset - 1);
564     }
565 }
566
567
568
569 extern void growshrink_active_window(char change)
570 {
571     if (0 != world.wmeta->active)
572     {
573         struct yx_uint16 size = world.wmeta->active->framesize;
574         if      (change == '-')
575         {
576             size.y--;
577         }
578         else if (change == '+')
579         {
580             size.y++;
581         }
582         else if (change == '_')
583         {
584             size.x--;
585         }
586         else if (change == '*')
587         {
588             size.x++;
589         }
590         resize_active_win(size);
591         struct WinConf * wcp = get_winconf_by_win(world.wmeta->active);
592         if (   1 == wcp->width_type
593             && world.wmeta->active->framesize.x > world.wmeta->padsize.x)
594         {
595             wcp->width_type = 0;
596         }
597         set_winconf_geometry(wcp->id);
598     }
599 }