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 | |
parent | eba962c3e353b3fd3f11e30a7d6f48585e3a153c (diff) |
Ensure window gets clipboard owner before setting clipboard string
-rw-r--r-- | src/window/window.c | 20 | ||||
-rw-r--r-- | tests/main.c | 5 |
2 files changed, 14 insertions, 11 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; diff --git a/tests/main.c b/tests/main.c index a9aebec..cee3ec7 100644 --- a/tests/main.c +++ b/tests/main.c @@ -256,8 +256,9 @@ int main(int argc, char **argv) { } case MGL_EVENT_KEY_PRESSED: { if(event.key.code == MGL_KEY_C) { - mgl_window_set_clipboard(&window, "hello", 5); - fprintf(stderr, "Copied 'hello' to the clipboard\n"); + const char *str = "東京都, Tōkyō-to"; + mgl_window_set_clipboard(&window, str, strlen(str)); + fprintf(stderr, "Copied '%s' to the clipboard\n", str); } else if(event.key.code == MGL_KEY_V) { char *str; size_t size; |