From da4f881cc99b7cdcf333c73fbf16c74a4d19dd05 Mon Sep 17 00:00:00 2001 From: Charlotte Meyer Date: Sat, 13 Apr 2019 21:45:34 +0200 Subject: [PATCH] Basic command handling (without arguments) --- include/kiwmi/commands.h | 21 +++++++++++++++++ kiwmi/commands.c | 50 ++++++++++++++++++++++++++++++++++++++++ kiwmi/frontend.c | 11 ++++++--- kiwmi/meson.build | 1 + 4 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 include/kiwmi/commands.h create mode 100644 kiwmi/commands.c diff --git a/include/kiwmi/commands.h b/include/kiwmi/commands.h new file mode 100644 index 0000000..227891c --- /dev/null +++ b/include/kiwmi/commands.h @@ -0,0 +1,21 @@ +/* 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/kiwmi/commands.c b/kiwmi/commands.c new file mode 100644 index 0000000..f3f6d0a --- /dev/null +++ b/kiwmi/commands.c @@ -0,0 +1,50 @@ +/* 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 + +static bool +cmd_quit( + FILE *UNUSED(client), + const char **UNUSED(args), + struct kiwmi_server *server) +{ + wl_display_terminate(server->wl_display); + return false; +} + +typedef bool ( + *cmd_handler)(FILE *client, const char **args, struct kiwmi_server *server); + +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/frontend.c b/kiwmi/frontend.c index 5ade5b2..f805742 100644 --- a/kiwmi/frontend.c +++ b/kiwmi/frontend.c @@ -20,6 +20,7 @@ #include +#include "kiwmi/commands.h" #include "kiwmi/server.h" static void @@ -40,10 +41,12 @@ display_destroy_notify(struct wl_listener *listener, void *UNUSED(data)) } static int -ipc_connection(int fd, uint32_t mask, void *UNUSED(data)) +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; } @@ -85,8 +88,10 @@ ipc_connection(int fd, uint32_t mask, void *UNUSED(data)) msg[msg_len] = '\0'; - for (const char *cmd = strtok(msg, "\n"); cmd; cmd = strtok(NULL, "\n")) { - // TODO: handle client command + for (char *cmd = strtok(msg, "\n"); cmd; cmd = strtok(NULL, "\n")) { + if (!handle_client_command(cmd, client_file, server)) { + break; + } } fclose(client_file); diff --git a/kiwmi/meson.build b/kiwmi/meson.build index 3a8feeb..5c1d227 100644 --- a/kiwmi/meson.build +++ b/kiwmi/meson.build @@ -1,5 +1,6 @@ kiwmi_sources = files( 'main.c', + 'commands.c', 'frontend.c', 'server.c', 'desktop/desktop.c',