#include "e.h"
-static char cfg_grabs_db[4096];
-static char cfg_settings_db[4096];
-static char cfg_actions_db[4096];
-static char cfg_borders[4096];
+static char cfg_grabs_db[4096] = "";
+static char cfg_settings_db[4096] = "";
+static char cfg_actions_db[4096] = "";
+static char cfg_borders_db[4096] = "";
+static char cfg_user_dir[4096] = "";
+static char cfg_images_dir[4096] = "";
+static char cfg_fonts_dir[4096] = "";
-char *
-e_config_get(char *type)
+void
+e_config_init(void)
{
- if (!strcmp(type, "grabs"))
- {
- sprintf(cfg_grabs_db,
- PACKAGE_DATA_DIR"/data/config/behavior/default/grabs.db");
- return cfg_grabs_db;
- }
- if (!strcmp(type, "settings"))
- {
- sprintf(cfg_settings_db,
- PACKAGE_DATA_DIR"/data/config/behavior/default/settings.db");
- return cfg_settings_db;
- }
- if (!strcmp(type, "actions"))
- {
- sprintf(cfg_actions_db,
- PACKAGE_DATA_DIR"/data/config/behavior/default/actions.db");
- return cfg_actions_db;
- }
- if (!strcmp(type, "borders"))
- {
- sprintf(cfg_borders,
- PACKAGE_DATA_DIR"/data/config/appearance/default/borders/");
- return cfg_borders;
- }
- if (!strcmp(type, "images"))
- {
- sprintf(cfg_borders,
- PACKAGE_DATA_DIR"/data/images/");
- return cfg_borders;
- }
- if (!strcmp(type, "fonts"))
+ if (!e_file_is_dir(e_config_user_dir()))
{
- sprintf(cfg_borders,
- PACKAGE_DATA_DIR"/data/fonts/");
- return cfg_borders;
+ char buf[4096];
+
+ sprintf(buf, "%s", e_config_user_dir());
+ e_file_mkdir(buf);
+ sprintf(buf, "%sappearance", e_config_user_dir());
+ e_file_mkdir(buf);
+ sprintf(buf, "%sappearance/borders", e_config_user_dir());
+ e_file_mkdir(buf);
+ sprintf(buf, "%sbehavior", e_config_user_dir());
+ e_file_mkdir(buf);
+ sprintf(buf, "%sbehavior/grabs.db", e_config_user_dir());
+ e_file_cp(PACKAGE_DATA_DIR"/data/config/behavior/default/grabs.db", buf);
+ sprintf(buf, "%sbehavior/settings.db", e_config_user_dir());
+ e_file_cp(PACKAGE_DATA_DIR"/data/config/behavior/default/settings.db", buf);
+ sprintf(buf, "%sbehavior/actions.db", e_config_user_dir());
+ e_file_cp(PACKAGE_DATA_DIR"/data/config/behavior/default/actions.db", buf);
+
+ sprintf(buf, "%sappearance/borders/border.bits.db", e_config_user_dir());
+ e_file_cp(PACKAGE_DATA_DIR"/data/config/appearance/default/borders/border.bits.db", buf);
+ sprintf(buf, "%sappearance/borders/border2.bits.db", e_config_user_dir());
+ e_file_cp(PACKAGE_DATA_DIR"/data/config/appearance/default/borders/border2.bits.db", buf);
+ sprintf(buf, "%sappearance/borders/borderless.bits.db", e_config_user_dir());
+ e_file_cp(PACKAGE_DATA_DIR"/data/config/appearance/default/borders/borderless.bits.db", buf);
}
+}
+
+char *
+e_config_user_dir(void)
+{
+ if (cfg_user_dir[0]) return cfg_user_dir;
+ sprintf(cfg_user_dir, "%s/.e/", e_file_home());
+ return cfg_user_dir;
+}
+
+#define E_CONF(_key, _var, _args...) \
+{ \
+ if (!strcmp(type, _key)) \
+ { \
+ if ((_var)[0]) return (_var); \
+ sprintf((_var), ## _args); \
+ return (_var); \
+ } \
+}
+
+char *
+e_config_get(char *type)
+{
+ E_CONF("grabs", cfg_grabs_db,
+ PACKAGE_DATA_DIR"/data/config/behavior/default/grabs.db");
+ E_CONF("settings", cfg_settings_db,
+ PACKAGE_DATA_DIR"/data/config/behavior/default/settings.db");
+ E_CONF("actions", cfg_actions_db,
+ PACKAGE_DATA_DIR"/data/config/behavior/default/actions.db");
+ E_CONF("borders", cfg_borders_db,
+ PACKAGE_DATA_DIR"/data/config/appearance/default/borders/");
+ E_CONF("images", cfg_images_dir,
+ PACKAGE_DATA_DIR"/data/images/");
+ E_CONF("fonts", cfg_fonts_dir,
+ PACKAGE_DATA_DIR"/data/fonts/");
return "";
}
time_t e_file_modified_time(char *file);
void e_set_env(char *variable, char *content);
+int e_file_exists(char *file);
+int e_file_is_dir(char *file);
+char *e_file_home(void);
+int e_file_mkdir(char *dir);
+int e_file_cp(char *src, char *dst);
void e_exec_set_args(int argc, char **argv);
void e_exec_restart(void);
pid_t e_exec_run_in_dir(char *exe, char *dir);
pid_t e_run_in_dir_with_env(char *exe, char *dir, int *launch_id_ret, char **env, char *launch_path);
+void e_config_init(void);
+char *e_config_user_dir(void);
char *e_config_get(char *type);
sprintf(env, "%s=%s", variable, content);
putenv(env);
}
+
+int
+e_file_exists(char *file)
+{
+ struct stat st;
+
+ if (stat(file, &st) < 0) return 0;
+ return 1;
+}
+
+int
+e_file_is_dir(char *file)
+{
+ struct stat st;
+
+ if (stat(file, &st) < 0) return 0;
+ if (S_ISDIR(st.st_mode)) return 1;
+ return 0;
+}
+
+char *
+e_file_home(void)
+{
+ static char *home = NULL;
+
+ if (home) return home;
+ home = getenv("HOME");
+ if (!home) home = getenv("TMPDIR");
+ if (!home) home = "/tmp";
+ return home;
+}
+
+static mode_t default_mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
+
+int
+e_file_mkdir(char *dir)
+{
+ if (mkdir(dir, default_mode) < 0) return 0;
+ return 1;
+}
+
+int
+e_file_cp(char *src, char *dst)
+{
+ FILE *f1, *f2;
+ char buf[4096];
+ size_t num;
+
+ f1 = fopen(src, "rb");
+ if (!f1) return 0;
+ f2 = fopen(dst, "wb");
+ if (!f2)
+ {
+ fclose(f2);
+ return 0;
+ }
+ while ((num = fread(buf, 1, 4096, f1)) > 0) fwrite(buf, 1, num, f2);
+ fclose(f1);
+ fclose(f2);
+ return 1;
+}