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