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