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"
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
108 } CLIPBOARDINFO, *LPCLIPBOARDINFO;
110 typedef struct tagWINE_CLIPDATA {
116 struct tagWINE_CLIPDATA *PrevData;
117 struct tagWINE_CLIPDATA *NextData;
118 } WINE_CLIPDATA, *LPWINE_CLIPDATA;
120 typedef HANDLE (*DRVEXPORTFUNC)(Window requestor, Atom aTarget, Atom rprop,
121 LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
122 typedef HANDLE (*DRVIMPORTFUNC)(LPBYTE hData, UINT cBytes);
124 typedef struct tagWINE_CLIPFORMAT {
129 DRVIMPORTFUNC lpDrvImportFunc;
130 DRVEXPORTFUNC lpDrvExportFunc;
131 struct tagWINE_CLIPFORMAT *PrevFormat;
132 struct tagWINE_CLIPFORMAT *NextFormat;
133 } WINE_CLIPFORMAT, *LPWINE_CLIPFORMAT;
135 #define CF_FLAG_BUILTINFMT 1 /* Built-in windows format */
136 #define CF_FLAG_UNOWNED 2 /* cached data is not owned */
137 #define CF_FLAG_SYNTHESIZED 8 /* Implicitly converted data */
139 static int selectionAcquired = 0; /* Contains the current selection masks */
140 static Window selectionWindow = None; /* The top level X window which owns the selection */
141 static BOOL usePrimary = FALSE; /* Use primary selection in additon to the clipboard selection */
142 static Atom selectionCacheSrc = XA_PRIMARY; /* The selection source from which the clipboard cache was filled */
144 INT X11DRV_RegisterClipboardFormat(LPCWSTR FormatName);
145 void X11DRV_EmptyClipboard(BOOL keepunowned);
146 void X11DRV_EndClipboardUpdate(void);
147 static HANDLE X11DRV_CLIPBOARD_ImportClipboardData(LPBYTE lpdata, UINT cBytes);
148 static HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(LPBYTE lpdata, UINT cBytes);
149 static HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(LPBYTE lpdata, UINT cBytes);
150 static HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(LPBYTE lpdata, UINT cBytes);
151 static HANDLE X11DRV_CLIPBOARD_ImportXAString(LPBYTE lpdata, UINT cBytes);
152 static HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Window requestor, Atom aTarget,
153 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
154 static HANDLE X11DRV_CLIPBOARD_ExportString(Window requestor, Atom aTarget,
155 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
156 static HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Window requestor, Atom aTarget,
157 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
158 static HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Window requestor, Atom aTarget,
159 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
160 static HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Window requestor, Atom aTarget,
161 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
162 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(LPCWSTR FormatName, Atom prop);
163 static BOOL X11DRV_CLIPBOARD_ReadSelection(LPWINE_CLIPFORMAT lpData, Window w, Atom prop);
164 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(UINT wFormatID);
165 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData);
166 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void);
167 static int X11DRV_CLIPBOARD_QueryAvailableData(LPCLIPBOARDINFO lpcbinfo);
168 static BOOL X11DRV_CLIPBOARD_ReadClipboardData(UINT wFormat);
169 static BOOL X11DRV_CLIPBOARD_RenderFormat(LPWINE_CLIPDATA lpData);
170 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out);
171 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID);
172 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(LPWINE_CLIPDATA lpData);
173 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedDIB(void);
174 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedBitmap(void);
175 static void X11DRV_HandleSelectionRequest( HWND hWnd, XSelectionRequestEvent *event, BOOL bIsMultiple );
178 * WARNING: This data ordering is dependent on the WINE_CLIPFORMAT structure
179 * declared in clipboard.h
181 static const WCHAR wszCF_TEXT[] = {'W','C','F','_','T','E','X','T',0};
182 static const WCHAR wszCF_BITMAP[] = {'W','C','F','_','B','I','T','M','A','P',0};
183 static const WCHAR wszCF_METAFILEPICT[] = {'W','C','F','_','M','E','T','A','F','I','L','E','P','I','C','T',0};
184 static const WCHAR wszCF_SYLK[] = {'W','C','F','_','S','Y','L','K',0};
185 static const WCHAR wszCF_DIF[] = {'W','C','F','_','D','I','F',0};
186 static const WCHAR wszCF_TIFF[] = {'W','C','F','_','T','I','F','F',0};
187 static const WCHAR wszCF_OEMTEXT[] = {'W','C','F','_','O','E','M','T','E','X','T',0};
188 static const WCHAR wszCF_DIB[] = {'W','C','F','_','D','I','B',0};
189 static const WCHAR wszCF_PALETTE[] = {'W','C','F','_','P','A','L','E','T','T','E',0};
190 static const WCHAR wszCF_PENDATA[] = {'W','C','F','_','P','E','N','D','A','T','A',0};
191 static const WCHAR wszCF_RIFF[] = {'W','C','F','_','R','I','F','F',0};
192 static const WCHAR wszCF_WAVE[] = {'W','C','F','_','W','A','V','E',0};
193 static const WCHAR wszCF_UNICODETEXT[] = {'W','C','F','_','U','N','I','C','O','D','E','T','E','X','T',0};
194 static const WCHAR wszCF_ENHMETAFILE[] = {'W','C','F','_','E','N','H','M','E','T','A','F','I','L','E',0};
195 static const WCHAR wszCF_HDROP[] = {'W','C','F','_','H','D','R','O','P',0};
196 static const WCHAR wszCF_LOCALE[] = {'W','C','F','_','L','O','C','A','L','E',0};
197 static const WCHAR wszCF_DIBV5[] = {'W','C','F','_','D','I','B','V','5',0};
198 static const WCHAR wszCF_OWNERDISPLAY[] = {'W','C','F','_','O','W','N','E','R','D','I','S','P','L','A','Y',0};
199 static const WCHAR wszCF_DSPTEXT[] = {'W','C','F','_','D','S','P','T','E','X','T',0};
200 static const WCHAR wszCF_DSPBITMAP[] = {'W','C','F','_','D','S','P','B','I','T','M','A','P',0};
201 static const WCHAR wszCF_DSPMETAFILEPICT[] = {'W','C','F','_','D','S','P','M','E','T','A','F','I','L','E','P','I','C','T',0};
202 static const WCHAR wszCF_DSPENHMETAFILE[] = {'W','C','F','_','D','S','P','E','N','H','M','E','T','A','F','I','L','E',0};
204 static WINE_CLIPFORMAT ClipFormats[] =
206 { CF_TEXT, wszCF_TEXT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
207 X11DRV_CLIPBOARD_ExportClipboardData, NULL, &ClipFormats[1]},
209 { CF_BITMAP, wszCF_BITMAP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
210 NULL, &ClipFormats[0], &ClipFormats[2]},
212 { CF_METAFILEPICT, wszCF_METAFILEPICT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportMetaFilePict,
213 X11DRV_CLIPBOARD_ExportMetaFilePict, &ClipFormats[1], &ClipFormats[3]},
215 { CF_SYLK, wszCF_SYLK, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
216 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[2], &ClipFormats[4]},
218 { CF_DIF, wszCF_DIF, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
219 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[3], &ClipFormats[5]},
221 { CF_TIFF, wszCF_TIFF, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
222 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[4], &ClipFormats[6]},
224 { CF_OEMTEXT, wszCF_OEMTEXT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
225 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[5], &ClipFormats[7]},
227 { CF_DIB, wszCF_DIB, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportXAPIXMAP,
228 X11DRV_CLIPBOARD_ExportXAPIXMAP, &ClipFormats[6], &ClipFormats[8]},
230 { CF_PALETTE, wszCF_PALETTE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
231 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[7], &ClipFormats[9]},
233 { CF_PENDATA, wszCF_PENDATA, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
234 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[8], &ClipFormats[10]},
236 { CF_RIFF, wszCF_RIFF, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
237 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[9], &ClipFormats[11]},
239 { CF_WAVE, wszCF_WAVE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
240 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[10], &ClipFormats[12]},
242 { CF_UNICODETEXT, wszCF_UNICODETEXT, XA_STRING, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportXAString,
243 X11DRV_CLIPBOARD_ExportString, &ClipFormats[11], &ClipFormats[13]},
245 { CF_ENHMETAFILE, wszCF_ENHMETAFILE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportEnhMetaFile,
246 X11DRV_CLIPBOARD_ExportEnhMetaFile, &ClipFormats[12], &ClipFormats[14]},
248 { CF_HDROP, wszCF_HDROP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
249 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[13], &ClipFormats[15]},
251 { CF_LOCALE, wszCF_LOCALE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
252 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[14], &ClipFormats[16]},
254 { CF_DIBV5, wszCF_DIBV5, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
255 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[15], &ClipFormats[17]},
257 { CF_OWNERDISPLAY, wszCF_OWNERDISPLAY, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
258 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[16], &ClipFormats[18]},
260 { CF_DSPTEXT, wszCF_DSPTEXT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
261 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[17], &ClipFormats[19]},
263 { CF_DSPBITMAP, wszCF_DSPBITMAP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
264 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[18], &ClipFormats[20]},
266 { CF_DSPMETAFILEPICT, wszCF_DSPMETAFILEPICT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
267 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[19], &ClipFormats[21]},
269 { CF_DSPENHMETAFILE, wszCF_DSPENHMETAFILE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
270 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[20], NULL}
273 #define GET_ATOM(prop) (((prop) < FIRST_XATOM) ? (Atom)(prop) : X11DRV_Atoms[(prop) - FIRST_XATOM])
275 /* Maps X properties to Windows formats */
276 static const WCHAR wszRichTextFormat[] = {'R','i','c','h',' ','T','e','x','t',' ','F','o','r','m','a','t',0};
277 static const WCHAR wszGIF[] = {'G','I','F',0};
282 } PropertyFormatMap[] =
284 { wszRichTextFormat, XATOM_text_rtf },
285 /* Temporarily disable text/html because Evolution incorrectly pastes strings with extra nulls */
286 /*{ "text/html", "HTML Format" },*/
287 { wszGIF, XATOM_image_gif }
291 /* Maps equivalent X properties. It is assumed that lpszProperty must already
292 be in ClipFormats or PropertyFormatMap. */
295 UINT drvDataProperty;
297 } PropertyAliasMap[] =
299 /* DataProperty, DataAlias */
300 { XATOM_text_rtf, XATOM_text_richtext },
301 { XA_STRING, XATOM_COMPOUND_TEXT },
302 { XA_STRING, XATOM_TEXT },
303 { XATOM_WCF_DIB, XA_PIXMAP },
308 * Cached clipboard data.
310 static LPWINE_CLIPDATA ClipData = NULL;
311 static UINT ClipDataCount = 0;
314 * Clipboard sequence number
316 static UINT wSeqNo = 0;
318 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
320 /**************************************************************************
321 * Internal Clipboard implementation methods
322 **************************************************************************/
324 static Window thread_selection_wnd(void)
326 Window w = x11drv_thread_data()->selection_wnd;
331 w = XCreateWindow(thread_display(), root_window, 0, 0, 1, 1, 0, screen_depth,
332 InputOutput, CopyFromParent, 0, NULL);
336 x11drv_thread_data()->selection_wnd = w;
338 FIXME("Failed to create window. Fetching selection data will fail.\n");
344 /**************************************************************************
345 * X11DRV_InitClipboard
347 void X11DRV_InitClipboard(void)
352 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Clipboard", &hkey))
355 DWORD type, count = sizeof(buffer);
356 if(!RegQueryValueExA(hkey, "UsePrimary", 0, &type, buffer, &count))
357 usePrimary = IS_OPTION_TRUE( buffer[0] );
361 /* Register known mapping between window formats and X properties */
362 for (i = 0; i < sizeof(PropertyFormatMap)/sizeof(PropertyFormatMap[0]); i++)
363 X11DRV_CLIPBOARD_InsertClipboardFormat(PropertyFormatMap[i].lpszFormat,
364 GET_ATOM(PropertyFormatMap[i].prop));
368 /**************************************************************************
371 * Intern atoms for formats that don't have one yet.
373 static void intern_atoms(void)
375 LPWINE_CLIPFORMAT format;
380 for (format = ClipFormats, count = 0; format; format = format->NextFormat)
381 if (!format->drvData) count++;
384 names = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*names) );
385 atoms = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*atoms) );
387 for (format = ClipFormats, i = 0; format; format = format->NextFormat) {
388 if (!format->drvData) {
389 len = WideCharToMultiByte(CP_UNIXCP, 0, format->Name, -1, NULL, -1, 0, 0);
390 names[i] = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
391 WideCharToMultiByte(CP_UNIXCP, 0, format->Name, -1, names[i++], len, 0, 0);
396 XInternAtoms( thread_display(), names, count, False, atoms );
399 for (format = ClipFormats, i = 0; format; format = format->NextFormat) {
400 if (!format->drvData) {
401 HeapFree(GetProcessHeap(), 0, names[i]);
402 format->drvData = atoms[i++];
406 HeapFree( GetProcessHeap(), 0, names );
407 HeapFree( GetProcessHeap(), 0, atoms );
411 /**************************************************************************
414 * Register a custom X clipboard format.
416 static WINE_CLIPFORMAT *register_format( LPCWSTR FormatName, Atom prop )
418 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
420 TRACE("%s\n", debugstr_w(FormatName));
422 /* walk format chain to see if it's already registered */
425 if (CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, lpFormat->Name, -1, FormatName, -1) == CSTR_EQUAL
426 && (lpFormat->wFlags & CF_FLAG_BUILTINFMT) == 0)
428 lpFormat = lpFormat->NextFormat;
431 return X11DRV_CLIPBOARD_InsertClipboardFormat(FormatName, prop);
435 /**************************************************************************
436 * X11DRV_CLIPBOARD_LookupFormat
438 LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupFormat(WORD wID)
440 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
444 if (lpFormat->wFormatID == wID)
447 lpFormat = lpFormat->NextFormat;
449 if (lpFormat && !lpFormat->drvData) intern_atoms();
454 /**************************************************************************
455 * X11DRV_CLIPBOARD_LookupProperty
457 LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupProperty(UINT drvData)
461 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
462 BOOL need_intern = FALSE;
466 if (lpFormat->drvData == drvData) return lpFormat;
467 if (!lpFormat->drvData) need_intern = TRUE;
468 lpFormat = lpFormat->NextFormat;
470 if (!need_intern) return NULL;
472 /* restart the search for the new atoms */
477 /**************************************************************************
478 * X11DRV_CLIPBOARD_LookupAliasProperty
480 LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupAliasProperty(UINT drvDataAlias)
483 LPWINE_CLIPFORMAT lpFormat = NULL;
485 for (i = 0; i < sizeof(PropertyAliasMap)/sizeof(PropertyAliasMap[0]); i++)
487 if (GET_ATOM(PropertyAliasMap[i].drvDataAlias) == drvDataAlias)
489 lpFormat = X11DRV_CLIPBOARD_LookupProperty(GET_ATOM(PropertyAliasMap[i].drvDataProperty));
498 /**************************************************************************
499 * X11DRV_CLIPBOARD_LookupPropertyAlias
501 UINT X11DRV_CLIPBOARD_LookupPropertyAlias(UINT drvDataProperty)
506 for (i = 0; i < sizeof(PropertyAliasMap)/sizeof(PropertyAliasMap[0]); i++)
508 if (GET_ATOM(PropertyAliasMap[i].drvDataProperty) == drvDataProperty)
510 alias = GET_ATOM(PropertyAliasMap[i].drvDataAlias);
519 /**************************************************************************
520 * X11DRV_CLIPBOARD_LookupData
522 LPWINE_CLIPDATA X11DRV_CLIPBOARD_LookupData(DWORD wID)
524 LPWINE_CLIPDATA lpData = ClipData;
530 if (lpData->wFormatID == wID)
533 lpData = lpData->NextData;
535 while(lpData != ClipData);
537 if (lpData->wFormatID != wID)
545 /**************************************************************************
546 * InsertClipboardFormat
548 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(LPCWSTR FormatName, Atom prop)
550 LPWINE_CLIPFORMAT lpFormat;
551 LPWINE_CLIPFORMAT lpNewFormat;
553 /* allocate storage for new format entry */
554 lpNewFormat = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPFORMAT));
556 if(lpNewFormat == NULL)
558 WARN("No more memory for a new format!\n");
562 if (!(lpNewFormat->Name = HeapAlloc(GetProcessHeap(), 0, (strlenW(FormatName)+1)*sizeof(WCHAR))))
564 WARN("No more memory for the new format name!\n");
565 HeapFree(GetProcessHeap(), 0, lpNewFormat);
569 strcpyW((LPWSTR)lpNewFormat->Name, FormatName);
570 lpNewFormat->wFlags = 0;
571 lpNewFormat->wFormatID = GlobalAddAtomW(lpNewFormat->Name);
572 lpNewFormat->drvData = prop;
573 lpNewFormat->lpDrvImportFunc = X11DRV_CLIPBOARD_ImportClipboardData;
574 lpNewFormat->lpDrvExportFunc = X11DRV_CLIPBOARD_ExportClipboardData;
577 lpFormat = ClipFormats;
579 while(lpFormat->NextFormat) /* Move to last entry */
580 lpFormat = lpFormat->NextFormat;
582 lpNewFormat->NextFormat = NULL;
583 lpFormat->NextFormat = lpNewFormat;
584 lpNewFormat->PrevFormat = lpFormat;
586 TRACE("Registering format(%d): %s drvData %d\n",
587 lpNewFormat->wFormatID, debugstr_w(FormatName), lpNewFormat->drvData);
595 /**************************************************************************
596 * X11DRV_CLIPBOARD_GetClipboardInfo
598 static BOOL X11DRV_CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
602 SERVER_START_REQ( set_clipboard_info )
606 if (wine_server_call_err( req ))
608 ERR("Failed to get clipboard owner.\n");
612 cbInfo->hWndOpen = reply->old_clipboard;
613 cbInfo->hWndOwner = reply->old_owner;
614 cbInfo->hWndViewer = reply->old_viewer;
615 cbInfo->seqno = reply->seqno;
616 cbInfo->flags = reply->flags;
627 /**************************************************************************
628 * X11DRV_CLIPBOARD_ReleaseOwnership
630 static BOOL X11DRV_CLIPBOARD_ReleaseOwnership(void)
634 SERVER_START_REQ( set_clipboard_info )
636 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
638 if (wine_server_call_err( req ))
640 ERR("Failed to set clipboard.\n");
654 /**************************************************************************
655 * X11DRV_CLIPBOARD_InsertClipboardData
657 * Caller *must* have the clipboard open and be the owner.
659 static BOOL X11DRV_CLIPBOARD_InsertClipboardData(UINT wFormat, HANDLE16 hData16, HANDLE hData32, DWORD flags)
661 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormat);
663 TRACE("format=%d lpData=%p hData16=%08x hData32=%08x flags=0x%08lx\n",
664 wFormat, lpData, hData16, (unsigned int)hData32, flags);
668 X11DRV_CLIPBOARD_FreeData(lpData);
670 lpData->hData16 = hData16; /* 0 is legal, see WM_RENDERFORMAT */
671 lpData->hData32 = hData32;
675 lpData = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPDATA));
677 lpData->wFormatID = wFormat;
678 lpData->hData16 = hData16; /* 0 is legal, see WM_RENDERFORMAT */
679 lpData->hData32 = hData32;
684 LPWINE_CLIPDATA lpPrevData = ClipData->PrevData;
686 lpData->PrevData = lpPrevData;
687 lpData->NextData = ClipData;
689 lpPrevData->NextData = lpData;
690 ClipData->PrevData = lpData;
694 lpData->NextData = lpData;
695 lpData->PrevData = lpData;
702 lpData->wFlags = flags;
708 /**************************************************************************
709 * X11DRV_CLIPBOARD_FreeData
711 * Free clipboard data handle.
713 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData)
715 TRACE("%d\n", lpData->wFormatID);
717 if ((lpData->wFormatID >= CF_GDIOBJFIRST &&
718 lpData->wFormatID <= CF_GDIOBJLAST) ||
719 lpData->wFormatID == CF_BITMAP ||
720 lpData->wFormatID == CF_DIB ||
721 lpData->wFormatID == CF_PALETTE)
724 DeleteObject(lpData->hData32);
727 DeleteObject(HGDIOBJ_32(lpData->hData16));
729 if ((lpData->wFormatID == CF_DIB) && lpData->drvData)
730 XFreePixmap(gdi_display, lpData->drvData);
732 else if (lpData->wFormatID == CF_METAFILEPICT)
736 DeleteMetaFile(((METAFILEPICT *)GlobalLock( lpData->hData32 ))->hMF );
737 GlobalFree(lpData->hData32);
740 /* HMETAFILE16 and HMETAFILE32 are apparently the same thing,
741 and a shallow copy is enough to share a METAFILEPICT
742 structure between 16bit and 32bit clipboards. The MetaFile
743 should of course only be deleted once. */
744 GlobalFree16(lpData->hData16);
749 METAFILEPICT16* lpMetaPict = (METAFILEPICT16 *) GlobalLock16(lpData->hData16);
753 DeleteMetaFile16(lpMetaPict->hMF);
757 GlobalFree16(lpData->hData16);
760 else if (lpData->wFormatID == CF_ENHMETAFILE)
763 DeleteEnhMetaFile(lpData->hData32);
765 else if (lpData->wFormatID < CF_PRIVATEFIRST ||
766 lpData->wFormatID > CF_PRIVATELAST)
769 GlobalFree(lpData->hData32);
772 GlobalFree16(lpData->hData16);
781 /**************************************************************************
782 * X11DRV_CLIPBOARD_UpdateCache
784 static BOOL X11DRV_CLIPBOARD_UpdateCache(LPCLIPBOARDINFO lpcbinfo)
788 if (!X11DRV_CLIPBOARD_IsSelectionOwner())
790 if (!X11DRV_CLIPBOARD_GetClipboardInfo(lpcbinfo))
792 ERR("Failed to retrieve clipboard information.\n");
795 else if (wSeqNo < lpcbinfo->seqno)
797 X11DRV_EmptyClipboard(TRUE);
799 if (X11DRV_CLIPBOARD_QueryAvailableData(lpcbinfo) < 0)
801 ERR("Failed to cache clipboard data owned by another process.\n");
806 X11DRV_EndClipboardUpdate();
809 wSeqNo = lpcbinfo->seqno;
817 /**************************************************************************
818 * X11DRV_CLIPBOARD_RenderFormat
820 static BOOL X11DRV_CLIPBOARD_RenderFormat(LPWINE_CLIPDATA lpData)
824 TRACE(" 0x%04x hData32(0x%08x) hData16(0x%08x)\n",
825 lpData->wFormatID, (unsigned int)lpData->hData32, lpData->hData16);
827 if (lpData->hData32 || lpData->hData16)
828 return bret; /* Already rendered */
830 if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
831 bret = X11DRV_CLIPBOARD_RenderSynthesizedFormat(lpData);
832 else if (!X11DRV_CLIPBOARD_IsSelectionOwner())
834 if (!X11DRV_CLIPBOARD_ReadClipboardData(lpData->wFormatID))
836 ERR("Failed to cache clipboard data owned by another process. Format=%d\n",
843 CLIPBOARDINFO cbInfo;
845 if (X11DRV_CLIPBOARD_GetClipboardInfo(&cbInfo) && cbInfo.hWndOwner)
847 /* Send a WM_RENDERFORMAT message to notify the owner to render the
848 * data requested into the clipboard.
850 TRACE("Sending WM_RENDERFORMAT message to hwnd(%p)\n", cbInfo.hWndOwner);
851 SendMessageW(cbInfo.hWndOwner, WM_RENDERFORMAT, (WPARAM)lpData->wFormatID, 0);
853 if (!lpData->hData32 && !lpData->hData16)
858 ERR("hWndClipOwner is lost!\n");
867 /**************************************************************************
868 * CLIPBOARD_ConvertText
869 * Returns number of required/converted characters - not bytes!
871 static INT CLIPBOARD_ConvertText(WORD src_fmt, void const *src, INT src_size,
872 WORD dst_fmt, void *dst, INT dst_size)
876 if(src_fmt == CF_UNICODETEXT)
889 return WideCharToMultiByte(cp, 0, src, src_size, dst, dst_size, NULL, NULL);
892 if(dst_fmt == CF_UNICODETEXT)
905 return MultiByteToWideChar(cp, 0, src, src_size, dst, dst_size);
908 if(!dst_size) return src_size;
910 if(dst_size > src_size) dst_size = src_size;
912 if(src_fmt == CF_TEXT )
913 CharToOemBuffA(src, dst, dst_size);
915 OemToCharBuffA(src, dst, dst_size);
921 /**************************************************************************
922 * X11DRV_CLIPBOARD_RenderSynthesizedFormat
924 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(LPWINE_CLIPDATA lpData)
930 if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
932 UINT wFormatID = lpData->wFormatID;
934 if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
935 bret = X11DRV_CLIPBOARD_RenderSynthesizedText(wFormatID);
941 bret = X11DRV_CLIPBOARD_RenderSynthesizedDIB();
945 bret = X11DRV_CLIPBOARD_RenderSynthesizedBitmap();
949 case CF_METAFILEPICT:
950 FIXME("Synthesizing wFormatID(0x%08x) not implemented\n", wFormatID);
954 FIXME("Called to synthesize unknown format\n");
959 lpData->wFlags &= ~CF_FLAG_SYNTHESIZED;
966 /**************************************************************************
967 * X11DRV_CLIPBOARD_RenderSynthesizedText
969 * Renders synthesized text
971 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(UINT wFormatID)
976 INT src_chars, dst_chars, alloc_size;
977 LPWINE_CLIPDATA lpSource = NULL;
979 TRACE(" %d\n", wFormatID);
981 if ((lpSource = X11DRV_CLIPBOARD_LookupData(wFormatID)) &&
985 /* Look for rendered source or non-synthesized source */
986 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
987 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
989 TRACE("UNICODETEXT -> %d\n", wFormatID);
991 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
992 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
994 TRACE("TEXT -> %d\n", wFormatID);
996 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
997 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
999 TRACE("OEMTEXT -> %d\n", wFormatID);
1002 if (!lpSource || (lpSource->wFlags & CF_FLAG_SYNTHESIZED &&
1003 !lpSource->hData32))
1006 /* Ask the clipboard owner to render the source text if necessary */
1007 if (!lpSource->hData32 && !X11DRV_CLIPBOARD_RenderFormat(lpSource))
1010 if (lpSource->hData32)
1012 lpstrS = (LPSTR)GlobalLock(lpSource->hData32);
1016 lpstrS = (LPSTR)GlobalLock16(lpSource->hData16);
1022 /* Text always NULL terminated */
1023 if(lpSource->wFormatID == CF_UNICODETEXT)
1024 src_chars = strlenW((LPCWSTR)lpstrS) + 1;
1026 src_chars = strlen(lpstrS) + 1;
1028 /* Calculate number of characters in the destination buffer */
1029 dst_chars = CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS,
1030 src_chars, wFormatID, NULL, 0);
1035 TRACE("Converting from '%d' to '%d', %i chars\n",
1036 lpSource->wFormatID, wFormatID, src_chars);
1038 /* Convert characters to bytes */
1039 if(wFormatID == CF_UNICODETEXT)
1040 alloc_size = dst_chars * sizeof(WCHAR);
1042 alloc_size = dst_chars;
1044 hData32 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE |
1045 GMEM_DDESHARE, alloc_size);
1047 lpstrT = (LPSTR)GlobalLock(hData32);
1051 CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS, src_chars,
1052 wFormatID, lpstrT, dst_chars);
1053 GlobalUnlock(hData32);
1057 if (lpSource->hData32)
1058 GlobalUnlock(lpSource->hData32);
1060 GlobalUnlock16(lpSource->hData16);
1062 return X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, 0, hData32, 0);
1066 /**************************************************************************
1067 * X11DRV_CLIPBOARD_RenderSynthesizedDIB
1069 * Renders synthesized DIB
1071 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedDIB()
1074 LPWINE_CLIPDATA lpSource = NULL;
1078 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) && lpSource->hData32)
1082 /* If we have a bitmap and it's not synthesized or it has been rendered */
1083 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) &&
1084 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
1086 /* Render source if required */
1087 if (lpSource->hData32 || X11DRV_CLIPBOARD_RenderFormat(lpSource))
1093 hData32 = X11DRV_DIB_CreateDIBFromBitmap(hdc, lpSource->hData32);
1094 ReleaseDC(NULL, hdc);
1098 X11DRV_CLIPBOARD_InsertClipboardData(CF_DIB, 0, hData32, 0);
1108 /**************************************************************************
1109 * X11DRV_CLIPBOARD_RenderSynthesizedBitmap
1111 * Renders synthesized bitmap
1113 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedBitmap()
1116 LPWINE_CLIPDATA lpSource = NULL;
1120 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) && lpSource->hData32)
1124 /* If we have a dib and it's not synthesized or it has been rendered */
1125 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) &&
1126 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
1128 /* Render source if required */
1129 if (lpSource->hData32 || X11DRV_CLIPBOARD_RenderFormat(lpSource))
1133 unsigned int offset;
1134 LPBITMAPINFOHEADER lpbmih;
1137 lpbmih = (LPBITMAPINFOHEADER) GlobalLock(lpSource->hData32);
1139 offset = sizeof(BITMAPINFOHEADER)
1140 + ((lpbmih->biBitCount <= 8) ? (sizeof(RGBQUAD) *
1141 (1 << lpbmih->biBitCount)) : 0);
1143 hData32 = CreateDIBitmap(hdc, lpbmih, CBM_INIT, (LPBYTE)lpbmih +
1144 offset, (LPBITMAPINFO) lpbmih, DIB_RGB_COLORS);
1146 GlobalUnlock(lpSource->hData32);
1147 ReleaseDC(NULL, hdc);
1151 X11DRV_CLIPBOARD_InsertClipboardData(CF_BITMAP, 0, hData32, 0);
1161 /**************************************************************************
1162 * X11DRV_CLIPBOARD_ImportXAString
1164 * Import XA_STRING, converting the string to CF_UNICODE.
1166 HANDLE X11DRV_CLIPBOARD_ImportXAString(LPBYTE lpdata, UINT cBytes)
1169 UINT i, inlcount = 0;
1170 HANDLE hUnicodeText = 0;
1172 for (i = 0; i <= cBytes; i++)
1174 if (lpdata[i] == '\n')
1178 if ((lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cBytes + inlcount + 1)))
1182 for (i = 0, inlcount = 0; i <= cBytes; i++)
1184 if (lpdata[i] == '\n')
1185 lpstr[inlcount++] = '\r';
1187 lpstr[inlcount++] = lpdata[i];
1190 count = MultiByteToWideChar(CP_UNIXCP, 0, lpstr, -1, NULL, 0);
1191 hUnicodeText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, count * sizeof(WCHAR));
1195 WCHAR *textW = GlobalLock(hUnicodeText);
1196 MultiByteToWideChar(CP_UNIXCP, 0, lpstr, -1, textW, count);
1197 GlobalUnlock(hUnicodeText);
1200 HeapFree(GetProcessHeap(), 0, lpstr);
1203 return hUnicodeText;
1207 /**************************************************************************
1208 * X11DRV_CLIPBOARD_ImportXAPIXMAP
1210 * Import XA_PIXMAP, converting the image to CF_DIB.
1212 HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(LPBYTE lpdata, UINT cBytes)
1214 HANDLE hTargetImage = 0; /* Handle to store the converted DIB */
1215 Pixmap *pPixmap = (Pixmap *) lpdata;
1216 HWND hwnd = GetOpenClipboardWindow();
1217 HDC hdc = GetDC(hwnd);
1219 hTargetImage = X11DRV_DIB_CreateDIBFromPixmap(*pPixmap, hdc);
1221 ReleaseDC(hwnd, hdc);
1223 return hTargetImage;
1227 /**************************************************************************
1228 * X11DRV_CLIPBOARD_ImportMetaFilePict
1230 * Import MetaFilePict.
1232 HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(LPBYTE lpdata, UINT cBytes)
1234 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, (HANDLE)lpdata, (LPDWORD)&cBytes, FALSE);
1238 /**************************************************************************
1239 * X11DRV_ImportEnhMetaFile
1241 * Import EnhMetaFile.
1243 HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(LPBYTE lpdata, UINT cBytes)
1245 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, (HANDLE)lpdata, (LPDWORD)&cBytes, FALSE);
1249 /**************************************************************************
1250 * X11DRV_ImportClipbordaData
1252 * Generic import clipboard data routine.
1254 HANDLE X11DRV_CLIPBOARD_ImportClipboardData(LPBYTE lpdata, UINT cBytes)
1257 HANDLE hClipData = 0;
1261 /* Turn on the DDESHARE flag to enable shared 32 bit memory */
1262 hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cBytes);
1263 if (hClipData == 0) return NULL;
1265 if ((lpClipData = GlobalLock(hClipData)))
1267 memcpy(lpClipData, lpdata, cBytes);
1268 GlobalUnlock(hClipData);
1271 GlobalFree(hClipData);
1280 /**************************************************************************
1281 X11DRV_CLIPBOARD_ExportClipboardData
1283 * Generic export clipboard data routine.
1285 HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Window requestor, Atom aTarget,
1286 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1290 HANDLE hClipData = 0;
1292 *lpBytes = 0; /* Assume failure */
1294 if (!X11DRV_CLIPBOARD_RenderFormat(lpData))
1295 ERR("Failed to export %d format\n", lpData->wFormatID);
1298 cBytes = GlobalSize(lpData->hData32);
1300 hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cBytes);
1301 if (hClipData == 0) return NULL;
1303 if ((lpClipData = GlobalLock(hClipData)))
1305 LPVOID lpdata = GlobalLock(lpData->hData32);
1307 memcpy(lpClipData, lpdata, cBytes);
1310 GlobalUnlock(lpData->hData32);
1311 GlobalUnlock(hClipData);
1313 GlobalFree(hClipData);
1322 /**************************************************************************
1323 * X11DRV_CLIPBOARD_ExportXAString
1325 * Export CF_UNICODE converting the string to XA_STRING.
1326 * Helper function for X11DRV_CLIPBOARD_ExportString.
1328 HANDLE X11DRV_CLIPBOARD_ExportXAString(LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1333 LPSTR text, lpstr = NULL;
1335 *lpBytes = 0; /* Assume return has zero bytes */
1337 uni_text = GlobalLock(lpData->hData32);
1339 size = WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, NULL, 0, NULL, NULL);
1341 text = HeapAlloc(GetProcessHeap(), 0, size);
1342 if (!text) goto done;
1343 WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, text, size, NULL, NULL);
1345 /* remove carriage returns */
1347 lpstr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size-- );
1348 if(lpstr == NULL) goto done;
1350 for(i = 0,j = 0; i < size && text[i]; i++ )
1352 if( text[i] == '\r' &&
1353 (text[i+1] == '\n' || text[i+1] == '\0') ) continue;
1354 lpstr[j++] = text[i];
1358 *lpBytes = j; /* Number of bytes in string */
1361 HeapFree(GetProcessHeap(), 0, text);
1362 GlobalUnlock(lpData->hData32);
1368 /**************************************************************************
1369 * X11DRV_CLIPBOARD_ExportCompoundText
1371 * Export CF_UNICODE to COMPOUND_TEXT or TEXT
1372 * Helper function for X11DRV_CLIPBOARD_ExportString.
1374 HANDLE X11DRV_CLIPBOARD_ExportCompoundText(Window requestor, Atom aTarget, Atom rprop,
1375 LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1377 Display *display = thread_display();
1380 XICCEncodingStyle style;
1382 lpstr = (char*) X11DRV_CLIPBOARD_ExportXAString(lpData, lpBytes);
1386 if (aTarget == x11drv_atom(COMPOUND_TEXT))
1387 style = XCompoundTextStyle;
1389 style = XStdICCTextStyle;
1391 /* Update the X property */
1393 if (XmbTextListToTextProperty(display, &lpstr, 1, style, &prop) == Success)
1395 XSetTextProperty(display, requestor, &prop, rprop);
1398 wine_tsx11_unlock();
1400 HeapFree( GetProcessHeap(), 0, lpstr );
1406 /**************************************************************************
1407 * X11DRV_CLIPBOARD_ExportString
1409 * Export CF_UNICODE string
1411 HANDLE X11DRV_CLIPBOARD_ExportString(Window requestor, Atom aTarget, Atom rprop,
1412 LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1414 if (X11DRV_CLIPBOARD_RenderFormat(lpData))
1416 if (aTarget == XA_STRING)
1417 return X11DRV_CLIPBOARD_ExportXAString(lpData, lpBytes);
1418 else if (aTarget == x11drv_atom(COMPOUND_TEXT) || aTarget == x11drv_atom(TEXT))
1419 return X11DRV_CLIPBOARD_ExportCompoundText(requestor, aTarget,
1420 rprop, lpData, lpBytes);
1422 ERR("Unknown target %ld to %d format\n", aTarget, lpData->wFormatID);
1425 ERR("Failed to render %d format\n", lpData->wFormatID);
1431 /**************************************************************************
1432 * X11DRV_CLIPBOARD_ExportXAPIXMAP
1434 * Export CF_DIB to XA_PIXMAP.
1436 HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Window requestor, Atom aTarget, Atom rprop,
1437 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1441 unsigned char* lpData;
1443 if (!X11DRV_CLIPBOARD_RenderFormat(lpdata))
1445 ERR("Failed to export %d format\n", lpdata->wFormatID);
1449 if (!lpdata->drvData) /* If not already rendered */
1451 /* For convert from packed DIB to Pixmap */
1453 lpdata->drvData = (UINT) X11DRV_DIB_CreatePixmapFromDIB(lpdata->hData32, hdc);
1457 *lpBytes = sizeof(Pixmap); /* pixmap is a 32bit value */
1459 /* Wrap pixmap so we can return a handle */
1460 hData = GlobalAlloc(0, *lpBytes);
1461 lpData = GlobalLock(hData);
1462 memcpy(lpData, &lpdata->drvData, *lpBytes);
1463 GlobalUnlock(hData);
1465 return (HANDLE) hData;
1469 /**************************************************************************
1470 * X11DRV_CLIPBOARD_ExportMetaFilePict
1472 * Export MetaFilePict.
1474 HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Window requestor, Atom aTarget, Atom rprop,
1475 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1477 if (!X11DRV_CLIPBOARD_RenderFormat(lpdata))
1479 ERR("Failed to export %d format\n", lpdata->wFormatID);
1483 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, (HANDLE)lpdata->hData32,
1488 /**************************************************************************
1489 * X11DRV_CLIPBOARD_ExportEnhMetaFile
1491 * Export EnhMetaFile.
1493 HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Window requestor, Atom aTarget, Atom rprop,
1494 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1496 if (!X11DRV_CLIPBOARD_RenderFormat(lpdata))
1498 ERR("Failed to export %d format\n", lpdata->wFormatID);
1502 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, (HANDLE)lpdata->hData32,
1507 /**************************************************************************
1508 * X11DRV_CLIPBOARD_QueryTargets
1510 static BOOL X11DRV_CLIPBOARD_QueryTargets(Display *display, Window w, Atom selection,
1511 Atom target, XEvent *xe)
1517 XConvertSelection(display, selection, target,
1518 x11drv_atom(SELECTION_DATA), w, CurrentTime);
1519 wine_tsx11_unlock();
1522 * Wait until SelectionNotify is received
1524 for (i = 0; i < SELECTION_RETRIES; i++)
1527 res = XCheckTypedWindowEvent(display, w, SelectionNotify, xe);
1528 wine_tsx11_unlock();
1529 if (res && xe->xselection.selection == selection) break;
1531 usleep(SELECTION_WAIT);
1534 /* Verify that the selection returned a valid TARGETS property */
1535 if ((xe->xselection.target != target) || (xe->xselection.property == None))
1537 /* Selection owner failed to respond or we missed the SelectionNotify */
1538 WARN("Failed to retrieve TARGETS for selection %ld.\n", selection);
1546 /**************************************************************************
1547 * X11DRV_CLIPBOARD_InsertSelectionProperties
1549 * Mark property available for future retrieval.
1551 static VOID X11DRV_CLIPBOARD_InsertSelectionProperties(Display *display, Atom* properties, UINT count)
1553 UINT i, nb_atoms = 0;
1556 /* Cache these formats in the clipboard cache */
1557 for (i = 0; i < count; i++)
1559 LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupProperty(properties[i]);
1562 lpFormat = X11DRV_CLIPBOARD_LookupAliasProperty(properties[i]);
1566 /* add it to the list of atoms that we don't know about yet */
1567 if (!atoms) atoms = HeapAlloc( GetProcessHeap(), 0,
1568 (count - i) * sizeof(*atoms) );
1569 if (atoms) atoms[nb_atoms++] = properties[i];
1573 TRACE("Atom#%d Property(%d): --> FormatID(%d) %s\n",
1574 i, lpFormat->drvData, lpFormat->wFormatID, debugstr_w(lpFormat->Name));
1575 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, 0);
1579 /* query all unknown atoms in one go */
1582 char **names = HeapAlloc( GetProcessHeap(), 0, nb_atoms * sizeof(*names) );
1586 XGetAtomNames( display, atoms, nb_atoms, names );
1587 wine_tsx11_unlock();
1588 for (i = 0; i < nb_atoms; i++)
1590 WINE_CLIPFORMAT *lpFormat;
1592 int len = MultiByteToWideChar(CP_UNIXCP, 0, names[i], -1, NULL, 0);
1593 wname = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1594 MultiByteToWideChar(CP_UNIXCP, 0, names[i], -1, wname, len);
1596 lpFormat = register_format( wname, atoms[i] );
1597 HeapFree(GetProcessHeap(), 0, wname);
1600 ERR("Failed to register %s property. Type will not be cached.\n", names[i]);
1603 TRACE("Atom#%d Property(%d): --> FormatID(%d) %s\n",
1604 i, lpFormat->drvData, lpFormat->wFormatID, debugstr_w(lpFormat->Name));
1605 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, 0);
1608 for (i = 0; i < nb_atoms; i++) XFree( names[i] );
1609 wine_tsx11_unlock();
1610 HeapFree( GetProcessHeap(), 0, names );
1612 HeapFree( GetProcessHeap(), 0, atoms );
1617 /**************************************************************************
1618 * X11DRV_CLIPBOARD_QueryAvailableData
1620 * Caches the list of data formats available from the current selection.
1621 * This queries the selection owner for the TARGETS property and saves all
1622 * reported property types.
1624 static int X11DRV_CLIPBOARD_QueryAvailableData(LPCLIPBOARDINFO lpcbinfo)
1626 Display *display = thread_display();
1628 Atom atype=AnyPropertyType;
1630 unsigned long remain;
1631 Atom* targetList=NULL;
1633 unsigned long cSelectionTargets = 0;
1635 if (selectionAcquired & (S_PRIMARY | S_CLIPBOARD))
1637 ERR("Received request to cache selection but process is owner=(%08x)\n",
1638 (unsigned) selectionWindow);
1639 return -1; /* Prevent self request */
1642 w = thread_selection_wnd();
1645 ERR("No window available to retrieve selection!\n");
1650 * Query the selection owner for the TARGETS property
1653 if ((usePrimary && XGetSelectionOwner(display,XA_PRIMARY)) ||
1654 XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
1656 wine_tsx11_unlock();
1657 if (usePrimary && (X11DRV_CLIPBOARD_QueryTargets(display, w, XA_PRIMARY, x11drv_atom(TARGETS), &xe)))
1658 selectionCacheSrc = XA_PRIMARY;
1659 else if (X11DRV_CLIPBOARD_QueryTargets(display, w, x11drv_atom(CLIPBOARD), x11drv_atom(TARGETS), &xe))
1660 selectionCacheSrc = x11drv_atom(CLIPBOARD);
1663 Atom xstr = XA_PRIMARY;
1665 /* Selection Owner doesn't understand TARGETS, try retrieving XA_STRING */
1666 if (X11DRV_CLIPBOARD_QueryTargets(display, w, XA_PRIMARY, XA_STRING, &xe))
1668 X11DRV_CLIPBOARD_InsertSelectionProperties(display, &xstr, 1);
1669 selectionCacheSrc = XA_PRIMARY;
1672 else if (X11DRV_CLIPBOARD_QueryTargets(display, w, x11drv_atom(CLIPBOARD), XA_STRING, &xe))
1674 X11DRV_CLIPBOARD_InsertSelectionProperties(display, &xstr, 1);
1675 selectionCacheSrc = x11drv_atom(CLIPBOARD);
1680 WARN("Failed to query selection owner for available data.\n");
1685 else /* No selection owner so report 0 targets available */
1687 wine_tsx11_unlock();
1691 /* Read the TARGETS property contents */
1693 if(XGetWindowProperty(display, xe.xselection.requestor, xe.xselection.property,
1694 0, 0x3FFF, True, AnyPropertyType/*XA_ATOM*/, &atype, &aformat, &cSelectionTargets,
1695 &remain, (unsigned char**)&targetList) != Success)
1697 wine_tsx11_unlock();
1698 WARN("Failed to read TARGETS property\n");
1702 wine_tsx11_unlock();
1703 TRACE("Type %lx,Format %d,nItems %ld, Remain %ld\n",
1704 atype, aformat, cSelectionTargets, remain);
1706 * The TARGETS property should have returned us a list of atoms
1707 * corresponding to each selection target format supported.
1709 if ((atype == XA_ATOM || atype == x11drv_atom(TARGETS)) && aformat == 32)
1710 X11DRV_CLIPBOARD_InsertSelectionProperties(display, targetList, cSelectionTargets);
1712 /* Free the list of targets */
1715 wine_tsx11_unlock();
1718 return cSelectionTargets;
1722 /**************************************************************************
1723 * X11DRV_CLIPBOARD_ReadClipboardData
1725 * This method is invoked only when we DO NOT own the X selection
1727 * We always get the data from the selection client each time,
1728 * since we have no way of determining if the data in our cache is stale.
1730 static BOOL X11DRV_CLIPBOARD_ReadClipboardData(UINT wFormat)
1732 Display *display = thread_display();
1735 LPWINE_CLIPFORMAT lpFormat;
1737 TRACE("%d\n", wFormat);
1739 if (!selectionAcquired)
1741 Window w = thread_selection_wnd();
1744 ERR("No window available to read selection data!\n");
1748 lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
1750 if (lpFormat && lpFormat->drvData)
1756 TRACE("Requesting %s selection (%d) from win(%08x)\n",
1757 debugstr_w(lpFormat->Name), lpFormat->drvData, (UINT)selectionCacheSrc);
1760 XConvertSelection(display, selectionCacheSrc, lpFormat->drvData,
1761 x11drv_atom(SELECTION_DATA), w, CurrentTime);
1762 wine_tsx11_unlock();
1764 /* wait until SelectionNotify is received */
1765 for (i = 0; i < SELECTION_RETRIES; i++)
1768 res = XCheckTypedWindowEvent(display, w, SelectionNotify, &xe);
1769 wine_tsx11_unlock();
1770 if (res && xe.xselection.selection == selectionCacheSrc) break;
1772 usleep(SELECTION_WAIT);
1775 /* If the property wasn't available check for aliases */
1776 if (xe.xselection.property == None &&
1777 (alias = X11DRV_CLIPBOARD_LookupPropertyAlias(lpFormat->drvData)))
1780 XConvertSelection(display, selectionCacheSrc, alias,
1781 x11drv_atom(SELECTION_DATA), w, CurrentTime);
1782 wine_tsx11_unlock();
1784 /* wait until SelectionNotify is received */
1785 for (i = 0; i < SELECTION_RETRIES; i++)
1788 res = XCheckTypedWindowEvent(display, w, SelectionNotify, &xe);
1789 wine_tsx11_unlock();
1790 if (res && xe.xselection.selection == selectionCacheSrc) break;
1792 usleep(SELECTION_WAIT);
1796 /* Verify that the selection returned a valid TARGETS property */
1797 if (xe.xselection.property != None)
1800 * Read the contents of the X selection property
1801 * into WINE's clipboard cache converting the
1802 * selection to be compatible if possible.
1804 bRet = X11DRV_CLIPBOARD_ReadSelection(lpFormat, xe.xselection.requestor,
1805 xe.xselection.property);
1811 ERR("Received request to cache selection data but process is owner\n");
1814 TRACE("Returning %d\n", bRet);
1820 /**************************************************************************
1821 * X11DRV_CLIPBOARD_ReadSelection
1822 * Reads the contents of the X selection property into the WINE clipboard cache
1823 * converting the selection into a format compatible with the windows clipboard
1825 * This method is invoked only to read the contents of a the selection owned
1826 * by an external application. i.e. when we do not own the X selection.
1828 static BOOL X11DRV_CLIPBOARD_ReadSelection(LPWINE_CLIPFORMAT lpData, Window w, Atom prop)
1830 Display *display = thread_display();
1831 Atom atype=AnyPropertyType;
1833 unsigned long total,nitems,remain,val_cnt;
1836 unsigned char* buffer;
1842 TRACE("Reading X selection type %s\n", debugstr_w(lpData->Name));
1845 * First request a zero length in order to figure out the request size.
1848 if(XGetWindowProperty(display,w,prop,0,0,False, AnyPropertyType,
1849 &atype, &aformat, &nitems, &remain, &buffer) != Success)
1851 wine_tsx11_unlock();
1852 WARN("Failed to get property size\n");
1856 /* Free zero length return data if any */
1864 reqlen = remain * bwc;
1866 TRACE("Retrieving %ld bytes\n", reqlen);
1868 val = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, reqlen);
1870 /* Read property in 4K blocks */
1871 for (total = 0, val_cnt = 0; remain;)
1873 if (XGetWindowProperty(display, w, prop, (total / 4), 4096, False,
1874 AnyPropertyType, &atype, &aformat, &nitems, &remain, &buffer) != Success)
1876 wine_tsx11_unlock();
1877 WARN("Failed to read property\n");
1878 HeapFree(GetProcessHeap(), 0, val);
1883 memcpy(&val[val_cnt], buffer, nitems * bwc);
1884 val_cnt += nitems * bwc;
1885 total += nitems*bwc;
1888 wine_tsx11_unlock();
1890 bRet = X11DRV_CLIPBOARD_InsertClipboardData(lpData->wFormatID, 0, lpData->lpDrvImportFunc(val, total), 0);
1892 /* Delete the property on the window now that we are done
1893 * This will send a PropertyNotify event to the selection owner. */
1895 XDeleteProperty(display,w,prop);
1896 wine_tsx11_unlock();
1898 /* Free the retrieved property data */
1899 HeapFree(GetProcessHeap(),0,val);
1905 /**************************************************************************
1906 * CLIPBOARD_SerializeMetafile
1908 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out)
1912 TRACE(" wFormat=%d hdata=%08x out=%d\n", wformat, (unsigned int) hdata, out);
1914 if (out) /* Serialize out, caller should free memory */
1916 *lpcbytes = 0; /* Assume failure */
1918 if (wformat == CF_METAFILEPICT)
1920 LPMETAFILEPICT lpmfp = (LPMETAFILEPICT) GlobalLock(hdata);
1921 unsigned int size = GetMetaFileBitsEx(lpmfp->hMF, 0, NULL);
1923 h = GlobalAlloc(0, size + sizeof(METAFILEPICT));
1926 char *pdata = GlobalLock(h);
1928 memcpy(pdata, lpmfp, sizeof(METAFILEPICT));
1929 GetMetaFileBitsEx(lpmfp->hMF, size, pdata + sizeof(METAFILEPICT));
1931 *lpcbytes = size + sizeof(METAFILEPICT);
1936 GlobalUnlock(hdata);
1938 else if (wformat == CF_ENHMETAFILE)
1940 int size = GetEnhMetaFileBits(hdata, 0, NULL);
1942 h = GlobalAlloc(0, size);
1945 LPVOID pdata = GlobalLock(h);
1947 GetEnhMetaFileBits(hdata, size, pdata);
1956 if (wformat == CF_METAFILEPICT)
1958 h = GlobalAlloc(0, sizeof(METAFILEPICT));
1961 unsigned int wiresize, size;
1962 LPMETAFILEPICT lpmfp = (LPMETAFILEPICT) GlobalLock(h);
1964 memcpy(lpmfp, (LPVOID)hdata, sizeof(METAFILEPICT));
1965 wiresize = *lpcbytes - sizeof(METAFILEPICT);
1966 lpmfp->hMF = SetMetaFileBitsEx(wiresize,
1967 ((char *)hdata) + sizeof(METAFILEPICT));
1968 size = GetMetaFileBitsEx(lpmfp->hMF, 0, NULL);
1972 else if (wformat == CF_ENHMETAFILE)
1974 h = SetEnhMetaFileBits(*lpcbytes, (LPVOID)hdata);
1982 /**************************************************************************
1983 * X11DRV_CLIPBOARD_ReleaseSelection
1985 * Release XA_CLIPBOARD and XA_PRIMARY in response to a SelectionClear event.
1987 void X11DRV_CLIPBOARD_ReleaseSelection(Atom selType, Window w, HWND hwnd, Time time)
1989 Display *display = thread_display();
1991 /* w is the window that lost the selection
1993 TRACE("event->window = %08x (selectionWindow = %08x) selectionAcquired=0x%08x\n",
1994 (unsigned)w, (unsigned)selectionWindow, (unsigned)selectionAcquired);
1996 if (selectionAcquired && (w == selectionWindow))
1998 CLIPBOARDINFO cbinfo;
2000 /* completely give up the selection */
2001 TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
2003 X11DRV_CLIPBOARD_GetClipboardInfo(&cbinfo);
2005 if (cbinfo.flags & CB_PROCESS)
2007 /* Since we're still the owner, this wasn't initiated by
2008 another Wine process */
2009 if (OpenClipboard(hwnd))
2011 /* Destroy private objects */
2012 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
2014 /* Give up ownership of the windows clipboard */
2015 X11DRV_CLIPBOARD_ReleaseOwnership();
2020 if ((selType == x11drv_atom(CLIPBOARD)) && (selectionAcquired & S_PRIMARY))
2022 TRACE("Lost clipboard. Check if we need to release PRIMARY\n");
2025 if (selectionWindow == XGetSelectionOwner(display, XA_PRIMARY))
2027 TRACE("We still own PRIMARY. Releasing PRIMARY.\n");
2028 XSetSelectionOwner(display, XA_PRIMARY, None, time);
2031 TRACE("We no longer own PRIMARY\n");
2032 wine_tsx11_unlock();
2034 else if ((selType == XA_PRIMARY) && (selectionAcquired & S_CLIPBOARD))
2036 TRACE("Lost PRIMARY. Check if we need to release CLIPBOARD\n");
2039 if (selectionWindow == XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
2041 TRACE("We still own CLIPBOARD. Releasing CLIPBOARD.\n");
2042 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), None, time);
2045 TRACE("We no longer own CLIPBOARD\n");
2046 wine_tsx11_unlock();
2049 selectionWindow = None;
2051 X11DRV_EmptyClipboard(FALSE);
2053 /* Reset the selection flags now that we are done */
2054 selectionAcquired = S_NOSELECTION;
2059 /**************************************************************************
2060 * IsSelectionOwner (X11DRV.@)
2062 * Returns: TRUE if the selection is owned by this process, FALSE otherwise
2064 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void)
2066 return selectionAcquired;
2070 /**************************************************************************
2071 * X11DRV Clipboard Exports
2072 **************************************************************************/
2075 /**************************************************************************
2076 * RegisterClipboardFormat (X11DRV.@)
2078 * Registers a custom X clipboard format
2079 * Returns: Format id or 0 on failure
2081 INT X11DRV_RegisterClipboardFormat(LPCWSTR FormatName)
2083 LPWINE_CLIPFORMAT lpFormat;
2085 if (FormatName == NULL) return 0;
2086 if (!(lpFormat = register_format( FormatName, 0 ))) return 0;
2087 return lpFormat->wFormatID;
2091 /**************************************************************************
2092 * X11DRV_GetClipboardFormatName
2094 INT X11DRV_GetClipboardFormatName(UINT wFormat, LPWSTR retStr, INT maxlen)
2096 LPWINE_CLIPFORMAT lpFormat;
2098 TRACE("(%04X, %p, %d) !\n", wFormat, retStr, maxlen);
2100 if (wFormat < 0xc000)
2102 SetLastError(ERROR_INVALID_PARAMETER);
2106 lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
2108 if (!lpFormat || (lpFormat->wFlags & CF_FLAG_BUILTINFMT))
2110 TRACE("Unknown format 0x%08x!\n", wFormat);
2111 SetLastError(ERROR_INVALID_HANDLE);
2115 lstrcpynW(retStr, lpFormat->Name, maxlen);
2117 return strlenW(retStr);
2121 /**************************************************************************
2122 * AcquireClipboard (X11DRV.@)
2124 void X11DRV_AcquireClipboard(HWND hWndClipWindow)
2126 Display *display = thread_display();
2129 * Acquire X selection if we don't already own it.
2130 * Note that we only acquire the selection if it hasn't been already
2131 * acquired by us, and ignore the fact that another X window may be
2132 * asserting ownership. The reason for this is we need *any* top level
2133 * X window to hold selection ownership. The actual clipboard data requests
2134 * are made via GetClipboardData from EVENT_SelectionRequest and this
2135 * ensures that the real HWND owner services the request.
2136 * If the owning X window gets destroyed the selection ownership is
2137 * re-cycled to another top level X window in X11DRV_CLIPBOARD_ResetOwner.
2140 if (!(selectionAcquired == (S_PRIMARY | S_CLIPBOARD)))
2144 if (!hWndClipWindow)
2145 hWndClipWindow = GetActiveWindow();
2147 hWndClipWindow = GetAncestor(hWndClipWindow, GA_ROOT);
2149 if (GetCurrentThreadId() != GetWindowThreadProcessId(hWndClipWindow, NULL))
2151 TRACE("Thread %lx is acquiring selection with thread %lx's window %p\n",
2152 GetCurrentThreadId(),
2153 GetWindowThreadProcessId(hWndClipWindow, NULL),
2155 if (!SendMessageW(hWndClipWindow, WM_X11DRV_ACQUIRE_SELECTION, 0, 0))
2156 ERR("Failed to acquire selection\n");
2160 owner = X11DRV_get_whole_window(hWndClipWindow);
2163 /* Grab PRIMARY selection if not owned */
2164 if (usePrimary && !(selectionAcquired & S_PRIMARY))
2165 XSetSelectionOwner(display, XA_PRIMARY, owner, CurrentTime);
2167 /* Grab CLIPBOARD selection if not owned */
2168 if (!(selectionAcquired & S_CLIPBOARD))
2169 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), owner, CurrentTime);
2171 if (usePrimary && XGetSelectionOwner(display,XA_PRIMARY) == owner)
2172 selectionAcquired |= S_PRIMARY;
2174 if (XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)) == owner)
2175 selectionAcquired |= S_CLIPBOARD;
2176 wine_tsx11_unlock();
2178 if (selectionAcquired)
2180 selectionWindow = owner;
2181 TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner);
2186 ERR("Received request to acquire selection but process is already owner=(%08x)\n", (unsigned) selectionWindow);
2191 /**************************************************************************
2192 * X11DRV_EmptyClipboard
2194 * Empty cached clipboard data.
2196 void X11DRV_EmptyClipboard(BOOL keepunowned)
2200 LPWINE_CLIPDATA lpData, lpStart;
2201 LPWINE_CLIPDATA lpNext = ClipData;
2203 TRACE(" called with %d entries in cache.\n", ClipDataCount);
2209 lpNext = lpData->NextData;
2211 if (!keepunowned || !(lpData->wFlags & CF_FLAG_UNOWNED))
2213 lpData->PrevData->NextData = lpData->NextData;
2214 lpData->NextData->PrevData = lpData->PrevData;
2216 if (lpData == ClipData)
2217 ClipData = lpNext != lpData ? lpNext : NULL;
2219 X11DRV_CLIPBOARD_FreeData(lpData);
2220 HeapFree(GetProcessHeap(), 0, lpData);
2224 } while (lpNext != lpStart);
2227 TRACE(" %d entries remaining in cache.\n", ClipDataCount);
2232 /**************************************************************************
2233 * X11DRV_SetClipboardData
2235 BOOL X11DRV_SetClipboardData(UINT wFormat, HANDLE16 hData16, HANDLE hData32, BOOL owner)
2238 BOOL bResult = TRUE;
2240 /* If it's not owned, data can only be set if the format data is not already owned
2241 and its rendering is not delayed */
2244 CLIPBOARDINFO cbinfo;
2245 LPWINE_CLIPDATA lpRender;
2247 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2249 if ((!hData16 && !hData32) ||
2250 ((lpRender = X11DRV_CLIPBOARD_LookupData(wFormat)) &&
2251 !(lpRender->wFlags & CF_FLAG_UNOWNED)))
2254 flags = CF_FLAG_UNOWNED;
2257 bResult &= X11DRV_CLIPBOARD_InsertClipboardData(wFormat, hData16, hData32, flags);
2263 /**************************************************************************
2264 * CountClipboardFormats
2266 INT X11DRV_CountClipboardFormats(void)
2268 CLIPBOARDINFO cbinfo;
2270 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2272 TRACE(" count=%d\n", ClipDataCount);
2274 return ClipDataCount;
2278 /**************************************************************************
2279 * X11DRV_EnumClipboardFormats
2281 UINT X11DRV_EnumClipboardFormats(UINT wFormat)
2283 CLIPBOARDINFO cbinfo;
2284 UINT wNextFormat = 0;
2286 TRACE("(%04X)\n", wFormat);
2288 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2293 wNextFormat = ClipData->wFormatID;
2297 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormat);
2299 if (lpData && lpData->NextData != ClipData)
2300 wNextFormat = lpData->NextData->wFormatID;
2307 /**************************************************************************
2308 * X11DRV_IsClipboardFormatAvailable
2310 BOOL X11DRV_IsClipboardFormatAvailable(UINT wFormat)
2313 CLIPBOARDINFO cbinfo;
2315 TRACE("(%04X)\n", wFormat);
2317 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2319 if (wFormat != 0 && X11DRV_CLIPBOARD_LookupData(wFormat))
2322 TRACE("(%04X)- ret(%d)\n", wFormat, bRet);
2328 /**************************************************************************
2329 * GetClipboardData (USER.142)
2331 BOOL X11DRV_GetClipboardData(UINT wFormat, HANDLE16* phData16, HANDLE* phData32)
2333 CLIPBOARDINFO cbinfo;
2334 LPWINE_CLIPDATA lpRender;
2336 TRACE("(%04X)\n", wFormat);
2338 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2340 if ((lpRender = X11DRV_CLIPBOARD_LookupData(wFormat)))
2342 if ( !lpRender->hData32 )
2343 X11DRV_CLIPBOARD_RenderFormat(lpRender);
2345 /* Convert between 32 -> 16 bit data, if necessary */
2346 if (lpRender->hData32 && !lpRender->hData16)
2350 if (lpRender->wFormatID == CF_METAFILEPICT)
2351 size = sizeof(METAFILEPICT16);
2353 size = GlobalSize(lpRender->hData32);
2355 lpRender->hData16 = GlobalAlloc16(GMEM_ZEROINIT, size);
2357 if (!lpRender->hData16)
2358 ERR("(%04X) -- not enough memory in 16b heap\n", wFormat);
2361 if (lpRender->wFormatID == CF_METAFILEPICT)
2363 FIXME("\timplement function CopyMetaFilePict32to16\n");
2364 FIXME("\tin the appropriate file.\n");
2365 #ifdef SOMEONE_IMPLEMENTED_ME
2366 CopyMetaFilePict32to16(GlobalLock16(lpRender->hData16),
2367 GlobalLock(lpRender->hData32));
2372 memcpy(GlobalLock16(lpRender->hData16),
2373 GlobalLock(lpRender->hData32), size);
2376 GlobalUnlock16(lpRender->hData16);
2377 GlobalUnlock(lpRender->hData32);
2381 /* Convert between 32 -> 16 bit data, if necessary */
2382 if (lpRender->hData16 && !lpRender->hData32)
2386 if (lpRender->wFormatID == CF_METAFILEPICT)
2387 size = sizeof(METAFILEPICT16);
2389 size = GlobalSize(lpRender->hData32);
2391 lpRender->hData32 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE |
2392 GMEM_DDESHARE, size);
2394 if (lpRender->wFormatID == CF_METAFILEPICT)
2396 FIXME("\timplement function CopyMetaFilePict16to32\n");
2397 FIXME("\tin the appropriate file.\n");
2398 #ifdef SOMEONE_IMPLEMENTED_ME
2399 CopyMetaFilePict16to32(GlobalLock16(lpRender->hData32),
2400 GlobalLock(lpRender->hData16));
2405 memcpy(GlobalLock(lpRender->hData32),
2406 GlobalLock16(lpRender->hData16), size);
2409 GlobalUnlock(lpRender->hData32);
2410 GlobalUnlock16(lpRender->hData16);
2414 *phData16 = lpRender->hData16;
2417 *phData32 = lpRender->hData32;
2419 TRACE(" returning hData16(%04x) hData32(%04x) (type %d)\n",
2420 lpRender->hData16, (unsigned int) lpRender->hData32, lpRender->wFormatID);
2422 return lpRender->hData16 || lpRender->hData32;
2429 /**************************************************************************
2430 * ResetSelectionOwner (X11DRV.@)
2432 * Called from DestroyWindow() to prevent X selection from being lost when
2433 * a top level window is destroyed, by switching ownership to another top
2435 * Any top level window can own the selection. See X11DRV_CLIPBOARD_Acquire
2436 * for a more detailed description of this.
2438 void X11DRV_ResetSelectionOwner(HWND hwnd, BOOL bFooBar)
2440 Display *display = thread_display();
2441 HWND hWndClipOwner = 0;
2443 Window XWnd = X11DRV_get_whole_window(hwnd);
2444 BOOL bLostSelection = FALSE;
2445 Window selectionPrevWindow;
2447 /* There is nothing to do if we don't own the selection,
2448 * or if the X window which currently owns the selection is different
2449 * from the one passed in.
2451 if (!selectionAcquired || XWnd != selectionWindow
2452 || selectionWindow == None )
2455 if ((bFooBar && XWnd) || (!bFooBar && !XWnd))
2458 hWndClipOwner = GetClipboardOwner();
2460 TRACE("clipboard owner = %p, selection window = %08x\n",
2461 hWndClipOwner, (unsigned)selectionWindow);
2463 /* now try to salvage current selection from being destroyed by X */
2464 TRACE("checking %08x\n", (unsigned) XWnd);
2466 selectionPrevWindow = selectionWindow;
2467 selectionWindow = None;
2469 if (!(tmp = GetWindow(hwnd, GW_HWNDNEXT)))
2470 tmp = GetWindow(hwnd, GW_HWNDFIRST);
2472 if (tmp && tmp != hwnd)
2473 selectionWindow = X11DRV_get_whole_window(tmp);
2475 if (selectionWindow != None)
2477 /* We must pretend that we don't own the selection while making the switch
2478 * since a SelectionClear event will be sent to the last owner.
2479 * If there is no owner X11DRV_CLIPBOARD_ReleaseSelection will do nothing.
2481 int saveSelectionState = selectionAcquired;
2482 selectionAcquired = S_NOSELECTION;
2484 TRACE("\tswitching selection from %08x to %08x\n",
2485 (unsigned)selectionPrevWindow, (unsigned)selectionWindow);
2489 /* Assume ownership for the PRIMARY and CLIPBOARD selection */
2490 if (saveSelectionState & S_PRIMARY)
2491 XSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime);
2493 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), selectionWindow, CurrentTime);
2495 /* Restore the selection masks */
2496 selectionAcquired = saveSelectionState;
2498 /* Lose the selection if something went wrong */
2499 if (((saveSelectionState & S_PRIMARY) &&
2500 (XGetSelectionOwner(display, XA_PRIMARY) != selectionWindow)) ||
2501 (XGetSelectionOwner(display, x11drv_atom(CLIPBOARD)) != selectionWindow))
2503 bLostSelection = TRUE;
2505 wine_tsx11_unlock();
2509 bLostSelection = TRUE;
2514 TRACE("Lost the selection!\n");
2516 X11DRV_CLIPBOARD_ReleaseOwnership();
2517 selectionAcquired = S_NOSELECTION;
2518 selectionWindow = 0;
2523 /**************************************************************************
2524 * X11DRV_CLIPBOARD_SynthesizeData
2526 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID)
2529 LPWINE_CLIPDATA lpSource = NULL;
2531 TRACE(" %d\n", wFormatID);
2533 /* Don't need to synthesize if it already exists */
2534 if (X11DRV_CLIPBOARD_LookupData(wFormatID))
2537 if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
2539 bsyn = ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
2540 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2541 ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
2542 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2543 ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
2544 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED);
2546 else if (wFormatID == CF_ENHMETAFILE)
2548 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT)) &&
2549 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2551 else if (wFormatID == CF_METAFILEPICT)
2553 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT)) &&
2554 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2556 else if (wFormatID == CF_DIB)
2558 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) &&
2559 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2561 else if (wFormatID == CF_BITMAP)
2563 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) &&
2564 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2568 X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, 0, 0, CF_FLAG_SYNTHESIZED);
2575 /**************************************************************************
2576 * X11DRV_EndClipboardUpdate
2578 * Add locale if it hasn't already been added
2580 void X11DRV_EndClipboardUpdate(void)
2582 INT count = ClipDataCount;
2584 /* Do Unicode <-> Text <-> OEM mapping */
2585 X11DRV_CLIPBOARD_SynthesizeData(CF_UNICODETEXT);
2586 X11DRV_CLIPBOARD_SynthesizeData(CF_TEXT);
2587 X11DRV_CLIPBOARD_SynthesizeData(CF_OEMTEXT);
2589 /* Enhmetafile <-> MetafilePict mapping */
2590 X11DRV_CLIPBOARD_SynthesizeData(CF_ENHMETAFILE);
2591 X11DRV_CLIPBOARD_SynthesizeData(CF_METAFILEPICT);
2593 /* DIB <-> Bitmap mapping */
2594 X11DRV_CLIPBOARD_SynthesizeData(CF_DIB);
2595 X11DRV_CLIPBOARD_SynthesizeData(CF_BITMAP);
2597 TRACE("%d formats added to cached data\n", ClipDataCount - count);
2601 /***********************************************************************
2604 * Utility function for X11DRV_SelectionRequest_TARGETS.
2606 static BOOL add_targets(Atom* targets, unsigned long cTargets, Atom prop)
2611 /* Scan through what we have so far to avoid duplicates */
2612 for (i = 0, bExists = FALSE; i < cTargets; i++)
2614 if (targets[i] == prop)
2622 targets[cTargets] = prop;
2628 /***********************************************************************
2629 * X11DRV_SelectionRequest_TARGETS
2630 * Service a TARGETS selection request event
2632 static Atom X11DRV_SelectionRequest_TARGETS( Display *display, Window requestor,
2633 Atom target, Atom rprop )
2639 LPWINE_CLIPFORMAT lpFormat;
2642 * Count the number of items we wish to expose as selection targets.
2643 * We include the TARGETS item, and property aliases
2645 cTargets = X11DRV_CountClipboardFormats() + 1;
2647 for (wFormat = 0; (wFormat = X11DRV_EnumClipboardFormats(wFormat));)
2649 lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
2653 if (!lpFormat->lpDrvExportFunc)
2656 if (X11DRV_CLIPBOARD_LookupPropertyAlias(lpFormat->drvData))
2659 /* else most likely unregistered format such as CF_PRIVATE or CF_GDIOBJ */
2662 TRACE(" found %ld formats\n", cTargets);
2664 /* Allocate temp buffer */
2665 targets = HeapAlloc( GetProcessHeap(), 0, cTargets * sizeof(Atom));
2669 /* Create TARGETS property list (First item in list is TARGETS itself) */
2670 for (targets[0] = x11drv_atom(TARGETS), cTargets = 1, wFormat = 0;
2671 (wFormat = X11DRV_EnumClipboardFormats(wFormat));)
2673 lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
2677 if (lpFormat->lpDrvExportFunc)
2679 if (add_targets(targets, cTargets, lpFormat->drvData))
2683 /* Check if any alias should be listed */
2684 alias = X11DRV_CLIPBOARD_LookupPropertyAlias(lpFormat->drvData);
2687 if (add_targets(targets, cTargets, alias))
2695 if (TRACE_ON(clipboard))
2698 for ( i = 0; i < cTargets; i++)
2702 char *itemFmtName = XGetAtomName(display, targets[i]);
2703 TRACE("\tAtom# %d: Property %ld Type %s\n", i, targets[i], itemFmtName);
2709 /* We may want to consider setting the type to xaTargets instead,
2710 * in case some apps expect this instead of XA_ATOM */
2711 XChangeProperty(display, requestor, rprop, XA_ATOM, 32,
2712 PropModeReplace, (unsigned char *)targets, cTargets);
2713 wine_tsx11_unlock();
2715 HeapFree(GetProcessHeap(), 0, targets);
2721 /***********************************************************************
2722 * X11DRV_SelectionRequest_MULTIPLE
2723 * Service a MULTIPLE selection request event
2724 * rprop contains a list of (target,property) atom pairs.
2725 * The first atom names a target and the second names a property.
2726 * The effect is as if we have received a sequence of SelectionRequest events
2727 * (one for each atom pair) except that:
2728 * 1. We reply with a SelectionNotify only when all the requested conversions
2729 * have been performed.
2730 * 2. If we fail to convert the target named by an atom in the MULTIPLE property,
2731 * we replace the atom in the property by None.
2733 static Atom X11DRV_SelectionRequest_MULTIPLE( HWND hWnd, XSelectionRequestEvent *pevent )
2735 Display *display = pevent->display;
2737 Atom atype=AnyPropertyType;
2739 unsigned long remain;
2740 Atom* targetPropList=NULL;
2741 unsigned long cTargetPropList = 0;
2743 /* If the specified property is None the requestor is an obsolete client.
2744 * We support these by using the specified target atom as the reply property.
2746 rprop = pevent->property;
2748 rprop = pevent->target;
2752 /* Read the MULTIPLE property contents. This should contain a list of
2753 * (target,property) atom pairs.
2756 if(XGetWindowProperty(display, pevent->requestor, rprop,
2757 0, 0x3FFF, False, AnyPropertyType, &atype,&aformat,
2758 &cTargetPropList, &remain,
2759 (unsigned char**)&targetPropList) != Success)
2761 wine_tsx11_unlock();
2762 TRACE("\tCouldn't read MULTIPLE property\n");
2766 TRACE("\tType %s,Format %d,nItems %ld, Remain %ld\n",
2767 XGetAtomName(display, atype), aformat, cTargetPropList, remain);
2768 wine_tsx11_unlock();
2771 * Make sure we got what we expect.
2772 * NOTE: According to the X-ICCCM Version 2.0 documentation the property sent
2773 * in a MULTIPLE selection request should be of type ATOM_PAIR.
2774 * However some X apps(such as XPaint) are not compliant with this and return
2775 * a user defined atom in atype when XGetWindowProperty is called.
2776 * The data *is* an atom pair but is not denoted as such.
2778 if(aformat == 32 /* atype == xAtomPair */ )
2782 /* Iterate through the ATOM_PAIR list and execute a SelectionRequest
2783 * for each (target,property) pair */
2785 for (i = 0; i < cTargetPropList; i+=2)
2787 XSelectionRequestEvent event;
2789 if (TRACE_ON(clipboard))
2791 char *targetName, *propName;
2793 targetName = XGetAtomName(display, targetPropList[i]);
2794 propName = XGetAtomName(display, targetPropList[i+1]);
2795 TRACE("MULTIPLE(%d): Target='%s' Prop='%s'\n",
2796 i/2, targetName, propName);
2799 wine_tsx11_unlock();
2802 /* We must have a non "None" property to service a MULTIPLE target atom */
2803 if ( !targetPropList[i+1] )
2805 TRACE("\tMULTIPLE(%d): Skipping target with empty property!\n", i);
2809 /* Set up an XSelectionRequestEvent for this (target,property) pair */
2810 memcpy( &event, pevent, sizeof(XSelectionRequestEvent) );
2811 event.target = targetPropList[i];
2812 event.property = targetPropList[i+1];
2814 /* Fire a SelectionRequest, informing the handler that we are processing
2815 * a MULTIPLE selection request event.
2817 X11DRV_HandleSelectionRequest( hWnd, &event, TRUE );
2821 /* Free the list of targets/properties */
2823 XFree(targetPropList);
2824 wine_tsx11_unlock();
2831 /***********************************************************************
2832 * X11DRV_HandleSelectionRequest
2833 * Process an event selection request event.
2834 * The bIsMultiple flag is used to signal when EVENT_SelectionRequest is called
2835 * recursively while servicing a "MULTIPLE" selection target.
2837 * Note: We only receive this event when WINE owns the X selection
2839 static void X11DRV_HandleSelectionRequest( HWND hWnd, XSelectionRequestEvent *event, BOOL bIsMultiple )
2841 Display *display = event->display;
2842 XSelectionEvent result;
2844 Window request = event->requestor;
2849 * We can only handle the selection request if :
2850 * The selection is PRIMARY or CLIPBOARD, AND we can successfully open the clipboard.
2851 * Don't do these checks or open the clipboard while recursively processing MULTIPLE,
2852 * since this has been already done.
2856 if (((event->selection != XA_PRIMARY) && (event->selection != x11drv_atom(CLIPBOARD))))
2860 /* If the specified property is None the requestor is an obsolete client.
2861 * We support these by using the specified target atom as the reply property.
2863 rprop = event->property;
2865 rprop = event->target;
2867 if(event->target == x11drv_atom(TARGETS)) /* Return a list of all supported targets */
2869 /* TARGETS selection request */
2870 rprop = X11DRV_SelectionRequest_TARGETS( display, request, event->target, rprop );
2872 else if(event->target == x11drv_atom(MULTIPLE)) /* rprop contains a list of (target, property) atom pairs */
2874 /* MULTIPLE selection request */
2875 rprop = X11DRV_SelectionRequest_MULTIPLE( hWnd, event );
2879 LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupProperty(event->target);
2882 lpFormat = X11DRV_CLIPBOARD_LookupAliasProperty(event->target);
2884 if (lpFormat && lpFormat->lpDrvExportFunc)
2886 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(lpFormat->wFormatID);
2890 unsigned char* lpClipData;
2892 HANDLE hClipData = lpFormat->lpDrvExportFunc(request, event->target,
2893 rprop, lpData, &cBytes);
2895 if (hClipData && (lpClipData = GlobalLock(hClipData)))
2897 TRACE("\tUpdating property %s, %ld bytes\n", debugstr_w(lpFormat->Name), cBytes);
2900 XChangeProperty(display, request, rprop, event->target,
2901 8, PropModeReplace, (unsigned char *)lpClipData, cBytes);
2902 wine_tsx11_unlock();
2904 GlobalUnlock(hClipData);
2905 GlobalFree(hClipData);
2913 * SelectionNotify should be sent only at the end of a MULTIPLE request
2917 result.type = SelectionNotify;
2918 result.display = display;
2919 result.requestor = request;
2920 result.selection = event->selection;
2921 result.property = rprop;
2922 result.target = event->target;
2923 result.time = event->time;
2924 TRACE("Sending SelectionNotify event...\n");
2926 XSendEvent(display,event->requestor,False,NoEventMask,(XEvent*)&result);
2927 wine_tsx11_unlock();
2932 /***********************************************************************
2933 * X11DRV_SelectionRequest
2935 void X11DRV_SelectionRequest( HWND hWnd, XEvent *event )
2938 X11DRV_HandleSelectionRequest( hWnd, &event->xselectionrequest, FALSE );
2942 /***********************************************************************
2943 * X11DRV_SelectionClear
2945 void X11DRV_SelectionClear( HWND hWnd, XEvent *xev )
2947 XSelectionClearEvent *event = &xev->xselectionclear;
2949 if (event->selection == XA_PRIMARY || event->selection == x11drv_atom(CLIPBOARD))
2950 X11DRV_CLIPBOARD_ReleaseSelection( event->selection, event->window, hWnd, event->time );