2 * X11 clipboard windows driver
4 * Copyright 1994 Martin Ayotte
9 * This file contains the X specific implementation for the windows
12 * Wine's internal clipboard is exposed to external apps via the X
13 * selection mechanism.
14 * Currently the driver asserts ownership via two selection atoms:
15 * 1. PRIMARY(XA_PRIMARY)
18 * In our implementation, the CLIPBOARD selection takes precedence over PRIMARY.
19 * i.e. if a CLIPBOARD selection is available, it is used instead of PRIMARY.
20 * When Wine taks ownership of the clipboard, it takes ownership of BOTH selections.
21 * While giving up selection ownership, if the CLIPBOARD selection is lost,
22 * it will lose both PRIMARY and CLIPBOARD and empty the clipboard.
23 * However if only PRIMARY is lost, it will continue to hold the CLIPBOARD selection
24 * (leaving the clipboard cache content unaffected).
26 * Every format exposed via a windows clipboard format is also exposed through
27 * a corresponding X selection target. A selection target atom is synthesized
28 * whenever a new Windows clipboard format is registered via RegisterClipboardFormat,
29 * or when a built in format is used for the first time.
30 * Windows native format are exposed by prefixing the format name with "<WCF>"
31 * This allows us to uniquely identify windows native formats exposed by other
34 * In order to allow external applications to query WINE for supported formats,
35 * we respond to the "TARGETS" selection target. (See EVENT_SelectionRequest
36 * for implementation) We use the same mechanism to query external clients for
37 * availability of a particular format, by cacheing the list of available targets
38 * by using the clipboard cache's "delayed render" mechanism. If a selection client
39 * does not support the "TARGETS" selection target, we actually attempt to retrieve
40 * the format requested as a fallback mechanism.
42 * Certain Windows native formats are automatically converted to X native formats
43 * and vice versa. If a native format is available in the selection, it takes
44 * precedence, in order to avoid unnecessary conversions.
50 #ifndef X_DISPLAY_MISSING
53 #include <X11/Xatom.h>
60 #include "wine/winuser16.h"
61 #include "clipboard.h"
70 #include "debugtools.h"
72 DEFAULT_DEBUG_CHANNEL(clipboard
)
76 #define S_NOSELECTION 0
80 /* X selection context info */
82 static char _CLIPBOARD
[] = "CLIPBOARD"; /* CLIPBOARD atom name */
83 static char FMT_PREFIX
[] = "<WCF>"; /* Prefix for windows specific formats */
84 static int selectionAcquired
= 0; /* Contains the current selection masks */
85 static Window selectionWindow
= None
; /* The top level X window which owns the selection */
86 static Window selectionPrevWindow
= None
; /* The last X window that owned the selection */
87 static Window PrimarySelectionOwner
= None
; /* The window which owns the primary selection */
88 static Window ClipboardSelectionOwner
= None
; /* The window which owns the clipboard selection */
89 static unsigned long cSelectionTargets
= 0; /* Number of target formats reported by TARGETS selection */
90 static Atom selectionCacheSrc
= XA_PRIMARY
; /* The selection source from which the clipboard cache was filled */
91 static HANDLE selectionClearEvent
= NULL
; /* Synchronization object used to block until server is started */
94 * Dynamic pointer arrays to manage destruction of Pixmap resources
96 static HDPA PropDPA
= NULL
;
97 static HDPA PixmapDPA
= NULL
;
101 /**************************************************************************
102 * X11DRV_CLIPBOARD_MapPropertyToFormat
104 * Map an X selection property type atom name to a windows clipboard format ID
106 UINT
X11DRV_CLIPBOARD_MapPropertyToFormat(char *itemFmtName
)
109 * If the property name starts with FMT_PREFIX strip this off and
110 * get the ID for a custom Windows registered format with this name.
111 * We can also understand STRING, PIXMAP and BITMAP.
113 if ( NULL
== itemFmtName
)
115 else if ( 0 == strncmp(itemFmtName
, FMT_PREFIX
, strlen(FMT_PREFIX
)) )
116 return RegisterClipboardFormatA(itemFmtName
+ strlen(FMT_PREFIX
));
117 else if ( 0 == strcmp(itemFmtName
, "STRING") )
119 else if ( 0 == strcmp(itemFmtName
, "PIXMAP")
120 || 0 == strcmp(itemFmtName
, "BITMAP") )
123 * Return CF_DIB as first preference, if WINE is the selection owner
124 * and if CF_DIB exists in the cache.
125 * If wine dowsn't own the selection we always return CF_DIB
127 if ( !X11DRV_CLIPBOARD_IsSelectionowner() )
129 else if ( CLIPBOARD_IsPresent(CF_DIB
) )
135 WARN("\tNo mapping to Windows clipboard format for property %s\n", itemFmtName
);
139 /**************************************************************************
140 * X11DRV_CLIPBOARD_MapFormatToProperty
142 * Map a windows clipboard format ID to an X selection property atom
144 Atom
X11DRV_CLIPBOARD_MapFormatToProperty(UINT wFormat
)
159 * Request a PIXMAP, only if WINE is NOT the selection owner,
160 * AND the requested format is not in the cache.
162 if ( !X11DRV_CLIPBOARD_IsSelectionowner() && !CLIPBOARD_IsPresent(wFormat
) )
167 /* Fall thru to the default case in order to use the native format */
173 * If an X atom is registered for this format, return that
174 * Otherwise register a new atom.
177 char *fmtName
= CLIPBOARD_GetFormatName(wFormat
);
178 strcpy(str
, FMT_PREFIX
);
182 strncat(str
, fmtName
, sizeof(str
) - strlen(FMT_PREFIX
));
183 prop
= TSXInternAtom(display
, str
, False
);
190 TRACE("\tNo mapping to X property for Windows clipboard format %d(%s)\n",
191 wFormat
, CLIPBOARD_GetFormatName(wFormat
));
196 /**************************************************************************
197 * X11DRV_CLIPBOARD_IsNativeProperty
199 * Checks if a property is a native property type
201 BOOL
X11DRV_CLIPBOARD_IsNativeProperty(Atom prop
)
203 char *itemFmtName
= TSXGetAtomName(display
, prop
);
206 if ( 0 == strncmp(itemFmtName
, FMT_PREFIX
, strlen(FMT_PREFIX
)) )
209 TSXFree(itemFmtName
);
214 /**************************************************************************
215 * X11DRV_CLIPBOARD_LaunchServer
216 * Launches the clipboard server. This is called from X11DRV_CLIPBOARD_ResetOwner
217 * when the selection can no longer be recyled to another top level window.
218 * In order to make the selection persist after Wine shuts down a server
219 * process is launched which services subsequent selection requests.
221 BOOL
X11DRV_CLIPBOARD_LaunchServer()
225 /* If persistant selection has been disabled in the .winerc Clipboard section,
226 * don't launch the server
228 if ( !PROFILE_GetWineIniInt("Clipboard", "PersistentSelection", 1) )
231 /* Start up persistant WINE X clipboard server process which will
232 * take ownership of the X selection and continue to service selection
233 * requests from other apps.
235 selectionWindow
= selectionPrevWindow
;
238 /* NOTE: This code only executes in the context of the child process
239 * Do note make any Wine specific calls here.
243 char selMask
[8], dbgClassMask
[8], clearSelection
[8];
246 /* Don't inherit wine's X sockets to the wineclipsrv, otherwise
247 * windows stay around when you have to kill a hanging wine...
249 for (i
= 3; i
< 256; ++i
)
250 fcntl(i
, F_SETFD
, 1);
252 sprintf(selMask
, "%d", selectionAcquired
);
254 /* Build the debug class mask to pass to the server, by inheriting
255 * the settings for the clipboard debug channel.
257 dbgClasses
|= __GET_DEBUGGING(__DBCL_FIXME
, dbch_clipboard
) ? 1 : 0;
258 dbgClasses
|= __GET_DEBUGGING(__DBCL_ERR
, dbch_clipboard
) ? 2 : 0;
259 dbgClasses
|= __GET_DEBUGGING(__DBCL_WARN
, dbch_clipboard
) ? 4 : 0;
260 dbgClasses
|= __GET_DEBUGGING(__DBCL_TRACE
, dbch_clipboard
) ? 8 : 0;
261 sprintf(dbgClassMask
, "%d", dbgClasses
);
263 /* Get the clear selection preference */
264 sprintf(clearSelection
, "%d",
265 PROFILE_GetWineIniInt("Clipboard", "ClearAllSelections", 0));
267 /* Exec the clipboard server passing it the selection and debug class masks */
268 execl( BINDIR
"/wineclipsrv", "wineclipsrv",
269 selMask
, dbgClassMask
, clearSelection
, NULL
);
270 execlp( "wineclipsrv", "wineclipsrv", selMask
, dbgClassMask
, clearSelection
, NULL
);
271 execl( "./windows/x11drv/wineclipsrv", "wineclipsrv",
272 selMask
, dbgClassMask
, clearSelection
, NULL
);
275 perror("Could not start Wine clipboard server");
276 exit( 1 ); /* Exit the child process */
279 /* Wait until the clipboard server acquires the selection.
280 * We must release the windows lock to enable Wine to process
281 * selection messages in response to the servers requests.
284 iWndsLocks
= WIN_SuspendWndsLock();
286 /* We must wait until the server finishes acquiring the selection,
287 * before proceeding, otherwise the window which owns the selection
288 * will be destroyed prematurely!
289 * Create a non-signalled, auto-reset event which will be set by
290 * X11DRV_CLIPBOARD_ReleaseSelection, and wait until this gets
291 * signalled before proceeding.
294 if ( !(selectionClearEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
)) )
295 ERR("Could not create wait object. Clipboard server won't start!\n");
298 /* Make the event object's handle global */
299 selectionClearEvent
= ConvertToGlobalHandle(selectionClearEvent
);
301 /* Wait until we lose the selection, timing out after a minute */
303 TRACE("Waiting for clipboard server to acquire selection\n");
305 if ( WaitForSingleObject( selectionClearEvent
, 60000 ) != WAIT_OBJECT_0
)
306 TRACE("Server could not acquire selection, or a time out occured!\n");
308 TRACE("Server successfully acquired selection\n");
310 /* Release the event */
311 CloseHandle(selectionClearEvent
);
312 selectionClearEvent
= NULL
;
315 WIN_RestoreWndsLock(iWndsLocks
);
321 /**************************************************************************
322 * X11DRV_CLIPBOARD_CacheDataFormats
324 * Caches the list of data formats available from the current selection.
325 * This queries the selection owner for the TARGETS property and saves all
326 * reported property types.
328 int X11DRV_CLIPBOARD_CacheDataFormats( Atom SelectionName
)
331 HWND hWndClipWindow
= GetOpenClipboardWindow();
335 Atom atype
=AnyPropertyType
;
337 unsigned long remain
;
338 Atom
* targetList
=NULL
;
340 Window ownerSelection
= NULL
;
343 * Empty the clipboard cache
345 CLIPBOARD_EmptyCache(TRUE
);
347 cSelectionTargets
= 0;
348 selectionCacheSrc
= SelectionName
;
350 hWnd
= (hWndClipWindow
) ? hWndClipWindow
: GetActiveWindow();
352 ownerSelection
= TSXGetSelectionOwner(display
, SelectionName
);
353 if ( !hWnd
|| (ownerSelection
== None
) )
354 return cSelectionTargets
;
357 * Query the selection owner for the TARGETS property
359 wnd
= WIN_FindWndPtr(hWnd
);
360 w
= X11DRV_WND_FindXWindow(wnd
);
361 WIN_ReleaseWndPtr(wnd
);
364 aTargets
= TSXInternAtom(display
, "TARGETS", False
);
366 TRACE("Requesting TARGETS selection for '%s' (owner=%08x)...\n",
367 TSXGetAtomName(display
, selectionCacheSrc
), (unsigned)ownerSelection
);
369 EnterCriticalSection( &X11DRV_CritSection
);
370 XConvertSelection(display
, selectionCacheSrc
, aTargets
,
371 TSXInternAtom(display
, "SELECTION_DATA", False
),
375 * Wait until SelectionNotify is received
379 if( XCheckTypedWindowEvent(display
, w
, SelectionNotify
, &xe
) )
380 if( xe
.xselection
.selection
== selectionCacheSrc
)
383 LeaveCriticalSection( &X11DRV_CritSection
);
385 /* Verify that the selection returned a valid TARGETS property */
386 if ( (xe
.xselection
.target
!= aTargets
)
387 || (xe
.xselection
.property
== None
) )
389 TRACE("\tCould not retrieve TARGETS\n");
390 return cSelectionTargets
;
393 /* Read the TARGETS property contents */
394 if(TSXGetWindowProperty(display
, xe
.xselection
.requestor
, xe
.xselection
.property
,
395 0, 0x3FFF, True
, AnyPropertyType
/*XA_ATOM*/, &atype
, &aformat
,
396 &cSelectionTargets
, &remain
, (unsigned char**)&targetList
) != Success
)
397 TRACE("\tCouldn't read TARGETS property\n");
400 TRACE("\tType %s,Format %d,nItems %ld, Remain %ld\n",
401 TSXGetAtomName(display
,atype
),aformat
,cSelectionTargets
, remain
);
403 * The TARGETS property should have returned us a list of atoms
404 * corresponding to each selection target format supported.
406 if( (atype
== XA_ATOM
|| atype
== aTargets
) && aformat
== 32 )
409 LPWINE_CLIPFORMAT lpFormat
;
411 /* Cache these formats in the clipboard cache */
413 for (i
= 0; i
< cSelectionTargets
; i
++)
415 char *itemFmtName
= TSXGetAtomName(display
, targetList
[i
]);
416 UINT wFormat
= X11DRV_CLIPBOARD_MapPropertyToFormat(itemFmtName
);
419 * If the clipboard format maps to a Windows format, simply store
420 * the atom identifier and record its availablity status
421 * in the clipboard cache.
425 lpFormat
= CLIPBOARD_LookupFormat( wFormat
);
427 /* Don't replace if the property already cached is a native format,
428 * or if a PIXMAP is being replaced by a BITMAP.
430 if (lpFormat
->wDataPresent
&&
431 ( X11DRV_CLIPBOARD_IsNativeProperty(lpFormat
->drvData
)
432 || (lpFormat
->drvData
== XA_PIXMAP
&& targetList
[i
] == XA_BITMAP
) )
435 TRACE("\tAtom# %d: '%s' --> FormatID(%d) %s (Skipped)\n",
436 i
, itemFmtName
, wFormat
, lpFormat
->Name
);
440 lpFormat
->wDataPresent
= 1;
441 lpFormat
->drvData
= targetList
[i
];
442 TRACE("\tAtom# %d: '%s' --> FormatID(%d) %s\n",
443 i
, itemFmtName
, wFormat
, lpFormat
->Name
);
447 TSXFree(itemFmtName
);
451 /* Free the list of targets */
455 return cSelectionTargets
;
458 /**************************************************************************
459 * X11DRV_CLIPBOARD_ReadSelection
460 * Reads the contents of the X selection property into the WINE clipboard cache
461 * converting the selection into a format compatible with the windows clipboard
463 * This method is invoked only to read the contents of a the selection owned
464 * by an external application. i.e. when we do not own the X selection.
466 static BOOL
X11DRV_CLIPBOARD_ReadSelection(UINT wFormat
, Window w
, Atom prop
, Atom reqType
)
468 Atom atype
=AnyPropertyType
;
470 unsigned long nitems
,remain
,itemSize
;
472 unsigned char* val
=NULL
;
473 LPWINE_CLIPFORMAT lpFormat
;
475 HWND hWndClipWindow
= GetOpenClipboardWindow();
481 TRACE("Reading X selection...\n");
483 TRACE("\tretrieving property %s from window %ld into %s\n",
484 TSXGetAtomName(display
,reqType
), (long)w
, TSXGetAtomName(display
,prop
) );
487 * First request a zero length in order to figure out the request size.
489 if(TSXGetWindowProperty(display
,w
,prop
,0,0,False
, AnyPropertyType
/*reqType*/,
490 &atype
, &aformat
, &nitems
, &itemSize
, &val
) != Success
)
492 WARN("\tcouldn't get property size\n");
496 /* Free zero length return data if any */
503 TRACE("\tretrieving %ld bytes...\n", itemSize
* aformat
/8);
504 lRequestLength
= (itemSize
* aformat
/8)/4 + 1;
507 * Retrieve the actual property in the required X format.
509 if(TSXGetWindowProperty(display
,w
,prop
,0,lRequestLength
,False
,AnyPropertyType
/*reqType*/,
510 &atype
, &aformat
, &nitems
, &remain
, &val
) != Success
)
512 WARN("\tcouldn't read property\n");
516 TRACE("\tType %s,Format %d,nitems %ld,remain %ld,value %s\n",
517 atype
? TSXGetAtomName(display
,atype
) : NULL
, aformat
,nitems
,remain
,val
);
521 WARN("\tCouldn't read entire property- selection may be too large! Remain=%ld\n", remain
);
526 * Translate the X property into the appropriate Windows clipboard
527 * format, if possible.
529 if ( (reqType
== XA_STRING
)
530 && (atype
== XA_STRING
) && (aformat
== 8) ) /* treat Unix text as CF_OEMTEXT */
536 TRACE("\tselection is '%s'\n",val
);
538 for(i
=0; i
<= nitems
; i
++)
539 if( val
[i
] == '\n' ) inlcount
++;
543 hText
=GlobalAlloc16(GMEM_MOVEABLE
, nitems
+ inlcount
+ 1);
544 if( (lpstr
= (char*)GlobalLock16(hText
)) )
546 for(i
=0,inlcount
=0; i
<= nitems
; i
++)
548 if( val
[i
] == '\n' ) lpstr
[inlcount
++]='\r';
549 lpstr
[inlcount
++]=val
[i
];
551 GlobalUnlock16(hText
);
559 /* delete previous CF_TEXT and CF_OEMTEXT data */
560 lpFormat
= CLIPBOARD_LookupFormat(CF_TEXT
);
561 if (lpFormat
->wDataPresent
|| lpFormat
->hData16
|| lpFormat
->hData32
)
562 CLIPBOARD_DeleteRecord(lpFormat
, !(hWndClipWindow
));
564 lpFormat
= CLIPBOARD_LookupFormat(CF_OEMTEXT
);
565 if (lpFormat
->wDataPresent
|| lpFormat
->hData16
|| lpFormat
->hData32
)
566 CLIPBOARD_DeleteRecord(lpFormat
, !(hWndClipWindow
));
568 /* Update the CF_OEMTEXT record */
569 lpFormat
->wDataPresent
= 1;
570 lpFormat
->hData32
= 0;
571 lpFormat
->hData16
= hText
;
576 else if ( reqType
== XA_PIXMAP
|| reqType
== XA_BITMAP
) /* treat PIXMAP as CF_DIB or CF_BITMAP */
578 /* Get the first pixmap handle passed to us */
579 Pixmap
*pPixmap
= (Pixmap
*)val
;
580 HANDLE hTargetImage
= NULL
; /* Handle to store the converted bitmap or DIB */
582 if (aformat
!= 32 || nitems
< 1 || atype
!= XA_PIXMAP
583 || (wFormat
!= CF_BITMAP
&& wFormat
!= CF_DIB
))
585 WARN("\tUnimplemented format conversion request\n");
589 if ( wFormat
== CF_BITMAP
)
591 /* For CF_BITMAP requests we must return an HBITMAP */
592 hTargetImage
= X11DRV_BITMAP_CreateBitmapFromPixmap(*pPixmap
, TRUE
);
594 else if (wFormat
== CF_DIB
)
596 HWND hwnd
= GetOpenClipboardWindow();
597 HDC hdc
= GetDC(hwnd
);
599 /* For CF_DIB requests we must return an HGLOBAL storing a packed DIB */
600 hTargetImage
= X11DRV_DIB_CreateDIBFromPixmap(*pPixmap
, hdc
, TRUE
);
602 ReleaseDC(hdc
, hwnd
);
607 WARN("PIXMAP conversion failed!\n" );
611 /* Delete previous clipboard data */
612 lpFormat
= CLIPBOARD_LookupFormat(wFormat
);
613 if (lpFormat
->wDataPresent
&& (lpFormat
->hData16
|| lpFormat
->hData32
))
614 CLIPBOARD_DeleteRecord(lpFormat
, !(hWndClipWindow
));
616 /* Update the clipboard record */
617 lpFormat
->wDataPresent
= 1;
618 lpFormat
->hData32
= hTargetImage
;
619 lpFormat
->hData16
= 0;
624 /* For native properties simply copy the X data without conversion */
625 else if (X11DRV_CLIPBOARD_IsNativeProperty(reqType
)) /* <WCF>* */
627 HANDLE hClipData
= 0;
629 int cBytes
= nitems
* aformat
/8;
633 /* Turn on the DDESHARE flag to enable shared 32 bit memory */
634 hClipData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cBytes
);
635 if( (lpClipData
= GlobalLock(hClipData
)) )
637 memcpy(lpClipData
, val
, cBytes
);
638 GlobalUnlock(hClipData
);
646 /* delete previous clipboard record if any */
647 lpFormat
= CLIPBOARD_LookupFormat(wFormat
);
648 if (lpFormat
->wDataPresent
|| lpFormat
->hData16
|| lpFormat
->hData32
)
649 CLIPBOARD_DeleteRecord(lpFormat
, !(hWndClipWindow
));
651 /* Update the clipboard record */
652 lpFormat
->wDataPresent
= 1;
653 lpFormat
->hData32
= hClipData
;
654 lpFormat
->hData16
= 0;
661 WARN("\tUnimplemented format conversion request\n");
666 /* Delete the property on the window now that we are done
667 * This will send a PropertyNotify event to the selection owner. */
668 TSXDeleteProperty(display
,w
,prop
);
670 /* Free the retrieved property data */
677 /**************************************************************************
678 * X11DRV_CLIPBOARD_ReleaseSelection
680 * Release an XA_PRIMARY or XA_CLIPBOARD selection that we own, in response
681 * to a SelectionClear event.
682 * This can occur in response to another client grabbing the X selection.
683 * If the XA_CLIPBOARD selection is lost, we relinquish XA_PRIMARY as well.
685 void X11DRV_CLIPBOARD_ReleaseSelection(Atom selType
, Window w
, HWND hwnd
)
687 Atom xaClipboard
= TSXInternAtom(display
, "CLIPBOARD", False
);
688 int clearAllSelections
= PROFILE_GetWineIniInt("Clipboard", "ClearAllSelections", 0);
690 /* w is the window that lost the selection
691 * selectionPrevWindow is nonzero if CheckSelection() was called.
694 TRACE("\tevent->window = %08x (sw = %08x, spw=%08x)\n",
695 (unsigned)w
, (unsigned)selectionWindow
, (unsigned)selectionPrevWindow
);
697 if( selectionAcquired
)
699 if( w
== selectionWindow
|| selectionPrevWindow
== None
)
701 /* If we're losing the CLIPBOARD selection, or if the preferences in .winerc
702 * dictate that *all* selections should be cleared on loss of a selection,
703 * we must give up all the selections we own.
705 if ( clearAllSelections
|| (selType
== xaClipboard
) )
707 /* completely give up the selection */
708 TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
710 /* We are completely giving up the selection.
711 * Make sure we can open the windows clipboard first. */
713 if ( !OpenClipboard(hwnd
) )
716 * We can't empty the clipboard if we cant open it so abandon.
717 * Wine will think that it still owns the selection but this is
718 * safer than losing the selection without properly emptying
719 * the clipboard. Perhaps we should forcibly re-assert ownership
720 * of the CLIPBOARD selection in this case...
722 ERR("\tClipboard is busy. Could not give up selection!\n");
726 /* We really lost CLIPBOARD but want to voluntarily lose PRIMARY */
727 if ( (selType
== xaClipboard
)
728 && (selectionAcquired
& S_PRIMARY
) )
730 XSetSelectionOwner(display
, XA_PRIMARY
, None
, CurrentTime
);
733 /* We really lost PRIMARY but want to voluntarily lose CLIPBOARD */
734 if ( (selType
== XA_PRIMARY
)
735 && (selectionAcquired
& S_CLIPBOARD
) )
737 XSetSelectionOwner(display
, xaClipboard
, None
, CurrentTime
);
740 selectionWindow
= None
;
741 PrimarySelectionOwner
= ClipboardSelectionOwner
= 0;
743 /* Empty the windows clipboard.
744 * We should pretend that we still own the selection BEFORE calling
745 * EmptyClipboard() since otherwise this has the side effect of
746 * triggering X11DRV_CLIPBOARD_Acquire() and causing the X selection
747 * to be re-acquired by us!
749 selectionAcquired
= (S_PRIMARY
| S_CLIPBOARD
);
753 /* Give up ownership of the windows clipboard */
754 CLIPBOARD_ReleaseOwner();
756 /* Reset the selection flags now that we are done */
757 selectionAcquired
= S_NOSELECTION
;
759 else if ( selType
== XA_PRIMARY
) /* Give up only PRIMARY selection */
761 TRACE("Lost PRIMARY selection\n");
762 PrimarySelectionOwner
= 0;
763 selectionAcquired
&= ~S_PRIMARY
; /* clear S_PRIMARY mask */
766 cSelectionTargets
= 0;
768 /* but we'll keep existing data for internal use */
769 else if( w
== selectionPrevWindow
)
771 Atom xaClipboard
= TSXInternAtom(display
, _CLIPBOARD
, False
);
773 w
= TSXGetSelectionOwner(display
, XA_PRIMARY
);
775 TSXSetSelectionOwner(display
, XA_PRIMARY
, selectionWindow
, CurrentTime
);
777 w
= TSXGetSelectionOwner(display
, xaClipboard
);
779 TSXSetSelectionOwner(display
, xaClipboard
, selectionWindow
, CurrentTime
);
783 /* Signal to a selectionClearEvent listener if the selection is completely lost */
784 if (selectionClearEvent
&& !selectionAcquired
)
786 TRACE("Lost all selections, signalling to selectionClearEvent listener\n");
787 SetEvent(selectionClearEvent
);
790 selectionPrevWindow
= None
;
793 /**************************************************************************
794 * X11DRV_CLIPBOARD_Empty
795 * Voluntarily release all currently owned X selections
797 void X11DRV_CLIPBOARD_Release()
799 if( selectionAcquired
)
802 Window savePrevWindow
= selectionWindow
;
803 Atom xaClipboard
= TSXInternAtom(display
, _CLIPBOARD
, False
);
804 BOOL bHasPrimarySelection
= selectionAcquired
& S_PRIMARY
;
806 selectionAcquired
= S_NOSELECTION
;
807 selectionPrevWindow
= selectionWindow
;
808 selectionWindow
= None
;
810 TRACE("\tgiving up selection (spw = %08x)\n",
811 (unsigned)selectionPrevWindow
);
813 EnterCriticalSection(&X11DRV_CritSection
);
815 TRACE("Releasing CLIPBOARD selection\n");
816 XSetSelectionOwner(display
, xaClipboard
, None
, CurrentTime
);
817 if( selectionPrevWindow
)
818 while( !XCheckTypedWindowEvent( display
, selectionPrevWindow
,
819 SelectionClear
, &xe
) );
821 if ( bHasPrimarySelection
)
823 TRACE("Releasing XA_PRIMARY selection\n");
824 selectionPrevWindow
= savePrevWindow
; /* May be cleared in X11DRV_CLIPBOARD_ReleaseSelection */
825 XSetSelectionOwner(display
, XA_PRIMARY
, None
, CurrentTime
);
827 if( selectionPrevWindow
)
828 while( !XCheckTypedWindowEvent( display
, selectionPrevWindow
,
829 SelectionClear
, &xe
) );
832 LeaveCriticalSection(&X11DRV_CritSection
);
835 /* Get rid of any Pixmap resources we may still have */
837 DPA_Destroy( PropDPA
);
844 if ( (pixmap
= ((Pixmap
)DPA_GetPtr(PixmapDPA
, i
))) )
845 XFreePixmap(display
, pixmap
);
849 DPA_Destroy( PixmapDPA
);
851 PixmapDPA
= PropDPA
= NULL
;
854 /**************************************************************************
855 * X11DRV_CLIPBOARD_Acquire()
857 void X11DRV_CLIPBOARD_Acquire()
860 HWND hWndClipWindow
= GetOpenClipboardWindow();
863 * Acquire X selection if we don't already own it.
864 * Note that we only acquire the selection if it hasn't been already
865 * acquired by us, and ignore the fact that another X window may be
866 * asserting ownership. The reason for this is we need *any* top level
867 * X window to hold selection ownership. The actual clipboard data requests
868 * are made via GetClipboardData from EVENT_SelectionRequest and this
869 * ensures that the real HWND owner services the request.
870 * If the owning X window gets destroyed the selection ownership is
871 * re-cycled to another top level X window in X11DRV_CLIPBOARD_ResetOwner.
875 if ( !(selectionAcquired
== (S_PRIMARY
| S_CLIPBOARD
)) )
877 Atom xaClipboard
= TSXInternAtom(display
, _CLIPBOARD
, False
);
878 WND
*tmpWnd
= WIN_FindWndPtr( hWndClipWindow
? hWndClipWindow
: AnyPopup() );
879 owner
= X11DRV_WND_FindXWindow(tmpWnd
);
880 WIN_ReleaseWndPtr(tmpWnd
);
882 /* Grab PRIMARY selection if not owned */
883 if ( !(selectionAcquired
& S_PRIMARY
) )
884 TSXSetSelectionOwner(display
, XA_PRIMARY
, owner
, CurrentTime
);
886 /* Grab CLIPBOARD selection if not owned */
887 if ( !(selectionAcquired
& S_CLIPBOARD
) )
888 TSXSetSelectionOwner(display
, xaClipboard
, owner
, CurrentTime
);
890 if( TSXGetSelectionOwner(display
,XA_PRIMARY
) == owner
)
891 selectionAcquired
|= S_PRIMARY
;
893 if( TSXGetSelectionOwner(display
,xaClipboard
) == owner
)
894 selectionAcquired
|= S_CLIPBOARD
;
896 if (selectionAcquired
)
898 /* Create dynamic pointer arrays to manage Pixmap resources we may expose */
900 PropDPA
= DPA_CreateEx( 2, SystemHeap
);
902 PixmapDPA
= DPA_CreateEx( 2, SystemHeap
);
904 selectionWindow
= owner
;
905 TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner
);
910 /**************************************************************************
911 * X11DRV_CLIPBOARD_IsFormatAvailable
913 * Checks if the specified format is available in the current selection
914 * Only invoked when WINE is not the selection owner
916 BOOL
X11DRV_CLIPBOARD_IsFormatAvailable(UINT wFormat
)
918 Atom xaClipboard
= TSXInternAtom(display
, _CLIPBOARD
, False
);
919 Window ownerPrimary
= TSXGetSelectionOwner(display
,XA_PRIMARY
);
920 Window ownerClipboard
= TSXGetSelectionOwner(display
,xaClipboard
);
923 * If the selection has not been previously cached, or the selection has changed,
924 * try and cache the list of available selection targets from the current selection.
926 if ( !cSelectionTargets
|| (PrimarySelectionOwner
!= ownerPrimary
)
927 || (ClipboardSelectionOwner
!= ownerClipboard
) )
930 * First try cacheing the CLIPBOARD selection.
931 * If unavailable try PRIMARY.
933 if ( X11DRV_CLIPBOARD_CacheDataFormats(xaClipboard
) == 0 )
935 X11DRV_CLIPBOARD_CacheDataFormats(XA_PRIMARY
);
938 ClipboardSelectionOwner
= ownerClipboard
;
939 PrimarySelectionOwner
= ownerPrimary
;
942 /* Exit if there is no selection */
943 if ( !ownerClipboard
&& !ownerPrimary
)
946 if ( wFormat
== CF_TEXT
)
947 wFormat
= CF_OEMTEXT
;
949 /* Check if the format is available in the clipboard cache */
950 if ( CLIPBOARD_IsPresent(wFormat
) )
954 * Many X client apps (such as XTerminal) don't support being queried
955 * for the "TARGETS" target atom. To handle such clients we must actually
956 * try to convert the selection to the requested type.
958 if ( !cSelectionTargets
)
959 return X11DRV_CLIPBOARD_GetData( wFormat
);
964 /**************************************************************************
965 * X11DRV_CLIPBOARD_RegisterFormat
967 * Registers a custom X clipboard format
968 * Returns: TRUE - success, FALSE - failure
970 BOOL
X11DRV_CLIPBOARD_RegisterFormat( LPCSTR FormatName
)
976 * If an X atom is registered for this format, return that
977 * Otherwise register a new atom.
981 /* Add a WINE specific prefix to the format */
982 strcpy(str
, FMT_PREFIX
);
983 strncat(str
, FormatName
, sizeof(str
) - strlen(FMT_PREFIX
));
984 prop
= TSXInternAtom(display
, str
, False
);
987 return (prop
) ? TRUE
: FALSE
;
990 /**************************************************************************
991 * X11DRV_CLIPBOARD_IsSelectionowner
993 * Returns: TRUE - We(WINE) own the selection, FALSE - Selection not owned by us
995 BOOL
X11DRV_CLIPBOARD_IsSelectionowner()
997 return selectionAcquired
;
1000 /**************************************************************************
1001 * X11DRV_CLIPBOARD_SetData
1003 * We don't need to do anything special here since the clipboard code
1004 * maintains the cache.
1007 void X11DRV_CLIPBOARD_SetData(UINT wFormat
)
1009 /* Make sure we have acquired the X selection */
1010 X11DRV_CLIPBOARD_Acquire();
1013 /**************************************************************************
1014 * X11DRV_CLIPBOARD_GetData
1016 * This method is invoked only when we DO NOT own the X selection
1018 * NOTE: Clipboard driver doesn't get requests for CF_TEXT data, only
1020 * We always get the data from the selection client each time,
1021 * since we have no way of determining if the data in our cache is stale.
1023 BOOL
X11DRV_CLIPBOARD_GetData(UINT wFormat
)
1025 BOOL bRet
= selectionAcquired
;
1026 HWND hWndClipWindow
= GetOpenClipboardWindow();
1027 HWND hWnd
= (hWndClipWindow
) ? hWndClipWindow
: GetActiveWindow();
1029 LPWINE_CLIPFORMAT lpFormat
;
1031 if( !selectionAcquired
&& (wnd
= WIN_FindWndPtr(hWnd
)) )
1035 Window w
= X11DRV_WND_FindXWindow(wnd
);
1036 WIN_ReleaseWndPtr(wnd
);
1039 /* Map the format ID requested to an X selection property.
1040 * If the format is in the cache, use the atom associated
1044 lpFormat
= CLIPBOARD_LookupFormat( wFormat
);
1045 if (lpFormat
&& lpFormat
->wDataPresent
&& lpFormat
->drvData
)
1046 propRequest
= (Atom
)lpFormat
->drvData
;
1048 propRequest
= X11DRV_CLIPBOARD_MapFormatToProperty(wFormat
);
1052 TRACE("Requesting %s selection from %s...\n",
1053 TSXGetAtomName(display
, propRequest
),
1054 TSXGetAtomName(display
, selectionCacheSrc
) );
1056 EnterCriticalSection( &X11DRV_CritSection
);
1057 XConvertSelection(display
, selectionCacheSrc
, propRequest
,
1058 TSXInternAtom(display
, "SELECTION_DATA", False
),
1061 /* wait until SelectionNotify is received */
1065 if( XCheckTypedWindowEvent(display
, w
, SelectionNotify
, &xe
) )
1066 if( xe
.xselection
.selection
== selectionCacheSrc
)
1069 LeaveCriticalSection( &X11DRV_CritSection
);
1072 * Read the contents of the X selection property into WINE's
1073 * clipboard cache converting the selection to be compatible if possible.
1075 bRet
= X11DRV_CLIPBOARD_ReadSelection( wFormat
,
1076 xe
.xselection
.requestor
,
1077 xe
.xselection
.property
,
1078 xe
.xselection
.target
);
1083 TRACE("\tpresent %s = %i\n", CLIPBOARD_GetFormatName(wFormat
), bRet
);
1089 /**************************************************************************
1090 * X11DRV_CLIPBOARD_ResetOwner
1092 * Called from DestroyWindow() to prevent X selection from being lost when
1093 * a top level window is destroyed, by switching ownership to another top
1095 * Any top level window can own the selection. See X11DRV_CLIPBOARD_Acquire
1096 * for a more detailed description of this.
1098 void X11DRV_CLIPBOARD_ResetOwner(WND
*pWnd
, BOOL bFooBar
)
1100 HWND hWndClipOwner
= 0;
1101 Window XWnd
= X11DRV_WND_GetXWindow(pWnd
);
1103 BOOL bLostSelection
= FALSE
;
1105 /* There is nothing to do if we don't own the selection,
1106 * or if the X window which currently owns the selecion is different
1107 * from the one passed in.
1109 if ( !selectionAcquired
|| XWnd
!= selectionWindow
1110 || selectionWindow
== None
)
1113 if ( (bFooBar
&& XWnd
) || (!bFooBar
&& !XWnd
) )
1116 hWndClipOwner
= GetClipboardOwner();
1117 xaClipboard
= TSXInternAtom(display
, _CLIPBOARD
, False
);
1119 TRACE("clipboard owner = %04x, selection window = %08x\n",
1120 hWndClipOwner
, (unsigned)selectionWindow
);
1122 /* now try to salvage current selection from being destroyed by X */
1124 TRACE("\tchecking %08x\n", (unsigned) XWnd
);
1126 selectionPrevWindow
= selectionWindow
;
1127 selectionWindow
= None
;
1130 selectionWindow
= X11DRV_WND_GetXWindow(pWnd
->next
);
1131 else if( pWnd
->parent
)
1132 if( pWnd
->parent
->child
!= pWnd
)
1133 selectionWindow
= X11DRV_WND_GetXWindow(pWnd
->parent
->child
);
1135 if( selectionWindow
!= None
)
1137 /* We must pretend that we don't own the selection while making the switch
1138 * since a SelectionClear event will be sent to the last owner.
1139 * If there is no owner X11DRV_CLIPBOARD_ReleaseSelection will do nothing.
1141 int saveSelectionState
= selectionAcquired
;
1142 selectionAcquired
= False
;
1144 TRACE("\tswitching selection from %08x to %08x\n",
1145 (unsigned)selectionPrevWindow
, (unsigned)selectionWindow
);
1147 /* Assume ownership for the PRIMARY and CLIPBOARD selection */
1148 if ( saveSelectionState
& S_PRIMARY
)
1149 TSXSetSelectionOwner(display
, XA_PRIMARY
, selectionWindow
, CurrentTime
);
1151 TSXSetSelectionOwner(display
, xaClipboard
, selectionWindow
, CurrentTime
);
1153 /* Restore the selection masks */
1154 selectionAcquired
= saveSelectionState
;
1156 /* Lose the selection if something went wrong */
1157 if ( ( (saveSelectionState
& S_PRIMARY
) &&
1158 (TSXGetSelectionOwner(display
, XA_PRIMARY
) != selectionWindow
) )
1159 || (TSXGetSelectionOwner(display
, xaClipboard
) != selectionWindow
) )
1161 bLostSelection
= TRUE
;
1166 /* Update selection state */
1167 if (saveSelectionState
& S_PRIMARY
)
1168 PrimarySelectionOwner
= selectionWindow
;
1170 ClipboardSelectionOwner
= selectionWindow
;
1175 bLostSelection
= TRUE
;
1182 /* Launch the clipboard server if the selection can no longer be recyled
1183 * to another top level window. */
1185 if ( !X11DRV_CLIPBOARD_LaunchServer() )
1187 /* Empty the windows clipboard if the server was not launched.
1188 * We should pretend that we still own the selection BEFORE calling
1189 * EmptyClipboard() since otherwise this has the side effect of
1190 * triggering X11DRV_CLIPBOARD_Acquire() and causing the X selection
1191 * to be re-acquired by us!
1194 TRACE("\tLost the selection! Emptying the clipboard...\n");
1196 OpenClipboard(NULL
);
1197 selectionAcquired
= (S_PRIMARY
| S_CLIPBOARD
);
1202 /* Give up ownership of the windows clipboard */
1203 CLIPBOARD_ReleaseOwner();
1206 selectionAcquired
= S_NOSELECTION
;
1207 ClipboardSelectionOwner
= PrimarySelectionOwner
= 0;
1208 selectionWindow
= 0;
1212 /**************************************************************************
1213 * X11DRV_CLIPBOARD_RegisterPixmapResource
1214 * Registers a Pixmap resource which is to be associated with a property Atom.
1215 * When the property is destroyed we also destroy the Pixmap through the
1216 * PropertyNotify event.
1218 BOOL
X11DRV_CLIPBOARD_RegisterPixmapResource( Atom property
, Pixmap pixmap
)
1220 if ( -1 == DPA_InsertPtr( PropDPA
, 0, (void*)property
) )
1223 if ( -1 == DPA_InsertPtr( PixmapDPA
, 0, (void*)pixmap
) )
1229 /**************************************************************************
1230 * X11DRV_CLIPBOARD_FreeResources
1232 * Called from EVENT_PropertyNotify() to give us a chance to destroy
1233 * any resources associated with this property.
1235 void X11DRV_CLIPBOARD_FreeResources( Atom property
)
1237 /* Do a simple linear search to see if we have a Pixmap resource
1238 * associated with this property and release it.
1242 Atom cacheProp
= NULL
;
1245 if ( !(cacheProp
= ((Atom
)DPA_GetPtr(PropDPA
, i
))) )
1248 if ( cacheProp
== property
)
1250 /* Lookup the associated Pixmap and free it */
1251 pixmap
= (Pixmap
)DPA_GetPtr(PixmapDPA
, i
);
1253 TRACE("Releasing pixmap %ld for Property %s\n",
1254 (long)pixmap
, TSXGetAtomName(display
, cacheProp
));
1256 XFreePixmap(display
, pixmap
);
1258 /* Free the entries from the table */
1259 DPA_DeletePtr(PropDPA
, i
);
1260 DPA_DeletePtr(PixmapDPA
, i
);
1265 #endif /* !defined(X_DISPLAY_MISSING) */