clipboard.c
This file
Path
clipboard.c
752 lines · 19.6 KiB · C
Last changed by
clipboard: disable debug loggingDaniel Eklöfbefore adoption468ae06
History of this fileTake it
1#include "clipboard.h"23#include <string.h>4#include <errno.h>5#include <fcntl.h>67#include <sys/epoll.h>8#include <sys/timerfd.h>910#include <primary-selection-unstable-v1.h>1112#define LOG_MODULE "clipboard"13#define LOG_ENABLE_DBG 014#include "log.h"15#include "macros.h"16#include "prompt.h"17#include "uri.h"18#include "wayland.h"19#include "xmalloc.h"2021static const char *const mime_type_map[] = {22 [DATA_OFFER_MIME_UNSET] = NULL,23 [DATA_OFFER_MIME_TEXT_PLAIN] = "text/plain",24 [DATA_OFFER_MIME_TEXT_UTF8] = "text/plain;charset=utf-8",25 [DATA_OFFER_MIME_URI_LIST] = "text/uri-list",2627 [DATA_OFFER_MIME_TEXT_TEXT] = "TEXT",28 [DATA_OFFER_MIME_TEXT_STRING] = "STRING",29 [DATA_OFFER_MIME_TEXT_UTF8_STRING] = "UTF8_STRING",30};3132typedef void (*data_cb_t)(char *data, size_t size, void *user);33typedef void (*done_cb_t)(void *user);3435static void begin_receive_clipboard(36 struct seat *seat, int read_fd,37 enum data_offer_mime_type mime_type,38 data_cb_t data_cb, done_cb_t done_cb, void *user);3940static void41data_offer_reset(struct wl_clipboard *clipboard)42{43 if (clipboard->data_offer != NULL) {44 wl_data_offer_destroy(clipboard->data_offer);45 clipboard->data_offer = NULL;46 }4748 //clipboard->window = NULL;49 clipboard->mime_type = DATA_OFFER_MIME_UNSET;50}5152static void53clipboard_data(char *data, size_t size, void *user)54{55 struct seat *seat = user;56 wayl_clipboard_data(seat->wayl, data, size);57}5859static void60clipboard_done(void *user)61{62 struct seat *seat = user;63 wayl_clipboard_done(seat->wayl);64}6566static void67select_mime_type_for_offer(const char *_mime_type,68 enum data_offer_mime_type *type)69{70 enum data_offer_mime_type mime_type = DATA_OFFER_MIME_UNSET;7172 /* Translate offered mime type to our mime type enum */73 for (size_t i = 0; i < ALEN(mime_type_map); i++) {74 if (mime_type_map[i] == NULL)75 continue;7677 if (strcmp(_mime_type, mime_type_map[i]) == 0) {78 mime_type = i;79 break;80 }81 }8283 LOG_DBG("mime-type: %s -> %s (offered type was %s)",84 mime_type_map[*type],85 (mime_type_map[mime_type] == NULL ? "(null)" : mime_type_map[mime_type]),86 _mime_type);8788 /* Mime-type transition; if the new mime-type is "better" than89 * previously offered types, use the new type */9091 switch (mime_type) {92 case DATA_OFFER_MIME_TEXT_PLAIN:93 case DATA_OFFER_MIME_TEXT_TEXT:94 case DATA_OFFER_MIME_TEXT_STRING:95 /* text/plain is our least preferred type. Only use if current96 * type is unset */97 switch (*type) {98 case DATA_OFFER_MIME_UNSET:99 *type = mime_type;100 break;101102 default:103 break;104 }105 break;106107 case DATA_OFFER_MIME_TEXT_UTF8:108 case DATA_OFFER_MIME_TEXT_UTF8_STRING:109 /* text/plain;charset=utf-8 is preferred over text/plain */110 switch (*type) {111 case DATA_OFFER_MIME_UNSET:112 case DATA_OFFER_MIME_TEXT_PLAIN:113 case DATA_OFFER_MIME_TEXT_TEXT:114 case DATA_OFFER_MIME_TEXT_STRING:115 *type = mime_type;116 break;117118 default:119 break;120 }121 break;122123 case DATA_OFFER_MIME_URI_LIST:124 /* text/uri-list is always used when offered */125 *type = mime_type;126 break;127128 case DATA_OFFER_MIME_UNSET:129 break;130 }131}132133static void134offer(void *data, struct wl_data_offer *wl_data_offer, const char *mime_type)135{136 struct seat *seat = data;137 select_mime_type_for_offer(mime_type, &seat->clipboard.mime_type);138}139140static void141primary_offer(void *data,142 struct zwp_primary_selection_offer_v1 *zwp_primary_selection_offer,143 const char *mime_type)144{145 struct seat *seat = data;146 select_mime_type_for_offer(mime_type, &seat->primary.mime_type);147}148149static void150source_actions(void *data, struct wl_data_offer *wl_data_offer,151 uint32_t source_actions)152{153}154155static void156offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)157{158}159160static const struct wl_data_offer_listener data_offer_listener = {161 .offer = &offer,162 .source_actions = &source_actions,163 .action = &offer_action,164};165166static const struct zwp_primary_selection_offer_v1_listener primary_selection_offer_listener = {167 .offer = &primary_offer,168};169170171static void172data_offer(void *data, struct wl_data_device *wl_data_device,173 struct wl_data_offer *offer)174{175 struct seat *seat = data;176 data_offer_reset(&seat->clipboard);177 seat->clipboard.data_offer = offer;178 wl_data_offer_add_listener(offer, &data_offer_listener, seat);179}180181static void182enter(void *data, struct wl_data_device *wl_data_device, uint32_t serial,183 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y,184 struct wl_data_offer *offer)185{186 struct seat *seat = data;187188 assert(offer == seat->clipboard.data_offer);189190 //assert(seat->clipboard.window == NULL);191 if (seat->clipboard.mime_type != DATA_OFFER_MIME_UNSET &&192 !seat->is_pasting)193 {194 wl_data_offer_accept(195 offer, serial, mime_type_map[seat->clipboard.mime_type]);196 wl_data_offer_set_actions(197 offer,198 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY,199 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY);200 } else {201 /* reject offer */202 wl_data_offer_accept(offer, serial, NULL);203 wl_data_offer_set_actions(204 offer,205 WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE,206 WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE);207 }208}209210static void211leave(void *data, struct wl_data_device *wl_data_device)212{213}214215static void216motion(void *data, struct wl_data_device *wl_data_device, uint32_t time,217 wl_fixed_t x, wl_fixed_t y)218{219}220221struct dnd_context {222 struct seat *seat;223 struct wl_data_offer *data_offer;224};225226static void227receive_dnd(char *data, size_t size, void *user)228{229 struct dnd_context *ctx = user;230 clipboard_data(data, size, ctx->seat);231}232233static void234receive_dnd_done(void *user)235{236 struct dnd_context *ctx = user;237238 wl_data_offer_finish(ctx->data_offer);239 wl_data_offer_destroy(ctx->data_offer);240 clipboard_done(ctx->seat);241 free(ctx);242}243244static void245drop(void *data, struct wl_data_device *wl_data_device)246{247 struct seat *seat = data;248249250 struct wl_clipboard *clipboard = &seat->clipboard;251252 if (clipboard->mime_type == DATA_OFFER_MIME_UNSET) {253 LOG_WARN("compositor called data_device::drop() "254 "even though we rejected the drag-and-drop");255 return;256 }257258 struct dnd_context *ctx = xmalloc(sizeof(*ctx));259 *ctx = (struct dnd_context){260 .seat = seat,261 .data_offer = clipboard->data_offer,262 };263264 /* Prepare a pipe the other client can write its selection to us */265 int fds[2];266 if (pipe2(fds, O_CLOEXEC) == -1) {267 LOG_ERRNO("failed to create pipe");268 free(ctx);269 return;270 }271272 int read_fd = fds[0];273 int write_fd = fds[1];274275 LOG_DBG("DnD drop: mime-type=%s", mime_type_map[clipboard->mime_type]);276277 /* Give write-end of pipe to other client */278 wl_data_offer_receive(279 clipboard->data_offer, mime_type_map[clipboard->mime_type], write_fd);280281 /* Don't keep our copy of the write-end open (or we'll never get EOF) */282 close(write_fd);283284 seat->is_pasting = true;285 begin_receive_clipboard(286 seat, read_fd, clipboard->mime_type,287 &receive_dnd, &receive_dnd_done, ctx);288289 /* data offer is now "owned" by the receive context */290 clipboard->data_offer = NULL;291 clipboard->mime_type = DATA_OFFER_MIME_UNSET;292}293294static void295selection(void *data, struct wl_data_device *wl_data_device,296 struct wl_data_offer *offer)297{298 /* Selection offer from other client */299 struct seat *seat = data;300 if (offer == NULL)301 data_offer_reset(&seat->clipboard);302 else303 assert(offer == seat->clipboard.data_offer);304}305306static void307primary_offer_reset(struct wl_primary *primary)308{309 if (primary->data_offer != NULL) {310 zwp_primary_selection_offer_v1_destroy(primary->data_offer);311 primary->data_offer = NULL;312 }313314 primary->mime_type = DATA_OFFER_MIME_UNSET;315}316317static void318primary_data_offer(void *data,319 struct zwp_primary_selection_device_v1 *zwp_primary_selection_device,320 struct zwp_primary_selection_offer_v1 *offer)321{322 struct seat *seat = data;323 primary_offer_reset(&seat->primary);324 seat->primary.data_offer = offer;325 zwp_primary_selection_offer_v1_add_listener(326 offer, &primary_selection_offer_listener, seat);327}328329static void330primary_selection(void *data,331 struct zwp_primary_selection_device_v1 *zwp_primary_selection_device,332 struct zwp_primary_selection_offer_v1 *offer)333{334}335336const struct wl_data_device_listener data_device_listener = {337 .data_offer = &data_offer,338 .enter = &enter,339 .leave = &leave,340 .motion = &motion,341 .drop = &drop,342 .selection = &selection,343};344345const struct zwp_primary_selection_device_v1_listener primary_selection_device_listener = {346 .data_offer = &primary_data_offer,347 .selection = &primary_selection,348};349350struct clipboard_receive {351 struct seat *seat;352 int read_fd;353 int timeout_fd;354 struct itimerspec timeout;355 bool quote_paths;356357 void (*decoder)(struct clipboard_receive *ctx, char *data, size_t size);358 void (*finish)(struct clipboard_receive *ctx);359360 /* URI state */361 bool add_space;362 struct {363 char *data;364 size_t sz;365 size_t idx;366 } buf;367368 /* Callback data */369 data_cb_t data_cb;370 done_cb_t done_cb;371 void *user;372};373374static void375clipboard_receive_done(struct fdm *fdm, struct clipboard_receive *ctx)376{377 ctx->seat->is_pasting = false;378379 fdm_del(fdm, ctx->timeout_fd);380 fdm_del(fdm, ctx->read_fd);381 ctx->done_cb(ctx->user);382 free(ctx->buf.data);383 free(ctx);384}385386static bool387fdm_receive_timeout(struct fdm *fdm, int fd, int events, void *data)388{389 struct clipboard_receive *ctx = data;390 if (events & EPOLLHUP)391 return false;392393 assert(events & EPOLLIN);394395 uint64_t expire_count;396 ssize_t ret = read(fd, &expire_count, sizeof(expire_count));397 if (ret < 0) {398 if (errno == EAGAIN)399 return true;400401 LOG_ERRNO("failed to read clipboard timeout timer");402 return false;403 }404405 LOG_WARN("no data received from clipboard in %llu seconds, aborting",406 (unsigned long long)ctx->timeout.it_value.tv_sec);407408 clipboard_receive_done(fdm, ctx);409 return true;410}411412static void413fdm_receive_decoder_plain(struct clipboard_receive *ctx, char *data, size_t size)414{415 ctx->data_cb(data, size, ctx->user);416}417418static void419fdm_receive_finish_plain(struct clipboard_receive *ctx)420{421}422423static bool424decode_one_uri(struct clipboard_receive *ctx, char *uri, size_t len)425{426 LOG_DBG("URI: \"%.*s\"", (int)len, uri);427428 if (len == 0)429 return false;430431 char *scheme, *host, *path;432 if (!uri_parse(uri, len, &scheme, NULL, NULL, &host, NULL, &path, NULL, NULL)) {433 LOG_ERR("drag-and-drop: invalid URI: %.*s", (int)len, uri);434 return false;435 }436437 if (ctx->add_space)438 ctx->data_cb(" ", 1, ctx->user);439 ctx->add_space = true;440441 if (strcmp(scheme, "file") == 0 && hostname_is_localhost(host)) {442 if (ctx->quote_paths)443 ctx->data_cb("'", 1, ctx->user);444445 ctx->data_cb(path, strlen(path), ctx->user);446447 if (ctx->quote_paths)448 ctx->data_cb("'", 1, ctx->user);449 } else450 ctx->data_cb(uri, len, ctx->user);451452 free(scheme);453 free(host);454 free(path);455 return true;456}457458static void459fdm_receive_decoder_uri(struct clipboard_receive *ctx, char *data, size_t size)460{461 while (ctx->buf.idx + size > ctx->buf.sz) {462 size_t new_sz = ctx->buf.sz == 0 ? size : 2 * ctx->buf.sz;463 ctx->buf.data = xrealloc(ctx->buf.data, new_sz);464 ctx->buf.sz = new_sz;465 }466467 memcpy(&ctx->buf.data[ctx->buf.idx], data, size);468 ctx->buf.idx += size;469470 char *start = ctx->buf.data;471 char *end = NULL;472473 while (true) {474 for (end = start; end < &ctx->buf.data[ctx->buf.idx]; end++) {475 if (*end == '\r' || *end == '\n')476 break;477 }478479 if (end >= &ctx->buf.data[ctx->buf.idx])480 break;481482 decode_one_uri(ctx, start, end - start);483 start = end + 1;484 }485486 const size_t ofs = start - ctx->buf.data;487 const size_t left = ctx->buf.idx - ofs;488489 memmove(&ctx->buf.data[0], &ctx->buf.data[ofs], left);490 ctx->buf.idx = left;491}492493static void494fdm_receive_finish_uri(struct clipboard_receive *ctx)495{496 LOG_DBG("finish: %.*s", (int)ctx->buf.idx, ctx->buf.data);497 decode_one_uri(ctx, ctx->buf.data, ctx->buf.idx);498}499500static bool501fdm_receive(struct fdm *fdm, int fd, int events, void *data)502{503 struct clipboard_receive *ctx = data;504505 if ((events & EPOLLHUP) && !(events & EPOLLIN))506 goto done;507508 /* Reset timeout timer */509 if (timerfd_settime(ctx->timeout_fd, 0, &ctx->timeout, NULL) < 0) {510 LOG_ERRNO("failed to re-arm clipboard timeout timer");511 return false;512 }513514 /* Read until EOF */515 while (true) {516 char text[256];517 ssize_t count = read(fd, text, sizeof(text));518519 if (count == -1) {520 if (errno == EAGAIN || errno == EWOULDBLOCK)521 return true;522523 LOG_ERRNO("failed to read clipboard data");524 break;525 }526527 if (count == 0)528 break;529530 /*531 * Call cb while at same time replace:532 * - \r\n -> \r (non-bracketed paste)533 * - \n -> \r (non-bracketed paste)534 * - C0 -> <nothing> (strip non-formatting C0 characters)535 * - \e -> <nothing> (i.e. strip ESC)536 */537 char *p = text;538 size_t left = count;539540#define skip_one() \541 do { \542 ctx->decoder(ctx, p, i); \543 assert(i + 1 <= left); \544 p += i + 1; \545 left -= i + 1; \546 } while (0)547548 again:549 for (size_t i = 0; i < left; i++) {550 switch (p[i]) {551 default:552 break;553554 case '\n':555 skip_one();556 goto again;557558 case '\r':559 /* Convert \r\n -> \r */560 if (i + 1 < left && p[i + 1] == '\n') {561 i++;562 skip_one();563 goto again;564 }565 break;566567 /* C0 non-formatting control characters (\b \t \n \r excluded) */568 case '\x01': case '\x02': case '\x03': case '\x04': case '\x05':569 case '\x06': case '\x07': case '\x0e': case '\x0f': case '\x10':570 case '\x11': case '\x12': case '\x13': case '\x14': case '\x15':571 case '\x16': case '\x17': case '\x18': case '\x19': case '\x1a':572 case '\x1b': case '\x1c': case '\x1d': case '\x1e': case '\x1f':573 case '\b': case '\x7f': case '\x00':574 skip_one();575 goto again;576 }577 }578579 ctx->decoder(ctx, p, left);580 left = 0;581 }582583#undef skip_one584585done:586 ctx->finish(ctx);587 clipboard_receive_done(fdm, ctx);588 return true;589}590591static void592begin_receive_clipboard(struct seat *seat, int read_fd,593 enum data_offer_mime_type mime_type,594 data_cb_t data_cb, done_cb_t done_cb, void *user)595{596 int timeout_fd = -1;597 struct clipboard_receive *ctx = NULL;598599 int flags;600 if ((flags = fcntl(read_fd, F_GETFL)) < 0 ||601 fcntl(read_fd, F_SETFL, flags | O_NONBLOCK) < 0)602 {603 LOG_ERRNO("failed to set O_NONBLOCK");604 goto err;605 }606607 timeout_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);608 if (timeout_fd < 0) {609 LOG_ERRNO("failed to create clipboard timeout timer FD");610 goto err;611 }612613 const struct itimerspec timeout = {.it_value = {.tv_sec = 2}};614 if (timerfd_settime(timeout_fd, 0, &timeout, NULL) < 0) {615 LOG_ERRNO("failed to arm clipboard timeout timer");616 goto err;617 }618619 ctx = xmalloc(sizeof(*ctx));620 *ctx = (struct clipboard_receive) {621 .seat = seat,622 .read_fd = read_fd,623 .timeout_fd = timeout_fd,624 .timeout = timeout,625 .quote_paths = true,626 .decoder = (mime_type == DATA_OFFER_MIME_URI_LIST627 ? &fdm_receive_decoder_uri628 : &fdm_receive_decoder_plain),629 .finish = (mime_type == DATA_OFFER_MIME_URI_LIST630 ? &fdm_receive_finish_uri631 : &fdm_receive_finish_plain),632 .data_cb = data_cb,633 .done_cb = done_cb,634 .user = user,635 };636637 if (!fdm_add(seat->fdm, read_fd, EPOLLIN, &fdm_receive, ctx) ||638 !fdm_add(seat->fdm, timeout_fd, EPOLLIN, &fdm_receive_timeout, ctx))639 {640 goto err;641 }642643 return;644645err:646 free(ctx);647 fdm_del(seat->fdm, timeout_fd);648 fdm_del(seat->fdm, read_fd);649 done_cb(user);650}651652static void653text_from_clipboard(struct seat *seat,654 void (*cb)(char *data, size_t size, void *user),655 void (*done)(void *user), void *user)656{657 struct wl_clipboard *clipboard = &seat->clipboard;658 if (clipboard->data_offer == NULL ||659 clipboard->mime_type == DATA_OFFER_MIME_UNSET)660 {661 done(user);662 return;663 }664665 /* Prepare a pipe the other client can write its selection to us */666 int fds[2];667 if (pipe2(fds, O_CLOEXEC) == -1) {668 LOG_ERRNO("failed to create pipe");669 done(user);670 return;671 }672673 LOG_DBG("receive from clipboard: mime-type=%s",674 mime_type_map[clipboard->mime_type]);675676 int read_fd = fds[0];677 int write_fd = fds[1];678679 /* Give write-end of pipe to other client */680 wl_data_offer_receive(681 clipboard->data_offer, mime_type_map[clipboard->mime_type], write_fd);682683 /* Don't keep our copy of the write-end open (or we'll never get EOF) */684 close(write_fd);685 begin_receive_clipboard(seat, read_fd, clipboard->mime_type, cb, done, user);686}687688static void689text_from_primary(690 struct seat *seat,691 void (*cb)(char *data, size_t size, void *user),692 void (*done)(void *user),693 void *user)694{695 struct wl_primary *primary = &seat->primary;696 if (primary->data_offer == NULL ||697 primary->mime_type == DATA_OFFER_MIME_UNSET)698 {699 done(user);700 return;701 }702703 /* Prepare a pipe the other client can write its selection to us */704 int fds[2];705 if (pipe2(fds, O_CLOEXEC) == -1) {706 LOG_ERRNO("failed to create pipe");707 done(user);708 return;709 }710711 LOG_DBG("receive from primary: mime-type=%s",712 mime_type_map[primary->mime_type]);713714 int read_fd = fds[0];715 int write_fd = fds[1];716717 /* Give write-end of pipe to other client */718 zwp_primary_selection_offer_v1_receive(719 primary->data_offer, mime_type_map[primary->mime_type], write_fd);720721 /* Don't keep our copy of the write-end open (or we'll never get EOF) */722 close(write_fd);723 begin_receive_clipboard(seat, read_fd, primary->mime_type, cb, done, user);724}725726void727paste_from_clipboard(struct seat *seat)728{729 struct wl_clipboard *clipboard = &seat->clipboard;730 if (clipboard->data_offer == NULL)731 return;732733 if (seat->is_pasting)734 return;735736 seat->is_pasting = true;737 text_from_clipboard(seat, &clipboard_data, &clipboard_done, seat);738}739740void741paste_from_primary(struct seat *seat)742{743 struct wl_primary *primary = &seat->primary;744 if (primary->data_offer == NULL)745 return;746747 if (seat->is_pasting)748 return;749750 seat->is_pasting = true;751 text_from_primary(seat, &clipboard_data, &clipboard_done, seat);752}