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