home · contact · privacy
Got rid of misc.h. Split off remains into array_append.h and control.h.
[plomrogue] / src / client / interface_conf.c
1 /* src/client/interface_conf.c */
2
3 #include "interface_conf.h"
4 #include <ncurses.h> /* delwin() */
5 #include <stdint.h> /* uint8_t, uint32_t */
6 #include <stdio.h> /* FILE, sprintf() */
7 #include <stdlib.h> /* free(), exit() */
8 #include <string.h> /* strlen() */
9 #include <unistd.h> /* global optarg, getopt() */
10 #include "../common/err_try_fgets.h" /* reset_err_try_fgets_counter() */
11 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), textfile_width(),
12                                   * try_fclose_unlink_rename(),
13                                   */
14 #include "../common/rexit.h" /* exit_err() */
15 #include "cleanup.h" /* set_cleanup_flag() */
16 #include "keybindings.h" /* read_keybindings_from_file(),
17                           * write_keybindings_to_file()
18                           */
19 #include "map.h" /* map_center() */
20 #include "wincontrol.h" /* toggle_window() */
21 #include "windows.h" /* free_winDB(), make_v_screen_and_init_win_sizes(),
22                       * read_winconf_from_file(), write_winconf_of_id_to_file(),
23                       */
24 #include "world.h" /* global world */
25
26
27
28 extern void obey_argv(int argc, char * argv[])
29 {
30     int opt;
31     while (-1 != (opt = getopt(argc, argv, "i:")))
32     {
33         if      ('i' == opt)
34         {
35             world.path_interface = optarg;
36         }
37         else
38         {
39             exit(EXIT_FAILURE);
40         }
41     }
42 }
43
44
45
46 extern void save_interface_conf()
47 {
48     char * f_name = "save_interface_conf()";
49     char * path = world.path_interface;
50     char path_tmp[strlen(path) + 4 + 1];
51     sprintf(path_tmp, "%s_tmp", path);
52     FILE * file = try_fopen(path_tmp, "w", f_name);
53     write_keybindings_to_file(file, &world.kb_global);
54     write_keybindings_to_file(file, &world.kb_wingeom);
55     write_keybindings_to_file(file, &world.kb_winkeys);
56     write_order_wins_visible_active(file);
57     uint8_t i;
58     for (i = 0; i < strlen(world.winDB.ids); i++)
59     {
60         write_winconf_of_id_to_file(file, world.winDB.ids[i]);
61     }
62     try_fclose_unlink_rename(file, path_tmp, path, f_name);
63 }
64
65
66
67 extern void load_interface_conf()
68 {
69     char * f_name = "load_interface_conf()";
70
71     /* Read keybindings and WincConf DB from interface config file. */
72     reset_err_try_fgets_counter();
73     FILE * file = try_fopen(world.path_interface, "r", f_name);
74     uint32_t linemax = textfile_width(file);
75     char line[linemax + 1];
76     read_keybindings_from_file(line, linemax, file, &world.kb_global);
77     read_keybindings_from_file(line, linemax, file, &world.kb_wingeom);
78     read_keybindings_from_file(line, linemax, file, &world.kb_winkeys);
79     char active_tmp;
80     char * order_tmp;
81     read_order_wins_visible_active(line, linemax, file, &order_tmp,&active_tmp);
82     while (read_winconf_from_file(line, linemax, file));
83     try_fclose(file, f_name);
84
85     /* Check that windows of all legal IDs have been initalized. The validity of
86      * this test relies on read_winconf_from_file() failing on duplicates. Only
87      * on success initialize the windows as visible, to enable safe cleaning up.
88      */
89     char * err = "Failed to initialize all expected windows.";
90     exit_err(strlen(world.winDB.legal_ids) != strlen(world.winDB.ids), err);
91     world.winDB.active = active_tmp;
92     world.winDB.order = order_tmp;
93
94     /* Build windows as defined by read interface data and toggle them on. */
95     make_v_screen_and_init_win_sizes();
96     char tmp_active = world.winDB.active;
97     char tmp_order[strlen(world.winDB.order) + 1];
98     sprintf(tmp_order, "%s", world.winDB.order);
99     world.winDB.order[0] = '\0';
100     uint8_t i;
101     for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
102     world.winDB.active = tmp_active;
103
104     /* So that the interface config data and the window structs get freed. */
105     set_cleanup_flag(CLEANUP_INTERFACE);
106 }
107
108
109
110 extern void unload_interface_conf()
111 {
112     free(world.kb_global.kbs);
113     world.kb_global.kbs = NULL;
114     free(world.kb_wingeom.kbs);
115     world.kb_wingeom.kbs = NULL;
116     free(world.kb_winkeys.kbs);
117     world.kb_winkeys.kbs = NULL;
118     while ('\0' != world.winDB.active)
119     {
120         toggle_window(world.winDB.active);
121     }
122     free_winDB();
123     delwin(world.winDB.v_screen);
124 }
125
126
127
128 extern void reload_interface_conf()
129 {
130     unload_interface_conf();
131     load_interface_conf();
132     map_center();
133     world.winDB.v_screen_offset = 0;
134 }