From 6749053db02bef74b4fa97247d06bce085980138 Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Tue, 3 Mar 2020 22:57:21 -0800 Subject: [PATCH] Fix clipboard to work when catch all is set to false --- platform_linux/linux_4ed.cpp | 84 ++++++++++++++++++-------- platform_linux/linux_4ed_functions.cpp | 27 --------- 2 files changed, 60 insertions(+), 51 deletions(-) diff --git a/platform_linux/linux_4ed.cpp b/platform_linux/linux_4ed.cpp index 6cd4a6de..13e8f4bc 100644 --- a/platform_linux/linux_4ed.cpp +++ b/platform_linux/linux_4ed.cpp @@ -1218,6 +1218,32 @@ linux_clipboard_send(XSelectionRequestEvent* req) { XSendEvent(req->display, req->requestor, True, 0, (XEvent*)&rsp); } +internal String_Const_u8 +linux_clipboard_recv(Arena *arena){ + Atom type; + int fmt; + unsigned long nitems; + unsigned long bytes_left; + u8 *data; + + int result = XGetWindowProperty(linuxvars.dpy, + linuxvars.win, + linuxvars.atom_CLIPBOARD, + 0L, 0x20000000L, False, + linuxvars.atom_UTF8_STRING, + &type, &fmt, &nitems, + &bytes_left, &data); + + String_Const_u8 clip = {}; + if(result == Success && fmt == 8){ + clip= push_string_copy(arena, SCu8(data, nitems)); + XFree(data); + XDeleteProperty(linuxvars.dpy, linuxvars.win, linuxvars.atom_CLIPBOARD); + } + + return(clip); +} + internal void linux_clipboard_recv(XSelectionEvent* ev) { @@ -1227,31 +1253,42 @@ linux_clipboard_recv(XSelectionEvent* ev) { return; } - Atom type; - int fmt; - unsigned long nitems; - unsigned long bytes_left; - u8 *data; - - int result = XGetWindowProperty( - linuxvars.dpy, - linuxvars.win, - linuxvars.atom_CLIPBOARD, - 0L, 0x20000000L, False, - linuxvars.atom_UTF8_STRING, - &type, &fmt, &nitems, - &bytes_left, &data); - - if(result == Success && fmt == 8){ + Scratch_Block scratch(&linuxvars.tctx); + String_Const_u8 clip = linux_clipboard_recv(scratch); + if (clip.size > 0){ linalloc_clear(linuxvars.clipboard_arena); - linuxvars.clipboard_contents = push_u8_stringf(linuxvars.clipboard_arena, "%.*s", nitems, data); + linuxvars.clipboard_contents = push_string_copy(linuxvars.clipboard_arena, clip); linuxvars.received_new_clipboard = true; - XFree(data); - XDeleteProperty(linuxvars.dpy, linuxvars.win, linuxvars.atom_CLIPBOARD); linux_schedule_step(); } } +internal +system_get_clipboard_sig(){ + // TODO(inso): index? + return(push_string_copy(arena, linuxvars.clipboard_contents)); +} + +internal void +system_post_clipboard(String_Const_u8 str, i32 index){ + // TODO(inso): index? + //LINUX_FN_DEBUG("%.*s", string_expand(str)); + linalloc_clear(linuxvars.clipboard_arena); + linuxvars.clipboard_contents = push_u8_stringf(linuxvars.clipboard_arena, "%.*s", str.size, str.str); + XSetSelectionOwner(linuxvars.dpy, linuxvars.atom_CLIPBOARD, linuxvars.win, CurrentTime); +} + +internal void +system_set_clipboard_catch_all(b32 enabled){ + LINUX_FN_DEBUG("%d", enabled); + linuxvars.clipboard_catch_all = !!enabled; +} + +internal b32 +system_get_clipboard_catch_all(void){ + return linuxvars.clipboard_catch_all; +} + internal String_Const_u8 linux_filter_text(Arena* arena, u8* buf, int len) { u8* const result = push_array(arena, u8, len); @@ -1488,11 +1525,10 @@ linux_handle_x11_events() { default: { // clipboard update notification - ask for the new content - if (linuxvars.clipboard_catch_all && event.type == linuxvars.xfixes_selection_event) { + if (event.type == linuxvars.xfixes_selection_event) { XFixesSelectionNotifyEvent* sne = (XFixesSelectionNotifyEvent*)&event; if (sne->subtype == XFixesSelectionNotify && sne->owner != linuxvars.win){ - XConvertSelection( - linuxvars.dpy, + XConvertSelection(linuxvars.dpy, linuxvars.atom_CLIPBOARD, linuxvars.atom_UTF8_STRING, linuxvars.atom_CLIPBOARD, @@ -1768,10 +1804,10 @@ main(int argc, char **argv){ Application_Step_Input input = {}; - if (linuxvars.received_new_clipboard){ + if (linuxvars.received_new_clipboard && linuxvars.clipboard_catch_all){ input.clipboard = linuxvars.clipboard_contents; - linuxvars.received_new_clipboard = false; } + linuxvars.received_new_clipboard = false; input.first_step = first_step; input.dt = frame_useconds/1000000.f; // variable? diff --git a/platform_linux/linux_4ed_functions.cpp b/platform_linux/linux_4ed_functions.cpp index b3b0bed9..040d503f 100644 --- a/platform_linux/linux_4ed_functions.cpp +++ b/platform_linux/linux_4ed_functions.cpp @@ -335,33 +335,6 @@ system_sleep(u64 microseconds){ nanosleep(&requested, &remaining); } -internal String_Const_u8 -system_get_clipboard(Arena* arena, i32 index){ - // TODO(inso): index? - u8* ptr = push_array_write(arena, u8, linuxvars.clipboard_contents.size, linuxvars.clipboard_contents.str); - return SCu8(ptr, linuxvars.clipboard_contents.size); -} - -internal void -system_post_clipboard(String_Const_u8 str, i32 index){ - // TODO(inso): index? - //LINUX_FN_DEBUG("%.*s", string_expand(str)); - linalloc_clear(linuxvars.clipboard_arena); - linuxvars.clipboard_contents = push_u8_stringf(linuxvars.clipboard_arena, "%.*s", str.size, str.str); - XSetSelectionOwner(linuxvars.dpy, linuxvars.atom_CLIPBOARD, linuxvars.win, CurrentTime); -} - -internal void -system_set_clipboard_catch_all(b32 enabled){ - LINUX_FN_DEBUG("%d", enabled); - linuxvars.clipboard_catch_all = !!enabled; -} - -internal b32 -system_get_clipboard_catch_all(void){ - return linuxvars.clipboard_catch_all; -} - internal b32 system_cli_call(Arena* scratch, char* path, char* script, CLI_Handles* cli_out){ LINUX_FN_DEBUG("%s / %s", path, script);