4 * Copyright 1998 Ulrich Weigand
5 * Copyright 2007 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
26 #include <X11/cursorfont.h>
28 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
29 #include <X11/extensions/XInput2.h>
32 #ifdef SONAME_LIBXCURSOR
33 # include <X11/Xcursor/Xcursor.h>
34 static void *xcursor_handle
;
35 # define MAKE_FUNCPTR(f) static typeof(f) * p##f
36 MAKE_FUNCPTR(XcursorImageCreate
);
37 MAKE_FUNCPTR(XcursorImageDestroy
);
38 MAKE_FUNCPTR(XcursorImageLoadCursor
);
39 MAKE_FUNCPTR(XcursorImagesCreate
);
40 MAKE_FUNCPTR(XcursorImagesDestroy
);
41 MAKE_FUNCPTR(XcursorImagesLoadCursor
);
42 MAKE_FUNCPTR(XcursorLibraryLoadCursor
);
44 #endif /* SONAME_LIBXCURSOR */
46 #define NONAMELESSUNION
47 #define NONAMELESSSTRUCT
54 #include "wine/server.h"
55 #include "wine/library.h"
56 #include "wine/unicode.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(cursor
);
61 /**********************************************************************/
64 #define Button6Mask (1<<13)
67 #define Button7Mask (1<<14)
70 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
72 static const UINT button_down_flags
[NB_BUTTONS
] =
75 MOUSEEVENTF_MIDDLEDOWN
,
76 MOUSEEVENTF_RIGHTDOWN
,
79 MOUSEEVENTF_XDOWN
, /* FIXME: horizontal wheel */
85 static const UINT button_up_flags
[NB_BUTTONS
] =
98 static const UINT button_down_data
[NB_BUTTONS
] =
111 static const UINT button_up_data
[NB_BUTTONS
] =
124 XContext cursor_context
= 0;
126 static HWND cursor_window
;
127 static HCURSOR last_cursor
;
128 static DWORD last_cursor_change
;
129 static RECT clip_rect
;
130 static Cursor
create_cursor( HANDLE handle
);
132 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
133 static BOOL xinput2_available
;
134 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
135 MAKE_FUNCPTR(XIFreeDeviceInfo
);
136 MAKE_FUNCPTR(XIQueryDevice
);
137 MAKE_FUNCPTR(XIQueryVersion
);
138 MAKE_FUNCPTR(XISelectEvents
);
142 /***********************************************************************
143 * X11DRV_Xcursor_Init
145 * Load the Xcursor library for use.
147 void X11DRV_Xcursor_Init(void)
149 #ifdef SONAME_LIBXCURSOR
150 xcursor_handle
= wine_dlopen(SONAME_LIBXCURSOR
, RTLD_NOW
, NULL
, 0);
151 if (!xcursor_handle
) /* wine_dlopen failed. */
153 WARN("Xcursor failed to load. Using fallback code.\n");
156 #define LOAD_FUNCPTR(f) \
157 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
159 LOAD_FUNCPTR(XcursorImageCreate
);
160 LOAD_FUNCPTR(XcursorImageDestroy
);
161 LOAD_FUNCPTR(XcursorImageLoadCursor
);
162 LOAD_FUNCPTR(XcursorImagesCreate
);
163 LOAD_FUNCPTR(XcursorImagesDestroy
);
164 LOAD_FUNCPTR(XcursorImagesLoadCursor
);
165 LOAD_FUNCPTR(XcursorLibraryLoadCursor
);
167 #endif /* SONAME_LIBXCURSOR */
171 /***********************************************************************
174 static Cursor
get_empty_cursor(void)
176 static Cursor cursor
;
177 static const char data
[] = { 0 };
184 bg
.red
= bg
.green
= bg
.blue
= 0x0000;
185 pixmap
= XCreateBitmapFromData( gdi_display
, root_window
, data
, 1, 1 );
188 Cursor
new = XCreatePixmapCursor( gdi_display
, pixmap
, pixmap
, &bg
, &bg
, 0, 0 );
189 if (InterlockedCompareExchangePointer( (void **)&cursor
, (void *)new, 0 ))
190 XFreeCursor( gdi_display
, new );
191 XFreePixmap( gdi_display
, pixmap
);
197 /***********************************************************************
200 void set_window_cursor( Window window
, HCURSOR handle
)
204 if (!handle
) cursor
= get_empty_cursor();
205 else if (XFindContext( gdi_display
, (XID
)handle
, cursor_context
, (char **)&cursor
))
207 /* try to create it */
208 if (!(cursor
= create_cursor( handle
))) return;
210 XLockDisplay( gdi_display
);
211 if (!XFindContext( gdi_display
, (XID
)handle
, cursor_context
, (char **)&prev
))
213 /* someone else was here first */
214 XFreeCursor( gdi_display
, cursor
);
219 XSaveContext( gdi_display
, (XID
)handle
, cursor_context
, (char *)cursor
);
220 TRACE( "cursor %p created %lx\n", handle
, cursor
);
222 XUnlockDisplay( gdi_display
);
225 XDefineCursor( gdi_display
, window
, cursor
);
226 /* make the change take effect immediately */
227 XFlush( gdi_display
);
230 /***********************************************************************
233 void sync_window_cursor( Window window
)
237 SERVER_START_REQ( set_cursor
)
240 wine_server_call( req
);
241 cursor
= reply
->prev_count
>= 0 ? wine_server_ptr_handle( reply
->prev_handle
) : 0;
245 set_window_cursor( window
, cursor
);
248 /***********************************************************************
251 static void enable_xinput2(void)
253 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
254 struct x11drv_thread_data
*data
= x11drv_thread_data();
256 XIDeviceInfo
*devices
;
257 unsigned char mask_bits
[XIMaskLen(XI_LASTEVENT
)];
260 if (!xinput2_available
) return;
262 if (data
->xi2_state
== xi_unknown
)
264 int major
= 2, minor
= 0;
265 if (!pXIQueryVersion( data
->display
, &major
, &minor
)) data
->xi2_state
= xi_disabled
;
268 data
->xi2_state
= xi_unavailable
;
269 WARN( "X Input 2 not available\n" );
272 if (data
->xi2_state
== xi_unavailable
) return;
274 if (data
->xi2_devices
) pXIFreeDeviceInfo( data
->xi2_devices
);
275 data
->xi2_devices
= devices
= pXIQueryDevice( data
->display
, XIAllDevices
, &data
->xi2_device_count
);
276 for (i
= 0; i
< data
->xi2_device_count
; ++i
)
278 if (devices
[i
].use
!= XIMasterPointer
) continue;
279 for (j
= count
= 0; j
< devices
[i
].num_classes
; j
++)
281 XIValuatorClassInfo
*class = (XIValuatorClassInfo
*)devices
[i
].classes
[j
];
283 if (devices
[i
].classes
[j
]->type
!= XIValuatorClass
) continue;
284 TRACE( "Device %u (%s) num %u %f,%f res %u mode %u label %s\n",
285 devices
[i
].deviceid
, debugstr_a(devices
[i
].name
),
286 class->number
, class->min
, class->max
, class->resolution
, class->mode
,
287 XGetAtomName( data
->display
, class->label
));
288 if (class->label
== x11drv_atom( Rel_X
) || class->label
== x11drv_atom( Rel_Y
)) count
++;
290 if (count
< 2) continue;
291 TRACE( "Using %u (%s) as core pointer\n",
292 devices
[i
].deviceid
, debugstr_a(devices
[i
].name
) );
293 data
->xi2_core_pointer
= devices
[i
].deviceid
;
297 mask
.mask
= mask_bits
;
298 mask
.mask_len
= sizeof(mask_bits
);
299 memset( mask_bits
, 0, sizeof(mask_bits
) );
300 XISetMask( mask_bits
, XI_RawMotion
);
301 XISetMask( mask_bits
, XI_ButtonPress
);
303 for (i
= 0; i
< data
->xi2_device_count
; ++i
)
305 if (devices
[i
].use
== XISlavePointer
&& devices
[i
].attachment
== data
->xi2_core_pointer
)
307 TRACE( "Device %u (%s) is attached to the core pointer\n",
308 devices
[i
].deviceid
, debugstr_a(devices
[i
].name
) );
309 mask
.deviceid
= devices
[i
].deviceid
;
310 pXISelectEvents( data
->display
, DefaultRootWindow( data
->display
), &mask
, 1 );
311 data
->xi2_state
= xi_enabled
;
317 /***********************************************************************
320 static void disable_xinput2(void)
322 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
323 struct x11drv_thread_data
*data
= x11drv_thread_data();
324 XIDeviceInfo
*devices
= data
->xi2_devices
;
328 if (data
->xi2_state
!= xi_enabled
) return;
330 TRACE( "disabling\n" );
331 data
->xi2_state
= xi_disabled
;
336 for (i
= 0; i
< data
->xi2_device_count
; ++i
)
338 if (devices
[i
].use
== XISlavePointer
&& devices
[i
].attachment
== data
->xi2_core_pointer
)
340 mask
.deviceid
= devices
[i
].deviceid
;
341 pXISelectEvents( data
->display
, DefaultRootWindow( data
->display
), &mask
, 1 );
344 pXIFreeDeviceInfo( devices
);
345 data
->xi2_devices
= NULL
;
346 data
->xi2_device_count
= 0;
351 /***********************************************************************
352 * grab_clipping_window
354 * Start a pointer grab on the clip window.
356 static BOOL
grab_clipping_window( const RECT
*clip
)
358 static const WCHAR messageW
[] = {'M','e','s','s','a','g','e',0};
359 struct x11drv_thread_data
*data
= x11drv_thread_data();
363 if (!data
) return FALSE
;
364 if (!(clip_window
= init_clip_window())) return TRUE
;
366 if (!(msg_hwnd
= CreateWindowW( messageW
, NULL
, 0, 0, 0, 0, 0, HWND_MESSAGE
, 0,
367 GetModuleHandleW(0), NULL
)))
370 /* enable XInput2 unless we are already clipping */
371 if (!data
->clip_hwnd
) enable_xinput2();
373 if (data
->xi2_state
!= xi_enabled
)
375 WARN( "XInput2 not supported, refusing to clip to %s\n", wine_dbgstr_rect(clip
) );
376 DestroyWindow( msg_hwnd
);
381 TRACE( "clipping to %s win %lx\n", wine_dbgstr_rect(clip
), clip_window
);
383 if (!data
->clip_hwnd
) XUnmapWindow( data
->display
, clip_window
);
384 XMoveResizeWindow( data
->display
, clip_window
,
385 clip
->left
- virtual_screen_rect
.left
, clip
->top
- virtual_screen_rect
.top
,
386 max( 1, clip
->right
- clip
->left
), max( 1, clip
->bottom
- clip
->top
) );
387 XMapWindow( data
->display
, clip_window
);
389 /* if the rectangle is shrinking we may get a pointer warp */
390 if (!data
->clip_hwnd
|| clip
->left
> clip_rect
.left
|| clip
->top
> clip_rect
.top
||
391 clip
->right
< clip_rect
.right
|| clip
->bottom
< clip_rect
.bottom
)
392 data
->warp_serial
= NextRequest( data
->display
);
394 if (!XGrabPointer( data
->display
, clip_window
, False
,
395 PointerMotionMask
| ButtonPressMask
| ButtonReleaseMask
,
396 GrabModeAsync
, GrabModeAsync
, clip_window
, None
, CurrentTime
))
399 if (!clipping_cursor
)
402 DestroyWindow( msg_hwnd
);
406 if (!data
->clip_hwnd
) sync_window_cursor( clip_window
);
407 InterlockedExchangePointer( (void **)&cursor_window
, msg_hwnd
);
408 data
->clip_hwnd
= msg_hwnd
;
409 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR
, 0, (LPARAM
)msg_hwnd
);
413 /***********************************************************************
414 * ungrab_clipping_window
416 * Release the pointer grab on the clip window.
418 void ungrab_clipping_window(void)
420 Display
*display
= thread_init_display();
421 Window clip_window
= init_clip_window();
423 if (!clip_window
) return;
425 TRACE( "no longer clipping\n" );
426 XUnmapWindow( display
, clip_window
);
428 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR
, 0, 0 );
431 /***********************************************************************
432 * reset_clipping_window
434 * Forcibly reset the window clipping on external events.
436 void reset_clipping_window(void)
438 ungrab_clipping_window();
439 ClipCursor( NULL
); /* make sure the clip rectangle is reset too */
442 /***********************************************************************
445 * Notification function called upon receiving a WM_X11DRV_CLIP_CURSOR.
447 LRESULT
clip_cursor_notify( HWND hwnd
, HWND new_clip_hwnd
)
449 struct x11drv_thread_data
*data
= x11drv_thread_data();
451 if (hwnd
== GetDesktopWindow()) /* change the clip window stored in the desktop process */
453 static HWND clip_hwnd
;
455 HWND prev
= clip_hwnd
;
456 clip_hwnd
= new_clip_hwnd
;
457 if (prev
|| new_clip_hwnd
) TRACE( "clip hwnd changed from %p to %p\n", prev
, new_clip_hwnd
);
458 if (prev
) SendNotifyMessageW( prev
, WM_X11DRV_CLIP_CURSOR
, 0, 0 );
460 else if (hwnd
== data
->clip_hwnd
) /* this is a notification that clipping has been reset */
462 TRACE( "clip hwnd reset from %p\n", hwnd
);
464 data
->clip_reset
= GetTickCount();
466 DestroyWindow( hwnd
);
468 else if (hwnd
== GetForegroundWindow()) /* request to clip */
472 GetClipCursor( &clip
);
473 if (clip
.left
> virtual_screen_rect
.left
|| clip
.right
< virtual_screen_rect
.right
||
474 clip
.top
> virtual_screen_rect
.top
|| clip
.bottom
< virtual_screen_rect
.bottom
)
475 return grab_clipping_window( &clip
);
480 /***********************************************************************
481 * clip_fullscreen_window
483 * Turn on clipping if the active window is fullscreen.
485 BOOL
clip_fullscreen_window( HWND hwnd
, BOOL reset
)
487 struct x11drv_win_data
*data
;
488 struct x11drv_thread_data
*thread_data
;
492 if (hwnd
== GetDesktopWindow()) return FALSE
;
493 if (!(data
= X11DRV_get_win_data( hwnd
))) return FALSE
;
494 style
= GetWindowLongW( hwnd
, GWL_STYLE
);
495 if (!(style
& WS_VISIBLE
)) return FALSE
;
496 if ((style
& (WS_POPUP
| WS_CHILD
)) == WS_CHILD
) return FALSE
;
497 /* maximized windows don't count as full screen */
498 if ((style
& WS_MAXIMIZE
) && (style
& WS_CAPTION
) == WS_CAPTION
) return FALSE
;
499 if (!is_window_rect_fullscreen( &data
->whole_rect
)) return FALSE
;
500 if (!(thread_data
= x11drv_thread_data())) return FALSE
;
501 if (GetTickCount() - thread_data
->clip_reset
< 1000) return FALSE
;
502 if (!reset
&& clipping_cursor
&& thread_data
->clip_hwnd
) return FALSE
; /* already clipping */
503 SetRect( &rect
, 0, 0, screen_width
, screen_height
);
504 if (!grab_fullscreen
)
506 if (!EqualRect( &rect
, &virtual_screen_rect
)) return FALSE
;
507 if (root_window
!= DefaultRootWindow( gdi_display
)) return FALSE
;
509 TRACE( "win %p clipping fullscreen\n", hwnd
);
510 return grab_clipping_window( &rect
);
513 /***********************************************************************
516 * Update the various window states on a mouse event.
518 static void send_mouse_input( HWND hwnd
, Window window
, unsigned int state
, INPUT
*input
)
520 struct x11drv_win_data
*data
;
523 input
->type
= INPUT_MOUSE
;
527 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
528 HWND clip_hwnd
= thread_data
->clip_hwnd
;
530 if (!clip_hwnd
) return;
531 if (thread_data
->clip_window
!= window
) return;
532 if (InterlockedExchangePointer( (void **)&cursor_window
, clip_hwnd
) != clip_hwnd
||
533 input
->u
.mi
.time
- last_cursor_change
> 100)
535 sync_window_cursor( window
);
536 last_cursor_change
= input
->u
.mi
.time
;
538 input
->u
.mi
.dx
+= clip_rect
.left
;
539 input
->u
.mi
.dy
+= clip_rect
.top
;
540 __wine_send_input( hwnd
, input
);
544 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
546 if (window
== data
->whole_window
)
548 input
->u
.mi
.dx
+= data
->whole_rect
.left
- data
->client_rect
.left
;
549 input
->u
.mi
.dy
+= data
->whole_rect
.top
- data
->client_rect
.top
;
551 if (window
== root_window
)
553 input
->u
.mi
.dx
+= virtual_screen_rect
.left
;
554 input
->u
.mi
.dy
+= virtual_screen_rect
.top
;
556 pt
.x
= input
->u
.mi
.dx
;
557 pt
.y
= input
->u
.mi
.dy
;
558 if (GetWindowLongW( data
->hwnd
, GWL_EXSTYLE
) & WS_EX_LAYOUTRTL
)
559 pt
.x
= data
->client_rect
.right
- data
->client_rect
.left
- 1 - pt
.x
;
560 MapWindowPoints( hwnd
, 0, &pt
, 1 );
562 if (InterlockedExchangePointer( (void **)&cursor_window
, hwnd
) != hwnd
||
563 input
->u
.mi
.time
- last_cursor_change
> 100)
565 sync_window_cursor( data
->whole_window
);
566 last_cursor_change
= input
->u
.mi
.time
;
569 if (hwnd
!= GetDesktopWindow())
571 hwnd
= GetAncestor( hwnd
, GA_ROOT
);
572 if ((input
->u
.mi
.dwFlags
& (MOUSEEVENTF_LEFTDOWN
|MOUSEEVENTF_RIGHTDOWN
)) && hwnd
== GetForegroundWindow())
573 clip_fullscreen_window( hwnd
, FALSE
);
576 /* update the wine server Z-order */
578 if (window
!= x11drv_thread_data()->grab_window
&&
579 /* ignore event if a button is pressed, since the mouse is then grabbed too */
580 !(state
& (Button1Mask
|Button2Mask
|Button3Mask
|Button4Mask
|Button5Mask
|Button6Mask
|Button7Mask
)))
583 SetRect( &rect
, pt
.x
, pt
.y
, pt
.x
+ 1, pt
.y
+ 1 );
584 MapWindowPoints( 0, hwnd
, (POINT
*)&rect
, 2 );
586 SERVER_START_REQ( update_window_zorder
)
588 req
->window
= wine_server_user_handle( hwnd
);
589 req
->rect
.left
= rect
.left
;
590 req
->rect
.top
= rect
.top
;
591 req
->rect
.right
= rect
.right
;
592 req
->rect
.bottom
= rect
.bottom
;
593 wine_server_call( req
);
598 input
->u
.mi
.dx
= pt
.x
;
599 input
->u
.mi
.dy
= pt
.y
;
600 __wine_send_input( hwnd
, input
);
603 #ifdef SONAME_LIBXCURSOR
605 /***********************************************************************
606 * create_xcursor_frame
608 * Use Xcursor to create a frame of an X cursor from a Windows one.
610 static XcursorImage
*create_xcursor_frame( HDC hdc
, const ICONINFOEXW
*iinfo
, HANDLE icon
,
611 HBITMAP hbmColor
, unsigned char *color_bits
, int color_size
,
612 HBITMAP hbmMask
, unsigned char *mask_bits
, int mask_size
,
613 int width
, int height
, int istep
)
615 XcursorImage
*image
, *ret
= NULL
;
616 DWORD delay_jiffies
, num_steps
;
617 int x
, y
, i
, has_alpha
= FALSE
;
620 image
= pXcursorImageCreate( width
, height
);
623 ERR("X11 failed to produce a cursor frame!\n");
627 image
->xhot
= iinfo
->xHotspot
;
628 image
->yhot
= iinfo
->yHotspot
;
630 image
->delay
= 100; /* fallback delay, 100 ms */
631 if (GetCursorFrameInfo(icon
, 0x0 /* unknown parameter */, istep
, &delay_jiffies
, &num_steps
) != 0)
632 image
->delay
= (100 * delay_jiffies
) / 6; /* convert jiffies (1/60s) to milliseconds */
634 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep
);
636 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
637 memset( color_bits
, 0x00, color_size
);
638 SelectObject( hdc
, hbmColor
);
639 if (!DrawIconEx( hdc
, 0, 0, icon
, width
, height
, istep
, NULL
, DI_NORMAL
))
641 TRACE("Could not draw frame %d (walk past end of frames).\n", istep
);
644 memcpy( image
->pixels
, color_bits
, color_size
);
646 /* check if the cursor frame was drawn with an alpha channel */
647 for (i
= 0, ptr
= image
->pixels
; i
< width
* height
; i
++, ptr
++)
648 if ((has_alpha
= (*ptr
& 0xff000000) != 0)) break;
650 /* if no alpha channel was drawn then generate it from the mask */
653 unsigned int width_bytes
= (width
+ 31) / 32 * 4;
655 /* draw the cursor mask to a temporary buffer */
656 memset( mask_bits
, 0xFF, mask_size
);
657 SelectObject( hdc
, hbmMask
);
658 if (!DrawIconEx( hdc
, 0, 0, icon
, width
, height
, istep
, NULL
, DI_MASK
))
660 ERR("Failed to draw frame mask %d.\n", istep
);
663 /* use the buffer to directly modify the XcursorImage alpha channel */
664 for (y
= 0, ptr
= image
->pixels
; y
< height
; y
++)
665 for (x
= 0; x
< width
; x
++, ptr
++)
666 if (!((mask_bits
[y
* width_bytes
+ x
/ 8] << (x
% 8)) & 0x80))
672 if (ret
== NULL
) pXcursorImageDestroy( image
);
676 /***********************************************************************
677 * create_xcursor_cursor
679 * Use Xcursor to create an X cursor from a Windows one.
681 static Cursor
create_xcursor_cursor( HDC hdc
, const ICONINFOEXW
*iinfo
, HANDLE icon
, int width
, int height
)
683 unsigned char *color_bits
, *mask_bits
;
684 HBITMAP hbmColor
= 0, hbmMask
= 0;
685 DWORD nFrames
, delay_jiffies
, i
;
686 int color_size
, mask_size
;
687 BITMAPINFO
*info
= NULL
;
688 XcursorImages
*images
;
692 /* Retrieve the number of frames to render */
693 if (!GetCursorFrameInfo(icon
, 0x0 /* unknown parameter */, 0, &delay_jiffies
, &nFrames
)) return 0;
694 if (!(imgs
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(XcursorImage
*)*nFrames
))) return 0;
696 /* Allocate all of the resources necessary to obtain a cursor frame */
697 if (!(info
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )))) goto cleanup
;
698 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
699 info
->bmiHeader
.biWidth
= width
;
700 info
->bmiHeader
.biHeight
= -height
;
701 info
->bmiHeader
.biPlanes
= 1;
702 info
->bmiHeader
.biCompression
= BI_RGB
;
703 info
->bmiHeader
.biXPelsPerMeter
= 0;
704 info
->bmiHeader
.biYPelsPerMeter
= 0;
705 info
->bmiHeader
.biClrUsed
= 0;
706 info
->bmiHeader
.biClrImportant
= 0;
707 info
->bmiHeader
.biBitCount
= 32;
708 color_size
= width
* height
* 4;
709 info
->bmiHeader
.biSizeImage
= color_size
;
710 hbmColor
= CreateDIBSection( hdc
, info
, DIB_RGB_COLORS
, (VOID
**) &color_bits
, NULL
, 0);
713 ERR("Failed to create DIB section for cursor color data!\n");
716 info
->bmiHeader
.biBitCount
= 1;
717 info
->bmiColors
[0].rgbRed
= 0;
718 info
->bmiColors
[0].rgbGreen
= 0;
719 info
->bmiColors
[0].rgbBlue
= 0;
720 info
->bmiColors
[0].rgbReserved
= 0;
721 info
->bmiColors
[1].rgbRed
= 0xff;
722 info
->bmiColors
[1].rgbGreen
= 0xff;
723 info
->bmiColors
[1].rgbBlue
= 0xff;
724 info
->bmiColors
[1].rgbReserved
= 0;
726 mask_size
= ((width
+ 31) / 32 * 4) * height
; /* width_bytes * height */
727 info
->bmiHeader
.biSizeImage
= mask_size
;
728 hbmMask
= CreateDIBSection( hdc
, info
, DIB_RGB_COLORS
, (VOID
**) &mask_bits
, NULL
, 0);
731 ERR("Failed to create DIB section for cursor mask data!\n");
735 /* Create an XcursorImage for each frame of the cursor */
736 for (i
=0; i
<nFrames
; i
++)
738 imgs
[i
] = create_xcursor_frame( hdc
, iinfo
, icon
,
739 hbmColor
, color_bits
, color_size
,
740 hbmMask
, mask_bits
, mask_size
,
742 if (!imgs
[i
]) goto cleanup
;
745 /* Build an X cursor out of all of the frames */
746 if (!(images
= pXcursorImagesCreate( nFrames
))) goto cleanup
;
747 for (images
->nimage
= 0; images
->nimage
< nFrames
; images
->nimage
++)
748 images
->images
[images
->nimage
] = imgs
[images
->nimage
];
749 cursor
= pXcursorImagesLoadCursor( gdi_display
, images
);
750 pXcursorImagesDestroy( images
); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
751 HeapFree( GetProcessHeap(), 0, imgs
);
757 /* Failed to produce a cursor, free previously allocated frames */
758 for (i
=0; i
<nFrames
&& imgs
[i
]; i
++)
759 pXcursorImageDestroy( imgs
[i
] );
760 HeapFree( GetProcessHeap(), 0, imgs
);
762 /* Cleanup all of the resources used to obtain the frame data */
763 if (hbmColor
) DeleteObject( hbmColor
);
764 if (hbmMask
) DeleteObject( hbmMask
);
765 HeapFree( GetProcessHeap(), 0, info
);
770 struct system_cursors
776 static const struct system_cursors user32_cursors
[] =
778 { OCR_NORMAL
, "left_ptr" },
779 { OCR_IBEAM
, "xterm" },
780 { OCR_WAIT
, "watch" },
781 { OCR_CROSS
, "cross" },
782 { OCR_UP
, "center_ptr" },
783 { OCR_SIZE
, "fleur" },
784 { OCR_SIZEALL
, "fleur" },
785 { OCR_ICON
, "icon" },
786 { OCR_SIZENWSE
, "nwse-resize" },
787 { OCR_SIZENESW
, "nesw-resize" },
788 { OCR_SIZEWE
, "ew-resize" },
789 { OCR_SIZENS
, "ns-resize" },
790 { OCR_NO
, "not-allowed" },
791 { OCR_HAND
, "hand2" },
792 { OCR_APPSTARTING
, "left_ptr_watch" },
793 { OCR_HELP
, "question_arrow" },
797 static const struct system_cursors comctl32_cursors
[] =
802 { 106, "row-resize" },
803 { 107, "row-resize" },
805 { 135, "col-resize" },
809 static const struct system_cursors ole32_cursors
[] =
818 static const struct system_cursors riched20_cursors
[] =
821 { 107, "right_ptr" },
830 const struct system_cursors
*cursors
;
834 { user32_cursors
, {'u','s','e','r','3','2','.','d','l','l',0} },
835 { comctl32_cursors
, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
836 { ole32_cursors
, {'o','l','e','3','2','.','d','l','l',0} },
837 { riched20_cursors
, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
840 /***********************************************************************
841 * create_xcursor_system_cursor
843 * Create an X cursor for a system cursor.
845 static Cursor
create_xcursor_system_cursor( const ICONINFOEXW
*info
)
847 static const WCHAR idW
[] = {'%','h','u',0};
848 const struct system_cursors
*cursors
;
853 WCHAR
*p
, name
[MAX_PATH
* 2], valueW
[64];
857 if (!pXcursorLibraryLoadCursor
) return 0;
858 if (!info
->szModName
[0]) return 0;
860 p
= strrchrW( info
->szModName
, '\\' );
861 strcpyW( name
, p
? p
+ 1 : info
->szModName
);
862 p
= name
+ strlenW( name
);
864 if (info
->szResName
[0]) strcpyW( p
, info
->szResName
);
865 else sprintfW( p
, idW
, info
->wResID
);
868 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
869 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\X11 Driver\\Cursors", &key
))
871 size
= sizeof(valueW
) / sizeof(WCHAR
);
872 ret
= RegQueryValueExW( key
, name
, NULL
, NULL
, (BYTE
*)valueW
, &size
);
876 if (!valueW
[0]) return 0; /* force standard cursor */
877 if (!WideCharToMultiByte( CP_UNIXCP
, 0, valueW
, -1, valueA
, sizeof(valueA
), NULL
, NULL
))
883 if (info
->szResName
[0]) goto done
; /* only integer resources are supported here */
884 if (!(module
= GetModuleHandleW( info
->szModName
))) goto done
;
886 for (i
= 0; i
< sizeof(module_cursors
)/sizeof(module_cursors
[0]); i
++)
887 if (GetModuleHandleW( module_cursors
[i
].name
) == module
) break;
888 if (i
== sizeof(module_cursors
)/sizeof(module_cursors
[0])) goto done
;
890 cursors
= module_cursors
[i
].cursors
;
891 for (i
= 0; cursors
[i
].id
; i
++)
892 if (cursors
[i
].id
== info
->wResID
)
894 strcpy( valueA
, cursors
[i
].name
);
901 cursor
= pXcursorLibraryLoadCursor( gdi_display
, valueA
);
902 if (!cursor
) WARN( "no system cursor found for %s mapped to %s\n",
903 debugstr_w(name
), debugstr_a(valueA
) );
905 else WARN( "no system cursor found for %s\n", debugstr_w(name
) );
909 #endif /* SONAME_LIBXCURSOR */
912 /***********************************************************************
913 * create_xlib_monochrome_cursor
915 * Create a monochrome X cursor from a Windows one.
917 static Cursor
create_xlib_monochrome_cursor( HDC hdc
, const ICONINFOEXW
*icon
, int width
, int height
)
919 char buffer
[FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )];
920 BITMAPINFO
*info
= (BITMAPINFO
*)buffer
;
922 const int xor_y
= height
;
923 unsigned int width_bytes
= (width
+ 31) / 32 * 4;
924 unsigned char *mask_bits
= NULL
;
928 Pixmap src_pixmap
, bits_pixmap
, mask_pixmap
;
929 struct gdi_image_bits bits
;
932 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
933 info
->bmiHeader
.biWidth
= width
;
934 info
->bmiHeader
.biHeight
= -height
* 2;
935 info
->bmiHeader
.biPlanes
= 1;
936 info
->bmiHeader
.biBitCount
= 1;
937 info
->bmiHeader
.biCompression
= BI_RGB
;
938 info
->bmiHeader
.biSizeImage
= width_bytes
* height
* 2;
939 info
->bmiHeader
.biXPelsPerMeter
= 0;
940 info
->bmiHeader
.biYPelsPerMeter
= 0;
941 info
->bmiHeader
.biClrUsed
= 0;
942 info
->bmiHeader
.biClrImportant
= 0;
944 if (!(mask_bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
))) goto done
;
945 if (!GetDIBits( hdc
, icon
->hbmMask
, 0, height
* 2, mask_bits
, info
, DIB_RGB_COLORS
)) goto done
;
948 bits
.ptr
= mask_bits
;
951 if (!(src_pixmap
= create_pixmap_from_image( hdc
, &vis
, info
, &bits
, DIB_RGB_COLORS
))) goto done
;
953 bits_pixmap
= XCreatePixmap( gdi_display
, root_window
, width
, height
, 1 );
954 mask_pixmap
= XCreatePixmap( gdi_display
, root_window
, width
, height
, 1 );
955 gc
= XCreateGC( gdi_display
, src_pixmap
, 0, NULL
);
956 XSetGraphicsExposures( gdi_display
, gc
, False
);
958 /* We have to do some magic here, as cursors are not fully
959 * compatible between Windows and X11. Under X11, there are
960 * only 3 possible color cursor: black, white and masked. So
961 * we map the 4th Windows color (invert the bits on the screen)
962 * to black and an additional white bit on another place
963 * (+1,+1). This require some boolean arithmetic:
966 * And Xor Result | Bits Mask Result
967 * 0 0 black | 0 1 background
968 * 0 1 white | 1 1 foreground
969 * 1 0 no change | X 0 no change
970 * 1 1 inverted | 0 1 background
973 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
974 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
976 XSetFunction( gdi_display
, gc
, GXcopy
);
977 XCopyArea( gdi_display
, src_pixmap
, bits_pixmap
, gc
, 0, and_y
, width
, height
, 0, 0 );
978 XCopyArea( gdi_display
, src_pixmap
, mask_pixmap
, gc
, 0, and_y
, width
, height
, 0, 0 );
979 XSetFunction( gdi_display
, gc
, GXandReverse
);
980 XCopyArea( gdi_display
, src_pixmap
, bits_pixmap
, gc
, 0, xor_y
, width
, height
, 0, 0 );
981 XSetFunction( gdi_display
, gc
, GXorReverse
);
982 XCopyArea( gdi_display
, src_pixmap
, mask_pixmap
, gc
, 0, xor_y
, width
, height
, 0, 0 );
983 /* additional white */
984 XSetFunction( gdi_display
, gc
, GXand
);
985 XCopyArea( gdi_display
, src_pixmap
, src_pixmap
, gc
, 0, xor_y
, width
, height
, 0, and_y
);
986 XSetFunction( gdi_display
, gc
, GXor
);
987 XCopyArea( gdi_display
, src_pixmap
, mask_pixmap
, gc
, 0, and_y
, width
, height
, 1, 1 );
988 XCopyArea( gdi_display
, src_pixmap
, bits_pixmap
, gc
, 0, and_y
, width
, height
, 1, 1 );
989 XFreeGC( gdi_display
, gc
);
991 fg
.red
= fg
.green
= fg
.blue
= 0xffff;
992 bg
.red
= bg
.green
= bg
.blue
= 0;
993 cursor
= XCreatePixmapCursor( gdi_display
, bits_pixmap
, mask_pixmap
,
994 &fg
, &bg
, icon
->xHotspot
, icon
->yHotspot
);
995 XFreePixmap( gdi_display
, src_pixmap
);
996 XFreePixmap( gdi_display
, bits_pixmap
);
997 XFreePixmap( gdi_display
, mask_pixmap
);
1000 HeapFree( GetProcessHeap(), 0, mask_bits
);
1004 /***********************************************************************
1005 * create_xlib_color_cursor
1007 * Create a color X cursor from a Windows one.
1009 static Cursor
create_xlib_color_cursor( HDC hdc
, const ICONINFOEXW
*icon
, int width
, int height
)
1011 char buffer
[FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )];
1012 BITMAPINFO
*info
= (BITMAPINFO
*)buffer
;
1014 Cursor cursor
= None
;
1016 Pixmap xor_pixmap
, mask_pixmap
;
1017 struct gdi_image_bits bits
;
1018 unsigned int *color_bits
= NULL
, *ptr
;
1019 unsigned char *mask_bits
= NULL
, *xor_bits
= NULL
;
1020 int i
, x
, y
, has_alpha
= 0;
1021 int rfg
, gfg
, bfg
, rbg
, gbg
, bbg
, fgBits
, bgBits
;
1022 unsigned int width_bytes
= (width
+ 31) / 32 * 4;
1024 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1025 info
->bmiHeader
.biWidth
= width
;
1026 info
->bmiHeader
.biHeight
= -height
;
1027 info
->bmiHeader
.biPlanes
= 1;
1028 info
->bmiHeader
.biBitCount
= 1;
1029 info
->bmiHeader
.biCompression
= BI_RGB
;
1030 info
->bmiHeader
.biSizeImage
= width_bytes
* height
;
1031 info
->bmiHeader
.biXPelsPerMeter
= 0;
1032 info
->bmiHeader
.biYPelsPerMeter
= 0;
1033 info
->bmiHeader
.biClrUsed
= 0;
1034 info
->bmiHeader
.biClrImportant
= 0;
1036 if (!(mask_bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
))) goto done
;
1037 if (!GetDIBits( hdc
, icon
->hbmMask
, 0, height
, mask_bits
, info
, DIB_RGB_COLORS
)) goto done
;
1039 info
->bmiHeader
.biBitCount
= 32;
1040 info
->bmiHeader
.biSizeImage
= width
* height
* 4;
1041 if (!(color_bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
))) goto done
;
1042 if (!(xor_bits
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, width_bytes
* height
))) goto done
;
1043 GetDIBits( hdc
, icon
->hbmColor
, 0, height
, color_bits
, info
, DIB_RGB_COLORS
);
1045 /* compute fg/bg color and xor bitmap based on average of the color values */
1047 rfg
= gfg
= bfg
= rbg
= gbg
= bbg
= fgBits
= 0;
1048 for (y
= 0, ptr
= color_bits
; y
< height
; y
++)
1050 for (x
= 0; x
< width
; x
++, ptr
++)
1052 int red
= (*ptr
>> 16) & 0xff;
1053 int green
= (*ptr
>> 8) & 0xff;
1054 int blue
= (*ptr
>> 0) & 0xff;
1055 if (red
+ green
+ blue
> 0x40)
1061 xor_bits
[y
* width_bytes
+ x
/ 8] |= 0x80 >> (x
% 8);
1073 fg
.red
= rfg
* 257 / fgBits
;
1074 fg
.green
= gfg
* 257 / fgBits
;
1075 fg
.blue
= bfg
* 257 / fgBits
;
1077 else fg
.red
= fg
.green
= fg
.blue
= 0;
1078 bgBits
= width
* height
- fgBits
;
1081 bg
.red
= rbg
* 257 / bgBits
;
1082 bg
.green
= gbg
* 257 / bgBits
;
1083 bg
.blue
= bbg
* 257 / bgBits
;
1085 else bg
.red
= bg
.green
= bg
.blue
= 0;
1087 info
->bmiHeader
.biBitCount
= 1;
1088 info
->bmiHeader
.biClrUsed
= 0;
1089 info
->bmiHeader
.biSizeImage
= width_bytes
* height
;
1091 /* generate mask from the alpha channel if we have one */
1093 for (i
= 0, ptr
= color_bits
; i
< width
* height
; i
++, ptr
++)
1094 if ((has_alpha
= (*ptr
& 0xff000000) != 0)) break;
1098 memset( mask_bits
, 0, width_bytes
* height
);
1099 for (y
= 0, ptr
= color_bits
; y
< height
; y
++)
1100 for (x
= 0; x
< width
; x
++, ptr
++)
1101 if ((*ptr
>> 24) > 25) /* more than 10% alpha */
1102 mask_bits
[y
* width_bytes
+ x
/ 8] |= 0x80 >> (x
% 8);
1104 else /* invert the mask */
1106 ptr
= (unsigned int *)mask_bits
;
1107 for (i
= 0; i
< info
->bmiHeader
.biSizeImage
/ sizeof(*ptr
); i
++, ptr
++) *ptr
^= ~0u;
1111 bits
.ptr
= xor_bits
;
1113 bits
.is_copy
= TRUE
;
1114 if (!(xor_pixmap
= create_pixmap_from_image( hdc
, &vis
, info
, &bits
, DIB_RGB_COLORS
))) goto done
;
1116 bits
.ptr
= mask_bits
;
1117 mask_pixmap
= create_pixmap_from_image( hdc
, &vis
, info
, &bits
, DIB_RGB_COLORS
);
1121 cursor
= XCreatePixmapCursor( gdi_display
, xor_pixmap
, mask_pixmap
,
1122 &fg
, &bg
, icon
->xHotspot
, icon
->yHotspot
);
1123 XFreePixmap( gdi_display
, mask_pixmap
);
1125 XFreePixmap( gdi_display
, xor_pixmap
);
1128 HeapFree( GetProcessHeap(), 0, color_bits
);
1129 HeapFree( GetProcessHeap(), 0, xor_bits
);
1130 HeapFree( GetProcessHeap(), 0, mask_bits
);
1134 /***********************************************************************
1137 * Create an X cursor from a Windows one.
1139 static Cursor
create_cursor( HANDLE handle
)
1146 if (!handle
) return get_empty_cursor();
1148 info
.cbSize
= sizeof(info
);
1149 if (!GetIconInfoExW( handle
, &info
)) return 0;
1151 #ifdef SONAME_LIBXCURSOR
1152 if (use_system_cursors
&& (cursor
= create_xcursor_system_cursor( &info
)))
1154 DeleteObject( info
.hbmColor
);
1155 DeleteObject( info
.hbmMask
);
1160 GetObjectW( info
.hbmMask
, sizeof(bm
), &bm
);
1161 if (!info
.hbmColor
) bm
.bmHeight
= max( 1, bm
.bmHeight
/ 2 );
1163 /* make sure hotspot is valid */
1164 if (info
.xHotspot
>= bm
.bmWidth
|| info
.yHotspot
>= bm
.bmHeight
)
1166 info
.xHotspot
= bm
.bmWidth
/ 2;
1167 info
.yHotspot
= bm
.bmHeight
/ 2;
1170 hdc
= CreateCompatibleDC( 0 );
1174 #ifdef SONAME_LIBXCURSOR
1175 if (pXcursorImagesLoadCursor
)
1176 cursor
= create_xcursor_cursor( hdc
, &info
, handle
, bm
.bmWidth
, bm
.bmHeight
);
1178 if (!cursor
) cursor
= create_xlib_color_cursor( hdc
, &info
, bm
.bmWidth
, bm
.bmHeight
);
1179 DeleteObject( info
.hbmColor
);
1183 cursor
= create_xlib_monochrome_cursor( hdc
, &info
, bm
.bmWidth
, bm
.bmHeight
);
1186 DeleteObject( info
.hbmMask
);
1191 /***********************************************************************
1192 * DestroyCursorIcon (X11DRV.@)
1194 void CDECL
X11DRV_DestroyCursorIcon( HCURSOR handle
)
1198 if (!XFindContext( gdi_display
, (XID
)handle
, cursor_context
, (char **)&cursor
))
1200 TRACE( "%p xid %lx\n", handle
, cursor
);
1201 XFreeCursor( gdi_display
, cursor
);
1202 XDeleteContext( gdi_display
, (XID
)handle
, cursor_context
);
1206 /***********************************************************************
1207 * SetCursor (X11DRV.@)
1209 void CDECL
X11DRV_SetCursor( HCURSOR handle
)
1211 if (InterlockedExchangePointer( (void **)&last_cursor
, handle
) != handle
||
1212 GetTickCount() - last_cursor_change
> 100)
1214 last_cursor_change
= GetTickCount();
1215 if (cursor_window
) SendNotifyMessageW( cursor_window
, WM_X11DRV_SET_CURSOR
, 0, (LPARAM
)handle
);
1219 /***********************************************************************
1220 * SetCursorPos (X11DRV.@)
1222 BOOL CDECL
X11DRV_SetCursorPos( INT x
, INT y
)
1224 struct x11drv_thread_data
*data
= x11drv_init_thread_data();
1226 XWarpPointer( data
->display
, root_window
, root_window
, 0, 0, 0, 0,
1227 x
- virtual_screen_rect
.left
, y
- virtual_screen_rect
.top
);
1228 data
->warp_serial
= NextRequest( data
->display
);
1229 XNoOp( data
->display
);
1230 XFlush( data
->display
); /* avoids bad mouse lag in games that do their own mouse warping */
1231 TRACE( "warped to %d,%d serial %lu\n", x
, y
, data
->warp_serial
);
1235 /***********************************************************************
1236 * GetCursorPos (X11DRV.@)
1238 BOOL CDECL
X11DRV_GetCursorPos(LPPOINT pos
)
1240 Display
*display
= thread_init_display();
1242 int rootX
, rootY
, winX
, winY
;
1243 unsigned int xstate
;
1246 ret
= XQueryPointer( display
, root_window
, &root
, &child
, &rootX
, &rootY
, &winX
, &winY
, &xstate
);
1250 pos
->x
= winX
+ virtual_screen_rect
.left
;
1251 pos
->y
= winY
+ virtual_screen_rect
.top
;
1252 TRACE( "pointer at (%d,%d) server pos %d,%d\n", pos
->x
, pos
->y
, old
.x
, old
.y
);
1257 /***********************************************************************
1258 * ClipCursor (X11DRV.@)
1260 BOOL CDECL
X11DRV_ClipCursor( LPCRECT clip
)
1264 ungrab_clipping_window();
1268 if (GetWindowThreadProcessId( GetDesktopWindow(), NULL
) == GetCurrentThreadId())
1269 return TRUE
; /* don't clip in the desktop process */
1273 HWND foreground
= GetForegroundWindow();
1275 /* we are clipping if the clip rectangle is smaller than the screen */
1276 if (clip
->left
> virtual_screen_rect
.left
|| clip
->right
< virtual_screen_rect
.right
||
1277 clip
->top
> virtual_screen_rect
.top
|| clip
->bottom
< virtual_screen_rect
.bottom
)
1281 /* forward request to the foreground window if it's in a different thread */
1282 tid
= GetWindowThreadProcessId( foreground
, &pid
);
1283 if (tid
&& tid
!= GetCurrentThreadId() && pid
== GetCurrentProcessId())
1285 TRACE( "forwarding clip request to %p\n", foreground
);
1286 SendNotifyMessageW( foreground
, WM_X11DRV_CLIP_CURSOR
, 0, 0 );
1289 else if (grab_clipping_window( clip
)) return TRUE
;
1291 else /* if currently clipping, check if we should switch to fullscreen clipping */
1293 struct x11drv_thread_data
*data
= x11drv_thread_data();
1294 if (data
&& data
->clip_hwnd
)
1296 if (EqualRect( clip
, &clip_rect
) || clip_fullscreen_window( foreground
, TRUE
))
1301 ungrab_clipping_window();
1305 /***********************************************************************
1306 * move_resize_window
1308 void move_resize_window( HWND hwnd
, int dir
)
1310 Display
*display
= thread_display();
1312 int x
, y
, rootX
, rootY
, button
= 0;
1314 Window win
, root
, child
;
1315 unsigned int xstate
;
1317 if (!(win
= X11DRV_get_whole_window( hwnd
))) return;
1319 pt
= GetMessagePos();
1320 x
= (short)LOWORD( pt
);
1321 y
= (short)HIWORD( pt
);
1323 if (GetKeyState( VK_LBUTTON
) & 0x8000) button
= 1;
1324 else if (GetKeyState( VK_MBUTTON
) & 0x8000) button
= 2;
1325 else if (GetKeyState( VK_RBUTTON
) & 0x8000) button
= 3;
1327 TRACE( "hwnd %p/%lx, x %d, y %d, dir %d, button %d\n", hwnd
, win
, x
, y
, dir
, button
);
1329 xev
.xclient
.type
= ClientMessage
;
1330 xev
.xclient
.window
= win
;
1331 xev
.xclient
.message_type
= x11drv_atom(_NET_WM_MOVERESIZE
);
1332 xev
.xclient
.serial
= 0;
1333 xev
.xclient
.display
= display
;
1334 xev
.xclient
.send_event
= True
;
1335 xev
.xclient
.format
= 32;
1336 xev
.xclient
.data
.l
[0] = x
- virtual_screen_rect
.left
; /* x coord */
1337 xev
.xclient
.data
.l
[1] = y
- virtual_screen_rect
.top
; /* y coord */
1338 xev
.xclient
.data
.l
[2] = dir
; /* direction */
1339 xev
.xclient
.data
.l
[3] = button
; /* button */
1340 xev
.xclient
.data
.l
[4] = 0; /* unused */
1342 /* need to ungrab the pointer that may have been automatically grabbed
1343 * with a ButtonPress event */
1344 XUngrabPointer( display
, CurrentTime
);
1345 XSendEvent(display
, root_window
, False
, SubstructureNotifyMask
| SubstructureRedirectMask
, &xev
);
1347 /* try to detect the end of the size/move by polling for the mouse button to be released */
1348 /* (some apps don't like it if we return before the size/move is done) */
1350 if (!button
) return;
1356 if (!XQueryPointer( display
, root_window
, &root
, &child
, &rootX
, &rootY
, &x
, &y
, &xstate
)) break;
1358 if (!(xstate
& (Button1Mask
<< (button
- 1))))
1360 /* fake a button release event */
1361 input
.type
= INPUT_MOUSE
;
1362 input
.u
.mi
.dx
= x
+ virtual_screen_rect
.left
;
1363 input
.u
.mi
.dy
= y
+ virtual_screen_rect
.top
;
1364 input
.u
.mi
.mouseData
= button_up_data
[button
- 1];
1365 input
.u
.mi
.dwFlags
= button_up_flags
[button
- 1] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
;
1366 input
.u
.mi
.time
= GetTickCount();
1367 input
.u
.mi
.dwExtraInfo
= 0;
1368 __wine_send_input( hwnd
, &input
);
1371 while (PeekMessageW( &msg
, 0, 0, 0, PM_REMOVE
))
1373 if (!CallMsgFilterW( &msg
, MSGF_SIZE
))
1375 TranslateMessage( &msg
);
1376 DispatchMessageW( &msg
);
1380 if (!(xstate
& (Button1Mask
<< (button
- 1)))) break;
1381 MsgWaitForMultipleObjects( 0, NULL
, FALSE
, 100, QS_ALLINPUT
);
1384 TRACE( "hwnd %p/%lx done\n", hwnd
, win
);
1388 /***********************************************************************
1389 * X11DRV_ButtonPress
1391 void X11DRV_ButtonPress( HWND hwnd
, XEvent
*xev
)
1393 XButtonEvent
*event
= &xev
->xbutton
;
1394 int buttonNum
= event
->button
- 1;
1397 if (buttonNum
>= NB_BUTTONS
) return;
1399 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd
, event
->window
, buttonNum
, event
->x
, event
->y
);
1401 input
.u
.mi
.dx
= event
->x
;
1402 input
.u
.mi
.dy
= event
->y
;
1403 input
.u
.mi
.mouseData
= button_down_data
[buttonNum
];
1404 input
.u
.mi
.dwFlags
= button_down_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
;
1405 input
.u
.mi
.time
= EVENT_x11_time_to_win32_time( event
->time
);
1406 input
.u
.mi
.dwExtraInfo
= 0;
1408 update_user_time( event
->time
);
1409 send_mouse_input( hwnd
, event
->window
, event
->state
, &input
);
1413 /***********************************************************************
1414 * X11DRV_ButtonRelease
1416 void X11DRV_ButtonRelease( HWND hwnd
, XEvent
*xev
)
1418 XButtonEvent
*event
= &xev
->xbutton
;
1419 int buttonNum
= event
->button
- 1;
1422 if (buttonNum
>= NB_BUTTONS
|| !button_up_flags
[buttonNum
]) return;
1424 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd
, event
->window
, buttonNum
, event
->x
, event
->y
);
1426 input
.u
.mi
.dx
= event
->x
;
1427 input
.u
.mi
.dy
= event
->y
;
1428 input
.u
.mi
.mouseData
= button_up_data
[buttonNum
];
1429 input
.u
.mi
.dwFlags
= button_up_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
;
1430 input
.u
.mi
.time
= EVENT_x11_time_to_win32_time( event
->time
);
1431 input
.u
.mi
.dwExtraInfo
= 0;
1433 send_mouse_input( hwnd
, event
->window
, event
->state
, &input
);
1437 /***********************************************************************
1438 * X11DRV_MotionNotify
1440 void X11DRV_MotionNotify( HWND hwnd
, XEvent
*xev
)
1442 XMotionEvent
*event
= &xev
->xmotion
;
1445 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d serial %lu\n",
1446 hwnd
, event
->window
, event
->x
, event
->y
, event
->is_hint
, event
->serial
);
1448 input
.u
.mi
.dx
= event
->x
;
1449 input
.u
.mi
.dy
= event
->y
;
1450 input
.u
.mi
.mouseData
= 0;
1451 input
.u
.mi
.dwFlags
= MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
;
1452 input
.u
.mi
.time
= EVENT_x11_time_to_win32_time( event
->time
);
1453 input
.u
.mi
.dwExtraInfo
= 0;
1457 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
1458 if (thread_data
->warp_serial
&& (long)(event
->serial
- thread_data
->warp_serial
) < 0) return;
1461 send_mouse_input( hwnd
, event
->window
, event
->state
, &input
);
1465 /***********************************************************************
1466 * X11DRV_EnterNotify
1468 void X11DRV_EnterNotify( HWND hwnd
, XEvent
*xev
)
1470 XCrossingEvent
*event
= &xev
->xcrossing
;
1473 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd
, event
->window
, event
->x
, event
->y
, event
->detail
);
1475 if (event
->detail
== NotifyVirtual
) return;
1476 if (event
->window
== x11drv_thread_data()->grab_window
) return;
1478 /* simulate a mouse motion event */
1479 input
.u
.mi
.dx
= event
->x
;
1480 input
.u
.mi
.dy
= event
->y
;
1481 input
.u
.mi
.mouseData
= 0;
1482 input
.u
.mi
.dwFlags
= MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
;
1483 input
.u
.mi
.time
= EVENT_x11_time_to_win32_time( event
->time
);
1484 input
.u
.mi
.dwExtraInfo
= 0;
1486 send_mouse_input( hwnd
, event
->window
, event
->state
, &input
);
1489 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1491 /***********************************************************************
1494 static void X11DRV_RawMotion( XGenericEventCookie
*xev
)
1496 XIRawEvent
*event
= xev
->data
;
1497 const double *values
= event
->valuators
.values
;
1500 double dx
= 0, dy
= 0;
1501 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
1502 XIDeviceInfo
*devices
= thread_data
->xi2_devices
;
1504 if (!event
->valuators
.mask_len
) return;
1505 if (thread_data
->xi2_state
!= xi_enabled
) return;
1507 input
.u
.mi
.mouseData
= 0;
1508 input
.u
.mi
.dwFlags
= MOUSEEVENTF_MOVE
;
1509 input
.u
.mi
.time
= EVENT_x11_time_to_win32_time( event
->time
);
1510 input
.u
.mi
.dwExtraInfo
= 0;
1514 for (i
= 0; i
< thread_data
->xi2_device_count
; ++i
)
1516 if (devices
[i
].deviceid
!= event
->deviceid
) continue;
1517 for (j
= 0; j
< devices
[i
].num_classes
; j
++)
1519 XIValuatorClassInfo
*class = (XIValuatorClassInfo
*)devices
[i
].classes
[j
];
1521 if (devices
[i
].classes
[j
]->type
!= XIValuatorClass
) continue;
1522 if (XIMaskIsSet( event
->valuators
.mask
, class->number
))
1524 double val
= *values
++;
1525 if (class->label
== x11drv_atom( Rel_X
))
1527 input
.u
.mi
.dx
= dx
= val
;
1528 if (class->min
< class->max
)
1529 input
.u
.mi
.dx
= val
* (virtual_screen_rect
.right
- virtual_screen_rect
.left
)
1530 / (class->max
- class->min
);
1532 else if (class->label
== x11drv_atom( Rel_Y
))
1534 input
.u
.mi
.dy
= dy
= val
;
1535 if (class->min
< class->max
)
1536 input
.u
.mi
.dy
= val
* (virtual_screen_rect
.bottom
- virtual_screen_rect
.top
)
1537 / (class->max
- class->min
);
1544 if (thread_data
->warp_serial
)
1546 if ((long)(xev
->serial
- thread_data
->warp_serial
) < 0)
1548 TRACE( "pos %d,%d old serial %lu, ignoring\n", input
.u
.mi
.dx
, input
.u
.mi
.dy
, xev
->serial
);
1551 thread_data
->warp_serial
= 0; /* we caught up now */
1554 TRACE( "pos %d,%d (event %f,%f)\n", input
.u
.mi
.dx
, input
.u
.mi
.dy
, dx
, dy
);
1556 input
.type
= INPUT_MOUSE
;
1557 __wine_send_input( 0, &input
);
1560 #endif /* HAVE_X11_EXTENSIONS_XINPUT2_H */
1563 /***********************************************************************
1564 * X11DRV_XInput2_Init
1566 void X11DRV_XInput2_Init(void)
1568 #if defined(SONAME_LIBXI) && defined(HAVE_X11_EXTENSIONS_XINPUT2_H)
1570 void *libxi_handle
= wine_dlopen( SONAME_LIBXI
, RTLD_NOW
, NULL
, 0 );
1574 WARN( "couldn't load %s\n", SONAME_LIBXI
);
1577 #define LOAD_FUNCPTR(f) \
1578 if (!(p##f = wine_dlsym( libxi_handle, #f, NULL, 0))) \
1580 WARN("Failed to load %s.\n", #f); \
1584 LOAD_FUNCPTR(XIFreeDeviceInfo
);
1585 LOAD_FUNCPTR(XIQueryDevice
);
1586 LOAD_FUNCPTR(XIQueryVersion
);
1587 LOAD_FUNCPTR(XISelectEvents
);
1590 xinput2_available
= XQueryExtension( gdi_display
, "XInputExtension", &xinput2_opcode
, &event
, &error
);
1592 TRACE( "X Input 2 support not compiled in.\n" );
1597 /***********************************************************************
1598 * X11DRV_GenericEvent
1600 void X11DRV_GenericEvent( HWND hwnd
, XEvent
*xev
)
1602 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1603 XGenericEventCookie
*event
= &xev
->xcookie
;
1605 if (!event
->data
) return;
1606 if (event
->extension
!= xinput2_opcode
) return;
1608 switch (event
->evtype
)
1611 X11DRV_RawMotion( event
);
1615 TRACE( "Unhandled event %#x\n", event
->evtype
);