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
23 #include "wine/port.h"
26 #include <X11/cursorfont.h>
28 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
29 #include <X11/extensions/XInput2.h>
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);
44 #endif /* SONAME_LIBXCURSOR */
46 #define NONAMELESSUNION
47 #define NONAMELESSSTRUCT
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 /**********************************************************************/
64 #define Button6Mask (1<<13)
67 #define Button7Mask (1<<14)
70 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
72 static const UINT button_down_flags[NB_BUTTONS] =
75 MOUSEEVENTF_MIDDLEDOWN,
76 MOUSEEVENTF_RIGHTDOWN,
79 MOUSEEVENTF_XDOWN, /* FIXME: horizontal wheel */
85 static const UINT button_up_flags[NB_BUTTONS] =
98 static const UINT button_down_data[NB_BUTTONS] =
111 static const UINT button_up_data[NB_BUTTONS] =
124 XContext cursor_context = 0;
126 static HWND cursor_window;
127 static HCURSOR last_cursor;
128 static DWORD last_cursor_change;
129 static RECT clip_rect;
130 static Cursor create_cursor( HANDLE handle );
132 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
133 static BOOL xinput2_available;
134 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
135 MAKE_FUNCPTR(XIFreeDeviceInfo);
136 MAKE_FUNCPTR(XIQueryDevice);
137 MAKE_FUNCPTR(XIQueryVersion);
138 MAKE_FUNCPTR(XISelectEvents);
142 /***********************************************************************
143 * X11DRV_Xcursor_Init
145 * Load the Xcursor library for use.
147 void X11DRV_Xcursor_Init(void)
149 #ifdef SONAME_LIBXCURSOR
150 xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
151 if (!xcursor_handle) /* wine_dlopen failed. */
153 WARN("Xcursor failed to load. Using fallback code.\n");
156 #define LOAD_FUNCPTR(f) \
157 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
159 LOAD_FUNCPTR(XcursorImageCreate);
160 LOAD_FUNCPTR(XcursorImageDestroy);
161 LOAD_FUNCPTR(XcursorImageLoadCursor);
162 LOAD_FUNCPTR(XcursorImagesCreate);
163 LOAD_FUNCPTR(XcursorImagesDestroy);
164 LOAD_FUNCPTR(XcursorImagesLoadCursor);
165 LOAD_FUNCPTR(XcursorLibraryLoadCursor);
167 #endif /* SONAME_LIBXCURSOR */
171 /***********************************************************************
174 static Cursor get_empty_cursor(void)
176 static Cursor cursor;
177 static const char data[] = { 0 };
184 bg.red = bg.green = bg.blue = 0x0000;
185 pixmap = XCreateBitmapFromData( gdi_display, root_window, data, 1, 1 );
188 Cursor new = XCreatePixmapCursor( gdi_display, pixmap, pixmap, &bg, &bg, 0, 0 );
189 if (InterlockedCompareExchangePointer( (void **)&cursor, (void *)new, 0 ))
190 XFreeCursor( gdi_display, new );
191 XFreePixmap( gdi_display, pixmap );
197 /***********************************************************************
200 void set_window_cursor( Window window, HCURSOR handle )
204 if (!handle) cursor = get_empty_cursor();
205 else if (XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
207 /* try to create it */
208 if (!(cursor = create_cursor( handle ))) return;
210 XLockDisplay( gdi_display );
211 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&prev ))
213 /* someone else was here first */
214 XFreeCursor( gdi_display, cursor );
219 XSaveContext( gdi_display, (XID)handle, cursor_context, (char *)cursor );
220 TRACE( "cursor %p created %lx\n", handle, cursor );
222 XUnlockDisplay( gdi_display );
225 XDefineCursor( gdi_display, window, cursor );
226 /* make the change take effect immediately */
227 XFlush( gdi_display );
230 /***********************************************************************
233 void sync_window_cursor( Window window )
237 SERVER_START_REQ( set_cursor )
240 wine_server_call( req );
241 cursor = reply->prev_count >= 0 ? wine_server_ptr_handle( reply->prev_handle ) : 0;
245 set_window_cursor( window, cursor );
248 /***********************************************************************
251 static void enable_xinput2(void)
253 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
254 struct x11drv_thread_data *data = x11drv_thread_data();
256 XIDeviceInfo *devices;
257 unsigned char mask_bits[XIMaskLen(XI_LASTEVENT)];
260 if (!xinput2_available) return;
262 if (data->xi2_state == xi_unknown)
264 int major = 2, minor = 0;
265 if (!pXIQueryVersion( data->display, &major, &minor )) data->xi2_state = xi_disabled;
268 data->xi2_state = xi_unavailable;
269 WARN( "X Input 2 not available\n" );
272 if (data->xi2_state == xi_unavailable) return;
274 if (data->xi2_devices) pXIFreeDeviceInfo( data->xi2_devices );
275 data->xi2_devices = devices = pXIQueryDevice( data->display, XIAllDevices, &data->xi2_device_count );
276 for (i = 0; i < data->xi2_device_count; ++i)
278 if (devices[i].use != XIMasterPointer) continue;
279 for (j = count = 0; j < devices[i].num_classes; j++)
281 XIValuatorClassInfo *class = (XIValuatorClassInfo *)devices[i].classes[j];
283 if (devices[i].classes[j]->type != XIValuatorClass) continue;
284 TRACE( "Device %u (%s) num %u %f,%f res %u mode %u label %s\n",
285 devices[i].deviceid, debugstr_a(devices[i].name),
286 class->number, class->min, class->max, class->resolution, class->mode,
287 XGetAtomName( data->display, class->label ));
288 if (class->label == x11drv_atom( Rel_X ) || class->label == x11drv_atom( Rel_Y )) count++;
289 /* workaround for drivers that don't provide labels */
290 if (!class->label && class->number <= 1 && class->mode == XIModeRelative) count++;
292 if (count < 2) continue;
293 TRACE( "Using %u (%s) as core pointer\n",
294 devices[i].deviceid, debugstr_a(devices[i].name) );
295 data->xi2_core_pointer = devices[i].deviceid;
299 mask.mask = mask_bits;
300 mask.mask_len = sizeof(mask_bits);
301 memset( mask_bits, 0, sizeof(mask_bits) );
302 XISetMask( mask_bits, XI_RawMotion );
303 XISetMask( mask_bits, XI_ButtonPress );
305 for (i = 0; i < data->xi2_device_count; ++i)
307 if (devices[i].use == XISlavePointer && devices[i].attachment == data->xi2_core_pointer)
309 TRACE( "Device %u (%s) is attached to the core pointer\n",
310 devices[i].deviceid, debugstr_a(devices[i].name) );
311 mask.deviceid = devices[i].deviceid;
312 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
313 data->xi2_state = xi_enabled;
319 /***********************************************************************
322 static void disable_xinput2(void)
324 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
325 struct x11drv_thread_data *data = x11drv_thread_data();
326 XIDeviceInfo *devices = data->xi2_devices;
330 if (data->xi2_state != xi_enabled) return;
332 TRACE( "disabling\n" );
333 data->xi2_state = xi_disabled;
338 for (i = 0; i < data->xi2_device_count; ++i)
340 if (devices[i].use == XISlavePointer && devices[i].attachment == data->xi2_core_pointer)
342 mask.deviceid = devices[i].deviceid;
343 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
346 pXIFreeDeviceInfo( devices );
347 data->xi2_devices = NULL;
348 data->xi2_device_count = 0;
353 /***********************************************************************
354 * grab_clipping_window
356 * Start a pointer grab on the clip window.
358 static BOOL grab_clipping_window( const RECT *clip )
360 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
361 struct x11drv_thread_data *data = x11drv_thread_data();
365 if (!data) return FALSE;
366 if (!(clip_window = init_clip_window())) return TRUE;
368 if (!(msg_hwnd = CreateWindowW( messageW, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0,
369 GetModuleHandleW(0), NULL )))
372 /* enable XInput2 unless we are already clipping */
373 if (!data->clip_hwnd) enable_xinput2();
375 if (data->xi2_state != xi_enabled)
377 WARN( "XInput2 not supported, refusing to clip to %s\n", wine_dbgstr_rect(clip) );
378 DestroyWindow( msg_hwnd );
383 TRACE( "clipping to %s win %lx\n", wine_dbgstr_rect(clip), clip_window );
385 if (!data->clip_hwnd) XUnmapWindow( data->display, clip_window );
386 XMoveResizeWindow( data->display, clip_window,
387 clip->left - virtual_screen_rect.left, clip->top - virtual_screen_rect.top,
388 max( 1, clip->right - clip->left ), max( 1, clip->bottom - clip->top ) );
389 XMapWindow( data->display, clip_window );
391 /* if the rectangle is shrinking we may get a pointer warp */
392 if (!data->clip_hwnd || clip->left > clip_rect.left || clip->top > clip_rect.top ||
393 clip->right < clip_rect.right || clip->bottom < clip_rect.bottom)
394 data->warp_serial = NextRequest( data->display );
396 if (!XGrabPointer( data->display, clip_window, False,
397 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
398 GrabModeAsync, GrabModeAsync, clip_window, None, CurrentTime ))
401 if (!clipping_cursor)
404 DestroyWindow( msg_hwnd );
408 if (!data->clip_hwnd) sync_window_cursor( clip_window );
409 InterlockedExchangePointer( (void **)&cursor_window, msg_hwnd );
410 data->clip_hwnd = msg_hwnd;
411 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, (LPARAM)msg_hwnd );
415 /***********************************************************************
416 * ungrab_clipping_window
418 * Release the pointer grab on the clip window.
420 void ungrab_clipping_window(void)
422 Display *display = thread_init_display();
423 Window clip_window = init_clip_window();
425 if (!clip_window) return;
427 TRACE( "no longer clipping\n" );
428 XUnmapWindow( display, clip_window );
430 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, 0 );
433 /***********************************************************************
434 * reset_clipping_window
436 * Forcibly reset the window clipping on external events.
438 void reset_clipping_window(void)
440 ungrab_clipping_window();
441 ClipCursor( NULL ); /* make sure the clip rectangle is reset too */
444 /***********************************************************************
447 * Notification function called upon receiving a WM_X11DRV_CLIP_CURSOR.
449 LRESULT clip_cursor_notify( HWND hwnd, HWND new_clip_hwnd )
451 struct x11drv_thread_data *data = x11drv_thread_data();
453 if (hwnd == GetDesktopWindow()) /* change the clip window stored in the desktop process */
455 static HWND clip_hwnd;
457 HWND prev = clip_hwnd;
458 clip_hwnd = new_clip_hwnd;
459 if (prev || new_clip_hwnd) TRACE( "clip hwnd changed from %p to %p\n", prev, new_clip_hwnd );
460 if (prev) SendNotifyMessageW( prev, WM_X11DRV_CLIP_CURSOR, 0, 0 );
462 else if (hwnd == data->clip_hwnd) /* this is a notification that clipping has been reset */
464 TRACE( "clip hwnd reset from %p\n", hwnd );
466 data->clip_reset = GetTickCount();
468 DestroyWindow( hwnd );
470 else if (hwnd == GetForegroundWindow()) /* request to clip */
474 GetClipCursor( &clip );
475 if (clip.left > virtual_screen_rect.left || clip.right < virtual_screen_rect.right ||
476 clip.top > virtual_screen_rect.top || clip.bottom < virtual_screen_rect.bottom)
477 return grab_clipping_window( &clip );
482 /***********************************************************************
483 * clip_fullscreen_window
485 * Turn on clipping if the active window is fullscreen.
487 BOOL clip_fullscreen_window( HWND hwnd, BOOL reset )
489 struct x11drv_win_data *data;
490 struct x11drv_thread_data *thread_data;
495 if (hwnd == GetDesktopWindow()) return FALSE;
496 style = GetWindowLongW( hwnd, GWL_STYLE );
497 if (!(style & WS_VISIBLE)) return FALSE;
498 if ((style & (WS_POPUP | WS_CHILD)) == WS_CHILD) return FALSE;
499 /* maximized windows don't count as full screen */
500 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION) return FALSE;
501 if (!(data = get_win_data( hwnd ))) return FALSE;
502 fullscreen = is_window_rect_fullscreen( &data->whole_rect );
503 release_win_data( data );
504 if (!fullscreen) return FALSE;
505 if (!(thread_data = x11drv_thread_data())) return FALSE;
506 if (GetTickCount() - thread_data->clip_reset < 1000) return FALSE;
507 if (!reset && clipping_cursor && thread_data->clip_hwnd) return FALSE; /* already clipping */
508 SetRect( &rect, 0, 0, screen_width, screen_height );
509 if (!grab_fullscreen)
511 if (!EqualRect( &rect, &virtual_screen_rect )) return FALSE;
512 if (root_window != DefaultRootWindow( gdi_display )) return FALSE;
514 TRACE( "win %p clipping fullscreen\n", hwnd );
515 return grab_clipping_window( &rect );
518 /***********************************************************************
521 * Update the various window states on a mouse event.
523 static void send_mouse_input( HWND hwnd, Window window, unsigned int state, INPUT *input )
525 struct x11drv_win_data *data;
528 input->type = INPUT_MOUSE;
532 struct x11drv_thread_data *thread_data = x11drv_thread_data();
533 HWND clip_hwnd = thread_data->clip_hwnd;
535 if (!clip_hwnd) return;
536 if (thread_data->clip_window != window) return;
537 if (InterlockedExchangePointer( (void **)&cursor_window, clip_hwnd ) != clip_hwnd ||
538 input->u.mi.time - last_cursor_change > 100)
540 sync_window_cursor( window );
541 last_cursor_change = input->u.mi.time;
543 input->u.mi.dx += clip_rect.left;
544 input->u.mi.dy += clip_rect.top;
545 __wine_send_input( hwnd, input );
549 if (!(data = get_win_data( hwnd ))) return;
551 if (window == data->whole_window)
553 input->u.mi.dx += data->whole_rect.left - data->client_rect.left;
554 input->u.mi.dy += data->whole_rect.top - data->client_rect.top;
556 if (window == root_window)
558 input->u.mi.dx += virtual_screen_rect.left;
559 input->u.mi.dy += virtual_screen_rect.top;
561 pt.x = input->u.mi.dx;
562 pt.y = input->u.mi.dy;
563 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
564 pt.x = data->client_rect.right - data->client_rect.left - 1 - pt.x;
565 MapWindowPoints( hwnd, 0, &pt, 1 );
567 if (InterlockedExchangePointer( (void **)&cursor_window, hwnd ) != hwnd ||
568 input->u.mi.time - last_cursor_change > 100)
570 sync_window_cursor( data->whole_window );
571 last_cursor_change = input->u.mi.time;
573 release_win_data( data );
575 if (hwnd != GetDesktopWindow())
577 hwnd = GetAncestor( hwnd, GA_ROOT );
578 if ((input->u.mi.dwFlags & (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_RIGHTDOWN)) && hwnd == GetForegroundWindow())
579 clip_fullscreen_window( hwnd, FALSE );
582 /* update the wine server Z-order */
584 if (window != x11drv_thread_data()->grab_window &&
585 /* ignore event if a button is pressed, since the mouse is then grabbed too */
586 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
589 SetRect( &rect, pt.x, pt.y, pt.x + 1, pt.y + 1 );
590 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
592 SERVER_START_REQ( update_window_zorder )
594 req->window = wine_server_user_handle( hwnd );
595 req->rect.left = rect.left;
596 req->rect.top = rect.top;
597 req->rect.right = rect.right;
598 req->rect.bottom = rect.bottom;
599 wine_server_call( req );
604 input->u.mi.dx = pt.x;
605 input->u.mi.dy = pt.y;
606 __wine_send_input( hwnd, input );
609 #ifdef SONAME_LIBXCURSOR
611 /***********************************************************************
612 * create_xcursor_frame
614 * Use Xcursor to create a frame of an X cursor from a Windows one.
616 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
617 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
618 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
619 int width, int height, int istep )
621 XcursorImage *image, *ret = NULL;
622 DWORD delay_jiffies, num_steps;
623 int x, y, i, has_alpha = FALSE;
626 image = pXcursorImageCreate( width, height );
629 ERR("X11 failed to produce a cursor frame!\n");
633 image->xhot = iinfo->xHotspot;
634 image->yhot = iinfo->yHotspot;
636 image->delay = 100; /* fallback delay, 100 ms */
637 if (GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, istep, &delay_jiffies, &num_steps) != 0)
638 image->delay = (100 * delay_jiffies) / 6; /* convert jiffies (1/60s) to milliseconds */
640 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep);
642 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
643 memset( color_bits, 0x00, color_size );
644 SelectObject( hdc, hbmColor );
645 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
647 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
650 memcpy( image->pixels, color_bits, color_size );
652 /* check if the cursor frame was drawn with an alpha channel */
653 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
654 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
656 /* if no alpha channel was drawn then generate it from the mask */
659 unsigned int width_bytes = (width + 31) / 32 * 4;
661 /* draw the cursor mask to a temporary buffer */
662 memset( mask_bits, 0xFF, mask_size );
663 SelectObject( hdc, hbmMask );
664 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
666 ERR("Failed to draw frame mask %d.\n", istep);
669 /* use the buffer to directly modify the XcursorImage alpha channel */
670 for (y = 0, ptr = image->pixels; y < height; y++)
671 for (x = 0; x < width; x++, ptr++)
672 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
678 if (ret == NULL) pXcursorImageDestroy( image );
682 /***********************************************************************
683 * create_xcursor_cursor
685 * Use Xcursor to create an X cursor from a Windows one.
687 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
689 unsigned char *color_bits, *mask_bits;
690 HBITMAP hbmColor = 0, hbmMask = 0;
691 DWORD nFrames, delay_jiffies, i;
692 int color_size, mask_size;
693 BITMAPINFO *info = NULL;
694 XcursorImages *images;
698 /* Retrieve the number of frames to render */
699 if (!GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, 0, &delay_jiffies, &nFrames)) return 0;
700 if (!(imgs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XcursorImage*)*nFrames ))) return 0;
702 /* Allocate all of the resources necessary to obtain a cursor frame */
703 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
704 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
705 info->bmiHeader.biWidth = width;
706 info->bmiHeader.biHeight = -height;
707 info->bmiHeader.biPlanes = 1;
708 info->bmiHeader.biCompression = BI_RGB;
709 info->bmiHeader.biXPelsPerMeter = 0;
710 info->bmiHeader.biYPelsPerMeter = 0;
711 info->bmiHeader.biClrUsed = 0;
712 info->bmiHeader.biClrImportant = 0;
713 info->bmiHeader.biBitCount = 32;
714 color_size = width * height * 4;
715 info->bmiHeader.biSizeImage = color_size;
716 hbmColor = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &color_bits, NULL, 0);
719 ERR("Failed to create DIB section for cursor color data!\n");
722 info->bmiHeader.biBitCount = 1;
723 info->bmiColors[0].rgbRed = 0;
724 info->bmiColors[0].rgbGreen = 0;
725 info->bmiColors[0].rgbBlue = 0;
726 info->bmiColors[0].rgbReserved = 0;
727 info->bmiColors[1].rgbRed = 0xff;
728 info->bmiColors[1].rgbGreen = 0xff;
729 info->bmiColors[1].rgbBlue = 0xff;
730 info->bmiColors[1].rgbReserved = 0;
732 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
733 info->bmiHeader.biSizeImage = mask_size;
734 hbmMask = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &mask_bits, NULL, 0);
737 ERR("Failed to create DIB section for cursor mask data!\n");
741 /* Create an XcursorImage for each frame of the cursor */
742 for (i=0; i<nFrames; i++)
744 imgs[i] = create_xcursor_frame( hdc, iinfo, icon,
745 hbmColor, color_bits, color_size,
746 hbmMask, mask_bits, mask_size,
748 if (!imgs[i]) goto cleanup;
751 /* Build an X cursor out of all of the frames */
752 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
753 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
754 images->images[images->nimage] = imgs[images->nimage];
755 cursor = pXcursorImagesLoadCursor( gdi_display, images );
756 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
757 HeapFree( GetProcessHeap(), 0, imgs );
763 /* Failed to produce a cursor, free previously allocated frames */
764 for (i=0; i<nFrames && imgs[i]; i++)
765 pXcursorImageDestroy( imgs[i] );
766 HeapFree( GetProcessHeap(), 0, imgs );
768 /* Cleanup all of the resources used to obtain the frame data */
769 if (hbmColor) DeleteObject( hbmColor );
770 if (hbmMask) DeleteObject( hbmMask );
771 HeapFree( GetProcessHeap(), 0, info );
776 struct system_cursors
782 static const struct system_cursors user32_cursors[] =
784 { OCR_NORMAL, "left_ptr" },
785 { OCR_IBEAM, "xterm" },
786 { OCR_WAIT, "watch" },
787 { OCR_CROSS, "cross" },
788 { OCR_UP, "center_ptr" },
789 { OCR_SIZE, "fleur" },
790 { OCR_SIZEALL, "fleur" },
791 { OCR_ICON, "icon" },
792 { OCR_SIZENWSE, "nwse-resize" },
793 { OCR_SIZENESW, "nesw-resize" },
794 { OCR_SIZEWE, "ew-resize" },
795 { OCR_SIZENS, "ns-resize" },
796 { OCR_NO, "not-allowed" },
797 { OCR_HAND, "hand2" },
798 { OCR_APPSTARTING, "left_ptr_watch" },
799 { OCR_HELP, "question_arrow" },
803 static const struct system_cursors comctl32_cursors[] =
808 { 106, "row-resize" },
809 { 107, "row-resize" },
811 { 135, "col-resize" },
815 static const struct system_cursors ole32_cursors[] =
824 static const struct system_cursors riched20_cursors[] =
827 { 107, "right_ptr" },
836 const struct system_cursors *cursors;
840 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
841 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
842 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
843 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
846 /***********************************************************************
847 * create_xcursor_system_cursor
849 * Create an X cursor for a system cursor.
851 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
853 static const WCHAR idW[] = {'%','h','u',0};
854 const struct system_cursors *cursors;
859 WCHAR *p, name[MAX_PATH * 2], valueW[64];
863 if (!pXcursorLibraryLoadCursor) return 0;
864 if (!info->szModName[0]) return 0;
866 p = strrchrW( info->szModName, '\\' );
867 strcpyW( name, p ? p + 1 : info->szModName );
868 p = name + strlenW( name );
870 if (info->szResName[0]) strcpyW( p, info->szResName );
871 else sprintfW( p, idW, info->wResID );
874 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
875 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver\\Cursors", &key ))
877 size = sizeof(valueW) / sizeof(WCHAR);
878 ret = RegQueryValueExW( key, name, NULL, NULL, (BYTE *)valueW, &size );
882 if (!valueW[0]) return 0; /* force standard cursor */
883 if (!WideCharToMultiByte( CP_UNIXCP, 0, valueW, -1, valueA, sizeof(valueA), NULL, NULL ))
889 if (info->szResName[0]) goto done; /* only integer resources are supported here */
890 if (!(module = GetModuleHandleW( info->szModName ))) goto done;
892 for (i = 0; i < sizeof(module_cursors)/sizeof(module_cursors[0]); i++)
893 if (GetModuleHandleW( module_cursors[i].name ) == module) break;
894 if (i == sizeof(module_cursors)/sizeof(module_cursors[0])) goto done;
896 cursors = module_cursors[i].cursors;
897 for (i = 0; cursors[i].id; i++)
898 if (cursors[i].id == info->wResID)
900 strcpy( valueA, cursors[i].name );
907 cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
908 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
909 debugstr_w(name), debugstr_a(valueA) );
911 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
915 #endif /* SONAME_LIBXCURSOR */
918 /***********************************************************************
919 * create_xlib_monochrome_cursor
921 * Create a monochrome X cursor from a Windows one.
923 static Cursor create_xlib_monochrome_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
925 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
926 BITMAPINFO *info = (BITMAPINFO *)buffer;
928 const int xor_y = height;
929 unsigned int width_bytes = (width + 31) / 32 * 4;
930 unsigned char *mask_bits = NULL;
933 XVisualInfo vis = default_visual;
934 Pixmap src_pixmap, bits_pixmap, mask_pixmap;
935 struct gdi_image_bits bits;
938 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
939 info->bmiHeader.biWidth = width;
940 info->bmiHeader.biHeight = -height * 2;
941 info->bmiHeader.biPlanes = 1;
942 info->bmiHeader.biBitCount = 1;
943 info->bmiHeader.biCompression = BI_RGB;
944 info->bmiHeader.biSizeImage = width_bytes * height * 2;
945 info->bmiHeader.biXPelsPerMeter = 0;
946 info->bmiHeader.biYPelsPerMeter = 0;
947 info->bmiHeader.biClrUsed = 0;
948 info->bmiHeader.biClrImportant = 0;
950 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
951 if (!GetDIBits( hdc, icon->hbmMask, 0, height * 2, mask_bits, info, DIB_RGB_COLORS )) goto done;
954 bits.ptr = mask_bits;
957 if (!(src_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
959 bits_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
960 mask_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
961 gc = XCreateGC( gdi_display, src_pixmap, 0, NULL );
962 XSetGraphicsExposures( gdi_display, gc, False );
964 /* We have to do some magic here, as cursors are not fully
965 * compatible between Windows and X11. Under X11, there are
966 * only 3 possible color cursor: black, white and masked. So
967 * we map the 4th Windows color (invert the bits on the screen)
968 * to black and an additional white bit on another place
969 * (+1,+1). This require some boolean arithmetic:
972 * And Xor Result | Bits Mask Result
973 * 0 0 black | 0 1 background
974 * 0 1 white | 1 1 foreground
975 * 1 0 no change | X 0 no change
976 * 1 1 inverted | 0 1 background
979 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
980 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
982 XSetFunction( gdi_display, gc, GXcopy );
983 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 0, 0 );
984 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 0, 0 );
985 XSetFunction( gdi_display, gc, GXandReverse );
986 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, xor_y, width, height, 0, 0 );
987 XSetFunction( gdi_display, gc, GXorReverse );
988 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, xor_y, width, height, 0, 0 );
989 /* additional white */
990 XSetFunction( gdi_display, gc, GXand );
991 XCopyArea( gdi_display, src_pixmap, src_pixmap, gc, 0, xor_y, width, height, 0, and_y );
992 XSetFunction( gdi_display, gc, GXor );
993 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 1, 1 );
994 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 1, 1 );
995 XFreeGC( gdi_display, gc );
997 fg.red = fg.green = fg.blue = 0xffff;
998 bg.red = bg.green = bg.blue = 0;
999 cursor = XCreatePixmapCursor( gdi_display, bits_pixmap, mask_pixmap,
1000 &fg, &bg, icon->xHotspot, icon->yHotspot );
1001 XFreePixmap( gdi_display, src_pixmap );
1002 XFreePixmap( gdi_display, bits_pixmap );
1003 XFreePixmap( gdi_display, mask_pixmap );
1006 HeapFree( GetProcessHeap(), 0, mask_bits );
1010 /***********************************************************************
1011 * create_xlib_color_cursor
1013 * Create a color X cursor from a Windows one.
1015 static Cursor create_xlib_color_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1017 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1018 BITMAPINFO *info = (BITMAPINFO *)buffer;
1020 Cursor cursor = None;
1021 XVisualInfo vis = default_visual;
1022 Pixmap xor_pixmap, mask_pixmap;
1023 struct gdi_image_bits bits;
1024 unsigned int *color_bits = NULL, *ptr;
1025 unsigned char *mask_bits = NULL, *xor_bits = NULL;
1026 int i, x, y, has_alpha = 0;
1027 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
1028 unsigned int width_bytes = (width + 31) / 32 * 4;
1030 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1031 info->bmiHeader.biWidth = width;
1032 info->bmiHeader.biHeight = -height;
1033 info->bmiHeader.biPlanes = 1;
1034 info->bmiHeader.biBitCount = 1;
1035 info->bmiHeader.biCompression = BI_RGB;
1036 info->bmiHeader.biSizeImage = width_bytes * height;
1037 info->bmiHeader.biXPelsPerMeter = 0;
1038 info->bmiHeader.biYPelsPerMeter = 0;
1039 info->bmiHeader.biClrUsed = 0;
1040 info->bmiHeader.biClrImportant = 0;
1042 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1043 if (!GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS )) goto done;
1045 info->bmiHeader.biBitCount = 32;
1046 info->bmiHeader.biSizeImage = width * height * 4;
1047 if (!(color_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1048 if (!(xor_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, width_bytes * height ))) goto done;
1049 GetDIBits( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS );
1051 /* compute fg/bg color and xor bitmap based on average of the color values */
1053 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
1054 for (y = 0, ptr = color_bits; y < height; y++)
1056 for (x = 0; x < width; x++, ptr++)
1058 int red = (*ptr >> 16) & 0xff;
1059 int green = (*ptr >> 8) & 0xff;
1060 int blue = (*ptr >> 0) & 0xff;
1061 if (red + green + blue > 0x40)
1067 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1079 fg.red = rfg * 257 / fgBits;
1080 fg.green = gfg * 257 / fgBits;
1081 fg.blue = bfg * 257 / fgBits;
1083 else fg.red = fg.green = fg.blue = 0;
1084 bgBits = width * height - fgBits;
1087 bg.red = rbg * 257 / bgBits;
1088 bg.green = gbg * 257 / bgBits;
1089 bg.blue = bbg * 257 / bgBits;
1091 else bg.red = bg.green = bg.blue = 0;
1093 info->bmiHeader.biBitCount = 1;
1094 info->bmiHeader.biClrUsed = 0;
1095 info->bmiHeader.biSizeImage = width_bytes * height;
1097 /* generate mask from the alpha channel if we have one */
1099 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
1100 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
1104 memset( mask_bits, 0, width_bytes * height );
1105 for (y = 0, ptr = color_bits; y < height; y++)
1106 for (x = 0; x < width; x++, ptr++)
1107 if ((*ptr >> 24) > 25) /* more than 10% alpha */
1108 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1110 else /* invert the mask */
1112 ptr = (unsigned int *)mask_bits;
1113 for (i = 0; i < info->bmiHeader.biSizeImage / sizeof(*ptr); i++, ptr++) *ptr ^= ~0u;
1117 bits.ptr = xor_bits;
1119 bits.is_copy = TRUE;
1120 if (!(xor_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1122 bits.ptr = mask_bits;
1123 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
1127 cursor = XCreatePixmapCursor( gdi_display, xor_pixmap, mask_pixmap,
1128 &fg, &bg, icon->xHotspot, icon->yHotspot );
1129 XFreePixmap( gdi_display, mask_pixmap );
1131 XFreePixmap( gdi_display, xor_pixmap );
1134 HeapFree( GetProcessHeap(), 0, color_bits );
1135 HeapFree( GetProcessHeap(), 0, xor_bits );
1136 HeapFree( GetProcessHeap(), 0, mask_bits );
1140 /***********************************************************************
1143 * Create an X cursor from a Windows one.
1145 static Cursor create_cursor( HANDLE handle )
1152 if (!handle) return get_empty_cursor();
1154 info.cbSize = sizeof(info);
1155 if (!GetIconInfoExW( handle, &info )) return 0;
1157 #ifdef SONAME_LIBXCURSOR
1158 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
1160 DeleteObject( info.hbmColor );
1161 DeleteObject( info.hbmMask );
1166 GetObjectW( info.hbmMask, sizeof(bm), &bm );
1167 if (!info.hbmColor) bm.bmHeight = max( 1, bm.bmHeight / 2 );
1169 /* make sure hotspot is valid */
1170 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1172 info.xHotspot = bm.bmWidth / 2;
1173 info.yHotspot = bm.bmHeight / 2;
1176 hdc = CreateCompatibleDC( 0 );
1180 #ifdef SONAME_LIBXCURSOR
1181 if (pXcursorImagesLoadCursor)
1182 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
1184 if (!cursor) cursor = create_xlib_color_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1185 DeleteObject( info.hbmColor );
1189 cursor = create_xlib_monochrome_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1192 DeleteObject( info.hbmMask );
1197 /***********************************************************************
1198 * DestroyCursorIcon (X11DRV.@)
1200 void CDECL X11DRV_DestroyCursorIcon( HCURSOR handle )
1204 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1206 TRACE( "%p xid %lx\n", handle, cursor );
1207 XFreeCursor( gdi_display, cursor );
1208 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1212 /***********************************************************************
1213 * SetCursor (X11DRV.@)
1215 void CDECL X11DRV_SetCursor( HCURSOR handle )
1217 if (InterlockedExchangePointer( (void **)&last_cursor, handle ) != handle ||
1218 GetTickCount() - last_cursor_change > 100)
1220 last_cursor_change = GetTickCount();
1221 if (cursor_window) SendNotifyMessageW( cursor_window, WM_X11DRV_SET_CURSOR, 0, (LPARAM)handle );
1225 /***********************************************************************
1226 * SetCursorPos (X11DRV.@)
1228 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
1230 struct x11drv_thread_data *data = x11drv_init_thread_data();
1232 XWarpPointer( data->display, root_window, root_window, 0, 0, 0, 0,
1233 x - virtual_screen_rect.left, y - virtual_screen_rect.top );
1234 data->warp_serial = NextRequest( data->display );
1235 XNoOp( data->display );
1236 XFlush( data->display ); /* avoids bad mouse lag in games that do their own mouse warping */
1237 TRACE( "warped to %d,%d serial %lu\n", x, y, data->warp_serial );
1241 /***********************************************************************
1242 * GetCursorPos (X11DRV.@)
1244 BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
1246 Display *display = thread_init_display();
1248 int rootX, rootY, winX, winY;
1249 unsigned int xstate;
1252 ret = XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &winX, &winY, &xstate );
1256 pos->x = winX + virtual_screen_rect.left;
1257 pos->y = winY + virtual_screen_rect.top;
1258 TRACE( "pointer at (%d,%d) server pos %d,%d\n", pos->x, pos->y, old.x, old.y );
1263 /***********************************************************************
1264 * ClipCursor (X11DRV.@)
1266 BOOL CDECL X11DRV_ClipCursor( LPCRECT clip )
1268 if (!clip) clip = &virtual_screen_rect;
1270 if (GetWindowThreadProcessId( GetDesktopWindow(), NULL ) == GetCurrentThreadId())
1271 return TRUE; /* don't clip in the desktop process */
1275 HWND foreground = GetForegroundWindow();
1277 /* we are clipping if the clip rectangle is smaller than the screen */
1278 if (clip->left > virtual_screen_rect.left || clip->right < virtual_screen_rect.right ||
1279 clip->top > virtual_screen_rect.top || clip->bottom < virtual_screen_rect.bottom)
1283 /* forward request to the foreground window if it's in a different thread */
1284 tid = GetWindowThreadProcessId( foreground, &pid );
1285 if (tid && tid != GetCurrentThreadId() && pid == GetCurrentProcessId())
1287 TRACE( "forwarding clip request to %p\n", foreground );
1288 SendNotifyMessageW( foreground, WM_X11DRV_CLIP_CURSOR, 0, 0 );
1291 else if (grab_clipping_window( clip )) return TRUE;
1293 else /* if currently clipping, check if we should switch to fullscreen clipping */
1295 struct x11drv_thread_data *data = x11drv_thread_data();
1296 if (data && data->clip_hwnd)
1298 if (EqualRect( clip, &clip_rect ) || clip_fullscreen_window( foreground, TRUE ))
1303 ungrab_clipping_window();
1307 /***********************************************************************
1308 * move_resize_window
1310 void move_resize_window( HWND hwnd, int dir )
1312 Display *display = thread_display();
1314 int x, y, rootX, rootY, button = 0;
1316 Window win, root, child;
1317 unsigned int xstate;
1319 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1321 pt = GetMessagePos();
1322 x = (short)LOWORD( pt );
1323 y = (short)HIWORD( pt );
1325 if (GetKeyState( VK_LBUTTON ) & 0x8000) button = 1;
1326 else if (GetKeyState( VK_MBUTTON ) & 0x8000) button = 2;
1327 else if (GetKeyState( VK_RBUTTON ) & 0x8000) button = 3;
1329 TRACE( "hwnd %p/%lx, x %d, y %d, dir %d, button %d\n", hwnd, win, x, y, dir, button );
1331 xev.xclient.type = ClientMessage;
1332 xev.xclient.window = win;
1333 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1334 xev.xclient.serial = 0;
1335 xev.xclient.display = display;
1336 xev.xclient.send_event = True;
1337 xev.xclient.format = 32;
1338 xev.xclient.data.l[0] = x - virtual_screen_rect.left; /* x coord */
1339 xev.xclient.data.l[1] = y - virtual_screen_rect.top; /* y coord */
1340 xev.xclient.data.l[2] = dir; /* direction */
1341 xev.xclient.data.l[3] = button; /* button */
1342 xev.xclient.data.l[4] = 0; /* unused */
1344 /* need to ungrab the pointer that may have been automatically grabbed
1345 * with a ButtonPress event */
1346 XUngrabPointer( display, CurrentTime );
1347 XSendEvent(display, root_window, False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
1349 /* try to detect the end of the size/move by polling for the mouse button to be released */
1350 /* (some apps don't like it if we return before the size/move is done) */
1352 if (!button) return;
1358 if (!XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &x, &y, &xstate )) break;
1360 if (!(xstate & (Button1Mask << (button - 1))))
1362 /* fake a button release event */
1363 input.type = INPUT_MOUSE;
1364 input.u.mi.dx = x + virtual_screen_rect.left;
1365 input.u.mi.dy = y + virtual_screen_rect.top;
1366 input.u.mi.mouseData = button_up_data[button - 1];
1367 input.u.mi.dwFlags = button_up_flags[button - 1] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1368 input.u.mi.time = GetTickCount();
1369 input.u.mi.dwExtraInfo = 0;
1370 __wine_send_input( hwnd, &input );
1373 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
1375 if (!CallMsgFilterW( &msg, MSGF_SIZE ))
1377 TranslateMessage( &msg );
1378 DispatchMessageW( &msg );
1382 if (!(xstate & (Button1Mask << (button - 1)))) break;
1383 MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT );
1386 TRACE( "hwnd %p/%lx done\n", hwnd, win );
1390 /***********************************************************************
1391 * X11DRV_ButtonPress
1393 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1395 XButtonEvent *event = &xev->xbutton;
1396 int buttonNum = event->button - 1;
1399 if (buttonNum >= NB_BUTTONS) return;
1401 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1403 input.u.mi.dx = event->x;
1404 input.u.mi.dy = event->y;
1405 input.u.mi.mouseData = button_down_data[buttonNum];
1406 input.u.mi.dwFlags = button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1407 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1408 input.u.mi.dwExtraInfo = 0;
1410 update_user_time( event->time );
1411 send_mouse_input( hwnd, event->window, event->state, &input );
1415 /***********************************************************************
1416 * X11DRV_ButtonRelease
1418 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1420 XButtonEvent *event = &xev->xbutton;
1421 int buttonNum = event->button - 1;
1424 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1426 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1428 input.u.mi.dx = event->x;
1429 input.u.mi.dy = event->y;
1430 input.u.mi.mouseData = button_up_data[buttonNum];
1431 input.u.mi.dwFlags = button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1432 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1433 input.u.mi.dwExtraInfo = 0;
1435 send_mouse_input( hwnd, event->window, event->state, &input );
1439 /***********************************************************************
1440 * X11DRV_MotionNotify
1442 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1444 XMotionEvent *event = &xev->xmotion;
1447 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d serial %lu\n",
1448 hwnd, event->window, event->x, event->y, event->is_hint, event->serial );
1450 input.u.mi.dx = event->x;
1451 input.u.mi.dy = event->y;
1452 input.u.mi.mouseData = 0;
1453 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1454 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1455 input.u.mi.dwExtraInfo = 0;
1459 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1460 if (thread_data->warp_serial && (long)(event->serial - thread_data->warp_serial) < 0) return;
1463 send_mouse_input( hwnd, event->window, event->state, &input );
1467 /***********************************************************************
1468 * X11DRV_EnterNotify
1470 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1472 XCrossingEvent *event = &xev->xcrossing;
1475 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd, event->window, event->x, event->y, event->detail );
1477 if (event->detail == NotifyVirtual) return;
1478 if (event->window == x11drv_thread_data()->grab_window) return;
1480 /* simulate a mouse motion event */
1481 input.u.mi.dx = event->x;
1482 input.u.mi.dy = event->y;
1483 input.u.mi.mouseData = 0;
1484 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1485 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1486 input.u.mi.dwExtraInfo = 0;
1488 send_mouse_input( hwnd, event->window, event->state, &input );
1491 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1493 /***********************************************************************
1496 static void X11DRV_RawMotion( XGenericEventCookie *xev )
1498 XIRawEvent *event = xev->data;
1499 const double *values = event->valuators.values;
1502 double dx = 0, dy = 0;
1503 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1504 XIDeviceInfo *devices = thread_data->xi2_devices;
1506 if (!event->valuators.mask_len) return;
1507 if (thread_data->xi2_state != xi_enabled) return;
1509 input.u.mi.mouseData = 0;
1510 input.u.mi.dwFlags = MOUSEEVENTF_MOVE;
1511 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1512 input.u.mi.dwExtraInfo = 0;
1516 for (i = 0; i < thread_data->xi2_device_count; ++i)
1518 if (devices[i].deviceid != event->deviceid) continue;
1519 for (j = 0; j < devices[i].num_classes; j++)
1521 XIValuatorClassInfo *class = (XIValuatorClassInfo *)devices[i].classes[j];
1523 if (devices[i].classes[j]->type != XIValuatorClass) continue;
1524 if (XIMaskIsSet( event->valuators.mask, class->number ))
1526 double val = *values++;
1527 if (class->label == x11drv_atom( Rel_X ) ||
1528 (!class->label && class->number == 0 && class->mode == XIModeRelative))
1530 input.u.mi.dx = dx = val;
1531 if (class->min < class->max)
1532 input.u.mi.dx = val * (virtual_screen_rect.right - virtual_screen_rect.left)
1533 / (class->max - class->min);
1535 else if (class->label == x11drv_atom( Rel_Y ) ||
1536 (!class->label && class->number == 1 && class->mode == XIModeRelative))
1538 input.u.mi.dy = dy = val;
1539 if (class->min < class->max)
1540 input.u.mi.dy = val * (virtual_screen_rect.bottom - virtual_screen_rect.top)
1541 / (class->max - class->min);
1548 if (thread_data->warp_serial)
1550 if ((long)(xev->serial - thread_data->warp_serial) < 0)
1552 TRACE( "pos %d,%d old serial %lu, ignoring\n", input.u.mi.dx, input.u.mi.dy, xev->serial );
1555 thread_data->warp_serial = 0; /* we caught up now */
1558 TRACE( "pos %d,%d (event %f,%f)\n", input.u.mi.dx, input.u.mi.dy, dx, dy );
1560 input.type = INPUT_MOUSE;
1561 __wine_send_input( 0, &input );
1564 #endif /* HAVE_X11_EXTENSIONS_XINPUT2_H */
1567 /***********************************************************************
1568 * X11DRV_XInput2_Init
1570 void X11DRV_XInput2_Init(void)
1572 #if defined(SONAME_LIBXI) && defined(HAVE_X11_EXTENSIONS_XINPUT2_H)
1574 void *libxi_handle = wine_dlopen( SONAME_LIBXI, RTLD_NOW, NULL, 0 );
1578 WARN( "couldn't load %s\n", SONAME_LIBXI );
1581 #define LOAD_FUNCPTR(f) \
1582 if (!(p##f = wine_dlsym( libxi_handle, #f, NULL, 0))) \
1584 WARN("Failed to load %s.\n", #f); \
1588 LOAD_FUNCPTR(XIFreeDeviceInfo);
1589 LOAD_FUNCPTR(XIQueryDevice);
1590 LOAD_FUNCPTR(XIQueryVersion);
1591 LOAD_FUNCPTR(XISelectEvents);
1594 xinput2_available = XQueryExtension( gdi_display, "XInputExtension", &xinput2_opcode, &event, &error );
1596 TRACE( "X Input 2 support not compiled in.\n" );
1601 /***********************************************************************
1602 * X11DRV_GenericEvent
1604 void X11DRV_GenericEvent( HWND hwnd, XEvent *xev )
1606 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1607 XGenericEventCookie *event = &xev->xcookie;
1609 if (!event->data) return;
1610 if (event->extension != xinput2_opcode) return;
1612 switch (event->evtype)
1615 X11DRV_RawMotion( event );
1619 TRACE( "Unhandled event %#x\n", event->evtype );