2 * X11 clipboard windows driver
4 * Copyright 1994 Martin Ayotte
7 * 2003 Ulrich Czekalla for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * This file contains the X specific implementation for the windows
27 * Wine's internal clipboard is exposed to external apps via the X
28 * selection mechanism.
29 * Currently the driver asserts ownership via two selection atoms:
30 * 1. PRIMARY(XA_PRIMARY)
33 * In our implementation, the CLIPBOARD selection takes precedence over PRIMARY,
34 * i.e. if a CLIPBOARD selection is available, it is used instead of PRIMARY.
35 * When Wine takes ownership of the clipboard, it takes ownership of BOTH selections.
36 * While giving up selection ownership, if the CLIPBOARD selection is lost,
37 * it will lose both PRIMARY and CLIPBOARD and empty the clipboard.
38 * However if only PRIMARY is lost, it will continue to hold the CLIPBOARD selection
39 * (leaving the clipboard cache content unaffected).
41 * Every format exposed via a windows clipboard format is also exposed through
42 * a corresponding X selection target. A selection target atom is synthesized
43 * whenever a new Windows clipboard format is registered via RegisterClipboardFormat,
44 * or when a built-in format is used for the first time.
45 * Windows native format are exposed by prefixing the format name with "<WCF>"
46 * This allows us to uniquely identify windows native formats exposed by other
49 * In order to allow external applications to query WINE for supported formats,
50 * we respond to the "TARGETS" selection target. (See EVENT_SelectionRequest
51 * for implementation) We use the same mechanism to query external clients for
52 * availability of a particular format, by caching the list of available targets
53 * by using the clipboard cache's "delayed render" mechanism. If a selection client
54 * does not support the "TARGETS" selection target, we actually attempt to retrieve
55 * the format requested as a fallback mechanism.
57 * Certain Windows native formats are automatically converted to X native formats
58 * and vice versa. If a native format is available in the selection, it takes
59 * precedence, in order to avoid unnecessary conversions.
61 * FIXME: global format list needs a critical section
65 #include "wine/port.h"
80 #include "wine/wingdi16.h"
83 #include "wine/debug.h"
84 #include "wine/unicode.h"
85 #include "wine/server.h"
87 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
89 #define HGDIOBJ_32(handle16) ((HGDIOBJ)(ULONG_PTR)(handle16))
91 /* Maximum wait time for selection notify */
92 #define SELECTION_RETRIES 500 /* wait for .1 seconds */
93 #define SELECTION_WAIT 1000 /* us */
94 /* Minimum seconds that must lapse between owner queries */
95 #define OWNERQUERYLAPSETIME 1
98 #define S_NOSELECTION 0
100 #define S_CLIPBOARD 2
109 } CLIPBOARDINFO, *LPCLIPBOARDINFO;
111 static int selectionAcquired = 0; /* Contains the current selection masks */
112 static Window selectionWindow = None; /* The top level X window which owns the selection */
113 static BOOL clearAllSelections = FALSE; /* Always lose all selections */
114 static BOOL usePrimary = FALSE; /* Use primary selection in additon to the clipboard selection */
115 static Atom selectionCacheSrc = XA_PRIMARY; /* The selection source from which the clipboard cache was filled */
116 static Window PrimarySelectionOwner = None; /* The window which owns the primary selection */
117 static Window ClipboardSelectionOwner = None; /* The window which owns the clipboard selection */
119 INT X11DRV_RegisterClipboardFormat(LPCSTR FormatName);
120 void X11DRV_EmptyClipboard(void);
121 void X11DRV_EndClipboardUpdate(void);
122 HANDLE X11DRV_CLIPBOARD_ImportClipboardData(LPBYTE lpdata, UINT cBytes);
123 HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(LPBYTE lpdata, UINT cBytes);
124 HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(LPBYTE lpdata, UINT cBytes);
125 HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(LPBYTE lpdata, UINT cBytes);
126 HANDLE X11DRV_CLIPBOARD_ImportXAString(LPBYTE lpdata, UINT cBytes);
127 HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Window requestor, Atom aTarget,
128 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
129 HANDLE X11DRV_CLIPBOARD_ExportString(Window requestor, Atom aTarget,
130 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
131 HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Window requestor, Atom aTarget,
132 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
133 HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Window requestor, Atom aTarget,
134 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
135 HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Window requestor, Atom aTarget,
136 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
137 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(LPCSTR FormatName, Atom prop);
138 static BOOL X11DRV_CLIPBOARD_ReadSelection(LPWINE_CLIPFORMAT lpData, Window w, Atom prop);
139 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(UINT wFormatID);
140 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData);
141 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void);
142 static int X11DRV_CLIPBOARD_QueryAvailableData(LPCLIPBOARDINFO lpcbinfo);
143 static BOOL X11DRV_CLIPBOARD_ReadClipboardData(UINT wFormat);
144 static BOOL X11DRV_CLIPBOARD_RenderFormat(LPWINE_CLIPDATA lpData);
145 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out);
146 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID);
147 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(LPWINE_CLIPDATA lpData);
150 * WARNING: This data ordering is dependent on the WINE_CLIPFORMAT structure
151 * declared in clipboard.h
153 static WINE_CLIPFORMAT ClipFormats[] =
155 { CF_TEXT, "WCF_TEXT", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
156 X11DRV_CLIPBOARD_ExportClipboardData, NULL, &ClipFormats[1]},
158 { CF_BITMAP, "WCF_BITMAP", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
159 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[0], &ClipFormats[2]},
161 { CF_METAFILEPICT, "WCF_METAFILEPICT", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportMetaFilePict,
162 X11DRV_CLIPBOARD_ExportMetaFilePict, &ClipFormats[1], &ClipFormats[3]},
164 { CF_SYLK, "WCF_SYLK", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
165 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[2], &ClipFormats[4]},
167 { CF_DIF, "WCF_DIF", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
168 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[3], &ClipFormats[5]},
170 { CF_TIFF, "WCF_TIFF", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
171 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[4], &ClipFormats[6]},
173 { CF_OEMTEXT, "WCF_OEMTEXT", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
174 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[5], &ClipFormats[7]},
176 { CF_DIB, "WCF_DIB", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportXAPIXMAP,
177 X11DRV_CLIPBOARD_ExportXAPIXMAP, &ClipFormats[6], &ClipFormats[8]},
179 { CF_PALETTE, "WCF_PALETTE", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
180 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[7], &ClipFormats[9]},
182 { CF_PENDATA, "WCF_PENDATA", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
183 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[8], &ClipFormats[10]},
185 { CF_RIFF, "WCF_RIFF", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
186 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[9], &ClipFormats[11]},
188 { CF_WAVE, "WCF_WAVE", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
189 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[10], &ClipFormats[12]},
191 { CF_UNICODETEXT, "WCF_UNICODETEXT", XA_STRING, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportXAString,
192 X11DRV_CLIPBOARD_ExportString, &ClipFormats[11], &ClipFormats[13]},
194 { CF_ENHMETAFILE, "WCF_ENHMETAFILE", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportEnhMetaFile,
195 X11DRV_CLIPBOARD_ExportEnhMetaFile, &ClipFormats[12], &ClipFormats[14]},
197 { CF_HDROP, "WCF_HDROP", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
198 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[13], &ClipFormats[15]},
200 { CF_LOCALE, "WCF_LOCALE", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
201 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[14], &ClipFormats[16]},
203 { CF_DIBV5, "WCF_DIBV5", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
204 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[15], &ClipFormats[17]},
206 { CF_OWNERDISPLAY, "WCF_OWNERDISPLAY", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
207 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[16], &ClipFormats[18]},
209 { CF_DSPTEXT, "WCF_DSPTEXT", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
210 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[17], &ClipFormats[19]},
212 { CF_DSPBITMAP, "WCF_DSPBITMAP", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
213 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[18], &ClipFormats[20]},
215 { CF_DSPMETAFILEPICT, "WCF_DSPMETAFILEPICT", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
216 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[19], &ClipFormats[21]},
218 { CF_DSPENHMETAFILE, "WCF_DSPENHMETAFILE", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
219 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[20], NULL}
222 #define GET_ATOM(prop) (((prop) < FIRST_XATOM) ? (Atom)(prop) : X11DRV_Atoms[(prop) - FIRST_XATOM])
224 /* Maps X properties to Windows formats */
229 } PropertyFormatMap[] =
231 { "Rich Text Format", XATOM_text_rtf },
232 /* Temporarily disable text/html because Evolution incorrectly pastes strings with extra nulls */
233 /*{ "text/html", "HTML Format" },*/
234 { "GIF", XATOM_image_gif }
238 /* Maps equivalent X properties. It is assumed that lpszProperty must already
239 be in ClipFormats or PropertyFormatMap. */
242 UINT drvDataProperty;
244 } PropertyAliasMap[] =
246 /* DataProperty, DataAlias */
247 { XATOM_text_rtf, XATOM_text_richtext },
248 { XA_STRING, XATOM_COMPOUND_TEXT },
249 { XA_STRING, XATOM_TEXT },
254 * Cached clipboard data.
256 static LPWINE_CLIPDATA ClipData = NULL;
257 static UINT ClipDataCount = 0;
260 * Clipboard sequence number
262 static UINT wSeqNo = 0;
264 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
266 /**************************************************************************
267 * Internal Clipboard implementation methods
268 **************************************************************************/
270 /**************************************************************************
271 * X11DRV_InitClipboard
273 void X11DRV_InitClipboard(void)
278 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Clipboard", &hkey))
281 DWORD type, count = sizeof(buffer);
282 if(!RegQueryValueExA(hkey, "ClearAllSelections", 0, &type, buffer, &count))
283 clearAllSelections = IS_OPTION_TRUE( buffer[0] );
284 count = sizeof(buffer);
285 if(!RegQueryValueExA(hkey, "UsePrimary", 0, &type, buffer, &count))
286 usePrimary = IS_OPTION_TRUE( buffer[0] );
290 /* Register known mapping between window formats and X properties */
291 for (i = 0; i < sizeof(PropertyFormatMap)/sizeof(PropertyFormatMap[0]); i++)
292 X11DRV_CLIPBOARD_InsertClipboardFormat(PropertyFormatMap[i].lpszFormat,
293 GET_ATOM(PropertyFormatMap[i].prop));
297 /**************************************************************************
300 * Intern atoms for formats that don't have one yet.
302 static void intern_atoms(void)
304 LPWINE_CLIPFORMAT format;
309 for (format = ClipFormats, count = 0; format; format = format->NextFormat)
310 if (!format->drvData) count++;
313 names = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*names) );
314 atoms = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*atoms) );
316 for (format = ClipFormats, i = 0; format; format = format->NextFormat)
317 if (!format->drvData) names[i++] = format->Name;
320 XInternAtoms( thread_display(), names, count, False, atoms );
323 for (format = ClipFormats, i = 0; format; format = format->NextFormat)
324 if (!format->drvData) format->drvData = atoms[i++];
326 HeapFree( GetProcessHeap(), 0, names );
327 HeapFree( GetProcessHeap(), 0, atoms );
331 /**************************************************************************
334 * Register a custom X clipboard format.
336 static WINE_CLIPFORMAT *register_format( LPCSTR FormatName, Atom prop )
338 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
340 TRACE("'%s'\n", FormatName);
342 /* walk format chain to see if it's already registered */
345 if ( !strcasecmp(lpFormat->Name, FormatName) &&
346 (lpFormat->wFlags & CF_FLAG_BUILTINFMT) == 0)
348 lpFormat = lpFormat->NextFormat;
351 return X11DRV_CLIPBOARD_InsertClipboardFormat(FormatName, prop);
355 /**************************************************************************
356 * X11DRV_CLIPBOARD_LookupFormat
358 LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupFormat(WORD wID)
360 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
364 if (lpFormat->wFormatID == wID)
367 lpFormat = lpFormat->NextFormat;
369 if (!lpFormat->drvData) intern_atoms();
374 /**************************************************************************
375 * X11DRV_CLIPBOARD_LookupProperty
377 LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupProperty(UINT drvData)
381 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
382 BOOL need_intern = FALSE;
386 if (lpFormat->drvData == drvData) return lpFormat;
387 if (!lpFormat->drvData) need_intern = TRUE;
388 lpFormat = lpFormat->NextFormat;
390 if (!need_intern) return NULL;
392 /* restart the search for the new atoms */
397 /**************************************************************************
398 * X11DRV_CLIPBOARD_LookupAliasProperty
400 LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupAliasProperty(UINT drvDataAlias)
403 LPWINE_CLIPFORMAT lpFormat = NULL;
405 for (i = 0; i < sizeof(PropertyAliasMap)/sizeof(PropertyAliasMap[0]); i++)
407 if (GET_ATOM(PropertyAliasMap[i].drvDataAlias) == drvDataAlias)
409 lpFormat = X11DRV_CLIPBOARD_LookupProperty(GET_ATOM(PropertyAliasMap[i].drvDataProperty));
418 /**************************************************************************
419 * X11DRV_CLIPBOARD_LookupPropertyAlias
421 UINT X11DRV_CLIPBOARD_LookupPropertyAlias(UINT drvDataProperty)
426 for (i = 0; i < sizeof(PropertyAliasMap)/sizeof(PropertyAliasMap[0]); i++)
428 if (GET_ATOM(PropertyAliasMap[i].drvDataProperty) == drvDataProperty)
430 alias = GET_ATOM(PropertyAliasMap[i].drvDataAlias);
439 /**************************************************************************
440 * X11DRV_CLIPBOARD_LookupData
442 LPWINE_CLIPDATA X11DRV_CLIPBOARD_LookupData(DWORD wID)
444 LPWINE_CLIPDATA lpData = ClipData;
450 if (lpData->wFormatID == wID)
453 lpData = lpData->NextData;
455 while(lpData != ClipData);
457 if (lpData->wFormatID != wID)
465 /**************************************************************************
466 * InsertClipboardFormat
468 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(LPCSTR FormatName, Atom prop)
470 LPWINE_CLIPFORMAT lpFormat;
471 LPWINE_CLIPFORMAT lpNewFormat;
473 /* allocate storage for new format entry */
474 lpNewFormat = (LPWINE_CLIPFORMAT) HeapAlloc(GetProcessHeap(),
475 0, sizeof(WINE_CLIPFORMAT));
477 if(lpNewFormat == NULL)
479 WARN("No more memory for a new format!\n");
483 if (!(lpNewFormat->Name = HeapAlloc(GetProcessHeap(), 0, strlen(FormatName)+1)))
485 WARN("No more memory for the new format name!\n");
486 HeapFree(GetProcessHeap(), 0, lpNewFormat);
490 strcpy(lpNewFormat->Name, FormatName);
491 lpNewFormat->wFlags = 0;
492 lpNewFormat->wFormatID = GlobalAddAtomA(lpNewFormat->Name);
493 lpNewFormat->drvData = prop;
494 lpNewFormat->lpDrvImportFunc = X11DRV_CLIPBOARD_ImportClipboardData;
495 lpNewFormat->lpDrvExportFunc = X11DRV_CLIPBOARD_ExportClipboardData;
498 lpFormat = ClipFormats;
500 while(lpFormat->NextFormat) /* Move to last entry */
501 lpFormat = lpFormat->NextFormat;
503 lpNewFormat->NextFormat = NULL;
504 lpFormat->NextFormat = lpNewFormat;
505 lpNewFormat->PrevFormat = lpFormat;
507 TRACE("Registering format(%d): %s drvData %d\n",
508 lpNewFormat->wFormatID, FormatName, lpNewFormat->drvData);
516 /**************************************************************************
517 * X11DRV_CLIPBOARD_GetClipboardInfo
519 static BOOL X11DRV_CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
523 SERVER_START_REQ( set_clipboard_info )
527 if (wine_server_call_err( req ))
529 ERR("Failed to get clipboard owner.\n");
533 cbInfo->hWndOpen = reply->old_clipboard;
534 cbInfo->hWndOwner = reply->old_owner;
535 cbInfo->hWndViewer = reply->old_viewer;
536 cbInfo->seqno = reply->seqno;
537 cbInfo->flags = reply->flags;
548 /**************************************************************************
549 * X11DRV_CLIPBOARD_ReleaseOwnership
551 static BOOL X11DRV_CLIPBOARD_ReleaseOwnership(void)
555 SERVER_START_REQ( set_clipboard_info )
557 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
559 if (wine_server_call_err( req ))
561 ERR("Failed to set clipboard.\n");
575 /**************************************************************************
576 * X11DRV_CLIPBOARD_InsertClipboardData
578 * Caller *must* have the clipboard open and be the owner.
580 static BOOL X11DRV_CLIPBOARD_InsertClipboardData(UINT wFormat, HANDLE16 hData16, HANDLE hData32, DWORD flags)
582 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormat);
584 TRACE("format=%d lpData=%p hData16=%08x hData32=%08x flags=0x%08lx\n",
585 wFormat, lpData, hData16, (unsigned int)hData32, flags);
589 X11DRV_CLIPBOARD_FreeData(lpData);
591 lpData->hData16 = hData16; /* 0 is legal, see WM_RENDERFORMAT */
592 lpData->hData32 = hData32;
596 lpData = (LPWINE_CLIPDATA) HeapAlloc(GetProcessHeap(),
597 0, sizeof(WINE_CLIPDATA));
599 lpData->wFormatID = wFormat;
600 lpData->hData16 = hData16; /* 0 is legal, see WM_RENDERFORMAT */
601 lpData->hData32 = hData32;
605 LPWINE_CLIPDATA lpPrevData = ClipData->PrevData;
607 lpData->PrevData = lpPrevData;
608 lpData->NextData = ClipData;
610 lpPrevData->NextData = lpData;
611 ClipData->PrevData = lpData;
615 lpData->NextData = lpData;
616 lpData->PrevData = lpData;
623 lpData->wFlags = flags;
629 /**************************************************************************
630 * X11DRV_CLIPBOARD_FreeData
632 * Free clipboard data handle.
634 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData)
636 TRACE("%d\n", lpData->wFormatID);
638 if ((lpData->wFormatID >= CF_GDIOBJFIRST &&
639 lpData->wFormatID <= CF_GDIOBJLAST) ||
640 lpData->wFormatID == CF_BITMAP ||
641 lpData->wFormatID == CF_DIB ||
642 lpData->wFormatID == CF_PALETTE)
645 DeleteObject(lpData->hData32);
648 DeleteObject(HGDIOBJ_32(lpData->hData16));
650 else if (lpData->wFormatID == CF_METAFILEPICT)
654 DeleteMetaFile(((METAFILEPICT *)GlobalLock( lpData->hData32 ))->hMF );
655 GlobalFree(lpData->hData32);
658 /* HMETAFILE16 and HMETAFILE32 are apparently the same thing,
659 and a shallow copy is enough to share a METAFILEPICT
660 structure between 16bit and 32bit clipboards. The MetaFile
661 should of course only be deleted once. */
662 GlobalFree16(lpData->hData16);
667 METAFILEPICT16* lpMetaPict = (METAFILEPICT16 *) GlobalLock16(lpData->hData16);
671 DeleteMetaFile16(lpMetaPict->hMF);
675 GlobalFree16(lpData->hData16);
678 else if (lpData->wFormatID == CF_ENHMETAFILE)
681 DeleteEnhMetaFile(lpData->hData32);
683 else if (lpData->wFormatID < CF_PRIVATEFIRST ||
684 lpData->wFormatID > CF_PRIVATELAST)
687 GlobalFree(lpData->hData32);
690 GlobalFree16(lpData->hData16);
698 /**************************************************************************
699 * X11DRV_CLIPBOARD_UpdateCache
701 static BOOL X11DRV_CLIPBOARD_UpdateCache(LPCLIPBOARDINFO lpcbinfo)
705 if (!X11DRV_CLIPBOARD_IsSelectionOwner())
707 if (!X11DRV_CLIPBOARD_GetClipboardInfo(lpcbinfo))
709 ERR("Failed to retrieve clipboard information.\n");
712 else if (wSeqNo < lpcbinfo->seqno)
714 X11DRV_EmptyClipboard();
716 if (X11DRV_CLIPBOARD_QueryAvailableData(lpcbinfo) < 0)
718 ERR("Failed to cache clipboard data owned by another process.\n");
723 X11DRV_EndClipboardUpdate();
726 wSeqNo = lpcbinfo->seqno;
734 /**************************************************************************
735 * X11DRV_CLIPBOARD_RenderFormat
737 static BOOL X11DRV_CLIPBOARD_RenderFormat(LPWINE_CLIPDATA lpData)
741 TRACE(" 0x%04x hData32(0x%08x) hData16(0x%08x)\n",
742 lpData->wFormatID, (unsigned int)lpData->hData32, lpData->hData16);
744 if (lpData->hData32 || lpData->hData16)
745 return bret; /* Already rendered */
747 if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
748 bret = X11DRV_CLIPBOARD_RenderSynthesizedFormat(lpData);
749 else if (!X11DRV_CLIPBOARD_IsSelectionOwner())
751 if (!X11DRV_CLIPBOARD_ReadClipboardData(lpData->wFormatID))
753 ERR("Failed to cache clipboard data owned by another process. Format=%d\n",
760 CLIPBOARDINFO cbInfo;
762 if (X11DRV_CLIPBOARD_GetClipboardInfo(&cbInfo) && cbInfo.hWndOwner)
764 /* Send a WM_RENDERFORMAT message to notify the owner to render the
765 * data requested into the clipboard.
767 TRACE("Sending WM_RENDERFORMAT message to hwnd(%p)\n", cbInfo.hWndOwner);
768 SendMessageW(cbInfo.hWndOwner, WM_RENDERFORMAT, (WPARAM)lpData->wFormatID, 0);
770 if (!lpData->hData32 && !lpData->hData16)
775 ERR("hWndClipOwner is lost!\n");
784 /**************************************************************************
785 * CLIPBOARD_ConvertText
786 * Returns number of required/converted characters - not bytes!
788 static INT CLIPBOARD_ConvertText(WORD src_fmt, void const *src, INT src_size,
789 WORD dst_fmt, void *dst, INT dst_size)
793 if(src_fmt == CF_UNICODETEXT)
806 return WideCharToMultiByte(cp, 0, src, src_size, dst, dst_size, NULL, NULL);
809 if(dst_fmt == CF_UNICODETEXT)
822 return MultiByteToWideChar(cp, 0, src, src_size, dst, dst_size);
825 if(!dst_size) return src_size;
827 if(dst_size > src_size) dst_size = src_size;
829 if(src_fmt == CF_TEXT )
830 CharToOemBuffA(src, dst, dst_size);
832 OemToCharBuffA(src, dst, dst_size);
838 /**************************************************************************
839 * X11DRV_CLIPBOARD_RenderSynthesizedFormat
841 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(LPWINE_CLIPDATA lpData)
847 if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
849 UINT wFormatID = lpData->wFormatID;
851 if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
852 bret = X11DRV_CLIPBOARD_RenderSynthesizedText(wFormatID);
858 case CF_METAFILEPICT:
861 FIXME("Synthesizing wFormatID(0x%08x) not implemented\n", wFormatID);
865 FIXME("Called to synthesize unknown format\n");
870 lpData->wFlags &= ~CF_FLAG_SYNTHESIZED;
877 /**************************************************************************
878 * X11DRV_CLIPBOARD_RenderSynthesizedText
880 * Renders synthesized text
882 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(UINT wFormatID)
887 INT src_chars, dst_chars, alloc_size;
888 LPWINE_CLIPDATA lpSource = NULL;
890 TRACE(" %d\n", wFormatID);
892 if ((lpSource = X11DRV_CLIPBOARD_LookupData(wFormatID)) &&
896 /* Look for rendered source or non-synthesized source */
897 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
898 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
900 TRACE("UNICODETEXT -> %d\n", wFormatID);
902 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
903 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
905 TRACE("TEXT -> %d\n", wFormatID);
907 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
908 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
910 TRACE("OEMTEXT -> %d\n", wFormatID);
913 if (!lpSource || (lpSource->wFlags & CF_FLAG_SYNTHESIZED &&
917 /* Ask the clipboard owner to render the source text if necessary */
918 if (!lpSource->hData32 && !X11DRV_CLIPBOARD_RenderFormat(lpSource))
921 if (lpSource->hData32)
923 lpstrS = (LPSTR)GlobalLock(lpSource->hData32);
927 lpstrS = (LPSTR)GlobalLock16(lpSource->hData16);
933 /* Text always NULL terminated */
934 if(lpSource->wFormatID == CF_UNICODETEXT)
935 src_chars = strlenW((LPCWSTR)lpstrS) + 1;
937 src_chars = strlen(lpstrS) + 1;
939 /* Calculate number of characters in the destination buffer */
940 dst_chars = CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS,
941 src_chars, wFormatID, NULL, 0);
946 TRACE("Converting from '%d' to '%d', %i chars\n",
947 lpSource->wFormatID, wFormatID, src_chars);
949 /* Convert characters to bytes */
950 if(wFormatID == CF_UNICODETEXT)
951 alloc_size = dst_chars * sizeof(WCHAR);
953 alloc_size = dst_chars;
955 hData32 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE |
956 GMEM_DDESHARE, alloc_size);
958 lpstrT = (LPSTR)GlobalLock(hData32);
962 CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS, src_chars,
963 wFormatID, lpstrT, dst_chars);
964 GlobalUnlock(hData32);
968 if (lpSource->hData32)
969 GlobalUnlock(lpSource->hData32);
971 GlobalUnlock16(lpSource->hData16);
973 return X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, 0, hData32, 0);
977 /**************************************************************************
978 * X11DRV_CLIPBOARD_ImportXAString
980 * Import XA_STRING, converting the string to CF_UNICODE.
982 HANDLE X11DRV_CLIPBOARD_ImportXAString(LPBYTE lpdata, UINT cBytes)
985 UINT i, inlcount = 0;
986 HANDLE hUnicodeText = 0;
988 for (i = 0; i <= cBytes; i++)
990 if (lpdata[i] == '\n')
994 if ((lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cBytes + inlcount + 1)))
998 for (i = 0, inlcount = 0; i <= cBytes; i++)
1000 if (lpdata[i] == '\n')
1001 lpstr[inlcount++] = '\r';
1003 lpstr[inlcount++] = lpdata[i];
1006 count = MultiByteToWideChar(CP_UNIXCP, 0, lpstr, -1, NULL, 0);
1007 hUnicodeText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, count * sizeof(WCHAR));
1011 WCHAR *textW = GlobalLock(hUnicodeText);
1012 MultiByteToWideChar(CP_UNIXCP, 0, lpstr, -1, textW, count);
1013 GlobalUnlock(hUnicodeText);
1016 HeapFree(GetProcessHeap(), 0, lpstr);
1019 return hUnicodeText;
1023 /**************************************************************************
1024 * X11DRV_CLIPBOARD_ImportXAPIXMAP
1026 * Import XA_PIXMAP, converting the image to CF_DIB.
1028 HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(LPBYTE lpdata, UINT cBytes)
1030 HANDLE hTargetImage = 0; /* Handle to store the converted DIB */
1031 Pixmap *pPixmap = (Pixmap *) lpdata;
1032 HWND hwnd = GetOpenClipboardWindow();
1033 HDC hdc = GetDC(hwnd);
1035 hTargetImage = X11DRV_DIB_CreateDIBFromPixmap(*pPixmap, hdc, TRUE);
1037 ReleaseDC(hwnd, hdc);
1039 return hTargetImage;
1043 /**************************************************************************
1044 * X11DRV_CLIPBOARD_ImportMetaFilePict
1046 * Import MetaFilePict.
1048 HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(LPBYTE lpdata, UINT cBytes)
1050 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, (HANDLE)lpdata, (LPDWORD)&cBytes, FALSE);
1054 /**************************************************************************
1055 * X11DRV_ImportEnhMetaFile
1057 * Import EnhMetaFile.
1059 HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(LPBYTE lpdata, UINT cBytes)
1061 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, (HANDLE)lpdata, (LPDWORD)&cBytes, FALSE);
1065 /**************************************************************************
1066 * X11DRV_ImportClipbordaData
1068 * Generic import clipboard data routine.
1070 HANDLE X11DRV_CLIPBOARD_ImportClipboardData(LPBYTE lpdata, UINT cBytes)
1073 HANDLE hClipData = 0;
1077 /* Turn on the DDESHARE flag to enable shared 32 bit memory */
1078 hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cBytes);
1079 if ((lpClipData = GlobalLock(hClipData)))
1081 memcpy(lpClipData, lpdata, cBytes);
1082 GlobalUnlock(hClipData);
1092 /**************************************************************************
1093 X11DRV_CLIPBOARD_ExportClipboardData
1095 * Generic export clipboard data routine.
1097 HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Window requestor, Atom aTarget,
1098 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1102 HANDLE hClipData = 0;
1104 *lpBytes = 0; /* Assume failure */
1106 if (!X11DRV_CLIPBOARD_RenderFormat(lpData))
1107 ERR("Failed to export %d format\n", lpData->wFormatID);
1110 cBytes = GlobalSize(lpData->hData32);
1112 hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cBytes);
1114 if ((lpClipData = GlobalLock(hClipData)))
1116 LPVOID lpdata = GlobalLock(lpData->hData32);
1118 memcpy(lpClipData, lpdata, cBytes);
1121 GlobalUnlock(lpData->hData32);
1122 GlobalUnlock(hClipData);
1130 /**************************************************************************
1131 * X11DRV_CLIPBOARD_ExportXAString
1133 * Export CF_UNICODE converting the string to XA_STRING.
1134 * Helper function for X11DRV_CLIPBOARD_ExportString.
1136 HANDLE X11DRV_CLIPBOARD_ExportXAString(LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1143 *lpBytes = 0; /* Assume return has zero bytes */
1145 uni_text = GlobalLock(lpData->hData32);
1147 size = WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, NULL, 0, NULL, NULL);
1149 text = HeapAlloc(GetProcessHeap(), 0, size);
1152 WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, text, size, NULL, NULL);
1154 /* remove carriage returns */
1156 lpstr = (char*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size-- );
1157 if(lpstr == NULL) return None;
1158 for(i = 0,j = 0; i < size && text[i]; i++ )
1160 if( text[i] == '\r' &&
1161 (text[i+1] == '\n' || text[i+1] == '\0') ) continue;
1162 lpstr[j++] = text[i];
1166 *lpBytes = j; /* Number of bytes in string */
1168 HeapFree(GetProcessHeap(), 0, text);
1169 GlobalUnlock(lpData->hData32);
1175 /**************************************************************************
1176 * X11DRV_CLIPBOARD_ExportCompoundText
1178 * Export CF_UNICODE to COMPOUND_TEXT or TEXT
1179 * Helper function for X11DRV_CLIPBOARD_ExportString.
1181 HANDLE X11DRV_CLIPBOARD_ExportCompoundText(Window requestor, Atom aTarget, Atom rprop,
1182 LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1184 Display *display = thread_display();
1187 XICCEncodingStyle style;
1189 lpstr = (char*) X11DRV_CLIPBOARD_ExportXAString(lpData, lpBytes);
1193 if (aTarget == x11drv_atom(COMPOUND_TEXT))
1194 style = XCompoundTextStyle;
1196 style = XStdICCTextStyle;
1198 /* Update the X property */
1200 if (XmbTextListToTextProperty(display, &lpstr, 1, style, &prop) == Success)
1202 XSetTextProperty(display, requestor, &prop, rprop);
1205 wine_tsx11_unlock();
1207 HeapFree( GetProcessHeap(), 0, lpstr );
1213 /**************************************************************************
1214 * X11DRV_CLIPBOARD_ExportString
1216 * Export CF_UNICODE string
1218 HANDLE X11DRV_CLIPBOARD_ExportString(Window requestor, Atom aTarget, Atom rprop,
1219 LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1221 if (X11DRV_CLIPBOARD_RenderFormat(lpData))
1223 if (aTarget == XA_STRING)
1224 return X11DRV_CLIPBOARD_ExportXAString(lpData, lpBytes);
1225 else if (aTarget == x11drv_atom(COMPOUND_TEXT) || aTarget == x11drv_atom(TEXT))
1226 return X11DRV_CLIPBOARD_ExportCompoundText(requestor, aTarget,
1227 rprop, lpData, lpBytes);
1229 ERR("Unknown target %ld to %d format\n", aTarget, lpData->wFormatID);
1232 ERR("Failed to render %d format\n", lpData->wFormatID);
1238 /**************************************************************************
1239 * X11DRV_CLIPBOARD_ExportXAPIXMAP
1241 * Export CF_DIB to XA_PIXMAP.
1243 HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Window requestor, Atom aTarget, Atom rprop,
1244 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1249 if (!X11DRV_CLIPBOARD_RenderFormat(lpdata))
1251 ERR("Failed to export %d format\n", lpdata->wFormatID);
1257 /* For convert from packed DIB to Pixmap */
1258 pixmap = X11DRV_DIB_CreatePixmapFromDIB(lpdata, hdc);
1259 *lpBytes = 4; /* pixmap is a 32bit value */
1263 return (HANDLE) pixmap;
1267 /**************************************************************************
1268 * X11DRV_CLIPBOARD_ExportMetaFilePict
1270 * Export MetaFilePict.
1272 HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Window requestor, Atom aTarget, Atom rprop,
1273 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1275 if (!X11DRV_CLIPBOARD_RenderFormat(lpdata))
1277 ERR("Failed to export %d format\n", lpdata->wFormatID);
1281 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, (HANDLE)lpdata->hData32,
1286 /**************************************************************************
1287 * X11DRV_CLIPBOARD_ExportEnhMetaFile
1289 * Export EnhMetaFile.
1291 HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Window requestor, Atom aTarget, Atom rprop,
1292 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1294 if (!X11DRV_CLIPBOARD_RenderFormat(lpdata))
1296 ERR("Failed to export %d format\n", lpdata->wFormatID);
1300 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, (HANDLE)lpdata->hData32,
1305 /**************************************************************************
1306 * X11DRV_CLIPBOARD_QueryTargets
1308 static BOOL X11DRV_CLIPBOARD_QueryTargets(Display *display, Window w, Atom selection, XEvent *xe)
1314 XConvertSelection(display, selection, x11drv_atom(TARGETS),
1315 x11drv_atom(SELECTION_DATA), w, CurrentTime);
1316 wine_tsx11_unlock();
1319 * Wait until SelectionNotify is received
1321 for (i = 0; i < SELECTION_RETRIES; i++)
1324 res = XCheckTypedWindowEvent(display, w, SelectionNotify, xe);
1325 wine_tsx11_unlock();
1326 if (res && xe->xselection.selection == selection) break;
1328 usleep(SELECTION_WAIT);
1331 /* Verify that the selection returned a valid TARGETS property */
1332 if ((xe->xselection.target != x11drv_atom(TARGETS)) || (xe->xselection.property == None))
1334 /* Selection owner failed to respond or we missed the SelectionNotify */
1335 WARN("Failed to retrieve TARGETS for selection %ld.\n", selection);
1343 /**************************************************************************
1344 * X11DRV_CLIPBOARD_QueryAvailableData
1346 * Caches the list of data formats available from the current selection.
1347 * This queries the selection owner for the TARGETS property and saves all
1348 * reported property types.
1350 static int X11DRV_CLIPBOARD_QueryAvailableData(LPCLIPBOARDINFO lpcbinfo)
1352 Display *display = thread_display();
1354 Atom atype=AnyPropertyType;
1356 unsigned long remain;
1357 Atom* targetList=NULL;
1359 HWND hWndClipWindow;
1360 unsigned long cSelectionTargets = 0;
1362 if (selectionAcquired & (S_PRIMARY | S_CLIPBOARD))
1364 ERR("Received request to cache selection but process is owner=(%08x)\n",
1365 (unsigned) selectionWindow);
1367 selectionAcquired = S_NOSELECTION;
1370 if (XGetSelectionOwner(display,XA_PRIMARY) == selectionWindow)
1371 selectionAcquired |= S_PRIMARY;
1373 if (XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)) == selectionWindow)
1374 selectionAcquired |= S_CLIPBOARD;
1375 wine_tsx11_unlock();
1377 if (!(selectionAcquired == (S_PRIMARY | S_CLIPBOARD)))
1379 WARN("Lost selection but process didn't process SelectClear\n");
1380 selectionWindow = None;
1384 return -1; /* Prevent self request */
1388 if (lpcbinfo->flags & CB_OWNER)
1389 hWndClipWindow = lpcbinfo->hWndOwner;
1390 else if (lpcbinfo->flags & CB_OPEN)
1391 hWndClipWindow = lpcbinfo->hWndOpen;
1393 hWndClipWindow = GetActiveWindow();
1395 if (!hWndClipWindow)
1397 WARN("No window available to retrieve selection!\n");
1401 w = X11DRV_get_whole_window(GetAncestor(hWndClipWindow, GA_ROOT));
1404 * Query the selection owner for the TARGETS property
1407 if ((usePrimary && XGetSelectionOwner(display,XA_PRIMARY)) ||
1408 XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
1410 wine_tsx11_unlock();
1411 if (usePrimary && (X11DRV_CLIPBOARD_QueryTargets(display, w, XA_PRIMARY, &xe)))
1412 selectionCacheSrc = XA_PRIMARY;
1413 else if (X11DRV_CLIPBOARD_QueryTargets(display, w, x11drv_atom(CLIPBOARD), &xe))
1414 selectionCacheSrc = x11drv_atom(CLIPBOARD);
1418 else /* No selection owner so report 0 targets available */
1420 wine_tsx11_unlock();
1424 /* Read the TARGETS property contents */
1426 if(XGetWindowProperty(display, xe.xselection.requestor, xe.xselection.property,
1427 0, 0x3FFF, True, AnyPropertyType/*XA_ATOM*/, &atype, &aformat, &cSelectionTargets,
1428 &remain, (unsigned char**)&targetList) != Success)
1430 wine_tsx11_unlock();
1431 WARN("Failed to read TARGETS property\n");
1435 wine_tsx11_unlock();
1436 TRACE("Type %lx,Format %d,nItems %ld, Remain %ld\n",
1437 atype, aformat, cSelectionTargets, remain);
1439 * The TARGETS property should have returned us a list of atoms
1440 * corresponding to each selection target format supported.
1442 if ((atype == XA_ATOM || atype == x11drv_atom(TARGETS)) && aformat == 32)
1444 INT i, nb_atoms = 0;
1447 /* Cache these formats in the clipboard cache */
1448 for (i = 0; i < cSelectionTargets; i++)
1450 LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupProperty(targetList[i]);
1453 lpFormat = X11DRV_CLIPBOARD_LookupAliasProperty(targetList[i]);
1457 /* add it to the list of atoms that we don't know about yet */
1458 if (!atoms) atoms = HeapAlloc( GetProcessHeap(), 0,
1459 (cSelectionTargets - i) * sizeof(*atoms) );
1460 if (atoms) atoms[nb_atoms++] = targetList[i];
1464 TRACE("Atom#%d Property(%d): --> FormatID(%d) %s\n",
1465 i, lpFormat->drvData, lpFormat->wFormatID, lpFormat->Name);
1466 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, 0);
1470 /* query all unknown atoms in one go */
1473 char **names = HeapAlloc( GetProcessHeap(), 0, nb_atoms * sizeof(*names) );
1477 XGetAtomNames( display, atoms, nb_atoms, names );
1478 wine_tsx11_unlock();
1479 for (i = 0; i < nb_atoms; i++)
1481 WINE_CLIPFORMAT *lpFormat = register_format( names[i], atoms[i] );
1484 ERR("Failed to cache %s property\n", names[i]);
1487 TRACE("Atom#%d Property(%d): --> FormatID(%d) %s\n",
1488 i, lpFormat->drvData, lpFormat->wFormatID, lpFormat->Name);
1489 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, 0);
1492 for (i = 0; i < nb_atoms; i++) XFree( names[i] );
1493 wine_tsx11_unlock();
1494 HeapFree( GetProcessHeap(), 0, names );
1499 /* Free the list of targets */
1502 wine_tsx11_unlock();
1505 return cSelectionTargets;
1509 /**************************************************************************
1510 * X11DRV_CLIPBOARD_ReadClipboardData
1512 * This method is invoked only when we DO NOT own the X selection
1514 * We always get the data from the selection client each time,
1515 * since we have no way of determining if the data in our cache is stale.
1517 static BOOL X11DRV_CLIPBOARD_ReadClipboardData(UINT wFormat)
1519 Display *display = thread_display();
1523 HWND hWndClipWindow = GetOpenClipboardWindow();
1524 HWND hWnd = (hWndClipWindow) ? hWndClipWindow : GetActiveWindow();
1526 LPWINE_CLIPFORMAT lpFormat;
1528 TRACE("%d\n", wFormat);
1530 if (!selectionAcquired)
1532 Window w = X11DRV_get_whole_window(GetAncestor(hWnd, GA_ROOT));
1535 FIXME("No parent win found %p %p\n", hWnd, hWndClipWindow);
1539 lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
1541 if (lpFormat->drvData)
1547 TRACE("Requesting %s selection (%d) from win(%08x)\n",
1548 lpFormat->Name, lpFormat->drvData, (UINT)selectionCacheSrc);
1551 XConvertSelection(display, selectionCacheSrc, lpFormat->drvData,
1552 x11drv_atom(SELECTION_DATA), w, CurrentTime);
1553 wine_tsx11_unlock();
1555 /* wait until SelectionNotify is received */
1556 for (i = 0; i < SELECTION_RETRIES; i++)
1559 res = XCheckTypedWindowEvent(display, w, SelectionNotify, &xe);
1560 wine_tsx11_unlock();
1561 if (res && xe.xselection.selection == selectionCacheSrc) break;
1563 usleep(SELECTION_WAIT);
1566 /* If the property wasn't available check for aliases */
1567 if (xe.xselection.property == None &&
1568 (alias = X11DRV_CLIPBOARD_LookupPropertyAlias(lpFormat->drvData)))
1571 XConvertSelection(display, selectionCacheSrc, alias,
1572 x11drv_atom(SELECTION_DATA), w, CurrentTime);
1573 wine_tsx11_unlock();
1575 /* wait until SelectionNotify is received */
1576 for (i = 0; i < SELECTION_RETRIES; i++)
1579 res = XCheckTypedWindowEvent(display, w, SelectionNotify, &xe);
1580 wine_tsx11_unlock();
1581 if (res && xe.xselection.selection == selectionCacheSrc) break;
1583 usleep(SELECTION_WAIT);
1587 /* Verify that the selection returned a valid TARGETS property */
1588 if (xe.xselection.property != None)
1591 * Read the contents of the X selection property
1592 * into WINE's clipboard cache converting the
1593 * selection to be compatible if possible.
1595 bRet = X11DRV_CLIPBOARD_ReadSelection(lpFormat, xe.xselection.requestor,
1596 xe.xselection.property);
1602 ERR("Received request to cache selection data but process is owner\n");
1605 TRACE("Returning %d\n", bRet);
1611 /**************************************************************************
1612 * X11DRV_CLIPBOARD_ReadSelection
1613 * Reads the contents of the X selection property into the WINE clipboard cache
1614 * converting the selection into a format compatible with the windows clipboard
1616 * This method is invoked only to read the contents of a the selection owned
1617 * by an external application. i.e. when we do not own the X selection.
1619 static BOOL X11DRV_CLIPBOARD_ReadSelection(LPWINE_CLIPFORMAT lpData, Window w, Atom prop)
1621 Display *display = thread_display();
1622 Atom atype=AnyPropertyType;
1624 unsigned long total,nitems,remain,itemSize,val_cnt;
1625 long lRequestLength,bwc;
1627 unsigned char* buffer;
1633 TRACE("Reading X selection type %s\n", lpData->Name);
1636 * First request a zero length in order to figure out the request size.
1639 if(XGetWindowProperty(display,w,prop,0,0,False, AnyPropertyType,
1640 &atype, &aformat, &nitems, &itemSize, &val) != Success)
1642 wine_tsx11_unlock();
1643 WARN("Failed to get property size\n");
1647 /* Free zero length return data if any */
1654 TRACE("Retrieving %ld bytes\n", itemSize * aformat/8);
1656 lRequestLength = (itemSize * aformat/8)/4 + 1;
1659 /* Read property in 4K blocks */
1660 if (XGetWindowProperty(display,w,prop,0,4096,False, AnyPropertyType/*reqType*/,
1661 &atype, &aformat, &nitems, &remain, &buffer) != Success)
1663 wine_tsx11_unlock();
1664 WARN("Failed to read property\n");
1668 val = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nitems*bwc);
1669 memcpy(val,buffer,nitems*bwc);
1673 for (total = nitems*bwc, val_cnt = 0; remain;)
1675 val_cnt +=nitems*bwc;
1676 if (XGetWindowProperty(display, w, prop, (total / 4), 4096, False,
1677 AnyPropertyType, &atype, &aformat, &nitems, &remain, &buffer) != Success)
1679 wine_tsx11_unlock();
1680 WARN("Failed to read property\n");
1681 HeapFree(GetProcessHeap(), 0, val);
1685 total += nitems*bwc;
1686 HeapReAlloc(GetProcessHeap(),0,val, total);
1687 memcpy(&val[val_cnt], buffer, nitems*(aformat/8));
1690 wine_tsx11_unlock();
1692 bRet = X11DRV_CLIPBOARD_InsertClipboardData(lpData->wFormatID, 0, lpData->lpDrvImportFunc(val, total), 0);
1694 /* Delete the property on the window now that we are done
1695 * This will send a PropertyNotify event to the selection owner. */
1697 XDeleteProperty(display,w,prop);
1698 wine_tsx11_unlock();
1700 /* Free the retrieved property data */
1701 HeapFree(GetProcessHeap(),0,val);
1707 /**************************************************************************
1708 * CLIPBOARD_SerializeMetafile
1710 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out)
1714 TRACE(" wFormat=%d hdata=%08x out=%d\n", wformat, (unsigned int) hdata, out);
1716 if (out) /* Serialize out, caller should free memory */
1718 *lpcbytes = 0; /* Assume failure */
1720 if (wformat == CF_METAFILEPICT)
1722 LPMETAFILEPICT lpmfp = (LPMETAFILEPICT) GlobalLock(hdata);
1723 int size = GetMetaFileBitsEx(lpmfp->hMF, 0, NULL);
1725 h = GlobalAlloc(0, size + sizeof(METAFILEPICT));
1728 char *pdata = GlobalLock(h);
1730 memcpy(pdata, lpmfp, sizeof(METAFILEPICT));
1731 GetMetaFileBitsEx(lpmfp->hMF, size, pdata + sizeof(METAFILEPICT));
1733 *lpcbytes = size + sizeof(METAFILEPICT);
1738 GlobalUnlock(hdata);
1740 else if (wformat == CF_ENHMETAFILE)
1742 int size = GetEnhMetaFileBits(hdata, 0, NULL);
1744 h = GlobalAlloc(0, size);
1747 LPVOID pdata = GlobalLock(h);
1749 GetEnhMetaFileBits(hdata, size, pdata);
1758 if (wformat == CF_METAFILEPICT)
1760 h = GlobalAlloc(0, sizeof(METAFILEPICT));
1763 LPMETAFILEPICT pmfp = (LPMETAFILEPICT) GlobalLock(h);
1765 memcpy(pmfp, (LPVOID)hdata, sizeof(METAFILEPICT));
1766 pmfp->hMF = SetMetaFileBitsEx(*lpcbytes - sizeof(METAFILEPICT),
1767 (char *)hdata + sizeof(METAFILEPICT));
1772 else if (wformat == CF_ENHMETAFILE)
1774 h = SetEnhMetaFileBits(*lpcbytes, (LPVOID)hdata);
1782 /**************************************************************************
1783 * X11DRV_CLIPBOARD_ReleaseSelection
1785 * Release an XA_PRIMARY or XA_CLIPBOARD selection that we own, in response
1786 * to a SelectionClear event.
1787 * This can occur in response to another client grabbing the X selection.
1788 * If the XA_CLIPBOARD selection is lost, we relinquish XA_PRIMARY as well.
1790 void X11DRV_CLIPBOARD_ReleaseSelection(Atom selType, Window w, HWND hwnd)
1792 Display *display = thread_display();
1794 /* w is the window that lost the selection
1796 TRACE("event->window = %08x (selectionWindow = %08x) selectionAcquired=0x%08x\n",
1797 (unsigned)w, (unsigned)selectionWindow, (unsigned)selectionAcquired);
1799 if (selectionAcquired)
1801 if (w == selectionWindow)
1803 /* If we're losing the CLIPBOARD selection, or if the preferences in .winerc
1804 * dictate that *all* selections should be cleared on loss of a selection,
1805 * we must give up all the selections we own.
1807 if (clearAllSelections || (selType == x11drv_atom(CLIPBOARD)))
1809 CLIPBOARDINFO cbinfo;
1811 /* completely give up the selection */
1812 TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
1814 /* We are completely giving up the selection. There is a
1815 * potential race condition where the apps that now owns
1816 * the selection has already grabbed both selections. In
1817 * this case, if we clear any selection we may clear the
1818 * new owners selection. To prevent this common case we
1819 * try to open the clipboard. If we can't, we assume it
1820 * was a wine apps that took it and has taken both selections.
1821 * In this case, don't bother releasing the other selection.
1822 * Otherwise only release the selection if we still own it.
1824 X11DRV_CLIPBOARD_GetClipboardInfo(&cbinfo);
1826 if (cbinfo.flags & CB_OWNER)
1828 /* Since we're still the owner, this wasn't initiated by
1829 another Wine process */
1830 if (OpenClipboard(hwnd))
1832 /* We really lost CLIPBOARD but want to voluntarily lose PRIMARY */
1833 if ((selType == x11drv_atom(CLIPBOARD)) && (selectionAcquired & S_PRIMARY))
1835 TRACE("Lost clipboard. Check if we need to release PRIMARY\n");
1837 if (selectionWindow == XGetSelectionOwner(display,XA_PRIMARY))
1839 TRACE("We still own PRIMARY. Releasing PRIMARY.\n");
1840 XSetSelectionOwner(display, XA_PRIMARY, None, CurrentTime);
1843 TRACE("We no longer own PRIMARY\n");
1844 wine_tsx11_unlock();
1847 /* We really lost PRIMARY but want to voluntarily lose CLIPBOARD */
1848 if ((selType == XA_PRIMARY) && (selectionAcquired & S_CLIPBOARD))
1850 TRACE("Lost PRIMARY. Check if we need to release CLIPBOARD\n");
1852 if (selectionWindow == XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
1854 TRACE("We still own CLIPBOARD. Releasing CLIPBOARD.\n");
1855 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), None, CurrentTime);
1858 TRACE("We no longer own CLIPBOARD\n");
1859 wine_tsx11_unlock();
1862 /* Destroy private objects */
1863 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
1865 /* Give up ownership of the windows clipboard */
1866 X11DRV_CLIPBOARD_ReleaseOwnership();
1873 TRACE("Lost selection to other Wine process.\n");
1876 selectionWindow = None;
1877 PrimarySelectionOwner = ClipboardSelectionOwner = 0;
1879 X11DRV_EmptyClipboard();
1881 /* Reset the selection flags now that we are done */
1882 selectionAcquired = S_NOSELECTION;
1884 else if ( selType == XA_PRIMARY ) /* Give up only PRIMARY selection */
1886 TRACE("Lost PRIMARY selection\n");
1887 PrimarySelectionOwner = 0;
1888 selectionAcquired &= ~S_PRIMARY; /* clear S_PRIMARY mask */
1895 /**************************************************************************
1896 * IsSelectionOwner (X11DRV.@)
1898 * Returns: TRUE if the selection is owned by this process, FALSE otherwise
1900 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void)
1902 return selectionAcquired;
1906 /**************************************************************************
1907 * X11DRV Clipboard Exports
1908 **************************************************************************/
1911 /**************************************************************************
1912 * RegisterClipboardFormat (X11DRV.@)
1914 * Registers a custom X clipboard format
1915 * Returns: Format id or 0 on failure
1917 INT X11DRV_RegisterClipboardFormat(LPCSTR FormatName)
1919 LPWINE_CLIPFORMAT lpFormat;
1921 if (FormatName == NULL) return 0;
1922 if (!(lpFormat = register_format( FormatName, 0 ))) return 0;
1923 return lpFormat->wFormatID;
1927 /**************************************************************************
1928 * X11DRV_GetClipboardFormatName
1930 INT X11DRV_GetClipboardFormatName(UINT wFormat, LPSTR retStr, INT maxlen)
1933 LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
1935 TRACE("(%04X, %p, %d) !\n", wFormat, retStr, maxlen);
1937 if (!lpFormat || (lpFormat->wFlags & CF_FLAG_BUILTINFMT))
1939 TRACE("Unknown format 0x%08x!\n", wFormat);
1940 SetLastError(ERROR_INVALID_PARAMETER);
1944 strncpy(retStr, lpFormat->Name, maxlen - 1);
1945 retStr[maxlen - 1] = 0;
1947 len = strlen(retStr);
1954 /**************************************************************************
1955 * AcquireClipboard (X11DRV.@)
1957 void X11DRV_AcquireClipboard(HWND hWndClipWindow)
1959 Display *display = thread_display();
1962 * Acquire X selection if we don't already own it.
1963 * Note that we only acquire the selection if it hasn't been already
1964 * acquired by us, and ignore the fact that another X window may be
1965 * asserting ownership. The reason for this is we need *any* top level
1966 * X window to hold selection ownership. The actual clipboard data requests
1967 * are made via GetClipboardData from EVENT_SelectionRequest and this
1968 * ensures that the real HWND owner services the request.
1969 * If the owning X window gets destroyed the selection ownership is
1970 * re-cycled to another top level X window in X11DRV_CLIPBOARD_ResetOwner.
1973 if (!(selectionAcquired == (S_PRIMARY | S_CLIPBOARD)))
1977 if (!hWndClipWindow)
1978 hWndClipWindow = GetActiveWindow();
1980 owner = X11DRV_get_whole_window(GetAncestor(hWndClipWindow, GA_ROOT));
1983 /* Grab PRIMARY selection if not owned */
1984 if (usePrimary && !(selectionAcquired & S_PRIMARY))
1985 XSetSelectionOwner(display, XA_PRIMARY, owner, CurrentTime);
1987 /* Grab CLIPBOARD selection if not owned */
1988 if (!(selectionAcquired & S_CLIPBOARD))
1989 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), owner, CurrentTime);
1991 if (usePrimary && XGetSelectionOwner(display,XA_PRIMARY) == owner)
1992 selectionAcquired |= S_PRIMARY;
1994 if (XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)) == owner)
1995 selectionAcquired |= S_CLIPBOARD;
1996 wine_tsx11_unlock();
1998 if (selectionAcquired)
2000 selectionWindow = owner;
2001 TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner);
2006 WARN("Received request to acquire selection but process is already owner=(%08x)\n", (unsigned) selectionWindow);
2008 selectionAcquired = S_NOSELECTION;
2011 if (XGetSelectionOwner(display,XA_PRIMARY) == selectionWindow)
2012 selectionAcquired |= S_PRIMARY;
2014 if (XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)) == selectionWindow)
2015 selectionAcquired |= S_CLIPBOARD;
2016 wine_tsx11_unlock();
2018 if (!(selectionAcquired == (S_PRIMARY | S_CLIPBOARD)))
2020 WARN("Lost selection but process didn't process SelectClear\n");
2021 selectionWindow = None;
2027 /**************************************************************************
2028 * X11DRV_EmptyClipboard
2030 void X11DRV_EmptyClipboard(void)
2034 LPWINE_CLIPDATA lpData;
2035 LPWINE_CLIPDATA lpNext = ClipData;
2040 lpNext = lpData->NextData;
2041 lpData->PrevData->NextData = lpData->NextData;
2042 lpData->NextData->PrevData = lpData->PrevData;
2043 X11DRV_CLIPBOARD_FreeData(lpData);
2044 HeapFree(GetProcessHeap(), 0, lpData);
2045 } while (lpNext != lpData);
2048 TRACE(" %d entries deleted from cache.\n", ClipDataCount);
2056 /**************************************************************************
2057 * X11DRV_SetClipboardData
2059 BOOL X11DRV_SetClipboardData(UINT wFormat, HANDLE16 hData16, HANDLE hData32)
2061 BOOL bResult = FALSE;
2063 if (X11DRV_CLIPBOARD_InsertClipboardData(wFormat, hData16, hData32, 0))
2070 /**************************************************************************
2071 * CountClipboardFormats
2073 INT X11DRV_CountClipboardFormats(void)
2075 CLIPBOARDINFO cbinfo;
2077 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2079 TRACE(" count=%d\n", ClipDataCount);
2081 return ClipDataCount;
2085 /**************************************************************************
2086 * X11DRV_EnumClipboardFormats
2088 UINT X11DRV_EnumClipboardFormats(UINT wFormat)
2090 CLIPBOARDINFO cbinfo;
2091 UINT wNextFormat = 0;
2093 TRACE("(%04X)\n", wFormat);
2095 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2100 wNextFormat = ClipData->wFormatID;
2104 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormat);
2106 if (lpData && lpData->NextData != ClipData)
2107 wNextFormat = lpData->NextData->wFormatID;
2114 /**************************************************************************
2115 * X11DRV_IsClipboardFormatAvailable
2117 BOOL X11DRV_IsClipboardFormatAvailable(UINT wFormat)
2120 CLIPBOARDINFO cbinfo;
2122 TRACE("(%04X)\n", wFormat);
2124 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2126 if (wFormat != 0 && X11DRV_CLIPBOARD_LookupData(wFormat))
2129 TRACE("(%04X)- ret(%d)\n", wFormat, bRet);
2135 /**************************************************************************
2136 * GetClipboardData (USER.142)
2138 BOOL X11DRV_GetClipboardData(UINT wFormat, HANDLE16* phData16, HANDLE* phData32)
2140 CLIPBOARDINFO cbinfo;
2141 LPWINE_CLIPDATA lpRender;
2143 TRACE("(%04X)\n", wFormat);
2145 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2147 if ((lpRender = X11DRV_CLIPBOARD_LookupData(wFormat)))
2149 if ( !lpRender->hData32 )
2150 X11DRV_CLIPBOARD_RenderFormat(lpRender);
2152 /* Convert between 32 -> 16 bit data, if necessary */
2153 if (lpRender->hData32 && !lpRender->hData16)
2157 if (lpRender->wFormatID == CF_METAFILEPICT)
2158 size = sizeof(METAFILEPICT16);
2160 size = GlobalSize(lpRender->hData32);
2162 lpRender->hData16 = GlobalAlloc16(GMEM_ZEROINIT, size);
2164 if (!lpRender->hData16)
2165 ERR("(%04X) -- not enough memory in 16b heap\n", wFormat);
2168 if (lpRender->wFormatID == CF_METAFILEPICT)
2170 FIXME("\timplement function CopyMetaFilePict32to16\n");
2171 FIXME("\tin the appropriate file.\n");
2172 #ifdef SOMEONE_IMPLEMENTED_ME
2173 CopyMetaFilePict32to16(GlobalLock16(lpRender->hData16),
2174 GlobalLock(lpRender->hData32));
2179 memcpy(GlobalLock16(lpRender->hData16),
2180 GlobalLock(lpRender->hData32), size);
2183 GlobalUnlock16(lpRender->hData16);
2184 GlobalUnlock(lpRender->hData32);
2188 /* Convert between 32 -> 16 bit data, if necessary */
2189 if (lpRender->hData16 && !lpRender->hData32)
2193 if (lpRender->wFormatID == CF_METAFILEPICT)
2194 size = sizeof(METAFILEPICT16);
2196 size = GlobalSize(lpRender->hData32);
2198 lpRender->hData32 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE |
2199 GMEM_DDESHARE, size);
2201 if (lpRender->wFormatID == CF_METAFILEPICT)
2203 FIXME("\timplement function CopyMetaFilePict16to32\n");
2204 FIXME("\tin the appropriate file.\n");
2205 #ifdef SOMEONE_IMPLEMENTED_ME
2206 CopyMetaFilePict16to32(GlobalLock16(lpRender->hData32),
2207 GlobalLock(lpRender->hData16));
2212 memcpy(GlobalLock(lpRender->hData32),
2213 GlobalLock16(lpRender->hData16), size);
2216 GlobalUnlock(lpRender->hData32);
2217 GlobalUnlock16(lpRender->hData16);
2221 *phData16 = lpRender->hData16;
2224 *phData32 = lpRender->hData32;
2226 TRACE(" returning hData16(%04x) hData32(%04x) (type %d)\n",
2227 lpRender->hData16, (unsigned int) lpRender->hData32, lpRender->wFormatID);
2229 return lpRender->hData16 || lpRender->hData32;
2236 /**************************************************************************
2237 * ResetSelectionOwner (X11DRV.@)
2239 * Called from DestroyWindow() to prevent X selection from being lost when
2240 * a top level window is destroyed, by switching ownership to another top
2242 * Any top level window can own the selection. See X11DRV_CLIPBOARD_Acquire
2243 * for a more detailed description of this.
2245 void X11DRV_ResetSelectionOwner(HWND hwnd, BOOL bFooBar)
2247 Display *display = thread_display();
2248 HWND hWndClipOwner = 0;
2250 Window XWnd = X11DRV_get_whole_window(hwnd);
2251 BOOL bLostSelection = FALSE;
2252 Window selectionPrevWindow;
2254 /* There is nothing to do if we don't own the selection,
2255 * or if the X window which currently owns the selection is different
2256 * from the one passed in.
2258 if (!selectionAcquired || XWnd != selectionWindow
2259 || selectionWindow == None )
2262 if ((bFooBar && XWnd) || (!bFooBar && !XWnd))
2265 hWndClipOwner = GetClipboardOwner();
2267 TRACE("clipboard owner = %p, selection window = %08x\n",
2268 hWndClipOwner, (unsigned)selectionWindow);
2270 /* now try to salvage current selection from being destroyed by X */
2271 TRACE("checking %08x\n", (unsigned) XWnd);
2273 selectionPrevWindow = selectionWindow;
2274 selectionWindow = None;
2276 if (!(tmp = GetWindow(hwnd, GW_HWNDNEXT)))
2277 tmp = GetWindow(hwnd, GW_HWNDFIRST);
2279 if (tmp && tmp != hwnd)
2280 selectionWindow = X11DRV_get_whole_window(tmp);
2282 if (selectionWindow != None)
2284 /* We must pretend that we don't own the selection while making the switch
2285 * since a SelectionClear event will be sent to the last owner.
2286 * If there is no owner X11DRV_CLIPBOARD_ReleaseSelection will do nothing.
2288 int saveSelectionState = selectionAcquired;
2289 selectionAcquired = S_NOSELECTION;
2291 TRACE("\tswitching selection from %08x to %08x\n",
2292 (unsigned)selectionPrevWindow, (unsigned)selectionWindow);
2296 /* Assume ownership for the PRIMARY and CLIPBOARD selection */
2297 if (saveSelectionState & S_PRIMARY)
2298 XSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime);
2300 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), selectionWindow, CurrentTime);
2302 /* Restore the selection masks */
2303 selectionAcquired = saveSelectionState;
2305 /* Lose the selection if something went wrong */
2306 if (((saveSelectionState & S_PRIMARY) &&
2307 (XGetSelectionOwner(display, XA_PRIMARY) != selectionWindow)) ||
2308 (XGetSelectionOwner(display, x11drv_atom(CLIPBOARD)) != selectionWindow))
2310 bLostSelection = TRUE;
2314 /* Update selection state */
2315 if (saveSelectionState & S_PRIMARY)
2316 PrimarySelectionOwner = selectionWindow;
2318 ClipboardSelectionOwner = selectionWindow;
2320 wine_tsx11_unlock();
2324 bLostSelection = TRUE;
2329 TRACE("Lost the selection!\n");
2331 X11DRV_CLIPBOARD_ReleaseOwnership();
2332 selectionAcquired = S_NOSELECTION;
2333 ClipboardSelectionOwner = PrimarySelectionOwner = 0;
2334 selectionWindow = 0;
2339 /**************************************************************************
2340 * X11DRV_CLIPBOARD_SynthesizeData
2342 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID)
2345 LPWINE_CLIPDATA lpSource = NULL;
2347 TRACE(" %d\n", wFormatID);
2349 /* Don't need to synthesize if it already exists */
2350 if (X11DRV_CLIPBOARD_LookupData(wFormatID))
2353 if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
2355 bsyn = ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
2356 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2357 ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
2358 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2359 ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
2360 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED);
2362 else if (wFormatID == CF_ENHMETAFILE)
2364 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT)) &&
2365 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2367 else if (wFormatID == CF_METAFILEPICT)
2369 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT)) &&
2370 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2372 else if (wFormatID == CF_DIB)
2374 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) &&
2375 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2377 else if (wFormatID == CF_BITMAP)
2379 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) &&
2380 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2384 X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, 0, 0, CF_FLAG_SYNTHESIZED);
2391 /**************************************************************************
2392 * X11DRV_EndClipboardUpdate
2394 * Add locale if it hasn't already been added
2396 void X11DRV_EndClipboardUpdate(void)
2398 INT count = ClipDataCount;
2400 /* Do Unicode <-> Text <-> OEM mapping */
2401 X11DRV_CLIPBOARD_SynthesizeData(CF_UNICODETEXT);
2402 X11DRV_CLIPBOARD_SynthesizeData(CF_TEXT);
2403 X11DRV_CLIPBOARD_SynthesizeData(CF_OEMTEXT);
2405 /* Enhmetafile <-> MetafilePict mapping */
2406 X11DRV_CLIPBOARD_SynthesizeData(CF_ENHMETAFILE);
2407 X11DRV_CLIPBOARD_SynthesizeData(CF_METAFILEPICT);
2409 /* DIB <-> Bitmap mapping */
2410 X11DRV_CLIPBOARD_SynthesizeData(CF_DIB);
2411 X11DRV_CLIPBOARD_SynthesizeData(CF_BITMAP);
2413 TRACE("%d formats added to cached data\n", ClipDataCount - count);