dsound: Don't write more than three periods to IAudioClient.
[wine.git] / dlls / winex11.drv / mouse.c
blob186899fb1685eada08ee0b6d02545d66e65be714
1 /*
2 * X11 mouse driver
4 * Copyright 1998 Ulrich Weigand
5 * Copyright 2007 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <X11/Xlib.h>
26 #include <X11/cursorfont.h>
27 #include <stdarg.h>
28 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
29 #include <X11/extensions/XInput2.h>
30 #endif
32 #ifdef SONAME_LIBXCURSOR
33 # include <X11/Xcursor/Xcursor.h>
34 static void *xcursor_handle;
35 # define MAKE_FUNCPTR(f) static typeof(f) * p##f
36 MAKE_FUNCPTR(XcursorImageCreate);
37 MAKE_FUNCPTR(XcursorImageDestroy);
38 MAKE_FUNCPTR(XcursorImageLoadCursor);
39 MAKE_FUNCPTR(XcursorImagesCreate);
40 MAKE_FUNCPTR(XcursorImagesDestroy);
41 MAKE_FUNCPTR(XcursorImagesLoadCursor);
42 MAKE_FUNCPTR(XcursorLibraryLoadCursor);
43 # undef MAKE_FUNCPTR
44 #endif /* SONAME_LIBXCURSOR */
46 #define NONAMELESSUNION
47 #define OEMRESOURCE
48 #include "windef.h"
49 #include "winbase.h"
50 #include "winreg.h"
52 #include "x11drv.h"
53 #include "wine/server.h"
54 #include "wine/library.h"
55 #include "wine/unicode.h"
56 #include "wine/debug.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
60 /**********************************************************************/
62 #ifndef Button6Mask
63 #define Button6Mask (1<<13)
64 #endif
65 #ifndef Button7Mask
66 #define Button7Mask (1<<14)
67 #endif
69 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
71 static const UINT button_down_flags[NB_BUTTONS] =
73 MOUSEEVENTF_LEFTDOWN,
74 MOUSEEVENTF_MIDDLEDOWN,
75 MOUSEEVENTF_RIGHTDOWN,
76 MOUSEEVENTF_WHEEL,
77 MOUSEEVENTF_WHEEL,
78 MOUSEEVENTF_XDOWN, /* FIXME: horizontal wheel */
79 MOUSEEVENTF_XDOWN,
80 MOUSEEVENTF_XDOWN,
81 MOUSEEVENTF_XDOWN
84 static const UINT button_up_flags[NB_BUTTONS] =
86 MOUSEEVENTF_LEFTUP,
87 MOUSEEVENTF_MIDDLEUP,
88 MOUSEEVENTF_RIGHTUP,
91 MOUSEEVENTF_XUP,
92 MOUSEEVENTF_XUP,
93 MOUSEEVENTF_XUP,
94 MOUSEEVENTF_XUP
97 static const UINT button_down_data[NB_BUTTONS] =
102 WHEEL_DELTA,
103 -WHEEL_DELTA,
104 XBUTTON1,
105 XBUTTON2,
106 XBUTTON1,
107 XBUTTON2
110 static const UINT button_up_data[NB_BUTTONS] =
117 XBUTTON1,
118 XBUTTON2,
119 XBUTTON1,
120 XBUTTON2
123 XContext cursor_context = 0;
125 static HWND cursor_window;
126 static HCURSOR last_cursor;
127 static DWORD last_cursor_change;
128 static RECT clip_rect;
129 static Cursor create_cursor( HANDLE handle );
131 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
132 static BOOL xinput2_available;
133 static BOOL broken_rawevents;
134 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
135 MAKE_FUNCPTR(XIGetClientPointer);
136 MAKE_FUNCPTR(XIFreeDeviceInfo);
137 MAKE_FUNCPTR(XIQueryDevice);
138 MAKE_FUNCPTR(XIQueryVersion);
139 MAKE_FUNCPTR(XISelectEvents);
140 #undef MAKE_FUNCPTR
141 #endif
143 /***********************************************************************
144 * X11DRV_Xcursor_Init
146 * Load the Xcursor library for use.
148 void X11DRV_Xcursor_Init(void)
150 #ifdef SONAME_LIBXCURSOR
151 xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
152 if (!xcursor_handle) /* wine_dlopen failed. */
154 WARN("Xcursor failed to load. Using fallback code.\n");
155 return;
157 #define LOAD_FUNCPTR(f) \
158 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
160 LOAD_FUNCPTR(XcursorImageCreate);
161 LOAD_FUNCPTR(XcursorImageDestroy);
162 LOAD_FUNCPTR(XcursorImageLoadCursor);
163 LOAD_FUNCPTR(XcursorImagesCreate);
164 LOAD_FUNCPTR(XcursorImagesDestroy);
165 LOAD_FUNCPTR(XcursorImagesLoadCursor);
166 LOAD_FUNCPTR(XcursorLibraryLoadCursor);
167 #undef LOAD_FUNCPTR
168 #endif /* SONAME_LIBXCURSOR */
172 /***********************************************************************
173 * get_empty_cursor
175 static Cursor get_empty_cursor(void)
177 static Cursor cursor;
178 static const char data[] = { 0 };
180 if (!cursor)
182 XColor bg;
183 Pixmap pixmap;
185 bg.red = bg.green = bg.blue = 0x0000;
186 pixmap = XCreateBitmapFromData( gdi_display, root_window, data, 1, 1 );
187 if (pixmap)
189 Cursor new = XCreatePixmapCursor( gdi_display, pixmap, pixmap, &bg, &bg, 0, 0 );
190 if (InterlockedCompareExchangePointer( (void **)&cursor, (void *)new, 0 ))
191 XFreeCursor( gdi_display, new );
192 XFreePixmap( gdi_display, pixmap );
195 return cursor;
198 /***********************************************************************
199 * set_window_cursor
201 void set_window_cursor( Window window, HCURSOR handle )
203 Cursor cursor, prev;
205 if (!handle) cursor = get_empty_cursor();
206 else if (XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
208 /* try to create it */
209 if (!(cursor = create_cursor( handle ))) return;
211 XLockDisplay( gdi_display );
212 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&prev ))
214 /* someone else was here first */
215 XFreeCursor( gdi_display, cursor );
216 cursor = prev;
218 else
220 XSaveContext( gdi_display, (XID)handle, cursor_context, (char *)cursor );
221 TRACE( "cursor %p created %lx\n", handle, cursor );
223 XUnlockDisplay( gdi_display );
226 XDefineCursor( gdi_display, window, cursor );
227 /* make the change take effect immediately */
228 XFlush( gdi_display );
231 /***********************************************************************
232 * sync_window_cursor
234 void sync_window_cursor( Window window )
236 HCURSOR cursor;
238 SERVER_START_REQ( set_cursor )
240 req->flags = 0;
241 wine_server_call( req );
242 cursor = reply->prev_count >= 0 ? wine_server_ptr_handle( reply->prev_handle ) : 0;
244 SERVER_END_REQ;
246 set_window_cursor( window, cursor );
249 /***********************************************************************
250 * enable_xinput2
252 static void enable_xinput2(void)
254 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
255 struct x11drv_thread_data *data = x11drv_thread_data();
256 XIEventMask mask;
257 XIDeviceInfo *devices;
258 unsigned char mask_bits[XIMaskLen(XI_LASTEVENT)];
259 int i;
261 if (!xinput2_available) return;
263 if (data->xi2_state == xi_unknown)
265 int major = 2, minor = 0;
266 if (!pXIQueryVersion( data->display, &major, &minor )) data->xi2_state = xi_disabled;
267 else
269 data->xi2_state = xi_unavailable;
270 WARN( "X Input 2 not available\n" );
273 if (data->xi2_state == xi_unavailable) return;
275 if (data->xi2_devices) pXIFreeDeviceInfo( data->xi2_devices );
276 if (!pXIGetClientPointer( data->display, None, &data->xi2_core_pointer )) return;
277 data->xi2_devices = devices = pXIQueryDevice( data->display, XIAllDevices, &data->xi2_device_count );
279 mask.mask = mask_bits;
280 mask.mask_len = sizeof(mask_bits);
281 memset( mask_bits, 0, sizeof(mask_bits) );
282 XISetMask( mask_bits, XI_RawMotion );
283 XISetMask( mask_bits, XI_ButtonPress );
285 for (i = 0; i < data->xi2_device_count; ++i)
287 if (devices[i].use == XISlavePointer && devices[i].attachment == data->xi2_core_pointer)
289 TRACE( "Device %u (%s) is attached to the core pointer\n",
290 devices[i].deviceid, debugstr_a(devices[i].name) );
291 mask.deviceid = devices[i].deviceid;
292 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
293 data->xi2_state = xi_enabled;
296 #endif
299 /***********************************************************************
300 * disable_xinput2
302 static void disable_xinput2(void)
304 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
305 struct x11drv_thread_data *data = x11drv_thread_data();
306 XIDeviceInfo *devices = data->xi2_devices;
307 XIEventMask mask;
308 int i;
310 if (data->xi2_state != xi_enabled) return;
312 TRACE( "disabling\n" );
313 data->xi2_state = xi_disabled;
315 mask.mask = NULL;
316 mask.mask_len = 0;
318 for (i = 0; i < data->xi2_device_count; ++i)
320 if (devices[i].use == XISlavePointer && devices[i].attachment == data->xi2_core_pointer)
322 mask.deviceid = devices[i].deviceid;
323 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
326 pXIFreeDeviceInfo( devices );
327 data->xi2_devices = NULL;
328 data->xi2_device_count = 0;
329 #endif
333 /***********************************************************************
334 * grab_clipping_window
336 * Start a pointer grab on the clip window.
338 static BOOL grab_clipping_window( const RECT *clip )
340 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
341 struct x11drv_thread_data *data = x11drv_thread_data();
342 Window clip_window;
343 HWND msg_hwnd = 0;
344 POINT pos;
346 if (GetWindowThreadProcessId( GetDesktopWindow(), NULL ) == GetCurrentThreadId())
347 return TRUE; /* don't clip in the desktop process */
349 if (!data) return FALSE;
350 if (!(clip_window = init_clip_window())) return TRUE;
352 if (!(msg_hwnd = CreateWindowW( messageW, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0,
353 GetModuleHandleW(0), NULL )))
354 return TRUE;
356 /* enable XInput2 unless we are already clipping */
357 if (!data->clip_hwnd) enable_xinput2();
359 if (data->xi2_state != xi_enabled)
361 WARN( "XInput2 not supported, refusing to clip to %s\n", wine_dbgstr_rect(clip) );
362 DestroyWindow( msg_hwnd );
363 ClipCursor( NULL );
364 return TRUE;
367 TRACE( "clipping to %s win %lx\n", wine_dbgstr_rect(clip), clip_window );
369 if (!data->clip_hwnd) XUnmapWindow( data->display, clip_window );
370 pos = virtual_screen_to_root( clip->left, clip->top );
371 XMoveResizeWindow( data->display, clip_window, pos.x, pos.y,
372 max( 1, clip->right - clip->left ), max( 1, clip->bottom - clip->top ) );
373 XMapWindow( data->display, clip_window );
375 /* if the rectangle is shrinking we may get a pointer warp */
376 if (!data->clip_hwnd || clip->left > clip_rect.left || clip->top > clip_rect.top ||
377 clip->right < clip_rect.right || clip->bottom < clip_rect.bottom)
378 data->warp_serial = NextRequest( data->display );
380 if (!XGrabPointer( data->display, clip_window, False,
381 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
382 GrabModeAsync, GrabModeAsync, clip_window, None, CurrentTime ))
383 clipping_cursor = TRUE;
385 if (!clipping_cursor)
387 disable_xinput2();
388 DestroyWindow( msg_hwnd );
389 return FALSE;
391 clip_rect = *clip;
392 if (!data->clip_hwnd) sync_window_cursor( clip_window );
393 InterlockedExchangePointer( (void **)&cursor_window, msg_hwnd );
394 data->clip_hwnd = msg_hwnd;
395 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, (LPARAM)msg_hwnd );
396 return TRUE;
399 /***********************************************************************
400 * ungrab_clipping_window
402 * Release the pointer grab on the clip window.
404 void ungrab_clipping_window(void)
406 Display *display = thread_init_display();
407 Window clip_window = init_clip_window();
409 if (!clip_window) return;
411 TRACE( "no longer clipping\n" );
412 XUnmapWindow( display, clip_window );
413 clipping_cursor = FALSE;
414 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, 0 );
417 /***********************************************************************
418 * reset_clipping_window
420 * Forcibly reset the window clipping on external events.
422 void reset_clipping_window(void)
424 ungrab_clipping_window();
425 ClipCursor( NULL ); /* make sure the clip rectangle is reset too */
428 /***********************************************************************
429 * clip_cursor_notify
431 * Notification function called upon receiving a WM_X11DRV_CLIP_CURSOR.
433 LRESULT clip_cursor_notify( HWND hwnd, HWND new_clip_hwnd )
435 struct x11drv_thread_data *data = x11drv_init_thread_data();
437 if (hwnd == GetDesktopWindow()) /* change the clip window stored in the desktop process */
439 static HWND clip_hwnd;
441 HWND prev = clip_hwnd;
442 clip_hwnd = new_clip_hwnd;
443 if (prev || new_clip_hwnd) TRACE( "clip hwnd changed from %p to %p\n", prev, new_clip_hwnd );
444 if (prev) SendNotifyMessageW( prev, WM_X11DRV_CLIP_CURSOR, 0, 0 );
446 else if (hwnd == data->clip_hwnd) /* this is a notification that clipping has been reset */
448 TRACE( "clip hwnd reset from %p\n", hwnd );
449 data->clip_hwnd = 0;
450 data->clip_reset = GetTickCount();
451 disable_xinput2();
452 DestroyWindow( hwnd );
454 else if (hwnd == GetForegroundWindow()) /* request to clip */
456 RECT clip, virtual_rect = get_virtual_screen_rect();
458 GetClipCursor( &clip );
459 if (clip.left > virtual_rect.left || clip.right < virtual_rect.right ||
460 clip.top > virtual_rect.top || clip.bottom < virtual_rect.bottom)
461 return grab_clipping_window( &clip );
463 return 0;
466 /***********************************************************************
467 * clip_fullscreen_window
469 * Turn on clipping if the active window is fullscreen.
471 BOOL clip_fullscreen_window( HWND hwnd, BOOL reset )
473 struct x11drv_win_data *data;
474 struct x11drv_thread_data *thread_data;
475 RECT rect;
476 DWORD style;
477 BOOL fullscreen;
479 if (hwnd == GetDesktopWindow()) return FALSE;
480 style = GetWindowLongW( hwnd, GWL_STYLE );
481 if (!(style & WS_VISIBLE)) return FALSE;
482 if ((style & (WS_POPUP | WS_CHILD)) == WS_CHILD) return FALSE;
483 /* maximized windows don't count as full screen */
484 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION) return FALSE;
485 if (!(data = get_win_data( hwnd ))) return FALSE;
486 fullscreen = is_window_rect_fullscreen( &data->whole_rect );
487 release_win_data( data );
488 if (!fullscreen) return FALSE;
489 if (!(thread_data = x11drv_thread_data())) return FALSE;
490 if (GetTickCount() - thread_data->clip_reset < 1000) return FALSE;
491 if (!reset && clipping_cursor && thread_data->clip_hwnd) return FALSE; /* already clipping */
492 rect = get_primary_monitor_rect();
493 if (!grab_fullscreen)
495 RECT virtual_rect = get_virtual_screen_rect();
496 if (!EqualRect( &rect, &virtual_rect )) return FALSE;
497 if (root_window != DefaultRootWindow( gdi_display )) return FALSE;
499 TRACE( "win %p clipping fullscreen\n", hwnd );
500 return grab_clipping_window( &rect );
504 /***********************************************************************
505 * is_old_motion_event
507 static BOOL is_old_motion_event( unsigned long serial )
509 struct x11drv_thread_data *thread_data = x11drv_thread_data();
511 if (!thread_data->warp_serial) return FALSE;
512 if ((long)(serial - thread_data->warp_serial) < 0) return TRUE;
513 thread_data->warp_serial = 0; /* we caught up now */
514 return FALSE;
518 /***********************************************************************
519 * send_mouse_input
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;
526 POINT pt;
528 input->type = INPUT_MOUSE;
530 if (!hwnd)
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 );
546 return;
549 if (window != root_window)
551 pt.x = input->u.mi.dx;
552 pt.y = input->u.mi.dy;
554 else pt = root_to_virtual_screen( input->u.mi.dx, input->u.mi.dy );
556 if (!(data = get_win_data( hwnd ))) return;
558 if (window == data->whole_window)
560 pt.x += data->whole_rect.left - data->client_rect.left;
561 pt.y += data->whole_rect.top - data->client_rect.top;
564 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
565 pt.x = data->client_rect.right - data->client_rect.left - 1 - pt.x;
566 MapWindowPoints( hwnd, 0, &pt, 1 );
568 if (InterlockedExchangePointer( (void **)&cursor_window, hwnd ) != hwnd ||
569 input->u.mi.time - last_cursor_change > 100)
571 sync_window_cursor( data->whole_window );
572 last_cursor_change = input->u.mi.time;
574 release_win_data( data );
576 if (hwnd != GetDesktopWindow())
578 hwnd = GetAncestor( hwnd, GA_ROOT );
579 if ((input->u.mi.dwFlags & (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_RIGHTDOWN)) && hwnd == GetForegroundWindow())
580 clip_fullscreen_window( hwnd, FALSE );
583 /* update the wine server Z-order */
585 if (hwnd != x11drv_thread_data()->grab_hwnd &&
586 /* ignore event if a button is pressed, since the mouse is then grabbed too */
587 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
589 RECT rect;
590 SetRect( &rect, pt.x, pt.y, pt.x + 1, pt.y + 1 );
591 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
593 SERVER_START_REQ( update_window_zorder )
595 req->window = wine_server_user_handle( hwnd );
596 req->rect.left = rect.left;
597 req->rect.top = rect.top;
598 req->rect.right = rect.right;
599 req->rect.bottom = rect.bottom;
600 wine_server_call( req );
602 SERVER_END_REQ;
605 input->u.mi.dx = pt.x;
606 input->u.mi.dy = pt.y;
607 __wine_send_input( hwnd, input );
610 #ifdef SONAME_LIBXCURSOR
612 /***********************************************************************
613 * create_xcursor_frame
615 * Use Xcursor to create a frame of an X cursor from a Windows one.
617 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
618 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
619 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
620 int width, int height, int istep )
622 XcursorImage *image, *ret = NULL;
623 DWORD delay_jiffies, num_steps;
624 int x, y, i;
625 BOOL has_alpha = FALSE;
626 XcursorPixel *ptr;
628 image = pXcursorImageCreate( width, height );
629 if (!image)
631 ERR("X11 failed to produce a cursor frame!\n");
632 return NULL;
635 image->xhot = iinfo->xHotspot;
636 image->yhot = iinfo->yHotspot;
638 image->delay = 100; /* fallback delay, 100 ms */
639 if (GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, istep, &delay_jiffies, &num_steps) != 0)
640 image->delay = (100 * delay_jiffies) / 6; /* convert jiffies (1/60s) to milliseconds */
641 else
642 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep);
644 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
645 memset( color_bits, 0x00, color_size );
646 SelectObject( hdc, hbmColor );
647 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
649 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
650 goto cleanup;
652 memcpy( image->pixels, color_bits, color_size );
654 /* check if the cursor frame was drawn with an alpha channel */
655 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
656 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
658 /* if no alpha channel was drawn then generate it from the mask */
659 if (!has_alpha)
661 unsigned int width_bytes = (width + 31) / 32 * 4;
663 /* draw the cursor mask to a temporary buffer */
664 memset( mask_bits, 0xFF, mask_size );
665 SelectObject( hdc, hbmMask );
666 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
668 ERR("Failed to draw frame mask %d.\n", istep);
669 goto cleanup;
671 /* use the buffer to directly modify the XcursorImage alpha channel */
672 for (y = 0, ptr = image->pixels; y < height; y++)
673 for (x = 0; x < width; x++, ptr++)
674 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
675 *ptr |= 0xff000000;
677 ret = image;
679 cleanup:
680 if (ret == NULL) pXcursorImageDestroy( image );
681 return ret;
684 /***********************************************************************
685 * create_xcursor_cursor
687 * Use Xcursor to create an X cursor from a Windows one.
689 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
691 unsigned char *color_bits, *mask_bits;
692 HBITMAP hbmColor = 0, hbmMask = 0;
693 DWORD nFrames, delay_jiffies, i;
694 int color_size, mask_size;
695 BITMAPINFO *info = NULL;
696 XcursorImages *images;
697 XcursorImage **imgs;
698 Cursor cursor = 0;
700 /* Retrieve the number of frames to render */
701 if (!GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, 0, &delay_jiffies, &nFrames)) return 0;
702 if (!(imgs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XcursorImage*)*nFrames ))) return 0;
704 /* Allocate all of the resources necessary to obtain a cursor frame */
705 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
706 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
707 info->bmiHeader.biWidth = width;
708 info->bmiHeader.biHeight = -height;
709 info->bmiHeader.biPlanes = 1;
710 info->bmiHeader.biCompression = BI_RGB;
711 info->bmiHeader.biXPelsPerMeter = 0;
712 info->bmiHeader.biYPelsPerMeter = 0;
713 info->bmiHeader.biClrUsed = 0;
714 info->bmiHeader.biClrImportant = 0;
715 info->bmiHeader.biBitCount = 32;
716 color_size = width * height * 4;
717 info->bmiHeader.biSizeImage = color_size;
718 hbmColor = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &color_bits, NULL, 0);
719 if (!hbmColor)
721 ERR("Failed to create DIB section for cursor color data!\n");
722 goto cleanup;
724 info->bmiHeader.biBitCount = 1;
725 info->bmiColors[0].rgbRed = 0;
726 info->bmiColors[0].rgbGreen = 0;
727 info->bmiColors[0].rgbBlue = 0;
728 info->bmiColors[0].rgbReserved = 0;
729 info->bmiColors[1].rgbRed = 0xff;
730 info->bmiColors[1].rgbGreen = 0xff;
731 info->bmiColors[1].rgbBlue = 0xff;
732 info->bmiColors[1].rgbReserved = 0;
734 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
735 info->bmiHeader.biSizeImage = mask_size;
736 hbmMask = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &mask_bits, NULL, 0);
737 if (!hbmMask)
739 ERR("Failed to create DIB section for cursor mask data!\n");
740 goto cleanup;
743 /* Create an XcursorImage for each frame of the cursor */
744 for (i=0; i<nFrames; i++)
746 imgs[i] = create_xcursor_frame( hdc, iinfo, icon,
747 hbmColor, color_bits, color_size,
748 hbmMask, mask_bits, mask_size,
749 width, height, i );
750 if (!imgs[i]) goto cleanup;
753 /* Build an X cursor out of all of the frames */
754 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
755 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
756 images->images[images->nimage] = imgs[images->nimage];
757 cursor = pXcursorImagesLoadCursor( gdi_display, images );
758 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
759 HeapFree( GetProcessHeap(), 0, imgs );
760 imgs = NULL;
762 cleanup:
763 if (imgs)
765 /* Failed to produce a cursor, free previously allocated frames */
766 for (i=0; i<nFrames && imgs[i]; i++)
767 pXcursorImageDestroy( imgs[i] );
768 HeapFree( GetProcessHeap(), 0, imgs );
770 /* Cleanup all of the resources used to obtain the frame data */
771 if (hbmColor) DeleteObject( hbmColor );
772 if (hbmMask) DeleteObject( hbmMask );
773 HeapFree( GetProcessHeap(), 0, info );
774 return cursor;
777 #endif /* SONAME_LIBXCURSOR */
780 struct system_cursors
782 WORD id;
783 const char *name;
786 static const struct system_cursors user32_cursors[] =
788 { OCR_NORMAL, "left_ptr" },
789 { OCR_IBEAM, "xterm" },
790 { OCR_WAIT, "watch" },
791 { OCR_CROSS, "cross" },
792 { OCR_UP, "center_ptr" },
793 { OCR_SIZE, "fleur" },
794 { OCR_SIZEALL, "fleur" },
795 { OCR_ICON, "icon" },
796 { OCR_SIZENWSE, "nwse-resize" },
797 { OCR_SIZENESW, "nesw-resize" },
798 { OCR_SIZEWE, "ew-resize" },
799 { OCR_SIZENS, "ns-resize" },
800 { OCR_NO, "not-allowed" },
801 { OCR_HAND, "hand2" },
802 { OCR_APPSTARTING, "left_ptr_watch" },
803 { OCR_HELP, "question_arrow" },
804 { 0 }
807 static const struct system_cursors comctl32_cursors[] =
809 { 102, "move" },
810 { 104, "copy" },
811 { 105, "left_ptr" },
812 { 106, "col-resize" },
813 { 107, "col-resize" },
814 { 108, "hand2" },
815 { 135, "row-resize" },
816 { 0 }
819 static const struct system_cursors ole32_cursors[] =
821 { 1, "no-drop" },
822 { 2, "move" },
823 { 3, "copy" },
824 { 4, "alias" },
825 { 0 }
828 static const struct system_cursors riched20_cursors[] =
830 { 105, "hand2" },
831 { 107, "right_ptr" },
832 { 109, "copy" },
833 { 110, "move" },
834 { 111, "no-drop" },
835 { 0 }
838 static const struct
840 const struct system_cursors *cursors;
841 WCHAR name[16];
842 } module_cursors[] =
844 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
845 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
846 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
847 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
850 struct cursor_font_fallback
852 const char *name;
853 unsigned int shape;
856 static const struct cursor_font_fallback fallbacks[] =
858 { "X_cursor", XC_X_cursor },
859 { "arrow", XC_arrow },
860 { "based_arrow_down", XC_based_arrow_down },
861 { "based_arrow_up", XC_based_arrow_up },
862 { "boat", XC_boat },
863 { "bogosity", XC_bogosity },
864 { "bottom_left_corner", XC_bottom_left_corner },
865 { "bottom_right_corner", XC_bottom_right_corner },
866 { "bottom_side", XC_bottom_side },
867 { "bottom_tee", XC_bottom_tee },
868 { "box_spiral", XC_box_spiral },
869 { "center_ptr", XC_center_ptr },
870 { "circle", XC_circle },
871 { "clock", XC_clock },
872 { "coffee_mug", XC_coffee_mug },
873 { "col-resize", XC_sb_h_double_arrow },
874 { "cross", XC_cross },
875 { "cross_reverse", XC_cross_reverse },
876 { "crosshair", XC_crosshair },
877 { "diamond_cross", XC_diamond_cross },
878 { "dot", XC_dot },
879 { "dotbox", XC_dotbox },
880 { "double_arrow", XC_double_arrow },
881 { "draft_large", XC_draft_large },
882 { "draft_small", XC_draft_small },
883 { "draped_box", XC_draped_box },
884 { "exchange", XC_exchange },
885 { "fleur", XC_fleur },
886 { "gobbler", XC_gobbler },
887 { "gumby", XC_gumby },
888 { "hand1", XC_hand1 },
889 { "hand2", XC_hand2 },
890 { "heart", XC_heart },
891 { "icon", XC_icon },
892 { "iron_cross", XC_iron_cross },
893 { "left_ptr", XC_left_ptr },
894 { "left_side", XC_left_side },
895 { "left_tee", XC_left_tee },
896 { "leftbutton", XC_leftbutton },
897 { "ll_angle", XC_ll_angle },
898 { "lr_angle", XC_lr_angle },
899 { "man", XC_man },
900 { "middlebutton", XC_middlebutton },
901 { "mouse", XC_mouse },
902 { "pencil", XC_pencil },
903 { "pirate", XC_pirate },
904 { "plus", XC_plus },
905 { "question_arrow", XC_question_arrow },
906 { "right_ptr", XC_right_ptr },
907 { "right_side", XC_right_side },
908 { "right_tee", XC_right_tee },
909 { "rightbutton", XC_rightbutton },
910 { "row-resize", XC_sb_v_double_arrow },
911 { "rtl_logo", XC_rtl_logo },
912 { "sailboat", XC_sailboat },
913 { "sb_down_arrow", XC_sb_down_arrow },
914 { "sb_h_double_arrow", XC_sb_h_double_arrow },
915 { "sb_left_arrow", XC_sb_left_arrow },
916 { "sb_right_arrow", XC_sb_right_arrow },
917 { "sb_up_arrow", XC_sb_up_arrow },
918 { "sb_v_double_arrow", XC_sb_v_double_arrow },
919 { "shuttle", XC_shuttle },
920 { "sizing", XC_sizing },
921 { "spider", XC_spider },
922 { "spraycan", XC_spraycan },
923 { "star", XC_star },
924 { "target", XC_target },
925 { "tcross", XC_tcross },
926 { "top_left_arrow", XC_top_left_arrow },
927 { "top_left_corner", XC_top_left_corner },
928 { "top_right_corner", XC_top_right_corner },
929 { "top_side", XC_top_side },
930 { "top_tee", XC_top_tee },
931 { "trek", XC_trek },
932 { "ul_angle", XC_ul_angle },
933 { "umbrella", XC_umbrella },
934 { "ur_angle", XC_ur_angle },
935 { "watch", XC_watch },
936 { "xterm", XC_xterm }
939 static int fallback_cmp( const void *key, const void *member )
941 const struct cursor_font_fallback *fallback = member;
942 return strcmp( key, fallback->name );
945 static int find_fallback_shape( const char *name )
947 struct cursor_font_fallback *fallback;
949 if ((fallback = bsearch( name, fallbacks, sizeof(fallbacks) / sizeof(fallbacks[0]),
950 sizeof(*fallback), fallback_cmp )))
951 return fallback->shape;
952 return -1;
955 /***********************************************************************
956 * create_xcursor_system_cursor
958 * Create an X cursor for a system cursor.
960 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
962 static const WCHAR idW[] = {'%','h','u',0};
963 const struct system_cursors *cursors;
964 unsigned int i;
965 Cursor cursor = 0;
966 HMODULE module;
967 HKEY key;
968 WCHAR *p, name[MAX_PATH * 2], valueW[64];
969 char valueA[64];
970 DWORD size, ret;
972 if (!info->szModName[0]) return 0;
974 p = strrchrW( info->szModName, '\\' );
975 strcpyW( name, p ? p + 1 : info->szModName );
976 p = name + strlenW( name );
977 *p++ = ',';
978 if (info->szResName[0]) strcpyW( p, info->szResName );
979 else sprintfW( p, idW, info->wResID );
980 valueA[0] = 0;
982 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
983 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver\\Cursors", &key ))
985 size = sizeof(valueW) / sizeof(WCHAR);
986 ret = RegQueryValueExW( key, name, NULL, NULL, (BYTE *)valueW, &size );
987 RegCloseKey( key );
988 if (!ret)
990 if (!valueW[0]) return 0; /* force standard cursor */
991 if (!WideCharToMultiByte( CP_UNIXCP, 0, valueW, -1, valueA, sizeof(valueA), NULL, NULL ))
992 valueA[0] = 0;
993 goto done;
997 if (info->szResName[0]) goto done; /* only integer resources are supported here */
998 if (!(module = GetModuleHandleW( info->szModName ))) goto done;
1000 for (i = 0; i < sizeof(module_cursors)/sizeof(module_cursors[0]); i++)
1001 if (GetModuleHandleW( module_cursors[i].name ) == module) break;
1002 if (i == sizeof(module_cursors)/sizeof(module_cursors[0])) goto done;
1004 cursors = module_cursors[i].cursors;
1005 for (i = 0; cursors[i].id; i++)
1006 if (cursors[i].id == info->wResID)
1008 strcpy( valueA, cursors[i].name );
1009 break;
1012 done:
1013 if (valueA[0])
1015 #ifdef SONAME_LIBXCURSOR
1016 if (pXcursorLibraryLoadCursor) cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
1017 #endif
1018 if (!cursor)
1020 int shape = find_fallback_shape( valueA );
1021 if (shape != -1) cursor = XCreateFontCursor( gdi_display, shape );
1023 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
1024 debugstr_w(name), debugstr_a(valueA) );
1026 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
1027 return cursor;
1031 /***********************************************************************
1032 * create_xlib_monochrome_cursor
1034 * Create a monochrome X cursor from a Windows one.
1036 static Cursor create_xlib_monochrome_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1038 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1039 BITMAPINFO *info = (BITMAPINFO *)buffer;
1040 const int and_y = 0;
1041 const int xor_y = height;
1042 unsigned int width_bytes = (width + 31) / 32 * 4;
1043 unsigned char *mask_bits = NULL;
1044 GC gc;
1045 XColor fg, bg;
1046 XVisualInfo vis = default_visual;
1047 Pixmap src_pixmap, bits_pixmap, mask_pixmap;
1048 struct gdi_image_bits bits;
1049 Cursor cursor = 0;
1051 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1052 info->bmiHeader.biWidth = width;
1053 info->bmiHeader.biHeight = -height * 2;
1054 info->bmiHeader.biPlanes = 1;
1055 info->bmiHeader.biBitCount = 1;
1056 info->bmiHeader.biCompression = BI_RGB;
1057 info->bmiHeader.biSizeImage = width_bytes * height * 2;
1058 info->bmiHeader.biXPelsPerMeter = 0;
1059 info->bmiHeader.biYPelsPerMeter = 0;
1060 info->bmiHeader.biClrUsed = 0;
1061 info->bmiHeader.biClrImportant = 0;
1063 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1064 if (!GetDIBits( hdc, icon->hbmMask, 0, height * 2, mask_bits, info, DIB_RGB_COLORS )) goto done;
1066 vis.depth = 1;
1067 bits.ptr = mask_bits;
1068 bits.free = NULL;
1069 bits.is_copy = TRUE;
1070 if (!(src_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1072 bits_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
1073 mask_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
1074 gc = XCreateGC( gdi_display, src_pixmap, 0, NULL );
1075 XSetGraphicsExposures( gdi_display, gc, False );
1077 /* We have to do some magic here, as cursors are not fully
1078 * compatible between Windows and X11. Under X11, there are
1079 * only 3 possible color cursor: black, white and masked. So
1080 * we map the 4th Windows color (invert the bits on the screen)
1081 * to black and an additional white bit on another place
1082 * (+1,+1). This require some boolean arithmetic:
1084 * Windows | X11
1085 * And Xor Result | Bits Mask Result
1086 * 0 0 black | 0 1 background
1087 * 0 1 white | 1 1 foreground
1088 * 1 0 no change | X 0 no change
1089 * 1 1 inverted | 0 1 background
1091 * which gives:
1092 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
1093 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
1095 XSetFunction( gdi_display, gc, GXcopy );
1096 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 0, 0 );
1097 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 0, 0 );
1098 XSetFunction( gdi_display, gc, GXandReverse );
1099 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, xor_y, width, height, 0, 0 );
1100 XSetFunction( gdi_display, gc, GXorReverse );
1101 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, xor_y, width, height, 0, 0 );
1102 /* additional white */
1103 XSetFunction( gdi_display, gc, GXand );
1104 XCopyArea( gdi_display, src_pixmap, src_pixmap, gc, 0, xor_y, width, height, 0, and_y );
1105 XSetFunction( gdi_display, gc, GXor );
1106 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 1, 1 );
1107 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 1, 1 );
1108 XFreeGC( gdi_display, gc );
1110 fg.red = fg.green = fg.blue = 0xffff;
1111 bg.red = bg.green = bg.blue = 0;
1112 cursor = XCreatePixmapCursor( gdi_display, bits_pixmap, mask_pixmap,
1113 &fg, &bg, icon->xHotspot, icon->yHotspot );
1114 XFreePixmap( gdi_display, src_pixmap );
1115 XFreePixmap( gdi_display, bits_pixmap );
1116 XFreePixmap( gdi_display, mask_pixmap );
1118 done:
1119 HeapFree( GetProcessHeap(), 0, mask_bits );
1120 return cursor;
1123 /***********************************************************************
1124 * create_xlib_color_cursor
1126 * Create a color X cursor from a Windows one.
1128 static Cursor create_xlib_color_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1130 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1131 BITMAPINFO *info = (BITMAPINFO *)buffer;
1132 XColor fg, bg;
1133 Cursor cursor = None;
1134 XVisualInfo vis = default_visual;
1135 Pixmap xor_pixmap, mask_pixmap;
1136 struct gdi_image_bits bits;
1137 unsigned int *color_bits = NULL, *ptr;
1138 unsigned char *mask_bits = NULL, *xor_bits = NULL;
1139 int i, x, y;
1140 BOOL has_alpha = FALSE;
1141 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
1142 unsigned int width_bytes = (width + 31) / 32 * 4;
1144 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1145 info->bmiHeader.biWidth = width;
1146 info->bmiHeader.biHeight = -height;
1147 info->bmiHeader.biPlanes = 1;
1148 info->bmiHeader.biBitCount = 1;
1149 info->bmiHeader.biCompression = BI_RGB;
1150 info->bmiHeader.biSizeImage = width_bytes * height;
1151 info->bmiHeader.biXPelsPerMeter = 0;
1152 info->bmiHeader.biYPelsPerMeter = 0;
1153 info->bmiHeader.biClrUsed = 0;
1154 info->bmiHeader.biClrImportant = 0;
1156 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1157 if (!GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS )) goto done;
1159 info->bmiHeader.biBitCount = 32;
1160 info->bmiHeader.biSizeImage = width * height * 4;
1161 if (!(color_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1162 if (!(xor_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, width_bytes * height ))) goto done;
1163 GetDIBits( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS );
1165 /* compute fg/bg color and xor bitmap based on average of the color values */
1167 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
1168 for (y = 0, ptr = color_bits; y < height; y++)
1170 for (x = 0; x < width; x++, ptr++)
1172 int red = (*ptr >> 16) & 0xff;
1173 int green = (*ptr >> 8) & 0xff;
1174 int blue = (*ptr >> 0) & 0xff;
1175 if (red + green + blue > 0x40)
1177 rfg += red;
1178 gfg += green;
1179 bfg += blue;
1180 fgBits++;
1181 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1183 else
1185 rbg += red;
1186 gbg += green;
1187 bbg += blue;
1191 if (fgBits)
1193 fg.red = rfg * 257 / fgBits;
1194 fg.green = gfg * 257 / fgBits;
1195 fg.blue = bfg * 257 / fgBits;
1197 else fg.red = fg.green = fg.blue = 0;
1198 bgBits = width * height - fgBits;
1199 if (bgBits)
1201 bg.red = rbg * 257 / bgBits;
1202 bg.green = gbg * 257 / bgBits;
1203 bg.blue = bbg * 257 / bgBits;
1205 else bg.red = bg.green = bg.blue = 0;
1207 info->bmiHeader.biBitCount = 1;
1208 info->bmiHeader.biClrUsed = 0;
1209 info->bmiHeader.biSizeImage = width_bytes * height;
1211 /* generate mask from the alpha channel if we have one */
1213 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
1214 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
1216 if (has_alpha)
1218 memset( mask_bits, 0, width_bytes * height );
1219 for (y = 0, ptr = color_bits; y < height; y++)
1220 for (x = 0; x < width; x++, ptr++)
1221 if ((*ptr >> 24) > 25) /* more than 10% alpha */
1222 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1224 else /* invert the mask */
1226 unsigned int j;
1228 ptr = (unsigned int *)mask_bits;
1229 for (j = 0; j < info->bmiHeader.biSizeImage / sizeof(*ptr); j++, ptr++) *ptr ^= ~0u;
1232 vis.depth = 1;
1233 bits.ptr = xor_bits;
1234 bits.free = NULL;
1235 bits.is_copy = TRUE;
1236 if (!(xor_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1238 bits.ptr = mask_bits;
1239 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
1241 if (mask_pixmap)
1243 cursor = XCreatePixmapCursor( gdi_display, xor_pixmap, mask_pixmap,
1244 &fg, &bg, icon->xHotspot, icon->yHotspot );
1245 XFreePixmap( gdi_display, mask_pixmap );
1247 XFreePixmap( gdi_display, xor_pixmap );
1249 done:
1250 HeapFree( GetProcessHeap(), 0, color_bits );
1251 HeapFree( GetProcessHeap(), 0, xor_bits );
1252 HeapFree( GetProcessHeap(), 0, mask_bits );
1253 return cursor;
1256 /***********************************************************************
1257 * create_cursor
1259 * Create an X cursor from a Windows one.
1261 static Cursor create_cursor( HANDLE handle )
1263 Cursor cursor = 0;
1264 ICONINFOEXW info;
1265 BITMAP bm;
1266 HDC hdc;
1268 if (!handle) return get_empty_cursor();
1270 info.cbSize = sizeof(info);
1271 if (!GetIconInfoExW( handle, &info )) return 0;
1273 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
1275 DeleteObject( info.hbmColor );
1276 DeleteObject( info.hbmMask );
1277 return cursor;
1280 GetObjectW( info.hbmMask, sizeof(bm), &bm );
1281 if (!info.hbmColor) bm.bmHeight = max( 1, bm.bmHeight / 2 );
1283 /* make sure hotspot is valid */
1284 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1286 info.xHotspot = bm.bmWidth / 2;
1287 info.yHotspot = bm.bmHeight / 2;
1290 hdc = CreateCompatibleDC( 0 );
1292 if (info.hbmColor)
1294 #ifdef SONAME_LIBXCURSOR
1295 if (pXcursorImagesLoadCursor)
1296 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
1297 #endif
1298 if (!cursor) cursor = create_xlib_color_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1299 DeleteObject( info.hbmColor );
1301 else
1303 cursor = create_xlib_monochrome_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1306 DeleteObject( info.hbmMask );
1307 DeleteDC( hdc );
1308 return cursor;
1311 /***********************************************************************
1312 * DestroyCursorIcon (X11DRV.@)
1314 void CDECL X11DRV_DestroyCursorIcon( HCURSOR handle )
1316 Cursor cursor;
1318 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1320 TRACE( "%p xid %lx\n", handle, cursor );
1321 XFreeCursor( gdi_display, cursor );
1322 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1326 /***********************************************************************
1327 * SetCursor (X11DRV.@)
1329 void CDECL X11DRV_SetCursor( HCURSOR handle )
1331 if (InterlockedExchangePointer( (void **)&last_cursor, handle ) != handle ||
1332 GetTickCount() - last_cursor_change > 100)
1334 last_cursor_change = GetTickCount();
1335 if (cursor_window) SendNotifyMessageW( cursor_window, WM_X11DRV_SET_CURSOR, 0, (LPARAM)handle );
1339 /***********************************************************************
1340 * SetCursorPos (X11DRV.@)
1342 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
1344 struct x11drv_thread_data *data = x11drv_init_thread_data();
1345 POINT pos = virtual_screen_to_root( x, y );
1347 XWarpPointer( data->display, root_window, root_window, 0, 0, 0, 0, pos.x, pos.y );
1348 data->warp_serial = NextRequest( data->display );
1349 XNoOp( data->display );
1350 XFlush( data->display ); /* avoids bad mouse lag in games that do their own mouse warping */
1351 TRACE( "warped to %d,%d serial %lu\n", x, y, data->warp_serial );
1352 return TRUE;
1355 /***********************************************************************
1356 * GetCursorPos (X11DRV.@)
1358 BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
1360 Display *display = thread_init_display();
1361 Window root, child;
1362 int rootX, rootY, winX, winY;
1363 unsigned int xstate;
1364 BOOL ret;
1366 ret = XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &winX, &winY, &xstate );
1367 if (ret)
1369 POINT old = *pos;
1370 *pos = root_to_virtual_screen( winX, winY );
1371 TRACE( "pointer at %s server pos %s\n", wine_dbgstr_point(pos), wine_dbgstr_point(&old) );
1373 return ret;
1376 /***********************************************************************
1377 * ClipCursor (X11DRV.@)
1379 BOOL CDECL X11DRV_ClipCursor( LPCRECT clip )
1381 RECT virtual_rect = get_virtual_screen_rect();
1383 if (!clip) clip = &virtual_rect;
1385 if (grab_pointer)
1387 HWND foreground = GetForegroundWindow();
1389 /* we are clipping if the clip rectangle is smaller than the screen */
1390 if (clip->left > virtual_rect.left || clip->right < virtual_rect.right ||
1391 clip->top > virtual_rect.top || clip->bottom < virtual_rect.bottom)
1393 DWORD tid, pid;
1395 /* forward request to the foreground window if it's in a different thread */
1396 tid = GetWindowThreadProcessId( foreground, &pid );
1397 if (tid && tid != GetCurrentThreadId() && pid == GetCurrentProcessId())
1399 TRACE( "forwarding clip request to %p\n", foreground );
1400 SendNotifyMessageW( foreground, WM_X11DRV_CLIP_CURSOR, 0, 0 );
1401 return TRUE;
1403 else if (grab_clipping_window( clip )) return TRUE;
1405 else /* if currently clipping, check if we should switch to fullscreen clipping */
1407 struct x11drv_thread_data *data = x11drv_thread_data();
1408 if (data && data->clip_hwnd)
1410 if (EqualRect( clip, &clip_rect ) || clip_fullscreen_window( foreground, TRUE ))
1411 return TRUE;
1415 ungrab_clipping_window();
1416 return TRUE;
1419 /***********************************************************************
1420 * move_resize_window
1422 void move_resize_window( HWND hwnd, int dir )
1424 Display *display = thread_display();
1425 DWORD pt;
1426 POINT pos;
1427 int button = 0;
1428 XEvent xev;
1429 Window win, root, child;
1430 unsigned int xstate;
1432 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1434 pt = GetMessagePos();
1435 pos = virtual_screen_to_root( (short)LOWORD( pt ), (short)HIWORD( pt ) );
1437 if (GetKeyState( VK_LBUTTON ) & 0x8000) button = 1;
1438 else if (GetKeyState( VK_MBUTTON ) & 0x8000) button = 2;
1439 else if (GetKeyState( VK_RBUTTON ) & 0x8000) button = 3;
1441 TRACE( "hwnd %p/%lx, pos %s, dir %d, button %d\n", hwnd, win, wine_dbgstr_point(&pos), dir, button );
1443 xev.xclient.type = ClientMessage;
1444 xev.xclient.window = win;
1445 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1446 xev.xclient.serial = 0;
1447 xev.xclient.display = display;
1448 xev.xclient.send_event = True;
1449 xev.xclient.format = 32;
1450 xev.xclient.data.l[0] = pos.x; /* x coord */
1451 xev.xclient.data.l[1] = pos.y; /* y coord */
1452 xev.xclient.data.l[2] = dir; /* direction */
1453 xev.xclient.data.l[3] = button; /* button */
1454 xev.xclient.data.l[4] = 0; /* unused */
1456 /* need to ungrab the pointer that may have been automatically grabbed
1457 * with a ButtonPress event */
1458 XUngrabPointer( display, CurrentTime );
1459 XSendEvent(display, root_window, False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
1461 /* try to detect the end of the size/move by polling for the mouse button to be released */
1462 /* (some apps don't like it if we return before the size/move is done) */
1464 if (!button) return;
1465 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1467 for (;;)
1469 MSG msg;
1470 INPUT input;
1471 int x, y, rootX, rootY;
1473 if (!XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &x, &y, &xstate )) break;
1475 if (!(xstate & (Button1Mask << (button - 1))))
1477 /* fake a button release event */
1478 pos = root_to_virtual_screen( x, y );
1479 input.type = INPUT_MOUSE;
1480 input.u.mi.dx = pos.x;
1481 input.u.mi.dy = pos.y;
1482 input.u.mi.mouseData = button_up_data[button - 1];
1483 input.u.mi.dwFlags = button_up_flags[button - 1] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1484 input.u.mi.time = GetTickCount();
1485 input.u.mi.dwExtraInfo = 0;
1486 __wine_send_input( hwnd, &input );
1489 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
1491 if (!CallMsgFilterW( &msg, MSGF_SIZE ))
1493 TranslateMessage( &msg );
1494 DispatchMessageW( &msg );
1498 if (!(xstate & (Button1Mask << (button - 1)))) break;
1499 MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT );
1502 TRACE( "hwnd %p/%lx done\n", hwnd, win );
1503 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1507 /***********************************************************************
1508 * X11DRV_ButtonPress
1510 BOOL X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1512 XButtonEvent *event = &xev->xbutton;
1513 int buttonNum = event->button - 1;
1514 INPUT input;
1516 if (buttonNum >= NB_BUTTONS) return FALSE;
1518 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1520 input.u.mi.dx = event->x;
1521 input.u.mi.dy = event->y;
1522 input.u.mi.mouseData = button_down_data[buttonNum];
1523 input.u.mi.dwFlags = button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1524 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1525 input.u.mi.dwExtraInfo = 0;
1527 update_user_time( event->time );
1528 send_mouse_input( hwnd, event->window, event->state, &input );
1529 return TRUE;
1533 /***********************************************************************
1534 * X11DRV_ButtonRelease
1536 BOOL X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1538 XButtonEvent *event = &xev->xbutton;
1539 int buttonNum = event->button - 1;
1540 INPUT input;
1542 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return FALSE;
1544 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1546 input.u.mi.dx = event->x;
1547 input.u.mi.dy = event->y;
1548 input.u.mi.mouseData = button_up_data[buttonNum];
1549 input.u.mi.dwFlags = button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1550 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1551 input.u.mi.dwExtraInfo = 0;
1553 send_mouse_input( hwnd, event->window, event->state, &input );
1554 return TRUE;
1558 /***********************************************************************
1559 * X11DRV_MotionNotify
1561 BOOL X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1563 XMotionEvent *event = &xev->xmotion;
1564 INPUT input;
1566 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d serial %lu\n",
1567 hwnd, event->window, event->x, event->y, event->is_hint, event->serial );
1569 input.u.mi.dx = event->x;
1570 input.u.mi.dy = event->y;
1571 input.u.mi.mouseData = 0;
1572 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1573 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1574 input.u.mi.dwExtraInfo = 0;
1576 if (!hwnd && is_old_motion_event( event->serial ))
1578 TRACE( "pos %d,%d old serial %lu, ignoring\n", input.u.mi.dx, input.u.mi.dy, event->serial );
1579 return FALSE;
1581 send_mouse_input( hwnd, event->window, event->state, &input );
1582 return TRUE;
1586 /***********************************************************************
1587 * X11DRV_EnterNotify
1589 BOOL X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1591 XCrossingEvent *event = &xev->xcrossing;
1592 INPUT input;
1594 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd, event->window, event->x, event->y, event->detail );
1596 if (event->detail == NotifyVirtual) return FALSE;
1597 if (hwnd == x11drv_thread_data()->grab_hwnd) return FALSE;
1599 /* simulate a mouse motion event */
1600 input.u.mi.dx = event->x;
1601 input.u.mi.dy = event->y;
1602 input.u.mi.mouseData = 0;
1603 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1604 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1605 input.u.mi.dwExtraInfo = 0;
1607 if (is_old_motion_event( event->serial ))
1609 TRACE( "pos %d,%d old serial %lu, ignoring\n", input.u.mi.dx, input.u.mi.dy, event->serial );
1610 return FALSE;
1612 send_mouse_input( hwnd, event->window, event->state, &input );
1613 return TRUE;
1616 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1618 /***********************************************************************
1619 * X11DRV_RawMotion
1621 static BOOL X11DRV_RawMotion( XGenericEventCookie *xev )
1623 XIRawEvent *event = xev->data;
1624 const double *values = event->valuators.values;
1625 RECT virtual_rect;
1626 INPUT input;
1627 int i, j;
1628 double dx = 0, dy = 0;
1629 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1630 XIDeviceInfo *devices = thread_data->xi2_devices;
1632 if (!event->valuators.mask_len) return FALSE;
1633 if (thread_data->xi2_state != xi_enabled) return FALSE;
1635 input.u.mi.mouseData = 0;
1636 input.u.mi.dwFlags = MOUSEEVENTF_MOVE;
1637 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1638 input.u.mi.dwExtraInfo = 0;
1639 input.u.mi.dx = 0;
1640 input.u.mi.dy = 0;
1642 virtual_rect = get_virtual_screen_rect();
1643 for (i = 0; i < thread_data->xi2_device_count; ++i)
1645 if (devices[i].deviceid != event->deviceid) continue;
1646 for (j = 0; j < devices[i].num_classes; j++)
1648 XIValuatorClassInfo *class = (XIValuatorClassInfo *)devices[i].classes[j];
1650 if (devices[i].classes[j]->type != XIValuatorClass) continue;
1651 if (XIMaskIsSet( event->valuators.mask, class->number ))
1653 double val = *values++;
1654 if (class->label == x11drv_atom( Rel_X ) ||
1655 (!class->label && class->number == 0 && class->mode == XIModeRelative))
1657 input.u.mi.dx = dx = val;
1658 if (class->min < class->max)
1659 input.u.mi.dx = val * (virtual_rect.right - virtual_rect.left)
1660 / (class->max - class->min);
1662 else if (class->label == x11drv_atom( Rel_Y ) ||
1663 (!class->label && class->number == 1 && class->mode == XIModeRelative))
1665 input.u.mi.dy = dy = val;
1666 if (class->min < class->max)
1667 input.u.mi.dy = val * (virtual_rect.bottom - virtual_rect.top)
1668 / (class->max - class->min);
1672 break;
1675 if (broken_rawevents && is_old_motion_event( xev->serial ))
1677 TRACE( "pos %d,%d old serial %lu, ignoring\n", input.u.mi.dx, input.u.mi.dy, xev->serial );
1678 return FALSE;
1681 TRACE( "pos %d,%d (event %f,%f)\n", input.u.mi.dx, input.u.mi.dy, dx, dy );
1683 input.type = INPUT_MOUSE;
1684 __wine_send_input( 0, &input );
1685 return TRUE;
1688 #endif /* HAVE_X11_EXTENSIONS_XINPUT2_H */
1691 /***********************************************************************
1692 * X11DRV_XInput2_Init
1694 void X11DRV_XInput2_Init(void)
1696 #if defined(SONAME_LIBXI) && defined(HAVE_X11_EXTENSIONS_XINPUT2_H)
1697 int event, error;
1698 void *libxi_handle = wine_dlopen( SONAME_LIBXI, RTLD_NOW, NULL, 0 );
1700 if (!libxi_handle)
1702 WARN( "couldn't load %s\n", SONAME_LIBXI );
1703 return;
1705 #define LOAD_FUNCPTR(f) \
1706 if (!(p##f = wine_dlsym( libxi_handle, #f, NULL, 0))) \
1708 WARN("Failed to load %s.\n", #f); \
1709 return; \
1712 LOAD_FUNCPTR(XIGetClientPointer);
1713 LOAD_FUNCPTR(XIFreeDeviceInfo);
1714 LOAD_FUNCPTR(XIQueryDevice);
1715 LOAD_FUNCPTR(XIQueryVersion);
1716 LOAD_FUNCPTR(XISelectEvents);
1717 #undef LOAD_FUNCPTR
1719 xinput2_available = XQueryExtension( gdi_display, "XInputExtension", &xinput2_opcode, &event, &error );
1721 /* Until version 1.10.4 rawinput was broken in XOrg, see
1722 * https://bugs.freedesktop.org/show_bug.cgi?id=30068 */
1723 broken_rawevents = strstr(XServerVendor( gdi_display ), "X.Org") &&
1724 XVendorRelease( gdi_display ) < 11004000;
1726 #else
1727 TRACE( "X Input 2 support not compiled in.\n" );
1728 #endif
1732 /***********************************************************************
1733 * X11DRV_GenericEvent
1735 BOOL X11DRV_GenericEvent( HWND hwnd, XEvent *xev )
1737 BOOL ret = FALSE;
1738 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1739 XGenericEventCookie *event = &xev->xcookie;
1741 if (!event->data) return FALSE;
1742 if (event->extension != xinput2_opcode) return FALSE;
1744 switch (event->evtype)
1746 case XI_RawMotion:
1747 ret = X11DRV_RawMotion( event );
1748 break;
1750 default:
1751 TRACE( "Unhandled event %#x\n", event->evtype );
1752 break;
1754 #endif
1755 return ret;