Added full support for xbuttons (side mouse buttons).
[wine/multimedia.git] / dlls / x11drv / clipboard.c
blob3527b1a5424414911c27d1c2afb21d1d7f5fd6e2
1 /*
2 * X11 clipboard windows driver
4 * Copyright 1994 Martin Ayotte
5 * 1996 Alex Korobka
6 * 1999 Noel Borthwick
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
23 * NOTES:
24 * This file contains the X specific implementation for the windows
25 * Clipboard API.
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)
31 * 2. CLIPBOARD
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
47 * running WINE apps.
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
64 #include "config.h"
65 #include "wine/port.h"
67 #include <string.h>
68 #include <stdarg.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #ifdef HAVE_UNISTD_H
72 # include <unistd.h>
73 #endif
74 #include <fcntl.h>
75 #include <time.h>
77 #include "windef.h"
78 #include "winbase.h"
79 #include "winreg.h"
80 #include "wine/wingdi16.h"
81 #include "x11drv.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
96 /* Selection masks */
97 #define S_NOSELECTION 0
98 #define S_PRIMARY 1
99 #define S_CLIPBOARD 2
101 typedef struct
103 HWND hWndOpen;
104 HWND hWndOwner;
105 HWND hWndViewer;
106 UINT seqno;
107 UINT flags;
108 } CLIPBOARDINFO, *LPCLIPBOARDINFO;
110 typedef struct tagWINE_CLIPDATA {
111 UINT wFormatID;
112 HANDLE16 hData16;
113 HANDLE hData32;
114 UINT drvData;
115 UINT wFlags;
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 {
125 UINT wFormatID;
126 LPCWSTR Name;
127 UINT drvData;
128 UINT wFlags;
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 BOOL usePrimary = FALSE; /* Use primary selection in additon to the clipboard selection */
142 static Atom selectionCacheSrc = XA_PRIMARY; /* The selection source from which the clipboard cache was filled */
144 INT X11DRV_RegisterClipboardFormat(LPCWSTR FormatName);
145 void X11DRV_EmptyClipboard(BOOL keepunowned);
146 void X11DRV_EndClipboardUpdate(void);
147 static HANDLE X11DRV_CLIPBOARD_ImportClipboardData(LPBYTE lpdata, UINT cBytes);
148 static HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(LPBYTE lpdata, UINT cBytes);
149 static HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(LPBYTE lpdata, UINT cBytes);
150 static HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(LPBYTE lpdata, UINT cBytes);
151 static HANDLE X11DRV_CLIPBOARD_ImportXAString(LPBYTE lpdata, UINT cBytes);
152 static HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Window requestor, Atom aTarget,
153 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
154 static HANDLE X11DRV_CLIPBOARD_ExportString(Window requestor, Atom aTarget,
155 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
156 static HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Window requestor, Atom aTarget,
157 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
158 static HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Window requestor, Atom aTarget,
159 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
160 static HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Window requestor, Atom aTarget,
161 Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
162 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(LPCWSTR FormatName, Atom prop);
163 static BOOL X11DRV_CLIPBOARD_ReadSelection(LPWINE_CLIPFORMAT lpData, Window w, Atom prop);
164 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(UINT wFormatID);
165 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData);
166 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void);
167 static int X11DRV_CLIPBOARD_QueryAvailableData(LPCLIPBOARDINFO lpcbinfo);
168 static BOOL X11DRV_CLIPBOARD_ReadClipboardData(UINT wFormat);
169 static BOOL X11DRV_CLIPBOARD_RenderFormat(LPWINE_CLIPDATA lpData);
170 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out);
171 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID);
172 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(LPWINE_CLIPDATA lpData);
173 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedDIB(void);
174 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedBitmap(void);
175 static void X11DRV_HandleSelectionRequest( HWND hWnd, XSelectionRequestEvent *event, BOOL bIsMultiple );
177 /* Clipboard formats
178 * WARNING: This data ordering is dependent on the WINE_CLIPFORMAT structure
179 * declared in clipboard.h
181 static const WCHAR wszCF_TEXT[] = {'W','C','F','_','T','E','X','T',0};
182 static const WCHAR wszCF_BITMAP[] = {'W','C','F','_','B','I','T','M','A','P',0};
183 static const WCHAR wszCF_METAFILEPICT[] = {'W','C','F','_','M','E','T','A','F','I','L','E','P','I','C','T',0};
184 static const WCHAR wszCF_SYLK[] = {'W','C','F','_','S','Y','L','K',0};
185 static const WCHAR wszCF_DIF[] = {'W','C','F','_','D','I','F',0};
186 static const WCHAR wszCF_TIFF[] = {'W','C','F','_','T','I','F','F',0};
187 static const WCHAR wszCF_OEMTEXT[] = {'W','C','F','_','O','E','M','T','E','X','T',0};
188 static const WCHAR wszCF_DIB[] = {'W','C','F','_','D','I','B',0};
189 static const WCHAR wszCF_PALETTE[] = {'W','C','F','_','P','A','L','E','T','T','E',0};
190 static const WCHAR wszCF_PENDATA[] = {'W','C','F','_','P','E','N','D','A','T','A',0};
191 static const WCHAR wszCF_RIFF[] = {'W','C','F','_','R','I','F','F',0};
192 static const WCHAR wszCF_WAVE[] = {'W','C','F','_','W','A','V','E',0};
193 static const WCHAR wszCF_UNICODETEXT[] = {'W','C','F','_','U','N','I','C','O','D','E','T','E','X','T',0};
194 static const WCHAR wszCF_ENHMETAFILE[] = {'W','C','F','_','E','N','H','M','E','T','A','F','I','L','E',0};
195 static const WCHAR wszCF_HDROP[] = {'W','C','F','_','H','D','R','O','P',0};
196 static const WCHAR wszCF_LOCALE[] = {'W','C','F','_','L','O','C','A','L','E',0};
197 static const WCHAR wszCF_DIBV5[] = {'W','C','F','_','D','I','B','V','5',0};
198 static const WCHAR wszCF_OWNERDISPLAY[] = {'W','C','F','_','O','W','N','E','R','D','I','S','P','L','A','Y',0};
199 static const WCHAR wszCF_DSPTEXT[] = {'W','C','F','_','D','S','P','T','E','X','T',0};
200 static const WCHAR wszCF_DSPBITMAP[] = {'W','C','F','_','D','S','P','B','I','T','M','A','P',0};
201 static const WCHAR wszCF_DSPMETAFILEPICT[] = {'W','C','F','_','D','S','P','M','E','T','A','F','I','L','E','P','I','C','T',0};
202 static const WCHAR wszCF_DSPENHMETAFILE[] = {'W','C','F','_','D','S','P','E','N','H','M','E','T','A','F','I','L','E',0};
204 static WINE_CLIPFORMAT ClipFormats[] =
206 { CF_TEXT, wszCF_TEXT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
207 X11DRV_CLIPBOARD_ExportClipboardData, NULL, &ClipFormats[1]},
209 { CF_BITMAP, wszCF_BITMAP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
210 NULL, &ClipFormats[0], &ClipFormats[2]},
212 { CF_METAFILEPICT, wszCF_METAFILEPICT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportMetaFilePict,
213 X11DRV_CLIPBOARD_ExportMetaFilePict, &ClipFormats[1], &ClipFormats[3]},
215 { CF_SYLK, wszCF_SYLK, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
216 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[2], &ClipFormats[4]},
218 { CF_DIF, wszCF_DIF, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
219 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[3], &ClipFormats[5]},
221 { CF_TIFF, wszCF_TIFF, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
222 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[4], &ClipFormats[6]},
224 { CF_OEMTEXT, wszCF_OEMTEXT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
225 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[5], &ClipFormats[7]},
227 { CF_DIB, wszCF_DIB, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportXAPIXMAP,
228 X11DRV_CLIPBOARD_ExportXAPIXMAP, &ClipFormats[6], &ClipFormats[8]},
230 { CF_PALETTE, wszCF_PALETTE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
231 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[7], &ClipFormats[9]},
233 { CF_PENDATA, wszCF_PENDATA, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
234 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[8], &ClipFormats[10]},
236 { CF_RIFF, wszCF_RIFF, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
237 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[9], &ClipFormats[11]},
239 { CF_WAVE, wszCF_WAVE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
240 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[10], &ClipFormats[12]},
242 { CF_UNICODETEXT, wszCF_UNICODETEXT, XA_STRING, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportXAString,
243 X11DRV_CLIPBOARD_ExportString, &ClipFormats[11], &ClipFormats[13]},
245 { CF_ENHMETAFILE, wszCF_ENHMETAFILE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportEnhMetaFile,
246 X11DRV_CLIPBOARD_ExportEnhMetaFile, &ClipFormats[12], &ClipFormats[14]},
248 { CF_HDROP, wszCF_HDROP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
249 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[13], &ClipFormats[15]},
251 { CF_LOCALE, wszCF_LOCALE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
252 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[14], &ClipFormats[16]},
254 { CF_DIBV5, wszCF_DIBV5, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
255 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[15], &ClipFormats[17]},
257 { CF_OWNERDISPLAY, wszCF_OWNERDISPLAY, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
258 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[16], &ClipFormats[18]},
260 { CF_DSPTEXT, wszCF_DSPTEXT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
261 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[17], &ClipFormats[19]},
263 { CF_DSPBITMAP, wszCF_DSPBITMAP, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
264 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[18], &ClipFormats[20]},
266 { CF_DSPMETAFILEPICT, wszCF_DSPMETAFILEPICT, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
267 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[19], &ClipFormats[21]},
269 { CF_DSPENHMETAFILE, wszCF_DSPENHMETAFILE, 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
270 X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[20], NULL}
273 #define GET_ATOM(prop) (((prop) < FIRST_XATOM) ? (Atom)(prop) : X11DRV_Atoms[(prop) - FIRST_XATOM])
275 /* Maps X properties to Windows formats */
276 static const WCHAR wszRichTextFormat[] = {'R','i','c','h',' ','T','e','x','t',' ','F','o','r','m','a','t',0};
277 static const WCHAR wszGIF[] = {'G','I','F',0};
278 static const struct
280 LPCWSTR lpszFormat;
281 UINT prop;
282 } PropertyFormatMap[] =
284 { wszRichTextFormat, XATOM_text_rtf },
285 /* Temporarily disable text/html because Evolution incorrectly pastes strings with extra nulls */
286 /*{ "text/html", "HTML Format" },*/
287 { wszGIF, XATOM_image_gif }
291 /* Maps equivalent X properties. It is assumed that lpszProperty must already
292 be in ClipFormats or PropertyFormatMap. */
293 static const struct
295 UINT drvDataProperty;
296 UINT drvDataAlias;
297 } PropertyAliasMap[] =
299 /* DataProperty, DataAlias */
300 { XATOM_text_rtf, XATOM_text_richtext },
301 { XA_STRING, XATOM_COMPOUND_TEXT },
302 { XA_STRING, XATOM_TEXT },
303 { XATOM_WCF_DIB, XA_PIXMAP },
308 * Cached clipboard data.
310 static LPWINE_CLIPDATA ClipData = NULL;
311 static UINT ClipDataCount = 0;
314 * Clipboard sequence number
316 static UINT wSeqNo = 0;
318 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
320 /**************************************************************************
321 * Internal Clipboard implementation methods
322 **************************************************************************/
324 static Window thread_selection_wnd(void)
326 Window w = x11drv_thread_data()->selection_wnd;
328 if (!w)
330 wine_tsx11_lock();
331 w = XCreateWindow(thread_display(), root_window, 0, 0, 1, 1, 0, screen_depth,
332 InputOutput, CopyFromParent, 0, NULL);
333 wine_tsx11_unlock();
335 if (w)
336 x11drv_thread_data()->selection_wnd = w;
337 else
338 FIXME("Failed to create window. Fetching selection data will fail.\n");
341 return w;
344 /**************************************************************************
345 * X11DRV_InitClipboard
347 void X11DRV_InitClipboard(void)
349 UINT i;
350 HKEY hkey;
352 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Clipboard", &hkey))
354 char buffer[20];
355 DWORD type, count = sizeof(buffer);
356 if(!RegQueryValueExA(hkey, "UsePrimary", 0, &type, buffer, &count))
357 usePrimary = IS_OPTION_TRUE( buffer[0] );
358 RegCloseKey(hkey);
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,
364 GET_ATOM(PropertyFormatMap[i].prop));
368 /**************************************************************************
369 * intern_atoms
371 * Intern atoms for formats that don't have one yet.
373 static void intern_atoms(void)
375 LPWINE_CLIPFORMAT format;
376 int i, count, len;
377 char **names;
378 Atom *atoms;
380 for (format = ClipFormats, count = 0; format; format = format->NextFormat)
381 if (!format->drvData) count++;
382 if (!count) return;
384 names = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*names) );
385 atoms = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*atoms) );
387 for (format = ClipFormats, i = 0; format; format = format->NextFormat) {
388 if (!format->drvData) {
389 len = WideCharToMultiByte(CP_UNIXCP, 0, format->Name, -1, NULL, -1, 0, 0);
390 names[i] = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
391 WideCharToMultiByte(CP_UNIXCP, 0, format->Name, -1, names[i++], len, 0, 0);
395 wine_tsx11_lock();
396 XInternAtoms( thread_display(), names, count, False, atoms );
397 wine_tsx11_unlock();
399 for (format = ClipFormats, i = 0; format; format = format->NextFormat) {
400 if (!format->drvData) {
401 HeapFree(GetProcessHeap(), 0, names[i]);
402 format->drvData = atoms[i++];
406 HeapFree( GetProcessHeap(), 0, names );
407 HeapFree( GetProcessHeap(), 0, atoms );
411 /**************************************************************************
412 * register_format
414 * Register a custom X clipboard format.
416 static WINE_CLIPFORMAT *register_format( LPCWSTR FormatName, Atom prop )
418 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
420 TRACE("%s\n", debugstr_w(FormatName));
422 /* walk format chain to see if it's already registered */
423 while (lpFormat)
425 if (CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, lpFormat->Name, -1, FormatName, -1) == CSTR_EQUAL
426 && (lpFormat->wFlags & CF_FLAG_BUILTINFMT) == 0)
427 return lpFormat;
428 lpFormat = lpFormat->NextFormat;
431 return X11DRV_CLIPBOARD_InsertClipboardFormat(FormatName, prop);
435 /**************************************************************************
436 * X11DRV_CLIPBOARD_LookupFormat
438 LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupFormat(WORD wID)
440 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
442 while(lpFormat)
444 if (lpFormat->wFormatID == wID)
445 break;
447 lpFormat = lpFormat->NextFormat;
449 if (lpFormat && !lpFormat->drvData) intern_atoms();
450 return lpFormat;
454 /**************************************************************************
455 * X11DRV_CLIPBOARD_LookupProperty
457 LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupProperty(UINT drvData)
459 for (;;)
461 LPWINE_CLIPFORMAT lpFormat = ClipFormats;
462 BOOL need_intern = FALSE;
464 while(lpFormat)
466 if (lpFormat->drvData == drvData) return lpFormat;
467 if (!lpFormat->drvData) need_intern = TRUE;
468 lpFormat = lpFormat->NextFormat;
470 if (!need_intern) return NULL;
471 intern_atoms();
472 /* restart the search for the new atoms */
477 /**************************************************************************
478 * X11DRV_CLIPBOARD_LookupAliasProperty
480 LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupAliasProperty(UINT drvDataAlias)
482 unsigned int i;
483 LPWINE_CLIPFORMAT lpFormat = NULL;
485 for (i = 0; i < sizeof(PropertyAliasMap)/sizeof(PropertyAliasMap[0]); i++)
487 if (GET_ATOM(PropertyAliasMap[i].drvDataAlias) == drvDataAlias)
489 lpFormat = X11DRV_CLIPBOARD_LookupProperty(GET_ATOM(PropertyAliasMap[i].drvDataProperty));
490 break;
494 return lpFormat;
498 /**************************************************************************
499 * X11DRV_CLIPBOARD_LookupPropertyAlias
501 UINT X11DRV_CLIPBOARD_LookupPropertyAlias(UINT drvDataProperty)
503 unsigned int i;
504 UINT alias = 0;
506 for (i = 0; i < sizeof(PropertyAliasMap)/sizeof(PropertyAliasMap[0]); i++)
508 if (GET_ATOM(PropertyAliasMap[i].drvDataProperty) == drvDataProperty)
510 alias = GET_ATOM(PropertyAliasMap[i].drvDataAlias);
511 break;
515 return alias;
519 /**************************************************************************
520 * X11DRV_CLIPBOARD_LookupData
522 LPWINE_CLIPDATA X11DRV_CLIPBOARD_LookupData(DWORD wID)
524 LPWINE_CLIPDATA lpData = ClipData;
526 if (lpData)
530 if (lpData->wFormatID == wID)
531 break;
533 lpData = lpData->NextData;
535 while(lpData != ClipData);
537 if (lpData->wFormatID != wID)
538 lpData = NULL;
541 return lpData;
545 /**************************************************************************
546 * InsertClipboardFormat
548 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(LPCWSTR FormatName, Atom prop)
550 LPWINE_CLIPFORMAT lpFormat;
551 LPWINE_CLIPFORMAT lpNewFormat;
553 /* allocate storage for new format entry */
554 lpNewFormat = (LPWINE_CLIPFORMAT) HeapAlloc(GetProcessHeap(),
555 0, sizeof(WINE_CLIPFORMAT));
557 if(lpNewFormat == NULL)
559 WARN("No more memory for a new format!\n");
560 return NULL;
563 if (!(lpNewFormat->Name = HeapAlloc(GetProcessHeap(), 0, (strlenW(FormatName)+1)*sizeof(WCHAR))))
565 WARN("No more memory for the new format name!\n");
566 HeapFree(GetProcessHeap(), 0, lpNewFormat);
567 return NULL;
570 strcpyW((LPWSTR)lpNewFormat->Name, FormatName);
571 lpNewFormat->wFlags = 0;
572 lpNewFormat->wFormatID = GlobalAddAtomW(lpNewFormat->Name);
573 lpNewFormat->drvData = prop;
574 lpNewFormat->lpDrvImportFunc = X11DRV_CLIPBOARD_ImportClipboardData;
575 lpNewFormat->lpDrvExportFunc = X11DRV_CLIPBOARD_ExportClipboardData;
577 /* Link Format */
578 lpFormat = ClipFormats;
580 while(lpFormat->NextFormat) /* Move to last entry */
581 lpFormat = lpFormat->NextFormat;
583 lpNewFormat->NextFormat = NULL;
584 lpFormat->NextFormat = lpNewFormat;
585 lpNewFormat->PrevFormat = lpFormat;
587 TRACE("Registering format(%d): %s drvData %d\n",
588 lpNewFormat->wFormatID, debugstr_w(FormatName), lpNewFormat->drvData);
590 return lpNewFormat;
596 /**************************************************************************
597 * X11DRV_CLIPBOARD_GetClipboardInfo
599 static BOOL X11DRV_CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
601 BOOL bRet = FALSE;
603 SERVER_START_REQ( set_clipboard_info )
605 req->flags = 0;
607 if (wine_server_call_err( req ))
609 ERR("Failed to get clipboard owner.\n");
611 else
613 cbInfo->hWndOpen = reply->old_clipboard;
614 cbInfo->hWndOwner = reply->old_owner;
615 cbInfo->hWndViewer = reply->old_viewer;
616 cbInfo->seqno = reply->seqno;
617 cbInfo->flags = reply->flags;
619 bRet = TRUE;
622 SERVER_END_REQ;
624 return bRet;
628 /**************************************************************************
629 * X11DRV_CLIPBOARD_ReleaseOwnership
631 static BOOL X11DRV_CLIPBOARD_ReleaseOwnership(void)
633 BOOL bRet = FALSE;
635 SERVER_START_REQ( set_clipboard_info )
637 req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
639 if (wine_server_call_err( req ))
641 ERR("Failed to set clipboard.\n");
643 else
645 bRet = TRUE;
648 SERVER_END_REQ;
650 return bRet;
655 /**************************************************************************
656 * X11DRV_CLIPBOARD_InsertClipboardData
658 * Caller *must* have the clipboard open and be the owner.
660 static BOOL X11DRV_CLIPBOARD_InsertClipboardData(UINT wFormat, HANDLE16 hData16, HANDLE hData32, DWORD flags)
662 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormat);
664 TRACE("format=%d lpData=%p hData16=%08x hData32=%08x flags=0x%08lx\n",
665 wFormat, lpData, hData16, (unsigned int)hData32, flags);
667 if (lpData)
669 X11DRV_CLIPBOARD_FreeData(lpData);
671 lpData->hData16 = hData16; /* 0 is legal, see WM_RENDERFORMAT */
672 lpData->hData32 = hData32;
674 else
676 lpData = (LPWINE_CLIPDATA) HeapAlloc(GetProcessHeap(),
677 0, sizeof(WINE_CLIPDATA));
679 lpData->wFormatID = wFormat;
680 lpData->hData16 = hData16; /* 0 is legal, see WM_RENDERFORMAT */
681 lpData->hData32 = hData32;
682 lpData->drvData = 0;
684 if (ClipData)
686 LPWINE_CLIPDATA lpPrevData = ClipData->PrevData;
688 lpData->PrevData = lpPrevData;
689 lpData->NextData = ClipData;
691 lpPrevData->NextData = lpData;
692 ClipData->PrevData = lpData;
694 else
696 lpData->NextData = lpData;
697 lpData->PrevData = lpData;
698 ClipData = lpData;
701 ClipDataCount++;
704 lpData->wFlags = flags;
706 return TRUE;
710 /**************************************************************************
711 * X11DRV_CLIPBOARD_FreeData
713 * Free clipboard data handle.
715 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData)
717 TRACE("%d\n", lpData->wFormatID);
719 if ((lpData->wFormatID >= CF_GDIOBJFIRST &&
720 lpData->wFormatID <= CF_GDIOBJLAST) ||
721 lpData->wFormatID == CF_BITMAP ||
722 lpData->wFormatID == CF_DIB ||
723 lpData->wFormatID == CF_PALETTE)
725 if (lpData->hData32)
726 DeleteObject(lpData->hData32);
728 if (lpData->hData16)
729 DeleteObject(HGDIOBJ_32(lpData->hData16));
731 if ((lpData->wFormatID == CF_DIB) && lpData->drvData)
732 XFreePixmap(gdi_display, lpData->drvData);
734 else if (lpData->wFormatID == CF_METAFILEPICT)
736 if (lpData->hData32)
738 DeleteMetaFile(((METAFILEPICT *)GlobalLock( lpData->hData32 ))->hMF );
739 GlobalFree(lpData->hData32);
741 if (lpData->hData16)
742 /* HMETAFILE16 and HMETAFILE32 are apparently the same thing,
743 and a shallow copy is enough to share a METAFILEPICT
744 structure between 16bit and 32bit clipboards. The MetaFile
745 should of course only be deleted once. */
746 GlobalFree16(lpData->hData16);
749 if (lpData->hData16)
751 METAFILEPICT16* lpMetaPict = (METAFILEPICT16 *) GlobalLock16(lpData->hData16);
753 if (lpMetaPict)
755 DeleteMetaFile16(lpMetaPict->hMF);
756 lpMetaPict->hMF = 0;
759 GlobalFree16(lpData->hData16);
762 else if (lpData->wFormatID == CF_ENHMETAFILE)
764 if (lpData->hData32)
765 DeleteEnhMetaFile(lpData->hData32);
767 else if (lpData->wFormatID < CF_PRIVATEFIRST ||
768 lpData->wFormatID > CF_PRIVATELAST)
770 if (lpData->hData32)
771 GlobalFree(lpData->hData32);
773 if (lpData->hData16)
774 GlobalFree16(lpData->hData16);
777 lpData->hData16 = 0;
778 lpData->hData32 = 0;
779 lpData->drvData = 0;
783 /**************************************************************************
784 * X11DRV_CLIPBOARD_UpdateCache
786 static BOOL X11DRV_CLIPBOARD_UpdateCache(LPCLIPBOARDINFO lpcbinfo)
788 BOOL bret = TRUE;
790 if (!X11DRV_CLIPBOARD_IsSelectionOwner())
792 if (!X11DRV_CLIPBOARD_GetClipboardInfo(lpcbinfo))
794 ERR("Failed to retrieve clipboard information.\n");
795 bret = FALSE;
797 else if (wSeqNo < lpcbinfo->seqno)
799 X11DRV_EmptyClipboard(TRUE);
801 if (X11DRV_CLIPBOARD_QueryAvailableData(lpcbinfo) < 0)
803 ERR("Failed to cache clipboard data owned by another process.\n");
804 bret = FALSE;
806 else
808 X11DRV_EndClipboardUpdate();
811 wSeqNo = lpcbinfo->seqno;
815 return bret;
819 /**************************************************************************
820 * X11DRV_CLIPBOARD_RenderFormat
822 static BOOL X11DRV_CLIPBOARD_RenderFormat(LPWINE_CLIPDATA lpData)
824 BOOL bret = TRUE;
826 TRACE(" 0x%04x hData32(0x%08x) hData16(0x%08x)\n",
827 lpData->wFormatID, (unsigned int)lpData->hData32, lpData->hData16);
829 if (lpData->hData32 || lpData->hData16)
830 return bret; /* Already rendered */
832 if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
833 bret = X11DRV_CLIPBOARD_RenderSynthesizedFormat(lpData);
834 else if (!X11DRV_CLIPBOARD_IsSelectionOwner())
836 if (!X11DRV_CLIPBOARD_ReadClipboardData(lpData->wFormatID))
838 ERR("Failed to cache clipboard data owned by another process. Format=%d\n",
839 lpData->wFormatID);
840 bret = FALSE;
843 else
845 CLIPBOARDINFO cbInfo;
847 if (X11DRV_CLIPBOARD_GetClipboardInfo(&cbInfo) && cbInfo.hWndOwner)
849 /* Send a WM_RENDERFORMAT message to notify the owner to render the
850 * data requested into the clipboard.
852 TRACE("Sending WM_RENDERFORMAT message to hwnd(%p)\n", cbInfo.hWndOwner);
853 SendMessageW(cbInfo.hWndOwner, WM_RENDERFORMAT, (WPARAM)lpData->wFormatID, 0);
855 if (!lpData->hData32 && !lpData->hData16)
856 bret = FALSE;
858 else
860 ERR("hWndClipOwner is lost!\n");
861 bret = FALSE;
865 return bret;
869 /**************************************************************************
870 * CLIPBOARD_ConvertText
871 * Returns number of required/converted characters - not bytes!
873 static INT CLIPBOARD_ConvertText(WORD src_fmt, void const *src, INT src_size,
874 WORD dst_fmt, void *dst, INT dst_size)
876 UINT cp;
878 if(src_fmt == CF_UNICODETEXT)
880 switch(dst_fmt)
882 case CF_TEXT:
883 cp = CP_ACP;
884 break;
885 case CF_OEMTEXT:
886 cp = CP_OEMCP;
887 break;
888 default:
889 return 0;
891 return WideCharToMultiByte(cp, 0, src, src_size, dst, dst_size, NULL, NULL);
894 if(dst_fmt == CF_UNICODETEXT)
896 switch(src_fmt)
898 case CF_TEXT:
899 cp = CP_ACP;
900 break;
901 case CF_OEMTEXT:
902 cp = CP_OEMCP;
903 break;
904 default:
905 return 0;
907 return MultiByteToWideChar(cp, 0, src, src_size, dst, dst_size);
910 if(!dst_size) return src_size;
912 if(dst_size > src_size) dst_size = src_size;
914 if(src_fmt == CF_TEXT )
915 CharToOemBuffA(src, dst, dst_size);
916 else
917 OemToCharBuffA(src, dst, dst_size);
919 return dst_size;
923 /**************************************************************************
924 * X11DRV_CLIPBOARD_RenderSynthesizedFormat
926 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(LPWINE_CLIPDATA lpData)
928 BOOL bret = FALSE;
930 TRACE("\n");
932 if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
934 UINT wFormatID = lpData->wFormatID;
936 if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
937 bret = X11DRV_CLIPBOARD_RenderSynthesizedText(wFormatID);
938 else
940 switch (wFormatID)
942 case CF_DIB:
943 bret = X11DRV_CLIPBOARD_RenderSynthesizedDIB();
944 break;
946 case CF_BITMAP:
947 bret = X11DRV_CLIPBOARD_RenderSynthesizedBitmap();
948 break;
950 case CF_ENHMETAFILE:
951 case CF_METAFILEPICT:
952 FIXME("Synthesizing wFormatID(0x%08x) not implemented\n", wFormatID);
953 break;
955 default:
956 FIXME("Called to synthesize unknown format\n");
957 break;
961 lpData->wFlags &= ~CF_FLAG_SYNTHESIZED;
964 return bret;
968 /**************************************************************************
969 * X11DRV_CLIPBOARD_RenderSynthesizedText
971 * Renders synthesized text
973 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(UINT wFormatID)
975 LPCSTR lpstrS;
976 LPSTR lpstrT;
977 HANDLE hData32;
978 INT src_chars, dst_chars, alloc_size;
979 LPWINE_CLIPDATA lpSource = NULL;
981 TRACE(" %d\n", wFormatID);
983 if ((lpSource = X11DRV_CLIPBOARD_LookupData(wFormatID)) &&
984 lpSource->hData32)
985 return TRUE;
987 /* Look for rendered source or non-synthesized source */
988 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
989 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
991 TRACE("UNICODETEXT -> %d\n", wFormatID);
993 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
994 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
996 TRACE("TEXT -> %d\n", wFormatID);
998 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
999 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
1001 TRACE("OEMTEXT -> %d\n", wFormatID);
1004 if (!lpSource || (lpSource->wFlags & CF_FLAG_SYNTHESIZED &&
1005 !lpSource->hData32))
1006 return FALSE;
1008 /* Ask the clipboard owner to render the source text if necessary */
1009 if (!lpSource->hData32 && !X11DRV_CLIPBOARD_RenderFormat(lpSource))
1010 return FALSE;
1012 if (lpSource->hData32)
1014 lpstrS = (LPSTR)GlobalLock(lpSource->hData32);
1016 else
1018 lpstrS = (LPSTR)GlobalLock16(lpSource->hData16);
1021 if (!lpstrS)
1022 return FALSE;
1024 /* Text always NULL terminated */
1025 if(lpSource->wFormatID == CF_UNICODETEXT)
1026 src_chars = strlenW((LPCWSTR)lpstrS) + 1;
1027 else
1028 src_chars = strlen(lpstrS) + 1;
1030 /* Calculate number of characters in the destination buffer */
1031 dst_chars = CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS,
1032 src_chars, wFormatID, NULL, 0);
1034 if (!dst_chars)
1035 return FALSE;
1037 TRACE("Converting from '%d' to '%d', %i chars\n",
1038 lpSource->wFormatID, wFormatID, src_chars);
1040 /* Convert characters to bytes */
1041 if(wFormatID == CF_UNICODETEXT)
1042 alloc_size = dst_chars * sizeof(WCHAR);
1043 else
1044 alloc_size = dst_chars;
1046 hData32 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE |
1047 GMEM_DDESHARE, alloc_size);
1049 lpstrT = (LPSTR)GlobalLock(hData32);
1051 if (lpstrT)
1053 CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS, src_chars,
1054 wFormatID, lpstrT, dst_chars);
1055 GlobalUnlock(hData32);
1058 /* Unlock source */
1059 if (lpSource->hData32)
1060 GlobalUnlock(lpSource->hData32);
1061 else
1062 GlobalUnlock16(lpSource->hData16);
1064 return X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, 0, hData32, 0);
1068 /**************************************************************************
1069 * X11DRV_CLIPBOARD_RenderSynthesizedDIB
1071 * Renders synthesized DIB
1073 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedDIB()
1075 BOOL bret = FALSE;
1076 LPWINE_CLIPDATA lpSource = NULL;
1078 TRACE("\n");
1080 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) && lpSource->hData32)
1082 bret = TRUE;
1084 /* If we have a bitmap and it's not synthesized or it has been rendered */
1085 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) &&
1086 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
1088 /* Render source if required */
1089 if (lpSource->hData32 || X11DRV_CLIPBOARD_RenderFormat(lpSource))
1091 HDC hdc;
1092 HGLOBAL hData32;
1094 hdc = GetDC(NULL);
1095 hData32 = X11DRV_DIB_CreateDIBFromBitmap(hdc, lpSource->hData32);
1096 ReleaseDC(NULL, hdc);
1098 if (hData32)
1100 X11DRV_CLIPBOARD_InsertClipboardData(CF_DIB, 0, hData32, 0);
1101 bret = TRUE;
1106 return bret;
1110 /**************************************************************************
1111 * X11DRV_CLIPBOARD_RenderSynthesizedBitmap
1113 * Renders synthesized bitmap
1115 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedBitmap()
1117 BOOL bret = FALSE;
1118 LPWINE_CLIPDATA lpSource = NULL;
1120 TRACE("\n");
1122 if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) && lpSource->hData32)
1124 bret = TRUE;
1126 /* If we have a dib and it's not synthesized or it has been rendered */
1127 else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) &&
1128 (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
1130 /* Render source if required */
1131 if (lpSource->hData32 || X11DRV_CLIPBOARD_RenderFormat(lpSource))
1133 HDC hdc;
1134 HBITMAP hData32;
1135 unsigned int offset;
1136 LPBITMAPINFOHEADER lpbmih;
1138 hdc = GetDC(NULL);
1139 lpbmih = (LPBITMAPINFOHEADER) GlobalLock(lpSource->hData32);
1141 offset = sizeof(BITMAPINFOHEADER)
1142 + ((lpbmih->biBitCount <= 8) ? (sizeof(RGBQUAD) *
1143 (1 << lpbmih->biBitCount)) : 0);
1145 hData32 = CreateDIBitmap(hdc, lpbmih, CBM_INIT, (LPBYTE)lpbmih +
1146 offset, (LPBITMAPINFO) lpbmih, DIB_RGB_COLORS);
1148 GlobalUnlock(lpSource->hData32);
1149 ReleaseDC(NULL, hdc);
1151 if (hData32)
1153 X11DRV_CLIPBOARD_InsertClipboardData(CF_BITMAP, 0, hData32, 0);
1154 bret = TRUE;
1159 return bret;
1163 /**************************************************************************
1164 * X11DRV_CLIPBOARD_ImportXAString
1166 * Import XA_STRING, converting the string to CF_UNICODE.
1168 HANDLE X11DRV_CLIPBOARD_ImportXAString(LPBYTE lpdata, UINT cBytes)
1170 LPSTR lpstr;
1171 UINT i, inlcount = 0;
1172 HANDLE hUnicodeText = 0;
1174 for (i = 0; i <= cBytes; i++)
1176 if (lpdata[i] == '\n')
1177 inlcount++;
1180 if ((lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cBytes + inlcount + 1)))
1182 UINT count;
1184 for (i = 0, inlcount = 0; i <= cBytes; i++)
1186 if (lpdata[i] == '\n')
1187 lpstr[inlcount++] = '\r';
1189 lpstr[inlcount++] = lpdata[i];
1192 count = MultiByteToWideChar(CP_UNIXCP, 0, lpstr, -1, NULL, 0);
1193 hUnicodeText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, count * sizeof(WCHAR));
1195 if(hUnicodeText)
1197 WCHAR *textW = GlobalLock(hUnicodeText);
1198 MultiByteToWideChar(CP_UNIXCP, 0, lpstr, -1, textW, count);
1199 GlobalUnlock(hUnicodeText);
1202 HeapFree(GetProcessHeap(), 0, lpstr);
1205 return hUnicodeText;
1209 /**************************************************************************
1210 * X11DRV_CLIPBOARD_ImportXAPIXMAP
1212 * Import XA_PIXMAP, converting the image to CF_DIB.
1214 HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(LPBYTE lpdata, UINT cBytes)
1216 HANDLE hTargetImage = 0; /* Handle to store the converted DIB */
1217 Pixmap *pPixmap = (Pixmap *) lpdata;
1218 HWND hwnd = GetOpenClipboardWindow();
1219 HDC hdc = GetDC(hwnd);
1221 hTargetImage = X11DRV_DIB_CreateDIBFromPixmap(*pPixmap, hdc, TRUE);
1223 ReleaseDC(hwnd, hdc);
1225 return hTargetImage;
1229 /**************************************************************************
1230 * X11DRV_CLIPBOARD_ImportMetaFilePict
1232 * Import MetaFilePict.
1234 HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(LPBYTE lpdata, UINT cBytes)
1236 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, (HANDLE)lpdata, (LPDWORD)&cBytes, FALSE);
1240 /**************************************************************************
1241 * X11DRV_ImportEnhMetaFile
1243 * Import EnhMetaFile.
1245 HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(LPBYTE lpdata, UINT cBytes)
1247 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, (HANDLE)lpdata, (LPDWORD)&cBytes, FALSE);
1251 /**************************************************************************
1252 * X11DRV_ImportClipbordaData
1254 * Generic import clipboard data routine.
1256 HANDLE X11DRV_CLIPBOARD_ImportClipboardData(LPBYTE lpdata, UINT cBytes)
1258 LPVOID lpClipData;
1259 HANDLE hClipData = 0;
1261 if (cBytes)
1263 /* Turn on the DDESHARE flag to enable shared 32 bit memory */
1264 hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cBytes);
1265 if (hClipData == 0) return NULL;
1267 if ((lpClipData = GlobalLock(hClipData)))
1269 memcpy(lpClipData, lpdata, cBytes);
1270 GlobalUnlock(hClipData);
1272 else {
1273 GlobalFree(hClipData);
1274 hClipData = 0;
1278 return hClipData;
1282 /**************************************************************************
1283 X11DRV_CLIPBOARD_ExportClipboardData
1285 * Generic export clipboard data routine.
1287 HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Window requestor, Atom aTarget,
1288 Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1290 LPVOID lpClipData;
1291 UINT cBytes = 0;
1292 HANDLE hClipData = 0;
1294 *lpBytes = 0; /* Assume failure */
1296 if (!X11DRV_CLIPBOARD_RenderFormat(lpData))
1297 ERR("Failed to export %d format\n", lpData->wFormatID);
1298 else
1300 cBytes = GlobalSize(lpData->hData32);
1302 hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cBytes);
1303 if (hClipData == 0) return NULL;
1305 if ((lpClipData = GlobalLock(hClipData)))
1307 LPVOID lpdata = GlobalLock(lpData->hData32);
1309 memcpy(lpClipData, lpdata, cBytes);
1310 *lpBytes = cBytes;
1312 GlobalUnlock(lpData->hData32);
1313 GlobalUnlock(hClipData);
1314 } else {
1315 GlobalFree(hClipData);
1316 hClipData = 0;
1320 return hClipData;
1324 /**************************************************************************
1325 * X11DRV_CLIPBOARD_ExportXAString
1327 * Export CF_UNICODE converting the string to XA_STRING.
1328 * Helper function for X11DRV_CLIPBOARD_ExportString.
1330 HANDLE X11DRV_CLIPBOARD_ExportXAString(LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1332 UINT i, j;
1333 UINT size;
1334 LPWSTR uni_text;
1335 LPSTR text, lpstr = NULL;
1337 *lpBytes = 0; /* Assume return has zero bytes */
1339 uni_text = GlobalLock(lpData->hData32);
1341 size = WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, NULL, 0, NULL, NULL);
1343 text = HeapAlloc(GetProcessHeap(), 0, size);
1344 if (!text) goto done;
1345 WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, text, size, NULL, NULL);
1347 /* remove carriage returns */
1349 lpstr = (char*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size-- );
1350 if(lpstr == NULL) goto done;
1352 for(i = 0,j = 0; i < size && text[i]; i++ )
1354 if( text[i] == '\r' &&
1355 (text[i+1] == '\n' || text[i+1] == '\0') ) continue;
1356 lpstr[j++] = text[i];
1358 lpstr[j]='\0';
1360 *lpBytes = j; /* Number of bytes in string */
1362 done:
1363 HeapFree(GetProcessHeap(), 0, text);
1364 GlobalUnlock(lpData->hData32);
1366 return lpstr;
1370 /**************************************************************************
1371 * X11DRV_CLIPBOARD_ExportCompoundText
1373 * Export CF_UNICODE to COMPOUND_TEXT or TEXT
1374 * Helper function for X11DRV_CLIPBOARD_ExportString.
1376 HANDLE X11DRV_CLIPBOARD_ExportCompoundText(Window requestor, Atom aTarget, Atom rprop,
1377 LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1379 Display *display = thread_display();
1380 char* lpstr = 0;
1381 XTextProperty prop;
1382 XICCEncodingStyle style;
1384 lpstr = (char*) X11DRV_CLIPBOARD_ExportXAString(lpData, lpBytes);
1386 if (lpstr)
1388 if (aTarget == x11drv_atom(COMPOUND_TEXT))
1389 style = XCompoundTextStyle;
1390 else
1391 style = XStdICCTextStyle;
1393 /* Update the X property */
1394 wine_tsx11_lock();
1395 if (XmbTextListToTextProperty(display, &lpstr, 1, style, &prop) == Success)
1397 XSetTextProperty(display, requestor, &prop, rprop);
1398 XFree(prop.value);
1400 wine_tsx11_unlock();
1402 HeapFree( GetProcessHeap(), 0, lpstr );
1405 return 0;
1408 /**************************************************************************
1409 * X11DRV_CLIPBOARD_ExportString
1411 * Export CF_UNICODE string
1413 HANDLE X11DRV_CLIPBOARD_ExportString(Window requestor, Atom aTarget, Atom rprop,
1414 LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1416 if (X11DRV_CLIPBOARD_RenderFormat(lpData))
1418 if (aTarget == XA_STRING)
1419 return X11DRV_CLIPBOARD_ExportXAString(lpData, lpBytes);
1420 else if (aTarget == x11drv_atom(COMPOUND_TEXT) || aTarget == x11drv_atom(TEXT))
1421 return X11DRV_CLIPBOARD_ExportCompoundText(requestor, aTarget,
1422 rprop, lpData, lpBytes);
1423 else
1424 ERR("Unknown target %ld to %d format\n", aTarget, lpData->wFormatID);
1426 else
1427 ERR("Failed to render %d format\n", lpData->wFormatID);
1429 return 0;
1433 /**************************************************************************
1434 * X11DRV_CLIPBOARD_ExportXAPIXMAP
1436 * Export CF_DIB to XA_PIXMAP.
1438 HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Window requestor, Atom aTarget, Atom rprop,
1439 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1441 HDC hdc;
1442 HANDLE hData;
1443 unsigned char* lpData;
1445 if (!X11DRV_CLIPBOARD_RenderFormat(lpdata))
1447 ERR("Failed to export %d format\n", lpdata->wFormatID);
1448 return 0;
1451 if (!lpdata->drvData) /* If not already rendered */
1453 /* For convert from packed DIB to Pixmap */
1454 hdc = GetDC(0);
1455 lpdata->drvData = (UINT) X11DRV_DIB_CreatePixmapFromDIB(lpdata->hData32, hdc);
1456 ReleaseDC(0, hdc);
1459 *lpBytes = sizeof(Pixmap); /* pixmap is a 32bit value */
1461 /* Wrap pixmap so we can return a handle */
1462 hData = GlobalAlloc(0, *lpBytes);
1463 lpData = GlobalLock(hData);
1464 memcpy(lpData, &lpdata->drvData, *lpBytes);
1465 GlobalUnlock(hData);
1467 return (HANDLE) hData;
1471 /**************************************************************************
1472 * X11DRV_CLIPBOARD_ExportMetaFilePict
1474 * Export MetaFilePict.
1476 HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Window requestor, Atom aTarget, Atom rprop,
1477 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1479 if (!X11DRV_CLIPBOARD_RenderFormat(lpdata))
1481 ERR("Failed to export %d format\n", lpdata->wFormatID);
1482 return 0;
1485 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, (HANDLE)lpdata->hData32,
1486 lpBytes, TRUE);
1490 /**************************************************************************
1491 * X11DRV_CLIPBOARD_ExportEnhMetaFile
1493 * Export EnhMetaFile.
1495 HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Window requestor, Atom aTarget, Atom rprop,
1496 LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1498 if (!X11DRV_CLIPBOARD_RenderFormat(lpdata))
1500 ERR("Failed to export %d format\n", lpdata->wFormatID);
1501 return 0;
1504 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, (HANDLE)lpdata->hData32,
1505 lpBytes, TRUE);
1509 /**************************************************************************
1510 * X11DRV_CLIPBOARD_QueryTargets
1512 static BOOL X11DRV_CLIPBOARD_QueryTargets(Display *display, Window w, Atom selection,
1513 Atom target, XEvent *xe)
1515 INT i;
1516 Bool res;
1518 wine_tsx11_lock();
1519 XConvertSelection(display, selection, target,
1520 x11drv_atom(SELECTION_DATA), w, CurrentTime);
1521 wine_tsx11_unlock();
1524 * Wait until SelectionNotify is received
1526 for (i = 0; i < SELECTION_RETRIES; i++)
1528 wine_tsx11_lock();
1529 res = XCheckTypedWindowEvent(display, w, SelectionNotify, xe);
1530 wine_tsx11_unlock();
1531 if (res && xe->xselection.selection == selection) break;
1533 usleep(SELECTION_WAIT);
1536 /* Verify that the selection returned a valid TARGETS property */
1537 if ((xe->xselection.target != target) || (xe->xselection.property == None))
1539 /* Selection owner failed to respond or we missed the SelectionNotify */
1540 WARN("Failed to retrieve TARGETS for selection %ld.\n", selection);
1541 return FALSE;
1544 return TRUE;
1548 /**************************************************************************
1549 * X11DRV_CLIPBOARD_InsertSelectionProperties
1551 * Mark property available for future retrieval.
1553 static VOID X11DRV_CLIPBOARD_InsertSelectionProperties(Display *display, Atom* properties, UINT count)
1555 UINT i, nb_atoms = 0;
1556 Atom *atoms = NULL;
1558 /* Cache these formats in the clipboard cache */
1559 for (i = 0; i < count; i++)
1561 LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupProperty(properties[i]);
1563 if (!lpFormat)
1564 lpFormat = X11DRV_CLIPBOARD_LookupAliasProperty(properties[i]);
1566 if (!lpFormat)
1568 /* add it to the list of atoms that we don't know about yet */
1569 if (!atoms) atoms = HeapAlloc( GetProcessHeap(), 0,
1570 (count - i) * sizeof(*atoms) );
1571 if (atoms) atoms[nb_atoms++] = properties[i];
1573 else
1575 TRACE("Atom#%d Property(%d): --> FormatID(%d) %s\n",
1576 i, lpFormat->drvData, lpFormat->wFormatID, debugstr_w(lpFormat->Name));
1577 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, 0);
1581 /* query all unknown atoms in one go */
1582 if (atoms)
1584 char **names = HeapAlloc( GetProcessHeap(), 0, nb_atoms * sizeof(*names) );
1585 if (names)
1587 wine_tsx11_lock();
1588 XGetAtomNames( display, atoms, nb_atoms, names );
1589 wine_tsx11_unlock();
1590 for (i = 0; i < nb_atoms; i++)
1592 WINE_CLIPFORMAT *lpFormat;
1593 LPWSTR wname;
1594 int len = MultiByteToWideChar(CP_UNIXCP, 0, names[i], -1, NULL, 0);
1595 wname = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1596 MultiByteToWideChar(CP_UNIXCP, 0, names[i], -1, wname, len);
1598 lpFormat = register_format( wname, atoms[i] );
1599 HeapFree(GetProcessHeap(), 0, wname);
1600 if (!lpFormat)
1602 ERR("Failed to register %s property. Type will not be cached.\n", names[i]);
1603 continue;
1605 TRACE("Atom#%d Property(%d): --> FormatID(%d) %s\n",
1606 i, lpFormat->drvData, lpFormat->wFormatID, debugstr_w(lpFormat->Name));
1607 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, 0);
1609 wine_tsx11_lock();
1610 for (i = 0; i < nb_atoms; i++) XFree( names[i] );
1611 wine_tsx11_unlock();
1612 HeapFree( GetProcessHeap(), 0, names );
1614 HeapFree( GetProcessHeap(), 0, atoms );
1619 /**************************************************************************
1620 * X11DRV_CLIPBOARD_QueryAvailableData
1622 * Caches the list of data formats available from the current selection.
1623 * This queries the selection owner for the TARGETS property and saves all
1624 * reported property types.
1626 static int X11DRV_CLIPBOARD_QueryAvailableData(LPCLIPBOARDINFO lpcbinfo)
1628 Display *display = thread_display();
1629 XEvent xe;
1630 Atom atype=AnyPropertyType;
1631 int aformat;
1632 unsigned long remain;
1633 Atom* targetList=NULL;
1634 Window w;
1635 unsigned long cSelectionTargets = 0;
1637 if (selectionAcquired & (S_PRIMARY | S_CLIPBOARD))
1639 ERR("Received request to cache selection but process is owner=(%08x)\n",
1640 (unsigned) selectionWindow);
1641 return -1; /* Prevent self request */
1644 w = thread_selection_wnd();
1645 if (!w)
1647 ERR("No window available to retrieve selection!\n");
1648 return -1;
1652 * Query the selection owner for the TARGETS property
1654 wine_tsx11_lock();
1655 if ((usePrimary && XGetSelectionOwner(display,XA_PRIMARY)) ||
1656 XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
1658 wine_tsx11_unlock();
1659 if (usePrimary && (X11DRV_CLIPBOARD_QueryTargets(display, w, XA_PRIMARY, x11drv_atom(TARGETS), &xe)))
1660 selectionCacheSrc = XA_PRIMARY;
1661 else if (X11DRV_CLIPBOARD_QueryTargets(display, w, x11drv_atom(CLIPBOARD), x11drv_atom(TARGETS), &xe))
1662 selectionCacheSrc = x11drv_atom(CLIPBOARD);
1663 else
1665 Atom xstr = XA_PRIMARY;
1667 /* Selection Owner doesn't understand TARGETS, try retrieving XA_STRING */
1668 if (X11DRV_CLIPBOARD_QueryTargets(display, w, XA_PRIMARY, XA_STRING, &xe))
1670 X11DRV_CLIPBOARD_InsertSelectionProperties(display, &xstr, 1);
1671 selectionCacheSrc = XA_PRIMARY;
1672 return 1;
1674 else if (X11DRV_CLIPBOARD_QueryTargets(display, w, x11drv_atom(CLIPBOARD), XA_STRING, &xe))
1676 X11DRV_CLIPBOARD_InsertSelectionProperties(display, &xstr, 1);
1677 selectionCacheSrc = x11drv_atom(CLIPBOARD);
1678 return 1;
1680 else
1682 WARN("Failed to query selection owner for available data.\n");
1683 return -1;
1687 else /* No selection owner so report 0 targets available */
1689 wine_tsx11_unlock();
1690 return 0;
1693 /* Read the TARGETS property contents */
1694 wine_tsx11_lock();
1695 if(XGetWindowProperty(display, xe.xselection.requestor, xe.xselection.property,
1696 0, 0x3FFF, True, AnyPropertyType/*XA_ATOM*/, &atype, &aformat, &cSelectionTargets,
1697 &remain, (unsigned char**)&targetList) != Success)
1699 wine_tsx11_unlock();
1700 WARN("Failed to read TARGETS property\n");
1702 else
1704 wine_tsx11_unlock();
1705 TRACE("Type %lx,Format %d,nItems %ld, Remain %ld\n",
1706 atype, aformat, cSelectionTargets, remain);
1708 * The TARGETS property should have returned us a list of atoms
1709 * corresponding to each selection target format supported.
1711 if ((atype == XA_ATOM || atype == x11drv_atom(TARGETS)) && aformat == 32)
1712 X11DRV_CLIPBOARD_InsertSelectionProperties(display, targetList, cSelectionTargets);
1714 /* Free the list of targets */
1715 wine_tsx11_lock();
1716 XFree(targetList);
1717 wine_tsx11_unlock();
1720 return cSelectionTargets;
1724 /**************************************************************************
1725 * X11DRV_CLIPBOARD_ReadClipboardData
1727 * This method is invoked only when we DO NOT own the X selection
1729 * We always get the data from the selection client each time,
1730 * since we have no way of determining if the data in our cache is stale.
1732 static BOOL X11DRV_CLIPBOARD_ReadClipboardData(UINT wFormat)
1734 Display *display = thread_display();
1735 BOOL bRet = FALSE;
1736 Bool res;
1737 LPWINE_CLIPFORMAT lpFormat;
1739 TRACE("%d\n", wFormat);
1741 if (!selectionAcquired)
1743 Window w = thread_selection_wnd();
1744 if(!w)
1746 ERR("No window available to read selection data!\n");
1747 return FALSE;
1750 lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
1752 if (lpFormat && lpFormat->drvData)
1754 DWORD i;
1755 UINT alias;
1756 XEvent xe;
1758 TRACE("Requesting %s selection (%d) from win(%08x)\n",
1759 debugstr_w(lpFormat->Name), lpFormat->drvData, (UINT)selectionCacheSrc);
1761 wine_tsx11_lock();
1762 XConvertSelection(display, selectionCacheSrc, lpFormat->drvData,
1763 x11drv_atom(SELECTION_DATA), w, CurrentTime);
1764 wine_tsx11_unlock();
1766 /* wait until SelectionNotify is received */
1767 for (i = 0; i < SELECTION_RETRIES; i++)
1769 wine_tsx11_lock();
1770 res = XCheckTypedWindowEvent(display, w, SelectionNotify, &xe);
1771 wine_tsx11_unlock();
1772 if (res && xe.xselection.selection == selectionCacheSrc) break;
1774 usleep(SELECTION_WAIT);
1777 /* If the property wasn't available check for aliases */
1778 if (xe.xselection.property == None &&
1779 (alias = X11DRV_CLIPBOARD_LookupPropertyAlias(lpFormat->drvData)))
1781 wine_tsx11_lock();
1782 XConvertSelection(display, selectionCacheSrc, alias,
1783 x11drv_atom(SELECTION_DATA), w, CurrentTime);
1784 wine_tsx11_unlock();
1786 /* wait until SelectionNotify is received */
1787 for (i = 0; i < SELECTION_RETRIES; i++)
1789 wine_tsx11_lock();
1790 res = XCheckTypedWindowEvent(display, w, SelectionNotify, &xe);
1791 wine_tsx11_unlock();
1792 if (res && xe.xselection.selection == selectionCacheSrc) break;
1794 usleep(SELECTION_WAIT);
1798 /* Verify that the selection returned a valid TARGETS property */
1799 if (xe.xselection.property != None)
1802 * Read the contents of the X selection property
1803 * into WINE's clipboard cache converting the
1804 * selection to be compatible if possible.
1806 bRet = X11DRV_CLIPBOARD_ReadSelection(lpFormat, xe.xselection.requestor,
1807 xe.xselection.property);
1811 else
1813 ERR("Received request to cache selection data but process is owner\n");
1816 TRACE("Returning %d\n", bRet);
1818 return bRet;
1822 /**************************************************************************
1823 * X11DRV_CLIPBOARD_ReadSelection
1824 * Reads the contents of the X selection property into the WINE clipboard cache
1825 * converting the selection into a format compatible with the windows clipboard
1826 * if possible.
1827 * This method is invoked only to read the contents of a the selection owned
1828 * by an external application. i.e. when we do not own the X selection.
1830 static BOOL X11DRV_CLIPBOARD_ReadSelection(LPWINE_CLIPFORMAT lpData, Window w, Atom prop)
1832 Display *display = thread_display();
1833 Atom atype=AnyPropertyType;
1834 int aformat;
1835 unsigned long total,nitems,remain,val_cnt;
1836 long reqlen, bwc;
1837 unsigned char* val;
1838 unsigned char* buffer;
1839 BOOL bRet = FALSE;
1841 if(prop == None)
1842 return bRet;
1844 TRACE("Reading X selection type %s\n", debugstr_w(lpData->Name));
1847 * First request a zero length in order to figure out the request size.
1849 wine_tsx11_lock();
1850 if(XGetWindowProperty(display,w,prop,0,0,False, AnyPropertyType,
1851 &atype, &aformat, &nitems, &remain, &buffer) != Success)
1853 wine_tsx11_unlock();
1854 WARN("Failed to get property size\n");
1855 return bRet;
1858 /* Free zero length return data if any */
1859 if (buffer)
1861 XFree(buffer);
1862 buffer = NULL;
1865 bwc = aformat/8;
1866 reqlen = remain * bwc;
1868 TRACE("Retrieving %ld bytes\n", reqlen);
1870 val = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, reqlen);
1872 /* Read property in 4K blocks */
1873 for (total = 0, val_cnt = 0; remain;)
1875 if (XGetWindowProperty(display, w, prop, (total / 4), 4096, False,
1876 AnyPropertyType, &atype, &aformat, &nitems, &remain, &buffer) != Success)
1878 wine_tsx11_unlock();
1879 WARN("Failed to read property\n");
1880 HeapFree(GetProcessHeap(), 0, val);
1881 return bRet;
1884 bwc = aformat/8;
1885 memcpy(&val[val_cnt], buffer, nitems * bwc);
1886 val_cnt += nitems * bwc;
1887 total += nitems*bwc;
1888 XFree(buffer);
1890 wine_tsx11_unlock();
1892 bRet = X11DRV_CLIPBOARD_InsertClipboardData(lpData->wFormatID, 0, lpData->lpDrvImportFunc(val, total), 0);
1894 /* Delete the property on the window now that we are done
1895 * This will send a PropertyNotify event to the selection owner. */
1896 wine_tsx11_lock();
1897 XDeleteProperty(display,w,prop);
1898 wine_tsx11_unlock();
1900 /* Free the retrieved property data */
1901 HeapFree(GetProcessHeap(),0,val);
1903 return bRet;
1907 /**************************************************************************
1908 * CLIPBOARD_SerializeMetafile
1910 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out)
1912 HANDLE h = 0;
1914 TRACE(" wFormat=%d hdata=%08x out=%d\n", wformat, (unsigned int) hdata, out);
1916 if (out) /* Serialize out, caller should free memory */
1918 *lpcbytes = 0; /* Assume failure */
1920 if (wformat == CF_METAFILEPICT)
1922 LPMETAFILEPICT lpmfp = (LPMETAFILEPICT) GlobalLock(hdata);
1923 unsigned int size = GetMetaFileBitsEx(lpmfp->hMF, 0, NULL);
1925 h = GlobalAlloc(0, size + sizeof(METAFILEPICT));
1926 if (h)
1928 char *pdata = GlobalLock(h);
1930 memcpy(pdata, lpmfp, sizeof(METAFILEPICT));
1931 GetMetaFileBitsEx(lpmfp->hMF, size, pdata + sizeof(METAFILEPICT));
1933 *lpcbytes = size + sizeof(METAFILEPICT);
1935 GlobalUnlock(h);
1938 GlobalUnlock(hdata);
1940 else if (wformat == CF_ENHMETAFILE)
1942 int size = GetEnhMetaFileBits(hdata, 0, NULL);
1944 h = GlobalAlloc(0, size);
1945 if (h)
1947 LPVOID pdata = GlobalLock(h);
1949 GetEnhMetaFileBits(hdata, size, pdata);
1950 *lpcbytes = size;
1952 GlobalUnlock(h);
1956 else
1958 if (wformat == CF_METAFILEPICT)
1960 h = GlobalAlloc(0, sizeof(METAFILEPICT));
1961 if (h)
1963 unsigned int wiresize, size;
1964 LPMETAFILEPICT lpmfp = (LPMETAFILEPICT) GlobalLock(h);
1966 memcpy(lpmfp, (LPVOID)hdata, sizeof(METAFILEPICT));
1967 wiresize = *lpcbytes - sizeof(METAFILEPICT);
1968 lpmfp->hMF = SetMetaFileBitsEx(wiresize,
1969 ((char *)hdata) + sizeof(METAFILEPICT));
1970 size = GetMetaFileBitsEx(lpmfp->hMF, 0, NULL);
1971 GlobalUnlock(h);
1974 else if (wformat == CF_ENHMETAFILE)
1976 h = SetEnhMetaFileBits(*lpcbytes, (LPVOID)hdata);
1980 return h;
1984 /**************************************************************************
1985 * X11DRV_CLIPBOARD_ReleaseSelection
1987 * Release XA_CLIPBOARD and XA_PRIMARY in response to a SelectionClear event.
1989 void X11DRV_CLIPBOARD_ReleaseSelection(Atom selType, Window w, HWND hwnd, Time time)
1991 Display *display = thread_display();
1993 /* w is the window that lost the selection
1995 TRACE("event->window = %08x (selectionWindow = %08x) selectionAcquired=0x%08x\n",
1996 (unsigned)w, (unsigned)selectionWindow, (unsigned)selectionAcquired);
1998 if (selectionAcquired && (w == selectionWindow))
2000 CLIPBOARDINFO cbinfo;
2002 /* completely give up the selection */
2003 TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
2005 X11DRV_CLIPBOARD_GetClipboardInfo(&cbinfo);
2007 if (cbinfo.flags & CB_PROCESS)
2009 /* Since we're still the owner, this wasn't initiated by
2010 another Wine process */
2011 if (OpenClipboard(hwnd))
2013 /* Destroy private objects */
2014 SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
2016 /* Give up ownership of the windows clipboard */
2017 X11DRV_CLIPBOARD_ReleaseOwnership();
2018 CloseClipboard();
2022 if ((selType == x11drv_atom(CLIPBOARD)) && (selectionAcquired & S_PRIMARY))
2024 TRACE("Lost clipboard. Check if we need to release PRIMARY\n");
2026 wine_tsx11_lock();
2027 if (selectionWindow == XGetSelectionOwner(display, XA_PRIMARY))
2029 TRACE("We still own PRIMARY. Releasing PRIMARY.\n");
2030 XSetSelectionOwner(display, XA_PRIMARY, None, time);
2032 else
2033 TRACE("We no longer own PRIMARY\n");
2034 wine_tsx11_unlock();
2036 else if ((selType == XA_PRIMARY) && (selectionAcquired & S_CLIPBOARD))
2038 TRACE("Lost PRIMARY. Check if we need to release CLIPBOARD\n");
2040 wine_tsx11_lock();
2041 if (selectionWindow == XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
2043 TRACE("We still own CLIPBOARD. Releasing CLIPBOARD.\n");
2044 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), None, time);
2046 else
2047 TRACE("We no longer own CLIPBOARD\n");
2048 wine_tsx11_unlock();
2051 selectionWindow = None;
2053 X11DRV_EmptyClipboard(FALSE);
2055 /* Reset the selection flags now that we are done */
2056 selectionAcquired = S_NOSELECTION;
2061 /**************************************************************************
2062 * IsSelectionOwner (X11DRV.@)
2064 * Returns: TRUE if the selection is owned by this process, FALSE otherwise
2066 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void)
2068 return selectionAcquired;
2072 /**************************************************************************
2073 * X11DRV Clipboard Exports
2074 **************************************************************************/
2077 /**************************************************************************
2078 * RegisterClipboardFormat (X11DRV.@)
2080 * Registers a custom X clipboard format
2081 * Returns: Format id or 0 on failure
2083 INT X11DRV_RegisterClipboardFormat(LPCWSTR FormatName)
2085 LPWINE_CLIPFORMAT lpFormat;
2087 if (FormatName == NULL) return 0;
2088 if (!(lpFormat = register_format( FormatName, 0 ))) return 0;
2089 return lpFormat->wFormatID;
2093 /**************************************************************************
2094 * X11DRV_GetClipboardFormatName
2096 INT X11DRV_GetClipboardFormatName(UINT wFormat, LPWSTR retStr, INT maxlen)
2098 LPWINE_CLIPFORMAT lpFormat;
2100 TRACE("(%04X, %p, %d) !\n", wFormat, retStr, maxlen);
2102 if (wFormat < 0xc000)
2104 SetLastError(ERROR_INVALID_PARAMETER);
2105 return 0;
2108 lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
2110 if (!lpFormat || (lpFormat->wFlags & CF_FLAG_BUILTINFMT))
2112 TRACE("Unknown format 0x%08x!\n", wFormat);
2113 SetLastError(ERROR_INVALID_HANDLE);
2114 return 0;
2117 lstrcpynW(retStr, lpFormat->Name, maxlen);
2119 return strlenW(retStr);
2123 /**************************************************************************
2124 * AcquireClipboard (X11DRV.@)
2126 void X11DRV_AcquireClipboard(HWND hWndClipWindow)
2128 Display *display = thread_display();
2131 * Acquire X selection if we don't already own it.
2132 * Note that we only acquire the selection if it hasn't been already
2133 * acquired by us, and ignore the fact that another X window may be
2134 * asserting ownership. The reason for this is we need *any* top level
2135 * X window to hold selection ownership. The actual clipboard data requests
2136 * are made via GetClipboardData from EVENT_SelectionRequest and this
2137 * ensures that the real HWND owner services the request.
2138 * If the owning X window gets destroyed the selection ownership is
2139 * re-cycled to another top level X window in X11DRV_CLIPBOARD_ResetOwner.
2142 if (!(selectionAcquired == (S_PRIMARY | S_CLIPBOARD)))
2144 Window owner;
2146 if (!hWndClipWindow)
2147 hWndClipWindow = GetActiveWindow();
2149 hWndClipWindow = GetAncestor(hWndClipWindow, GA_ROOT);
2151 if (GetCurrentThreadId() != GetWindowThreadProcessId(hWndClipWindow, NULL))
2153 TRACE("Thread %lx is acquiring selection with thread %lx's window %p\n",
2154 GetCurrentThreadId(),
2155 GetWindowThreadProcessId(hWndClipWindow, NULL),
2156 hWndClipWindow);
2157 if (!SendMessageW(hWndClipWindow, WM_X11DRV_ACQUIRE_SELECTION, 0, 0))
2158 ERR("Failed to acquire selection\n");
2159 return;
2162 owner = X11DRV_get_whole_window(hWndClipWindow);
2164 wine_tsx11_lock();
2165 /* Grab PRIMARY selection if not owned */
2166 if (usePrimary && !(selectionAcquired & S_PRIMARY))
2167 XSetSelectionOwner(display, XA_PRIMARY, owner, CurrentTime);
2169 /* Grab CLIPBOARD selection if not owned */
2170 if (!(selectionAcquired & S_CLIPBOARD))
2171 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), owner, CurrentTime);
2173 if (usePrimary && XGetSelectionOwner(display,XA_PRIMARY) == owner)
2174 selectionAcquired |= S_PRIMARY;
2176 if (XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)) == owner)
2177 selectionAcquired |= S_CLIPBOARD;
2178 wine_tsx11_unlock();
2180 if (selectionAcquired)
2182 selectionWindow = owner;
2183 TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner);
2186 else
2188 ERR("Received request to acquire selection but process is already owner=(%08x)\n", (unsigned) selectionWindow);
2193 /**************************************************************************
2194 * X11DRV_EmptyClipboard
2196 * Empty cached clipboard data.
2198 void X11DRV_EmptyClipboard(BOOL keepunowned)
2200 if (ClipData)
2202 LPWINE_CLIPDATA lpData, lpStart;
2203 LPWINE_CLIPDATA lpNext = ClipData;
2205 TRACE(" called with %d entries in cache.\n", ClipDataCount);
2209 lpStart = ClipData;
2210 lpData = lpNext;
2211 lpNext = lpData->NextData;
2213 if (!keepunowned || !(lpData->wFlags & CF_FLAG_UNOWNED))
2215 lpData->PrevData->NextData = lpData->NextData;
2216 lpData->NextData->PrevData = lpData->PrevData;
2218 if (lpData == ClipData)
2219 ClipData = lpNext != lpData ? lpNext : NULL;
2221 X11DRV_CLIPBOARD_FreeData(lpData);
2222 HeapFree(GetProcessHeap(), 0, lpData);
2224 ClipDataCount--;
2226 } while (lpNext != lpStart);
2229 TRACE(" %d entries remaining in cache.\n", ClipDataCount);
2234 /**************************************************************************
2235 * X11DRV_SetClipboardData
2237 BOOL X11DRV_SetClipboardData(UINT wFormat, HANDLE16 hData16, HANDLE hData32, BOOL owner)
2239 DWORD flags = 0;
2240 BOOL bResult = TRUE;
2242 /* If it's not owned, data can only be set if the format data is not already owned
2243 and its rendering is not delayed */
2244 if (!owner)
2246 CLIPBOARDINFO cbinfo;
2247 LPWINE_CLIPDATA lpRender;
2249 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2251 if ((!hData16 && !hData32) ||
2252 ((lpRender = X11DRV_CLIPBOARD_LookupData(wFormat)) &&
2253 !(lpRender->wFlags & CF_FLAG_UNOWNED)))
2254 bResult = FALSE;
2255 else
2256 flags = CF_FLAG_UNOWNED;
2259 bResult &= X11DRV_CLIPBOARD_InsertClipboardData(wFormat, hData16, hData32, flags);
2261 return bResult;
2265 /**************************************************************************
2266 * CountClipboardFormats
2268 INT X11DRV_CountClipboardFormats(void)
2270 CLIPBOARDINFO cbinfo;
2272 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2274 TRACE(" count=%d\n", ClipDataCount);
2276 return ClipDataCount;
2280 /**************************************************************************
2281 * X11DRV_EnumClipboardFormats
2283 UINT X11DRV_EnumClipboardFormats(UINT wFormat)
2285 CLIPBOARDINFO cbinfo;
2286 UINT wNextFormat = 0;
2288 TRACE("(%04X)\n", wFormat);
2290 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2292 if (!wFormat)
2294 if (ClipData)
2295 wNextFormat = ClipData->wFormatID;
2297 else
2299 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormat);
2301 if (lpData && lpData->NextData != ClipData)
2302 wNextFormat = lpData->NextData->wFormatID;
2305 return wNextFormat;
2309 /**************************************************************************
2310 * X11DRV_IsClipboardFormatAvailable
2312 BOOL X11DRV_IsClipboardFormatAvailable(UINT wFormat)
2314 BOOL bRet = FALSE;
2315 CLIPBOARDINFO cbinfo;
2317 TRACE("(%04X)\n", wFormat);
2319 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2321 if (wFormat != 0 && X11DRV_CLIPBOARD_LookupData(wFormat))
2322 bRet = TRUE;
2324 TRACE("(%04X)- ret(%d)\n", wFormat, bRet);
2326 return bRet;
2330 /**************************************************************************
2331 * GetClipboardData (USER.142)
2333 BOOL X11DRV_GetClipboardData(UINT wFormat, HANDLE16* phData16, HANDLE* phData32)
2335 CLIPBOARDINFO cbinfo;
2336 LPWINE_CLIPDATA lpRender;
2338 TRACE("(%04X)\n", wFormat);
2340 X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2342 if ((lpRender = X11DRV_CLIPBOARD_LookupData(wFormat)))
2344 if ( !lpRender->hData32 )
2345 X11DRV_CLIPBOARD_RenderFormat(lpRender);
2347 /* Convert between 32 -> 16 bit data, if necessary */
2348 if (lpRender->hData32 && !lpRender->hData16)
2350 int size;
2352 if (lpRender->wFormatID == CF_METAFILEPICT)
2353 size = sizeof(METAFILEPICT16);
2354 else
2355 size = GlobalSize(lpRender->hData32);
2357 lpRender->hData16 = GlobalAlloc16(GMEM_ZEROINIT, size);
2359 if (!lpRender->hData16)
2360 ERR("(%04X) -- not enough memory in 16b heap\n", wFormat);
2361 else
2363 if (lpRender->wFormatID == CF_METAFILEPICT)
2365 FIXME("\timplement function CopyMetaFilePict32to16\n");
2366 FIXME("\tin the appropriate file.\n");
2367 #ifdef SOMEONE_IMPLEMENTED_ME
2368 CopyMetaFilePict32to16(GlobalLock16(lpRender->hData16),
2369 GlobalLock(lpRender->hData32));
2370 #endif
2372 else
2374 memcpy(GlobalLock16(lpRender->hData16),
2375 GlobalLock(lpRender->hData32), size);
2378 GlobalUnlock16(lpRender->hData16);
2379 GlobalUnlock(lpRender->hData32);
2383 /* Convert between 32 -> 16 bit data, if necessary */
2384 if (lpRender->hData16 && !lpRender->hData32)
2386 int size;
2388 if (lpRender->wFormatID == CF_METAFILEPICT)
2389 size = sizeof(METAFILEPICT16);
2390 else
2391 size = GlobalSize(lpRender->hData32);
2393 lpRender->hData32 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE |
2394 GMEM_DDESHARE, size);
2396 if (lpRender->wFormatID == CF_METAFILEPICT)
2398 FIXME("\timplement function CopyMetaFilePict16to32\n");
2399 FIXME("\tin the appropriate file.\n");
2400 #ifdef SOMEONE_IMPLEMENTED_ME
2401 CopyMetaFilePict16to32(GlobalLock16(lpRender->hData32),
2402 GlobalLock(lpRender->hData16));
2403 #endif
2405 else
2407 memcpy(GlobalLock(lpRender->hData32),
2408 GlobalLock16(lpRender->hData16), size);
2411 GlobalUnlock(lpRender->hData32);
2412 GlobalUnlock16(lpRender->hData16);
2415 if (phData16)
2416 *phData16 = lpRender->hData16;
2418 if (phData32)
2419 *phData32 = lpRender->hData32;
2421 TRACE(" returning hData16(%04x) hData32(%04x) (type %d)\n",
2422 lpRender->hData16, (unsigned int) lpRender->hData32, lpRender->wFormatID);
2424 return lpRender->hData16 || lpRender->hData32;
2427 return 0;
2431 /**************************************************************************
2432 * ResetSelectionOwner (X11DRV.@)
2434 * Called from DestroyWindow() to prevent X selection from being lost when
2435 * a top level window is destroyed, by switching ownership to another top
2436 * level window.
2437 * Any top level window can own the selection. See X11DRV_CLIPBOARD_Acquire
2438 * for a more detailed description of this.
2440 void X11DRV_ResetSelectionOwner(HWND hwnd, BOOL bFooBar)
2442 Display *display = thread_display();
2443 HWND hWndClipOwner = 0;
2444 HWND tmp;
2445 Window XWnd = X11DRV_get_whole_window(hwnd);
2446 BOOL bLostSelection = FALSE;
2447 Window selectionPrevWindow;
2449 /* There is nothing to do if we don't own the selection,
2450 * or if the X window which currently owns the selection is different
2451 * from the one passed in.
2453 if (!selectionAcquired || XWnd != selectionWindow
2454 || selectionWindow == None )
2455 return;
2457 if ((bFooBar && XWnd) || (!bFooBar && !XWnd))
2458 return;
2460 hWndClipOwner = GetClipboardOwner();
2462 TRACE("clipboard owner = %p, selection window = %08x\n",
2463 hWndClipOwner, (unsigned)selectionWindow);
2465 /* now try to salvage current selection from being destroyed by X */
2466 TRACE("checking %08x\n", (unsigned) XWnd);
2468 selectionPrevWindow = selectionWindow;
2469 selectionWindow = None;
2471 if (!(tmp = GetWindow(hwnd, GW_HWNDNEXT)))
2472 tmp = GetWindow(hwnd, GW_HWNDFIRST);
2474 if (tmp && tmp != hwnd)
2475 selectionWindow = X11DRV_get_whole_window(tmp);
2477 if (selectionWindow != None)
2479 /* We must pretend that we don't own the selection while making the switch
2480 * since a SelectionClear event will be sent to the last owner.
2481 * If there is no owner X11DRV_CLIPBOARD_ReleaseSelection will do nothing.
2483 int saveSelectionState = selectionAcquired;
2484 selectionAcquired = S_NOSELECTION;
2486 TRACE("\tswitching selection from %08x to %08x\n",
2487 (unsigned)selectionPrevWindow, (unsigned)selectionWindow);
2489 wine_tsx11_lock();
2491 /* Assume ownership for the PRIMARY and CLIPBOARD selection */
2492 if (saveSelectionState & S_PRIMARY)
2493 XSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime);
2495 XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), selectionWindow, CurrentTime);
2497 /* Restore the selection masks */
2498 selectionAcquired = saveSelectionState;
2500 /* Lose the selection if something went wrong */
2501 if (((saveSelectionState & S_PRIMARY) &&
2502 (XGetSelectionOwner(display, XA_PRIMARY) != selectionWindow)) ||
2503 (XGetSelectionOwner(display, x11drv_atom(CLIPBOARD)) != selectionWindow))
2505 bLostSelection = TRUE;
2507 wine_tsx11_unlock();
2509 else
2511 bLostSelection = TRUE;
2514 if (bLostSelection)
2516 TRACE("Lost the selection!\n");
2518 X11DRV_CLIPBOARD_ReleaseOwnership();
2519 selectionAcquired = S_NOSELECTION;
2520 selectionWindow = 0;
2525 /**************************************************************************
2526 * X11DRV_CLIPBOARD_SynthesizeData
2528 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID)
2530 BOOL bsyn = TRUE;
2531 LPWINE_CLIPDATA lpSource = NULL;
2533 TRACE(" %d\n", wFormatID);
2535 /* Don't need to synthesize if it already exists */
2536 if (X11DRV_CLIPBOARD_LookupData(wFormatID))
2537 return TRUE;
2539 if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
2541 bsyn = ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
2542 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2543 ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
2544 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2545 ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
2546 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED);
2548 else if (wFormatID == CF_ENHMETAFILE)
2550 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT)) &&
2551 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2553 else if (wFormatID == CF_METAFILEPICT)
2555 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT)) &&
2556 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2558 else if (wFormatID == CF_DIB)
2560 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) &&
2561 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2563 else if (wFormatID == CF_BITMAP)
2565 bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) &&
2566 ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2569 if (bsyn)
2570 X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, 0, 0, CF_FLAG_SYNTHESIZED);
2572 return bsyn;
2577 /**************************************************************************
2578 * X11DRV_EndClipboardUpdate
2579 * TODO:
2580 * Add locale if it hasn't already been added
2582 void X11DRV_EndClipboardUpdate(void)
2584 INT count = ClipDataCount;
2586 /* Do Unicode <-> Text <-> OEM mapping */
2587 X11DRV_CLIPBOARD_SynthesizeData(CF_UNICODETEXT);
2588 X11DRV_CLIPBOARD_SynthesizeData(CF_TEXT);
2589 X11DRV_CLIPBOARD_SynthesizeData(CF_OEMTEXT);
2591 /* Enhmetafile <-> MetafilePict mapping */
2592 X11DRV_CLIPBOARD_SynthesizeData(CF_ENHMETAFILE);
2593 X11DRV_CLIPBOARD_SynthesizeData(CF_METAFILEPICT);
2595 /* DIB <-> Bitmap mapping */
2596 X11DRV_CLIPBOARD_SynthesizeData(CF_DIB);
2597 X11DRV_CLIPBOARD_SynthesizeData(CF_BITMAP);
2599 TRACE("%d formats added to cached data\n", ClipDataCount - count);
2603 /***********************************************************************
2604 * add_targets
2606 * Utility function for X11DRV_SelectionRequest_TARGETS.
2608 static BOOL add_targets(Atom* targets, unsigned long cTargets, Atom prop)
2610 unsigned int i;
2611 BOOL bExists;
2613 /* Scan through what we have so far to avoid duplicates */
2614 for (i = 0, bExists = FALSE; i < cTargets; i++)
2616 if (targets[i] == prop)
2618 bExists = TRUE;
2619 break;
2623 if (!bExists)
2624 targets[cTargets] = prop;
2626 return !bExists;
2630 /***********************************************************************
2631 * X11DRV_SelectionRequest_TARGETS
2632 * Service a TARGETS selection request event
2634 static Atom X11DRV_SelectionRequest_TARGETS( Display *display, Window requestor,
2635 Atom target, Atom rprop )
2637 Atom* targets;
2638 UINT wFormat;
2639 UINT alias;
2640 ULONG cTargets;
2641 LPWINE_CLIPFORMAT lpFormat;
2644 * Count the number of items we wish to expose as selection targets.
2645 * We include the TARGETS item, and property aliases
2647 cTargets = X11DRV_CountClipboardFormats() + 1;
2649 for (wFormat = 0; (wFormat = X11DRV_EnumClipboardFormats(wFormat));)
2651 lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
2653 if (lpFormat)
2655 if (!lpFormat->lpDrvExportFunc)
2656 cTargets--;
2658 if (X11DRV_CLIPBOARD_LookupPropertyAlias(lpFormat->drvData))
2659 cTargets++;
2661 /* else most likely unregistered format such as CF_PRIVATE or CF_GDIOBJ */
2664 TRACE(" found %ld formats\n", cTargets);
2666 /* Allocate temp buffer */
2667 targets = (Atom*)HeapAlloc( GetProcessHeap(), 0, cTargets * sizeof(Atom));
2668 if(targets == NULL)
2669 return None;
2671 /* Create TARGETS property list (First item in list is TARGETS itself) */
2672 for (targets[0] = x11drv_atom(TARGETS), cTargets = 1, wFormat = 0;
2673 (wFormat = X11DRV_EnumClipboardFormats(wFormat));)
2675 lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
2677 if (lpFormat)
2679 if (lpFormat->lpDrvExportFunc)
2681 if (add_targets(targets, cTargets, lpFormat->drvData))
2682 cTargets++;
2685 /* Check if any alias should be listed */
2686 alias = X11DRV_CLIPBOARD_LookupPropertyAlias(lpFormat->drvData);
2687 if (alias)
2689 if (add_targets(targets, cTargets, alias))
2690 cTargets++;
2695 wine_tsx11_lock();
2697 if (TRACE_ON(clipboard))
2699 unsigned int i;
2700 for ( i = 0; i < cTargets; i++)
2702 if (targets[i])
2704 char *itemFmtName = XGetAtomName(display, targets[i]);
2705 TRACE("\tAtom# %d: Property %ld Type %s\n", i, targets[i], itemFmtName);
2706 XFree(itemFmtName);
2711 /* We may want to consider setting the type to xaTargets instead,
2712 * in case some apps expect this instead of XA_ATOM */
2713 XChangeProperty(display, requestor, rprop, XA_ATOM, 32,
2714 PropModeReplace, (unsigned char *)targets, cTargets);
2715 wine_tsx11_unlock();
2717 HeapFree(GetProcessHeap(), 0, targets);
2719 return rprop;
2723 /***********************************************************************
2724 * X11DRV_SelectionRequest_MULTIPLE
2725 * Service a MULTIPLE selection request event
2726 * rprop contains a list of (target,property) atom pairs.
2727 * The first atom names a target and the second names a property.
2728 * The effect is as if we have received a sequence of SelectionRequest events
2729 * (one for each atom pair) except that:
2730 * 1. We reply with a SelectionNotify only when all the requested conversions
2731 * have been performed.
2732 * 2. If we fail to convert the target named by an atom in the MULTIPLE property,
2733 * we replace the atom in the property by None.
2735 static Atom X11DRV_SelectionRequest_MULTIPLE( HWND hWnd, XSelectionRequestEvent *pevent )
2737 Display *display = pevent->display;
2738 Atom rprop;
2739 Atom atype=AnyPropertyType;
2740 int aformat;
2741 unsigned long remain;
2742 Atom* targetPropList=NULL;
2743 unsigned long cTargetPropList = 0;
2745 /* If the specified property is None the requestor is an obsolete client.
2746 * We support these by using the specified target atom as the reply property.
2748 rprop = pevent->property;
2749 if( rprop == None )
2750 rprop = pevent->target;
2751 if (!rprop)
2752 return 0;
2754 /* Read the MULTIPLE property contents. This should contain a list of
2755 * (target,property) atom pairs.
2757 wine_tsx11_lock();
2758 if(XGetWindowProperty(display, pevent->requestor, rprop,
2759 0, 0x3FFF, False, AnyPropertyType, &atype,&aformat,
2760 &cTargetPropList, &remain,
2761 (unsigned char**)&targetPropList) != Success)
2763 wine_tsx11_unlock();
2764 TRACE("\tCouldn't read MULTIPLE property\n");
2766 else
2768 TRACE("\tType %s,Format %d,nItems %ld, Remain %ld\n",
2769 XGetAtomName(display, atype), aformat, cTargetPropList, remain);
2770 wine_tsx11_unlock();
2773 * Make sure we got what we expect.
2774 * NOTE: According to the X-ICCCM Version 2.0 documentation the property sent
2775 * in a MULTIPLE selection request should be of type ATOM_PAIR.
2776 * However some X apps(such as XPaint) are not compliant with this and return
2777 * a user defined atom in atype when XGetWindowProperty is called.
2778 * The data *is* an atom pair but is not denoted as such.
2780 if(aformat == 32 /* atype == xAtomPair */ )
2782 unsigned int i;
2784 /* Iterate through the ATOM_PAIR list and execute a SelectionRequest
2785 * for each (target,property) pair */
2787 for (i = 0; i < cTargetPropList; i+=2)
2789 XSelectionRequestEvent event;
2791 if (TRACE_ON(clipboard))
2793 char *targetName, *propName;
2794 wine_tsx11_lock();
2795 targetName = XGetAtomName(display, targetPropList[i]);
2796 propName = XGetAtomName(display, targetPropList[i+1]);
2797 TRACE("MULTIPLE(%d): Target='%s' Prop='%s'\n",
2798 i/2, targetName, propName);
2799 XFree(targetName);
2800 XFree(propName);
2801 wine_tsx11_unlock();
2804 /* We must have a non "None" property to service a MULTIPLE target atom */
2805 if ( !targetPropList[i+1] )
2807 TRACE("\tMULTIPLE(%d): Skipping target with empty property!\n", i);
2808 continue;
2811 /* Set up an XSelectionRequestEvent for this (target,property) pair */
2812 memcpy( &event, pevent, sizeof(XSelectionRequestEvent) );
2813 event.target = targetPropList[i];
2814 event.property = targetPropList[i+1];
2816 /* Fire a SelectionRequest, informing the handler that we are processing
2817 * a MULTIPLE selection request event.
2819 X11DRV_HandleSelectionRequest( hWnd, &event, TRUE );
2823 /* Free the list of targets/properties */
2824 wine_tsx11_lock();
2825 XFree(targetPropList);
2826 wine_tsx11_unlock();
2829 return rprop;
2833 /***********************************************************************
2834 * X11DRV_HandleSelectionRequest
2835 * Process an event selection request event.
2836 * The bIsMultiple flag is used to signal when EVENT_SelectionRequest is called
2837 * recursively while servicing a "MULTIPLE" selection target.
2839 * Note: We only receive this event when WINE owns the X selection
2841 static void X11DRV_HandleSelectionRequest( HWND hWnd, XSelectionRequestEvent *event, BOOL bIsMultiple )
2843 Display *display = event->display;
2844 XSelectionEvent result;
2845 Atom rprop = None;
2846 Window request = event->requestor;
2848 TRACE("\n");
2851 * We can only handle the selection request if :
2852 * The selection is PRIMARY or CLIPBOARD, AND we can successfully open the clipboard.
2853 * Don't do these checks or open the clipboard while recursively processing MULTIPLE,
2854 * since this has been already done.
2856 if ( !bIsMultiple )
2858 if (((event->selection != XA_PRIMARY) && (event->selection != x11drv_atom(CLIPBOARD))))
2859 goto END;
2862 /* If the specified property is None the requestor is an obsolete client.
2863 * We support these by using the specified target atom as the reply property.
2865 rprop = event->property;
2866 if( rprop == None )
2867 rprop = event->target;
2869 if(event->target == x11drv_atom(TARGETS)) /* Return a list of all supported targets */
2871 /* TARGETS selection request */
2872 rprop = X11DRV_SelectionRequest_TARGETS( display, request, event->target, rprop );
2874 else if(event->target == x11drv_atom(MULTIPLE)) /* rprop contains a list of (target, property) atom pairs */
2876 /* MULTIPLE selection request */
2877 rprop = X11DRV_SelectionRequest_MULTIPLE( hWnd, event );
2879 else
2881 LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupProperty(event->target);
2883 if (!lpFormat)
2884 lpFormat = X11DRV_CLIPBOARD_LookupAliasProperty(event->target);
2886 if (lpFormat && lpFormat->lpDrvExportFunc)
2888 LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(lpFormat->wFormatID);
2890 if (lpData)
2892 unsigned char* lpClipData;
2893 DWORD cBytes;
2894 HANDLE hClipData = lpFormat->lpDrvExportFunc(request, event->target,
2895 rprop, lpData, &cBytes);
2897 if (hClipData && (lpClipData = GlobalLock(hClipData)))
2899 TRACE("\tUpdating property %s, %ld bytes\n", debugstr_w(lpFormat->Name), cBytes);
2901 wine_tsx11_lock();
2902 XChangeProperty(display, request, rprop, event->target,
2903 8, PropModeReplace, (unsigned char *)lpClipData, cBytes);
2904 wine_tsx11_unlock();
2906 GlobalUnlock(hClipData);
2907 GlobalFree(hClipData);
2913 END:
2914 /* reply to sender
2915 * SelectionNotify should be sent only at the end of a MULTIPLE request
2917 if ( !bIsMultiple )
2919 result.type = SelectionNotify;
2920 result.display = display;
2921 result.requestor = request;
2922 result.selection = event->selection;
2923 result.property = rprop;
2924 result.target = event->target;
2925 result.time = event->time;
2926 TRACE("Sending SelectionNotify event...\n");
2927 wine_tsx11_lock();
2928 XSendEvent(display,event->requestor,False,NoEventMask,(XEvent*)&result);
2929 wine_tsx11_unlock();
2934 /***********************************************************************
2935 * X11DRV_SelectionRequest
2937 void X11DRV_SelectionRequest( HWND hWnd, XEvent *event )
2939 if (!hWnd) return;
2940 X11DRV_HandleSelectionRequest( hWnd, &event->xselectionrequest, FALSE );
2944 /***********************************************************************
2945 * X11DRV_SelectionClear
2947 void X11DRV_SelectionClear( HWND hWnd, XEvent *xev )
2949 XSelectionClearEvent *event = &xev->xselectionclear;
2950 if (!hWnd) return;
2951 if (event->selection == XA_PRIMARY || event->selection == x11drv_atom(CLIPBOARD))
2952 X11DRV_CLIPBOARD_ReleaseSelection( event->selection, event->window, hWnd, event->time );