home · contact · privacy
Mostly cosmetic changes to various file reading/writing functions for greater readibi...
[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 = malloc(linemax);
100     exit_err(NULL == line, world, err_m);
101
102     struct WinConf * winconf = get_winconf_by_id(world, id);
103     exit_err(NULL == fgets(line, linemax, file), world, err_g);
104     exit_err(NULL == (winconf->title = malloc(strlen(line))), world, err_m);
105     memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */
106     winconf->title[strlen(line) - 1] = '\0';        /* char at end of string. */
107     exit_err(NULL == fgets(line, linemax, file), world, err_g);
108     winconf->height = atoi(line);
109     if (0 >= winconf->height)
110     {
111         winconf->height_type = 1;
112     }
113     exit_err(NULL == fgets(line, linemax, file), world, err_g);
114     winconf->width = atoi(line);
115     if (0 >= winconf->width)
116     {
117         winconf->width_type = 1;
118     }
119
120     free(line);
121     exit_err(fclose(file), world, err_c);
122 }
123
124
125
126 static void init_win_from_winconf(struct World * world, char id)
127 {
128     char * err = "Trouble in init_win_from_file() with init_win().";
129     struct WinConf * winconf = get_winconf_by_id(world, id);
130     exit_err(init_win(world->wmeta, &winconf->win, winconf->title,
131                       winconf->height, winconf->width, world, winconf->draw),
132              world, err);
133 }
134
135
136
137 extern void save_win_config(struct World * world, char id)
138 {
139     char * err_o = "Trouble in save_win_config() with fopen().";
140     char * err_m = "Trouble in save_win_config() with malloc().";
141     char * err_c = "Trouble in save_win_config() with fclose().";
142     char * err_u = "Trouble in save_win_config() with unlink().";
143     char * err_r = "Trouble in save_win_config() with rename().";
144
145     char * path_tmp = string_prefixed_id(world, "config/windows/Win_tmp_", id);
146     FILE * file = fopen(path_tmp, "w");
147     exit_err(NULL == file, world, err_o);
148
149     struct WinConf * wc = get_winconf_by_id(world, id);
150     uint8_t size = strlen(wc->title) + 2;
151     if (size < 7)
152     {
153         size = 7;
154     }
155     char * line = malloc(size);
156     exit_err(NULL == line, world, err_m);
157     sprintf(line, "%s\n", wc->title);
158     fwrite(line, sizeof(char), strlen(line), file);
159     sprintf(line, "%d\n", wc->height);
160     fwrite(line, sizeof(char), strlen(line), file);
161     sprintf(line, "%d\n", wc->width);
162     fwrite(line, sizeof(char), strlen(line), file);
163
164     exit_err(fclose(file), world, err_c);
165     char * path = string_prefixed_id(world, "config/windows/Win_", id);
166     if (!access(path, F_OK))
167     {
168         exit_err(unlink(path), world, err_u);
169     }
170     exit_err(rename(path_tmp, path), world, err_r);
171     free(path);
172     free(path_tmp);
173 }
174
175
176
177 static void set_winconf(struct World * world, char id)
178 {
179     struct WinConf * wcp = get_winconf_by_id(world, id);
180     if      (0 == wcp->height_type)
181     {
182         wcp->height = wcp->win->frame.size.y;
183     }
184     else if (1 == wcp->height_type)
185     {
186         wcp->height = wcp->win->frame.size.y - world->wmeta->padframe.size.y
187                       + 1;
188     }
189     if      (0 == wcp->width_type)
190     {
191         wcp->width = wcp->win->frame.size.x;
192     }
193     else if (1 == wcp->width_type)
194     {
195         wcp->width = wcp->win->frame.size.x - world->wmeta->padframe.size.x;
196     }
197 }
198
199
200
201 static struct WinConf * get_winconf_by_id(struct World * world, char id)
202 {
203     uint8_t i = 0;
204     while (1)
205     {
206         if (id == world->winconfs[i].id)
207         {
208             return &world->winconfs[i];
209         }
210         i++;
211     }
212 }
213
214
215
216 static char get_id_by_win(struct World * world, struct Win * win)
217 {
218     struct WinConf * wc = get_winconf_by_win(world, win);
219     return wc->id;
220 }
221
222
223
224 extern struct WinConf * get_winconf_by_win(struct World * world,
225                                            struct Win * win)
226 {
227     uint8_t i = 0;
228     while (1)
229     {
230         if (win == world->winconfs[i].win)
231         {
232             return &world->winconfs[i];
233         }
234         i++;
235     }
236 }
237
238
239
240 extern struct Win * get_win_by_id(struct World * world, char id)
241 {
242     struct WinConf * wc = get_winconf_by_id(world, id);
243     return wc->win;
244 }
245
246
247
248 extern void init_winconfs(struct World * world)
249 {
250     char * err = "Trouble with malloc() in init_winconfs().";
251     struct WinConf * winconfs = malloc(4 * sizeof(struct WinConf));
252     exit_err(NULL == winconfs, world, err);
253     create_winconf('i', &winconfs[0], draw_info_win);
254     create_winconf('k', &winconfs[1], draw_keys_win);
255     create_winconf('l', &winconfs[2], draw_log_win);
256     create_winconf('m', &winconfs[3], draw_map_win);
257     world->winconfs = winconfs;
258     init_winconf_from_file(world, 'i');
259     init_winconf_from_file(world, 'k');
260     init_winconf_from_file(world, 'l');
261     init_winconf_from_file(world, 'm');
262 }
263
264
265
266 extern void free_winconf(struct World * world, char id)
267 {
268     struct WinConf * wc = get_winconf_by_id(world, id);
269     free(wc->title);
270 }
271
272
273
274 extern void free_winconfs(struct World * world)
275 {
276     free_winconf(world, 'i');
277     free_winconf(world, 'k');
278     free_winconf(world, 'l');
279     free_winconf(world, 'm');
280     free(world->winconfs);
281 }
282
283
284
285 extern void init_wins(struct World * world)
286 {
287     init_win_from_winconf(world, 'i');
288     init_win_from_winconf(world, 'k');
289     init_win_from_winconf(world, 'l');
290     init_win_from_winconf(world, 'm');
291 }
292
293
294
295 extern void free_wins(struct World * world)
296 {
297     free_win(get_win_by_id(world, 'i'));
298     free_win(get_win_by_id(world, 'k'));
299     free_win(get_win_by_id(world, 'l'));
300     free_win(get_win_by_id(world, 'm'));
301 }
302
303
304
305 extern void sorted_wintoggle(struct World * world)
306 {
307     char * err_o = "Trouble in sorted_wintoggle() with fopen().";
308     char * err_s = "Trouble in sorted_wintoggle() with textfile_sizes().";
309     char * err_g = "Trouble in sorted_wintoggle() with fgets().";
310     char * err_c = "Trouble in sorted_wintoggle() with fclose().";
311
312     char * path = "config/windows/toggle_order";
313     FILE * file = fopen(path, "r");
314     exit_err(NULL == file, world, err_o);
315
316     uint16_t linemax;
317     exit_err(textfile_sizes(file, &linemax, NULL), world, err_s);
318
319     char win_order[linemax];
320     exit_err(NULL == fgets(win_order, linemax, file), world, err_g);
321     exit_err(fclose(file), world, err_c);
322
323     uint8_t i = 0;
324     for (; i < linemax - 2; i++)
325     {
326         toggle_window(world->wmeta, get_win_by_id(world, win_order[i]));
327     }
328 }
329
330
331
332 extern void reload_win_config(struct World * world)
333 {
334     while (0 != world->wmeta->active)
335     {
336         suspend_win(world->wmeta, world->wmeta->active);
337     }
338     free_wins(world);
339     free_winconfs(world);
340     init_winconfs(world);
341     init_wins(world);
342     sorted_wintoggle(world);
343 }
344
345
346
347 extern void save_win_configs(struct World * world)
348 {
349     save_win_config(world, 'i');
350     save_win_config(world, 'k');
351     save_win_config(world, 'l');
352     save_win_config(world, 'm');
353
354     char * err_o = "Trouble in save_win_configs() with fopen().";
355     char * err_m = "Trouble in save_win_configs() with calloc().";
356     char * err_c = "Trouble in save_win_configs() with fclose().";
357     char * err_u = "Trouble in save_win_configs() with unlink().";
358     char * err_r = "Trouble in save_win_configs() with rename().";
359
360     char * path     = "config/windows/toggle_order";
361     char * path_tmp = "config/windows/toggle_order_tmp";
362     FILE * file = fopen(path_tmp, "w");
363     exit_err(NULL == file, world, err_o);
364
365     char * line = calloc(6, sizeof(char));
366     exit_err(NULL == line, world, err_m);
367     struct Win * w_p = world->wmeta->_chain_start;
368     uint8_t i = 0;
369     while (0 != w_p)
370     {
371         line[i] = get_id_by_win(world, w_p);
372         w_p = w_p->_next;
373         i++;
374     }
375     line[i] = '\n';
376     fwrite(line, sizeof(char), strlen(line), file);
377
378     exit_err(fclose(file), world, err_c);
379     if (!access(path, F_OK))
380     {
381         exit_err(unlink(path), world, err_u);
382     }
383     exit_err(rename(path_tmp, path), world, err_r);
384 }
385
386
387
388 extern uint8_t toggle_window(struct WinMeta * win_meta, struct Win * win)
389 {
390     if (0 != win->frame.curses_win)
391     {
392         return suspend_win(win_meta, win);
393     }
394     else
395     {
396         return append_win(win_meta, win);
397     }
398 }
399
400
401
402 extern void toggle_winconfig(struct World * world, struct Win * win)
403 {
404     struct WinConf * wcp = get_winconf_by_win(world, win);
405     if (0 == wcp->view)
406     {
407         win->_draw = draw_winconf;
408         wcp->view = 1;
409     }
410     else
411     {
412         win->_draw = wcp->draw;
413         wcp->view = 0;
414     }
415 }
416
417
418
419 extern void toggle_win_height_type(struct World * world, struct Win * win)
420 {
421     struct WinConf * wcp = get_winconf_by_win(world, win);
422     if (0 == wcp->height_type)
423     {
424         wcp->height_type = 1;
425     }
426     else
427     {
428         wcp->height_type = 0;
429     }
430     set_winconf(world, wcp->id);
431 }
432
433
434
435 extern void toggle_win_width_type(struct World * world, struct Win * win)
436 {
437     struct WinConf * wcp = get_winconf_by_win(world, win);
438     if (   0 == wcp->width_type
439         && win->frame.size.x <= world->wmeta->padframe.size.x)
440     {
441         wcp->width_type = 1;
442     }
443     else
444     {
445         wcp->width_type = 0;
446     }
447     set_winconf(world, wcp->id);
448 }
449
450
451
452 extern void scroll_pad(struct WinMeta * win_meta, char dir)
453 {
454     if      ('+' == dir)
455     {
456         reset_pad_offset(win_meta, win_meta->pad_offset + 1);
457     }
458     else if ('-' == dir)
459     {
460         reset_pad_offset(win_meta, win_meta->pad_offset - 1);
461     }
462 }
463
464
465
466 extern uint8_t growshrink_active_window(struct World * world, char change)
467 {
468     if (0 != world->wmeta->active)
469     {
470         struct yx_uint16 size = world->wmeta->active->frame.size;
471         if      (change == '-')
472         {
473             size.y--;
474         }
475         else if (change == '+')
476         {
477             size.y++;
478         }
479         else if (change == '_')
480         {
481             size.x--;
482         }
483         else if (change == '*')
484         {
485             size.x++;
486         }
487         uint8_t x = resize_active_win(world->wmeta, size);
488         struct WinConf * wcp = get_winconf_by_win(world, world->wmeta->active);
489         if (   1 == wcp->width_type
490             && world->wmeta->active->frame.size.x
491                > world->wmeta->padframe.size.x)
492         {
493             wcp->width_type = 0;
494         }
495         set_winconf(world, wcp->id);
496         return x;
497     }
498     return 0;
499 }