diff options
Diffstat (limited to 'swaymsg')
-rw-r--r-- | swaymsg/main.c | 70 |
1 files changed, 2 insertions, 68 deletions
diff --git a/swaymsg/main.c b/swaymsg/main.c index ea8e0a55..8d20905a 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c | |||
@@ -7,80 +7,14 @@ | |||
7 | #include <sys/socket.h> | 7 | #include <sys/socket.h> |
8 | #include <unistd.h> | 8 | #include <unistd.h> |
9 | #include "stringop.h" | 9 | #include "stringop.h" |
10 | #include "ipc.h" | 10 | #include "ipc-client.h" |
11 | #include "readline.h" | 11 | #include "readline.h" |
12 | #include "log.h" | 12 | #include "log.h" |
13 | 13 | ||
14 | static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; | ||
15 | static const size_t ipc_header_size = sizeof(ipc_magic)+8; | ||
16 | |||
17 | void sway_terminate(void) { | 14 | void sway_terminate(void) { |
18 | exit(1); | 15 | exit(1); |
19 | } | 16 | } |
20 | 17 | ||
21 | char *get_socketpath(void) { | ||
22 | FILE *fp = popen("sway --get-socketpath", "r"); | ||
23 | if (!fp) { | ||
24 | return NULL; | ||
25 | } | ||
26 | char *line = read_line(fp); | ||
27 | pclose(fp); | ||
28 | return line; | ||
29 | } | ||
30 | |||
31 | char *do_ipc(const char *socket_path, uint32_t type, const char *payload, uint32_t len) { | ||
32 | struct sockaddr_un addr; | ||
33 | int socketfd; | ||
34 | if ((socketfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { | ||
35 | sway_abort("Unable to open Unix socket"); | ||
36 | } | ||
37 | addr.sun_family = AF_UNIX; | ||
38 | strcpy(addr.sun_path, socket_path); | ||
39 | int l = sizeof(addr.sun_family) + strlen(addr.sun_path); | ||
40 | if (connect(socketfd, (struct sockaddr *)&addr, l) == -1) { | ||
41 | sway_abort("Unable to connect to %s", socket_path); | ||
42 | } | ||
43 | |||
44 | char data[ipc_header_size]; | ||
45 | uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic)); | ||
46 | memcpy(data, ipc_magic, sizeof(ipc_magic)); | ||
47 | data32[0] = len; | ||
48 | data32[1] = type; | ||
49 | |||
50 | if (write(socketfd, data, ipc_header_size) == -1) { | ||
51 | sway_abort("Unable to send IPC header"); | ||
52 | } | ||
53 | |||
54 | if (write(socketfd, payload, len) == -1) { | ||
55 | sway_abort("Unable to send IPC payload"); | ||
56 | } | ||
57 | |||
58 | size_t total = 0; | ||
59 | while (total < ipc_header_size) { | ||
60 | ssize_t received = recv(socketfd, data + total, ipc_header_size - total, 0); | ||
61 | if (received < 0) { | ||
62 | sway_abort("Unable to receive IPC response"); | ||
63 | } | ||
64 | total += received; | ||
65 | } | ||
66 | |||
67 | total = 0; | ||
68 | len = data32[0]; | ||
69 | char *response = malloc(len + 1); | ||
70 | while (total < len) { | ||
71 | ssize_t received = recv(socketfd, response + total, len - total, 0); | ||
72 | if (received < 0) { | ||
73 | sway_abort("Unable to receive IPC response"); | ||
74 | } | ||
75 | total += received; | ||
76 | } | ||
77 | response[len] = '\0'; | ||
78 | |||
79 | close(socketfd); | ||
80 | |||
81 | return response; | ||
82 | } | ||
83 | |||
84 | int main(int argc, char **argv) { | 18 | int main(int argc, char **argv) { |
85 | static int quiet = 0; | 19 | static int quiet = 0; |
86 | char *socket_path = NULL; | 20 | char *socket_path = NULL; |
@@ -159,7 +93,7 @@ int main(int argc, char **argv) { | |||
159 | command = join_args(argv + optind, argc - optind); | 93 | command = join_args(argv + optind, argc - optind); |
160 | } | 94 | } |
161 | 95 | ||
162 | char *resp = do_ipc(socket_path, type, command, strlen(command)); | 96 | char *resp = ipc_single_command(socket_path, type, command, strlen(command)); |
163 | if (!quiet) { | 97 | if (!quiet) { |
164 | printf("%s", resp); | 98 | printf("%s", resp); |
165 | } | 99 | } |