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/list.h"
83 #include "wine/debug.h"
84 #include "wine/unicode.h"
85 #include "wine/server.h"
87 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
89 /* Maximum wait time for selection notify */
90 #define SELECTION_RETRIES 500 /* wait for .5 seconds */
91 #define SELECTION_WAIT 1000 /* us */
94 #define S_NOSELECTION 0
105 } CLIPBOARDINFO, *LPCLIPBOARDINFO;
107 struct tagWINE_CLIPDATA; /* Forward */
109 typedef HANDLE (*DRVEXPORTFUNC)(Display *display, Window requestor, Atom aTarget, Atom rprop,
110 struct tagWINE_CLIPDATA* lpData, LPDWORD lpBytes);
111 typedef HANDLE (*DRVIMPORTFUNC)(Display *d, Window w, Atom prop);
113 typedef struct tagWINE_CLIPFORMAT {
117 DRVIMPORTFUNC lpDrvImportFunc;
118 DRVEXPORTFUNC lpDrvExportFunc;
119 } WINE_CLIPFORMAT, *LPWINE_CLIPFORMAT;
121 typedef struct tagWINE_CLIPDATA {
127 LPWINE_CLIPFORMAT lpFormat;
128 } WINE_CLIPDATA, *LPWINE_CLIPDATA;
130 #define CF_FLAG_UNOWNED 0x0001 /* cached data is not owned */
131 #define CF_FLAG_SYNTHESIZED 0x0002 /* Implicitly converted data */
133 static int selectionAcquired = 0; /* Contains the current selection masks */
134 static Window selectionWindow = None; /* The top level X window which owns the selection */
135 static Atom selectionCacheSrc = XA_PRIMARY; /* The selection source from which the clipboard cache was filled */
137 void CDECL X11DRV_EmptyClipboard(BOOL keepunowned);
138 void CDECL X11DRV_EndClipboardUpdate(void);
139 static HANDLE X11DRV_CLIPBOARD_ImportClipboardData(Display *d, Window w, Atom prop);
140 static HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(Display *d, Window w, Atom prop);
141 static HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(Display *d, Window w, Atom prop);
142 static HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(Display *d, Window w, Atom prop);
143 static HANDLE X11DRV_CLIPBOARD_ImportImageBmp(Display *d, Window w, Atom prop);
144 static HANDLE X11DRV_CLIPBOARD_ImportXAString(Display *d, Window w, Atom prop);
145 static HANDLE X11DRV_CLIPBOARD_ImportUTF8(Display *d, Window w, Atom prop);
146 static HANDLE X11DRV_CLIPBOARD_ImportCompoundText(Display *d, Window w, Atom prop);
147 static HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Display *display, Window requestor, Atom aTarget,
148 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
149 static HANDLE X11DRV_CLIPBOARD_ExportString(Display *display, Window requestor, Atom aTarget,
150 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
151 static HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Display *display, Window requestor, Atom aTarget,
152 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
153 static HANDLE X11DRV_CLIPBOARD_ExportImageBmp(Display *display, Window requestor, Atom aTarget,
154 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
155 static HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Display *display, Window requestor, Atom aTarget,
156 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
157 static HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Display *display, Window requestor, Atom aTarget,
158 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
159 static HANDLE X11DRV_CLIPBOARD_ExportTextHtml(Display *display, Window requestor, Atom aTarget,
160 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
161 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(UINT id, Atom prop);
162 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(Display *display, UINT wFormatID);
163 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData);
164 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void);
165 static int X11DRV_CLIPBOARD_QueryAvailableData(Display *display, LPCLIPBOARDINFO lpcbinfo);
166 static BOOL X11DRV_CLIPBOARD_ReadSelectionData(Display *display, LPWINE_CLIPDATA lpData);
167 static BOOL X11DRV_CLIPBOARD_ReadProperty(Display *display, Window w, Atom prop,
168 unsigned char** data, unsigned long* datasize);
169 static BOOL X11DRV_CLIPBOARD_RenderFormat(Display *display, LPWINE_CLIPDATA lpData);
170 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out);
171 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID);
172 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(Display *display, LPWINE_CLIPDATA lpData);
173 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedDIB(Display *display);
174 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedBitmap(Display *display);
175 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile(Display *display);
176 static void X11DRV_HandleSelectionRequest( HWND hWnd, XSelectionRequestEvent *event, BOOL bIsMultiple );
178 /* Clipboard formats */
184 DRVIMPORTFUNC import;
185 DRVEXPORTFUNC export;
186 } builtin_formats[] =
188 { CF_TEXT, XA_STRING, X11DRV_CLIPBOARD_ImportXAString, X11DRV_CLIPBOARD_ExportString},
189 { CF_BITMAP, XATOM_WCF_BITMAP, X11DRV_CLIPBOARD_ImportClipboardData, NULL},
190 { CF_METAFILEPICT, XATOM_WCF_METAFILEPICT, X11DRV_CLIPBOARD_ImportMetaFilePict, X11DRV_CLIPBOARD_ExportMetaFilePict },
191 { CF_SYLK, XATOM_WCF_SYLK, X11DRV_CLIPBOARD_ImportClipboardData, X11DRV_CLIPBOARD_ExportClipboardData },
192 { CF_DIF, XATOM_WCF_DIF, X11DRV_CLIPBOARD_ImportClipboardData, X11DRV_CLIPBOARD_ExportClipboardData },
193 { CF_TIFF, XATOM_WCF_TIFF, X11DRV_CLIPBOARD_ImportClipboardData, X11DRV_CLIPBOARD_ExportClipboardData },
194 { CF_OEMTEXT, XATOM_WCF_OEMTEXT, X11DRV_CLIPBOARD_ImportClipboardData, X11DRV_CLIPBOARD_ExportClipboardData },
195 { CF_DIB, XA_PIXMAP, X11DRV_CLIPBOARD_ImportXAPIXMAP, X11DRV_CLIPBOARD_ExportXAPIXMAP },
196 { CF_PALETTE, XATOM_WCF_PALETTE, X11DRV_CLIPBOARD_ImportClipboardData, X11DRV_CLIPBOARD_ExportClipboardData },
197 { CF_PENDATA, XATOM_WCF_PENDATA, X11DRV_CLIPBOARD_ImportClipboardData, X11DRV_CLIPBOARD_ExportClipboardData },
198 { CF_RIFF, XATOM_WCF_RIFF, X11DRV_CLIPBOARD_ImportClipboardData, X11DRV_CLIPBOARD_ExportClipboardData },
199 { CF_WAVE, XATOM_WCF_WAVE, X11DRV_CLIPBOARD_ImportClipboardData, X11DRV_CLIPBOARD_ExportClipboardData },
200 { CF_UNICODETEXT, XATOM_UTF8_STRING, X11DRV_CLIPBOARD_ImportUTF8, X11DRV_CLIPBOARD_ExportString },
201 /* If UTF8_STRING is not available, attempt COMPOUND_TEXT */
202 { CF_UNICODETEXT, XATOM_COMPOUND_TEXT, X11DRV_CLIPBOARD_ImportCompoundText, X11DRV_CLIPBOARD_ExportString },
203 { CF_ENHMETAFILE, XATOM_WCF_ENHMETAFILE, X11DRV_CLIPBOARD_ImportEnhMetaFile, X11DRV_CLIPBOARD_ExportEnhMetaFile },
204 { CF_HDROP, XATOM_WCF_HDROP, X11DRV_CLIPBOARD_ImportClipboardData, X11DRV_CLIPBOARD_ExportClipboardData },
205 { CF_LOCALE, XATOM_WCF_LOCALE, X11DRV_CLIPBOARD_ImportClipboardData, X11DRV_CLIPBOARD_ExportClipboardData },
206 { CF_DIBV5, XATOM_WCF_DIBV5, X11DRV_CLIPBOARD_ImportClipboardData, X11DRV_CLIPBOARD_ExportClipboardData },
207 { CF_OWNERDISPLAY, XATOM_WCF_OWNERDISPLAY, X11DRV_CLIPBOARD_ImportClipboardData, X11DRV_CLIPBOARD_ExportClipboardData },
208 { CF_DSPTEXT, XATOM_WCF_DSPTEXT, X11DRV_CLIPBOARD_ImportClipboardData, X11DRV_CLIPBOARD_ExportClipboardData },
209 { CF_DSPBITMAP, XATOM_WCF_DSPBITMAP, X11DRV_CLIPBOARD_ImportClipboardData, X11DRV_CLIPBOARD_ExportClipboardData },
210 { CF_DSPMETAFILEPICT, XATOM_WCF_DSPMETAFILEPICT, X11DRV_CLIPBOARD_ImportClipboardData, X11DRV_CLIPBOARD_ExportClipboardData },
211 { CF_DSPENHMETAFILE, XATOM_WCF_DSPENHMETAFILE, X11DRV_CLIPBOARD_ImportClipboardData, X11DRV_CLIPBOARD_ExportClipboardData },
212 { CF_DIB, XATOM_image_bmp, X11DRV_CLIPBOARD_ImportImageBmp, X11DRV_CLIPBOARD_ExportImageBmp },
215 static struct list format_list = LIST_INIT( format_list );
217 #define GET_ATOM(prop) (((prop) < FIRST_XATOM) ? (Atom)(prop) : X11DRV_Atoms[(prop) - FIRST_XATOM])
219 /* Maps X properties to Windows formats */
220 static const WCHAR wszRichTextFormat[] = {'R','i','c','h',' ','T','e','x','t',' ','F','o','r','m','a','t',0};
221 static const WCHAR wszGIF[] = {'G','I','F',0};
222 static const WCHAR wszJFIF[] = {'J','F','I','F',0};
223 static const WCHAR wszPNG[] = {'P','N','G',0};
224 static const WCHAR wszHTMLFormat[] = {'H','T','M','L',' ','F','o','r','m','a','t',0};
229 } PropertyFormatMap[] =
231 { wszRichTextFormat, XATOM_text_rtf },
232 { wszRichTextFormat, XATOM_text_richtext },
233 { wszGIF, XATOM_image_gif },
234 { wszJFIF, XATOM_image_jpeg },
235 { wszPNG, XATOM_image_png },
236 { wszHTMLFormat, XATOM_HTML_Format }, /* prefer this to text/html */
241 * Cached clipboard data.
243 static struct list data_list = LIST_INIT( data_list );
244 static UINT ClipDataCount = 0;
247 * Clipboard sequence number
249 static UINT wSeqNo = 0;
251 /**************************************************************************
252 * Internal Clipboard implementation methods
253 **************************************************************************/
255 static Window thread_selection_wnd(void)
257 struct x11drv_thread_data *thread_data = x11drv_init_thread_data();
258 Window w = thread_data->selection_wnd;
262 w = XCreateWindow(thread_data->display, root_window, 0, 0, 1, 1, 0, CopyFromParent,
263 InputOnly, CopyFromParent, 0, NULL);
265 thread_data->selection_wnd = w;
267 FIXME("Failed to create window. Fetching selection data will fail.\n");
273 static const char *debugstr_format( UINT id )
277 if (GetClipboardFormatNameW( id, buffer, 256 ))
278 return wine_dbg_sprintf( "%04x %s", id, debugstr_w(buffer) );
282 #define BUILTIN(id) case id: return #id;
285 BUILTIN(CF_METAFILEPICT)
295 BUILTIN(CF_UNICODETEXT)
296 BUILTIN(CF_ENHMETAFILE)
300 BUILTIN(CF_OWNERDISPLAY)
302 BUILTIN(CF_DSPBITMAP)
303 BUILTIN(CF_DSPMETAFILEPICT)
304 BUILTIN(CF_DSPENHMETAFILE)
306 default: return wine_dbg_sprintf( "%04x", id );
310 /**************************************************************************
311 * X11DRV_InitClipboard
313 void X11DRV_InitClipboard(void)
316 WINE_CLIPFORMAT *format;
318 /* Register built-in formats */
319 for (i = 0; i < sizeof(builtin_formats)/sizeof(builtin_formats[0]); i++)
321 if (!(format = HeapAlloc( GetProcessHeap(), 0, sizeof(*format )))) break;
322 format->wFormatID = builtin_formats[i].id;
323 format->drvData = GET_ATOM(builtin_formats[i].data);
324 format->lpDrvImportFunc = builtin_formats[i].import;
325 format->lpDrvExportFunc = builtin_formats[i].export;
326 list_add_tail( &format_list, &format->entry );
329 /* Register known mapping between window formats and X properties */
330 for (i = 0; i < sizeof(PropertyFormatMap)/sizeof(PropertyFormatMap[0]); i++)
331 X11DRV_CLIPBOARD_InsertClipboardFormat( RegisterClipboardFormatW(PropertyFormatMap[i].lpszFormat),
332 GET_ATOM(PropertyFormatMap[i].prop));
334 /* Set up a conversion function from "HTML Format" to "text/html" */
335 format = X11DRV_CLIPBOARD_InsertClipboardFormat( RegisterClipboardFormatW(wszHTMLFormat),
336 GET_ATOM(XATOM_text_html));
337 format->lpDrvExportFunc = X11DRV_CLIPBOARD_ExportTextHtml;
341 /**************************************************************************
344 * Intern atoms for formats that don't have one yet.
346 static void intern_atoms(void)
348 LPWINE_CLIPFORMAT format;
356 LIST_FOR_EACH_ENTRY( format, &format_list, WINE_CLIPFORMAT, entry )
357 if (!format->drvData) count++;
360 display = thread_init_display();
362 names = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*names) );
363 atoms = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*atoms) );
366 LIST_FOR_EACH_ENTRY( format, &format_list, WINE_CLIPFORMAT, entry )
367 if (!format->drvData) {
368 GetClipboardFormatNameW( format->wFormatID, buffer, 256 );
369 len = WideCharToMultiByte(CP_UNIXCP, 0, buffer, -1, NULL, 0, NULL, NULL);
370 names[i] = HeapAlloc(GetProcessHeap(), 0, len);
371 WideCharToMultiByte(CP_UNIXCP, 0, buffer, -1, names[i++], len, NULL, NULL);
374 XInternAtoms( display, names, count, False, atoms );
377 LIST_FOR_EACH_ENTRY( format, &format_list, WINE_CLIPFORMAT, entry )
378 if (!format->drvData) {
379 HeapFree(GetProcessHeap(), 0, names[i]);
380 format->drvData = atoms[i++];
383 HeapFree( GetProcessHeap(), 0, names );
384 HeapFree( GetProcessHeap(), 0, atoms );
388 /**************************************************************************
391 * Register a custom X clipboard format.
393 static WINE_CLIPFORMAT *register_format( UINT id, Atom prop )
395 LPWINE_CLIPFORMAT lpFormat;
397 /* walk format chain to see if it's already registered */
398 LIST_FOR_EACH_ENTRY( lpFormat, &format_list, WINE_CLIPFORMAT, entry )
399 if (lpFormat->wFormatID == id) return lpFormat;
401 return X11DRV_CLIPBOARD_InsertClipboardFormat(id, prop);
405 /**************************************************************************
406 * X11DRV_CLIPBOARD_LookupProperty
408 static LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupProperty(LPWINE_CLIPFORMAT current, UINT drvData)
412 struct list *ptr = current ? ¤t->entry : &format_list;
413 BOOL need_intern = FALSE;
415 while ((ptr = list_next( &format_list, ptr )))
417 LPWINE_CLIPFORMAT lpFormat = LIST_ENTRY( ptr, WINE_CLIPFORMAT, entry );
418 if (lpFormat->drvData == drvData) return lpFormat;
419 if (!lpFormat->drvData) need_intern = TRUE;
421 if (!need_intern) return NULL;
423 /* restart the search for the new atoms */
428 /**************************************************************************
429 * X11DRV_CLIPBOARD_LookupData
431 static LPWINE_CLIPDATA X11DRV_CLIPBOARD_LookupData(DWORD wID)
435 LIST_FOR_EACH_ENTRY( data, &data_list, WINE_CLIPDATA, entry )
436 if (data->wFormatID == wID) return data;
442 /**************************************************************************
443 * InsertClipboardFormat
445 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat( UINT id, Atom prop )
447 LPWINE_CLIPFORMAT lpNewFormat;
449 /* allocate storage for new format entry */
450 lpNewFormat = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPFORMAT));
452 if(lpNewFormat == NULL)
454 WARN("No more memory for a new format!\n");
457 lpNewFormat->wFormatID = id;
458 lpNewFormat->drvData = prop;
459 lpNewFormat->lpDrvImportFunc = X11DRV_CLIPBOARD_ImportClipboardData;
460 lpNewFormat->lpDrvExportFunc = X11DRV_CLIPBOARD_ExportClipboardData;
462 list_add_tail( &format_list, &lpNewFormat->entry );
464 TRACE("Registering format %s drvData %d\n",
465 debugstr_format(lpNewFormat->wFormatID), lpNewFormat->drvData);
473 /**************************************************************************
474 * X11DRV_CLIPBOARD_GetClipboardInfo
476 static BOOL X11DRV_CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
480 SERVER_START_REQ( set_clipboard_info )
484 if (wine_server_call_err( req ))
486 ERR("Failed to get clipboard owner.\n");
490 cbInfo->hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
491 cbInfo->hWndOwner = wine_server_ptr_handle( reply->old_owner );
492 cbInfo->hWndViewer = wine_server_ptr_handle( reply->old_viewer );
493 cbInfo->seqno = reply->seqno;
494 cbInfo->flags = reply->flags;
505 /**************************************************************************
506 * X11DRV_CLIPBOARD_ReleaseOwnership
508 static BOOL X11DRV_CLIPBOARD_ReleaseOwnership(void)
512 SERVER_START_REQ( set_clipboard_info )
514 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
516 if (wine_server_call_err( req ))
518 ERR("Failed to set clipboard.\n");
532 /**************************************************************************
533 * X11DRV_CLIPBOARD_InsertClipboardData
535 * Caller *must* have the clipboard open and be the owner.
537 static BOOL X11DRV_CLIPBOARD_InsertClipboardData(UINT wFormatID, HANDLE hData, DWORD flags,
538 LPWINE_CLIPFORMAT lpFormat, BOOL override)
540 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormatID);
542 TRACE("format=%04x lpData=%p hData=%p flags=0x%08x lpFormat=%p override=%d\n",
543 wFormatID, lpData, hData, flags, lpFormat, override);
545 /* make sure the format exists */
546 if (!lpFormat) register_format( wFormatID, 0 );
548 if (lpData && !override)
553 X11DRV_CLIPBOARD_FreeData(lpData);
555 lpData->hData = hData;
559 lpData = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPDATA));
561 lpData->wFormatID = wFormatID;
562 lpData->hData = hData;
563 lpData->lpFormat = lpFormat;
566 list_add_tail( &data_list, &lpData->entry );
570 lpData->wFlags = flags;
576 /**************************************************************************
577 * X11DRV_CLIPBOARD_FreeData
579 * Free clipboard data handle.
581 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData)
583 TRACE("%04x\n", lpData->wFormatID);
585 if ((lpData->wFormatID >= CF_GDIOBJFIRST &&
586 lpData->wFormatID <= CF_GDIOBJLAST) ||
587 lpData->wFormatID == CF_BITMAP ||
588 lpData->wFormatID == CF_DIB ||
589 lpData->wFormatID == CF_PALETTE)
592 DeleteObject(lpData->hData);
594 if ((lpData->wFormatID == CF_DIB) && lpData->drvData)
595 XFreePixmap(gdi_display, lpData->drvData);
597 else if (lpData->wFormatID == CF_METAFILEPICT)
601 DeleteMetaFile(((METAFILEPICT *)GlobalLock( lpData->hData ))->hMF );
602 GlobalFree(lpData->hData);
605 else if (lpData->wFormatID == CF_ENHMETAFILE)
608 DeleteEnhMetaFile(lpData->hData);
610 else if (lpData->wFormatID < CF_PRIVATEFIRST ||
611 lpData->wFormatID > CF_PRIVATELAST)
614 GlobalFree(lpData->hData);
622 /**************************************************************************
623 * X11DRV_CLIPBOARD_UpdateCache
625 static BOOL X11DRV_CLIPBOARD_UpdateCache(LPCLIPBOARDINFO lpcbinfo)
629 if (!X11DRV_CLIPBOARD_IsSelectionOwner())
631 if (!X11DRV_CLIPBOARD_GetClipboardInfo(lpcbinfo))
633 ERR("Failed to retrieve clipboard information.\n");
636 else if (wSeqNo < lpcbinfo->seqno)
638 X11DRV_EmptyClipboard(TRUE);
640 if (X11DRV_CLIPBOARD_QueryAvailableData(thread_init_display(), lpcbinfo) < 0)
642 ERR("Failed to cache clipboard data owned by another process.\n");
647 X11DRV_EndClipboardUpdate();
650 wSeqNo = lpcbinfo->seqno;
658 /**************************************************************************
659 * X11DRV_CLIPBOARD_RenderFormat
661 static BOOL X11DRV_CLIPBOARD_RenderFormat(Display *display, LPWINE_CLIPDATA lpData)
665 TRACE(" 0x%04x hData(%p)\n", lpData->wFormatID, lpData->hData);
667 if (lpData->hData) return bret; /* Already rendered */
669 if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
670 bret = X11DRV_CLIPBOARD_RenderSynthesizedFormat(display, lpData);
671 else if (!X11DRV_CLIPBOARD_IsSelectionOwner())
673 if (!X11DRV_CLIPBOARD_ReadSelectionData(display, lpData))
675 ERR("Failed to cache clipboard data owned by another process. Format=%04x\n",
682 CLIPBOARDINFO cbInfo;
684 if (X11DRV_CLIPBOARD_GetClipboardInfo(&cbInfo) && cbInfo.hWndOwner)
686 /* Send a WM_RENDERFORMAT message to notify the owner to render the
687 * data requested into the clipboard.
689 TRACE("Sending WM_RENDERFORMAT message to hwnd(%p)\n", cbInfo.hWndOwner);
690 SendMessageW(cbInfo.hWndOwner, WM_RENDERFORMAT, lpData->wFormatID, 0);
692 if (!lpData->hData) bret = FALSE;
696 ERR("hWndClipOwner is lost!\n");
705 /**************************************************************************
706 * CLIPBOARD_ConvertText
707 * Returns number of required/converted characters - not bytes!
709 static INT CLIPBOARD_ConvertText(WORD src_fmt, void const *src, INT src_size,
710 WORD dst_fmt, void *dst, INT dst_size)
714 if(src_fmt == CF_UNICODETEXT)
727 return WideCharToMultiByte(cp, 0, src, src_size, dst, dst_size, NULL, NULL);
730 if(dst_fmt == CF_UNICODETEXT)
743 return MultiByteToWideChar(cp, 0, src, src_size, dst, dst_size);
746 if(!dst_size) return src_size;
748 if(dst_size > src_size) dst_size = src_size;
750 if(src_fmt == CF_TEXT )
751 CharToOemBuffA(src, dst, dst_size);
753 OemToCharBuffA(src, dst, dst_size);
759 /**************************************************************************
760 * X11DRV_CLIPBOARD_RenderSynthesizedFormat
762 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(Display *display, LPWINE_CLIPDATA lpData)
768 if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
770 UINT wFormatID = lpData->wFormatID;
772 if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
773 bret = X11DRV_CLIPBOARD_RenderSynthesizedText(display, wFormatID);
779 bret = X11DRV_CLIPBOARD_RenderSynthesizedDIB( display );
783 bret = X11DRV_CLIPBOARD_RenderSynthesizedBitmap( display );
787 bret = X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile( display );
790 case CF_METAFILEPICT:
791 FIXME("Synthesizing CF_METAFILEPICT not implemented\n");
795 FIXME("Called to synthesize unknown format 0x%08x\n", wFormatID);
800 lpData->wFlags &= ~CF_FLAG_SYNTHESIZED;
807 /**************************************************************************
808 * X11DRV_CLIPBOARD_RenderSynthesizedText
810 * Renders synthesized text
812 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(Display *display, UINT wFormatID)
817 INT src_chars, dst_chars, alloc_size;
818 LPWINE_CLIPDATA lpSource = NULL;
820 TRACE("%04x\n", wFormatID);
822 if ((lpSource = X11DRV_CLIPBOARD_LookupData(wFormatID)) &&
826 /* Look for rendered source or non-synthesized source */
827 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
828 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData))
830 TRACE("UNICODETEXT -> %04x\n", wFormatID);
832 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
833 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData))
835 TRACE("TEXT -> %04x\n", wFormatID);
837 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
838 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData))
840 TRACE("OEMTEXT -> %04x\n", wFormatID);
843 if (!lpSource || (lpSource->wFlags & CF_FLAG_SYNTHESIZED &&
847 /* Ask the clipboard owner to render the source text if necessary */
848 if (!lpSource->hData && !X11DRV_CLIPBOARD_RenderFormat(display, lpSource))
851 lpstrS = GlobalLock(lpSource->hData);
855 /* Text always NULL terminated */
856 if(lpSource->wFormatID == CF_UNICODETEXT)
857 src_chars = strlenW((LPCWSTR)lpstrS) + 1;
859 src_chars = strlen(lpstrS) + 1;
861 /* Calculate number of characters in the destination buffer */
862 dst_chars = CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS,
863 src_chars, wFormatID, NULL, 0);
868 TRACE("Converting from '%04x' to '%04x', %i chars\n",
869 lpSource->wFormatID, wFormatID, src_chars);
871 /* Convert characters to bytes */
872 if(wFormatID == CF_UNICODETEXT)
873 alloc_size = dst_chars * sizeof(WCHAR);
875 alloc_size = dst_chars;
877 hData = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE |
878 GMEM_DDESHARE, alloc_size);
880 lpstrT = GlobalLock(hData);
884 CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS, src_chars,
885 wFormatID, lpstrT, dst_chars);
889 GlobalUnlock(lpSource->hData);
891 return X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, hData, 0, NULL, TRUE);
895 /***********************************************************************
898 * Return the size of the bitmap info structure including color table.
900 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
902 unsigned int colors, size, masks = 0;
904 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
906 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
907 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
908 return sizeof(BITMAPCOREHEADER) + colors *
909 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
911 else /* assume BITMAPINFOHEADER */
913 colors = info->bmiHeader.biClrUsed;
914 if (!colors && (info->bmiHeader.biBitCount <= 8))
915 colors = 1 << info->bmiHeader.biBitCount;
916 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
917 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
918 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
923 /***********************************************************************
924 * create_dib_from_bitmap
926 * Allocates a packed DIB and copies the bitmap data into it.
928 static HGLOBAL create_dib_from_bitmap(HBITMAP hBmp)
934 LPBITMAPINFOHEADER pbmiHeader;
935 unsigned int cDataSize, cPackedSize, OffsetBits;
938 if (!GetObjectW( hBmp, sizeof(bmp), &bmp )) return 0;
941 * A packed DIB contains a BITMAPINFO structure followed immediately by
942 * an optional color palette and the pixel data.
945 /* Calculate the size of the packed DIB */
946 cDataSize = abs( bmp.bmHeight ) * (((bmp.bmWidth * bmp.bmBitsPixel + 31) / 8) & ~3);
947 cPackedSize = sizeof(BITMAPINFOHEADER)
948 + ( (bmp.bmBitsPixel <= 8) ? (sizeof(RGBQUAD) * (1 << bmp.bmBitsPixel)) : 0 )
950 /* Get the offset to the bits */
951 OffsetBits = cPackedSize - cDataSize;
953 /* Allocate the packed DIB */
954 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
955 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
959 WARN("Could not allocate packed DIB!\n");
963 /* A packed DIB starts with a BITMAPINFOHEADER */
964 pPackedDIB = GlobalLock(hPackedDIB);
965 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
967 /* Init the BITMAPINFOHEADER */
968 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
969 pbmiHeader->biWidth = bmp.bmWidth;
970 pbmiHeader->biHeight = bmp.bmHeight;
971 pbmiHeader->biPlanes = 1;
972 pbmiHeader->biBitCount = bmp.bmBitsPixel;
973 pbmiHeader->biCompression = BI_RGB;
974 pbmiHeader->biSizeImage = 0;
975 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
976 pbmiHeader->biClrUsed = 0;
977 pbmiHeader->biClrImportant = 0;
979 /* Retrieve the DIB bits from the bitmap and fill in the
980 * DIB color table if present */
982 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
983 hBmp, /* Handle to bitmap */
984 0, /* First scan line to set in dest bitmap */
985 bmp.bmHeight, /* Number of scan lines to copy */
986 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
987 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
988 0); /* RGB or palette index */
989 GlobalUnlock(hPackedDIB);
992 /* Cleanup if GetDIBits failed */
993 if (nLinesCopied != bmp.bmHeight)
995 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, bmp.bmHeight);
996 GlobalFree(hPackedDIB);
1003 /**************************************************************************
1004 * X11DRV_CLIPBOARD_RenderSynthesizedDIB
1006 * Renders synthesized DIB
1008 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedDIB(Display *display)
1011 LPWINE_CLIPDATA lpSource = NULL;
1015 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) && lpSource->hData)
1019 /* If we have a bitmap and it's not synthesized or it has been rendered */
1020 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) &&
1021 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData))
1023 /* Render source if required */
1024 if (lpSource->hData || X11DRV_CLIPBOARD_RenderFormat(display, lpSource))
1026 HGLOBAL hData = create_dib_from_bitmap( lpSource->hData );
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);
1260 ret = XmbTextPropertyToTextList(display, &txtprop, &srcstr, &count);
1261 HeapFree(GetProcessHeap(), 0, txtprop.value);
1262 if (ret != Success || !count) return 0;
1264 TRACE("Importing %d line(s)\n", count);
1266 /* Compute number of lines */
1267 srclen = strlen(srcstr[0]);
1268 for (i = 0, lcount = 0; i <= srclen; i++)
1270 if (srcstr[0][i] == '\n')
1274 destlen = MultiByteToWideChar(CP_UNIXCP, 0, srcstr[0], -1, NULL, 0);
1276 TRACE("lcount = %d, destlen=%d, srcstr %s\n", lcount, destlen, srcstr[0]);
1278 if ((hUnicodeText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (destlen + lcount + 1) * sizeof(WCHAR))))
1280 WCHAR *deststr = GlobalLock(hUnicodeText);
1281 MultiByteToWideChar(CP_UNIXCP, 0, srcstr[0], -1, deststr, destlen);
1285 for (i = destlen - 1, j = destlen + lcount - 1; i >= 0; i--, j--)
1287 deststr[j] = deststr[i];
1289 if (deststr[i] == '\n')
1290 deststr[--j] = '\r';
1294 GlobalUnlock(hUnicodeText);
1297 XFreeStringList(srcstr);
1299 return hUnicodeText;
1303 /**************************************************************************
1304 * X11DRV_CLIPBOARD_ImportXAPIXMAP
1306 * Import XA_PIXMAP, converting the image to CF_DIB.
1308 static HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(Display *display, Window w, Atom prop)
1311 unsigned long cbytes;
1313 HANDLE hClipData = 0;
1315 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1317 XVisualInfo vis = default_visual;
1318 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1319 BITMAPINFO *info = (BITMAPINFO *)buffer;
1320 struct gdi_image_bits bits;
1322 int x,y; /* Unused */
1323 unsigned border_width; /* Unused */
1324 unsigned int depth, width, height;
1326 pPixmap = (Pixmap *) lpdata;
1328 /* Get the Pixmap dimensions and bit depth */
1329 if (!XGetGeometry(gdi_display, *pPixmap, &root, &x, &y, &width, &height,
1330 &border_width, &depth)) depth = 0;
1331 if (!pixmap_formats[depth]) return 0;
1333 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
1334 width, height, depth);
1336 if (depth != vis.depth) switch (pixmap_formats[depth]->bits_per_pixel)
1342 case 16: /* assume R5G5B5 */
1343 vis.red_mask = 0x7c00;
1344 vis.green_mask = 0x03e0;
1345 vis.blue_mask = 0x001f;
1347 case 24: /* assume R8G8B8 */
1348 case 32: /* assume A8R8G8B8 */
1349 vis.red_mask = 0xff0000;
1350 vis.green_mask = 0x00ff00;
1351 vis.blue_mask = 0x0000ff;
1357 if (!get_pixmap_image( *pPixmap, width, height, &vis, info, &bits ))
1359 DWORD info_size = bitmap_info_size( info, DIB_RGB_COLORS );
1362 hClipData = GlobalAlloc( GMEM_MOVEABLE | GMEM_DDESHARE,
1363 info_size + info->bmiHeader.biSizeImage );
1366 ptr = GlobalLock( hClipData );
1367 memcpy( ptr, info, info_size );
1368 memcpy( ptr + info_size, bits.ptr, info->bmiHeader.biSizeImage );
1369 GlobalUnlock( hClipData );
1371 if (bits.free) bits.free( &bits );
1374 HeapFree(GetProcessHeap(), 0, lpdata);
1381 /**************************************************************************
1382 * X11DRV_CLIPBOARD_ImportImageBmp
1384 * Import image/bmp, converting the image to CF_DIB.
1386 static HANDLE X11DRV_CLIPBOARD_ImportImageBmp(Display *display, Window w, Atom prop)
1389 unsigned long cbytes;
1390 HANDLE hClipData = 0;
1392 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1394 BITMAPFILEHEADER *bfh = (BITMAPFILEHEADER*)lpdata;
1396 if (cbytes >= sizeof(BITMAPFILEHEADER)+sizeof(BITMAPCOREHEADER) &&
1397 bfh->bfType == 0x4d42 /* "BM" */)
1399 BITMAPINFO *bmi = (BITMAPINFO*)(bfh+1);
1404 hbmp = CreateDIBitmap(
1408 lpdata+bfh->bfOffBits,
1413 hClipData = create_dib_from_bitmap( hbmp );
1419 /* Free the retrieved property data */
1420 HeapFree(GetProcessHeap(), 0, lpdata);
1427 /**************************************************************************
1428 * X11DRV_CLIPBOARD_ImportMetaFilePict
1430 * Import MetaFilePict.
1432 static HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(Display *display, Window w, Atom prop)
1435 unsigned long cbytes;
1436 HANDLE hClipData = 0;
1438 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1441 hClipData = X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, lpdata, (LPDWORD)&cbytes, FALSE);
1443 /* Free the retrieved property data */
1444 HeapFree(GetProcessHeap(), 0, lpdata);
1451 /**************************************************************************
1452 * X11DRV_ImportEnhMetaFile
1454 * Import EnhMetaFile.
1456 static HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(Display *display, Window w, Atom prop)
1459 unsigned long cbytes;
1460 HANDLE hClipData = 0;
1462 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1465 hClipData = X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, lpdata, (LPDWORD)&cbytes, FALSE);
1467 /* Free the retrieved property data */
1468 HeapFree(GetProcessHeap(), 0, lpdata);
1475 /**************************************************************************
1476 * X11DRV_ImportClipbordaData
1478 * Generic import clipboard data routine.
1480 static HANDLE X11DRV_CLIPBOARD_ImportClipboardData(Display *display, Window w, Atom prop)
1484 unsigned long cbytes;
1485 HANDLE hClipData = 0;
1487 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1491 /* Turn on the DDESHARE flag to enable shared 32 bit memory */
1492 hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cbytes);
1495 HeapFree(GetProcessHeap(), 0, lpdata);
1499 if ((lpClipData = GlobalLock(hClipData)))
1501 memcpy(lpClipData, lpdata, cbytes);
1502 GlobalUnlock(hClipData);
1506 GlobalFree(hClipData);
1511 /* Free the retrieved property data */
1512 HeapFree(GetProcessHeap(), 0, lpdata);
1519 /**************************************************************************
1520 X11DRV_CLIPBOARD_ExportClipboardData
1522 * Generic export clipboard data routine.
1524 static HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Display *display, Window requestor, Atom aTarget,
1525 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1529 HANDLE hClipData = 0;
1531 *lpBytes = 0; /* Assume failure */
1533 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpData))
1534 ERR("Failed to export %04x format\n", lpData->wFormatID);
1537 datasize = GlobalSize(lpData->hData);
1539 hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, datasize);
1540 if (hClipData == 0) return NULL;
1542 if ((lpClipData = GlobalLock(hClipData)))
1544 LPVOID lpdata = GlobalLock(lpData->hData);
1546 memcpy(lpClipData, lpdata, datasize);
1547 *lpBytes = datasize;
1549 GlobalUnlock(lpData->hData);
1550 GlobalUnlock(hClipData);
1552 GlobalFree(hClipData);
1561 /**************************************************************************
1562 * X11DRV_CLIPBOARD_ExportXAString
1564 * Export CF_TEXT converting the string to XA_STRING.
1565 * Helper function for X11DRV_CLIPBOARD_ExportString.
1567 static HANDLE X11DRV_CLIPBOARD_ExportXAString(LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1571 LPSTR text, lpstr = NULL;
1573 *lpBytes = 0; /* Assume return has zero bytes */
1575 text = GlobalLock(lpData->hData);
1576 size = strlen(text);
1578 /* remove carriage returns */
1579 lpstr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size + 1);
1583 for (i = 0,j = 0; i < size && text[i]; i++)
1585 if (text[i] == '\r' && (text[i+1] == '\n' || text[i+1] == '\0'))
1587 lpstr[j++] = text[i];
1591 *lpBytes = j; /* Number of bytes in string */
1594 GlobalUnlock(lpData->hData);
1600 /**************************************************************************
1601 * X11DRV_CLIPBOARD_ExportUTF8String
1603 * Export CF_UNICODE converting the string to UTF8.
1604 * Helper function for X11DRV_CLIPBOARD_ExportString.
1606 static HANDLE X11DRV_CLIPBOARD_ExportUTF8String(LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1611 LPSTR text, lpstr = NULL;
1613 *lpBytes = 0; /* Assume return has zero bytes */
1615 uni_text = GlobalLock(lpData->hData);
1617 size = WideCharToMultiByte(CP_UTF8, 0, uni_text, -1, NULL, 0, NULL, NULL);
1619 text = HeapAlloc(GetProcessHeap(), 0, size);
1622 WideCharToMultiByte(CP_UTF8, 0, uni_text, -1, text, size, NULL, NULL);
1624 /* remove carriage returns */
1625 lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size--);
1629 for (i = 0,j = 0; i < size && text[i]; i++)
1631 if (text[i] == '\r' && (text[i+1] == '\n' || text[i+1] == '\0'))
1633 lpstr[j++] = text[i];
1637 *lpBytes = j; /* Number of bytes in string */
1640 HeapFree(GetProcessHeap(), 0, text);
1641 GlobalUnlock(lpData->hData);
1648 /**************************************************************************
1649 * X11DRV_CLIPBOARD_ExportCompoundText
1651 * Export CF_UNICODE to COMPOUND_TEXT
1652 * Helper function for X11DRV_CLIPBOARD_ExportString.
1654 static HANDLE X11DRV_CLIPBOARD_ExportCompoundText(Display *display, Window requestor, Atom aTarget, Atom rprop,
1655 LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1659 XICCEncodingStyle style;
1664 uni_text = GlobalLock(lpData->hData);
1666 size = WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, NULL, 0, NULL, NULL);
1667 lpstr = HeapAlloc(GetProcessHeap(), 0, size);
1671 WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, lpstr, size, NULL, NULL);
1673 /* remove carriage returns */
1674 for (i = 0, j = 0; i < size && lpstr[i]; i++)
1676 if (lpstr[i] == '\r' && (lpstr[i+1] == '\n' || lpstr[i+1] == '\0'))
1678 lpstr[j++] = lpstr[i];
1682 GlobalUnlock(lpData->hData);
1684 if (aTarget == x11drv_atom(COMPOUND_TEXT))
1685 style = XCompoundTextStyle;
1687 style = XStdICCTextStyle;
1689 /* Update the X property */
1690 if (XmbTextListToTextProperty(display, &lpstr, 1, style, &prop) == Success)
1692 XSetTextProperty(display, requestor, &prop, rprop);
1696 HeapFree(GetProcessHeap(), 0, lpstr);
1701 /**************************************************************************
1702 * X11DRV_CLIPBOARD_ExportString
1706 static HANDLE X11DRV_CLIPBOARD_ExportString(Display *display, Window requestor, Atom aTarget, Atom rprop,
1707 LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1709 if (X11DRV_CLIPBOARD_RenderFormat(display, lpData))
1711 if (aTarget == XA_STRING)
1712 return X11DRV_CLIPBOARD_ExportXAString(lpData, lpBytes);
1713 else if (aTarget == x11drv_atom(COMPOUND_TEXT) || aTarget == x11drv_atom(TEXT))
1714 return X11DRV_CLIPBOARD_ExportCompoundText(display, requestor, aTarget,
1715 rprop, lpData, lpBytes);
1718 TRACE("Exporting target %ld to default UTF8_STRING\n", aTarget);
1719 return X11DRV_CLIPBOARD_ExportUTF8String(lpData, lpBytes);
1723 ERR("Failed to render %04x format\n", lpData->wFormatID);
1729 /**************************************************************************
1730 * X11DRV_CLIPBOARD_ExportXAPIXMAP
1732 * Export CF_DIB to XA_PIXMAP.
1734 static HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Display *display, Window requestor, Atom aTarget, Atom rprop,
1735 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1738 unsigned char* lpData;
1740 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1742 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1746 if (!lpdata->drvData) /* If not already rendered */
1750 struct gdi_image_bits bits;
1752 pbmi = GlobalLock( lpdata->hData );
1753 bits.ptr = (LPBYTE)pbmi + bitmap_info_size( pbmi, DIB_RGB_COLORS );
1755 bits.is_copy = FALSE;
1756 pixmap = create_pixmap_from_image( 0, &default_visual, pbmi, &bits, DIB_RGB_COLORS );
1757 GlobalUnlock( lpdata->hData );
1758 lpdata->drvData = pixmap;
1761 *lpBytes = sizeof(Pixmap); /* pixmap is a 32bit value */
1763 /* Wrap pixmap so we can return a handle */
1764 hData = GlobalAlloc(0, *lpBytes);
1765 lpData = GlobalLock(hData);
1766 memcpy(lpData, &lpdata->drvData, *lpBytes);
1767 GlobalUnlock(hData);
1773 /**************************************************************************
1774 * X11DRV_CLIPBOARD_ExportImageBmp
1776 * Export CF_DIB to image/bmp.
1778 static HANDLE X11DRV_CLIPBOARD_ExportImageBmp(Display *display, Window requestor, Atom aTarget, Atom rprop,
1779 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1786 BITMAPFILEHEADER *bfh;
1790 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1792 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1796 hpackeddib = lpdata->hData;
1798 dibdata = GlobalLock(hpackeddib);
1801 ERR("Failed to lock packed DIB\n");
1805 bmpsize = sizeof(BITMAPFILEHEADER) + GlobalSize(hpackeddib);
1807 hbmpdata = GlobalAlloc(0, bmpsize);
1811 bmpdata = GlobalLock(hbmpdata);
1815 GlobalFree(hbmpdata);
1816 GlobalUnlock(hpackeddib);
1820 /* bitmap file header */
1821 bfh = (BITMAPFILEHEADER*)bmpdata;
1822 bfh->bfType = 0x4d42; /* "BM" */
1823 bfh->bfSize = bmpsize;
1824 bfh->bfReserved1 = 0;
1825 bfh->bfReserved2 = 0;
1826 bfh->bfOffBits = sizeof(BITMAPFILEHEADER) + bitmap_info_size((BITMAPINFO*)dibdata, DIB_RGB_COLORS);
1828 /* rest of bitmap is the same as the packed dib */
1829 memcpy(bfh+1, dibdata, bmpsize-sizeof(BITMAPFILEHEADER));
1833 GlobalUnlock(hbmpdata);
1836 GlobalUnlock(hpackeddib);
1842 /**************************************************************************
1843 * X11DRV_CLIPBOARD_ExportMetaFilePict
1845 * Export MetaFilePict.
1847 static HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Display *display, Window requestor, Atom aTarget, Atom rprop,
1848 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1850 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1852 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1856 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, lpdata->hData, lpBytes, TRUE);
1860 /**************************************************************************
1861 * X11DRV_CLIPBOARD_ExportEnhMetaFile
1863 * Export EnhMetaFile.
1865 static HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Display *display, Window requestor, Atom aTarget, Atom rprop,
1866 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1868 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1870 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1874 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, lpdata->hData, lpBytes, TRUE);
1878 /**************************************************************************
1879 * get_html_description_field
1881 * Find the value of a field in an HTML Format description.
1883 static LPCSTR get_html_description_field(LPCSTR data, LPCSTR keyword)
1887 while (pos && *pos && *pos != '<')
1889 if (memcmp(pos, keyword, strlen(keyword)) == 0)
1890 return pos+strlen(keyword);
1892 pos = strchr(pos, '\n');
1900 /**************************************************************************
1901 * X11DRV_CLIPBOARD_ExportTextHtml
1903 * Export HTML Format to text/html.
1905 * FIXME: We should attempt to add an <a base> tag and convert windows paths.
1907 static HANDLE X11DRV_CLIPBOARD_ExportTextHtml(Display *display, Window requestor, Atom aTarget,
1908 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1911 LPCSTR data, field_value;
1912 UINT fragmentstart, fragmentend, htmlsize;
1913 HANDLE hhtmldata=NULL;
1918 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1920 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1924 hdata = lpdata->hData;
1926 data = GlobalLock(hdata);
1929 ERR("Failed to lock HTML Format data\n");
1933 /* read the important fields */
1934 field_value = get_html_description_field(data, "StartFragment:");
1937 ERR("Couldn't find StartFragment value\n");
1940 fragmentstart = atoi(field_value);
1942 field_value = get_html_description_field(data, "EndFragment:");
1945 ERR("Couldn't find EndFragment value\n");
1948 fragmentend = atoi(field_value);
1950 /* export only the fragment */
1951 htmlsize = fragmentend - fragmentstart + 1;
1953 hhtmldata = GlobalAlloc(0, htmlsize);
1957 htmldata = GlobalLock(hhtmldata);
1961 GlobalFree(hhtmldata);
1966 memcpy(htmldata, &data[fragmentstart], fragmentend-fragmentstart);
1967 htmldata[htmlsize-1] = '\0';
1969 *lpBytes = htmlsize;
1971 GlobalUnlock(htmldata);
1976 GlobalUnlock(hdata);
1982 /**************************************************************************
1983 * X11DRV_CLIPBOARD_QueryTargets
1985 static BOOL X11DRV_CLIPBOARD_QueryTargets(Display *display, Window w, Atom selection,
1986 Atom target, XEvent *xe)
1990 XConvertSelection(display, selection, target, x11drv_atom(SELECTION_DATA), w, CurrentTime);
1993 * Wait until SelectionNotify is received
1995 for (i = 0; i < SELECTION_RETRIES; i++)
1997 Bool res = XCheckTypedWindowEvent(display, w, SelectionNotify, xe);
1998 if (res && xe->xselection.selection == selection) break;
2000 usleep(SELECTION_WAIT);
2003 if (i == SELECTION_RETRIES)
2005 ERR("Timed out waiting for SelectionNotify event\n");
2008 /* Verify that the selection returned a valid TARGETS property */
2009 if ((xe->xselection.target != target) || (xe->xselection.property == None))
2011 /* Selection owner failed to respond or we missed the SelectionNotify */
2012 WARN("Failed to retrieve TARGETS for selection %ld.\n", selection);
2020 static int is_atom_error( Display *display, XErrorEvent *event, void *arg )
2022 return (event->error_code == BadAtom);
2025 /**************************************************************************
2026 * X11DRV_CLIPBOARD_InsertSelectionProperties
2028 * Mark properties available for future retrieval.
2030 static VOID X11DRV_CLIPBOARD_InsertSelectionProperties(Display *display, Atom* properties, UINT count)
2032 UINT i, nb_atoms = 0;
2035 /* Cache these formats in the clipboard cache */
2036 for (i = 0; i < count; i++)
2038 LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupProperty(NULL, properties[i]);
2042 /* We found at least one Window's format that mapps to the property.
2043 * Continue looking for more.
2045 * If more than one property map to a Window's format then we use the first
2046 * one and ignore the rest.
2050 TRACE("Atom#%d Property(%d): --> Format %s\n",
2051 i, lpFormat->drvData, debugstr_format(lpFormat->wFormatID));
2052 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, lpFormat, FALSE);
2053 lpFormat = X11DRV_CLIPBOARD_LookupProperty(lpFormat, properties[i]);
2056 else if (properties[i])
2058 /* add it to the list of atoms that we don't know about yet */
2059 if (!atoms) atoms = HeapAlloc( GetProcessHeap(), 0,
2060 (count - i) * sizeof(*atoms) );
2061 if (atoms) atoms[nb_atoms++] = properties[i];
2065 /* query all unknown atoms in one go */
2068 char **names = HeapAlloc( GetProcessHeap(), 0, nb_atoms * sizeof(*names) );
2071 X11DRV_expect_error( display, is_atom_error, NULL );
2072 if (!XGetAtomNames( display, atoms, nb_atoms, names )) nb_atoms = 0;
2073 if (X11DRV_check_error())
2075 WARN( "got some bad atoms, ignoring\n" );
2078 for (i = 0; i < nb_atoms; i++)
2080 WINE_CLIPFORMAT *lpFormat;
2082 int len = MultiByteToWideChar(CP_UNIXCP, 0, names[i], -1, NULL, 0);
2083 wname = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
2084 MultiByteToWideChar(CP_UNIXCP, 0, names[i], -1, wname, len);
2086 lpFormat = register_format( RegisterClipboardFormatW(wname), atoms[i] );
2087 HeapFree(GetProcessHeap(), 0, wname);
2090 ERR("Failed to register %s property. Type will not be cached.\n", names[i]);
2093 TRACE("Atom#%d Property(%d): --> Format %s\n",
2094 i, lpFormat->drvData, debugstr_format(lpFormat->wFormatID));
2095 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, lpFormat, FALSE);
2097 for (i = 0; i < nb_atoms; i++) XFree( names[i] );
2098 HeapFree( GetProcessHeap(), 0, names );
2100 HeapFree( GetProcessHeap(), 0, atoms );
2105 /**************************************************************************
2106 * X11DRV_CLIPBOARD_QueryAvailableData
2108 * Caches the list of data formats available from the current selection.
2109 * This queries the selection owner for the TARGETS property and saves all
2110 * reported property types.
2112 static int X11DRV_CLIPBOARD_QueryAvailableData(Display *display, LPCLIPBOARDINFO lpcbinfo)
2115 Atom atype=AnyPropertyType;
2117 unsigned long remain;
2118 Atom* targetList=NULL;
2120 unsigned long cSelectionTargets = 0;
2122 if (selectionAcquired & (S_PRIMARY | S_CLIPBOARD))
2124 ERR("Received request to cache selection but process is owner=(%08x)\n",
2125 (unsigned) selectionWindow);
2126 return -1; /* Prevent self request */
2129 w = thread_selection_wnd();
2132 ERR("No window available to retrieve selection!\n");
2137 * Query the selection owner for the TARGETS property
2139 if ((use_primary_selection && XGetSelectionOwner(display,XA_PRIMARY)) ||
2140 XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
2142 if (use_primary_selection && (X11DRV_CLIPBOARD_QueryTargets(display, w, XA_PRIMARY, x11drv_atom(TARGETS), &xe)))
2143 selectionCacheSrc = XA_PRIMARY;
2144 else if (X11DRV_CLIPBOARD_QueryTargets(display, w, x11drv_atom(CLIPBOARD), x11drv_atom(TARGETS), &xe))
2145 selectionCacheSrc = x11drv_atom(CLIPBOARD);
2148 Atom xstr = XA_STRING;
2150 /* Selection Owner doesn't understand TARGETS, try retrieving XA_STRING */
2151 if (X11DRV_CLIPBOARD_QueryTargets(display, w, XA_PRIMARY, XA_STRING, &xe))
2153 X11DRV_CLIPBOARD_InsertSelectionProperties(display, &xstr, 1);
2154 selectionCacheSrc = XA_PRIMARY;
2157 else if (X11DRV_CLIPBOARD_QueryTargets(display, w, x11drv_atom(CLIPBOARD), XA_STRING, &xe))
2159 X11DRV_CLIPBOARD_InsertSelectionProperties(display, &xstr, 1);
2160 selectionCacheSrc = x11drv_atom(CLIPBOARD);
2165 WARN("Failed to query selection owner for available data.\n");
2170 else return 0; /* No selection owner so report 0 targets available */
2172 /* Read the TARGETS property contents */
2173 if (!XGetWindowProperty(display, xe.xselection.requestor, xe.xselection.property,
2174 0, 0x3FFF, True, AnyPropertyType/*XA_ATOM*/, &atype, &aformat, &cSelectionTargets,
2175 &remain, (unsigned char**)&targetList) != Success)
2177 TRACE("Type %lx,Format %d,nItems %ld, Remain %ld\n",
2178 atype, aformat, cSelectionTargets, remain);
2180 * The TARGETS property should have returned us a list of atoms
2181 * corresponding to each selection target format supported.
2183 if (atype == XA_ATOM || atype == x11drv_atom(TARGETS))
2187 X11DRV_CLIPBOARD_InsertSelectionProperties(display, targetList, cSelectionTargets);
2189 else if (aformat == 8) /* work around quartz-wm brain damage */
2191 unsigned long i, count = cSelectionTargets / sizeof(CARD32);
2192 Atom *atoms = HeapAlloc( GetProcessHeap(), 0, count * sizeof(Atom) );
2193 for (i = 0; i < count; i++)
2194 atoms[i] = ((CARD32 *)targetList)[i]; /* FIXME: byte swapping */
2195 X11DRV_CLIPBOARD_InsertSelectionProperties( display, atoms, count );
2196 HeapFree( GetProcessHeap(), 0, atoms );
2200 /* Free the list of targets */
2203 else WARN("Failed to read TARGETS property\n");
2205 return cSelectionTargets;
2209 /**************************************************************************
2210 * X11DRV_CLIPBOARD_ReadSelectionData
2212 * This method is invoked only when we DO NOT own the X selection
2214 * We always get the data from the selection client each time,
2215 * since we have no way of determining if the data in our cache is stale.
2217 static BOOL X11DRV_CLIPBOARD_ReadSelectionData(Display *display, LPWINE_CLIPDATA lpData)
2224 TRACE("%04x\n", lpData->wFormatID);
2226 if (!lpData->lpFormat)
2228 ERR("Requesting format %04x but no source format linked to data.\n",
2233 if (!selectionAcquired)
2235 Window w = thread_selection_wnd();
2238 ERR("No window available to read selection data!\n");
2242 TRACE("Requesting conversion of %s property (%d) from selection type %08x\n",
2243 debugstr_format(lpData->lpFormat->wFormatID), lpData->lpFormat->drvData,
2244 (UINT)selectionCacheSrc);
2246 XConvertSelection(display, selectionCacheSrc, lpData->lpFormat->drvData,
2247 x11drv_atom(SELECTION_DATA), w, CurrentTime);
2249 /* wait until SelectionNotify is received */
2250 for (i = 0; i < SELECTION_RETRIES; i++)
2252 res = XCheckTypedWindowEvent(display, w, SelectionNotify, &xe);
2253 if (res && xe.xselection.selection == selectionCacheSrc) break;
2255 usleep(SELECTION_WAIT);
2258 if (i == SELECTION_RETRIES)
2260 ERR("Timed out waiting for SelectionNotify event\n");
2262 /* Verify that the selection returned a valid TARGETS property */
2263 else if (xe.xselection.property != None)
2266 * Read the contents of the X selection property
2267 * into WINE's clipboard cache and converting the
2268 * data format if necessary.
2270 HANDLE hData = lpData->lpFormat->lpDrvImportFunc(display, xe.xselection.requestor,
2271 xe.xselection.property);
2274 bRet = X11DRV_CLIPBOARD_InsertClipboardData(lpData->wFormatID, hData, 0, lpData->lpFormat, TRUE);
2276 TRACE("Import function failed\n");
2280 TRACE("Failed to convert selection\n");
2285 ERR("Received request to cache selection data but process is owner\n");
2288 TRACE("Returning %d\n", bRet);
2294 /**************************************************************************
2295 * X11DRV_CLIPBOARD_GetProperty
2296 * Gets type, data and size.
2298 static BOOL X11DRV_CLIPBOARD_GetProperty(Display *display, Window w, Atom prop,
2299 Atom *atype, unsigned char** data, unsigned long* datasize)
2302 unsigned long pos = 0, nitems, remain, count;
2303 unsigned char *val = NULL, *buffer;
2305 TRACE("Reading property %lu from X window %lx\n", prop, w);
2309 if (XGetWindowProperty(display, w, prop, pos, INT_MAX / 4, False,
2310 AnyPropertyType, atype, &aformat, &nitems, &remain, &buffer) != Success)
2312 WARN("Failed to read property\n");
2313 HeapFree( GetProcessHeap(), 0, val );
2317 count = get_property_size( aformat, nitems );
2318 if (!val) *data = HeapAlloc( GetProcessHeap(), 0, pos * sizeof(int) + count + 1 );
2319 else *data = HeapReAlloc( GetProcessHeap(), 0, val, pos * sizeof(int) + count + 1 );
2324 HeapFree( GetProcessHeap(), 0, val );
2328 memcpy( (int *)val + pos, buffer, count );
2332 *datasize = pos * sizeof(int) + count;
2336 pos += count / sizeof(int);
2339 /* Delete the property on the window now that we are done
2340 * This will send a PropertyNotify event to the selection owner. */
2341 XDeleteProperty(display, w, prop);
2346 /**************************************************************************
2347 * X11DRV_CLIPBOARD_ReadProperty
2348 * Reads the contents of the X selection property.
2350 static BOOL X11DRV_CLIPBOARD_ReadProperty(Display *display, Window w, Atom prop,
2351 unsigned char** data, unsigned long* datasize)
2359 if (!X11DRV_CLIPBOARD_GetProperty(display, w, prop, &atype, data, datasize))
2362 while (XCheckTypedWindowEvent(display, w, PropertyNotify, &xe))
2365 if (atype == x11drv_atom(INCR))
2367 unsigned char *buf = *data;
2368 unsigned long bufsize = 0;
2373 unsigned char *prop_data, *tmp;
2374 unsigned long prop_size;
2376 /* Wait until PropertyNotify is received */
2377 for (i = 0; i < SELECTION_RETRIES; i++)
2381 res = XCheckTypedWindowEvent(display, w, PropertyNotify, &xe);
2382 if (res && xe.xproperty.atom == prop &&
2383 xe.xproperty.state == PropertyNewValue)
2385 usleep(SELECTION_WAIT);
2388 if (i >= SELECTION_RETRIES ||
2389 !X11DRV_CLIPBOARD_GetProperty(display, w, prop, &atype, &prop_data, &prop_size))
2391 HeapFree(GetProcessHeap(), 0, buf);
2395 /* Retrieved entire data. */
2398 HeapFree(GetProcessHeap(), 0, prop_data);
2400 *datasize = bufsize;
2404 tmp = HeapReAlloc(GetProcessHeap(), 0, buf, bufsize + prop_size + 1);
2407 HeapFree(GetProcessHeap(), 0, buf);
2408 HeapFree(GetProcessHeap(), 0, prop_data);
2413 memcpy(buf + bufsize, prop_data, prop_size + 1);
2414 bufsize += prop_size;
2415 HeapFree(GetProcessHeap(), 0, prop_data);
2423 /**************************************************************************
2424 * CLIPBOARD_SerializeMetafile
2426 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out)
2430 TRACE(" wFormat=%d hdata=%p out=%d\n", wformat, hdata, out);
2432 if (out) /* Serialize out, caller should free memory */
2434 *lpcbytes = 0; /* Assume failure */
2436 if (wformat == CF_METAFILEPICT)
2438 LPMETAFILEPICT lpmfp = GlobalLock(hdata);
2439 unsigned int size = GetMetaFileBitsEx(lpmfp->hMF, 0, NULL);
2441 h = GlobalAlloc(0, size + sizeof(METAFILEPICT));
2444 char *pdata = GlobalLock(h);
2446 memcpy(pdata, lpmfp, sizeof(METAFILEPICT));
2447 GetMetaFileBitsEx(lpmfp->hMF, size, pdata + sizeof(METAFILEPICT));
2449 *lpcbytes = size + sizeof(METAFILEPICT);
2454 GlobalUnlock(hdata);
2456 else if (wformat == CF_ENHMETAFILE)
2458 int size = GetEnhMetaFileBits(hdata, 0, NULL);
2460 h = GlobalAlloc(0, size);
2463 LPVOID pdata = GlobalLock(h);
2465 GetEnhMetaFileBits(hdata, size, pdata);
2474 if (wformat == CF_METAFILEPICT)
2476 h = GlobalAlloc(0, sizeof(METAFILEPICT));
2479 unsigned int wiresize;
2480 LPMETAFILEPICT lpmfp = GlobalLock(h);
2482 memcpy(lpmfp, hdata, sizeof(METAFILEPICT));
2483 wiresize = *lpcbytes - sizeof(METAFILEPICT);
2484 lpmfp->hMF = SetMetaFileBitsEx(wiresize,
2485 ((const BYTE *)hdata) + sizeof(METAFILEPICT));
2489 else if (wformat == CF_ENHMETAFILE)
2491 h = SetEnhMetaFileBits(*lpcbytes, hdata);
2499 /**************************************************************************
2500 * X11DRV_CLIPBOARD_ReleaseSelection
2502 * Release XA_CLIPBOARD and XA_PRIMARY in response to a SelectionClear event.
2504 static void X11DRV_CLIPBOARD_ReleaseSelection(Display *display, Atom selType, Window w, HWND hwnd, Time time)
2506 /* w is the window that lost the selection
2508 TRACE("event->window = %08x (selectionWindow = %08x) selectionAcquired=0x%08x\n",
2509 (unsigned)w, (unsigned)selectionWindow, (unsigned)selectionAcquired);
2511 if (selectionAcquired && (w == selectionWindow))
2513 CLIPBOARDINFO cbinfo;
2515 /* completely give up the selection */
2516 TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
2518 X11DRV_CLIPBOARD_GetClipboardInfo(&cbinfo);
2520 if (cbinfo.flags & CB_PROCESS)
2522 /* Since we're still the owner, this wasn't initiated by
2523 another Wine process */
2524 if (OpenClipboard(hwnd))
2526 /* Destroy private objects */
2527 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
2529 /* Give up ownership of the windows clipboard */
2530 X11DRV_CLIPBOARD_ReleaseOwnership();
2535 if ((selType == x11drv_atom(CLIPBOARD)) && (selectionAcquired & S_PRIMARY))
2537 TRACE("Lost clipboard. Check if we need to release PRIMARY\n");
2539 if (selectionWindow == XGetSelectionOwner(display, XA_PRIMARY))
2541 TRACE("We still own PRIMARY. Releasing PRIMARY.\n");
2542 XSetSelectionOwner(display, XA_PRIMARY, None, time);
2545 TRACE("We no longer own PRIMARY\n");
2547 else if ((selType == XA_PRIMARY) && (selectionAcquired & S_CLIPBOARD))
2549 TRACE("Lost PRIMARY. Check if we need to release CLIPBOARD\n");
2551 if (selectionWindow == XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
2553 TRACE("We still own CLIPBOARD. Releasing CLIPBOARD.\n");
2554 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), None, time);
2557 TRACE("We no longer own CLIPBOARD\n");
2560 selectionWindow = None;
2562 X11DRV_EmptyClipboard(FALSE);
2564 /* Reset the selection flags now that we are done */
2565 selectionAcquired = S_NOSELECTION;
2570 /**************************************************************************
2571 * IsSelectionOwner (X11DRV.@)
2573 * Returns: TRUE if the selection is owned by this process, FALSE otherwise
2575 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void)
2577 return selectionAcquired;
2581 /**************************************************************************
2582 * X11DRV Clipboard Exports
2583 **************************************************************************/
2586 static void selection_acquire(void)
2591 owner = thread_selection_wnd();
2592 display = thread_display();
2594 selectionAcquired = 0;
2595 selectionWindow = 0;
2597 /* Grab PRIMARY selection if not owned */
2598 if (use_primary_selection)
2599 XSetSelectionOwner(display, XA_PRIMARY, owner, CurrentTime);
2601 /* Grab CLIPBOARD selection if not owned */
2602 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), owner, CurrentTime);
2604 if (use_primary_selection && XGetSelectionOwner(display, XA_PRIMARY) == owner)
2605 selectionAcquired |= S_PRIMARY;
2607 if (XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)) == owner)
2608 selectionAcquired |= S_CLIPBOARD;
2610 if (selectionAcquired)
2612 selectionWindow = owner;
2613 TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner);
2617 static DWORD WINAPI selection_thread_proc(LPVOID p)
2623 selection_acquire();
2626 while (selectionAcquired)
2628 MsgWaitForMultipleObjectsEx(0, NULL, INFINITE, QS_SENDMESSAGE, 0);
2634 /**************************************************************************
2635 * AcquireClipboard (X11DRV.@)
2637 int CDECL X11DRV_AcquireClipboard(HWND hWndClipWindow)
2640 HANDLE selectionThread;
2642 TRACE(" %p\n", hWndClipWindow);
2645 * It's important that the selection get acquired from the thread
2646 * that owns the clipboard window. The primary reason is that we know
2647 * it is running a message loop and therefore can process the
2648 * X selection events.
2650 if (hWndClipWindow &&
2651 GetCurrentThreadId() != GetWindowThreadProcessId(hWndClipWindow, &procid))
2653 if (procid != GetCurrentProcessId())
2655 WARN("Setting clipboard owner to other process is not supported\n");
2656 hWndClipWindow = NULL;
2660 TRACE("Thread %x is acquiring selection with thread %x's window %p\n",
2661 GetCurrentThreadId(),
2662 GetWindowThreadProcessId(hWndClipWindow, NULL), hWndClipWindow);
2664 return SendMessageW(hWndClipWindow, WM_X11DRV_ACQUIRE_SELECTION, 0, 0);
2670 selection_acquire();
2674 HANDLE event = CreateEventW(NULL, FALSE, FALSE, NULL);
2675 selectionThread = CreateThread(NULL, 0, selection_thread_proc, event, 0, NULL);
2677 if (!selectionThread)
2679 WARN("Could not start clipboard thread\n");
2684 WaitForSingleObject(event, INFINITE);
2686 CloseHandle(selectionThread);
2693 /**************************************************************************
2694 * X11DRV_EmptyClipboard
2696 * Empty cached clipboard data.
2698 void CDECL X11DRV_EmptyClipboard(BOOL keepunowned)
2700 WINE_CLIPDATA *data, *next;
2702 LIST_FOR_EACH_ENTRY_SAFE( data, next, &data_list, WINE_CLIPDATA, entry )
2704 if (keepunowned && (data->wFlags & CF_FLAG_UNOWNED)) continue;
2705 list_remove( &data->entry );
2706 X11DRV_CLIPBOARD_FreeData( data );
2707 HeapFree( GetProcessHeap(), 0, data );
2711 TRACE(" %d entries remaining in cache.\n", ClipDataCount);
2716 /**************************************************************************
2717 * X11DRV_SetClipboardData
2719 BOOL CDECL X11DRV_SetClipboardData(UINT wFormat, HANDLE hData, BOOL owner)
2722 BOOL bResult = TRUE;
2724 /* If it's not owned, data can only be set if the format data is not already owned
2725 and its rendering is not delayed */
2728 CLIPBOARDINFO cbinfo;
2729 LPWINE_CLIPDATA lpRender;
2731 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2734 ((lpRender = X11DRV_CLIPBOARD_LookupData(wFormat)) &&
2735 !(lpRender->wFlags & CF_FLAG_UNOWNED)))
2738 flags = CF_FLAG_UNOWNED;
2741 bResult &= X11DRV_CLIPBOARD_InsertClipboardData(wFormat, hData, flags, NULL, TRUE);
2747 /**************************************************************************
2748 * CountClipboardFormats
2750 INT CDECL X11DRV_CountClipboardFormats(void)
2752 CLIPBOARDINFO cbinfo;
2754 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2756 TRACE(" count=%d\n", ClipDataCount);
2758 return ClipDataCount;
2762 /**************************************************************************
2763 * X11DRV_EnumClipboardFormats
2765 UINT CDECL X11DRV_EnumClipboardFormats(UINT wFormat)
2767 CLIPBOARDINFO cbinfo;
2768 struct list *ptr = NULL;
2770 TRACE("(%04X)\n", wFormat);
2772 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2776 ptr = list_head( &data_list );
2780 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormat);
2781 if (lpData) ptr = list_next( &data_list, &lpData->entry );
2785 return LIST_ENTRY( ptr, WINE_CLIPDATA, entry )->wFormatID;
2789 /**************************************************************************
2790 * X11DRV_IsClipboardFormatAvailable
2792 BOOL CDECL X11DRV_IsClipboardFormatAvailable(UINT wFormat)
2795 CLIPBOARDINFO cbinfo;
2797 TRACE("(%04X)\n", wFormat);
2799 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2801 if (wFormat != 0 && X11DRV_CLIPBOARD_LookupData(wFormat))
2804 TRACE("(%04X)- ret(%d)\n", wFormat, bRet);
2810 /**************************************************************************
2811 * GetClipboardData (USER.142)
2813 HANDLE CDECL X11DRV_GetClipboardData(UINT wFormat)
2815 CLIPBOARDINFO cbinfo;
2816 LPWINE_CLIPDATA lpRender;
2818 TRACE("(%04X)\n", wFormat);
2820 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2822 if ((lpRender = X11DRV_CLIPBOARD_LookupData(wFormat)))
2824 if ( !lpRender->hData )
2825 X11DRV_CLIPBOARD_RenderFormat(thread_init_display(), lpRender);
2827 TRACE(" returning %p (type %04x)\n", lpRender->hData, lpRender->wFormatID);
2828 return lpRender->hData;
2835 /**************************************************************************
2836 * ResetSelectionOwner
2838 * Called when the thread owning the selection is destroyed and we need to
2839 * preserve the selection ownership. We look for another top level window
2840 * in this process and send it a message to acquire the selection.
2842 void X11DRV_ResetSelectionOwner(void)
2849 if (!selectionAcquired || thread_selection_wnd() != selectionWindow)
2852 selectionAcquired = S_NOSELECTION;
2853 selectionWindow = 0;
2855 hwnd = GetWindow(GetDesktopWindow(), GW_CHILD);
2858 if (GetCurrentThreadId() != GetWindowThreadProcessId(hwnd, &procid))
2860 if (GetCurrentProcessId() == procid)
2862 if (SendMessageW(hwnd, WM_X11DRV_ACQUIRE_SELECTION, 0, 0))
2866 } while ((hwnd = GetWindow(hwnd, GW_HWNDNEXT)) != NULL);
2868 WARN("Failed to find another thread to take selection ownership. Clipboard data will be lost.\n");
2870 X11DRV_CLIPBOARD_ReleaseOwnership();
2871 X11DRV_EmptyClipboard(FALSE);
2875 /**************************************************************************
2876 * X11DRV_CLIPBOARD_SynthesizeData
2878 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID)
2881 LPWINE_CLIPDATA lpSource = NULL;
2883 TRACE(" %04x\n", wFormatID);
2885 /* Don't need to synthesize if it already exists */
2886 if (X11DRV_CLIPBOARD_LookupData(wFormatID))
2889 if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
2891 bsyn = ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
2892 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2893 ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
2894 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2895 ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
2896 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED);
2898 else if (wFormatID == CF_ENHMETAFILE)
2900 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT)) &&
2901 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2903 else if (wFormatID == CF_METAFILEPICT)
2905 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_ENHMETAFILE)) &&
2906 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2908 else if (wFormatID == CF_DIB)
2910 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) &&
2911 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2913 else if (wFormatID == CF_BITMAP)
2915 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) &&
2916 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2920 X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, 0, CF_FLAG_SYNTHESIZED, NULL, TRUE);
2927 /**************************************************************************
2928 * X11DRV_EndClipboardUpdate
2930 * Add locale if it hasn't already been added
2932 void CDECL X11DRV_EndClipboardUpdate(void)
2934 INT count = ClipDataCount;
2936 /* Do Unicode <-> Text <-> OEM mapping */
2937 X11DRV_CLIPBOARD_SynthesizeData(CF_TEXT);
2938 X11DRV_CLIPBOARD_SynthesizeData(CF_OEMTEXT);
2939 X11DRV_CLIPBOARD_SynthesizeData(CF_UNICODETEXT);
2941 /* Enhmetafile <-> MetafilePict mapping */
2942 X11DRV_CLIPBOARD_SynthesizeData(CF_ENHMETAFILE);
2943 X11DRV_CLIPBOARD_SynthesizeData(CF_METAFILEPICT);
2945 /* DIB <-> Bitmap mapping */
2946 X11DRV_CLIPBOARD_SynthesizeData(CF_DIB);
2947 X11DRV_CLIPBOARD_SynthesizeData(CF_BITMAP);
2949 TRACE("%d formats added to cached data\n", ClipDataCount - count);
2953 /***********************************************************************
2954 * X11DRV_SelectionRequest_TARGETS
2955 * Service a TARGETS selection request event
2957 static Atom X11DRV_SelectionRequest_TARGETS( Display *display, Window requestor,
2958 Atom target, Atom rprop )
2963 LPWINE_CLIPFORMAT format;
2964 LPWINE_CLIPDATA lpData;
2966 /* Create X atoms for any clipboard types which don't have atoms yet.
2967 * This avoids sending bogus zero atoms.
2968 * Without this, copying might not have access to all clipboard types.
2969 * FIXME: is it safe to call this here?
2974 * Count the number of items we wish to expose as selection targets.
2976 cTargets = 1; /* Include TARGETS */
2978 if (!list_head( &data_list )) return None;
2980 LIST_FOR_EACH_ENTRY( lpData, &data_list, WINE_CLIPDATA, entry )
2981 LIST_FOR_EACH_ENTRY( format, &format_list, WINE_CLIPFORMAT, entry )
2982 if ((format->wFormatID == lpData->wFormatID) &&
2983 format->lpDrvExportFunc && format->drvData)
2986 TRACE(" found %d formats\n", cTargets);
2988 /* Allocate temp buffer */
2989 targets = HeapAlloc( GetProcessHeap(), 0, cTargets * sizeof(Atom));
2994 targets[i++] = x11drv_atom(TARGETS);
2996 LIST_FOR_EACH_ENTRY( lpData, &data_list, WINE_CLIPDATA, entry )
2997 LIST_FOR_EACH_ENTRY( format, &format_list, WINE_CLIPFORMAT, entry )
2998 if ((format->wFormatID == lpData->wFormatID) &&
2999 format->lpDrvExportFunc && format->drvData)
3000 targets[i++] = format->drvData;
3002 if (TRACE_ON(clipboard))
3005 for ( i = 0; i < cTargets; i++)
3007 char *itemFmtName = XGetAtomName(display, targets[i]);
3008 TRACE("\tAtom# %d: Property %ld Type %s\n", i, targets[i], itemFmtName);
3013 /* We may want to consider setting the type to xaTargets instead,
3014 * in case some apps expect this instead of XA_ATOM */
3015 XChangeProperty(display, requestor, rprop, XA_ATOM, 32,
3016 PropModeReplace, (unsigned char *)targets, cTargets);
3018 HeapFree(GetProcessHeap(), 0, targets);
3024 /***********************************************************************
3025 * X11DRV_SelectionRequest_MULTIPLE
3026 * Service a MULTIPLE selection request event
3027 * rprop contains a list of (target,property) atom pairs.
3028 * The first atom names a target and the second names a property.
3029 * The effect is as if we have received a sequence of SelectionRequest events
3030 * (one for each atom pair) except that:
3031 * 1. We reply with a SelectionNotify only when all the requested conversions
3032 * have been performed.
3033 * 2. If we fail to convert the target named by an atom in the MULTIPLE property,
3034 * we replace the atom in the property by None.
3036 static Atom X11DRV_SelectionRequest_MULTIPLE( HWND hWnd, XSelectionRequestEvent *pevent )
3038 Display *display = pevent->display;
3040 Atom atype=AnyPropertyType;
3042 unsigned long remain;
3043 Atom* targetPropList=NULL;
3044 unsigned long cTargetPropList = 0;
3046 /* If the specified property is None the requestor is an obsolete client.
3047 * We support these by using the specified target atom as the reply property.
3049 rprop = pevent->property;
3051 rprop = pevent->target;
3055 /* Read the MULTIPLE property contents. This should contain a list of
3056 * (target,property) atom pairs.
3058 if (!XGetWindowProperty(display, pevent->requestor, rprop,
3059 0, 0x3FFF, False, AnyPropertyType, &atype,&aformat,
3060 &cTargetPropList, &remain,
3061 (unsigned char**)&targetPropList) != Success)
3063 if (TRACE_ON(clipboard))
3065 char * const typeName = XGetAtomName(display, atype);
3066 TRACE("\tType %s,Format %d,nItems %ld, Remain %ld\n",
3067 typeName, aformat, cTargetPropList, remain);
3072 * Make sure we got what we expect.
3073 * NOTE: According to the X-ICCCM Version 2.0 documentation the property sent
3074 * in a MULTIPLE selection request should be of type ATOM_PAIR.
3075 * However some X apps(such as XPaint) are not compliant with this and return
3076 * a user defined atom in atype when XGetWindowProperty is called.
3077 * The data *is* an atom pair but is not denoted as such.
3079 if(aformat == 32 /* atype == xAtomPair */ )
3083 /* Iterate through the ATOM_PAIR list and execute a SelectionRequest
3084 * for each (target,property) pair */
3086 for (i = 0; i < cTargetPropList; i+=2)
3088 XSelectionRequestEvent event;
3090 if (TRACE_ON(clipboard))
3092 char *targetName, *propName;
3093 targetName = XGetAtomName(display, targetPropList[i]);
3094 propName = XGetAtomName(display, targetPropList[i+1]);
3095 TRACE("MULTIPLE(%d): Target='%s' Prop='%s'\n",
3096 i/2, targetName, propName);
3101 /* We must have a non "None" property to service a MULTIPLE target atom */
3102 if ( !targetPropList[i+1] )
3104 TRACE("\tMULTIPLE(%d): Skipping target with empty property!\n", i);
3108 /* Set up an XSelectionRequestEvent for this (target,property) pair */
3110 event.target = targetPropList[i];
3111 event.property = targetPropList[i+1];
3113 /* Fire a SelectionRequest, informing the handler that we are processing
3114 * a MULTIPLE selection request event.
3116 X11DRV_HandleSelectionRequest( hWnd, &event, TRUE );
3120 /* Free the list of targets/properties */
3121 XFree(targetPropList);
3123 else TRACE("Couldn't read MULTIPLE property\n");
3129 /***********************************************************************
3130 * X11DRV_HandleSelectionRequest
3131 * Process an event selection request event.
3132 * The bIsMultiple flag is used to signal when EVENT_SelectionRequest is called
3133 * recursively while servicing a "MULTIPLE" selection target.
3135 * Note: We only receive this event when WINE owns the X selection
3137 static void X11DRV_HandleSelectionRequest( HWND hWnd, XSelectionRequestEvent *event, BOOL bIsMultiple )
3139 Display *display = event->display;
3140 XSelectionEvent result;
3142 Window request = event->requestor;
3147 * We can only handle the selection request if :
3148 * The selection is PRIMARY or CLIPBOARD, AND we can successfully open the clipboard.
3149 * Don't do these checks or open the clipboard while recursively processing MULTIPLE,
3150 * since this has been already done.
3154 if (((event->selection != XA_PRIMARY) && (event->selection != x11drv_atom(CLIPBOARD))))
3158 /* If the specified property is None the requestor is an obsolete client.
3159 * We support these by using the specified target atom as the reply property.
3161 rprop = event->property;
3163 rprop = event->target;
3165 if(event->target == x11drv_atom(TARGETS)) /* Return a list of all supported targets */
3167 /* TARGETS selection request */
3168 rprop = X11DRV_SelectionRequest_TARGETS( display, request, event->target, rprop );
3170 else if(event->target == x11drv_atom(MULTIPLE)) /* rprop contains a list of (target, property) atom pairs */
3172 /* MULTIPLE selection request */
3173 rprop = X11DRV_SelectionRequest_MULTIPLE( hWnd, event );
3177 LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupProperty(NULL, event->target);
3179 if (lpFormat && lpFormat->lpDrvExportFunc)
3181 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(lpFormat->wFormatID);
3185 unsigned char* lpClipData;
3187 HANDLE hClipData = lpFormat->lpDrvExportFunc(display, request, event->target,
3188 rprop, lpData, &cBytes);
3190 if (hClipData && (lpClipData = GlobalLock(hClipData)))
3192 int mode = PropModeReplace;
3194 TRACE("\tUpdating property %s, %d bytes\n",
3195 debugstr_format(lpFormat->wFormatID), cBytes);
3198 int nelements = min(cBytes, 65536);
3199 XChangeProperty(display, request, rprop, event->target,
3200 8, mode, lpClipData, nelements);
3201 mode = PropModeAppend;
3202 cBytes -= nelements;
3203 lpClipData += nelements;
3204 } while (cBytes > 0);
3206 GlobalUnlock(hClipData);
3207 GlobalFree(hClipData);
3215 * SelectionNotify should be sent only at the end of a MULTIPLE request
3219 result.type = SelectionNotify;
3220 result.display = display;
3221 result.requestor = request;
3222 result.selection = event->selection;
3223 result.property = rprop;
3224 result.target = event->target;
3225 result.time = event->time;
3226 TRACE("Sending SelectionNotify event...\n");
3227 XSendEvent(display,event->requestor,False,NoEventMask,(XEvent*)&result);
3232 /***********************************************************************
3233 * X11DRV_SelectionRequest
3235 void X11DRV_SelectionRequest( HWND hWnd, XEvent *event )
3237 X11DRV_HandleSelectionRequest( hWnd, &event->xselectionrequest, FALSE );
3241 /***********************************************************************
3242 * X11DRV_SelectionClear
3244 void X11DRV_SelectionClear( HWND hWnd, XEvent *xev )
3246 XSelectionClearEvent *event = &xev->xselectionclear;
3247 if (event->selection == XA_PRIMARY || event->selection == x11drv_atom(CLIPBOARD))
3248 X11DRV_CLIPBOARD_ReleaseSelection( event->display, event->selection,
3249 event->window, hWnd, event->time );