summaryrefslogtreecommitdiff
path: root/src/platform/x11
diff options
context:
space:
mode:
Diffstat (limited to 'src/platform/x11')
-rw-r--r--src/platform/x11/window.c504
1 files changed, 440 insertions, 64 deletions
diff --git a/src/platform/x11/window.c b/src/platform/x11/window.c
index 765d83c..004d56c 100644
--- a/src/platform/x11/window.c
+++ b/src/platform/x11/window.c
@@ -32,8 +32,10 @@
#include <stdlib.h>
#include <string.h>
#include <strings.h>
+#include <wchar.h>
#define TEXT_LIMIT (16 * 1024 * 1024)
+#define PASTE_TIMEOUT_NS YASL_MS_TO_NS(30000)
#ifndef likely
#define likely(x) __builtin_expect(!!(x), 1)
@@ -72,8 +74,12 @@ struct x11_surface {
struct paste_request {
struct paste_request *next;
+ struct x11_display *display;
struct x11_surface *target;
Window window;
+ Atom selection;
+ uint64_t id;
+ yasl_timer_id timeout;
};
struct tablet_axis {
@@ -111,6 +117,7 @@ struct x11_display {
bool xi2;
bool has_shm;
bool has_xkb;
+ bool lost;
int xkb_event;
enum yasl_key keys[256];
Cursor cursors[16];
@@ -150,6 +157,7 @@ static bool place(struct yasl_surface *base, struct yasl_vector2i at);
static void drag_end(struct x11_display *d, enum yasl_drag_end how);
static void drag_cancel(struct yasl_display *base);
static void schedule_redraw(struct yasl_surface *base);
+static uint64_t paste_timeout(void *ud, yasl_timer_id id, uint64_t interval_ns);
static void leave(struct x11_display *d)
{
@@ -188,23 +196,23 @@ static void drag_set_hover(struct x11_display *d, struct x11_surface *s, struct
struct x11_surface *old = d->drag.hover;
struct yasl_vector2i old_pos = d->drag.hover_pos;
d->drag.hover = NULL;
- if (!old->destroyed && old->cbs && old->cbs->drag)
- old->cbs->drag(&old->base, YASL_DRAG_LEAVE, old_pos, old->ud);
- if (!d->drag.active) return;
+ if (!old->destroyed)
+ yasl_surface_notify_drag(&old->base, YASL_DRAG_LEAVE, old_pos);
+ if (!d->drag.active || (s && s->destroyed)) return;
}
if (s && s->destroyed) return;
d->drag.hover = s;
d->drag.hover_pos = pos;
- if (s && s->cbs && s->cbs->drag)
- s->cbs->drag(&s->base, YASL_DRAG_OVER, pos, s->ud);
+ if (s && !s->destroyed)
+ yasl_surface_notify_drag(&s->base, YASL_DRAG_OVER, pos);
}
static void drag_leave_hover(struct x11_display *d, struct x11_surface *s, struct yasl_vector2i pos)
{
if (!d->drag.active || d->drag.hover != s) return;
d->drag.hover = NULL;
- if (s && !s->destroyed && s->cbs && s->cbs->drag)
- s->cbs->drag(&s->base, YASL_DRAG_LEAVE, pos, s->ud);
+ if (s && !s->destroyed)
+ yasl_surface_notify_drag(&s->base, YASL_DRAG_LEAVE, pos);
}
static void drag_drop_hover(struct x11_display *d)
@@ -559,19 +567,41 @@ static void cancel_pastes(struct x11_display *d, struct x11_surface *target)
{
for (struct paste_request **p = &d->pastes; *p;) {
struct paste_request *r = *p;
- if (!target) {
+ if (target && r->target == target) {
+ /* Keep the requestor window alive until the selection owner replies.
+ * Some INCR owners write to it after the original surface is gone. */
+ r->target = NULL;
+ r->id = 0;
+ p = &r->next;
+ } else if (!target) {
*p = r->next;
+ if (r->timeout) yasl_loop_del_timer(d->loop, r->timeout);
XDestroyWindow(d->native, r->window);
free(r);
} else {
- /* Keep the requestor alive until SelectionNotify: the owner may
- * still be writing to its property. Never retain a dead target. */
- if (r->target == target) r->target = NULL;
p = &r->next;
}
}
}
+static uint64_t paste_timeout(void *ud, yasl_timer_id id, uint64_t interval_ns)
+{
+ (void)id;
+ (void)interval_ns;
+ struct paste_request *r = ud;
+ struct x11_surface *s = r->target;
+ struct x11_display *d = r->display;
+ struct paste_request **p = &d->pastes;
+ while (*p && *p != r) p = &(*p)->next;
+ if (*p) *p = r->next;
+ r->timeout = 0; /* The loop retires this one-shot timer after the callback. */
+ XDestroyWindow(d->native, r->window);
+ if (s && !s->destroyed)
+ yasl_surface_complete_paste(&s->base, r->id, YASL_TRANSFER_FAILED, NULL, 0);
+ free(r);
+ return 0;
+}
+
static void surface_destroy(struct yasl_surface *base)
{
struct x11_surface *s = (struct x11_surface *)base;
@@ -628,6 +658,7 @@ static void set_callbacks(struct yasl_surface *base,
if (s->destroyed) return;
s->base.callbacks = cbs ? *cbs : (struct yasl_window_cbs){0};
s->cbs = &s->base.callbacks;
+ s->base.ud = ud;
s->ud = ud;
}
@@ -641,7 +672,7 @@ static void set_title(struct yasl_surface *base, const char *title)
PropModeReplace, (const unsigned char *)title, (int)strlen(title));
}
-static void net_message(struct x11_surface *s, Atom type,
+static bool net_message(struct x11_surface *s, Atom type,
long a, long b, long c, long e, long f)
{
struct x11_display *d = (struct x11_display *)s->base.display;
@@ -650,8 +681,246 @@ static void net_message(struct x11_surface *s, Atom type,
.type = ClientMessage, .display = d->native, .window = s->window,
.message_type = type, .format = 32, .data.l = {a, b, c, e, f},
};
- XSendEvent(d->native, RootWindow(d->native, d->screen), False,
- SubstructureRedirectMask | SubstructureNotifyMask, &event);
+ return XSendEvent(d->native, RootWindow(d->native, d->screen), False,
+ SubstructureRedirectMask | SubstructureNotifyMask, &event) != 0;
+}
+
+static bool surface_set_size(struct yasl_surface *base, struct yasl_vector2i size)
+{
+ struct x11_surface *s = (struct x11_surface *)base;
+ struct x11_display *d = (struct x11_display *)base->display;
+ if (s->destroyed || s->is_popup || size.x <= 0 || size.y <= 0 ||
+ size.x > 32767 || size.y > 32767) {
+ errno = EINVAL;
+ return false;
+ }
+ if (!d->native || !s->window) { errno = ENODEV; return false; }
+ XResizeWindow(d->native, s->window, (unsigned)size.x, (unsigned)size.y);
+ XFlush(d->native);
+ return true;
+}
+
+static bool surface_set_fullscreen(struct yasl_surface *base, bool enabled)
+{
+ struct x11_surface *s = (struct x11_surface *)base;
+ struct x11_display *d = (struct x11_display *)base->display;
+ if (s->destroyed || s->is_popup) { errno = EINVAL; return false; }
+ if (!d->native || !s->window) { errno = ENODEV; return false; }
+ if (!net_message(s, d->net_state, enabled ? 1 : 0, d->fullscreen, 0, 1, 0)) {
+ errno = ENOTSUP;
+ return false;
+ }
+ XFlush(d->native);
+ return true;
+}
+
+static bool surface_set_maximized(struct yasl_surface *base, bool enabled)
+{
+ struct x11_surface *s = (struct x11_surface *)base;
+ struct x11_display *d = (struct x11_display *)base->display;
+ if (s->destroyed || s->is_popup) { errno = EINVAL; return false; }
+ if (!d->native || !s->window) { errno = ENODEV; return false; }
+ long action = enabled ? 1 : 0;
+ if (!net_message(s, d->net_state, action, d->maximized_v, d->maximized_h, 1, 0)) {
+ errno = ENOTSUP;
+ return false;
+ }
+ XFlush(d->native);
+ return true;
+}
+
+static bool surface_minimize(struct yasl_surface *base)
+{
+ struct x11_surface *s = (struct x11_surface *)base;
+ struct x11_display *d = (struct x11_display *)base->display;
+ if (s->destroyed || s->is_popup) { errno = EINVAL; return false; }
+ if (!d->native || !s->window) { errno = ENODEV; return false; }
+ if (!XIconifyWindow(d->native, s->window, d->screen)) { errno = ENOTSUP; return false; }
+ XFlush(d->native);
+ return true;
+}
+
+static size_t utf8_offset_for_chars(const char *text, size_t len, int chars)
+{
+ size_t at = 0;
+ while (at < len && chars-- > 0) {
+ unsigned char c = (unsigned char)text[at];
+ size_t step = c < 0x80 ? 1 : (c < 0xe0 ? 2 : (c < 0xf0 ? 3 : 4));
+ if (step > len - at) step = 1;
+ at += step;
+ }
+ return at;
+}
+
+static size_t utf8_encode(char *out, uint32_t cp)
+{
+ if (!cp || cp > 0x10ffff || (cp >= 0xd800 && cp <= 0xdfff)) cp = 0xfffd;
+ if (cp < 0x80) { out[0] = (char)cp; return 1; }
+ if (cp < 0x800) {
+ out[0] = (char)(0xc0 | (cp >> 6));
+ out[1] = (char)(0x80 | (cp & 0x3f));
+ return 2;
+ }
+ if (cp < 0x10000) {
+ out[0] = (char)(0xe0 | (cp >> 12));
+ out[1] = (char)(0x80 | ((cp >> 6) & 0x3f));
+ out[2] = (char)(0x80 | (cp & 0x3f));
+ return 3;
+ }
+ out[0] = (char)(0xf0 | (cp >> 18));
+ out[1] = (char)(0x80 | ((cp >> 12) & 0x3f));
+ out[2] = (char)(0x80 | ((cp >> 6) & 0x3f));
+ out[3] = (char)(0x80 | (cp & 0x3f));
+ return 4;
+}
+
+static char *xim_text_dup(const XIMText *text, size_t *len)
+{
+ *len = 0;
+ if (!text) return NULL;
+ const void *source = text->encoding_is_wchar
+ ? (const void *)text->string.wide_char
+ : (const void *)text->string.multi_byte;
+ if (!source) return NULL;
+ /* XIMText.length is an unsigned short, so four bytes per scalar cannot
+ * overflow size_t on the supported X11 targets. */
+ size_t cap = (size_t)text->length * 4 + 1;
+ char *out = malloc(cap);
+ if (!out) return NULL;
+ size_t used = 0;
+ if (text->encoding_is_wchar) {
+ const wchar_t *src = text->string.wide_char;
+ for (unsigned i = 0; i < text->length; i++) {
+ uint32_t cp = (uint32_t)src[i];
+ used += utf8_encode(out + used, cp);
+ }
+ } else {
+ const char *src = text->string.multi_byte;
+ mbstate_t state = {0};
+ size_t consumed_bytes = 0;
+ size_t max_bytes = (size_t)text->length * MB_LEN_MAX;
+ unsigned converted = 0;
+ while (converted < text->length && consumed_bytes < max_bytes) {
+ wchar_t wc = 0;
+ size_t result = mbrtowc(&wc, src + consumed_bytes, 1, &state);
+ if (result == (size_t)-1) { free(out); return NULL; }
+ consumed_bytes++;
+ if (result == (size_t)-2) continue;
+ if (result == 0) {
+ /* The public UTF-8 event payload cannot contain an embedded NUL. */
+ wc = (wchar_t)0xfffd;
+ state = (mbstate_t){0};
+ }
+ used += utf8_encode(out + used, (uint32_t)wc);
+ converted++;
+ }
+ if (converted != text->length) { free(out); return NULL; }
+ }
+ out[used] = '\0';
+ if (!yasl_utf8_validate(out, used)) { free(out); return NULL; }
+ *len = used;
+ return out;
+}
+
+static void xim_preedit_done(XIM im, XPointer client_data, XPointer call_data)
+{
+ (void)im;
+ (void)call_data;
+ struct x11_surface *s = (struct x11_surface *)client_data;
+ if (s->destroyed) return;
+ free(s->ime_preedit);
+ s->ime_preedit = NULL;
+ s->ime_cursor_begin = s->ime_cursor_end = -1;
+ yasl_surface_notify_composition(&s->base, NULL, -1, -1);
+}
+
+static int xim_preedit_start(XIM im, XPointer client_data, XPointer call_data)
+{
+ (void)im;
+ (void)client_data;
+ (void)call_data;
+ return -1; /* Accept preedit strings of any length. */
+}
+
+static void xim_preedit_draw(XIM im, XPointer client_data, XPointer call_data)
+{
+ (void)im;
+ struct x11_surface *s = (struct x11_surface *)client_data;
+ XIMPreeditDrawCallbackStruct *draw = (XIMPreeditDrawCallbackStruct *)call_data;
+ if (s->destroyed || !draw || draw->chg_first < 0 || draw->chg_length < 0) return;
+ size_t old_len = s->ime_preedit ? strlen(s->ime_preedit) : 0;
+ size_t new_len = old_len;
+ bool has_string = draw->text && (draw->text->encoding_is_wchar
+ ? draw->text->string.wide_char != NULL
+ : draw->text->string.multi_byte != NULL);
+ if (has_string) {
+ const char *old_text = s->ime_preedit ? s->ime_preedit : "";
+ size_t first = utf8_offset_for_chars(old_text, old_len, draw->chg_first);
+ size_t after = first + utf8_offset_for_chars(old_text + first,
+ old_len - first, draw->chg_length);
+ size_t insert_len = 0;
+ char *insert = xim_text_dup(draw->text, &insert_len);
+ if (!insert) return;
+ if (insert_len > SIZE_MAX - (old_len - (after - first)) - 1) {
+ free(insert);
+ return;
+ }
+ new_len = old_len - (after - first) + insert_len;
+ char *updated = malloc(new_len + 1);
+ if (!updated) { free(insert); return; }
+ if (first) memcpy(updated, old_text, first);
+ if (insert_len) memcpy(updated + first, insert, insert_len);
+ if (old_len > after) memcpy(updated + first + insert_len, old_text + after,
+ old_len - after);
+ updated[new_len] = '\0';
+ free(insert);
+ free(s->ime_preedit);
+ s->ime_preedit = new_len ? updated : (free(updated), NULL);
+ }
+ int caret = draw->caret < 0 ? -1 : (int)utf8_offset_for_chars(
+ s->ime_preedit ? s->ime_preedit : "", new_len, draw->caret);
+ s->ime_cursor_begin = s->ime_cursor_end = caret;
+ yasl_surface_notify_composition(&s->base, s->ime_preedit, caret, caret);
+}
+
+static void xim_preedit_caret(XIM im, XPointer client_data, XPointer call_data)
+{
+ (void)im;
+ struct x11_surface *s = (struct x11_surface *)client_data;
+ XIMPreeditCaretCallbackStruct *caret = (XIMPreeditCaretCallbackStruct *)call_data;
+ if (s->destroyed || !caret || !s->ime_preedit) return;
+ int offset = caret->position < 0 ? -1 : (int)utf8_offset_for_chars(
+ s->ime_preedit, strlen(s->ime_preedit), caret->position);
+ s->ime_cursor_begin = s->ime_cursor_end = offset;
+ yasl_surface_notify_composition(&s->base, s->ime_preedit, offset, offset);
+}
+
+static XIC create_ic(struct x11_display *d, struct x11_surface *s)
+{
+ if (!d->im) return NULL;
+ union {
+ int (*start)(XIM, XPointer, XPointer);
+ XIMProc proc;
+ } start_proc = {.start = xim_preedit_start};
+ XIMCallback start = {.client_data = (XPointer)s,
+ .callback = start_proc.proc};
+ XIMCallback done = {.client_data = (XPointer)s, .callback = xim_preedit_done};
+ XIMCallback draw = {.client_data = (XPointer)s, .callback = xim_preedit_draw};
+ XIMCallback caret = {.client_data = (XPointer)s, .callback = xim_preedit_caret};
+ XVaNestedList preedit = XVaCreateNestedList(0,
+ XNPreeditStartCallback, &start,
+ XNPreeditDoneCallback, &done,
+ XNPreeditDrawCallback, &draw,
+ XNPreeditCaretCallback, &caret, NULL);
+ XIC ic = preedit ? XCreateIC(d->im,
+ XNInputStyle, XIMPreeditCallbacks | XIMStatusNothing,
+ XNPreeditAttributes, preedit,
+ XNClientWindow, s->window, XNFocusWindow, s->window, NULL) : NULL;
+ if (preedit) XFree(preedit);
+ if (!ic)
+ ic = XCreateIC(d->im, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
+ XNClientWindow, s->window, XNFocusWindow, s->window, NULL);
+ return ic;
}
static struct yasl_surface *create_toplevel(struct yasl_display *base,
@@ -691,10 +960,7 @@ static struct yasl_surface *create_toplevel(struct yasl_display *base,
Atom xdnd_version = 5;
XChangeProperty(d->native, s->window, d->xdnd_aware, XA_ATOM, 32,
PropModeReplace, (unsigned char *)&xdnd_version, 1);
- if (d->im) {
- s->ic = XCreateIC(d->im, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
- XNClientWindow, s->window, XNFocusWindow, s->window, NULL);
- }
+ s->ic = create_ic(d, s);
s->next = d->surfaces;
d->surfaces = s;
set_title(&s->base, spec->title ? spec->title : "YASL");
@@ -851,10 +1117,7 @@ static struct yasl_surface *surface_create_popup(struct yasl_surface *parent_bas
Atom xdnd_version = 5;
XChangeProperty(d->native, s->window, d->xdnd_aware, XA_ATOM, 32,
PropModeReplace, (unsigned char *)&xdnd_version, 1);
- if (d->im) {
- s->ic = XCreateIC(d->im, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
- XNClientWindow, s->window, XNFocusWindow, s->window, NULL);
- }
+ s->ic = create_ic(d, s);
XMapRaised(d->native, s->window);
XFlush(d->native);
}
@@ -909,7 +1172,7 @@ static void update_state(struct x11_surface *s)
if (data) XFree(data);
if (state == s->base.state) return;
s->base.state = state;
- if (s->cbs && s->cbs->state) s->cbs->state(&s->base, state, s->ud);
+ yasl_surface_notify_state(&s->base, state);
}
static void selection_request(struct x11_display *d, XSelectionRequestEvent *request)
@@ -1036,13 +1299,48 @@ static void selection_notify(struct x11_display *d, XSelectionEvent *event)
while (*p && (*p)->window != event->requestor) p = &(*p)->next;
if (!*p) return;
struct paste_request *r = *p;
- *p = r->next;
struct x11_surface *s = r->target;
- if (!s || event->property == None) {
+ if (!s) {
+ /* Surface destruction detached this accepted request. The reply means
+ * the owner has finished writing the initial property; retire safely. */
+ *p = r->next;
+ if (r->timeout) yasl_loop_del_timer(d->loop, r->timeout);
XDestroyWindow(d->native, r->window);
free(r);
return;
}
+ if (event->property == None) {
+ *p = r->next;
+ if (r->timeout) yasl_loop_del_timer(d->loop, r->timeout);
+ enum yasl_transfer_status status =
+ XGetSelectionOwner(d->native, r->selection) == None
+ ? YASL_TRANSFER_UNAVAILABLE : YASL_TRANSFER_FAILED;
+ XDestroyWindow(d->native, r->window);
+ if (s && !s->destroyed)
+ yasl_surface_complete_paste(&s->base, r->id, status, NULL, 0);
+ free(r);
+ return;
+ }
+ /* Probe without deleting first. Deleting an INCR marker acknowledges it
+ * and invites the owner to write more properties; this backend rejects
+ * streaming, so leave it intact and retain a tombstone until timeout. */
+ Atom probe_type = None;
+ int probe_format = 0;
+ unsigned long probe_count = 0, probe_left = 0;
+ unsigned char *probe_data = NULL;
+ int probe_result = XGetWindowProperty(d->native, r->window, event->property,
+ 0, 0, False, AnyPropertyType, &probe_type, &probe_format,
+ &probe_count, &probe_left, &probe_data);
+ if (probe_data) XFree(probe_data);
+ if (probe_result == Success && probe_type == d->incr) {
+ yasl_surface_complete_paste(&s->base, r->id,
+ YASL_TRANSFER_FAILED, NULL, 0);
+ r->target = NULL;
+ r->id = 0;
+ return;
+ }
+ *p = r->next;
+ if (r->timeout) yasl_loop_del_timer(d->loop, r->timeout);
Atom actual = None;
int format = 0;
unsigned long count = 0, left = 0;
@@ -1070,7 +1368,12 @@ static void selection_notify(struct x11_display *d, XSelectionEvent *event)
if (data) { XFree(data); data = NULL; }
break;
}
- char *nb = realloc(buf, total + count + 1);
+ if (count > TEXT_LIMIT - total) {
+ XFree(data);
+ data = NULL;
+ break;
+ }
+ char *nb = realloc(buf, total + (size_t)count + 1);
if (!nb) {
XFree(data);
break;
@@ -1079,7 +1382,7 @@ static void selection_notify(struct x11_display *d, XSelectionEvent *event)
if (count) memcpy(buf + total, data, count);
total += count;
buf[total] = '\0';
- offset += count / 4;
+ offset += (count + 3) / 4;
XFree(data);
data = NULL;
if (left == 0) { complete = true; break; }
@@ -1091,8 +1394,12 @@ static void selection_notify(struct x11_display *d, XSelectionEvent *event)
buf = utf8;
if (buf) total = utf8_len;
}
+ enum yasl_transfer_status status = YASL_TRANSFER_FAILED;
if (complete && buf && yasl_utf8_validate(buf, total))
- push(s, (struct yasl_event){.type = YASL_EVENT_PASTE}, buf, total);
+ status = YASL_TRANSFER_OK;
+ yasl_surface_complete_paste(&s->base, r->id, status,
+ status == YASL_TRANSFER_OK ? buf : NULL,
+ status == YASL_TRANSFER_OK ? total : 0);
free(buf);
XDestroyWindow(d->native, r->window);
free(r);
@@ -1172,8 +1479,8 @@ static void handle_client_message(struct x11_display *d, struct x11_surface *s,
const XClientMessageEvent *ev)
{
if (ev->message_type == d->protocols &&
- (Atom)ev->data.l[0] == d->delete_window && s->cbs && s->cbs->close) {
- s->cbs->close(&s->base, s->ud);
+ (Atom)ev->data.l[0] == d->delete_window) {
+ yasl_surface_notify_close(&s->base);
} else if (ev->message_type == d->xdnd_enter) {
Window source = (Window)ev->data.l[0];
int version = (int)((unsigned long)ev->data.l[1] >> 24);
@@ -1204,25 +1511,29 @@ static void handle_client_message(struct x11_display *d, struct x11_surface *s,
d->xdnd.accepts = has_uri;
} else if (ev->message_type == d->xdnd_position) {
Window source = (Window)ev->data.l[0];
+ Window target = s->window;
int rx = (int)(int16_t)((ev->data.l[2] >> 16) & 0xFFFF);
int ry = (int)(int16_t)(ev->data.l[2] & 0xFFFF);
Time t = (Time)ev->data.l[3];
int wx = rx, wy = ry;
if (d->native) {
Window child;
- XTranslateCoordinates(d->native, RootWindow(d->native, d->screen), s->window,
+ XTranslateCoordinates(d->native, RootWindow(d->native, d->screen), target,
rx, ry, &wx, &wy, &child);
}
struct yasl_vector2i pos = {wx, wy};
if (d->xdnd.surface && d->xdnd.surface != s) {
- if (d->xdnd.accepts && d->xdnd.surface->cbs && d->xdnd.surface->cbs->drop)
- d->xdnd.surface->cbs->drop(&d->xdnd.surface->base, YASL_DROP_LEAVE, d->xdnd.pos, d->xdnd.surface->ud);
+ struct x11_surface *old = d->xdnd.surface;
+ struct yasl_vector2i old_pos = d->xdnd.pos;
+ d->xdnd.surface = NULL;
+ if (d->xdnd.accepts && !old->destroyed)
+ yasl_surface_notify_drop(&old->base, YASL_DROP_LEAVE, old_pos);
}
d->xdnd.surface = s;
d->xdnd.pos = pos;
d->xdnd.time = t;
- if (d->xdnd.accepts && s->cbs && s->cbs->drop)
- s->cbs->drop(&s->base, YASL_DROP_OVER, pos, s->ud);
+ if (d->xdnd.accepts && !s->destroyed)
+ yasl_surface_notify_drop(&s->base, YASL_DROP_OVER, pos);
if (d->native) {
XEvent reply = {0};
@@ -1233,37 +1544,38 @@ static void handle_client_message(struct x11_display *d, struct x11_surface *s,
.message_type = d->xdnd_status,
.format = 32,
.data.l = {
- s->window,
- d->xdnd.accepts ? 1 : 0,
+ target,
+ d->xdnd.accepts && !s->destroyed ? 1 : 0,
0,
0,
- d->xdnd.accepts ? d->xdnd_action_copy : None,
+ d->xdnd.accepts && !s->destroyed ? d->xdnd_action_copy : None,
},
};
XSendEvent(d->native, source, False, NoEventMask, &reply);
}
} else if (ev->message_type == d->xdnd_leave) {
if (d->xdnd.surface == s) {
- if (d->xdnd.accepts && s->cbs && s->cbs->drop)
- s->cbs->drop(&s->base, YASL_DROP_LEAVE, d->xdnd.pos, s->ud);
+ if (d->xdnd.accepts && !s->destroyed)
+ yasl_surface_notify_drop(&s->base, YASL_DROP_LEAVE, d->xdnd.pos);
}
d->xdnd = (typeof(d->xdnd)){0};
} else if (ev->message_type == d->xdnd_drop) {
Window source = (Window)ev->data.l[0];
+ Window target = s->window;
Time drop_time = (Time)ev->data.l[2];
bool was_accepting = d->xdnd.accepts && d->xdnd.has_uri_list && (d->xdnd.source == source);
if (d->xdnd.surface == s) {
- if (was_accepting && s->cbs && s->cbs->drop)
- s->cbs->drop(&s->base, YASL_DROP_LEAVE, d->xdnd.pos, s->ud);
+ if (was_accepting && !s->destroyed)
+ yasl_surface_notify_drop(&s->base, YASL_DROP_LEAVE, d->xdnd.pos);
}
- if (was_accepting) {
+ if (was_accepting && !s->destroyed) {
d->pending_drop.active = true;
d->pending_drop.source = source;
d->pending_drop.surface = s;
d->pending_drop.time = drop_time;
if (d->native) {
XConvertSelection(d->native, d->xdnd_selection, d->uri_list,
- d->paste_property, s->window, drop_time);
+ d->paste_property, target, drop_time);
}
} else if (d->native) {
XEvent reply = {0};
@@ -1273,7 +1585,7 @@ static void handle_client_message(struct x11_display *d, struct x11_surface *s,
.window = source,
.message_type = d->xdnd_finished,
.format = 32,
- .data.l = { s->window, 0, None },
+ .data.l = { target, 0, None },
};
XSendEvent(d->native, source, False, NoEventMask, &reply);
}
@@ -1326,8 +1638,9 @@ static void dispatch(struct yasl_display *base)
case ConfigureNotify: {
struct yasl_vector2i size = {event.xconfigure.width, event.xconfigure.height};
if (size.x == s->size.x && size.y == s->size.y) break;
- if (resize(s, size) && s->cbs && s->cbs->configure)
- s->cbs->configure(&s->base, (struct yasl_geometry){size, YASL_SCALE_UNIT}, s->ud);
+ if (resize(s, size))
+ yasl_surface_notify_configure(&s->base,
+ (struct yasl_geometry){size, YASL_SCALE_UNIT});
break;
}
case ClientMessage:
@@ -1340,7 +1653,7 @@ static void dispatch(struct yasl_display *base)
else s->base.state &= ~YASL_STATE_ACTIVATED;
push(s, (struct yasl_event){
.type = YASL_EVENT_FOCUS, .focus.focused = focused}, NULL, 0);
- if (s->cbs && s->cbs->state) s->cbs->state(&s->base, s->base.state, s->ud);
+ yasl_surface_notify_state(&s->base, s->base.state);
break;
}
case PropertyNotify:
@@ -1514,8 +1827,8 @@ static void flush(struct yasl_display *base)
.target = {.generation = yasl_frame_begin(&s->base),
.pixels = s->pixels, .size = s->size},
};
- s->cbs->draw(&s->base, &s->frame, s->ud);
- s->drawing = false;
+ yasl_surface_draw(&s->base, &s->frame);
+ if (!s->destroyed) s->drawing = false;
}
XFlush(d->native);
leave(d);
@@ -1525,7 +1838,13 @@ static int ready(int fd, short events, void *ud)
{
(void)fd;
struct yasl_display *base = ud;
- if (events & (POLLIN | POLLERR | POLLHUP)) {
+ struct x11_display *d = (struct x11_display *)base;
+ if ((events & (POLLERR | POLLHUP)) && !d->lost) {
+ d->lost = true;
+ yasl_display_notify_event(base, YASL_EVENT_DISPLAY_LOST);
+ return 0;
+ }
+ if ((events & POLLIN) && !d->lost) {
dispatch(base);
flush(base);
}
@@ -1535,7 +1854,7 @@ static int ready(int fd, short events, void *ud)
static int dispatch_timeout(struct yasl_display *base, int timeout_ms)
{
struct x11_display *d = (struct x11_display *)base;
- if (unlikely(!d || !d->native)) return -1;
+ if (unlikely(!d || !d->native || d->lost)) return -1;
dispatch(base);
for (struct x11_surface *s = d->surfaces; s; s = s->next) {
if (s->want_draw || s->input.count || s->input.reset_pending) {
@@ -1692,46 +2011,96 @@ static uint32_t caps(const struct yasl_display *d)
(((const struct x11_display *)d)->xi2 ? YASL_CAP_TABLET : 0);
}
-static void copy(struct yasl_display *base, enum yasl_selection sel, const char *text)
+static bool copy_text(struct yasl_display *base, enum yasl_selection sel, const char *text)
{
- if (!text || (sel != YASL_CLIPBOARD && sel != YASL_PRIMARY)) return;
+ if (!text || (sel != YASL_CLIPBOARD && sel != YASL_PRIMARY)) {
+ errno = EINVAL;
+ return false;
+ }
struct x11_display *d = (struct x11_display *)base;
char *value = strdup(text);
- if (!value) return;
+ if (!value) { errno = ENOMEM; return false; }
free(d->selection[sel]);
d->selection[sel] = value;
XSetSelectionOwner(d->native, sel == YASL_PRIMARY ? XA_PRIMARY : d->clipboard,
d->selection_window, CurrentTime);
XFlush(d->native);
+ return true;
}
-static void paste(struct yasl_display *base, enum yasl_selection sel, struct yasl_surface *target)
+static void copy(struct yasl_display *base, enum yasl_selection sel, const char *text)
{
- if (sel != YASL_CLIPBOARD && sel != YASL_PRIMARY) return;
+ (void)copy_text(base, sel, text);
+}
+
+static bool request_paste(struct yasl_display *base, enum yasl_selection sel,
+ struct yasl_surface *target, uint64_t id)
+{
+ if (!target || (sel != YASL_CLIPBOARD && sel != YASL_PRIMARY)) {
+ errno = EINVAL;
+ return false;
+ }
struct x11_display *d = (struct x11_display *)base;
struct x11_surface *s = (struct x11_surface *)target;
+ if (s->destroyed || target->display != base) { errno = EINVAL; return false; }
Atom selection = sel == YASL_PRIMARY ? XA_PRIMARY : d->clipboard;
- if (XGetSelectionOwner(d->native, selection) == d->selection_window) {
+ Window owner = XGetSelectionOwner(d->native, selection);
+ if (owner == None) {
+ yasl_surface_complete_paste(target, id, YASL_TRANSFER_UNAVAILABLE, NULL, 0);
+ return true;
+ }
+ if (owner == d->selection_window) {
const char *text = d->selection[sel];
- if (text) push(s, (struct yasl_event){.type = YASL_EVENT_PASTE}, text, strlen(text));
+ if (text)
+ yasl_surface_complete_paste(target, id, YASL_TRANSFER_OK, text, strlen(text));
+ else
+ yasl_surface_complete_paste(target, id, YASL_TRANSFER_UNAVAILABLE, NULL, 0);
+ return true;
} else {
struct paste_request *r = calloc(1, sizeof(*r));
- if (!r) return;
+ if (!r) { errno = ENOMEM; return false; }
r->window = XCreateSimpleWindow(d->native, RootWindow(d->native, d->screen),
0, 0, 1, 1, 0, 0, 0);
+ if (!r->window) { free(r); errno = EIO; return false; }
+ r->display = d;
r->target = s;
+ r->selection = selection;
+ r->id = id;
+ errno = 0;
+ r->timeout = yasl_loop_add_timer(d->loop, PASTE_TIMEOUT_NS,
+ paste_timeout, r);
+ if (!r->timeout) {
+ XDestroyWindow(d->native, r->window);
+ free(r);
+ if (!errno) errno = ENOMEM;
+ return false;
+ }
r->next = d->pastes;
d->pastes = r;
XConvertSelection(d->native, selection, d->utf8, d->paste_property,
r->window, CurrentTime);
XFlush(d->native);
+ return true;
}
}
+static void paste(struct yasl_display *base, enum yasl_selection sel,
+ struct yasl_surface *target)
+{
+ (void)request_paste(base, sel, target, 0);
+}
+
static bool input_poll(struct yasl_surface *base, struct yasl_event *event)
{
struct x11_surface *s = (struct x11_surface *)base;
- return !s->destroyed && input_queue_pop(&s->input, event);
+ if (s->destroyed) return false;
+ if (base->display->events) return yasl_events_poll_surface(base, event);
+ return input_queue_pop(&s->input, event);
+}
+
+static struct input_queue *surface_input_queue(struct yasl_surface *base)
+{
+ return &((struct x11_surface *)base)->input;
}
static void schedule_redraw(struct yasl_surface *base)
@@ -1929,11 +2298,11 @@ static void drag_end(struct x11_display *d, enum yasl_drag_end how)
d->drag.icon_pixmap = None;
}
- if (hover && !hover->destroyed && hover->cbs && hover->cbs->drag) {
+ if (hover && !hover->destroyed) {
if (how == YASL_DRAG_DROPPED)
- hover->cbs->drag(&hover->base, YASL_DRAG_DROP, hover_pos, hover->ud);
+ yasl_surface_notify_drag(&hover->base, YASL_DRAG_DROP, hover_pos);
else
- hover->cbs->drag(&hover->base, YASL_DRAG_LEAVE, hover_pos, hover->ud);
+ yasl_surface_notify_drag(&hover->base, YASL_DRAG_LEAVE, hover_pos);
}
if (l.end)
@@ -2130,12 +2499,19 @@ const struct yasl_backend_ops yasl_x11_backend = {
.display_modifiers = mods,
.display_copy = copy,
.display_paste = paste,
+ .display_copy_text = copy_text,
+ .display_request_paste = request_paste,
.display_drag_start = drag_start,
.display_drag_cancel = drag_cancel,
.surface_create_toplevel = create_toplevel,
.surface_create_popup = surface_create_popup,
.surface_popup_reposition = surface_popup_reposition,
.surface_destroy = surface_destroy,
+ .surface_input_queue = surface_input_queue,
+ .surface_set_size = surface_set_size,
+ .surface_set_fullscreen = surface_set_fullscreen,
+ .surface_set_maximized = surface_set_maximized,
+ .surface_minimize = surface_minimize,
.surface_display = surface_display,
.surface_set_callbacks = set_callbacks,
.surface_set_title = set_title,