winex11: Create contexts at initialization time to avoid the need for locks.
[wine/multimedia.git] / dlls / winex11.drv / mouse.c
blobcaa2530935634cb1a55cd12bb92de06bfdd0b45f
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 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);
139 #undef MAKE_FUNCPTR
140 #endif
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");
154 return;
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);
166 #undef LOAD_FUNCPTR
167 #endif /* SONAME_LIBXCURSOR */
171 /***********************************************************************
172 * get_empty_cursor
174 static Cursor get_empty_cursor(void)
176 static Cursor cursor;
177 static const char data[] = { 0 };
179 wine_tsx11_lock();
180 if (!cursor)
182 XColor bg;
183 Pixmap pixmap;
185 bg.red = bg.green = bg.blue = 0x0000;
186 pixmap = XCreateBitmapFromData( gdi_display, root_window, data, 1, 1 );
187 if (pixmap)
189 cursor = XCreatePixmapCursor( gdi_display, pixmap, pixmap, &bg, &bg, 0, 0 );
190 XFreePixmap( gdi_display, pixmap );
193 wine_tsx11_unlock();
194 return cursor;
197 /***********************************************************************
198 * set_window_cursor
200 void set_window_cursor( Window window, HCURSOR handle )
202 Cursor cursor, prev;
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 wine_tsx11_lock();
211 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&prev ))
213 /* someone else was here first */
214 XFreeCursor( gdi_display, cursor );
215 cursor = prev;
217 else
219 XSaveContext( gdi_display, (XID)handle, cursor_context, (char *)cursor );
220 TRACE( "cursor %p created %lx\n", handle, cursor );
222 wine_tsx11_unlock();
225 XDefineCursor( gdi_display, window, cursor );
226 /* make the change take effect immediately */
227 XFlush( gdi_display );
230 /***********************************************************************
231 * sync_window_cursor
233 void sync_window_cursor( Window window )
235 HCURSOR cursor;
237 SERVER_START_REQ( set_cursor )
239 req->flags = 0;
240 wine_server_call( req );
241 cursor = reply->prev_count >= 0 ? wine_server_ptr_handle( reply->prev_handle ) : 0;
243 SERVER_END_REQ;
245 set_window_cursor( window, cursor );
248 /***********************************************************************
249 * enable_xinput2
251 static void enable_xinput2(void)
253 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
254 struct x11drv_thread_data *data = x11drv_thread_data();
255 XIEventMask mask;
256 XIDeviceInfo *devices;
257 unsigned char mask_bits[XIMaskLen(XI_LASTEVENT)];
258 int i, j, count;
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;
266 else
268 data->xi2_state = xi_unavailable;
269 WARN( "X Input 2 not available\n" );
272 if (data->xi2_state == xi_unavailable) return;
274 wine_tsx11_lock();
275 if (data->xi2_devices) pXIFreeDeviceInfo( data->xi2_devices );
276 data->xi2_devices = devices = pXIQueryDevice( data->display, XIAllDevices, &data->xi2_device_count );
277 for (i = 0; i < data->xi2_device_count; ++i)
279 if (devices[i].use != XIMasterPointer) continue;
280 for (j = count = 0; j < devices[i].num_classes; j++)
282 XIValuatorClassInfo *class = (XIValuatorClassInfo *)devices[i].classes[j];
284 if (devices[i].classes[j]->type != XIValuatorClass) continue;
285 TRACE( "Device %u (%s) num %u %f,%f res %u mode %u label %s\n",
286 devices[i].deviceid, debugstr_a(devices[i].name),
287 class->number, class->min, class->max, class->resolution, class->mode,
288 XGetAtomName( data->display, class->label ));
289 if (class->label == x11drv_atom( Rel_X ) || class->label == x11drv_atom( Rel_Y )) count++;
291 if (count < 2) continue;
292 TRACE( "Using %u (%s) as core pointer\n",
293 devices[i].deviceid, debugstr_a(devices[i].name) );
294 data->xi2_core_pointer = devices[i].deviceid;
295 break;
298 mask.mask = mask_bits;
299 mask.mask_len = sizeof(mask_bits);
300 memset( mask_bits, 0, sizeof(mask_bits) );
301 XISetMask( mask_bits, XI_RawMotion );
302 XISetMask( mask_bits, XI_ButtonPress );
304 for (i = 0; i < data->xi2_device_count; ++i)
306 if (devices[i].use == XISlavePointer && devices[i].attachment == data->xi2_core_pointer)
308 TRACE( "Device %u (%s) is attached to the core pointer\n",
309 devices[i].deviceid, debugstr_a(devices[i].name) );
310 mask.deviceid = devices[i].deviceid;
311 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
312 data->xi2_state = xi_enabled;
316 wine_tsx11_unlock();
317 #endif
320 /***********************************************************************
321 * disable_xinput2
323 static void disable_xinput2(void)
325 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
326 struct x11drv_thread_data *data = x11drv_thread_data();
327 XIDeviceInfo *devices = data->xi2_devices;
328 XIEventMask mask;
329 int i;
331 if (data->xi2_state != xi_enabled) return;
333 TRACE( "disabling\n" );
334 data->xi2_state = xi_disabled;
336 mask.mask = NULL;
337 mask.mask_len = 0;
339 wine_tsx11_lock();
340 for (i = 0; i < data->xi2_device_count; ++i)
342 if (devices[i].use == XISlavePointer && devices[i].attachment == data->xi2_core_pointer)
344 mask.deviceid = devices[i].deviceid;
345 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
348 pXIFreeDeviceInfo( devices );
349 data->xi2_devices = NULL;
350 data->xi2_device_count = 0;
351 wine_tsx11_unlock();
352 #endif
356 /***********************************************************************
357 * grab_clipping_window
359 * Start a pointer grab on the clip window.
361 static BOOL grab_clipping_window( const RECT *clip )
363 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
364 struct x11drv_thread_data *data = x11drv_thread_data();
365 Window clip_window;
366 HWND msg_hwnd = 0;
368 if (!data) return FALSE;
369 if (!(clip_window = init_clip_window())) return TRUE;
371 if (!(msg_hwnd = CreateWindowW( messageW, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0,
372 GetModuleHandleW(0), NULL )))
373 return TRUE;
375 /* enable XInput2 unless we are already clipping */
376 if (!data->clip_hwnd) enable_xinput2();
378 if (data->xi2_state != xi_enabled)
380 WARN( "XInput2 not supported, refusing to clip to %s\n", wine_dbgstr_rect(clip) );
381 DestroyWindow( msg_hwnd );
382 ClipCursor( NULL );
383 return TRUE;
386 TRACE( "clipping to %s win %lx\n", wine_dbgstr_rect(clip), clip_window );
388 wine_tsx11_lock();
389 if (!data->clip_hwnd) XUnmapWindow( data->display, clip_window );
390 XMoveResizeWindow( data->display, clip_window,
391 clip->left - virtual_screen_rect.left, clip->top - virtual_screen_rect.top,
392 max( 1, clip->right - clip->left ), max( 1, clip->bottom - clip->top ) );
393 XMapWindow( data->display, clip_window );
395 /* if the rectangle is shrinking we may get a pointer warp */
396 if (!data->clip_hwnd || clip->left > clip_rect.left || clip->top > clip_rect.top ||
397 clip->right < clip_rect.right || clip->bottom < clip_rect.bottom)
398 data->warp_serial = NextRequest( data->display );
400 if (!XGrabPointer( data->display, clip_window, False,
401 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
402 GrabModeAsync, GrabModeAsync, clip_window, None, CurrentTime ))
403 clipping_cursor = 1;
404 wine_tsx11_unlock();
406 if (!clipping_cursor)
408 disable_xinput2();
409 DestroyWindow( msg_hwnd );
410 return FALSE;
412 clip_rect = *clip;
413 if (!data->clip_hwnd) sync_window_cursor( clip_window );
414 InterlockedExchangePointer( (void **)&cursor_window, msg_hwnd );
415 data->clip_hwnd = msg_hwnd;
416 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, (LPARAM)msg_hwnd );
417 return TRUE;
420 /***********************************************************************
421 * ungrab_clipping_window
423 * Release the pointer grab on the clip window.
425 void ungrab_clipping_window(void)
427 Display *display = thread_init_display();
428 Window clip_window = init_clip_window();
430 if (!clip_window) return;
432 TRACE( "no longer clipping\n" );
433 XUnmapWindow( display, clip_window );
434 clipping_cursor = 0;
435 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, 0 );
438 /***********************************************************************
439 * reset_clipping_window
441 * Forcibly reset the window clipping on external events.
443 void reset_clipping_window(void)
445 ungrab_clipping_window();
446 ClipCursor( NULL ); /* make sure the clip rectangle is reset too */
449 /***********************************************************************
450 * clip_cursor_notify
452 * Notification function called upon receiving a WM_X11DRV_CLIP_CURSOR.
454 LRESULT clip_cursor_notify( HWND hwnd, HWND new_clip_hwnd )
456 struct x11drv_thread_data *data = x11drv_thread_data();
458 if (hwnd == GetDesktopWindow()) /* change the clip window stored in the desktop process */
460 static HWND clip_hwnd;
462 HWND prev = clip_hwnd;
463 clip_hwnd = new_clip_hwnd;
464 if (prev || new_clip_hwnd) TRACE( "clip hwnd changed from %p to %p\n", prev, new_clip_hwnd );
465 if (prev) SendNotifyMessageW( prev, WM_X11DRV_CLIP_CURSOR, 0, 0 );
467 else if (hwnd == data->clip_hwnd) /* this is a notification that clipping has been reset */
469 TRACE( "clip hwnd reset from %p\n", hwnd );
470 data->clip_hwnd = 0;
471 data->clip_reset = GetTickCount();
472 disable_xinput2();
473 DestroyWindow( hwnd );
475 else if (hwnd == GetForegroundWindow()) /* request to clip */
477 RECT clip;
479 GetClipCursor( &clip );
480 if (clip.left > virtual_screen_rect.left || clip.right < virtual_screen_rect.right ||
481 clip.top > virtual_screen_rect.top || clip.bottom < virtual_screen_rect.bottom)
482 return grab_clipping_window( &clip );
484 return 0;
487 /***********************************************************************
488 * clip_fullscreen_window
490 * Turn on clipping if the active window is fullscreen.
492 BOOL clip_fullscreen_window( HWND hwnd, BOOL reset )
494 struct x11drv_win_data *data;
495 struct x11drv_thread_data *thread_data;
496 RECT rect;
497 DWORD style;
499 if (hwnd == GetDesktopWindow()) return FALSE;
500 if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
501 style = GetWindowLongW( hwnd, GWL_STYLE );
502 if (!(style & WS_VISIBLE)) return FALSE;
503 if ((style & (WS_POPUP | WS_CHILD)) == WS_CHILD) return FALSE;
504 /* maximized windows don't count as full screen */
505 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION) return FALSE;
506 if (!is_window_rect_fullscreen( &data->whole_rect )) return FALSE;
507 if (!(thread_data = x11drv_thread_data())) return FALSE;
508 if (GetTickCount() - thread_data->clip_reset < 1000) return FALSE;
509 if (!reset && clipping_cursor && thread_data->clip_hwnd) return FALSE; /* already clipping */
510 SetRect( &rect, 0, 0, screen_width, screen_height );
511 if (!grab_fullscreen)
513 if (!EqualRect( &rect, &virtual_screen_rect )) return FALSE;
514 if (root_window != DefaultRootWindow( gdi_display )) return FALSE;
516 TRACE( "win %p clipping fullscreen\n", hwnd );
517 return grab_clipping_window( &rect );
520 /***********************************************************************
521 * send_mouse_input
523 * Update the various window states on a mouse event.
525 static void send_mouse_input( HWND hwnd, Window window, unsigned int state, INPUT *input )
527 struct x11drv_win_data *data;
528 POINT pt;
530 input->type = INPUT_MOUSE;
532 if (!hwnd)
534 struct x11drv_thread_data *thread_data = x11drv_thread_data();
535 HWND clip_hwnd = thread_data->clip_hwnd;
537 if (!clip_hwnd) return;
538 if (thread_data->clip_window != window) return;
539 if (InterlockedExchangePointer( (void **)&cursor_window, clip_hwnd ) != clip_hwnd ||
540 input->u.mi.time - last_cursor_change > 100)
542 sync_window_cursor( window );
543 last_cursor_change = input->u.mi.time;
545 input->u.mi.dx += clip_rect.left;
546 input->u.mi.dy += clip_rect.top;
547 __wine_send_input( hwnd, input );
548 return;
551 if (!(data = X11DRV_get_win_data( hwnd ))) return;
553 if (window == data->whole_window)
555 input->u.mi.dx += data->whole_rect.left - data->client_rect.left;
556 input->u.mi.dy += data->whole_rect.top - data->client_rect.top;
558 if (window == root_window)
560 input->u.mi.dx += virtual_screen_rect.left;
561 input->u.mi.dy += virtual_screen_rect.top;
563 pt.x = input->u.mi.dx;
564 pt.y = input->u.mi.dy;
565 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
566 pt.x = data->client_rect.right - data->client_rect.left - 1 - pt.x;
567 MapWindowPoints( hwnd, 0, &pt, 1 );
569 if (InterlockedExchangePointer( (void **)&cursor_window, hwnd ) != hwnd ||
570 input->u.mi.time - last_cursor_change > 100)
572 sync_window_cursor( data->whole_window );
573 last_cursor_change = input->u.mi.time;
576 if (hwnd != GetDesktopWindow())
578 hwnd = GetAncestor( hwnd, GA_ROOT );
579 if ((input->u.mi.dwFlags & (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_RIGHTDOWN)) && hwnd == GetForegroundWindow())
580 clip_fullscreen_window( hwnd, FALSE );
583 /* update the wine server Z-order */
585 if (window != x11drv_thread_data()->grab_window &&
586 /* ignore event if a button is pressed, since the mouse is then grabbed too */
587 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
589 RECT rect;
590 SetRect( &rect, pt.x, pt.y, pt.x + 1, pt.y + 1 );
591 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
593 SERVER_START_REQ( update_window_zorder )
595 req->window = wine_server_user_handle( hwnd );
596 req->rect.left = rect.left;
597 req->rect.top = rect.top;
598 req->rect.right = rect.right;
599 req->rect.bottom = rect.bottom;
600 wine_server_call( req );
602 SERVER_END_REQ;
605 input->u.mi.dx = pt.x;
606 input->u.mi.dy = pt.y;
607 __wine_send_input( hwnd, input );
610 #ifdef SONAME_LIBXCURSOR
612 /***********************************************************************
613 * create_xcursor_frame
615 * Use Xcursor to create a frame of an X cursor from a Windows one.
617 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
618 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
619 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
620 int width, int height, int istep )
622 XcursorImage *image, *ret = NULL;
623 DWORD delay_jiffies, num_steps;
624 int x, y, i, has_alpha = FALSE;
625 XcursorPixel *ptr;
627 image = pXcursorImageCreate( width, height );
628 if (!image)
630 ERR("X11 failed to produce a cursor frame!\n");
631 return NULL;
634 image->xhot = iinfo->xHotspot;
635 image->yhot = iinfo->yHotspot;
637 image->delay = 100; /* fallback delay, 100 ms */
638 if (GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, istep, &delay_jiffies, &num_steps) != 0)
639 image->delay = (100 * delay_jiffies) / 6; /* convert jiffies (1/60s) to milliseconds */
640 else
641 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep);
643 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
644 memset( color_bits, 0x00, color_size );
645 SelectObject( hdc, hbmColor );
646 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
648 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
649 goto cleanup;
651 memcpy( image->pixels, color_bits, color_size );
653 /* check if the cursor frame was drawn with an alpha channel */
654 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
655 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
657 /* if no alpha channel was drawn then generate it from the mask */
658 if (!has_alpha)
660 unsigned int width_bytes = (width + 31) / 32 * 4;
662 /* draw the cursor mask to a temporary buffer */
663 memset( mask_bits, 0xFF, mask_size );
664 SelectObject( hdc, hbmMask );
665 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
667 ERR("Failed to draw frame mask %d.\n", istep);
668 goto cleanup;
670 /* use the buffer to directly modify the XcursorImage alpha channel */
671 for (y = 0, ptr = image->pixels; y < height; y++)
672 for (x = 0; x < width; x++, ptr++)
673 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
674 *ptr |= 0xff000000;
676 ret = image;
678 cleanup:
679 if (ret == NULL) pXcursorImageDestroy( image );
680 return ret;
683 /***********************************************************************
684 * create_xcursor_cursor
686 * Use Xcursor to create an X cursor from a Windows one.
688 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
690 unsigned char *color_bits, *mask_bits;
691 HBITMAP hbmColor = 0, hbmMask = 0;
692 DWORD nFrames, delay_jiffies, i;
693 int color_size, mask_size;
694 BITMAPINFO *info = NULL;
695 XcursorImages *images;
696 XcursorImage **imgs;
697 Cursor cursor = 0;
699 /* Retrieve the number of frames to render */
700 if (!GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, 0, &delay_jiffies, &nFrames)) return 0;
701 if (!(imgs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XcursorImage*)*nFrames ))) return 0;
703 /* Allocate all of the resources necessary to obtain a cursor frame */
704 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
705 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
706 info->bmiHeader.biWidth = width;
707 info->bmiHeader.biHeight = -height;
708 info->bmiHeader.biPlanes = 1;
709 info->bmiHeader.biCompression = BI_RGB;
710 info->bmiHeader.biXPelsPerMeter = 0;
711 info->bmiHeader.biYPelsPerMeter = 0;
712 info->bmiHeader.biClrUsed = 0;
713 info->bmiHeader.biClrImportant = 0;
714 info->bmiHeader.biBitCount = 32;
715 color_size = width * height * 4;
716 info->bmiHeader.biSizeImage = color_size;
717 hbmColor = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &color_bits, NULL, 0);
718 if (!hbmColor)
720 ERR("Failed to create DIB section for cursor color data!\n");
721 goto cleanup;
723 info->bmiHeader.biBitCount = 1;
724 info->bmiColors[0].rgbRed = 0;
725 info->bmiColors[0].rgbGreen = 0;
726 info->bmiColors[0].rgbBlue = 0;
727 info->bmiColors[0].rgbReserved = 0;
728 info->bmiColors[1].rgbRed = 0xff;
729 info->bmiColors[1].rgbGreen = 0xff;
730 info->bmiColors[1].rgbBlue = 0xff;
731 info->bmiColors[1].rgbReserved = 0;
733 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
734 info->bmiHeader.biSizeImage = mask_size;
735 hbmMask = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &mask_bits, NULL, 0);
736 if (!hbmMask)
738 ERR("Failed to create DIB section for cursor mask data!\n");
739 goto cleanup;
742 /* Create an XcursorImage for each frame of the cursor */
743 for (i=0; i<nFrames; i++)
745 imgs[i] = create_xcursor_frame( hdc, iinfo, icon,
746 hbmColor, color_bits, color_size,
747 hbmMask, mask_bits, mask_size,
748 width, height, i );
749 if (!imgs[i]) goto cleanup;
752 /* Build an X cursor out of all of the frames */
753 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
754 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
755 images->images[images->nimage] = imgs[images->nimage];
756 cursor = pXcursorImagesLoadCursor( gdi_display, images );
757 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
758 HeapFree( GetProcessHeap(), 0, imgs );
759 imgs = NULL;
761 cleanup:
762 if (imgs)
764 /* Failed to produce a cursor, free previously allocated frames */
765 for (i=0; i<nFrames && imgs[i]; i++)
766 pXcursorImageDestroy( imgs[i] );
767 HeapFree( GetProcessHeap(), 0, imgs );
769 /* Cleanup all of the resources used to obtain the frame data */
770 if (hbmColor) DeleteObject( hbmColor );
771 if (hbmMask) DeleteObject( hbmMask );
772 HeapFree( GetProcessHeap(), 0, info );
773 return cursor;
777 struct system_cursors
779 WORD id;
780 const char *name;
783 static const struct system_cursors user32_cursors[] =
785 { OCR_NORMAL, "left_ptr" },
786 { OCR_IBEAM, "xterm" },
787 { OCR_WAIT, "watch" },
788 { OCR_CROSS, "cross" },
789 { OCR_UP, "center_ptr" },
790 { OCR_SIZE, "fleur" },
791 { OCR_SIZEALL, "fleur" },
792 { OCR_ICON, "icon" },
793 { OCR_SIZENWSE, "nwse-resize" },
794 { OCR_SIZENESW, "nesw-resize" },
795 { OCR_SIZEWE, "ew-resize" },
796 { OCR_SIZENS, "ns-resize" },
797 { OCR_NO, "not-allowed" },
798 { OCR_HAND, "hand2" },
799 { OCR_APPSTARTING, "left_ptr_watch" },
800 { OCR_HELP, "question_arrow" },
801 { 0 }
804 static const struct system_cursors comctl32_cursors[] =
806 { 102, "move" },
807 { 104, "copy" },
808 { 105, "left_ptr" },
809 { 106, "row-resize" },
810 { 107, "row-resize" },
811 { 108, "hand2" },
812 { 135, "col-resize" },
813 { 0 }
816 static const struct system_cursors ole32_cursors[] =
818 { 1, "no-drop" },
819 { 2, "move" },
820 { 3, "copy" },
821 { 4, "alias" },
822 { 0 }
825 static const struct system_cursors riched20_cursors[] =
827 { 105, "hand2" },
828 { 107, "right_ptr" },
829 { 109, "copy" },
830 { 110, "move" },
831 { 111, "no-drop" },
832 { 0 }
835 static const struct
837 const struct system_cursors *cursors;
838 WCHAR name[16];
839 } module_cursors[] =
841 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
842 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
843 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
844 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
847 /***********************************************************************
848 * create_xcursor_system_cursor
850 * Create an X cursor for a system cursor.
852 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
854 static const WCHAR idW[] = {'%','h','u',0};
855 const struct system_cursors *cursors;
856 unsigned int i;
857 Cursor cursor = 0;
858 HMODULE module;
859 HKEY key;
860 WCHAR *p, name[MAX_PATH * 2], valueW[64];
861 char valueA[64];
862 DWORD size, ret;
864 if (!pXcursorLibraryLoadCursor) return 0;
865 if (!info->szModName[0]) return 0;
867 p = strrchrW( info->szModName, '\\' );
868 strcpyW( name, p ? p + 1 : info->szModName );
869 p = name + strlenW( name );
870 *p++ = ',';
871 if (info->szResName[0]) strcpyW( p, info->szResName );
872 else sprintfW( p, idW, info->wResID );
873 valueA[0] = 0;
875 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
876 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver\\Cursors", &key ))
878 size = sizeof(valueW) / sizeof(WCHAR);
879 ret = RegQueryValueExW( key, name, NULL, NULL, (BYTE *)valueW, &size );
880 RegCloseKey( key );
881 if (!ret)
883 if (!valueW[0]) return 0; /* force standard cursor */
884 if (!WideCharToMultiByte( CP_UNIXCP, 0, valueW, -1, valueA, sizeof(valueA), NULL, NULL ))
885 valueA[0] = 0;
886 goto done;
890 if (info->szResName[0]) goto done; /* only integer resources are supported here */
891 if (!(module = GetModuleHandleW( info->szModName ))) goto done;
893 for (i = 0; i < sizeof(module_cursors)/sizeof(module_cursors[0]); i++)
894 if (GetModuleHandleW( module_cursors[i].name ) == module) break;
895 if (i == sizeof(module_cursors)/sizeof(module_cursors[0])) goto done;
897 cursors = module_cursors[i].cursors;
898 for (i = 0; cursors[i].id; i++)
899 if (cursors[i].id == info->wResID)
901 strcpy( valueA, cursors[i].name );
902 break;
905 done:
906 if (valueA[0])
908 cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
909 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
910 debugstr_w(name), debugstr_a(valueA) );
912 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
913 return cursor;
916 #endif /* SONAME_LIBXCURSOR */
919 /***********************************************************************
920 * create_xlib_monochrome_cursor
922 * Create a monochrome X cursor from a Windows one.
924 static Cursor create_xlib_monochrome_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
926 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
927 BITMAPINFO *info = (BITMAPINFO *)buffer;
928 const int and_y = 0;
929 const int xor_y = height;
930 unsigned int width_bytes = (width + 31) / 32 * 4;
931 unsigned char *mask_bits = NULL;
932 GC gc;
933 XColor fg, bg;
934 XVisualInfo vis;
935 Pixmap src_pixmap, bits_pixmap, mask_pixmap;
936 struct gdi_image_bits bits;
937 Cursor cursor = 0;
939 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
940 info->bmiHeader.biWidth = width;
941 info->bmiHeader.biHeight = -height * 2;
942 info->bmiHeader.biPlanes = 1;
943 info->bmiHeader.biBitCount = 1;
944 info->bmiHeader.biCompression = BI_RGB;
945 info->bmiHeader.biSizeImage = width_bytes * height * 2;
946 info->bmiHeader.biXPelsPerMeter = 0;
947 info->bmiHeader.biYPelsPerMeter = 0;
948 info->bmiHeader.biClrUsed = 0;
949 info->bmiHeader.biClrImportant = 0;
951 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
952 if (!GetDIBits( hdc, icon->hbmMask, 0, height * 2, mask_bits, info, DIB_RGB_COLORS )) goto done;
954 vis.depth = 1;
955 bits.ptr = mask_bits;
956 bits.free = NULL;
957 bits.is_copy = TRUE;
958 if (!(src_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
960 bits_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
961 mask_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
962 gc = XCreateGC( gdi_display, src_pixmap, 0, NULL );
963 XSetGraphicsExposures( gdi_display, gc, False );
965 /* We have to do some magic here, as cursors are not fully
966 * compatible between Windows and X11. Under X11, there are
967 * only 3 possible color cursor: black, white and masked. So
968 * we map the 4th Windows color (invert the bits on the screen)
969 * to black and an additional white bit on another place
970 * (+1,+1). This require some boolean arithmetic:
972 * Windows | X11
973 * And Xor Result | Bits Mask Result
974 * 0 0 black | 0 1 background
975 * 0 1 white | 1 1 foreground
976 * 1 0 no change | X 0 no change
977 * 1 1 inverted | 0 1 background
979 * which gives:
980 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
981 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
983 XSetFunction( gdi_display, gc, GXcopy );
984 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 0, 0 );
985 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 0, 0 );
986 XSetFunction( gdi_display, gc, GXandReverse );
987 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, xor_y, width, height, 0, 0 );
988 XSetFunction( gdi_display, gc, GXorReverse );
989 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, xor_y, width, height, 0, 0 );
990 /* additional white */
991 XSetFunction( gdi_display, gc, GXand );
992 XCopyArea( gdi_display, src_pixmap, src_pixmap, gc, 0, xor_y, width, height, 0, and_y );
993 XSetFunction( gdi_display, gc, GXor );
994 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 1, 1 );
995 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 1, 1 );
996 XFreeGC( gdi_display, gc );
998 fg.red = fg.green = fg.blue = 0xffff;
999 bg.red = bg.green = bg.blue = 0;
1000 cursor = XCreatePixmapCursor( gdi_display, bits_pixmap, mask_pixmap,
1001 &fg, &bg, icon->xHotspot, icon->yHotspot );
1002 XFreePixmap( gdi_display, src_pixmap );
1003 XFreePixmap( gdi_display, bits_pixmap );
1004 XFreePixmap( gdi_display, mask_pixmap );
1006 done:
1007 HeapFree( GetProcessHeap(), 0, mask_bits );
1008 return cursor;
1011 /***********************************************************************
1012 * create_xlib_color_cursor
1014 * Create a color X cursor from a Windows one.
1016 static Cursor create_xlib_color_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1018 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1019 BITMAPINFO *info = (BITMAPINFO *)buffer;
1020 XColor fg, bg;
1021 Cursor cursor = None;
1022 XVisualInfo vis;
1023 Pixmap xor_pixmap, mask_pixmap;
1024 struct gdi_image_bits bits;
1025 unsigned int *color_bits = NULL, *ptr;
1026 unsigned char *mask_bits = NULL, *xor_bits = NULL;
1027 int i, x, y, has_alpha = 0;
1028 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
1029 unsigned int width_bytes = (width + 31) / 32 * 4;
1031 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1032 info->bmiHeader.biWidth = width;
1033 info->bmiHeader.biHeight = -height;
1034 info->bmiHeader.biPlanes = 1;
1035 info->bmiHeader.biBitCount = 1;
1036 info->bmiHeader.biCompression = BI_RGB;
1037 info->bmiHeader.biSizeImage = width_bytes * height;
1038 info->bmiHeader.biXPelsPerMeter = 0;
1039 info->bmiHeader.biYPelsPerMeter = 0;
1040 info->bmiHeader.biClrUsed = 0;
1041 info->bmiHeader.biClrImportant = 0;
1043 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1044 if (!GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS )) goto done;
1046 info->bmiHeader.biBitCount = 32;
1047 info->bmiHeader.biSizeImage = width * height * 4;
1048 if (!(color_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1049 if (!(xor_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, width_bytes * height ))) goto done;
1050 GetDIBits( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS );
1052 /* compute fg/bg color and xor bitmap based on average of the color values */
1054 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
1055 for (y = 0, ptr = color_bits; y < height; y++)
1057 for (x = 0; x < width; x++, ptr++)
1059 int red = (*ptr >> 16) & 0xff;
1060 int green = (*ptr >> 8) & 0xff;
1061 int blue = (*ptr >> 0) & 0xff;
1062 if (red + green + blue > 0x40)
1064 rfg += red;
1065 gfg += green;
1066 bfg += blue;
1067 fgBits++;
1068 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1070 else
1072 rbg += red;
1073 gbg += green;
1074 bbg += blue;
1078 if (fgBits)
1080 fg.red = rfg * 257 / fgBits;
1081 fg.green = gfg * 257 / fgBits;
1082 fg.blue = bfg * 257 / fgBits;
1084 else fg.red = fg.green = fg.blue = 0;
1085 bgBits = width * height - fgBits;
1086 if (bgBits)
1088 bg.red = rbg * 257 / bgBits;
1089 bg.green = gbg * 257 / bgBits;
1090 bg.blue = bbg * 257 / bgBits;
1092 else bg.red = bg.green = bg.blue = 0;
1094 info->bmiHeader.biBitCount = 1;
1095 info->bmiHeader.biClrUsed = 0;
1096 info->bmiHeader.biSizeImage = width_bytes * height;
1098 /* generate mask from the alpha channel if we have one */
1100 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
1101 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
1103 if (has_alpha)
1105 memset( mask_bits, 0, width_bytes * height );
1106 for (y = 0, ptr = color_bits; y < height; y++)
1107 for (x = 0; x < width; x++, ptr++)
1108 if ((*ptr >> 24) > 25) /* more than 10% alpha */
1109 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1111 else /* invert the mask */
1113 ptr = (unsigned int *)mask_bits;
1114 for (i = 0; i < info->bmiHeader.biSizeImage / sizeof(*ptr); i++, ptr++) *ptr ^= ~0u;
1117 vis.depth = 1;
1118 bits.ptr = xor_bits;
1119 bits.free = NULL;
1120 bits.is_copy = TRUE;
1121 if (!(xor_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1123 bits.ptr = mask_bits;
1124 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
1126 if (mask_pixmap)
1128 cursor = XCreatePixmapCursor( gdi_display, xor_pixmap, mask_pixmap,
1129 &fg, &bg, icon->xHotspot, icon->yHotspot );
1130 XFreePixmap( gdi_display, mask_pixmap );
1132 XFreePixmap( gdi_display, xor_pixmap );
1134 done:
1135 HeapFree( GetProcessHeap(), 0, color_bits );
1136 HeapFree( GetProcessHeap(), 0, xor_bits );
1137 HeapFree( GetProcessHeap(), 0, mask_bits );
1138 return cursor;
1141 /***********************************************************************
1142 * create_cursor
1144 * Create an X cursor from a Windows one.
1146 static Cursor create_cursor( HANDLE handle )
1148 Cursor cursor = 0;
1149 ICONINFOEXW info;
1150 BITMAP bm;
1151 HDC hdc;
1153 if (!handle) return get_empty_cursor();
1155 info.cbSize = sizeof(info);
1156 if (!GetIconInfoExW( handle, &info )) return 0;
1158 #ifdef SONAME_LIBXCURSOR
1159 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
1161 DeleteObject( info.hbmColor );
1162 DeleteObject( info.hbmMask );
1163 return cursor;
1165 #endif
1167 GetObjectW( info.hbmMask, sizeof(bm), &bm );
1168 if (!info.hbmColor) bm.bmHeight = max( 1, bm.bmHeight / 2 );
1170 /* make sure hotspot is valid */
1171 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1173 info.xHotspot = bm.bmWidth / 2;
1174 info.yHotspot = bm.bmHeight / 2;
1177 hdc = CreateCompatibleDC( 0 );
1179 if (info.hbmColor)
1181 #ifdef SONAME_LIBXCURSOR
1182 if (pXcursorImagesLoadCursor)
1183 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
1184 #endif
1185 if (!cursor) cursor = create_xlib_color_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1186 DeleteObject( info.hbmColor );
1188 else
1190 cursor = create_xlib_monochrome_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1193 DeleteObject( info.hbmMask );
1194 DeleteDC( hdc );
1195 return cursor;
1198 /***********************************************************************
1199 * DestroyCursorIcon (X11DRV.@)
1201 void CDECL X11DRV_DestroyCursorIcon( HCURSOR handle )
1203 Cursor cursor;
1205 wine_tsx11_lock();
1206 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1208 TRACE( "%p xid %lx\n", handle, cursor );
1209 XFreeCursor( gdi_display, cursor );
1210 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1212 wine_tsx11_unlock();
1215 /***********************************************************************
1216 * SetCursor (X11DRV.@)
1218 void CDECL X11DRV_SetCursor( HCURSOR handle )
1220 if (InterlockedExchangePointer( (void **)&last_cursor, handle ) != handle ||
1221 GetTickCount() - last_cursor_change > 100)
1223 last_cursor_change = GetTickCount();
1224 if (cursor_window) SendNotifyMessageW( cursor_window, WM_X11DRV_SET_CURSOR, 0, (LPARAM)handle );
1228 /***********************************************************************
1229 * SetCursorPos (X11DRV.@)
1231 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
1233 struct x11drv_thread_data *data = x11drv_init_thread_data();
1235 XWarpPointer( data->display, root_window, root_window, 0, 0, 0, 0,
1236 x - virtual_screen_rect.left, y - virtual_screen_rect.top );
1237 data->warp_serial = NextRequest( data->display );
1238 XNoOp( data->display );
1239 XFlush( data->display ); /* avoids bad mouse lag in games that do their own mouse warping */
1240 TRACE( "warped to %d,%d serial %lu\n", x, y, data->warp_serial );
1241 return TRUE;
1244 /***********************************************************************
1245 * GetCursorPos (X11DRV.@)
1247 BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
1249 Display *display = thread_init_display();
1250 Window root, child;
1251 int rootX, rootY, winX, winY;
1252 unsigned int xstate;
1253 BOOL ret;
1255 wine_tsx11_lock();
1256 ret = XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &winX, &winY, &xstate );
1257 if (ret)
1259 POINT old = *pos;
1260 pos->x = winX + virtual_screen_rect.left;
1261 pos->y = winY + virtual_screen_rect.top;
1262 TRACE( "pointer at (%d,%d) server pos %d,%d\n", pos->x, pos->y, old.x, old.y );
1264 wine_tsx11_unlock();
1265 return ret;
1268 /***********************************************************************
1269 * ClipCursor (X11DRV.@)
1271 BOOL CDECL X11DRV_ClipCursor( LPCRECT clip )
1273 if (!clip)
1275 ungrab_clipping_window();
1276 return TRUE;
1279 if (GetWindowThreadProcessId( GetDesktopWindow(), NULL ) == GetCurrentThreadId())
1280 return TRUE; /* don't clip in the desktop process */
1282 if (grab_pointer)
1284 HWND foreground = GetForegroundWindow();
1286 /* we are clipping if the clip rectangle is smaller than the screen */
1287 if (clip->left > virtual_screen_rect.left || clip->right < virtual_screen_rect.right ||
1288 clip->top > virtual_screen_rect.top || clip->bottom < virtual_screen_rect.bottom)
1290 DWORD tid, pid;
1292 /* forward request to the foreground window if it's in a different thread */
1293 tid = GetWindowThreadProcessId( foreground, &pid );
1294 if (tid && tid != GetCurrentThreadId() && pid == GetCurrentProcessId())
1296 TRACE( "forwarding clip request to %p\n", foreground );
1297 SendNotifyMessageW( foreground, WM_X11DRV_CLIP_CURSOR, 0, 0 );
1298 return TRUE;
1300 else if (grab_clipping_window( clip )) return TRUE;
1302 else /* if currently clipping, check if we should switch to fullscreen clipping */
1304 struct x11drv_thread_data *data = x11drv_thread_data();
1305 if (data && data->clip_hwnd)
1307 if (EqualRect( clip, &clip_rect ) || clip_fullscreen_window( foreground, TRUE ))
1308 return TRUE;
1312 ungrab_clipping_window();
1313 return TRUE;
1316 /***********************************************************************
1317 * move_resize_window
1319 void move_resize_window( Display *display, struct x11drv_win_data *data, int dir )
1321 DWORD pt;
1322 int x, y, rootX, rootY, button = 0;
1323 XEvent xev;
1324 Window root, child;
1325 unsigned int xstate;
1327 pt = GetMessagePos();
1328 x = (short)LOWORD( pt );
1329 y = (short)HIWORD( pt );
1331 if (GetKeyState( VK_LBUTTON ) & 0x8000) button = 1;
1332 else if (GetKeyState( VK_MBUTTON ) & 0x8000) button = 2;
1333 else if (GetKeyState( VK_RBUTTON ) & 0x8000) button = 3;
1335 TRACE( "hwnd %p/%lx, x %d, y %d, dir %d, button %d\n",
1336 data->hwnd, data->whole_window, x, y, dir, button );
1338 xev.xclient.type = ClientMessage;
1339 xev.xclient.window = data->whole_window;
1340 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1341 xev.xclient.serial = 0;
1342 xev.xclient.display = display;
1343 xev.xclient.send_event = True;
1344 xev.xclient.format = 32;
1345 xev.xclient.data.l[0] = x - virtual_screen_rect.left; /* x coord */
1346 xev.xclient.data.l[1] = y - virtual_screen_rect.top; /* y coord */
1347 xev.xclient.data.l[2] = dir; /* direction */
1348 xev.xclient.data.l[3] = button; /* button */
1349 xev.xclient.data.l[4] = 0; /* unused */
1351 /* need to ungrab the pointer that may have been automatically grabbed
1352 * with a ButtonPress event */
1353 XUngrabPointer( display, CurrentTime );
1354 XSendEvent(display, root_window, False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
1356 /* try to detect the end of the size/move by polling for the mouse button to be released */
1357 /* (some apps don't like it if we return before the size/move is done) */
1359 if (!button) return;
1360 for (;;)
1362 MSG msg;
1363 INPUT input;
1365 if (!XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &x, &y, &xstate )) break;
1367 if (!(xstate & (Button1Mask << (button - 1))))
1369 /* fake a button release event */
1370 input.type = INPUT_MOUSE;
1371 input.u.mi.dx = x + virtual_screen_rect.left;
1372 input.u.mi.dy = y + virtual_screen_rect.top;
1373 input.u.mi.mouseData = button_up_data[button - 1];
1374 input.u.mi.dwFlags = button_up_flags[button - 1] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1375 input.u.mi.time = GetTickCount();
1376 input.u.mi.dwExtraInfo = 0;
1377 __wine_send_input( data->hwnd, &input );
1380 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
1382 if (!CallMsgFilterW( &msg, MSGF_SIZE ))
1384 TranslateMessage( &msg );
1385 DispatchMessageW( &msg );
1389 if (!(xstate & (Button1Mask << (button - 1)))) break;
1390 MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT );
1393 TRACE( "hwnd %p/%lx done\n", data->hwnd, data->whole_window );
1397 /***********************************************************************
1398 * X11DRV_ButtonPress
1400 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1402 XButtonEvent *event = &xev->xbutton;
1403 int buttonNum = event->button - 1;
1404 INPUT input;
1406 if (buttonNum >= NB_BUTTONS) return;
1408 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1410 input.u.mi.dx = event->x;
1411 input.u.mi.dy = event->y;
1412 input.u.mi.mouseData = button_down_data[buttonNum];
1413 input.u.mi.dwFlags = button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1414 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1415 input.u.mi.dwExtraInfo = 0;
1417 update_user_time( event->time );
1418 send_mouse_input( hwnd, event->window, event->state, &input );
1422 /***********************************************************************
1423 * X11DRV_ButtonRelease
1425 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1427 XButtonEvent *event = &xev->xbutton;
1428 int buttonNum = event->button - 1;
1429 INPUT input;
1431 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1433 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1435 input.u.mi.dx = event->x;
1436 input.u.mi.dy = event->y;
1437 input.u.mi.mouseData = button_up_data[buttonNum];
1438 input.u.mi.dwFlags = button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1439 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1440 input.u.mi.dwExtraInfo = 0;
1442 send_mouse_input( hwnd, event->window, event->state, &input );
1446 /***********************************************************************
1447 * X11DRV_MotionNotify
1449 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1451 XMotionEvent *event = &xev->xmotion;
1452 INPUT input;
1454 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d serial %lu\n",
1455 hwnd, event->window, event->x, event->y, event->is_hint, event->serial );
1457 input.u.mi.dx = event->x;
1458 input.u.mi.dy = event->y;
1459 input.u.mi.mouseData = 0;
1460 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1461 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1462 input.u.mi.dwExtraInfo = 0;
1464 if (!hwnd)
1466 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1467 if (thread_data->warp_serial && (long)(event->serial - thread_data->warp_serial) < 0) return;
1470 send_mouse_input( hwnd, event->window, event->state, &input );
1474 /***********************************************************************
1475 * X11DRV_EnterNotify
1477 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1479 XCrossingEvent *event = &xev->xcrossing;
1480 INPUT input;
1482 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd, event->window, event->x, event->y, event->detail );
1484 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1485 if (event->window == x11drv_thread_data()->grab_window) return;
1487 /* simulate a mouse motion event */
1488 input.u.mi.dx = event->x;
1489 input.u.mi.dy = event->y;
1490 input.u.mi.mouseData = 0;
1491 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1492 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1493 input.u.mi.dwExtraInfo = 0;
1495 send_mouse_input( hwnd, event->window, event->state, &input );
1498 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1500 /***********************************************************************
1501 * X11DRV_RawMotion
1503 static void X11DRV_RawMotion( XGenericEventCookie *xev )
1505 XIRawEvent *event = xev->data;
1506 const double *values = event->valuators.values;
1507 INPUT input;
1508 int i, j;
1509 double dx = 0, dy = 0;
1510 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1511 XIDeviceInfo *devices = thread_data->xi2_devices;
1513 if (!event->valuators.mask_len) return;
1514 if (thread_data->xi2_state != xi_enabled) return;
1516 input.u.mi.mouseData = 0;
1517 input.u.mi.dwFlags = MOUSEEVENTF_MOVE;
1518 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1519 input.u.mi.dwExtraInfo = 0;
1520 input.u.mi.dx = 0;
1521 input.u.mi.dy = 0;
1523 wine_tsx11_lock();
1524 for (i = 0; i < thread_data->xi2_device_count; ++i)
1526 if (devices[i].deviceid != event->deviceid) continue;
1527 for (j = 0; j < devices[i].num_classes; j++)
1529 XIValuatorClassInfo *class = (XIValuatorClassInfo *)devices[i].classes[j];
1531 if (devices[i].classes[j]->type != XIValuatorClass) continue;
1532 if (XIMaskIsSet( event->valuators.mask, class->number ))
1534 double val = *values++;
1535 if (class->label == x11drv_atom( Rel_X ))
1537 input.u.mi.dx = dx = val;
1538 if (class->min < class->max)
1539 input.u.mi.dx = val * (virtual_screen_rect.right - virtual_screen_rect.left)
1540 / (class->max - class->min);
1542 else if (class->label == x11drv_atom( Rel_Y ))
1544 input.u.mi.dy = dy = val;
1545 if (class->min < class->max)
1546 input.u.mi.dy = val * (virtual_screen_rect.bottom - virtual_screen_rect.top)
1547 / (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