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
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 /**********************************************************************/
63 #define Button6Mask (1<<13)
66 #define Button7Mask (1<<14)
69 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
71 static const UINT button_down_flags
[NB_BUTTONS
] =
74 MOUSEEVENTF_MIDDLEDOWN
,
75 MOUSEEVENTF_RIGHTDOWN
,
78 MOUSEEVENTF_XDOWN
, /* FIXME: horizontal wheel */
84 static const UINT button_up_flags
[NB_BUTTONS
] =
97 static const UINT button_down_data
[NB_BUTTONS
] =
110 static const UINT button_up_data
[NB_BUTTONS
] =
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
);
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");
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
);
168 #endif /* SONAME_LIBXCURSOR */
172 /***********************************************************************
175 static Cursor
get_empty_cursor(void)
177 static Cursor cursor
;
178 static const char data
[] = { 0 };
185 bg
.red
= bg
.green
= bg
.blue
= 0x0000;
186 pixmap
= XCreateBitmapFromData( gdi_display
, root_window
, data
, 1, 1 );
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
);
198 /***********************************************************************
201 void set_window_cursor( Window window
, HCURSOR handle
)
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
);
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 /***********************************************************************
234 void sync_window_cursor( Window window
)
238 SERVER_START_REQ( set_cursor
)
241 wine_server_call( req
);
242 cursor
= reply
->prev_count
>= 0 ? wine_server_ptr_handle( reply
->prev_handle
) : 0;
246 set_window_cursor( window
, cursor
);
249 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
250 /***********************************************************************
251 * update_relative_valuators
253 static void update_relative_valuators(XIAnyClassInfo
**valuators
, int n_valuators
)
255 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
258 thread_data
->x_rel_valuator
.number
= -1;
259 thread_data
->y_rel_valuator
.number
= -1;
261 for (i
= 0; i
< n_valuators
; i
++)
263 XIValuatorClassInfo
*class = (XIValuatorClassInfo
*)valuators
[i
];
264 struct x11drv_valuator_data
*valuator_data
= NULL
;
266 if (valuators
[i
]->type
!= XIValuatorClass
) continue;
267 if (class->label
== x11drv_atom( Rel_X
) ||
268 (!class->label
&& class->number
== 0 && class->mode
== XIModeRelative
))
270 valuator_data
= &thread_data
->x_rel_valuator
;
272 else if (class->label
== x11drv_atom( Rel_Y
) ||
273 (!class->label
&& class->number
== 1 && class->mode
== XIModeRelative
))
275 valuator_data
= &thread_data
->y_rel_valuator
;
279 valuator_data
->number
= class->number
;
280 valuator_data
->min
= class->min
;
281 valuator_data
->max
= class->max
;
288 /***********************************************************************
291 static void enable_xinput2(void)
293 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
294 struct x11drv_thread_data
*data
= x11drv_thread_data();
296 XIDeviceInfo
*pointer_info
;
297 unsigned char mask_bits
[XIMaskLen(XI_LASTEVENT
)];
300 if (!xinput2_available
) return;
302 if (data
->xi2_state
== xi_unknown
)
304 int major
= 2, minor
= 0;
305 if (!pXIQueryVersion( data
->display
, &major
, &minor
)) data
->xi2_state
= xi_disabled
;
308 data
->xi2_state
= xi_unavailable
;
309 WARN( "X Input 2 not available\n" );
312 if (data
->xi2_state
== xi_unavailable
) return;
313 if (!pXIGetClientPointer( data
->display
, None
, &data
->xi2_core_pointer
)) return;
315 mask
.mask
= mask_bits
;
316 mask
.mask_len
= sizeof(mask_bits
);
317 mask
.deviceid
= XIAllDevices
;
318 memset( mask_bits
, 0, sizeof(mask_bits
) );
319 XISetMask( mask_bits
, XI_DeviceChanged
);
320 XISetMask( mask_bits
, XI_RawMotion
);
321 XISetMask( mask_bits
, XI_ButtonPress
);
323 pXISelectEvents( data
->display
, DefaultRootWindow( data
->display
), &mask
, 1 );
325 pointer_info
= pXIQueryDevice( data
->display
, data
->xi2_core_pointer
, &count
);
326 update_relative_valuators( pointer_info
->classes
, pointer_info
->num_classes
);
327 pXIFreeDeviceInfo( pointer_info
);
329 /* This device info list is only used to find the initial current slave if
330 * no XI_DeviceChanged events happened. If any hierarchy change occurred that
331 * might be relevant here (eg. user switching mice after (un)plugging), a
332 * XI_DeviceChanged event will point us to the right slave. So this list is
333 * safe to be obtained statically at enable_xinput2() time.
335 if (data
->xi2_devices
) pXIFreeDeviceInfo( data
->xi2_devices
);
336 data
->xi2_devices
= pXIQueryDevice( data
->display
, XIAllDevices
, &data
->xi2_device_count
);
337 data
->xi2_current_slave
= 0;
339 data
->xi2_state
= xi_enabled
;
343 /***********************************************************************
346 static void disable_xinput2(void)
348 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
349 struct x11drv_thread_data
*data
= x11drv_thread_data();
352 if (data
->xi2_state
!= xi_enabled
) return;
354 TRACE( "disabling\n" );
355 data
->xi2_state
= xi_disabled
;
359 mask
.deviceid
= XIAllDevices
;
361 pXISelectEvents( data
->display
, DefaultRootWindow( data
->display
), &mask
, 1 );
362 pXIFreeDeviceInfo( data
->xi2_devices
);
363 data
->x_rel_valuator
.number
= -1;
364 data
->y_rel_valuator
.number
= -1;
365 data
->xi2_devices
= NULL
;
366 data
->xi2_core_pointer
= 0;
367 data
->xi2_current_slave
= 0;
372 /***********************************************************************
373 * grab_clipping_window
375 * Start a pointer grab on the clip window.
377 static BOOL
grab_clipping_window( const RECT
*clip
)
379 static const WCHAR messageW
[] = {'M','e','s','s','a','g','e',0};
380 struct x11drv_thread_data
*data
= x11drv_thread_data();
385 if (GetWindowThreadProcessId( GetDesktopWindow(), NULL
) == GetCurrentThreadId())
386 return TRUE
; /* don't clip in the desktop process */
388 if (!data
) return FALSE
;
389 if (!(clip_window
= init_clip_window())) return TRUE
;
391 if (!(msg_hwnd
= CreateWindowW( messageW
, NULL
, 0, 0, 0, 0, 0, HWND_MESSAGE
, 0,
392 GetModuleHandleW(0), NULL
)))
395 /* enable XInput2 unless we are already clipping */
396 if (!data
->clip_hwnd
) enable_xinput2();
398 if (data
->xi2_state
!= xi_enabled
)
400 WARN( "XInput2 not supported, refusing to clip to %s\n", wine_dbgstr_rect(clip
) );
401 DestroyWindow( msg_hwnd
);
406 TRACE( "clipping to %s win %lx\n", wine_dbgstr_rect(clip
), clip_window
);
408 if (!data
->clip_hwnd
) XUnmapWindow( data
->display
, clip_window
);
409 pos
= virtual_screen_to_root( clip
->left
, clip
->top
);
410 XMoveResizeWindow( data
->display
, clip_window
, pos
.x
, pos
.y
,
411 max( 1, clip
->right
- clip
->left
), max( 1, clip
->bottom
- clip
->top
) );
412 XMapWindow( data
->display
, clip_window
);
414 /* if the rectangle is shrinking we may get a pointer warp */
415 if (!data
->clip_hwnd
|| clip
->left
> clip_rect
.left
|| clip
->top
> clip_rect
.top
||
416 clip
->right
< clip_rect
.right
|| clip
->bottom
< clip_rect
.bottom
)
417 data
->warp_serial
= NextRequest( data
->display
);
419 if (!XGrabPointer( data
->display
, clip_window
, False
,
420 PointerMotionMask
| ButtonPressMask
| ButtonReleaseMask
,
421 GrabModeAsync
, GrabModeAsync
, clip_window
, None
, CurrentTime
))
422 clipping_cursor
= TRUE
;
424 if (!clipping_cursor
)
427 DestroyWindow( msg_hwnd
);
431 if (!data
->clip_hwnd
) sync_window_cursor( clip_window
);
432 InterlockedExchangePointer( (void **)&cursor_window
, msg_hwnd
);
433 data
->clip_hwnd
= msg_hwnd
;
434 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR
, 0, (LPARAM
)msg_hwnd
);
438 /***********************************************************************
439 * ungrab_clipping_window
441 * Release the pointer grab on the clip window.
443 void ungrab_clipping_window(void)
445 Display
*display
= thread_init_display();
446 Window clip_window
= init_clip_window();
448 if (!clip_window
) return;
450 TRACE( "no longer clipping\n" );
451 XUnmapWindow( display
, clip_window
);
452 clipping_cursor
= FALSE
;
453 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR
, 0, 0 );
456 /***********************************************************************
457 * reset_clipping_window
459 * Forcibly reset the window clipping on external events.
461 void reset_clipping_window(void)
463 ungrab_clipping_window();
464 ClipCursor( NULL
); /* make sure the clip rectangle is reset too */
467 /***********************************************************************
470 * Notification function called upon receiving a WM_X11DRV_CLIP_CURSOR.
472 LRESULT
clip_cursor_notify( HWND hwnd
, HWND new_clip_hwnd
)
474 struct x11drv_thread_data
*data
= x11drv_init_thread_data();
476 if (hwnd
== GetDesktopWindow()) /* change the clip window stored in the desktop process */
478 static HWND clip_hwnd
;
480 HWND prev
= clip_hwnd
;
481 clip_hwnd
= new_clip_hwnd
;
482 if (prev
|| new_clip_hwnd
) TRACE( "clip hwnd changed from %p to %p\n", prev
, new_clip_hwnd
);
483 if (prev
) SendNotifyMessageW( prev
, WM_X11DRV_CLIP_CURSOR
, 0, 0 );
485 else if (hwnd
== data
->clip_hwnd
) /* this is a notification that clipping has been reset */
487 TRACE( "clip hwnd reset from %p\n", hwnd
);
489 data
->clip_reset
= GetTickCount();
491 DestroyWindow( hwnd
);
493 else if (hwnd
== GetForegroundWindow()) /* request to clip */
495 RECT clip
, virtual_rect
= get_virtual_screen_rect();
497 GetClipCursor( &clip
);
498 if (clip
.left
> virtual_rect
.left
|| clip
.right
< virtual_rect
.right
||
499 clip
.top
> virtual_rect
.top
|| clip
.bottom
< virtual_rect
.bottom
)
500 return grab_clipping_window( &clip
);
505 /***********************************************************************
506 * clip_fullscreen_window
508 * Turn on clipping if the active window is fullscreen.
510 BOOL
clip_fullscreen_window( HWND hwnd
, BOOL reset
)
512 struct x11drv_win_data
*data
;
513 struct x11drv_thread_data
*thread_data
;
518 if (hwnd
== GetDesktopWindow()) return FALSE
;
519 style
= GetWindowLongW( hwnd
, GWL_STYLE
);
520 if (!(style
& WS_VISIBLE
)) return FALSE
;
521 if ((style
& (WS_POPUP
| WS_CHILD
)) == WS_CHILD
) return FALSE
;
522 /* maximized windows don't count as full screen */
523 if ((style
& WS_MAXIMIZE
) && (style
& WS_CAPTION
) == WS_CAPTION
) return FALSE
;
524 if (!(data
= get_win_data( hwnd
))) return FALSE
;
525 fullscreen
= is_window_rect_fullscreen( &data
->whole_rect
);
526 release_win_data( data
);
527 if (!fullscreen
) return FALSE
;
528 if (!(thread_data
= x11drv_thread_data())) return FALSE
;
529 if (GetTickCount() - thread_data
->clip_reset
< 1000) return FALSE
;
530 if (!reset
&& clipping_cursor
&& thread_data
->clip_hwnd
) return FALSE
; /* already clipping */
531 rect
= get_primary_monitor_rect();
532 if (!grab_fullscreen
)
534 RECT virtual_rect
= get_virtual_screen_rect();
535 if (!EqualRect( &rect
, &virtual_rect
)) return FALSE
;
536 if (root_window
!= DefaultRootWindow( gdi_display
)) return FALSE
;
538 TRACE( "win %p clipping fullscreen\n", hwnd
);
539 return grab_clipping_window( &rect
);
543 /***********************************************************************
544 * is_old_motion_event
546 static BOOL
is_old_motion_event( unsigned long serial
)
548 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
550 if (!thread_data
->warp_serial
) return FALSE
;
551 if ((long)(serial
- thread_data
->warp_serial
) < 0) return TRUE
;
552 thread_data
->warp_serial
= 0; /* we caught up now */
557 /***********************************************************************
560 * Update the various window states on a mouse event.
562 static void send_mouse_input( HWND hwnd
, Window window
, unsigned int state
, INPUT
*input
)
564 struct x11drv_win_data
*data
;
567 input
->type
= INPUT_MOUSE
;
571 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
572 HWND clip_hwnd
= thread_data
->clip_hwnd
;
574 if (!clip_hwnd
) return;
575 if (thread_data
->clip_window
!= window
) return;
576 if (InterlockedExchangePointer( (void **)&cursor_window
, clip_hwnd
) != clip_hwnd
||
577 input
->u
.mi
.time
- last_cursor_change
> 100)
579 sync_window_cursor( window
);
580 last_cursor_change
= input
->u
.mi
.time
;
582 input
->u
.mi
.dx
+= clip_rect
.left
;
583 input
->u
.mi
.dy
+= clip_rect
.top
;
584 __wine_send_input( hwnd
, input
);
588 if (window
!= root_window
)
590 pt
.x
= input
->u
.mi
.dx
;
591 pt
.y
= input
->u
.mi
.dy
;
593 else pt
= root_to_virtual_screen( input
->u
.mi
.dx
, input
->u
.mi
.dy
);
595 if (!(data
= get_win_data( hwnd
))) return;
597 if (window
== data
->whole_window
)
599 pt
.x
+= data
->whole_rect
.left
- data
->client_rect
.left
;
600 pt
.y
+= data
->whole_rect
.top
- data
->client_rect
.top
;
603 if (GetWindowLongW( data
->hwnd
, GWL_EXSTYLE
) & WS_EX_LAYOUTRTL
)
604 pt
.x
= data
->client_rect
.right
- data
->client_rect
.left
- 1 - pt
.x
;
605 MapWindowPoints( hwnd
, 0, &pt
, 1 );
607 if (InterlockedExchangePointer( (void **)&cursor_window
, hwnd
) != hwnd
||
608 input
->u
.mi
.time
- last_cursor_change
> 100)
610 sync_window_cursor( data
->whole_window
);
611 last_cursor_change
= input
->u
.mi
.time
;
613 release_win_data( data
);
615 if (hwnd
!= GetDesktopWindow())
617 hwnd
= GetAncestor( hwnd
, GA_ROOT
);
618 if ((input
->u
.mi
.dwFlags
& (MOUSEEVENTF_LEFTDOWN
|MOUSEEVENTF_RIGHTDOWN
)) && hwnd
== GetForegroundWindow())
619 clip_fullscreen_window( hwnd
, FALSE
);
622 /* update the wine server Z-order */
624 if (hwnd
!= x11drv_thread_data()->grab_hwnd
&&
625 /* ignore event if a button is pressed, since the mouse is then grabbed too */
626 !(state
& (Button1Mask
|Button2Mask
|Button3Mask
|Button4Mask
|Button5Mask
|Button6Mask
|Button7Mask
)))
629 SetRect( &rect
, pt
.x
, pt
.y
, pt
.x
+ 1, pt
.y
+ 1 );
630 MapWindowPoints( 0, hwnd
, (POINT
*)&rect
, 2 );
632 SERVER_START_REQ( update_window_zorder
)
634 req
->window
= wine_server_user_handle( hwnd
);
635 req
->rect
.left
= rect
.left
;
636 req
->rect
.top
= rect
.top
;
637 req
->rect
.right
= rect
.right
;
638 req
->rect
.bottom
= rect
.bottom
;
639 wine_server_call( req
);
644 input
->u
.mi
.dx
= pt
.x
;
645 input
->u
.mi
.dy
= pt
.y
;
646 __wine_send_input( hwnd
, input
);
649 #ifdef SONAME_LIBXCURSOR
651 /***********************************************************************
652 * create_xcursor_frame
654 * Use Xcursor to create a frame of an X cursor from a Windows one.
656 static XcursorImage
*create_xcursor_frame( HDC hdc
, const ICONINFOEXW
*iinfo
, HANDLE icon
,
657 HBITMAP hbmColor
, unsigned char *color_bits
, int color_size
,
658 HBITMAP hbmMask
, unsigned char *mask_bits
, int mask_size
,
659 int width
, int height
, int istep
)
661 XcursorImage
*image
, *ret
= NULL
;
662 DWORD delay_jiffies
, num_steps
;
664 BOOL has_alpha
= FALSE
;
667 image
= pXcursorImageCreate( width
, height
);
670 ERR("X11 failed to produce a cursor frame!\n");
674 image
->xhot
= iinfo
->xHotspot
;
675 image
->yhot
= iinfo
->yHotspot
;
677 image
->delay
= 100; /* fallback delay, 100 ms */
678 if (GetCursorFrameInfo(icon
, 0x0 /* unknown parameter */, istep
, &delay_jiffies
, &num_steps
) != 0)
679 image
->delay
= (100 * delay_jiffies
) / 6; /* convert jiffies (1/60s) to milliseconds */
681 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep
);
683 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
684 memset( color_bits
, 0x00, color_size
);
685 SelectObject( hdc
, hbmColor
);
686 if (!DrawIconEx( hdc
, 0, 0, icon
, width
, height
, istep
, NULL
, DI_NORMAL
))
688 TRACE("Could not draw frame %d (walk past end of frames).\n", istep
);
691 memcpy( image
->pixels
, color_bits
, color_size
);
693 /* check if the cursor frame was drawn with an alpha channel */
694 for (i
= 0, ptr
= image
->pixels
; i
< width
* height
; i
++, ptr
++)
695 if ((has_alpha
= (*ptr
& 0xff000000) != 0)) break;
697 /* if no alpha channel was drawn then generate it from the mask */
700 unsigned int width_bytes
= (width
+ 31) / 32 * 4;
702 /* draw the cursor mask to a temporary buffer */
703 memset( mask_bits
, 0xFF, mask_size
);
704 SelectObject( hdc
, hbmMask
);
705 if (!DrawIconEx( hdc
, 0, 0, icon
, width
, height
, istep
, NULL
, DI_MASK
))
707 ERR("Failed to draw frame mask %d.\n", istep
);
710 /* use the buffer to directly modify the XcursorImage alpha channel */
711 for (y
= 0, ptr
= image
->pixels
; y
< height
; y
++)
712 for (x
= 0; x
< width
; x
++, ptr
++)
713 if (!((mask_bits
[y
* width_bytes
+ x
/ 8] << (x
% 8)) & 0x80))
719 if (ret
== NULL
) pXcursorImageDestroy( image
);
723 /***********************************************************************
724 * create_xcursor_cursor
726 * Use Xcursor to create an X cursor from a Windows one.
728 static Cursor
create_xcursor_cursor( HDC hdc
, const ICONINFOEXW
*iinfo
, HANDLE icon
, int width
, int height
)
730 unsigned char *color_bits
, *mask_bits
;
731 HBITMAP hbmColor
= 0, hbmMask
= 0;
732 DWORD nFrames
, delay_jiffies
, i
;
733 int color_size
, mask_size
;
734 BITMAPINFO
*info
= NULL
;
735 XcursorImages
*images
;
739 /* Retrieve the number of frames to render */
740 if (!GetCursorFrameInfo(icon
, 0x0 /* unknown parameter */, 0, &delay_jiffies
, &nFrames
)) return 0;
741 if (!(imgs
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(XcursorImage
*)*nFrames
))) return 0;
743 /* Allocate all of the resources necessary to obtain a cursor frame */
744 if (!(info
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )))) goto cleanup
;
745 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
746 info
->bmiHeader
.biWidth
= width
;
747 info
->bmiHeader
.biHeight
= -height
;
748 info
->bmiHeader
.biPlanes
= 1;
749 info
->bmiHeader
.biCompression
= BI_RGB
;
750 info
->bmiHeader
.biXPelsPerMeter
= 0;
751 info
->bmiHeader
.biYPelsPerMeter
= 0;
752 info
->bmiHeader
.biClrUsed
= 0;
753 info
->bmiHeader
.biClrImportant
= 0;
754 info
->bmiHeader
.biBitCount
= 32;
755 color_size
= width
* height
* 4;
756 info
->bmiHeader
.biSizeImage
= color_size
;
757 hbmColor
= CreateDIBSection( hdc
, info
, DIB_RGB_COLORS
, (VOID
**) &color_bits
, NULL
, 0);
760 ERR("Failed to create DIB section for cursor color data!\n");
763 info
->bmiHeader
.biBitCount
= 1;
764 info
->bmiColors
[0].rgbRed
= 0;
765 info
->bmiColors
[0].rgbGreen
= 0;
766 info
->bmiColors
[0].rgbBlue
= 0;
767 info
->bmiColors
[0].rgbReserved
= 0;
768 info
->bmiColors
[1].rgbRed
= 0xff;
769 info
->bmiColors
[1].rgbGreen
= 0xff;
770 info
->bmiColors
[1].rgbBlue
= 0xff;
771 info
->bmiColors
[1].rgbReserved
= 0;
773 mask_size
= ((width
+ 31) / 32 * 4) * height
; /* width_bytes * height */
774 info
->bmiHeader
.biSizeImage
= mask_size
;
775 hbmMask
= CreateDIBSection( hdc
, info
, DIB_RGB_COLORS
, (VOID
**) &mask_bits
, NULL
, 0);
778 ERR("Failed to create DIB section for cursor mask data!\n");
782 /* Create an XcursorImage for each frame of the cursor */
783 for (i
=0; i
<nFrames
; i
++)
785 imgs
[i
] = create_xcursor_frame( hdc
, iinfo
, icon
,
786 hbmColor
, color_bits
, color_size
,
787 hbmMask
, mask_bits
, mask_size
,
789 if (!imgs
[i
]) goto cleanup
;
792 /* Build an X cursor out of all of the frames */
793 if (!(images
= pXcursorImagesCreate( nFrames
))) goto cleanup
;
794 for (images
->nimage
= 0; images
->nimage
< nFrames
; images
->nimage
++)
795 images
->images
[images
->nimage
] = imgs
[images
->nimage
];
796 cursor
= pXcursorImagesLoadCursor( gdi_display
, images
);
797 pXcursorImagesDestroy( images
); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
798 HeapFree( GetProcessHeap(), 0, imgs
);
804 /* Failed to produce a cursor, free previously allocated frames */
805 for (i
=0; i
<nFrames
&& imgs
[i
]; i
++)
806 pXcursorImageDestroy( imgs
[i
] );
807 HeapFree( GetProcessHeap(), 0, imgs
);
809 /* Cleanup all of the resources used to obtain the frame data */
810 if (hbmColor
) DeleteObject( hbmColor
);
811 if (hbmMask
) DeleteObject( hbmMask
);
812 HeapFree( GetProcessHeap(), 0, info
);
816 #endif /* SONAME_LIBXCURSOR */
819 struct system_cursors
825 static const struct system_cursors user32_cursors
[] =
827 { OCR_NORMAL
, "left_ptr" },
828 { OCR_IBEAM
, "xterm" },
829 { OCR_WAIT
, "watch" },
830 { OCR_CROSS
, "cross" },
831 { OCR_UP
, "center_ptr" },
832 { OCR_SIZE
, "fleur" },
833 { OCR_SIZEALL
, "fleur" },
834 { OCR_ICON
, "icon" },
835 { OCR_SIZENWSE
, "nwse-resize" },
836 { OCR_SIZENESW
, "nesw-resize" },
837 { OCR_SIZEWE
, "ew-resize" },
838 { OCR_SIZENS
, "ns-resize" },
839 { OCR_NO
, "not-allowed" },
840 { OCR_HAND
, "hand2" },
841 { OCR_APPSTARTING
, "left_ptr_watch" },
842 { OCR_HELP
, "question_arrow" },
846 static const struct system_cursors comctl32_cursors
[] =
851 { 106, "col-resize" },
852 { 107, "col-resize" },
854 { 135, "row-resize" },
858 static const struct system_cursors ole32_cursors
[] =
867 static const struct system_cursors riched20_cursors
[] =
870 { 107, "right_ptr" },
879 const struct system_cursors
*cursors
;
883 { user32_cursors
, {'u','s','e','r','3','2','.','d','l','l',0} },
884 { comctl32_cursors
, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
885 { ole32_cursors
, {'o','l','e','3','2','.','d','l','l',0} },
886 { riched20_cursors
, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
889 struct cursor_font_fallback
895 static const struct cursor_font_fallback fallbacks
[] =
897 { "X_cursor", XC_X_cursor
},
898 { "arrow", XC_arrow
},
899 { "based_arrow_down", XC_based_arrow_down
},
900 { "based_arrow_up", XC_based_arrow_up
},
902 { "bogosity", XC_bogosity
},
903 { "bottom_left_corner", XC_bottom_left_corner
},
904 { "bottom_right_corner", XC_bottom_right_corner
},
905 { "bottom_side", XC_bottom_side
},
906 { "bottom_tee", XC_bottom_tee
},
907 { "box_spiral", XC_box_spiral
},
908 { "center_ptr", XC_center_ptr
},
909 { "circle", XC_circle
},
910 { "clock", XC_clock
},
911 { "coffee_mug", XC_coffee_mug
},
912 { "col-resize", XC_sb_h_double_arrow
},
913 { "cross", XC_cross
},
914 { "cross_reverse", XC_cross_reverse
},
915 { "crosshair", XC_crosshair
},
916 { "diamond_cross", XC_diamond_cross
},
918 { "dotbox", XC_dotbox
},
919 { "double_arrow", XC_double_arrow
},
920 { "draft_large", XC_draft_large
},
921 { "draft_small", XC_draft_small
},
922 { "draped_box", XC_draped_box
},
923 { "exchange", XC_exchange
},
924 { "fleur", XC_fleur
},
925 { "gobbler", XC_gobbler
},
926 { "gumby", XC_gumby
},
927 { "hand1", XC_hand1
},
928 { "hand2", XC_hand2
},
929 { "heart", XC_heart
},
931 { "iron_cross", XC_iron_cross
},
932 { "left_ptr", XC_left_ptr
},
933 { "left_side", XC_left_side
},
934 { "left_tee", XC_left_tee
},
935 { "leftbutton", XC_leftbutton
},
936 { "ll_angle", XC_ll_angle
},
937 { "lr_angle", XC_lr_angle
},
939 { "middlebutton", XC_middlebutton
},
940 { "mouse", XC_mouse
},
941 { "pencil", XC_pencil
},
942 { "pirate", XC_pirate
},
944 { "question_arrow", XC_question_arrow
},
945 { "right_ptr", XC_right_ptr
},
946 { "right_side", XC_right_side
},
947 { "right_tee", XC_right_tee
},
948 { "rightbutton", XC_rightbutton
},
949 { "row-resize", XC_sb_v_double_arrow
},
950 { "rtl_logo", XC_rtl_logo
},
951 { "sailboat", XC_sailboat
},
952 { "sb_down_arrow", XC_sb_down_arrow
},
953 { "sb_h_double_arrow", XC_sb_h_double_arrow
},
954 { "sb_left_arrow", XC_sb_left_arrow
},
955 { "sb_right_arrow", XC_sb_right_arrow
},
956 { "sb_up_arrow", XC_sb_up_arrow
},
957 { "sb_v_double_arrow", XC_sb_v_double_arrow
},
958 { "shuttle", XC_shuttle
},
959 { "sizing", XC_sizing
},
960 { "spider", XC_spider
},
961 { "spraycan", XC_spraycan
},
963 { "target", XC_target
},
964 { "tcross", XC_tcross
},
965 { "top_left_arrow", XC_top_left_arrow
},
966 { "top_left_corner", XC_top_left_corner
},
967 { "top_right_corner", XC_top_right_corner
},
968 { "top_side", XC_top_side
},
969 { "top_tee", XC_top_tee
},
971 { "ul_angle", XC_ul_angle
},
972 { "umbrella", XC_umbrella
},
973 { "ur_angle", XC_ur_angle
},
974 { "watch", XC_watch
},
975 { "xterm", XC_xterm
}
978 static int fallback_cmp( const void *key
, const void *member
)
980 const struct cursor_font_fallback
*fallback
= member
;
981 return strcmp( key
, fallback
->name
);
984 static int find_fallback_shape( const char *name
)
986 struct cursor_font_fallback
*fallback
;
988 if ((fallback
= bsearch( name
, fallbacks
, sizeof(fallbacks
) / sizeof(fallbacks
[0]),
989 sizeof(*fallback
), fallback_cmp
)))
990 return fallback
->shape
;
994 /***********************************************************************
995 * create_xcursor_system_cursor
997 * Create an X cursor for a system cursor.
999 static Cursor
create_xcursor_system_cursor( const ICONINFOEXW
*info
)
1001 static const WCHAR idW
[] = {'%','h','u',0};
1002 const struct system_cursors
*cursors
;
1007 WCHAR
*p
, name
[MAX_PATH
* 2], valueW
[64];
1011 if (!info
->szModName
[0]) return 0;
1013 p
= strrchrW( info
->szModName
, '\\' );
1014 strcpyW( name
, p
? p
+ 1 : info
->szModName
);
1015 p
= name
+ strlenW( name
);
1017 if (info
->szResName
[0]) strcpyW( p
, info
->szResName
);
1018 else sprintfW( p
, idW
, info
->wResID
);
1021 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
1022 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\X11 Driver\\Cursors", &key
))
1024 DWORD size
= sizeof(valueW
);
1025 ret
= RegQueryValueExW( key
, name
, NULL
, NULL
, (BYTE
*)valueW
, &size
);
1029 if (!valueW
[0]) return 0; /* force standard cursor */
1030 if (!WideCharToMultiByte( CP_UNIXCP
, 0, valueW
, -1, valueA
, sizeof(valueA
), NULL
, NULL
))
1036 if (info
->szResName
[0]) goto done
; /* only integer resources are supported here */
1037 if (!(module
= GetModuleHandleW( info
->szModName
))) goto done
;
1039 for (i
= 0; i
< sizeof(module_cursors
)/sizeof(module_cursors
[0]); i
++)
1040 if (GetModuleHandleW( module_cursors
[i
].name
) == module
) break;
1041 if (i
== sizeof(module_cursors
)/sizeof(module_cursors
[0])) goto done
;
1043 cursors
= module_cursors
[i
].cursors
;
1044 for (i
= 0; cursors
[i
].id
; i
++)
1045 if (cursors
[i
].id
== info
->wResID
)
1047 strcpy( valueA
, cursors
[i
].name
);
1054 #ifdef SONAME_LIBXCURSOR
1055 if (pXcursorLibraryLoadCursor
) cursor
= pXcursorLibraryLoadCursor( gdi_display
, valueA
);
1059 int shape
= find_fallback_shape( valueA
);
1060 if (shape
!= -1) cursor
= XCreateFontCursor( gdi_display
, shape
);
1062 if (!cursor
) WARN( "no system cursor found for %s mapped to %s\n",
1063 debugstr_w(name
), debugstr_a(valueA
) );
1065 else WARN( "no system cursor found for %s\n", debugstr_w(name
) );
1070 /***********************************************************************
1071 * create_xlib_monochrome_cursor
1073 * Create a monochrome X cursor from a Windows one.
1075 static Cursor
create_xlib_monochrome_cursor( HDC hdc
, const ICONINFOEXW
*icon
, int width
, int height
)
1077 char buffer
[FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )];
1078 BITMAPINFO
*info
= (BITMAPINFO
*)buffer
;
1079 const int and_y
= 0;
1080 const int xor_y
= height
;
1081 unsigned int width_bytes
= (width
+ 31) / 32 * 4;
1082 unsigned char *mask_bits
= NULL
;
1085 XVisualInfo vis
= default_visual
;
1086 Pixmap src_pixmap
, bits_pixmap
, mask_pixmap
;
1087 struct gdi_image_bits bits
;
1090 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1091 info
->bmiHeader
.biWidth
= width
;
1092 info
->bmiHeader
.biHeight
= -height
* 2;
1093 info
->bmiHeader
.biPlanes
= 1;
1094 info
->bmiHeader
.biBitCount
= 1;
1095 info
->bmiHeader
.biCompression
= BI_RGB
;
1096 info
->bmiHeader
.biSizeImage
= width_bytes
* height
* 2;
1097 info
->bmiHeader
.biXPelsPerMeter
= 0;
1098 info
->bmiHeader
.biYPelsPerMeter
= 0;
1099 info
->bmiHeader
.biClrUsed
= 0;
1100 info
->bmiHeader
.biClrImportant
= 0;
1102 if (!(mask_bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
))) goto done
;
1103 if (!GetDIBits( hdc
, icon
->hbmMask
, 0, height
* 2, mask_bits
, info
, DIB_RGB_COLORS
)) goto done
;
1106 bits
.ptr
= mask_bits
;
1108 bits
.is_copy
= TRUE
;
1109 if (!(src_pixmap
= create_pixmap_from_image( hdc
, &vis
, info
, &bits
, DIB_RGB_COLORS
))) goto done
;
1111 bits_pixmap
= XCreatePixmap( gdi_display
, root_window
, width
, height
, 1 );
1112 mask_pixmap
= XCreatePixmap( gdi_display
, root_window
, width
, height
, 1 );
1113 gc
= XCreateGC( gdi_display
, src_pixmap
, 0, NULL
);
1114 XSetGraphicsExposures( gdi_display
, gc
, False
);
1116 /* We have to do some magic here, as cursors are not fully
1117 * compatible between Windows and X11. Under X11, there are
1118 * only 3 possible color cursor: black, white and masked. So
1119 * we map the 4th Windows color (invert the bits on the screen)
1120 * to black and an additional white bit on another place
1121 * (+1,+1). This require some boolean arithmetic:
1124 * And Xor Result | Bits Mask Result
1125 * 0 0 black | 0 1 background
1126 * 0 1 white | 1 1 foreground
1127 * 1 0 no change | X 0 no change
1128 * 1 1 inverted | 0 1 background
1131 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
1132 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
1134 XSetFunction( gdi_display
, gc
, GXcopy
);
1135 XCopyArea( gdi_display
, src_pixmap
, bits_pixmap
, gc
, 0, and_y
, width
, height
, 0, 0 );
1136 XCopyArea( gdi_display
, src_pixmap
, mask_pixmap
, gc
, 0, and_y
, width
, height
, 0, 0 );
1137 XSetFunction( gdi_display
, gc
, GXandReverse
);
1138 XCopyArea( gdi_display
, src_pixmap
, bits_pixmap
, gc
, 0, xor_y
, width
, height
, 0, 0 );
1139 XSetFunction( gdi_display
, gc
, GXorReverse
);
1140 XCopyArea( gdi_display
, src_pixmap
, mask_pixmap
, gc
, 0, xor_y
, width
, height
, 0, 0 );
1141 /* additional white */
1142 XSetFunction( gdi_display
, gc
, GXand
);
1143 XCopyArea( gdi_display
, src_pixmap
, src_pixmap
, gc
, 0, xor_y
, width
, height
, 0, and_y
);
1144 XSetFunction( gdi_display
, gc
, GXor
);
1145 XCopyArea( gdi_display
, src_pixmap
, mask_pixmap
, gc
, 0, and_y
, width
, height
, 1, 1 );
1146 XCopyArea( gdi_display
, src_pixmap
, bits_pixmap
, gc
, 0, and_y
, width
, height
, 1, 1 );
1147 XFreeGC( gdi_display
, gc
);
1149 fg
.red
= fg
.green
= fg
.blue
= 0xffff;
1150 bg
.red
= bg
.green
= bg
.blue
= 0;
1151 cursor
= XCreatePixmapCursor( gdi_display
, bits_pixmap
, mask_pixmap
,
1152 &fg
, &bg
, icon
->xHotspot
, icon
->yHotspot
);
1153 XFreePixmap( gdi_display
, src_pixmap
);
1154 XFreePixmap( gdi_display
, bits_pixmap
);
1155 XFreePixmap( gdi_display
, mask_pixmap
);
1158 HeapFree( GetProcessHeap(), 0, mask_bits
);
1162 /***********************************************************************
1163 * create_xlib_load_mono_cursor
1165 * Create a monochrome X cursor from a color Windows one by trying to load the monochrome resource.
1167 static Cursor
create_xlib_load_mono_cursor( HDC hdc
, HANDLE handle
, int width
, int height
)
1169 Cursor cursor
= None
;
1174 if (!(mono
= CopyImage( handle
, IMAGE_CURSOR
, width
, height
, LR_MONOCHROME
| LR_COPYFROMRESOURCE
)))
1177 info
.cbSize
= sizeof(info
);
1178 if (GetIconInfoExW( mono
, &info
))
1182 GetObjectW( info
.hbmMask
, sizeof(bm
), &bm
);
1183 bm
.bmHeight
= max( 1, bm
.bmHeight
/ 2 );
1184 /* make sure hotspot is valid */
1185 if (info
.xHotspot
>= bm
.bmWidth
|| info
.yHotspot
>= bm
.bmHeight
)
1187 info
.xHotspot
= bm
.bmWidth
/ 2;
1188 info
.yHotspot
= bm
.bmHeight
/ 2;
1190 cursor
= create_xlib_monochrome_cursor( hdc
, &info
, bm
.bmWidth
, bm
.bmHeight
);
1192 else DeleteObject( info
.hbmColor
);
1193 DeleteObject( info
.hbmMask
);
1195 DestroyCursor( mono
);
1199 /***********************************************************************
1200 * create_xlib_color_cursor
1202 * Create a color X cursor from a Windows one.
1204 static Cursor
create_xlib_color_cursor( HDC hdc
, const ICONINFOEXW
*icon
, int width
, int height
)
1206 char buffer
[FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )];
1207 BITMAPINFO
*info
= (BITMAPINFO
*)buffer
;
1209 Cursor cursor
= None
;
1210 XVisualInfo vis
= default_visual
;
1211 Pixmap xor_pixmap
, mask_pixmap
;
1212 struct gdi_image_bits bits
;
1213 unsigned int *color_bits
= NULL
, *ptr
;
1214 unsigned char *mask_bits
= NULL
, *xor_bits
= NULL
;
1216 BOOL has_alpha
= FALSE
;
1217 int rfg
, gfg
, bfg
, rbg
, gbg
, bbg
, fgBits
, bgBits
;
1218 unsigned int width_bytes
= (width
+ 31) / 32 * 4;
1220 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1221 info
->bmiHeader
.biWidth
= width
;
1222 info
->bmiHeader
.biHeight
= -height
;
1223 info
->bmiHeader
.biPlanes
= 1;
1224 info
->bmiHeader
.biBitCount
= 1;
1225 info
->bmiHeader
.biCompression
= BI_RGB
;
1226 info
->bmiHeader
.biSizeImage
= width_bytes
* height
;
1227 info
->bmiHeader
.biXPelsPerMeter
= 0;
1228 info
->bmiHeader
.biYPelsPerMeter
= 0;
1229 info
->bmiHeader
.biClrUsed
= 0;
1230 info
->bmiHeader
.biClrImportant
= 0;
1232 if (!(mask_bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
))) goto done
;
1233 if (!GetDIBits( hdc
, icon
->hbmMask
, 0, height
, mask_bits
, info
, DIB_RGB_COLORS
)) goto done
;
1235 info
->bmiHeader
.biBitCount
= 32;
1236 info
->bmiHeader
.biSizeImage
= width
* height
* 4;
1237 if (!(color_bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
))) goto done
;
1238 if (!(xor_bits
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, width_bytes
* height
))) goto done
;
1239 GetDIBits( hdc
, icon
->hbmColor
, 0, height
, color_bits
, info
, DIB_RGB_COLORS
);
1241 /* compute fg/bg color and xor bitmap based on average of the color values */
1243 rfg
= gfg
= bfg
= rbg
= gbg
= bbg
= fgBits
= 0;
1244 for (y
= 0, ptr
= color_bits
; y
< height
; y
++)
1246 for (x
= 0; x
< width
; x
++, ptr
++)
1248 int red
= (*ptr
>> 16) & 0xff;
1249 int green
= (*ptr
>> 8) & 0xff;
1250 int blue
= (*ptr
>> 0) & 0xff;
1251 if (red
+ green
+ blue
> 0x40)
1257 xor_bits
[y
* width_bytes
+ x
/ 8] |= 0x80 >> (x
% 8);
1269 fg
.red
= rfg
* 257 / fgBits
;
1270 fg
.green
= gfg
* 257 / fgBits
;
1271 fg
.blue
= bfg
* 257 / fgBits
;
1273 else fg
.red
= fg
.green
= fg
.blue
= 0;
1274 bgBits
= width
* height
- fgBits
;
1277 bg
.red
= rbg
* 257 / bgBits
;
1278 bg
.green
= gbg
* 257 / bgBits
;
1279 bg
.blue
= bbg
* 257 / bgBits
;
1281 else bg
.red
= bg
.green
= bg
.blue
= 0;
1283 info
->bmiHeader
.biBitCount
= 1;
1284 info
->bmiHeader
.biClrUsed
= 0;
1285 info
->bmiHeader
.biSizeImage
= width_bytes
* height
;
1287 /* generate mask from the alpha channel if we have one */
1289 for (i
= 0, ptr
= color_bits
; i
< width
* height
; i
++, ptr
++)
1290 if ((has_alpha
= (*ptr
& 0xff000000) != 0)) break;
1294 memset( mask_bits
, 0, width_bytes
* height
);
1295 for (y
= 0, ptr
= color_bits
; y
< height
; y
++)
1296 for (x
= 0; x
< width
; x
++, ptr
++)
1297 if ((*ptr
>> 24) > 25) /* more than 10% alpha */
1298 mask_bits
[y
* width_bytes
+ x
/ 8] |= 0x80 >> (x
% 8);
1300 else /* invert the mask */
1304 ptr
= (unsigned int *)mask_bits
;
1305 for (j
= 0; j
< info
->bmiHeader
.biSizeImage
/ sizeof(*ptr
); j
++, ptr
++) *ptr
^= ~0u;
1309 bits
.ptr
= xor_bits
;
1311 bits
.is_copy
= TRUE
;
1312 if (!(xor_pixmap
= create_pixmap_from_image( hdc
, &vis
, info
, &bits
, DIB_RGB_COLORS
))) goto done
;
1314 bits
.ptr
= mask_bits
;
1315 mask_pixmap
= create_pixmap_from_image( hdc
, &vis
, info
, &bits
, DIB_RGB_COLORS
);
1319 cursor
= XCreatePixmapCursor( gdi_display
, xor_pixmap
, mask_pixmap
,
1320 &fg
, &bg
, icon
->xHotspot
, icon
->yHotspot
);
1321 XFreePixmap( gdi_display
, mask_pixmap
);
1323 XFreePixmap( gdi_display
, xor_pixmap
);
1326 HeapFree( GetProcessHeap(), 0, color_bits
);
1327 HeapFree( GetProcessHeap(), 0, xor_bits
);
1328 HeapFree( GetProcessHeap(), 0, mask_bits
);
1332 /***********************************************************************
1335 * Create an X cursor from a Windows one.
1337 static Cursor
create_cursor( HANDLE handle
)
1344 if (!handle
) return get_empty_cursor();
1346 info
.cbSize
= sizeof(info
);
1347 if (!GetIconInfoExW( handle
, &info
)) return 0;
1349 if (use_system_cursors
&& (cursor
= create_xcursor_system_cursor( &info
)))
1351 DeleteObject( info
.hbmColor
);
1352 DeleteObject( info
.hbmMask
);
1356 GetObjectW( info
.hbmMask
, sizeof(bm
), &bm
);
1357 if (!info
.hbmColor
) bm
.bmHeight
= max( 1, bm
.bmHeight
/ 2 );
1359 /* make sure hotspot is valid */
1360 if (info
.xHotspot
>= bm
.bmWidth
|| info
.yHotspot
>= bm
.bmHeight
)
1362 info
.xHotspot
= bm
.bmWidth
/ 2;
1363 info
.yHotspot
= bm
.bmHeight
/ 2;
1366 hdc
= CreateCompatibleDC( 0 );
1370 #ifdef SONAME_LIBXCURSOR
1371 if (pXcursorImagesLoadCursor
)
1372 cursor
= create_xcursor_cursor( hdc
, &info
, handle
, bm
.bmWidth
, bm
.bmHeight
);
1374 if (!cursor
) cursor
= create_xlib_load_mono_cursor( hdc
, handle
, bm
.bmWidth
, bm
.bmHeight
);
1375 if (!cursor
) cursor
= create_xlib_color_cursor( hdc
, &info
, bm
.bmWidth
, bm
.bmHeight
);
1376 DeleteObject( info
.hbmColor
);
1380 cursor
= create_xlib_monochrome_cursor( hdc
, &info
, bm
.bmWidth
, bm
.bmHeight
);
1383 DeleteObject( info
.hbmMask
);
1388 /***********************************************************************
1389 * DestroyCursorIcon (X11DRV.@)
1391 void CDECL
X11DRV_DestroyCursorIcon( HCURSOR handle
)
1395 if (!XFindContext( gdi_display
, (XID
)handle
, cursor_context
, (char **)&cursor
))
1397 TRACE( "%p xid %lx\n", handle
, cursor
);
1398 XFreeCursor( gdi_display
, cursor
);
1399 XDeleteContext( gdi_display
, (XID
)handle
, cursor_context
);
1403 /***********************************************************************
1404 * SetCursor (X11DRV.@)
1406 void CDECL
X11DRV_SetCursor( HCURSOR handle
)
1408 if (InterlockedExchangePointer( (void **)&last_cursor
, handle
) != handle
||
1409 GetTickCount() - last_cursor_change
> 100)
1411 last_cursor_change
= GetTickCount();
1412 if (cursor_window
) SendNotifyMessageW( cursor_window
, WM_X11DRV_SET_CURSOR
, 0, (LPARAM
)handle
);
1416 /***********************************************************************
1417 * SetCursorPos (X11DRV.@)
1419 BOOL CDECL
X11DRV_SetCursorPos( INT x
, INT y
)
1421 struct x11drv_thread_data
*data
= x11drv_init_thread_data();
1422 POINT pos
= virtual_screen_to_root( x
, y
);
1424 XWarpPointer( data
->display
, root_window
, root_window
, 0, 0, 0, 0, pos
.x
, pos
.y
);
1425 data
->warp_serial
= NextRequest( data
->display
);
1426 XNoOp( data
->display
);
1427 XFlush( data
->display
); /* avoids bad mouse lag in games that do their own mouse warping */
1428 TRACE( "warped to %d,%d serial %lu\n", x
, y
, data
->warp_serial
);
1432 /***********************************************************************
1433 * GetCursorPos (X11DRV.@)
1435 BOOL CDECL
X11DRV_GetCursorPos(LPPOINT pos
)
1437 Display
*display
= thread_init_display();
1439 int rootX
, rootY
, winX
, winY
;
1440 unsigned int xstate
;
1443 ret
= XQueryPointer( display
, root_window
, &root
, &child
, &rootX
, &rootY
, &winX
, &winY
, &xstate
);
1447 *pos
= root_to_virtual_screen( winX
, winY
);
1448 TRACE( "pointer at %s server pos %s\n", wine_dbgstr_point(pos
), wine_dbgstr_point(&old
) );
1453 /***********************************************************************
1454 * ClipCursor (X11DRV.@)
1456 BOOL CDECL
X11DRV_ClipCursor( LPCRECT clip
)
1458 RECT virtual_rect
= get_virtual_screen_rect();
1460 if (!clip
) clip
= &virtual_rect
;
1464 HWND foreground
= GetForegroundWindow();
1466 /* we are clipping if the clip rectangle is smaller than the screen */
1467 if (clip
->left
> virtual_rect
.left
|| clip
->right
< virtual_rect
.right
||
1468 clip
->top
> virtual_rect
.top
|| clip
->bottom
< virtual_rect
.bottom
)
1472 /* forward request to the foreground window if it's in a different thread */
1473 tid
= GetWindowThreadProcessId( foreground
, &pid
);
1474 if (tid
&& tid
!= GetCurrentThreadId() && pid
== GetCurrentProcessId())
1476 TRACE( "forwarding clip request to %p\n", foreground
);
1477 SendNotifyMessageW( foreground
, WM_X11DRV_CLIP_CURSOR
, 0, 0 );
1480 else if (grab_clipping_window( clip
)) return TRUE
;
1482 else /* if currently clipping, check if we should switch to fullscreen clipping */
1484 struct x11drv_thread_data
*data
= x11drv_thread_data();
1485 if (data
&& data
->clip_hwnd
)
1487 if (EqualRect( clip
, &clip_rect
) || clip_fullscreen_window( foreground
, TRUE
))
1492 ungrab_clipping_window();
1496 /***********************************************************************
1497 * move_resize_window
1499 void move_resize_window( HWND hwnd
, int dir
)
1501 Display
*display
= thread_display();
1506 Window win
, root
, child
;
1507 unsigned int xstate
;
1509 if (!(win
= X11DRV_get_whole_window( hwnd
))) return;
1511 pt
= GetMessagePos();
1512 pos
= virtual_screen_to_root( (short)LOWORD( pt
), (short)HIWORD( pt
) );
1514 if (GetKeyState( VK_LBUTTON
) & 0x8000) button
= 1;
1515 else if (GetKeyState( VK_MBUTTON
) & 0x8000) button
= 2;
1516 else if (GetKeyState( VK_RBUTTON
) & 0x8000) button
= 3;
1518 TRACE( "hwnd %p/%lx, pos %s, dir %d, button %d\n", hwnd
, win
, wine_dbgstr_point(&pos
), dir
, button
);
1520 xev
.xclient
.type
= ClientMessage
;
1521 xev
.xclient
.window
= win
;
1522 xev
.xclient
.message_type
= x11drv_atom(_NET_WM_MOVERESIZE
);
1523 xev
.xclient
.serial
= 0;
1524 xev
.xclient
.display
= display
;
1525 xev
.xclient
.send_event
= True
;
1526 xev
.xclient
.format
= 32;
1527 xev
.xclient
.data
.l
[0] = pos
.x
; /* x coord */
1528 xev
.xclient
.data
.l
[1] = pos
.y
; /* y coord */
1529 xev
.xclient
.data
.l
[2] = dir
; /* direction */
1530 xev
.xclient
.data
.l
[3] = button
; /* button */
1531 xev
.xclient
.data
.l
[4] = 0; /* unused */
1533 /* need to ungrab the pointer that may have been automatically grabbed
1534 * with a ButtonPress event */
1535 XUngrabPointer( display
, CurrentTime
);
1536 XSendEvent(display
, root_window
, False
, SubstructureNotifyMask
| SubstructureRedirectMask
, &xev
);
1538 /* try to detect the end of the size/move by polling for the mouse button to be released */
1539 /* (some apps don't like it if we return before the size/move is done) */
1541 if (!button
) return;
1542 SendMessageW( hwnd
, WM_ENTERSIZEMOVE
, 0, 0 );
1548 int x
, y
, rootX
, rootY
;
1550 if (!XQueryPointer( display
, root_window
, &root
, &child
, &rootX
, &rootY
, &x
, &y
, &xstate
)) break;
1552 if (!(xstate
& (Button1Mask
<< (button
- 1))))
1554 /* fake a button release event */
1555 pos
= root_to_virtual_screen( x
, y
);
1556 input
.type
= INPUT_MOUSE
;
1557 input
.u
.mi
.dx
= pos
.x
;
1558 input
.u
.mi
.dy
= pos
.y
;
1559 input
.u
.mi
.mouseData
= button_up_data
[button
- 1];
1560 input
.u
.mi
.dwFlags
= button_up_flags
[button
- 1] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
;
1561 input
.u
.mi
.time
= GetTickCount();
1562 input
.u
.mi
.dwExtraInfo
= 0;
1563 __wine_send_input( hwnd
, &input
);
1566 while (PeekMessageW( &msg
, 0, 0, 0, PM_REMOVE
))
1568 if (!CallMsgFilterW( &msg
, MSGF_SIZE
))
1570 TranslateMessage( &msg
);
1571 DispatchMessageW( &msg
);
1575 if (!(xstate
& (Button1Mask
<< (button
- 1)))) break;
1576 MsgWaitForMultipleObjects( 0, NULL
, FALSE
, 100, QS_ALLINPUT
);
1579 TRACE( "hwnd %p/%lx done\n", hwnd
, win
);
1580 SendMessageW( hwnd
, WM_EXITSIZEMOVE
, 0, 0 );
1584 /***********************************************************************
1585 * X11DRV_ButtonPress
1587 BOOL
X11DRV_ButtonPress( HWND hwnd
, XEvent
*xev
)
1589 XButtonEvent
*event
= &xev
->xbutton
;
1590 int buttonNum
= event
->button
- 1;
1593 if (buttonNum
>= NB_BUTTONS
) return FALSE
;
1595 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd
, event
->window
, buttonNum
, event
->x
, event
->y
);
1597 input
.u
.mi
.dx
= event
->x
;
1598 input
.u
.mi
.dy
= event
->y
;
1599 input
.u
.mi
.mouseData
= button_down_data
[buttonNum
];
1600 input
.u
.mi
.dwFlags
= button_down_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
;
1601 input
.u
.mi
.time
= EVENT_x11_time_to_win32_time( event
->time
);
1602 input
.u
.mi
.dwExtraInfo
= 0;
1604 update_user_time( event
->time
);
1605 send_mouse_input( hwnd
, event
->window
, event
->state
, &input
);
1610 /***********************************************************************
1611 * X11DRV_ButtonRelease
1613 BOOL
X11DRV_ButtonRelease( HWND hwnd
, XEvent
*xev
)
1615 XButtonEvent
*event
= &xev
->xbutton
;
1616 int buttonNum
= event
->button
- 1;
1619 if (buttonNum
>= NB_BUTTONS
|| !button_up_flags
[buttonNum
]) return FALSE
;
1621 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd
, event
->window
, buttonNum
, event
->x
, event
->y
);
1623 input
.u
.mi
.dx
= event
->x
;
1624 input
.u
.mi
.dy
= event
->y
;
1625 input
.u
.mi
.mouseData
= button_up_data
[buttonNum
];
1626 input
.u
.mi
.dwFlags
= button_up_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
;
1627 input
.u
.mi
.time
= EVENT_x11_time_to_win32_time( event
->time
);
1628 input
.u
.mi
.dwExtraInfo
= 0;
1630 send_mouse_input( hwnd
, event
->window
, event
->state
, &input
);
1635 /***********************************************************************
1636 * X11DRV_MotionNotify
1638 BOOL
X11DRV_MotionNotify( HWND hwnd
, XEvent
*xev
)
1640 XMotionEvent
*event
= &xev
->xmotion
;
1643 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d serial %lu\n",
1644 hwnd
, event
->window
, event
->x
, event
->y
, event
->is_hint
, event
->serial
);
1646 input
.u
.mi
.dx
= event
->x
;
1647 input
.u
.mi
.dy
= event
->y
;
1648 input
.u
.mi
.mouseData
= 0;
1649 input
.u
.mi
.dwFlags
= MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
;
1650 input
.u
.mi
.time
= EVENT_x11_time_to_win32_time( event
->time
);
1651 input
.u
.mi
.dwExtraInfo
= 0;
1653 if (!hwnd
&& is_old_motion_event( event
->serial
))
1655 TRACE( "pos %d,%d old serial %lu, ignoring\n", input
.u
.mi
.dx
, input
.u
.mi
.dy
, event
->serial
);
1658 send_mouse_input( hwnd
, event
->window
, event
->state
, &input
);
1663 /***********************************************************************
1664 * X11DRV_EnterNotify
1666 BOOL
X11DRV_EnterNotify( HWND hwnd
, XEvent
*xev
)
1668 XCrossingEvent
*event
= &xev
->xcrossing
;
1671 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd
, event
->window
, event
->x
, event
->y
, event
->detail
);
1673 if (event
->detail
== NotifyVirtual
) return FALSE
;
1674 if (hwnd
== x11drv_thread_data()->grab_hwnd
) return FALSE
;
1676 /* simulate a mouse motion event */
1677 input
.u
.mi
.dx
= event
->x
;
1678 input
.u
.mi
.dy
= event
->y
;
1679 input
.u
.mi
.mouseData
= 0;
1680 input
.u
.mi
.dwFlags
= MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
;
1681 input
.u
.mi
.time
= EVENT_x11_time_to_win32_time( event
->time
);
1682 input
.u
.mi
.dwExtraInfo
= 0;
1684 if (is_old_motion_event( event
->serial
))
1686 TRACE( "pos %d,%d old serial %lu, ignoring\n", input
.u
.mi
.dx
, input
.u
.mi
.dy
, event
->serial
);
1689 send_mouse_input( hwnd
, event
->window
, event
->state
, &input
);
1693 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1695 /***********************************************************************
1696 * X11DRV_DeviceChanged
1698 static BOOL
X11DRV_DeviceChanged( XGenericEventCookie
*xev
)
1700 XIDeviceChangedEvent
*event
= xev
->data
;
1701 struct x11drv_thread_data
*data
= x11drv_thread_data();
1703 if (event
->deviceid
!= data
->xi2_core_pointer
) return FALSE
;
1704 if (event
->reason
!= XISlaveSwitch
) return FALSE
;
1706 update_relative_valuators( event
->classes
, event
->num_classes
);
1707 data
->xi2_current_slave
= event
->sourceid
;
1711 /***********************************************************************
1714 static BOOL
X11DRV_RawMotion( XGenericEventCookie
*xev
)
1716 XIRawEvent
*event
= xev
->data
;
1717 const double *values
= event
->valuators
.values
;
1721 double dx
= 0, dy
= 0, val
;
1722 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
1723 struct x11drv_valuator_data
*x_rel
, *y_rel
;
1725 if (thread_data
->x_rel_valuator
.number
< 0 || thread_data
->y_rel_valuator
.number
< 0) return FALSE
;
1726 if (!event
->valuators
.mask_len
) return FALSE
;
1727 if (thread_data
->xi2_state
!= xi_enabled
) return FALSE
;
1729 /* If there is no slave currently detected, no previous motion nor device
1730 * change events were received. Look it up now on the device list in this
1733 if (!thread_data
->xi2_current_slave
)
1735 XIDeviceInfo
*devices
= thread_data
->xi2_devices
;
1737 for (i
= 0; i
< thread_data
->xi2_device_count
; i
++)
1739 if (devices
[i
].use
!= XISlavePointer
) continue;
1740 if (devices
[i
].deviceid
!= event
->deviceid
) continue;
1741 if (devices
[i
].attachment
!= thread_data
->xi2_core_pointer
) continue;
1742 thread_data
->xi2_current_slave
= event
->deviceid
;
1747 if (event
->deviceid
!= thread_data
->xi2_current_slave
) return FALSE
;
1749 x_rel
= &thread_data
->x_rel_valuator
;
1750 y_rel
= &thread_data
->y_rel_valuator
;
1752 input
.u
.mi
.mouseData
= 0;
1753 input
.u
.mi
.dwFlags
= MOUSEEVENTF_MOVE
;
1754 input
.u
.mi
.time
= EVENT_x11_time_to_win32_time( event
->time
);
1755 input
.u
.mi
.dwExtraInfo
= 0;
1759 virtual_rect
= get_virtual_screen_rect();
1761 for (i
= 0; i
<= max ( x_rel
->number
, y_rel
->number
); i
++)
1763 if (!XIMaskIsSet( event
->valuators
.mask
, i
)) continue;
1765 if (i
== x_rel
->number
)
1767 input
.u
.mi
.dx
= dx
= val
;
1768 if (x_rel
->min
< x_rel
->max
)
1769 input
.u
.mi
.dx
= val
* (virtual_rect
.right
- virtual_rect
.left
)
1770 / (x_rel
->max
- x_rel
->min
);
1772 if (i
== y_rel
->number
)
1774 input
.u
.mi
.dy
= dy
= val
;
1775 if (y_rel
->min
< y_rel
->max
)
1776 input
.u
.mi
.dy
= val
* (virtual_rect
.bottom
- virtual_rect
.top
)
1777 / (y_rel
->max
- y_rel
->min
);
1781 if (broken_rawevents
&& is_old_motion_event( xev
->serial
))
1783 TRACE( "pos %d,%d old serial %lu, ignoring\n", input
.u
.mi
.dx
, input
.u
.mi
.dy
, xev
->serial
);
1787 TRACE( "pos %d,%d (event %f,%f)\n", input
.u
.mi
.dx
, input
.u
.mi
.dy
, dx
, dy
);
1789 input
.type
= INPUT_MOUSE
;
1790 __wine_send_input( 0, &input
);
1794 #endif /* HAVE_X11_EXTENSIONS_XINPUT2_H */
1797 /***********************************************************************
1798 * X11DRV_XInput2_Init
1800 void X11DRV_XInput2_Init(void)
1802 #if defined(SONAME_LIBXI) && defined(HAVE_X11_EXTENSIONS_XINPUT2_H)
1804 void *libxi_handle
= wine_dlopen( SONAME_LIBXI
, RTLD_NOW
, NULL
, 0 );
1808 WARN( "couldn't load %s\n", SONAME_LIBXI
);
1811 #define LOAD_FUNCPTR(f) \
1812 if (!(p##f = wine_dlsym( libxi_handle, #f, NULL, 0))) \
1814 WARN("Failed to load %s.\n", #f); \
1818 LOAD_FUNCPTR(XIGetClientPointer
);
1819 LOAD_FUNCPTR(XIFreeDeviceInfo
);
1820 LOAD_FUNCPTR(XIQueryDevice
);
1821 LOAD_FUNCPTR(XIQueryVersion
);
1822 LOAD_FUNCPTR(XISelectEvents
);
1825 xinput2_available
= XQueryExtension( gdi_display
, "XInputExtension", &xinput2_opcode
, &event
, &error
);
1827 /* Until version 1.10.4 rawinput was broken in XOrg, see
1828 * https://bugs.freedesktop.org/show_bug.cgi?id=30068 */
1829 broken_rawevents
= strstr(XServerVendor( gdi_display
), "X.Org") &&
1830 XVendorRelease( gdi_display
) < 11004000;
1833 TRACE( "X Input 2 support not compiled in.\n" );
1838 /***********************************************************************
1839 * X11DRV_GenericEvent
1841 BOOL
X11DRV_GenericEvent( HWND hwnd
, XEvent
*xev
)
1844 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1845 XGenericEventCookie
*event
= &xev
->xcookie
;
1847 if (!event
->data
) return FALSE
;
1848 if (event
->extension
!= xinput2_opcode
) return FALSE
;
1850 switch (event
->evtype
)
1852 case XI_DeviceChanged
:
1853 ret
= X11DRV_DeviceChanged( event
);
1856 ret
= X11DRV_RawMotion( event
);
1860 TRACE( "Unhandled event %#x\n", event
->evtype
);