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