src/video/x11/SDL_x11clipboard.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 02 Jan 2016 10:10:34 -0800
changeset 9998 f67cf37e9cd4
parent 9653 0a2de253c67e
child 10737 3406a0f8b041
permissions -rw-r--r--
Updated copyright to 2016
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../../SDL_internal.h"
    22 
    23 #if SDL_VIDEO_DRIVER_X11
    24 
    25 #include <limits.h> /* For INT_MAX */
    26 
    27 #include "SDL_events.h"
    28 #include "SDL_x11video.h"
    29 #include "SDL_timer.h"
    30 
    31 
    32 /* If you don't support UTF-8, you might use XA_STRING here */
    33 #ifdef X_HAVE_UTF8_STRING
    34 #define TEXT_FORMAT X11_XInternAtom(display, "UTF8_STRING", False)
    35 #else
    36 #define TEXT_FORMAT XA_STRING
    37 #endif
    38 
    39 /* Get any application owned window handle for clipboard association */
    40 static Window
    41 GetWindow(_THIS)
    42 {
    43     SDL_Window *window;
    44 
    45     window = _this->windows;
    46     if (window) {
    47         return ((SDL_WindowData *) window->driverdata)->xwindow;
    48     }
    49     return None;
    50 }
    51 
    52 /* We use our own cut-buffer for intermediate storage instead of  
    53    XA_CUT_BUFFER0 because their use isn't really defined for holding UTF8. */ 
    54 Atom
    55 X11_GetSDLCutBufferClipboardType(Display *display)
    56 {
    57     return X11_XInternAtom(display, "SDL_CUTBUFFER", False);
    58 }
    59 
    60 int
    61 X11_SetClipboardText(_THIS, const char *text)
    62 {
    63     Display *display = ((SDL_VideoData *) _this->driverdata)->display;
    64     Atom format;
    65     Window window;
    66     Atom XA_CLIPBOARD = X11_XInternAtom(display, "CLIPBOARD", 0);
    67 
    68     /* Get the SDL window that will own the selection */
    69     window = GetWindow(_this);
    70     if (window == None) {
    71         return SDL_SetError("Couldn't find a window to own the selection");
    72     }
    73 
    74     /* Save the selection on the root window */
    75     format = TEXT_FORMAT;
    76     X11_XChangeProperty(display, DefaultRootWindow(display),
    77         X11_GetSDLCutBufferClipboardType(display), format, 8, PropModeReplace,
    78         (const unsigned char *)text, SDL_strlen(text));
    79 
    80     if (XA_CLIPBOARD != None &&
    81         X11_XGetSelectionOwner(display, XA_CLIPBOARD) != window) {
    82         X11_XSetSelectionOwner(display, XA_CLIPBOARD, window, CurrentTime);
    83     }
    84 
    85     if (X11_XGetSelectionOwner(display, XA_PRIMARY) != window) {
    86         X11_XSetSelectionOwner(display, XA_PRIMARY, window, CurrentTime);
    87     }
    88     return 0;
    89 }
    90 
    91 char *
    92 X11_GetClipboardText(_THIS)
    93 {
    94     SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
    95     Display *display = videodata->display;
    96     Atom format;
    97     Window window;
    98     Window owner;
    99     Atom selection;
   100     Atom seln_type;
   101     int seln_format;
   102     unsigned long nbytes;
   103     unsigned long overflow;
   104     unsigned char *src;
   105     char *text;
   106     Uint32 waitStart;
   107     Uint32 waitElapsed;
   108     Atom XA_CLIPBOARD = X11_XInternAtom(display, "CLIPBOARD", 0);
   109     if (XA_CLIPBOARD == None) {
   110         SDL_SetError("Couldn't access X clipboard");
   111         return SDL_strdup("");
   112     }
   113 
   114     text = NULL;
   115 
   116     /* Get the window that holds the selection */
   117     window = GetWindow(_this);
   118     format = TEXT_FORMAT;
   119     owner = X11_XGetSelectionOwner(display, XA_CLIPBOARD);
   120     if (owner == None) {
   121         /* Fall back to ancient X10 cut-buffers which do not support UTF8 strings*/
   122         owner = DefaultRootWindow(display);
   123         selection = XA_CUT_BUFFER0;
   124         format = XA_STRING;
   125     } else if (owner == window) {
   126         owner = DefaultRootWindow(display);
   127         selection = X11_GetSDLCutBufferClipboardType(display);
   128     } else {
   129         /* Request that the selection owner copy the data to our window */
   130         owner = window;
   131         selection = X11_XInternAtom(display, "SDL_SELECTION", False);
   132         X11_XConvertSelection(display, XA_CLIPBOARD, format, selection, owner,
   133             CurrentTime);
   134 
   135         /* When using synergy on Linux and when data has been put in the clipboard
   136            on the remote (Windows anyway) machine then selection_waiting may never
   137            be set to False. Time out after a while. */
   138         waitStart = SDL_GetTicks();
   139         videodata->selection_waiting = SDL_TRUE;
   140         while (videodata->selection_waiting) {
   141             SDL_PumpEvents();
   142             waitElapsed = SDL_GetTicks() - waitStart;
   143             /* Wait one second for a clipboard response. */
   144             if (waitElapsed > 1000) {
   145                 videodata->selection_waiting = SDL_FALSE;
   146                 SDL_SetError("Clipboard timeout");
   147                 /* We need to set the clipboard text so that next time we won't
   148                    timeout, otherwise we will hang on every call to this function. */
   149                 X11_SetClipboardText(_this, "");
   150                 return SDL_strdup("");
   151             }
   152         }
   153     }
   154 
   155     if (X11_XGetWindowProperty(display, owner, selection, 0, INT_MAX/4, False,
   156             format, &seln_type, &seln_format, &nbytes, &overflow, &src)
   157             == Success) {
   158         if (seln_type == format) {
   159             text = (char *)SDL_malloc(nbytes+1);
   160             if (text) {
   161                 SDL_memcpy(text, src, nbytes);
   162                 text[nbytes] = '\0';
   163             }
   164         }
   165         X11_XFree(src);
   166     }
   167 
   168     if (!text) {
   169         text = SDL_strdup("");
   170     }
   171 
   172     return text;
   173 }
   174 
   175 SDL_bool
   176 X11_HasClipboardText(_THIS)
   177 {
   178     SDL_bool result = SDL_FALSE;
   179     char *text = X11_GetClipboardText(_this);
   180     if (text) {
   181         result = text[0] != '\0' ? SDL_TRUE : SDL_FALSE;
   182         SDL_free(text);
   183     }
   184     return result;
   185 }
   186 
   187 #endif /* SDL_VIDEO_DRIVER_X11 */
   188 
   189 /* vi: set ts=4 sw=4 expandtab: */