diff options
-rw-r--r-- | completions/zsh/_swaymsg | 1 | ||||
-rw-r--r-- | include/ipc.h | 1 | ||||
-rw-r--r-- | include/sway/config.h | 3 | ||||
-rw-r--r-- | include/util.h | 5 | ||||
-rw-r--r-- | sway/CMakeLists.txt | 3 | ||||
-rw-r--r-- | sway/base64.c | 221 | ||||
-rw-r--r-- | sway/commands/ipc.c | 1 | ||||
-rw-r--r-- | sway/ipc-server.c | 284 | ||||
-rw-r--r-- | swaymsg/main.c | 57 | ||||
-rw-r--r-- | swaymsg/swaymsg.1.txt | 6 |
10 files changed, 574 insertions, 8 deletions
diff --git a/completions/zsh/_swaymsg b/completions/zsh/_swaymsg index 4207ce3b..1c3ccd65 100644 --- a/completions/zsh/_swaymsg +++ b/completions/zsh/_swaymsg | |||
@@ -21,6 +21,7 @@ types=( | |||
21 | 'get_marks' | 21 | 'get_marks' |
22 | 'get_bar_config' | 22 | 'get_bar_config' |
23 | 'get_version' | 23 | 'get_version' |
24 | 'get_clipboard' | ||
24 | ) | 25 | ) |
25 | 26 | ||
26 | _arguments -s \ | 27 | _arguments -s \ |
diff --git a/include/ipc.h b/include/ipc.h index 98390335..2b16dc50 100644 --- a/include/ipc.h +++ b/include/ipc.h | |||
@@ -13,6 +13,7 @@ enum ipc_command_type { | |||
13 | IPC_GET_BAR_CONFIG = 6, | 13 | IPC_GET_BAR_CONFIG = 6, |
14 | IPC_GET_VERSION = 7, | 14 | IPC_GET_VERSION = 7, |
15 | IPC_GET_INPUTS = 8, | 15 | IPC_GET_INPUTS = 8, |
16 | IPC_GET_CLIPBOARD = 9, | ||
16 | // Events send from sway to clients. Events have the highest bits set. | 17 | // Events send from sway to clients. Events have the highest bits set. |
17 | IPC_EVENT_WORKSPACE = ((1<<31) | 0), | 18 | IPC_EVENT_WORKSPACE = ((1<<31) | 0), |
18 | IPC_EVENT_OUTPUT = ((1<<31) | 1), | 19 | IPC_EVENT_OUTPUT = ((1<<31) | 1), |
diff --git a/include/sway/config.h b/include/sway/config.h index 999a471a..a05d5ede 100644 --- a/include/sway/config.h +++ b/include/sway/config.h | |||
@@ -235,8 +235,9 @@ enum ipc_feature { | |||
235 | IPC_FEATURE_EVENT_WINDOW = 2048, | 235 | IPC_FEATURE_EVENT_WINDOW = 2048, |
236 | IPC_FEATURE_EVENT_BINDING = 4096, | 236 | IPC_FEATURE_EVENT_BINDING = 4096, |
237 | IPC_FEATURE_EVENT_INPUT = 8192, | 237 | IPC_FEATURE_EVENT_INPUT = 8192, |
238 | IPC_FEATURE_GET_CLIPBOARD = 16384, | ||
238 | 239 | ||
239 | IPC_FEATURE_ALL_COMMANDS = 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, | 240 | IPC_FEATURE_ALL_COMMANDS = 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 16384, |
240 | IPC_FEATURE_ALL_EVENTS = 256 | 512 | 1024 | 2048 | 4096 | 8192, | 241 | IPC_FEATURE_ALL_EVENTS = 256 | 512 | 1024 | 2048 | 4096 | 8192, |
241 | 242 | ||
242 | IPC_FEATURE_ALL = IPC_FEATURE_ALL_COMMANDS | IPC_FEATURE_ALL_EVENTS, | 243 | IPC_FEATURE_ALL = IPC_FEATURE_ALL_COMMANDS | IPC_FEATURE_ALL_EVENTS, |
diff --git a/include/util.h b/include/util.h index e5365458..f68deae8 100644 --- a/include/util.h +++ b/include/util.h | |||
@@ -3,6 +3,7 @@ | |||
3 | 3 | ||
4 | #include <stdint.h> | 4 | #include <stdint.h> |
5 | #include <unistd.h> | 5 | #include <unistd.h> |
6 | #include <sys/types.h> | ||
6 | #include <xkbcommon/xkbcommon.h> | 7 | #include <xkbcommon/xkbcommon.h> |
7 | 8 | ||
8 | /** | 9 | /** |
@@ -57,4 +58,8 @@ uint32_t parse_color(const char *color); | |||
57 | * to a dangling symlink, NULL is returned. | 58 | * to a dangling symlink, NULL is returned. |
58 | */ | 59 | */ |
59 | char* resolve_path(const char* path); | 60 | char* resolve_path(const char* path); |
61 | |||
62 | char *b64_encode(const char* binaryData, size_t len, size_t *flen); | ||
63 | unsigned char *b64_decode(const char *ascii, size_t len, size_t *flen); | ||
64 | |||
60 | #endif | 65 | #endif |
diff --git a/sway/CMakeLists.txt b/sway/CMakeLists.txt index 61c22b84..48f7a7a8 100644 --- a/sway/CMakeLists.txt +++ b/sway/CMakeLists.txt | |||
@@ -19,6 +19,7 @@ file(GLOB cmds | |||
19 | add_executable(sway | 19 | add_executable(sway |
20 | commands.c | 20 | commands.c |
21 | ${cmds} | 21 | ${cmds} |
22 | base64.c | ||
22 | config.c | 23 | config.c |
23 | container.c | 24 | container.c |
24 | criteria.c | 25 | criteria.c |
@@ -35,7 +36,7 @@ add_executable(sway | |||
35 | output.c | 36 | output.c |
36 | workspace.c | 37 | workspace.c |
37 | border.c | 38 | border.c |
38 | security.c | 39 | security.c |
39 | ) | 40 | ) |
40 | 41 | ||
41 | add_definitions( | 42 | add_definitions( |
diff --git a/sway/base64.c b/sway/base64.c new file mode 100644 index 00000000..0d51b5c7 --- /dev/null +++ b/sway/base64.c | |||
@@ -0,0 +1,221 @@ | |||
1 | /* | ||
2 | * Adapted from https://github.com/littlstar/b64.c | ||
3 | * License under the MIT License: | ||
4 | * Copyright (c) 2014 Little Star Media, Inc. | ||
5 | * | ||
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
7 | * of this software and associated documentation files (the "Software"), to deal | ||
8 | * in the Software without restriction, including without limitation the rights | ||
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
10 | * copies of the Software, and to permit persons to whom the Software is | ||
11 | * furnished to do so, subject to the following conditions: | ||
12 | * | ||
13 | * The above copyright notice and this permission notice shall be included in all | ||
14 | * copies or substantial portions of the Software. | ||
15 | * | ||
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
22 | * SOFTWARE. | ||
23 | */ | ||
24 | |||
25 | #include <ctype.h> | ||
26 | #include <stdlib.h> | ||
27 | #include "util.h" | ||
28 | |||
29 | static const char b64_table[] = { | ||
30 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', | ||
31 | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', | ||
32 | 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', | ||
33 | 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', | ||
34 | 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', | ||
35 | 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', | ||
36 | 'w', 'x', 'y', 'z', '0', '1', '2', '3', | ||
37 | '4', '5', '6', '7', '8', '9', '+', '/' | ||
38 | }; | ||
39 | |||
40 | char *b64_encode(const char *src, size_t len, size_t *flen) { | ||
41 | int i = 0; | ||
42 | int j = 0; | ||
43 | char *enc = NULL; | ||
44 | size_t size = len * 4 / 3; | ||
45 | size_t idx = 0; | ||
46 | unsigned char buf[4]; | ||
47 | char tmp[3]; | ||
48 | |||
49 | // alloc | ||
50 | enc = (char *) malloc(size + 1); | ||
51 | if (NULL == enc) { return NULL; } | ||
52 | |||
53 | // parse until end of source | ||
54 | while (len--) { | ||
55 | // read up to 3 bytes at a time into `tmp' | ||
56 | tmp[i++] = *(src++); | ||
57 | |||
58 | // if 3 bytes read then encode into `buf' | ||
59 | if (3 == i) { | ||
60 | buf[0] = (tmp[0] & 0xfc) >> 2; | ||
61 | buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4); | ||
62 | buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6); | ||
63 | buf[3] = tmp[2] & 0x3f; | ||
64 | |||
65 | // shouldn't really happen | ||
66 | if (idx + 4 > size) { | ||
67 | size += 16; | ||
68 | enc = (char *) realloc(enc, size + 1); | ||
69 | } | ||
70 | for (i = 0; i < 4; ++i) { | ||
71 | enc[idx++] = b64_table[buf[i]]; | ||
72 | } | ||
73 | |||
74 | // reset index | ||
75 | i = 0; | ||
76 | } | ||
77 | } | ||
78 | |||
79 | // remainder | ||
80 | if (i > 0) { | ||
81 | // fill `tmp' with `\0' at most 3 times | ||
82 | for (j = i; j < 3; ++j) { | ||
83 | tmp[j] = '\0'; | ||
84 | } | ||
85 | |||
86 | // perform same codec as above | ||
87 | buf[0] = (tmp[0] & 0xfc) >> 2; | ||
88 | buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4); | ||
89 | buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6); | ||
90 | buf[3] = tmp[2] & 0x3f; | ||
91 | |||
92 | // perform same write to `enc` with new allocation | ||
93 | size_t delta = (i > 3 ? 0 : 3 - i) + (j > i + 1 ? 0 : i + 1 - j); | ||
94 | if (idx + delta > size) { | ||
95 | size += delta; | ||
96 | enc = (char *) realloc(enc, size + 1); | ||
97 | } | ||
98 | for (j = 0; (j < i + 1); ++j) { | ||
99 | enc[idx++] = b64_table[buf[j]]; | ||
100 | } | ||
101 | |||
102 | // while there is still a remainder | ||
103 | // append `=' to `enc' | ||
104 | while ((i++ < 3)) { | ||
105 | enc[idx++] = '='; | ||
106 | } | ||
107 | } | ||
108 | |||
109 | enc[idx] = '\0'; | ||
110 | |||
111 | if (flen) | ||
112 | *flen = size; | ||
113 | return enc; | ||
114 | } | ||
115 | |||
116 | unsigned char *b64_decode(const char *src, size_t len, size_t *decsize) { | ||
117 | int i = 0; | ||
118 | int j = 0; | ||
119 | int l = 0; | ||
120 | // max size estimate | ||
121 | size_t size = len * 3 / 4; | ||
122 | size_t idx = 0; | ||
123 | unsigned char *dec = NULL; | ||
124 | unsigned char buf[3]; | ||
125 | unsigned char tmp[4]; | ||
126 | |||
127 | // alloc | ||
128 | dec = (unsigned char *) malloc(size + 1); | ||
129 | if (NULL == dec) { return NULL; } | ||
130 | |||
131 | // parse until end of source | ||
132 | while (len--) { | ||
133 | if (isspace(src[j])) { j++; continue; } | ||
134 | // break if char is `=' or not base64 char | ||
135 | if ('=' == src[j]) { break; } | ||
136 | if (!(isalnum(src[j]) || '+' == src[j] || '/' == src[j])) { break; } | ||
137 | |||
138 | // read up to 4 bytes at a time into `tmp' | ||
139 | tmp[i++] = src[j++]; | ||
140 | |||
141 | // if 4 bytes read then decode into `buf' | ||
142 | if (4 == i) { | ||
143 | // translate values in `tmp' from table | ||
144 | for (i = 0; i < 4; ++i) { | ||
145 | // find translation char in `b64_table' | ||
146 | for (l = 0; l < 64; ++l) { | ||
147 | if (tmp[i] == b64_table[l]) { | ||
148 | tmp[i] = l; | ||
149 | break; | ||
150 | } | ||
151 | } | ||
152 | } | ||
153 | |||
154 | // decode | ||
155 | buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4); | ||
156 | buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2); | ||
157 | buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3]; | ||
158 | |||
159 | // unlikely | ||
160 | if (idx + 3 > size) { | ||
161 | size += 16; | ||
162 | dec = (unsigned char *) realloc(dec, size + 1); | ||
163 | } | ||
164 | if (dec != NULL){ | ||
165 | for (i = 0; i < 3; ++i) { | ||
166 | dec[idx++] = buf[i]; | ||
167 | } | ||
168 | } else { | ||
169 | return NULL; | ||
170 | } | ||
171 | |||
172 | // reset | ||
173 | i = 0; | ||
174 | } | ||
175 | } | ||
176 | |||
177 | // remainder | ||
178 | if (i > 0) { | ||
179 | // fill `tmp' with `\0' at most 4 times | ||
180 | for (j = i; j < 4; ++j) { | ||
181 | tmp[j] = '\0'; | ||
182 | } | ||
183 | |||
184 | // translate remainder | ||
185 | for (j = 0; j < 4; ++j) { | ||
186 | // find translation char in `b64_table' | ||
187 | for (l = 0; l < 64; ++l) { | ||
188 | if (tmp[j] == b64_table[l]) { | ||
189 | tmp[j] = l; | ||
190 | break; | ||
191 | } | ||
192 | } | ||
193 | } | ||
194 | |||
195 | // decode remainder | ||
196 | buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4); | ||
197 | buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2); | ||
198 | buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3]; | ||
199 | |||
200 | // write remainer decoded buffer to `dec' | ||
201 | if (idx + (i - 1) > size) { | ||
202 | size += 16; | ||
203 | dec = (unsigned char *) realloc(dec, size + 1); | ||
204 | } | ||
205 | if (dec != NULL){ | ||
206 | for (j = 0; (j < i - 1); ++j) { | ||
207 | dec[idx++] = buf[j]; | ||
208 | } | ||
209 | } else { | ||
210 | return NULL; | ||
211 | } | ||
212 | } | ||
213 | |||
214 | dec[idx] = '\0'; | ||
215 | // Return back the size of decoded string if demanded. | ||
216 | if (decsize != NULL) { | ||
217 | *decsize = size; | ||
218 | } | ||
219 | |||
220 | return dec; | ||
221 | } | ||
diff --git a/sway/commands/ipc.c b/sway/commands/ipc.c index f0b3035a..0c678961 100644 --- a/sway/commands/ipc.c +++ b/sway/commands/ipc.c | |||
@@ -89,6 +89,7 @@ struct cmd_results *cmd_ipc_cmd(int argc, char **argv) { | |||
89 | { "marks", IPC_FEATURE_GET_MARKS }, | 89 | { "marks", IPC_FEATURE_GET_MARKS }, |
90 | { "bar-config", IPC_FEATURE_GET_BAR_CONFIG }, | 90 | { "bar-config", IPC_FEATURE_GET_BAR_CONFIG }, |
91 | { "inputs", IPC_FEATURE_GET_INPUTS }, | 91 | { "inputs", IPC_FEATURE_GET_INPUTS }, |
92 | { "clipboard", IPC_FEATURE_GET_CLIPBOARD }, | ||
92 | }; | 93 | }; |
93 | 94 | ||
94 | uint32_t type = 0; | 95 | uint32_t type = 0; |
diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 9122d548..4ce2b7eb 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c | |||
@@ -1,5 +1,6 @@ | |||
1 | // See https://i3wm.org/docs/ipc.html for protocol information | 1 | // See https://i3wm.org/docs/ipc.html for protocol information |
2 | 2 | ||
3 | #define _XOPEN_SOURCE 700 | ||
3 | #include <errno.h> | 4 | #include <errno.h> |
4 | #include <string.h> | 5 | #include <string.h> |
5 | #include <sys/socket.h> | 6 | #include <sys/socket.h> |
@@ -59,6 +60,19 @@ struct get_pixels_request { | |||
59 | struct wlc_geometry geo; | 60 | struct wlc_geometry geo; |
60 | }; | 61 | }; |
61 | 62 | ||
63 | struct get_clipboard_request { | ||
64 | struct ipc_client *client; | ||
65 | json_object *json; | ||
66 | int fd; | ||
67 | struct wlc_event_source *fd_event_source; | ||
68 | struct wlc_event_source *timer_event_source; | ||
69 | char *type; | ||
70 | unsigned int *pending; | ||
71 | char *buf; | ||
72 | size_t buf_size; | ||
73 | size_t buf_position; | ||
74 | }; | ||
75 | |||
62 | struct sockaddr_un *ipc_user_sockaddr(void); | 76 | struct sockaddr_un *ipc_user_sockaddr(void); |
63 | int ipc_handle_connection(int fd, uint32_t mask, void *data); | 77 | int ipc_handle_connection(int fd, uint32_t mask, void *data); |
64 | int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data); | 78 | int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data); |
@@ -394,6 +408,266 @@ void ipc_get_pixels(wlc_handle output) { | |||
394 | ipc_get_pixel_requests = unhandled; | 408 | ipc_get_pixel_requests = unhandled; |
395 | } | 409 | } |
396 | 410 | ||
411 | static bool is_text_target(const char *target) { | ||
412 | return (strncmp(target, "text/", 5) == 0 | ||
413 | || strcmp(target, "UTF8_STRING") == 0 | ||
414 | || strcmp(target, "STRING") == 0 | ||
415 | || strcmp(target, "TEXT") == 0 | ||
416 | || strcmp(target, "COMPOUND_TEXT") == 0); | ||
417 | } | ||
418 | |||
419 | static void release_clipboard_request(struct get_clipboard_request *req) { | ||
420 | if (--(*req->pending) == 0) { | ||
421 | const char *str = json_object_to_json_string(req->json); | ||
422 | ipc_send_reply(req->client, str, (uint32_t)strlen(str)); | ||
423 | json_object_put(req->json); | ||
424 | } | ||
425 | |||
426 | free(req->type); | ||
427 | free(req->buf); | ||
428 | wlc_event_source_remove(req->fd_event_source); | ||
429 | wlc_event_source_remove(req->timer_event_source); | ||
430 | close(req->fd); | ||
431 | free(req); | ||
432 | } | ||
433 | |||
434 | static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) { | ||
435 | assert(data); | ||
436 | struct get_clipboard_request *req = (struct get_clipboard_request *)data; | ||
437 | |||
438 | if (mask & WLC_EVENT_ERROR) { | ||
439 | sway_log(L_ERROR, "Selection data fd error"); | ||
440 | goto error; | ||
441 | } | ||
442 | |||
443 | if (mask & WLC_EVENT_READABLE) { | ||
444 | static const unsigned int max_size = 8192 * 1024; | ||
445 | int amt = 0; | ||
446 | |||
447 | do { | ||
448 | int size = req->buf_size - req->buf_position; | ||
449 | int amt = read(fd, req->buf + req->buf_position, size - 1); | ||
450 | if (amt < 0) { | ||
451 | if (errno == EAGAIN) { | ||
452 | return 0; | ||
453 | } | ||
454 | |||
455 | sway_log_errno(L_INFO, "Failed to read from clipboard data fd"); | ||
456 | goto release; | ||
457 | } | ||
458 | |||
459 | req->buf_position += amt; | ||
460 | if (req->buf_position >= req->buf_size - 1) { | ||
461 | if (req->buf_size >= max_size) { | ||
462 | sway_log(L_ERROR, "get_clipbard: selection data too large"); | ||
463 | goto error; | ||
464 | } | ||
465 | char *next = realloc(req->buf, req->buf_size *= 2); | ||
466 | if (!next) { | ||
467 | sway_log_errno(L_ERROR, "get_clipboard: realloc data buffer failed"); | ||
468 | goto error; | ||
469 | } | ||
470 | |||
471 | req->buf = next; | ||
472 | } | ||
473 | } while(amt != 0); | ||
474 | |||
475 | req->buf[req->buf_position] = '\0'; | ||
476 | |||
477 | json_object *obj = json_object_new_object(); | ||
478 | json_object_object_add(obj, "success", json_object_new_boolean(true)); | ||
479 | if (is_text_target(req->type)) { | ||
480 | json_object_object_add(obj, "content", json_object_new_string(req->buf)); | ||
481 | json_object_object_add(req->json, req->type, obj); | ||
482 | } else { | ||
483 | size_t outlen; | ||
484 | char *b64 = b64_encode(req->buf, req->buf_position, &outlen); | ||
485 | json_object_object_add(obj, "content", json_object_new_string(b64)); | ||
486 | free(b64); | ||
487 | |||
488 | char *type = malloc(strlen(req->type) + 8); | ||
489 | strcat(type, ";base64"); | ||
490 | json_object_object_add(req->json, type, obj); | ||
491 | free(type); | ||
492 | } | ||
493 | } | ||
494 | |||
495 | goto release; | ||
496 | |||
497 | error:; | ||
498 | json_object *obj = json_object_new_object(); | ||
499 | json_object_object_add(obj, "success", json_object_new_boolean(false)); | ||
500 | json_object_object_add(obj, "error", | ||
501 | json_object_new_string("Failed to retrieve data")); | ||
502 | json_object_object_add(req->json, req->type, obj); | ||
503 | |||
504 | release: | ||
505 | release_clipboard_request(req); | ||
506 | return 0; | ||
507 | } | ||
508 | |||
509 | static int ipc_selection_timer_cb(void *data) { | ||
510 | assert(data); | ||
511 | struct get_clipboard_request *req = (struct get_clipboard_request *)data; | ||
512 | |||
513 | sway_log(L_INFO, "get_clipbard: timeout for type %s", req->type); | ||
514 | json_object *obj = json_object_new_object(); | ||
515 | json_object_object_add(obj, "success", json_object_new_boolean(false)); | ||
516 | json_object_object_add(obj, "error", json_object_new_string("Timeout")); | ||
517 | json_object_object_add(req->json, req->type, obj); | ||
518 | |||
519 | release_clipboard_request(req); | ||
520 | return 0; | ||
521 | } | ||
522 | |||
523 | // greedy wildcard (only "*") matching | ||
524 | bool mime_type_matches(const char *mime_type, const char *pattern) { | ||
525 | const char *wildcard = NULL; | ||
526 | while (*mime_type && *pattern) { | ||
527 | if (*pattern == '*' && !wildcard) { | ||
528 | wildcard = pattern; | ||
529 | ++pattern; | ||
530 | } | ||
531 | |||
532 | if (*mime_type != *pattern) { | ||
533 | if (!wildcard) | ||
534 | return false; | ||
535 | |||
536 | pattern = wildcard; | ||
537 | ++mime_type; | ||
538 | continue; | ||
539 | } | ||
540 | |||
541 | ++mime_type; | ||
542 | ++pattern; | ||
543 | } | ||
544 | |||
545 | while (*pattern == '*') { | ||
546 | ++pattern; | ||
547 | } | ||
548 | |||
549 | return (*mime_type == *pattern); | ||
550 | } | ||
551 | |||
552 | void ipc_get_clipboard(struct ipc_client *client, char *buf) { | ||
553 | size_t size; | ||
554 | const char **types = wlc_get_selection_types(&size); | ||
555 | if (client->payload_length == 0) { | ||
556 | json_object *obj = json_object_new_array(); | ||
557 | for (size_t i = 0; i < size; ++i) { | ||
558 | json_object_array_add(obj, json_object_new_string(types[i])); | ||
559 | } | ||
560 | |||
561 | const char *str = json_object_to_json_string(obj); | ||
562 | ipc_send_reply(client, str, strlen(str)); | ||
563 | json_object_put(obj); | ||
564 | return; | ||
565 | } | ||
566 | |||
567 | unescape_string(buf); | ||
568 | strip_quotes(buf); | ||
569 | list_t *requested = split_string(buf, " "); | ||
570 | json_object *json = json_object_new_object(); | ||
571 | unsigned int *pending = malloc(sizeof(unsigned int)); | ||
572 | *pending = 0; | ||
573 | |||
574 | for (size_t l = 0; l < (size_t) requested->length; ++l) { | ||
575 | const char *pattern = requested->items[l]; | ||
576 | bool found = false; | ||
577 | for (size_t i = 0; i < size; ++i) { | ||
578 | if (!mime_type_matches(types[i], pattern)) { | ||
579 | continue; | ||
580 | } | ||
581 | |||
582 | found = true; | ||
583 | |||
584 | struct get_clipboard_request *req = malloc(sizeof(*req)); | ||
585 | if (!req) { | ||
586 | sway_log(L_ERROR, "get_clipboard: request malloc failed"); | ||
587 | goto data_error; | ||
588 | } | ||
589 | |||
590 | int pipes[2]; | ||
591 | if (pipe(pipes) == -1) { | ||
592 | sway_log_errno(L_ERROR, "get_clipboard: pipe call failed"); | ||
593 | free(req); | ||
594 | goto data_error; | ||
595 | } | ||
596 | |||
597 | fcntl(pipes[0], F_SETFD, FD_CLOEXEC | O_NONBLOCK); | ||
598 | fcntl(pipes[1], F_SETFD, FD_CLOEXEC | O_NONBLOCK); | ||
599 | |||
600 | if (!wlc_get_selection_data(types[i], pipes[1])) { | ||
601 | close(pipes[0]); | ||
602 | close(pipes[1]); | ||
603 | free(req); | ||
604 | sway_log(L_ERROR, "get_clipboard: failed to retrieve " | ||
605 | "selection data"); | ||
606 | goto data_error; | ||
607 | } | ||
608 | |||
609 | if (!(req->buf = malloc(512))) { | ||
610 | close(pipes[0]); | ||
611 | close(pipes[1]); | ||
612 | free(req); | ||
613 | sway_log_errno(L_ERROR, "get_clipboard: buf malloc failed"); | ||
614 | goto data_error; | ||
615 | } | ||
616 | |||
617 | (*pending)++; | ||
618 | |||
619 | req->client = client; | ||
620 | req->type = strdup(types[i]); | ||
621 | req->json = json; | ||
622 | req->pending = pending; | ||
623 | req->buf_position = 0; | ||
624 | req->buf_size = 512; | ||
625 | req->fd = pipes[0]; | ||
626 | req->timer_event_source = wlc_event_loop_add_timer(ipc_selection_timer_cb, req); | ||
627 | req->fd_event_source = wlc_event_loop_add_fd(pipes[0], | ||
628 | WLC_EVENT_READABLE | WLC_EVENT_ERROR | WLC_EVENT_HANGUP, | ||
629 | &ipc_selection_data_cb, req); | ||
630 | |||
631 | wlc_event_source_timer_update(req->timer_event_source, 30000); | ||
632 | |||
633 | // NOTE: remove this goto to enable retrieving multiple | ||
634 | // targets at once. The whole implementation is already | ||
635 | // made for it. The only reason it was disabled | ||
636 | // at the time of writing is that neither wlc's xselection | ||
637 | // implementation nor (apparently) gtk on wayland supports | ||
638 | // multiple send requests at the same time which makes | ||
639 | // every request except the last one fail (and therefore | ||
640 | // return empty data) | ||
641 | goto cleanup; | ||
642 | } | ||
643 | |||
644 | if (!found) { | ||
645 | sway_log(L_INFO, "Invalid clipboard type %s requested", pattern); | ||
646 | } | ||
647 | } | ||
648 | |||
649 | if (*pending == 0) { | ||
650 | static const char *error_empty = "{ \"success\": false, \"error\": " | ||
651 | "\"No matching types found\" }"; | ||
652 | ipc_send_reply(client, error_empty, (uint32_t)strlen(error_empty)); | ||
653 | free(json); | ||
654 | free(pending); | ||
655 | } | ||
656 | |||
657 | goto cleanup; | ||
658 | |||
659 | data_error:; | ||
660 | static const char *error_json = "{ \"success\": false, \"error\": " | ||
661 | "\"Failed to create clipboard data request\" }"; | ||
662 | ipc_send_reply(client, error_json, (uint32_t)strlen(error_json)); | ||
663 | free(json); | ||
664 | free(pending); | ||
665 | |||
666 | cleanup: | ||
667 | list_free(requested); | ||
668 | free(types); | ||
669 | } | ||
670 | |||
397 | void ipc_client_handle_command(struct ipc_client *client) { | 671 | void ipc_client_handle_command(struct ipc_client *client) { |
398 | if (!sway_assert(client != NULL, "client != NULL")) { | 672 | if (!sway_assert(client != NULL, "client != NULL")) { |
399 | return; | 673 | return; |
@@ -638,6 +912,16 @@ void ipc_client_handle_command(struct ipc_client *client) { | |||
638 | goto exit_cleanup; | 912 | goto exit_cleanup; |
639 | } | 913 | } |
640 | 914 | ||
915 | case IPC_GET_CLIPBOARD: | ||
916 | { | ||
917 | if (!(client->security_policy & IPC_FEATURE_GET_CLIPBOARD)) { | ||
918 | goto exit_denied; | ||
919 | } | ||
920 | |||
921 | ipc_get_clipboard(client, buf); | ||
922 | goto exit_cleanup; | ||
923 | } | ||
924 | |||
641 | default: | 925 | default: |
642 | sway_log(L_INFO, "Unknown IPC command type %i", client->current_command); | 926 | sway_log(L_INFO, "Unknown IPC command type %i", client->current_command); |
643 | goto exit_cleanup; | 927 | goto exit_cleanup; |
diff --git a/swaymsg/main.c b/swaymsg/main.c index efd0ec25..2f9cfb14 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c | |||
@@ -19,15 +19,17 @@ void sway_terminate(int exit_code) { | |||
19 | exit(exit_code); | 19 | exit(exit_code); |
20 | } | 20 | } |
21 | 21 | ||
22 | static void pretty_print_cmd(json_object *r) { | 22 | static bool success(json_object *r, bool fallback) { |
23 | bool _success; | ||
24 | json_object *success; | 23 | json_object *success; |
25 | if (!json_object_object_get_ex(r, "success", &success)) { | 24 | if (!json_object_object_get_ex(r, "success", &success)) { |
26 | _success = true; | 25 | return fallback; |
27 | } else { | 26 | } else { |
28 | _success = json_object_get_boolean(success); | 27 | return json_object_get_boolean(success); |
29 | } | 28 | } |
30 | if (!_success) { | 29 | } |
30 | |||
31 | static void pretty_print_cmd(json_object *r) { | ||
32 | if (!success(r, true)) { | ||
31 | json_object *error; | 33 | json_object *error; |
32 | if (!json_object_object_get_ex(r, "error", &error)) { | 34 | if (!json_object_object_get_ex(r, "error", &error)) { |
33 | printf("An unknkown error occured"); | 35 | printf("An unknkown error occured"); |
@@ -144,10 +146,43 @@ static void pretty_print_version(json_object *v) { | |||
144 | printf("sway version %s\n", json_object_get_string(ver)); | 146 | printf("sway version %s\n", json_object_get_string(ver)); |
145 | } | 147 | } |
146 | 148 | ||
149 | static void pretty_print_clipboard(json_object *v) { | ||
150 | if (success(v, true)) { | ||
151 | if (json_object_is_type(v, json_type_array)) { | ||
152 | for (int i = 0; i < json_object_array_length(v); ++i) { | ||
153 | json_object *o = json_object_array_get_idx(v, i); | ||
154 | printf("%s\n", json_object_get_string(o)); | ||
155 | } | ||
156 | } else { | ||
157 | // NOTE: could be extended to print all received types | ||
158 | // instead just the first one when sways ipc server | ||
159 | // supports it | ||
160 | struct json_object_iterator iter = json_object_iter_begin(v); | ||
161 | struct json_object_iterator end = json_object_iter_end(v); | ||
162 | if (!json_object_iter_equal(&iter, &end)) { | ||
163 | json_object *obj = json_object_iter_peek_value(&iter); | ||
164 | if (success(obj, false)) { | ||
165 | json_object *content; | ||
166 | json_object_object_get_ex(obj, "content", &content); | ||
167 | printf("%s\n", json_object_get_string(content)); | ||
168 | } else { | ||
169 | json_object *error; | ||
170 | json_object_object_get_ex(obj, "error", &error); | ||
171 | printf("Error: %s\n", json_object_get_string(error)); | ||
172 | } | ||
173 | } | ||
174 | } | ||
175 | } else { | ||
176 | json_object *error; | ||
177 | json_object_object_get_ex(v, "error", &error); | ||
178 | printf("Error: %s\n", json_object_get_string(error)); | ||
179 | } | ||
180 | } | ||
181 | |||
147 | static void pretty_print(int type, json_object *resp) { | 182 | static void pretty_print(int type, json_object *resp) { |
148 | if (type != IPC_COMMAND && type != IPC_GET_WORKSPACES && | 183 | if (type != IPC_COMMAND && type != IPC_GET_WORKSPACES && |
149 | type != IPC_GET_INPUTS && type != IPC_GET_OUTPUTS && | 184 | type != IPC_GET_INPUTS && type != IPC_GET_OUTPUTS && |
150 | type != IPC_GET_VERSION) { | 185 | type != IPC_GET_VERSION && type != IPC_GET_CLIPBOARD) { |
151 | printf("%s\n", json_object_to_json_string_ext(resp, | 186 | printf("%s\n", json_object_to_json_string_ext(resp, |
152 | JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); | 187 | JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); |
153 | return; | 188 | return; |
@@ -158,6 +193,11 @@ static void pretty_print(int type, json_object *resp) { | |||
158 | return; | 193 | return; |
159 | } | 194 | } |
160 | 195 | ||
196 | if (type == IPC_GET_CLIPBOARD) { | ||
197 | pretty_print_clipboard(resp); | ||
198 | return; | ||
199 | } | ||
200 | |||
161 | json_object *obj; | 201 | json_object *obj; |
162 | size_t len = json_object_array_length(resp); | 202 | size_t len = json_object_array_length(resp); |
163 | for (size_t i = 0; i < len; ++i) { | 203 | for (size_t i = 0; i < len; ++i) { |
@@ -267,6 +307,8 @@ int main(int argc, char **argv) { | |||
267 | type = IPC_GET_BAR_CONFIG; | 307 | type = IPC_GET_BAR_CONFIG; |
268 | } else if (strcasecmp(cmdtype, "get_version") == 0) { | 308 | } else if (strcasecmp(cmdtype, "get_version") == 0) { |
269 | type = IPC_GET_VERSION; | 309 | type = IPC_GET_VERSION; |
310 | } else if (strcasecmp(cmdtype, "get_clipboard") == 0) { | ||
311 | type = IPC_GET_CLIPBOARD; | ||
270 | } else { | 312 | } else { |
271 | sway_abort("Unknown message type %s", cmdtype); | 313 | sway_abort("Unknown message type %s", cmdtype); |
272 | } | 314 | } |
@@ -290,6 +332,9 @@ int main(int argc, char **argv) { | |||
290 | printf("%s\n", resp); | 332 | printf("%s\n", resp); |
291 | ret = 1; | 333 | ret = 1; |
292 | } else { | 334 | } else { |
335 | if (!success(obj, true)) { | ||
336 | ret = 1; | ||
337 | } | ||
293 | if (raw) { | 338 | if (raw) { |
294 | printf("%s\n", json_object_to_json_string_ext(obj, | 339 | printf("%s\n", json_object_to_json_string_ext(obj, |
295 | JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); | 340 | JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); |
diff --git a/swaymsg/swaymsg.1.txt b/swaymsg/swaymsg.1.txt index 1466a0fa..27813588 100644 --- a/swaymsg/swaymsg.1.txt +++ b/swaymsg/swaymsg.1.txt | |||
@@ -65,6 +65,12 @@ IPC Message Types | |||
65 | *get_version*:: | 65 | *get_version*:: |
66 | Get JSON-encoded version information for the running instance of sway. | 66 | Get JSON-encoded version information for the running instance of sway. |
67 | 67 | ||
68 | *get_clipboard*:: | ||
69 | Get JSON-encoded information about the clipboard. | ||
70 | Returns the current clipboard mime-types if called without | ||
71 | arguments, otherwise returns the clipboard data in the requested | ||
72 | formats. Encodes the data using base64 for non-text mime types. | ||
73 | |||
68 | Authors | 74 | Authors |
69 | ------- | 75 | ------- |
70 | 76 | ||