gphoto2.ds: Set supported groups.
[wine.git] / dlls / winex11.drv / clipboard.c
blobc55a2661fb422a8097199993a618033780cd1660
1 /*
2 * X11 clipboard windows driver
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1996 Alex Korobka
6 * Copyright 1999 Noel Borthwick
7 * Copyright 2003 Ulrich Czekalla for CodeWeavers
8 * Copyright 2014 Damjan Jovanovic
9 * Copyright 2016 Alexandre Julliard
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * NOTES:
26 * This file contains the X specific implementation for the windows
27 * Clipboard API.
29 * Wine's internal clipboard is exposed to external apps via the X
30 * selection mechanism.
31 * Currently the driver asserts ownership via two selection atoms:
32 * 1. PRIMARY(XA_PRIMARY)
33 * 2. CLIPBOARD
35 * In our implementation, the CLIPBOARD selection takes precedence over PRIMARY,
36 * i.e. if a CLIPBOARD selection is available, it is used instead of PRIMARY.
37 * When Wine takes ownership of the clipboard, it takes ownership of BOTH selections.
38 * While giving up selection ownership, if the CLIPBOARD selection is lost,
39 * it will lose both PRIMARY and CLIPBOARD and empty the clipboard.
40 * However if only PRIMARY is lost, it will continue to hold the CLIPBOARD selection
41 * (leaving the clipboard cache content unaffected).
43 * Every format exposed via a windows clipboard format is also exposed through
44 * a corresponding X selection target. A selection target atom is synthesized
45 * whenever a new Windows clipboard format is registered via RegisterClipboardFormat,
46 * or when a built-in format is used for the first time.
47 * Windows native format are exposed by prefixing the format name with "<WCF>"
48 * This allows us to uniquely identify windows native formats exposed by other
49 * running WINE apps.
51 * In order to allow external applications to query WINE for supported formats,
52 * we respond to the "TARGETS" selection target. (See EVENT_SelectionRequest
53 * for implementation) We use the same mechanism to query external clients for
54 * availability of a particular format, by caching the list of available targets
55 * by using the clipboard cache's "delayed render" mechanism. If a selection client
56 * does not support the "TARGETS" selection target, we actually attempt to retrieve
57 * the format requested as a fallback mechanism.
59 * Certain Windows native formats are automatically converted to X native formats
60 * and vice versa. If a native format is available in the selection, it takes
61 * precedence, in order to avoid unnecessary conversions.
63 * FIXME: global format list needs a critical section
66 #include "config.h"
67 #include "wine/port.h"
69 #include <string.h>
70 #include <stdarg.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #ifdef HAVE_UNISTD_H
74 # include <unistd.h>
75 #endif
76 #include <fcntl.h>
77 #include <limits.h>
78 #include <time.h>
79 #include <assert.h>
81 #include "x11drv.h"
83 #ifdef HAVE_X11_EXTENSIONS_XFIXES_H
84 #include <X11/extensions/Xfixes.h>
85 #endif
87 #include "shlobj.h"
88 #include "shellapi.h"
89 #include "shlwapi.h"
90 #include "wine/library.h"
91 #include "wine/list.h"
92 #include "wine/debug.h"
93 #include "wine/unicode.h"
95 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
97 /* Maximum wait time for selection notify */
98 #define SELECTION_RETRIES 500 /* wait for .5 seconds */
99 #define SELECTION_WAIT 1000 /* us */
101 #define SELECTION_UPDATE_DELAY 2000 /* delay between checks of the X11 selection */
103 typedef BOOL (*EXPORTFUNC)( Display *display, Window win, Atom prop, Atom target, HANDLE handle );
104 typedef HANDLE (*IMPORTFUNC)( Atom type, const void *data, size_t size );
106 struct clipboard_format
108 struct list entry;
109 UINT id;
110 Atom atom;
111 IMPORTFUNC import;
112 EXPORTFUNC export;
115 static HANDLE import_data( Atom type, const void *data, size_t size );
116 static HANDLE import_enhmetafile( Atom type, const void *data, size_t size );
117 static HANDLE import_pixmap( Atom type, const void *data, size_t size );
118 static HANDLE import_image_bmp( Atom type, const void *data, size_t size );
119 static HANDLE import_string( Atom type, const void *data, size_t size );
120 static HANDLE import_utf8_string( Atom type, const void *data, size_t size );
121 static HANDLE import_compound_text( Atom type, const void *data, size_t size );
122 static HANDLE import_text( Atom type, const void *data, size_t size );
123 static HANDLE import_text_html( Atom type, const void *data, size_t size );
124 static HANDLE import_text_uri_list( Atom type, const void *data, size_t size );
125 static HANDLE import_targets( Atom type, const void *data, size_t size );
127 static BOOL export_data( Display *display, Window win, Atom prop, Atom target, HANDLE handle );
128 static BOOL export_string( Display *display, Window win, Atom prop, Atom target, HANDLE handle );
129 static BOOL export_utf8_string( Display *display, Window win, Atom prop, Atom target, HANDLE handle );
130 static BOOL export_text( Display *display, Window win, Atom prop, Atom target, HANDLE handle );
131 static BOOL export_compound_text( Display *display, Window win, Atom prop, Atom target, HANDLE handle );
132 static BOOL export_pixmap( Display *display, Window win, Atom prop, Atom target, HANDLE handle );
133 static BOOL export_image_bmp( Display *display, Window win, Atom prop, Atom target, HANDLE handle );
134 static BOOL export_enhmetafile( Display *display, Window win, Atom prop, Atom target, HANDLE handle );
135 static BOOL export_text_html( Display *display, Window win, Atom prop, Atom target, HANDLE handle );
136 static BOOL export_hdrop( Display *display, Window win, Atom prop, Atom target, HANDLE handle );
137 static BOOL export_targets( Display *display, Window win, Atom prop, Atom target, HANDLE handle );
138 static BOOL export_multiple( Display *display, Window win, Atom prop, Atom target, HANDLE handle );
139 static BOOL export_timestamp( Display *display, Window win, Atom prop, Atom target, HANDLE handle );
141 static BOOL read_property( Display *display, Window w, Atom prop,
142 Atom *type, unsigned char **data, unsigned long *datasize );
144 /* Clipboard formats */
146 static const WCHAR RichTextFormatW[] = {'R','i','c','h',' ','T','e','x','t',' ','F','o','r','m','a','t',0};
147 static const WCHAR GIFW[] = {'G','I','F',0};
148 static const WCHAR JFIFW[] = {'J','F','I','F',0};
149 static const WCHAR PNGW[] = {'P','N','G',0};
150 static const WCHAR HTMLFormatW[] = {'H','T','M','L',' ','F','o','r','m','a','t',0};
152 static const struct
154 const WCHAR *name;
155 UINT id;
156 UINT data;
157 IMPORTFUNC import;
158 EXPORTFUNC export;
159 } builtin_formats[] =
161 { 0, CF_UNICODETEXT, XATOM_UTF8_STRING, import_utf8_string, export_utf8_string },
162 { 0, CF_UNICODETEXT, XATOM_COMPOUND_TEXT, import_compound_text, export_compound_text },
163 { 0, CF_UNICODETEXT, XA_STRING, import_string, export_string },
164 { 0, CF_UNICODETEXT, XATOM_text_plain, import_string, export_string },
165 { 0, CF_UNICODETEXT, XATOM_TEXT, import_text, export_text },
166 { 0, CF_SYLK, XATOM_WCF_SYLK, import_data, export_data },
167 { 0, CF_DIF, XATOM_WCF_DIF, import_data, export_data },
168 { 0, CF_TIFF, XATOM_WCF_TIFF, import_data, export_data },
169 { 0, CF_DIB, XA_PIXMAP, import_pixmap, export_pixmap },
170 { 0, CF_PENDATA, XATOM_WCF_PENDATA, import_data, export_data },
171 { 0, CF_RIFF, XATOM_WCF_RIFF, import_data, export_data },
172 { 0, CF_WAVE, XATOM_WCF_WAVE, import_data, export_data },
173 { 0, CF_ENHMETAFILE, XATOM_WCF_ENHMETAFILE, import_enhmetafile, export_enhmetafile },
174 { 0, CF_HDROP, XATOM_text_uri_list, import_text_uri_list, export_hdrop },
175 { 0, CF_DIB, XATOM_image_bmp, import_image_bmp, export_image_bmp },
176 { RichTextFormatW, 0, XATOM_text_rtf, import_data, export_data },
177 { RichTextFormatW, 0, XATOM_text_richtext, import_data, export_data },
178 { GIFW, 0, XATOM_image_gif, import_data, export_data },
179 { JFIFW, 0, XATOM_image_jpeg, import_data, export_data },
180 { PNGW, 0, XATOM_image_png, import_data, export_data },
181 { HTMLFormatW, 0, XATOM_HTML_Format, import_data, export_data },
182 { HTMLFormatW, 0, XATOM_text_html, import_text_html, export_text_html },
183 { 0, 0, XATOM_TARGETS, import_targets, export_targets },
184 { 0, 0, XATOM_MULTIPLE, NULL, export_multiple },
185 { 0, 0, XATOM_TIMESTAMP, NULL, export_timestamp },
188 static struct list format_list = LIST_INIT( format_list );
190 #define NB_BUILTIN_FORMATS (sizeof(builtin_formats) / sizeof(builtin_formats[0]))
191 #define GET_ATOM(prop) (((prop) < FIRST_XATOM) ? (Atom)(prop) : X11DRV_Atoms[(prop) - FIRST_XATOM])
193 static DWORD clipboard_thread_id;
194 static HWND clipboard_hwnd;
195 static BOOL is_clipboard_owner;
196 static Window selection_window;
197 static Window import_window;
198 static Atom current_selection;
199 static UINT rendered_formats;
200 static ULONG64 last_clipboard_update;
201 static struct clipboard_format **current_x11_formats;
202 static unsigned int nb_current_x11_formats;
203 static BOOL use_xfixes;
205 Display *clipboard_display = NULL;
207 static const char *debugstr_format( UINT id )
209 WCHAR buffer[256];
211 if (GetClipboardFormatNameW( id, buffer, 256 ))
212 return wine_dbg_sprintf( "%04x %s", id, debugstr_w(buffer) );
214 switch (id)
216 case 0: return "(none)";
217 #define BUILTIN(id) case id: return #id;
218 BUILTIN(CF_TEXT)
219 BUILTIN(CF_BITMAP)
220 BUILTIN(CF_METAFILEPICT)
221 BUILTIN(CF_SYLK)
222 BUILTIN(CF_DIF)
223 BUILTIN(CF_TIFF)
224 BUILTIN(CF_OEMTEXT)
225 BUILTIN(CF_DIB)
226 BUILTIN(CF_PALETTE)
227 BUILTIN(CF_PENDATA)
228 BUILTIN(CF_RIFF)
229 BUILTIN(CF_WAVE)
230 BUILTIN(CF_UNICODETEXT)
231 BUILTIN(CF_ENHMETAFILE)
232 BUILTIN(CF_HDROP)
233 BUILTIN(CF_LOCALE)
234 BUILTIN(CF_DIBV5)
235 BUILTIN(CF_OWNERDISPLAY)
236 BUILTIN(CF_DSPTEXT)
237 BUILTIN(CF_DSPBITMAP)
238 BUILTIN(CF_DSPMETAFILEPICT)
239 BUILTIN(CF_DSPENHMETAFILE)
240 #undef BUILTIN
241 default: return wine_dbg_sprintf( "%04x", id );
245 static const char *debugstr_xatom( Atom atom )
247 const char *ret;
248 char *name;
250 if (!atom) return "(None)";
251 name = XGetAtomName( thread_display(), atom );
252 ret = debugstr_a( name );
253 XFree( name );
254 return ret;
258 static int is_atom_error( Display *display, XErrorEvent *event, void *arg )
260 return (event->error_code == BadAtom);
264 /**************************************************************************
265 * find_win32_format
267 static struct clipboard_format *find_win32_format( UINT id )
269 struct clipboard_format *format;
271 LIST_FOR_EACH_ENTRY( format, &format_list, struct clipboard_format, entry )
272 if (format->id == id) return format;
273 return NULL;
277 /**************************************************************************
278 * find_x11_format
280 static struct clipboard_format *find_x11_format( Atom atom )
282 struct clipboard_format *format;
284 LIST_FOR_EACH_ENTRY( format, &format_list, struct clipboard_format, entry )
285 if (format->atom == atom) return format;
286 return NULL;
290 /**************************************************************************
291 * register_builtin_formats
293 static void register_builtin_formats(void)
295 struct clipboard_format *formats;
296 unsigned int i;
298 if (!(formats = HeapAlloc( GetProcessHeap(), 0, NB_BUILTIN_FORMATS * sizeof(*formats)))) return;
300 for (i = 0; i < NB_BUILTIN_FORMATS; i++)
302 if (builtin_formats[i].name)
303 formats[i].id = RegisterClipboardFormatW( builtin_formats[i].name );
304 else
305 formats[i].id = builtin_formats[i].id;
307 formats[i].atom = GET_ATOM(builtin_formats[i].data);
308 formats[i].import = builtin_formats[i].import;
309 formats[i].export = builtin_formats[i].export;
310 list_add_tail( &format_list, &formats[i].entry );
315 /**************************************************************************
316 * register_formats
318 static void register_formats( const UINT *ids, const Atom *atoms, unsigned int count )
320 struct clipboard_format *formats;
321 unsigned int i;
323 if (!(formats = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*formats)))) return;
325 for (i = 0; i < count; i++)
327 formats[i].id = ids[i];
328 formats[i].atom = atoms[i];
329 formats[i].import = import_data;
330 formats[i].export = export_data;
331 list_add_tail( &format_list, &formats[i].entry );
332 TRACE( "registered %s atom %s\n", debugstr_format( ids[i] ), debugstr_xatom( atoms[i] ));
337 /**************************************************************************
338 * register_win32_formats
340 * Register Win32 clipboard formats the first time we encounter them.
342 static void register_win32_formats( const UINT *ids, UINT size )
344 unsigned int count, len;
345 UINT new_ids[256];
346 char *names[256];
347 Atom atoms[256];
348 WCHAR buffer[256];
350 if (list_empty( &format_list)) register_builtin_formats();
352 while (size)
354 for (count = 0; count < 256 && size; ids++, size--)
356 if (find_win32_format( *ids )) continue; /* it already exists */
357 if (!GetClipboardFormatNameW( *ids, buffer, 256 )) continue; /* not a named format */
358 if (!(len = WideCharToMultiByte( CP_UNIXCP, 0, buffer, -1, NULL, 0, NULL, NULL ))) continue;
359 if (!(names[count] = HeapAlloc( GetProcessHeap(), 0, len ))) continue;
360 WideCharToMultiByte( CP_UNIXCP, 0, buffer, -1, names[count], len, NULL, NULL );
361 new_ids[count++] = *ids;
363 if (!count) return;
365 XInternAtoms( thread_display(), names, count, False, atoms );
366 register_formats( new_ids, atoms, count );
367 while (count) HeapFree( GetProcessHeap(), 0, names[--count] );
372 /**************************************************************************
373 * register_x11_formats
375 * Register X11 atom formats the first time we encounter them.
377 static void register_x11_formats( const Atom *atoms, UINT size )
379 Display *display = thread_display();
380 unsigned int i, pos, count;
381 char *names[256];
382 UINT ids[256];
383 Atom new_atoms[256];
384 WCHAR buffer[256];
386 if (list_empty( &format_list)) register_builtin_formats();
388 while (size)
390 for (count = 0; count < 256 && size; atoms++, size--)
391 if (!find_x11_format( *atoms )) new_atoms[count++] = *atoms;
393 if (!count) return;
395 X11DRV_expect_error( display, is_atom_error, NULL );
396 if (!XGetAtomNames( display, new_atoms, count, names )) count = 0;
397 if (X11DRV_check_error())
399 WARN( "got some bad atoms, ignoring\n" );
400 count = 0;
403 for (i = pos = 0; i < count; i++)
405 if (MultiByteToWideChar( CP_UNIXCP, 0, names[i], -1, buffer, 256 ) &&
406 (ids[pos] = RegisterClipboardFormatW( buffer )))
407 new_atoms[pos++] = new_atoms[i];
408 XFree( names[i] );
410 register_formats( ids, new_atoms, pos );
415 /**************************************************************************
416 * put_property
418 * Put data as a property on the specified window.
420 static void put_property( Display *display, Window win, Atom prop, Atom type, int format,
421 const void *ptr, size_t size )
423 const unsigned char *data = ptr;
424 int mode = PropModeReplace;
425 size_t width = (format == 32) ? sizeof(long) : format / 8;
426 size_t max_size = XExtendedMaxRequestSize( display ) * 4;
428 if (!max_size) max_size = XMaxRequestSize( display ) * 4;
429 max_size -= 64; /* request overhead */
433 size_t count = min( size, max_size / width );
434 XChangeProperty( display, win, prop, type, format, mode, data, count );
435 mode = PropModeAppend;
436 size -= count;
437 data += count * width;
438 } while (size > 0);
442 /**************************************************************************
443 * convert_selection
445 static BOOL convert_selection( Display *display, Window win, Atom selection,
446 struct clipboard_format *format, Atom *type,
447 unsigned char **data, unsigned long *size )
449 int i;
450 XEvent event;
452 TRACE( "import %s from %s win %lx to format %s\n",
453 debugstr_xatom( format->atom ), debugstr_xatom( selection ),
454 win, debugstr_format( format->id ) );
456 XConvertSelection( display, selection, format->atom, x11drv_atom(SELECTION_DATA), win, CurrentTime );
458 for (i = 0; i < SELECTION_RETRIES; i++)
460 Bool res = XCheckTypedWindowEvent( display, win, SelectionNotify, &event );
461 if (res && event.xselection.selection == selection && event.xselection.target == format->atom)
462 return read_property( display, win, event.xselection.property, type, data, size );
463 usleep( SELECTION_WAIT );
465 ERR( "Timed out waiting for SelectionNotify event\n" );
466 return FALSE;
470 /***********************************************************************
471 * bitmap_info_size
473 * Return the size of the bitmap info structure including color table.
475 static int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
477 unsigned int colors, size, masks = 0;
479 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
481 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
482 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
483 return sizeof(BITMAPCOREHEADER) + colors *
484 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
486 else /* assume BITMAPINFOHEADER */
488 colors = info->bmiHeader.biClrUsed;
489 if (!colors && (info->bmiHeader.biBitCount <= 8))
490 colors = 1 << info->bmiHeader.biBitCount;
491 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
492 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
493 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
498 /***********************************************************************
499 * create_dib_from_bitmap
501 * Allocates a packed DIB and copies the bitmap data into it.
503 static HGLOBAL create_dib_from_bitmap(HBITMAP hBmp)
505 BITMAP bmp;
506 HDC hdc;
507 HGLOBAL hPackedDIB;
508 LPBYTE pPackedDIB;
509 LPBITMAPINFOHEADER pbmiHeader;
510 unsigned int cDataSize, cPackedSize, OffsetBits;
511 int nLinesCopied;
513 if (!GetObjectW( hBmp, sizeof(bmp), &bmp )) return 0;
516 * A packed DIB contains a BITMAPINFO structure followed immediately by
517 * an optional color palette and the pixel data.
520 /* Calculate the size of the packed DIB */
521 cDataSize = abs( bmp.bmHeight ) * (((bmp.bmWidth * bmp.bmBitsPixel + 31) / 8) & ~3);
522 cPackedSize = sizeof(BITMAPINFOHEADER)
523 + ( (bmp.bmBitsPixel <= 8) ? (sizeof(RGBQUAD) * (1 << bmp.bmBitsPixel)) : 0 )
524 + cDataSize;
525 /* Get the offset to the bits */
526 OffsetBits = cPackedSize - cDataSize;
528 /* Allocate the packed DIB */
529 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
530 hPackedDIB = GlobalAlloc( GMEM_FIXED, cPackedSize );
531 if ( !hPackedDIB )
533 WARN("Could not allocate packed DIB!\n");
534 return 0;
537 /* A packed DIB starts with a BITMAPINFOHEADER */
538 pPackedDIB = GlobalLock(hPackedDIB);
539 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
541 /* Init the BITMAPINFOHEADER */
542 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
543 pbmiHeader->biWidth = bmp.bmWidth;
544 pbmiHeader->biHeight = bmp.bmHeight;
545 pbmiHeader->biPlanes = 1;
546 pbmiHeader->biBitCount = bmp.bmBitsPixel;
547 pbmiHeader->biCompression = BI_RGB;
548 pbmiHeader->biSizeImage = 0;
549 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
550 pbmiHeader->biClrUsed = 0;
551 pbmiHeader->biClrImportant = 0;
553 /* Retrieve the DIB bits from the bitmap and fill in the
554 * DIB color table if present */
555 hdc = GetDC( 0 );
556 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
557 hBmp, /* Handle to bitmap */
558 0, /* First scan line to set in dest bitmap */
559 bmp.bmHeight, /* Number of scan lines to copy */
560 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
561 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
562 0); /* RGB or palette index */
563 GlobalUnlock(hPackedDIB);
564 ReleaseDC( 0, hdc );
566 /* Cleanup if GetDIBits failed */
567 if (nLinesCopied != bmp.bmHeight)
569 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, bmp.bmHeight);
570 GlobalFree(hPackedDIB);
571 hPackedDIB = 0;
573 return hPackedDIB;
577 /***********************************************************************
578 * uri_to_dos
580 * Converts a text/uri-list URI to DOS filename.
582 static WCHAR* uri_to_dos(char *encodedURI)
584 WCHAR *ret = NULL;
585 int i;
586 int j = 0;
587 char *uri = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlen(encodedURI) + 1);
588 if (uri == NULL)
589 return NULL;
590 for (i = 0; encodedURI[i]; ++i)
592 if (encodedURI[i] == '%')
594 if (encodedURI[i+1] && encodedURI[i+2])
596 char buffer[3];
597 int number;
598 buffer[0] = encodedURI[i+1];
599 buffer[1] = encodedURI[i+2];
600 buffer[2] = '\0';
601 sscanf(buffer, "%x", &number);
602 uri[j++] = number;
603 i += 2;
605 else
607 WARN("invalid URI encoding in %s\n", debugstr_a(encodedURI));
608 HeapFree(GetProcessHeap(), 0, uri);
609 return NULL;
612 else
613 uri[j++] = encodedURI[i];
616 /* Read http://www.freedesktop.org/wiki/Draganddropwarts and cry... */
617 if (strncmp(uri, "file:/", 6) == 0)
619 if (uri[6] == '/')
621 if (uri[7] == '/')
623 /* file:///path/to/file (nautilus, thunar) */
624 ret = wine_get_dos_file_name(&uri[7]);
626 else if (uri[7])
628 /* file://hostname/path/to/file (X file drag spec) */
629 char hostname[256];
630 char *path = strchr(&uri[7], '/');
631 if (path)
633 *path = '\0';
634 if (strcmp(&uri[7], "localhost") == 0)
636 *path = '/';
637 ret = wine_get_dos_file_name(path);
639 else if (gethostname(hostname, sizeof(hostname)) == 0)
641 if (strcmp(hostname, &uri[7]) == 0)
643 *path = '/';
644 ret = wine_get_dos_file_name(path);
650 else if (uri[6])
652 /* file:/path/to/file (konqueror) */
653 ret = wine_get_dos_file_name(&uri[5]);
656 HeapFree(GetProcessHeap(), 0, uri);
657 return ret;
661 /**************************************************************************
662 * unicode_text_from_string
664 * Convert a string in the specified encoding to CF_UNICODETEXT format.
666 static HANDLE unicode_text_from_string( UINT codepage, const void *data, size_t size )
668 DWORD i, j, count;
669 WCHAR *strW;
671 count = MultiByteToWideChar( codepage, 0, data, size, NULL, 0);
673 if (!(strW = GlobalAlloc( GMEM_FIXED, (count * 2 + 1) * sizeof(WCHAR) ))) return 0;
675 MultiByteToWideChar( codepage, 0, data, size, strW + count, count );
676 for (i = j = 0; i < count; i++)
678 if (strW[i + count] == '\n') strW[j++] = '\r';
679 strW[j++] = strW[i + count];
681 strW[j++] = 0;
682 GlobalReAlloc( strW, j * sizeof(WCHAR), GMEM_FIXED ); /* release unused space */
683 TRACE( "returning %s\n", debugstr_wn( strW, j - 1 ));
684 return strW;
688 /**************************************************************************
689 * import_string
691 * Import XA_STRING, converting the string to CF_UNICODETEXT.
693 static HANDLE import_string( Atom type, const void *data, size_t size )
695 return unicode_text_from_string( 28591, data, size );
699 /**************************************************************************
700 * import_utf8_string
702 * Import XA_UTF8_STRING, converting the string to CF_UNICODETEXT.
704 static HANDLE import_utf8_string( Atom type, const void *data, size_t size )
706 return unicode_text_from_string( CP_UTF8, data, size );
710 /**************************************************************************
711 * import_compound_text
713 * Import COMPOUND_TEXT to CF_UNICODETEXT.
715 static HANDLE import_compound_text( Atom type, const void *data, size_t size )
717 char** srcstr;
718 int count;
719 HANDLE ret;
720 XTextProperty txtprop;
722 txtprop.value = (BYTE *)data;
723 txtprop.nitems = size;
724 txtprop.encoding = x11drv_atom(COMPOUND_TEXT);
725 txtprop.format = 8;
726 if (XmbTextPropertyToTextList( thread_display(), &txtprop, &srcstr, &count ) != Success) return 0;
727 if (!count) return 0;
729 ret = unicode_text_from_string( CP_UNIXCP, srcstr[0], strlen(srcstr[0]) + 1 );
730 XFreeStringList(srcstr);
731 return ret;
735 /**************************************************************************
736 * import_text
738 * Import XA_TEXT, converting the string to CF_UNICODETEXT.
740 static HANDLE import_text( Atom type, const void *data, size_t size )
742 if (type == XA_STRING) return import_string( type, data, size );
743 if (type == x11drv_atom(UTF8_STRING)) return import_utf8_string( type, data, size );
744 if (type == x11drv_atom(COMPOUND_TEXT)) return import_compound_text( type, data, size );
745 FIXME( "unsupported TEXT type %s\n", debugstr_xatom( type ));
746 return 0;
750 /**************************************************************************
751 * import_pixmap
753 * Import XA_PIXMAP, converting the image to CF_DIB.
755 static HANDLE import_pixmap( Atom type, const void *data, size_t size )
757 const Pixmap *pPixmap = (const Pixmap *)data;
758 BYTE *ptr = NULL;
759 XVisualInfo vis = default_visual;
760 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
761 BITMAPINFO *info = (BITMAPINFO *)buffer;
762 struct gdi_image_bits bits;
763 Window root;
764 int x,y; /* Unused */
765 unsigned border_width; /* Unused */
766 unsigned int depth, width, height;
768 /* Get the Pixmap dimensions and bit depth */
769 if (!XGetGeometry(gdi_display, *pPixmap, &root, &x, &y, &width, &height,
770 &border_width, &depth)) depth = 0;
771 if (!pixmap_formats[depth]) return 0;
773 TRACE( "pixmap properties: width=%d, height=%d, depth=%d\n", width, height, depth );
775 if (depth != vis.depth) switch (pixmap_formats[depth]->bits_per_pixel)
777 case 1:
778 case 4:
779 case 8:
780 break;
781 case 16: /* assume R5G5B5 */
782 vis.red_mask = 0x7c00;
783 vis.green_mask = 0x03e0;
784 vis.blue_mask = 0x001f;
785 break;
786 case 24: /* assume R8G8B8 */
787 case 32: /* assume A8R8G8B8 */
788 vis.red_mask = 0xff0000;
789 vis.green_mask = 0x00ff00;
790 vis.blue_mask = 0x0000ff;
791 break;
792 default:
793 return 0;
796 if (!get_pixmap_image( *pPixmap, width, height, &vis, info, &bits ))
798 DWORD info_size = bitmap_info_size( info, DIB_RGB_COLORS );
800 ptr = GlobalAlloc( GMEM_FIXED, info_size + info->bmiHeader.biSizeImage );
801 if (ptr)
803 memcpy( ptr, info, info_size );
804 memcpy( ptr + info_size, bits.ptr, info->bmiHeader.biSizeImage );
806 if (bits.free) bits.free( &bits );
808 return ptr;
812 /**************************************************************************
813 * import_image_bmp
815 * Import image/bmp, converting the image to CF_DIB.
817 static HANDLE import_image_bmp( Atom type, const void *data, size_t size )
819 HANDLE hClipData = 0;
820 const BITMAPFILEHEADER *bfh = data;
822 if (size >= sizeof(BITMAPFILEHEADER)+sizeof(BITMAPCOREHEADER) &&
823 bfh->bfType == 0x4d42 /* "BM" */)
825 const BITMAPINFO *bmi = (const BITMAPINFO *)(bfh + 1);
826 HBITMAP hbmp;
827 HDC hdc = GetDC(0);
829 if ((hbmp = CreateDIBitmap( hdc, &bmi->bmiHeader, CBM_INIT,
830 (const BYTE *)data + bfh->bfOffBits, bmi, DIB_RGB_COLORS )))
832 hClipData = create_dib_from_bitmap( hbmp );
833 DeleteObject(hbmp);
835 ReleaseDC(0, hdc);
837 return hClipData;
841 /**************************************************************************
842 * import_enhmetafile
844 static HANDLE import_enhmetafile( Atom type, const void *data, size_t size )
846 return SetEnhMetaFileBits( size, data );
850 /**************************************************************************
851 * import_text_html
853 static HANDLE import_text_html( Atom type, const void *data, size_t size )
855 static const char header[] =
856 "Version:0.9\n"
857 "StartHTML:0000000100\n"
858 "EndHTML:%010lu\n"
859 "StartFragment:%010lu\n"
860 "EndFragment:%010lu\n"
861 "<!--StartFragment-->";
862 static const char trailer[] = "\n<!--EndFragment-->";
863 char *text = NULL;
864 HANDLE ret;
865 SIZE_T len, total;
867 /* Firefox uses UTF-16LE with byte order mark. Convert to UTF-8 without the BOM. */
868 if (size >= sizeof(WCHAR) && ((const WCHAR *)data)[0] == 0xfeff)
870 len = WideCharToMultiByte( CP_UTF8, 0, (const WCHAR *)data + 1, size / sizeof(WCHAR) - 1,
871 NULL, 0, NULL, NULL );
872 if (!(text = HeapAlloc( GetProcessHeap(), 0, len ))) return 0;
873 WideCharToMultiByte( CP_UTF8, 0, (const WCHAR *)data + 1, size / sizeof(WCHAR) - 1,
874 text, len, NULL, NULL );
875 size = len;
876 data = text;
879 len = strlen( header ) + 12; /* 3 * 4 extra chars for %010lu */
880 total = len + size + sizeof(trailer);
881 if ((ret = GlobalAlloc( GMEM_FIXED, total )))
883 char *p = ret;
884 p += sprintf( p, header, total - 1, len, len + size + 1 /* include the final \n in the data */ );
885 memcpy( p, data, size );
886 strcpy( p + size, trailer );
887 TRACE( "returning %s\n", debugstr_a( ret ));
889 HeapFree( GetProcessHeap(), 0, text );
890 return ret;
894 /**************************************************************************
895 * import_text_uri_list
897 * Import text/uri-list.
899 static HANDLE import_text_uri_list( Atom type, const void *data, size_t size )
901 const char *uriList = data;
902 char *uri;
903 WCHAR *path;
904 WCHAR *out = NULL;
905 int total = 0;
906 int capacity = 4096;
907 int start = 0;
908 int end = 0;
909 DROPFILES *dropFiles = NULL;
911 if (!(out = HeapAlloc(GetProcessHeap(), 0, capacity * sizeof(WCHAR)))) return 0;
913 while (end < size)
915 while (end < size && uriList[end] != '\r')
916 ++end;
917 if (end < (size - 1) && uriList[end+1] != '\n')
919 WARN("URI list line doesn't end in \\r\\n\n");
920 break;
923 uri = HeapAlloc(GetProcessHeap(), 0, end - start + 1);
924 if (uri == NULL)
925 break;
926 lstrcpynA(uri, &uriList[start], end - start + 1);
927 path = uri_to_dos(uri);
928 TRACE("converted URI %s to DOS path %s\n", debugstr_a(uri), debugstr_w(path));
929 HeapFree(GetProcessHeap(), 0, uri);
931 if (path)
933 int pathSize = strlenW(path) + 1;
934 if (pathSize > capacity - total)
936 capacity = 2*capacity + pathSize;
937 out = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, out, (capacity + 1)*sizeof(WCHAR));
938 if (out == NULL)
939 goto done;
941 memcpy(&out[total], path, pathSize * sizeof(WCHAR));
942 total += pathSize;
943 done:
944 HeapFree(GetProcessHeap(), 0, path);
945 if (out == NULL)
946 break;
949 start = end + 2;
950 end = start;
952 if (out && end >= size)
954 if ((dropFiles = GlobalAlloc( GMEM_FIXED, sizeof(DROPFILES) + (total + 1) * sizeof(WCHAR) )))
956 dropFiles->pFiles = sizeof(DROPFILES);
957 dropFiles->pt.x = 0;
958 dropFiles->pt.y = 0;
959 dropFiles->fNC = 0;
960 dropFiles->fWide = TRUE;
961 out[total] = '\0';
962 memcpy( (char*)dropFiles + dropFiles->pFiles, out, (total + 1) * sizeof(WCHAR) );
965 HeapFree(GetProcessHeap(), 0, out);
966 return dropFiles;
970 /**************************************************************************
971 * import_targets
973 * Import TARGETS and mark the corresponding clipboard formats as available.
975 static HANDLE import_targets( Atom type, const void *data, size_t size )
977 UINT i, pos, count = size / sizeof(Atom);
978 const Atom *properties = data;
979 struct clipboard_format *format, **formats;
981 if (type != XA_ATOM && type != x11drv_atom(TARGETS)) return 0;
983 register_x11_formats( properties, count );
985 /* the builtin formats contain duplicates, so allocate some extra space */
986 if (!(formats = HeapAlloc( GetProcessHeap(), 0, (count + NB_BUILTIN_FORMATS) * sizeof(*formats ))))
987 return 0;
989 pos = 0;
990 LIST_FOR_EACH_ENTRY( format, &format_list, struct clipboard_format, entry )
992 for (i = 0; i < count; i++) if (properties[i] == format->atom) break;
993 if (i == count) continue;
994 if (format->import && format->id)
996 TRACE( "property %s -> format %s\n",
997 debugstr_xatom( properties[i] ), debugstr_format( format->id ));
998 SetClipboardData( format->id, 0 );
999 formats[pos++] = format;
1001 else TRACE( "property %s (ignoring)\n", debugstr_xatom( properties[i] ));
1004 HeapFree( GetProcessHeap(), 0, current_x11_formats );
1005 current_x11_formats = formats;
1006 nb_current_x11_formats = pos;
1007 return (HANDLE)1;
1011 /**************************************************************************
1012 * import_data
1014 * Generic import clipboard data routine.
1016 static HANDLE import_data( Atom type, const void *data, size_t size )
1018 void *ret = GlobalAlloc( GMEM_FIXED, size );
1020 if (ret) memcpy( ret, data, size );
1021 return ret;
1025 /**************************************************************************
1026 * import_selection
1028 * Import the specified format from the selection and return a global handle to the data.
1030 static HANDLE import_selection( Display *display, Window win, Atom selection,
1031 struct clipboard_format *format )
1033 unsigned char *data;
1034 unsigned long size;
1035 Atom type;
1036 HANDLE ret;
1038 if (!format->import) return 0;
1040 if (!convert_selection( display, win, selection, format, &type, &data, &size ))
1042 TRACE( "failed to convert selection\n" );
1043 return 0;
1045 ret = format->import( type, data, size );
1046 HeapFree( GetProcessHeap(), 0, data );
1047 return ret;
1051 /**************************************************************************
1052 * X11DRV_CLIPBOARD_ImportSelection
1054 * Import the X selection into the clipboard format registered for the given X target.
1056 void X11DRV_CLIPBOARD_ImportSelection( Display *display, Window win, Atom selection,
1057 Atom *targets, UINT count,
1058 void (*callback)( Atom, UINT, HANDLE ))
1060 UINT i;
1061 HANDLE handle;
1062 struct clipboard_format *format;
1064 register_x11_formats( targets, count );
1066 for (i = 0; i < count; i++)
1068 if (!(format = find_x11_format( targets[i] ))) continue;
1069 if (!format->id) continue;
1070 if (!(handle = import_selection( display, win, selection, format ))) continue;
1071 callback( targets[i], format->id, handle );
1076 /**************************************************************************
1077 * render_format
1079 static HANDLE render_format( UINT id )
1081 Display *display = thread_display();
1082 unsigned int i;
1083 HANDLE handle = 0;
1085 if (!current_selection) return 0;
1087 for (i = 0; i < nb_current_x11_formats; i++)
1089 if (current_x11_formats[i]->id != id) continue;
1090 if (!(handle = import_selection( display, import_window,
1091 current_selection, current_x11_formats[i] ))) continue;
1092 SetClipboardData( id, handle );
1093 break;
1095 return handle;
1099 /**************************************************************************
1100 * export_data
1102 * Generic export clipboard data routine.
1104 static BOOL export_data( Display *display, Window win, Atom prop, Atom target, HANDLE handle )
1106 void *ptr = GlobalLock( handle );
1108 if (!ptr) return FALSE;
1109 put_property( display, win, prop, target, 8, ptr, GlobalSize( handle ));
1110 GlobalUnlock( handle );
1111 return TRUE;
1115 /**************************************************************************
1116 * string_from_unicode_text
1118 * Convert CF_UNICODETEXT data to a string in the specified codepage.
1120 static char *string_from_unicode_text( UINT codepage, HANDLE handle, UINT *size )
1122 UINT i, j;
1123 char *str;
1124 WCHAR *strW = GlobalLock( handle );
1125 UINT lenW = GlobalSize( handle ) / sizeof(WCHAR);
1126 DWORD len = WideCharToMultiByte( codepage, 0, strW, lenW, NULL, 0, NULL, NULL );
1128 if ((str = HeapAlloc( GetProcessHeap(), 0, len )))
1130 WideCharToMultiByte( codepage, 0, strW, lenW, str, len, NULL, NULL);
1131 GlobalUnlock( handle );
1133 /* remove carriage returns */
1134 for (i = j = 0; i < len; i++)
1136 if (str[i] == '\r' && (i == len - 1 || str[i + 1] == '\n')) continue;
1137 str[j++] = str[i];
1139 while (j && !str[j - 1]) j--; /* remove trailing nulls */
1140 *size = j;
1141 TRACE( "returning %s\n", debugstr_an( str, j ));
1143 GlobalUnlock( handle );
1144 return str;
1148 /**************************************************************************
1149 * export_string
1151 * Export CF_UNICODETEXT converting the string to XA_STRING.
1153 static BOOL export_string( Display *display, Window win, Atom prop, Atom target, HANDLE handle )
1155 UINT size;
1156 char *text = string_from_unicode_text( 28591, handle, &size );
1158 if (!text) return FALSE;
1159 put_property( display, win, prop, target, 8, text, size );
1160 HeapFree( GetProcessHeap(), 0, text );
1161 GlobalUnlock( handle );
1162 return TRUE;
1166 /**************************************************************************
1167 * export_utf8_string
1169 * Export CF_UNICODE converting the string to UTF8.
1171 static BOOL export_utf8_string( Display *display, Window win, Atom prop, Atom target, HANDLE handle )
1173 UINT size;
1174 char *text = string_from_unicode_text( CP_UTF8, handle, &size );
1176 if (!text) return FALSE;
1177 put_property( display, win, prop, target, 8, text, size );
1178 HeapFree( GetProcessHeap(), 0, text );
1179 GlobalUnlock( handle );
1180 return TRUE;
1184 /**************************************************************************
1185 * export_text
1187 * Export CF_UNICODE to the polymorphic TEXT type, using UTF8.
1189 static BOOL export_text( Display *display, Window win, Atom prop, Atom target, HANDLE handle )
1191 return export_utf8_string( display, win, prop, x11drv_atom(UTF8_STRING), handle );
1195 /**************************************************************************
1196 * export_compound_text
1198 * Export CF_UNICODE to COMPOUND_TEXT
1200 static BOOL export_compound_text( Display *display, Window win, Atom prop, Atom target, HANDLE handle )
1202 XTextProperty textprop;
1203 XICCEncodingStyle style;
1204 UINT size;
1205 char *text = string_from_unicode_text( CP_UNIXCP, handle, &size );
1207 if (!text) return FALSE;
1208 if (target == x11drv_atom(COMPOUND_TEXT))
1209 style = XCompoundTextStyle;
1210 else
1211 style = XStdICCTextStyle;
1213 /* Update the X property */
1214 if (XmbTextListToTextProperty( display, &text, 1, style, &textprop ) == Success)
1216 XSetTextProperty( display, win, &textprop, prop );
1217 XFree( textprop.value );
1220 HeapFree( GetProcessHeap(), 0, text );
1221 return TRUE;
1225 /**************************************************************************
1226 * export_pixmap
1228 * Export CF_DIB to XA_PIXMAP.
1230 static BOOL export_pixmap( Display *display, Window win, Atom prop, Atom target, HANDLE handle )
1232 Pixmap pixmap;
1233 BITMAPINFO *pbmi;
1234 struct gdi_image_bits bits;
1236 pbmi = GlobalLock( handle );
1237 bits.ptr = (LPBYTE)pbmi + bitmap_info_size( pbmi, DIB_RGB_COLORS );
1238 bits.free = NULL;
1239 bits.is_copy = FALSE;
1240 pixmap = create_pixmap_from_image( 0, &default_visual, pbmi, &bits, DIB_RGB_COLORS );
1241 GlobalUnlock( handle );
1243 put_property( display, win, prop, target, 32, &pixmap, 1 );
1244 /* FIXME: free the pixmap when the property is deleted */
1245 return TRUE;
1249 /**************************************************************************
1250 * export_image_bmp
1252 * Export CF_DIB to image/bmp.
1254 static BOOL export_image_bmp( Display *display, Window win, Atom prop, Atom target, HANDLE handle )
1256 LPBYTE dibdata = GlobalLock( handle );
1257 UINT bmpsize;
1258 BITMAPFILEHEADER *bfh;
1260 bmpsize = sizeof(BITMAPFILEHEADER) + GlobalSize( handle );
1261 bfh = HeapAlloc( GetProcessHeap(), 0, bmpsize );
1262 if (bfh)
1264 /* bitmap file header */
1265 bfh->bfType = 0x4d42; /* "BM" */
1266 bfh->bfSize = bmpsize;
1267 bfh->bfReserved1 = 0;
1268 bfh->bfReserved2 = 0;
1269 bfh->bfOffBits = sizeof(BITMAPFILEHEADER) + bitmap_info_size((BITMAPINFO*)dibdata, DIB_RGB_COLORS);
1271 /* rest of bitmap is the same as the packed dib */
1272 memcpy(bfh+1, dibdata, bmpsize-sizeof(BITMAPFILEHEADER));
1274 GlobalUnlock( handle );
1275 put_property( display, win, prop, target, 8, bfh, bmpsize );
1276 HeapFree( GetProcessHeap(), 0, bfh );
1277 return TRUE;
1281 /**************************************************************************
1282 * export_enhmetafile
1284 * Export EnhMetaFile.
1286 static BOOL export_enhmetafile( Display *display, Window win, Atom prop, Atom target, HANDLE handle )
1288 unsigned int size;
1289 void *ptr;
1291 if (!(size = GetEnhMetaFileBits( handle, 0, NULL ))) return FALSE;
1292 if (!(ptr = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1294 GetEnhMetaFileBits( handle, size, ptr );
1295 put_property( display, win, prop, target, 8, ptr, size );
1296 HeapFree( GetProcessHeap(), 0, ptr );
1297 return TRUE;
1301 /**************************************************************************
1302 * export_text_html
1304 * Export HTML Format to text/html.
1306 * FIXME: We should attempt to add an <a base> tag and convert windows paths.
1308 static BOOL export_text_html( Display *display, Window win, Atom prop, Atom target, HANDLE handle )
1310 const char *p, *data;
1311 UINT start = 0, end = 0;
1312 BOOL ret = TRUE;
1314 if (!(data = GlobalLock( handle ))) return FALSE;
1316 p = data;
1317 while (*p && *p != '<')
1319 if (!strncmp( p, "StartFragment:", 14 )) start = atoi( p + 14 );
1320 else if (!strncmp( p, "EndFragment:", 12 )) end = atoi( p + 12 );
1321 if (!(p = strpbrk( p, "\r\n" ))) break;
1322 while (*p == '\r' || *p == '\n') p++;
1324 if (start && start < end && end <= GlobalSize( handle ))
1325 put_property( display, win, prop, target, 8, data + start, end - start );
1326 else
1327 ret = FALSE;
1329 GlobalUnlock( handle );
1330 return ret;
1334 /**************************************************************************
1335 * export_hdrop
1337 * Export CF_HDROP format to text/uri-list.
1339 static BOOL export_hdrop( Display *display, Window win, Atom prop, Atom target, HANDLE handle )
1341 UINT i;
1342 UINT numFiles;
1343 char *textUriList;
1344 UINT textUriListSize = 32;
1345 UINT next = 0;
1347 textUriList = HeapAlloc( GetProcessHeap(), 0, textUriListSize );
1348 if (!textUriList) return FALSE;
1349 numFiles = DragQueryFileW( handle, 0xFFFFFFFF, NULL, 0 );
1350 for (i = 0; i < numFiles; i++)
1352 UINT dosFilenameSize;
1353 WCHAR *dosFilename = NULL;
1354 char *unixFilename = NULL;
1355 UINT uriSize;
1356 UINT u;
1358 dosFilenameSize = 1 + DragQueryFileW( handle, i, NULL, 0 );
1359 dosFilename = HeapAlloc(GetProcessHeap(), 0, dosFilenameSize*sizeof(WCHAR));
1360 if (dosFilename == NULL) goto failed;
1361 DragQueryFileW( handle, i, dosFilename, dosFilenameSize );
1362 unixFilename = wine_get_unix_file_name(dosFilename);
1363 HeapFree(GetProcessHeap(), 0, dosFilename);
1364 if (unixFilename == NULL) goto failed;
1365 uriSize = 8 + /* file:/// */
1366 3 * (lstrlenA(unixFilename) - 1) + /* "%xy" per char except first '/' */
1367 2; /* \r\n */
1368 if ((next + uriSize) > textUriListSize)
1370 UINT biggerSize = max( 2 * textUriListSize, next + uriSize );
1371 void *bigger = HeapReAlloc( GetProcessHeap(), 0, textUriList, biggerSize );
1372 if (bigger)
1374 textUriList = bigger;
1375 textUriListSize = biggerSize;
1377 else
1379 HeapFree(GetProcessHeap(), 0, unixFilename);
1380 goto failed;
1383 lstrcpyA(&textUriList[next], "file:///");
1384 next += 8;
1385 /* URL encode everything - unnecessary, but easier/lighter than linking in shlwapi, and can't hurt */
1386 for (u = 1; unixFilename[u]; u++)
1388 static const char hex_table[] = "0123456789abcdef";
1389 textUriList[next++] = '%';
1390 textUriList[next++] = hex_table[unixFilename[u] >> 4];
1391 textUriList[next++] = hex_table[unixFilename[u] & 0xf];
1393 textUriList[next++] = '\r';
1394 textUriList[next++] = '\n';
1395 HeapFree(GetProcessHeap(), 0, unixFilename);
1397 put_property( display, win, prop, target, 8, textUriList, next );
1398 HeapFree( GetProcessHeap(), 0, textUriList );
1399 return TRUE;
1401 failed:
1402 HeapFree( GetProcessHeap(), 0, textUriList );
1403 return FALSE;
1407 /***********************************************************************
1408 * get_clipboard_formats
1410 * Return a list of all formats currently available on the Win32 clipboard.
1411 * Helper for export_targets.
1413 static UINT *get_clipboard_formats( UINT *size )
1415 UINT *ids;
1417 *size = 256;
1418 for (;;)
1420 if (!(ids = HeapAlloc( GetProcessHeap(), 0, *size * sizeof(*ids) ))) return NULL;
1421 if (GetUpdatedClipboardFormats( ids, *size, size )) break;
1422 HeapFree( GetProcessHeap(), 0, ids );
1423 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return NULL;
1425 register_win32_formats( ids, *size );
1426 return ids;
1430 /***********************************************************************
1431 * is_format_available
1433 * Check if a clipboard format is included in the list.
1434 * Helper for export_targets.
1436 static BOOL is_format_available( UINT format, const UINT *ids, unsigned int count )
1438 while (count--) if (*ids++ == format) return TRUE;
1439 return FALSE;
1443 /***********************************************************************
1444 * export_targets
1446 * Service a TARGETS selection request event
1448 static BOOL export_targets( Display *display, Window win, Atom prop, Atom target, HANDLE handle )
1450 struct clipboard_format *format;
1451 UINT pos, count, *formats;
1452 Atom *targets;
1454 if (!(formats = get_clipboard_formats( &count ))) return FALSE;
1456 /* the builtin formats contain duplicates, so allocate some extra space */
1457 if (!(targets = HeapAlloc( GetProcessHeap(), 0, (count + NB_BUILTIN_FORMATS) * sizeof(*targets) )))
1459 HeapFree( GetProcessHeap(), 0, formats );
1460 return FALSE;
1463 pos = 0;
1464 LIST_FOR_EACH_ENTRY( format, &format_list, struct clipboard_format, entry )
1466 if (!format->export) continue;
1467 /* formats with id==0 are always exported */
1468 if (format->id && !is_format_available( format->id, formats, count )) continue;
1469 TRACE( "%d: %s -> %s\n", pos, debugstr_format( format->id ), debugstr_xatom( format->atom ));
1470 targets[pos++] = format->atom;
1473 put_property( display, win, prop, XA_ATOM, 32, targets, pos );
1474 HeapFree( GetProcessHeap(), 0, targets );
1475 HeapFree( GetProcessHeap(), 0, formats );
1476 return TRUE;
1480 /**************************************************************************
1481 * export_selection
1483 * Export selection data, depending on the target type.
1485 static BOOL export_selection( Display *display, Window win, Atom prop, Atom target )
1487 struct clipboard_format *format;
1488 HANDLE handle = 0;
1489 BOOL open = FALSE, ret = FALSE;
1491 LIST_FOR_EACH_ENTRY( format, &format_list, struct clipboard_format, entry )
1493 if (format->atom != target) continue;
1494 if (!format->export) continue;
1495 if (!format->id)
1497 TRACE( "win %lx prop %s target %s\n", win, debugstr_xatom( prop ), debugstr_xatom( target ));
1498 ret = format->export( display, win, prop, target, 0 );
1499 break;
1501 if (!open && !(open = OpenClipboard( clipboard_hwnd )))
1503 ERR( "failed to open clipboard for %s\n", debugstr_xatom( target ));
1504 return FALSE;
1506 if ((handle = GetClipboardData( format->id )))
1508 TRACE( "win %lx prop %s target %s exporting %s %p\n",
1509 win, debugstr_xatom( prop ), debugstr_xatom( target ),
1510 debugstr_format( format->id ), handle );
1512 ret = format->export( display, win, prop, target, handle );
1513 break;
1515 /* keep looking for another Win32 format mapping to the same target */
1517 if (open) CloseClipboard();
1518 return ret;
1522 /***********************************************************************
1523 * export_multiple
1525 * Service a MULTIPLE selection request event
1526 * prop contains a list of (target,property) atom pairs.
1527 * The first atom names a target and the second names a property.
1528 * The effect is as if we have received a sequence of SelectionRequest events
1529 * (one for each atom pair) except that:
1530 * 1. We reply with a SelectionNotify only when all the requested conversions
1531 * have been performed.
1532 * 2. If we fail to convert the target named by an atom in the MULTIPLE property,
1533 * we replace the atom in the property by None.
1535 static BOOL export_multiple( Display *display, Window win, Atom prop, Atom target, HANDLE handle )
1537 Atom atype;
1538 int aformat;
1539 Atom *list;
1540 unsigned long i, count, failed, remain;
1542 /* Read the MULTIPLE property contents. This should contain a list of
1543 * (target,property) atom pairs.
1545 if (XGetWindowProperty( display, win, prop, 0, 0x3FFF, False, AnyPropertyType, &atype, &aformat,
1546 &count, &remain, (unsigned char**)&list ))
1547 return FALSE;
1549 TRACE( "type %s format %d count %ld remain %ld\n",
1550 debugstr_xatom( atype ), aformat, count, remain );
1553 * Make sure we got what we expect.
1554 * NOTE: According to the X-ICCCM Version 2.0 documentation the property sent
1555 * in a MULTIPLE selection request should be of type ATOM_PAIR.
1556 * However some X apps(such as XPaint) are not compliant with this and return
1557 * a user defined atom in atype when XGetWindowProperty is called.
1558 * The data *is* an atom pair but is not denoted as such.
1560 if (aformat == 32 /* atype == xAtomPair */ )
1562 for (i = failed = 0; i < count; i += 2)
1564 if (list[i+1] == None) continue;
1565 if (export_selection( display, win, list[i + 1], list[i] )) continue;
1566 failed++;
1567 list[i + 1] = None;
1569 if (failed) put_property( display, win, prop, atype, 32, list, count );
1571 XFree( list );
1572 return TRUE;
1576 /***********************************************************************
1577 * export_timestamp
1579 * Export the timestamp that was used to acquire the selection
1581 static BOOL export_timestamp( Display *display, Window win, Atom prop, Atom target, HANDLE handle )
1583 Time time = CurrentTime; /* FIXME */
1584 put_property( display, win, prop, XA_INTEGER, 32, &time, 1 );
1585 return TRUE;
1589 /**************************************************************************
1590 * X11DRV_CLIPBOARD_GetProperty
1591 * Gets type, data and size.
1593 static BOOL X11DRV_CLIPBOARD_GetProperty(Display *display, Window w, Atom prop,
1594 Atom *atype, unsigned char** data, unsigned long* datasize)
1596 int aformat;
1597 unsigned long pos = 0, nitems, remain, count;
1598 unsigned char *val = NULL, *buffer;
1600 for (;;)
1602 if (XGetWindowProperty(display, w, prop, pos, INT_MAX / 4, False,
1603 AnyPropertyType, atype, &aformat, &nitems, &remain, &buffer))
1605 WARN("Failed to read property\n");
1606 HeapFree( GetProcessHeap(), 0, val );
1607 return FALSE;
1610 count = get_property_size( aformat, nitems );
1611 if (!val) *data = HeapAlloc( GetProcessHeap(), 0, pos * sizeof(int) + count + 1 );
1612 else *data = HeapReAlloc( GetProcessHeap(), 0, val, pos * sizeof(int) + count + 1 );
1614 if (!*data)
1616 XFree( buffer );
1617 HeapFree( GetProcessHeap(), 0, val );
1618 return FALSE;
1620 val = *data;
1621 memcpy( (int *)val + pos, buffer, count );
1622 XFree( buffer );
1623 if (!remain)
1625 *datasize = pos * sizeof(int) + count;
1626 val[*datasize] = 0;
1627 break;
1629 pos += count / sizeof(int);
1632 TRACE( "got property %s type %s format %u len %lu from window %lx\n",
1633 debugstr_xatom( prop ), debugstr_xatom( *atype ), aformat, *datasize, w );
1635 /* Delete the property on the window now that we are done
1636 * This will send a PropertyNotify event to the selection owner. */
1637 XDeleteProperty(display, w, prop);
1638 return TRUE;
1642 struct clipboard_data_packet {
1643 struct list entry;
1644 unsigned long size;
1645 unsigned char *data;
1648 /**************************************************************************
1649 * read_property
1651 * Reads the contents of the X selection property.
1653 static BOOL read_property( Display *display, Window w, Atom prop,
1654 Atom *type, unsigned char **data, unsigned long *datasize )
1656 XEvent xe;
1658 if (prop == None)
1659 return FALSE;
1661 while (XCheckTypedWindowEvent(display, w, PropertyNotify, &xe))
1664 if (!X11DRV_CLIPBOARD_GetProperty(display, w, prop, type, data, datasize))
1665 return FALSE;
1667 if (*type == x11drv_atom(INCR))
1669 unsigned char *buf;
1670 unsigned long bufsize = 0;
1671 struct list packets;
1672 struct clipboard_data_packet *packet, *packet2;
1673 BOOL res;
1675 HeapFree(GetProcessHeap(), 0, *data);
1676 *data = NULL;
1678 list_init(&packets);
1680 for (;;)
1682 int i;
1683 unsigned char *prop_data;
1684 unsigned long prop_size;
1686 /* Wait until PropertyNotify is received */
1687 for (i = 0; i < SELECTION_RETRIES; i++)
1689 Bool res;
1691 res = XCheckTypedWindowEvent(display, w, PropertyNotify, &xe);
1692 if (res && xe.xproperty.atom == prop &&
1693 xe.xproperty.state == PropertyNewValue)
1694 break;
1695 usleep(SELECTION_WAIT);
1698 if (i >= SELECTION_RETRIES ||
1699 !X11DRV_CLIPBOARD_GetProperty(display, w, prop, type, &prop_data, &prop_size))
1701 res = FALSE;
1702 break;
1705 /* Retrieved entire data. */
1706 if (prop_size == 0)
1708 HeapFree(GetProcessHeap(), 0, prop_data);
1709 res = TRUE;
1710 break;
1713 packet = HeapAlloc(GetProcessHeap(), 0, sizeof(*packet));
1714 if (!packet)
1716 HeapFree(GetProcessHeap(), 0, prop_data);
1717 res = FALSE;
1718 break;
1721 packet->size = prop_size;
1722 packet->data = prop_data;
1723 list_add_tail(&packets, &packet->entry);
1724 bufsize += prop_size;
1727 if (res)
1729 buf = HeapAlloc(GetProcessHeap(), 0, bufsize + 1);
1730 if (buf)
1732 unsigned long bytes_copied = 0;
1733 *datasize = bufsize;
1734 LIST_FOR_EACH_ENTRY( packet, &packets, struct clipboard_data_packet, entry)
1736 memcpy(&buf[bytes_copied], packet->data, packet->size);
1737 bytes_copied += packet->size;
1739 buf[bufsize] = 0;
1740 *data = buf;
1742 else
1743 res = FALSE;
1746 LIST_FOR_EACH_ENTRY_SAFE( packet, packet2, &packets, struct clipboard_data_packet, entry)
1748 HeapFree(GetProcessHeap(), 0, packet->data);
1749 HeapFree(GetProcessHeap(), 0, packet);
1752 return res;
1755 return TRUE;
1759 /**************************************************************************
1760 * acquire_selection
1762 * Acquire the X11 selection when the Win32 clipboard has changed.
1764 static void acquire_selection( Display *display )
1766 if (selection_window) XDestroyWindow( display, selection_window );
1768 selection_window = XCreateWindow( display, root_window, 0, 0, 1, 1, 0, CopyFromParent,
1769 InputOutput, CopyFromParent, 0, NULL );
1770 if (!selection_window) return;
1772 XSetSelectionOwner( display, x11drv_atom(CLIPBOARD), selection_window, CurrentTime );
1773 if (use_primary_selection) XSetSelectionOwner( display, XA_PRIMARY, selection_window, CurrentTime );
1774 TRACE( "win %lx\n", selection_window );
1778 /**************************************************************************
1779 * release_selection
1781 * Release the X11 selection when some other X11 app has grabbed it.
1783 static void release_selection( Display *display, Time time )
1785 assert( selection_window );
1787 TRACE( "win %lx\n", selection_window );
1789 /* release PRIMARY if we still own it */
1790 if (use_primary_selection && XGetSelectionOwner( display, XA_PRIMARY ) == selection_window)
1791 XSetSelectionOwner( display, XA_PRIMARY, None, time );
1793 XDestroyWindow( display, selection_window );
1794 selection_window = 0;
1798 /**************************************************************************
1799 * request_selection_contents
1801 * Retrieve the contents of the X11 selection when it's owned by an X11 app.
1803 static BOOL request_selection_contents( Display *display, BOOL changed )
1805 struct clipboard_format *targets = find_x11_format( x11drv_atom(TARGETS) );
1806 struct clipboard_format *string = find_x11_format( XA_STRING );
1807 struct clipboard_format *format = NULL;
1808 Window owner = 0;
1809 unsigned char *data = NULL;
1810 unsigned long size = 0;
1811 Atom type = 0;
1813 static Atom last_selection;
1814 static Window last_owner;
1815 static struct clipboard_format *last_format;
1816 static Atom last_type;
1817 static unsigned char *last_data;
1818 static unsigned long last_size;
1820 assert( targets );
1821 assert( string );
1823 current_selection = 0;
1824 if (use_primary_selection)
1826 if ((owner = XGetSelectionOwner( display, XA_PRIMARY )))
1827 current_selection = XA_PRIMARY;
1829 if (!current_selection)
1831 if ((owner = XGetSelectionOwner( display, x11drv_atom(CLIPBOARD) )))
1832 current_selection = x11drv_atom(CLIPBOARD);
1835 if (current_selection)
1837 if (convert_selection( display, import_window, current_selection, targets, &type, &data, &size ))
1838 format = targets;
1839 else if (convert_selection( display, import_window, current_selection, string, &type, &data, &size ))
1840 format = string;
1843 changed = (changed ||
1844 rendered_formats ||
1845 last_selection != current_selection ||
1846 last_owner != owner ||
1847 last_format != format ||
1848 last_type != type ||
1849 last_size != size ||
1850 memcmp( last_data, data, size ));
1852 if (!changed)
1854 HeapFree( GetProcessHeap(), 0, data );
1855 return FALSE;
1858 if (!OpenClipboard( clipboard_hwnd )) return FALSE;
1859 TRACE( "selection changed, importing\n" );
1860 EmptyClipboard();
1861 is_clipboard_owner = TRUE;
1862 rendered_formats = 0;
1864 if (format) format->import( type, data, size );
1866 HeapFree( GetProcessHeap(), 0, last_data );
1867 last_selection = current_selection;
1868 last_owner = owner;
1869 last_format = format;
1870 last_type = type;
1871 last_data = data;
1872 last_size = size;
1873 last_clipboard_update = GetTickCount64();
1874 CloseClipboard();
1875 if (!use_xfixes)
1876 SetTimer( clipboard_hwnd, 1, SELECTION_UPDATE_DELAY, NULL );
1877 return TRUE;
1881 /**************************************************************************
1882 * update_clipboard
1884 * Periodically update the clipboard while the selection is owned by an X11 app.
1886 BOOL update_clipboard( HWND hwnd )
1888 if (use_xfixes) return TRUE;
1889 if (hwnd != clipboard_hwnd) return TRUE;
1890 if (!is_clipboard_owner) return TRUE;
1891 if (GetTickCount64() - last_clipboard_update <= SELECTION_UPDATE_DELAY) return TRUE;
1892 return request_selection_contents( thread_display(), FALSE );
1896 /**************************************************************************
1897 * clipboard_wndproc
1899 * Window procedure for the clipboard manager.
1901 static LRESULT CALLBACK clipboard_wndproc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
1903 switch (msg)
1905 case WM_NCCREATE:
1906 return TRUE;
1907 case WM_CLIPBOARDUPDATE:
1908 if (is_clipboard_owner) break; /* ignore our own changes */
1909 acquire_selection( thread_init_display() );
1910 break;
1911 case WM_RENDERFORMAT:
1912 if (render_format( wp )) rendered_formats++;
1913 break;
1914 case WM_TIMER:
1915 if (!is_clipboard_owner) break;
1916 request_selection_contents( thread_display(), FALSE );
1917 break;
1918 case WM_DESTROYCLIPBOARD:
1919 TRACE( "WM_DESTROYCLIPBOARD: lost ownership\n" );
1920 is_clipboard_owner = FALSE;
1921 KillTimer( hwnd, 1 );
1922 break;
1924 return DefWindowProcW( hwnd, msg, wp, lp );
1928 /**************************************************************************
1929 * wait_clipboard_mutex
1931 * Make sure that there's only one clipboard thread per window station.
1933 static BOOL wait_clipboard_mutex(void)
1935 static const WCHAR prefix[] = {'_','_','w','i','n','e','_','c','l','i','p','b','o','a','r','d','_'};
1936 WCHAR buffer[MAX_PATH + sizeof(prefix) / sizeof(WCHAR)];
1937 HANDLE mutex;
1939 memcpy( buffer, prefix, sizeof(prefix) );
1940 if (!GetUserObjectInformationW( GetProcessWindowStation(), UOI_NAME,
1941 buffer + sizeof(prefix) / sizeof(WCHAR),
1942 sizeof(buffer) - sizeof(prefix), NULL ))
1944 ERR( "failed to get winstation name\n" );
1945 return FALSE;
1947 mutex = CreateMutexW( NULL, TRUE, buffer );
1948 if (GetLastError() == ERROR_ALREADY_EXISTS)
1950 TRACE( "waiting for mutex %s\n", debugstr_w( buffer ));
1951 WaitForSingleObject( mutex, INFINITE );
1953 return TRUE;
1957 /**************************************************************************
1958 * selection_notify_event
1960 * Called when x11 clipboard content changes
1962 #ifdef SONAME_LIBXFIXES
1963 static BOOL selection_notify_event( HWND hwnd, XEvent *event )
1965 XFixesSelectionNotifyEvent *req = (XFixesSelectionNotifyEvent*)event;
1967 if (!is_clipboard_owner) return FALSE;
1968 if (req->owner == selection_window) return FALSE;
1969 request_selection_contents( req->display, TRUE );
1970 return FALSE;
1972 #endif
1974 /**************************************************************************
1975 * xfixes_init
1977 * Initialize xfixes to receive clipboard update notifications
1979 static void xfixes_init(void)
1981 #ifdef SONAME_LIBXFIXES
1982 typeof(XFixesSelectSelectionInput) *pXFixesSelectSelectionInput;
1983 typeof(XFixesQueryExtension) *pXFixesQueryExtension;
1984 typeof(XFixesQueryVersion) *pXFixesQueryVersion;
1986 int event_base, error_base;
1987 int major = 3, minor = 0;
1988 void *handle;
1990 handle = wine_dlopen(SONAME_LIBXFIXES, RTLD_NOW, NULL, 0);
1991 if (!handle) return;
1993 pXFixesQueryExtension = wine_dlsym(handle, "XFixesQueryExtension", NULL, 0);
1994 if (!pXFixesQueryExtension) return;
1995 pXFixesQueryVersion = wine_dlsym(handle, "XFixesQueryVersion", NULL, 0);
1996 if (!pXFixesQueryVersion) return;
1997 pXFixesSelectSelectionInput = wine_dlsym(handle, "XFixesSelectSelectionInput", NULL, 0);
1998 if (!pXFixesSelectSelectionInput) return;
2000 if (!pXFixesQueryExtension(clipboard_display, &event_base, &error_base))
2001 return;
2002 pXFixesQueryVersion(clipboard_display, &major, &minor);
2003 use_xfixes = (major >= 1);
2004 if (!use_xfixes) return;
2006 pXFixesSelectSelectionInput(clipboard_display, import_window, x11drv_atom(CLIPBOARD),
2007 XFixesSetSelectionOwnerNotifyMask |
2008 XFixesSelectionWindowDestroyNotifyMask |
2009 XFixesSelectionClientCloseNotifyMask);
2010 if (use_primary_selection)
2012 pXFixesSelectSelectionInput(clipboard_display, import_window, XA_PRIMARY,
2013 XFixesSetSelectionOwnerNotifyMask |
2014 XFixesSelectionWindowDestroyNotifyMask |
2015 XFixesSelectionClientCloseNotifyMask);
2017 X11DRV_register_event_handler(event_base + XFixesSelectionNotify,
2018 selection_notify_event, "XFixesSelectionNotify");
2019 TRACE("xfixes succesully initialized\n");
2020 #else
2021 WARN("xfixes not supported\n");
2022 #endif
2026 /**************************************************************************
2027 * clipboard_thread
2029 * Thread running inside the desktop process to manage the clipboard
2031 static DWORD WINAPI clipboard_thread( void *arg )
2033 static const WCHAR clipboard_classname[] = {'_','_','w','i','n','e','_','c','l','i','p','b','o','a','r','d','_','m','a','n','a','g','e','r',0};
2034 XSetWindowAttributes attr;
2035 WNDCLASSW class;
2036 MSG msg;
2038 if (!wait_clipboard_mutex()) return 0;
2040 clipboard_display = thread_init_display();
2041 attr.event_mask = PropertyChangeMask;
2042 import_window = XCreateWindow( clipboard_display, root_window, 0, 0, 1, 1, 0, CopyFromParent,
2043 InputOutput, CopyFromParent, CWEventMask, &attr );
2044 if (!import_window)
2046 ERR( "failed to create import window\n" );
2047 return 0;
2050 memset( &class, 0, sizeof(class) );
2051 class.lpfnWndProc = clipboard_wndproc;
2052 class.lpszClassName = clipboard_classname;
2054 if (!RegisterClassW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
2056 ERR( "could not register clipboard window class err %u\n", GetLastError() );
2057 return 0;
2059 if (!(clipboard_hwnd = CreateWindowW( clipboard_classname, NULL, 0, 0, 0, 0, 0,
2060 HWND_MESSAGE, 0, 0, NULL )))
2062 ERR( "failed to create clipboard window err %u\n", GetLastError() );
2063 return 0;
2066 clipboard_thread_id = GetCurrentThreadId();
2067 AddClipboardFormatListener( clipboard_hwnd );
2068 register_builtin_formats();
2069 request_selection_contents( clipboard_display, TRUE );
2071 xfixes_init();
2073 TRACE( "clipboard thread %04x running\n", GetCurrentThreadId() );
2074 while (GetMessageW( &msg, 0, 0, 0 )) DispatchMessageW( &msg );
2075 return 0;
2079 /**************************************************************************
2080 * X11DRV_UpdateClipboard
2082 void CDECL X11DRV_UpdateClipboard(void)
2084 static ULONG last_update;
2085 ULONG now;
2086 DWORD_PTR ret;
2088 if (use_xfixes) return;
2089 if (GetCurrentThreadId() == clipboard_thread_id) return;
2090 now = GetTickCount();
2091 if ((int)(now - last_update) <= SELECTION_UPDATE_DELAY) return;
2092 if (SendMessageTimeoutW( GetClipboardOwner(), WM_X11DRV_UPDATE_CLIPBOARD, 0, 0,
2093 SMTO_ABORTIFHUNG, 5000, &ret ) && ret)
2094 last_update = now;
2098 /***********************************************************************
2099 * X11DRV_HandleSelectionRequest
2101 BOOL X11DRV_SelectionRequest( HWND hwnd, XEvent *xev )
2103 XSelectionRequestEvent *event = &xev->xselectionrequest;
2104 Display *display = event->display;
2105 XEvent result;
2106 Atom rprop = None;
2108 TRACE( "got request on %lx for selection %s target %s win %lx prop %s\n",
2109 event->owner, debugstr_xatom( event->selection ), debugstr_xatom( event->target ),
2110 event->requestor, debugstr_xatom( event->property ));
2112 if (event->owner != selection_window) goto done;
2113 if ((event->selection != x11drv_atom(CLIPBOARD)) &&
2114 (!use_primary_selection || event->selection != XA_PRIMARY)) goto done;
2116 /* If the specified property is None the requestor is an obsolete client.
2117 * We support these by using the specified target atom as the reply property.
2119 rprop = event->property;
2120 if( rprop == None )
2121 rprop = event->target;
2123 if (!export_selection( display, event->requestor, rprop, event->target ))
2124 rprop = None; /* report failure to client */
2126 done:
2127 result.xselection.type = SelectionNotify;
2128 result.xselection.display = display;
2129 result.xselection.requestor = event->requestor;
2130 result.xselection.selection = event->selection;
2131 result.xselection.property = rprop;
2132 result.xselection.target = event->target;
2133 result.xselection.time = event->time;
2134 TRACE( "sending SelectionNotify for %s to %lx\n", debugstr_xatom( rprop ), event->requestor );
2135 XSendEvent( display, event->requestor, False, NoEventMask, &result );
2136 return FALSE;
2140 /***********************************************************************
2141 * X11DRV_SelectionClear
2143 BOOL X11DRV_SelectionClear( HWND hwnd, XEvent *xev )
2145 XSelectionClearEvent *event = &xev->xselectionclear;
2147 if (event->window != selection_window) return FALSE;
2148 if (event->selection != x11drv_atom(CLIPBOARD)) return FALSE;
2150 release_selection( event->display, event->time );
2151 request_selection_contents( event->display, TRUE );
2152 return FALSE;
2156 /**************************************************************************
2157 * X11DRV_InitClipboard
2159 void X11DRV_InitClipboard(void)
2161 DWORD id;
2162 HANDLE handle = CreateThread( NULL, 0, clipboard_thread, NULL, 0, &id );
2164 if (handle) CloseHandle( handle );
2165 else ERR( "failed to create clipboard thread\n" );