2 * X11 clipboard windows driver
4 * Copyright 1994 Martin Ayotte
7 * 2003 Ulrich Czekalla for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * This file contains the X specific implementation for the windows
27 * Wine's internal clipboard is exposed to external apps via the X
28 * selection mechanism.
29 * Currently the driver asserts ownership via two selection atoms:
30 * 1. PRIMARY(XA_PRIMARY)
33 * In our implementation, the CLIPBOARD selection takes precedence over PRIMARY,
34 * i.e. if a CLIPBOARD selection is available, it is used instead of PRIMARY.
35 * When Wine takes ownership of the clipboard, it takes ownership of BOTH selections.
36 * While giving up selection ownership, if the CLIPBOARD selection is lost,
37 * it will lose both PRIMARY and CLIPBOARD and empty the clipboard.
38 * However if only PRIMARY is lost, it will continue to hold the CLIPBOARD selection
39 * (leaving the clipboard cache content unaffected).
41 * Every format exposed via a windows clipboard format is also exposed through
42 * a corresponding X selection target. A selection target atom is synthesized
43 * whenever a new Windows clipboard format is registered via RegisterClipboardFormat,
44 * or when a built-in format is used for the first time.
45 * Windows native format are exposed by prefixing the format name with "<WCF>"
46 * This allows us to uniquely identify windows native formats exposed by other
49 * In order to allow external applications to query WINE for supported formats,
50 * we respond to the "TARGETS" selection target. (See EVENT_SelectionRequest
51 * for implementation) We use the same mechanism to query external clients for
52 * availability of a particular format, by caching the list of available targets
53 * by using the clipboard cache's "delayed render" mechanism. If a selection client
54 * does not support the "TARGETS" selection target, we actually attempt to retrieve
55 * the format requested as a fallback mechanism.
57 * Certain Windows native formats are automatically converted to X native formats
58 * and vice versa. If a native format is available in the selection, it takes
59 * precedence, in order to avoid unnecessary conversions.
61 * FIXME: global format list needs a critical section
65 #include "wine/port.h"
82 #include "wine/list.h"
83 #include "wine/debug.h"
84 #include "wine/unicode.h"
85 #include "wine/server.h"
87 WINE_DEFAULT_DEBUG_CHANNEL(clipboard
);
89 /* Maximum wait time for selection notify */
90 #define SELECTION_RETRIES 500 /* wait for .5 seconds */
91 #define SELECTION_WAIT 1000 /* us */
94 #define S_NOSELECTION 0
105 } CLIPBOARDINFO
, *LPCLIPBOARDINFO
;
107 struct tagWINE_CLIPDATA
; /* Forward */
109 typedef HANDLE (*DRVEXPORTFUNC
)(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
110 struct tagWINE_CLIPDATA
* lpData
, LPDWORD lpBytes
);
111 typedef HANDLE (*DRVIMPORTFUNC
)(Display
*d
, Window w
, Atom prop
);
113 typedef struct tagWINE_CLIPFORMAT
{
117 DRVIMPORTFUNC lpDrvImportFunc
;
118 DRVEXPORTFUNC lpDrvExportFunc
;
119 } WINE_CLIPFORMAT
, *LPWINE_CLIPFORMAT
;
121 typedef struct tagWINE_CLIPDATA
{
127 LPWINE_CLIPFORMAT lpFormat
;
128 } WINE_CLIPDATA
, *LPWINE_CLIPDATA
;
130 #define CF_FLAG_UNOWNED 0x0001 /* cached data is not owned */
131 #define CF_FLAG_SYNTHESIZED 0x0002 /* Implicitly converted data */
133 static int selectionAcquired
= 0; /* Contains the current selection masks */
134 static Window selectionWindow
= None
; /* The top level X window which owns the selection */
135 static Atom selectionCacheSrc
= XA_PRIMARY
; /* The selection source from which the clipboard cache was filled */
137 void CDECL
X11DRV_EmptyClipboard(BOOL keepunowned
);
138 void CDECL
X11DRV_EndClipboardUpdate(void);
139 static HANDLE
X11DRV_CLIPBOARD_ImportClipboardData(Display
*d
, Window w
, Atom prop
);
140 static HANDLE
X11DRV_CLIPBOARD_ImportEnhMetaFile(Display
*d
, Window w
, Atom prop
);
141 static HANDLE
X11DRV_CLIPBOARD_ImportMetaFilePict(Display
*d
, Window w
, Atom prop
);
142 static HANDLE
X11DRV_CLIPBOARD_ImportXAPIXMAP(Display
*d
, Window w
, Atom prop
);
143 static HANDLE
X11DRV_CLIPBOARD_ImportImageBmp(Display
*d
, Window w
, Atom prop
);
144 static HANDLE
X11DRV_CLIPBOARD_ImportXAString(Display
*d
, Window w
, Atom prop
);
145 static HANDLE
X11DRV_CLIPBOARD_ImportUTF8(Display
*d
, Window w
, Atom prop
);
146 static HANDLE
X11DRV_CLIPBOARD_ImportCompoundText(Display
*d
, Window w
, Atom prop
);
147 static HANDLE
X11DRV_CLIPBOARD_ExportClipboardData(Display
*display
, Window requestor
, Atom aTarget
,
148 Atom rprop
, LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
);
149 static HANDLE
X11DRV_CLIPBOARD_ExportString(Display
*display
, Window requestor
, Atom aTarget
,
150 Atom rprop
, LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
);
151 static HANDLE
X11DRV_CLIPBOARD_ExportXAPIXMAP(Display
*display
, Window requestor
, Atom aTarget
,
152 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
);
153 static HANDLE
X11DRV_CLIPBOARD_ExportImageBmp(Display
*display
, Window requestor
, Atom aTarget
,
154 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
);
155 static HANDLE
X11DRV_CLIPBOARD_ExportMetaFilePict(Display
*display
, Window requestor
, Atom aTarget
,
156 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
);
157 static HANDLE
X11DRV_CLIPBOARD_ExportEnhMetaFile(Display
*display
, Window requestor
, Atom aTarget
,
158 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
);
159 static HANDLE
X11DRV_CLIPBOARD_ExportTextHtml(Display
*display
, Window requestor
, Atom aTarget
,
160 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
);
161 static WINE_CLIPFORMAT
*X11DRV_CLIPBOARD_InsertClipboardFormat(UINT id
, Atom prop
);
162 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedText(Display
*display
, UINT wFormatID
);
163 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData
);
164 static BOOL
X11DRV_CLIPBOARD_IsSelectionOwner(void);
165 static int X11DRV_CLIPBOARD_QueryAvailableData(Display
*display
, LPCLIPBOARDINFO lpcbinfo
);
166 static BOOL
X11DRV_CLIPBOARD_ReadSelectionData(Display
*display
, LPWINE_CLIPDATA lpData
);
167 static BOOL
X11DRV_CLIPBOARD_ReadProperty(Display
*display
, Window w
, Atom prop
,
168 unsigned char** data
, unsigned long* datasize
);
169 static BOOL
X11DRV_CLIPBOARD_RenderFormat(Display
*display
, LPWINE_CLIPDATA lpData
);
170 static HANDLE
X11DRV_CLIPBOARD_SerializeMetafile(INT wformat
, HANDLE hdata
, LPDWORD lpcbytes
, BOOL out
);
171 static BOOL
X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID
);
172 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedFormat(Display
*display
, LPWINE_CLIPDATA lpData
);
173 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedDIB(Display
*display
);
174 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedBitmap(Display
*display
);
175 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile(Display
*display
);
176 static void X11DRV_HandleSelectionRequest( HWND hWnd
, XSelectionRequestEvent
*event
, BOOL bIsMultiple
);
178 /* Clipboard formats */
184 DRVIMPORTFUNC import
;
185 DRVEXPORTFUNC export
;
186 } builtin_formats
[] =
188 { CF_TEXT
, XA_STRING
, X11DRV_CLIPBOARD_ImportXAString
, X11DRV_CLIPBOARD_ExportString
},
189 { CF_BITMAP
, XATOM_WCF_BITMAP
, X11DRV_CLIPBOARD_ImportClipboardData
, NULL
},
190 { CF_METAFILEPICT
, XATOM_WCF_METAFILEPICT
, X11DRV_CLIPBOARD_ImportMetaFilePict
, X11DRV_CLIPBOARD_ExportMetaFilePict
},
191 { CF_SYLK
, XATOM_WCF_SYLK
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
192 { CF_DIF
, XATOM_WCF_DIF
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
193 { CF_TIFF
, XATOM_WCF_TIFF
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
194 { CF_OEMTEXT
, XATOM_WCF_OEMTEXT
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
195 { CF_DIB
, XA_PIXMAP
, X11DRV_CLIPBOARD_ImportXAPIXMAP
, X11DRV_CLIPBOARD_ExportXAPIXMAP
},
196 { CF_PALETTE
, XATOM_WCF_PALETTE
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
197 { CF_PENDATA
, XATOM_WCF_PENDATA
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
198 { CF_RIFF
, XATOM_WCF_RIFF
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
199 { CF_WAVE
, XATOM_WCF_WAVE
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
200 { CF_UNICODETEXT
, XATOM_UTF8_STRING
, X11DRV_CLIPBOARD_ImportUTF8
, X11DRV_CLIPBOARD_ExportString
},
201 /* If UTF8_STRING is not available, attempt COMPOUND_TEXT */
202 { CF_UNICODETEXT
, XATOM_COMPOUND_TEXT
, X11DRV_CLIPBOARD_ImportCompoundText
, X11DRV_CLIPBOARD_ExportString
},
203 { CF_ENHMETAFILE
, XATOM_WCF_ENHMETAFILE
, X11DRV_CLIPBOARD_ImportEnhMetaFile
, X11DRV_CLIPBOARD_ExportEnhMetaFile
},
204 { CF_HDROP
, XATOM_WCF_HDROP
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
205 { CF_LOCALE
, XATOM_WCF_LOCALE
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
206 { CF_DIBV5
, XATOM_WCF_DIBV5
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
207 { CF_OWNERDISPLAY
, XATOM_WCF_OWNERDISPLAY
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
208 { CF_DSPTEXT
, XATOM_WCF_DSPTEXT
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
209 { CF_DSPBITMAP
, XATOM_WCF_DSPBITMAP
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
210 { CF_DSPMETAFILEPICT
, XATOM_WCF_DSPMETAFILEPICT
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
211 { CF_DSPENHMETAFILE
, XATOM_WCF_DSPENHMETAFILE
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
212 { CF_DIB
, XATOM_image_bmp
, X11DRV_CLIPBOARD_ImportImageBmp
, X11DRV_CLIPBOARD_ExportImageBmp
},
215 static struct list format_list
= LIST_INIT( format_list
);
217 #define GET_ATOM(prop) (((prop) < FIRST_XATOM) ? (Atom)(prop) : X11DRV_Atoms[(prop) - FIRST_XATOM])
219 /* Maps X properties to Windows formats */
220 static const WCHAR wszRichTextFormat
[] = {'R','i','c','h',' ','T','e','x','t',' ','F','o','r','m','a','t',0};
221 static const WCHAR wszGIF
[] = {'G','I','F',0};
222 static const WCHAR wszJFIF
[] = {'J','F','I','F',0};
223 static const WCHAR wszPNG
[] = {'P','N','G',0};
224 static const WCHAR wszHTMLFormat
[] = {'H','T','M','L',' ','F','o','r','m','a','t',0};
229 } PropertyFormatMap
[] =
231 { wszRichTextFormat
, XATOM_text_rtf
},
232 { wszRichTextFormat
, XATOM_text_richtext
},
233 { wszGIF
, XATOM_image_gif
},
234 { wszJFIF
, XATOM_image_jpeg
},
235 { wszPNG
, XATOM_image_png
},
236 { wszHTMLFormat
, XATOM_HTML_Format
}, /* prefer this to text/html */
241 * Cached clipboard data.
243 static struct list data_list
= LIST_INIT( data_list
);
244 static UINT ClipDataCount
= 0;
247 * Clipboard sequence number
249 static UINT wSeqNo
= 0;
251 /**************************************************************************
252 * Internal Clipboard implementation methods
253 **************************************************************************/
255 static Window
thread_selection_wnd(void)
257 struct x11drv_thread_data
*thread_data
= x11drv_init_thread_data();
258 Window w
= thread_data
->selection_wnd
;
262 w
= XCreateWindow(thread_data
->display
, root_window
, 0, 0, 1, 1, 0, CopyFromParent
,
263 InputOnly
, CopyFromParent
, 0, NULL
);
266 thread_data
->selection_wnd
= w
;
268 XSelectInput(thread_data
->display
, w
, PropertyChangeMask
);
271 FIXME("Failed to create window. Fetching selection data will fail.\n");
277 static const char *debugstr_format( UINT id
)
281 if (GetClipboardFormatNameW( id
, buffer
, 256 ))
282 return wine_dbg_sprintf( "%04x %s", id
, debugstr_w(buffer
) );
286 #define BUILTIN(id) case id: return #id;
289 BUILTIN(CF_METAFILEPICT
)
299 BUILTIN(CF_UNICODETEXT
)
300 BUILTIN(CF_ENHMETAFILE
)
304 BUILTIN(CF_OWNERDISPLAY
)
306 BUILTIN(CF_DSPBITMAP
)
307 BUILTIN(CF_DSPMETAFILEPICT
)
308 BUILTIN(CF_DSPENHMETAFILE
)
310 default: return wine_dbg_sprintf( "%04x", id
);
314 /**************************************************************************
315 * X11DRV_InitClipboard
317 void X11DRV_InitClipboard(void)
320 WINE_CLIPFORMAT
*format
;
322 /* Register built-in formats */
323 for (i
= 0; i
< sizeof(builtin_formats
)/sizeof(builtin_formats
[0]); i
++)
325 if (!(format
= HeapAlloc( GetProcessHeap(), 0, sizeof(*format
)))) break;
326 format
->wFormatID
= builtin_formats
[i
].id
;
327 format
->drvData
= GET_ATOM(builtin_formats
[i
].data
);
328 format
->lpDrvImportFunc
= builtin_formats
[i
].import
;
329 format
->lpDrvExportFunc
= builtin_formats
[i
].export
;
330 list_add_tail( &format_list
, &format
->entry
);
333 /* Register known mapping between window formats and X properties */
334 for (i
= 0; i
< sizeof(PropertyFormatMap
)/sizeof(PropertyFormatMap
[0]); i
++)
335 X11DRV_CLIPBOARD_InsertClipboardFormat( RegisterClipboardFormatW(PropertyFormatMap
[i
].lpszFormat
),
336 GET_ATOM(PropertyFormatMap
[i
].prop
));
338 /* Set up a conversion function from "HTML Format" to "text/html" */
339 format
= X11DRV_CLIPBOARD_InsertClipboardFormat( RegisterClipboardFormatW(wszHTMLFormat
),
340 GET_ATOM(XATOM_text_html
));
341 format
->lpDrvExportFunc
= X11DRV_CLIPBOARD_ExportTextHtml
;
345 /**************************************************************************
348 * Intern atoms for formats that don't have one yet.
350 static void intern_atoms(void)
352 LPWINE_CLIPFORMAT format
;
360 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
361 if (!format
->drvData
) count
++;
364 display
= thread_init_display();
366 names
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*names
) );
367 atoms
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*atoms
) );
370 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
371 if (!format
->drvData
) {
372 GetClipboardFormatNameW( format
->wFormatID
, buffer
, 256 );
373 len
= WideCharToMultiByte(CP_UNIXCP
, 0, buffer
, -1, NULL
, 0, NULL
, NULL
);
374 names
[i
] = HeapAlloc(GetProcessHeap(), 0, len
);
375 WideCharToMultiByte(CP_UNIXCP
, 0, buffer
, -1, names
[i
++], len
, NULL
, NULL
);
378 XInternAtoms( display
, names
, count
, False
, atoms
);
381 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
382 if (!format
->drvData
) {
383 HeapFree(GetProcessHeap(), 0, names
[i
]);
384 format
->drvData
= atoms
[i
++];
387 HeapFree( GetProcessHeap(), 0, names
);
388 HeapFree( GetProcessHeap(), 0, atoms
);
392 /**************************************************************************
395 * Register a custom X clipboard format.
397 static WINE_CLIPFORMAT
*register_format( UINT id
, Atom prop
)
399 LPWINE_CLIPFORMAT lpFormat
;
401 /* walk format chain to see if it's already registered */
402 LIST_FOR_EACH_ENTRY( lpFormat
, &format_list
, WINE_CLIPFORMAT
, entry
)
403 if (lpFormat
->wFormatID
== id
) return lpFormat
;
405 return X11DRV_CLIPBOARD_InsertClipboardFormat(id
, prop
);
409 /**************************************************************************
410 * X11DRV_CLIPBOARD_LookupProperty
412 static LPWINE_CLIPFORMAT
X11DRV_CLIPBOARD_LookupProperty(LPWINE_CLIPFORMAT current
, UINT drvData
)
416 struct list
*ptr
= current
? ¤t
->entry
: &format_list
;
417 BOOL need_intern
= FALSE
;
419 while ((ptr
= list_next( &format_list
, ptr
)))
421 LPWINE_CLIPFORMAT lpFormat
= LIST_ENTRY( ptr
, WINE_CLIPFORMAT
, entry
);
422 if (lpFormat
->drvData
== drvData
) return lpFormat
;
423 if (!lpFormat
->drvData
) need_intern
= TRUE
;
425 if (!need_intern
) return NULL
;
427 /* restart the search for the new atoms */
432 /**************************************************************************
433 * X11DRV_CLIPBOARD_LookupData
435 static LPWINE_CLIPDATA
X11DRV_CLIPBOARD_LookupData(DWORD wID
)
439 LIST_FOR_EACH_ENTRY( data
, &data_list
, WINE_CLIPDATA
, entry
)
440 if (data
->wFormatID
== wID
) return data
;
446 /**************************************************************************
447 * InsertClipboardFormat
449 static WINE_CLIPFORMAT
*X11DRV_CLIPBOARD_InsertClipboardFormat( UINT id
, Atom prop
)
451 LPWINE_CLIPFORMAT lpNewFormat
;
453 /* allocate storage for new format entry */
454 lpNewFormat
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPFORMAT
));
456 if(lpNewFormat
== NULL
)
458 WARN("No more memory for a new format!\n");
461 lpNewFormat
->wFormatID
= id
;
462 lpNewFormat
->drvData
= prop
;
463 lpNewFormat
->lpDrvImportFunc
= X11DRV_CLIPBOARD_ImportClipboardData
;
464 lpNewFormat
->lpDrvExportFunc
= X11DRV_CLIPBOARD_ExportClipboardData
;
466 list_add_tail( &format_list
, &lpNewFormat
->entry
);
468 TRACE("Registering format %s drvData %d\n",
469 debugstr_format(lpNewFormat
->wFormatID
), lpNewFormat
->drvData
);
477 /**************************************************************************
478 * X11DRV_CLIPBOARD_GetClipboardInfo
480 static BOOL
X11DRV_CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo
)
484 SERVER_START_REQ( set_clipboard_info
)
488 if (wine_server_call_err( req
))
490 ERR("Failed to get clipboard owner.\n");
494 cbInfo
->hWndOpen
= wine_server_ptr_handle( reply
->old_clipboard
);
495 cbInfo
->hWndOwner
= wine_server_ptr_handle( reply
->old_owner
);
496 cbInfo
->hWndViewer
= wine_server_ptr_handle( reply
->old_viewer
);
497 cbInfo
->seqno
= reply
->seqno
;
498 cbInfo
->flags
= reply
->flags
;
509 /**************************************************************************
510 * X11DRV_CLIPBOARD_ReleaseOwnership
512 static BOOL
X11DRV_CLIPBOARD_ReleaseOwnership(void)
516 SERVER_START_REQ( set_clipboard_info
)
518 req
->flags
= SET_CB_RELOWNER
| SET_CB_SEQNO
;
520 if (wine_server_call_err( req
))
522 ERR("Failed to set clipboard.\n");
536 /**************************************************************************
537 * X11DRV_CLIPBOARD_InsertClipboardData
539 * Caller *must* have the clipboard open and be the owner.
541 static BOOL
X11DRV_CLIPBOARD_InsertClipboardData(UINT wFormatID
, HANDLE hData
, DWORD flags
,
542 LPWINE_CLIPFORMAT lpFormat
, BOOL override
)
544 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(wFormatID
);
546 TRACE("format=%04x lpData=%p hData=%p flags=0x%08x lpFormat=%p override=%d\n",
547 wFormatID
, lpData
, hData
, flags
, lpFormat
, override
);
549 /* make sure the format exists */
550 if (!lpFormat
) register_format( wFormatID
, 0 );
552 if (lpData
&& !override
)
557 X11DRV_CLIPBOARD_FreeData(lpData
);
559 lpData
->hData
= hData
;
563 lpData
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPDATA
));
565 lpData
->wFormatID
= wFormatID
;
566 lpData
->hData
= hData
;
567 lpData
->lpFormat
= lpFormat
;
570 list_add_tail( &data_list
, &lpData
->entry
);
574 lpData
->wFlags
= flags
;
580 /**************************************************************************
581 * X11DRV_CLIPBOARD_FreeData
583 * Free clipboard data handle.
585 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData
)
587 TRACE("%04x\n", lpData
->wFormatID
);
589 if ((lpData
->wFormatID
>= CF_GDIOBJFIRST
&&
590 lpData
->wFormatID
<= CF_GDIOBJLAST
) ||
591 lpData
->wFormatID
== CF_BITMAP
||
592 lpData
->wFormatID
== CF_DIB
||
593 lpData
->wFormatID
== CF_PALETTE
)
596 DeleteObject(lpData
->hData
);
598 if ((lpData
->wFormatID
== CF_DIB
) && lpData
->drvData
)
599 XFreePixmap(gdi_display
, lpData
->drvData
);
601 else if (lpData
->wFormatID
== CF_METAFILEPICT
)
605 DeleteMetaFile(((METAFILEPICT
*)GlobalLock( lpData
->hData
))->hMF
);
606 GlobalFree(lpData
->hData
);
609 else if (lpData
->wFormatID
== CF_ENHMETAFILE
)
612 DeleteEnhMetaFile(lpData
->hData
);
614 else if (lpData
->wFormatID
< CF_PRIVATEFIRST
||
615 lpData
->wFormatID
> CF_PRIVATELAST
)
618 GlobalFree(lpData
->hData
);
626 /**************************************************************************
627 * X11DRV_CLIPBOARD_UpdateCache
629 static BOOL
X11DRV_CLIPBOARD_UpdateCache(LPCLIPBOARDINFO lpcbinfo
)
633 if (!X11DRV_CLIPBOARD_IsSelectionOwner())
635 if (!X11DRV_CLIPBOARD_GetClipboardInfo(lpcbinfo
))
637 ERR("Failed to retrieve clipboard information.\n");
640 else if (wSeqNo
< lpcbinfo
->seqno
)
642 X11DRV_EmptyClipboard(TRUE
);
644 if (X11DRV_CLIPBOARD_QueryAvailableData(thread_init_display(), lpcbinfo
) < 0)
646 ERR("Failed to cache clipboard data owned by another process.\n");
651 X11DRV_EndClipboardUpdate();
654 wSeqNo
= lpcbinfo
->seqno
;
662 /**************************************************************************
663 * X11DRV_CLIPBOARD_RenderFormat
665 static BOOL
X11DRV_CLIPBOARD_RenderFormat(Display
*display
, LPWINE_CLIPDATA lpData
)
669 TRACE(" 0x%04x hData(%p)\n", lpData
->wFormatID
, lpData
->hData
);
671 if (lpData
->hData
) return bret
; /* Already rendered */
673 if (lpData
->wFlags
& CF_FLAG_SYNTHESIZED
)
674 bret
= X11DRV_CLIPBOARD_RenderSynthesizedFormat(display
, lpData
);
675 else if (!X11DRV_CLIPBOARD_IsSelectionOwner())
677 if (!X11DRV_CLIPBOARD_ReadSelectionData(display
, lpData
))
679 ERR("Failed to cache clipboard data owned by another process. Format=%04x\n",
686 CLIPBOARDINFO cbInfo
;
688 if (X11DRV_CLIPBOARD_GetClipboardInfo(&cbInfo
) && cbInfo
.hWndOwner
)
690 /* Send a WM_RENDERFORMAT message to notify the owner to render the
691 * data requested into the clipboard.
693 TRACE("Sending WM_RENDERFORMAT message to hwnd(%p)\n", cbInfo
.hWndOwner
);
694 SendMessageW(cbInfo
.hWndOwner
, WM_RENDERFORMAT
, lpData
->wFormatID
, 0);
696 if (!lpData
->hData
) bret
= FALSE
;
700 ERR("hWndClipOwner is lost!\n");
709 /**************************************************************************
710 * CLIPBOARD_ConvertText
711 * Returns number of required/converted characters - not bytes!
713 static INT
CLIPBOARD_ConvertText(WORD src_fmt
, void const *src
, INT src_size
,
714 WORD dst_fmt
, void *dst
, INT dst_size
)
718 if(src_fmt
== CF_UNICODETEXT
)
731 return WideCharToMultiByte(cp
, 0, src
, src_size
, dst
, dst_size
, NULL
, NULL
);
734 if(dst_fmt
== CF_UNICODETEXT
)
747 return MultiByteToWideChar(cp
, 0, src
, src_size
, dst
, dst_size
);
750 if(!dst_size
) return src_size
;
752 if(dst_size
> src_size
) dst_size
= src_size
;
754 if(src_fmt
== CF_TEXT
)
755 CharToOemBuffA(src
, dst
, dst_size
);
757 OemToCharBuffA(src
, dst
, dst_size
);
763 /**************************************************************************
764 * X11DRV_CLIPBOARD_RenderSynthesizedFormat
766 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedFormat(Display
*display
, LPWINE_CLIPDATA lpData
)
772 if (lpData
->wFlags
& CF_FLAG_SYNTHESIZED
)
774 UINT wFormatID
= lpData
->wFormatID
;
776 if (wFormatID
== CF_UNICODETEXT
|| wFormatID
== CF_TEXT
|| wFormatID
== CF_OEMTEXT
)
777 bret
= X11DRV_CLIPBOARD_RenderSynthesizedText(display
, wFormatID
);
783 bret
= X11DRV_CLIPBOARD_RenderSynthesizedDIB( display
);
787 bret
= X11DRV_CLIPBOARD_RenderSynthesizedBitmap( display
);
791 bret
= X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile( display
);
794 case CF_METAFILEPICT
:
795 FIXME("Synthesizing CF_METAFILEPICT not implemented\n");
799 FIXME("Called to synthesize unknown format 0x%08x\n", wFormatID
);
804 lpData
->wFlags
&= ~CF_FLAG_SYNTHESIZED
;
811 /**************************************************************************
812 * X11DRV_CLIPBOARD_RenderSynthesizedText
814 * Renders synthesized text
816 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedText(Display
*display
, UINT wFormatID
)
821 INT src_chars
, dst_chars
, alloc_size
;
822 LPWINE_CLIPDATA lpSource
= NULL
;
824 TRACE("%04x\n", wFormatID
);
826 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(wFormatID
)) &&
830 /* Look for rendered source or non-synthesized source */
831 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT
)) &&
832 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
834 TRACE("UNICODETEXT -> %04x\n", wFormatID
);
836 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_TEXT
)) &&
837 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
839 TRACE("TEXT -> %04x\n", wFormatID
);
841 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT
)) &&
842 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
844 TRACE("OEMTEXT -> %04x\n", wFormatID
);
847 if (!lpSource
|| (lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
&&
851 /* Ask the clipboard owner to render the source text if necessary */
852 if (!lpSource
->hData
&& !X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
855 lpstrS
= GlobalLock(lpSource
->hData
);
859 /* Text always NULL terminated */
860 if(lpSource
->wFormatID
== CF_UNICODETEXT
)
861 src_chars
= strlenW((LPCWSTR
)lpstrS
) + 1;
863 src_chars
= strlen(lpstrS
) + 1;
865 /* Calculate number of characters in the destination buffer */
866 dst_chars
= CLIPBOARD_ConvertText(lpSource
->wFormatID
, lpstrS
,
867 src_chars
, wFormatID
, NULL
, 0);
872 TRACE("Converting from '%04x' to '%04x', %i chars\n",
873 lpSource
->wFormatID
, wFormatID
, src_chars
);
875 /* Convert characters to bytes */
876 if(wFormatID
== CF_UNICODETEXT
)
877 alloc_size
= dst_chars
* sizeof(WCHAR
);
879 alloc_size
= dst_chars
;
881 hData
= GlobalAlloc(GMEM_ZEROINIT
| GMEM_MOVEABLE
|
882 GMEM_DDESHARE
, alloc_size
);
884 lpstrT
= GlobalLock(hData
);
888 CLIPBOARD_ConvertText(lpSource
->wFormatID
, lpstrS
, src_chars
,
889 wFormatID
, lpstrT
, dst_chars
);
893 GlobalUnlock(lpSource
->hData
);
895 return X11DRV_CLIPBOARD_InsertClipboardData(wFormatID
, hData
, 0, NULL
, TRUE
);
899 /***********************************************************************
902 * Return the size of the bitmap info structure including color table.
904 static int bitmap_info_size( const BITMAPINFO
* info
, WORD coloruse
)
906 unsigned int colors
, size
, masks
= 0;
908 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
910 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
911 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
912 return sizeof(BITMAPCOREHEADER
) + colors
*
913 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
915 else /* assume BITMAPINFOHEADER */
917 colors
= info
->bmiHeader
.biClrUsed
;
918 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
919 colors
= 1 << info
->bmiHeader
.biBitCount
;
920 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
921 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
922 return size
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
927 /***********************************************************************
928 * create_dib_from_bitmap
930 * Allocates a packed DIB and copies the bitmap data into it.
932 static HGLOBAL
create_dib_from_bitmap(HBITMAP hBmp
)
938 LPBITMAPINFOHEADER pbmiHeader
;
939 unsigned int cDataSize
, cPackedSize
, OffsetBits
;
942 if (!GetObjectW( hBmp
, sizeof(bmp
), &bmp
)) return 0;
945 * A packed DIB contains a BITMAPINFO structure followed immediately by
946 * an optional color palette and the pixel data.
949 /* Calculate the size of the packed DIB */
950 cDataSize
= abs( bmp
.bmHeight
) * (((bmp
.bmWidth
* bmp
.bmBitsPixel
+ 31) / 8) & ~3);
951 cPackedSize
= sizeof(BITMAPINFOHEADER
)
952 + ( (bmp
.bmBitsPixel
<= 8) ? (sizeof(RGBQUAD
) * (1 << bmp
.bmBitsPixel
)) : 0 )
954 /* Get the offset to the bits */
955 OffsetBits
= cPackedSize
- cDataSize
;
957 /* Allocate the packed DIB */
958 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize
);
959 hPackedDIB
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
/*| GMEM_ZEROINIT*/,
963 WARN("Could not allocate packed DIB!\n");
967 /* A packed DIB starts with a BITMAPINFOHEADER */
968 pPackedDIB
= GlobalLock(hPackedDIB
);
969 pbmiHeader
= (LPBITMAPINFOHEADER
)pPackedDIB
;
971 /* Init the BITMAPINFOHEADER */
972 pbmiHeader
->biSize
= sizeof(BITMAPINFOHEADER
);
973 pbmiHeader
->biWidth
= bmp
.bmWidth
;
974 pbmiHeader
->biHeight
= bmp
.bmHeight
;
975 pbmiHeader
->biPlanes
= 1;
976 pbmiHeader
->biBitCount
= bmp
.bmBitsPixel
;
977 pbmiHeader
->biCompression
= BI_RGB
;
978 pbmiHeader
->biSizeImage
= 0;
979 pbmiHeader
->biXPelsPerMeter
= pbmiHeader
->biYPelsPerMeter
= 0;
980 pbmiHeader
->biClrUsed
= 0;
981 pbmiHeader
->biClrImportant
= 0;
983 /* Retrieve the DIB bits from the bitmap and fill in the
984 * DIB color table if present */
986 nLinesCopied
= GetDIBits(hdc
, /* Handle to device context */
987 hBmp
, /* Handle to bitmap */
988 0, /* First scan line to set in dest bitmap */
989 bmp
.bmHeight
, /* Number of scan lines to copy */
990 pPackedDIB
+ OffsetBits
, /* [out] Address of array for bitmap bits */
991 (LPBITMAPINFO
) pbmiHeader
, /* [out] Address of BITMAPINFO structure */
992 0); /* RGB or palette index */
993 GlobalUnlock(hPackedDIB
);
996 /* Cleanup if GetDIBits failed */
997 if (nLinesCopied
!= bmp
.bmHeight
)
999 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied
, bmp
.bmHeight
);
1000 GlobalFree(hPackedDIB
);
1007 /**************************************************************************
1008 * X11DRV_CLIPBOARD_RenderSynthesizedDIB
1010 * Renders synthesized DIB
1012 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedDIB(Display
*display
)
1015 LPWINE_CLIPDATA lpSource
= NULL
;
1019 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) && lpSource
->hData
)
1023 /* If we have a bitmap and it's not synthesized or it has been rendered */
1024 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) &&
1025 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
1027 /* Render source if required */
1028 if (lpSource
->hData
|| X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
1030 HGLOBAL hData
= create_dib_from_bitmap( lpSource
->hData
);
1033 X11DRV_CLIPBOARD_InsertClipboardData(CF_DIB
, hData
, 0, NULL
, TRUE
);
1043 /**************************************************************************
1044 * X11DRV_CLIPBOARD_RenderSynthesizedBitmap
1046 * Renders synthesized bitmap
1048 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedBitmap(Display
*display
)
1051 LPWINE_CLIPDATA lpSource
= NULL
;
1055 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) && lpSource
->hData
)
1059 /* If we have a dib and it's not synthesized or it has been rendered */
1060 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) &&
1061 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
1063 /* Render source if required */
1064 if (lpSource
->hData
|| X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
1067 HBITMAP hData
= NULL
;
1068 unsigned int offset
;
1069 LPBITMAPINFOHEADER lpbmih
;
1072 lpbmih
= GlobalLock(lpSource
->hData
);
1075 offset
= sizeof(BITMAPINFOHEADER
)
1076 + ((lpbmih
->biBitCount
<= 8) ? (sizeof(RGBQUAD
) *
1077 (1 << lpbmih
->biBitCount
)) : 0);
1079 hData
= CreateDIBitmap(hdc
, lpbmih
, CBM_INIT
, (LPBYTE
)lpbmih
+
1080 offset
, (LPBITMAPINFO
) lpbmih
, DIB_RGB_COLORS
);
1082 GlobalUnlock(lpSource
->hData
);
1084 ReleaseDC(NULL
, hdc
);
1088 X11DRV_CLIPBOARD_InsertClipboardData(CF_BITMAP
, hData
, 0, NULL
, TRUE
);
1098 /**************************************************************************
1099 * X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile
1101 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile(Display
*display
)
1103 LPWINE_CLIPDATA lpSource
= NULL
;
1107 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_ENHMETAFILE
)) && lpSource
->hData
)
1109 /* If we have a MF pict and it's not synthesized or it has been rendered */
1110 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT
)) &&
1111 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
1113 /* Render source if required */
1114 if (lpSource
->hData
|| X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
1117 HENHMETAFILE hData
= NULL
;
1119 pmfp
= GlobalLock(lpSource
->hData
);
1122 UINT size_mf_bits
= GetMetaFileBitsEx(pmfp
->hMF
, 0, NULL
);
1123 void *mf_bits
= HeapAlloc(GetProcessHeap(), 0, size_mf_bits
);
1126 GetMetaFileBitsEx(pmfp
->hMF
, size_mf_bits
, mf_bits
);
1127 hData
= SetWinMetaFileBits(size_mf_bits
, mf_bits
, NULL
, pmfp
);
1128 HeapFree(GetProcessHeap(), 0, mf_bits
);
1130 GlobalUnlock(lpSource
->hData
);
1135 X11DRV_CLIPBOARD_InsertClipboardData(CF_ENHMETAFILE
, hData
, 0, NULL
, TRUE
);
1145 /**************************************************************************
1146 * X11DRV_CLIPBOARD_ImportXAString
1148 * Import XA_STRING, converting the string to CF_TEXT.
1150 static HANDLE
X11DRV_CLIPBOARD_ImportXAString(Display
*display
, Window w
, Atom prop
)
1153 unsigned long cbytes
;
1155 unsigned long i
, inlcount
= 0;
1158 if (!X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1161 for (i
= 0; i
<= cbytes
; i
++)
1163 if (lpdata
[i
] == '\n')
1167 if ((hText
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cbytes
+ inlcount
+ 1)))
1169 lpstr
= GlobalLock(hText
);
1171 for (i
= 0, inlcount
= 0; i
<= cbytes
; i
++)
1173 if (lpdata
[i
] == '\n')
1174 lpstr
[inlcount
++] = '\r';
1176 lpstr
[inlcount
++] = lpdata
[i
];
1179 GlobalUnlock(hText
);
1182 /* Free the retrieved property data */
1183 HeapFree(GetProcessHeap(), 0, lpdata
);
1189 /**************************************************************************
1190 * X11DRV_CLIPBOARD_ImportUTF8
1192 * Import XA_STRING, converting the string to CF_UNICODE.
1194 static HANDLE
X11DRV_CLIPBOARD_ImportUTF8(Display
*display
, Window w
, Atom prop
)
1197 unsigned long cbytes
;
1199 unsigned long i
, inlcount
= 0;
1200 HANDLE hUnicodeText
= 0;
1202 if (!X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1205 for (i
= 0; i
<= cbytes
; i
++)
1207 if (lpdata
[i
] == '\n')
1211 if ((lpstr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cbytes
+ inlcount
+ 1)))
1215 for (i
= 0, inlcount
= 0; i
<= cbytes
; i
++)
1217 if (lpdata
[i
] == '\n')
1218 lpstr
[inlcount
++] = '\r';
1220 lpstr
[inlcount
++] = lpdata
[i
];
1223 count
= MultiByteToWideChar(CP_UTF8
, 0, lpstr
, -1, NULL
, 0);
1224 hUnicodeText
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, count
* sizeof(WCHAR
));
1228 WCHAR
*textW
= GlobalLock(hUnicodeText
);
1229 MultiByteToWideChar(CP_UTF8
, 0, lpstr
, -1, textW
, count
);
1230 GlobalUnlock(hUnicodeText
);
1233 HeapFree(GetProcessHeap(), 0, lpstr
);
1236 /* Free the retrieved property data */
1237 HeapFree(GetProcessHeap(), 0, lpdata
);
1239 return hUnicodeText
;
1243 /**************************************************************************
1244 * X11DRV_CLIPBOARD_ImportCompoundText
1246 * Import COMPOUND_TEXT to CF_UNICODE
1248 static HANDLE
X11DRV_CLIPBOARD_ImportCompoundText(Display
*display
, Window w
, Atom prop
)
1253 int srclen
, destlen
;
1254 HANDLE hUnicodeText
;
1255 XTextProperty txtprop
;
1257 if (!X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &txtprop
.value
, &txtprop
.nitems
))
1262 txtprop
.encoding
= x11drv_atom(COMPOUND_TEXT
);
1264 ret
= XmbTextPropertyToTextList(display
, &txtprop
, &srcstr
, &count
);
1265 HeapFree(GetProcessHeap(), 0, txtprop
.value
);
1266 if (ret
!= Success
|| !count
) return 0;
1268 TRACE("Importing %d line(s)\n", count
);
1270 /* Compute number of lines */
1271 srclen
= strlen(srcstr
[0]);
1272 for (i
= 0, lcount
= 0; i
<= srclen
; i
++)
1274 if (srcstr
[0][i
] == '\n')
1278 destlen
= MultiByteToWideChar(CP_UNIXCP
, 0, srcstr
[0], -1, NULL
, 0);
1280 TRACE("lcount = %d, destlen=%d, srcstr %s\n", lcount
, destlen
, srcstr
[0]);
1282 if ((hUnicodeText
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, (destlen
+ lcount
+ 1) * sizeof(WCHAR
))))
1284 WCHAR
*deststr
= GlobalLock(hUnicodeText
);
1285 MultiByteToWideChar(CP_UNIXCP
, 0, srcstr
[0], -1, deststr
, destlen
);
1289 for (i
= destlen
- 1, j
= destlen
+ lcount
- 1; i
>= 0; i
--, j
--)
1291 deststr
[j
] = deststr
[i
];
1293 if (deststr
[i
] == '\n')
1294 deststr
[--j
] = '\r';
1298 GlobalUnlock(hUnicodeText
);
1301 XFreeStringList(srcstr
);
1303 return hUnicodeText
;
1307 /**************************************************************************
1308 * X11DRV_CLIPBOARD_ImportXAPIXMAP
1310 * Import XA_PIXMAP, converting the image to CF_DIB.
1312 static HANDLE
X11DRV_CLIPBOARD_ImportXAPIXMAP(Display
*display
, Window w
, Atom prop
)
1315 unsigned long cbytes
;
1317 HANDLE hClipData
= 0;
1319 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1321 XVisualInfo vis
= default_visual
;
1322 char buffer
[FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )];
1323 BITMAPINFO
*info
= (BITMAPINFO
*)buffer
;
1324 struct gdi_image_bits bits
;
1326 int x
,y
; /* Unused */
1327 unsigned border_width
; /* Unused */
1328 unsigned int depth
, width
, height
;
1330 pPixmap
= (Pixmap
*) lpdata
;
1332 /* Get the Pixmap dimensions and bit depth */
1333 if (!XGetGeometry(gdi_display
, *pPixmap
, &root
, &x
, &y
, &width
, &height
,
1334 &border_width
, &depth
)) depth
= 0;
1335 if (!pixmap_formats
[depth
]) return 0;
1337 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
1338 width
, height
, depth
);
1340 if (depth
!= vis
.depth
) switch (pixmap_formats
[depth
]->bits_per_pixel
)
1346 case 16: /* assume R5G5B5 */
1347 vis
.red_mask
= 0x7c00;
1348 vis
.green_mask
= 0x03e0;
1349 vis
.blue_mask
= 0x001f;
1351 case 24: /* assume R8G8B8 */
1352 case 32: /* assume A8R8G8B8 */
1353 vis
.red_mask
= 0xff0000;
1354 vis
.green_mask
= 0x00ff00;
1355 vis
.blue_mask
= 0x0000ff;
1361 if (!get_pixmap_image( *pPixmap
, width
, height
, &vis
, info
, &bits
))
1363 DWORD info_size
= bitmap_info_size( info
, DIB_RGB_COLORS
);
1366 hClipData
= GlobalAlloc( GMEM_MOVEABLE
| GMEM_DDESHARE
,
1367 info_size
+ info
->bmiHeader
.biSizeImage
);
1370 ptr
= GlobalLock( hClipData
);
1371 memcpy( ptr
, info
, info_size
);
1372 memcpy( ptr
+ info_size
, bits
.ptr
, info
->bmiHeader
.biSizeImage
);
1373 GlobalUnlock( hClipData
);
1375 if (bits
.free
) bits
.free( &bits
);
1378 HeapFree(GetProcessHeap(), 0, lpdata
);
1385 /**************************************************************************
1386 * X11DRV_CLIPBOARD_ImportImageBmp
1388 * Import image/bmp, converting the image to CF_DIB.
1390 static HANDLE
X11DRV_CLIPBOARD_ImportImageBmp(Display
*display
, Window w
, Atom prop
)
1393 unsigned long cbytes
;
1394 HANDLE hClipData
= 0;
1396 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1398 BITMAPFILEHEADER
*bfh
= (BITMAPFILEHEADER
*)lpdata
;
1400 if (cbytes
>= sizeof(BITMAPFILEHEADER
)+sizeof(BITMAPCOREHEADER
) &&
1401 bfh
->bfType
== 0x4d42 /* "BM" */)
1403 BITMAPINFO
*bmi
= (BITMAPINFO
*)(bfh
+1);
1408 hbmp
= CreateDIBitmap(
1412 lpdata
+bfh
->bfOffBits
,
1417 hClipData
= create_dib_from_bitmap( hbmp
);
1423 /* Free the retrieved property data */
1424 HeapFree(GetProcessHeap(), 0, lpdata
);
1431 /**************************************************************************
1432 * X11DRV_CLIPBOARD_ImportMetaFilePict
1434 * Import MetaFilePict.
1436 static HANDLE
X11DRV_CLIPBOARD_ImportMetaFilePict(Display
*display
, Window w
, Atom prop
)
1439 unsigned long cbytes
;
1440 HANDLE hClipData
= 0;
1442 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1445 hClipData
= X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT
, lpdata
, (LPDWORD
)&cbytes
, FALSE
);
1447 /* Free the retrieved property data */
1448 HeapFree(GetProcessHeap(), 0, lpdata
);
1455 /**************************************************************************
1456 * X11DRV_ImportEnhMetaFile
1458 * Import EnhMetaFile.
1460 static HANDLE
X11DRV_CLIPBOARD_ImportEnhMetaFile(Display
*display
, Window w
, Atom prop
)
1463 unsigned long cbytes
;
1464 HANDLE hClipData
= 0;
1466 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1469 hClipData
= X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE
, lpdata
, (LPDWORD
)&cbytes
, FALSE
);
1471 /* Free the retrieved property data */
1472 HeapFree(GetProcessHeap(), 0, lpdata
);
1479 /**************************************************************************
1480 * X11DRV_ImportClipbordaData
1482 * Generic import clipboard data routine.
1484 static HANDLE
X11DRV_CLIPBOARD_ImportClipboardData(Display
*display
, Window w
, Atom prop
)
1488 unsigned long cbytes
;
1489 HANDLE hClipData
= 0;
1491 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1495 /* Turn on the DDESHARE flag to enable shared 32 bit memory */
1496 hClipData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cbytes
);
1499 HeapFree(GetProcessHeap(), 0, lpdata
);
1503 if ((lpClipData
= GlobalLock(hClipData
)))
1505 memcpy(lpClipData
, lpdata
, cbytes
);
1506 GlobalUnlock(hClipData
);
1510 GlobalFree(hClipData
);
1515 /* Free the retrieved property data */
1516 HeapFree(GetProcessHeap(), 0, lpdata
);
1523 /**************************************************************************
1524 X11DRV_CLIPBOARD_ExportClipboardData
1526 * Generic export clipboard data routine.
1528 static HANDLE
X11DRV_CLIPBOARD_ExportClipboardData(Display
*display
, Window requestor
, Atom aTarget
,
1529 Atom rprop
, LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1533 HANDLE hClipData
= 0;
1535 *lpBytes
= 0; /* Assume failure */
1537 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpData
))
1538 ERR("Failed to export %04x format\n", lpData
->wFormatID
);
1541 datasize
= GlobalSize(lpData
->hData
);
1543 hClipData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, datasize
);
1544 if (hClipData
== 0) return NULL
;
1546 if ((lpClipData
= GlobalLock(hClipData
)))
1548 LPVOID lpdata
= GlobalLock(lpData
->hData
);
1550 memcpy(lpClipData
, lpdata
, datasize
);
1551 *lpBytes
= datasize
;
1553 GlobalUnlock(lpData
->hData
);
1554 GlobalUnlock(hClipData
);
1556 GlobalFree(hClipData
);
1565 /**************************************************************************
1566 * X11DRV_CLIPBOARD_ExportXAString
1568 * Export CF_TEXT converting the string to XA_STRING.
1569 * Helper function for X11DRV_CLIPBOARD_ExportString.
1571 static HANDLE
X11DRV_CLIPBOARD_ExportXAString(LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1575 LPSTR text
, lpstr
= NULL
;
1577 *lpBytes
= 0; /* Assume return has zero bytes */
1579 text
= GlobalLock(lpData
->hData
);
1580 size
= strlen(text
);
1582 /* remove carriage returns */
1583 lpstr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
+ 1);
1587 for (i
= 0,j
= 0; i
< size
&& text
[i
]; i
++)
1589 if (text
[i
] == '\r' && (text
[i
+1] == '\n' || text
[i
+1] == '\0'))
1591 lpstr
[j
++] = text
[i
];
1595 *lpBytes
= j
; /* Number of bytes in string */
1598 GlobalUnlock(lpData
->hData
);
1604 /**************************************************************************
1605 * X11DRV_CLIPBOARD_ExportUTF8String
1607 * Export CF_UNICODE converting the string to UTF8.
1608 * Helper function for X11DRV_CLIPBOARD_ExportString.
1610 static HANDLE
X11DRV_CLIPBOARD_ExportUTF8String(LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1615 LPSTR text
, lpstr
= NULL
;
1617 *lpBytes
= 0; /* Assume return has zero bytes */
1619 uni_text
= GlobalLock(lpData
->hData
);
1621 size
= WideCharToMultiByte(CP_UTF8
, 0, uni_text
, -1, NULL
, 0, NULL
, NULL
);
1623 text
= HeapAlloc(GetProcessHeap(), 0, size
);
1626 WideCharToMultiByte(CP_UTF8
, 0, uni_text
, -1, text
, size
, NULL
, NULL
);
1628 /* remove carriage returns */
1629 lpstr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
--);
1633 for (i
= 0,j
= 0; i
< size
&& text
[i
]; i
++)
1635 if (text
[i
] == '\r' && (text
[i
+1] == '\n' || text
[i
+1] == '\0'))
1637 lpstr
[j
++] = text
[i
];
1641 *lpBytes
= j
; /* Number of bytes in string */
1644 HeapFree(GetProcessHeap(), 0, text
);
1645 GlobalUnlock(lpData
->hData
);
1652 /**************************************************************************
1653 * X11DRV_CLIPBOARD_ExportCompoundText
1655 * Export CF_UNICODE to COMPOUND_TEXT
1656 * Helper function for X11DRV_CLIPBOARD_ExportString.
1658 static HANDLE
X11DRV_CLIPBOARD_ExportCompoundText(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1659 LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1663 XICCEncodingStyle style
;
1668 uni_text
= GlobalLock(lpData
->hData
);
1670 size
= WideCharToMultiByte(CP_UNIXCP
, 0, uni_text
, -1, NULL
, 0, NULL
, NULL
);
1671 lpstr
= HeapAlloc(GetProcessHeap(), 0, size
);
1675 WideCharToMultiByte(CP_UNIXCP
, 0, uni_text
, -1, lpstr
, size
, NULL
, NULL
);
1677 /* remove carriage returns */
1678 for (i
= 0, j
= 0; i
< size
&& lpstr
[i
]; i
++)
1680 if (lpstr
[i
] == '\r' && (lpstr
[i
+1] == '\n' || lpstr
[i
+1] == '\0'))
1682 lpstr
[j
++] = lpstr
[i
];
1686 GlobalUnlock(lpData
->hData
);
1688 if (aTarget
== x11drv_atom(COMPOUND_TEXT
))
1689 style
= XCompoundTextStyle
;
1691 style
= XStdICCTextStyle
;
1693 /* Update the X property */
1694 if (XmbTextListToTextProperty(display
, &lpstr
, 1, style
, &prop
) == Success
)
1696 XSetTextProperty(display
, requestor
, &prop
, rprop
);
1700 HeapFree(GetProcessHeap(), 0, lpstr
);
1705 /**************************************************************************
1706 * X11DRV_CLIPBOARD_ExportString
1710 static HANDLE
X11DRV_CLIPBOARD_ExportString(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1711 LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1713 if (X11DRV_CLIPBOARD_RenderFormat(display
, lpData
))
1715 if (aTarget
== XA_STRING
)
1716 return X11DRV_CLIPBOARD_ExportXAString(lpData
, lpBytes
);
1717 else if (aTarget
== x11drv_atom(COMPOUND_TEXT
) || aTarget
== x11drv_atom(TEXT
))
1718 return X11DRV_CLIPBOARD_ExportCompoundText(display
, requestor
, aTarget
,
1719 rprop
, lpData
, lpBytes
);
1722 TRACE("Exporting target %ld to default UTF8_STRING\n", aTarget
);
1723 return X11DRV_CLIPBOARD_ExportUTF8String(lpData
, lpBytes
);
1727 ERR("Failed to render %04x format\n", lpData
->wFormatID
);
1733 /**************************************************************************
1734 * X11DRV_CLIPBOARD_ExportXAPIXMAP
1736 * Export CF_DIB to XA_PIXMAP.
1738 static HANDLE
X11DRV_CLIPBOARD_ExportXAPIXMAP(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1739 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1742 unsigned char* lpData
;
1744 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1746 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1750 if (!lpdata
->drvData
) /* If not already rendered */
1754 struct gdi_image_bits bits
;
1756 pbmi
= GlobalLock( lpdata
->hData
);
1757 bits
.ptr
= (LPBYTE
)pbmi
+ bitmap_info_size( pbmi
, DIB_RGB_COLORS
);
1759 bits
.is_copy
= FALSE
;
1760 pixmap
= create_pixmap_from_image( 0, &default_visual
, pbmi
, &bits
, DIB_RGB_COLORS
);
1761 GlobalUnlock( lpdata
->hData
);
1762 lpdata
->drvData
= pixmap
;
1765 *lpBytes
= sizeof(Pixmap
); /* pixmap is a 32bit value */
1767 /* Wrap pixmap so we can return a handle */
1768 hData
= GlobalAlloc(0, *lpBytes
);
1769 lpData
= GlobalLock(hData
);
1770 memcpy(lpData
, &lpdata
->drvData
, *lpBytes
);
1771 GlobalUnlock(hData
);
1777 /**************************************************************************
1778 * X11DRV_CLIPBOARD_ExportImageBmp
1780 * Export CF_DIB to image/bmp.
1782 static HANDLE
X11DRV_CLIPBOARD_ExportImageBmp(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1783 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1790 BITMAPFILEHEADER
*bfh
;
1794 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1796 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1800 hpackeddib
= lpdata
->hData
;
1802 dibdata
= GlobalLock(hpackeddib
);
1805 ERR("Failed to lock packed DIB\n");
1809 bmpsize
= sizeof(BITMAPFILEHEADER
) + GlobalSize(hpackeddib
);
1811 hbmpdata
= GlobalAlloc(0, bmpsize
);
1815 bmpdata
= GlobalLock(hbmpdata
);
1819 GlobalFree(hbmpdata
);
1820 GlobalUnlock(hpackeddib
);
1824 /* bitmap file header */
1825 bfh
= (BITMAPFILEHEADER
*)bmpdata
;
1826 bfh
->bfType
= 0x4d42; /* "BM" */
1827 bfh
->bfSize
= bmpsize
;
1828 bfh
->bfReserved1
= 0;
1829 bfh
->bfReserved2
= 0;
1830 bfh
->bfOffBits
= sizeof(BITMAPFILEHEADER
) + bitmap_info_size((BITMAPINFO
*)dibdata
, DIB_RGB_COLORS
);
1832 /* rest of bitmap is the same as the packed dib */
1833 memcpy(bfh
+1, dibdata
, bmpsize
-sizeof(BITMAPFILEHEADER
));
1837 GlobalUnlock(hbmpdata
);
1840 GlobalUnlock(hpackeddib
);
1846 /**************************************************************************
1847 * X11DRV_CLIPBOARD_ExportMetaFilePict
1849 * Export MetaFilePict.
1851 static HANDLE
X11DRV_CLIPBOARD_ExportMetaFilePict(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1852 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1854 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1856 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1860 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT
, lpdata
->hData
, lpBytes
, TRUE
);
1864 /**************************************************************************
1865 * X11DRV_CLIPBOARD_ExportEnhMetaFile
1867 * Export EnhMetaFile.
1869 static HANDLE
X11DRV_CLIPBOARD_ExportEnhMetaFile(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1870 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1872 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1874 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1878 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE
, lpdata
->hData
, lpBytes
, TRUE
);
1882 /**************************************************************************
1883 * get_html_description_field
1885 * Find the value of a field in an HTML Format description.
1887 static LPCSTR
get_html_description_field(LPCSTR data
, LPCSTR keyword
)
1891 while (pos
&& *pos
&& *pos
!= '<')
1893 if (memcmp(pos
, keyword
, strlen(keyword
)) == 0)
1894 return pos
+strlen(keyword
);
1896 pos
= strchr(pos
, '\n');
1904 /**************************************************************************
1905 * X11DRV_CLIPBOARD_ExportTextHtml
1907 * Export HTML Format to text/html.
1909 * FIXME: We should attempt to add an <a base> tag and convert windows paths.
1911 static HANDLE
X11DRV_CLIPBOARD_ExportTextHtml(Display
*display
, Window requestor
, Atom aTarget
,
1912 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1915 LPCSTR data
, field_value
;
1916 UINT fragmentstart
, fragmentend
, htmlsize
;
1917 HANDLE hhtmldata
=NULL
;
1922 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1924 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1928 hdata
= lpdata
->hData
;
1930 data
= GlobalLock(hdata
);
1933 ERR("Failed to lock HTML Format data\n");
1937 /* read the important fields */
1938 field_value
= get_html_description_field(data
, "StartFragment:");
1941 ERR("Couldn't find StartFragment value\n");
1944 fragmentstart
= atoi(field_value
);
1946 field_value
= get_html_description_field(data
, "EndFragment:");
1949 ERR("Couldn't find EndFragment value\n");
1952 fragmentend
= atoi(field_value
);
1954 /* export only the fragment */
1955 htmlsize
= fragmentend
- fragmentstart
+ 1;
1957 hhtmldata
= GlobalAlloc(0, htmlsize
);
1961 htmldata
= GlobalLock(hhtmldata
);
1965 GlobalFree(hhtmldata
);
1970 memcpy(htmldata
, &data
[fragmentstart
], fragmentend
-fragmentstart
);
1971 htmldata
[htmlsize
-1] = '\0';
1973 *lpBytes
= htmlsize
;
1975 GlobalUnlock(htmldata
);
1980 GlobalUnlock(hdata
);
1986 /**************************************************************************
1987 * X11DRV_CLIPBOARD_QueryTargets
1989 static BOOL
X11DRV_CLIPBOARD_QueryTargets(Display
*display
, Window w
, Atom selection
,
1990 Atom target
, XEvent
*xe
)
1994 XConvertSelection(display
, selection
, target
, x11drv_atom(SELECTION_DATA
), w
, CurrentTime
);
1997 * Wait until SelectionNotify is received
1999 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
2001 Bool res
= XCheckTypedWindowEvent(display
, w
, SelectionNotify
, xe
);
2002 if (res
&& xe
->xselection
.selection
== selection
) break;
2004 usleep(SELECTION_WAIT
);
2007 if (i
== SELECTION_RETRIES
)
2009 ERR("Timed out waiting for SelectionNotify event\n");
2012 /* Verify that the selection returned a valid TARGETS property */
2013 if ((xe
->xselection
.target
!= target
) || (xe
->xselection
.property
== None
))
2015 /* Selection owner failed to respond or we missed the SelectionNotify */
2016 WARN("Failed to retrieve TARGETS for selection %ld.\n", selection
);
2024 static int is_atom_error( Display
*display
, XErrorEvent
*event
, void *arg
)
2026 return (event
->error_code
== BadAtom
);
2029 /**************************************************************************
2030 * X11DRV_CLIPBOARD_InsertSelectionProperties
2032 * Mark properties available for future retrieval.
2034 static VOID
X11DRV_CLIPBOARD_InsertSelectionProperties(Display
*display
, Atom
* properties
, UINT count
)
2036 UINT i
, nb_atoms
= 0;
2039 /* Cache these formats in the clipboard cache */
2040 for (i
= 0; i
< count
; i
++)
2042 LPWINE_CLIPFORMAT lpFormat
= X11DRV_CLIPBOARD_LookupProperty(NULL
, properties
[i
]);
2046 /* We found at least one Window's format that mapps to the property.
2047 * Continue looking for more.
2049 * If more than one property map to a Window's format then we use the first
2050 * one and ignore the rest.
2054 TRACE("Atom#%d Property(%d): --> Format %s\n",
2055 i
, lpFormat
->drvData
, debugstr_format(lpFormat
->wFormatID
));
2056 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat
->wFormatID
, 0, 0, lpFormat
, FALSE
);
2057 lpFormat
= X11DRV_CLIPBOARD_LookupProperty(lpFormat
, properties
[i
]);
2060 else if (properties
[i
])
2062 /* add it to the list of atoms that we don't know about yet */
2063 if (!atoms
) atoms
= HeapAlloc( GetProcessHeap(), 0,
2064 (count
- i
) * sizeof(*atoms
) );
2065 if (atoms
) atoms
[nb_atoms
++] = properties
[i
];
2069 /* query all unknown atoms in one go */
2072 char **names
= HeapAlloc( GetProcessHeap(), 0, nb_atoms
* sizeof(*names
) );
2075 X11DRV_expect_error( display
, is_atom_error
, NULL
);
2076 if (!XGetAtomNames( display
, atoms
, nb_atoms
, names
)) nb_atoms
= 0;
2077 if (X11DRV_check_error())
2079 WARN( "got some bad atoms, ignoring\n" );
2082 for (i
= 0; i
< nb_atoms
; i
++)
2084 WINE_CLIPFORMAT
*lpFormat
;
2086 int len
= MultiByteToWideChar(CP_UNIXCP
, 0, names
[i
], -1, NULL
, 0);
2087 wname
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
2088 MultiByteToWideChar(CP_UNIXCP
, 0, names
[i
], -1, wname
, len
);
2090 lpFormat
= register_format( RegisterClipboardFormatW(wname
), atoms
[i
] );
2091 HeapFree(GetProcessHeap(), 0, wname
);
2094 ERR("Failed to register %s property. Type will not be cached.\n", names
[i
]);
2097 TRACE("Atom#%d Property(%d): --> Format %s\n",
2098 i
, lpFormat
->drvData
, debugstr_format(lpFormat
->wFormatID
));
2099 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat
->wFormatID
, 0, 0, lpFormat
, FALSE
);
2101 for (i
= 0; i
< nb_atoms
; i
++) XFree( names
[i
] );
2102 HeapFree( GetProcessHeap(), 0, names
);
2104 HeapFree( GetProcessHeap(), 0, atoms
);
2109 /**************************************************************************
2110 * X11DRV_CLIPBOARD_QueryAvailableData
2112 * Caches the list of data formats available from the current selection.
2113 * This queries the selection owner for the TARGETS property and saves all
2114 * reported property types.
2116 static int X11DRV_CLIPBOARD_QueryAvailableData(Display
*display
, LPCLIPBOARDINFO lpcbinfo
)
2119 Atom atype
=AnyPropertyType
;
2121 unsigned long remain
;
2122 Atom
* targetList
=NULL
;
2124 unsigned long cSelectionTargets
= 0;
2126 if (selectionAcquired
& (S_PRIMARY
| S_CLIPBOARD
))
2128 ERR("Received request to cache selection but process is owner=(%08x)\n",
2129 (unsigned) selectionWindow
);
2130 return -1; /* Prevent self request */
2133 w
= thread_selection_wnd();
2136 ERR("No window available to retrieve selection!\n");
2141 * Query the selection owner for the TARGETS property
2143 if ((use_primary_selection
&& XGetSelectionOwner(display
,XA_PRIMARY
)) ||
2144 XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)))
2146 if (use_primary_selection
&& (X11DRV_CLIPBOARD_QueryTargets(display
, w
, XA_PRIMARY
, x11drv_atom(TARGETS
), &xe
)))
2147 selectionCacheSrc
= XA_PRIMARY
;
2148 else if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, x11drv_atom(CLIPBOARD
), x11drv_atom(TARGETS
), &xe
))
2149 selectionCacheSrc
= x11drv_atom(CLIPBOARD
);
2152 Atom xstr
= XA_STRING
;
2154 /* Selection Owner doesn't understand TARGETS, try retrieving XA_STRING */
2155 if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, XA_PRIMARY
, XA_STRING
, &xe
))
2157 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, &xstr
, 1);
2158 selectionCacheSrc
= XA_PRIMARY
;
2161 else if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, x11drv_atom(CLIPBOARD
), XA_STRING
, &xe
))
2163 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, &xstr
, 1);
2164 selectionCacheSrc
= x11drv_atom(CLIPBOARD
);
2169 WARN("Failed to query selection owner for available data.\n");
2174 else return 0; /* No selection owner so report 0 targets available */
2176 /* Read the TARGETS property contents */
2177 if (!XGetWindowProperty(display
, xe
.xselection
.requestor
, xe
.xselection
.property
,
2178 0, 0x3FFF, True
, AnyPropertyType
/*XA_ATOM*/, &atype
, &aformat
, &cSelectionTargets
,
2179 &remain
, (unsigned char**)&targetList
) != Success
)
2181 TRACE("Type %lx,Format %d,nItems %ld, Remain %ld\n",
2182 atype
, aformat
, cSelectionTargets
, remain
);
2184 * The TARGETS property should have returned us a list of atoms
2185 * corresponding to each selection target format supported.
2187 if (atype
== XA_ATOM
|| atype
== x11drv_atom(TARGETS
))
2191 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, targetList
, cSelectionTargets
);
2193 else if (aformat
== 8) /* work around quartz-wm brain damage */
2195 unsigned long i
, count
= cSelectionTargets
/ sizeof(CARD32
);
2196 Atom
*atoms
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(Atom
) );
2197 for (i
= 0; i
< count
; i
++)
2198 atoms
[i
] = ((CARD32
*)targetList
)[i
]; /* FIXME: byte swapping */
2199 X11DRV_CLIPBOARD_InsertSelectionProperties( display
, atoms
, count
);
2200 HeapFree( GetProcessHeap(), 0, atoms
);
2204 /* Free the list of targets */
2207 else WARN("Failed to read TARGETS property\n");
2209 return cSelectionTargets
;
2213 /**************************************************************************
2214 * X11DRV_CLIPBOARD_ReadSelectionData
2216 * This method is invoked only when we DO NOT own the X selection
2218 * We always get the data from the selection client each time,
2219 * since we have no way of determining if the data in our cache is stale.
2221 static BOOL
X11DRV_CLIPBOARD_ReadSelectionData(Display
*display
, LPWINE_CLIPDATA lpData
)
2228 TRACE("%04x\n", lpData
->wFormatID
);
2230 if (!lpData
->lpFormat
)
2232 ERR("Requesting format %04x but no source format linked to data.\n",
2237 if (!selectionAcquired
)
2239 Window w
= thread_selection_wnd();
2242 ERR("No window available to read selection data!\n");
2246 TRACE("Requesting conversion of %s property (%d) from selection type %08x\n",
2247 debugstr_format(lpData
->lpFormat
->wFormatID
), lpData
->lpFormat
->drvData
,
2248 (UINT
)selectionCacheSrc
);
2250 XConvertSelection(display
, selectionCacheSrc
, lpData
->lpFormat
->drvData
,
2251 x11drv_atom(SELECTION_DATA
), w
, CurrentTime
);
2253 /* wait until SelectionNotify is received */
2254 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
2256 res
= XCheckTypedWindowEvent(display
, w
, SelectionNotify
, &xe
);
2257 if (res
&& xe
.xselection
.selection
== selectionCacheSrc
) break;
2259 usleep(SELECTION_WAIT
);
2262 if (i
== SELECTION_RETRIES
)
2264 ERR("Timed out waiting for SelectionNotify event\n");
2266 /* Verify that the selection returned a valid TARGETS property */
2267 else if (xe
.xselection
.property
!= None
)
2270 * Read the contents of the X selection property
2271 * into WINE's clipboard cache and converting the
2272 * data format if necessary.
2274 HANDLE hData
= lpData
->lpFormat
->lpDrvImportFunc(display
, xe
.xselection
.requestor
,
2275 xe
.xselection
.property
);
2278 bRet
= X11DRV_CLIPBOARD_InsertClipboardData(lpData
->wFormatID
, hData
, 0, lpData
->lpFormat
, TRUE
);
2280 TRACE("Import function failed\n");
2284 TRACE("Failed to convert selection\n");
2289 ERR("Received request to cache selection data but process is owner\n");
2292 TRACE("Returning %d\n", bRet
);
2298 /**************************************************************************
2299 * X11DRV_CLIPBOARD_GetProperty
2300 * Gets type, data and size.
2302 static BOOL
X11DRV_CLIPBOARD_GetProperty(Display
*display
, Window w
, Atom prop
,
2303 Atom
*atype
, unsigned char** data
, unsigned long* datasize
)
2306 unsigned long pos
= 0, nitems
, remain
, count
;
2307 unsigned char *val
= NULL
, *buffer
;
2309 TRACE("Reading property %lu from X window %lx\n", prop
, w
);
2313 if (XGetWindowProperty(display
, w
, prop
, pos
, INT_MAX
/ 4, False
,
2314 AnyPropertyType
, atype
, &aformat
, &nitems
, &remain
, &buffer
) != Success
)
2316 WARN("Failed to read property\n");
2317 HeapFree( GetProcessHeap(), 0, val
);
2321 count
= get_property_size( aformat
, nitems
);
2322 if (!val
) *data
= HeapAlloc( GetProcessHeap(), 0, pos
* sizeof(int) + count
+ 1 );
2323 else *data
= HeapReAlloc( GetProcessHeap(), 0, val
, pos
* sizeof(int) + count
+ 1 );
2328 HeapFree( GetProcessHeap(), 0, val
);
2332 memcpy( (int *)val
+ pos
, buffer
, count
);
2336 *datasize
= pos
* sizeof(int) + count
;
2340 pos
+= count
/ sizeof(int);
2343 /* Delete the property on the window now that we are done
2344 * This will send a PropertyNotify event to the selection owner. */
2345 XDeleteProperty(display
, w
, prop
);
2350 struct clipboard_data_packet
{
2353 unsigned char *data
;
2356 /**************************************************************************
2357 * X11DRV_CLIPBOARD_ReadProperty
2358 * Reads the contents of the X selection property.
2360 static BOOL
X11DRV_CLIPBOARD_ReadProperty(Display
*display
, Window w
, Atom prop
,
2361 unsigned char** data
, unsigned long* datasize
)
2369 while (XCheckTypedWindowEvent(display
, w
, PropertyNotify
, &xe
))
2372 if (!X11DRV_CLIPBOARD_GetProperty(display
, w
, prop
, &atype
, data
, datasize
))
2375 if (atype
== x11drv_atom(INCR
))
2378 unsigned long bufsize
= 0;
2379 struct list packets
;
2380 struct clipboard_data_packet
*packet
, *packet2
;
2383 HeapFree(GetProcessHeap(), 0, *data
);
2386 list_init(&packets
);
2391 unsigned char *prop_data
;
2392 unsigned long prop_size
;
2394 /* Wait until PropertyNotify is received */
2395 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
2399 res
= XCheckTypedWindowEvent(display
, w
, PropertyNotify
, &xe
);
2400 if (res
&& xe
.xproperty
.atom
== prop
&&
2401 xe
.xproperty
.state
== PropertyNewValue
)
2403 usleep(SELECTION_WAIT
);
2406 if (i
>= SELECTION_RETRIES
||
2407 !X11DRV_CLIPBOARD_GetProperty(display
, w
, prop
, &atype
, &prop_data
, &prop_size
))
2413 /* Retrieved entire data. */
2416 HeapFree(GetProcessHeap(), 0, prop_data
);
2421 packet
= HeapAlloc(GetProcessHeap(), 0, sizeof(*packet
));
2424 HeapFree(GetProcessHeap(), 0, prop_data
);
2429 packet
->size
= prop_size
;
2430 packet
->data
= prop_data
;
2431 list_add_tail(&packets
, &packet
->entry
);
2432 bufsize
+= prop_size
;
2437 buf
= HeapAlloc(GetProcessHeap(), 0, bufsize
+ 1);
2440 unsigned long bytes_copied
= 0;
2441 *datasize
= bufsize
;
2442 LIST_FOR_EACH_ENTRY( packet
, &packets
, struct clipboard_data_packet
, entry
)
2444 memcpy(&buf
[bytes_copied
], packet
->data
, packet
->size
);
2445 bytes_copied
+= packet
->size
;
2454 LIST_FOR_EACH_ENTRY_SAFE( packet
, packet2
, &packets
, struct clipboard_data_packet
, entry
)
2456 HeapFree(GetProcessHeap(), 0, packet
->data
);
2457 HeapFree(GetProcessHeap(), 0, packet
);
2467 /**************************************************************************
2468 * CLIPBOARD_SerializeMetafile
2470 static HANDLE
X11DRV_CLIPBOARD_SerializeMetafile(INT wformat
, HANDLE hdata
, LPDWORD lpcbytes
, BOOL out
)
2474 TRACE(" wFormat=%d hdata=%p out=%d\n", wformat
, hdata
, out
);
2476 if (out
) /* Serialize out, caller should free memory */
2478 *lpcbytes
= 0; /* Assume failure */
2480 if (wformat
== CF_METAFILEPICT
)
2482 LPMETAFILEPICT lpmfp
= GlobalLock(hdata
);
2483 unsigned int size
= GetMetaFileBitsEx(lpmfp
->hMF
, 0, NULL
);
2485 h
= GlobalAlloc(0, size
+ sizeof(METAFILEPICT
));
2488 char *pdata
= GlobalLock(h
);
2490 memcpy(pdata
, lpmfp
, sizeof(METAFILEPICT
));
2491 GetMetaFileBitsEx(lpmfp
->hMF
, size
, pdata
+ sizeof(METAFILEPICT
));
2493 *lpcbytes
= size
+ sizeof(METAFILEPICT
);
2498 GlobalUnlock(hdata
);
2500 else if (wformat
== CF_ENHMETAFILE
)
2502 int size
= GetEnhMetaFileBits(hdata
, 0, NULL
);
2504 h
= GlobalAlloc(0, size
);
2507 LPVOID pdata
= GlobalLock(h
);
2509 GetEnhMetaFileBits(hdata
, size
, pdata
);
2518 if (wformat
== CF_METAFILEPICT
)
2520 h
= GlobalAlloc(0, sizeof(METAFILEPICT
));
2523 unsigned int wiresize
;
2524 LPMETAFILEPICT lpmfp
= GlobalLock(h
);
2526 memcpy(lpmfp
, hdata
, sizeof(METAFILEPICT
));
2527 wiresize
= *lpcbytes
- sizeof(METAFILEPICT
);
2528 lpmfp
->hMF
= SetMetaFileBitsEx(wiresize
,
2529 ((const BYTE
*)hdata
) + sizeof(METAFILEPICT
));
2533 else if (wformat
== CF_ENHMETAFILE
)
2535 h
= SetEnhMetaFileBits(*lpcbytes
, hdata
);
2543 /**************************************************************************
2544 * X11DRV_CLIPBOARD_ReleaseSelection
2546 * Release XA_CLIPBOARD and XA_PRIMARY in response to a SelectionClear event.
2548 static void X11DRV_CLIPBOARD_ReleaseSelection(Display
*display
, Atom selType
, Window w
, HWND hwnd
, Time time
)
2550 /* w is the window that lost the selection
2552 TRACE("event->window = %08x (selectionWindow = %08x) selectionAcquired=0x%08x\n",
2553 (unsigned)w
, (unsigned)selectionWindow
, (unsigned)selectionAcquired
);
2555 if (selectionAcquired
&& (w
== selectionWindow
))
2557 CLIPBOARDINFO cbinfo
;
2559 /* completely give up the selection */
2560 TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
2562 X11DRV_CLIPBOARD_GetClipboardInfo(&cbinfo
);
2564 if (cbinfo
.flags
& CB_PROCESS
)
2566 /* Since we're still the owner, this wasn't initiated by
2567 another Wine process */
2568 if (OpenClipboard(hwnd
))
2570 /* Destroy private objects */
2571 SendMessageW(cbinfo
.hWndOwner
, WM_DESTROYCLIPBOARD
, 0, 0);
2573 /* Give up ownership of the windows clipboard */
2574 X11DRV_CLIPBOARD_ReleaseOwnership();
2579 if ((selType
== x11drv_atom(CLIPBOARD
)) && (selectionAcquired
& S_PRIMARY
))
2581 TRACE("Lost clipboard. Check if we need to release PRIMARY\n");
2583 if (selectionWindow
== XGetSelectionOwner(display
, XA_PRIMARY
))
2585 TRACE("We still own PRIMARY. Releasing PRIMARY.\n");
2586 XSetSelectionOwner(display
, XA_PRIMARY
, None
, time
);
2589 TRACE("We no longer own PRIMARY\n");
2591 else if ((selType
== XA_PRIMARY
) && (selectionAcquired
& S_CLIPBOARD
))
2593 TRACE("Lost PRIMARY. Check if we need to release CLIPBOARD\n");
2595 if (selectionWindow
== XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)))
2597 TRACE("We still own CLIPBOARD. Releasing CLIPBOARD.\n");
2598 XSetSelectionOwner(display
, x11drv_atom(CLIPBOARD
), None
, time
);
2601 TRACE("We no longer own CLIPBOARD\n");
2604 selectionWindow
= None
;
2606 X11DRV_EmptyClipboard(FALSE
);
2608 /* Reset the selection flags now that we are done */
2609 selectionAcquired
= S_NOSELECTION
;
2614 /**************************************************************************
2615 * IsSelectionOwner (X11DRV.@)
2617 * Returns: TRUE if the selection is owned by this process, FALSE otherwise
2619 static BOOL
X11DRV_CLIPBOARD_IsSelectionOwner(void)
2621 return selectionAcquired
;
2625 /**************************************************************************
2626 * X11DRV Clipboard Exports
2627 **************************************************************************/
2630 static void selection_acquire(void)
2635 owner
= thread_selection_wnd();
2636 display
= thread_display();
2638 selectionAcquired
= 0;
2639 selectionWindow
= 0;
2641 /* Grab PRIMARY selection if not owned */
2642 if (use_primary_selection
)
2643 XSetSelectionOwner(display
, XA_PRIMARY
, owner
, CurrentTime
);
2645 /* Grab CLIPBOARD selection if not owned */
2646 XSetSelectionOwner(display
, x11drv_atom(CLIPBOARD
), owner
, CurrentTime
);
2648 if (use_primary_selection
&& XGetSelectionOwner(display
, XA_PRIMARY
) == owner
)
2649 selectionAcquired
|= S_PRIMARY
;
2651 if (XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)) == owner
)
2652 selectionAcquired
|= S_CLIPBOARD
;
2654 if (selectionAcquired
)
2656 selectionWindow
= owner
;
2657 TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner
);
2661 static DWORD WINAPI
selection_thread_proc(LPVOID p
)
2667 selection_acquire();
2670 while (selectionAcquired
)
2672 MsgWaitForMultipleObjectsEx(0, NULL
, INFINITE
, QS_SENDMESSAGE
, 0);
2678 /**************************************************************************
2679 * AcquireClipboard (X11DRV.@)
2681 int CDECL
X11DRV_AcquireClipboard(HWND hWndClipWindow
)
2684 HANDLE selectionThread
;
2686 TRACE(" %p\n", hWndClipWindow
);
2689 * It's important that the selection get acquired from the thread
2690 * that owns the clipboard window. The primary reason is that we know
2691 * it is running a message loop and therefore can process the
2692 * X selection events.
2694 if (hWndClipWindow
&&
2695 GetCurrentThreadId() != GetWindowThreadProcessId(hWndClipWindow
, &procid
))
2697 if (procid
!= GetCurrentProcessId())
2699 WARN("Setting clipboard owner to other process is not supported\n");
2700 hWndClipWindow
= NULL
;
2704 TRACE("Thread %x is acquiring selection with thread %x's window %p\n",
2705 GetCurrentThreadId(),
2706 GetWindowThreadProcessId(hWndClipWindow
, NULL
), hWndClipWindow
);
2708 return SendMessageW(hWndClipWindow
, WM_X11DRV_ACQUIRE_SELECTION
, 0, 0);
2714 selection_acquire();
2718 HANDLE event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2719 selectionThread
= CreateThread(NULL
, 0, selection_thread_proc
, event
, 0, NULL
);
2721 if (!selectionThread
)
2723 WARN("Could not start clipboard thread\n");
2728 WaitForSingleObject(event
, INFINITE
);
2730 CloseHandle(selectionThread
);
2737 /**************************************************************************
2738 * X11DRV_EmptyClipboard
2740 * Empty cached clipboard data.
2742 void CDECL
X11DRV_EmptyClipboard(BOOL keepunowned
)
2744 WINE_CLIPDATA
*data
, *next
;
2746 LIST_FOR_EACH_ENTRY_SAFE( data
, next
, &data_list
, WINE_CLIPDATA
, entry
)
2748 if (keepunowned
&& (data
->wFlags
& CF_FLAG_UNOWNED
)) continue;
2749 list_remove( &data
->entry
);
2750 X11DRV_CLIPBOARD_FreeData( data
);
2751 HeapFree( GetProcessHeap(), 0, data
);
2755 TRACE(" %d entries remaining in cache.\n", ClipDataCount
);
2760 /**************************************************************************
2761 * X11DRV_SetClipboardData
2763 BOOL CDECL
X11DRV_SetClipboardData(UINT wFormat
, HANDLE hData
, BOOL owner
)
2766 BOOL bResult
= TRUE
;
2768 /* If it's not owned, data can only be set if the format data is not already owned
2769 and its rendering is not delayed */
2772 CLIPBOARDINFO cbinfo
;
2773 LPWINE_CLIPDATA lpRender
;
2775 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2778 ((lpRender
= X11DRV_CLIPBOARD_LookupData(wFormat
)) &&
2779 !(lpRender
->wFlags
& CF_FLAG_UNOWNED
)))
2782 flags
= CF_FLAG_UNOWNED
;
2785 bResult
&= X11DRV_CLIPBOARD_InsertClipboardData(wFormat
, hData
, flags
, NULL
, TRUE
);
2791 /**************************************************************************
2792 * CountClipboardFormats
2794 INT CDECL
X11DRV_CountClipboardFormats(void)
2796 CLIPBOARDINFO cbinfo
;
2798 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2800 TRACE(" count=%d\n", ClipDataCount
);
2802 return ClipDataCount
;
2806 /**************************************************************************
2807 * X11DRV_EnumClipboardFormats
2809 UINT CDECL
X11DRV_EnumClipboardFormats(UINT wFormat
)
2811 CLIPBOARDINFO cbinfo
;
2812 struct list
*ptr
= NULL
;
2814 TRACE("(%04X)\n", wFormat
);
2816 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2820 ptr
= list_head( &data_list
);
2824 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(wFormat
);
2825 if (lpData
) ptr
= list_next( &data_list
, &lpData
->entry
);
2829 return LIST_ENTRY( ptr
, WINE_CLIPDATA
, entry
)->wFormatID
;
2833 /**************************************************************************
2834 * X11DRV_IsClipboardFormatAvailable
2836 BOOL CDECL
X11DRV_IsClipboardFormatAvailable(UINT wFormat
)
2839 CLIPBOARDINFO cbinfo
;
2841 TRACE("(%04X)\n", wFormat
);
2843 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2845 if (wFormat
!= 0 && X11DRV_CLIPBOARD_LookupData(wFormat
))
2848 TRACE("(%04X)- ret(%d)\n", wFormat
, bRet
);
2854 /**************************************************************************
2855 * GetClipboardData (USER.142)
2857 HANDLE CDECL
X11DRV_GetClipboardData(UINT wFormat
)
2859 CLIPBOARDINFO cbinfo
;
2860 LPWINE_CLIPDATA lpRender
;
2862 TRACE("(%04X)\n", wFormat
);
2864 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2866 if ((lpRender
= X11DRV_CLIPBOARD_LookupData(wFormat
)))
2868 if ( !lpRender
->hData
)
2869 X11DRV_CLIPBOARD_RenderFormat(thread_init_display(), lpRender
);
2871 TRACE(" returning %p (type %04x)\n", lpRender
->hData
, lpRender
->wFormatID
);
2872 return lpRender
->hData
;
2879 /**************************************************************************
2880 * ResetSelectionOwner
2882 * Called when the thread owning the selection is destroyed and we need to
2883 * preserve the selection ownership. We look for another top level window
2884 * in this process and send it a message to acquire the selection.
2886 void X11DRV_ResetSelectionOwner(void)
2893 if (!selectionAcquired
|| thread_selection_wnd() != selectionWindow
)
2896 selectionAcquired
= S_NOSELECTION
;
2897 selectionWindow
= 0;
2899 hwnd
= GetWindow(GetDesktopWindow(), GW_CHILD
);
2902 if (GetCurrentThreadId() != GetWindowThreadProcessId(hwnd
, &procid
))
2904 if (GetCurrentProcessId() == procid
)
2906 if (SendMessageW(hwnd
, WM_X11DRV_ACQUIRE_SELECTION
, 0, 0))
2910 } while ((hwnd
= GetWindow(hwnd
, GW_HWNDNEXT
)) != NULL
);
2912 WARN("Failed to find another thread to take selection ownership. Clipboard data will be lost.\n");
2914 X11DRV_CLIPBOARD_ReleaseOwnership();
2915 X11DRV_EmptyClipboard(FALSE
);
2919 /**************************************************************************
2920 * X11DRV_CLIPBOARD_SynthesizeData
2922 static BOOL
X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID
)
2925 LPWINE_CLIPDATA lpSource
= NULL
;
2927 TRACE(" %04x\n", wFormatID
);
2929 /* Don't need to synthesize if it already exists */
2930 if (X11DRV_CLIPBOARD_LookupData(wFormatID
))
2933 if (wFormatID
== CF_UNICODETEXT
|| wFormatID
== CF_TEXT
|| wFormatID
== CF_OEMTEXT
)
2935 bsyn
= ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT
)) &&
2936 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) ||
2937 ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_TEXT
)) &&
2938 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) ||
2939 ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT
)) &&
2940 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
);
2942 else if (wFormatID
== CF_ENHMETAFILE
)
2944 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT
)) &&
2945 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2947 else if (wFormatID
== CF_METAFILEPICT
)
2949 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_ENHMETAFILE
)) &&
2950 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2952 else if (wFormatID
== CF_DIB
)
2954 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) &&
2955 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2957 else if (wFormatID
== CF_BITMAP
)
2959 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) &&
2960 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2964 X11DRV_CLIPBOARD_InsertClipboardData(wFormatID
, 0, CF_FLAG_SYNTHESIZED
, NULL
, TRUE
);
2971 /**************************************************************************
2972 * X11DRV_EndClipboardUpdate
2974 * Add locale if it hasn't already been added
2976 void CDECL
X11DRV_EndClipboardUpdate(void)
2978 INT count
= ClipDataCount
;
2980 /* Do Unicode <-> Text <-> OEM mapping */
2981 X11DRV_CLIPBOARD_SynthesizeData(CF_TEXT
);
2982 X11DRV_CLIPBOARD_SynthesizeData(CF_OEMTEXT
);
2983 X11DRV_CLIPBOARD_SynthesizeData(CF_UNICODETEXT
);
2985 /* Enhmetafile <-> MetafilePict mapping */
2986 X11DRV_CLIPBOARD_SynthesizeData(CF_ENHMETAFILE
);
2987 X11DRV_CLIPBOARD_SynthesizeData(CF_METAFILEPICT
);
2989 /* DIB <-> Bitmap mapping */
2990 X11DRV_CLIPBOARD_SynthesizeData(CF_DIB
);
2991 X11DRV_CLIPBOARD_SynthesizeData(CF_BITMAP
);
2993 TRACE("%d formats added to cached data\n", ClipDataCount
- count
);
2997 /***********************************************************************
2998 * X11DRV_SelectionRequest_TARGETS
2999 * Service a TARGETS selection request event
3001 static Atom
X11DRV_SelectionRequest_TARGETS( Display
*display
, Window requestor
,
3002 Atom target
, Atom rprop
)
3007 LPWINE_CLIPFORMAT format
;
3008 LPWINE_CLIPDATA lpData
;
3010 /* Create X atoms for any clipboard types which don't have atoms yet.
3011 * This avoids sending bogus zero atoms.
3012 * Without this, copying might not have access to all clipboard types.
3013 * FIXME: is it safe to call this here?
3018 * Count the number of items we wish to expose as selection targets.
3020 cTargets
= 1; /* Include TARGETS */
3022 if (!list_head( &data_list
)) return None
;
3024 LIST_FOR_EACH_ENTRY( lpData
, &data_list
, WINE_CLIPDATA
, entry
)
3025 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
3026 if ((format
->wFormatID
== lpData
->wFormatID
) &&
3027 format
->lpDrvExportFunc
&& format
->drvData
)
3030 TRACE(" found %d formats\n", cTargets
);
3032 /* Allocate temp buffer */
3033 targets
= HeapAlloc( GetProcessHeap(), 0, cTargets
* sizeof(Atom
));
3038 targets
[i
++] = x11drv_atom(TARGETS
);
3040 LIST_FOR_EACH_ENTRY( lpData
, &data_list
, WINE_CLIPDATA
, entry
)
3041 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
3042 if ((format
->wFormatID
== lpData
->wFormatID
) &&
3043 format
->lpDrvExportFunc
&& format
->drvData
)
3044 targets
[i
++] = format
->drvData
;
3046 if (TRACE_ON(clipboard
))
3049 for ( i
= 0; i
< cTargets
; i
++)
3051 char *itemFmtName
= XGetAtomName(display
, targets
[i
]);
3052 TRACE("\tAtom# %d: Property %ld Type %s\n", i
, targets
[i
], itemFmtName
);
3057 /* We may want to consider setting the type to xaTargets instead,
3058 * in case some apps expect this instead of XA_ATOM */
3059 XChangeProperty(display
, requestor
, rprop
, XA_ATOM
, 32,
3060 PropModeReplace
, (unsigned char *)targets
, cTargets
);
3062 HeapFree(GetProcessHeap(), 0, targets
);
3068 /***********************************************************************
3069 * X11DRV_SelectionRequest_MULTIPLE
3070 * Service a MULTIPLE selection request event
3071 * rprop contains a list of (target,property) atom pairs.
3072 * The first atom names a target and the second names a property.
3073 * The effect is as if we have received a sequence of SelectionRequest events
3074 * (one for each atom pair) except that:
3075 * 1. We reply with a SelectionNotify only when all the requested conversions
3076 * have been performed.
3077 * 2. If we fail to convert the target named by an atom in the MULTIPLE property,
3078 * we replace the atom in the property by None.
3080 static Atom
X11DRV_SelectionRequest_MULTIPLE( HWND hWnd
, XSelectionRequestEvent
*pevent
)
3082 Display
*display
= pevent
->display
;
3084 Atom atype
=AnyPropertyType
;
3086 unsigned long remain
;
3087 Atom
* targetPropList
=NULL
;
3088 unsigned long cTargetPropList
= 0;
3090 /* If the specified property is None the requestor is an obsolete client.
3091 * We support these by using the specified target atom as the reply property.
3093 rprop
= pevent
->property
;
3095 rprop
= pevent
->target
;
3099 /* Read the MULTIPLE property contents. This should contain a list of
3100 * (target,property) atom pairs.
3102 if (!XGetWindowProperty(display
, pevent
->requestor
, rprop
,
3103 0, 0x3FFF, False
, AnyPropertyType
, &atype
,&aformat
,
3104 &cTargetPropList
, &remain
,
3105 (unsigned char**)&targetPropList
) != Success
)
3107 if (TRACE_ON(clipboard
))
3109 char * const typeName
= XGetAtomName(display
, atype
);
3110 TRACE("\tType %s,Format %d,nItems %ld, Remain %ld\n",
3111 typeName
, aformat
, cTargetPropList
, remain
);
3116 * Make sure we got what we expect.
3117 * NOTE: According to the X-ICCCM Version 2.0 documentation the property sent
3118 * in a MULTIPLE selection request should be of type ATOM_PAIR.
3119 * However some X apps(such as XPaint) are not compliant with this and return
3120 * a user defined atom in atype when XGetWindowProperty is called.
3121 * The data *is* an atom pair but is not denoted as such.
3123 if(aformat
== 32 /* atype == xAtomPair */ )
3127 /* Iterate through the ATOM_PAIR list and execute a SelectionRequest
3128 * for each (target,property) pair */
3130 for (i
= 0; i
< cTargetPropList
; i
+=2)
3132 XSelectionRequestEvent event
;
3134 if (TRACE_ON(clipboard
))
3136 char *targetName
, *propName
;
3137 targetName
= XGetAtomName(display
, targetPropList
[i
]);
3138 propName
= XGetAtomName(display
, targetPropList
[i
+1]);
3139 TRACE("MULTIPLE(%d): Target='%s' Prop='%s'\n",
3140 i
/2, targetName
, propName
);
3145 /* We must have a non "None" property to service a MULTIPLE target atom */
3146 if ( !targetPropList
[i
+1] )
3148 TRACE("\tMULTIPLE(%d): Skipping target with empty property!\n", i
);
3152 /* Set up an XSelectionRequestEvent for this (target,property) pair */
3154 event
.target
= targetPropList
[i
];
3155 event
.property
= targetPropList
[i
+1];
3157 /* Fire a SelectionRequest, informing the handler that we are processing
3158 * a MULTIPLE selection request event.
3160 X11DRV_HandleSelectionRequest( hWnd
, &event
, TRUE
);
3164 /* Free the list of targets/properties */
3165 XFree(targetPropList
);
3167 else TRACE("Couldn't read MULTIPLE property\n");
3173 /***********************************************************************
3174 * X11DRV_HandleSelectionRequest
3175 * Process an event selection request event.
3176 * The bIsMultiple flag is used to signal when EVENT_SelectionRequest is called
3177 * recursively while servicing a "MULTIPLE" selection target.
3179 * Note: We only receive this event when WINE owns the X selection
3181 static void X11DRV_HandleSelectionRequest( HWND hWnd
, XSelectionRequestEvent
*event
, BOOL bIsMultiple
)
3183 Display
*display
= event
->display
;
3184 XSelectionEvent result
;
3186 Window request
= event
->requestor
;
3191 * We can only handle the selection request if :
3192 * The selection is PRIMARY or CLIPBOARD, AND we can successfully open the clipboard.
3193 * Don't do these checks or open the clipboard while recursively processing MULTIPLE,
3194 * since this has been already done.
3198 if (((event
->selection
!= XA_PRIMARY
) && (event
->selection
!= x11drv_atom(CLIPBOARD
))))
3202 /* If the specified property is None the requestor is an obsolete client.
3203 * We support these by using the specified target atom as the reply property.
3205 rprop
= event
->property
;
3207 rprop
= event
->target
;
3209 if(event
->target
== x11drv_atom(TARGETS
)) /* Return a list of all supported targets */
3211 /* TARGETS selection request */
3212 rprop
= X11DRV_SelectionRequest_TARGETS( display
, request
, event
->target
, rprop
);
3214 else if(event
->target
== x11drv_atom(MULTIPLE
)) /* rprop contains a list of (target, property) atom pairs */
3216 /* MULTIPLE selection request */
3217 rprop
= X11DRV_SelectionRequest_MULTIPLE( hWnd
, event
);
3221 LPWINE_CLIPFORMAT lpFormat
= X11DRV_CLIPBOARD_LookupProperty(NULL
, event
->target
);
3223 if (lpFormat
&& lpFormat
->lpDrvExportFunc
)
3225 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(lpFormat
->wFormatID
);
3229 unsigned char* lpClipData
;
3231 HANDLE hClipData
= lpFormat
->lpDrvExportFunc(display
, request
, event
->target
,
3232 rprop
, lpData
, &cBytes
);
3234 if (hClipData
&& (lpClipData
= GlobalLock(hClipData
)))
3236 int mode
= PropModeReplace
;
3238 TRACE("\tUpdating property %s, %d bytes\n",
3239 debugstr_format(lpFormat
->wFormatID
), cBytes
);
3242 int nelements
= min(cBytes
, 65536);
3243 XChangeProperty(display
, request
, rprop
, event
->target
,
3244 8, mode
, lpClipData
, nelements
);
3245 mode
= PropModeAppend
;
3246 cBytes
-= nelements
;
3247 lpClipData
+= nelements
;
3248 } while (cBytes
> 0);
3250 GlobalUnlock(hClipData
);
3251 GlobalFree(hClipData
);
3259 * SelectionNotify should be sent only at the end of a MULTIPLE request
3263 result
.type
= SelectionNotify
;
3264 result
.display
= display
;
3265 result
.requestor
= request
;
3266 result
.selection
= event
->selection
;
3267 result
.property
= rprop
;
3268 result
.target
= event
->target
;
3269 result
.time
= event
->time
;
3270 TRACE("Sending SelectionNotify event...\n");
3271 XSendEvent(display
,event
->requestor
,False
,NoEventMask
,(XEvent
*)&result
);
3276 /***********************************************************************
3277 * X11DRV_SelectionRequest
3279 void X11DRV_SelectionRequest( HWND hWnd
, XEvent
*event
)
3281 X11DRV_HandleSelectionRequest( hWnd
, &event
->xselectionrequest
, FALSE
);
3285 /***********************************************************************
3286 * X11DRV_SelectionClear
3288 void X11DRV_SelectionClear( HWND hWnd
, XEvent
*xev
)
3290 XSelectionClearEvent
*event
= &xev
->xselectionclear
;
3291 if (event
->selection
== XA_PRIMARY
|| event
->selection
== x11drv_atom(CLIPBOARD
))
3292 X11DRV_CLIPBOARD_ReleaseSelection( event
->display
, event
->selection
,
3293 event
->window
, hWnd
, event
->time
);