Basic command handling (without arguments)

This commit is contained in:
buffet 2019-04-13 21:45:34 +02:00
parent 8b2d2bcd50
commit da4f881cc9
4 changed files with 80 additions and 3 deletions

21
include/kiwmi/commands.h Normal file
View file

@ -0,0 +1,21 @@
/* Copyright (c), Charlotte Meyer <dev@buffet.sh>
*
* 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 <stdbool.h>
#include <stdio.h>
#include "kiwmi/server.h"
bool handle_client_command(
char *command,
FILE *client,
struct kiwmi_server *server);
#endif /* KIWMI_COMMANDS_H */

50
kiwmi/commands.c Normal file
View file

@ -0,0 +1,50 @@
/* Copyright (c), Charlotte Meyer <dev@buffet.sh>
*
* 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 <string.h>
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
}

View file

@ -20,6 +20,7 @@
#include <wlr/util/log.h>
#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);

View file

@ -1,5 +1,6 @@
kiwmi_sources = files(
'main.c',
'commands.c',
'frontend.c',
'server.c',
'desktop/desktop.c',