msi/tests: Avoid a TRUE:FALSE conditional expression.
[wine/multimedia.git] / dlls / winex11.drv / mouse.c
blob2098a8f41c45c2465fef0591a1015002fad11b05
1 /*
2 * X11 mouse driver
4 * Copyright 1998 Ulrich Weigand
5 * Copyright 2007 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <X11/Xlib.h>
26 #include <X11/cursorfont.h>
27 #include <stdarg.h>
28 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
29 #include <X11/extensions/XInput2.h>
30 #endif
32 #ifdef SONAME_LIBXCURSOR
33 # include <X11/Xcursor/Xcursor.h>
34 static void *xcursor_handle;
35 # define MAKE_FUNCPTR(f) static typeof(f) * p##f
36 MAKE_FUNCPTR(XcursorImageCreate);
37 MAKE_FUNCPTR(XcursorImageDestroy);
38 MAKE_FUNCPTR(XcursorImageLoadCursor);
39 MAKE_FUNCPTR(XcursorImagesCreate);
40 MAKE_FUNCPTR(XcursorImagesDestroy);
41 MAKE_FUNCPTR(XcursorImagesLoadCursor);
42 MAKE_FUNCPTR(XcursorLibraryLoadCursor);
43 # undef MAKE_FUNCPTR
44 #endif /* SONAME_LIBXCURSOR */
46 #define NONAMELESSUNION
47 #define NONAMELESSSTRUCT
48 #define OEMRESOURCE
49 #include "windef.h"
50 #include "winbase.h"
51 #include "winreg.h"
53 #include "x11drv.h"
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 /**********************************************************************/
63 #ifndef Button6Mask
64 #define Button6Mask (1<<13)
65 #endif
66 #ifndef Button7Mask
67 #define Button7Mask (1<<14)
68 #endif
70 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
72 static const UINT button_down_flags[NB_BUTTONS] =
74 MOUSEEVENTF_LEFTDOWN,
75 MOUSEEVENTF_MIDDLEDOWN,
76 MOUSEEVENTF_RIGHTDOWN,
77 MOUSEEVENTF_WHEEL,
78 MOUSEEVENTF_WHEEL,
79 MOUSEEVENTF_XDOWN, /* FIXME: horizontal wheel */
80 MOUSEEVENTF_XDOWN,
81 MOUSEEVENTF_XDOWN,
82 MOUSEEVENTF_XDOWN
85 static const UINT button_up_flags[NB_BUTTONS] =
87 MOUSEEVENTF_LEFTUP,
88 MOUSEEVENTF_MIDDLEUP,
89 MOUSEEVENTF_RIGHTUP,
92 MOUSEEVENTF_XUP,
93 MOUSEEVENTF_XUP,
94 MOUSEEVENTF_XUP,
95 MOUSEEVENTF_XUP
98 static const UINT button_down_data[NB_BUTTONS] =
103 WHEEL_DELTA,
104 -WHEEL_DELTA,
105 XBUTTON1,
106 XBUTTON2,
107 XBUTTON1,
108 XBUTTON2
111 static const UINT button_up_data[NB_BUTTONS] =
118 XBUTTON1,
119 XBUTTON2,
120 XBUTTON1,
121 XBUTTON2
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);
141 #undef MAKE_FUNCPTR
142 #endif
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");
156 return;
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);
168 #undef LOAD_FUNCPTR
169 #endif /* SONAME_LIBXCURSOR */
173 /***********************************************************************
174 * get_empty_cursor
176 static Cursor get_empty_cursor(void)
178 static Cursor cursor;
179 static const char data[] = { 0 };
181 wine_tsx11_lock();
182 if (!cursor)
184 XColor bg;
185 Pixmap pixmap;
187 bg.red = bg.green = bg.blue = 0x0000;
188 pixmap = XCreateBitmapFromData( gdi_display, root_window, data, 1, 1 );
189 if (pixmap)
191 cursor = XCreatePixmapCursor( gdi_display, pixmap, pixmap, &bg, &bg, 0, 0 );
192 XFreePixmap( gdi_display, pixmap );
195 wine_tsx11_unlock();
196 return cursor;
199 /***********************************************************************
200 * set_window_cursor
202 void set_window_cursor( Window window, HCURSOR handle )
204 Cursor cursor, prev;
206 wine_tsx11_lock();
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 */
211 wine_tsx11_unlock();
212 if (!(cursor = create_cursor( handle ))) return;
214 wine_tsx11_lock();
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 );
220 cursor = prev;
222 else
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 );
232 wine_tsx11_unlock();
235 /***********************************************************************
236 * sync_window_cursor
238 void sync_window_cursor( Window window )
240 HCURSOR cursor;
242 SERVER_START_REQ( set_cursor )
244 req->flags = 0;
245 wine_server_call( req );
246 cursor = reply->prev_count >= 0 ? wine_server_ptr_handle( reply->prev_handle ) : 0;
248 SERVER_END_REQ;
250 set_window_cursor( window, cursor );
253 /***********************************************************************
254 * enable_xinput2
256 static void enable_xinput2(void)
258 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
259 struct x11drv_thread_data *data = x11drv_thread_data();
260 XIEventMask mask;
261 unsigned char mask_bits[XIMaskLen(XI_LASTEVENT)];
262 int i, j;
264 if (!xinput2_available) return;
266 if (data->xi2_state == xi_unknown)
268 int major = 2, minor = 0;
269 if (!pXIQueryVersion( data->display, &major, &minor )) data->xi2_state = xi_disabled;
270 else
272 data->xi2_state = xi_unavailable;
273 WARN( "X Input 2 not available\n" );
276 if (data->xi2_state == xi_unavailable) return;
278 wine_tsx11_lock();
279 if (xinput2_devices) pXIFreeDeviceInfo( xinput2_devices );
280 xinput2_devices = pXIQueryDevice( data->display, XIAllDevices, &xinput2_device_count );
281 for (i = 0; i < xinput2_device_count; ++i)
283 if (xinput2_devices[i].use != XIMasterPointer) continue;
284 for (j = 0; j < xinput2_devices[i].num_classes; j++)
286 XIValuatorClassInfo *class = (XIValuatorClassInfo *)xinput2_devices[i].classes[j];
288 if (xinput2_devices[i].classes[j]->type != XIValuatorClass) continue;
289 if (class->number != 0 && class->number != 1) continue;
290 TRACE( "Device %u (%s) class %u num %u %f,%f res %u mode %u\n",
291 xinput2_devices[i].deviceid, debugstr_a(xinput2_devices[i].name),
292 j, class->number, class->min, class->max, class->resolution, class->mode );
293 if (class->mode == XIModeAbsolute)
295 TRACE( "Device is absolute, not enabling XInput2\n" );
296 goto done;
299 TRACE( "Using %u (%s) as core pointer\n",
300 xinput2_devices[i].deviceid, debugstr_a(xinput2_devices[i].name) );
301 xinput2_core_pointer = xinput2_devices[i].deviceid;
302 break;
305 mask.mask = mask_bits;
306 mask.mask_len = sizeof(mask_bits);
307 memset( mask_bits, 0, sizeof(mask_bits) );
308 XISetMask( mask_bits, XI_RawMotion );
309 XISetMask( mask_bits, XI_ButtonPress );
311 for (i = 0; i < xinput2_device_count; ++i)
313 if (xinput2_devices[i].use == XISlavePointer &&
314 xinput2_devices[i].attachment == xinput2_core_pointer)
316 TRACE( "Device %u (%s) is attached to the core pointer\n",
317 xinput2_devices[i].deviceid, debugstr_a(xinput2_devices[i].name) );
318 mask.deviceid = xinput2_devices[i].deviceid;
319 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
320 data->xi2_state = xi_enabled;
324 done:
325 wine_tsx11_unlock();
326 #endif
329 /***********************************************************************
330 * disable_xinput2
332 static void disable_xinput2(void)
334 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
335 struct x11drv_thread_data *data = x11drv_thread_data();
336 XIEventMask mask;
337 int i;
339 if (data->xi2_state != xi_enabled) return;
341 TRACE( "disabling\n" );
342 data->xi2_state = xi_disabled;
344 mask.mask = NULL;
345 mask.mask_len = 0;
347 wine_tsx11_lock();
348 for (i = 0; i < xinput2_device_count; ++i)
350 if (xinput2_devices[i].use == XISlavePointer &&
351 xinput2_devices[i].attachment == xinput2_core_pointer)
353 mask.deviceid = xinput2_devices[i].deviceid;
354 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
357 pXIFreeDeviceInfo( xinput2_devices );
358 xinput2_devices = NULL;
359 xinput2_device_count = 0;
360 wine_tsx11_unlock();
361 #endif
365 /***********************************************************************
366 * grab_clipping_window
368 * Start a pointer grab on the clip window.
370 static BOOL grab_clipping_window( const RECT *clip )
372 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
373 struct x11drv_thread_data *data = x11drv_thread_data();
374 Window clip_window;
375 HWND msg_hwnd = 0;
377 if (!data) return FALSE;
378 if (!(clip_window = init_clip_window())) return TRUE;
380 if (!(msg_hwnd = CreateWindowW( messageW, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0,
381 GetModuleHandleW(0), NULL )))
382 return TRUE;
384 /* enable XInput2 unless we are already clipping */
385 if (!data->clip_hwnd) enable_xinput2();
387 if (data->xi2_state != xi_enabled)
389 WARN( "XInput2 not supported, refusing to clip to %s\n", wine_dbgstr_rect(clip) );
390 DestroyWindow( msg_hwnd );
391 ClipCursor( NULL );
392 return TRUE;
395 TRACE( "clipping to %s win %lx\n", wine_dbgstr_rect(clip), clip_window );
397 wine_tsx11_lock();
398 if (!data->clip_hwnd) XUnmapWindow( data->display, clip_window );
399 XMoveResizeWindow( data->display, clip_window,
400 clip->left - virtual_screen_rect.left, clip->top - virtual_screen_rect.top,
401 max( 1, clip->right - clip->left ), max( 1, clip->bottom - clip->top ) );
402 XMapWindow( data->display, clip_window );
404 /* if the rectangle is shrinking we may get a pointer warp */
405 if (!data->clip_hwnd || clip->left > clip_rect.left || clip->top > clip_rect.top ||
406 clip->right < clip_rect.right || clip->bottom < clip_rect.bottom)
407 data->warp_serial = NextRequest( data->display );
409 if (!XGrabPointer( data->display, clip_window, False,
410 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
411 GrabModeAsync, GrabModeAsync, clip_window, None, CurrentTime ))
412 clipping_cursor = 1;
413 wine_tsx11_unlock();
415 if (!clipping_cursor)
417 disable_xinput2();
418 DestroyWindow( msg_hwnd );
419 return FALSE;
421 clip_rect = *clip;
422 if (!data->clip_hwnd) sync_window_cursor( clip_window );
423 InterlockedExchangePointer( (void **)&cursor_window, msg_hwnd );
424 data->clip_hwnd = msg_hwnd;
425 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, (LPARAM)msg_hwnd );
426 return TRUE;
429 /***********************************************************************
430 * ungrab_clipping_window
432 * Release the pointer grab on the clip window.
434 void ungrab_clipping_window(void)
436 Display *display = thread_init_display();
437 Window clip_window = init_clip_window();
439 if (!clip_window) return;
441 TRACE( "no longer clipping\n" );
442 XUnmapWindow( display, clip_window );
443 clipping_cursor = 0;
444 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, 0 );
447 /***********************************************************************
448 * reset_clipping_window
450 * Forcibly reset the window clipping on external events.
452 void reset_clipping_window(void)
454 ungrab_clipping_window();
455 ClipCursor( NULL ); /* make sure the clip rectangle is reset too */
458 /***********************************************************************
459 * clip_cursor_notify
461 * Notification function called upon receiving a WM_X11DRV_CLIP_CURSOR.
463 LRESULT clip_cursor_notify( HWND hwnd, HWND new_clip_hwnd )
465 struct x11drv_thread_data *data = x11drv_thread_data();
467 if (hwnd == GetDesktopWindow()) /* change the clip window stored in the desktop process */
469 static HWND clip_hwnd;
471 HWND prev = clip_hwnd;
472 clip_hwnd = new_clip_hwnd;
473 if (prev || new_clip_hwnd) TRACE( "clip hwnd changed from %p to %p\n", prev, new_clip_hwnd );
474 if (prev) SendNotifyMessageW( prev, WM_X11DRV_CLIP_CURSOR, 0, 0 );
476 else if (hwnd == data->clip_hwnd) /* this is a notification that clipping has been reset */
478 TRACE( "clip hwnd reset from %p\n", hwnd );
479 data->clip_hwnd = 0;
480 data->clip_reset = GetTickCount();
481 disable_xinput2();
482 DestroyWindow( hwnd );
484 else if (hwnd == GetForegroundWindow()) /* request to clip */
486 RECT clip;
488 GetClipCursor( &clip );
489 if (clip.left > virtual_screen_rect.left || clip.right < virtual_screen_rect.right ||
490 clip.top > virtual_screen_rect.top || clip.bottom < virtual_screen_rect.bottom)
491 return grab_clipping_window( &clip );
493 return 0;
496 /***********************************************************************
497 * clip_fullscreen_window
499 * Turn on clipping if the active window is fullscreen.
501 BOOL clip_fullscreen_window( HWND hwnd, BOOL reset )
503 struct x11drv_win_data *data;
504 struct x11drv_thread_data *thread_data;
505 RECT rect;
506 DWORD style;
508 if (hwnd == GetDesktopWindow()) return FALSE;
509 if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
510 style = GetWindowLongW( hwnd, GWL_STYLE );
511 if (!(style & WS_VISIBLE)) return FALSE;
512 if ((style & (WS_POPUP | WS_CHILD)) == WS_CHILD) return FALSE;
513 /* maximized windows don't count as full screen */
514 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION) return FALSE;
515 if (!is_window_rect_fullscreen( &data->whole_rect )) return FALSE;
516 if (!(thread_data = x11drv_thread_data())) return FALSE;
517 if (GetTickCount() - thread_data->clip_reset < 1000) return FALSE;
518 if (!reset && clipping_cursor && thread_data->clip_hwnd) return FALSE; /* already clipping */
519 SetRect( &rect, 0, 0, screen_width, screen_height );
520 if (!grab_fullscreen)
522 if (!EqualRect( &rect, &virtual_screen_rect )) return FALSE;
523 if (root_window != DefaultRootWindow( gdi_display )) return FALSE;
525 TRACE( "win %p clipping fullscreen\n", hwnd );
526 return grab_clipping_window( &rect );
529 /***********************************************************************
530 * send_mouse_input
532 * Update the various window states on a mouse event.
534 static void send_mouse_input( HWND hwnd, Window window, unsigned int state, INPUT *input )
536 struct x11drv_win_data *data;
537 POINT pt;
539 input->type = INPUT_MOUSE;
541 if (!hwnd)
543 struct x11drv_thread_data *thread_data = x11drv_thread_data();
544 HWND clip_hwnd = thread_data->clip_hwnd;
546 if (!clip_hwnd) return;
547 if (thread_data->clip_window != window) return;
548 if (InterlockedExchangePointer( (void **)&cursor_window, clip_hwnd ) != clip_hwnd ||
549 input->u.mi.time - last_cursor_change > 100)
551 sync_window_cursor( window );
552 last_cursor_change = input->u.mi.time;
554 input->u.mi.dx += clip_rect.left;
555 input->u.mi.dy += clip_rect.top;
556 __wine_send_input( hwnd, input );
557 return;
560 if (!(data = X11DRV_get_win_data( hwnd ))) return;
562 if (window == data->whole_window)
564 input->u.mi.dx += data->whole_rect.left - data->client_rect.left;
565 input->u.mi.dy += data->whole_rect.top - data->client_rect.top;
567 if (window == root_window)
569 input->u.mi.dx += virtual_screen_rect.left;
570 input->u.mi.dy += virtual_screen_rect.top;
572 pt.x = input->u.mi.dx;
573 pt.y = input->u.mi.dy;
574 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
575 pt.x = data->client_rect.right - data->client_rect.left - 1 - pt.x;
576 MapWindowPoints( hwnd, 0, &pt, 1 );
578 if (InterlockedExchangePointer( (void **)&cursor_window, hwnd ) != hwnd ||
579 input->u.mi.time - last_cursor_change > 100)
581 sync_window_cursor( data->whole_window );
582 last_cursor_change = input->u.mi.time;
585 if (hwnd != GetDesktopWindow())
587 hwnd = GetAncestor( hwnd, GA_ROOT );
588 if ((input->u.mi.dwFlags & (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_RIGHTDOWN)) && hwnd == GetForegroundWindow())
589 clip_fullscreen_window( hwnd, FALSE );
592 /* update the wine server Z-order */
594 if (window != x11drv_thread_data()->grab_window &&
595 /* ignore event if a button is pressed, since the mouse is then grabbed too */
596 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
598 RECT rect;
599 SetRect( &rect, pt.x, pt.y, pt.x + 1, pt.y + 1 );
600 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
602 SERVER_START_REQ( update_window_zorder )
604 req->window = wine_server_user_handle( hwnd );
605 req->rect.left = rect.left;
606 req->rect.top = rect.top;
607 req->rect.right = rect.right;
608 req->rect.bottom = rect.bottom;
609 wine_server_call( req );
611 SERVER_END_REQ;
614 input->u.mi.dx = pt.x;
615 input->u.mi.dy = pt.y;
616 __wine_send_input( hwnd, input );
619 #ifdef SONAME_LIBXCURSOR
621 /***********************************************************************
622 * create_xcursor_frame
624 * Use Xcursor to create a frame of an X cursor from a Windows one.
626 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
627 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
628 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
629 int width, int height, int istep )
631 XcursorImage *image, *ret = NULL;
632 DWORD delay_jiffies, num_steps;
633 int x, y, i, has_alpha = FALSE;
634 XcursorPixel *ptr;
636 image = pXcursorImageCreate( width, height );
637 if (!image)
639 ERR("X11 failed to produce a cursor frame!\n");
640 return NULL;
643 image->xhot = iinfo->xHotspot;
644 image->yhot = iinfo->yHotspot;
646 image->delay = 100; /* fallback delay, 100 ms */
647 if (GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, istep, &delay_jiffies, &num_steps) != 0)
648 image->delay = (100 * delay_jiffies) / 6; /* convert jiffies (1/60s) to milliseconds */
649 else
650 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep);
652 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
653 memset( color_bits, 0x00, color_size );
654 SelectObject( hdc, hbmColor );
655 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
657 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
658 goto cleanup;
660 memcpy( image->pixels, color_bits, color_size );
662 /* check if the cursor frame was drawn with an alpha channel */
663 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
664 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
666 /* if no alpha channel was drawn then generate it from the mask */
667 if (!has_alpha)
669 unsigned int width_bytes = (width + 31) / 32 * 4;
671 /* draw the cursor mask to a temporary buffer */
672 memset( mask_bits, 0xFF, mask_size );
673 SelectObject( hdc, hbmMask );
674 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
676 ERR("Failed to draw frame mask %d.\n", istep);
677 goto cleanup;
679 /* use the buffer to directly modify the XcursorImage alpha channel */
680 for (y = 0, ptr = image->pixels; y < height; y++)
681 for (x = 0; x < width; x++, ptr++)
682 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
683 *ptr |= 0xff000000;
685 ret = image;
687 cleanup:
688 if (ret == NULL) pXcursorImageDestroy( image );
689 return ret;
692 /***********************************************************************
693 * create_xcursor_cursor
695 * Use Xcursor to create an X cursor from a Windows one.
697 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
699 unsigned char *color_bits, *mask_bits;
700 HBITMAP hbmColor = 0, hbmMask = 0;
701 DWORD nFrames, delay_jiffies, i;
702 int color_size, mask_size;
703 BITMAPINFO *info = NULL;
704 XcursorImages *images;
705 XcursorImage **imgs;
706 Cursor cursor = 0;
708 /* Retrieve the number of frames to render */
709 if (!GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, 0, &delay_jiffies, &nFrames)) return 0;
710 if (!(imgs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XcursorImage*)*nFrames ))) return 0;
712 /* Allocate all of the resources necessary to obtain a cursor frame */
713 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
714 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
715 info->bmiHeader.biWidth = width;
716 info->bmiHeader.biHeight = -height;
717 info->bmiHeader.biPlanes = 1;
718 info->bmiHeader.biCompression = BI_RGB;
719 info->bmiHeader.biXPelsPerMeter = 0;
720 info->bmiHeader.biYPelsPerMeter = 0;
721 info->bmiHeader.biClrUsed = 0;
722 info->bmiHeader.biClrImportant = 0;
723 info->bmiHeader.biBitCount = 32;
724 color_size = width * height * 4;
725 info->bmiHeader.biSizeImage = color_size;
726 hbmColor = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &color_bits, NULL, 0);
727 if (!hbmColor)
729 ERR("Failed to create DIB section for cursor color data!\n");
730 goto cleanup;
732 info->bmiHeader.biBitCount = 1;
733 info->bmiColors[0].rgbRed = 0;
734 info->bmiColors[0].rgbGreen = 0;
735 info->bmiColors[0].rgbBlue = 0;
736 info->bmiColors[0].rgbReserved = 0;
737 info->bmiColors[1].rgbRed = 0xff;
738 info->bmiColors[1].rgbGreen = 0xff;
739 info->bmiColors[1].rgbBlue = 0xff;
740 info->bmiColors[1].rgbReserved = 0;
742 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
743 info->bmiHeader.biSizeImage = mask_size;
744 hbmMask = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &mask_bits, NULL, 0);
745 if (!hbmMask)
747 ERR("Failed to create DIB section for cursor mask data!\n");
748 goto cleanup;
751 /* Create an XcursorImage for each frame of the cursor */
752 for (i=0; i<nFrames; i++)
754 imgs[i] = create_xcursor_frame( hdc, iinfo, icon,
755 hbmColor, color_bits, color_size,
756 hbmMask, mask_bits, mask_size,
757 width, height, i );
758 if (!imgs[i]) goto cleanup;
761 /* Build an X cursor out of all of the frames */
762 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
763 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
764 images->images[images->nimage] = imgs[images->nimage];
765 cursor = pXcursorImagesLoadCursor( gdi_display, images );
766 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
767 HeapFree( GetProcessHeap(), 0, imgs );
768 imgs = NULL;
770 cleanup:
771 if (imgs)
773 /* Failed to produce a cursor, free previously allocated frames */
774 for (i=0; i<nFrames && imgs[i]; i++)
775 pXcursorImageDestroy( imgs[i] );
776 HeapFree( GetProcessHeap(), 0, imgs );
778 /* Cleanup all of the resources used to obtain the frame data */
779 if (hbmColor) DeleteObject( hbmColor );
780 if (hbmMask) DeleteObject( hbmMask );
781 HeapFree( GetProcessHeap(), 0, info );
782 return cursor;
786 struct system_cursors
788 WORD id;
789 const char *name;
792 static const struct system_cursors user32_cursors[] =
794 { OCR_NORMAL, "left_ptr" },
795 { OCR_IBEAM, "xterm" },
796 { OCR_WAIT, "watch" },
797 { OCR_CROSS, "cross" },
798 { OCR_UP, "center_ptr" },
799 { OCR_SIZE, "fleur" },
800 { OCR_SIZEALL, "fleur" },
801 { OCR_ICON, "icon" },
802 { OCR_SIZENWSE, "nwse-resize" },
803 { OCR_SIZENESW, "nesw-resize" },
804 { OCR_SIZEWE, "ew-resize" },
805 { OCR_SIZENS, "ns-resize" },
806 { OCR_NO, "not-allowed" },
807 { OCR_HAND, "hand2" },
808 { OCR_APPSTARTING, "left_ptr_watch" },
809 { OCR_HELP, "question_arrow" },
810 { 0 }
813 static const struct system_cursors comctl32_cursors[] =
815 { 102, "move" },
816 { 104, "copy" },
817 { 105, "left_ptr" },
818 { 106, "row-resize" },
819 { 107, "row-resize" },
820 { 108, "hand2" },
821 { 135, "col-resize" },
822 { 0 }
825 static const struct system_cursors ole32_cursors[] =
827 { 1, "no-drop" },
828 { 2, "move" },
829 { 3, "copy" },
830 { 4, "alias" },
831 { 0 }
834 static const struct system_cursors riched20_cursors[] =
836 { 105, "hand2" },
837 { 107, "right_ptr" },
838 { 109, "copy" },
839 { 110, "move" },
840 { 111, "no-drop" },
841 { 0 }
844 static const struct
846 const struct system_cursors *cursors;
847 WCHAR name[16];
848 } module_cursors[] =
850 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
851 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
852 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
853 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
856 /***********************************************************************
857 * create_xcursor_system_cursor
859 * Create an X cursor for a system cursor.
861 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
863 static const WCHAR idW[] = {'%','h','u',0};
864 const struct system_cursors *cursors;
865 unsigned int i;
866 Cursor cursor = 0;
867 HMODULE module;
868 HKEY key;
869 WCHAR *p, name[MAX_PATH * 2], valueW[64];
870 char valueA[64];
871 DWORD size, ret;
873 if (!pXcursorLibraryLoadCursor) return 0;
874 if (!info->szModName[0]) return 0;
876 p = strrchrW( info->szModName, '\\' );
877 strcpyW( name, p ? p + 1 : info->szModName );
878 p = name + strlenW( name );
879 *p++ = ',';
880 if (info->szResName[0]) strcpyW( p, info->szResName );
881 else sprintfW( p, idW, info->wResID );
882 valueA[0] = 0;
884 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
885 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver\\Cursors", &key ))
887 size = sizeof(valueW) / sizeof(WCHAR);
888 ret = RegQueryValueExW( key, name, NULL, NULL, (BYTE *)valueW, &size );
889 RegCloseKey( key );
890 if (!ret)
892 if (!valueW[0]) return 0; /* force standard cursor */
893 if (!WideCharToMultiByte( CP_UNIXCP, 0, valueW, -1, valueA, sizeof(valueA), NULL, NULL ))
894 valueA[0] = 0;
895 goto done;
899 if (info->szResName[0]) goto done; /* only integer resources are supported here */
900 if (!(module = GetModuleHandleW( info->szModName ))) goto done;
902 for (i = 0; i < sizeof(module_cursors)/sizeof(module_cursors[0]); i++)
903 if (GetModuleHandleW( module_cursors[i].name ) == module) break;
904 if (i == sizeof(module_cursors)/sizeof(module_cursors[0])) goto done;
906 cursors = module_cursors[i].cursors;
907 for (i = 0; cursors[i].id; i++)
908 if (cursors[i].id == info->wResID)
910 strcpy( valueA, cursors[i].name );
911 break;
914 done:
915 if (valueA[0])
917 cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
918 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
919 debugstr_w(name), debugstr_a(valueA) );
921 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
922 return cursor;
925 #endif /* SONAME_LIBXCURSOR */
928 /***********************************************************************
929 * create_xlib_monochrome_cursor
931 * Create a monochrome X cursor from a Windows one.
933 static Cursor create_xlib_monochrome_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
935 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
936 BITMAPINFO *info = (BITMAPINFO *)buffer;
937 const int and_y = 0;
938 const int xor_y = height;
939 unsigned int width_bytes = (width + 31) / 32 * 4;
940 unsigned char *mask_bits = NULL;
941 GC gc;
942 XColor fg, bg;
943 XVisualInfo vis;
944 Pixmap src_pixmap, bits_pixmap, mask_pixmap;
945 struct gdi_image_bits bits;
946 Cursor cursor = 0;
948 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
949 info->bmiHeader.biWidth = width;
950 info->bmiHeader.biHeight = -height * 2;
951 info->bmiHeader.biPlanes = 1;
952 info->bmiHeader.biBitCount = 1;
953 info->bmiHeader.biCompression = BI_RGB;
954 info->bmiHeader.biSizeImage = width_bytes * height * 2;
955 info->bmiHeader.biXPelsPerMeter = 0;
956 info->bmiHeader.biYPelsPerMeter = 0;
957 info->bmiHeader.biClrUsed = 0;
958 info->bmiHeader.biClrImportant = 0;
960 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
961 if (!GetDIBits( hdc, icon->hbmMask, 0, height * 2, mask_bits, info, DIB_RGB_COLORS )) goto done;
963 vis.depth = 1;
964 bits.ptr = mask_bits;
965 bits.free = NULL;
966 bits.is_copy = TRUE;
967 if (!(src_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
969 bits_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
970 mask_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
971 gc = XCreateGC( gdi_display, src_pixmap, 0, NULL );
972 XSetGraphicsExposures( gdi_display, gc, False );
974 /* We have to do some magic here, as cursors are not fully
975 * compatible between Windows and X11. Under X11, there are
976 * only 3 possible color cursor: black, white and masked. So
977 * we map the 4th Windows color (invert the bits on the screen)
978 * to black and an additional white bit on another place
979 * (+1,+1). This require some boolean arithmetic:
981 * Windows | X11
982 * And Xor Result | Bits Mask Result
983 * 0 0 black | 0 1 background
984 * 0 1 white | 1 1 foreground
985 * 1 0 no change | X 0 no change
986 * 1 1 inverted | 0 1 background
988 * which gives:
989 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
990 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
992 XSetFunction( gdi_display, gc, GXcopy );
993 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 0, 0 );
994 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 0, 0 );
995 XSetFunction( gdi_display, gc, GXandReverse );
996 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, xor_y, width, height, 0, 0 );
997 XSetFunction( gdi_display, gc, GXorReverse );
998 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, xor_y, width, height, 0, 0 );
999 /* additional white */
1000 XSetFunction( gdi_display, gc, GXand );
1001 XCopyArea( gdi_display, src_pixmap, src_pixmap, gc, 0, xor_y, width, height, 0, and_y );
1002 XSetFunction( gdi_display, gc, GXor );
1003 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 1, 1 );
1004 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 1, 1 );
1005 XFreeGC( gdi_display, gc );
1007 fg.red = fg.green = fg.blue = 0xffff;
1008 bg.red = bg.green = bg.blue = 0;
1009 cursor = XCreatePixmapCursor( gdi_display, bits_pixmap, mask_pixmap,
1010 &fg, &bg, icon->xHotspot, icon->yHotspot );
1011 XFreePixmap( gdi_display, src_pixmap );
1012 XFreePixmap( gdi_display, bits_pixmap );
1013 XFreePixmap( gdi_display, mask_pixmap );
1015 done:
1016 HeapFree( GetProcessHeap(), 0, mask_bits );
1017 return cursor;
1020 /***********************************************************************
1021 * create_xlib_color_cursor
1023 * Create a color X cursor from a Windows one.
1025 static Cursor create_xlib_color_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1027 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1028 BITMAPINFO *info = (BITMAPINFO *)buffer;
1029 XColor fg, bg;
1030 Cursor cursor = None;
1031 XVisualInfo vis;
1032 Pixmap xor_pixmap, mask_pixmap;
1033 struct gdi_image_bits bits;
1034 unsigned int *color_bits = NULL, *ptr;
1035 unsigned char *mask_bits = NULL, *xor_bits = NULL;
1036 int i, x, y, has_alpha = 0;
1037 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
1038 unsigned int width_bytes = (width + 31) / 32 * 4;
1040 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1041 info->bmiHeader.biWidth = width;
1042 info->bmiHeader.biHeight = -height;
1043 info->bmiHeader.biPlanes = 1;
1044 info->bmiHeader.biBitCount = 1;
1045 info->bmiHeader.biCompression = BI_RGB;
1046 info->bmiHeader.biSizeImage = width_bytes * height;
1047 info->bmiHeader.biXPelsPerMeter = 0;
1048 info->bmiHeader.biYPelsPerMeter = 0;
1049 info->bmiHeader.biClrUsed = 0;
1050 info->bmiHeader.biClrImportant = 0;
1052 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1053 if (!GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS )) goto done;
1055 info->bmiHeader.biBitCount = 32;
1056 info->bmiHeader.biSizeImage = width * height * 4;
1057 if (!(color_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1058 if (!(xor_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, width_bytes * height ))) goto done;
1059 GetDIBits( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS );
1061 /* compute fg/bg color and xor bitmap based on average of the color values */
1063 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
1064 for (y = 0, ptr = color_bits; y < height; y++)
1066 for (x = 0; x < width; x++, ptr++)
1068 int red = (*ptr >> 16) & 0xff;
1069 int green = (*ptr >> 8) & 0xff;
1070 int blue = (*ptr >> 0) & 0xff;
1071 if (red + green + blue > 0x40)
1073 rfg += red;
1074 gfg += green;
1075 bfg += blue;
1076 fgBits++;
1077 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1079 else
1081 rbg += red;
1082 gbg += green;
1083 bbg += blue;
1087 if (fgBits)
1089 fg.red = rfg * 257 / fgBits;
1090 fg.green = gfg * 257 / fgBits;
1091 fg.blue = bfg * 257 / fgBits;
1093 else fg.red = fg.green = fg.blue = 0;
1094 bgBits = width * height - fgBits;
1095 if (bgBits)
1097 bg.red = rbg * 257 / bgBits;
1098 bg.green = gbg * 257 / bgBits;
1099 bg.blue = bbg * 257 / bgBits;
1101 else bg.red = bg.green = bg.blue = 0;
1103 info->bmiHeader.biBitCount = 1;
1104 info->bmiHeader.biClrUsed = 0;
1105 info->bmiHeader.biSizeImage = width_bytes * height;
1107 /* generate mask from the alpha channel if we have one */
1109 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
1110 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
1112 if (has_alpha)
1114 memset( mask_bits, 0, width_bytes * height );
1115 for (y = 0, ptr = color_bits; y < height; y++)
1116 for (x = 0; x < width; x++, ptr++)
1117 if ((*ptr >> 24) > 25) /* more than 10% alpha */
1118 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1120 else /* invert the mask */
1122 ptr = (unsigned int *)mask_bits;
1123 for (i = 0; i < info->bmiHeader.biSizeImage / sizeof(*ptr); i++, ptr++) *ptr ^= ~0u;
1126 vis.depth = 1;
1127 bits.ptr = xor_bits;
1128 bits.free = NULL;
1129 bits.is_copy = TRUE;
1130 if (!(xor_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1132 bits.ptr = mask_bits;
1133 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
1135 if (mask_pixmap)
1137 cursor = XCreatePixmapCursor( gdi_display, xor_pixmap, mask_pixmap,
1138 &fg, &bg, icon->xHotspot, icon->yHotspot );
1139 XFreePixmap( gdi_display, mask_pixmap );
1141 XFreePixmap( gdi_display, xor_pixmap );
1143 done:
1144 HeapFree( GetProcessHeap(), 0, color_bits );
1145 HeapFree( GetProcessHeap(), 0, xor_bits );
1146 HeapFree( GetProcessHeap(), 0, mask_bits );
1147 return cursor;
1150 /***********************************************************************
1151 * create_cursor
1153 * Create an X cursor from a Windows one.
1155 static Cursor create_cursor( HANDLE handle )
1157 Cursor cursor = 0;
1158 ICONINFOEXW info;
1159 BITMAP bm;
1160 HDC hdc;
1162 if (!handle) return get_empty_cursor();
1164 info.cbSize = sizeof(info);
1165 if (!GetIconInfoExW( handle, &info )) return 0;
1167 #ifdef SONAME_LIBXCURSOR
1168 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
1170 DeleteObject( info.hbmColor );
1171 DeleteObject( info.hbmMask );
1172 return cursor;
1174 #endif
1176 GetObjectW( info.hbmMask, sizeof(bm), &bm );
1177 if (!info.hbmColor) bm.bmHeight = max( 1, bm.bmHeight / 2 );
1179 /* make sure hotspot is valid */
1180 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1182 info.xHotspot = bm.bmWidth / 2;
1183 info.yHotspot = bm.bmHeight / 2;
1186 hdc = CreateCompatibleDC( 0 );
1188 if (info.hbmColor)
1190 #ifdef SONAME_LIBXCURSOR
1191 if (pXcursorImagesLoadCursor)
1192 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
1193 #endif
1194 if (!cursor) cursor = create_xlib_color_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1195 DeleteObject( info.hbmColor );
1197 else
1199 cursor = create_xlib_monochrome_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1202 DeleteObject( info.hbmMask );
1203 DeleteDC( hdc );
1204 return cursor;
1207 /***********************************************************************
1208 * DestroyCursorIcon (X11DRV.@)
1210 void CDECL X11DRV_DestroyCursorIcon( HCURSOR handle )
1212 Cursor cursor;
1214 wine_tsx11_lock();
1215 if (cursor_context && !XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1217 TRACE( "%p xid %lx\n", handle, cursor );
1218 XFreeCursor( gdi_display, cursor );
1219 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1221 wine_tsx11_unlock();
1224 /***********************************************************************
1225 * SetCursor (X11DRV.@)
1227 void CDECL X11DRV_SetCursor( HCURSOR handle )
1229 if (InterlockedExchangePointer( (void **)&last_cursor, handle ) != handle ||
1230 GetTickCount() - last_cursor_change > 100)
1232 last_cursor_change = GetTickCount();
1233 if (cursor_window) SendNotifyMessageW( cursor_window, WM_X11DRV_SET_CURSOR, 0, (LPARAM)handle );
1237 /***********************************************************************
1238 * SetCursorPos (X11DRV.@)
1240 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
1242 struct x11drv_thread_data *data = x11drv_init_thread_data();
1244 XWarpPointer( data->display, root_window, root_window, 0, 0, 0, 0,
1245 x - virtual_screen_rect.left, y - virtual_screen_rect.top );
1246 data->warp_serial = NextRequest( data->display );
1247 XNoOp( data->display );
1248 XFlush( data->display ); /* avoids bad mouse lag in games that do their own mouse warping */
1249 TRACE( "warped to %d,%d serial %lu\n", x, y, data->warp_serial );
1250 return TRUE;
1253 /***********************************************************************
1254 * GetCursorPos (X11DRV.@)
1256 BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
1258 Display *display = thread_init_display();
1259 Window root, child;
1260 int rootX, rootY, winX, winY;
1261 unsigned int xstate;
1262 BOOL ret;
1264 wine_tsx11_lock();
1265 ret = XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &winX, &winY, &xstate );
1266 if (ret)
1268 POINT old = *pos;
1269 pos->x = winX + virtual_screen_rect.left;
1270 pos->y = winY + virtual_screen_rect.top;
1271 TRACE( "pointer at (%d,%d) server pos %d,%d\n", pos->x, pos->y, old.x, old.y );
1273 wine_tsx11_unlock();
1274 return ret;
1277 /***********************************************************************
1278 * ClipCursor (X11DRV.@)
1280 BOOL CDECL X11DRV_ClipCursor( LPCRECT clip )
1282 if (!clip)
1284 ungrab_clipping_window();
1285 return TRUE;
1288 if (GetWindowThreadProcessId( GetDesktopWindow(), NULL ) == GetCurrentThreadId())
1289 return TRUE; /* don't clip in the desktop process */
1291 if (grab_pointer)
1293 HWND foreground = GetForegroundWindow();
1295 /* we are clipping if the clip rectangle is smaller than the screen */
1296 if (clip->left > virtual_screen_rect.left || clip->right < virtual_screen_rect.right ||
1297 clip->top > virtual_screen_rect.top || clip->bottom < virtual_screen_rect.bottom)
1299 DWORD tid, pid;
1301 /* forward request to the foreground window if it's in a different thread */
1302 tid = GetWindowThreadProcessId( foreground, &pid );
1303 if (tid && tid != GetCurrentThreadId() && pid == GetCurrentProcessId())
1305 TRACE( "forwarding clip request to %p\n", foreground );
1306 SendNotifyMessageW( foreground, WM_X11DRV_CLIP_CURSOR, 0, 0 );
1307 return TRUE;
1309 else if (grab_clipping_window( clip )) return TRUE;
1311 else /* if currently clipping, check if we should switch to fullscreen clipping */
1313 struct x11drv_thread_data *data = x11drv_thread_data();
1314 if (data && data->clip_hwnd)
1316 if (EqualRect( clip, &clip_rect ) || clip_fullscreen_window( foreground, TRUE ))
1317 return TRUE;
1321 ungrab_clipping_window();
1322 return TRUE;
1325 /***********************************************************************
1326 * move_resize_window
1328 void move_resize_window( Display *display, struct x11drv_win_data *data, int dir )
1330 DWORD pt;
1331 int x, y, rootX, rootY, button = 0;
1332 XEvent xev;
1333 Window root, child;
1334 unsigned int xstate;
1336 pt = GetMessagePos();
1337 x = (short)LOWORD( pt );
1338 y = (short)HIWORD( pt );
1340 if (GetKeyState( VK_LBUTTON ) & 0x8000) button = 1;
1341 else if (GetKeyState( VK_MBUTTON ) & 0x8000) button = 2;
1342 else if (GetKeyState( VK_RBUTTON ) & 0x8000) button = 3;
1344 TRACE( "hwnd %p/%lx, x %d, y %d, dir %d, button %d\n",
1345 data->hwnd, data->whole_window, x, y, dir, button );
1347 xev.xclient.type = ClientMessage;
1348 xev.xclient.window = data->whole_window;
1349 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1350 xev.xclient.serial = 0;
1351 xev.xclient.display = display;
1352 xev.xclient.send_event = True;
1353 xev.xclient.format = 32;
1354 xev.xclient.data.l[0] = x - virtual_screen_rect.left; /* x coord */
1355 xev.xclient.data.l[1] = y - virtual_screen_rect.top; /* y coord */
1356 xev.xclient.data.l[2] = dir; /* direction */
1357 xev.xclient.data.l[3] = button; /* button */
1358 xev.xclient.data.l[4] = 0; /* unused */
1360 /* need to ungrab the pointer that may have been automatically grabbed
1361 * with a ButtonPress event */
1362 XUngrabPointer( display, CurrentTime );
1363 XSendEvent(display, root_window, False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
1365 /* try to detect the end of the size/move by polling for the mouse button to be released */
1366 /* (some apps don't like it if we return before the size/move is done) */
1368 if (!button) return;
1369 for (;;)
1371 MSG msg;
1372 INPUT input;
1374 if (!XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &x, &y, &xstate )) break;
1376 if (!(xstate & (Button1Mask << (button - 1))))
1378 /* fake a button release event */
1379 input.type = INPUT_MOUSE;
1380 input.u.mi.dx = x + virtual_screen_rect.left;
1381 input.u.mi.dy = y + virtual_screen_rect.top;
1382 input.u.mi.mouseData = button_up_data[button - 1];
1383 input.u.mi.dwFlags = button_up_flags[button - 1] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1384 input.u.mi.time = GetTickCount();
1385 input.u.mi.dwExtraInfo = 0;
1386 __wine_send_input( data->hwnd, &input );
1389 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
1391 if (!CallMsgFilterW( &msg, MSGF_SIZE ))
1393 TranslateMessage( &msg );
1394 DispatchMessageW( &msg );
1398 if (!(xstate & (Button1Mask << (button - 1)))) break;
1399 MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT );
1402 TRACE( "hwnd %p/%lx done\n", data->hwnd, data->whole_window );
1406 /***********************************************************************
1407 * X11DRV_ButtonPress
1409 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1411 XButtonEvent *event = &xev->xbutton;
1412 int buttonNum = event->button - 1;
1413 INPUT input;
1415 if (buttonNum >= NB_BUTTONS) return;
1417 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1419 input.u.mi.dx = event->x;
1420 input.u.mi.dy = event->y;
1421 input.u.mi.mouseData = button_down_data[buttonNum];
1422 input.u.mi.dwFlags = button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1423 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1424 input.u.mi.dwExtraInfo = 0;
1426 update_user_time( event->time );
1427 send_mouse_input( hwnd, event->window, event->state, &input );
1431 /***********************************************************************
1432 * X11DRV_ButtonRelease
1434 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1436 XButtonEvent *event = &xev->xbutton;
1437 int buttonNum = event->button - 1;
1438 INPUT input;
1440 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1442 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1444 input.u.mi.dx = event->x;
1445 input.u.mi.dy = event->y;
1446 input.u.mi.mouseData = button_up_data[buttonNum];
1447 input.u.mi.dwFlags = button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1448 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1449 input.u.mi.dwExtraInfo = 0;
1451 send_mouse_input( hwnd, event->window, event->state, &input );
1455 /***********************************************************************
1456 * X11DRV_MotionNotify
1458 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1460 XMotionEvent *event = &xev->xmotion;
1461 INPUT input;
1463 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d serial %lu\n",
1464 hwnd, event->window, event->x, event->y, event->is_hint, event->serial );
1466 input.u.mi.dx = event->x;
1467 input.u.mi.dy = event->y;
1468 input.u.mi.mouseData = 0;
1469 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1470 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1471 input.u.mi.dwExtraInfo = 0;
1473 if (!hwnd)
1475 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1476 if (thread_data->warp_serial && (long)(event->serial - thread_data->warp_serial) < 0) return;
1479 send_mouse_input( hwnd, event->window, event->state, &input );
1483 /***********************************************************************
1484 * X11DRV_EnterNotify
1486 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1488 XCrossingEvent *event = &xev->xcrossing;
1489 INPUT input;
1491 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd, event->window, event->x, event->y, event->detail );
1493 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1494 if (event->window == x11drv_thread_data()->grab_window) return;
1496 /* simulate a mouse motion event */
1497 input.u.mi.dx = event->x;
1498 input.u.mi.dy = event->y;
1499 input.u.mi.mouseData = 0;
1500 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1501 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1502 input.u.mi.dwExtraInfo = 0;
1504 send_mouse_input( hwnd, event->window, event->state, &input );
1507 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1509 /***********************************************************************
1510 * X11DRV_RawMotion
1512 static void X11DRV_RawMotion( XGenericEventCookie *xev )
1514 XIRawEvent *event = xev->data;
1515 const double *values = event->valuators.values;
1516 INPUT input;
1517 int i, j;
1518 double dx = 0, dy = 0;
1519 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1521 if (!event->valuators.mask_len) return;
1522 if (thread_data->xi2_state != xi_enabled) return;
1524 input.u.mi.mouseData = 0;
1525 input.u.mi.dwFlags = MOUSEEVENTF_MOVE;
1526 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1527 input.u.mi.dwExtraInfo = 0;
1529 if (XIMaskIsSet( event->valuators.mask, 0 )) dx = *values++;
1530 if (XIMaskIsSet( event->valuators.mask, 1 )) dy = *values++;
1531 input.u.mi.dx = dx;
1532 input.u.mi.dy = dy;
1534 wine_tsx11_lock();
1535 for (i = 0; i < xinput2_device_count; ++i)
1537 if (xinput2_devices[i].deviceid != event->deviceid) continue;
1538 for (j = 0; j < xinput2_devices[i].num_classes; j++)
1540 XIValuatorClassInfo *class = (XIValuatorClassInfo *)xinput2_devices[i].classes[j];
1542 if (xinput2_devices[i].classes[j]->type != XIValuatorClass) continue;
1543 if (class->min >= class->max) continue;
1544 if (class->number == 0)
1545 input.u.mi.dx = dx * (virtual_screen_rect.right - virtual_screen_rect.left)
1546 / (class->max - class->min);
1547 else if (class->number == 1)
1548 input.u.mi.dy = dy * (virtual_screen_rect.bottom - virtual_screen_rect.top)
1549 / (class->max - class->min);
1551 break;
1554 wine_tsx11_unlock();
1556 if (thread_data->warp_serial)
1558 if ((long)(xev->serial - thread_data->warp_serial) < 0)
1560 TRACE( "pos %d,%d old serial %lu, ignoring\n", input.u.mi.dx, input.u.mi.dy, xev->serial );
1561 return;
1563 thread_data->warp_serial = 0; /* we caught up now */
1566 TRACE( "pos %d,%d (event %f,%f)\n", input.u.mi.dx, input.u.mi.dy, dx, dy );
1568 input.type = INPUT_MOUSE;
1569 __wine_send_input( 0, &input );
1572 #endif /* HAVE_X11_EXTENSIONS_XINPUT2_H */
1575 /***********************************************************************
1576 * X11DRV_XInput2_Init
1578 void X11DRV_XInput2_Init(void)
1580 #if defined(SONAME_LIBXI) && defined(HAVE_X11_EXTENSIONS_XINPUT2_H)
1581 int event, error;
1582 void *libxi_handle = wine_dlopen( SONAME_LIBXI, RTLD_NOW, NULL, 0 );
1584 if (!libxi_handle)
1586 WARN( "couldn't load %s\n", SONAME_LIBXI );
1587 return;
1589 #define LOAD_FUNCPTR(f) \
1590 if (!(p##f = wine_dlsym( libxi_handle, #f, NULL, 0))) \
1592 WARN("Failed to load %s.\n", #f); \
1593 return; \
1596 LOAD_FUNCPTR(XIFreeDeviceInfo);
1597 LOAD_FUNCPTR(XIQueryDevice);
1598 LOAD_FUNCPTR(XIQueryVersion);
1599 LOAD_FUNCPTR(XISelectEvents);
1600 #undef LOAD_FUNCPTR
1602 xinput2_available = XQueryExtension( gdi_display, "XInputExtension", &xinput2_opcode, &event, &error );
1603 #else
1604 TRACE( "X Input 2 support not compiled in.\n" );
1605 #endif
1609 /***********************************************************************
1610 * X11DRV_GenericEvent
1612 void X11DRV_GenericEvent( HWND hwnd, XEvent *xev )
1614 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1615 XGenericEventCookie *event = &xev->xcookie;
1617 if (!event->data) return;
1618 if (event->extension != xinput2_opcode) return;
1620 switch (event->evtype)
1622 case XI_RawMotion:
1623 X11DRV_RawMotion( event );
1624 break;
1626 default:
1627 TRACE( "Unhandled event %#x\n", event->evtype );
1628 break;
1630 #endif