winealsa.drv: Add midi realtime and midi common messages on midi input.
[wine/testsucceed.git] / dlls / winex11.drv / xdnd.c
blobaf04d7590126f9f6d519c6f279676c6df62cba8d
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"
36 #include "x11drv.h"
37 #include "shlobj.h" /* DROPFILES */
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(xdnd);
43 /* Maximum wait time for selection notify */
44 #define SELECTION_RETRIES 500 /* wait for .1 seconds */
45 #define SELECTION_WAIT 1000 /* us */
47 typedef struct tagXDNDDATA
49 int cf_win;
50 Atom cf_xdnd;
51 void *data;
52 unsigned int size;
53 struct tagXDNDDATA *next;
54 } XDNDDATA, *LPXDNDDATA;
56 static LPXDNDDATA XDNDData = NULL;
57 static POINT XDNDxy = { 0, 0 };
59 static void X11DRV_XDND_InsertXDNDData(int property, int format, void* data, unsigned int len);
60 static int X11DRV_XDND_DeconstructTextPlain(int property, void* data, int len);
61 static int X11DRV_XDND_DeconstructTextHTML(int property, void* data, int len);
62 static int X11DRV_XDND_MapFormat(unsigned int property, unsigned char *data, int len);
63 static void X11DRV_XDND_ResolveProperty(Display *display, Window xwin, Time tm,
64 Atom *types, unsigned long *count);
65 static void X11DRV_XDND_SendDropFiles(HWND hwnd);
66 static void X11DRV_XDND_FreeDragDropOp(void);
67 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest, char* lpsrc, int len);
68 static DROPFILES* X11DRV_XDND_BuildDropFiles(char* filename, unsigned int len, POINT pt);
70 static CRITICAL_SECTION xdnd_cs;
71 static CRITICAL_SECTION_DEBUG critsect_debug =
73 0, 0, &xdnd_cs,
74 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
75 0, 0, { (DWORD_PTR)(__FILE__ ": xdnd_cs") }
77 static CRITICAL_SECTION xdnd_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
80 /**************************************************************************
81 * X11DRV_XDND_EnterEvent
83 * Handle an XdndEnter event.
85 void X11DRV_XDND_EnterEvent( HWND hWnd, XClientMessageEvent *event )
87 Atom *xdndtypes;
88 unsigned long count = 0;
90 TRACE("ver(%ld) check-XdndTypeList(%ld) data=%ld,%ld,%ld,%ld,%ld\n",
91 (event->data.l[1] & 0xFF000000) >> 24, (event->data.l[1] & 1),
92 event->data.l[0], event->data.l[1], event->data.l[2],
93 event->data.l[3], event->data.l[4]);
95 /* If the source supports more than 3 data types we retrieve
96 * the entire list. */
97 if (event->data.l[1] & 1)
99 Atom acttype;
100 int actfmt;
101 unsigned long bytesret;
103 /* Request supported formats from source window */
104 wine_tsx11_lock();
105 XGetWindowProperty(event->display, event->data.l[0], x11drv_atom(XdndTypeList),
106 0, 65535, FALSE, AnyPropertyType, &acttype, &actfmt, &count,
107 &bytesret, (unsigned char**)&xdndtypes);
108 wine_tsx11_unlock();
110 else
112 count = 3;
113 xdndtypes = (Atom*) &event->data.l[2];
116 if (TRACE_ON(xdnd))
118 unsigned int i = 0;
120 wine_tsx11_lock();
121 for (; i < count; i++)
123 if (xdndtypes[i] != 0)
125 char * pn = XGetAtomName(event->display, xdndtypes[i]);
126 TRACE("XDNDEnterAtom %ld: %s\n", xdndtypes[i], pn);
127 XFree(pn);
130 wine_tsx11_unlock();
133 /* Do a one-time data read and cache results */
134 X11DRV_XDND_ResolveProperty(event->display, event->window,
135 event->data.l[1], xdndtypes, &count);
137 if (event->data.l[1] & 1)
138 XFree(xdndtypes);
141 /**************************************************************************
142 * X11DRV_XDND_PositionEvent
144 * Handle an XdndPosition event.
146 void X11DRV_XDND_PositionEvent( HWND hWnd, XClientMessageEvent *event )
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("action req: %ld accept(%d) at x(%d),y(%d)\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 */
186 /**************************************************************************
187 * X11DRV_XDND_DropEvent
189 * Handle an XdndDrop event.
191 void X11DRV_XDND_DropEvent( HWND hWnd, XClientMessageEvent *event )
193 XClientMessageEvent e;
195 TRACE("\n");
197 /* If we have a HDROP type we send a WM_ACCEPTFILES.*/
198 if (GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES)
199 X11DRV_XDND_SendDropFiles( hWnd );
201 /* FIXME: Notify OLE of Drop */
202 X11DRV_XDND_FreeDragDropOp();
204 /* Tell the target we are finished. */
205 memset(&e, 0, sizeof(e));
206 e.type = ClientMessage;
207 e.display = event->display;
208 e.window = event->data.l[0];
209 e.message_type = x11drv_atom(XdndFinished);
210 e.format = 32;
211 e.data.l[0] = event->window;
212 wine_tsx11_lock();
213 XSendEvent(event->display, event->data.l[0], False, NoEventMask, (XEvent*)&e);
214 wine_tsx11_unlock();
217 /**************************************************************************
218 * X11DRV_XDND_LeaveEvent
220 * Handle an XdndLeave event.
222 void X11DRV_XDND_LeaveEvent( HWND hWnd, XClientMessageEvent *event )
224 TRACE("DND Operation canceled\n");
226 X11DRV_XDND_FreeDragDropOp();
228 /* FIXME: Notify OLE of DragLeave */
232 /**************************************************************************
233 * X11DRV_XDND_ResolveProperty
235 * Resolve all MIME types to windows clipboard formats. All data is cached.
237 static void X11DRV_XDND_ResolveProperty(Display *display, Window xwin, Time tm,
238 Atom *types, unsigned long *count)
240 unsigned int i, j;
241 BOOL res;
242 XEvent xe;
243 Atom acttype;
244 int actfmt;
245 unsigned long bytesret, icount;
246 int entries = 0;
247 unsigned char* data = NULL;
249 TRACE("count(%ld)\n", *count);
251 X11DRV_XDND_FreeDragDropOp(); /* Clear previously cached data */
253 for (i = 0; i < *count; i++)
255 TRACE("requesting atom %ld from xwin %ld\n", types[i], xwin);
257 if (types[i] == 0)
258 continue;
260 wine_tsx11_lock();
261 XConvertSelection(display, x11drv_atom(XdndSelection), types[i],
262 x11drv_atom(XdndTarget), xwin, /*tm*/CurrentTime);
263 wine_tsx11_unlock();
266 * Wait for SelectionNotify
268 for (j = 0; j < SELECTION_RETRIES; j++)
270 wine_tsx11_lock();
271 res = XCheckTypedWindowEvent(display, xwin, SelectionNotify, &xe);
272 wine_tsx11_unlock();
273 if (res && xe.xselection.selection == x11drv_atom(XdndSelection)) break;
275 usleep(SELECTION_WAIT);
278 if (xe.xselection.property == None)
279 continue;
281 wine_tsx11_lock();
282 XGetWindowProperty(display, xwin, x11drv_atom(XdndTarget), 0, 65535, FALSE,
283 AnyPropertyType, &acttype, &actfmt, &icount, &bytesret, &data);
284 wine_tsx11_unlock();
286 entries += X11DRV_XDND_MapFormat(types[i], data, icount * (actfmt / 8));
287 wine_tsx11_lock();
288 XFree(data);
289 wine_tsx11_unlock();
292 *count = entries;
296 /**************************************************************************
297 * X11DRV_XDND_InsertXDNDData
299 * Cache available XDND property
301 static void X11DRV_XDND_InsertXDNDData(int property, int format, void* data, unsigned int len)
303 LPXDNDDATA current = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XDNDDATA));
305 if (current)
307 EnterCriticalSection(&xdnd_cs);
308 current->next = XDNDData;
309 current->cf_xdnd = property;
310 current->cf_win = format;
311 current->data = data;
312 current->size = len;
313 XDNDData = current;
314 LeaveCriticalSection(&xdnd_cs);
319 /**************************************************************************
320 * X11DRV_XDND_MapFormat
322 * Map XDND MIME format to windows clipboard format.
324 static int X11DRV_XDND_MapFormat(unsigned int property, unsigned char *data, int len)
326 void* xdata;
327 int count = 0;
329 TRACE("%d: %s\n", property, data);
331 /* Always include the raw type */
332 xdata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
333 memcpy(xdata, data, len);
334 X11DRV_XDND_InsertXDNDData(property, property, xdata, len);
335 count++;
337 if (property == x11drv_atom(text_plain))
338 count += X11DRV_XDND_DeconstructTextPlain(property, data, len);
339 else if (property == x11drv_atom(text_html))
340 count += X11DRV_XDND_DeconstructTextHTML(property, data, len);
342 return count;
346 /**************************************************************************
347 * X11DRV_XDND_DeconstructTextPlain
349 * Interpret text/plain Data and add records to <dndfmt> linked list
351 static int X11DRV_XDND_DeconstructTextPlain(int property, void* data, int len)
353 char *p = (char*) data;
354 char* dostext;
355 int count = 0;
357 /* Always suppply plain text */
358 X11DRV_XDND_UnixToDos(&dostext, (char*)data, len);
359 X11DRV_XDND_InsertXDNDData(property, CF_TEXT, dostext, strlen(dostext));
360 count++;
362 TRACE("CF_TEXT (%d): %s\n", CF_TEXT, dostext);
364 /* Check for additional mappings */
365 while (*p != '\0' && *p != ':') /* Advance to end of protocol */
366 p++;
368 if (*p == ':')
370 if (!strncasecmp(data, "http", 4))
372 X11DRV_XDND_InsertXDNDData(property, RegisterClipboardFormatA("UniformResourceLocator"),
373 strdup(dostext), strlen(dostext));
374 count++;
376 TRACE("UniformResourceLocator: %s\n", dostext);
378 else if (!strncasecmp(data, "file", 4))
380 DROPFILES* pdf;
382 pdf = X11DRV_XDND_BuildDropFiles(p+1, len - 5, XDNDxy);
383 if (pdf)
385 unsigned int size = HeapSize(GetProcessHeap(), 0, pdf);
387 X11DRV_XDND_InsertXDNDData(property, CF_HDROP, pdf, size);
388 count++;
391 TRACE("CF_HDROP: %p\n", pdf);
395 return count;
399 /**************************************************************************
400 * X11DRV_XDND_DeconstructTextHTML
402 * Interpret text/html data and add records to <dndfmt> linked list
404 static int X11DRV_XDND_DeconstructTextHTML(int property, void* data, int len)
406 char* dostext;
408 X11DRV_XDND_UnixToDos(&dostext, data, len);
410 X11DRV_XDND_InsertXDNDData(property,
411 RegisterClipboardFormatA("UniformResourceLocator"), dostext, strlen(dostext));
413 TRACE("UniformResourceLocator: %s\n", dostext);
415 return 1;
419 /**************************************************************************
420 * X11DRV_XDND_SendDropFiles
422 static void X11DRV_XDND_SendDropFiles(HWND hwnd)
424 LPXDNDDATA current;
426 EnterCriticalSection(&xdnd_cs);
428 current = XDNDData;
430 /* Find CF_HDROP type if any */
431 while (current != NULL)
433 if (current->cf_win == CF_HDROP)
434 break;
435 current = current->next;
438 if (current != NULL)
440 DROPFILES *lpDrop = (DROPFILES*) current->data;
442 if (lpDrop)
444 lpDrop->pt.x = XDNDxy.x;
445 lpDrop->pt.y = XDNDxy.y;
447 TRACE("Sending WM_DROPFILES: hWnd(0x%p) %p(%s)\n", hwnd,
448 ((char*)lpDrop) + lpDrop->pFiles, ((char*)lpDrop) + lpDrop->pFiles);
450 PostMessageA(hwnd, WM_DROPFILES, (WPARAM)lpDrop, 0L);
454 LeaveCriticalSection(&xdnd_cs);
458 /**************************************************************************
459 * X11DRV_XDND_FreeDragDropOp
461 static void X11DRV_XDND_FreeDragDropOp(void)
463 LPXDNDDATA next;
464 LPXDNDDATA current;
466 TRACE("\n");
468 EnterCriticalSection(&xdnd_cs);
470 current = XDNDData;
472 /** Free data cache */
473 while (current != NULL)
475 next = current->next;
476 HeapFree(GetProcessHeap(), 0, current);
477 current = next;
480 XDNDData = NULL;
481 XDNDxy.x = XDNDxy.y = 0;
483 LeaveCriticalSection(&xdnd_cs);
488 /**************************************************************************
489 * X11DRV_XDND_BuildDropFiles
491 static DROPFILES* X11DRV_XDND_BuildDropFiles(char* filename, unsigned int len, POINT pt)
493 char* pfn;
494 int pathlen;
495 char path[MAX_PATH];
496 DROPFILES *lpDrop = NULL;
498 /* Advance to last starting slash */
499 pfn = filename + 1;
500 while (*pfn && (*pfn == '\\' || *pfn =='/'))
502 pfn++;
503 filename++;
506 /* Remove any trailing \r or \n */
507 while (*pfn)
509 if (*pfn == '\r' || *pfn == '\n')
511 *pfn = 0;
512 break;
514 pfn++;
517 TRACE("%s\n", filename);
519 pathlen = GetLongPathNameA(filename, path, MAX_PATH);
520 if (pathlen)
522 lpDrop = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DROPFILES) + pathlen + 1);
524 lpDrop->pFiles = sizeof(DROPFILES);
525 lpDrop->pt.x = pt.x;
526 lpDrop->pt.y = pt.y;
527 lpDrop->fNC = 0;
528 lpDrop->fWide = FALSE;
530 strcpy(((char*)lpDrop)+lpDrop->pFiles, path);
533 TRACE("resolved %s\n", lpDrop ? filename : NULL);
535 return lpDrop;
539 /**************************************************************************
540 * X11DRV_XDND_UnixToDos
542 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest, char* lpsrc, int len)
544 int i;
545 unsigned int destlen, lines;
547 for (i = 0, lines = 0; i <= len; i++)
549 if (lpsrc[i] == '\n')
550 lines++;
553 destlen = len + lines + 1;
555 if (lpdest)
557 char* lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, destlen);
558 for (i = 0, lines = 0; i <= len; i++)
560 if (lpsrc[i] == '\n')
561 lpstr[++lines + i] = '\r';
562 lpstr[lines + i] = lpsrc[i];
565 *lpdest = lpstr;
568 return lines;