aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2019-07-19 12:20:00 +0000
committerDaniel Eklöf <daniel@ekloef.se>2019-07-19 12:20:00 +0000
commit793c37923ebae5251ed55f7d55bc026b2e511fa0 (patch)
tree599095b3ed4caa5a77d74dd1158d5d489114e9cf
parentbase64: implement encode() (diff)
selection: add text_from_clipboard()
This function reads data from the clipboard, and calls a user-provided callback for each chunk of clipboard data.
-rw-r--r--selection.c33
-rw-r--r--selection.h4
2 files changed, 30 insertions, 7 deletions
diff --git a/selection.c b/selection.c
index 024e7f37..6f7c015e 100644
--- a/selection.c
+++ b/selection.c
@@ -454,7 +454,9 @@ selection_to_clipboard(struct terminal *term, uint32_t serial)
}
void
-selection_from_clipboard(struct terminal *term, uint32_t serial)
+text_from_clipboard(struct terminal *term, uint32_t serial,
+ void (*cb)(const char *data, size_t size, void *user),
+ void *user)
{
struct clipboard *clipboard = &term->selection.clipboard;
if (clipboard->data_offer == NULL)
@@ -478,9 +480,6 @@ selection_from_clipboard(struct terminal *term, uint32_t serial)
/* Don't keep our copy of the write-end open (or we'll never get EOF) */
close(write_fd);
- if (term->bracketed_paste)
- vt_to_slave(term, "\033[200~", 6);
-
/* Read until EOF */
while (true) {
char text[256];
@@ -492,13 +491,33 @@ selection_from_clipboard(struct terminal *term, uint32_t serial)
} else if (amount == 0)
break;
- vt_to_slave(term, text, amount);
+ cb(text, amount, user);
}
+ close(read_fd);
+}
+
+static void
+from_clipboard_cb(const char *data, size_t size, void *user)
+{
+ struct terminal *term = user;
+ vt_to_slave(term, data, size);
+}
+
+void
+selection_from_clipboard(struct terminal *term, uint32_t serial)
+{
+ struct clipboard *clipboard = &term->selection.clipboard;
+ if (clipboard->data_offer == NULL)
+ return;
+
if (term->bracketed_paste)
- vt_to_slave(term, "\033[201~", 6);
+ vt_to_slave(term, "\033[200~", 6);
- close(read_fd);
+ text_from_clipboard(term, serial, &from_clipboard_cb, term);
+
+ if (term->bracketed_paste)
+ vt_to_slave(term, "\033[201~", 6);
}
void
diff --git a/selection.h b/selection.h
index a98a0327..8473dbfa 100644
--- a/selection.h
+++ b/selection.h
@@ -18,3 +18,7 @@ void selection_from_clipboard(struct terminal *term, uint32_t serial);
void selection_from_primary(struct terminal *term);
bool text_to_clipboard(struct terminal *term, char *text, uint32_t serial);
+void text_from_clipboard(
+ struct terminal *term, uint32_t serial,
+ void (*cb)(const char *data, size_t size, void *user),
+ void *user);