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
);
267 w
= XCreateWindow(thread_data
->display
, root_window
, 0, 0, 1, 1, 0, screen_depth
,
268 InputOutput
, CopyFromParent
, CWEventMask
, &attr
);
270 thread_data
->selection_wnd
= w
;
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
))
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 memset( &vis
, 0, sizeof(vis
) );
1343 if (depth
== screen_depth
)
1345 vis
.visual
= visual
;
1346 vis
.visualid
= visual
->visualid
;
1347 vis
.class = visual
->class;
1348 vis
.red_mask
= visual
->red_mask
;
1349 vis
.green_mask
= visual
->green_mask
;
1350 vis
.blue_mask
= visual
->blue_mask
;
1352 else switch (pixmap_formats
[depth
]->bits_per_pixel
)
1358 case 16: /* assume R5G5B5 */
1359 vis
.red_mask
= 0x7c00;
1360 vis
.green_mask
= 0x03e0;
1361 vis
.blue_mask
= 0x001f;
1363 case 24: /* assume R8G8B8 */
1364 case 32: /* assume A8R8G8B8 */
1365 vis
.red_mask
= 0xff0000;
1366 vis
.green_mask
= 0x00ff00;
1367 vis
.blue_mask
= 0x0000ff;
1373 if (!get_pixmap_image( *pPixmap
, width
, height
, &vis
, info
, &bits
))
1375 DWORD info_size
= bitmap_info_size( info
, DIB_RGB_COLORS
);
1378 hClipData
= GlobalAlloc( GMEM_MOVEABLE
| GMEM_DDESHARE
,
1379 info_size
+ info
->bmiHeader
.biSizeImage
);
1382 ptr
= GlobalLock( hClipData
);
1383 memcpy( ptr
, info
, info_size
);
1384 memcpy( ptr
+ info_size
, bits
.ptr
, info
->bmiHeader
.biSizeImage
);
1385 GlobalUnlock( hClipData
);
1387 if (bits
.free
) bits
.free( &bits
);
1395 /**************************************************************************
1396 * X11DRV_CLIPBOARD_ImportImageBmp
1398 * Import image/bmp, converting the image to CF_DIB.
1400 static HANDLE
X11DRV_CLIPBOARD_ImportImageBmp(Display
*display
, Window w
, Atom prop
)
1403 unsigned long cbytes
;
1404 HANDLE hClipData
= 0;
1406 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1408 BITMAPFILEHEADER
*bfh
= (BITMAPFILEHEADER
*)lpdata
;
1410 if (cbytes
>= sizeof(BITMAPFILEHEADER
)+sizeof(BITMAPCOREHEADER
) &&
1411 bfh
->bfType
== 0x4d42 /* "BM" */)
1413 BITMAPINFO
*bmi
= (BITMAPINFO
*)(bfh
+1);
1418 hbmp
= CreateDIBitmap(
1422 lpdata
+bfh
->bfOffBits
,
1427 hClipData
= create_dib_from_bitmap( hbmp
);
1433 /* Free the retrieved property data */
1434 HeapFree(GetProcessHeap(), 0, lpdata
);
1441 /**************************************************************************
1442 * X11DRV_CLIPBOARD_ImportMetaFilePict
1444 * Import MetaFilePict.
1446 static HANDLE
X11DRV_CLIPBOARD_ImportMetaFilePict(Display
*display
, Window w
, Atom prop
)
1449 unsigned long cbytes
;
1450 HANDLE hClipData
= 0;
1452 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1455 hClipData
= X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT
, lpdata
, (LPDWORD
)&cbytes
, FALSE
);
1457 /* Free the retrieved property data */
1458 HeapFree(GetProcessHeap(), 0, lpdata
);
1465 /**************************************************************************
1466 * X11DRV_ImportEnhMetaFile
1468 * Import EnhMetaFile.
1470 static HANDLE
X11DRV_CLIPBOARD_ImportEnhMetaFile(Display
*display
, Window w
, Atom prop
)
1473 unsigned long cbytes
;
1474 HANDLE hClipData
= 0;
1476 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1479 hClipData
= X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE
, lpdata
, (LPDWORD
)&cbytes
, FALSE
);
1481 /* Free the retrieved property data */
1482 HeapFree(GetProcessHeap(), 0, lpdata
);
1489 /**************************************************************************
1490 * X11DRV_ImportClipbordaData
1492 * Generic import clipboard data routine.
1494 static HANDLE
X11DRV_CLIPBOARD_ImportClipboardData(Display
*display
, Window w
, Atom prop
)
1498 unsigned long cbytes
;
1499 HANDLE hClipData
= 0;
1501 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1505 /* Turn on the DDESHARE flag to enable shared 32 bit memory */
1506 hClipData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cbytes
);
1510 if ((lpClipData
= GlobalLock(hClipData
)))
1512 memcpy(lpClipData
, lpdata
, cbytes
);
1513 GlobalUnlock(hClipData
);
1517 GlobalFree(hClipData
);
1522 /* Free the retrieved property data */
1523 HeapFree(GetProcessHeap(), 0, lpdata
);
1530 /**************************************************************************
1531 X11DRV_CLIPBOARD_ExportClipboardData
1533 * Generic export clipboard data routine.
1535 static HANDLE
X11DRV_CLIPBOARD_ExportClipboardData(Display
*display
, Window requestor
, Atom aTarget
,
1536 Atom rprop
, LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1540 HANDLE hClipData
= 0;
1542 *lpBytes
= 0; /* Assume failure */
1544 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpData
))
1545 ERR("Failed to export %04x format\n", lpData
->wFormatID
);
1548 datasize
= GlobalSize(lpData
->hData
);
1550 hClipData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, datasize
);
1551 if (hClipData
== 0) return NULL
;
1553 if ((lpClipData
= GlobalLock(hClipData
)))
1555 LPVOID lpdata
= GlobalLock(lpData
->hData
);
1557 memcpy(lpClipData
, lpdata
, datasize
);
1558 *lpBytes
= datasize
;
1560 GlobalUnlock(lpData
->hData
);
1561 GlobalUnlock(hClipData
);
1563 GlobalFree(hClipData
);
1572 /**************************************************************************
1573 * X11DRV_CLIPBOARD_ExportXAString
1575 * Export CF_TEXT converting the string to XA_STRING.
1576 * Helper function for X11DRV_CLIPBOARD_ExportString.
1578 static HANDLE
X11DRV_CLIPBOARD_ExportXAString(LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1582 LPSTR text
, lpstr
= NULL
;
1584 *lpBytes
= 0; /* Assume return has zero bytes */
1586 text
= GlobalLock(lpData
->hData
);
1587 size
= strlen(text
);
1589 /* remove carriage returns */
1590 lpstr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
+ 1);
1594 for (i
= 0,j
= 0; i
< size
&& text
[i
]; i
++)
1596 if (text
[i
] == '\r' && (text
[i
+1] == '\n' || text
[i
+1] == '\0'))
1598 lpstr
[j
++] = text
[i
];
1602 *lpBytes
= j
; /* Number of bytes in string */
1605 GlobalUnlock(lpData
->hData
);
1611 /**************************************************************************
1612 * X11DRV_CLIPBOARD_ExportUTF8String
1614 * Export CF_UNICODE converting the string to UTF8.
1615 * Helper function for X11DRV_CLIPBOARD_ExportString.
1617 static HANDLE
X11DRV_CLIPBOARD_ExportUTF8String(LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1622 LPSTR text
, lpstr
= NULL
;
1624 *lpBytes
= 0; /* Assume return has zero bytes */
1626 uni_text
= GlobalLock(lpData
->hData
);
1628 size
= WideCharToMultiByte(CP_UTF8
, 0, uni_text
, -1, NULL
, 0, NULL
, NULL
);
1630 text
= HeapAlloc(GetProcessHeap(), 0, size
);
1633 WideCharToMultiByte(CP_UTF8
, 0, uni_text
, -1, text
, size
, NULL
, NULL
);
1635 /* remove carriage returns */
1636 lpstr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
--);
1640 for (i
= 0,j
= 0; i
< size
&& text
[i
]; i
++)
1642 if (text
[i
] == '\r' && (text
[i
+1] == '\n' || text
[i
+1] == '\0'))
1644 lpstr
[j
++] = text
[i
];
1648 *lpBytes
= j
; /* Number of bytes in string */
1651 HeapFree(GetProcessHeap(), 0, text
);
1652 GlobalUnlock(lpData
->hData
);
1659 /**************************************************************************
1660 * X11DRV_CLIPBOARD_ExportCompoundText
1662 * Export CF_UNICODE to COMPOUND_TEXT
1663 * Helper function for X11DRV_CLIPBOARD_ExportString.
1665 static HANDLE
X11DRV_CLIPBOARD_ExportCompoundText(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1666 LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1670 XICCEncodingStyle style
;
1675 uni_text
= GlobalLock(lpData
->hData
);
1677 size
= WideCharToMultiByte(CP_UNIXCP
, 0, uni_text
, -1, NULL
, 0, NULL
, NULL
);
1678 lpstr
= HeapAlloc(GetProcessHeap(), 0, size
);
1682 WideCharToMultiByte(CP_UNIXCP
, 0, uni_text
, -1, lpstr
, size
, NULL
, NULL
);
1684 /* remove carriage returns */
1685 for (i
= 0, j
= 0; i
< size
&& lpstr
[i
]; i
++)
1687 if (lpstr
[i
] == '\r' && (lpstr
[i
+1] == '\n' || lpstr
[i
+1] == '\0'))
1689 lpstr
[j
++] = lpstr
[i
];
1693 GlobalUnlock(lpData
->hData
);
1695 if (aTarget
== x11drv_atom(COMPOUND_TEXT
))
1696 style
= XCompoundTextStyle
;
1698 style
= XStdICCTextStyle
;
1700 /* Update the X property */
1701 if (XmbTextListToTextProperty(display
, &lpstr
, 1, style
, &prop
) == Success
)
1703 XSetTextProperty(display
, requestor
, &prop
, rprop
);
1707 HeapFree(GetProcessHeap(), 0, lpstr
);
1712 /**************************************************************************
1713 * X11DRV_CLIPBOARD_ExportString
1717 static HANDLE
X11DRV_CLIPBOARD_ExportString(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1718 LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1720 if (X11DRV_CLIPBOARD_RenderFormat(display
, lpData
))
1722 if (aTarget
== XA_STRING
)
1723 return X11DRV_CLIPBOARD_ExportXAString(lpData
, lpBytes
);
1724 else if (aTarget
== x11drv_atom(COMPOUND_TEXT
) || aTarget
== x11drv_atom(TEXT
))
1725 return X11DRV_CLIPBOARD_ExportCompoundText(display
, requestor
, aTarget
,
1726 rprop
, lpData
, lpBytes
);
1729 TRACE("Exporting target %ld to default UTF8_STRING\n", aTarget
);
1730 return X11DRV_CLIPBOARD_ExportUTF8String(lpData
, lpBytes
);
1734 ERR("Failed to render %04x format\n", lpData
->wFormatID
);
1740 /**************************************************************************
1741 * X11DRV_CLIPBOARD_ExportXAPIXMAP
1743 * Export CF_DIB to XA_PIXMAP.
1745 static HANDLE
X11DRV_CLIPBOARD_ExportXAPIXMAP(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1746 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1749 unsigned char* lpData
;
1751 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1753 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1757 if (!lpdata
->drvData
) /* If not already rendered */
1762 struct gdi_image_bits bits
;
1764 memset( &vis
, 0, sizeof(vis
) );
1765 vis
.visual
= visual
;
1766 vis
.depth
= screen_depth
;
1767 vis
.visualid
= visual
->visualid
;
1768 vis
.class = visual
->class;
1769 vis
.red_mask
= visual
->red_mask
;
1770 vis
.green_mask
= visual
->green_mask
;
1771 vis
.blue_mask
= visual
->blue_mask
;
1773 pbmi
= GlobalLock( lpdata
->hData
);
1774 bits
.ptr
= (LPBYTE
)pbmi
+ bitmap_info_size( pbmi
, DIB_RGB_COLORS
);
1776 bits
.is_copy
= FALSE
;
1777 pixmap
= create_pixmap_from_image( 0, &vis
, pbmi
, &bits
, DIB_RGB_COLORS
);
1778 GlobalUnlock( lpdata
->hData
);
1779 lpdata
->drvData
= pixmap
;
1782 *lpBytes
= sizeof(Pixmap
); /* pixmap is a 32bit value */
1784 /* Wrap pixmap so we can return a handle */
1785 hData
= GlobalAlloc(0, *lpBytes
);
1786 lpData
= GlobalLock(hData
);
1787 memcpy(lpData
, &lpdata
->drvData
, *lpBytes
);
1788 GlobalUnlock(hData
);
1794 /**************************************************************************
1795 * X11DRV_CLIPBOARD_ExportImageBmp
1797 * Export CF_DIB to image/bmp.
1799 static HANDLE
X11DRV_CLIPBOARD_ExportImageBmp(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1800 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1807 BITMAPFILEHEADER
*bfh
;
1811 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1813 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1817 hpackeddib
= lpdata
->hData
;
1819 dibdata
= GlobalLock(hpackeddib
);
1822 ERR("Failed to lock packed DIB\n");
1826 bmpsize
= sizeof(BITMAPFILEHEADER
) + GlobalSize(hpackeddib
);
1828 hbmpdata
= GlobalAlloc(0, bmpsize
);
1832 bmpdata
= GlobalLock(hbmpdata
);
1836 GlobalFree(hbmpdata
);
1837 GlobalUnlock(hpackeddib
);
1841 /* bitmap file header */
1842 bfh
= (BITMAPFILEHEADER
*)bmpdata
;
1843 bfh
->bfType
= 0x4d42; /* "BM" */
1844 bfh
->bfSize
= bmpsize
;
1845 bfh
->bfReserved1
= 0;
1846 bfh
->bfReserved2
= 0;
1847 bfh
->bfOffBits
= sizeof(BITMAPFILEHEADER
) + bitmap_info_size((BITMAPINFO
*)dibdata
, DIB_RGB_COLORS
);
1849 /* rest of bitmap is the same as the packed dib */
1850 memcpy(bfh
+1, dibdata
, bmpsize
-sizeof(BITMAPFILEHEADER
));
1854 GlobalUnlock(hbmpdata
);
1857 GlobalUnlock(hpackeddib
);
1863 /**************************************************************************
1864 * X11DRV_CLIPBOARD_ExportMetaFilePict
1866 * Export MetaFilePict.
1868 static HANDLE
X11DRV_CLIPBOARD_ExportMetaFilePict(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1869 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1871 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1873 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1877 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT
, lpdata
->hData
, lpBytes
, TRUE
);
1881 /**************************************************************************
1882 * X11DRV_CLIPBOARD_ExportEnhMetaFile
1884 * Export EnhMetaFile.
1886 static HANDLE
X11DRV_CLIPBOARD_ExportEnhMetaFile(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1887 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1889 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1891 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1895 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE
, lpdata
->hData
, lpBytes
, TRUE
);
1899 /**************************************************************************
1900 * get_html_description_field
1902 * Find the value of a field in an HTML Format description.
1904 static LPCSTR
get_html_description_field(LPCSTR data
, LPCSTR keyword
)
1908 while (pos
&& *pos
&& *pos
!= '<')
1910 if (memcmp(pos
, keyword
, strlen(keyword
)) == 0)
1911 return pos
+strlen(keyword
);
1913 pos
= strchr(pos
, '\n');
1921 /**************************************************************************
1922 * X11DRV_CLIPBOARD_ExportTextHtml
1924 * Export HTML Format to text/html.
1926 * FIXME: We should attempt to add an <a base> tag and convert windows paths.
1928 static HANDLE
X11DRV_CLIPBOARD_ExportTextHtml(Display
*display
, Window requestor
, Atom aTarget
,
1929 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1932 LPCSTR data
, field_value
;
1933 UINT fragmentstart
, fragmentend
, htmlsize
;
1934 HANDLE hhtmldata
=NULL
;
1939 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1941 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1945 hdata
= lpdata
->hData
;
1947 data
= GlobalLock(hdata
);
1950 ERR("Failed to lock HTML Format data\n");
1954 /* read the important fields */
1955 field_value
= get_html_description_field(data
, "StartFragment:");
1958 ERR("Couldn't find StartFragment value\n");
1961 fragmentstart
= atoi(field_value
);
1963 field_value
= get_html_description_field(data
, "EndFragment:");
1966 ERR("Couldn't find EndFragment value\n");
1969 fragmentend
= atoi(field_value
);
1971 /* export only the fragment */
1972 htmlsize
= fragmentend
- fragmentstart
+ 1;
1974 hhtmldata
= GlobalAlloc(0, htmlsize
);
1978 htmldata
= GlobalLock(hhtmldata
);
1982 GlobalFree(hhtmldata
);
1987 memcpy(htmldata
, &data
[fragmentstart
], fragmentend
-fragmentstart
);
1988 htmldata
[htmlsize
-1] = '\0';
1990 *lpBytes
= htmlsize
;
1992 GlobalUnlock(htmldata
);
1997 GlobalUnlock(hdata
);
2003 /**************************************************************************
2004 * X11DRV_CLIPBOARD_QueryTargets
2006 static BOOL
X11DRV_CLIPBOARD_QueryTargets(Display
*display
, Window w
, Atom selection
,
2007 Atom target
, XEvent
*xe
)
2011 XConvertSelection(display
, selection
, target
, x11drv_atom(SELECTION_DATA
), w
, CurrentTime
);
2014 * Wait until SelectionNotify is received
2016 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
2018 Bool res
= XCheckTypedWindowEvent(display
, w
, SelectionNotify
, xe
);
2019 if (res
&& xe
->xselection
.selection
== selection
) break;
2021 usleep(SELECTION_WAIT
);
2024 if (i
== SELECTION_RETRIES
)
2026 ERR("Timed out waiting for SelectionNotify event\n");
2029 /* Verify that the selection returned a valid TARGETS property */
2030 if ((xe
->xselection
.target
!= target
) || (xe
->xselection
.property
== None
))
2032 /* Selection owner failed to respond or we missed the SelectionNotify */
2033 WARN("Failed to retrieve TARGETS for selection %ld.\n", selection
);
2041 static int is_atom_error( Display
*display
, XErrorEvent
*event
, void *arg
)
2043 return (event
->error_code
== BadAtom
);
2046 /**************************************************************************
2047 * X11DRV_CLIPBOARD_InsertSelectionProperties
2049 * Mark properties available for future retrieval.
2051 static VOID
X11DRV_CLIPBOARD_InsertSelectionProperties(Display
*display
, Atom
* properties
, UINT count
)
2053 UINT i
, nb_atoms
= 0;
2056 /* Cache these formats in the clipboard cache */
2057 for (i
= 0; i
< count
; i
++)
2059 LPWINE_CLIPFORMAT lpFormat
= X11DRV_CLIPBOARD_LookupProperty(NULL
, properties
[i
]);
2063 /* We found at least one Window's format that mapps to the property.
2064 * Continue looking for more.
2066 * If more than one property map to a Window's format then we use the first
2067 * one and ignore the rest.
2071 TRACE("Atom#%d Property(%d): --> Format %s\n",
2072 i
, lpFormat
->drvData
, debugstr_format(lpFormat
->wFormatID
));
2073 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat
->wFormatID
, 0, 0, lpFormat
, FALSE
);
2074 lpFormat
= X11DRV_CLIPBOARD_LookupProperty(lpFormat
, properties
[i
]);
2077 else if (properties
[i
])
2079 /* add it to the list of atoms that we don't know about yet */
2080 if (!atoms
) atoms
= HeapAlloc( GetProcessHeap(), 0,
2081 (count
- i
) * sizeof(*atoms
) );
2082 if (atoms
) atoms
[nb_atoms
++] = properties
[i
];
2086 /* query all unknown atoms in one go */
2089 char **names
= HeapAlloc( GetProcessHeap(), 0, nb_atoms
* sizeof(*names
) );
2092 X11DRV_expect_error( display
, is_atom_error
, NULL
);
2093 if (!XGetAtomNames( display
, atoms
, nb_atoms
, names
)) nb_atoms
= 0;
2094 if (X11DRV_check_error())
2096 WARN( "got some bad atoms, ignoring\n" );
2099 for (i
= 0; i
< nb_atoms
; i
++)
2101 WINE_CLIPFORMAT
*lpFormat
;
2103 int len
= MultiByteToWideChar(CP_UNIXCP
, 0, names
[i
], -1, NULL
, 0);
2104 wname
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
2105 MultiByteToWideChar(CP_UNIXCP
, 0, names
[i
], -1, wname
, len
);
2107 lpFormat
= register_format( RegisterClipboardFormatW(wname
), atoms
[i
] );
2108 HeapFree(GetProcessHeap(), 0, wname
);
2111 ERR("Failed to register %s property. Type will not be cached.\n", names
[i
]);
2114 TRACE("Atom#%d Property(%d): --> Format %s\n",
2115 i
, lpFormat
->drvData
, debugstr_format(lpFormat
->wFormatID
));
2116 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat
->wFormatID
, 0, 0, lpFormat
, FALSE
);
2118 for (i
= 0; i
< nb_atoms
; i
++) XFree( names
[i
] );
2119 HeapFree( GetProcessHeap(), 0, names
);
2121 HeapFree( GetProcessHeap(), 0, atoms
);
2126 /**************************************************************************
2127 * X11DRV_CLIPBOARD_QueryAvailableData
2129 * Caches the list of data formats available from the current selection.
2130 * This queries the selection owner for the TARGETS property and saves all
2131 * reported property types.
2133 static int X11DRV_CLIPBOARD_QueryAvailableData(Display
*display
, LPCLIPBOARDINFO lpcbinfo
)
2136 Atom atype
=AnyPropertyType
;
2138 unsigned long remain
;
2139 Atom
* targetList
=NULL
;
2141 unsigned long cSelectionTargets
= 0;
2143 if (selectionAcquired
& (S_PRIMARY
| S_CLIPBOARD
))
2145 ERR("Received request to cache selection but process is owner=(%08x)\n",
2146 (unsigned) selectionWindow
);
2147 return -1; /* Prevent self request */
2150 w
= thread_selection_wnd();
2153 ERR("No window available to retrieve selection!\n");
2158 * Query the selection owner for the TARGETS property
2160 if ((use_primary_selection
&& XGetSelectionOwner(display
,XA_PRIMARY
)) ||
2161 XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)))
2163 if (use_primary_selection
&& (X11DRV_CLIPBOARD_QueryTargets(display
, w
, XA_PRIMARY
, x11drv_atom(TARGETS
), &xe
)))
2164 selectionCacheSrc
= XA_PRIMARY
;
2165 else if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, x11drv_atom(CLIPBOARD
), x11drv_atom(TARGETS
), &xe
))
2166 selectionCacheSrc
= x11drv_atom(CLIPBOARD
);
2169 Atom xstr
= XA_STRING
;
2171 /* Selection Owner doesn't understand TARGETS, try retrieving XA_STRING */
2172 if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, XA_PRIMARY
, XA_STRING
, &xe
))
2174 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, &xstr
, 1);
2175 selectionCacheSrc
= XA_PRIMARY
;
2178 else if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, x11drv_atom(CLIPBOARD
), XA_STRING
, &xe
))
2180 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, &xstr
, 1);
2181 selectionCacheSrc
= x11drv_atom(CLIPBOARD
);
2186 WARN("Failed to query selection owner for available data.\n");
2191 else return 0; /* No selection owner so report 0 targets available */
2193 /* Read the TARGETS property contents */
2194 if (!XGetWindowProperty(display
, xe
.xselection
.requestor
, xe
.xselection
.property
,
2195 0, 0x3FFF, True
, AnyPropertyType
/*XA_ATOM*/, &atype
, &aformat
, &cSelectionTargets
,
2196 &remain
, (unsigned char**)&targetList
) != Success
)
2198 TRACE("Type %lx,Format %d,nItems %ld, Remain %ld\n",
2199 atype
, aformat
, cSelectionTargets
, remain
);
2201 * The TARGETS property should have returned us a list of atoms
2202 * corresponding to each selection target format supported.
2204 if (atype
== XA_ATOM
|| atype
== x11drv_atom(TARGETS
))
2208 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, targetList
, cSelectionTargets
);
2210 else if (aformat
== 8) /* work around quartz-wm brain damage */
2212 unsigned long i
, count
= cSelectionTargets
/ sizeof(CARD32
);
2213 Atom
*atoms
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(Atom
) );
2214 for (i
= 0; i
< count
; i
++)
2215 atoms
[i
] = ((CARD32
*)targetList
)[i
]; /* FIXME: byte swapping */
2216 X11DRV_CLIPBOARD_InsertSelectionProperties( display
, atoms
, count
);
2217 HeapFree( GetProcessHeap(), 0, atoms
);
2221 /* Free the list of targets */
2224 else WARN("Failed to read TARGETS property\n");
2226 return cSelectionTargets
;
2230 /**************************************************************************
2231 * X11DRV_CLIPBOARD_ReadSelectionData
2233 * This method is invoked only when we DO NOT own the X selection
2235 * We always get the data from the selection client each time,
2236 * since we have no way of determining if the data in our cache is stale.
2238 static BOOL
X11DRV_CLIPBOARD_ReadSelectionData(Display
*display
, LPWINE_CLIPDATA lpData
)
2245 TRACE("%04x\n", lpData
->wFormatID
);
2247 if (!lpData
->lpFormat
)
2249 ERR("Requesting format %04x but no source format linked to data.\n",
2254 if (!selectionAcquired
)
2256 Window w
= thread_selection_wnd();
2259 ERR("No window available to read selection data!\n");
2263 TRACE("Requesting conversion of %s property (%d) from selection type %08x\n",
2264 debugstr_format(lpData
->lpFormat
->wFormatID
), lpData
->lpFormat
->drvData
,
2265 (UINT
)selectionCacheSrc
);
2267 XConvertSelection(display
, selectionCacheSrc
, lpData
->lpFormat
->drvData
,
2268 x11drv_atom(SELECTION_DATA
), w
, CurrentTime
);
2270 /* wait until SelectionNotify is received */
2271 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
2273 res
= XCheckTypedWindowEvent(display
, w
, SelectionNotify
, &xe
);
2274 if (res
&& xe
.xselection
.selection
== selectionCacheSrc
) break;
2276 usleep(SELECTION_WAIT
);
2279 if (i
== SELECTION_RETRIES
)
2281 ERR("Timed out waiting for SelectionNotify event\n");
2283 /* Verify that the selection returned a valid TARGETS property */
2284 else if (xe
.xselection
.property
!= None
)
2287 * Read the contents of the X selection property
2288 * into WINE's clipboard cache and converting the
2289 * data format if necessary.
2291 HANDLE hData
= lpData
->lpFormat
->lpDrvImportFunc(display
, xe
.xselection
.requestor
,
2292 xe
.xselection
.property
);
2295 bRet
= X11DRV_CLIPBOARD_InsertClipboardData(lpData
->wFormatID
, hData
, 0, lpData
->lpFormat
, TRUE
);
2297 TRACE("Import function failed\n");
2301 TRACE("Failed to convert selection\n");
2306 ERR("Received request to cache selection data but process is owner\n");
2309 TRACE("Returning %d\n", bRet
);
2315 /**************************************************************************
2316 * X11DRV_CLIPBOARD_GetProperty
2317 * Gets type, data and size.
2319 static BOOL
X11DRV_CLIPBOARD_GetProperty(Display
*display
, Window w
, Atom prop
,
2320 Atom
*atype
, unsigned char** data
, unsigned long* datasize
)
2323 unsigned long pos
= 0, nitems
, remain
, count
;
2324 unsigned char *val
= NULL
, *buffer
;
2326 TRACE("Reading property %lu from X window %lx\n", prop
, w
);
2330 if (XGetWindowProperty(display
, w
, prop
, pos
, INT_MAX
/ 4, False
,
2331 AnyPropertyType
, atype
, &aformat
, &nitems
, &remain
, &buffer
) != Success
)
2333 WARN("Failed to read property\n");
2334 HeapFree( GetProcessHeap(), 0, val
);
2338 count
= get_property_size( aformat
, nitems
);
2339 if (!val
) *data
= HeapAlloc( GetProcessHeap(), 0, pos
* sizeof(int) + count
+ 1 );
2340 else *data
= HeapReAlloc( GetProcessHeap(), 0, val
, pos
* sizeof(int) + count
+ 1 );
2345 HeapFree( GetProcessHeap(), 0, val
);
2349 memcpy( (int *)val
+ pos
, buffer
, count
);
2353 *datasize
= pos
* sizeof(int) + count
;
2357 pos
+= count
/ sizeof(int);
2360 /* Delete the property on the window now that we are done
2361 * This will send a PropertyNotify event to the selection owner. */
2362 XDeleteProperty(display
, w
, prop
);
2367 /**************************************************************************
2368 * X11DRV_CLIPBOARD_ReadProperty
2369 * Reads the contents of the X selection property.
2371 static BOOL
X11DRV_CLIPBOARD_ReadProperty(Display
*display
, Window w
, Atom prop
,
2372 unsigned char** data
, unsigned long* datasize
)
2380 if (!X11DRV_CLIPBOARD_GetProperty(display
, w
, prop
, &atype
, data
, datasize
))
2383 while (XCheckTypedWindowEvent(display
, w
, PropertyNotify
, &xe
))
2386 if (atype
== x11drv_atom(INCR
))
2388 unsigned char *buf
= *data
;
2389 unsigned long bufsize
= 0;
2394 unsigned char *prop_data
, *tmp
;
2395 unsigned long prop_size
;
2397 /* Wait until PropertyNotify is received */
2398 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
2402 res
= XCheckTypedWindowEvent(display
, w
, PropertyNotify
, &xe
);
2403 if (res
&& xe
.xproperty
.atom
== prop
&&
2404 xe
.xproperty
.state
== PropertyNewValue
)
2406 usleep(SELECTION_WAIT
);
2409 if (i
>= SELECTION_RETRIES
||
2410 !X11DRV_CLIPBOARD_GetProperty(display
, w
, prop
, &atype
, &prop_data
, &prop_size
))
2412 HeapFree(GetProcessHeap(), 0, buf
);
2416 /* Retrieved entire data. */
2419 HeapFree(GetProcessHeap(), 0, prop_data
);
2421 *datasize
= bufsize
;
2425 tmp
= HeapReAlloc(GetProcessHeap(), 0, buf
, bufsize
+ prop_size
+ 1);
2428 HeapFree(GetProcessHeap(), 0, buf
);
2433 memcpy(buf
+ bufsize
, prop_data
, prop_size
+ 1);
2434 bufsize
+= prop_size
;
2435 HeapFree(GetProcessHeap(), 0, prop_data
);
2443 /**************************************************************************
2444 * CLIPBOARD_SerializeMetafile
2446 static HANDLE
X11DRV_CLIPBOARD_SerializeMetafile(INT wformat
, HANDLE hdata
, LPDWORD lpcbytes
, BOOL out
)
2450 TRACE(" wFormat=%d hdata=%p out=%d\n", wformat
, hdata
, out
);
2452 if (out
) /* Serialize out, caller should free memory */
2454 *lpcbytes
= 0; /* Assume failure */
2456 if (wformat
== CF_METAFILEPICT
)
2458 LPMETAFILEPICT lpmfp
= GlobalLock(hdata
);
2459 unsigned int size
= GetMetaFileBitsEx(lpmfp
->hMF
, 0, NULL
);
2461 h
= GlobalAlloc(0, size
+ sizeof(METAFILEPICT
));
2464 char *pdata
= GlobalLock(h
);
2466 memcpy(pdata
, lpmfp
, sizeof(METAFILEPICT
));
2467 GetMetaFileBitsEx(lpmfp
->hMF
, size
, pdata
+ sizeof(METAFILEPICT
));
2469 *lpcbytes
= size
+ sizeof(METAFILEPICT
);
2474 GlobalUnlock(hdata
);
2476 else if (wformat
== CF_ENHMETAFILE
)
2478 int size
= GetEnhMetaFileBits(hdata
, 0, NULL
);
2480 h
= GlobalAlloc(0, size
);
2483 LPVOID pdata
= GlobalLock(h
);
2485 GetEnhMetaFileBits(hdata
, size
, pdata
);
2494 if (wformat
== CF_METAFILEPICT
)
2496 h
= GlobalAlloc(0, sizeof(METAFILEPICT
));
2499 unsigned int wiresize
;
2500 LPMETAFILEPICT lpmfp
= GlobalLock(h
);
2502 memcpy(lpmfp
, hdata
, sizeof(METAFILEPICT
));
2503 wiresize
= *lpcbytes
- sizeof(METAFILEPICT
);
2504 lpmfp
->hMF
= SetMetaFileBitsEx(wiresize
,
2505 ((const BYTE
*)hdata
) + sizeof(METAFILEPICT
));
2509 else if (wformat
== CF_ENHMETAFILE
)
2511 h
= SetEnhMetaFileBits(*lpcbytes
, hdata
);
2519 /**************************************************************************
2520 * X11DRV_CLIPBOARD_ReleaseSelection
2522 * Release XA_CLIPBOARD and XA_PRIMARY in response to a SelectionClear event.
2524 static void X11DRV_CLIPBOARD_ReleaseSelection(Display
*display
, Atom selType
, Window w
, HWND hwnd
, Time time
)
2526 /* w is the window that lost the selection
2528 TRACE("event->window = %08x (selectionWindow = %08x) selectionAcquired=0x%08x\n",
2529 (unsigned)w
, (unsigned)selectionWindow
, (unsigned)selectionAcquired
);
2531 if (selectionAcquired
&& (w
== selectionWindow
))
2533 CLIPBOARDINFO cbinfo
;
2535 /* completely give up the selection */
2536 TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
2538 X11DRV_CLIPBOARD_GetClipboardInfo(&cbinfo
);
2540 if (cbinfo
.flags
& CB_PROCESS
)
2542 /* Since we're still the owner, this wasn't initiated by
2543 another Wine process */
2544 if (OpenClipboard(hwnd
))
2546 /* Destroy private objects */
2547 SendMessageW(cbinfo
.hWndOwner
, WM_DESTROYCLIPBOARD
, 0, 0);
2549 /* Give up ownership of the windows clipboard */
2550 X11DRV_CLIPBOARD_ReleaseOwnership();
2555 if ((selType
== x11drv_atom(CLIPBOARD
)) && (selectionAcquired
& S_PRIMARY
))
2557 TRACE("Lost clipboard. Check if we need to release PRIMARY\n");
2559 if (selectionWindow
== XGetSelectionOwner(display
, XA_PRIMARY
))
2561 TRACE("We still own PRIMARY. Releasing PRIMARY.\n");
2562 XSetSelectionOwner(display
, XA_PRIMARY
, None
, time
);
2565 TRACE("We no longer own PRIMARY\n");
2567 else if ((selType
== XA_PRIMARY
) && (selectionAcquired
& S_CLIPBOARD
))
2569 TRACE("Lost PRIMARY. Check if we need to release CLIPBOARD\n");
2571 if (selectionWindow
== XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)))
2573 TRACE("We still own CLIPBOARD. Releasing CLIPBOARD.\n");
2574 XSetSelectionOwner(display
, x11drv_atom(CLIPBOARD
), None
, time
);
2577 TRACE("We no longer own CLIPBOARD\n");
2580 selectionWindow
= None
;
2582 X11DRV_EmptyClipboard(FALSE
);
2584 /* Reset the selection flags now that we are done */
2585 selectionAcquired
= S_NOSELECTION
;
2590 /**************************************************************************
2591 * IsSelectionOwner (X11DRV.@)
2593 * Returns: TRUE if the selection is owned by this process, FALSE otherwise
2595 static BOOL
X11DRV_CLIPBOARD_IsSelectionOwner(void)
2597 return selectionAcquired
;
2601 /**************************************************************************
2602 * X11DRV Clipboard Exports
2603 **************************************************************************/
2606 static void selection_acquire(void)
2611 owner
= thread_selection_wnd();
2612 display
= thread_display();
2614 selectionAcquired
= 0;
2615 selectionWindow
= 0;
2617 /* Grab PRIMARY selection if not owned */
2618 if (use_primary_selection
)
2619 XSetSelectionOwner(display
, XA_PRIMARY
, owner
, CurrentTime
);
2621 /* Grab CLIPBOARD selection if not owned */
2622 XSetSelectionOwner(display
, x11drv_atom(CLIPBOARD
), owner
, CurrentTime
);
2624 if (use_primary_selection
&& XGetSelectionOwner(display
, XA_PRIMARY
) == owner
)
2625 selectionAcquired
|= S_PRIMARY
;
2627 if (XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)) == owner
)
2628 selectionAcquired
|= S_CLIPBOARD
;
2630 if (selectionAcquired
)
2632 selectionWindow
= owner
;
2633 TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner
);
2637 static DWORD WINAPI
selection_thread_proc(LPVOID p
)
2643 selection_acquire();
2646 while (selectionAcquired
)
2648 MsgWaitForMultipleObjectsEx(0, NULL
, INFINITE
, QS_SENDMESSAGE
, 0);
2654 /**************************************************************************
2655 * AcquireClipboard (X11DRV.@)
2657 int CDECL
X11DRV_AcquireClipboard(HWND hWndClipWindow
)
2660 HANDLE selectionThread
;
2662 TRACE(" %p\n", hWndClipWindow
);
2665 * It's important that the selection get acquired from the thread
2666 * that owns the clipboard window. The primary reason is that we know
2667 * it is running a message loop and therefore can process the
2668 * X selection events.
2670 if (hWndClipWindow
&&
2671 GetCurrentThreadId() != GetWindowThreadProcessId(hWndClipWindow
, &procid
))
2673 if (procid
!= GetCurrentProcessId())
2675 WARN("Setting clipboard owner to other process is not supported\n");
2676 hWndClipWindow
= NULL
;
2680 TRACE("Thread %x is acquiring selection with thread %x's window %p\n",
2681 GetCurrentThreadId(),
2682 GetWindowThreadProcessId(hWndClipWindow
, NULL
), hWndClipWindow
);
2684 return SendMessageW(hWndClipWindow
, WM_X11DRV_ACQUIRE_SELECTION
, 0, 0);
2690 selection_acquire();
2694 HANDLE event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2695 selectionThread
= CreateThread(NULL
, 0, selection_thread_proc
, event
, 0, NULL
);
2697 if (!selectionThread
)
2699 WARN("Could not start clipboard thread\n");
2704 WaitForSingleObject(event
, INFINITE
);
2706 CloseHandle(selectionThread
);
2713 /**************************************************************************
2714 * X11DRV_EmptyClipboard
2716 * Empty cached clipboard data.
2718 void CDECL
X11DRV_EmptyClipboard(BOOL keepunowned
)
2720 WINE_CLIPDATA
*data
, *next
;
2722 LIST_FOR_EACH_ENTRY_SAFE( data
, next
, &data_list
, WINE_CLIPDATA
, entry
)
2724 if (keepunowned
&& (data
->wFlags
& CF_FLAG_UNOWNED
)) continue;
2725 list_remove( &data
->entry
);
2726 X11DRV_CLIPBOARD_FreeData( data
);
2727 HeapFree( GetProcessHeap(), 0, data
);
2731 TRACE(" %d entries remaining in cache.\n", ClipDataCount
);
2736 /**************************************************************************
2737 * X11DRV_SetClipboardData
2739 BOOL CDECL
X11DRV_SetClipboardData(UINT wFormat
, HANDLE hData
, BOOL owner
)
2742 BOOL bResult
= TRUE
;
2744 /* If it's not owned, data can only be set if the format data is not already owned
2745 and its rendering is not delayed */
2748 CLIPBOARDINFO cbinfo
;
2749 LPWINE_CLIPDATA lpRender
;
2751 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2754 ((lpRender
= X11DRV_CLIPBOARD_LookupData(wFormat
)) &&
2755 !(lpRender
->wFlags
& CF_FLAG_UNOWNED
)))
2758 flags
= CF_FLAG_UNOWNED
;
2761 bResult
&= X11DRV_CLIPBOARD_InsertClipboardData(wFormat
, hData
, flags
, NULL
, TRUE
);
2767 /**************************************************************************
2768 * CountClipboardFormats
2770 INT CDECL
X11DRV_CountClipboardFormats(void)
2772 CLIPBOARDINFO cbinfo
;
2774 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2776 TRACE(" count=%d\n", ClipDataCount
);
2778 return ClipDataCount
;
2782 /**************************************************************************
2783 * X11DRV_EnumClipboardFormats
2785 UINT CDECL
X11DRV_EnumClipboardFormats(UINT wFormat
)
2787 CLIPBOARDINFO cbinfo
;
2788 struct list
*ptr
= NULL
;
2790 TRACE("(%04X)\n", wFormat
);
2792 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2796 ptr
= list_head( &data_list
);
2800 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(wFormat
);
2801 if (lpData
) ptr
= list_next( &data_list
, &lpData
->entry
);
2805 return LIST_ENTRY( ptr
, WINE_CLIPDATA
, entry
)->wFormatID
;
2809 /**************************************************************************
2810 * X11DRV_IsClipboardFormatAvailable
2812 BOOL CDECL
X11DRV_IsClipboardFormatAvailable(UINT wFormat
)
2815 CLIPBOARDINFO cbinfo
;
2817 TRACE("(%04X)\n", wFormat
);
2819 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2821 if (wFormat
!= 0 && X11DRV_CLIPBOARD_LookupData(wFormat
))
2824 TRACE("(%04X)- ret(%d)\n", wFormat
, bRet
);
2830 /**************************************************************************
2831 * GetClipboardData (USER.142)
2833 HANDLE CDECL
X11DRV_GetClipboardData(UINT wFormat
)
2835 CLIPBOARDINFO cbinfo
;
2836 LPWINE_CLIPDATA lpRender
;
2838 TRACE("(%04X)\n", wFormat
);
2840 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
2842 if ((lpRender
= X11DRV_CLIPBOARD_LookupData(wFormat
)))
2844 if ( !lpRender
->hData
)
2845 X11DRV_CLIPBOARD_RenderFormat(thread_init_display(), lpRender
);
2847 TRACE(" returning %p (type %04x)\n", lpRender
->hData
, lpRender
->wFormatID
);
2848 return lpRender
->hData
;
2855 /**************************************************************************
2856 * ResetSelectionOwner
2858 * Called when the thread owning the selection is destroyed and we need to
2859 * preserve the selection ownership. We look for another top level window
2860 * in this process and send it a message to acquire the selection.
2862 void X11DRV_ResetSelectionOwner(void)
2869 if (!selectionAcquired
|| thread_selection_wnd() != selectionWindow
)
2872 selectionAcquired
= S_NOSELECTION
;
2873 selectionWindow
= 0;
2875 hwnd
= GetWindow(GetDesktopWindow(), GW_CHILD
);
2878 if (GetCurrentThreadId() != GetWindowThreadProcessId(hwnd
, &procid
))
2880 if (GetCurrentProcessId() == procid
)
2882 if (SendMessageW(hwnd
, WM_X11DRV_ACQUIRE_SELECTION
, 0, 0))
2886 } while ((hwnd
= GetWindow(hwnd
, GW_HWNDNEXT
)) != NULL
);
2888 WARN("Failed to find another thread to take selection ownership. Clipboard data will be lost.\n");
2890 X11DRV_CLIPBOARD_ReleaseOwnership();
2891 X11DRV_EmptyClipboard(FALSE
);
2895 /**************************************************************************
2896 * X11DRV_CLIPBOARD_SynthesizeData
2898 static BOOL
X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID
)
2901 LPWINE_CLIPDATA lpSource
= NULL
;
2903 TRACE(" %04x\n", wFormatID
);
2905 /* Don't need to synthesize if it already exists */
2906 if (X11DRV_CLIPBOARD_LookupData(wFormatID
))
2909 if (wFormatID
== CF_UNICODETEXT
|| wFormatID
== CF_TEXT
|| wFormatID
== CF_OEMTEXT
)
2911 bsyn
= ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT
)) &&
2912 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) ||
2913 ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_TEXT
)) &&
2914 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) ||
2915 ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT
)) &&
2916 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
);
2918 else if (wFormatID
== CF_ENHMETAFILE
)
2920 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT
)) &&
2921 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2923 else if (wFormatID
== CF_METAFILEPICT
)
2925 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_ENHMETAFILE
)) &&
2926 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2928 else if (wFormatID
== CF_DIB
)
2930 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) &&
2931 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2933 else if (wFormatID
== CF_BITMAP
)
2935 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) &&
2936 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
2940 X11DRV_CLIPBOARD_InsertClipboardData(wFormatID
, 0, CF_FLAG_SYNTHESIZED
, NULL
, TRUE
);
2947 /**************************************************************************
2948 * X11DRV_EndClipboardUpdate
2950 * Add locale if it hasn't already been added
2952 void CDECL
X11DRV_EndClipboardUpdate(void)
2954 INT count
= ClipDataCount
;
2956 /* Do Unicode <-> Text <-> OEM mapping */
2957 X11DRV_CLIPBOARD_SynthesizeData(CF_TEXT
);
2958 X11DRV_CLIPBOARD_SynthesizeData(CF_OEMTEXT
);
2959 X11DRV_CLIPBOARD_SynthesizeData(CF_UNICODETEXT
);
2961 /* Enhmetafile <-> MetafilePict mapping */
2962 X11DRV_CLIPBOARD_SynthesizeData(CF_ENHMETAFILE
);
2963 X11DRV_CLIPBOARD_SynthesizeData(CF_METAFILEPICT
);
2965 /* DIB <-> Bitmap mapping */
2966 X11DRV_CLIPBOARD_SynthesizeData(CF_DIB
);
2967 X11DRV_CLIPBOARD_SynthesizeData(CF_BITMAP
);
2969 TRACE("%d formats added to cached data\n", ClipDataCount
- count
);
2973 /***********************************************************************
2974 * X11DRV_SelectionRequest_TARGETS
2975 * Service a TARGETS selection request event
2977 static Atom
X11DRV_SelectionRequest_TARGETS( Display
*display
, Window requestor
,
2978 Atom target
, Atom rprop
)
2983 LPWINE_CLIPFORMAT format
;
2984 LPWINE_CLIPDATA lpData
;
2986 /* Create X atoms for any clipboard types which don't have atoms yet.
2987 * This avoids sending bogus zero atoms.
2988 * Without this, copying might not have access to all clipboard types.
2989 * FIXME: is it safe to call this here?
2994 * Count the number of items we wish to expose as selection targets.
2996 cTargets
= 1; /* Include TARGETS */
2998 if (!list_head( &data_list
)) return None
;
3000 LIST_FOR_EACH_ENTRY( lpData
, &data_list
, WINE_CLIPDATA
, entry
)
3001 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
3002 if ((format
->wFormatID
== lpData
->wFormatID
) &&
3003 format
->lpDrvExportFunc
&& format
->drvData
)
3006 TRACE(" found %d formats\n", cTargets
);
3008 /* Allocate temp buffer */
3009 targets
= HeapAlloc( GetProcessHeap(), 0, cTargets
* sizeof(Atom
));
3014 targets
[i
++] = x11drv_atom(TARGETS
);
3016 LIST_FOR_EACH_ENTRY( lpData
, &data_list
, WINE_CLIPDATA
, entry
)
3017 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
3018 if ((format
->wFormatID
== lpData
->wFormatID
) &&
3019 format
->lpDrvExportFunc
&& format
->drvData
)
3020 targets
[i
++] = format
->drvData
;
3022 if (TRACE_ON(clipboard
))
3025 for ( i
= 0; i
< cTargets
; i
++)
3027 char *itemFmtName
= XGetAtomName(display
, targets
[i
]);
3028 TRACE("\tAtom# %d: Property %ld Type %s\n", i
, targets
[i
], itemFmtName
);
3033 /* We may want to consider setting the type to xaTargets instead,
3034 * in case some apps expect this instead of XA_ATOM */
3035 XChangeProperty(display
, requestor
, rprop
, XA_ATOM
, 32,
3036 PropModeReplace
, (unsigned char *)targets
, cTargets
);
3038 HeapFree(GetProcessHeap(), 0, targets
);
3044 /***********************************************************************
3045 * X11DRV_SelectionRequest_MULTIPLE
3046 * Service a MULTIPLE selection request event
3047 * rprop contains a list of (target,property) atom pairs.
3048 * The first atom names a target and the second names a property.
3049 * The effect is as if we have received a sequence of SelectionRequest events
3050 * (one for each atom pair) except that:
3051 * 1. We reply with a SelectionNotify only when all the requested conversions
3052 * have been performed.
3053 * 2. If we fail to convert the target named by an atom in the MULTIPLE property,
3054 * we replace the atom in the property by None.
3056 static Atom
X11DRV_SelectionRequest_MULTIPLE( HWND hWnd
, XSelectionRequestEvent
*pevent
)
3058 Display
*display
= pevent
->display
;
3060 Atom atype
=AnyPropertyType
;
3062 unsigned long remain
;
3063 Atom
* targetPropList
=NULL
;
3064 unsigned long cTargetPropList
= 0;
3066 /* If the specified property is None the requestor is an obsolete client.
3067 * We support these by using the specified target atom as the reply property.
3069 rprop
= pevent
->property
;
3071 rprop
= pevent
->target
;
3075 /* Read the MULTIPLE property contents. This should contain a list of
3076 * (target,property) atom pairs.
3078 if (!XGetWindowProperty(display
, pevent
->requestor
, rprop
,
3079 0, 0x3FFF, False
, AnyPropertyType
, &atype
,&aformat
,
3080 &cTargetPropList
, &remain
,
3081 (unsigned char**)&targetPropList
) != Success
)
3083 if (TRACE_ON(clipboard
))
3085 char * const typeName
= XGetAtomName(display
, atype
);
3086 TRACE("\tType %s,Format %d,nItems %ld, Remain %ld\n",
3087 typeName
, aformat
, cTargetPropList
, remain
);
3092 * Make sure we got what we expect.
3093 * NOTE: According to the X-ICCCM Version 2.0 documentation the property sent
3094 * in a MULTIPLE selection request should be of type ATOM_PAIR.
3095 * However some X apps(such as XPaint) are not compliant with this and return
3096 * a user defined atom in atype when XGetWindowProperty is called.
3097 * The data *is* an atom pair but is not denoted as such.
3099 if(aformat
== 32 /* atype == xAtomPair */ )
3103 /* Iterate through the ATOM_PAIR list and execute a SelectionRequest
3104 * for each (target,property) pair */
3106 for (i
= 0; i
< cTargetPropList
; i
+=2)
3108 XSelectionRequestEvent event
;
3110 if (TRACE_ON(clipboard
))
3112 char *targetName
, *propName
;
3113 targetName
= XGetAtomName(display
, targetPropList
[i
]);
3114 propName
= XGetAtomName(display
, targetPropList
[i
+1]);
3115 TRACE("MULTIPLE(%d): Target='%s' Prop='%s'\n",
3116 i
/2, targetName
, propName
);
3121 /* We must have a non "None" property to service a MULTIPLE target atom */
3122 if ( !targetPropList
[i
+1] )
3124 TRACE("\tMULTIPLE(%d): Skipping target with empty property!\n", i
);
3128 /* Set up an XSelectionRequestEvent for this (target,property) pair */
3130 event
.target
= targetPropList
[i
];
3131 event
.property
= targetPropList
[i
+1];
3133 /* Fire a SelectionRequest, informing the handler that we are processing
3134 * a MULTIPLE selection request event.
3136 X11DRV_HandleSelectionRequest( hWnd
, &event
, TRUE
);
3140 /* Free the list of targets/properties */
3141 XFree(targetPropList
);
3143 else TRACE("Couldn't read MULTIPLE property\n");
3149 /***********************************************************************
3150 * X11DRV_HandleSelectionRequest
3151 * Process an event selection request event.
3152 * The bIsMultiple flag is used to signal when EVENT_SelectionRequest is called
3153 * recursively while servicing a "MULTIPLE" selection target.
3155 * Note: We only receive this event when WINE owns the X selection
3157 static void X11DRV_HandleSelectionRequest( HWND hWnd
, XSelectionRequestEvent
*event
, BOOL bIsMultiple
)
3159 Display
*display
= event
->display
;
3160 XSelectionEvent result
;
3162 Window request
= event
->requestor
;
3167 * We can only handle the selection request if :
3168 * The selection is PRIMARY or CLIPBOARD, AND we can successfully open the clipboard.
3169 * Don't do these checks or open the clipboard while recursively processing MULTIPLE,
3170 * since this has been already done.
3174 if (((event
->selection
!= XA_PRIMARY
) && (event
->selection
!= x11drv_atom(CLIPBOARD
))))
3178 /* If the specified property is None the requestor is an obsolete client.
3179 * We support these by using the specified target atom as the reply property.
3181 rprop
= event
->property
;
3183 rprop
= event
->target
;
3185 if(event
->target
== x11drv_atom(TARGETS
)) /* Return a list of all supported targets */
3187 /* TARGETS selection request */
3188 rprop
= X11DRV_SelectionRequest_TARGETS( display
, request
, event
->target
, rprop
);
3190 else if(event
->target
== x11drv_atom(MULTIPLE
)) /* rprop contains a list of (target, property) atom pairs */
3192 /* MULTIPLE selection request */
3193 rprop
= X11DRV_SelectionRequest_MULTIPLE( hWnd
, event
);
3197 LPWINE_CLIPFORMAT lpFormat
= X11DRV_CLIPBOARD_LookupProperty(NULL
, event
->target
);
3199 if (lpFormat
&& lpFormat
->lpDrvExportFunc
)
3201 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(lpFormat
->wFormatID
);
3205 unsigned char* lpClipData
;
3207 HANDLE hClipData
= lpFormat
->lpDrvExportFunc(display
, request
, event
->target
,
3208 rprop
, lpData
, &cBytes
);
3210 if (hClipData
&& (lpClipData
= GlobalLock(hClipData
)))
3212 int mode
= PropModeReplace
;
3214 TRACE("\tUpdating property %s, %d bytes\n",
3215 debugstr_format(lpFormat
->wFormatID
), cBytes
);
3218 int nelements
= min(cBytes
, 65536);
3219 XChangeProperty(display
, request
, rprop
, event
->target
,
3220 8, mode
, lpClipData
, nelements
);
3221 mode
= PropModeAppend
;
3222 cBytes
-= nelements
;
3223 lpClipData
+= nelements
;
3224 } while (cBytes
> 0);
3226 GlobalUnlock(hClipData
);
3227 GlobalFree(hClipData
);
3235 * SelectionNotify should be sent only at the end of a MULTIPLE request
3239 result
.type
= SelectionNotify
;
3240 result
.display
= display
;
3241 result
.requestor
= request
;
3242 result
.selection
= event
->selection
;
3243 result
.property
= rprop
;
3244 result
.target
= event
->target
;
3245 result
.time
= event
->time
;
3246 TRACE("Sending SelectionNotify event...\n");
3247 XSendEvent(display
,event
->requestor
,False
,NoEventMask
,(XEvent
*)&result
);
3252 /***********************************************************************
3253 * X11DRV_SelectionRequest
3255 void X11DRV_SelectionRequest( HWND hWnd
, XEvent
*event
)
3257 X11DRV_HandleSelectionRequest( hWnd
, &event
->xselectionrequest
, FALSE
);
3261 /***********************************************************************
3262 * X11DRV_SelectionClear
3264 void X11DRV_SelectionClear( HWND hWnd
, XEvent
*xev
)
3266 XSelectionClearEvent
*event
= &xev
->xselectionclear
;
3267 if (event
->selection
== XA_PRIMARY
|| event
->selection
== x11drv_atom(CLIPBOARD
))
3268 X11DRV_CLIPBOARD_ReleaseSelection( event
->display
, event
->selection
,
3269 event
->window
, hWnd
, event
->time
);