Convert MsiEnumComponentQualifiers to use msi_strcpy_to_awstring.
[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, { (DWORD_PTR)(__FILE__ ": xdnd_cs") }
77 };
78 static CRITICAL_SECTION xdnd_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
79
80
81 /**************************************************************************
82  * X11DRV_XDND_EnterEvent
83  *
84  * Handle an XdndEnter event.
85  */
86 void X11DRV_XDND_EnterEvent( HWND hWnd, XClientMessageEvent *event )
87 {
88     Atom *xdndtypes;
89     unsigned long count = 0;
90
91     TRACE("ver(%ld) check-XdndTypeList(%ld) data=%ld,%ld,%ld,%ld,%ld\n",
92           (event->data.l[1] & 0xFF000000) >> 24, (event->data.l[1] & 1),
93           event->data.l[0], event->data.l[1], event->data.l[2],
94           event->data.l[3], event->data.l[4]);
95
96     /* If the source supports more than 3 data types we retrieve
97      * the entire list. */
98     if (event->data.l[1] & 1)
99     {
100         Atom acttype;
101         int actfmt;
102         unsigned long bytesret;
103
104         /* Request supported formats from source window */
105         wine_tsx11_lock();
106         XGetWindowProperty(event->display, event->data.l[0], x11drv_atom(XdndTypeList),
107                            0, 65535, FALSE, AnyPropertyType, &acttype, &actfmt, &count,
108                            &bytesret, (unsigned char**)&xdndtypes);
109         wine_tsx11_unlock();
110     }
111     else
112     {
113         count = 3;
114         xdndtypes = (Atom*) &event->data.l[2];
115     }
116
117     if (TRACE_ON(xdnd))
118     {
119         unsigned int i = 0;
120
121         wine_tsx11_lock();
122         for (; i < count; i++)
123         {
124             if (xdndtypes[i] != 0)
125             {
126                 char * pn = XGetAtomName(event->display, xdndtypes[i]);
127                 TRACE("XDNDEnterAtom %ld: %s\n", xdndtypes[i], pn);
128                 XFree(pn);
129             }
130         }
131         wine_tsx11_unlock();
132     }
133
134     /* Do a one-time data read and cache results */
135     X11DRV_XDND_ResolveProperty(event->display, event->window,
136                                 event->data.l[1], xdndtypes, &count);
137
138     if (event->data.l[1] & 1)
139         XFree(xdndtypes);
140 }
141
142 /**************************************************************************
143  * X11DRV_XDND_PositionEvent
144  *
145  * Handle an XdndPosition event.
146  */
147 void X11DRV_XDND_PositionEvent( HWND hWnd, XClientMessageEvent *event )
148 {
149     XClientMessageEvent e;
150     int accept = 0; /* Assume we're not accepting */
151
152     XDNDxy.x = event->data.l[2] >> 16;
153     XDNDxy.y = event->data.l[2] & 0xFFFF;
154
155     /* FIXME: Notify OLE of DragEnter. Result determines if we accept */
156
157     if (GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES)
158         accept = 1;
159
160     TRACE("action req: %ld accept(%d) at x(%ld),y(%ld)\n",
161           event->data.l[4], accept, XDNDxy.x, XDNDxy.y);
162
163     /*
164      * Let source know if we're accepting the drop by
165      * sending a status message.
166      */
167     e.type = ClientMessage;
168     e.display = event->display;
169     e.window = event->data.l[0];
170     e.message_type = x11drv_atom(XdndStatus);
171     e.format = 32;
172     e.data.l[0] = event->window;
173     e.data.l[1] = accept;
174     e.data.l[2] = 0; /* Empty Rect */
175     e.data.l[3] = 0; /* Empty Rect */
176     if (accept)
177         e.data.l[4] = event->data.l[4];
178     else
179         e.data.l[4] = None;
180     wine_tsx11_lock();
181     XSendEvent(event->display, event->data.l[0], False, NoEventMask, (XEvent*)&e);
182     wine_tsx11_unlock();
183
184     /* FIXME: if drag accepted notify OLE of DragOver */
185 }
186
187 /**************************************************************************
188  * X11DRV_XDND_DropEvent
189  *
190  * Handle an XdndDrop event.
191  */
192 void X11DRV_XDND_DropEvent( HWND hWnd, XClientMessageEvent *event )
193 {
194     XClientMessageEvent e;
195
196     TRACE("\n");
197
198     /* If we have a HDROP type we send a WM_ACCEPTFILES.*/
199     if (GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES)
200         X11DRV_XDND_SendDropFiles( hWnd );
201
202     /* FIXME: Notify OLE of Drop */
203     X11DRV_XDND_FreeDragDropOp();
204
205     /* Tell the target we are finished. */
206     memset(&e, 0, sizeof(e));
207     e.type = ClientMessage;
208     e.display = event->display;
209     e.window = event->data.l[0];
210     e.message_type = x11drv_atom(XdndFinished);
211     e.format = 32;
212     e.data.l[0] = event->window;
213     wine_tsx11_lock();
214     XSendEvent(event->display, event->data.l[0], False, NoEventMask, (XEvent*)&e);
215     wine_tsx11_unlock();
216 }
217
218 /**************************************************************************
219  * X11DRV_XDND_LeaveEvent
220  *
221  * Handle an XdndLeave event.
222  */
223 void X11DRV_XDND_LeaveEvent( HWND hWnd, XClientMessageEvent *event )
224 {
225     TRACE("DND Operation canceled\n");
226
227     X11DRV_XDND_FreeDragDropOp();
228
229     /* FIXME: Notify OLE of DragLeave */
230 }
231
232
233 /**************************************************************************
234  * X11DRV_XDND_ResolveProperty
235  *
236  * Resolve all MIME types to windows clipboard formats. All data is cached.
237  */
238 static void X11DRV_XDND_ResolveProperty(Display *display, Window xwin, Time tm,
239                                         Atom *types, unsigned long *count)
240 {
241     unsigned int i, j;
242     BOOL res;
243     XEvent xe;
244     Atom acttype;
245     int actfmt;
246     unsigned long bytesret, icount;
247     int entries = 0;
248     unsigned char* data = NULL;
249
250     TRACE("count(%ld)\n", *count);
251
252     X11DRV_XDND_FreeDragDropOp(); /* Clear previously cached data */
253
254     for (i = 0; i < *count; i++)
255     {
256         TRACE("requesting atom %ld from xwin %ld\n", types[i], xwin);
257
258         if (types[i] == 0)
259             continue;
260
261         wine_tsx11_lock();
262         XConvertSelection(display, x11drv_atom(XdndSelection), types[i],
263                           x11drv_atom(XdndTarget), xwin, /*tm*/CurrentTime);
264         wine_tsx11_unlock();
265
266         /*
267          * Wait for SelectionNotify
268          */
269         for (j = 0; j < SELECTION_RETRIES; j++)
270         {
271             wine_tsx11_lock();
272             res = XCheckTypedWindowEvent(display, xwin, SelectionNotify, &xe);
273             wine_tsx11_unlock();
274             if (res && xe.xselection.selection == x11drv_atom(XdndSelection)) break;
275
276             usleep(SELECTION_WAIT);
277         }
278
279         if (xe.xselection.property == None)
280             continue;
281
282         wine_tsx11_lock();
283         XGetWindowProperty(display, xwin, x11drv_atom(XdndTarget), 0, 65535, FALSE,
284             AnyPropertyType, &acttype, &actfmt, &icount, &bytesret, &data);
285         wine_tsx11_unlock();
286
287         entries += X11DRV_XDND_MapFormat(types[i], data, icount * (actfmt / 8));
288         wine_tsx11_lock();
289         XFree(data);
290         wine_tsx11_unlock();
291     }
292
293     *count = entries;
294 }
295
296
297 /**************************************************************************
298  * X11DRV_XDND_InsertXDNDData
299  *
300  * Cache available XDND property
301  */
302 static void X11DRV_XDND_InsertXDNDData(int property, int format, void* data, unsigned int len)
303 {
304     LPXDNDDATA current = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XDNDDATA));
305
306     if (current)
307     {
308         EnterCriticalSection(&xdnd_cs);
309         current->next = XDNDData;
310         current->cf_xdnd = property;
311         current->cf_win = format;
312         current->data = data;
313         current->size = len;
314         XDNDData = current;
315         LeaveCriticalSection(&xdnd_cs);
316     }
317 }
318
319
320 /**************************************************************************
321  * X11DRV_XDND_MapFormat
322  *
323  * Map XDND MIME format to windows clipboard format.
324  */
325 static int X11DRV_XDND_MapFormat(unsigned int property, unsigned char *data, int len)
326 {
327     void* xdata;
328     int count = 0;
329
330     TRACE("%d: %s\n", property, data);
331
332     /* Always include the raw type */
333     xdata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
334     memcpy(xdata, data, len);
335     X11DRV_XDND_InsertXDNDData(property, property, xdata, len);
336     count++;
337
338     if (property == x11drv_atom(text_plain))
339         count += X11DRV_XDND_DeconstructTextPlain(property, data, len);
340     else if (property == x11drv_atom(text_html))
341         count += X11DRV_XDND_DeconstructTextHTML(property, data, len);
342
343     return count;
344 }
345
346
347 /**************************************************************************
348  * X11DRV_XDND_DeconstructTextPlain
349  *
350  * Interpret text/plain Data and add records to <dndfmt> linked list
351  */
352 static int X11DRV_XDND_DeconstructTextPlain(int property, void* data, int len)
353 {
354     char *p = (char*) data;
355     char* dostext;
356     int count = 0;
357
358     /* Always suppply plain text */
359     X11DRV_XDND_UnixToDos(&dostext, (char*)data, len);
360     X11DRV_XDND_InsertXDNDData(property, CF_TEXT, dostext, strlen(dostext));
361     count++;
362
363     TRACE("CF_TEXT (%d): %s\n", CF_TEXT, dostext);
364
365     /* Check for additional mappings */
366     while (*p != '\0' && *p != ':') /* Advance to end of protocol */
367         p++;
368
369     if (*p == ':')
370     {
371         if (!strncasecmp(data, "http", 4))
372         {
373             X11DRV_XDND_InsertXDNDData(property, RegisterClipboardFormatA("UniformResourceLocator"),
374                 strdup(dostext), strlen(dostext));
375                 count++;
376
377             TRACE("UniformResourceLocator: %s\n", dostext);
378         }
379         else if (!strncasecmp(data, "file", 4))
380         {
381             DROPFILES* pdf;
382
383             pdf = X11DRV_XDND_BuildDropFiles(p+1, len - 5, XDNDxy);
384             if (pdf)
385             {
386                 unsigned int size = HeapSize(GetProcessHeap(), 0, pdf);
387
388                 X11DRV_XDND_InsertXDNDData(property, CF_HDROP, pdf, size);
389                 count++;
390             }
391
392             TRACE("CF_HDROP: %p\n", pdf);
393         }
394     }
395
396     return count;
397 }
398
399
400 /**************************************************************************
401  * X11DRV_XDND_DeconstructTextHTML
402  *
403  * Interpret text/html data and add records to <dndfmt> linked list
404  */
405 static int X11DRV_XDND_DeconstructTextHTML(int property, void* data, int len)
406 {
407     char* dostext;
408
409     X11DRV_XDND_UnixToDos(&dostext, data, len);
410
411     X11DRV_XDND_InsertXDNDData(property,
412         RegisterClipboardFormatA("UniformResourceLocator"), dostext, strlen(dostext));
413
414     TRACE("UniformResourceLocator: %s\n", dostext);
415
416     return 1;
417 }
418
419
420 /**************************************************************************
421  * X11DRV_XDND_SendDropFiles
422  */
423 static void X11DRV_XDND_SendDropFiles(HWND hwnd)
424 {
425     LPXDNDDATA current;
426
427     EnterCriticalSection(&xdnd_cs);
428
429     current = XDNDData;
430
431     /* Find CF_HDROP type if any */
432     while (current != NULL)
433     {
434         if (current->cf_win == CF_HDROP)
435             break;
436         current = current->next;
437     }
438
439     if (current != NULL)
440     {
441         DROPFILES *lpDrop = (DROPFILES*) current->data;
442
443         if (lpDrop)
444         {
445             lpDrop->pt.x = XDNDxy.x;
446             lpDrop->pt.y = XDNDxy.y;
447
448             TRACE("Sending WM_DROPFILES: hWnd(0x%p) %p(%s)\n", hwnd,
449                 ((char*)lpDrop) + lpDrop->pFiles, ((char*)lpDrop) + lpDrop->pFiles);
450
451             PostMessageA(hwnd, WM_DROPFILES, (WPARAM)lpDrop, 0L);
452         }
453     }
454
455     LeaveCriticalSection(&xdnd_cs);
456 }
457
458
459 /**************************************************************************
460  * X11DRV_XDND_FreeDragDropOp
461  */
462 static void X11DRV_XDND_FreeDragDropOp(void)
463 {
464     LPXDNDDATA next;
465     LPXDNDDATA current;
466
467     TRACE("\n");
468
469     EnterCriticalSection(&xdnd_cs);
470
471     current = XDNDData;
472
473     /** Free data cache */
474     while (current != NULL)
475     {
476         next = current->next;
477         HeapFree(GetProcessHeap(), 0, current);
478         current = next;
479     }
480
481     XDNDData = NULL;
482     XDNDxy.x = XDNDxy.y = 0;
483
484     LeaveCriticalSection(&xdnd_cs);
485 }
486
487
488
489 /**************************************************************************
490  * X11DRV_XDND_BuildDropFiles
491  */
492 static DROPFILES* X11DRV_XDND_BuildDropFiles(char* filename, unsigned int len, POINT pt)
493 {
494     char* pfn;
495     int pathlen;
496     char path[MAX_PATH];
497     DROPFILES *lpDrop = NULL;
498
499     /* Advance to last starting slash */
500     pfn = filename + 1;
501     while (*pfn && (*pfn == '\\' || *pfn =='/'))
502     {
503         pfn++;
504         filename++;
505     }
506
507     /* Remove any trailing \r or \n */
508     while (*pfn)
509     {
510         if (*pfn == '\r' || *pfn == '\n')
511         {
512             *pfn = 0;
513             break;
514         }
515         pfn++;
516     }
517
518     TRACE("%s\n", filename);
519
520     pathlen = GetLongPathNameA(filename, path, MAX_PATH);
521     if (pathlen)
522     {
523         lpDrop = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DROPFILES) + pathlen + 1);
524
525         lpDrop->pFiles = sizeof(DROPFILES);
526         lpDrop->pt.x = pt.x;
527         lpDrop->pt.y = pt.y;
528         lpDrop->fNC = 0;
529         lpDrop->fWide = FALSE;
530
531         strcpy(((char*)lpDrop)+lpDrop->pFiles, path);
532     }
533
534     TRACE("resolved %s\n", lpDrop ? filename : NULL);
535
536     return lpDrop;
537 }
538
539
540 /**************************************************************************
541  * X11DRV_XDND_UnixToDos
542  */
543 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest, char* lpsrc, int len)
544 {
545     int i;
546     unsigned int destlen, lines;
547
548     for (i = 0, lines = 0; i <= len; i++)
549     {
550         if (lpsrc[i] == '\n')
551             lines++;
552     }
553
554     destlen = len + lines + 1;
555
556     if (lpdest)
557     {
558         char* lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, destlen);
559         for (i = 0, lines = 0; i <= len; i++)
560         {
561             if (lpsrc[i] == '\n')
562                 lpstr[++lines + i] = '\r';
563             lpstr[lines + i] = lpsrc[i];
564         }
565
566         *lpdest = lpstr;
567     }
568
569     return lines;
570 }