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