Commit a3daecf2087655e7a090464e3ae8c59d49e37307 Parent: a342482973b79b927b11731bef82c3bebc624d6b Author: mcol <mcol@posteo.net> Date: 2021-02-21 23:50:51 +0000 Committer: mcol <mcol@posteo.net> Committed: 2021-02-21 23:50:51 +0000 Send responses from wimp to wimptool
src/wimptool.c Modified
@@ -2,12 +2,13 @@ #include <stdlib.h> #include <sys/socket.h> #include <sys/un.h> +#include <unistd.h> #define SOCKET_PATH "/tmp/wimpy-sock-%s" void usage() { - printf("Usage: wimptool <command> <arguments>\n"); + fprintf(stdout, "Usage: wimptool <command> <arguments>\n"); } @@ -19,13 +20,13 @@ char *display = getenv("WAYLAND_DISPLAY"); if (!display) { - printf("WAYLAND_DISPLAY not set.\n"); + fprintf(stderr, "WAYLAND_DISPLAY not set.\n"); return EXIT_FAILURE; } int sock; if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { - printf("Failed to create socket.\n"); + fprintf(stderr, "Failed to create socket.\n"); return EXIT_FAILURE; } @@ -34,7 +35,7 @@ addr.sun_family = AF_UNIX; if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) { - printf("Failed to connect to wimp.\n"); + fprintf(stderr, "Failed to connect to wimp.\n"); return EXIT_FAILURE; } @@ -45,9 +46,21 @@ } if (send(sock, buffer, strlen(buffer), 0) == -1) { - printf("Failed to send message to wimp.\n"); + fprintf(stderr, "Failed to send message to wimp.\n"); return EXIT_FAILURE; } + char response[BUFSIZ] = {0}; + ssize_t len = recv(sock, response, sizeof(response) - 1, 0); + if (len < 0) { + fprintf(stderr, "Error receiving a response.\n"); + return EXIT_FAILURE; + } + + if (len > 0) { + response[len] = '\0'; + fprintf(stdout, "wimp: %s\n", response); + } + close(sock); return EXIT_SUCCESS; }
src/ipc.c Modified
@@ -16,7 +16,8 @@ static int dispatch(int sock, unsigned int mask, void *data) { - char buffer[1024]; + char message[1024]; + char response[1024] = {0}; if (mask & WL_EVENT_READABLE) { int fd = accept(sock, NULL, NULL); @@ -24,10 +25,11 @@ wlr_log(WLR_ERROR, "Failed to accept connection from client."); return 0; } - ssize_t len = recv(fd, buffer, sizeof(buffer) - 1, 0); - if (len > 0) { - buffer[len] = '\0'; - handle_message(buffer); + ssize_t len = recv(fd, message, sizeof(message) - 1, 0); + message[len] = '\0'; + handle_message(message, response); + if (response[0]) { + send(fd, response, strlen(response), 0); } close(fd); }
src/config.h Modified
@@ -5,7 +5,7 @@ bool wlr_box_from_str(char* str, struct wlr_box *box); void assign_colour(char *hex, float dest[4]); -void handle_message(char *buffer); +void handle_message(char *buffer, char *response); void schedule_startup(); void set_up_defaults();
src/config.c Modified
@@ -478,11 +478,8 @@ } -void handle_message(char *message) { - char *s; - if (!(s = strtok(message, " \t\n\r"))) { - return; - } +void handle_message(char *message, char *response) { + char *s = strtok(message, " \t\n\r"); // desks <int> if (!strcasecmp(s, "desks")) { @@ -614,7 +611,7 @@ // other non-empty, non-commented lines else { if(!handle_do_action(s)) { - wlr_log(WLR_ERROR, "Unknown command '%s'.", s); + sprintf(response, "Unknown command '%s'.", s); } } } @@ -675,7 +672,7 @@ "bind Ctrl Escape shutdown", }; for (size_t i = 0; i < sizeof(defaults) / sizeof(defaults[0]); i++) { - handle_message(defaults[i]); + handle_message(defaults[i], NULL); } setup_vt_switching(); }