Make the standard create_file request handle serial ports too, and
[wine.git] / dlls / x11drv / xdnd.c
blob52ade4ca09d5828045c2bb0131c543b733e17b13
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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, { 0, (DWORD)(__FILE__ ": xdnd_cs") }
78 static CRITICAL_SECTION xdnd_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
80 /**************************************************************************
81 * X11DRV_XDND_Event
83 * Entry point for X11 XDND events. Returns FALSE if event is not handled.
85 int X11DRV_XDND_Event(HWND hWnd, XClientMessageEvent *event)
87 int isXDNDMsg = 1;
89 TRACE("0x%p\n", hWnd);
91 if (event->message_type == x11drv_atom(XdndEnter))
93 Atom *xdndtypes;
94 unsigned long count = 0;
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]);
101 /* If the source supports more than 3 data types we retrieve
102 * the entire list. */
103 if (event->data.l[1] & 1)
105 Atom acttype;
106 int actfmt;
107 unsigned long bytesret;
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();
116 else
118 count = 3;
119 xdndtypes = &event->data.l[2];
122 if (TRACE_ON(xdnd))
124 unsigned int i = 0;
126 wine_tsx11_lock();
127 for (; i < count; i++)
129 if (xdndtypes[i] != 0)
131 char * pn = XGetAtomName(event->display, xdndtypes[i]);
132 TRACE("XDNDEnterAtom %ld: %s\n", xdndtypes[i], pn);
133 XFree(pn);
136 wine_tsx11_unlock();
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);
143 if (event->data.l[1] & 1)
144 XFree(xdndtypes);
146 else if (event->message_type == x11drv_atom(XdndPosition))
148 XClientMessageEvent e;
149 int accept = 0; /* Assume we're not accepting */
151 XDNDxy.x = event->data.l[2] >> 16;
152 XDNDxy.y = event->data.l[2] & 0xFFFF;
154 /* FIXME: Notify OLE of DragEnter. Result determines if we accept */
156 if (GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES)
157 accept = 1;
159 TRACE("XDNDPosition. action req: %ld accept(%d) at x(%ld),y(%ld)\n",
160 event->data.l[4], accept, XDNDxy.x, XDNDxy.y);
163 * Let source know if we're accepting the drop by
164 * sending a status message.
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();
183 /* FIXME: if drag accepted notify OLE of DragOver */
185 else if (event->message_type == x11drv_atom(XdndDrop))
187 XClientMessageEvent e;
189 TRACE("XDNDDrop\n");
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 );
195 /* FIXME: Notify OLE of Drop */
196 X11DRV_XDND_FreeDragDropOp();
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();
210 else if (event->message_type == x11drv_atom(XdndLeave))
212 TRACE("DND Operation canceled\n");
214 X11DRV_XDND_FreeDragDropOp();
216 /* FIXME: Notify OLE of DragLeave */
218 else /* Not an XDND message */
219 isXDNDMsg = 0;
221 return isXDNDMsg;
225 /**************************************************************************
226 * X11DRV_XDND_ResolveProperty
228 * Resolve all MIME types to windows clipboard formats. All data is cached.
230 static void X11DRV_XDND_ResolveProperty(Display *display, Window xwin, Time tm,
231 Atom *types, unsigned long *count)
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;
242 TRACE("count(%ld)\n", *count);
244 X11DRV_XDND_FreeDragDropOp(); /* Clear previously cached data */
246 for (i = 0; i < *count; i++)
248 TRACE("requesting atom %ld from xwin %ld\n", types[i], xwin);
250 if (types[i] == 0)
251 continue;
253 wine_tsx11_lock();
254 XConvertSelection(display, x11drv_atom(XdndSelection), types[i],
255 x11drv_atom(XdndTarget), xwin, /*tm*/CurrentTime);
256 wine_tsx11_unlock();
259 * Wait for SelectionNotify
261 for (j = 0; j < SELECTION_RETRIES; j++)
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;
268 usleep(SELECTION_WAIT);
271 if (xe.xselection.property == None)
272 continue;
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();
279 entries += X11DRV_XDND_MapFormat(types[i], data, icount * (actfmt / 8));
280 wine_tsx11_lock();
281 XFree(data);
282 wine_tsx11_unlock();
285 *count = entries;
289 /**************************************************************************
290 * X11DRV_XDND_InsertXDNDData
292 * Cache available XDND property
294 static void X11DRV_XDND_InsertXDNDData(int property, int format, void* data, unsigned int len)
296 LPXDNDDATA current = (LPXDNDDATA) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XDNDDATA));
298 if (current)
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);
312 /**************************************************************************
313 * X11DRV_XDND_MapFormat
315 * Map XDND MIME format to windows clipboard format.
317 static int X11DRV_XDND_MapFormat(unsigned int property, unsigned char *data, int len)
319 void* xdata;
320 int count = 0;
322 TRACE("%d: %s\n", property, data);
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++;
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);
335 return count;
339 /**************************************************************************
340 * X11DRV_XDND_DeconstructTextPlain
342 * Interpret text/plain Data and add records to <dndfmt> linked list
344 static int X11DRV_XDND_DeconstructTextPlain(int property, void* data, int len)
346 char *p = (char*) data;
347 char* dostext;
348 int count = 0;
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++;
355 TRACE("CF_TEXT (%d): %s\n", CF_TEXT, dostext);
357 /* Check for additional mappings */
358 while (*p != '\0' && *p != ':') /* Advance to end of protocol */
359 p++;
361 if (*p == ':')
363 if (!strncasecmp(data, "http", 4))
365 X11DRV_XDND_InsertXDNDData(property, RegisterClipboardFormatA("UniformResourceLocator"),
366 strdup(dostext), strlen(dostext));
367 count++;
369 TRACE("UniformResourceLocator: %s\n", dostext);
371 else if (!strncasecmp(data, "file", 4))
373 DROPFILES* pdf;
375 pdf = X11DRV_XDND_BuildDropFiles(p+1, len - 5, XDNDxy);
376 if (pdf)
378 unsigned int size = HeapSize(GetProcessHeap(), 0, pdf);
380 X11DRV_XDND_InsertXDNDData(property, CF_HDROP, pdf, size);
381 count++;
384 TRACE("CF_HDROP: %p\n", pdf);
388 return count;
392 /**************************************************************************
393 * X11DRV_XDND_DeconstructTextHTML
395 * Interpret text/html data and add records to <dndfmt> linked list
397 static int X11DRV_XDND_DeconstructTextHTML(int property, void* data, int len)
399 char* dostext;
401 X11DRV_XDND_UnixToDos(&dostext, data, len);
403 X11DRV_XDND_InsertXDNDData(property,
404 RegisterClipboardFormatA("UniformResourceLocator"), dostext, strlen(dostext));
406 TRACE("UniformResourceLocator: %s\n", dostext);
408 return 1;
412 /**************************************************************************
413 * X11DRV_XDND_SendDropFiles
415 static void X11DRV_XDND_SendDropFiles(HWND hwnd)
417 LPXDNDDATA current;
419 EnterCriticalSection(&xdnd_cs);
421 current = XDNDData;
423 /* Find CF_HDROP type if any */
424 while (current != NULL)
426 if (current->cf_win == CF_HDROP)
427 break;
428 current = current->next;
431 if (current != NULL)
433 DROPFILES *lpDrop = (DROPFILES*) current->data;
435 if (lpDrop)
437 lpDrop->pt.x = XDNDxy.x;
438 lpDrop->pt.y = XDNDxy.y;
440 TRACE("Sending WM_DROPFILES: hWnd(0x%p) %p(%s)\n", hwnd,
441 ((char*)lpDrop) + lpDrop->pFiles, ((char*)lpDrop) + lpDrop->pFiles);
443 PostMessageA(hwnd, WM_DROPFILES, (WPARAM)lpDrop, 0L);
447 LeaveCriticalSection(&xdnd_cs);
451 /**************************************************************************
452 * X11DRV_XDND_FreeDragDropOp
454 static void X11DRV_XDND_FreeDragDropOp(void)
456 LPXDNDDATA next;
457 LPXDNDDATA current;
459 TRACE("\n");
461 EnterCriticalSection(&xdnd_cs);
463 current = XDNDData;
465 /** Free data cache */
466 while (current != NULL)
468 next = current->next;
469 HeapFree(GetProcessHeap(), 0, current);
470 current = next;
473 XDNDData = NULL;
474 XDNDxy.x = XDNDxy.y = 0;
476 LeaveCriticalSection(&xdnd_cs);
481 /**************************************************************************
482 * X11DRV_XDND_BuildDropFiles
484 static DROPFILES* X11DRV_XDND_BuildDropFiles(char* filename, unsigned int len, POINT pt)
486 char* pfn;
487 int pathlen;
488 char path[MAX_PATH];
489 DROPFILES *lpDrop = NULL;
491 /* Advance to last starting slash */
492 pfn = filename + 1;
493 while (*pfn && (*pfn == '\\' || *pfn =='/'))
495 pfn++;
496 filename++;
499 /* Remove any trailing \r or \n */
500 while (*pfn)
502 if (*pfn == '\r' || *pfn == '\n')
504 *pfn = 0;
505 break;
507 pfn++;
510 TRACE("%s\n", filename);
512 pathlen = GetLongPathNameA(filename, path, MAX_PATH);
513 if (pathlen)
515 lpDrop = (DROPFILES*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
516 sizeof(DROPFILES) + pathlen + 1);
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;
524 strcpy(((char*)lpDrop)+lpDrop->pFiles, path);
527 TRACE("resolved %s\n", lpDrop ? filename : NULL);
529 return lpDrop;
533 /**************************************************************************
534 * X11DRV_XDND_UnixToDos
536 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest, char* lpsrc, int len)
538 int i;
539 unsigned int destlen, lines;
541 for (i = 0, lines = 0; i <= len; i++)
543 if (lpsrc[i] == '\n')
544 lines++;
547 destlen = len + lines + 1;
549 if (lpdest)
551 char* lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, destlen);
552 for (i = 0, lines = 0; i <= len; i++)
554 if (lpsrc[i] == '\n')
555 lpstr[++lines + i] = '\r';
556 lpstr[lines + i] = lpsrc[i];
559 *lpdest = lpstr;
562 return lines;