diff options
-rw-r--r-- | common/ipc-client.c | 78 | ||||
-rw-r--r-- | include/ipc-client.h | 9 | ||||
-rw-r--r-- | include/ipc-server.h | 13 | ||||
-rw-r--r-- | include/ipc.h | 8 | ||||
-rw-r--r-- | sway/focus.c | 2 | ||||
-rw-r--r-- | sway/ipc-server.c (renamed from sway/ipc.c) | 2 | ||||
-rw-r--r-- | sway/main.c | 2 | ||||
-rw-r--r-- | swaymsg/main.c | 70 |
8 files changed, 105 insertions, 79 deletions
diff --git a/common/ipc-client.c b/common/ipc-client.c new file mode 100644 index 00000000..916676a9 --- /dev/null +++ b/common/ipc-client.c | |||
@@ -0,0 +1,78 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <string.h> | ||
4 | #include <getopt.h> | ||
5 | #include <stdint.h> | ||
6 | #include <sys/un.h> | ||
7 | #include <sys/socket.h> | ||
8 | #include <unistd.h> | ||
9 | #include "log.h" | ||
10 | #include "stringop.h" | ||
11 | #include "ipc-server.h" | ||
12 | #include "readline.h" | ||
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 | char *get_socketpath(void) { | ||
18 | FILE *fp = popen("sway --get-socketpath", "r"); | ||
19 | if (!fp) { | ||
20 | return NULL; | ||
21 | } | ||
22 | char *line = read_line(fp); | ||
23 | pclose(fp); | ||
24 | return line; | ||
25 | } | ||
26 | |||
27 | char *ipc_single_command(const char *socket_path, uint32_t type, const char *payload, uint32_t len) { | ||
28 | struct sockaddr_un addr; | ||
29 | int socketfd; | ||
30 | if ((socketfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { | ||
31 | sway_abort("Unable to open Unix socket"); | ||
32 | } | ||
33 | addr.sun_family = AF_UNIX; | ||
34 | strcpy(addr.sun_path, socket_path); | ||
35 | int l = sizeof(addr.sun_family) + strlen(addr.sun_path); | ||
36 | if (connect(socketfd, (struct sockaddr *)&addr, l) == -1) { | ||
37 | sway_abort("Unable to connect to %s", socket_path); | ||
38 | } | ||
39 | |||
40 | char data[ipc_header_size]; | ||
41 | uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic)); | ||
42 | memcpy(data, ipc_magic, sizeof(ipc_magic)); | ||
43 | data32[0] = len; | ||
44 | data32[1] = type; | ||
45 | |||
46 | if (write(socketfd, data, ipc_header_size) == -1) { | ||
47 | sway_abort("Unable to send IPC header"); | ||
48 | } | ||
49 | |||
50 | if (write(socketfd, payload, len) == -1) { | ||
51 | sway_abort("Unable to send IPC payload"); | ||
52 | } | ||
53 | |||
54 | size_t total = 0; | ||
55 | while (total < ipc_header_size) { | ||
56 | ssize_t received = recv(socketfd, data + total, ipc_header_size - total, 0); | ||
57 | if (received < 0) { | ||
58 | sway_abort("Unable to receive IPC response"); | ||
59 | } | ||
60 | total += received; | ||
61 | } | ||
62 | |||
63 | total = 0; | ||
64 | len = data32[0]; | ||
65 | char *response = malloc(len + 1); | ||
66 | while (total < len) { | ||
67 | ssize_t received = recv(socketfd, response + total, len - total, 0); | ||
68 | if (received < 0) { | ||
69 | sway_abort("Unable to receive IPC response"); | ||
70 | } | ||
71 | total += received; | ||
72 | } | ||
73 | response[len] = '\0'; | ||
74 | |||
75 | close(socketfd); | ||
76 | |||
77 | return response; | ||
78 | } | ||
diff --git a/include/ipc-client.h b/include/ipc-client.h new file mode 100644 index 00000000..a56fee43 --- /dev/null +++ b/include/ipc-client.h | |||
@@ -0,0 +1,9 @@ | |||
1 | #ifndef _SWAY_IPC_CLIENT_H | ||
2 | #define _SWAY_IPC_CLIENT_H | ||
3 | |||
4 | #include "ipc.h" | ||
5 | |||
6 | char *get_socketpath(void); | ||
7 | char *ipc_single_command(const char *socket_path, uint32_t type, const char *payload, uint32_t len); | ||
8 | |||
9 | #endif | ||
diff --git a/include/ipc-server.h b/include/ipc-server.h new file mode 100644 index 00000000..35b3748b --- /dev/null +++ b/include/ipc-server.h | |||
@@ -0,0 +1,13 @@ | |||
1 | #ifndef _SWAY_IPC_SERVER_H | ||
2 | #define _SWAY_IPC_SERVER_H | ||
3 | |||
4 | #include "container.h" | ||
5 | #include "ipc.h" | ||
6 | |||
7 | void ipc_init(void); | ||
8 | void ipc_terminate(void); | ||
9 | struct sockaddr_un *ipc_user_sockaddr(void); | ||
10 | |||
11 | void ipc_event_workspace(swayc_t *old, swayc_t *new); | ||
12 | |||
13 | #endif | ||
diff --git a/include/ipc.h b/include/ipc.h index 02aa1c1e..75be58a6 100644 --- a/include/ipc.h +++ b/include/ipc.h | |||
@@ -1,8 +1,6 @@ | |||
1 | #ifndef _SWAY_IPC_H | 1 | #ifndef _SWAY_IPC_H |
2 | #define _SWAY_IPC_H | 2 | #define _SWAY_IPC_H |
3 | 3 | ||
4 | #include "container.h" | ||
5 | |||
6 | enum ipc_command_type { | 4 | enum ipc_command_type { |
7 | IPC_COMMAND = 0, | 5 | IPC_COMMAND = 0, |
8 | IPC_GET_WORKSPACES = 1, | 6 | IPC_GET_WORKSPACES = 1, |
@@ -15,10 +13,4 @@ enum ipc_command_type { | |||
15 | IPC_SWAY_GET_PIXELS = 0x81 | 13 | IPC_SWAY_GET_PIXELS = 0x81 |
16 | }; | 14 | }; |
17 | 15 | ||
18 | void ipc_init(void); | ||
19 | void ipc_terminate(void); | ||
20 | struct sockaddr_un *ipc_user_sockaddr(void); | ||
21 | |||
22 | void ipc_event_workspace(swayc_t *old, swayc_t *new); | ||
23 | |||
24 | #endif | 16 | #endif |
diff --git a/sway/focus.c b/sway/focus.c index 3800a46c..7af858a1 100644 --- a/sway/focus.c +++ b/sway/focus.c | |||
@@ -6,7 +6,7 @@ | |||
6 | #include "layout.h" | 6 | #include "layout.h" |
7 | #include "config.h" | 7 | #include "config.h" |
8 | #include "input_state.h" | 8 | #include "input_state.h" |
9 | #include "ipc.h" | 9 | #include "ipc-server.h" |
10 | 10 | ||
11 | bool locked_container_focus = false; | 11 | bool locked_container_focus = false; |
12 | bool locked_view_focus = false; | 12 | bool locked_view_focus = false; |
diff --git a/sway/ipc.c b/sway/ipc-server.c index e004aff1..7c737307 100644 --- a/sway/ipc.c +++ b/sway/ipc-server.c | |||
@@ -13,7 +13,7 @@ | |||
13 | #include <ctype.h> | 13 | #include <ctype.h> |
14 | #include <json-c/json.h> | 14 | #include <json-c/json.h> |
15 | #include <list.h> | 15 | #include <list.h> |
16 | #include "ipc.h" | 16 | #include "ipc-server.h" |
17 | #include "log.h" | 17 | #include "log.h" |
18 | #include "config.h" | 18 | #include "config.h" |
19 | #include "commands.h" | 19 | #include "commands.h" |
diff --git a/sway/main.c b/sway/main.c index ebb45930..19648782 100644 --- a/sway/main.c +++ b/sway/main.c | |||
@@ -14,7 +14,7 @@ | |||
14 | #include "log.h" | 14 | #include "log.h" |
15 | #include "readline.h" | 15 | #include "readline.h" |
16 | #include "handlers.h" | 16 | #include "handlers.h" |
17 | #include "ipc.h" | 17 | #include "ipc-server.h" |
18 | #include "sway.h" | 18 | #include "sway.h" |
19 | 19 | ||
20 | static bool terminate_request = false; | 20 | static bool terminate_request = false; |
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 | } |