widl: Fix alignment check for struct members format string.
[wine/multimedia.git] / dlls / winex11.drv / xdnd.c
blobcfb28abf483acb5796492d956a4c565171c11c5f
1 /*
2 * XDND handler code
4 * Copyright 2003 Ulrich Czekalla
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <string.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <stdarg.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "winerror.h"
35 #include "wownt32.h"
37 #include "x11drv.h"
38 #include "shlobj.h" /* DROPFILES */
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(xdnd);
44 /* Maximum wait time for selection notify */
45 #define SELECTION_RETRIES 500 /* wait for .1 seconds */
46 #define SELECTION_WAIT 1000 /* us */
48 typedef struct tagXDNDDATA
50 int cf_win;
51 Atom cf_xdnd;
52 void *data;
53 unsigned int size;
54 struct tagXDNDDATA *next;
55 } XDNDDATA, *LPXDNDDATA;
57 static LPXDNDDATA XDNDData = NULL;
58 static POINT XDNDxy = { 0, 0 };
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);
71 static CRITICAL_SECTION xdnd_cs;
72 static CRITICAL_SECTION_DEBUG critsect_debug =
74 0, 0, &xdnd_cs,
75 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
76 0, 0, { (DWORD_PTR)(__FILE__ ": xdnd_cs") }
78 static CRITICAL_SECTION xdnd_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
81 /**************************************************************************
82 * X11DRV_XDND_EnterEvent
84 * Handle an XdndEnter event.
86 void X11DRV_XDND_EnterEvent( HWND hWnd, XClientMessageEvent *event )
88 Atom *xdndtypes;
89 unsigned long count = 0;
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]);
96 /* If the source supports more than 3 data types we retrieve
97 * the entire list. */
98 if (event->data.l[1] & 1)
100 Atom acttype;
101 int actfmt;
102 unsigned long bytesret;
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();
111 else
113 count = 3;
114 xdndtypes = (Atom*) &event->data.l[2];
117 if (TRACE_ON(xdnd))
119 unsigned int i = 0;
121 wine_tsx11_lock();
122 for (; i < count; i++)
124 if (xdndtypes[i] != 0)
126 char * pn = XGetAtomName(event->display, xdndtypes[i]);
127 TRACE("XDNDEnterAtom %ld: %s\n", xdndtypes[i], pn);
128 XFree(pn);
131 wine_tsx11_unlock();
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);
138 if (event->data.l[1] & 1)
139 XFree(xdndtypes);
142 /**************************************************************************
143 * X11DRV_XDND_PositionEvent
145 * Handle an XdndPosition event.
147 void X11DRV_XDND_PositionEvent( HWND hWnd, XClientMessageEvent *event )
149 XClientMessageEvent e;
150 int accept = 0; /* Assume we're not accepting */
152 XDNDxy.x = event->data.l[2] >> 16;
153 XDNDxy.y = event->data.l[2] & 0xFFFF;
155 /* FIXME: Notify OLE of DragEnter. Result determines if we accept */
157 if (GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES)
158 accept = 1;
160 TRACE("action req: %ld accept(%d) at x(%d),y(%d)\n",
161 event->data.l[4], accept, XDNDxy.x, XDNDxy.y);
164 * Let source know if we're accepting the drop by
165 * sending a status message.
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();
184 /* FIXME: if drag accepted notify OLE of DragOver */
187 /**************************************************************************
188 * X11DRV_XDND_DropEvent
190 * Handle an XdndDrop event.
192 void X11DRV_XDND_DropEvent( HWND hWnd, XClientMessageEvent *event )
194 XClientMessageEvent e;
196 TRACE("\n");
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 );
202 /* FIXME: Notify OLE of Drop */
203 X11DRV_XDND_FreeDragDropOp();
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();
218 /**************************************************************************
219 * X11DRV_XDND_LeaveEvent
221 * Handle an XdndLeave event.
223 void X11DRV_XDND_LeaveEvent( HWND hWnd, XClientMessageEvent *event )
225 TRACE("DND Operation canceled\n");
227 X11DRV_XDND_FreeDragDropOp();
229 /* FIXME: Notify OLE of DragLeave */
233 /**************************************************************************
234 * X11DRV_XDND_ResolveProperty
236 * Resolve all MIME types to windows clipboard formats. All data is cached.
238 static void X11DRV_XDND_ResolveProperty(Display *display, Window xwin, Time tm,
239 Atom *types, unsigned long *count)
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;
250 TRACE("count(%ld)\n", *count);
252 X11DRV_XDND_FreeDragDropOp(); /* Clear previously cached data */
254 for (i = 0; i < *count; i++)
256 TRACE("requesting atom %ld from xwin %ld\n", types[i], xwin);
258 if (types[i] == 0)
259 continue;
261 wine_tsx11_lock();
262 XConvertSelection(display, x11drv_atom(XdndSelection), types[i],
263 x11drv_atom(XdndTarget), xwin, /*tm*/CurrentTime);
264 wine_tsx11_unlock();
267 * Wait for SelectionNotify
269 for (j = 0; j < SELECTION_RETRIES; j++)
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;
276 usleep(SELECTION_WAIT);
279 if (xe.xselection.property == None)
280 continue;
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();
287 entries += X11DRV_XDND_MapFormat(types[i], data, icount * (actfmt / 8));
288 wine_tsx11_lock();
289 XFree(data);
290 wine_tsx11_unlock();
293 *count = entries;
297 /**************************************************************************
298 * X11DRV_XDND_InsertXDNDData
300 * Cache available XDND property
302 static void X11DRV_XDND_InsertXDNDData(int property, int format, void* data, unsigned int len)
304 LPXDNDDATA current = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XDNDDATA));
306 if (current)
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);
320 /**************************************************************************
321 * X11DRV_XDND_MapFormat
323 * Map XDND MIME format to windows clipboard format.
325 static int X11DRV_XDND_MapFormat(unsigned int property, unsigned char *data, int len)
327 void* xdata;
328 int count = 0;
330 TRACE("%d: %s\n", property, data);
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++;
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);
343 return count;
347 /**************************************************************************
348 * X11DRV_XDND_DeconstructTextPlain
350 * Interpret text/plain Data and add records to <dndfmt> linked list
352 static int X11DRV_XDND_DeconstructTextPlain(int property, void* data, int len)
354 char *p = (char*) data;
355 char* dostext;
356 int count = 0;
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++;
363 TRACE("CF_TEXT (%d): %s\n", CF_TEXT, dostext);
365 /* Check for additional mappings */
366 while (*p != '\0' && *p != ':') /* Advance to end of protocol */
367 p++;
369 if (*p == ':')
371 if (!strncasecmp(data, "http", 4))
373 X11DRV_XDND_InsertXDNDData(property, RegisterClipboardFormatA("UniformResourceLocator"),
374 strdup(dostext), strlen(dostext));
375 count++;
377 TRACE("UniformResourceLocator: %s\n", dostext);
379 else if (!strncasecmp(data, "file", 4))
381 DROPFILES* pdf;
383 pdf = X11DRV_XDND_BuildDropFiles(p+1, len - 5, XDNDxy);
384 if (pdf)
386 unsigned int size = HeapSize(GetProcessHeap(), 0, pdf);
388 X11DRV_XDND_InsertXDNDData(property, CF_HDROP, pdf, size);
389 count++;
392 TRACE("CF_HDROP: %p\n", pdf);
396 return count;
400 /**************************************************************************
401 * X11DRV_XDND_DeconstructTextHTML
403 * Interpret text/html data and add records to <dndfmt> linked list
405 static int X11DRV_XDND_DeconstructTextHTML(int property, void* data, int len)
407 char* dostext;
409 X11DRV_XDND_UnixToDos(&dostext, data, len);
411 X11DRV_XDND_InsertXDNDData(property,
412 RegisterClipboardFormatA("UniformResourceLocator"), dostext, strlen(dostext));
414 TRACE("UniformResourceLocator: %s\n", dostext);
416 return 1;
420 /**************************************************************************
421 * X11DRV_XDND_SendDropFiles
423 static void X11DRV_XDND_SendDropFiles(HWND hwnd)
425 LPXDNDDATA current;
427 EnterCriticalSection(&xdnd_cs);
429 current = XDNDData;
431 /* Find CF_HDROP type if any */
432 while (current != NULL)
434 if (current->cf_win == CF_HDROP)
435 break;
436 current = current->next;
439 if (current != NULL)
441 DROPFILES *lpDrop = (DROPFILES*) current->data;
443 if (lpDrop)
445 lpDrop->pt.x = XDNDxy.x;
446 lpDrop->pt.y = XDNDxy.y;
448 TRACE("Sending WM_DROPFILES: hWnd(0x%p) %p(%s)\n", hwnd,
449 ((char*)lpDrop) + lpDrop->pFiles, ((char*)lpDrop) + lpDrop->pFiles);
451 PostMessageA(hwnd, WM_DROPFILES, (WPARAM)lpDrop, 0L);
455 LeaveCriticalSection(&xdnd_cs);
459 /**************************************************************************
460 * X11DRV_XDND_FreeDragDropOp
462 static void X11DRV_XDND_FreeDragDropOp(void)
464 LPXDNDDATA next;
465 LPXDNDDATA current;
467 TRACE("\n");
469 EnterCriticalSection(&xdnd_cs);
471 current = XDNDData;
473 /** Free data cache */
474 while (current != NULL)
476 next = current->next;
477 HeapFree(GetProcessHeap(), 0, current);
478 current = next;
481 XDNDData = NULL;
482 XDNDxy.x = XDNDxy.y = 0;
484 LeaveCriticalSection(&xdnd_cs);
489 /**************************************************************************
490 * X11DRV_XDND_BuildDropFiles
492 static DROPFILES* X11DRV_XDND_BuildDropFiles(char* filename, unsigned int len, POINT pt)
494 char* pfn;
495 int pathlen;
496 char path[MAX_PATH];
497 DROPFILES *lpDrop = NULL;
499 /* Advance to last starting slash */
500 pfn = filename + 1;
501 while (*pfn && (*pfn == '\\' || *pfn =='/'))
503 pfn++;
504 filename++;
507 /* Remove any trailing \r or \n */
508 while (*pfn)
510 if (*pfn == '\r' || *pfn == '\n')
512 *pfn = 0;
513 break;
515 pfn++;
518 TRACE("%s\n", filename);
520 pathlen = GetLongPathNameA(filename, path, MAX_PATH);
521 if (pathlen)
523 lpDrop = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DROPFILES) + pathlen + 1);
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;
531 strcpy(((char*)lpDrop)+lpDrop->pFiles, path);
534 TRACE("resolved %s\n", lpDrop ? filename : NULL);
536 return lpDrop;
540 /**************************************************************************
541 * X11DRV_XDND_UnixToDos
543 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest, char* lpsrc, int len)
545 int i;
546 unsigned int destlen, lines;
548 for (i = 0, lines = 0; i <= len; i++)
550 if (lpsrc[i] == '\n')
551 lines++;
554 destlen = len + lines + 1;
556 if (lpdest)
558 char* lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, destlen);
559 for (i = 0, lines = 0; i <= len; i++)
561 if (lpsrc[i] == '\n')
562 lpstr[++lines + i] = '\r';
563 lpstr[lines + i] = lpsrc[i];
566 *lpdest = lpstr;
569 return lines;