2
*Copyright (C) 2003-2004 Harold L Hunt II All Rights Reserved.
3
*Copyright (C) Colin Harrison 2005-2008
5
*Permission is hereby granted, free of charge, to any person obtaining
6
* a copy of this software and associated documentation files (the
7
*"Software"), to deal in the Software without restriction, including
8
*without limitation the rights to use, copy, modify, merge, publish,
9
*distribute, sublicense, and/or sell copies of the Software, and to
10
*permit persons to whom the Software is furnished to do so, subject to
11
*the following conditions:
13
*The above copyright notice and this permission notice shall be
14
*included in all copies or substantial portions of the Software.
16
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
*EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
*MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
*NONINFRINGEMENT. IN NO EVENT SHALL HAROLD L HUNT II BE LIABLE FOR
20
*ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21
*CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
*WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
*Except as contained in this notice, the name of the copyright holder(s)
25
*and author(s) shall not be used in advertising or otherwise to promote
26
*the sale, use or other dealings in this Software without prior written
27
*authorization from the copyright holder(s) and author(s).
29
* Authors: Harold L Hunt II
33
#ifdef HAVE_XWIN_CONFIG_H
34
#include <xwin-config.h>
37
#include "dixstruct.h"
38
#include <X11/Xatom.h>
44
#define CLIP_NUM_SELECTIONS 2
45
#define CLIP_OWN_PRIMARY 0
46
#define CLIP_OWN_CLIPBOARD 1
49
* Local function prototypes
52
DISPATCH_PROC(winProcEstablishConnection);
53
DISPATCH_PROC(winProcSetSelectionOwner);
56
* References to external symbols
59
extern Bool g_fClipboardLaunched;
60
extern Bool g_fClipboardStarted;
61
extern Bool g_fClipboard;
62
extern Window g_iClipboardWindow;
63
extern Atom g_atomLastOwnedSelection;
64
extern HWND g_hwndClipboard;
68
* Wrapper for internal EstablishConnection function.
69
* Initializes internal clients that must not be started until
70
* an external client has connected.
74
winProcEstablishConnection(ClientPtr client)
77
static int s_iCallCount = 0;
78
static unsigned long s_ulServerGeneration = 0;
80
if (s_iCallCount == 0)
81
winDebug("winProcEstablishConnection - Hello\n");
83
/* Do nothing if clipboard is not enabled */
85
ErrorF("winProcEstablishConnection - Clipboard is not enabled, "
88
/* Unwrap the original function, call it, and return */
89
InitialVector[2] = winProcEstablishConnectionOrig;
90
iReturn = (*winProcEstablishConnectionOrig) (client);
91
winProcEstablishConnectionOrig = NULL;
95
/* Watch for server reset */
96
if (s_ulServerGeneration != serverGeneration) {
97
/* Save new generation number */
98
s_ulServerGeneration = serverGeneration;
100
/* Reset call count */
104
/* Increment call count */
108
* This procedure is only used for initialization.
109
* We can unwrap the original procedure at this point
110
* so that this function is no longer called until the
111
* server resets and the function is wrapped again.
113
InitialVector[2] = winProcEstablishConnectionOrig;
116
* Call original function and bail if it fails.
117
* NOTE: We must do this first, since we need XdmcpOpenDisplay
118
* to be called before we initialize our clipboard client.
120
iReturn = (*winProcEstablishConnectionOrig) (client);
122
ErrorF("winProcEstablishConnection - ProcEstablishConnection "
123
"failed, bailing.\n");
127
/* Clear original function pointer */
128
winProcEstablishConnectionOrig = NULL;
130
/* If the clipboard client has already been started, abort */
131
if (g_fClipboardLaunched) {
132
ErrorF("winProcEstablishConnection - Clipboard client already "
133
"launched, returning.\n");
137
/* Startup the clipboard client if clipboard mode is being used */
140
* NOTE: The clipboard client is started here for a reason:
141
* 1) Assume you are using XDMCP (e.g. XWin -query %hostname%)
142
* 2) If the clipboard client attaches during X Server startup,
143
* then it becomes the "magic client" that causes the X Server
144
* to reset if it exits.
145
* 3) XDMCP calls KillAllClients when it starts up.
146
* 4) The clipboard client is a client, so it is killed.
147
* 5) The clipboard client is the "magic client", so the X Server
149
* 6) This repeats ad infinitum.
150
* 7) We avoid this by waiting until at least one client (could
151
* be XDM, could be another client) connects, which makes it
152
* almost certain that the clipboard client will not connect
153
* until after XDM when using XDMCP.
156
/* Create the clipboard client thread */
157
if (!winInitClipboard()) {
158
ErrorF("winProcEstablishConnection - winClipboardInit "
163
ErrorF("winProcEstablishConnection - winInitClipboard returned.\n");
166
/* Flag that clipboard client has been launched */
167
g_fClipboardLaunched = TRUE;
173
* Wrapper for internal SetSelectionOwner function.
174
* Grabs ownership of Windows clipboard when X11 clipboard owner changes.
178
winProcSetSelectionOwner(ClientPtr client)
181
DrawablePtr pDrawable;
182
WindowPtr pWindow = None;
183
Bool fOwnedToNotOwned = FALSE;
184
static Window s_iOwners[CLIP_NUM_SELECTIONS] = { None };
185
static unsigned long s_ulServerGeneration = 0;
187
REQUEST(xSetSelectionOwnerReq);
189
REQUEST_SIZE_MATCH(xSetSelectionOwnerReq);
191
winDebug("winProcSetSelectionOwner - Hello.\n");
193
/* Watch for server reset */
194
if (s_ulServerGeneration != serverGeneration) {
195
/* Save new generation number */
196
s_ulServerGeneration = serverGeneration;
198
/* Initialize static variables */
199
for (i = 0; i < CLIP_NUM_SELECTIONS; ++i)
203
/* Abort if clipboard not completely initialized yet */
204
if (!g_fClipboardStarted) {
205
/* ErrorF ("winProcSetSelectionOwner - Clipboard not yet started, "
207
goto winProcSetSelectionOwner_Done;
210
/* Grab window if we have one */
211
if (None != stuff->window) {
212
/* Grab the Window from the request */
214
dixLookupWindow(&pWindow, stuff->window, client, DixReadAccess);
216
ErrorF("winProcSetSelectionOwner - Found BadWindow, aborting.\n");
217
goto winProcSetSelectionOwner_Done;
221
/* Now we either have a valid window or None */
223
/* Save selection owners for monitored selections, ignore other selections */
224
if (XA_PRIMARY == stuff->selection) {
225
/* Look for owned -> not owned transition */
226
if (None == stuff->window && None != s_iOwners[CLIP_OWN_PRIMARY]) {
227
fOwnedToNotOwned = TRUE;
229
winDebug("winProcSetSelectionOwner - PRIMARY - Going from "
230
"owned to not owned.\n");
232
/* Adjust last owned selection */
233
if (None != s_iOwners[CLIP_OWN_CLIPBOARD])
234
g_atomLastOwnedSelection = MakeAtom("CLIPBOARD", 9, TRUE);
236
g_atomLastOwnedSelection = None;
239
/* Save new selection owner or None */
240
s_iOwners[CLIP_OWN_PRIMARY] = stuff->window;
242
winDebug("winProcSetSelectionOwner - PRIMARY - Now owned by: %d\n",
245
else if (MakeAtom("CLIPBOARD", 9, TRUE) == stuff->selection) {
246
/* Look for owned -> not owned transition */
247
if (None == stuff->window && None != s_iOwners[CLIP_OWN_CLIPBOARD]) {
248
fOwnedToNotOwned = TRUE;
250
winDebug("winProcSetSelectionOwner - CLIPBOARD - Going from "
251
"owned to not owned.\n");
253
/* Adjust last owned selection */
254
if (None != s_iOwners[CLIP_OWN_PRIMARY])
255
g_atomLastOwnedSelection = XA_PRIMARY;
257
g_atomLastOwnedSelection = None;
260
/* Save new selection owner or None */
261
s_iOwners[CLIP_OWN_CLIPBOARD] = stuff->window;
263
winDebug("winProcSetSelectionOwner - CLIPBOARD - Now owned by: %d\n",
268
goto winProcSetSelectionOwner_Done;
271
* At this point, if one of the selections is still owned by the
272
* clipboard manager then it should be marked as unowned since
273
* we will be taking ownership of the Win32 clipboard.
275
if (g_iClipboardWindow == s_iOwners[CLIP_OWN_PRIMARY])
276
s_iOwners[CLIP_OWN_PRIMARY] = None;
277
if (g_iClipboardWindow == s_iOwners[CLIP_OWN_CLIPBOARD])
278
s_iOwners[CLIP_OWN_CLIPBOARD] = None;
281
* Handle case when selection is being disowned,
282
* WM_DRAWCLIPBOARD did not do the disowning,
283
* both monitored selections are no longer owned,
284
* an owned to not owned transition was detected,
285
* and we currently own the Win32 clipboard.
287
if (stuff->window == None
288
&& s_iOwners[CLIP_OWN_PRIMARY] == None
289
&& s_iOwners[CLIP_OWN_CLIPBOARD] == None
291
&& g_hwndClipboard != NULL && g_hwndClipboard == GetClipboardOwner()) {
292
winDebug("winProcSetSelectionOwner - We currently own the "
293
"clipboard and neither the PRIMARY nor the CLIPBOARD "
294
"selections are owned, releasing ownership of Win32 "
297
/* Release ownership of the Windows clipboard */
302
goto winProcSetSelectionOwner_Done;
305
/* Abort if no window at this point */
306
if (None == stuff->window) {
307
winDebug("winProcSetSelectionOwner - No window, returning.\n");
308
goto winProcSetSelectionOwner_Done;
311
/* Abort if invalid selection */
312
if (!ValidAtom(stuff->selection)) {
313
ErrorF("winProcSetSelectionOwner - Found BadAtom, aborting.\n");
314
goto winProcSetSelectionOwner_Done;
317
/* Cast Window to Drawable */
318
pDrawable = (DrawablePtr) pWindow;
320
/* Abort if clipboard manager is owning the selection */
321
if (pDrawable->id == g_iClipboardWindow) {
322
winDebug("winProcSetSelectionOwner - We changed ownership, "
324
goto winProcSetSelectionOwner_Done;
327
/* Abort if root window is taking ownership */
328
if (pDrawable->id == 0) {
329
ErrorF("winProcSetSelectionOwner - Root window taking ownership, "
331
goto winProcSetSelectionOwner_Done;
334
/* Close clipboard if we have it open already */
335
if (GetOpenClipboardWindow() == g_hwndClipboard) {
339
/* Access the Windows clipboard */
340
if (!OpenClipboard(g_hwndClipboard)) {
341
ErrorF("winProcSetSelectionOwner - OpenClipboard () failed: %08x\n",
342
(int) GetLastError());
343
goto winProcSetSelectionOwner_Done;
346
/* Take ownership of the Windows clipboard */
347
if (!EmptyClipboard()) {
348
ErrorF("winProcSetSelectionOwner - EmptyClipboard () failed: %08x\n",
349
(int) GetLastError());
350
goto winProcSetSelectionOwner_Done;
353
/* Advertise regular text and unicode */
354
SetClipboardData(CF_UNICODETEXT, NULL);
355
SetClipboardData(CF_TEXT, NULL);
357
/* Save handle to last owned selection */
358
g_atomLastOwnedSelection = stuff->selection;
360
/* Release the clipboard */
361
if (!CloseClipboard()) {
362
ErrorF("winProcSetSelectionOwner - CloseClipboard () failed: "
363
"%08x\n", (int) GetLastError());
364
goto winProcSetSelectionOwner_Done;
367
winProcSetSelectionOwner_Done:
368
return (*winProcSetSelectionOwnerOrig) (client);