diff --git a/include/server.h b/include/server.h index d2a4ca9..2e74f5a 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 30f0b8a..1053311 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 d823dda..f7a25d7 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); }