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 XSetWindowAttributes attr
;
264 attr
.event_mask
= (ExposureMask
| KeyPressMask
| KeyReleaseMask
| PointerMotionMask
|
265 ButtonPressMask
| ButtonReleaseMask
| EnterWindowMask
| PropertyChangeMask
);
268 w
= XCreateWindow(thread_data
->display
, root_window
, 0, 0, 1, 1, 0, screen_depth
,
269 InputOutput
, CopyFromParent
, CWEventMask
, &attr
);
273 thread_data
->selection_wnd
= w
;
275 FIXME("Failed to create window. Fetching selection data will fail.\n");
281 static const char *debugstr_format( UINT id
)
285 if (GetClipboardFormatNameW( id
, buffer
, 256 ))
286 return wine_dbg_sprintf( "%04x %s", id
, debugstr_w(buffer
) );
290 #define BUILTIN(id) case id: return #id;
293 BUILTIN(CF_METAFILEPICT
)
303 BUILTIN(CF_UNICODETEXT
)
304 BUILTIN(CF_ENHMETAFILE
)
308 BUILTIN(CF_OWNERDISPLAY
)
310 BUILTIN(CF_DSPBITMAP
)
311 BUILTIN(CF_DSPMETAFILEPICT
)
312 BUILTIN(CF_DSPENHMETAFILE
)
314 default: return wine_dbg_sprintf( "%04x", id
);
318 /**************************************************************************
319 * X11DRV_InitClipboard
321 void X11DRV_InitClipboard(void)
324 WINE_CLIPFORMAT
*format
;
326 /* Register built-in formats */
327 for (i
= 0; i
< sizeof(builtin_formats
)/sizeof(builtin_formats
[0]); i
++)
329 if (!(format
= HeapAlloc( GetProcessHeap(), 0, sizeof(*format
)))) break;
330 format
->wFormatID
= builtin_formats
[i
].id
;
331 format
->drvData
= GET_ATOM(builtin_formats
[i
].data
);
332 format
->lpDrvImportFunc
= builtin_formats
[i
].import
;
333 format
->lpDrvExportFunc
= builtin_formats
[i
].export
;
334 list_add_tail( &format_list
, &format
->entry
);
337 /* Register known mapping between window formats and X properties */
338 for (i
= 0; i
< sizeof(PropertyFormatMap
)/sizeof(PropertyFormatMap
[0]); i
++)
339 X11DRV_CLIPBOARD_InsertClipboardFormat( RegisterClipboardFormatW(PropertyFormatMap
[i
].lpszFormat
),
340 GET_ATOM(PropertyFormatMap
[i
].prop
));
342 /* Set up a conversion function from "HTML Format" to "text/html" */
343 format
= X11DRV_CLIPBOARD_InsertClipboardFormat( RegisterClipboardFormatW(wszHTMLFormat
),
344 GET_ATOM(XATOM_text_html
));
345 format
->lpDrvExportFunc
= X11DRV_CLIPBOARD_ExportTextHtml
;
349 /**************************************************************************
352 * Intern atoms for formats that don't have one yet.
354 static void intern_atoms(void)
356 LPWINE_CLIPFORMAT format
;
364 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
365 if (!format
->drvData
) count
++;
368 display
= thread_init_display();
370 names
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*names
) );
371 atoms
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*atoms
) );
374 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
375 if (!format
->drvData
) {
376 GetClipboardFormatNameW( format
->wFormatID
, buffer
, 256 );
377 len
= WideCharToMultiByte(CP_UNIXCP
, 0, buffer
, -1, NULL
, 0, NULL
, NULL
);
378 names
[i
] = HeapAlloc(GetProcessHeap(), 0, len
);
379 WideCharToMultiByte(CP_UNIXCP
, 0, buffer
, -1, names
[i
++], len
, NULL
, NULL
);
383 XInternAtoms( display
, names
, count
, False
, atoms
);
387 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
388 if (!format
->drvData
) {
389 HeapFree(GetProcessHeap(), 0, names
[i
]);
390 format
->drvData
= atoms
[i
++];
393 HeapFree( GetProcessHeap(), 0, names
);
394 HeapFree( GetProcessHeap(), 0, atoms
);
398 /**************************************************************************
401 * Register a custom X clipboard format.
403 static WINE_CLIPFORMAT
*register_format( UINT id
, Atom prop
)
405 LPWINE_CLIPFORMAT lpFormat
;
407 /* walk format chain to see if it's already registered */
408 LIST_FOR_EACH_ENTRY( lpFormat
, &format_list
, WINE_CLIPFORMAT
, entry
)
409 if (lpFormat
->wFormatID
== id
) return lpFormat
;
411 return X11DRV_CLIPBOARD_InsertClipboardFormat(id
, prop
);
415 /**************************************************************************
416 * X11DRV_CLIPBOARD_LookupProperty
418 static LPWINE_CLIPFORMAT
X11DRV_CLIPBOARD_LookupProperty(LPWINE_CLIPFORMAT current
, UINT drvData
)
422 struct list
*ptr
= current
? ¤t
->entry
: &format_list
;
423 BOOL need_intern
= FALSE
;
425 while ((ptr
= list_next( &format_list
, ptr
)))
427 LPWINE_CLIPFORMAT lpFormat
= LIST_ENTRY( ptr
, WINE_CLIPFORMAT
, entry
);
428 if (lpFormat
->drvData
== drvData
) return lpFormat
;
429 if (!lpFormat
->drvData
) need_intern
= TRUE
;
431 if (!need_intern
) return NULL
;
433 /* restart the search for the new atoms */
438 /**************************************************************************
439 * X11DRV_CLIPBOARD_LookupData
441 static LPWINE_CLIPDATA
X11DRV_CLIPBOARD_LookupData(DWORD wID
)
445 LIST_FOR_EACH_ENTRY( data
, &data_list
, WINE_CLIPDATA
, entry
)
446 if (data
->wFormatID
== wID
) return data
;
452 /**************************************************************************
453 * InsertClipboardFormat
455 static WINE_CLIPFORMAT
*X11DRV_CLIPBOARD_InsertClipboardFormat( UINT id
, Atom prop
)
457 LPWINE_CLIPFORMAT lpNewFormat
;
459 /* allocate storage for new format entry */
460 lpNewFormat
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPFORMAT
));
462 if(lpNewFormat
== NULL
)
464 WARN("No more memory for a new format!\n");
467 lpNewFormat
->wFormatID
= id
;
468 lpNewFormat
->drvData
= prop
;
469 lpNewFormat
->lpDrvImportFunc
= X11DRV_CLIPBOARD_ImportClipboardData
;
470 lpNewFormat
->lpDrvExportFunc
= X11DRV_CLIPBOARD_ExportClipboardData
;
472 list_add_tail( &format_list
, &lpNewFormat
->entry
);
474 TRACE("Registering format %s drvData %d\n",
475 debugstr_format(lpNewFormat
->wFormatID
), lpNewFormat
->drvData
);
483 /**************************************************************************
484 * X11DRV_CLIPBOARD_GetClipboardInfo
486 static BOOL
X11DRV_CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo
)
490 SERVER_START_REQ( set_clipboard_info
)
494 if (wine_server_call_err( req
))
496 ERR("Failed to get clipboard owner.\n");
500 cbInfo
->hWndOpen
= wine_server_ptr_handle( reply
->old_clipboard
);
501 cbInfo
->hWndOwner
= wine_server_ptr_handle( reply
->old_owner
);
502 cbInfo
->hWndViewer
= wine_server_ptr_handle( reply
->old_viewer
);
503 cbInfo
->seqno
= reply
->seqno
;
504 cbInfo
->flags
= reply
->flags
;
515 /**************************************************************************
516 * X11DRV_CLIPBOARD_ReleaseOwnership
518 static BOOL
X11DRV_CLIPBOARD_ReleaseOwnership(void)
522 SERVER_START_REQ( set_clipboard_info
)
524 req
->flags
= SET_CB_RELOWNER
| SET_CB_SEQNO
;
526 if (wine_server_call_err( req
))
528 ERR("Failed to set clipboard.\n");
542 /**************************************************************************
543 * X11DRV_CLIPBOARD_InsertClipboardData
545 * Caller *must* have the clipboard open and be the owner.
547 static BOOL
X11DRV_CLIPBOARD_InsertClipboardData(UINT wFormatID
, HANDLE hData
, DWORD flags
,
548 LPWINE_CLIPFORMAT lpFormat
, BOOL override
)
550 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(wFormatID
);
552 TRACE("format=%04x lpData=%p hData=%p flags=0x%08x lpFormat=%p override=%d\n",
553 wFormatID
, lpData
, hData
, flags
, lpFormat
, override
);
555 /* make sure the format exists */
556 if (!lpFormat
) register_format( wFormatID
, 0 );
558 if (lpData
&& !override
)
563 X11DRV_CLIPBOARD_FreeData(lpData
);
565 lpData
->hData
= hData
;
569 lpData
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPDATA
));
571 lpData
->wFormatID
= wFormatID
;
572 lpData
->hData
= hData
;
573 lpData
->lpFormat
= lpFormat
;
576 list_add_tail( &data_list
, &lpData
->entry
);
580 lpData
->wFlags
= flags
;
586 /**************************************************************************
587 * X11DRV_CLIPBOARD_FreeData
589 * Free clipboard data handle.
591 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData
)
593 TRACE("%04x\n", lpData
->wFormatID
);
595 if ((lpData
->wFormatID
>= CF_GDIOBJFIRST
&&
596 lpData
->wFormatID
<= CF_GDIOBJLAST
) ||
597 lpData
->wFormatID
== CF_BITMAP
||
598 lpData
->wFormatID
== CF_DIB
||
599 lpData
->wFormatID
== CF_PALETTE
)
602 DeleteObject(lpData
->hData
);
604 if ((lpData
->wFormatID
== CF_DIB
) && lpData
->drvData
)
605 XFreePixmap(gdi_display
, lpData
->drvData
);
607 else if (lpData
->wFormatID
== CF_METAFILEPICT
)
611 DeleteMetaFile(((METAFILEPICT
*)GlobalLock( lpData
->hData
))->hMF
);
612 GlobalFree(lpData
->hData
);
615 else if (lpData
->wFormatID
== CF_ENHMETAFILE
)
618 DeleteEnhMetaFile(lpData
->hData
);
620 else if (lpData
->wFormatID
< CF_PRIVATEFIRST
||
621 lpData
->wFormatID
> CF_PRIVATELAST
)
624 GlobalFree(lpData
->hData
);
632 /**************************************************************************
633 * X11DRV_CLIPBOARD_UpdateCache
635 static BOOL
X11DRV_CLIPBOARD_UpdateCache(LPCLIPBOARDINFO lpcbinfo
)
639 if (!X11DRV_CLIPBOARD_IsSelectionOwner())
641 if (!X11DRV_CLIPBOARD_GetClipboardInfo(lpcbinfo
))
643 ERR("Failed to retrieve clipboard information.\n");
646 else if (wSeqNo
< lpcbinfo
->seqno
)
648 X11DRV_EmptyClipboard(TRUE
);
650 if (X11DRV_CLIPBOARD_QueryAvailableData(thread_init_display(), lpcbinfo
) < 0)
652 ERR("Failed to cache clipboard data owned by another process.\n");
657 X11DRV_EndClipboardUpdate();
660 wSeqNo
= lpcbinfo
->seqno
;
668 /**************************************************************************
669 * X11DRV_CLIPBOARD_RenderFormat
671 static BOOL
X11DRV_CLIPBOARD_RenderFormat(Display
*display
, LPWINE_CLIPDATA lpData
)
675 TRACE(" 0x%04x hData(%p)\n", lpData
->wFormatID
, lpData
->hData
);
677 if (lpData
->hData
) return bret
; /* Already rendered */
679 if (lpData
->wFlags
& CF_FLAG_SYNTHESIZED
)
680 bret
= X11DRV_CLIPBOARD_RenderSynthesizedFormat(display
, lpData
);
681 else if (!X11DRV_CLIPBOARD_IsSelectionOwner())
683 if (!X11DRV_CLIPBOARD_ReadSelectionData(display
, lpData
))
685 ERR("Failed to cache clipboard data owned by another process. Format=%04x\n",
692 CLIPBOARDINFO cbInfo
;
694 if (X11DRV_CLIPBOARD_GetClipboardInfo(&cbInfo
) && cbInfo
.hWndOwner
)
696 /* Send a WM_RENDERFORMAT message to notify the owner to render the
697 * data requested into the clipboard.
699 TRACE("Sending WM_RENDERFORMAT message to hwnd(%p)\n", cbInfo
.hWndOwner
);
700 SendMessageW(cbInfo
.hWndOwner
, WM_RENDERFORMAT
, lpData
->wFormatID
, 0);
702 if (!lpData
->hData
) bret
= FALSE
;
706 ERR("hWndClipOwner is lost!\n");
715 /**************************************************************************
716 * CLIPBOARD_ConvertText
717 * Returns number of required/converted characters - not bytes!
719 static INT
CLIPBOARD_ConvertText(WORD src_fmt
, void const *src
, INT src_size
,
720 WORD dst_fmt
, void *dst
, INT dst_size
)
724 if(src_fmt
== CF_UNICODETEXT
)
737 return WideCharToMultiByte(cp
, 0, src
, src_size
, dst
, dst_size
, NULL
, NULL
);
740 if(dst_fmt
== CF_UNICODETEXT
)
753 return MultiByteToWideChar(cp
, 0, src
, src_size
, dst
, dst_size
);
756 if(!dst_size
) return src_size
;
758 if(dst_size
> src_size
) dst_size
= src_size
;
760 if(src_fmt
== CF_TEXT
)
761 CharToOemBuffA(src
, dst
, dst_size
);
763 OemToCharBuffA(src
, dst
, dst_size
);
769 /**************************************************************************
770 * X11DRV_CLIPBOARD_RenderSynthesizedFormat
772 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedFormat(Display
*display
, LPWINE_CLIPDATA lpData
)
778 if (lpData
->wFlags
& CF_FLAG_SYNTHESIZED
)
780 UINT wFormatID
= lpData
->wFormatID
;
782 if (wFormatID
== CF_UNICODETEXT
|| wFormatID
== CF_TEXT
|| wFormatID
== CF_OEMTEXT
)
783 bret
= X11DRV_CLIPBOARD_RenderSynthesizedText(display
, wFormatID
);
789 bret
= X11DRV_CLIPBOARD_RenderSynthesizedDIB( display
);
793 bret
= X11DRV_CLIPBOARD_RenderSynthesizedBitmap( display
);
797 bret
= X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile( display
);
800 case CF_METAFILEPICT
:
801 FIXME("Synthesizing CF_METAFILEPICT not implemented\n");
805 FIXME("Called to synthesize unknown format 0x%08x\n", wFormatID
);
810 lpData
->wFlags
&= ~CF_FLAG_SYNTHESIZED
;
817 /**************************************************************************
818 * X11DRV_CLIPBOARD_RenderSynthesizedText
820 * Renders synthesized text
822 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedText(Display
*display
, UINT wFormatID
)
827 INT src_chars
, dst_chars
, alloc_size
;
828 LPWINE_CLIPDATA lpSource
= NULL
;
830 TRACE("%04x\n", wFormatID
);
832 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(wFormatID
)) &&
836 /* Look for rendered source or non-synthesized source */
837 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT
)) &&
838 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
840 TRACE("UNICODETEXT -> %04x\n", wFormatID
);
842 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_TEXT
)) &&
843 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
845 TRACE("TEXT -> %04x\n", wFormatID
);
847 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT
)) &&
848 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
850 TRACE("OEMTEXT -> %04x\n", wFormatID
);
853 if (!lpSource
|| (lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
&&
857 /* Ask the clipboard owner to render the source text if necessary */
858 if (!lpSource
->hData
&& !X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
861 lpstrS
= GlobalLock(lpSource
->hData
);
865 /* Text always NULL terminated */
866 if(lpSource
->wFormatID
== CF_UNICODETEXT
)
867 src_chars
= strlenW((LPCWSTR
)lpstrS
) + 1;
869 src_chars
= strlen(lpstrS
) + 1;
871 /* Calculate number of characters in the destination buffer */
872 dst_chars
= CLIPBOARD_ConvertText(lpSource
->wFormatID
, lpstrS
,
873 src_chars
, wFormatID
, NULL
, 0);
878 TRACE("Converting from '%04x' to '%04x', %i chars\n",
879 lpSource
->wFormatID
, wFormatID
, src_chars
);
881 /* Convert characters to bytes */
882 if(wFormatID
== CF_UNICODETEXT
)
883 alloc_size
= dst_chars
* sizeof(WCHAR
);
885 alloc_size
= dst_chars
;
887 hData
= GlobalAlloc(GMEM_ZEROINIT
| GMEM_MOVEABLE
|
888 GMEM_DDESHARE
, alloc_size
);
890 lpstrT
= GlobalLock(hData
);
894 CLIPBOARD_ConvertText(lpSource
->wFormatID
, lpstrS
, src_chars
,
895 wFormatID
, lpstrT
, dst_chars
);
899 GlobalUnlock(lpSource
->hData
);
901 return X11DRV_CLIPBOARD_InsertClipboardData(wFormatID
, hData
, 0, NULL
, TRUE
);
905 /***********************************************************************
908 * Return the size of the bitmap info structure including color table.
910 static int bitmap_info_size( const BITMAPINFO
* info
, WORD coloruse
)
912 unsigned int colors
, size
, masks
= 0;
914 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
916 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
917 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
918 return sizeof(BITMAPCOREHEADER
) + colors
*
919 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
921 else /* assume BITMAPINFOHEADER */
923 colors
= info
->bmiHeader
.biClrUsed
;
924 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
925 colors
= 1 << info
->bmiHeader
.biBitCount
;
926 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
927 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
928 return size
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
933 /***********************************************************************
934 * create_dib_from_bitmap
936 * Allocates a packed DIB and copies the bitmap data into it.
938 static HGLOBAL
create_dib_from_bitmap(HBITMAP hBmp
)
944 LPBITMAPINFOHEADER pbmiHeader
;
945 unsigned int cDataSize
, cPackedSize
, OffsetBits
;
948 if (!GetObjectW( hBmp
, sizeof(bmp
), &bmp
)) return 0;
951 * A packed DIB contains a BITMAPINFO structure followed immediately by
952 * an optional color palette and the pixel data.
955 /* Calculate the size of the packed DIB */
956 cDataSize
= abs( bmp
.bmHeight
) * (((bmp
.bmWidth
* bmp
.bmBitsPixel
+ 31) / 8) & ~3);
957 cPackedSize
= sizeof(BITMAPINFOHEADER
)
958 + ( (bmp
.bmBitsPixel
<= 8) ? (sizeof(RGBQUAD
) * (1 << bmp
.bmBitsPixel
)) : 0 )
960 /* Get the offset to the bits */
961 OffsetBits
= cPackedSize
- cDataSize
;
963 /* Allocate the packed DIB */
964 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize
);
965 hPackedDIB
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
/*| GMEM_ZEROINIT*/,
969 WARN("Could not allocate packed DIB!\n");
973 /* A packed DIB starts with a BITMAPINFOHEADER */
974 pPackedDIB
= GlobalLock(hPackedDIB
);
975 pbmiHeader
= (LPBITMAPINFOHEADER
)pPackedDIB
;
977 /* Init the BITMAPINFOHEADER */
978 pbmiHeader
->biSize
= sizeof(BITMAPINFOHEADER
);
979 pbmiHeader
->biWidth
= bmp
.bmWidth
;
980 pbmiHeader
->biHeight
= bmp
.bmHeight
;
981 pbmiHeader
->biPlanes
= 1;
982 pbmiHeader
->biBitCount
= bmp
.bmBitsPixel
;
983 pbmiHeader
->biCompression
= BI_RGB
;
984 pbmiHeader
->biSizeImage
= 0;
985 pbmiHeader
->biXPelsPerMeter
= pbmiHeader
->biYPelsPerMeter
= 0;
986 pbmiHeader
->biClrUsed
= 0;
987 pbmiHeader
->biClrImportant
= 0;
989 /* Retrieve the DIB bits from the bitmap and fill in the
990 * DIB color table if present */
992 nLinesCopied
= GetDIBits(hdc
, /* Handle to device context */
993 hBmp
, /* Handle to bitmap */
994 0, /* First scan line to set in dest bitmap */
995 bmp
.bmHeight
, /* Number of scan lines to copy */
996 pPackedDIB
+ OffsetBits
, /* [out] Address of array for bitmap bits */
997 (LPBITMAPINFO
) pbmiHeader
, /* [out] Address of BITMAPINFO structure */
998 0); /* RGB or palette index */
999 GlobalUnlock(hPackedDIB
);
1000 ReleaseDC( 0, hdc
);
1002 /* Cleanup if GetDIBits failed */
1003 if (nLinesCopied
!= bmp
.bmHeight
)
1005 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied
, bmp
.bmHeight
);
1006 GlobalFree(hPackedDIB
);
1013 /**************************************************************************
1014 * X11DRV_CLIPBOARD_RenderSynthesizedDIB
1016 * Renders synthesized DIB
1018 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedDIB(Display
*display
)
1021 LPWINE_CLIPDATA lpSource
= NULL
;
1025 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) && lpSource
->hData
)
1029 /* If we have a bitmap and it's not synthesized or it has been rendered */
1030 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) &&
1031 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
1033 /* Render source if required */
1034 if (lpSource
->hData
|| X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
1036 HGLOBAL hData
= create_dib_from_bitmap( lpSource
->hData
);
1039 X11DRV_CLIPBOARD_InsertClipboardData(CF_DIB
, hData
, 0, NULL
, TRUE
);
1049 /**************************************************************************
1050 * X11DRV_CLIPBOARD_RenderSynthesizedBitmap
1052 * Renders synthesized bitmap
1054 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedBitmap(Display
*display
)
1057 LPWINE_CLIPDATA lpSource
= NULL
;
1061 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) && lpSource
->hData
)
1065 /* If we have a dib and it's not synthesized or it has been rendered */
1066 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) &&
1067 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
1069 /* Render source if required */
1070 if (lpSource
->hData
|| X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
1073 HBITMAP hData
= NULL
;
1074 unsigned int offset
;
1075 LPBITMAPINFOHEADER lpbmih
;
1078 lpbmih
= GlobalLock(lpSource
->hData
);
1081 offset
= sizeof(BITMAPINFOHEADER
)
1082 + ((lpbmih
->biBitCount
<= 8) ? (sizeof(RGBQUAD
) *
1083 (1 << lpbmih
->biBitCount
)) : 0);
1085 hData
= CreateDIBitmap(hdc
, lpbmih
, CBM_INIT
, (LPBYTE
)lpbmih
+
1086 offset
, (LPBITMAPINFO
) lpbmih
, DIB_RGB_COLORS
);
1088 GlobalUnlock(lpSource
->hData
);
1090 ReleaseDC(NULL
, hdc
);
1094 X11DRV_CLIPBOARD_InsertClipboardData(CF_BITMAP
, hData
, 0, NULL
, TRUE
);
1104 /**************************************************************************
1105 * X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile
1107 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile(Display
*display
)
1109 LPWINE_CLIPDATA lpSource
= NULL
;
1113 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_ENHMETAFILE
)) && lpSource
->hData
)
1115 /* If we have a MF pict and it's not synthesized or it has been rendered */
1116 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT
)) &&
1117 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
1119 /* Render source if required */
1120 if (lpSource
->hData
|| X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
1123 HENHMETAFILE hData
= NULL
;
1125 pmfp
= GlobalLock(lpSource
->hData
);
1128 UINT size_mf_bits
= GetMetaFileBitsEx(pmfp
->hMF
, 0, NULL
);
1129 void *mf_bits
= HeapAlloc(GetProcessHeap(), 0, size_mf_bits
);
1132 GetMetaFileBitsEx(pmfp
->hMF
, size_mf_bits
, mf_bits
);
1133 hData
= SetWinMetaFileBits(size_mf_bits
, mf_bits
, NULL
, pmfp
);
1134 HeapFree(GetProcessHeap(), 0, mf_bits
);
1136 GlobalUnlock(lpSource
->hData
);
1141 X11DRV_CLIPBOARD_InsertClipboardData(CF_ENHMETAFILE
, hData
, 0, NULL
, TRUE
);
1151 /**************************************************************************
1152 * X11DRV_CLIPBOARD_ImportXAString
1154 * Import XA_STRING, converting the string to CF_TEXT.
1156 static HANDLE
X11DRV_CLIPBOARD_ImportXAString(Display
*display
, Window w
, Atom prop
)
1159 unsigned long cbytes
;
1161 unsigned long i
, inlcount
= 0;
1164 if (!X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1167 for (i
= 0; i
<= cbytes
; i
++)
1169 if (lpdata
[i
] == '\n')
1173 if ((hText
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cbytes
+ inlcount
+ 1)))
1175 lpstr
= GlobalLock(hText
);
1177 for (i
= 0, inlcount
= 0; i
<= cbytes
; i
++)
1179 if (lpdata
[i
] == '\n')
1180 lpstr
[inlcount
++] = '\r';
1182 lpstr
[inlcount
++] = lpdata
[i
];
1185 GlobalUnlock(hText
);
1188 /* Free the retrieved property data */
1189 HeapFree(GetProcessHeap(), 0, lpdata
);
1195 /**************************************************************************
1196 * X11DRV_CLIPBOARD_ImportUTF8
1198 * Import XA_STRING, converting the string to CF_UNICODE.
1200 static HANDLE
X11DRV_CLIPBOARD_ImportUTF8(Display
*display
, Window w
, Atom prop
)
1203 unsigned long cbytes
;
1205 unsigned long i
, inlcount
= 0;
1206 HANDLE hUnicodeText
= 0;
1208 if (!X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1211 for (i
= 0; i
<= cbytes
; i
++)
1213 if (lpdata
[i
] == '\n')
1217 if ((lpstr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cbytes
+ inlcount
+ 1)))
1221 for (i
= 0, inlcount
= 0; i
<= cbytes
; i
++)
1223 if (lpdata
[i
] == '\n')
1224 lpstr
[inlcount
++] = '\r';
1226 lpstr
[inlcount
++] = lpdata
[i
];
1229 count
= MultiByteToWideChar(CP_UTF8
, 0, lpstr
, -1, NULL
, 0);
1230 hUnicodeText
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, count
* sizeof(WCHAR
));
1234 WCHAR
*textW
= GlobalLock(hUnicodeText
);
1235 MultiByteToWideChar(CP_UTF8
, 0, lpstr
, -1, textW
, count
);
1236 GlobalUnlock(hUnicodeText
);
1239 HeapFree(GetProcessHeap(), 0, lpstr
);
1242 /* Free the retrieved property data */
1243 HeapFree(GetProcessHeap(), 0, lpdata
);
1245 return hUnicodeText
;
1249 /**************************************************************************
1250 * X11DRV_CLIPBOARD_ImportCompoundText
1252 * Import COMPOUND_TEXT to CF_UNICODE
1254 static HANDLE
X11DRV_CLIPBOARD_ImportCompoundText(Display
*display
, Window w
, Atom prop
)
1259 int srclen
, destlen
;
1260 HANDLE hUnicodeText
;
1261 XTextProperty txtprop
;
1263 if (!X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &txtprop
.value
, &txtprop
.nitems
))
1268 txtprop
.encoding
= x11drv_atom(COMPOUND_TEXT
);
1271 ret
= XmbTextPropertyToTextList(display
, &txtprop
, &srcstr
, &count
);
1272 wine_tsx11_unlock();
1273 HeapFree(GetProcessHeap(), 0, txtprop
.value
);
1274 if (ret
!= Success
|| !count
) return 0;
1276 TRACE("Importing %d line(s)\n", count
);
1278 /* Compute number of lines */
1279 srclen
= strlen(srcstr
[0]);
1280 for (i
= 0, lcount
= 0; i
<= srclen
; i
++)
1282 if (srcstr
[0][i
] == '\n')
1286 destlen
= MultiByteToWideChar(CP_UNIXCP
, 0, srcstr
[0], -1, NULL
, 0);
1288 TRACE("lcount = %d, destlen=%d, srcstr %s\n", lcount
, destlen
, srcstr
[0]);
1290 if ((hUnicodeText
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, (destlen
+ lcount
+ 1) * sizeof(WCHAR
))))
1292 WCHAR
*deststr
= GlobalLock(hUnicodeText
);
1293 MultiByteToWideChar(CP_UNIXCP
, 0, srcstr
[0], -1, deststr
, destlen
);
1297 for (i
= destlen
- 1, j
= destlen
+ lcount
- 1; i
>= 0; i
--, j
--)
1299 deststr
[j
] = deststr
[i
];
1301 if (deststr
[i
] == '\n')
1302 deststr
[--j
] = '\r';
1306 GlobalUnlock(hUnicodeText
);
1310 XFreeStringList(srcstr
);
1311 wine_tsx11_unlock();
1313 return hUnicodeText
;
1317 /**************************************************************************
1318 * X11DRV_CLIPBOARD_ImportXAPIXMAP
1320 * Import XA_PIXMAP, converting the image to CF_DIB.
1322 static HANDLE
X11DRV_CLIPBOARD_ImportXAPIXMAP(Display
*display
, Window w
, Atom prop
)
1325 unsigned long cbytes
;
1327 HANDLE hClipData
= 0;
1329 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1332 char buffer
[FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )];
1333 BITMAPINFO
*info
= (BITMAPINFO
*)buffer
;
1334 struct gdi_image_bits bits
;
1336 int x
,y
; /* Unused */
1337 unsigned border_width
; /* Unused */
1338 unsigned int depth
, width
, height
;
1340 pPixmap
= (Pixmap
*) lpdata
;
1342 /* Get the Pixmap dimensions and bit depth */
1344 if (!XGetGeometry(gdi_display
, *pPixmap
, &root
, &x
, &y
, &width
, &height
,
1345 &border_width
, &depth
)) depth
= 0;
1346 wine_tsx11_unlock();
1347 if (!pixmap_formats
[depth
]) return 0;
1349 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
1350 width
, height
, depth
);
1352 memset( &vis
, 0, sizeof(vis
) );
1354 if (depth
== screen_depth
)
1356 vis
.visual
= visual
;
1357 vis
.visualid
= visual
->visualid
;
1358 vis
.class = visual
->class;
1359 vis
.red_mask
= visual
->red_mask
;
1360 vis
.green_mask
= visual
->green_mask
;
1361 vis
.blue_mask
= visual
->blue_mask
;
1363 else switch (pixmap_formats
[depth
]->bits_per_pixel
)
1369 case 16: /* assume R5G5B5 */
1370 vis
.red_mask
= 0x7c00;
1371 vis
.green_mask
= 0x03e0;
1372 vis
.blue_mask
= 0x001f;
1374 case 24: /* assume R8G8B8 */
1375 case 32: /* assume A8R8G8B8 */
1376 vis
.red_mask
= 0xff0000;
1377 vis
.green_mask
= 0x00ff00;
1378 vis
.blue_mask
= 0x0000ff;
1384 if (!get_pixmap_image( *pPixmap
, width
, height
, &vis
, info
, &bits
))
1386 DWORD info_size
= bitmap_info_size( info
, DIB_RGB_COLORS
);
1389 hClipData
= GlobalAlloc( GMEM_MOVEABLE
| GMEM_DDESHARE
,
1390 info_size
+ info
->bmiHeader
.biSizeImage
);
1393 ptr
= GlobalLock( hClipData
);
1394 memcpy( ptr
, info
, info_size
);
1395 memcpy( ptr
+ info_size
, bits
.ptr
, info
->bmiHeader
.biSizeImage
);
1396 GlobalUnlock( hClipData
);
1398 if (bits
.free
) bits
.free( &bits
);
1406 /**************************************************************************
1407 * X11DRV_CLIPBOARD_ImportImageBmp
1409 * Import image/bmp, converting the image to CF_DIB.
1411 static HANDLE
X11DRV_CLIPBOARD_ImportImageBmp(Display
*display
, Window w
, Atom prop
)
1414 unsigned long cbytes
;
1415 HANDLE hClipData
= 0;
1417 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1419 BITMAPFILEHEADER
*bfh
= (BITMAPFILEHEADER
*)lpdata
;
1421 if (cbytes
>= sizeof(BITMAPFILEHEADER
)+sizeof(BITMAPCOREHEADER
) &&
1422 bfh
->bfType
== 0x4d42 /* "BM" */)
1424 BITMAPINFO
*bmi
= (BITMAPINFO
*)(bfh
+1);
1429 hbmp
= CreateDIBitmap(
1433 lpdata
+bfh
->bfOffBits
,
1438 hClipData
= create_dib_from_bitmap( hbmp
);
1444 /* Free the retrieved property data */
1445 HeapFree(GetProcessHeap(), 0, lpdata
);
1452 /**************************************************************************
1453 * X11DRV_CLIPBOARD_ImportMetaFilePict
1455 * Import MetaFilePict.
1457 static HANDLE
X11DRV_CLIPBOARD_ImportMetaFilePict(Display
*display
, Window w
, Atom prop
)
1460 unsigned long cbytes
;
1461 HANDLE hClipData
= 0;
1463 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1466 hClipData
= X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT
, lpdata
, (LPDWORD
)&cbytes
, FALSE
);
1468 /* Free the retrieved property data */
1469 HeapFree(GetProcessHeap(), 0, lpdata
);
1476 /**************************************************************************
1477 * X11DRV_ImportEnhMetaFile
1479 * Import EnhMetaFile.
1481 static HANDLE
X11DRV_CLIPBOARD_ImportEnhMetaFile(Display
*display
, Window w
, Atom prop
)
1484 unsigned long cbytes
;
1485 HANDLE hClipData
= 0;
1487 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1490 hClipData
= X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE
, lpdata
, (LPDWORD
)&cbytes
, FALSE
);
1492 /* Free the retrieved property data */
1493 HeapFree(GetProcessHeap(), 0, lpdata
);
1500 /**************************************************************************
1501 * X11DRV_ImportClipbordaData
1503 * Generic import clipboard data routine.
1505 static HANDLE
X11DRV_CLIPBOARD_ImportClipboardData(Display
*display
, Window w
, Atom prop
)
1509 unsigned long cbytes
;
1510 HANDLE hClipData
= 0;
1512 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1516 /* Turn on the DDESHARE flag to enable shared 32 bit memory */
1517 hClipData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cbytes
);
1521 if ((lpClipData
= GlobalLock(hClipData
)))
1523 memcpy(lpClipData
, lpdata
, cbytes
);
1524 GlobalUnlock(hClipData
);
1528 GlobalFree(hClipData
);
1533 /* Free the retrieved property data */
1534 HeapFree(GetProcessHeap(), 0, lpdata
);
1541 /**************************************************************************
1542 X11DRV_CLIPBOARD_ExportClipboardData
1544 * Generic export clipboard data routine.
1546 static HANDLE
X11DRV_CLIPBOARD_ExportClipboardData(Display
*display
, Window requestor
, Atom aTarget
,
1547 Atom rprop
, LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1551 HANDLE hClipData
= 0;
1553 *lpBytes
= 0; /* Assume failure */
1555 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpData
))
1556 ERR("Failed to export %04x format\n", lpData
->wFormatID
);
1559 datasize
= GlobalSize(lpData
->hData
);
1561 hClipData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, datasize
);
1562 if (hClipData
== 0) return NULL
;
1564 if ((lpClipData
= GlobalLock(hClipData
)))
1566 LPVOID lpdata
= GlobalLock(lpData
->hData
);
1568 memcpy(lpClipData
, lpdata
, datasize
);
1569 *lpBytes
= datasize
;
1571 GlobalUnlock(lpData
->hData
);
1572 GlobalUnlock(hClipData
);
1574 GlobalFree(hClipData
);
1583 /**************************************************************************
1584 * X11DRV_CLIPBOARD_ExportXAString
1586 * Export CF_TEXT converting the string to XA_STRING.
1587 * Helper function for X11DRV_CLIPBOARD_ExportString.
1589 static HANDLE
X11DRV_CLIPBOARD_ExportXAString(LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1593 LPSTR text
, lpstr
= NULL
;
1595 *lpBytes
= 0; /* Assume return has zero bytes */
1597 text
= GlobalLock(lpData
->hData
);
1598 size
= strlen(text
);
1600 /* remove carriage returns */
1601 lpstr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
+ 1);
1605 for (i
= 0,j
= 0; i
< size
&& text
[i
]; i
++)
1607 if (text
[i
] == '\r' && (text
[i
+1] == '\n' || text
[i
+1] == '\0'))
1609 lpstr
[j
++] = text
[i
];
1613 *lpBytes
= j
; /* Number of bytes in string */
1616 GlobalUnlock(lpData
->hData
);
1622 /**************************************************************************
1623 * X11DRV_CLIPBOARD_ExportUTF8String
1625 * Export CF_UNICODE converting the string to UTF8.
1626 * Helper function for X11DRV_CLIPBOARD_ExportString.
1628 static HANDLE
X11DRV_CLIPBOARD_ExportUTF8String(LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1633 LPSTR text
, lpstr
= NULL
;
1635 *lpBytes
= 0; /* Assume return has zero bytes */
1637 uni_text
= GlobalLock(lpData
->hData
);
1639 size
= WideCharToMultiByte(CP_UTF8
, 0, uni_text
, -1, NULL
, 0, NULL
, NULL
);
1641 text
= HeapAlloc(GetProcessHeap(), 0, size
);
1644 WideCharToMultiByte(CP_UTF8
, 0, uni_text
, -1, text
, size
, NULL
, NULL
);
1646 /* remove carriage returns */
1647 lpstr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
--);
1651 for (i
= 0,j
= 0; i
< size
&& text
[i
]; i
++)
1653 if (text
[i
] == '\r' && (text
[i
+1] == '\n' || text
[i
+1] == '\0'))
1655 lpstr
[j
++] = text
[i
];
1659 *lpBytes
= j
; /* Number of bytes in string */
1662 HeapFree(GetProcessHeap(), 0, text
);
1663 GlobalUnlock(lpData
->hData
);
1670 /**************************************************************************
1671 * X11DRV_CLIPBOARD_ExportCompoundText
1673 * Export CF_UNICODE to COMPOUND_TEXT
1674 * Helper function for X11DRV_CLIPBOARD_ExportString.
1676 static HANDLE
X11DRV_CLIPBOARD_ExportCompoundText(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1677 LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1681 XICCEncodingStyle style
;
1686 uni_text
= GlobalLock(lpData
->hData
);
1688 size
= WideCharToMultiByte(CP_UNIXCP
, 0, uni_text
, -1, NULL
, 0, NULL
, NULL
);
1689 lpstr
= HeapAlloc(GetProcessHeap(), 0, size
);
1693 WideCharToMultiByte(CP_UNIXCP
, 0, uni_text
, -1, lpstr
, size
, NULL
, NULL
);
1695 /* remove carriage returns */
1696 for (i
= 0, j
= 0; i
< size
&& lpstr
[i
]; i
++)
1698 if (lpstr
[i
] == '\r' && (lpstr
[i
+1] == '\n' || lpstr
[i
+1] == '\0'))
1700 lpstr
[j
++] = lpstr
[i
];
1704 GlobalUnlock(lpData
->hData
);
1706 if (aTarget
== x11drv_atom(COMPOUND_TEXT
))
1707 style
= XCompoundTextStyle
;
1709 style
= XStdICCTextStyle
;
1711 /* Update the X property */
1713 if (XmbTextListToTextProperty(display
, &lpstr
, 1, style
, &prop
) == Success
)
1715 XSetTextProperty(display
, requestor
, &prop
, rprop
);
1718 wine_tsx11_unlock();
1720 HeapFree(GetProcessHeap(), 0, lpstr
);
1725 /**************************************************************************
1726 * X11DRV_CLIPBOARD_ExportString
1730 static HANDLE
X11DRV_CLIPBOARD_ExportString(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1731 LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1733 if (X11DRV_CLIPBOARD_RenderFormat(display
, lpData
))
1735 if (aTarget
== XA_STRING
)
1736 return X11DRV_CLIPBOARD_ExportXAString(lpData
, lpBytes
);
1737 else if (aTarget
== x11drv_atom(COMPOUND_TEXT
) || aTarget
== x11drv_atom(TEXT
))
1738 return X11DRV_CLIPBOARD_ExportCompoundText(display
, requestor
, aTarget
,
1739 rprop
, lpData
, lpBytes
);
1742 TRACE("Exporting target %ld to default UTF8_STRING\n", aTarget
);
1743 return X11DRV_CLIPBOARD_ExportUTF8String(lpData
, lpBytes
);
1747 ERR("Failed to render %04x format\n", lpData
->wFormatID
);
1753 /**************************************************************************
1754 * X11DRV_CLIPBOARD_ExportXAPIXMAP
1756 * Export CF_DIB to XA_PIXMAP.
1758 static HANDLE
X11DRV_CLIPBOARD_ExportXAPIXMAP(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1759 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1762 unsigned char* lpData
;
1764 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1766 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1770 if (!lpdata
->drvData
) /* If not already rendered */
1775 struct gdi_image_bits bits
;
1777 memset( &vis
, 0, sizeof(vis
) );
1778 vis
.visual
= visual
;
1779 vis
.depth
= screen_depth
;
1780 vis
.visualid
= visual
->visualid
;
1781 vis
.class = visual
->class;
1782 vis
.red_mask
= visual
->red_mask
;
1783 vis
.green_mask
= visual
->green_mask
;
1784 vis
.blue_mask
= visual
->blue_mask
;
1786 pbmi
= GlobalLock( lpdata
->hData
);
1787 bits
.ptr
= (LPBYTE
)pbmi
+ bitmap_info_size( pbmi
, DIB_RGB_COLORS
);
1789 bits
.is_copy
= FALSE
;
1790 pixmap
= create_pixmap_from_image( 0, &vis
, pbmi
, &bits
, DIB_RGB_COLORS
);
1791 GlobalUnlock( lpdata
->hData
);
1792 lpdata
->drvData
= pixmap
;
1795 *lpBytes
= sizeof(Pixmap
); /* pixmap is a 32bit value */
1797 /* Wrap pixmap so we can return a handle */
1798 hData
= GlobalAlloc(0, *lpBytes
);
1799 lpData
= GlobalLock(hData
);
1800 memcpy(lpData
, &lpdata
->drvData
, *lpBytes
);
1801 GlobalUnlock(hData
);
1807 /**************************************************************************
1808 * X11DRV_CLIPBOARD_ExportImageBmp
1810 * Export CF_DIB to image/bmp.
1812 static HANDLE
X11DRV_CLIPBOARD_ExportImageBmp(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1813 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1820 BITMAPFILEHEADER
*bfh
;
1824 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1826 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1830 hpackeddib
= lpdata
->hData
;
1832 dibdata
= GlobalLock(hpackeddib
);
1835 ERR("Failed to lock packed DIB\n");
1839 bmpsize
= sizeof(BITMAPFILEHEADER
) + GlobalSize(hpackeddib
);
1841 hbmpdata
= GlobalAlloc(0, bmpsize
);
1845 bmpdata
= GlobalLock(hbmpdata
);
1849 GlobalFree(hbmpdata
);
1850 GlobalUnlock(hpackeddib
);
1854 /* bitmap file header */
1855 bfh
= (BITMAPFILEHEADER
*)bmpdata
;
1856 bfh
->bfType
= 0x4d42; /* "BM" */
1857 bfh
->bfSize
= bmpsize
;
1858 bfh
->bfReserved1
= 0;
1859 bfh
->bfReserved2
= 0;
1860 bfh
->bfOffBits
= sizeof(BITMAPFILEHEADER
) + bitmap_info_size((BITMAPINFO
*)dibdata
, DIB_RGB_COLORS
);
1862 /* rest of bitmap is the same as the packed dib */
1863 memcpy(bfh
+1, dibdata
, bmpsize
-sizeof(BITMAPFILEHEADER
));
1867 GlobalUnlock(hbmpdata
);
1870 GlobalUnlock(hpackeddib
);
1876 /**************************************************************************
1877 * X11DRV_CLIPBOARD_ExportMetaFilePict
1879 * Export MetaFilePict.
1881 static HANDLE
X11DRV_CLIPBOARD_ExportMetaFilePict(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1882 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1884 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1886 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1890 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT
, lpdata
->hData
, lpBytes
, TRUE
);
1894 /**************************************************************************
1895 * X11DRV_CLIPBOARD_ExportEnhMetaFile
1897 * Export EnhMetaFile.
1899 static HANDLE
X11DRV_CLIPBOARD_ExportEnhMetaFile(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1900 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1902 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1904 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1908 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE
, lpdata
->hData
, lpBytes
, TRUE
);
1912 /**************************************************************************
1913 * get_html_description_field
1915 * Find the value of a field in an HTML Format description.
1917 static LPCSTR
get_html_description_field(LPCSTR data
, LPCSTR keyword
)
1921 while (pos
&& *pos
&& *pos
!= '<')
1923 if (memcmp(pos
, keyword
, strlen(keyword
)) == 0)
1924 return pos
+strlen(keyword
);
1926 pos
= strchr(pos
, '\n');
1934 /**************************************************************************
1935 * X11DRV_CLIPBOARD_ExportTextHtml
1937 * Export HTML Format to text/html.
1939 * FIXME: We should attempt to add an <a base> tag and convert windows paths.
1941 static HANDLE
X11DRV_CLIPBOARD_ExportTextHtml(Display
*display
, Window requestor
, Atom aTarget
,
1942 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1945 LPCSTR data
, field_value
;
1946 UINT fragmentstart
, fragmentend
, htmlsize
;
1947 HANDLE hhtmldata
=NULL
;
1952 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1954 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1958 hdata
= lpdata
->hData
;
1960 data
= GlobalLock(hdata
);
1963 ERR("Failed to lock HTML Format data\n");
1967 /* read the important fields */
1968 field_value
= get_html_description_field(data
, "StartFragment:");
1971 ERR("Couldn't find StartFragment value\n");
1974 fragmentstart
= atoi(field_value
);
1976 field_value
= get_html_description_field(data
, "EndFragment:");
1979 ERR("Couldn't find EndFragment value\n");
1982 fragmentend
= atoi(field_value
);
1984 /* export only the fragment */
1985 htmlsize
= fragmentend
- fragmentstart
+ 1;
1987 hhtmldata
= GlobalAlloc(0, htmlsize
);
1991 htmldata
= GlobalLock(hhtmldata
);
1995 GlobalFree(hhtmldata
);
2000 memcpy(htmldata
, &data
[fragmentstart
], fragmentend
-fragmentstart
);
2001 htmldata
[htmlsize
-1] = '\0';
2003 *lpBytes
= htmlsize
;
2005 GlobalUnlock(htmldata
);
2010 GlobalUnlock(hdata
);
2016 /**************************************************************************
2017 * X11DRV_CLIPBOARD_QueryTargets
2019 static BOOL
X11DRV_CLIPBOARD_QueryTargets(Display
*display
, Window w
, Atom selection
,
2020 Atom target
, XEvent
*xe
)
2026 XConvertSelection(display
, selection
, target
,
2027 x11drv_atom(SELECTION_DATA
), w
, CurrentTime
);
2028 wine_tsx11_unlock();
2031 * Wait until SelectionNotify is received
2033 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
2036 res
= XCheckTypedWindowEvent(display
, w
, SelectionNotify
, xe
);
2037 wine_tsx11_unlock();
2038 if (res
&& xe
->xselection
.selection
== selection
) break;
2040 usleep(SELECTION_WAIT
);
2043 if (i
== SELECTION_RETRIES
)
2045 ERR("Timed out waiting for SelectionNotify event\n");
2048 /* Verify that the selection returned a valid TARGETS property */
2049 if ((xe
->xselection
.target
!= target
) || (xe
->xselection
.property
== None
))
2051 /* Selection owner failed to respond or we missed the SelectionNotify */
2052 WARN("Failed to retrieve TARGETS for selection %ld.\n", selection
);
2060 static int is_atom_error( Display
*display
, XErrorEvent
*event
, void *arg
)
2062 return (event
->error_code
== BadAtom
);
2065 /**************************************************************************
2066 * X11DRV_CLIPBOARD_InsertSelectionProperties
2068 * Mark properties available for future retrieval.
2070 static VOID
X11DRV_CLIPBOARD_InsertSelectionProperties(Display
*display
, Atom
* properties
, UINT count
)
2072 UINT i
, nb_atoms
= 0;
2075 /* Cache these formats in the clipboard cache */
2076 for (i
= 0; i
< count
; i
++)
2078 LPWINE_CLIPFORMAT lpFormat
= X11DRV_CLIPBOARD_LookupProperty(NULL
, properties
[i
]);
2082 /* We found at least one Window's format that mapps to the property.
2083 * Continue looking for more.
2085 * If more than one property map to a Window's format then we use the first
2086 * one and ignore the rest.
2090 TRACE("Atom#%d Property(%d): --> Format %s\n",
2091 i
, lpFormat
->drvData
, debugstr_format(lpFormat
->wFormatID
));
2092 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat
->wFormatID
, 0, 0, lpFormat
, FALSE
);
2093 lpFormat
= X11DRV_CLIPBOARD_LookupProperty(lpFormat
, properties
[i
]);
2096 else if (properties
[i
])
2098 /* add it to the list of atoms that we don't know about yet */
2099 if (!atoms
) atoms
= HeapAlloc( GetProcessHeap(), 0,
2100 (count
- i
) * sizeof(*atoms
) );
2101 if (atoms
) atoms
[nb_atoms
++] = properties
[i
];
2105 /* query all unknown atoms in one go */
2108 char **names
= HeapAlloc( GetProcessHeap(), 0, nb_atoms
* sizeof(*names
) );
2111 X11DRV_expect_error( display
, is_atom_error
, NULL
);
2112 if (!XGetAtomNames( display
, atoms
, nb_atoms
, names
)) nb_atoms
= 0;
2113 if (X11DRV_check_error())
2115 WARN( "got some bad atoms, ignoring\n" );
2118 for (i
= 0; i
< nb_atoms
; i
++)
2120 WINE_CLIPFORMAT
*lpFormat
;
2122 int len
= MultiByteToWideChar(CP_UNIXCP
, 0, names
[i
], -1, NULL
, 0);
2123 wname
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
2124 MultiByteToWideChar(CP_UNIXCP
, 0, names
[i
], -1, wname
, len
);
2126 lpFormat
= register_format( RegisterClipboardFormatW(wname
), atoms
[i
] );
2127 HeapFree(GetProcessHeap(), 0, wname
);
2130 ERR("Failed to register %s property. Type will not be cached.\n", names
[i
]);
2133 TRACE("Atom#%d Property(%d): --> Format %s\n",
2134 i
, lpFormat
->drvData
, debugstr_format(lpFormat
->wFormatID
));
2135 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat
->wFormatID
, 0, 0, lpFormat
, FALSE
);
2138 for (i
= 0; i
< nb_atoms
; i
++) XFree( names
[i
] );
2139 wine_tsx11_unlock();
2140 HeapFree( GetProcessHeap(), 0, names
);
2142 HeapFree( GetProcessHeap(), 0, atoms
);
2147 /**************************************************************************
2148 * X11DRV_CLIPBOARD_QueryAvailableData
2150 * Caches the list of data formats available from the current selection.
2151 * This queries the selection owner for the TARGETS property and saves all
2152 * reported property types.
2154 static int X11DRV_CLIPBOARD_QueryAvailableData(Display
*display
, LPCLIPBOARDINFO lpcbinfo
)
2157 Atom atype
=AnyPropertyType
;
2159 unsigned long remain
;
2160 Atom
* targetList
=NULL
;
2162 unsigned long cSelectionTargets
= 0;
2164 if (selectionAcquired
& (S_PRIMARY
| S_CLIPBOARD
))
2166 ERR("Received request to cache selection but process is owner=(%08x)\n",
2167 (unsigned) selectionWindow
);
2168 return -1; /* Prevent self request */
2171 w
= thread_selection_wnd();
2174 ERR("No window available to retrieve selection!\n");
2179 * Query the selection owner for the TARGETS property
2182 if ((use_primary_selection
&& XGetSelectionOwner(display
,XA_PRIMARY
)) ||
2183 XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)))
2185 wine_tsx11_unlock();
2186 if (use_primary_selection
&& (X11DRV_CLIPBOARD_QueryTargets(display
, w
, XA_PRIMARY
, x11drv_atom(TARGETS
), &xe
)))
2187 selectionCacheSrc
= XA_PRIMARY
;
2188 else if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, x11drv_atom(CLIPBOARD
), x11drv_atom(TARGETS
), &xe
))
2189 selectionCacheSrc
= x11drv_atom(CLIPBOARD
);
2192 Atom xstr
= XA_STRING
;
2194 /* Selection Owner doesn't understand TARGETS, try retrieving XA_STRING */
2195 if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, XA_PRIMARY
, XA_STRING
, &xe
))
2197 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, &xstr
, 1);
2198 selectionCacheSrc
= XA_PRIMARY
;
2201 else if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, x11drv_atom(CLIPBOARD
), XA_STRING
, &xe
))
2203 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, &xstr
, 1);
2204 selectionCacheSrc
= x11drv_atom(CLIPBOARD
);
2209 WARN("Failed to query selection owner for available data.\n");
2214 else /* No selection owner so report 0 targets available */
2216 wine_tsx11_unlock();
2220 /* Read the TARGETS property contents */
2222 if(XGetWindowProperty(display
, xe
.xselection
.requestor
, xe
.xselection
.property
,
2223 0, 0x3FFF, True
, AnyPropertyType
/*XA_ATOM*/, &atype
, &aformat
, &cSelectionTargets
,
2224 &remain
, (unsigned char**)&targetList
) != Success
)
2226 wine_tsx11_unlock();
2227 WARN("Failed to read TARGETS property\n");
2231 wine_tsx11_unlock();
2232 TRACE("Type %lx,Format %d,nItems %ld, Remain %ld\n",
2233 atype
, aformat
, cSelectionTargets
, remain
);
2235 * The TARGETS property should have returned us a list of atoms
2236 * corresponding to each selection target format supported.
2238 if (atype
== XA_ATOM
|| atype
== x11drv_atom(TARGETS
))
2242 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, targetList
, cSelectionTargets
);
2244 else if (aformat
== 8) /* work around quartz-wm brain damage */
2246 unsigned long i
, count
= cSelectionTargets
/ sizeof(CARD32
);
2247 Atom
*atoms
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(Atom
) );
2248 for (i
= 0; i
< count
; i
++)
2249 atoms
[i
] = ((CARD32
*)targetList
)[i
]; /* FIXME: byte swapping */
2250 X11DRV_CLIPBOARD_InsertSelectionProperties( display
, atoms
, count
);
2251 HeapFree( GetProcessHeap(), 0, atoms
);
2255 /* Free the list of targets */
2258 wine_tsx11_unlock();
2261 return cSelectionTargets
;
2265 /**************************************************************************
2266 * X11DRV_CLIPBOARD_ReadSelectionData
2268 * This method is invoked only when we DO NOT own the X selection
2270 * We always get the data from the selection client each time,
2271 * since we have no way of determining if the data in our cache is stale.
2273 static BOOL
X11DRV_CLIPBOARD_ReadSelectionData(Display
*display
, LPWINE_CLIPDATA lpData
)
2280 TRACE("%04x\n", lpData
->wFormatID
);
2282 if (!lpData
->lpFormat
)
2284 ERR("Requesting format %04x but no source format linked to data.\n",
2289 if (!selectionAcquired
)
2291 Window w
= thread_selection_wnd();
2294 ERR("No window available to read selection data!\n");
2298 TRACE("Requesting conversion of %s property (%d) from selection type %08x\n",
2299 debugstr_format(lpData
->lpFormat
->wFormatID
), lpData
->lpFormat
->drvData
,
2300 (UINT
)selectionCacheSrc
);
2303 XConvertSelection(display
, selectionCacheSrc
, lpData
->lpFormat
->drvData
,
2304 x11drv_atom(SELECTION_DATA
), w
, CurrentTime
);
2305 wine_tsx11_unlock();
2307 /* wait until SelectionNotify is received */
2308 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
2311 res
= XCheckTypedWindowEvent(display
, w
, SelectionNotify
, &xe
);
2312 wine_tsx11_unlock();
2313 if (res
&& xe
.xselection
.selection
== selectionCacheSrc
) break;
2315 usleep(SELECTION_WAIT
);
2318 if (i
== SELECTION_RETRIES
)
2320 ERR("Timed out waiting for SelectionNotify event\n");
2322 /* Verify that the selection returned a valid TARGETS property */
2323 else if (xe
.xselection
.property
!= None
)
2326 * Read the contents of the X selection property
2327 * into WINE's clipboard cache and converting the
2328 * data format if necessary.
2330 HANDLE hData
= lpData
->lpFormat
->lpDrvImportFunc(display
, xe
.xselection
.requestor
,
2331 xe
.xselection
.property
);
2334 bRet
= X11DRV_CLIPBOARD_InsertClipboardData(lpData
->wFormatID
, hData
, 0, lpData
->lpFormat
, TRUE
);
2336 TRACE("Import function failed\n");
2340 TRACE("Failed to convert selection\n");
2345 ERR("Received request to cache selection data but process is owner\n");
2348 TRACE("Returning %d\n", bRet
);
2354 /**************************************************************************
2355 * X11DRV_CLIPBOARD_GetProperty
2356 * Gets type, data and size.
2358 static BOOL
X11DRV_CLIPBOARD_GetProperty(Display
*display
, Window w
, Atom prop
,
2359 Atom
*atype
, unsigned char** data
, unsigned long* datasize
)
2362 unsigned long pos
= 0, nitems
, remain
, count
;
2363 unsigned char *val
= NULL
, *buffer
;
2365 TRACE("Reading property %lu from X window %lx\n", prop
, w
);
2370 if (XGetWindowProperty(display
, w
, prop
, pos
, INT_MAX
/ 4, False
,
2371 AnyPropertyType
, atype
, &aformat
, &nitems
, &remain
, &buffer
) != Success
)
2373 wine_tsx11_unlock();
2374 WARN("Failed to read property\n");
2375 HeapFree( GetProcessHeap(), 0, val
);
2379 count
= get_property_size( aformat
, nitems
);
2380 if (!val
) *data
= HeapAlloc( GetProcessHeap(), 0, pos
* sizeof(int) + count
+ 1 );
2381 else *data
= HeapReAlloc( GetProcessHeap(), 0, val
, pos
* sizeof(int) + count
+ 1 );
2386 wine_tsx11_unlock();
2387 HeapFree( GetProcessHeap(), 0, val
);
2391 memcpy( (int *)val
+ pos
, buffer
, count
);
2393 wine_tsx11_unlock();
2396 *datasize
= pos
* sizeof(int) + count
;
2400 pos
+= count
/ sizeof(int);
2403 /* Delete the property on the window now that we are done
2404 * This will send a PropertyNotify event to the selection owner. */
2406 XDeleteProperty(display
, w
, prop
);
2407 wine_tsx11_unlock();
2412 /**************************************************************************
2413 * X11DRV_CLIPBOARD_ReadProperty
2414 * Reads the contents of the X selection property.
2416 static BOOL
X11DRV_CLIPBOARD_ReadProperty(Display
*display
, Window w
, Atom prop
,
2417 unsigned char** data
, unsigned long* datasize
)
2425 if (!X11DRV_CLIPBOARD_GetProperty(display
, w
, prop
, &atype
, data
, datasize
))
2429 while (XCheckTypedWindowEvent(display
, w
, PropertyNotify
, &xe
))
2431 wine_tsx11_unlock();
2433 if (atype
== x11drv_atom(INCR
))
2435 unsigned char *buf
= *data
;
2436 unsigned long bufsize
= 0;
2441 unsigned char *prop_data
, *tmp
;
2442 unsigned long prop_size
;
2444 /* Wait until PropertyNotify is received */
2445 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
2450 res
= XCheckTypedWindowEvent(display
, w
, PropertyNotify
, &xe
);
2451 wine_tsx11_unlock();
2452 if (res
&& xe
.xproperty
.atom
== prop
&&
2453 xe
.xproperty
.state
== PropertyNewValue
)
2455 usleep(SELECTION_WAIT
);
2458 if (i
>= SELECTION_RETRIES
||
2459 !X11DRV_CLIPBOARD_GetProperty(display
, w
, prop
, &atype
, &prop_data
, &prop_size
))
2461 HeapFree(GetProcessHeap(), 0, buf
);
2465 /* Retrieved entire data. */
2468 HeapFree(GetProcessHeap(), 0, prop_data
);
2470 *datasize
= bufsize
;
2474 tmp
= HeapReAlloc(GetProcessHeap(), 0, buf
, bufsize
+ prop_size
+ 1);
2477 HeapFree(GetProcessHeap(), 0, buf
);
2482 memcpy(buf
+ bufsize
, prop_data
, prop_size
+ 1);
2483 bufsize
+= prop_size
;
2484 HeapFree(GetProcessHeap(), 0, prop_data
);
2492 /**************************************************************************
2493 * CLIPBOARD_SerializeMetafile
2495 static HANDLE
X11DRV_CLIPBOARD_SerializeMetafile(INT wformat
, HANDLE hdata
, LPDWORD lpcbytes
, BOOL out
)
2499 TRACE(" wFormat=%d hdata=%p out=%d\n", wformat
, hdata
, out
);
2501 if (out
) /* Serialize out, caller should free memory */
2503 *lpcbytes
= 0; /* Assume failure */
2505 if (wformat
== CF_METAFILEPICT
)
2507 LPMETAFILEPICT lpmfp
= GlobalLock(hdata
);
2508 unsigned int size
= GetMetaFileBitsEx(lpmfp
->hMF
, 0, NULL
);
2510 h
= GlobalAlloc(0, size
+ sizeof(METAFILEPICT
));
2513 char *pdata
= GlobalLock(h
);
2515 memcpy(pdata
, lpmfp
, sizeof(METAFILEPICT
));
2516 GetMetaFileBitsEx(lpmfp
->hMF
, size
, pdata
+ sizeof(METAFILEPICT
));
2518 *lpcbytes
= size
+ sizeof(METAFILEPICT
);
2523 GlobalUnlock(hdata
);
2525 else if (wformat
== CF_ENHMETAFILE
)
2527 int size
= GetEnhMetaFileBits(hdata
, 0, NULL
);
2529 h
= GlobalAlloc(0, size
);
2532 LPVOID pdata
= GlobalLock(h
);
2534 GetEnhMetaFileBits(hdata
, size
, pdata
);
2543 if (wformat
== CF_METAFILEPICT
)
2545 h
= GlobalAlloc(0, sizeof(METAFILEPICT
));
2548 unsigned int wiresize
;
2549 LPMETAFILEPICT lpmfp
= GlobalLock(h
);
2551 memcpy(lpmfp
, hdata
, sizeof(METAFILEPICT
));
2552 wiresize
= *lpcbytes
- sizeof(METAFILEPICT
);
2553 lpmfp
->hMF
= SetMetaFileBitsEx(wiresize
,
2554 ((const BYTE
*)hdata
) + sizeof(METAFILEPICT
));
2558 else if (wformat
== CF_ENHMETAFILE
)
2560 h
= SetEnhMetaFileBits(*lpcbytes
, hdata
);
2568 /**************************************************************************
2569 * X11DRV_CLIPBOARD_ReleaseSelection
2571 * Release XA_CLIPBOARD and XA_PRIMARY in response to a SelectionClear event.
2573 static void X11DRV_CLIPBOARD_ReleaseSelection(Display
*display
, Atom selType
, Window w
, HWND hwnd
, Time time
)
2575 /* w is the window that lost the selection
2577 TRACE("event->window = %08x (selectionWindow = %08x) selectionAcquired=0x%08x\n",
2578 (unsigned)w
, (unsigned)selectionWindow
, (unsigned)selectionAcquired
);
2580 if (selectionAcquired
&& (w
== selectionWindow
))
2582 CLIPBOARDINFO cbinfo
;
2584 /* completely give up the selection */
2585 TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
2587 X11DRV_CLIPBOARD_GetClipboardInfo(&cbinfo
);
2589 if (cbinfo
.flags
& CB_PROCESS
)
2591 /* Since we're still the owner, this wasn't initiated by
2592 another Wine process */
2593 if (OpenClipboard(hwnd
))
2595 /* Destroy private objects */
2596 SendMessageW(cbinfo
.hWndOwner
, WM_DESTROYCLIPBOARD
, 0, 0);
2598 /* Give up ownership of the windows clipboard */
2599 X11DRV_CLIPBOARD_ReleaseOwnership();
2604 if ((selType
== x11drv_atom(CLIPBOARD
)) && (selectionAcquired
& S_PRIMARY
))
2606 TRACE("Lost clipboard. Check if we need to release PRIMARY\n");
2609 if (selectionWindow
== XGetSelectionOwner(display
, XA_PRIMARY
))
2611 TRACE("We still own PRIMARY. Releasing PRIMARY.\n");
2612 XSetSelectionOwner(display
, XA_PRIMARY
, None
, time
);
2615 TRACE("We no longer own PRIMARY\n");
2616 wine_tsx11_unlock();
2618 else if ((selType
== XA_PRIMARY
) && (selectionAcquired
& S_CLIPBOARD
))
2620 TRACE("Lost PRIMARY. Check if we need to release CLIPBOARD\n");
2623 if (selectionWindow
== XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)))
2625 TRACE("We still own CLIPBOARD. Releasing CLIPBOARD.\n");
2626 XSetSelectionOwner(display
, x11drv_atom(CLIPBOARD
), None
, time
);
2629 TRACE("We no longer own CLIPBOARD\n");
2630 wine_tsx11_unlock();
2633 selectionWindow
= None
;
2635 X11DRV_EmptyClipboard(FALSE
);
2637 /* Reset the selection flags now that we are done */
2638 selectionAcquired
= S_NOSELECTION
;
2643 /**************************************************************************
2644 * IsSelectionOwner (X11DRV.@)
2646 * Returns: TRUE if the selection is owned by this process, FALSE otherwise
2648 static BOOL
X11DRV_CLIPBOARD_IsSelectionOwner(void)
2650 return selectionAcquired
;
2654 /**************************************************************************
2655 * X11DRV Clipboard Exports
2656 **************************************************************************/
2659 static void selection_acquire(void)
2664 owner
= thread_selection_wnd();
2665 display
= thread_display();
2669 selectionAcquired
= 0;
2670 selectionWindow
= 0;
2672 /* Grab PRIMARY selection if not owned */
2673 if (use_primary_selection
)
2674 XSetSelectionOwner(display
, XA_PRIMARY
, owner
, CurrentTime
);
2676 /* Grab CLIPBOARD selection if not owned */
2677 XSetSelectionOwner(display
, x11drv_atom(CLIPBOARD
), owner
, CurrentTime
);
2679 if (use_primary_selection
&& XGetSelectionOwner(display
, XA_PRIMARY
) == owner
)
2680 selectionAcquired
|= S_PRIMARY
;
2682 if (XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)) == owner
)
2683 selectionAcquired
|= S_CLIPBOARD
;
2685 wine_tsx11_unlock();
2687 if (selectionAcquired
)
2689 selectionWindow
= owner
;
2690 TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner
);
2694 static DWORD WINAPI
selection_thread_proc(LPVOID p
)
2700 selection_acquire();
2703 while (selectionAcquired
)
2705 MsgWaitForMultipleObjectsEx(0, NULL
, INFINITE
, QS_SENDMESSAGE
, 0);
2711 /**************************************************************************
2712 * AcquireClipboard (X11DRV.@)
2714 int CDECL
X11DRV_AcquireClipboard(HWND hWndClipWindow
)
2717 HANDLE selectionThread
;
2719 TRACE(" %p\n", hWndClipWindow
);
2722 * It's important that the selection get acquired from the thread
2723 * that owns the clipboard window. The primary reason is that we know
2724 * it is running a message loop and therefore can process the
2725 * X selection events.
2727 if (hWndClipWindow
&&
2728 GetCurrentThreadId() != GetWindowThreadProcessId(hWndClipWindow
, &procid
))
2730 if (procid
!= GetCurrentProcessId())
2732 WARN("Setting clipboard owner to other process is not supported\n");
2733 hWndClipWindow
= NULL
;
2737 TRACE("Thread %x is acquiring selection with thread %x's window %p\n",
2738 GetCurrentThreadId(),
2739 GetWindowThreadProcessId(hWndClipWindow
, NULL
), hWndClipWindow
);
2741 return SendMessageW(hWndClipWindow
, WM_X11DRV_ACQUIRE_SELECTION
, 0, 0);
2747 selection_acquire();
2751 HANDLE event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2752 selectionThread
= CreateThread(NULL
, 0, selection_thread_proc
, event
, 0, NULL
);
2754 if (!selectionThread
)
2756 WARN("Could not start clipboard thread\n");
2761 WaitForSingleObject(event
, INFINITE
);
2763 CloseHandle(selectionThread
);
2770 /**************************************************************************
2771 * X11DRV_EmptyClipboard
2773 * Empty cached clipboard data.
2775 void CDECL
X11DRV_EmptyClipboard(BOOL keepunowned
)
2777 WINE_CLIPDATA
*data
, *next
;
2779 LIST_FOR_EACH_ENTRY_SAFE( data
, next
, &data_list
, WINE_CLIPDATA
, entry
)
2781 if (keepunowned
&& (data
->wFlags
& CF_FLAG_UNOWNED
)) continue;
2782 list_remove( &data
->entry
);
2783 X11DRV_CLIPBOARD_FreeData( data
);
2784 HeapFree( GetProcessHeap(), 0, data
);
2788 TRACE(" %d entries remaining in cache.\n", ClipDataCount
);
2793 /**************************************************************************
2794 * X11DRV_SetClipboardData
2796 BOOL CDECL
X11DRV_SetClipboardData(UINT wFormat
, HANDLE hData
, BOOL owner
)
2799 BOOL bResult
= TRUE
;
2801 /* If it's not owned, data can only be set if the format data is not already owned
2802 and its rendering is not delayed */
2805 CLIPBOARDINFO cbinfo
;
2806 LPWINE_CLIPDATA lpRender
;
2808 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2811 ((lpRender
= X11DRV_CLIPBOARD_LookupData(wFormat
)) &&
2812 !(lpRender
->wFlags
& CF_FLAG_UNOWNED
)))
2815 flags
= CF_FLAG_UNOWNED
;
2818 bResult
&= X11DRV_CLIPBOARD_InsertClipboardData(wFormat
, hData
, flags
, NULL
, TRUE
);
2824 /**************************************************************************
2825 * CountClipboardFormats
2827 INT CDECL
X11DRV_CountClipboardFormats(void)
2829 CLIPBOARDINFO cbinfo
;
2831 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2833 TRACE(" count=%d\n", ClipDataCount
);
2835 return ClipDataCount
;
2839 /**************************************************************************
2840 * X11DRV_EnumClipboardFormats
2842 UINT CDECL
X11DRV_EnumClipboardFormats(UINT wFormat
)
2844 CLIPBOARDINFO cbinfo
;
2845 struct list
*ptr
= NULL
;
2847 TRACE("(%04X)\n", wFormat
);
2849 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2853 ptr
= list_head( &data_list
);
2857 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(wFormat
);
2858 if (lpData
) ptr
= list_next( &data_list
, &lpData
->entry
);
2862 return LIST_ENTRY( ptr
, WINE_CLIPDATA
, entry
)->wFormatID
;
2866 /**************************************************************************
2867 * X11DRV_IsClipboardFormatAvailable
2869 BOOL CDECL
X11DRV_IsClipboardFormatAvailable(UINT wFormat
)
2872 CLIPBOARDINFO cbinfo
;
2874 TRACE("(%04X)\n", wFormat
);
2876 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2878 if (wFormat
!= 0 && X11DRV_CLIPBOARD_LookupData(wFormat
))
2881 TRACE("(%04X)- ret(%d)\n", wFormat
, bRet
);
2887 /**************************************************************************
2888 * GetClipboardData (USER.142)
2890 HANDLE CDECL
X11DRV_GetClipboardData(UINT wFormat
)
2892 CLIPBOARDINFO cbinfo
;
2893 LPWINE_CLIPDATA lpRender
;
2895 TRACE("(%04X)\n", wFormat
);
2897 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2899 if ((lpRender
= X11DRV_CLIPBOARD_LookupData(wFormat
)))
2901 if ( !lpRender
->hData
)
2902 X11DRV_CLIPBOARD_RenderFormat(thread_init_display(), lpRender
);
2904 TRACE(" returning %p (type %04x)\n", lpRender
->hData
, lpRender
->wFormatID
);
2905 return lpRender
->hData
;
2912 /**************************************************************************
2913 * ResetSelectionOwner
2915 * Called when the thread owning the selection is destroyed and we need to
2916 * preserve the selection ownership. We look for another top level window
2917 * in this process and send it a message to acquire the selection.
2919 void X11DRV_ResetSelectionOwner(void)
2926 if (!selectionAcquired
|| thread_selection_wnd() != selectionWindow
)
2929 selectionAcquired
= S_NOSELECTION
;
2930 selectionWindow
= 0;
2932 hwnd
= GetWindow(GetDesktopWindow(), GW_CHILD
);
2935 if (GetCurrentThreadId() != GetWindowThreadProcessId(hwnd
, &procid
))
2937 if (GetCurrentProcessId() == procid
)
2939 if (SendMessageW(hwnd
, WM_X11DRV_ACQUIRE_SELECTION
, 0, 0))
2943 } while ((hwnd
= GetWindow(hwnd
, GW_HWNDNEXT
)) != NULL
);
2945 WARN("Failed to find another thread to take selection ownership. Clipboard data will be lost.\n");
2947 X11DRV_CLIPBOARD_ReleaseOwnership();
2948 X11DRV_EmptyClipboard(FALSE
);
2952 /**************************************************************************
2953 * X11DRV_CLIPBOARD_SynthesizeData
2955 static BOOL
X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID
)
2958 LPWINE_CLIPDATA lpSource
= NULL
;
2960 TRACE(" %04x\n", wFormatID
);
2962 /* Don't need to synthesize if it already exists */
2963 if (X11DRV_CLIPBOARD_LookupData(wFormatID
))
2966 if (wFormatID
== CF_UNICODETEXT
|| wFormatID
== CF_TEXT
|| wFormatID
== CF_OEMTEXT
)
2968 bsyn
= ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT
)) &&
2969 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) ||
2970 ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_TEXT
)) &&
2971 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) ||
2972 ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT
)) &&
2973 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
);
2975 else if (wFormatID
== CF_ENHMETAFILE
)
2977 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT
)) &&
2978 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2980 else if (wFormatID
== CF_METAFILEPICT
)
2982 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_ENHMETAFILE
)) &&
2983 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2985 else if (wFormatID
== CF_DIB
)
2987 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) &&
2988 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2990 else if (wFormatID
== CF_BITMAP
)
2992 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) &&
2993 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2997 X11DRV_CLIPBOARD_InsertClipboardData(wFormatID
, 0, CF_FLAG_SYNTHESIZED
, NULL
, TRUE
);
3004 /**************************************************************************
3005 * X11DRV_EndClipboardUpdate
3007 * Add locale if it hasn't already been added
3009 void CDECL
X11DRV_EndClipboardUpdate(void)
3011 INT count
= ClipDataCount
;
3013 /* Do Unicode <-> Text <-> OEM mapping */
3014 X11DRV_CLIPBOARD_SynthesizeData(CF_TEXT
);
3015 X11DRV_CLIPBOARD_SynthesizeData(CF_OEMTEXT
);
3016 X11DRV_CLIPBOARD_SynthesizeData(CF_UNICODETEXT
);
3018 /* Enhmetafile <-> MetafilePict mapping */
3019 X11DRV_CLIPBOARD_SynthesizeData(CF_ENHMETAFILE
);
3020 X11DRV_CLIPBOARD_SynthesizeData(CF_METAFILEPICT
);
3022 /* DIB <-> Bitmap mapping */
3023 X11DRV_CLIPBOARD_SynthesizeData(CF_DIB
);
3024 X11DRV_CLIPBOARD_SynthesizeData(CF_BITMAP
);
3026 TRACE("%d formats added to cached data\n", ClipDataCount
- count
);
3030 /***********************************************************************
3031 * X11DRV_SelectionRequest_TARGETS
3032 * Service a TARGETS selection request event
3034 static Atom
X11DRV_SelectionRequest_TARGETS( Display
*display
, Window requestor
,
3035 Atom target
, Atom rprop
)
3040 LPWINE_CLIPFORMAT format
;
3041 LPWINE_CLIPDATA lpData
;
3043 /* Create X atoms for any clipboard types which don't have atoms yet.
3044 * This avoids sending bogus zero atoms.
3045 * Without this, copying might not have access to all clipboard types.
3046 * FIXME: is it safe to call this here?
3051 * Count the number of items we wish to expose as selection targets.
3053 cTargets
= 1; /* Include TARGETS */
3055 if (!list_head( &data_list
)) return None
;
3057 LIST_FOR_EACH_ENTRY( lpData
, &data_list
, WINE_CLIPDATA
, entry
)
3058 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
3059 if ((format
->wFormatID
== lpData
->wFormatID
) &&
3060 format
->lpDrvExportFunc
&& format
->drvData
)
3063 TRACE(" found %d formats\n", cTargets
);
3065 /* Allocate temp buffer */
3066 targets
= HeapAlloc( GetProcessHeap(), 0, cTargets
* sizeof(Atom
));
3071 targets
[i
++] = x11drv_atom(TARGETS
);
3073 LIST_FOR_EACH_ENTRY( lpData
, &data_list
, WINE_CLIPDATA
, entry
)
3074 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
3075 if ((format
->wFormatID
== lpData
->wFormatID
) &&
3076 format
->lpDrvExportFunc
&& format
->drvData
)
3077 targets
[i
++] = format
->drvData
;
3081 if (TRACE_ON(clipboard
))
3084 for ( i
= 0; i
< cTargets
; i
++)
3086 char *itemFmtName
= XGetAtomName(display
, targets
[i
]);
3087 TRACE("\tAtom# %d: Property %ld Type %s\n", i
, targets
[i
], itemFmtName
);
3092 /* We may want to consider setting the type to xaTargets instead,
3093 * in case some apps expect this instead of XA_ATOM */
3094 XChangeProperty(display
, requestor
, rprop
, XA_ATOM
, 32,
3095 PropModeReplace
, (unsigned char *)targets
, cTargets
);
3096 wine_tsx11_unlock();
3098 HeapFree(GetProcessHeap(), 0, targets
);
3104 /***********************************************************************
3105 * X11DRV_SelectionRequest_MULTIPLE
3106 * Service a MULTIPLE selection request event
3107 * rprop contains a list of (target,property) atom pairs.
3108 * The first atom names a target and the second names a property.
3109 * The effect is as if we have received a sequence of SelectionRequest events
3110 * (one for each atom pair) except that:
3111 * 1. We reply with a SelectionNotify only when all the requested conversions
3112 * have been performed.
3113 * 2. If we fail to convert the target named by an atom in the MULTIPLE property,
3114 * we replace the atom in the property by None.
3116 static Atom
X11DRV_SelectionRequest_MULTIPLE( HWND hWnd
, XSelectionRequestEvent
*pevent
)
3118 Display
*display
= pevent
->display
;
3120 Atom atype
=AnyPropertyType
;
3122 unsigned long remain
;
3123 Atom
* targetPropList
=NULL
;
3124 unsigned long cTargetPropList
= 0;
3126 /* If the specified property is None the requestor is an obsolete client.
3127 * We support these by using the specified target atom as the reply property.
3129 rprop
= pevent
->property
;
3131 rprop
= pevent
->target
;
3135 /* Read the MULTIPLE property contents. This should contain a list of
3136 * (target,property) atom pairs.
3139 if(XGetWindowProperty(display
, pevent
->requestor
, rprop
,
3140 0, 0x3FFF, False
, AnyPropertyType
, &atype
,&aformat
,
3141 &cTargetPropList
, &remain
,
3142 (unsigned char**)&targetPropList
) != Success
)
3144 wine_tsx11_unlock();
3145 TRACE("\tCouldn't read MULTIPLE property\n");
3149 if (TRACE_ON(clipboard
))
3151 char * const typeName
= XGetAtomName(display
, atype
);
3152 TRACE("\tType %s,Format %d,nItems %ld, Remain %ld\n",
3153 typeName
, aformat
, cTargetPropList
, remain
);
3156 wine_tsx11_unlock();
3159 * Make sure we got what we expect.
3160 * NOTE: According to the X-ICCCM Version 2.0 documentation the property sent
3161 * in a MULTIPLE selection request should be of type ATOM_PAIR.
3162 * However some X apps(such as XPaint) are not compliant with this and return
3163 * a user defined atom in atype when XGetWindowProperty is called.
3164 * The data *is* an atom pair but is not denoted as such.
3166 if(aformat
== 32 /* atype == xAtomPair */ )
3170 /* Iterate through the ATOM_PAIR list and execute a SelectionRequest
3171 * for each (target,property) pair */
3173 for (i
= 0; i
< cTargetPropList
; i
+=2)
3175 XSelectionRequestEvent event
;
3177 if (TRACE_ON(clipboard
))
3179 char *targetName
, *propName
;
3181 targetName
= XGetAtomName(display
, targetPropList
[i
]);
3182 propName
= XGetAtomName(display
, targetPropList
[i
+1]);
3183 TRACE("MULTIPLE(%d): Target='%s' Prop='%s'\n",
3184 i
/2, targetName
, propName
);
3187 wine_tsx11_unlock();
3190 /* We must have a non "None" property to service a MULTIPLE target atom */
3191 if ( !targetPropList
[i
+1] )
3193 TRACE("\tMULTIPLE(%d): Skipping target with empty property!\n", i
);
3197 /* Set up an XSelectionRequestEvent for this (target,property) pair */
3199 event
.target
= targetPropList
[i
];
3200 event
.property
= targetPropList
[i
+1];
3202 /* Fire a SelectionRequest, informing the handler that we are processing
3203 * a MULTIPLE selection request event.
3205 X11DRV_HandleSelectionRequest( hWnd
, &event
, TRUE
);
3209 /* Free the list of targets/properties */
3211 XFree(targetPropList
);
3212 wine_tsx11_unlock();
3219 /***********************************************************************
3220 * X11DRV_HandleSelectionRequest
3221 * Process an event selection request event.
3222 * The bIsMultiple flag is used to signal when EVENT_SelectionRequest is called
3223 * recursively while servicing a "MULTIPLE" selection target.
3225 * Note: We only receive this event when WINE owns the X selection
3227 static void X11DRV_HandleSelectionRequest( HWND hWnd
, XSelectionRequestEvent
*event
, BOOL bIsMultiple
)
3229 Display
*display
= event
->display
;
3230 XSelectionEvent result
;
3232 Window request
= event
->requestor
;
3237 * We can only handle the selection request if :
3238 * The selection is PRIMARY or CLIPBOARD, AND we can successfully open the clipboard.
3239 * Don't do these checks or open the clipboard while recursively processing MULTIPLE,
3240 * since this has been already done.
3244 if (((event
->selection
!= XA_PRIMARY
) && (event
->selection
!= x11drv_atom(CLIPBOARD
))))
3248 /* If the specified property is None the requestor is an obsolete client.
3249 * We support these by using the specified target atom as the reply property.
3251 rprop
= event
->property
;
3253 rprop
= event
->target
;
3255 if(event
->target
== x11drv_atom(TARGETS
)) /* Return a list of all supported targets */
3257 /* TARGETS selection request */
3258 rprop
= X11DRV_SelectionRequest_TARGETS( display
, request
, event
->target
, rprop
);
3260 else if(event
->target
== x11drv_atom(MULTIPLE
)) /* rprop contains a list of (target, property) atom pairs */
3262 /* MULTIPLE selection request */
3263 rprop
= X11DRV_SelectionRequest_MULTIPLE( hWnd
, event
);
3267 LPWINE_CLIPFORMAT lpFormat
= X11DRV_CLIPBOARD_LookupProperty(NULL
, event
->target
);
3269 if (lpFormat
&& lpFormat
->lpDrvExportFunc
)
3271 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(lpFormat
->wFormatID
);
3275 unsigned char* lpClipData
;
3277 HANDLE hClipData
= lpFormat
->lpDrvExportFunc(display
, request
, event
->target
,
3278 rprop
, lpData
, &cBytes
);
3280 if (hClipData
&& (lpClipData
= GlobalLock(hClipData
)))
3282 int mode
= PropModeReplace
;
3284 TRACE("\tUpdating property %s, %d bytes\n",
3285 debugstr_format(lpFormat
->wFormatID
), cBytes
);
3289 int nelements
= min(cBytes
, 65536);
3290 XChangeProperty(display
, request
, rprop
, event
->target
,
3291 8, mode
, lpClipData
, nelements
);
3292 mode
= PropModeAppend
;
3293 cBytes
-= nelements
;
3294 lpClipData
+= nelements
;
3295 } while (cBytes
> 0);
3296 wine_tsx11_unlock();
3298 GlobalUnlock(hClipData
);
3299 GlobalFree(hClipData
);
3307 * SelectionNotify should be sent only at the end of a MULTIPLE request
3311 result
.type
= SelectionNotify
;
3312 result
.display
= display
;
3313 result
.requestor
= request
;
3314 result
.selection
= event
->selection
;
3315 result
.property
= rprop
;
3316 result
.target
= event
->target
;
3317 result
.time
= event
->time
;
3318 TRACE("Sending SelectionNotify event...\n");
3320 XSendEvent(display
,event
->requestor
,False
,NoEventMask
,(XEvent
*)&result
);
3321 wine_tsx11_unlock();
3326 /***********************************************************************
3327 * X11DRV_SelectionRequest
3329 void X11DRV_SelectionRequest( HWND hWnd
, XEvent
*event
)
3331 X11DRV_HandleSelectionRequest( hWnd
, &event
->xselectionrequest
, FALSE
);
3335 /***********************************************************************
3336 * X11DRV_SelectionClear
3338 void X11DRV_SelectionClear( HWND hWnd
, XEvent
*xev
)
3340 XSelectionClearEvent
*event
= &xev
->xselectionclear
;
3341 if (event
->selection
== XA_PRIMARY
|| event
->selection
== x11drv_atom(CLIPBOARD
))
3342 X11DRV_CLIPBOARD_ReleaseSelection( event
->display
, event
->selection
,
3343 event
->window
, hWnd
, event
->time
);
3346 /***********************************************************************
3347 * X11DRV_Clipboard_Cleanup
3349 void X11DRV_Clipboard_Cleanup(void)
3351 selectionAcquired
= S_NOSELECTION
;
3353 X11DRV_EmptyClipboard(FALSE
);