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"
82 #include "wine/debug.h"
83 #include "wine/unicode.h"
84 #include "wine/server.h"
86 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
88 /* Maximum wait time for selection notify */
89 #define SELECTION_RETRIES 500 /* wait for .5 seconds */
90 #define SELECTION_WAIT 1000 /* us */
93 #define S_NOSELECTION 0
104 } CLIPBOARDINFO, *LPCLIPBOARDINFO;
106 struct tagWINE_CLIPDATA; /* Forward */
108 typedef HANDLE (*DRVEXPORTFUNC)(Display *display, Window requestor, Atom aTarget, Atom rprop,
109 struct tagWINE_CLIPDATA* lpData, LPDWORD lpBytes);
110 typedef HANDLE (*DRVIMPORTFUNC)(Display *d, Window w, Atom prop);
112 typedef struct tagWINE_CLIPFORMAT {
117 DRVIMPORTFUNC lpDrvImportFunc;
118 DRVEXPORTFUNC lpDrvExportFunc;
119 struct tagWINE_CLIPFORMAT *PrevFormat;
120 struct tagWINE_CLIPFORMAT *NextFormat;
121 } WINE_CLIPFORMAT, *LPWINE_CLIPFORMAT;
123 typedef struct tagWINE_CLIPDATA {
128 LPWINE_CLIPFORMAT lpFormat;
129 struct tagWINE_CLIPDATA *PrevData;
130 struct tagWINE_CLIPDATA *NextData;
131 } WINE_CLIPDATA, *LPWINE_CLIPDATA;
133 #define CF_FLAG_BUILTINFMT 0x0001 /* Built-in windows format */
134 #define CF_FLAG_UNOWNED 0x0002 /* cached data is not owned */
135 #define CF_FLAG_SYNTHESIZED 0x0004 /* Implicitly converted data */
136 #define CF_FLAG_UNICODE 0x0008 /* Data is in unicode */
138 static int selectionAcquired = 0; /* Contains the current selection masks */
139 static Window selectionWindow = None; /* The top level X window which owns the selection */
140 static Atom selectionCacheSrc = XA_PRIMARY; /* The selection source from which the clipboard cache was filled */
142 void CDECL X11DRV_EmptyClipboard(BOOL keepunowned);
143 void CDECL X11DRV_EndClipboardUpdate(void);
144 static HANDLE X11DRV_CLIPBOARD_ImportClipboardData(Display *d, Window w, Atom prop);
145 static HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(Display *d, Window w, Atom prop);
146 static HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(Display *d, Window w, Atom prop);
147 static HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(Display *d, Window w, Atom prop);
148 static HANDLE X11DRV_CLIPBOARD_ImportImageBmp(Display *d, Window w, Atom prop);
149 static HANDLE X11DRV_CLIPBOARD_ImportXAString(Display *d, Window w, Atom prop);
150 static HANDLE X11DRV_CLIPBOARD_ImportUTF8(Display *d, Window w, Atom prop);
151 static HANDLE X11DRV_CLIPBOARD_ImportCompoundText(Display *d, Window w, Atom prop);
152 static HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Display *display, Window requestor, Atom aTarget,
153 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
154 static HANDLE X11DRV_CLIPBOARD_ExportString(Display *display, Window requestor, Atom aTarget,
155 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
156 static HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Display *display, Window requestor, Atom aTarget,
157 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
158 static HANDLE X11DRV_CLIPBOARD_ExportImageBmp(Display *display, Window requestor, Atom aTarget,
159 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
160 static HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Display *display, Window requestor, Atom aTarget,
161 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
162 static HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Display *display, Window requestor, Atom aTarget,
163 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
164 static HANDLE X11DRV_CLIPBOARD_ExportTextHtml(Display *display, Window requestor, Atom aTarget,
165 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
166 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(LPCWSTR FormatName, Atom prop);
167 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(Display *display, UINT wFormatID);
168 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData);
169 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void);
170 static int X11DRV_CLIPBOARD_QueryAvailableData(Display *display, LPCLIPBOARDINFO lpcbinfo);
171 static BOOL X11DRV_CLIPBOARD_ReadSelectionData(Display *display, LPWINE_CLIPDATA lpData);
172 static BOOL X11DRV_CLIPBOARD_ReadProperty(Display *display, Window w, Atom prop,
173 unsigned char** data, unsigned long* datasize);
174 static BOOL X11DRV_CLIPBOARD_RenderFormat(Display *display, LPWINE_CLIPDATA lpData);
175 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out);
176 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID);
177 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(Display *display, LPWINE_CLIPDATA lpData);
178 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedDIB(Display *display);
179 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedBitmap(Display *display);
180 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile(Display *display);
181 static void X11DRV_HandleSelectionRequest( HWND hWnd, XSelectionRequestEvent *event, BOOL bIsMultiple );
184 * WARNING: This data ordering is dependent on the WINE_CLIPFORMAT structure
185 * declared in clipboard.h
187 static const WCHAR wszCF_TEXT[] = {'W','C','F','_','T','E','X','T',0};
188 static const WCHAR wszCF_BITMAP[] = {'W','C','F','_','B','I','T','M','A','P',0};
189 static const WCHAR wszCF_METAFILEPICT[] = {'W','C','F','_','M','E','T','A','F','I','L','E','P','I','C','T',0};
190 static const WCHAR wszCF_SYLK[] = {'W','C','F','_','S','Y','L','K',0};
191 static const WCHAR wszCF_DIF[] = {'W','C','F','_','D','I','F',0};
192 static const WCHAR wszCF_TIFF[] = {'W','C','F','_','T','I','F','F',0};
193 static const WCHAR wszCF_OEMTEXT[] = {'W','C','F','_','O','E','M','T','E','X','T',0};
194 static const WCHAR wszCF_DIB[] = {'W','C','F','_','D','I','B',0};
195 static const WCHAR wszIMAGEBMP[] = {'i','m','a','g','e','/','b','m','p',0};
196 static const WCHAR wszCF_PALETTE[] = {'W','C','F','_','P','A','L','E','T','T','E',0};
197 static const WCHAR wszCF_PENDATA[] = {'W','C','F','_','P','E','N','D','A','T','A',0};
198 static const WCHAR wszCF_RIFF[] = {'W','C','F','_','R','I','F','F',0};
199 static const WCHAR wszCF_WAVE[] = {'W','C','F','_','W','A','V','E',0};
200 static const WCHAR wszCOMPOUNDTEXT[] = {'C','O','M','P','O','U','N','D','_','T','E','X','T',0};
201 static const WCHAR wszUTF8STRING[] = {'U','T','F','8','_','S','T','R','I','N','G',0};
202 static const WCHAR wszCF_ENHMETAFILE[] = {'W','C','F','_','E','N','H','M','E','T','A','F','I','L','E',0};
203 static const WCHAR wszCF_HDROP[] = {'W','C','F','_','H','D','R','O','P',0};
204 static const WCHAR wszCF_LOCALE[] = {'W','C','F','_','L','O','C','A','L','E',0};
205 static const WCHAR wszCF_DIBV5[] = {'W','C','F','_','D','I','B','V','5',0};
206 static const WCHAR wszCF_OWNERDISPLAY[] = {'W','C','F','_','O','W','N','E','R','D','I','S','P','L','A','Y',0};
207 static const WCHAR wszCF_DSPTEXT[] = {'W','C','F','_','D','S','P','T','E','X','T',0};
208 static const WCHAR wszCF_DSPBITMAP[] = {'W','C','F','_','D','S','P','B','I','T','M','A','P',0};
209 static const WCHAR wszCF_DSPMETAFILEPICT[] = {'W','C','F','_','D','S','P','M','E','T','A','F','I','L','E','P','I','C','T',0};
210 static const WCHAR wszCF_DSPENHMETAFILE[] = {'W','C','F','_','D','S','P','E','N','H','M','E','T','A','F','I','L','E',0};
212 static WINE_CLIPFORMAT ClipFormats[] =
214 { CF_TEXT, wszCF_TEXT, XA_STRING, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportXAString,
215 X11DRV_CLIPBOARD_ExportString, NULL, &ClipFormats[1]},
217 { CF_BITMAP, wszCF_BITMAP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
218 NULL, &ClipFormats[0], &ClipFormats[2]},
220 { CF_METAFILEPICT, wszCF_METAFILEPICT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportMetaFilePict,
221 X11DRV_CLIPBOARD_ExportMetaFilePict, &ClipFormats[1], &ClipFormats[3]},
223 { CF_SYLK, wszCF_SYLK, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
224 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[2], &ClipFormats[4]},
226 { CF_DIF, wszCF_DIF, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
227 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[3], &ClipFormats[5]},
229 { CF_TIFF, wszCF_TIFF, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
230 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[4], &ClipFormats[6]},
232 { CF_OEMTEXT, wszCF_OEMTEXT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
233 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[5], &ClipFormats[7]},
235 { CF_DIB, wszCF_DIB, XA_PIXMAP, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportXAPIXMAP,
236 X11DRV_CLIPBOARD_ExportXAPIXMAP, &ClipFormats[6], &ClipFormats[8]},
238 { CF_PALETTE, wszCF_PALETTE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
239 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[7], &ClipFormats[9]},
241 { CF_PENDATA, wszCF_PENDATA, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
242 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[8], &ClipFormats[10]},
244 { CF_RIFF, wszCF_RIFF, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
245 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[9], &ClipFormats[11]},
247 { CF_WAVE, wszCF_WAVE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
248 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[10], &ClipFormats[12]},
250 { CF_UNICODETEXT, wszUTF8STRING, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportUTF8,
251 X11DRV_CLIPBOARD_ExportString, &ClipFormats[11], &ClipFormats[13]},
253 /* If UTF8_STRING is not available, attempt COMPUND_TEXT */
254 { CF_UNICODETEXT, wszCOMPOUNDTEXT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportCompoundText,
255 X11DRV_CLIPBOARD_ExportString, &ClipFormats[12], &ClipFormats[14]},
257 { CF_ENHMETAFILE, wszCF_ENHMETAFILE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportEnhMetaFile,
258 X11DRV_CLIPBOARD_ExportEnhMetaFile, &ClipFormats[13], &ClipFormats[15]},
260 { CF_HDROP, wszCF_HDROP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
261 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[14], &ClipFormats[16]},
263 { CF_LOCALE, wszCF_LOCALE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
264 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[15], &ClipFormats[17]},
266 { CF_DIBV5, wszCF_DIBV5, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
267 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[16], &ClipFormats[18]},
269 { CF_OWNERDISPLAY, wszCF_OWNERDISPLAY, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
270 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[17], &ClipFormats[19]},
272 { CF_DSPTEXT, wszCF_DSPTEXT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
273 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[18], &ClipFormats[20]},
275 { CF_DSPBITMAP, wszCF_DSPBITMAP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
276 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[19], &ClipFormats[21]},
278 { CF_DSPMETAFILEPICT, wszCF_DSPMETAFILEPICT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
279 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[20], &ClipFormats[22]},
281 { CF_DSPENHMETAFILE, wszCF_DSPENHMETAFILE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
282 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[21], &ClipFormats[23]},
284 { CF_DIB, wszIMAGEBMP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportImageBmp,
285 X11DRV_CLIPBOARD_ExportImageBmp, &ClipFormats[22], NULL},
288 #define GET_ATOM(prop) (((prop) < FIRST_XATOM) ? (Atom)(prop) : X11DRV_Atoms[(prop) - FIRST_XATOM])
290 /* Maps X properties to Windows formats */
291 static const WCHAR wszRichTextFormat[] = {'R','i','c','h',' ','T','e','x','t',' ','F','o','r','m','a','t',0};
292 static const WCHAR wszGIF[] = {'G','I','F',0};
293 static const WCHAR wszJFIF[] = {'J','F','I','F',0};
294 static const WCHAR wszPNG[] = {'P','N','G',0};
295 static const WCHAR wszHTMLFormat[] = {'H','T','M','L',' ','F','o','r','m','a','t',0};
300 } PropertyFormatMap[] =
302 { wszRichTextFormat, XATOM_text_rtf },
303 { wszRichTextFormat, XATOM_text_richtext },
304 { wszGIF, XATOM_image_gif },
305 { wszJFIF, XATOM_image_jpeg },
306 { wszPNG, XATOM_image_png },
307 { wszHTMLFormat, XATOM_HTML_Format }, /* prefer this to text/html */
312 * Cached clipboard data.
314 static LPWINE_CLIPDATA ClipData = NULL;
315 static UINT ClipDataCount = 0;
318 * Clipboard sequence number
320 static UINT wSeqNo = 0;
322 /**************************************************************************
323 * Internal Clipboard implementation methods
324 **************************************************************************/
326 static Window thread_selection_wnd(void)
328 struct x11drv_thread_data *thread_data = x11drv_init_thread_data();
329 Window w = thread_data->selection_wnd;
333 XSetWindowAttributes attr;
335 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
336 ButtonPressMask | ButtonReleaseMask | EnterWindowMask | PropertyChangeMask);
339 w = XCreateWindow(thread_data->display, root_window, 0, 0, 1, 1, 0, screen_depth,
340 InputOutput, CopyFromParent, CWEventMask, &attr);
344 thread_data->selection_wnd = w;
346 FIXME("Failed to create window. Fetching selection data will fail.\n");
352 /**************************************************************************
353 * X11DRV_InitClipboard
355 void X11DRV_InitClipboard(void)
358 WINE_CLIPFORMAT *format;
360 /* Register known mapping between window formats and X properties */
361 for (i = 0; i < sizeof(PropertyFormatMap)/sizeof(PropertyFormatMap[0]); i++)
362 X11DRV_CLIPBOARD_InsertClipboardFormat(PropertyFormatMap[i].lpszFormat, GET_ATOM(PropertyFormatMap[i].prop));
364 /* Set up a conversion function from "HTML Format" to "text/html" */
365 format = X11DRV_CLIPBOARD_InsertClipboardFormat(wszHTMLFormat, GET_ATOM(XATOM_text_html));
366 format->lpDrvExportFunc = X11DRV_CLIPBOARD_ExportTextHtml;
370 /**************************************************************************
373 * Intern atoms for formats that don't have one yet.
375 static void intern_atoms(void)
377 LPWINE_CLIPFORMAT format;
383 for (format = ClipFormats, count = 0; format; format = format->NextFormat)
384 if (!format->drvData) count++;
387 display = thread_init_display();
389 names = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*names) );
390 atoms = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*atoms) );
392 for (format = ClipFormats, i = 0; format; format = format->NextFormat) {
393 if (!format->drvData) {
394 len = WideCharToMultiByte(CP_UNIXCP, 0, format->Name, -1, NULL, 0, NULL, NULL);
395 names[i] = HeapAlloc(GetProcessHeap(), 0, len);
396 WideCharToMultiByte(CP_UNIXCP, 0, format->Name, -1, names[i++], len, NULL, NULL);
401 XInternAtoms( display, names, count, False, atoms );
404 for (format = ClipFormats, i = 0; format; format = format->NextFormat) {
405 if (!format->drvData) {
406 HeapFree(GetProcessHeap(), 0, names[i]);
407 format->drvData = atoms[i++];
411 HeapFree( GetProcessHeap(), 0, names );
412 HeapFree( GetProcessHeap(), 0, atoms );
416 /**************************************************************************
419 * Register a custom X clipboard format.
421 static WINE_CLIPFORMAT *register_format( LPCWSTR FormatName, Atom prop )
423 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
425 TRACE("%s\n", debugstr_w(FormatName));
427 /* walk format chain to see if it's already registered */
430 if (CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, lpFormat->Name, -1, FormatName, -1) == CSTR_EQUAL
431 && (lpFormat->wFlags & CF_FLAG_BUILTINFMT) == 0)
433 lpFormat = lpFormat->NextFormat;
436 return X11DRV_CLIPBOARD_InsertClipboardFormat(FormatName, prop);
440 /**************************************************************************
441 * X11DRV_CLIPBOARD_LookupFormat
443 static LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupFormat(WORD wID)
445 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
449 if (lpFormat->wFormatID == wID)
452 lpFormat = lpFormat->NextFormat;
454 if (lpFormat && !lpFormat->drvData) intern_atoms();
459 /**************************************************************************
460 * X11DRV_CLIPBOARD_LookupProperty
462 static LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupProperty(LPWINE_CLIPFORMAT current, UINT drvData)
466 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
467 BOOL need_intern = FALSE;
470 lpFormat = current->NextFormat;
474 if (lpFormat->drvData == drvData) return lpFormat;
475 if (!lpFormat->drvData) need_intern = TRUE;
476 lpFormat = lpFormat->NextFormat;
478 if (!need_intern) return NULL;
480 /* restart the search for the new atoms */
485 /**************************************************************************
486 * X11DRV_CLIPBOARD_LookupData
488 static LPWINE_CLIPDATA X11DRV_CLIPBOARD_LookupData(DWORD wID)
490 LPWINE_CLIPDATA lpData = ClipData;
496 if (lpData->wFormatID == wID)
499 lpData = lpData->NextData;
501 while(lpData != ClipData);
503 if (lpData->wFormatID != wID)
511 /**************************************************************************
512 * InsertClipboardFormat
514 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(LPCWSTR FormatName, Atom prop)
516 LPWINE_CLIPFORMAT lpFormat;
517 LPWINE_CLIPFORMAT lpNewFormat;
520 /* allocate storage for new format entry */
521 lpNewFormat = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPFORMAT));
523 if(lpNewFormat == NULL)
525 WARN("No more memory for a new format!\n");
529 if (!(new_name = HeapAlloc(GetProcessHeap(), 0, (strlenW(FormatName)+1)*sizeof(WCHAR))))
531 WARN("No more memory for the new format name!\n");
532 HeapFree(GetProcessHeap(), 0, lpNewFormat);
536 lpNewFormat->Name = strcpyW(new_name, FormatName);
537 lpNewFormat->wFlags = 0;
538 lpNewFormat->wFormatID = GlobalAddAtomW(lpNewFormat->Name);
539 lpNewFormat->drvData = prop;
540 lpNewFormat->lpDrvImportFunc = X11DRV_CLIPBOARD_ImportClipboardData;
541 lpNewFormat->lpDrvExportFunc = X11DRV_CLIPBOARD_ExportClipboardData;
544 lpFormat = ClipFormats;
546 while(lpFormat->NextFormat) /* Move to last entry */
547 lpFormat = lpFormat->NextFormat;
549 lpNewFormat->NextFormat = NULL;
550 lpFormat->NextFormat = lpNewFormat;
551 lpNewFormat->PrevFormat = lpFormat;
553 TRACE("Registering format(%04x): %s drvData %d\n",
554 lpNewFormat->wFormatID, debugstr_w(FormatName), lpNewFormat->drvData);
562 /**************************************************************************
563 * X11DRV_CLIPBOARD_GetClipboardInfo
565 static BOOL X11DRV_CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
569 SERVER_START_REQ( set_clipboard_info )
573 if (wine_server_call_err( req ))
575 ERR("Failed to get clipboard owner.\n");
579 cbInfo->hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
580 cbInfo->hWndOwner = wine_server_ptr_handle( reply->old_owner );
581 cbInfo->hWndViewer = wine_server_ptr_handle( reply->old_viewer );
582 cbInfo->seqno = reply->seqno;
583 cbInfo->flags = reply->flags;
594 /**************************************************************************
595 * X11DRV_CLIPBOARD_ReleaseOwnership
597 static BOOL X11DRV_CLIPBOARD_ReleaseOwnership(void)
601 SERVER_START_REQ( set_clipboard_info )
603 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
605 if (wine_server_call_err( req ))
607 ERR("Failed to set clipboard.\n");
621 /**************************************************************************
622 * X11DRV_CLIPBOARD_InsertClipboardData
624 * Caller *must* have the clipboard open and be the owner.
626 static BOOL X11DRV_CLIPBOARD_InsertClipboardData(UINT wFormatID, HANDLE hData, DWORD flags,
627 LPWINE_CLIPFORMAT lpFormat, BOOL override)
629 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormatID);
631 TRACE("format=%04x lpData=%p hData=%p flags=0x%08x lpFormat=%p override=%d\n",
632 wFormatID, lpData, hData, flags, lpFormat, override);
634 if (lpData && !override)
639 X11DRV_CLIPBOARD_FreeData(lpData);
641 lpData->hData = hData;
645 lpData = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPDATA));
647 lpData->wFormatID = wFormatID;
648 lpData->hData = hData;
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->hData);
696 if ((lpData->wFormatID == CF_DIB) && lpData->drvData)
697 XFreePixmap(gdi_display, lpData->drvData);
699 else if (lpData->wFormatID == CF_METAFILEPICT)
703 DeleteMetaFile(((METAFILEPICT *)GlobalLock( lpData->hData ))->hMF );
704 GlobalFree(lpData->hData);
707 else if (lpData->wFormatID == CF_ENHMETAFILE)
710 DeleteEnhMetaFile(lpData->hData);
712 else if (lpData->wFormatID < CF_PRIVATEFIRST ||
713 lpData->wFormatID > CF_PRIVATELAST)
716 GlobalFree(lpData->hData);
724 /**************************************************************************
725 * X11DRV_CLIPBOARD_UpdateCache
727 static BOOL X11DRV_CLIPBOARD_UpdateCache(LPCLIPBOARDINFO lpcbinfo)
731 if (!X11DRV_CLIPBOARD_IsSelectionOwner())
733 if (!X11DRV_CLIPBOARD_GetClipboardInfo(lpcbinfo))
735 ERR("Failed to retrieve clipboard information.\n");
738 else if (wSeqNo < lpcbinfo->seqno)
740 X11DRV_EmptyClipboard(TRUE);
742 if (X11DRV_CLIPBOARD_QueryAvailableData(thread_init_display(), lpcbinfo) < 0)
744 ERR("Failed to cache clipboard data owned by another process.\n");
749 X11DRV_EndClipboardUpdate();
752 wSeqNo = lpcbinfo->seqno;
760 /**************************************************************************
761 * X11DRV_CLIPBOARD_RenderFormat
763 static BOOL X11DRV_CLIPBOARD_RenderFormat(Display *display, LPWINE_CLIPDATA lpData)
767 TRACE(" 0x%04x hData(%p)\n", lpData->wFormatID, lpData->hData);
769 if (lpData->hData) return bret; /* Already rendered */
771 if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
772 bret = X11DRV_CLIPBOARD_RenderSynthesizedFormat(display, lpData);
773 else if (!X11DRV_CLIPBOARD_IsSelectionOwner())
775 if (!X11DRV_CLIPBOARD_ReadSelectionData(display, lpData))
777 ERR("Failed to cache clipboard data owned by another process. Format=%04x\n",
784 CLIPBOARDINFO cbInfo;
786 if (X11DRV_CLIPBOARD_GetClipboardInfo(&cbInfo) && cbInfo.hWndOwner)
788 /* Send a WM_RENDERFORMAT message to notify the owner to render the
789 * data requested into the clipboard.
791 TRACE("Sending WM_RENDERFORMAT message to hwnd(%p)\n", cbInfo.hWndOwner);
792 SendMessageW(cbInfo.hWndOwner, WM_RENDERFORMAT, lpData->wFormatID, 0);
794 if (!lpData->hData) bret = FALSE;
798 ERR("hWndClipOwner is lost!\n");
807 /**************************************************************************
808 * CLIPBOARD_ConvertText
809 * Returns number of required/converted characters - not bytes!
811 static INT CLIPBOARD_ConvertText(WORD src_fmt, void const *src, INT src_size,
812 WORD dst_fmt, void *dst, INT dst_size)
816 if(src_fmt == CF_UNICODETEXT)
829 return WideCharToMultiByte(cp, 0, src, src_size, dst, dst_size, NULL, NULL);
832 if(dst_fmt == CF_UNICODETEXT)
845 return MultiByteToWideChar(cp, 0, src, src_size, dst, dst_size);
848 if(!dst_size) return src_size;
850 if(dst_size > src_size) dst_size = src_size;
852 if(src_fmt == CF_TEXT )
853 CharToOemBuffA(src, dst, dst_size);
855 OemToCharBuffA(src, dst, dst_size);
861 /**************************************************************************
862 * X11DRV_CLIPBOARD_RenderSynthesizedFormat
864 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(Display *display, LPWINE_CLIPDATA lpData)
870 if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
872 UINT wFormatID = lpData->wFormatID;
874 if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
875 bret = X11DRV_CLIPBOARD_RenderSynthesizedText(display, wFormatID);
881 bret = X11DRV_CLIPBOARD_RenderSynthesizedDIB( display );
885 bret = X11DRV_CLIPBOARD_RenderSynthesizedBitmap( display );
889 bret = X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile( display );
892 case CF_METAFILEPICT:
893 FIXME("Synthesizing CF_METAFILEPICT not implemented\n");
897 FIXME("Called to synthesize unknown format 0x%08x\n", wFormatID);
902 lpData->wFlags &= ~CF_FLAG_SYNTHESIZED;
909 /**************************************************************************
910 * X11DRV_CLIPBOARD_RenderSynthesizedText
912 * Renders synthesized text
914 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(Display *display, UINT wFormatID)
919 INT src_chars, dst_chars, alloc_size;
920 LPWINE_CLIPDATA lpSource = NULL;
922 TRACE("%04x\n", wFormatID);
924 if ((lpSource = X11DRV_CLIPBOARD_LookupData(wFormatID)) &&
928 /* Look for rendered source or non-synthesized source */
929 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
930 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData))
932 TRACE("UNICODETEXT -> %04x\n", wFormatID);
934 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
935 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData))
937 TRACE("TEXT -> %04x\n", wFormatID);
939 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
940 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData))
942 TRACE("OEMTEXT -> %04x\n", wFormatID);
945 if (!lpSource || (lpSource->wFlags & CF_FLAG_SYNTHESIZED &&
949 /* Ask the clipboard owner to render the source text if necessary */
950 if (!lpSource->hData && !X11DRV_CLIPBOARD_RenderFormat(display, lpSource))
953 lpstrS = GlobalLock(lpSource->hData);
957 /* Text always NULL terminated */
958 if(lpSource->wFormatID == CF_UNICODETEXT)
959 src_chars = strlenW((LPCWSTR)lpstrS) + 1;
961 src_chars = strlen(lpstrS) + 1;
963 /* Calculate number of characters in the destination buffer */
964 dst_chars = CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS,
965 src_chars, wFormatID, NULL, 0);
970 TRACE("Converting from '%04x' to '%04x', %i chars\n",
971 lpSource->wFormatID, wFormatID, src_chars);
973 /* Convert characters to bytes */
974 if(wFormatID == CF_UNICODETEXT)
975 alloc_size = dst_chars * sizeof(WCHAR);
977 alloc_size = dst_chars;
979 hData = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE |
980 GMEM_DDESHARE, alloc_size);
982 lpstrT = GlobalLock(hData);
986 CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS, src_chars,
987 wFormatID, lpstrT, dst_chars);
991 GlobalUnlock(lpSource->hData);
993 return X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, hData, 0, NULL, TRUE);
997 /**************************************************************************
998 * X11DRV_CLIPBOARD_RenderSynthesizedDIB
1000 * Renders synthesized DIB
1002 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedDIB(Display *display)
1005 LPWINE_CLIPDATA lpSource = NULL;
1009 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) && lpSource->hData)
1013 /* If we have a bitmap and it's not synthesized or it has been rendered */
1014 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) &&
1015 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData))
1017 /* Render source if required */
1018 if (lpSource->hData || X11DRV_CLIPBOARD_RenderFormat(display, lpSource))
1024 hData = X11DRV_DIB_CreateDIBFromBitmap(hdc, lpSource->hData);
1025 ReleaseDC(NULL, hdc);
1029 X11DRV_CLIPBOARD_InsertClipboardData(CF_DIB, hData, 0, NULL, TRUE);
1039 /**************************************************************************
1040 * X11DRV_CLIPBOARD_RenderSynthesizedBitmap
1042 * Renders synthesized bitmap
1044 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedBitmap(Display *display)
1047 LPWINE_CLIPDATA lpSource = NULL;
1051 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) && lpSource->hData)
1055 /* If we have a dib and it's not synthesized or it has been rendered */
1056 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) &&
1057 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData))
1059 /* Render source if required */
1060 if (lpSource->hData || X11DRV_CLIPBOARD_RenderFormat(display, lpSource))
1063 HBITMAP hData = NULL;
1064 unsigned int offset;
1065 LPBITMAPINFOHEADER lpbmih;
1068 lpbmih = GlobalLock(lpSource->hData);
1071 offset = sizeof(BITMAPINFOHEADER)
1072 + ((lpbmih->biBitCount <= 8) ? (sizeof(RGBQUAD) *
1073 (1 << lpbmih->biBitCount)) : 0);
1075 hData = CreateDIBitmap(hdc, lpbmih, CBM_INIT, (LPBYTE)lpbmih +
1076 offset, (LPBITMAPINFO) lpbmih, DIB_RGB_COLORS);
1078 GlobalUnlock(lpSource->hData);
1080 ReleaseDC(NULL, hdc);
1084 X11DRV_CLIPBOARD_InsertClipboardData(CF_BITMAP, hData, 0, NULL, TRUE);
1094 /**************************************************************************
1095 * X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile
1097 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile(Display *display)
1099 LPWINE_CLIPDATA lpSource = NULL;
1103 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_ENHMETAFILE)) && lpSource->hData)
1105 /* If we have a MF pict and it's not synthesized or it has been rendered */
1106 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT)) &&
1107 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData))
1109 /* Render source if required */
1110 if (lpSource->hData || X11DRV_CLIPBOARD_RenderFormat(display, lpSource))
1113 HENHMETAFILE hData = NULL;
1115 pmfp = GlobalLock(lpSource->hData);
1118 UINT size_mf_bits = GetMetaFileBitsEx(pmfp->hMF, 0, NULL);
1119 void *mf_bits = HeapAlloc(GetProcessHeap(), 0, size_mf_bits);
1122 GetMetaFileBitsEx(pmfp->hMF, size_mf_bits, mf_bits);
1123 hData = SetWinMetaFileBits(size_mf_bits, mf_bits, NULL, pmfp);
1124 HeapFree(GetProcessHeap(), 0, mf_bits);
1126 GlobalUnlock(lpSource->hData);
1131 X11DRV_CLIPBOARD_InsertClipboardData(CF_ENHMETAFILE, hData, 0, NULL, TRUE);
1141 /**************************************************************************
1142 * X11DRV_CLIPBOARD_ImportXAString
1144 * Import XA_STRING, converting the string to CF_TEXT.
1146 static HANDLE X11DRV_CLIPBOARD_ImportXAString(Display *display, Window w, Atom prop)
1149 unsigned long cbytes;
1151 unsigned long i, inlcount = 0;
1154 if (!X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1157 for (i = 0; i <= cbytes; i++)
1159 if (lpdata[i] == '\n')
1163 if ((hText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cbytes + inlcount + 1)))
1165 lpstr = GlobalLock(hText);
1167 for (i = 0, inlcount = 0; i <= cbytes; i++)
1169 if (lpdata[i] == '\n')
1170 lpstr[inlcount++] = '\r';
1172 lpstr[inlcount++] = lpdata[i];
1175 GlobalUnlock(hText);
1178 /* Free the retrieved property data */
1179 HeapFree(GetProcessHeap(), 0, lpdata);
1185 /**************************************************************************
1186 * X11DRV_CLIPBOARD_ImportUTF8
1188 * Import XA_STRING, converting the string to CF_UNICODE.
1190 static HANDLE X11DRV_CLIPBOARD_ImportUTF8(Display *display, Window w, Atom prop)
1193 unsigned long cbytes;
1195 unsigned long i, inlcount = 0;
1196 HANDLE hUnicodeText = 0;
1198 if (!X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1201 for (i = 0; i <= cbytes; i++)
1203 if (lpdata[i] == '\n')
1207 if ((lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cbytes + inlcount + 1)))
1211 for (i = 0, inlcount = 0; i <= cbytes; i++)
1213 if (lpdata[i] == '\n')
1214 lpstr[inlcount++] = '\r';
1216 lpstr[inlcount++] = lpdata[i];
1219 count = MultiByteToWideChar(CP_UTF8, 0, lpstr, -1, NULL, 0);
1220 hUnicodeText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, count * sizeof(WCHAR));
1224 WCHAR *textW = GlobalLock(hUnicodeText);
1225 MultiByteToWideChar(CP_UTF8, 0, lpstr, -1, textW, count);
1226 GlobalUnlock(hUnicodeText);
1229 HeapFree(GetProcessHeap(), 0, lpstr);
1232 /* Free the retrieved property data */
1233 HeapFree(GetProcessHeap(), 0, lpdata);
1235 return hUnicodeText;
1239 /**************************************************************************
1240 * X11DRV_CLIPBOARD_ImportCompoundText
1242 * Import COMPOUND_TEXT to CF_UNICODE
1244 static HANDLE X11DRV_CLIPBOARD_ImportCompoundText(Display *display, Window w, Atom prop)
1249 int srclen, destlen;
1250 HANDLE hUnicodeText;
1251 XTextProperty txtprop;
1253 if (!X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &txtprop.value, &txtprop.nitems))
1258 txtprop.encoding = x11drv_atom(COMPOUND_TEXT);
1261 ret = XmbTextPropertyToTextList(display, &txtprop, &srcstr, &count);
1262 wine_tsx11_unlock();
1263 HeapFree(GetProcessHeap(), 0, txtprop.value);
1264 if (ret != Success || !count) return 0;
1266 TRACE("Importing %d line(s)\n", count);
1268 /* Compute number of lines */
1269 srclen = strlen(srcstr[0]);
1270 for (i = 0, lcount = 0; i <= srclen; i++)
1272 if (srcstr[0][i] == '\n')
1276 destlen = MultiByteToWideChar(CP_UNIXCP, 0, srcstr[0], -1, NULL, 0);
1278 TRACE("lcount = %d, destlen=%d, srcstr %s\n", lcount, destlen, srcstr[0]);
1280 if ((hUnicodeText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (destlen + lcount + 1) * sizeof(WCHAR))))
1282 WCHAR *deststr = GlobalLock(hUnicodeText);
1283 MultiByteToWideChar(CP_UNIXCP, 0, srcstr[0], -1, deststr, destlen);
1287 for (i = destlen - 1, j = destlen + lcount - 1; i >= 0; i--, j--)
1289 deststr[j] = deststr[i];
1291 if (deststr[i] == '\n')
1292 deststr[--j] = '\r';
1296 GlobalUnlock(hUnicodeText);
1300 XFreeStringList(srcstr);
1301 wine_tsx11_unlock();
1303 return hUnicodeText;
1307 /**************************************************************************
1308 * X11DRV_CLIPBOARD_ImportXAPIXMAP
1310 * Import XA_PIXMAP, converting the image to CF_DIB.
1312 static HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(Display *display, Window w, Atom prop)
1317 unsigned long cbytes;
1319 HANDLE hClipData = 0;
1321 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1323 pPixmap = (Pixmap *) lpdata;
1325 hwnd = GetOpenClipboardWindow();
1328 hClipData = X11DRV_DIB_CreateDIBFromPixmap(*pPixmap, hdc);
1329 ReleaseDC(hwnd, hdc);
1331 /* Free the retrieved property data */
1332 HeapFree(GetProcessHeap(), 0, lpdata);
1339 /**************************************************************************
1340 * X11DRV_CLIPBOARD_ImportImageBmp
1342 * Import image/bmp, converting the image to CF_DIB.
1344 static HANDLE X11DRV_CLIPBOARD_ImportImageBmp(Display *display, Window w, Atom prop)
1347 unsigned long cbytes;
1348 HANDLE hClipData = 0;
1350 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1352 BITMAPFILEHEADER *bfh = (BITMAPFILEHEADER*)lpdata;
1354 if (cbytes >= sizeof(BITMAPFILEHEADER)+sizeof(BITMAPCOREHEADER) &&
1355 bfh->bfType == 0x4d42 /* "BM" */)
1357 BITMAPINFO *bmi = (BITMAPINFO*)(bfh+1);
1362 hbmp = CreateDIBitmap(
1366 lpdata+bfh->bfOffBits,
1371 hClipData = X11DRV_DIB_CreateDIBFromBitmap(hdc, hbmp);
1377 /* Free the retrieved property data */
1378 HeapFree(GetProcessHeap(), 0, lpdata);
1385 /**************************************************************************
1386 * X11DRV_CLIPBOARD_ImportMetaFilePict
1388 * Import MetaFilePict.
1390 static HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(Display *display, Window w, Atom prop)
1393 unsigned long cbytes;
1394 HANDLE hClipData = 0;
1396 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1399 hClipData = X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, lpdata, (LPDWORD)&cbytes, FALSE);
1401 /* Free the retrieved property data */
1402 HeapFree(GetProcessHeap(), 0, lpdata);
1409 /**************************************************************************
1410 * X11DRV_ImportEnhMetaFile
1412 * Import EnhMetaFile.
1414 static HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(Display *display, Window w, Atom prop)
1417 unsigned long cbytes;
1418 HANDLE hClipData = 0;
1420 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1423 hClipData = X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, lpdata, (LPDWORD)&cbytes, FALSE);
1425 /* Free the retrieved property data */
1426 HeapFree(GetProcessHeap(), 0, lpdata);
1433 /**************************************************************************
1434 * X11DRV_ImportClipbordaData
1436 * Generic import clipboard data routine.
1438 static HANDLE X11DRV_CLIPBOARD_ImportClipboardData(Display *display, Window w, Atom prop)
1442 unsigned long cbytes;
1443 HANDLE hClipData = 0;
1445 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1449 /* Turn on the DDESHARE flag to enable shared 32 bit memory */
1450 hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cbytes);
1454 if ((lpClipData = GlobalLock(hClipData)))
1456 memcpy(lpClipData, lpdata, cbytes);
1457 GlobalUnlock(hClipData);
1461 GlobalFree(hClipData);
1466 /* Free the retrieved property data */
1467 HeapFree(GetProcessHeap(), 0, lpdata);
1474 /**************************************************************************
1475 X11DRV_CLIPBOARD_ExportClipboardData
1477 * Generic export clipboard data routine.
1479 static HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Display *display, Window requestor, Atom aTarget,
1480 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1484 HANDLE hClipData = 0;
1486 *lpBytes = 0; /* Assume failure */
1488 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpData))
1489 ERR("Failed to export %04x format\n", lpData->wFormatID);
1492 datasize = GlobalSize(lpData->hData);
1494 hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, datasize);
1495 if (hClipData == 0) return NULL;
1497 if ((lpClipData = GlobalLock(hClipData)))
1499 LPVOID lpdata = GlobalLock(lpData->hData);
1501 memcpy(lpClipData, lpdata, datasize);
1502 *lpBytes = datasize;
1504 GlobalUnlock(lpData->hData);
1505 GlobalUnlock(hClipData);
1507 GlobalFree(hClipData);
1516 /**************************************************************************
1517 * X11DRV_CLIPBOARD_ExportXAString
1519 * Export CF_TEXT converting the string to XA_STRING.
1520 * Helper function for X11DRV_CLIPBOARD_ExportString.
1522 static HANDLE X11DRV_CLIPBOARD_ExportXAString(LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1526 LPSTR text, lpstr = NULL;
1528 *lpBytes = 0; /* Assume return has zero bytes */
1530 text = GlobalLock(lpData->hData);
1531 size = strlen(text);
1533 /* remove carriage returns */
1534 lpstr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size + 1);
1538 for (i = 0,j = 0; i < size && text[i]; i++)
1540 if (text[i] == '\r' && (text[i+1] == '\n' || text[i+1] == '\0'))
1542 lpstr[j++] = text[i];
1546 *lpBytes = j; /* Number of bytes in string */
1549 GlobalUnlock(lpData->hData);
1555 /**************************************************************************
1556 * X11DRV_CLIPBOARD_ExportUTF8String
1558 * Export CF_UNICODE converting the string to UTF8.
1559 * Helper function for X11DRV_CLIPBOARD_ExportString.
1561 static HANDLE X11DRV_CLIPBOARD_ExportUTF8String(LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1566 LPSTR text, lpstr = NULL;
1568 *lpBytes = 0; /* Assume return has zero bytes */
1570 uni_text = GlobalLock(lpData->hData);
1572 size = WideCharToMultiByte(CP_UTF8, 0, uni_text, -1, NULL, 0, NULL, NULL);
1574 text = HeapAlloc(GetProcessHeap(), 0, size);
1577 WideCharToMultiByte(CP_UTF8, 0, uni_text, -1, text, size, NULL, NULL);
1579 /* remove carriage returns */
1580 lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size--);
1584 for (i = 0,j = 0; i < size && text[i]; i++)
1586 if (text[i] == '\r' && (text[i+1] == '\n' || text[i+1] == '\0'))
1588 lpstr[j++] = text[i];
1592 *lpBytes = j; /* Number of bytes in string */
1595 HeapFree(GetProcessHeap(), 0, text);
1596 GlobalUnlock(lpData->hData);
1603 /**************************************************************************
1604 * X11DRV_CLIPBOARD_ExportCompoundText
1606 * Export CF_UNICODE to COMPOUND_TEXT
1607 * Helper function for X11DRV_CLIPBOARD_ExportString.
1609 static HANDLE X11DRV_CLIPBOARD_ExportCompoundText(Display *display, Window requestor, Atom aTarget, Atom rprop,
1610 LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1614 XICCEncodingStyle style;
1619 uni_text = GlobalLock(lpData->hData);
1621 size = WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, NULL, 0, NULL, NULL);
1622 lpstr = HeapAlloc(GetProcessHeap(), 0, size);
1626 WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, lpstr, size, NULL, NULL);
1628 /* remove carriage returns */
1629 for (i = 0, j = 0; i < size && lpstr[i]; i++)
1631 if (lpstr[i] == '\r' && (lpstr[i+1] == '\n' || lpstr[i+1] == '\0'))
1633 lpstr[j++] = lpstr[i];
1637 GlobalUnlock(lpData->hData);
1639 if (aTarget == x11drv_atom(COMPOUND_TEXT))
1640 style = XCompoundTextStyle;
1642 style = XStdICCTextStyle;
1644 /* Update the X property */
1646 if (XmbTextListToTextProperty(display, &lpstr, 1, style, &prop) == Success)
1648 XSetTextProperty(display, requestor, &prop, rprop);
1651 wine_tsx11_unlock();
1653 HeapFree(GetProcessHeap(), 0, lpstr);
1658 /**************************************************************************
1659 * X11DRV_CLIPBOARD_ExportString
1663 static HANDLE X11DRV_CLIPBOARD_ExportString(Display *display, Window requestor, Atom aTarget, Atom rprop,
1664 LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1666 if (X11DRV_CLIPBOARD_RenderFormat(display, lpData))
1668 if (aTarget == XA_STRING)
1669 return X11DRV_CLIPBOARD_ExportXAString(lpData, lpBytes);
1670 else if (aTarget == x11drv_atom(COMPOUND_TEXT) || aTarget == x11drv_atom(TEXT))
1671 return X11DRV_CLIPBOARD_ExportCompoundText(display, requestor, aTarget,
1672 rprop, lpData, lpBytes);
1675 TRACE("Exporting target %ld to default UTF8_STRING\n", aTarget);
1676 return X11DRV_CLIPBOARD_ExportUTF8String(lpData, lpBytes);
1680 ERR("Failed to render %04x format\n", lpData->wFormatID);
1686 /**************************************************************************
1687 * X11DRV_CLIPBOARD_ExportXAPIXMAP
1689 * Export CF_DIB to XA_PIXMAP.
1691 static HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Display *display, Window requestor, Atom aTarget, Atom rprop,
1692 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1696 unsigned char* lpData;
1698 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1700 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1704 if (!lpdata->drvData) /* If not already rendered */
1706 /* For convert from packed DIB to Pixmap */
1708 lpdata->drvData = (UINT) X11DRV_DIB_CreatePixmapFromDIB(lpdata->hData, hdc);
1712 *lpBytes = sizeof(Pixmap); /* pixmap is a 32bit value */
1714 /* Wrap pixmap so we can return a handle */
1715 hData = GlobalAlloc(0, *lpBytes);
1716 lpData = GlobalLock(hData);
1717 memcpy(lpData, &lpdata->drvData, *lpBytes);
1718 GlobalUnlock(hData);
1724 /**************************************************************************
1725 * X11DRV_CLIPBOARD_ExportImageBmp
1727 * Export CF_DIB to image/bmp.
1729 static HANDLE X11DRV_CLIPBOARD_ExportImageBmp(Display *display, Window requestor, Atom aTarget, Atom rprop,
1730 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1737 BITMAPFILEHEADER *bfh;
1741 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1743 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1747 hpackeddib = lpdata->hData;
1749 dibdata = GlobalLock(hpackeddib);
1752 ERR("Failed to lock packed DIB\n");
1756 bmpsize = sizeof(BITMAPFILEHEADER) + GlobalSize(hpackeddib);
1758 hbmpdata = GlobalAlloc(0, bmpsize);
1762 bmpdata = GlobalLock(hbmpdata);
1766 GlobalFree(hbmpdata);
1767 GlobalUnlock(hpackeddib);
1771 /* bitmap file header */
1772 bfh = (BITMAPFILEHEADER*)bmpdata;
1773 bfh->bfType = 0x4d42; /* "BM" */
1774 bfh->bfSize = bmpsize;
1775 bfh->bfReserved1 = 0;
1776 bfh->bfReserved2 = 0;
1777 bfh->bfOffBits = sizeof(BITMAPFILEHEADER) + bitmap_info_size((BITMAPINFO*)dibdata, DIB_RGB_COLORS);
1779 /* rest of bitmap is the same as the packed dib */
1780 memcpy(bfh+1, dibdata, bmpsize-sizeof(BITMAPFILEHEADER));
1784 GlobalUnlock(hbmpdata);
1787 GlobalUnlock(hpackeddib);
1793 /**************************************************************************
1794 * X11DRV_CLIPBOARD_ExportMetaFilePict
1796 * Export MetaFilePict.
1798 static HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Display *display, Window requestor, Atom aTarget, Atom rprop,
1799 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1801 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1803 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1807 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, lpdata->hData, lpBytes, TRUE);
1811 /**************************************************************************
1812 * X11DRV_CLIPBOARD_ExportEnhMetaFile
1814 * Export EnhMetaFile.
1816 static HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Display *display, Window requestor, Atom aTarget, Atom rprop,
1817 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1819 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1821 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1825 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, lpdata->hData, lpBytes, TRUE);
1829 /**************************************************************************
1830 * get_html_description_field
1832 * Find the value of a field in an HTML Format description.
1834 static LPCSTR get_html_description_field(LPCSTR data, LPCSTR keyword)
1838 while (pos && *pos && *pos != '<')
1840 if (memcmp(pos, keyword, strlen(keyword)) == 0)
1841 return pos+strlen(keyword);
1843 pos = strchr(pos, '\n');
1851 /**************************************************************************
1852 * X11DRV_CLIPBOARD_ExportTextHtml
1854 * Export HTML Format to text/html.
1856 * FIXME: We should attempt to add an <a base> tag and convert windows paths.
1858 static HANDLE X11DRV_CLIPBOARD_ExportTextHtml(Display *display, Window requestor, Atom aTarget,
1859 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1862 LPCSTR data, field_value;
1863 UINT fragmentstart, fragmentend, htmlsize;
1864 HANDLE hhtmldata=NULL;
1869 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1871 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1875 hdata = lpdata->hData;
1877 data = GlobalLock(hdata);
1880 ERR("Failed to lock HTML Format data\n");
1884 /* read the important fields */
1885 field_value = get_html_description_field(data, "StartFragment:");
1888 ERR("Couldn't find StartFragment value\n");
1891 fragmentstart = atoi(field_value);
1893 field_value = get_html_description_field(data, "EndFragment:");
1896 ERR("Couldn't find EndFragment value\n");
1899 fragmentend = atoi(field_value);
1901 /* export only the fragment */
1902 htmlsize = fragmentend - fragmentstart + 1;
1904 hhtmldata = GlobalAlloc(0, htmlsize);
1908 htmldata = GlobalLock(hhtmldata);
1912 GlobalFree(hhtmldata);
1917 memcpy(htmldata, &data[fragmentstart], fragmentend-fragmentstart);
1918 htmldata[htmlsize-1] = '\0';
1920 *lpBytes = htmlsize;
1922 GlobalUnlock(htmldata);
1927 GlobalUnlock(hdata);
1933 /**************************************************************************
1934 * X11DRV_CLIPBOARD_QueryTargets
1936 static BOOL X11DRV_CLIPBOARD_QueryTargets(Display *display, Window w, Atom selection,
1937 Atom target, XEvent *xe)
1943 XConvertSelection(display, selection, target,
1944 x11drv_atom(SELECTION_DATA), w, CurrentTime);
1945 wine_tsx11_unlock();
1948 * Wait until SelectionNotify is received
1950 for (i = 0; i < SELECTION_RETRIES; i++)
1953 res = XCheckTypedWindowEvent(display, w, SelectionNotify, xe);
1954 wine_tsx11_unlock();
1955 if (res && xe->xselection.selection == selection) break;
1957 usleep(SELECTION_WAIT);
1960 if (i == SELECTION_RETRIES)
1962 ERR("Timed out waiting for SelectionNotify event\n");
1965 /* Verify that the selection returned a valid TARGETS property */
1966 if ((xe->xselection.target != target) || (xe->xselection.property == None))
1968 /* Selection owner failed to respond or we missed the SelectionNotify */
1969 WARN("Failed to retrieve TARGETS for selection %ld.\n", selection);
1977 static int is_atom_error( Display *display, XErrorEvent *event, void *arg )
1979 return (event->error_code == BadAtom);
1982 /**************************************************************************
1983 * X11DRV_CLIPBOARD_InsertSelectionProperties
1985 * Mark properties available for future retrieval.
1987 static VOID X11DRV_CLIPBOARD_InsertSelectionProperties(Display *display, Atom* properties, UINT count)
1989 UINT i, nb_atoms = 0;
1992 /* Cache these formats in the clipboard cache */
1993 for (i = 0; i < count; i++)
1995 LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupProperty(NULL, properties[i]);
1999 /* We found at least one Window's format that mapps to the property.
2000 * Continue looking for more.
2002 * If more than one property map to a Window's format then we use the first
2003 * one and ignore the rest.
2007 TRACE("Atom#%d Property(%d): --> FormatID(%04x) %s\n",
2008 i, lpFormat->drvData, lpFormat->wFormatID, debugstr_w(lpFormat->Name));
2009 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, lpFormat, FALSE);
2010 lpFormat = X11DRV_CLIPBOARD_LookupProperty(lpFormat, properties[i]);
2013 else if (properties[i])
2015 /* add it to the list of atoms that we don't know about yet */
2016 if (!atoms) atoms = HeapAlloc( GetProcessHeap(), 0,
2017 (count - i) * sizeof(*atoms) );
2018 if (atoms) atoms[nb_atoms++] = properties[i];
2022 /* query all unknown atoms in one go */
2025 char **names = HeapAlloc( GetProcessHeap(), 0, nb_atoms * sizeof(*names) );
2028 X11DRV_expect_error( display, is_atom_error, NULL );
2029 if (!XGetAtomNames( display, atoms, nb_atoms, names )) nb_atoms = 0;
2030 if (X11DRV_check_error())
2032 WARN( "got some bad atoms, ignoring\n" );
2035 for (i = 0; i < nb_atoms; i++)
2037 WINE_CLIPFORMAT *lpFormat;
2039 int len = MultiByteToWideChar(CP_UNIXCP, 0, names[i], -1, NULL, 0);
2040 wname = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
2041 MultiByteToWideChar(CP_UNIXCP, 0, names[i], -1, wname, len);
2043 lpFormat = register_format( wname, atoms[i] );
2044 HeapFree(GetProcessHeap(), 0, wname);
2047 ERR("Failed to register %s property. Type will not be cached.\n", names[i]);
2050 TRACE("Atom#%d Property(%d): --> FormatID(%04x) %s\n",
2051 i, lpFormat->drvData, lpFormat->wFormatID, debugstr_w(lpFormat->Name));
2052 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, lpFormat, FALSE);
2055 for (i = 0; i < nb_atoms; i++) XFree( names[i] );
2056 wine_tsx11_unlock();
2057 HeapFree( GetProcessHeap(), 0, names );
2059 HeapFree( GetProcessHeap(), 0, atoms );
2064 /**************************************************************************
2065 * X11DRV_CLIPBOARD_QueryAvailableData
2067 * Caches the list of data formats available from the current selection.
2068 * This queries the selection owner for the TARGETS property and saves all
2069 * reported property types.
2071 static int X11DRV_CLIPBOARD_QueryAvailableData(Display *display, LPCLIPBOARDINFO lpcbinfo)
2074 Atom atype=AnyPropertyType;
2076 unsigned long remain;
2077 Atom* targetList=NULL;
2079 unsigned long cSelectionTargets = 0;
2081 if (selectionAcquired & (S_PRIMARY | S_CLIPBOARD))
2083 ERR("Received request to cache selection but process is owner=(%08x)\n",
2084 (unsigned) selectionWindow);
2085 return -1; /* Prevent self request */
2088 w = thread_selection_wnd();
2091 ERR("No window available to retrieve selection!\n");
2096 * Query the selection owner for the TARGETS property
2099 if ((use_primary_selection && XGetSelectionOwner(display,XA_PRIMARY)) ||
2100 XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
2102 wine_tsx11_unlock();
2103 if (use_primary_selection && (X11DRV_CLIPBOARD_QueryTargets(display, w, XA_PRIMARY, x11drv_atom(TARGETS), &xe)))
2104 selectionCacheSrc = XA_PRIMARY;
2105 else if (X11DRV_CLIPBOARD_QueryTargets(display, w, x11drv_atom(CLIPBOARD), x11drv_atom(TARGETS), &xe))
2106 selectionCacheSrc = x11drv_atom(CLIPBOARD);
2109 Atom xstr = XA_STRING;
2111 /* Selection Owner doesn't understand TARGETS, try retrieving XA_STRING */
2112 if (X11DRV_CLIPBOARD_QueryTargets(display, w, XA_PRIMARY, XA_STRING, &xe))
2114 X11DRV_CLIPBOARD_InsertSelectionProperties(display, &xstr, 1);
2115 selectionCacheSrc = XA_PRIMARY;
2118 else if (X11DRV_CLIPBOARD_QueryTargets(display, w, x11drv_atom(CLIPBOARD), XA_STRING, &xe))
2120 X11DRV_CLIPBOARD_InsertSelectionProperties(display, &xstr, 1);
2121 selectionCacheSrc = x11drv_atom(CLIPBOARD);
2126 WARN("Failed to query selection owner for available data.\n");
2131 else /* No selection owner so report 0 targets available */
2133 wine_tsx11_unlock();
2137 /* Read the TARGETS property contents */
2139 if(XGetWindowProperty(display, xe.xselection.requestor, xe.xselection.property,
2140 0, 0x3FFF, True, AnyPropertyType/*XA_ATOM*/, &atype, &aformat, &cSelectionTargets,
2141 &remain, (unsigned char**)&targetList) != Success)
2143 wine_tsx11_unlock();
2144 WARN("Failed to read TARGETS property\n");
2148 wine_tsx11_unlock();
2149 TRACE("Type %lx,Format %d,nItems %ld, Remain %ld\n",
2150 atype, aformat, cSelectionTargets, remain);
2152 * The TARGETS property should have returned us a list of atoms
2153 * corresponding to each selection target format supported.
2155 if (atype == XA_ATOM || atype == x11drv_atom(TARGETS))
2159 X11DRV_CLIPBOARD_InsertSelectionProperties(display, targetList, cSelectionTargets);
2161 else if (aformat == 8) /* work around quartz-wm brain damage */
2163 unsigned long i, count = cSelectionTargets / sizeof(CARD32);
2164 Atom *atoms = HeapAlloc( GetProcessHeap(), 0, count * sizeof(Atom) );
2165 for (i = 0; i < count; i++)
2166 atoms[i] = ((CARD32 *)targetList)[i]; /* FIXME: byte swapping */
2167 X11DRV_CLIPBOARD_InsertSelectionProperties( display, atoms, count );
2168 HeapFree( GetProcessHeap(), 0, atoms );
2172 /* Free the list of targets */
2175 wine_tsx11_unlock();
2178 return cSelectionTargets;
2182 /**************************************************************************
2183 * X11DRV_CLIPBOARD_ReadSelectionData
2185 * This method is invoked only when we DO NOT own the X selection
2187 * We always get the data from the selection client each time,
2188 * since we have no way of determining if the data in our cache is stale.
2190 static BOOL X11DRV_CLIPBOARD_ReadSelectionData(Display *display, LPWINE_CLIPDATA lpData)
2197 TRACE("%04x\n", lpData->wFormatID);
2199 if (!lpData->lpFormat)
2201 ERR("Requesting format %04x but no source format linked to data.\n",
2206 if (!selectionAcquired)
2208 Window w = thread_selection_wnd();
2211 ERR("No window available to read selection data!\n");
2215 TRACE("Requesting conversion of %s property (%d) from selection type %08x\n",
2216 debugstr_w(lpData->lpFormat->Name), lpData->lpFormat->drvData, (UINT)selectionCacheSrc);
2219 XConvertSelection(display, selectionCacheSrc, lpData->lpFormat->drvData,
2220 x11drv_atom(SELECTION_DATA), w, CurrentTime);
2221 wine_tsx11_unlock();
2223 /* wait until SelectionNotify is received */
2224 for (i = 0; i < SELECTION_RETRIES; i++)
2227 res = XCheckTypedWindowEvent(display, w, SelectionNotify, &xe);
2228 wine_tsx11_unlock();
2229 if (res && xe.xselection.selection == selectionCacheSrc) break;
2231 usleep(SELECTION_WAIT);
2234 if (i == SELECTION_RETRIES)
2236 ERR("Timed out waiting for SelectionNotify event\n");
2238 /* Verify that the selection returned a valid TARGETS property */
2239 else if (xe.xselection.property != None)
2242 * Read the contents of the X selection property
2243 * into WINE's clipboard cache and converting the
2244 * data format if necessary.
2246 HANDLE hData = lpData->lpFormat->lpDrvImportFunc(display, xe.xselection.requestor,
2247 xe.xselection.property);
2250 bRet = X11DRV_CLIPBOARD_InsertClipboardData(lpData->wFormatID, hData, 0, lpData->lpFormat, TRUE);
2252 TRACE("Import function failed\n");
2256 TRACE("Failed to convert selection\n");
2261 ERR("Received request to cache selection data but process is owner\n");
2264 TRACE("Returning %d\n", bRet);
2270 /**************************************************************************
2271 * X11DRV_CLIPBOARD_GetProperty
2272 * Gets type, data and size.
2274 static BOOL X11DRV_CLIPBOARD_GetProperty(Display *display, Window w, Atom prop,
2275 Atom *atype, unsigned char** data, unsigned long* datasize)
2278 unsigned long pos = 0, nitems, remain, count;
2279 unsigned char *val = NULL, *buffer;
2281 TRACE("Reading property %lu from X window %lx\n", prop, w);
2286 if (XGetWindowProperty(display, w, prop, pos, INT_MAX / 4, False,
2287 AnyPropertyType, atype, &aformat, &nitems, &remain, &buffer) != Success)
2289 wine_tsx11_unlock();
2290 WARN("Failed to read property\n");
2291 HeapFree( GetProcessHeap(), 0, val );
2295 count = get_property_size( aformat, nitems );
2296 if (!val) *data = HeapAlloc( GetProcessHeap(), 0, pos * sizeof(int) + count + 1 );
2297 else *data = HeapReAlloc( GetProcessHeap(), 0, val, pos * sizeof(int) + count + 1 );
2302 wine_tsx11_unlock();
2303 HeapFree( GetProcessHeap(), 0, val );
2307 memcpy( (int *)val + pos, buffer, count );
2309 wine_tsx11_unlock();
2312 *datasize = pos * sizeof(int) + count;
2316 pos += count / sizeof(int);
2319 /* Delete the property on the window now that we are done
2320 * This will send a PropertyNotify event to the selection owner. */
2322 XDeleteProperty(display, w, prop);
2323 wine_tsx11_unlock();
2328 /**************************************************************************
2329 * X11DRV_CLIPBOARD_ReadProperty
2330 * Reads the contents of the X selection property.
2332 static BOOL X11DRV_CLIPBOARD_ReadProperty(Display *display, Window w, Atom prop,
2333 unsigned char** data, unsigned long* datasize)
2341 if (!X11DRV_CLIPBOARD_GetProperty(display, w, prop, &atype, data, datasize))
2345 while (XCheckTypedWindowEvent(display, w, PropertyNotify, &xe))
2347 wine_tsx11_unlock();
2349 if (atype == x11drv_atom(INCR))
2351 unsigned char *buf = *data;
2352 unsigned long bufsize = 0;
2357 unsigned char *prop_data, *tmp;
2358 unsigned long prop_size;
2360 /* Wait until PropertyNotify is received */
2361 for (i = 0; i < SELECTION_RETRIES; i++)
2366 res = XCheckTypedWindowEvent(display, w, PropertyNotify, &xe);
2367 wine_tsx11_unlock();
2368 if (res && xe.xproperty.atom == prop &&
2369 xe.xproperty.state == PropertyNewValue)
2371 usleep(SELECTION_WAIT);
2374 if (i >= SELECTION_RETRIES ||
2375 !X11DRV_CLIPBOARD_GetProperty(display, w, prop, &atype, &prop_data, &prop_size))
2377 HeapFree(GetProcessHeap(), 0, buf);
2381 /* Retrieved entire data. */
2384 HeapFree(GetProcessHeap(), 0, prop_data);
2386 *datasize = bufsize;
2390 tmp = HeapReAlloc(GetProcessHeap(), 0, buf, bufsize + prop_size + 1);
2393 HeapFree(GetProcessHeap(), 0, buf);
2398 memcpy(buf + bufsize, prop_data, prop_size + 1);
2399 bufsize += prop_size;
2400 HeapFree(GetProcessHeap(), 0, prop_data);
2408 /**************************************************************************
2409 * CLIPBOARD_SerializeMetafile
2411 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out)
2415 TRACE(" wFormat=%d hdata=%p out=%d\n", wformat, hdata, out);
2417 if (out) /* Serialize out, caller should free memory */
2419 *lpcbytes = 0; /* Assume failure */
2421 if (wformat == CF_METAFILEPICT)
2423 LPMETAFILEPICT lpmfp = GlobalLock(hdata);
2424 unsigned int size = GetMetaFileBitsEx(lpmfp->hMF, 0, NULL);
2426 h = GlobalAlloc(0, size + sizeof(METAFILEPICT));
2429 char *pdata = GlobalLock(h);
2431 memcpy(pdata, lpmfp, sizeof(METAFILEPICT));
2432 GetMetaFileBitsEx(lpmfp->hMF, size, pdata + sizeof(METAFILEPICT));
2434 *lpcbytes = size + sizeof(METAFILEPICT);
2439 GlobalUnlock(hdata);
2441 else if (wformat == CF_ENHMETAFILE)
2443 int size = GetEnhMetaFileBits(hdata, 0, NULL);
2445 h = GlobalAlloc(0, size);
2448 LPVOID pdata = GlobalLock(h);
2450 GetEnhMetaFileBits(hdata, size, pdata);
2459 if (wformat == CF_METAFILEPICT)
2461 h = GlobalAlloc(0, sizeof(METAFILEPICT));
2464 unsigned int wiresize;
2465 LPMETAFILEPICT lpmfp = GlobalLock(h);
2467 memcpy(lpmfp, hdata, sizeof(METAFILEPICT));
2468 wiresize = *lpcbytes - sizeof(METAFILEPICT);
2469 lpmfp->hMF = SetMetaFileBitsEx(wiresize,
2470 ((const BYTE *)hdata) + sizeof(METAFILEPICT));
2474 else if (wformat == CF_ENHMETAFILE)
2476 h = SetEnhMetaFileBits(*lpcbytes, hdata);
2484 /**************************************************************************
2485 * X11DRV_CLIPBOARD_ReleaseSelection
2487 * Release XA_CLIPBOARD and XA_PRIMARY in response to a SelectionClear event.
2489 static void X11DRV_CLIPBOARD_ReleaseSelection(Display *display, Atom selType, Window w, HWND hwnd, Time time)
2491 /* w is the window that lost the selection
2493 TRACE("event->window = %08x (selectionWindow = %08x) selectionAcquired=0x%08x\n",
2494 (unsigned)w, (unsigned)selectionWindow, (unsigned)selectionAcquired);
2496 if (selectionAcquired && (w == selectionWindow))
2498 CLIPBOARDINFO cbinfo;
2500 /* completely give up the selection */
2501 TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
2503 X11DRV_CLIPBOARD_GetClipboardInfo(&cbinfo);
2505 if (cbinfo.flags & CB_PROCESS)
2507 /* Since we're still the owner, this wasn't initiated by
2508 another Wine process */
2509 if (OpenClipboard(hwnd))
2511 /* Destroy private objects */
2512 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
2514 /* Give up ownership of the windows clipboard */
2515 X11DRV_CLIPBOARD_ReleaseOwnership();
2520 if ((selType == x11drv_atom(CLIPBOARD)) && (selectionAcquired & S_PRIMARY))
2522 TRACE("Lost clipboard. Check if we need to release PRIMARY\n");
2525 if (selectionWindow == XGetSelectionOwner(display, XA_PRIMARY))
2527 TRACE("We still own PRIMARY. Releasing PRIMARY.\n");
2528 XSetSelectionOwner(display, XA_PRIMARY, None, time);
2531 TRACE("We no longer own PRIMARY\n");
2532 wine_tsx11_unlock();
2534 else if ((selType == XA_PRIMARY) && (selectionAcquired & S_CLIPBOARD))
2536 TRACE("Lost PRIMARY. Check if we need to release CLIPBOARD\n");
2539 if (selectionWindow == XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
2541 TRACE("We still own CLIPBOARD. Releasing CLIPBOARD.\n");
2542 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), None, time);
2545 TRACE("We no longer own CLIPBOARD\n");
2546 wine_tsx11_unlock();
2549 selectionWindow = None;
2551 X11DRV_EmptyClipboard(FALSE);
2553 /* Reset the selection flags now that we are done */
2554 selectionAcquired = S_NOSELECTION;
2559 /**************************************************************************
2560 * IsSelectionOwner (X11DRV.@)
2562 * Returns: TRUE if the selection is owned by this process, FALSE otherwise
2564 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void)
2566 return selectionAcquired;
2570 /**************************************************************************
2571 * X11DRV Clipboard Exports
2572 **************************************************************************/
2575 /**************************************************************************
2576 * RegisterClipboardFormat (X11DRV.@)
2578 * Registers a custom X clipboard format
2579 * Returns: Format id or 0 on failure
2581 UINT CDECL X11DRV_RegisterClipboardFormat(LPCWSTR FormatName)
2583 LPWINE_CLIPFORMAT lpFormat;
2585 if (FormatName == NULL) return 0;
2586 if (!(lpFormat = register_format( FormatName, 0 ))) return 0;
2587 return lpFormat->wFormatID;
2591 /**************************************************************************
2592 * X11DRV_GetClipboardFormatName
2594 INT CDECL X11DRV_GetClipboardFormatName(UINT wFormat, LPWSTR retStr, INT maxlen)
2596 LPWINE_CLIPFORMAT lpFormat;
2598 TRACE("(%04X, %p, %d) !\n", wFormat, retStr, maxlen);
2600 if (wFormat < 0xc000)
2602 SetLastError(ERROR_INVALID_PARAMETER);
2606 lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
2608 if (!lpFormat || (lpFormat->wFlags & CF_FLAG_BUILTINFMT))
2610 TRACE("Unknown format 0x%08x!\n", wFormat);
2611 SetLastError(ERROR_INVALID_HANDLE);
2615 lstrcpynW(retStr, lpFormat->Name, maxlen);
2617 return strlenW(retStr);
2620 static void selection_acquire(void)
2625 owner = thread_selection_wnd();
2626 display = thread_display();
2630 selectionAcquired = 0;
2631 selectionWindow = 0;
2633 /* Grab PRIMARY selection if not owned */
2634 if (use_primary_selection)
2635 XSetSelectionOwner(display, XA_PRIMARY, owner, CurrentTime);
2637 /* Grab CLIPBOARD selection if not owned */
2638 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), owner, CurrentTime);
2640 if (use_primary_selection && XGetSelectionOwner(display, XA_PRIMARY) == owner)
2641 selectionAcquired |= S_PRIMARY;
2643 if (XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)) == owner)
2644 selectionAcquired |= S_CLIPBOARD;
2646 wine_tsx11_unlock();
2648 if (selectionAcquired)
2650 selectionWindow = owner;
2651 TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner);
2655 static DWORD WINAPI selection_thread_proc(LPVOID p)
2661 selection_acquire();
2664 while (selectionAcquired)
2666 MsgWaitForMultipleObjectsEx(0, NULL, INFINITE, QS_SENDMESSAGE, 0);
2672 /**************************************************************************
2673 * AcquireClipboard (X11DRV.@)
2675 int CDECL X11DRV_AcquireClipboard(HWND hWndClipWindow)
2678 HANDLE selectionThread;
2680 TRACE(" %p\n", hWndClipWindow);
2683 * It's important that the selection get acquired from the thread
2684 * that owns the clipboard window. The primary reason is that we know
2685 * it is running a message loop and therefore can process the
2686 * X selection events.
2688 if (hWndClipWindow &&
2689 GetCurrentThreadId() != GetWindowThreadProcessId(hWndClipWindow, &procid))
2691 if (procid != GetCurrentProcessId())
2693 WARN("Setting clipboard owner to other process is not supported\n");
2694 hWndClipWindow = NULL;
2698 TRACE("Thread %x is acquiring selection with thread %x's window %p\n",
2699 GetCurrentThreadId(),
2700 GetWindowThreadProcessId(hWndClipWindow, NULL), hWndClipWindow);
2702 return SendMessageW(hWndClipWindow, WM_X11DRV_ACQUIRE_SELECTION, 0, 0);
2708 selection_acquire();
2712 HANDLE event = CreateEventW(NULL, FALSE, FALSE, NULL);
2713 selectionThread = CreateThread(NULL, 0, &selection_thread_proc, event, 0, NULL);
2715 if (!selectionThread)
2717 WARN("Could not start clipboard thread\n");
2721 WaitForSingleObject(event, INFINITE);
2723 CloseHandle(selectionThread);
2730 /**************************************************************************
2731 * X11DRV_EmptyClipboard
2733 * Empty cached clipboard data.
2735 void CDECL X11DRV_EmptyClipboard(BOOL keepunowned)
2739 LPWINE_CLIPDATA lpData, lpStart;
2740 LPWINE_CLIPDATA lpNext = ClipData;
2742 TRACE(" called with %d entries in cache.\n", ClipDataCount);
2748 lpNext = lpData->NextData;
2750 if (!keepunowned || !(lpData->wFlags & CF_FLAG_UNOWNED))
2752 lpData->PrevData->NextData = lpData->NextData;
2753 lpData->NextData->PrevData = lpData->PrevData;
2755 if (lpData == ClipData)
2756 ClipData = lpNext != lpData ? lpNext : NULL;
2758 X11DRV_CLIPBOARD_FreeData(lpData);
2759 HeapFree(GetProcessHeap(), 0, lpData);
2763 } while (lpNext != lpStart);
2766 TRACE(" %d entries remaining in cache.\n", ClipDataCount);
2771 /**************************************************************************
2772 * X11DRV_SetClipboardData
2774 BOOL CDECL X11DRV_SetClipboardData(UINT wFormat, HANDLE hData, BOOL owner)
2777 BOOL bResult = TRUE;
2779 /* If it's not owned, data can only be set if the format data is not already owned
2780 and its rendering is not delayed */
2783 CLIPBOARDINFO cbinfo;
2784 LPWINE_CLIPDATA lpRender;
2786 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2789 ((lpRender = X11DRV_CLIPBOARD_LookupData(wFormat)) &&
2790 !(lpRender->wFlags & CF_FLAG_UNOWNED)))
2793 flags = CF_FLAG_UNOWNED;
2796 bResult &= X11DRV_CLIPBOARD_InsertClipboardData(wFormat, hData, flags, NULL, TRUE);
2802 /**************************************************************************
2803 * CountClipboardFormats
2805 INT CDECL X11DRV_CountClipboardFormats(void)
2807 CLIPBOARDINFO cbinfo;
2809 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2811 TRACE(" count=%d\n", ClipDataCount);
2813 return ClipDataCount;
2817 /**************************************************************************
2818 * X11DRV_EnumClipboardFormats
2820 UINT CDECL X11DRV_EnumClipboardFormats(UINT wFormat)
2822 CLIPBOARDINFO cbinfo;
2823 UINT wNextFormat = 0;
2825 TRACE("(%04X)\n", wFormat);
2827 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2832 wNextFormat = ClipData->wFormatID;
2836 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormat);
2838 if (lpData && lpData->NextData != ClipData)
2839 wNextFormat = lpData->NextData->wFormatID;
2846 /**************************************************************************
2847 * X11DRV_IsClipboardFormatAvailable
2849 BOOL CDECL X11DRV_IsClipboardFormatAvailable(UINT wFormat)
2852 CLIPBOARDINFO cbinfo;
2854 TRACE("(%04X)\n", wFormat);
2856 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2858 if (wFormat != 0 && X11DRV_CLIPBOARD_LookupData(wFormat))
2861 TRACE("(%04X)- ret(%d)\n", wFormat, bRet);
2867 /**************************************************************************
2868 * GetClipboardData (USER.142)
2870 HANDLE CDECL X11DRV_GetClipboardData(UINT wFormat)
2872 CLIPBOARDINFO cbinfo;
2873 LPWINE_CLIPDATA lpRender;
2875 TRACE("(%04X)\n", wFormat);
2877 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2879 if ((lpRender = X11DRV_CLIPBOARD_LookupData(wFormat)))
2881 if ( !lpRender->hData )
2882 X11DRV_CLIPBOARD_RenderFormat(thread_init_display(), lpRender);
2884 TRACE(" returning %p (type %04x)\n", lpRender->hData, lpRender->wFormatID);
2885 return lpRender->hData;
2892 /**************************************************************************
2893 * ResetSelectionOwner
2895 * Called when the thread owning the selection is destroyed and we need to
2896 * preserve the selection ownership. We look for another top level window
2897 * in this process and send it a message to acquire the selection.
2899 void X11DRV_ResetSelectionOwner(void)
2906 if (!selectionAcquired || thread_selection_wnd() != selectionWindow)
2909 selectionAcquired = S_NOSELECTION;
2910 selectionWindow = 0;
2912 hwnd = GetWindow(GetDesktopWindow(), GW_CHILD);
2915 if (GetCurrentThreadId() != GetWindowThreadProcessId(hwnd, &procid))
2917 if (GetCurrentProcessId() == procid)
2919 if (SendMessageW(hwnd, WM_X11DRV_ACQUIRE_SELECTION, 0, 0))
2923 } while ((hwnd = GetWindow(hwnd, GW_HWNDNEXT)) != NULL);
2925 WARN("Failed to find another thread to take selection ownership. Clipboard data will be lost.\n");
2927 X11DRV_CLIPBOARD_ReleaseOwnership();
2928 X11DRV_EmptyClipboard(FALSE);
2932 /**************************************************************************
2933 * X11DRV_CLIPBOARD_SynthesizeData
2935 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID)
2938 LPWINE_CLIPDATA lpSource = NULL;
2940 TRACE(" %04x\n", wFormatID);
2942 /* Don't need to synthesize if it already exists */
2943 if (X11DRV_CLIPBOARD_LookupData(wFormatID))
2946 if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
2948 bsyn = ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
2949 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2950 ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
2951 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2952 ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
2953 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED);
2955 else if (wFormatID == CF_ENHMETAFILE)
2957 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT)) &&
2958 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2960 else if (wFormatID == CF_METAFILEPICT)
2962 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_ENHMETAFILE)) &&
2963 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2965 else if (wFormatID == CF_DIB)
2967 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) &&
2968 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2970 else if (wFormatID == CF_BITMAP)
2972 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) &&
2973 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2977 X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, 0, CF_FLAG_SYNTHESIZED, NULL, TRUE);
2984 /**************************************************************************
2985 * X11DRV_EndClipboardUpdate
2987 * Add locale if it hasn't already been added
2989 void CDECL X11DRV_EndClipboardUpdate(void)
2991 INT count = ClipDataCount;
2993 /* Do Unicode <-> Text <-> OEM mapping */
2994 X11DRV_CLIPBOARD_SynthesizeData(CF_TEXT);
2995 X11DRV_CLIPBOARD_SynthesizeData(CF_OEMTEXT);
2996 X11DRV_CLIPBOARD_SynthesizeData(CF_UNICODETEXT);
2998 /* Enhmetafile <-> MetafilePict mapping */
2999 X11DRV_CLIPBOARD_SynthesizeData(CF_ENHMETAFILE);
3000 X11DRV_CLIPBOARD_SynthesizeData(CF_METAFILEPICT);
3002 /* DIB <-> Bitmap mapping */
3003 X11DRV_CLIPBOARD_SynthesizeData(CF_DIB);
3004 X11DRV_CLIPBOARD_SynthesizeData(CF_BITMAP);
3006 TRACE("%d formats added to cached data\n", ClipDataCount - count);
3010 /***********************************************************************
3011 * X11DRV_SelectionRequest_TARGETS
3012 * Service a TARGETS selection request event
3014 static Atom X11DRV_SelectionRequest_TARGETS( Display *display, Window requestor,
3015 Atom target, Atom rprop )
3020 LPWINE_CLIPFORMAT lpFormats;
3021 LPWINE_CLIPDATA lpData;
3023 /* Create X atoms for any clipboard types which don't have atoms yet.
3024 * This avoids sending bogus zero atoms.
3025 * Without this, copying might not have access to all clipboard types.
3026 * FIXME: is it safe to call this here?
3031 * Count the number of items we wish to expose as selection targets.
3033 cTargets = 1; /* Include TARGETS */
3035 if (!(lpData = ClipData)) return None;
3039 lpFormats = ClipFormats;
3043 if ((lpFormats->wFormatID == lpData->wFormatID) &&
3044 lpFormats->lpDrvExportFunc && lpFormats->drvData)
3047 lpFormats = lpFormats->NextFormat;
3050 lpData = lpData->NextData;
3052 while (lpData != ClipData);
3054 TRACE(" found %d formats\n", cTargets);
3056 /* Allocate temp buffer */
3057 targets = HeapAlloc( GetProcessHeap(), 0, cTargets * sizeof(Atom));
3063 targets[i++] = x11drv_atom(TARGETS);
3067 lpFormats = ClipFormats;
3071 if ((lpFormats->wFormatID == lpData->wFormatID) &&
3072 lpFormats->lpDrvExportFunc && lpFormats->drvData)
3073 targets[i++] = lpFormats->drvData;
3075 lpFormats = lpFormats->NextFormat;
3078 lpData = lpData->NextData;
3080 while (lpData != ClipData);
3084 if (TRACE_ON(clipboard))
3087 for ( i = 0; i < cTargets; i++)
3089 char *itemFmtName = XGetAtomName(display, targets[i]);
3090 TRACE("\tAtom# %d: Property %ld Type %s\n", i, targets[i], itemFmtName);
3095 /* We may want to consider setting the type to xaTargets instead,
3096 * in case some apps expect this instead of XA_ATOM */
3097 XChangeProperty(display, requestor, rprop, XA_ATOM, 32,
3098 PropModeReplace, (unsigned char *)targets, cTargets);
3099 wine_tsx11_unlock();
3101 HeapFree(GetProcessHeap(), 0, targets);
3107 /***********************************************************************
3108 * X11DRV_SelectionRequest_MULTIPLE
3109 * Service a MULTIPLE selection request event
3110 * rprop contains a list of (target,property) atom pairs.
3111 * The first atom names a target and the second names a property.
3112 * The effect is as if we have received a sequence of SelectionRequest events
3113 * (one for each atom pair) except that:
3114 * 1. We reply with a SelectionNotify only when all the requested conversions
3115 * have been performed.
3116 * 2. If we fail to convert the target named by an atom in the MULTIPLE property,
3117 * we replace the atom in the property by None.
3119 static Atom X11DRV_SelectionRequest_MULTIPLE( HWND hWnd, XSelectionRequestEvent *pevent )
3121 Display *display = pevent->display;
3123 Atom atype=AnyPropertyType;
3125 unsigned long remain;
3126 Atom* targetPropList=NULL;
3127 unsigned long cTargetPropList = 0;
3129 /* If the specified property is None the requestor is an obsolete client.
3130 * We support these by using the specified target atom as the reply property.
3132 rprop = pevent->property;
3134 rprop = pevent->target;
3138 /* Read the MULTIPLE property contents. This should contain a list of
3139 * (target,property) atom pairs.
3142 if(XGetWindowProperty(display, pevent->requestor, rprop,
3143 0, 0x3FFF, False, AnyPropertyType, &atype,&aformat,
3144 &cTargetPropList, &remain,
3145 (unsigned char**)&targetPropList) != Success)
3147 wine_tsx11_unlock();
3148 TRACE("\tCouldn't read MULTIPLE property\n");
3152 TRACE("\tType %s,Format %d,nItems %ld, Remain %ld\n",
3153 XGetAtomName(display, atype), aformat, cTargetPropList, remain);
3154 wine_tsx11_unlock();
3157 * Make sure we got what we expect.
3158 * NOTE: According to the X-ICCCM Version 2.0 documentation the property sent
3159 * in a MULTIPLE selection request should be of type ATOM_PAIR.
3160 * However some X apps(such as XPaint) are not compliant with this and return
3161 * a user defined atom in atype when XGetWindowProperty is called.
3162 * The data *is* an atom pair but is not denoted as such.
3164 if(aformat == 32 /* atype == xAtomPair */ )
3168 /* Iterate through the ATOM_PAIR list and execute a SelectionRequest
3169 * for each (target,property) pair */
3171 for (i = 0; i < cTargetPropList; i+=2)
3173 XSelectionRequestEvent event;
3175 if (TRACE_ON(clipboard))
3177 char *targetName, *propName;
3179 targetName = XGetAtomName(display, targetPropList[i]);
3180 propName = XGetAtomName(display, targetPropList[i+1]);
3181 TRACE("MULTIPLE(%d): Target='%s' Prop='%s'\n",
3182 i/2, targetName, propName);
3185 wine_tsx11_unlock();
3188 /* We must have a non "None" property to service a MULTIPLE target atom */
3189 if ( !targetPropList[i+1] )
3191 TRACE("\tMULTIPLE(%d): Skipping target with empty property!\n", i);
3195 /* Set up an XSelectionRequestEvent for this (target,property) pair */
3197 event.target = targetPropList[i];
3198 event.property = targetPropList[i+1];
3200 /* Fire a SelectionRequest, informing the handler that we are processing
3201 * a MULTIPLE selection request event.
3203 X11DRV_HandleSelectionRequest( hWnd, &event, TRUE );
3207 /* Free the list of targets/properties */
3209 XFree(targetPropList);
3210 wine_tsx11_unlock();
3217 /***********************************************************************
3218 * X11DRV_HandleSelectionRequest
3219 * Process an event selection request event.
3220 * The bIsMultiple flag is used to signal when EVENT_SelectionRequest is called
3221 * recursively while servicing a "MULTIPLE" selection target.
3223 * Note: We only receive this event when WINE owns the X selection
3225 static void X11DRV_HandleSelectionRequest( HWND hWnd, XSelectionRequestEvent *event, BOOL bIsMultiple )
3227 Display *display = event->display;
3228 XSelectionEvent result;
3230 Window request = event->requestor;
3235 * We can only handle the selection request if :
3236 * The selection is PRIMARY or CLIPBOARD, AND we can successfully open the clipboard.
3237 * Don't do these checks or open the clipboard while recursively processing MULTIPLE,
3238 * since this has been already done.
3242 if (((event->selection != XA_PRIMARY) && (event->selection != x11drv_atom(CLIPBOARD))))
3246 /* If the specified property is None the requestor is an obsolete client.
3247 * We support these by using the specified target atom as the reply property.
3249 rprop = event->property;
3251 rprop = event->target;
3253 if(event->target == x11drv_atom(TARGETS)) /* Return a list of all supported targets */
3255 /* TARGETS selection request */
3256 rprop = X11DRV_SelectionRequest_TARGETS( display, request, event->target, rprop );
3258 else if(event->target == x11drv_atom(MULTIPLE)) /* rprop contains a list of (target, property) atom pairs */
3260 /* MULTIPLE selection request */
3261 rprop = X11DRV_SelectionRequest_MULTIPLE( hWnd, event );
3265 LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupProperty(NULL, event->target);
3267 if (lpFormat && lpFormat->lpDrvExportFunc)
3269 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(lpFormat->wFormatID);
3273 unsigned char* lpClipData;
3275 HANDLE hClipData = lpFormat->lpDrvExportFunc(display, request, event->target,
3276 rprop, lpData, &cBytes);
3278 if (hClipData && (lpClipData = GlobalLock(hClipData)))
3280 int mode = PropModeReplace;
3282 TRACE("\tUpdating property %s, %d bytes\n", debugstr_w(lpFormat->Name), cBytes);
3287 int nelements = min(cBytes, 65536);
3288 XChangeProperty(display, request, rprop, event->target,
3289 8, mode, lpClipData, nelements);
3290 mode = PropModeAppend;
3291 cBytes -= nelements;
3292 lpClipData += nelements;
3293 } while (cBytes > 0);
3294 wine_tsx11_unlock();
3296 GlobalUnlock(hClipData);
3297 GlobalFree(hClipData);
3305 * SelectionNotify should be sent only at the end of a MULTIPLE request
3309 result.type = SelectionNotify;
3310 result.display = display;
3311 result.requestor = request;
3312 result.selection = event->selection;
3313 result.property = rprop;
3314 result.target = event->target;
3315 result.time = event->time;
3316 TRACE("Sending SelectionNotify event...\n");
3318 XSendEvent(display,event->requestor,False,NoEventMask,(XEvent*)&result);
3319 wine_tsx11_unlock();
3324 /***********************************************************************
3325 * X11DRV_SelectionRequest
3327 void X11DRV_SelectionRequest( HWND hWnd, XEvent *event )
3329 X11DRV_HandleSelectionRequest( hWnd, &event->xselectionrequest, FALSE );
3333 /***********************************************************************
3334 * X11DRV_SelectionClear
3336 void X11DRV_SelectionClear( HWND hWnd, XEvent *xev )
3338 XSelectionClearEvent *event = &xev->xselectionclear;
3339 if (event->selection == XA_PRIMARY || event->selection == x11drv_atom(CLIPBOARD))
3340 X11DRV_CLIPBOARD_ReleaseSelection( event->display, event->selection,
3341 event->window, hWnd, event->time );
3344 /***********************************************************************
3345 * X11DRV_Clipboard_Cleanup
3347 void X11DRV_Clipboard_Cleanup(void)
3349 selectionAcquired = S_NOSELECTION;
3351 X11DRV_EmptyClipboard(FALSE);