1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
/*
This file is part of Warzone 2100.
Copyright (C) 2008 Freddie Witherden
Copyright (C) 2008-2009 Warzone Resurrection Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* Something wicked this way comes...
* Documentation/reference:
* http://svr-www.eng.cam.ac.uk/~er258/code/dist/x_clipboard/paste.cc
*/
#include <stdbool.h>
#include <assert.h>
#include <SDL_syswm.h>
#include <SDL.h>
static SDL_SysWMinfo info;
// Atoms
static Atom XA_CLIPBOARD;
static Atom XA_COMPOUND_TEXT;
static Atom XA_UTF8_STRING;
static Atom XA_TARGETS;
/**
* Filters through SDL_Events searching for clipboard requests from the X
* server.
*
* @param evt The event to filter.
*/
static int widgetClipboardFilterX11(const SDL_Event *evt)
{
// We are only interested in window manager events
if (evt->type == SDL_SYSWMEVENT)
{
XEvent xevent = evt->syswm.msg->event.xevent;
// See if the event is a selection/clipboard request
if (xevent.type == SelectionRequest)
{
// Get the request in question
XSelectionRequestEvent *request = &xevent.xselectionrequest;
// Generate a reply to the selection request
XSelectionEvent reply;
reply.type = SelectionNotify;
reply.serial = xevent.xany.send_event;
reply.send_event = True;
reply.display = info.info.x11.display;
reply.requestor = request->requestor;
reply.selection = request->selection;
reply.property = request->property;
reply.target = None;
reply.time = request->time;
// They want to know what we can provide/offer
if (request->target == XA_TARGETS)
{
Atom possibleTargets[] =
{
XA_STRING,
XA_UTF8_STRING,
XA_COMPOUND_TEXT
};
XChangeProperty(info.info.x11.display, request->requestor,
request->property, XA_ATOM, 32, PropModeReplace,
(unsigned char *) possibleTargets, 3);
}
// They want a string (all we can provide)
else if (request->target == XA_STRING
|| request->target == XA_UTF8_STRING
|| request->target == XA_COMPOUND_TEXT)
{
int len;
char *xdata = XFetchBytes(info.info.x11.display, &len);
XChangeProperty(info.info.x11.display, request->requestor,
request->property, request->target, 8,
PropModeReplace, (unsigned char *) xdata,
len);
XFree(xdata);
}
else
{
// Did not have what they wanted, so no property set
reply.property = None;
}
// Dispatch the event
XSendEvent(request->display, request->requestor, 0, NoEventMask,
(XEvent *) &reply);
XSync(info.info.x11.display, False);
}
}
return 1;
}
static void widgetInitialiseClipboardX11()
{
static bool initialised = false;
if (!initialised)
{
// Get the window manager information
SDL_GetWMInfo(&info);
// Ensure we're running under X11
assert(info.subsystem == SDL_SYSWM_X11);
// Register the event filter
SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
SDL_SetEventFilter(widgetClipboardFilterX11);
// Lock the connection to the X server
info.info.x11.lock_func();
// Get the clipboard atom (it is not defined by default)
XA_CLIPBOARD = XInternAtom(info.info.x11.display, "CLIPBOARD", True);
// Get the compound text type atom
XA_COMPOUND_TEXT = XInternAtom(info.info.x11.display, "COMPOUND_TEXT",
True);
// UTF-8 string atom
XA_UTF8_STRING = XInternAtom(info.info.x11.display, "UTF8_STRING",
True);
// TARGETS atom
XA_TARGETS = XInternAtom(info.info.x11.display, "TARGETS", True);
// Unlock the connection
info.info.x11.unlock_func();
// We are initialised
initialised = true;
}
}
char *widgetGetClipboardText()
{
char *text = NULL;
unsigned char *data = NULL;
Atom type;
int format, result;
unsigned long len, bytesLeft, dummy;
Window selectionOwner;
// Make sure we are initialised
widgetInitialiseClipboardX11();
// Lock the connection
info.info.x11.lock_func();
// Get the owner of the clipboard selection
selectionOwner = XGetSelectionOwner(info.info.x11.display, XA_CLIPBOARD);
// If there is a selection (and therefore owner) fetch it
if (selectionOwner != None)
{
SDL_Event event;
bool response = false;
/*
* Ask the window whom current owns the clipboard to convert it to an
* XA_UTF8_STRING and place it into the XA_CLIPBOARD property of our
* window.
*/
XConvertSelection(info.info.x11.display, XA_CLIPBOARD, XA_UTF8_STRING,
XA_CLIPBOARD, info.info.x11.window, CurrentTime);
XFlush(info.info.x11.display);
/*
* We now need to wait for a response from the window that owns the
* clipboard.
*/
// Unlock the connection so that the SDL event loop may function
info.info.x11.unlock_func();
while (!response)
{
// Wait for an event
SDL_WaitEvent(&event);
// If the event is a window manager event
if (event.type == SDL_SYSWMEVENT)
{
XEvent xevent = event.syswm.msg->event.xevent;
// See if it is a response to our request
if (xevent.type == SelectionNotify
&& xevent.xselection.requestor == info.info.x11.window)
{
response = true;
}
}
}
// Lock the connection once again
info.info.x11.lock_func();
// See how much data is there
XGetWindowProperty(info.info.x11.display, info.info.x11.window,
XA_CLIPBOARD, 0, 0, False, AnyPropertyType, &type,
&format, &len, &bytesLeft, &data);
// If any 0-length data was returned, free it
if (data)
{
XFree(data);
data = NULL;
}
// If there is any data
if (bytesLeft)
{
// Fetch the data
result = XGetWindowProperty(info.info.x11.display,
info.info.x11.window, XA_CLIPBOARD, 0,
bytesLeft, False, AnyPropertyType,
&type, &format, &len, &dummy, &data);
// If we got some data, duplicate it
if (result == Success)
{
text = strdup((char *) data);
XFree(data);
}
}
// Delete the property now that we are finished with it
XDeleteProperty(info.info.x11.display, info.info.x11.window,
XA_CLIPBOARD);
}
// Unlock the connection
info.info.x11.unlock_func();
return text;
}
bool widgetSetClipboardText(const char *text)
{
Window selectionOwner;
// Make sure we are initialised
widgetInitialiseClipboardX11();
// Lock the connection
info.info.x11.lock_func();
// Copy the text into the root windows cut buffer (for Xterm compatibility)
XStoreBytes(info.info.x11.display, text, strlen(text) + 1);
// Set ourself as the owner of the CLIPBOARD atom
XSetSelectionOwner(info.info.x11.display, XA_CLIPBOARD,
info.info.x11.window, CurrentTime);
// Check if we acquired ownership or not
selectionOwner = XGetSelectionOwner(info.info.x11.display, XA_CLIPBOARD);
// We got ownership
if (selectionOwner == info.info.x11.window)
{
info.info.x11.unlock_func();
return true;
}
// We did not get ownership
else
{
info.info.x11.unlock_func();
return false;
}
}
|