Release 0.9.33.
[wine/dibdrv.git] / dlls / winex11.drv / mouse.c
blob1016c6971278fbbde8b917677bc609f0598057bf
1 /*
2 * X11 mouse driver
4 * Copyright 1998 Ulrich Weigand
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.
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.
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
21 #include "config.h"
23 #include <X11/Xlib.h>
24 #include <stdarg.h>
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wine/winuser16.h"
32 #include "win.h"
33 #include "x11drv.h"
34 #include "wine/server.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
39 /**********************************************************************/
41 #ifndef Button6Mask
42 #define Button6Mask (1<<13)
43 #endif
44 #ifndef Button7Mask
45 #define Button7Mask (1<<14)
46 #endif
48 #define NB_BUTTONS 7 /* Windows can handle 5 buttons and the wheel too */
50 static const UINT button_down_flags[NB_BUTTONS] =
52 MOUSEEVENTF_LEFTDOWN,
53 MOUSEEVENTF_MIDDLEDOWN,
54 MOUSEEVENTF_RIGHTDOWN,
55 MOUSEEVENTF_WHEEL,
56 MOUSEEVENTF_WHEEL,
57 MOUSEEVENTF_XDOWN,
58 MOUSEEVENTF_XDOWN
61 static const UINT button_up_flags[NB_BUTTONS] =
63 MOUSEEVENTF_LEFTUP,
64 MOUSEEVENTF_MIDDLEUP,
65 MOUSEEVENTF_RIGHTUP,
68 MOUSEEVENTF_XUP,
69 MOUSEEVENTF_XUP
72 POINT cursor_pos;
73 static DWORD last_time_modified;
74 static RECT cursor_clip; /* Cursor clipping rect */
76 BOOL X11DRV_SetCursorPos( INT x, INT y );
78 /***********************************************************************
79 * get_coords
81 * get the coordinates of a mouse event
83 static inline void get_coords( HWND hwnd, int x, int y, POINT *pt )
85 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
87 if (!data) return;
89 pt->x = x + data->whole_rect.left;
90 pt->y = y + data->whole_rect.top;
93 /***********************************************************************
94 * clip_point_to_rect
96 * Clip point to the provided rectangle
98 static inline void clip_point_to_rect( LPCRECT rect, LPPOINT pt )
100 if (pt->x < rect->left) pt->x = rect->left;
101 else if (pt->x >= rect->right) pt->x = rect->right - 1;
102 if (pt->y < rect->top) pt->y = rect->top;
103 else if (pt->y >= rect->bottom) pt->y = rect->bottom - 1;
106 /***********************************************************************
107 * update_button_state
109 * Update the button state with what X provides us
111 static inline void update_button_state( unsigned int state )
113 key_state_table[VK_LBUTTON] = (state & Button1Mask ? 0x80 : 0);
114 key_state_table[VK_MBUTTON] = (state & Button2Mask ? 0x80 : 0);
115 key_state_table[VK_RBUTTON] = (state & Button3Mask ? 0x80 : 0);
116 /* X-buttons are not reported from XQueryPointer */
120 /***********************************************************************
121 * update_mouse_state
123 * Update the various window states on a mouse event.
125 static void update_mouse_state( HWND hwnd, Window window, int x, int y, unsigned int state, POINT *pt )
127 struct x11drv_thread_data *data = x11drv_thread_data();
129 if (window == root_window)
131 x += virtual_screen_rect.left;
132 y += virtual_screen_rect.top;
134 get_coords( hwnd, x, y, pt );
136 /* update the cursor */
138 if (data->cursor_window != window)
140 data->cursor_window = window;
141 wine_tsx11_lock();
142 if (data->cursor) XDefineCursor( data->display, window, data->cursor );
143 wine_tsx11_unlock();
146 /* update the wine server Z-order */
148 if (window != data->grab_window &&
149 /* ignore event if a button is pressed, since the mouse is then grabbed too */
150 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
152 SERVER_START_REQ( update_window_zorder )
154 req->window = hwnd;
155 req->rect.left = pt->x;
156 req->rect.top = pt->y;
157 req->rect.right = pt->x + 1;
158 req->rect.bottom = pt->y + 1;
159 wine_server_call( req );
161 SERVER_END_REQ;
166 /***********************************************************************
167 * get_key_state
169 static WORD get_key_state(void)
171 WORD ret = 0;
173 if (GetSystemMetrics( SM_SWAPBUTTON ))
175 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
176 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
178 else
180 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
181 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
183 if (key_state_table[VK_MBUTTON] & 0x80) ret |= MK_MBUTTON;
184 if (key_state_table[VK_SHIFT] & 0x80) ret |= MK_SHIFT;
185 if (key_state_table[VK_CONTROL] & 0x80) ret |= MK_CONTROL;
186 if (key_state_table[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
187 if (key_state_table[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
188 return ret;
192 /***********************************************************************
193 * queue_raw_mouse_message
195 static void queue_raw_mouse_message( UINT message, HWND hwnd, DWORD x, DWORD y,
196 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
198 MSLLHOOKSTRUCT hook;
200 hook.pt.x = x;
201 hook.pt.y = y;
202 hook.mouseData = MAKELONG( 0, data );
203 hook.flags = injected_flags;
204 hook.time = time;
205 hook.dwExtraInfo = extra_info;
207 last_time_modified = GetTickCount();
208 if (HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE )) return;
210 SERVER_START_REQ( send_hardware_message )
212 req->id = (injected_flags & LLMHF_INJECTED) ? 0 : GetCurrentThreadId();
213 req->win = hwnd;
214 req->msg = message;
215 req->wparam = MAKEWPARAM( get_key_state(), data );
216 req->lparam = 0;
217 req->x = x;
218 req->y = y;
219 req->time = time;
220 req->info = extra_info;
221 wine_server_call( req );
223 SERVER_END_REQ;
228 /***********************************************************************
229 * X11DRV_send_mouse_input
231 void X11DRV_send_mouse_input( HWND hwnd, DWORD flags, DWORD x, DWORD y,
232 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
234 POINT pt;
236 if (flags & MOUSEEVENTF_MOVE && flags & MOUSEEVENTF_ABSOLUTE)
238 if (injected_flags & LLMHF_INJECTED)
240 pt.x = (x * screen_width) >> 16;
241 pt.y = (y * screen_height) >> 16;
243 else
245 pt.x = x;
246 pt.y = y;
247 wine_tsx11_lock();
248 if (cursor_pos.x == x && cursor_pos.y == y) flags &= ~MOUSEEVENTF_MOVE;
249 wine_tsx11_unlock();
252 else if (flags & MOUSEEVENTF_MOVE)
254 int accel[3], xMult = 1, yMult = 1;
256 /* dx and dy can be negative numbers for relative movements */
257 SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
259 if (abs(x) > accel[0] && accel[2] != 0)
261 xMult = 2;
262 if ((abs(x) > accel[1]) && (accel[2] == 2)) xMult = 4;
264 if (abs(y) > accel[0] && accel[2] != 0)
266 yMult = 2;
267 if ((abs(y) > accel[1]) && (accel[2] == 2)) yMult = 4;
270 wine_tsx11_lock();
271 pt.x = cursor_pos.x + (long)x * xMult;
272 pt.y = cursor_pos.y + (long)y * yMult;
273 wine_tsx11_unlock();
275 else
277 wine_tsx11_lock();
278 pt = cursor_pos;
279 wine_tsx11_unlock();
282 if (flags & MOUSEEVENTF_MOVE)
284 queue_raw_mouse_message( WM_MOUSEMOVE, hwnd, pt.x, pt.y, data, time,
285 extra_info, injected_flags );
286 if ((injected_flags & LLMHF_INJECTED) &&
287 ((flags & MOUSEEVENTF_ABSOLUTE) || x || y)) /* we have to actually move the cursor */
289 X11DRV_SetCursorPos( pt.x, pt.y );
291 else
293 wine_tsx11_lock();
294 clip_point_to_rect( &cursor_clip, &pt);
295 cursor_pos = pt;
296 wine_tsx11_unlock();
299 if (flags & MOUSEEVENTF_LEFTDOWN)
301 key_state_table[VK_LBUTTON] |= 0xc0;
302 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
303 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
305 if (flags & MOUSEEVENTF_LEFTUP)
307 key_state_table[VK_LBUTTON] &= ~0x80;
308 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONUP : WM_LBUTTONUP,
309 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
311 if (flags & MOUSEEVENTF_RIGHTDOWN)
313 key_state_table[VK_RBUTTON] |= 0xc0;
314 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
315 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
317 if (flags & MOUSEEVENTF_RIGHTUP)
319 key_state_table[VK_RBUTTON] &= ~0x80;
320 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONUP : WM_RBUTTONUP,
321 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
323 if (flags & MOUSEEVENTF_MIDDLEDOWN)
325 key_state_table[VK_MBUTTON] |= 0xc0;
326 queue_raw_mouse_message( WM_MBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
327 extra_info, injected_flags );
329 if (flags & MOUSEEVENTF_MIDDLEUP)
331 key_state_table[VK_MBUTTON] &= ~0x80;
332 queue_raw_mouse_message( WM_MBUTTONUP, hwnd, pt.x, pt.y, data, time,
333 extra_info, injected_flags );
335 if (flags & MOUSEEVENTF_WHEEL)
337 queue_raw_mouse_message( WM_MOUSEWHEEL, hwnd, pt.x, pt.y, data, time,
338 extra_info, injected_flags );
340 if (flags & MOUSEEVENTF_XDOWN)
342 key_state_table[VK_XBUTTON1 + data - 1] |= 0xc0;
343 queue_raw_mouse_message( WM_XBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
344 extra_info, injected_flags );
346 if (flags & MOUSEEVENTF_XUP)
348 key_state_table[VK_XBUTTON1 + data - 1] &= ~0x80;
349 queue_raw_mouse_message( WM_XBUTTONUP, hwnd, pt.x, pt.y, data, time,
350 extra_info, injected_flags );
355 /***********************************************************************
356 * create_cursor
358 * Create an X cursor from a Windows one.
360 static Cursor create_cursor( Display *display, CURSORICONINFO *ptr )
362 Pixmap pixmapBits, pixmapMask, pixmapMaskInv, pixmapAll;
363 XColor fg, bg;
364 Cursor cursor = None;
366 if (!ptr) /* Create an empty cursor */
368 static const char data[] = { 0 };
370 bg.red = bg.green = bg.blue = 0x0000;
371 pixmapBits = XCreateBitmapFromData( display, root_window, data, 1, 1 );
372 if (pixmapBits)
374 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
375 &bg, &bg, 0, 0 );
376 XFreePixmap( display, pixmapBits );
379 else /* Create the X cursor from the bits */
381 XImage *image;
382 GC gc;
384 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
385 ptr->nWidth, ptr->nHeight, ptr->bPlanes, ptr->bBitsPerPixel,
386 ptr->nWidthBytes);
387 /* Create a pixmap and transfer all the bits to it */
389 /* NOTE: Following hack works, but only because XFree depth
390 * 1 images really use 1 bit/pixel (and so the same layout
391 * as the Windows cursor data). Perhaps use a more generic
392 * algorithm here.
394 /* This pixmap will be written with two bitmaps. The first is
395 * the mask and the second is the image.
397 if (!(pixmapAll = XCreatePixmap( display, root_window,
398 ptr->nWidth, ptr->nHeight * 2, 1 )))
399 return 0;
400 if (!(image = XCreateImage( display, visual,
401 1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
402 ptr->nHeight * 2, 16, ptr->nWidthBytes/ptr->bBitsPerPixel)))
404 XFreePixmap( display, pixmapAll );
405 return 0;
407 gc = XCreateGC( display, pixmapAll, 0, NULL );
408 XSetGraphicsExposures( display, gc, False );
409 image->byte_order = MSBFirst;
410 image->bitmap_bit_order = MSBFirst;
411 image->bitmap_unit = 16;
412 _XInitImageFuncPtrs(image);
413 if (ptr->bPlanes * ptr->bBitsPerPixel == 1)
415 /* A plain old white on black cursor. */
416 fg.red = fg.green = fg.blue = 0xffff;
417 bg.red = bg.green = bg.blue = 0x0000;
418 XPutImage( display, pixmapAll, gc, image,
419 0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
421 else
423 int rbits, gbits, bbits, red, green, blue;
424 int rfg, gfg, bfg, rbg, gbg, bbg;
425 int rscale, gscale, bscale;
426 int x, y, xmax, ymax, bitIndex, byteIndex, xorIndex;
427 unsigned char *theMask, *theImage, theChar;
428 int threshold, fgBits, bgBits, bitShifted;
429 BYTE pXorBits[128]; /* Up to 32x32 icons */
431 switch (ptr->bBitsPerPixel)
433 case 24:
434 rbits = 8;
435 gbits = 8;
436 bbits = 8;
437 threshold = 0x40;
438 break;
439 case 16:
440 rbits = 5;
441 gbits = 6;
442 bbits = 5;
443 threshold = 0x40;
444 break;
445 default:
446 FIXME("Currently no support for cursors with %d bits per pixel\n",
447 ptr->bBitsPerPixel);
448 XFreePixmap( display, pixmapAll );
449 XFreeGC( display, gc );
450 image->data = NULL;
451 XDestroyImage( image );
452 return 0;
454 /* The location of the mask. */
455 theMask = (unsigned char *)(ptr + 1);
456 /* The mask should still be 1 bit per pixel. The color image
457 * should immediately follow the mask.
459 theImage = &theMask[ptr->nWidth/8 * ptr->nHeight];
460 rfg = gfg = bfg = rbg = gbg = bbg = 0;
461 bitIndex = 0;
462 byteIndex = 0;
463 xorIndex = 0;
464 fgBits = 0;
465 bitShifted = 0x01;
466 xmax = (ptr->nWidth > 32) ? 32 : ptr->nWidth;
467 if (ptr->nWidth > 32) {
468 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
469 ptr->nWidth, ptr->nHeight);
471 ymax = (ptr->nHeight > 32) ? 32 : ptr->nHeight;
473 memset(pXorBits, 0, 128);
474 for (y=0; y<ymax; y++)
476 for (x=0; x<xmax; x++)
478 red = green = blue = 0;
479 switch (ptr->bBitsPerPixel)
481 case 24:
482 theChar = theImage[byteIndex++];
483 blue = theChar;
484 theChar = theImage[byteIndex++];
485 green = theChar;
486 theChar = theImage[byteIndex++];
487 red = theChar;
488 break;
489 case 16:
490 theChar = theImage[byteIndex++];
491 blue = theChar & 0x1F;
492 green = (theChar & 0xE0) >> 5;
493 theChar = theImage[byteIndex++];
494 green |= (theChar & 0x07) << 3;
495 red = (theChar & 0xF8) >> 3;
496 break;
499 if (red+green+blue > threshold)
501 rfg += red;
502 gfg += green;
503 bfg += blue;
504 fgBits++;
505 pXorBits[xorIndex] |= bitShifted;
507 else
509 rbg += red;
510 gbg += green;
511 bbg += blue;
513 if (x%8 == 7)
515 bitShifted = 0x01;
516 xorIndex++;
518 else
519 bitShifted = bitShifted << 1;
522 rscale = 1 << (16 - rbits);
523 gscale = 1 << (16 - gbits);
524 bscale = 1 << (16 - bbits);
525 if (fgBits)
527 fg.red = rfg * rscale / fgBits;
528 fg.green = gfg * gscale / fgBits;
529 fg.blue = bfg * bscale / fgBits;
531 else fg.red = fg.green = fg.blue = 0;
532 bgBits = xmax * ymax - fgBits;
533 if (bgBits)
535 bg.red = rbg * rscale / bgBits;
536 bg.green = gbg * gscale / bgBits;
537 bg.blue = bbg * bscale / bgBits;
539 else bg.red = bg.green = bg.blue = 0;
540 pixmapBits = XCreateBitmapFromData( display, root_window, (char *)pXorBits, xmax, ymax );
541 if (!pixmapBits)
543 XFreePixmap( display, pixmapAll );
544 XFreeGC( display, gc );
545 image->data = NULL;
546 XDestroyImage( image );
547 return 0;
550 /* Put the mask. */
551 XPutImage( display, pixmapAll, gc, image,
552 0, 0, 0, 0, ptr->nWidth, ptr->nHeight );
553 XSetFunction( display, gc, GXcopy );
554 /* Put the image */
555 XCopyArea( display, pixmapBits, pixmapAll, gc,
556 0, 0, xmax, ymax, 0, ptr->nHeight );
557 XFreePixmap( display, pixmapBits );
559 image->data = NULL;
560 XDestroyImage( image );
562 /* Now create the 2 pixmaps for bits and mask */
564 pixmapBits = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
565 pixmapMask = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
566 pixmapMaskInv = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
568 /* Make sure everything went OK so far */
570 if (pixmapBits && pixmapMask && pixmapMaskInv)
572 POINT hotspot;
574 /* We have to do some magic here, as cursors are not fully
575 * compatible between Windows and X11. Under X11, there
576 * are only 3 possible color cursor: black, white and
577 * masked. So we map the 4th Windows color (invert the
578 * bits on the screen) to black and an additional white bit on
579 * an other place (+1,+1). This require some boolean arithmetic:
581 * Windows | X11
582 * And Xor Result | Bits Mask Result
583 * 0 0 black | 0 1 background
584 * 0 1 white | 1 1 foreground
585 * 1 0 no change | X 0 no change
586 * 1 1 inverted | 0 1 background
588 * which gives:
589 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
590 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
592 * FIXME: apparently some servers do support 'inverted' color.
593 * I don't know if it's correct per the X spec, but maybe
594 * we ought to take advantage of it. -- AJ
596 XSetFunction( display, gc, GXcopy );
597 XCopyArea( display, pixmapAll, pixmapBits, gc,
598 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
599 XCopyArea( display, pixmapAll, pixmapMask, gc,
600 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
601 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
602 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
603 XSetFunction( display, gc, GXand );
604 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
605 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
606 XSetFunction( display, gc, GXandReverse );
607 XCopyArea( display, pixmapAll, pixmapBits, gc,
608 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
609 XSetFunction( display, gc, GXorReverse );
610 XCopyArea( display, pixmapAll, pixmapMask, gc,
611 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
612 /* Additional white */
613 XSetFunction( display, gc, GXor );
614 XCopyArea( display, pixmapMaskInv, pixmapMask, gc,
615 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
616 XCopyArea( display, pixmapMaskInv, pixmapBits, gc,
617 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
618 XSetFunction( display, gc, GXcopy );
620 /* Make sure hotspot is valid */
621 hotspot.x = ptr->ptHotSpot.x;
622 hotspot.y = ptr->ptHotSpot.y;
623 if (hotspot.x < 0 || hotspot.x >= ptr->nWidth ||
624 hotspot.y < 0 || hotspot.y >= ptr->nHeight)
626 hotspot.x = ptr->nWidth / 2;
627 hotspot.y = ptr->nHeight / 2;
629 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
630 &fg, &bg, hotspot.x, hotspot.y );
633 /* Now free everything */
635 if (pixmapAll) XFreePixmap( display, pixmapAll );
636 if (pixmapBits) XFreePixmap( display, pixmapBits );
637 if (pixmapMask) XFreePixmap( display, pixmapMask );
638 if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
639 XFreeGC( display, gc );
641 return cursor;
645 /***********************************************************************
646 * SetCursor (X11DRV.@)
648 void X11DRV_SetCursor( CURSORICONINFO *lpCursor )
650 Cursor cursor;
652 if (lpCursor)
653 TRACE("%ux%u, planes %u, bpp %u\n",
654 lpCursor->nWidth, lpCursor->nHeight, lpCursor->bPlanes, lpCursor->bBitsPerPixel);
655 else
656 TRACE("NULL\n");
658 if (root_window != DefaultRootWindow(gdi_display))
660 /* If in desktop mode, set the cursor on the desktop window */
662 wine_tsx11_lock();
663 cursor = create_cursor( gdi_display, lpCursor );
664 if (cursor)
666 XDefineCursor( gdi_display, root_window, cursor );
667 /* Make the change take effect immediately */
668 XFlush(gdi_display);
669 XFreeCursor( gdi_display, cursor );
671 wine_tsx11_unlock();
673 else /* set the same cursor for all top-level windows of the current thread */
675 struct x11drv_thread_data *data = x11drv_thread_data();
677 wine_tsx11_lock();
678 cursor = create_cursor( data->display, lpCursor );
679 if (cursor)
681 if (data->cursor) XFreeCursor( data->display, data->cursor );
682 data->cursor = cursor;
683 if (data->cursor_window)
685 XDefineCursor( data->display, data->cursor_window, cursor );
686 /* Make the change take effect immediately */
687 XFlush( data->display );
690 wine_tsx11_unlock();
694 /***********************************************************************
695 * SetCursorPos (X11DRV.@)
697 BOOL X11DRV_SetCursorPos( INT x, INT y )
699 Display *display = thread_display();
700 POINT pt;
702 TRACE( "warping to (%d,%d)\n", x, y );
704 wine_tsx11_lock();
705 if (cursor_pos.x == x && cursor_pos.y == y)
707 wine_tsx11_unlock();
708 /* We still need to generate WM_MOUSEMOVE */
709 queue_raw_mouse_message( WM_MOUSEMOVE, NULL, x, y, 0, GetCurrentTime(), 0, 0 );
710 return TRUE;
713 pt.x = x; pt.y = y;
714 clip_point_to_rect( &cursor_clip, &pt);
715 XWarpPointer( display, root_window, root_window, 0, 0, 0, 0,
716 pt.x - virtual_screen_rect.left, pt.y - virtual_screen_rect.top );
717 XFlush( display ); /* avoids bad mouse lag in games that do their own mouse warping */
718 cursor_pos = pt;
719 wine_tsx11_unlock();
720 return TRUE;
723 /***********************************************************************
724 * GetCursorPos (X11DRV.@)
726 BOOL X11DRV_GetCursorPos(LPPOINT pos)
728 Display *display = thread_display();
729 Window root, child;
730 int rootX, rootY, winX, winY;
731 unsigned int xstate;
733 wine_tsx11_lock();
734 if ((GetTickCount() - last_time_modified > 100) &&
735 XQueryPointer( display, root_window, &root, &child,
736 &rootX, &rootY, &winX, &winY, &xstate ))
738 update_button_state( xstate );
739 winX += virtual_screen_rect.left;
740 winY += virtual_screen_rect.top;
741 TRACE("pointer at (%d,%d)\n", winX, winY );
742 cursor_pos.x = winX;
743 cursor_pos.y = winY;
745 *pos = cursor_pos;
746 wine_tsx11_unlock();
747 return TRUE;
751 /***********************************************************************
752 * ClipCursor (X11DRV.@)
754 * Set the cursor clipping rectangle.
756 BOOL X11DRV_ClipCursor( LPCRECT clip )
758 if (!IntersectRect( &cursor_clip, &virtual_screen_rect, clip ))
759 cursor_clip = virtual_screen_rect;
761 return TRUE;
764 /***********************************************************************
765 * X11DRV_ButtonPress
767 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
769 XButtonEvent *event = &xev->xbutton;
770 int buttonNum = event->button - 1;
771 WORD wData = 0;
772 POINT pt;
774 if (buttonNum >= NB_BUTTONS) return;
775 if (!hwnd) return;
777 switch (buttonNum)
779 case 3:
780 wData = WHEEL_DELTA;
781 break;
782 case 4:
783 wData = -WHEEL_DELTA;
784 break;
785 case 5:
786 wData = XBUTTON1;
787 break;
788 case 6:
789 wData = XBUTTON2;
790 break;
793 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
795 X11DRV_send_mouse_input( hwnd, button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
796 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
800 /***********************************************************************
801 * X11DRV_ButtonRelease
803 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
805 XButtonEvent *event = &xev->xbutton;
806 int buttonNum = event->button - 1;
807 WORD wData = 0;
808 POINT pt;
810 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
811 if (!hwnd) return;
813 switch (buttonNum)
815 case 5:
816 wData = XBUTTON1;
817 break;
818 case 6:
819 wData = XBUTTON2;
820 break;
823 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
825 X11DRV_send_mouse_input( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
826 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
830 /***********************************************************************
831 * X11DRV_MotionNotify
833 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
835 XMotionEvent *event = &xev->xmotion;
836 POINT pt;
838 TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);
840 if (!hwnd) return;
842 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
844 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
845 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
849 /***********************************************************************
850 * X11DRV_EnterNotify
852 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
854 XCrossingEvent *event = &xev->xcrossing;
855 POINT pt;
857 TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);
859 if (!hwnd) return;
860 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
862 /* simulate a mouse motion event */
863 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
865 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
866 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );