Added basic IPC event handling (quit, reload)

- Added main.h
- config_path is now global
This commit is contained in:
buffet 2018-10-29 15:18:25 +01:00
parent 21473c93a8
commit 40f66a03b2
4 changed files with 48 additions and 14 deletions

View file

@ -7,6 +7,7 @@
#include "ipc.h" #include "ipc.h"
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -15,8 +16,12 @@
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#include "main.h"
#include "common.h" #include "common.h"
#define STREQ(a, b) (strcmp((a), (b)) == 0)
int g_sock_fd; int g_sock_fd;
void void
@ -50,3 +55,17 @@ init_socket(void)
die("failed to listen to socket\n"); 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);
}
}

View file

@ -9,6 +9,7 @@
#define IPC_H #define IPC_H
void init_socket(void); void init_socket(void);
void handle_ipc_event(char *msg);
extern int g_sock_fd; extern int g_sock_fd;

View file

@ -5,6 +5,8 @@
* You can obtain one at https://mozilla.org/MPL/2.0/. * You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
#include "main.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -27,20 +29,21 @@
#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b))
static void exec_config(const char *path);
static void sig_handler(int sig); static void sig_handler(int sig);
bool g_is_about_to_quit = false; bool g_is_about_to_quit = false;
static char *g_config_path;
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
argv0 = argv[0]; argv0 = argv[0];
char config_path[PATH_MAX];
int option; int option;
g_config_path = malloc(PATH_MAX);
config_path[0] = '\0'; g_config_path[0] = '\0';
while ((option = getopt(argc, argv, "hvc:")) != -1) { while ((option = getopt(argc, argv, "hvc:")) != -1) {
switch (option) { switch (option) {
@ -51,18 +54,18 @@ main(int argc, char *argv[])
printf("v" VERSION_STRING "\n"); printf("v" VERSION_STRING "\n");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
case 'c': case 'c':
strncpy(config_path, optarg, sizeof(config_path) - 1); strncpy(g_config_path, optarg, PATH_MAX - 1);
break; break;
} }
} }
// default config path // default config path
if (!config_path[0]) { if (!g_config_path[0]) {
char *config_home = getenv("XDG_CONFIG_HOME"); char *config_home = getenv("XDG_CONFIG_HOME");
if (config_home) { if (config_home) {
snprintf( snprintf(
config_path, g_config_path,
sizeof(config_path), PATH_MAX,
"%s/%s", "%s/%s",
config_home, config_home,
CONFIG_FILE CONFIG_FILE
@ -70,8 +73,8 @@ main(int argc, char *argv[])
} else { } else {
// ${HOME}/.config as fallback // ${HOME}/.config as fallback
snprintf( snprintf(
config_path, g_config_path,
sizeof(config_path), PATH_MAX,
"%s/%s", "%s/%s",
getenv("HOME"), getenv("HOME"),
".config/" CONFIG_FILE ".config/" CONFIG_FILE
@ -88,7 +91,7 @@ main(int argc, char *argv[])
init_socket(); init_socket();
init_xcb(); init_xcb();
exec_config(config_path); exec_config();
int max_fd = MAX(g_sock_fd, g_dpy_fd) + 1; int max_fd = MAX(g_sock_fd, g_dpy_fd) + 1;
fd_set file_descriptors; 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) { if (!(client_fd < 0) && (msg_len = read(client_fd, msg, sizeof(msg))) > 0) {
// client sent something // client sent something
msg[msg_len] = '\0'; msg[msg_len] = '\0';
// TODO: handle event handle_ipc_event(msg);
close(client_fd); close(client_fd);
} }
} }
@ -126,19 +129,20 @@ main(int argc, char *argv[])
} }
} }
free(g_config_path);
close(g_sock_fd); close(g_sock_fd);
xcb_disconnect(g_dpy); xcb_disconnect(g_dpy);
} }
static void void
exec_config(const char *path) exec_config(void)
{ {
switch(fork()) { switch(fork()) {
case -1: case -1:
warn("failed to execute config\n"); warn("failed to execute config\n");
break; break;
case 0: case 0:
execl(path, path, NULL); execl(g_config_path, g_config_path, NULL);
die("failed to execute config\n"); die("failed to execute config\n");
} }
} }

10
src/kiwmi/main.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef MAIN_H
#define MAIN_H
#include <stdbool.h>
void exec_config(void);
extern bool g_is_about_to_quit;
#endif /* MAIN_H */