summaryrefslogtreecommitdiffstats
path: root/sway
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2017-10-08 11:05:54 -0400
committerLibravatar GitHub <noreply@github.com>2017-10-08 11:05:54 -0400
commit6d83a59b461d07c4c9c5ee5809da658f5c56afc3 (patch)
tree6fd6b55d7f335a3fb4a2f06d81417464087f1dfc /sway
parentRemove destroyed views from scratchpad (diff)
parentAdd get_clipbard ipc errors; Adapt swaymsg (diff)
downloadsway-6d83a59b461d07c4c9c5ee5809da658f5c56afc3.tar.gz
sway-6d83a59b461d07c4c9c5ee5809da658f5c56afc3.tar.zst
sway-6d83a59b461d07c4c9c5ee5809da658f5c56afc3.zip
Merge pull request #1263 from nyorain/master
Implement get_clipboard ipc message
Diffstat (limited to 'sway')
-rw-r--r--sway/CMakeLists.txt3
-rw-r--r--sway/base64.c221
-rw-r--r--sway/commands/ipc.c1
-rw-r--r--sway/ipc-server.c284
4 files changed, 508 insertions, 1 deletions
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
19add_executable(sway 19add_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
41add_definitions( 42add_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
29static 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
40char *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
116unsigned 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
63struct 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
62struct sockaddr_un *ipc_user_sockaddr(void); 76struct sockaddr_un *ipc_user_sockaddr(void);
63int ipc_handle_connection(int fd, uint32_t mask, void *data); 77int ipc_handle_connection(int fd, uint32_t mask, void *data);
64int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data); 78int 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
411static 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
419static 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
434static 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
497error:;
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
504release:
505 release_clipboard_request(req);
506 return 0;
507}
508
509static 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
524bool 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
552void 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
659data_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
666cleanup:
667 list_free(requested);
668 free(types);
669}
670
397void ipc_client_handle_command(struct ipc_client *client) { 671void 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;