Add command line to swaygrab

Also modifies IPC client so that we can work with persistent
connections.
This commit is contained in:
Drew DeVault 2015-11-27 10:10:29 -05:00
parent 59e97c2788
commit 062c74b7d0
4 changed files with 81 additions and 13 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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;
}

View File

@ -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);