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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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"
81 #include "wine/wingdi16.h"
83 #include "wine/debug.h"
84 #include "wine/unicode.h"
85 #include "wine/server.h"
87 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
89 #define HGDIOBJ_32(handle16) ((HGDIOBJ)(ULONG_PTR)(handle16))
91 /* Maximum wait time for selection notify */
92 #define SELECTION_RETRIES 500 /* wait for .1 seconds */
93 #define SELECTION_WAIT 1000 /* us */
94 /* Minimum seconds that must lapse between owner queries */
95 #define OWNERQUERYLAPSETIME 1
98 #define S_NOSELECTION 0
100 #define S_CLIPBOARD 2
109 } CLIPBOARDINFO, *LPCLIPBOARDINFO;
111 struct tagWINE_CLIPDATA; /* Forward */
113 typedef HANDLE (*DRVEXPORTFUNC)(Display *display, Window requestor, Atom aTarget, Atom rprop,
114 struct tagWINE_CLIPDATA* lpData, LPDWORD lpBytes);
115 typedef HANDLE (*DRVIMPORTFUNC)(Display *d, Window w, Atom prop);
117 typedef struct tagWINE_CLIPFORMAT {
122 DRVIMPORTFUNC lpDrvImportFunc;
123 DRVEXPORTFUNC lpDrvExportFunc;
124 struct tagWINE_CLIPFORMAT *PrevFormat;
125 struct tagWINE_CLIPFORMAT *NextFormat;
126 } WINE_CLIPFORMAT, *LPWINE_CLIPFORMAT;
128 typedef struct tagWINE_CLIPDATA {
134 LPWINE_CLIPFORMAT lpFormat;
135 struct tagWINE_CLIPDATA *PrevData;
136 struct tagWINE_CLIPDATA *NextData;
137 } WINE_CLIPDATA, *LPWINE_CLIPDATA;
139 #define CF_FLAG_BUILTINFMT 0x0001 /* Built-in windows format */
140 #define CF_FLAG_UNOWNED 0x0002 /* cached data is not owned */
141 #define CF_FLAG_SYNTHESIZED 0x0004 /* Implicitly converted data */
142 #define CF_FLAG_UNICODE 0x0008 /* Data is in unicode */
144 static int selectionAcquired = 0; /* Contains the current selection masks */
145 static Window selectionWindow = None; /* The top level X window which owns the selection */
146 static Atom selectionCacheSrc = XA_PRIMARY; /* The selection source from which the clipboard cache was filled */
148 void CDECL X11DRV_EmptyClipboard(BOOL keepunowned);
149 void CDECL X11DRV_EndClipboardUpdate(void);
150 static HANDLE X11DRV_CLIPBOARD_ImportClipboardData(Display *d, Window w, Atom prop);
151 static HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(Display *d, Window w, Atom prop);
152 static HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(Display *d, Window w, Atom prop);
153 static HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(Display *d, Window w, Atom prop);
154 static HANDLE X11DRV_CLIPBOARD_ImportImageBmp(Display *d, Window w, Atom prop);
155 static HANDLE X11DRV_CLIPBOARD_ImportXAString(Display *d, Window w, Atom prop);
156 static HANDLE X11DRV_CLIPBOARD_ImportUTF8(Display *d, Window w, Atom prop);
157 static HANDLE X11DRV_CLIPBOARD_ImportCompoundText(Display *d, Window w, Atom prop);
158 static HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Display *display, Window requestor, Atom aTarget,
159 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
160 static HANDLE X11DRV_CLIPBOARD_ExportString(Display *display, Window requestor, Atom aTarget,
161 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
162 static HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Display *display, Window requestor, Atom aTarget,
163 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
164 static HANDLE X11DRV_CLIPBOARD_ExportImageBmp(Display *display, Window requestor, Atom aTarget,
165 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
166 static HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Display *display, Window requestor, Atom aTarget,
167 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
168 static HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Display *display, Window requestor, Atom aTarget,
169 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
170 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(LPCWSTR FormatName, Atom prop);
171 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(Display *display, UINT wFormatID);
172 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData);
173 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void);
174 static int X11DRV_CLIPBOARD_QueryAvailableData(Display *display, LPCLIPBOARDINFO lpcbinfo);
175 static BOOL X11DRV_CLIPBOARD_ReadSelectionData(Display *display, LPWINE_CLIPDATA lpData);
176 static BOOL X11DRV_CLIPBOARD_ReadProperty(Display *display, Window w, Atom prop,
177 unsigned char** data, unsigned long* datasize);
178 static BOOL X11DRV_CLIPBOARD_RenderFormat(Display *display, LPWINE_CLIPDATA lpData);
179 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out);
180 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID);
181 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(Display *display, LPWINE_CLIPDATA lpData);
182 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedDIB(Display *display);
183 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedBitmap(Display *display);
184 static void X11DRV_HandleSelectionRequest( HWND hWnd, XSelectionRequestEvent *event, BOOL bIsMultiple );
187 * WARNING: This data ordering is dependent on the WINE_CLIPFORMAT structure
188 * declared in clipboard.h
190 static const WCHAR wszCF_TEXT[] = {'W','C','F','_','T','E','X','T',0};
191 static const WCHAR wszCF_BITMAP[] = {'W','C','F','_','B','I','T','M','A','P',0};
192 static const WCHAR wszCF_METAFILEPICT[] = {'W','C','F','_','M','E','T','A','F','I','L','E','P','I','C','T',0};
193 static const WCHAR wszCF_SYLK[] = {'W','C','F','_','S','Y','L','K',0};
194 static const WCHAR wszCF_DIF[] = {'W','C','F','_','D','I','F',0};
195 static const WCHAR wszCF_TIFF[] = {'W','C','F','_','T','I','F','F',0};
196 static const WCHAR wszCF_OEMTEXT[] = {'W','C','F','_','O','E','M','T','E','X','T',0};
197 static const WCHAR wszCF_DIB[] = {'W','C','F','_','D','I','B',0};
198 static const WCHAR wszIMAGEBMP[] = {'i','m','a','g','e','/','b','m','p',0};
199 static const WCHAR wszCF_PALETTE[] = {'W','C','F','_','P','A','L','E','T','T','E',0};
200 static const WCHAR wszCF_PENDATA[] = {'W','C','F','_','P','E','N','D','A','T','A',0};
201 static const WCHAR wszCF_RIFF[] = {'W','C','F','_','R','I','F','F',0};
202 static const WCHAR wszCF_WAVE[] = {'W','C','F','_','W','A','V','E',0};
203 static const WCHAR wszCOMPOUNDTEXT[] = {'C','O','M','P','O','U','N','D','_','T','E','X','T',0};
204 static const WCHAR wszUTF8STRING[] = {'U','T','F','8','_','S','T','R','I','N','G',0};
205 static const WCHAR wszCF_ENHMETAFILE[] = {'W','C','F','_','E','N','H','M','E','T','A','F','I','L','E',0};
206 static const WCHAR wszCF_HDROP[] = {'W','C','F','_','H','D','R','O','P',0};
207 static const WCHAR wszCF_LOCALE[] = {'W','C','F','_','L','O','C','A','L','E',0};
208 static const WCHAR wszCF_DIBV5[] = {'W','C','F','_','D','I','B','V','5',0};
209 static const WCHAR wszCF_OWNERDISPLAY[] = {'W','C','F','_','O','W','N','E','R','D','I','S','P','L','A','Y',0};
210 static const WCHAR wszCF_DSPTEXT[] = {'W','C','F','_','D','S','P','T','E','X','T',0};
211 static const WCHAR wszCF_DSPBITMAP[] = {'W','C','F','_','D','S','P','B','I','T','M','A','P',0};
212 static const WCHAR wszCF_DSPMETAFILEPICT[] = {'W','C','F','_','D','S','P','M','E','T','A','F','I','L','E','P','I','C','T',0};
213 static const WCHAR wszCF_DSPENHMETAFILE[] = {'W','C','F','_','D','S','P','E','N','H','M','E','T','A','F','I','L','E',0};
215 static WINE_CLIPFORMAT ClipFormats[] =
217 { CF_TEXT, wszCF_TEXT, XA_STRING, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportXAString,
218 X11DRV_CLIPBOARD_ExportString, NULL, &ClipFormats[1]},
220 { CF_BITMAP, wszCF_BITMAP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
221 NULL, &ClipFormats[0], &ClipFormats[2]},
223 { CF_METAFILEPICT, wszCF_METAFILEPICT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportMetaFilePict,
224 X11DRV_CLIPBOARD_ExportMetaFilePict, &ClipFormats[1], &ClipFormats[3]},
226 { CF_SYLK, wszCF_SYLK, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
227 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[2], &ClipFormats[4]},
229 { CF_DIF, wszCF_DIF, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
230 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[3], &ClipFormats[5]},
232 { CF_TIFF, wszCF_TIFF, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
233 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[4], &ClipFormats[6]},
235 { CF_OEMTEXT, wszCF_OEMTEXT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
236 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[5], &ClipFormats[7]},
238 { CF_DIB, wszCF_DIB, XA_PIXMAP, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportXAPIXMAP,
239 X11DRV_CLIPBOARD_ExportXAPIXMAP, &ClipFormats[6], &ClipFormats[8]},
241 { CF_PALETTE, wszCF_PALETTE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
242 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[7], &ClipFormats[9]},
244 { CF_PENDATA, wszCF_PENDATA, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
245 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[8], &ClipFormats[10]},
247 { CF_RIFF, wszCF_RIFF, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
248 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[9], &ClipFormats[11]},
250 { CF_WAVE, wszCF_WAVE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
251 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[10], &ClipFormats[12]},
253 { CF_UNICODETEXT, wszUTF8STRING, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportUTF8,
254 X11DRV_CLIPBOARD_ExportString, &ClipFormats[11], &ClipFormats[13]},
256 /* If UTF8_STRING is not available, attempt COMPUND_TEXT */
257 { CF_UNICODETEXT, wszCOMPOUNDTEXT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportCompoundText,
258 X11DRV_CLIPBOARD_ExportString, &ClipFormats[12], &ClipFormats[14]},
260 { CF_ENHMETAFILE, wszCF_ENHMETAFILE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportEnhMetaFile,
261 X11DRV_CLIPBOARD_ExportEnhMetaFile, &ClipFormats[13], &ClipFormats[15]},
263 { CF_HDROP, wszCF_HDROP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
264 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[14], &ClipFormats[16]},
266 { CF_LOCALE, wszCF_LOCALE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
267 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[15], &ClipFormats[17]},
269 { CF_DIBV5, wszCF_DIBV5, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
270 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[16], &ClipFormats[18]},
272 { CF_OWNERDISPLAY, wszCF_OWNERDISPLAY, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
273 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[17], &ClipFormats[19]},
275 { CF_DSPTEXT, wszCF_DSPTEXT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
276 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[18], &ClipFormats[20]},
278 { CF_DSPBITMAP, wszCF_DSPBITMAP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
279 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[19], &ClipFormats[21]},
281 { CF_DSPMETAFILEPICT, wszCF_DSPMETAFILEPICT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
282 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[20], &ClipFormats[22]},
284 { CF_DSPENHMETAFILE, wszCF_DSPENHMETAFILE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
285 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[21], &ClipFormats[23]},
287 { CF_DIB, wszIMAGEBMP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportImageBmp,
288 X11DRV_CLIPBOARD_ExportImageBmp, &ClipFormats[22], NULL},
291 #define GET_ATOM(prop) (((prop) < FIRST_XATOM) ? (Atom)(prop) : X11DRV_Atoms[(prop) - FIRST_XATOM])
293 /* Maps X properties to Windows formats */
294 static const WCHAR wszRichTextFormat[] = {'R','i','c','h',' ','T','e','x','t',' ','F','o','r','m','a','t',0};
295 static const WCHAR wszGIF[] = {'G','I','F',0};
296 static const WCHAR wszJFIF[] = {'J','F','I','F',0};
297 static const WCHAR wszPNG[] = {'P','N','G',0};
298 static const WCHAR wszHTMLFormat[] = {'H','T','M','L',' ','F','o','r','m','a','t',0};
303 } PropertyFormatMap[] =
305 { wszRichTextFormat, XATOM_text_rtf },
306 { wszRichTextFormat, XATOM_text_richtext },
307 { wszGIF, XATOM_image_gif },
308 { wszJFIF, XATOM_image_jpeg },
309 { wszPNG, XATOM_image_png },
310 { wszHTMLFormat, XATOM_text_html },
315 * Cached clipboard data.
317 static LPWINE_CLIPDATA ClipData = NULL;
318 static UINT ClipDataCount = 0;
321 * Clipboard sequence number
323 static UINT wSeqNo = 0;
325 /**************************************************************************
326 * Internal Clipboard implementation methods
327 **************************************************************************/
329 static Window thread_selection_wnd(void)
331 struct x11drv_thread_data *thread_data = x11drv_init_thread_data();
332 Window w = thread_data->selection_wnd;
336 XSetWindowAttributes attr;
338 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
339 ButtonPressMask | ButtonReleaseMask | EnterWindowMask | PropertyChangeMask);
342 w = XCreateWindow(thread_data->display, root_window, 0, 0, 1, 1, 0, screen_depth,
343 InputOutput, CopyFromParent, CWEventMask, &attr);
347 thread_data->selection_wnd = w;
349 FIXME("Failed to create window. Fetching selection data will fail.\n");
355 /**************************************************************************
356 * X11DRV_InitClipboard
358 void X11DRV_InitClipboard(void)
362 /* Register known mapping between window formats and X properties */
363 for (i = 0; i < sizeof(PropertyFormatMap)/sizeof(PropertyFormatMap[0]); i++)
364 X11DRV_CLIPBOARD_InsertClipboardFormat(PropertyFormatMap[i].lpszFormat, 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;
381 for (format = ClipFormats, count = 0; format; format = format->NextFormat)
382 if (!format->drvData) count++;
385 display = thread_init_display();
387 names = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*names) );
388 atoms = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*atoms) );
390 for (format = ClipFormats, i = 0; format; format = format->NextFormat) {
391 if (!format->drvData) {
392 len = WideCharToMultiByte(CP_UNIXCP, 0, format->Name, -1, NULL, 0, NULL, NULL);
393 names[i] = HeapAlloc(GetProcessHeap(), 0, len);
394 WideCharToMultiByte(CP_UNIXCP, 0, format->Name, -1, names[i++], len, NULL, NULL);
399 XInternAtoms( display, names, count, False, atoms );
402 for (format = ClipFormats, i = 0; format; format = format->NextFormat) {
403 if (!format->drvData) {
404 HeapFree(GetProcessHeap(), 0, names[i]);
405 format->drvData = atoms[i++];
409 HeapFree( GetProcessHeap(), 0, names );
410 HeapFree( GetProcessHeap(), 0, atoms );
414 /**************************************************************************
417 * Register a custom X clipboard format.
419 static WINE_CLIPFORMAT *register_format( LPCWSTR FormatName, Atom prop )
421 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
423 TRACE("%s\n", debugstr_w(FormatName));
425 /* walk format chain to see if it's already registered */
428 if (CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, lpFormat->Name, -1, FormatName, -1) == CSTR_EQUAL
429 && (lpFormat->wFlags & CF_FLAG_BUILTINFMT) == 0)
431 lpFormat = lpFormat->NextFormat;
434 return X11DRV_CLIPBOARD_InsertClipboardFormat(FormatName, prop);
438 /**************************************************************************
439 * X11DRV_CLIPBOARD_LookupFormat
441 static LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupFormat(WORD wID)
443 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
447 if (lpFormat->wFormatID == wID)
450 lpFormat = lpFormat->NextFormat;
452 if (lpFormat && !lpFormat->drvData) intern_atoms();
457 /**************************************************************************
458 * X11DRV_CLIPBOARD_LookupProperty
460 static LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupProperty(LPWINE_CLIPFORMAT current, UINT drvData)
464 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
465 BOOL need_intern = FALSE;
468 lpFormat = current->NextFormat;
472 if (lpFormat->drvData == drvData) return lpFormat;
473 if (!lpFormat->drvData) need_intern = TRUE;
474 lpFormat = lpFormat->NextFormat;
476 if (!need_intern) return NULL;
478 /* restart the search for the new atoms */
483 /**************************************************************************
484 * X11DRV_CLIPBOARD_LookupData
486 static LPWINE_CLIPDATA X11DRV_CLIPBOARD_LookupData(DWORD wID)
488 LPWINE_CLIPDATA lpData = ClipData;
494 if (lpData->wFormatID == wID)
497 lpData = lpData->NextData;
499 while(lpData != ClipData);
501 if (lpData->wFormatID != wID)
509 /**************************************************************************
510 * InsertClipboardFormat
512 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(LPCWSTR FormatName, Atom prop)
514 LPWINE_CLIPFORMAT lpFormat;
515 LPWINE_CLIPFORMAT lpNewFormat;
518 /* allocate storage for new format entry */
519 lpNewFormat = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPFORMAT));
521 if(lpNewFormat == NULL)
523 WARN("No more memory for a new format!\n");
527 if (!(new_name = HeapAlloc(GetProcessHeap(), 0, (strlenW(FormatName)+1)*sizeof(WCHAR))))
529 WARN("No more memory for the new format name!\n");
530 HeapFree(GetProcessHeap(), 0, lpNewFormat);
534 lpNewFormat->Name = strcpyW(new_name, FormatName);
535 lpNewFormat->wFlags = 0;
536 lpNewFormat->wFormatID = GlobalAddAtomW(lpNewFormat->Name);
537 lpNewFormat->drvData = prop;
538 lpNewFormat->lpDrvImportFunc = X11DRV_CLIPBOARD_ImportClipboardData;
539 lpNewFormat->lpDrvExportFunc = X11DRV_CLIPBOARD_ExportClipboardData;
542 lpFormat = ClipFormats;
544 while(lpFormat->NextFormat) /* Move to last entry */
545 lpFormat = lpFormat->NextFormat;
547 lpNewFormat->NextFormat = NULL;
548 lpFormat->NextFormat = lpNewFormat;
549 lpNewFormat->PrevFormat = lpFormat;
551 TRACE("Registering format(%04x): %s drvData %d\n",
552 lpNewFormat->wFormatID, debugstr_w(FormatName), lpNewFormat->drvData);
560 /**************************************************************************
561 * X11DRV_CLIPBOARD_GetClipboardInfo
563 static BOOL X11DRV_CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
567 SERVER_START_REQ( set_clipboard_info )
571 if (wine_server_call_err( req ))
573 ERR("Failed to get clipboard owner.\n");
577 cbInfo->hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
578 cbInfo->hWndOwner = wine_server_ptr_handle( reply->old_owner );
579 cbInfo->hWndViewer = wine_server_ptr_handle( reply->old_viewer );
580 cbInfo->seqno = reply->seqno;
581 cbInfo->flags = reply->flags;
592 /**************************************************************************
593 * X11DRV_CLIPBOARD_ReleaseOwnership
595 static BOOL X11DRV_CLIPBOARD_ReleaseOwnership(void)
599 SERVER_START_REQ( set_clipboard_info )
601 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
603 if (wine_server_call_err( req ))
605 ERR("Failed to set clipboard.\n");
619 /**************************************************************************
620 * X11DRV_CLIPBOARD_InsertClipboardData
622 * Caller *must* have the clipboard open and be the owner.
624 static BOOL X11DRV_CLIPBOARD_InsertClipboardData(UINT wFormatID, HANDLE16 hData16,
625 HANDLE hData32, DWORD flags, LPWINE_CLIPFORMAT lpFormat, BOOL override)
627 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormatID);
629 TRACE("format=%04x lpData=%p hData16=%08x hData32=%p flags=0x%08x lpFormat=%p override=%d\n",
630 wFormatID, lpData, hData16, hData32, flags, lpFormat, override);
632 if (lpData && !override)
637 X11DRV_CLIPBOARD_FreeData(lpData);
639 lpData->hData16 = hData16; /* 0 is legal, see WM_RENDERFORMAT */
640 lpData->hData32 = hData32;
644 lpData = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPDATA));
646 lpData->wFormatID = wFormatID;
647 lpData->hData16 = hData16; /* 0 is legal, see WM_RENDERFORMAT */
648 lpData->hData32 = hData32;
649 lpData->lpFormat = lpFormat;
654 LPWINE_CLIPDATA lpPrevData = ClipData->PrevData;
656 lpData->PrevData = lpPrevData;
657 lpData->NextData = ClipData;
659 lpPrevData->NextData = lpData;
660 ClipData->PrevData = lpData;
664 lpData->NextData = lpData;
665 lpData->PrevData = lpData;
672 lpData->wFlags = flags;
678 /**************************************************************************
679 * X11DRV_CLIPBOARD_FreeData
681 * Free clipboard data handle.
683 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData)
685 TRACE("%04x\n", lpData->wFormatID);
687 if ((lpData->wFormatID >= CF_GDIOBJFIRST &&
688 lpData->wFormatID <= CF_GDIOBJLAST) ||
689 lpData->wFormatID == CF_BITMAP ||
690 lpData->wFormatID == CF_DIB ||
691 lpData->wFormatID == CF_PALETTE)
694 DeleteObject(lpData->hData32);
697 DeleteObject(HGDIOBJ_32(lpData->hData16));
699 if ((lpData->wFormatID == CF_DIB) && lpData->drvData)
700 XFreePixmap(gdi_display, lpData->drvData);
702 else if (lpData->wFormatID == CF_METAFILEPICT)
706 DeleteMetaFile(((METAFILEPICT *)GlobalLock( lpData->hData32 ))->hMF );
707 GlobalFree(lpData->hData32);
710 /* HMETAFILE16 and HMETAFILE32 are apparently the same thing,
711 and a shallow copy is enough to share a METAFILEPICT
712 structure between 16bit and 32bit clipboards. The MetaFile
713 should of course only be deleted once. */
714 GlobalFree16(lpData->hData16);
719 METAFILEPICT16* lpMetaPict = GlobalLock16(lpData->hData16);
723 /* To delete 16-bit meta file, we just need to free the associated
724 handle. See DeleteMetaFile16() in dlls/gdi/metafile.c. */
725 GlobalFree16(lpMetaPict->hMF);
729 GlobalFree16(lpData->hData16);
732 else if (lpData->wFormatID == CF_ENHMETAFILE)
735 DeleteEnhMetaFile(lpData->hData32);
737 else if (lpData->wFormatID < CF_PRIVATEFIRST ||
738 lpData->wFormatID > CF_PRIVATELAST)
741 GlobalFree(lpData->hData32);
744 GlobalFree16(lpData->hData16);
753 /**************************************************************************
754 * X11DRV_CLIPBOARD_UpdateCache
756 static BOOL X11DRV_CLIPBOARD_UpdateCache(LPCLIPBOARDINFO lpcbinfo)
760 if (!X11DRV_CLIPBOARD_IsSelectionOwner())
762 if (!X11DRV_CLIPBOARD_GetClipboardInfo(lpcbinfo))
764 ERR("Failed to retrieve clipboard information.\n");
767 else if (wSeqNo < lpcbinfo->seqno)
769 X11DRV_EmptyClipboard(TRUE);
771 if (X11DRV_CLIPBOARD_QueryAvailableData(thread_init_display(), lpcbinfo) < 0)
773 ERR("Failed to cache clipboard data owned by another process.\n");
778 X11DRV_EndClipboardUpdate();
781 wSeqNo = lpcbinfo->seqno;
789 /**************************************************************************
790 * X11DRV_CLIPBOARD_RenderFormat
792 static BOOL X11DRV_CLIPBOARD_RenderFormat(Display *display, LPWINE_CLIPDATA lpData)
796 TRACE(" 0x%04x hData32(%p) hData16(0x%08x)\n",
797 lpData->wFormatID, lpData->hData32, lpData->hData16);
799 if (lpData->hData32 || lpData->hData16)
800 return bret; /* Already rendered */
802 if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
803 bret = X11DRV_CLIPBOARD_RenderSynthesizedFormat(display, lpData);
804 else if (!X11DRV_CLIPBOARD_IsSelectionOwner())
806 if (!X11DRV_CLIPBOARD_ReadSelectionData(display, lpData))
808 ERR("Failed to cache clipboard data owned by another process. Format=%04x\n",
815 CLIPBOARDINFO cbInfo;
817 if (X11DRV_CLIPBOARD_GetClipboardInfo(&cbInfo) && cbInfo.hWndOwner)
819 /* Send a WM_RENDERFORMAT message to notify the owner to render the
820 * data requested into the clipboard.
822 TRACE("Sending WM_RENDERFORMAT message to hwnd(%p)\n", cbInfo.hWndOwner);
823 SendMessageW(cbInfo.hWndOwner, WM_RENDERFORMAT, (WPARAM)lpData->wFormatID, 0);
825 if (!lpData->hData32 && !lpData->hData16)
830 ERR("hWndClipOwner is lost!\n");
839 /**************************************************************************
840 * CLIPBOARD_ConvertText
841 * Returns number of required/converted characters - not bytes!
843 static INT CLIPBOARD_ConvertText(WORD src_fmt, void const *src, INT src_size,
844 WORD dst_fmt, void *dst, INT dst_size)
848 if(src_fmt == CF_UNICODETEXT)
861 return WideCharToMultiByte(cp, 0, src, src_size, dst, dst_size, NULL, NULL);
864 if(dst_fmt == CF_UNICODETEXT)
877 return MultiByteToWideChar(cp, 0, src, src_size, dst, dst_size);
880 if(!dst_size) return src_size;
882 if(dst_size > src_size) dst_size = src_size;
884 if(src_fmt == CF_TEXT )
885 CharToOemBuffA(src, dst, dst_size);
887 OemToCharBuffA(src, dst, dst_size);
893 /**************************************************************************
894 * X11DRV_CLIPBOARD_RenderSynthesizedFormat
896 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(Display *display, LPWINE_CLIPDATA lpData)
902 if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
904 UINT wFormatID = lpData->wFormatID;
906 if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
907 bret = X11DRV_CLIPBOARD_RenderSynthesizedText(display, wFormatID);
913 bret = X11DRV_CLIPBOARD_RenderSynthesizedDIB( display );
917 bret = X11DRV_CLIPBOARD_RenderSynthesizedBitmap( display );
921 case CF_METAFILEPICT:
922 FIXME("Synthesizing wFormatID(0x%08x) not implemented\n", wFormatID);
926 FIXME("Called to synthesize unknown format\n");
931 lpData->wFlags &= ~CF_FLAG_SYNTHESIZED;
938 /**************************************************************************
939 * X11DRV_CLIPBOARD_RenderSynthesizedText
941 * Renders synthesized text
943 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(Display *display, UINT wFormatID)
948 INT src_chars, dst_chars, alloc_size;
949 LPWINE_CLIPDATA lpSource = NULL;
951 TRACE("%04x\n", wFormatID);
953 if ((lpSource = X11DRV_CLIPBOARD_LookupData(wFormatID)) &&
957 /* Look for rendered source or non-synthesized source */
958 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
959 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
961 TRACE("UNICODETEXT -> %04x\n", wFormatID);
963 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
964 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
966 TRACE("TEXT -> %04x\n", wFormatID);
968 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
969 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
971 TRACE("OEMTEXT -> %04x\n", wFormatID);
974 if (!lpSource || (lpSource->wFlags & CF_FLAG_SYNTHESIZED &&
978 /* Ask the clipboard owner to render the source text if necessary */
979 if (!lpSource->hData32 && !X11DRV_CLIPBOARD_RenderFormat(display, lpSource))
982 if (lpSource->hData32)
984 lpstrS = GlobalLock(lpSource->hData32);
988 lpstrS = GlobalLock16(lpSource->hData16);
994 /* Text always NULL terminated */
995 if(lpSource->wFormatID == CF_UNICODETEXT)
996 src_chars = strlenW((LPCWSTR)lpstrS) + 1;
998 src_chars = strlen(lpstrS) + 1;
1000 /* Calculate number of characters in the destination buffer */
1001 dst_chars = CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS,
1002 src_chars, wFormatID, NULL, 0);
1007 TRACE("Converting from '%04x' to '%04x', %i chars\n",
1008 lpSource->wFormatID, wFormatID, src_chars);
1010 /* Convert characters to bytes */
1011 if(wFormatID == CF_UNICODETEXT)
1012 alloc_size = dst_chars * sizeof(WCHAR);
1014 alloc_size = dst_chars;
1016 hData32 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE |
1017 GMEM_DDESHARE, alloc_size);
1019 lpstrT = GlobalLock(hData32);
1023 CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS, src_chars,
1024 wFormatID, lpstrT, dst_chars);
1025 GlobalUnlock(hData32);
1029 if (lpSource->hData32)
1030 GlobalUnlock(lpSource->hData32);
1032 GlobalUnlock16(lpSource->hData16);
1034 return X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, 0, hData32, 0, NULL, TRUE);
1038 /**************************************************************************
1039 * X11DRV_CLIPBOARD_RenderSynthesizedDIB
1041 * Renders synthesized DIB
1043 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedDIB(Display *display)
1046 LPWINE_CLIPDATA lpSource = NULL;
1050 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) && lpSource->hData32)
1054 /* If we have a bitmap and it's not synthesized or it has been rendered */
1055 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) &&
1056 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
1058 /* Render source if required */
1059 if (lpSource->hData32 || X11DRV_CLIPBOARD_RenderFormat(display, lpSource))
1065 hData32 = X11DRV_DIB_CreateDIBFromBitmap(hdc, lpSource->hData32);
1066 ReleaseDC(NULL, hdc);
1070 X11DRV_CLIPBOARD_InsertClipboardData(CF_DIB, 0, hData32, 0, NULL, TRUE);
1080 /**************************************************************************
1081 * X11DRV_CLIPBOARD_RenderSynthesizedBitmap
1083 * Renders synthesized bitmap
1085 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedBitmap(Display *display)
1088 LPWINE_CLIPDATA lpSource = NULL;
1092 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) && lpSource->hData32)
1096 /* If we have a dib and it's not synthesized or it has been rendered */
1097 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) &&
1098 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
1100 /* Render source if required */
1101 if (lpSource->hData32 || X11DRV_CLIPBOARD_RenderFormat(display, lpSource))
1105 unsigned int offset;
1106 LPBITMAPINFOHEADER lpbmih;
1109 lpbmih = GlobalLock(lpSource->hData32);
1111 offset = sizeof(BITMAPINFOHEADER)
1112 + ((lpbmih->biBitCount <= 8) ? (sizeof(RGBQUAD) *
1113 (1 << lpbmih->biBitCount)) : 0);
1115 hData32 = CreateDIBitmap(hdc, lpbmih, CBM_INIT, (LPBYTE)lpbmih +
1116 offset, (LPBITMAPINFO) lpbmih, DIB_RGB_COLORS);
1118 GlobalUnlock(lpSource->hData32);
1119 ReleaseDC(NULL, hdc);
1123 X11DRV_CLIPBOARD_InsertClipboardData(CF_BITMAP, 0, hData32, 0, NULL, TRUE);
1133 /**************************************************************************
1134 * X11DRV_CLIPBOARD_ImportXAString
1136 * Import XA_STRING, converting the string to CF_TEXT.
1138 static HANDLE X11DRV_CLIPBOARD_ImportXAString(Display *display, Window w, Atom prop)
1141 unsigned long cbytes;
1143 unsigned long i, inlcount = 0;
1146 if (!X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1149 for (i = 0; i <= cbytes; i++)
1151 if (lpdata[i] == '\n')
1155 if ((hText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cbytes + inlcount + 1)))
1157 lpstr = GlobalLock(hText);
1159 for (i = 0, inlcount = 0; i <= cbytes; i++)
1161 if (lpdata[i] == '\n')
1162 lpstr[inlcount++] = '\r';
1164 lpstr[inlcount++] = lpdata[i];
1167 GlobalUnlock(hText);
1170 /* Free the retrieved property data */
1171 HeapFree(GetProcessHeap(), 0, lpdata);
1177 /**************************************************************************
1178 * X11DRV_CLIPBOARD_ImportUTF8
1180 * Import XA_STRING, converting the string to CF_UNICODE.
1182 static HANDLE X11DRV_CLIPBOARD_ImportUTF8(Display *display, Window w, Atom prop)
1185 unsigned long cbytes;
1187 unsigned long i, inlcount = 0;
1188 HANDLE hUnicodeText = 0;
1190 if (!X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1193 for (i = 0; i <= cbytes; i++)
1195 if (lpdata[i] == '\n')
1199 if ((lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cbytes + inlcount + 1)))
1203 for (i = 0, inlcount = 0; i <= cbytes; i++)
1205 if (lpdata[i] == '\n')
1206 lpstr[inlcount++] = '\r';
1208 lpstr[inlcount++] = lpdata[i];
1211 count = MultiByteToWideChar(CP_UTF8, 0, lpstr, -1, NULL, 0);
1212 hUnicodeText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, count * sizeof(WCHAR));
1216 WCHAR *textW = GlobalLock(hUnicodeText);
1217 MultiByteToWideChar(CP_UTF8, 0, lpstr, -1, textW, count);
1218 GlobalUnlock(hUnicodeText);
1221 HeapFree(GetProcessHeap(), 0, lpstr);
1224 /* Free the retrieved property data */
1225 HeapFree(GetProcessHeap(), 0, lpdata);
1227 return hUnicodeText;
1231 /**************************************************************************
1232 * X11DRV_CLIPBOARD_ImportCompoundText
1234 * Import COMPOUND_TEXT to CF_UNICODE
1236 static HANDLE X11DRV_CLIPBOARD_ImportCompoundText(Display *display, Window w, Atom prop)
1241 int srclen, destlen;
1242 HANDLE hUnicodeText;
1243 XTextProperty txtprop;
1245 if (!X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &txtprop.value, &txtprop.nitems))
1250 txtprop.encoding = x11drv_atom(COMPOUND_TEXT);
1253 ret = XmbTextPropertyToTextList(display, &txtprop, &srcstr, &count);
1254 wine_tsx11_unlock();
1255 HeapFree(GetProcessHeap(), 0, txtprop.value);
1256 if (ret != Success || !count) return 0;
1258 TRACE("Importing %d line(s)\n", count);
1260 /* Compute number of lines */
1261 srclen = strlen(srcstr[0]);
1262 for (i = 0, lcount = 0; i <= srclen; i++)
1264 if (srcstr[0][i] == '\n')
1268 destlen = MultiByteToWideChar(CP_UNIXCP, 0, srcstr[0], -1, NULL, 0);
1270 TRACE("lcount = %d, destlen=%d, srcstr %s\n", lcount, destlen, srcstr[0]);
1272 if ((hUnicodeText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (destlen + lcount + 1) * sizeof(WCHAR))))
1274 WCHAR *deststr = GlobalLock(hUnicodeText);
1275 MultiByteToWideChar(CP_UNIXCP, 0, srcstr[0], -1, deststr, destlen);
1279 for (i = destlen - 1, j = destlen + lcount - 1; i >= 0; i--, j--)
1281 deststr[j] = deststr[i];
1283 if (deststr[i] == '\n')
1284 deststr[--j] = '\r';
1288 GlobalUnlock(hUnicodeText);
1292 XFreeStringList(srcstr);
1293 wine_tsx11_unlock();
1295 return hUnicodeText;
1299 /**************************************************************************
1300 * X11DRV_CLIPBOARD_ImportXAPIXMAP
1302 * Import XA_PIXMAP, converting the image to CF_DIB.
1304 static HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(Display *display, Window w, Atom prop)
1309 unsigned long cbytes;
1311 HANDLE hClipData = 0;
1313 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1315 pPixmap = (Pixmap *) lpdata;
1317 hwnd = GetOpenClipboardWindow();
1320 hClipData = X11DRV_DIB_CreateDIBFromPixmap(*pPixmap, hdc);
1321 ReleaseDC(hwnd, hdc);
1323 /* Free the retrieved property data */
1324 HeapFree(GetProcessHeap(), 0, lpdata);
1331 /**************************************************************************
1332 * X11DRV_CLIPBOARD_ImportImageBmp
1334 * Import image/bmp, converting the image to CF_DIB.
1336 static HANDLE X11DRV_CLIPBOARD_ImportImageBmp(Display *display, Window w, Atom prop)
1339 unsigned long cbytes;
1340 HANDLE hClipData = 0;
1342 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1344 BITMAPFILEHEADER *bfh = (BITMAPFILEHEADER*)lpdata;
1346 if (cbytes >= sizeof(BITMAPFILEHEADER)+sizeof(BITMAPCOREHEADER) &&
1347 bfh->bfType == 0x4d42 /* "BM" */)
1349 BITMAPINFO *bmi = (BITMAPINFO*)(bfh+1);
1354 hbmp = CreateDIBitmap(
1358 lpdata+bfh->bfOffBits,
1363 hClipData = X11DRV_DIB_CreateDIBFromBitmap(hdc, hbmp);
1369 /* Free the retrieved property data */
1370 HeapFree(GetProcessHeap(), 0, lpdata);
1377 /**************************************************************************
1378 * X11DRV_CLIPBOARD_ImportMetaFilePict
1380 * Import MetaFilePict.
1382 static HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(Display *display, Window w, Atom prop)
1385 unsigned long cbytes;
1386 HANDLE hClipData = 0;
1388 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1391 hClipData = X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, lpdata, (LPDWORD)&cbytes, FALSE);
1393 /* Free the retrieved property data */
1394 HeapFree(GetProcessHeap(), 0, lpdata);
1401 /**************************************************************************
1402 * X11DRV_ImportEnhMetaFile
1404 * Import EnhMetaFile.
1406 static HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(Display *display, Window w, Atom prop)
1409 unsigned long cbytes;
1410 HANDLE hClipData = 0;
1412 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1415 hClipData = X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, lpdata, (LPDWORD)&cbytes, FALSE);
1417 /* Free the retrieved property data */
1418 HeapFree(GetProcessHeap(), 0, lpdata);
1425 /**************************************************************************
1426 * X11DRV_ImportClipbordaData
1428 * Generic import clipboard data routine.
1430 static HANDLE X11DRV_CLIPBOARD_ImportClipboardData(Display *display, Window w, Atom prop)
1434 unsigned long cbytes;
1435 HANDLE hClipData = 0;
1437 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1441 /* Turn on the DDESHARE flag to enable shared 32 bit memory */
1442 hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cbytes);
1446 if ((lpClipData = GlobalLock(hClipData)))
1448 memcpy(lpClipData, lpdata, cbytes);
1449 GlobalUnlock(hClipData);
1453 GlobalFree(hClipData);
1458 /* Free the retrieved property data */
1459 HeapFree(GetProcessHeap(), 0, lpdata);
1466 /**************************************************************************
1467 X11DRV_CLIPBOARD_ExportClipboardData
1469 * Generic export clipboard data routine.
1471 static HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Display *display, Window requestor, Atom aTarget,
1472 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1476 HANDLE hClipData = 0;
1478 *lpBytes = 0; /* Assume failure */
1480 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpData))
1481 ERR("Failed to export %04x format\n", lpData->wFormatID);
1484 datasize = GlobalSize(lpData->hData32);
1486 hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, datasize);
1487 if (hClipData == 0) return NULL;
1489 if ((lpClipData = GlobalLock(hClipData)))
1491 LPVOID lpdata = GlobalLock(lpData->hData32);
1493 memcpy(lpClipData, lpdata, datasize);
1494 *lpBytes = datasize;
1496 GlobalUnlock(lpData->hData32);
1497 GlobalUnlock(hClipData);
1499 GlobalFree(hClipData);
1508 /**************************************************************************
1509 * X11DRV_CLIPBOARD_ExportXAString
1511 * Export CF_TEXT converting the string to XA_STRING.
1512 * Helper function for X11DRV_CLIPBOARD_ExportString.
1514 static HANDLE X11DRV_CLIPBOARD_ExportXAString(LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1518 LPSTR text, lpstr = NULL;
1520 *lpBytes = 0; /* Assume return has zero bytes */
1522 text = GlobalLock(lpData->hData32);
1523 size = strlen(text);
1525 /* remove carriage returns */
1526 lpstr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size + 1);
1530 for (i = 0,j = 0; i < size && text[i]; i++)
1532 if (text[i] == '\r' && (text[i+1] == '\n' || text[i+1] == '\0'))
1534 lpstr[j++] = text[i];
1538 *lpBytes = j; /* Number of bytes in string */
1541 HeapFree(GetProcessHeap(), 0, text);
1542 GlobalUnlock(lpData->hData32);
1548 /**************************************************************************
1549 * X11DRV_CLIPBOARD_ExportUTF8String
1551 * Export CF_UNICODE converting the string to UTF8.
1552 * Helper function for X11DRV_CLIPBOARD_ExportString.
1554 static HANDLE X11DRV_CLIPBOARD_ExportUTF8String(LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1559 LPSTR text, lpstr = NULL;
1561 *lpBytes = 0; /* Assume return has zero bytes */
1563 uni_text = GlobalLock(lpData->hData32);
1565 size = WideCharToMultiByte(CP_UTF8, 0, uni_text, -1, NULL, 0, NULL, NULL);
1567 text = HeapAlloc(GetProcessHeap(), 0, size);
1570 WideCharToMultiByte(CP_UTF8, 0, uni_text, -1, text, size, NULL, NULL);
1572 /* remove carriage returns */
1573 lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size--);
1577 for (i = 0,j = 0; i < size && text[i]; i++)
1579 if (text[i] == '\r' && (text[i+1] == '\n' || text[i+1] == '\0'))
1581 lpstr[j++] = text[i];
1585 *lpBytes = j; /* Number of bytes in string */
1588 HeapFree(GetProcessHeap(), 0, text);
1589 GlobalUnlock(lpData->hData32);
1596 /**************************************************************************
1597 * X11DRV_CLIPBOARD_ExportCompoundText
1599 * Export CF_UNICODE to COMPOUND_TEXT
1600 * Helper function for X11DRV_CLIPBOARD_ExportString.
1602 static HANDLE X11DRV_CLIPBOARD_ExportCompoundText(Display *display, Window requestor, Atom aTarget, Atom rprop,
1603 LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1607 XICCEncodingStyle style;
1612 uni_text = GlobalLock(lpData->hData32);
1614 size = WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, NULL, 0, NULL, NULL);
1615 lpstr = HeapAlloc(GetProcessHeap(), 0, size);
1619 WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, lpstr, size, NULL, NULL);
1621 /* remove carriage returns */
1622 for (i = 0, j = 0; i < size && lpstr[i]; i++)
1624 if (lpstr[i] == '\r' && (lpstr[i+1] == '\n' || lpstr[i+1] == '\0'))
1626 lpstr[j++] = lpstr[i];
1630 GlobalUnlock(lpData->hData32);
1632 if (aTarget == x11drv_atom(COMPOUND_TEXT))
1633 style = XCompoundTextStyle;
1635 style = XStdICCTextStyle;
1637 /* Update the X property */
1639 if (XmbTextListToTextProperty(display, &lpstr, 1, style, &prop) == Success)
1641 XSetTextProperty(display, requestor, &prop, rprop);
1644 wine_tsx11_unlock();
1646 HeapFree(GetProcessHeap(), 0, lpstr);
1651 /**************************************************************************
1652 * X11DRV_CLIPBOARD_ExportString
1656 static HANDLE X11DRV_CLIPBOARD_ExportString(Display *display, Window requestor, Atom aTarget, Atom rprop,
1657 LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1659 if (X11DRV_CLIPBOARD_RenderFormat(display, lpData))
1661 if (aTarget == XA_STRING)
1662 return X11DRV_CLIPBOARD_ExportXAString(lpData, lpBytes);
1663 else if (aTarget == x11drv_atom(COMPOUND_TEXT) || aTarget == x11drv_atom(TEXT))
1664 return X11DRV_CLIPBOARD_ExportCompoundText(display, requestor, aTarget,
1665 rprop, lpData, lpBytes);
1668 TRACE("Exporting target %ld to default UTF8_STRING\n", aTarget);
1669 return X11DRV_CLIPBOARD_ExportUTF8String(lpData, lpBytes);
1673 ERR("Failed to render %04x format\n", lpData->wFormatID);
1679 /**************************************************************************
1680 * X11DRV_CLIPBOARD_ExportXAPIXMAP
1682 * Export CF_DIB to XA_PIXMAP.
1684 static HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Display *display, Window requestor, Atom aTarget, Atom rprop,
1685 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1689 unsigned char* lpData;
1691 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1693 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1697 if (!lpdata->drvData) /* If not already rendered */
1699 /* For convert from packed DIB to Pixmap */
1701 lpdata->drvData = (UINT) X11DRV_DIB_CreatePixmapFromDIB(lpdata->hData32, hdc);
1705 *lpBytes = sizeof(Pixmap); /* pixmap is a 32bit value */
1707 /* Wrap pixmap so we can return a handle */
1708 hData = GlobalAlloc(0, *lpBytes);
1709 lpData = GlobalLock(hData);
1710 memcpy(lpData, &lpdata->drvData, *lpBytes);
1711 GlobalUnlock(hData);
1717 /**************************************************************************
1718 * X11DRV_CLIPBOARD_ExportImageBmp
1720 * Export CF_DIB to image/bmp.
1722 static HANDLE X11DRV_CLIPBOARD_ExportImageBmp(Display *display, Window requestor, Atom aTarget, Atom rprop,
1723 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1730 BITMAPFILEHEADER *bfh;
1734 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1736 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1740 hpackeddib = lpdata->hData32;
1742 dibdata = GlobalLock(hpackeddib);
1745 ERR("Failed to lock packed DIB\n");
1749 bmpsize = sizeof(BITMAPFILEHEADER) + GlobalSize(hpackeddib);
1751 hbmpdata = GlobalAlloc(0, bmpsize);
1755 bmpdata = GlobalLock(hbmpdata);
1759 GlobalFree(hbmpdata);
1760 GlobalUnlock(hpackeddib);
1764 /* bitmap file header */
1765 bfh = (BITMAPFILEHEADER*)bmpdata;
1766 bfh->bfType = 0x4d42; /* "BM" */
1767 bfh->bfSize = bmpsize;
1768 bfh->bfReserved1 = 0;
1769 bfh->bfReserved2 = 0;
1770 bfh->bfOffBits = sizeof(BITMAPFILEHEADER) + bitmap_info_size((BITMAPINFO*)dibdata, DIB_RGB_COLORS);
1772 /* rest of bitmap is the same as the packed dib */
1773 memcpy(bfh+1, dibdata, bmpsize-sizeof(BITMAPFILEHEADER));
1777 GlobalUnlock(hbmpdata);
1780 GlobalUnlock(hpackeddib);
1786 /**************************************************************************
1787 * X11DRV_CLIPBOARD_ExportMetaFilePict
1789 * Export MetaFilePict.
1791 static HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Display *display, Window requestor, Atom aTarget, Atom rprop,
1792 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1794 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1796 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1800 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, lpdata->hData32, lpBytes, TRUE);
1804 /**************************************************************************
1805 * X11DRV_CLIPBOARD_ExportEnhMetaFile
1807 * Export EnhMetaFile.
1809 static HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Display *display, Window requestor, Atom aTarget, Atom rprop,
1810 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1812 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1814 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1818 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, lpdata->hData32, lpBytes, TRUE);
1822 /**************************************************************************
1823 * X11DRV_CLIPBOARD_QueryTargets
1825 static BOOL X11DRV_CLIPBOARD_QueryTargets(Display *display, Window w, Atom selection,
1826 Atom target, XEvent *xe)
1832 XConvertSelection(display, selection, target,
1833 x11drv_atom(SELECTION_DATA), w, CurrentTime);
1834 wine_tsx11_unlock();
1837 * Wait until SelectionNotify is received
1839 for (i = 0; i < SELECTION_RETRIES; i++)
1842 res = XCheckTypedWindowEvent(display, w, SelectionNotify, xe);
1843 wine_tsx11_unlock();
1844 if (res && xe->xselection.selection == selection) break;
1846 usleep(SELECTION_WAIT);
1849 if (i == SELECTION_RETRIES)
1851 ERR("Timed out waiting for SelectionNotify event\n");
1854 /* Verify that the selection returned a valid TARGETS property */
1855 if ((xe->xselection.target != target) || (xe->xselection.property == None))
1857 /* Selection owner failed to respond or we missed the SelectionNotify */
1858 WARN("Failed to retrieve TARGETS for selection %ld.\n", selection);
1866 static int is_atom_error( Display *display, XErrorEvent *event, void *arg )
1868 return (event->error_code == BadAtom);
1871 /**************************************************************************
1872 * X11DRV_CLIPBOARD_InsertSelectionProperties
1874 * Mark properties available for future retrieval.
1876 static VOID X11DRV_CLIPBOARD_InsertSelectionProperties(Display *display, Atom* properties, UINT count)
1878 UINT i, nb_atoms = 0;
1881 /* Cache these formats in the clipboard cache */
1882 for (i = 0; i < count; i++)
1884 LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupProperty(NULL, properties[i]);
1888 /* We found at least one Window's format that mapps to the property.
1889 * Continue looking for more.
1891 * If more than one property map to a Window's format then we use the first
1892 * one and ignore the rest.
1896 TRACE("Atom#%d Property(%d): --> FormatID(%04x) %s\n",
1897 i, lpFormat->drvData, lpFormat->wFormatID, debugstr_w(lpFormat->Name));
1898 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, 0, lpFormat, FALSE);
1899 lpFormat = X11DRV_CLIPBOARD_LookupProperty(lpFormat, properties[i]);
1902 else if (properties[i])
1904 /* add it to the list of atoms that we don't know about yet */
1905 if (!atoms) atoms = HeapAlloc( GetProcessHeap(), 0,
1906 (count - i) * sizeof(*atoms) );
1907 if (atoms) atoms[nb_atoms++] = properties[i];
1911 /* query all unknown atoms in one go */
1914 char **names = HeapAlloc( GetProcessHeap(), 0, nb_atoms * sizeof(*names) );
1917 X11DRV_expect_error( display, is_atom_error, NULL );
1918 if (!XGetAtomNames( display, atoms, nb_atoms, names )) nb_atoms = 0;
1919 if (X11DRV_check_error())
1921 WARN( "got some bad atoms, ignoring\n" );
1924 for (i = 0; i < nb_atoms; i++)
1926 WINE_CLIPFORMAT *lpFormat;
1928 int len = MultiByteToWideChar(CP_UNIXCP, 0, names[i], -1, NULL, 0);
1929 wname = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1930 MultiByteToWideChar(CP_UNIXCP, 0, names[i], -1, wname, len);
1932 lpFormat = register_format( wname, atoms[i] );
1933 HeapFree(GetProcessHeap(), 0, wname);
1936 ERR("Failed to register %s property. Type will not be cached.\n", names[i]);
1939 TRACE("Atom#%d Property(%d): --> FormatID(%04x) %s\n",
1940 i, lpFormat->drvData, lpFormat->wFormatID, debugstr_w(lpFormat->Name));
1941 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, 0, lpFormat, FALSE);
1944 for (i = 0; i < nb_atoms; i++) XFree( names[i] );
1945 wine_tsx11_unlock();
1946 HeapFree( GetProcessHeap(), 0, names );
1948 HeapFree( GetProcessHeap(), 0, atoms );
1953 /**************************************************************************
1954 * X11DRV_CLIPBOARD_QueryAvailableData
1956 * Caches the list of data formats available from the current selection.
1957 * This queries the selection owner for the TARGETS property and saves all
1958 * reported property types.
1960 static int X11DRV_CLIPBOARD_QueryAvailableData(Display *display, LPCLIPBOARDINFO lpcbinfo)
1963 Atom atype=AnyPropertyType;
1965 unsigned long remain;
1966 Atom* targetList=NULL;
1968 unsigned long cSelectionTargets = 0;
1970 if (selectionAcquired & (S_PRIMARY | S_CLIPBOARD))
1972 ERR("Received request to cache selection but process is owner=(%08x)\n",
1973 (unsigned) selectionWindow);
1974 return -1; /* Prevent self request */
1977 w = thread_selection_wnd();
1980 ERR("No window available to retrieve selection!\n");
1985 * Query the selection owner for the TARGETS property
1988 if ((use_primary_selection && XGetSelectionOwner(display,XA_PRIMARY)) ||
1989 XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
1991 wine_tsx11_unlock();
1992 if (use_primary_selection && (X11DRV_CLIPBOARD_QueryTargets(display, w, XA_PRIMARY, x11drv_atom(TARGETS), &xe)))
1993 selectionCacheSrc = XA_PRIMARY;
1994 else if (X11DRV_CLIPBOARD_QueryTargets(display, w, x11drv_atom(CLIPBOARD), x11drv_atom(TARGETS), &xe))
1995 selectionCacheSrc = x11drv_atom(CLIPBOARD);
1998 Atom xstr = XA_STRING;
2000 /* Selection Owner doesn't understand TARGETS, try retrieving XA_STRING */
2001 if (X11DRV_CLIPBOARD_QueryTargets(display, w, XA_PRIMARY, XA_STRING, &xe))
2003 X11DRV_CLIPBOARD_InsertSelectionProperties(display, &xstr, 1);
2004 selectionCacheSrc = XA_PRIMARY;
2007 else if (X11DRV_CLIPBOARD_QueryTargets(display, w, x11drv_atom(CLIPBOARD), XA_STRING, &xe))
2009 X11DRV_CLIPBOARD_InsertSelectionProperties(display, &xstr, 1);
2010 selectionCacheSrc = x11drv_atom(CLIPBOARD);
2015 WARN("Failed to query selection owner for available data.\n");
2020 else /* No selection owner so report 0 targets available */
2022 wine_tsx11_unlock();
2026 /* Read the TARGETS property contents */
2028 if(XGetWindowProperty(display, xe.xselection.requestor, xe.xselection.property,
2029 0, 0x3FFF, True, AnyPropertyType/*XA_ATOM*/, &atype, &aformat, &cSelectionTargets,
2030 &remain, (unsigned char**)&targetList) != Success)
2032 wine_tsx11_unlock();
2033 WARN("Failed to read TARGETS property\n");
2037 wine_tsx11_unlock();
2038 TRACE("Type %lx,Format %d,nItems %ld, Remain %ld\n",
2039 atype, aformat, cSelectionTargets, remain);
2041 * The TARGETS property should have returned us a list of atoms
2042 * corresponding to each selection target format supported.
2044 if (atype == XA_ATOM || atype == x11drv_atom(TARGETS))
2048 X11DRV_CLIPBOARD_InsertSelectionProperties(display, targetList, cSelectionTargets);
2050 else if (aformat == 8) /* work around quartz-wm brain damage */
2052 unsigned long i, count = cSelectionTargets / sizeof(CARD32);
2053 Atom *atoms = HeapAlloc( GetProcessHeap(), 0, count * sizeof(Atom) );
2054 for (i = 0; i < count; i++)
2055 atoms[i] = ((CARD32 *)targetList)[i]; /* FIXME: byte swapping */
2056 X11DRV_CLIPBOARD_InsertSelectionProperties( display, atoms, count );
2057 HeapFree( GetProcessHeap(), 0, atoms );
2061 /* Free the list of targets */
2064 wine_tsx11_unlock();
2067 return cSelectionTargets;
2071 /**************************************************************************
2072 * X11DRV_CLIPBOARD_ReadSelectionData
2074 * This method is invoked only when we DO NOT own the X selection
2076 * We always get the data from the selection client each time,
2077 * since we have no way of determining if the data in our cache is stale.
2079 static BOOL X11DRV_CLIPBOARD_ReadSelectionData(Display *display, LPWINE_CLIPDATA lpData)
2086 TRACE("%04x\n", lpData->wFormatID);
2088 if (!lpData->lpFormat)
2090 ERR("Requesting format %04x but no source format linked to data.\n",
2095 if (!selectionAcquired)
2097 Window w = thread_selection_wnd();
2100 ERR("No window available to read selection data!\n");
2104 TRACE("Requesting conversion of %s property (%d) from selection type %08x\n",
2105 debugstr_w(lpData->lpFormat->Name), lpData->lpFormat->drvData, (UINT)selectionCacheSrc);
2108 XConvertSelection(display, selectionCacheSrc, lpData->lpFormat->drvData,
2109 x11drv_atom(SELECTION_DATA), w, CurrentTime);
2110 wine_tsx11_unlock();
2112 /* wait until SelectionNotify is received */
2113 for (i = 0; i < SELECTION_RETRIES; i++)
2116 res = XCheckTypedWindowEvent(display, w, SelectionNotify, &xe);
2117 wine_tsx11_unlock();
2118 if (res && xe.xselection.selection == selectionCacheSrc) break;
2120 usleep(SELECTION_WAIT);
2123 if (i == SELECTION_RETRIES)
2125 ERR("Timed out waiting for SelectionNotify event\n");
2127 /* Verify that the selection returned a valid TARGETS property */
2128 else if (xe.xselection.property != None)
2131 * Read the contents of the X selection property
2132 * into WINE's clipboard cache and converting the
2133 * data format if necessary.
2135 HANDLE hData = lpData->lpFormat->lpDrvImportFunc(display, xe.xselection.requestor,
2136 xe.xselection.property);
2138 bRet = X11DRV_CLIPBOARD_InsertClipboardData(lpData->wFormatID, 0, hData, 0, lpData->lpFormat, TRUE);
2142 TRACE("Failed to convert selection\n");
2147 ERR("Received request to cache selection data but process is owner\n");
2150 TRACE("Returning %d\n", bRet);
2156 /**************************************************************************
2157 * X11DRV_CLIPBOARD_GetProperty
2158 * Gets type, data and size.
2160 static BOOL X11DRV_CLIPBOARD_GetProperty(Display *display, Window w, Atom prop,
2161 Atom *atype, unsigned char** data, unsigned long* datasize)
2164 unsigned long pos = 0, nitems, remain, count;
2165 unsigned char *val = NULL, *buffer;
2167 TRACE("Reading property %lu from X window %lx\n", prop, w);
2172 if (XGetWindowProperty(display, w, prop, pos, INT_MAX / 4, False,
2173 AnyPropertyType, atype, &aformat, &nitems, &remain, &buffer) != Success)
2175 wine_tsx11_unlock();
2176 WARN("Failed to read property\n");
2177 HeapFree( GetProcessHeap(), 0, val );
2181 count = get_property_size( aformat, nitems );
2182 if (!val) *data = HeapAlloc( GetProcessHeap(), 0, pos * sizeof(int) + count + 1 );
2183 else *data = HeapReAlloc( GetProcessHeap(), 0, val, pos * sizeof(int) + count + 1 );
2188 wine_tsx11_unlock();
2189 HeapFree( GetProcessHeap(), 0, val );
2193 memcpy( (int *)val + pos, buffer, count );
2195 wine_tsx11_unlock();
2198 *datasize = pos * sizeof(int) + count;
2202 pos += count / sizeof(int);
2205 /* Delete the property on the window now that we are done
2206 * This will send a PropertyNotify event to the selection owner. */
2208 XDeleteProperty(display, w, prop);
2209 wine_tsx11_unlock();
2214 /**************************************************************************
2215 * X11DRV_CLIPBOARD_ReadProperty
2216 * Reads the contents of the X selection property.
2218 static BOOL X11DRV_CLIPBOARD_ReadProperty(Display *display, Window w, Atom prop,
2219 unsigned char** data, unsigned long* datasize)
2227 if (!X11DRV_CLIPBOARD_GetProperty(display, w, prop, &atype, data, datasize))
2231 while (XCheckTypedWindowEvent(display, w, PropertyNotify, &xe))
2233 wine_tsx11_unlock();
2235 if (atype == x11drv_atom(INCR))
2237 unsigned char *buf = *data;
2238 unsigned long bufsize = 0;
2243 unsigned char *prop_data, *tmp;
2244 unsigned long prop_size;
2246 /* Wait until PropertyNotify is received */
2247 for (i = 0; i < SELECTION_RETRIES; i++)
2252 res = XCheckTypedWindowEvent(display, w, PropertyNotify, &xe);
2253 wine_tsx11_unlock();
2254 if (res && xe.xproperty.atom == prop &&
2255 xe.xproperty.state == PropertyNewValue)
2257 usleep(SELECTION_WAIT);
2260 if (i >= SELECTION_RETRIES ||
2261 !X11DRV_CLIPBOARD_GetProperty(display, w, prop, &atype, &prop_data, &prop_size))
2263 HeapFree(GetProcessHeap(), 0, buf);
2267 /* Retrieved entire data. */
2270 HeapFree(GetProcessHeap(), 0, prop_data);
2272 *datasize = bufsize;
2276 tmp = HeapReAlloc(GetProcessHeap(), 0, buf, bufsize + prop_size + 1);
2279 HeapFree(GetProcessHeap(), 0, buf);
2284 memcpy(buf + bufsize, prop_data, prop_size + 1);
2285 bufsize += prop_size;
2286 HeapFree(GetProcessHeap(), 0, prop_data);
2294 /**************************************************************************
2295 * CLIPBOARD_SerializeMetafile
2297 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out)
2301 TRACE(" wFormat=%d hdata=%p out=%d\n", wformat, hdata, out);
2303 if (out) /* Serialize out, caller should free memory */
2305 *lpcbytes = 0; /* Assume failure */
2307 if (wformat == CF_METAFILEPICT)
2309 LPMETAFILEPICT lpmfp = GlobalLock(hdata);
2310 unsigned int size = GetMetaFileBitsEx(lpmfp->hMF, 0, NULL);
2312 h = GlobalAlloc(0, size + sizeof(METAFILEPICT));
2315 char *pdata = GlobalLock(h);
2317 memcpy(pdata, lpmfp, sizeof(METAFILEPICT));
2318 GetMetaFileBitsEx(lpmfp->hMF, size, pdata + sizeof(METAFILEPICT));
2320 *lpcbytes = size + sizeof(METAFILEPICT);
2325 GlobalUnlock(hdata);
2327 else if (wformat == CF_ENHMETAFILE)
2329 int size = GetEnhMetaFileBits(hdata, 0, NULL);
2331 h = GlobalAlloc(0, size);
2334 LPVOID pdata = GlobalLock(h);
2336 GetEnhMetaFileBits(hdata, size, pdata);
2345 if (wformat == CF_METAFILEPICT)
2347 h = GlobalAlloc(0, sizeof(METAFILEPICT));
2350 unsigned int wiresize, size;
2351 LPMETAFILEPICT lpmfp = GlobalLock(h);
2353 memcpy(lpmfp, hdata, sizeof(METAFILEPICT));
2354 wiresize = *lpcbytes - sizeof(METAFILEPICT);
2355 lpmfp->hMF = SetMetaFileBitsEx(wiresize,
2356 ((const BYTE *)hdata) + sizeof(METAFILEPICT));
2357 size = GetMetaFileBitsEx(lpmfp->hMF, 0, NULL);
2361 else if (wformat == CF_ENHMETAFILE)
2363 h = SetEnhMetaFileBits(*lpcbytes, hdata);
2371 /**************************************************************************
2372 * X11DRV_CLIPBOARD_ReleaseSelection
2374 * Release XA_CLIPBOARD and XA_PRIMARY in response to a SelectionClear event.
2376 static void X11DRV_CLIPBOARD_ReleaseSelection(Display *display, Atom selType, Window w, HWND hwnd, Time time)
2378 /* w is the window that lost the selection
2380 TRACE("event->window = %08x (selectionWindow = %08x) selectionAcquired=0x%08x\n",
2381 (unsigned)w, (unsigned)selectionWindow, (unsigned)selectionAcquired);
2383 if (selectionAcquired && (w == selectionWindow))
2385 CLIPBOARDINFO cbinfo;
2387 /* completely give up the selection */
2388 TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
2390 X11DRV_CLIPBOARD_GetClipboardInfo(&cbinfo);
2392 if (cbinfo.flags & CB_PROCESS)
2394 /* Since we're still the owner, this wasn't initiated by
2395 another Wine process */
2396 if (OpenClipboard(hwnd))
2398 /* Destroy private objects */
2399 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
2401 /* Give up ownership of the windows clipboard */
2402 X11DRV_CLIPBOARD_ReleaseOwnership();
2407 if ((selType == x11drv_atom(CLIPBOARD)) && (selectionAcquired & S_PRIMARY))
2409 TRACE("Lost clipboard. Check if we need to release PRIMARY\n");
2412 if (selectionWindow == XGetSelectionOwner(display, XA_PRIMARY))
2414 TRACE("We still own PRIMARY. Releasing PRIMARY.\n");
2415 XSetSelectionOwner(display, XA_PRIMARY, None, time);
2418 TRACE("We no longer own PRIMARY\n");
2419 wine_tsx11_unlock();
2421 else if ((selType == XA_PRIMARY) && (selectionAcquired & S_CLIPBOARD))
2423 TRACE("Lost PRIMARY. Check if we need to release CLIPBOARD\n");
2426 if (selectionWindow == XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
2428 TRACE("We still own CLIPBOARD. Releasing CLIPBOARD.\n");
2429 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), None, time);
2432 TRACE("We no longer own CLIPBOARD\n");
2433 wine_tsx11_unlock();
2436 selectionWindow = None;
2438 X11DRV_EmptyClipboard(FALSE);
2440 /* Reset the selection flags now that we are done */
2441 selectionAcquired = S_NOSELECTION;
2446 /**************************************************************************
2447 * IsSelectionOwner (X11DRV.@)
2449 * Returns: TRUE if the selection is owned by this process, FALSE otherwise
2451 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void)
2453 return selectionAcquired;
2457 /**************************************************************************
2458 * X11DRV Clipboard Exports
2459 **************************************************************************/
2462 /**************************************************************************
2463 * RegisterClipboardFormat (X11DRV.@)
2465 * Registers a custom X clipboard format
2466 * Returns: Format id or 0 on failure
2468 UINT CDECL X11DRV_RegisterClipboardFormat(LPCWSTR FormatName)
2470 LPWINE_CLIPFORMAT lpFormat;
2472 if (FormatName == NULL) return 0;
2473 if (!(lpFormat = register_format( FormatName, 0 ))) return 0;
2474 return lpFormat->wFormatID;
2478 /**************************************************************************
2479 * X11DRV_GetClipboardFormatName
2481 INT CDECL X11DRV_GetClipboardFormatName(UINT wFormat, LPWSTR retStr, INT maxlen)
2483 LPWINE_CLIPFORMAT lpFormat;
2485 TRACE("(%04X, %p, %d) !\n", wFormat, retStr, maxlen);
2487 if (wFormat < 0xc000)
2489 SetLastError(ERROR_INVALID_PARAMETER);
2493 lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
2495 if (!lpFormat || (lpFormat->wFlags & CF_FLAG_BUILTINFMT))
2497 TRACE("Unknown format 0x%08x!\n", wFormat);
2498 SetLastError(ERROR_INVALID_HANDLE);
2502 lstrcpynW(retStr, lpFormat->Name, maxlen);
2504 return strlenW(retStr);
2507 static void selection_acquire(void)
2512 owner = thread_selection_wnd();
2513 display = thread_display();
2517 selectionAcquired = 0;
2518 selectionWindow = 0;
2520 /* Grab PRIMARY selection if not owned */
2521 if (use_primary_selection)
2522 XSetSelectionOwner(display, XA_PRIMARY, owner, CurrentTime);
2524 /* Grab CLIPBOARD selection if not owned */
2525 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), owner, CurrentTime);
2527 if (use_primary_selection && XGetSelectionOwner(display, XA_PRIMARY) == owner)
2528 selectionAcquired |= S_PRIMARY;
2530 if (XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)) == owner)
2531 selectionAcquired |= S_CLIPBOARD;
2533 wine_tsx11_unlock();
2535 if (selectionAcquired)
2537 selectionWindow = owner;
2538 TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner);
2542 static DWORD WINAPI selection_thread_proc(LPVOID unused)
2544 selection_acquire();
2546 while (selectionAcquired)
2548 MsgWaitForMultipleObjectsEx(0, NULL, INFINITE, QS_SENDMESSAGE, 0);
2554 /**************************************************************************
2555 * AcquireClipboard (X11DRV.@)
2557 int CDECL X11DRV_AcquireClipboard(HWND hWndClipWindow)
2560 HANDLE selectionThread;
2562 TRACE(" %p\n", hWndClipWindow);
2565 * It's important that the selection get acquired from the thread
2566 * that owns the clipboard window. The primary reason is that we know
2567 * it is running a message loop and therefore can process the
2568 * X selection events.
2570 if (hWndClipWindow &&
2571 GetCurrentThreadId() != GetWindowThreadProcessId(hWndClipWindow, &procid))
2573 if (procid != GetCurrentProcessId())
2575 WARN("Setting clipboard owner to other process is not supported\n");
2576 hWndClipWindow = NULL;
2580 TRACE("Thread %x is acquiring selection with thread %x's window %p\n",
2581 GetCurrentThreadId(),
2582 GetWindowThreadProcessId(hWndClipWindow, NULL), hWndClipWindow);
2584 return SendMessageW(hWndClipWindow, WM_X11DRV_ACQUIRE_SELECTION, 0, 0);
2590 selection_acquire();
2594 selectionThread = CreateThread(NULL, 0, &selection_thread_proc, NULL, 0, NULL);
2596 if (!selectionThread)
2598 WARN("Could not start clipboard thread\n");
2602 CloseHandle(selectionThread);
2609 /**************************************************************************
2610 * X11DRV_EmptyClipboard
2612 * Empty cached clipboard data.
2614 void CDECL X11DRV_EmptyClipboard(BOOL keepunowned)
2618 LPWINE_CLIPDATA lpData, lpStart;
2619 LPWINE_CLIPDATA lpNext = ClipData;
2621 TRACE(" called with %d entries in cache.\n", ClipDataCount);
2627 lpNext = lpData->NextData;
2629 if (!keepunowned || !(lpData->wFlags & CF_FLAG_UNOWNED))
2631 lpData->PrevData->NextData = lpData->NextData;
2632 lpData->NextData->PrevData = lpData->PrevData;
2634 if (lpData == ClipData)
2635 ClipData = lpNext != lpData ? lpNext : NULL;
2637 X11DRV_CLIPBOARD_FreeData(lpData);
2638 HeapFree(GetProcessHeap(), 0, lpData);
2642 } while (lpNext != lpStart);
2645 TRACE(" %d entries remaining in cache.\n", ClipDataCount);
2650 /**************************************************************************
2651 * X11DRV_SetClipboardData
2653 BOOL CDECL X11DRV_SetClipboardData(UINT wFormat, HANDLE16 hData16, HANDLE hData32, BOOL owner)
2656 BOOL bResult = TRUE;
2658 /* If it's not owned, data can only be set if the format data is not already owned
2659 and its rendering is not delayed */
2662 CLIPBOARDINFO cbinfo;
2663 LPWINE_CLIPDATA lpRender;
2665 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2667 if ((!hData16 && !hData32) ||
2668 ((lpRender = X11DRV_CLIPBOARD_LookupData(wFormat)) &&
2669 !(lpRender->wFlags & CF_FLAG_UNOWNED)))
2672 flags = CF_FLAG_UNOWNED;
2675 bResult &= X11DRV_CLIPBOARD_InsertClipboardData(wFormat, hData16, hData32, flags, NULL, TRUE);
2681 /**************************************************************************
2682 * CountClipboardFormats
2684 INT CDECL X11DRV_CountClipboardFormats(void)
2686 CLIPBOARDINFO cbinfo;
2688 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2690 TRACE(" count=%d\n", ClipDataCount);
2692 return ClipDataCount;
2696 /**************************************************************************
2697 * X11DRV_EnumClipboardFormats
2699 UINT CDECL X11DRV_EnumClipboardFormats(UINT wFormat)
2701 CLIPBOARDINFO cbinfo;
2702 UINT wNextFormat = 0;
2704 TRACE("(%04X)\n", wFormat);
2706 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2711 wNextFormat = ClipData->wFormatID;
2715 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormat);
2717 if (lpData && lpData->NextData != ClipData)
2718 wNextFormat = lpData->NextData->wFormatID;
2725 /**************************************************************************
2726 * X11DRV_IsClipboardFormatAvailable
2728 BOOL CDECL X11DRV_IsClipboardFormatAvailable(UINT wFormat)
2731 CLIPBOARDINFO cbinfo;
2733 TRACE("(%04X)\n", wFormat);
2735 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2737 if (wFormat != 0 && X11DRV_CLIPBOARD_LookupData(wFormat))
2740 TRACE("(%04X)- ret(%d)\n", wFormat, bRet);
2746 /**************************************************************************
2747 * GetClipboardData (USER.142)
2749 BOOL CDECL X11DRV_GetClipboardData(UINT wFormat, HANDLE16* phData16, HANDLE* phData32)
2751 CLIPBOARDINFO cbinfo;
2752 LPWINE_CLIPDATA lpRender;
2754 TRACE("(%04X)\n", wFormat);
2756 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2758 if ((lpRender = X11DRV_CLIPBOARD_LookupData(wFormat)))
2760 if ( !lpRender->hData32 )
2761 X11DRV_CLIPBOARD_RenderFormat(thread_init_display(), lpRender);
2763 /* Convert between 32 -> 16 bit data, if necessary */
2764 if (lpRender->hData32 && !lpRender->hData16)
2768 if (lpRender->wFormatID == CF_METAFILEPICT)
2769 size = sizeof(METAFILEPICT16);
2771 size = GlobalSize(lpRender->hData32);
2773 lpRender->hData16 = GlobalAlloc16(GMEM_ZEROINIT, size);
2775 if (!lpRender->hData16)
2776 ERR("(%04X) -- not enough memory in 16b heap\n", wFormat);
2779 if (lpRender->wFormatID == CF_METAFILEPICT)
2781 FIXME("\timplement function CopyMetaFilePict32to16\n");
2782 FIXME("\tin the appropriate file.\n");
2783 #ifdef SOMEONE_IMPLEMENTED_ME
2784 CopyMetaFilePict32to16(GlobalLock16(lpRender->hData16),
2785 GlobalLock(lpRender->hData32));
2790 memcpy(GlobalLock16(lpRender->hData16),
2791 GlobalLock(lpRender->hData32), size);
2794 GlobalUnlock16(lpRender->hData16);
2795 GlobalUnlock(lpRender->hData32);
2799 /* Convert between 32 -> 16 bit data, if necessary */
2800 if (lpRender->hData16 && !lpRender->hData32)
2804 if (lpRender->wFormatID == CF_METAFILEPICT)
2805 size = sizeof(METAFILEPICT16);
2807 size = GlobalSize(lpRender->hData32);
2809 lpRender->hData32 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE |
2810 GMEM_DDESHARE, size);
2812 if (lpRender->wFormatID == CF_METAFILEPICT)
2814 FIXME("\timplement function CopyMetaFilePict16to32\n");
2815 FIXME("\tin the appropriate file.\n");
2816 #ifdef SOMEONE_IMPLEMENTED_ME
2817 CopyMetaFilePict16to32(GlobalLock16(lpRender->hData32),
2818 GlobalLock(lpRender->hData16));
2823 memcpy(GlobalLock(lpRender->hData32),
2824 GlobalLock16(lpRender->hData16), size);
2827 GlobalUnlock(lpRender->hData32);
2828 GlobalUnlock16(lpRender->hData16);
2832 *phData16 = lpRender->hData16;
2835 *phData32 = lpRender->hData32;
2837 TRACE(" returning hData16(%04x) hData32(%p) (type %04x)\n",
2838 lpRender->hData16, lpRender->hData32, lpRender->wFormatID);
2840 return lpRender->hData16 || lpRender->hData32;
2847 /**************************************************************************
2848 * ResetSelectionOwner
2850 * Called when the thread owning the selection is destroyed and we need to
2851 * preserve the selection ownership. We look for another top level window
2852 * in this process and send it a message to acquire the selection.
2854 void X11DRV_ResetSelectionOwner(void)
2861 if (!selectionAcquired || thread_selection_wnd() != selectionWindow)
2864 selectionAcquired = S_NOSELECTION;
2865 selectionWindow = 0;
2867 hwnd = GetWindow(GetDesktopWindow(), GW_CHILD);
2870 if (GetCurrentThreadId() != GetWindowThreadProcessId(hwnd, &procid))
2872 if (GetCurrentProcessId() == procid)
2874 if (SendMessageW(hwnd, WM_X11DRV_ACQUIRE_SELECTION, 0, 0))
2878 } while ((hwnd = GetWindow(hwnd, GW_HWNDNEXT)) != NULL);
2880 WARN("Failed to find another thread to take selection ownership. Clipboard data will be lost.\n");
2882 X11DRV_CLIPBOARD_ReleaseOwnership();
2883 X11DRV_EmptyClipboard(FALSE);
2887 /**************************************************************************
2888 * X11DRV_CLIPBOARD_SynthesizeData
2890 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID)
2893 LPWINE_CLIPDATA lpSource = NULL;
2895 TRACE(" %04x\n", wFormatID);
2897 /* Don't need to synthesize if it already exists */
2898 if (X11DRV_CLIPBOARD_LookupData(wFormatID))
2901 if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
2903 bsyn = ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
2904 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2905 ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
2906 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2907 ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
2908 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED);
2910 else if (wFormatID == CF_ENHMETAFILE)
2912 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT)) &&
2913 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2915 else if (wFormatID == CF_METAFILEPICT)
2917 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_ENHMETAFILE)) &&
2918 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2920 else if (wFormatID == CF_DIB)
2922 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) &&
2923 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2925 else if (wFormatID == CF_BITMAP)
2927 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) &&
2928 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2932 X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, 0, 0, CF_FLAG_SYNTHESIZED, NULL, TRUE);
2939 /**************************************************************************
2940 * X11DRV_EndClipboardUpdate
2942 * Add locale if it hasn't already been added
2944 void CDECL X11DRV_EndClipboardUpdate(void)
2946 INT count = ClipDataCount;
2948 /* Do Unicode <-> Text <-> OEM mapping */
2949 X11DRV_CLIPBOARD_SynthesizeData(CF_TEXT);
2950 X11DRV_CLIPBOARD_SynthesizeData(CF_OEMTEXT);
2951 X11DRV_CLIPBOARD_SynthesizeData(CF_UNICODETEXT);
2953 /* Enhmetafile <-> MetafilePict mapping */
2954 X11DRV_CLIPBOARD_SynthesizeData(CF_ENHMETAFILE);
2955 X11DRV_CLIPBOARD_SynthesizeData(CF_METAFILEPICT);
2957 /* DIB <-> Bitmap mapping */
2958 X11DRV_CLIPBOARD_SynthesizeData(CF_DIB);
2959 X11DRV_CLIPBOARD_SynthesizeData(CF_BITMAP);
2961 TRACE("%d formats added to cached data\n", ClipDataCount - count);
2965 /***********************************************************************
2966 * X11DRV_SelectionRequest_TARGETS
2967 * Service a TARGETS selection request event
2969 static Atom X11DRV_SelectionRequest_TARGETS( Display *display, Window requestor,
2970 Atom target, Atom rprop )
2975 LPWINE_CLIPFORMAT lpFormats;
2976 LPWINE_CLIPDATA lpData;
2978 /* Create X atoms for any clipboard types which don't have atoms yet.
2979 * This avoids sending bogus zero atoms.
2980 * Without this, copying might not have access to all clipboard types.
2981 * FIXME: is it safe to call this here?
2986 * Count the number of items we wish to expose as selection targets.
2988 cTargets = 1; /* Include TARGETS */
2990 if (!(lpData = ClipData)) return None;
2994 lpFormats = ClipFormats;
2998 if ((lpFormats->wFormatID == lpData->wFormatID) &&
2999 lpFormats->lpDrvExportFunc && lpFormats->drvData)
3002 lpFormats = lpFormats->NextFormat;
3005 lpData = lpData->NextData;
3007 while (lpData != ClipData);
3009 TRACE(" found %d formats\n", cTargets);
3011 /* Allocate temp buffer */
3012 targets = HeapAlloc( GetProcessHeap(), 0, cTargets * sizeof(Atom));
3018 targets[i++] = x11drv_atom(TARGETS);
3022 lpFormats = ClipFormats;
3026 if ((lpFormats->wFormatID == lpData->wFormatID) &&
3027 lpFormats->lpDrvExportFunc && lpFormats->drvData)
3028 targets[i++] = lpFormats->drvData;
3030 lpFormats = lpFormats->NextFormat;
3033 lpData = lpData->NextData;
3035 while (lpData != ClipData);
3039 if (TRACE_ON(clipboard))
3042 for ( i = 0; i < cTargets; i++)
3044 char *itemFmtName = XGetAtomName(display, targets[i]);
3045 TRACE("\tAtom# %d: Property %ld Type %s\n", i, targets[i], itemFmtName);
3050 /* We may want to consider setting the type to xaTargets instead,
3051 * in case some apps expect this instead of XA_ATOM */
3052 XChangeProperty(display, requestor, rprop, XA_ATOM, 32,
3053 PropModeReplace, (unsigned char *)targets, cTargets);
3054 wine_tsx11_unlock();
3056 HeapFree(GetProcessHeap(), 0, targets);
3062 /***********************************************************************
3063 * X11DRV_SelectionRequest_MULTIPLE
3064 * Service a MULTIPLE selection request event
3065 * rprop contains a list of (target,property) atom pairs.
3066 * The first atom names a target and the second names a property.
3067 * The effect is as if we have received a sequence of SelectionRequest events
3068 * (one for each atom pair) except that:
3069 * 1. We reply with a SelectionNotify only when all the requested conversions
3070 * have been performed.
3071 * 2. If we fail to convert the target named by an atom in the MULTIPLE property,
3072 * we replace the atom in the property by None.
3074 static Atom X11DRV_SelectionRequest_MULTIPLE( HWND hWnd, XSelectionRequestEvent *pevent )
3076 Display *display = pevent->display;
3078 Atom atype=AnyPropertyType;
3080 unsigned long remain;
3081 Atom* targetPropList=NULL;
3082 unsigned long cTargetPropList = 0;
3084 /* If the specified property is None the requestor is an obsolete client.
3085 * We support these by using the specified target atom as the reply property.
3087 rprop = pevent->property;
3089 rprop = pevent->target;
3093 /* Read the MULTIPLE property contents. This should contain a list of
3094 * (target,property) atom pairs.
3097 if(XGetWindowProperty(display, pevent->requestor, rprop,
3098 0, 0x3FFF, False, AnyPropertyType, &atype,&aformat,
3099 &cTargetPropList, &remain,
3100 (unsigned char**)&targetPropList) != Success)
3102 wine_tsx11_unlock();
3103 TRACE("\tCouldn't read MULTIPLE property\n");
3107 TRACE("\tType %s,Format %d,nItems %ld, Remain %ld\n",
3108 XGetAtomName(display, atype), aformat, cTargetPropList, remain);
3109 wine_tsx11_unlock();
3112 * Make sure we got what we expect.
3113 * NOTE: According to the X-ICCCM Version 2.0 documentation the property sent
3114 * in a MULTIPLE selection request should be of type ATOM_PAIR.
3115 * However some X apps(such as XPaint) are not compliant with this and return
3116 * a user defined atom in atype when XGetWindowProperty is called.
3117 * The data *is* an atom pair but is not denoted as such.
3119 if(aformat == 32 /* atype == xAtomPair */ )
3123 /* Iterate through the ATOM_PAIR list and execute a SelectionRequest
3124 * for each (target,property) pair */
3126 for (i = 0; i < cTargetPropList; i+=2)
3128 XSelectionRequestEvent event;
3130 if (TRACE_ON(clipboard))
3132 char *targetName, *propName;
3134 targetName = XGetAtomName(display, targetPropList[i]);
3135 propName = XGetAtomName(display, targetPropList[i+1]);
3136 TRACE("MULTIPLE(%d): Target='%s' Prop='%s'\n",
3137 i/2, targetName, propName);
3140 wine_tsx11_unlock();
3143 /* We must have a non "None" property to service a MULTIPLE target atom */
3144 if ( !targetPropList[i+1] )
3146 TRACE("\tMULTIPLE(%d): Skipping target with empty property!\n", i);
3150 /* Set up an XSelectionRequestEvent for this (target,property) pair */
3152 event.target = targetPropList[i];
3153 event.property = targetPropList[i+1];
3155 /* Fire a SelectionRequest, informing the handler that we are processing
3156 * a MULTIPLE selection request event.
3158 X11DRV_HandleSelectionRequest( hWnd, &event, TRUE );
3162 /* Free the list of targets/properties */
3164 XFree(targetPropList);
3165 wine_tsx11_unlock();
3172 /***********************************************************************
3173 * X11DRV_HandleSelectionRequest
3174 * Process an event selection request event.
3175 * The bIsMultiple flag is used to signal when EVENT_SelectionRequest is called
3176 * recursively while servicing a "MULTIPLE" selection target.
3178 * Note: We only receive this event when WINE owns the X selection
3180 static void X11DRV_HandleSelectionRequest( HWND hWnd, XSelectionRequestEvent *event, BOOL bIsMultiple )
3182 Display *display = event->display;
3183 XSelectionEvent result;
3185 Window request = event->requestor;
3190 * We can only handle the selection request if :
3191 * The selection is PRIMARY or CLIPBOARD, AND we can successfully open the clipboard.
3192 * Don't do these checks or open the clipboard while recursively processing MULTIPLE,
3193 * since this has been already done.
3197 if (((event->selection != XA_PRIMARY) && (event->selection != x11drv_atom(CLIPBOARD))))
3201 /* If the specified property is None the requestor is an obsolete client.
3202 * We support these by using the specified target atom as the reply property.
3204 rprop = event->property;
3206 rprop = event->target;
3208 if(event->target == x11drv_atom(TARGETS)) /* Return a list of all supported targets */
3210 /* TARGETS selection request */
3211 rprop = X11DRV_SelectionRequest_TARGETS( display, request, event->target, rprop );
3213 else if(event->target == x11drv_atom(MULTIPLE)) /* rprop contains a list of (target, property) atom pairs */
3215 /* MULTIPLE selection request */
3216 rprop = X11DRV_SelectionRequest_MULTIPLE( hWnd, event );
3220 LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupProperty(NULL, event->target);
3222 if (lpFormat && lpFormat->lpDrvExportFunc)
3224 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(lpFormat->wFormatID);
3228 unsigned char* lpClipData;
3230 HANDLE hClipData = lpFormat->lpDrvExportFunc(display, request, event->target,
3231 rprop, lpData, &cBytes);
3233 if (hClipData && (lpClipData = GlobalLock(hClipData)))
3235 TRACE("\tUpdating property %s, %d bytes\n", debugstr_w(lpFormat->Name), cBytes);
3238 XChangeProperty(display, request, rprop, event->target,
3239 8, PropModeReplace, lpClipData, cBytes);
3240 wine_tsx11_unlock();
3242 GlobalUnlock(hClipData);
3243 GlobalFree(hClipData);
3251 * SelectionNotify should be sent only at the end of a MULTIPLE request
3255 result.type = SelectionNotify;
3256 result.display = display;
3257 result.requestor = request;
3258 result.selection = event->selection;
3259 result.property = rprop;
3260 result.target = event->target;
3261 result.time = event->time;
3262 TRACE("Sending SelectionNotify event...\n");
3264 XSendEvent(display,event->requestor,False,NoEventMask,(XEvent*)&result);
3265 wine_tsx11_unlock();
3270 /***********************************************************************
3271 * X11DRV_SelectionRequest
3273 void X11DRV_SelectionRequest( HWND hWnd, XEvent *event )
3275 X11DRV_HandleSelectionRequest( hWnd, &event->xselectionrequest, FALSE );
3279 /***********************************************************************
3280 * X11DRV_SelectionClear
3282 void X11DRV_SelectionClear( HWND hWnd, XEvent *xev )
3284 XSelectionClearEvent *event = &xev->xselectionclear;
3285 if (event->selection == XA_PRIMARY || event->selection == x11drv_atom(CLIPBOARD))
3286 X11DRV_CLIPBOARD_ReleaseSelection( event->display, event->selection,
3287 event->window, hWnd, event->time );