From 21f8186fc54a2928a3c272ec38db57ec3c02edd0 Mon Sep 17 00:00:00 2001 From: buffet Date: Mon, 1 Oct 2018 11:45:38 +0200 Subject: [PATCH] Added basic main loop --- lit/wmaffle.lit | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/lit/wmaffle.lit b/lit/wmaffle.lit index 6979557..9814803 100644 --- a/lit/wmaffle.lit +++ b/lit/wmaffle.lit @@ -18,6 +18,8 @@ Here's an overview: char *argv0; +int g_about_to_quit = 0; + @{function definitions} int @@ -33,6 +35,8 @@ main(int argc, char *argv[]) @{execute config} + @{main loop} + @{cleanup} } --- @@ -292,3 +296,56 @@ We need to invoke this once all the connections are open --- execute config exec_config(config_path); --- + +@s Main loop + +This is the heart of wmaffle. + +A pretty basic loop: fetch event, decide if it's from X or the client, handle it. +`select` is used for the first two steps. + +--- posix headers += +#include +--- + +--- variables local to main += +int max_fd; +fd_set file_descriptors; +--- + +--- main loop +max_fd = sock_fd + 1; + +while (!g_about_to_quit) { + FD_ZERO(&file_descriptors); + FD_SET(sock_fd, &file_descriptors); + + select(max_fd, &file_descriptors, NULL, NULL, NULL); + + if (FD_ISSET(sock_fd, &file_descriptors)) { + @{handle client event} + } +} +--- + +@s Handle client event + +Until there's an actual WM, I'm just going to read messages and then print them out, so we have some sort of feedback. + +--- variables local to main += +int client_fd; +--- + +--- handle client event +char msg[BUFSIZ]; +int msg_len; + +client_fd = accept(sock_fd, NULL, 0); + +if (!(client_fd < 0) && (msg_len = read(client_fd, msg, sizeof(msg))) > 0) { + // Client sent something + msg[msg_len] = '\0'; + printf("Client sent: %s\n", msg); + close(client_fd); +} +---