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