diff options
-rw-r--r-- | common/ipc-client.c | 21 | ||||
-rw-r--r-- | include/ipc-client.h | 13 | ||||
-rw-r--r-- | swaygrab/main.c | 55 | ||||
-rw-r--r-- | swaymsg/main.c | 5 |
4 files changed, 81 insertions, 13 deletions
diff --git a/common/ipc-client.c b/common/ipc-client.c index 916676a9..c3a9c9a5 100644 --- a/common/ipc-client.c +++ b/common/ipc-client.c @@ -24,7 +24,7 @@ char *get_socketpath(void) { return line; } -char *ipc_single_command(const char *socket_path, uint32_t type, const char *payload, uint32_t len) { +int ipc_open_socket(const char *socket_path) { struct sockaddr_un addr; int socketfd; if ((socketfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { @@ -36,18 +36,21 @@ char *ipc_single_command(const char *socket_path, uint32_t type, const char *pay if (connect(socketfd, (struct sockaddr *)&addr, l) == -1) { sway_abort("Unable to connect to %s", socket_path); } + return socketfd; +} +char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len) { char data[ipc_header_size]; uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic)); memcpy(data, ipc_magic, sizeof(ipc_magic)); - data32[0] = len; + data32[0] = *len; data32[1] = type; if (write(socketfd, data, ipc_header_size) == -1) { sway_abort("Unable to send IPC header"); } - if (write(socketfd, payload, len) == -1) { + if (write(socketfd, payload, *len) == -1) { sway_abort("Unable to send IPC payload"); } @@ -61,18 +64,16 @@ char *ipc_single_command(const char *socket_path, uint32_t type, const char *pay } total = 0; - len = data32[0]; - char *response = malloc(len + 1); - while (total < len) { - ssize_t received = recv(socketfd, response + total, len - total, 0); + *len = data32[0]; + char *response = malloc(*len + 1); + while (total < *len) { + ssize_t received = recv(socketfd, response + total, *len - total, 0); if (received < 0) { sway_abort("Unable to receive IPC response"); } total += received; } - response[len] = '\0'; - - close(socketfd); + response[*len] = '\0'; return response; } diff --git a/include/ipc-client.h b/include/ipc-client.h index a56fee43..e6c988c2 100644 --- a/include/ipc-client.h +++ b/include/ipc-client.h @@ -3,7 +3,18 @@ #include "ipc.h" +/** + * Gets the path to the IPC socket from sway. + */ char *get_socketpath(void); -char *ipc_single_command(const char *socket_path, uint32_t type, const char *payload, uint32_t len); +/** + * Opens the sway socket. + */ +int ipc_open_socket(const char *socket_path); +/** + * Issues a single IPC command and returns the buffer. len will be updated with + * the length of the buffer returned from sway. + */ +char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len); #endif diff --git a/swaygrab/main.c b/swaygrab/main.c index 4a15bd06..bd64bd93 100644 --- a/swaygrab/main.c +++ b/swaygrab/main.c @@ -1,12 +1,65 @@ #include <stdio.h> #include <stdlib.h> #include "log.h" +#include "ipc-client.h" void sway_terminate(void) { exit(1); } int main(int argc, const char **argv) { + int capture; + char *socket_path = NULL; + init_log(L_INFO); - sway_log(L_INFO, "Hello world!"); + + static struct option long_options[] = { + {"capture", no_argument, &capture, 'c'}, + {"version", no_argument, NULL, 'v'}, + {"socket", required_argument, NULL, 's'}, + {0, 0, 0, 0} + }; + + int c; + while (1) { + int option_index = 0; + c = getopt_long(argc, argv, "cvs:", long_options, &option_index); + if (c == -1) { + break; + } + switch (c) { + case 0: // Flag + break; + case 's': // Socket + socket_path = strdup(optarg); + break; + case 'v': +#if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE + fprintf(stdout, "sway version %s (%s, branch \"%s\")\n", SWAY_GIT_VERSION, SWAY_VERSION_DATE, SWAY_GIT_BRANCH); +#else + fprintf(stdout, "version not detected\n"); +#endif + exit(0); + break; + } + } + + if (!socket_path) { + socket_path = get_socketpath(); + if (!socket_path) { + sway_abort("Unable to retrieve socket path"); + } + } + + if (optind >= argc) { + sway_abort("Expected output file on command line. See `man swaygrab`"); + } + + char *out = argv[optind]; + int socketfd = ipc_open_socket(socket_path); + free(socket_path); + + close(socketfd); + free(out); + return 0; } diff --git a/swaymsg/main.c b/swaymsg/main.c index 8d20905a..3a2e1ee7 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -93,10 +93,13 @@ int main(int argc, char **argv) { command = join_args(argv + optind, argc - optind); } - char *resp = ipc_single_command(socket_path, type, command, strlen(command)); + int socketfd = ipc_open_socket(socket_path); + uint32_t len = strlen(command); + char *resp = ipc_single_command(socketfd, type, command, &len); if (!quiet) { printf("%s", resp); } + close(socketfd); free(command); free(resp); |