winex11: Release the clip cursor grab when the clipping window loses focus.
[wine/multimedia.git] / dlls / winex11.drv / mouse.c
blob1e783d28ab91139922e1d0cd724ee44fcfb38fa0
1 /*
2 * X11 mouse driver
4 * Copyright 1998 Ulrich Weigand
5 * Copyright 2007 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <X11/Xlib.h>
26 #include <X11/cursorfont.h>
27 #include <stdarg.h>
28 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
29 #include <X11/extensions/XInput2.h>
30 #endif
32 #ifdef SONAME_LIBXCURSOR
33 # include <X11/Xcursor/Xcursor.h>
34 static void *xcursor_handle;
35 # define MAKE_FUNCPTR(f) static typeof(f) * p##f
36 MAKE_FUNCPTR(XcursorImageCreate);
37 MAKE_FUNCPTR(XcursorImageDestroy);
38 MAKE_FUNCPTR(XcursorImageLoadCursor);
39 MAKE_FUNCPTR(XcursorImagesCreate);
40 MAKE_FUNCPTR(XcursorImagesDestroy);
41 MAKE_FUNCPTR(XcursorImagesLoadCursor);
42 MAKE_FUNCPTR(XcursorLibraryLoadCursor);
43 # undef MAKE_FUNCPTR
44 #endif /* SONAME_LIBXCURSOR */
46 #define NONAMELESSUNION
47 #define NONAMELESSSTRUCT
48 #define OEMRESOURCE
49 #include "windef.h"
50 #include "winbase.h"
51 #include "winreg.h"
53 #include "x11drv.h"
54 #include "wine/server.h"
55 #include "wine/library.h"
56 #include "wine/unicode.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
61 /**********************************************************************/
63 #ifndef Button6Mask
64 #define Button6Mask (1<<13)
65 #endif
66 #ifndef Button7Mask
67 #define Button7Mask (1<<14)
68 #endif
70 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
72 static const UINT button_down_flags[NB_BUTTONS] =
74 MOUSEEVENTF_LEFTDOWN,
75 MOUSEEVENTF_MIDDLEDOWN,
76 MOUSEEVENTF_RIGHTDOWN,
77 MOUSEEVENTF_WHEEL,
78 MOUSEEVENTF_WHEEL,
79 MOUSEEVENTF_XDOWN, /* FIXME: horizontal wheel */
80 MOUSEEVENTF_XDOWN,
81 MOUSEEVENTF_XDOWN,
82 MOUSEEVENTF_XDOWN
85 static const UINT button_up_flags[NB_BUTTONS] =
87 MOUSEEVENTF_LEFTUP,
88 MOUSEEVENTF_MIDDLEUP,
89 MOUSEEVENTF_RIGHTUP,
92 MOUSEEVENTF_XUP,
93 MOUSEEVENTF_XUP,
94 MOUSEEVENTF_XUP,
95 MOUSEEVENTF_XUP
98 static const UINT button_down_data[NB_BUTTONS] =
103 WHEEL_DELTA,
104 -WHEEL_DELTA,
105 XBUTTON1,
106 XBUTTON2,
107 XBUTTON1,
108 XBUTTON2
111 static const UINT button_up_data[NB_BUTTONS] =
118 XBUTTON1,
119 XBUTTON2,
120 XBUTTON1,
121 XBUTTON2
124 static HWND cursor_window;
125 static HCURSOR last_cursor;
126 static DWORD last_cursor_change;
127 static XContext cursor_context;
128 static RECT clip_rect;
129 static Cursor create_cursor( HANDLE handle );
131 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
132 static BOOL xinput2_available;
133 static int xinput2_opcode;
134 static int xinput2_core_pointer;
135 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
136 MAKE_FUNCPTR(XIFreeDeviceInfo);
137 MAKE_FUNCPTR(XIQueryDevice);
138 MAKE_FUNCPTR(XIQueryVersion);
139 MAKE_FUNCPTR(XISelectEvents);
140 #undef MAKE_FUNCPTR
141 #endif
143 /***********************************************************************
144 * X11DRV_Xcursor_Init
146 * Load the Xcursor library for use.
148 void X11DRV_Xcursor_Init(void)
150 #ifdef SONAME_LIBXCURSOR
151 xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
152 if (!xcursor_handle) /* wine_dlopen failed. */
154 WARN("Xcursor failed to load. Using fallback code.\n");
155 return;
157 #define LOAD_FUNCPTR(f) \
158 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
160 LOAD_FUNCPTR(XcursorImageCreate);
161 LOAD_FUNCPTR(XcursorImageDestroy);
162 LOAD_FUNCPTR(XcursorImageLoadCursor);
163 LOAD_FUNCPTR(XcursorImagesCreate);
164 LOAD_FUNCPTR(XcursorImagesDestroy);
165 LOAD_FUNCPTR(XcursorImagesLoadCursor);
166 LOAD_FUNCPTR(XcursorLibraryLoadCursor);
167 #undef LOAD_FUNCPTR
168 #endif /* SONAME_LIBXCURSOR */
172 /***********************************************************************
173 * get_empty_cursor
175 static Cursor get_empty_cursor(void)
177 static Cursor cursor;
178 static const char data[] = { 0 };
180 wine_tsx11_lock();
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 = XCreatePixmapCursor( gdi_display, pixmap, pixmap, &bg, &bg, 0, 0 );
191 XFreePixmap( gdi_display, pixmap );
194 wine_tsx11_unlock();
195 return cursor;
198 /***********************************************************************
199 * set_window_cursor
201 void set_window_cursor( Window window, HCURSOR handle )
203 Cursor cursor, prev;
205 wine_tsx11_lock();
206 if (!handle) cursor = get_empty_cursor();
207 else if (!cursor_context || XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
209 /* try to create it */
210 wine_tsx11_unlock();
211 if (!(cursor = create_cursor( handle ))) return;
213 wine_tsx11_lock();
214 if (!cursor_context) cursor_context = XUniqueContext();
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 );
228 XDefineCursor( gdi_display, window, cursor );
229 /* make the change take effect immediately */
230 XFlush( gdi_display );
231 wine_tsx11_unlock();
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 /***********************************************************************
253 * enable_xinput2
255 static void enable_xinput2(void)
257 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
258 struct x11drv_thread_data *data = x11drv_thread_data();
259 XIDeviceInfo *devices;
260 XIEventMask mask;
261 unsigned char mask_bits[XIMaskLen(XI_LASTEVENT)];
262 int i, count;
264 if (!xinput2_available) return;
266 if (data->xi2_state == xi_unknown)
268 int major = 2, minor = 0;
269 wine_tsx11_lock();
270 if (!pXIQueryVersion( data->display, &major, &minor )) data->xi2_state = xi_disabled;
271 else
273 data->xi2_state = xi_unavailable;
274 WARN( "X Input 2 not available\n" );
276 wine_tsx11_unlock();
278 if (data->xi2_state == xi_unavailable) return;
280 wine_tsx11_lock();
281 devices = pXIQueryDevice( data->display, XIAllDevices, &count );
282 for (i = 0; i < count; ++i)
284 if (devices[i].use != XIMasterPointer) continue;
285 TRACE( "Using %u (%s) as core pointer\n",
286 devices[i].deviceid, debugstr_a(devices[i].name) );
287 xinput2_core_pointer = devices[i].deviceid;
288 break;
291 mask.mask = mask_bits;
292 mask.mask_len = sizeof(mask_bits);
293 memset( mask_bits, 0, sizeof(mask_bits) );
295 XISetMask( mask_bits, XI_RawButtonPress );
296 XISetMask( mask_bits, XI_RawButtonRelease );
297 XISetMask( mask_bits, XI_RawMotion );
299 for (i = 0; i < count; ++i)
301 if (devices[i].use == XISlavePointer && devices[i].attachment == xinput2_core_pointer)
303 TRACE( "Device %u (%s) is attached to the core pointer\n",
304 devices[i].deviceid, debugstr_a(devices[i].name) );
305 mask.deviceid = devices[i].deviceid;
306 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
307 data->xi2_state = xi_enabled;
311 pXIFreeDeviceInfo( devices );
312 wine_tsx11_unlock();
313 #endif
316 /***********************************************************************
317 * disable_xinput2
319 static void disable_xinput2(void)
321 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
322 struct x11drv_thread_data *data = x11drv_thread_data();
323 XIEventMask mask;
324 XIDeviceInfo *devices;
325 int i, count;
327 if (data->xi2_state != xi_enabled) return;
329 TRACE( "disabling\n" );
330 data->xi2_state = xi_disabled;
332 mask.mask = NULL;
333 mask.mask_len = 0;
335 wine_tsx11_lock();
336 devices = pXIQueryDevice( data->display, XIAllDevices, &count );
337 for (i = 0; i < count; ++i)
339 if (devices[i].use == XISlavePointer && devices[i].attachment == xinput2_core_pointer)
341 mask.deviceid = devices[i].deviceid;
342 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
345 pXIFreeDeviceInfo( devices );
346 wine_tsx11_unlock();
347 #endif
350 /***********************************************************************
351 * create_clipping_msg_window
353 static HWND create_clipping_msg_window(void)
355 static const WCHAR class_name[] = {'_','_','x','1','1','d','r','v','_','c','l','i','p','_','c','l','a','s','s',0};
356 static ATOM clip_class;
358 if (!clip_class)
360 WNDCLASSW class;
361 ATOM atom;
363 memset( &class, 0, sizeof(class) );
364 class.lpfnWndProc = DefWindowProcW;
365 class.hInstance = GetModuleHandleW(0);
366 class.lpszClassName = class_name;
367 if ((atom = RegisterClassW( &class ))) clip_class = atom;
369 return CreateWindowW( class_name, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, GetModuleHandleW(0), NULL );
372 /***********************************************************************
373 * clip_cursor_notify
375 * Notification function called upon receiving a WM_X11DRV_CLIP_CURSOR.
377 LRESULT clip_cursor_notify( HWND hwnd, HWND new_clip_hwnd )
379 struct x11drv_thread_data *data = x11drv_thread_data();
381 if (hwnd == GetDesktopWindow()) /* change the clip window stored in the desktop process */
383 static HWND clip_hwnd;
385 HWND prev = clip_hwnd;
386 clip_hwnd = new_clip_hwnd;
387 if (prev || new_clip_hwnd) TRACE( "clip hwnd changed from %p to %p\n", prev, new_clip_hwnd );
388 if (prev) SendNotifyMessageW( prev, WM_X11DRV_CLIP_CURSOR, 0, 0 );
390 else if (hwnd == data->clip_hwnd) /* this is a notification that clipping has been reset */
392 data->clip_hwnd = 0;
393 disable_xinput2();
394 DestroyWindow( hwnd );
396 return 0;
399 /***********************************************************************
400 * grab_clipping_window
402 * Start a pointer grab on the clip window.
404 static BOOL grab_clipping_window( const RECT *clip )
406 struct x11drv_thread_data *data = x11drv_init_thread_data();
407 Window clip_window = init_clip_window();
408 HWND msg_hwnd = 0;
410 if (!clip_window) return TRUE;
412 /* create a clip message window unless we are already clipping */
413 if (!data->clip_hwnd && !(msg_hwnd = create_clipping_msg_window())) return TRUE;
415 TRACE( "clipping to %s\n", wine_dbgstr_rect(clip) );
417 wine_tsx11_lock();
418 if (msg_hwnd) XUnmapWindow( data->display, clip_window );
419 XMoveResizeWindow( data->display, clip_window,
420 clip->left - virtual_screen_rect.left, clip->top - virtual_screen_rect.top,
421 clip->right - clip->left, clip->bottom - clip->top );
422 if (msg_hwnd) XMapWindow( data->display, clip_window );
423 if (!XGrabPointer( data->display, clip_window, False,
424 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
425 GrabModeAsync, GrabModeAsync, clip_window, None, CurrentTime ))
426 clipping_cursor = 1;
427 wine_tsx11_unlock();
429 if (!clipping_cursor)
431 if (msg_hwnd) DestroyWindow( msg_hwnd );
432 return FALSE;
434 if (msg_hwnd)
436 data->clip_hwnd = msg_hwnd;
437 enable_xinput2();
438 sync_window_cursor( clip_window );
439 clip_rect = *clip;
440 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, (LPARAM)msg_hwnd );
442 return TRUE;
445 /***********************************************************************
446 * ungrab_clipping_window
448 * Release the pointer grab on the clip window.
450 void ungrab_clipping_window(void)
452 Display *display = thread_init_display();
453 Window clip_window = init_clip_window();
455 if (!clip_window) return;
457 TRACE( "no longer clipping\n" );
458 wine_tsx11_lock();
459 XUnmapWindow( display, clip_window );
460 wine_tsx11_unlock();
461 clipping_cursor = 0;
462 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, 0 );
465 /***********************************************************************
466 * send_mouse_input
468 * Update the various window states on a mouse event.
470 static void send_mouse_input( HWND hwnd, Window window, unsigned int state, INPUT *input )
472 struct x11drv_win_data *data;
473 POINT pt;
475 input->type = INPUT_MOUSE;
477 if (!hwnd && window == x11drv_thread_data()->clip_window)
479 input->u.mi.dx += clip_rect.left;
480 input->u.mi.dy += clip_rect.top;
481 if (x11drv_thread_data()->xi2_state != xi_enabled) __wine_send_input( hwnd, input );
482 return;
485 if (!(data = X11DRV_get_win_data( hwnd ))) return;
487 if (window == data->whole_window)
489 input->u.mi.dx += data->whole_rect.left - data->client_rect.left;
490 input->u.mi.dy += data->whole_rect.top - data->client_rect.top;
492 if (window == root_window)
494 input->u.mi.dx += virtual_screen_rect.left;
495 input->u.mi.dy += virtual_screen_rect.top;
497 pt.x = input->u.mi.dx;
498 pt.y = input->u.mi.dy;
499 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
500 pt.x = data->client_rect.right - data->client_rect.left - 1 - pt.x;
501 MapWindowPoints( hwnd, 0, &pt, 1 );
503 if (InterlockedExchangePointer( (void **)&cursor_window, hwnd ) != hwnd ||
504 GetTickCount() - last_cursor_change > 100)
506 sync_window_cursor( data->whole_window );
507 last_cursor_change = GetTickCount();
510 if (hwnd != GetDesktopWindow()) hwnd = GetAncestor( hwnd, GA_ROOT );
512 /* update the wine server Z-order */
514 if (window != x11drv_thread_data()->grab_window &&
515 /* ignore event if a button is pressed, since the mouse is then grabbed too */
516 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
518 RECT rect;
519 SetRect( &rect, pt.x, pt.y, pt.x + 1, pt.y + 1 );
520 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
522 SERVER_START_REQ( update_window_zorder )
524 req->window = wine_server_user_handle( hwnd );
525 req->rect.left = rect.left;
526 req->rect.top = rect.top;
527 req->rect.right = rect.right;
528 req->rect.bottom = rect.bottom;
529 wine_server_call( req );
531 SERVER_END_REQ;
534 input->u.mi.dx = pt.x;
535 input->u.mi.dy = pt.y;
536 __wine_send_input( hwnd, input );
539 #ifdef SONAME_LIBXCURSOR
541 /***********************************************************************
542 * create_xcursor_frame
544 * Use Xcursor to create a frame of an X cursor from a Windows one.
546 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
547 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
548 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
549 int width, int height, int istep )
551 XcursorImage *image, *ret = NULL;
552 DWORD delay_jiffies, num_steps;
553 int x, y, i, has_alpha = FALSE;
554 XcursorPixel *ptr;
556 wine_tsx11_lock();
557 image = pXcursorImageCreate( width, height );
558 wine_tsx11_unlock();
559 if (!image)
561 ERR("X11 failed to produce a cursor frame!\n");
562 goto cleanup;
565 image->xhot = iinfo->xHotspot;
566 image->yhot = iinfo->yHotspot;
568 image->delay = 100; /* fallback delay, 100 ms */
569 if (GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, istep, &delay_jiffies, &num_steps) != 0)
570 image->delay = (100 * delay_jiffies) / 6; /* convert jiffies (1/60s) to milliseconds */
571 else
572 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep);
574 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
575 memset( color_bits, 0x00, color_size );
576 SelectObject( hdc, hbmColor );
577 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
579 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
580 goto cleanup;
582 memcpy( image->pixels, color_bits, color_size );
584 /* check if the cursor frame was drawn with an alpha channel */
585 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
586 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
588 /* if no alpha channel was drawn then generate it from the mask */
589 if (!has_alpha)
591 unsigned int width_bytes = (width + 31) / 32 * 4;
593 /* draw the cursor mask to a temporary buffer */
594 memset( mask_bits, 0xFF, mask_size );
595 SelectObject( hdc, hbmMask );
596 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
598 ERR("Failed to draw frame mask %d.\n", istep);
599 goto cleanup;
601 /* use the buffer to directly modify the XcursorImage alpha channel */
602 for (y = 0, ptr = image->pixels; y < height; y++)
603 for (x = 0; x < width; x++, ptr++)
604 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
605 *ptr |= 0xff000000;
607 ret = image;
609 cleanup:
610 if (ret == NULL) pXcursorImageDestroy( image );
611 return ret;
614 /***********************************************************************
615 * create_xcursor_cursor
617 * Use Xcursor to create an X cursor from a Windows one.
619 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
621 unsigned char *color_bits, *mask_bits;
622 HBITMAP hbmColor = 0, hbmMask = 0;
623 DWORD nFrames, delay_jiffies, i;
624 int color_size, mask_size;
625 BITMAPINFO *info = NULL;
626 XcursorImages *images;
627 XcursorImage **imgs;
628 Cursor cursor = 0;
630 /* Retrieve the number of frames to render */
631 if (!GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, 0, &delay_jiffies, &nFrames)) return 0;
632 if (!(imgs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XcursorImage*)*nFrames ))) return 0;
634 /* Allocate all of the resources necessary to obtain a cursor frame */
635 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
636 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
637 info->bmiHeader.biWidth = width;
638 info->bmiHeader.biHeight = -height;
639 info->bmiHeader.biPlanes = 1;
640 info->bmiHeader.biCompression = BI_RGB;
641 info->bmiHeader.biXPelsPerMeter = 0;
642 info->bmiHeader.biYPelsPerMeter = 0;
643 info->bmiHeader.biClrUsed = 0;
644 info->bmiHeader.biClrImportant = 0;
645 info->bmiHeader.biBitCount = 32;
646 color_size = width * height * 4;
647 info->bmiHeader.biSizeImage = color_size;
648 hbmColor = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &color_bits, NULL, 0);
649 if (!hbmColor)
651 ERR("Failed to create DIB section for cursor color data!\n");
652 goto cleanup;
654 info->bmiHeader.biBitCount = 1;
655 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
656 info->bmiHeader.biSizeImage = mask_size;
657 hbmMask = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &mask_bits, NULL, 0);
658 if (!hbmMask)
660 ERR("Failed to create DIB section for cursor mask data!\n");
661 goto cleanup;
664 /* Create an XcursorImage for each frame of the cursor */
665 for (i=0; i<nFrames; i++)
667 imgs[i] = create_xcursor_frame( hdc, iinfo, icon,
668 hbmColor, color_bits, color_size,
669 hbmMask, mask_bits, mask_size,
670 width, height, i );
671 if (!imgs[i]) goto cleanup;
674 /* Build an X cursor out of all of the frames */
675 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
676 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
677 images->images[images->nimage] = imgs[images->nimage];
678 wine_tsx11_lock();
679 cursor = pXcursorImagesLoadCursor( gdi_display, images );
680 wine_tsx11_unlock();
681 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
682 HeapFree( GetProcessHeap(), 0, imgs );
683 imgs = NULL;
685 cleanup:
686 if (imgs)
688 /* Failed to produce a cursor, free previously allocated frames */
689 for (i=0; i<nFrames && imgs[i]; i++)
690 pXcursorImageDestroy( imgs[i] );
691 HeapFree( GetProcessHeap(), 0, imgs );
693 /* Cleanup all of the resources used to obtain the frame data */
694 if (hbmColor) DeleteObject( hbmColor );
695 if (hbmMask) DeleteObject( hbmMask );
696 HeapFree( GetProcessHeap(), 0, info );
697 return cursor;
701 struct system_cursors
703 WORD id;
704 const char *name;
707 static const struct system_cursors user32_cursors[] =
709 { OCR_NORMAL, "left_ptr" },
710 { OCR_IBEAM, "xterm" },
711 { OCR_WAIT, "watch" },
712 { OCR_CROSS, "cross" },
713 { OCR_UP, "center_ptr" },
714 { OCR_SIZE, "fleur" },
715 { OCR_SIZEALL, "fleur" },
716 { OCR_ICON, "icon" },
717 { OCR_SIZENWSE, "nwse-resize" },
718 { OCR_SIZENESW, "nesw-resize" },
719 { OCR_SIZEWE, "ew-resize" },
720 { OCR_SIZENS, "ns-resize" },
721 { OCR_NO, "not-allowed" },
722 { OCR_HAND, "hand2" },
723 { OCR_APPSTARTING, "left_ptr_watch" },
724 { OCR_HELP, "question_arrow" },
725 { 0 }
728 static const struct system_cursors comctl32_cursors[] =
730 { 102, "move" },
731 { 104, "copy" },
732 { 105, "left_ptr" },
733 { 106, "row-resize" },
734 { 107, "row-resize" },
735 { 108, "hand2" },
736 { 135, "col-resize" },
737 { 0 }
740 static const struct system_cursors ole32_cursors[] =
742 { 1, "no-drop" },
743 { 2, "move" },
744 { 3, "copy" },
745 { 4, "alias" },
746 { 0 }
749 static const struct system_cursors riched20_cursors[] =
751 { 105, "hand2" },
752 { 107, "right_ptr" },
753 { 109, "copy" },
754 { 110, "move" },
755 { 111, "no-drop" },
756 { 0 }
759 static const struct
761 const struct system_cursors *cursors;
762 WCHAR name[16];
763 } module_cursors[] =
765 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
766 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
767 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
768 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
771 /***********************************************************************
772 * create_xcursor_system_cursor
774 * Create an X cursor for a system cursor.
776 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
778 static const WCHAR idW[] = {'%','h','u',0};
779 const struct system_cursors *cursors;
780 unsigned int i;
781 Cursor cursor = 0;
782 HMODULE module;
783 HKEY key;
784 WCHAR *p, name[MAX_PATH * 2], valueW[64];
785 char valueA[64];
786 DWORD size, ret;
788 if (!pXcursorLibraryLoadCursor) return 0;
789 if (!info->szModName[0]) return 0;
791 p = strrchrW( info->szModName, '\\' );
792 strcpyW( name, p ? p + 1 : info->szModName );
793 p = name + strlenW( name );
794 *p++ = ',';
795 if (info->szResName[0]) strcpyW( p, info->szResName );
796 else sprintfW( p, idW, info->wResID );
797 valueA[0] = 0;
799 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
800 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver\\Cursors", &key ))
802 size = sizeof(valueW) / sizeof(WCHAR);
803 ret = RegQueryValueExW( key, name, NULL, NULL, (BYTE *)valueW, &size );
804 RegCloseKey( key );
805 if (!ret)
807 if (!valueW[0]) return 0; /* force standard cursor */
808 if (!WideCharToMultiByte( CP_UNIXCP, 0, valueW, -1, valueA, sizeof(valueA), NULL, NULL ))
809 valueA[0] = 0;
810 goto done;
814 if (info->szResName[0]) goto done; /* only integer resources are supported here */
815 if (!(module = GetModuleHandleW( info->szModName ))) goto done;
817 for (i = 0; i < sizeof(module_cursors)/sizeof(module_cursors[0]); i++)
818 if (GetModuleHandleW( module_cursors[i].name ) == module) break;
819 if (i == sizeof(module_cursors)/sizeof(module_cursors[0])) goto done;
821 cursors = module_cursors[i].cursors;
822 for (i = 0; cursors[i].id; i++)
823 if (cursors[i].id == info->wResID)
825 strcpy( valueA, cursors[i].name );
826 break;
829 done:
830 if (valueA[0])
832 wine_tsx11_lock();
833 cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
834 wine_tsx11_unlock();
835 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
836 debugstr_w(name), debugstr_a(valueA) );
838 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
839 return cursor;
842 #endif /* SONAME_LIBXCURSOR */
845 /***********************************************************************
846 * create_cursor_from_bitmaps
848 * Create an X11 cursor from source bitmaps.
850 static Cursor create_cursor_from_bitmaps( HBITMAP src_xor, HBITMAP src_and, int width, int height,
851 int xor_y, int and_y, XColor *fg, XColor *bg,
852 int hotspot_x, int hotspot_y )
854 HDC src = 0, dst = 0;
855 HBITMAP bits = 0, mask = 0, mask_inv = 0;
856 Cursor cursor = 0;
858 if (!(src = CreateCompatibleDC( 0 ))) goto done;
859 if (!(dst = CreateCompatibleDC( 0 ))) goto done;
861 if (!(bits = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
862 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
863 if (!(mask_inv = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
865 /* We have to do some magic here, as cursors are not fully
866 * compatible between Windows and X11. Under X11, there are
867 * only 3 possible color cursor: black, white and masked. So
868 * we map the 4th Windows color (invert the bits on the screen)
869 * to black and an additional white bit on an other place
870 * (+1,+1). This require some boolean arithmetic:
872 * Windows | X11
873 * And Xor Result | Bits Mask Result
874 * 0 0 black | 0 1 background
875 * 0 1 white | 1 1 foreground
876 * 1 0 no change | X 0 no change
877 * 1 1 inverted | 0 1 background
879 * which gives:
880 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
881 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
883 SelectObject( src, src_and );
884 SelectObject( dst, bits );
885 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
886 SelectObject( dst, mask );
887 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
888 SelectObject( dst, mask_inv );
889 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
890 SelectObject( src, src_xor );
891 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, SRCAND /* src & dst */ );
892 SelectObject( dst, bits );
893 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, SRCERASE /* src & ~dst */ );
894 SelectObject( dst, mask );
895 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, 0xdd0228 /* src | ~dst */ );
896 /* additional white */
897 SelectObject( src, mask_inv );
898 BitBlt( dst, 1, 1, width, height, src, 0, 0, SRCPAINT /* src | dst */);
899 SelectObject( dst, bits );
900 BitBlt( dst, 1, 1, width, height, src, 0, 0, SRCPAINT /* src | dst */ );
902 wine_tsx11_lock();
903 cursor = XCreatePixmapCursor( gdi_display, X11DRV_get_pixmap(bits), X11DRV_get_pixmap(mask),
904 fg, bg, hotspot_x, hotspot_y );
905 wine_tsx11_unlock();
907 done:
908 DeleteDC( src );
909 DeleteDC( dst );
910 DeleteObject( bits );
911 DeleteObject( mask );
912 DeleteObject( mask_inv );
913 return cursor;
916 /***********************************************************************
917 * create_xlib_cursor
919 * Create an X cursor from a Windows one.
921 static Cursor create_xlib_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
923 XColor fg, bg;
924 Cursor cursor = None;
925 HBITMAP xor_bitmap = 0;
926 BITMAPINFO *info;
927 unsigned int *color_bits = NULL, *ptr;
928 unsigned char *mask_bits = NULL, *xor_bits = NULL;
929 int i, x, y, has_alpha = 0;
930 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
931 unsigned int width_bytes = (width + 31) / 32 * 4;
933 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] ))))
934 return FALSE;
935 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
936 info->bmiHeader.biWidth = width;
937 info->bmiHeader.biHeight = -height;
938 info->bmiHeader.biPlanes = 1;
939 info->bmiHeader.biBitCount = 1;
940 info->bmiHeader.biCompression = BI_RGB;
941 info->bmiHeader.biSizeImage = width_bytes * height;
942 info->bmiHeader.biXPelsPerMeter = 0;
943 info->bmiHeader.biYPelsPerMeter = 0;
944 info->bmiHeader.biClrUsed = 0;
945 info->bmiHeader.biClrImportant = 0;
947 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
948 if (!GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS )) goto done;
950 info->bmiHeader.biBitCount = 32;
951 info->bmiHeader.biSizeImage = width * height * 4;
952 if (!(color_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
953 if (!(xor_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, width_bytes * height ))) goto done;
954 GetDIBits( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS );
956 /* compute fg/bg color and xor bitmap based on average of the color values */
958 if (!(xor_bitmap = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
959 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
960 for (y = 0, ptr = color_bits; y < height; y++)
962 for (x = 0; x < width; x++, ptr++)
964 int red = (*ptr >> 16) & 0xff;
965 int green = (*ptr >> 8) & 0xff;
966 int blue = (*ptr >> 0) & 0xff;
967 if (red + green + blue > 0x40)
969 rfg += red;
970 gfg += green;
971 bfg += blue;
972 fgBits++;
973 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
975 else
977 rbg += red;
978 gbg += green;
979 bbg += blue;
983 if (fgBits)
985 fg.red = rfg * 257 / fgBits;
986 fg.green = gfg * 257 / fgBits;
987 fg.blue = bfg * 257 / fgBits;
989 else fg.red = fg.green = fg.blue = 0;
990 bgBits = width * height - fgBits;
991 if (bgBits)
993 bg.red = rbg * 257 / bgBits;
994 bg.green = gbg * 257 / bgBits;
995 bg.blue = bbg * 257 / bgBits;
997 else bg.red = bg.green = bg.blue = 0;
999 info->bmiHeader.biBitCount = 1;
1000 info->bmiHeader.biSizeImage = width_bytes * height;
1001 SetDIBits( hdc, xor_bitmap, 0, height, xor_bits, info, DIB_RGB_COLORS );
1003 /* generate mask from the alpha channel if we have one */
1005 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
1006 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
1008 if (has_alpha)
1010 memset( mask_bits, 0, width_bytes * height );
1011 for (y = 0, ptr = color_bits; y < height; y++)
1012 for (x = 0; x < width; x++, ptr++)
1013 if ((*ptr >> 24) > 25) /* more than 10% alpha */
1014 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1016 info->bmiHeader.biBitCount = 1;
1017 info->bmiHeader.biSizeImage = width_bytes * height;
1018 SetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS );
1020 wine_tsx11_lock();
1021 cursor = XCreatePixmapCursor( gdi_display,
1022 X11DRV_get_pixmap(xor_bitmap),
1023 X11DRV_get_pixmap(icon->hbmMask),
1024 &fg, &bg, icon->xHotspot, icon->yHotspot );
1025 wine_tsx11_unlock();
1027 else
1029 cursor = create_cursor_from_bitmaps( xor_bitmap, icon->hbmMask, width, height, 0, 0,
1030 &fg, &bg, icon->xHotspot, icon->yHotspot );
1033 done:
1034 DeleteObject( xor_bitmap );
1035 HeapFree( GetProcessHeap(), 0, info );
1036 HeapFree( GetProcessHeap(), 0, color_bits );
1037 HeapFree( GetProcessHeap(), 0, xor_bits );
1038 HeapFree( GetProcessHeap(), 0, mask_bits );
1039 return cursor;
1042 /***********************************************************************
1043 * create_cursor
1045 * Create an X cursor from a Windows one.
1047 static Cursor create_cursor( HANDLE handle )
1049 Cursor cursor = 0;
1050 ICONINFOEXW info;
1051 BITMAP bm;
1053 if (!handle) return get_empty_cursor();
1055 info.cbSize = sizeof(info);
1056 if (!GetIconInfoExW( handle, &info )) return 0;
1058 #ifdef SONAME_LIBXCURSOR
1059 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
1061 DeleteObject( info.hbmColor );
1062 DeleteObject( info.hbmMask );
1063 return cursor;
1065 #endif
1067 GetObjectW( info.hbmMask, sizeof(bm), &bm );
1068 if (!info.hbmColor) bm.bmHeight /= 2;
1070 /* make sure hotspot is valid */
1071 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1073 info.xHotspot = bm.bmWidth / 2;
1074 info.yHotspot = bm.bmHeight / 2;
1077 if (info.hbmColor)
1079 HDC hdc = CreateCompatibleDC( 0 );
1080 if (hdc)
1082 #ifdef SONAME_LIBXCURSOR
1083 if (pXcursorImagesLoadCursor)
1084 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
1085 #endif
1086 if (!cursor) cursor = create_xlib_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1088 DeleteObject( info.hbmColor );
1089 DeleteDC( hdc );
1091 else
1093 XColor fg, bg;
1094 fg.red = fg.green = fg.blue = 0xffff;
1095 bg.red = bg.green = bg.blue = 0;
1096 cursor = create_cursor_from_bitmaps( info.hbmMask, info.hbmMask, bm.bmWidth, bm.bmHeight,
1097 bm.bmHeight, 0, &fg, &bg, info.xHotspot, info.yHotspot );
1100 DeleteObject( info.hbmMask );
1101 return cursor;
1104 /***********************************************************************
1105 * DestroyCursorIcon (X11DRV.@)
1107 void CDECL X11DRV_DestroyCursorIcon( HCURSOR handle )
1109 Cursor cursor;
1111 wine_tsx11_lock();
1112 if (cursor_context && !XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1114 TRACE( "%p xid %lx\n", handle, cursor );
1115 XFreeCursor( gdi_display, cursor );
1116 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1118 wine_tsx11_unlock();
1121 /***********************************************************************
1122 * SetCursor (X11DRV.@)
1124 void CDECL X11DRV_SetCursor( HCURSOR handle )
1126 if (InterlockedExchangePointer( (void **)&last_cursor, handle ) != handle ||
1127 GetTickCount() - last_cursor_change > 100)
1129 last_cursor_change = GetTickCount();
1130 if (clipping_cursor) set_window_cursor( init_clip_window(), handle );
1131 else if (cursor_window) SendNotifyMessageW( cursor_window, WM_X11DRV_SET_CURSOR, 0, (LPARAM)handle );
1135 /***********************************************************************
1136 * SetCursorPos (X11DRV.@)
1138 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
1140 struct x11drv_thread_data *data = x11drv_init_thread_data();
1142 if (data->xi2_state == xi_enabled) return TRUE;
1144 TRACE( "warping to (%d,%d)\n", x, y );
1146 wine_tsx11_lock();
1147 XWarpPointer( data->display, root_window, root_window, 0, 0, 0, 0,
1148 x - virtual_screen_rect.left, y - virtual_screen_rect.top );
1149 XFlush( data->display ); /* avoids bad mouse lag in games that do their own mouse warping */
1150 wine_tsx11_unlock();
1151 return TRUE;
1154 /***********************************************************************
1155 * GetCursorPos (X11DRV.@)
1157 BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
1159 Display *display = thread_init_display();
1160 Window root, child;
1161 int rootX, rootY, winX, winY;
1162 unsigned int xstate;
1163 BOOL ret;
1165 wine_tsx11_lock();
1166 ret = XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &winX, &winY, &xstate );
1167 if (ret)
1169 pos->x = winX + virtual_screen_rect.left;
1170 pos->y = winY + virtual_screen_rect.top;
1171 TRACE("pointer at (%d,%d)\n", pos->x, pos->y );
1173 wine_tsx11_unlock();
1174 return ret;
1177 /***********************************************************************
1178 * ClipCursor (X11DRV.@)
1180 BOOL CDECL X11DRV_ClipCursor( LPCRECT clip )
1182 /* we are clipping if the clip rectangle is smaller than the screen */
1183 if (clip && (clip->left > virtual_screen_rect.left ||
1184 clip->right < virtual_screen_rect.right ||
1185 clip->top > virtual_screen_rect.top ||
1186 clip->bottom < virtual_screen_rect.bottom))
1188 if (GetWindowThreadProcessId( GetDesktopWindow(), NULL ) == GetCurrentThreadId())
1189 return TRUE; /* don't clip in the desktop process */
1191 if (grab_pointer && grab_clipping_window( clip )) return TRUE;
1194 ungrab_clipping_window();
1195 return TRUE;
1198 /***********************************************************************
1199 * X11DRV_ButtonPress
1201 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1203 XButtonEvent *event = &xev->xbutton;
1204 int buttonNum = event->button - 1;
1205 INPUT input;
1207 if (buttonNum >= NB_BUTTONS) return;
1209 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1211 input.u.mi.dx = event->x;
1212 input.u.mi.dy = event->y;
1213 input.u.mi.mouseData = button_down_data[buttonNum];
1214 input.u.mi.dwFlags = button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1215 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1216 input.u.mi.dwExtraInfo = 0;
1218 update_user_time( event->time );
1219 send_mouse_input( hwnd, event->window, event->state, &input );
1223 /***********************************************************************
1224 * X11DRV_ButtonRelease
1226 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1228 XButtonEvent *event = &xev->xbutton;
1229 int buttonNum = event->button - 1;
1230 INPUT input;
1232 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1234 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1236 input.u.mi.dx = event->x;
1237 input.u.mi.dy = event->y;
1238 input.u.mi.mouseData = button_up_data[buttonNum];
1239 input.u.mi.dwFlags = button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1240 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1241 input.u.mi.dwExtraInfo = 0;
1243 send_mouse_input( hwnd, event->window, event->state, &input );
1247 /***********************************************************************
1248 * X11DRV_MotionNotify
1250 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1252 XMotionEvent *event = &xev->xmotion;
1253 INPUT input;
1255 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d\n", hwnd, event->window, event->x, event->y, event->is_hint );
1257 input.u.mi.dx = event->x;
1258 input.u.mi.dy = event->y;
1259 input.u.mi.mouseData = 0;
1260 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1261 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1262 input.u.mi.dwExtraInfo = 0;
1264 send_mouse_input( hwnd, event->window, event->state, &input );
1268 /***********************************************************************
1269 * X11DRV_EnterNotify
1271 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1273 XCrossingEvent *event = &xev->xcrossing;
1274 INPUT input;
1276 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd, event->window, event->x, event->y, event->detail );
1278 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1279 if (event->window == x11drv_thread_data()->grab_window) return;
1281 /* simulate a mouse motion event */
1282 input.u.mi.dx = event->x;
1283 input.u.mi.dy = event->y;
1284 input.u.mi.mouseData = 0;
1285 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1286 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1287 input.u.mi.dwExtraInfo = 0;
1289 send_mouse_input( hwnd, event->window, event->state, &input );
1292 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1294 /***********************************************************************
1295 * X11DRV_RawButtonPress
1297 static void X11DRV_RawButtonPress( XIRawEvent *event )
1299 int button = event->detail - 1;
1300 INPUT input;
1302 if (button >= NB_BUTTONS) return;
1304 TRACE( "button %u\n", button );
1306 input.type = INPUT_MOUSE;
1307 input.u.mi.dx = 0;
1308 input.u.mi.dy = 0;
1309 input.u.mi.mouseData = button_down_data[button];
1310 input.u.mi.dwFlags = button_down_flags[button];
1311 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1312 input.u.mi.dwExtraInfo = 0;
1314 update_user_time( event->time );
1315 input.type = INPUT_MOUSE;
1316 __wine_send_input( 0, &input );
1320 /***********************************************************************
1321 * X11DRV_RawButtonRelease
1323 static void X11DRV_RawButtonRelease( XIRawEvent *event )
1325 int button = event->detail - 1;
1326 INPUT input;
1328 if (button >= NB_BUTTONS) return;
1330 TRACE( "button %u\n", button );
1332 input.u.mi.dx = 0;
1333 input.u.mi.dy = 0;
1334 input.u.mi.mouseData = button_up_data[button];
1335 input.u.mi.dwFlags = button_up_flags[button];
1336 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1337 input.u.mi.dwExtraInfo = 0;
1339 input.type = INPUT_MOUSE;
1340 __wine_send_input( 0, &input );
1344 /***********************************************************************
1345 * X11DRV_RawMotion
1347 static void X11DRV_RawMotion( XIRawEvent *event )
1349 const double *values = event->valuators.values;
1350 INPUT input;
1352 if (!event->valuators.mask_len) return;
1354 input.u.mi.dx = 0;
1355 input.u.mi.dy = 0;
1356 input.u.mi.mouseData = 0;
1357 input.u.mi.dwFlags = MOUSEEVENTF_MOVE;
1358 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1359 input.u.mi.dwExtraInfo = 0;
1361 if (XIMaskIsSet( event->valuators.mask, 0 )) input.u.mi.dx = *values++;
1362 if (XIMaskIsSet( event->valuators.mask, 1 )) input.u.mi.dy = *values++;
1364 TRACE( "pos %d,%d\n", input.u.mi.dx, input.u.mi.dy );
1366 input.type = INPUT_MOUSE;
1367 __wine_send_input( 0, &input );
1370 #endif /* HAVE_X11_EXTENSIONS_XINPUT2_H */
1373 /***********************************************************************
1374 * X11DRV_XInput2_Init
1376 void X11DRV_XInput2_Init(void)
1378 #if defined(SONAME_LIBXI) && defined(HAVE_X11_EXTENSIONS_XINPUT2_H)
1379 int event, error;
1380 void *libxi_handle = wine_dlopen( SONAME_LIBXI, RTLD_NOW, NULL, 0 );
1382 if (!libxi_handle)
1384 WARN( "couldn't load %s\n", SONAME_LIBXI );
1385 return;
1387 #define LOAD_FUNCPTR(f) \
1388 if (!(p##f = wine_dlsym( libxi_handle, #f, NULL, 0))) \
1390 WARN("Failed to load %s.\n", #f); \
1391 return; \
1394 LOAD_FUNCPTR(XIFreeDeviceInfo);
1395 LOAD_FUNCPTR(XIQueryDevice);
1396 LOAD_FUNCPTR(XIQueryVersion);
1397 LOAD_FUNCPTR(XISelectEvents);
1398 #undef LOAD_FUNCPTR
1400 wine_tsx11_lock();
1401 xinput2_available = XQueryExtension( gdi_display, "XInputExtension", &xinput2_opcode, &event, &error );
1402 wine_tsx11_unlock();
1403 #else
1404 TRACE( "X Input 2 support not compiled in.\n" );
1405 #endif
1409 /***********************************************************************
1410 * X11DRV_GenericEvent
1412 void X11DRV_GenericEvent( HWND hwnd, XEvent *xev )
1414 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1415 XGenericEventCookie *event = &xev->xcookie;
1417 if (!event->data) return;
1418 if (event->extension != xinput2_opcode) return;
1420 switch (event->evtype)
1422 case XI_RawButtonPress:
1423 X11DRV_RawButtonPress( event->data );
1424 break;
1426 case XI_RawButtonRelease:
1427 X11DRV_RawButtonRelease( event->data );
1428 break;
1430 case XI_RawMotion:
1431 X11DRV_RawMotion( event->data );
1432 break;
1434 default:
1435 TRACE( "Unhandled event %#x\n", event->evtype );
1436 break;
1438 #endif