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