4 * Copyright 1998 Ulrich Weigand
5 * Copyright 2007 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
26 #include <X11/cursorfont.h>
28 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
29 #include <X11/extensions/XInput2.h>
32 #ifdef SONAME_LIBXCURSOR
33 # include <X11/Xcursor/Xcursor.h>
34 static void *xcursor_handle
;
35 # define MAKE_FUNCPTR(f) static typeof(f) * p##f
36 MAKE_FUNCPTR(XcursorImageCreate
);
37 MAKE_FUNCPTR(XcursorImageDestroy
);
38 MAKE_FUNCPTR(XcursorImageLoadCursor
);
39 MAKE_FUNCPTR(XcursorImagesCreate
);
40 MAKE_FUNCPTR(XcursorImagesDestroy
);
41 MAKE_FUNCPTR(XcursorImagesLoadCursor
);
42 MAKE_FUNCPTR(XcursorLibraryLoadCursor
);
44 #endif /* SONAME_LIBXCURSOR */
46 #define NONAMELESSUNION
47 #define NONAMELESSSTRUCT
54 #include "wine/server.h"
55 #include "wine/library.h"
56 #include "wine/unicode.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(cursor
);
61 /**********************************************************************/
64 #define Button6Mask (1<<13)
67 #define Button7Mask (1<<14)
70 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
72 static const UINT button_down_flags
[NB_BUTTONS
] =
75 MOUSEEVENTF_MIDDLEDOWN
,
76 MOUSEEVENTF_RIGHTDOWN
,
79 MOUSEEVENTF_XDOWN
, /* FIXME: horizontal wheel */
85 static const UINT button_up_flags
[NB_BUTTONS
] =
98 static const UINT button_down_data
[NB_BUTTONS
] =
111 static const UINT button_up_data
[NB_BUTTONS
] =
124 static HWND cursor_window
;
125 static HCURSOR last_cursor
;
126 static DWORD last_cursor_change
;
127 static XContext cursor_context
;
128 static RECT clip_rect
;
129 static Cursor
create_cursor( HANDLE handle
);
131 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
132 static BOOL xinput2_available
;
133 static int xinput2_core_pointer
;
134 static int xinput2_device_count
;
135 static XIDeviceInfo
*xinput2_devices
;
136 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
137 MAKE_FUNCPTR(XIFreeDeviceInfo
);
138 MAKE_FUNCPTR(XIQueryDevice
);
139 MAKE_FUNCPTR(XIQueryVersion
);
140 MAKE_FUNCPTR(XISelectEvents
);
144 /***********************************************************************
145 * X11DRV_Xcursor_Init
147 * Load the Xcursor library for use.
149 void X11DRV_Xcursor_Init(void)
151 #ifdef SONAME_LIBXCURSOR
152 xcursor_handle
= wine_dlopen(SONAME_LIBXCURSOR
, RTLD_NOW
, NULL
, 0);
153 if (!xcursor_handle
) /* wine_dlopen failed. */
155 WARN("Xcursor failed to load. Using fallback code.\n");
158 #define LOAD_FUNCPTR(f) \
159 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
161 LOAD_FUNCPTR(XcursorImageCreate
);
162 LOAD_FUNCPTR(XcursorImageDestroy
);
163 LOAD_FUNCPTR(XcursorImageLoadCursor
);
164 LOAD_FUNCPTR(XcursorImagesCreate
);
165 LOAD_FUNCPTR(XcursorImagesDestroy
);
166 LOAD_FUNCPTR(XcursorImagesLoadCursor
);
167 LOAD_FUNCPTR(XcursorLibraryLoadCursor
);
169 #endif /* SONAME_LIBXCURSOR */
173 /***********************************************************************
176 static Cursor
get_empty_cursor(void)
178 static Cursor cursor
;
179 static const char data
[] = { 0 };
187 bg
.red
= bg
.green
= bg
.blue
= 0x0000;
188 pixmap
= XCreateBitmapFromData( gdi_display
, root_window
, data
, 1, 1 );
191 cursor
= XCreatePixmapCursor( gdi_display
, pixmap
, pixmap
, &bg
, &bg
, 0, 0 );
192 XFreePixmap( gdi_display
, pixmap
);
199 /***********************************************************************
202 void set_window_cursor( Window window
, HCURSOR handle
)
207 if (!handle
) cursor
= get_empty_cursor();
208 else if (!cursor_context
|| XFindContext( gdi_display
, (XID
)handle
, cursor_context
, (char **)&cursor
))
210 /* try to create it */
212 if (!(cursor
= create_cursor( handle
))) return;
215 if (!cursor_context
) cursor_context
= XUniqueContext();
216 if (!XFindContext( gdi_display
, (XID
)handle
, cursor_context
, (char **)&prev
))
218 /* someone else was here first */
219 XFreeCursor( gdi_display
, cursor
);
224 XSaveContext( gdi_display
, (XID
)handle
, cursor_context
, (char *)cursor
);
225 TRACE( "cursor %p created %lx\n", handle
, cursor
);
229 XDefineCursor( gdi_display
, window
, cursor
);
230 /* make the change take effect immediately */
231 XFlush( gdi_display
);
235 /***********************************************************************
238 void sync_window_cursor( Window window
)
242 SERVER_START_REQ( set_cursor
)
245 wine_server_call( req
);
246 cursor
= reply
->prev_count
>= 0 ? wine_server_ptr_handle( reply
->prev_handle
) : 0;
250 set_window_cursor( window
, cursor
);
253 /***********************************************************************
256 static void enable_xinput2(void)
258 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
259 struct x11drv_thread_data
*data
= x11drv_thread_data();
261 unsigned char mask_bits
[XIMaskLen(XI_LASTEVENT
)];
264 if (!xinput2_available
) return;
266 if (data
->xi2_state
== xi_unknown
)
268 int major
= 2, minor
= 0;
270 if (!pXIQueryVersion( data
->display
, &major
, &minor
)) data
->xi2_state
= xi_disabled
;
273 data
->xi2_state
= xi_unavailable
;
274 WARN( "X Input 2 not available\n" );
278 if (data
->xi2_state
== xi_unavailable
) return;
281 if (xinput2_devices
) pXIFreeDeviceInfo( xinput2_devices
);
282 xinput2_devices
= pXIQueryDevice( data
->display
, XIAllDevices
, &xinput2_device_count
);
283 for (i
= 0; i
< xinput2_device_count
; ++i
)
285 if (xinput2_devices
[i
].use
!= XIMasterPointer
) continue;
286 for (j
= 0; j
< xinput2_devices
[i
].num_classes
; j
++)
288 XIValuatorClassInfo
*class = (XIValuatorClassInfo
*)xinput2_devices
[i
].classes
[j
];
290 if (xinput2_devices
[i
].classes
[j
]->type
!= XIValuatorClass
) continue;
291 if (class->number
!= 0 && class->number
!= 1) continue;
292 TRACE( "Device %u (%s) class %u num %u %f,%f res %u mode %u\n",
293 xinput2_devices
[i
].deviceid
, debugstr_a(xinput2_devices
[i
].name
),
294 j
, class->number
, class->min
, class->max
, class->resolution
, class->mode
);
295 if (class->mode
== XIModeAbsolute
)
297 TRACE( "Device is absolute, not enabling XInput2\n" );
301 TRACE( "Using %u (%s) as core pointer\n",
302 xinput2_devices
[i
].deviceid
, debugstr_a(xinput2_devices
[i
].name
) );
303 xinput2_core_pointer
= xinput2_devices
[i
].deviceid
;
307 mask
.mask
= mask_bits
;
308 mask
.mask_len
= sizeof(mask_bits
);
309 memset( mask_bits
, 0, sizeof(mask_bits
) );
310 XISetMask( mask_bits
, XI_RawMotion
);
311 XISetMask( mask_bits
, XI_ButtonPress
);
313 for (i
= 0; i
< xinput2_device_count
; ++i
)
315 if (xinput2_devices
[i
].use
== XISlavePointer
&&
316 xinput2_devices
[i
].attachment
== xinput2_core_pointer
)
318 TRACE( "Device %u (%s) is attached to the core pointer\n",
319 xinput2_devices
[i
].deviceid
, debugstr_a(xinput2_devices
[i
].name
) );
320 mask
.deviceid
= xinput2_devices
[i
].deviceid
;
321 pXISelectEvents( data
->display
, DefaultRootWindow( data
->display
), &mask
, 1 );
322 data
->xi2_state
= xi_enabled
;
331 /***********************************************************************
334 static void disable_xinput2(void)
336 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
337 struct x11drv_thread_data
*data
= x11drv_thread_data();
341 if (data
->xi2_state
!= xi_enabled
) return;
343 TRACE( "disabling\n" );
344 data
->xi2_state
= xi_disabled
;
350 for (i
= 0; i
< xinput2_device_count
; ++i
)
352 if (xinput2_devices
[i
].use
== XISlavePointer
&&
353 xinput2_devices
[i
].attachment
== xinput2_core_pointer
)
355 mask
.deviceid
= xinput2_devices
[i
].deviceid
;
356 pXISelectEvents( data
->display
, DefaultRootWindow( data
->display
), &mask
, 1 );
359 pXIFreeDeviceInfo( xinput2_devices
);
360 xinput2_devices
= NULL
;
361 xinput2_device_count
= 0;
367 /***********************************************************************
368 * grab_clipping_window
370 * Start a pointer grab on the clip window.
372 static BOOL
grab_clipping_window( const RECT
*clip
)
374 static const WCHAR messageW
[] = {'M','e','s','s','a','g','e',0};
375 struct x11drv_thread_data
*data
= x11drv_thread_data();
379 if (!data
) return FALSE
;
380 if (!(clip_window
= init_clip_window())) return TRUE
;
382 if (!(msg_hwnd
= CreateWindowW( messageW
, NULL
, 0, 0, 0, 0, 0, HWND_MESSAGE
, 0,
383 GetModuleHandleW(0), NULL
)))
386 /* enable XInput2 unless we are already clipping */
387 if (!data
->clip_hwnd
) enable_xinput2();
389 if (data
->xi2_state
!= xi_enabled
)
391 WARN( "XInput2 not supported, refusing to clip to %s\n", wine_dbgstr_rect(clip
) );
392 DestroyWindow( msg_hwnd
);
397 TRACE( "clipping to %s win %lx\n", wine_dbgstr_rect(clip
), clip_window
);
400 if (!data
->clip_hwnd
) XUnmapWindow( data
->display
, clip_window
);
401 XMoveResizeWindow( data
->display
, clip_window
,
402 clip
->left
- virtual_screen_rect
.left
, clip
->top
- virtual_screen_rect
.top
,
403 max( 1, clip
->right
- clip
->left
), max( 1, clip
->bottom
- clip
->top
) );
404 XMapWindow( data
->display
, clip_window
);
406 /* if the rectangle is shrinking we may get a pointer warp */
407 if (!data
->clip_hwnd
|| clip
->left
> clip_rect
.left
|| clip
->top
> clip_rect
.top
||
408 clip
->right
< clip_rect
.right
|| clip
->bottom
< clip_rect
.bottom
)
409 data
->warp_serial
= NextRequest( data
->display
);
411 if (!XGrabPointer( data
->display
, clip_window
, False
,
412 PointerMotionMask
| ButtonPressMask
| ButtonReleaseMask
,
413 GrabModeAsync
, GrabModeAsync
, clip_window
, None
, CurrentTime
))
417 if (!clipping_cursor
)
420 DestroyWindow( msg_hwnd
);
424 if (!data
->clip_hwnd
) sync_window_cursor( clip_window
);
425 InterlockedExchangePointer( (void **)&cursor_window
, msg_hwnd
);
426 data
->clip_hwnd
= msg_hwnd
;
427 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR
, 0, (LPARAM
)msg_hwnd
);
431 /***********************************************************************
432 * ungrab_clipping_window
434 * Release the pointer grab on the clip window.
436 void ungrab_clipping_window(void)
438 Display
*display
= thread_init_display();
439 Window clip_window
= init_clip_window();
441 if (!clip_window
) return;
443 TRACE( "no longer clipping\n" );
445 XUnmapWindow( display
, clip_window
);
448 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR
, 0, 0 );
451 /***********************************************************************
452 * reset_clipping_window
454 * Forcibly reset the window clipping on external events.
456 void reset_clipping_window(void)
458 ungrab_clipping_window();
459 ClipCursor( NULL
); /* make sure the clip rectangle is reset too */
462 /***********************************************************************
465 * Notification function called upon receiving a WM_X11DRV_CLIP_CURSOR.
467 LRESULT
clip_cursor_notify( HWND hwnd
, HWND new_clip_hwnd
)
469 struct x11drv_thread_data
*data
= x11drv_thread_data();
471 if (hwnd
== GetDesktopWindow()) /* change the clip window stored in the desktop process */
473 static HWND clip_hwnd
;
475 HWND prev
= clip_hwnd
;
476 clip_hwnd
= new_clip_hwnd
;
477 if (prev
|| new_clip_hwnd
) TRACE( "clip hwnd changed from %p to %p\n", prev
, new_clip_hwnd
);
478 if (prev
) SendNotifyMessageW( prev
, WM_X11DRV_CLIP_CURSOR
, 0, 0 );
480 else if (hwnd
== data
->clip_hwnd
) /* this is a notification that clipping has been reset */
482 TRACE( "clip hwnd reset from %p\n", hwnd
);
484 data
->clip_reset
= GetTickCount();
486 DestroyWindow( hwnd
);
488 else if (hwnd
== GetForegroundWindow()) /* request to clip */
492 GetClipCursor( &clip
);
493 if (clip
.left
> virtual_screen_rect
.left
|| clip
.right
< virtual_screen_rect
.right
||
494 clip
.top
> virtual_screen_rect
.top
|| clip
.bottom
< virtual_screen_rect
.bottom
)
495 return grab_clipping_window( &clip
);
500 /***********************************************************************
501 * clip_fullscreen_window
503 * Turn on clipping if the active window is fullscreen.
505 BOOL
clip_fullscreen_window( HWND hwnd
, BOOL reset
)
507 struct x11drv_win_data
*data
;
508 struct x11drv_thread_data
*thread_data
;
512 if (hwnd
== GetDesktopWindow()) return FALSE
;
513 if (!(data
= X11DRV_get_win_data( hwnd
))) return FALSE
;
514 style
= GetWindowLongW( hwnd
, GWL_STYLE
);
515 if (!(style
& WS_VISIBLE
)) return FALSE
;
516 if ((style
& (WS_POPUP
| WS_CHILD
)) == WS_CHILD
) return FALSE
;
517 /* maximized windows don't count as full screen */
518 if ((style
& WS_MAXIMIZE
) && (style
& WS_CAPTION
) == WS_CAPTION
) return FALSE
;
519 if (!is_window_rect_fullscreen( &data
->whole_rect
)) return FALSE
;
520 if (!(thread_data
= x11drv_thread_data())) return FALSE
;
521 if (GetTickCount() - thread_data
->clip_reset
< 1000) return FALSE
;
522 if (!reset
&& clipping_cursor
&& thread_data
->clip_hwnd
) return FALSE
; /* already clipping */
523 SetRect( &rect
, 0, 0, screen_width
, screen_height
);
524 if (!grab_fullscreen
)
526 if (!EqualRect( &rect
, &virtual_screen_rect
)) return FALSE
;
527 if (root_window
!= DefaultRootWindow( gdi_display
)) return FALSE
;
529 TRACE( "win %p clipping fullscreen\n", hwnd
);
530 return grab_clipping_window( &rect
);
533 /***********************************************************************
536 * Update the various window states on a mouse event.
538 static void send_mouse_input( HWND hwnd
, Window window
, unsigned int state
, INPUT
*input
)
540 struct x11drv_win_data
*data
;
543 input
->type
= INPUT_MOUSE
;
547 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
548 HWND clip_hwnd
= thread_data
->clip_hwnd
;
550 if (!clip_hwnd
) return;
551 if (thread_data
->clip_window
!= window
) return;
552 if (InterlockedExchangePointer( (void **)&cursor_window
, clip_hwnd
) != clip_hwnd
||
553 input
->u
.mi
.time
- last_cursor_change
> 100)
555 sync_window_cursor( window
);
556 last_cursor_change
= input
->u
.mi
.time
;
558 input
->u
.mi
.dx
+= clip_rect
.left
;
559 input
->u
.mi
.dy
+= clip_rect
.top
;
560 __wine_send_input( hwnd
, input
);
564 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
566 if (window
== data
->whole_window
)
568 input
->u
.mi
.dx
+= data
->whole_rect
.left
- data
->client_rect
.left
;
569 input
->u
.mi
.dy
+= data
->whole_rect
.top
- data
->client_rect
.top
;
571 if (window
== root_window
)
573 input
->u
.mi
.dx
+= virtual_screen_rect
.left
;
574 input
->u
.mi
.dy
+= virtual_screen_rect
.top
;
576 pt
.x
= input
->u
.mi
.dx
;
577 pt
.y
= input
->u
.mi
.dy
;
578 if (GetWindowLongW( data
->hwnd
, GWL_EXSTYLE
) & WS_EX_LAYOUTRTL
)
579 pt
.x
= data
->client_rect
.right
- data
->client_rect
.left
- 1 - pt
.x
;
580 MapWindowPoints( hwnd
, 0, &pt
, 1 );
582 if (InterlockedExchangePointer( (void **)&cursor_window
, hwnd
) != hwnd
||
583 input
->u
.mi
.time
- last_cursor_change
> 100)
585 sync_window_cursor( data
->whole_window
);
586 last_cursor_change
= input
->u
.mi
.time
;
589 if (hwnd
!= GetDesktopWindow())
591 hwnd
= GetAncestor( hwnd
, GA_ROOT
);
592 if ((input
->u
.mi
.dwFlags
& (MOUSEEVENTF_LEFTDOWN
|MOUSEEVENTF_RIGHTDOWN
)) && hwnd
== GetForegroundWindow())
593 clip_fullscreen_window( hwnd
, FALSE
);
596 /* update the wine server Z-order */
598 if (window
!= x11drv_thread_data()->grab_window
&&
599 /* ignore event if a button is pressed, since the mouse is then grabbed too */
600 !(state
& (Button1Mask
|Button2Mask
|Button3Mask
|Button4Mask
|Button5Mask
|Button6Mask
|Button7Mask
)))
603 SetRect( &rect
, pt
.x
, pt
.y
, pt
.x
+ 1, pt
.y
+ 1 );
604 MapWindowPoints( 0, hwnd
, (POINT
*)&rect
, 2 );
606 SERVER_START_REQ( update_window_zorder
)
608 req
->window
= wine_server_user_handle( hwnd
);
609 req
->rect
.left
= rect
.left
;
610 req
->rect
.top
= rect
.top
;
611 req
->rect
.right
= rect
.right
;
612 req
->rect
.bottom
= rect
.bottom
;
613 wine_server_call( req
);
618 input
->u
.mi
.dx
= pt
.x
;
619 input
->u
.mi
.dy
= pt
.y
;
620 __wine_send_input( hwnd
, input
);
623 #ifdef SONAME_LIBXCURSOR
625 /***********************************************************************
626 * create_xcursor_frame
628 * Use Xcursor to create a frame of an X cursor from a Windows one.
630 static XcursorImage
*create_xcursor_frame( HDC hdc
, const ICONINFOEXW
*iinfo
, HANDLE icon
,
631 HBITMAP hbmColor
, unsigned char *color_bits
, int color_size
,
632 HBITMAP hbmMask
, unsigned char *mask_bits
, int mask_size
,
633 int width
, int height
, int istep
)
635 XcursorImage
*image
, *ret
= NULL
;
636 DWORD delay_jiffies
, num_steps
;
637 int x
, y
, i
, has_alpha
= FALSE
;
641 image
= pXcursorImageCreate( width
, height
);
645 ERR("X11 failed to produce a cursor frame!\n");
649 image
->xhot
= iinfo
->xHotspot
;
650 image
->yhot
= iinfo
->yHotspot
;
652 image
->delay
= 100; /* fallback delay, 100 ms */
653 if (GetCursorFrameInfo(icon
, 0x0 /* unknown parameter */, istep
, &delay_jiffies
, &num_steps
) != 0)
654 image
->delay
= (100 * delay_jiffies
) / 6; /* convert jiffies (1/60s) to milliseconds */
656 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep
);
658 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
659 memset( color_bits
, 0x00, color_size
);
660 SelectObject( hdc
, hbmColor
);
661 if (!DrawIconEx( hdc
, 0, 0, icon
, width
, height
, istep
, NULL
, DI_NORMAL
))
663 TRACE("Could not draw frame %d (walk past end of frames).\n", istep
);
666 memcpy( image
->pixels
, color_bits
, color_size
);
668 /* check if the cursor frame was drawn with an alpha channel */
669 for (i
= 0, ptr
= image
->pixels
; i
< width
* height
; i
++, ptr
++)
670 if ((has_alpha
= (*ptr
& 0xff000000) != 0)) break;
672 /* if no alpha channel was drawn then generate it from the mask */
675 unsigned int width_bytes
= (width
+ 31) / 32 * 4;
677 /* draw the cursor mask to a temporary buffer */
678 memset( mask_bits
, 0xFF, mask_size
);
679 SelectObject( hdc
, hbmMask
);
680 if (!DrawIconEx( hdc
, 0, 0, icon
, width
, height
, istep
, NULL
, DI_MASK
))
682 ERR("Failed to draw frame mask %d.\n", istep
);
685 /* use the buffer to directly modify the XcursorImage alpha channel */
686 for (y
= 0, ptr
= image
->pixels
; y
< height
; y
++)
687 for (x
= 0; x
< width
; x
++, ptr
++)
688 if (!((mask_bits
[y
* width_bytes
+ x
/ 8] << (x
% 8)) & 0x80))
694 if (ret
== NULL
) pXcursorImageDestroy( image
);
698 /***********************************************************************
699 * create_xcursor_cursor
701 * Use Xcursor to create an X cursor from a Windows one.
703 static Cursor
create_xcursor_cursor( HDC hdc
, const ICONINFOEXW
*iinfo
, HANDLE icon
, int width
, int height
)
705 unsigned char *color_bits
, *mask_bits
;
706 HBITMAP hbmColor
= 0, hbmMask
= 0;
707 DWORD nFrames
, delay_jiffies
, i
;
708 int color_size
, mask_size
;
709 BITMAPINFO
*info
= NULL
;
710 XcursorImages
*images
;
714 /* Retrieve the number of frames to render */
715 if (!GetCursorFrameInfo(icon
, 0x0 /* unknown parameter */, 0, &delay_jiffies
, &nFrames
)) return 0;
716 if (!(imgs
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(XcursorImage
*)*nFrames
))) return 0;
718 /* Allocate all of the resources necessary to obtain a cursor frame */
719 if (!(info
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )))) goto cleanup
;
720 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
721 info
->bmiHeader
.biWidth
= width
;
722 info
->bmiHeader
.biHeight
= -height
;
723 info
->bmiHeader
.biPlanes
= 1;
724 info
->bmiHeader
.biCompression
= BI_RGB
;
725 info
->bmiHeader
.biXPelsPerMeter
= 0;
726 info
->bmiHeader
.biYPelsPerMeter
= 0;
727 info
->bmiHeader
.biClrUsed
= 0;
728 info
->bmiHeader
.biClrImportant
= 0;
729 info
->bmiHeader
.biBitCount
= 32;
730 color_size
= width
* height
* 4;
731 info
->bmiHeader
.biSizeImage
= color_size
;
732 hbmColor
= CreateDIBSection( hdc
, info
, DIB_RGB_COLORS
, (VOID
**) &color_bits
, NULL
, 0);
735 ERR("Failed to create DIB section for cursor color data!\n");
738 info
->bmiHeader
.biBitCount
= 1;
739 info
->bmiColors
[0].rgbRed
= 0;
740 info
->bmiColors
[0].rgbGreen
= 0;
741 info
->bmiColors
[0].rgbBlue
= 0;
742 info
->bmiColors
[0].rgbReserved
= 0;
743 info
->bmiColors
[1].rgbRed
= 0xff;
744 info
->bmiColors
[1].rgbGreen
= 0xff;
745 info
->bmiColors
[1].rgbBlue
= 0xff;
746 info
->bmiColors
[1].rgbReserved
= 0;
748 mask_size
= ((width
+ 31) / 32 * 4) * height
; /* width_bytes * height */
749 info
->bmiHeader
.biSizeImage
= mask_size
;
750 hbmMask
= CreateDIBSection( hdc
, info
, DIB_RGB_COLORS
, (VOID
**) &mask_bits
, NULL
, 0);
753 ERR("Failed to create DIB section for cursor mask data!\n");
757 /* Create an XcursorImage for each frame of the cursor */
758 for (i
=0; i
<nFrames
; i
++)
760 imgs
[i
] = create_xcursor_frame( hdc
, iinfo
, icon
,
761 hbmColor
, color_bits
, color_size
,
762 hbmMask
, mask_bits
, mask_size
,
764 if (!imgs
[i
]) goto cleanup
;
767 /* Build an X cursor out of all of the frames */
768 if (!(images
= pXcursorImagesCreate( nFrames
))) goto cleanup
;
769 for (images
->nimage
= 0; images
->nimage
< nFrames
; images
->nimage
++)
770 images
->images
[images
->nimage
] = imgs
[images
->nimage
];
772 cursor
= pXcursorImagesLoadCursor( gdi_display
, images
);
774 pXcursorImagesDestroy( images
); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
775 HeapFree( GetProcessHeap(), 0, imgs
);
781 /* Failed to produce a cursor, free previously allocated frames */
782 for (i
=0; i
<nFrames
&& imgs
[i
]; i
++)
783 pXcursorImageDestroy( imgs
[i
] );
784 HeapFree( GetProcessHeap(), 0, imgs
);
786 /* Cleanup all of the resources used to obtain the frame data */
787 if (hbmColor
) DeleteObject( hbmColor
);
788 if (hbmMask
) DeleteObject( hbmMask
);
789 HeapFree( GetProcessHeap(), 0, info
);
794 struct system_cursors
800 static const struct system_cursors user32_cursors
[] =
802 { OCR_NORMAL
, "left_ptr" },
803 { OCR_IBEAM
, "xterm" },
804 { OCR_WAIT
, "watch" },
805 { OCR_CROSS
, "cross" },
806 { OCR_UP
, "center_ptr" },
807 { OCR_SIZE
, "fleur" },
808 { OCR_SIZEALL
, "fleur" },
809 { OCR_ICON
, "icon" },
810 { OCR_SIZENWSE
, "nwse-resize" },
811 { OCR_SIZENESW
, "nesw-resize" },
812 { OCR_SIZEWE
, "ew-resize" },
813 { OCR_SIZENS
, "ns-resize" },
814 { OCR_NO
, "not-allowed" },
815 { OCR_HAND
, "hand2" },
816 { OCR_APPSTARTING
, "left_ptr_watch" },
817 { OCR_HELP
, "question_arrow" },
821 static const struct system_cursors comctl32_cursors
[] =
826 { 106, "row-resize" },
827 { 107, "row-resize" },
829 { 135, "col-resize" },
833 static const struct system_cursors ole32_cursors
[] =
842 static const struct system_cursors riched20_cursors
[] =
845 { 107, "right_ptr" },
854 const struct system_cursors
*cursors
;
858 { user32_cursors
, {'u','s','e','r','3','2','.','d','l','l',0} },
859 { comctl32_cursors
, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
860 { ole32_cursors
, {'o','l','e','3','2','.','d','l','l',0} },
861 { riched20_cursors
, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
864 /***********************************************************************
865 * create_xcursor_system_cursor
867 * Create an X cursor for a system cursor.
869 static Cursor
create_xcursor_system_cursor( const ICONINFOEXW
*info
)
871 static const WCHAR idW
[] = {'%','h','u',0};
872 const struct system_cursors
*cursors
;
877 WCHAR
*p
, name
[MAX_PATH
* 2], valueW
[64];
881 if (!pXcursorLibraryLoadCursor
) return 0;
882 if (!info
->szModName
[0]) return 0;
884 p
= strrchrW( info
->szModName
, '\\' );
885 strcpyW( name
, p
? p
+ 1 : info
->szModName
);
886 p
= name
+ strlenW( name
);
888 if (info
->szResName
[0]) strcpyW( p
, info
->szResName
);
889 else sprintfW( p
, idW
, info
->wResID
);
892 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
893 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\X11 Driver\\Cursors", &key
))
895 size
= sizeof(valueW
) / sizeof(WCHAR
);
896 ret
= RegQueryValueExW( key
, name
, NULL
, NULL
, (BYTE
*)valueW
, &size
);
900 if (!valueW
[0]) return 0; /* force standard cursor */
901 if (!WideCharToMultiByte( CP_UNIXCP
, 0, valueW
, -1, valueA
, sizeof(valueA
), NULL
, NULL
))
907 if (info
->szResName
[0]) goto done
; /* only integer resources are supported here */
908 if (!(module
= GetModuleHandleW( info
->szModName
))) goto done
;
910 for (i
= 0; i
< sizeof(module_cursors
)/sizeof(module_cursors
[0]); i
++)
911 if (GetModuleHandleW( module_cursors
[i
].name
) == module
) break;
912 if (i
== sizeof(module_cursors
)/sizeof(module_cursors
[0])) goto done
;
914 cursors
= module_cursors
[i
].cursors
;
915 for (i
= 0; cursors
[i
].id
; i
++)
916 if (cursors
[i
].id
== info
->wResID
)
918 strcpy( valueA
, cursors
[i
].name
);
926 cursor
= pXcursorLibraryLoadCursor( gdi_display
, valueA
);
928 if (!cursor
) WARN( "no system cursor found for %s mapped to %s\n",
929 debugstr_w(name
), debugstr_a(valueA
) );
931 else WARN( "no system cursor found for %s\n", debugstr_w(name
) );
935 #endif /* SONAME_LIBXCURSOR */
938 /***********************************************************************
939 * create_xlib_monochrome_cursor
941 * Create a monochrome X cursor from a Windows one.
943 static Cursor
create_xlib_monochrome_cursor( HDC hdc
, const ICONINFOEXW
*icon
, int width
, int height
)
945 char buffer
[FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )];
946 BITMAPINFO
*info
= (BITMAPINFO
*)buffer
;
948 const int xor_y
= height
;
949 unsigned int width_bytes
= (width
+ 31) / 32 * 4;
950 unsigned char *mask_bits
= NULL
;
954 Pixmap src_pixmap
, bits_pixmap
, mask_pixmap
;
955 struct gdi_image_bits bits
;
958 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
959 info
->bmiHeader
.biWidth
= width
;
960 info
->bmiHeader
.biHeight
= -height
* 2;
961 info
->bmiHeader
.biPlanes
= 1;
962 info
->bmiHeader
.biBitCount
= 1;
963 info
->bmiHeader
.biCompression
= BI_RGB
;
964 info
->bmiHeader
.biSizeImage
= width_bytes
* height
* 2;
965 info
->bmiHeader
.biXPelsPerMeter
= 0;
966 info
->bmiHeader
.biYPelsPerMeter
= 0;
967 info
->bmiHeader
.biClrUsed
= 0;
968 info
->bmiHeader
.biClrImportant
= 0;
970 if (!(mask_bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
))) goto done
;
971 if (!GetDIBits( hdc
, icon
->hbmMask
, 0, height
* 2, mask_bits
, info
, DIB_RGB_COLORS
)) goto done
;
974 bits
.ptr
= mask_bits
;
977 if (!(src_pixmap
= create_pixmap_from_image( hdc
, &vis
, info
, &bits
, DIB_RGB_COLORS
))) goto done
;
980 bits_pixmap
= XCreatePixmap( gdi_display
, root_window
, width
, height
, 1 );
981 mask_pixmap
= XCreatePixmap( gdi_display
, root_window
, width
, height
, 1 );
982 gc
= XCreateGC( gdi_display
, src_pixmap
, 0, NULL
);
983 XSetGraphicsExposures( gdi_display
, gc
, False
);
985 /* We have to do some magic here, as cursors are not fully
986 * compatible between Windows and X11. Under X11, there are
987 * only 3 possible color cursor: black, white and masked. So
988 * we map the 4th Windows color (invert the bits on the screen)
989 * to black and an additional white bit on another place
990 * (+1,+1). This require some boolean arithmetic:
993 * And Xor Result | Bits Mask Result
994 * 0 0 black | 0 1 background
995 * 0 1 white | 1 1 foreground
996 * 1 0 no change | X 0 no change
997 * 1 1 inverted | 0 1 background
1000 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
1001 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
1003 XSetFunction( gdi_display
, gc
, GXcopy
);
1004 XCopyArea( gdi_display
, src_pixmap
, bits_pixmap
, gc
, 0, and_y
, width
, height
, 0, 0 );
1005 XCopyArea( gdi_display
, src_pixmap
, mask_pixmap
, gc
, 0, and_y
, width
, height
, 0, 0 );
1006 XSetFunction( gdi_display
, gc
, GXandReverse
);
1007 XCopyArea( gdi_display
, src_pixmap
, bits_pixmap
, gc
, 0, xor_y
, width
, height
, 0, 0 );
1008 XSetFunction( gdi_display
, gc
, GXorReverse
);
1009 XCopyArea( gdi_display
, src_pixmap
, mask_pixmap
, gc
, 0, xor_y
, width
, height
, 0, 0 );
1010 /* additional white */
1011 XSetFunction( gdi_display
, gc
, GXand
);
1012 XCopyArea( gdi_display
, src_pixmap
, src_pixmap
, gc
, 0, xor_y
, width
, height
, 0, and_y
);
1013 XSetFunction( gdi_display
, gc
, GXor
);
1014 XCopyArea( gdi_display
, src_pixmap
, mask_pixmap
, gc
, 0, and_y
, width
, height
, 1, 1 );
1015 XCopyArea( gdi_display
, src_pixmap
, bits_pixmap
, gc
, 0, and_y
, width
, height
, 1, 1 );
1016 XFreeGC( gdi_display
, gc
);
1018 fg
.red
= fg
.green
= fg
.blue
= 0xffff;
1019 bg
.red
= bg
.green
= bg
.blue
= 0;
1020 cursor
= XCreatePixmapCursor( gdi_display
, bits_pixmap
, mask_pixmap
,
1021 &fg
, &bg
, icon
->xHotspot
, icon
->yHotspot
);
1022 XFreePixmap( gdi_display
, src_pixmap
);
1023 XFreePixmap( gdi_display
, bits_pixmap
);
1024 XFreePixmap( gdi_display
, mask_pixmap
);
1025 wine_tsx11_unlock();
1028 HeapFree( GetProcessHeap(), 0, mask_bits
);
1032 /***********************************************************************
1033 * create_xlib_color_cursor
1035 * Create a color X cursor from a Windows one.
1037 static Cursor
create_xlib_color_cursor( HDC hdc
, const ICONINFOEXW
*icon
, int width
, int height
)
1039 char buffer
[FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )];
1040 BITMAPINFO
*info
= (BITMAPINFO
*)buffer
;
1042 Cursor cursor
= None
;
1044 Pixmap xor_pixmap
, mask_pixmap
;
1045 struct gdi_image_bits bits
;
1046 unsigned int *color_bits
= NULL
, *ptr
;
1047 unsigned char *mask_bits
= NULL
, *xor_bits
= NULL
;
1048 int i
, x
, y
, has_alpha
= 0;
1049 int rfg
, gfg
, bfg
, rbg
, gbg
, bbg
, fgBits
, bgBits
;
1050 unsigned int width_bytes
= (width
+ 31) / 32 * 4;
1052 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1053 info
->bmiHeader
.biWidth
= width
;
1054 info
->bmiHeader
.biHeight
= -height
;
1055 info
->bmiHeader
.biPlanes
= 1;
1056 info
->bmiHeader
.biBitCount
= 1;
1057 info
->bmiHeader
.biCompression
= BI_RGB
;
1058 info
->bmiHeader
.biSizeImage
= width_bytes
* height
;
1059 info
->bmiHeader
.biXPelsPerMeter
= 0;
1060 info
->bmiHeader
.biYPelsPerMeter
= 0;
1061 info
->bmiHeader
.biClrUsed
= 0;
1062 info
->bmiHeader
.biClrImportant
= 0;
1064 if (!(mask_bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
))) goto done
;
1065 if (!GetDIBits( hdc
, icon
->hbmMask
, 0, height
, mask_bits
, info
, DIB_RGB_COLORS
)) goto done
;
1067 info
->bmiHeader
.biBitCount
= 32;
1068 info
->bmiHeader
.biSizeImage
= width
* height
* 4;
1069 if (!(color_bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
))) goto done
;
1070 if (!(xor_bits
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, width_bytes
* height
))) goto done
;
1071 GetDIBits( hdc
, icon
->hbmColor
, 0, height
, color_bits
, info
, DIB_RGB_COLORS
);
1073 /* compute fg/bg color and xor bitmap based on average of the color values */
1075 rfg
= gfg
= bfg
= rbg
= gbg
= bbg
= fgBits
= 0;
1076 for (y
= 0, ptr
= color_bits
; y
< height
; y
++)
1078 for (x
= 0; x
< width
; x
++, ptr
++)
1080 int red
= (*ptr
>> 16) & 0xff;
1081 int green
= (*ptr
>> 8) & 0xff;
1082 int blue
= (*ptr
>> 0) & 0xff;
1083 if (red
+ green
+ blue
> 0x40)
1089 xor_bits
[y
* width_bytes
+ x
/ 8] |= 0x80 >> (x
% 8);
1101 fg
.red
= rfg
* 257 / fgBits
;
1102 fg
.green
= gfg
* 257 / fgBits
;
1103 fg
.blue
= bfg
* 257 / fgBits
;
1105 else fg
.red
= fg
.green
= fg
.blue
= 0;
1106 bgBits
= width
* height
- fgBits
;
1109 bg
.red
= rbg
* 257 / bgBits
;
1110 bg
.green
= gbg
* 257 / bgBits
;
1111 bg
.blue
= bbg
* 257 / bgBits
;
1113 else bg
.red
= bg
.green
= bg
.blue
= 0;
1115 info
->bmiHeader
.biBitCount
= 1;
1116 info
->bmiHeader
.biClrUsed
= 0;
1117 info
->bmiHeader
.biSizeImage
= width_bytes
* height
;
1119 /* generate mask from the alpha channel if we have one */
1121 for (i
= 0, ptr
= color_bits
; i
< width
* height
; i
++, ptr
++)
1122 if ((has_alpha
= (*ptr
& 0xff000000) != 0)) break;
1126 memset( mask_bits
, 0, width_bytes
* height
);
1127 for (y
= 0, ptr
= color_bits
; y
< height
; y
++)
1128 for (x
= 0; x
< width
; x
++, ptr
++)
1129 if ((*ptr
>> 24) > 25) /* more than 10% alpha */
1130 mask_bits
[y
* width_bytes
+ x
/ 8] |= 0x80 >> (x
% 8);
1132 else /* invert the mask */
1134 ptr
= (unsigned int *)mask_bits
;
1135 for (i
= 0; i
< info
->bmiHeader
.biSizeImage
/ sizeof(*ptr
); i
++, ptr
++) *ptr
^= ~0u;
1139 bits
.ptr
= xor_bits
;
1141 bits
.is_copy
= TRUE
;
1142 if (!(xor_pixmap
= create_pixmap_from_image( hdc
, &vis
, info
, &bits
, DIB_RGB_COLORS
))) goto done
;
1144 bits
.ptr
= mask_bits
;
1145 mask_pixmap
= create_pixmap_from_image( hdc
, &vis
, info
, &bits
, DIB_RGB_COLORS
);
1150 cursor
= XCreatePixmapCursor( gdi_display
, xor_pixmap
, mask_pixmap
,
1151 &fg
, &bg
, icon
->xHotspot
, icon
->yHotspot
);
1152 XFreePixmap( gdi_display
, mask_pixmap
);
1154 XFreePixmap( gdi_display
, xor_pixmap
);
1155 wine_tsx11_unlock();
1158 HeapFree( GetProcessHeap(), 0, color_bits
);
1159 HeapFree( GetProcessHeap(), 0, xor_bits
);
1160 HeapFree( GetProcessHeap(), 0, mask_bits
);
1164 /***********************************************************************
1167 * Create an X cursor from a Windows one.
1169 static Cursor
create_cursor( HANDLE handle
)
1176 if (!handle
) return get_empty_cursor();
1178 info
.cbSize
= sizeof(info
);
1179 if (!GetIconInfoExW( handle
, &info
)) return 0;
1181 #ifdef SONAME_LIBXCURSOR
1182 if (use_system_cursors
&& (cursor
= create_xcursor_system_cursor( &info
)))
1184 DeleteObject( info
.hbmColor
);
1185 DeleteObject( info
.hbmMask
);
1190 GetObjectW( info
.hbmMask
, sizeof(bm
), &bm
);
1191 if (!info
.hbmColor
) bm
.bmHeight
= max( 1, bm
.bmHeight
/ 2 );
1193 /* make sure hotspot is valid */
1194 if (info
.xHotspot
>= bm
.bmWidth
|| info
.yHotspot
>= bm
.bmHeight
)
1196 info
.xHotspot
= bm
.bmWidth
/ 2;
1197 info
.yHotspot
= bm
.bmHeight
/ 2;
1200 hdc
= CreateCompatibleDC( 0 );
1204 #ifdef SONAME_LIBXCURSOR
1205 if (pXcursorImagesLoadCursor
)
1206 cursor
= create_xcursor_cursor( hdc
, &info
, handle
, bm
.bmWidth
, bm
.bmHeight
);
1208 if (!cursor
) cursor
= create_xlib_color_cursor( hdc
, &info
, bm
.bmWidth
, bm
.bmHeight
);
1209 DeleteObject( info
.hbmColor
);
1213 cursor
= create_xlib_monochrome_cursor( hdc
, &info
, bm
.bmWidth
, bm
.bmHeight
);
1216 DeleteObject( info
.hbmMask
);
1221 /***********************************************************************
1222 * DestroyCursorIcon (X11DRV.@)
1224 void CDECL
X11DRV_DestroyCursorIcon( HCURSOR handle
)
1229 if (cursor_context
&& !XFindContext( gdi_display
, (XID
)handle
, cursor_context
, (char **)&cursor
))
1231 TRACE( "%p xid %lx\n", handle
, cursor
);
1232 XFreeCursor( gdi_display
, cursor
);
1233 XDeleteContext( gdi_display
, (XID
)handle
, cursor_context
);
1235 wine_tsx11_unlock();
1238 /***********************************************************************
1239 * SetCursor (X11DRV.@)
1241 void CDECL
X11DRV_SetCursor( HCURSOR handle
)
1243 if (InterlockedExchangePointer( (void **)&last_cursor
, handle
) != handle
||
1244 GetTickCount() - last_cursor_change
> 100)
1246 last_cursor_change
= GetTickCount();
1247 if (cursor_window
) SendNotifyMessageW( cursor_window
, WM_X11DRV_SET_CURSOR
, 0, (LPARAM
)handle
);
1251 /***********************************************************************
1252 * SetCursorPos (X11DRV.@)
1254 BOOL CDECL
X11DRV_SetCursorPos( INT x
, INT y
)
1256 struct x11drv_thread_data
*data
= x11drv_init_thread_data();
1259 XWarpPointer( data
->display
, root_window
, root_window
, 0, 0, 0, 0,
1260 x
- virtual_screen_rect
.left
, y
- virtual_screen_rect
.top
);
1261 data
->warp_serial
= NextRequest( data
->display
);
1262 XNoOp( data
->display
);
1263 XFlush( data
->display
); /* avoids bad mouse lag in games that do their own mouse warping */
1264 wine_tsx11_unlock();
1265 TRACE( "warped to %d,%d serial %lu\n", x
, y
, data
->warp_serial
);
1269 /***********************************************************************
1270 * GetCursorPos (X11DRV.@)
1272 BOOL CDECL
X11DRV_GetCursorPos(LPPOINT pos
)
1274 Display
*display
= thread_init_display();
1276 int rootX
, rootY
, winX
, winY
;
1277 unsigned int xstate
;
1281 ret
= XQueryPointer( display
, root_window
, &root
, &child
, &rootX
, &rootY
, &winX
, &winY
, &xstate
);
1285 pos
->x
= winX
+ virtual_screen_rect
.left
;
1286 pos
->y
= winY
+ virtual_screen_rect
.top
;
1287 TRACE( "pointer at (%d,%d) server pos %d,%d\n", pos
->x
, pos
->y
, old
.x
, old
.y
);
1289 wine_tsx11_unlock();
1293 /***********************************************************************
1294 * ClipCursor (X11DRV.@)
1296 BOOL CDECL
X11DRV_ClipCursor( LPCRECT clip
)
1300 ungrab_clipping_window();
1304 if (GetWindowThreadProcessId( GetDesktopWindow(), NULL
) == GetCurrentThreadId())
1305 return TRUE
; /* don't clip in the desktop process */
1309 HWND foreground
= GetForegroundWindow();
1311 /* we are clipping if the clip rectangle is smaller than the screen */
1312 if (clip
->left
> virtual_screen_rect
.left
|| clip
->right
< virtual_screen_rect
.right
||
1313 clip
->top
> virtual_screen_rect
.top
|| clip
->bottom
< virtual_screen_rect
.bottom
)
1317 /* forward request to the foreground window if it's in a different thread */
1318 tid
= GetWindowThreadProcessId( foreground
, &pid
);
1319 if (tid
&& tid
!= GetCurrentThreadId() && pid
== GetCurrentProcessId())
1321 TRACE( "forwarding clip request to %p\n", foreground
);
1322 SendNotifyMessageW( foreground
, WM_X11DRV_CLIP_CURSOR
, 0, 0 );
1325 else if (grab_clipping_window( clip
)) return TRUE
;
1327 else /* if currently clipping, check if we should switch to fullscreen clipping */
1329 struct x11drv_thread_data
*data
= x11drv_thread_data();
1330 if (data
&& data
->clip_hwnd
)
1332 if (EqualRect( clip
, &clip_rect
) || clip_fullscreen_window( foreground
, TRUE
))
1337 ungrab_clipping_window();
1341 /***********************************************************************
1342 * move_resize_window
1344 void move_resize_window( Display
*display
, struct x11drv_win_data
*data
, int dir
)
1347 int x
, y
, rootX
, rootY
, ret
, button
= 0;
1350 unsigned int xstate
;
1352 pt
= GetMessagePos();
1353 x
= (short)LOWORD( pt
);
1354 y
= (short)HIWORD( pt
);
1356 if (GetKeyState( VK_LBUTTON
) & 0x8000) button
= 1;
1357 else if (GetKeyState( VK_MBUTTON
) & 0x8000) button
= 2;
1358 else if (GetKeyState( VK_RBUTTON
) & 0x8000) button
= 3;
1360 TRACE( "hwnd %p/%lx, x %d, y %d, dir %d, button %d\n",
1361 data
->hwnd
, data
->whole_window
, x
, y
, dir
, button
);
1363 xev
.xclient
.type
= ClientMessage
;
1364 xev
.xclient
.window
= data
->whole_window
;
1365 xev
.xclient
.message_type
= x11drv_atom(_NET_WM_MOVERESIZE
);
1366 xev
.xclient
.serial
= 0;
1367 xev
.xclient
.display
= display
;
1368 xev
.xclient
.send_event
= True
;
1369 xev
.xclient
.format
= 32;
1370 xev
.xclient
.data
.l
[0] = x
- virtual_screen_rect
.left
; /* x coord */
1371 xev
.xclient
.data
.l
[1] = y
- virtual_screen_rect
.top
; /* y coord */
1372 xev
.xclient
.data
.l
[2] = dir
; /* direction */
1373 xev
.xclient
.data
.l
[3] = button
; /* button */
1374 xev
.xclient
.data
.l
[4] = 0; /* unused */
1376 /* need to ungrab the pointer that may have been automatically grabbed
1377 * with a ButtonPress event */
1379 XUngrabPointer( display
, CurrentTime
);
1380 XSendEvent(display
, root_window
, False
, SubstructureNotifyMask
| SubstructureRedirectMask
, &xev
);
1381 wine_tsx11_unlock();
1383 /* try to detect the end of the size/move by polling for the mouse button to be released */
1384 /* (some apps don't like it if we return before the size/move is done) */
1386 if (!button
) return;
1393 ret
= XQueryPointer( display
, root_window
, &root
, &child
, &rootX
, &rootY
, &x
, &y
, &xstate
);
1394 wine_tsx11_unlock();
1397 if (!(xstate
& (Button1Mask
<< (button
- 1))))
1399 /* fake a button release event */
1400 input
.type
= INPUT_MOUSE
;
1401 input
.u
.mi
.dx
= x
+ virtual_screen_rect
.left
;
1402 input
.u
.mi
.dy
= y
+ virtual_screen_rect
.top
;
1403 input
.u
.mi
.mouseData
= button_up_data
[button
- 1];
1404 input
.u
.mi
.dwFlags
= button_up_flags
[button
- 1] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
;
1405 input
.u
.mi
.time
= GetTickCount();
1406 input
.u
.mi
.dwExtraInfo
= 0;
1407 __wine_send_input( data
->hwnd
, &input
);
1410 while (PeekMessageW( &msg
, 0, 0, 0, PM_REMOVE
))
1412 if (!CallMsgFilterW( &msg
, MSGF_SIZE
))
1414 TranslateMessage( &msg
);
1415 DispatchMessageW( &msg
);
1419 if (!(xstate
& (Button1Mask
<< (button
- 1)))) break;
1420 MsgWaitForMultipleObjects( 0, NULL
, FALSE
, 100, QS_ALLINPUT
);
1423 TRACE( "hwnd %p/%lx done\n", data
->hwnd
, data
->whole_window
);
1427 /***********************************************************************
1428 * X11DRV_ButtonPress
1430 void X11DRV_ButtonPress( HWND hwnd
, XEvent
*xev
)
1432 XButtonEvent
*event
= &xev
->xbutton
;
1433 int buttonNum
= event
->button
- 1;
1436 if (buttonNum
>= NB_BUTTONS
) return;
1438 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd
, event
->window
, buttonNum
, event
->x
, event
->y
);
1440 input
.u
.mi
.dx
= event
->x
;
1441 input
.u
.mi
.dy
= event
->y
;
1442 input
.u
.mi
.mouseData
= button_down_data
[buttonNum
];
1443 input
.u
.mi
.dwFlags
= button_down_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
;
1444 input
.u
.mi
.time
= EVENT_x11_time_to_win32_time( event
->time
);
1445 input
.u
.mi
.dwExtraInfo
= 0;
1447 update_user_time( event
->time
);
1448 send_mouse_input( hwnd
, event
->window
, event
->state
, &input
);
1452 /***********************************************************************
1453 * X11DRV_ButtonRelease
1455 void X11DRV_ButtonRelease( HWND hwnd
, XEvent
*xev
)
1457 XButtonEvent
*event
= &xev
->xbutton
;
1458 int buttonNum
= event
->button
- 1;
1461 if (buttonNum
>= NB_BUTTONS
|| !button_up_flags
[buttonNum
]) return;
1463 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd
, event
->window
, buttonNum
, event
->x
, event
->y
);
1465 input
.u
.mi
.dx
= event
->x
;
1466 input
.u
.mi
.dy
= event
->y
;
1467 input
.u
.mi
.mouseData
= button_up_data
[buttonNum
];
1468 input
.u
.mi
.dwFlags
= button_up_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
;
1469 input
.u
.mi
.time
= EVENT_x11_time_to_win32_time( event
->time
);
1470 input
.u
.mi
.dwExtraInfo
= 0;
1472 send_mouse_input( hwnd
, event
->window
, event
->state
, &input
);
1476 /***********************************************************************
1477 * X11DRV_MotionNotify
1479 void X11DRV_MotionNotify( HWND hwnd
, XEvent
*xev
)
1481 XMotionEvent
*event
= &xev
->xmotion
;
1484 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d serial %lu\n",
1485 hwnd
, event
->window
, event
->x
, event
->y
, event
->is_hint
, event
->serial
);
1487 input
.u
.mi
.dx
= event
->x
;
1488 input
.u
.mi
.dy
= event
->y
;
1489 input
.u
.mi
.mouseData
= 0;
1490 input
.u
.mi
.dwFlags
= MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
;
1491 input
.u
.mi
.time
= EVENT_x11_time_to_win32_time( event
->time
);
1492 input
.u
.mi
.dwExtraInfo
= 0;
1496 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
1497 if (thread_data
->warp_serial
&& (long)(event
->serial
- thread_data
->warp_serial
) < 0) return;
1500 send_mouse_input( hwnd
, event
->window
, event
->state
, &input
);
1504 /***********************************************************************
1505 * X11DRV_EnterNotify
1507 void X11DRV_EnterNotify( HWND hwnd
, XEvent
*xev
)
1509 XCrossingEvent
*event
= &xev
->xcrossing
;
1512 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd
, event
->window
, event
->x
, event
->y
, event
->detail
);
1514 if (event
->detail
== NotifyVirtual
|| event
->detail
== NotifyNonlinearVirtual
) return;
1515 if (event
->window
== x11drv_thread_data()->grab_window
) return;
1517 /* simulate a mouse motion event */
1518 input
.u
.mi
.dx
= event
->x
;
1519 input
.u
.mi
.dy
= event
->y
;
1520 input
.u
.mi
.mouseData
= 0;
1521 input
.u
.mi
.dwFlags
= MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
;
1522 input
.u
.mi
.time
= EVENT_x11_time_to_win32_time( event
->time
);
1523 input
.u
.mi
.dwExtraInfo
= 0;
1525 send_mouse_input( hwnd
, event
->window
, event
->state
, &input
);
1528 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1530 /***********************************************************************
1533 static void X11DRV_RawMotion( XGenericEventCookie
*xev
)
1535 XIRawEvent
*event
= xev
->data
;
1536 const double *values
= event
->valuators
.values
;
1539 double dx
= 0, dy
= 0;
1540 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
1542 if (!event
->valuators
.mask_len
) return;
1543 if (thread_data
->xi2_state
!= xi_enabled
) return;
1545 input
.u
.mi
.mouseData
= 0;
1546 input
.u
.mi
.dwFlags
= MOUSEEVENTF_MOVE
;
1547 input
.u
.mi
.time
= EVENT_x11_time_to_win32_time( event
->time
);
1548 input
.u
.mi
.dwExtraInfo
= 0;
1550 if (XIMaskIsSet( event
->valuators
.mask
, 0 )) dx
= *values
++;
1551 if (XIMaskIsSet( event
->valuators
.mask
, 1 )) dy
= *values
++;
1556 for (i
= 0; i
< xinput2_device_count
; ++i
)
1558 if (xinput2_devices
[i
].deviceid
!= event
->deviceid
) continue;
1559 for (j
= 0; j
< xinput2_devices
[i
].num_classes
; j
++)
1561 XIValuatorClassInfo
*class = (XIValuatorClassInfo
*)xinput2_devices
[i
].classes
[j
];
1563 if (xinput2_devices
[i
].classes
[j
]->type
!= XIValuatorClass
) continue;
1564 if (class->min
>= class->max
) continue;
1565 if (class->number
== 0)
1566 input
.u
.mi
.dx
= dx
* (virtual_screen_rect
.right
- virtual_screen_rect
.left
)
1567 / (class->max
- class->min
);
1568 else if (class->number
== 1)
1569 input
.u
.mi
.dy
= dy
* (virtual_screen_rect
.bottom
- virtual_screen_rect
.top
)
1570 / (class->max
- class->min
);
1575 wine_tsx11_unlock();
1577 if (thread_data
->warp_serial
)
1579 if ((long)(xev
->serial
- thread_data
->warp_serial
) < 0)
1581 TRACE( "pos %d,%d old serial %lu, ignoring\n", input
.u
.mi
.dx
, input
.u
.mi
.dy
, xev
->serial
);
1584 thread_data
->warp_serial
= 0; /* we caught up now */
1587 TRACE( "pos %d,%d (event %f,%f)\n", input
.u
.mi
.dx
, input
.u
.mi
.dy
, dx
, dy
);
1589 input
.type
= INPUT_MOUSE
;
1590 __wine_send_input( 0, &input
);
1593 #endif /* HAVE_X11_EXTENSIONS_XINPUT2_H */
1596 /***********************************************************************
1597 * X11DRV_XInput2_Init
1599 void X11DRV_XInput2_Init(void)
1601 #if defined(SONAME_LIBXI) && defined(HAVE_X11_EXTENSIONS_XINPUT2_H)
1603 void *libxi_handle
= wine_dlopen( SONAME_LIBXI
, RTLD_NOW
, NULL
, 0 );
1607 WARN( "couldn't load %s\n", SONAME_LIBXI
);
1610 #define LOAD_FUNCPTR(f) \
1611 if (!(p##f = wine_dlsym( libxi_handle, #f, NULL, 0))) \
1613 WARN("Failed to load %s.\n", #f); \
1617 LOAD_FUNCPTR(XIFreeDeviceInfo
);
1618 LOAD_FUNCPTR(XIQueryDevice
);
1619 LOAD_FUNCPTR(XIQueryVersion
);
1620 LOAD_FUNCPTR(XISelectEvents
);
1624 xinput2_available
= XQueryExtension( gdi_display
, "XInputExtension", &xinput2_opcode
, &event
, &error
);
1625 wine_tsx11_unlock();
1627 TRACE( "X Input 2 support not compiled in.\n" );
1632 /***********************************************************************
1633 * X11DRV_GenericEvent
1635 void X11DRV_GenericEvent( HWND hwnd
, XEvent
*xev
)
1637 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1638 XGenericEventCookie
*event
= &xev
->xcookie
;
1640 if (!event
->data
) return;
1641 if (event
->extension
!= xinput2_opcode
) return;
1643 switch (event
->evtype
)
1646 X11DRV_RawMotion( event
);
1650 TRACE( "Unhandled event %#x\n", event
->evtype
);