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