home · contact · privacy
Windows are no longer hardcoded. Winconf files now contain a draw function identifier.
[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 window draw functin (Win->_draw) identified by "c". */
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 * 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,
130                       get_drawfunc_by_char(winconf->draw)),
131              world, err);
132 }
133
134
135
136 extern void save_win_config(struct World * world, char id)
137 {
138     char * f_name = "save_win_config()";
139
140     char * path_tmp = string_prefixed_id(world, "config/windows/Win_tmp_", id);
141     FILE * file = try_fopen(path_tmp, "w", world, f_name);
142
143     struct WinConf * wc = get_winconf_by_id(world, id);
144     uint8_t size = strlen(wc->title) + 2;
145     if (size < 7)
146     {
147         size = 7;
148     }
149     char line[size];
150     sprintf(line, "%s\n", wc->title);
151     fwrite(line, sizeof(char), strlen(line), file);
152     sprintf(line, "%c\n", wc->draw);
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     char * path = string_prefixed_id(world, "config/windows/Win_", id);
160     try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
161     free(path);
162     free(path_tmp);
163 }
164
165
166
167 static void set_winconf(struct World * world, char id)
168 {
169     struct WinConf * wcp = get_winconf_by_id(world, id);
170     if      (0 == wcp->height_type)
171     {
172         wcp->height = wcp->win->frame.size.y;
173     }
174     else if (1 == wcp->height_type)
175     {
176         wcp->height = wcp->win->frame.size.y - world->wmeta->padframe.size.y
177                       + 1;
178     }
179     if      (0 == wcp->width_type)
180     {
181         wcp->width = wcp->win->frame.size.x;
182     }
183     else if (1 == wcp->width_type)
184     {
185         wcp->width = wcp->win->frame.size.x - world->wmeta->padframe.size.x;
186     }
187 }
188
189
190
191 static struct WinConf * get_winconf_by_id(struct World * world, char id)
192 {
193     uint8_t i = 0;
194     while (1)
195     {
196         if (id == world->winconfs[i].id)
197         {
198             return &world->winconfs[i];
199         }
200         i++;
201     }
202 }
203
204
205
206 static void * get_drawfunc_by_char(char c)
207 {
208     if      ('i' == c)
209     {
210         return draw_info_win;
211     }
212     else if ('k' == c)
213     {
214         return draw_keys_win;
215     }
216     else if ('l' == c)
217     {
218         return draw_log_win;
219     }
220     else if ('m' == c)
221     {
222         return draw_map_win;
223     }
224     return NULL;
225 }
226
227
228
229 static char get_next_winconf_id(struct World * world)
230 {
231     static uint8_t i = 0;
232     char c = world->winconf_ids[i];
233     if (0 == c)
234     {
235         i = 0;
236     }
237     else
238     {
239         i++;
240     }
241     return c;
242 }
243
244
245
246 extern struct WinConf * get_winconf_by_win(struct World * world,
247                                            struct Win * win)
248 {
249     uint8_t i = 0;
250     while (1)
251     {
252         if (win == world->winconfs[i].win)
253         {
254             return &world->winconfs[i];
255         }
256         i++;
257     }
258 }
259
260
261
262 extern struct Win * get_win_by_id(struct World * world, char id)
263 {
264     struct WinConf * wc = get_winconf_by_id(world, id);
265     return wc->win;
266 }
267
268
269
270 extern void init_winconfs(struct World * world)
271 {
272     char * f_name = "init_winconfs()";
273     char * err_o = "Trouble in init_winconfs() with opendir().";
274     char * err_r = "Trouble in init_winconfs() with readdir().";
275     char * err_c = "Trouble in init_winconfs() with closedir().";
276
277     DIR * dp = opendir("config/windows");
278     exit_err(NULL == dp, world, err_o);
279     struct dirent * fn;
280     errno = 0;
281     char * winconf_ids = try_malloc(256, world, f_name);
282     uint8_t i = 0;
283     char id;
284     while (NULL != (fn = readdir(dp)))
285     {
286         if (   5 == strlen(fn->d_name)
287             && fn->d_name == strstr(fn->d_name, "Win_"))
288         {
289             id = fn->d_name[4];
290             winconf_ids[i] = id;
291             i++;
292         }
293     }
294     winconf_ids[i] = '\0';
295     exit_err(errno, world, err_r);
296     exit_err(closedir(dp), world, err_c);
297     world->winconf_ids = try_malloc(strlen(winconf_ids + 1), world, f_name);
298     memcpy(world->winconf_ids, winconf_ids, strlen(winconf_ids) + 1);
299     free(winconf_ids);
300
301     struct WinConf * winconfs;
302     winconfs = try_malloc(strlen(world->winconf_ids) * sizeof(struct WinConf),
303                                  world, f_name);
304     i = 0;
305     while (0 != (id = get_next_winconf_id(world)))
306     {
307         create_winconf(id, &winconfs[i]);
308         i++;
309     }
310     world->winconfs = winconfs;
311     while (0 != (id = get_next_winconf_id(world)))
312     {
313         init_winconf_from_file(world, id);
314         i++;
315     }
316 }
317
318
319
320 extern void free_winconf(struct World * world, char id)
321 {
322     struct WinConf * wc = get_winconf_by_id(world, id);
323     free(wc->title);
324 }
325
326
327
328 extern void free_winconfs(struct World * world)
329 {
330     char id;
331     while (0 != (id = get_next_winconf_id(world)))
332     {
333         free_winconf(world, id);
334     }
335     free(world->winconf_ids);
336     free(world->winconfs);
337 }
338
339
340
341 extern void init_wins(struct World * world)
342 {
343     char id;
344     while (0 != (id = get_next_winconf_id(world)))
345     {
346         init_win_from_winconf(world, id);
347     }
348 }
349
350
351
352 extern void free_wins(struct World * world)
353 {
354     char id;
355     while (0 != (id = get_next_winconf_id(world)))
356     {
357         free_win(get_win_by_id(world, id));
358     }
359 }
360
361
362
363 extern void sorted_wintoggle(struct World * world)
364 {
365     char * f_name = "sorted_wintoggle()";
366     char * path = "config/windows/toggle_order";
367     FILE * file = try_fopen(path, "r", world, f_name);
368     uint16_t linemax = get_linemax(file, world, f_name);
369     char win_order[linemax + 1];
370     try_fgets(win_order, linemax + 1, file, world, f_name);
371     try_fclose(file, world, f_name);
372     uint8_t i = 0;
373     for (; i < linemax - 1; i++)
374     {
375         toggle_window(world->wmeta, get_win_by_id(world, win_order[i]));
376     }
377 }
378
379
380
381 extern void reload_win_config(struct World * world)
382 {
383     while (0 != world->wmeta->active)
384     {
385         suspend_win(world->wmeta, world->wmeta->active);
386     }
387     free_wins(world);
388     free_winconfs(world);
389     init_winconfs(world);
390     init_wins(world);
391     sorted_wintoggle(world);
392 }
393
394
395
396 extern void save_win_configs(struct World * world)
397 {
398     char * f_name = "save_win_configs()";
399
400     char id;
401     while (0 != (id = get_next_winconf_id(world)))
402     {
403         save_win_config(world, id);
404     }
405
406     char * path     = "config/windows/toggle_order";
407     char * path_tmp = "config/windows/toggle_order_tmp";
408     FILE * file = try_fopen(path_tmp, "w", world, f_name);
409
410     char line[6];
411     struct Win * w_p = world->wmeta->_chain_start;
412     uint8_t i = 0;
413     while (0 != w_p)
414     {
415         struct WinConf * wc = get_winconf_by_win(world, w_p);
416         line[i] = wc->id;
417         w_p = w_p->_next;
418         i++;
419     }
420     line[i] = '\n';
421     fwrite(line, sizeof(char), strlen(line), file);
422
423     try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
424 }
425
426
427
428 extern uint8_t toggle_window(struct WinMeta * win_meta, struct Win * win)
429 {
430     if (0 != win->frame.curses_win)
431     {
432         return suspend_win(win_meta, win);
433     }
434     else
435     {
436         return append_win(win_meta, win);
437     }
438 }
439
440
441
442 extern void toggle_winconfig(struct World * world, struct Win * win)
443 {
444     struct WinConf * wcp = get_winconf_by_win(world, win);
445     if (0 == wcp->view)
446     {
447         win->_draw = draw_winconf;
448         wcp->view = 1;
449     }
450     else
451     {
452         win->_draw = get_drawfunc_by_char(wcp->draw); // wcp->draw;
453         wcp->view = 0;
454     }
455 }
456
457
458
459 extern void toggle_win_height_type(struct World * world, struct Win * win)
460 {
461     struct WinConf * wcp = get_winconf_by_win(world, win);
462     if (0 == wcp->height_type)
463     {
464         wcp->height_type = 1;
465     }
466     else
467     {
468         wcp->height_type = 0;
469     }
470     set_winconf(world, wcp->id);
471 }
472
473
474
475 extern void toggle_win_width_type(struct World * world, struct Win * win)
476 {
477     struct WinConf * wcp = get_winconf_by_win(world, win);
478     if (   0 == wcp->width_type
479         && win->frame.size.x <= world->wmeta->padframe.size.x)
480     {
481         wcp->width_type = 1;
482     }
483     else
484     {
485         wcp->width_type = 0;
486     }
487     set_winconf(world, wcp->id);
488 }
489
490
491
492 extern void scroll_pad(struct WinMeta * win_meta, char dir)
493 {
494     if      ('+' == dir)
495     {
496         reset_pad_offset(win_meta, win_meta->pad_offset + 1);
497     }
498     else if ('-' == dir)
499     {
500         reset_pad_offset(win_meta, win_meta->pad_offset - 1);
501     }
502 }
503
504
505
506 extern uint8_t growshrink_active_window(struct World * world, char change)
507 {
508     if (0 != world->wmeta->active)
509     {
510         struct yx_uint16 size = world->wmeta->active->frame.size;
511         if      (change == '-')
512         {
513             size.y--;
514         }
515         else if (change == '+')
516         {
517             size.y++;
518         }
519         else if (change == '_')
520         {
521             size.x--;
522         }
523         else if (change == '*')
524         {
525             size.x++;
526         }
527         uint8_t x = resize_active_win(world->wmeta, size);
528         struct WinConf * wcp = get_winconf_by_win(world, world->wmeta->active);
529         if (   1 == wcp->width_type
530             && world->wmeta->active->frame.size.x
531                > world->wmeta->padframe.size.x)
532         {
533             wcp->width_type = 0;
534         }
535         set_winconf(world, wcp->id);
536         return x;
537     }
538     return 0;
539 }