comctl32: Never release state image list.
[wine/multimedia.git] / dlls / winex11.drv / mouse.c
blob07a420e59893aadcf82e84d63bbeee8327fb1741
1 /*
2 * X11 mouse driver
4 * Copyright 1998 Ulrich Weigand
5 * Copyright 2007 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <X11/Xlib.h>
26 #include <X11/cursorfont.h>
27 #include <stdarg.h>
28 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
29 #include <X11/extensions/XInput2.h>
30 #endif
32 #ifdef SONAME_LIBXCURSOR
33 # include <X11/Xcursor/Xcursor.h>
34 static void *xcursor_handle;
35 # define MAKE_FUNCPTR(f) static typeof(f) * p##f
36 MAKE_FUNCPTR(XcursorImageCreate);
37 MAKE_FUNCPTR(XcursorImageDestroy);
38 MAKE_FUNCPTR(XcursorImageLoadCursor);
39 MAKE_FUNCPTR(XcursorImagesCreate);
40 MAKE_FUNCPTR(XcursorImagesDestroy);
41 MAKE_FUNCPTR(XcursorImagesLoadCursor);
42 MAKE_FUNCPTR(XcursorLibraryLoadCursor);
43 # undef MAKE_FUNCPTR
44 #endif /* SONAME_LIBXCURSOR */
46 #define NONAMELESSUNION
47 #define NONAMELESSSTRUCT
48 #define OEMRESOURCE
49 #include "windef.h"
50 #include "winbase.h"
51 #include "winreg.h"
53 #include "x11drv.h"
54 #include "wine/server.h"
55 #include "wine/library.h"
56 #include "wine/unicode.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
61 /**********************************************************************/
63 #ifndef Button6Mask
64 #define Button6Mask (1<<13)
65 #endif
66 #ifndef Button7Mask
67 #define Button7Mask (1<<14)
68 #endif
70 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
72 static const UINT button_down_flags[NB_BUTTONS] =
74 MOUSEEVENTF_LEFTDOWN,
75 MOUSEEVENTF_MIDDLEDOWN,
76 MOUSEEVENTF_RIGHTDOWN,
77 MOUSEEVENTF_WHEEL,
78 MOUSEEVENTF_WHEEL,
79 MOUSEEVENTF_XDOWN, /* FIXME: horizontal wheel */
80 MOUSEEVENTF_XDOWN,
81 MOUSEEVENTF_XDOWN,
82 MOUSEEVENTF_XDOWN
85 static const UINT button_up_flags[NB_BUTTONS] =
87 MOUSEEVENTF_LEFTUP,
88 MOUSEEVENTF_MIDDLEUP,
89 MOUSEEVENTF_RIGHTUP,
92 MOUSEEVENTF_XUP,
93 MOUSEEVENTF_XUP,
94 MOUSEEVENTF_XUP,
95 MOUSEEVENTF_XUP
98 static const UINT button_down_data[NB_BUTTONS] =
103 WHEEL_DELTA,
104 -WHEEL_DELTA,
105 XBUTTON1,
106 XBUTTON2,
107 XBUTTON1,
108 XBUTTON2
111 static const UINT button_up_data[NB_BUTTONS] =
118 XBUTTON1,
119 XBUTTON2,
120 XBUTTON1,
121 XBUTTON2
124 static HWND cursor_window;
125 static HCURSOR last_cursor;
126 static DWORD last_cursor_change;
127 static XContext cursor_context;
128 static RECT clip_rect;
129 static Cursor create_cursor( HANDLE handle );
131 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
132 static BOOL xinput2_available;
133 static int xinput2_core_pointer;
134 static int xinput2_device_count;
135 static XIDeviceInfo *xinput2_devices;
136 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
137 MAKE_FUNCPTR(XIFreeDeviceInfo);
138 MAKE_FUNCPTR(XIQueryDevice);
139 MAKE_FUNCPTR(XIQueryVersion);
140 MAKE_FUNCPTR(XISelectEvents);
141 #undef MAKE_FUNCPTR
142 #endif
144 /***********************************************************************
145 * X11DRV_Xcursor_Init
147 * Load the Xcursor library for use.
149 void X11DRV_Xcursor_Init(void)
151 #ifdef SONAME_LIBXCURSOR
152 xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
153 if (!xcursor_handle) /* wine_dlopen failed. */
155 WARN("Xcursor failed to load. Using fallback code.\n");
156 return;
158 #define LOAD_FUNCPTR(f) \
159 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
161 LOAD_FUNCPTR(XcursorImageCreate);
162 LOAD_FUNCPTR(XcursorImageDestroy);
163 LOAD_FUNCPTR(XcursorImageLoadCursor);
164 LOAD_FUNCPTR(XcursorImagesCreate);
165 LOAD_FUNCPTR(XcursorImagesDestroy);
166 LOAD_FUNCPTR(XcursorImagesLoadCursor);
167 LOAD_FUNCPTR(XcursorLibraryLoadCursor);
168 #undef LOAD_FUNCPTR
169 #endif /* SONAME_LIBXCURSOR */
173 /***********************************************************************
174 * get_empty_cursor
176 static Cursor get_empty_cursor(void)
178 static Cursor cursor;
179 static const char data[] = { 0 };
181 wine_tsx11_lock();
182 if (!cursor)
184 XColor bg;
185 Pixmap pixmap;
187 bg.red = bg.green = bg.blue = 0x0000;
188 pixmap = XCreateBitmapFromData( gdi_display, root_window, data, 1, 1 );
189 if (pixmap)
191 cursor = XCreatePixmapCursor( gdi_display, pixmap, pixmap, &bg, &bg, 0, 0 );
192 XFreePixmap( gdi_display, pixmap );
195 wine_tsx11_unlock();
196 return cursor;
199 /***********************************************************************
200 * set_window_cursor
202 void set_window_cursor( Window window, HCURSOR handle )
204 Cursor cursor, prev;
206 wine_tsx11_lock();
207 if (!handle) cursor = get_empty_cursor();
208 else if (!cursor_context || XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
210 /* try to create it */
211 wine_tsx11_unlock();
212 if (!(cursor = create_cursor( handle ))) return;
214 wine_tsx11_lock();
215 if (!cursor_context) cursor_context = XUniqueContext();
216 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&prev ))
218 /* someone else was here first */
219 XFreeCursor( gdi_display, cursor );
220 cursor = prev;
222 else
224 XSaveContext( gdi_display, (XID)handle, cursor_context, (char *)cursor );
225 TRACE( "cursor %p created %lx\n", handle, cursor );
229 XDefineCursor( gdi_display, window, cursor );
230 /* make the change take effect immediately */
231 XFlush( gdi_display );
232 wine_tsx11_unlock();
235 /***********************************************************************
236 * sync_window_cursor
238 void sync_window_cursor( Window window )
240 HCURSOR cursor;
242 SERVER_START_REQ( set_cursor )
244 req->flags = 0;
245 wine_server_call( req );
246 cursor = reply->prev_count >= 0 ? wine_server_ptr_handle( reply->prev_handle ) : 0;
248 SERVER_END_REQ;
250 set_window_cursor( window, cursor );
253 /***********************************************************************
254 * enable_xinput2
256 static void enable_xinput2(void)
258 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
259 struct x11drv_thread_data *data = x11drv_thread_data();
260 XIEventMask mask;
261 unsigned char mask_bits[XIMaskLen(XI_LASTEVENT)];
262 int i, j;
264 if (!xinput2_available) return;
266 if (data->xi2_state == xi_unknown)
268 int major = 2, minor = 0;
269 wine_tsx11_lock();
270 if (!pXIQueryVersion( data->display, &major, &minor )) data->xi2_state = xi_disabled;
271 else
273 data->xi2_state = xi_unavailable;
274 WARN( "X Input 2 not available\n" );
276 wine_tsx11_unlock();
278 if (data->xi2_state == xi_unavailable) return;
280 wine_tsx11_lock();
281 if (xinput2_devices) pXIFreeDeviceInfo( xinput2_devices );
282 xinput2_devices = pXIQueryDevice( data->display, XIAllDevices, &xinput2_device_count );
283 for (i = 0; i < xinput2_device_count; ++i)
285 if (xinput2_devices[i].use != XIMasterPointer) continue;
286 for (j = 0; j < xinput2_devices[i].num_classes; j++)
288 XIValuatorClassInfo *class = (XIValuatorClassInfo *)xinput2_devices[i].classes[j];
290 if (xinput2_devices[i].classes[j]->type != XIValuatorClass) continue;
291 if (class->number != 0 && class->number != 1) continue;
292 TRACE( "Device %u (%s) class %u num %u %f,%f res %u mode %u\n",
293 xinput2_devices[i].deviceid, debugstr_a(xinput2_devices[i].name),
294 j, class->number, class->min, class->max, class->resolution, class->mode );
295 if (class->mode == XIModeAbsolute)
297 TRACE( "Device is absolute, not enabling XInput2\n" );
298 goto done;
301 TRACE( "Using %u (%s) as core pointer\n",
302 xinput2_devices[i].deviceid, debugstr_a(xinput2_devices[i].name) );
303 xinput2_core_pointer = xinput2_devices[i].deviceid;
304 break;
307 mask.mask = mask_bits;
308 mask.mask_len = sizeof(mask_bits);
309 memset( mask_bits, 0, sizeof(mask_bits) );
310 XISetMask( mask_bits, XI_RawMotion );
311 XISetMask( mask_bits, XI_ButtonPress );
313 for (i = 0; i < xinput2_device_count; ++i)
315 if (xinput2_devices[i].use == XISlavePointer &&
316 xinput2_devices[i].attachment == xinput2_core_pointer)
318 TRACE( "Device %u (%s) is attached to the core pointer\n",
319 xinput2_devices[i].deviceid, debugstr_a(xinput2_devices[i].name) );
320 mask.deviceid = xinput2_devices[i].deviceid;
321 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
322 data->xi2_state = xi_enabled;
326 done:
327 wine_tsx11_unlock();
328 #endif
331 /***********************************************************************
332 * disable_xinput2
334 static void disable_xinput2(void)
336 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
337 struct x11drv_thread_data *data = x11drv_thread_data();
338 XIEventMask mask;
339 int i;
341 if (data->xi2_state != xi_enabled) return;
343 TRACE( "disabling\n" );
344 data->xi2_state = xi_disabled;
346 mask.mask = NULL;
347 mask.mask_len = 0;
349 wine_tsx11_lock();
350 for (i = 0; i < xinput2_device_count; ++i)
352 if (xinput2_devices[i].use == XISlavePointer &&
353 xinput2_devices[i].attachment == xinput2_core_pointer)
355 mask.deviceid = xinput2_devices[i].deviceid;
356 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
359 pXIFreeDeviceInfo( xinput2_devices );
360 xinput2_devices = NULL;
361 xinput2_device_count = 0;
362 wine_tsx11_unlock();
363 #endif
367 /***********************************************************************
368 * grab_clipping_window
370 * Start a pointer grab on the clip window.
372 static BOOL grab_clipping_window( const RECT *clip, BOOL only_with_xinput )
374 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
375 struct x11drv_thread_data *data = x11drv_thread_data();
376 Window clip_window;
377 HWND msg_hwnd = 0;
379 if (!data) return FALSE;
380 if (!(clip_window = init_clip_window())) return TRUE;
382 if (!(msg_hwnd = CreateWindowW( messageW, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0,
383 GetModuleHandleW(0), NULL )))
384 return TRUE;
386 /* enable XInput2 unless we are already clipping */
387 if (!data->clip_hwnd) enable_xinput2();
389 /* don't clip to 1x1 rectangle if we don't have XInput */
390 if (clip->right - clip->left == 1 && clip->bottom - clip->top == 1) only_with_xinput = TRUE;
391 if (only_with_xinput && data->xi2_state != xi_enabled)
393 WARN( "XInput2 not supported, refusing to clip to %s\n", wine_dbgstr_rect(clip) );
394 DestroyWindow( msg_hwnd );
395 ClipCursor( NULL );
396 return TRUE;
399 TRACE( "clipping to %s win %lx\n", wine_dbgstr_rect(clip), clip_window );
401 wine_tsx11_lock();
402 if (!data->clip_hwnd) XUnmapWindow( data->display, clip_window );
403 XMoveResizeWindow( data->display, clip_window,
404 clip->left - virtual_screen_rect.left, clip->top - virtual_screen_rect.top,
405 max( 1, clip->right - clip->left ), max( 1, clip->bottom - clip->top ) );
406 XMapWindow( data->display, clip_window );
408 /* if the rectangle is shrinking we may get a pointer warp */
409 if (!data->clip_hwnd || clip->left > clip_rect.left || clip->top > clip_rect.top ||
410 clip->right < clip_rect.right || clip->bottom < clip_rect.bottom)
411 data->warp_serial = NextRequest( data->display );
413 if (!XGrabPointer( data->display, clip_window, False,
414 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
415 GrabModeAsync, GrabModeAsync, clip_window, None, CurrentTime ))
416 clipping_cursor = 1;
417 wine_tsx11_unlock();
419 if (!clipping_cursor)
421 disable_xinput2();
422 DestroyWindow( msg_hwnd );
423 return FALSE;
425 clip_rect = *clip;
426 if (!data->clip_hwnd) sync_window_cursor( clip_window );
427 InterlockedExchangePointer( (void **)&cursor_window, msg_hwnd );
428 data->clip_hwnd = msg_hwnd;
429 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, (LPARAM)msg_hwnd );
430 return TRUE;
433 /***********************************************************************
434 * ungrab_clipping_window
436 * Release the pointer grab on the clip window.
438 void ungrab_clipping_window(void)
440 Display *display = thread_init_display();
441 Window clip_window = init_clip_window();
443 if (!clip_window) return;
445 TRACE( "no longer clipping\n" );
446 wine_tsx11_lock();
447 XUnmapWindow( display, clip_window );
448 wine_tsx11_unlock();
449 clipping_cursor = 0;
450 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, 0 );
453 /***********************************************************************
454 * reset_clipping_window
456 * Forcibly reset the window clipping on external events.
458 void reset_clipping_window(void)
460 ungrab_clipping_window();
461 ClipCursor( NULL ); /* make sure the clip rectangle is reset too */
464 /***********************************************************************
465 * clip_cursor_notify
467 * Notification function called upon receiving a WM_X11DRV_CLIP_CURSOR.
469 LRESULT clip_cursor_notify( HWND hwnd, HWND new_clip_hwnd )
471 struct x11drv_thread_data *data = x11drv_thread_data();
473 if (hwnd == GetDesktopWindow()) /* change the clip window stored in the desktop process */
475 static HWND clip_hwnd;
477 HWND prev = clip_hwnd;
478 clip_hwnd = new_clip_hwnd;
479 if (prev || new_clip_hwnd) TRACE( "clip hwnd changed from %p to %p\n", prev, new_clip_hwnd );
480 if (prev) SendNotifyMessageW( prev, WM_X11DRV_CLIP_CURSOR, 0, 0 );
482 else if (hwnd == data->clip_hwnd) /* this is a notification that clipping has been reset */
484 TRACE( "clip hwnd reset from %p\n", hwnd );
485 data->clip_hwnd = 0;
486 data->clip_reset = GetTickCount();
487 disable_xinput2();
488 DestroyWindow( hwnd );
490 else if (hwnd == GetForegroundWindow()) /* request to clip */
492 RECT clip;
494 GetClipCursor( &clip );
495 if (clip.left > virtual_screen_rect.left || clip.right < virtual_screen_rect.right ||
496 clip.top > virtual_screen_rect.top || clip.bottom < virtual_screen_rect.bottom)
497 return grab_clipping_window( &clip, FALSE );
499 return 0;
502 /***********************************************************************
503 * clip_fullscreen_window
505 * Turn on clipping if the active window is fullscreen.
507 BOOL clip_fullscreen_window( HWND hwnd, BOOL reset )
509 struct x11drv_win_data *data;
510 struct x11drv_thread_data *thread_data;
511 RECT rect;
512 DWORD style;
514 if (hwnd == GetDesktopWindow()) return FALSE;
515 if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
516 style = GetWindowLongW( hwnd, GWL_STYLE );
517 if (!(style & WS_VISIBLE)) return FALSE;
518 if ((style & (WS_POPUP | WS_CHILD)) == WS_CHILD) return FALSE;
519 /* maximized windows don't count as full screen */
520 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION) return FALSE;
521 if (!is_window_rect_fullscreen( &data->whole_rect )) return FALSE;
522 if (!(thread_data = x11drv_thread_data())) return FALSE;
523 if (GetTickCount() - thread_data->clip_reset < 1000) return FALSE;
524 if (!reset && clipping_cursor && thread_data->clip_hwnd) return FALSE; /* already clipping */
525 SetRect( &rect, 0, 0, screen_width, screen_height );
526 if (!grab_fullscreen)
528 if (!EqualRect( &rect, &virtual_screen_rect )) return FALSE;
529 if (root_window != DefaultRootWindow( gdi_display )) return FALSE;
531 TRACE( "win %p clipping fullscreen\n", hwnd );
532 return grab_clipping_window( &rect, TRUE );
535 /***********************************************************************
536 * send_mouse_input
538 * Update the various window states on a mouse event.
540 static void send_mouse_input( HWND hwnd, Window window, unsigned int state, INPUT *input )
542 struct x11drv_win_data *data;
543 POINT pt;
545 input->type = INPUT_MOUSE;
547 if (!hwnd)
549 struct x11drv_thread_data *thread_data = x11drv_thread_data();
550 HWND clip_hwnd = thread_data->clip_hwnd;
552 if (!clip_hwnd) return;
553 if (thread_data->clip_window != window) return;
554 if (InterlockedExchangePointer( (void **)&cursor_window, clip_hwnd ) != clip_hwnd ||
555 input->u.mi.time - last_cursor_change > 100)
557 sync_window_cursor( window );
558 last_cursor_change = input->u.mi.time;
560 input->u.mi.dx += clip_rect.left;
561 input->u.mi.dy += clip_rect.top;
562 __wine_send_input( hwnd, input );
563 return;
566 if (!(data = X11DRV_get_win_data( hwnd ))) return;
568 if (window == data->whole_window)
570 input->u.mi.dx += data->whole_rect.left - data->client_rect.left;
571 input->u.mi.dy += data->whole_rect.top - data->client_rect.top;
573 if (window == root_window)
575 input->u.mi.dx += virtual_screen_rect.left;
576 input->u.mi.dy += virtual_screen_rect.top;
578 pt.x = input->u.mi.dx;
579 pt.y = input->u.mi.dy;
580 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
581 pt.x = data->client_rect.right - data->client_rect.left - 1 - pt.x;
582 MapWindowPoints( hwnd, 0, &pt, 1 );
584 if (InterlockedExchangePointer( (void **)&cursor_window, hwnd ) != hwnd ||
585 input->u.mi.time - last_cursor_change > 100)
587 sync_window_cursor( data->whole_window );
588 last_cursor_change = input->u.mi.time;
591 if (hwnd != GetDesktopWindow())
593 hwnd = GetAncestor( hwnd, GA_ROOT );
594 if ((input->u.mi.dwFlags & (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_RIGHTDOWN)) && hwnd == GetForegroundWindow())
595 clip_fullscreen_window( hwnd, FALSE );
598 /* update the wine server Z-order */
600 if (window != x11drv_thread_data()->grab_window &&
601 /* ignore event if a button is pressed, since the mouse is then grabbed too */
602 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
604 RECT rect;
605 SetRect( &rect, pt.x, pt.y, pt.x + 1, pt.y + 1 );
606 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
608 SERVER_START_REQ( update_window_zorder )
610 req->window = wine_server_user_handle( hwnd );
611 req->rect.left = rect.left;
612 req->rect.top = rect.top;
613 req->rect.right = rect.right;
614 req->rect.bottom = rect.bottom;
615 wine_server_call( req );
617 SERVER_END_REQ;
620 input->u.mi.dx = pt.x;
621 input->u.mi.dy = pt.y;
622 __wine_send_input( hwnd, input );
625 #ifdef SONAME_LIBXCURSOR
627 /***********************************************************************
628 * create_xcursor_frame
630 * Use Xcursor to create a frame of an X cursor from a Windows one.
632 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
633 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
634 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
635 int width, int height, int istep )
637 XcursorImage *image, *ret = NULL;
638 DWORD delay_jiffies, num_steps;
639 int x, y, i, has_alpha = FALSE;
640 XcursorPixel *ptr;
642 wine_tsx11_lock();
643 image = pXcursorImageCreate( width, height );
644 wine_tsx11_unlock();
645 if (!image)
647 ERR("X11 failed to produce a cursor frame!\n");
648 goto cleanup;
651 image->xhot = iinfo->xHotspot;
652 image->yhot = iinfo->yHotspot;
654 image->delay = 100; /* fallback delay, 100 ms */
655 if (GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, istep, &delay_jiffies, &num_steps) != 0)
656 image->delay = (100 * delay_jiffies) / 6; /* convert jiffies (1/60s) to milliseconds */
657 else
658 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep);
660 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
661 memset( color_bits, 0x00, color_size );
662 SelectObject( hdc, hbmColor );
663 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
665 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
666 goto cleanup;
668 memcpy( image->pixels, color_bits, color_size );
670 /* check if the cursor frame was drawn with an alpha channel */
671 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
672 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
674 /* if no alpha channel was drawn then generate it from the mask */
675 if (!has_alpha)
677 unsigned int width_bytes = (width + 31) / 32 * 4;
679 /* draw the cursor mask to a temporary buffer */
680 memset( mask_bits, 0xFF, mask_size );
681 SelectObject( hdc, hbmMask );
682 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
684 ERR("Failed to draw frame mask %d.\n", istep);
685 goto cleanup;
687 /* use the buffer to directly modify the XcursorImage alpha channel */
688 for (y = 0, ptr = image->pixels; y < height; y++)
689 for (x = 0; x < width; x++, ptr++)
690 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
691 *ptr |= 0xff000000;
693 ret = image;
695 cleanup:
696 if (ret == NULL) pXcursorImageDestroy( image );
697 return ret;
700 /***********************************************************************
701 * create_xcursor_cursor
703 * Use Xcursor to create an X cursor from a Windows one.
705 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
707 unsigned char *color_bits, *mask_bits;
708 HBITMAP hbmColor = 0, hbmMask = 0;
709 DWORD nFrames, delay_jiffies, i;
710 int color_size, mask_size;
711 BITMAPINFO *info = NULL;
712 XcursorImages *images;
713 XcursorImage **imgs;
714 Cursor cursor = 0;
716 /* Retrieve the number of frames to render */
717 if (!GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, 0, &delay_jiffies, &nFrames)) return 0;
718 if (!(imgs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XcursorImage*)*nFrames ))) return 0;
720 /* Allocate all of the resources necessary to obtain a cursor frame */
721 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
722 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
723 info->bmiHeader.biWidth = width;
724 info->bmiHeader.biHeight = -height;
725 info->bmiHeader.biPlanes = 1;
726 info->bmiHeader.biCompression = BI_RGB;
727 info->bmiHeader.biXPelsPerMeter = 0;
728 info->bmiHeader.biYPelsPerMeter = 0;
729 info->bmiHeader.biClrUsed = 0;
730 info->bmiHeader.biClrImportant = 0;
731 info->bmiHeader.biBitCount = 32;
732 color_size = width * height * 4;
733 info->bmiHeader.biSizeImage = color_size;
734 hbmColor = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &color_bits, NULL, 0);
735 if (!hbmColor)
737 ERR("Failed to create DIB section for cursor color data!\n");
738 goto cleanup;
740 info->bmiHeader.biBitCount = 1;
741 info->bmiColors[0].rgbRed = 0;
742 info->bmiColors[0].rgbGreen = 0;
743 info->bmiColors[0].rgbBlue = 0;
744 info->bmiColors[0].rgbReserved = 0;
745 info->bmiColors[1].rgbRed = 0xff;
746 info->bmiColors[1].rgbGreen = 0xff;
747 info->bmiColors[1].rgbBlue = 0xff;
748 info->bmiColors[1].rgbReserved = 0;
750 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
751 info->bmiHeader.biSizeImage = mask_size;
752 hbmMask = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &mask_bits, NULL, 0);
753 if (!hbmMask)
755 ERR("Failed to create DIB section for cursor mask data!\n");
756 goto cleanup;
759 /* Create an XcursorImage for each frame of the cursor */
760 for (i=0; i<nFrames; i++)
762 imgs[i] = create_xcursor_frame( hdc, iinfo, icon,
763 hbmColor, color_bits, color_size,
764 hbmMask, mask_bits, mask_size,
765 width, height, i );
766 if (!imgs[i]) goto cleanup;
769 /* Build an X cursor out of all of the frames */
770 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
771 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
772 images->images[images->nimage] = imgs[images->nimage];
773 wine_tsx11_lock();
774 cursor = pXcursorImagesLoadCursor( gdi_display, images );
775 wine_tsx11_unlock();
776 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
777 HeapFree( GetProcessHeap(), 0, imgs );
778 imgs = NULL;
780 cleanup:
781 if (imgs)
783 /* Failed to produce a cursor, free previously allocated frames */
784 for (i=0; i<nFrames && imgs[i]; i++)
785 pXcursorImageDestroy( imgs[i] );
786 HeapFree( GetProcessHeap(), 0, imgs );
788 /* Cleanup all of the resources used to obtain the frame data */
789 if (hbmColor) DeleteObject( hbmColor );
790 if (hbmMask) DeleteObject( hbmMask );
791 HeapFree( GetProcessHeap(), 0, info );
792 return cursor;
796 struct system_cursors
798 WORD id;
799 const char *name;
802 static const struct system_cursors user32_cursors[] =
804 { OCR_NORMAL, "left_ptr" },
805 { OCR_IBEAM, "xterm" },
806 { OCR_WAIT, "watch" },
807 { OCR_CROSS, "cross" },
808 { OCR_UP, "center_ptr" },
809 { OCR_SIZE, "fleur" },
810 { OCR_SIZEALL, "fleur" },
811 { OCR_ICON, "icon" },
812 { OCR_SIZENWSE, "nwse-resize" },
813 { OCR_SIZENESW, "nesw-resize" },
814 { OCR_SIZEWE, "ew-resize" },
815 { OCR_SIZENS, "ns-resize" },
816 { OCR_NO, "not-allowed" },
817 { OCR_HAND, "hand2" },
818 { OCR_APPSTARTING, "left_ptr_watch" },
819 { OCR_HELP, "question_arrow" },
820 { 0 }
823 static const struct system_cursors comctl32_cursors[] =
825 { 102, "move" },
826 { 104, "copy" },
827 { 105, "left_ptr" },
828 { 106, "row-resize" },
829 { 107, "row-resize" },
830 { 108, "hand2" },
831 { 135, "col-resize" },
832 { 0 }
835 static const struct system_cursors ole32_cursors[] =
837 { 1, "no-drop" },
838 { 2, "move" },
839 { 3, "copy" },
840 { 4, "alias" },
841 { 0 }
844 static const struct system_cursors riched20_cursors[] =
846 { 105, "hand2" },
847 { 107, "right_ptr" },
848 { 109, "copy" },
849 { 110, "move" },
850 { 111, "no-drop" },
851 { 0 }
854 static const struct
856 const struct system_cursors *cursors;
857 WCHAR name[16];
858 } module_cursors[] =
860 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
861 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
862 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
863 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
866 /***********************************************************************
867 * create_xcursor_system_cursor
869 * Create an X cursor for a system cursor.
871 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
873 static const WCHAR idW[] = {'%','h','u',0};
874 const struct system_cursors *cursors;
875 unsigned int i;
876 Cursor cursor = 0;
877 HMODULE module;
878 HKEY key;
879 WCHAR *p, name[MAX_PATH * 2], valueW[64];
880 char valueA[64];
881 DWORD size, ret;
883 if (!pXcursorLibraryLoadCursor) return 0;
884 if (!info->szModName[0]) return 0;
886 p = strrchrW( info->szModName, '\\' );
887 strcpyW( name, p ? p + 1 : info->szModName );
888 p = name + strlenW( name );
889 *p++ = ',';
890 if (info->szResName[0]) strcpyW( p, info->szResName );
891 else sprintfW( p, idW, info->wResID );
892 valueA[0] = 0;
894 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
895 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver\\Cursors", &key ))
897 size = sizeof(valueW) / sizeof(WCHAR);
898 ret = RegQueryValueExW( key, name, NULL, NULL, (BYTE *)valueW, &size );
899 RegCloseKey( key );
900 if (!ret)
902 if (!valueW[0]) return 0; /* force standard cursor */
903 if (!WideCharToMultiByte( CP_UNIXCP, 0, valueW, -1, valueA, sizeof(valueA), NULL, NULL ))
904 valueA[0] = 0;
905 goto done;
909 if (info->szResName[0]) goto done; /* only integer resources are supported here */
910 if (!(module = GetModuleHandleW( info->szModName ))) goto done;
912 for (i = 0; i < sizeof(module_cursors)/sizeof(module_cursors[0]); i++)
913 if (GetModuleHandleW( module_cursors[i].name ) == module) break;
914 if (i == sizeof(module_cursors)/sizeof(module_cursors[0])) goto done;
916 cursors = module_cursors[i].cursors;
917 for (i = 0; cursors[i].id; i++)
918 if (cursors[i].id == info->wResID)
920 strcpy( valueA, cursors[i].name );
921 break;
924 done:
925 if (valueA[0])
927 wine_tsx11_lock();
928 cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
929 wine_tsx11_unlock();
930 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
931 debugstr_w(name), debugstr_a(valueA) );
933 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
934 return cursor;
937 #endif /* SONAME_LIBXCURSOR */
940 /***********************************************************************
941 * create_cursor_from_bitmaps
943 * Create an X11 cursor from source bitmaps.
945 static Cursor create_cursor_from_bitmaps( HBITMAP src_xor, HBITMAP src_and, int width, int height,
946 int xor_y, int and_y, XColor *fg, XColor *bg,
947 int hotspot_x, int hotspot_y )
949 HDC src = 0, dst = 0;
950 HBITMAP bits = 0, mask = 0, mask_inv = 0;
951 Cursor cursor = 0;
953 if (!(src = CreateCompatibleDC( 0 ))) goto done;
954 if (!(dst = CreateCompatibleDC( 0 ))) goto done;
956 if (!(bits = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
957 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
958 if (!(mask_inv = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
960 /* We have to do some magic here, as cursors are not fully
961 * compatible between Windows and X11. Under X11, there are
962 * only 3 possible color cursor: black, white and masked. So
963 * we map the 4th Windows color (invert the bits on the screen)
964 * to black and an additional white bit on an other place
965 * (+1,+1). This require some boolean arithmetic:
967 * Windows | X11
968 * And Xor Result | Bits Mask Result
969 * 0 0 black | 0 1 background
970 * 0 1 white | 1 1 foreground
971 * 1 0 no change | X 0 no change
972 * 1 1 inverted | 0 1 background
974 * which gives:
975 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
976 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
978 SelectObject( src, src_and );
979 SelectObject( dst, bits );
980 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
981 SelectObject( dst, mask );
982 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
983 SelectObject( dst, mask_inv );
984 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
985 SelectObject( src, src_xor );
986 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, SRCAND /* src & dst */ );
987 SelectObject( dst, bits );
988 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, SRCERASE /* src & ~dst */ );
989 SelectObject( dst, mask );
990 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, 0xdd0228 /* src | ~dst */ );
991 /* additional white */
992 SelectObject( src, mask_inv );
993 BitBlt( dst, 1, 1, width, height, src, 0, 0, SRCPAINT /* src | dst */);
994 SelectObject( dst, bits );
995 BitBlt( dst, 1, 1, width, height, src, 0, 0, SRCPAINT /* src | dst */ );
997 wine_tsx11_lock();
998 cursor = XCreatePixmapCursor( gdi_display, X11DRV_get_pixmap(bits), X11DRV_get_pixmap(mask),
999 fg, bg, hotspot_x, hotspot_y );
1000 wine_tsx11_unlock();
1002 done:
1003 DeleteDC( src );
1004 DeleteDC( dst );
1005 DeleteObject( bits );
1006 DeleteObject( mask );
1007 DeleteObject( mask_inv );
1008 return cursor;
1011 /***********************************************************************
1012 * create_xlib_cursor
1014 * Create an X cursor from a Windows one.
1016 static Cursor create_xlib_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1018 XColor fg, bg;
1019 Cursor cursor = None;
1020 HBITMAP xor_bitmap = 0;
1021 BITMAPINFO *info;
1022 unsigned int *color_bits = NULL, *ptr;
1023 unsigned char *mask_bits = NULL, *xor_bits = NULL;
1024 int i, x, y, has_alpha = 0;
1025 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
1026 unsigned int width_bytes = (width + 31) / 32 * 4;
1028 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] ))))
1029 return FALSE;
1030 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1031 info->bmiHeader.biWidth = width;
1032 info->bmiHeader.biHeight = -height;
1033 info->bmiHeader.biPlanes = 1;
1034 info->bmiHeader.biBitCount = 1;
1035 info->bmiHeader.biCompression = BI_RGB;
1036 info->bmiHeader.biSizeImage = width_bytes * height;
1037 info->bmiHeader.biXPelsPerMeter = 0;
1038 info->bmiHeader.biYPelsPerMeter = 0;
1039 info->bmiHeader.biClrUsed = 0;
1040 info->bmiHeader.biClrImportant = 0;
1042 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1043 if (!GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS )) goto done;
1045 info->bmiHeader.biBitCount = 32;
1046 info->bmiHeader.biSizeImage = width * height * 4;
1047 if (!(color_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1048 if (!(xor_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, width_bytes * height ))) goto done;
1049 GetDIBits( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS );
1051 /* compute fg/bg color and xor bitmap based on average of the color values */
1053 if (!(xor_bitmap = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
1054 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
1055 for (y = 0, ptr = color_bits; y < height; y++)
1057 for (x = 0; x < width; x++, ptr++)
1059 int red = (*ptr >> 16) & 0xff;
1060 int green = (*ptr >> 8) & 0xff;
1061 int blue = (*ptr >> 0) & 0xff;
1062 if (red + green + blue > 0x40)
1064 rfg += red;
1065 gfg += green;
1066 bfg += blue;
1067 fgBits++;
1068 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1070 else
1072 rbg += red;
1073 gbg += green;
1074 bbg += blue;
1078 if (fgBits)
1080 fg.red = rfg * 257 / fgBits;
1081 fg.green = gfg * 257 / fgBits;
1082 fg.blue = bfg * 257 / fgBits;
1084 else fg.red = fg.green = fg.blue = 0;
1085 bgBits = width * height - fgBits;
1086 if (bgBits)
1088 bg.red = rbg * 257 / bgBits;
1089 bg.green = gbg * 257 / bgBits;
1090 bg.blue = bbg * 257 / bgBits;
1092 else bg.red = bg.green = bg.blue = 0;
1094 info->bmiHeader.biBitCount = 1;
1095 info->bmiHeader.biSizeImage = width_bytes * height;
1096 SetDIBits( hdc, xor_bitmap, 0, height, xor_bits, info, DIB_RGB_COLORS );
1098 /* generate mask from the alpha channel if we have one */
1100 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
1101 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
1103 if (has_alpha)
1105 /* make sure the bitmaps are owned by x11drv */
1106 HBITMAP orig = SelectObject( hdc, icon->hbmMask );
1107 SelectObject( hdc, xor_bitmap );
1108 SelectObject( hdc, orig );
1110 memset( mask_bits, 0, width_bytes * height );
1111 for (y = 0, ptr = color_bits; y < height; y++)
1112 for (x = 0; x < width; x++, ptr++)
1113 if ((*ptr >> 24) > 25) /* more than 10% alpha */
1114 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1116 info->bmiHeader.biBitCount = 1;
1117 info->bmiHeader.biSizeImage = width_bytes * height;
1118 SetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS );
1120 wine_tsx11_lock();
1121 cursor = XCreatePixmapCursor( gdi_display,
1122 X11DRV_get_pixmap(xor_bitmap),
1123 X11DRV_get_pixmap(icon->hbmMask),
1124 &fg, &bg, icon->xHotspot, icon->yHotspot );
1125 wine_tsx11_unlock();
1127 else
1129 cursor = create_cursor_from_bitmaps( xor_bitmap, icon->hbmMask, width, height, 0, 0,
1130 &fg, &bg, icon->xHotspot, icon->yHotspot );
1133 done:
1134 DeleteObject( xor_bitmap );
1135 HeapFree( GetProcessHeap(), 0, info );
1136 HeapFree( GetProcessHeap(), 0, color_bits );
1137 HeapFree( GetProcessHeap(), 0, xor_bits );
1138 HeapFree( GetProcessHeap(), 0, mask_bits );
1139 return cursor;
1142 /***********************************************************************
1143 * create_cursor
1145 * Create an X cursor from a Windows one.
1147 static Cursor create_cursor( HANDLE handle )
1149 Cursor cursor = 0;
1150 ICONINFOEXW info;
1151 BITMAP bm;
1153 if (!handle) return get_empty_cursor();
1155 info.cbSize = sizeof(info);
1156 if (!GetIconInfoExW( handle, &info )) return 0;
1158 #ifdef SONAME_LIBXCURSOR
1159 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
1161 DeleteObject( info.hbmColor );
1162 DeleteObject( info.hbmMask );
1163 return cursor;
1165 #endif
1167 GetObjectW( info.hbmMask, sizeof(bm), &bm );
1168 if (!info.hbmColor) bm.bmHeight = max( 1, bm.bmHeight / 2 );
1170 /* make sure hotspot is valid */
1171 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1173 info.xHotspot = bm.bmWidth / 2;
1174 info.yHotspot = bm.bmHeight / 2;
1177 if (info.hbmColor)
1179 HDC hdc = CreateCompatibleDC( 0 );
1180 if (hdc)
1182 #ifdef SONAME_LIBXCURSOR
1183 if (pXcursorImagesLoadCursor)
1184 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
1185 #endif
1186 if (!cursor) cursor = create_xlib_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1188 DeleteObject( info.hbmColor );
1189 DeleteDC( hdc );
1191 else
1193 XColor fg, bg;
1194 fg.red = fg.green = fg.blue = 0xffff;
1195 bg.red = bg.green = bg.blue = 0;
1196 cursor = create_cursor_from_bitmaps( info.hbmMask, info.hbmMask, bm.bmWidth, bm.bmHeight,
1197 bm.bmHeight, 0, &fg, &bg, info.xHotspot, info.yHotspot );
1200 DeleteObject( info.hbmMask );
1201 return cursor;
1204 /***********************************************************************
1205 * DestroyCursorIcon (X11DRV.@)
1207 void CDECL X11DRV_DestroyCursorIcon( HCURSOR handle )
1209 Cursor cursor;
1211 wine_tsx11_lock();
1212 if (cursor_context && !XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1214 TRACE( "%p xid %lx\n", handle, cursor );
1215 XFreeCursor( gdi_display, cursor );
1216 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1218 wine_tsx11_unlock();
1221 /***********************************************************************
1222 * SetCursor (X11DRV.@)
1224 void CDECL X11DRV_SetCursor( HCURSOR handle )
1226 if (InterlockedExchangePointer( (void **)&last_cursor, handle ) != handle ||
1227 GetTickCount() - last_cursor_change > 100)
1229 last_cursor_change = GetTickCount();
1230 if (cursor_window) SendNotifyMessageW( cursor_window, WM_X11DRV_SET_CURSOR, 0, (LPARAM)handle );
1234 /***********************************************************************
1235 * SetCursorPos (X11DRV.@)
1237 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
1239 struct x11drv_thread_data *data = x11drv_init_thread_data();
1241 wine_tsx11_lock();
1242 XWarpPointer( data->display, root_window, root_window, 0, 0, 0, 0,
1243 x - virtual_screen_rect.left, y - virtual_screen_rect.top );
1244 data->warp_serial = NextRequest( data->display );
1245 XNoOp( data->display );
1246 XFlush( data->display ); /* avoids bad mouse lag in games that do their own mouse warping */
1247 wine_tsx11_unlock();
1248 TRACE( "warped to %d,%d serial %lu\n", x, y, data->warp_serial );
1249 return TRUE;
1252 /***********************************************************************
1253 * GetCursorPos (X11DRV.@)
1255 BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
1257 Display *display = thread_init_display();
1258 Window root, child;
1259 int rootX, rootY, winX, winY;
1260 unsigned int xstate;
1261 BOOL ret;
1263 wine_tsx11_lock();
1264 ret = XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &winX, &winY, &xstate );
1265 if (ret)
1267 POINT old = *pos;
1268 pos->x = winX + virtual_screen_rect.left;
1269 pos->y = winY + virtual_screen_rect.top;
1270 TRACE( "pointer at (%d,%d) server pos %d,%d\n", pos->x, pos->y, old.x, old.y );
1272 wine_tsx11_unlock();
1273 return ret;
1276 /***********************************************************************
1277 * ClipCursor (X11DRV.@)
1279 BOOL CDECL X11DRV_ClipCursor( LPCRECT clip )
1281 if (!clip)
1283 ungrab_clipping_window();
1284 return TRUE;
1287 if (GetWindowThreadProcessId( GetDesktopWindow(), NULL ) == GetCurrentThreadId())
1288 return TRUE; /* don't clip in the desktop process */
1290 if (grab_pointer)
1292 HWND foreground = GetForegroundWindow();
1294 /* we are clipping if the clip rectangle is smaller than the screen */
1295 if (clip->left > virtual_screen_rect.left || clip->right < virtual_screen_rect.right ||
1296 clip->top > virtual_screen_rect.top || clip->bottom < virtual_screen_rect.bottom)
1298 DWORD tid, pid;
1300 /* forward request to the foreground window if it's in a different thread */
1301 tid = GetWindowThreadProcessId( foreground, &pid );
1302 if (tid && tid != GetCurrentThreadId() && pid == GetCurrentProcessId())
1304 TRACE( "forwarding clip request to %p\n", foreground );
1305 SendNotifyMessageW( foreground, WM_X11DRV_CLIP_CURSOR, 0, 0 );
1306 return TRUE;
1308 else if (grab_clipping_window( clip, FALSE )) return TRUE;
1310 else /* if currently clipping, check if we should switch to fullscreen clipping */
1312 struct x11drv_thread_data *data = x11drv_thread_data();
1313 if (data && data->clip_hwnd)
1315 if (EqualRect( clip, &clip_rect ) || clip_fullscreen_window( foreground, TRUE ))
1316 return TRUE;
1320 ungrab_clipping_window();
1321 return TRUE;
1324 /***********************************************************************
1325 * X11DRV_ButtonPress
1327 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1329 XButtonEvent *event = &xev->xbutton;
1330 int buttonNum = event->button - 1;
1331 INPUT input;
1333 if (buttonNum >= NB_BUTTONS) return;
1335 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1337 input.u.mi.dx = event->x;
1338 input.u.mi.dy = event->y;
1339 input.u.mi.mouseData = button_down_data[buttonNum];
1340 input.u.mi.dwFlags = button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1341 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1342 input.u.mi.dwExtraInfo = 0;
1344 update_user_time( event->time );
1345 send_mouse_input( hwnd, event->window, event->state, &input );
1349 /***********************************************************************
1350 * X11DRV_ButtonRelease
1352 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1354 XButtonEvent *event = &xev->xbutton;
1355 int buttonNum = event->button - 1;
1356 INPUT input;
1358 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1360 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1362 input.u.mi.dx = event->x;
1363 input.u.mi.dy = event->y;
1364 input.u.mi.mouseData = button_up_data[buttonNum];
1365 input.u.mi.dwFlags = button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1366 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1367 input.u.mi.dwExtraInfo = 0;
1369 send_mouse_input( hwnd, event->window, event->state, &input );
1373 /***********************************************************************
1374 * X11DRV_MotionNotify
1376 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1378 XMotionEvent *event = &xev->xmotion;
1379 INPUT input;
1381 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d serial %lu\n",
1382 hwnd, event->window, event->x, event->y, event->is_hint, event->serial );
1384 input.u.mi.dx = event->x;
1385 input.u.mi.dy = event->y;
1386 input.u.mi.mouseData = 0;
1387 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1388 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1389 input.u.mi.dwExtraInfo = 0;
1391 if (!hwnd)
1393 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1394 if (thread_data->warp_serial && (long)(event->serial - thread_data->warp_serial) < 0) return;
1397 send_mouse_input( hwnd, event->window, event->state, &input );
1401 /***********************************************************************
1402 * X11DRV_EnterNotify
1404 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1406 XCrossingEvent *event = &xev->xcrossing;
1407 INPUT input;
1409 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd, event->window, event->x, event->y, event->detail );
1411 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1412 if (event->window == x11drv_thread_data()->grab_window) return;
1414 /* simulate a mouse motion event */
1415 input.u.mi.dx = event->x;
1416 input.u.mi.dy = event->y;
1417 input.u.mi.mouseData = 0;
1418 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1419 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1420 input.u.mi.dwExtraInfo = 0;
1422 send_mouse_input( hwnd, event->window, event->state, &input );
1425 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1427 /***********************************************************************
1428 * X11DRV_RawMotion
1430 static void X11DRV_RawMotion( XGenericEventCookie *xev )
1432 XIRawEvent *event = xev->data;
1433 const double *values = event->valuators.values;
1434 INPUT input;
1435 int i, j;
1436 double dx = 0, dy = 0;
1437 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1439 if (!event->valuators.mask_len) return;
1440 if (thread_data->xi2_state != xi_enabled) return;
1442 input.u.mi.mouseData = 0;
1443 input.u.mi.dwFlags = MOUSEEVENTF_MOVE;
1444 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1445 input.u.mi.dwExtraInfo = 0;
1447 if (XIMaskIsSet( event->valuators.mask, 0 )) dx = *values++;
1448 if (XIMaskIsSet( event->valuators.mask, 1 )) dy = *values++;
1449 input.u.mi.dx = dx;
1450 input.u.mi.dy = dy;
1452 wine_tsx11_lock();
1453 for (i = 0; i < xinput2_device_count; ++i)
1455 if (xinput2_devices[i].deviceid != event->deviceid) continue;
1456 for (j = 0; j < xinput2_devices[i].num_classes; j++)
1458 XIValuatorClassInfo *class = (XIValuatorClassInfo *)xinput2_devices[i].classes[j];
1460 if (xinput2_devices[i].classes[j]->type != XIValuatorClass) continue;
1461 if (class->min >= class->max) continue;
1462 if (class->number == 0)
1463 input.u.mi.dx = dx * (virtual_screen_rect.right - virtual_screen_rect.left)
1464 / (class->max - class->min);
1465 else if (class->number == 1)
1466 input.u.mi.dy = dy * (virtual_screen_rect.bottom - virtual_screen_rect.top)
1467 / (class->max - class->min);
1469 break;
1472 wine_tsx11_unlock();
1474 if (thread_data->warp_serial)
1476 if ((long)(xev->serial - thread_data->warp_serial) < 0)
1478 TRACE( "pos %d,%d old serial %lu, ignoring\n", input.u.mi.dx, input.u.mi.dy, xev->serial );
1479 return;
1481 thread_data->warp_serial = 0; /* we caught up now */
1484 TRACE( "pos %d,%d (event %f,%f)\n", input.u.mi.dx, input.u.mi.dy, dx, dy );
1486 input.type = INPUT_MOUSE;
1487 __wine_send_input( 0, &input );
1490 #endif /* HAVE_X11_EXTENSIONS_XINPUT2_H */
1493 /***********************************************************************
1494 * X11DRV_XInput2_Init
1496 void X11DRV_XInput2_Init(void)
1498 #if defined(SONAME_LIBXI) && defined(HAVE_X11_EXTENSIONS_XINPUT2_H)
1499 int event, error;
1500 void *libxi_handle = wine_dlopen( SONAME_LIBXI, RTLD_NOW, NULL, 0 );
1502 if (!libxi_handle)
1504 WARN( "couldn't load %s\n", SONAME_LIBXI );
1505 return;
1507 #define LOAD_FUNCPTR(f) \
1508 if (!(p##f = wine_dlsym( libxi_handle, #f, NULL, 0))) \
1510 WARN("Failed to load %s.\n", #f); \
1511 return; \
1514 LOAD_FUNCPTR(XIFreeDeviceInfo);
1515 LOAD_FUNCPTR(XIQueryDevice);
1516 LOAD_FUNCPTR(XIQueryVersion);
1517 LOAD_FUNCPTR(XISelectEvents);
1518 #undef LOAD_FUNCPTR
1520 wine_tsx11_lock();
1521 xinput2_available = XQueryExtension( gdi_display, "XInputExtension", &xinput2_opcode, &event, &error );
1522 wine_tsx11_unlock();
1523 #else
1524 TRACE( "X Input 2 support not compiled in.\n" );
1525 #endif
1529 /***********************************************************************
1530 * X11DRV_GenericEvent
1532 void X11DRV_GenericEvent( HWND hwnd, XEvent *xev )
1534 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1535 XGenericEventCookie *event = &xev->xcookie;
1537 if (!event->data) return;
1538 if (event->extension != xinput2_opcode) return;
1540 switch (event->evtype)
1542 case XI_RawMotion:
1543 X11DRV_RawMotion( event );
1544 break;
1546 default:
1547 TRACE( "Unhandled event %#x\n", event->evtype );
1548 break;
1550 #endif