home · contact · privacy
Removed memory leak in client's reset_windows(). (Previously, not all Win
[plomrogue] / src / client / wincontrol.c
1 /* src/client/wincontrol.c */
2
3 #include "wincontrol.h"
4 #include <errno.h> /* global errno */
5 #include <dirent.h> /* DIR, struct dirent, opendir(), closedir(), readdir() */
6 #include <stddef.h> /* NULL */
7 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
8 #include <stdio.h> /* FILE */
9 #include <stdlib.h> /* free(), atoi() */
10 #include <string.h> /* strlen(), strchr(), strstr(), memcpy() */
11 #include "../common/readwrite.h" /* try_fopen(), textfile_sizes(), try_fgets(),
12                                   * textfile_sizes(), try_fwrite(), try_fgetc(),
13                                   * try_fclose_unlink_rename(), try_fputc()
14                                   */
15 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
16 #include "../common/try_malloc.h" /* try_malloc() */
17 #include "../common/yx_uint16.h" /* struct yx_uint16 */
18 #include "draw_wins.h" /* draw_win_map(), draw_win_info(), draw_win_log(),
19                         * draw_win_available_keybindings(),
20                         * draw_win_inventory(), draw_win_keybindings_global(),
21                         * draw_win_keybindings_winconf_geometry(),
22                         * draw_win_keybindings_winconf_keybindings(),
23                         * draw_winconf_geometry(), draw_winconf_keybindings()
24                         */
25 #include "keybindings.h" /* struct KeyBinding, free_keybindings() */
26 #include "windows.h" /* struct Win, resize_active_win(), reset_pad_offset(),
27                       * append_win(), suspend_win(), init_win(), free_win()
28                       */
29 #include "world.h" /* global world */
30
31
32
33 /* Return string "prefix" + "id"; malloc()'s string, remember to call free()! */
34 static char * string_prefixed_id(char * prefix, char id);
35
36 /* Initialize Winconf of "id" from appropriate config file.*/
37 static void init_winconf_from_file(char id, struct WinConf * winconf);
38
39 /* Wrapper around init_win() called with values from Winconf of "id". */
40 static void init_win_from_winconf(char id);
41
42 /* Save title, draw function, size of window identified by "id" to conffile. */
43 static void save_win_config(char id);
44
45 /* Free data pointed to inside individual WinConf struct of "id". */
46 static void free_winconf_data(char id);
47
48 /* Write geometry 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_geometry(char id);
52
53 /* Get WinConf by "id"; get id of WinConf mothering "win". */
54 static struct WinConf * get_winconf_by_id(char id);
55
56 /* Get (Win->draw) function identified by "c"; NULL if c not mapped to one. */
57 static void * get_drawfunc_by_char(char c);
58
59
60
61 static char * string_prefixed_id(char * prefix, char id)
62 {
63     uint8_t size = strlen(prefix) + 2;
64     char * path = try_malloc(size, "string_prefixed_id()");
65     sprintf(path, "%s_", prefix);
66     path[size - 2] = id;
67     return path;
68 }
69
70
71
72 static void init_winconf_from_file(char id, struct WinConf * winconf)
73 {
74     /* Assign WinConf id to filename path, error message context, winconf->id.*/
75     char * tmp = "init_winconf_from_file() on window id '_'";
76     char * context = try_malloc(strlen(tmp) + 1, "init_winconf_from_file()");
77     memcpy(context, tmp, strlen(tmp) + 1);
78     context[strlen(tmp) - 2] = id;
79     char * path = string_prefixed_id("confclient/windows/Win_", id);
80     winconf->id = id;
81
82     /* Prepare reading in file line by line into "line" array. */
83     FILE * file = try_fopen(path, "r", context);
84     free(path);
85     uint32_t linemax = textfile_sizes(file, NULL);
86     char line[linemax + 1];
87
88     /* Read/determine winconf->title, ->draw, ->height(_type),->width(_type). */
89     try_fgets(line, linemax + 1, file, context);
90     winconf->title = try_malloc(strlen(line), context);
91     memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */
92     winconf->title[strlen(line) - 1] = '\0';        /* char at end of string. */
93     try_fgets(line, linemax + 1, file, context);
94     winconf->draw = line[0];
95     try_fgets(line, linemax + 1, file, context);
96     winconf->height = atoi(line);
97     winconf->height_type = (0 >= winconf->height);
98     try_fgets(line, linemax + 1, file, context);
99     winconf->width = atoi(line);
100     winconf->width_type = (0 >= winconf->width);
101
102     /* Read in window-specific keybindings (winconf->kb). */
103     char command[linemax + 1];
104     char * cmdptr;
105     struct KeyBinding ** loc_last_ptr = &winconf->kb.kbs;
106     * loc_last_ptr = 0;
107     while (try_fgets(command, linemax + 1, file, context))
108     {
109         if ('\n' == command[0] || 0 == command[0])
110         {
111             break;
112         }
113         * loc_last_ptr = try_malloc(sizeof(struct KeyBinding), context);
114         struct KeyBinding * kb_p = * loc_last_ptr;
115         kb_p->next = 0;
116         kb_p->key = atoi(command);
117         cmdptr = strchr(command, ' ') + 1;
118         cmdptr[strlen(cmdptr) - 1] = '\0';
119         kb_p->command = get_command(cmdptr);
120         loc_last_ptr = & kb_p->next;
121     }
122
123     /* Init remaining values to zero and cleaning up. */
124     winconf->view = 0;
125     winconf->kb.edit = 0;
126     winconf->kb.select = 0;
127     try_fclose(file, context);
128     free(context);
129 }
130
131
132
133 static void init_win_from_winconf(char id)
134 {
135     char * err = "get_drawfunc_by_char() returns NULL to init_win_from_file().";
136     struct WinConf * winconf = get_winconf_by_id(id);
137     void * f = get_drawfunc_by_char(winconf->draw);
138     exit_err(NULL == f, err);
139     init_win(&winconf->win, winconf->title, winconf->height, winconf->width, f);
140 }
141
142
143
144 static void save_win_config(char id)
145 {
146     char * f_name = "save_win_config()";
147
148     /* Prepare atomic file saving. */
149     char * path_tmp = string_prefixed_id("confclient/windows/Win_tmp_", id);
150     FILE * file = try_fopen(path_tmp, "w", f_name);
151
152     /* Save, line by line, ->title, ->draw, ->height and ->width. */
153     struct WinConf * wc = get_winconf_by_id(id);
154     uint8_t size = strlen(wc->title) + 2;
155     if (size < 7)  /* Ensure that at least 5 + 2 char fit into line so that   */
156     {              /* the digit representation of any uint16_t may be stored. */
157         size = 7;
158     }
159     char line[size];
160     sprintf(line, "%s\n", wc->title);
161     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
162     sprintf(line, "%c\n", wc->draw);
163     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
164     sprintf(line, "%d\n", wc->height);
165     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
166     sprintf(line, "%d\n", wc->width);
167     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
168
169     /* Save window-specific keybindings (->kb.kbs). */
170     uint16_t linemax = 0;
171     struct KeyBinding * kb_p = wc->kb.kbs;
172     while (0 != kb_p)
173     {
174         if (strlen(kb_p->command->dsc_short) > linemax)
175         {
176             linemax = strlen(kb_p->command->dsc_short);
177         }
178         kb_p = kb_p->next;
179     }
180     linemax = linemax + 6;          /* + 6: + 3 digits + whitespace + \n + \0 */
181     char kb_line[linemax];
182     kb_p = wc->kb.kbs;
183     while (0 != kb_p)
184     {
185         sprintf(kb_line, "%d %s\n", kb_p->key, kb_p->command->dsc_short);
186         try_fwrite(kb_line, sizeof(char), strlen(kb_line), file, f_name);
187         kb_p = kb_p->next;
188     }
189
190     /* Finish atomic file saving and clean up. */
191     char * path = string_prefixed_id("confclient/windows/Win_", id);
192     try_fclose_unlink_rename(file, path_tmp, path, f_name);
193     free(path);
194     free(path_tmp);
195 }
196
197
198
199 static void free_winconf_data(char id)
200 {
201     struct WinConf * wc = get_winconf_by_id(id);
202     free(wc->title);
203     free_keybindings(wc->kb.kbs);
204     free_win(wc->win);
205 }
206
207
208
209 static void set_winconf_geometry(char id)
210 {
211     struct WinConf * wcp = get_winconf_by_id(id);
212     if      (0 == wcp->height_type)
213     {
214         wcp->height = wcp->win->framesize.y;
215     }
216     else if (1 == wcp->height_type)
217     {
218         wcp->height = wcp->win->framesize.y - world.wmeta.padsize.y + 1;
219     }
220     if      (0 == wcp->width_type)
221     {
222         wcp->width = wcp->win->framesize.x;
223     }
224     else if (1 == wcp->width_type)
225     {
226         wcp->width = wcp->win->framesize.x - world.wmeta.padsize.x;
227     }
228 }
229
230
231
232 static struct WinConf * get_winconf_by_id(char id)
233 {
234     uint8_t i = 0;
235     while (1)
236     {
237         if (id == world.winconf_db.winconfs[i].id)
238         {
239             return &world.winconf_db.winconfs[i];
240         }
241         i++;
242     }
243 }
244
245
246
247 static void * get_drawfunc_by_char(char c)
248 {
249
250     if      ('c' == c)
251     {
252         return draw_win_inventory;
253     }
254     else if ('i' == c)
255     {
256         return draw_win_info;
257     }
258     else if ('l' == c)
259     {
260         return draw_win_log;
261     }
262     else if ('k' == c)
263     {
264         return draw_win_available_keybindings;
265     }
266     else if ('m' == c)
267     {
268         return draw_win_map;
269     }
270     else if ('0' == c)
271     {
272         return draw_win_keybindings_global;
273     }
274     else if ('1' == c)
275     {
276         return draw_win_keybindings_winconf_geometry;
277     }
278     else if ('2' == c)
279     {
280         return draw_win_keybindings_winconf_keybindings;
281     }
282     return NULL;
283 }
284
285
286
287 extern struct WinConf * get_winconf_by_win(struct Win * win)
288 {
289     uint8_t i = 0;
290     while (1)
291     {
292         if (win == world.winconf_db.winconfs[i].win)
293         {
294             return &world.winconf_db.winconfs[i];
295         }
296         i++;
297     }
298 }
299
300
301
302 extern struct Win * get_win_by_id(char id)
303 {
304     struct WinConf * wc = get_winconf_by_id(id);
305     return wc->win;
306 }
307
308
309
310 extern void init_winconfs()
311 {
312     char * f_name = "init_winconfs()";
313
314     /* Fill world.winconf_db.winconf_ids with confclient/windows/Win_*
315      * filenames' end chars.
316      */
317     uint8_t max_wins = 255;         /* Maximum number of window ids to store. */
318     DIR * dp = opendir("confclient/windows");
319     exit_trouble(NULL == dp, f_name, "opendir()");
320     struct dirent * fn;
321     errno = 0;
322     char * winconf_ids = try_malloc(max_wins + 1, f_name);
323     uint8_t i = 0;
324     char id;
325     while (NULL != (fn = readdir(dp)) && i < max_wins)
326     {
327         if (5 == strlen(fn->d_name) && fn->d_name == strstr(fn->d_name, "Win_"))
328         {
329             id = fn->d_name[4];
330             winconf_ids[i] = id;
331             i++;
332         }
333     }
334     winconf_ids[i] = '\0';
335     exit_trouble(errno, f_name, "readdir()");
336     exit_trouble(closedir(dp), f_name, "closedir()");
337     world.winconf_db.winconf_ids = try_malloc(strlen(winconf_ids) + 1, f_name);
338     memcpy(world.winconf_db.winconf_ids, winconf_ids, strlen(winconf_ids) + 1);
339     free(winconf_ids);
340
341     /* Initialize world.winconf_db.winconfs from Win_* files named in
342      * world.winconf_db.winconf_ids.
343      */
344     size_t size = strlen(world.winconf_db.winconf_ids) * sizeof(struct WinConf);
345     world.winconf_db.winconfs = try_malloc(size, f_name);
346     i = 0;
347     while (0 != (id = get_next_winconf_id()))
348     {
349         init_winconf_from_file(id, &world.winconf_db.winconfs[i]);
350         i++;
351     }
352 }
353
354
355
356 extern void free_winconfs()
357 {
358     char id;
359     while (0 != (id = get_next_winconf_id()))
360     {
361         free_winconf_data(id);
362     }
363     free(world.winconf_db.winconf_ids);
364     free(world.winconf_db.winconfs);
365 }
366
367
368
369 extern void init_wins()
370 {
371     char id;
372     while (0 != (id = get_next_winconf_id()))
373     {
374         init_win_from_winconf(id);
375     }
376 }
377
378
379
380 extern void sorted_wintoggle_and_activate()
381 {
382     char * f_name = "sorted_wintoggle_and_activate()";
383
384     /* Read from file order of windows to be toggled + active win selection. */
385     char * path = "confclient/windows/toggle_order_and_active";
386     FILE * file = try_fopen(path, "r", f_name);
387     uint32_t linemax = textfile_sizes(file, NULL);
388     char win_order[linemax + 1];
389     try_fgets(win_order, linemax + 1, file, f_name);
390     int char_or_eof = try_fgetc(file, f_name);
391     exit_trouble(EOF==char_or_eof, f_name, "fgetc() unexpectedly hitting EOF");
392     uint8_t a = (uint8_t) char_or_eof;
393     try_fclose(file, f_name);
394
395     /* Toggle windows and set active window selection. */
396     uint8_t i = 0;
397     for (; i < strlen(win_order) - 1; i++)
398     {
399         if (NULL == strchr(world.winconf_db.winconf_ids, win_order[i]))
400         {
401             continue;
402         }
403         toggle_window(win_order[i]);
404         if (a == (uint8_t) win_order[i])
405         {
406             world.wmeta.active = get_win_by_id(win_order[i]);
407         }
408     }
409 }
410
411
412
413 extern char get_next_winconf_id()
414 {
415     static uint8_t i = 0;
416     char c = world.winconf_db.winconf_ids[i];
417     if (0 == c)
418     {
419         i = 0;
420         return c;
421     }
422     i++;
423     return c;
424 }
425
426
427
428 extern void save_win_configs()
429 {
430     char * f_name = "save_win_configs()";
431
432     /* Save individual world.winconf_db.winconfs to their proper files. */
433     uint8_t max_wins = 255;     /* n of WinConf fitting into world.winconf_db */
434     char id;
435     while (0 != (id = get_next_winconf_id()))
436     {
437         save_win_config(id);
438     }
439
440     /* Save order of windows to toggle on start / which to select as active. */
441     char * path     = "confclient/windows/toggle_order_and_active";
442     char * path_tmp = "confclient/windows/toggle_order_and_active_tmp";
443     FILE * file = try_fopen(path_tmp, "w", f_name);
444     char line[max_wins + 2];
445     struct Win * w_p = world.wmeta.chain_start;
446     uint8_t i = 0;
447     while (0 != w_p && i < max_wins)
448     {
449         struct WinConf * wc = get_winconf_by_win(w_p);
450         line[i] = wc->id;
451         w_p = w_p->next;
452         i++;
453     }
454     line[i] = '\n';
455     line[i + 1] = '\0';
456     try_fwrite(line, sizeof(char), strlen(line), file, f_name);
457     if (0 != world.wmeta.active)
458     {
459         struct WinConf * wc = get_winconf_by_win(world.wmeta.active);
460         try_fputc(wc->id, file, f_name);
461     }
462     try_fclose_unlink_rename(file, path_tmp, path, f_name);
463 }
464
465
466
467 extern void toggle_window(char id)
468 {
469     struct Win * win = get_win_by_id(id);
470     if (0 == win->prev && world.wmeta.chain_start != win)   /* Win struct is  */
471     {                                                       /* outside chain? */
472         append_win(win);
473     }
474     else
475     {
476         suspend_win(win);
477     }
478 }
479
480
481
482 extern void toggle_winconfig()
483 {
484    struct Win * win = world.wmeta.active;
485    struct WinConf * wcp = get_winconf_by_win(win);
486     if      (0 == wcp->view)
487     {
488         wcp->view     = 1;
489         win->draw     = draw_winconf_geometry;
490         wcp->center   = win->center;
491         win->center.y = 0;
492         win->center.x = 0;
493     }
494     else if (1 == wcp->view)
495     {
496         wcp->view     = 2;
497         win->draw     = draw_winconf_keybindings;
498         win->center.x = 0;
499     }
500     else
501     {
502         wcp->view     = 0;
503         win->draw     = get_drawfunc_by_char(wcp->draw);
504         win->center   = wcp->center;
505     }
506 }
507
508
509
510 extern void toggle_win_size_type(char axis)
511 {
512     struct Win * win = world.wmeta.active;
513     struct WinConf * wcp = get_winconf_by_win(win);
514     if ('y' == axis)
515     {
516         wcp->height_type = (0 == wcp->height_type);
517         set_winconf_geometry(wcp->id);
518         return;
519     }
520     wcp->width_type = (   0 == wcp->width_type
521                        && win->framesize.x <= world.wmeta.padsize.x);
522     set_winconf_geometry(wcp->id);
523 }
524
525
526
527 extern void scroll_pad(char dir)
528 {
529     if      ('+' == dir)
530     {
531         reset_pad_offset(world.wmeta.pad_offset + 1);
532     }
533     else if ('-' == dir)
534     {
535         reset_pad_offset(world.wmeta.pad_offset - 1);
536     }
537 }
538
539
540
541 extern void growshrink_active_window(char change)
542 {
543     if (0 != world.wmeta.active)
544     {
545         struct yx_uint16 size = world.wmeta.active->framesize;
546         if      (change == '-')
547         {
548             size.y--;
549         }
550         else if (change == '+')
551         {
552             size.y++;
553         }
554         else if (change == '_')
555         {
556             size.x--;
557         }
558         else if (change == '*')
559         {
560             size.x++;
561         }
562         resize_active_win(size);
563         struct WinConf * wcp = get_winconf_by_win(world.wmeta.active);
564         if (   1 == wcp->width_type
565             && world.wmeta.active->framesize.x > world.wmeta.padsize.x)
566         {
567             wcp->width_type = 0;
568         }
569         set_winconf_geometry(wcp->id);
570     }
571 }