- Partially implement LdrGetDllHandle.
[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  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * NOTES:
23  *    This file contains the X specific implementation for the windows
24  *    Clipboard API.
25  *
26  *    Wine's internal clipboard is exposed to external apps via the X
27  *    selection mechanism.
28  *    Currently the driver asserts ownership via two selection atoms:
29  *    1. PRIMARY(XA_PRIMARY)
30  *    2. CLIPBOARD
31  *
32  *    In our implementation, the CLIPBOARD selection takes precedence over PRIMARY,
33  *    i.e. if a CLIPBOARD selection is available, it is used instead of PRIMARY.
34  *    When Wine takes ownership of the clipboard, it takes ownership of BOTH selections.
35  *    While giving up selection ownership, if the CLIPBOARD selection is lost,
36  *    it will lose both PRIMARY and CLIPBOARD and empty the clipboard.
37  *    However if only PRIMARY is lost, it will continue to hold the CLIPBOARD selection
38  *    (leaving the clipboard cache content unaffected).
39  *
40  *      Every format exposed via a windows clipboard format is also exposed through
41  *    a corresponding X selection target. A selection target atom is synthesized
42  *    whenever a new Windows clipboard format is registered via RegisterClipboardFormat,
43  *    or when a built-in format is used for the first time.
44  *    Windows native format are exposed by prefixing the format name with "<WCF>"
45  *    This allows us to uniquely identify windows native formats exposed by other
46  *    running WINE apps.
47  *
48  *      In order to allow external applications to query WINE for supported formats,
49  *    we respond to the "TARGETS" selection target. (See EVENT_SelectionRequest
50  *    for implementation) We use the same mechanism to query external clients for
51  *    availability of a particular format, by caching the list of available targets
52  *    by using the clipboard cache's "delayed render" mechanism. If a selection client
53  *    does not support the "TARGETS" selection target, we actually attempt to retrieve
54  *    the format requested as a fallback mechanism.
55  *
56  *      Certain Windows native formats are automatically converted to X native formats
57  *    and vice versa. If a native format is available in the selection, it takes
58  *    precedence, in order to avoid unnecessary conversions.
59  *
60  */
61
62 #include "config.h"
63
64 #include <string.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <unistd.h>
68 #include <fcntl.h>
69
70 #include "ts_xlib.h"
71 #include "winreg.h"
72 #include "clipboard.h"
73 #include "win.h"
74 #include "x11drv.h"
75 #include "wine/debug.h"
76
77 WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
78
79 /* Selection masks */
80
81 #define S_NOSELECTION    0
82 #define S_PRIMARY        1
83 #define S_CLIPBOARD      2
84
85 /* X selection context info */
86
87 static char _CLIPBOARD[] = "CLIPBOARD";        /* CLIPBOARD atom name */
88 static char FMT_PREFIX[] = "<WCF>";            /* Prefix for windows specific formats */
89 static int selectionAcquired = 0;              /* Contains the current selection masks */
90 static Window selectionWindow = None;          /* The top level X window which owns the selection */
91 static Window selectionPrevWindow = None;      /* The last X window that owned the selection */
92 static Window PrimarySelectionOwner = None;    /* The window which owns the primary selection */
93 static Window ClipboardSelectionOwner = None;  /* The window which owns the clipboard selection */
94 static unsigned long cSelectionTargets = 0;    /* Number of target formats reported by TARGETS selection */
95 static Atom selectionCacheSrc = XA_PRIMARY;    /* The selection source from which the clipboard cache was filled */
96 static HANDLE selectionClearEvent = 0;/* Synchronization object used to block until server is started */
97
98 typedef struct tagPROPERTY
99 {
100     struct tagPROPERTY *next;
101     Atom                atom;
102     Pixmap              pixmap;
103 } PROPERTY;
104
105 static PROPERTY *prop_head;
106
107
108 /**************************************************************************
109  *              X11DRV_CLIPBOARD_MapPropertyToFormat
110  *
111  *  Map an X selection property type atom name to a windows clipboard format ID
112  */
113 UINT X11DRV_CLIPBOARD_MapPropertyToFormat(char *itemFmtName)
114 {
115     /*
116      * If the property name starts with FMT_PREFIX strip this off and
117      * get the ID for a custom Windows registered format with this name.
118      * We can also understand STRING, PIXMAP and BITMAP.
119      */
120     if ( NULL == itemFmtName )
121         return 0;
122     else if ( 0 == strncmp(itemFmtName, FMT_PREFIX, strlen(FMT_PREFIX)) )
123         return RegisterClipboardFormatA(itemFmtName + strlen(FMT_PREFIX));
124     else if ( 0 == strcmp(itemFmtName, "STRING") )
125         return CF_UNICODETEXT;
126     else if ( 0 == strcmp(itemFmtName, "PIXMAP")
127                 ||  0 == strcmp(itemFmtName, "BITMAP") )
128     {
129         /*
130          * Return CF_DIB as first preference, if WINE is the selection owner
131          * and if CF_DIB exists in the cache.
132          * If wine dowsn't own the selection we always return CF_DIB
133          */
134         if ( !X11DRV_IsSelectionOwner() )
135             return CF_DIB;
136         else if ( CLIPBOARD_IsPresent(CF_DIB) )
137             return CF_DIB;
138         else
139             return CF_BITMAP;
140     }
141
142     WARN("\tNo mapping to Windows clipboard format for property %s\n", itemFmtName);
143     return 0;
144 }
145
146 /**************************************************************************
147  *              X11DRV_CLIPBOARD_MapFormatToProperty
148  *
149  *  Map a windows clipboard format ID to an X selection property atom
150  */
151 Atom X11DRV_CLIPBOARD_MapFormatToProperty(UINT wFormat)
152 {
153     Atom prop = None;
154
155     switch (wFormat)
156     {
157         /* We support only CF_UNICODETEXT, other formats are synthesized */
158         case CF_OEMTEXT:
159         case CF_TEXT:
160             return None;
161
162         case CF_UNICODETEXT:
163             prop = XA_STRING;
164             break;
165
166         case CF_DIB:
167         case CF_BITMAP:
168         {
169             /*
170              * Request a PIXMAP, only if WINE is NOT the selection owner,
171              * AND the requested format is not in the cache.
172              */
173             if ( !X11DRV_IsSelectionOwner() && !CLIPBOARD_IsPresent(wFormat) )
174             {
175               prop = XA_PIXMAP;
176               break;
177             }
178             /* Fall through to the default case in order to use the native format */
179         }
180
181         default:
182         {
183             /*
184              * If an X atom is registered for this format, return that
185              * Otherwise register a new atom.
186              */
187             char str[256];
188             char *fmtName = CLIPBOARD_GetFormatName(wFormat);
189             strcpy(str, FMT_PREFIX);
190
191             if (fmtName)
192             {
193                 strncat(str, fmtName, sizeof(str) - strlen(FMT_PREFIX));
194                 prop = TSXInternAtom(thread_display(), str, False);
195             }
196             break;
197         }
198     }
199
200     if (prop == None)
201         TRACE("\tNo mapping to X property for Windows clipboard format %d(%s)\n",
202               wFormat, CLIPBOARD_GetFormatName(wFormat));
203
204     return prop;
205 }
206
207 /**************************************************************************
208  *              X11DRV_CLIPBOARD_IsNativeProperty
209  *
210  *  Checks if a property is a native Wine property type
211  */
212 BOOL X11DRV_CLIPBOARD_IsNativeProperty(Atom prop)
213 {
214     char *itemFmtName = TSXGetAtomName(thread_display(), prop);
215     BOOL bRet = FALSE;
216
217     if ( 0 == strncmp(itemFmtName, FMT_PREFIX, strlen(FMT_PREFIX)) )
218         bRet = TRUE;
219
220     TSXFree(itemFmtName);
221     return bRet;
222 }
223
224
225 /**************************************************************************
226  *              X11DRV_CLIPBOARD_LaunchServer
227  * Launches the clipboard server. This is called from X11DRV_CLIPBOARD_ResetOwner
228  * when the selection can no longer be recyled to another top level window.
229  * In order to make the selection persist after Wine shuts down a server
230  * process is launched which services subsequent selection requests.
231  */
232 BOOL X11DRV_CLIPBOARD_LaunchServer()
233 {
234     int iWndsLocks;
235     char clearSelection[8] = "0";
236     int persistent_selection = 1;
237     HKEY hkey;
238     int fd[2], err;
239
240     /* If persistant selection has been disabled in the .winerc Clipboard section,
241      * don't launch the server
242      */
243     if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Clipboard", &hkey))
244     {
245         char buffer[20];
246         DWORD type, count = sizeof(buffer);
247         if(!RegQueryValueExA(hkey, "PersistentSelection", 0, &type, buffer, &count))
248             persistent_selection = atoi(buffer);
249
250         /* Get the clear selection preference */
251         count = sizeof(clearSelection);
252         RegQueryValueExA(hkey, "ClearAllSelections", 0, &type, clearSelection, &count);
253         RegCloseKey(hkey);
254     }
255     if ( !persistent_selection )
256         return FALSE;
257
258     /*  Start up persistant WINE X clipboard server process which will
259      *  take ownership of the X selection and continue to service selection
260      *  requests from other apps.
261      */
262
263     if(pipe(fd) == -1) return FALSE;
264     fcntl(fd[1], F_SETFD, 1); /* set close on exec */
265
266     selectionWindow = selectionPrevWindow;
267     if ( !fork() )
268     {
269         /* NOTE: This code only executes in the context of the child process
270          * Do note make any Wine specific calls here.
271          */
272         int dbgClasses = 0;
273         char selMask[8], dbgClassMask[8];
274
275         close(fd[0]);
276         sprintf(selMask, "%d", selectionAcquired);
277
278         /* Build the debug class mask to pass to the server, by inheriting
279          * the settings for the clipboard debug channel.
280          */
281         dbgClasses |= FIXME_ON(clipboard) ? 1 : 0;
282         dbgClasses |= ERR_ON(clipboard) ? 2 : 0;
283         dbgClasses |= WARN_ON(clipboard) ? 4 : 0;
284         dbgClasses |= TRACE_ON(clipboard) ? 8 : 0;
285         sprintf(dbgClassMask, "%d", dbgClasses);
286
287         /* Exec the clipboard server passing it the selection and debug class masks */
288         execl( BINDIR "/wineclipsrv", "wineclipsrv",
289                selMask, dbgClassMask, clearSelection, NULL );
290         execlp( "wineclipsrv", "wineclipsrv", selMask, dbgClassMask, clearSelection, NULL );
291
292         /* Exec Failed! */
293         perror("Could not start Wine clipboard server");
294         write(fd[1], &err, sizeof(err));
295         _exit( 1 ); /* Exit the child process */
296     }
297     close(fd[1]);
298
299     if(read(fd[0], &err, sizeof(err)) > 0) { /* exec failed */
300         close(fd[0]);
301         return FALSE;
302     }
303     close(fd[0]);
304
305     /* Wait until the clipboard server acquires the selection.
306      * We must release the windows lock to enable Wine to process
307      * selection messages in response to the servers requests.
308      */
309
310     iWndsLocks = WIN_SuspendWndsLock();
311
312     /* We must wait until the server finishes acquiring the selection,
313      * before proceeding, otherwise the window which owns the selection
314      * will be destroyed prematurely!
315      * Create a non-signalled, auto-reset event which will be set by
316      * X11DRV_CLIPBOARD_ReleaseSelection, and wait until this gets
317      * signalled before proceeding.
318      */
319
320     if ( !(selectionClearEvent = CreateEventA(NULL, FALSE, FALSE, NULL)) )
321         ERR("Could not create wait object. Clipboard server won't start!\n");
322     else
323     {
324         /* Wait until we lose the selection, timing out after a minute */
325         DWORD start_time, timeout, elapsed, ret;
326
327         TRACE("Waiting for clipboard server to acquire selection\n");
328
329         timeout = 60000;
330         start_time = GetTickCount();
331         elapsed=0;
332         do
333         {
334             ret = MsgWaitForMultipleObjects( 1, &selectionClearEvent, FALSE, timeout - elapsed, QS_ALLINPUT );
335             if (ret != WAIT_OBJECT_0+1)
336                 break;
337             elapsed = GetTickCount() - start_time;
338             if (elapsed > timeout)
339                 break;
340         }
341         while (1);
342         if ( ret != WAIT_OBJECT_0 )
343             TRACE("Server could not acquire selection, or a timeout occurred!\n");
344         else
345             TRACE("Server successfully acquired selection\n");
346
347         /* Release the event */
348         CloseHandle(selectionClearEvent);
349         selectionClearEvent = 0;
350     }
351
352     WIN_RestoreWndsLock(iWndsLocks);
353
354     return TRUE;
355 }
356
357
358 /**************************************************************************
359  *              X11DRV_CLIPBOARD_CacheDataFormats
360  *
361  * Caches the list of data formats available from the current selection.
362  * This queries the selection owner for the TARGETS property and saves all
363  * reported property types.
364  */
365 int X11DRV_CLIPBOARD_CacheDataFormats( Atom SelectionName )
366 {
367     Display *display = thread_display();
368     HWND           hWnd = 0;
369     HWND           hWndClipWindow = GetOpenClipboardWindow();
370     XEvent         xe;
371     Atom           aTargets;
372     Atom           atype=AnyPropertyType;
373     int            aformat;
374     unsigned long  remain;
375     Atom*          targetList=NULL;
376     Window         w;
377     Window         ownerSelection = 0;
378
379     TRACE("enter\n");
380     /*
381      * Empty the clipboard cache
382      */
383     CLIPBOARD_EmptyCache(TRUE);
384
385     cSelectionTargets = 0;
386     selectionCacheSrc = SelectionName;
387
388     hWnd = (hWndClipWindow) ? hWndClipWindow : GetActiveWindow();
389
390     ownerSelection = TSXGetSelectionOwner(display, SelectionName);
391     if ( !hWnd || (ownerSelection == None) )
392         return cSelectionTargets;
393
394     /*
395      * Query the selection owner for the TARGETS property
396      */
397     w = X11DRV_get_whole_window( GetAncestor(hWnd,GA_ROOT) );
398
399     aTargets = TSXInternAtom(display, "TARGETS", False);
400
401     TRACE("Requesting TARGETS selection for '%s' (owner=%08x)...\n",
402           TSXGetAtomName(display, selectionCacheSrc), (unsigned)ownerSelection );
403     wine_tsx11_lock();
404     XConvertSelection(display, selectionCacheSrc, aTargets,
405                     TSXInternAtom(display, "SELECTION_DATA", False),
406                     w, CurrentTime);
407
408     /*
409      * Wait until SelectionNotify is received
410      */
411     while( TRUE )
412     {
413        if( XCheckTypedWindowEvent(display, w, SelectionNotify, &xe) )
414            if( xe.xselection.selection == selectionCacheSrc )
415                break;
416     }
417     wine_tsx11_unlock();
418
419     /* Verify that the selection returned a valid TARGETS property */
420     if ( (xe.xselection.target != aTargets)
421           || (xe.xselection.property == None) )
422     {
423         TRACE("\tExit, could not retrieve TARGETS\n");
424         return cSelectionTargets;
425     }
426
427     /* Read the TARGETS property contents */
428     if(TSXGetWindowProperty(display, xe.xselection.requestor, xe.xselection.property,
429                             0, 0x3FFF, True, AnyPropertyType/*XA_ATOM*/, &atype, &aformat,
430                             &cSelectionTargets, &remain, (unsigned char**)&targetList) != Success)
431         TRACE("\tCouldn't read TARGETS property\n");
432     else
433     {
434        TRACE("\tType %s,Format %d,nItems %ld, Remain %ld\n",
435              TSXGetAtomName(display,atype),aformat,cSelectionTargets, remain);
436        /*
437         * The TARGETS property should have returned us a list of atoms
438         * corresponding to each selection target format supported.
439         */
440        if( (atype == XA_ATOM || atype == aTargets) && aformat == 32 )
441        {
442           int i;
443           LPWINE_CLIPFORMAT lpFormat;
444
445           /* Cache these formats in the clipboard cache */
446
447           for (i = 0; i < cSelectionTargets; i++)
448           {
449               char *itemFmtName = TSXGetAtomName(display, targetList[i]);
450               UINT wFormat = X11DRV_CLIPBOARD_MapPropertyToFormat(itemFmtName);
451
452               /*
453                * If the clipboard format maps to a Windows format, simply store
454                * the atom identifier and record its availablity status
455                * in the clipboard cache.
456                */
457               if (wFormat)
458               {
459                   lpFormat = CLIPBOARD_LookupFormat( wFormat );
460
461                   /* Don't replace if the property already cached is a native format,
462                    * or if a PIXMAP is being replaced by a BITMAP.
463                    */
464                   if (lpFormat->wDataPresent &&
465                         ( X11DRV_CLIPBOARD_IsNativeProperty(lpFormat->drvData)
466                           || (lpFormat->drvData == XA_PIXMAP && targetList[i] == XA_BITMAP) )
467                      )
468                   {
469                       TRACE("\tAtom# %d: '%s' --> FormatID(%d) %s (Skipped)\n",
470                             i, itemFmtName, wFormat, lpFormat->Name);
471                   }
472                   else
473                   {
474                       lpFormat->wDataPresent = 1;
475                       lpFormat->drvData = targetList[i];
476                       TRACE("\tAtom# %d: '%s' --> FormatID(%d) %s\n",
477                             i, itemFmtName, wFormat, lpFormat->Name);
478                   }
479               }
480
481               TSXFree(itemFmtName);
482           }
483        }
484
485        /* Free the list of targets */
486        TSXFree(targetList);
487     }
488
489     return cSelectionTargets;
490 }
491
492 /**************************************************************************
493  *              X11DRV_CLIPBOARD_ReadSelection
494  *  Reads the contents of the X selection property into the WINE clipboard cache
495  *  converting the selection into a format compatible with the windows clipboard
496  *  if possible.
497  *  This method is invoked only to read the contents of a the selection owned
498  *  by an external application. i.e. when we do not own the X selection.
499  */
500 static BOOL X11DRV_CLIPBOARD_ReadSelection(UINT wFormat, Window w, Atom prop, Atom reqType)
501 {
502     Display *display = thread_display();
503     Atom              atype=AnyPropertyType;
504     int               aformat;
505     unsigned long     total,nitems,remain,itemSize,val_cnt;
506     long              lRequestLength,bwc;
507     unsigned char*    val;
508     unsigned char*    buffer;
509     LPWINE_CLIPFORMAT lpFormat;
510     BOOL              bRet = FALSE;
511     HWND              hWndClipWindow = GetOpenClipboardWindow();
512
513
514     if(prop == None)
515         return bRet;
516
517     TRACE("Reading X selection...\n");
518
519     TRACE("\tretrieving property %s from window %ld into %s\n",
520           TSXGetAtomName(display,reqType), (long)w, TSXGetAtomName(display,prop) );
521
522     /*
523      * First request a zero length in order to figure out the request size.
524      */
525     if(TSXGetWindowProperty(display,w,prop,0,0,False, AnyPropertyType/*reqType*/,
526                             &atype, &aformat, &nitems, &itemSize, &val) != Success)
527     {
528         WARN("\tcouldn't get property size\n");
529         return bRet;
530     }
531
532     /* Free zero length return data if any */
533     if ( val )
534     {
535        TSXFree(val);
536        val = NULL;
537     }
538
539     TRACE("\tretrieving %ld bytes...\n", itemSize * aformat/8);
540     lRequestLength = (itemSize * aformat/8)/4  + 1;
541
542    bwc = aformat/8;
543    /* we want to read the property, but not it too large of chunks or
544       we could hang the cause problems. Lets go for 4k blocks */
545
546     if(TSXGetWindowProperty(display,w,prop,0,4096,False,
547                             AnyPropertyType/*reqType*/,
548                             &atype, &aformat, &nitems, &remain, &buffer)
549         != Success)
550     {
551         WARN("\tcouldn't read property\n");
552         return bRet;
553     }
554    val = (char*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
555                           nitems*bwc);
556    memcpy(val,buffer,nitems*bwc);
557    TSXFree(buffer);
558
559    for (total = nitems*bwc,val_cnt=0; remain;)
560    {
561        val_cnt +=nitems*bwc;
562        TSXGetWindowProperty(display, w, prop,
563                           (total / 4), 4096, False,
564                           AnyPropertyType, &atype,
565                           &aformat, &nitems, &remain,
566                           &buffer);
567
568        total += nitems*bwc;
569        HeapReAlloc(GetProcessHeap(),0,val, total);
570        memcpy(&val[val_cnt], buffer, nitems*(aformat/8));
571        TSXFree(buffer);
572    }
573    nitems = total;
574
575     /*
576      * Translate the X property into the appropriate Windows clipboard
577      * format, if possible.
578      */
579     if ( (reqType == XA_STRING)
580          && (atype == XA_STRING) && (aformat == 8) )
581     /* convert Unix text to CF_UNICODETEXT */
582     {
583       int          i,inlcount = 0;
584       char*      lpstr;
585
586       for(i=0; i <= nitems; i++)
587           if( val[i] == '\n' ) inlcount++;
588
589       if( (lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nitems + inlcount + 1)) )
590       {
591           static UINT text_cp = (UINT)-1;
592           UINT count;
593           HANDLE hUnicodeText;
594
595           for(i=0,inlcount=0; i <= nitems; i++)
596           {
597              if( val[i] == '\n' ) lpstr[inlcount++]='\r';
598              lpstr[inlcount++]=val[i];
599           }
600
601           if(text_cp == (UINT)-1)
602           {
603               HKEY hkey;
604               /* default value */
605               text_cp = CP_ACP;
606               if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", &hkey))
607               {
608                   char buf[20];
609                   DWORD type, count = sizeof(buf);
610                   if(!RegQueryValueExA(hkey, "TextCP", 0, &type, buf, &count))
611                       text_cp = atoi(buf);
612                   RegCloseKey(hkey);
613               }
614           }
615
616           count = MultiByteToWideChar(text_cp, 0, lpstr, -1, NULL, 0);
617           hUnicodeText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, count * sizeof(WCHAR));
618           if(hUnicodeText)
619           {
620               WCHAR *textW = GlobalLock(hUnicodeText);
621               MultiByteToWideChar(text_cp, 0, lpstr, -1, textW, count);
622               GlobalUnlock(hUnicodeText);
623               if (!SetClipboardData(CF_UNICODETEXT, hUnicodeText))
624               {
625             ERR("Not SET! Need to free our own block\n");
626                     GlobalFree(hUnicodeText);
627           }
628               bRet = TRUE;
629           }
630           HeapFree(GetProcessHeap(), 0, lpstr);
631       }
632     }
633     else if ( reqType == XA_PIXMAP || reqType == XA_BITMAP ) /* treat PIXMAP as CF_DIB or CF_BITMAP */
634     {
635       /* Get the first pixmap handle passed to us */
636       Pixmap *pPixmap = (Pixmap *)val;
637       HANDLE hTargetImage = 0;  /* Handle to store the converted bitmap or DIB */
638
639       if (aformat != 32 || nitems < 1 || atype != XA_PIXMAP
640           || (wFormat != CF_BITMAP && wFormat != CF_DIB))
641       {
642           WARN("\tUnimplemented format conversion request\n");
643           goto END;
644       }
645
646       if ( wFormat == CF_BITMAP )
647       {
648         /* For CF_BITMAP requests we must return an HBITMAP */
649         hTargetImage = X11DRV_BITMAP_CreateBitmapFromPixmap(*pPixmap, TRUE);
650       }
651       else if (wFormat == CF_DIB)
652       {
653         HWND hwnd = GetOpenClipboardWindow();
654         HDC hdc = GetDC(hwnd);
655
656         /* For CF_DIB requests we must return an HGLOBAL storing a packed DIB */
657         hTargetImage = X11DRV_DIB_CreateDIBFromPixmap(*pPixmap, hdc, TRUE);
658
659         ReleaseDC(hwnd, hdc);
660       }
661
662       if (!hTargetImage)
663       {
664           WARN("PIXMAP conversion failed!\n" );
665           goto END;
666       }
667
668       /* Delete previous clipboard data */
669       lpFormat = CLIPBOARD_LookupFormat(wFormat);
670       if (lpFormat->wDataPresent && (lpFormat->hData16 || lpFormat->hData32))
671           CLIPBOARD_DeleteRecord(lpFormat, !(hWndClipWindow));
672
673       /* Update the clipboard record */
674       lpFormat->wDataPresent = 1;
675       lpFormat->hData32 = hTargetImage;
676       lpFormat->hData16 = 0;
677
678       bRet = TRUE;
679     }
680
681     /* For native properties simply copy the X data without conversion */
682     else if (X11DRV_CLIPBOARD_IsNativeProperty(reqType)) /* <WCF>* */
683     {
684       HANDLE hClipData = 0;
685       void*  lpClipData;
686       int cBytes = nitems * aformat/8;
687
688       if( cBytes )
689       {
690         /* Turn on the DDESHARE flag to enable shared 32 bit memory */
691         hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cBytes );
692         if( (lpClipData = GlobalLock(hClipData)) )
693         {
694             memcpy(lpClipData, val, cBytes);
695             GlobalUnlock(hClipData);
696         }
697         else
698             hClipData = 0;
699       }
700
701       if( hClipData )
702       {
703           /* delete previous clipboard record if any */
704           lpFormat = CLIPBOARD_LookupFormat(wFormat);
705           if (lpFormat->wDataPresent || lpFormat->hData16 || lpFormat->hData32)
706               CLIPBOARD_DeleteRecord(lpFormat, !(hWndClipWindow));
707
708           /* Update the clipboard record */
709           lpFormat->wDataPresent = 1;
710           lpFormat->hData32 = hClipData;
711           lpFormat->hData16 = 0;
712
713           bRet = TRUE;
714       }
715     }
716     else
717     {
718         WARN("\tUnimplemented format conversion request\n");
719         goto END;
720     }
721
722 END:
723     /* Delete the property on the window now that we are done
724      * This will send a PropertyNotify event to the selection owner. */
725     TSXDeleteProperty(display,w,prop);
726
727     /* Free the retrieved property data */
728     HeapFree(GetProcessHeap(),0,val);
729     return bRet;
730 }
731
732 /**************************************************************************
733  *              X11DRV_CLIPBOARD_ReleaseSelection
734  *
735  * Release an XA_PRIMARY or XA_CLIPBOARD selection that we own, in response
736  * to a SelectionClear event.
737  * This can occur in response to another client grabbing the X selection.
738  * If the XA_CLIPBOARD selection is lost, we relinquish XA_PRIMARY as well.
739  */
740 void X11DRV_CLIPBOARD_ReleaseSelection(Atom selType, Window w, HWND hwnd)
741 {
742     Display *display = thread_display();
743     Atom xaClipboard = TSXInternAtom(display, "CLIPBOARD", False);
744     int clearAllSelections = 0;
745     HKEY hkey;
746
747     if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Clipboard", &hkey))
748     {
749         char buffer[20];
750         DWORD type, count = sizeof(buffer);
751         if(!RegQueryValueExA(hkey, "ClearAllSelections", 0, &type, buffer, &count))
752             clearAllSelections = atoi(buffer);
753         RegCloseKey(hkey);
754     }
755
756     /* w is the window that lost the selection
757      * selectionPrevWindow is nonzero if CheckSelection() was called.
758      */
759
760     TRACE("\tevent->window = %08x (sw = %08x, spw=%08x)\n",
761           (unsigned)w, (unsigned)selectionWindow, (unsigned)selectionPrevWindow );
762
763     if( selectionAcquired )
764     {
765         if( w == selectionWindow || selectionPrevWindow == None)
766         {
767             /* If we're losing the CLIPBOARD selection, or if the preferences in .winerc
768              * dictate that *all* selections should be cleared on loss of a selection,
769              * we must give up all the selections we own.
770              */
771             if ( clearAllSelections || (selType == xaClipboard) )
772             {
773               /* completely give up the selection */
774               TRACE("Lost CLIPBOARD (+PRIMARY) selection\n");
775
776               /* We are completely giving up the selection.
777                * Make sure we can open the windows clipboard first. */
778
779               if ( !OpenClipboard(hwnd) )
780               {
781                   /*
782                    * We can't empty the clipboard if we cant open it so abandon.
783                    * Wine will think that it still owns the selection but this is
784                    * safer than losing the selection without properly emptying
785                    * the clipboard. Perhaps we should forcibly re-assert ownership
786                    * of the CLIPBOARD selection in this case...
787                    */
788                   ERR("\tClipboard is busy. Could not give up selection!\n");
789                   return;
790               }
791
792               /* We really lost CLIPBOARD but want to voluntarily lose PRIMARY */
793               if ( (selType == xaClipboard)
794                    && (selectionAcquired & S_PRIMARY) )
795               {
796                   XSetSelectionOwner(display, XA_PRIMARY, None, CurrentTime);
797               }
798
799               /* We really lost PRIMARY but want to voluntarily lose CLIPBOARD  */
800               if ( (selType == XA_PRIMARY)
801                    && (selectionAcquired & S_CLIPBOARD) )
802               {
803                   XSetSelectionOwner(display, xaClipboard, None, CurrentTime);
804               }
805
806               selectionWindow = None;
807               PrimarySelectionOwner = ClipboardSelectionOwner = 0;
808
809               /* Empty the windows clipboard.
810                * We should pretend that we still own the selection BEFORE calling
811                * EmptyClipboard() since otherwise this has the side effect of
812                * triggering X11DRV_CLIPBOARD_Acquire() and causing the X selection
813                * to be re-acquired by us!
814                */
815               selectionAcquired = (S_PRIMARY | S_CLIPBOARD);
816               EmptyClipboard();
817               CloseClipboard();
818
819               /* Give up ownership of the windows clipboard */
820               CLIPBOARD_ReleaseOwner();
821
822               /* Reset the selection flags now that we are done */
823               selectionAcquired = S_NOSELECTION;
824             }
825             else if ( selType == XA_PRIMARY ) /* Give up only PRIMARY selection */
826             {
827                 TRACE("Lost PRIMARY selection\n");
828                 PrimarySelectionOwner = 0;
829                 selectionAcquired &= ~S_PRIMARY;  /* clear S_PRIMARY mask */
830             }
831
832             cSelectionTargets = 0;
833         }
834         /* but we'll keep existing data for internal use */
835         else if( w == selectionPrevWindow )
836         {
837             Atom xaClipboard = TSXInternAtom(display, _CLIPBOARD, False);
838
839             w = TSXGetSelectionOwner(display, XA_PRIMARY);
840             if( w == None )
841                 TSXSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime);
842
843             w = TSXGetSelectionOwner(display, xaClipboard);
844             if( w == None )
845                 TSXSetSelectionOwner(display, xaClipboard, selectionWindow, CurrentTime);
846         }
847     }
848
849     /* Signal to a selectionClearEvent listener if the selection is completely lost */
850     if (selectionClearEvent && !selectionAcquired)
851     {
852         TRACE("Lost all selections, signalling to selectionClearEvent listener\n");
853         SetEvent(selectionClearEvent);
854     }
855
856     selectionPrevWindow = None;
857 }
858
859 /**************************************************************************
860  *              ReleaseClipboard (X11DRV.@)
861  *  Voluntarily release all currently owned X selections
862  */
863 void X11DRV_ReleaseClipboard(void)
864 {
865     Display *display = thread_display();
866     if( selectionAcquired )
867     {
868         XEvent xe;
869         Window savePrevWindow = selectionWindow;
870         Atom xaClipboard = TSXInternAtom(display, _CLIPBOARD, False);
871         BOOL bHasPrimarySelection = selectionAcquired & S_PRIMARY;
872
873         selectionAcquired   = S_NOSELECTION;
874         selectionPrevWindow = selectionWindow;
875         selectionWindow     = None;
876
877         TRACE("\tgiving up selection (spw = %08x)\n",
878              (unsigned)selectionPrevWindow);
879
880         wine_tsx11_lock();
881
882         TRACE("Releasing CLIPBOARD selection\n");
883         XSetSelectionOwner(display, xaClipboard, None, CurrentTime);
884         if( selectionPrevWindow )
885             while( !XCheckTypedWindowEvent( display, selectionPrevWindow,
886                                             SelectionClear, &xe ) );
887
888         if ( bHasPrimarySelection )
889         {
890             TRACE("Releasing XA_PRIMARY selection\n");
891             selectionPrevWindow = savePrevWindow; /* May be cleared in X11DRV_CLIPBOARD_ReleaseSelection */
892             XSetSelectionOwner(display, XA_PRIMARY, None, CurrentTime);
893
894             if( selectionPrevWindow )
895                 while( !XCheckTypedWindowEvent( display, selectionPrevWindow,
896                                                 SelectionClear, &xe ) );
897         }
898         wine_tsx11_unlock();
899     }
900
901     /* Get rid of any Pixmap resources we may still have */
902     while (prop_head)
903     {
904         PROPERTY *prop = prop_head;
905         prop_head = prop->next;
906         XFreePixmap( gdi_display, prop->pixmap );
907         HeapFree( GetProcessHeap(), 0, prop );
908     }
909 }
910
911 /**************************************************************************
912  *              AcquireClipboard (X11DRV.@)
913  */
914 void X11DRV_AcquireClipboard(void)
915 {
916     Display *display = thread_display();
917     Window       owner;
918     HWND         hWndClipWindow = GetOpenClipboardWindow();
919
920     /*
921      * Acquire X selection if we don't already own it.
922      * Note that we only acquire the selection if it hasn't been already
923      * acquired by us, and ignore the fact that another X window may be
924      * asserting ownership. The reason for this is we need *any* top level
925      * X window to hold selection ownership. The actual clipboard data requests
926      * are made via GetClipboardData from EVENT_SelectionRequest and this
927      * ensures that the real HWND owner services the request.
928      * If the owning X window gets destroyed the selection ownership is
929      * re-cycled to another top level X window in X11DRV_CLIPBOARD_ResetOwner.
930      *
931      */
932
933     if ( !(selectionAcquired == (S_PRIMARY | S_CLIPBOARD)) )
934     {
935         Atom xaClipboard = TSXInternAtom(display, _CLIPBOARD, False);
936         owner = X11DRV_get_whole_window( GetAncestor( hWndClipWindow ? hWndClipWindow : AnyPopup(),
937                                                       GA_ROOT ) );
938
939         /* Grab PRIMARY selection if not owned */
940         if ( !(selectionAcquired & S_PRIMARY) )
941             TSXSetSelectionOwner(display, XA_PRIMARY, owner, CurrentTime);
942
943         /* Grab CLIPBOARD selection if not owned */
944         if ( !(selectionAcquired & S_CLIPBOARD) )
945             TSXSetSelectionOwner(display, xaClipboard, owner, CurrentTime);
946
947         if( TSXGetSelectionOwner(display,XA_PRIMARY) == owner )
948             selectionAcquired |= S_PRIMARY;
949
950         if( TSXGetSelectionOwner(display,xaClipboard) == owner)
951             selectionAcquired |= S_CLIPBOARD;
952
953         if (selectionAcquired)
954         {
955             selectionWindow = owner;
956             TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner);
957         }
958     }
959 }
960
961 /**************************************************************************
962  *              IsClipboardFormatAvailable (X11DRV.@)
963  *
964  * Checks if the specified format is available in the current selection
965  * Only invoked when WINE is not the selection owner
966  */
967 BOOL X11DRV_IsClipboardFormatAvailable(UINT wFormat)
968 {
969     Display *display = thread_display();
970     Atom xaClipboard = TSXInternAtom(display, _CLIPBOARD, False);
971     Window ownerPrimary = TSXGetSelectionOwner(display,XA_PRIMARY);
972     Window ownerClipboard = TSXGetSelectionOwner(display,xaClipboard);
973
974     TRACE("enter for %d\n", wFormat);
975
976     /*
977      * If the selection has not been previously cached, or the selection has changed,
978      * try and cache the list of available selection targets from the current selection.
979      */
980     if ( !cSelectionTargets || (PrimarySelectionOwner != ownerPrimary)
981                          || (ClipboardSelectionOwner != ownerClipboard) )
982     {
983         /*
984          * First try caching the CLIPBOARD selection.
985          * If unavailable try PRIMARY.
986          */
987         if ( X11DRV_CLIPBOARD_CacheDataFormats(xaClipboard) == 0 )
988         {
989             X11DRV_CLIPBOARD_CacheDataFormats(XA_PRIMARY);
990         }
991
992         ClipboardSelectionOwner = ownerClipboard;
993         PrimarySelectionOwner = ownerPrimary;
994     }
995
996     /* Exit if there is no selection */
997     if ( !ownerClipboard && !ownerPrimary )
998     {
999         TRACE("There is no selection owner\n");
1000         return FALSE;
1001     }
1002
1003     /* Check if the format is available in the clipboard cache */
1004     if ( CLIPBOARD_IsPresent(wFormat) )
1005         return TRUE;
1006
1007     /*
1008      * Many X client apps (such as XTerminal) don't support being queried
1009      * for the "TARGETS" target atom. To handle such clients we must actually
1010      * try to convert the selection to the requested type.
1011      */
1012     if ( !cSelectionTargets )
1013         return X11DRV_GetClipboardData( wFormat );
1014
1015     TRACE("There is no selection\n");
1016     return FALSE;
1017 }
1018
1019 /**************************************************************************
1020  *              RegisterClipboardFormat (X11DRV.@)
1021  *
1022  * Registers a custom X clipboard format
1023  * Returns: TRUE - success,  FALSE - failure
1024  */
1025 BOOL X11DRV_RegisterClipboardFormat( LPCSTR FormatName )
1026 {
1027     Display *display = thread_display();
1028     Atom prop = None;
1029     char str[256];
1030
1031     /*
1032      * If an X atom is registered for this format, return that
1033      * Otherwise register a new atom.
1034      */
1035     if (FormatName)
1036     {
1037         /* Add a WINE specific prefix to the format */
1038         strcpy(str, FMT_PREFIX);
1039         strncat(str, FormatName, sizeof(str) - strlen(FMT_PREFIX));
1040         prop = TSXInternAtom(display, str, False);
1041     }
1042
1043     return (prop) ? TRUE : FALSE;
1044 }
1045
1046 /**************************************************************************
1047  *              IsSelectionOwner (X11DRV.@)
1048  *
1049  * Returns: TRUE - We(WINE) own the selection, FALSE - Selection not owned by us
1050  */
1051 BOOL X11DRV_IsSelectionOwner(void)
1052 {
1053     return selectionAcquired;
1054 }
1055
1056 /**************************************************************************
1057  *              SetClipboardData (X11DRV.@)
1058  *
1059  * We don't need to do anything special here since the clipboard code
1060  * maintains the cache.
1061  *
1062  */
1063 void X11DRV_SetClipboardData(UINT wFormat)
1064 {
1065     /* Make sure we have acquired the X selection */
1066     X11DRV_AcquireClipboard();
1067 }
1068
1069 /**************************************************************************
1070  *              GetClipboardData (X11DRV.@)
1071  *
1072  * This method is invoked only when we DO NOT own the X selection
1073  *
1074  * NOTE: Clipboard driver get requests only for CF_UNICODETEXT data.
1075  * We always get the data from the selection client each time,
1076  * since we have no way of determining if the data in our cache is stale.
1077  */
1078 BOOL X11DRV_GetClipboardData(UINT wFormat)
1079 {
1080     Display *display = thread_display();
1081     BOOL bRet = selectionAcquired;
1082     HWND hWndClipWindow = GetOpenClipboardWindow();
1083     HWND hWnd = (hWndClipWindow) ? hWndClipWindow : GetActiveWindow();
1084     LPWINE_CLIPFORMAT lpFormat;
1085
1086     TRACE("%d\n", wFormat);
1087
1088     if (!selectionAcquired)
1089     {
1090         XEvent xe;
1091         Atom propRequest;
1092         Window w = X11DRV_get_whole_window( GetAncestor( hWnd, GA_ROOT ));
1093         if(!w)
1094         {
1095             FIXME("No parent win found %x %x\n", hWnd, hWndClipWindow);
1096             return FALSE;
1097         }
1098
1099         /* Map the format ID requested to an X selection property.
1100          * If the format is in the cache, use the atom associated
1101          * with it.
1102          */
1103
1104         lpFormat = CLIPBOARD_LookupFormat( wFormat );
1105         if (lpFormat && lpFormat->wDataPresent && lpFormat->drvData)
1106             propRequest = (Atom)lpFormat->drvData;
1107         else
1108             propRequest = X11DRV_CLIPBOARD_MapFormatToProperty(wFormat);
1109
1110         if (propRequest)
1111         {
1112             TRACE("Requesting %s selection from %s...\n",
1113                   TSXGetAtomName(display, propRequest),
1114                   TSXGetAtomName(display, selectionCacheSrc) );
1115             wine_tsx11_lock();
1116             XConvertSelection(display, selectionCacheSrc, propRequest,
1117                             TSXInternAtom(display, "SELECTION_DATA", False),
1118                             w, CurrentTime);
1119
1120             /* wait until SelectionNotify is received */
1121
1122             while( TRUE )
1123             {
1124                if( XCheckTypedWindowEvent(display, w, SelectionNotify, &xe) )
1125                    if( xe.xselection.selection == selectionCacheSrc )
1126                        break;
1127             }
1128             wine_tsx11_unlock();
1129
1130             /*
1131              *  Read the contents of the X selection property into WINE's
1132              *  clipboard cache converting the selection to be compatible if possible.
1133              */
1134             bRet = X11DRV_CLIPBOARD_ReadSelection( wFormat,
1135                                                    xe.xselection.requestor,
1136                                                    xe.xselection.property,
1137                                                    xe.xselection.target);
1138         }
1139         else
1140             bRet = FALSE;
1141
1142         TRACE("\tpresent %s = %i\n", CLIPBOARD_GetFormatName(wFormat), bRet );
1143     }
1144
1145     TRACE("Returning %d\n", bRet);
1146
1147     return bRet;
1148 }
1149
1150 /**************************************************************************
1151  *              ResetSelectionOwner (X11DRV.@)
1152  *
1153  * Called from DestroyWindow() to prevent X selection from being lost when
1154  * a top level window is destroyed, by switching ownership to another top
1155  * level window.
1156  * Any top level window can own the selection. See X11DRV_CLIPBOARD_Acquire
1157  * for a more detailed description of this.
1158  */
1159 void X11DRV_ResetSelectionOwner(HWND hwnd, BOOL bFooBar)
1160 {
1161     Display *display = thread_display();
1162     HWND hWndClipOwner = 0;
1163     HWND tmp;
1164     Window XWnd = X11DRV_get_whole_window(hwnd);
1165     Atom xaClipboard;
1166     BOOL bLostSelection = FALSE;
1167
1168     /* There is nothing to do if we don't own the selection,
1169      * or if the X window which currently owns the selecion is different
1170      * from the one passed in.
1171      */
1172     if ( !selectionAcquired || XWnd != selectionWindow
1173          || selectionWindow == None )
1174        return;
1175
1176     if ( (bFooBar && XWnd) || (!bFooBar && !XWnd) )
1177        return;
1178
1179     hWndClipOwner = GetClipboardOwner();
1180     xaClipboard = TSXInternAtom(display, _CLIPBOARD, False);
1181
1182     TRACE("clipboard owner = %04x, selection window = %08x\n",
1183           hWndClipOwner, (unsigned)selectionWindow);
1184
1185     /* now try to salvage current selection from being destroyed by X */
1186
1187     TRACE("\tchecking %08x\n", (unsigned) XWnd);
1188
1189     selectionPrevWindow = selectionWindow;
1190     selectionWindow = None;
1191
1192     if (!(tmp = GetWindow( hwnd, GW_HWNDNEXT ))) tmp = GetWindow( hwnd, GW_HWNDFIRST );
1193     if (tmp && tmp != hwnd) selectionWindow = X11DRV_get_whole_window(tmp);
1194
1195     if( selectionWindow != None )
1196     {
1197         /* We must pretend that we don't own the selection while making the switch
1198          * since a SelectionClear event will be sent to the last owner.
1199          * If there is no owner X11DRV_CLIPBOARD_ReleaseSelection will do nothing.
1200          */
1201         int saveSelectionState = selectionAcquired;
1202         selectionAcquired = False;
1203
1204         TRACE("\tswitching selection from %08x to %08x\n",
1205                     (unsigned)selectionPrevWindow, (unsigned)selectionWindow);
1206
1207         /* Assume ownership for the PRIMARY and CLIPBOARD selection */
1208         if ( saveSelectionState & S_PRIMARY )
1209             TSXSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime);
1210
1211         TSXSetSelectionOwner(display, xaClipboard, selectionWindow, CurrentTime);
1212
1213         /* Restore the selection masks */
1214         selectionAcquired = saveSelectionState;
1215
1216         /* Lose the selection if something went wrong */
1217         if ( ( (saveSelectionState & S_PRIMARY) &&
1218                (TSXGetSelectionOwner(display, XA_PRIMARY) != selectionWindow) )
1219              || (TSXGetSelectionOwner(display, xaClipboard) != selectionWindow) )
1220         {
1221             bLostSelection = TRUE;
1222             goto END;
1223         }
1224         else
1225         {
1226             /* Update selection state */
1227             if (saveSelectionState & S_PRIMARY)
1228                PrimarySelectionOwner = selectionWindow;
1229
1230             ClipboardSelectionOwner = selectionWindow;
1231         }
1232     }
1233     else
1234     {
1235         bLostSelection = TRUE;
1236         goto END;
1237     }
1238
1239 END:
1240     if (bLostSelection)
1241     {
1242       /* Launch the clipboard server if the selection can no longer be recyled
1243        * to another top level window. */
1244
1245       if ( !X11DRV_CLIPBOARD_LaunchServer() )
1246       {
1247          /* Empty the windows clipboard if the server was not launched.
1248           * We should pretend that we still own the selection BEFORE calling
1249           * EmptyClipboard() since otherwise this has the side effect of
1250           * triggering X11DRV_CLIPBOARD_Acquire() and causing the X selection
1251           * to be re-acquired by us!
1252           */
1253
1254          TRACE("\tLost the selection! Emptying the clipboard...\n");
1255
1256          OpenClipboard( 0 );
1257          selectionAcquired = (S_PRIMARY | S_CLIPBOARD);
1258          EmptyClipboard();
1259
1260          CloseClipboard();
1261
1262          /* Give up ownership of the windows clipboard */
1263          CLIPBOARD_ReleaseOwner();
1264       }
1265
1266       selectionAcquired = S_NOSELECTION;
1267       ClipboardSelectionOwner = PrimarySelectionOwner = 0;
1268       selectionWindow = 0;
1269     }
1270 }
1271
1272 /**************************************************************************
1273  *              X11DRV_CLIPBOARD_RegisterPixmapResource
1274  * Registers a Pixmap resource which is to be associated with a property Atom.
1275  * When the property is destroyed we also destroy the Pixmap through the
1276  * PropertyNotify event.
1277  */
1278 BOOL X11DRV_CLIPBOARD_RegisterPixmapResource( Atom property, Pixmap pixmap )
1279 {
1280     PROPERTY *prop = HeapAlloc( GetProcessHeap(), 0, sizeof(*prop) );
1281     if (!prop) return FALSE;
1282     prop->atom = property;
1283     prop->pixmap = pixmap;
1284     prop->next = prop_head;
1285     prop_head = prop;
1286     return TRUE;
1287 }
1288
1289 /**************************************************************************
1290  *              X11DRV_CLIPBOARD_FreeResources
1291  *
1292  * Called from EVENT_PropertyNotify() to give us a chance to destroy
1293  * any resources associated with this property.
1294  */
1295 void X11DRV_CLIPBOARD_FreeResources( Atom property )
1296 {
1297     /* Do a simple linear search to see if we have a Pixmap resource
1298      * associated with this property and release it.
1299      */
1300     PROPERTY **prop = &prop_head;
1301
1302     while (*prop)
1303     {
1304         if ((*prop)->atom == property)
1305         {
1306             PROPERTY *next = (*prop)->next;
1307             XFreePixmap( gdi_display, (*prop)->pixmap );
1308             HeapFree( GetProcessHeap(), 0, *prop );
1309             *prop = next;
1310         }
1311         else prop = &(*prop)->next;
1312     }
1313 }