aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar nyorain <nyorain@gmail.com>2017-07-07 15:46:17 +0200
committerLibravatar nyorain <nyorain@gmail.com>2017-07-07 15:46:17 +0200
commit42547cafb64be99debf5cbcd5986e2c1fa30216a (patch)
tree1e37b128e055f9a88c58d9e10c69dffaa71ff338
parentImplement ipc get_clipboard (diff)
downloadsway-42547cafb64be99debf5cbcd5986e2c1fa30216a.tar.gz
sway-42547cafb64be99debf5cbcd5986e2c1fa30216a.tar.zst
sway-42547cafb64be99debf5cbcd5986e2c1fa30216a.zip
Fix/Simplify get_clipboard ipc-server impl
-rw-r--r--sway/ipc-server.c34
1 files changed, 18 insertions, 16 deletions
diff --git a/sway/ipc-server.c b/sway/ipc-server.c
index 5bc64b1f..b6268404 100644
--- a/sway/ipc-server.c
+++ b/sway/ipc-server.c
@@ -617,8 +617,8 @@ void ipc_client_handle_command(struct ipc_client *client) {
617 } 617 }
618 618
619 size_t size; 619 size_t size;
620 bool found = false;
621 const char **types = wlc_get_selection_types(&size); 620 const char **types = wlc_get_selection_types(&size);
621 const char *type = NULL;
622 if (types == NULL || size == 0) { 622 if (types == NULL || size == 0) {
623 const char *error = "{ \"success\": false, \"error\": " 623 const char *error = "{ \"success\": false, \"error\": "
624 "\"Empty clipboard\" }"; 624 "\"Empty clipboard\" }";
@@ -627,48 +627,50 @@ void ipc_client_handle_command(struct ipc_client *client) {
627 } 627 }
628 628
629 for (size_t i = 0; i < size; ++i) { 629 for (size_t i = 0; i < size; ++i) {
630 if (strcmp(types[i], "text/plain;charset=utf-8") != 0 630 if (strcmp(types[i], "text/plain;charset=utf-8") == 0
631 && strcmp(types[i], "text/plain") != 0) { 631 || strcmp(types[i], "text/plain") == 0) {
632 continue; 632 type = types[i];
633 break;
633 } 634 }
635 }
634 636
637 if (type) {
635 struct get_clipboard_request *req = malloc(sizeof(*req)); 638 struct get_clipboard_request *req = malloc(sizeof(*req));
636 if (!req) { 639 if (!req) {
637 sway_log(L_ERROR, "Unable to allocate get_clipboard_request"); 640 sway_log(L_ERROR, "Unable to allocate get_clipboard_request");
638 goto exit_cleanup; 641 goto clipboard_error;
639 } 642 }
640 643
641 int pipes[2]; 644 int pipes[2];
642 if (pipe(pipes) == -1) { 645 if (pipe(pipes) == -1) {
643 sway_log_errno(L_ERROR, "pipe call failed"); 646 sway_log_errno(L_ERROR, "pipe call failed");
644 free(req); 647 free(req);
645 break; 648 goto clipboard_error;
646 } 649 }
647 650
648 fcntl(pipes[0], F_SETFD, FD_CLOEXEC | O_NONBLOCK); 651 fcntl(pipes[0], F_SETFD, FD_CLOEXEC | O_NONBLOCK);
649 fcntl(pipes[1], F_SETFD, FD_CLOEXEC | O_NONBLOCK); 652 fcntl(pipes[1], F_SETFD, FD_CLOEXEC | O_NONBLOCK);
650 if (!wlc_get_selection_data(types[i], pipes[1])) { 653 if (!wlc_get_selection_data(type, pipes[1])) {
651 close(pipes[0]); 654 close(pipes[0]);
652 close(pipes[1]); 655 close(pipes[1]);
653 free(req); 656 free(req);
654 sway_log(L_ERROR, "wlc_get_selection_data failed"); 657 sway_log(L_ERROR, "wlc_get_selection_data failed");
655 break; 658 goto clipboard_error;
656 } 659 }
657 660
658 req->client = client; 661 req->client = client;
659 req->event_source = wlc_event_loop_add_fd(pipes[0], 662 req->event_source = wlc_event_loop_add_fd(pipes[0],
660 WLC_EVENT_READABLE | WLC_EVENT_ERROR | WLC_EVENT_HANGUP, 663 WLC_EVENT_READABLE | WLC_EVENT_ERROR | WLC_EVENT_HANGUP,
661 &ipc_selection_data_cb, req); 664 &ipc_selection_data_cb, req);
662 found = true; 665 free(types);
663 break; 666 goto exit_cleanup;
664 } 667 }
665 668
666 if (!found) { 669clipboard_error:
667 sway_log(L_INFO, "Clipboard has to text data"); 670 sway_log(L_INFO, "Clipboard has to text data");
668 const char *error = "{ \"success\": false, \"error\": " 671 const char *error = "{ \"success\": false, \"error\": "
669 "\"Could not receive text data from clipboard\" }"; 672 "\"Could not receive text data from clipboard\" }";
670 ipc_send_reply(client, error, (uint32_t)strlen(error)); 673 ipc_send_reply(client, error, (uint32_t)strlen(error));
671 }
672 674
673 free(types); 675 free(types);
674 goto exit_cleanup; 676 goto exit_cleanup;