#include "wincontrol.h"
#include <stdlib.h> /* for free() */
-#include <string.h> /* for strlen() */
+#include <string.h> /* for strlen(), strchr(), strstr() */
#include <stdint.h> /* for uint8_t, uint16_t */
#include <stdio.h> /* for fwrite() */
#include <unistd.h> /* for access(), unlink() */
* draw_map_win
*/
#include "misc.h" /* for try_malloc() */
+#include "dirent.h" /* for opendir(), closedir(), readdir() */
+#include "errno.h" /* for errno */
-/* Create Winconf, initialize ->view/height_type/width_type to 0, ->id to "id"
- * and ->draw to "f".
- */
-static void create_winconf(char id, struct WinConf * wcp,
- void (* f) (struct Win *));
+/* Create Winconf, init ->view/height_type/width_type to 0, ->id to "id". */
+static void create_winconf(char id, struct WinConf * wcp);
/* Initialize Winconf of "id" from appropriate config file.*/
static void init_winconf_from_file(struct World * world, char id);
/* Wrapper around init_win() called with values from Winconf of "id". */
static void init_win_from_winconf(struct World * world, char id);
-/* Save title, size of window identified by "id" to its configuration file. */
+/* Save title, draw function, size of window identified by "id" to conffile. */
static void save_win_config(struct World * world, char id);
/* Get WinConf by "id"; get id of WinConf mothering "win". */
static struct WinConf * get_winconf_by_id(struct World * world, char id);
+/* Get (Win->draw) function identified by "c"; NULL if c not mapped to one. */
+static void * get_drawfunc_by_char(char c);
+
+/* Iterate over bytes of world->winconf_ids array. Re-start after null byte. */
+static char get_next_winconf_id(struct World * world);
+
static char * string_prefixed_id(struct World * world, char * prefix, char id)
-static void create_winconf(char id, struct WinConf * wcp,
- void (* f) (struct Win *))
+static void create_winconf(char id, struct WinConf * wcp)
{
wcp->id = id;
- wcp->draw = f;
wcp->view = 0;
wcp->height_type = 0;
wcp->width_type = 0;
static void init_winconf_from_file(struct World * world, char id)
{
- char * f_name = "init_winconf_from_file()";
+ char * tmp = "init_winconf_from_file() on window id '_'";
+ char * context = try_malloc(strlen(tmp) + 1, world,
+ "init_winconf_from_file()");
+ memcpy(context, tmp, strlen(tmp) + 1);
+ context[strlen(tmp) - 2] = id;
char * path = string_prefixed_id(world, "config/windows/Win_", id);
- FILE * file = try_fopen(path, "r", world, f_name);
+ FILE * file = try_fopen(path, "r", world, context);
free(path);
- uint16_t linemax = get_linemax(file, world, f_name);
+ uint16_t linemax = get_linemax(file, world, context);
char line[linemax + 1];
struct WinConf * winconf = get_winconf_by_id(world, id);
- try_fgets(line, linemax + 1, file, world, f_name);
- winconf->title = try_malloc(strlen(line), world, f_name);
+ try_fgets(line, linemax + 1, file, world, context);
+ winconf->title = try_malloc(strlen(line), world, context);
memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */
winconf->title[strlen(line) - 1] = '\0'; /* char at end of string. */
- try_fgets(line, linemax + 1, file, world, f_name);
+
+ try_fgets(line, linemax + 1, file, world, context);
+ winconf->draw = line[0];
+
+ try_fgets(line, linemax + 1, file, world, context);
winconf->height = atoi(line);
if (0 >= winconf->height)
{
winconf->height_type = 1;
}
- try_fgets(line, linemax + 1, file, world, f_name);
+ try_fgets(line, linemax + 1, file, world, context);
winconf->width = atoi(line);
if (0 >= winconf->width)
{
winconf->width_type = 1;
}
- try_fclose(file, world, f_name);
+ try_fclose(file, world, context);
+ free(context);
}
static void init_win_from_winconf(struct World * world, char id)
{
- char * err = "Trouble in init_win_from_file() with init_win().";
+ char * tmp = "Trouble in init_win_from_file() with init_win() (win id: _).";
+ char * err = try_malloc(strlen(tmp) + 1, world, "init_win_from_file()");
+ memcpy(err, tmp, strlen(tmp) + 1);
+ err[strlen(tmp) - 3] = id;
struct WinConf * winconf = get_winconf_by_id(world, id);
+ void * f = get_drawfunc_by_char(winconf->draw);
+ exit_err(NULL == f, world, err);
exit_err(init_win(world->wmeta, &winconf->win, winconf->title,
- winconf->height, winconf->width, world, winconf->draw),
+ winconf->height, winconf->width, world, f),
world, err);
+ free(err);
}
char line[size];
sprintf(line, "%s\n", wc->title);
fwrite(line, sizeof(char), strlen(line), file);
+ sprintf(line, "%c\n", wc->draw);
+ fwrite(line, sizeof(char), strlen(line), file);
sprintf(line, "%d\n", wc->height);
fwrite(line, sizeof(char), strlen(line), file);
sprintf(line, "%d\n", wc->width);
+static void * get_drawfunc_by_char(char c)
+{
+ if ('i' == c)
+ {
+ return draw_info_win;
+ }
+ else if ('k' == c)
+ {
+ return draw_keys_win;
+ }
+ else if ('l' == c)
+ {
+ return draw_log_win;
+ }
+ else if ('m' == c)
+ {
+ return draw_map_win;
+ }
+ return NULL;
+}
+
+
+
+static char get_next_winconf_id(struct World * world)
+{
+ static uint8_t i = 0;
+ char c = world->winconf_ids[i];
+ if (0 == c)
+ {
+ i = 0;
+ }
+ else
+ {
+ i++;
+ }
+ return c;
+}
+
+
+
extern struct WinConf * get_winconf_by_win(struct World * world,
struct Win * win)
{
extern void init_winconfs(struct World * world)
{
char * f_name = "init_winconfs()";
- struct WinConf * winconfs = try_malloc(4 * sizeof(struct WinConf),
- world, f_name);
- create_winconf('i', &winconfs[0], draw_info_win);
- create_winconf('k', &winconfs[1], draw_keys_win);
- create_winconf('l', &winconfs[2], draw_log_win);
- create_winconf('m', &winconfs[3], draw_map_win);
+ char * err_o = "Trouble in init_winconfs() with opendir().";
+ char * err_r = "Trouble in init_winconfs() with readdir().";
+ char * err_c = "Trouble in init_winconfs() with closedir().";
+
+ DIR * dp = opendir("config/windows");
+ exit_err(NULL == dp, world, err_o);
+ struct dirent * fn;
+ errno = 0;
+ char * winconf_ids = try_malloc(256, world, f_name);
+ uint8_t i = 0;
+ char id;
+ while (NULL != (fn = readdir(dp)))
+ {
+ if ( 5 == strlen(fn->d_name)
+ && fn->d_name == strstr(fn->d_name, "Win_"))
+ {
+ id = fn->d_name[4];
+ winconf_ids[i] = id;
+ i++;
+ }
+ }
+ winconf_ids[i] = '\0';
+ exit_err(errno, world, err_r);
+ exit_err(closedir(dp), world, err_c);
+ world->winconf_ids = try_malloc(strlen(winconf_ids + 1), world, f_name);
+ memcpy(world->winconf_ids, winconf_ids, strlen(winconf_ids) + 1);
+ free(winconf_ids);
+
+ struct WinConf * winconfs;
+ winconfs = try_malloc(strlen(world->winconf_ids) * sizeof(struct WinConf),
+ world, f_name);
+ i = 0;
+ while (0 != (id = get_next_winconf_id(world)))
+ {
+ create_winconf(id, &winconfs[i]);
+ i++;
+ }
world->winconfs = winconfs;
- init_winconf_from_file(world, 'i');
- init_winconf_from_file(world, 'k');
- init_winconf_from_file(world, 'l');
- init_winconf_from_file(world, 'm');
+ while (0 != (id = get_next_winconf_id(world)))
+ {
+ init_winconf_from_file(world, id);
+ i++;
+ }
}
extern void free_winconfs(struct World * world)
{
- free_winconf(world, 'i');
- free_winconf(world, 'k');
- free_winconf(world, 'l');
- free_winconf(world, 'm');
+ char id;
+ while (0 != (id = get_next_winconf_id(world)))
+ {
+ free_winconf(world, id);
+ }
+ free(world->winconf_ids);
free(world->winconfs);
}
extern void init_wins(struct World * world)
{
- init_win_from_winconf(world, 'i');
- init_win_from_winconf(world, 'k');
- init_win_from_winconf(world, 'l');
- init_win_from_winconf(world, 'm');
+ char id;
+ while (0 != (id = get_next_winconf_id(world)))
+ {
+ init_win_from_winconf(world, id);
+ }
}
extern void free_wins(struct World * world)
{
- free_win(get_win_by_id(world, 'i'));
- free_win(get_win_by_id(world, 'k'));
- free_win(get_win_by_id(world, 'l'));
- free_win(get_win_by_id(world, 'm'));
+ char id;
+ while (0 != (id = get_next_winconf_id(world)))
+ {
+ free_win(get_win_by_id(world, id));
+ }
}
uint8_t i = 0;
for (; i < linemax - 1; i++)
{
+ if (NULL == strchr(world->winconf_ids, win_order[i]))
+ {
+ continue;
+ }
toggle_window(world->wmeta, get_win_by_id(world, win_order[i]));
}
}
{
char * f_name = "save_win_configs()";
- save_win_config(world, 'i');
- save_win_config(world, 'k');
- save_win_config(world, 'l');
- save_win_config(world, 'm');
+ char id;
+ while (0 != (id = get_next_winconf_id(world)))
+ {
+ save_win_config(world, id);
+ }
char * path = "config/windows/toggle_order";
char * path_tmp = "config/windows/toggle_order_tmp";
FILE * file = try_fopen(path_tmp, "w", world, f_name);
char line[6];
- struct Win * w_p = world->wmeta->_chain_start;
+ struct Win * w_p = world->wmeta->chain_start;
uint8_t i = 0;
while (0 != w_p)
{
struct WinConf * wc = get_winconf_by_win(world, w_p);
line[i] = wc->id;
- w_p = w_p->_next;
+ w_p = w_p->next;
i++;
}
line[i] = '\n';
struct WinConf * wcp = get_winconf_by_win(world, win);
if (0 == wcp->view)
{
- win->_draw = draw_winconf;
+ win->draw = draw_winconf;
wcp->view = 1;
}
else
{
- win->_draw = wcp->draw;
+ win->draw = get_drawfunc_by_char(wcp->draw);
wcp->view = 0;
}
}