2 * X11 clipboard windows driver
4 * Copyright 1994 Martin Ayotte
7 * 2003 Ulrich Czekalla for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * This file contains the X specific implementation for the windows
27 * Wine's internal clipboard is exposed to external apps via the X
28 * selection mechanism.
29 * Currently the driver asserts ownership via two selection atoms:
30 * 1. PRIMARY(XA_PRIMARY)
33 * In our implementation, the CLIPBOARD selection takes precedence over PRIMARY,
34 * i.e. if a CLIPBOARD selection is available, it is used instead of PRIMARY.
35 * When Wine takes ownership of the clipboard, it takes ownership of BOTH selections.
36 * While giving up selection ownership, if the CLIPBOARD selection is lost,
37 * it will lose both PRIMARY and CLIPBOARD and empty the clipboard.
38 * However if only PRIMARY is lost, it will continue to hold the CLIPBOARD selection
39 * (leaving the clipboard cache content unaffected).
41 * Every format exposed via a windows clipboard format is also exposed through
42 * a corresponding X selection target. A selection target atom is synthesized
43 * whenever a new Windows clipboard format is registered via RegisterClipboardFormat,
44 * or when a built-in format is used for the first time.
45 * Windows native format are exposed by prefixing the format name with "<WCF>"
46 * This allows us to uniquely identify windows native formats exposed by other
49 * In order to allow external applications to query WINE for supported formats,
50 * we respond to the "TARGETS" selection target. (See EVENT_SelectionRequest
51 * for implementation) We use the same mechanism to query external clients for
52 * availability of a particular format, by caching the list of available targets
53 * by using the clipboard cache's "delayed render" mechanism. If a selection client
54 * does not support the "TARGETS" selection target, we actually attempt to retrieve
55 * the format requested as a fallback mechanism.
57 * Certain Windows native formats are automatically converted to X native formats
58 * and vice versa. If a native format is available in the selection, it takes
59 * precedence, in order to avoid unnecessary conversions.
61 * FIXME: global format list needs a critical section
65 #include "wine/port.h"
80 #include "wine/wingdi16.h"
82 #include "wine/debug.h"
83 #include "wine/unicode.h"
84 #include "wine/server.h"
86 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
88 #define HGDIOBJ_32(handle16) ((HGDIOBJ)(ULONG_PTR)(handle16))
90 /* Maximum wait time for selection notify */
91 #define SELECTION_RETRIES 500 /* wait for .1 seconds */
92 #define SELECTION_WAIT 1000 /* us */
93 /* Minimum seconds that must lapse between owner queries */
94 #define OWNERQUERYLAPSETIME 1
97 #define S_NOSELECTION 0
108 } CLIPBOARDINFO, *LPCLIPBOARDINFO;
110 typedef struct tagWINE_CLIPDATA {
116 struct tagWINE_CLIPDATA *PrevData;
117 struct tagWINE_CLIPDATA *NextData;
118 } WINE_CLIPDATA, *LPWINE_CLIPDATA;
120 typedef HANDLE (*DRVEXPORTFUNC)(Window requestor, Atom aTarget, Atom rprop,
121 LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
122 typedef HANDLE (*DRVIMPORTFUNC)(LPBYTE hData, UINT cBytes);
124 typedef struct tagWINE_CLIPFORMAT {
129 DRVIMPORTFUNC lpDrvImportFunc;
130 DRVEXPORTFUNC lpDrvExportFunc;
131 struct tagWINE_CLIPFORMAT *PrevFormat;
132 struct tagWINE_CLIPFORMAT *NextFormat;
133 } WINE_CLIPFORMAT, *LPWINE_CLIPFORMAT;
135 #define CF_FLAG_BUILTINFMT 1 /* Built-in windows format */
136 #define CF_FLAG_UNOWNED 2 /* cached data is not owned */
137 #define CF_FLAG_SYNTHESIZED 8 /* Implicitly converted data */
139 static int selectionAcquired = 0; /* Contains the current selection masks */
140 static Window selectionWindow = None; /* The top level X window which owns the selection */
141 static Atom selectionCacheSrc = XA_PRIMARY; /* The selection source from which the clipboard cache was filled */
143 INT X11DRV_RegisterClipboardFormat(LPCWSTR FormatName);
144 void X11DRV_EmptyClipboard(BOOL keepunowned);
145 void X11DRV_EndClipboardUpdate(void);
146 static HANDLE X11DRV_CLIPBOARD_ImportClipboardData(LPBYTE lpdata, UINT cBytes);
147 static HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(LPBYTE lpdata, UINT cBytes);
148 static HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(LPBYTE lpdata, UINT cBytes);
149 static HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(LPBYTE lpdata, UINT cBytes);
150 static HANDLE X11DRV_CLIPBOARD_ImportXAString(LPBYTE lpdata, UINT cBytes);
151 static HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Window requestor, Atom aTarget,
152 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
153 static HANDLE X11DRV_CLIPBOARD_ExportString(Window requestor, Atom aTarget,
154 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
155 static HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Window requestor, Atom aTarget,
156 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
157 static HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Window requestor, Atom aTarget,
158 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
159 static HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Window requestor, Atom aTarget,
160 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
161 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(LPCWSTR FormatName, Atom prop);
162 static BOOL X11DRV_CLIPBOARD_ReadSelection(LPWINE_CLIPFORMAT lpData, Window w, Atom prop);
163 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(UINT wFormatID);
164 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData);
165 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void);
166 static int X11DRV_CLIPBOARD_QueryAvailableData(LPCLIPBOARDINFO lpcbinfo);
167 static BOOL X11DRV_CLIPBOARD_ReadClipboardData(UINT wFormat);
168 static BOOL X11DRV_CLIPBOARD_RenderFormat(LPWINE_CLIPDATA lpData);
169 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out);
170 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID);
171 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(LPWINE_CLIPDATA lpData);
172 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedDIB(void);
173 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedBitmap(void);
174 static void X11DRV_HandleSelectionRequest( HWND hWnd, XSelectionRequestEvent *event, BOOL bIsMultiple );
177 * WARNING: This data ordering is dependent on the WINE_CLIPFORMAT structure
178 * declared in clipboard.h
180 static const WCHAR wszCF_TEXT[] = {'W','C','F','_','T','E','X','T',0};
181 static const WCHAR wszCF_BITMAP[] = {'W','C','F','_','B','I','T','M','A','P',0};
182 static const WCHAR wszCF_METAFILEPICT[] = {'W','C','F','_','M','E','T','A','F','I','L','E','P','I','C','T',0};
183 static const WCHAR wszCF_SYLK[] = {'W','C','F','_','S','Y','L','K',0};
184 static const WCHAR wszCF_DIF[] = {'W','C','F','_','D','I','F',0};
185 static const WCHAR wszCF_TIFF[] = {'W','C','F','_','T','I','F','F',0};
186 static const WCHAR wszCF_OEMTEXT[] = {'W','C','F','_','O','E','M','T','E','X','T',0};
187 static const WCHAR wszCF_DIB[] = {'W','C','F','_','D','I','B',0};
188 static const WCHAR wszCF_PALETTE[] = {'W','C','F','_','P','A','L','E','T','T','E',0};
189 static const WCHAR wszCF_PENDATA[] = {'W','C','F','_','P','E','N','D','A','T','A',0};
190 static const WCHAR wszCF_RIFF[] = {'W','C','F','_','R','I','F','F',0};
191 static const WCHAR wszCF_WAVE[] = {'W','C','F','_','W','A','V','E',0};
192 static const WCHAR wszCF_UNICODETEXT[] = {'W','C','F','_','U','N','I','C','O','D','E','T','E','X','T',0};
193 static const WCHAR wszCF_ENHMETAFILE[] = {'W','C','F','_','E','N','H','M','E','T','A','F','I','L','E',0};
194 static const WCHAR wszCF_HDROP[] = {'W','C','F','_','H','D','R','O','P',0};
195 static const WCHAR wszCF_LOCALE[] = {'W','C','F','_','L','O','C','A','L','E',0};
196 static const WCHAR wszCF_DIBV5[] = {'W','C','F','_','D','I','B','V','5',0};
197 static const WCHAR wszCF_OWNERDISPLAY[] = {'W','C','F','_','O','W','N','E','R','D','I','S','P','L','A','Y',0};
198 static const WCHAR wszCF_DSPTEXT[] = {'W','C','F','_','D','S','P','T','E','X','T',0};
199 static const WCHAR wszCF_DSPBITMAP[] = {'W','C','F','_','D','S','P','B','I','T','M','A','P',0};
200 static const WCHAR wszCF_DSPMETAFILEPICT[] = {'W','C','F','_','D','S','P','M','E','T','A','F','I','L','E','P','I','C','T',0};
201 static const WCHAR wszCF_DSPENHMETAFILE[] = {'W','C','F','_','D','S','P','E','N','H','M','E','T','A','F','I','L','E',0};
203 static WINE_CLIPFORMAT ClipFormats[] =
205 { CF_TEXT, wszCF_TEXT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
206 X11DRV_CLIPBOARD_ExportClipboardData, NULL, &ClipFormats[1]},
208 { CF_BITMAP, wszCF_BITMAP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
209 NULL, &ClipFormats[0], &ClipFormats[2]},
211 { CF_METAFILEPICT, wszCF_METAFILEPICT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportMetaFilePict,
212 X11DRV_CLIPBOARD_ExportMetaFilePict, &ClipFormats[1], &ClipFormats[3]},
214 { CF_SYLK, wszCF_SYLK, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
215 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[2], &ClipFormats[4]},
217 { CF_DIF, wszCF_DIF, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
218 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[3], &ClipFormats[5]},
220 { CF_TIFF, wszCF_TIFF, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
221 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[4], &ClipFormats[6]},
223 { CF_OEMTEXT, wszCF_OEMTEXT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
224 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[5], &ClipFormats[7]},
226 { CF_DIB, wszCF_DIB, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportXAPIXMAP,
227 X11DRV_CLIPBOARD_ExportXAPIXMAP, &ClipFormats[6], &ClipFormats[8]},
229 { CF_PALETTE, wszCF_PALETTE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
230 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[7], &ClipFormats[9]},
232 { CF_PENDATA, wszCF_PENDATA, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
233 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[8], &ClipFormats[10]},
235 { CF_RIFF, wszCF_RIFF, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
236 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[9], &ClipFormats[11]},
238 { CF_WAVE, wszCF_WAVE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
239 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[10], &ClipFormats[12]},
241 { CF_UNICODETEXT, wszCF_UNICODETEXT, XA_STRING, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportXAString,
242 X11DRV_CLIPBOARD_ExportString, &ClipFormats[11], &ClipFormats[13]},
244 { CF_ENHMETAFILE, wszCF_ENHMETAFILE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportEnhMetaFile,
245 X11DRV_CLIPBOARD_ExportEnhMetaFile, &ClipFormats[12], &ClipFormats[14]},
247 { CF_HDROP, wszCF_HDROP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
248 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[13], &ClipFormats[15]},
250 { CF_LOCALE, wszCF_LOCALE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
251 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[14], &ClipFormats[16]},
253 { CF_DIBV5, wszCF_DIBV5, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
254 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[15], &ClipFormats[17]},
256 { CF_OWNERDISPLAY, wszCF_OWNERDISPLAY, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
257 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[16], &ClipFormats[18]},
259 { CF_DSPTEXT, wszCF_DSPTEXT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
260 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[17], &ClipFormats[19]},
262 { CF_DSPBITMAP, wszCF_DSPBITMAP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
263 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[18], &ClipFormats[20]},
265 { CF_DSPMETAFILEPICT, wszCF_DSPMETAFILEPICT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
266 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[19], &ClipFormats[21]},
268 { CF_DSPENHMETAFILE, wszCF_DSPENHMETAFILE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
269 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[20], NULL}
272 #define GET_ATOM(prop) (((prop) < FIRST_XATOM) ? (Atom)(prop) : X11DRV_Atoms[(prop) - FIRST_XATOM])
274 /* Maps X properties to Windows formats */
275 static const WCHAR wszRichTextFormat[] = {'R','i','c','h',' ','T','e','x','t',' ','F','o','r','m','a','t',0};
276 static const WCHAR wszGIF[] = {'G','I','F',0};
281 } PropertyFormatMap[] =
283 { wszRichTextFormat, XATOM_text_rtf },
284 /* Temporarily disable text/html because Evolution incorrectly pastes strings with extra nulls */
285 /*{ "text/html", "HTML Format" },*/
286 { wszGIF, XATOM_image_gif }
290 /* Maps equivalent X properties. It is assumed that lpszProperty must already
291 be in ClipFormats or PropertyFormatMap. */
294 UINT drvDataProperty;
296 } PropertyAliasMap[] =
298 /* DataProperty, DataAlias */
299 { XATOM_text_rtf, XATOM_text_richtext },
300 { XA_STRING, XATOM_COMPOUND_TEXT },
301 { XA_STRING, XATOM_TEXT },
302 { XATOM_WCF_DIB, XA_PIXMAP },
307 * Cached clipboard data.
309 static LPWINE_CLIPDATA ClipData = NULL;
310 static UINT ClipDataCount = 0;
313 * Clipboard sequence number
315 static UINT wSeqNo = 0;
317 /**************************************************************************
318 * Internal Clipboard implementation methods
319 **************************************************************************/
321 static Window thread_selection_wnd(void)
323 Window w = x11drv_thread_data()->selection_wnd;
328 w = XCreateWindow(thread_display(), root_window, 0, 0, 1, 1, 0, screen_depth,
329 InputOutput, CopyFromParent, 0, NULL);
333 x11drv_thread_data()->selection_wnd = w;
335 FIXME("Failed to create window. Fetching selection data will fail.\n");
341 /**************************************************************************
342 * X11DRV_InitClipboard
344 void X11DRV_InitClipboard(void)
348 /* Register known mapping between window formats and X properties */
349 for (i = 0; i < sizeof(PropertyFormatMap)/sizeof(PropertyFormatMap[0]); i++)
350 X11DRV_CLIPBOARD_InsertClipboardFormat(PropertyFormatMap[i].lpszFormat,
351 GET_ATOM(PropertyFormatMap[i].prop));
355 /**************************************************************************
358 * Intern atoms for formats that don't have one yet.
360 static void intern_atoms(void)
362 LPWINE_CLIPFORMAT format;
367 for (format = ClipFormats, count = 0; format; format = format->NextFormat)
368 if (!format->drvData) count++;
371 names = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*names) );
372 atoms = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*atoms) );
374 for (format = ClipFormats, i = 0; format; format = format->NextFormat) {
375 if (!format->drvData) {
376 len = WideCharToMultiByte(CP_UNIXCP, 0, format->Name, -1, NULL, 0, NULL, NULL);
377 names[i] = HeapAlloc(GetProcessHeap(), 0, len);
378 WideCharToMultiByte(CP_UNIXCP, 0, format->Name, -1, names[i++], len, NULL, NULL);
383 XInternAtoms( thread_display(), names, count, False, atoms );
386 for (format = ClipFormats, i = 0; format; format = format->NextFormat) {
387 if (!format->drvData) {
388 HeapFree(GetProcessHeap(), 0, names[i]);
389 format->drvData = atoms[i++];
393 HeapFree( GetProcessHeap(), 0, names );
394 HeapFree( GetProcessHeap(), 0, atoms );
398 /**************************************************************************
401 * Register a custom X clipboard format.
403 static WINE_CLIPFORMAT *register_format( LPCWSTR FormatName, Atom prop )
405 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
407 TRACE("%s\n", debugstr_w(FormatName));
409 /* walk format chain to see if it's already registered */
412 if (CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, lpFormat->Name, -1, FormatName, -1) == CSTR_EQUAL
413 && (lpFormat->wFlags & CF_FLAG_BUILTINFMT) == 0)
415 lpFormat = lpFormat->NextFormat;
418 return X11DRV_CLIPBOARD_InsertClipboardFormat(FormatName, prop);
422 /**************************************************************************
423 * X11DRV_CLIPBOARD_LookupFormat
425 LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupFormat(WORD wID)
427 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
431 if (lpFormat->wFormatID == wID)
434 lpFormat = lpFormat->NextFormat;
436 if (lpFormat && !lpFormat->drvData) intern_atoms();
441 /**************************************************************************
442 * X11DRV_CLIPBOARD_LookupProperty
444 LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupProperty(UINT drvData)
448 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
449 BOOL need_intern = FALSE;
453 if (lpFormat->drvData == drvData) return lpFormat;
454 if (!lpFormat->drvData) need_intern = TRUE;
455 lpFormat = lpFormat->NextFormat;
457 if (!need_intern) return NULL;
459 /* restart the search for the new atoms */
464 /**************************************************************************
465 * X11DRV_CLIPBOARD_LookupAliasProperty
467 LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupAliasProperty(UINT drvDataAlias)
470 LPWINE_CLIPFORMAT lpFormat = NULL;
472 for (i = 0; i < sizeof(PropertyAliasMap)/sizeof(PropertyAliasMap[0]); i++)
474 if (GET_ATOM(PropertyAliasMap[i].drvDataAlias) == drvDataAlias)
476 lpFormat = X11DRV_CLIPBOARD_LookupProperty(GET_ATOM(PropertyAliasMap[i].drvDataProperty));
485 /**************************************************************************
486 * X11DRV_CLIPBOARD_LookupPropertyAlias
488 UINT X11DRV_CLIPBOARD_LookupPropertyAlias(UINT drvDataProperty)
493 for (i = 0; i < sizeof(PropertyAliasMap)/sizeof(PropertyAliasMap[0]); i++)
495 if (GET_ATOM(PropertyAliasMap[i].drvDataProperty) == drvDataProperty)
497 alias = GET_ATOM(PropertyAliasMap[i].drvDataAlias);
506 /**************************************************************************
507 * X11DRV_CLIPBOARD_LookupData
509 LPWINE_CLIPDATA X11DRV_CLIPBOARD_LookupData(DWORD wID)
511 LPWINE_CLIPDATA lpData = ClipData;
517 if (lpData->wFormatID == wID)
520 lpData = lpData->NextData;
522 while(lpData != ClipData);
524 if (lpData->wFormatID != wID)
532 /**************************************************************************
533 * InsertClipboardFormat
535 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(LPCWSTR FormatName, Atom prop)
537 LPWINE_CLIPFORMAT lpFormat;
538 LPWINE_CLIPFORMAT lpNewFormat;
540 /* allocate storage for new format entry */
541 lpNewFormat = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPFORMAT));
543 if(lpNewFormat == NULL)
545 WARN("No more memory for a new format!\n");
549 if (!(lpNewFormat->Name = HeapAlloc(GetProcessHeap(), 0, (strlenW(FormatName)+1)*sizeof(WCHAR))))
551 WARN("No more memory for the new format name!\n");
552 HeapFree(GetProcessHeap(), 0, lpNewFormat);
556 strcpyW((LPWSTR)lpNewFormat->Name, FormatName);
557 lpNewFormat->wFlags = 0;
558 lpNewFormat->wFormatID = GlobalAddAtomW(lpNewFormat->Name);
559 lpNewFormat->drvData = prop;
560 lpNewFormat->lpDrvImportFunc = X11DRV_CLIPBOARD_ImportClipboardData;
561 lpNewFormat->lpDrvExportFunc = X11DRV_CLIPBOARD_ExportClipboardData;
564 lpFormat = ClipFormats;
566 while(lpFormat->NextFormat) /* Move to last entry */
567 lpFormat = lpFormat->NextFormat;
569 lpNewFormat->NextFormat = NULL;
570 lpFormat->NextFormat = lpNewFormat;
571 lpNewFormat->PrevFormat = lpFormat;
573 TRACE("Registering format(%d): %s drvData %d\n",
574 lpNewFormat->wFormatID, debugstr_w(FormatName), lpNewFormat->drvData);
582 /**************************************************************************
583 * X11DRV_CLIPBOARD_GetClipboardInfo
585 static BOOL X11DRV_CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
589 SERVER_START_REQ( set_clipboard_info )
593 if (wine_server_call_err( req ))
595 ERR("Failed to get clipboard owner.\n");
599 cbInfo->hWndOpen = reply->old_clipboard;
600 cbInfo->hWndOwner = reply->old_owner;
601 cbInfo->hWndViewer = reply->old_viewer;
602 cbInfo->seqno = reply->seqno;
603 cbInfo->flags = reply->flags;
614 /**************************************************************************
615 * X11DRV_CLIPBOARD_ReleaseOwnership
617 static BOOL X11DRV_CLIPBOARD_ReleaseOwnership(void)
621 SERVER_START_REQ( set_clipboard_info )
623 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
625 if (wine_server_call_err( req ))
627 ERR("Failed to set clipboard.\n");
641 /**************************************************************************
642 * X11DRV_CLIPBOARD_InsertClipboardData
644 * Caller *must* have the clipboard open and be the owner.
646 static BOOL X11DRV_CLIPBOARD_InsertClipboardData(UINT wFormat, HANDLE16 hData16, HANDLE hData32, DWORD flags)
648 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormat);
650 TRACE("format=%d lpData=%p hData16=%08x hData32=%08x flags=0x%08lx\n",
651 wFormat, lpData, hData16, (unsigned int)hData32, flags);
655 X11DRV_CLIPBOARD_FreeData(lpData);
657 lpData->hData16 = hData16; /* 0 is legal, see WM_RENDERFORMAT */
658 lpData->hData32 = hData32;
662 lpData = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPDATA));
664 lpData->wFormatID = wFormat;
665 lpData->hData16 = hData16; /* 0 is legal, see WM_RENDERFORMAT */
666 lpData->hData32 = hData32;
671 LPWINE_CLIPDATA lpPrevData = ClipData->PrevData;
673 lpData->PrevData = lpPrevData;
674 lpData->NextData = ClipData;
676 lpPrevData->NextData = lpData;
677 ClipData->PrevData = lpData;
681 lpData->NextData = lpData;
682 lpData->PrevData = lpData;
689 lpData->wFlags = flags;
695 /**************************************************************************
696 * X11DRV_CLIPBOARD_FreeData
698 * Free clipboard data handle.
700 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData)
702 TRACE("%d\n", lpData->wFormatID);
704 if ((lpData->wFormatID >= CF_GDIOBJFIRST &&
705 lpData->wFormatID <= CF_GDIOBJLAST) ||
706 lpData->wFormatID == CF_BITMAP ||
707 lpData->wFormatID == CF_DIB ||
708 lpData->wFormatID == CF_PALETTE)
711 DeleteObject(lpData->hData32);
714 DeleteObject(HGDIOBJ_32(lpData->hData16));
716 if ((lpData->wFormatID == CF_DIB) && lpData->drvData)
717 XFreePixmap(gdi_display, lpData->drvData);
719 else if (lpData->wFormatID == CF_METAFILEPICT)
723 DeleteMetaFile(((METAFILEPICT *)GlobalLock( lpData->hData32 ))->hMF );
724 GlobalFree(lpData->hData32);
727 /* HMETAFILE16 and HMETAFILE32 are apparently the same thing,
728 and a shallow copy is enough to share a METAFILEPICT
729 structure between 16bit and 32bit clipboards. The MetaFile
730 should of course only be deleted once. */
731 GlobalFree16(lpData->hData16);
736 METAFILEPICT16* lpMetaPict = (METAFILEPICT16 *) GlobalLock16(lpData->hData16);
740 /* To delete 16-bit meta file, we just need to free the associated
741 handle. See DeleteMetaFile16() in dlls/gdi/metafile.c. */
742 GlobalFree16(lpMetaPict->hMF);
746 GlobalFree16(lpData->hData16);
749 else if (lpData->wFormatID == CF_ENHMETAFILE)
752 DeleteEnhMetaFile(lpData->hData32);
754 else if (lpData->wFormatID < CF_PRIVATEFIRST ||
755 lpData->wFormatID > CF_PRIVATELAST)
758 GlobalFree(lpData->hData32);
761 GlobalFree16(lpData->hData16);
770 /**************************************************************************
771 * X11DRV_CLIPBOARD_UpdateCache
773 static BOOL X11DRV_CLIPBOARD_UpdateCache(LPCLIPBOARDINFO lpcbinfo)
777 if (!X11DRV_CLIPBOARD_IsSelectionOwner())
779 if (!X11DRV_CLIPBOARD_GetClipboardInfo(lpcbinfo))
781 ERR("Failed to retrieve clipboard information.\n");
784 else if (wSeqNo < lpcbinfo->seqno)
786 X11DRV_EmptyClipboard(TRUE);
788 if (X11DRV_CLIPBOARD_QueryAvailableData(lpcbinfo) < 0)
790 ERR("Failed to cache clipboard data owned by another process.\n");
795 X11DRV_EndClipboardUpdate();
798 wSeqNo = lpcbinfo->seqno;
806 /**************************************************************************
807 * X11DRV_CLIPBOARD_RenderFormat
809 static BOOL X11DRV_CLIPBOARD_RenderFormat(LPWINE_CLIPDATA lpData)
813 TRACE(" 0x%04x hData32(0x%08x) hData16(0x%08x)\n",
814 lpData->wFormatID, (unsigned int)lpData->hData32, lpData->hData16);
816 if (lpData->hData32 || lpData->hData16)
817 return bret; /* Already rendered */
819 if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
820 bret = X11DRV_CLIPBOARD_RenderSynthesizedFormat(lpData);
821 else if (!X11DRV_CLIPBOARD_IsSelectionOwner())
823 if (!X11DRV_CLIPBOARD_ReadClipboardData(lpData->wFormatID))
825 ERR("Failed to cache clipboard data owned by another process. Format=%d\n",
832 CLIPBOARDINFO cbInfo;
834 if (X11DRV_CLIPBOARD_GetClipboardInfo(&cbInfo) && cbInfo.hWndOwner)
836 /* Send a WM_RENDERFORMAT message to notify the owner to render the
837 * data requested into the clipboard.
839 TRACE("Sending WM_RENDERFORMAT message to hwnd(%p)\n", cbInfo.hWndOwner);
840 SendMessageW(cbInfo.hWndOwner, WM_RENDERFORMAT, (WPARAM)lpData->wFormatID, 0);
842 if (!lpData->hData32 && !lpData->hData16)
847 ERR("hWndClipOwner is lost!\n");
856 /**************************************************************************
857 * CLIPBOARD_ConvertText
858 * Returns number of required/converted characters - not bytes!
860 static INT CLIPBOARD_ConvertText(WORD src_fmt, void const *src, INT src_size,
861 WORD dst_fmt, void *dst, INT dst_size)
865 if(src_fmt == CF_UNICODETEXT)
878 return WideCharToMultiByte(cp, 0, src, src_size, dst, dst_size, NULL, NULL);
881 if(dst_fmt == CF_UNICODETEXT)
894 return MultiByteToWideChar(cp, 0, src, src_size, dst, dst_size);
897 if(!dst_size) return src_size;
899 if(dst_size > src_size) dst_size = src_size;
901 if(src_fmt == CF_TEXT )
902 CharToOemBuffA(src, dst, dst_size);
904 OemToCharBuffA(src, dst, dst_size);
910 /**************************************************************************
911 * X11DRV_CLIPBOARD_RenderSynthesizedFormat
913 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(LPWINE_CLIPDATA lpData)
919 if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
921 UINT wFormatID = lpData->wFormatID;
923 if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
924 bret = X11DRV_CLIPBOARD_RenderSynthesizedText(wFormatID);
930 bret = X11DRV_CLIPBOARD_RenderSynthesizedDIB();
934 bret = X11DRV_CLIPBOARD_RenderSynthesizedBitmap();
938 case CF_METAFILEPICT:
939 FIXME("Synthesizing wFormatID(0x%08x) not implemented\n", wFormatID);
943 FIXME("Called to synthesize unknown format\n");
948 lpData->wFlags &= ~CF_FLAG_SYNTHESIZED;
955 /**************************************************************************
956 * X11DRV_CLIPBOARD_RenderSynthesizedText
958 * Renders synthesized text
960 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(UINT wFormatID)
965 INT src_chars, dst_chars, alloc_size;
966 LPWINE_CLIPDATA lpSource = NULL;
968 TRACE(" %d\n", wFormatID);
970 if ((lpSource = X11DRV_CLIPBOARD_LookupData(wFormatID)) &&
974 /* Look for rendered source or non-synthesized source */
975 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
976 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
978 TRACE("UNICODETEXT -> %d\n", wFormatID);
980 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
981 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
983 TRACE("TEXT -> %d\n", wFormatID);
985 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
986 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
988 TRACE("OEMTEXT -> %d\n", wFormatID);
991 if (!lpSource || (lpSource->wFlags & CF_FLAG_SYNTHESIZED &&
995 /* Ask the clipboard owner to render the source text if necessary */
996 if (!lpSource->hData32 && !X11DRV_CLIPBOARD_RenderFormat(lpSource))
999 if (lpSource->hData32)
1001 lpstrS = (LPSTR)GlobalLock(lpSource->hData32);
1005 lpstrS = (LPSTR)GlobalLock16(lpSource->hData16);
1011 /* Text always NULL terminated */
1012 if(lpSource->wFormatID == CF_UNICODETEXT)
1013 src_chars = strlenW((LPCWSTR)lpstrS) + 1;
1015 src_chars = strlen(lpstrS) + 1;
1017 /* Calculate number of characters in the destination buffer */
1018 dst_chars = CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS,
1019 src_chars, wFormatID, NULL, 0);
1024 TRACE("Converting from '%d' to '%d', %i chars\n",
1025 lpSource->wFormatID, wFormatID, src_chars);
1027 /* Convert characters to bytes */
1028 if(wFormatID == CF_UNICODETEXT)
1029 alloc_size = dst_chars * sizeof(WCHAR);
1031 alloc_size = dst_chars;
1033 hData32 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE |
1034 GMEM_DDESHARE, alloc_size);
1036 lpstrT = (LPSTR)GlobalLock(hData32);
1040 CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS, src_chars,
1041 wFormatID, lpstrT, dst_chars);
1042 GlobalUnlock(hData32);
1046 if (lpSource->hData32)
1047 GlobalUnlock(lpSource->hData32);
1049 GlobalUnlock16(lpSource->hData16);
1051 return X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, 0, hData32, 0);
1055 /**************************************************************************
1056 * X11DRV_CLIPBOARD_RenderSynthesizedDIB
1058 * Renders synthesized DIB
1060 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedDIB()
1063 LPWINE_CLIPDATA lpSource = NULL;
1067 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) && lpSource->hData32)
1071 /* If we have a bitmap and it's not synthesized or it has been rendered */
1072 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) &&
1073 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
1075 /* Render source if required */
1076 if (lpSource->hData32 || X11DRV_CLIPBOARD_RenderFormat(lpSource))
1082 hData32 = X11DRV_DIB_CreateDIBFromBitmap(hdc, lpSource->hData32);
1083 ReleaseDC(NULL, hdc);
1087 X11DRV_CLIPBOARD_InsertClipboardData(CF_DIB, 0, hData32, 0);
1097 /**************************************************************************
1098 * X11DRV_CLIPBOARD_RenderSynthesizedBitmap
1100 * Renders synthesized bitmap
1102 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedBitmap()
1105 LPWINE_CLIPDATA lpSource = NULL;
1109 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) && lpSource->hData32)
1113 /* If we have a dib and it's not synthesized or it has been rendered */
1114 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) &&
1115 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
1117 /* Render source if required */
1118 if (lpSource->hData32 || X11DRV_CLIPBOARD_RenderFormat(lpSource))
1122 unsigned int offset;
1123 LPBITMAPINFOHEADER lpbmih;
1126 lpbmih = (LPBITMAPINFOHEADER) GlobalLock(lpSource->hData32);
1128 offset = sizeof(BITMAPINFOHEADER)
1129 + ((lpbmih->biBitCount <= 8) ? (sizeof(RGBQUAD) *
1130 (1 << lpbmih->biBitCount)) : 0);
1132 hData32 = CreateDIBitmap(hdc, lpbmih, CBM_INIT, (LPBYTE)lpbmih +
1133 offset, (LPBITMAPINFO) lpbmih, DIB_RGB_COLORS);
1135 GlobalUnlock(lpSource->hData32);
1136 ReleaseDC(NULL, hdc);
1140 X11DRV_CLIPBOARD_InsertClipboardData(CF_BITMAP, 0, hData32, 0);
1150 /**************************************************************************
1151 * X11DRV_CLIPBOARD_ImportXAString
1153 * Import XA_STRING, converting the string to CF_UNICODE.
1155 HANDLE X11DRV_CLIPBOARD_ImportXAString(LPBYTE lpdata, UINT cBytes)
1158 UINT i, inlcount = 0;
1159 HANDLE hUnicodeText = 0;
1161 for (i = 0; i <= cBytes; i++)
1163 if (lpdata[i] == '\n')
1167 if ((lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cBytes + inlcount + 1)))
1171 for (i = 0, inlcount = 0; i <= cBytes; i++)
1173 if (lpdata[i] == '\n')
1174 lpstr[inlcount++] = '\r';
1176 lpstr[inlcount++] = lpdata[i];
1179 count = MultiByteToWideChar(CP_UNIXCP, 0, lpstr, -1, NULL, 0);
1180 hUnicodeText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, count * sizeof(WCHAR));
1184 WCHAR *textW = GlobalLock(hUnicodeText);
1185 MultiByteToWideChar(CP_UNIXCP, 0, lpstr, -1, textW, count);
1186 GlobalUnlock(hUnicodeText);
1189 HeapFree(GetProcessHeap(), 0, lpstr);
1192 return hUnicodeText;
1196 /**************************************************************************
1197 * X11DRV_CLIPBOARD_ImportXAPIXMAP
1199 * Import XA_PIXMAP, converting the image to CF_DIB.
1201 HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(LPBYTE lpdata, UINT cBytes)
1203 HANDLE hTargetImage = 0; /* Handle to store the converted DIB */
1204 Pixmap *pPixmap = (Pixmap *) lpdata;
1205 HWND hwnd = GetOpenClipboardWindow();
1206 HDC hdc = GetDC(hwnd);
1208 hTargetImage = X11DRV_DIB_CreateDIBFromPixmap(*pPixmap, hdc);
1210 ReleaseDC(hwnd, hdc);
1212 return hTargetImage;
1216 /**************************************************************************
1217 * X11DRV_CLIPBOARD_ImportMetaFilePict
1219 * Import MetaFilePict.
1221 HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(LPBYTE lpdata, UINT cBytes)
1223 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, (HANDLE)lpdata, (LPDWORD)&cBytes, FALSE);
1227 /**************************************************************************
1228 * X11DRV_ImportEnhMetaFile
1230 * Import EnhMetaFile.
1232 HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(LPBYTE lpdata, UINT cBytes)
1234 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, (HANDLE)lpdata, (LPDWORD)&cBytes, FALSE);
1238 /**************************************************************************
1239 * X11DRV_ImportClipbordaData
1241 * Generic import clipboard data routine.
1243 HANDLE X11DRV_CLIPBOARD_ImportClipboardData(LPBYTE lpdata, UINT cBytes)
1246 HANDLE hClipData = 0;
1250 /* Turn on the DDESHARE flag to enable shared 32 bit memory */
1251 hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cBytes);
1252 if (hClipData == 0) return NULL;
1254 if ((lpClipData = GlobalLock(hClipData)))
1256 memcpy(lpClipData, lpdata, cBytes);
1257 GlobalUnlock(hClipData);
1260 GlobalFree(hClipData);
1269 /**************************************************************************
1270 X11DRV_CLIPBOARD_ExportClipboardData
1272 * Generic export clipboard data routine.
1274 HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Window requestor, Atom aTarget,
1275 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1279 HANDLE hClipData = 0;
1281 *lpBytes = 0; /* Assume failure */
1283 if (!X11DRV_CLIPBOARD_RenderFormat(lpData))
1284 ERR("Failed to export %d format\n", lpData->wFormatID);
1287 cBytes = GlobalSize(lpData->hData32);
1289 hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cBytes);
1290 if (hClipData == 0) return NULL;
1292 if ((lpClipData = GlobalLock(hClipData)))
1294 LPVOID lpdata = GlobalLock(lpData->hData32);
1296 memcpy(lpClipData, lpdata, cBytes);
1299 GlobalUnlock(lpData->hData32);
1300 GlobalUnlock(hClipData);
1302 GlobalFree(hClipData);
1311 /**************************************************************************
1312 * X11DRV_CLIPBOARD_ExportXAString
1314 * Export CF_UNICODE converting the string to XA_STRING.
1315 * Helper function for X11DRV_CLIPBOARD_ExportString.
1317 HANDLE X11DRV_CLIPBOARD_ExportXAString(LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1322 LPSTR text, lpstr = NULL;
1324 *lpBytes = 0; /* Assume return has zero bytes */
1326 uni_text = GlobalLock(lpData->hData32);
1328 size = WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, NULL, 0, NULL, NULL);
1330 text = HeapAlloc(GetProcessHeap(), 0, size);
1331 if (!text) goto done;
1332 WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, text, size, NULL, NULL);
1334 /* remove carriage returns */
1336 lpstr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size-- );
1337 if(lpstr == NULL) goto done;
1339 for(i = 0,j = 0; i < size && text[i]; i++ )
1341 if( text[i] == '\r' &&
1342 (text[i+1] == '\n' || text[i+1] == '\0') ) continue;
1343 lpstr[j++] = text[i];
1347 *lpBytes = j; /* Number of bytes in string */
1350 HeapFree(GetProcessHeap(), 0, text);
1351 GlobalUnlock(lpData->hData32);
1357 /**************************************************************************
1358 * X11DRV_CLIPBOARD_ExportCompoundText
1360 * Export CF_UNICODE to COMPOUND_TEXT or TEXT
1361 * Helper function for X11DRV_CLIPBOARD_ExportString.
1363 HANDLE X11DRV_CLIPBOARD_ExportCompoundText(Window requestor, Atom aTarget, Atom rprop,
1364 LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1366 Display *display = thread_display();
1369 XICCEncodingStyle style;
1371 lpstr = (char*) X11DRV_CLIPBOARD_ExportXAString(lpData, lpBytes);
1375 if (aTarget == x11drv_atom(COMPOUND_TEXT))
1376 style = XCompoundTextStyle;
1378 style = XStdICCTextStyle;
1380 /* Update the X property */
1382 if (XmbTextListToTextProperty(display, &lpstr, 1, style, &prop) == Success)
1384 XSetTextProperty(display, requestor, &prop, rprop);
1387 wine_tsx11_unlock();
1389 HeapFree( GetProcessHeap(), 0, lpstr );
1395 /**************************************************************************
1396 * X11DRV_CLIPBOARD_ExportString
1398 * Export CF_UNICODE string
1400 HANDLE X11DRV_CLIPBOARD_ExportString(Window requestor, Atom aTarget, Atom rprop,
1401 LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1403 if (X11DRV_CLIPBOARD_RenderFormat(lpData))
1405 if (aTarget == XA_STRING)
1406 return X11DRV_CLIPBOARD_ExportXAString(lpData, lpBytes);
1407 else if (aTarget == x11drv_atom(COMPOUND_TEXT) || aTarget == x11drv_atom(TEXT))
1408 return X11DRV_CLIPBOARD_ExportCompoundText(requestor, aTarget,
1409 rprop, lpData, lpBytes);
1411 ERR("Unknown target %ld to %d format\n", aTarget, lpData->wFormatID);
1414 ERR("Failed to render %d format\n", lpData->wFormatID);
1420 /**************************************************************************
1421 * X11DRV_CLIPBOARD_ExportXAPIXMAP
1423 * Export CF_DIB to XA_PIXMAP.
1425 HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Window requestor, Atom aTarget, Atom rprop,
1426 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1430 unsigned char* lpData;
1432 if (!X11DRV_CLIPBOARD_RenderFormat(lpdata))
1434 ERR("Failed to export %d format\n", lpdata->wFormatID);
1438 if (!lpdata->drvData) /* If not already rendered */
1440 /* For convert from packed DIB to Pixmap */
1442 lpdata->drvData = (UINT) X11DRV_DIB_CreatePixmapFromDIB(lpdata->hData32, hdc);
1446 *lpBytes = sizeof(Pixmap); /* pixmap is a 32bit value */
1448 /* Wrap pixmap so we can return a handle */
1449 hData = GlobalAlloc(0, *lpBytes);
1450 lpData = GlobalLock(hData);
1451 memcpy(lpData, &lpdata->drvData, *lpBytes);
1452 GlobalUnlock(hData);
1454 return (HANDLE) hData;
1458 /**************************************************************************
1459 * X11DRV_CLIPBOARD_ExportMetaFilePict
1461 * Export MetaFilePict.
1463 HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Window requestor, Atom aTarget, Atom rprop,
1464 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1466 if (!X11DRV_CLIPBOARD_RenderFormat(lpdata))
1468 ERR("Failed to export %d format\n", lpdata->wFormatID);
1472 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, (HANDLE)lpdata->hData32,
1477 /**************************************************************************
1478 * X11DRV_CLIPBOARD_ExportEnhMetaFile
1480 * Export EnhMetaFile.
1482 HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Window requestor, Atom aTarget, Atom rprop,
1483 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1485 if (!X11DRV_CLIPBOARD_RenderFormat(lpdata))
1487 ERR("Failed to export %d format\n", lpdata->wFormatID);
1491 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, (HANDLE)lpdata->hData32,
1496 /**************************************************************************
1497 * X11DRV_CLIPBOARD_QueryTargets
1499 static BOOL X11DRV_CLIPBOARD_QueryTargets(Display *display, Window w, Atom selection,
1500 Atom target, XEvent *xe)
1506 XConvertSelection(display, selection, target,
1507 x11drv_atom(SELECTION_DATA), w, CurrentTime);
1508 wine_tsx11_unlock();
1511 * Wait until SelectionNotify is received
1513 for (i = 0; i < SELECTION_RETRIES; i++)
1516 res = XCheckTypedWindowEvent(display, w, SelectionNotify, xe);
1517 wine_tsx11_unlock();
1518 if (res && xe->xselection.selection == selection) break;
1520 usleep(SELECTION_WAIT);
1523 /* Verify that the selection returned a valid TARGETS property */
1524 if ((xe->xselection.target != target) || (xe->xselection.property == None))
1526 /* Selection owner failed to respond or we missed the SelectionNotify */
1527 WARN("Failed to retrieve TARGETS for selection %ld.\n", selection);
1535 /**************************************************************************
1536 * X11DRV_CLIPBOARD_InsertSelectionProperties
1538 * Mark property available for future retrieval.
1540 static VOID X11DRV_CLIPBOARD_InsertSelectionProperties(Display *display, Atom* properties, UINT count)
1542 UINT i, nb_atoms = 0;
1545 /* Cache these formats in the clipboard cache */
1546 for (i = 0; i < count; i++)
1548 LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupProperty(properties[i]);
1551 lpFormat = X11DRV_CLIPBOARD_LookupAliasProperty(properties[i]);
1555 /* add it to the list of atoms that we don't know about yet */
1556 if (!atoms) atoms = HeapAlloc( GetProcessHeap(), 0,
1557 (count - i) * sizeof(*atoms) );
1558 if (atoms) atoms[nb_atoms++] = properties[i];
1562 TRACE("Atom#%d Property(%d): --> FormatID(%d) %s\n",
1563 i, lpFormat->drvData, lpFormat->wFormatID, debugstr_w(lpFormat->Name));
1564 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, 0);
1568 /* query all unknown atoms in one go */
1571 char **names = HeapAlloc( GetProcessHeap(), 0, nb_atoms * sizeof(*names) );
1575 XGetAtomNames( display, atoms, nb_atoms, names );
1576 wine_tsx11_unlock();
1577 for (i = 0; i < nb_atoms; i++)
1579 WINE_CLIPFORMAT *lpFormat;
1581 int len = MultiByteToWideChar(CP_UNIXCP, 0, names[i], -1, NULL, 0);
1582 wname = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1583 MultiByteToWideChar(CP_UNIXCP, 0, names[i], -1, wname, len);
1585 lpFormat = register_format( wname, atoms[i] );
1586 HeapFree(GetProcessHeap(), 0, wname);
1589 ERR("Failed to register %s property. Type will not be cached.\n", names[i]);
1592 TRACE("Atom#%d Property(%d): --> FormatID(%d) %s\n",
1593 i, lpFormat->drvData, lpFormat->wFormatID, debugstr_w(lpFormat->Name));
1594 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, 0);
1597 for (i = 0; i < nb_atoms; i++) XFree( names[i] );
1598 wine_tsx11_unlock();
1599 HeapFree( GetProcessHeap(), 0, names );
1601 HeapFree( GetProcessHeap(), 0, atoms );
1606 /**************************************************************************
1607 * X11DRV_CLIPBOARD_QueryAvailableData
1609 * Caches the list of data formats available from the current selection.
1610 * This queries the selection owner for the TARGETS property and saves all
1611 * reported property types.
1613 static int X11DRV_CLIPBOARD_QueryAvailableData(LPCLIPBOARDINFO lpcbinfo)
1615 Display *display = thread_display();
1617 Atom atype=AnyPropertyType;
1619 unsigned long remain;
1620 Atom* targetList=NULL;
1622 unsigned long cSelectionTargets = 0;
1624 if (selectionAcquired & (S_PRIMARY | S_CLIPBOARD))
1626 ERR("Received request to cache selection but process is owner=(%08x)\n",
1627 (unsigned) selectionWindow);
1628 return -1; /* Prevent self request */
1631 w = thread_selection_wnd();
1634 ERR("No window available to retrieve selection!\n");
1639 * Query the selection owner for the TARGETS property
1642 if ((use_primary_selection && XGetSelectionOwner(display,XA_PRIMARY)) ||
1643 XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
1645 wine_tsx11_unlock();
1646 if (use_primary_selection && (X11DRV_CLIPBOARD_QueryTargets(display, w, XA_PRIMARY, x11drv_atom(TARGETS), &xe)))
1647 selectionCacheSrc = XA_PRIMARY;
1648 else if (X11DRV_CLIPBOARD_QueryTargets(display, w, x11drv_atom(CLIPBOARD), x11drv_atom(TARGETS), &xe))
1649 selectionCacheSrc = x11drv_atom(CLIPBOARD);
1652 Atom xstr = XA_PRIMARY;
1654 /* Selection Owner doesn't understand TARGETS, try retrieving XA_STRING */
1655 if (X11DRV_CLIPBOARD_QueryTargets(display, w, XA_PRIMARY, XA_STRING, &xe))
1657 X11DRV_CLIPBOARD_InsertSelectionProperties(display, &xstr, 1);
1658 selectionCacheSrc = XA_PRIMARY;
1661 else if (X11DRV_CLIPBOARD_QueryTargets(display, w, x11drv_atom(CLIPBOARD), XA_STRING, &xe))
1663 X11DRV_CLIPBOARD_InsertSelectionProperties(display, &xstr, 1);
1664 selectionCacheSrc = x11drv_atom(CLIPBOARD);
1669 WARN("Failed to query selection owner for available data.\n");
1674 else /* No selection owner so report 0 targets available */
1676 wine_tsx11_unlock();
1680 /* Read the TARGETS property contents */
1682 if(XGetWindowProperty(display, xe.xselection.requestor, xe.xselection.property,
1683 0, 0x3FFF, True, AnyPropertyType/*XA_ATOM*/, &atype, &aformat, &cSelectionTargets,
1684 &remain, (unsigned char**)&targetList) != Success)
1686 wine_tsx11_unlock();
1687 WARN("Failed to read TARGETS property\n");
1691 wine_tsx11_unlock();
1692 TRACE("Type %lx,Format %d,nItems %ld, Remain %ld\n",
1693 atype, aformat, cSelectionTargets, remain);
1695 * The TARGETS property should have returned us a list of atoms
1696 * corresponding to each selection target format supported.
1698 if ((atype == XA_ATOM || atype == x11drv_atom(TARGETS)) && aformat == 32)
1699 X11DRV_CLIPBOARD_InsertSelectionProperties(display, targetList, cSelectionTargets);
1701 /* Free the list of targets */
1704 wine_tsx11_unlock();
1707 return cSelectionTargets;
1711 /**************************************************************************
1712 * X11DRV_CLIPBOARD_ReadClipboardData
1714 * This method is invoked only when we DO NOT own the X selection
1716 * We always get the data from the selection client each time,
1717 * since we have no way of determining if the data in our cache is stale.
1719 static BOOL X11DRV_CLIPBOARD_ReadClipboardData(UINT wFormat)
1721 Display *display = thread_display();
1724 LPWINE_CLIPFORMAT lpFormat;
1726 TRACE("%d\n", wFormat);
1728 if (!selectionAcquired)
1730 Window w = thread_selection_wnd();
1733 ERR("No window available to read selection data!\n");
1737 lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
1739 if (lpFormat && lpFormat->drvData)
1745 TRACE("Requesting %s selection (%d) from win(%08x)\n",
1746 debugstr_w(lpFormat->Name), lpFormat->drvData, (UINT)selectionCacheSrc);
1749 XConvertSelection(display, selectionCacheSrc, lpFormat->drvData,
1750 x11drv_atom(SELECTION_DATA), w, CurrentTime);
1751 wine_tsx11_unlock();
1753 /* wait until SelectionNotify is received */
1754 for (i = 0; i < SELECTION_RETRIES; i++)
1757 res = XCheckTypedWindowEvent(display, w, SelectionNotify, &xe);
1758 wine_tsx11_unlock();
1759 if (res && xe.xselection.selection == selectionCacheSrc) break;
1761 usleep(SELECTION_WAIT);
1764 /* If the property wasn't available check for aliases */
1765 if (xe.xselection.property == None &&
1766 (alias = X11DRV_CLIPBOARD_LookupPropertyAlias(lpFormat->drvData)))
1769 XConvertSelection(display, selectionCacheSrc, alias,
1770 x11drv_atom(SELECTION_DATA), w, CurrentTime);
1771 wine_tsx11_unlock();
1773 /* wait until SelectionNotify is received */
1774 for (i = 0; i < SELECTION_RETRIES; i++)
1777 res = XCheckTypedWindowEvent(display, w, SelectionNotify, &xe);
1778 wine_tsx11_unlock();
1779 if (res && xe.xselection.selection == selectionCacheSrc) break;
1781 usleep(SELECTION_WAIT);
1785 /* Verify that the selection returned a valid TARGETS property */
1786 if (xe.xselection.property != None)
1789 * Read the contents of the X selection property
1790 * into WINE's clipboard cache converting the
1791 * selection to be compatible if possible.
1793 bRet = X11DRV_CLIPBOARD_ReadSelection(lpFormat, xe.xselection.requestor,
1794 xe.xselection.property);
1800 ERR("Received request to cache selection data but process is owner\n");
1803 TRACE("Returning %d\n", bRet);
1809 /**************************************************************************
1810 * X11DRV_CLIPBOARD_ReadSelection
1811 * Reads the contents of the X selection property into the WINE clipboard cache
1812 * converting the selection into a format compatible with the windows clipboard
1814 * This method is invoked only to read the contents of a the selection owned
1815 * by an external application. i.e. when we do not own the X selection.
1817 static BOOL X11DRV_CLIPBOARD_ReadSelection(LPWINE_CLIPFORMAT lpData, Window w, Atom prop)
1819 Display *display = thread_display();
1820 Atom atype=AnyPropertyType;
1822 unsigned long total,nitems,remain,val_cnt;
1825 unsigned char* buffer;
1831 TRACE("Reading X selection type %s\n", debugstr_w(lpData->Name));
1834 * First request a zero length in order to figure out the request size.
1837 if(XGetWindowProperty(display,w,prop,0,0,False, AnyPropertyType,
1838 &atype, &aformat, &nitems, &remain, &buffer) != Success)
1840 wine_tsx11_unlock();
1841 WARN("Failed to get property size\n");
1845 /* Free zero length return data if any */
1853 reqlen = remain * bwc;
1855 TRACE("Retrieving %ld bytes\n", reqlen);
1857 val = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, reqlen);
1859 /* Read property in 4K blocks */
1860 for (total = 0, val_cnt = 0; remain;)
1862 if (XGetWindowProperty(display, w, prop, (total / 4), 4096, False,
1863 AnyPropertyType, &atype, &aformat, &nitems, &remain, &buffer) != Success)
1865 wine_tsx11_unlock();
1866 WARN("Failed to read property\n");
1867 HeapFree(GetProcessHeap(), 0, val);
1872 memcpy(&val[val_cnt], buffer, nitems * bwc);
1873 val_cnt += nitems * bwc;
1874 total += nitems*bwc;
1877 wine_tsx11_unlock();
1879 bRet = X11DRV_CLIPBOARD_InsertClipboardData(lpData->wFormatID, 0, lpData->lpDrvImportFunc(val, total), 0);
1881 /* Delete the property on the window now that we are done
1882 * This will send a PropertyNotify event to the selection owner. */
1884 XDeleteProperty(display,w,prop);
1885 wine_tsx11_unlock();
1887 /* Free the retrieved property data */
1888 HeapFree(GetProcessHeap(),0,val);
1894 /**************************************************************************
1895 * CLIPBOARD_SerializeMetafile
1897 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out)
1901 TRACE(" wFormat=%d hdata=%08x out=%d\n", wformat, (unsigned int) hdata, out);
1903 if (out) /* Serialize out, caller should free memory */
1905 *lpcbytes = 0; /* Assume failure */
1907 if (wformat == CF_METAFILEPICT)
1909 LPMETAFILEPICT lpmfp = (LPMETAFILEPICT) GlobalLock(hdata);
1910 unsigned int size = GetMetaFileBitsEx(lpmfp->hMF, 0, NULL);
1912 h = GlobalAlloc(0, size + sizeof(METAFILEPICT));
1915 char *pdata = GlobalLock(h);
1917 memcpy(pdata, lpmfp, sizeof(METAFILEPICT));
1918 GetMetaFileBitsEx(lpmfp->hMF, size, pdata + sizeof(METAFILEPICT));
1920 *lpcbytes = size + sizeof(METAFILEPICT);
1925 GlobalUnlock(hdata);
1927 else if (wformat == CF_ENHMETAFILE)
1929 int size = GetEnhMetaFileBits(hdata, 0, NULL);
1931 h = GlobalAlloc(0, size);
1934 LPVOID pdata = GlobalLock(h);
1936 GetEnhMetaFileBits(hdata, size, pdata);
1945 if (wformat == CF_METAFILEPICT)
1947 h = GlobalAlloc(0, sizeof(METAFILEPICT));
1950 unsigned int wiresize, size;
1951 LPMETAFILEPICT lpmfp = (LPMETAFILEPICT) GlobalLock(h);
1953 memcpy(lpmfp, (LPVOID)hdata, sizeof(METAFILEPICT));
1954 wiresize = *lpcbytes - sizeof(METAFILEPICT);
1955 lpmfp->hMF = SetMetaFileBitsEx(wiresize,
1956 ((const char *)hdata) + sizeof(METAFILEPICT));
1957 size = GetMetaFileBitsEx(lpmfp->hMF, 0, NULL);
1961 else if (wformat == CF_ENHMETAFILE)
1963 h = SetEnhMetaFileBits(*lpcbytes, (LPVOID)hdata);
1971 /**************************************************************************
1972 * X11DRV_CLIPBOARD_ReleaseSelection
1974 * Release XA_CLIPBOARD and XA_PRIMARY in response to a SelectionClear event.
1976 void X11DRV_CLIPBOARD_ReleaseSelection(Atom selType, Window w, HWND hwnd, Time time)
1978 Display *display = thread_display();
1980 /* w is the window that lost the selection
1982 TRACE("event->window = %08x (selectionWindow = %08x) selectionAcquired=0x%08x\n",
1983 (unsigned)w, (unsigned)selectionWindow, (unsigned)selectionAcquired);
1985 if (selectionAcquired && (w == selectionWindow))
1987 CLIPBOARDINFO cbinfo;
1989 /* completely give up the selection */
1990 TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
1992 X11DRV_CLIPBOARD_GetClipboardInfo(&cbinfo);
1994 if (cbinfo.flags & CB_PROCESS)
1996 /* Since we're still the owner, this wasn't initiated by
1997 another Wine process */
1998 if (OpenClipboard(hwnd))
2000 /* Destroy private objects */
2001 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
2003 /* Give up ownership of the windows clipboard */
2004 X11DRV_CLIPBOARD_ReleaseOwnership();
2009 if ((selType == x11drv_atom(CLIPBOARD)) && (selectionAcquired & S_PRIMARY))
2011 TRACE("Lost clipboard. Check if we need to release PRIMARY\n");
2014 if (selectionWindow == XGetSelectionOwner(display, XA_PRIMARY))
2016 TRACE("We still own PRIMARY. Releasing PRIMARY.\n");
2017 XSetSelectionOwner(display, XA_PRIMARY, None, time);
2020 TRACE("We no longer own PRIMARY\n");
2021 wine_tsx11_unlock();
2023 else if ((selType == XA_PRIMARY) && (selectionAcquired & S_CLIPBOARD))
2025 TRACE("Lost PRIMARY. Check if we need to release CLIPBOARD\n");
2028 if (selectionWindow == XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
2030 TRACE("We still own CLIPBOARD. Releasing CLIPBOARD.\n");
2031 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), None, time);
2034 TRACE("We no longer own CLIPBOARD\n");
2035 wine_tsx11_unlock();
2038 selectionWindow = None;
2040 X11DRV_EmptyClipboard(FALSE);
2042 /* Reset the selection flags now that we are done */
2043 selectionAcquired = S_NOSELECTION;
2048 /**************************************************************************
2049 * IsSelectionOwner (X11DRV.@)
2051 * Returns: TRUE if the selection is owned by this process, FALSE otherwise
2053 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void)
2055 return selectionAcquired;
2059 /**************************************************************************
2060 * X11DRV Clipboard Exports
2061 **************************************************************************/
2064 /**************************************************************************
2065 * RegisterClipboardFormat (X11DRV.@)
2067 * Registers a custom X clipboard format
2068 * Returns: Format id or 0 on failure
2070 INT X11DRV_RegisterClipboardFormat(LPCWSTR FormatName)
2072 LPWINE_CLIPFORMAT lpFormat;
2074 if (FormatName == NULL) return 0;
2075 if (!(lpFormat = register_format( FormatName, 0 ))) return 0;
2076 return lpFormat->wFormatID;
2080 /**************************************************************************
2081 * X11DRV_GetClipboardFormatName
2083 INT X11DRV_GetClipboardFormatName(UINT wFormat, LPWSTR retStr, INT maxlen)
2085 LPWINE_CLIPFORMAT lpFormat;
2087 TRACE("(%04X, %p, %d) !\n", wFormat, retStr, maxlen);
2089 if (wFormat < 0xc000)
2091 SetLastError(ERROR_INVALID_PARAMETER);
2095 lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
2097 if (!lpFormat || (lpFormat->wFlags & CF_FLAG_BUILTINFMT))
2099 TRACE("Unknown format 0x%08x!\n", wFormat);
2100 SetLastError(ERROR_INVALID_HANDLE);
2104 lstrcpynW(retStr, lpFormat->Name, maxlen);
2106 return strlenW(retStr);
2110 /**************************************************************************
2111 * AcquireClipboard (X11DRV.@)
2113 void X11DRV_AcquireClipboard(HWND hWndClipWindow)
2115 Display *display = thread_display();
2118 * Acquire X selection if we don't already own it.
2119 * Note that we only acquire the selection if it hasn't been already
2120 * acquired by us, and ignore the fact that another X window may be
2121 * asserting ownership. The reason for this is we need *any* top level
2122 * X window to hold selection ownership. The actual clipboard data requests
2123 * are made via GetClipboardData from EVENT_SelectionRequest and this
2124 * ensures that the real HWND owner services the request.
2125 * If the owning X window gets destroyed the selection ownership is
2126 * re-cycled to another top level X window in X11DRV_CLIPBOARD_ResetOwner.
2129 if (!(selectionAcquired == (S_PRIMARY | S_CLIPBOARD)))
2133 if (!hWndClipWindow)
2134 hWndClipWindow = GetActiveWindow();
2136 hWndClipWindow = GetAncestor(hWndClipWindow, GA_ROOT);
2138 if (GetCurrentThreadId() != GetWindowThreadProcessId(hWndClipWindow, NULL))
2140 TRACE("Thread %lx is acquiring selection with thread %lx's window %p\n",
2141 GetCurrentThreadId(),
2142 GetWindowThreadProcessId(hWndClipWindow, NULL),
2144 if (!SendMessageW(hWndClipWindow, WM_X11DRV_ACQUIRE_SELECTION, 0, 0))
2145 ERR("Failed to acquire selection\n");
2149 owner = X11DRV_get_whole_window(hWndClipWindow);
2152 /* Grab PRIMARY selection if not owned */
2153 if (use_primary_selection && !(selectionAcquired & S_PRIMARY))
2154 XSetSelectionOwner(display, XA_PRIMARY, owner, CurrentTime);
2156 /* Grab CLIPBOARD selection if not owned */
2157 if (!(selectionAcquired & S_CLIPBOARD))
2158 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), owner, CurrentTime);
2160 if (use_primary_selection && XGetSelectionOwner(display,XA_PRIMARY) == owner)
2161 selectionAcquired |= S_PRIMARY;
2163 if (XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)) == owner)
2164 selectionAcquired |= S_CLIPBOARD;
2165 wine_tsx11_unlock();
2167 if (selectionAcquired)
2169 selectionWindow = owner;
2170 TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner);
2175 ERR("Received request to acquire selection but process is already owner=(%08x)\n", (unsigned) selectionWindow);
2180 /**************************************************************************
2181 * X11DRV_EmptyClipboard
2183 * Empty cached clipboard data.
2185 void X11DRV_EmptyClipboard(BOOL keepunowned)
2189 LPWINE_CLIPDATA lpData, lpStart;
2190 LPWINE_CLIPDATA lpNext = ClipData;
2192 TRACE(" called with %d entries in cache.\n", ClipDataCount);
2198 lpNext = lpData->NextData;
2200 if (!keepunowned || !(lpData->wFlags & CF_FLAG_UNOWNED))
2202 lpData->PrevData->NextData = lpData->NextData;
2203 lpData->NextData->PrevData = lpData->PrevData;
2205 if (lpData == ClipData)
2206 ClipData = lpNext != lpData ? lpNext : NULL;
2208 X11DRV_CLIPBOARD_FreeData(lpData);
2209 HeapFree(GetProcessHeap(), 0, lpData);
2213 } while (lpNext != lpStart);
2216 TRACE(" %d entries remaining in cache.\n", ClipDataCount);
2221 /**************************************************************************
2222 * X11DRV_SetClipboardData
2224 BOOL X11DRV_SetClipboardData(UINT wFormat, HANDLE16 hData16, HANDLE hData32, BOOL owner)
2227 BOOL bResult = TRUE;
2229 /* If it's not owned, data can only be set if the format data is not already owned
2230 and its rendering is not delayed */
2233 CLIPBOARDINFO cbinfo;
2234 LPWINE_CLIPDATA lpRender;
2236 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2238 if ((!hData16 && !hData32) ||
2239 ((lpRender = X11DRV_CLIPBOARD_LookupData(wFormat)) &&
2240 !(lpRender->wFlags & CF_FLAG_UNOWNED)))
2243 flags = CF_FLAG_UNOWNED;
2246 bResult &= X11DRV_CLIPBOARD_InsertClipboardData(wFormat, hData16, hData32, flags);
2252 /**************************************************************************
2253 * CountClipboardFormats
2255 INT X11DRV_CountClipboardFormats(void)
2257 CLIPBOARDINFO cbinfo;
2259 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2261 TRACE(" count=%d\n", ClipDataCount);
2263 return ClipDataCount;
2267 /**************************************************************************
2268 * X11DRV_EnumClipboardFormats
2270 UINT X11DRV_EnumClipboardFormats(UINT wFormat)
2272 CLIPBOARDINFO cbinfo;
2273 UINT wNextFormat = 0;
2275 TRACE("(%04X)\n", wFormat);
2277 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2282 wNextFormat = ClipData->wFormatID;
2286 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormat);
2288 if (lpData && lpData->NextData != ClipData)
2289 wNextFormat = lpData->NextData->wFormatID;
2296 /**************************************************************************
2297 * X11DRV_IsClipboardFormatAvailable
2299 BOOL X11DRV_IsClipboardFormatAvailable(UINT wFormat)
2302 CLIPBOARDINFO cbinfo;
2304 TRACE("(%04X)\n", wFormat);
2306 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2308 if (wFormat != 0 && X11DRV_CLIPBOARD_LookupData(wFormat))
2311 TRACE("(%04X)- ret(%d)\n", wFormat, bRet);
2317 /**************************************************************************
2318 * GetClipboardData (USER.142)
2320 BOOL X11DRV_GetClipboardData(UINT wFormat, HANDLE16* phData16, HANDLE* phData32)
2322 CLIPBOARDINFO cbinfo;
2323 LPWINE_CLIPDATA lpRender;
2325 TRACE("(%04X)\n", wFormat);
2327 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2329 if ((lpRender = X11DRV_CLIPBOARD_LookupData(wFormat)))
2331 if ( !lpRender->hData32 )
2332 X11DRV_CLIPBOARD_RenderFormat(lpRender);
2334 /* Convert between 32 -> 16 bit data, if necessary */
2335 if (lpRender->hData32 && !lpRender->hData16)
2339 if (lpRender->wFormatID == CF_METAFILEPICT)
2340 size = sizeof(METAFILEPICT16);
2342 size = GlobalSize(lpRender->hData32);
2344 lpRender->hData16 = GlobalAlloc16(GMEM_ZEROINIT, size);
2346 if (!lpRender->hData16)
2347 ERR("(%04X) -- not enough memory in 16b heap\n", wFormat);
2350 if (lpRender->wFormatID == CF_METAFILEPICT)
2352 FIXME("\timplement function CopyMetaFilePict32to16\n");
2353 FIXME("\tin the appropriate file.\n");
2354 #ifdef SOMEONE_IMPLEMENTED_ME
2355 CopyMetaFilePict32to16(GlobalLock16(lpRender->hData16),
2356 GlobalLock(lpRender->hData32));
2361 memcpy(GlobalLock16(lpRender->hData16),
2362 GlobalLock(lpRender->hData32), size);
2365 GlobalUnlock16(lpRender->hData16);
2366 GlobalUnlock(lpRender->hData32);
2370 /* Convert between 32 -> 16 bit data, if necessary */
2371 if (lpRender->hData16 && !lpRender->hData32)
2375 if (lpRender->wFormatID == CF_METAFILEPICT)
2376 size = sizeof(METAFILEPICT16);
2378 size = GlobalSize(lpRender->hData32);
2380 lpRender->hData32 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE |
2381 GMEM_DDESHARE, size);
2383 if (lpRender->wFormatID == CF_METAFILEPICT)
2385 FIXME("\timplement function CopyMetaFilePict16to32\n");
2386 FIXME("\tin the appropriate file.\n");
2387 #ifdef SOMEONE_IMPLEMENTED_ME
2388 CopyMetaFilePict16to32(GlobalLock16(lpRender->hData32),
2389 GlobalLock(lpRender->hData16));
2394 memcpy(GlobalLock(lpRender->hData32),
2395 GlobalLock16(lpRender->hData16), size);
2398 GlobalUnlock(lpRender->hData32);
2399 GlobalUnlock16(lpRender->hData16);
2403 *phData16 = lpRender->hData16;
2406 *phData32 = lpRender->hData32;
2408 TRACE(" returning hData16(%04x) hData32(%04x) (type %d)\n",
2409 lpRender->hData16, (unsigned int) lpRender->hData32, lpRender->wFormatID);
2411 return lpRender->hData16 || lpRender->hData32;
2418 /**************************************************************************
2419 * ResetSelectionOwner (X11DRV.@)
2421 * Called from DestroyWindow() to prevent X selection from being lost when
2422 * a top level window is destroyed, by switching ownership to another top
2424 * Any top level window can own the selection. See X11DRV_CLIPBOARD_Acquire
2425 * for a more detailed description of this.
2427 void X11DRV_ResetSelectionOwner(HWND hwnd, BOOL bFooBar)
2429 Display *display = thread_display();
2430 HWND hWndClipOwner = 0;
2432 Window XWnd = X11DRV_get_whole_window(hwnd);
2433 BOOL bLostSelection = FALSE;
2434 Window selectionPrevWindow;
2436 /* There is nothing to do if we don't own the selection,
2437 * or if the X window which currently owns the selection is different
2438 * from the one passed in.
2440 if (!selectionAcquired || XWnd != selectionWindow
2441 || selectionWindow == None )
2444 if ((bFooBar && XWnd) || (!bFooBar && !XWnd))
2447 hWndClipOwner = GetClipboardOwner();
2449 TRACE("clipboard owner = %p, selection window = %08x\n",
2450 hWndClipOwner, (unsigned)selectionWindow);
2452 /* now try to salvage current selection from being destroyed by X */
2453 TRACE("checking %08x\n", (unsigned) XWnd);
2455 selectionPrevWindow = selectionWindow;
2456 selectionWindow = None;
2458 if (!(tmp = GetWindow(hwnd, GW_HWNDNEXT)))
2459 tmp = GetWindow(hwnd, GW_HWNDFIRST);
2461 if (tmp && tmp != hwnd)
2462 selectionWindow = X11DRV_get_whole_window(tmp);
2464 if (selectionWindow != None)
2466 /* We must pretend that we don't own the selection while making the switch
2467 * since a SelectionClear event will be sent to the last owner.
2468 * If there is no owner X11DRV_CLIPBOARD_ReleaseSelection will do nothing.
2470 int saveSelectionState = selectionAcquired;
2471 selectionAcquired = S_NOSELECTION;
2473 TRACE("\tswitching selection from %08x to %08x\n",
2474 (unsigned)selectionPrevWindow, (unsigned)selectionWindow);
2478 /* Assume ownership for the PRIMARY and CLIPBOARD selection */
2479 if (saveSelectionState & S_PRIMARY)
2480 XSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime);
2482 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), selectionWindow, CurrentTime);
2484 /* Restore the selection masks */
2485 selectionAcquired = saveSelectionState;
2487 /* Lose the selection if something went wrong */
2488 if (((saveSelectionState & S_PRIMARY) &&
2489 (XGetSelectionOwner(display, XA_PRIMARY) != selectionWindow)) ||
2490 (XGetSelectionOwner(display, x11drv_atom(CLIPBOARD)) != selectionWindow))
2492 bLostSelection = TRUE;
2494 wine_tsx11_unlock();
2498 bLostSelection = TRUE;
2503 TRACE("Lost the selection!\n");
2505 X11DRV_CLIPBOARD_ReleaseOwnership();
2506 selectionAcquired = S_NOSELECTION;
2507 selectionWindow = 0;
2512 /**************************************************************************
2513 * X11DRV_CLIPBOARD_SynthesizeData
2515 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID)
2518 LPWINE_CLIPDATA lpSource = NULL;
2520 TRACE(" %d\n", wFormatID);
2522 /* Don't need to synthesize if it already exists */
2523 if (X11DRV_CLIPBOARD_LookupData(wFormatID))
2526 if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
2528 bsyn = ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
2529 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2530 ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
2531 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2532 ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
2533 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED);
2535 else if (wFormatID == CF_ENHMETAFILE)
2537 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT)) &&
2538 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2540 else if (wFormatID == CF_METAFILEPICT)
2542 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT)) &&
2543 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2545 else if (wFormatID == CF_DIB)
2547 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) &&
2548 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2550 else if (wFormatID == CF_BITMAP)
2552 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) &&
2553 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2557 X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, 0, 0, CF_FLAG_SYNTHESIZED);
2564 /**************************************************************************
2565 * X11DRV_EndClipboardUpdate
2567 * Add locale if it hasn't already been added
2569 void X11DRV_EndClipboardUpdate(void)
2571 INT count = ClipDataCount;
2573 /* Do Unicode <-> Text <-> OEM mapping */
2574 X11DRV_CLIPBOARD_SynthesizeData(CF_UNICODETEXT);
2575 X11DRV_CLIPBOARD_SynthesizeData(CF_TEXT);
2576 X11DRV_CLIPBOARD_SynthesizeData(CF_OEMTEXT);
2578 /* Enhmetafile <-> MetafilePict mapping */
2579 X11DRV_CLIPBOARD_SynthesizeData(CF_ENHMETAFILE);
2580 X11DRV_CLIPBOARD_SynthesizeData(CF_METAFILEPICT);
2582 /* DIB <-> Bitmap mapping */
2583 X11DRV_CLIPBOARD_SynthesizeData(CF_DIB);
2584 X11DRV_CLIPBOARD_SynthesizeData(CF_BITMAP);
2586 TRACE("%d formats added to cached data\n", ClipDataCount - count);
2590 /***********************************************************************
2593 * Utility function for X11DRV_SelectionRequest_TARGETS.
2595 static BOOL add_targets(Atom* targets, unsigned long cTargets, Atom prop)
2600 /* Scan through what we have so far to avoid duplicates */
2601 for (i = 0, bExists = FALSE; i < cTargets; i++)
2603 if (targets[i] == prop)
2611 targets[cTargets] = prop;
2617 /***********************************************************************
2618 * X11DRV_SelectionRequest_TARGETS
2619 * Service a TARGETS selection request event
2621 static Atom X11DRV_SelectionRequest_TARGETS( Display *display, Window requestor,
2622 Atom target, Atom rprop )
2628 LPWINE_CLIPFORMAT lpFormat;
2631 * Count the number of items we wish to expose as selection targets.
2632 * We include the TARGETS item, and property aliases
2634 cTargets = X11DRV_CountClipboardFormats() + 1;
2636 for (wFormat = 0; (wFormat = X11DRV_EnumClipboardFormats(wFormat));)
2638 lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
2642 if (!lpFormat->lpDrvExportFunc)
2645 if (X11DRV_CLIPBOARD_LookupPropertyAlias(lpFormat->drvData))
2648 /* else most likely unregistered format such as CF_PRIVATE or CF_GDIOBJ */
2651 TRACE(" found %ld formats\n", cTargets);
2653 /* Allocate temp buffer */
2654 targets = HeapAlloc( GetProcessHeap(), 0, cTargets * sizeof(Atom));
2658 /* Create TARGETS property list (First item in list is TARGETS itself) */
2659 for (targets[0] = x11drv_atom(TARGETS), cTargets = 1, wFormat = 0;
2660 (wFormat = X11DRV_EnumClipboardFormats(wFormat));)
2662 lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
2666 if (lpFormat->lpDrvExportFunc)
2668 if (add_targets(targets, cTargets, lpFormat->drvData))
2672 /* Check if any alias should be listed */
2673 alias = X11DRV_CLIPBOARD_LookupPropertyAlias(lpFormat->drvData);
2676 if (add_targets(targets, cTargets, alias))
2684 if (TRACE_ON(clipboard))
2687 for ( i = 0; i < cTargets; i++)
2691 char *itemFmtName = XGetAtomName(display, targets[i]);
2692 TRACE("\tAtom# %d: Property %ld Type %s\n", i, targets[i], itemFmtName);
2698 /* We may want to consider setting the type to xaTargets instead,
2699 * in case some apps expect this instead of XA_ATOM */
2700 XChangeProperty(display, requestor, rprop, XA_ATOM, 32,
2701 PropModeReplace, (unsigned char *)targets, cTargets);
2702 wine_tsx11_unlock();
2704 HeapFree(GetProcessHeap(), 0, targets);
2710 /***********************************************************************
2711 * X11DRV_SelectionRequest_MULTIPLE
2712 * Service a MULTIPLE selection request event
2713 * rprop contains a list of (target,property) atom pairs.
2714 * The first atom names a target and the second names a property.
2715 * The effect is as if we have received a sequence of SelectionRequest events
2716 * (one for each atom pair) except that:
2717 * 1. We reply with a SelectionNotify only when all the requested conversions
2718 * have been performed.
2719 * 2. If we fail to convert the target named by an atom in the MULTIPLE property,
2720 * we replace the atom in the property by None.
2722 static Atom X11DRV_SelectionRequest_MULTIPLE( HWND hWnd, XSelectionRequestEvent *pevent )
2724 Display *display = pevent->display;
2726 Atom atype=AnyPropertyType;
2728 unsigned long remain;
2729 Atom* targetPropList=NULL;
2730 unsigned long cTargetPropList = 0;
2732 /* If the specified property is None the requestor is an obsolete client.
2733 * We support these by using the specified target atom as the reply property.
2735 rprop = pevent->property;
2737 rprop = pevent->target;
2741 /* Read the MULTIPLE property contents. This should contain a list of
2742 * (target,property) atom pairs.
2745 if(XGetWindowProperty(display, pevent->requestor, rprop,
2746 0, 0x3FFF, False, AnyPropertyType, &atype,&aformat,
2747 &cTargetPropList, &remain,
2748 (unsigned char**)&targetPropList) != Success)
2750 wine_tsx11_unlock();
2751 TRACE("\tCouldn't read MULTIPLE property\n");
2755 TRACE("\tType %s,Format %d,nItems %ld, Remain %ld\n",
2756 XGetAtomName(display, atype), aformat, cTargetPropList, remain);
2757 wine_tsx11_unlock();
2760 * Make sure we got what we expect.
2761 * NOTE: According to the X-ICCCM Version 2.0 documentation the property sent
2762 * in a MULTIPLE selection request should be of type ATOM_PAIR.
2763 * However some X apps(such as XPaint) are not compliant with this and return
2764 * a user defined atom in atype when XGetWindowProperty is called.
2765 * The data *is* an atom pair but is not denoted as such.
2767 if(aformat == 32 /* atype == xAtomPair */ )
2771 /* Iterate through the ATOM_PAIR list and execute a SelectionRequest
2772 * for each (target,property) pair */
2774 for (i = 0; i < cTargetPropList; i+=2)
2776 XSelectionRequestEvent event;
2778 if (TRACE_ON(clipboard))
2780 char *targetName, *propName;
2782 targetName = XGetAtomName(display, targetPropList[i]);
2783 propName = XGetAtomName(display, targetPropList[i+1]);
2784 TRACE("MULTIPLE(%d): Target='%s' Prop='%s'\n",
2785 i/2, targetName, propName);
2788 wine_tsx11_unlock();
2791 /* We must have a non "None" property to service a MULTIPLE target atom */
2792 if ( !targetPropList[i+1] )
2794 TRACE("\tMULTIPLE(%d): Skipping target with empty property!\n", i);
2798 /* Set up an XSelectionRequestEvent for this (target,property) pair */
2799 memcpy( &event, pevent, sizeof(XSelectionRequestEvent) );
2800 event.target = targetPropList[i];
2801 event.property = targetPropList[i+1];
2803 /* Fire a SelectionRequest, informing the handler that we are processing
2804 * a MULTIPLE selection request event.
2806 X11DRV_HandleSelectionRequest( hWnd, &event, TRUE );
2810 /* Free the list of targets/properties */
2812 XFree(targetPropList);
2813 wine_tsx11_unlock();
2820 /***********************************************************************
2821 * X11DRV_HandleSelectionRequest
2822 * Process an event selection request event.
2823 * The bIsMultiple flag is used to signal when EVENT_SelectionRequest is called
2824 * recursively while servicing a "MULTIPLE" selection target.
2826 * Note: We only receive this event when WINE owns the X selection
2828 static void X11DRV_HandleSelectionRequest( HWND hWnd, XSelectionRequestEvent *event, BOOL bIsMultiple )
2830 Display *display = event->display;
2831 XSelectionEvent result;
2833 Window request = event->requestor;
2838 * We can only handle the selection request if :
2839 * The selection is PRIMARY or CLIPBOARD, AND we can successfully open the clipboard.
2840 * Don't do these checks or open the clipboard while recursively processing MULTIPLE,
2841 * since this has been already done.
2845 if (((event->selection != XA_PRIMARY) && (event->selection != x11drv_atom(CLIPBOARD))))
2849 /* If the specified property is None the requestor is an obsolete client.
2850 * We support these by using the specified target atom as the reply property.
2852 rprop = event->property;
2854 rprop = event->target;
2856 if(event->target == x11drv_atom(TARGETS)) /* Return a list of all supported targets */
2858 /* TARGETS selection request */
2859 rprop = X11DRV_SelectionRequest_TARGETS( display, request, event->target, rprop );
2861 else if(event->target == x11drv_atom(MULTIPLE)) /* rprop contains a list of (target, property) atom pairs */
2863 /* MULTIPLE selection request */
2864 rprop = X11DRV_SelectionRequest_MULTIPLE( hWnd, event );
2868 LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupProperty(event->target);
2871 lpFormat = X11DRV_CLIPBOARD_LookupAliasProperty(event->target);
2873 if (lpFormat && lpFormat->lpDrvExportFunc)
2875 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(lpFormat->wFormatID);
2879 unsigned char* lpClipData;
2881 HANDLE hClipData = lpFormat->lpDrvExportFunc(request, event->target,
2882 rprop, lpData, &cBytes);
2884 if (hClipData && (lpClipData = GlobalLock(hClipData)))
2886 TRACE("\tUpdating property %s, %ld bytes\n", debugstr_w(lpFormat->Name), cBytes);
2889 XChangeProperty(display, request, rprop, event->target,
2890 8, PropModeReplace, (unsigned char *)lpClipData, cBytes);
2891 wine_tsx11_unlock();
2893 GlobalUnlock(hClipData);
2894 GlobalFree(hClipData);
2902 * SelectionNotify should be sent only at the end of a MULTIPLE request
2906 result.type = SelectionNotify;
2907 result.display = display;
2908 result.requestor = request;
2909 result.selection = event->selection;
2910 result.property = rprop;
2911 result.target = event->target;
2912 result.time = event->time;
2913 TRACE("Sending SelectionNotify event...\n");
2915 XSendEvent(display,event->requestor,False,NoEventMask,(XEvent*)&result);
2916 wine_tsx11_unlock();
2921 /***********************************************************************
2922 * X11DRV_SelectionRequest
2924 void X11DRV_SelectionRequest( HWND hWnd, XEvent *event )
2927 X11DRV_HandleSelectionRequest( hWnd, &event->xselectionrequest, FALSE );
2931 /***********************************************************************
2932 * X11DRV_SelectionClear
2934 void X11DRV_SelectionClear( HWND hWnd, XEvent *xev )
2936 XSelectionClearEvent *event = &xev->xselectionclear;
2938 if (event->selection == XA_PRIMARY || event->selection == x11drv_atom(CLIPBOARD))
2939 X11DRV_CLIPBOARD_ReleaseSelection( event->selection, event->window, hWnd, event->time );