winex11: Reset clipping by calling NtUserClipCursor directly.
[wine.git] / dlls / winex11.drv / mouse.c
blobfc35081652c97816c5c951d1638f89d89b1a5e70
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 #if 0
23 #pragma makedep unix
24 #endif
26 #include "config.h"
28 #include <math.h>
29 #include <dlfcn.h>
30 #include <X11/Xlib.h>
31 #include <X11/cursorfont.h>
32 #include <stdarg.h>
33 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
34 #include <X11/extensions/XInput2.h>
35 #endif
37 #ifdef SONAME_LIBXCURSOR
38 # include <X11/Xcursor/Xcursor.h>
39 static void *xcursor_handle;
40 # define MAKE_FUNCPTR(f) static typeof(f) * p##f
41 MAKE_FUNCPTR(XcursorImageCreate);
42 MAKE_FUNCPTR(XcursorImageDestroy);
43 MAKE_FUNCPTR(XcursorImageLoadCursor);
44 MAKE_FUNCPTR(XcursorImagesCreate);
45 MAKE_FUNCPTR(XcursorImagesDestroy);
46 MAKE_FUNCPTR(XcursorImagesLoadCursor);
47 MAKE_FUNCPTR(XcursorLibraryLoadCursor);
48 # undef MAKE_FUNCPTR
49 #endif /* SONAME_LIBXCURSOR */
51 #define NONAMELESSUNION
52 #define OEMRESOURCE
54 #include "x11drv.h"
55 #include "winreg.h"
56 #include "wine/server.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_HWHEEL,
80 MOUSEEVENTF_HWHEEL,
81 MOUSEEVENTF_XDOWN,
82 MOUSEEVENTF_XDOWN
85 static const UINT button_up_flags[NB_BUTTONS] =
87 MOUSEEVENTF_LEFTUP,
88 MOUSEEVENTF_MIDDLEUP,
89 MOUSEEVENTF_RIGHTUP,
94 MOUSEEVENTF_XUP,
95 MOUSEEVENTF_XUP
98 static const UINT button_down_data[NB_BUTTONS] =
103 WHEEL_DELTA,
104 -WHEEL_DELTA,
105 -WHEEL_DELTA,
106 WHEEL_DELTA,
107 XBUTTON1,
108 XBUTTON2
111 static const UINT button_up_data[NB_BUTTONS] =
120 XBUTTON1,
121 XBUTTON2
124 XContext cursor_context = 0;
126 static HWND cursor_window;
127 static HCURSOR last_cursor;
128 static DWORD last_cursor_change;
129 static RECT last_clip_rect;
130 static HWND last_clip_foreground_window;
131 static BOOL last_clip_refused;
132 static RECT clip_rect;
133 static Cursor create_cursor( HANDLE handle );
135 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
136 static BOOL xinput2_available;
137 static BOOL broken_rawevents;
138 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
139 MAKE_FUNCPTR(XIGetClientPointer);
140 MAKE_FUNCPTR(XIFreeDeviceInfo);
141 MAKE_FUNCPTR(XIQueryDevice);
142 MAKE_FUNCPTR(XIQueryVersion);
143 MAKE_FUNCPTR(XISelectEvents);
144 #undef MAKE_FUNCPTR
145 #endif
147 /***********************************************************************
148 * X11DRV_Xcursor_Init
150 * Load the Xcursor library for use.
152 void X11DRV_Xcursor_Init(void)
154 #ifdef SONAME_LIBXCURSOR
155 xcursor_handle = dlopen(SONAME_LIBXCURSOR, RTLD_NOW);
156 if (!xcursor_handle)
158 WARN("Xcursor failed to load. Using fallback code.\n");
159 return;
161 #define LOAD_FUNCPTR(f) p##f = dlsym(xcursor_handle, #f)
163 LOAD_FUNCPTR(XcursorImageCreate);
164 LOAD_FUNCPTR(XcursorImageDestroy);
165 LOAD_FUNCPTR(XcursorImageLoadCursor);
166 LOAD_FUNCPTR(XcursorImagesCreate);
167 LOAD_FUNCPTR(XcursorImagesDestroy);
168 LOAD_FUNCPTR(XcursorImagesLoadCursor);
169 LOAD_FUNCPTR(XcursorLibraryLoadCursor);
170 #undef LOAD_FUNCPTR
171 #endif /* SONAME_LIBXCURSOR */
175 /***********************************************************************
176 * get_empty_cursor
178 static Cursor get_empty_cursor(void)
180 static Cursor cursor;
181 static const char data[] = { 0 };
183 if (!cursor)
185 XColor bg;
186 Pixmap pixmap;
188 bg.red = bg.green = bg.blue = 0x0000;
189 pixmap = XCreateBitmapFromData( gdi_display, root_window, data, 1, 1 );
190 if (pixmap)
192 Cursor new = XCreatePixmapCursor( gdi_display, pixmap, pixmap, &bg, &bg, 0, 0 );
193 if (InterlockedCompareExchangePointer( (void **)&cursor, (void *)new, 0 ))
194 XFreeCursor( gdi_display, new );
195 XFreePixmap( gdi_display, pixmap );
198 return cursor;
201 /***********************************************************************
202 * set_window_cursor
204 void set_window_cursor( Window window, HCURSOR handle )
206 Cursor cursor, prev;
208 if (!handle) cursor = get_empty_cursor();
209 else if (XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
211 /* try to create it */
212 if (!(cursor = create_cursor( handle ))) return;
214 XLockDisplay( gdi_display );
215 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&prev ))
217 /* someone else was here first */
218 XFreeCursor( gdi_display, cursor );
219 cursor = prev;
221 else
223 XSaveContext( gdi_display, (XID)handle, cursor_context, (char *)cursor );
224 TRACE( "cursor %p created %lx\n", handle, cursor );
226 XUnlockDisplay( gdi_display );
229 XDefineCursor( gdi_display, window, cursor );
230 /* make the change take effect immediately */
231 XFlush( gdi_display );
234 /***********************************************************************
235 * sync_window_cursor
237 void sync_window_cursor( Window window )
239 HCURSOR cursor;
241 SERVER_START_REQ( set_cursor )
243 req->flags = 0;
244 wine_server_call( req );
245 cursor = reply->prev_count >= 0 ? wine_server_ptr_handle( reply->prev_handle ) : 0;
247 SERVER_END_REQ;
249 set_window_cursor( window, cursor );
252 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
253 /***********************************************************************
254 * update_relative_valuators
256 static void update_relative_valuators(XIAnyClassInfo **valuators, int n_valuators)
258 struct x11drv_thread_data *thread_data = x11drv_thread_data();
259 int i;
261 thread_data->x_valuator.number = -1;
262 thread_data->y_valuator.number = -1;
264 for (i = 0; i < n_valuators; i++)
266 XIValuatorClassInfo *class = (XIValuatorClassInfo *)valuators[i];
267 if (valuators[i]->type != XIValuatorClass) continue;
268 if (class->label == x11drv_atom( Rel_X ) ||
269 (!class->label && class->number == 0 && class->mode == XIModeRelative))
270 thread_data->x_valuator = *class;
271 else if (class->label == x11drv_atom( Rel_Y ) ||
272 (!class->label && class->number == 1 && class->mode == XIModeRelative))
273 thread_data->y_valuator = *class;
276 thread_data->x_valuator.value = 0;
277 thread_data->y_valuator.value = 0;
281 /***********************************************************************
282 * enable_xinput2
284 static void enable_xinput2(void)
286 struct x11drv_thread_data *data = x11drv_thread_data();
287 XIEventMask mask;
288 XIDeviceInfo *pointer_info;
289 unsigned char mask_bits[XIMaskLen(XI_LASTEVENT)];
290 int count;
292 if (!xinput2_available) return;
294 if (data->xi2_state == xi_unknown)
296 int major = 2, minor = 0;
297 if (!pXIQueryVersion( data->display, &major, &minor )) data->xi2_state = xi_disabled;
298 else
300 data->xi2_state = xi_unavailable;
301 WARN( "X Input 2 not available\n" );
304 if (data->xi2_state == xi_unavailable) return;
305 if (!pXIGetClientPointer( data->display, None, &data->xi2_core_pointer )) return;
307 mask.mask = mask_bits;
308 mask.mask_len = sizeof(mask_bits);
309 mask.deviceid = XIAllDevices;
310 memset( mask_bits, 0, sizeof(mask_bits) );
311 XISetMask( mask_bits, XI_DeviceChanged );
312 XISetMask( mask_bits, XI_RawMotion );
313 XISetMask( mask_bits, XI_ButtonPress );
315 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
317 pointer_info = pXIQueryDevice( data->display, data->xi2_core_pointer, &count );
318 update_relative_valuators( pointer_info->classes, pointer_info->num_classes );
319 pXIFreeDeviceInfo( pointer_info );
321 /* This device info list is only used to find the initial current slave if
322 * no XI_DeviceChanged events happened. If any hierarchy change occurred that
323 * might be relevant here (eg. user switching mice after (un)plugging), a
324 * XI_DeviceChanged event will point us to the right slave. So this list is
325 * safe to be obtained statically at enable_xinput2() time.
327 if (data->xi2_devices) pXIFreeDeviceInfo( data->xi2_devices );
328 data->xi2_devices = pXIQueryDevice( data->display, XIAllDevices, &data->xi2_device_count );
329 data->xi2_current_slave = 0;
331 data->xi2_state = xi_enabled;
334 #endif
336 /***********************************************************************
337 * disable_xinput2
339 static void disable_xinput2(void)
341 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
342 struct x11drv_thread_data *data = x11drv_thread_data();
343 XIEventMask mask;
345 if (data->xi2_state != xi_enabled) return;
347 TRACE( "disabling\n" );
348 data->xi2_state = xi_disabled;
350 mask.mask = NULL;
351 mask.mask_len = 0;
352 mask.deviceid = XIAllDevices;
354 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
355 pXIFreeDeviceInfo( data->xi2_devices );
356 data->x_valuator.number = -1;
357 data->y_valuator.number = -1;
358 data->x_valuator.value = 0;
359 data->y_valuator.value = 0;
360 data->xi2_devices = NULL;
361 data->xi2_core_pointer = 0;
362 data->xi2_current_slave = 0;
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 )
374 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
375 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
376 struct x11drv_thread_data *data = x11drv_thread_data();
377 UNICODE_STRING class_name = RTL_CONSTANT_STRING( messageW );
378 Window clip_window;
379 HWND msg_hwnd = 0;
380 POINT pos;
382 if (NtUserGetWindowThread( NtUserGetDesktopWindow(), NULL ) == GetCurrentThreadId())
383 return TRUE; /* don't clip in the desktop process */
385 if (!data) return FALSE;
386 if (!(clip_window = init_clip_window())) return TRUE;
388 if (!(msg_hwnd = NtUserCreateWindowEx( 0, &class_name, &class_name, NULL, 0, 0, 0, 0, 0,
389 HWND_MESSAGE, 0, NtCurrentTeb()->Peb->ImageBaseAddress,
390 NULL, 0, NULL, 0, FALSE )))
391 return TRUE;
393 if (keyboard_grabbed)
395 WARN( "refusing to clip to %s\n", wine_dbgstr_rect(clip) );
396 last_clip_refused = TRUE;
397 last_clip_foreground_window = NtUserGetForegroundWindow();
398 last_clip_rect = *clip;
399 return FALSE;
401 else
403 last_clip_refused = FALSE;
406 /* enable XInput2 unless we are already clipping */
407 if (!data->clip_hwnd) enable_xinput2();
409 if (data->xi2_state != xi_enabled)
411 WARN( "XInput2 not supported, refusing to clip to %s\n", wine_dbgstr_rect(clip) );
412 NtUserDestroyWindow( msg_hwnd );
413 NtUserClipCursor( NULL );
414 return TRUE;
417 TRACE( "clipping to %s win %lx\n", wine_dbgstr_rect(clip), clip_window );
419 if (!data->clip_hwnd) XUnmapWindow( data->display, clip_window );
420 pos = virtual_screen_to_root( clip->left, clip->top );
421 XMoveResizeWindow( data->display, clip_window, pos.x, pos.y,
422 max( 1, clip->right - clip->left ), max( 1, clip->bottom - clip->top ) );
423 XMapWindow( data->display, clip_window );
425 /* if the rectangle is shrinking we may get a pointer warp */
426 if (!data->clip_hwnd || clip->left > clip_rect.left || clip->top > clip_rect.top ||
427 clip->right < clip_rect.right || clip->bottom < clip_rect.bottom)
428 data->warp_serial = NextRequest( data->display );
430 if (!XGrabPointer( data->display, clip_window, False,
431 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
432 GrabModeAsync, GrabModeAsync, clip_window, None, CurrentTime ))
433 clipping_cursor = TRUE;
435 if (!clipping_cursor)
437 disable_xinput2();
438 NtUserDestroyWindow( msg_hwnd );
439 return FALSE;
441 clip_rect = *clip;
442 if (!data->clip_hwnd) sync_window_cursor( clip_window );
443 InterlockedExchangePointer( (void **)&cursor_window, msg_hwnd );
444 data->clip_hwnd = msg_hwnd;
445 send_notify_message( NtUserGetDesktopWindow(), WM_X11DRV_CLIP_CURSOR_NOTIFY, 0, (LPARAM)msg_hwnd );
446 return TRUE;
447 #else
448 WARN( "XInput2 was not available at compile time\n" );
449 return FALSE;
450 #endif
453 /***********************************************************************
454 * ungrab_clipping_window
456 * Release the pointer grab on the clip window.
458 static void ungrab_clipping_window(void)
460 Display *display = thread_init_display();
461 Window clip_window = init_clip_window();
463 if (!clip_window) return;
465 TRACE( "no longer clipping\n" );
466 XUnmapWindow( display, clip_window );
467 if (clipping_cursor) XUngrabPointer( display, CurrentTime );
468 clipping_cursor = FALSE;
469 send_notify_message( NtUserGetDesktopWindow(), WM_X11DRV_CLIP_CURSOR_NOTIFY, 0, 0 );
472 /***********************************************************************
473 * retry_grab_clipping_window
475 * Restore the current clip rectangle or retry the last one if it has
476 * been refused because of an active keyboard grab.
478 void retry_grab_clipping_window(void)
480 if (clipping_cursor)
481 NtUserClipCursor( &clip_rect );
482 else if (last_clip_refused && NtUserGetForegroundWindow() == last_clip_foreground_window)
483 NtUserClipCursor( &last_clip_rect );
486 /***********************************************************************
487 * clip_cursor_notify
489 * Notification function called upon receiving a WM_X11DRV_CLIP_CURSOR_NOTIFY.
491 LRESULT clip_cursor_notify( HWND hwnd, HWND prev_clip_hwnd, HWND new_clip_hwnd )
493 struct x11drv_thread_data *data = x11drv_init_thread_data();
495 if (hwnd == NtUserGetDesktopWindow()) /* change the clip window stored in the desktop process */
497 static HWND clip_hwnd;
499 HWND prev = clip_hwnd;
500 clip_hwnd = new_clip_hwnd;
501 if (prev || new_clip_hwnd) TRACE( "clip hwnd changed from %p to %p\n", prev, new_clip_hwnd );
502 if (prev) send_notify_message( prev, WM_X11DRV_CLIP_CURSOR_NOTIFY, (WPARAM)prev, 0 );
504 else if (hwnd == data->clip_hwnd) /* this is a notification that clipping has been reset */
506 TRACE( "clip hwnd reset from %p\n", hwnd );
507 data->clip_hwnd = 0;
508 data->clip_reset = NtGetTickCount();
509 disable_xinput2();
510 NtUserDestroyWindow( hwnd );
512 else if (prev_clip_hwnd)
514 /* This is a notification send by the desktop window to an old
515 * dangling clip window.
517 TRACE( "destroying old clip hwnd %p\n", prev_clip_hwnd );
518 NtUserDestroyWindow( prev_clip_hwnd );
520 return 0;
523 /***********************************************************************
524 * clip_fullscreen_window
526 * Turn on clipping if the active window is fullscreen.
528 BOOL clip_fullscreen_window( HWND hwnd, BOOL reset )
530 struct x11drv_win_data *data;
531 struct x11drv_thread_data *thread_data;
532 MONITORINFO monitor_info;
533 HMONITOR monitor;
534 DWORD style;
535 BOOL fullscreen;
537 if (hwnd == NtUserGetDesktopWindow()) return FALSE;
538 style = NtUserGetWindowLongW( hwnd, GWL_STYLE );
539 if (!(style & WS_VISIBLE)) return FALSE;
540 if ((style & (WS_POPUP | WS_CHILD)) == WS_CHILD) return FALSE;
541 /* maximized windows don't count as full screen */
542 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION) return FALSE;
543 if (!(data = get_win_data( hwnd ))) return FALSE;
544 fullscreen = NtUserIsWindowRectFullScreen( &data->whole_rect );
545 release_win_data( data );
546 if (!fullscreen) return FALSE;
547 if (!(thread_data = x11drv_thread_data())) return FALSE;
548 if (NtGetTickCount() - thread_data->clip_reset < 1000) return FALSE;
549 if (!reset && clipping_cursor && thread_data->clip_hwnd) return FALSE; /* already clipping */
551 monitor = NtUserMonitorFromWindow( hwnd, MONITOR_DEFAULTTONEAREST );
552 if (!monitor) return FALSE;
553 monitor_info.cbSize = sizeof(monitor_info);
554 if (!NtUserGetMonitorInfo( monitor, &monitor_info )) return FALSE;
555 if (!grab_fullscreen)
557 RECT virtual_rect = NtUserGetVirtualScreenRect();
558 if (!EqualRect( &monitor_info.rcMonitor, &virtual_rect )) return FALSE;
559 if (is_virtual_desktop()) return FALSE;
561 TRACE( "win %p clipping fullscreen\n", hwnd );
562 return grab_clipping_window( &monitor_info.rcMonitor );
566 /***********************************************************************
567 * is_old_motion_event
569 static BOOL is_old_motion_event( unsigned long serial )
571 struct x11drv_thread_data *thread_data = x11drv_thread_data();
573 if (!thread_data->warp_serial) return FALSE;
574 if ((long)(serial - thread_data->warp_serial) < 0) return TRUE;
575 thread_data->warp_serial = 0; /* we caught up now */
576 return FALSE;
580 /***********************************************************************
581 * map_event_coords
583 * Map the input event coordinates so they're relative to the desktop.
585 static void map_event_coords( HWND hwnd, Window window, Window event_root, int x_root, int y_root, INPUT *input )
587 struct x11drv_thread_data *thread_data;
588 struct x11drv_win_data *data;
589 POINT pt = { input->u.mi.dx, input->u.mi.dy };
591 TRACE( "hwnd %p, window %lx, event_root %lx, x_root %d, y_root %d, input %p\n", hwnd, window, event_root,
592 x_root, y_root, input );
594 if (!hwnd)
596 thread_data = x11drv_thread_data();
597 if (!thread_data->clip_hwnd) return;
598 if (thread_data->clip_window != window) return;
599 pt.x += clip_rect.left;
600 pt.y += clip_rect.top;
602 else if ((data = get_win_data( hwnd )))
604 if (window == root_window) pt = root_to_virtual_screen( pt.x, pt.y );
605 else if (event_root == root_window) pt = root_to_virtual_screen( x_root, y_root );
606 else
608 if (window == data->whole_window)
610 pt.x += data->whole_rect.left - data->client_rect.left;
611 pt.y += data->whole_rect.top - data->client_rect.top;
614 if (NtUserGetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
615 pt.x = data->client_rect.right - data->client_rect.left - 1 - pt.x;
616 NtUserMapWindowPoints( hwnd, 0, &pt, 1 );
618 release_win_data( data );
621 TRACE( "mapped %s to %s\n", wine_dbgstr_point( (POINT *)&input->u.mi.dx ), wine_dbgstr_point( &pt ) );
623 input->u.mi.dx = pt.x;
624 input->u.mi.dy = pt.y;
627 /***********************************************************************
628 * send_mouse_input
630 * Update the various window states on a mouse event.
632 static void send_mouse_input( HWND hwnd, Window window, unsigned int state, INPUT *input )
634 struct x11drv_win_data *data;
635 Window win = 0;
637 input->type = INPUT_MOUSE;
639 if (!hwnd)
641 struct x11drv_thread_data *thread_data = x11drv_thread_data();
642 HWND clip_hwnd = thread_data->clip_hwnd;
644 if (!clip_hwnd) return;
645 if (thread_data->clip_window != window) return;
646 if (InterlockedExchangePointer( (void **)&cursor_window, clip_hwnd ) != clip_hwnd ||
647 input->u.mi.time - last_cursor_change > 100)
649 sync_window_cursor( window );
650 last_cursor_change = input->u.mi.time;
652 __wine_send_input( hwnd, input, NULL );
653 return;
656 if (!(data = get_win_data( hwnd ))) return;
657 win = data->whole_window;
658 release_win_data( data );
659 if (InterlockedExchangePointer( (void **)&cursor_window, hwnd ) != hwnd ||
660 input->u.mi.time - last_cursor_change > 100)
662 sync_window_cursor( win );
663 last_cursor_change = input->u.mi.time;
666 if (hwnd != NtUserGetDesktopWindow())
668 hwnd = NtUserGetAncestor( hwnd, GA_ROOT );
669 if ((input->u.mi.dwFlags & (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_RIGHTDOWN)) && hwnd == NtUserGetForegroundWindow())
670 clip_fullscreen_window( hwnd, FALSE );
673 /* update the wine server Z-order */
675 if (hwnd != x11drv_thread_data()->grab_hwnd &&
676 /* ignore event if a button is pressed, since the mouse is then grabbed too */
677 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
679 RECT rect = { input->u.mi.dx, input->u.mi.dy, input->u.mi.dx + 1, input->u.mi.dy + 1 };
681 SERVER_START_REQ( update_window_zorder )
683 req->window = wine_server_user_handle( hwnd );
684 req->rect.left = rect.left;
685 req->rect.top = rect.top;
686 req->rect.right = rect.right;
687 req->rect.bottom = rect.bottom;
688 wine_server_call( req );
690 SERVER_END_REQ;
693 __wine_send_input( hwnd, input, NULL );
696 #ifdef SONAME_LIBXCURSOR
698 /***********************************************************************
699 * create_xcursor_frame
701 * Use Xcursor to create a frame of an X cursor from a Windows one.
703 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
704 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
705 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
706 int width, int height, int istep )
708 XcursorImage *image, *ret = NULL;
709 DWORD delay_jiffies, num_steps;
710 int x, y, i;
711 BOOL has_alpha = FALSE;
712 XcursorPixel *ptr;
714 image = pXcursorImageCreate( width, height );
715 if (!image)
717 ERR("X11 failed to produce a cursor frame!\n");
718 return NULL;
721 image->xhot = iinfo->xHotspot;
722 image->yhot = iinfo->yHotspot;
724 image->delay = 100; /* fallback delay, 100 ms */
725 if (NtUserGetCursorFrameInfo(icon, istep, &delay_jiffies, &num_steps) != 0)
726 image->delay = (100 * delay_jiffies) / 6; /* convert jiffies (1/60s) to milliseconds */
727 else
728 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep);
730 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
731 memset( color_bits, 0x00, color_size );
732 NtGdiSelectBitmap( hdc, hbmColor );
733 if (!NtUserDrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
735 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
736 goto cleanup;
738 memcpy( image->pixels, color_bits, color_size );
740 /* check if the cursor frame was drawn with an alpha channel */
741 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
742 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
744 /* if no alpha channel was drawn then generate it from the mask */
745 if (!has_alpha)
747 unsigned int width_bytes = (width + 31) / 32 * 4;
749 /* draw the cursor mask to a temporary buffer */
750 memset( mask_bits, 0xFF, mask_size );
751 NtGdiSelectBitmap( hdc, hbmMask );
752 if (!NtUserDrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
754 ERR("Failed to draw frame mask %d.\n", istep);
755 goto cleanup;
757 /* use the buffer to directly modify the XcursorImage alpha channel */
758 for (y = 0, ptr = image->pixels; y < height; y++)
759 for (x = 0; x < width; x++, ptr++)
760 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
761 *ptr |= 0xff000000;
763 ret = image;
765 cleanup:
766 if (ret == NULL) pXcursorImageDestroy( image );
767 return ret;
770 /***********************************************************************
771 * create_xcursor_cursor
773 * Use Xcursor to create an X cursor from a Windows one.
775 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
777 unsigned char *color_bits, *mask_bits;
778 HBITMAP hbmColor = 0, hbmMask = 0;
779 DWORD nFrames, delay_jiffies, i;
780 int color_size, mask_size;
781 BITMAPINFO *info = NULL;
782 XcursorImages *images;
783 XcursorImage **imgs;
784 Cursor cursor = 0;
786 /* Retrieve the number of frames to render */
787 if (!NtUserGetCursorFrameInfo(icon, 0, &delay_jiffies, &nFrames)) return 0;
788 if (!(imgs = calloc( 1, sizeof(XcursorImage*) * nFrames ))) return 0;
790 /* Allocate all of the resources necessary to obtain a cursor frame */
791 if (!(info = malloc( FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
792 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
793 info->bmiHeader.biWidth = width;
794 info->bmiHeader.biHeight = -height;
795 info->bmiHeader.biPlanes = 1;
796 info->bmiHeader.biCompression = BI_RGB;
797 info->bmiHeader.biXPelsPerMeter = 0;
798 info->bmiHeader.biYPelsPerMeter = 0;
799 info->bmiHeader.biClrUsed = 0;
800 info->bmiHeader.biClrImportant = 0;
801 info->bmiHeader.biBitCount = 32;
802 color_size = width * height * 4;
803 info->bmiHeader.biSizeImage = color_size;
804 hbmColor = NtGdiCreateDIBSection( hdc, NULL, 0, info, DIB_RGB_COLORS, 0, 0, 0, (void **)&color_bits );
805 if (!hbmColor)
807 ERR("Failed to create DIB section for cursor color data!\n");
808 goto cleanup;
810 info->bmiHeader.biBitCount = 1;
811 info->bmiColors[0].rgbRed = 0;
812 info->bmiColors[0].rgbGreen = 0;
813 info->bmiColors[0].rgbBlue = 0;
814 info->bmiColors[0].rgbReserved = 0;
815 info->bmiColors[1].rgbRed = 0xff;
816 info->bmiColors[1].rgbGreen = 0xff;
817 info->bmiColors[1].rgbBlue = 0xff;
818 info->bmiColors[1].rgbReserved = 0;
820 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
821 info->bmiHeader.biSizeImage = mask_size;
822 hbmMask = NtGdiCreateDIBSection( hdc, NULL, 0, info, DIB_RGB_COLORS, 0, 0, 0, (void **)&mask_bits );
823 if (!hbmMask)
825 ERR("Failed to create DIB section for cursor mask data!\n");
826 goto cleanup;
829 /* Create an XcursorImage for each frame of the cursor */
830 for (i=0; i<nFrames; i++)
832 imgs[i] = create_xcursor_frame( hdc, iinfo, icon,
833 hbmColor, color_bits, color_size,
834 hbmMask, mask_bits, mask_size,
835 width, height, i );
836 if (!imgs[i]) goto cleanup;
839 /* Build an X cursor out of all of the frames */
840 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
841 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
842 images->images[images->nimage] = imgs[images->nimage];
843 cursor = pXcursorImagesLoadCursor( gdi_display, images );
844 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
845 free( imgs );
846 imgs = NULL;
848 cleanup:
849 if (imgs)
851 /* Failed to produce a cursor, free previously allocated frames */
852 for (i=0; i<nFrames && imgs[i]; i++)
853 pXcursorImageDestroy( imgs[i] );
854 free( imgs );
856 /* Cleanup all of the resources used to obtain the frame data */
857 if (hbmColor) NtGdiDeleteObjectApp( hbmColor );
858 if (hbmMask) NtGdiDeleteObjectApp( hbmMask );
859 free( info );
860 return cursor;
863 #endif /* SONAME_LIBXCURSOR */
866 struct system_cursors
868 WORD id;
869 const char *names[8];
872 static const struct system_cursors user32_cursors[] =
874 { OCR_NORMAL, { "left_ptr" }},
875 { OCR_IBEAM, { "xterm", "text" }},
876 { OCR_WAIT, { "watch", "wait" }},
877 { OCR_CROSS, { "cross" }},
878 { OCR_UP, { "center_ptr" }},
879 { OCR_SIZE, { "fleur", "size_all" }},
880 { OCR_SIZEALL, { "fleur", "size_all" }},
881 { OCR_ICON, { "icon" }},
882 { OCR_SIZENWSE, { "top_left_corner", "nw-resize" }},
883 { OCR_SIZENESW, { "top_right_corner", "ne-resize" }},
884 { OCR_SIZEWE, { "h_double_arrow", "size_hor", "ew-resize" }},
885 { OCR_SIZENS, { "v_double_arrow", "size_ver", "ns-resize" }},
886 { OCR_NO, { "not-allowed", "forbidden", "no-drop" }},
887 { OCR_HAND, { "hand2", "pointer", "pointing-hand" }},
888 { OCR_APPSTARTING, { "left_ptr_watch" }},
889 { OCR_HELP, { "question_arrow", "help" }},
890 { 0 }
893 static const struct system_cursors comctl32_cursors[] =
895 { 102, { "move", "dnd-move" }},
896 { 104, { "copy", "dnd-copy" }},
897 { 105, { "left_ptr" }},
898 { 106, { "col-resize", "split_v" }},
899 { 107, { "col-resize", "split_v" }},
900 { 108, { "hand2", "pointer", "pointing-hand" }},
901 { 135, { "row-resize", "split_h" }},
902 { 0 }
905 static const struct system_cursors ole32_cursors[] =
907 { 1, { "no-drop", "dnd-no-drop" }},
908 { 2, { "move", "dnd-move" }},
909 { 3, { "copy", "dnd-copy" }},
910 { 4, { "alias", "dnd-link" }},
911 { 0 }
914 static const struct system_cursors riched20_cursors[] =
916 { 105, { "hand2", "pointer", "pointing-hand" }},
917 { 107, { "right_ptr" }},
918 { 109, { "copy", "dnd-copy" }},
919 { 110, { "move", "dnd-move" }},
920 { 111, { "no-drop", "dnd-no-drop" }},
921 { 0 }
924 static const struct
926 const struct system_cursors *cursors;
927 WCHAR name[16];
928 } module_cursors[] =
930 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
931 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
932 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
933 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
936 struct cursor_font_fallback
938 const char *name;
939 unsigned int shape;
942 static const struct cursor_font_fallback fallbacks[] =
944 { "X_cursor", XC_X_cursor },
945 { "arrow", XC_arrow },
946 { "based_arrow_down", XC_based_arrow_down },
947 { "based_arrow_up", XC_based_arrow_up },
948 { "boat", XC_boat },
949 { "bogosity", XC_bogosity },
950 { "bottom_left_corner", XC_bottom_left_corner },
951 { "bottom_right_corner", XC_bottom_right_corner },
952 { "bottom_side", XC_bottom_side },
953 { "bottom_tee", XC_bottom_tee },
954 { "box_spiral", XC_box_spiral },
955 { "center_ptr", XC_center_ptr },
956 { "circle", XC_circle },
957 { "clock", XC_clock },
958 { "coffee_mug", XC_coffee_mug },
959 { "col-resize", XC_sb_h_double_arrow },
960 { "cross", XC_cross },
961 { "cross_reverse", XC_cross_reverse },
962 { "crosshair", XC_crosshair },
963 { "diamond_cross", XC_diamond_cross },
964 { "dot", XC_dot },
965 { "dotbox", XC_dotbox },
966 { "double_arrow", XC_double_arrow },
967 { "draft_large", XC_draft_large },
968 { "draft_small", XC_draft_small },
969 { "draped_box", XC_draped_box },
970 { "exchange", XC_exchange },
971 { "fleur", XC_fleur },
972 { "gobbler", XC_gobbler },
973 { "gumby", XC_gumby },
974 { "h_double_arrow", XC_sb_h_double_arrow },
975 { "hand1", XC_hand1 },
976 { "hand2", XC_hand2 },
977 { "heart", XC_heart },
978 { "icon", XC_icon },
979 { "iron_cross", XC_iron_cross },
980 { "left_ptr", XC_left_ptr },
981 { "left_side", XC_left_side },
982 { "left_tee", XC_left_tee },
983 { "leftbutton", XC_leftbutton },
984 { "ll_angle", XC_ll_angle },
985 { "lr_angle", XC_lr_angle },
986 { "man", XC_man },
987 { "middlebutton", XC_middlebutton },
988 { "mouse", XC_mouse },
989 { "pencil", XC_pencil },
990 { "pirate", XC_pirate },
991 { "plus", XC_plus },
992 { "question_arrow", XC_question_arrow },
993 { "right_ptr", XC_right_ptr },
994 { "right_side", XC_right_side },
995 { "right_tee", XC_right_tee },
996 { "rightbutton", XC_rightbutton },
997 { "row-resize", XC_sb_v_double_arrow },
998 { "rtl_logo", XC_rtl_logo },
999 { "sailboat", XC_sailboat },
1000 { "sb_down_arrow", XC_sb_down_arrow },
1001 { "sb_h_double_arrow", XC_sb_h_double_arrow },
1002 { "sb_left_arrow", XC_sb_left_arrow },
1003 { "sb_right_arrow", XC_sb_right_arrow },
1004 { "sb_up_arrow", XC_sb_up_arrow },
1005 { "sb_v_double_arrow", XC_sb_v_double_arrow },
1006 { "shuttle", XC_shuttle },
1007 { "sizing", XC_sizing },
1008 { "spider", XC_spider },
1009 { "spraycan", XC_spraycan },
1010 { "star", XC_star },
1011 { "target", XC_target },
1012 { "tcross", XC_tcross },
1013 { "top_left_arrow", XC_top_left_arrow },
1014 { "top_left_corner", XC_top_left_corner },
1015 { "top_right_corner", XC_top_right_corner },
1016 { "top_side", XC_top_side },
1017 { "top_tee", XC_top_tee },
1018 { "trek", XC_trek },
1019 { "ul_angle", XC_ul_angle },
1020 { "umbrella", XC_umbrella },
1021 { "ur_angle", XC_ur_angle },
1022 { "v_double_arrow", XC_sb_v_double_arrow },
1023 { "watch", XC_watch },
1024 { "xterm", XC_xterm }
1027 static int fallback_cmp( const void *key, const void *member )
1029 const struct cursor_font_fallback *fallback = member;
1030 return strcmp( key, fallback->name );
1033 static int find_fallback_shape( const char *name )
1035 struct cursor_font_fallback *fallback;
1037 if ((fallback = bsearch( name, fallbacks, ARRAY_SIZE( fallbacks ),
1038 sizeof(*fallback), fallback_cmp )))
1039 return fallback->shape;
1040 return -1;
1043 /***********************************************************************
1044 * create_xcursor_system_cursor
1046 * Create an X cursor for a system cursor.
1048 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
1050 const struct system_cursors *cursors;
1051 const WCHAR *module;
1052 unsigned int i;
1053 Cursor cursor = 0;
1054 HKEY key;
1055 const char * const *names = NULL;
1056 WCHAR *p, name[MAX_PATH * 2];
1057 char valueA[64];
1059 if (!info->szModName[0]) return 0;
1061 p = wcsrchr( info->szModName, '\\' );
1062 wcscpy( name, p ? p + 1 : info->szModName );
1063 p = name + lstrlenW( name );
1064 *p++ = ',';
1065 if (info->szResName[0]) wcscpy( p, info->szResName );
1066 else
1068 char buf[16];
1069 sprintf( buf, "%hu", info->wResID );
1070 asciiz_to_unicode( p, buf );
1072 valueA[0] = 0;
1074 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
1075 if ((key = open_hkcu_key( "Software\\Wine\\X11 Driver\\Cursors" )))
1077 char buffer[4096];
1078 KEY_VALUE_PARTIAL_INFORMATION *value = (void *)buffer;
1079 DWORD size = query_reg_value( key, NULL, value, sizeof(buffer) );
1080 NtClose( key );
1081 if (size && value->Type == REG_SZ)
1083 const WCHAR *valueW = (const WCHAR *)value->Data;
1084 if (!valueW[0]) return 0; /* force standard cursor */
1085 if (!ntdll_wcstoumbs( valueW, lstrlenW(valueW) + 1, valueA, sizeof(valueA), FALSE ))
1086 valueA[0] = 0;
1087 goto done;
1091 if (info->szResName[0]) goto done; /* only integer resources are supported here */
1093 if ((module = wcsrchr( info->szModName, '\\' ))) module++;
1094 else module = info->szModName;
1095 for (i = 0; i < ARRAY_SIZE( module_cursors ); i++)
1096 if (!wcsicmp( module, module_cursors[i].name )) break;
1097 if (i == ARRAY_SIZE( module_cursors )) goto done;
1099 cursors = module_cursors[i].cursors;
1100 for (i = 0; cursors[i].id; i++)
1101 if (cursors[i].id == info->wResID)
1103 strcpy( valueA, cursors[i].names[0] );
1104 names = cursors[i].names;
1105 break;
1108 done:
1109 if (valueA[0])
1111 #ifdef SONAME_LIBXCURSOR
1112 if (pXcursorLibraryLoadCursor)
1114 if (!names)
1115 cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
1116 else
1117 while (*names && !cursor) cursor = pXcursorLibraryLoadCursor( gdi_display, *names++ );
1119 #endif
1120 if (!cursor)
1122 int shape = find_fallback_shape( valueA );
1123 if (shape != -1) cursor = XCreateFontCursor( gdi_display, shape );
1125 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
1126 debugstr_w(name), debugstr_a(valueA) );
1128 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
1129 return cursor;
1133 /***********************************************************************
1134 * create_xlib_monochrome_cursor
1136 * Create a monochrome X cursor from a Windows one.
1138 static Cursor create_xlib_monochrome_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1140 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1141 BITMAPINFO *info = (BITMAPINFO *)buffer;
1142 const int and_y = 0;
1143 const int xor_y = height;
1144 unsigned int width_bytes = (width + 31) / 32 * 4;
1145 unsigned char *mask_bits = NULL;
1146 GC gc;
1147 XColor fg, bg;
1148 XVisualInfo vis = default_visual;
1149 Pixmap src_pixmap, bits_pixmap, mask_pixmap;
1150 struct gdi_image_bits bits;
1151 Cursor cursor = 0;
1153 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1154 info->bmiHeader.biWidth = width;
1155 info->bmiHeader.biHeight = -height * 2;
1156 info->bmiHeader.biPlanes = 1;
1157 info->bmiHeader.biBitCount = 1;
1158 info->bmiHeader.biCompression = BI_RGB;
1159 info->bmiHeader.biSizeImage = width_bytes * height * 2;
1160 info->bmiHeader.biXPelsPerMeter = 0;
1161 info->bmiHeader.biYPelsPerMeter = 0;
1162 info->bmiHeader.biClrUsed = 0;
1163 info->bmiHeader.biClrImportant = 0;
1165 if (!(mask_bits = malloc( info->bmiHeader.biSizeImage ))) goto done;
1166 if (!NtGdiGetDIBitsInternal( hdc, icon->hbmMask, 0, height * 2, mask_bits, info,
1167 DIB_RGB_COLORS, 0, 0 )) goto done;
1169 vis.depth = 1;
1170 bits.ptr = mask_bits;
1171 bits.free = NULL;
1172 bits.is_copy = TRUE;
1173 if (!(src_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1175 bits_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
1176 mask_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
1177 gc = XCreateGC( gdi_display, src_pixmap, 0, NULL );
1178 XSetGraphicsExposures( gdi_display, gc, False );
1180 /* We have to do some magic here, as cursors are not fully
1181 * compatible between Windows and X11. Under X11, there are
1182 * only 3 possible color cursor: black, white and masked. So
1183 * we map the 4th Windows color (invert the bits on the screen)
1184 * to black and an additional white bit on another place
1185 * (+1,+1). This require some boolean arithmetic:
1187 * Windows | X11
1188 * And Xor Result | Bits Mask Result
1189 * 0 0 black | 0 1 background
1190 * 0 1 white | 1 1 foreground
1191 * 1 0 no change | X 0 no change
1192 * 1 1 inverted | 0 1 background
1194 * which gives:
1195 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
1196 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
1198 XSetFunction( gdi_display, gc, GXcopy );
1199 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 0, 0 );
1200 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 0, 0 );
1201 XSetFunction( gdi_display, gc, GXandReverse );
1202 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, xor_y, width, height, 0, 0 );
1203 XSetFunction( gdi_display, gc, GXorReverse );
1204 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, xor_y, width, height, 0, 0 );
1205 /* additional white */
1206 XSetFunction( gdi_display, gc, GXand );
1207 XCopyArea( gdi_display, src_pixmap, src_pixmap, gc, 0, xor_y, width, height, 0, and_y );
1208 XSetFunction( gdi_display, gc, GXor );
1209 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 1, 1 );
1210 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 1, 1 );
1211 XFreeGC( gdi_display, gc );
1213 fg.red = fg.green = fg.blue = 0xffff;
1214 bg.red = bg.green = bg.blue = 0;
1215 cursor = XCreatePixmapCursor( gdi_display, bits_pixmap, mask_pixmap,
1216 &fg, &bg, icon->xHotspot, icon->yHotspot );
1217 XFreePixmap( gdi_display, src_pixmap );
1218 XFreePixmap( gdi_display, bits_pixmap );
1219 XFreePixmap( gdi_display, mask_pixmap );
1221 done:
1222 free( mask_bits );
1223 return cursor;
1226 static BOOL get_icon_info( HICON handle, ICONINFOEXW *ret )
1228 UNICODE_STRING module, res_name;
1229 ICONINFO info;
1231 module.Buffer = ret->szModName;
1232 module.MaximumLength = sizeof(ret->szModName) - sizeof(WCHAR);
1233 res_name.Buffer = ret->szResName;
1234 res_name.MaximumLength = sizeof(ret->szResName) - sizeof(WCHAR);
1235 if (!NtUserGetIconInfo( handle, &info, &module, &res_name, NULL, 0 )) return FALSE;
1236 ret->fIcon = info.fIcon;
1237 ret->xHotspot = info.xHotspot;
1238 ret->yHotspot = info.yHotspot;
1239 ret->hbmColor = info.hbmColor;
1240 ret->hbmMask = info.hbmMask;
1241 ret->wResID = res_name.Length ? 0 : LOWORD( res_name.Buffer );
1242 ret->szModName[module.Length] = 0;
1243 ret->szResName[res_name.Length] = 0;
1244 return TRUE;
1247 /***********************************************************************
1248 * create_xlib_load_mono_cursor
1250 * Create a monochrome X cursor from a color Windows one by trying to load the monochrome resource.
1252 static Cursor create_xlib_load_mono_cursor( HDC hdc, HANDLE handle, int width, int height )
1254 Cursor cursor = None;
1255 HANDLE mono;
1256 ICONINFOEXW info;
1257 BITMAP bm;
1259 if (!(mono = CopyImage( handle, IMAGE_CURSOR, width, height, LR_MONOCHROME | LR_COPYFROMRESOURCE )))
1260 return None;
1262 if (get_icon_info( mono, &info ))
1264 if (!info.hbmColor)
1266 NtGdiExtGetObjectW( info.hbmMask, sizeof(bm), &bm );
1267 bm.bmHeight = max( 1, bm.bmHeight / 2 );
1268 /* make sure hotspot is valid */
1269 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1271 info.xHotspot = bm.bmWidth / 2;
1272 info.yHotspot = bm.bmHeight / 2;
1274 cursor = create_xlib_monochrome_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1276 else NtGdiDeleteObjectApp( info.hbmColor );
1277 NtGdiDeleteObjectApp( info.hbmMask );
1279 NtUserDestroyCursor( mono, 0 );
1280 return cursor;
1283 /***********************************************************************
1284 * create_xlib_color_cursor
1286 * Create a color X cursor from a Windows one.
1288 static Cursor create_xlib_color_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1290 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1291 BITMAPINFO *info = (BITMAPINFO *)buffer;
1292 XColor fg, bg;
1293 Cursor cursor = None;
1294 XVisualInfo vis = default_visual;
1295 Pixmap xor_pixmap, mask_pixmap;
1296 struct gdi_image_bits bits;
1297 unsigned int *color_bits = NULL, *ptr;
1298 unsigned char *mask_bits = NULL, *xor_bits = NULL;
1299 int i, x, y;
1300 BOOL has_alpha = FALSE;
1301 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
1302 unsigned int width_bytes = (width + 31) / 32 * 4;
1304 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1305 info->bmiHeader.biWidth = width;
1306 info->bmiHeader.biHeight = -height;
1307 info->bmiHeader.biPlanes = 1;
1308 info->bmiHeader.biBitCount = 1;
1309 info->bmiHeader.biCompression = BI_RGB;
1310 info->bmiHeader.biSizeImage = width_bytes * height;
1311 info->bmiHeader.biXPelsPerMeter = 0;
1312 info->bmiHeader.biYPelsPerMeter = 0;
1313 info->bmiHeader.biClrUsed = 0;
1314 info->bmiHeader.biClrImportant = 0;
1316 if (!(mask_bits = malloc( info->bmiHeader.biSizeImage ))) goto done;
1317 if (!NtGdiGetDIBitsInternal( hdc, icon->hbmMask, 0, height, mask_bits, info,
1318 DIB_RGB_COLORS, 0, 0 )) goto done;
1320 info->bmiHeader.biBitCount = 32;
1321 info->bmiHeader.biSizeImage = width * height * 4;
1322 if (!(color_bits = malloc( info->bmiHeader.biSizeImage ))) goto done;
1323 if (!(xor_bits = calloc( 1, width_bytes * height ))) goto done;
1324 NtGdiGetDIBitsInternal( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS, 0, 0 );
1326 /* compute fg/bg color and xor bitmap based on average of the color values */
1328 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
1329 for (y = 0, ptr = color_bits; y < height; y++)
1331 for (x = 0; x < width; x++, ptr++)
1333 int red = (*ptr >> 16) & 0xff;
1334 int green = (*ptr >> 8) & 0xff;
1335 int blue = (*ptr >> 0) & 0xff;
1336 if (red + green + blue > 0x40)
1338 rfg += red;
1339 gfg += green;
1340 bfg += blue;
1341 fgBits++;
1342 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1344 else
1346 rbg += red;
1347 gbg += green;
1348 bbg += blue;
1352 if (fgBits)
1354 fg.red = rfg * 257 / fgBits;
1355 fg.green = gfg * 257 / fgBits;
1356 fg.blue = bfg * 257 / fgBits;
1358 else fg.red = fg.green = fg.blue = 0;
1359 bgBits = width * height - fgBits;
1360 if (bgBits)
1362 bg.red = rbg * 257 / bgBits;
1363 bg.green = gbg * 257 / bgBits;
1364 bg.blue = bbg * 257 / bgBits;
1366 else bg.red = bg.green = bg.blue = 0;
1368 info->bmiHeader.biBitCount = 1;
1369 info->bmiHeader.biClrUsed = 0;
1370 info->bmiHeader.biSizeImage = width_bytes * height;
1372 /* generate mask from the alpha channel if we have one */
1374 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
1375 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
1377 if (has_alpha)
1379 memset( mask_bits, 0, width_bytes * height );
1380 for (y = 0, ptr = color_bits; y < height; y++)
1381 for (x = 0; x < width; x++, ptr++)
1382 if ((*ptr >> 24) > 25) /* more than 10% alpha */
1383 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1385 else /* invert the mask */
1387 unsigned int j;
1389 ptr = (unsigned int *)mask_bits;
1390 for (j = 0; j < info->bmiHeader.biSizeImage / sizeof(*ptr); j++, ptr++) *ptr ^= ~0u;
1393 vis.depth = 1;
1394 bits.ptr = xor_bits;
1395 bits.free = NULL;
1396 bits.is_copy = TRUE;
1397 if (!(xor_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1399 bits.ptr = mask_bits;
1400 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
1402 if (mask_pixmap)
1404 cursor = XCreatePixmapCursor( gdi_display, xor_pixmap, mask_pixmap,
1405 &fg, &bg, icon->xHotspot, icon->yHotspot );
1406 XFreePixmap( gdi_display, mask_pixmap );
1408 XFreePixmap( gdi_display, xor_pixmap );
1410 done:
1411 free( color_bits );
1412 free( xor_bits );
1413 free( mask_bits );
1414 return cursor;
1417 /***********************************************************************
1418 * create_cursor
1420 * Create an X cursor from a Windows one.
1422 static Cursor create_cursor( HANDLE handle )
1424 Cursor cursor = 0;
1425 ICONINFOEXW info;
1426 BITMAP bm;
1427 HDC hdc;
1429 if (!handle) return get_empty_cursor();
1431 if (!get_icon_info( handle, &info )) return 0;
1433 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
1435 NtGdiDeleteObjectApp( info.hbmColor );
1436 NtGdiDeleteObjectApp( info.hbmMask );
1437 return cursor;
1440 NtGdiExtGetObjectW( info.hbmMask, sizeof(bm), &bm );
1441 if (!info.hbmColor) bm.bmHeight = max( 1, bm.bmHeight / 2 );
1443 /* make sure hotspot is valid */
1444 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1446 info.xHotspot = bm.bmWidth / 2;
1447 info.yHotspot = bm.bmHeight / 2;
1450 hdc = NtGdiCreateCompatibleDC( 0 );
1452 if (info.hbmColor)
1454 #ifdef SONAME_LIBXCURSOR
1455 if (pXcursorImagesLoadCursor)
1456 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
1457 #endif
1458 if (!cursor) cursor = create_xlib_load_mono_cursor( hdc, handle, bm.bmWidth, bm.bmHeight );
1459 if (!cursor) cursor = create_xlib_color_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1460 NtGdiDeleteObjectApp( info.hbmColor );
1462 else
1464 cursor = create_xlib_monochrome_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1467 NtGdiDeleteObjectApp( info.hbmMask );
1468 NtGdiDeleteObjectApp( hdc );
1469 return cursor;
1472 /***********************************************************************
1473 * DestroyCursorIcon (X11DRV.@)
1475 void X11DRV_DestroyCursorIcon( HCURSOR handle )
1477 Cursor cursor;
1479 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1481 TRACE( "%p xid %lx\n", handle, cursor );
1482 XFreeCursor( gdi_display, cursor );
1483 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1487 /***********************************************************************
1488 * SetCursor (X11DRV.@)
1490 void X11DRV_SetCursor( HCURSOR handle )
1492 if (InterlockedExchangePointer( (void **)&last_cursor, handle ) != handle ||
1493 NtGetTickCount() - last_cursor_change > 100)
1495 last_cursor_change = NtGetTickCount();
1496 if (cursor_window) send_notify_message( cursor_window, WM_X11DRV_SET_CURSOR,
1497 GetCurrentThreadId(), (LPARAM)handle );
1501 /***********************************************************************
1502 * SetCursorPos (X11DRV.@)
1504 BOOL X11DRV_SetCursorPos( INT x, INT y )
1506 struct x11drv_thread_data *data = x11drv_init_thread_data();
1507 POINT pos = virtual_screen_to_root( x, y );
1509 if (keyboard_grabbed)
1511 WARN( "refusing to warp to %u, %u\n", (int)pos.x, (int)pos.y );
1512 return FALSE;
1515 if (!clipping_cursor &&
1516 XGrabPointer( data->display, root_window, False,
1517 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1518 GrabModeAsync, GrabModeAsync, None, None, CurrentTime ) != GrabSuccess)
1520 WARN( "refusing to warp pointer to %u, %u without exclusive grab\n", (int)pos.x, (int)pos.y );
1521 return FALSE;
1524 XWarpPointer( data->display, root_window, root_window, 0, 0, 0, 0, pos.x, pos.y );
1525 data->warp_serial = NextRequest( data->display );
1527 if (!clipping_cursor)
1528 XUngrabPointer( data->display, CurrentTime );
1530 XNoOp( data->display );
1531 XFlush( data->display ); /* avoids bad mouse lag in games that do their own mouse warping */
1532 TRACE( "warped to %d,%d serial %lu\n", x, y, data->warp_serial );
1533 return TRUE;
1536 /***********************************************************************
1537 * GetCursorPos (X11DRV.@)
1539 BOOL X11DRV_GetCursorPos(LPPOINT pos)
1541 Display *display = thread_init_display();
1542 Window root, child;
1543 int rootX, rootY, winX, winY;
1544 unsigned int xstate;
1545 BOOL ret;
1547 ret = XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &winX, &winY, &xstate );
1548 if (ret)
1550 POINT old = *pos;
1551 *pos = root_to_virtual_screen( winX, winY );
1552 TRACE( "pointer at %s server pos %s\n", wine_dbgstr_point(pos), wine_dbgstr_point(&old) );
1554 return ret;
1557 /***********************************************************************
1558 * ClipCursor (X11DRV.@)
1560 BOOL X11DRV_ClipCursor( LPCRECT clip )
1562 RECT virtual_rect = NtUserGetVirtualScreenRect();
1564 if (!clip) clip = &virtual_rect;
1566 if (grab_pointer)
1568 HWND foreground = NtUserGetForegroundWindow();
1569 DWORD tid, pid;
1571 /* forward request to the foreground window if it's in a different thread */
1572 tid = NtUserGetWindowThread( foreground, &pid );
1573 if (tid && tid != GetCurrentThreadId() && pid == GetCurrentProcessId())
1575 TRACE( "forwarding clip request to %p\n", foreground );
1576 send_notify_message( foreground, WM_X11DRV_CLIP_CURSOR_REQUEST, FALSE, FALSE );
1577 return TRUE;
1580 /* we are clipping if the clip rectangle is smaller than the screen */
1581 if (clip->left > virtual_rect.left || clip->right < virtual_rect.right ||
1582 clip->top > virtual_rect.top || clip->bottom < virtual_rect.bottom)
1584 if (grab_clipping_window( clip )) return TRUE;
1586 else /* if currently clipping, check if we should switch to fullscreen clipping */
1588 struct x11drv_thread_data *data = x11drv_thread_data();
1589 if (data && data->clip_hwnd)
1591 if (EqualRect( clip, &clip_rect ) || clip_fullscreen_window( foreground, TRUE ))
1592 return TRUE;
1596 ungrab_clipping_window();
1597 return TRUE;
1600 /***********************************************************************
1601 * clip_cursor_request
1603 * Function called upon receiving a WM_X11DRV_CLIP_CURSOR_REQUEST.
1605 LRESULT clip_cursor_request( HWND hwnd, BOOL fullscreen, BOOL reset )
1607 RECT clip;
1609 if (hwnd == NtUserGetDesktopWindow())
1610 WARN( "ignoring clip cursor request on desktop window.\n" );
1611 else if (hwnd != NtUserGetForegroundWindow())
1612 WARN( "ignoring clip cursor request on non-foreground window.\n" );
1613 else if (fullscreen)
1614 clip_fullscreen_window( hwnd, reset );
1615 else
1617 NtUserGetClipCursor( &clip );
1618 X11DRV_ClipCursor( &clip );
1621 return 0;
1624 /***********************************************************************
1625 * move_resize_window
1627 void move_resize_window( HWND hwnd, int dir )
1629 Display *display = thread_display();
1630 DWORD pt;
1631 POINT pos;
1632 int button = 0;
1633 XEvent xev;
1634 Window win, root, child;
1635 unsigned int xstate;
1637 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1639 pt = NtUserGetThreadInfo()->message_pos;
1640 pos = virtual_screen_to_root( (short)LOWORD( pt ), (short)HIWORD( pt ) );
1642 if (NtUserGetKeyState( VK_LBUTTON ) & 0x8000) button = 1;
1643 else if (NtUserGetKeyState( VK_MBUTTON ) & 0x8000) button = 2;
1644 else if (NtUserGetKeyState( VK_RBUTTON ) & 0x8000) button = 3;
1646 TRACE( "hwnd %p/%lx, pos %s, dir %d, button %d\n", hwnd, win, wine_dbgstr_point(&pos), dir, button );
1648 xev.xclient.type = ClientMessage;
1649 xev.xclient.window = win;
1650 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1651 xev.xclient.serial = 0;
1652 xev.xclient.display = display;
1653 xev.xclient.send_event = True;
1654 xev.xclient.format = 32;
1655 xev.xclient.data.l[0] = pos.x; /* x coord */
1656 xev.xclient.data.l[1] = pos.y; /* y coord */
1657 xev.xclient.data.l[2] = dir; /* direction */
1658 xev.xclient.data.l[3] = button; /* button */
1659 xev.xclient.data.l[4] = 0; /* unused */
1661 /* need to ungrab the pointer that may have been automatically grabbed
1662 * with a ButtonPress event */
1663 XUngrabPointer( display, CurrentTime );
1664 XSendEvent(display, root_window, False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
1666 /* try to detect the end of the size/move by polling for the mouse button to be released */
1667 /* (some apps don't like it if we return before the size/move is done) */
1669 if (!button) return;
1670 send_message( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1672 for (;;)
1674 MSG msg;
1675 INPUT input;
1676 int x, y, rootX, rootY;
1678 if (!XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &x, &y, &xstate )) break;
1680 if (!(xstate & (Button1Mask << (button - 1))))
1682 /* fake a button release event */
1683 pos = root_to_virtual_screen( x, y );
1684 input.type = INPUT_MOUSE;
1685 input.u.mi.dx = pos.x;
1686 input.u.mi.dy = pos.y;
1687 input.u.mi.mouseData = button_up_data[button - 1];
1688 input.u.mi.dwFlags = button_up_flags[button - 1] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1689 input.u.mi.time = NtGetTickCount();
1690 input.u.mi.dwExtraInfo = 0;
1691 __wine_send_input( hwnd, &input, NULL );
1694 while (NtUserPeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
1696 if (!NtUserCallMsgFilter( &msg, MSGF_SIZE ))
1698 NtUserTranslateMessage( &msg, 0 );
1699 NtUserDispatchMessage( &msg );
1703 if (!(xstate & (Button1Mask << (button - 1)))) break;
1704 NtUserMsgWaitForMultipleObjectsEx( 0, NULL, 100, QS_ALLINPUT, 0 );
1707 TRACE( "hwnd %p/%lx done\n", hwnd, win );
1708 send_message( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1712 /***********************************************************************
1713 * X11DRV_ButtonPress
1715 BOOL X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1717 XButtonEvent *event = &xev->xbutton;
1718 int buttonNum = event->button - 1;
1719 INPUT input;
1721 if (buttonNum >= NB_BUTTONS) return FALSE;
1723 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1725 input.u.mi.dx = event->x;
1726 input.u.mi.dy = event->y;
1727 input.u.mi.mouseData = button_down_data[buttonNum];
1728 input.u.mi.dwFlags = button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1729 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1730 input.u.mi.dwExtraInfo = 0;
1732 update_user_time( event->time );
1733 map_event_coords( hwnd, event->window, event->root, event->x_root, event->y_root, &input );
1734 send_mouse_input( hwnd, event->window, event->state, &input );
1735 return TRUE;
1739 /***********************************************************************
1740 * X11DRV_ButtonRelease
1742 BOOL X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1744 XButtonEvent *event = &xev->xbutton;
1745 int buttonNum = event->button - 1;
1746 INPUT input;
1748 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return FALSE;
1750 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1752 input.u.mi.dx = event->x;
1753 input.u.mi.dy = event->y;
1754 input.u.mi.mouseData = button_up_data[buttonNum];
1755 input.u.mi.dwFlags = button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1756 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1757 input.u.mi.dwExtraInfo = 0;
1759 map_event_coords( hwnd, event->window, event->root, event->x_root, event->y_root, &input );
1760 send_mouse_input( hwnd, event->window, event->state, &input );
1761 return TRUE;
1765 /***********************************************************************
1766 * X11DRV_MotionNotify
1768 BOOL X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1770 XMotionEvent *event = &xev->xmotion;
1771 INPUT input;
1773 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d serial %lu\n",
1774 hwnd, event->window, event->x, event->y, event->is_hint, event->serial );
1776 input.u.mi.dx = event->x;
1777 input.u.mi.dy = event->y;
1778 input.u.mi.mouseData = 0;
1779 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1780 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1781 input.u.mi.dwExtraInfo = 0;
1783 if (!hwnd && is_old_motion_event( event->serial ))
1785 TRACE( "pos %d,%d old serial %lu, ignoring\n", event->x, event->y, event->serial );
1786 return FALSE;
1788 map_event_coords( hwnd, event->window, event->root, event->x_root, event->y_root, &input );
1789 send_mouse_input( hwnd, event->window, event->state, &input );
1790 return TRUE;
1794 /***********************************************************************
1795 * X11DRV_EnterNotify
1797 BOOL X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1799 XCrossingEvent *event = &xev->xcrossing;
1800 INPUT input;
1802 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd, event->window, event->x, event->y, event->detail );
1804 if (hwnd == x11drv_thread_data()->grab_hwnd) return FALSE;
1806 /* simulate a mouse motion event */
1807 input.u.mi.dx = event->x;
1808 input.u.mi.dy = event->y;
1809 input.u.mi.mouseData = 0;
1810 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1811 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1812 input.u.mi.dwExtraInfo = 0;
1814 if (is_old_motion_event( event->serial ))
1816 TRACE( "pos %d,%d old serial %lu, ignoring\n", event->x, event->y, event->serial );
1817 return FALSE;
1819 map_event_coords( hwnd, event->window, event->root, event->x_root, event->y_root, &input );
1820 send_mouse_input( hwnd, event->window, event->state, &input );
1821 return TRUE;
1824 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1826 /***********************************************************************
1827 * X11DRV_DeviceChanged
1829 static BOOL X11DRV_DeviceChanged( XGenericEventCookie *xev )
1831 XIDeviceChangedEvent *event = xev->data;
1832 struct x11drv_thread_data *data = x11drv_thread_data();
1834 if (event->deviceid != data->xi2_core_pointer) return FALSE;
1835 if (event->reason != XISlaveSwitch) return FALSE;
1837 update_relative_valuators( event->classes, event->num_classes );
1838 data->xi2_current_slave = event->sourceid;
1839 return TRUE;
1842 static BOOL map_raw_event_coords( XIRawEvent *event, INPUT *input )
1844 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1845 XIValuatorClassInfo *x = &thread_data->x_valuator, *y = &thread_data->y_valuator;
1846 double x_value = 0, y_value = 0, x_scale, y_scale;
1847 const double *values = event->valuators.values;
1848 RECT virtual_rect;
1849 int i;
1851 if (x->number < 0 || y->number < 0) return FALSE;
1852 if (!event->valuators.mask_len) return FALSE;
1853 if (thread_data->xi2_state != xi_enabled) return FALSE;
1855 /* If there is no slave currently detected, no previous motion nor device
1856 * change events were received. Look it up now on the device list in this
1857 * case.
1859 if (!thread_data->xi2_current_slave)
1861 XIDeviceInfo *devices = thread_data->xi2_devices;
1863 for (i = 0; i < thread_data->xi2_device_count; i++)
1865 if (devices[i].use != XISlavePointer) continue;
1866 if (devices[i].deviceid != event->deviceid) continue;
1867 if (devices[i].attachment != thread_data->xi2_core_pointer) continue;
1868 thread_data->xi2_current_slave = event->deviceid;
1869 break;
1872 if (event->deviceid != thread_data->xi2_current_slave) return FALSE;
1874 virtual_rect = NtUserGetVirtualScreenRect();
1876 if (x->max <= x->min) x_scale = 1;
1877 else x_scale = (virtual_rect.right - virtual_rect.left) / (x->max - x->min);
1878 if (y->max <= y->min) y_scale = 1;
1879 else y_scale = (virtual_rect.bottom - virtual_rect.top) / (y->max - y->min);
1881 for (i = 0; i <= max( x->number, y->number ); i++)
1883 if (!XIMaskIsSet( event->valuators.mask, i )) continue;
1884 if (i == x->number)
1886 x_value = *values;
1887 x->value += x_value * x_scale;
1889 if (i == y->number)
1891 y_value = *values;
1892 y->value += y_value * y_scale;
1894 values++;
1897 input->u.mi.dx = round( x->value );
1898 input->u.mi.dy = round( y->value );
1900 TRACE( "event %f,%f value %f,%f input %d,%d\n", x_value, y_value, x->value, y->value,
1901 (int)input->u.mi.dx, (int)input->u.mi.dy );
1903 x->value -= input->u.mi.dx;
1904 y->value -= input->u.mi.dy;
1906 if (!input->u.mi.dx && !input->u.mi.dy)
1908 TRACE( "accumulating motion\n" );
1909 return FALSE;
1912 return TRUE;
1915 /***********************************************************************
1916 * X11DRV_RawMotion
1918 static BOOL X11DRV_RawMotion( XGenericEventCookie *xev )
1920 XIRawEvent *event = xev->data;
1921 INPUT input;
1923 if (broken_rawevents && is_old_motion_event( xev->serial ))
1925 TRACE( "old serial %lu, ignoring\n", xev->serial );
1926 return FALSE;
1929 input.type = INPUT_MOUSE;
1930 input.u.mi.mouseData = 0;
1931 input.u.mi.dwFlags = MOUSEEVENTF_MOVE;
1932 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1933 input.u.mi.dwExtraInfo = 0;
1934 input.u.mi.dx = 0;
1935 input.u.mi.dy = 0;
1936 if (!map_raw_event_coords( event, &input )) return FALSE;
1938 __wine_send_input( 0, &input, NULL );
1939 return TRUE;
1942 #endif /* HAVE_X11_EXTENSIONS_XINPUT2_H */
1945 /***********************************************************************
1946 * X11DRV_XInput2_Init
1948 void X11DRV_XInput2_Init(void)
1950 #if defined(SONAME_LIBXI) && defined(HAVE_X11_EXTENSIONS_XINPUT2_H)
1951 int event, error;
1952 void *libxi_handle = dlopen( SONAME_LIBXI, RTLD_NOW );
1954 if (!libxi_handle)
1956 WARN( "couldn't load %s\n", SONAME_LIBXI );
1957 return;
1959 #define LOAD_FUNCPTR(f) \
1960 if (!(p##f = dlsym( libxi_handle, #f))) \
1962 WARN("Failed to load %s.\n", #f); \
1963 return; \
1966 LOAD_FUNCPTR(XIGetClientPointer);
1967 LOAD_FUNCPTR(XIFreeDeviceInfo);
1968 LOAD_FUNCPTR(XIQueryDevice);
1969 LOAD_FUNCPTR(XIQueryVersion);
1970 LOAD_FUNCPTR(XISelectEvents);
1971 #undef LOAD_FUNCPTR
1973 xinput2_available = XQueryExtension( gdi_display, "XInputExtension", &xinput2_opcode, &event, &error );
1975 /* Until version 1.10.4 rawinput was broken in XOrg, see
1976 * https://bugs.freedesktop.org/show_bug.cgi?id=30068 */
1977 broken_rawevents = strstr(XServerVendor( gdi_display ), "X.Org") &&
1978 XVendorRelease( gdi_display ) < 11004000;
1980 #else
1981 TRACE( "X Input 2 support not compiled in.\n" );
1982 #endif
1986 /***********************************************************************
1987 * X11DRV_GenericEvent
1989 BOOL X11DRV_GenericEvent( HWND hwnd, XEvent *xev )
1991 BOOL ret = FALSE;
1992 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1993 XGenericEventCookie *event = &xev->xcookie;
1995 if (!event->data) return FALSE;
1996 if (event->extension != xinput2_opcode) return FALSE;
1998 switch (event->evtype)
2000 case XI_DeviceChanged:
2001 ret = X11DRV_DeviceChanged( event );
2002 break;
2003 case XI_RawMotion:
2004 ret = X11DRV_RawMotion( event );
2005 break;
2007 default:
2008 TRACE( "Unhandled event %#x\n", event->evtype );
2009 break;
2011 #endif
2012 return ret;