diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-11-16 12:17:08 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-11-16 12:17:08 +0100 |
commit | 62a89c4bf63357fb95c34898a37ff663293a8dea (patch) | |
tree | 8ef3fc321a0461ab158c14669b1e647dfe55da69 /src | |
parent | eba962c3e353b3fd3f11e30a7d6f48585e3a153c (diff) |
Ensure window gets clipboard owner before setting clipboard string
Diffstat (limited to 'src')
-rw-r--r-- | src/window/window.c | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/src/window/window.c b/src/window/window.c index 5599c58..65a2509 100644 --- a/src/window/window.c +++ b/src/window/window.c @@ -624,7 +624,6 @@ static void mgl_window_on_receive_event(mgl_window *self, XEvent *xev, mgl_event } } - /* Repond to the client that wants our clipboard content */ selection_event.property = None; XSendEvent(context->connection, selection_event.requestor, True, NoEventMask, (XEvent*)&selection_event); event->type = MGL_EVENT_UNKNOWN; @@ -788,7 +787,15 @@ void mgl_window_set_clipboard(mgl_window *self, const char *str, size_t size) { self->clipboard_string = NULL; self->clipboard_size = 0; } - + + XSetSelectionOwner(context->connection, x11_context->clipboard_atom, self->window, CurrentTime); + + /* Check if setting the selection owner was successful */ + if(XGetSelectionOwner(context->connection, x11_context->clipboard_atom) != self->window) { + fprintf(stderr, "Error: mgl_window_set_clipboard failed\n"); + return; + } + self->clipboard_string = malloc(size + 1); if(!self->clipboard_string) { fprintf(stderr, "Error: failed to allocate string for clipboard\n"); @@ -797,12 +804,6 @@ void mgl_window_set_clipboard(mgl_window *self, const char *str, size_t size) { memcpy(self->clipboard_string, str, size); self->clipboard_string[size] = '\0'; self->clipboard_size = size; - - XSetSelectionOwner(context->connection, x11_context->clipboard_atom, self->window, CurrentTime); - - /* Check if setting the selection owner was successful */ - if(XGetSelectionOwner(context->connection, x11_context->clipboard_atom) != self->window) - fprintf(stderr, "Error: mgl_window_set_clipboard failed\n"); } /* TODO: Right now the returned str is free'd with free, but shouldn't it be free'd with free? */ @@ -839,7 +840,8 @@ bool mgl_window_get_clipboard(mgl_window *self, char **str, size_t *size) { if(!data) return false; - memcpy(data, self->clipboard_string, self->clipboard_size + 1); + memcpy(data, self->clipboard_string, self->clipboard_size); + data[self->clipboard_size] = '\0'; *str = data; *size = self->clipboard_size; return true; |