cmd: DIR command outputs free space for the path.
[wine.git] / dlls / winex11.drv / mouse.c
blob3ddcf6095494b7659e15d9aecb9242020d21eea3
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 #if 0
23 #pragma makedep unix
24 #endif
26 #include "config.h"
28 #include <math.h>
29 #include <dlfcn.h>
30 #include <X11/Xlib.h>
31 #include <X11/cursorfont.h>
32 #include <stdarg.h>
33 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
34 #include <X11/extensions/XInput2.h>
35 #endif
37 #ifdef SONAME_LIBXCURSOR
38 # include <X11/Xcursor/Xcursor.h>
39 static void *xcursor_handle;
40 # define MAKE_FUNCPTR(f) static typeof(f) * p##f
41 MAKE_FUNCPTR(XcursorImageCreate);
42 MAKE_FUNCPTR(XcursorImageDestroy);
43 MAKE_FUNCPTR(XcursorImageLoadCursor);
44 MAKE_FUNCPTR(XcursorImagesCreate);
45 MAKE_FUNCPTR(XcursorImagesDestroy);
46 MAKE_FUNCPTR(XcursorImagesLoadCursor);
47 MAKE_FUNCPTR(XcursorLibraryLoadCursor);
48 # undef MAKE_FUNCPTR
49 #endif /* SONAME_LIBXCURSOR */
51 #define OEMRESOURCE
53 #include "x11drv.h"
54 #include "winreg.h"
55 #include "wine/server.h"
56 #include "wine/debug.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
60 /**********************************************************************/
62 #ifndef Button6Mask
63 #define Button6Mask (1<<13)
64 #endif
65 #ifndef Button7Mask
66 #define Button7Mask (1<<14)
67 #endif
69 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
71 static const UINT button_down_flags[NB_BUTTONS] =
73 MOUSEEVENTF_LEFTDOWN,
74 MOUSEEVENTF_MIDDLEDOWN,
75 MOUSEEVENTF_RIGHTDOWN,
76 MOUSEEVENTF_WHEEL,
77 MOUSEEVENTF_WHEEL,
78 MOUSEEVENTF_HWHEEL,
79 MOUSEEVENTF_HWHEEL,
80 MOUSEEVENTF_XDOWN,
81 MOUSEEVENTF_XDOWN
84 static const UINT button_up_flags[NB_BUTTONS] =
86 MOUSEEVENTF_LEFTUP,
87 MOUSEEVENTF_MIDDLEUP,
88 MOUSEEVENTF_RIGHTUP,
93 MOUSEEVENTF_XUP,
94 MOUSEEVENTF_XUP
97 static const UINT button_down_data[NB_BUTTONS] =
102 WHEEL_DELTA,
103 -WHEEL_DELTA,
104 -WHEEL_DELTA,
105 WHEEL_DELTA,
106 XBUTTON1,
107 XBUTTON2
110 static const UINT button_up_data[NB_BUTTONS] =
119 XBUTTON1,
120 XBUTTON2
123 XContext cursor_context = 0;
125 static RECT clip_rect;
126 static Cursor create_cursor( HANDLE handle );
128 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
129 static BOOL xinput2_available;
130 static BOOL broken_rawevents;
131 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
132 MAKE_FUNCPTR(XIGetClientPointer);
133 MAKE_FUNCPTR(XIFreeDeviceInfo);
134 MAKE_FUNCPTR(XIQueryDevice);
135 MAKE_FUNCPTR(XIQueryVersion);
136 MAKE_FUNCPTR(XISelectEvents);
137 #undef MAKE_FUNCPTR
138 #endif
140 /***********************************************************************
141 * X11DRV_Xcursor_Init
143 * Load the Xcursor library for use.
145 void X11DRV_Xcursor_Init(void)
147 #ifdef SONAME_LIBXCURSOR
148 xcursor_handle = dlopen(SONAME_LIBXCURSOR, RTLD_NOW);
149 if (!xcursor_handle)
151 WARN("Xcursor failed to load. Using fallback code.\n");
152 return;
154 #define LOAD_FUNCPTR(f) p##f = dlsym(xcursor_handle, #f)
156 LOAD_FUNCPTR(XcursorImageCreate);
157 LOAD_FUNCPTR(XcursorImageDestroy);
158 LOAD_FUNCPTR(XcursorImageLoadCursor);
159 LOAD_FUNCPTR(XcursorImagesCreate);
160 LOAD_FUNCPTR(XcursorImagesDestroy);
161 LOAD_FUNCPTR(XcursorImagesLoadCursor);
162 LOAD_FUNCPTR(XcursorLibraryLoadCursor);
163 #undef LOAD_FUNCPTR
164 #endif /* SONAME_LIBXCURSOR */
168 /***********************************************************************
169 * get_empty_cursor
171 static Cursor get_empty_cursor(void)
173 static Cursor cursor;
174 static const char data[] = { 0 };
176 if (!cursor)
178 XColor bg;
179 Pixmap pixmap;
181 bg.red = bg.green = bg.blue = 0x0000;
182 pixmap = XCreateBitmapFromData( gdi_display, root_window, data, 1, 1 );
183 if (pixmap)
185 Cursor new = XCreatePixmapCursor( gdi_display, pixmap, pixmap, &bg, &bg, 0, 0 );
186 if (InterlockedCompareExchangePointer( (void **)&cursor, (void *)new, 0 ))
187 XFreeCursor( gdi_display, new );
188 XFreePixmap( gdi_display, pixmap );
191 return cursor;
194 /***********************************************************************
195 * set_window_cursor
197 void set_window_cursor( Window window, HCURSOR handle )
199 Cursor cursor, prev;
201 if (!handle) cursor = get_empty_cursor();
202 else if (XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
204 /* try to create it */
205 if (!(cursor = create_cursor( handle ))) return;
207 XLockDisplay( gdi_display );
208 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&prev ))
210 /* someone else was here first */
211 XFreeCursor( gdi_display, cursor );
212 cursor = prev;
214 else
216 XSaveContext( gdi_display, (XID)handle, cursor_context, (char *)cursor );
217 TRACE( "cursor %p created %lx\n", handle, cursor );
219 XUnlockDisplay( gdi_display );
222 XDefineCursor( gdi_display, window, cursor );
223 /* make the change take effect immediately */
224 XFlush( gdi_display );
227 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
228 /***********************************************************************
229 * update_relative_valuators
231 static void update_relative_valuators(XIAnyClassInfo **valuators, int n_valuators)
233 struct x11drv_thread_data *thread_data = x11drv_thread_data();
234 int i;
236 thread_data->x_valuator.number = -1;
237 thread_data->y_valuator.number = -1;
239 for (i = 0; i < n_valuators; i++)
241 XIValuatorClassInfo *class = (XIValuatorClassInfo *)valuators[i];
242 if (valuators[i]->type != XIValuatorClass) continue;
243 if (class->label == x11drv_atom( Rel_X ) ||
244 (!class->label && class->number == 0 && class->mode == XIModeRelative))
245 thread_data->x_valuator = *class;
246 else if (class->label == x11drv_atom( Rel_Y ) ||
247 (!class->label && class->number == 1 && class->mode == XIModeRelative))
248 thread_data->y_valuator = *class;
251 thread_data->x_valuator.value = 0;
252 thread_data->y_valuator.value = 0;
256 /***********************************************************************
257 * enable_xinput2
259 static void enable_xinput2(void)
261 struct x11drv_thread_data *data = x11drv_thread_data();
262 XIEventMask mask;
263 XIDeviceInfo *pointer_info;
264 unsigned char mask_bits[XIMaskLen(XI_LASTEVENT)];
265 int count;
267 if (!xinput2_available) return;
269 if (data->xi2_state == xi_unknown)
271 int major = 2, minor = 0;
272 if (!pXIQueryVersion( data->display, &major, &minor )) data->xi2_state = xi_disabled;
273 else
275 data->xi2_state = xi_unavailable;
276 WARN( "X Input 2 not available\n" );
279 if (data->xi2_state == xi_unavailable) return;
280 if (!pXIGetClientPointer( data->display, None, &data->xi2_core_pointer )) return;
282 mask.mask = mask_bits;
283 mask.mask_len = sizeof(mask_bits);
284 mask.deviceid = XIAllDevices;
285 memset( mask_bits, 0, sizeof(mask_bits) );
286 XISetMask( mask_bits, XI_DeviceChanged );
287 XISetMask( mask_bits, XI_RawMotion );
288 XISetMask( mask_bits, XI_ButtonPress );
290 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
292 pointer_info = pXIQueryDevice( data->display, data->xi2_core_pointer, &count );
293 update_relative_valuators( pointer_info->classes, pointer_info->num_classes );
294 pXIFreeDeviceInfo( pointer_info );
296 /* This device info list is only used to find the initial current slave if
297 * no XI_DeviceChanged events happened. If any hierarchy change occurred that
298 * might be relevant here (eg. user switching mice after (un)plugging), a
299 * XI_DeviceChanged event will point us to the right slave. So this list is
300 * safe to be obtained statically at enable_xinput2() time.
302 if (data->xi2_devices) pXIFreeDeviceInfo( data->xi2_devices );
303 data->xi2_devices = pXIQueryDevice( data->display, XIAllDevices, &data->xi2_device_count );
304 data->xi2_current_slave = 0;
306 data->xi2_state = xi_enabled;
309 #endif
311 /***********************************************************************
312 * disable_xinput2
314 static void disable_xinput2(void)
316 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
317 struct x11drv_thread_data *data = x11drv_thread_data();
318 XIEventMask mask;
320 if (data->xi2_state != xi_enabled) return;
322 TRACE( "disabling\n" );
323 data->xi2_state = xi_disabled;
325 mask.mask = NULL;
326 mask.mask_len = 0;
327 mask.deviceid = XIAllDevices;
329 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
330 pXIFreeDeviceInfo( data->xi2_devices );
331 data->x_valuator.number = -1;
332 data->y_valuator.number = -1;
333 data->x_valuator.value = 0;
334 data->y_valuator.value = 0;
335 data->xi2_devices = NULL;
336 data->xi2_core_pointer = 0;
337 data->xi2_current_slave = 0;
338 #endif
342 /***********************************************************************
343 * grab_clipping_window
345 * Start a pointer grab on the clip window.
347 static BOOL grab_clipping_window( const RECT *clip )
349 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
350 struct x11drv_thread_data *data = x11drv_thread_data();
351 Window clip_window;
352 HCURSOR cursor;
353 POINT pos;
355 /* don't clip in the desktop process */
356 if (NtUserGetWindowThread( NtUserGetDesktopWindow(), NULL ) == GetCurrentThreadId()) return TRUE;
357 /* don't clip the cursor if the X input focus is on another process window */
358 if (!is_current_process_focused()) return TRUE;
360 if (!data) return FALSE;
361 if (!(clip_window = init_clip_window())) return TRUE;
363 if (keyboard_grabbed)
365 WARN( "refusing to clip to %s\n", wine_dbgstr_rect(clip) );
366 return FALSE;
369 /* enable XInput2 unless we are already clipping */
370 if (!data->clipping_cursor) enable_xinput2();
372 if (data->xi2_state != xi_enabled)
374 WARN( "XInput2 not supported, refusing to clip to %s\n", wine_dbgstr_rect(clip) );
375 NtUserClipCursor( NULL );
376 return TRUE;
379 TRACE( "clipping to %s win %lx\n", wine_dbgstr_rect(clip), clip_window );
381 if (!data->clipping_cursor) XUnmapWindow( data->display, clip_window );
382 pos = virtual_screen_to_root( clip->left, clip->top );
383 XMoveResizeWindow( data->display, clip_window, pos.x, pos.y,
384 max( 1, clip->right - clip->left ), max( 1, clip->bottom - clip->top ) );
385 XMapWindow( data->display, clip_window );
387 /* if the rectangle is shrinking we may get a pointer warp */
388 if (!data->clipping_cursor || clip->left > clip_rect.left || clip->top > clip_rect.top ||
389 clip->right < clip_rect.right || clip->bottom < clip_rect.bottom)
390 data->warp_serial = NextRequest( data->display );
392 if (!XGrabPointer( data->display, clip_window, False,
393 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
394 GrabModeAsync, GrabModeAsync, clip_window, None, CurrentTime ))
395 clipping_cursor = TRUE;
397 SERVER_START_REQ( set_cursor )
399 req->flags = 0;
400 wine_server_call( req );
401 if (reply->prev_count < 0) cursor = 0;
402 else cursor = wine_server_ptr_handle( reply->prev_handle );
404 SERVER_END_REQ;
406 set_window_cursor( clip_window, cursor );
408 if (!clipping_cursor)
410 disable_xinput2();
411 return FALSE;
413 clip_rect = *clip;
414 data->clipping_cursor = TRUE;
415 return TRUE;
416 #else
417 WARN( "XInput2 was not available at compile time\n" );
418 return FALSE;
419 #endif
422 /***********************************************************************
423 * ungrab_clipping_window
425 * Release the pointer grab on the clip window.
427 void ungrab_clipping_window(void)
429 struct x11drv_thread_data *data = x11drv_init_thread_data();
430 Window clip_window = init_clip_window();
432 if (!clip_window) return;
434 TRACE( "no longer clipping\n" );
435 XUnmapWindow( data->display, clip_window );
436 if (clipping_cursor) XUngrabPointer( data->display, CurrentTime );
437 clipping_cursor = FALSE;
438 data->clipping_cursor = FALSE;
439 disable_xinput2();
442 /***********************************************************************
443 * retry_grab_clipping_window
445 * Restore the current clip rectangle.
447 void retry_grab_clipping_window(void)
449 RECT rect;
450 NtUserGetClipCursor( &rect );
451 NtUserClipCursor( &rect );
455 /***********************************************************************
456 * is_old_motion_event
458 static BOOL is_old_motion_event( unsigned long serial )
460 struct x11drv_thread_data *thread_data = x11drv_thread_data();
462 if (!thread_data->warp_serial) return FALSE;
463 if ((long)(serial - thread_data->warp_serial) < 0) return TRUE;
464 thread_data->warp_serial = 0; /* we caught up now */
465 return FALSE;
469 /***********************************************************************
470 * map_event_coords
472 * Map the input event coordinates so they're relative to the desktop.
474 static void map_event_coords( HWND hwnd, Window window, Window event_root, int x_root, int y_root, INPUT *input )
476 struct x11drv_thread_data *thread_data;
477 struct x11drv_win_data *data;
478 POINT pt = { input->mi.dx, input->mi.dy };
480 TRACE( "hwnd %p, window %lx, event_root %lx, x_root %d, y_root %d, input %p\n", hwnd, window, event_root,
481 x_root, y_root, input );
483 if (!hwnd)
485 thread_data = x11drv_thread_data();
486 if (!thread_data->clipping_cursor) return;
487 if (thread_data->clip_window != window) return;
488 pt.x += clip_rect.left;
489 pt.y += clip_rect.top;
491 else if ((data = get_win_data( hwnd )))
493 if (window == root_window) pt = root_to_virtual_screen( pt.x, pt.y );
494 else if (event_root == root_window) pt = root_to_virtual_screen( x_root, y_root );
495 else
497 if (window == data->whole_window)
499 pt.x += data->whole_rect.left - data->client_rect.left;
500 pt.y += data->whole_rect.top - data->client_rect.top;
503 if (NtUserGetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
504 pt.x = data->client_rect.right - data->client_rect.left - 1 - pt.x;
505 NtUserMapWindowPoints( hwnd, 0, &pt, 1 );
507 release_win_data( data );
510 TRACE( "mapped %s to %s\n", wine_dbgstr_point( (POINT *)&input->mi.dx ), wine_dbgstr_point( &pt ) );
512 input->mi.dx = pt.x;
513 input->mi.dy = pt.y;
516 /***********************************************************************
517 * send_mouse_input
519 * Update the various window states on a mouse event.
521 static void send_mouse_input( HWND hwnd, Window window, unsigned int state, INPUT *input )
523 struct x11drv_win_data *data;
525 input->type = INPUT_MOUSE;
527 if (!hwnd)
529 struct x11drv_thread_data *thread_data = x11drv_thread_data();
530 if (!thread_data->clipping_cursor || thread_data->clip_window != window) return;
531 __wine_send_input( hwnd, input, NULL );
532 return;
535 if (!(data = get_win_data( hwnd ))) return;
536 release_win_data( data );
538 /* update the wine server Z-order */
540 if (hwnd != x11drv_thread_data()->grab_hwnd &&
541 /* ignore event if a button is pressed, since the mouse is then grabbed too */
542 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
544 RECT rect = { input->mi.dx, input->mi.dy, input->mi.dx + 1, input->mi.dy + 1 };
546 SERVER_START_REQ( update_window_zorder )
548 req->window = wine_server_user_handle( hwnd );
549 req->rect.left = rect.left;
550 req->rect.top = rect.top;
551 req->rect.right = rect.right;
552 req->rect.bottom = rect.bottom;
553 wine_server_call( req );
555 SERVER_END_REQ;
558 __wine_send_input( hwnd, input, NULL );
561 #ifdef SONAME_LIBXCURSOR
563 /***********************************************************************
564 * create_xcursor_frame
566 * Use Xcursor to create a frame of an X cursor from a Windows one.
568 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
569 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
570 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
571 int width, int height, int istep )
573 XcursorImage *image, *ret = NULL;
574 DWORD delay_jiffies, num_steps;
575 int x, y, i;
576 BOOL has_alpha = FALSE;
577 XcursorPixel *ptr;
579 image = pXcursorImageCreate( width, height );
580 if (!image)
582 ERR("X11 failed to produce a cursor frame!\n");
583 return NULL;
586 image->xhot = iinfo->xHotspot;
587 image->yhot = iinfo->yHotspot;
589 image->delay = 100; /* fallback delay, 100 ms */
590 if (NtUserGetCursorFrameInfo(icon, istep, &delay_jiffies, &num_steps) != 0)
591 image->delay = (100 * delay_jiffies) / 6; /* convert jiffies (1/60s) to milliseconds */
592 else
593 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep);
595 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
596 memset( color_bits, 0x00, color_size );
597 NtGdiSelectBitmap( hdc, hbmColor );
598 if (!NtUserDrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
600 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
601 goto cleanup;
603 memcpy( image->pixels, color_bits, color_size );
605 /* check if the cursor frame was drawn with an alpha channel */
606 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
607 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
609 /* if no alpha channel was drawn then generate it from the mask */
610 if (!has_alpha)
612 unsigned int width_bytes = (width + 31) / 32 * 4;
614 /* draw the cursor mask to a temporary buffer */
615 memset( mask_bits, 0xFF, mask_size );
616 NtGdiSelectBitmap( hdc, hbmMask );
617 if (!NtUserDrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
619 ERR("Failed to draw frame mask %d.\n", istep);
620 goto cleanup;
622 /* use the buffer to directly modify the XcursorImage alpha channel */
623 for (y = 0, ptr = image->pixels; y < height; y++)
624 for (x = 0; x < width; x++, ptr++)
625 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
626 *ptr |= 0xff000000;
628 ret = image;
630 cleanup:
631 if (ret == NULL) pXcursorImageDestroy( image );
632 return ret;
635 /***********************************************************************
636 * create_xcursor_cursor
638 * Use Xcursor to create an X cursor from a Windows one.
640 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
642 unsigned char *color_bits, *mask_bits;
643 HBITMAP hbmColor = 0, hbmMask = 0;
644 DWORD nFrames, delay_jiffies, i;
645 int color_size, mask_size;
646 BITMAPINFO *info = NULL;
647 XcursorImages *images;
648 XcursorImage **imgs;
649 Cursor cursor = 0;
651 /* Retrieve the number of frames to render */
652 if (!NtUserGetCursorFrameInfo(icon, 0, &delay_jiffies, &nFrames)) return 0;
653 if (!(imgs = calloc( 1, sizeof(XcursorImage*) * nFrames ))) return 0;
655 /* Allocate all of the resources necessary to obtain a cursor frame */
656 if (!(info = malloc( FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
657 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
658 info->bmiHeader.biWidth = width;
659 info->bmiHeader.biHeight = -height;
660 info->bmiHeader.biPlanes = 1;
661 info->bmiHeader.biCompression = BI_RGB;
662 info->bmiHeader.biXPelsPerMeter = 0;
663 info->bmiHeader.biYPelsPerMeter = 0;
664 info->bmiHeader.biClrUsed = 0;
665 info->bmiHeader.biClrImportant = 0;
666 info->bmiHeader.biBitCount = 32;
667 color_size = width * height * 4;
668 info->bmiHeader.biSizeImage = color_size;
669 hbmColor = NtGdiCreateDIBSection( hdc, NULL, 0, info, DIB_RGB_COLORS, 0, 0, 0, (void **)&color_bits );
670 if (!hbmColor)
672 ERR("Failed to create DIB section for cursor color data!\n");
673 goto cleanup;
675 info->bmiHeader.biBitCount = 1;
676 info->bmiColors[0].rgbRed = 0;
677 info->bmiColors[0].rgbGreen = 0;
678 info->bmiColors[0].rgbBlue = 0;
679 info->bmiColors[0].rgbReserved = 0;
680 info->bmiColors[1].rgbRed = 0xff;
681 info->bmiColors[1].rgbGreen = 0xff;
682 info->bmiColors[1].rgbBlue = 0xff;
683 info->bmiColors[1].rgbReserved = 0;
685 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
686 info->bmiHeader.biSizeImage = mask_size;
687 hbmMask = NtGdiCreateDIBSection( hdc, NULL, 0, info, DIB_RGB_COLORS, 0, 0, 0, (void **)&mask_bits );
688 if (!hbmMask)
690 ERR("Failed to create DIB section for cursor mask data!\n");
691 goto cleanup;
694 /* Create an XcursorImage for each frame of the cursor */
695 for (i=0; i<nFrames; i++)
697 imgs[i] = create_xcursor_frame( hdc, iinfo, icon,
698 hbmColor, color_bits, color_size,
699 hbmMask, mask_bits, mask_size,
700 width, height, i );
701 if (!imgs[i]) goto cleanup;
704 /* Build an X cursor out of all of the frames */
705 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
706 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
707 images->images[images->nimage] = imgs[images->nimage];
708 cursor = pXcursorImagesLoadCursor( gdi_display, images );
709 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
710 free( imgs );
711 imgs = NULL;
713 cleanup:
714 if (imgs)
716 /* Failed to produce a cursor, free previously allocated frames */
717 for (i=0; i<nFrames && imgs[i]; i++)
718 pXcursorImageDestroy( imgs[i] );
719 free( imgs );
721 /* Cleanup all of the resources used to obtain the frame data */
722 if (hbmColor) NtGdiDeleteObjectApp( hbmColor );
723 if (hbmMask) NtGdiDeleteObjectApp( hbmMask );
724 free( info );
725 return cursor;
728 #endif /* SONAME_LIBXCURSOR */
731 struct system_cursors
733 WORD id;
734 const char *names[8];
737 static const struct system_cursors user32_cursors[] =
739 { OCR_NORMAL, { "left_ptr" }},
740 { OCR_IBEAM, { "xterm", "text" }},
741 { OCR_WAIT, { "watch", "wait" }},
742 { OCR_CROSS, { "cross" }},
743 { OCR_UP, { "center_ptr" }},
744 { OCR_SIZE, { "fleur", "size_all" }},
745 { OCR_SIZEALL, { "fleur", "size_all" }},
746 { OCR_ICON, { "icon" }},
747 { OCR_SIZENWSE, { "top_left_corner", "nw-resize" }},
748 { OCR_SIZENESW, { "top_right_corner", "ne-resize" }},
749 { OCR_SIZEWE, { "h_double_arrow", "size_hor", "ew-resize" }},
750 { OCR_SIZENS, { "v_double_arrow", "size_ver", "ns-resize" }},
751 { OCR_NO, { "not-allowed", "forbidden", "no-drop" }},
752 { OCR_HAND, { "hand2", "pointer", "pointing-hand" }},
753 { OCR_APPSTARTING, { "left_ptr_watch" }},
754 { OCR_HELP, { "question_arrow", "help" }},
755 { 0 }
758 static const struct system_cursors comctl32_cursors[] =
760 { 102, { "move", "dnd-move" }},
761 { 104, { "copy", "dnd-copy" }},
762 { 105, { "left_ptr" }},
763 { 106, { "col-resize", "split_v" }},
764 { 107, { "col-resize", "split_v" }},
765 { 108, { "hand2", "pointer", "pointing-hand" }},
766 { 135, { "row-resize", "split_h" }},
767 { 0 }
770 static const struct system_cursors ole32_cursors[] =
772 { 1, { "no-drop", "dnd-no-drop" }},
773 { 2, { "move", "dnd-move" }},
774 { 3, { "copy", "dnd-copy" }},
775 { 4, { "alias", "dnd-link" }},
776 { 0 }
779 static const struct system_cursors riched20_cursors[] =
781 { 105, { "hand2", "pointer", "pointing-hand" }},
782 { 107, { "right_ptr" }},
783 { 109, { "copy", "dnd-copy" }},
784 { 110, { "move", "dnd-move" }},
785 { 111, { "no-drop", "dnd-no-drop" }},
786 { 0 }
789 static const struct
791 const struct system_cursors *cursors;
792 WCHAR name[16];
793 } module_cursors[] =
795 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
796 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
797 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
798 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
801 struct cursor_font_fallback
803 const char *name;
804 unsigned int shape;
807 static const struct cursor_font_fallback fallbacks[] =
809 { "X_cursor", XC_X_cursor },
810 { "arrow", XC_arrow },
811 { "based_arrow_down", XC_based_arrow_down },
812 { "based_arrow_up", XC_based_arrow_up },
813 { "boat", XC_boat },
814 { "bogosity", XC_bogosity },
815 { "bottom_left_corner", XC_bottom_left_corner },
816 { "bottom_right_corner", XC_bottom_right_corner },
817 { "bottom_side", XC_bottom_side },
818 { "bottom_tee", XC_bottom_tee },
819 { "box_spiral", XC_box_spiral },
820 { "center_ptr", XC_center_ptr },
821 { "circle", XC_circle },
822 { "clock", XC_clock },
823 { "coffee_mug", XC_coffee_mug },
824 { "col-resize", XC_sb_h_double_arrow },
825 { "cross", XC_cross },
826 { "cross_reverse", XC_cross_reverse },
827 { "crosshair", XC_crosshair },
828 { "diamond_cross", XC_diamond_cross },
829 { "dot", XC_dot },
830 { "dotbox", XC_dotbox },
831 { "double_arrow", XC_double_arrow },
832 { "draft_large", XC_draft_large },
833 { "draft_small", XC_draft_small },
834 { "draped_box", XC_draped_box },
835 { "exchange", XC_exchange },
836 { "fleur", XC_fleur },
837 { "gobbler", XC_gobbler },
838 { "gumby", XC_gumby },
839 { "h_double_arrow", XC_sb_h_double_arrow },
840 { "hand1", XC_hand1 },
841 { "hand2", XC_hand2 },
842 { "heart", XC_heart },
843 { "icon", XC_icon },
844 { "iron_cross", XC_iron_cross },
845 { "left_ptr", XC_left_ptr },
846 { "left_side", XC_left_side },
847 { "left_tee", XC_left_tee },
848 { "leftbutton", XC_leftbutton },
849 { "ll_angle", XC_ll_angle },
850 { "lr_angle", XC_lr_angle },
851 { "man", XC_man },
852 { "middlebutton", XC_middlebutton },
853 { "mouse", XC_mouse },
854 { "pencil", XC_pencil },
855 { "pirate", XC_pirate },
856 { "plus", XC_plus },
857 { "question_arrow", XC_question_arrow },
858 { "right_ptr", XC_right_ptr },
859 { "right_side", XC_right_side },
860 { "right_tee", XC_right_tee },
861 { "rightbutton", XC_rightbutton },
862 { "row-resize", XC_sb_v_double_arrow },
863 { "rtl_logo", XC_rtl_logo },
864 { "sailboat", XC_sailboat },
865 { "sb_down_arrow", XC_sb_down_arrow },
866 { "sb_h_double_arrow", XC_sb_h_double_arrow },
867 { "sb_left_arrow", XC_sb_left_arrow },
868 { "sb_right_arrow", XC_sb_right_arrow },
869 { "sb_up_arrow", XC_sb_up_arrow },
870 { "sb_v_double_arrow", XC_sb_v_double_arrow },
871 { "shuttle", XC_shuttle },
872 { "sizing", XC_sizing },
873 { "spider", XC_spider },
874 { "spraycan", XC_spraycan },
875 { "star", XC_star },
876 { "target", XC_target },
877 { "tcross", XC_tcross },
878 { "top_left_arrow", XC_top_left_arrow },
879 { "top_left_corner", XC_top_left_corner },
880 { "top_right_corner", XC_top_right_corner },
881 { "top_side", XC_top_side },
882 { "top_tee", XC_top_tee },
883 { "trek", XC_trek },
884 { "ul_angle", XC_ul_angle },
885 { "umbrella", XC_umbrella },
886 { "ur_angle", XC_ur_angle },
887 { "v_double_arrow", XC_sb_v_double_arrow },
888 { "watch", XC_watch },
889 { "xterm", XC_xterm }
892 static int fallback_cmp( const void *key, const void *member )
894 const struct cursor_font_fallback *fallback = member;
895 return strcmp( key, fallback->name );
898 static int find_fallback_shape( const char *name )
900 struct cursor_font_fallback *fallback;
902 if ((fallback = bsearch( name, fallbacks, ARRAY_SIZE( fallbacks ),
903 sizeof(*fallback), fallback_cmp )))
904 return fallback->shape;
905 return -1;
908 /***********************************************************************
909 * create_xcursor_system_cursor
911 * Create an X cursor for a system cursor.
913 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
915 const struct system_cursors *cursors;
916 const WCHAR *module;
917 unsigned int i;
918 Cursor cursor = 0;
919 HKEY key;
920 const char * const *names = NULL;
921 WCHAR *p, name[MAX_PATH * 2];
922 char valueA[64];
924 if (!info->szModName[0]) return 0;
926 p = wcsrchr( info->szModName, '\\' );
927 wcscpy( name, p ? p + 1 : info->szModName );
928 p = name + lstrlenW( name );
929 *p++ = ',';
930 if (info->szResName[0]) wcscpy( p, info->szResName );
931 else
933 char buf[16];
934 sprintf( buf, "%hu", info->wResID );
935 asciiz_to_unicode( p, buf );
937 valueA[0] = 0;
939 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
940 if ((key = open_hkcu_key( "Software\\Wine\\X11 Driver\\Cursors" )))
942 char buffer[4096];
943 KEY_VALUE_PARTIAL_INFORMATION *value = (void *)buffer;
944 DWORD size = query_reg_value( key, NULL, value, sizeof(buffer) );
945 NtClose( key );
946 if (size && value->Type == REG_SZ)
948 const WCHAR *valueW = (const WCHAR *)value->Data;
949 if (!valueW[0]) return 0; /* force standard cursor */
950 if (!ntdll_wcstoumbs( valueW, lstrlenW(valueW) + 1, valueA, sizeof(valueA), FALSE ))
951 valueA[0] = 0;
952 goto done;
956 if (info->szResName[0]) goto done; /* only integer resources are supported here */
958 if ((module = wcsrchr( info->szModName, '\\' ))) module++;
959 else module = info->szModName;
960 for (i = 0; i < ARRAY_SIZE( module_cursors ); i++)
961 if (!wcsicmp( module, module_cursors[i].name )) break;
962 if (i == ARRAY_SIZE( module_cursors )) goto done;
964 cursors = module_cursors[i].cursors;
965 for (i = 0; cursors[i].id; i++)
966 if (cursors[i].id == info->wResID)
968 strcpy( valueA, cursors[i].names[0] );
969 names = cursors[i].names;
970 break;
973 done:
974 if (valueA[0])
976 #ifdef SONAME_LIBXCURSOR
977 if (pXcursorLibraryLoadCursor)
979 if (!names)
980 cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
981 else
982 while (*names && !cursor) cursor = pXcursorLibraryLoadCursor( gdi_display, *names++ );
984 #endif
985 if (!cursor)
987 int shape = find_fallback_shape( valueA );
988 if (shape != -1) cursor = XCreateFontCursor( gdi_display, shape );
990 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
991 debugstr_w(name), debugstr_a(valueA) );
993 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
994 return cursor;
998 /***********************************************************************
999 * create_xlib_monochrome_cursor
1001 * Create a monochrome X cursor from a Windows one.
1003 static Cursor create_xlib_monochrome_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1005 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1006 BITMAPINFO *info = (BITMAPINFO *)buffer;
1007 const int and_y = 0;
1008 const int xor_y = height;
1009 unsigned int width_bytes = (width + 31) / 32 * 4;
1010 unsigned char *mask_bits = NULL;
1011 GC gc;
1012 XColor fg, bg;
1013 XVisualInfo vis = default_visual;
1014 Pixmap src_pixmap, bits_pixmap, mask_pixmap;
1015 struct gdi_image_bits bits;
1016 Cursor cursor = 0;
1018 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1019 info->bmiHeader.biWidth = width;
1020 info->bmiHeader.biHeight = -height * 2;
1021 info->bmiHeader.biPlanes = 1;
1022 info->bmiHeader.biBitCount = 1;
1023 info->bmiHeader.biCompression = BI_RGB;
1024 info->bmiHeader.biSizeImage = width_bytes * height * 2;
1025 info->bmiHeader.biXPelsPerMeter = 0;
1026 info->bmiHeader.biYPelsPerMeter = 0;
1027 info->bmiHeader.biClrUsed = 0;
1028 info->bmiHeader.biClrImportant = 0;
1030 if (!(mask_bits = malloc( info->bmiHeader.biSizeImage ))) goto done;
1031 if (!NtGdiGetDIBitsInternal( hdc, icon->hbmMask, 0, height * 2, mask_bits, info,
1032 DIB_RGB_COLORS, 0, 0 )) goto done;
1034 vis.depth = 1;
1035 bits.ptr = mask_bits;
1036 bits.free = NULL;
1037 bits.is_copy = TRUE;
1038 if (!(src_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1040 bits_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
1041 mask_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
1042 gc = XCreateGC( gdi_display, src_pixmap, 0, NULL );
1043 XSetGraphicsExposures( gdi_display, gc, False );
1045 /* We have to do some magic here, as cursors are not fully
1046 * compatible between Windows and X11. Under X11, there are
1047 * only 3 possible color cursor: black, white and masked. So
1048 * we map the 4th Windows color (invert the bits on the screen)
1049 * to black and an additional white bit on another place
1050 * (+1,+1). This require some boolean arithmetic:
1052 * Windows | X11
1053 * And Xor Result | Bits Mask Result
1054 * 0 0 black | 0 1 background
1055 * 0 1 white | 1 1 foreground
1056 * 1 0 no change | X 0 no change
1057 * 1 1 inverted | 0 1 background
1059 * which gives:
1060 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
1061 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
1063 XSetFunction( gdi_display, gc, GXcopy );
1064 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 0, 0 );
1065 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 0, 0 );
1066 XSetFunction( gdi_display, gc, GXandReverse );
1067 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, xor_y, width, height, 0, 0 );
1068 XSetFunction( gdi_display, gc, GXorReverse );
1069 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, xor_y, width, height, 0, 0 );
1070 /* additional white */
1071 XSetFunction( gdi_display, gc, GXand );
1072 XCopyArea( gdi_display, src_pixmap, src_pixmap, gc, 0, xor_y, width, height, 0, and_y );
1073 XSetFunction( gdi_display, gc, GXor );
1074 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 1, 1 );
1075 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 1, 1 );
1076 XFreeGC( gdi_display, gc );
1078 fg.red = fg.green = fg.blue = 0xffff;
1079 bg.red = bg.green = bg.blue = 0;
1080 cursor = XCreatePixmapCursor( gdi_display, bits_pixmap, mask_pixmap,
1081 &fg, &bg, icon->xHotspot, icon->yHotspot );
1082 XFreePixmap( gdi_display, src_pixmap );
1083 XFreePixmap( gdi_display, bits_pixmap );
1084 XFreePixmap( gdi_display, mask_pixmap );
1086 done:
1087 free( mask_bits );
1088 return cursor;
1091 static BOOL get_icon_info( HICON handle, ICONINFOEXW *ret )
1093 UNICODE_STRING module, res_name;
1094 ICONINFO info;
1096 module.Buffer = ret->szModName;
1097 module.MaximumLength = sizeof(ret->szModName) - sizeof(WCHAR);
1098 res_name.Buffer = ret->szResName;
1099 res_name.MaximumLength = sizeof(ret->szResName) - sizeof(WCHAR);
1100 if (!NtUserGetIconInfo( handle, &info, &module, &res_name, NULL, 0 )) return FALSE;
1101 ret->fIcon = info.fIcon;
1102 ret->xHotspot = info.xHotspot;
1103 ret->yHotspot = info.yHotspot;
1104 ret->hbmColor = info.hbmColor;
1105 ret->hbmMask = info.hbmMask;
1106 ret->wResID = res_name.Length ? 0 : LOWORD( res_name.Buffer );
1107 ret->szModName[module.Length] = 0;
1108 ret->szResName[res_name.Length] = 0;
1109 return TRUE;
1112 /***********************************************************************
1113 * create_xlib_load_mono_cursor
1115 * Create a monochrome X cursor from a color Windows one by trying to load the monochrome resource.
1117 static Cursor create_xlib_load_mono_cursor( HDC hdc, HANDLE handle, int width, int height )
1119 Cursor cursor = None;
1120 HANDLE mono;
1121 ICONINFOEXW info;
1122 BITMAP bm;
1124 if (!(mono = CopyImage( handle, IMAGE_CURSOR, width, height, LR_MONOCHROME | LR_COPYFROMRESOURCE )))
1125 return None;
1127 if (get_icon_info( mono, &info ))
1129 if (!info.hbmColor)
1131 NtGdiExtGetObjectW( info.hbmMask, sizeof(bm), &bm );
1132 bm.bmHeight = max( 1, bm.bmHeight / 2 );
1133 /* make sure hotspot is valid */
1134 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1136 info.xHotspot = bm.bmWidth / 2;
1137 info.yHotspot = bm.bmHeight / 2;
1139 cursor = create_xlib_monochrome_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1141 else NtGdiDeleteObjectApp( info.hbmColor );
1142 NtGdiDeleteObjectApp( info.hbmMask );
1144 NtUserDestroyCursor( mono, 0 );
1145 return cursor;
1148 /***********************************************************************
1149 * create_xlib_color_cursor
1151 * Create a color X cursor from a Windows one.
1153 static Cursor create_xlib_color_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1155 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1156 BITMAPINFO *info = (BITMAPINFO *)buffer;
1157 XColor fg, bg;
1158 Cursor cursor = None;
1159 XVisualInfo vis = default_visual;
1160 Pixmap xor_pixmap, mask_pixmap;
1161 struct gdi_image_bits bits;
1162 unsigned int *color_bits = NULL, *ptr;
1163 unsigned char *mask_bits = NULL, *xor_bits = NULL;
1164 int i, x, y;
1165 BOOL has_alpha = FALSE;
1166 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
1167 unsigned int width_bytes = (width + 31) / 32 * 4;
1169 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1170 info->bmiHeader.biWidth = width;
1171 info->bmiHeader.biHeight = -height;
1172 info->bmiHeader.biPlanes = 1;
1173 info->bmiHeader.biBitCount = 1;
1174 info->bmiHeader.biCompression = BI_RGB;
1175 info->bmiHeader.biSizeImage = width_bytes * height;
1176 info->bmiHeader.biXPelsPerMeter = 0;
1177 info->bmiHeader.biYPelsPerMeter = 0;
1178 info->bmiHeader.biClrUsed = 0;
1179 info->bmiHeader.biClrImportant = 0;
1181 if (!(mask_bits = malloc( info->bmiHeader.biSizeImage ))) goto done;
1182 if (!NtGdiGetDIBitsInternal( hdc, icon->hbmMask, 0, height, mask_bits, info,
1183 DIB_RGB_COLORS, 0, 0 )) goto done;
1185 info->bmiHeader.biBitCount = 32;
1186 info->bmiHeader.biSizeImage = width * height * 4;
1187 if (!(color_bits = malloc( info->bmiHeader.biSizeImage ))) goto done;
1188 if (!(xor_bits = calloc( 1, width_bytes * height ))) goto done;
1189 NtGdiGetDIBitsInternal( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS, 0, 0 );
1191 /* compute fg/bg color and xor bitmap based on average of the color values */
1193 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
1194 for (y = 0, ptr = color_bits; y < height; y++)
1196 for (x = 0; x < width; x++, ptr++)
1198 int red = (*ptr >> 16) & 0xff;
1199 int green = (*ptr >> 8) & 0xff;
1200 int blue = (*ptr >> 0) & 0xff;
1201 if (red + green + blue > 0x40)
1203 rfg += red;
1204 gfg += green;
1205 bfg += blue;
1206 fgBits++;
1207 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1209 else
1211 rbg += red;
1212 gbg += green;
1213 bbg += blue;
1217 if (fgBits)
1219 fg.red = rfg * 257 / fgBits;
1220 fg.green = gfg * 257 / fgBits;
1221 fg.blue = bfg * 257 / fgBits;
1223 else fg.red = fg.green = fg.blue = 0;
1224 bgBits = width * height - fgBits;
1225 if (bgBits)
1227 bg.red = rbg * 257 / bgBits;
1228 bg.green = gbg * 257 / bgBits;
1229 bg.blue = bbg * 257 / bgBits;
1231 else bg.red = bg.green = bg.blue = 0;
1233 info->bmiHeader.biBitCount = 1;
1234 info->bmiHeader.biClrUsed = 0;
1235 info->bmiHeader.biSizeImage = width_bytes * height;
1237 /* generate mask from the alpha channel if we have one */
1239 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
1240 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
1242 if (has_alpha)
1244 memset( mask_bits, 0, width_bytes * height );
1245 for (y = 0, ptr = color_bits; y < height; y++)
1246 for (x = 0; x < width; x++, ptr++)
1247 if ((*ptr >> 24) > 25) /* more than 10% alpha */
1248 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1250 else /* invert the mask */
1252 unsigned int j;
1254 ptr = (unsigned int *)mask_bits;
1255 for (j = 0; j < info->bmiHeader.biSizeImage / sizeof(*ptr); j++, ptr++) *ptr ^= ~0u;
1258 vis.depth = 1;
1259 bits.ptr = xor_bits;
1260 bits.free = NULL;
1261 bits.is_copy = TRUE;
1262 if (!(xor_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1264 bits.ptr = mask_bits;
1265 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
1267 if (mask_pixmap)
1269 cursor = XCreatePixmapCursor( gdi_display, xor_pixmap, mask_pixmap,
1270 &fg, &bg, icon->xHotspot, icon->yHotspot );
1271 XFreePixmap( gdi_display, mask_pixmap );
1273 XFreePixmap( gdi_display, xor_pixmap );
1275 done:
1276 free( color_bits );
1277 free( xor_bits );
1278 free( mask_bits );
1279 return cursor;
1282 /***********************************************************************
1283 * create_cursor
1285 * Create an X cursor from a Windows one.
1287 static Cursor create_cursor( HANDLE handle )
1289 Cursor cursor = 0;
1290 ICONINFOEXW info;
1291 BITMAP bm;
1292 HDC hdc;
1294 if (!handle) return get_empty_cursor();
1296 if (!get_icon_info( handle, &info )) return 0;
1298 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
1300 NtGdiDeleteObjectApp( info.hbmColor );
1301 NtGdiDeleteObjectApp( info.hbmMask );
1302 return cursor;
1305 NtGdiExtGetObjectW( info.hbmMask, sizeof(bm), &bm );
1306 if (!info.hbmColor) bm.bmHeight = max( 1, bm.bmHeight / 2 );
1308 /* make sure hotspot is valid */
1309 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1311 info.xHotspot = bm.bmWidth / 2;
1312 info.yHotspot = bm.bmHeight / 2;
1315 hdc = NtGdiCreateCompatibleDC( 0 );
1317 if (info.hbmColor)
1319 #ifdef SONAME_LIBXCURSOR
1320 if (pXcursorImagesLoadCursor)
1321 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
1322 #endif
1323 if (!cursor) cursor = create_xlib_load_mono_cursor( hdc, handle, bm.bmWidth, bm.bmHeight );
1324 if (!cursor) cursor = create_xlib_color_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1325 NtGdiDeleteObjectApp( info.hbmColor );
1327 else
1329 cursor = create_xlib_monochrome_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1332 NtGdiDeleteObjectApp( info.hbmMask );
1333 NtGdiDeleteObjectApp( hdc );
1334 return cursor;
1337 /***********************************************************************
1338 * DestroyCursorIcon (X11DRV.@)
1340 void X11DRV_DestroyCursorIcon( HCURSOR handle )
1342 Cursor cursor;
1344 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1346 TRACE( "%p xid %lx\n", handle, cursor );
1347 XFreeCursor( gdi_display, cursor );
1348 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1352 /***********************************************************************
1353 * SetCursor (X11DRV.@)
1355 void X11DRV_SetCursor( HWND hwnd, HCURSOR handle )
1357 struct x11drv_win_data *data;
1359 if ((data = get_win_data( hwnd )))
1361 set_window_cursor( data->whole_window, handle );
1362 release_win_data( data );
1365 if (clipping_cursor) set_window_cursor( x11drv_thread_data()->clip_window, handle );
1368 /***********************************************************************
1369 * SetCursorPos (X11DRV.@)
1371 BOOL X11DRV_SetCursorPos( INT x, INT y )
1373 struct x11drv_thread_data *data = x11drv_init_thread_data();
1374 POINT pos = virtual_screen_to_root( x, y );
1376 if (keyboard_grabbed)
1378 WARN( "refusing to warp to %u, %u\n", (int)pos.x, (int)pos.y );
1379 return FALSE;
1382 if (!clipping_cursor &&
1383 XGrabPointer( data->display, root_window, False,
1384 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1385 GrabModeAsync, GrabModeAsync, None, None, CurrentTime ) != GrabSuccess)
1387 WARN( "refusing to warp pointer to %u, %u without exclusive grab\n", (int)pos.x, (int)pos.y );
1388 return FALSE;
1391 XWarpPointer( data->display, root_window, root_window, 0, 0, 0, 0, pos.x, pos.y );
1392 data->warp_serial = NextRequest( data->display );
1394 if (!clipping_cursor)
1395 XUngrabPointer( data->display, CurrentTime );
1397 XNoOp( data->display );
1398 XFlush( data->display ); /* avoids bad mouse lag in games that do their own mouse warping */
1399 TRACE( "warped to %d,%d serial %lu\n", x, y, data->warp_serial );
1400 return TRUE;
1403 /***********************************************************************
1404 * GetCursorPos (X11DRV.@)
1406 BOOL X11DRV_GetCursorPos(LPPOINT pos)
1408 Display *display = thread_init_display();
1409 Window root, child;
1410 int rootX, rootY, winX, winY;
1411 unsigned int xstate;
1412 BOOL ret;
1414 ret = XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &winX, &winY, &xstate );
1415 if (ret)
1417 POINT old = *pos;
1418 *pos = root_to_virtual_screen( winX, winY );
1419 TRACE( "pointer at %s server pos %s\n", wine_dbgstr_point(pos), wine_dbgstr_point(&old) );
1421 return ret;
1424 /***********************************************************************
1425 * ClipCursor (X11DRV.@)
1427 BOOL X11DRV_ClipCursor( const RECT *clip, BOOL reset )
1429 if (!reset && clip && grab_clipping_window( clip )) return TRUE;
1430 ungrab_clipping_window();
1431 return TRUE;
1434 /***********************************************************************
1435 * move_resize_window
1437 void move_resize_window( HWND hwnd, int dir )
1439 Display *display = thread_display();
1440 DWORD pt;
1441 POINT pos;
1442 int button = 0;
1443 XEvent xev;
1444 Window win, root, child;
1445 unsigned int xstate;
1447 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1449 pt = NtUserGetThreadInfo()->message_pos;
1450 pos = virtual_screen_to_root( (short)LOWORD( pt ), (short)HIWORD( pt ) );
1452 if (NtUserGetKeyState( VK_LBUTTON ) & 0x8000) button = 1;
1453 else if (NtUserGetKeyState( VK_MBUTTON ) & 0x8000) button = 2;
1454 else if (NtUserGetKeyState( VK_RBUTTON ) & 0x8000) button = 3;
1456 TRACE( "hwnd %p/%lx, pos %s, dir %d, button %d\n", hwnd, win, wine_dbgstr_point(&pos), dir, button );
1458 xev.xclient.type = ClientMessage;
1459 xev.xclient.window = win;
1460 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1461 xev.xclient.serial = 0;
1462 xev.xclient.display = display;
1463 xev.xclient.send_event = True;
1464 xev.xclient.format = 32;
1465 xev.xclient.data.l[0] = pos.x; /* x coord */
1466 xev.xclient.data.l[1] = pos.y; /* y coord */
1467 xev.xclient.data.l[2] = dir; /* direction */
1468 xev.xclient.data.l[3] = button; /* button */
1469 xev.xclient.data.l[4] = 0; /* unused */
1471 /* need to ungrab the pointer that may have been automatically grabbed
1472 * with a ButtonPress event */
1473 XUngrabPointer( display, CurrentTime );
1474 XSendEvent(display, root_window, False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
1476 /* try to detect the end of the size/move by polling for the mouse button to be released */
1477 /* (some apps don't like it if we return before the size/move is done) */
1479 if (!button) return;
1480 send_message( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1482 for (;;)
1484 MSG msg;
1485 INPUT input;
1486 int x, y, rootX, rootY;
1488 if (!XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &x, &y, &xstate )) break;
1490 if (!(xstate & (Button1Mask << (button - 1))))
1492 /* fake a button release event */
1493 pos = root_to_virtual_screen( x, y );
1494 input.type = INPUT_MOUSE;
1495 input.mi.dx = pos.x;
1496 input.mi.dy = pos.y;
1497 input.mi.mouseData = button_up_data[button - 1];
1498 input.mi.dwFlags = button_up_flags[button - 1] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1499 input.mi.time = NtGetTickCount();
1500 input.mi.dwExtraInfo = 0;
1501 __wine_send_input( hwnd, &input, NULL );
1504 while (NtUserPeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
1506 if (!NtUserCallMsgFilter( &msg, MSGF_SIZE ))
1508 NtUserTranslateMessage( &msg, 0 );
1509 NtUserDispatchMessage( &msg );
1513 if (!(xstate & (Button1Mask << (button - 1)))) break;
1514 NtUserMsgWaitForMultipleObjectsEx( 0, NULL, 100, QS_ALLINPUT, 0 );
1517 TRACE( "hwnd %p/%lx done\n", hwnd, win );
1518 send_message( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1522 /***********************************************************************
1523 * X11DRV_ButtonPress
1525 BOOL X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1527 XButtonEvent *event = &xev->xbutton;
1528 int buttonNum = event->button - 1;
1529 INPUT input;
1531 if (buttonNum >= NB_BUTTONS) return FALSE;
1533 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1535 input.mi.dx = event->x;
1536 input.mi.dy = event->y;
1537 input.mi.mouseData = button_down_data[buttonNum];
1538 input.mi.dwFlags = button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1539 input.mi.time = EVENT_x11_time_to_win32_time( event->time );
1540 input.mi.dwExtraInfo = 0;
1542 update_user_time( event->time );
1543 map_event_coords( hwnd, event->window, event->root, event->x_root, event->y_root, &input );
1544 send_mouse_input( hwnd, event->window, event->state, &input );
1545 return TRUE;
1549 /***********************************************************************
1550 * X11DRV_ButtonRelease
1552 BOOL X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1554 XButtonEvent *event = &xev->xbutton;
1555 int buttonNum = event->button - 1;
1556 INPUT input;
1558 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return FALSE;
1560 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1562 input.mi.dx = event->x;
1563 input.mi.dy = event->y;
1564 input.mi.mouseData = button_up_data[buttonNum];
1565 input.mi.dwFlags = button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1566 input.mi.time = EVENT_x11_time_to_win32_time( event->time );
1567 input.mi.dwExtraInfo = 0;
1569 map_event_coords( hwnd, event->window, event->root, event->x_root, event->y_root, &input );
1570 send_mouse_input( hwnd, event->window, event->state, &input );
1571 return TRUE;
1575 /***********************************************************************
1576 * X11DRV_MotionNotify
1578 BOOL X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1580 XMotionEvent *event = &xev->xmotion;
1581 INPUT input;
1583 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d serial %lu\n",
1584 hwnd, event->window, event->x, event->y, event->is_hint, event->serial );
1586 input.mi.dx = event->x;
1587 input.mi.dy = event->y;
1588 input.mi.mouseData = 0;
1589 input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1590 input.mi.time = EVENT_x11_time_to_win32_time( event->time );
1591 input.mi.dwExtraInfo = 0;
1593 if (!hwnd && is_old_motion_event( event->serial ))
1595 TRACE( "pos %d,%d old serial %lu, ignoring\n", event->x, event->y, event->serial );
1596 return FALSE;
1598 map_event_coords( hwnd, event->window, event->root, event->x_root, event->y_root, &input );
1599 send_mouse_input( hwnd, event->window, event->state, &input );
1600 return TRUE;
1604 /***********************************************************************
1605 * X11DRV_EnterNotify
1607 BOOL X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1609 XCrossingEvent *event = &xev->xcrossing;
1610 INPUT input;
1612 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd, event->window, event->x, event->y, event->detail );
1614 if (hwnd == x11drv_thread_data()->grab_hwnd) return FALSE;
1616 /* simulate a mouse motion event */
1617 input.mi.dx = event->x;
1618 input.mi.dy = event->y;
1619 input.mi.mouseData = 0;
1620 input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1621 input.mi.time = EVENT_x11_time_to_win32_time( event->time );
1622 input.mi.dwExtraInfo = 0;
1624 if (is_old_motion_event( event->serial ))
1626 TRACE( "pos %d,%d old serial %lu, ignoring\n", event->x, event->y, event->serial );
1627 return FALSE;
1629 map_event_coords( hwnd, event->window, event->root, event->x_root, event->y_root, &input );
1630 send_mouse_input( hwnd, event->window, event->state, &input );
1631 return TRUE;
1634 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1636 /***********************************************************************
1637 * X11DRV_DeviceChanged
1639 static BOOL X11DRV_DeviceChanged( XGenericEventCookie *xev )
1641 XIDeviceChangedEvent *event = xev->data;
1642 struct x11drv_thread_data *data = x11drv_thread_data();
1644 if (event->deviceid != data->xi2_core_pointer) return FALSE;
1645 if (event->reason != XISlaveSwitch) return FALSE;
1647 update_relative_valuators( event->classes, event->num_classes );
1648 data->xi2_current_slave = event->sourceid;
1649 return TRUE;
1652 static BOOL map_raw_event_coords( XIRawEvent *event, INPUT *input )
1654 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1655 XIValuatorClassInfo *x = &thread_data->x_valuator, *y = &thread_data->y_valuator;
1656 double x_value = 0, y_value = 0, x_scale, y_scale;
1657 const double *values = event->valuators.values;
1658 RECT virtual_rect;
1659 int i;
1661 if (x->number < 0 || y->number < 0) return FALSE;
1662 if (!event->valuators.mask_len) return FALSE;
1663 if (thread_data->xi2_state != xi_enabled) return FALSE;
1665 /* If there is no slave currently detected, no previous motion nor device
1666 * change events were received. Look it up now on the device list in this
1667 * case.
1669 if (!thread_data->xi2_current_slave)
1671 XIDeviceInfo *devices = thread_data->xi2_devices;
1673 for (i = 0; i < thread_data->xi2_device_count; i++)
1675 if (devices[i].use != XISlavePointer) continue;
1676 if (devices[i].deviceid != event->deviceid) continue;
1677 if (devices[i].attachment != thread_data->xi2_core_pointer) continue;
1678 thread_data->xi2_current_slave = event->deviceid;
1679 break;
1682 if (event->deviceid != thread_data->xi2_current_slave) return FALSE;
1684 virtual_rect = NtUserGetVirtualScreenRect();
1686 if (x->max <= x->min) x_scale = 1;
1687 else x_scale = (virtual_rect.right - virtual_rect.left) / (x->max - x->min);
1688 if (y->max <= y->min) y_scale = 1;
1689 else y_scale = (virtual_rect.bottom - virtual_rect.top) / (y->max - y->min);
1691 for (i = 0; i <= max( x->number, y->number ); i++)
1693 if (!XIMaskIsSet( event->valuators.mask, i )) continue;
1694 if (i == x->number)
1696 x_value = *values;
1697 x->value += x_value * x_scale;
1699 if (i == y->number)
1701 y_value = *values;
1702 y->value += y_value * y_scale;
1704 values++;
1707 input->mi.dx = round( x->value );
1708 input->mi.dy = round( y->value );
1710 TRACE( "event %f,%f value %f,%f input %d,%d\n", x_value, y_value, x->value, y->value,
1711 (int)input->mi.dx, (int)input->mi.dy );
1713 x->value -= input->mi.dx;
1714 y->value -= input->mi.dy;
1716 if (!input->mi.dx && !input->mi.dy)
1718 TRACE( "accumulating motion\n" );
1719 return FALSE;
1722 return TRUE;
1725 /***********************************************************************
1726 * X11DRV_RawMotion
1728 static BOOL X11DRV_RawMotion( XGenericEventCookie *xev )
1730 XIRawEvent *event = xev->data;
1731 INPUT input;
1733 if (broken_rawevents && is_old_motion_event( xev->serial ))
1735 TRACE( "old serial %lu, ignoring\n", xev->serial );
1736 return FALSE;
1739 input.type = INPUT_MOUSE;
1740 input.mi.mouseData = 0;
1741 input.mi.dwFlags = MOUSEEVENTF_MOVE;
1742 input.mi.time = EVENT_x11_time_to_win32_time( event->time );
1743 input.mi.dwExtraInfo = 0;
1744 input.mi.dx = 0;
1745 input.mi.dy = 0;
1746 if (!map_raw_event_coords( event, &input )) return FALSE;
1748 __wine_send_input( 0, &input, NULL );
1749 return TRUE;
1752 #endif /* HAVE_X11_EXTENSIONS_XINPUT2_H */
1755 /***********************************************************************
1756 * X11DRV_XInput2_Init
1758 void X11DRV_XInput2_Init(void)
1760 #if defined(SONAME_LIBXI) && defined(HAVE_X11_EXTENSIONS_XINPUT2_H)
1761 int event, error;
1762 void *libxi_handle = dlopen( SONAME_LIBXI, RTLD_NOW );
1764 if (!libxi_handle)
1766 WARN( "couldn't load %s\n", SONAME_LIBXI );
1767 return;
1769 #define LOAD_FUNCPTR(f) \
1770 if (!(p##f = dlsym( libxi_handle, #f))) \
1772 WARN("Failed to load %s.\n", #f); \
1773 return; \
1776 LOAD_FUNCPTR(XIGetClientPointer);
1777 LOAD_FUNCPTR(XIFreeDeviceInfo);
1778 LOAD_FUNCPTR(XIQueryDevice);
1779 LOAD_FUNCPTR(XIQueryVersion);
1780 LOAD_FUNCPTR(XISelectEvents);
1781 #undef LOAD_FUNCPTR
1783 xinput2_available = XQueryExtension( gdi_display, "XInputExtension", &xinput2_opcode, &event, &error );
1785 /* Until version 1.10.4 rawinput was broken in XOrg, see
1786 * https://bugs.freedesktop.org/show_bug.cgi?id=30068 */
1787 broken_rawevents = strstr(XServerVendor( gdi_display ), "X.Org") &&
1788 XVendorRelease( gdi_display ) < 11004000;
1790 #else
1791 TRACE( "X Input 2 support not compiled in.\n" );
1792 #endif
1796 /***********************************************************************
1797 * X11DRV_GenericEvent
1799 BOOL X11DRV_GenericEvent( HWND hwnd, XEvent *xev )
1801 BOOL ret = FALSE;
1802 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1803 XGenericEventCookie *event = &xev->xcookie;
1805 if (!event->data) return FALSE;
1806 if (event->extension != xinput2_opcode) return FALSE;
1808 switch (event->evtype)
1810 case XI_DeviceChanged:
1811 ret = X11DRV_DeviceChanged( event );
1812 break;
1813 case XI_RawMotion:
1814 ret = X11DRV_RawMotion( event );
1815 break;
1817 default:
1818 TRACE( "Unhandled event %#x\n", event->evtype );
1819 break;
1821 #endif
1822 return ret;