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_TEXT
, XATOM_text_plain
, X11DRV_CLIPBOARD_ImportXAString
, X11DRV_CLIPBOARD_ExportString
},
190 { CF_BITMAP
, XATOM_WCF_BITMAP
, X11DRV_CLIPBOARD_ImportClipboardData
, NULL
},
191 { CF_METAFILEPICT
, XATOM_WCF_METAFILEPICT
, X11DRV_CLIPBOARD_ImportMetaFilePict
, X11DRV_CLIPBOARD_ExportMetaFilePict
},
192 { CF_SYLK
, XATOM_WCF_SYLK
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
193 { CF_DIF
, XATOM_WCF_DIF
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
194 { CF_TIFF
, XATOM_WCF_TIFF
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
195 { CF_OEMTEXT
, XATOM_WCF_OEMTEXT
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
196 { CF_DIB
, XA_PIXMAP
, X11DRV_CLIPBOARD_ImportXAPIXMAP
, X11DRV_CLIPBOARD_ExportXAPIXMAP
},
197 { CF_PALETTE
, XATOM_WCF_PALETTE
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
198 { CF_PENDATA
, XATOM_WCF_PENDATA
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
199 { CF_RIFF
, XATOM_WCF_RIFF
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
200 { CF_WAVE
, XATOM_WCF_WAVE
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
201 { CF_UNICODETEXT
, XATOM_UTF8_STRING
, X11DRV_CLIPBOARD_ImportUTF8
, X11DRV_CLIPBOARD_ExportString
},
202 /* If UTF8_STRING is not available, attempt COMPOUND_TEXT */
203 { CF_UNICODETEXT
, XATOM_COMPOUND_TEXT
, X11DRV_CLIPBOARD_ImportCompoundText
, X11DRV_CLIPBOARD_ExportString
},
204 { CF_ENHMETAFILE
, XATOM_WCF_ENHMETAFILE
, X11DRV_CLIPBOARD_ImportEnhMetaFile
, X11DRV_CLIPBOARD_ExportEnhMetaFile
},
205 { CF_HDROP
, XATOM_WCF_HDROP
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
206 { CF_LOCALE
, XATOM_WCF_LOCALE
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
207 { CF_DIBV5
, XATOM_WCF_DIBV5
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
208 { CF_OWNERDISPLAY
, XATOM_WCF_OWNERDISPLAY
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
209 { CF_DSPTEXT
, XATOM_WCF_DSPTEXT
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
210 { CF_DSPBITMAP
, XATOM_WCF_DSPBITMAP
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
211 { CF_DSPMETAFILEPICT
, XATOM_WCF_DSPMETAFILEPICT
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
212 { CF_DSPENHMETAFILE
, XATOM_WCF_DSPENHMETAFILE
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
213 { CF_DIB
, XATOM_image_bmp
, X11DRV_CLIPBOARD_ImportImageBmp
, X11DRV_CLIPBOARD_ExportImageBmp
},
216 static struct list format_list
= LIST_INIT( format_list
);
218 #define GET_ATOM(prop) (((prop) < FIRST_XATOM) ? (Atom)(prop) : X11DRV_Atoms[(prop) - FIRST_XATOM])
220 /* Maps X properties to Windows formats */
221 static const WCHAR wszRichTextFormat
[] = {'R','i','c','h',' ','T','e','x','t',' ','F','o','r','m','a','t',0};
222 static const WCHAR wszGIF
[] = {'G','I','F',0};
223 static const WCHAR wszJFIF
[] = {'J','F','I','F',0};
224 static const WCHAR wszPNG
[] = {'P','N','G',0};
225 static const WCHAR wszHTMLFormat
[] = {'H','T','M','L',' ','F','o','r','m','a','t',0};
230 } PropertyFormatMap
[] =
232 { wszRichTextFormat
, XATOM_text_rtf
},
233 { wszRichTextFormat
, XATOM_text_richtext
},
234 { wszGIF
, XATOM_image_gif
},
235 { wszJFIF
, XATOM_image_jpeg
},
236 { wszPNG
, XATOM_image_png
},
237 { wszHTMLFormat
, XATOM_HTML_Format
}, /* prefer this to text/html */
242 * Cached clipboard data.
244 static struct list data_list
= LIST_INIT( data_list
);
245 static UINT ClipDataCount
= 0;
248 * Clipboard sequence number
250 static UINT wSeqNo
= 0;
252 /**************************************************************************
253 * Internal Clipboard implementation methods
254 **************************************************************************/
256 static Window
thread_selection_wnd(void)
258 struct x11drv_thread_data
*thread_data
= x11drv_init_thread_data();
259 Window w
= thread_data
->selection_wnd
;
263 w
= XCreateWindow(thread_data
->display
, root_window
, 0, 0, 1, 1, 0, CopyFromParent
,
264 InputOnly
, CopyFromParent
, 0, NULL
);
267 thread_data
->selection_wnd
= w
;
269 XSelectInput(thread_data
->display
, w
, PropertyChangeMask
);
272 FIXME("Failed to create window. Fetching selection data will fail.\n");
278 static const char *debugstr_format( UINT id
)
282 if (GetClipboardFormatNameW( id
, buffer
, 256 ))
283 return wine_dbg_sprintf( "%04x %s", id
, debugstr_w(buffer
) );
287 #define BUILTIN(id) case id: return #id;
290 BUILTIN(CF_METAFILEPICT
)
300 BUILTIN(CF_UNICODETEXT
)
301 BUILTIN(CF_ENHMETAFILE
)
305 BUILTIN(CF_OWNERDISPLAY
)
307 BUILTIN(CF_DSPBITMAP
)
308 BUILTIN(CF_DSPMETAFILEPICT
)
309 BUILTIN(CF_DSPENHMETAFILE
)
311 default: return wine_dbg_sprintf( "%04x", id
);
315 /**************************************************************************
316 * X11DRV_InitClipboard
318 void X11DRV_InitClipboard(void)
321 WINE_CLIPFORMAT
*format
;
323 /* Register built-in formats */
324 for (i
= 0; i
< sizeof(builtin_formats
)/sizeof(builtin_formats
[0]); i
++)
326 if (!(format
= HeapAlloc( GetProcessHeap(), 0, sizeof(*format
)))) break;
327 format
->wFormatID
= builtin_formats
[i
].id
;
328 format
->drvData
= GET_ATOM(builtin_formats
[i
].data
);
329 format
->lpDrvImportFunc
= builtin_formats
[i
].import
;
330 format
->lpDrvExportFunc
= builtin_formats
[i
].export
;
331 list_add_tail( &format_list
, &format
->entry
);
334 /* Register known mapping between window formats and X properties */
335 for (i
= 0; i
< sizeof(PropertyFormatMap
)/sizeof(PropertyFormatMap
[0]); i
++)
336 X11DRV_CLIPBOARD_InsertClipboardFormat( RegisterClipboardFormatW(PropertyFormatMap
[i
].lpszFormat
),
337 GET_ATOM(PropertyFormatMap
[i
].prop
));
339 /* Set up a conversion function from "HTML Format" to "text/html" */
340 format
= X11DRV_CLIPBOARD_InsertClipboardFormat( RegisterClipboardFormatW(wszHTMLFormat
),
341 GET_ATOM(XATOM_text_html
));
342 format
->lpDrvExportFunc
= X11DRV_CLIPBOARD_ExportTextHtml
;
346 /**************************************************************************
349 * Intern atoms for formats that don't have one yet.
351 static void intern_atoms(void)
353 LPWINE_CLIPFORMAT format
;
361 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
362 if (!format
->drvData
) count
++;
365 display
= thread_init_display();
367 names
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*names
) );
368 atoms
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*atoms
) );
371 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
372 if (!format
->drvData
) {
373 GetClipboardFormatNameW( format
->wFormatID
, buffer
, 256 );
374 len
= WideCharToMultiByte(CP_UNIXCP
, 0, buffer
, -1, NULL
, 0, NULL
, NULL
);
375 names
[i
] = HeapAlloc(GetProcessHeap(), 0, len
);
376 WideCharToMultiByte(CP_UNIXCP
, 0, buffer
, -1, names
[i
++], len
, NULL
, NULL
);
379 XInternAtoms( display
, names
, count
, False
, atoms
);
382 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
383 if (!format
->drvData
) {
384 HeapFree(GetProcessHeap(), 0, names
[i
]);
385 format
->drvData
= atoms
[i
++];
388 HeapFree( GetProcessHeap(), 0, names
);
389 HeapFree( GetProcessHeap(), 0, atoms
);
393 /**************************************************************************
396 * Register a custom X clipboard format.
398 static WINE_CLIPFORMAT
*register_format( UINT id
, Atom prop
)
400 LPWINE_CLIPFORMAT lpFormat
;
402 /* walk format chain to see if it's already registered */
403 LIST_FOR_EACH_ENTRY( lpFormat
, &format_list
, WINE_CLIPFORMAT
, entry
)
404 if (lpFormat
->wFormatID
== id
) return lpFormat
;
406 return X11DRV_CLIPBOARD_InsertClipboardFormat(id
, prop
);
410 /**************************************************************************
411 * X11DRV_CLIPBOARD_LookupProperty
413 static LPWINE_CLIPFORMAT
X11DRV_CLIPBOARD_LookupProperty(LPWINE_CLIPFORMAT current
, UINT drvData
)
417 struct list
*ptr
= current
? ¤t
->entry
: &format_list
;
418 BOOL need_intern
= FALSE
;
420 while ((ptr
= list_next( &format_list
, ptr
)))
422 LPWINE_CLIPFORMAT lpFormat
= LIST_ENTRY( ptr
, WINE_CLIPFORMAT
, entry
);
423 if (lpFormat
->drvData
== drvData
) return lpFormat
;
424 if (!lpFormat
->drvData
) need_intern
= TRUE
;
426 if (!need_intern
) return NULL
;
428 /* restart the search for the new atoms */
433 /**************************************************************************
434 * X11DRV_CLIPBOARD_LookupData
436 static LPWINE_CLIPDATA
X11DRV_CLIPBOARD_LookupData(DWORD wID
)
440 LIST_FOR_EACH_ENTRY( data
, &data_list
, WINE_CLIPDATA
, entry
)
441 if (data
->wFormatID
== wID
) return data
;
447 /**************************************************************************
448 * InsertClipboardFormat
450 static WINE_CLIPFORMAT
*X11DRV_CLIPBOARD_InsertClipboardFormat( UINT id
, Atom prop
)
452 LPWINE_CLIPFORMAT lpNewFormat
;
454 /* allocate storage for new format entry */
455 lpNewFormat
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPFORMAT
));
457 if(lpNewFormat
== NULL
)
459 WARN("No more memory for a new format!\n");
462 lpNewFormat
->wFormatID
= id
;
463 lpNewFormat
->drvData
= prop
;
464 lpNewFormat
->lpDrvImportFunc
= X11DRV_CLIPBOARD_ImportClipboardData
;
465 lpNewFormat
->lpDrvExportFunc
= X11DRV_CLIPBOARD_ExportClipboardData
;
467 list_add_tail( &format_list
, &lpNewFormat
->entry
);
469 TRACE("Registering format %s drvData %d\n",
470 debugstr_format(lpNewFormat
->wFormatID
), lpNewFormat
->drvData
);
478 /**************************************************************************
479 * X11DRV_CLIPBOARD_GetClipboardInfo
481 static BOOL
X11DRV_CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo
)
485 SERVER_START_REQ( set_clipboard_info
)
489 if (wine_server_call_err( req
))
491 ERR("Failed to get clipboard owner.\n");
495 cbInfo
->hWndOpen
= wine_server_ptr_handle( reply
->old_clipboard
);
496 cbInfo
->hWndOwner
= wine_server_ptr_handle( reply
->old_owner
);
497 cbInfo
->hWndViewer
= wine_server_ptr_handle( reply
->old_viewer
);
498 cbInfo
->seqno
= reply
->seqno
;
499 cbInfo
->flags
= reply
->flags
;
510 /**************************************************************************
511 * X11DRV_CLIPBOARD_ReleaseOwnership
513 static BOOL
X11DRV_CLIPBOARD_ReleaseOwnership(void)
517 SERVER_START_REQ( set_clipboard_info
)
519 req
->flags
= SET_CB_RELOWNER
| SET_CB_SEQNO
;
521 if (wine_server_call_err( req
))
523 ERR("Failed to set clipboard.\n");
537 /**************************************************************************
538 * X11DRV_CLIPBOARD_InsertClipboardData
540 * Caller *must* have the clipboard open and be the owner.
542 static BOOL
X11DRV_CLIPBOARD_InsertClipboardData(UINT wFormatID
, HANDLE hData
, DWORD flags
,
543 LPWINE_CLIPFORMAT lpFormat
, BOOL override
)
545 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(wFormatID
);
547 TRACE("format=%04x lpData=%p hData=%p flags=0x%08x lpFormat=%p override=%d\n",
548 wFormatID
, lpData
, hData
, flags
, lpFormat
, override
);
550 /* make sure the format exists */
551 if (!lpFormat
) register_format( wFormatID
, 0 );
553 if (lpData
&& !override
)
558 X11DRV_CLIPBOARD_FreeData(lpData
);
560 lpData
->hData
= hData
;
564 lpData
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPDATA
));
566 lpData
->wFormatID
= wFormatID
;
567 lpData
->hData
= hData
;
568 lpData
->lpFormat
= lpFormat
;
571 list_add_tail( &data_list
, &lpData
->entry
);
575 lpData
->wFlags
= flags
;
581 /**************************************************************************
582 * X11DRV_CLIPBOARD_FreeData
584 * Free clipboard data handle.
586 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData
)
588 TRACE("%04x\n", lpData
->wFormatID
);
590 if ((lpData
->wFormatID
>= CF_GDIOBJFIRST
&&
591 lpData
->wFormatID
<= CF_GDIOBJLAST
) ||
592 lpData
->wFormatID
== CF_BITMAP
||
593 lpData
->wFormatID
== CF_DIB
||
594 lpData
->wFormatID
== CF_PALETTE
)
597 DeleteObject(lpData
->hData
);
599 if ((lpData
->wFormatID
== CF_DIB
) && lpData
->drvData
)
600 XFreePixmap(gdi_display
, lpData
->drvData
);
602 else if (lpData
->wFormatID
== CF_METAFILEPICT
)
606 DeleteMetaFile(((METAFILEPICT
*)GlobalLock( lpData
->hData
))->hMF
);
607 GlobalFree(lpData
->hData
);
610 else if (lpData
->wFormatID
== CF_ENHMETAFILE
)
613 DeleteEnhMetaFile(lpData
->hData
);
615 else if (lpData
->wFormatID
< CF_PRIVATEFIRST
||
616 lpData
->wFormatID
> CF_PRIVATELAST
)
619 GlobalFree(lpData
->hData
);
627 /**************************************************************************
628 * X11DRV_CLIPBOARD_UpdateCache
630 static BOOL
X11DRV_CLIPBOARD_UpdateCache(LPCLIPBOARDINFO lpcbinfo
)
634 if (!X11DRV_CLIPBOARD_IsSelectionOwner())
636 if (!X11DRV_CLIPBOARD_GetClipboardInfo(lpcbinfo
))
638 ERR("Failed to retrieve clipboard information.\n");
641 else if (wSeqNo
< lpcbinfo
->seqno
)
643 X11DRV_EmptyClipboard(TRUE
);
645 if (X11DRV_CLIPBOARD_QueryAvailableData(thread_init_display(), lpcbinfo
) < 0)
647 ERR("Failed to cache clipboard data owned by another process.\n");
652 X11DRV_EndClipboardUpdate();
655 wSeqNo
= lpcbinfo
->seqno
;
663 /**************************************************************************
664 * X11DRV_CLIPBOARD_RenderFormat
666 static BOOL
X11DRV_CLIPBOARD_RenderFormat(Display
*display
, LPWINE_CLIPDATA lpData
)
670 TRACE(" 0x%04x hData(%p)\n", lpData
->wFormatID
, lpData
->hData
);
672 if (lpData
->hData
) return bret
; /* Already rendered */
674 if (lpData
->wFlags
& CF_FLAG_SYNTHESIZED
)
675 bret
= X11DRV_CLIPBOARD_RenderSynthesizedFormat(display
, lpData
);
676 else if (!X11DRV_CLIPBOARD_IsSelectionOwner())
678 if (!X11DRV_CLIPBOARD_ReadSelectionData(display
, lpData
))
680 ERR("Failed to cache clipboard data owned by another process. Format=%04x\n",
687 CLIPBOARDINFO cbInfo
;
689 if (X11DRV_CLIPBOARD_GetClipboardInfo(&cbInfo
) && cbInfo
.hWndOwner
)
691 /* Send a WM_RENDERFORMAT message to notify the owner to render the
692 * data requested into the clipboard.
694 TRACE("Sending WM_RENDERFORMAT message to hwnd(%p)\n", cbInfo
.hWndOwner
);
695 SendMessageW(cbInfo
.hWndOwner
, WM_RENDERFORMAT
, lpData
->wFormatID
, 0);
697 if (!lpData
->hData
) bret
= FALSE
;
701 ERR("hWndClipOwner is lost!\n");
710 /**************************************************************************
711 * CLIPBOARD_ConvertText
712 * Returns number of required/converted characters - not bytes!
714 static INT
CLIPBOARD_ConvertText(WORD src_fmt
, void const *src
, INT src_size
,
715 WORD dst_fmt
, void *dst
, INT dst_size
)
719 if(src_fmt
== CF_UNICODETEXT
)
732 return WideCharToMultiByte(cp
, 0, src
, src_size
, dst
, dst_size
, NULL
, NULL
);
735 if(dst_fmt
== CF_UNICODETEXT
)
748 return MultiByteToWideChar(cp
, 0, src
, src_size
, dst
, dst_size
);
751 if(!dst_size
) return src_size
;
753 if(dst_size
> src_size
) dst_size
= src_size
;
755 if(src_fmt
== CF_TEXT
)
756 CharToOemBuffA(src
, dst
, dst_size
);
758 OemToCharBuffA(src
, dst
, dst_size
);
764 /**************************************************************************
765 * X11DRV_CLIPBOARD_RenderSynthesizedFormat
767 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedFormat(Display
*display
, LPWINE_CLIPDATA lpData
)
773 if (lpData
->wFlags
& CF_FLAG_SYNTHESIZED
)
775 UINT wFormatID
= lpData
->wFormatID
;
777 if (wFormatID
== CF_UNICODETEXT
|| wFormatID
== CF_TEXT
|| wFormatID
== CF_OEMTEXT
)
778 bret
= X11DRV_CLIPBOARD_RenderSynthesizedText(display
, wFormatID
);
784 bret
= X11DRV_CLIPBOARD_RenderSynthesizedDIB( display
);
788 bret
= X11DRV_CLIPBOARD_RenderSynthesizedBitmap( display
);
792 bret
= X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile( display
);
795 case CF_METAFILEPICT
:
796 FIXME("Synthesizing CF_METAFILEPICT not implemented\n");
800 FIXME("Called to synthesize unknown format 0x%08x\n", wFormatID
);
805 lpData
->wFlags
&= ~CF_FLAG_SYNTHESIZED
;
812 /**************************************************************************
813 * X11DRV_CLIPBOARD_RenderSynthesizedText
815 * Renders synthesized text
817 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedText(Display
*display
, UINT wFormatID
)
822 INT src_chars
, dst_chars
, alloc_size
;
823 LPWINE_CLIPDATA lpSource
= NULL
;
825 TRACE("%04x\n", wFormatID
);
827 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(wFormatID
)) &&
831 /* Look for rendered source or non-synthesized source */
832 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT
)) &&
833 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
835 TRACE("UNICODETEXT -> %04x\n", wFormatID
);
837 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_TEXT
)) &&
838 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
840 TRACE("TEXT -> %04x\n", wFormatID
);
842 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT
)) &&
843 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
845 TRACE("OEMTEXT -> %04x\n", wFormatID
);
848 if (!lpSource
|| (lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
&&
852 /* Ask the clipboard owner to render the source text if necessary */
853 if (!lpSource
->hData
&& !X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
856 lpstrS
= GlobalLock(lpSource
->hData
);
860 /* Text always NULL terminated */
861 if(lpSource
->wFormatID
== CF_UNICODETEXT
)
862 src_chars
= strlenW((LPCWSTR
)lpstrS
) + 1;
864 src_chars
= strlen(lpstrS
) + 1;
866 /* Calculate number of characters in the destination buffer */
867 dst_chars
= CLIPBOARD_ConvertText(lpSource
->wFormatID
, lpstrS
,
868 src_chars
, wFormatID
, NULL
, 0);
873 TRACE("Converting from '%04x' to '%04x', %i chars\n",
874 lpSource
->wFormatID
, wFormatID
, src_chars
);
876 /* Convert characters to bytes */
877 if(wFormatID
== CF_UNICODETEXT
)
878 alloc_size
= dst_chars
* sizeof(WCHAR
);
880 alloc_size
= dst_chars
;
882 hData
= GlobalAlloc(GMEM_ZEROINIT
| GMEM_MOVEABLE
|
883 GMEM_DDESHARE
, alloc_size
);
885 lpstrT
= GlobalLock(hData
);
889 CLIPBOARD_ConvertText(lpSource
->wFormatID
, lpstrS
, src_chars
,
890 wFormatID
, lpstrT
, dst_chars
);
894 GlobalUnlock(lpSource
->hData
);
896 return X11DRV_CLIPBOARD_InsertClipboardData(wFormatID
, hData
, 0, NULL
, TRUE
);
900 /***********************************************************************
903 * Return the size of the bitmap info structure including color table.
905 static int bitmap_info_size( const BITMAPINFO
* info
, WORD coloruse
)
907 unsigned int colors
, size
, masks
= 0;
909 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
911 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
912 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
913 return sizeof(BITMAPCOREHEADER
) + colors
*
914 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
916 else /* assume BITMAPINFOHEADER */
918 colors
= info
->bmiHeader
.biClrUsed
;
919 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
920 colors
= 1 << info
->bmiHeader
.biBitCount
;
921 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
922 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
923 return size
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
928 /***********************************************************************
929 * create_dib_from_bitmap
931 * Allocates a packed DIB and copies the bitmap data into it.
933 static HGLOBAL
create_dib_from_bitmap(HBITMAP hBmp
)
939 LPBITMAPINFOHEADER pbmiHeader
;
940 unsigned int cDataSize
, cPackedSize
, OffsetBits
;
943 if (!GetObjectW( hBmp
, sizeof(bmp
), &bmp
)) return 0;
946 * A packed DIB contains a BITMAPINFO structure followed immediately by
947 * an optional color palette and the pixel data.
950 /* Calculate the size of the packed DIB */
951 cDataSize
= abs( bmp
.bmHeight
) * (((bmp
.bmWidth
* bmp
.bmBitsPixel
+ 31) / 8) & ~3);
952 cPackedSize
= sizeof(BITMAPINFOHEADER
)
953 + ( (bmp
.bmBitsPixel
<= 8) ? (sizeof(RGBQUAD
) * (1 << bmp
.bmBitsPixel
)) : 0 )
955 /* Get the offset to the bits */
956 OffsetBits
= cPackedSize
- cDataSize
;
958 /* Allocate the packed DIB */
959 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize
);
960 hPackedDIB
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
/*| GMEM_ZEROINIT*/,
964 WARN("Could not allocate packed DIB!\n");
968 /* A packed DIB starts with a BITMAPINFOHEADER */
969 pPackedDIB
= GlobalLock(hPackedDIB
);
970 pbmiHeader
= (LPBITMAPINFOHEADER
)pPackedDIB
;
972 /* Init the BITMAPINFOHEADER */
973 pbmiHeader
->biSize
= sizeof(BITMAPINFOHEADER
);
974 pbmiHeader
->biWidth
= bmp
.bmWidth
;
975 pbmiHeader
->biHeight
= bmp
.bmHeight
;
976 pbmiHeader
->biPlanes
= 1;
977 pbmiHeader
->biBitCount
= bmp
.bmBitsPixel
;
978 pbmiHeader
->biCompression
= BI_RGB
;
979 pbmiHeader
->biSizeImage
= 0;
980 pbmiHeader
->biXPelsPerMeter
= pbmiHeader
->biYPelsPerMeter
= 0;
981 pbmiHeader
->biClrUsed
= 0;
982 pbmiHeader
->biClrImportant
= 0;
984 /* Retrieve the DIB bits from the bitmap and fill in the
985 * DIB color table if present */
987 nLinesCopied
= GetDIBits(hdc
, /* Handle to device context */
988 hBmp
, /* Handle to bitmap */
989 0, /* First scan line to set in dest bitmap */
990 bmp
.bmHeight
, /* Number of scan lines to copy */
991 pPackedDIB
+ OffsetBits
, /* [out] Address of array for bitmap bits */
992 (LPBITMAPINFO
) pbmiHeader
, /* [out] Address of BITMAPINFO structure */
993 0); /* RGB or palette index */
994 GlobalUnlock(hPackedDIB
);
997 /* Cleanup if GetDIBits failed */
998 if (nLinesCopied
!= bmp
.bmHeight
)
1000 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied
, bmp
.bmHeight
);
1001 GlobalFree(hPackedDIB
);
1008 /**************************************************************************
1009 * X11DRV_CLIPBOARD_RenderSynthesizedDIB
1011 * Renders synthesized DIB
1013 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedDIB(Display
*display
)
1016 LPWINE_CLIPDATA lpSource
= NULL
;
1020 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) && lpSource
->hData
)
1024 /* If we have a bitmap and it's not synthesized or it has been rendered */
1025 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) &&
1026 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
1028 /* Render source if required */
1029 if (lpSource
->hData
|| X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
1031 HGLOBAL hData
= create_dib_from_bitmap( lpSource
->hData
);
1034 X11DRV_CLIPBOARD_InsertClipboardData(CF_DIB
, hData
, 0, NULL
, TRUE
);
1044 /**************************************************************************
1045 * X11DRV_CLIPBOARD_RenderSynthesizedBitmap
1047 * Renders synthesized bitmap
1049 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedBitmap(Display
*display
)
1052 LPWINE_CLIPDATA lpSource
= NULL
;
1056 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) && lpSource
->hData
)
1060 /* If we have a dib and it's not synthesized or it has been rendered */
1061 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) &&
1062 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
1064 /* Render source if required */
1065 if (lpSource
->hData
|| X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
1068 HBITMAP hData
= NULL
;
1069 unsigned int offset
;
1070 LPBITMAPINFOHEADER lpbmih
;
1073 lpbmih
= GlobalLock(lpSource
->hData
);
1076 offset
= sizeof(BITMAPINFOHEADER
)
1077 + ((lpbmih
->biBitCount
<= 8) ? (sizeof(RGBQUAD
) *
1078 (1 << lpbmih
->biBitCount
)) : 0);
1080 hData
= CreateDIBitmap(hdc
, lpbmih
, CBM_INIT
, (LPBYTE
)lpbmih
+
1081 offset
, (LPBITMAPINFO
) lpbmih
, DIB_RGB_COLORS
);
1083 GlobalUnlock(lpSource
->hData
);
1085 ReleaseDC(NULL
, hdc
);
1089 X11DRV_CLIPBOARD_InsertClipboardData(CF_BITMAP
, hData
, 0, NULL
, TRUE
);
1099 /**************************************************************************
1100 * X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile
1102 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile(Display
*display
)
1104 LPWINE_CLIPDATA lpSource
= NULL
;
1108 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_ENHMETAFILE
)) && lpSource
->hData
)
1110 /* If we have a MF pict and it's not synthesized or it has been rendered */
1111 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT
)) &&
1112 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
1114 /* Render source if required */
1115 if (lpSource
->hData
|| X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
1118 HENHMETAFILE hData
= NULL
;
1120 pmfp
= GlobalLock(lpSource
->hData
);
1123 UINT size_mf_bits
= GetMetaFileBitsEx(pmfp
->hMF
, 0, NULL
);
1124 void *mf_bits
= HeapAlloc(GetProcessHeap(), 0, size_mf_bits
);
1127 GetMetaFileBitsEx(pmfp
->hMF
, size_mf_bits
, mf_bits
);
1128 hData
= SetWinMetaFileBits(size_mf_bits
, mf_bits
, NULL
, pmfp
);
1129 HeapFree(GetProcessHeap(), 0, mf_bits
);
1131 GlobalUnlock(lpSource
->hData
);
1136 X11DRV_CLIPBOARD_InsertClipboardData(CF_ENHMETAFILE
, hData
, 0, NULL
, TRUE
);
1146 /**************************************************************************
1147 * X11DRV_CLIPBOARD_ImportXAString
1149 * Import XA_STRING, converting the string to CF_TEXT.
1151 static HANDLE
X11DRV_CLIPBOARD_ImportXAString(Display
*display
, Window w
, Atom prop
)
1154 unsigned long cbytes
;
1156 unsigned long i
, inlcount
= 0;
1159 if (!X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1162 for (i
= 0; i
<= cbytes
; i
++)
1164 if (lpdata
[i
] == '\n')
1168 if ((hText
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cbytes
+ inlcount
+ 1)))
1170 lpstr
= GlobalLock(hText
);
1172 for (i
= 0, inlcount
= 0; i
<= cbytes
; i
++)
1174 if (lpdata
[i
] == '\n')
1175 lpstr
[inlcount
++] = '\r';
1177 lpstr
[inlcount
++] = lpdata
[i
];
1180 GlobalUnlock(hText
);
1183 /* Free the retrieved property data */
1184 HeapFree(GetProcessHeap(), 0, lpdata
);
1190 /**************************************************************************
1191 * X11DRV_CLIPBOARD_ImportUTF8
1193 * Import XA_STRING, converting the string to CF_UNICODE.
1195 static HANDLE
X11DRV_CLIPBOARD_ImportUTF8(Display
*display
, Window w
, Atom prop
)
1198 unsigned long cbytes
;
1200 unsigned long i
, inlcount
= 0;
1201 HANDLE hUnicodeText
= 0;
1203 if (!X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1206 for (i
= 0; i
<= cbytes
; i
++)
1208 if (lpdata
[i
] == '\n')
1212 if ((lpstr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cbytes
+ inlcount
+ 1)))
1216 for (i
= 0, inlcount
= 0; i
<= cbytes
; i
++)
1218 if (lpdata
[i
] == '\n')
1219 lpstr
[inlcount
++] = '\r';
1221 lpstr
[inlcount
++] = lpdata
[i
];
1224 count
= MultiByteToWideChar(CP_UTF8
, 0, lpstr
, -1, NULL
, 0);
1225 hUnicodeText
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, count
* sizeof(WCHAR
));
1229 WCHAR
*textW
= GlobalLock(hUnicodeText
);
1230 MultiByteToWideChar(CP_UTF8
, 0, lpstr
, -1, textW
, count
);
1231 GlobalUnlock(hUnicodeText
);
1234 HeapFree(GetProcessHeap(), 0, lpstr
);
1237 /* Free the retrieved property data */
1238 HeapFree(GetProcessHeap(), 0, lpdata
);
1240 return hUnicodeText
;
1244 /**************************************************************************
1245 * X11DRV_CLIPBOARD_ImportCompoundText
1247 * Import COMPOUND_TEXT to CF_UNICODE
1249 static HANDLE
X11DRV_CLIPBOARD_ImportCompoundText(Display
*display
, Window w
, Atom prop
)
1254 int srclen
, destlen
;
1255 HANDLE hUnicodeText
;
1256 XTextProperty txtprop
;
1258 if (!X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &txtprop
.value
, &txtprop
.nitems
))
1263 txtprop
.encoding
= x11drv_atom(COMPOUND_TEXT
);
1265 ret
= XmbTextPropertyToTextList(display
, &txtprop
, &srcstr
, &count
);
1266 HeapFree(GetProcessHeap(), 0, txtprop
.value
);
1267 if (ret
!= Success
|| !count
) return 0;
1269 TRACE("Importing %d line(s)\n", count
);
1271 /* Compute number of lines */
1272 srclen
= strlen(srcstr
[0]);
1273 for (i
= 0, lcount
= 0; i
<= srclen
; i
++)
1275 if (srcstr
[0][i
] == '\n')
1279 destlen
= MultiByteToWideChar(CP_UNIXCP
, 0, srcstr
[0], -1, NULL
, 0);
1281 TRACE("lcount = %d, destlen=%d, srcstr %s\n", lcount
, destlen
, srcstr
[0]);
1283 if ((hUnicodeText
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, (destlen
+ lcount
+ 1) * sizeof(WCHAR
))))
1285 WCHAR
*deststr
= GlobalLock(hUnicodeText
);
1286 MultiByteToWideChar(CP_UNIXCP
, 0, srcstr
[0], -1, deststr
, destlen
);
1290 for (i
= destlen
- 1, j
= destlen
+ lcount
- 1; i
>= 0; i
--, j
--)
1292 deststr
[j
] = deststr
[i
];
1294 if (deststr
[i
] == '\n')
1295 deststr
[--j
] = '\r';
1299 GlobalUnlock(hUnicodeText
);
1302 XFreeStringList(srcstr
);
1304 return hUnicodeText
;
1308 /**************************************************************************
1309 * X11DRV_CLIPBOARD_ImportXAPIXMAP
1311 * Import XA_PIXMAP, converting the image to CF_DIB.
1313 static HANDLE
X11DRV_CLIPBOARD_ImportXAPIXMAP(Display
*display
, Window w
, Atom prop
)
1316 unsigned long cbytes
;
1318 HANDLE hClipData
= 0;
1320 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1322 XVisualInfo vis
= default_visual
;
1323 char buffer
[FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )];
1324 BITMAPINFO
*info
= (BITMAPINFO
*)buffer
;
1325 struct gdi_image_bits bits
;
1327 int x
,y
; /* Unused */
1328 unsigned border_width
; /* Unused */
1329 unsigned int depth
, width
, height
;
1331 pPixmap
= (Pixmap
*) lpdata
;
1333 /* Get the Pixmap dimensions and bit depth */
1334 if (!XGetGeometry(gdi_display
, *pPixmap
, &root
, &x
, &y
, &width
, &height
,
1335 &border_width
, &depth
)) depth
= 0;
1336 if (!pixmap_formats
[depth
]) return 0;
1338 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
1339 width
, height
, depth
);
1341 if (depth
!= vis
.depth
) switch (pixmap_formats
[depth
]->bits_per_pixel
)
1347 case 16: /* assume R5G5B5 */
1348 vis
.red_mask
= 0x7c00;
1349 vis
.green_mask
= 0x03e0;
1350 vis
.blue_mask
= 0x001f;
1352 case 24: /* assume R8G8B8 */
1353 case 32: /* assume A8R8G8B8 */
1354 vis
.red_mask
= 0xff0000;
1355 vis
.green_mask
= 0x00ff00;
1356 vis
.blue_mask
= 0x0000ff;
1362 if (!get_pixmap_image( *pPixmap
, width
, height
, &vis
, info
, &bits
))
1364 DWORD info_size
= bitmap_info_size( info
, DIB_RGB_COLORS
);
1367 hClipData
= GlobalAlloc( GMEM_MOVEABLE
| GMEM_DDESHARE
,
1368 info_size
+ info
->bmiHeader
.biSizeImage
);
1371 ptr
= GlobalLock( hClipData
);
1372 memcpy( ptr
, info
, info_size
);
1373 memcpy( ptr
+ info_size
, bits
.ptr
, info
->bmiHeader
.biSizeImage
);
1374 GlobalUnlock( hClipData
);
1376 if (bits
.free
) bits
.free( &bits
);
1379 HeapFree(GetProcessHeap(), 0, lpdata
);
1386 /**************************************************************************
1387 * X11DRV_CLIPBOARD_ImportImageBmp
1389 * Import image/bmp, converting the image to CF_DIB.
1391 static HANDLE
X11DRV_CLIPBOARD_ImportImageBmp(Display
*display
, Window w
, Atom prop
)
1394 unsigned long cbytes
;
1395 HANDLE hClipData
= 0;
1397 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1399 BITMAPFILEHEADER
*bfh
= (BITMAPFILEHEADER
*)lpdata
;
1401 if (cbytes
>= sizeof(BITMAPFILEHEADER
)+sizeof(BITMAPCOREHEADER
) &&
1402 bfh
->bfType
== 0x4d42 /* "BM" */)
1404 BITMAPINFO
*bmi
= (BITMAPINFO
*)(bfh
+1);
1409 hbmp
= CreateDIBitmap(
1413 lpdata
+bfh
->bfOffBits
,
1418 hClipData
= create_dib_from_bitmap( hbmp
);
1424 /* Free the retrieved property data */
1425 HeapFree(GetProcessHeap(), 0, lpdata
);
1432 /**************************************************************************
1433 * X11DRV_CLIPBOARD_ImportMetaFilePict
1435 * Import MetaFilePict.
1437 static HANDLE
X11DRV_CLIPBOARD_ImportMetaFilePict(Display
*display
, Window w
, Atom prop
)
1440 unsigned long cbytes
;
1441 HANDLE hClipData
= 0;
1443 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1446 hClipData
= X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT
, lpdata
, (LPDWORD
)&cbytes
, FALSE
);
1448 /* Free the retrieved property data */
1449 HeapFree(GetProcessHeap(), 0, lpdata
);
1456 /**************************************************************************
1457 * X11DRV_ImportEnhMetaFile
1459 * Import EnhMetaFile.
1461 static HANDLE
X11DRV_CLIPBOARD_ImportEnhMetaFile(Display
*display
, Window w
, Atom prop
)
1464 unsigned long cbytes
;
1465 HANDLE hClipData
= 0;
1467 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1470 hClipData
= X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE
, lpdata
, (LPDWORD
)&cbytes
, FALSE
);
1472 /* Free the retrieved property data */
1473 HeapFree(GetProcessHeap(), 0, lpdata
);
1480 /**************************************************************************
1481 * X11DRV_ImportClipbordaData
1483 * Generic import clipboard data routine.
1485 static HANDLE
X11DRV_CLIPBOARD_ImportClipboardData(Display
*display
, Window w
, Atom prop
)
1489 unsigned long cbytes
;
1490 HANDLE hClipData
= 0;
1492 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1496 /* Turn on the DDESHARE flag to enable shared 32 bit memory */
1497 hClipData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cbytes
);
1500 HeapFree(GetProcessHeap(), 0, lpdata
);
1504 if ((lpClipData
= GlobalLock(hClipData
)))
1506 memcpy(lpClipData
, lpdata
, cbytes
);
1507 GlobalUnlock(hClipData
);
1511 GlobalFree(hClipData
);
1516 /* Free the retrieved property data */
1517 HeapFree(GetProcessHeap(), 0, lpdata
);
1523 /**************************************************************************
1524 * X11DRV_CLIPBOARD_ImportSelection
1526 * Import the X selection into the clipboard format registered for the given X target.
1528 HANDLE
X11DRV_CLIPBOARD_ImportSelection(Display
*d
, Atom target
, Window w
, Atom prop
, UINT
*windowsFormat
)
1530 WINE_CLIPFORMAT
*clipFormat
;
1532 clipFormat
= X11DRV_CLIPBOARD_LookupProperty(NULL
, target
);
1535 *windowsFormat
= clipFormat
->wFormatID
;
1536 return clipFormat
->lpDrvImportFunc(d
, w
, prop
);
1542 /**************************************************************************
1543 X11DRV_CLIPBOARD_ExportClipboardData
1545 * Generic export clipboard data routine.
1547 static HANDLE
X11DRV_CLIPBOARD_ExportClipboardData(Display
*display
, Window requestor
, Atom aTarget
,
1548 Atom rprop
, LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1552 HANDLE hClipData
= 0;
1554 *lpBytes
= 0; /* Assume failure */
1556 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpData
))
1557 ERR("Failed to export %04x format\n", lpData
->wFormatID
);
1560 datasize
= GlobalSize(lpData
->hData
);
1562 hClipData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, datasize
);
1563 if (hClipData
== 0) return NULL
;
1565 if ((lpClipData
= GlobalLock(hClipData
)))
1567 LPVOID lpdata
= GlobalLock(lpData
->hData
);
1569 memcpy(lpClipData
, lpdata
, datasize
);
1570 *lpBytes
= datasize
;
1572 GlobalUnlock(lpData
->hData
);
1573 GlobalUnlock(hClipData
);
1575 GlobalFree(hClipData
);
1584 /**************************************************************************
1585 * X11DRV_CLIPBOARD_ExportXAString
1587 * Export CF_TEXT converting the string to XA_STRING.
1588 * Helper function for X11DRV_CLIPBOARD_ExportString.
1590 static HANDLE
X11DRV_CLIPBOARD_ExportXAString(LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1594 LPSTR text
, lpstr
= NULL
;
1596 *lpBytes
= 0; /* Assume return has zero bytes */
1598 text
= GlobalLock(lpData
->hData
);
1599 size
= strlen(text
);
1601 /* remove carriage returns */
1602 lpstr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
+ 1);
1606 for (i
= 0,j
= 0; i
< size
&& text
[i
]; i
++)
1608 if (text
[i
] == '\r' && (text
[i
+1] == '\n' || text
[i
+1] == '\0'))
1610 lpstr
[j
++] = text
[i
];
1614 *lpBytes
= j
; /* Number of bytes in string */
1617 GlobalUnlock(lpData
->hData
);
1623 /**************************************************************************
1624 * X11DRV_CLIPBOARD_ExportUTF8String
1626 * Export CF_UNICODE converting the string to UTF8.
1627 * Helper function for X11DRV_CLIPBOARD_ExportString.
1629 static HANDLE
X11DRV_CLIPBOARD_ExportUTF8String(LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1634 LPSTR text
, lpstr
= NULL
;
1636 *lpBytes
= 0; /* Assume return has zero bytes */
1638 uni_text
= GlobalLock(lpData
->hData
);
1640 size
= WideCharToMultiByte(CP_UTF8
, 0, uni_text
, -1, NULL
, 0, NULL
, NULL
);
1642 text
= HeapAlloc(GetProcessHeap(), 0, size
);
1645 WideCharToMultiByte(CP_UTF8
, 0, uni_text
, -1, text
, size
, NULL
, NULL
);
1647 /* remove carriage returns */
1648 lpstr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
--);
1652 for (i
= 0,j
= 0; i
< size
&& text
[i
]; i
++)
1654 if (text
[i
] == '\r' && (text
[i
+1] == '\n' || text
[i
+1] == '\0'))
1656 lpstr
[j
++] = text
[i
];
1660 *lpBytes
= j
; /* Number of bytes in string */
1663 HeapFree(GetProcessHeap(), 0, text
);
1664 GlobalUnlock(lpData
->hData
);
1671 /**************************************************************************
1672 * X11DRV_CLIPBOARD_ExportCompoundText
1674 * Export CF_UNICODE to COMPOUND_TEXT
1675 * Helper function for X11DRV_CLIPBOARD_ExportString.
1677 static HANDLE
X11DRV_CLIPBOARD_ExportCompoundText(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1678 LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1682 XICCEncodingStyle style
;
1687 uni_text
= GlobalLock(lpData
->hData
);
1689 size
= WideCharToMultiByte(CP_UNIXCP
, 0, uni_text
, -1, NULL
, 0, NULL
, NULL
);
1690 lpstr
= HeapAlloc(GetProcessHeap(), 0, size
);
1694 WideCharToMultiByte(CP_UNIXCP
, 0, uni_text
, -1, lpstr
, size
, NULL
, NULL
);
1696 /* remove carriage returns */
1697 for (i
= 0, j
= 0; i
< size
&& lpstr
[i
]; i
++)
1699 if (lpstr
[i
] == '\r' && (lpstr
[i
+1] == '\n' || lpstr
[i
+1] == '\0'))
1701 lpstr
[j
++] = lpstr
[i
];
1705 GlobalUnlock(lpData
->hData
);
1707 if (aTarget
== x11drv_atom(COMPOUND_TEXT
))
1708 style
= XCompoundTextStyle
;
1710 style
= XStdICCTextStyle
;
1712 /* Update the X property */
1713 if (XmbTextListToTextProperty(display
, &lpstr
, 1, style
, &prop
) == Success
)
1715 XSetTextProperty(display
, requestor
, &prop
, rprop
);
1719 HeapFree(GetProcessHeap(), 0, lpstr
);
1724 /**************************************************************************
1725 * X11DRV_CLIPBOARD_ExportString
1729 static HANDLE
X11DRV_CLIPBOARD_ExportString(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1730 LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1732 if (X11DRV_CLIPBOARD_RenderFormat(display
, lpData
))
1734 if (aTarget
== XA_STRING
)
1735 return X11DRV_CLIPBOARD_ExportXAString(lpData
, lpBytes
);
1736 else if (aTarget
== x11drv_atom(COMPOUND_TEXT
) || aTarget
== x11drv_atom(TEXT
))
1737 return X11DRV_CLIPBOARD_ExportCompoundText(display
, requestor
, aTarget
,
1738 rprop
, lpData
, lpBytes
);
1741 TRACE("Exporting target %ld to default UTF8_STRING\n", aTarget
);
1742 return X11DRV_CLIPBOARD_ExportUTF8String(lpData
, lpBytes
);
1746 ERR("Failed to render %04x format\n", lpData
->wFormatID
);
1752 /**************************************************************************
1753 * X11DRV_CLIPBOARD_ExportXAPIXMAP
1755 * Export CF_DIB to XA_PIXMAP.
1757 static HANDLE
X11DRV_CLIPBOARD_ExportXAPIXMAP(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1758 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1761 unsigned char* lpData
;
1763 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1765 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1769 if (!lpdata
->drvData
) /* If not already rendered */
1773 struct gdi_image_bits bits
;
1775 pbmi
= GlobalLock( lpdata
->hData
);
1776 bits
.ptr
= (LPBYTE
)pbmi
+ bitmap_info_size( pbmi
, DIB_RGB_COLORS
);
1778 bits
.is_copy
= FALSE
;
1779 pixmap
= create_pixmap_from_image( 0, &default_visual
, pbmi
, &bits
, DIB_RGB_COLORS
);
1780 GlobalUnlock( lpdata
->hData
);
1781 lpdata
->drvData
= pixmap
;
1784 *lpBytes
= sizeof(Pixmap
); /* pixmap is a 32bit value */
1786 /* Wrap pixmap so we can return a handle */
1787 hData
= GlobalAlloc(0, *lpBytes
);
1788 lpData
= GlobalLock(hData
);
1789 memcpy(lpData
, &lpdata
->drvData
, *lpBytes
);
1790 GlobalUnlock(hData
);
1796 /**************************************************************************
1797 * X11DRV_CLIPBOARD_ExportImageBmp
1799 * Export CF_DIB to image/bmp.
1801 static HANDLE
X11DRV_CLIPBOARD_ExportImageBmp(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1802 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1809 BITMAPFILEHEADER
*bfh
;
1813 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1815 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1819 hpackeddib
= lpdata
->hData
;
1821 dibdata
= GlobalLock(hpackeddib
);
1824 ERR("Failed to lock packed DIB\n");
1828 bmpsize
= sizeof(BITMAPFILEHEADER
) + GlobalSize(hpackeddib
);
1830 hbmpdata
= GlobalAlloc(0, bmpsize
);
1834 bmpdata
= GlobalLock(hbmpdata
);
1838 GlobalFree(hbmpdata
);
1839 GlobalUnlock(hpackeddib
);
1843 /* bitmap file header */
1844 bfh
= (BITMAPFILEHEADER
*)bmpdata
;
1845 bfh
->bfType
= 0x4d42; /* "BM" */
1846 bfh
->bfSize
= bmpsize
;
1847 bfh
->bfReserved1
= 0;
1848 bfh
->bfReserved2
= 0;
1849 bfh
->bfOffBits
= sizeof(BITMAPFILEHEADER
) + bitmap_info_size((BITMAPINFO
*)dibdata
, DIB_RGB_COLORS
);
1851 /* rest of bitmap is the same as the packed dib */
1852 memcpy(bfh
+1, dibdata
, bmpsize
-sizeof(BITMAPFILEHEADER
));
1856 GlobalUnlock(hbmpdata
);
1859 GlobalUnlock(hpackeddib
);
1865 /**************************************************************************
1866 * X11DRV_CLIPBOARD_ExportMetaFilePict
1868 * Export MetaFilePict.
1870 static HANDLE
X11DRV_CLIPBOARD_ExportMetaFilePict(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1871 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1873 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1875 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1879 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT
, lpdata
->hData
, lpBytes
, TRUE
);
1883 /**************************************************************************
1884 * X11DRV_CLIPBOARD_ExportEnhMetaFile
1886 * Export EnhMetaFile.
1888 static HANDLE
X11DRV_CLIPBOARD_ExportEnhMetaFile(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1889 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1891 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1893 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1897 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE
, lpdata
->hData
, lpBytes
, TRUE
);
1901 /**************************************************************************
1902 * get_html_description_field
1904 * Find the value of a field in an HTML Format description.
1906 static LPCSTR
get_html_description_field(LPCSTR data
, LPCSTR keyword
)
1910 while (pos
&& *pos
&& *pos
!= '<')
1912 if (memcmp(pos
, keyword
, strlen(keyword
)) == 0)
1913 return pos
+strlen(keyword
);
1915 pos
= strchr(pos
, '\n');
1923 /**************************************************************************
1924 * X11DRV_CLIPBOARD_ExportTextHtml
1926 * Export HTML Format to text/html.
1928 * FIXME: We should attempt to add an <a base> tag and convert windows paths.
1930 static HANDLE
X11DRV_CLIPBOARD_ExportTextHtml(Display
*display
, Window requestor
, Atom aTarget
,
1931 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1934 LPCSTR data
, field_value
;
1935 UINT fragmentstart
, fragmentend
, htmlsize
;
1936 HANDLE hhtmldata
=NULL
;
1941 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1943 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1947 hdata
= lpdata
->hData
;
1949 data
= GlobalLock(hdata
);
1952 ERR("Failed to lock HTML Format data\n");
1956 /* read the important fields */
1957 field_value
= get_html_description_field(data
, "StartFragment:");
1960 ERR("Couldn't find StartFragment value\n");
1963 fragmentstart
= atoi(field_value
);
1965 field_value
= get_html_description_field(data
, "EndFragment:");
1968 ERR("Couldn't find EndFragment value\n");
1971 fragmentend
= atoi(field_value
);
1973 /* export only the fragment */
1974 htmlsize
= fragmentend
- fragmentstart
+ 1;
1976 hhtmldata
= GlobalAlloc(0, htmlsize
);
1980 htmldata
= GlobalLock(hhtmldata
);
1984 GlobalFree(hhtmldata
);
1989 memcpy(htmldata
, &data
[fragmentstart
], fragmentend
-fragmentstart
);
1990 htmldata
[htmlsize
-1] = '\0';
1992 *lpBytes
= htmlsize
;
1994 GlobalUnlock(htmldata
);
1999 GlobalUnlock(hdata
);
2005 /**************************************************************************
2006 * X11DRV_CLIPBOARD_QueryTargets
2008 static BOOL
X11DRV_CLIPBOARD_QueryTargets(Display
*display
, Window w
, Atom selection
,
2009 Atom target
, XEvent
*xe
)
2013 XConvertSelection(display
, selection
, target
, x11drv_atom(SELECTION_DATA
), w
, CurrentTime
);
2016 * Wait until SelectionNotify is received
2018 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
2020 Bool res
= XCheckTypedWindowEvent(display
, w
, SelectionNotify
, xe
);
2021 if (res
&& xe
->xselection
.selection
== selection
) break;
2023 usleep(SELECTION_WAIT
);
2026 if (i
== SELECTION_RETRIES
)
2028 ERR("Timed out waiting for SelectionNotify event\n");
2031 /* Verify that the selection returned a valid TARGETS property */
2032 if ((xe
->xselection
.target
!= target
) || (xe
->xselection
.property
== None
))
2034 /* Selection owner failed to respond or we missed the SelectionNotify */
2035 WARN("Failed to retrieve TARGETS for selection %ld.\n", selection
);
2043 static int is_atom_error( Display
*display
, XErrorEvent
*event
, void *arg
)
2045 return (event
->error_code
== BadAtom
);
2048 /**************************************************************************
2049 * X11DRV_CLIPBOARD_InsertSelectionProperties
2051 * Mark properties available for future retrieval.
2053 static VOID
X11DRV_CLIPBOARD_InsertSelectionProperties(Display
*display
, Atom
* properties
, UINT count
)
2055 UINT i
, nb_atoms
= 0;
2058 /* Cache these formats in the clipboard cache */
2059 for (i
= 0; i
< count
; i
++)
2061 LPWINE_CLIPFORMAT lpFormat
= X11DRV_CLIPBOARD_LookupProperty(NULL
, properties
[i
]);
2065 /* We found at least one Window's format that mapps to the property.
2066 * Continue looking for more.
2068 * If more than one property map to a Window's format then we use the first
2069 * one and ignore the rest.
2073 TRACE("Atom#%d Property(%d): --> Format %s\n",
2074 i
, lpFormat
->drvData
, debugstr_format(lpFormat
->wFormatID
));
2075 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat
->wFormatID
, 0, 0, lpFormat
, FALSE
);
2076 lpFormat
= X11DRV_CLIPBOARD_LookupProperty(lpFormat
, properties
[i
]);
2079 else if (properties
[i
])
2081 /* add it to the list of atoms that we don't know about yet */
2082 if (!atoms
) atoms
= HeapAlloc( GetProcessHeap(), 0,
2083 (count
- i
) * sizeof(*atoms
) );
2084 if (atoms
) atoms
[nb_atoms
++] = properties
[i
];
2088 /* query all unknown atoms in one go */
2091 char **names
= HeapAlloc( GetProcessHeap(), 0, nb_atoms
* sizeof(*names
) );
2094 X11DRV_expect_error( display
, is_atom_error
, NULL
);
2095 if (!XGetAtomNames( display
, atoms
, nb_atoms
, names
)) nb_atoms
= 0;
2096 if (X11DRV_check_error())
2098 WARN( "got some bad atoms, ignoring\n" );
2101 for (i
= 0; i
< nb_atoms
; i
++)
2103 WINE_CLIPFORMAT
*lpFormat
;
2105 int len
= MultiByteToWideChar(CP_UNIXCP
, 0, names
[i
], -1, NULL
, 0);
2106 wname
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
2107 MultiByteToWideChar(CP_UNIXCP
, 0, names
[i
], -1, wname
, len
);
2109 lpFormat
= register_format( RegisterClipboardFormatW(wname
), atoms
[i
] );
2110 HeapFree(GetProcessHeap(), 0, wname
);
2113 ERR("Failed to register %s property. Type will not be cached.\n", names
[i
]);
2116 TRACE("Atom#%d Property(%d): --> Format %s\n",
2117 i
, lpFormat
->drvData
, debugstr_format(lpFormat
->wFormatID
));
2118 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat
->wFormatID
, 0, 0, lpFormat
, FALSE
);
2120 for (i
= 0; i
< nb_atoms
; i
++) XFree( names
[i
] );
2121 HeapFree( GetProcessHeap(), 0, names
);
2123 HeapFree( GetProcessHeap(), 0, atoms
);
2128 /**************************************************************************
2129 * X11DRV_CLIPBOARD_QueryAvailableData
2131 * Caches the list of data formats available from the current selection.
2132 * This queries the selection owner for the TARGETS property and saves all
2133 * reported property types.
2135 static int X11DRV_CLIPBOARD_QueryAvailableData(Display
*display
, LPCLIPBOARDINFO lpcbinfo
)
2138 Atom atype
=AnyPropertyType
;
2140 unsigned long remain
;
2141 Atom
* targetList
=NULL
;
2143 unsigned long cSelectionTargets
= 0;
2145 if (selectionAcquired
& (S_PRIMARY
| S_CLIPBOARD
))
2147 ERR("Received request to cache selection but process is owner=(%08x)\n",
2148 (unsigned) selectionWindow
);
2149 return -1; /* Prevent self request */
2152 w
= thread_selection_wnd();
2155 ERR("No window available to retrieve selection!\n");
2160 * Query the selection owner for the TARGETS property
2162 if ((use_primary_selection
&& XGetSelectionOwner(display
,XA_PRIMARY
)) ||
2163 XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)))
2165 if (use_primary_selection
&& (X11DRV_CLIPBOARD_QueryTargets(display
, w
, XA_PRIMARY
, x11drv_atom(TARGETS
), &xe
)))
2166 selectionCacheSrc
= XA_PRIMARY
;
2167 else if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, x11drv_atom(CLIPBOARD
), x11drv_atom(TARGETS
), &xe
))
2168 selectionCacheSrc
= x11drv_atom(CLIPBOARD
);
2171 Atom xstr
= XA_STRING
;
2173 /* Selection Owner doesn't understand TARGETS, try retrieving XA_STRING */
2174 if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, XA_PRIMARY
, XA_STRING
, &xe
))
2176 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, &xstr
, 1);
2177 selectionCacheSrc
= XA_PRIMARY
;
2180 else if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, x11drv_atom(CLIPBOARD
), XA_STRING
, &xe
))
2182 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, &xstr
, 1);
2183 selectionCacheSrc
= x11drv_atom(CLIPBOARD
);
2188 WARN("Failed to query selection owner for available data.\n");
2193 else return 0; /* No selection owner so report 0 targets available */
2195 /* Read the TARGETS property contents */
2196 if (!XGetWindowProperty(display
, xe
.xselection
.requestor
, xe
.xselection
.property
,
2197 0, 0x3FFF, True
, AnyPropertyType
/*XA_ATOM*/, &atype
, &aformat
, &cSelectionTargets
,
2198 &remain
, (unsigned char**)&targetList
) != Success
)
2200 TRACE("Type %lx,Format %d,nItems %ld, Remain %ld\n",
2201 atype
, aformat
, cSelectionTargets
, remain
);
2203 * The TARGETS property should have returned us a list of atoms
2204 * corresponding to each selection target format supported.
2206 if (atype
== XA_ATOM
|| atype
== x11drv_atom(TARGETS
))
2210 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, targetList
, cSelectionTargets
);
2212 else if (aformat
== 8) /* work around quartz-wm brain damage */
2214 unsigned long i
, count
= cSelectionTargets
/ sizeof(CARD32
);
2215 Atom
*atoms
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(Atom
) );
2216 for (i
= 0; i
< count
; i
++)
2217 atoms
[i
] = ((CARD32
*)targetList
)[i
]; /* FIXME: byte swapping */
2218 X11DRV_CLIPBOARD_InsertSelectionProperties( display
, atoms
, count
);
2219 HeapFree( GetProcessHeap(), 0, atoms
);
2223 /* Free the list of targets */
2226 else WARN("Failed to read TARGETS property\n");
2228 return cSelectionTargets
;
2232 /**************************************************************************
2233 * X11DRV_CLIPBOARD_ReadSelectionData
2235 * This method is invoked only when we DO NOT own the X selection
2237 * We always get the data from the selection client each time,
2238 * since we have no way of determining if the data in our cache is stale.
2240 static BOOL
X11DRV_CLIPBOARD_ReadSelectionData(Display
*display
, LPWINE_CLIPDATA lpData
)
2247 TRACE("%04x\n", lpData
->wFormatID
);
2249 if (!lpData
->lpFormat
)
2251 ERR("Requesting format %04x but no source format linked to data.\n",
2256 if (!selectionAcquired
)
2258 Window w
= thread_selection_wnd();
2261 ERR("No window available to read selection data!\n");
2265 TRACE("Requesting conversion of %s property (%d) from selection type %08x\n",
2266 debugstr_format(lpData
->lpFormat
->wFormatID
), lpData
->lpFormat
->drvData
,
2267 (UINT
)selectionCacheSrc
);
2269 XConvertSelection(display
, selectionCacheSrc
, lpData
->lpFormat
->drvData
,
2270 x11drv_atom(SELECTION_DATA
), w
, CurrentTime
);
2272 /* wait until SelectionNotify is received */
2273 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
2275 res
= XCheckTypedWindowEvent(display
, w
, SelectionNotify
, &xe
);
2276 if (res
&& xe
.xselection
.selection
== selectionCacheSrc
) break;
2278 usleep(SELECTION_WAIT
);
2281 if (i
== SELECTION_RETRIES
)
2283 ERR("Timed out waiting for SelectionNotify event\n");
2285 /* Verify that the selection returned a valid TARGETS property */
2286 else if (xe
.xselection
.property
!= None
)
2289 * Read the contents of the X selection property
2290 * into WINE's clipboard cache and converting the
2291 * data format if necessary.
2293 HANDLE hData
= lpData
->lpFormat
->lpDrvImportFunc(display
, xe
.xselection
.requestor
,
2294 xe
.xselection
.property
);
2297 bRet
= X11DRV_CLIPBOARD_InsertClipboardData(lpData
->wFormatID
, hData
, 0, lpData
->lpFormat
, TRUE
);
2299 TRACE("Import function failed\n");
2303 TRACE("Failed to convert selection\n");
2308 ERR("Received request to cache selection data but process is owner\n");
2311 TRACE("Returning %d\n", bRet
);
2317 /**************************************************************************
2318 * X11DRV_CLIPBOARD_GetProperty
2319 * Gets type, data and size.
2321 static BOOL
X11DRV_CLIPBOARD_GetProperty(Display
*display
, Window w
, Atom prop
,
2322 Atom
*atype
, unsigned char** data
, unsigned long* datasize
)
2325 unsigned long pos
= 0, nitems
, remain
, count
;
2326 unsigned char *val
= NULL
, *buffer
;
2328 TRACE("Reading property %lu from X window %lx\n", prop
, w
);
2332 if (XGetWindowProperty(display
, w
, prop
, pos
, INT_MAX
/ 4, False
,
2333 AnyPropertyType
, atype
, &aformat
, &nitems
, &remain
, &buffer
) != Success
)
2335 WARN("Failed to read property\n");
2336 HeapFree( GetProcessHeap(), 0, val
);
2340 count
= get_property_size( aformat
, nitems
);
2341 if (!val
) *data
= HeapAlloc( GetProcessHeap(), 0, pos
* sizeof(int) + count
+ 1 );
2342 else *data
= HeapReAlloc( GetProcessHeap(), 0, val
, pos
* sizeof(int) + count
+ 1 );
2347 HeapFree( GetProcessHeap(), 0, val
);
2351 memcpy( (int *)val
+ pos
, buffer
, count
);
2355 *datasize
= pos
* sizeof(int) + count
;
2359 pos
+= count
/ sizeof(int);
2362 /* Delete the property on the window now that we are done
2363 * This will send a PropertyNotify event to the selection owner. */
2364 XDeleteProperty(display
, w
, prop
);
2369 struct clipboard_data_packet
{
2372 unsigned char *data
;
2375 /**************************************************************************
2376 * X11DRV_CLIPBOARD_ReadProperty
2377 * Reads the contents of the X selection property.
2379 static BOOL
X11DRV_CLIPBOARD_ReadProperty(Display
*display
, Window w
, Atom prop
,
2380 unsigned char** data
, unsigned long* datasize
)
2388 while (XCheckTypedWindowEvent(display
, w
, PropertyNotify
, &xe
))
2391 if (!X11DRV_CLIPBOARD_GetProperty(display
, w
, prop
, &atype
, data
, datasize
))
2394 if (atype
== x11drv_atom(INCR
))
2397 unsigned long bufsize
= 0;
2398 struct list packets
;
2399 struct clipboard_data_packet
*packet
, *packet2
;
2402 HeapFree(GetProcessHeap(), 0, *data
);
2405 list_init(&packets
);
2410 unsigned char *prop_data
;
2411 unsigned long prop_size
;
2413 /* Wait until PropertyNotify is received */
2414 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
2418 res
= XCheckTypedWindowEvent(display
, w
, PropertyNotify
, &xe
);
2419 if (res
&& xe
.xproperty
.atom
== prop
&&
2420 xe
.xproperty
.state
== PropertyNewValue
)
2422 usleep(SELECTION_WAIT
);
2425 if (i
>= SELECTION_RETRIES
||
2426 !X11DRV_CLIPBOARD_GetProperty(display
, w
, prop
, &atype
, &prop_data
, &prop_size
))
2432 /* Retrieved entire data. */
2435 HeapFree(GetProcessHeap(), 0, prop_data
);
2440 packet
= HeapAlloc(GetProcessHeap(), 0, sizeof(*packet
));
2443 HeapFree(GetProcessHeap(), 0, prop_data
);
2448 packet
->size
= prop_size
;
2449 packet
->data
= prop_data
;
2450 list_add_tail(&packets
, &packet
->entry
);
2451 bufsize
+= prop_size
;
2456 buf
= HeapAlloc(GetProcessHeap(), 0, bufsize
+ 1);
2459 unsigned long bytes_copied
= 0;
2460 *datasize
= bufsize
;
2461 LIST_FOR_EACH_ENTRY( packet
, &packets
, struct clipboard_data_packet
, entry
)
2463 memcpy(&buf
[bytes_copied
], packet
->data
, packet
->size
);
2464 bytes_copied
+= packet
->size
;
2473 LIST_FOR_EACH_ENTRY_SAFE( packet
, packet2
, &packets
, struct clipboard_data_packet
, entry
)
2475 HeapFree(GetProcessHeap(), 0, packet
->data
);
2476 HeapFree(GetProcessHeap(), 0, packet
);
2486 /**************************************************************************
2487 * CLIPBOARD_SerializeMetafile
2489 static HANDLE
X11DRV_CLIPBOARD_SerializeMetafile(INT wformat
, HANDLE hdata
, LPDWORD lpcbytes
, BOOL out
)
2493 TRACE(" wFormat=%d hdata=%p out=%d\n", wformat
, hdata
, out
);
2495 if (out
) /* Serialize out, caller should free memory */
2497 *lpcbytes
= 0; /* Assume failure */
2499 if (wformat
== CF_METAFILEPICT
)
2501 LPMETAFILEPICT lpmfp
= GlobalLock(hdata
);
2502 unsigned int size
= GetMetaFileBitsEx(lpmfp
->hMF
, 0, NULL
);
2504 h
= GlobalAlloc(0, size
+ sizeof(METAFILEPICT
));
2507 char *pdata
= GlobalLock(h
);
2509 memcpy(pdata
, lpmfp
, sizeof(METAFILEPICT
));
2510 GetMetaFileBitsEx(lpmfp
->hMF
, size
, pdata
+ sizeof(METAFILEPICT
));
2512 *lpcbytes
= size
+ sizeof(METAFILEPICT
);
2517 GlobalUnlock(hdata
);
2519 else if (wformat
== CF_ENHMETAFILE
)
2521 int size
= GetEnhMetaFileBits(hdata
, 0, NULL
);
2523 h
= GlobalAlloc(0, size
);
2526 LPVOID pdata
= GlobalLock(h
);
2528 GetEnhMetaFileBits(hdata
, size
, pdata
);
2537 if (wformat
== CF_METAFILEPICT
)
2539 h
= GlobalAlloc(0, sizeof(METAFILEPICT
));
2542 unsigned int wiresize
;
2543 LPMETAFILEPICT lpmfp
= GlobalLock(h
);
2545 memcpy(lpmfp
, hdata
, sizeof(METAFILEPICT
));
2546 wiresize
= *lpcbytes
- sizeof(METAFILEPICT
);
2547 lpmfp
->hMF
= SetMetaFileBitsEx(wiresize
,
2548 ((const BYTE
*)hdata
) + sizeof(METAFILEPICT
));
2552 else if (wformat
== CF_ENHMETAFILE
)
2554 h
= SetEnhMetaFileBits(*lpcbytes
, hdata
);
2562 /**************************************************************************
2563 * X11DRV_CLIPBOARD_ReleaseSelection
2565 * Release XA_CLIPBOARD and XA_PRIMARY in response to a SelectionClear event.
2567 static void X11DRV_CLIPBOARD_ReleaseSelection(Display
*display
, Atom selType
, Window w
, HWND hwnd
, Time time
)
2569 /* w is the window that lost the selection
2571 TRACE("event->window = %08x (selectionWindow = %08x) selectionAcquired=0x%08x\n",
2572 (unsigned)w
, (unsigned)selectionWindow
, (unsigned)selectionAcquired
);
2574 if (selectionAcquired
&& (w
== selectionWindow
))
2576 CLIPBOARDINFO cbinfo
;
2578 /* completely give up the selection */
2579 TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
2581 X11DRV_CLIPBOARD_GetClipboardInfo(&cbinfo
);
2583 if (cbinfo
.flags
& CB_PROCESS
)
2585 /* Since we're still the owner, this wasn't initiated by
2586 another Wine process */
2587 if (OpenClipboard(hwnd
))
2589 /* Destroy private objects */
2590 SendMessageW(cbinfo
.hWndOwner
, WM_DESTROYCLIPBOARD
, 0, 0);
2592 /* Give up ownership of the windows clipboard */
2593 X11DRV_CLIPBOARD_ReleaseOwnership();
2598 if ((selType
== x11drv_atom(CLIPBOARD
)) && (selectionAcquired
& S_PRIMARY
))
2600 TRACE("Lost clipboard. Check if we need to release PRIMARY\n");
2602 if (selectionWindow
== XGetSelectionOwner(display
, XA_PRIMARY
))
2604 TRACE("We still own PRIMARY. Releasing PRIMARY.\n");
2605 XSetSelectionOwner(display
, XA_PRIMARY
, None
, time
);
2608 TRACE("We no longer own PRIMARY\n");
2610 else if ((selType
== XA_PRIMARY
) && (selectionAcquired
& S_CLIPBOARD
))
2612 TRACE("Lost PRIMARY. Check if we need to release CLIPBOARD\n");
2614 if (selectionWindow
== XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)))
2616 TRACE("We still own CLIPBOARD. Releasing CLIPBOARD.\n");
2617 XSetSelectionOwner(display
, x11drv_atom(CLIPBOARD
), None
, time
);
2620 TRACE("We no longer own CLIPBOARD\n");
2623 selectionWindow
= None
;
2625 X11DRV_EmptyClipboard(FALSE
);
2627 /* Reset the selection flags now that we are done */
2628 selectionAcquired
= S_NOSELECTION
;
2633 /**************************************************************************
2634 * IsSelectionOwner (X11DRV.@)
2636 * Returns: TRUE if the selection is owned by this process, FALSE otherwise
2638 static BOOL
X11DRV_CLIPBOARD_IsSelectionOwner(void)
2640 return selectionAcquired
;
2644 /**************************************************************************
2645 * X11DRV Clipboard Exports
2646 **************************************************************************/
2649 static void selection_acquire(void)
2654 owner
= thread_selection_wnd();
2655 display
= thread_display();
2657 selectionAcquired
= 0;
2658 selectionWindow
= 0;
2660 /* Grab PRIMARY selection if not owned */
2661 if (use_primary_selection
)
2662 XSetSelectionOwner(display
, XA_PRIMARY
, owner
, CurrentTime
);
2664 /* Grab CLIPBOARD selection if not owned */
2665 XSetSelectionOwner(display
, x11drv_atom(CLIPBOARD
), owner
, CurrentTime
);
2667 if (use_primary_selection
&& XGetSelectionOwner(display
, XA_PRIMARY
) == owner
)
2668 selectionAcquired
|= S_PRIMARY
;
2670 if (XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)) == owner
)
2671 selectionAcquired
|= S_CLIPBOARD
;
2673 if (selectionAcquired
)
2675 selectionWindow
= owner
;
2676 TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner
);
2680 static DWORD WINAPI
selection_thread_proc(LPVOID p
)
2686 selection_acquire();
2689 while (selectionAcquired
)
2691 MsgWaitForMultipleObjectsEx(0, NULL
, INFINITE
, QS_SENDMESSAGE
, 0);
2697 /**************************************************************************
2698 * AcquireClipboard (X11DRV.@)
2700 int CDECL
X11DRV_AcquireClipboard(HWND hWndClipWindow
)
2703 HANDLE selectionThread
;
2705 TRACE(" %p\n", hWndClipWindow
);
2708 * It's important that the selection get acquired from the thread
2709 * that owns the clipboard window. The primary reason is that we know
2710 * it is running a message loop and therefore can process the
2711 * X selection events.
2713 if (hWndClipWindow
&&
2714 GetCurrentThreadId() != GetWindowThreadProcessId(hWndClipWindow
, &procid
))
2716 if (procid
!= GetCurrentProcessId())
2718 WARN("Setting clipboard owner to other process is not supported\n");
2719 hWndClipWindow
= NULL
;
2723 TRACE("Thread %x is acquiring selection with thread %x's window %p\n",
2724 GetCurrentThreadId(),
2725 GetWindowThreadProcessId(hWndClipWindow
, NULL
), hWndClipWindow
);
2727 return SendMessageW(hWndClipWindow
, WM_X11DRV_ACQUIRE_SELECTION
, 0, 0);
2733 selection_acquire();
2737 HANDLE event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2738 selectionThread
= CreateThread(NULL
, 0, selection_thread_proc
, event
, 0, NULL
);
2740 if (!selectionThread
)
2742 WARN("Could not start clipboard thread\n");
2747 WaitForSingleObject(event
, INFINITE
);
2749 CloseHandle(selectionThread
);
2756 /**************************************************************************
2757 * X11DRV_EmptyClipboard
2759 * Empty cached clipboard data.
2761 void CDECL
X11DRV_EmptyClipboard(BOOL keepunowned
)
2763 WINE_CLIPDATA
*data
, *next
;
2765 LIST_FOR_EACH_ENTRY_SAFE( data
, next
, &data_list
, WINE_CLIPDATA
, entry
)
2767 if (keepunowned
&& (data
->wFlags
& CF_FLAG_UNOWNED
)) continue;
2768 list_remove( &data
->entry
);
2769 X11DRV_CLIPBOARD_FreeData( data
);
2770 HeapFree( GetProcessHeap(), 0, data
);
2774 TRACE(" %d entries remaining in cache.\n", ClipDataCount
);
2779 /**************************************************************************
2780 * X11DRV_SetClipboardData
2782 BOOL CDECL
X11DRV_SetClipboardData(UINT wFormat
, HANDLE hData
, BOOL owner
)
2785 BOOL bResult
= TRUE
;
2787 /* If it's not owned, data can only be set if the format data is not already owned
2788 and its rendering is not delayed */
2791 CLIPBOARDINFO cbinfo
;
2792 LPWINE_CLIPDATA lpRender
;
2794 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2797 ((lpRender
= X11DRV_CLIPBOARD_LookupData(wFormat
)) &&
2798 !(lpRender
->wFlags
& CF_FLAG_UNOWNED
)))
2801 flags
= CF_FLAG_UNOWNED
;
2804 bResult
&= X11DRV_CLIPBOARD_InsertClipboardData(wFormat
, hData
, flags
, NULL
, TRUE
);
2810 /**************************************************************************
2811 * CountClipboardFormats
2813 INT CDECL
X11DRV_CountClipboardFormats(void)
2815 CLIPBOARDINFO cbinfo
;
2817 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2819 TRACE(" count=%d\n", ClipDataCount
);
2821 return ClipDataCount
;
2825 /**************************************************************************
2826 * X11DRV_EnumClipboardFormats
2828 UINT CDECL
X11DRV_EnumClipboardFormats(UINT wFormat
)
2830 CLIPBOARDINFO cbinfo
;
2831 struct list
*ptr
= NULL
;
2833 TRACE("(%04X)\n", wFormat
);
2835 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2839 ptr
= list_head( &data_list
);
2843 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(wFormat
);
2844 if (lpData
) ptr
= list_next( &data_list
, &lpData
->entry
);
2848 return LIST_ENTRY( ptr
, WINE_CLIPDATA
, entry
)->wFormatID
;
2852 /**************************************************************************
2853 * X11DRV_IsClipboardFormatAvailable
2855 BOOL CDECL
X11DRV_IsClipboardFormatAvailable(UINT wFormat
)
2858 CLIPBOARDINFO cbinfo
;
2860 TRACE("(%04X)\n", wFormat
);
2862 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2864 if (wFormat
!= 0 && X11DRV_CLIPBOARD_LookupData(wFormat
))
2867 TRACE("(%04X)- ret(%d)\n", wFormat
, bRet
);
2873 /**************************************************************************
2874 * GetClipboardData (USER.142)
2876 HANDLE CDECL
X11DRV_GetClipboardData(UINT wFormat
)
2878 CLIPBOARDINFO cbinfo
;
2879 LPWINE_CLIPDATA lpRender
;
2881 TRACE("(%04X)\n", wFormat
);
2883 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2885 if ((lpRender
= X11DRV_CLIPBOARD_LookupData(wFormat
)))
2887 if ( !lpRender
->hData
)
2888 X11DRV_CLIPBOARD_RenderFormat(thread_init_display(), lpRender
);
2890 TRACE(" returning %p (type %04x)\n", lpRender
->hData
, lpRender
->wFormatID
);
2891 return lpRender
->hData
;
2898 /**************************************************************************
2899 * ResetSelectionOwner
2901 * Called when the thread owning the selection is destroyed and we need to
2902 * preserve the selection ownership. We look for another top level window
2903 * in this process and send it a message to acquire the selection.
2905 void X11DRV_ResetSelectionOwner(void)
2912 if (!selectionAcquired
|| thread_selection_wnd() != selectionWindow
)
2915 selectionAcquired
= S_NOSELECTION
;
2916 selectionWindow
= 0;
2918 hwnd
= GetWindow(GetDesktopWindow(), GW_CHILD
);
2921 if (GetCurrentThreadId() != GetWindowThreadProcessId(hwnd
, &procid
))
2923 if (GetCurrentProcessId() == procid
)
2925 if (SendMessageW(hwnd
, WM_X11DRV_ACQUIRE_SELECTION
, 0, 0))
2929 } while ((hwnd
= GetWindow(hwnd
, GW_HWNDNEXT
)) != NULL
);
2931 WARN("Failed to find another thread to take selection ownership. Clipboard data will be lost.\n");
2933 X11DRV_CLIPBOARD_ReleaseOwnership();
2934 X11DRV_EmptyClipboard(FALSE
);
2938 /**************************************************************************
2939 * X11DRV_CLIPBOARD_SynthesizeData
2941 static BOOL
X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID
)
2944 LPWINE_CLIPDATA lpSource
= NULL
;
2946 TRACE(" %04x\n", wFormatID
);
2948 /* Don't need to synthesize if it already exists */
2949 if (X11DRV_CLIPBOARD_LookupData(wFormatID
))
2952 if (wFormatID
== CF_UNICODETEXT
|| wFormatID
== CF_TEXT
|| wFormatID
== CF_OEMTEXT
)
2954 bsyn
= ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT
)) &&
2955 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) ||
2956 ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_TEXT
)) &&
2957 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) ||
2958 ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT
)) &&
2959 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
);
2961 else if (wFormatID
== CF_ENHMETAFILE
)
2963 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT
)) &&
2964 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2966 else if (wFormatID
== CF_METAFILEPICT
)
2968 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_ENHMETAFILE
)) &&
2969 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2971 else if (wFormatID
== CF_DIB
)
2973 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) &&
2974 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2976 else if (wFormatID
== CF_BITMAP
)
2978 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) &&
2979 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2983 X11DRV_CLIPBOARD_InsertClipboardData(wFormatID
, 0, CF_FLAG_SYNTHESIZED
, NULL
, TRUE
);
2990 /**************************************************************************
2991 * X11DRV_EndClipboardUpdate
2993 * Add locale if it hasn't already been added
2995 void CDECL
X11DRV_EndClipboardUpdate(void)
2997 INT count
= ClipDataCount
;
2999 /* Do Unicode <-> Text <-> OEM mapping */
3000 X11DRV_CLIPBOARD_SynthesizeData(CF_TEXT
);
3001 X11DRV_CLIPBOARD_SynthesizeData(CF_OEMTEXT
);
3002 X11DRV_CLIPBOARD_SynthesizeData(CF_UNICODETEXT
);
3004 /* Enhmetafile <-> MetafilePict mapping */
3005 X11DRV_CLIPBOARD_SynthesizeData(CF_ENHMETAFILE
);
3006 X11DRV_CLIPBOARD_SynthesizeData(CF_METAFILEPICT
);
3008 /* DIB <-> Bitmap mapping */
3009 X11DRV_CLIPBOARD_SynthesizeData(CF_DIB
);
3010 X11DRV_CLIPBOARD_SynthesizeData(CF_BITMAP
);
3012 TRACE("%d formats added to cached data\n", ClipDataCount
- count
);
3016 /***********************************************************************
3017 * X11DRV_SelectionRequest_TARGETS
3018 * Service a TARGETS selection request event
3020 static Atom
X11DRV_SelectionRequest_TARGETS( Display
*display
, Window requestor
,
3021 Atom target
, Atom rprop
)
3026 LPWINE_CLIPFORMAT format
;
3027 LPWINE_CLIPDATA lpData
;
3029 /* Create X atoms for any clipboard types which don't have atoms yet.
3030 * This avoids sending bogus zero atoms.
3031 * Without this, copying might not have access to all clipboard types.
3032 * FIXME: is it safe to call this here?
3037 * Count the number of items we wish to expose as selection targets.
3039 cTargets
= 1; /* Include TARGETS */
3041 if (!list_head( &data_list
)) return None
;
3043 LIST_FOR_EACH_ENTRY( lpData
, &data_list
, WINE_CLIPDATA
, entry
)
3044 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
3045 if ((format
->wFormatID
== lpData
->wFormatID
) &&
3046 format
->lpDrvExportFunc
&& format
->drvData
)
3049 TRACE(" found %d formats\n", cTargets
);
3051 /* Allocate temp buffer */
3052 targets
= HeapAlloc( GetProcessHeap(), 0, cTargets
* sizeof(Atom
));
3057 targets
[i
++] = x11drv_atom(TARGETS
);
3059 LIST_FOR_EACH_ENTRY( lpData
, &data_list
, WINE_CLIPDATA
, entry
)
3060 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
3061 if ((format
->wFormatID
== lpData
->wFormatID
) &&
3062 format
->lpDrvExportFunc
&& format
->drvData
)
3063 targets
[i
++] = format
->drvData
;
3065 if (TRACE_ON(clipboard
))
3068 for ( i
= 0; i
< cTargets
; i
++)
3070 char *itemFmtName
= XGetAtomName(display
, targets
[i
]);
3071 TRACE("\tAtom# %d: Property %ld Type %s\n", i
, targets
[i
], itemFmtName
);
3076 /* We may want to consider setting the type to xaTargets instead,
3077 * in case some apps expect this instead of XA_ATOM */
3078 XChangeProperty(display
, requestor
, rprop
, XA_ATOM
, 32,
3079 PropModeReplace
, (unsigned char *)targets
, cTargets
);
3081 HeapFree(GetProcessHeap(), 0, targets
);
3087 /***********************************************************************
3088 * X11DRV_SelectionRequest_MULTIPLE
3089 * Service a MULTIPLE selection request event
3090 * rprop contains a list of (target,property) atom pairs.
3091 * The first atom names a target and the second names a property.
3092 * The effect is as if we have received a sequence of SelectionRequest events
3093 * (one for each atom pair) except that:
3094 * 1. We reply with a SelectionNotify only when all the requested conversions
3095 * have been performed.
3096 * 2. If we fail to convert the target named by an atom in the MULTIPLE property,
3097 * we replace the atom in the property by None.
3099 static Atom
X11DRV_SelectionRequest_MULTIPLE( HWND hWnd
, XSelectionRequestEvent
*pevent
)
3101 Display
*display
= pevent
->display
;
3103 Atom atype
=AnyPropertyType
;
3105 unsigned long remain
;
3106 Atom
* targetPropList
=NULL
;
3107 unsigned long cTargetPropList
= 0;
3109 /* If the specified property is None the requestor is an obsolete client.
3110 * We support these by using the specified target atom as the reply property.
3112 rprop
= pevent
->property
;
3114 rprop
= pevent
->target
;
3118 /* Read the MULTIPLE property contents. This should contain a list of
3119 * (target,property) atom pairs.
3121 if (!XGetWindowProperty(display
, pevent
->requestor
, rprop
,
3122 0, 0x3FFF, False
, AnyPropertyType
, &atype
,&aformat
,
3123 &cTargetPropList
, &remain
,
3124 (unsigned char**)&targetPropList
) != Success
)
3126 if (TRACE_ON(clipboard
))
3128 char * const typeName
= XGetAtomName(display
, atype
);
3129 TRACE("\tType %s,Format %d,nItems %ld, Remain %ld\n",
3130 typeName
, aformat
, cTargetPropList
, remain
);
3135 * Make sure we got what we expect.
3136 * NOTE: According to the X-ICCCM Version 2.0 documentation the property sent
3137 * in a MULTIPLE selection request should be of type ATOM_PAIR.
3138 * However some X apps(such as XPaint) are not compliant with this and return
3139 * a user defined atom in atype when XGetWindowProperty is called.
3140 * The data *is* an atom pair but is not denoted as such.
3142 if(aformat
== 32 /* atype == xAtomPair */ )
3146 /* Iterate through the ATOM_PAIR list and execute a SelectionRequest
3147 * for each (target,property) pair */
3149 for (i
= 0; i
< cTargetPropList
; i
+=2)
3151 XSelectionRequestEvent event
;
3153 if (TRACE_ON(clipboard
))
3155 char *targetName
, *propName
;
3156 targetName
= XGetAtomName(display
, targetPropList
[i
]);
3157 propName
= XGetAtomName(display
, targetPropList
[i
+1]);
3158 TRACE("MULTIPLE(%d): Target='%s' Prop='%s'\n",
3159 i
/2, targetName
, propName
);
3164 /* We must have a non "None" property to service a MULTIPLE target atom */
3165 if ( !targetPropList
[i
+1] )
3167 TRACE("\tMULTIPLE(%d): Skipping target with empty property!\n", i
);
3171 /* Set up an XSelectionRequestEvent for this (target,property) pair */
3173 event
.target
= targetPropList
[i
];
3174 event
.property
= targetPropList
[i
+1];
3176 /* Fire a SelectionRequest, informing the handler that we are processing
3177 * a MULTIPLE selection request event.
3179 X11DRV_HandleSelectionRequest( hWnd
, &event
, TRUE
);
3183 /* Free the list of targets/properties */
3184 XFree(targetPropList
);
3186 else TRACE("Couldn't read MULTIPLE property\n");
3192 /***********************************************************************
3193 * X11DRV_HandleSelectionRequest
3194 * Process an event selection request event.
3195 * The bIsMultiple flag is used to signal when EVENT_SelectionRequest is called
3196 * recursively while servicing a "MULTIPLE" selection target.
3198 * Note: We only receive this event when WINE owns the X selection
3200 static void X11DRV_HandleSelectionRequest( HWND hWnd
, XSelectionRequestEvent
*event
, BOOL bIsMultiple
)
3202 Display
*display
= event
->display
;
3203 XSelectionEvent result
;
3205 Window request
= event
->requestor
;
3210 * We can only handle the selection request if :
3211 * The selection is PRIMARY or CLIPBOARD, AND we can successfully open the clipboard.
3212 * Don't do these checks or open the clipboard while recursively processing MULTIPLE,
3213 * since this has been already done.
3217 if (((event
->selection
!= XA_PRIMARY
) && (event
->selection
!= x11drv_atom(CLIPBOARD
))))
3221 /* If the specified property is None the requestor is an obsolete client.
3222 * We support these by using the specified target atom as the reply property.
3224 rprop
= event
->property
;
3226 rprop
= event
->target
;
3228 if(event
->target
== x11drv_atom(TARGETS
)) /* Return a list of all supported targets */
3230 /* TARGETS selection request */
3231 rprop
= X11DRV_SelectionRequest_TARGETS( display
, request
, event
->target
, rprop
);
3233 else if(event
->target
== x11drv_atom(MULTIPLE
)) /* rprop contains a list of (target, property) atom pairs */
3235 /* MULTIPLE selection request */
3236 rprop
= X11DRV_SelectionRequest_MULTIPLE( hWnd
, event
);
3240 LPWINE_CLIPFORMAT lpFormat
= X11DRV_CLIPBOARD_LookupProperty(NULL
, event
->target
);
3242 if (lpFormat
&& lpFormat
->lpDrvExportFunc
)
3244 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(lpFormat
->wFormatID
);
3248 unsigned char* lpClipData
;
3250 HANDLE hClipData
= lpFormat
->lpDrvExportFunc(display
, request
, event
->target
,
3251 rprop
, lpData
, &cBytes
);
3253 if (hClipData
&& (lpClipData
= GlobalLock(hClipData
)))
3255 int mode
= PropModeReplace
;
3257 TRACE("\tUpdating property %s, %d bytes\n",
3258 debugstr_format(lpFormat
->wFormatID
), cBytes
);
3261 int nelements
= min(cBytes
, 65536);
3262 XChangeProperty(display
, request
, rprop
, event
->target
,
3263 8, mode
, lpClipData
, nelements
);
3264 mode
= PropModeAppend
;
3265 cBytes
-= nelements
;
3266 lpClipData
+= nelements
;
3267 } while (cBytes
> 0);
3269 GlobalUnlock(hClipData
);
3270 GlobalFree(hClipData
);
3278 * SelectionNotify should be sent only at the end of a MULTIPLE request
3282 result
.type
= SelectionNotify
;
3283 result
.display
= display
;
3284 result
.requestor
= request
;
3285 result
.selection
= event
->selection
;
3286 result
.property
= rprop
;
3287 result
.target
= event
->target
;
3288 result
.time
= event
->time
;
3289 TRACE("Sending SelectionNotify event...\n");
3290 XSendEvent(display
,event
->requestor
,False
,NoEventMask
,(XEvent
*)&result
);
3295 /***********************************************************************
3296 * X11DRV_SelectionRequest
3298 void X11DRV_SelectionRequest( HWND hWnd
, XEvent
*event
)
3300 X11DRV_HandleSelectionRequest( hWnd
, &event
->xselectionrequest
, FALSE
);
3304 /***********************************************************************
3305 * X11DRV_SelectionClear
3307 void X11DRV_SelectionClear( HWND hWnd
, XEvent
*xev
)
3309 XSelectionClearEvent
*event
= &xev
->xselectionclear
;
3310 if (event
->selection
== XA_PRIMARY
|| event
->selection
== x11drv_atom(CLIPBOARD
))
3311 X11DRV_CLIPBOARD_ReleaseSelection( event
->display
, event
->selection
,
3312 event
->window
, hWnd
, event
->time
);