Add config path default

This commit is contained in:
buffet 2019-10-18 22:42:30 +02:00
parent 16653242e8
commit 71917a46cf
3 changed files with 34 additions and 4 deletions

View file

@ -18,11 +18,12 @@ struct kiwmi_server {
struct wl_event_loop *wl_event_loop; struct wl_event_loop *wl_event_loop;
struct wlr_backend *backend; struct wlr_backend *backend;
const char *socket; const char *socket;
char *config_path;
struct kiwmi_desktop desktop; struct kiwmi_desktop desktop;
struct kiwmi_input input; 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); bool server_run(struct kiwmi_server *server);
void server_fini(struct kiwmi_server *server); void server_fini(struct kiwmi_server *server);

View file

@ -20,7 +20,7 @@ int
main(int argc, char **argv) main(int argc, char **argv)
{ {
int verbosity = 0; int verbosity = 0;
const char *config_path = NULL; char *config_path = NULL;
const char *usage = const char *usage =
"Usage: kiwmi [options]\n" "Usage: kiwmi [options]\n"
@ -42,7 +42,11 @@ main(int argc, char **argv)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break; break;
case 'c': 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; break;
case 'V': case 'V':
++verbosity; ++verbosity;

View file

@ -7,15 +7,18 @@
#include "server.h" #include "server.h"
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <limits.h>
#include <wayland-server.h> #include <wayland-server.h>
#include <wlr/backend.h> #include <wlr/backend.h>
#include <wlr/render/wlr_renderer.h> #include <wlr/render/wlr_renderer.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
bool 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"); wlr_log(WLR_DEBUG, "Initializing Wayland server");
@ -50,6 +53,26 @@ server_init(struct kiwmi_server *server, const char *UNUSED(config_path))
return false; 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; return true;
} }
@ -79,4 +102,6 @@ server_fini(struct kiwmi_server *server)
wl_display_destroy_clients(server->wl_display); wl_display_destroy_clients(server->wl_display);
wl_display_destroy(server->wl_display); wl_display_destroy(server->wl_display);
free(server->config_path);
} }