diff --git a/include/commands.h b/include/commands.h deleted file mode 100644 index ae1bd5b..0000000 --- a/include/commands.h +++ /dev/null @@ -1,19 +0,0 @@ -/* Copyright (c), Charlotte Meyer - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#ifndef KIWMI_COMMANDS_H -#define KIWMI_COMMANDS_H - -#include -#include - -#include "kiwmi/server.h" - -bool -handle_client_command(char *command, FILE *client, struct kiwmi_server *server); - -#endif /* KIWMI_COMMANDS_H */ diff --git a/include/frontend.h b/include/frontend.h deleted file mode 100644 index 0f225d0..0000000 --- a/include/frontend.h +++ /dev/null @@ -1,26 +0,0 @@ -/* Copyright (c), Charlotte Meyer - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#ifndef KIWMI_FRONTEND_H -#define KIWMI_FRONTEND_H - -#include -#include - -#include - -struct kiwmi_frontend { - const char *frontend_path; - int sock_fd; - char *sock_path; - struct wl_event_source *sock_event_source; - struct wl_listener display_destroy; -}; - -bool frontend_init(struct kiwmi_frontend *frontend, const char *frontend_path); - -#endif /* KIWMI_FRONTEND_H */ diff --git a/include/server.h b/include/server.h index 5b3bdbe..64a882c 100644 --- a/include/server.h +++ b/include/server.h @@ -10,9 +10,8 @@ #include -#include "kiwmi/desktop/desktop.h" -#include "kiwmi/frontend.h" -#include "kiwmi/input/input.h" +#include "desktop/desktop.h" +#include "input/input.h" struct kiwmi_server { struct wl_display *wl_display; @@ -21,10 +20,9 @@ struct kiwmi_server { const char *socket; struct kiwmi_desktop desktop; struct kiwmi_input input; - struct kiwmi_frontend frontend; }; -bool server_init(struct kiwmi_server *server, const char *frontend_path); +bool server_init(struct kiwmi_server *server, const char *config_path); bool server_run(struct kiwmi_server *server); void server_fini(struct kiwmi_server *server); diff --git a/kiwmi/commands.c b/kiwmi/commands.c deleted file mode 100644 index 11e999a..0000000 --- a/kiwmi/commands.c +++ /dev/null @@ -1,50 +0,0 @@ -/* Copyright (c), Charlotte Meyer - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#include "kiwmi/commands.h" - -#include - -typedef bool ( - *cmd_handler)(FILE *client, const char **args, struct kiwmi_server *server); - -static bool -cmd_quit( - FILE *UNUSED(client), - const char **UNUSED(args), - struct kiwmi_server *server) -{ - wl_display_terminate(server->wl_display); - return false; -} - -static const struct { - const char *name; - cmd_handler handler; -} commands[] = { - {"quit", cmd_quit}, -}; - -bool -handle_client_command(char *command, FILE *client, struct kiwmi_server *server) -{ -#define SIZE(arr) (sizeof((arr)) / sizeof(*(arr))) - - const char *name = strtok(command, " \t\r"); - - for (size_t i = 0; i < SIZE(commands); ++i) { - if (strcmp(name, commands[i].name) == 0) { - return commands[i].handler(client, NULL, server); - } - } - - fprintf(client, "Unknown command: %s\n", name); - - return false; - -#undef SIZE -} diff --git a/kiwmi/desktop/desktop.c b/kiwmi/desktop/desktop.c index 3dae385..da2341e 100644 --- a/kiwmi/desktop/desktop.c +++ b/kiwmi/desktop/desktop.c @@ -5,7 +5,7 @@ * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include "kiwmi/desktop/desktop.h" +#include "desktop/desktop.h" #include @@ -15,8 +15,8 @@ #include #include -#include "kiwmi/desktop/output.h" -#include "kiwmi/server.h" +#include "desktop/output.h" +#include "server.h" bool desktop_init(struct kiwmi_desktop *desktop, struct wlr_renderer *renderer) diff --git a/kiwmi/desktop/output.c b/kiwmi/desktop/output.c index 0e80354..1b04572 100644 --- a/kiwmi/desktop/output.c +++ b/kiwmi/desktop/output.c @@ -5,7 +5,7 @@ * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include "kiwmi/desktop/output.h" +#include "desktop/output.h" #include @@ -17,10 +17,10 @@ #include #include -#include "kiwmi/desktop/desktop.h" -#include "kiwmi/input/cursor.h" -#include "kiwmi/input/input.h" -#include "kiwmi/server.h" +#include "desktop/desktop.h" +#include "input/cursor.h" +#include "input/input.h" +#include "server.h" static void output_frame_notify(struct wl_listener *listener, void *data) diff --git a/kiwmi/frontend.c b/kiwmi/frontend.c deleted file mode 100644 index eeae856..0000000 --- a/kiwmi/frontend.c +++ /dev/null @@ -1,220 +0,0 @@ -/* Copyright (c), Charlotte Meyer - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#include "kiwmi/frontend.h" - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "kiwmi/commands.h" -#include "kiwmi/server.h" - -static void -display_destroy_notify(struct wl_listener *listener, void *UNUSED(data)) -{ - struct kiwmi_frontend *frontend = - wl_container_of(listener, frontend, display_destroy); - - if (frontend->sock_event_source) { - wl_event_source_remove(frontend->sock_event_source); - } - - close(frontend->sock_fd); - unlink(frontend->sock_path); - free(frontend->sock_path); - - wl_list_remove(&frontend->display_destroy.link); -} - -static int -ipc_connection(int fd, uint32_t mask, void *data) -{ - wlr_log(WLR_DEBUG, "Received an IPC event"); - - struct kiwmi_server *server = data; - - if (!(mask & WL_EVENT_READABLE)) { - return 0; - } - - int client_fd = accept(fd, NULL, NULL); - - if (client_fd < 0) { - wlr_log(WLR_ERROR, "Failed to accept client connection"); - return 0; - } - - FILE *client_file = fdopen(client_fd, "r+"); - - size_t buffer_size = BUFSIZ; - size_t msg_len = 0; - char *msg = malloc(buffer_size); - if (!msg) { - wlr_log(WLR_ERROR, "Failed to allocate memory"); - fclose(client_file); - return 0; - } - - int c; - while ((c = getc(client_file)) != '\0') { - if (msg_len >= buffer_size) { - buffer_size *= 2; - char *tmp = realloc(msg, buffer_size); - if (!tmp) { - wlr_log(WLR_ERROR, "Failed to allocate memory"); - fclose(client_file); - free(msg); - return 0; - } - msg = tmp; - } - - msg[msg_len++] = c; - } - - msg[msg_len] = '\0'; - - for (char *cmd = strtok(msg, "\n"); cmd; cmd = strtok(NULL, "\n")) { - if (!handle_client_command(cmd, client_file, server)) { - break; - } - } - - fclose(client_file); - free(msg); - - return 0; -} - -static bool -ipc_init(struct kiwmi_frontend *frontend) -{ - struct kiwmi_server *server = wl_container_of(frontend, server, frontend); - struct sockaddr_un sock_addr; - - memset(&sock_addr, 0, sizeof(sock_addr)); - - size_t path_len = snprintf( - sock_addr.sun_path, - sizeof(sock_addr.sun_path), - "%s/kiwmi_%" PRIdMAX ".sock", - getenv("XDG_RUNTIME_DIR"), - (intmax_t)getpid()); - - frontend->sock_path = malloc(path_len + 1); - if (!frontend->sock_path) { - wlr_log(WLR_ERROR, "Failed to allocate memory"); - return false; - } - - strcpy(frontend->sock_path, sock_addr.sun_path); - - setenv("KIWMI_SOCKET", sock_addr.sun_path, true); - - sock_addr.sun_family = AF_UNIX; - - if ((frontend->sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { - wlr_log(WLR_ERROR, "Failed to create socket"); - return false; - } - - if (fcntl(frontend->sock_fd, F_SETFD, FD_CLOEXEC) < 0) { - wlr_log(WLR_ERROR, "Failed to set CLOEXEC on socket"); - return false; - } - - unlink(sock_addr.sun_path); - - if (bind( - frontend->sock_fd, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) - < 0) { - wlr_log(WLR_ERROR, "Failed to bind socket"); - return false; - } - - if (listen(frontend->sock_fd, 3) < 0) { - wlr_log(WLR_ERROR, "Failed to listen to socket"); - return false; - } - - frontend->display_destroy.notify = display_destroy_notify; - wl_display_add_destroy_listener( - server->wl_display, &frontend->display_destroy); - - frontend->sock_event_source = wl_event_loop_add_fd( - server->wl_event_loop, - frontend->sock_fd, - WL_EVENT_READABLE, - ipc_connection, - server); - - return true; -} - -static bool -spawn_frontend(const char *path) -{ - pid_t pid = fork(); - - if (pid < 0) { - wlr_log(WLR_ERROR, "Failed to start frontend (fork)"); - return false; - } - - if (pid == 0) { - pid = fork(); - - if (pid < 0) { - wlr_log(WLR_ERROR, "Failed to start frontend (fork)"); - _exit(EXIT_FAILURE); - } - - if (pid == 0) { - execlp(path, path, NULL); - wlr_log(WLR_ERROR, "Failed to start frontend (exec), continuing"); - _exit(EXIT_FAILURE); - } - - _exit(EXIT_SUCCESS); - } - - if (waitpid(pid, NULL, 0) < 0) { - wlr_log(WLR_ERROR, "Failed to start frontend (waitpid)"); - return false; - } - - return true; -} - -bool -frontend_init(struct kiwmi_frontend *frontend, const char *frontend_path) -{ - frontend->frontend_path = frontend_path; - - if (!ipc_init(frontend)) { - wlr_log(WLR_ERROR, "Failed to create socket"); - return false; - } - - if (strcmp(frontend_path, "NONE") == 0) { - wlr_log(WLR_ERROR, "Launching without a frontend"); - return true; - } else { - return spawn_frontend(frontend_path); - } -} diff --git a/kiwmi/input/cursor.c b/kiwmi/input/cursor.c index 37cb16e..2d00e08 100644 --- a/kiwmi/input/cursor.c +++ b/kiwmi/input/cursor.c @@ -5,7 +5,7 @@ * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include "kiwmi/input/cursor.h" +#include "input/cursor.h" #include diff --git a/kiwmi/input/input.c b/kiwmi/input/input.c index a3cf7c0..6dffaf5 100644 --- a/kiwmi/input/input.c +++ b/kiwmi/input/input.c @@ -5,7 +5,7 @@ * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include "kiwmi/input/input.h" +#include "input/input.h" #include #include @@ -13,10 +13,10 @@ #include #include -#include "kiwmi/desktop/desktop.h" -#include "kiwmi/input/cursor.h" -#include "kiwmi/input/keyboard.h" -#include "kiwmi/server.h" +#include "desktop/desktop.h" +#include "input/cursor.h" +#include "input/keyboard.h" +#include "server.h" static void new_pointer(struct kiwmi_input *input, struct wlr_input_device *device) diff --git a/kiwmi/input/keyboard.c b/kiwmi/input/keyboard.c index 1d0d10e..68e3088 100644 --- a/kiwmi/input/keyboard.c +++ b/kiwmi/input/keyboard.c @@ -5,7 +5,7 @@ * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include "kiwmi/input/keyboard.h" +#include "input/keyboard.h" #include #include @@ -17,7 +17,7 @@ #include #include -#include "kiwmi/server.h" +#include "server.h" static bool switch_vt(const xkb_keysym_t *syms, int nsyms, struct wlr_backend *backend) diff --git a/kiwmi/main.c b/kiwmi/main.c index a0d3628..19843d2 100644 --- a/kiwmi/main.c +++ b/kiwmi/main.c @@ -14,23 +14,24 @@ #include -#include "kiwmi/server.h" +#include "server.h" int main(int argc, char **argv) { int verbosity = 0; - const char *frontend_path; + const char *config_path = NULL; const char *usage = - "Usage: kiwmi [options] FRONTEND\n" + "Usage: kiwmi [options]\n" "\n" - " -h Show help message and quit\n" - " -v Show version number and quit\n" - " -V Increase verbosity Level\n"; + " -h Show help message and exit\n" + " -v Show version number and exit\n" + " -c Change config path\n" + " -V Increase verbosity level\n"; int option; - while ((option = getopt(argc, argv, "hvV")) != -1) { + while ((option = getopt(argc, argv, "hvc:V")) != -1) { switch (option) { case 'h': printf("%s", usage); @@ -40,6 +41,9 @@ main(int argc, char **argv) printf("kiwmi version " KIWMI_VERSION "\n"); exit(EXIT_SUCCESS); break; + case 'c': + config_path = optarg; + break; case 'V': ++verbosity; break; @@ -49,14 +53,6 @@ main(int argc, char **argv) } } - // no frontend passsed - if (optind >= argc) { - fprintf(stderr, "%s", usage); - exit(EXIT_FAILURE); - } - - frontend_path = argv[optind]; - switch (verbosity) { case 0: wlr_log_init(WLR_ERROR, NULL); @@ -77,7 +73,7 @@ main(int argc, char **argv) struct kiwmi_server server; - if (!server_init(&server, frontend_path)) { + if (!server_init(&server, config_path)) { wlr_log(WLR_ERROR, "Failed to initialize server"); exit(EXIT_FAILURE); } diff --git a/kiwmi/meson.build b/kiwmi/meson.build index 5c1d227..489b970 100644 --- a/kiwmi/meson.build +++ b/kiwmi/meson.build @@ -1,7 +1,5 @@ kiwmi_sources = files( 'main.c', - 'commands.c', - 'frontend.c', 'server.c', 'desktop/desktop.c', 'desktop/output.c', diff --git a/kiwmi/server.c b/kiwmi/server.c index 6798d82..b748e8a 100644 --- a/kiwmi/server.c +++ b/kiwmi/server.c @@ -5,7 +5,7 @@ * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include "kiwmi/server.h" +#include "server.h" #include @@ -15,7 +15,7 @@ #include bool -server_init(struct kiwmi_server *server, const char *frontend_path) +server_init(struct kiwmi_server *server, const char *UNUSED(config_path)) { wlr_log(WLR_DEBUG, "Initializing Wayland server"); @@ -50,12 +50,6 @@ server_init(struct kiwmi_server *server, const char *frontend_path) return false; } - if (!frontend_init(&server->frontend, frontend_path)) { - wlr_log(WLR_ERROR, "Failed to initialize frontend"); - wl_display_destroy(server->wl_display); - return false; - } - return true; }