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