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++;
290 if (count < 2) continue;
291 TRACE( "Using %u (%s) as core pointer\n",
292 devices[i].deviceid, debugstr_a(devices[i].name) );
293 data->xi2_core_pointer = devices[i].deviceid;
297 mask.mask = mask_bits;
298 mask.mask_len = sizeof(mask_bits);
299 memset( mask_bits, 0, sizeof(mask_bits) );
300 XISetMask( mask_bits, XI_RawMotion );
301 XISetMask( mask_bits, XI_ButtonPress );
303 for (i = 0; i < data->xi2_device_count; ++i)
305 if (devices[i].use == XISlavePointer && devices[i].attachment == data->xi2_core_pointer)
307 TRACE( "Device %u (%s) is attached to the core pointer\n",
308 devices[i].deviceid, debugstr_a(devices[i].name) );
309 mask.deviceid = devices[i].deviceid;
310 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
311 data->xi2_state = xi_enabled;
317 /***********************************************************************
320 static void disable_xinput2(void)
322 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
323 struct x11drv_thread_data *data = x11drv_thread_data();
324 XIDeviceInfo *devices = data->xi2_devices;
328 if (data->xi2_state != xi_enabled) return;
330 TRACE( "disabling\n" );
331 data->xi2_state = xi_disabled;
336 for (i = 0; i < data->xi2_device_count; ++i)
338 if (devices[i].use == XISlavePointer && devices[i].attachment == data->xi2_core_pointer)
340 mask.deviceid = devices[i].deviceid;
341 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
344 pXIFreeDeviceInfo( devices );
345 data->xi2_devices = NULL;
346 data->xi2_device_count = 0;
351 /***********************************************************************
352 * grab_clipping_window
354 * Start a pointer grab on the clip window.
356 static BOOL grab_clipping_window( const RECT *clip )
358 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
359 struct x11drv_thread_data *data = x11drv_thread_data();
363 if (!data) return FALSE;
364 if (!(clip_window = init_clip_window())) return TRUE;
366 if (!(msg_hwnd = CreateWindowW( messageW, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0,
367 GetModuleHandleW(0), NULL )))
370 /* enable XInput2 unless we are already clipping */
371 if (!data->clip_hwnd) enable_xinput2();
373 if (data->xi2_state != xi_enabled)
375 WARN( "XInput2 not supported, refusing to clip to %s\n", wine_dbgstr_rect(clip) );
376 DestroyWindow( msg_hwnd );
381 TRACE( "clipping to %s win %lx\n", wine_dbgstr_rect(clip), clip_window );
383 if (!data->clip_hwnd) XUnmapWindow( data->display, clip_window );
384 XMoveResizeWindow( data->display, clip_window,
385 clip->left - virtual_screen_rect.left, clip->top - virtual_screen_rect.top,
386 max( 1, clip->right - clip->left ), max( 1, clip->bottom - clip->top ) );
387 XMapWindow( data->display, clip_window );
389 /* if the rectangle is shrinking we may get a pointer warp */
390 if (!data->clip_hwnd || clip->left > clip_rect.left || clip->top > clip_rect.top ||
391 clip->right < clip_rect.right || clip->bottom < clip_rect.bottom)
392 data->warp_serial = NextRequest( data->display );
394 if (!XGrabPointer( data->display, clip_window, False,
395 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
396 GrabModeAsync, GrabModeAsync, clip_window, None, CurrentTime ))
399 if (!clipping_cursor)
402 DestroyWindow( msg_hwnd );
406 if (!data->clip_hwnd) sync_window_cursor( clip_window );
407 InterlockedExchangePointer( (void **)&cursor_window, msg_hwnd );
408 data->clip_hwnd = msg_hwnd;
409 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, (LPARAM)msg_hwnd );
413 /***********************************************************************
414 * ungrab_clipping_window
416 * Release the pointer grab on the clip window.
418 void ungrab_clipping_window(void)
420 Display *display = thread_init_display();
421 Window clip_window = init_clip_window();
423 if (!clip_window) return;
425 TRACE( "no longer clipping\n" );
426 XUnmapWindow( display, clip_window );
428 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, 0 );
431 /***********************************************************************
432 * reset_clipping_window
434 * Forcibly reset the window clipping on external events.
436 void reset_clipping_window(void)
438 ungrab_clipping_window();
439 ClipCursor( NULL ); /* make sure the clip rectangle is reset too */
442 /***********************************************************************
445 * Notification function called upon receiving a WM_X11DRV_CLIP_CURSOR.
447 LRESULT clip_cursor_notify( HWND hwnd, HWND new_clip_hwnd )
449 struct x11drv_thread_data *data = x11drv_thread_data();
451 if (hwnd == GetDesktopWindow()) /* change the clip window stored in the desktop process */
453 static HWND clip_hwnd;
455 HWND prev = clip_hwnd;
456 clip_hwnd = new_clip_hwnd;
457 if (prev || new_clip_hwnd) TRACE( "clip hwnd changed from %p to %p\n", prev, new_clip_hwnd );
458 if (prev) SendNotifyMessageW( prev, WM_X11DRV_CLIP_CURSOR, 0, 0 );
460 else if (hwnd == data->clip_hwnd) /* this is a notification that clipping has been reset */
462 TRACE( "clip hwnd reset from %p\n", hwnd );
464 data->clip_reset = GetTickCount();
466 DestroyWindow( hwnd );
468 else if (hwnd == GetForegroundWindow()) /* request to clip */
472 GetClipCursor( &clip );
473 if (clip.left > virtual_screen_rect.left || clip.right < virtual_screen_rect.right ||
474 clip.top > virtual_screen_rect.top || clip.bottom < virtual_screen_rect.bottom)
475 return grab_clipping_window( &clip );
480 /***********************************************************************
481 * clip_fullscreen_window
483 * Turn on clipping if the active window is fullscreen.
485 BOOL clip_fullscreen_window( HWND hwnd, BOOL reset )
487 struct x11drv_win_data *data;
488 struct x11drv_thread_data *thread_data;
493 if (hwnd == GetDesktopWindow()) return FALSE;
494 style = GetWindowLongW( hwnd, GWL_STYLE );
495 if (!(style & WS_VISIBLE)) return FALSE;
496 if ((style & (WS_POPUP | WS_CHILD)) == WS_CHILD) return FALSE;
497 /* maximized windows don't count as full screen */
498 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION) return FALSE;
499 if (!(data = get_win_data( hwnd ))) return FALSE;
500 fullscreen = is_window_rect_fullscreen( &data->whole_rect );
501 release_win_data( data );
502 if (!fullscreen) return FALSE;
503 if (!(thread_data = x11drv_thread_data())) return FALSE;
504 if (GetTickCount() - thread_data->clip_reset < 1000) return FALSE;
505 if (!reset && clipping_cursor && thread_data->clip_hwnd) return FALSE; /* already clipping */
506 SetRect( &rect, 0, 0, screen_width, screen_height );
507 if (!grab_fullscreen)
509 if (!EqualRect( &rect, &virtual_screen_rect )) return FALSE;
510 if (root_window != DefaultRootWindow( gdi_display )) return FALSE;
512 TRACE( "win %p clipping fullscreen\n", hwnd );
513 return grab_clipping_window( &rect );
516 /***********************************************************************
519 * Update the various window states on a mouse event.
521 static void send_mouse_input( HWND hwnd, Window window, unsigned int state, INPUT *input )
523 struct x11drv_win_data *data;
526 input->type = INPUT_MOUSE;
530 struct x11drv_thread_data *thread_data = x11drv_thread_data();
531 HWND clip_hwnd = thread_data->clip_hwnd;
533 if (!clip_hwnd) return;
534 if (thread_data->clip_window != window) return;
535 if (InterlockedExchangePointer( (void **)&cursor_window, clip_hwnd ) != clip_hwnd ||
536 input->u.mi.time - last_cursor_change > 100)
538 sync_window_cursor( window );
539 last_cursor_change = input->u.mi.time;
541 input->u.mi.dx += clip_rect.left;
542 input->u.mi.dy += clip_rect.top;
543 __wine_send_input( hwnd, input );
547 if (!(data = get_win_data( hwnd ))) return;
549 if (window == data->whole_window)
551 input->u.mi.dx += data->whole_rect.left - data->client_rect.left;
552 input->u.mi.dy += data->whole_rect.top - data->client_rect.top;
554 if (window == root_window)
556 input->u.mi.dx += virtual_screen_rect.left;
557 input->u.mi.dy += virtual_screen_rect.top;
559 pt.x = input->u.mi.dx;
560 pt.y = input->u.mi.dy;
561 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
562 pt.x = data->client_rect.right - data->client_rect.left - 1 - pt.x;
563 MapWindowPoints( hwnd, 0, &pt, 1 );
565 if (InterlockedExchangePointer( (void **)&cursor_window, hwnd ) != hwnd ||
566 input->u.mi.time - last_cursor_change > 100)
568 sync_window_cursor( data->whole_window );
569 last_cursor_change = input->u.mi.time;
571 release_win_data( data );
573 if (hwnd != GetDesktopWindow())
575 hwnd = GetAncestor( hwnd, GA_ROOT );
576 if ((input->u.mi.dwFlags & (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_RIGHTDOWN)) && hwnd == GetForegroundWindow())
577 clip_fullscreen_window( hwnd, FALSE );
580 /* update the wine server Z-order */
582 if (window != x11drv_thread_data()->grab_window &&
583 /* ignore event if a button is pressed, since the mouse is then grabbed too */
584 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
587 SetRect( &rect, pt.x, pt.y, pt.x + 1, pt.y + 1 );
588 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
590 SERVER_START_REQ( update_window_zorder )
592 req->window = wine_server_user_handle( hwnd );
593 req->rect.left = rect.left;
594 req->rect.top = rect.top;
595 req->rect.right = rect.right;
596 req->rect.bottom = rect.bottom;
597 wine_server_call( req );
602 input->u.mi.dx = pt.x;
603 input->u.mi.dy = pt.y;
604 __wine_send_input( hwnd, input );
607 #ifdef SONAME_LIBXCURSOR
609 /***********************************************************************
610 * create_xcursor_frame
612 * Use Xcursor to create a frame of an X cursor from a Windows one.
614 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
615 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
616 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
617 int width, int height, int istep )
619 XcursorImage *image, *ret = NULL;
620 DWORD delay_jiffies, num_steps;
621 int x, y, i, has_alpha = FALSE;
624 image = pXcursorImageCreate( width, height );
627 ERR("X11 failed to produce a cursor frame!\n");
631 image->xhot = iinfo->xHotspot;
632 image->yhot = iinfo->yHotspot;
634 image->delay = 100; /* fallback delay, 100 ms */
635 if (GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, istep, &delay_jiffies, &num_steps) != 0)
636 image->delay = (100 * delay_jiffies) / 6; /* convert jiffies (1/60s) to milliseconds */
638 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep);
640 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
641 memset( color_bits, 0x00, color_size );
642 SelectObject( hdc, hbmColor );
643 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
645 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
648 memcpy( image->pixels, color_bits, color_size );
650 /* check if the cursor frame was drawn with an alpha channel */
651 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
652 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
654 /* if no alpha channel was drawn then generate it from the mask */
657 unsigned int width_bytes = (width + 31) / 32 * 4;
659 /* draw the cursor mask to a temporary buffer */
660 memset( mask_bits, 0xFF, mask_size );
661 SelectObject( hdc, hbmMask );
662 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
664 ERR("Failed to draw frame mask %d.\n", istep);
667 /* use the buffer to directly modify the XcursorImage alpha channel */
668 for (y = 0, ptr = image->pixels; y < height; y++)
669 for (x = 0; x < width; x++, ptr++)
670 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
676 if (ret == NULL) pXcursorImageDestroy( image );
680 /***********************************************************************
681 * create_xcursor_cursor
683 * Use Xcursor to create an X cursor from a Windows one.
685 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
687 unsigned char *color_bits, *mask_bits;
688 HBITMAP hbmColor = 0, hbmMask = 0;
689 DWORD nFrames, delay_jiffies, i;
690 int color_size, mask_size;
691 BITMAPINFO *info = NULL;
692 XcursorImages *images;
696 /* Retrieve the number of frames to render */
697 if (!GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, 0, &delay_jiffies, &nFrames)) return 0;
698 if (!(imgs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XcursorImage*)*nFrames ))) return 0;
700 /* Allocate all of the resources necessary to obtain a cursor frame */
701 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
702 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
703 info->bmiHeader.biWidth = width;
704 info->bmiHeader.biHeight = -height;
705 info->bmiHeader.biPlanes = 1;
706 info->bmiHeader.biCompression = BI_RGB;
707 info->bmiHeader.biXPelsPerMeter = 0;
708 info->bmiHeader.biYPelsPerMeter = 0;
709 info->bmiHeader.biClrUsed = 0;
710 info->bmiHeader.biClrImportant = 0;
711 info->bmiHeader.biBitCount = 32;
712 color_size = width * height * 4;
713 info->bmiHeader.biSizeImage = color_size;
714 hbmColor = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &color_bits, NULL, 0);
717 ERR("Failed to create DIB section for cursor color data!\n");
720 info->bmiHeader.biBitCount = 1;
721 info->bmiColors[0].rgbRed = 0;
722 info->bmiColors[0].rgbGreen = 0;
723 info->bmiColors[0].rgbBlue = 0;
724 info->bmiColors[0].rgbReserved = 0;
725 info->bmiColors[1].rgbRed = 0xff;
726 info->bmiColors[1].rgbGreen = 0xff;
727 info->bmiColors[1].rgbBlue = 0xff;
728 info->bmiColors[1].rgbReserved = 0;
730 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
731 info->bmiHeader.biSizeImage = mask_size;
732 hbmMask = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &mask_bits, NULL, 0);
735 ERR("Failed to create DIB section for cursor mask data!\n");
739 /* Create an XcursorImage for each frame of the cursor */
740 for (i=0; i<nFrames; i++)
742 imgs[i] = create_xcursor_frame( hdc, iinfo, icon,
743 hbmColor, color_bits, color_size,
744 hbmMask, mask_bits, mask_size,
746 if (!imgs[i]) goto cleanup;
749 /* Build an X cursor out of all of the frames */
750 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
751 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
752 images->images[images->nimage] = imgs[images->nimage];
753 cursor = pXcursorImagesLoadCursor( gdi_display, images );
754 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
755 HeapFree( GetProcessHeap(), 0, imgs );
761 /* Failed to produce a cursor, free previously allocated frames */
762 for (i=0; i<nFrames && imgs[i]; i++)
763 pXcursorImageDestroy( imgs[i] );
764 HeapFree( GetProcessHeap(), 0, imgs );
766 /* Cleanup all of the resources used to obtain the frame data */
767 if (hbmColor) DeleteObject( hbmColor );
768 if (hbmMask) DeleteObject( hbmMask );
769 HeapFree( GetProcessHeap(), 0, info );
774 struct system_cursors
780 static const struct system_cursors user32_cursors[] =
782 { OCR_NORMAL, "left_ptr" },
783 { OCR_IBEAM, "xterm" },
784 { OCR_WAIT, "watch" },
785 { OCR_CROSS, "cross" },
786 { OCR_UP, "center_ptr" },
787 { OCR_SIZE, "fleur" },
788 { OCR_SIZEALL, "fleur" },
789 { OCR_ICON, "icon" },
790 { OCR_SIZENWSE, "nwse-resize" },
791 { OCR_SIZENESW, "nesw-resize" },
792 { OCR_SIZEWE, "ew-resize" },
793 { OCR_SIZENS, "ns-resize" },
794 { OCR_NO, "not-allowed" },
795 { OCR_HAND, "hand2" },
796 { OCR_APPSTARTING, "left_ptr_watch" },
797 { OCR_HELP, "question_arrow" },
801 static const struct system_cursors comctl32_cursors[] =
806 { 106, "row-resize" },
807 { 107, "row-resize" },
809 { 135, "col-resize" },
813 static const struct system_cursors ole32_cursors[] =
822 static const struct system_cursors riched20_cursors[] =
825 { 107, "right_ptr" },
834 const struct system_cursors *cursors;
838 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
839 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
840 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
841 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
844 /***********************************************************************
845 * create_xcursor_system_cursor
847 * Create an X cursor for a system cursor.
849 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
851 static const WCHAR idW[] = {'%','h','u',0};
852 const struct system_cursors *cursors;
857 WCHAR *p, name[MAX_PATH * 2], valueW[64];
861 if (!pXcursorLibraryLoadCursor) return 0;
862 if (!info->szModName[0]) return 0;
864 p = strrchrW( info->szModName, '\\' );
865 strcpyW( name, p ? p + 1 : info->szModName );
866 p = name + strlenW( name );
868 if (info->szResName[0]) strcpyW( p, info->szResName );
869 else sprintfW( p, idW, info->wResID );
872 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
873 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver\\Cursors", &key ))
875 size = sizeof(valueW) / sizeof(WCHAR);
876 ret = RegQueryValueExW( key, name, NULL, NULL, (BYTE *)valueW, &size );
880 if (!valueW[0]) return 0; /* force standard cursor */
881 if (!WideCharToMultiByte( CP_UNIXCP, 0, valueW, -1, valueA, sizeof(valueA), NULL, NULL ))
887 if (info->szResName[0]) goto done; /* only integer resources are supported here */
888 if (!(module = GetModuleHandleW( info->szModName ))) goto done;
890 for (i = 0; i < sizeof(module_cursors)/sizeof(module_cursors[0]); i++)
891 if (GetModuleHandleW( module_cursors[i].name ) == module) break;
892 if (i == sizeof(module_cursors)/sizeof(module_cursors[0])) goto done;
894 cursors = module_cursors[i].cursors;
895 for (i = 0; cursors[i].id; i++)
896 if (cursors[i].id == info->wResID)
898 strcpy( valueA, cursors[i].name );
905 cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
906 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
907 debugstr_w(name), debugstr_a(valueA) );
909 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
913 #endif /* SONAME_LIBXCURSOR */
916 /***********************************************************************
917 * create_xlib_monochrome_cursor
919 * Create a monochrome X cursor from a Windows one.
921 static Cursor create_xlib_monochrome_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
923 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
924 BITMAPINFO *info = (BITMAPINFO *)buffer;
926 const int xor_y = height;
927 unsigned int width_bytes = (width + 31) / 32 * 4;
928 unsigned char *mask_bits = NULL;
932 Pixmap src_pixmap, bits_pixmap, mask_pixmap;
933 struct gdi_image_bits bits;
936 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
937 info->bmiHeader.biWidth = width;
938 info->bmiHeader.biHeight = -height * 2;
939 info->bmiHeader.biPlanes = 1;
940 info->bmiHeader.biBitCount = 1;
941 info->bmiHeader.biCompression = BI_RGB;
942 info->bmiHeader.biSizeImage = width_bytes * height * 2;
943 info->bmiHeader.biXPelsPerMeter = 0;
944 info->bmiHeader.biYPelsPerMeter = 0;
945 info->bmiHeader.biClrUsed = 0;
946 info->bmiHeader.biClrImportant = 0;
948 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
949 if (!GetDIBits( hdc, icon->hbmMask, 0, height * 2, mask_bits, info, DIB_RGB_COLORS )) goto done;
952 bits.ptr = mask_bits;
955 if (!(src_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
957 bits_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
958 mask_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
959 gc = XCreateGC( gdi_display, src_pixmap, 0, NULL );
960 XSetGraphicsExposures( gdi_display, gc, False );
962 /* We have to do some magic here, as cursors are not fully
963 * compatible between Windows and X11. Under X11, there are
964 * only 3 possible color cursor: black, white and masked. So
965 * we map the 4th Windows color (invert the bits on the screen)
966 * to black and an additional white bit on another place
967 * (+1,+1). This require some boolean arithmetic:
970 * And Xor Result | Bits Mask Result
971 * 0 0 black | 0 1 background
972 * 0 1 white | 1 1 foreground
973 * 1 0 no change | X 0 no change
974 * 1 1 inverted | 0 1 background
977 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
978 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
980 XSetFunction( gdi_display, gc, GXcopy );
981 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 0, 0 );
982 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 0, 0 );
983 XSetFunction( gdi_display, gc, GXandReverse );
984 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, xor_y, width, height, 0, 0 );
985 XSetFunction( gdi_display, gc, GXorReverse );
986 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, xor_y, width, height, 0, 0 );
987 /* additional white */
988 XSetFunction( gdi_display, gc, GXand );
989 XCopyArea( gdi_display, src_pixmap, src_pixmap, gc, 0, xor_y, width, height, 0, and_y );
990 XSetFunction( gdi_display, gc, GXor );
991 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 1, 1 );
992 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 1, 1 );
993 XFreeGC( gdi_display, gc );
995 fg.red = fg.green = fg.blue = 0xffff;
996 bg.red = bg.green = bg.blue = 0;
997 cursor = XCreatePixmapCursor( gdi_display, bits_pixmap, mask_pixmap,
998 &fg, &bg, icon->xHotspot, icon->yHotspot );
999 XFreePixmap( gdi_display, src_pixmap );
1000 XFreePixmap( gdi_display, bits_pixmap );
1001 XFreePixmap( gdi_display, mask_pixmap );
1004 HeapFree( GetProcessHeap(), 0, mask_bits );
1008 /***********************************************************************
1009 * create_xlib_color_cursor
1011 * Create a color X cursor from a Windows one.
1013 static Cursor create_xlib_color_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1015 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1016 BITMAPINFO *info = (BITMAPINFO *)buffer;
1018 Cursor cursor = None;
1020 Pixmap xor_pixmap, mask_pixmap;
1021 struct gdi_image_bits bits;
1022 unsigned int *color_bits = NULL, *ptr;
1023 unsigned char *mask_bits = NULL, *xor_bits = NULL;
1024 int i, x, y, has_alpha = 0;
1025 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
1026 unsigned int width_bytes = (width + 31) / 32 * 4;
1028 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1029 info->bmiHeader.biWidth = width;
1030 info->bmiHeader.biHeight = -height;
1031 info->bmiHeader.biPlanes = 1;
1032 info->bmiHeader.biBitCount = 1;
1033 info->bmiHeader.biCompression = BI_RGB;
1034 info->bmiHeader.biSizeImage = width_bytes * height;
1035 info->bmiHeader.biXPelsPerMeter = 0;
1036 info->bmiHeader.biYPelsPerMeter = 0;
1037 info->bmiHeader.biClrUsed = 0;
1038 info->bmiHeader.biClrImportant = 0;
1040 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1041 if (!GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS )) goto done;
1043 info->bmiHeader.biBitCount = 32;
1044 info->bmiHeader.biSizeImage = width * height * 4;
1045 if (!(color_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1046 if (!(xor_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, width_bytes * height ))) goto done;
1047 GetDIBits( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS );
1049 /* compute fg/bg color and xor bitmap based on average of the color values */
1051 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
1052 for (y = 0, ptr = color_bits; y < height; y++)
1054 for (x = 0; x < width; x++, ptr++)
1056 int red = (*ptr >> 16) & 0xff;
1057 int green = (*ptr >> 8) & 0xff;
1058 int blue = (*ptr >> 0) & 0xff;
1059 if (red + green + blue > 0x40)
1065 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1077 fg.red = rfg * 257 / fgBits;
1078 fg.green = gfg * 257 / fgBits;
1079 fg.blue = bfg * 257 / fgBits;
1081 else fg.red = fg.green = fg.blue = 0;
1082 bgBits = width * height - fgBits;
1085 bg.red = rbg * 257 / bgBits;
1086 bg.green = gbg * 257 / bgBits;
1087 bg.blue = bbg * 257 / bgBits;
1089 else bg.red = bg.green = bg.blue = 0;
1091 info->bmiHeader.biBitCount = 1;
1092 info->bmiHeader.biClrUsed = 0;
1093 info->bmiHeader.biSizeImage = width_bytes * height;
1095 /* generate mask from the alpha channel if we have one */
1097 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
1098 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
1102 memset( mask_bits, 0, width_bytes * height );
1103 for (y = 0, ptr = color_bits; y < height; y++)
1104 for (x = 0; x < width; x++, ptr++)
1105 if ((*ptr >> 24) > 25) /* more than 10% alpha */
1106 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1108 else /* invert the mask */
1110 ptr = (unsigned int *)mask_bits;
1111 for (i = 0; i < info->bmiHeader.biSizeImage / sizeof(*ptr); i++, ptr++) *ptr ^= ~0u;
1115 bits.ptr = xor_bits;
1117 bits.is_copy = TRUE;
1118 if (!(xor_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1120 bits.ptr = mask_bits;
1121 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
1125 cursor = XCreatePixmapCursor( gdi_display, xor_pixmap, mask_pixmap,
1126 &fg, &bg, icon->xHotspot, icon->yHotspot );
1127 XFreePixmap( gdi_display, mask_pixmap );
1129 XFreePixmap( gdi_display, xor_pixmap );
1132 HeapFree( GetProcessHeap(), 0, color_bits );
1133 HeapFree( GetProcessHeap(), 0, xor_bits );
1134 HeapFree( GetProcessHeap(), 0, mask_bits );
1138 /***********************************************************************
1141 * Create an X cursor from a Windows one.
1143 static Cursor create_cursor( HANDLE handle )
1150 if (!handle) return get_empty_cursor();
1152 info.cbSize = sizeof(info);
1153 if (!GetIconInfoExW( handle, &info )) return 0;
1155 #ifdef SONAME_LIBXCURSOR
1156 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
1158 DeleteObject( info.hbmColor );
1159 DeleteObject( info.hbmMask );
1164 GetObjectW( info.hbmMask, sizeof(bm), &bm );
1165 if (!info.hbmColor) bm.bmHeight = max( 1, bm.bmHeight / 2 );
1167 /* make sure hotspot is valid */
1168 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1170 info.xHotspot = bm.bmWidth / 2;
1171 info.yHotspot = bm.bmHeight / 2;
1174 hdc = CreateCompatibleDC( 0 );
1178 #ifdef SONAME_LIBXCURSOR
1179 if (pXcursorImagesLoadCursor)
1180 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
1182 if (!cursor) cursor = create_xlib_color_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1183 DeleteObject( info.hbmColor );
1187 cursor = create_xlib_monochrome_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1190 DeleteObject( info.hbmMask );
1195 /***********************************************************************
1196 * DestroyCursorIcon (X11DRV.@)
1198 void CDECL X11DRV_DestroyCursorIcon( HCURSOR handle )
1202 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1204 TRACE( "%p xid %lx\n", handle, cursor );
1205 XFreeCursor( gdi_display, cursor );
1206 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1210 /***********************************************************************
1211 * SetCursor (X11DRV.@)
1213 void CDECL X11DRV_SetCursor( HCURSOR handle )
1215 if (InterlockedExchangePointer( (void **)&last_cursor, handle ) != handle ||
1216 GetTickCount() - last_cursor_change > 100)
1218 last_cursor_change = GetTickCount();
1219 if (cursor_window) SendNotifyMessageW( cursor_window, WM_X11DRV_SET_CURSOR, 0, (LPARAM)handle );
1223 /***********************************************************************
1224 * SetCursorPos (X11DRV.@)
1226 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
1228 struct x11drv_thread_data *data = x11drv_init_thread_data();
1230 XWarpPointer( data->display, root_window, root_window, 0, 0, 0, 0,
1231 x - virtual_screen_rect.left, y - virtual_screen_rect.top );
1232 data->warp_serial = NextRequest( data->display );
1233 XNoOp( data->display );
1234 XFlush( data->display ); /* avoids bad mouse lag in games that do their own mouse warping */
1235 TRACE( "warped to %d,%d serial %lu\n", x, y, data->warp_serial );
1239 /***********************************************************************
1240 * GetCursorPos (X11DRV.@)
1242 BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
1244 Display *display = thread_init_display();
1246 int rootX, rootY, winX, winY;
1247 unsigned int xstate;
1250 ret = XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &winX, &winY, &xstate );
1254 pos->x = winX + virtual_screen_rect.left;
1255 pos->y = winY + virtual_screen_rect.top;
1256 TRACE( "pointer at (%d,%d) server pos %d,%d\n", pos->x, pos->y, old.x, old.y );
1261 /***********************************************************************
1262 * ClipCursor (X11DRV.@)
1264 BOOL CDECL X11DRV_ClipCursor( LPCRECT clip )
1268 ungrab_clipping_window();
1272 if (GetWindowThreadProcessId( GetDesktopWindow(), NULL ) == GetCurrentThreadId())
1273 return TRUE; /* don't clip in the desktop process */
1277 HWND foreground = GetForegroundWindow();
1279 /* we are clipping if the clip rectangle is smaller than the screen */
1280 if (clip->left > virtual_screen_rect.left || clip->right < virtual_screen_rect.right ||
1281 clip->top > virtual_screen_rect.top || clip->bottom < virtual_screen_rect.bottom)
1285 /* forward request to the foreground window if it's in a different thread */
1286 tid = GetWindowThreadProcessId( foreground, &pid );
1287 if (tid && tid != GetCurrentThreadId() && pid == GetCurrentProcessId())
1289 TRACE( "forwarding clip request to %p\n", foreground );
1290 SendNotifyMessageW( foreground, WM_X11DRV_CLIP_CURSOR, 0, 0 );
1293 else if (grab_clipping_window( clip )) return TRUE;
1295 else /* if currently clipping, check if we should switch to fullscreen clipping */
1297 struct x11drv_thread_data *data = x11drv_thread_data();
1298 if (data && data->clip_hwnd)
1300 if (EqualRect( clip, &clip_rect ) || clip_fullscreen_window( foreground, TRUE ))
1305 ungrab_clipping_window();
1309 /***********************************************************************
1310 * move_resize_window
1312 void move_resize_window( HWND hwnd, int dir )
1314 Display *display = thread_display();
1316 int x, y, rootX, rootY, button = 0;
1318 Window win, root, child;
1319 unsigned int xstate;
1321 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1323 pt = GetMessagePos();
1324 x = (short)LOWORD( pt );
1325 y = (short)HIWORD( pt );
1327 if (GetKeyState( VK_LBUTTON ) & 0x8000) button = 1;
1328 else if (GetKeyState( VK_MBUTTON ) & 0x8000) button = 2;
1329 else if (GetKeyState( VK_RBUTTON ) & 0x8000) button = 3;
1331 TRACE( "hwnd %p/%lx, x %d, y %d, dir %d, button %d\n", hwnd, win, x, y, dir, button );
1333 xev.xclient.type = ClientMessage;
1334 xev.xclient.window = win;
1335 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1336 xev.xclient.serial = 0;
1337 xev.xclient.display = display;
1338 xev.xclient.send_event = True;
1339 xev.xclient.format = 32;
1340 xev.xclient.data.l[0] = x - virtual_screen_rect.left; /* x coord */
1341 xev.xclient.data.l[1] = y - virtual_screen_rect.top; /* y coord */
1342 xev.xclient.data.l[2] = dir; /* direction */
1343 xev.xclient.data.l[3] = button; /* button */
1344 xev.xclient.data.l[4] = 0; /* unused */
1346 /* need to ungrab the pointer that may have been automatically grabbed
1347 * with a ButtonPress event */
1348 XUngrabPointer( display, CurrentTime );
1349 XSendEvent(display, root_window, False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
1351 /* try to detect the end of the size/move by polling for the mouse button to be released */
1352 /* (some apps don't like it if we return before the size/move is done) */
1354 if (!button) return;
1360 if (!XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &x, &y, &xstate )) break;
1362 if (!(xstate & (Button1Mask << (button - 1))))
1364 /* fake a button release event */
1365 input.type = INPUT_MOUSE;
1366 input.u.mi.dx = x + virtual_screen_rect.left;
1367 input.u.mi.dy = y + virtual_screen_rect.top;
1368 input.u.mi.mouseData = button_up_data[button - 1];
1369 input.u.mi.dwFlags = button_up_flags[button - 1] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1370 input.u.mi.time = GetTickCount();
1371 input.u.mi.dwExtraInfo = 0;
1372 __wine_send_input( hwnd, &input );
1375 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
1377 if (!CallMsgFilterW( &msg, MSGF_SIZE ))
1379 TranslateMessage( &msg );
1380 DispatchMessageW( &msg );
1384 if (!(xstate & (Button1Mask << (button - 1)))) break;
1385 MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT );
1388 TRACE( "hwnd %p/%lx done\n", hwnd, win );
1392 /***********************************************************************
1393 * X11DRV_ButtonPress
1395 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1397 XButtonEvent *event = &xev->xbutton;
1398 int buttonNum = event->button - 1;
1401 if (buttonNum >= NB_BUTTONS) return;
1403 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1405 input.u.mi.dx = event->x;
1406 input.u.mi.dy = event->y;
1407 input.u.mi.mouseData = button_down_data[buttonNum];
1408 input.u.mi.dwFlags = button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1409 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1410 input.u.mi.dwExtraInfo = 0;
1412 update_user_time( event->time );
1413 send_mouse_input( hwnd, event->window, event->state, &input );
1417 /***********************************************************************
1418 * X11DRV_ButtonRelease
1420 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1422 XButtonEvent *event = &xev->xbutton;
1423 int buttonNum = event->button - 1;
1426 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1428 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1430 input.u.mi.dx = event->x;
1431 input.u.mi.dy = event->y;
1432 input.u.mi.mouseData = button_up_data[buttonNum];
1433 input.u.mi.dwFlags = button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1434 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1435 input.u.mi.dwExtraInfo = 0;
1437 send_mouse_input( hwnd, event->window, event->state, &input );
1441 /***********************************************************************
1442 * X11DRV_MotionNotify
1444 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1446 XMotionEvent *event = &xev->xmotion;
1449 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d serial %lu\n",
1450 hwnd, event->window, event->x, event->y, event->is_hint, event->serial );
1452 input.u.mi.dx = event->x;
1453 input.u.mi.dy = event->y;
1454 input.u.mi.mouseData = 0;
1455 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1456 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1457 input.u.mi.dwExtraInfo = 0;
1461 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1462 if (thread_data->warp_serial && (long)(event->serial - thread_data->warp_serial) < 0) return;
1465 send_mouse_input( hwnd, event->window, event->state, &input );
1469 /***********************************************************************
1470 * X11DRV_EnterNotify
1472 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1474 XCrossingEvent *event = &xev->xcrossing;
1477 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd, event->window, event->x, event->y, event->detail );
1479 if (event->detail == NotifyVirtual) return;
1480 if (event->window == x11drv_thread_data()->grab_window) return;
1482 /* simulate a mouse motion event */
1483 input.u.mi.dx = event->x;
1484 input.u.mi.dy = event->y;
1485 input.u.mi.mouseData = 0;
1486 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1487 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1488 input.u.mi.dwExtraInfo = 0;
1490 send_mouse_input( hwnd, event->window, event->state, &input );
1493 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1495 /***********************************************************************
1498 static void X11DRV_RawMotion( XGenericEventCookie *xev )
1500 XIRawEvent *event = xev->data;
1501 const double *values = event->valuators.values;
1504 double dx = 0, dy = 0;
1505 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1506 XIDeviceInfo *devices = thread_data->xi2_devices;
1508 if (!event->valuators.mask_len) return;
1509 if (thread_data->xi2_state != xi_enabled) return;
1511 input.u.mi.mouseData = 0;
1512 input.u.mi.dwFlags = MOUSEEVENTF_MOVE;
1513 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1514 input.u.mi.dwExtraInfo = 0;
1518 for (i = 0; i < thread_data->xi2_device_count; ++i)
1520 if (devices[i].deviceid != event->deviceid) continue;
1521 for (j = 0; j < devices[i].num_classes; j++)
1523 XIValuatorClassInfo *class = (XIValuatorClassInfo *)devices[i].classes[j];
1525 if (devices[i].classes[j]->type != XIValuatorClass) continue;
1526 if (XIMaskIsSet( event->valuators.mask, class->number ))
1528 double val = *values++;
1529 if (class->label == x11drv_atom( Rel_X ))
1531 input.u.mi.dx = dx = val;
1532 if (class->min < class->max)
1533 input.u.mi.dx = val * (virtual_screen_rect.right - virtual_screen_rect.left)
1534 / (class->max - class->min);
1536 else if (class->label == x11drv_atom( Rel_Y ))
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 );