Avoid direct access to the palette structure.
[wine] / dlls / x11drv / xdnd.c
1 /*
2  * XDND handler code
3  *
4  * Copyright 2003 Ulrich Czekalla
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <string.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <stdarg.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "winerror.h"
35 #include "wownt32.h"
36
37 #include "x11drv.h"
38 #include "shlobj.h"  /* DROPFILES */
39
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(xdnd);
43
44 /* Maximum wait time for selection notify */
45 #define SELECTION_RETRIES 500  /* wait for .1 seconds */
46 #define SELECTION_WAIT    1000 /* us */
47
48 typedef struct tagXDNDDATA
49 {
50     int cf_win;
51     Atom cf_xdnd;
52     void *data;
53     unsigned int size;
54     struct tagXDNDDATA *next;
55 } XDNDDATA, *LPXDNDDATA;
56
57 static LPXDNDDATA XDNDData = NULL;
58 static POINT XDNDxy = { 0, 0 };
59
60 static void X11DRV_XDND_InsertXDNDData(int property, int format, void* data, unsigned int len);
61 static int X11DRV_XDND_DeconstructTextPlain(int property, void* data, int len);
62 static int X11DRV_XDND_DeconstructTextHTML(int property, void* data, int len);
63 static int X11DRV_XDND_MapFormat(unsigned int property, unsigned char *data, int len);
64 static void X11DRV_XDND_ResolveProperty(Display *display, Window xwin, Time tm,
65     Atom *types, unsigned long *count);
66 static void X11DRV_XDND_SendDropFiles(HWND hwnd);
67 static void X11DRV_XDND_FreeDragDropOp(void);
68 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest, char* lpsrc, int len);
69 static DROPFILES* X11DRV_XDND_BuildDropFiles(char* filename, unsigned int len, POINT pt);
70
71 static CRITICAL_SECTION xdnd_cs;
72 static CRITICAL_SECTION_DEBUG critsect_debug =
73 {
74     0, 0, &xdnd_cs,
75     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
76       0, 0, { 0, (DWORD)(__FILE__ ": xdnd_cs") }
77 };
78 static CRITICAL_SECTION xdnd_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
79
80 /**************************************************************************
81  * X11DRV_XDND_Event
82  *
83  * Entry point for X11 XDND events. Returns FALSE if event is not handled.
84  */
85 int X11DRV_XDND_Event(HWND hWnd, XClientMessageEvent *event)
86 {
87     int isXDNDMsg = 1;
88
89     TRACE("0x%p\n", hWnd);
90
91     if (event->message_type == x11drv_atom(XdndEnter))
92     {
93         Atom *xdndtypes;
94         unsigned long count = 0;
95
96         TRACE("XDNDEnter: ver(%ld) check-XdndTypeList(%ld) data=%ld,%ld,%ld,%ld,%ld\n",
97             (event->data.l[1] & 0xFF000000) >> 24, (event->data.l[1] & 1),
98              event->data.l[0], event->data.l[1], event->data.l[2],
99              event->data.l[3], event->data.l[4]);
100
101         /* If the source supports more than 3 data types we retrieve
102          * the entire list. */
103         if (event->data.l[1] & 1)
104         {
105             Atom acttype;
106             int actfmt;
107             unsigned long bytesret;
108
109             /* Request supported formats from source window */
110             wine_tsx11_lock();
111             XGetWindowProperty(event->display, event->data.l[0], x11drv_atom(XdndTypeList),
112                                0, 65535, FALSE, AnyPropertyType, &acttype, &actfmt, &count,
113                                &bytesret, (unsigned char**)&xdndtypes);
114             wine_tsx11_unlock();
115         }
116         else
117         {
118             count = 3;
119             xdndtypes = &event->data.l[2];
120         }
121
122         if (TRACE_ON(xdnd))
123         {
124             unsigned int i = 0;
125
126             wine_tsx11_lock();
127             for (; i < count; i++)
128             {
129                 if (xdndtypes[i] != 0)
130                 {
131                     char * pn = XGetAtomName(event->display, xdndtypes[i]);
132                     TRACE("XDNDEnterAtom %ld: %s\n", xdndtypes[i], pn);
133                     XFree(pn);
134                 }
135             }
136             wine_tsx11_unlock();
137         }
138
139         /* Do a one-time data read and cache results */
140         X11DRV_XDND_ResolveProperty(event->display, event->window,
141             event->data.l[1], xdndtypes, &count);
142
143         if (event->data.l[1] & 1)
144             XFree(xdndtypes);
145     }
146     else if (event->message_type == x11drv_atom(XdndPosition))
147     {
148         XClientMessageEvent e;
149         int accept = 0; /* Assume we're not accepting */
150
151         XDNDxy.x = event->data.l[2] >> 16;
152         XDNDxy.y = event->data.l[2] & 0xFFFF;
153
154         /* FIXME: Notify OLE of DragEnter. Result determines if we accept */
155
156         if (GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES)
157             accept = 1;
158
159         TRACE("XDNDPosition. action req: %ld accept(%d) at x(%ld),y(%ld)\n",
160                  event->data.l[4], accept, XDNDxy.x, XDNDxy.y);
161
162         /*
163          * Let source know if we're accepting the drop by
164          * sending a status message.
165          */
166         e.type = ClientMessage;
167         e.display = event->display;
168         e.window = event->data.l[0];
169         e.message_type = x11drv_atom(XdndStatus);
170         e.format = 32;
171         e.data.l[0] = event->window;
172         e.data.l[1] = accept;
173         e.data.l[2] = 0; /* Empty Rect */
174         e.data.l[3] = 0; /* Empty Rect */
175         if (accept)
176             e.data.l[4] = event->data.l[4];
177         else
178             e.data.l[4] = None;
179         wine_tsx11_lock();
180         XSendEvent(event->display, event->data.l[0], False, NoEventMask, (XEvent*)&e);
181         wine_tsx11_unlock();
182
183         /* FIXME: if drag accepted notify OLE of DragOver */
184     }
185     else if (event->message_type == x11drv_atom(XdndDrop))
186     {
187         XClientMessageEvent e;
188
189         TRACE("XDNDDrop\n");
190
191         /* If we have a HDROP type we send a WM_ACCEPTFILES.*/
192         if (GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES)
193             X11DRV_XDND_SendDropFiles( hWnd );
194
195         /* FIXME: Notify OLE of Drop */
196         X11DRV_XDND_FreeDragDropOp();
197
198         /* Tell the target we are finished. */
199         memset(&e, 0, sizeof(e));
200         e.type = ClientMessage;
201         e.display = event->display;
202         e.window = event->data.l[0];
203         e.message_type = x11drv_atom(XdndFinished);
204         e.format = 32;
205         e.data.l[0] = event->window;
206         wine_tsx11_lock();
207         XSendEvent(event->display, event->data.l[0], False, NoEventMask, (XEvent*)&e);
208         wine_tsx11_unlock();
209     }
210     else if (event->message_type == x11drv_atom(XdndLeave))
211     {
212         TRACE("DND Operation canceled\n");
213
214         X11DRV_XDND_FreeDragDropOp();
215
216         /* FIXME: Notify OLE of DragLeave */
217     }
218     else /* Not an XDND message */
219         isXDNDMsg = 0;
220
221     return isXDNDMsg;
222 }
223
224
225 /**************************************************************************
226  * X11DRV_XDND_ResolveProperty
227  *
228  * Resolve all MIME types to windows clipboard formats. All data is cached.
229  */
230 static void X11DRV_XDND_ResolveProperty(Display *display, Window xwin, Time tm,
231                                         Atom *types, unsigned long *count)
232 {
233     unsigned int i, j;
234     BOOL res;
235     XEvent xe;
236     Atom acttype;
237     int actfmt;
238     unsigned long bytesret, icount;
239     int entries = 0;
240     unsigned char* data = NULL;
241
242     TRACE("count(%ld)\n", *count);
243
244     X11DRV_XDND_FreeDragDropOp(); /* Clear previously cached data */
245
246     for (i = 0; i < *count; i++)
247     {
248         TRACE("requesting atom %ld from xwin %ld\n", types[i], xwin);
249
250         if (types[i] == 0)
251             continue;
252
253         wine_tsx11_lock();
254         XConvertSelection(display, x11drv_atom(XdndSelection), types[i],
255                           x11drv_atom(XdndTarget), xwin, /*tm*/CurrentTime);
256         wine_tsx11_unlock();
257
258         /*
259          * Wait for SelectionNotify
260          */
261         for (j = 0; j < SELECTION_RETRIES; j++)
262         {
263             wine_tsx11_lock();
264             res = XCheckTypedWindowEvent(display, xwin, SelectionNotify, &xe);
265             wine_tsx11_unlock();
266             if (res && xe.xselection.selection == x11drv_atom(XdndSelection)) break;
267
268             usleep(SELECTION_WAIT);
269         }
270
271         if (xe.xselection.property == None)
272             continue;
273
274         wine_tsx11_lock();
275         XGetWindowProperty(display, xwin, x11drv_atom(XdndTarget), 0, 65535, FALSE,
276             AnyPropertyType, &acttype, &actfmt, &icount, &bytesret, &data);
277         wine_tsx11_unlock();
278
279         entries += X11DRV_XDND_MapFormat(types[i], data, icount * (actfmt / 8));
280         wine_tsx11_lock();
281         XFree(data);
282         wine_tsx11_unlock();
283     }
284
285     *count = entries;
286 }
287
288
289 /**************************************************************************
290  * X11DRV_XDND_InsertXDNDData
291  *
292  * Cache available XDND property
293  */
294 static void X11DRV_XDND_InsertXDNDData(int property, int format, void* data, unsigned int len)
295 {
296     LPXDNDDATA current = (LPXDNDDATA) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XDNDDATA));
297
298     if (current)
299     {
300         EnterCriticalSection(&xdnd_cs);
301         current->next = XDNDData;
302         current->cf_xdnd = property;
303         current->cf_win = format;
304         current->data = data;
305         current->size = len;
306         XDNDData = current;
307         LeaveCriticalSection(&xdnd_cs);
308     }
309 }
310
311
312 /**************************************************************************
313  * X11DRV_XDND_MapFormat
314  *
315  * Map XDND MIME format to windows clipboard format.
316  */
317 static int X11DRV_XDND_MapFormat(unsigned int property, unsigned char *data, int len)
318 {
319     void* xdata;
320     int count = 0;
321
322     TRACE("%d: %s\n", property, data);
323
324     /* Always include the raw type */
325     xdata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
326     memcpy(xdata, data, len);
327     X11DRV_XDND_InsertXDNDData(property, property, xdata, len);
328     count++;
329
330     if (property == x11drv_atom(text_plain))
331         count += X11DRV_XDND_DeconstructTextPlain(property, data, len);
332     else if (property == x11drv_atom(text_html))
333         count += X11DRV_XDND_DeconstructTextHTML(property, data, len);
334
335     return count;
336 }
337
338
339 /**************************************************************************
340  * X11DRV_XDND_DeconstructTextPlain
341  *
342  * Interpret text/plain Data and add records to <dndfmt> linked list
343  */
344 static int X11DRV_XDND_DeconstructTextPlain(int property, void* data, int len)
345 {
346     char *p = (char*) data;
347     char* dostext;
348     int count = 0;
349
350     /* Always suppply plain text */
351     X11DRV_XDND_UnixToDos(&dostext, (char*)data, len);
352     X11DRV_XDND_InsertXDNDData(property, CF_TEXT, dostext, strlen(dostext));
353     count++;
354
355     TRACE("CF_TEXT (%d): %s\n", CF_TEXT, dostext);
356
357     /* Check for additional mappings */
358     while (*p != '\0' && *p != ':') /* Advance to end of protocol */
359         p++;
360
361     if (*p == ':')
362     {
363         if (!strncasecmp(data, "http", 4))
364         {
365             X11DRV_XDND_InsertXDNDData(property, RegisterClipboardFormatA("UniformResourceLocator"),
366                 strdup(dostext), strlen(dostext));
367                 count++;
368
369             TRACE("UniformResourceLocator: %s\n", dostext);
370         }
371         else if (!strncasecmp(data, "file", 4))
372         {
373             DROPFILES* pdf;
374
375             pdf = X11DRV_XDND_BuildDropFiles(p+1, len - 5, XDNDxy);
376             if (pdf)
377             {
378                 unsigned int size = HeapSize(GetProcessHeap(), 0, pdf);
379
380                 X11DRV_XDND_InsertXDNDData(property, CF_HDROP, pdf, size);
381                 count++;
382             }
383
384             TRACE("CF_HDROP: %p\n", pdf);
385         }
386     }
387
388     return count;
389 }
390
391
392 /**************************************************************************
393  * X11DRV_XDND_DeconstructTextHTML
394  *
395  * Interpret text/html data and add records to <dndfmt> linked list
396  */
397 static int X11DRV_XDND_DeconstructTextHTML(int property, void* data, int len)
398 {
399     char* dostext;
400
401     X11DRV_XDND_UnixToDos(&dostext, data, len);
402
403     X11DRV_XDND_InsertXDNDData(property,
404         RegisterClipboardFormatA("UniformResourceLocator"), dostext, strlen(dostext));
405
406     TRACE("UniformResourceLocator: %s\n", dostext);
407
408     return 1;
409 }
410
411
412 /**************************************************************************
413  * X11DRV_XDND_SendDropFiles
414  */
415 static void X11DRV_XDND_SendDropFiles(HWND hwnd)
416 {
417     LPXDNDDATA current;
418
419     EnterCriticalSection(&xdnd_cs);
420
421     current = XDNDData;
422
423     /* Find CF_HDROP type if any */
424     while (current != NULL)
425     {
426         if (current->cf_win == CF_HDROP)
427             break;
428         current = current->next;
429     }
430
431     if (current != NULL)
432     {
433         DROPFILES *lpDrop = (DROPFILES*) current->data;
434
435         if (lpDrop)
436         {
437             lpDrop->pt.x = XDNDxy.x;
438             lpDrop->pt.y = XDNDxy.y;
439
440             TRACE("Sending WM_DROPFILES: hWnd(0x%p) %p(%s)\n", hwnd,
441                 ((char*)lpDrop) + lpDrop->pFiles, ((char*)lpDrop) + lpDrop->pFiles);
442
443             PostMessageA(hwnd, WM_DROPFILES, (WPARAM)lpDrop, 0L);
444         }
445     }
446
447     LeaveCriticalSection(&xdnd_cs);
448 }
449
450
451 /**************************************************************************
452  * X11DRV_XDND_FreeDragDropOp
453  */
454 static void X11DRV_XDND_FreeDragDropOp(void)
455 {
456     LPXDNDDATA next;
457     LPXDNDDATA current;
458
459     TRACE("\n");
460
461     EnterCriticalSection(&xdnd_cs);
462
463     current = XDNDData;
464
465     /** Free data cache */
466     while (current != NULL)
467     {
468         next = current->next;
469         HeapFree(GetProcessHeap(), 0, current);
470         current = next;
471     }
472
473     XDNDData = NULL;
474     XDNDxy.x = XDNDxy.y = 0;
475
476     LeaveCriticalSection(&xdnd_cs);
477 }
478
479
480
481 /**************************************************************************
482  * X11DRV_XDND_BuildDropFiles
483  */
484 static DROPFILES* X11DRV_XDND_BuildDropFiles(char* filename, unsigned int len, POINT pt)
485 {
486     char* pfn;
487     int pathlen;
488     char path[MAX_PATH];
489     DROPFILES *lpDrop = NULL;
490
491     /* Advance to last starting slash */
492     pfn = filename + 1;
493     while (*pfn && (*pfn == '\\' || *pfn =='/'))
494     {
495         pfn++;
496         filename++;
497     }
498
499     /* Remove any trailing \r or \n */
500     while (*pfn)
501     {
502         if (*pfn == '\r' || *pfn == '\n')
503         {
504             *pfn = 0;
505             break;
506         }
507         pfn++;
508     }
509
510     TRACE("%s\n", filename);
511
512     pathlen = GetLongPathNameA(filename, path, MAX_PATH);
513     if (pathlen)
514     {
515         lpDrop = (DROPFILES*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
516             sizeof(DROPFILES) + pathlen + 1);
517
518         lpDrop->pFiles = sizeof(DROPFILES);
519         lpDrop->pt.x = pt.x;
520         lpDrop->pt.y = pt.y;
521         lpDrop->fNC = 0;
522         lpDrop->fWide = FALSE;
523
524         strcpy(((char*)lpDrop)+lpDrop->pFiles, path);
525     }
526
527     TRACE("resolved %s\n", lpDrop ? filename : NULL);
528
529     return lpDrop;
530 }
531
532
533 /**************************************************************************
534  * X11DRV_XDND_UnixToDos
535  */
536 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest, char* lpsrc, int len)
537 {
538     int i;
539     unsigned int destlen, lines;
540
541     for (i = 0, lines = 0; i <= len; i++)
542     {
543         if (lpsrc[i] == '\n')
544             lines++;
545     }
546
547     destlen = len + lines + 1;
548
549     if (lpdest)
550     {
551         char* lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, destlen);
552         for (i = 0, lines = 0; i <= len; i++)
553         {
554             if (lpsrc[i] == '\n')
555                 lpstr[++lines + i] = '\r';
556             lpstr[lines + i] = lpsrc[i];
557         }
558
559         *lpdest = lpstr;
560     }
561
562     return lines;
563 }