From 40f66a03b2550fbe4f74037f11a401c0664c066f Mon Sep 17 00:00:00 2001 From: buffet Date: Mon, 29 Oct 2018 15:18:25 +0100 Subject: [PATCH] Added basic IPC event handling (quit, reload) - Added main.h - config_path is now global --- src/kiwmi/ipc.c | 19 +++++++++++++++++++ src/kiwmi/ipc.h | 1 + src/kiwmi/main.c | 32 ++++++++++++++++++-------------- src/kiwmi/main.h | 10 ++++++++++ 4 files changed, 48 insertions(+), 14 deletions(-) create mode 100644 src/kiwmi/main.h diff --git a/src/kiwmi/ipc.c b/src/kiwmi/ipc.c index 706c530..7c5c9d4 100644 --- a/src/kiwmi/ipc.c +++ b/src/kiwmi/ipc.c @@ -7,6 +7,7 @@ #include "ipc.h" +#include #include #include #include @@ -15,8 +16,12 @@ #include #include +#include "main.h" + #include "common.h" +#define STREQ(a, b) (strcmp((a), (b)) == 0) + int g_sock_fd; void @@ -50,3 +55,17 @@ init_socket(void) die("failed to listen to socket\n"); } } + +void +handle_ipc_event(char *msg) +{ + char *command = strtok(msg, " "); + + if (STREQ(command, "quit")) { + g_is_about_to_quit = true; + } else if (STREQ(command, "reload")) { + exec_config(); + } else { + warn("ignoring unknown command: %s\n", command); + } +} diff --git a/src/kiwmi/ipc.h b/src/kiwmi/ipc.h index 1b27fc5..670891e 100644 --- a/src/kiwmi/ipc.h +++ b/src/kiwmi/ipc.h @@ -9,6 +9,7 @@ #define IPC_H void init_socket(void); +void handle_ipc_event(char *msg); extern int g_sock_fd; diff --git a/src/kiwmi/main.c b/src/kiwmi/main.c index 5ecf6eb..b99c3b2 100644 --- a/src/kiwmi/main.c +++ b/src/kiwmi/main.c @@ -5,6 +5,8 @@ * You can obtain one at https://mozilla.org/MPL/2.0/. */ +#include "main.h" + #include #include #include @@ -27,20 +29,21 @@ #define MAX(a, b) ((a) > (b) ? (a) : (b)) -static void exec_config(const char *path); static void sig_handler(int sig); bool g_is_about_to_quit = false; +static char *g_config_path; + int main(int argc, char *argv[]) { argv0 = argv[0]; - char config_path[PATH_MAX]; int option; + g_config_path = malloc(PATH_MAX); - config_path[0] = '\0'; + g_config_path[0] = '\0'; while ((option = getopt(argc, argv, "hvc:")) != -1) { switch (option) { @@ -51,18 +54,18 @@ main(int argc, char *argv[]) printf("v" VERSION_STRING "\n"); exit(EXIT_SUCCESS); case 'c': - strncpy(config_path, optarg, sizeof(config_path) - 1); + strncpy(g_config_path, optarg, PATH_MAX - 1); break; } } // default config path - if (!config_path[0]) { + if (!g_config_path[0]) { char *config_home = getenv("XDG_CONFIG_HOME"); if (config_home) { snprintf( - config_path, - sizeof(config_path), + g_config_path, + PATH_MAX, "%s/%s", config_home, CONFIG_FILE @@ -70,8 +73,8 @@ main(int argc, char *argv[]) } else { // ${HOME}/.config as fallback snprintf( - config_path, - sizeof(config_path), + g_config_path, + PATH_MAX, "%s/%s", getenv("HOME"), ".config/" CONFIG_FILE @@ -88,7 +91,7 @@ main(int argc, char *argv[]) init_socket(); init_xcb(); - exec_config(config_path); + exec_config(); int max_fd = MAX(g_sock_fd, g_dpy_fd) + 1; fd_set file_descriptors; @@ -112,7 +115,7 @@ main(int argc, char *argv[]) if (!(client_fd < 0) && (msg_len = read(client_fd, msg, sizeof(msg))) > 0) { // client sent something msg[msg_len] = '\0'; - // TODO: handle event + handle_ipc_event(msg); close(client_fd); } } @@ -126,19 +129,20 @@ main(int argc, char *argv[]) } } + free(g_config_path); close(g_sock_fd); xcb_disconnect(g_dpy); } -static void -exec_config(const char *path) +void +exec_config(void) { switch(fork()) { case -1: warn("failed to execute config\n"); break; case 0: - execl(path, path, NULL); + execl(g_config_path, g_config_path, NULL); die("failed to execute config\n"); } } diff --git a/src/kiwmi/main.h b/src/kiwmi/main.h new file mode 100644 index 0000000..7336143 --- /dev/null +++ b/src/kiwmi/main.h @@ -0,0 +1,10 @@ +#ifndef MAIN_H +#define MAIN_H + +#include + +void exec_config(void); + +extern bool g_is_about_to_quit; + +#endif /* MAIN_H */