xolehlp: Fix calling convention.
[wine.git] / dlls / winex11.drv / mouse.c
blob168914fafde7be833ef38d798df1a8d811af2334
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 if (!cursor)
181 XColor bg;
182 Pixmap pixmap;
184 bg.red = bg.green = bg.blue = 0x0000;
185 pixmap = XCreateBitmapFromData( gdi_display, root_window, data, 1, 1 );
186 if (pixmap)
188 Cursor new = XCreatePixmapCursor( gdi_display, pixmap, pixmap, &bg, &bg, 0, 0 );
189 if (InterlockedCompareExchangePointer( (void **)&cursor, (void *)new, 0 ))
190 XFreeCursor( gdi_display, new );
191 XFreePixmap( gdi_display, pixmap );
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 XLockDisplay( gdi_display );
211 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&prev ))
213 /* someone else was here first */
214 XFreeCursor( gdi_display, cursor );
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 XUnlockDisplay( gdi_display );
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 if (data->xi2_devices) pXIFreeDeviceInfo( data->xi2_devices );
275 data->xi2_devices = devices = pXIQueryDevice( data->display, XIAllDevices, &data->xi2_device_count );
276 for (i = 0; i < data->xi2_device_count; ++i)
278 if (devices[i].use != XIMasterPointer) continue;
279 for (j = count = 0; j < devices[i].num_classes; j++)
281 XIValuatorClassInfo *class = (XIValuatorClassInfo *)devices[i].classes[j];
283 if (devices[i].classes[j]->type != XIValuatorClass) continue;
284 TRACE( "Device %u (%s) num %u %f,%f res %u mode %u label %s\n",
285 devices[i].deviceid, debugstr_a(devices[i].name),
286 class->number, class->min, class->max, class->resolution, class->mode,
287 XGetAtomName( data->display, class->label ));
288 if (class->label == x11drv_atom( Rel_X ) || class->label == x11drv_atom( Rel_Y )) count++;
289 /* workaround for drivers that don't provide labels */
290 if (!class->label && class->number <= 1 && class->mode == XIModeRelative) count++;
292 if (count < 2) continue;
293 TRACE( "Using %u (%s) as core pointer\n",
294 devices[i].deviceid, debugstr_a(devices[i].name) );
295 data->xi2_core_pointer = devices[i].deviceid;
296 break;
299 mask.mask = mask_bits;
300 mask.mask_len = sizeof(mask_bits);
301 memset( mask_bits, 0, sizeof(mask_bits) );
302 XISetMask( mask_bits, XI_RawMotion );
303 XISetMask( mask_bits, XI_ButtonPress );
305 for (i = 0; i < data->xi2_device_count; ++i)
307 if (devices[i].use == XISlavePointer && devices[i].attachment == data->xi2_core_pointer)
309 TRACE( "Device %u (%s) is attached to the core pointer\n",
310 devices[i].deviceid, debugstr_a(devices[i].name) );
311 mask.deviceid = devices[i].deviceid;
312 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
313 data->xi2_state = xi_enabled;
316 #endif
319 /***********************************************************************
320 * disable_xinput2
322 static void disable_xinput2(void)
324 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
325 struct x11drv_thread_data *data = x11drv_thread_data();
326 XIDeviceInfo *devices = data->xi2_devices;
327 XIEventMask mask;
328 int i;
330 if (data->xi2_state != xi_enabled) return;
332 TRACE( "disabling\n" );
333 data->xi2_state = xi_disabled;
335 mask.mask = NULL;
336 mask.mask_len = 0;
338 for (i = 0; i < data->xi2_device_count; ++i)
340 if (devices[i].use == XISlavePointer && devices[i].attachment == data->xi2_core_pointer)
342 mask.deviceid = devices[i].deviceid;
343 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
346 pXIFreeDeviceInfo( devices );
347 data->xi2_devices = NULL;
348 data->xi2_device_count = 0;
349 #endif
353 /***********************************************************************
354 * grab_clipping_window
356 * Start a pointer grab on the clip window.
358 static BOOL grab_clipping_window( const RECT *clip )
360 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
361 struct x11drv_thread_data *data = x11drv_thread_data();
362 Window clip_window;
363 HWND msg_hwnd = 0;
365 if (GetWindowThreadProcessId( GetDesktopWindow(), NULL ) == GetCurrentThreadId())
366 return TRUE; /* don't clip in the desktop process */
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 if (!data->clip_hwnd) XUnmapWindow( data->display, clip_window );
389 XMoveResizeWindow( data->display, clip_window,
390 clip->left - virtual_screen_rect.left, clip->top - virtual_screen_rect.top,
391 max( 1, clip->right - clip->left ), max( 1, clip->bottom - clip->top ) );
392 XMapWindow( data->display, clip_window );
394 /* if the rectangle is shrinking we may get a pointer warp */
395 if (!data->clip_hwnd || clip->left > clip_rect.left || clip->top > clip_rect.top ||
396 clip->right < clip_rect.right || clip->bottom < clip_rect.bottom)
397 data->warp_serial = NextRequest( data->display );
399 if (!XGrabPointer( data->display, clip_window, False,
400 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
401 GrabModeAsync, GrabModeAsync, clip_window, None, CurrentTime ))
402 clipping_cursor = 1;
404 if (!clipping_cursor)
406 disable_xinput2();
407 DestroyWindow( msg_hwnd );
408 return FALSE;
410 clip_rect = *clip;
411 if (!data->clip_hwnd) sync_window_cursor( clip_window );
412 InterlockedExchangePointer( (void **)&cursor_window, msg_hwnd );
413 data->clip_hwnd = msg_hwnd;
414 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, (LPARAM)msg_hwnd );
415 return TRUE;
418 /***********************************************************************
419 * ungrab_clipping_window
421 * Release the pointer grab on the clip window.
423 void ungrab_clipping_window(void)
425 Display *display = thread_init_display();
426 Window clip_window = init_clip_window();
428 if (!clip_window) return;
430 TRACE( "no longer clipping\n" );
431 XUnmapWindow( display, clip_window );
432 clipping_cursor = 0;
433 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, 0 );
436 /***********************************************************************
437 * reset_clipping_window
439 * Forcibly reset the window clipping on external events.
441 void reset_clipping_window(void)
443 ungrab_clipping_window();
444 ClipCursor( NULL ); /* make sure the clip rectangle is reset too */
447 /***********************************************************************
448 * clip_cursor_notify
450 * Notification function called upon receiving a WM_X11DRV_CLIP_CURSOR.
452 LRESULT clip_cursor_notify( HWND hwnd, HWND new_clip_hwnd )
454 struct x11drv_thread_data *data = x11drv_thread_data();
456 if (hwnd == GetDesktopWindow()) /* change the clip window stored in the desktop process */
458 static HWND clip_hwnd;
460 HWND prev = clip_hwnd;
461 clip_hwnd = new_clip_hwnd;
462 if (prev || new_clip_hwnd) TRACE( "clip hwnd changed from %p to %p\n", prev, new_clip_hwnd );
463 if (prev) SendNotifyMessageW( prev, WM_X11DRV_CLIP_CURSOR, 0, 0 );
465 else if (hwnd == data->clip_hwnd) /* this is a notification that clipping has been reset */
467 TRACE( "clip hwnd reset from %p\n", hwnd );
468 data->clip_hwnd = 0;
469 data->clip_reset = GetTickCount();
470 disable_xinput2();
471 DestroyWindow( hwnd );
473 else if (hwnd == GetForegroundWindow()) /* request to clip */
475 RECT clip;
477 GetClipCursor( &clip );
478 if (clip.left > virtual_screen_rect.left || clip.right < virtual_screen_rect.right ||
479 clip.top > virtual_screen_rect.top || clip.bottom < virtual_screen_rect.bottom)
480 return grab_clipping_window( &clip );
482 return 0;
485 /***********************************************************************
486 * clip_fullscreen_window
488 * Turn on clipping if the active window is fullscreen.
490 BOOL clip_fullscreen_window( HWND hwnd, BOOL reset )
492 struct x11drv_win_data *data;
493 struct x11drv_thread_data *thread_data;
494 RECT rect;
495 DWORD style;
496 BOOL fullscreen;
498 if (hwnd == GetDesktopWindow()) return FALSE;
499 style = GetWindowLongW( hwnd, GWL_STYLE );
500 if (!(style & WS_VISIBLE)) return FALSE;
501 if ((style & (WS_POPUP | WS_CHILD)) == WS_CHILD) return FALSE;
502 /* maximized windows don't count as full screen */
503 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION) return FALSE;
504 if (!(data = get_win_data( hwnd ))) return FALSE;
505 fullscreen = is_window_rect_fullscreen( &data->whole_rect );
506 release_win_data( data );
507 if (!fullscreen) return FALSE;
508 if (!(thread_data = x11drv_thread_data())) return FALSE;
509 if (GetTickCount() - thread_data->clip_reset < 1000) return FALSE;
510 if (!reset && clipping_cursor && thread_data->clip_hwnd) return FALSE; /* already clipping */
511 SetRect( &rect, 0, 0, screen_width, screen_height );
512 if (!grab_fullscreen)
514 if (!EqualRect( &rect, &virtual_screen_rect )) return FALSE;
515 if (root_window != DefaultRootWindow( gdi_display )) return FALSE;
517 TRACE( "win %p clipping fullscreen\n", hwnd );
518 return grab_clipping_window( &rect );
521 /***********************************************************************
522 * send_mouse_input
524 * Update the various window states on a mouse event.
526 static void send_mouse_input( HWND hwnd, Window window, unsigned int state, INPUT *input )
528 struct x11drv_win_data *data;
529 POINT pt;
531 input->type = INPUT_MOUSE;
533 if (!hwnd)
535 struct x11drv_thread_data *thread_data = x11drv_thread_data();
536 HWND clip_hwnd = thread_data->clip_hwnd;
538 if (!clip_hwnd) return;
539 if (thread_data->clip_window != window) return;
540 if (InterlockedExchangePointer( (void **)&cursor_window, clip_hwnd ) != clip_hwnd ||
541 input->u.mi.time - last_cursor_change > 100)
543 sync_window_cursor( window );
544 last_cursor_change = input->u.mi.time;
546 input->u.mi.dx += clip_rect.left;
547 input->u.mi.dy += clip_rect.top;
548 __wine_send_input( hwnd, input );
549 return;
552 if (!(data = get_win_data( hwnd ))) return;
554 if (window == data->whole_window)
556 input->u.mi.dx += data->whole_rect.left - data->client_rect.left;
557 input->u.mi.dy += data->whole_rect.top - data->client_rect.top;
559 if (window == root_window)
561 input->u.mi.dx += virtual_screen_rect.left;
562 input->u.mi.dy += virtual_screen_rect.top;
564 pt.x = input->u.mi.dx;
565 pt.y = input->u.mi.dy;
566 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
567 pt.x = data->client_rect.right - data->client_rect.left - 1 - pt.x;
568 MapWindowPoints( hwnd, 0, &pt, 1 );
570 if (InterlockedExchangePointer( (void **)&cursor_window, hwnd ) != hwnd ||
571 input->u.mi.time - last_cursor_change > 100)
573 sync_window_cursor( data->whole_window );
574 last_cursor_change = input->u.mi.time;
576 release_win_data( data );
578 if (hwnd != GetDesktopWindow())
580 hwnd = GetAncestor( hwnd, GA_ROOT );
581 if ((input->u.mi.dwFlags & (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_RIGHTDOWN)) && hwnd == GetForegroundWindow())
582 clip_fullscreen_window( hwnd, FALSE );
585 /* update the wine server Z-order */
587 if (window != x11drv_thread_data()->grab_window &&
588 /* ignore event if a button is pressed, since the mouse is then grabbed too */
589 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
591 RECT rect;
592 SetRect( &rect, pt.x, pt.y, pt.x + 1, pt.y + 1 );
593 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
595 SERVER_START_REQ( update_window_zorder )
597 req->window = wine_server_user_handle( hwnd );
598 req->rect.left = rect.left;
599 req->rect.top = rect.top;
600 req->rect.right = rect.right;
601 req->rect.bottom = rect.bottom;
602 wine_server_call( req );
604 SERVER_END_REQ;
607 input->u.mi.dx = pt.x;
608 input->u.mi.dy = pt.y;
609 __wine_send_input( hwnd, input );
612 #ifdef SONAME_LIBXCURSOR
614 /***********************************************************************
615 * create_xcursor_frame
617 * Use Xcursor to create a frame of an X cursor from a Windows one.
619 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
620 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
621 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
622 int width, int height, int istep )
624 XcursorImage *image, *ret = NULL;
625 DWORD delay_jiffies, num_steps;
626 int x, y, i, has_alpha = FALSE;
627 XcursorPixel *ptr;
629 image = pXcursorImageCreate( width, height );
630 if (!image)
632 ERR("X11 failed to produce a cursor frame!\n");
633 return NULL;
636 image->xhot = iinfo->xHotspot;
637 image->yhot = iinfo->yHotspot;
639 image->delay = 100; /* fallback delay, 100 ms */
640 if (GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, istep, &delay_jiffies, &num_steps) != 0)
641 image->delay = (100 * delay_jiffies) / 6; /* convert jiffies (1/60s) to milliseconds */
642 else
643 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep);
645 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
646 memset( color_bits, 0x00, color_size );
647 SelectObject( hdc, hbmColor );
648 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
650 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
651 goto cleanup;
653 memcpy( image->pixels, color_bits, color_size );
655 /* check if the cursor frame was drawn with an alpha channel */
656 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
657 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
659 /* if no alpha channel was drawn then generate it from the mask */
660 if (!has_alpha)
662 unsigned int width_bytes = (width + 31) / 32 * 4;
664 /* draw the cursor mask to a temporary buffer */
665 memset( mask_bits, 0xFF, mask_size );
666 SelectObject( hdc, hbmMask );
667 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
669 ERR("Failed to draw frame mask %d.\n", istep);
670 goto cleanup;
672 /* use the buffer to directly modify the XcursorImage alpha channel */
673 for (y = 0, ptr = image->pixels; y < height; y++)
674 for (x = 0; x < width; x++, ptr++)
675 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
676 *ptr |= 0xff000000;
678 ret = image;
680 cleanup:
681 if (ret == NULL) pXcursorImageDestroy( image );
682 return ret;
685 /***********************************************************************
686 * create_xcursor_cursor
688 * Use Xcursor to create an X cursor from a Windows one.
690 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
692 unsigned char *color_bits, *mask_bits;
693 HBITMAP hbmColor = 0, hbmMask = 0;
694 DWORD nFrames, delay_jiffies, i;
695 int color_size, mask_size;
696 BITMAPINFO *info = NULL;
697 XcursorImages *images;
698 XcursorImage **imgs;
699 Cursor cursor = 0;
701 /* Retrieve the number of frames to render */
702 if (!GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, 0, &delay_jiffies, &nFrames)) return 0;
703 if (!(imgs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XcursorImage*)*nFrames ))) return 0;
705 /* Allocate all of the resources necessary to obtain a cursor frame */
706 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
707 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
708 info->bmiHeader.biWidth = width;
709 info->bmiHeader.biHeight = -height;
710 info->bmiHeader.biPlanes = 1;
711 info->bmiHeader.biCompression = BI_RGB;
712 info->bmiHeader.biXPelsPerMeter = 0;
713 info->bmiHeader.biYPelsPerMeter = 0;
714 info->bmiHeader.biClrUsed = 0;
715 info->bmiHeader.biClrImportant = 0;
716 info->bmiHeader.biBitCount = 32;
717 color_size = width * height * 4;
718 info->bmiHeader.biSizeImage = color_size;
719 hbmColor = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &color_bits, NULL, 0);
720 if (!hbmColor)
722 ERR("Failed to create DIB section for cursor color data!\n");
723 goto cleanup;
725 info->bmiHeader.biBitCount = 1;
726 info->bmiColors[0].rgbRed = 0;
727 info->bmiColors[0].rgbGreen = 0;
728 info->bmiColors[0].rgbBlue = 0;
729 info->bmiColors[0].rgbReserved = 0;
730 info->bmiColors[1].rgbRed = 0xff;
731 info->bmiColors[1].rgbGreen = 0xff;
732 info->bmiColors[1].rgbBlue = 0xff;
733 info->bmiColors[1].rgbReserved = 0;
735 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
736 info->bmiHeader.biSizeImage = mask_size;
737 hbmMask = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &mask_bits, NULL, 0);
738 if (!hbmMask)
740 ERR("Failed to create DIB section for cursor mask data!\n");
741 goto cleanup;
744 /* Create an XcursorImage for each frame of the cursor */
745 for (i=0; i<nFrames; i++)
747 imgs[i] = create_xcursor_frame( hdc, iinfo, icon,
748 hbmColor, color_bits, color_size,
749 hbmMask, mask_bits, mask_size,
750 width, height, i );
751 if (!imgs[i]) goto cleanup;
754 /* Build an X cursor out of all of the frames */
755 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
756 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
757 images->images[images->nimage] = imgs[images->nimage];
758 cursor = pXcursorImagesLoadCursor( gdi_display, images );
759 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
760 HeapFree( GetProcessHeap(), 0, imgs );
761 imgs = NULL;
763 cleanup:
764 if (imgs)
766 /* Failed to produce a cursor, free previously allocated frames */
767 for (i=0; i<nFrames && imgs[i]; i++)
768 pXcursorImageDestroy( imgs[i] );
769 HeapFree( GetProcessHeap(), 0, imgs );
771 /* Cleanup all of the resources used to obtain the frame data */
772 if (hbmColor) DeleteObject( hbmColor );
773 if (hbmMask) DeleteObject( hbmMask );
774 HeapFree( GetProcessHeap(), 0, info );
775 return cursor;
778 #endif /* SONAME_LIBXCURSOR */
781 struct system_cursors
783 WORD id;
784 const char *name;
787 static const struct system_cursors user32_cursors[] =
789 { OCR_NORMAL, "left_ptr" },
790 { OCR_IBEAM, "xterm" },
791 { OCR_WAIT, "watch" },
792 { OCR_CROSS, "cross" },
793 { OCR_UP, "center_ptr" },
794 { OCR_SIZE, "fleur" },
795 { OCR_SIZEALL, "fleur" },
796 { OCR_ICON, "icon" },
797 { OCR_SIZENWSE, "nwse-resize" },
798 { OCR_SIZENESW, "nesw-resize" },
799 { OCR_SIZEWE, "ew-resize" },
800 { OCR_SIZENS, "ns-resize" },
801 { OCR_NO, "not-allowed" },
802 { OCR_HAND, "hand2" },
803 { OCR_APPSTARTING, "left_ptr_watch" },
804 { OCR_HELP, "question_arrow" },
805 { 0 }
808 static const struct system_cursors comctl32_cursors[] =
810 { 102, "move" },
811 { 104, "copy" },
812 { 105, "left_ptr" },
813 { 106, "row-resize" },
814 { 107, "row-resize" },
815 { 108, "hand2" },
816 { 135, "col-resize" },
817 { 0 }
820 static const struct system_cursors ole32_cursors[] =
822 { 1, "no-drop" },
823 { 2, "move" },
824 { 3, "copy" },
825 { 4, "alias" },
826 { 0 }
829 static const struct system_cursors riched20_cursors[] =
831 { 105, "hand2" },
832 { 107, "right_ptr" },
833 { 109, "copy" },
834 { 110, "move" },
835 { 111, "no-drop" },
836 { 0 }
839 static const struct
841 const struct system_cursors *cursors;
842 WCHAR name[16];
843 } module_cursors[] =
845 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
846 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
847 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
848 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
851 struct cursor_font_fallback
853 const char *name;
854 unsigned int shape;
857 static const struct cursor_font_fallback fallbacks[] =
859 { "X_cursor", XC_X_cursor },
860 { "arrow", XC_arrow },
861 { "based_arrow_down", XC_based_arrow_down },
862 { "based_arrow_up", XC_based_arrow_up },
863 { "boat", XC_boat },
864 { "bogosity", XC_bogosity },
865 { "bottom_left_corner", XC_bottom_left_corner },
866 { "bottom_right_corner", XC_bottom_right_corner },
867 { "bottom_side", XC_bottom_side },
868 { "bottom_tee", XC_bottom_tee },
869 { "box_spiral", XC_box_spiral },
870 { "center_ptr", XC_center_ptr },
871 { "circle", XC_circle },
872 { "clock", XC_clock },
873 { "coffee_mug", XC_coffee_mug },
874 { "col-resize", XC_sb_v_double_arrow },
875 { "cross", XC_cross },
876 { "cross_reverse", XC_cross_reverse },
877 { "crosshair", XC_crosshair },
878 { "diamond_cross", XC_diamond_cross },
879 { "dot", XC_dot },
880 { "dotbox", XC_dotbox },
881 { "double_arrow", XC_double_arrow },
882 { "draft_large", XC_draft_large },
883 { "draft_small", XC_draft_small },
884 { "draped_box", XC_draped_box },
885 { "exchange", XC_exchange },
886 { "fleur", XC_fleur },
887 { "gobbler", XC_gobbler },
888 { "gumby", XC_gumby },
889 { "hand1", XC_hand1 },
890 { "hand2", XC_hand2 },
891 { "heart", XC_heart },
892 { "icon", XC_icon },
893 { "iron_cross", XC_iron_cross },
894 { "left_ptr", XC_left_ptr },
895 { "left_side", XC_left_side },
896 { "left_tee", XC_left_tee },
897 { "leftbutton", XC_leftbutton },
898 { "ll_angle", XC_ll_angle },
899 { "lr_angle", XC_lr_angle },
900 { "man", XC_man },
901 { "middlebutton", XC_middlebutton },
902 { "mouse", XC_mouse },
903 { "pencil", XC_pencil },
904 { "pirate", XC_pirate },
905 { "plus", XC_plus },
906 { "question_arrow", XC_question_arrow },
907 { "right_ptr", XC_right_ptr },
908 { "right_side", XC_right_side },
909 { "right_tee", XC_right_tee },
910 { "rightbutton", XC_rightbutton },
911 { "row-resize", XC_sb_h_double_arrow },
912 { "rtl_logo", XC_rtl_logo },
913 { "sailboat", XC_sailboat },
914 { "sb_down_arrow", XC_sb_down_arrow },
915 { "sb_h_double_arrow", XC_sb_h_double_arrow },
916 { "sb_left_arrow", XC_sb_left_arrow },
917 { "sb_right_arrow", XC_sb_right_arrow },
918 { "sb_up_arrow", XC_sb_up_arrow },
919 { "sb_v_double_arrow", XC_sb_v_double_arrow },
920 { "shuttle", XC_shuttle },
921 { "sizing", XC_sizing },
922 { "spider", XC_spider },
923 { "spraycan", XC_spraycan },
924 { "star", XC_star },
925 { "target", XC_target },
926 { "tcross", XC_tcross },
927 { "top_left_arrow", XC_top_left_arrow },
928 { "top_left_corner", XC_top_left_corner },
929 { "top_right_corner", XC_top_right_corner },
930 { "top_side", XC_top_side },
931 { "top_tee", XC_top_tee },
932 { "trek", XC_trek },
933 { "ul_angle", XC_ul_angle },
934 { "umbrella", XC_umbrella },
935 { "ur_angle", XC_ur_angle },
936 { "watch", XC_watch },
937 { "xterm", XC_xterm }
940 static int fallback_cmp( const void *key, const void *member )
942 const struct cursor_font_fallback *fallback = member;
943 return strcmp( key, fallback->name );
946 static int find_fallback_shape( const char *name )
948 struct cursor_font_fallback *fallback;
950 if ((fallback = bsearch( name, fallbacks, sizeof(fallbacks) / sizeof(fallbacks[0]),
951 sizeof(*fallback), fallback_cmp )))
952 return fallback->shape;
953 return -1;
956 /***********************************************************************
957 * create_xcursor_system_cursor
959 * Create an X cursor for a system cursor.
961 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
963 static const WCHAR idW[] = {'%','h','u',0};
964 const struct system_cursors *cursors;
965 unsigned int i;
966 Cursor cursor = 0;
967 HMODULE module;
968 HKEY key;
969 WCHAR *p, name[MAX_PATH * 2], valueW[64];
970 char valueA[64];
971 DWORD size, ret;
973 if (!info->szModName[0]) return 0;
975 p = strrchrW( info->szModName, '\\' );
976 strcpyW( name, p ? p + 1 : info->szModName );
977 p = name + strlenW( name );
978 *p++ = ',';
979 if (info->szResName[0]) strcpyW( p, info->szResName );
980 else sprintfW( p, idW, info->wResID );
981 valueA[0] = 0;
983 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
984 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver\\Cursors", &key ))
986 size = sizeof(valueW) / sizeof(WCHAR);
987 ret = RegQueryValueExW( key, name, NULL, NULL, (BYTE *)valueW, &size );
988 RegCloseKey( key );
989 if (!ret)
991 if (!valueW[0]) return 0; /* force standard cursor */
992 if (!WideCharToMultiByte( CP_UNIXCP, 0, valueW, -1, valueA, sizeof(valueA), NULL, NULL ))
993 valueA[0] = 0;
994 goto done;
998 if (info->szResName[0]) goto done; /* only integer resources are supported here */
999 if (!(module = GetModuleHandleW( info->szModName ))) goto done;
1001 for (i = 0; i < sizeof(module_cursors)/sizeof(module_cursors[0]); i++)
1002 if (GetModuleHandleW( module_cursors[i].name ) == module) break;
1003 if (i == sizeof(module_cursors)/sizeof(module_cursors[0])) goto done;
1005 cursors = module_cursors[i].cursors;
1006 for (i = 0; cursors[i].id; i++)
1007 if (cursors[i].id == info->wResID)
1009 strcpy( valueA, cursors[i].name );
1010 break;
1013 done:
1014 if (valueA[0])
1016 #ifdef SONAME_LIBXCURSOR
1017 if (pXcursorLibraryLoadCursor) cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
1018 #endif
1019 if (!cursor)
1021 int shape = find_fallback_shape( valueA );
1022 if (shape != -1) cursor = XCreateFontCursor( gdi_display, shape );
1024 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
1025 debugstr_w(name), debugstr_a(valueA) );
1027 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
1028 return cursor;
1032 /***********************************************************************
1033 * create_xlib_monochrome_cursor
1035 * Create a monochrome X cursor from a Windows one.
1037 static Cursor create_xlib_monochrome_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1039 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1040 BITMAPINFO *info = (BITMAPINFO *)buffer;
1041 const int and_y = 0;
1042 const int xor_y = height;
1043 unsigned int width_bytes = (width + 31) / 32 * 4;
1044 unsigned char *mask_bits = NULL;
1045 GC gc;
1046 XColor fg, bg;
1047 XVisualInfo vis = default_visual;
1048 Pixmap src_pixmap, bits_pixmap, mask_pixmap;
1049 struct gdi_image_bits bits;
1050 Cursor cursor = 0;
1052 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1053 info->bmiHeader.biWidth = width;
1054 info->bmiHeader.biHeight = -height * 2;
1055 info->bmiHeader.biPlanes = 1;
1056 info->bmiHeader.biBitCount = 1;
1057 info->bmiHeader.biCompression = BI_RGB;
1058 info->bmiHeader.biSizeImage = width_bytes * height * 2;
1059 info->bmiHeader.biXPelsPerMeter = 0;
1060 info->bmiHeader.biYPelsPerMeter = 0;
1061 info->bmiHeader.biClrUsed = 0;
1062 info->bmiHeader.biClrImportant = 0;
1064 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1065 if (!GetDIBits( hdc, icon->hbmMask, 0, height * 2, mask_bits, info, DIB_RGB_COLORS )) goto done;
1067 vis.depth = 1;
1068 bits.ptr = mask_bits;
1069 bits.free = NULL;
1070 bits.is_copy = TRUE;
1071 if (!(src_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1073 bits_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
1074 mask_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
1075 gc = XCreateGC( gdi_display, src_pixmap, 0, NULL );
1076 XSetGraphicsExposures( gdi_display, gc, False );
1078 /* We have to do some magic here, as cursors are not fully
1079 * compatible between Windows and X11. Under X11, there are
1080 * only 3 possible color cursor: black, white and masked. So
1081 * we map the 4th Windows color (invert the bits on the screen)
1082 * to black and an additional white bit on another place
1083 * (+1,+1). This require some boolean arithmetic:
1085 * Windows | X11
1086 * And Xor Result | Bits Mask Result
1087 * 0 0 black | 0 1 background
1088 * 0 1 white | 1 1 foreground
1089 * 1 0 no change | X 0 no change
1090 * 1 1 inverted | 0 1 background
1092 * which gives:
1093 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
1094 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
1096 XSetFunction( gdi_display, gc, GXcopy );
1097 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 0, 0 );
1098 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 0, 0 );
1099 XSetFunction( gdi_display, gc, GXandReverse );
1100 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, xor_y, width, height, 0, 0 );
1101 XSetFunction( gdi_display, gc, GXorReverse );
1102 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, xor_y, width, height, 0, 0 );
1103 /* additional white */
1104 XSetFunction( gdi_display, gc, GXand );
1105 XCopyArea( gdi_display, src_pixmap, src_pixmap, gc, 0, xor_y, width, height, 0, and_y );
1106 XSetFunction( gdi_display, gc, GXor );
1107 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 1, 1 );
1108 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 1, 1 );
1109 XFreeGC( gdi_display, gc );
1111 fg.red = fg.green = fg.blue = 0xffff;
1112 bg.red = bg.green = bg.blue = 0;
1113 cursor = XCreatePixmapCursor( gdi_display, bits_pixmap, mask_pixmap,
1114 &fg, &bg, icon->xHotspot, icon->yHotspot );
1115 XFreePixmap( gdi_display, src_pixmap );
1116 XFreePixmap( gdi_display, bits_pixmap );
1117 XFreePixmap( gdi_display, mask_pixmap );
1119 done:
1120 HeapFree( GetProcessHeap(), 0, mask_bits );
1121 return cursor;
1124 /***********************************************************************
1125 * create_xlib_color_cursor
1127 * Create a color X cursor from a Windows one.
1129 static Cursor create_xlib_color_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1131 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1132 BITMAPINFO *info = (BITMAPINFO *)buffer;
1133 XColor fg, bg;
1134 Cursor cursor = None;
1135 XVisualInfo vis = default_visual;
1136 Pixmap xor_pixmap, mask_pixmap;
1137 struct gdi_image_bits bits;
1138 unsigned int *color_bits = NULL, *ptr;
1139 unsigned char *mask_bits = NULL, *xor_bits = NULL;
1140 int i, x, y, has_alpha = 0;
1141 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
1142 unsigned int width_bytes = (width + 31) / 32 * 4;
1144 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1145 info->bmiHeader.biWidth = width;
1146 info->bmiHeader.biHeight = -height;
1147 info->bmiHeader.biPlanes = 1;
1148 info->bmiHeader.biBitCount = 1;
1149 info->bmiHeader.biCompression = BI_RGB;
1150 info->bmiHeader.biSizeImage = width_bytes * height;
1151 info->bmiHeader.biXPelsPerMeter = 0;
1152 info->bmiHeader.biYPelsPerMeter = 0;
1153 info->bmiHeader.biClrUsed = 0;
1154 info->bmiHeader.biClrImportant = 0;
1156 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1157 if (!GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS )) goto done;
1159 info->bmiHeader.biBitCount = 32;
1160 info->bmiHeader.biSizeImage = width * height * 4;
1161 if (!(color_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1162 if (!(xor_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, width_bytes * height ))) goto done;
1163 GetDIBits( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS );
1165 /* compute fg/bg color and xor bitmap based on average of the color values */
1167 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
1168 for (y = 0, ptr = color_bits; y < height; y++)
1170 for (x = 0; x < width; x++, ptr++)
1172 int red = (*ptr >> 16) & 0xff;
1173 int green = (*ptr >> 8) & 0xff;
1174 int blue = (*ptr >> 0) & 0xff;
1175 if (red + green + blue > 0x40)
1177 rfg += red;
1178 gfg += green;
1179 bfg += blue;
1180 fgBits++;
1181 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1183 else
1185 rbg += red;
1186 gbg += green;
1187 bbg += blue;
1191 if (fgBits)
1193 fg.red = rfg * 257 / fgBits;
1194 fg.green = gfg * 257 / fgBits;
1195 fg.blue = bfg * 257 / fgBits;
1197 else fg.red = fg.green = fg.blue = 0;
1198 bgBits = width * height - fgBits;
1199 if (bgBits)
1201 bg.red = rbg * 257 / bgBits;
1202 bg.green = gbg * 257 / bgBits;
1203 bg.blue = bbg * 257 / bgBits;
1205 else bg.red = bg.green = bg.blue = 0;
1207 info->bmiHeader.biBitCount = 1;
1208 info->bmiHeader.biClrUsed = 0;
1209 info->bmiHeader.biSizeImage = width_bytes * height;
1211 /* generate mask from the alpha channel if we have one */
1213 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
1214 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
1216 if (has_alpha)
1218 memset( mask_bits, 0, width_bytes * height );
1219 for (y = 0, ptr = color_bits; y < height; y++)
1220 for (x = 0; x < width; x++, ptr++)
1221 if ((*ptr >> 24) > 25) /* more than 10% alpha */
1222 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1224 else /* invert the mask */
1226 unsigned int j;
1228 ptr = (unsigned int *)mask_bits;
1229 for (j = 0; j < info->bmiHeader.biSizeImage / sizeof(*ptr); j++, ptr++) *ptr ^= ~0u;
1232 vis.depth = 1;
1233 bits.ptr = xor_bits;
1234 bits.free = NULL;
1235 bits.is_copy = TRUE;
1236 if (!(xor_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1238 bits.ptr = mask_bits;
1239 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
1241 if (mask_pixmap)
1243 cursor = XCreatePixmapCursor( gdi_display, xor_pixmap, mask_pixmap,
1244 &fg, &bg, icon->xHotspot, icon->yHotspot );
1245 XFreePixmap( gdi_display, mask_pixmap );
1247 XFreePixmap( gdi_display, xor_pixmap );
1249 done:
1250 HeapFree( GetProcessHeap(), 0, color_bits );
1251 HeapFree( GetProcessHeap(), 0, xor_bits );
1252 HeapFree( GetProcessHeap(), 0, mask_bits );
1253 return cursor;
1256 /***********************************************************************
1257 * create_cursor
1259 * Create an X cursor from a Windows one.
1261 static Cursor create_cursor( HANDLE handle )
1263 Cursor cursor = 0;
1264 ICONINFOEXW info;
1265 BITMAP bm;
1266 HDC hdc;
1268 if (!handle) return get_empty_cursor();
1270 info.cbSize = sizeof(info);
1271 if (!GetIconInfoExW( handle, &info )) return 0;
1273 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
1275 DeleteObject( info.hbmColor );
1276 DeleteObject( info.hbmMask );
1277 return cursor;
1280 GetObjectW( info.hbmMask, sizeof(bm), &bm );
1281 if (!info.hbmColor) bm.bmHeight = max( 1, bm.bmHeight / 2 );
1283 /* make sure hotspot is valid */
1284 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1286 info.xHotspot = bm.bmWidth / 2;
1287 info.yHotspot = bm.bmHeight / 2;
1290 hdc = CreateCompatibleDC( 0 );
1292 if (info.hbmColor)
1294 #ifdef SONAME_LIBXCURSOR
1295 if (pXcursorImagesLoadCursor)
1296 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
1297 #endif
1298 if (!cursor) cursor = create_xlib_color_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1299 DeleteObject( info.hbmColor );
1301 else
1303 cursor = create_xlib_monochrome_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1306 DeleteObject( info.hbmMask );
1307 DeleteDC( hdc );
1308 return cursor;
1311 /***********************************************************************
1312 * DestroyCursorIcon (X11DRV.@)
1314 void CDECL X11DRV_DestroyCursorIcon( HCURSOR handle )
1316 Cursor cursor;
1318 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1320 TRACE( "%p xid %lx\n", handle, cursor );
1321 XFreeCursor( gdi_display, cursor );
1322 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1326 /***********************************************************************
1327 * SetCursor (X11DRV.@)
1329 void CDECL X11DRV_SetCursor( HCURSOR handle )
1331 if (InterlockedExchangePointer( (void **)&last_cursor, handle ) != handle ||
1332 GetTickCount() - last_cursor_change > 100)
1334 last_cursor_change = GetTickCount();
1335 if (cursor_window) SendNotifyMessageW( cursor_window, WM_X11DRV_SET_CURSOR, 0, (LPARAM)handle );
1339 /***********************************************************************
1340 * SetCursorPos (X11DRV.@)
1342 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
1344 struct x11drv_thread_data *data = x11drv_init_thread_data();
1346 XWarpPointer( data->display, root_window, root_window, 0, 0, 0, 0,
1347 x - virtual_screen_rect.left, y - virtual_screen_rect.top );
1348 data->warp_serial = NextRequest( data->display );
1349 XNoOp( data->display );
1350 XFlush( data->display ); /* avoids bad mouse lag in games that do their own mouse warping */
1351 TRACE( "warped to %d,%d serial %lu\n", x, y, data->warp_serial );
1352 return TRUE;
1355 /***********************************************************************
1356 * GetCursorPos (X11DRV.@)
1358 BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
1360 Display *display = thread_init_display();
1361 Window root, child;
1362 int rootX, rootY, winX, winY;
1363 unsigned int xstate;
1364 BOOL ret;
1366 ret = XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &winX, &winY, &xstate );
1367 if (ret)
1369 POINT old = *pos;
1370 pos->x = winX + virtual_screen_rect.left;
1371 pos->y = winY + virtual_screen_rect.top;
1372 TRACE( "pointer at (%d,%d) server pos %d,%d\n", pos->x, pos->y, old.x, old.y );
1374 return ret;
1377 /***********************************************************************
1378 * ClipCursor (X11DRV.@)
1380 BOOL CDECL X11DRV_ClipCursor( LPCRECT clip )
1382 if (!clip) clip = &virtual_screen_rect;
1384 if (grab_pointer)
1386 HWND foreground = GetForegroundWindow();
1388 /* we are clipping if the clip rectangle is smaller than the screen */
1389 if (clip->left > virtual_screen_rect.left || clip->right < virtual_screen_rect.right ||
1390 clip->top > virtual_screen_rect.top || clip->bottom < virtual_screen_rect.bottom)
1392 DWORD tid, pid;
1394 /* forward request to the foreground window if it's in a different thread */
1395 tid = GetWindowThreadProcessId( foreground, &pid );
1396 if (tid && tid != GetCurrentThreadId() && pid == GetCurrentProcessId())
1398 TRACE( "forwarding clip request to %p\n", foreground );
1399 SendNotifyMessageW( foreground, WM_X11DRV_CLIP_CURSOR, 0, 0 );
1400 return TRUE;
1402 else if (grab_clipping_window( clip )) return TRUE;
1404 else /* if currently clipping, check if we should switch to fullscreen clipping */
1406 struct x11drv_thread_data *data = x11drv_thread_data();
1407 if (data && data->clip_hwnd)
1409 if (EqualRect( clip, &clip_rect ) || clip_fullscreen_window( foreground, TRUE ))
1410 return TRUE;
1414 ungrab_clipping_window();
1415 return TRUE;
1418 /***********************************************************************
1419 * move_resize_window
1421 void move_resize_window( HWND hwnd, int dir )
1423 Display *display = thread_display();
1424 DWORD pt;
1425 int x, y, rootX, rootY, button = 0;
1426 XEvent xev;
1427 Window win, root, child;
1428 unsigned int xstate;
1430 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1432 pt = GetMessagePos();
1433 x = (short)LOWORD( pt );
1434 y = (short)HIWORD( pt );
1436 if (GetKeyState( VK_LBUTTON ) & 0x8000) button = 1;
1437 else if (GetKeyState( VK_MBUTTON ) & 0x8000) button = 2;
1438 else if (GetKeyState( VK_RBUTTON ) & 0x8000) button = 3;
1440 TRACE( "hwnd %p/%lx, x %d, y %d, dir %d, button %d\n", hwnd, win, x, y, dir, button );
1442 xev.xclient.type = ClientMessage;
1443 xev.xclient.window = win;
1444 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1445 xev.xclient.serial = 0;
1446 xev.xclient.display = display;
1447 xev.xclient.send_event = True;
1448 xev.xclient.format = 32;
1449 xev.xclient.data.l[0] = x - virtual_screen_rect.left; /* x coord */
1450 xev.xclient.data.l[1] = y - virtual_screen_rect.top; /* y coord */
1451 xev.xclient.data.l[2] = dir; /* direction */
1452 xev.xclient.data.l[3] = button; /* button */
1453 xev.xclient.data.l[4] = 0; /* unused */
1455 /* need to ungrab the pointer that may have been automatically grabbed
1456 * with a ButtonPress event */
1457 XUngrabPointer( display, CurrentTime );
1458 XSendEvent(display, root_window, False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
1460 /* try to detect the end of the size/move by polling for the mouse button to be released */
1461 /* (some apps don't like it if we return before the size/move is done) */
1463 if (!button) return;
1464 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1466 for (;;)
1468 MSG msg;
1469 INPUT input;
1471 if (!XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &x, &y, &xstate )) break;
1473 if (!(xstate & (Button1Mask << (button - 1))))
1475 /* fake a button release event */
1476 input.type = INPUT_MOUSE;
1477 input.u.mi.dx = x + virtual_screen_rect.left;
1478 input.u.mi.dy = y + virtual_screen_rect.top;
1479 input.u.mi.mouseData = button_up_data[button - 1];
1480 input.u.mi.dwFlags = button_up_flags[button - 1] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1481 input.u.mi.time = GetTickCount();
1482 input.u.mi.dwExtraInfo = 0;
1483 __wine_send_input( hwnd, &input );
1486 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
1488 if (!CallMsgFilterW( &msg, MSGF_SIZE ))
1490 TranslateMessage( &msg );
1491 DispatchMessageW( &msg );
1495 if (!(xstate & (Button1Mask << (button - 1)))) break;
1496 MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT );
1499 TRACE( "hwnd %p/%lx done\n", hwnd, win );
1500 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1504 /***********************************************************************
1505 * X11DRV_ButtonPress
1507 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1509 XButtonEvent *event = &xev->xbutton;
1510 int buttonNum = event->button - 1;
1511 INPUT input;
1513 if (buttonNum >= NB_BUTTONS) return;
1515 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1517 input.u.mi.dx = event->x;
1518 input.u.mi.dy = event->y;
1519 input.u.mi.mouseData = button_down_data[buttonNum];
1520 input.u.mi.dwFlags = button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1521 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1522 input.u.mi.dwExtraInfo = 0;
1524 update_user_time( event->time );
1525 send_mouse_input( hwnd, event->window, event->state, &input );
1529 /***********************************************************************
1530 * X11DRV_ButtonRelease
1532 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1534 XButtonEvent *event = &xev->xbutton;
1535 int buttonNum = event->button - 1;
1536 INPUT input;
1538 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1540 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1542 input.u.mi.dx = event->x;
1543 input.u.mi.dy = event->y;
1544 input.u.mi.mouseData = button_up_data[buttonNum];
1545 input.u.mi.dwFlags = button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1546 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1547 input.u.mi.dwExtraInfo = 0;
1549 send_mouse_input( hwnd, event->window, event->state, &input );
1553 /***********************************************************************
1554 * X11DRV_MotionNotify
1556 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1558 XMotionEvent *event = &xev->xmotion;
1559 INPUT input;
1561 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d serial %lu\n",
1562 hwnd, event->window, event->x, event->y, event->is_hint, event->serial );
1564 input.u.mi.dx = event->x;
1565 input.u.mi.dy = event->y;
1566 input.u.mi.mouseData = 0;
1567 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1568 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1569 input.u.mi.dwExtraInfo = 0;
1571 if (!hwnd)
1573 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1574 if (thread_data->warp_serial && (long)(event->serial - thread_data->warp_serial) < 0) return;
1577 send_mouse_input( hwnd, event->window, event->state, &input );
1581 /***********************************************************************
1582 * X11DRV_EnterNotify
1584 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1586 XCrossingEvent *event = &xev->xcrossing;
1587 INPUT input;
1589 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd, event->window, event->x, event->y, event->detail );
1591 if (event->detail == NotifyVirtual) return;
1592 if (event->window == x11drv_thread_data()->grab_window) return;
1594 /* simulate a mouse motion event */
1595 input.u.mi.dx = event->x;
1596 input.u.mi.dy = event->y;
1597 input.u.mi.mouseData = 0;
1598 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1599 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1600 input.u.mi.dwExtraInfo = 0;
1602 send_mouse_input( hwnd, event->window, event->state, &input );
1605 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1607 /***********************************************************************
1608 * X11DRV_RawMotion
1610 static void X11DRV_RawMotion( XGenericEventCookie *xev )
1612 XIRawEvent *event = xev->data;
1613 const double *values = event->valuators.values;
1614 INPUT input;
1615 int i, j;
1616 double dx = 0, dy = 0;
1617 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1618 XIDeviceInfo *devices = thread_data->xi2_devices;
1620 if (!event->valuators.mask_len) return;
1621 if (thread_data->xi2_state != xi_enabled) return;
1623 input.u.mi.mouseData = 0;
1624 input.u.mi.dwFlags = MOUSEEVENTF_MOVE;
1625 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1626 input.u.mi.dwExtraInfo = 0;
1627 input.u.mi.dx = 0;
1628 input.u.mi.dy = 0;
1630 for (i = 0; i < thread_data->xi2_device_count; ++i)
1632 if (devices[i].deviceid != event->deviceid) continue;
1633 for (j = 0; j < devices[i].num_classes; j++)
1635 XIValuatorClassInfo *class = (XIValuatorClassInfo *)devices[i].classes[j];
1637 if (devices[i].classes[j]->type != XIValuatorClass) continue;
1638 if (XIMaskIsSet( event->valuators.mask, class->number ))
1640 double val = *values++;
1641 if (class->label == x11drv_atom( Rel_X ) ||
1642 (!class->label && class->number == 0 && class->mode == XIModeRelative))
1644 input.u.mi.dx = dx = val;
1645 if (class->min < class->max)
1646 input.u.mi.dx = val * (virtual_screen_rect.right - virtual_screen_rect.left)
1647 / (class->max - class->min);
1649 else if (class->label == x11drv_atom( Rel_Y ) ||
1650 (!class->label && class->number == 1 && class->mode == XIModeRelative))
1652 input.u.mi.dy = dy = val;
1653 if (class->min < class->max)
1654 input.u.mi.dy = val * (virtual_screen_rect.bottom - virtual_screen_rect.top)
1655 / (class->max - class->min);
1659 break;
1662 if (thread_data->warp_serial)
1664 if ((long)(xev->serial - thread_data->warp_serial) < 0)
1666 TRACE( "pos %d,%d old serial %lu, ignoring\n", input.u.mi.dx, input.u.mi.dy, xev->serial );
1667 return;
1669 thread_data->warp_serial = 0; /* we caught up now */
1672 TRACE( "pos %d,%d (event %f,%f)\n", input.u.mi.dx, input.u.mi.dy, dx, dy );
1674 input.type = INPUT_MOUSE;
1675 __wine_send_input( 0, &input );
1678 #endif /* HAVE_X11_EXTENSIONS_XINPUT2_H */
1681 /***********************************************************************
1682 * X11DRV_XInput2_Init
1684 void X11DRV_XInput2_Init(void)
1686 #if defined(SONAME_LIBXI) && defined(HAVE_X11_EXTENSIONS_XINPUT2_H)
1687 int event, error;
1688 void *libxi_handle = wine_dlopen( SONAME_LIBXI, RTLD_NOW, NULL, 0 );
1690 if (!libxi_handle)
1692 WARN( "couldn't load %s\n", SONAME_LIBXI );
1693 return;
1695 #define LOAD_FUNCPTR(f) \
1696 if (!(p##f = wine_dlsym( libxi_handle, #f, NULL, 0))) \
1698 WARN("Failed to load %s.\n", #f); \
1699 return; \
1702 LOAD_FUNCPTR(XIFreeDeviceInfo);
1703 LOAD_FUNCPTR(XIQueryDevice);
1704 LOAD_FUNCPTR(XIQueryVersion);
1705 LOAD_FUNCPTR(XISelectEvents);
1706 #undef LOAD_FUNCPTR
1708 xinput2_available = XQueryExtension( gdi_display, "XInputExtension", &xinput2_opcode, &event, &error );
1709 #else
1710 TRACE( "X Input 2 support not compiled in.\n" );
1711 #endif
1715 /***********************************************************************
1716 * X11DRV_GenericEvent
1718 void X11DRV_GenericEvent( HWND hwnd, XEvent *xev )
1720 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1721 XGenericEventCookie *event = &xev->xcookie;
1723 if (!event->data) return;
1724 if (event->extension != xinput2_opcode) return;
1726 switch (event->evtype)
1728 case XI_RawMotion:
1729 X11DRV_RawMotion( event );
1730 break;
1732 default:
1733 TRACE( "Unhandled event %#x\n", event->evtype );
1734 break;
1736 #endif