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
);
265 thread_data
->selection_wnd
= w
;
267 FIXME("Failed to create window. Fetching selection data will fail.\n");
273 static const char *debugstr_format( UINT id
)
277 if (GetClipboardFormatNameW( id
, buffer
, 256 ))
278 return wine_dbg_sprintf( "%04x %s", id
, debugstr_w(buffer
) );
282 #define BUILTIN(id) case id: return #id;
285 BUILTIN(CF_METAFILEPICT
)
295 BUILTIN(CF_UNICODETEXT
)
296 BUILTIN(CF_ENHMETAFILE
)
300 BUILTIN(CF_OWNERDISPLAY
)
302 BUILTIN(CF_DSPBITMAP
)
303 BUILTIN(CF_DSPMETAFILEPICT
)
304 BUILTIN(CF_DSPENHMETAFILE
)
306 default: return wine_dbg_sprintf( "%04x", id
);
310 /**************************************************************************
311 * X11DRV_InitClipboard
313 void X11DRV_InitClipboard(void)
316 WINE_CLIPFORMAT
*format
;
318 /* Register built-in formats */
319 for (i
= 0; i
< sizeof(builtin_formats
)/sizeof(builtin_formats
[0]); i
++)
321 if (!(format
= HeapAlloc( GetProcessHeap(), 0, sizeof(*format
)))) break;
322 format
->wFormatID
= builtin_formats
[i
].id
;
323 format
->drvData
= GET_ATOM(builtin_formats
[i
].data
);
324 format
->lpDrvImportFunc
= builtin_formats
[i
].import
;
325 format
->lpDrvExportFunc
= builtin_formats
[i
].export
;
326 list_add_tail( &format_list
, &format
->entry
);
329 /* Register known mapping between window formats and X properties */
330 for (i
= 0; i
< sizeof(PropertyFormatMap
)/sizeof(PropertyFormatMap
[0]); i
++)
331 X11DRV_CLIPBOARD_InsertClipboardFormat( RegisterClipboardFormatW(PropertyFormatMap
[i
].lpszFormat
),
332 GET_ATOM(PropertyFormatMap
[i
].prop
));
334 /* Set up a conversion function from "HTML Format" to "text/html" */
335 format
= X11DRV_CLIPBOARD_InsertClipboardFormat( RegisterClipboardFormatW(wszHTMLFormat
),
336 GET_ATOM(XATOM_text_html
));
337 format
->lpDrvExportFunc
= X11DRV_CLIPBOARD_ExportTextHtml
;
341 /**************************************************************************
344 * Intern atoms for formats that don't have one yet.
346 static void intern_atoms(void)
348 LPWINE_CLIPFORMAT format
;
356 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
357 if (!format
->drvData
) count
++;
360 display
= thread_init_display();
362 names
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*names
) );
363 atoms
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*atoms
) );
366 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
367 if (!format
->drvData
) {
368 GetClipboardFormatNameW( format
->wFormatID
, buffer
, 256 );
369 len
= WideCharToMultiByte(CP_UNIXCP
, 0, buffer
, -1, NULL
, 0, NULL
, NULL
);
370 names
[i
] = HeapAlloc(GetProcessHeap(), 0, len
);
371 WideCharToMultiByte(CP_UNIXCP
, 0, buffer
, -1, names
[i
++], len
, NULL
, NULL
);
374 XInternAtoms( display
, names
, count
, False
, atoms
);
377 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
378 if (!format
->drvData
) {
379 HeapFree(GetProcessHeap(), 0, names
[i
]);
380 format
->drvData
= atoms
[i
++];
383 HeapFree( GetProcessHeap(), 0, names
);
384 HeapFree( GetProcessHeap(), 0, atoms
);
388 /**************************************************************************
391 * Register a custom X clipboard format.
393 static WINE_CLIPFORMAT
*register_format( UINT id
, Atom prop
)
395 LPWINE_CLIPFORMAT lpFormat
;
397 /* walk format chain to see if it's already registered */
398 LIST_FOR_EACH_ENTRY( lpFormat
, &format_list
, WINE_CLIPFORMAT
, entry
)
399 if (lpFormat
->wFormatID
== id
) return lpFormat
;
401 return X11DRV_CLIPBOARD_InsertClipboardFormat(id
, prop
);
405 /**************************************************************************
406 * X11DRV_CLIPBOARD_LookupProperty
408 static LPWINE_CLIPFORMAT
X11DRV_CLIPBOARD_LookupProperty(LPWINE_CLIPFORMAT current
, UINT drvData
)
412 struct list
*ptr
= current
? ¤t
->entry
: &format_list
;
413 BOOL need_intern
= FALSE
;
415 while ((ptr
= list_next( &format_list
, ptr
)))
417 LPWINE_CLIPFORMAT lpFormat
= LIST_ENTRY( ptr
, WINE_CLIPFORMAT
, entry
);
418 if (lpFormat
->drvData
== drvData
) return lpFormat
;
419 if (!lpFormat
->drvData
) need_intern
= TRUE
;
421 if (!need_intern
) return NULL
;
423 /* restart the search for the new atoms */
428 /**************************************************************************
429 * X11DRV_CLIPBOARD_LookupData
431 static LPWINE_CLIPDATA
X11DRV_CLIPBOARD_LookupData(DWORD wID
)
435 LIST_FOR_EACH_ENTRY( data
, &data_list
, WINE_CLIPDATA
, entry
)
436 if (data
->wFormatID
== wID
) return data
;
442 /**************************************************************************
443 * InsertClipboardFormat
445 static WINE_CLIPFORMAT
*X11DRV_CLIPBOARD_InsertClipboardFormat( UINT id
, Atom prop
)
447 LPWINE_CLIPFORMAT lpNewFormat
;
449 /* allocate storage for new format entry */
450 lpNewFormat
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPFORMAT
));
452 if(lpNewFormat
== NULL
)
454 WARN("No more memory for a new format!\n");
457 lpNewFormat
->wFormatID
= id
;
458 lpNewFormat
->drvData
= prop
;
459 lpNewFormat
->lpDrvImportFunc
= X11DRV_CLIPBOARD_ImportClipboardData
;
460 lpNewFormat
->lpDrvExportFunc
= X11DRV_CLIPBOARD_ExportClipboardData
;
462 list_add_tail( &format_list
, &lpNewFormat
->entry
);
464 TRACE("Registering format %s drvData %d\n",
465 debugstr_format(lpNewFormat
->wFormatID
), lpNewFormat
->drvData
);
473 /**************************************************************************
474 * X11DRV_CLIPBOARD_GetClipboardInfo
476 static BOOL
X11DRV_CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo
)
480 SERVER_START_REQ( set_clipboard_info
)
484 if (wine_server_call_err( req
))
486 ERR("Failed to get clipboard owner.\n");
490 cbInfo
->hWndOpen
= wine_server_ptr_handle( reply
->old_clipboard
);
491 cbInfo
->hWndOwner
= wine_server_ptr_handle( reply
->old_owner
);
492 cbInfo
->hWndViewer
= wine_server_ptr_handle( reply
->old_viewer
);
493 cbInfo
->seqno
= reply
->seqno
;
494 cbInfo
->flags
= reply
->flags
;
505 /**************************************************************************
506 * X11DRV_CLIPBOARD_ReleaseOwnership
508 static BOOL
X11DRV_CLIPBOARD_ReleaseOwnership(void)
512 SERVER_START_REQ( set_clipboard_info
)
514 req
->flags
= SET_CB_RELOWNER
| SET_CB_SEQNO
;
516 if (wine_server_call_err( req
))
518 ERR("Failed to set clipboard.\n");
532 /**************************************************************************
533 * X11DRV_CLIPBOARD_InsertClipboardData
535 * Caller *must* have the clipboard open and be the owner.
537 static BOOL
X11DRV_CLIPBOARD_InsertClipboardData(UINT wFormatID
, HANDLE hData
, DWORD flags
,
538 LPWINE_CLIPFORMAT lpFormat
, BOOL override
)
540 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(wFormatID
);
542 TRACE("format=%04x lpData=%p hData=%p flags=0x%08x lpFormat=%p override=%d\n",
543 wFormatID
, lpData
, hData
, flags
, lpFormat
, override
);
545 /* make sure the format exists */
546 if (!lpFormat
) register_format( wFormatID
, 0 );
548 if (lpData
&& !override
)
553 X11DRV_CLIPBOARD_FreeData(lpData
);
555 lpData
->hData
= hData
;
559 lpData
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPDATA
));
561 lpData
->wFormatID
= wFormatID
;
562 lpData
->hData
= hData
;
563 lpData
->lpFormat
= lpFormat
;
566 list_add_tail( &data_list
, &lpData
->entry
);
570 lpData
->wFlags
= flags
;
576 /**************************************************************************
577 * X11DRV_CLIPBOARD_FreeData
579 * Free clipboard data handle.
581 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData
)
583 TRACE("%04x\n", lpData
->wFormatID
);
585 if ((lpData
->wFormatID
>= CF_GDIOBJFIRST
&&
586 lpData
->wFormatID
<= CF_GDIOBJLAST
) ||
587 lpData
->wFormatID
== CF_BITMAP
||
588 lpData
->wFormatID
== CF_DIB
||
589 lpData
->wFormatID
== CF_PALETTE
)
592 DeleteObject(lpData
->hData
);
594 if ((lpData
->wFormatID
== CF_DIB
) && lpData
->drvData
)
595 XFreePixmap(gdi_display
, lpData
->drvData
);
597 else if (lpData
->wFormatID
== CF_METAFILEPICT
)
601 DeleteMetaFile(((METAFILEPICT
*)GlobalLock( lpData
->hData
))->hMF
);
602 GlobalFree(lpData
->hData
);
605 else if (lpData
->wFormatID
== CF_ENHMETAFILE
)
608 DeleteEnhMetaFile(lpData
->hData
);
610 else if (lpData
->wFormatID
< CF_PRIVATEFIRST
||
611 lpData
->wFormatID
> CF_PRIVATELAST
)
614 GlobalFree(lpData
->hData
);
622 /**************************************************************************
623 * X11DRV_CLIPBOARD_UpdateCache
625 static BOOL
X11DRV_CLIPBOARD_UpdateCache(LPCLIPBOARDINFO lpcbinfo
)
629 if (!X11DRV_CLIPBOARD_IsSelectionOwner())
631 if (!X11DRV_CLIPBOARD_GetClipboardInfo(lpcbinfo
))
633 ERR("Failed to retrieve clipboard information.\n");
636 else if (wSeqNo
< lpcbinfo
->seqno
)
638 X11DRV_EmptyClipboard(TRUE
);
640 if (X11DRV_CLIPBOARD_QueryAvailableData(thread_init_display(), lpcbinfo
) < 0)
642 ERR("Failed to cache clipboard data owned by another process.\n");
647 X11DRV_EndClipboardUpdate();
650 wSeqNo
= lpcbinfo
->seqno
;
658 /**************************************************************************
659 * X11DRV_CLIPBOARD_RenderFormat
661 static BOOL
X11DRV_CLIPBOARD_RenderFormat(Display
*display
, LPWINE_CLIPDATA lpData
)
665 TRACE(" 0x%04x hData(%p)\n", lpData
->wFormatID
, lpData
->hData
);
667 if (lpData
->hData
) return bret
; /* Already rendered */
669 if (lpData
->wFlags
& CF_FLAG_SYNTHESIZED
)
670 bret
= X11DRV_CLIPBOARD_RenderSynthesizedFormat(display
, lpData
);
671 else if (!X11DRV_CLIPBOARD_IsSelectionOwner())
673 if (!X11DRV_CLIPBOARD_ReadSelectionData(display
, lpData
))
675 ERR("Failed to cache clipboard data owned by another process. Format=%04x\n",
682 CLIPBOARDINFO cbInfo
;
684 if (X11DRV_CLIPBOARD_GetClipboardInfo(&cbInfo
) && cbInfo
.hWndOwner
)
686 /* Send a WM_RENDERFORMAT message to notify the owner to render the
687 * data requested into the clipboard.
689 TRACE("Sending WM_RENDERFORMAT message to hwnd(%p)\n", cbInfo
.hWndOwner
);
690 SendMessageW(cbInfo
.hWndOwner
, WM_RENDERFORMAT
, lpData
->wFormatID
, 0);
692 if (!lpData
->hData
) bret
= FALSE
;
696 ERR("hWndClipOwner is lost!\n");
705 /**************************************************************************
706 * CLIPBOARD_ConvertText
707 * Returns number of required/converted characters - not bytes!
709 static INT
CLIPBOARD_ConvertText(WORD src_fmt
, void const *src
, INT src_size
,
710 WORD dst_fmt
, void *dst
, INT dst_size
)
714 if(src_fmt
== CF_UNICODETEXT
)
727 return WideCharToMultiByte(cp
, 0, src
, src_size
, dst
, dst_size
, NULL
, NULL
);
730 if(dst_fmt
== CF_UNICODETEXT
)
743 return MultiByteToWideChar(cp
, 0, src
, src_size
, dst
, dst_size
);
746 if(!dst_size
) return src_size
;
748 if(dst_size
> src_size
) dst_size
= src_size
;
750 if(src_fmt
== CF_TEXT
)
751 CharToOemBuffA(src
, dst
, dst_size
);
753 OemToCharBuffA(src
, dst
, dst_size
);
759 /**************************************************************************
760 * X11DRV_CLIPBOARD_RenderSynthesizedFormat
762 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedFormat(Display
*display
, LPWINE_CLIPDATA lpData
)
768 if (lpData
->wFlags
& CF_FLAG_SYNTHESIZED
)
770 UINT wFormatID
= lpData
->wFormatID
;
772 if (wFormatID
== CF_UNICODETEXT
|| wFormatID
== CF_TEXT
|| wFormatID
== CF_OEMTEXT
)
773 bret
= X11DRV_CLIPBOARD_RenderSynthesizedText(display
, wFormatID
);
779 bret
= X11DRV_CLIPBOARD_RenderSynthesizedDIB( display
);
783 bret
= X11DRV_CLIPBOARD_RenderSynthesizedBitmap( display
);
787 bret
= X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile( display
);
790 case CF_METAFILEPICT
:
791 FIXME("Synthesizing CF_METAFILEPICT not implemented\n");
795 FIXME("Called to synthesize unknown format 0x%08x\n", wFormatID
);
800 lpData
->wFlags
&= ~CF_FLAG_SYNTHESIZED
;
807 /**************************************************************************
808 * X11DRV_CLIPBOARD_RenderSynthesizedText
810 * Renders synthesized text
812 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedText(Display
*display
, UINT wFormatID
)
817 INT src_chars
, dst_chars
, alloc_size
;
818 LPWINE_CLIPDATA lpSource
= NULL
;
820 TRACE("%04x\n", wFormatID
);
822 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(wFormatID
)) &&
826 /* Look for rendered source or non-synthesized source */
827 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT
)) &&
828 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
830 TRACE("UNICODETEXT -> %04x\n", wFormatID
);
832 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_TEXT
)) &&
833 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
835 TRACE("TEXT -> %04x\n", wFormatID
);
837 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT
)) &&
838 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
840 TRACE("OEMTEXT -> %04x\n", wFormatID
);
843 if (!lpSource
|| (lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
&&
847 /* Ask the clipboard owner to render the source text if necessary */
848 if (!lpSource
->hData
&& !X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
851 lpstrS
= GlobalLock(lpSource
->hData
);
855 /* Text always NULL terminated */
856 if(lpSource
->wFormatID
== CF_UNICODETEXT
)
857 src_chars
= strlenW((LPCWSTR
)lpstrS
) + 1;
859 src_chars
= strlen(lpstrS
) + 1;
861 /* Calculate number of characters in the destination buffer */
862 dst_chars
= CLIPBOARD_ConvertText(lpSource
->wFormatID
, lpstrS
,
863 src_chars
, wFormatID
, NULL
, 0);
868 TRACE("Converting from '%04x' to '%04x', %i chars\n",
869 lpSource
->wFormatID
, wFormatID
, src_chars
);
871 /* Convert characters to bytes */
872 if(wFormatID
== CF_UNICODETEXT
)
873 alloc_size
= dst_chars
* sizeof(WCHAR
);
875 alloc_size
= dst_chars
;
877 hData
= GlobalAlloc(GMEM_ZEROINIT
| GMEM_MOVEABLE
|
878 GMEM_DDESHARE
, alloc_size
);
880 lpstrT
= GlobalLock(hData
);
884 CLIPBOARD_ConvertText(lpSource
->wFormatID
, lpstrS
, src_chars
,
885 wFormatID
, lpstrT
, dst_chars
);
889 GlobalUnlock(lpSource
->hData
);
891 return X11DRV_CLIPBOARD_InsertClipboardData(wFormatID
, hData
, 0, NULL
, TRUE
);
895 /***********************************************************************
898 * Return the size of the bitmap info structure including color table.
900 static int bitmap_info_size( const BITMAPINFO
* info
, WORD coloruse
)
902 unsigned int colors
, size
, masks
= 0;
904 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
906 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
907 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
908 return sizeof(BITMAPCOREHEADER
) + colors
*
909 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
911 else /* assume BITMAPINFOHEADER */
913 colors
= info
->bmiHeader
.biClrUsed
;
914 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
915 colors
= 1 << info
->bmiHeader
.biBitCount
;
916 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
917 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
918 return size
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
923 /***********************************************************************
924 * create_dib_from_bitmap
926 * Allocates a packed DIB and copies the bitmap data into it.
928 static HGLOBAL
create_dib_from_bitmap(HBITMAP hBmp
)
934 LPBITMAPINFOHEADER pbmiHeader
;
935 unsigned int cDataSize
, cPackedSize
, OffsetBits
;
938 if (!GetObjectW( hBmp
, sizeof(bmp
), &bmp
)) return 0;
941 * A packed DIB contains a BITMAPINFO structure followed immediately by
942 * an optional color palette and the pixel data.
945 /* Calculate the size of the packed DIB */
946 cDataSize
= abs( bmp
.bmHeight
) * (((bmp
.bmWidth
* bmp
.bmBitsPixel
+ 31) / 8) & ~3);
947 cPackedSize
= sizeof(BITMAPINFOHEADER
)
948 + ( (bmp
.bmBitsPixel
<= 8) ? (sizeof(RGBQUAD
) * (1 << bmp
.bmBitsPixel
)) : 0 )
950 /* Get the offset to the bits */
951 OffsetBits
= cPackedSize
- cDataSize
;
953 /* Allocate the packed DIB */
954 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize
);
955 hPackedDIB
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
/*| GMEM_ZEROINIT*/,
959 WARN("Could not allocate packed DIB!\n");
963 /* A packed DIB starts with a BITMAPINFOHEADER */
964 pPackedDIB
= GlobalLock(hPackedDIB
);
965 pbmiHeader
= (LPBITMAPINFOHEADER
)pPackedDIB
;
967 /* Init the BITMAPINFOHEADER */
968 pbmiHeader
->biSize
= sizeof(BITMAPINFOHEADER
);
969 pbmiHeader
->biWidth
= bmp
.bmWidth
;
970 pbmiHeader
->biHeight
= bmp
.bmHeight
;
971 pbmiHeader
->biPlanes
= 1;
972 pbmiHeader
->biBitCount
= bmp
.bmBitsPixel
;
973 pbmiHeader
->biCompression
= BI_RGB
;
974 pbmiHeader
->biSizeImage
= 0;
975 pbmiHeader
->biXPelsPerMeter
= pbmiHeader
->biYPelsPerMeter
= 0;
976 pbmiHeader
->biClrUsed
= 0;
977 pbmiHeader
->biClrImportant
= 0;
979 /* Retrieve the DIB bits from the bitmap and fill in the
980 * DIB color table if present */
982 nLinesCopied
= GetDIBits(hdc
, /* Handle to device context */
983 hBmp
, /* Handle to bitmap */
984 0, /* First scan line to set in dest bitmap */
985 bmp
.bmHeight
, /* Number of scan lines to copy */
986 pPackedDIB
+ OffsetBits
, /* [out] Address of array for bitmap bits */
987 (LPBITMAPINFO
) pbmiHeader
, /* [out] Address of BITMAPINFO structure */
988 0); /* RGB or palette index */
989 GlobalUnlock(hPackedDIB
);
992 /* Cleanup if GetDIBits failed */
993 if (nLinesCopied
!= bmp
.bmHeight
)
995 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied
, bmp
.bmHeight
);
996 GlobalFree(hPackedDIB
);
1003 /**************************************************************************
1004 * X11DRV_CLIPBOARD_RenderSynthesizedDIB
1006 * Renders synthesized DIB
1008 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedDIB(Display
*display
)
1011 LPWINE_CLIPDATA lpSource
= NULL
;
1015 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) && lpSource
->hData
)
1019 /* If we have a bitmap and it's not synthesized or it has been rendered */
1020 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) &&
1021 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
1023 /* Render source if required */
1024 if (lpSource
->hData
|| X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
1026 HGLOBAL hData
= create_dib_from_bitmap( lpSource
->hData
);
1029 X11DRV_CLIPBOARD_InsertClipboardData(CF_DIB
, hData
, 0, NULL
, TRUE
);
1039 /**************************************************************************
1040 * X11DRV_CLIPBOARD_RenderSynthesizedBitmap
1042 * Renders synthesized bitmap
1044 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedBitmap(Display
*display
)
1047 LPWINE_CLIPDATA lpSource
= NULL
;
1051 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) && lpSource
->hData
)
1055 /* If we have a dib and it's not synthesized or it has been rendered */
1056 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) &&
1057 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
1059 /* Render source if required */
1060 if (lpSource
->hData
|| X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
1063 HBITMAP hData
= NULL
;
1064 unsigned int offset
;
1065 LPBITMAPINFOHEADER lpbmih
;
1068 lpbmih
= GlobalLock(lpSource
->hData
);
1071 offset
= sizeof(BITMAPINFOHEADER
)
1072 + ((lpbmih
->biBitCount
<= 8) ? (sizeof(RGBQUAD
) *
1073 (1 << lpbmih
->biBitCount
)) : 0);
1075 hData
= CreateDIBitmap(hdc
, lpbmih
, CBM_INIT
, (LPBYTE
)lpbmih
+
1076 offset
, (LPBITMAPINFO
) lpbmih
, DIB_RGB_COLORS
);
1078 GlobalUnlock(lpSource
->hData
);
1080 ReleaseDC(NULL
, hdc
);
1084 X11DRV_CLIPBOARD_InsertClipboardData(CF_BITMAP
, hData
, 0, NULL
, TRUE
);
1094 /**************************************************************************
1095 * X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile
1097 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile(Display
*display
)
1099 LPWINE_CLIPDATA lpSource
= NULL
;
1103 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_ENHMETAFILE
)) && lpSource
->hData
)
1105 /* If we have a MF pict and it's not synthesized or it has been rendered */
1106 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT
)) &&
1107 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
1109 /* Render source if required */
1110 if (lpSource
->hData
|| X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
1113 HENHMETAFILE hData
= NULL
;
1115 pmfp
= GlobalLock(lpSource
->hData
);
1118 UINT size_mf_bits
= GetMetaFileBitsEx(pmfp
->hMF
, 0, NULL
);
1119 void *mf_bits
= HeapAlloc(GetProcessHeap(), 0, size_mf_bits
);
1122 GetMetaFileBitsEx(pmfp
->hMF
, size_mf_bits
, mf_bits
);
1123 hData
= SetWinMetaFileBits(size_mf_bits
, mf_bits
, NULL
, pmfp
);
1124 HeapFree(GetProcessHeap(), 0, mf_bits
);
1126 GlobalUnlock(lpSource
->hData
);
1131 X11DRV_CLIPBOARD_InsertClipboardData(CF_ENHMETAFILE
, hData
, 0, NULL
, TRUE
);
1141 /**************************************************************************
1142 * X11DRV_CLIPBOARD_ImportXAString
1144 * Import XA_STRING, converting the string to CF_TEXT.
1146 static HANDLE
X11DRV_CLIPBOARD_ImportXAString(Display
*display
, Window w
, Atom prop
)
1149 unsigned long cbytes
;
1151 unsigned long i
, inlcount
= 0;
1154 if (!X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1157 for (i
= 0; i
<= cbytes
; i
++)
1159 if (lpdata
[i
] == '\n')
1163 if ((hText
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cbytes
+ inlcount
+ 1)))
1165 lpstr
= GlobalLock(hText
);
1167 for (i
= 0, inlcount
= 0; i
<= cbytes
; i
++)
1169 if (lpdata
[i
] == '\n')
1170 lpstr
[inlcount
++] = '\r';
1172 lpstr
[inlcount
++] = lpdata
[i
];
1175 GlobalUnlock(hText
);
1178 /* Free the retrieved property data */
1179 HeapFree(GetProcessHeap(), 0, lpdata
);
1185 /**************************************************************************
1186 * X11DRV_CLIPBOARD_ImportUTF8
1188 * Import XA_STRING, converting the string to CF_UNICODE.
1190 static HANDLE
X11DRV_CLIPBOARD_ImportUTF8(Display
*display
, Window w
, Atom prop
)
1193 unsigned long cbytes
;
1195 unsigned long i
, inlcount
= 0;
1196 HANDLE hUnicodeText
= 0;
1198 if (!X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1201 for (i
= 0; i
<= cbytes
; i
++)
1203 if (lpdata
[i
] == '\n')
1207 if ((lpstr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cbytes
+ inlcount
+ 1)))
1211 for (i
= 0, inlcount
= 0; i
<= cbytes
; i
++)
1213 if (lpdata
[i
] == '\n')
1214 lpstr
[inlcount
++] = '\r';
1216 lpstr
[inlcount
++] = lpdata
[i
];
1219 count
= MultiByteToWideChar(CP_UTF8
, 0, lpstr
, -1, NULL
, 0);
1220 hUnicodeText
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, count
* sizeof(WCHAR
));
1224 WCHAR
*textW
= GlobalLock(hUnicodeText
);
1225 MultiByteToWideChar(CP_UTF8
, 0, lpstr
, -1, textW
, count
);
1226 GlobalUnlock(hUnicodeText
);
1229 HeapFree(GetProcessHeap(), 0, lpstr
);
1232 /* Free the retrieved property data */
1233 HeapFree(GetProcessHeap(), 0, lpdata
);
1235 return hUnicodeText
;
1239 /**************************************************************************
1240 * X11DRV_CLIPBOARD_ImportCompoundText
1242 * Import COMPOUND_TEXT to CF_UNICODE
1244 static HANDLE
X11DRV_CLIPBOARD_ImportCompoundText(Display
*display
, Window w
, Atom prop
)
1249 int srclen
, destlen
;
1250 HANDLE hUnicodeText
;
1251 XTextProperty txtprop
;
1253 if (!X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &txtprop
.value
, &txtprop
.nitems
))
1258 txtprop
.encoding
= x11drv_atom(COMPOUND_TEXT
);
1260 ret
= XmbTextPropertyToTextList(display
, &txtprop
, &srcstr
, &count
);
1261 HeapFree(GetProcessHeap(), 0, txtprop
.value
);
1262 if (ret
!= Success
|| !count
) return 0;
1264 TRACE("Importing %d line(s)\n", count
);
1266 /* Compute number of lines */
1267 srclen
= strlen(srcstr
[0]);
1268 for (i
= 0, lcount
= 0; i
<= srclen
; i
++)
1270 if (srcstr
[0][i
] == '\n')
1274 destlen
= MultiByteToWideChar(CP_UNIXCP
, 0, srcstr
[0], -1, NULL
, 0);
1276 TRACE("lcount = %d, destlen=%d, srcstr %s\n", lcount
, destlen
, srcstr
[0]);
1278 if ((hUnicodeText
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, (destlen
+ lcount
+ 1) * sizeof(WCHAR
))))
1280 WCHAR
*deststr
= GlobalLock(hUnicodeText
);
1281 MultiByteToWideChar(CP_UNIXCP
, 0, srcstr
[0], -1, deststr
, destlen
);
1285 for (i
= destlen
- 1, j
= destlen
+ lcount
- 1; i
>= 0; i
--, j
--)
1287 deststr
[j
] = deststr
[i
];
1289 if (deststr
[i
] == '\n')
1290 deststr
[--j
] = '\r';
1294 GlobalUnlock(hUnicodeText
);
1297 XFreeStringList(srcstr
);
1299 return hUnicodeText
;
1303 /**************************************************************************
1304 * X11DRV_CLIPBOARD_ImportXAPIXMAP
1306 * Import XA_PIXMAP, converting the image to CF_DIB.
1308 static HANDLE
X11DRV_CLIPBOARD_ImportXAPIXMAP(Display
*display
, Window w
, Atom prop
)
1311 unsigned long cbytes
;
1313 HANDLE hClipData
= 0;
1315 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1317 XVisualInfo vis
= default_visual
;
1318 char buffer
[FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )];
1319 BITMAPINFO
*info
= (BITMAPINFO
*)buffer
;
1320 struct gdi_image_bits bits
;
1322 int x
,y
; /* Unused */
1323 unsigned border_width
; /* Unused */
1324 unsigned int depth
, width
, height
;
1326 pPixmap
= (Pixmap
*) lpdata
;
1328 /* Get the Pixmap dimensions and bit depth */
1329 if (!XGetGeometry(gdi_display
, *pPixmap
, &root
, &x
, &y
, &width
, &height
,
1330 &border_width
, &depth
)) depth
= 0;
1331 if (!pixmap_formats
[depth
]) return 0;
1333 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
1334 width
, height
, depth
);
1336 if (depth
!= vis
.depth
) switch (pixmap_formats
[depth
]->bits_per_pixel
)
1342 case 16: /* assume R5G5B5 */
1343 vis
.red_mask
= 0x7c00;
1344 vis
.green_mask
= 0x03e0;
1345 vis
.blue_mask
= 0x001f;
1347 case 24: /* assume R8G8B8 */
1348 case 32: /* assume A8R8G8B8 */
1349 vis
.red_mask
= 0xff0000;
1350 vis
.green_mask
= 0x00ff00;
1351 vis
.blue_mask
= 0x0000ff;
1357 if (!get_pixmap_image( *pPixmap
, width
, height
, &vis
, info
, &bits
))
1359 DWORD info_size
= bitmap_info_size( info
, DIB_RGB_COLORS
);
1362 hClipData
= GlobalAlloc( GMEM_MOVEABLE
| GMEM_DDESHARE
,
1363 info_size
+ info
->bmiHeader
.biSizeImage
);
1366 ptr
= GlobalLock( hClipData
);
1367 memcpy( ptr
, info
, info_size
);
1368 memcpy( ptr
+ info_size
, bits
.ptr
, info
->bmiHeader
.biSizeImage
);
1369 GlobalUnlock( hClipData
);
1371 if (bits
.free
) bits
.free( &bits
);
1379 /**************************************************************************
1380 * X11DRV_CLIPBOARD_ImportImageBmp
1382 * Import image/bmp, converting the image to CF_DIB.
1384 static HANDLE
X11DRV_CLIPBOARD_ImportImageBmp(Display
*display
, Window w
, Atom prop
)
1387 unsigned long cbytes
;
1388 HANDLE hClipData
= 0;
1390 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1392 BITMAPFILEHEADER
*bfh
= (BITMAPFILEHEADER
*)lpdata
;
1394 if (cbytes
>= sizeof(BITMAPFILEHEADER
)+sizeof(BITMAPCOREHEADER
) &&
1395 bfh
->bfType
== 0x4d42 /* "BM" */)
1397 BITMAPINFO
*bmi
= (BITMAPINFO
*)(bfh
+1);
1402 hbmp
= CreateDIBitmap(
1406 lpdata
+bfh
->bfOffBits
,
1411 hClipData
= create_dib_from_bitmap( hbmp
);
1417 /* Free the retrieved property data */
1418 HeapFree(GetProcessHeap(), 0, lpdata
);
1425 /**************************************************************************
1426 * X11DRV_CLIPBOARD_ImportMetaFilePict
1428 * Import MetaFilePict.
1430 static HANDLE
X11DRV_CLIPBOARD_ImportMetaFilePict(Display
*display
, Window w
, Atom prop
)
1433 unsigned long cbytes
;
1434 HANDLE hClipData
= 0;
1436 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1439 hClipData
= X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT
, lpdata
, (LPDWORD
)&cbytes
, FALSE
);
1441 /* Free the retrieved property data */
1442 HeapFree(GetProcessHeap(), 0, lpdata
);
1449 /**************************************************************************
1450 * X11DRV_ImportEnhMetaFile
1452 * Import EnhMetaFile.
1454 static HANDLE
X11DRV_CLIPBOARD_ImportEnhMetaFile(Display
*display
, Window w
, Atom prop
)
1457 unsigned long cbytes
;
1458 HANDLE hClipData
= 0;
1460 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1463 hClipData
= X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE
, lpdata
, (LPDWORD
)&cbytes
, FALSE
);
1465 /* Free the retrieved property data */
1466 HeapFree(GetProcessHeap(), 0, lpdata
);
1473 /**************************************************************************
1474 * X11DRV_ImportClipbordaData
1476 * Generic import clipboard data routine.
1478 static HANDLE
X11DRV_CLIPBOARD_ImportClipboardData(Display
*display
, Window w
, Atom prop
)
1482 unsigned long cbytes
;
1483 HANDLE hClipData
= 0;
1485 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1489 /* Turn on the DDESHARE flag to enable shared 32 bit memory */
1490 hClipData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cbytes
);
1494 if ((lpClipData
= GlobalLock(hClipData
)))
1496 memcpy(lpClipData
, lpdata
, cbytes
);
1497 GlobalUnlock(hClipData
);
1501 GlobalFree(hClipData
);
1506 /* Free the retrieved property data */
1507 HeapFree(GetProcessHeap(), 0, lpdata
);
1514 /**************************************************************************
1515 X11DRV_CLIPBOARD_ExportClipboardData
1517 * Generic export clipboard data routine.
1519 static HANDLE
X11DRV_CLIPBOARD_ExportClipboardData(Display
*display
, Window requestor
, Atom aTarget
,
1520 Atom rprop
, LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1524 HANDLE hClipData
= 0;
1526 *lpBytes
= 0; /* Assume failure */
1528 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpData
))
1529 ERR("Failed to export %04x format\n", lpData
->wFormatID
);
1532 datasize
= GlobalSize(lpData
->hData
);
1534 hClipData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, datasize
);
1535 if (hClipData
== 0) return NULL
;
1537 if ((lpClipData
= GlobalLock(hClipData
)))
1539 LPVOID lpdata
= GlobalLock(lpData
->hData
);
1541 memcpy(lpClipData
, lpdata
, datasize
);
1542 *lpBytes
= datasize
;
1544 GlobalUnlock(lpData
->hData
);
1545 GlobalUnlock(hClipData
);
1547 GlobalFree(hClipData
);
1556 /**************************************************************************
1557 * X11DRV_CLIPBOARD_ExportXAString
1559 * Export CF_TEXT converting the string to XA_STRING.
1560 * Helper function for X11DRV_CLIPBOARD_ExportString.
1562 static HANDLE
X11DRV_CLIPBOARD_ExportXAString(LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1566 LPSTR text
, lpstr
= NULL
;
1568 *lpBytes
= 0; /* Assume return has zero bytes */
1570 text
= GlobalLock(lpData
->hData
);
1571 size
= strlen(text
);
1573 /* remove carriage returns */
1574 lpstr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
+ 1);
1578 for (i
= 0,j
= 0; i
< size
&& text
[i
]; i
++)
1580 if (text
[i
] == '\r' && (text
[i
+1] == '\n' || text
[i
+1] == '\0'))
1582 lpstr
[j
++] = text
[i
];
1586 *lpBytes
= j
; /* Number of bytes in string */
1589 GlobalUnlock(lpData
->hData
);
1595 /**************************************************************************
1596 * X11DRV_CLIPBOARD_ExportUTF8String
1598 * Export CF_UNICODE converting the string to UTF8.
1599 * Helper function for X11DRV_CLIPBOARD_ExportString.
1601 static HANDLE
X11DRV_CLIPBOARD_ExportUTF8String(LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1606 LPSTR text
, lpstr
= NULL
;
1608 *lpBytes
= 0; /* Assume return has zero bytes */
1610 uni_text
= GlobalLock(lpData
->hData
);
1612 size
= WideCharToMultiByte(CP_UTF8
, 0, uni_text
, -1, NULL
, 0, NULL
, NULL
);
1614 text
= HeapAlloc(GetProcessHeap(), 0, size
);
1617 WideCharToMultiByte(CP_UTF8
, 0, uni_text
, -1, text
, size
, NULL
, NULL
);
1619 /* remove carriage returns */
1620 lpstr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
--);
1624 for (i
= 0,j
= 0; i
< size
&& text
[i
]; i
++)
1626 if (text
[i
] == '\r' && (text
[i
+1] == '\n' || text
[i
+1] == '\0'))
1628 lpstr
[j
++] = text
[i
];
1632 *lpBytes
= j
; /* Number of bytes in string */
1635 HeapFree(GetProcessHeap(), 0, text
);
1636 GlobalUnlock(lpData
->hData
);
1643 /**************************************************************************
1644 * X11DRV_CLIPBOARD_ExportCompoundText
1646 * Export CF_UNICODE to COMPOUND_TEXT
1647 * Helper function for X11DRV_CLIPBOARD_ExportString.
1649 static HANDLE
X11DRV_CLIPBOARD_ExportCompoundText(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1650 LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1654 XICCEncodingStyle style
;
1659 uni_text
= GlobalLock(lpData
->hData
);
1661 size
= WideCharToMultiByte(CP_UNIXCP
, 0, uni_text
, -1, NULL
, 0, NULL
, NULL
);
1662 lpstr
= HeapAlloc(GetProcessHeap(), 0, size
);
1666 WideCharToMultiByte(CP_UNIXCP
, 0, uni_text
, -1, lpstr
, size
, NULL
, NULL
);
1668 /* remove carriage returns */
1669 for (i
= 0, j
= 0; i
< size
&& lpstr
[i
]; i
++)
1671 if (lpstr
[i
] == '\r' && (lpstr
[i
+1] == '\n' || lpstr
[i
+1] == '\0'))
1673 lpstr
[j
++] = lpstr
[i
];
1677 GlobalUnlock(lpData
->hData
);
1679 if (aTarget
== x11drv_atom(COMPOUND_TEXT
))
1680 style
= XCompoundTextStyle
;
1682 style
= XStdICCTextStyle
;
1684 /* Update the X property */
1685 if (XmbTextListToTextProperty(display
, &lpstr
, 1, style
, &prop
) == Success
)
1687 XSetTextProperty(display
, requestor
, &prop
, rprop
);
1691 HeapFree(GetProcessHeap(), 0, lpstr
);
1696 /**************************************************************************
1697 * X11DRV_CLIPBOARD_ExportString
1701 static HANDLE
X11DRV_CLIPBOARD_ExportString(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1702 LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1704 if (X11DRV_CLIPBOARD_RenderFormat(display
, lpData
))
1706 if (aTarget
== XA_STRING
)
1707 return X11DRV_CLIPBOARD_ExportXAString(lpData
, lpBytes
);
1708 else if (aTarget
== x11drv_atom(COMPOUND_TEXT
) || aTarget
== x11drv_atom(TEXT
))
1709 return X11DRV_CLIPBOARD_ExportCompoundText(display
, requestor
, aTarget
,
1710 rprop
, lpData
, lpBytes
);
1713 TRACE("Exporting target %ld to default UTF8_STRING\n", aTarget
);
1714 return X11DRV_CLIPBOARD_ExportUTF8String(lpData
, lpBytes
);
1718 ERR("Failed to render %04x format\n", lpData
->wFormatID
);
1724 /**************************************************************************
1725 * X11DRV_CLIPBOARD_ExportXAPIXMAP
1727 * Export CF_DIB to XA_PIXMAP.
1729 static HANDLE
X11DRV_CLIPBOARD_ExportXAPIXMAP(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1730 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1733 unsigned char* lpData
;
1735 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1737 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1741 if (!lpdata
->drvData
) /* If not already rendered */
1745 struct gdi_image_bits bits
;
1747 pbmi
= GlobalLock( lpdata
->hData
);
1748 bits
.ptr
= (LPBYTE
)pbmi
+ bitmap_info_size( pbmi
, DIB_RGB_COLORS
);
1750 bits
.is_copy
= FALSE
;
1751 pixmap
= create_pixmap_from_image( 0, &default_visual
, pbmi
, &bits
, DIB_RGB_COLORS
);
1752 GlobalUnlock( lpdata
->hData
);
1753 lpdata
->drvData
= pixmap
;
1756 *lpBytes
= sizeof(Pixmap
); /* pixmap is a 32bit value */
1758 /* Wrap pixmap so we can return a handle */
1759 hData
= GlobalAlloc(0, *lpBytes
);
1760 lpData
= GlobalLock(hData
);
1761 memcpy(lpData
, &lpdata
->drvData
, *lpBytes
);
1762 GlobalUnlock(hData
);
1768 /**************************************************************************
1769 * X11DRV_CLIPBOARD_ExportImageBmp
1771 * Export CF_DIB to image/bmp.
1773 static HANDLE
X11DRV_CLIPBOARD_ExportImageBmp(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1774 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1781 BITMAPFILEHEADER
*bfh
;
1785 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1787 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1791 hpackeddib
= lpdata
->hData
;
1793 dibdata
= GlobalLock(hpackeddib
);
1796 ERR("Failed to lock packed DIB\n");
1800 bmpsize
= sizeof(BITMAPFILEHEADER
) + GlobalSize(hpackeddib
);
1802 hbmpdata
= GlobalAlloc(0, bmpsize
);
1806 bmpdata
= GlobalLock(hbmpdata
);
1810 GlobalFree(hbmpdata
);
1811 GlobalUnlock(hpackeddib
);
1815 /* bitmap file header */
1816 bfh
= (BITMAPFILEHEADER
*)bmpdata
;
1817 bfh
->bfType
= 0x4d42; /* "BM" */
1818 bfh
->bfSize
= bmpsize
;
1819 bfh
->bfReserved1
= 0;
1820 bfh
->bfReserved2
= 0;
1821 bfh
->bfOffBits
= sizeof(BITMAPFILEHEADER
) + bitmap_info_size((BITMAPINFO
*)dibdata
, DIB_RGB_COLORS
);
1823 /* rest of bitmap is the same as the packed dib */
1824 memcpy(bfh
+1, dibdata
, bmpsize
-sizeof(BITMAPFILEHEADER
));
1828 GlobalUnlock(hbmpdata
);
1831 GlobalUnlock(hpackeddib
);
1837 /**************************************************************************
1838 * X11DRV_CLIPBOARD_ExportMetaFilePict
1840 * Export MetaFilePict.
1842 static HANDLE
X11DRV_CLIPBOARD_ExportMetaFilePict(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1843 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1845 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1847 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1851 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT
, lpdata
->hData
, lpBytes
, TRUE
);
1855 /**************************************************************************
1856 * X11DRV_CLIPBOARD_ExportEnhMetaFile
1858 * Export EnhMetaFile.
1860 static HANDLE
X11DRV_CLIPBOARD_ExportEnhMetaFile(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1861 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1863 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1865 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1869 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE
, lpdata
->hData
, lpBytes
, TRUE
);
1873 /**************************************************************************
1874 * get_html_description_field
1876 * Find the value of a field in an HTML Format description.
1878 static LPCSTR
get_html_description_field(LPCSTR data
, LPCSTR keyword
)
1882 while (pos
&& *pos
&& *pos
!= '<')
1884 if (memcmp(pos
, keyword
, strlen(keyword
)) == 0)
1885 return pos
+strlen(keyword
);
1887 pos
= strchr(pos
, '\n');
1895 /**************************************************************************
1896 * X11DRV_CLIPBOARD_ExportTextHtml
1898 * Export HTML Format to text/html.
1900 * FIXME: We should attempt to add an <a base> tag and convert windows paths.
1902 static HANDLE
X11DRV_CLIPBOARD_ExportTextHtml(Display
*display
, Window requestor
, Atom aTarget
,
1903 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1906 LPCSTR data
, field_value
;
1907 UINT fragmentstart
, fragmentend
, htmlsize
;
1908 HANDLE hhtmldata
=NULL
;
1913 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1915 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1919 hdata
= lpdata
->hData
;
1921 data
= GlobalLock(hdata
);
1924 ERR("Failed to lock HTML Format data\n");
1928 /* read the important fields */
1929 field_value
= get_html_description_field(data
, "StartFragment:");
1932 ERR("Couldn't find StartFragment value\n");
1935 fragmentstart
= atoi(field_value
);
1937 field_value
= get_html_description_field(data
, "EndFragment:");
1940 ERR("Couldn't find EndFragment value\n");
1943 fragmentend
= atoi(field_value
);
1945 /* export only the fragment */
1946 htmlsize
= fragmentend
- fragmentstart
+ 1;
1948 hhtmldata
= GlobalAlloc(0, htmlsize
);
1952 htmldata
= GlobalLock(hhtmldata
);
1956 GlobalFree(hhtmldata
);
1961 memcpy(htmldata
, &data
[fragmentstart
], fragmentend
-fragmentstart
);
1962 htmldata
[htmlsize
-1] = '\0';
1964 *lpBytes
= htmlsize
;
1966 GlobalUnlock(htmldata
);
1971 GlobalUnlock(hdata
);
1977 /**************************************************************************
1978 * X11DRV_CLIPBOARD_QueryTargets
1980 static BOOL
X11DRV_CLIPBOARD_QueryTargets(Display
*display
, Window w
, Atom selection
,
1981 Atom target
, XEvent
*xe
)
1985 XConvertSelection(display
, selection
, target
, x11drv_atom(SELECTION_DATA
), w
, CurrentTime
);
1988 * Wait until SelectionNotify is received
1990 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
1992 Bool res
= XCheckTypedWindowEvent(display
, w
, SelectionNotify
, xe
);
1993 if (res
&& xe
->xselection
.selection
== selection
) break;
1995 usleep(SELECTION_WAIT
);
1998 if (i
== SELECTION_RETRIES
)
2000 ERR("Timed out waiting for SelectionNotify event\n");
2003 /* Verify that the selection returned a valid TARGETS property */
2004 if ((xe
->xselection
.target
!= target
) || (xe
->xselection
.property
== None
))
2006 /* Selection owner failed to respond or we missed the SelectionNotify */
2007 WARN("Failed to retrieve TARGETS for selection %ld.\n", selection
);
2015 static int is_atom_error( Display
*display
, XErrorEvent
*event
, void *arg
)
2017 return (event
->error_code
== BadAtom
);
2020 /**************************************************************************
2021 * X11DRV_CLIPBOARD_InsertSelectionProperties
2023 * Mark properties available for future retrieval.
2025 static VOID
X11DRV_CLIPBOARD_InsertSelectionProperties(Display
*display
, Atom
* properties
, UINT count
)
2027 UINT i
, nb_atoms
= 0;
2030 /* Cache these formats in the clipboard cache */
2031 for (i
= 0; i
< count
; i
++)
2033 LPWINE_CLIPFORMAT lpFormat
= X11DRV_CLIPBOARD_LookupProperty(NULL
, properties
[i
]);
2037 /* We found at least one Window's format that mapps to the property.
2038 * Continue looking for more.
2040 * If more than one property map to a Window's format then we use the first
2041 * one and ignore the rest.
2045 TRACE("Atom#%d Property(%d): --> Format %s\n",
2046 i
, lpFormat
->drvData
, debugstr_format(lpFormat
->wFormatID
));
2047 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat
->wFormatID
, 0, 0, lpFormat
, FALSE
);
2048 lpFormat
= X11DRV_CLIPBOARD_LookupProperty(lpFormat
, properties
[i
]);
2051 else if (properties
[i
])
2053 /* add it to the list of atoms that we don't know about yet */
2054 if (!atoms
) atoms
= HeapAlloc( GetProcessHeap(), 0,
2055 (count
- i
) * sizeof(*atoms
) );
2056 if (atoms
) atoms
[nb_atoms
++] = properties
[i
];
2060 /* query all unknown atoms in one go */
2063 char **names
= HeapAlloc( GetProcessHeap(), 0, nb_atoms
* sizeof(*names
) );
2066 X11DRV_expect_error( display
, is_atom_error
, NULL
);
2067 if (!XGetAtomNames( display
, atoms
, nb_atoms
, names
)) nb_atoms
= 0;
2068 if (X11DRV_check_error())
2070 WARN( "got some bad atoms, ignoring\n" );
2073 for (i
= 0; i
< nb_atoms
; i
++)
2075 WINE_CLIPFORMAT
*lpFormat
;
2077 int len
= MultiByteToWideChar(CP_UNIXCP
, 0, names
[i
], -1, NULL
, 0);
2078 wname
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
2079 MultiByteToWideChar(CP_UNIXCP
, 0, names
[i
], -1, wname
, len
);
2081 lpFormat
= register_format( RegisterClipboardFormatW(wname
), atoms
[i
] );
2082 HeapFree(GetProcessHeap(), 0, wname
);
2085 ERR("Failed to register %s property. Type will not be cached.\n", names
[i
]);
2088 TRACE("Atom#%d Property(%d): --> Format %s\n",
2089 i
, lpFormat
->drvData
, debugstr_format(lpFormat
->wFormatID
));
2090 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat
->wFormatID
, 0, 0, lpFormat
, FALSE
);
2092 for (i
= 0; i
< nb_atoms
; i
++) XFree( names
[i
] );
2093 HeapFree( GetProcessHeap(), 0, names
);
2095 HeapFree( GetProcessHeap(), 0, atoms
);
2100 /**************************************************************************
2101 * X11DRV_CLIPBOARD_QueryAvailableData
2103 * Caches the list of data formats available from the current selection.
2104 * This queries the selection owner for the TARGETS property and saves all
2105 * reported property types.
2107 static int X11DRV_CLIPBOARD_QueryAvailableData(Display
*display
, LPCLIPBOARDINFO lpcbinfo
)
2110 Atom atype
=AnyPropertyType
;
2112 unsigned long remain
;
2113 Atom
* targetList
=NULL
;
2115 unsigned long cSelectionTargets
= 0;
2117 if (selectionAcquired
& (S_PRIMARY
| S_CLIPBOARD
))
2119 ERR("Received request to cache selection but process is owner=(%08x)\n",
2120 (unsigned) selectionWindow
);
2121 return -1; /* Prevent self request */
2124 w
= thread_selection_wnd();
2127 ERR("No window available to retrieve selection!\n");
2132 * Query the selection owner for the TARGETS property
2134 if ((use_primary_selection
&& XGetSelectionOwner(display
,XA_PRIMARY
)) ||
2135 XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)))
2137 if (use_primary_selection
&& (X11DRV_CLIPBOARD_QueryTargets(display
, w
, XA_PRIMARY
, x11drv_atom(TARGETS
), &xe
)))
2138 selectionCacheSrc
= XA_PRIMARY
;
2139 else if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, x11drv_atom(CLIPBOARD
), x11drv_atom(TARGETS
), &xe
))
2140 selectionCacheSrc
= x11drv_atom(CLIPBOARD
);
2143 Atom xstr
= XA_STRING
;
2145 /* Selection Owner doesn't understand TARGETS, try retrieving XA_STRING */
2146 if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, XA_PRIMARY
, XA_STRING
, &xe
))
2148 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, &xstr
, 1);
2149 selectionCacheSrc
= XA_PRIMARY
;
2152 else if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, x11drv_atom(CLIPBOARD
), XA_STRING
, &xe
))
2154 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, &xstr
, 1);
2155 selectionCacheSrc
= x11drv_atom(CLIPBOARD
);
2160 WARN("Failed to query selection owner for available data.\n");
2165 else return 0; /* No selection owner so report 0 targets available */
2167 /* Read the TARGETS property contents */
2168 if (!XGetWindowProperty(display
, xe
.xselection
.requestor
, xe
.xselection
.property
,
2169 0, 0x3FFF, True
, AnyPropertyType
/*XA_ATOM*/, &atype
, &aformat
, &cSelectionTargets
,
2170 &remain
, (unsigned char**)&targetList
) != Success
)
2172 TRACE("Type %lx,Format %d,nItems %ld, Remain %ld\n",
2173 atype
, aformat
, cSelectionTargets
, remain
);
2175 * The TARGETS property should have returned us a list of atoms
2176 * corresponding to each selection target format supported.
2178 if (atype
== XA_ATOM
|| atype
== x11drv_atom(TARGETS
))
2182 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, targetList
, cSelectionTargets
);
2184 else if (aformat
== 8) /* work around quartz-wm brain damage */
2186 unsigned long i
, count
= cSelectionTargets
/ sizeof(CARD32
);
2187 Atom
*atoms
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(Atom
) );
2188 for (i
= 0; i
< count
; i
++)
2189 atoms
[i
] = ((CARD32
*)targetList
)[i
]; /* FIXME: byte swapping */
2190 X11DRV_CLIPBOARD_InsertSelectionProperties( display
, atoms
, count
);
2191 HeapFree( GetProcessHeap(), 0, atoms
);
2195 /* Free the list of targets */
2198 else WARN("Failed to read TARGETS property\n");
2200 return cSelectionTargets
;
2204 /**************************************************************************
2205 * X11DRV_CLIPBOARD_ReadSelectionData
2207 * This method is invoked only when we DO NOT own the X selection
2209 * We always get the data from the selection client each time,
2210 * since we have no way of determining if the data in our cache is stale.
2212 static BOOL
X11DRV_CLIPBOARD_ReadSelectionData(Display
*display
, LPWINE_CLIPDATA lpData
)
2219 TRACE("%04x\n", lpData
->wFormatID
);
2221 if (!lpData
->lpFormat
)
2223 ERR("Requesting format %04x but no source format linked to data.\n",
2228 if (!selectionAcquired
)
2230 Window w
= thread_selection_wnd();
2233 ERR("No window available to read selection data!\n");
2237 TRACE("Requesting conversion of %s property (%d) from selection type %08x\n",
2238 debugstr_format(lpData
->lpFormat
->wFormatID
), lpData
->lpFormat
->drvData
,
2239 (UINT
)selectionCacheSrc
);
2241 XConvertSelection(display
, selectionCacheSrc
, lpData
->lpFormat
->drvData
,
2242 x11drv_atom(SELECTION_DATA
), w
, CurrentTime
);
2244 /* wait until SelectionNotify is received */
2245 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
2247 res
= XCheckTypedWindowEvent(display
, w
, SelectionNotify
, &xe
);
2248 if (res
&& xe
.xselection
.selection
== selectionCacheSrc
) break;
2250 usleep(SELECTION_WAIT
);
2253 if (i
== SELECTION_RETRIES
)
2255 ERR("Timed out waiting for SelectionNotify event\n");
2257 /* Verify that the selection returned a valid TARGETS property */
2258 else if (xe
.xselection
.property
!= None
)
2261 * Read the contents of the X selection property
2262 * into WINE's clipboard cache and converting the
2263 * data format if necessary.
2265 HANDLE hData
= lpData
->lpFormat
->lpDrvImportFunc(display
, xe
.xselection
.requestor
,
2266 xe
.xselection
.property
);
2269 bRet
= X11DRV_CLIPBOARD_InsertClipboardData(lpData
->wFormatID
, hData
, 0, lpData
->lpFormat
, TRUE
);
2271 TRACE("Import function failed\n");
2275 TRACE("Failed to convert selection\n");
2280 ERR("Received request to cache selection data but process is owner\n");
2283 TRACE("Returning %d\n", bRet
);
2289 /**************************************************************************
2290 * X11DRV_CLIPBOARD_GetProperty
2291 * Gets type, data and size.
2293 static BOOL
X11DRV_CLIPBOARD_GetProperty(Display
*display
, Window w
, Atom prop
,
2294 Atom
*atype
, unsigned char** data
, unsigned long* datasize
)
2297 unsigned long pos
= 0, nitems
, remain
, count
;
2298 unsigned char *val
= NULL
, *buffer
;
2300 TRACE("Reading property %lu from X window %lx\n", prop
, w
);
2304 if (XGetWindowProperty(display
, w
, prop
, pos
, INT_MAX
/ 4, False
,
2305 AnyPropertyType
, atype
, &aformat
, &nitems
, &remain
, &buffer
) != Success
)
2307 WARN("Failed to read property\n");
2308 HeapFree( GetProcessHeap(), 0, val
);
2312 count
= get_property_size( aformat
, nitems
);
2313 if (!val
) *data
= HeapAlloc( GetProcessHeap(), 0, pos
* sizeof(int) + count
+ 1 );
2314 else *data
= HeapReAlloc( GetProcessHeap(), 0, val
, pos
* sizeof(int) + count
+ 1 );
2319 HeapFree( GetProcessHeap(), 0, val
);
2323 memcpy( (int *)val
+ pos
, buffer
, count
);
2327 *datasize
= pos
* sizeof(int) + count
;
2331 pos
+= count
/ sizeof(int);
2334 /* Delete the property on the window now that we are done
2335 * This will send a PropertyNotify event to the selection owner. */
2336 XDeleteProperty(display
, w
, prop
);
2341 /**************************************************************************
2342 * X11DRV_CLIPBOARD_ReadProperty
2343 * Reads the contents of the X selection property.
2345 static BOOL
X11DRV_CLIPBOARD_ReadProperty(Display
*display
, Window w
, Atom prop
,
2346 unsigned char** data
, unsigned long* datasize
)
2354 if (!X11DRV_CLIPBOARD_GetProperty(display
, w
, prop
, &atype
, data
, datasize
))
2357 while (XCheckTypedWindowEvent(display
, w
, PropertyNotify
, &xe
))
2360 if (atype
== x11drv_atom(INCR
))
2362 unsigned char *buf
= *data
;
2363 unsigned long bufsize
= 0;
2368 unsigned char *prop_data
, *tmp
;
2369 unsigned long prop_size
;
2371 /* Wait until PropertyNotify is received */
2372 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
2376 res
= XCheckTypedWindowEvent(display
, w
, PropertyNotify
, &xe
);
2377 if (res
&& xe
.xproperty
.atom
== prop
&&
2378 xe
.xproperty
.state
== PropertyNewValue
)
2380 usleep(SELECTION_WAIT
);
2383 if (i
>= SELECTION_RETRIES
||
2384 !X11DRV_CLIPBOARD_GetProperty(display
, w
, prop
, &atype
, &prop_data
, &prop_size
))
2386 HeapFree(GetProcessHeap(), 0, buf
);
2390 /* Retrieved entire data. */
2393 HeapFree(GetProcessHeap(), 0, prop_data
);
2395 *datasize
= bufsize
;
2399 tmp
= HeapReAlloc(GetProcessHeap(), 0, buf
, bufsize
+ prop_size
+ 1);
2402 HeapFree(GetProcessHeap(), 0, buf
);
2407 memcpy(buf
+ bufsize
, prop_data
, prop_size
+ 1);
2408 bufsize
+= prop_size
;
2409 HeapFree(GetProcessHeap(), 0, prop_data
);
2417 /**************************************************************************
2418 * CLIPBOARD_SerializeMetafile
2420 static HANDLE
X11DRV_CLIPBOARD_SerializeMetafile(INT wformat
, HANDLE hdata
, LPDWORD lpcbytes
, BOOL out
)
2424 TRACE(" wFormat=%d hdata=%p out=%d\n", wformat
, hdata
, out
);
2426 if (out
) /* Serialize out, caller should free memory */
2428 *lpcbytes
= 0; /* Assume failure */
2430 if (wformat
== CF_METAFILEPICT
)
2432 LPMETAFILEPICT lpmfp
= GlobalLock(hdata
);
2433 unsigned int size
= GetMetaFileBitsEx(lpmfp
->hMF
, 0, NULL
);
2435 h
= GlobalAlloc(0, size
+ sizeof(METAFILEPICT
));
2438 char *pdata
= GlobalLock(h
);
2440 memcpy(pdata
, lpmfp
, sizeof(METAFILEPICT
));
2441 GetMetaFileBitsEx(lpmfp
->hMF
, size
, pdata
+ sizeof(METAFILEPICT
));
2443 *lpcbytes
= size
+ sizeof(METAFILEPICT
);
2448 GlobalUnlock(hdata
);
2450 else if (wformat
== CF_ENHMETAFILE
)
2452 int size
= GetEnhMetaFileBits(hdata
, 0, NULL
);
2454 h
= GlobalAlloc(0, size
);
2457 LPVOID pdata
= GlobalLock(h
);
2459 GetEnhMetaFileBits(hdata
, size
, pdata
);
2468 if (wformat
== CF_METAFILEPICT
)
2470 h
= GlobalAlloc(0, sizeof(METAFILEPICT
));
2473 unsigned int wiresize
;
2474 LPMETAFILEPICT lpmfp
= GlobalLock(h
);
2476 memcpy(lpmfp
, hdata
, sizeof(METAFILEPICT
));
2477 wiresize
= *lpcbytes
- sizeof(METAFILEPICT
);
2478 lpmfp
->hMF
= SetMetaFileBitsEx(wiresize
,
2479 ((const BYTE
*)hdata
) + sizeof(METAFILEPICT
));
2483 else if (wformat
== CF_ENHMETAFILE
)
2485 h
= SetEnhMetaFileBits(*lpcbytes
, hdata
);
2493 /**************************************************************************
2494 * X11DRV_CLIPBOARD_ReleaseSelection
2496 * Release XA_CLIPBOARD and XA_PRIMARY in response to a SelectionClear event.
2498 static void X11DRV_CLIPBOARD_ReleaseSelection(Display
*display
, Atom selType
, Window w
, HWND hwnd
, Time time
)
2500 /* w is the window that lost the selection
2502 TRACE("event->window = %08x (selectionWindow = %08x) selectionAcquired=0x%08x\n",
2503 (unsigned)w
, (unsigned)selectionWindow
, (unsigned)selectionAcquired
);
2505 if (selectionAcquired
&& (w
== selectionWindow
))
2507 CLIPBOARDINFO cbinfo
;
2509 /* completely give up the selection */
2510 TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
2512 X11DRV_CLIPBOARD_GetClipboardInfo(&cbinfo
);
2514 if (cbinfo
.flags
& CB_PROCESS
)
2516 /* Since we're still the owner, this wasn't initiated by
2517 another Wine process */
2518 if (OpenClipboard(hwnd
))
2520 /* Destroy private objects */
2521 SendMessageW(cbinfo
.hWndOwner
, WM_DESTROYCLIPBOARD
, 0, 0);
2523 /* Give up ownership of the windows clipboard */
2524 X11DRV_CLIPBOARD_ReleaseOwnership();
2529 if ((selType
== x11drv_atom(CLIPBOARD
)) && (selectionAcquired
& S_PRIMARY
))
2531 TRACE("Lost clipboard. Check if we need to release PRIMARY\n");
2533 if (selectionWindow
== XGetSelectionOwner(display
, XA_PRIMARY
))
2535 TRACE("We still own PRIMARY. Releasing PRIMARY.\n");
2536 XSetSelectionOwner(display
, XA_PRIMARY
, None
, time
);
2539 TRACE("We no longer own PRIMARY\n");
2541 else if ((selType
== XA_PRIMARY
) && (selectionAcquired
& S_CLIPBOARD
))
2543 TRACE("Lost PRIMARY. Check if we need to release CLIPBOARD\n");
2545 if (selectionWindow
== XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)))
2547 TRACE("We still own CLIPBOARD. Releasing CLIPBOARD.\n");
2548 XSetSelectionOwner(display
, x11drv_atom(CLIPBOARD
), None
, time
);
2551 TRACE("We no longer own CLIPBOARD\n");
2554 selectionWindow
= None
;
2556 X11DRV_EmptyClipboard(FALSE
);
2558 /* Reset the selection flags now that we are done */
2559 selectionAcquired
= S_NOSELECTION
;
2564 /**************************************************************************
2565 * IsSelectionOwner (X11DRV.@)
2567 * Returns: TRUE if the selection is owned by this process, FALSE otherwise
2569 static BOOL
X11DRV_CLIPBOARD_IsSelectionOwner(void)
2571 return selectionAcquired
;
2575 /**************************************************************************
2576 * X11DRV Clipboard Exports
2577 **************************************************************************/
2580 static void selection_acquire(void)
2585 owner
= thread_selection_wnd();
2586 display
= thread_display();
2588 selectionAcquired
= 0;
2589 selectionWindow
= 0;
2591 /* Grab PRIMARY selection if not owned */
2592 if (use_primary_selection
)
2593 XSetSelectionOwner(display
, XA_PRIMARY
, owner
, CurrentTime
);
2595 /* Grab CLIPBOARD selection if not owned */
2596 XSetSelectionOwner(display
, x11drv_atom(CLIPBOARD
), owner
, CurrentTime
);
2598 if (use_primary_selection
&& XGetSelectionOwner(display
, XA_PRIMARY
) == owner
)
2599 selectionAcquired
|= S_PRIMARY
;
2601 if (XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)) == owner
)
2602 selectionAcquired
|= S_CLIPBOARD
;
2604 if (selectionAcquired
)
2606 selectionWindow
= owner
;
2607 TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner
);
2611 static DWORD WINAPI
selection_thread_proc(LPVOID p
)
2617 selection_acquire();
2620 while (selectionAcquired
)
2622 MsgWaitForMultipleObjectsEx(0, NULL
, INFINITE
, QS_SENDMESSAGE
, 0);
2628 /**************************************************************************
2629 * AcquireClipboard (X11DRV.@)
2631 int CDECL
X11DRV_AcquireClipboard(HWND hWndClipWindow
)
2634 HANDLE selectionThread
;
2636 TRACE(" %p\n", hWndClipWindow
);
2639 * It's important that the selection get acquired from the thread
2640 * that owns the clipboard window. The primary reason is that we know
2641 * it is running a message loop and therefore can process the
2642 * X selection events.
2644 if (hWndClipWindow
&&
2645 GetCurrentThreadId() != GetWindowThreadProcessId(hWndClipWindow
, &procid
))
2647 if (procid
!= GetCurrentProcessId())
2649 WARN("Setting clipboard owner to other process is not supported\n");
2650 hWndClipWindow
= NULL
;
2654 TRACE("Thread %x is acquiring selection with thread %x's window %p\n",
2655 GetCurrentThreadId(),
2656 GetWindowThreadProcessId(hWndClipWindow
, NULL
), hWndClipWindow
);
2658 return SendMessageW(hWndClipWindow
, WM_X11DRV_ACQUIRE_SELECTION
, 0, 0);
2664 selection_acquire();
2668 HANDLE event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2669 selectionThread
= CreateThread(NULL
, 0, selection_thread_proc
, event
, 0, NULL
);
2671 if (!selectionThread
)
2673 WARN("Could not start clipboard thread\n");
2678 WaitForSingleObject(event
, INFINITE
);
2680 CloseHandle(selectionThread
);
2687 /**************************************************************************
2688 * X11DRV_EmptyClipboard
2690 * Empty cached clipboard data.
2692 void CDECL
X11DRV_EmptyClipboard(BOOL keepunowned
)
2694 WINE_CLIPDATA
*data
, *next
;
2696 LIST_FOR_EACH_ENTRY_SAFE( data
, next
, &data_list
, WINE_CLIPDATA
, entry
)
2698 if (keepunowned
&& (data
->wFlags
& CF_FLAG_UNOWNED
)) continue;
2699 list_remove( &data
->entry
);
2700 X11DRV_CLIPBOARD_FreeData( data
);
2701 HeapFree( GetProcessHeap(), 0, data
);
2705 TRACE(" %d entries remaining in cache.\n", ClipDataCount
);
2710 /**************************************************************************
2711 * X11DRV_SetClipboardData
2713 BOOL CDECL
X11DRV_SetClipboardData(UINT wFormat
, HANDLE hData
, BOOL owner
)
2716 BOOL bResult
= TRUE
;
2718 /* If it's not owned, data can only be set if the format data is not already owned
2719 and its rendering is not delayed */
2722 CLIPBOARDINFO cbinfo
;
2723 LPWINE_CLIPDATA lpRender
;
2725 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2728 ((lpRender
= X11DRV_CLIPBOARD_LookupData(wFormat
)) &&
2729 !(lpRender
->wFlags
& CF_FLAG_UNOWNED
)))
2732 flags
= CF_FLAG_UNOWNED
;
2735 bResult
&= X11DRV_CLIPBOARD_InsertClipboardData(wFormat
, hData
, flags
, NULL
, TRUE
);
2741 /**************************************************************************
2742 * CountClipboardFormats
2744 INT CDECL
X11DRV_CountClipboardFormats(void)
2746 CLIPBOARDINFO cbinfo
;
2748 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2750 TRACE(" count=%d\n", ClipDataCount
);
2752 return ClipDataCount
;
2756 /**************************************************************************
2757 * X11DRV_EnumClipboardFormats
2759 UINT CDECL
X11DRV_EnumClipboardFormats(UINT wFormat
)
2761 CLIPBOARDINFO cbinfo
;
2762 struct list
*ptr
= NULL
;
2764 TRACE("(%04X)\n", wFormat
);
2766 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2770 ptr
= list_head( &data_list
);
2774 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(wFormat
);
2775 if (lpData
) ptr
= list_next( &data_list
, &lpData
->entry
);
2779 return LIST_ENTRY( ptr
, WINE_CLIPDATA
, entry
)->wFormatID
;
2783 /**************************************************************************
2784 * X11DRV_IsClipboardFormatAvailable
2786 BOOL CDECL
X11DRV_IsClipboardFormatAvailable(UINT wFormat
)
2789 CLIPBOARDINFO cbinfo
;
2791 TRACE("(%04X)\n", wFormat
);
2793 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2795 if (wFormat
!= 0 && X11DRV_CLIPBOARD_LookupData(wFormat
))
2798 TRACE("(%04X)- ret(%d)\n", wFormat
, bRet
);
2804 /**************************************************************************
2805 * GetClipboardData (USER.142)
2807 HANDLE CDECL
X11DRV_GetClipboardData(UINT wFormat
)
2809 CLIPBOARDINFO cbinfo
;
2810 LPWINE_CLIPDATA lpRender
;
2812 TRACE("(%04X)\n", wFormat
);
2814 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2816 if ((lpRender
= X11DRV_CLIPBOARD_LookupData(wFormat
)))
2818 if ( !lpRender
->hData
)
2819 X11DRV_CLIPBOARD_RenderFormat(thread_init_display(), lpRender
);
2821 TRACE(" returning %p (type %04x)\n", lpRender
->hData
, lpRender
->wFormatID
);
2822 return lpRender
->hData
;
2829 /**************************************************************************
2830 * ResetSelectionOwner
2832 * Called when the thread owning the selection is destroyed and we need to
2833 * preserve the selection ownership. We look for another top level window
2834 * in this process and send it a message to acquire the selection.
2836 void X11DRV_ResetSelectionOwner(void)
2843 if (!selectionAcquired
|| thread_selection_wnd() != selectionWindow
)
2846 selectionAcquired
= S_NOSELECTION
;
2847 selectionWindow
= 0;
2849 hwnd
= GetWindow(GetDesktopWindow(), GW_CHILD
);
2852 if (GetCurrentThreadId() != GetWindowThreadProcessId(hwnd
, &procid
))
2854 if (GetCurrentProcessId() == procid
)
2856 if (SendMessageW(hwnd
, WM_X11DRV_ACQUIRE_SELECTION
, 0, 0))
2860 } while ((hwnd
= GetWindow(hwnd
, GW_HWNDNEXT
)) != NULL
);
2862 WARN("Failed to find another thread to take selection ownership. Clipboard data will be lost.\n");
2864 X11DRV_CLIPBOARD_ReleaseOwnership();
2865 X11DRV_EmptyClipboard(FALSE
);
2869 /**************************************************************************
2870 * X11DRV_CLIPBOARD_SynthesizeData
2872 static BOOL
X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID
)
2875 LPWINE_CLIPDATA lpSource
= NULL
;
2877 TRACE(" %04x\n", wFormatID
);
2879 /* Don't need to synthesize if it already exists */
2880 if (X11DRV_CLIPBOARD_LookupData(wFormatID
))
2883 if (wFormatID
== CF_UNICODETEXT
|| wFormatID
== CF_TEXT
|| wFormatID
== CF_OEMTEXT
)
2885 bsyn
= ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT
)) &&
2886 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) ||
2887 ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_TEXT
)) &&
2888 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) ||
2889 ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT
)) &&
2890 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
);
2892 else if (wFormatID
== CF_ENHMETAFILE
)
2894 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT
)) &&
2895 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2897 else if (wFormatID
== CF_METAFILEPICT
)
2899 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_ENHMETAFILE
)) &&
2900 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2902 else if (wFormatID
== CF_DIB
)
2904 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) &&
2905 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2907 else if (wFormatID
== CF_BITMAP
)
2909 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) &&
2910 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2914 X11DRV_CLIPBOARD_InsertClipboardData(wFormatID
, 0, CF_FLAG_SYNTHESIZED
, NULL
, TRUE
);
2921 /**************************************************************************
2922 * X11DRV_EndClipboardUpdate
2924 * Add locale if it hasn't already been added
2926 void CDECL
X11DRV_EndClipboardUpdate(void)
2928 INT count
= ClipDataCount
;
2930 /* Do Unicode <-> Text <-> OEM mapping */
2931 X11DRV_CLIPBOARD_SynthesizeData(CF_TEXT
);
2932 X11DRV_CLIPBOARD_SynthesizeData(CF_OEMTEXT
);
2933 X11DRV_CLIPBOARD_SynthesizeData(CF_UNICODETEXT
);
2935 /* Enhmetafile <-> MetafilePict mapping */
2936 X11DRV_CLIPBOARD_SynthesizeData(CF_ENHMETAFILE
);
2937 X11DRV_CLIPBOARD_SynthesizeData(CF_METAFILEPICT
);
2939 /* DIB <-> Bitmap mapping */
2940 X11DRV_CLIPBOARD_SynthesizeData(CF_DIB
);
2941 X11DRV_CLIPBOARD_SynthesizeData(CF_BITMAP
);
2943 TRACE("%d formats added to cached data\n", ClipDataCount
- count
);
2947 /***********************************************************************
2948 * X11DRV_SelectionRequest_TARGETS
2949 * Service a TARGETS selection request event
2951 static Atom
X11DRV_SelectionRequest_TARGETS( Display
*display
, Window requestor
,
2952 Atom target
, Atom rprop
)
2957 LPWINE_CLIPFORMAT format
;
2958 LPWINE_CLIPDATA lpData
;
2960 /* Create X atoms for any clipboard types which don't have atoms yet.
2961 * This avoids sending bogus zero atoms.
2962 * Without this, copying might not have access to all clipboard types.
2963 * FIXME: is it safe to call this here?
2968 * Count the number of items we wish to expose as selection targets.
2970 cTargets
= 1; /* Include TARGETS */
2972 if (!list_head( &data_list
)) return None
;
2974 LIST_FOR_EACH_ENTRY( lpData
, &data_list
, WINE_CLIPDATA
, entry
)
2975 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
2976 if ((format
->wFormatID
== lpData
->wFormatID
) &&
2977 format
->lpDrvExportFunc
&& format
->drvData
)
2980 TRACE(" found %d formats\n", cTargets
);
2982 /* Allocate temp buffer */
2983 targets
= HeapAlloc( GetProcessHeap(), 0, cTargets
* sizeof(Atom
));
2988 targets
[i
++] = x11drv_atom(TARGETS
);
2990 LIST_FOR_EACH_ENTRY( lpData
, &data_list
, WINE_CLIPDATA
, entry
)
2991 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
2992 if ((format
->wFormatID
== lpData
->wFormatID
) &&
2993 format
->lpDrvExportFunc
&& format
->drvData
)
2994 targets
[i
++] = format
->drvData
;
2996 if (TRACE_ON(clipboard
))
2999 for ( i
= 0; i
< cTargets
; i
++)
3001 char *itemFmtName
= XGetAtomName(display
, targets
[i
]);
3002 TRACE("\tAtom# %d: Property %ld Type %s\n", i
, targets
[i
], itemFmtName
);
3007 /* We may want to consider setting the type to xaTargets instead,
3008 * in case some apps expect this instead of XA_ATOM */
3009 XChangeProperty(display
, requestor
, rprop
, XA_ATOM
, 32,
3010 PropModeReplace
, (unsigned char *)targets
, cTargets
);
3012 HeapFree(GetProcessHeap(), 0, targets
);
3018 /***********************************************************************
3019 * X11DRV_SelectionRequest_MULTIPLE
3020 * Service a MULTIPLE selection request event
3021 * rprop contains a list of (target,property) atom pairs.
3022 * The first atom names a target and the second names a property.
3023 * The effect is as if we have received a sequence of SelectionRequest events
3024 * (one for each atom pair) except that:
3025 * 1. We reply with a SelectionNotify only when all the requested conversions
3026 * have been performed.
3027 * 2. If we fail to convert the target named by an atom in the MULTIPLE property,
3028 * we replace the atom in the property by None.
3030 static Atom
X11DRV_SelectionRequest_MULTIPLE( HWND hWnd
, XSelectionRequestEvent
*pevent
)
3032 Display
*display
= pevent
->display
;
3034 Atom atype
=AnyPropertyType
;
3036 unsigned long remain
;
3037 Atom
* targetPropList
=NULL
;
3038 unsigned long cTargetPropList
= 0;
3040 /* If the specified property is None the requestor is an obsolete client.
3041 * We support these by using the specified target atom as the reply property.
3043 rprop
= pevent
->property
;
3045 rprop
= pevent
->target
;
3049 /* Read the MULTIPLE property contents. This should contain a list of
3050 * (target,property) atom pairs.
3052 if (!XGetWindowProperty(display
, pevent
->requestor
, rprop
,
3053 0, 0x3FFF, False
, AnyPropertyType
, &atype
,&aformat
,
3054 &cTargetPropList
, &remain
,
3055 (unsigned char**)&targetPropList
) != Success
)
3057 if (TRACE_ON(clipboard
))
3059 char * const typeName
= XGetAtomName(display
, atype
);
3060 TRACE("\tType %s,Format %d,nItems %ld, Remain %ld\n",
3061 typeName
, aformat
, cTargetPropList
, remain
);
3066 * Make sure we got what we expect.
3067 * NOTE: According to the X-ICCCM Version 2.0 documentation the property sent
3068 * in a MULTIPLE selection request should be of type ATOM_PAIR.
3069 * However some X apps(such as XPaint) are not compliant with this and return
3070 * a user defined atom in atype when XGetWindowProperty is called.
3071 * The data *is* an atom pair but is not denoted as such.
3073 if(aformat
== 32 /* atype == xAtomPair */ )
3077 /* Iterate through the ATOM_PAIR list and execute a SelectionRequest
3078 * for each (target,property) pair */
3080 for (i
= 0; i
< cTargetPropList
; i
+=2)
3082 XSelectionRequestEvent event
;
3084 if (TRACE_ON(clipboard
))
3086 char *targetName
, *propName
;
3087 targetName
= XGetAtomName(display
, targetPropList
[i
]);
3088 propName
= XGetAtomName(display
, targetPropList
[i
+1]);
3089 TRACE("MULTIPLE(%d): Target='%s' Prop='%s'\n",
3090 i
/2, targetName
, propName
);
3095 /* We must have a non "None" property to service a MULTIPLE target atom */
3096 if ( !targetPropList
[i
+1] )
3098 TRACE("\tMULTIPLE(%d): Skipping target with empty property!\n", i
);
3102 /* Set up an XSelectionRequestEvent for this (target,property) pair */
3104 event
.target
= targetPropList
[i
];
3105 event
.property
= targetPropList
[i
+1];
3107 /* Fire a SelectionRequest, informing the handler that we are processing
3108 * a MULTIPLE selection request event.
3110 X11DRV_HandleSelectionRequest( hWnd
, &event
, TRUE
);
3114 /* Free the list of targets/properties */
3115 XFree(targetPropList
);
3117 else TRACE("Couldn't read MULTIPLE property\n");
3123 /***********************************************************************
3124 * X11DRV_HandleSelectionRequest
3125 * Process an event selection request event.
3126 * The bIsMultiple flag is used to signal when EVENT_SelectionRequest is called
3127 * recursively while servicing a "MULTIPLE" selection target.
3129 * Note: We only receive this event when WINE owns the X selection
3131 static void X11DRV_HandleSelectionRequest( HWND hWnd
, XSelectionRequestEvent
*event
, BOOL bIsMultiple
)
3133 Display
*display
= event
->display
;
3134 XSelectionEvent result
;
3136 Window request
= event
->requestor
;
3141 * We can only handle the selection request if :
3142 * The selection is PRIMARY or CLIPBOARD, AND we can successfully open the clipboard.
3143 * Don't do these checks or open the clipboard while recursively processing MULTIPLE,
3144 * since this has been already done.
3148 if (((event
->selection
!= XA_PRIMARY
) && (event
->selection
!= x11drv_atom(CLIPBOARD
))))
3152 /* If the specified property is None the requestor is an obsolete client.
3153 * We support these by using the specified target atom as the reply property.
3155 rprop
= event
->property
;
3157 rprop
= event
->target
;
3159 if(event
->target
== x11drv_atom(TARGETS
)) /* Return a list of all supported targets */
3161 /* TARGETS selection request */
3162 rprop
= X11DRV_SelectionRequest_TARGETS( display
, request
, event
->target
, rprop
);
3164 else if(event
->target
== x11drv_atom(MULTIPLE
)) /* rprop contains a list of (target, property) atom pairs */
3166 /* MULTIPLE selection request */
3167 rprop
= X11DRV_SelectionRequest_MULTIPLE( hWnd
, event
);
3171 LPWINE_CLIPFORMAT lpFormat
= X11DRV_CLIPBOARD_LookupProperty(NULL
, event
->target
);
3173 if (lpFormat
&& lpFormat
->lpDrvExportFunc
)
3175 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(lpFormat
->wFormatID
);
3179 unsigned char* lpClipData
;
3181 HANDLE hClipData
= lpFormat
->lpDrvExportFunc(display
, request
, event
->target
,
3182 rprop
, lpData
, &cBytes
);
3184 if (hClipData
&& (lpClipData
= GlobalLock(hClipData
)))
3186 int mode
= PropModeReplace
;
3188 TRACE("\tUpdating property %s, %d bytes\n",
3189 debugstr_format(lpFormat
->wFormatID
), cBytes
);
3192 int nelements
= min(cBytes
, 65536);
3193 XChangeProperty(display
, request
, rprop
, event
->target
,
3194 8, mode
, lpClipData
, nelements
);
3195 mode
= PropModeAppend
;
3196 cBytes
-= nelements
;
3197 lpClipData
+= nelements
;
3198 } while (cBytes
> 0);
3200 GlobalUnlock(hClipData
);
3201 GlobalFree(hClipData
);
3209 * SelectionNotify should be sent only at the end of a MULTIPLE request
3213 result
.type
= SelectionNotify
;
3214 result
.display
= display
;
3215 result
.requestor
= request
;
3216 result
.selection
= event
->selection
;
3217 result
.property
= rprop
;
3218 result
.target
= event
->target
;
3219 result
.time
= event
->time
;
3220 TRACE("Sending SelectionNotify event...\n");
3221 XSendEvent(display
,event
->requestor
,False
,NoEventMask
,(XEvent
*)&result
);
3226 /***********************************************************************
3227 * X11DRV_SelectionRequest
3229 void X11DRV_SelectionRequest( HWND hWnd
, XEvent
*event
)
3231 X11DRV_HandleSelectionRequest( hWnd
, &event
->xselectionrequest
, FALSE
);
3235 /***********************************************************************
3236 * X11DRV_SelectionClear
3238 void X11DRV_SelectionClear( HWND hWnd
, XEvent
*xev
)
3240 XSelectionClearEvent
*event
= &xev
->xselectionclear
;
3241 if (event
->selection
== XA_PRIMARY
|| event
->selection
== x11drv_atom(CLIPBOARD
))
3242 X11DRV_CLIPBOARD_ReleaseSelection( event
->display
, event
->selection
,
3243 event
->window
, hWnd
, event
->time
);