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 .1 seconds */
90 #define SELECTION_WAIT 1000 /* us */
91 /* Minimum seconds that must lapse between owner queries */
92 #define OWNERQUERYLAPSETIME 1
95 #define S_NOSELECTION 0
106 } CLIPBOARDINFO, *LPCLIPBOARDINFO;
108 struct tagWINE_CLIPDATA; /* Forward */
110 typedef HANDLE (*DRVEXPORTFUNC)(Display *display, Window requestor, Atom aTarget, Atom rprop,
111 struct tagWINE_CLIPDATA* lpData, LPDWORD lpBytes);
112 typedef HANDLE (*DRVIMPORTFUNC)(Display *d, Window w, Atom prop);
114 typedef struct tagWINE_CLIPFORMAT {
119 DRVIMPORTFUNC lpDrvImportFunc;
120 DRVEXPORTFUNC lpDrvExportFunc;
121 struct tagWINE_CLIPFORMAT *PrevFormat;
122 struct tagWINE_CLIPFORMAT *NextFormat;
123 } WINE_CLIPFORMAT, *LPWINE_CLIPFORMAT;
125 typedef struct tagWINE_CLIPDATA {
130 LPWINE_CLIPFORMAT lpFormat;
131 struct tagWINE_CLIPDATA *PrevData;
132 struct tagWINE_CLIPDATA *NextData;
133 } WINE_CLIPDATA, *LPWINE_CLIPDATA;
135 #define CF_FLAG_BUILTINFMT 0x0001 /* Built-in windows format */
136 #define CF_FLAG_UNOWNED 0x0002 /* cached data is not owned */
137 #define CF_FLAG_SYNTHESIZED 0x0004 /* Implicitly converted data */
138 #define CF_FLAG_UNICODE 0x0008 /* Data is in unicode */
140 static int selectionAcquired = 0; /* Contains the current selection masks */
141 static Window selectionWindow = None; /* The top level X window which owns the selection */
142 static Atom selectionCacheSrc = XA_PRIMARY; /* The selection source from which the clipboard cache was filled */
144 void CDECL X11DRV_EmptyClipboard(BOOL keepunowned);
145 void CDECL X11DRV_EndClipboardUpdate(void);
146 static HANDLE X11DRV_CLIPBOARD_ImportClipboardData(Display *d, Window w, Atom prop);
147 static HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(Display *d, Window w, Atom prop);
148 static HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(Display *d, Window w, Atom prop);
149 static HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(Display *d, Window w, Atom prop);
150 static HANDLE X11DRV_CLIPBOARD_ImportImageBmp(Display *d, Window w, Atom prop);
151 static HANDLE X11DRV_CLIPBOARD_ImportXAString(Display *d, Window w, Atom prop);
152 static HANDLE X11DRV_CLIPBOARD_ImportUTF8(Display *d, Window w, Atom prop);
153 static HANDLE X11DRV_CLIPBOARD_ImportCompoundText(Display *d, Window w, Atom prop);
154 static HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Display *display, Window requestor, Atom aTarget,
155 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
156 static HANDLE X11DRV_CLIPBOARD_ExportString(Display *display, Window requestor, Atom aTarget,
157 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
158 static HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Display *display, Window requestor, Atom aTarget,
159 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
160 static HANDLE X11DRV_CLIPBOARD_ExportImageBmp(Display *display, Window requestor, Atom aTarget,
161 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
162 static HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Display *display, Window requestor, Atom aTarget,
163 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
164 static HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Display *display, Window requestor, Atom aTarget,
165 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
166 static HANDLE X11DRV_CLIPBOARD_ExportTextHtml(Display *display, Window requestor, Atom aTarget,
167 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
168 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(LPCWSTR FormatName, Atom prop);
169 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(Display *display, UINT wFormatID);
170 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData);
171 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void);
172 static int X11DRV_CLIPBOARD_QueryAvailableData(Display *display, LPCLIPBOARDINFO lpcbinfo);
173 static BOOL X11DRV_CLIPBOARD_ReadSelectionData(Display *display, LPWINE_CLIPDATA lpData);
174 static BOOL X11DRV_CLIPBOARD_ReadProperty(Display *display, Window w, Atom prop,
175 unsigned char** data, unsigned long* datasize);
176 static BOOL X11DRV_CLIPBOARD_RenderFormat(Display *display, LPWINE_CLIPDATA lpData);
177 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out);
178 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID);
179 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(Display *display, LPWINE_CLIPDATA lpData);
180 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedDIB(Display *display);
181 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedBitmap(Display *display);
182 static void X11DRV_HandleSelectionRequest( HWND hWnd, XSelectionRequestEvent *event, BOOL bIsMultiple );
185 * WARNING: This data ordering is dependent on the WINE_CLIPFORMAT structure
186 * declared in clipboard.h
188 static const WCHAR wszCF_TEXT[] = {'W','C','F','_','T','E','X','T',0};
189 static const WCHAR wszCF_BITMAP[] = {'W','C','F','_','B','I','T','M','A','P',0};
190 static const WCHAR wszCF_METAFILEPICT[] = {'W','C','F','_','M','E','T','A','F','I','L','E','P','I','C','T',0};
191 static const WCHAR wszCF_SYLK[] = {'W','C','F','_','S','Y','L','K',0};
192 static const WCHAR wszCF_DIF[] = {'W','C','F','_','D','I','F',0};
193 static const WCHAR wszCF_TIFF[] = {'W','C','F','_','T','I','F','F',0};
194 static const WCHAR wszCF_OEMTEXT[] = {'W','C','F','_','O','E','M','T','E','X','T',0};
195 static const WCHAR wszCF_DIB[] = {'W','C','F','_','D','I','B',0};
196 static const WCHAR wszIMAGEBMP[] = {'i','m','a','g','e','/','b','m','p',0};
197 static const WCHAR wszCF_PALETTE[] = {'W','C','F','_','P','A','L','E','T','T','E',0};
198 static const WCHAR wszCF_PENDATA[] = {'W','C','F','_','P','E','N','D','A','T','A',0};
199 static const WCHAR wszCF_RIFF[] = {'W','C','F','_','R','I','F','F',0};
200 static const WCHAR wszCF_WAVE[] = {'W','C','F','_','W','A','V','E',0};
201 static const WCHAR wszCOMPOUNDTEXT[] = {'C','O','M','P','O','U','N','D','_','T','E','X','T',0};
202 static const WCHAR wszUTF8STRING[] = {'U','T','F','8','_','S','T','R','I','N','G',0};
203 static const WCHAR wszCF_ENHMETAFILE[] = {'W','C','F','_','E','N','H','M','E','T','A','F','I','L','E',0};
204 static const WCHAR wszCF_HDROP[] = {'W','C','F','_','H','D','R','O','P',0};
205 static const WCHAR wszCF_LOCALE[] = {'W','C','F','_','L','O','C','A','L','E',0};
206 static const WCHAR wszCF_DIBV5[] = {'W','C','F','_','D','I','B','V','5',0};
207 static const WCHAR wszCF_OWNERDISPLAY[] = {'W','C','F','_','O','W','N','E','R','D','I','S','P','L','A','Y',0};
208 static const WCHAR wszCF_DSPTEXT[] = {'W','C','F','_','D','S','P','T','E','X','T',0};
209 static const WCHAR wszCF_DSPBITMAP[] = {'W','C','F','_','D','S','P','B','I','T','M','A','P',0};
210 static const WCHAR wszCF_DSPMETAFILEPICT[] = {'W','C','F','_','D','S','P','M','E','T','A','F','I','L','E','P','I','C','T',0};
211 static const WCHAR wszCF_DSPENHMETAFILE[] = {'W','C','F','_','D','S','P','E','N','H','M','E','T','A','F','I','L','E',0};
213 static WINE_CLIPFORMAT ClipFormats[] =
215 { CF_TEXT, wszCF_TEXT, XA_STRING, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportXAString,
216 X11DRV_CLIPBOARD_ExportString, NULL, &ClipFormats[1]},
218 { CF_BITMAP, wszCF_BITMAP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
219 NULL, &ClipFormats[0], &ClipFormats[2]},
221 { CF_METAFILEPICT, wszCF_METAFILEPICT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportMetaFilePict,
222 X11DRV_CLIPBOARD_ExportMetaFilePict, &ClipFormats[1], &ClipFormats[3]},
224 { CF_SYLK, wszCF_SYLK, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
225 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[2], &ClipFormats[4]},
227 { CF_DIF, wszCF_DIF, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
228 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[3], &ClipFormats[5]},
230 { CF_TIFF, wszCF_TIFF, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
231 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[4], &ClipFormats[6]},
233 { CF_OEMTEXT, wszCF_OEMTEXT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
234 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[5], &ClipFormats[7]},
236 { CF_DIB, wszCF_DIB, XA_PIXMAP, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportXAPIXMAP,
237 X11DRV_CLIPBOARD_ExportXAPIXMAP, &ClipFormats[6], &ClipFormats[8]},
239 { CF_PALETTE, wszCF_PALETTE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
240 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[7], &ClipFormats[9]},
242 { CF_PENDATA, wszCF_PENDATA, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
243 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[8], &ClipFormats[10]},
245 { CF_RIFF, wszCF_RIFF, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
246 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[9], &ClipFormats[11]},
248 { CF_WAVE, wszCF_WAVE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
249 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[10], &ClipFormats[12]},
251 { CF_UNICODETEXT, wszUTF8STRING, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportUTF8,
252 X11DRV_CLIPBOARD_ExportString, &ClipFormats[11], &ClipFormats[13]},
254 /* If UTF8_STRING is not available, attempt COMPUND_TEXT */
255 { CF_UNICODETEXT, wszCOMPOUNDTEXT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportCompoundText,
256 X11DRV_CLIPBOARD_ExportString, &ClipFormats[12], &ClipFormats[14]},
258 { CF_ENHMETAFILE, wszCF_ENHMETAFILE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportEnhMetaFile,
259 X11DRV_CLIPBOARD_ExportEnhMetaFile, &ClipFormats[13], &ClipFormats[15]},
261 { CF_HDROP, wszCF_HDROP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
262 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[14], &ClipFormats[16]},
264 { CF_LOCALE, wszCF_LOCALE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
265 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[15], &ClipFormats[17]},
267 { CF_DIBV5, wszCF_DIBV5, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
268 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[16], &ClipFormats[18]},
270 { CF_OWNERDISPLAY, wszCF_OWNERDISPLAY, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
271 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[17], &ClipFormats[19]},
273 { CF_DSPTEXT, wszCF_DSPTEXT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
274 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[18], &ClipFormats[20]},
276 { CF_DSPBITMAP, wszCF_DSPBITMAP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
277 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[19], &ClipFormats[21]},
279 { CF_DSPMETAFILEPICT, wszCF_DSPMETAFILEPICT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
280 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[20], &ClipFormats[22]},
282 { CF_DSPENHMETAFILE, wszCF_DSPENHMETAFILE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
283 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[21], &ClipFormats[23]},
285 { CF_DIB, wszIMAGEBMP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportImageBmp,
286 X11DRV_CLIPBOARD_ExportImageBmp, &ClipFormats[22], NULL},
289 #define GET_ATOM(prop) (((prop) < FIRST_XATOM) ? (Atom)(prop) : X11DRV_Atoms[(prop) - FIRST_XATOM])
291 /* Maps X properties to Windows formats */
292 static const WCHAR wszRichTextFormat[] = {'R','i','c','h',' ','T','e','x','t',' ','F','o','r','m','a','t',0};
293 static const WCHAR wszGIF[] = {'G','I','F',0};
294 static const WCHAR wszJFIF[] = {'J','F','I','F',0};
295 static const WCHAR wszPNG[] = {'P','N','G',0};
296 static const WCHAR wszHTMLFormat[] = {'H','T','M','L',' ','F','o','r','m','a','t',0};
301 } PropertyFormatMap[] =
303 { wszRichTextFormat, XATOM_text_rtf },
304 { wszRichTextFormat, XATOM_text_richtext },
305 { wszGIF, XATOM_image_gif },
306 { wszJFIF, XATOM_image_jpeg },
307 { wszPNG, XATOM_image_png },
308 { wszHTMLFormat, XATOM_HTML_Format }, /* prefer this to text/html */
313 * Cached clipboard data.
315 static LPWINE_CLIPDATA ClipData = NULL;
316 static UINT ClipDataCount = 0;
319 * Clipboard sequence number
321 static UINT wSeqNo = 0;
323 /**************************************************************************
324 * Internal Clipboard implementation methods
325 **************************************************************************/
327 static Window thread_selection_wnd(void)
329 struct x11drv_thread_data *thread_data = x11drv_init_thread_data();
330 Window w = thread_data->selection_wnd;
334 XSetWindowAttributes attr;
336 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
337 ButtonPressMask | ButtonReleaseMask | EnterWindowMask | PropertyChangeMask);
340 w = XCreateWindow(thread_data->display, root_window, 0, 0, 1, 1, 0, screen_depth,
341 InputOutput, CopyFromParent, CWEventMask, &attr);
345 thread_data->selection_wnd = w;
347 FIXME("Failed to create window. Fetching selection data will fail.\n");
353 /**************************************************************************
354 * X11DRV_InitClipboard
356 void X11DRV_InitClipboard(void)
359 WINE_CLIPFORMAT *format;
361 /* Register known mapping between window formats and X properties */
362 for (i = 0; i < sizeof(PropertyFormatMap)/sizeof(PropertyFormatMap[0]); i++)
363 X11DRV_CLIPBOARD_InsertClipboardFormat(PropertyFormatMap[i].lpszFormat, GET_ATOM(PropertyFormatMap[i].prop));
365 /* Set up a conversion function from "HTML Format" to "text/html" */
366 format = X11DRV_CLIPBOARD_InsertClipboardFormat(wszHTMLFormat, GET_ATOM(XATOM_text_html));
367 format->lpDrvExportFunc = X11DRV_CLIPBOARD_ExportTextHtml;
371 /**************************************************************************
374 * Intern atoms for formats that don't have one yet.
376 static void intern_atoms(void)
378 LPWINE_CLIPFORMAT format;
384 for (format = ClipFormats, count = 0; format; format = format->NextFormat)
385 if (!format->drvData) count++;
388 display = thread_init_display();
390 names = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*names) );
391 atoms = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*atoms) );
393 for (format = ClipFormats, i = 0; format; format = format->NextFormat) {
394 if (!format->drvData) {
395 len = WideCharToMultiByte(CP_UNIXCP, 0, format->Name, -1, NULL, 0, NULL, NULL);
396 names[i] = HeapAlloc(GetProcessHeap(), 0, len);
397 WideCharToMultiByte(CP_UNIXCP, 0, format->Name, -1, names[i++], len, NULL, NULL);
402 XInternAtoms( display, names, count, False, atoms );
405 for (format = ClipFormats, i = 0; format; format = format->NextFormat) {
406 if (!format->drvData) {
407 HeapFree(GetProcessHeap(), 0, names[i]);
408 format->drvData = atoms[i++];
412 HeapFree( GetProcessHeap(), 0, names );
413 HeapFree( GetProcessHeap(), 0, atoms );
417 /**************************************************************************
420 * Register a custom X clipboard format.
422 static WINE_CLIPFORMAT *register_format( LPCWSTR FormatName, Atom prop )
424 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
426 TRACE("%s\n", debugstr_w(FormatName));
428 /* walk format chain to see if it's already registered */
431 if (CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, lpFormat->Name, -1, FormatName, -1) == CSTR_EQUAL
432 && (lpFormat->wFlags & CF_FLAG_BUILTINFMT) == 0)
434 lpFormat = lpFormat->NextFormat;
437 return X11DRV_CLIPBOARD_InsertClipboardFormat(FormatName, prop);
441 /**************************************************************************
442 * X11DRV_CLIPBOARD_LookupFormat
444 static LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupFormat(WORD wID)
446 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
450 if (lpFormat->wFormatID == wID)
453 lpFormat = lpFormat->NextFormat;
455 if (lpFormat && !lpFormat->drvData) intern_atoms();
460 /**************************************************************************
461 * X11DRV_CLIPBOARD_LookupProperty
463 static LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupProperty(LPWINE_CLIPFORMAT current, UINT drvData)
467 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
468 BOOL need_intern = FALSE;
471 lpFormat = current->NextFormat;
475 if (lpFormat->drvData == drvData) return lpFormat;
476 if (!lpFormat->drvData) need_intern = TRUE;
477 lpFormat = lpFormat->NextFormat;
479 if (!need_intern) return NULL;
481 /* restart the search for the new atoms */
486 /**************************************************************************
487 * X11DRV_CLIPBOARD_LookupData
489 static LPWINE_CLIPDATA X11DRV_CLIPBOARD_LookupData(DWORD wID)
491 LPWINE_CLIPDATA lpData = ClipData;
497 if (lpData->wFormatID == wID)
500 lpData = lpData->NextData;
502 while(lpData != ClipData);
504 if (lpData->wFormatID != wID)
512 /**************************************************************************
513 * InsertClipboardFormat
515 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(LPCWSTR FormatName, Atom prop)
517 LPWINE_CLIPFORMAT lpFormat;
518 LPWINE_CLIPFORMAT lpNewFormat;
521 /* allocate storage for new format entry */
522 lpNewFormat = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPFORMAT));
524 if(lpNewFormat == NULL)
526 WARN("No more memory for a new format!\n");
530 if (!(new_name = HeapAlloc(GetProcessHeap(), 0, (strlenW(FormatName)+1)*sizeof(WCHAR))))
532 WARN("No more memory for the new format name!\n");
533 HeapFree(GetProcessHeap(), 0, lpNewFormat);
537 lpNewFormat->Name = strcpyW(new_name, FormatName);
538 lpNewFormat->wFlags = 0;
539 lpNewFormat->wFormatID = GlobalAddAtomW(lpNewFormat->Name);
540 lpNewFormat->drvData = prop;
541 lpNewFormat->lpDrvImportFunc = X11DRV_CLIPBOARD_ImportClipboardData;
542 lpNewFormat->lpDrvExportFunc = X11DRV_CLIPBOARD_ExportClipboardData;
545 lpFormat = ClipFormats;
547 while(lpFormat->NextFormat) /* Move to last entry */
548 lpFormat = lpFormat->NextFormat;
550 lpNewFormat->NextFormat = NULL;
551 lpFormat->NextFormat = lpNewFormat;
552 lpNewFormat->PrevFormat = lpFormat;
554 TRACE("Registering format(%04x): %s drvData %d\n",
555 lpNewFormat->wFormatID, debugstr_w(FormatName), lpNewFormat->drvData);
563 /**************************************************************************
564 * X11DRV_CLIPBOARD_GetClipboardInfo
566 static BOOL X11DRV_CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
570 SERVER_START_REQ( set_clipboard_info )
574 if (wine_server_call_err( req ))
576 ERR("Failed to get clipboard owner.\n");
580 cbInfo->hWndOpen = wine_server_ptr_handle( reply->old_clipboard );
581 cbInfo->hWndOwner = wine_server_ptr_handle( reply->old_owner );
582 cbInfo->hWndViewer = wine_server_ptr_handle( reply->old_viewer );
583 cbInfo->seqno = reply->seqno;
584 cbInfo->flags = reply->flags;
595 /**************************************************************************
596 * X11DRV_CLIPBOARD_ReleaseOwnership
598 static BOOL X11DRV_CLIPBOARD_ReleaseOwnership(void)
602 SERVER_START_REQ( set_clipboard_info )
604 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
606 if (wine_server_call_err( req ))
608 ERR("Failed to set clipboard.\n");
622 /**************************************************************************
623 * X11DRV_CLIPBOARD_InsertClipboardData
625 * Caller *must* have the clipboard open and be the owner.
627 static BOOL X11DRV_CLIPBOARD_InsertClipboardData(UINT wFormatID, HANDLE hData, DWORD flags,
628 LPWINE_CLIPFORMAT lpFormat, BOOL override)
630 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormatID);
632 TRACE("format=%04x lpData=%p hData=%p flags=0x%08x lpFormat=%p override=%d\n",
633 wFormatID, lpData, hData, flags, lpFormat, override);
635 if (lpData && !override)
640 X11DRV_CLIPBOARD_FreeData(lpData);
642 lpData->hData = hData;
646 lpData = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPDATA));
648 lpData->wFormatID = wFormatID;
649 lpData->hData = hData;
650 lpData->lpFormat = lpFormat;
655 LPWINE_CLIPDATA lpPrevData = ClipData->PrevData;
657 lpData->PrevData = lpPrevData;
658 lpData->NextData = ClipData;
660 lpPrevData->NextData = lpData;
661 ClipData->PrevData = lpData;
665 lpData->NextData = lpData;
666 lpData->PrevData = lpData;
673 lpData->wFlags = flags;
679 /**************************************************************************
680 * X11DRV_CLIPBOARD_FreeData
682 * Free clipboard data handle.
684 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData)
686 TRACE("%04x\n", lpData->wFormatID);
688 if ((lpData->wFormatID >= CF_GDIOBJFIRST &&
689 lpData->wFormatID <= CF_GDIOBJLAST) ||
690 lpData->wFormatID == CF_BITMAP ||
691 lpData->wFormatID == CF_DIB ||
692 lpData->wFormatID == CF_PALETTE)
695 DeleteObject(lpData->hData);
697 if ((lpData->wFormatID == CF_DIB) && lpData->drvData)
698 XFreePixmap(gdi_display, lpData->drvData);
700 else if (lpData->wFormatID == CF_METAFILEPICT)
704 DeleteMetaFile(((METAFILEPICT *)GlobalLock( lpData->hData ))->hMF );
705 GlobalFree(lpData->hData);
708 else if (lpData->wFormatID == CF_ENHMETAFILE)
711 DeleteEnhMetaFile(lpData->hData);
713 else if (lpData->wFormatID < CF_PRIVATEFIRST ||
714 lpData->wFormatID > CF_PRIVATELAST)
717 GlobalFree(lpData->hData);
725 /**************************************************************************
726 * X11DRV_CLIPBOARD_UpdateCache
728 static BOOL X11DRV_CLIPBOARD_UpdateCache(LPCLIPBOARDINFO lpcbinfo)
732 if (!X11DRV_CLIPBOARD_IsSelectionOwner())
734 if (!X11DRV_CLIPBOARD_GetClipboardInfo(lpcbinfo))
736 ERR("Failed to retrieve clipboard information.\n");
739 else if (wSeqNo < lpcbinfo->seqno)
741 X11DRV_EmptyClipboard(TRUE);
743 if (X11DRV_CLIPBOARD_QueryAvailableData(thread_init_display(), lpcbinfo) < 0)
745 ERR("Failed to cache clipboard data owned by another process.\n");
750 X11DRV_EndClipboardUpdate();
753 wSeqNo = lpcbinfo->seqno;
761 /**************************************************************************
762 * X11DRV_CLIPBOARD_RenderFormat
764 static BOOL X11DRV_CLIPBOARD_RenderFormat(Display *display, LPWINE_CLIPDATA lpData)
768 TRACE(" 0x%04x hData(%p)\n", lpData->wFormatID, lpData->hData);
770 if (lpData->hData) return bret; /* Already rendered */
772 if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
773 bret = X11DRV_CLIPBOARD_RenderSynthesizedFormat(display, lpData);
774 else if (!X11DRV_CLIPBOARD_IsSelectionOwner())
776 if (!X11DRV_CLIPBOARD_ReadSelectionData(display, lpData))
778 ERR("Failed to cache clipboard data owned by another process. Format=%04x\n",
785 CLIPBOARDINFO cbInfo;
787 if (X11DRV_CLIPBOARD_GetClipboardInfo(&cbInfo) && cbInfo.hWndOwner)
789 /* Send a WM_RENDERFORMAT message to notify the owner to render the
790 * data requested into the clipboard.
792 TRACE("Sending WM_RENDERFORMAT message to hwnd(%p)\n", cbInfo.hWndOwner);
793 SendMessageW(cbInfo.hWndOwner, WM_RENDERFORMAT, lpData->wFormatID, 0);
795 if (!lpData->hData) bret = FALSE;
799 ERR("hWndClipOwner is lost!\n");
808 /**************************************************************************
809 * CLIPBOARD_ConvertText
810 * Returns number of required/converted characters - not bytes!
812 static INT CLIPBOARD_ConvertText(WORD src_fmt, void const *src, INT src_size,
813 WORD dst_fmt, void *dst, INT dst_size)
817 if(src_fmt == CF_UNICODETEXT)
830 return WideCharToMultiByte(cp, 0, src, src_size, dst, dst_size, NULL, NULL);
833 if(dst_fmt == CF_UNICODETEXT)
846 return MultiByteToWideChar(cp, 0, src, src_size, dst, dst_size);
849 if(!dst_size) return src_size;
851 if(dst_size > src_size) dst_size = src_size;
853 if(src_fmt == CF_TEXT )
854 CharToOemBuffA(src, dst, dst_size);
856 OemToCharBuffA(src, dst, dst_size);
862 /**************************************************************************
863 * X11DRV_CLIPBOARD_RenderSynthesizedFormat
865 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(Display *display, LPWINE_CLIPDATA lpData)
871 if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
873 UINT wFormatID = lpData->wFormatID;
875 if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
876 bret = X11DRV_CLIPBOARD_RenderSynthesizedText(display, wFormatID);
882 bret = X11DRV_CLIPBOARD_RenderSynthesizedDIB( display );
886 bret = X11DRV_CLIPBOARD_RenderSynthesizedBitmap( display );
890 case CF_METAFILEPICT:
891 FIXME("Synthesizing wFormatID(0x%08x) not implemented\n", wFormatID);
895 FIXME("Called to synthesize unknown format\n");
900 lpData->wFlags &= ~CF_FLAG_SYNTHESIZED;
907 /**************************************************************************
908 * X11DRV_CLIPBOARD_RenderSynthesizedText
910 * Renders synthesized text
912 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(Display *display, UINT wFormatID)
917 INT src_chars, dst_chars, alloc_size;
918 LPWINE_CLIPDATA lpSource = NULL;
920 TRACE("%04x\n", wFormatID);
922 if ((lpSource = X11DRV_CLIPBOARD_LookupData(wFormatID)) &&
926 /* Look for rendered source or non-synthesized source */
927 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
928 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData))
930 TRACE("UNICODETEXT -> %04x\n", wFormatID);
932 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
933 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData))
935 TRACE("TEXT -> %04x\n", wFormatID);
937 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
938 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData))
940 TRACE("OEMTEXT -> %04x\n", wFormatID);
943 if (!lpSource || (lpSource->wFlags & CF_FLAG_SYNTHESIZED &&
947 /* Ask the clipboard owner to render the source text if necessary */
948 if (!lpSource->hData && !X11DRV_CLIPBOARD_RenderFormat(display, lpSource))
951 lpstrS = GlobalLock(lpSource->hData);
955 /* Text always NULL terminated */
956 if(lpSource->wFormatID == CF_UNICODETEXT)
957 src_chars = strlenW((LPCWSTR)lpstrS) + 1;
959 src_chars = strlen(lpstrS) + 1;
961 /* Calculate number of characters in the destination buffer */
962 dst_chars = CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS,
963 src_chars, wFormatID, NULL, 0);
968 TRACE("Converting from '%04x' to '%04x', %i chars\n",
969 lpSource->wFormatID, wFormatID, src_chars);
971 /* Convert characters to bytes */
972 if(wFormatID == CF_UNICODETEXT)
973 alloc_size = dst_chars * sizeof(WCHAR);
975 alloc_size = dst_chars;
977 hData = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE |
978 GMEM_DDESHARE, alloc_size);
980 lpstrT = GlobalLock(hData);
984 CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS, src_chars,
985 wFormatID, lpstrT, dst_chars);
989 GlobalUnlock(lpSource->hData);
991 return X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, hData, 0, NULL, TRUE);
995 /**************************************************************************
996 * X11DRV_CLIPBOARD_RenderSynthesizedDIB
998 * Renders synthesized DIB
1000 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedDIB(Display *display)
1003 LPWINE_CLIPDATA lpSource = NULL;
1007 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) && lpSource->hData)
1011 /* If we have a bitmap and it's not synthesized or it has been rendered */
1012 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) &&
1013 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData))
1015 /* Render source if required */
1016 if (lpSource->hData || X11DRV_CLIPBOARD_RenderFormat(display, lpSource))
1022 hData = X11DRV_DIB_CreateDIBFromBitmap(hdc, lpSource->hData);
1023 ReleaseDC(NULL, hdc);
1027 X11DRV_CLIPBOARD_InsertClipboardData(CF_DIB, hData, 0, NULL, TRUE);
1037 /**************************************************************************
1038 * X11DRV_CLIPBOARD_RenderSynthesizedBitmap
1040 * Renders synthesized bitmap
1042 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedBitmap(Display *display)
1045 LPWINE_CLIPDATA lpSource = NULL;
1049 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) && lpSource->hData)
1053 /* If we have a dib and it's not synthesized or it has been rendered */
1054 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) &&
1055 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData))
1057 /* Render source if required */
1058 if (lpSource->hData || X11DRV_CLIPBOARD_RenderFormat(display, lpSource))
1062 unsigned int offset;
1063 LPBITMAPINFOHEADER lpbmih;
1066 lpbmih = GlobalLock(lpSource->hData);
1068 offset = sizeof(BITMAPINFOHEADER)
1069 + ((lpbmih->biBitCount <= 8) ? (sizeof(RGBQUAD) *
1070 (1 << lpbmih->biBitCount)) : 0);
1072 hData = CreateDIBitmap(hdc, lpbmih, CBM_INIT, (LPBYTE)lpbmih +
1073 offset, (LPBITMAPINFO) lpbmih, DIB_RGB_COLORS);
1075 GlobalUnlock(lpSource->hData);
1076 ReleaseDC(NULL, hdc);
1080 X11DRV_CLIPBOARD_InsertClipboardData(CF_BITMAP, hData, 0, NULL, TRUE);
1090 /**************************************************************************
1091 * X11DRV_CLIPBOARD_ImportXAString
1093 * Import XA_STRING, converting the string to CF_TEXT.
1095 static HANDLE X11DRV_CLIPBOARD_ImportXAString(Display *display, Window w, Atom prop)
1098 unsigned long cbytes;
1100 unsigned long i, inlcount = 0;
1103 if (!X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1106 for (i = 0; i <= cbytes; i++)
1108 if (lpdata[i] == '\n')
1112 if ((hText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cbytes + inlcount + 1)))
1114 lpstr = GlobalLock(hText);
1116 for (i = 0, inlcount = 0; i <= cbytes; i++)
1118 if (lpdata[i] == '\n')
1119 lpstr[inlcount++] = '\r';
1121 lpstr[inlcount++] = lpdata[i];
1124 GlobalUnlock(hText);
1127 /* Free the retrieved property data */
1128 HeapFree(GetProcessHeap(), 0, lpdata);
1134 /**************************************************************************
1135 * X11DRV_CLIPBOARD_ImportUTF8
1137 * Import XA_STRING, converting the string to CF_UNICODE.
1139 static HANDLE X11DRV_CLIPBOARD_ImportUTF8(Display *display, Window w, Atom prop)
1142 unsigned long cbytes;
1144 unsigned long i, inlcount = 0;
1145 HANDLE hUnicodeText = 0;
1147 if (!X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1150 for (i = 0; i <= cbytes; i++)
1152 if (lpdata[i] == '\n')
1156 if ((lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cbytes + inlcount + 1)))
1160 for (i = 0, inlcount = 0; i <= cbytes; i++)
1162 if (lpdata[i] == '\n')
1163 lpstr[inlcount++] = '\r';
1165 lpstr[inlcount++] = lpdata[i];
1168 count = MultiByteToWideChar(CP_UTF8, 0, lpstr, -1, NULL, 0);
1169 hUnicodeText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, count * sizeof(WCHAR));
1173 WCHAR *textW = GlobalLock(hUnicodeText);
1174 MultiByteToWideChar(CP_UTF8, 0, lpstr, -1, textW, count);
1175 GlobalUnlock(hUnicodeText);
1178 HeapFree(GetProcessHeap(), 0, lpstr);
1181 /* Free the retrieved property data */
1182 HeapFree(GetProcessHeap(), 0, lpdata);
1184 return hUnicodeText;
1188 /**************************************************************************
1189 * X11DRV_CLIPBOARD_ImportCompoundText
1191 * Import COMPOUND_TEXT to CF_UNICODE
1193 static HANDLE X11DRV_CLIPBOARD_ImportCompoundText(Display *display, Window w, Atom prop)
1198 int srclen, destlen;
1199 HANDLE hUnicodeText;
1200 XTextProperty txtprop;
1202 if (!X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &txtprop.value, &txtprop.nitems))
1207 txtprop.encoding = x11drv_atom(COMPOUND_TEXT);
1210 ret = XmbTextPropertyToTextList(display, &txtprop, &srcstr, &count);
1211 wine_tsx11_unlock();
1212 HeapFree(GetProcessHeap(), 0, txtprop.value);
1213 if (ret != Success || !count) return 0;
1215 TRACE("Importing %d line(s)\n", count);
1217 /* Compute number of lines */
1218 srclen = strlen(srcstr[0]);
1219 for (i = 0, lcount = 0; i <= srclen; i++)
1221 if (srcstr[0][i] == '\n')
1225 destlen = MultiByteToWideChar(CP_UNIXCP, 0, srcstr[0], -1, NULL, 0);
1227 TRACE("lcount = %d, destlen=%d, srcstr %s\n", lcount, destlen, srcstr[0]);
1229 if ((hUnicodeText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (destlen + lcount + 1) * sizeof(WCHAR))))
1231 WCHAR *deststr = GlobalLock(hUnicodeText);
1232 MultiByteToWideChar(CP_UNIXCP, 0, srcstr[0], -1, deststr, destlen);
1236 for (i = destlen - 1, j = destlen + lcount - 1; i >= 0; i--, j--)
1238 deststr[j] = deststr[i];
1240 if (deststr[i] == '\n')
1241 deststr[--j] = '\r';
1245 GlobalUnlock(hUnicodeText);
1249 XFreeStringList(srcstr);
1250 wine_tsx11_unlock();
1252 return hUnicodeText;
1256 /**************************************************************************
1257 * X11DRV_CLIPBOARD_ImportXAPIXMAP
1259 * Import XA_PIXMAP, converting the image to CF_DIB.
1261 static HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(Display *display, Window w, Atom prop)
1266 unsigned long cbytes;
1268 HANDLE hClipData = 0;
1270 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1272 pPixmap = (Pixmap *) lpdata;
1274 hwnd = GetOpenClipboardWindow();
1277 hClipData = X11DRV_DIB_CreateDIBFromPixmap(*pPixmap, hdc);
1278 ReleaseDC(hwnd, hdc);
1280 /* Free the retrieved property data */
1281 HeapFree(GetProcessHeap(), 0, lpdata);
1288 /**************************************************************************
1289 * X11DRV_CLIPBOARD_ImportImageBmp
1291 * Import image/bmp, converting the image to CF_DIB.
1293 static HANDLE X11DRV_CLIPBOARD_ImportImageBmp(Display *display, Window w, Atom prop)
1296 unsigned long cbytes;
1297 HANDLE hClipData = 0;
1299 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1301 BITMAPFILEHEADER *bfh = (BITMAPFILEHEADER*)lpdata;
1303 if (cbytes >= sizeof(BITMAPFILEHEADER)+sizeof(BITMAPCOREHEADER) &&
1304 bfh->bfType == 0x4d42 /* "BM" */)
1306 BITMAPINFO *bmi = (BITMAPINFO*)(bfh+1);
1311 hbmp = CreateDIBitmap(
1315 lpdata+bfh->bfOffBits,
1320 hClipData = X11DRV_DIB_CreateDIBFromBitmap(hdc, hbmp);
1326 /* Free the retrieved property data */
1327 HeapFree(GetProcessHeap(), 0, lpdata);
1334 /**************************************************************************
1335 * X11DRV_CLIPBOARD_ImportMetaFilePict
1337 * Import MetaFilePict.
1339 static HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(Display *display, Window w, Atom prop)
1342 unsigned long cbytes;
1343 HANDLE hClipData = 0;
1345 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1348 hClipData = X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, lpdata, (LPDWORD)&cbytes, FALSE);
1350 /* Free the retrieved property data */
1351 HeapFree(GetProcessHeap(), 0, lpdata);
1358 /**************************************************************************
1359 * X11DRV_ImportEnhMetaFile
1361 * Import EnhMetaFile.
1363 static HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(Display *display, Window w, Atom prop)
1366 unsigned long cbytes;
1367 HANDLE hClipData = 0;
1369 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1372 hClipData = X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, lpdata, (LPDWORD)&cbytes, FALSE);
1374 /* Free the retrieved property data */
1375 HeapFree(GetProcessHeap(), 0, lpdata);
1382 /**************************************************************************
1383 * X11DRV_ImportClipbordaData
1385 * Generic import clipboard data routine.
1387 static HANDLE X11DRV_CLIPBOARD_ImportClipboardData(Display *display, Window w, Atom prop)
1391 unsigned long cbytes;
1392 HANDLE hClipData = 0;
1394 if (X11DRV_CLIPBOARD_ReadProperty(display, w, prop, &lpdata, &cbytes))
1398 /* Turn on the DDESHARE flag to enable shared 32 bit memory */
1399 hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cbytes);
1403 if ((lpClipData = GlobalLock(hClipData)))
1405 memcpy(lpClipData, lpdata, cbytes);
1406 GlobalUnlock(hClipData);
1410 GlobalFree(hClipData);
1415 /* Free the retrieved property data */
1416 HeapFree(GetProcessHeap(), 0, lpdata);
1423 /**************************************************************************
1424 X11DRV_CLIPBOARD_ExportClipboardData
1426 * Generic export clipboard data routine.
1428 static HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Display *display, Window requestor, Atom aTarget,
1429 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1433 HANDLE hClipData = 0;
1435 *lpBytes = 0; /* Assume failure */
1437 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpData))
1438 ERR("Failed to export %04x format\n", lpData->wFormatID);
1441 datasize = GlobalSize(lpData->hData);
1443 hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, datasize);
1444 if (hClipData == 0) return NULL;
1446 if ((lpClipData = GlobalLock(hClipData)))
1448 LPVOID lpdata = GlobalLock(lpData->hData);
1450 memcpy(lpClipData, lpdata, datasize);
1451 *lpBytes = datasize;
1453 GlobalUnlock(lpData->hData);
1454 GlobalUnlock(hClipData);
1456 GlobalFree(hClipData);
1465 /**************************************************************************
1466 * X11DRV_CLIPBOARD_ExportXAString
1468 * Export CF_TEXT converting the string to XA_STRING.
1469 * Helper function for X11DRV_CLIPBOARD_ExportString.
1471 static HANDLE X11DRV_CLIPBOARD_ExportXAString(LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1475 LPSTR text, lpstr = NULL;
1477 *lpBytes = 0; /* Assume return has zero bytes */
1479 text = GlobalLock(lpData->hData);
1480 size = strlen(text);
1482 /* remove carriage returns */
1483 lpstr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size + 1);
1487 for (i = 0,j = 0; i < size && text[i]; i++)
1489 if (text[i] == '\r' && (text[i+1] == '\n' || text[i+1] == '\0'))
1491 lpstr[j++] = text[i];
1495 *lpBytes = j; /* Number of bytes in string */
1498 HeapFree(GetProcessHeap(), 0, text);
1499 GlobalUnlock(lpData->hData);
1505 /**************************************************************************
1506 * X11DRV_CLIPBOARD_ExportUTF8String
1508 * Export CF_UNICODE converting the string to UTF8.
1509 * Helper function for X11DRV_CLIPBOARD_ExportString.
1511 static HANDLE X11DRV_CLIPBOARD_ExportUTF8String(LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1516 LPSTR text, lpstr = NULL;
1518 *lpBytes = 0; /* Assume return has zero bytes */
1520 uni_text = GlobalLock(lpData->hData);
1522 size = WideCharToMultiByte(CP_UTF8, 0, uni_text, -1, NULL, 0, NULL, NULL);
1524 text = HeapAlloc(GetProcessHeap(), 0, size);
1527 WideCharToMultiByte(CP_UTF8, 0, uni_text, -1, text, size, NULL, NULL);
1529 /* remove carriage returns */
1530 lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size--);
1534 for (i = 0,j = 0; i < size && text[i]; i++)
1536 if (text[i] == '\r' && (text[i+1] == '\n' || text[i+1] == '\0'))
1538 lpstr[j++] = text[i];
1542 *lpBytes = j; /* Number of bytes in string */
1545 HeapFree(GetProcessHeap(), 0, text);
1546 GlobalUnlock(lpData->hData);
1553 /**************************************************************************
1554 * X11DRV_CLIPBOARD_ExportCompoundText
1556 * Export CF_UNICODE to COMPOUND_TEXT
1557 * Helper function for X11DRV_CLIPBOARD_ExportString.
1559 static HANDLE X11DRV_CLIPBOARD_ExportCompoundText(Display *display, Window requestor, Atom aTarget, Atom rprop,
1560 LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1564 XICCEncodingStyle style;
1569 uni_text = GlobalLock(lpData->hData);
1571 size = WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, NULL, 0, NULL, NULL);
1572 lpstr = HeapAlloc(GetProcessHeap(), 0, size);
1576 WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, lpstr, size, NULL, NULL);
1578 /* remove carriage returns */
1579 for (i = 0, j = 0; i < size && lpstr[i]; i++)
1581 if (lpstr[i] == '\r' && (lpstr[i+1] == '\n' || lpstr[i+1] == '\0'))
1583 lpstr[j++] = lpstr[i];
1587 GlobalUnlock(lpData->hData);
1589 if (aTarget == x11drv_atom(COMPOUND_TEXT))
1590 style = XCompoundTextStyle;
1592 style = XStdICCTextStyle;
1594 /* Update the X property */
1596 if (XmbTextListToTextProperty(display, &lpstr, 1, style, &prop) == Success)
1598 XSetTextProperty(display, requestor, &prop, rprop);
1601 wine_tsx11_unlock();
1603 HeapFree(GetProcessHeap(), 0, lpstr);
1608 /**************************************************************************
1609 * X11DRV_CLIPBOARD_ExportString
1613 static HANDLE X11DRV_CLIPBOARD_ExportString(Display *display, Window requestor, Atom aTarget, Atom rprop,
1614 LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1616 if (X11DRV_CLIPBOARD_RenderFormat(display, lpData))
1618 if (aTarget == XA_STRING)
1619 return X11DRV_CLIPBOARD_ExportXAString(lpData, lpBytes);
1620 else if (aTarget == x11drv_atom(COMPOUND_TEXT) || aTarget == x11drv_atom(TEXT))
1621 return X11DRV_CLIPBOARD_ExportCompoundText(display, requestor, aTarget,
1622 rprop, lpData, lpBytes);
1625 TRACE("Exporting target %ld to default UTF8_STRING\n", aTarget);
1626 return X11DRV_CLIPBOARD_ExportUTF8String(lpData, lpBytes);
1630 ERR("Failed to render %04x format\n", lpData->wFormatID);
1636 /**************************************************************************
1637 * X11DRV_CLIPBOARD_ExportXAPIXMAP
1639 * Export CF_DIB to XA_PIXMAP.
1641 static HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Display *display, Window requestor, Atom aTarget, Atom rprop,
1642 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1646 unsigned char* lpData;
1648 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1650 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1654 if (!lpdata->drvData) /* If not already rendered */
1656 /* For convert from packed DIB to Pixmap */
1658 lpdata->drvData = (UINT) X11DRV_DIB_CreatePixmapFromDIB(lpdata->hData, hdc);
1662 *lpBytes = sizeof(Pixmap); /* pixmap is a 32bit value */
1664 /* Wrap pixmap so we can return a handle */
1665 hData = GlobalAlloc(0, *lpBytes);
1666 lpData = GlobalLock(hData);
1667 memcpy(lpData, &lpdata->drvData, *lpBytes);
1668 GlobalUnlock(hData);
1674 /**************************************************************************
1675 * X11DRV_CLIPBOARD_ExportImageBmp
1677 * Export CF_DIB to image/bmp.
1679 static HANDLE X11DRV_CLIPBOARD_ExportImageBmp(Display *display, Window requestor, Atom aTarget, Atom rprop,
1680 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1687 BITMAPFILEHEADER *bfh;
1691 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1693 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1697 hpackeddib = lpdata->hData;
1699 dibdata = GlobalLock(hpackeddib);
1702 ERR("Failed to lock packed DIB\n");
1706 bmpsize = sizeof(BITMAPFILEHEADER) + GlobalSize(hpackeddib);
1708 hbmpdata = GlobalAlloc(0, bmpsize);
1712 bmpdata = GlobalLock(hbmpdata);
1716 GlobalFree(hbmpdata);
1717 GlobalUnlock(hpackeddib);
1721 /* bitmap file header */
1722 bfh = (BITMAPFILEHEADER*)bmpdata;
1723 bfh->bfType = 0x4d42; /* "BM" */
1724 bfh->bfSize = bmpsize;
1725 bfh->bfReserved1 = 0;
1726 bfh->bfReserved2 = 0;
1727 bfh->bfOffBits = sizeof(BITMAPFILEHEADER) + bitmap_info_size((BITMAPINFO*)dibdata, DIB_RGB_COLORS);
1729 /* rest of bitmap is the same as the packed dib */
1730 memcpy(bfh+1, dibdata, bmpsize-sizeof(BITMAPFILEHEADER));
1734 GlobalUnlock(hbmpdata);
1737 GlobalUnlock(hpackeddib);
1743 /**************************************************************************
1744 * X11DRV_CLIPBOARD_ExportMetaFilePict
1746 * Export MetaFilePict.
1748 static HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Display *display, Window requestor, Atom aTarget, Atom rprop,
1749 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1751 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1753 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1757 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, lpdata->hData, lpBytes, TRUE);
1761 /**************************************************************************
1762 * X11DRV_CLIPBOARD_ExportEnhMetaFile
1764 * Export EnhMetaFile.
1766 static HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Display *display, Window requestor, Atom aTarget, Atom rprop,
1767 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1769 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1771 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1775 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, lpdata->hData, lpBytes, TRUE);
1779 /**************************************************************************
1780 * get_html_description_field
1782 * Find the value of a field in an HTML Format description.
1784 static LPCSTR get_html_description_field(LPCSTR data, LPCSTR keyword)
1788 while (pos && *pos && *pos != '<')
1790 if (memcmp(pos, keyword, strlen(keyword)) == 0)
1791 return pos+strlen(keyword);
1793 pos = strchr(pos, '\n');
1801 /**************************************************************************
1802 * X11DRV_CLIPBOARD_ExportTextHtml
1804 * Export HTML Format to text/html.
1806 * FIXME: We should attempt to add an <a base> tag and convert windows paths.
1808 static HANDLE X11DRV_CLIPBOARD_ExportTextHtml(Display *display, Window requestor, Atom aTarget,
1809 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1813 LPCSTR data, field_value;
1814 UINT fragmentstart, fragmentend, htmlsize;
1815 HANDLE hhtmldata=NULL;
1820 if (!X11DRV_CLIPBOARD_RenderFormat(display, lpdata))
1822 ERR("Failed to export %04x format\n", lpdata->wFormatID);
1826 hdata = lpdata->hData;
1828 datasize = GlobalSize(hdata);
1830 data = GlobalLock(hdata);
1833 ERR("Failed to lock HTML Format data\n");
1837 /* read the important fields */
1838 field_value = get_html_description_field(data, "StartFragment:");
1841 ERR("Couldn't find StartFragment value\n");
1844 fragmentstart = atoi(field_value);
1846 field_value = get_html_description_field(data, "EndFragment:");
1849 ERR("Couldn't find EndFragment value\n");
1852 fragmentend = atoi(field_value);
1854 /* export only the fragment */
1855 htmlsize = fragmentend - fragmentstart + 1;
1857 hhtmldata = GlobalAlloc(0, htmlsize);
1861 htmldata = GlobalLock(hhtmldata);
1865 GlobalFree(hhtmldata);
1870 memcpy(htmldata, &data[fragmentstart], fragmentend-fragmentstart);
1871 htmldata[htmlsize-1] = '\0';
1873 *lpBytes = htmlsize;
1875 GlobalUnlock(htmldata);
1880 GlobalUnlock(hdata);
1886 /**************************************************************************
1887 * X11DRV_CLIPBOARD_QueryTargets
1889 static BOOL X11DRV_CLIPBOARD_QueryTargets(Display *display, Window w, Atom selection,
1890 Atom target, XEvent *xe)
1896 XConvertSelection(display, selection, target,
1897 x11drv_atom(SELECTION_DATA), w, CurrentTime);
1898 wine_tsx11_unlock();
1901 * Wait until SelectionNotify is received
1903 for (i = 0; i < SELECTION_RETRIES; i++)
1906 res = XCheckTypedWindowEvent(display, w, SelectionNotify, xe);
1907 wine_tsx11_unlock();
1908 if (res && xe->xselection.selection == selection) break;
1910 usleep(SELECTION_WAIT);
1913 if (i == SELECTION_RETRIES)
1915 ERR("Timed out waiting for SelectionNotify event\n");
1918 /* Verify that the selection returned a valid TARGETS property */
1919 if ((xe->xselection.target != target) || (xe->xselection.property == None))
1921 /* Selection owner failed to respond or we missed the SelectionNotify */
1922 WARN("Failed to retrieve TARGETS for selection %ld.\n", selection);
1930 static int is_atom_error( Display *display, XErrorEvent *event, void *arg )
1932 return (event->error_code == BadAtom);
1935 /**************************************************************************
1936 * X11DRV_CLIPBOARD_InsertSelectionProperties
1938 * Mark properties available for future retrieval.
1940 static VOID X11DRV_CLIPBOARD_InsertSelectionProperties(Display *display, Atom* properties, UINT count)
1942 UINT i, nb_atoms = 0;
1945 /* Cache these formats in the clipboard cache */
1946 for (i = 0; i < count; i++)
1948 LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupProperty(NULL, properties[i]);
1952 /* We found at least one Window's format that mapps to the property.
1953 * Continue looking for more.
1955 * If more than one property map to a Window's format then we use the first
1956 * one and ignore the rest.
1960 TRACE("Atom#%d Property(%d): --> FormatID(%04x) %s\n",
1961 i, lpFormat->drvData, lpFormat->wFormatID, debugstr_w(lpFormat->Name));
1962 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, lpFormat, FALSE);
1963 lpFormat = X11DRV_CLIPBOARD_LookupProperty(lpFormat, properties[i]);
1966 else if (properties[i])
1968 /* add it to the list of atoms that we don't know about yet */
1969 if (!atoms) atoms = HeapAlloc( GetProcessHeap(), 0,
1970 (count - i) * sizeof(*atoms) );
1971 if (atoms) atoms[nb_atoms++] = properties[i];
1975 /* query all unknown atoms in one go */
1978 char **names = HeapAlloc( GetProcessHeap(), 0, nb_atoms * sizeof(*names) );
1981 X11DRV_expect_error( display, is_atom_error, NULL );
1982 if (!XGetAtomNames( display, atoms, nb_atoms, names )) nb_atoms = 0;
1983 if (X11DRV_check_error())
1985 WARN( "got some bad atoms, ignoring\n" );
1988 for (i = 0; i < nb_atoms; i++)
1990 WINE_CLIPFORMAT *lpFormat;
1992 int len = MultiByteToWideChar(CP_UNIXCP, 0, names[i], -1, NULL, 0);
1993 wname = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1994 MultiByteToWideChar(CP_UNIXCP, 0, names[i], -1, wname, len);
1996 lpFormat = register_format( wname, atoms[i] );
1997 HeapFree(GetProcessHeap(), 0, wname);
2000 ERR("Failed to register %s property. Type will not be cached.\n", names[i]);
2003 TRACE("Atom#%d Property(%d): --> FormatID(%04x) %s\n",
2004 i, lpFormat->drvData, lpFormat->wFormatID, debugstr_w(lpFormat->Name));
2005 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, lpFormat, FALSE);
2008 for (i = 0; i < nb_atoms; i++) XFree( names[i] );
2009 wine_tsx11_unlock();
2010 HeapFree( GetProcessHeap(), 0, names );
2012 HeapFree( GetProcessHeap(), 0, atoms );
2017 /**************************************************************************
2018 * X11DRV_CLIPBOARD_QueryAvailableData
2020 * Caches the list of data formats available from the current selection.
2021 * This queries the selection owner for the TARGETS property and saves all
2022 * reported property types.
2024 static int X11DRV_CLIPBOARD_QueryAvailableData(Display *display, LPCLIPBOARDINFO lpcbinfo)
2027 Atom atype=AnyPropertyType;
2029 unsigned long remain;
2030 Atom* targetList=NULL;
2032 unsigned long cSelectionTargets = 0;
2034 if (selectionAcquired & (S_PRIMARY | S_CLIPBOARD))
2036 ERR("Received request to cache selection but process is owner=(%08x)\n",
2037 (unsigned) selectionWindow);
2038 return -1; /* Prevent self request */
2041 w = thread_selection_wnd();
2044 ERR("No window available to retrieve selection!\n");
2049 * Query the selection owner for the TARGETS property
2052 if ((use_primary_selection && XGetSelectionOwner(display,XA_PRIMARY)) ||
2053 XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
2055 wine_tsx11_unlock();
2056 if (use_primary_selection && (X11DRV_CLIPBOARD_QueryTargets(display, w, XA_PRIMARY, x11drv_atom(TARGETS), &xe)))
2057 selectionCacheSrc = XA_PRIMARY;
2058 else if (X11DRV_CLIPBOARD_QueryTargets(display, w, x11drv_atom(CLIPBOARD), x11drv_atom(TARGETS), &xe))
2059 selectionCacheSrc = x11drv_atom(CLIPBOARD);
2062 Atom xstr = XA_STRING;
2064 /* Selection Owner doesn't understand TARGETS, try retrieving XA_STRING */
2065 if (X11DRV_CLIPBOARD_QueryTargets(display, w, XA_PRIMARY, XA_STRING, &xe))
2067 X11DRV_CLIPBOARD_InsertSelectionProperties(display, &xstr, 1);
2068 selectionCacheSrc = XA_PRIMARY;
2071 else if (X11DRV_CLIPBOARD_QueryTargets(display, w, x11drv_atom(CLIPBOARD), XA_STRING, &xe))
2073 X11DRV_CLIPBOARD_InsertSelectionProperties(display, &xstr, 1);
2074 selectionCacheSrc = x11drv_atom(CLIPBOARD);
2079 WARN("Failed to query selection owner for available data.\n");
2084 else /* No selection owner so report 0 targets available */
2086 wine_tsx11_unlock();
2090 /* Read the TARGETS property contents */
2092 if(XGetWindowProperty(display, xe.xselection.requestor, xe.xselection.property,
2093 0, 0x3FFF, True, AnyPropertyType/*XA_ATOM*/, &atype, &aformat, &cSelectionTargets,
2094 &remain, (unsigned char**)&targetList) != Success)
2096 wine_tsx11_unlock();
2097 WARN("Failed to read TARGETS property\n");
2101 wine_tsx11_unlock();
2102 TRACE("Type %lx,Format %d,nItems %ld, Remain %ld\n",
2103 atype, aformat, cSelectionTargets, remain);
2105 * The TARGETS property should have returned us a list of atoms
2106 * corresponding to each selection target format supported.
2108 if (atype == XA_ATOM || atype == x11drv_atom(TARGETS))
2112 X11DRV_CLIPBOARD_InsertSelectionProperties(display, targetList, cSelectionTargets);
2114 else if (aformat == 8) /* work around quartz-wm brain damage */
2116 unsigned long i, count = cSelectionTargets / sizeof(CARD32);
2117 Atom *atoms = HeapAlloc( GetProcessHeap(), 0, count * sizeof(Atom) );
2118 for (i = 0; i < count; i++)
2119 atoms[i] = ((CARD32 *)targetList)[i]; /* FIXME: byte swapping */
2120 X11DRV_CLIPBOARD_InsertSelectionProperties( display, atoms, count );
2121 HeapFree( GetProcessHeap(), 0, atoms );
2125 /* Free the list of targets */
2128 wine_tsx11_unlock();
2131 return cSelectionTargets;
2135 /**************************************************************************
2136 * X11DRV_CLIPBOARD_ReadSelectionData
2138 * This method is invoked only when we DO NOT own the X selection
2140 * We always get the data from the selection client each time,
2141 * since we have no way of determining if the data in our cache is stale.
2143 static BOOL X11DRV_CLIPBOARD_ReadSelectionData(Display *display, LPWINE_CLIPDATA lpData)
2150 TRACE("%04x\n", lpData->wFormatID);
2152 if (!lpData->lpFormat)
2154 ERR("Requesting format %04x but no source format linked to data.\n",
2159 if (!selectionAcquired)
2161 Window w = thread_selection_wnd();
2164 ERR("No window available to read selection data!\n");
2168 TRACE("Requesting conversion of %s property (%d) from selection type %08x\n",
2169 debugstr_w(lpData->lpFormat->Name), lpData->lpFormat->drvData, (UINT)selectionCacheSrc);
2172 XConvertSelection(display, selectionCacheSrc, lpData->lpFormat->drvData,
2173 x11drv_atom(SELECTION_DATA), w, CurrentTime);
2174 wine_tsx11_unlock();
2176 /* wait until SelectionNotify is received */
2177 for (i = 0; i < SELECTION_RETRIES; i++)
2180 res = XCheckTypedWindowEvent(display, w, SelectionNotify, &xe);
2181 wine_tsx11_unlock();
2182 if (res && xe.xselection.selection == selectionCacheSrc) break;
2184 usleep(SELECTION_WAIT);
2187 if (i == SELECTION_RETRIES)
2189 ERR("Timed out waiting for SelectionNotify event\n");
2191 /* Verify that the selection returned a valid TARGETS property */
2192 else if (xe.xselection.property != None)
2195 * Read the contents of the X selection property
2196 * into WINE's clipboard cache and converting the
2197 * data format if necessary.
2199 HANDLE hData = lpData->lpFormat->lpDrvImportFunc(display, xe.xselection.requestor,
2200 xe.xselection.property);
2203 bRet = X11DRV_CLIPBOARD_InsertClipboardData(lpData->wFormatID, hData, 0, lpData->lpFormat, TRUE);
2205 TRACE("Import function failed\n");
2209 TRACE("Failed to convert selection\n");
2214 ERR("Received request to cache selection data but process is owner\n");
2217 TRACE("Returning %d\n", bRet);
2223 /**************************************************************************
2224 * X11DRV_CLIPBOARD_GetProperty
2225 * Gets type, data and size.
2227 static BOOL X11DRV_CLIPBOARD_GetProperty(Display *display, Window w, Atom prop,
2228 Atom *atype, unsigned char** data, unsigned long* datasize)
2231 unsigned long pos = 0, nitems, remain, count;
2232 unsigned char *val = NULL, *buffer;
2234 TRACE("Reading property %lu from X window %lx\n", prop, w);
2239 if (XGetWindowProperty(display, w, prop, pos, INT_MAX / 4, False,
2240 AnyPropertyType, atype, &aformat, &nitems, &remain, &buffer) != Success)
2242 wine_tsx11_unlock();
2243 WARN("Failed to read property\n");
2244 HeapFree( GetProcessHeap(), 0, val );
2248 count = get_property_size( aformat, nitems );
2249 if (!val) *data = HeapAlloc( GetProcessHeap(), 0, pos * sizeof(int) + count + 1 );
2250 else *data = HeapReAlloc( GetProcessHeap(), 0, val, pos * sizeof(int) + count + 1 );
2255 wine_tsx11_unlock();
2256 HeapFree( GetProcessHeap(), 0, val );
2260 memcpy( (int *)val + pos, buffer, count );
2262 wine_tsx11_unlock();
2265 *datasize = pos * sizeof(int) + count;
2269 pos += count / sizeof(int);
2272 /* Delete the property on the window now that we are done
2273 * This will send a PropertyNotify event to the selection owner. */
2275 XDeleteProperty(display, w, prop);
2276 wine_tsx11_unlock();
2281 /**************************************************************************
2282 * X11DRV_CLIPBOARD_ReadProperty
2283 * Reads the contents of the X selection property.
2285 static BOOL X11DRV_CLIPBOARD_ReadProperty(Display *display, Window w, Atom prop,
2286 unsigned char** data, unsigned long* datasize)
2294 if (!X11DRV_CLIPBOARD_GetProperty(display, w, prop, &atype, data, datasize))
2298 while (XCheckTypedWindowEvent(display, w, PropertyNotify, &xe))
2300 wine_tsx11_unlock();
2302 if (atype == x11drv_atom(INCR))
2304 unsigned char *buf = *data;
2305 unsigned long bufsize = 0;
2310 unsigned char *prop_data, *tmp;
2311 unsigned long prop_size;
2313 /* Wait until PropertyNotify is received */
2314 for (i = 0; i < SELECTION_RETRIES; i++)
2319 res = XCheckTypedWindowEvent(display, w, PropertyNotify, &xe);
2320 wine_tsx11_unlock();
2321 if (res && xe.xproperty.atom == prop &&
2322 xe.xproperty.state == PropertyNewValue)
2324 usleep(SELECTION_WAIT);
2327 if (i >= SELECTION_RETRIES ||
2328 !X11DRV_CLIPBOARD_GetProperty(display, w, prop, &atype, &prop_data, &prop_size))
2330 HeapFree(GetProcessHeap(), 0, buf);
2334 /* Retrieved entire data. */
2337 HeapFree(GetProcessHeap(), 0, prop_data);
2339 *datasize = bufsize;
2343 tmp = HeapReAlloc(GetProcessHeap(), 0, buf, bufsize + prop_size + 1);
2346 HeapFree(GetProcessHeap(), 0, buf);
2351 memcpy(buf + bufsize, prop_data, prop_size + 1);
2352 bufsize += prop_size;
2353 HeapFree(GetProcessHeap(), 0, prop_data);
2361 /**************************************************************************
2362 * CLIPBOARD_SerializeMetafile
2364 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out)
2368 TRACE(" wFormat=%d hdata=%p out=%d\n", wformat, hdata, out);
2370 if (out) /* Serialize out, caller should free memory */
2372 *lpcbytes = 0; /* Assume failure */
2374 if (wformat == CF_METAFILEPICT)
2376 LPMETAFILEPICT lpmfp = GlobalLock(hdata);
2377 unsigned int size = GetMetaFileBitsEx(lpmfp->hMF, 0, NULL);
2379 h = GlobalAlloc(0, size + sizeof(METAFILEPICT));
2382 char *pdata = GlobalLock(h);
2384 memcpy(pdata, lpmfp, sizeof(METAFILEPICT));
2385 GetMetaFileBitsEx(lpmfp->hMF, size, pdata + sizeof(METAFILEPICT));
2387 *lpcbytes = size + sizeof(METAFILEPICT);
2392 GlobalUnlock(hdata);
2394 else if (wformat == CF_ENHMETAFILE)
2396 int size = GetEnhMetaFileBits(hdata, 0, NULL);
2398 h = GlobalAlloc(0, size);
2401 LPVOID pdata = GlobalLock(h);
2403 GetEnhMetaFileBits(hdata, size, pdata);
2412 if (wformat == CF_METAFILEPICT)
2414 h = GlobalAlloc(0, sizeof(METAFILEPICT));
2417 unsigned int wiresize, size;
2418 LPMETAFILEPICT lpmfp = GlobalLock(h);
2420 memcpy(lpmfp, hdata, sizeof(METAFILEPICT));
2421 wiresize = *lpcbytes - sizeof(METAFILEPICT);
2422 lpmfp->hMF = SetMetaFileBitsEx(wiresize,
2423 ((const BYTE *)hdata) + sizeof(METAFILEPICT));
2424 size = GetMetaFileBitsEx(lpmfp->hMF, 0, NULL);
2428 else if (wformat == CF_ENHMETAFILE)
2430 h = SetEnhMetaFileBits(*lpcbytes, hdata);
2438 /**************************************************************************
2439 * X11DRV_CLIPBOARD_ReleaseSelection
2441 * Release XA_CLIPBOARD and XA_PRIMARY in response to a SelectionClear event.
2443 static void X11DRV_CLIPBOARD_ReleaseSelection(Display *display, Atom selType, Window w, HWND hwnd, Time time)
2445 /* w is the window that lost the selection
2447 TRACE("event->window = %08x (selectionWindow = %08x) selectionAcquired=0x%08x\n",
2448 (unsigned)w, (unsigned)selectionWindow, (unsigned)selectionAcquired);
2450 if (selectionAcquired && (w == selectionWindow))
2452 CLIPBOARDINFO cbinfo;
2454 /* completely give up the selection */
2455 TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
2457 X11DRV_CLIPBOARD_GetClipboardInfo(&cbinfo);
2459 if (cbinfo.flags & CB_PROCESS)
2461 /* Since we're still the owner, this wasn't initiated by
2462 another Wine process */
2463 if (OpenClipboard(hwnd))
2465 /* Destroy private objects */
2466 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
2468 /* Give up ownership of the windows clipboard */
2469 X11DRV_CLIPBOARD_ReleaseOwnership();
2474 if ((selType == x11drv_atom(CLIPBOARD)) && (selectionAcquired & S_PRIMARY))
2476 TRACE("Lost clipboard. Check if we need to release PRIMARY\n");
2479 if (selectionWindow == XGetSelectionOwner(display, XA_PRIMARY))
2481 TRACE("We still own PRIMARY. Releasing PRIMARY.\n");
2482 XSetSelectionOwner(display, XA_PRIMARY, None, time);
2485 TRACE("We no longer own PRIMARY\n");
2486 wine_tsx11_unlock();
2488 else if ((selType == XA_PRIMARY) && (selectionAcquired & S_CLIPBOARD))
2490 TRACE("Lost PRIMARY. Check if we need to release CLIPBOARD\n");
2493 if (selectionWindow == XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
2495 TRACE("We still own CLIPBOARD. Releasing CLIPBOARD.\n");
2496 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), None, time);
2499 TRACE("We no longer own CLIPBOARD\n");
2500 wine_tsx11_unlock();
2503 selectionWindow = None;
2505 X11DRV_EmptyClipboard(FALSE);
2507 /* Reset the selection flags now that we are done */
2508 selectionAcquired = S_NOSELECTION;
2513 /**************************************************************************
2514 * IsSelectionOwner (X11DRV.@)
2516 * Returns: TRUE if the selection is owned by this process, FALSE otherwise
2518 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void)
2520 return selectionAcquired;
2524 /**************************************************************************
2525 * X11DRV Clipboard Exports
2526 **************************************************************************/
2529 /**************************************************************************
2530 * RegisterClipboardFormat (X11DRV.@)
2532 * Registers a custom X clipboard format
2533 * Returns: Format id or 0 on failure
2535 UINT CDECL X11DRV_RegisterClipboardFormat(LPCWSTR FormatName)
2537 LPWINE_CLIPFORMAT lpFormat;
2539 if (FormatName == NULL) return 0;
2540 if (!(lpFormat = register_format( FormatName, 0 ))) return 0;
2541 return lpFormat->wFormatID;
2545 /**************************************************************************
2546 * X11DRV_GetClipboardFormatName
2548 INT CDECL X11DRV_GetClipboardFormatName(UINT wFormat, LPWSTR retStr, INT maxlen)
2550 LPWINE_CLIPFORMAT lpFormat;
2552 TRACE("(%04X, %p, %d) !\n", wFormat, retStr, maxlen);
2554 if (wFormat < 0xc000)
2556 SetLastError(ERROR_INVALID_PARAMETER);
2560 lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
2562 if (!lpFormat || (lpFormat->wFlags & CF_FLAG_BUILTINFMT))
2564 TRACE("Unknown format 0x%08x!\n", wFormat);
2565 SetLastError(ERROR_INVALID_HANDLE);
2569 lstrcpynW(retStr, lpFormat->Name, maxlen);
2571 return strlenW(retStr);
2574 static void selection_acquire(void)
2579 owner = thread_selection_wnd();
2580 display = thread_display();
2584 selectionAcquired = 0;
2585 selectionWindow = 0;
2587 /* Grab PRIMARY selection if not owned */
2588 if (use_primary_selection)
2589 XSetSelectionOwner(display, XA_PRIMARY, owner, CurrentTime);
2591 /* Grab CLIPBOARD selection if not owned */
2592 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), owner, CurrentTime);
2594 if (use_primary_selection && XGetSelectionOwner(display, XA_PRIMARY) == owner)
2595 selectionAcquired |= S_PRIMARY;
2597 if (XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)) == owner)
2598 selectionAcquired |= S_CLIPBOARD;
2600 wine_tsx11_unlock();
2602 if (selectionAcquired)
2604 selectionWindow = owner;
2605 TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner);
2609 static DWORD WINAPI selection_thread_proc(LPVOID p)
2615 selection_acquire();
2618 while (selectionAcquired)
2620 MsgWaitForMultipleObjectsEx(0, NULL, INFINITE, QS_SENDMESSAGE, 0);
2626 /**************************************************************************
2627 * AcquireClipboard (X11DRV.@)
2629 int CDECL X11DRV_AcquireClipboard(HWND hWndClipWindow)
2632 HANDLE selectionThread;
2634 TRACE(" %p\n", hWndClipWindow);
2637 * It's important that the selection get acquired from the thread
2638 * that owns the clipboard window. The primary reason is that we know
2639 * it is running a message loop and therefore can process the
2640 * X selection events.
2642 if (hWndClipWindow &&
2643 GetCurrentThreadId() != GetWindowThreadProcessId(hWndClipWindow, &procid))
2645 if (procid != GetCurrentProcessId())
2647 WARN("Setting clipboard owner to other process is not supported\n");
2648 hWndClipWindow = NULL;
2652 TRACE("Thread %x is acquiring selection with thread %x's window %p\n",
2653 GetCurrentThreadId(),
2654 GetWindowThreadProcessId(hWndClipWindow, NULL), hWndClipWindow);
2656 return SendMessageW(hWndClipWindow, WM_X11DRV_ACQUIRE_SELECTION, 0, 0);
2662 selection_acquire();
2666 HANDLE event = CreateEventW(NULL, FALSE, FALSE, NULL);
2667 selectionThread = CreateThread(NULL, 0, &selection_thread_proc, event, 0, NULL);
2669 if (!selectionThread)
2671 WARN("Could not start clipboard thread\n");
2675 WaitForSingleObject(event, INFINITE);
2677 CloseHandle(selectionThread);
2684 /**************************************************************************
2685 * X11DRV_EmptyClipboard
2687 * Empty cached clipboard data.
2689 void CDECL X11DRV_EmptyClipboard(BOOL keepunowned)
2693 LPWINE_CLIPDATA lpData, lpStart;
2694 LPWINE_CLIPDATA lpNext = ClipData;
2696 TRACE(" called with %d entries in cache.\n", ClipDataCount);
2702 lpNext = lpData->NextData;
2704 if (!keepunowned || !(lpData->wFlags & CF_FLAG_UNOWNED))
2706 lpData->PrevData->NextData = lpData->NextData;
2707 lpData->NextData->PrevData = lpData->PrevData;
2709 if (lpData == ClipData)
2710 ClipData = lpNext != lpData ? lpNext : NULL;
2712 X11DRV_CLIPBOARD_FreeData(lpData);
2713 HeapFree(GetProcessHeap(), 0, lpData);
2717 } while (lpNext != lpStart);
2720 TRACE(" %d entries remaining in cache.\n", ClipDataCount);
2725 /**************************************************************************
2726 * X11DRV_SetClipboardData
2728 BOOL CDECL X11DRV_SetClipboardData(UINT wFormat, HANDLE hData, BOOL owner)
2731 BOOL bResult = TRUE;
2733 /* If it's not owned, data can only be set if the format data is not already owned
2734 and its rendering is not delayed */
2737 CLIPBOARDINFO cbinfo;
2738 LPWINE_CLIPDATA lpRender;
2740 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2743 ((lpRender = X11DRV_CLIPBOARD_LookupData(wFormat)) &&
2744 !(lpRender->wFlags & CF_FLAG_UNOWNED)))
2747 flags = CF_FLAG_UNOWNED;
2750 bResult &= X11DRV_CLIPBOARD_InsertClipboardData(wFormat, hData, flags, NULL, TRUE);
2756 /**************************************************************************
2757 * CountClipboardFormats
2759 INT CDECL X11DRV_CountClipboardFormats(void)
2761 CLIPBOARDINFO cbinfo;
2763 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2765 TRACE(" count=%d\n", ClipDataCount);
2767 return ClipDataCount;
2771 /**************************************************************************
2772 * X11DRV_EnumClipboardFormats
2774 UINT CDECL X11DRV_EnumClipboardFormats(UINT wFormat)
2776 CLIPBOARDINFO cbinfo;
2777 UINT wNextFormat = 0;
2779 TRACE("(%04X)\n", wFormat);
2781 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2786 wNextFormat = ClipData->wFormatID;
2790 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormat);
2792 if (lpData && lpData->NextData != ClipData)
2793 wNextFormat = lpData->NextData->wFormatID;
2800 /**************************************************************************
2801 * X11DRV_IsClipboardFormatAvailable
2803 BOOL CDECL X11DRV_IsClipboardFormatAvailable(UINT wFormat)
2806 CLIPBOARDINFO cbinfo;
2808 TRACE("(%04X)\n", wFormat);
2810 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2812 if (wFormat != 0 && X11DRV_CLIPBOARD_LookupData(wFormat))
2815 TRACE("(%04X)- ret(%d)\n", wFormat, bRet);
2821 /**************************************************************************
2822 * GetClipboardData (USER.142)
2824 HANDLE CDECL X11DRV_GetClipboardData(UINT wFormat)
2826 CLIPBOARDINFO cbinfo;
2827 LPWINE_CLIPDATA lpRender;
2829 TRACE("(%04X)\n", wFormat);
2831 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2833 if ((lpRender = X11DRV_CLIPBOARD_LookupData(wFormat)))
2835 if ( !lpRender->hData )
2836 X11DRV_CLIPBOARD_RenderFormat(thread_init_display(), lpRender);
2838 TRACE(" returning %p (type %04x)\n", lpRender->hData, lpRender->wFormatID);
2839 return lpRender->hData;
2846 /**************************************************************************
2847 * ResetSelectionOwner
2849 * Called when the thread owning the selection is destroyed and we need to
2850 * preserve the selection ownership. We look for another top level window
2851 * in this process and send it a message to acquire the selection.
2853 void X11DRV_ResetSelectionOwner(void)
2860 if (!selectionAcquired || thread_selection_wnd() != selectionWindow)
2863 selectionAcquired = S_NOSELECTION;
2864 selectionWindow = 0;
2866 hwnd = GetWindow(GetDesktopWindow(), GW_CHILD);
2869 if (GetCurrentThreadId() != GetWindowThreadProcessId(hwnd, &procid))
2871 if (GetCurrentProcessId() == procid)
2873 if (SendMessageW(hwnd, WM_X11DRV_ACQUIRE_SELECTION, 0, 0))
2877 } while ((hwnd = GetWindow(hwnd, GW_HWNDNEXT)) != NULL);
2879 WARN("Failed to find another thread to take selection ownership. Clipboard data will be lost.\n");
2881 X11DRV_CLIPBOARD_ReleaseOwnership();
2882 X11DRV_EmptyClipboard(FALSE);
2886 /**************************************************************************
2887 * X11DRV_CLIPBOARD_SynthesizeData
2889 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID)
2892 LPWINE_CLIPDATA lpSource = NULL;
2894 TRACE(" %04x\n", wFormatID);
2896 /* Don't need to synthesize if it already exists */
2897 if (X11DRV_CLIPBOARD_LookupData(wFormatID))
2900 if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
2902 bsyn = ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
2903 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2904 ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
2905 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2906 ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
2907 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED);
2909 else if (wFormatID == CF_ENHMETAFILE)
2911 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT)) &&
2912 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2914 else if (wFormatID == CF_METAFILEPICT)
2916 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_ENHMETAFILE)) &&
2917 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2919 else if (wFormatID == CF_DIB)
2921 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) &&
2922 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2924 else if (wFormatID == CF_BITMAP)
2926 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) &&
2927 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2931 X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, 0, CF_FLAG_SYNTHESIZED, NULL, TRUE);
2938 /**************************************************************************
2939 * X11DRV_EndClipboardUpdate
2941 * Add locale if it hasn't already been added
2943 void CDECL X11DRV_EndClipboardUpdate(void)
2945 INT count = ClipDataCount;
2947 /* Do Unicode <-> Text <-> OEM mapping */
2948 X11DRV_CLIPBOARD_SynthesizeData(CF_TEXT);
2949 X11DRV_CLIPBOARD_SynthesizeData(CF_OEMTEXT);
2950 X11DRV_CLIPBOARD_SynthesizeData(CF_UNICODETEXT);
2952 /* Enhmetafile <-> MetafilePict mapping */
2953 X11DRV_CLIPBOARD_SynthesizeData(CF_ENHMETAFILE);
2954 X11DRV_CLIPBOARD_SynthesizeData(CF_METAFILEPICT);
2956 /* DIB <-> Bitmap mapping */
2957 X11DRV_CLIPBOARD_SynthesizeData(CF_DIB);
2958 X11DRV_CLIPBOARD_SynthesizeData(CF_BITMAP);
2960 TRACE("%d formats added to cached data\n", ClipDataCount - count);
2964 /***********************************************************************
2965 * X11DRV_SelectionRequest_TARGETS
2966 * Service a TARGETS selection request event
2968 static Atom X11DRV_SelectionRequest_TARGETS( Display *display, Window requestor,
2969 Atom target, Atom rprop )
2974 LPWINE_CLIPFORMAT lpFormats;
2975 LPWINE_CLIPDATA lpData;
2977 /* Create X atoms for any clipboard types which don't have atoms yet.
2978 * This avoids sending bogus zero atoms.
2979 * Without this, copying might not have access to all clipboard types.
2980 * FIXME: is it safe to call this here?
2985 * Count the number of items we wish to expose as selection targets.
2987 cTargets = 1; /* Include TARGETS */
2989 if (!(lpData = ClipData)) return None;
2993 lpFormats = ClipFormats;
2997 if ((lpFormats->wFormatID == lpData->wFormatID) &&
2998 lpFormats->lpDrvExportFunc && lpFormats->drvData)
3001 lpFormats = lpFormats->NextFormat;
3004 lpData = lpData->NextData;
3006 while (lpData != ClipData);
3008 TRACE(" found %d formats\n", cTargets);
3010 /* Allocate temp buffer */
3011 targets = HeapAlloc( GetProcessHeap(), 0, cTargets * sizeof(Atom));
3017 targets[i++] = x11drv_atom(TARGETS);
3021 lpFormats = ClipFormats;
3025 if ((lpFormats->wFormatID == lpData->wFormatID) &&
3026 lpFormats->lpDrvExportFunc && lpFormats->drvData)
3027 targets[i++] = lpFormats->drvData;
3029 lpFormats = lpFormats->NextFormat;
3032 lpData = lpData->NextData;
3034 while (lpData != ClipData);
3038 if (TRACE_ON(clipboard))
3041 for ( i = 0; i < cTargets; i++)
3043 char *itemFmtName = XGetAtomName(display, targets[i]);
3044 TRACE("\tAtom# %d: Property %ld Type %s\n", i, targets[i], itemFmtName);
3049 /* We may want to consider setting the type to xaTargets instead,
3050 * in case some apps expect this instead of XA_ATOM */
3051 XChangeProperty(display, requestor, rprop, XA_ATOM, 32,
3052 PropModeReplace, (unsigned char *)targets, cTargets);
3053 wine_tsx11_unlock();
3055 HeapFree(GetProcessHeap(), 0, targets);
3061 /***********************************************************************
3062 * X11DRV_SelectionRequest_MULTIPLE
3063 * Service a MULTIPLE selection request event
3064 * rprop contains a list of (target,property) atom pairs.
3065 * The first atom names a target and the second names a property.
3066 * The effect is as if we have received a sequence of SelectionRequest events
3067 * (one for each atom pair) except that:
3068 * 1. We reply with a SelectionNotify only when all the requested conversions
3069 * have been performed.
3070 * 2. If we fail to convert the target named by an atom in the MULTIPLE property,
3071 * we replace the atom in the property by None.
3073 static Atom X11DRV_SelectionRequest_MULTIPLE( HWND hWnd, XSelectionRequestEvent *pevent )
3075 Display *display = pevent->display;
3077 Atom atype=AnyPropertyType;
3079 unsigned long remain;
3080 Atom* targetPropList=NULL;
3081 unsigned long cTargetPropList = 0;
3083 /* If the specified property is None the requestor is an obsolete client.
3084 * We support these by using the specified target atom as the reply property.
3086 rprop = pevent->property;
3088 rprop = pevent->target;
3092 /* Read the MULTIPLE property contents. This should contain a list of
3093 * (target,property) atom pairs.
3096 if(XGetWindowProperty(display, pevent->requestor, rprop,
3097 0, 0x3FFF, False, AnyPropertyType, &atype,&aformat,
3098 &cTargetPropList, &remain,
3099 (unsigned char**)&targetPropList) != Success)
3101 wine_tsx11_unlock();
3102 TRACE("\tCouldn't read MULTIPLE property\n");
3106 TRACE("\tType %s,Format %d,nItems %ld, Remain %ld\n",
3107 XGetAtomName(display, atype), aformat, cTargetPropList, remain);
3108 wine_tsx11_unlock();
3111 * Make sure we got what we expect.
3112 * NOTE: According to the X-ICCCM Version 2.0 documentation the property sent
3113 * in a MULTIPLE selection request should be of type ATOM_PAIR.
3114 * However some X apps(such as XPaint) are not compliant with this and return
3115 * a user defined atom in atype when XGetWindowProperty is called.
3116 * The data *is* an atom pair but is not denoted as such.
3118 if(aformat == 32 /* atype == xAtomPair */ )
3122 /* Iterate through the ATOM_PAIR list and execute a SelectionRequest
3123 * for each (target,property) pair */
3125 for (i = 0; i < cTargetPropList; i+=2)
3127 XSelectionRequestEvent event;
3129 if (TRACE_ON(clipboard))
3131 char *targetName, *propName;
3133 targetName = XGetAtomName(display, targetPropList[i]);
3134 propName = XGetAtomName(display, targetPropList[i+1]);
3135 TRACE("MULTIPLE(%d): Target='%s' Prop='%s'\n",
3136 i/2, targetName, propName);
3139 wine_tsx11_unlock();
3142 /* We must have a non "None" property to service a MULTIPLE target atom */
3143 if ( !targetPropList[i+1] )
3145 TRACE("\tMULTIPLE(%d): Skipping target with empty property!\n", i);
3149 /* Set up an XSelectionRequestEvent for this (target,property) pair */
3151 event.target = targetPropList[i];
3152 event.property = targetPropList[i+1];
3154 /* Fire a SelectionRequest, informing the handler that we are processing
3155 * a MULTIPLE selection request event.
3157 X11DRV_HandleSelectionRequest( hWnd, &event, TRUE );
3161 /* Free the list of targets/properties */
3163 XFree(targetPropList);
3164 wine_tsx11_unlock();
3171 /***********************************************************************
3172 * X11DRV_HandleSelectionRequest
3173 * Process an event selection request event.
3174 * The bIsMultiple flag is used to signal when EVENT_SelectionRequest is called
3175 * recursively while servicing a "MULTIPLE" selection target.
3177 * Note: We only receive this event when WINE owns the X selection
3179 static void X11DRV_HandleSelectionRequest( HWND hWnd, XSelectionRequestEvent *event, BOOL bIsMultiple )
3181 Display *display = event->display;
3182 XSelectionEvent result;
3184 Window request = event->requestor;
3189 * We can only handle the selection request if :
3190 * The selection is PRIMARY or CLIPBOARD, AND we can successfully open the clipboard.
3191 * Don't do these checks or open the clipboard while recursively processing MULTIPLE,
3192 * since this has been already done.
3196 if (((event->selection != XA_PRIMARY) && (event->selection != x11drv_atom(CLIPBOARD))))
3200 /* If the specified property is None the requestor is an obsolete client.
3201 * We support these by using the specified target atom as the reply property.
3203 rprop = event->property;
3205 rprop = event->target;
3207 if(event->target == x11drv_atom(TARGETS)) /* Return a list of all supported targets */
3209 /* TARGETS selection request */
3210 rprop = X11DRV_SelectionRequest_TARGETS( display, request, event->target, rprop );
3212 else if(event->target == x11drv_atom(MULTIPLE)) /* rprop contains a list of (target, property) atom pairs */
3214 /* MULTIPLE selection request */
3215 rprop = X11DRV_SelectionRequest_MULTIPLE( hWnd, event );
3219 LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupProperty(NULL, event->target);
3221 if (lpFormat && lpFormat->lpDrvExportFunc)
3223 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(lpFormat->wFormatID);
3227 unsigned char* lpClipData;
3229 HANDLE hClipData = lpFormat->lpDrvExportFunc(display, request, event->target,
3230 rprop, lpData, &cBytes);
3232 if (hClipData && (lpClipData = GlobalLock(hClipData)))
3234 TRACE("\tUpdating property %s, %d bytes\n", debugstr_w(lpFormat->Name), cBytes);
3237 XChangeProperty(display, request, rprop, event->target,
3238 8, PropModeReplace, lpClipData, cBytes);
3239 wine_tsx11_unlock();
3241 GlobalUnlock(hClipData);
3242 GlobalFree(hClipData);
3250 * SelectionNotify should be sent only at the end of a MULTIPLE request
3254 result.type = SelectionNotify;
3255 result.display = display;
3256 result.requestor = request;
3257 result.selection = event->selection;
3258 result.property = rprop;
3259 result.target = event->target;
3260 result.time = event->time;
3261 TRACE("Sending SelectionNotify event...\n");
3263 XSendEvent(display,event->requestor,False,NoEventMask,(XEvent*)&result);
3264 wine_tsx11_unlock();
3269 /***********************************************************************
3270 * X11DRV_SelectionRequest
3272 void X11DRV_SelectionRequest( HWND hWnd, XEvent *event )
3274 X11DRV_HandleSelectionRequest( hWnd, &event->xselectionrequest, FALSE );
3278 /***********************************************************************
3279 * X11DRV_SelectionClear
3281 void X11DRV_SelectionClear( HWND hWnd, XEvent *xev )
3283 XSelectionClearEvent *event = &xev->xselectionclear;
3284 if (event->selection == XA_PRIMARY || event->selection == x11drv_atom(CLIPBOARD))
3285 X11DRV_CLIPBOARD_ReleaseSelection( event->display, event->selection,
3286 event->window, hWnd, event->time );
3289 /***********************************************************************
3290 * X11DRV_Clipboard_Cleanup
3292 void X11DRV_Clipboard_Cleanup(void)
3294 selectionAcquired = S_NOSELECTION;
3296 X11DRV_EmptyClipboard(FALSE);