Delete frontend, add config

This commit is contained in:
buffet 2019-10-16 18:59:29 +02:00
parent f81cfc1725
commit 5a362d63c5
13 changed files with 33 additions and 362 deletions

View file

@ -1,19 +0,0 @@
/* 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 */

View file

@ -1,26 +0,0 @@
/* 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_FRONTEND_H
#define KIWMI_FRONTEND_H
#include <stdbool.h>
#include <stdio.h>
#include <wayland-server.h>
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 */

View file

@ -10,9 +10,8 @@
#include <stdbool.h> #include <stdbool.h>
#include "kiwmi/desktop/desktop.h" #include "desktop/desktop.h"
#include "kiwmi/frontend.h" #include "input/input.h"
#include "kiwmi/input/input.h"
struct kiwmi_server { struct kiwmi_server {
struct wl_display *wl_display; struct wl_display *wl_display;
@ -21,10 +20,9 @@ struct kiwmi_server {
const char *socket; const char *socket;
struct kiwmi_desktop desktop; struct kiwmi_desktop desktop;
struct kiwmi_input input; 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); bool server_run(struct kiwmi_server *server);
void server_fini(struct kiwmi_server *server); void server_fini(struct kiwmi_server *server);

View file

@ -1,50 +0,0 @@
/* 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>
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
}

View file

@ -5,7 +5,7 @@
* You can obtain one at https://mozilla.org/MPL/2.0/. * You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
#include "kiwmi/desktop/desktop.h" #include "desktop/desktop.h"
#include <stdbool.h> #include <stdbool.h>
@ -15,8 +15,8 @@
#include <wlr/types/wlr_data_device.h> #include <wlr/types/wlr_data_device.h>
#include <wlr/types/wlr_output_layout.h> #include <wlr/types/wlr_output_layout.h>
#include "kiwmi/desktop/output.h" #include "desktop/output.h"
#include "kiwmi/server.h" #include "server.h"
bool bool
desktop_init(struct kiwmi_desktop *desktop, struct wlr_renderer *renderer) desktop_init(struct kiwmi_desktop *desktop, struct wlr_renderer *renderer)

View file

@ -5,7 +5,7 @@
* You can obtain one at https://mozilla.org/MPL/2.0/. * You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
#include "kiwmi/desktop/output.h" #include "desktop/output.h"
#include <stdlib.h> #include <stdlib.h>
@ -17,10 +17,10 @@
#include <wlr/types/wlr_xcursor_manager.h> #include <wlr/types/wlr_xcursor_manager.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
#include "kiwmi/desktop/desktop.h" #include "desktop/desktop.h"
#include "kiwmi/input/cursor.h" #include "input/cursor.h"
#include "kiwmi/input/input.h" #include "input/input.h"
#include "kiwmi/server.h" #include "server.h"
static void static void
output_frame_notify(struct wl_listener *listener, void *data) output_frame_notify(struct wl_listener *listener, void *data)

View file

@ -1,220 +0,0 @@
/* 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/frontend.h"
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#include <wlr/util/log.h>
#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);
}
}

View file

@ -5,7 +5,7 @@
* You can obtain one at https://mozilla.org/MPL/2.0/. * You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
#include "kiwmi/input/cursor.h" #include "input/cursor.h"
#include <stdlib.h> #include <stdlib.h>

View file

@ -5,7 +5,7 @@
* You can obtain one at https://mozilla.org/MPL/2.0/. * You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
#include "kiwmi/input/input.h" #include "input/input.h"
#include <wayland-server.h> #include <wayland-server.h>
#include <wlr/backend.h> #include <wlr/backend.h>
@ -13,10 +13,10 @@
#include <wlr/types/wlr_input_device.h> #include <wlr/types/wlr_input_device.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
#include "kiwmi/desktop/desktop.h" #include "desktop/desktop.h"
#include "kiwmi/input/cursor.h" #include "input/cursor.h"
#include "kiwmi/input/keyboard.h" #include "input/keyboard.h"
#include "kiwmi/server.h" #include "server.h"
static void static void
new_pointer(struct kiwmi_input *input, struct wlr_input_device *device) new_pointer(struct kiwmi_input *input, struct wlr_input_device *device)

View file

@ -5,7 +5,7 @@
* You can obtain one at https://mozilla.org/MPL/2.0/. * You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
#include "kiwmi/input/keyboard.h" #include "input/keyboard.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
@ -17,7 +17,7 @@
#include <wlr/util/log.h> #include <wlr/util/log.h>
#include <xkbcommon/xkbcommon.h> #include <xkbcommon/xkbcommon.h>
#include "kiwmi/server.h" #include "server.h"
static bool static bool
switch_vt(const xkb_keysym_t *syms, int nsyms, struct wlr_backend *backend) switch_vt(const xkb_keysym_t *syms, int nsyms, struct wlr_backend *backend)

View file

@ -14,23 +14,24 @@
#include <wlr/util/log.h> #include <wlr/util/log.h>
#include "kiwmi/server.h" #include "server.h"
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
int verbosity = 0; int verbosity = 0;
const char *frontend_path; const char *config_path = NULL;
const char *usage = const char *usage =
"Usage: kiwmi [options] FRONTEND\n" "Usage: kiwmi [options]\n"
"\n" "\n"
" -h Show help message and quit\n" " -h Show help message and exit\n"
" -v Show version number and quit\n" " -v Show version number and exit\n"
" -V Increase verbosity Level\n"; " -c Change config path\n"
" -V Increase verbosity level\n";
int option; int option;
while ((option = getopt(argc, argv, "hvV")) != -1) { while ((option = getopt(argc, argv, "hvc:V")) != -1) {
switch (option) { switch (option) {
case 'h': case 'h':
printf("%s", usage); printf("%s", usage);
@ -40,6 +41,9 @@ main(int argc, char **argv)
printf("kiwmi version " KIWMI_VERSION "\n"); printf("kiwmi version " KIWMI_VERSION "\n");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break; break;
case 'c':
config_path = optarg;
break;
case 'V': case 'V':
++verbosity; ++verbosity;
break; 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) { switch (verbosity) {
case 0: case 0:
wlr_log_init(WLR_ERROR, NULL); wlr_log_init(WLR_ERROR, NULL);
@ -77,7 +73,7 @@ main(int argc, char **argv)
struct kiwmi_server server; struct kiwmi_server server;
if (!server_init(&server, frontend_path)) { if (!server_init(&server, config_path)) {
wlr_log(WLR_ERROR, "Failed to initialize server"); wlr_log(WLR_ERROR, "Failed to initialize server");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }

View file

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

View file

@ -5,7 +5,7 @@
* You can obtain one at https://mozilla.org/MPL/2.0/. * You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
#include "kiwmi/server.h" #include "server.h"
#include <stdlib.h> #include <stdlib.h>
@ -15,7 +15,7 @@
#include <wlr/util/log.h> #include <wlr/util/log.h>
bool 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"); wlr_log(WLR_DEBUG, "Initializing Wayland server");
@ -50,12 +50,6 @@ server_init(struct kiwmi_server *server, const char *frontend_path)
return false; 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; return true;
} }