home · contact · privacy
Removed some superfluous calloc()/malloc() calls.
[plomrogue] / src / wincontrol.c
1 /* wincontrol.c */
2
3 #include "wincontrol.h"
4 #include <stdlib.h> /* for malloc(), free() */
5 #include <string.h> /* for strlen() */
6 #include <stdint.h> /* for uint8_t, uint16_t */
7 #include <stdio.h> /* for fopen(), fclose(), fwrite() */
8 #include <unistd.h> /* for access(), unlink() */
9 #include "windows.h" /* for suspend_win(), append_win(), reset_pad_offset(),
10                       * resize_active_win(), init_win(), free_win(),
11                       * structs Win, WinMeta
12                       */
13 #include "yx_uint16.h" /* for yx_uint16 struct */
14 #include "main.h" /* for Wins struct */
15 #include "misc.h" /* for textfile_sizes() */
16 #include "rexit.h" /* for exit_err() */
17 #include "main.h" /* for World, Wins structs */
18 #include "draw_wins.h" /* for draw_keys_win(), draw_info_win(), draw_log_win(),
19                         * draw_map_win
20                         */
21
22
23
24 /* Return string "prefix" + "id"; malloc()'s string, remember to call free()! */
25 static char * string_prefixed_id(struct World * world, char * prefix, char id);
26
27
28
29 /* Create Winconf, initialize ->iew/height_type/width_type to 0, ->id to "id"
30  * and ->draw to f.
31  */
32 static void create_winconf(char id, struct WinConf * wcp,
33                            void (* f) (struct Win *));
34
35 /* Initialize Winconf of "id" from appropriate config file.*/
36 static void init_winconf_from_file(struct World * world, char id);
37
38 /* Wrapper around init_win() called with values from Winconf of "id". */
39 static void init_win_from_winconf(struct World * world, char id);
40
41 /* Save title, size of window identified by "id" to its configuration file. */
42 static void save_win_config(struct World * world, char id);
43
44
45
46 /* Write size of a window to its WinConf, as positive or negative values
47  * (dependent on state ofWinConf->height_type / WinConf->width_type).
48  */
49 static void set_winconf(struct World * world, char id);
50
51
52
53 /* Get WinConf by "id"; get id of WinConf mothering "win". */
54 static struct WinConf * get_winconf_by_id(struct World * world, char id);
55 static char get_id_by_win(struct World * world, struct Win * win);
56
57
58
59 static char * string_prefixed_id(struct World * world, char * prefix, char id)
60 {
61     char * err = "Trouble in string_prefixed_id() with malloc().";
62     uint8_t size = strlen(prefix) + 2;
63     char * path = malloc(size);
64     exit_err(NULL == path, world, err);
65     sprintf(path, "%s_", prefix);
66     path[size - 2] = id;
67     return path;
68 }
69
70
71
72 static void create_winconf(char id, struct WinConf * wcp,
73                            void (* f) (struct Win *))
74 {
75     wcp->id = id;
76     wcp->draw = f;
77     wcp->view = 0;
78     wcp->height_type = 0;
79     wcp->width_type = 0;
80 }
81
82
83
84 static void init_winconf_from_file(struct World * world, char id)
85 {
86     char * err_o = "Trouble in init_win_from_file() with fopen().";
87     char * err_m = "Trouble in init_win_from_file() with malloc().";
88     char * err_s = "Trouble in init_win_from_file() with textfile_sizes().";
89     char * err_g = "Trouble in init_win_from_file() with fgets().";
90     char * err_c = "Trouble in init_win_from_file() with fclose().";
91
92     char * path = string_prefixed_id(world, "config/windows/Win_", id);
93     FILE * file = fopen(path, "r");
94     exit_err(NULL == file, world, err_o);
95     free(path);
96
97     uint16_t linemax;
98     exit_err(textfile_sizes(file, &linemax, NULL), world, err_s);
99     char line[linemax];
100
101     struct WinConf * winconf = get_winconf_by_id(world, id);
102     exit_err(NULL == fgets(line, linemax, file), world, err_g);
103     exit_err(NULL == (winconf->title = malloc(strlen(line))), world, err_m);
104     memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */
105     winconf->title[strlen(line) - 1] = '\0';        /* char at end of string. */
106     exit_err(NULL == fgets(line, linemax, file), world, err_g);
107     winconf->height = atoi(line);
108     if (0 >= winconf->height)
109     {
110         winconf->height_type = 1;
111     }
112     exit_err(NULL == fgets(line, linemax, file), world, err_g);
113     winconf->width = atoi(line);
114     if (0 >= winconf->width)
115     {
116         winconf->width_type = 1;
117     }
118
119     exit_err(fclose(file), world, err_c);
120 }
121
122
123
124 static void init_win_from_winconf(struct World * world, char id)
125 {
126     char * err = "Trouble in init_win_from_file() with init_win().";
127     struct WinConf * winconf = get_winconf_by_id(world, id);
128     exit_err(init_win(world->wmeta, &winconf->win, winconf->title,
129                       winconf->height, winconf->width, world, winconf->draw),
130              world, err);
131 }
132
133
134
135 extern void save_win_config(struct World * world, char id)
136 {
137     char * err_o = "Trouble in save_win_config() with fopen().";
138     char * err_c = "Trouble in save_win_config() with fclose().";
139     char * err_u = "Trouble in save_win_config() with unlink().";
140     char * err_r = "Trouble in save_win_config() with rename().";
141
142     char * path_tmp = string_prefixed_id(world, "config/windows/Win_tmp_", id);
143     FILE * file = fopen(path_tmp, "w");
144     exit_err(NULL == file, world, err_o);
145
146     struct WinConf * wc = get_winconf_by_id(world, id);
147     uint8_t size = strlen(wc->title) + 2;
148     if (size < 7)
149     {
150         size = 7;
151     }
152     char line[size];
153     sprintf(line, "%s\n", wc->title);
154     fwrite(line, sizeof(char), strlen(line), file);
155     sprintf(line, "%d\n", wc->height);
156     fwrite(line, sizeof(char), strlen(line), file);
157     sprintf(line, "%d\n", wc->width);
158     fwrite(line, sizeof(char), strlen(line), file);
159
160     exit_err(fclose(file), world, err_c);
161     char * path = string_prefixed_id(world, "config/windows/Win_", id);
162     if (!access(path, F_OK))
163     {
164         exit_err(unlink(path), world, err_u);
165     }
166     exit_err(rename(path_tmp, path), world, err_r);
167     free(path);
168     free(path_tmp);
169 }
170
171
172
173 static void set_winconf(struct World * world, char id)
174 {
175     struct WinConf * wcp = get_winconf_by_id(world, id);
176     if      (0 == wcp->height_type)
177     {
178         wcp->height = wcp->win->frame.size.y;
179     }
180     else if (1 == wcp->height_type)
181     {
182         wcp->height = wcp->win->frame.size.y - world->wmeta->padframe.size.y
183                       + 1;
184     }
185     if      (0 == wcp->width_type)
186     {
187         wcp->width = wcp->win->frame.size.x;
188     }
189     else if (1 == wcp->width_type)
190     {
191         wcp->width = wcp->win->frame.size.x - world->wmeta->padframe.size.x;
192     }
193 }
194
195
196
197 static struct WinConf * get_winconf_by_id(struct World * world, char id)
198 {
199     uint8_t i = 0;
200     while (1)
201     {
202         if (id == world->winconfs[i].id)
203         {
204             return &world->winconfs[i];
205         }
206         i++;
207     }
208 }
209
210
211
212 static char get_id_by_win(struct World * world, struct Win * win)
213 {
214     struct WinConf * wc = get_winconf_by_win(world, win);
215     return wc->id;
216 }
217
218
219
220 extern struct WinConf * get_winconf_by_win(struct World * world,
221                                            struct Win * win)
222 {
223     uint8_t i = 0;
224     while (1)
225     {
226         if (win == world->winconfs[i].win)
227         {
228             return &world->winconfs[i];
229         }
230         i++;
231     }
232 }
233
234
235
236 extern struct Win * get_win_by_id(struct World * world, char id)
237 {
238     struct WinConf * wc = get_winconf_by_id(world, id);
239     return wc->win;
240 }
241
242
243
244 extern void init_winconfs(struct World * world)
245 {
246     char * err = "Trouble with malloc() in init_winconfs().";
247     struct WinConf * winconfs = malloc(4 * sizeof(struct WinConf));
248     exit_err(NULL == winconfs, world, err);
249     create_winconf('i', &winconfs[0], draw_info_win);
250     create_winconf('k', &winconfs[1], draw_keys_win);
251     create_winconf('l', &winconfs[2], draw_log_win);
252     create_winconf('m', &winconfs[3], draw_map_win);
253     world->winconfs = winconfs;
254     init_winconf_from_file(world, 'i');
255     init_winconf_from_file(world, 'k');
256     init_winconf_from_file(world, 'l');
257     init_winconf_from_file(world, 'm');
258 }
259
260
261
262 extern void free_winconf(struct World * world, char id)
263 {
264     struct WinConf * wc = get_winconf_by_id(world, id);
265     free(wc->title);
266 }
267
268
269
270 extern void free_winconfs(struct World * world)
271 {
272     free_winconf(world, 'i');
273     free_winconf(world, 'k');
274     free_winconf(world, 'l');
275     free_winconf(world, 'm');
276     free(world->winconfs);
277 }
278
279
280
281 extern void init_wins(struct World * world)
282 {
283     init_win_from_winconf(world, 'i');
284     init_win_from_winconf(world, 'k');
285     init_win_from_winconf(world, 'l');
286     init_win_from_winconf(world, 'm');
287 }
288
289
290
291 extern void free_wins(struct World * world)
292 {
293     free_win(get_win_by_id(world, 'i'));
294     free_win(get_win_by_id(world, 'k'));
295     free_win(get_win_by_id(world, 'l'));
296     free_win(get_win_by_id(world, 'm'));
297 }
298
299
300
301 extern void sorted_wintoggle(struct World * world)
302 {
303     char * err_o = "Trouble in sorted_wintoggle() with fopen().";
304     char * err_s = "Trouble in sorted_wintoggle() with textfile_sizes().";
305     char * err_g = "Trouble in sorted_wintoggle() with fgets().";
306     char * err_c = "Trouble in sorted_wintoggle() with fclose().";
307
308     char * path = "config/windows/toggle_order";
309     FILE * file = fopen(path, "r");
310     exit_err(NULL == file, world, err_o);
311
312     uint16_t linemax;
313     exit_err(textfile_sizes(file, &linemax, NULL), world, err_s);
314
315     char win_order[linemax];
316     exit_err(NULL == fgets(win_order, linemax, file), world, err_g);
317     exit_err(fclose(file), world, err_c);
318
319     uint8_t i = 0;
320     for (; i < linemax - 2; i++)
321     {
322         toggle_window(world->wmeta, get_win_by_id(world, win_order[i]));
323     }
324 }
325
326
327
328 extern void reload_win_config(struct World * world)
329 {
330     while (0 != world->wmeta->active)
331     {
332         suspend_win(world->wmeta, world->wmeta->active);
333     }
334     free_wins(world);
335     free_winconfs(world);
336     init_winconfs(world);
337     init_wins(world);
338     sorted_wintoggle(world);
339 }
340
341
342
343 extern void save_win_configs(struct World * world)
344 {
345     save_win_config(world, 'i');
346     save_win_config(world, 'k');
347     save_win_config(world, 'l');
348     save_win_config(world, 'm');
349
350     char * err_o = "Trouble in save_win_configs() with fopen().";
351     char * err_c = "Trouble in save_win_configs() with fclose().";
352     char * err_u = "Trouble in save_win_configs() with unlink().";
353     char * err_r = "Trouble in save_win_configs() with rename().";
354
355     char * path     = "config/windows/toggle_order";
356     char * path_tmp = "config/windows/toggle_order_tmp";
357     FILE * file = fopen(path_tmp, "w");
358     exit_err(NULL == file, world, err_o);
359
360     char line[6];
361     struct Win * w_p = world->wmeta->_chain_start;
362     uint8_t i = 0;
363     while (0 != w_p)
364     {
365         line[i] = get_id_by_win(world, w_p);
366         w_p = w_p->_next;
367         i++;
368     }
369     line[i] = '\n';
370     fwrite(line, sizeof(char), strlen(line), file);
371
372     exit_err(fclose(file), world, err_c);
373     if (!access(path, F_OK))
374     {
375         exit_err(unlink(path), world, err_u);
376     }
377     exit_err(rename(path_tmp, path), world, err_r);
378 }
379
380
381
382 extern uint8_t toggle_window(struct WinMeta * win_meta, struct Win * win)
383 {
384     if (0 != win->frame.curses_win)
385     {
386         return suspend_win(win_meta, win);
387     }
388     else
389     {
390         return append_win(win_meta, win);
391     }
392 }
393
394
395
396 extern void toggle_winconfig(struct World * world, struct Win * win)
397 {
398     struct WinConf * wcp = get_winconf_by_win(world, win);
399     if (0 == wcp->view)
400     {
401         win->_draw = draw_winconf;
402         wcp->view = 1;
403     }
404     else
405     {
406         win->_draw = wcp->draw;
407         wcp->view = 0;
408     }
409 }
410
411
412
413 extern void toggle_win_height_type(struct World * world, struct Win * win)
414 {
415     struct WinConf * wcp = get_winconf_by_win(world, win);
416     if (0 == wcp->height_type)
417     {
418         wcp->height_type = 1;
419     }
420     else
421     {
422         wcp->height_type = 0;
423     }
424     set_winconf(world, wcp->id);
425 }
426
427
428
429 extern void toggle_win_width_type(struct World * world, struct Win * win)
430 {
431     struct WinConf * wcp = get_winconf_by_win(world, win);
432     if (   0 == wcp->width_type
433         && win->frame.size.x <= world->wmeta->padframe.size.x)
434     {
435         wcp->width_type = 1;
436     }
437     else
438     {
439         wcp->width_type = 0;
440     }
441     set_winconf(world, wcp->id);
442 }
443
444
445
446 extern void scroll_pad(struct WinMeta * win_meta, char dir)
447 {
448     if      ('+' == dir)
449     {
450         reset_pad_offset(win_meta, win_meta->pad_offset + 1);
451     }
452     else if ('-' == dir)
453     {
454         reset_pad_offset(win_meta, win_meta->pad_offset - 1);
455     }
456 }
457
458
459
460 extern uint8_t growshrink_active_window(struct World * world, char change)
461 {
462     if (0 != world->wmeta->active)
463     {
464         struct yx_uint16 size = world->wmeta->active->frame.size;
465         if      (change == '-')
466         {
467             size.y--;
468         }
469         else if (change == '+')
470         {
471             size.y++;
472         }
473         else if (change == '_')
474         {
475             size.x--;
476         }
477         else if (change == '*')
478         {
479             size.x++;
480         }
481         uint8_t x = resize_active_win(world->wmeta, size);
482         struct WinConf * wcp = get_winconf_by_win(world, world->wmeta->active);
483         if (   1 == wcp->width_type
484             && world->wmeta->active->frame.size.x
485                > world->wmeta->padframe.size.x)
486         {
487             wcp->width_type = 0;
488         }
489         set_winconf(world, wcp->id);
490         return x;
491     }
492     return 0;
493 }