From 71917a46cfaa10243b297051108453041ebd983c Mon Sep 17 00:00:00 2001 From: Charlotte Meyer Date: Fri, 18 Oct 2019 22:42:30 +0200 Subject: [PATCH] Add config path default --- include/server.h | 3 ++- kiwmi/main.c | 8 ++++++-- kiwmi/server.c | 27 ++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/include/server.h b/include/server.h index 64a882c..d10841c 100644 --- a/include/server.h +++ b/include/server.h @@ -18,11 +18,12 @@ struct kiwmi_server { struct wl_event_loop *wl_event_loop; struct wlr_backend *backend; const char *socket; + char *config_path; struct kiwmi_desktop desktop; struct kiwmi_input input; }; -bool server_init(struct kiwmi_server *server, const char *config_path); +bool server_init(struct kiwmi_server *server, char *config_path); bool server_run(struct kiwmi_server *server); void server_fini(struct kiwmi_server *server); diff --git a/kiwmi/main.c b/kiwmi/main.c index 19843d2..64522d0 100644 --- a/kiwmi/main.c +++ b/kiwmi/main.c @@ -20,7 +20,7 @@ int main(int argc, char **argv) { int verbosity = 0; - const char *config_path = NULL; + char *config_path = NULL; const char *usage = "Usage: kiwmi [options]\n" @@ -42,7 +42,11 @@ main(int argc, char **argv) exit(EXIT_SUCCESS); break; case 'c': - config_path = optarg; + config_path = strdup(optarg); // gets freed in kiwmi_fini + if (!config_path) { + fprintf(stderr, "Failed to allocate memory\n"); + exit(EXIT_FAILURE); + } break; case 'V': ++verbosity; diff --git a/kiwmi/server.c b/kiwmi/server.c index b748e8a..5637eb7 100644 --- a/kiwmi/server.c +++ b/kiwmi/server.c @@ -7,15 +7,18 @@ #include "server.h" +#include #include +#include + #include #include #include #include bool -server_init(struct kiwmi_server *server, const char *UNUSED(config_path)) +server_init(struct kiwmi_server *server, char *config_path) { wlr_log(WLR_DEBUG, "Initializing Wayland server"); @@ -50,6 +53,26 @@ server_init(struct kiwmi_server *server, const char *UNUSED(config_path)) return false; } + if (!config_path) { + // default config path + free(config_path); + config_path = malloc(PATH_MAX); + if (!config_path) { + wlr_log(WLR_ERROR, "Falied to allocate memory"); + wl_display_destroy(server->wl_display); + return false; + } + + const char *config_home = getenv("XDG_CONFIG_HOME"); + if (config_home) { + snprintf(config_path, PATH_MAX, "%s/kiwmi/init.lua", config_home); + } else { + snprintf(config_path, PATH_MAX, "%s/.config/kiwmi/init.lua", getenv("HOME")); + } + } + + server->config_path = config_path; + return true; } @@ -79,4 +102,6 @@ server_fini(struct kiwmi_server *server) wl_display_destroy_clients(server->wl_display); wl_display_destroy(server->wl_display); + + free(server->config_path); }