aboutsummaryrefslogtreecommitdiffstats
path: root/sway/ipc-server.c
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/ipc-server.c
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/ipc-server.c')
-rw-r--r--sway/ipc-server.c284
1 files changed, 284 insertions, 0 deletions
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;