src/video/x11/SDL_x11clipboard.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 02 Mar 2013 20:44:16 -0800
changeset 6950 1ddb72193079
parent 6885 700f1b25f77f
child 7037 3fedf1f25b94
permissions -rw-r--r--
Added a mouse ID to the mouse events, which set to the special value SDL_TOUCH_MOUSEID for mouse events simulated by touch input.
slouken@4508
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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
*/
slouken@4508
    21
#include "SDL_config.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@4508
    29
slouken@4508
    30
slouken@4509
    31
/* If you don't support UTF-8, you might use XA_STRING here */
slouken@4516
    32
#ifdef X_HAVE_UTF8_STRING
slouken@4509
    33
#define TEXT_FORMAT XInternAtom(display, "UTF8_STRING", False)
slouken@4509
    34
#else
slouken@4509
    35
#define TEXT_FORMAT XA_STRING
slouken@4509
    36
#endif
slouken@4509
    37
slouken@4508
    38
/* Get any application owned window handle for clipboard association */
slouken@4508
    39
static Window
slouken@4508
    40
GetWindow(_THIS)
slouken@4508
    41
{
slouken@4508
    42
    SDL_Window *window;
slouken@4508
    43
slouken@5246
    44
    window = _this->windows;
slouken@5246
    45
    if (window) {
slouken@5246
    46
        return ((SDL_WindowData *) window->driverdata)->xwindow;
slouken@4508
    47
    }
slouken@4508
    48
    return None;
slouken@4508
    49
}
slouken@4508
    50
slouken@4508
    51
int
slouken@4508
    52
X11_SetClipboardText(_THIS, const char *text)
slouken@4508
    53
{
slouken@4508
    54
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@4508
    55
    Atom format;
slouken@4508
    56
    Window window;
slouken@6420
    57
    Atom XA_CLIPBOARD = XInternAtom(display, "CLIPBOARD", 0);
slouken@4508
    58
slouken@4508
    59
    /* Get the SDL window that will own the selection */
slouken@4508
    60
    window = GetWindow(_this);
slouken@4508
    61
    if (window == None) {
slouken@4508
    62
        SDL_SetError("Couldn't find a window to own the selection");
slouken@4508
    63
        return -1;
slouken@4508
    64
    }
slouken@4508
    65
slouken@4509
    66
    /* Save the selection on the root window */
slouken@4509
    67
    format = TEXT_FORMAT;
slouken@4508
    68
    XChangeProperty(display, DefaultRootWindow(display),
slouken@4508
    69
        XA_CUT_BUFFER0, format, 8, PropModeReplace,
slouken@4508
    70
        (const unsigned char *)text, SDL_strlen(text));
slouken@4508
    71
slouken@6420
    72
    if (XA_CLIPBOARD != None &&
slouken@6420
    73
        XGetSelectionOwner(display, XA_CLIPBOARD) != window) {
slouken@6420
    74
        XSetSelectionOwner(display, XA_CLIPBOARD, window, CurrentTime);
slouken@6420
    75
    }
slouken@6420
    76
slouken@4508
    77
    if (XGetSelectionOwner(display, XA_PRIMARY) != window) {
slouken@4508
    78
        XSetSelectionOwner(display, XA_PRIMARY, window, CurrentTime);
slouken@4508
    79
    }
slouken@4508
    80
    return 0;
slouken@4508
    81
}
slouken@4508
    82
slouken@4508
    83
char *
slouken@4508
    84
X11_GetClipboardText(_THIS)
slouken@4508
    85
{
slouken@4508
    86
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@4508
    87
    Display *display = videodata->display;
slouken@4508
    88
    Atom format;
slouken@4508
    89
    Window window;
slouken@4508
    90
    Window owner;
slouken@4508
    91
    Atom selection;
slouken@4508
    92
    Atom seln_type;
slouken@4508
    93
    int seln_format;
slouken@4508
    94
    unsigned long nbytes;
slouken@4508
    95
    unsigned long overflow;
slouken@4508
    96
    unsigned char *src;
slouken@4508
    97
    char *text;
slouken@6420
    98
    Atom XA_CLIPBOARD = XInternAtom(display, "CLIPBOARD", 0);
slouken@6420
    99
    if (XA_CLIPBOARD == None) {
slouken@6420
   100
        SDL_SetError("Couldn't access X clipboard");
slouken@6420
   101
        return NULL;
slouken@6420
   102
    }
slouken@4508
   103
slouken@4508
   104
    text = NULL;
slouken@4508
   105
slouken@4509
   106
    /* Get the window that holds the selection */
slouken@4508
   107
    window = GetWindow(_this);
slouken@4509
   108
    format = TEXT_FORMAT;
slouken@6420
   109
    owner = XGetSelectionOwner(display, XA_CLIPBOARD);
slouken@4508
   110
    if ((owner == None) || (owner == window)) {
slouken@4508
   111
        owner = DefaultRootWindow(display);
slouken@4508
   112
        selection = XA_CUT_BUFFER0;
slouken@4508
   113
    } else {
slouken@4508
   114
        /* Request that the selection owner copy the data to our window */
slouken@4508
   115
        owner = window;
slouken@4508
   116
        selection = XInternAtom(display, "SDL_SELECTION", False);
slouken@6420
   117
        XConvertSelection(display, XA_CLIPBOARD, format, selection, owner,
slouken@4508
   118
            CurrentTime);
slouken@4508
   119
slouken@4508
   120
        /* FIXME: Should we have a timeout here? */
slouken@4508
   121
        videodata->selection_waiting = SDL_TRUE;
slouken@4508
   122
        while (videodata->selection_waiting) {
slouken@4508
   123
            SDL_PumpEvents();
slouken@4508
   124
        }
slouken@4508
   125
    }
slouken@4508
   126
slouken@4508
   127
    if (XGetWindowProperty(display, owner, selection, 0, INT_MAX/4, False,
slouken@4508
   128
            format, &seln_type, &seln_format, &nbytes, &overflow, &src)
slouken@4508
   129
            == Success) {
slouken@4508
   130
        if (seln_type == format) {
slouken@4508
   131
            text = (char *)SDL_malloc(nbytes+1);
slouken@4508
   132
            if (text) {
slouken@4508
   133
                SDL_memcpy(text, src, nbytes);
slouken@4508
   134
                text[nbytes] = '\0';
slouken@4508
   135
            }
slouken@4508
   136
        }
slouken@4508
   137
        XFree(src);
slouken@4508
   138
    }
slouken@4508
   139
slouken@4508
   140
    if (!text) {
slouken@4508
   141
        text = SDL_strdup("");
slouken@4508
   142
    }
aschiffler@6036
   143
    
slouken@4508
   144
    return text;
slouken@4508
   145
}
slouken@4508
   146
slouken@4508
   147
SDL_bool
slouken@4508
   148
X11_HasClipboardText(_THIS)
slouken@4508
   149
{
aschiffler@6036
   150
    SDL_bool result = SDL_FALSE;
aschiffler@6036
   151
    char *text = X11_GetClipboardText(_this);
aschiffler@6036
   152
    if (text) {
aschiffler@6036
   153
        result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
aschiffler@6036
   154
        SDL_free(text);
aschiffler@6036
   155
    }    
aschiffler@6036
   156
    return result;
slouken@4508
   157
}
slouken@4508
   158
slouken@5481
   159
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   160
slouken@4508
   161
/* vi: set ts=4 sw=4 expandtab: */