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) { | |||
24 | return line; | 24 | return line; |
25 | } | 25 | } |
26 | 26 | ||
27 | char *ipc_single_command(const char *socket_path, uint32_t type, const char *payload, uint32_t len) { | 27 | int ipc_open_socket(const char *socket_path) { |
28 | struct sockaddr_un addr; | 28 | struct sockaddr_un addr; |
29 | int socketfd; | 29 | int socketfd; |
30 | if ((socketfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { | 30 | 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 | |||
36 | if (connect(socketfd, (struct sockaddr *)&addr, l) == -1) { | 36 | if (connect(socketfd, (struct sockaddr *)&addr, l) == -1) { |
37 | sway_abort("Unable to connect to %s", socket_path); | 37 | sway_abort("Unable to connect to %s", socket_path); |
38 | } | 38 | } |
39 | return socketfd; | ||
40 | } | ||
39 | 41 | ||
42 | char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len) { | ||
40 | char data[ipc_header_size]; | 43 | char data[ipc_header_size]; |
41 | uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic)); | 44 | uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic)); |
42 | memcpy(data, ipc_magic, sizeof(ipc_magic)); | 45 | memcpy(data, ipc_magic, sizeof(ipc_magic)); |
43 | data32[0] = len; | 46 | data32[0] = *len; |
44 | data32[1] = type; | 47 | data32[1] = type; |
45 | 48 | ||
46 | if (write(socketfd, data, ipc_header_size) == -1) { | 49 | if (write(socketfd, data, ipc_header_size) == -1) { |
47 | sway_abort("Unable to send IPC header"); | 50 | sway_abort("Unable to send IPC header"); |
48 | } | 51 | } |
49 | 52 | ||
50 | if (write(socketfd, payload, len) == -1) { | 53 | if (write(socketfd, payload, *len) == -1) { |
51 | sway_abort("Unable to send IPC payload"); | 54 | sway_abort("Unable to send IPC payload"); |
52 | } | 55 | } |
53 | 56 | ||
@@ -61,18 +64,16 @@ char *ipc_single_command(const char *socket_path, uint32_t type, const char *pay | |||
61 | } | 64 | } |
62 | 65 | ||
63 | total = 0; | 66 | total = 0; |
64 | len = data32[0]; | 67 | *len = data32[0]; |
65 | char *response = malloc(len + 1); | 68 | char *response = malloc(*len + 1); |
66 | while (total < len) { | 69 | while (total < *len) { |
67 | ssize_t received = recv(socketfd, response + total, len - total, 0); | 70 | ssize_t received = recv(socketfd, response + total, *len - total, 0); |
68 | if (received < 0) { | 71 | if (received < 0) { |
69 | sway_abort("Unable to receive IPC response"); | 72 | sway_abort("Unable to receive IPC response"); |
70 | } | 73 | } |
71 | total += received; | 74 | total += received; |
72 | } | 75 | } |
73 | response[len] = '\0'; | 76 | response[*len] = '\0'; |
74 | |||
75 | close(socketfd); | ||
76 | 77 | ||
77 | return response; | 78 | return response; |
78 | } | 79 | } |
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 @@ | |||
3 | 3 | ||
4 | #include "ipc.h" | 4 | #include "ipc.h" |
5 | 5 | ||
6 | /** | ||
7 | * Gets the path to the IPC socket from sway. | ||
8 | */ | ||
6 | char *get_socketpath(void); | 9 | char *get_socketpath(void); |
7 | char *ipc_single_command(const char *socket_path, uint32_t type, const char *payload, uint32_t len); | 10 | /** |
11 | * Opens the sway socket. | ||
12 | */ | ||
13 | int ipc_open_socket(const char *socket_path); | ||
14 | /** | ||
15 | * Issues a single IPC command and returns the buffer. len will be updated with | ||
16 | * the length of the buffer returned from sway. | ||
17 | */ | ||
18 | char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len); | ||
8 | 19 | ||
9 | #endif | 20 | #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 @@ | |||
1 | #include <stdio.h> | 1 | #include <stdio.h> |
2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
3 | #include "log.h" | 3 | #include "log.h" |
4 | #include "ipc-client.h" | ||
4 | 5 | ||
5 | void sway_terminate(void) { | 6 | void sway_terminate(void) { |
6 | exit(1); | 7 | exit(1); |
7 | } | 8 | } |
8 | 9 | ||
9 | int main(int argc, const char **argv) { | 10 | int main(int argc, const char **argv) { |
11 | int capture; | ||
12 | char *socket_path = NULL; | ||
13 | |||
10 | init_log(L_INFO); | 14 | init_log(L_INFO); |
11 | sway_log(L_INFO, "Hello world!"); | 15 | |
16 | static struct option long_options[] = { | ||
17 | {"capture", no_argument, &capture, 'c'}, | ||
18 | {"version", no_argument, NULL, 'v'}, | ||
19 | {"socket", required_argument, NULL, 's'}, | ||
20 | {0, 0, 0, 0} | ||
21 | }; | ||
22 | |||
23 | int c; | ||
24 | while (1) { | ||
25 | int option_index = 0; | ||
26 | c = getopt_long(argc, argv, "cvs:", long_options, &option_index); | ||
27 | if (c == -1) { | ||
28 | break; | ||
29 | } | ||
30 | switch (c) { | ||
31 | case 0: // Flag | ||
32 | break; | ||
33 | case 's': // Socket | ||
34 | socket_path = strdup(optarg); | ||
35 | break; | ||
36 | case 'v': | ||
37 | #if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE | ||
38 | fprintf(stdout, "sway version %s (%s, branch \"%s\")\n", SWAY_GIT_VERSION, SWAY_VERSION_DATE, SWAY_GIT_BRANCH); | ||
39 | #else | ||
40 | fprintf(stdout, "version not detected\n"); | ||
41 | #endif | ||
42 | exit(0); | ||
43 | break; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | if (!socket_path) { | ||
48 | socket_path = get_socketpath(); | ||
49 | if (!socket_path) { | ||
50 | sway_abort("Unable to retrieve socket path"); | ||
51 | } | ||
52 | } | ||
53 | |||
54 | if (optind >= argc) { | ||
55 | sway_abort("Expected output file on command line. See `man swaygrab`"); | ||
56 | } | ||
57 | |||
58 | char *out = argv[optind]; | ||
59 | int socketfd = ipc_open_socket(socket_path); | ||
60 | free(socket_path); | ||
61 | |||
62 | close(socketfd); | ||
63 | free(out); | ||
64 | return 0; | ||
12 | } | 65 | } |
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) { | |||
93 | command = join_args(argv + optind, argc - optind); | 93 | command = join_args(argv + optind, argc - optind); |
94 | } | 94 | } |
95 | 95 | ||
96 | char *resp = ipc_single_command(socket_path, type, command, strlen(command)); | 96 | int socketfd = ipc_open_socket(socket_path); |
97 | uint32_t len = strlen(command); | ||
98 | char *resp = ipc_single_command(socketfd, type, command, &len); | ||
97 | if (!quiet) { | 99 | if (!quiet) { |
98 | printf("%s", resp); | 100 | printf("%s", resp); |
99 | } | 101 | } |
102 | close(socketfd); | ||
100 | 103 | ||
101 | free(command); | 104 | free(command); |
102 | free(resp); | 105 | free(resp); |