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