aboutsummaryrefslogtreecommitdiff
path: root/osc.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2020-09-10 05:36:19 +0000
committerDaniel Eklöf <daniel@ekloef.se>2020-09-10 05:36:19 +0000
commitfc510d423b1acf0b4c75f53b36edef9d0b135216 (patch)
treed0574e4830bccff656ad8153070e100d154768ad /osc.c
parentconfig: spelling: mosue -> mouse (diff)
parentosc52: use first source that actually *has* data (diff)
Merge branch 'osc52-clear-selection-on-invalid-payload' into master
Diffstat (limited to 'osc.c')
-rw-r--r--osc.c79
1 files changed, 48 insertions, 31 deletions
diff --git a/osc.c b/osc.c
index 50d6ab3b..7e22d7c1 100644
--- a/osc.c
+++ b/osc.c
@@ -23,17 +23,6 @@ static void
osc_to_clipboard(struct terminal *term, const char *target,
const char *base64_data)
{
- char *decoded = base64_decode(base64_data);
- if (decoded == NULL) {
- if (errno == EINVAL)
- LOG_WARN("OSC: invalid clipboard data: %s", base64_data);
- else
- LOG_ERRNO("base64_decode() failed");
- return;
- }
-
- LOG_DBG("decoded: %s", decoded);
-
bool to_clipboard = false;
bool to_primary = false;
@@ -71,6 +60,22 @@ osc_to_clipboard(struct terminal *term, const char *target,
return;
}
+ char *decoded = base64_decode(base64_data);
+ if (decoded == NULL) {
+ if (errno == EINVAL)
+ LOG_WARN("OSC: invalid clipboard data: %s", base64_data);
+ else
+ LOG_ERRNO("base64_decode() failed");
+
+ if (to_clipboard)
+ selection_clipboard_unset(seat);
+ if (to_primary)
+ selection_primary_unset(seat);
+ return;
+ }
+
+ LOG_DBG("decoded: %s", decoded);
+
if (to_clipboard) {
char *copy = xstrdup(decoded);
if (!text_to_clipboard(seat, term, copy, seat->kbd.serial))
@@ -157,20 +162,6 @@ from_clipboard_done(void *user)
static void
osc_from_clipboard(struct terminal *term, const char *source)
{
- /* Use clipboard if no source has been specified */
- char src = source[0] == '\0' ? 'c' : 0;
-
- for (const char *s = source; *s != '\0'; s++) {
- if (*s == 'c' || *s == 'p' || *s == 's') {
- src = *s;
- break;
- } else
- LOG_WARN("unimplemented: clipboard source '%c'", *s);
- }
-
- if (src == 0)
- return;
-
/* Find a seat in which the terminal has focus */
struct seat *seat = NULL;
tll_foreach(term->wl->seats, it) {
@@ -185,6 +176,35 @@ osc_from_clipboard(struct terminal *term, const char *source)
return;
}
+ /* Use clipboard if no source has been specified */
+ char src = source[0] == '\0' ? 'c' : 0;
+ bool from_clipboard = src == 'c';
+ bool from_primary = false;
+
+ for (const char *s = source;
+ *s != '\0' && !from_clipboard && !from_primary;
+ s++)
+ {
+ if (*s == 'c' || *s == 'p' || *s == 's') {
+ src = *s;
+
+ switch (src) {
+ case 'c':
+ from_clipboard = selection_clipboard_has_data(seat);
+ break;
+
+ case 's':
+ case 'p':
+ from_primary = selection_primary_has_data(seat);
+ break;
+ }
+ } else
+ LOG_WARN("unimplemented: clipboard source '%c'", *s);
+ }
+
+ if (!from_clipboard && !from_primary)
+ return;
+
term_to_slave(term, "\033]52;", 5);
term_to_slave(term, &src, 1);
term_to_slave(term, ";", 1);
@@ -192,17 +212,14 @@ osc_from_clipboard(struct terminal *term, const char *source)
struct clip_context *ctx = xmalloc(sizeof(*ctx));
*ctx = (struct clip_context) {.seat = seat, .term = term};
- switch (src) {
- case 'c':
+ if (from_clipboard) {
text_from_clipboard(
seat, term, &from_clipboard_cb, &from_clipboard_done, ctx);
- break;
+ }
- case 's':
- case 'p':
+ if (from_primary) {
text_from_primary(
seat, term, &from_clipboard_cb, &from_clipboard_done, ctx);
- break;
}
}