Fixed bug 1599 - On X11, SDL2 should use CLIPBOARD as well as PRIMARY for copy/paste
authorSam Lantinga <slouken@libsdl.org>
Fri, 14 Sep 2012 01:16:27 -0700
changeset 6420 6f52dc57f05e
parent 6419 b75e82af5ef1
child 6421 798d88be4de9
Fixed bug 1599 - On X11, SDL2 should use CLIPBOARD as well as PRIMARY for copy/paste David White 2012-09-12 13:22:52 PDT Background: X has two main 'buffers' for copy/paste: PRIMARY -- normally used for implicit selection of text, with middle-click to paste CLIPBOARD -- works with an explicit copy/paste like on other platforms. Currently SDL2 only provides access to PRIMARY. Since CLIPBOARD is much closer to functionality of other platforms, SDL should provide access to that instead. The attached patch makes it so that SDL_SetClipboardText() sets both PRIMARY and CLIPBOARD and SDL_GetClipboardText() reads from CLIPBOARD instead of primary.
src/video/x11/SDL_x11clipboard.c
--- a/src/video/x11/SDL_x11clipboard.c	Wed Sep 12 19:36:18 2012 -0400
+++ b/src/video/x11/SDL_x11clipboard.c	Fri Sep 14 01:16:27 2012 -0700
@@ -54,6 +54,7 @@
     Display *display = ((SDL_VideoData *) _this->driverdata)->display;
     Atom format;
     Window window;
+    Atom XA_CLIPBOARD = XInternAtom(display, "CLIPBOARD", 0);
 
     /* Get the SDL window that will own the selection */
     window = GetWindow(_this);
@@ -68,6 +69,11 @@
         XA_CUT_BUFFER0, format, 8, PropModeReplace,
         (const unsigned char *)text, SDL_strlen(text));
 
+    if (XA_CLIPBOARD != None &&
+        XGetSelectionOwner(display, XA_CLIPBOARD) != window) {
+        XSetSelectionOwner(display, XA_CLIPBOARD, window, CurrentTime);
+    }
+
     if (XGetSelectionOwner(display, XA_PRIMARY) != window) {
         XSetSelectionOwner(display, XA_PRIMARY, window, CurrentTime);
     }
@@ -89,13 +95,18 @@
     unsigned long overflow;
     unsigned char *src;
     char *text;
+    Atom XA_CLIPBOARD = XInternAtom(display, "CLIPBOARD", 0);
+    if (XA_CLIPBOARD == None) {
+        SDL_SetError("Couldn't access X clipboard");
+        return NULL;
+    }
 
     text = NULL;
 
     /* Get the window that holds the selection */
     window = GetWindow(_this);
     format = TEXT_FORMAT;
-    owner = XGetSelectionOwner(display, XA_PRIMARY);
+    owner = XGetSelectionOwner(display, XA_CLIPBOARD);
     if ((owner == None) || (owner == window)) {
         owner = DefaultRootWindow(display);
         selection = XA_CUT_BUFFER0;
@@ -103,7 +114,7 @@
         /* Request that the selection owner copy the data to our window */
         owner = window;
         selection = XInternAtom(display, "SDL_SELECTION", False);
-        XConvertSelection(display, XA_PRIMARY, format, selection, owner,
+        XConvertSelection(display, XA_CLIPBOARD, format, selection, owner,
             CurrentTime);
 
         /* FIXME: Should we have a timeout here? */