aboutsummaryrefslogtreecommitdiffstats
path: root/sway/ipc-server.c
diff options
context:
space:
mode:
authorLibravatar nyorain <nyorain@gmail.com>2017-07-07 21:51:34 +0200
committerLibravatar nyorain <nyorain@gmail.com>2017-07-07 21:51:34 +0200
commitc0f2acce4e08eecdcf29058335aece113467daee (patch)
tree7f871abe8b5d1cdf4a74512810d68e9e66f0d57f /sway/ipc-server.c
parentFix/Simplify get_clipboard ipc-server impl (diff)
downloadsway-c0f2acce4e08eecdcf29058335aece113467daee.tar.gz
sway-c0f2acce4e08eecdcf29058335aece113467daee.tar.zst
sway-c0f2acce4e08eecdcf29058335aece113467daee.zip
Rework get_clipboard implementation
Diffstat (limited to 'sway/ipc-server.c')
-rw-r--r--sway/ipc-server.c275
1 files changed, 190 insertions, 85 deletions
diff --git a/sway/ipc-server.c b/sway/ipc-server.c
index b6268404..70692f64 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>
@@ -56,8 +57,11 @@ struct get_pixels_request {
56}; 57};
57 58
58struct get_clipboard_request { 59struct get_clipboard_request {
59 struct ipc_client *client; 60 struct ipc_client *client;
60 struct wlc_event_source *event_source; 61 json_object *json;
62 struct wlc_event_source *event_source;
63 char *type;
64 unsigned int *pending;
61}; 65};
62 66
63struct sockaddr_un *ipc_user_sockaddr(void); 67struct sockaddr_un *ipc_user_sockaddr(void);
@@ -327,44 +331,201 @@ void ipc_get_pixels(wlc_handle output) {
327 ipc_get_pixel_requests = unhandled; 331 ipc_get_pixel_requests = unhandled;
328} 332}
329 333
330static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) 334static int ipc_selection_data_cb(int fd, uint32_t mask, void *data) {
331{
332 assert(data); 335 assert(data);
333 struct get_clipboard_request *req = (struct get_clipboard_request*) data; 336 struct get_clipboard_request *req = (struct get_clipboard_request *)data;
334 337
335 if (mask & WLC_EVENT_ERROR) { 338 if (mask & WLC_EVENT_ERROR) {
336 sway_log(L_ERROR, "Selection data fd error"); 339 sway_log(L_ERROR, "Selection data fd error");
337 const char *error = "{ \"success\": false, \"error\": "
338 "\"Could not receive text data from clipboard\" }";
339 ipc_send_reply(req->client, error, (uint32_t)strlen(error));
340 goto cleanup; 340 goto cleanup;
341 } 341 }
342 342
343 if (mask & WLC_EVENT_READABLE) { 343 if (mask & WLC_EVENT_READABLE || true) {
344 char buf[512]; 344 static const int step_size = 512;
345 int ret = read(fd, buf, 511); 345 char *data = NULL;
346 if (ret < 0) { 346 int ret = 0;
347 sway_log_errno(L_ERROR, "Reading from selection data fd failed"); 347 int current = 0;
348 const char *error = "{ \"success\": false, \"error\": " 348
349 "\"Could not receive text data from clipboard\" }"; 349 // read data as long as there is data avilable
350 ipc_send_reply(req->client, error, (uint32_t)strlen(error)); 350 // grow the buffer step_size in every iteration
351 goto cleanup; 351 do {
352 if (data == NULL) {
353 data = malloc(step_size);
354 } else {
355 data = realloc(data, current + step_size);
356 }
357
358 ret = read(fd, data + current, step_size - 1);
359 if (ret < 0) {
360 sway_log_errno(L_ERROR, "Reading from selection data fd failed");
361 goto cleanup;
362 }
363
364 current += ret;
365 } while (ret == step_size - 1);
366
367 data[current] = '\0';
368
369 if (strncmp(req->type, "text/", 5) == 0) {
370 json_object_object_add(req->json, req->type,
371 json_object_new_string(data));
372 } else {
373 size_t outlen;
374 char *b64 = b64_encode(data, current, &outlen);
375 json_object_object_add(req->json, req->type,
376 json_object_new_string(b64));
377 free(b64);
352 } 378 }
353 379
354 buf[ret] = '\0'; 380 free(data);
355 json_object *obj = json_object_new_object(); 381 } else {
356 json_object_object_add(obj, "success", json_object_new_boolean(true)); 382 return 0; // TODO
357 json_object_object_add(obj, "content", json_object_new_string(buf)); 383 }
358 const char *str = json_object_to_json_string(obj); 384
385cleanup:
386 close(fd);
387
388 if (--(*req->pending) == 0) {
389 const char *str = json_object_to_json_string(req->json);
359 ipc_send_reply(req->client, str, (uint32_t)strlen(str)); 390 ipc_send_reply(req->client, str, (uint32_t)strlen(str));
391 json_object_put(req->json);
392 }
393
394 free(req->type);
395 wlc_event_source_remove(req->event_source);
396 free(req);
397 return 0;
398}
399
400// greedy wildcard (only "*") matching
401bool mime_type_matches(const char *mime_type, const char *pattern) {
402 const char* wildcard = NULL;
403 while (*mime_type && *pattern) {
404 if (*pattern == '*' && !wildcard) {
405 wildcard = pattern;
406 ++pattern;
407 }
408
409 if (*mime_type != *pattern) {
410 if (!wildcard)
411 return false;
412
413 pattern = wildcard;
414 ++mime_type;
415 continue;
416 }
417
418 ++mime_type;
419 ++pattern;
420 }
421
422 while (*pattern == '*') {
423 ++pattern;
424 }
425
426 return (*mime_type == *pattern);
427}
428
429void ipc_get_clipboard(struct ipc_client *client, char *buf) {
430 static const char *error_json = "{ \"success\": false, \"error\": "
431 "\"Failed to retrieve clipboard data\" }";
432
433 size_t size;
434 const char **types = wlc_get_selection_types(&size);
435 if (client->payload_length == 0) {
436 json_object *obj = json_object_new_array();
437 for (size_t i = 0; i < size; ++i) {
438 json_object_array_add(obj, json_object_new_string(types[i]));
439 }
440
441 const char *str = json_object_to_json_string(obj);
442 ipc_send_reply(client, str, strlen(str));
360 json_object_put(obj); 443 json_object_put(obj);
444 return;
445 }
446
447 unescape_string(buf);
448 strip_quotes(buf);
449 list_t *requested = split_string(buf, " ");
450 json_object *json = json_object_new_object();
451 unsigned int *pending = malloc(sizeof(unsigned int));
452 *pending = 0;
453
454 for (size_t l = 0; l < (size_t) requested->length; ++l) {
455 const char *pattern = requested->items[l];
456 bool found = false;
457 for (size_t i = 0; i < size; ++i) {
458 if (mime_type_matches(types[i], pattern)) {
459 found = true;
460
461 struct get_clipboard_request *req = malloc(sizeof(*req));
462 if (!req) {
463 sway_log(L_ERROR, "Cannot allocate get_clipboard_request");
464 goto data_error;
465 }
466
467 int pipes[2];
468 if (pipe(pipes) == -1) {
469 sway_log_errno(L_ERROR, "pipe call failed");
470 free(req);
471 goto data_error;
472 }
473
474 fcntl(pipes[0], F_SETFD, FD_CLOEXEC | O_NONBLOCK);
475 fcntl(pipes[1], F_SETFD, FD_CLOEXEC | O_NONBLOCK);
476
477 if (!wlc_get_selection_data(types[i], pipes[1])) {
478 close(pipes[0]);
479 close(pipes[1]);
480 free(req);
481 sway_log(L_ERROR, "wlc_get_selection_data failed");
482 goto data_error;
483 }
484
485 (*pending)++;
486
487 req->client = client;
488 req->type = strdup(types[i]);
489 req->json = json;
490 req->pending = pending;
491 req->event_source = wlc_event_loop_add_fd(pipes[0],
492 WLC_EVENT_READABLE | WLC_EVENT_ERROR | WLC_EVENT_HANGUP,
493 &ipc_selection_data_cb, req);
494
495 // NOTE: remove this goto to enable retrieving multiple
496 // targets at once. The whole implementation is already
497 // made for it. The only reason it was disabled
498 // at the time of writing is that neither wlc's xselection
499 // implementation nor (apparently) gtk on wayland supports
500 // multiple send requests at the same time which makes
501 // every request except the last one fail (and therefore
502 // return empty data)
503 goto cleanup;
504 }
505 }
506
507 if (!found) {
508 sway_log(L_INFO, "Invalid clipboard type %s requested", pattern);
509 }
361 } 510 }
362 511
512 if (*pending == 0) {
513 static const char *empty = "[]";
514 ipc_send_reply(client, empty, (uint32_t)strlen(empty));
515 free(json);
516 free(pending);
517 }
518
519 goto cleanup;
520
521data_error:
522 ipc_send_reply(client, error_json, (uint32_t)strlen(error_json));
523 free(json);
524 free(pending);
525
363cleanup: 526cleanup:
364 wlc_event_source_remove(req->event_source); 527 list_free(requested);
365 close(fd); 528 free(types);
366 free(req);
367 return 0;
368} 529}
369 530
370void ipc_client_handle_command(struct ipc_client *client) { 531void ipc_client_handle_command(struct ipc_client *client) {
@@ -610,69 +771,13 @@ void ipc_client_handle_command(struct ipc_client *client) {
610 goto exit_cleanup; 771 goto exit_cleanup;
611 } 772 }
612 773
613 case IPC_GET_CLIPBOARD: 774 case IPC_GET_CLIPBOARD:
614 { 775 {
615 if (!(client->security_policy & IPC_FEATURE_GET_CLIPBOARD)) { 776 if (!(client->security_policy & IPC_FEATURE_GET_CLIPBOARD)) {
616 goto exit_denied; 777 goto exit_denied;
617 } 778 }
618 779
619 size_t size; 780 ipc_get_clipboard(client, buf);
620 const char **types = wlc_get_selection_types(&size);
621 const char *type = NULL;
622 if (types == NULL || size == 0) {
623 const char *error = "{ \"success\": false, \"error\": "
624 "\"Empty clipboard\" }";
625 ipc_send_reply(client, error, (uint32_t)strlen(error));
626 goto exit_cleanup;
627 }
628
629 for (size_t i = 0; i < size; ++i) {
630 if (strcmp(types[i], "text/plain;charset=utf-8") == 0
631 || strcmp(types[i], "text/plain") == 0) {
632 type = types[i];
633 break;
634 }
635 }
636
637 if (type) {
638 struct get_clipboard_request *req = malloc(sizeof(*req));
639 if (!req) {
640 sway_log(L_ERROR, "Unable to allocate get_clipboard_request");
641 goto clipboard_error;
642 }
643
644 int pipes[2];
645 if (pipe(pipes) == -1) {
646 sway_log_errno(L_ERROR, "pipe call failed");
647 free(req);
648 goto clipboard_error;
649 }
650
651 fcntl(pipes[0], F_SETFD, FD_CLOEXEC | O_NONBLOCK);
652 fcntl(pipes[1], F_SETFD, FD_CLOEXEC | O_NONBLOCK);
653 if (!wlc_get_selection_data(type, pipes[1])) {
654 close(pipes[0]);
655 close(pipes[1]);
656 free(req);
657 sway_log(L_ERROR, "wlc_get_selection_data failed");
658 goto clipboard_error;
659 }
660
661 req->client = client;
662 req->event_source = wlc_event_loop_add_fd(pipes[0],
663 WLC_EVENT_READABLE | WLC_EVENT_ERROR | WLC_EVENT_HANGUP,
664 &ipc_selection_data_cb, req);
665 free(types);
666 goto exit_cleanup;
667 }
668
669clipboard_error:
670 sway_log(L_INFO, "Clipboard has to text data");
671 const char *error = "{ \"success\": false, \"error\": "
672 "\"Could not receive text data from clipboard\" }";
673 ipc_send_reply(client, error, (uint32_t)strlen(error));
674
675 free(types);
676 goto exit_cleanup; 781 goto exit_cleanup;
677 } 782 }
678 783