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