aboutsummaryrefslogtreecommitdiff
path: root/selection.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2019-07-11 14:26:25 +0000
committerDaniel Eklöf <daniel@ekloef.se>2019-07-11 15:38:03 +0000
commit3f02a7354ec940514193eeccbdcaa5272560bb0a (patch)
treedbbd37bfac589d565618502517e32b3e5e574cad /selection.c
parentselection: add support for pasting from clipboard (diff)
selection: handle partial writes and EINTR when writing to clipboard
Diffstat (limited to 'selection.c')
-rw-r--r--selection.c18
1 files changed, 16 insertions, 2 deletions
diff --git a/selection.c b/selection.c
index 29d4cb46..50c7cc6b 100644
--- a/selection.c
+++ b/selection.c
@@ -3,6 +3,7 @@
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
+#include <errno.h>
#define LOG_MODULE "selection"
#define LOG_ENABLE_DBG 0
@@ -43,6 +44,7 @@ extract_selection(const struct terminal *term)
if (row == NULL)
continue;
+ /* TODO: replace '\0' with spaces, then trim lines */
for (int col = start_col; col < term->cols; col++) {
const struct cell *cell = &row->cells[col];
if (cell->c[0] == '\0')
@@ -180,11 +182,23 @@ send(void *data, struct wl_data_source *wl_data_source, const char *mime_type,
{
const struct clipboard *clipboard = wl_data_source_get_user_data(wl_data_source);
- LOG_WARN("SENDING CLIPBOARD!");
assert(clipboard != NULL);
assert(clipboard->text != NULL);
- write(fd, clipboard->text, strlen(clipboard->text));
+ size_t left = strlen(clipboard->text);
+ size_t idx = 0;
+
+ while (left > 0) {
+ ssize_t ret = write(fd, &clipboard->text[idx], left);
+ if (ret == -1 && errno != EINTR) {
+ LOG_ERRNO("failed to write to clipboard");
+ break;
+ }
+
+ left -= ret;
+ idx += ret;
+ }
+
close(fd);
}