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
79 #include "clipboard.h"
82 #include "wine/debug.h"
83 #include "wine/unicode.h"
84 #include "wine/server.h"
86 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
88 #define HGDIOBJ_32(handle16) ((HGDIOBJ)(ULONG_PTR)(handle16))
90 /* Maximum wait time for selection notify */
91 #define SELECTION_RETRIES 500 /* wait for .1 seconds */
92 #define SELECTION_WAIT 1000 /* us */
93 /* Minimum seconds that must lapse between owner queries */
94 #define OWNERQUERYLAPSETIME 1
97 #define S_NOSELECTION 0
101 static int selectionAcquired = 0; /* Contains the current selection masks */
102 static Window selectionWindow = None; /* The top level X window which owns the selection */
103 static BOOL clearAllSelections = FALSE; /* Always lose all selections */
104 static Atom selectionCacheSrc = XA_PRIMARY; /* The selection source from which the clipboard cache was filled */
105 static Window PrimarySelectionOwner = None; /* The window which owns the primary selection */
106 static Window ClipboardSelectionOwner = None; /* The window which owns the clipboard selection */
108 INT X11DRV_RegisterClipboardFormat(LPCSTR FormatName);
109 void X11DRV_EmptyClipboard(void);
110 void X11DRV_EndClipboardUpdate(void);
111 HANDLE X11DRV_CLIPBOARD_ImportClipboardData(LPBYTE lpdata, UINT cBytes);
112 HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(LPBYTE lpdata, UINT cBytes);
113 HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(LPBYTE lpdata, UINT cBytes);
114 HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(LPBYTE lpdata, UINT cBytes);
115 HANDLE X11DRV_CLIPBOARD_ImportXAString(LPBYTE lpdata, UINT cBytes);
116 HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Window requestor, Atom aTarget,
117 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
118 HANDLE X11DRV_CLIPBOARD_ExportString(Window requestor, Atom aTarget,
119 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
120 HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Window requestor, Atom aTarget,
121 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
122 HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Window requestor, Atom aTarget,
123 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
124 HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Window requestor, Atom aTarget,
125 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
126 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(LPCSTR FormatName, Atom prop);
127 static BOOL X11DRV_CLIPBOARD_ReadSelection(LPWINE_CLIPFORMAT lpData, Window w, Atom prop);
128 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(UINT wFormatID);
129 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData);
130 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void);
131 static int X11DRV_CLIPBOARD_QueryAvailableData(LPCLIPBOARDINFO lpcbinfo);
132 static BOOL X11DRV_CLIPBOARD_ReadClipboardData(UINT wFormat);
133 static BOOL X11DRV_CLIPBOARD_RenderFormat(LPWINE_CLIPDATA lpData);
134 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out);
135 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID);
136 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(LPWINE_CLIPDATA lpData);
139 * WARNING: This data ordering is dependent on the WINE_CLIPFORMAT structure
140 * declared in clipboard.h
142 static WINE_CLIPFORMAT ClipFormats[] =
144 { CF_TEXT, "WCF_TEXT", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
145 X11DRV_CLIPBOARD_ExportClipboardData, NULL, &ClipFormats[1]},
147 { CF_BITMAP, "WCF_BITMAP", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
148 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[0], &ClipFormats[2]},
150 { CF_METAFILEPICT, "WCF_METAFILEPICT", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportMetaFilePict,
151 X11DRV_CLIPBOARD_ExportMetaFilePict, &ClipFormats[1], &ClipFormats[3]},
153 { CF_SYLK, "WCF_SYLK", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
154 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[2], &ClipFormats[4]},
156 { CF_DIF, "WCF_DIF", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
157 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[3], &ClipFormats[5]},
159 { CF_TIFF, "WCF_TIFF", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
160 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[4], &ClipFormats[6]},
162 { CF_OEMTEXT, "WCF_OEMTEXT", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
163 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[5], &ClipFormats[7]},
165 { CF_DIB, "WCF_DIB", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportXAPIXMAP,
166 X11DRV_CLIPBOARD_ExportXAPIXMAP, &ClipFormats[6], &ClipFormats[8]},
168 { CF_PALETTE, "WCF_PALETTE", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
169 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[7], &ClipFormats[9]},
171 { CF_PENDATA, "WCF_PENDATA", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
172 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[8], &ClipFormats[10]},
174 { CF_RIFF, "WCF_RIFF", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
175 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[9], &ClipFormats[11]},
177 { CF_WAVE, "WCF_WAVE", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
178 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[10], &ClipFormats[12]},
180 { CF_UNICODETEXT, "WCF_UNICODETEXT", XA_STRING, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportXAString,
181 X11DRV_CLIPBOARD_ExportString, &ClipFormats[11], &ClipFormats[13]},
183 { CF_ENHMETAFILE, "WCF_ENHMETAFILE", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportEnhMetaFile,
184 X11DRV_CLIPBOARD_ExportEnhMetaFile, &ClipFormats[12], &ClipFormats[14]},
186 { CF_HDROP, "WCF_HDROP", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
187 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[13], &ClipFormats[15]},
189 { CF_LOCALE, "WCF_LOCALE", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
190 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[14], &ClipFormats[16]},
192 { CF_DIBV5, "WCF_DIBV5", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
193 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[15], &ClipFormats[17]},
195 { CF_OWNERDISPLAY, "WCF_OWNERDISPLAY", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
196 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[16], &ClipFormats[18]},
198 { CF_DSPTEXT, "WCF_DSPTEXT", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
199 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[17], &ClipFormats[19]},
201 { CF_DSPBITMAP, "WCF_DSPBITMAP", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
202 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[18], &ClipFormats[20]},
204 { CF_DSPMETAFILEPICT, "WCF_DSPMETAFILEPICT", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
205 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[19], &ClipFormats[21]},
207 { CF_DSPENHMETAFILE, "WCF_DSPENHMETAFILE", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
208 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[20], NULL}
211 #define GET_ATOM(prop) (((prop) < FIRST_XATOM) ? (Atom)(prop) : X11DRV_Atoms[(prop) - FIRST_XATOM])
213 /* Maps X properties to Windows formats */
218 } PropertyFormatMap[] =
220 { "Rich Text Format", XATOM_text_rtf },
221 /* Temporarily disable text/html because Evolution incorrectly pastes strings with extra nulls */
222 /*{ "text/html", "HTML Format" },*/
223 { "GIF", XATOM_image_gif }
227 /* Maps equivalent X properties. It is assumed that lpszProperty must already
228 be in ClipFormats or PropertyFormatMap. */
231 UINT drvDataProperty;
233 } PropertyAliasMap[] =
235 /* DataProperty, DataAlias */
236 { XATOM_text_rtf, XATOM_text_richtext },
237 { XA_STRING, XATOM_COMPOUND_TEXT },
238 { XA_STRING, XATOM_TEXT },
243 * Cached clipboard data.
245 static LPWINE_CLIPDATA ClipData = NULL;
246 static UINT ClipDataCount = 0;
249 * Clipboard sequence number
251 static UINT wSeqNo = 0;
253 /**************************************************************************
254 * Internal Clipboard implementation methods
255 **************************************************************************/
257 /**************************************************************************
258 * X11DRV_InitClipboard
260 void X11DRV_InitClipboard(void)
265 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Clipboard", &hkey))
268 DWORD type, count = sizeof(buffer);
269 if(!RegQueryValueExA(hkey, "ClearAllSelections", 0, &type, buffer, &count))
270 clearAllSelections = atoi(buffer);
274 /* Register known mapping between window formats and X properties */
275 for (i = 0; i < sizeof(PropertyFormatMap)/sizeof(PropertyFormatMap[0]); i++)
276 X11DRV_CLIPBOARD_InsertClipboardFormat(PropertyFormatMap[i].lpszFormat,
277 GET_ATOM(PropertyFormatMap[i].prop));
281 /**************************************************************************
284 * Intern atoms for formats that don't have one yet.
286 static void intern_atoms(void)
288 LPWINE_CLIPFORMAT format;
293 for (format = ClipFormats, count = 0; format; format = format->NextFormat)
294 if (!format->drvData) count++;
297 names = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*names) );
298 atoms = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*atoms) );
300 for (format = ClipFormats, i = 0; format; format = format->NextFormat)
301 if (!format->drvData) names[i++] = format->Name;
304 XInternAtoms( thread_display(), names, count, False, atoms );
307 for (format = ClipFormats, i = 0; format; format = format->NextFormat)
308 if (!format->drvData) format->drvData = atoms[i];
310 HeapFree( GetProcessHeap(), 0, names );
311 HeapFree( GetProcessHeap(), 0, atoms );
315 /**************************************************************************
318 * Register a custom X clipboard format.
320 static WINE_CLIPFORMAT *register_format( LPCSTR FormatName, Atom prop )
322 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
324 TRACE("'%s'\n", FormatName);
326 /* walk format chain to see if it's already registered */
329 if ( !strcasecmp(lpFormat->Name, FormatName) &&
330 (lpFormat->wFlags & CF_FLAG_BUILTINFMT) == 0)
332 lpFormat = lpFormat->NextFormat;
335 return X11DRV_CLIPBOARD_InsertClipboardFormat(FormatName, prop);
339 /**************************************************************************
340 * X11DRV_CLIPBOARD_LookupFormat
342 LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupFormat(WORD wID)
344 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
348 if (lpFormat->wFormatID == wID)
351 lpFormat = lpFormat->NextFormat;
353 if (!lpFormat->drvData) intern_atoms();
358 /**************************************************************************
359 * X11DRV_CLIPBOARD_LookupProperty
361 LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupProperty(UINT drvData)
365 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
366 BOOL need_intern = FALSE;
370 if (lpFormat->drvData == drvData) return lpFormat;
371 if (!lpFormat->drvData) need_intern = TRUE;
372 lpFormat = lpFormat->NextFormat;
374 if (!need_intern) return NULL;
376 /* restart the search for the new atoms */
381 /**************************************************************************
382 * X11DRV_CLIPBOARD_LookupAliasProperty
384 LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupAliasProperty(UINT drvDataAlias)
387 LPWINE_CLIPFORMAT lpFormat = NULL;
389 for (i = 0; i < sizeof(PropertyAliasMap)/sizeof(PropertyAliasMap[0]); i++)
391 if (GET_ATOM(PropertyAliasMap[i].drvDataAlias) == drvDataAlias)
393 lpFormat = X11DRV_CLIPBOARD_LookupProperty(GET_ATOM(PropertyAliasMap[i].drvDataProperty));
402 /**************************************************************************
403 * X11DRV_CLIPBOARD_LookupPropertyAlias
405 UINT X11DRV_CLIPBOARD_LookupPropertyAlias(UINT drvDataProperty)
410 for (i = 0; i < sizeof(PropertyAliasMap)/sizeof(PropertyAliasMap[0]); i++)
412 if (GET_ATOM(PropertyAliasMap[i].drvDataProperty) == drvDataProperty)
414 alias = GET_ATOM(PropertyAliasMap[i].drvDataAlias);
423 /**************************************************************************
424 * X11DRV_CLIPBOARD_LookupData
426 LPWINE_CLIPDATA X11DRV_CLIPBOARD_LookupData(DWORD wID)
428 LPWINE_CLIPDATA lpData = ClipData;
434 if (lpData->wFormatID == wID)
437 lpData = lpData->NextData;
439 while(lpData != ClipData);
441 if (lpData->wFormatID != wID)
449 /**************************************************************************
450 * InsertClipboardFormat
452 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(LPCSTR FormatName, Atom prop)
454 LPWINE_CLIPFORMAT lpFormat;
455 LPWINE_CLIPFORMAT lpNewFormat;
457 /* allocate storage for new format entry */
458 lpNewFormat = (LPWINE_CLIPFORMAT) HeapAlloc(GetProcessHeap(),
459 0, sizeof(WINE_CLIPFORMAT));
461 if(lpNewFormat == NULL)
463 WARN("No more memory for a new format!\n");
467 if (!(lpNewFormat->Name = HeapAlloc(GetProcessHeap(), 0, strlen(FormatName)+1)))
469 WARN("No more memory for the new format name!\n");
470 HeapFree(GetProcessHeap(), 0, lpNewFormat);
474 strcpy(lpNewFormat->Name, FormatName);
475 lpNewFormat->wFlags = 0;
476 lpNewFormat->wFormatID = GlobalAddAtomA(lpNewFormat->Name);
477 lpNewFormat->drvData = prop;
478 lpNewFormat->lpDrvImportFunc = X11DRV_CLIPBOARD_ImportClipboardData;
479 lpNewFormat->lpDrvExportFunc = X11DRV_CLIPBOARD_ExportClipboardData;
482 lpFormat = ClipFormats;
484 while(lpFormat->NextFormat) /* Move to last entry */
485 lpFormat = lpFormat->NextFormat;
487 lpNewFormat->NextFormat = NULL;
488 lpFormat->NextFormat = lpNewFormat;
489 lpNewFormat->PrevFormat = lpFormat;
491 TRACE("Registering format(%d): %s drvData %d\n",
492 lpNewFormat->wFormatID, FormatName, lpNewFormat->drvData);
500 /**************************************************************************
501 * X11DRV_CLIPBOARD_GetClipboardInfo
503 static BOOL X11DRV_CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
507 SERVER_START_REQ( set_clipboard_info )
511 if (wine_server_call_err( req ))
513 ERR("Failed to get clipboard owner.\n");
517 cbInfo->hWndOpen = reply->old_clipboard;
518 cbInfo->hWndOwner = reply->old_owner;
519 cbInfo->hWndViewer = reply->old_viewer;
520 cbInfo->seqno = reply->seqno;
521 cbInfo->flags = reply->flags;
532 /**************************************************************************
533 * X11DRV_CLIPBOARD_ReleaseOwnership
535 static BOOL X11DRV_CLIPBOARD_ReleaseOwnership(void)
539 SERVER_START_REQ( set_clipboard_info )
541 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
543 if (wine_server_call_err( req ))
545 ERR("Failed to set clipboard.\n");
559 /**************************************************************************
560 * X11DRV_CLIPBOARD_InsertClipboardData
562 * Caller *must* have the clipboard open and be the owner.
564 static BOOL X11DRV_CLIPBOARD_InsertClipboardData(UINT wFormat, HANDLE16 hData16, HANDLE hData32, DWORD flags)
566 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormat);
568 TRACE("format=%d lpData=%p hData16=%08x hData32=%08x flags=0x%08lx\n",
569 wFormat, lpData, hData16, (unsigned int)hData32, flags);
573 X11DRV_CLIPBOARD_FreeData(lpData);
575 lpData->hData16 = hData16; /* 0 is legal, see WM_RENDERFORMAT */
576 lpData->hData32 = hData32;
580 lpData = (LPWINE_CLIPDATA) HeapAlloc(GetProcessHeap(),
581 0, sizeof(WINE_CLIPDATA));
583 lpData->wFormatID = wFormat;
584 lpData->hData16 = hData16; /* 0 is legal, see WM_RENDERFORMAT */
585 lpData->hData32 = hData32;
589 LPWINE_CLIPDATA lpPrevData = ClipData->PrevData;
591 lpData->PrevData = lpPrevData;
592 lpData->NextData = ClipData;
594 lpPrevData->NextData = lpData;
595 ClipData->PrevData = lpData;
599 lpData->NextData = lpData;
600 lpData->PrevData = lpData;
607 lpData->wFlags = flags;
613 /**************************************************************************
614 * X11DRV_CLIPBOARD_FreeData
616 * Free clipboard data handle.
618 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData)
620 TRACE("%d\n", lpData->wFormatID);
622 if ((lpData->wFormatID >= CF_GDIOBJFIRST &&
623 lpData->wFormatID <= CF_GDIOBJLAST) ||
624 lpData->wFormatID == CF_BITMAP ||
625 lpData->wFormatID == CF_DIB ||
626 lpData->wFormatID == CF_PALETTE)
629 DeleteObject(lpData->hData32);
632 DeleteObject(HGDIOBJ_32(lpData->hData16));
634 else if (lpData->wFormatID == CF_METAFILEPICT)
638 DeleteMetaFile(((METAFILEPICT *)GlobalLock( lpData->hData32 ))->hMF );
639 GlobalFree(lpData->hData32);
642 /* HMETAFILE16 and HMETAFILE32 are apparently the same thing,
643 and a shallow copy is enough to share a METAFILEPICT
644 structure between 16bit and 32bit clipboards. The MetaFile
645 should of course only be deleted once. */
646 GlobalFree16(lpData->hData16);
651 METAFILEPICT16* lpMetaPict = (METAFILEPICT16 *) GlobalLock16(lpData->hData16);
655 DeleteMetaFile16(lpMetaPict->hMF);
659 GlobalFree16(lpData->hData16);
662 else if (lpData->wFormatID == CF_ENHMETAFILE)
665 DeleteEnhMetaFile(lpData->hData32);
667 else if (lpData->wFormatID < CF_PRIVATEFIRST ||
668 lpData->wFormatID > CF_PRIVATELAST)
671 GlobalFree(lpData->hData32);
674 GlobalFree16(lpData->hData16);
682 /**************************************************************************
683 * X11DRV_CLIPBOARD_UpdateCache
685 static BOOL X11DRV_CLIPBOARD_UpdateCache(LPCLIPBOARDINFO lpcbinfo)
689 if (!X11DRV_CLIPBOARD_IsSelectionOwner())
691 if (!X11DRV_CLIPBOARD_GetClipboardInfo(lpcbinfo))
693 ERR("Failed to retrieve clipboard information.\n");
696 else if (wSeqNo < lpcbinfo->seqno)
698 X11DRV_EmptyClipboard();
700 if (X11DRV_CLIPBOARD_QueryAvailableData(lpcbinfo) < 0)
702 ERR("Failed to cache clipboard data owned by another process.\n");
707 X11DRV_EndClipboardUpdate();
710 wSeqNo = lpcbinfo->seqno;
718 /**************************************************************************
719 * X11DRV_CLIPBOARD_RenderFormat
721 static BOOL X11DRV_CLIPBOARD_RenderFormat(LPWINE_CLIPDATA lpData)
725 TRACE(" 0x%04x hData32(0x%08x) hData16(0x%08x)\n",
726 lpData->wFormatID, (unsigned int)lpData->hData32, lpData->hData16);
728 if (lpData->hData32 || lpData->hData16)
729 return bret; /* Already rendered */
731 if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
732 bret = X11DRV_CLIPBOARD_RenderSynthesizedFormat(lpData);
733 else if (!X11DRV_CLIPBOARD_IsSelectionOwner())
735 if (!X11DRV_CLIPBOARD_ReadClipboardData(lpData->wFormatID))
737 ERR("Failed to cache clipboard data owned by another process. Format=%d\n",
744 CLIPBOARDINFO cbInfo;
746 if (X11DRV_CLIPBOARD_GetClipboardInfo(&cbInfo) && cbInfo.hWndOwner)
748 /* Send a WM_RENDERFORMAT message to notify the owner to render the
749 * data requested into the clipboard.
751 TRACE("Sending WM_RENDERFORMAT message to hwnd(%p)\n", cbInfo.hWndOwner);
752 SendMessageW(cbInfo.hWndOwner, WM_RENDERFORMAT, (WPARAM)lpData->wFormatID, 0);
754 if (!lpData->hData32 && !lpData->hData16)
759 ERR("hWndClipOwner is lost!\n");
768 /**************************************************************************
769 * CLIPBOARD_ConvertText
770 * Returns number of required/converted characters - not bytes!
772 static INT CLIPBOARD_ConvertText(WORD src_fmt, void const *src, INT src_size,
773 WORD dst_fmt, void *dst, INT dst_size)
777 if(src_fmt == CF_UNICODETEXT)
790 return WideCharToMultiByte(cp, 0, src, src_size, dst, dst_size, NULL, NULL);
793 if(dst_fmt == CF_UNICODETEXT)
806 return MultiByteToWideChar(cp, 0, src, src_size, dst, dst_size);
809 if(!dst_size) return src_size;
811 if(dst_size > src_size) dst_size = src_size;
813 if(src_fmt == CF_TEXT )
814 CharToOemBuffA(src, dst, dst_size);
816 OemToCharBuffA(src, dst, dst_size);
822 /**************************************************************************
823 * X11DRV_CLIPBOARD_RenderSynthesizedFormat
825 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(LPWINE_CLIPDATA lpData)
831 if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
833 UINT wFormatID = lpData->wFormatID;
835 if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
836 bret = X11DRV_CLIPBOARD_RenderSynthesizedText(wFormatID);
842 case CF_METAFILEPICT:
845 FIXME("Synthesizing wFormatID(0x%08x) not implemented\n", wFormatID);
849 FIXME("Called to synthesize unknown format\n");
854 lpData->wFlags &= ~CF_FLAG_SYNTHESIZED;
861 /**************************************************************************
862 * X11DRV_CLIPBOARD_RenderSynthesizedText
864 * Renders synthesized text
866 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(UINT wFormatID)
871 INT src_chars, dst_chars, alloc_size;
872 LPWINE_CLIPDATA lpSource = NULL;
874 TRACE(" %d\n", wFormatID);
876 if ((lpSource = X11DRV_CLIPBOARD_LookupData(wFormatID)) &&
880 /* Look for rendered source or non-synthesized source */
881 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
882 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
884 TRACE("UNICODETEXT -> %d\n", wFormatID);
886 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
887 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
889 TRACE("TEXT -> %d\n", wFormatID);
891 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
892 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
894 TRACE("OEMTEXT -> %d\n", wFormatID);
897 if (!lpSource || (lpSource->wFlags & CF_FLAG_SYNTHESIZED &&
901 /* Ask the clipboard owner to render the source text if necessary */
902 if (!lpSource->hData32 && !X11DRV_CLIPBOARD_RenderFormat(lpSource))
905 if (lpSource->hData32)
907 lpstrS = (LPSTR)GlobalLock(lpSource->hData32);
911 lpstrS = (LPSTR)GlobalLock16(lpSource->hData16);
917 /* Text always NULL terminated */
918 if(lpSource->wFormatID == CF_UNICODETEXT)
919 src_chars = strlenW((LPCWSTR)lpstrS) + 1;
921 src_chars = strlen(lpstrS) + 1;
923 /* Calculate number of characters in the destination buffer */
924 dst_chars = CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS,
925 src_chars, wFormatID, NULL, 0);
930 TRACE("Converting from '%d' to '%d', %i chars\n",
931 lpSource->wFormatID, wFormatID, src_chars);
933 /* Convert characters to bytes */
934 if(wFormatID == CF_UNICODETEXT)
935 alloc_size = dst_chars * sizeof(WCHAR);
937 alloc_size = dst_chars;
939 hData32 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE |
940 GMEM_DDESHARE, alloc_size);
942 lpstrT = (LPSTR)GlobalLock(hData32);
946 CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS, src_chars,
947 wFormatID, lpstrT, dst_chars);
948 GlobalUnlock(hData32);
952 if (lpSource->hData32)
953 GlobalUnlock(lpSource->hData32);
955 GlobalUnlock16(lpSource->hData16);
957 return X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, 0, hData32, 0);
961 /**************************************************************************
962 * X11DRV_CLIPBOARD_ImportXAString
964 * Import XA_STRING, converting the string to CF_UNICODE.
966 HANDLE X11DRV_CLIPBOARD_ImportXAString(LPBYTE lpdata, UINT cBytes)
969 UINT i, inlcount = 0;
970 HANDLE hUnicodeText = 0;
972 for (i = 0; i <= cBytes; i++)
974 if (lpdata[i] == '\n')
978 if ((lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cBytes + inlcount + 1)))
982 for (i = 0, inlcount = 0; i <= cBytes; i++)
984 if (lpdata[i] == '\n')
985 lpstr[inlcount++] = '\r';
987 lpstr[inlcount++] = lpdata[i];
990 count = MultiByteToWideChar(CP_UNIXCP, 0, lpstr, -1, NULL, 0);
991 hUnicodeText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, count * sizeof(WCHAR));
995 WCHAR *textW = GlobalLock(hUnicodeText);
996 MultiByteToWideChar(CP_UNIXCP, 0, lpstr, -1, textW, count);
997 GlobalUnlock(hUnicodeText);
1000 HeapFree(GetProcessHeap(), 0, lpstr);
1003 return hUnicodeText;
1007 /**************************************************************************
1008 * X11DRV_CLIPBOARD_ImportXAPIXMAP
1010 * Import XA_PIXMAP, converting the image to CF_DIB.
1012 HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(LPBYTE lpdata, UINT cBytes)
1014 HANDLE hTargetImage = 0; /* Handle to store the converted DIB */
1015 Pixmap *pPixmap = (Pixmap *) lpdata;
1016 HWND hwnd = GetOpenClipboardWindow();
1017 HDC hdc = GetDC(hwnd);
1019 hTargetImage = X11DRV_DIB_CreateDIBFromPixmap(*pPixmap, hdc, TRUE);
1021 ReleaseDC(hwnd, hdc);
1023 return hTargetImage;
1027 /**************************************************************************
1028 * X11DRV_CLIPBOARD_ImportMetaFilePict
1030 * Import MetaFilePict.
1032 HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(LPBYTE lpdata, UINT cBytes)
1034 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, (HANDLE)lpdata, (LPDWORD)&cBytes, FALSE);
1038 /**************************************************************************
1039 * X11DRV_ImportEnhMetaFile
1041 * Import EnhMetaFile.
1043 HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(LPBYTE lpdata, UINT cBytes)
1045 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, (HANDLE)lpdata, (LPDWORD)&cBytes, FALSE);
1049 /**************************************************************************
1050 * X11DRV_ImportClipbordaData
1052 * Generic import clipboard data routine.
1054 HANDLE X11DRV_CLIPBOARD_ImportClipboardData(LPBYTE lpdata, UINT cBytes)
1057 HANDLE hClipData = 0;
1061 /* Turn on the DDESHARE flag to enable shared 32 bit memory */
1062 hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cBytes);
1063 if ((lpClipData = GlobalLock(hClipData)))
1065 memcpy(lpClipData, lpdata, cBytes);
1066 GlobalUnlock(hClipData);
1076 /**************************************************************************
1077 * X11DRV_CLIPBOARD_ExportClipboardData
1079 * Generic export clipboard data routine.
1081 HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Window requestor, Atom aTarget,
1082 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1086 HANDLE hClipData = 0;
1088 *lpBytes = 0; /* Assume failure */
1090 if (!X11DRV_CLIPBOARD_RenderFormat(lpData))
1091 ERR("Failed to export %d format\n", lpData->wFormatID);
1094 cBytes = GlobalSize(lpData->hData32);
1096 hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cBytes);
1098 if ((lpClipData = GlobalLock(hClipData)))
1100 LPVOID lpdata = GlobalLock(lpData->hData32);
1102 memcpy(lpClipData, lpdata, cBytes);
1105 GlobalUnlock(lpData->hData32);
1106 GlobalUnlock(hClipData);
1114 /**************************************************************************
1115 * X11DRV_CLIPBOARD_ExportXAString
1117 * Export CF_UNICODE converting the string to XA_STRING.
1118 * Helper function for X11DRV_CLIPBOARD_ExportString.
1120 HANDLE X11DRV_CLIPBOARD_ExportXAString(LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1127 *lpBytes = 0; /* Assume return has zero bytes */
1129 uni_text = GlobalLock(lpData->hData32);
1131 size = WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, NULL, 0, NULL, NULL);
1133 text = HeapAlloc(GetProcessHeap(), 0, size);
1136 WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, text, size, NULL, NULL);
1138 /* remove carriage returns */
1140 lpstr = (char*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size-- );
1141 if(lpstr == NULL) return None;
1142 for(i = 0,j = 0; i < size && text[i]; i++ )
1144 if( text[i] == '\r' &&
1145 (text[i+1] == '\n' || text[i+1] == '\0') ) continue;
1146 lpstr[j++] = text[i];
1150 *lpBytes = j; /* Number of bytes in string */
1152 HeapFree(GetProcessHeap(), 0, text);
1153 GlobalUnlock(lpData->hData32);
1159 /**************************************************************************
1160 * X11DRV_CLIPBOARD_ExportCompoundText
1162 * Export CF_UNICODE to COMPOUND_TEXT or TEXT
1163 * Helper function for X11DRV_CLIPBOARD_ExportString.
1165 HANDLE X11DRV_CLIPBOARD_ExportCompoundText(Window requestor, Atom aTarget, Atom rprop,
1166 LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1168 Display *display = thread_display();
1171 XICCEncodingStyle style;
1173 lpstr = (char*) X11DRV_CLIPBOARD_ExportXAString(lpData, lpBytes);
1177 if (aTarget == x11drv_atom(COMPOUND_TEXT))
1178 style = XCompoundTextStyle;
1180 style = XStdICCTextStyle;
1182 /* Update the X property */
1184 if (XmbTextListToTextProperty(display, &lpstr, 1, style, &prop) == Success)
1186 XSetTextProperty(display, requestor, &prop, rprop);
1189 wine_tsx11_unlock();
1191 HeapFree( GetProcessHeap(), 0, lpstr );
1197 /**************************************************************************
1198 * X11DRV_CLIPBOARD_ExportString
1200 * Export CF_UNICODE string
1202 HANDLE X11DRV_CLIPBOARD_ExportString(Window requestor, Atom aTarget, Atom rprop,
1203 LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1205 if (X11DRV_CLIPBOARD_RenderFormat(lpData))
1207 if (aTarget == XA_STRING)
1208 return X11DRV_CLIPBOARD_ExportXAString(lpData, lpBytes);
1209 else if (aTarget == x11drv_atom(COMPOUND_TEXT) || aTarget == x11drv_atom(TEXT))
1210 return X11DRV_CLIPBOARD_ExportCompoundText(requestor, aTarget,
1211 rprop, lpData, lpBytes);
1213 ERR("Unknown target %ld to %d format\n", aTarget, lpData->wFormatID);
1216 ERR("Failed to render %d format\n", lpData->wFormatID);
1222 /**************************************************************************
1223 * X11DRV_CLIPBOARD_ExportXAPIXMAP
1225 * Export CF_DIB to XA_PIXMAP.
1227 HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Window requestor, Atom aTarget, Atom rprop,
1228 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1233 if (!X11DRV_CLIPBOARD_RenderFormat(lpdata))
1235 ERR("Failed to export %d format\n", lpdata->wFormatID);
1241 /* For convert from packed DIB to Pixmap */
1242 pixmap = X11DRV_DIB_CreatePixmapFromDIB(lpdata, hdc);
1243 *lpBytes = 4; /* pixmap is a 32bit value */
1247 return (HANDLE) pixmap;
1251 /**************************************************************************
1252 * X11DRV_CLIPBOARD_ExportMetaFilePict
1254 * Export MetaFilePict.
1256 HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Window requestor, Atom aTarget, Atom rprop,
1257 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1259 if (!X11DRV_CLIPBOARD_RenderFormat(lpdata))
1261 ERR("Failed to export %d format\n", lpdata->wFormatID);
1265 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, (HANDLE)lpdata->hData32,
1270 /**************************************************************************
1271 * X11DRV_CLIPBOARD_ExportEnhMetaFile
1273 * Export EnhMetaFile.
1275 HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Window requestor, Atom aTarget, Atom rprop,
1276 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1278 if (!X11DRV_CLIPBOARD_RenderFormat(lpdata))
1280 ERR("Failed to export %d format\n", lpdata->wFormatID);
1284 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, (HANDLE)lpdata->hData32,
1289 /**************************************************************************
1290 * X11DRV_CLIPBOARD_QueryTargets
1292 static BOOL X11DRV_CLIPBOARD_QueryTargets(Display *display, Window w, Atom selection, XEvent *xe)
1298 XConvertSelection(display, selection, x11drv_atom(TARGETS),
1299 x11drv_atom(SELECTION_DATA), w, CurrentTime);
1300 wine_tsx11_unlock();
1303 * Wait until SelectionNotify is received
1305 for (i = 0; i < SELECTION_RETRIES; i++)
1308 res = XCheckTypedWindowEvent(display, w, SelectionNotify, xe);
1309 wine_tsx11_unlock();
1310 if (res && xe->xselection.selection == selection) break;
1312 usleep(SELECTION_WAIT);
1315 /* Verify that the selection returned a valid TARGETS property */
1316 if ((xe->xselection.target != x11drv_atom(TARGETS)) || (xe->xselection.property == None))
1318 /* Selection owner failed to respond or we missed the SelectionNotify */
1319 WARN("Failed to retrieve TARGETS for selection %ld.\n", selection);
1327 /**************************************************************************
1328 * X11DRV_CLIPBOARD_QueryAvailableData
1330 * Caches the list of data formats available from the current selection.
1331 * This queries the selection owner for the TARGETS property and saves all
1332 * reported property types.
1334 static int X11DRV_CLIPBOARD_QueryAvailableData(LPCLIPBOARDINFO lpcbinfo)
1336 Display *display = thread_display();
1338 Atom atype=AnyPropertyType;
1340 unsigned long remain;
1341 Atom* targetList=NULL;
1343 HWND hWndClipWindow;
1344 unsigned long cSelectionTargets = 0;
1346 if (selectionAcquired & (S_PRIMARY | S_CLIPBOARD))
1348 ERR("Received request to cache selection but process is owner=(%08x)\n",
1349 (unsigned) selectionWindow);
1351 selectionAcquired = S_NOSELECTION;
1354 if (XGetSelectionOwner(display,XA_PRIMARY) == selectionWindow)
1355 selectionAcquired |= S_PRIMARY;
1357 if (XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)) == selectionWindow)
1358 selectionAcquired |= S_CLIPBOARD;
1359 wine_tsx11_unlock();
1361 if (!(selectionAcquired == (S_PRIMARY | S_CLIPBOARD)))
1363 WARN("Lost selection but process didn't process SelectClear\n");
1364 selectionWindow = None;
1368 return -1; /* Prevent self request */
1372 if (lpcbinfo->flags & CB_OWNER)
1373 hWndClipWindow = lpcbinfo->hWndOwner;
1374 else if (lpcbinfo->flags & CB_OPEN)
1375 hWndClipWindow = lpcbinfo->hWndOpen;
1377 hWndClipWindow = GetActiveWindow();
1379 if (!hWndClipWindow)
1381 WARN("No window available to retrieve selection!n");
1385 w = X11DRV_get_whole_window(GetAncestor(hWndClipWindow, GA_ROOT));
1388 * Query the selection owner for the TARGETS property
1391 if (XGetSelectionOwner(display,XA_PRIMARY) ||
1392 XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
1394 wine_tsx11_unlock();
1395 if (X11DRV_CLIPBOARD_QueryTargets(display, w, XA_PRIMARY, &xe))
1396 selectionCacheSrc = XA_PRIMARY;
1397 else if (X11DRV_CLIPBOARD_QueryTargets(display, w, x11drv_atom(CLIPBOARD), &xe))
1398 selectionCacheSrc = x11drv_atom(CLIPBOARD);
1402 else /* No selection owner so report 0 targets available */
1404 wine_tsx11_unlock();
1408 /* Read the TARGETS property contents */
1410 if(XGetWindowProperty(display, xe.xselection.requestor, xe.xselection.property,
1411 0, 0x3FFF, True, AnyPropertyType/*XA_ATOM*/, &atype, &aformat, &cSelectionTargets,
1412 &remain, (unsigned char**)&targetList) != Success)
1414 wine_tsx11_unlock();
1415 WARN("Failed to read TARGETS property\n");
1419 wine_tsx11_unlock();
1420 TRACE("Type %lx,Format %d,nItems %ld, Remain %ld\n",
1421 atype, aformat, cSelectionTargets, remain);
1423 * The TARGETS property should have returned us a list of atoms
1424 * corresponding to each selection target format supported.
1426 if ((atype == XA_ATOM || atype == x11drv_atom(TARGETS)) && aformat == 32)
1428 INT i, nb_atoms = 0;
1431 /* Cache these formats in the clipboard cache */
1432 for (i = 0; i < cSelectionTargets; i++)
1434 LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupProperty(targetList[i]);
1437 lpFormat = X11DRV_CLIPBOARD_LookupAliasProperty(targetList[i]);
1441 /* add it to the list of atoms that we don't know about yet */
1442 if (!atoms) atoms = HeapAlloc( GetProcessHeap(), 0,
1443 (cSelectionTargets - i) * sizeof(*atoms) );
1444 if (atoms) atoms[nb_atoms++] = targetList[i];
1448 TRACE("Atom#%d Property(%d): --> FormatID(%d) %s\n",
1449 i, lpFormat->drvData, lpFormat->wFormatID, lpFormat->Name);
1450 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, 0);
1454 /* query all unknown atoms in one go */
1457 char **names = HeapAlloc( GetProcessHeap(), 0, nb_atoms * sizeof(*names) );
1461 XGetAtomNames( display, atoms, nb_atoms, names );
1462 wine_tsx11_unlock();
1463 for (i = 0; i < nb_atoms; i++)
1465 WINE_CLIPFORMAT *lpFormat = register_format( names[i], atoms[i] );
1468 ERR("Failed to cache %s property\n", names[i]);
1471 TRACE("Atom#%d Property(%d): --> FormatID(%d) %s\n",
1472 i, lpFormat->drvData, lpFormat->wFormatID, lpFormat->Name);
1473 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, 0);
1476 for (i = 0; i < nb_atoms; i++) XFree( names[i] );
1477 wine_tsx11_unlock();
1478 HeapFree( GetProcessHeap(), 0, names );
1483 /* Free the list of targets */
1486 wine_tsx11_unlock();
1489 return cSelectionTargets;
1493 /**************************************************************************
1494 * X11DRV_CLIPBOARD_ReadClipboardData
1496 * This method is invoked only when we DO NOT own the X selection
1498 * We always get the data from the selection client each time,
1499 * since we have no way of determining if the data in our cache is stale.
1501 static BOOL X11DRV_CLIPBOARD_ReadClipboardData(UINT wFormat)
1503 Display *display = thread_display();
1507 HWND hWndClipWindow = GetOpenClipboardWindow();
1508 HWND hWnd = (hWndClipWindow) ? hWndClipWindow : GetActiveWindow();
1510 LPWINE_CLIPFORMAT lpFormat;
1512 TRACE("%d\n", wFormat);
1514 if (!selectionAcquired)
1516 Window w = X11DRV_get_whole_window(GetAncestor(hWnd, GA_ROOT));
1519 FIXME("No parent win found %p %p\n", hWnd, hWndClipWindow);
1523 lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
1525 if (lpFormat->drvData)
1531 TRACE("Requesting %s selection (%d) from win(%08x)\n",
1532 lpFormat->Name, lpFormat->drvData, (UINT)selectionCacheSrc);
1535 XConvertSelection(display, selectionCacheSrc, lpFormat->drvData,
1536 x11drv_atom(SELECTION_DATA), w, CurrentTime);
1537 wine_tsx11_unlock();
1539 /* wait until SelectionNotify is received */
1540 for (i = 0; i < SELECTION_RETRIES; i++)
1543 res = XCheckTypedWindowEvent(display, w, SelectionNotify, &xe);
1544 wine_tsx11_unlock();
1545 if (res && xe.xselection.selection == selectionCacheSrc) break;
1547 usleep(SELECTION_WAIT);
1550 /* If the property wasn't available check for aliases */
1551 if (xe.xselection.property == None &&
1552 (alias = X11DRV_CLIPBOARD_LookupPropertyAlias(lpFormat->drvData)))
1555 XConvertSelection(display, selectionCacheSrc, alias,
1556 x11drv_atom(SELECTION_DATA), w, CurrentTime);
1557 wine_tsx11_unlock();
1559 /* wait until SelectionNotify is received */
1560 for (i = 0; i < SELECTION_RETRIES; i++)
1563 res = XCheckTypedWindowEvent(display, w, SelectionNotify, &xe);
1564 wine_tsx11_unlock();
1565 if (res && xe.xselection.selection == selectionCacheSrc) break;
1567 usleep(SELECTION_WAIT);
1571 /* Verify that the selection returned a valid TARGETS property */
1572 if (xe.xselection.property != None)
1575 * Read the contents of the X selection property
1576 * into WINE's clipboard cache converting the
1577 * selection to be compatible if possible.
1579 bRet = X11DRV_CLIPBOARD_ReadSelection(lpFormat, xe.xselection.requestor,
1580 xe.xselection.property);
1586 ERR("Received request to cache selection data but process is owner\n");
1589 TRACE("Returning %d\n", bRet);
1595 /**************************************************************************
1596 * X11DRV_CLIPBOARD_ReadSelection
1597 * Reads the contents of the X selection property into the WINE clipboard cache
1598 * converting the selection into a format compatible with the windows clipboard
1600 * This method is invoked only to read the contents of a the selection owned
1601 * by an external application. i.e. when we do not own the X selection.
1603 static BOOL X11DRV_CLIPBOARD_ReadSelection(LPWINE_CLIPFORMAT lpData, Window w, Atom prop)
1605 Display *display = thread_display();
1606 Atom atype=AnyPropertyType;
1608 unsigned long total,nitems,remain,itemSize,val_cnt;
1609 long lRequestLength,bwc;
1611 unsigned char* buffer;
1617 TRACE("Reading X selection type %s\n", lpData->Name);
1620 * First request a zero length in order to figure out the request size.
1623 if(XGetWindowProperty(display,w,prop,0,0,False, AnyPropertyType,
1624 &atype, &aformat, &nitems, &itemSize, &val) != Success)
1626 wine_tsx11_unlock();
1627 WARN("Failed to get property size\n");
1631 /* Free zero length return data if any */
1638 TRACE("Retrieving %ld bytes\n", itemSize * aformat/8);
1640 lRequestLength = (itemSize * aformat/8)/4 + 1;
1643 /* Read property in 4K blocks */
1644 if (XGetWindowProperty(display,w,prop,0,4096,False, AnyPropertyType/*reqType*/,
1645 &atype, &aformat, &nitems, &remain, &buffer) != Success)
1647 wine_tsx11_unlock();
1648 WARN("Failed to read property\n");
1652 val = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nitems*bwc);
1653 memcpy(val,buffer,nitems*bwc);
1657 for (total = nitems*bwc, val_cnt = 0; remain;)
1659 val_cnt +=nitems*bwc;
1660 if (XGetWindowProperty(display, w, prop, (total / 4), 4096, False,
1661 AnyPropertyType, &atype, &aformat, &nitems, &remain, &buffer) != Success)
1663 wine_tsx11_unlock();
1664 WARN("Failed to read property\n");
1665 HeapFree(GetProcessHeap(), 0, val);
1669 total += nitems*bwc;
1670 HeapReAlloc(GetProcessHeap(),0,val, total);
1671 memcpy(&val[val_cnt], buffer, nitems*(aformat/8));
1674 wine_tsx11_unlock();
1676 bRet = X11DRV_CLIPBOARD_InsertClipboardData(lpData->wFormatID, 0, lpData->lpDrvImportFunc(val, total), 0);
1678 /* Delete the property on the window now that we are done
1679 * This will send a PropertyNotify event to the selection owner. */
1681 XDeleteProperty(display,w,prop);
1682 wine_tsx11_unlock();
1684 /* Free the retrieved property data */
1685 HeapFree(GetProcessHeap(),0,val);
1691 /**************************************************************************
1692 * CLIPBOARD_SerializeMetafile
1694 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out)
1698 TRACE(" wFormat=%d hdata=%08x out=%d\n", wformat, (unsigned int) hdata, out);
1700 if (out) /* Serialize out, caller should free memory */
1702 *lpcbytes = 0; /* Assume failure */
1704 if (wformat == CF_METAFILEPICT)
1706 LPMETAFILEPICT lpmfp = (LPMETAFILEPICT) GlobalLock(hdata);
1707 int size = GetMetaFileBitsEx(lpmfp->hMF, 0, NULL);
1709 h = GlobalAlloc(0, size + sizeof(METAFILEPICT));
1712 char *pdata = GlobalLock(h);
1714 memcpy(pdata, lpmfp, sizeof(METAFILEPICT));
1715 GetMetaFileBitsEx(lpmfp->hMF, size, pdata + sizeof(METAFILEPICT));
1717 *lpcbytes = size + sizeof(METAFILEPICT);
1722 GlobalUnlock(hdata);
1724 else if (wformat == CF_ENHMETAFILE)
1726 int size = GetEnhMetaFileBits(hdata, 0, NULL);
1728 h = GlobalAlloc(0, size);
1731 LPVOID pdata = GlobalLock(h);
1733 GetEnhMetaFileBits(hdata, size, pdata);
1742 if (wformat == CF_METAFILEPICT)
1744 h = GlobalAlloc(0, sizeof(METAFILEPICT));
1747 LPMETAFILEPICT pmfp = (LPMETAFILEPICT) GlobalLock(h);
1749 memcpy(pmfp, (LPVOID)hdata, sizeof(METAFILEPICT));
1750 pmfp->hMF = SetMetaFileBitsEx(*lpcbytes - sizeof(METAFILEPICT),
1751 (char *)hdata + sizeof(METAFILEPICT));
1756 else if (wformat == CF_ENHMETAFILE)
1758 h = SetEnhMetaFileBits(*lpcbytes, (LPVOID)hdata);
1766 /**************************************************************************
1767 * X11DRV_CLIPBOARD_ReleaseSelection
1769 * Release an XA_PRIMARY or XA_CLIPBOARD selection that we own, in response
1770 * to a SelectionClear event.
1771 * This can occur in response to another client grabbing the X selection.
1772 * If the XA_CLIPBOARD selection is lost, we relinquish XA_PRIMARY as well.
1774 void X11DRV_CLIPBOARD_ReleaseSelection(Atom selType, Window w, HWND hwnd)
1776 Display *display = thread_display();
1778 /* w is the window that lost the selection
1780 TRACE("event->window = %08x (selectionWindow = %08x) selectionAcquired=0x%08x\n",
1781 (unsigned)w, (unsigned)selectionWindow, (unsigned)selectionAcquired);
1783 if (selectionAcquired)
1785 if (w == selectionWindow)
1787 /* If we're losing the CLIPBOARD selection, or if the preferences in .winerc
1788 * dictate that *all* selections should be cleared on loss of a selection,
1789 * we must give up all the selections we own.
1791 if (clearAllSelections || (selType == x11drv_atom(CLIPBOARD)))
1793 CLIPBOARDINFO cbinfo;
1795 /* completely give up the selection */
1796 TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
1798 /* We are completely giving up the selection. There is a
1799 * potential race condition where the apps that now owns
1800 * the selection has already grabbed both selections. In
1801 * this case, if we clear any selection we may clear the
1802 * new owners selection. To prevent this common case we
1803 * try to open the clipboard. If we can't, we assume it
1804 * was a wine apps that took it and has taken both selections.
1805 * In this case, don't bother releasing the other selection.
1806 * Otherwise only release the selection if we still own it.
1808 X11DRV_CLIPBOARD_GetClipboardInfo(&cbinfo);
1810 if (cbinfo.flags & CB_OWNER)
1812 /* Since we're still the owner, this wasn't initiated by
1813 another Wine process */
1814 if (OpenClipboard(hwnd))
1816 /* We really lost CLIPBOARD but want to voluntarily lose PRIMARY */
1817 if ((selType == x11drv_atom(CLIPBOARD)) && (selectionAcquired & S_PRIMARY))
1819 TRACE("Lost clipboard. Check if we need to release PRIMARY\n");
1821 if (selectionWindow == XGetSelectionOwner(display,XA_PRIMARY))
1823 TRACE("We still own PRIMARY. Releasing PRIMARY.\n");
1824 XSetSelectionOwner(display, XA_PRIMARY, None, CurrentTime);
1827 TRACE("We no longer own PRIMARY\n");
1828 wine_tsx11_unlock();
1831 /* We really lost PRIMARY but want to voluntarily lose CLIPBOARD */
1832 if ((selType == XA_PRIMARY) && (selectionAcquired & S_CLIPBOARD))
1834 TRACE("Lost PRIMARY. Check if we need to release CLIPBOARD\n");
1836 if (selectionWindow == XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
1838 TRACE("We still own CLIPBOARD. Releasing CLIPBOARD.\n");
1839 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), None, CurrentTime);
1842 TRACE("We no longer own CLIPBOARD\n");
1843 wine_tsx11_unlock();
1846 /* Destroy private objects */
1847 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
1849 /* Give up ownership of the windows clipboard */
1850 X11DRV_CLIPBOARD_ReleaseOwnership();
1857 TRACE("Lost selection to other Wine process.\n");
1860 selectionWindow = None;
1861 PrimarySelectionOwner = ClipboardSelectionOwner = 0;
1863 X11DRV_EmptyClipboard();
1865 /* Reset the selection flags now that we are done */
1866 selectionAcquired = S_NOSELECTION;
1868 else if ( selType == XA_PRIMARY ) /* Give up only PRIMARY selection */
1870 TRACE("Lost PRIMARY selection\n");
1871 PrimarySelectionOwner = 0;
1872 selectionAcquired &= ~S_PRIMARY; /* clear S_PRIMARY mask */
1879 /**************************************************************************
1880 * IsSelectionOwner (X11DRV.@)
1882 * Returns: TRUE if the selection is owned by this process, FALSE otherwise
1884 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void)
1886 return selectionAcquired;
1890 /**************************************************************************
1891 * X11DRV Clipboard Exports
1892 **************************************************************************/
1895 /**************************************************************************
1896 * RegisterClipboardFormat (X11DRV.@)
1898 * Registers a custom X clipboard format
1899 * Returns: Format id or 0 on failure
1901 INT X11DRV_RegisterClipboardFormat(LPCSTR FormatName)
1903 LPWINE_CLIPFORMAT lpFormat;
1905 if (FormatName == NULL) return 0;
1906 if (!(lpFormat = register_format( FormatName, 0 ))) return 0;
1907 return lpFormat->wFormatID;
1911 /**************************************************************************
1912 * X11DRV_GetClipboardFormatName
1914 INT X11DRV_GetClipboardFormatName(UINT wFormat, LPSTR retStr, INT maxlen)
1917 LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
1919 TRACE("(%04X, %p, %d) !\n", wFormat, retStr, maxlen);
1921 if (!lpFormat || (lpFormat->wFlags & CF_FLAG_BUILTINFMT))
1923 TRACE("Unknown format 0x%08x!\n", wFormat);
1924 SetLastError(ERROR_INVALID_PARAMETER);
1928 strncpy(retStr, lpFormat->Name, maxlen - 1);
1929 retStr[maxlen - 1] = 0;
1931 len = strlen(retStr);
1938 /**************************************************************************
1939 * AcquireClipboard (X11DRV.@)
1941 void X11DRV_AcquireClipboard(HWND hWndClipWindow)
1943 Display *display = thread_display();
1946 * Acquire X selection if we don't already own it.
1947 * Note that we only acquire the selection if it hasn't been already
1948 * acquired by us, and ignore the fact that another X window may be
1949 * asserting ownership. The reason for this is we need *any* top level
1950 * X window to hold selection ownership. The actual clipboard data requests
1951 * are made via GetClipboardData from EVENT_SelectionRequest and this
1952 * ensures that the real HWND owner services the request.
1953 * If the owning X window gets destroyed the selection ownership is
1954 * re-cycled to another top level X window in X11DRV_CLIPBOARD_ResetOwner.
1957 if (!(selectionAcquired == (S_PRIMARY | S_CLIPBOARD)))
1961 if (!hWndClipWindow)
1962 hWndClipWindow = GetActiveWindow();
1964 owner = X11DRV_get_whole_window(GetAncestor(hWndClipWindow, GA_ROOT));
1967 /* Grab PRIMARY selection if not owned */
1968 if (!(selectionAcquired & S_PRIMARY))
1969 XSetSelectionOwner(display, XA_PRIMARY, owner, CurrentTime);
1971 /* Grab CLIPBOARD selection if not owned */
1972 if (!(selectionAcquired & S_CLIPBOARD))
1973 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), owner, CurrentTime);
1975 if (XGetSelectionOwner(display,XA_PRIMARY) == owner)
1976 selectionAcquired |= S_PRIMARY;
1978 if (XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)) == owner)
1979 selectionAcquired |= S_CLIPBOARD;
1980 wine_tsx11_unlock();
1982 if (selectionAcquired)
1984 selectionWindow = owner;
1985 TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner);
1990 WARN("Received request to acquire selection but process is already owner=(%08x)\n", (unsigned) selectionWindow);
1992 selectionAcquired = S_NOSELECTION;
1995 if (XGetSelectionOwner(display,XA_PRIMARY) == selectionWindow)
1996 selectionAcquired |= S_PRIMARY;
1998 if (XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)) == selectionWindow)
1999 selectionAcquired |= S_CLIPBOARD;
2000 wine_tsx11_unlock();
2002 if (!(selectionAcquired == (S_PRIMARY | S_CLIPBOARD)))
2004 WARN("Lost selection but process didn't process SelectClear\n");
2005 selectionWindow = None;
2011 /**************************************************************************
2012 * X11DRV_EmptyClipboard
2014 void X11DRV_EmptyClipboard(void)
2018 LPWINE_CLIPDATA lpData;
2019 LPWINE_CLIPDATA lpNext = ClipData;
2024 lpNext = lpData->NextData;
2025 lpData->PrevData->NextData = lpData->NextData;
2026 lpData->NextData->PrevData = lpData->PrevData;
2027 X11DRV_CLIPBOARD_FreeData(lpData);
2028 HeapFree(GetProcessHeap(), 0, lpData);
2029 } while (lpNext != lpData);
2032 TRACE(" %d entries deleted from cache.\n", ClipDataCount);
2040 /**************************************************************************
2041 * X11DRV_SetClipboardData
2043 BOOL X11DRV_SetClipboardData(UINT wFormat, HANDLE16 hData16, HANDLE hData32)
2045 BOOL bResult = FALSE;
2047 if (X11DRV_CLIPBOARD_InsertClipboardData(wFormat, hData16, hData32, 0))
2054 /**************************************************************************
2055 * CountClipboardFormats
2057 INT X11DRV_CountClipboardFormats(void)
2059 CLIPBOARDINFO cbinfo;
2061 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2063 TRACE(" count=%d\n", ClipDataCount);
2065 return ClipDataCount;
2069 /**************************************************************************
2070 * X11DRV_EnumClipboardFormats
2072 UINT X11DRV_EnumClipboardFormats(UINT wFormat)
2074 CLIPBOARDINFO cbinfo;
2075 UINT wNextFormat = 0;
2077 TRACE("(%04X)\n", wFormat);
2079 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2084 wNextFormat = ClipData->wFormatID;
2088 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormat);
2090 if (lpData && lpData->NextData != ClipData)
2091 wNextFormat = lpData->NextData->wFormatID;
2098 /**************************************************************************
2099 * X11DRV_IsClipboardFormatAvailable
2101 BOOL X11DRV_IsClipboardFormatAvailable(UINT wFormat)
2104 CLIPBOARDINFO cbinfo;
2106 TRACE("(%04X)\n", wFormat);
2108 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2110 if (wFormat != 0 && X11DRV_CLIPBOARD_LookupData(wFormat))
2113 TRACE("(%04X)- ret(%d)\n", wFormat, bRet);
2119 /**************************************************************************
2120 * GetClipboardData (USER.142)
2122 BOOL X11DRV_GetClipboardData(UINT wFormat, HANDLE16* phData16, HANDLE* phData32)
2124 CLIPBOARDINFO cbinfo;
2125 LPWINE_CLIPDATA lpRender;
2127 TRACE("(%04X)\n", wFormat);
2129 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2131 if ((lpRender = X11DRV_CLIPBOARD_LookupData(wFormat)))
2133 if ( !lpRender->hData32 )
2134 X11DRV_CLIPBOARD_RenderFormat(lpRender);
2136 /* Convert between 32 -> 16 bit data, if necessary */
2137 if (lpRender->hData32 && !lpRender->hData16)
2141 if (lpRender->wFormatID == CF_METAFILEPICT)
2142 size = sizeof(METAFILEPICT16);
2144 size = GlobalSize(lpRender->hData32);
2146 lpRender->hData16 = GlobalAlloc16(GMEM_ZEROINIT, size);
2148 if (!lpRender->hData16)
2149 ERR("(%04X) -- not enough memory in 16b heap\n", wFormat);
2152 if (lpRender->wFormatID == CF_METAFILEPICT)
2154 FIXME("\timplement function CopyMetaFilePict32to16\n");
2155 FIXME("\tin the appropriate file.\n");
2156 #ifdef SOMEONE_IMPLEMENTED_ME
2157 CopyMetaFilePict32to16(GlobalLock16(lpRender->hData16),
2158 GlobalLock(lpRender->hData32));
2163 memcpy(GlobalLock16(lpRender->hData16),
2164 GlobalLock(lpRender->hData32), size);
2167 GlobalUnlock16(lpRender->hData16);
2168 GlobalUnlock(lpRender->hData32);
2172 /* Convert between 32 -> 16 bit data, if necessary */
2173 if (lpRender->hData16 && !lpRender->hData32)
2177 if (lpRender->wFormatID == CF_METAFILEPICT)
2178 size = sizeof(METAFILEPICT16);
2180 size = GlobalSize(lpRender->hData32);
2182 lpRender->hData32 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE |
2183 GMEM_DDESHARE, size);
2185 if (lpRender->wFormatID == CF_METAFILEPICT)
2187 FIXME("\timplement function CopyMetaFilePict16to32\n");
2188 FIXME("\tin the appropriate file.\n");
2189 #ifdef SOMEONE_IMPLEMENTED_ME
2190 CopyMetaFilePict16to32(GlobalLock16(lpRender->hData32),
2191 GlobalLock(lpRender->hData16));
2196 memcpy(GlobalLock(lpRender->hData32),
2197 GlobalLock16(lpRender->hData16), size);
2200 GlobalUnlock(lpRender->hData32);
2201 GlobalUnlock16(lpRender->hData16);
2205 *phData16 = lpRender->hData16;
2208 *phData32 = lpRender->hData32;
2210 TRACE(" returning hData16(%04x) hData32(%04x) (type %d)\n",
2211 lpRender->hData16, (unsigned int) lpRender->hData32, lpRender->wFormatID);
2213 return lpRender->hData16 || lpRender->hData32;
2220 /**************************************************************************
2221 * ResetSelectionOwner (X11DRV.@)
2223 * Called from DestroyWindow() to prevent X selection from being lost when
2224 * a top level window is destroyed, by switching ownership to another top
2226 * Any top level window can own the selection. See X11DRV_CLIPBOARD_Acquire
2227 * for a more detailed description of this.
2229 void X11DRV_ResetSelectionOwner(HWND hwnd, BOOL bFooBar)
2231 Display *display = thread_display();
2232 HWND hWndClipOwner = 0;
2234 Window XWnd = X11DRV_get_whole_window(hwnd);
2235 BOOL bLostSelection = FALSE;
2236 Window selectionPrevWindow;
2238 /* There is nothing to do if we don't own the selection,
2239 * or if the X window which currently owns the selection is different
2240 * from the one passed in.
2242 if (!selectionAcquired || XWnd != selectionWindow
2243 || selectionWindow == None )
2246 if ((bFooBar && XWnd) || (!bFooBar && !XWnd))
2249 hWndClipOwner = GetClipboardOwner();
2251 TRACE("clipboard owner = %p, selection window = %08x\n",
2252 hWndClipOwner, (unsigned)selectionWindow);
2254 /* now try to salvage current selection from being destroyed by X */
2255 TRACE("checking %08x\n", (unsigned) XWnd);
2257 selectionPrevWindow = selectionWindow;
2258 selectionWindow = None;
2260 if (!(tmp = GetWindow(hwnd, GW_HWNDNEXT)))
2261 tmp = GetWindow(hwnd, GW_HWNDFIRST);
2263 if (tmp && tmp != hwnd)
2264 selectionWindow = X11DRV_get_whole_window(tmp);
2266 if (selectionWindow != None)
2268 /* We must pretend that we don't own the selection while making the switch
2269 * since a SelectionClear event will be sent to the last owner.
2270 * If there is no owner X11DRV_CLIPBOARD_ReleaseSelection will do nothing.
2272 int saveSelectionState = selectionAcquired;
2273 selectionAcquired = S_NOSELECTION;
2275 TRACE("\tswitching selection from %08x to %08x\n",
2276 (unsigned)selectionPrevWindow, (unsigned)selectionWindow);
2280 /* Assume ownership for the PRIMARY and CLIPBOARD selection */
2281 if (saveSelectionState & S_PRIMARY)
2282 XSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime);
2284 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), selectionWindow, CurrentTime);
2286 /* Restore the selection masks */
2287 selectionAcquired = saveSelectionState;
2289 /* Lose the selection if something went wrong */
2290 if (((saveSelectionState & S_PRIMARY) &&
2291 (XGetSelectionOwner(display, XA_PRIMARY) != selectionWindow)) ||
2292 (XGetSelectionOwner(display, x11drv_atom(CLIPBOARD)) != selectionWindow))
2294 bLostSelection = TRUE;
2298 /* Update selection state */
2299 if (saveSelectionState & S_PRIMARY)
2300 PrimarySelectionOwner = selectionWindow;
2302 ClipboardSelectionOwner = selectionWindow;
2304 wine_tsx11_unlock();
2308 bLostSelection = TRUE;
2313 TRACE("Lost the selection!\n");
2315 X11DRV_CLIPBOARD_ReleaseOwnership();
2316 selectionAcquired = S_NOSELECTION;
2317 ClipboardSelectionOwner = PrimarySelectionOwner = 0;
2318 selectionWindow = 0;
2323 /**************************************************************************
2324 * X11DRV_CLIPBOARD_SynthesizeData
2326 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID)
2329 LPWINE_CLIPDATA lpSource = NULL;
2331 TRACE(" %d\n", wFormatID);
2333 /* Don't need to synthesize if it already exists */
2334 if (X11DRV_CLIPBOARD_LookupData(wFormatID))
2337 if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
2339 bsyn = ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
2340 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2341 ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
2342 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2343 ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
2344 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED);
2346 else if (wFormatID == CF_ENHMETAFILE)
2348 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT)) &&
2349 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2351 else if (wFormatID == CF_METAFILEPICT)
2353 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT)) &&
2354 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2356 else if (wFormatID == CF_DIB)
2358 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) &&
2359 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2361 else if (wFormatID == CF_BITMAP)
2363 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) &&
2364 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2368 X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, 0, 0, CF_FLAG_SYNTHESIZED);
2375 /**************************************************************************
2376 * X11DRV_EndClipboardUpdate
2378 * Add locale if it hasn't already been added
2380 void X11DRV_EndClipboardUpdate(void)
2382 INT count = ClipDataCount;
2384 /* Do Unicode <-> Text <-> OEM mapping */
2385 X11DRV_CLIPBOARD_SynthesizeData(CF_UNICODETEXT);
2386 X11DRV_CLIPBOARD_SynthesizeData(CF_TEXT);
2387 X11DRV_CLIPBOARD_SynthesizeData(CF_OEMTEXT);
2389 /* Enhmetafile <-> MetafilePict mapping */
2390 X11DRV_CLIPBOARD_SynthesizeData(CF_ENHMETAFILE);
2391 X11DRV_CLIPBOARD_SynthesizeData(CF_METAFILEPICT);
2393 /* DIB <-> Bitmap mapping */
2394 X11DRV_CLIPBOARD_SynthesizeData(CF_DIB);
2395 X11DRV_CLIPBOARD_SynthesizeData(CF_BITMAP);
2397 TRACE("%d formats added to cached data\n", ClipDataCount - count);