annotate src/video/x11/SDL_x11clipboard.c @ 4508:15d2c6f40c48

Added X11 clipboard support. As far as I know there isn't any real way to tell when the clipboard contents have changed without polling them, so I didn't implement the clipboard update event on X11.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 12 Jul 2010 00:36:55 -0700
parents
children 8e91c3947210
rev   line source
4508
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1 /*
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
3 Copyright (C) 1997-2010 Sam Lantinga
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
4
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
6 modify it under the terms of the GNU Lesser General Public
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
7 License as published by the Free Software Foundation; either
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
9
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
13 Lesser General Public License for more details.
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
14
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
16 License along with this library; if not, write to the Free Software
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
18
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
19 Sam Lantinga
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
20 slouken@libsdl.org
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
21 */
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
22 #include "SDL_config.h"
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
23
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
24 #include <limits.h> /* For INT_MAX */
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
25
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
26 #include "SDL_events.h"
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
27 #include "SDL_x11video.h"
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
28
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
29
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
30 /* Get any application owned window handle for clipboard association */
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
31 static Window
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
32 GetWindow(_THIS)
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
33 {
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
34 SDL_VideoDisplay *display;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
35 SDL_Window *window;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
36
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
37 display = _this->displays;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
38 if (display) {
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
39 window = display->windows;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
40 if (window) {
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
41 return ((SDL_WindowData *) window->driverdata)->xwindow;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
42 }
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
43 }
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
44 return None;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
45 }
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
46
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
47 int
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
48 X11_SetClipboardText(_THIS, const char *text)
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
49 {
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
50 Display *display = ((SDL_VideoData *) _this->driverdata)->display;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
51 Atom format;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
52 Window window;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
53
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
54 /* Get the SDL window that will own the selection */
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
55 window = GetWindow(_this);
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
56 if (window == None) {
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
57 SDL_SetError("Couldn't find a window to own the selection");
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
58 return -1;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
59 }
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
60
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
61 /* If you don't support UTF-8, you might use XA_STRING here */
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
62 format = XInternAtom(display, "UTF8_STRING", False);
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
63 XChangeProperty(display, DefaultRootWindow(display),
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
64 XA_CUT_BUFFER0, format, 8, PropModeReplace,
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
65 (const unsigned char *)text, SDL_strlen(text));
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
66
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
67 if (XGetSelectionOwner(display, XA_PRIMARY) != window) {
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
68 XSetSelectionOwner(display, XA_PRIMARY, window, CurrentTime);
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
69 }
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
70 return 0;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
71 }
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
72
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
73 char *
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
74 X11_GetClipboardText(_THIS)
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
75 {
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
76 SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
77 Display *display = videodata->display;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
78 Atom format;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
79 Window window;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
80 Window owner;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
81 Atom selection;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
82 Atom seln_type;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
83 int seln_format;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
84 unsigned long nbytes;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
85 unsigned long overflow;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
86 unsigned char *src;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
87 char *text;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
88
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
89 text = NULL;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
90
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
91 /* Get the SDL window that will own the selection */
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
92 window = GetWindow(_this);
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
93
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
94 /* If you don't support UTF-8, you might use XA_STRING here */
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
95 format = XInternAtom(display, "UTF8_STRING", False);
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
96
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
97 owner = XGetSelectionOwner(display, XA_PRIMARY);
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
98 if ((owner == None) || (owner == window)) {
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
99 owner = DefaultRootWindow(display);
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
100 selection = XA_CUT_BUFFER0;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
101 } else {
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
102 /* Request that the selection owner copy the data to our window */
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
103 owner = window;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
104 selection = XInternAtom(display, "SDL_SELECTION", False);
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
105 XConvertSelection(display, XA_PRIMARY, format, selection, owner,
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
106 CurrentTime);
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
107
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
108 /* FIXME: Should we have a timeout here? */
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
109 videodata->selection_waiting = SDL_TRUE;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
110 while (videodata->selection_waiting) {
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
111 SDL_PumpEvents();
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
112 }
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
113 }
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
114
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
115 if (XGetWindowProperty(display, owner, selection, 0, INT_MAX/4, False,
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
116 format, &seln_type, &seln_format, &nbytes, &overflow, &src)
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
117 == Success) {
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
118 if (seln_type == format) {
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
119 text = (char *)SDL_malloc(nbytes+1);
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
120 if (text) {
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
121 SDL_memcpy(text, src, nbytes);
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
122 text[nbytes] = '\0';
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
123 }
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
124 }
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
125 XFree(src);
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
126 }
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
127
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
128 if (!text) {
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
129 text = SDL_strdup("");
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
130 }
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
131 return text;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
132 }
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
133
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
134 SDL_bool
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
135 X11_HasClipboardText(_THIS)
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
136 {
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
137 /* Not an easy way to tell with X11, as far as I know... */
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
138 char *text;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
139 SDL_bool retval;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
140
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
141 text = X11_GetClipboardText(_this);
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
142 if (*text) {
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
143 retval = SDL_TRUE;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
144 } else {
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
145 retval = SDL_FALSE;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
146 }
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
147 SDL_free(text);
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
148
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
149 return retval;
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
150 }
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
151
15d2c6f40c48 Added X11 clipboard support.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
152 /* vi: set ts=4 sw=4 expandtab: */