Advance index as we set the atom values.
[wine] / dlls / x11drv / clipboard.c
1 /*
2  * X11 clipboard windows driver
3  *
4  * Copyright 1994 Martin Ayotte
5  *           1996 Alex Korobka
6  *           1999 Noel Borthwick
7  *           2003 Ulrich Czekalla for CodeWeavers
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  * NOTES:
24  *    This file contains the X specific implementation for the windows
25  *    Clipboard API.
26  *
27  *    Wine's internal clipboard is exposed to external apps via the X
28  *    selection mechanism.
29  *    Currently the driver asserts ownership via two selection atoms:
30  *    1. PRIMARY(XA_PRIMARY)
31  *    2. CLIPBOARD
32  *
33  *    In our implementation, the CLIPBOARD selection takes precedence over PRIMARY,
34  *    i.e. if a CLIPBOARD selection is available, it is used instead of PRIMARY.
35  *    When Wine takes ownership of the clipboard, it takes ownership of BOTH selections.
36  *    While giving up selection ownership, if the CLIPBOARD selection is lost,
37  *    it will lose both PRIMARY and CLIPBOARD and empty the clipboard.
38  *    However if only PRIMARY is lost, it will continue to hold the CLIPBOARD selection
39  *    (leaving the clipboard cache content unaffected).
40  *
41  *      Every format exposed via a windows clipboard format is also exposed through
42  *    a corresponding X selection target. A selection target atom is synthesized
43  *    whenever a new Windows clipboard format is registered via RegisterClipboardFormat,
44  *    or when a built-in format is used for the first time.
45  *    Windows native format are exposed by prefixing the format name with "<WCF>"
46  *    This allows us to uniquely identify windows native formats exposed by other
47  *    running WINE apps.
48  *
49  *      In order to allow external applications to query WINE for supported formats,
50  *    we respond to the "TARGETS" selection target. (See EVENT_SelectionRequest
51  *    for implementation) We use the same mechanism to query external clients for
52  *    availability of a particular format, by caching the list of available targets
53  *    by using the clipboard cache's "delayed render" mechanism. If a selection client
54  *    does not support the "TARGETS" selection target, we actually attempt to retrieve
55  *    the format requested as a fallback mechanism.
56  *
57  *      Certain Windows native formats are automatically converted to X native formats
58  *    and vice versa. If a native format is available in the selection, it takes
59  *    precedence, in order to avoid unnecessary conversions.
60  *
61  * FIXME: global format list needs a critical section
62  */
63
64 #include "config.h"
65 #include "wine/port.h"
66
67 #include <string.h>
68 #include <stdarg.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #ifdef HAVE_UNISTD_H
72 # include <unistd.h>
73 #endif
74 #include <fcntl.h>
75 #include <time.h>
76
77 #include "windef.h"
78 #include "winbase.h"
79 #include "winreg.h"
80 #include "wine/wingdi16.h"
81 #include "win.h"
82 #include "x11drv.h"
83 #include "wine/debug.h"
84 #include "wine/unicode.h"
85 #include "wine/server.h"
86
87 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
88
89 #define HGDIOBJ_32(handle16)  ((HGDIOBJ)(ULONG_PTR)(handle16))
90
91 /* Maximum wait time for selection notify */
92 #define SELECTION_RETRIES 500  /* wait for .1 seconds */
93 #define SELECTION_WAIT    1000 /* us */
94 /* Minimum seconds that must lapse between owner queries */
95 #define OWNERQUERYLAPSETIME 1
96
97 /* Selection masks */
98 #define S_NOSELECTION    0
99 #define S_PRIMARY        1
100 #define S_CLIPBOARD      2
101
102 typedef struct
103 {
104     HWND hWndOpen;
105     HWND hWndOwner;
106     HWND hWndViewer;
107     UINT seqno;
108     UINT flags;
109 } CLIPBOARDINFO, *LPCLIPBOARDINFO;
110
111 static int selectionAcquired = 0;              /* Contains the current selection masks */
112 static Window selectionWindow = None;          /* The top level X window which owns the selection */
113 static BOOL clearAllSelections = FALSE;        /* Always lose all selections */
114 static BOOL usePrimary = FALSE;                /* Use primary selection in additon to the clipboard selection */
115 static Atom selectionCacheSrc = XA_PRIMARY;    /* The selection source from which the clipboard cache was filled */
116 static Window PrimarySelectionOwner = None;    /* The window which owns the primary selection */
117 static Window ClipboardSelectionOwner = None;  /* The window which owns the clipboard selection */
118
119 INT X11DRV_RegisterClipboardFormat(LPCSTR FormatName);
120 void X11DRV_EmptyClipboard(void);
121 void X11DRV_EndClipboardUpdate(void);
122 HANDLE X11DRV_CLIPBOARD_ImportClipboardData(LPBYTE lpdata, UINT cBytes);
123 HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(LPBYTE lpdata, UINT cBytes);
124 HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(LPBYTE lpdata, UINT cBytes);
125 HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(LPBYTE lpdata, UINT cBytes);
126 HANDLE X11DRV_CLIPBOARD_ImportXAString(LPBYTE lpdata, UINT cBytes);
127 HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Window requestor, Atom aTarget,
128     Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
129 HANDLE X11DRV_CLIPBOARD_ExportString(Window requestor, Atom aTarget,
130     Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes);
131 HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Window requestor, Atom aTarget,
132     Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
133 HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Window requestor, Atom aTarget,
134     Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
135 HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Window requestor, Atom aTarget,
136     Atom rprop, LPWINE_CLIPDATA lpdata, LPDWORD lpBytes);
137 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(LPCSTR FormatName, Atom prop);
138 static BOOL X11DRV_CLIPBOARD_ReadSelection(LPWINE_CLIPFORMAT lpData, Window w, Atom prop);
139 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(UINT wFormatID);
140 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData);
141 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void);
142 static int X11DRV_CLIPBOARD_QueryAvailableData(LPCLIPBOARDINFO lpcbinfo);
143 static BOOL X11DRV_CLIPBOARD_ReadClipboardData(UINT wFormat);
144 static BOOL X11DRV_CLIPBOARD_RenderFormat(LPWINE_CLIPDATA lpData);
145 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out);
146 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID);
147 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(LPWINE_CLIPDATA lpData);
148
149 /* Clipboard formats
150  * WARNING: This data ordering is dependent on the WINE_CLIPFORMAT structure
151  * declared in clipboard.h
152  */
153 static WINE_CLIPFORMAT ClipFormats[]  =
154 {
155     { CF_TEXT, "WCF_TEXT",  0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
156         X11DRV_CLIPBOARD_ExportClipboardData, NULL, &ClipFormats[1]},
157
158     { CF_BITMAP, "WCF_BITMAP", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
159         X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[0], &ClipFormats[2]},
160
161     { CF_METAFILEPICT, "WCF_METAFILEPICT", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportMetaFilePict,
162         X11DRV_CLIPBOARD_ExportMetaFilePict, &ClipFormats[1], &ClipFormats[3]},
163
164     { CF_SYLK, "WCF_SYLK", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
165         X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[2], &ClipFormats[4]},
166
167     { CF_DIF, "WCF_DIF", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
168         X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[3], &ClipFormats[5]},
169
170     { CF_TIFF, "WCF_TIFF", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
171         X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[4], &ClipFormats[6]},
172
173     { CF_OEMTEXT, "WCF_OEMTEXT", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
174         X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[5], &ClipFormats[7]},
175
176     { CF_DIB, "WCF_DIB", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportXAPIXMAP,
177         X11DRV_CLIPBOARD_ExportXAPIXMAP, &ClipFormats[6], &ClipFormats[8]},
178
179     { CF_PALETTE, "WCF_PALETTE", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
180         X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[7], &ClipFormats[9]},
181
182     { CF_PENDATA, "WCF_PENDATA", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
183         X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[8], &ClipFormats[10]},
184
185     { CF_RIFF, "WCF_RIFF", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
186         X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[9], &ClipFormats[11]},
187
188     { CF_WAVE, "WCF_WAVE", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
189         X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[10], &ClipFormats[12]},
190
191     { CF_UNICODETEXT, "WCF_UNICODETEXT", XA_STRING, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportXAString,
192         X11DRV_CLIPBOARD_ExportString, &ClipFormats[11], &ClipFormats[13]},
193
194     { CF_ENHMETAFILE, "WCF_ENHMETAFILE", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportEnhMetaFile,
195         X11DRV_CLIPBOARD_ExportEnhMetaFile, &ClipFormats[12], &ClipFormats[14]},
196
197     { CF_HDROP, "WCF_HDROP", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
198         X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[13], &ClipFormats[15]},
199
200     { CF_LOCALE, "WCF_LOCALE", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
201         X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[14], &ClipFormats[16]},
202
203     { CF_DIBV5, "WCF_DIBV5", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
204         X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[15], &ClipFormats[17]},
205
206     { CF_OWNERDISPLAY, "WCF_OWNERDISPLAY", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
207         X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[16], &ClipFormats[18]},
208
209     { CF_DSPTEXT, "WCF_DSPTEXT", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
210         X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[17], &ClipFormats[19]},
211
212     { CF_DSPBITMAP, "WCF_DSPBITMAP", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
213         X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[18], &ClipFormats[20]},
214
215     { CF_DSPMETAFILEPICT, "WCF_DSPMETAFILEPICT", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
216         X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[19], &ClipFormats[21]},
217
218     { CF_DSPENHMETAFILE, "WCF_DSPENHMETAFILE", 0, CF_FLAG_BUILTINFMT, X11DRV_CLIPBOARD_ImportClipboardData,
219         X11DRV_CLIPBOARD_ExportClipboardData, &ClipFormats[20], NULL}
220 };
221
222 #define GET_ATOM(prop)  (((prop) < FIRST_XATOM) ? (Atom)(prop) : X11DRV_Atoms[(prop) - FIRST_XATOM])
223
224 /* Maps X properties to Windows formats */
225 static const struct
226 {
227     LPCSTR lpszFormat;
228     UINT   prop;
229 } PropertyFormatMap[] =
230 {
231     { "Rich Text Format", XATOM_text_rtf },
232     /* Temporarily disable text/html because Evolution incorrectly pastes strings with extra nulls */
233     /*{ "text/html", "HTML Format" },*/
234     { "GIF", XATOM_image_gif }
235 };
236
237
238 /* Maps equivalent X properties. It is assumed that lpszProperty must already 
239    be in ClipFormats or PropertyFormatMap. */
240 static const struct
241 {
242     UINT drvDataProperty;
243     UINT drvDataAlias;
244 } PropertyAliasMap[] =
245 {
246     /* DataProperty,   DataAlias */
247     { XATOM_text_rtf,  XATOM_text_richtext },
248     { XA_STRING,       XATOM_COMPOUND_TEXT },
249     { XA_STRING,       XATOM_TEXT },
250 };
251
252
253 /*
254  * Cached clipboard data.
255  */
256 static LPWINE_CLIPDATA ClipData = NULL;
257 static UINT ClipDataCount = 0;
258
259 /*
260  * Clipboard sequence number
261  */
262 static UINT wSeqNo = 0;
263
264 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
265
266 /**************************************************************************
267  *                Internal Clipboard implementation methods
268  **************************************************************************/
269
270 /**************************************************************************
271  *              X11DRV_InitClipboard
272  */
273 void X11DRV_InitClipboard(void)
274 {
275     INT i;
276     HKEY hkey;
277
278     if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Clipboard", &hkey))
279     {
280         char buffer[20];
281         DWORD type, count = sizeof(buffer);
282         if(!RegQueryValueExA(hkey, "ClearAllSelections", 0, &type, buffer, &count))
283             clearAllSelections = IS_OPTION_TRUE( buffer[0] );
284         count = sizeof(buffer);
285         if(!RegQueryValueExA(hkey, "UsePrimary", 0, &type, buffer, &count))
286             usePrimary = IS_OPTION_TRUE( buffer[0] );
287         RegCloseKey(hkey);
288     }
289
290     /* Register known mapping between window formats and X properties */
291     for (i = 0; i < sizeof(PropertyFormatMap)/sizeof(PropertyFormatMap[0]); i++)
292         X11DRV_CLIPBOARD_InsertClipboardFormat(PropertyFormatMap[i].lpszFormat,
293                                                GET_ATOM(PropertyFormatMap[i].prop));
294 }
295
296
297 /**************************************************************************
298  *                intern_atoms
299  *
300  * Intern atoms for formats that don't have one yet.
301  */
302 static void intern_atoms(void)
303 {
304     LPWINE_CLIPFORMAT format;
305     int i, count;
306     char **names;
307     Atom *atoms;
308
309     for (format = ClipFormats, count = 0; format; format = format->NextFormat)
310         if (!format->drvData) count++;
311     if (!count) return;
312
313     names = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*names) );
314     atoms = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*atoms) );
315
316     for (format = ClipFormats, i = 0; format; format = format->NextFormat)
317         if (!format->drvData) names[i++] = format->Name;
318
319     wine_tsx11_lock();
320     XInternAtoms( thread_display(), names, count, False, atoms );
321     wine_tsx11_unlock();
322
323     for (format = ClipFormats, i = 0; format; format = format->NextFormat)
324         if (!format->drvData) format->drvData = atoms[i++];
325
326     HeapFree( GetProcessHeap(), 0, names );
327     HeapFree( GetProcessHeap(), 0, atoms );
328 }
329
330
331 /**************************************************************************
332  *              register_format
333  *
334  * Register a custom X clipboard format.
335  */
336 static WINE_CLIPFORMAT *register_format( LPCSTR FormatName, Atom prop )
337 {
338     LPWINE_CLIPFORMAT lpFormat = ClipFormats;
339
340     TRACE("'%s'\n", FormatName);
341
342     /* walk format chain to see if it's already registered */
343     while (lpFormat)
344     {
345         if ( !strcasecmp(lpFormat->Name, FormatName) && 
346              (lpFormat->wFlags & CF_FLAG_BUILTINFMT) == 0)
347              return lpFormat;
348         lpFormat = lpFormat->NextFormat;
349     }
350
351     return X11DRV_CLIPBOARD_InsertClipboardFormat(FormatName, prop);
352 }
353
354
355 /**************************************************************************
356  *                X11DRV_CLIPBOARD_LookupFormat
357  */
358 LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupFormat(WORD wID)
359 {
360     LPWINE_CLIPFORMAT lpFormat = ClipFormats;
361
362     while(lpFormat)
363     {
364         if (lpFormat->wFormatID == wID) 
365             break;
366
367         lpFormat = lpFormat->NextFormat;
368     }
369     if (!lpFormat->drvData) intern_atoms();
370     return lpFormat;
371 }
372
373
374 /**************************************************************************
375  *                X11DRV_CLIPBOARD_LookupProperty
376  */
377 LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupProperty(UINT drvData)
378 {
379     for (;;)
380     {
381         LPWINE_CLIPFORMAT lpFormat = ClipFormats;
382         BOOL need_intern = FALSE;
383
384         while(lpFormat)
385         {
386             if (lpFormat->drvData == drvData) return lpFormat;
387             if (!lpFormat->drvData) need_intern = TRUE;
388             lpFormat = lpFormat->NextFormat;
389         }
390         if (!need_intern) return NULL;
391         intern_atoms();
392         /* restart the search for the new atoms */
393     }
394 }
395
396
397 /**************************************************************************
398  *                X11DRV_CLIPBOARD_LookupAliasProperty
399  */
400 LPWINE_CLIPFORMAT X11DRV_CLIPBOARD_LookupAliasProperty(UINT drvDataAlias)
401 {
402     unsigned int i;
403     LPWINE_CLIPFORMAT lpFormat = NULL;
404
405     for (i = 0; i < sizeof(PropertyAliasMap)/sizeof(PropertyAliasMap[0]); i++)
406     {
407         if (GET_ATOM(PropertyAliasMap[i].drvDataAlias) == drvDataAlias)
408         {
409             lpFormat = X11DRV_CLIPBOARD_LookupProperty(GET_ATOM(PropertyAliasMap[i].drvDataProperty));
410             break;
411         }
412    }
413
414     return lpFormat;
415 }
416
417
418 /**************************************************************************
419  *                X11DRV_CLIPBOARD_LookupPropertyAlias
420  */
421 UINT  X11DRV_CLIPBOARD_LookupPropertyAlias(UINT drvDataProperty)
422 {
423     unsigned int i;
424     UINT alias = 0;
425
426     for (i = 0; i < sizeof(PropertyAliasMap)/sizeof(PropertyAliasMap[0]); i++)
427     {
428         if (GET_ATOM(PropertyAliasMap[i].drvDataProperty) == drvDataProperty)
429         {
430             alias = GET_ATOM(PropertyAliasMap[i].drvDataAlias);
431             break;
432         }
433    }
434
435     return alias;
436 }
437
438
439 /**************************************************************************
440  *               X11DRV_CLIPBOARD_LookupData
441  */
442 LPWINE_CLIPDATA X11DRV_CLIPBOARD_LookupData(DWORD wID)
443 {
444     LPWINE_CLIPDATA lpData = ClipData;
445
446     if (lpData)
447     {
448         do
449         {
450             if (lpData->wFormatID == wID) 
451                 break;
452
453             lpData = lpData->NextData;
454         }
455         while(lpData != ClipData);
456
457         if (lpData->wFormatID != wID)
458             lpData = NULL;
459     }
460
461     return lpData;
462 }
463
464
465 /**************************************************************************
466  *              InsertClipboardFormat
467  */
468 static WINE_CLIPFORMAT *X11DRV_CLIPBOARD_InsertClipboardFormat(LPCSTR FormatName, Atom prop)
469 {
470     LPWINE_CLIPFORMAT lpFormat;
471     LPWINE_CLIPFORMAT lpNewFormat;
472    
473     /* allocate storage for new format entry */
474     lpNewFormat = (LPWINE_CLIPFORMAT) HeapAlloc(GetProcessHeap(), 
475         0, sizeof(WINE_CLIPFORMAT));
476
477     if(lpNewFormat == NULL) 
478     {
479         WARN("No more memory for a new format!\n");
480         return NULL;
481     }
482
483     if (!(lpNewFormat->Name = HeapAlloc(GetProcessHeap(), 0, strlen(FormatName)+1)))
484     {
485         WARN("No more memory for the new format name!\n");
486         HeapFree(GetProcessHeap(), 0, lpNewFormat);
487         return NULL;
488     }
489
490     strcpy(lpNewFormat->Name, FormatName);
491     lpNewFormat->wFlags = 0;
492     lpNewFormat->wFormatID = GlobalAddAtomA(lpNewFormat->Name);
493     lpNewFormat->drvData = prop;
494     lpNewFormat->lpDrvImportFunc = X11DRV_CLIPBOARD_ImportClipboardData;
495     lpNewFormat->lpDrvExportFunc = X11DRV_CLIPBOARD_ExportClipboardData;
496
497     /* Link Format */
498     lpFormat = ClipFormats;
499
500     while(lpFormat->NextFormat) /* Move to last entry */
501         lpFormat = lpFormat->NextFormat;
502
503     lpNewFormat->NextFormat = NULL;
504     lpFormat->NextFormat = lpNewFormat;
505     lpNewFormat->PrevFormat = lpFormat;
506
507     TRACE("Registering format(%d): %s drvData %d\n",
508         lpNewFormat->wFormatID, FormatName, lpNewFormat->drvData);
509
510     return lpNewFormat;
511 }
512
513
514
515
516 /**************************************************************************
517  *                      X11DRV_CLIPBOARD_GetClipboardInfo
518  */
519 static BOOL X11DRV_CLIPBOARD_GetClipboardInfo(LPCLIPBOARDINFO cbInfo)
520 {
521     BOOL bRet = FALSE;
522
523     SERVER_START_REQ( set_clipboard_info )
524     {
525         req->flags = 0;
526
527         if (wine_server_call_err( req ))
528         {
529             ERR("Failed to get clipboard owner.\n");
530         }
531         else
532         {
533             cbInfo->hWndOpen = reply->old_clipboard;
534             cbInfo->hWndOwner = reply->old_owner;
535             cbInfo->hWndViewer = reply->old_viewer;
536             cbInfo->seqno = reply->seqno;
537             cbInfo->flags = reply->flags;
538
539             bRet = TRUE;
540         }
541     }
542     SERVER_END_REQ;
543
544     return bRet;
545 }
546
547
548 /**************************************************************************
549  *      X11DRV_CLIPBOARD_ReleaseOwnership
550  */
551 static BOOL X11DRV_CLIPBOARD_ReleaseOwnership(void)
552 {
553     BOOL bRet = FALSE;
554
555     SERVER_START_REQ( set_clipboard_info )
556     {
557         req->flags = SET_CB_RELOWNER | SET_CB_SEQNO;
558
559         if (wine_server_call_err( req ))
560         {
561             ERR("Failed to set clipboard.\n");
562         }
563         else
564         {
565             bRet = TRUE;
566         }
567     }
568     SERVER_END_REQ;
569
570     return bRet;
571 }
572
573
574
575 /**************************************************************************
576  *                      X11DRV_CLIPBOARD_InsertClipboardData
577  *
578  * Caller *must* have the clipboard open and be the owner.
579  */
580 static BOOL X11DRV_CLIPBOARD_InsertClipboardData(UINT wFormat, HANDLE16 hData16, HANDLE hData32, DWORD flags)
581 {
582     LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormat);
583
584     TRACE("format=%d lpData=%p hData16=%08x hData32=%08x flags=0x%08lx\n", 
585         wFormat, lpData, hData16, (unsigned int)hData32, flags);
586
587     if (lpData)
588     {
589         X11DRV_CLIPBOARD_FreeData(lpData);
590
591         lpData->hData16 = hData16;  /* 0 is legal, see WM_RENDERFORMAT */
592         lpData->hData32 = hData32;
593     }
594     else
595     {
596         lpData = (LPWINE_CLIPDATA) HeapAlloc(GetProcessHeap(), 
597             0, sizeof(WINE_CLIPDATA));
598
599         lpData->wFormatID = wFormat;
600         lpData->hData16 = hData16;  /* 0 is legal, see WM_RENDERFORMAT */
601         lpData->hData32 = hData32;
602
603         if (ClipData)
604         {
605             LPWINE_CLIPDATA lpPrevData = ClipData->PrevData;
606
607             lpData->PrevData = lpPrevData;
608             lpData->NextData = ClipData;
609
610             lpPrevData->NextData = lpData;
611             ClipData->PrevData = lpData;
612         }
613         else
614         {
615             lpData->NextData = lpData;
616             lpData->PrevData = lpData;
617             ClipData = lpData;
618         }
619
620         ClipDataCount++;
621     }
622
623     lpData->wFlags = flags;
624
625     return TRUE;
626 }
627
628
629 /**************************************************************************
630  *                      X11DRV_CLIPBOARD_FreeData
631  *
632  * Free clipboard data handle.
633  */
634 static void X11DRV_CLIPBOARD_FreeData(LPWINE_CLIPDATA lpData)
635 {
636     TRACE("%d\n", lpData->wFormatID);
637
638     if ((lpData->wFormatID >= CF_GDIOBJFIRST &&
639         lpData->wFormatID <= CF_GDIOBJLAST) || 
640         lpData->wFormatID == CF_BITMAP || 
641         lpData->wFormatID == CF_DIB || 
642         lpData->wFormatID == CF_PALETTE)
643     {
644       if (lpData->hData32)
645         DeleteObject(lpData->hData32);
646
647       if (lpData->hData16)
648         DeleteObject(HGDIOBJ_32(lpData->hData16));
649     }
650     else if (lpData->wFormatID == CF_METAFILEPICT)
651     {
652       if (lpData->hData32)
653       {
654         DeleteMetaFile(((METAFILEPICT *)GlobalLock( lpData->hData32 ))->hMF );
655         GlobalFree(lpData->hData32);
656
657         if (lpData->hData16)
658           /* HMETAFILE16 and HMETAFILE32 are apparently the same thing,
659              and a shallow copy is enough to share a METAFILEPICT
660              structure between 16bit and 32bit clipboards.  The MetaFile
661              should of course only be deleted once. */
662           GlobalFree16(lpData->hData16);
663       }
664
665       if (lpData->hData16)
666       {
667         METAFILEPICT16* lpMetaPict = (METAFILEPICT16 *) GlobalLock16(lpData->hData16);
668
669         if (lpMetaPict)
670         {
671             DeleteMetaFile16(lpMetaPict->hMF);
672             lpMetaPict->hMF = 0;
673         }
674
675         GlobalFree16(lpData->hData16);
676       }
677     }
678     else if (lpData->wFormatID == CF_ENHMETAFILE)
679     {
680         if (lpData->hData32)
681             DeleteEnhMetaFile(lpData->hData32);
682     }
683     else if (lpData->wFormatID < CF_PRIVATEFIRST ||
684              lpData->wFormatID > CF_PRIVATELAST)
685     {
686       if (lpData->hData32)
687         GlobalFree(lpData->hData32);
688
689       if (lpData->hData16)
690         GlobalFree16(lpData->hData16);
691     }
692
693     lpData->hData16 = 0;
694     lpData->hData32 = 0;
695 }
696
697
698 /**************************************************************************
699  *                      X11DRV_CLIPBOARD_UpdateCache
700  */
701 static BOOL X11DRV_CLIPBOARD_UpdateCache(LPCLIPBOARDINFO lpcbinfo)
702 {
703     BOOL bret = TRUE;
704
705     if (!X11DRV_CLIPBOARD_IsSelectionOwner())
706     {
707         if (!X11DRV_CLIPBOARD_GetClipboardInfo(lpcbinfo))
708         {
709             ERR("Failed to retrieve clipboard information.\n");
710             bret = FALSE;
711         }
712         else if (wSeqNo < lpcbinfo->seqno)
713         {
714             X11DRV_EmptyClipboard();
715
716             if (X11DRV_CLIPBOARD_QueryAvailableData(lpcbinfo) < 0)
717             {
718                 ERR("Failed to cache clipboard data owned by another process.\n");
719                 bret = FALSE;
720             }
721             else
722             {
723                 X11DRV_EndClipboardUpdate();
724             }
725
726             wSeqNo = lpcbinfo->seqno;
727         }
728     }
729
730     return bret;
731 }
732
733
734 /**************************************************************************
735  *                      X11DRV_CLIPBOARD_RenderFormat
736  */
737 static BOOL X11DRV_CLIPBOARD_RenderFormat(LPWINE_CLIPDATA lpData)
738 {
739     BOOL bret = TRUE;
740
741     TRACE(" 0x%04x hData32(0x%08x) hData16(0x%08x)\n", 
742         lpData->wFormatID, (unsigned int)lpData->hData32, lpData->hData16);
743
744     if (lpData->hData32 || lpData->hData16)
745         return bret; /* Already rendered */
746
747     if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
748         bret = X11DRV_CLIPBOARD_RenderSynthesizedFormat(lpData);
749     else if (!X11DRV_CLIPBOARD_IsSelectionOwner())
750     {
751         if (!X11DRV_CLIPBOARD_ReadClipboardData(lpData->wFormatID))
752         {
753             ERR("Failed to cache clipboard data owned by another process. Format=%d\n", 
754                 lpData->wFormatID);
755             bret = FALSE;
756         }
757     }
758     else
759     {
760         CLIPBOARDINFO cbInfo;
761
762         if (X11DRV_CLIPBOARD_GetClipboardInfo(&cbInfo) && cbInfo.hWndOwner)
763         {
764             /* Send a WM_RENDERFORMAT message to notify the owner to render the
765              * data requested into the clipboard.
766              */
767             TRACE("Sending WM_RENDERFORMAT message to hwnd(%p)\n", cbInfo.hWndOwner);
768             SendMessageW(cbInfo.hWndOwner, WM_RENDERFORMAT, (WPARAM)lpData->wFormatID, 0);
769
770             if (!lpData->hData32 && !lpData->hData16)
771                 bret = FALSE;
772         }
773         else
774         {
775             ERR("hWndClipOwner is lost!\n");
776             bret = FALSE;
777         }
778     }
779
780     return bret;
781 }
782
783
784 /**************************************************************************
785  *                      CLIPBOARD_ConvertText
786  * Returns number of required/converted characters - not bytes!
787  */
788 static INT CLIPBOARD_ConvertText(WORD src_fmt, void const *src, INT src_size,
789                                  WORD dst_fmt, void *dst, INT dst_size)
790 {
791     UINT cp;
792
793     if(src_fmt == CF_UNICODETEXT)
794     {
795         switch(dst_fmt)
796         {
797         case CF_TEXT:
798             cp = CP_ACP;
799             break;
800         case CF_OEMTEXT:
801             cp = CP_OEMCP;
802             break;
803         default:
804             return 0;
805         }
806         return WideCharToMultiByte(cp, 0, src, src_size, dst, dst_size, NULL, NULL);
807     }
808
809     if(dst_fmt == CF_UNICODETEXT)
810     {
811         switch(src_fmt)
812         {
813         case CF_TEXT:
814             cp = CP_ACP;
815             break;
816         case CF_OEMTEXT:
817             cp = CP_OEMCP;
818             break;
819         default:
820             return 0;
821         }
822         return MultiByteToWideChar(cp, 0, src, src_size, dst, dst_size);
823     }
824
825     if(!dst_size) return src_size;
826
827     if(dst_size > src_size) dst_size = src_size;
828
829     if(src_fmt == CF_TEXT )
830         CharToOemBuffA(src, dst, dst_size);
831     else
832         OemToCharBuffA(src, dst, dst_size);
833
834     return dst_size;
835 }
836
837
838 /**************************************************************************
839  *                      X11DRV_CLIPBOARD_RenderSynthesizedFormat
840  */
841 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedFormat(LPWINE_CLIPDATA lpData)
842 {
843     BOOL bret = FALSE;
844
845     TRACE("\n");
846
847     if (lpData->wFlags & CF_FLAG_SYNTHESIZED)
848     {
849         UINT wFormatID = lpData->wFormatID;
850
851         if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
852             bret = X11DRV_CLIPBOARD_RenderSynthesizedText(wFormatID);
853         else 
854         {
855             switch (wFormatID)
856             {
857                 case CF_ENHMETAFILE:
858                 case CF_METAFILEPICT:
859                 case CF_DIB:
860                 case CF_BITMAP:
861                     FIXME("Synthesizing wFormatID(0x%08x) not implemented\n", wFormatID);
862                     break;
863
864                 default:
865                     FIXME("Called to synthesize unknown format\n");
866                     break;
867             }
868         }
869
870         lpData->wFlags &= ~CF_FLAG_SYNTHESIZED;
871     }
872
873     return bret;
874 }
875
876
877 /**************************************************************************
878  *                      X11DRV_CLIPBOARD_RenderSynthesizedText
879  *
880  * Renders synthesized text
881  */
882 static BOOL X11DRV_CLIPBOARD_RenderSynthesizedText(UINT wFormatID)
883 {
884     LPCSTR lpstrS;
885     LPSTR  lpstrT;
886     HANDLE hData32;
887     INT src_chars, dst_chars, alloc_size;
888     LPWINE_CLIPDATA lpSource = NULL;
889
890     TRACE(" %d\n", wFormatID);
891
892     if ((lpSource = X11DRV_CLIPBOARD_LookupData(wFormatID)) &&
893         lpSource->hData32)
894         return TRUE;
895
896     /* Look for rendered source or non-synthesized source */
897     if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
898         (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
899     {
900         TRACE("UNICODETEXT -> %d\n", wFormatID);
901     }
902     else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
903         (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
904     {
905         TRACE("TEXT -> %d\n", wFormatID);
906     }
907     else if ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
908         (!(lpSource->wFlags & CF_FLAG_SYNTHESIZED) || lpSource->hData32))
909     {
910         TRACE("OEMTEXT -> %d\n", wFormatID);
911     }
912
913     if (!lpSource || (lpSource->wFlags & CF_FLAG_SYNTHESIZED &&
914         !lpSource->hData32))
915         return FALSE;
916
917     /* Ask the clipboard owner to render the source text if necessary */
918     if (!lpSource->hData32 && !X11DRV_CLIPBOARD_RenderFormat(lpSource))
919         return FALSE;
920
921     if (lpSource->hData32)
922     {
923         lpstrS = (LPSTR)GlobalLock(lpSource->hData32);
924     }
925     else
926     {
927         lpstrS = (LPSTR)GlobalLock16(lpSource->hData16);
928     }
929
930     if (!lpstrS)
931         return FALSE;
932
933     /* Text always NULL terminated */
934     if(lpSource->wFormatID == CF_UNICODETEXT)
935         src_chars = strlenW((LPCWSTR)lpstrS) + 1;
936     else
937         src_chars = strlen(lpstrS) + 1;
938
939     /* Calculate number of characters in the destination buffer */
940     dst_chars = CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS, 
941         src_chars, wFormatID, NULL, 0);
942
943     if (!dst_chars)
944         return FALSE;
945
946     TRACE("Converting from '%d' to '%d', %i chars\n",
947         lpSource->wFormatID, wFormatID, src_chars);
948
949     /* Convert characters to bytes */
950     if(wFormatID == CF_UNICODETEXT)
951         alloc_size = dst_chars * sizeof(WCHAR);
952     else
953         alloc_size = dst_chars;
954
955     hData32 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE | 
956         GMEM_DDESHARE, alloc_size);
957
958     lpstrT = (LPSTR)GlobalLock(hData32);
959
960     if (lpstrT)
961     {
962         CLIPBOARD_ConvertText(lpSource->wFormatID, lpstrS, src_chars,
963             wFormatID, lpstrT, dst_chars);
964         GlobalUnlock(hData32);
965     }
966
967     /* Unlock source */
968     if (lpSource->hData32)
969         GlobalUnlock(lpSource->hData32);
970     else
971         GlobalUnlock16(lpSource->hData16);
972
973     return X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, 0, hData32, 0);
974 }
975
976
977 /**************************************************************************
978  *              X11DRV_CLIPBOARD_ImportXAString
979  *
980  *  Import XA_STRING, converting the string to CF_UNICODE.
981  */
982 HANDLE X11DRV_CLIPBOARD_ImportXAString(LPBYTE lpdata, UINT cBytes)
983 {
984     LPSTR lpstr;
985     UINT i, inlcount = 0;
986     HANDLE hUnicodeText = 0;
987
988     for (i = 0; i <= cBytes; i++)
989     {
990         if (lpdata[i] == '\n')
991             inlcount++;
992     }
993
994     if ((lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cBytes + inlcount + 1)))
995     {
996         UINT count;
997
998         for (i = 0, inlcount = 0; i <= cBytes; i++)
999         {
1000             if (lpdata[i] == '\n')
1001                 lpstr[inlcount++] = '\r';
1002
1003             lpstr[inlcount++] = lpdata[i];
1004         }
1005
1006         count = MultiByteToWideChar(CP_UNIXCP, 0, lpstr, -1, NULL, 0);
1007         hUnicodeText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, count * sizeof(WCHAR));
1008
1009         if(hUnicodeText)
1010         {
1011             WCHAR *textW = GlobalLock(hUnicodeText);
1012             MultiByteToWideChar(CP_UNIXCP, 0, lpstr, -1, textW, count);
1013             GlobalUnlock(hUnicodeText);
1014         }
1015
1016         HeapFree(GetProcessHeap(), 0, lpstr);
1017     }
1018
1019     return hUnicodeText;
1020 }
1021
1022
1023 /**************************************************************************
1024  *              X11DRV_CLIPBOARD_ImportXAPIXMAP
1025  *
1026  *  Import XA_PIXMAP, converting the image to CF_DIB.
1027  */
1028 HANDLE X11DRV_CLIPBOARD_ImportXAPIXMAP(LPBYTE lpdata, UINT cBytes)
1029 {
1030     HANDLE hTargetImage = 0;  /* Handle to store the converted DIB */
1031     Pixmap *pPixmap = (Pixmap *) lpdata;
1032     HWND hwnd = GetOpenClipboardWindow();
1033     HDC hdc = GetDC(hwnd);
1034
1035     hTargetImage = X11DRV_DIB_CreateDIBFromPixmap(*pPixmap, hdc, TRUE);
1036
1037     ReleaseDC(hwnd, hdc);
1038
1039     return hTargetImage;
1040 }
1041
1042
1043 /**************************************************************************
1044  *              X11DRV_CLIPBOARD_ImportMetaFilePict
1045  *
1046  *  Import MetaFilePict.
1047  */
1048 HANDLE X11DRV_CLIPBOARD_ImportMetaFilePict(LPBYTE lpdata, UINT cBytes)
1049 {
1050     return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, (HANDLE)lpdata, (LPDWORD)&cBytes, FALSE);
1051 }
1052
1053
1054 /**************************************************************************
1055  *              X11DRV_ImportEnhMetaFile
1056  *
1057  *  Import EnhMetaFile.
1058  */
1059 HANDLE X11DRV_CLIPBOARD_ImportEnhMetaFile(LPBYTE lpdata, UINT cBytes)
1060 {
1061     return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, (HANDLE)lpdata, (LPDWORD)&cBytes, FALSE);
1062 }
1063
1064
1065 /**************************************************************************
1066  *              X11DRV_ImportClipbordaData
1067  *
1068  *  Generic import clipboard data routine.
1069  */
1070 HANDLE X11DRV_CLIPBOARD_ImportClipboardData(LPBYTE lpdata, UINT cBytes)
1071 {
1072     LPVOID lpClipData;
1073     HANDLE hClipData = 0;
1074
1075     if (cBytes)
1076     {
1077         /* Turn on the DDESHARE flag to enable shared 32 bit memory */
1078         hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cBytes);
1079         if ((lpClipData = GlobalLock(hClipData)))
1080         {
1081             memcpy(lpClipData, lpdata, cBytes);
1082             GlobalUnlock(hClipData);
1083         }
1084         else
1085             hClipData = 0;
1086     }
1087
1088     return hClipData;
1089 }
1090
1091
1092 /**************************************************************************
1093                 X11DRV_CLIPBOARD_ExportClipboardData
1094  *
1095  *  Generic export clipboard data routine.
1096  */
1097 HANDLE X11DRV_CLIPBOARD_ExportClipboardData(Window requestor, Atom aTarget,
1098     Atom rprop, LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1099 {
1100     LPVOID lpClipData;
1101     UINT cBytes = 0;
1102     HANDLE hClipData = 0;
1103
1104     *lpBytes = 0; /* Assume failure */
1105
1106     if (!X11DRV_CLIPBOARD_RenderFormat(lpData))
1107         ERR("Failed to export %d format\n", lpData->wFormatID);
1108     else
1109     {
1110         cBytes = GlobalSize(lpData->hData32);
1111
1112         hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cBytes);
1113
1114         if ((lpClipData = GlobalLock(hClipData)))
1115         {
1116             LPVOID lpdata = GlobalLock(lpData->hData32);
1117
1118             memcpy(lpClipData, lpdata, cBytes);
1119             *lpBytes = cBytes;
1120
1121             GlobalUnlock(lpData->hData32);
1122             GlobalUnlock(hClipData);
1123         }
1124     }
1125
1126     return hClipData;
1127 }
1128
1129
1130 /**************************************************************************
1131  *              X11DRV_CLIPBOARD_ExportXAString
1132  *
1133  *  Export CF_UNICODE converting the string to XA_STRING.
1134  *  Helper function for X11DRV_CLIPBOARD_ExportString.
1135  */
1136 HANDLE X11DRV_CLIPBOARD_ExportXAString(LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1137 {
1138     INT i, j;
1139     UINT size;
1140     LPWSTR uni_text;
1141     LPSTR text, lpstr;
1142
1143     *lpBytes = 0; /* Assume return has zero bytes */
1144
1145     uni_text = GlobalLock(lpData->hData32);
1146
1147     size = WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, NULL, 0, NULL, NULL);
1148
1149     text = HeapAlloc(GetProcessHeap(), 0, size);
1150     if (!text)
1151        return None;
1152     WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, text, size, NULL, NULL);
1153
1154     /* remove carriage returns */
1155
1156     lpstr = (char*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size-- );
1157     if(lpstr == NULL) return None;
1158     for(i = 0,j = 0; i < size && text[i]; i++ )
1159     {
1160         if( text[i] == '\r' &&
1161             (text[i+1] == '\n' || text[i+1] == '\0') ) continue;
1162         lpstr[j++] = text[i];
1163     }
1164     lpstr[j]='\0';
1165
1166     *lpBytes = j; /* Number of bytes in string */
1167
1168     HeapFree(GetProcessHeap(), 0, text);
1169     GlobalUnlock(lpData->hData32);
1170
1171     return lpstr;
1172 }
1173
1174
1175 /**************************************************************************
1176  *              X11DRV_CLIPBOARD_ExportCompoundText
1177  *
1178  *  Export CF_UNICODE to COMPOUND_TEXT or TEXT
1179  *  Helper function for X11DRV_CLIPBOARD_ExportString.
1180  */
1181 HANDLE X11DRV_CLIPBOARD_ExportCompoundText(Window requestor, Atom aTarget, Atom rprop,
1182     LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1183 {
1184     Display *display = thread_display();
1185     char* lpstr = 0;
1186     XTextProperty prop;
1187     XICCEncodingStyle style;
1188
1189     lpstr = (char*) X11DRV_CLIPBOARD_ExportXAString(lpData, lpBytes);
1190
1191     if (lpstr)
1192     {
1193         if (aTarget == x11drv_atom(COMPOUND_TEXT))
1194            style = XCompoundTextStyle;
1195         else
1196            style = XStdICCTextStyle;
1197
1198         /* Update the X property */
1199         wine_tsx11_lock();
1200         if (XmbTextListToTextProperty(display, &lpstr, 1, style, &prop) == Success)
1201         {
1202            XSetTextProperty(display, requestor, &prop, rprop);
1203            XFree(prop.value);
1204         }
1205         wine_tsx11_unlock();
1206
1207         HeapFree( GetProcessHeap(), 0, lpstr );
1208     }
1209
1210     return 0;
1211 }
1212
1213 /**************************************************************************
1214  *              X11DRV_CLIPBOARD_ExportString
1215  *
1216  *  Export CF_UNICODE string
1217  */
1218 HANDLE X11DRV_CLIPBOARD_ExportString(Window requestor, Atom aTarget, Atom rprop,
1219     LPWINE_CLIPDATA lpData, LPDWORD lpBytes)
1220 {
1221     if (X11DRV_CLIPBOARD_RenderFormat(lpData))
1222     {
1223         if (aTarget == XA_STRING)
1224             return X11DRV_CLIPBOARD_ExportXAString(lpData, lpBytes);
1225         else if (aTarget == x11drv_atom(COMPOUND_TEXT) || aTarget == x11drv_atom(TEXT))
1226             return X11DRV_CLIPBOARD_ExportCompoundText(requestor, aTarget,
1227                 rprop, lpData, lpBytes);
1228         else
1229             ERR("Unknown target %ld to %d format\n", aTarget, lpData->wFormatID);
1230     }
1231     else
1232         ERR("Failed to render %d format\n", lpData->wFormatID);
1233
1234     return 0;
1235 }
1236
1237
1238 /**************************************************************************
1239  *              X11DRV_CLIPBOARD_ExportXAPIXMAP
1240  *
1241  *  Export CF_DIB to XA_PIXMAP.
1242  */
1243 HANDLE X11DRV_CLIPBOARD_ExportXAPIXMAP(Window requestor, Atom aTarget, Atom rprop,
1244     LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1245 {
1246     HDC hdc;
1247     Pixmap pixmap;
1248
1249     if (!X11DRV_CLIPBOARD_RenderFormat(lpdata))
1250     {
1251         ERR("Failed to export %d format\n", lpdata->wFormatID);
1252         return 0;
1253     }
1254
1255     hdc = GetDC(0);
1256
1257     /* For convert from packed DIB to Pixmap */
1258     pixmap = X11DRV_DIB_CreatePixmapFromDIB(lpdata, hdc);
1259     *lpBytes = 4; /* pixmap is a 32bit value */
1260
1261     ReleaseDC(0, hdc);
1262
1263     return (HANDLE) pixmap;
1264 }
1265
1266
1267 /**************************************************************************
1268  *              X11DRV_CLIPBOARD_ExportMetaFilePict
1269  *
1270  *  Export MetaFilePict.
1271  */
1272 HANDLE X11DRV_CLIPBOARD_ExportMetaFilePict(Window requestor, Atom aTarget, Atom rprop,
1273     LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1274 {
1275     if (!X11DRV_CLIPBOARD_RenderFormat(lpdata))
1276     {
1277         ERR("Failed to export %d format\n", lpdata->wFormatID);
1278         return 0;
1279     }
1280
1281     return X11DRV_CLIPBOARD_SerializeMetafile(CF_METAFILEPICT, (HANDLE)lpdata->hData32, 
1282         lpBytes, TRUE);
1283 }
1284
1285
1286 /**************************************************************************
1287  *              X11DRV_CLIPBOARD_ExportEnhMetaFile
1288  *
1289  *  Export EnhMetaFile.
1290  */
1291 HANDLE X11DRV_CLIPBOARD_ExportEnhMetaFile(Window requestor, Atom aTarget, Atom rprop,
1292     LPWINE_CLIPDATA lpdata, LPDWORD lpBytes)
1293 {
1294     if (!X11DRV_CLIPBOARD_RenderFormat(lpdata))
1295     {
1296         ERR("Failed to export %d format\n", lpdata->wFormatID);
1297         return 0;
1298     }
1299
1300     return X11DRV_CLIPBOARD_SerializeMetafile(CF_ENHMETAFILE, (HANDLE)lpdata->hData32, 
1301         lpBytes, TRUE);
1302 }
1303
1304
1305 /**************************************************************************
1306  *              X11DRV_CLIPBOARD_QueryTargets
1307  */
1308 static BOOL X11DRV_CLIPBOARD_QueryTargets(Display *display, Window w, Atom selection, XEvent *xe)
1309 {
1310     INT i;
1311     Bool res;
1312
1313     wine_tsx11_lock();
1314     XConvertSelection(display, selection, x11drv_atom(TARGETS),
1315                       x11drv_atom(SELECTION_DATA), w, CurrentTime);
1316     wine_tsx11_unlock();
1317
1318     /*
1319      * Wait until SelectionNotify is received
1320      */
1321     for (i = 0; i < SELECTION_RETRIES; i++)
1322     {
1323         wine_tsx11_lock();
1324         res = XCheckTypedWindowEvent(display, w, SelectionNotify, xe);
1325         wine_tsx11_unlock();
1326         if (res && xe->xselection.selection == selection) break;
1327
1328         usleep(SELECTION_WAIT);
1329     }
1330
1331     /* Verify that the selection returned a valid TARGETS property */
1332     if ((xe->xselection.target != x11drv_atom(TARGETS)) || (xe->xselection.property == None))
1333     {
1334         /* Selection owner failed to respond or we missed the SelectionNotify */
1335         WARN("Failed to retrieve TARGETS for selection %ld.\n", selection);
1336         return FALSE;
1337     }
1338
1339     return TRUE;
1340 }
1341
1342
1343 /**************************************************************************
1344  *              X11DRV_CLIPBOARD_QueryAvailableData
1345  *
1346  * Caches the list of data formats available from the current selection.
1347  * This queries the selection owner for the TARGETS property and saves all
1348  * reported property types.
1349  */
1350 static int X11DRV_CLIPBOARD_QueryAvailableData(LPCLIPBOARDINFO lpcbinfo)
1351 {
1352     Display *display = thread_display();
1353     XEvent         xe;
1354     Atom           atype=AnyPropertyType;
1355     int            aformat;
1356     unsigned long  remain;
1357     Atom*          targetList=NULL;
1358     Window         w;
1359     HWND           hWndClipWindow; 
1360     unsigned long  cSelectionTargets = 0;
1361
1362     if (selectionAcquired & (S_PRIMARY | S_CLIPBOARD))
1363     {
1364         ERR("Received request to cache selection but process is owner=(%08x)\n", 
1365             (unsigned) selectionWindow);
1366
1367         selectionAcquired  = S_NOSELECTION;
1368
1369         wine_tsx11_lock();
1370         if (XGetSelectionOwner(display,XA_PRIMARY) == selectionWindow)
1371             selectionAcquired |= S_PRIMARY;
1372
1373         if (XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)) == selectionWindow)
1374             selectionAcquired |= S_CLIPBOARD;
1375         wine_tsx11_unlock();
1376
1377         if (!(selectionAcquired == (S_PRIMARY | S_CLIPBOARD)))
1378         {
1379             WARN("Lost selection but process didn't process SelectClear\n");
1380             selectionWindow = None;
1381         }
1382         else
1383         {
1384             return -1; /* Prevent self request */
1385         }
1386     }
1387
1388     if (lpcbinfo->flags & CB_OWNER)
1389         hWndClipWindow = lpcbinfo->hWndOwner;
1390     else if (lpcbinfo->flags & CB_OPEN)
1391         hWndClipWindow = lpcbinfo->hWndOpen;
1392     else
1393         hWndClipWindow = GetActiveWindow();
1394
1395     if (!hWndClipWindow)
1396     {
1397         WARN("No window available to retrieve selection!\n");
1398         return -1;
1399     }
1400
1401     w = X11DRV_get_whole_window(GetAncestor(hWndClipWindow, GA_ROOT));
1402
1403     /*
1404      * Query the selection owner for the TARGETS property
1405      */
1406     wine_tsx11_lock();
1407     if ((usePrimary && XGetSelectionOwner(display,XA_PRIMARY)) ||
1408         XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
1409     {
1410         wine_tsx11_unlock();
1411         if (usePrimary && (X11DRV_CLIPBOARD_QueryTargets(display, w, XA_PRIMARY, &xe)))
1412             selectionCacheSrc = XA_PRIMARY;
1413         else if (X11DRV_CLIPBOARD_QueryTargets(display, w, x11drv_atom(CLIPBOARD), &xe))
1414             selectionCacheSrc = x11drv_atom(CLIPBOARD);
1415         else
1416             return -1;
1417     }
1418     else /* No selection owner so report 0 targets available */
1419     {
1420         wine_tsx11_unlock();
1421         return 0;
1422     }
1423
1424     /* Read the TARGETS property contents */
1425     wine_tsx11_lock();
1426     if(XGetWindowProperty(display, xe.xselection.requestor, xe.xselection.property,
1427         0, 0x3FFF, True, AnyPropertyType/*XA_ATOM*/, &atype, &aformat, &cSelectionTargets, 
1428         &remain, (unsigned char**)&targetList) != Success)
1429     {
1430         wine_tsx11_unlock();
1431         WARN("Failed to read TARGETS property\n");
1432     }
1433     else
1434     {
1435         wine_tsx11_unlock();
1436        TRACE("Type %lx,Format %d,nItems %ld, Remain %ld\n",
1437              atype, aformat, cSelectionTargets, remain);
1438        /*
1439         * The TARGETS property should have returned us a list of atoms
1440         * corresponding to each selection target format supported.
1441         */
1442        if ((atype == XA_ATOM || atype == x11drv_atom(TARGETS)) && aformat == 32)
1443        {
1444           INT i, nb_atoms = 0;
1445           Atom *atoms = NULL;
1446
1447           /* Cache these formats in the clipboard cache */
1448           for (i = 0; i < cSelectionTargets; i++)
1449           {
1450               LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupProperty(targetList[i]);
1451
1452               if (!lpFormat)
1453                   lpFormat = X11DRV_CLIPBOARD_LookupAliasProperty(targetList[i]);
1454
1455               if (!lpFormat)
1456               {
1457                   /* add it to the list of atoms that we don't know about yet */
1458                   if (!atoms) atoms = HeapAlloc( GetProcessHeap(), 0,
1459                                                  (cSelectionTargets - i) * sizeof(*atoms) );
1460                   if (atoms) atoms[nb_atoms++] = targetList[i];
1461               }
1462               else
1463               {
1464                   TRACE("Atom#%d Property(%d): --> FormatID(%d) %s\n",
1465                         i, lpFormat->drvData, lpFormat->wFormatID, lpFormat->Name);
1466                   X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, 0);
1467               }
1468           }
1469
1470           /* query all unknown atoms in one go */
1471           if (atoms)
1472           {
1473               char **names = HeapAlloc( GetProcessHeap(), 0, nb_atoms * sizeof(*names) );
1474               if (names)
1475               {
1476                   wine_tsx11_lock();
1477                   XGetAtomNames( display, atoms, nb_atoms, names );
1478                   wine_tsx11_unlock();
1479                   for (i = 0; i < nb_atoms; i++)
1480                   {
1481                       WINE_CLIPFORMAT *lpFormat = register_format( names[i], atoms[i] );
1482                       if (!lpFormat)
1483                       {
1484                           ERR("Failed to cache %s property\n", names[i]);
1485                           continue;
1486                       }
1487                       TRACE("Atom#%d Property(%d): --> FormatID(%d) %s\n",
1488                             i, lpFormat->drvData, lpFormat->wFormatID, lpFormat->Name);
1489                       X11DRV_CLIPBOARD_InsertClipboardData(lpFormat->wFormatID, 0, 0, 0);
1490                   }
1491                   wine_tsx11_lock();
1492                   for (i = 0; i < nb_atoms; i++) XFree( names[i] );
1493                   wine_tsx11_unlock();
1494                   HeapFree( GetProcessHeap(), 0, names );
1495               }
1496           }
1497        }
1498
1499        /* Free the list of targets */
1500        wine_tsx11_lock();
1501        XFree(targetList);
1502        wine_tsx11_unlock();
1503     }
1504
1505     return cSelectionTargets;
1506 }
1507
1508
1509 /**************************************************************************
1510  *      X11DRV_CLIPBOARD_ReadClipboardData
1511  *
1512  * This method is invoked only when we DO NOT own the X selection
1513  *
1514  * We always get the data from the selection client each time,
1515  * since we have no way of determining if the data in our cache is stale.
1516  */
1517 static BOOL X11DRV_CLIPBOARD_ReadClipboardData(UINT wFormat)
1518 {
1519     Display *display = thread_display();
1520     BOOL bRet = FALSE;
1521     Bool res;
1522
1523     HWND hWndClipWindow = GetOpenClipboardWindow();
1524     HWND hWnd = (hWndClipWindow) ? hWndClipWindow : GetActiveWindow();
1525
1526     LPWINE_CLIPFORMAT lpFormat;
1527
1528     TRACE("%d\n", wFormat);
1529
1530     if (!selectionAcquired)
1531     {
1532         Window w = X11DRV_get_whole_window(GetAncestor(hWnd, GA_ROOT));
1533         if(!w)
1534         {
1535             FIXME("No parent win found %p %p\n", hWnd, hWndClipWindow);
1536             return FALSE;
1537         }
1538
1539         lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
1540
1541         if (lpFormat->drvData)
1542         {
1543             DWORD i;
1544             UINT alias;
1545             XEvent xe;
1546
1547             TRACE("Requesting %s selection (%d) from win(%08x)\n", 
1548                 lpFormat->Name, lpFormat->drvData, (UINT)selectionCacheSrc);
1549
1550             wine_tsx11_lock();
1551             XConvertSelection(display, selectionCacheSrc, lpFormat->drvData,
1552                               x11drv_atom(SELECTION_DATA), w, CurrentTime);
1553             wine_tsx11_unlock();
1554
1555             /* wait until SelectionNotify is received */
1556             for (i = 0; i < SELECTION_RETRIES; i++)
1557             {
1558                 wine_tsx11_lock();
1559                 res = XCheckTypedWindowEvent(display, w, SelectionNotify, &xe);
1560                 wine_tsx11_unlock();
1561                 if (res && xe.xselection.selection == selectionCacheSrc) break;
1562
1563                 usleep(SELECTION_WAIT);
1564             }
1565
1566             /* If the property wasn't available check for aliases */
1567             if (xe.xselection.property == None &&
1568                 (alias = X11DRV_CLIPBOARD_LookupPropertyAlias(lpFormat->drvData)))
1569             {
1570                 wine_tsx11_lock();
1571                 XConvertSelection(display, selectionCacheSrc, alias,
1572                                   x11drv_atom(SELECTION_DATA), w, CurrentTime);
1573                 wine_tsx11_unlock();
1574
1575                 /* wait until SelectionNotify is received */
1576                 for (i = 0; i < SELECTION_RETRIES; i++)
1577                 {
1578                     wine_tsx11_lock();
1579                     res = XCheckTypedWindowEvent(display, w, SelectionNotify, &xe);
1580                     wine_tsx11_unlock();
1581                     if (res && xe.xselection.selection == selectionCacheSrc) break;
1582
1583                     usleep(SELECTION_WAIT);
1584                 }
1585             }
1586
1587             /* Verify that the selection returned a valid TARGETS property */
1588             if (xe.xselection.property != None)
1589             {
1590                 /*
1591                  *  Read the contents of the X selection property 
1592                  *  into WINE's clipboard cache converting the 
1593                  *  selection to be compatible if possible.
1594                  */
1595                 bRet = X11DRV_CLIPBOARD_ReadSelection(lpFormat, xe.xselection.requestor, 
1596                     xe.xselection.property);
1597             }
1598         }
1599     }
1600     else
1601     {
1602         ERR("Received request to cache selection data but process is owner\n");
1603     }
1604
1605     TRACE("Returning %d\n", bRet);
1606
1607     return bRet;
1608 }
1609
1610
1611 /**************************************************************************
1612  *              X11DRV_CLIPBOARD_ReadSelection
1613  *  Reads the contents of the X selection property into the WINE clipboard cache
1614  *  converting the selection into a format compatible with the windows clipboard
1615  *  if possible.
1616  *  This method is invoked only to read the contents of a the selection owned
1617  *  by an external application. i.e. when we do not own the X selection.
1618  */
1619 static BOOL X11DRV_CLIPBOARD_ReadSelection(LPWINE_CLIPFORMAT lpData, Window w, Atom prop)
1620 {
1621     Display *display = thread_display();
1622     Atom              atype=AnyPropertyType;
1623     int               aformat;
1624     unsigned long     total,nitems,remain,itemSize,val_cnt;
1625     long              lRequestLength,bwc;
1626     unsigned char*    val;
1627     unsigned char*    buffer;
1628     BOOL              bRet = FALSE;
1629
1630     if(prop == None)
1631         return bRet;
1632
1633     TRACE("Reading X selection type %s\n", lpData->Name);
1634
1635     /*
1636      * First request a zero length in order to figure out the request size.
1637      */
1638     wine_tsx11_lock();
1639     if(XGetWindowProperty(display,w,prop,0,0,False, AnyPropertyType,
1640         &atype, &aformat, &nitems, &itemSize, &val) != Success)
1641     {
1642         wine_tsx11_unlock();
1643         WARN("Failed to get property size\n");
1644         return bRet;
1645     }
1646
1647     /* Free zero length return data if any */
1648     if (val)
1649     {
1650        XFree(val);
1651        val = NULL;
1652     }
1653
1654     TRACE("Retrieving %ld bytes\n", itemSize * aformat/8);
1655
1656     lRequestLength = (itemSize * aformat/8)/4  + 1;
1657     bwc = aformat/8;
1658
1659     /* Read property in 4K blocks */
1660     if (XGetWindowProperty(display,w,prop,0,4096,False, AnyPropertyType/*reqType*/, 
1661         &atype, &aformat, &nitems, &remain, &buffer) != Success)
1662     {
1663         wine_tsx11_unlock();
1664         WARN("Failed to read property\n");
1665         return bRet;
1666     }
1667
1668     val = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nitems*bwc);
1669     memcpy(val,buffer,nitems*bwc);
1670
1671     XFree(buffer);
1672
1673     for (total = nitems*bwc, val_cnt = 0; remain;)
1674     {
1675        val_cnt +=nitems*bwc;
1676        if (XGetWindowProperty(display, w, prop, (total / 4), 4096, False,
1677            AnyPropertyType, &atype, &aformat, &nitems, &remain, &buffer) != Success)
1678        {
1679            wine_tsx11_unlock();
1680            WARN("Failed to read property\n");
1681            HeapFree(GetProcessHeap(), 0, val);
1682            return bRet;
1683        }
1684
1685        total += nitems*bwc;
1686        HeapReAlloc(GetProcessHeap(),0,val, total);
1687        memcpy(&val[val_cnt], buffer, nitems*(aformat/8));
1688        XFree(buffer);
1689     }
1690     wine_tsx11_unlock();
1691
1692     bRet = X11DRV_CLIPBOARD_InsertClipboardData(lpData->wFormatID, 0, lpData->lpDrvImportFunc(val, total), 0);
1693
1694     /* Delete the property on the window now that we are done
1695      * This will send a PropertyNotify event to the selection owner. */
1696     wine_tsx11_lock();
1697     XDeleteProperty(display,w,prop);
1698     wine_tsx11_unlock();
1699
1700     /* Free the retrieved property data */
1701     HeapFree(GetProcessHeap(),0,val);
1702
1703     return bRet;
1704 }
1705
1706
1707 /**************************************************************************
1708  *              CLIPBOARD_SerializeMetafile
1709  */
1710 static HANDLE X11DRV_CLIPBOARD_SerializeMetafile(INT wformat, HANDLE hdata, LPDWORD lpcbytes, BOOL out)
1711 {
1712     HANDLE h = 0;
1713
1714     TRACE(" wFormat=%d hdata=%08x out=%d\n", wformat, (unsigned int) hdata, out);
1715
1716     if (out) /* Serialize out, caller should free memory */
1717     {
1718         *lpcbytes = 0; /* Assume failure */
1719
1720         if (wformat == CF_METAFILEPICT)
1721         {
1722             LPMETAFILEPICT lpmfp = (LPMETAFILEPICT) GlobalLock(hdata);
1723             int size = GetMetaFileBitsEx(lpmfp->hMF, 0, NULL);
1724
1725             h = GlobalAlloc(0, size + sizeof(METAFILEPICT));
1726             if (h)
1727             {
1728                 char *pdata = GlobalLock(h);
1729
1730                 memcpy(pdata, lpmfp, sizeof(METAFILEPICT));
1731                 GetMetaFileBitsEx(lpmfp->hMF, size, pdata + sizeof(METAFILEPICT));
1732
1733                 *lpcbytes = size + sizeof(METAFILEPICT);
1734
1735                 GlobalUnlock(h);
1736             }
1737
1738             GlobalUnlock(hdata);
1739         }
1740         else if (wformat == CF_ENHMETAFILE)
1741         {
1742             int size = GetEnhMetaFileBits(hdata, 0, NULL);
1743
1744             h = GlobalAlloc(0, size);
1745             if (h)
1746             {
1747                 LPVOID pdata = GlobalLock(h);
1748
1749                 GetEnhMetaFileBits(hdata, size, pdata);
1750                 *lpcbytes = size;
1751
1752                 GlobalUnlock(h);
1753             }
1754         }
1755     }
1756     else
1757     {
1758         if (wformat == CF_METAFILEPICT)
1759         {
1760             h = GlobalAlloc(0, sizeof(METAFILEPICT));
1761             if (h)
1762             {
1763                 LPMETAFILEPICT pmfp = (LPMETAFILEPICT) GlobalLock(h);
1764
1765                 memcpy(pmfp, (LPVOID)hdata, sizeof(METAFILEPICT));
1766                 pmfp->hMF = SetMetaFileBitsEx(*lpcbytes - sizeof(METAFILEPICT),
1767                                               (char *)hdata + sizeof(METAFILEPICT));
1768
1769                 GlobalUnlock(h);
1770             }
1771         }
1772         else if (wformat == CF_ENHMETAFILE)
1773         {
1774             h = SetEnhMetaFileBits(*lpcbytes, (LPVOID)hdata);
1775         }
1776     }
1777
1778     return h;
1779 }
1780
1781
1782 /**************************************************************************
1783  *              X11DRV_CLIPBOARD_ReleaseSelection
1784  *
1785  * Release an XA_PRIMARY or XA_CLIPBOARD selection that we own, in response
1786  * to a SelectionClear event.
1787  * This can occur in response to another client grabbing the X selection.
1788  * If the XA_CLIPBOARD selection is lost, we relinquish XA_PRIMARY as well.
1789  */
1790 void X11DRV_CLIPBOARD_ReleaseSelection(Atom selType, Window w, HWND hwnd)
1791 {
1792     Display *display = thread_display();
1793
1794     /* w is the window that lost the selection
1795      */
1796     TRACE("event->window = %08x (selectionWindow = %08x) selectionAcquired=0x%08x\n",
1797           (unsigned)w, (unsigned)selectionWindow, (unsigned)selectionAcquired);
1798
1799     if (selectionAcquired)
1800     {
1801         if (w == selectionWindow)
1802         {
1803             /* If we're losing the CLIPBOARD selection, or if the preferences in .winerc
1804              * dictate that *all* selections should be cleared on loss of a selection,
1805              * we must give up all the selections we own.
1806              */
1807             if (clearAllSelections || (selType == x11drv_atom(CLIPBOARD)))
1808             {
1809                 CLIPBOARDINFO cbinfo;
1810
1811                 /* completely give up the selection */
1812                 TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
1813
1814               /* We are completely giving up the selection. There is a
1815                * potential race condition where the apps that now owns
1816                * the selection has already grabbed both selections. In
1817                * this case, if we clear any selection we may clear the
1818                * new owners selection. To prevent this common case we
1819                * try to open the clipboard. If we can't, we assume it
1820                * was a wine apps that took it and has taken both selections.
1821                * In this case, don't bother releasing the other selection.
1822                * Otherwise only release the selection if we still own it.
1823                */
1824                 X11DRV_CLIPBOARD_GetClipboardInfo(&cbinfo);
1825
1826                 if (cbinfo.flags & CB_OWNER)
1827                 {
1828                     /* Since we're still the owner, this wasn't initiated by 
1829                        another Wine process */
1830                     if (OpenClipboard(hwnd))
1831                     {
1832                       /* We really lost CLIPBOARD but want to voluntarily lose PRIMARY */
1833                       if ((selType == x11drv_atom(CLIPBOARD)) && (selectionAcquired & S_PRIMARY))
1834                       {
1835                           TRACE("Lost clipboard. Check if we need to release PRIMARY\n");
1836                           wine_tsx11_lock();
1837                           if (selectionWindow == XGetSelectionOwner(display,XA_PRIMARY))
1838                           {
1839                              TRACE("We still own PRIMARY. Releasing PRIMARY.\n");
1840                              XSetSelectionOwner(display, XA_PRIMARY, None, CurrentTime);
1841                           }
1842                           else
1843                              TRACE("We no longer own PRIMARY\n");
1844                           wine_tsx11_unlock();
1845                       }
1846
1847                       /* We really lost PRIMARY but want to voluntarily lose CLIPBOARD  */
1848                       if ((selType == XA_PRIMARY) && (selectionAcquired & S_CLIPBOARD))
1849                       {
1850                           TRACE("Lost PRIMARY. Check if we need to release CLIPBOARD\n");
1851                           wine_tsx11_lock();
1852                           if (selectionWindow == XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)))
1853                           {
1854                               TRACE("We still own CLIPBOARD. Releasing CLIPBOARD.\n");
1855                               XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), None, CurrentTime);
1856                           }
1857                           else
1858                               TRACE("We no longer own CLIPBOARD\n");
1859                           wine_tsx11_unlock();
1860                       }
1861
1862                       /* Destroy private objects */
1863                       SendMessageW(cbinfo.hWndOwner, WM_DESTROYCLIPBOARD, 0, 0);
1864    
1865                       /* Give up ownership of the windows clipboard */
1866                       X11DRV_CLIPBOARD_ReleaseOwnership();
1867
1868                       CloseClipboard();
1869                     }
1870                 }
1871                 else
1872                 {
1873                     TRACE("Lost selection to other Wine process.\n");
1874                 }
1875
1876                 selectionWindow = None;
1877                 PrimarySelectionOwner = ClipboardSelectionOwner = 0;
1878
1879                 X11DRV_EmptyClipboard();
1880
1881                 /* Reset the selection flags now that we are done */
1882                 selectionAcquired = S_NOSELECTION;
1883             }
1884             else if ( selType == XA_PRIMARY ) /* Give up only PRIMARY selection */
1885             {
1886                 TRACE("Lost PRIMARY selection\n");
1887                 PrimarySelectionOwner = 0;
1888                 selectionAcquired &= ~S_PRIMARY;  /* clear S_PRIMARY mask */
1889             }
1890         }
1891     }
1892 }
1893
1894
1895 /**************************************************************************
1896  *              IsSelectionOwner (X11DRV.@)
1897  *
1898  * Returns: TRUE if the selection is owned by this process, FALSE otherwise
1899  */
1900 static BOOL X11DRV_CLIPBOARD_IsSelectionOwner(void)
1901 {
1902     return selectionAcquired;
1903 }
1904
1905
1906 /**************************************************************************
1907  *                X11DRV Clipboard Exports
1908  **************************************************************************/
1909
1910
1911 /**************************************************************************
1912  *              RegisterClipboardFormat (X11DRV.@)
1913  *
1914  * Registers a custom X clipboard format
1915  * Returns: Format id or 0 on failure
1916  */
1917 INT X11DRV_RegisterClipboardFormat(LPCSTR FormatName)
1918 {
1919     LPWINE_CLIPFORMAT lpFormat;
1920
1921     if (FormatName == NULL) return 0;
1922     if (!(lpFormat = register_format( FormatName, 0 ))) return 0;
1923     return lpFormat->wFormatID;
1924 }
1925
1926
1927 /**************************************************************************
1928  *              X11DRV_GetClipboardFormatName
1929  */
1930 INT X11DRV_GetClipboardFormatName(UINT wFormat, LPSTR retStr, INT maxlen)
1931 {
1932     INT len = 0;
1933     LPWINE_CLIPFORMAT lpFormat = X11DRV_CLIPBOARD_LookupFormat(wFormat);
1934
1935     TRACE("(%04X, %p, %d) !\n", wFormat, retStr, maxlen);
1936
1937     if (!lpFormat || (lpFormat->wFlags & CF_FLAG_BUILTINFMT))
1938     {
1939         TRACE("Unknown format 0x%08x!\n", wFormat);
1940         SetLastError(ERROR_INVALID_PARAMETER);
1941     }
1942     else
1943     {
1944         strncpy(retStr, lpFormat->Name, maxlen - 1);
1945         retStr[maxlen - 1] = 0;
1946
1947         len = strlen(retStr);
1948     }
1949
1950     return len;
1951 }
1952
1953
1954 /**************************************************************************
1955  *              AcquireClipboard (X11DRV.@)
1956  */
1957 void X11DRV_AcquireClipboard(HWND hWndClipWindow)
1958 {
1959     Display *display = thread_display();
1960
1961     /*
1962      * Acquire X selection if we don't already own it.
1963      * Note that we only acquire the selection if it hasn't been already
1964      * acquired by us, and ignore the fact that another X window may be
1965      * asserting ownership. The reason for this is we need *any* top level
1966      * X window to hold selection ownership. The actual clipboard data requests
1967      * are made via GetClipboardData from EVENT_SelectionRequest and this
1968      * ensures that the real HWND owner services the request.
1969      * If the owning X window gets destroyed the selection ownership is
1970      * re-cycled to another top level X window in X11DRV_CLIPBOARD_ResetOwner.
1971      *
1972      */
1973     if (!(selectionAcquired == (S_PRIMARY | S_CLIPBOARD)))
1974     {
1975         Window owner;
1976
1977         if (!hWndClipWindow)
1978             hWndClipWindow = GetActiveWindow();
1979
1980         owner = X11DRV_get_whole_window(GetAncestor(hWndClipWindow, GA_ROOT));
1981
1982         wine_tsx11_lock();
1983         /* Grab PRIMARY selection if not owned */
1984         if (usePrimary && !(selectionAcquired & S_PRIMARY))
1985             XSetSelectionOwner(display, XA_PRIMARY, owner, CurrentTime);
1986
1987         /* Grab CLIPBOARD selection if not owned */
1988         if (!(selectionAcquired & S_CLIPBOARD))
1989             XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), owner, CurrentTime);
1990
1991         if (usePrimary && XGetSelectionOwner(display,XA_PRIMARY) == owner)
1992             selectionAcquired |= S_PRIMARY;
1993
1994         if (XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)) == owner)
1995             selectionAcquired |= S_CLIPBOARD;
1996         wine_tsx11_unlock();
1997
1998         if (selectionAcquired)
1999         {
2000             selectionWindow = owner;
2001             TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner);
2002         }
2003     }
2004     else
2005     {
2006         WARN("Received request to acquire selection but process is already owner=(%08x)\n", (unsigned) selectionWindow);
2007
2008         selectionAcquired  = S_NOSELECTION;
2009
2010         wine_tsx11_lock();
2011         if (XGetSelectionOwner(display,XA_PRIMARY) == selectionWindow)
2012             selectionAcquired |= S_PRIMARY;
2013
2014         if (XGetSelectionOwner(display,x11drv_atom(CLIPBOARD)) == selectionWindow)
2015             selectionAcquired |= S_CLIPBOARD;
2016         wine_tsx11_unlock();
2017
2018         if (!(selectionAcquired == (S_PRIMARY | S_CLIPBOARD)))
2019         {
2020             WARN("Lost selection but process didn't process SelectClear\n");
2021             selectionWindow = None;
2022         }
2023     }
2024 }
2025
2026
2027 /**************************************************************************
2028  *      X11DRV_EmptyClipboard
2029  */
2030 void X11DRV_EmptyClipboard(void)
2031 {
2032     if (ClipData)
2033     {
2034         LPWINE_CLIPDATA lpData;
2035         LPWINE_CLIPDATA lpNext = ClipData;
2036
2037         do
2038         {
2039             lpData = lpNext;
2040             lpNext = lpData->NextData;
2041             lpData->PrevData->NextData = lpData->NextData;
2042             lpData->NextData->PrevData = lpData->PrevData;
2043             X11DRV_CLIPBOARD_FreeData(lpData);
2044             HeapFree(GetProcessHeap(), 0, lpData);
2045         } while (lpNext != lpData);
2046     }
2047
2048     TRACE(" %d entries deleted from cache.\n", ClipDataCount);
2049
2050     ClipData = NULL;
2051     ClipDataCount = 0;
2052 }
2053
2054
2055
2056 /**************************************************************************
2057  *              X11DRV_SetClipboardData
2058  */
2059 BOOL X11DRV_SetClipboardData(UINT wFormat, HANDLE16 hData16, HANDLE hData32)
2060 {
2061     BOOL bResult = FALSE;
2062
2063     if (X11DRV_CLIPBOARD_InsertClipboardData(wFormat, hData16, hData32, 0))
2064         bResult = TRUE;
2065
2066     return bResult;
2067 }
2068
2069
2070 /**************************************************************************
2071  *              CountClipboardFormats
2072  */
2073 INT X11DRV_CountClipboardFormats(void)
2074 {
2075     CLIPBOARDINFO cbinfo;
2076
2077     X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2078
2079     TRACE(" count=%d\n", ClipDataCount);
2080
2081     return ClipDataCount;
2082 }
2083
2084
2085 /**************************************************************************
2086  *              X11DRV_EnumClipboardFormats
2087  */
2088 UINT X11DRV_EnumClipboardFormats(UINT wFormat)
2089 {
2090     CLIPBOARDINFO cbinfo;
2091     UINT wNextFormat = 0;
2092
2093     TRACE("(%04X)\n", wFormat);
2094
2095     X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2096
2097     if (!wFormat)
2098     {
2099         if (ClipData)
2100             wNextFormat = ClipData->wFormatID;
2101     }
2102     else
2103     {
2104         LPWINE_CLIPDATA lpData = X11DRV_CLIPBOARD_LookupData(wFormat);
2105
2106         if (lpData && lpData->NextData != ClipData)
2107             wNextFormat = lpData->NextData->wFormatID;
2108     }
2109
2110     return wNextFormat;
2111 }
2112
2113
2114 /**************************************************************************
2115  *              X11DRV_IsClipboardFormatAvailable
2116  */
2117 BOOL X11DRV_IsClipboardFormatAvailable(UINT wFormat)
2118 {
2119     BOOL bRet = FALSE;
2120     CLIPBOARDINFO cbinfo;
2121
2122     TRACE("(%04X)\n", wFormat);
2123
2124     X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2125
2126     if (wFormat != 0 && X11DRV_CLIPBOARD_LookupData(wFormat))
2127         bRet = TRUE;
2128
2129     TRACE("(%04X)- ret(%d)\n", wFormat, bRet);
2130
2131     return bRet;
2132 }
2133
2134
2135 /**************************************************************************
2136  *              GetClipboardData (USER.142)
2137  */
2138 BOOL X11DRV_GetClipboardData(UINT wFormat, HANDLE16* phData16, HANDLE* phData32)
2139 {
2140     CLIPBOARDINFO cbinfo;
2141     LPWINE_CLIPDATA lpRender;
2142
2143     TRACE("(%04X)\n", wFormat);
2144
2145     X11DRV_CLIPBOARD_UpdateCache(&cbinfo);
2146
2147     if ((lpRender = X11DRV_CLIPBOARD_LookupData(wFormat)))
2148     {
2149         if ( !lpRender->hData32 )
2150             X11DRV_CLIPBOARD_RenderFormat(lpRender);
2151
2152         /* Convert between 32 -> 16 bit data, if necessary */
2153         if (lpRender->hData32 && !lpRender->hData16)
2154         {
2155             int size;
2156
2157             if (lpRender->wFormatID == CF_METAFILEPICT)
2158                 size = sizeof(METAFILEPICT16);
2159             else
2160                 size = GlobalSize(lpRender->hData32);
2161
2162             lpRender->hData16 = GlobalAlloc16(GMEM_ZEROINIT, size);
2163
2164             if (!lpRender->hData16)
2165                 ERR("(%04X) -- not enough memory in 16b heap\n", wFormat);
2166             else
2167             {
2168                 if (lpRender->wFormatID == CF_METAFILEPICT)
2169                 {
2170                     FIXME("\timplement function CopyMetaFilePict32to16\n");
2171                     FIXME("\tin the appropriate file.\n");
2172   #ifdef SOMEONE_IMPLEMENTED_ME
2173                     CopyMetaFilePict32to16(GlobalLock16(lpRender->hData16),
2174                         GlobalLock(lpRender->hData32));
2175   #endif
2176                 }
2177                 else
2178                 {
2179                     memcpy(GlobalLock16(lpRender->hData16),
2180                         GlobalLock(lpRender->hData32), size);
2181                 }
2182
2183                 GlobalUnlock16(lpRender->hData16);
2184                 GlobalUnlock(lpRender->hData32);
2185             }
2186         }
2187
2188         /* Convert between 32 -> 16 bit data, if necessary */
2189         if (lpRender->hData16 && !lpRender->hData32)
2190         {
2191             int size;
2192
2193             if (lpRender->wFormatID == CF_METAFILEPICT)
2194                 size = sizeof(METAFILEPICT16);
2195             else
2196                 size = GlobalSize(lpRender->hData32);
2197
2198             lpRender->hData32 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE | 
2199                 GMEM_DDESHARE, size);
2200
2201             if (lpRender->wFormatID == CF_METAFILEPICT)
2202             {
2203                 FIXME("\timplement function CopyMetaFilePict16to32\n");
2204                 FIXME("\tin the appropriate file.\n");
2205 #ifdef SOMEONE_IMPLEMENTED_ME
2206                 CopyMetaFilePict16to32(GlobalLock16(lpRender->hData32),
2207                     GlobalLock(lpRender->hData16));
2208 #endif
2209             }
2210             else
2211             {
2212                 memcpy(GlobalLock(lpRender->hData32),
2213                     GlobalLock16(lpRender->hData16), size);
2214             }
2215
2216             GlobalUnlock(lpRender->hData32);
2217             GlobalUnlock16(lpRender->hData16);
2218         }
2219
2220         if (phData16)
2221             *phData16 = lpRender->hData16;
2222
2223         if (phData32)
2224             *phData32 = lpRender->hData32;
2225
2226         TRACE(" returning hData16(%04x) hData32(%04x) (type %d)\n", 
2227             lpRender->hData16, (unsigned int) lpRender->hData32, lpRender->wFormatID);
2228
2229         return lpRender->hData16 || lpRender->hData32;
2230     }
2231
2232     return 0;
2233 }
2234
2235
2236 /**************************************************************************
2237  *              ResetSelectionOwner (X11DRV.@)
2238  *
2239  * Called from DestroyWindow() to prevent X selection from being lost when
2240  * a top level window is destroyed, by switching ownership to another top
2241  * level window.
2242  * Any top level window can own the selection. See X11DRV_CLIPBOARD_Acquire
2243  * for a more detailed description of this.
2244  */
2245 void X11DRV_ResetSelectionOwner(HWND hwnd, BOOL bFooBar)
2246 {
2247     Display *display = thread_display();
2248     HWND hWndClipOwner = 0;
2249     HWND tmp;
2250     Window XWnd = X11DRV_get_whole_window(hwnd);
2251     BOOL bLostSelection = FALSE;
2252     Window selectionPrevWindow;
2253
2254     /* There is nothing to do if we don't own the selection,
2255      * or if the X window which currently owns the selection is different
2256      * from the one passed in.
2257      */
2258     if (!selectionAcquired || XWnd != selectionWindow
2259          || selectionWindow == None )
2260        return;
2261
2262     if ((bFooBar && XWnd) || (!bFooBar && !XWnd))
2263        return;
2264
2265     hWndClipOwner = GetClipboardOwner();
2266
2267     TRACE("clipboard owner = %p, selection window = %08x\n",
2268           hWndClipOwner, (unsigned)selectionWindow);
2269
2270     /* now try to salvage current selection from being destroyed by X */
2271     TRACE("checking %08x\n", (unsigned) XWnd);
2272
2273     selectionPrevWindow = selectionWindow;
2274     selectionWindow = None;
2275
2276     if (!(tmp = GetWindow(hwnd, GW_HWNDNEXT)))
2277         tmp = GetWindow(hwnd, GW_HWNDFIRST);
2278
2279     if (tmp && tmp != hwnd) 
2280         selectionWindow = X11DRV_get_whole_window(tmp);
2281
2282     if (selectionWindow != None)
2283     {
2284         /* We must pretend that we don't own the selection while making the switch
2285          * since a SelectionClear event will be sent to the last owner.
2286          * If there is no owner X11DRV_CLIPBOARD_ReleaseSelection will do nothing.
2287          */
2288         int saveSelectionState = selectionAcquired;
2289         selectionAcquired = S_NOSELECTION;
2290
2291         TRACE("\tswitching selection from %08x to %08x\n",
2292                     (unsigned)selectionPrevWindow, (unsigned)selectionWindow);
2293
2294         wine_tsx11_lock();
2295
2296         /* Assume ownership for the PRIMARY and CLIPBOARD selection */
2297         if (saveSelectionState & S_PRIMARY)
2298             XSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime);
2299
2300         XSetSelectionOwner(display, x11drv_atom(CLIPBOARD), selectionWindow, CurrentTime);
2301
2302         /* Restore the selection masks */
2303         selectionAcquired = saveSelectionState;
2304
2305         /* Lose the selection if something went wrong */
2306         if (((saveSelectionState & S_PRIMARY) &&
2307            (XGetSelectionOwner(display, XA_PRIMARY) != selectionWindow)) || 
2308            (XGetSelectionOwner(display, x11drv_atom(CLIPBOARD)) != selectionWindow))
2309         {
2310             bLostSelection = TRUE;
2311         }
2312         else
2313         {
2314             /* Update selection state */
2315             if (saveSelectionState & S_PRIMARY)
2316                PrimarySelectionOwner = selectionWindow;
2317
2318             ClipboardSelectionOwner = selectionWindow;
2319         }
2320         wine_tsx11_unlock();
2321     }
2322     else
2323     {
2324         bLostSelection = TRUE;
2325     }
2326
2327     if (bLostSelection)
2328     {
2329         TRACE("Lost the selection!\n");
2330
2331         X11DRV_CLIPBOARD_ReleaseOwnership();
2332         selectionAcquired = S_NOSELECTION;
2333         ClipboardSelectionOwner = PrimarySelectionOwner = 0;
2334         selectionWindow = 0;
2335     }
2336 }
2337
2338
2339 /**************************************************************************
2340  *                      X11DRV_CLIPBOARD_SynthesizeData
2341  */
2342 static BOOL X11DRV_CLIPBOARD_SynthesizeData(UINT wFormatID)
2343 {
2344     BOOL bsyn = TRUE;
2345     LPWINE_CLIPDATA lpSource = NULL;
2346
2347     TRACE(" %d\n", wFormatID);
2348
2349     /* Don't need to synthesize if it already exists */
2350     if (X11DRV_CLIPBOARD_LookupData(wFormatID))
2351         return TRUE;
2352
2353     if (wFormatID == CF_UNICODETEXT || wFormatID == CF_TEXT || wFormatID == CF_OEMTEXT)
2354     {
2355         bsyn = ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_UNICODETEXT)) &&
2356             ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2357             ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_TEXT)) &&
2358             ~lpSource->wFlags & CF_FLAG_SYNTHESIZED) ||
2359             ((lpSource = X11DRV_CLIPBOARD_LookupData(CF_OEMTEXT)) &&
2360             ~lpSource->wFlags & CF_FLAG_SYNTHESIZED);
2361     }
2362     else if (wFormatID == CF_ENHMETAFILE)
2363     {
2364         bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT)) &&
2365             ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2366     }
2367     else if (wFormatID == CF_METAFILEPICT)
2368     {
2369         bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_METAFILEPICT)) &&
2370             ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2371     }
2372     else if (wFormatID == CF_DIB)
2373     {
2374         bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_BITMAP)) &&
2375             ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2376     }
2377     else if (wFormatID == CF_BITMAP)
2378     {
2379         bsyn = (lpSource = X11DRV_CLIPBOARD_LookupData(CF_DIB)) &&
2380             ~lpSource->wFlags & CF_FLAG_SYNTHESIZED;
2381     }
2382
2383     if (bsyn)
2384         X11DRV_CLIPBOARD_InsertClipboardData(wFormatID, 0, 0, CF_FLAG_SYNTHESIZED);
2385
2386     return bsyn;
2387 }
2388
2389
2390
2391 /**************************************************************************
2392  *              X11DRV_EndClipboardUpdate
2393  * TODO:
2394  *  Add locale if it hasn't already been added
2395  */
2396 void X11DRV_EndClipboardUpdate(void)
2397 {
2398     INT count = ClipDataCount;
2399
2400     /* Do Unicode <-> Text <-> OEM mapping */
2401     X11DRV_CLIPBOARD_SynthesizeData(CF_UNICODETEXT);
2402     X11DRV_CLIPBOARD_SynthesizeData(CF_TEXT);
2403     X11DRV_CLIPBOARD_SynthesizeData(CF_OEMTEXT);
2404
2405     /* Enhmetafile <-> MetafilePict mapping */
2406     X11DRV_CLIPBOARD_SynthesizeData(CF_ENHMETAFILE);
2407     X11DRV_CLIPBOARD_SynthesizeData(CF_METAFILEPICT);
2408
2409     /* DIB <-> Bitmap mapping */
2410     X11DRV_CLIPBOARD_SynthesizeData(CF_DIB);
2411     X11DRV_CLIPBOARD_SynthesizeData(CF_BITMAP);
2412
2413     TRACE("%d formats added to cached data\n", ClipDataCount - count);
2414 }