winex11: Rely on win32u to reset clipping on display mode change.
[wine.git] / dlls / winex11.drv / mouse.c
blobb17e8b12d25ab525c2fbae68ad34484d6d4cedcf
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 * reset_clipping_window
475 * Forcibly reset the window clipping on external events.
477 void reset_clipping_window(void)
479 ungrab_clipping_window();
480 NtUserClipCursor( NULL ); /* make sure the clip rectangle is reset too */
483 /***********************************************************************
484 * retry_grab_clipping_window
486 * Restore the current clip rectangle or retry the last one if it has
487 * been refused because of an active keyboard grab.
489 void retry_grab_clipping_window(void)
491 if (clipping_cursor)
492 NtUserClipCursor( &clip_rect );
493 else if (last_clip_refused && NtUserGetForegroundWindow() == last_clip_foreground_window)
494 NtUserClipCursor( &last_clip_rect );
497 /***********************************************************************
498 * clip_cursor_notify
500 * Notification function called upon receiving a WM_X11DRV_CLIP_CURSOR_NOTIFY.
502 LRESULT clip_cursor_notify( HWND hwnd, HWND prev_clip_hwnd, HWND new_clip_hwnd )
504 struct x11drv_thread_data *data = x11drv_init_thread_data();
506 if (hwnd == NtUserGetDesktopWindow()) /* change the clip window stored in the desktop process */
508 static HWND clip_hwnd;
510 HWND prev = clip_hwnd;
511 clip_hwnd = new_clip_hwnd;
512 if (prev || new_clip_hwnd) TRACE( "clip hwnd changed from %p to %p\n", prev, new_clip_hwnd );
513 if (prev) send_notify_message( prev, WM_X11DRV_CLIP_CURSOR_NOTIFY, (WPARAM)prev, 0 );
515 else if (hwnd == data->clip_hwnd) /* this is a notification that clipping has been reset */
517 TRACE( "clip hwnd reset from %p\n", hwnd );
518 data->clip_hwnd = 0;
519 data->clip_reset = NtGetTickCount();
520 disable_xinput2();
521 NtUserDestroyWindow( hwnd );
523 else if (prev_clip_hwnd)
525 /* This is a notification send by the desktop window to an old
526 * dangling clip window.
528 TRACE( "destroying old clip hwnd %p\n", prev_clip_hwnd );
529 NtUserDestroyWindow( prev_clip_hwnd );
531 return 0;
534 /***********************************************************************
535 * clip_fullscreen_window
537 * Turn on clipping if the active window is fullscreen.
539 BOOL clip_fullscreen_window( HWND hwnd, BOOL reset )
541 struct x11drv_win_data *data;
542 struct x11drv_thread_data *thread_data;
543 MONITORINFO monitor_info;
544 HMONITOR monitor;
545 DWORD style;
546 BOOL fullscreen;
548 if (hwnd == NtUserGetDesktopWindow()) return FALSE;
549 style = NtUserGetWindowLongW( hwnd, GWL_STYLE );
550 if (!(style & WS_VISIBLE)) return FALSE;
551 if ((style & (WS_POPUP | WS_CHILD)) == WS_CHILD) return FALSE;
552 /* maximized windows don't count as full screen */
553 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION) return FALSE;
554 if (!(data = get_win_data( hwnd ))) return FALSE;
555 fullscreen = NtUserIsWindowRectFullScreen( &data->whole_rect );
556 release_win_data( data );
557 if (!fullscreen) return FALSE;
558 if (!(thread_data = x11drv_thread_data())) return FALSE;
559 if (NtGetTickCount() - thread_data->clip_reset < 1000) return FALSE;
560 if (!reset && clipping_cursor && thread_data->clip_hwnd) return FALSE; /* already clipping */
562 monitor = NtUserMonitorFromWindow( hwnd, MONITOR_DEFAULTTONEAREST );
563 if (!monitor) return FALSE;
564 monitor_info.cbSize = sizeof(monitor_info);
565 if (!NtUserGetMonitorInfo( monitor, &monitor_info )) return FALSE;
566 if (!grab_fullscreen)
568 RECT virtual_rect = NtUserGetVirtualScreenRect();
569 if (!EqualRect( &monitor_info.rcMonitor, &virtual_rect )) return FALSE;
570 if (is_virtual_desktop()) return FALSE;
572 TRACE( "win %p clipping fullscreen\n", hwnd );
573 return grab_clipping_window( &monitor_info.rcMonitor );
577 /***********************************************************************
578 * is_old_motion_event
580 static BOOL is_old_motion_event( unsigned long serial )
582 struct x11drv_thread_data *thread_data = x11drv_thread_data();
584 if (!thread_data->warp_serial) return FALSE;
585 if ((long)(serial - thread_data->warp_serial) < 0) return TRUE;
586 thread_data->warp_serial = 0; /* we caught up now */
587 return FALSE;
591 /***********************************************************************
592 * map_event_coords
594 * Map the input event coordinates so they're relative to the desktop.
596 static void map_event_coords( HWND hwnd, Window window, Window event_root, int x_root, int y_root, INPUT *input )
598 struct x11drv_thread_data *thread_data;
599 struct x11drv_win_data *data;
600 POINT pt = { input->u.mi.dx, input->u.mi.dy };
602 TRACE( "hwnd %p, window %lx, event_root %lx, x_root %d, y_root %d, input %p\n", hwnd, window, event_root,
603 x_root, y_root, input );
605 if (!hwnd)
607 thread_data = x11drv_thread_data();
608 if (!thread_data->clip_hwnd) return;
609 if (thread_data->clip_window != window) return;
610 pt.x += clip_rect.left;
611 pt.y += clip_rect.top;
613 else if ((data = get_win_data( hwnd )))
615 if (window == root_window) pt = root_to_virtual_screen( pt.x, pt.y );
616 else if (event_root == root_window) pt = root_to_virtual_screen( x_root, y_root );
617 else
619 if (window == data->whole_window)
621 pt.x += data->whole_rect.left - data->client_rect.left;
622 pt.y += data->whole_rect.top - data->client_rect.top;
625 if (NtUserGetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
626 pt.x = data->client_rect.right - data->client_rect.left - 1 - pt.x;
627 NtUserMapWindowPoints( hwnd, 0, &pt, 1 );
629 release_win_data( data );
632 TRACE( "mapped %s to %s\n", wine_dbgstr_point( (POINT *)&input->u.mi.dx ), wine_dbgstr_point( &pt ) );
634 input->u.mi.dx = pt.x;
635 input->u.mi.dy = pt.y;
638 /***********************************************************************
639 * send_mouse_input
641 * Update the various window states on a mouse event.
643 static void send_mouse_input( HWND hwnd, Window window, unsigned int state, INPUT *input )
645 struct x11drv_win_data *data;
646 Window win = 0;
648 input->type = INPUT_MOUSE;
650 if (!hwnd)
652 struct x11drv_thread_data *thread_data = x11drv_thread_data();
653 HWND clip_hwnd = thread_data->clip_hwnd;
655 if (!clip_hwnd) return;
656 if (thread_data->clip_window != window) return;
657 if (InterlockedExchangePointer( (void **)&cursor_window, clip_hwnd ) != clip_hwnd ||
658 input->u.mi.time - last_cursor_change > 100)
660 sync_window_cursor( window );
661 last_cursor_change = input->u.mi.time;
663 __wine_send_input( hwnd, input, NULL );
664 return;
667 if (!(data = get_win_data( hwnd ))) return;
668 win = data->whole_window;
669 release_win_data( data );
670 if (InterlockedExchangePointer( (void **)&cursor_window, hwnd ) != hwnd ||
671 input->u.mi.time - last_cursor_change > 100)
673 sync_window_cursor( win );
674 last_cursor_change = input->u.mi.time;
677 if (hwnd != NtUserGetDesktopWindow())
679 hwnd = NtUserGetAncestor( hwnd, GA_ROOT );
680 if ((input->u.mi.dwFlags & (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_RIGHTDOWN)) && hwnd == NtUserGetForegroundWindow())
681 clip_fullscreen_window( hwnd, FALSE );
684 /* update the wine server Z-order */
686 if (hwnd != x11drv_thread_data()->grab_hwnd &&
687 /* ignore event if a button is pressed, since the mouse is then grabbed too */
688 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
690 RECT rect = { input->u.mi.dx, input->u.mi.dy, input->u.mi.dx + 1, input->u.mi.dy + 1 };
692 SERVER_START_REQ( update_window_zorder )
694 req->window = wine_server_user_handle( hwnd );
695 req->rect.left = rect.left;
696 req->rect.top = rect.top;
697 req->rect.right = rect.right;
698 req->rect.bottom = rect.bottom;
699 wine_server_call( req );
701 SERVER_END_REQ;
704 __wine_send_input( hwnd, input, NULL );
707 #ifdef SONAME_LIBXCURSOR
709 /***********************************************************************
710 * create_xcursor_frame
712 * Use Xcursor to create a frame of an X cursor from a Windows one.
714 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
715 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
716 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
717 int width, int height, int istep )
719 XcursorImage *image, *ret = NULL;
720 DWORD delay_jiffies, num_steps;
721 int x, y, i;
722 BOOL has_alpha = FALSE;
723 XcursorPixel *ptr;
725 image = pXcursorImageCreate( width, height );
726 if (!image)
728 ERR("X11 failed to produce a cursor frame!\n");
729 return NULL;
732 image->xhot = iinfo->xHotspot;
733 image->yhot = iinfo->yHotspot;
735 image->delay = 100; /* fallback delay, 100 ms */
736 if (NtUserGetCursorFrameInfo(icon, istep, &delay_jiffies, &num_steps) != 0)
737 image->delay = (100 * delay_jiffies) / 6; /* convert jiffies (1/60s) to milliseconds */
738 else
739 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep);
741 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
742 memset( color_bits, 0x00, color_size );
743 NtGdiSelectBitmap( hdc, hbmColor );
744 if (!NtUserDrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
746 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
747 goto cleanup;
749 memcpy( image->pixels, color_bits, color_size );
751 /* check if the cursor frame was drawn with an alpha channel */
752 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
753 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
755 /* if no alpha channel was drawn then generate it from the mask */
756 if (!has_alpha)
758 unsigned int width_bytes = (width + 31) / 32 * 4;
760 /* draw the cursor mask to a temporary buffer */
761 memset( mask_bits, 0xFF, mask_size );
762 NtGdiSelectBitmap( hdc, hbmMask );
763 if (!NtUserDrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
765 ERR("Failed to draw frame mask %d.\n", istep);
766 goto cleanup;
768 /* use the buffer to directly modify the XcursorImage alpha channel */
769 for (y = 0, ptr = image->pixels; y < height; y++)
770 for (x = 0; x < width; x++, ptr++)
771 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
772 *ptr |= 0xff000000;
774 ret = image;
776 cleanup:
777 if (ret == NULL) pXcursorImageDestroy( image );
778 return ret;
781 /***********************************************************************
782 * create_xcursor_cursor
784 * Use Xcursor to create an X cursor from a Windows one.
786 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
788 unsigned char *color_bits, *mask_bits;
789 HBITMAP hbmColor = 0, hbmMask = 0;
790 DWORD nFrames, delay_jiffies, i;
791 int color_size, mask_size;
792 BITMAPINFO *info = NULL;
793 XcursorImages *images;
794 XcursorImage **imgs;
795 Cursor cursor = 0;
797 /* Retrieve the number of frames to render */
798 if (!NtUserGetCursorFrameInfo(icon, 0, &delay_jiffies, &nFrames)) return 0;
799 if (!(imgs = calloc( 1, sizeof(XcursorImage*) * nFrames ))) return 0;
801 /* Allocate all of the resources necessary to obtain a cursor frame */
802 if (!(info = malloc( FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
803 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
804 info->bmiHeader.biWidth = width;
805 info->bmiHeader.biHeight = -height;
806 info->bmiHeader.biPlanes = 1;
807 info->bmiHeader.biCompression = BI_RGB;
808 info->bmiHeader.biXPelsPerMeter = 0;
809 info->bmiHeader.biYPelsPerMeter = 0;
810 info->bmiHeader.biClrUsed = 0;
811 info->bmiHeader.biClrImportant = 0;
812 info->bmiHeader.biBitCount = 32;
813 color_size = width * height * 4;
814 info->bmiHeader.biSizeImage = color_size;
815 hbmColor = NtGdiCreateDIBSection( hdc, NULL, 0, info, DIB_RGB_COLORS, 0, 0, 0, (void **)&color_bits );
816 if (!hbmColor)
818 ERR("Failed to create DIB section for cursor color data!\n");
819 goto cleanup;
821 info->bmiHeader.biBitCount = 1;
822 info->bmiColors[0].rgbRed = 0;
823 info->bmiColors[0].rgbGreen = 0;
824 info->bmiColors[0].rgbBlue = 0;
825 info->bmiColors[0].rgbReserved = 0;
826 info->bmiColors[1].rgbRed = 0xff;
827 info->bmiColors[1].rgbGreen = 0xff;
828 info->bmiColors[1].rgbBlue = 0xff;
829 info->bmiColors[1].rgbReserved = 0;
831 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
832 info->bmiHeader.biSizeImage = mask_size;
833 hbmMask = NtGdiCreateDIBSection( hdc, NULL, 0, info, DIB_RGB_COLORS, 0, 0, 0, (void **)&mask_bits );
834 if (!hbmMask)
836 ERR("Failed to create DIB section for cursor mask data!\n");
837 goto cleanup;
840 /* Create an XcursorImage for each frame of the cursor */
841 for (i=0; i<nFrames; i++)
843 imgs[i] = create_xcursor_frame( hdc, iinfo, icon,
844 hbmColor, color_bits, color_size,
845 hbmMask, mask_bits, mask_size,
846 width, height, i );
847 if (!imgs[i]) goto cleanup;
850 /* Build an X cursor out of all of the frames */
851 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
852 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
853 images->images[images->nimage] = imgs[images->nimage];
854 cursor = pXcursorImagesLoadCursor( gdi_display, images );
855 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
856 free( imgs );
857 imgs = NULL;
859 cleanup:
860 if (imgs)
862 /* Failed to produce a cursor, free previously allocated frames */
863 for (i=0; i<nFrames && imgs[i]; i++)
864 pXcursorImageDestroy( imgs[i] );
865 free( imgs );
867 /* Cleanup all of the resources used to obtain the frame data */
868 if (hbmColor) NtGdiDeleteObjectApp( hbmColor );
869 if (hbmMask) NtGdiDeleteObjectApp( hbmMask );
870 free( info );
871 return cursor;
874 #endif /* SONAME_LIBXCURSOR */
877 struct system_cursors
879 WORD id;
880 const char *names[8];
883 static const struct system_cursors user32_cursors[] =
885 { OCR_NORMAL, { "left_ptr" }},
886 { OCR_IBEAM, { "xterm", "text" }},
887 { OCR_WAIT, { "watch", "wait" }},
888 { OCR_CROSS, { "cross" }},
889 { OCR_UP, { "center_ptr" }},
890 { OCR_SIZE, { "fleur", "size_all" }},
891 { OCR_SIZEALL, { "fleur", "size_all" }},
892 { OCR_ICON, { "icon" }},
893 { OCR_SIZENWSE, { "top_left_corner", "nw-resize" }},
894 { OCR_SIZENESW, { "top_right_corner", "ne-resize" }},
895 { OCR_SIZEWE, { "h_double_arrow", "size_hor", "ew-resize" }},
896 { OCR_SIZENS, { "v_double_arrow", "size_ver", "ns-resize" }},
897 { OCR_NO, { "not-allowed", "forbidden", "no-drop" }},
898 { OCR_HAND, { "hand2", "pointer", "pointing-hand" }},
899 { OCR_APPSTARTING, { "left_ptr_watch" }},
900 { OCR_HELP, { "question_arrow", "help" }},
901 { 0 }
904 static const struct system_cursors comctl32_cursors[] =
906 { 102, { "move", "dnd-move" }},
907 { 104, { "copy", "dnd-copy" }},
908 { 105, { "left_ptr" }},
909 { 106, { "col-resize", "split_v" }},
910 { 107, { "col-resize", "split_v" }},
911 { 108, { "hand2", "pointer", "pointing-hand" }},
912 { 135, { "row-resize", "split_h" }},
913 { 0 }
916 static const struct system_cursors ole32_cursors[] =
918 { 1, { "no-drop", "dnd-no-drop" }},
919 { 2, { "move", "dnd-move" }},
920 { 3, { "copy", "dnd-copy" }},
921 { 4, { "alias", "dnd-link" }},
922 { 0 }
925 static const struct system_cursors riched20_cursors[] =
927 { 105, { "hand2", "pointer", "pointing-hand" }},
928 { 107, { "right_ptr" }},
929 { 109, { "copy", "dnd-copy" }},
930 { 110, { "move", "dnd-move" }},
931 { 111, { "no-drop", "dnd-no-drop" }},
932 { 0 }
935 static const struct
937 const struct system_cursors *cursors;
938 WCHAR name[16];
939 } module_cursors[] =
941 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
942 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
943 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
944 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
947 struct cursor_font_fallback
949 const char *name;
950 unsigned int shape;
953 static const struct cursor_font_fallback fallbacks[] =
955 { "X_cursor", XC_X_cursor },
956 { "arrow", XC_arrow },
957 { "based_arrow_down", XC_based_arrow_down },
958 { "based_arrow_up", XC_based_arrow_up },
959 { "boat", XC_boat },
960 { "bogosity", XC_bogosity },
961 { "bottom_left_corner", XC_bottom_left_corner },
962 { "bottom_right_corner", XC_bottom_right_corner },
963 { "bottom_side", XC_bottom_side },
964 { "bottom_tee", XC_bottom_tee },
965 { "box_spiral", XC_box_spiral },
966 { "center_ptr", XC_center_ptr },
967 { "circle", XC_circle },
968 { "clock", XC_clock },
969 { "coffee_mug", XC_coffee_mug },
970 { "col-resize", XC_sb_h_double_arrow },
971 { "cross", XC_cross },
972 { "cross_reverse", XC_cross_reverse },
973 { "crosshair", XC_crosshair },
974 { "diamond_cross", XC_diamond_cross },
975 { "dot", XC_dot },
976 { "dotbox", XC_dotbox },
977 { "double_arrow", XC_double_arrow },
978 { "draft_large", XC_draft_large },
979 { "draft_small", XC_draft_small },
980 { "draped_box", XC_draped_box },
981 { "exchange", XC_exchange },
982 { "fleur", XC_fleur },
983 { "gobbler", XC_gobbler },
984 { "gumby", XC_gumby },
985 { "h_double_arrow", XC_sb_h_double_arrow },
986 { "hand1", XC_hand1 },
987 { "hand2", XC_hand2 },
988 { "heart", XC_heart },
989 { "icon", XC_icon },
990 { "iron_cross", XC_iron_cross },
991 { "left_ptr", XC_left_ptr },
992 { "left_side", XC_left_side },
993 { "left_tee", XC_left_tee },
994 { "leftbutton", XC_leftbutton },
995 { "ll_angle", XC_ll_angle },
996 { "lr_angle", XC_lr_angle },
997 { "man", XC_man },
998 { "middlebutton", XC_middlebutton },
999 { "mouse", XC_mouse },
1000 { "pencil", XC_pencil },
1001 { "pirate", XC_pirate },
1002 { "plus", XC_plus },
1003 { "question_arrow", XC_question_arrow },
1004 { "right_ptr", XC_right_ptr },
1005 { "right_side", XC_right_side },
1006 { "right_tee", XC_right_tee },
1007 { "rightbutton", XC_rightbutton },
1008 { "row-resize", XC_sb_v_double_arrow },
1009 { "rtl_logo", XC_rtl_logo },
1010 { "sailboat", XC_sailboat },
1011 { "sb_down_arrow", XC_sb_down_arrow },
1012 { "sb_h_double_arrow", XC_sb_h_double_arrow },
1013 { "sb_left_arrow", XC_sb_left_arrow },
1014 { "sb_right_arrow", XC_sb_right_arrow },
1015 { "sb_up_arrow", XC_sb_up_arrow },
1016 { "sb_v_double_arrow", XC_sb_v_double_arrow },
1017 { "shuttle", XC_shuttle },
1018 { "sizing", XC_sizing },
1019 { "spider", XC_spider },
1020 { "spraycan", XC_spraycan },
1021 { "star", XC_star },
1022 { "target", XC_target },
1023 { "tcross", XC_tcross },
1024 { "top_left_arrow", XC_top_left_arrow },
1025 { "top_left_corner", XC_top_left_corner },
1026 { "top_right_corner", XC_top_right_corner },
1027 { "top_side", XC_top_side },
1028 { "top_tee", XC_top_tee },
1029 { "trek", XC_trek },
1030 { "ul_angle", XC_ul_angle },
1031 { "umbrella", XC_umbrella },
1032 { "ur_angle", XC_ur_angle },
1033 { "v_double_arrow", XC_sb_v_double_arrow },
1034 { "watch", XC_watch },
1035 { "xterm", XC_xterm }
1038 static int fallback_cmp( const void *key, const void *member )
1040 const struct cursor_font_fallback *fallback = member;
1041 return strcmp( key, fallback->name );
1044 static int find_fallback_shape( const char *name )
1046 struct cursor_font_fallback *fallback;
1048 if ((fallback = bsearch( name, fallbacks, ARRAY_SIZE( fallbacks ),
1049 sizeof(*fallback), fallback_cmp )))
1050 return fallback->shape;
1051 return -1;
1054 /***********************************************************************
1055 * create_xcursor_system_cursor
1057 * Create an X cursor for a system cursor.
1059 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
1061 const struct system_cursors *cursors;
1062 const WCHAR *module;
1063 unsigned int i;
1064 Cursor cursor = 0;
1065 HKEY key;
1066 const char * const *names = NULL;
1067 WCHAR *p, name[MAX_PATH * 2];
1068 char valueA[64];
1070 if (!info->szModName[0]) return 0;
1072 p = wcsrchr( info->szModName, '\\' );
1073 wcscpy( name, p ? p + 1 : info->szModName );
1074 p = name + lstrlenW( name );
1075 *p++ = ',';
1076 if (info->szResName[0]) wcscpy( p, info->szResName );
1077 else
1079 char buf[16];
1080 sprintf( buf, "%hu", info->wResID );
1081 asciiz_to_unicode( p, buf );
1083 valueA[0] = 0;
1085 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
1086 if ((key = open_hkcu_key( "Software\\Wine\\X11 Driver\\Cursors" )))
1088 char buffer[4096];
1089 KEY_VALUE_PARTIAL_INFORMATION *value = (void *)buffer;
1090 DWORD size = query_reg_value( key, NULL, value, sizeof(buffer) );
1091 NtClose( key );
1092 if (size && value->Type == REG_SZ)
1094 const WCHAR *valueW = (const WCHAR *)value->Data;
1095 if (!valueW[0]) return 0; /* force standard cursor */
1096 if (!ntdll_wcstoumbs( valueW, lstrlenW(valueW) + 1, valueA, sizeof(valueA), FALSE ))
1097 valueA[0] = 0;
1098 goto done;
1102 if (info->szResName[0]) goto done; /* only integer resources are supported here */
1104 if ((module = wcsrchr( info->szModName, '\\' ))) module++;
1105 else module = info->szModName;
1106 for (i = 0; i < ARRAY_SIZE( module_cursors ); i++)
1107 if (!wcsicmp( module, module_cursors[i].name )) break;
1108 if (i == ARRAY_SIZE( module_cursors )) goto done;
1110 cursors = module_cursors[i].cursors;
1111 for (i = 0; cursors[i].id; i++)
1112 if (cursors[i].id == info->wResID)
1114 strcpy( valueA, cursors[i].names[0] );
1115 names = cursors[i].names;
1116 break;
1119 done:
1120 if (valueA[0])
1122 #ifdef SONAME_LIBXCURSOR
1123 if (pXcursorLibraryLoadCursor)
1125 if (!names)
1126 cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
1127 else
1128 while (*names && !cursor) cursor = pXcursorLibraryLoadCursor( gdi_display, *names++ );
1130 #endif
1131 if (!cursor)
1133 int shape = find_fallback_shape( valueA );
1134 if (shape != -1) cursor = XCreateFontCursor( gdi_display, shape );
1136 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
1137 debugstr_w(name), debugstr_a(valueA) );
1139 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
1140 return cursor;
1144 /***********************************************************************
1145 * create_xlib_monochrome_cursor
1147 * Create a monochrome X cursor from a Windows one.
1149 static Cursor create_xlib_monochrome_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1151 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1152 BITMAPINFO *info = (BITMAPINFO *)buffer;
1153 const int and_y = 0;
1154 const int xor_y = height;
1155 unsigned int width_bytes = (width + 31) / 32 * 4;
1156 unsigned char *mask_bits = NULL;
1157 GC gc;
1158 XColor fg, bg;
1159 XVisualInfo vis = default_visual;
1160 Pixmap src_pixmap, bits_pixmap, mask_pixmap;
1161 struct gdi_image_bits bits;
1162 Cursor cursor = 0;
1164 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1165 info->bmiHeader.biWidth = width;
1166 info->bmiHeader.biHeight = -height * 2;
1167 info->bmiHeader.biPlanes = 1;
1168 info->bmiHeader.biBitCount = 1;
1169 info->bmiHeader.biCompression = BI_RGB;
1170 info->bmiHeader.biSizeImage = width_bytes * height * 2;
1171 info->bmiHeader.biXPelsPerMeter = 0;
1172 info->bmiHeader.biYPelsPerMeter = 0;
1173 info->bmiHeader.biClrUsed = 0;
1174 info->bmiHeader.biClrImportant = 0;
1176 if (!(mask_bits = malloc( info->bmiHeader.biSizeImage ))) goto done;
1177 if (!NtGdiGetDIBitsInternal( hdc, icon->hbmMask, 0, height * 2, mask_bits, info,
1178 DIB_RGB_COLORS, 0, 0 )) goto done;
1180 vis.depth = 1;
1181 bits.ptr = mask_bits;
1182 bits.free = NULL;
1183 bits.is_copy = TRUE;
1184 if (!(src_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1186 bits_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
1187 mask_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
1188 gc = XCreateGC( gdi_display, src_pixmap, 0, NULL );
1189 XSetGraphicsExposures( gdi_display, gc, False );
1191 /* We have to do some magic here, as cursors are not fully
1192 * compatible between Windows and X11. Under X11, there are
1193 * only 3 possible color cursor: black, white and masked. So
1194 * we map the 4th Windows color (invert the bits on the screen)
1195 * to black and an additional white bit on another place
1196 * (+1,+1). This require some boolean arithmetic:
1198 * Windows | X11
1199 * And Xor Result | Bits Mask Result
1200 * 0 0 black | 0 1 background
1201 * 0 1 white | 1 1 foreground
1202 * 1 0 no change | X 0 no change
1203 * 1 1 inverted | 0 1 background
1205 * which gives:
1206 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
1207 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
1209 XSetFunction( gdi_display, gc, GXcopy );
1210 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 0, 0 );
1211 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 0, 0 );
1212 XSetFunction( gdi_display, gc, GXandReverse );
1213 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, xor_y, width, height, 0, 0 );
1214 XSetFunction( gdi_display, gc, GXorReverse );
1215 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, xor_y, width, height, 0, 0 );
1216 /* additional white */
1217 XSetFunction( gdi_display, gc, GXand );
1218 XCopyArea( gdi_display, src_pixmap, src_pixmap, gc, 0, xor_y, width, height, 0, and_y );
1219 XSetFunction( gdi_display, gc, GXor );
1220 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 1, 1 );
1221 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 1, 1 );
1222 XFreeGC( gdi_display, gc );
1224 fg.red = fg.green = fg.blue = 0xffff;
1225 bg.red = bg.green = bg.blue = 0;
1226 cursor = XCreatePixmapCursor( gdi_display, bits_pixmap, mask_pixmap,
1227 &fg, &bg, icon->xHotspot, icon->yHotspot );
1228 XFreePixmap( gdi_display, src_pixmap );
1229 XFreePixmap( gdi_display, bits_pixmap );
1230 XFreePixmap( gdi_display, mask_pixmap );
1232 done:
1233 free( mask_bits );
1234 return cursor;
1237 static BOOL get_icon_info( HICON handle, ICONINFOEXW *ret )
1239 UNICODE_STRING module, res_name;
1240 ICONINFO info;
1242 module.Buffer = ret->szModName;
1243 module.MaximumLength = sizeof(ret->szModName) - sizeof(WCHAR);
1244 res_name.Buffer = ret->szResName;
1245 res_name.MaximumLength = sizeof(ret->szResName) - sizeof(WCHAR);
1246 if (!NtUserGetIconInfo( handle, &info, &module, &res_name, NULL, 0 )) return FALSE;
1247 ret->fIcon = info.fIcon;
1248 ret->xHotspot = info.xHotspot;
1249 ret->yHotspot = info.yHotspot;
1250 ret->hbmColor = info.hbmColor;
1251 ret->hbmMask = info.hbmMask;
1252 ret->wResID = res_name.Length ? 0 : LOWORD( res_name.Buffer );
1253 ret->szModName[module.Length] = 0;
1254 ret->szResName[res_name.Length] = 0;
1255 return TRUE;
1258 /***********************************************************************
1259 * create_xlib_load_mono_cursor
1261 * Create a monochrome X cursor from a color Windows one by trying to load the monochrome resource.
1263 static Cursor create_xlib_load_mono_cursor( HDC hdc, HANDLE handle, int width, int height )
1265 Cursor cursor = None;
1266 HANDLE mono;
1267 ICONINFOEXW info;
1268 BITMAP bm;
1270 if (!(mono = CopyImage( handle, IMAGE_CURSOR, width, height, LR_MONOCHROME | LR_COPYFROMRESOURCE )))
1271 return None;
1273 if (get_icon_info( mono, &info ))
1275 if (!info.hbmColor)
1277 NtGdiExtGetObjectW( info.hbmMask, sizeof(bm), &bm );
1278 bm.bmHeight = max( 1, bm.bmHeight / 2 );
1279 /* make sure hotspot is valid */
1280 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1282 info.xHotspot = bm.bmWidth / 2;
1283 info.yHotspot = bm.bmHeight / 2;
1285 cursor = create_xlib_monochrome_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1287 else NtGdiDeleteObjectApp( info.hbmColor );
1288 NtGdiDeleteObjectApp( info.hbmMask );
1290 NtUserDestroyCursor( mono, 0 );
1291 return cursor;
1294 /***********************************************************************
1295 * create_xlib_color_cursor
1297 * Create a color X cursor from a Windows one.
1299 static Cursor create_xlib_color_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1301 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1302 BITMAPINFO *info = (BITMAPINFO *)buffer;
1303 XColor fg, bg;
1304 Cursor cursor = None;
1305 XVisualInfo vis = default_visual;
1306 Pixmap xor_pixmap, mask_pixmap;
1307 struct gdi_image_bits bits;
1308 unsigned int *color_bits = NULL, *ptr;
1309 unsigned char *mask_bits = NULL, *xor_bits = NULL;
1310 int i, x, y;
1311 BOOL has_alpha = FALSE;
1312 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
1313 unsigned int width_bytes = (width + 31) / 32 * 4;
1315 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1316 info->bmiHeader.biWidth = width;
1317 info->bmiHeader.biHeight = -height;
1318 info->bmiHeader.biPlanes = 1;
1319 info->bmiHeader.biBitCount = 1;
1320 info->bmiHeader.biCompression = BI_RGB;
1321 info->bmiHeader.biSizeImage = width_bytes * height;
1322 info->bmiHeader.biXPelsPerMeter = 0;
1323 info->bmiHeader.biYPelsPerMeter = 0;
1324 info->bmiHeader.biClrUsed = 0;
1325 info->bmiHeader.biClrImportant = 0;
1327 if (!(mask_bits = malloc( info->bmiHeader.biSizeImage ))) goto done;
1328 if (!NtGdiGetDIBitsInternal( hdc, icon->hbmMask, 0, height, mask_bits, info,
1329 DIB_RGB_COLORS, 0, 0 )) goto done;
1331 info->bmiHeader.biBitCount = 32;
1332 info->bmiHeader.biSizeImage = width * height * 4;
1333 if (!(color_bits = malloc( info->bmiHeader.biSizeImage ))) goto done;
1334 if (!(xor_bits = calloc( 1, width_bytes * height ))) goto done;
1335 NtGdiGetDIBitsInternal( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS, 0, 0 );
1337 /* compute fg/bg color and xor bitmap based on average of the color values */
1339 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
1340 for (y = 0, ptr = color_bits; y < height; y++)
1342 for (x = 0; x < width; x++, ptr++)
1344 int red = (*ptr >> 16) & 0xff;
1345 int green = (*ptr >> 8) & 0xff;
1346 int blue = (*ptr >> 0) & 0xff;
1347 if (red + green + blue > 0x40)
1349 rfg += red;
1350 gfg += green;
1351 bfg += blue;
1352 fgBits++;
1353 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1355 else
1357 rbg += red;
1358 gbg += green;
1359 bbg += blue;
1363 if (fgBits)
1365 fg.red = rfg * 257 / fgBits;
1366 fg.green = gfg * 257 / fgBits;
1367 fg.blue = bfg * 257 / fgBits;
1369 else fg.red = fg.green = fg.blue = 0;
1370 bgBits = width * height - fgBits;
1371 if (bgBits)
1373 bg.red = rbg * 257 / bgBits;
1374 bg.green = gbg * 257 / bgBits;
1375 bg.blue = bbg * 257 / bgBits;
1377 else bg.red = bg.green = bg.blue = 0;
1379 info->bmiHeader.biBitCount = 1;
1380 info->bmiHeader.biClrUsed = 0;
1381 info->bmiHeader.biSizeImage = width_bytes * height;
1383 /* generate mask from the alpha channel if we have one */
1385 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
1386 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
1388 if (has_alpha)
1390 memset( mask_bits, 0, width_bytes * height );
1391 for (y = 0, ptr = color_bits; y < height; y++)
1392 for (x = 0; x < width; x++, ptr++)
1393 if ((*ptr >> 24) > 25) /* more than 10% alpha */
1394 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1396 else /* invert the mask */
1398 unsigned int j;
1400 ptr = (unsigned int *)mask_bits;
1401 for (j = 0; j < info->bmiHeader.biSizeImage / sizeof(*ptr); j++, ptr++) *ptr ^= ~0u;
1404 vis.depth = 1;
1405 bits.ptr = xor_bits;
1406 bits.free = NULL;
1407 bits.is_copy = TRUE;
1408 if (!(xor_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1410 bits.ptr = mask_bits;
1411 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
1413 if (mask_pixmap)
1415 cursor = XCreatePixmapCursor( gdi_display, xor_pixmap, mask_pixmap,
1416 &fg, &bg, icon->xHotspot, icon->yHotspot );
1417 XFreePixmap( gdi_display, mask_pixmap );
1419 XFreePixmap( gdi_display, xor_pixmap );
1421 done:
1422 free( color_bits );
1423 free( xor_bits );
1424 free( mask_bits );
1425 return cursor;
1428 /***********************************************************************
1429 * create_cursor
1431 * Create an X cursor from a Windows one.
1433 static Cursor create_cursor( HANDLE handle )
1435 Cursor cursor = 0;
1436 ICONINFOEXW info;
1437 BITMAP bm;
1438 HDC hdc;
1440 if (!handle) return get_empty_cursor();
1442 if (!get_icon_info( handle, &info )) return 0;
1444 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
1446 NtGdiDeleteObjectApp( info.hbmColor );
1447 NtGdiDeleteObjectApp( info.hbmMask );
1448 return cursor;
1451 NtGdiExtGetObjectW( info.hbmMask, sizeof(bm), &bm );
1452 if (!info.hbmColor) bm.bmHeight = max( 1, bm.bmHeight / 2 );
1454 /* make sure hotspot is valid */
1455 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1457 info.xHotspot = bm.bmWidth / 2;
1458 info.yHotspot = bm.bmHeight / 2;
1461 hdc = NtGdiCreateCompatibleDC( 0 );
1463 if (info.hbmColor)
1465 #ifdef SONAME_LIBXCURSOR
1466 if (pXcursorImagesLoadCursor)
1467 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
1468 #endif
1469 if (!cursor) cursor = create_xlib_load_mono_cursor( hdc, handle, bm.bmWidth, bm.bmHeight );
1470 if (!cursor) cursor = create_xlib_color_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1471 NtGdiDeleteObjectApp( info.hbmColor );
1473 else
1475 cursor = create_xlib_monochrome_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1478 NtGdiDeleteObjectApp( info.hbmMask );
1479 NtGdiDeleteObjectApp( hdc );
1480 return cursor;
1483 /***********************************************************************
1484 * DestroyCursorIcon (X11DRV.@)
1486 void X11DRV_DestroyCursorIcon( HCURSOR handle )
1488 Cursor cursor;
1490 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1492 TRACE( "%p xid %lx\n", handle, cursor );
1493 XFreeCursor( gdi_display, cursor );
1494 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1498 /***********************************************************************
1499 * SetCursor (X11DRV.@)
1501 void X11DRV_SetCursor( HCURSOR handle )
1503 if (InterlockedExchangePointer( (void **)&last_cursor, handle ) != handle ||
1504 NtGetTickCount() - last_cursor_change > 100)
1506 last_cursor_change = NtGetTickCount();
1507 if (cursor_window) send_notify_message( cursor_window, WM_X11DRV_SET_CURSOR,
1508 GetCurrentThreadId(), (LPARAM)handle );
1512 /***********************************************************************
1513 * SetCursorPos (X11DRV.@)
1515 BOOL X11DRV_SetCursorPos( INT x, INT y )
1517 struct x11drv_thread_data *data = x11drv_init_thread_data();
1518 POINT pos = virtual_screen_to_root( x, y );
1520 if (keyboard_grabbed)
1522 WARN( "refusing to warp to %u, %u\n", (int)pos.x, (int)pos.y );
1523 return FALSE;
1526 if (!clipping_cursor &&
1527 XGrabPointer( data->display, root_window, False,
1528 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1529 GrabModeAsync, GrabModeAsync, None, None, CurrentTime ) != GrabSuccess)
1531 WARN( "refusing to warp pointer to %u, %u without exclusive grab\n", (int)pos.x, (int)pos.y );
1532 return FALSE;
1535 XWarpPointer( data->display, root_window, root_window, 0, 0, 0, 0, pos.x, pos.y );
1536 data->warp_serial = NextRequest( data->display );
1538 if (!clipping_cursor)
1539 XUngrabPointer( data->display, CurrentTime );
1541 XNoOp( data->display );
1542 XFlush( data->display ); /* avoids bad mouse lag in games that do their own mouse warping */
1543 TRACE( "warped to %d,%d serial %lu\n", x, y, data->warp_serial );
1544 return TRUE;
1547 /***********************************************************************
1548 * GetCursorPos (X11DRV.@)
1550 BOOL X11DRV_GetCursorPos(LPPOINT pos)
1552 Display *display = thread_init_display();
1553 Window root, child;
1554 int rootX, rootY, winX, winY;
1555 unsigned int xstate;
1556 BOOL ret;
1558 ret = XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &winX, &winY, &xstate );
1559 if (ret)
1561 POINT old = *pos;
1562 *pos = root_to_virtual_screen( winX, winY );
1563 TRACE( "pointer at %s server pos %s\n", wine_dbgstr_point(pos), wine_dbgstr_point(&old) );
1565 return ret;
1568 /***********************************************************************
1569 * ClipCursor (X11DRV.@)
1571 BOOL X11DRV_ClipCursor( LPCRECT clip )
1573 RECT virtual_rect = NtUserGetVirtualScreenRect();
1575 if (!clip) clip = &virtual_rect;
1577 if (grab_pointer)
1579 HWND foreground = NtUserGetForegroundWindow();
1580 DWORD tid, pid;
1582 /* forward request to the foreground window if it's in a different thread */
1583 tid = NtUserGetWindowThread( foreground, &pid );
1584 if (tid && tid != GetCurrentThreadId() && pid == GetCurrentProcessId())
1586 TRACE( "forwarding clip request to %p\n", foreground );
1587 send_notify_message( foreground, WM_X11DRV_CLIP_CURSOR_REQUEST, FALSE, FALSE );
1588 return TRUE;
1591 /* we are clipping if the clip rectangle is smaller than the screen */
1592 if (clip->left > virtual_rect.left || clip->right < virtual_rect.right ||
1593 clip->top > virtual_rect.top || clip->bottom < virtual_rect.bottom)
1595 if (grab_clipping_window( clip )) return TRUE;
1597 else /* if currently clipping, check if we should switch to fullscreen clipping */
1599 struct x11drv_thread_data *data = x11drv_thread_data();
1600 if (data && data->clip_hwnd)
1602 if (EqualRect( clip, &clip_rect ) || clip_fullscreen_window( foreground, TRUE ))
1603 return TRUE;
1607 ungrab_clipping_window();
1608 return TRUE;
1611 /***********************************************************************
1612 * clip_cursor_request
1614 * Function called upon receiving a WM_X11DRV_CLIP_CURSOR_REQUEST.
1616 LRESULT clip_cursor_request( HWND hwnd, BOOL fullscreen, BOOL reset )
1618 RECT clip;
1620 if (hwnd == NtUserGetDesktopWindow())
1621 WARN( "ignoring clip cursor request on desktop window.\n" );
1622 else if (hwnd != NtUserGetForegroundWindow())
1623 WARN( "ignoring clip cursor request on non-foreground window.\n" );
1624 else if (fullscreen)
1625 clip_fullscreen_window( hwnd, reset );
1626 else
1628 NtUserGetClipCursor( &clip );
1629 X11DRV_ClipCursor( &clip );
1632 return 0;
1635 /***********************************************************************
1636 * move_resize_window
1638 void move_resize_window( HWND hwnd, int dir )
1640 Display *display = thread_display();
1641 DWORD pt;
1642 POINT pos;
1643 int button = 0;
1644 XEvent xev;
1645 Window win, root, child;
1646 unsigned int xstate;
1648 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1650 pt = NtUserGetThreadInfo()->message_pos;
1651 pos = virtual_screen_to_root( (short)LOWORD( pt ), (short)HIWORD( pt ) );
1653 if (NtUserGetKeyState( VK_LBUTTON ) & 0x8000) button = 1;
1654 else if (NtUserGetKeyState( VK_MBUTTON ) & 0x8000) button = 2;
1655 else if (NtUserGetKeyState( VK_RBUTTON ) & 0x8000) button = 3;
1657 TRACE( "hwnd %p/%lx, pos %s, dir %d, button %d\n", hwnd, win, wine_dbgstr_point(&pos), dir, button );
1659 xev.xclient.type = ClientMessage;
1660 xev.xclient.window = win;
1661 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1662 xev.xclient.serial = 0;
1663 xev.xclient.display = display;
1664 xev.xclient.send_event = True;
1665 xev.xclient.format = 32;
1666 xev.xclient.data.l[0] = pos.x; /* x coord */
1667 xev.xclient.data.l[1] = pos.y; /* y coord */
1668 xev.xclient.data.l[2] = dir; /* direction */
1669 xev.xclient.data.l[3] = button; /* button */
1670 xev.xclient.data.l[4] = 0; /* unused */
1672 /* need to ungrab the pointer that may have been automatically grabbed
1673 * with a ButtonPress event */
1674 XUngrabPointer( display, CurrentTime );
1675 XSendEvent(display, root_window, False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
1677 /* try to detect the end of the size/move by polling for the mouse button to be released */
1678 /* (some apps don't like it if we return before the size/move is done) */
1680 if (!button) return;
1681 send_message( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1683 for (;;)
1685 MSG msg;
1686 INPUT input;
1687 int x, y, rootX, rootY;
1689 if (!XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &x, &y, &xstate )) break;
1691 if (!(xstate & (Button1Mask << (button - 1))))
1693 /* fake a button release event */
1694 pos = root_to_virtual_screen( x, y );
1695 input.type = INPUT_MOUSE;
1696 input.u.mi.dx = pos.x;
1697 input.u.mi.dy = pos.y;
1698 input.u.mi.mouseData = button_up_data[button - 1];
1699 input.u.mi.dwFlags = button_up_flags[button - 1] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1700 input.u.mi.time = NtGetTickCount();
1701 input.u.mi.dwExtraInfo = 0;
1702 __wine_send_input( hwnd, &input, NULL );
1705 while (NtUserPeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
1707 if (!NtUserCallMsgFilter( &msg, MSGF_SIZE ))
1709 NtUserTranslateMessage( &msg, 0 );
1710 NtUserDispatchMessage( &msg );
1714 if (!(xstate & (Button1Mask << (button - 1)))) break;
1715 NtUserMsgWaitForMultipleObjectsEx( 0, NULL, 100, QS_ALLINPUT, 0 );
1718 TRACE( "hwnd %p/%lx done\n", hwnd, win );
1719 send_message( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1723 /***********************************************************************
1724 * X11DRV_ButtonPress
1726 BOOL X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1728 XButtonEvent *event = &xev->xbutton;
1729 int buttonNum = event->button - 1;
1730 INPUT input;
1732 if (buttonNum >= NB_BUTTONS) return FALSE;
1734 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1736 input.u.mi.dx = event->x;
1737 input.u.mi.dy = event->y;
1738 input.u.mi.mouseData = button_down_data[buttonNum];
1739 input.u.mi.dwFlags = button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1740 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1741 input.u.mi.dwExtraInfo = 0;
1743 update_user_time( event->time );
1744 map_event_coords( hwnd, event->window, event->root, event->x_root, event->y_root, &input );
1745 send_mouse_input( hwnd, event->window, event->state, &input );
1746 return TRUE;
1750 /***********************************************************************
1751 * X11DRV_ButtonRelease
1753 BOOL X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1755 XButtonEvent *event = &xev->xbutton;
1756 int buttonNum = event->button - 1;
1757 INPUT input;
1759 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return FALSE;
1761 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1763 input.u.mi.dx = event->x;
1764 input.u.mi.dy = event->y;
1765 input.u.mi.mouseData = button_up_data[buttonNum];
1766 input.u.mi.dwFlags = button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1767 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1768 input.u.mi.dwExtraInfo = 0;
1770 map_event_coords( hwnd, event->window, event->root, event->x_root, event->y_root, &input );
1771 send_mouse_input( hwnd, event->window, event->state, &input );
1772 return TRUE;
1776 /***********************************************************************
1777 * X11DRV_MotionNotify
1779 BOOL X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1781 XMotionEvent *event = &xev->xmotion;
1782 INPUT input;
1784 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d serial %lu\n",
1785 hwnd, event->window, event->x, event->y, event->is_hint, event->serial );
1787 input.u.mi.dx = event->x;
1788 input.u.mi.dy = event->y;
1789 input.u.mi.mouseData = 0;
1790 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1791 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1792 input.u.mi.dwExtraInfo = 0;
1794 if (!hwnd && is_old_motion_event( event->serial ))
1796 TRACE( "pos %d,%d old serial %lu, ignoring\n", event->x, event->y, event->serial );
1797 return FALSE;
1799 map_event_coords( hwnd, event->window, event->root, event->x_root, event->y_root, &input );
1800 send_mouse_input( hwnd, event->window, event->state, &input );
1801 return TRUE;
1805 /***********************************************************************
1806 * X11DRV_EnterNotify
1808 BOOL X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1810 XCrossingEvent *event = &xev->xcrossing;
1811 INPUT input;
1813 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd, event->window, event->x, event->y, event->detail );
1815 if (hwnd == x11drv_thread_data()->grab_hwnd) return FALSE;
1817 /* simulate a mouse motion event */
1818 input.u.mi.dx = event->x;
1819 input.u.mi.dy = event->y;
1820 input.u.mi.mouseData = 0;
1821 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1822 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1823 input.u.mi.dwExtraInfo = 0;
1825 if (is_old_motion_event( event->serial ))
1827 TRACE( "pos %d,%d old serial %lu, ignoring\n", event->x, event->y, event->serial );
1828 return FALSE;
1830 map_event_coords( hwnd, event->window, event->root, event->x_root, event->y_root, &input );
1831 send_mouse_input( hwnd, event->window, event->state, &input );
1832 return TRUE;
1835 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1837 /***********************************************************************
1838 * X11DRV_DeviceChanged
1840 static BOOL X11DRV_DeviceChanged( XGenericEventCookie *xev )
1842 XIDeviceChangedEvent *event = xev->data;
1843 struct x11drv_thread_data *data = x11drv_thread_data();
1845 if (event->deviceid != data->xi2_core_pointer) return FALSE;
1846 if (event->reason != XISlaveSwitch) return FALSE;
1848 update_relative_valuators( event->classes, event->num_classes );
1849 data->xi2_current_slave = event->sourceid;
1850 return TRUE;
1853 static BOOL map_raw_event_coords( XIRawEvent *event, INPUT *input )
1855 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1856 XIValuatorClassInfo *x = &thread_data->x_valuator, *y = &thread_data->y_valuator;
1857 double x_value = 0, y_value = 0, x_scale, y_scale;
1858 const double *values = event->valuators.values;
1859 RECT virtual_rect;
1860 int i;
1862 if (x->number < 0 || y->number < 0) return FALSE;
1863 if (!event->valuators.mask_len) return FALSE;
1864 if (thread_data->xi2_state != xi_enabled) return FALSE;
1866 /* If there is no slave currently detected, no previous motion nor device
1867 * change events were received. Look it up now on the device list in this
1868 * case.
1870 if (!thread_data->xi2_current_slave)
1872 XIDeviceInfo *devices = thread_data->xi2_devices;
1874 for (i = 0; i < thread_data->xi2_device_count; i++)
1876 if (devices[i].use != XISlavePointer) continue;
1877 if (devices[i].deviceid != event->deviceid) continue;
1878 if (devices[i].attachment != thread_data->xi2_core_pointer) continue;
1879 thread_data->xi2_current_slave = event->deviceid;
1880 break;
1883 if (event->deviceid != thread_data->xi2_current_slave) return FALSE;
1885 virtual_rect = NtUserGetVirtualScreenRect();
1887 if (x->max <= x->min) x_scale = 1;
1888 else x_scale = (virtual_rect.right - virtual_rect.left) / (x->max - x->min);
1889 if (y->max <= y->min) y_scale = 1;
1890 else y_scale = (virtual_rect.bottom - virtual_rect.top) / (y->max - y->min);
1892 for (i = 0; i <= max( x->number, y->number ); i++)
1894 if (!XIMaskIsSet( event->valuators.mask, i )) continue;
1895 if (i == x->number)
1897 x_value = *values;
1898 x->value += x_value * x_scale;
1900 if (i == y->number)
1902 y_value = *values;
1903 y->value += y_value * y_scale;
1905 values++;
1908 input->u.mi.dx = round( x->value );
1909 input->u.mi.dy = round( y->value );
1911 TRACE( "event %f,%f value %f,%f input %d,%d\n", x_value, y_value, x->value, y->value,
1912 (int)input->u.mi.dx, (int)input->u.mi.dy );
1914 x->value -= input->u.mi.dx;
1915 y->value -= input->u.mi.dy;
1917 if (!input->u.mi.dx && !input->u.mi.dy)
1919 TRACE( "accumulating motion\n" );
1920 return FALSE;
1923 return TRUE;
1926 /***********************************************************************
1927 * X11DRV_RawMotion
1929 static BOOL X11DRV_RawMotion( XGenericEventCookie *xev )
1931 XIRawEvent *event = xev->data;
1932 INPUT input;
1934 if (broken_rawevents && is_old_motion_event( xev->serial ))
1936 TRACE( "old serial %lu, ignoring\n", xev->serial );
1937 return FALSE;
1940 input.type = INPUT_MOUSE;
1941 input.u.mi.mouseData = 0;
1942 input.u.mi.dwFlags = MOUSEEVENTF_MOVE;
1943 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1944 input.u.mi.dwExtraInfo = 0;
1945 input.u.mi.dx = 0;
1946 input.u.mi.dy = 0;
1947 if (!map_raw_event_coords( event, &input )) return FALSE;
1949 __wine_send_input( 0, &input, NULL );
1950 return TRUE;
1953 #endif /* HAVE_X11_EXTENSIONS_XINPUT2_H */
1956 /***********************************************************************
1957 * X11DRV_XInput2_Init
1959 void X11DRV_XInput2_Init(void)
1961 #if defined(SONAME_LIBXI) && defined(HAVE_X11_EXTENSIONS_XINPUT2_H)
1962 int event, error;
1963 void *libxi_handle = dlopen( SONAME_LIBXI, RTLD_NOW );
1965 if (!libxi_handle)
1967 WARN( "couldn't load %s\n", SONAME_LIBXI );
1968 return;
1970 #define LOAD_FUNCPTR(f) \
1971 if (!(p##f = dlsym( libxi_handle, #f))) \
1973 WARN("Failed to load %s.\n", #f); \
1974 return; \
1977 LOAD_FUNCPTR(XIGetClientPointer);
1978 LOAD_FUNCPTR(XIFreeDeviceInfo);
1979 LOAD_FUNCPTR(XIQueryDevice);
1980 LOAD_FUNCPTR(XIQueryVersion);
1981 LOAD_FUNCPTR(XISelectEvents);
1982 #undef LOAD_FUNCPTR
1984 xinput2_available = XQueryExtension( gdi_display, "XInputExtension", &xinput2_opcode, &event, &error );
1986 /* Until version 1.10.4 rawinput was broken in XOrg, see
1987 * https://bugs.freedesktop.org/show_bug.cgi?id=30068 */
1988 broken_rawevents = strstr(XServerVendor( gdi_display ), "X.Org") &&
1989 XVendorRelease( gdi_display ) < 11004000;
1991 #else
1992 TRACE( "X Input 2 support not compiled in.\n" );
1993 #endif
1997 /***********************************************************************
1998 * X11DRV_GenericEvent
2000 BOOL X11DRV_GenericEvent( HWND hwnd, XEvent *xev )
2002 BOOL ret = FALSE;
2003 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
2004 XGenericEventCookie *event = &xev->xcookie;
2006 if (!event->data) return FALSE;
2007 if (event->extension != xinput2_opcode) return FALSE;
2009 switch (event->evtype)
2011 case XI_DeviceChanged:
2012 ret = X11DRV_DeviceChanged( event );
2013 break;
2014 case XI_RawMotion:
2015 ret = X11DRV_RawMotion( event );
2016 break;
2018 default:
2019 TRACE( "Unhandled event %#x\n", event->evtype );
2020 break;
2022 #endif
2023 return ret;