home · contact · privacy
Repaired erroneous previous commit and added check for validity of draw function...
[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 #include "dirent.h" /* for opendir(), closedir(), readdir() */
25 #include "errno.h" /* for errno */
26
27
28
29 /* Return string "prefix" + "id"; malloc()'s string, remember to call free()! */
30 static char * string_prefixed_id(struct World * world, char * prefix, char id);
31
32
33
34 /* Create Winconf, init ->view/height_type/width_type to 0, ->id to "id". */
35 static void create_winconf(char id, struct WinConf * wcp);
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, draw function, size of window identified by "id" to conffile. */
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
58 /* Get (Win->_draw) function identified by "c"; NULL if c not mapped to one. */
59 static void * get_drawfunc_by_char(char c);
60
61 /* Iterate over bytes of world->winconf_ids array. Re-start after null byte. */
62 static char get_next_winconf_id(struct World * world);
63
64
65
66 static char * string_prefixed_id(struct World * world, char * prefix, char id)
67 {
68     uint8_t size = strlen(prefix) + 2;
69     char * path = try_malloc(size, world, "string_prefixed_id()");
70     sprintf(path, "%s_", prefix);
71     path[size - 2] = id;
72     return path;
73 }
74
75
76
77 static void create_winconf(char id, struct WinConf * wcp)
78 {
79     wcp->id = id;
80     wcp->view = 0;
81     wcp->height_type = 0;
82     wcp->width_type = 0;
83 }
84
85
86
87 static void init_winconf_from_file(struct World * world, char id)
88 {
89     char * f_name = "init_winconf_from_file()";
90
91     char * path = string_prefixed_id(world, "config/windows/Win_", id);
92     FILE * file = try_fopen(path, "r", world, f_name);
93     free(path);
94     uint16_t linemax = get_linemax(file, world, f_name);
95     char line[linemax + 1];
96
97     struct WinConf * winconf = get_winconf_by_id(world, id);
98     try_fgets(line, linemax + 1, file, world, f_name);
99     winconf->title = try_malloc(strlen(line), world, f_name);
100     memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */
101     winconf->title[strlen(line) - 1] = '\0';        /* char at end of string. */
102
103     try_fgets(line, linemax + 1, file, world, f_name);
104     winconf->draw = line[0];
105
106     try_fgets(line, linemax + 1, file, world, f_name);
107     winconf->height = atoi(line);
108     if (0 >= winconf->height)
109     {
110         winconf->height_type = 1;
111     }
112     try_fgets(line, linemax + 1, file, world, f_name);
113     winconf->width = atoi(line);
114     if (0 >= winconf->width)
115     {
116         winconf->width_type = 1;
117     }
118
119     try_fclose(file, world, f_name);
120 }
121
122
123
124 static void init_win_from_winconf(struct World * world, char id)
125 {
126     char * tmp = "Trouble in init_win_from_file() with init_win() (win id: _).";
127     char * err = try_malloc(strlen(tmp) + 1, world, "init_win_from_file()");
128     memcpy(err, tmp, strlen(tmp) + 1);
129     err[strlen(tmp) - 3] = id;
130     struct WinConf * winconf = get_winconf_by_id(world, id);
131     void * f = get_drawfunc_by_char(winconf->draw);
132     exit_err(NULL == f, world, err);
133     exit_err(init_win(world->wmeta, &winconf->win, winconf->title,
134                       winconf->height, winconf->width, world, f),
135              world, err);
136     free(err);
137 }
138
139
140
141 extern void save_win_config(struct World * world, char id)
142 {
143     char * f_name = "save_win_config()";
144
145     char * path_tmp = string_prefixed_id(world, "config/windows/Win_tmp_", id);
146     FILE * file = try_fopen(path_tmp, "w", world, f_name);
147
148     struct WinConf * wc = get_winconf_by_id(world, id);
149     uint8_t size = strlen(wc->title) + 2;
150     if (size < 7)
151     {
152         size = 7;
153     }
154     char line[size];
155     sprintf(line, "%s\n", wc->title);
156     fwrite(line, sizeof(char), strlen(line), file);
157     sprintf(line, "%c\n", wc->draw);
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     char * path = string_prefixed_id(world, "config/windows/Win_", id);
165     try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
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 void * get_drawfunc_by_char(char c)
212 {
213     if      ('i' == c)
214     {
215         return draw_info_win;
216     }
217     else if ('k' == c)
218     {
219         return draw_keys_win;
220     }
221     else if ('l' == c)
222     {
223         return draw_log_win;
224     }
225     else if ('m' == c)
226     {
227         return draw_map_win;
228     }
229     return NULL;
230 }
231
232
233
234 static char get_next_winconf_id(struct World * world)
235 {
236     static uint8_t i = 0;
237     char c = world->winconf_ids[i];
238     if (0 == c)
239     {
240         i = 0;
241     }
242     else
243     {
244         i++;
245     }
246     return c;
247 }
248
249
250
251 extern struct WinConf * get_winconf_by_win(struct World * world,
252                                            struct Win * win)
253 {
254     uint8_t i = 0;
255     while (1)
256     {
257         if (win == world->winconfs[i].win)
258         {
259             return &world->winconfs[i];
260         }
261         i++;
262     }
263 }
264
265
266
267 extern struct Win * get_win_by_id(struct World * world, char id)
268 {
269     struct WinConf * wc = get_winconf_by_id(world, id);
270     return wc->win;
271 }
272
273
274
275 extern void init_winconfs(struct World * world)
276 {
277     char * f_name = "init_winconfs()";
278     char * err_o = "Trouble in init_winconfs() with opendir().";
279     char * err_r = "Trouble in init_winconfs() with readdir().";
280     char * err_c = "Trouble in init_winconfs() with closedir().";
281
282     DIR * dp = opendir("config/windows");
283     exit_err(NULL == dp, world, err_o);
284     struct dirent * fn;
285     errno = 0;
286     char * winconf_ids = try_malloc(256, world, f_name);
287     uint8_t i = 0;
288     char id;
289     while (NULL != (fn = readdir(dp)))
290     {
291         if (   5 == strlen(fn->d_name)
292             && fn->d_name == strstr(fn->d_name, "Win_"))
293         {
294             id = fn->d_name[4];
295             winconf_ids[i] = id;
296             i++;
297         }
298     }
299     winconf_ids[i] = '\0';
300     exit_err(errno, world, err_r);
301     exit_err(closedir(dp), world, err_c);
302     world->winconf_ids = try_malloc(strlen(winconf_ids + 1), world, f_name);
303     memcpy(world->winconf_ids, winconf_ids, strlen(winconf_ids) + 1);
304     free(winconf_ids);
305
306     struct WinConf * winconfs;
307     winconfs = try_malloc(strlen(world->winconf_ids) * sizeof(struct WinConf),
308                                  world, f_name);
309     i = 0;
310     while (0 != (id = get_next_winconf_id(world)))
311     {
312         create_winconf(id, &winconfs[i]);
313         i++;
314     }
315     world->winconfs = winconfs;
316     while (0 != (id = get_next_winconf_id(world)))
317     {
318         init_winconf_from_file(world, id);
319         i++;
320     }
321 }
322
323
324
325 extern void free_winconf(struct World * world, char id)
326 {
327     struct WinConf * wc = get_winconf_by_id(world, id);
328     free(wc->title);
329 }
330
331
332
333 extern void free_winconfs(struct World * world)
334 {
335     char id;
336     while (0 != (id = get_next_winconf_id(world)))
337     {
338         free_winconf(world, id);
339     }
340     free(world->winconf_ids);
341     free(world->winconfs);
342 }
343
344
345
346 extern void init_wins(struct World * world)
347 {
348     char id;
349     while (0 != (id = get_next_winconf_id(world)))
350     {
351         init_win_from_winconf(world, id);
352     }
353 }
354
355
356
357 extern void free_wins(struct World * world)
358 {
359     char id;
360     while (0 != (id = get_next_winconf_id(world)))
361     {
362         free_win(get_win_by_id(world, id));
363     }
364 }
365
366
367
368 extern void sorted_wintoggle(struct World * world)
369 {
370     char * f_name = "sorted_wintoggle()";
371     char * path = "config/windows/toggle_order";
372     FILE * file = try_fopen(path, "r", world, f_name);
373     uint16_t linemax = get_linemax(file, world, f_name);
374     char win_order[linemax + 1];
375     try_fgets(win_order, linemax + 1, file, world, f_name);
376     try_fclose(file, world, f_name);
377     uint8_t i = 0;
378     for (; i < linemax - 1; i++)
379     {
380         toggle_window(world->wmeta, get_win_by_id(world, win_order[i]));
381     }
382 }
383
384
385
386 extern void reload_win_config(struct World * world)
387 {
388     while (0 != world->wmeta->active)
389     {
390         suspend_win(world->wmeta, world->wmeta->active);
391     }
392     free_wins(world);
393     free_winconfs(world);
394     init_winconfs(world);
395     init_wins(world);
396     sorted_wintoggle(world);
397 }
398
399
400
401 extern void save_win_configs(struct World * world)
402 {
403     char * f_name = "save_win_configs()";
404
405     char id;
406     while (0 != (id = get_next_winconf_id(world)))
407     {
408         save_win_config(world, id);
409     }
410
411     char * path     = "config/windows/toggle_order";
412     char * path_tmp = "config/windows/toggle_order_tmp";
413     FILE * file = try_fopen(path_tmp, "w", world, f_name);
414
415     char line[6];
416     struct Win * w_p = world->wmeta->_chain_start;
417     uint8_t i = 0;
418     while (0 != w_p)
419     {
420         struct WinConf * wc = get_winconf_by_win(world, w_p);
421         line[i] = wc->id;
422         w_p = w_p->_next;
423         i++;
424     }
425     line[i] = '\n';
426     fwrite(line, sizeof(char), strlen(line), file);
427
428     try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
429 }
430
431
432
433 extern uint8_t toggle_window(struct WinMeta * win_meta, struct Win * win)
434 {
435     if (0 != win->frame.curses_win)
436     {
437         return suspend_win(win_meta, win);
438     }
439     else
440     {
441         return append_win(win_meta, win);
442     }
443 }
444
445
446
447 extern void toggle_winconfig(struct World * world, struct Win * win)
448 {
449     struct WinConf * wcp = get_winconf_by_win(world, win);
450     if (0 == wcp->view)
451     {
452         win->_draw = draw_winconf;
453         wcp->view = 1;
454     }
455     else
456     {
457         win->_draw = get_drawfunc_by_char(wcp->draw);
458         wcp->view = 0;
459     }
460 }
461
462
463
464 extern void toggle_win_height_type(struct World * world, struct Win * win)
465 {
466     struct WinConf * wcp = get_winconf_by_win(world, win);
467     if (0 == wcp->height_type)
468     {
469         wcp->height_type = 1;
470     }
471     else
472     {
473         wcp->height_type = 0;
474     }
475     set_winconf(world, wcp->id);
476 }
477
478
479
480 extern void toggle_win_width_type(struct World * world, struct Win * win)
481 {
482     struct WinConf * wcp = get_winconf_by_win(world, win);
483     if (   0 == wcp->width_type
484         && win->frame.size.x <= world->wmeta->padframe.size.x)
485     {
486         wcp->width_type = 1;
487     }
488     else
489     {
490         wcp->width_type = 0;
491     }
492     set_winconf(world, wcp->id);
493 }
494
495
496
497 extern void scroll_pad(struct WinMeta * win_meta, char dir)
498 {
499     if      ('+' == dir)
500     {
501         reset_pad_offset(win_meta, win_meta->pad_offset + 1);
502     }
503     else if ('-' == dir)
504     {
505         reset_pad_offset(win_meta, win_meta->pad_offset - 1);
506     }
507 }
508
509
510
511 extern uint8_t growshrink_active_window(struct World * world, char change)
512 {
513     if (0 != world->wmeta->active)
514     {
515         struct yx_uint16 size = world->wmeta->active->frame.size;
516         if      (change == '-')
517         {
518             size.y--;
519         }
520         else if (change == '+')
521         {
522             size.y++;
523         }
524         else if (change == '_')
525         {
526             size.x--;
527         }
528         else if (change == '*')
529         {
530             size.x++;
531         }
532         uint8_t x = resize_active_win(world->wmeta, size);
533         struct WinConf * wcp = get_winconf_by_win(world, world->wmeta->active);
534         if (   1 == wcp->width_type
535             && world->wmeta->active->frame.size.x
536                > world->wmeta->padframe.size.x)
537         {
538             wcp->width_type = 0;
539         }
540         set_winconf(world, wcp->id);
541         return x;
542     }
543     return 0;
544 }