StrRetToStrNAW returns ok on NT4, though it should return FALSE.
[wine] / dlls / x11drv / mouse.c
1 /*
2  * X11 mouse driver
3  *
4  * Copyright 1998 Ulrich Weigand
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
23 #include <X11/Xlib.h>
24 #ifdef HAVE_LIBXXF86DGA2
25 #include <X11/extensions/xf86dga.h>
26 #endif
27 #include <stdarg.h>
28
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wine/winuser16.h"
34
35 #include "win.h"
36 #include "x11drv.h"
37 #include "wine/server.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
41
42 /**********************************************************************/
43
44 #define NB_BUTTONS   5     /* Windows can handle 3 buttons and the wheel too */
45
46 static const UINT button_down_flags[NB_BUTTONS] =
47 {
48     MOUSEEVENTF_LEFTDOWN,
49     MOUSEEVENTF_MIDDLEDOWN,
50     MOUSEEVENTF_RIGHTDOWN,
51     MOUSEEVENTF_WHEEL,
52     MOUSEEVENTF_WHEEL
53 };
54
55 static const UINT button_up_flags[NB_BUTTONS] =
56 {
57     MOUSEEVENTF_LEFTUP,
58     MOUSEEVENTF_MIDDLEUP,
59     MOUSEEVENTF_RIGHTUP,
60     0,
61     0
62 };
63
64 POINT cursor_pos;
65
66 /***********************************************************************
67  *              get_coords
68  *
69  * get the coordinates of a mouse event
70  */
71 static inline void get_coords( HWND hwnd, int x, int y, POINT *pt )
72 {
73     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
74
75     if (!data) return;
76
77     pt->x = x + data->whole_rect.left;
78     pt->y = y + data->whole_rect.top;
79 }
80
81
82 /***********************************************************************
83  *              update_button_state
84  *
85  * Update the button state with what X provides us
86  */
87 static inline void update_button_state( unsigned int state )
88 {
89     key_state_table[VK_LBUTTON] = (state & Button1Mask ? 0x80 : 0);
90     key_state_table[VK_MBUTTON] = (state & Button2Mask ? 0x80 : 0);
91     key_state_table[VK_RBUTTON] = (state & Button3Mask ? 0x80 : 0);
92 }
93
94
95 /***********************************************************************
96  *              update_key_state
97  *
98  * Update the key state with what X provides us
99  */
100 static inline void update_key_state( unsigned int state )
101 {
102     key_state_table[VK_SHIFT]   = (state & ShiftMask   ? 0x80 : 0);
103     key_state_table[VK_CONTROL] = (state & ControlMask ? 0x80 : 0);
104 }
105
106
107 /***********************************************************************
108  *              update_mouse_state
109  *
110  * Update the various window states on a mouse event.
111  */
112 static void update_mouse_state( HWND hwnd, Window window, int x, int y, unsigned int state, POINT *pt )
113 {
114     struct x11drv_thread_data *data = x11drv_thread_data();
115
116     get_coords( hwnd, x, y, pt );
117     update_key_state( state );
118
119     /* update the cursor */
120
121     if (data->cursor_window != window)
122     {
123         data->cursor_window = window;
124         wine_tsx11_lock();
125         if (data->cursor) XDefineCursor( data->display, window, data->cursor );
126         wine_tsx11_unlock();
127     }
128
129     /* update the wine server Z-order */
130
131     if (window != data->grab_window &&
132         /* ignore event if a button is pressed, since the mouse is then grabbed too */
133         !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask)))
134     {
135         SERVER_START_REQ( update_window_zorder )
136         {
137             req->window      = hwnd;
138             req->rect.left   = pt->x;
139             req->rect.top    = pt->y;
140             req->rect.right  = pt->x + 1;
141             req->rect.bottom = pt->y + 1;
142             wine_server_call( req );
143         }
144         SERVER_END_REQ;
145     }
146 }
147
148
149 /***********************************************************************
150  *           get_key_state
151  */
152 static WORD get_key_state(void)
153 {
154     WORD ret = 0;
155
156     if (GetSystemMetrics( SM_SWAPBUTTON ))
157     {
158         if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
159         if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
160     }
161     else
162     {
163         if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
164         if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
165     }
166     if (key_state_table[VK_MBUTTON] & 0x80)  ret |= MK_MBUTTON;
167     if (key_state_table[VK_SHIFT] & 0x80)    ret |= MK_SHIFT;
168     if (key_state_table[VK_CONTROL] & 0x80)  ret |= MK_CONTROL;
169     if (key_state_table[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
170     if (key_state_table[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
171     return ret;
172 }
173
174
175 /***********************************************************************
176  *           queue_raw_mouse_message
177  */
178 static void queue_raw_mouse_message( UINT message, HWND hwnd, DWORD x, DWORD y,
179                                      DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
180 {
181     MSLLHOOKSTRUCT hook;
182
183     hook.pt.x        = x;
184     hook.pt.y        = y;
185     hook.mouseData   = MAKELONG( 0, data );
186     hook.flags       = injected_flags;
187     hook.time        = time;
188     hook.dwExtraInfo = extra_info;
189
190     if (HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE )) return;
191
192     SERVER_START_REQ( send_message )
193     {
194         req->id       = (injected_flags & LLMHF_INJECTED) ? 0 : GetCurrentThreadId();
195         req->type     = MSG_HARDWARE;
196         req->flags    = 0;
197         req->win      = hwnd;
198         req->msg      = message;
199         req->wparam   = MAKEWPARAM( get_key_state(), data );
200         req->lparam   = 0;
201         req->x        = x;
202         req->y        = y;
203         req->time     = time;
204         req->info     = extra_info;
205         req->timeout  = -1;
206         req->callback = NULL;
207         wine_server_call( req );
208     }
209     SERVER_END_REQ;
210
211 }
212
213
214 /***********************************************************************
215  *              X11DRV_send_mouse_input
216  */
217 void X11DRV_send_mouse_input( HWND hwnd, DWORD flags, DWORD x, DWORD y,
218                               DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
219 {
220     POINT pt;
221
222     if (flags & MOUSEEVENTF_ABSOLUTE)
223     {
224         if (injected_flags & LLMHF_INJECTED)
225         {
226             pt.x = (x * screen_width) >> 16;
227             pt.y = (y * screen_height) >> 16;
228         }
229         else
230         {
231             pt.x = x;
232             pt.y = y;
233         }
234         wine_tsx11_lock();
235         cursor_pos = pt;
236         wine_tsx11_unlock();
237     }
238     else if (flags & MOUSEEVENTF_MOVE)
239     {
240         int accel[3], xMult = 1, yMult = 1;
241
242         /* dx and dy can be negative numbers for relative movements */
243         SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
244
245         if (x > accel[0] && accel[2] != 0)
246         {
247             xMult = 2;
248             if ((x > accel[1]) && (accel[2] == 2)) xMult = 4;
249         }
250         if (y > accel[0] && accel[2] != 0)
251         {
252             yMult = 2;
253             if ((y > accel[1]) && (accel[2] == 2)) yMult = 4;
254         }
255
256         wine_tsx11_lock();
257         pt.x = cursor_pos.x + (long)x * xMult;
258         pt.y = cursor_pos.y + (long)y * yMult;
259
260         /* Clip to the current screen size */
261         if (pt.x < 0) pt.x = 0;
262         else if (pt.x >= screen_width) pt.x = screen_width - 1;
263         if (pt.y < 0) pt.y = 0;
264         else if (pt.y >= screen_height) pt.y = screen_height - 1;
265         cursor_pos = pt;
266         wine_tsx11_unlock();
267     }
268     else
269     {
270         wine_tsx11_lock();
271         pt = cursor_pos;
272         wine_tsx11_unlock();
273     }
274
275     if (flags & MOUSEEVENTF_MOVE)
276     {
277         queue_raw_mouse_message( WM_MOUSEMOVE, hwnd, pt.x, pt.y, data, time,
278                                  extra_info, injected_flags );
279         if (injected_flags & LLMHF_INJECTED)  /* we have to actually move the cursor */
280         {
281             TRACE( "warping to (%ld,%ld)\n", pt.x, pt.y );
282             wine_tsx11_lock();
283             XWarpPointer( thread_display(), root_window, root_window, 0, 0, 0, 0, pt.x, pt.y );
284             wine_tsx11_unlock();
285         }
286     }
287     if (flags & MOUSEEVENTF_LEFTDOWN)
288     {
289         key_state_table[VK_LBUTTON] |= 0xc0;
290         queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
291                                  hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
292     }
293     if (flags & MOUSEEVENTF_LEFTUP)
294     {
295         key_state_table[VK_LBUTTON] &= ~0x80;
296         queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONUP : WM_LBUTTONUP,
297                                  hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
298     }
299     if (flags & MOUSEEVENTF_RIGHTDOWN)
300     {
301         key_state_table[VK_RBUTTON] |= 0xc0;
302         queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
303                                  hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
304     }
305     if (flags & MOUSEEVENTF_RIGHTUP)
306     {
307         key_state_table[VK_RBUTTON] &= ~0x80;
308         queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONUP : WM_RBUTTONUP,
309                                  hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
310     }
311     if (flags & MOUSEEVENTF_MIDDLEDOWN)
312     {
313         key_state_table[VK_MBUTTON] |= 0xc0;
314         queue_raw_mouse_message( WM_MBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
315                                  extra_info, injected_flags );
316     }
317     if (flags & MOUSEEVENTF_MIDDLEUP)
318     {
319         key_state_table[VK_MBUTTON] &= ~0x80;
320         queue_raw_mouse_message( WM_MBUTTONUP, hwnd, pt.x, pt.y, data, time,
321                                  extra_info, injected_flags );
322     }
323     if (flags & MOUSEEVENTF_WHEEL)
324     {
325         queue_raw_mouse_message( WM_MOUSEWHEEL, hwnd, pt.x, pt.y, data, time,
326                                  extra_info, injected_flags );
327     }
328 }
329
330
331 /***********************************************************************
332  *              create_cursor
333  *
334  * Create an X cursor from a Windows one.
335  */
336 static Cursor create_cursor( Display *display, CURSORICONINFO *ptr )
337 {
338     Pixmap pixmapBits, pixmapMask, pixmapMaskInv, pixmapAll;
339     XColor fg, bg;
340     Cursor cursor = None;
341
342     if (!ptr)  /* Create an empty cursor */
343     {
344         static const char data[] = { 0 };
345
346         bg.red = bg.green = bg.blue = 0x0000;
347         pixmapBits = XCreateBitmapFromData( display, root_window, data, 1, 1 );
348         if (pixmapBits)
349         {
350             cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
351                                           &bg, &bg, 0, 0 );
352             XFreePixmap( display, pixmapBits );
353         }
354     }
355     else  /* Create the X cursor from the bits */
356     {
357         XImage *image;
358         GC gc;
359
360         TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
361             ptr->nWidth, ptr->nHeight, ptr->bPlanes, ptr->bBitsPerPixel,
362             ptr->nWidthBytes);
363         /* Create a pixmap and transfer all the bits to it */
364
365         /* NOTE: Following hack works, but only because XFree depth
366          *       1 images really use 1 bit/pixel (and so the same layout
367          *       as the Windows cursor data). Perhaps use a more generic
368          *       algorithm here.
369          */
370         /* This pixmap will be written with two bitmaps. The first is
371          *  the mask and the second is the image.
372          */
373         if (!(pixmapAll = XCreatePixmap( display, root_window,
374                   ptr->nWidth, ptr->nHeight * 2, 1 )))
375             return 0;
376         if (!(image = XCreateImage( display, visual,
377                 1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
378                 ptr->nHeight * 2, 16, ptr->nWidthBytes/ptr->bBitsPerPixel)))
379         {
380             XFreePixmap( display, pixmapAll );
381             return 0;
382         }
383         gc = XCreateGC( display, pixmapAll, 0, NULL );
384         XSetGraphicsExposures( display, gc, False );
385         image->byte_order = MSBFirst;
386         image->bitmap_bit_order = MSBFirst;
387         image->bitmap_unit = 16;
388         _XInitImageFuncPtrs(image);
389         if (ptr->bPlanes * ptr->bBitsPerPixel == 1)
390         {
391             /* A plain old white on black cursor. */
392             fg.red = fg.green = fg.blue = 0xffff;
393             bg.red = bg.green = bg.blue = 0x0000;
394             XPutImage( display, pixmapAll, gc, image,
395                 0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
396         }
397         else
398         {
399             int     rbits, gbits, bbits, red, green, blue;
400             int     rfg, gfg, bfg, rbg, gbg, bbg;
401             int     rscale, gscale, bscale;
402             int     x, y, xmax, ymax, bitIndex, byteIndex, xorIndex;
403             unsigned char *theMask, *theImage, theChar;
404             int     threshold, fgBits, bgBits, bitShifted;
405             BYTE    pXorBits[128];   /* Up to 32x32 icons */
406
407             switch (ptr->bBitsPerPixel)
408             {
409             case 24:
410                 rbits = 8;
411                 gbits = 8;
412                 bbits = 8;
413                 threshold = 0x40;
414                 break;
415             case 16:
416                 rbits = 5;
417                 gbits = 6;
418                 bbits = 5;
419                 threshold = 0x40;
420                 break;
421             default:
422                 FIXME("Currently no support for cursors with %d bits per pixel\n",
423                   ptr->bBitsPerPixel);
424                 XFreePixmap( display, pixmapAll );
425                 XFreeGC( display, gc );
426                 image->data = NULL;
427                 XDestroyImage( image );
428                 return 0;
429             }
430             /* The location of the mask. */
431             theMask = (char *)(ptr + 1);
432             /* The mask should still be 1 bit per pixel. The color image
433              * should immediately follow the mask.
434              */
435             theImage = &theMask[ptr->nWidth/8 * ptr->nHeight];
436             rfg = gfg = bfg = rbg = gbg = bbg = 0;
437             bitIndex = 0;
438             byteIndex = 0;
439             xorIndex = 0;
440             fgBits = 0;
441             bitShifted = 0x01;
442             xmax = (ptr->nWidth > 32) ? 32 : ptr->nWidth;
443             if (ptr->nWidth > 32) {
444                 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
445                   ptr->nWidth, ptr->nHeight);
446             }
447             ymax = (ptr->nHeight > 32) ? 32 : ptr->nHeight;
448
449             memset(pXorBits, 0, 128);
450             for (y=0; y<ymax; y++)
451             {
452                 for (x=0; x<xmax; x++)
453                 {
454                         red = green = blue = 0;
455                         switch (ptr->bBitsPerPixel)
456                         {
457                         case 24:
458                             theChar = theImage[byteIndex++];
459                             blue = theChar;
460                             theChar = theImage[byteIndex++];
461                             green = theChar;
462                             theChar = theImage[byteIndex++];
463                             red = theChar;
464                             break;
465                         case 16:
466                             theChar = theImage[byteIndex++];
467                             blue = theChar & 0x1F;
468                             green = (theChar & 0xE0) >> 5;
469                             theChar = theImage[byteIndex++];
470                             green |= (theChar & 0x07) << 3;
471                             red = (theChar & 0xF8) >> 3;
472                             break;
473                         }
474
475                     if (red+green+blue > threshold)
476                     {
477                         rfg += red;
478                         gfg += green;
479                         bfg += blue;
480                         fgBits++;
481                         pXorBits[xorIndex] |= bitShifted;
482                     }
483                     else
484                     {
485                         rbg += red;
486                         gbg += green;
487                         bbg += blue;
488                     }
489                     if (x%8 == 7)
490                     {
491                         bitShifted = 0x01;
492                         xorIndex++;
493                     }
494                     else
495                         bitShifted = bitShifted << 1;
496                 }
497             }
498             rscale = 1 << (16 - rbits);
499             gscale = 1 << (16 - gbits);
500             bscale = 1 << (16 - bbits);
501             if (fgBits)
502             {
503                 fg.red   = rfg * rscale / fgBits;
504                 fg.green = gfg * gscale / fgBits;
505                 fg.blue  = bfg * bscale / fgBits;
506             }
507             else fg.red = fg.green = fg.blue = 0;
508             bgBits = xmax * ymax - fgBits;
509             if (bgBits)
510             {
511                 bg.red   = rbg * rscale / bgBits;
512                 bg.green = gbg * gscale / bgBits;
513                 bg.blue  = bbg * bscale / bgBits;
514             }
515             else bg.red = bg.green = bg.blue = 0;
516             pixmapBits = XCreateBitmapFromData( display, root_window, pXorBits, xmax, ymax );
517             if (!pixmapBits)
518             {
519                 XFreePixmap( display, pixmapAll );
520                 XFreeGC( display, gc );
521                 image->data = NULL;
522                 XDestroyImage( image );
523                 return 0;
524             }
525
526             /* Put the mask. */
527             XPutImage( display, pixmapAll, gc, image,
528                    0, 0, 0, 0, ptr->nWidth, ptr->nHeight );
529             XSetFunction( display, gc, GXcopy );
530             /* Put the image */
531             XCopyArea( display, pixmapBits, pixmapAll, gc,
532                        0, 0, xmax, ymax, 0, ptr->nHeight );
533             XFreePixmap( display, pixmapBits );
534         }
535         image->data = NULL;
536         XDestroyImage( image );
537
538         /* Now create the 2 pixmaps for bits and mask */
539
540         pixmapBits = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
541         pixmapMask = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
542         pixmapMaskInv = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
543
544         /* Make sure everything went OK so far */
545
546         if (pixmapBits && pixmapMask && pixmapMaskInv)
547         {
548             POINT hotspot;
549
550             /* We have to do some magic here, as cursors are not fully
551              * compatible between Windows and X11. Under X11, there
552              * are only 3 possible color cursor: black, white and
553              * masked. So we map the 4th Windows color (invert the
554              * bits on the screen) to black and an additional white bit on
555              * an other place (+1,+1). This require some boolean arithmetic:
556              *
557              *         Windows          |          X11
558              * And    Xor      Result   |   Bits     Mask     Result
559              *  0      0     black      |    0        1     background
560              *  0      1     white      |    1        1     foreground
561              *  1      0     no change  |    X        0     no change
562              *  1      1     inverted   |    0        1     background
563              *
564              * which gives:
565              *  Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
566              *  Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
567              *
568              * FIXME: apparently some servers do support 'inverted' color.
569              * I don't know if it's correct per the X spec, but maybe
570              * we ought to take advantage of it.  -- AJ
571              */
572             XSetFunction( display, gc, GXcopy );
573             XCopyArea( display, pixmapAll, pixmapBits, gc,
574                        0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
575             XCopyArea( display, pixmapAll, pixmapMask, gc,
576                        0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
577             XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
578                        0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
579             XSetFunction( display, gc, GXand );
580             XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
581                        0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
582             XSetFunction( display, gc, GXandReverse );
583             XCopyArea( display, pixmapAll, pixmapBits, gc,
584                        0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
585             XSetFunction( display, gc, GXorReverse );
586             XCopyArea( display, pixmapAll, pixmapMask, gc,
587                        0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
588             /* Additional white */
589             XSetFunction( display, gc, GXor );
590             XCopyArea( display, pixmapMaskInv, pixmapMask, gc,
591                        0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
592             XCopyArea( display, pixmapMaskInv, pixmapBits, gc,
593                        0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
594             XSetFunction( display, gc, GXcopy );
595
596             /* Make sure hotspot is valid */
597             hotspot.x = ptr->ptHotSpot.x;
598             hotspot.y = ptr->ptHotSpot.y;
599             if (hotspot.x < 0 || hotspot.x >= ptr->nWidth ||
600                 hotspot.y < 0 || hotspot.y >= ptr->nHeight)
601             {
602                 hotspot.x = ptr->nWidth / 2;
603                 hotspot.y = ptr->nHeight / 2;
604             }
605             cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
606                                           &fg, &bg, hotspot.x, hotspot.y );
607         }
608
609         /* Now free everything */
610
611         if (pixmapAll) XFreePixmap( display, pixmapAll );
612         if (pixmapBits) XFreePixmap( display, pixmapBits );
613         if (pixmapMask) XFreePixmap( display, pixmapMask );
614         if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
615         XFreeGC( display, gc );
616     }
617     return cursor;
618 }
619
620
621 /***********************************************************************
622  *              SetCursor (X11DRV.@)
623  */
624 void X11DRV_SetCursor( CURSORICONINFO *lpCursor )
625 {
626     Cursor cursor;
627
628     if (root_window != DefaultRootWindow(gdi_display))
629     {
630         /* If in desktop mode, set the cursor on the desktop window */
631
632         wine_tsx11_lock();
633         cursor = create_cursor( gdi_display, lpCursor );
634         if (cursor)
635         {
636             XDefineCursor( gdi_display, root_window, cursor );
637             /* Make the change take effect immediately */
638             XFlush(gdi_display);
639             XFreeCursor( gdi_display, cursor );
640         }
641         wine_tsx11_unlock();
642     }
643     else /* set the same cursor for all top-level windows of the current thread */
644     {
645         struct x11drv_thread_data *data = x11drv_thread_data();
646
647         wine_tsx11_lock();
648         cursor = create_cursor( data->display, lpCursor );
649         if (cursor)
650         {
651             if (data->cursor) XFreeCursor( data->display, data->cursor );
652             data->cursor = cursor;
653             if (data->cursor_window)
654             {
655                 XDefineCursor( data->display, data->cursor_window, cursor );
656                 /* Make the change take effect immediately */
657                 XFlush( data->display );
658             }
659         }
660         wine_tsx11_unlock();
661     }
662 }
663
664 /***********************************************************************
665  *              SetCursorPos (X11DRV.@)
666  */
667 BOOL X11DRV_SetCursorPos( INT x, INT y )
668 {
669     Display *display = thread_display();
670
671     TRACE( "warping to (%d,%d)\n", x, y );
672
673     wine_tsx11_lock();
674     XWarpPointer( display, root_window, root_window, 0, 0, 0, 0, x, y );
675     cursor_pos.x = x;
676     cursor_pos.y = y;
677     wine_tsx11_unlock();
678     return TRUE;
679 }
680
681 /***********************************************************************
682  *              GetCursorPos (X11DRV.@)
683  */
684 BOOL X11DRV_GetCursorPos(LPPOINT pos)
685 {
686     Display *display = thread_display();
687     Window root, child;
688     int rootX, rootY, winX, winY;
689     unsigned int xstate;
690
691     wine_tsx11_lock();
692     if (XQueryPointer( display, root_window, &root, &child,
693                        &rootX, &rootY, &winX, &winY, &xstate ))
694     {
695         update_key_state( xstate );
696         update_button_state( xstate );
697         TRACE("pointer at (%d,%d)\n", winX, winY );
698         cursor_pos.x = winX;
699         cursor_pos.y = winY;
700     }
701     *pos = cursor_pos;
702     wine_tsx11_unlock();
703     return TRUE;
704 }
705
706 /***********************************************************************
707  *           X11DRV_ButtonPress
708  */
709 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
710 {
711     XButtonEvent *event = &xev->xbutton;
712     int buttonNum = event->button - 1;
713     WORD wData = 0;
714     POINT pt;
715
716     if (buttonNum >= NB_BUTTONS) return;
717     if (!hwnd) return;
718
719     switch (buttonNum)
720     {
721     case 3:
722         wData = WHEEL_DELTA;
723         break;
724     case 4:
725         wData = -WHEEL_DELTA;
726         break;
727     }
728
729     update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
730
731     X11DRV_send_mouse_input( hwnd, button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE,
732                              pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
733 }
734
735
736 /***********************************************************************
737  *           X11DRV_ButtonRelease
738  */
739 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
740 {
741     XButtonEvent *event = &xev->xbutton;
742     int buttonNum = event->button - 1;
743     POINT pt;
744
745     if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
746     if (!hwnd) return;
747
748     update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
749
750     X11DRV_send_mouse_input( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE,
751                              pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
752 }
753
754
755 /***********************************************************************
756  *           X11DRV_MotionNotify
757  */
758 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
759 {
760     XMotionEvent *event = &xev->xmotion;
761     POINT pt;
762
763     TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);
764
765     if (!hwnd) return;
766
767     update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
768
769     X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
770                              pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
771 }
772
773
774 /***********************************************************************
775  *           X11DRV_EnterNotify
776  */
777 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
778 {
779     XCrossingEvent *event = &xev->xcrossing;
780     POINT pt;
781
782     TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);
783
784     if (!hwnd) return;
785     if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
786
787     /* simulate a mouse motion event */
788     update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
789
790     X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
791                              pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
792 }
793
794
795 #ifdef HAVE_LIBXXF86DGA2
796
797 extern HWND DGAhwnd;
798
799 /**********************************************************************
800  *              X11DRV_DGAMotionEvent
801  */
802 void X11DRV_DGAMotionEvent( HWND hwnd, XEvent *xev )
803 {
804     XDGAMotionEvent *event = (XDGAMotionEvent *)xev;
805     update_key_state( event->state );
806     X11DRV_send_mouse_input( DGAhwnd, MOUSEEVENTF_MOVE, event->dx, event->dy,
807                              0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
808 }
809
810 /**********************************************************************
811  *              X11DRV_DGAButtonPressEvent
812  */
813 void X11DRV_DGAButtonPressEvent( HWND hwnd, XEvent *xev )
814 {
815     XDGAButtonEvent *event = (XDGAButtonEvent *)xev;
816     int buttonNum = event->button - 1;
817
818     if (buttonNum >= NB_BUTTONS) return;
819     update_key_state( event->state );
820     X11DRV_send_mouse_input( DGAhwnd, button_down_flags[buttonNum], 0, 0,
821                              0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
822 }
823
824 /**********************************************************************
825  *              X11DRV_DGAButtonReleaseEvent
826  */
827 void X11DRV_DGAButtonReleaseEvent( HWND hwnd, XEvent *xev )
828 {
829     XDGAButtonEvent *event = (XDGAButtonEvent *)xev;
830     int buttonNum = event->button - 1;
831
832     if (buttonNum >= NB_BUTTONS) return;
833     update_key_state( event->state );
834     X11DRV_send_mouse_input( DGAhwnd, button_up_flags[buttonNum], 0, 0,
835                              0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
836 }
837
838 #endif /* HAVE_LIBXXF86DGA2 */