winex11.drv: Map coordinates before calling send_mouse_input.
[wine/zf.git] / dlls / winex11.drv / mouse.c
blob25b0b742fe3739cc3352ac3fcb37f31e6bc5d370
1 /*
2 * X11 mouse driver
4 * Copyright 1998 Ulrich Weigand
5 * Copyright 2007 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <X11/Xlib.h>
26 #include <X11/cursorfont.h>
27 #include <stdarg.h>
28 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
29 #include <X11/extensions/XInput2.h>
30 #endif
32 #ifdef SONAME_LIBXCURSOR
33 # include <X11/Xcursor/Xcursor.h>
34 static void *xcursor_handle;
35 # define MAKE_FUNCPTR(f) static typeof(f) * p##f
36 MAKE_FUNCPTR(XcursorImageCreate);
37 MAKE_FUNCPTR(XcursorImageDestroy);
38 MAKE_FUNCPTR(XcursorImageLoadCursor);
39 MAKE_FUNCPTR(XcursorImagesCreate);
40 MAKE_FUNCPTR(XcursorImagesDestroy);
41 MAKE_FUNCPTR(XcursorImagesLoadCursor);
42 MAKE_FUNCPTR(XcursorLibraryLoadCursor);
43 # undef MAKE_FUNCPTR
44 #endif /* SONAME_LIBXCURSOR */
46 #define NONAMELESSUNION
47 #define OEMRESOURCE
48 #include "windef.h"
49 #include "winbase.h"
50 #include "winreg.h"
52 #include "x11drv.h"
53 #include "wine/server.h"
54 #include "wine/unicode.h"
55 #include "wine/debug.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
59 /**********************************************************************/
61 #ifndef Button6Mask
62 #define Button6Mask (1<<13)
63 #endif
64 #ifndef Button7Mask
65 #define Button7Mask (1<<14)
66 #endif
68 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
70 static const UINT button_down_flags[NB_BUTTONS] =
72 MOUSEEVENTF_LEFTDOWN,
73 MOUSEEVENTF_MIDDLEDOWN,
74 MOUSEEVENTF_RIGHTDOWN,
75 MOUSEEVENTF_WHEEL,
76 MOUSEEVENTF_WHEEL,
77 MOUSEEVENTF_HWHEEL,
78 MOUSEEVENTF_HWHEEL,
79 MOUSEEVENTF_XDOWN,
80 MOUSEEVENTF_XDOWN
83 static const UINT button_up_flags[NB_BUTTONS] =
85 MOUSEEVENTF_LEFTUP,
86 MOUSEEVENTF_MIDDLEUP,
87 MOUSEEVENTF_RIGHTUP,
92 MOUSEEVENTF_XUP,
93 MOUSEEVENTF_XUP
96 static const UINT button_down_data[NB_BUTTONS] =
101 WHEEL_DELTA,
102 -WHEEL_DELTA,
103 -WHEEL_DELTA,
104 WHEEL_DELTA,
105 XBUTTON1,
106 XBUTTON2
109 static const UINT button_up_data[NB_BUTTONS] =
118 XBUTTON1,
119 XBUTTON2
122 XContext cursor_context = 0;
124 static HWND cursor_window;
125 static HCURSOR last_cursor;
126 static DWORD last_cursor_change;
127 static RECT last_clip_rect;
128 static HWND last_clip_foreground_window;
129 static BOOL last_clip_refused;
130 static RECT clip_rect;
131 static Cursor create_cursor( HANDLE handle );
133 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
134 static BOOL xinput2_available;
135 static BOOL broken_rawevents;
136 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
137 MAKE_FUNCPTR(XIGetClientPointer);
138 MAKE_FUNCPTR(XIFreeDeviceInfo);
139 MAKE_FUNCPTR(XIQueryDevice);
140 MAKE_FUNCPTR(XIQueryVersion);
141 MAKE_FUNCPTR(XISelectEvents);
142 #undef MAKE_FUNCPTR
143 #endif
145 /***********************************************************************
146 * X11DRV_Xcursor_Init
148 * Load the Xcursor library for use.
150 void X11DRV_Xcursor_Init(void)
152 #ifdef SONAME_LIBXCURSOR
153 xcursor_handle = dlopen(SONAME_LIBXCURSOR, RTLD_NOW);
154 if (!xcursor_handle)
156 WARN("Xcursor failed to load. Using fallback code.\n");
157 return;
159 #define LOAD_FUNCPTR(f) p##f = dlsym(xcursor_handle, #f)
161 LOAD_FUNCPTR(XcursorImageCreate);
162 LOAD_FUNCPTR(XcursorImageDestroy);
163 LOAD_FUNCPTR(XcursorImageLoadCursor);
164 LOAD_FUNCPTR(XcursorImagesCreate);
165 LOAD_FUNCPTR(XcursorImagesDestroy);
166 LOAD_FUNCPTR(XcursorImagesLoadCursor);
167 LOAD_FUNCPTR(XcursorLibraryLoadCursor);
168 #undef LOAD_FUNCPTR
169 #endif /* SONAME_LIBXCURSOR */
173 /***********************************************************************
174 * get_empty_cursor
176 static Cursor get_empty_cursor(void)
178 static Cursor cursor;
179 static const char data[] = { 0 };
181 if (!cursor)
183 XColor bg;
184 Pixmap pixmap;
186 bg.red = bg.green = bg.blue = 0x0000;
187 pixmap = XCreateBitmapFromData( gdi_display, root_window, data, 1, 1 );
188 if (pixmap)
190 Cursor new = XCreatePixmapCursor( gdi_display, pixmap, pixmap, &bg, &bg, 0, 0 );
191 if (InterlockedCompareExchangePointer( (void **)&cursor, (void *)new, 0 ))
192 XFreeCursor( gdi_display, new );
193 XFreePixmap( gdi_display, pixmap );
196 return cursor;
199 /***********************************************************************
200 * set_window_cursor
202 void set_window_cursor( Window window, HCURSOR handle )
204 Cursor cursor, prev;
206 if (!handle) cursor = get_empty_cursor();
207 else if (XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
209 /* try to create it */
210 if (!(cursor = create_cursor( handle ))) return;
212 XLockDisplay( gdi_display );
213 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&prev ))
215 /* someone else was here first */
216 XFreeCursor( gdi_display, cursor );
217 cursor = prev;
219 else
221 XSaveContext( gdi_display, (XID)handle, cursor_context, (char *)cursor );
222 TRACE( "cursor %p created %lx\n", handle, cursor );
224 XUnlockDisplay( gdi_display );
227 XDefineCursor( gdi_display, window, cursor );
228 /* make the change take effect immediately */
229 XFlush( gdi_display );
232 /***********************************************************************
233 * sync_window_cursor
235 void sync_window_cursor( Window window )
237 HCURSOR cursor;
239 SERVER_START_REQ( set_cursor )
241 req->flags = 0;
242 wine_server_call( req );
243 cursor = reply->prev_count >= 0 ? wine_server_ptr_handle( reply->prev_handle ) : 0;
245 SERVER_END_REQ;
247 set_window_cursor( window, cursor );
250 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
251 /***********************************************************************
252 * update_relative_valuators
254 static void update_relative_valuators(XIAnyClassInfo **valuators, int n_valuators)
256 struct x11drv_thread_data *thread_data = x11drv_thread_data();
257 int i;
259 thread_data->x_rel_valuator.number = -1;
260 thread_data->y_rel_valuator.number = -1;
262 for (i = 0; i < n_valuators; i++)
264 XIValuatorClassInfo *class = (XIValuatorClassInfo *)valuators[i];
265 struct x11drv_valuator_data *valuator_data = NULL;
267 if (valuators[i]->type != XIValuatorClass) continue;
268 if (class->label == x11drv_atom( Rel_X ) ||
269 (!class->label && class->number == 0 && class->mode == XIModeRelative))
271 valuator_data = &thread_data->x_rel_valuator;
273 else if (class->label == x11drv_atom( Rel_Y ) ||
274 (!class->label && class->number == 1 && class->mode == XIModeRelative))
276 valuator_data = &thread_data->y_rel_valuator;
279 if (valuator_data) {
280 valuator_data->number = class->number;
281 valuator_data->min = class->min;
282 valuator_data->max = class->max;
286 #endif
289 /***********************************************************************
290 * enable_xinput2
292 static void enable_xinput2(void)
294 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
295 struct x11drv_thread_data *data = x11drv_thread_data();
296 XIEventMask mask;
297 XIDeviceInfo *pointer_info;
298 unsigned char mask_bits[XIMaskLen(XI_LASTEVENT)];
299 int count;
301 if (!xinput2_available) return;
303 if (data->xi2_state == xi_unknown)
305 int major = 2, minor = 0;
306 if (!pXIQueryVersion( data->display, &major, &minor )) data->xi2_state = xi_disabled;
307 else
309 data->xi2_state = xi_unavailable;
310 WARN( "X Input 2 not available\n" );
313 if (data->xi2_state == xi_unavailable) return;
314 if (!pXIGetClientPointer( data->display, None, &data->xi2_core_pointer )) return;
316 mask.mask = mask_bits;
317 mask.mask_len = sizeof(mask_bits);
318 mask.deviceid = XIAllDevices;
319 memset( mask_bits, 0, sizeof(mask_bits) );
320 XISetMask( mask_bits, XI_DeviceChanged );
321 XISetMask( mask_bits, XI_RawMotion );
322 XISetMask( mask_bits, XI_ButtonPress );
324 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
326 pointer_info = pXIQueryDevice( data->display, data->xi2_core_pointer, &count );
327 update_relative_valuators( pointer_info->classes, pointer_info->num_classes );
328 pXIFreeDeviceInfo( pointer_info );
330 /* This device info list is only used to find the initial current slave if
331 * no XI_DeviceChanged events happened. If any hierarchy change occurred that
332 * might be relevant here (eg. user switching mice after (un)plugging), a
333 * XI_DeviceChanged event will point us to the right slave. So this list is
334 * safe to be obtained statically at enable_xinput2() time.
336 if (data->xi2_devices) pXIFreeDeviceInfo( data->xi2_devices );
337 data->xi2_devices = pXIQueryDevice( data->display, XIAllDevices, &data->xi2_device_count );
338 data->xi2_current_slave = 0;
340 data->xi2_state = xi_enabled;
341 #endif
344 /***********************************************************************
345 * disable_xinput2
347 static void disable_xinput2(void)
349 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
350 struct x11drv_thread_data *data = x11drv_thread_data();
351 XIEventMask mask;
353 if (data->xi2_state != xi_enabled) return;
355 TRACE( "disabling\n" );
356 data->xi2_state = xi_disabled;
358 mask.mask = NULL;
359 mask.mask_len = 0;
360 mask.deviceid = XIAllDevices;
362 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
363 pXIFreeDeviceInfo( data->xi2_devices );
364 data->x_rel_valuator.number = -1;
365 data->y_rel_valuator.number = -1;
366 data->xi2_devices = NULL;
367 data->xi2_core_pointer = 0;
368 data->xi2_current_slave = 0;
369 #endif
373 /***********************************************************************
374 * grab_clipping_window
376 * Start a pointer grab on the clip window.
378 static BOOL grab_clipping_window( const RECT *clip )
380 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
381 struct x11drv_thread_data *data = x11drv_thread_data();
382 Window clip_window;
383 HWND msg_hwnd = 0;
384 POINT pos;
386 if (GetWindowThreadProcessId( GetDesktopWindow(), NULL ) == GetCurrentThreadId())
387 return TRUE; /* don't clip in the desktop process */
389 if (!data) return FALSE;
390 if (!(clip_window = init_clip_window())) return TRUE;
392 if (!(msg_hwnd = CreateWindowW( messageW, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0,
393 GetModuleHandleW(0), NULL )))
394 return TRUE;
396 if (keyboard_grabbed)
398 WARN( "refusing to clip to %s\n", wine_dbgstr_rect(clip) );
399 last_clip_refused = TRUE;
400 last_clip_foreground_window = GetForegroundWindow();
401 last_clip_rect = *clip;
402 return FALSE;
404 else
406 last_clip_refused = FALSE;
409 /* enable XInput2 unless we are already clipping */
410 if (!data->clip_hwnd) enable_xinput2();
412 if (data->xi2_state != xi_enabled)
414 WARN( "XInput2 not supported, refusing to clip to %s\n", wine_dbgstr_rect(clip) );
415 DestroyWindow( msg_hwnd );
416 ClipCursor( NULL );
417 return TRUE;
420 TRACE( "clipping to %s win %lx\n", wine_dbgstr_rect(clip), clip_window );
422 if (!data->clip_hwnd) XUnmapWindow( data->display, clip_window );
423 pos = virtual_screen_to_root( clip->left, clip->top );
424 XMoveResizeWindow( data->display, clip_window, pos.x, pos.y,
425 max( 1, clip->right - clip->left ), max( 1, clip->bottom - clip->top ) );
426 XMapWindow( data->display, clip_window );
428 /* if the rectangle is shrinking we may get a pointer warp */
429 if (!data->clip_hwnd || clip->left > clip_rect.left || clip->top > clip_rect.top ||
430 clip->right < clip_rect.right || clip->bottom < clip_rect.bottom)
431 data->warp_serial = NextRequest( data->display );
433 if (!XGrabPointer( data->display, clip_window, False,
434 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
435 GrabModeAsync, GrabModeAsync, clip_window, None, CurrentTime ))
436 clipping_cursor = TRUE;
438 if (!clipping_cursor)
440 disable_xinput2();
441 DestroyWindow( msg_hwnd );
442 return FALSE;
444 clip_rect = *clip;
445 if (!data->clip_hwnd) sync_window_cursor( clip_window );
446 InterlockedExchangePointer( (void **)&cursor_window, msg_hwnd );
447 data->clip_hwnd = msg_hwnd;
448 SendNotifyMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR_NOTIFY, 0, (LPARAM)msg_hwnd );
449 return TRUE;
452 /***********************************************************************
453 * ungrab_clipping_window
455 * Release the pointer grab on the clip window.
457 void ungrab_clipping_window(void)
459 Display *display = thread_init_display();
460 Window clip_window = init_clip_window();
462 if (!clip_window) return;
464 TRACE( "no longer clipping\n" );
465 XUnmapWindow( display, clip_window );
466 if (clipping_cursor) XUngrabPointer( display, CurrentTime );
467 clipping_cursor = FALSE;
468 SendNotifyMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR_NOTIFY, 0, 0 );
471 /***********************************************************************
472 * reset_clipping_window
474 * Forcibly reset the window clipping on external events.
476 void reset_clipping_window(void)
478 ungrab_clipping_window();
479 ClipCursor( NULL ); /* make sure the clip rectangle is reset too */
482 /***********************************************************************
483 * retry_grab_clipping_window
485 * Restore the current clip rectangle or retry the last one if it has
486 * been refused because of an active keyboard grab.
488 void retry_grab_clipping_window(void)
490 if (clipping_cursor)
491 ClipCursor( &clip_rect );
492 else if (last_clip_refused && GetForegroundWindow() == last_clip_foreground_window)
493 ClipCursor( &last_clip_rect );
496 /***********************************************************************
497 * clip_cursor_notify
499 * Notification function called upon receiving a WM_X11DRV_CLIP_CURSOR_NOTIFY.
501 LRESULT clip_cursor_notify( HWND hwnd, HWND prev_clip_hwnd, HWND new_clip_hwnd )
503 struct x11drv_thread_data *data = x11drv_init_thread_data();
505 if (hwnd == GetDesktopWindow()) /* change the clip window stored in the desktop process */
507 static HWND clip_hwnd;
509 HWND prev = clip_hwnd;
510 clip_hwnd = new_clip_hwnd;
511 if (prev || new_clip_hwnd) TRACE( "clip hwnd changed from %p to %p\n", prev, new_clip_hwnd );
512 if (prev) SendNotifyMessageW( prev, WM_X11DRV_CLIP_CURSOR_NOTIFY, (WPARAM)prev, 0 );
514 else if (hwnd == data->clip_hwnd) /* this is a notification that clipping has been reset */
516 TRACE( "clip hwnd reset from %p\n", hwnd );
517 data->clip_hwnd = 0;
518 data->clip_reset = GetTickCount();
519 disable_xinput2();
520 DestroyWindow( hwnd );
522 else if (prev_clip_hwnd)
524 /* This is a notification send by the desktop window to an old
525 * dangling clip window.
527 TRACE( "destroying old clip hwnd %p\n", prev_clip_hwnd );
528 DestroyWindow( prev_clip_hwnd );
530 return 0;
533 /***********************************************************************
534 * clip_fullscreen_window
536 * Turn on clipping if the active window is fullscreen.
538 BOOL clip_fullscreen_window( HWND hwnd, BOOL reset )
540 struct x11drv_win_data *data;
541 struct x11drv_thread_data *thread_data;
542 MONITORINFO monitor_info;
543 HMONITOR monitor;
544 DWORD style;
545 BOOL fullscreen;
547 if (hwnd == GetDesktopWindow()) return FALSE;
548 style = GetWindowLongW( hwnd, GWL_STYLE );
549 if (!(style & WS_VISIBLE)) return FALSE;
550 if ((style & (WS_POPUP | WS_CHILD)) == WS_CHILD) return FALSE;
551 /* maximized windows don't count as full screen */
552 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION) return FALSE;
553 if (!(data = get_win_data( hwnd ))) return FALSE;
554 fullscreen = is_window_rect_full_screen( &data->whole_rect );
555 release_win_data( data );
556 if (!fullscreen) return FALSE;
557 if (!(thread_data = x11drv_thread_data())) return FALSE;
558 if (GetTickCount() - thread_data->clip_reset < 1000) return FALSE;
559 if (!reset && clipping_cursor && thread_data->clip_hwnd) return FALSE; /* already clipping */
561 monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTONEAREST );
562 if (!monitor) return FALSE;
563 monitor_info.cbSize = sizeof(monitor_info);
564 if (!GetMonitorInfoW( monitor, &monitor_info )) return FALSE;
565 if (!grab_fullscreen)
567 RECT virtual_rect = get_virtual_screen_rect();
568 if (!EqualRect( &monitor_info.rcMonitor, &virtual_rect )) return FALSE;
569 if (is_virtual_desktop()) return FALSE;
571 TRACE( "win %p clipping fullscreen\n", hwnd );
572 return grab_clipping_window( &monitor_info.rcMonitor );
576 /***********************************************************************
577 * is_old_motion_event
579 static BOOL is_old_motion_event( unsigned long serial )
581 struct x11drv_thread_data *thread_data = x11drv_thread_data();
583 if (!thread_data->warp_serial) return FALSE;
584 if ((long)(serial - thread_data->warp_serial) < 0) return TRUE;
585 thread_data->warp_serial = 0; /* we caught up now */
586 return FALSE;
590 /***********************************************************************
591 * map_event_coords
593 * Map the input event coordinates so they're relative to the desktop.
595 static void map_event_coords( HWND hwnd, Window window, INPUT *input )
597 struct x11drv_thread_data *thread_data;
598 struct x11drv_win_data *data;
599 POINT pt = { input->u.mi.dx, input->u.mi.dy };
601 TRACE( "hwnd %p, window %lx, input %p\n", hwnd, window, input );
603 if (!hwnd)
605 thread_data = x11drv_thread_data();
606 if (!thread_data->clip_hwnd) return;
607 if (thread_data->clip_window != window) return;
608 pt.x += clip_rect.left;
609 pt.y += clip_rect.top;
611 else if ((data = get_win_data( hwnd )))
613 if (window == root_window) pt = root_to_virtual_screen( pt.x, pt.y );
614 else
616 if (window == data->whole_window)
618 pt.x += data->whole_rect.left - data->client_rect.left;
619 pt.y += data->whole_rect.top - data->client_rect.top;
622 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
623 pt.x = data->client_rect.right - data->client_rect.left - 1 - pt.x;
624 MapWindowPoints( hwnd, 0, &pt, 1 );
626 release_win_data( data );
629 TRACE( "mapped %s to %s\n", wine_dbgstr_point( (POINT *)&input->u.mi.dx ), wine_dbgstr_point( &pt ) );
631 input->u.mi.dx = pt.x;
632 input->u.mi.dy = pt.y;
636 /***********************************************************************
637 * send_mouse_input
639 * Update the various window states on a mouse event.
641 static void send_mouse_input( HWND hwnd, Window window, unsigned int state, INPUT *input )
643 struct x11drv_win_data *data;
645 input->type = INPUT_MOUSE;
647 if (!hwnd)
649 struct x11drv_thread_data *thread_data = x11drv_thread_data();
650 HWND clip_hwnd = thread_data->clip_hwnd;
652 if (!clip_hwnd) return;
653 if (thread_data->clip_window != window) return;
654 if (InterlockedExchangePointer( (void **)&cursor_window, clip_hwnd ) != clip_hwnd ||
655 input->u.mi.time - last_cursor_change > 100)
657 sync_window_cursor( window );
658 last_cursor_change = input->u.mi.time;
660 __wine_send_input( hwnd, input );
661 return;
664 if (!(data = get_win_data( hwnd ))) return;
665 if (InterlockedExchangePointer( (void **)&cursor_window, hwnd ) != hwnd ||
666 input->u.mi.time - last_cursor_change > 100)
668 sync_window_cursor( data->whole_window );
669 last_cursor_change = input->u.mi.time;
671 release_win_data( data );
673 if (hwnd != GetDesktopWindow())
675 hwnd = GetAncestor( hwnd, GA_ROOT );
676 if ((input->u.mi.dwFlags & (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_RIGHTDOWN)) && hwnd == GetForegroundWindow())
677 clip_fullscreen_window( hwnd, FALSE );
680 /* update the wine server Z-order */
682 if (hwnd != x11drv_thread_data()->grab_hwnd &&
683 /* ignore event if a button is pressed, since the mouse is then grabbed too */
684 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
686 RECT rect = { input->u.mi.dx, input->u.mi.dy, input->u.mi.dx + 1, input->u.mi.dy + 1 };
688 SERVER_START_REQ( update_window_zorder )
690 req->window = wine_server_user_handle( hwnd );
691 req->rect.left = rect.left;
692 req->rect.top = rect.top;
693 req->rect.right = rect.right;
694 req->rect.bottom = rect.bottom;
695 wine_server_call( req );
697 SERVER_END_REQ;
700 __wine_send_input( hwnd, input );
703 #ifdef SONAME_LIBXCURSOR
705 /***********************************************************************
706 * create_xcursor_frame
708 * Use Xcursor to create a frame of an X cursor from a Windows one.
710 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
711 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
712 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
713 int width, int height, int istep )
715 XcursorImage *image, *ret = NULL;
716 DWORD delay_jiffies, num_steps;
717 int x, y, i;
718 BOOL has_alpha = FALSE;
719 XcursorPixel *ptr;
721 image = pXcursorImageCreate( width, height );
722 if (!image)
724 ERR("X11 failed to produce a cursor frame!\n");
725 return NULL;
728 image->xhot = iinfo->xHotspot;
729 image->yhot = iinfo->yHotspot;
731 image->delay = 100; /* fallback delay, 100 ms */
732 if (GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, istep, &delay_jiffies, &num_steps) != 0)
733 image->delay = (100 * delay_jiffies) / 6; /* convert jiffies (1/60s) to milliseconds */
734 else
735 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep);
737 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
738 memset( color_bits, 0x00, color_size );
739 SelectObject( hdc, hbmColor );
740 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
742 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
743 goto cleanup;
745 memcpy( image->pixels, color_bits, color_size );
747 /* check if the cursor frame was drawn with an alpha channel */
748 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
749 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
751 /* if no alpha channel was drawn then generate it from the mask */
752 if (!has_alpha)
754 unsigned int width_bytes = (width + 31) / 32 * 4;
756 /* draw the cursor mask to a temporary buffer */
757 memset( mask_bits, 0xFF, mask_size );
758 SelectObject( hdc, hbmMask );
759 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
761 ERR("Failed to draw frame mask %d.\n", istep);
762 goto cleanup;
764 /* use the buffer to directly modify the XcursorImage alpha channel */
765 for (y = 0, ptr = image->pixels; y < height; y++)
766 for (x = 0; x < width; x++, ptr++)
767 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
768 *ptr |= 0xff000000;
770 ret = image;
772 cleanup:
773 if (ret == NULL) pXcursorImageDestroy( image );
774 return ret;
777 /***********************************************************************
778 * create_xcursor_cursor
780 * Use Xcursor to create an X cursor from a Windows one.
782 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
784 unsigned char *color_bits, *mask_bits;
785 HBITMAP hbmColor = 0, hbmMask = 0;
786 DWORD nFrames, delay_jiffies, i;
787 int color_size, mask_size;
788 BITMAPINFO *info = NULL;
789 XcursorImages *images;
790 XcursorImage **imgs;
791 Cursor cursor = 0;
793 /* Retrieve the number of frames to render */
794 if (!GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, 0, &delay_jiffies, &nFrames)) return 0;
795 if (!(imgs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XcursorImage*)*nFrames ))) return 0;
797 /* Allocate all of the resources necessary to obtain a cursor frame */
798 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
799 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
800 info->bmiHeader.biWidth = width;
801 info->bmiHeader.biHeight = -height;
802 info->bmiHeader.biPlanes = 1;
803 info->bmiHeader.biCompression = BI_RGB;
804 info->bmiHeader.biXPelsPerMeter = 0;
805 info->bmiHeader.biYPelsPerMeter = 0;
806 info->bmiHeader.biClrUsed = 0;
807 info->bmiHeader.biClrImportant = 0;
808 info->bmiHeader.biBitCount = 32;
809 color_size = width * height * 4;
810 info->bmiHeader.biSizeImage = color_size;
811 hbmColor = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &color_bits, NULL, 0);
812 if (!hbmColor)
814 ERR("Failed to create DIB section for cursor color data!\n");
815 goto cleanup;
817 info->bmiHeader.biBitCount = 1;
818 info->bmiColors[0].rgbRed = 0;
819 info->bmiColors[0].rgbGreen = 0;
820 info->bmiColors[0].rgbBlue = 0;
821 info->bmiColors[0].rgbReserved = 0;
822 info->bmiColors[1].rgbRed = 0xff;
823 info->bmiColors[1].rgbGreen = 0xff;
824 info->bmiColors[1].rgbBlue = 0xff;
825 info->bmiColors[1].rgbReserved = 0;
827 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
828 info->bmiHeader.biSizeImage = mask_size;
829 hbmMask = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &mask_bits, NULL, 0);
830 if (!hbmMask)
832 ERR("Failed to create DIB section for cursor mask data!\n");
833 goto cleanup;
836 /* Create an XcursorImage for each frame of the cursor */
837 for (i=0; i<nFrames; i++)
839 imgs[i] = create_xcursor_frame( hdc, iinfo, icon,
840 hbmColor, color_bits, color_size,
841 hbmMask, mask_bits, mask_size,
842 width, height, i );
843 if (!imgs[i]) goto cleanup;
846 /* Build an X cursor out of all of the frames */
847 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
848 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
849 images->images[images->nimage] = imgs[images->nimage];
850 cursor = pXcursorImagesLoadCursor( gdi_display, images );
851 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
852 HeapFree( GetProcessHeap(), 0, imgs );
853 imgs = NULL;
855 cleanup:
856 if (imgs)
858 /* Failed to produce a cursor, free previously allocated frames */
859 for (i=0; i<nFrames && imgs[i]; i++)
860 pXcursorImageDestroy( imgs[i] );
861 HeapFree( GetProcessHeap(), 0, imgs );
863 /* Cleanup all of the resources used to obtain the frame data */
864 if (hbmColor) DeleteObject( hbmColor );
865 if (hbmMask) DeleteObject( hbmMask );
866 HeapFree( GetProcessHeap(), 0, info );
867 return cursor;
870 #endif /* SONAME_LIBXCURSOR */
873 struct system_cursors
875 WORD id;
876 const char *names[8];
879 static const struct system_cursors user32_cursors[] =
881 { OCR_NORMAL, { "left_ptr" }},
882 { OCR_IBEAM, { "xterm", "text" }},
883 { OCR_WAIT, { "watch", "wait" }},
884 { OCR_CROSS, { "cross" }},
885 { OCR_UP, { "center_ptr" }},
886 { OCR_SIZE, { "fleur", "size_all" }},
887 { OCR_SIZEALL, { "fleur", "size_all" }},
888 { OCR_ICON, { "icon" }},
889 { OCR_SIZENWSE, { "top_left_corner", "nw-resize" }},
890 { OCR_SIZENESW, { "top_right_corner", "ne-resize" }},
891 { OCR_SIZEWE, { "h_double_arrow", "size_hor", "ew-resize" }},
892 { OCR_SIZENS, { "v_double_arrow", "size_ver", "ns-resize" }},
893 { OCR_NO, { "not-allowed", "forbidden", "no-drop" }},
894 { OCR_HAND, { "hand2", "pointer", "pointing-hand" }},
895 { OCR_APPSTARTING, { "left_ptr_watch" }},
896 { OCR_HELP, { "question_arrow", "help" }},
897 { 0 }
900 static const struct system_cursors comctl32_cursors[] =
902 { 102, { "move", "dnd-move" }},
903 { 104, { "copy", "dnd-copy" }},
904 { 105, { "left_ptr" }},
905 { 106, { "col-resize", "split_v" }},
906 { 107, { "col-resize", "split_v" }},
907 { 108, { "hand2", "pointer", "pointing-hand" }},
908 { 135, { "row-resize", "split_h" }},
909 { 0 }
912 static const struct system_cursors ole32_cursors[] =
914 { 1, { "no-drop", "dnd-no-drop" }},
915 { 2, { "move", "dnd-move" }},
916 { 3, { "copy", "dnd-copy" }},
917 { 4, { "alias", "dnd-link" }},
918 { 0 }
921 static const struct system_cursors riched20_cursors[] =
923 { 105, { "hand2", "pointer", "pointing-hand" }},
924 { 107, { "right_ptr" }},
925 { 109, { "copy", "dnd-copy" }},
926 { 110, { "move", "dnd-move" }},
927 { 111, { "no-drop", "dnd-no-drop" }},
928 { 0 }
931 static const struct
933 const struct system_cursors *cursors;
934 WCHAR name[16];
935 } module_cursors[] =
937 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
938 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
939 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
940 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
943 struct cursor_font_fallback
945 const char *name;
946 unsigned int shape;
949 static const struct cursor_font_fallback fallbacks[] =
951 { "X_cursor", XC_X_cursor },
952 { "arrow", XC_arrow },
953 { "based_arrow_down", XC_based_arrow_down },
954 { "based_arrow_up", XC_based_arrow_up },
955 { "boat", XC_boat },
956 { "bogosity", XC_bogosity },
957 { "bottom_left_corner", XC_bottom_left_corner },
958 { "bottom_right_corner", XC_bottom_right_corner },
959 { "bottom_side", XC_bottom_side },
960 { "bottom_tee", XC_bottom_tee },
961 { "box_spiral", XC_box_spiral },
962 { "center_ptr", XC_center_ptr },
963 { "circle", XC_circle },
964 { "clock", XC_clock },
965 { "coffee_mug", XC_coffee_mug },
966 { "col-resize", XC_sb_h_double_arrow },
967 { "cross", XC_cross },
968 { "cross_reverse", XC_cross_reverse },
969 { "crosshair", XC_crosshair },
970 { "diamond_cross", XC_diamond_cross },
971 { "dot", XC_dot },
972 { "dotbox", XC_dotbox },
973 { "double_arrow", XC_double_arrow },
974 { "draft_large", XC_draft_large },
975 { "draft_small", XC_draft_small },
976 { "draped_box", XC_draped_box },
977 { "exchange", XC_exchange },
978 { "fleur", XC_fleur },
979 { "gobbler", XC_gobbler },
980 { "gumby", XC_gumby },
981 { "h_double_arrow", XC_sb_h_double_arrow },
982 { "hand1", XC_hand1 },
983 { "hand2", XC_hand2 },
984 { "heart", XC_heart },
985 { "icon", XC_icon },
986 { "iron_cross", XC_iron_cross },
987 { "left_ptr", XC_left_ptr },
988 { "left_side", XC_left_side },
989 { "left_tee", XC_left_tee },
990 { "leftbutton", XC_leftbutton },
991 { "ll_angle", XC_ll_angle },
992 { "lr_angle", XC_lr_angle },
993 { "man", XC_man },
994 { "middlebutton", XC_middlebutton },
995 { "mouse", XC_mouse },
996 { "pencil", XC_pencil },
997 { "pirate", XC_pirate },
998 { "plus", XC_plus },
999 { "question_arrow", XC_question_arrow },
1000 { "right_ptr", XC_right_ptr },
1001 { "right_side", XC_right_side },
1002 { "right_tee", XC_right_tee },
1003 { "rightbutton", XC_rightbutton },
1004 { "row-resize", XC_sb_v_double_arrow },
1005 { "rtl_logo", XC_rtl_logo },
1006 { "sailboat", XC_sailboat },
1007 { "sb_down_arrow", XC_sb_down_arrow },
1008 { "sb_h_double_arrow", XC_sb_h_double_arrow },
1009 { "sb_left_arrow", XC_sb_left_arrow },
1010 { "sb_right_arrow", XC_sb_right_arrow },
1011 { "sb_up_arrow", XC_sb_up_arrow },
1012 { "sb_v_double_arrow", XC_sb_v_double_arrow },
1013 { "shuttle", XC_shuttle },
1014 { "sizing", XC_sizing },
1015 { "spider", XC_spider },
1016 { "spraycan", XC_spraycan },
1017 { "star", XC_star },
1018 { "target", XC_target },
1019 { "tcross", XC_tcross },
1020 { "top_left_arrow", XC_top_left_arrow },
1021 { "top_left_corner", XC_top_left_corner },
1022 { "top_right_corner", XC_top_right_corner },
1023 { "top_side", XC_top_side },
1024 { "top_tee", XC_top_tee },
1025 { "trek", XC_trek },
1026 { "ul_angle", XC_ul_angle },
1027 { "umbrella", XC_umbrella },
1028 { "ur_angle", XC_ur_angle },
1029 { "v_double_arrow", XC_sb_v_double_arrow },
1030 { "watch", XC_watch },
1031 { "xterm", XC_xterm }
1034 static int fallback_cmp( const void *key, const void *member )
1036 const struct cursor_font_fallback *fallback = member;
1037 return strcmp( key, fallback->name );
1040 static int find_fallback_shape( const char *name )
1042 struct cursor_font_fallback *fallback;
1044 if ((fallback = bsearch( name, fallbacks, ARRAY_SIZE( fallbacks ),
1045 sizeof(*fallback), fallback_cmp )))
1046 return fallback->shape;
1047 return -1;
1050 /***********************************************************************
1051 * create_xcursor_system_cursor
1053 * Create an X cursor for a system cursor.
1055 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
1057 static const WCHAR idW[] = {'%','h','u',0};
1058 const struct system_cursors *cursors;
1059 unsigned int i;
1060 Cursor cursor = 0;
1061 HMODULE module;
1062 HKEY key;
1063 const char * const *names = NULL;
1064 WCHAR *p, name[MAX_PATH * 2], valueW[64];
1065 char valueA[64];
1066 DWORD ret;
1068 if (!info->szModName[0]) return 0;
1070 p = strrchrW( info->szModName, '\\' );
1071 strcpyW( name, p ? p + 1 : info->szModName );
1072 p = name + strlenW( name );
1073 *p++ = ',';
1074 if (info->szResName[0]) strcpyW( p, info->szResName );
1075 else sprintfW( p, idW, info->wResID );
1076 valueA[0] = 0;
1078 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
1079 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver\\Cursors", &key ))
1081 DWORD size = sizeof(valueW);
1082 ret = RegQueryValueExW( key, name, NULL, NULL, (BYTE *)valueW, &size );
1083 RegCloseKey( key );
1084 if (!ret)
1086 if (!valueW[0]) return 0; /* force standard cursor */
1087 if (!WideCharToMultiByte( CP_UNIXCP, 0, valueW, -1, valueA, sizeof(valueA), NULL, NULL ))
1088 valueA[0] = 0;
1089 goto done;
1093 if (info->szResName[0]) goto done; /* only integer resources are supported here */
1094 if (!(module = GetModuleHandleW( info->szModName ))) goto done;
1096 for (i = 0; i < ARRAY_SIZE( module_cursors ); i++)
1097 if (GetModuleHandleW( module_cursors[i].name ) == module) break;
1098 if (i == ARRAY_SIZE( module_cursors )) goto done;
1100 cursors = module_cursors[i].cursors;
1101 for (i = 0; cursors[i].id; i++)
1102 if (cursors[i].id == info->wResID)
1104 strcpy( valueA, cursors[i].names[0] );
1105 names = cursors[i].names;
1106 break;
1109 done:
1110 if (valueA[0])
1112 #ifdef SONAME_LIBXCURSOR
1113 if (pXcursorLibraryLoadCursor)
1115 if (!names)
1116 cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
1117 else
1118 while (*names && !cursor) cursor = pXcursorLibraryLoadCursor( gdi_display, *names++ );
1120 #endif
1121 if (!cursor)
1123 int shape = find_fallback_shape( valueA );
1124 if (shape != -1) cursor = XCreateFontCursor( gdi_display, shape );
1126 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
1127 debugstr_w(name), debugstr_a(valueA) );
1129 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
1130 return cursor;
1134 /***********************************************************************
1135 * create_xlib_monochrome_cursor
1137 * Create a monochrome X cursor from a Windows one.
1139 static Cursor create_xlib_monochrome_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1141 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1142 BITMAPINFO *info = (BITMAPINFO *)buffer;
1143 const int and_y = 0;
1144 const int xor_y = height;
1145 unsigned int width_bytes = (width + 31) / 32 * 4;
1146 unsigned char *mask_bits = NULL;
1147 GC gc;
1148 XColor fg, bg;
1149 XVisualInfo vis = default_visual;
1150 Pixmap src_pixmap, bits_pixmap, mask_pixmap;
1151 struct gdi_image_bits bits;
1152 Cursor cursor = 0;
1154 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1155 info->bmiHeader.biWidth = width;
1156 info->bmiHeader.biHeight = -height * 2;
1157 info->bmiHeader.biPlanes = 1;
1158 info->bmiHeader.biBitCount = 1;
1159 info->bmiHeader.biCompression = BI_RGB;
1160 info->bmiHeader.biSizeImage = width_bytes * height * 2;
1161 info->bmiHeader.biXPelsPerMeter = 0;
1162 info->bmiHeader.biYPelsPerMeter = 0;
1163 info->bmiHeader.biClrUsed = 0;
1164 info->bmiHeader.biClrImportant = 0;
1166 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1167 if (!GetDIBits( hdc, icon->hbmMask, 0, height * 2, mask_bits, info, DIB_RGB_COLORS )) 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 HeapFree( GetProcessHeap(), 0, mask_bits );
1223 return cursor;
1226 /***********************************************************************
1227 * create_xlib_load_mono_cursor
1229 * Create a monochrome X cursor from a color Windows one by trying to load the monochrome resource.
1231 static Cursor create_xlib_load_mono_cursor( HDC hdc, HANDLE handle, int width, int height )
1233 Cursor cursor = None;
1234 HANDLE mono;
1235 ICONINFOEXW info;
1236 BITMAP bm;
1238 if (!(mono = CopyImage( handle, IMAGE_CURSOR, width, height, LR_MONOCHROME | LR_COPYFROMRESOURCE )))
1239 return None;
1241 info.cbSize = sizeof(info);
1242 if (GetIconInfoExW( mono, &info ))
1244 if (!info.hbmColor)
1246 GetObjectW( info.hbmMask, sizeof(bm), &bm );
1247 bm.bmHeight = max( 1, bm.bmHeight / 2 );
1248 /* make sure hotspot is valid */
1249 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1251 info.xHotspot = bm.bmWidth / 2;
1252 info.yHotspot = bm.bmHeight / 2;
1254 cursor = create_xlib_monochrome_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1256 else DeleteObject( info.hbmColor );
1257 DeleteObject( info.hbmMask );
1259 DestroyCursor( mono );
1260 return cursor;
1263 /***********************************************************************
1264 * create_xlib_color_cursor
1266 * Create a color X cursor from a Windows one.
1268 static Cursor create_xlib_color_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1270 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1271 BITMAPINFO *info = (BITMAPINFO *)buffer;
1272 XColor fg, bg;
1273 Cursor cursor = None;
1274 XVisualInfo vis = default_visual;
1275 Pixmap xor_pixmap, mask_pixmap;
1276 struct gdi_image_bits bits;
1277 unsigned int *color_bits = NULL, *ptr;
1278 unsigned char *mask_bits = NULL, *xor_bits = NULL;
1279 int i, x, y;
1280 BOOL has_alpha = FALSE;
1281 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
1282 unsigned int width_bytes = (width + 31) / 32 * 4;
1284 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1285 info->bmiHeader.biWidth = width;
1286 info->bmiHeader.biHeight = -height;
1287 info->bmiHeader.biPlanes = 1;
1288 info->bmiHeader.biBitCount = 1;
1289 info->bmiHeader.biCompression = BI_RGB;
1290 info->bmiHeader.biSizeImage = width_bytes * height;
1291 info->bmiHeader.biXPelsPerMeter = 0;
1292 info->bmiHeader.biYPelsPerMeter = 0;
1293 info->bmiHeader.biClrUsed = 0;
1294 info->bmiHeader.biClrImportant = 0;
1296 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1297 if (!GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS )) goto done;
1299 info->bmiHeader.biBitCount = 32;
1300 info->bmiHeader.biSizeImage = width * height * 4;
1301 if (!(color_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1302 if (!(xor_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, width_bytes * height ))) goto done;
1303 GetDIBits( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS );
1305 /* compute fg/bg color and xor bitmap based on average of the color values */
1307 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
1308 for (y = 0, ptr = color_bits; y < height; y++)
1310 for (x = 0; x < width; x++, ptr++)
1312 int red = (*ptr >> 16) & 0xff;
1313 int green = (*ptr >> 8) & 0xff;
1314 int blue = (*ptr >> 0) & 0xff;
1315 if (red + green + blue > 0x40)
1317 rfg += red;
1318 gfg += green;
1319 bfg += blue;
1320 fgBits++;
1321 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1323 else
1325 rbg += red;
1326 gbg += green;
1327 bbg += blue;
1331 if (fgBits)
1333 fg.red = rfg * 257 / fgBits;
1334 fg.green = gfg * 257 / fgBits;
1335 fg.blue = bfg * 257 / fgBits;
1337 else fg.red = fg.green = fg.blue = 0;
1338 bgBits = width * height - fgBits;
1339 if (bgBits)
1341 bg.red = rbg * 257 / bgBits;
1342 bg.green = gbg * 257 / bgBits;
1343 bg.blue = bbg * 257 / bgBits;
1345 else bg.red = bg.green = bg.blue = 0;
1347 info->bmiHeader.biBitCount = 1;
1348 info->bmiHeader.biClrUsed = 0;
1349 info->bmiHeader.biSizeImage = width_bytes * height;
1351 /* generate mask from the alpha channel if we have one */
1353 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
1354 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
1356 if (has_alpha)
1358 memset( mask_bits, 0, width_bytes * height );
1359 for (y = 0, ptr = color_bits; y < height; y++)
1360 for (x = 0; x < width; x++, ptr++)
1361 if ((*ptr >> 24) > 25) /* more than 10% alpha */
1362 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1364 else /* invert the mask */
1366 unsigned int j;
1368 ptr = (unsigned int *)mask_bits;
1369 for (j = 0; j < info->bmiHeader.biSizeImage / sizeof(*ptr); j++, ptr++) *ptr ^= ~0u;
1372 vis.depth = 1;
1373 bits.ptr = xor_bits;
1374 bits.free = NULL;
1375 bits.is_copy = TRUE;
1376 if (!(xor_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1378 bits.ptr = mask_bits;
1379 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
1381 if (mask_pixmap)
1383 cursor = XCreatePixmapCursor( gdi_display, xor_pixmap, mask_pixmap,
1384 &fg, &bg, icon->xHotspot, icon->yHotspot );
1385 XFreePixmap( gdi_display, mask_pixmap );
1387 XFreePixmap( gdi_display, xor_pixmap );
1389 done:
1390 HeapFree( GetProcessHeap(), 0, color_bits );
1391 HeapFree( GetProcessHeap(), 0, xor_bits );
1392 HeapFree( GetProcessHeap(), 0, mask_bits );
1393 return cursor;
1396 /***********************************************************************
1397 * create_cursor
1399 * Create an X cursor from a Windows one.
1401 static Cursor create_cursor( HANDLE handle )
1403 Cursor cursor = 0;
1404 ICONINFOEXW info;
1405 BITMAP bm;
1406 HDC hdc;
1408 if (!handle) return get_empty_cursor();
1410 info.cbSize = sizeof(info);
1411 if (!GetIconInfoExW( handle, &info )) return 0;
1413 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
1415 DeleteObject( info.hbmColor );
1416 DeleteObject( info.hbmMask );
1417 return cursor;
1420 GetObjectW( info.hbmMask, sizeof(bm), &bm );
1421 if (!info.hbmColor) bm.bmHeight = max( 1, bm.bmHeight / 2 );
1423 /* make sure hotspot is valid */
1424 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1426 info.xHotspot = bm.bmWidth / 2;
1427 info.yHotspot = bm.bmHeight / 2;
1430 hdc = CreateCompatibleDC( 0 );
1432 if (info.hbmColor)
1434 #ifdef SONAME_LIBXCURSOR
1435 if (pXcursorImagesLoadCursor)
1436 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
1437 #endif
1438 if (!cursor) cursor = create_xlib_load_mono_cursor( hdc, handle, bm.bmWidth, bm.bmHeight );
1439 if (!cursor) cursor = create_xlib_color_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1440 DeleteObject( info.hbmColor );
1442 else
1444 cursor = create_xlib_monochrome_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1447 DeleteObject( info.hbmMask );
1448 DeleteDC( hdc );
1449 return cursor;
1452 /***********************************************************************
1453 * DestroyCursorIcon (X11DRV.@)
1455 void CDECL X11DRV_DestroyCursorIcon( HCURSOR handle )
1457 Cursor cursor;
1459 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1461 TRACE( "%p xid %lx\n", handle, cursor );
1462 XFreeCursor( gdi_display, cursor );
1463 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1467 /***********************************************************************
1468 * SetCursor (X11DRV.@)
1470 void CDECL X11DRV_SetCursor( HCURSOR handle )
1472 if (InterlockedExchangePointer( (void **)&last_cursor, handle ) != handle ||
1473 GetTickCount() - last_cursor_change > 100)
1475 last_cursor_change = GetTickCount();
1476 if (cursor_window) SendNotifyMessageW( cursor_window, WM_X11DRV_SET_CURSOR, 0, (LPARAM)handle );
1480 /***********************************************************************
1481 * SetCursorPos (X11DRV.@)
1483 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
1485 struct x11drv_thread_data *data = x11drv_init_thread_data();
1486 POINT pos = virtual_screen_to_root( x, y );
1488 if (keyboard_grabbed)
1490 WARN( "refusing to warp to %u, %u\n", pos.x, pos.y );
1491 return FALSE;
1494 if (!clipping_cursor &&
1495 XGrabPointer( data->display, root_window, False,
1496 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1497 GrabModeAsync, GrabModeAsync, None, None, CurrentTime ) != GrabSuccess)
1499 WARN( "refusing to warp pointer to %u, %u without exclusive grab\n", pos.x, pos.y );
1500 return FALSE;
1503 XWarpPointer( data->display, root_window, root_window, 0, 0, 0, 0, pos.x, pos.y );
1504 data->warp_serial = NextRequest( data->display );
1506 if (!clipping_cursor)
1507 XUngrabPointer( data->display, CurrentTime );
1509 XNoOp( data->display );
1510 XFlush( data->display ); /* avoids bad mouse lag in games that do their own mouse warping */
1511 TRACE( "warped to %d,%d serial %lu\n", x, y, data->warp_serial );
1512 return TRUE;
1515 /***********************************************************************
1516 * GetCursorPos (X11DRV.@)
1518 BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
1520 Display *display = thread_init_display();
1521 Window root, child;
1522 int rootX, rootY, winX, winY;
1523 unsigned int xstate;
1524 BOOL ret;
1526 ret = XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &winX, &winY, &xstate );
1527 if (ret)
1529 POINT old = *pos;
1530 *pos = root_to_virtual_screen( winX, winY );
1531 TRACE( "pointer at %s server pos %s\n", wine_dbgstr_point(pos), wine_dbgstr_point(&old) );
1533 return ret;
1536 /***********************************************************************
1537 * ClipCursor (X11DRV.@)
1539 BOOL CDECL X11DRV_ClipCursor( LPCRECT clip )
1541 RECT virtual_rect = get_virtual_screen_rect();
1543 if (!clip) clip = &virtual_rect;
1545 if (grab_pointer)
1547 HWND foreground = GetForegroundWindow();
1548 DWORD tid, pid;
1550 /* forward request to the foreground window if it's in a different thread */
1551 tid = GetWindowThreadProcessId( foreground, &pid );
1552 if (tid && tid != GetCurrentThreadId() && pid == GetCurrentProcessId())
1554 TRACE( "forwarding clip request to %p\n", foreground );
1555 SendNotifyMessageW( foreground, WM_X11DRV_CLIP_CURSOR_REQUEST, FALSE, FALSE );
1556 return TRUE;
1559 /* we are clipping if the clip rectangle is smaller than the screen */
1560 if (clip->left > virtual_rect.left || clip->right < virtual_rect.right ||
1561 clip->top > virtual_rect.top || clip->bottom < virtual_rect.bottom)
1563 if (grab_clipping_window( clip )) return TRUE;
1565 else /* if currently clipping, check if we should switch to fullscreen clipping */
1567 struct x11drv_thread_data *data = x11drv_thread_data();
1568 if (data && data->clip_hwnd)
1570 if (EqualRect( clip, &clip_rect ) || clip_fullscreen_window( foreground, TRUE ))
1571 return TRUE;
1575 ungrab_clipping_window();
1576 return TRUE;
1579 /***********************************************************************
1580 * clip_cursor_request
1582 * Function called upon receiving a WM_X11DRV_CLIP_CURSOR_REQUEST.
1584 LRESULT clip_cursor_request( HWND hwnd, BOOL fullscreen, BOOL reset )
1586 RECT clip;
1588 if (hwnd == GetDesktopWindow())
1589 WARN( "ignoring clip cursor request on desktop window.\n" );
1590 else if (hwnd != GetForegroundWindow())
1591 WARN( "ignoring clip cursor request on non-foreground window.\n" );
1592 else if (fullscreen)
1593 clip_fullscreen_window( hwnd, reset );
1594 else
1596 GetClipCursor( &clip );
1597 X11DRV_ClipCursor( &clip );
1600 return 0;
1603 /***********************************************************************
1604 * move_resize_window
1606 void move_resize_window( HWND hwnd, int dir )
1608 Display *display = thread_display();
1609 DWORD pt;
1610 POINT pos;
1611 int button = 0;
1612 XEvent xev;
1613 Window win, root, child;
1614 unsigned int xstate;
1616 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1618 pt = GetMessagePos();
1619 pos = virtual_screen_to_root( (short)LOWORD( pt ), (short)HIWORD( pt ) );
1621 if (GetKeyState( VK_LBUTTON ) & 0x8000) button = 1;
1622 else if (GetKeyState( VK_MBUTTON ) & 0x8000) button = 2;
1623 else if (GetKeyState( VK_RBUTTON ) & 0x8000) button = 3;
1625 TRACE( "hwnd %p/%lx, pos %s, dir %d, button %d\n", hwnd, win, wine_dbgstr_point(&pos), dir, button );
1627 xev.xclient.type = ClientMessage;
1628 xev.xclient.window = win;
1629 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1630 xev.xclient.serial = 0;
1631 xev.xclient.display = display;
1632 xev.xclient.send_event = True;
1633 xev.xclient.format = 32;
1634 xev.xclient.data.l[0] = pos.x; /* x coord */
1635 xev.xclient.data.l[1] = pos.y; /* y coord */
1636 xev.xclient.data.l[2] = dir; /* direction */
1637 xev.xclient.data.l[3] = button; /* button */
1638 xev.xclient.data.l[4] = 0; /* unused */
1640 /* need to ungrab the pointer that may have been automatically grabbed
1641 * with a ButtonPress event */
1642 XUngrabPointer( display, CurrentTime );
1643 XSendEvent(display, root_window, False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
1645 /* try to detect the end of the size/move by polling for the mouse button to be released */
1646 /* (some apps don't like it if we return before the size/move is done) */
1648 if (!button) return;
1649 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1651 for (;;)
1653 MSG msg;
1654 INPUT input;
1655 int x, y, rootX, rootY;
1657 if (!XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &x, &y, &xstate )) break;
1659 if (!(xstate & (Button1Mask << (button - 1))))
1661 /* fake a button release event */
1662 pos = root_to_virtual_screen( x, y );
1663 input.type = INPUT_MOUSE;
1664 input.u.mi.dx = pos.x;
1665 input.u.mi.dy = pos.y;
1666 input.u.mi.mouseData = button_up_data[button - 1];
1667 input.u.mi.dwFlags = button_up_flags[button - 1] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1668 input.u.mi.time = GetTickCount();
1669 input.u.mi.dwExtraInfo = 0;
1670 __wine_send_input( hwnd, &input );
1673 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
1675 if (!CallMsgFilterW( &msg, MSGF_SIZE ))
1677 TranslateMessage( &msg );
1678 DispatchMessageW( &msg );
1682 if (!(xstate & (Button1Mask << (button - 1)))) break;
1683 MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT );
1686 TRACE( "hwnd %p/%lx done\n", hwnd, win );
1687 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1691 /***********************************************************************
1692 * X11DRV_ButtonPress
1694 BOOL X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1696 XButtonEvent *event = &xev->xbutton;
1697 int buttonNum = event->button - 1;
1698 INPUT input;
1700 if (buttonNum >= NB_BUTTONS) return FALSE;
1702 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1704 input.u.mi.dx = event->x;
1705 input.u.mi.dy = event->y;
1706 input.u.mi.mouseData = button_down_data[buttonNum];
1707 input.u.mi.dwFlags = button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1708 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1709 input.u.mi.dwExtraInfo = 0;
1711 update_user_time( event->time );
1712 map_event_coords( hwnd, event->window, &input );
1713 send_mouse_input( hwnd, event->window, event->state, &input );
1714 return TRUE;
1718 /***********************************************************************
1719 * X11DRV_ButtonRelease
1721 BOOL X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1723 XButtonEvent *event = &xev->xbutton;
1724 int buttonNum = event->button - 1;
1725 INPUT input;
1727 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return FALSE;
1729 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1731 input.u.mi.dx = event->x;
1732 input.u.mi.dy = event->y;
1733 input.u.mi.mouseData = button_up_data[buttonNum];
1734 input.u.mi.dwFlags = button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1735 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1736 input.u.mi.dwExtraInfo = 0;
1738 map_event_coords( hwnd, event->window, &input );
1739 send_mouse_input( hwnd, event->window, event->state, &input );
1740 return TRUE;
1744 /***********************************************************************
1745 * X11DRV_MotionNotify
1747 BOOL X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1749 XMotionEvent *event = &xev->xmotion;
1750 INPUT input;
1752 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d serial %lu\n",
1753 hwnd, event->window, event->x, event->y, event->is_hint, event->serial );
1755 input.u.mi.dx = event->x;
1756 input.u.mi.dy = event->y;
1757 input.u.mi.mouseData = 0;
1758 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1759 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1760 input.u.mi.dwExtraInfo = 0;
1762 if (!hwnd && is_old_motion_event( event->serial ))
1764 TRACE( "pos %d,%d old serial %lu, ignoring\n", input.u.mi.dx, input.u.mi.dy, event->serial );
1765 return FALSE;
1767 map_event_coords( hwnd, event->window, &input );
1768 send_mouse_input( hwnd, event->window, event->state, &input );
1769 return TRUE;
1773 /***********************************************************************
1774 * X11DRV_EnterNotify
1776 BOOL X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1778 XCrossingEvent *event = &xev->xcrossing;
1779 INPUT input;
1781 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd, event->window, event->x, event->y, event->detail );
1783 if (event->detail == NotifyVirtual) return FALSE;
1784 if (hwnd == x11drv_thread_data()->grab_hwnd) return FALSE;
1786 /* simulate a mouse motion event */
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 (is_old_motion_event( event->serial ))
1796 TRACE( "pos %d,%d old serial %lu, ignoring\n", input.u.mi.dx, input.u.mi.dy, event->serial );
1797 return FALSE;
1799 map_event_coords( hwnd, event->window, &input );
1800 send_mouse_input( hwnd, event->window, event->state, &input );
1801 return TRUE;
1804 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1806 /***********************************************************************
1807 * X11DRV_DeviceChanged
1809 static BOOL X11DRV_DeviceChanged( XGenericEventCookie *xev )
1811 XIDeviceChangedEvent *event = xev->data;
1812 struct x11drv_thread_data *data = x11drv_thread_data();
1814 if (event->deviceid != data->xi2_core_pointer) return FALSE;
1815 if (event->reason != XISlaveSwitch) return FALSE;
1817 update_relative_valuators( event->classes, event->num_classes );
1818 data->xi2_current_slave = event->sourceid;
1819 return TRUE;
1822 /***********************************************************************
1823 * X11DRV_RawMotion
1825 static BOOL X11DRV_RawMotion( XGenericEventCookie *xev )
1827 XIRawEvent *event = xev->data;
1828 const double *values = event->valuators.values;
1829 RECT virtual_rect;
1830 INPUT input;
1831 int i;
1832 double dx = 0, dy = 0, val;
1833 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1834 struct x11drv_valuator_data *x_rel, *y_rel;
1836 if (thread_data->x_rel_valuator.number < 0 || thread_data->y_rel_valuator.number < 0) return FALSE;
1837 if (!event->valuators.mask_len) return FALSE;
1838 if (thread_data->xi2_state != xi_enabled) return FALSE;
1840 /* If there is no slave currently detected, no previous motion nor device
1841 * change events were received. Look it up now on the device list in this
1842 * case.
1844 if (!thread_data->xi2_current_slave)
1846 XIDeviceInfo *devices = thread_data->xi2_devices;
1848 for (i = 0; i < thread_data->xi2_device_count; i++)
1850 if (devices[i].use != XISlavePointer) continue;
1851 if (devices[i].deviceid != event->deviceid) continue;
1852 if (devices[i].attachment != thread_data->xi2_core_pointer) continue;
1853 thread_data->xi2_current_slave = event->deviceid;
1854 break;
1858 if (event->deviceid != thread_data->xi2_current_slave) return FALSE;
1860 x_rel = &thread_data->x_rel_valuator;
1861 y_rel = &thread_data->y_rel_valuator;
1863 input.u.mi.mouseData = 0;
1864 input.u.mi.dwFlags = MOUSEEVENTF_MOVE;
1865 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1866 input.u.mi.dwExtraInfo = 0;
1867 input.u.mi.dx = 0;
1868 input.u.mi.dy = 0;
1870 virtual_rect = get_virtual_screen_rect();
1872 for (i = 0; i <= max ( x_rel->number, y_rel->number ); i++)
1874 if (!XIMaskIsSet( event->valuators.mask, i )) continue;
1875 val = *values++;
1876 if (i == x_rel->number)
1878 input.u.mi.dx = dx = val;
1879 if (x_rel->min < x_rel->max)
1880 input.u.mi.dx = val * (virtual_rect.right - virtual_rect.left)
1881 / (x_rel->max - x_rel->min);
1883 if (i == y_rel->number)
1885 input.u.mi.dy = dy = val;
1886 if (y_rel->min < y_rel->max)
1887 input.u.mi.dy = val * (virtual_rect.bottom - virtual_rect.top)
1888 / (y_rel->max - y_rel->min);
1892 if (broken_rawevents && is_old_motion_event( xev->serial ))
1894 TRACE( "pos %d,%d old serial %lu, ignoring\n", input.u.mi.dx, input.u.mi.dy, xev->serial );
1895 return FALSE;
1898 TRACE( "pos %d,%d (event %f,%f)\n", input.u.mi.dx, input.u.mi.dy, dx, dy );
1900 input.type = INPUT_MOUSE;
1901 __wine_send_input( 0, &input );
1902 return TRUE;
1905 #endif /* HAVE_X11_EXTENSIONS_XINPUT2_H */
1908 /***********************************************************************
1909 * X11DRV_XInput2_Init
1911 void X11DRV_XInput2_Init(void)
1913 #if defined(SONAME_LIBXI) && defined(HAVE_X11_EXTENSIONS_XINPUT2_H)
1914 int event, error;
1915 void *libxi_handle = dlopen( SONAME_LIBXI, RTLD_NOW );
1917 if (!libxi_handle)
1919 WARN( "couldn't load %s\n", SONAME_LIBXI );
1920 return;
1922 #define LOAD_FUNCPTR(f) \
1923 if (!(p##f = dlsym( libxi_handle, #f))) \
1925 WARN("Failed to load %s.\n", #f); \
1926 return; \
1929 LOAD_FUNCPTR(XIGetClientPointer);
1930 LOAD_FUNCPTR(XIFreeDeviceInfo);
1931 LOAD_FUNCPTR(XIQueryDevice);
1932 LOAD_FUNCPTR(XIQueryVersion);
1933 LOAD_FUNCPTR(XISelectEvents);
1934 #undef LOAD_FUNCPTR
1936 xinput2_available = XQueryExtension( gdi_display, "XInputExtension", &xinput2_opcode, &event, &error );
1938 /* Until version 1.10.4 rawinput was broken in XOrg, see
1939 * https://bugs.freedesktop.org/show_bug.cgi?id=30068 */
1940 broken_rawevents = strstr(XServerVendor( gdi_display ), "X.Org") &&
1941 XVendorRelease( gdi_display ) < 11004000;
1943 #else
1944 TRACE( "X Input 2 support not compiled in.\n" );
1945 #endif
1949 /***********************************************************************
1950 * X11DRV_GenericEvent
1952 BOOL X11DRV_GenericEvent( HWND hwnd, XEvent *xev )
1954 BOOL ret = FALSE;
1955 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1956 XGenericEventCookie *event = &xev->xcookie;
1958 if (!event->data) return FALSE;
1959 if (event->extension != xinput2_opcode) return FALSE;
1961 switch (event->evtype)
1963 case XI_DeviceChanged:
1964 ret = X11DRV_DeviceChanged( event );
1965 break;
1966 case XI_RawMotion:
1967 ret = X11DRV_RawMotion( event );
1968 break;
1970 default:
1971 TRACE( "Unhandled event %#x\n", event->evtype );
1972 break;
1974 #endif
1975 return ret;