2 * X11 clipboard windows driver
4 * Copyright 1994 Martin Ayotte
7 * 2003 Ulrich Czekalla for CodeWeavers
8 * 2014 Damjan Jovanovic
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * This file contains the X specific implementation for the windows
28 * Wine's internal clipboard is exposed to external apps via the X
29 * selection mechanism.
30 * Currently the driver asserts ownership via two selection atoms:
31 * 1. PRIMARY(XA_PRIMARY)
34 * In our implementation, the CLIPBOARD selection takes precedence over PRIMARY,
35 * i.e. if a CLIPBOARD selection is available, it is used instead of PRIMARY.
36 * When Wine takes ownership of the clipboard, it takes ownership of BOTH selections.
37 * While giving up selection ownership, if the CLIPBOARD selection is lost,
38 * it will lose both PRIMARY and CLIPBOARD and empty the clipboard.
39 * However if only PRIMARY is lost, it will continue to hold the CLIPBOARD selection
40 * (leaving the clipboard cache content unaffected).
42 * Every format exposed via a windows clipboard format is also exposed through
43 * a corresponding X selection target. A selection target atom is synthesized
44 * whenever a new Windows clipboard format is registered via RegisterClipboardFormat,
45 * or when a built-in format is used for the first time.
46 * Windows native format are exposed by prefixing the format name with "<WCF>"
47 * This allows us to uniquely identify windows native formats exposed by other
50 * In order to allow external applications to query WINE for supported formats,
51 * we respond to the "TARGETS" selection target. (See EVENT_SelectionRequest
52 * for implementation) We use the same mechanism to query external clients for
53 * availability of a particular format, by caching the list of available targets
54 * by using the clipboard cache's "delayed render" mechanism. If a selection client
55 * does not support the "TARGETS" selection target, we actually attempt to retrieve
56 * the format requested as a fallback mechanism.
58 * Certain Windows native formats are automatically converted to X native formats
59 * and vice versa. If a native format is available in the selection, it takes
60 * precedence, in order to avoid unnecessary conversions.
62 * FIXME: global format list needs a critical section
66 #include "wine/port.h"
86 #include "wine/list.h"
87 #include "wine/debug.h"
88 #include "wine/unicode.h"
89 #include "wine/server.h"
91 WINE_DEFAULT_DEBUG_CHANNEL(clipboard
);
93 /* Maximum wait time for selection notify */
94 #define SELECTION_RETRIES 500 /* wait for .5 seconds */
95 #define SELECTION_WAIT 1000 /* us */
98 #define S_NOSELECTION 0
100 #define S_CLIPBOARD 2
109 } CLIPBOARDINFO
, *LPCLIPBOARDINFO
;
111 struct tagWINE_CLIPDATA
; /* Forward */
113 typedef HANDLE (*DRVEXPORTFUNC
)(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
114 struct tagWINE_CLIPDATA
* lpData
, LPDWORD lpBytes
);
115 typedef HANDLE (*DRVIMPORTFUNC
)(Display
*d
, Window w
, Atom prop
);
117 typedef struct tagWINE_CLIPFORMAT
{
121 DRVIMPORTFUNC lpDrvImportFunc
;
122 DRVEXPORTFUNC lpDrvExportFunc
;
123 } WINE_CLIPFORMAT
, *LPWINE_CLIPFORMAT
;
125 typedef struct tagWINE_CLIPDATA
{
131 LPWINE_CLIPFORMAT lpFormat
;
132 } WINE_CLIPDATA
, *LPWINE_CLIPDATA
;
134 #define CF_FLAG_UNOWNED 0x0001 /* cached data is not owned */
135 #define CF_FLAG_SYNTHESIZED 0x0002 /* Implicitly converted data */
137 static int selectionAcquired
= 0; /* Contains the current selection masks */
138 static Window selectionWindow
= None
; /* The top level X window which owns the selection */
139 static Atom selectionCacheSrc
= XA_PRIMARY
; /* The selection source from which the clipboard cache was filled */
141 void CDECL
X11DRV_EmptyClipboard(BOOL keepunowned
);
142 void CDECL
X11DRV_EndClipboardUpdate(void);
143 static HANDLE
X11DRV_CLIPBOARD_ImportClipboardData(Display
*d
, Window w
, Atom prop
);
144 static HANDLE
X11DRV_CLIPBOARD_ImportEnhMetaFile(Display
*d
, Window w
, Atom prop
);
145 static HANDLE
X11DRV_CLIPBOARD_ImportMetaFilePict(Display
*d
, Window w
, Atom prop
);
146 static HANDLE
X11DRV_CLIPBOARD_ImportXAPIXMAP(Display
*d
, Window w
, Atom prop
);
147 static HANDLE
X11DRV_CLIPBOARD_ImportImageBmp(Display
*d
, Window w
, Atom prop
);
148 static HANDLE
X11DRV_CLIPBOARD_ImportXAString(Display
*d
, Window w
, Atom prop
);
149 static HANDLE
X11DRV_CLIPBOARD_ImportUTF8(Display
*d
, Window w
, Atom prop
);
150 static HANDLE
X11DRV_CLIPBOARD_ImportCompoundText(Display
*d
, Window w
, Atom prop
);
151 static HANDLE
X11DRV_CLIPBOARD_ImportTextUriList(Display
*display
, Window w
, Atom prop
);
152 static HANDLE
X11DRV_CLIPBOARD_ExportClipboardData(Display
*display
, Window requestor
, Atom aTarget
,
153 Atom rprop
, LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
);
154 static HANDLE
X11DRV_CLIPBOARD_ExportString(Display
*display
, Window requestor
, Atom aTarget
,
155 Atom rprop
, LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
);
156 static HANDLE
X11DRV_CLIPBOARD_ExportXAPIXMAP(Display
*display
, Window requestor
, Atom aTarget
,
157 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
);
158 static HANDLE
X11DRV_CLIPBOARD_ExportImageBmp(Display
*display
, Window requestor
, Atom aTarget
,
159 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
);
160 static HANDLE
X11DRV_CLIPBOARD_ExportMetaFilePict(Display
*display
, Window requestor
, Atom aTarget
,
161 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
);
162 static HANDLE
X11DRV_CLIPBOARD_ExportEnhMetaFile(Display
*display
, Window requestor
, Atom aTarget
,
163 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
);
164 static HANDLE
X11DRV_CLIPBOARD_ExportTextHtml(Display
*display
, Window requestor
, Atom aTarget
,
165 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
);
166 static HANDLE
X11DRV_CLIPBOARD_ExportHDROP(Display
*display
, Window requestor
, Atom aTarget
,
167 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
);
168 static WINE_CLIPFORMAT
*X11DRV_CLIPBOARD_InsertClipboardFormat(UINT id
, Atom prop
);
169 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedText(Display
*display
, UINT wFormatID
);
170 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData
);
171 static BOOL
X11DRV_CLIPBOARD_IsSelectionOwner(void);
172 static int X11DRV_CLIPBOARD_QueryAvailableData(Display
*display
, LPCLIPBOARDINFO lpcbinfo
);
173 static BOOL
X11DRV_CLIPBOARD_ReadSelectionData(Display
*display
, LPWINE_CLIPDATA lpData
);
174 static BOOL
X11DRV_CLIPBOARD_ReadProperty(Display
*display
, Window w
, Atom prop
,
175 unsigned char** data
, unsigned long* datasize
);
176 static BOOL
X11DRV_CLIPBOARD_RenderFormat(Display
*display
, LPWINE_CLIPDATA lpData
);
177 static HANDLE
X11DRV_CLIPBOARD_SerializeMetafile(INT wformat
, HANDLE hdata
, LPDWORD lpcbytes
, BOOL out
);
178 static BOOL
X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID
);
179 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedFormat(Display
*display
, LPWINE_CLIPDATA lpData
);
180 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedDIB(Display
*display
);
181 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedBitmap(Display
*display
);
182 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile(Display
*display
);
183 static void X11DRV_HandleSelectionRequest( HWND hWnd
, XSelectionRequestEvent
*event
, BOOL bIsMultiple
);
185 /* Clipboard formats */
191 DRVIMPORTFUNC import
;
192 DRVEXPORTFUNC export
;
193 } builtin_formats
[] =
195 { CF_TEXT
, XA_STRING
, X11DRV_CLIPBOARD_ImportXAString
, X11DRV_CLIPBOARD_ExportString
},
196 { CF_TEXT
, XATOM_text_plain
, X11DRV_CLIPBOARD_ImportXAString
, X11DRV_CLIPBOARD_ExportString
},
197 { CF_BITMAP
, XATOM_WCF_BITMAP
, X11DRV_CLIPBOARD_ImportClipboardData
, NULL
},
198 { CF_METAFILEPICT
, XATOM_WCF_METAFILEPICT
, X11DRV_CLIPBOARD_ImportMetaFilePict
, X11DRV_CLIPBOARD_ExportMetaFilePict
},
199 { CF_SYLK
, XATOM_WCF_SYLK
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
200 { CF_DIF
, XATOM_WCF_DIF
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
201 { CF_TIFF
, XATOM_WCF_TIFF
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
202 { CF_OEMTEXT
, XATOM_WCF_OEMTEXT
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
203 { CF_DIB
, XA_PIXMAP
, X11DRV_CLIPBOARD_ImportXAPIXMAP
, X11DRV_CLIPBOARD_ExportXAPIXMAP
},
204 { CF_PALETTE
, XATOM_WCF_PALETTE
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
205 { CF_PENDATA
, XATOM_WCF_PENDATA
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
206 { CF_RIFF
, XATOM_WCF_RIFF
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
207 { CF_WAVE
, XATOM_WCF_WAVE
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
208 { CF_UNICODETEXT
, XATOM_UTF8_STRING
, X11DRV_CLIPBOARD_ImportUTF8
, X11DRV_CLIPBOARD_ExportString
},
209 /* If UTF8_STRING is not available, attempt COMPOUND_TEXT */
210 { CF_UNICODETEXT
, XATOM_COMPOUND_TEXT
, X11DRV_CLIPBOARD_ImportCompoundText
, X11DRV_CLIPBOARD_ExportString
},
211 { CF_ENHMETAFILE
, XATOM_WCF_ENHMETAFILE
, X11DRV_CLIPBOARD_ImportEnhMetaFile
, X11DRV_CLIPBOARD_ExportEnhMetaFile
},
212 { CF_HDROP
, XATOM_text_uri_list
, X11DRV_CLIPBOARD_ImportTextUriList
, X11DRV_CLIPBOARD_ExportHDROP
},
213 { CF_LOCALE
, XATOM_WCF_LOCALE
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
214 { CF_DIBV5
, XATOM_WCF_DIBV5
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
215 { CF_OWNERDISPLAY
, XATOM_WCF_OWNERDISPLAY
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
216 { CF_DSPTEXT
, XATOM_WCF_DSPTEXT
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
217 { CF_DSPBITMAP
, XATOM_WCF_DSPBITMAP
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
218 { CF_DSPMETAFILEPICT
, XATOM_WCF_DSPMETAFILEPICT
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
219 { CF_DSPENHMETAFILE
, XATOM_WCF_DSPENHMETAFILE
, X11DRV_CLIPBOARD_ImportClipboardData
, X11DRV_CLIPBOARD_ExportClipboardData
},
220 { CF_DIB
, XATOM_image_bmp
, X11DRV_CLIPBOARD_ImportImageBmp
, X11DRV_CLIPBOARD_ExportImageBmp
},
223 static struct list format_list
= LIST_INIT( format_list
);
225 #define GET_ATOM(prop) (((prop) < FIRST_XATOM) ? (Atom)(prop) : X11DRV_Atoms[(prop) - FIRST_XATOM])
227 /* Maps X properties to Windows formats */
228 static const WCHAR wszRichTextFormat
[] = {'R','i','c','h',' ','T','e','x','t',' ','F','o','r','m','a','t',0};
229 static const WCHAR wszGIF
[] = {'G','I','F',0};
230 static const WCHAR wszJFIF
[] = {'J','F','I','F',0};
231 static const WCHAR wszPNG
[] = {'P','N','G',0};
232 static const WCHAR wszHTMLFormat
[] = {'H','T','M','L',' ','F','o','r','m','a','t',0};
237 } PropertyFormatMap
[] =
239 { wszRichTextFormat
, XATOM_text_rtf
},
240 { wszRichTextFormat
, XATOM_text_richtext
},
241 { wszGIF
, XATOM_image_gif
},
242 { wszJFIF
, XATOM_image_jpeg
},
243 { wszPNG
, XATOM_image_png
},
244 { wszHTMLFormat
, XATOM_HTML_Format
}, /* prefer this to text/html */
249 * Cached clipboard data.
251 static struct list data_list
= LIST_INIT( data_list
);
252 static UINT ClipDataCount
= 0;
255 * Clipboard sequence number
257 static UINT wSeqNo
= 0;
259 /**************************************************************************
260 * Internal Clipboard implementation methods
261 **************************************************************************/
263 static Window
thread_selection_wnd(void)
265 struct x11drv_thread_data
*thread_data
= x11drv_init_thread_data();
266 Window w
= thread_data
->selection_wnd
;
270 w
= XCreateWindow(thread_data
->display
, root_window
, 0, 0, 1, 1, 0, CopyFromParent
,
271 InputOnly
, CopyFromParent
, 0, NULL
);
274 thread_data
->selection_wnd
= w
;
276 XSelectInput(thread_data
->display
, w
, PropertyChangeMask
);
279 FIXME("Failed to create window. Fetching selection data will fail.\n");
285 static const char *debugstr_format( UINT id
)
289 if (GetClipboardFormatNameW( id
, buffer
, 256 ))
290 return wine_dbg_sprintf( "%04x %s", id
, debugstr_w(buffer
) );
294 #define BUILTIN(id) case id: return #id;
297 BUILTIN(CF_METAFILEPICT
)
307 BUILTIN(CF_UNICODETEXT
)
308 BUILTIN(CF_ENHMETAFILE
)
312 BUILTIN(CF_OWNERDISPLAY
)
314 BUILTIN(CF_DSPBITMAP
)
315 BUILTIN(CF_DSPMETAFILEPICT
)
316 BUILTIN(CF_DSPENHMETAFILE
)
318 default: return wine_dbg_sprintf( "%04x", id
);
322 /**************************************************************************
323 * X11DRV_InitClipboard
325 void X11DRV_InitClipboard(void)
328 WINE_CLIPFORMAT
*format
;
330 /* Register built-in formats */
331 for (i
= 0; i
< sizeof(builtin_formats
)/sizeof(builtin_formats
[0]); i
++)
333 if (!(format
= HeapAlloc( GetProcessHeap(), 0, sizeof(*format
)))) break;
334 format
->wFormatID
= builtin_formats
[i
].id
;
335 format
->drvData
= GET_ATOM(builtin_formats
[i
].data
);
336 format
->lpDrvImportFunc
= builtin_formats
[i
].import
;
337 format
->lpDrvExportFunc
= builtin_formats
[i
].export
;
338 list_add_tail( &format_list
, &format
->entry
);
341 /* Register known mapping between window formats and X properties */
342 for (i
= 0; i
< sizeof(PropertyFormatMap
)/sizeof(PropertyFormatMap
[0]); i
++)
343 X11DRV_CLIPBOARD_InsertClipboardFormat( RegisterClipboardFormatW(PropertyFormatMap
[i
].lpszFormat
),
344 GET_ATOM(PropertyFormatMap
[i
].prop
));
346 /* Set up a conversion function from "HTML Format" to "text/html" */
347 format
= X11DRV_CLIPBOARD_InsertClipboardFormat( RegisterClipboardFormatW(wszHTMLFormat
),
348 GET_ATOM(XATOM_text_html
));
349 format
->lpDrvExportFunc
= X11DRV_CLIPBOARD_ExportTextHtml
;
353 /**************************************************************************
356 * Intern atoms for formats that don't have one yet.
358 static void intern_atoms(void)
360 LPWINE_CLIPFORMAT format
;
368 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
369 if (!format
->drvData
) count
++;
372 display
= thread_init_display();
374 names
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*names
) );
375 atoms
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*atoms
) );
378 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
379 if (!format
->drvData
) {
380 GetClipboardFormatNameW( format
->wFormatID
, buffer
, 256 );
381 len
= WideCharToMultiByte(CP_UNIXCP
, 0, buffer
, -1, NULL
, 0, NULL
, NULL
);
382 names
[i
] = HeapAlloc(GetProcessHeap(), 0, len
);
383 WideCharToMultiByte(CP_UNIXCP
, 0, buffer
, -1, names
[i
++], len
, NULL
, NULL
);
386 XInternAtoms( display
, names
, count
, False
, atoms
);
389 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
390 if (!format
->drvData
) {
391 HeapFree(GetProcessHeap(), 0, names
[i
]);
392 format
->drvData
= atoms
[i
++];
395 HeapFree( GetProcessHeap(), 0, names
);
396 HeapFree( GetProcessHeap(), 0, atoms
);
400 /**************************************************************************
403 * Register a custom X clipboard format.
405 static WINE_CLIPFORMAT
*register_format( UINT id
, Atom prop
)
407 LPWINE_CLIPFORMAT lpFormat
;
409 /* walk format chain to see if it's already registered */
410 LIST_FOR_EACH_ENTRY( lpFormat
, &format_list
, WINE_CLIPFORMAT
, entry
)
411 if (lpFormat
->wFormatID
== id
) return lpFormat
;
413 return X11DRV_CLIPBOARD_InsertClipboardFormat(id
, prop
);
417 /**************************************************************************
418 * X11DRV_CLIPBOARD_LookupProperty
420 static LPWINE_CLIPFORMAT
X11DRV_CLIPBOARD_LookupProperty(LPWINE_CLIPFORMAT current
, UINT drvData
)
424 struct list
*ptr
= current
? ¤t
->entry
: &format_list
;
425 BOOL need_intern
= FALSE
;
427 while ((ptr
= list_next( &format_list
, ptr
)))
429 LPWINE_CLIPFORMAT lpFormat
= LIST_ENTRY( ptr
, WINE_CLIPFORMAT
, entry
);
430 if (lpFormat
->drvData
== drvData
) return lpFormat
;
431 if (!lpFormat
->drvData
) need_intern
= TRUE
;
433 if (!need_intern
) return NULL
;
435 /* restart the search for the new atoms */
440 /**************************************************************************
441 * X11DRV_CLIPBOARD_LookupData
443 static LPWINE_CLIPDATA
X11DRV_CLIPBOARD_LookupData(DWORD wID
)
447 LIST_FOR_EACH_ENTRY( data
, &data_list
, WINE_CLIPDATA
, entry
)
448 if (data
->wFormatID
== wID
) return data
;
454 /**************************************************************************
455 * InsertClipboardFormat
457 static WINE_CLIPFORMAT
*X11DRV_CLIPBOARD_InsertClipboardFormat( UINT id
, Atom prop
)
459 LPWINE_CLIPFORMAT lpNewFormat
;
461 /* allocate storage for new format entry */
462 lpNewFormat
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPFORMAT
));
464 if(lpNewFormat
== NULL
)
466 WARN("No more memory for a new format!\n");
469 lpNewFormat
->wFormatID
= id
;
470 lpNewFormat
->drvData
= prop
;
471 lpNewFormat
->lpDrvImportFunc
= X11DRV_CLIPBOARD_ImportClipboardData
;
472 lpNewFormat
->lpDrvExportFunc
= X11DRV_CLIPBOARD_ExportClipboardData
;
474 list_add_tail( &format_list
, &lpNewFormat
->entry
);
476 TRACE("Registering format %s drvData %d\n",
477 debugstr_format(lpNewFormat
->wFormatID
), lpNewFormat
->drvData
);
485 /**************************************************************************
486 * X11DRV_CLIPBOARD_GetClipboardInfo
488 static BOOL
X11DRV_CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo
)
492 SERVER_START_REQ( set_clipboard_info
)
496 if (wine_server_call_err( req
))
498 ERR("Failed to get clipboard owner.\n");
502 cbInfo
->hWndOpen
= wine_server_ptr_handle( reply
->old_clipboard
);
503 cbInfo
->hWndOwner
= wine_server_ptr_handle( reply
->old_owner
);
504 cbInfo
->hWndViewer
= wine_server_ptr_handle( reply
->old_viewer
);
505 cbInfo
->seqno
= reply
->seqno
;
506 cbInfo
->flags
= reply
->flags
;
517 /**************************************************************************
518 * X11DRV_CLIPBOARD_ReleaseOwnership
520 static BOOL
X11DRV_CLIPBOARD_ReleaseOwnership(void)
524 SERVER_START_REQ( set_clipboard_info
)
526 req
->flags
= SET_CB_RELOWNER
| SET_CB_SEQNO
;
528 if (wine_server_call_err( req
))
530 ERR("Failed to set clipboard.\n");
544 /**************************************************************************
545 * X11DRV_CLIPBOARD_InsertClipboardData
547 * Caller *must* have the clipboard open and be the owner.
549 static BOOL
X11DRV_CLIPBOARD_InsertClipboardData(UINT wFormatID
, HANDLE hData
, DWORD flags
,
550 LPWINE_CLIPFORMAT lpFormat
, BOOL override
)
552 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(wFormatID
);
554 TRACE("format=%04x lpData=%p hData=%p flags=0x%08x lpFormat=%p override=%d\n",
555 wFormatID
, lpData
, hData
, flags
, lpFormat
, override
);
557 /* make sure the format exists */
558 if (!lpFormat
) register_format( wFormatID
, 0 );
560 if (lpData
&& !override
)
565 X11DRV_CLIPBOARD_FreeData(lpData
);
567 lpData
->hData
= hData
;
571 lpData
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_CLIPDATA
));
573 lpData
->wFormatID
= wFormatID
;
574 lpData
->hData
= hData
;
575 lpData
->lpFormat
= lpFormat
;
578 list_add_tail( &data_list
, &lpData
->entry
);
582 lpData
->wFlags
= flags
;
588 /**************************************************************************
589 * X11DRV_CLIPBOARD_FreeData
591 * Free clipboard data handle.
593 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData
)
595 TRACE("%04x\n", lpData
->wFormatID
);
597 if ((lpData
->wFormatID
>= CF_GDIOBJFIRST
&&
598 lpData
->wFormatID
<= CF_GDIOBJLAST
) ||
599 lpData
->wFormatID
== CF_BITMAP
||
600 lpData
->wFormatID
== CF_DIB
||
601 lpData
->wFormatID
== CF_PALETTE
)
604 DeleteObject(lpData
->hData
);
606 if ((lpData
->wFormatID
== CF_DIB
) && lpData
->drvData
)
607 XFreePixmap(gdi_display
, lpData
->drvData
);
609 else if (lpData
->wFormatID
== CF_METAFILEPICT
)
613 DeleteMetaFile(((METAFILEPICT
*)GlobalLock( lpData
->hData
))->hMF
);
614 GlobalFree(lpData
->hData
);
617 else if (lpData
->wFormatID
== CF_ENHMETAFILE
)
620 DeleteEnhMetaFile(lpData
->hData
);
622 else if (lpData
->wFormatID
< CF_PRIVATEFIRST
||
623 lpData
->wFormatID
> CF_PRIVATELAST
)
626 GlobalFree(lpData
->hData
);
634 /**************************************************************************
635 * X11DRV_CLIPBOARD_UpdateCache
637 static BOOL
X11DRV_CLIPBOARD_UpdateCache(LPCLIPBOARDINFO lpcbinfo
)
641 if (!X11DRV_CLIPBOARD_IsSelectionOwner())
643 if (!X11DRV_CLIPBOARD_GetClipboardInfo(lpcbinfo
))
645 ERR("Failed to retrieve clipboard information.\n");
648 else if (wSeqNo
< lpcbinfo
->seqno
)
650 X11DRV_EmptyClipboard(TRUE
);
652 if (X11DRV_CLIPBOARD_QueryAvailableData(thread_init_display(), lpcbinfo
) < 0)
654 ERR("Failed to cache clipboard data owned by another process.\n");
659 X11DRV_EndClipboardUpdate();
662 wSeqNo
= lpcbinfo
->seqno
;
670 /**************************************************************************
671 * X11DRV_CLIPBOARD_RenderFormat
673 static BOOL
X11DRV_CLIPBOARD_RenderFormat(Display
*display
, LPWINE_CLIPDATA lpData
)
677 TRACE(" 0x%04x hData(%p)\n", lpData
->wFormatID
, lpData
->hData
);
679 if (lpData
->hData
) return bret
; /* Already rendered */
681 if (lpData
->wFlags
& CF_FLAG_SYNTHESIZED
)
682 bret
= X11DRV_CLIPBOARD_RenderSynthesizedFormat(display
, lpData
);
683 else if (!X11DRV_CLIPBOARD_IsSelectionOwner())
685 if (!X11DRV_CLIPBOARD_ReadSelectionData(display
, lpData
))
687 ERR("Failed to cache clipboard data owned by another process. Format=%04x\n",
694 CLIPBOARDINFO cbInfo
;
696 if (X11DRV_CLIPBOARD_GetClipboardInfo(&cbInfo
) && cbInfo
.hWndOwner
)
698 /* Send a WM_RENDERFORMAT message to notify the owner to render the
699 * data requested into the clipboard.
701 TRACE("Sending WM_RENDERFORMAT message to hwnd(%p)\n", cbInfo
.hWndOwner
);
702 SendMessageW(cbInfo
.hWndOwner
, WM_RENDERFORMAT
, lpData
->wFormatID
, 0);
704 if (!lpData
->hData
) bret
= FALSE
;
708 ERR("hWndClipOwner is lost!\n");
717 /**************************************************************************
718 * CLIPBOARD_ConvertText
719 * Returns number of required/converted characters - not bytes!
721 static INT
CLIPBOARD_ConvertText(WORD src_fmt
, void const *src
, INT src_size
,
722 WORD dst_fmt
, void *dst
, INT dst_size
)
726 if(src_fmt
== CF_UNICODETEXT
)
739 return WideCharToMultiByte(cp
, 0, src
, src_size
, dst
, dst_size
, NULL
, NULL
);
742 if(dst_fmt
== CF_UNICODETEXT
)
755 return MultiByteToWideChar(cp
, 0, src
, src_size
, dst
, dst_size
);
758 if(!dst_size
) return src_size
;
760 if(dst_size
> src_size
) dst_size
= src_size
;
762 if(src_fmt
== CF_TEXT
)
763 CharToOemBuffA(src
, dst
, dst_size
);
765 OemToCharBuffA(src
, dst
, dst_size
);
771 /**************************************************************************
772 * X11DRV_CLIPBOARD_RenderSynthesizedFormat
774 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedFormat(Display
*display
, LPWINE_CLIPDATA lpData
)
780 if (lpData
->wFlags
& CF_FLAG_SYNTHESIZED
)
782 UINT wFormatID
= lpData
->wFormatID
;
784 if (wFormatID
== CF_UNICODETEXT
|| wFormatID
== CF_TEXT
|| wFormatID
== CF_OEMTEXT
)
785 bret
= X11DRV_CLIPBOARD_RenderSynthesizedText(display
, wFormatID
);
791 bret
= X11DRV_CLIPBOARD_RenderSynthesizedDIB( display
);
795 bret
= X11DRV_CLIPBOARD_RenderSynthesizedBitmap( display
);
799 bret
= X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile( display
);
802 case CF_METAFILEPICT
:
803 FIXME("Synthesizing CF_METAFILEPICT not implemented\n");
807 FIXME("Called to synthesize unknown format 0x%08x\n", wFormatID
);
812 lpData
->wFlags
&= ~CF_FLAG_SYNTHESIZED
;
819 /**************************************************************************
820 * X11DRV_CLIPBOARD_RenderSynthesizedText
822 * Renders synthesized text
824 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedText(Display
*display
, UINT wFormatID
)
829 INT src_chars
, dst_chars
, alloc_size
;
830 LPWINE_CLIPDATA lpSource
= NULL
;
832 TRACE("%04x\n", wFormatID
);
834 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(wFormatID
)) &&
838 /* Look for rendered source or non-synthesized source */
839 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT
)) &&
840 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
842 TRACE("UNICODETEXT -> %04x\n", wFormatID
);
844 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_TEXT
)) &&
845 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
847 TRACE("TEXT -> %04x\n", wFormatID
);
849 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT
)) &&
850 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
852 TRACE("OEMTEXT -> %04x\n", wFormatID
);
855 if (!lpSource
|| (lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
&&
859 /* Ask the clipboard owner to render the source text if necessary */
860 if (!lpSource
->hData
&& !X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
863 lpstrS
= GlobalLock(lpSource
->hData
);
867 /* Text always NULL terminated */
868 if(lpSource
->wFormatID
== CF_UNICODETEXT
)
869 src_chars
= strlenW((LPCWSTR
)lpstrS
) + 1;
871 src_chars
= strlen(lpstrS
) + 1;
873 /* Calculate number of characters in the destination buffer */
874 dst_chars
= CLIPBOARD_ConvertText(lpSource
->wFormatID
, lpstrS
,
875 src_chars
, wFormatID
, NULL
, 0);
880 TRACE("Converting from '%04x' to '%04x', %i chars\n",
881 lpSource
->wFormatID
, wFormatID
, src_chars
);
883 /* Convert characters to bytes */
884 if(wFormatID
== CF_UNICODETEXT
)
885 alloc_size
= dst_chars
* sizeof(WCHAR
);
887 alloc_size
= dst_chars
;
889 hData
= GlobalAlloc(GMEM_ZEROINIT
| GMEM_MOVEABLE
|
890 GMEM_DDESHARE
, alloc_size
);
892 lpstrT
= GlobalLock(hData
);
896 CLIPBOARD_ConvertText(lpSource
->wFormatID
, lpstrS
, src_chars
,
897 wFormatID
, lpstrT
, dst_chars
);
901 GlobalUnlock(lpSource
->hData
);
903 return X11DRV_CLIPBOARD_InsertClipboardData(wFormatID
, hData
, 0, NULL
, TRUE
);
907 /***********************************************************************
910 * Return the size of the bitmap info structure including color table.
912 static int bitmap_info_size( const BITMAPINFO
* info
, WORD coloruse
)
914 unsigned int colors
, size
, masks
= 0;
916 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
918 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
919 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
920 return sizeof(BITMAPCOREHEADER
) + colors
*
921 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
923 else /* assume BITMAPINFOHEADER */
925 colors
= info
->bmiHeader
.biClrUsed
;
926 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
927 colors
= 1 << info
->bmiHeader
.biBitCount
;
928 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
929 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
930 return size
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
935 /***********************************************************************
936 * create_dib_from_bitmap
938 * Allocates a packed DIB and copies the bitmap data into it.
940 static HGLOBAL
create_dib_from_bitmap(HBITMAP hBmp
)
946 LPBITMAPINFOHEADER pbmiHeader
;
947 unsigned int cDataSize
, cPackedSize
, OffsetBits
;
950 if (!GetObjectW( hBmp
, sizeof(bmp
), &bmp
)) return 0;
953 * A packed DIB contains a BITMAPINFO structure followed immediately by
954 * an optional color palette and the pixel data.
957 /* Calculate the size of the packed DIB */
958 cDataSize
= abs( bmp
.bmHeight
) * (((bmp
.bmWidth
* bmp
.bmBitsPixel
+ 31) / 8) & ~3);
959 cPackedSize
= sizeof(BITMAPINFOHEADER
)
960 + ( (bmp
.bmBitsPixel
<= 8) ? (sizeof(RGBQUAD
) * (1 << bmp
.bmBitsPixel
)) : 0 )
962 /* Get the offset to the bits */
963 OffsetBits
= cPackedSize
- cDataSize
;
965 /* Allocate the packed DIB */
966 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize
);
967 hPackedDIB
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
/*| GMEM_ZEROINIT*/,
971 WARN("Could not allocate packed DIB!\n");
975 /* A packed DIB starts with a BITMAPINFOHEADER */
976 pPackedDIB
= GlobalLock(hPackedDIB
);
977 pbmiHeader
= (LPBITMAPINFOHEADER
)pPackedDIB
;
979 /* Init the BITMAPINFOHEADER */
980 pbmiHeader
->biSize
= sizeof(BITMAPINFOHEADER
);
981 pbmiHeader
->biWidth
= bmp
.bmWidth
;
982 pbmiHeader
->biHeight
= bmp
.bmHeight
;
983 pbmiHeader
->biPlanes
= 1;
984 pbmiHeader
->biBitCount
= bmp
.bmBitsPixel
;
985 pbmiHeader
->biCompression
= BI_RGB
;
986 pbmiHeader
->biSizeImage
= 0;
987 pbmiHeader
->biXPelsPerMeter
= pbmiHeader
->biYPelsPerMeter
= 0;
988 pbmiHeader
->biClrUsed
= 0;
989 pbmiHeader
->biClrImportant
= 0;
991 /* Retrieve the DIB bits from the bitmap and fill in the
992 * DIB color table if present */
994 nLinesCopied
= GetDIBits(hdc
, /* Handle to device context */
995 hBmp
, /* Handle to bitmap */
996 0, /* First scan line to set in dest bitmap */
997 bmp
.bmHeight
, /* Number of scan lines to copy */
998 pPackedDIB
+ OffsetBits
, /* [out] Address of array for bitmap bits */
999 (LPBITMAPINFO
) pbmiHeader
, /* [out] Address of BITMAPINFO structure */
1000 0); /* RGB or palette index */
1001 GlobalUnlock(hPackedDIB
);
1002 ReleaseDC( 0, hdc
);
1004 /* Cleanup if GetDIBits failed */
1005 if (nLinesCopied
!= bmp
.bmHeight
)
1007 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied
, bmp
.bmHeight
);
1008 GlobalFree(hPackedDIB
);
1015 /***********************************************************************
1018 * Converts a text/uri-list URI to DOS filename.
1020 static WCHAR
* uri_to_dos(char *encodedURI
)
1025 char *uri
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, strlen(encodedURI
) + 1);
1028 for (i
= 0; encodedURI
[i
]; ++i
)
1030 if (encodedURI
[i
] == '%')
1032 if (encodedURI
[i
+1] && encodedURI
[i
+2])
1036 buffer
[0] = encodedURI
[i
+1];
1037 buffer
[1] = encodedURI
[i
+2];
1039 sscanf(buffer
, "%x", &number
);
1045 WARN("invalid URI encoding in %s\n", debugstr_a(encodedURI
));
1046 HeapFree(GetProcessHeap(), 0, uri
);
1051 uri
[j
++] = encodedURI
[i
];
1054 /* Read http://www.freedesktop.org/wiki/Draganddropwarts and cry... */
1055 if (strncmp(uri
, "file:/", 6) == 0)
1061 /* file:///path/to/file (nautilus, thunar) */
1062 ret
= wine_get_dos_file_name(&uri
[7]);
1066 /* file://hostname/path/to/file (X file drag spec) */
1068 char *path
= strchr(&uri
[7], '/');
1072 if (strcmp(&uri
[7], "localhost") == 0)
1075 ret
= wine_get_dos_file_name(path
);
1077 else if (gethostname(hostname
, sizeof(hostname
)) == 0)
1079 if (strcmp(hostname
, &uri
[7]) == 0)
1082 ret
= wine_get_dos_file_name(path
);
1090 /* file:/path/to/file (konqueror) */
1091 ret
= wine_get_dos_file_name(&uri
[5]);
1094 HeapFree(GetProcessHeap(), 0, uri
);
1099 /**************************************************************************
1100 * X11DRV_CLIPBOARD_RenderSynthesizedDIB
1102 * Renders synthesized DIB
1104 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedDIB(Display
*display
)
1107 LPWINE_CLIPDATA lpSource
= NULL
;
1111 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) && lpSource
->hData
)
1115 /* If we have a bitmap and it's not synthesized or it has been rendered */
1116 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) &&
1117 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
1119 /* Render source if required */
1120 if (lpSource
->hData
|| X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
1122 HGLOBAL hData
= create_dib_from_bitmap( lpSource
->hData
);
1125 X11DRV_CLIPBOARD_InsertClipboardData(CF_DIB
, hData
, 0, NULL
, TRUE
);
1135 /**************************************************************************
1136 * X11DRV_CLIPBOARD_RenderSynthesizedBitmap
1138 * Renders synthesized bitmap
1140 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedBitmap(Display
*display
)
1143 LPWINE_CLIPDATA lpSource
= NULL
;
1147 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) && lpSource
->hData
)
1151 /* If we have a dib and it's not synthesized or it has been rendered */
1152 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) &&
1153 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
1155 /* Render source if required */
1156 if (lpSource
->hData
|| X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
1159 HBITMAP hData
= NULL
;
1160 unsigned int offset
;
1161 LPBITMAPINFOHEADER lpbmih
;
1164 lpbmih
= GlobalLock(lpSource
->hData
);
1167 offset
= sizeof(BITMAPINFOHEADER
)
1168 + ((lpbmih
->biBitCount
<= 8) ? (sizeof(RGBQUAD
) *
1169 (1 << lpbmih
->biBitCount
)) : 0);
1171 hData
= CreateDIBitmap(hdc
, lpbmih
, CBM_INIT
, (LPBYTE
)lpbmih
+
1172 offset
, (LPBITMAPINFO
) lpbmih
, DIB_RGB_COLORS
);
1174 GlobalUnlock(lpSource
->hData
);
1176 ReleaseDC(NULL
, hdc
);
1180 X11DRV_CLIPBOARD_InsertClipboardData(CF_BITMAP
, hData
, 0, NULL
, TRUE
);
1190 /**************************************************************************
1191 * X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile
1193 static BOOL
X11DRV_CLIPBOARD_RenderSynthesizedEnhMetaFile(Display
*display
)
1195 LPWINE_CLIPDATA lpSource
= NULL
;
1199 if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_ENHMETAFILE
)) && lpSource
->hData
)
1201 /* If we have a MF pict and it's not synthesized or it has been rendered */
1202 else if ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT
)) &&
1203 (!(lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) || lpSource
->hData
))
1205 /* Render source if required */
1206 if (lpSource
->hData
|| X11DRV_CLIPBOARD_RenderFormat(display
, lpSource
))
1209 HENHMETAFILE hData
= NULL
;
1211 pmfp
= GlobalLock(lpSource
->hData
);
1214 UINT size_mf_bits
= GetMetaFileBitsEx(pmfp
->hMF
, 0, NULL
);
1215 void *mf_bits
= HeapAlloc(GetProcessHeap(), 0, size_mf_bits
);
1218 GetMetaFileBitsEx(pmfp
->hMF
, size_mf_bits
, mf_bits
);
1219 hData
= SetWinMetaFileBits(size_mf_bits
, mf_bits
, NULL
, pmfp
);
1220 HeapFree(GetProcessHeap(), 0, mf_bits
);
1222 GlobalUnlock(lpSource
->hData
);
1227 X11DRV_CLIPBOARD_InsertClipboardData(CF_ENHMETAFILE
, hData
, 0, NULL
, TRUE
);
1237 /**************************************************************************
1238 * X11DRV_CLIPBOARD_ImportXAString
1240 * Import XA_STRING, converting the string to CF_TEXT.
1242 static HANDLE
X11DRV_CLIPBOARD_ImportXAString(Display
*display
, Window w
, Atom prop
)
1245 unsigned long cbytes
;
1247 unsigned long i
, inlcount
= 0;
1250 if (!X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1253 for (i
= 0; i
<= cbytes
; i
++)
1255 if (lpdata
[i
] == '\n')
1259 if ((hText
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cbytes
+ inlcount
+ 1)))
1261 lpstr
= GlobalLock(hText
);
1263 for (i
= 0, inlcount
= 0; i
<= cbytes
; i
++)
1265 if (lpdata
[i
] == '\n')
1266 lpstr
[inlcount
++] = '\r';
1268 lpstr
[inlcount
++] = lpdata
[i
];
1271 GlobalUnlock(hText
);
1274 /* Free the retrieved property data */
1275 HeapFree(GetProcessHeap(), 0, lpdata
);
1281 /**************************************************************************
1282 * X11DRV_CLIPBOARD_ImportUTF8
1284 * Import XA_STRING, converting the string to CF_UNICODE.
1286 static HANDLE
X11DRV_CLIPBOARD_ImportUTF8(Display
*display
, Window w
, Atom prop
)
1289 unsigned long cbytes
;
1291 unsigned long i
, inlcount
= 0;
1292 HANDLE hUnicodeText
= 0;
1294 if (!X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1297 for (i
= 0; i
<= cbytes
; i
++)
1299 if (lpdata
[i
] == '\n')
1303 if ((lpstr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cbytes
+ inlcount
+ 1)))
1307 for (i
= 0, inlcount
= 0; i
<= cbytes
; i
++)
1309 if (lpdata
[i
] == '\n')
1310 lpstr
[inlcount
++] = '\r';
1312 lpstr
[inlcount
++] = lpdata
[i
];
1315 count
= MultiByteToWideChar(CP_UTF8
, 0, lpstr
, -1, NULL
, 0);
1316 hUnicodeText
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, count
* sizeof(WCHAR
));
1320 WCHAR
*textW
= GlobalLock(hUnicodeText
);
1321 MultiByteToWideChar(CP_UTF8
, 0, lpstr
, -1, textW
, count
);
1322 GlobalUnlock(hUnicodeText
);
1325 HeapFree(GetProcessHeap(), 0, lpstr
);
1328 /* Free the retrieved property data */
1329 HeapFree(GetProcessHeap(), 0, lpdata
);
1331 return hUnicodeText
;
1335 /**************************************************************************
1336 * X11DRV_CLIPBOARD_ImportCompoundText
1338 * Import COMPOUND_TEXT to CF_UNICODE
1340 static HANDLE
X11DRV_CLIPBOARD_ImportCompoundText(Display
*display
, Window w
, Atom prop
)
1345 int srclen
, destlen
;
1346 HANDLE hUnicodeText
;
1347 XTextProperty txtprop
;
1349 if (!X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &txtprop
.value
, &txtprop
.nitems
))
1354 txtprop
.encoding
= x11drv_atom(COMPOUND_TEXT
);
1356 ret
= XmbTextPropertyToTextList(display
, &txtprop
, &srcstr
, &count
);
1357 HeapFree(GetProcessHeap(), 0, txtprop
.value
);
1358 if (ret
!= Success
|| !count
) return 0;
1360 TRACE("Importing %d line(s)\n", count
);
1362 /* Compute number of lines */
1363 srclen
= strlen(srcstr
[0]);
1364 for (i
= 0, lcount
= 0; i
<= srclen
; i
++)
1366 if (srcstr
[0][i
] == '\n')
1370 destlen
= MultiByteToWideChar(CP_UNIXCP
, 0, srcstr
[0], -1, NULL
, 0);
1372 TRACE("lcount = %d, destlen=%d, srcstr %s\n", lcount
, destlen
, srcstr
[0]);
1374 if ((hUnicodeText
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, (destlen
+ lcount
+ 1) * sizeof(WCHAR
))))
1376 WCHAR
*deststr
= GlobalLock(hUnicodeText
);
1377 MultiByteToWideChar(CP_UNIXCP
, 0, srcstr
[0], -1, deststr
, destlen
);
1381 for (i
= destlen
- 1, j
= destlen
+ lcount
- 1; i
>= 0; i
--, j
--)
1383 deststr
[j
] = deststr
[i
];
1385 if (deststr
[i
] == '\n')
1386 deststr
[--j
] = '\r';
1390 GlobalUnlock(hUnicodeText
);
1393 XFreeStringList(srcstr
);
1395 return hUnicodeText
;
1399 /**************************************************************************
1400 * X11DRV_CLIPBOARD_ImportXAPIXMAP
1402 * Import XA_PIXMAP, converting the image to CF_DIB.
1404 static HANDLE
X11DRV_CLIPBOARD_ImportXAPIXMAP(Display
*display
, Window w
, Atom prop
)
1407 unsigned long cbytes
;
1409 HANDLE hClipData
= 0;
1411 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1413 XVisualInfo vis
= default_visual
;
1414 char buffer
[FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )];
1415 BITMAPINFO
*info
= (BITMAPINFO
*)buffer
;
1416 struct gdi_image_bits bits
;
1418 int x
,y
; /* Unused */
1419 unsigned border_width
; /* Unused */
1420 unsigned int depth
, width
, height
;
1422 pPixmap
= (Pixmap
*) lpdata
;
1424 /* Get the Pixmap dimensions and bit depth */
1425 if (!XGetGeometry(gdi_display
, *pPixmap
, &root
, &x
, &y
, &width
, &height
,
1426 &border_width
, &depth
)) depth
= 0;
1427 if (!pixmap_formats
[depth
]) return 0;
1429 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
1430 width
, height
, depth
);
1432 if (depth
!= vis
.depth
) switch (pixmap_formats
[depth
]->bits_per_pixel
)
1438 case 16: /* assume R5G5B5 */
1439 vis
.red_mask
= 0x7c00;
1440 vis
.green_mask
= 0x03e0;
1441 vis
.blue_mask
= 0x001f;
1443 case 24: /* assume R8G8B8 */
1444 case 32: /* assume A8R8G8B8 */
1445 vis
.red_mask
= 0xff0000;
1446 vis
.green_mask
= 0x00ff00;
1447 vis
.blue_mask
= 0x0000ff;
1453 if (!get_pixmap_image( *pPixmap
, width
, height
, &vis
, info
, &bits
))
1455 DWORD info_size
= bitmap_info_size( info
, DIB_RGB_COLORS
);
1458 hClipData
= GlobalAlloc( GMEM_MOVEABLE
| GMEM_DDESHARE
,
1459 info_size
+ info
->bmiHeader
.biSizeImage
);
1462 ptr
= GlobalLock( hClipData
);
1463 memcpy( ptr
, info
, info_size
);
1464 memcpy( ptr
+ info_size
, bits
.ptr
, info
->bmiHeader
.biSizeImage
);
1465 GlobalUnlock( hClipData
);
1467 if (bits
.free
) bits
.free( &bits
);
1470 HeapFree(GetProcessHeap(), 0, lpdata
);
1477 /**************************************************************************
1478 * X11DRV_CLIPBOARD_ImportImageBmp
1480 * Import image/bmp, converting the image to CF_DIB.
1482 static HANDLE
X11DRV_CLIPBOARD_ImportImageBmp(Display
*display
, Window w
, Atom prop
)
1485 unsigned long cbytes
;
1486 HANDLE hClipData
= 0;
1488 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1490 BITMAPFILEHEADER
*bfh
= (BITMAPFILEHEADER
*)lpdata
;
1492 if (cbytes
>= sizeof(BITMAPFILEHEADER
)+sizeof(BITMAPCOREHEADER
) &&
1493 bfh
->bfType
== 0x4d42 /* "BM" */)
1495 BITMAPINFO
*bmi
= (BITMAPINFO
*)(bfh
+1);
1500 hbmp
= CreateDIBitmap(
1504 lpdata
+bfh
->bfOffBits
,
1509 hClipData
= create_dib_from_bitmap( hbmp
);
1515 /* Free the retrieved property data */
1516 HeapFree(GetProcessHeap(), 0, lpdata
);
1523 /**************************************************************************
1524 * X11DRV_CLIPBOARD_ImportMetaFilePict
1526 * Import MetaFilePict.
1528 static HANDLE
X11DRV_CLIPBOARD_ImportMetaFilePict(Display
*display
, Window w
, Atom prop
)
1531 unsigned long cbytes
;
1532 HANDLE hClipData
= 0;
1534 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1537 hClipData
= X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT
, lpdata
, (LPDWORD
)&cbytes
, FALSE
);
1539 /* Free the retrieved property data */
1540 HeapFree(GetProcessHeap(), 0, lpdata
);
1547 /**************************************************************************
1548 * X11DRV_ImportEnhMetaFile
1550 * Import EnhMetaFile.
1552 static HANDLE
X11DRV_CLIPBOARD_ImportEnhMetaFile(Display
*display
, Window w
, Atom prop
)
1555 unsigned long cbytes
;
1556 HANDLE hClipData
= 0;
1558 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1561 hClipData
= X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE
, lpdata
, (LPDWORD
)&cbytes
, FALSE
);
1563 /* Free the retrieved property data */
1564 HeapFree(GetProcessHeap(), 0, lpdata
);
1571 /**************************************************************************
1572 * X11DRV_CLIPBOARD_ImportTextUriList
1574 * Import text/uri-list.
1576 static HANDLE
X11DRV_CLIPBOARD_ImportTextUriList(Display
*display
, Window w
, Atom prop
)
1584 int capacity
= 4096;
1587 HANDLE handle
= NULL
;
1589 if (!X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, (LPBYTE
*)&uriList
, &len
))
1592 out
= HeapAlloc(GetProcessHeap(), 0, capacity
* sizeof(WCHAR
));
1598 while (end
< len
&& uriList
[end
] != '\r')
1600 if (end
< (len
- 1) && uriList
[end
+1] != '\n')
1602 WARN("URI list line doesn't end in \\r\\n\n");
1606 uri
= HeapAlloc(GetProcessHeap(), 0, end
- start
+ 1);
1609 lstrcpynA(uri
, &uriList
[start
], end
- start
+ 1);
1610 path
= uri_to_dos(uri
);
1611 TRACE("converted URI %s to DOS path %s\n", debugstr_a(uri
), debugstr_w(path
));
1612 HeapFree(GetProcessHeap(), 0, uri
);
1616 int pathSize
= strlenW(path
) + 1;
1617 if (pathSize
> capacity
-size
)
1619 capacity
= 2*capacity
+ pathSize
;
1620 out
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, out
, (capacity
+ 1)*sizeof(WCHAR
));
1624 memcpy(&out
[size
], path
, pathSize
* sizeof(WCHAR
));
1627 HeapFree(GetProcessHeap(), 0, path
);
1635 if (out
&& end
>= len
)
1637 DROPFILES
*dropFiles
;
1638 handle
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, sizeof(DROPFILES
) + (size
+ 1)*sizeof(WCHAR
));
1641 dropFiles
= (DROPFILES
*) GlobalLock(handle
);
1642 dropFiles
->pFiles
= sizeof(DROPFILES
);
1643 dropFiles
->pt
.x
= 0;
1644 dropFiles
->pt
.y
= 0;
1646 dropFiles
->fWide
= TRUE
;
1648 memcpy(((char*)dropFiles
) + dropFiles
->pFiles
, out
, (size
+ 1)*sizeof(WCHAR
));
1649 GlobalUnlock(handle
);
1652 HeapFree(GetProcessHeap(), 0, out
);
1657 /**************************************************************************
1658 * X11DRV_ImportClipbordaData
1660 * Generic import clipboard data routine.
1662 static HANDLE
X11DRV_CLIPBOARD_ImportClipboardData(Display
*display
, Window w
, Atom prop
)
1666 unsigned long cbytes
;
1667 HANDLE hClipData
= 0;
1669 if (X11DRV_CLIPBOARD_ReadProperty(display
, w
, prop
, &lpdata
, &cbytes
))
1673 /* Turn on the DDESHARE flag to enable shared 32 bit memory */
1674 hClipData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cbytes
);
1677 HeapFree(GetProcessHeap(), 0, lpdata
);
1681 if ((lpClipData
= GlobalLock(hClipData
)))
1683 memcpy(lpClipData
, lpdata
, cbytes
);
1684 GlobalUnlock(hClipData
);
1688 GlobalFree(hClipData
);
1693 /* Free the retrieved property data */
1694 HeapFree(GetProcessHeap(), 0, lpdata
);
1700 /**************************************************************************
1701 * X11DRV_CLIPBOARD_ImportSelection
1703 * Import the X selection into the clipboard format registered for the given X target.
1705 HANDLE
X11DRV_CLIPBOARD_ImportSelection(Display
*d
, Atom target
, Window w
, Atom prop
, UINT
*windowsFormat
)
1707 WINE_CLIPFORMAT
*clipFormat
;
1709 clipFormat
= X11DRV_CLIPBOARD_LookupProperty(NULL
, target
);
1712 *windowsFormat
= clipFormat
->wFormatID
;
1713 return clipFormat
->lpDrvImportFunc(d
, w
, prop
);
1719 /**************************************************************************
1720 X11DRV_CLIPBOARD_ExportClipboardData
1722 * Generic export clipboard data routine.
1724 static HANDLE
X11DRV_CLIPBOARD_ExportClipboardData(Display
*display
, Window requestor
, Atom aTarget
,
1725 Atom rprop
, LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1729 HANDLE hClipData
= 0;
1731 *lpBytes
= 0; /* Assume failure */
1733 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpData
))
1734 ERR("Failed to export %04x format\n", lpData
->wFormatID
);
1737 datasize
= GlobalSize(lpData
->hData
);
1739 hClipData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, datasize
);
1740 if (hClipData
== 0) return NULL
;
1742 if ((lpClipData
= GlobalLock(hClipData
)))
1744 LPVOID lpdata
= GlobalLock(lpData
->hData
);
1746 memcpy(lpClipData
, lpdata
, datasize
);
1747 *lpBytes
= datasize
;
1749 GlobalUnlock(lpData
->hData
);
1750 GlobalUnlock(hClipData
);
1752 GlobalFree(hClipData
);
1761 /**************************************************************************
1762 * X11DRV_CLIPBOARD_ExportXAString
1764 * Export CF_TEXT converting the string to XA_STRING.
1765 * Helper function for X11DRV_CLIPBOARD_ExportString.
1767 static HANDLE
X11DRV_CLIPBOARD_ExportXAString(LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1771 LPSTR text
, lpstr
= NULL
;
1773 *lpBytes
= 0; /* Assume return has zero bytes */
1775 text
= GlobalLock(lpData
->hData
);
1776 size
= strlen(text
);
1778 /* remove carriage returns */
1779 lpstr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
+ 1);
1783 for (i
= 0,j
= 0; i
< size
&& text
[i
]; i
++)
1785 if (text
[i
] == '\r' && (text
[i
+1] == '\n' || text
[i
+1] == '\0'))
1787 lpstr
[j
++] = text
[i
];
1791 *lpBytes
= j
; /* Number of bytes in string */
1794 GlobalUnlock(lpData
->hData
);
1800 /**************************************************************************
1801 * X11DRV_CLIPBOARD_ExportUTF8String
1803 * Export CF_UNICODE converting the string to UTF8.
1804 * Helper function for X11DRV_CLIPBOARD_ExportString.
1806 static HANDLE
X11DRV_CLIPBOARD_ExportUTF8String(LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1811 LPSTR text
, lpstr
= NULL
;
1813 *lpBytes
= 0; /* Assume return has zero bytes */
1815 uni_text
= GlobalLock(lpData
->hData
);
1817 size
= WideCharToMultiByte(CP_UTF8
, 0, uni_text
, -1, NULL
, 0, NULL
, NULL
);
1819 text
= HeapAlloc(GetProcessHeap(), 0, size
);
1822 WideCharToMultiByte(CP_UTF8
, 0, uni_text
, -1, text
, size
, NULL
, NULL
);
1824 /* remove carriage returns */
1825 lpstr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
--);
1829 for (i
= 0,j
= 0; i
< size
&& text
[i
]; i
++)
1831 if (text
[i
] == '\r' && (text
[i
+1] == '\n' || text
[i
+1] == '\0'))
1833 lpstr
[j
++] = text
[i
];
1837 *lpBytes
= j
; /* Number of bytes in string */
1840 HeapFree(GetProcessHeap(), 0, text
);
1841 GlobalUnlock(lpData
->hData
);
1848 /**************************************************************************
1849 * X11DRV_CLIPBOARD_ExportCompoundText
1851 * Export CF_UNICODE to COMPOUND_TEXT
1852 * Helper function for X11DRV_CLIPBOARD_ExportString.
1854 static HANDLE
X11DRV_CLIPBOARD_ExportCompoundText(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1855 LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1859 XICCEncodingStyle style
;
1864 uni_text
= GlobalLock(lpData
->hData
);
1866 size
= WideCharToMultiByte(CP_UNIXCP
, 0, uni_text
, -1, NULL
, 0, NULL
, NULL
);
1867 lpstr
= HeapAlloc(GetProcessHeap(), 0, size
);
1871 WideCharToMultiByte(CP_UNIXCP
, 0, uni_text
, -1, lpstr
, size
, NULL
, NULL
);
1873 /* remove carriage returns */
1874 for (i
= 0, j
= 0; i
< size
&& lpstr
[i
]; i
++)
1876 if (lpstr
[i
] == '\r' && (lpstr
[i
+1] == '\n' || lpstr
[i
+1] == '\0'))
1878 lpstr
[j
++] = lpstr
[i
];
1882 GlobalUnlock(lpData
->hData
);
1884 if (aTarget
== x11drv_atom(COMPOUND_TEXT
))
1885 style
= XCompoundTextStyle
;
1887 style
= XStdICCTextStyle
;
1889 /* Update the X property */
1890 if (XmbTextListToTextProperty(display
, &lpstr
, 1, style
, &prop
) == Success
)
1892 XSetTextProperty(display
, requestor
, &prop
, rprop
);
1896 HeapFree(GetProcessHeap(), 0, lpstr
);
1901 /**************************************************************************
1902 * X11DRV_CLIPBOARD_ExportString
1906 static HANDLE
X11DRV_CLIPBOARD_ExportString(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1907 LPWINE_CLIPDATA lpData
, LPDWORD lpBytes
)
1909 if (X11DRV_CLIPBOARD_RenderFormat(display
, lpData
))
1911 if (aTarget
== XA_STRING
)
1912 return X11DRV_CLIPBOARD_ExportXAString(lpData
, lpBytes
);
1913 else if (aTarget
== x11drv_atom(COMPOUND_TEXT
) || aTarget
== x11drv_atom(TEXT
))
1914 return X11DRV_CLIPBOARD_ExportCompoundText(display
, requestor
, aTarget
,
1915 rprop
, lpData
, lpBytes
);
1918 TRACE("Exporting target %ld to default UTF8_STRING\n", aTarget
);
1919 return X11DRV_CLIPBOARD_ExportUTF8String(lpData
, lpBytes
);
1923 ERR("Failed to render %04x format\n", lpData
->wFormatID
);
1929 /**************************************************************************
1930 * X11DRV_CLIPBOARD_ExportXAPIXMAP
1932 * Export CF_DIB to XA_PIXMAP.
1934 static HANDLE
X11DRV_CLIPBOARD_ExportXAPIXMAP(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1935 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1938 unsigned char* lpData
;
1940 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1942 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1946 if (!lpdata
->drvData
) /* If not already rendered */
1950 struct gdi_image_bits bits
;
1952 pbmi
= GlobalLock( lpdata
->hData
);
1953 bits
.ptr
= (LPBYTE
)pbmi
+ bitmap_info_size( pbmi
, DIB_RGB_COLORS
);
1955 bits
.is_copy
= FALSE
;
1956 pixmap
= create_pixmap_from_image( 0, &default_visual
, pbmi
, &bits
, DIB_RGB_COLORS
);
1957 GlobalUnlock( lpdata
->hData
);
1958 lpdata
->drvData
= pixmap
;
1961 *lpBytes
= sizeof(Pixmap
); /* pixmap is a 32bit value */
1963 /* Wrap pixmap so we can return a handle */
1964 hData
= GlobalAlloc(0, *lpBytes
);
1965 lpData
= GlobalLock(hData
);
1966 memcpy(lpData
, &lpdata
->drvData
, *lpBytes
);
1967 GlobalUnlock(hData
);
1973 /**************************************************************************
1974 * X11DRV_CLIPBOARD_ExportImageBmp
1976 * Export CF_DIB to image/bmp.
1978 static HANDLE
X11DRV_CLIPBOARD_ExportImageBmp(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
1979 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
1986 BITMAPFILEHEADER
*bfh
;
1990 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
1992 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
1996 hpackeddib
= lpdata
->hData
;
1998 dibdata
= GlobalLock(hpackeddib
);
2001 ERR("Failed to lock packed DIB\n");
2005 bmpsize
= sizeof(BITMAPFILEHEADER
) + GlobalSize(hpackeddib
);
2007 hbmpdata
= GlobalAlloc(0, bmpsize
);
2011 bmpdata
= GlobalLock(hbmpdata
);
2015 GlobalFree(hbmpdata
);
2016 GlobalUnlock(hpackeddib
);
2020 /* bitmap file header */
2021 bfh
= (BITMAPFILEHEADER
*)bmpdata
;
2022 bfh
->bfType
= 0x4d42; /* "BM" */
2023 bfh
->bfSize
= bmpsize
;
2024 bfh
->bfReserved1
= 0;
2025 bfh
->bfReserved2
= 0;
2026 bfh
->bfOffBits
= sizeof(BITMAPFILEHEADER
) + bitmap_info_size((BITMAPINFO
*)dibdata
, DIB_RGB_COLORS
);
2028 /* rest of bitmap is the same as the packed dib */
2029 memcpy(bfh
+1, dibdata
, bmpsize
-sizeof(BITMAPFILEHEADER
));
2033 GlobalUnlock(hbmpdata
);
2036 GlobalUnlock(hpackeddib
);
2042 /**************************************************************************
2043 * X11DRV_CLIPBOARD_ExportMetaFilePict
2045 * Export MetaFilePict.
2047 static HANDLE
X11DRV_CLIPBOARD_ExportMetaFilePict(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
2048 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
2050 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
2052 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
2056 return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT
, lpdata
->hData
, lpBytes
, TRUE
);
2060 /**************************************************************************
2061 * X11DRV_CLIPBOARD_ExportEnhMetaFile
2063 * Export EnhMetaFile.
2065 static HANDLE
X11DRV_CLIPBOARD_ExportEnhMetaFile(Display
*display
, Window requestor
, Atom aTarget
, Atom rprop
,
2066 LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
2068 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
2070 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
2074 return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE
, lpdata
->hData
, lpBytes
, TRUE
);
2078 /**************************************************************************
2079 * get_html_description_field
2081 * Find the value of a field in an HTML Format description.
2083 static LPCSTR
get_html_description_field(LPCSTR data
, LPCSTR keyword
)
2087 while (pos
&& *pos
&& *pos
!= '<')
2089 if (memcmp(pos
, keyword
, strlen(keyword
)) == 0)
2090 return pos
+strlen(keyword
);
2092 pos
= strchr(pos
, '\n');
2100 /**************************************************************************
2101 * X11DRV_CLIPBOARD_ExportTextHtml
2103 * Export HTML Format to text/html.
2105 * FIXME: We should attempt to add an <a base> tag and convert windows paths.
2107 static HANDLE
X11DRV_CLIPBOARD_ExportTextHtml(Display
*display
, Window requestor
, Atom aTarget
,
2108 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
2111 LPCSTR data
, field_value
;
2112 UINT fragmentstart
, fragmentend
, htmlsize
;
2113 HANDLE hhtmldata
=NULL
;
2118 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
2120 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
2124 hdata
= lpdata
->hData
;
2126 data
= GlobalLock(hdata
);
2129 ERR("Failed to lock HTML Format data\n");
2133 /* read the important fields */
2134 field_value
= get_html_description_field(data
, "StartFragment:");
2137 ERR("Couldn't find StartFragment value\n");
2140 fragmentstart
= atoi(field_value
);
2142 field_value
= get_html_description_field(data
, "EndFragment:");
2145 ERR("Couldn't find EndFragment value\n");
2148 fragmentend
= atoi(field_value
);
2150 /* export only the fragment */
2151 htmlsize
= fragmentend
- fragmentstart
+ 1;
2153 hhtmldata
= GlobalAlloc(0, htmlsize
);
2157 htmldata
= GlobalLock(hhtmldata
);
2161 GlobalFree(hhtmldata
);
2166 memcpy(htmldata
, &data
[fragmentstart
], fragmentend
-fragmentstart
);
2167 htmldata
[htmlsize
-1] = '\0';
2169 *lpBytes
= htmlsize
;
2171 GlobalUnlock(htmldata
);
2176 GlobalUnlock(hdata
);
2182 /**************************************************************************
2183 * X11DRV_CLIPBOARD_ExportHDROP
2185 * Export CF_HDROP format to text/uri-list.
2187 static HANDLE
X11DRV_CLIPBOARD_ExportHDROP(Display
*display
, Window requestor
, Atom aTarget
,
2188 Atom rprop
, LPWINE_CLIPDATA lpdata
, LPDWORD lpBytes
)
2193 HGLOBAL hClipData
= NULL
;
2194 char *textUriList
= NULL
;
2195 UINT textUriListSize
= 32;
2200 if (!X11DRV_CLIPBOARD_RenderFormat(display
, lpdata
))
2202 ERR("Failed to export %04x format\n", lpdata
->wFormatID
);
2205 hClipData
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, textUriListSize
);
2206 if (hClipData
== NULL
)
2208 hDrop
= (HDROP
) lpdata
->hData
;
2209 numFiles
= DragQueryFileW(hDrop
, 0xFFFFFFFF, NULL
, 0);
2210 for (i
= 0; i
< numFiles
; i
++)
2212 UINT dosFilenameSize
;
2213 WCHAR
*dosFilename
= NULL
;
2214 char *unixFilename
= NULL
;
2218 dosFilenameSize
= 1 + DragQueryFileW(hDrop
, i
, NULL
, 0);
2219 dosFilename
= HeapAlloc(GetProcessHeap(), 0, dosFilenameSize
*sizeof(WCHAR
));
2220 if (dosFilename
== NULL
) goto failed
;
2221 DragQueryFileW(hDrop
, i
, dosFilename
, dosFilenameSize
);
2222 unixFilename
= wine_get_unix_file_name(dosFilename
);
2223 HeapFree(GetProcessHeap(), 0, dosFilename
);
2224 if (unixFilename
== NULL
) goto failed
;
2225 uriSize
= 8 + /* file:/// */
2226 3 * (lstrlenA(unixFilename
) - 1) + /* "%xy" per char except first '/' */
2228 if ((next
+ uriSize
) > textUriListSize
)
2230 UINT biggerSize
= max( 2 * textUriListSize
, next
+ uriSize
);
2231 HGLOBAL bigger
= GlobalReAlloc(hClipData
, biggerSize
, 0);
2235 textUriListSize
= biggerSize
;
2239 HeapFree(GetProcessHeap(), 0, unixFilename
);
2243 textUriList
= GlobalLock(hClipData
);
2244 lstrcpyA(&textUriList
[next
], "file:///");
2246 /* URL encode everything - unnecessary, but easier/lighter than linking in shlwapi, and can't hurt */
2247 for (u
= 1; unixFilename
[u
]; u
++)
2249 static const char hex_table
[] = "0123456789abcdef";
2250 textUriList
[next
++] = '%';
2251 textUriList
[next
++] = hex_table
[unixFilename
[u
] >> 4];
2252 textUriList
[next
++] = hex_table
[unixFilename
[u
] & 0xf];
2254 textUriList
[next
++] = '\r';
2255 textUriList
[next
++] = '\n';
2256 GlobalUnlock(hClipData
);
2257 HeapFree(GetProcessHeap(), 0, unixFilename
);
2264 GlobalFree(hClipData
);
2270 /**************************************************************************
2271 * X11DRV_CLIPBOARD_QueryTargets
2273 static BOOL
X11DRV_CLIPBOARD_QueryTargets(Display
*display
, Window w
, Atom selection
,
2274 Atom target
, XEvent
*xe
)
2278 XConvertSelection(display
, selection
, target
, x11drv_atom(SELECTION_DATA
), w
, CurrentTime
);
2281 * Wait until SelectionNotify is received
2283 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
2285 Bool res
= XCheckTypedWindowEvent(display
, w
, SelectionNotify
, xe
);
2286 if (res
&& xe
->xselection
.selection
== selection
) break;
2288 usleep(SELECTION_WAIT
);
2291 if (i
== SELECTION_RETRIES
)
2293 ERR("Timed out waiting for SelectionNotify event\n");
2296 /* Verify that the selection returned a valid TARGETS property */
2297 if ((xe
->xselection
.target
!= target
) || (xe
->xselection
.property
== None
))
2299 /* Selection owner failed to respond or we missed the SelectionNotify */
2300 WARN("Failed to retrieve TARGETS for selection %ld.\n", selection
);
2308 static int is_atom_error( Display
*display
, XErrorEvent
*event
, void *arg
)
2310 return (event
->error_code
== BadAtom
);
2313 /**************************************************************************
2314 * X11DRV_CLIPBOARD_InsertSelectionProperties
2316 * Mark properties available for future retrieval.
2318 static VOID
X11DRV_CLIPBOARD_InsertSelectionProperties(Display
*display
, Atom
* properties
, UINT count
)
2320 UINT i
, nb_atoms
= 0;
2323 /* Cache these formats in the clipboard cache */
2324 for (i
= 0; i
< count
; i
++)
2326 LPWINE_CLIPFORMAT lpFormat
= X11DRV_CLIPBOARD_LookupProperty(NULL
, properties
[i
]);
2330 /* We found at least one Window's format that mapps to the property.
2331 * Continue looking for more.
2333 * If more than one property map to a Window's format then we use the first
2334 * one and ignore the rest.
2338 TRACE("Atom#%d Property(%d): --> Format %s\n",
2339 i
, lpFormat
->drvData
, debugstr_format(lpFormat
->wFormatID
));
2340 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat
->wFormatID
, 0, 0, lpFormat
, FALSE
);
2341 lpFormat
= X11DRV_CLIPBOARD_LookupProperty(lpFormat
, properties
[i
]);
2344 else if (properties
[i
])
2346 /* add it to the list of atoms that we don't know about yet */
2347 if (!atoms
) atoms
= HeapAlloc( GetProcessHeap(), 0,
2348 (count
- i
) * sizeof(*atoms
) );
2349 if (atoms
) atoms
[nb_atoms
++] = properties
[i
];
2353 /* query all unknown atoms in one go */
2356 char **names
= HeapAlloc( GetProcessHeap(), 0, nb_atoms
* sizeof(*names
) );
2359 X11DRV_expect_error( display
, is_atom_error
, NULL
);
2360 if (!XGetAtomNames( display
, atoms
, nb_atoms
, names
)) nb_atoms
= 0;
2361 if (X11DRV_check_error())
2363 WARN( "got some bad atoms, ignoring\n" );
2366 for (i
= 0; i
< nb_atoms
; i
++)
2368 WINE_CLIPFORMAT
*lpFormat
;
2370 int len
= MultiByteToWideChar(CP_UNIXCP
, 0, names
[i
], -1, NULL
, 0);
2371 wname
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
2372 MultiByteToWideChar(CP_UNIXCP
, 0, names
[i
], -1, wname
, len
);
2374 lpFormat
= register_format( RegisterClipboardFormatW(wname
), atoms
[i
] );
2375 HeapFree(GetProcessHeap(), 0, wname
);
2378 ERR("Failed to register %s property. Type will not be cached.\n", names
[i
]);
2381 TRACE("Atom#%d Property(%d): --> Format %s\n",
2382 i
, lpFormat
->drvData
, debugstr_format(lpFormat
->wFormatID
));
2383 X11DRV_CLIPBOARD_InsertClipboardData(lpFormat
->wFormatID
, 0, 0, lpFormat
, FALSE
);
2385 for (i
= 0; i
< nb_atoms
; i
++) XFree( names
[i
] );
2386 HeapFree( GetProcessHeap(), 0, names
);
2388 HeapFree( GetProcessHeap(), 0, atoms
);
2393 /**************************************************************************
2394 * X11DRV_CLIPBOARD_QueryAvailableData
2396 * Caches the list of data formats available from the current selection.
2397 * This queries the selection owner for the TARGETS property and saves all
2398 * reported property types.
2400 static int X11DRV_CLIPBOARD_QueryAvailableData(Display
*display
, LPCLIPBOARDINFO lpcbinfo
)
2403 Atom atype
=AnyPropertyType
;
2405 unsigned long remain
;
2406 Atom
* targetList
=NULL
;
2408 unsigned long cSelectionTargets
= 0;
2410 if (selectionAcquired
& (S_PRIMARY
| S_CLIPBOARD
))
2412 ERR("Received request to cache selection but process is owner=(%08x)\n",
2413 (unsigned) selectionWindow
);
2414 return -1; /* Prevent self request */
2417 w
= thread_selection_wnd();
2420 ERR("No window available to retrieve selection!\n");
2425 * Query the selection owner for the TARGETS property
2427 if ((use_primary_selection
&& XGetSelectionOwner(display
,XA_PRIMARY
)) ||
2428 XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)))
2430 if (use_primary_selection
&& (X11DRV_CLIPBOARD_QueryTargets(display
, w
, XA_PRIMARY
, x11drv_atom(TARGETS
), &xe
)))
2431 selectionCacheSrc
= XA_PRIMARY
;
2432 else if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, x11drv_atom(CLIPBOARD
), x11drv_atom(TARGETS
), &xe
))
2433 selectionCacheSrc
= x11drv_atom(CLIPBOARD
);
2436 Atom xstr
= XA_STRING
;
2438 /* Selection Owner doesn't understand TARGETS, try retrieving XA_STRING */
2439 if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, XA_PRIMARY
, XA_STRING
, &xe
))
2441 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, &xstr
, 1);
2442 selectionCacheSrc
= XA_PRIMARY
;
2445 else if (X11DRV_CLIPBOARD_QueryTargets(display
, w
, x11drv_atom(CLIPBOARD
), XA_STRING
, &xe
))
2447 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, &xstr
, 1);
2448 selectionCacheSrc
= x11drv_atom(CLIPBOARD
);
2453 WARN("Failed to query selection owner for available data.\n");
2458 else return 0; /* No selection owner so report 0 targets available */
2460 /* Read the TARGETS property contents */
2461 if (!XGetWindowProperty(display
, xe
.xselection
.requestor
, xe
.xselection
.property
,
2462 0, 0x3FFF, True
, AnyPropertyType
/*XA_ATOM*/, &atype
, &aformat
, &cSelectionTargets
,
2463 &remain
, (unsigned char**)&targetList
) != Success
)
2465 TRACE("Type %lx,Format %d,nItems %ld, Remain %ld\n",
2466 atype
, aformat
, cSelectionTargets
, remain
);
2468 * The TARGETS property should have returned us a list of atoms
2469 * corresponding to each selection target format supported.
2471 if (atype
== XA_ATOM
|| atype
== x11drv_atom(TARGETS
))
2475 X11DRV_CLIPBOARD_InsertSelectionProperties(display
, targetList
, cSelectionTargets
);
2477 else if (aformat
== 8) /* work around quartz-wm brain damage */
2479 unsigned long i
, count
= cSelectionTargets
/ sizeof(CARD32
);
2480 Atom
*atoms
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(Atom
) );
2481 for (i
= 0; i
< count
; i
++)
2482 atoms
[i
] = ((CARD32
*)targetList
)[i
]; /* FIXME: byte swapping */
2483 X11DRV_CLIPBOARD_InsertSelectionProperties( display
, atoms
, count
);
2484 HeapFree( GetProcessHeap(), 0, atoms
);
2488 /* Free the list of targets */
2491 else WARN("Failed to read TARGETS property\n");
2493 return cSelectionTargets
;
2497 /**************************************************************************
2498 * X11DRV_CLIPBOARD_ReadSelectionData
2500 * This method is invoked only when we DO NOT own the X selection
2502 * We always get the data from the selection client each time,
2503 * since we have no way of determining if the data in our cache is stale.
2505 static BOOL
X11DRV_CLIPBOARD_ReadSelectionData(Display
*display
, LPWINE_CLIPDATA lpData
)
2512 TRACE("%04x\n", lpData
->wFormatID
);
2514 if (!lpData
->lpFormat
)
2516 ERR("Requesting format %04x but no source format linked to data.\n",
2521 if (!selectionAcquired
)
2523 Window w
= thread_selection_wnd();
2526 ERR("No window available to read selection data!\n");
2530 TRACE("Requesting conversion of %s property (%d) from selection type %08x\n",
2531 debugstr_format(lpData
->lpFormat
->wFormatID
), lpData
->lpFormat
->drvData
,
2532 (UINT
)selectionCacheSrc
);
2534 XConvertSelection(display
, selectionCacheSrc
, lpData
->lpFormat
->drvData
,
2535 x11drv_atom(SELECTION_DATA
), w
, CurrentTime
);
2537 /* wait until SelectionNotify is received */
2538 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
2540 res
= XCheckTypedWindowEvent(display
, w
, SelectionNotify
, &xe
);
2541 if (res
&& xe
.xselection
.selection
== selectionCacheSrc
) break;
2543 usleep(SELECTION_WAIT
);
2546 if (i
== SELECTION_RETRIES
)
2548 ERR("Timed out waiting for SelectionNotify event\n");
2550 /* Verify that the selection returned a valid TARGETS property */
2551 else if (xe
.xselection
.property
!= None
)
2554 * Read the contents of the X selection property
2555 * into WINE's clipboard cache and converting the
2556 * data format if necessary.
2558 HANDLE hData
= lpData
->lpFormat
->lpDrvImportFunc(display
, xe
.xselection
.requestor
,
2559 xe
.xselection
.property
);
2562 bRet
= X11DRV_CLIPBOARD_InsertClipboardData(lpData
->wFormatID
, hData
, 0, lpData
->lpFormat
, TRUE
);
2564 TRACE("Import function failed\n");
2568 TRACE("Failed to convert selection\n");
2573 ERR("Received request to cache selection data but process is owner\n");
2576 TRACE("Returning %d\n", bRet
);
2582 /**************************************************************************
2583 * X11DRV_CLIPBOARD_GetProperty
2584 * Gets type, data and size.
2586 static BOOL
X11DRV_CLIPBOARD_GetProperty(Display
*display
, Window w
, Atom prop
,
2587 Atom
*atype
, unsigned char** data
, unsigned long* datasize
)
2590 unsigned long pos
= 0, nitems
, remain
, count
;
2591 unsigned char *val
= NULL
, *buffer
;
2593 TRACE("Reading property %lu from X window %lx\n", prop
, w
);
2597 if (XGetWindowProperty(display
, w
, prop
, pos
, INT_MAX
/ 4, False
,
2598 AnyPropertyType
, atype
, &aformat
, &nitems
, &remain
, &buffer
) != Success
)
2600 WARN("Failed to read property\n");
2601 HeapFree( GetProcessHeap(), 0, val
);
2605 count
= get_property_size( aformat
, nitems
);
2606 if (!val
) *data
= HeapAlloc( GetProcessHeap(), 0, pos
* sizeof(int) + count
+ 1 );
2607 else *data
= HeapReAlloc( GetProcessHeap(), 0, val
, pos
* sizeof(int) + count
+ 1 );
2612 HeapFree( GetProcessHeap(), 0, val
);
2616 memcpy( (int *)val
+ pos
, buffer
, count
);
2620 *datasize
= pos
* sizeof(int) + count
;
2624 pos
+= count
/ sizeof(int);
2627 /* Delete the property on the window now that we are done
2628 * This will send a PropertyNotify event to the selection owner. */
2629 XDeleteProperty(display
, w
, prop
);
2634 struct clipboard_data_packet
{
2637 unsigned char *data
;
2640 /**************************************************************************
2641 * X11DRV_CLIPBOARD_ReadProperty
2642 * Reads the contents of the X selection property.
2644 static BOOL
X11DRV_CLIPBOARD_ReadProperty(Display
*display
, Window w
, Atom prop
,
2645 unsigned char** data
, unsigned long* datasize
)
2653 while (XCheckTypedWindowEvent(display
, w
, PropertyNotify
, &xe
))
2656 if (!X11DRV_CLIPBOARD_GetProperty(display
, w
, prop
, &atype
, data
, datasize
))
2659 if (atype
== x11drv_atom(INCR
))
2662 unsigned long bufsize
= 0;
2663 struct list packets
;
2664 struct clipboard_data_packet
*packet
, *packet2
;
2667 HeapFree(GetProcessHeap(), 0, *data
);
2670 list_init(&packets
);
2675 unsigned char *prop_data
;
2676 unsigned long prop_size
;
2678 /* Wait until PropertyNotify is received */
2679 for (i
= 0; i
< SELECTION_RETRIES
; i
++)
2683 res
= XCheckTypedWindowEvent(display
, w
, PropertyNotify
, &xe
);
2684 if (res
&& xe
.xproperty
.atom
== prop
&&
2685 xe
.xproperty
.state
== PropertyNewValue
)
2687 usleep(SELECTION_WAIT
);
2690 if (i
>= SELECTION_RETRIES
||
2691 !X11DRV_CLIPBOARD_GetProperty(display
, w
, prop
, &atype
, &prop_data
, &prop_size
))
2697 /* Retrieved entire data. */
2700 HeapFree(GetProcessHeap(), 0, prop_data
);
2705 packet
= HeapAlloc(GetProcessHeap(), 0, sizeof(*packet
));
2708 HeapFree(GetProcessHeap(), 0, prop_data
);
2713 packet
->size
= prop_size
;
2714 packet
->data
= prop_data
;
2715 list_add_tail(&packets
, &packet
->entry
);
2716 bufsize
+= prop_size
;
2721 buf
= HeapAlloc(GetProcessHeap(), 0, bufsize
+ 1);
2724 unsigned long bytes_copied
= 0;
2725 *datasize
= bufsize
;
2726 LIST_FOR_EACH_ENTRY( packet
, &packets
, struct clipboard_data_packet
, entry
)
2728 memcpy(&buf
[bytes_copied
], packet
->data
, packet
->size
);
2729 bytes_copied
+= packet
->size
;
2738 LIST_FOR_EACH_ENTRY_SAFE( packet
, packet2
, &packets
, struct clipboard_data_packet
, entry
)
2740 HeapFree(GetProcessHeap(), 0, packet
->data
);
2741 HeapFree(GetProcessHeap(), 0, packet
);
2751 /**************************************************************************
2752 * CLIPBOARD_SerializeMetafile
2754 static HANDLE
X11DRV_CLIPBOARD_SerializeMetafile(INT wformat
, HANDLE hdata
, LPDWORD lpcbytes
, BOOL out
)
2758 TRACE(" wFormat=%d hdata=%p out=%d\n", wformat
, hdata
, out
);
2760 if (out
) /* Serialize out, caller should free memory */
2762 *lpcbytes
= 0; /* Assume failure */
2764 if (wformat
== CF_METAFILEPICT
)
2766 LPMETAFILEPICT lpmfp
= GlobalLock(hdata
);
2767 unsigned int size
= GetMetaFileBitsEx(lpmfp
->hMF
, 0, NULL
);
2769 h
= GlobalAlloc(0, size
+ sizeof(METAFILEPICT
));
2772 char *pdata
= GlobalLock(h
);
2774 memcpy(pdata
, lpmfp
, sizeof(METAFILEPICT
));
2775 GetMetaFileBitsEx(lpmfp
->hMF
, size
, pdata
+ sizeof(METAFILEPICT
));
2777 *lpcbytes
= size
+ sizeof(METAFILEPICT
);
2782 GlobalUnlock(hdata
);
2784 else if (wformat
== CF_ENHMETAFILE
)
2786 int size
= GetEnhMetaFileBits(hdata
, 0, NULL
);
2788 h
= GlobalAlloc(0, size
);
2791 LPVOID pdata
= GlobalLock(h
);
2793 GetEnhMetaFileBits(hdata
, size
, pdata
);
2802 if (wformat
== CF_METAFILEPICT
)
2804 h
= GlobalAlloc(0, sizeof(METAFILEPICT
));
2807 unsigned int wiresize
;
2808 LPMETAFILEPICT lpmfp
= GlobalLock(h
);
2810 memcpy(lpmfp
, hdata
, sizeof(METAFILEPICT
));
2811 wiresize
= *lpcbytes
- sizeof(METAFILEPICT
);
2812 lpmfp
->hMF
= SetMetaFileBitsEx(wiresize
,
2813 ((const BYTE
*)hdata
) + sizeof(METAFILEPICT
));
2817 else if (wformat
== CF_ENHMETAFILE
)
2819 h
= SetEnhMetaFileBits(*lpcbytes
, hdata
);
2827 /**************************************************************************
2828 * X11DRV_CLIPBOARD_ReleaseSelection
2830 * Release XA_CLIPBOARD and XA_PRIMARY in response to a SelectionClear event.
2832 static void X11DRV_CLIPBOARD_ReleaseSelection(Display
*display
, Atom selType
, Window w
, HWND hwnd
, Time time
)
2834 /* w is the window that lost the selection
2836 TRACE("event->window = %08x (selectionWindow = %08x) selectionAcquired=0x%08x\n",
2837 (unsigned)w
, (unsigned)selectionWindow
, (unsigned)selectionAcquired
);
2839 if (selectionAcquired
&& (w
== selectionWindow
))
2841 CLIPBOARDINFO cbinfo
;
2843 /* completely give up the selection */
2844 TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
2846 X11DRV_CLIPBOARD_GetClipboardInfo(&cbinfo
);
2848 if (cbinfo
.flags
& CB_PROCESS
)
2850 /* Since we're still the owner, this wasn't initiated by
2851 another Wine process */
2852 if (OpenClipboard(hwnd
))
2854 /* Destroy private objects */
2855 SendMessageW(cbinfo
.hWndOwner
, WM_DESTROYCLIPBOARD
, 0, 0);
2857 /* Give up ownership of the windows clipboard */
2858 X11DRV_CLIPBOARD_ReleaseOwnership();
2863 if ((selType
== x11drv_atom(CLIPBOARD
)) && (selectionAcquired
& S_PRIMARY
))
2865 TRACE("Lost clipboard. Check if we need to release PRIMARY\n");
2867 if (selectionWindow
== XGetSelectionOwner(display
, XA_PRIMARY
))
2869 TRACE("We still own PRIMARY. Releasing PRIMARY.\n");
2870 XSetSelectionOwner(display
, XA_PRIMARY
, None
, time
);
2873 TRACE("We no longer own PRIMARY\n");
2875 else if ((selType
== XA_PRIMARY
) && (selectionAcquired
& S_CLIPBOARD
))
2877 TRACE("Lost PRIMARY. Check if we need to release CLIPBOARD\n");
2879 if (selectionWindow
== XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)))
2881 TRACE("We still own CLIPBOARD. Releasing CLIPBOARD.\n");
2882 XSetSelectionOwner(display
, x11drv_atom(CLIPBOARD
), None
, time
);
2885 TRACE("We no longer own CLIPBOARD\n");
2888 selectionWindow
= None
;
2890 X11DRV_EmptyClipboard(FALSE
);
2892 /* Reset the selection flags now that we are done */
2893 selectionAcquired
= S_NOSELECTION
;
2898 /**************************************************************************
2899 * IsSelectionOwner (X11DRV.@)
2901 * Returns: TRUE if the selection is owned by this process, FALSE otherwise
2903 static BOOL
X11DRV_CLIPBOARD_IsSelectionOwner(void)
2905 return selectionAcquired
;
2909 /**************************************************************************
2910 * X11DRV Clipboard Exports
2911 **************************************************************************/
2914 static void selection_acquire(void)
2919 owner
= thread_selection_wnd();
2920 display
= thread_display();
2922 selectionAcquired
= 0;
2923 selectionWindow
= 0;
2925 /* Grab PRIMARY selection if not owned */
2926 if (use_primary_selection
)
2927 XSetSelectionOwner(display
, XA_PRIMARY
, owner
, CurrentTime
);
2929 /* Grab CLIPBOARD selection if not owned */
2930 XSetSelectionOwner(display
, x11drv_atom(CLIPBOARD
), owner
, CurrentTime
);
2932 if (use_primary_selection
&& XGetSelectionOwner(display
, XA_PRIMARY
) == owner
)
2933 selectionAcquired
|= S_PRIMARY
;
2935 if (XGetSelectionOwner(display
,x11drv_atom(CLIPBOARD
)) == owner
)
2936 selectionAcquired
|= S_CLIPBOARD
;
2938 if (selectionAcquired
)
2940 selectionWindow
= owner
;
2941 TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner
);
2945 static DWORD WINAPI
selection_thread_proc(LPVOID p
)
2951 selection_acquire();
2954 while (selectionAcquired
)
2956 MsgWaitForMultipleObjectsEx(0, NULL
, INFINITE
, QS_SENDMESSAGE
, 0);
2962 /**************************************************************************
2963 * AcquireClipboard (X11DRV.@)
2965 int CDECL
X11DRV_AcquireClipboard(HWND hWndClipWindow
)
2968 HANDLE selectionThread
;
2970 TRACE(" %p\n", hWndClipWindow
);
2973 * It's important that the selection get acquired from the thread
2974 * that owns the clipboard window. The primary reason is that we know
2975 * it is running a message loop and therefore can process the
2976 * X selection events.
2978 if (hWndClipWindow
&&
2979 GetCurrentThreadId() != GetWindowThreadProcessId(hWndClipWindow
, &procid
))
2981 if (procid
!= GetCurrentProcessId())
2983 WARN("Setting clipboard owner to other process is not supported\n");
2984 hWndClipWindow
= NULL
;
2988 TRACE("Thread %x is acquiring selection with thread %x's window %p\n",
2989 GetCurrentThreadId(),
2990 GetWindowThreadProcessId(hWndClipWindow
, NULL
), hWndClipWindow
);
2992 return SendMessageW(hWndClipWindow
, WM_X11DRV_ACQUIRE_SELECTION
, 0, 0);
2998 selection_acquire();
3002 HANDLE event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
3003 selectionThread
= CreateThread(NULL
, 0, selection_thread_proc
, event
, 0, NULL
);
3005 if (!selectionThread
)
3007 WARN("Could not start clipboard thread\n");
3012 WaitForSingleObject(event
, INFINITE
);
3014 CloseHandle(selectionThread
);
3021 /**************************************************************************
3022 * X11DRV_EmptyClipboard
3024 * Empty cached clipboard data.
3026 void CDECL
X11DRV_EmptyClipboard(BOOL keepunowned
)
3028 WINE_CLIPDATA
*data
, *next
;
3030 LIST_FOR_EACH_ENTRY_SAFE( data
, next
, &data_list
, WINE_CLIPDATA
, entry
)
3032 if (keepunowned
&& (data
->wFlags
& CF_FLAG_UNOWNED
)) continue;
3033 list_remove( &data
->entry
);
3034 X11DRV_CLIPBOARD_FreeData( data
);
3035 HeapFree( GetProcessHeap(), 0, data
);
3039 TRACE(" %d entries remaining in cache.\n", ClipDataCount
);
3044 /**************************************************************************
3045 * X11DRV_SetClipboardData
3047 BOOL CDECL
X11DRV_SetClipboardData(UINT wFormat
, HANDLE hData
, BOOL owner
)
3050 BOOL bResult
= TRUE
;
3052 /* If it's not owned, data can only be set if the format data is not already owned
3053 and its rendering is not delayed */
3056 CLIPBOARDINFO cbinfo
;
3057 LPWINE_CLIPDATA lpRender
;
3059 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
3062 ((lpRender
= X11DRV_CLIPBOARD_LookupData(wFormat
)) &&
3063 !(lpRender
->wFlags
& CF_FLAG_UNOWNED
)))
3066 flags
= CF_FLAG_UNOWNED
;
3069 bResult
&= X11DRV_CLIPBOARD_InsertClipboardData(wFormat
, hData
, flags
, NULL
, TRUE
);
3075 /**************************************************************************
3076 * CountClipboardFormats
3078 INT CDECL
X11DRV_CountClipboardFormats(void)
3080 CLIPBOARDINFO cbinfo
;
3082 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
3084 TRACE(" count=%d\n", ClipDataCount
);
3086 return ClipDataCount
;
3090 /**************************************************************************
3091 * X11DRV_EnumClipboardFormats
3093 UINT CDECL
X11DRV_EnumClipboardFormats(UINT wFormat
)
3095 CLIPBOARDINFO cbinfo
;
3096 struct list
*ptr
= NULL
;
3098 TRACE("(%04X)\n", wFormat
);
3100 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
3104 ptr
= list_head( &data_list
);
3108 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(wFormat
);
3109 if (lpData
) ptr
= list_next( &data_list
, &lpData
->entry
);
3113 return LIST_ENTRY( ptr
, WINE_CLIPDATA
, entry
)->wFormatID
;
3117 /**************************************************************************
3118 * X11DRV_IsClipboardFormatAvailable
3120 BOOL CDECL
X11DRV_IsClipboardFormatAvailable(UINT wFormat
)
3123 CLIPBOARDINFO cbinfo
;
3125 TRACE("(%04X)\n", wFormat
);
3127 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
3129 if (wFormat
!= 0 && X11DRV_CLIPBOARD_LookupData(wFormat
))
3132 TRACE("(%04X)- ret(%d)\n", wFormat
, bRet
);
3138 /**************************************************************************
3139 * GetClipboardData (USER.142)
3141 HANDLE CDECL
X11DRV_GetClipboardData(UINT wFormat
)
3143 CLIPBOARDINFO cbinfo
;
3144 LPWINE_CLIPDATA lpRender
;
3146 TRACE("(%04X)\n", wFormat
);
3148 X11DRV_CLIPBOARD_UpdateCache(&cbinfo
);
3150 if ((lpRender
= X11DRV_CLIPBOARD_LookupData(wFormat
)))
3152 if ( !lpRender
->hData
)
3153 X11DRV_CLIPBOARD_RenderFormat(thread_init_display(), lpRender
);
3155 TRACE(" returning %p (type %04x)\n", lpRender
->hData
, lpRender
->wFormatID
);
3156 return lpRender
->hData
;
3163 /**************************************************************************
3164 * ResetSelectionOwner
3166 * Called when the thread owning the selection is destroyed and we need to
3167 * preserve the selection ownership. We look for another top level window
3168 * in this process and send it a message to acquire the selection.
3170 void X11DRV_ResetSelectionOwner(void)
3177 if (!selectionAcquired
|| thread_selection_wnd() != selectionWindow
)
3180 selectionAcquired
= S_NOSELECTION
;
3181 selectionWindow
= 0;
3183 hwnd
= GetWindow(GetDesktopWindow(), GW_CHILD
);
3186 if (GetCurrentThreadId() != GetWindowThreadProcessId(hwnd
, &procid
))
3188 if (GetCurrentProcessId() == procid
)
3190 if (SendMessageW(hwnd
, WM_X11DRV_ACQUIRE_SELECTION
, 0, 0))
3194 } while ((hwnd
= GetWindow(hwnd
, GW_HWNDNEXT
)) != NULL
);
3196 WARN("Failed to find another thread to take selection ownership. Clipboard data will be lost.\n");
3198 X11DRV_CLIPBOARD_ReleaseOwnership();
3199 X11DRV_EmptyClipboard(FALSE
);
3203 /**************************************************************************
3204 * X11DRV_CLIPBOARD_SynthesizeData
3206 static BOOL
X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID
)
3209 LPWINE_CLIPDATA lpSource
= NULL
;
3211 TRACE(" %04x\n", wFormatID
);
3213 /* Don't need to synthesize if it already exists */
3214 if (X11DRV_CLIPBOARD_LookupData(wFormatID
))
3217 if (wFormatID
== CF_UNICODETEXT
|| wFormatID
== CF_TEXT
|| wFormatID
== CF_OEMTEXT
)
3219 bsyn
= ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT
)) &&
3220 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) ||
3221 ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_TEXT
)) &&
3222 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
) ||
3223 ((lpSource
= X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT
)) &&
3224 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
);
3226 else if (wFormatID
== CF_ENHMETAFILE
)
3228 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT
)) &&
3229 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
3231 else if (wFormatID
== CF_METAFILEPICT
)
3233 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_ENHMETAFILE
)) &&
3234 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
3236 else if (wFormatID
== CF_DIB
)
3238 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_BITMAP
)) &&
3239 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
3241 else if (wFormatID
== CF_BITMAP
)
3243 bsyn
= (lpSource
= X11DRV_CLIPBOARD_LookupData(CF_DIB
)) &&
3244 ~lpSource
->wFlags
& CF_FLAG_SYNTHESIZED
;
3248 X11DRV_CLIPBOARD_InsertClipboardData(wFormatID
, 0, CF_FLAG_SYNTHESIZED
, NULL
, TRUE
);
3255 /**************************************************************************
3256 * X11DRV_EndClipboardUpdate
3258 * Add locale if it hasn't already been added
3260 void CDECL
X11DRV_EndClipboardUpdate(void)
3262 INT count
= ClipDataCount
;
3264 /* Do Unicode <-> Text <-> OEM mapping */
3265 X11DRV_CLIPBOARD_SynthesizeData(CF_TEXT
);
3266 X11DRV_CLIPBOARD_SynthesizeData(CF_OEMTEXT
);
3267 X11DRV_CLIPBOARD_SynthesizeData(CF_UNICODETEXT
);
3269 /* Enhmetafile <-> MetafilePict mapping */
3270 X11DRV_CLIPBOARD_SynthesizeData(CF_ENHMETAFILE
);
3271 X11DRV_CLIPBOARD_SynthesizeData(CF_METAFILEPICT
);
3273 /* DIB <-> Bitmap mapping */
3274 X11DRV_CLIPBOARD_SynthesizeData(CF_DIB
);
3275 X11DRV_CLIPBOARD_SynthesizeData(CF_BITMAP
);
3277 TRACE("%d formats added to cached data\n", ClipDataCount
- count
);
3281 /***********************************************************************
3282 * X11DRV_SelectionRequest_TARGETS
3283 * Service a TARGETS selection request event
3285 static Atom
X11DRV_SelectionRequest_TARGETS( Display
*display
, Window requestor
,
3286 Atom target
, Atom rprop
)
3291 LPWINE_CLIPFORMAT format
;
3292 LPWINE_CLIPDATA lpData
;
3294 /* Create X atoms for any clipboard types which don't have atoms yet.
3295 * This avoids sending bogus zero atoms.
3296 * Without this, copying might not have access to all clipboard types.
3297 * FIXME: is it safe to call this here?
3302 * Count the number of items we wish to expose as selection targets.
3304 cTargets
= 1; /* Include TARGETS */
3306 if (!list_head( &data_list
)) return None
;
3308 LIST_FOR_EACH_ENTRY( lpData
, &data_list
, WINE_CLIPDATA
, entry
)
3309 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
3310 if ((format
->wFormatID
== lpData
->wFormatID
) &&
3311 format
->lpDrvExportFunc
&& format
->drvData
)
3314 TRACE(" found %d formats\n", cTargets
);
3316 /* Allocate temp buffer */
3317 targets
= HeapAlloc( GetProcessHeap(), 0, cTargets
* sizeof(Atom
));
3322 targets
[i
++] = x11drv_atom(TARGETS
);
3324 LIST_FOR_EACH_ENTRY( lpData
, &data_list
, WINE_CLIPDATA
, entry
)
3325 LIST_FOR_EACH_ENTRY( format
, &format_list
, WINE_CLIPFORMAT
, entry
)
3326 if ((format
->wFormatID
== lpData
->wFormatID
) &&
3327 format
->lpDrvExportFunc
&& format
->drvData
)
3328 targets
[i
++] = format
->drvData
;
3330 if (TRACE_ON(clipboard
))
3333 for ( i
= 0; i
< cTargets
; i
++)
3335 char *itemFmtName
= XGetAtomName(display
, targets
[i
]);
3336 TRACE("\tAtom# %d: Property %ld Type %s\n", i
, targets
[i
], itemFmtName
);
3341 /* We may want to consider setting the type to xaTargets instead,
3342 * in case some apps expect this instead of XA_ATOM */
3343 XChangeProperty(display
, requestor
, rprop
, XA_ATOM
, 32,
3344 PropModeReplace
, (unsigned char *)targets
, cTargets
);
3346 HeapFree(GetProcessHeap(), 0, targets
);
3352 /***********************************************************************
3353 * X11DRV_SelectionRequest_MULTIPLE
3354 * Service a MULTIPLE selection request event
3355 * rprop contains a list of (target,property) atom pairs.
3356 * The first atom names a target and the second names a property.
3357 * The effect is as if we have received a sequence of SelectionRequest events
3358 * (one for each atom pair) except that:
3359 * 1. We reply with a SelectionNotify only when all the requested conversions
3360 * have been performed.
3361 * 2. If we fail to convert the target named by an atom in the MULTIPLE property,
3362 * we replace the atom in the property by None.
3364 static Atom
X11DRV_SelectionRequest_MULTIPLE( HWND hWnd
, XSelectionRequestEvent
*pevent
)
3366 Display
*display
= pevent
->display
;
3368 Atom atype
=AnyPropertyType
;
3370 unsigned long remain
;
3371 Atom
* targetPropList
=NULL
;
3372 unsigned long cTargetPropList
= 0;
3374 /* If the specified property is None the requestor is an obsolete client.
3375 * We support these by using the specified target atom as the reply property.
3377 rprop
= pevent
->property
;
3379 rprop
= pevent
->target
;
3383 /* Read the MULTIPLE property contents. This should contain a list of
3384 * (target,property) atom pairs.
3386 if (!XGetWindowProperty(display
, pevent
->requestor
, rprop
,
3387 0, 0x3FFF, False
, AnyPropertyType
, &atype
,&aformat
,
3388 &cTargetPropList
, &remain
,
3389 (unsigned char**)&targetPropList
) != Success
)
3391 if (TRACE_ON(clipboard
))
3393 char * const typeName
= XGetAtomName(display
, atype
);
3394 TRACE("\tType %s,Format %d,nItems %ld, Remain %ld\n",
3395 typeName
, aformat
, cTargetPropList
, remain
);
3400 * Make sure we got what we expect.
3401 * NOTE: According to the X-ICCCM Version 2.0 documentation the property sent
3402 * in a MULTIPLE selection request should be of type ATOM_PAIR.
3403 * However some X apps(such as XPaint) are not compliant with this and return
3404 * a user defined atom in atype when XGetWindowProperty is called.
3405 * The data *is* an atom pair but is not denoted as such.
3407 if(aformat
== 32 /* atype == xAtomPair */ )
3411 /* Iterate through the ATOM_PAIR list and execute a SelectionRequest
3412 * for each (target,property) pair */
3414 for (i
= 0; i
< cTargetPropList
; i
+=2)
3416 XSelectionRequestEvent event
;
3418 if (TRACE_ON(clipboard
))
3420 char *targetName
, *propName
;
3421 targetName
= XGetAtomName(display
, targetPropList
[i
]);
3422 propName
= XGetAtomName(display
, targetPropList
[i
+1]);
3423 TRACE("MULTIPLE(%d): Target='%s' Prop='%s'\n",
3424 i
/2, targetName
, propName
);
3429 /* We must have a non "None" property to service a MULTIPLE target atom */
3430 if ( !targetPropList
[i
+1] )
3432 TRACE("\tMULTIPLE(%d): Skipping target with empty property!\n", i
);
3436 /* Set up an XSelectionRequestEvent for this (target,property) pair */
3438 event
.target
= targetPropList
[i
];
3439 event
.property
= targetPropList
[i
+1];
3441 /* Fire a SelectionRequest, informing the handler that we are processing
3442 * a MULTIPLE selection request event.
3444 X11DRV_HandleSelectionRequest( hWnd
, &event
, TRUE
);
3448 /* Free the list of targets/properties */
3449 XFree(targetPropList
);
3451 else TRACE("Couldn't read MULTIPLE property\n");
3457 /***********************************************************************
3458 * X11DRV_HandleSelectionRequest
3459 * Process an event selection request event.
3460 * The bIsMultiple flag is used to signal when EVENT_SelectionRequest is called
3461 * recursively while servicing a "MULTIPLE" selection target.
3463 * Note: We only receive this event when WINE owns the X selection
3465 static void X11DRV_HandleSelectionRequest( HWND hWnd
, XSelectionRequestEvent
*event
, BOOL bIsMultiple
)
3467 Display
*display
= event
->display
;
3468 XSelectionEvent result
;
3470 Window request
= event
->requestor
;
3475 * We can only handle the selection request if :
3476 * The selection is PRIMARY or CLIPBOARD, AND we can successfully open the clipboard.
3477 * Don't do these checks or open the clipboard while recursively processing MULTIPLE,
3478 * since this has been already done.
3482 if (((event
->selection
!= XA_PRIMARY
) && (event
->selection
!= x11drv_atom(CLIPBOARD
))))
3486 /* If the specified property is None the requestor is an obsolete client.
3487 * We support these by using the specified target atom as the reply property.
3489 rprop
= event
->property
;
3491 rprop
= event
->target
;
3493 if(event
->target
== x11drv_atom(TARGETS
)) /* Return a list of all supported targets */
3495 /* TARGETS selection request */
3496 rprop
= X11DRV_SelectionRequest_TARGETS( display
, request
, event
->target
, rprop
);
3498 else if(event
->target
== x11drv_atom(MULTIPLE
)) /* rprop contains a list of (target, property) atom pairs */
3500 /* MULTIPLE selection request */
3501 rprop
= X11DRV_SelectionRequest_MULTIPLE( hWnd
, event
);
3505 LPWINE_CLIPFORMAT lpFormat
= X11DRV_CLIPBOARD_LookupProperty(NULL
, event
->target
);
3507 if (lpFormat
&& lpFormat
->lpDrvExportFunc
)
3509 LPWINE_CLIPDATA lpData
= X11DRV_CLIPBOARD_LookupData(lpFormat
->wFormatID
);
3513 unsigned char* lpClipData
;
3515 HANDLE hClipData
= lpFormat
->lpDrvExportFunc(display
, request
, event
->target
,
3516 rprop
, lpData
, &cBytes
);
3518 if (hClipData
&& (lpClipData
= GlobalLock(hClipData
)))
3520 int mode
= PropModeReplace
;
3522 TRACE("\tUpdating property %s, %d bytes\n",
3523 debugstr_format(lpFormat
->wFormatID
), cBytes
);
3526 int nelements
= min(cBytes
, 65536);
3527 XChangeProperty(display
, request
, rprop
, event
->target
,
3528 8, mode
, lpClipData
, nelements
);
3529 mode
= PropModeAppend
;
3530 cBytes
-= nelements
;
3531 lpClipData
+= nelements
;
3532 } while (cBytes
> 0);
3534 GlobalUnlock(hClipData
);
3535 GlobalFree(hClipData
);
3543 * SelectionNotify should be sent only at the end of a MULTIPLE request
3547 result
.type
= SelectionNotify
;
3548 result
.display
= display
;
3549 result
.requestor
= request
;
3550 result
.selection
= event
->selection
;
3551 result
.property
= rprop
;
3552 result
.target
= event
->target
;
3553 result
.time
= event
->time
;
3554 TRACE("Sending SelectionNotify event...\n");
3555 XSendEvent(display
,event
->requestor
,False
,NoEventMask
,(XEvent
*)&result
);
3560 /***********************************************************************
3561 * X11DRV_SelectionRequest
3563 void X11DRV_SelectionRequest( HWND hWnd
, XEvent
*event
)
3565 X11DRV_HandleSelectionRequest( hWnd
, &event
->xselectionrequest
, FALSE
);
3569 /***********************************************************************
3570 * X11DRV_SelectionClear
3572 void X11DRV_SelectionClear( HWND hWnd
, XEvent
*xev
)
3574 XSelectionClearEvent
*event
= &xev
->xselectionclear
;
3575 if (event
->selection
== XA_PRIMARY
|| event
->selection
== x11drv_atom(CLIPBOARD
))
3576 X11DRV_CLIPBOARD_ReleaseSelection( event
->display
, event
->selection
,
3577 event
->window
, hWnd
, event
->time
);