4 * Copyright 1998 Ulrich Weigand
5 * Copyright 2007 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
26 #include <X11/cursorfont.h>
28 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
29 #include <X11/extensions/XInput2.h>
32 #ifdef SONAME_LIBXCURSOR
33 # include <X11/Xcursor/Xcursor.h>
34 static void *xcursor_handle;
35 # define MAKE_FUNCPTR(f) static typeof(f) * p##f
36 MAKE_FUNCPTR(XcursorImageCreate);
37 MAKE_FUNCPTR(XcursorImageDestroy);
38 MAKE_FUNCPTR(XcursorImageLoadCursor);
39 MAKE_FUNCPTR(XcursorImagesCreate);
40 MAKE_FUNCPTR(XcursorImagesDestroy);
41 MAKE_FUNCPTR(XcursorImagesLoadCursor);
42 MAKE_FUNCPTR(XcursorLibraryLoadCursor);
44 #endif /* SONAME_LIBXCURSOR */
46 #define NONAMELESSUNION
47 #define NONAMELESSSTRUCT
54 #include "wine/server.h"
55 #include "wine/library.h"
56 #include "wine/unicode.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
61 /**********************************************************************/
64 #define Button6Mask (1<<13)
67 #define Button7Mask (1<<14)
70 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
72 static const UINT button_down_flags[NB_BUTTONS] =
75 MOUSEEVENTF_MIDDLEDOWN,
76 MOUSEEVENTF_RIGHTDOWN,
79 MOUSEEVENTF_XDOWN, /* FIXME: horizontal wheel */
85 static const UINT button_up_flags[NB_BUTTONS] =
98 static const UINT button_down_data[NB_BUTTONS] =
111 static const UINT button_up_data[NB_BUTTONS] =
124 static HWND cursor_window;
125 static HCURSOR last_cursor;
126 static DWORD last_cursor_change;
127 static XContext cursor_context;
128 static RECT clip_rect;
129 static Cursor create_cursor( HANDLE handle );
131 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
132 static BOOL xinput2_available;
133 static int xinput2_core_pointer;
134 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
135 MAKE_FUNCPTR(XIFreeDeviceInfo);
136 MAKE_FUNCPTR(XIQueryDevice);
137 MAKE_FUNCPTR(XIQueryVersion);
138 MAKE_FUNCPTR(XISelectEvents);
142 /***********************************************************************
143 * X11DRV_Xcursor_Init
145 * Load the Xcursor library for use.
147 void X11DRV_Xcursor_Init(void)
149 #ifdef SONAME_LIBXCURSOR
150 xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
151 if (!xcursor_handle) /* wine_dlopen failed. */
153 WARN("Xcursor failed to load. Using fallback code.\n");
156 #define LOAD_FUNCPTR(f) \
157 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
159 LOAD_FUNCPTR(XcursorImageCreate);
160 LOAD_FUNCPTR(XcursorImageDestroy);
161 LOAD_FUNCPTR(XcursorImageLoadCursor);
162 LOAD_FUNCPTR(XcursorImagesCreate);
163 LOAD_FUNCPTR(XcursorImagesDestroy);
164 LOAD_FUNCPTR(XcursorImagesLoadCursor);
165 LOAD_FUNCPTR(XcursorLibraryLoadCursor);
167 #endif /* SONAME_LIBXCURSOR */
171 /***********************************************************************
174 static Cursor get_empty_cursor(void)
176 static Cursor cursor;
177 static const char data[] = { 0 };
185 bg.red = bg.green = bg.blue = 0x0000;
186 pixmap = XCreateBitmapFromData( gdi_display, root_window, data, 1, 1 );
189 cursor = XCreatePixmapCursor( gdi_display, pixmap, pixmap, &bg, &bg, 0, 0 );
190 XFreePixmap( gdi_display, pixmap );
197 /***********************************************************************
200 void set_window_cursor( Window window, HCURSOR handle )
205 if (!handle) cursor = get_empty_cursor();
206 else if (!cursor_context || XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
208 /* try to create it */
210 if (!(cursor = create_cursor( handle ))) return;
213 if (!cursor_context) cursor_context = XUniqueContext();
214 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&prev ))
216 /* someone else was here first */
217 XFreeCursor( gdi_display, cursor );
222 XSaveContext( gdi_display, (XID)handle, cursor_context, (char *)cursor );
223 TRACE( "cursor %p created %lx\n", handle, cursor );
227 XDefineCursor( gdi_display, window, cursor );
228 /* make the change take effect immediately */
229 XFlush( gdi_display );
233 /***********************************************************************
236 void sync_window_cursor( Window window )
240 SERVER_START_REQ( set_cursor )
243 wine_server_call( req );
244 cursor = reply->prev_count >= 0 ? wine_server_ptr_handle( reply->prev_handle ) : 0;
248 set_window_cursor( window, cursor );
251 /***********************************************************************
254 static void enable_xinput2(void)
256 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
257 struct x11drv_thread_data *data = x11drv_thread_data();
258 XIDeviceInfo *devices;
260 unsigned char mask_bits[XIMaskLen(XI_LASTEVENT)];
263 if (!xinput2_available) return;
265 if (data->xi2_state == xi_unknown)
267 int major = 2, minor = 0;
269 if (!pXIQueryVersion( data->display, &major, &minor )) data->xi2_state = xi_disabled;
272 data->xi2_state = xi_unavailable;
273 WARN( "X Input 2 not available\n" );
277 if (data->xi2_state == xi_unavailable) return;
280 devices = pXIQueryDevice( data->display, XIAllDevices, &count );
281 for (i = 0; i < count; ++i)
283 if (devices[i].use != XIMasterPointer) continue;
284 TRACE( "Using %u (%s) as core pointer\n",
285 devices[i].deviceid, debugstr_a(devices[i].name) );
286 xinput2_core_pointer = devices[i].deviceid;
290 mask.mask = mask_bits;
291 mask.mask_len = sizeof(mask_bits);
292 memset( mask_bits, 0, sizeof(mask_bits) );
294 XISetMask( mask_bits, XI_RawButtonPress );
295 XISetMask( mask_bits, XI_RawButtonRelease );
296 XISetMask( mask_bits, XI_RawMotion );
298 for (i = 0; i < count; ++i)
300 if (devices[i].use == XISlavePointer && devices[i].attachment == xinput2_core_pointer)
302 TRACE( "Device %u (%s) is attached to the core pointer\n",
303 devices[i].deviceid, debugstr_a(devices[i].name) );
304 mask.deviceid = devices[i].deviceid;
305 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
306 data->xi2_state = xi_enabled;
310 pXIFreeDeviceInfo( devices );
315 /***********************************************************************
318 static void disable_xinput2(void)
320 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
321 struct x11drv_thread_data *data = x11drv_thread_data();
323 XIDeviceInfo *devices;
326 if (data->xi2_state != xi_enabled) return;
328 TRACE( "disabling\n" );
329 data->xi2_state = xi_disabled;
335 devices = pXIQueryDevice( data->display, XIAllDevices, &count );
336 for (i = 0; i < count; ++i)
338 if (devices[i].use == XISlavePointer && devices[i].attachment == xinput2_core_pointer)
340 mask.deviceid = devices[i].deviceid;
341 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
344 pXIFreeDeviceInfo( devices );
349 /***********************************************************************
350 * create_clipping_msg_window
352 static HWND create_clipping_msg_window(void)
354 static const WCHAR class_name[] = {'_','_','x','1','1','d','r','v','_','c','l','i','p','_','c','l','a','s','s',0};
355 static ATOM clip_class;
362 memset( &class, 0, sizeof(class) );
363 class.lpfnWndProc = DefWindowProcW;
364 class.hInstance = GetModuleHandleW(0);
365 class.lpszClassName = class_name;
366 if ((atom = RegisterClassW( &class ))) clip_class = atom;
368 return CreateWindowW( class_name, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, GetModuleHandleW(0), NULL );
371 /***********************************************************************
372 * grab_clipping_window
374 * Start a pointer grab on the clip window.
376 static BOOL grab_clipping_window( const RECT *clip )
378 struct x11drv_thread_data *data = x11drv_thread_data();
382 if (!data) return FALSE;
383 if (!(clip_window = init_clip_window())) return TRUE;
385 /* create a clip message window unless we are already clipping */
386 if (!data->clip_hwnd)
388 if (!(msg_hwnd = create_clipping_msg_window())) return TRUE;
392 /* don't clip to 1x1 rectangle if we don't have XInput */
393 if (data->xi2_state != xi_enabled && clip->right - clip->left == 1 && clip->bottom - clip->top == 1)
395 WARN( "XInput2 not supported, refusing to clip to %s\n", wine_dbgstr_rect(clip) );
396 if (msg_hwnd) DestroyWindow( msg_hwnd );
401 TRACE( "clipping to %s\n", wine_dbgstr_rect(clip) );
404 if (msg_hwnd) XUnmapWindow( data->display, clip_window );
405 XMoveResizeWindow( data->display, clip_window,
406 clip->left - virtual_screen_rect.left, clip->top - virtual_screen_rect.top,
407 clip->right - clip->left, clip->bottom - clip->top );
408 if (msg_hwnd) XMapWindow( data->display, clip_window );
409 if (!XGrabPointer( data->display, clip_window, False,
410 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
411 GrabModeAsync, GrabModeAsync, clip_window, None, CurrentTime ))
415 if (!clipping_cursor)
418 if (msg_hwnd) DestroyWindow( msg_hwnd );
423 data->clip_hwnd = msg_hwnd;
424 sync_window_cursor( clip_window );
426 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, (LPARAM)msg_hwnd );
431 /***********************************************************************
432 * ungrab_clipping_window
434 * Release the pointer grab on the clip window.
436 void ungrab_clipping_window(void)
438 Display *display = thread_init_display();
439 Window clip_window = init_clip_window();
441 if (!clip_window) return;
443 TRACE( "no longer clipping\n" );
445 XUnmapWindow( display, clip_window );
448 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, 0 );
451 /***********************************************************************
454 * Notification function called upon receiving a WM_X11DRV_CLIP_CURSOR.
456 LRESULT clip_cursor_notify( HWND hwnd, HWND new_clip_hwnd )
458 struct x11drv_thread_data *data = x11drv_thread_data();
460 if (hwnd == GetDesktopWindow()) /* change the clip window stored in the desktop process */
462 static HWND clip_hwnd;
464 HWND prev = clip_hwnd;
465 clip_hwnd = new_clip_hwnd;
466 if (prev || new_clip_hwnd) TRACE( "clip hwnd changed from %p to %p\n", prev, new_clip_hwnd );
467 if (prev) SendNotifyMessageW( prev, WM_X11DRV_CLIP_CURSOR, 0, 0 );
469 else if (hwnd == data->clip_hwnd) /* this is a notification that clipping has been reset */
473 DestroyWindow( hwnd );
475 else if (hwnd == GetForegroundWindow()) /* request to clip */
479 GetClipCursor( &clip );
480 if (clip.left > virtual_screen_rect.left || clip.right < virtual_screen_rect.right ||
481 clip.top > virtual_screen_rect.top || clip.bottom < virtual_screen_rect.bottom)
482 return grab_clipping_window( &clip );
487 /***********************************************************************
490 * Update the various window states on a mouse event.
492 static void send_mouse_input( HWND hwnd, Window window, unsigned int state, INPUT *input )
494 struct x11drv_win_data *data;
497 input->type = INPUT_MOUSE;
499 if (!hwnd && window == x11drv_thread_data()->clip_window)
501 input->u.mi.dx += clip_rect.left;
502 input->u.mi.dy += clip_rect.top;
503 if (x11drv_thread_data()->xi2_state != xi_enabled) __wine_send_input( hwnd, input );
507 if (!(data = X11DRV_get_win_data( hwnd ))) return;
509 if (window == data->whole_window)
511 input->u.mi.dx += data->whole_rect.left - data->client_rect.left;
512 input->u.mi.dy += data->whole_rect.top - data->client_rect.top;
514 if (window == root_window)
516 input->u.mi.dx += virtual_screen_rect.left;
517 input->u.mi.dy += virtual_screen_rect.top;
519 pt.x = input->u.mi.dx;
520 pt.y = input->u.mi.dy;
521 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
522 pt.x = data->client_rect.right - data->client_rect.left - 1 - pt.x;
523 MapWindowPoints( hwnd, 0, &pt, 1 );
525 if (InterlockedExchangePointer( (void **)&cursor_window, hwnd ) != hwnd ||
526 GetTickCount() - last_cursor_change > 100)
528 sync_window_cursor( data->whole_window );
529 last_cursor_change = GetTickCount();
532 if (hwnd != GetDesktopWindow()) hwnd = GetAncestor( hwnd, GA_ROOT );
534 /* update the wine server Z-order */
536 if (window != x11drv_thread_data()->grab_window &&
537 /* ignore event if a button is pressed, since the mouse is then grabbed too */
538 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
541 SetRect( &rect, pt.x, pt.y, pt.x + 1, pt.y + 1 );
542 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
544 SERVER_START_REQ( update_window_zorder )
546 req->window = wine_server_user_handle( hwnd );
547 req->rect.left = rect.left;
548 req->rect.top = rect.top;
549 req->rect.right = rect.right;
550 req->rect.bottom = rect.bottom;
551 wine_server_call( req );
556 input->u.mi.dx = pt.x;
557 input->u.mi.dy = pt.y;
558 __wine_send_input( hwnd, input );
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, has_alpha = FALSE;
579 image = pXcursorImageCreate( width, height );
583 ERR("X11 failed to produce a cursor frame!\n");
587 image->xhot = iinfo->xHotspot;
588 image->yhot = iinfo->yHotspot;
590 image->delay = 100; /* fallback delay, 100 ms */
591 if (GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, istep, &delay_jiffies, &num_steps) != 0)
592 image->delay = (100 * delay_jiffies) / 6; /* convert jiffies (1/60s) to milliseconds */
594 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep);
596 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
597 memset( color_bits, 0x00, color_size );
598 SelectObject( hdc, hbmColor );
599 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
601 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
604 memcpy( image->pixels, color_bits, color_size );
606 /* check if the cursor frame was drawn with an alpha channel */
607 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
608 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
610 /* if no alpha channel was drawn then generate it from the mask */
613 unsigned int width_bytes = (width + 31) / 32 * 4;
615 /* draw the cursor mask to a temporary buffer */
616 memset( mask_bits, 0xFF, mask_size );
617 SelectObject( hdc, hbmMask );
618 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
620 ERR("Failed to draw frame mask %d.\n", istep);
623 /* use the buffer to directly modify the XcursorImage alpha channel */
624 for (y = 0, ptr = image->pixels; y < height; y++)
625 for (x = 0; x < width; x++, ptr++)
626 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
632 if (ret == NULL) pXcursorImageDestroy( image );
636 /***********************************************************************
637 * create_xcursor_cursor
639 * Use Xcursor to create an X cursor from a Windows one.
641 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
643 unsigned char *color_bits, *mask_bits;
644 HBITMAP hbmColor = 0, hbmMask = 0;
645 DWORD nFrames, delay_jiffies, i;
646 int color_size, mask_size;
647 BITMAPINFO *info = NULL;
648 XcursorImages *images;
652 /* Retrieve the number of frames to render */
653 if (!GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, 0, &delay_jiffies, &nFrames)) return 0;
654 if (!(imgs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XcursorImage*)*nFrames ))) return 0;
656 /* Allocate all of the resources necessary to obtain a cursor frame */
657 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
658 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
659 info->bmiHeader.biWidth = width;
660 info->bmiHeader.biHeight = -height;
661 info->bmiHeader.biPlanes = 1;
662 info->bmiHeader.biCompression = BI_RGB;
663 info->bmiHeader.biXPelsPerMeter = 0;
664 info->bmiHeader.biYPelsPerMeter = 0;
665 info->bmiHeader.biClrUsed = 0;
666 info->bmiHeader.biClrImportant = 0;
667 info->bmiHeader.biBitCount = 32;
668 color_size = width * height * 4;
669 info->bmiHeader.biSizeImage = color_size;
670 hbmColor = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &color_bits, NULL, 0);
673 ERR("Failed to create DIB section for cursor color data!\n");
676 info->bmiHeader.biBitCount = 1;
677 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
678 info->bmiHeader.biSizeImage = mask_size;
679 hbmMask = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &mask_bits, NULL, 0);
682 ERR("Failed to create DIB section for cursor mask data!\n");
686 /* Create an XcursorImage for each frame of the cursor */
687 for (i=0; i<nFrames; i++)
689 imgs[i] = create_xcursor_frame( hdc, iinfo, icon,
690 hbmColor, color_bits, color_size,
691 hbmMask, mask_bits, mask_size,
693 if (!imgs[i]) goto cleanup;
696 /* Build an X cursor out of all of the frames */
697 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
698 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
699 images->images[images->nimage] = imgs[images->nimage];
701 cursor = pXcursorImagesLoadCursor( gdi_display, images );
703 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
704 HeapFree( GetProcessHeap(), 0, imgs );
710 /* Failed to produce a cursor, free previously allocated frames */
711 for (i=0; i<nFrames && imgs[i]; i++)
712 pXcursorImageDestroy( imgs[i] );
713 HeapFree( GetProcessHeap(), 0, imgs );
715 /* Cleanup all of the resources used to obtain the frame data */
716 if (hbmColor) DeleteObject( hbmColor );
717 if (hbmMask) DeleteObject( hbmMask );
718 HeapFree( GetProcessHeap(), 0, info );
723 struct system_cursors
729 static const struct system_cursors user32_cursors[] =
731 { OCR_NORMAL, "left_ptr" },
732 { OCR_IBEAM, "xterm" },
733 { OCR_WAIT, "watch" },
734 { OCR_CROSS, "cross" },
735 { OCR_UP, "center_ptr" },
736 { OCR_SIZE, "fleur" },
737 { OCR_SIZEALL, "fleur" },
738 { OCR_ICON, "icon" },
739 { OCR_SIZENWSE, "nwse-resize" },
740 { OCR_SIZENESW, "nesw-resize" },
741 { OCR_SIZEWE, "ew-resize" },
742 { OCR_SIZENS, "ns-resize" },
743 { OCR_NO, "not-allowed" },
744 { OCR_HAND, "hand2" },
745 { OCR_APPSTARTING, "left_ptr_watch" },
746 { OCR_HELP, "question_arrow" },
750 static const struct system_cursors comctl32_cursors[] =
755 { 106, "row-resize" },
756 { 107, "row-resize" },
758 { 135, "col-resize" },
762 static const struct system_cursors ole32_cursors[] =
771 static const struct system_cursors riched20_cursors[] =
774 { 107, "right_ptr" },
783 const struct system_cursors *cursors;
787 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
788 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
789 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
790 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
793 /***********************************************************************
794 * create_xcursor_system_cursor
796 * Create an X cursor for a system cursor.
798 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
800 static const WCHAR idW[] = {'%','h','u',0};
801 const struct system_cursors *cursors;
806 WCHAR *p, name[MAX_PATH * 2], valueW[64];
810 if (!pXcursorLibraryLoadCursor) return 0;
811 if (!info->szModName[0]) return 0;
813 p = strrchrW( info->szModName, '\\' );
814 strcpyW( name, p ? p + 1 : info->szModName );
815 p = name + strlenW( name );
817 if (info->szResName[0]) strcpyW( p, info->szResName );
818 else sprintfW( p, idW, info->wResID );
821 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
822 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver\\Cursors", &key ))
824 size = sizeof(valueW) / sizeof(WCHAR);
825 ret = RegQueryValueExW( key, name, NULL, NULL, (BYTE *)valueW, &size );
829 if (!valueW[0]) return 0; /* force standard cursor */
830 if (!WideCharToMultiByte( CP_UNIXCP, 0, valueW, -1, valueA, sizeof(valueA), NULL, NULL ))
836 if (info->szResName[0]) goto done; /* only integer resources are supported here */
837 if (!(module = GetModuleHandleW( info->szModName ))) goto done;
839 for (i = 0; i < sizeof(module_cursors)/sizeof(module_cursors[0]); i++)
840 if (GetModuleHandleW( module_cursors[i].name ) == module) break;
841 if (i == sizeof(module_cursors)/sizeof(module_cursors[0])) goto done;
843 cursors = module_cursors[i].cursors;
844 for (i = 0; cursors[i].id; i++)
845 if (cursors[i].id == info->wResID)
847 strcpy( valueA, cursors[i].name );
855 cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
857 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
858 debugstr_w(name), debugstr_a(valueA) );
860 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
864 #endif /* SONAME_LIBXCURSOR */
867 /***********************************************************************
868 * create_cursor_from_bitmaps
870 * Create an X11 cursor from source bitmaps.
872 static Cursor create_cursor_from_bitmaps( HBITMAP src_xor, HBITMAP src_and, int width, int height,
873 int xor_y, int and_y, XColor *fg, XColor *bg,
874 int hotspot_x, int hotspot_y )
876 HDC src = 0, dst = 0;
877 HBITMAP bits = 0, mask = 0, mask_inv = 0;
880 if (!(src = CreateCompatibleDC( 0 ))) goto done;
881 if (!(dst = CreateCompatibleDC( 0 ))) goto done;
883 if (!(bits = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
884 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
885 if (!(mask_inv = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
887 /* We have to do some magic here, as cursors are not fully
888 * compatible between Windows and X11. Under X11, there are
889 * only 3 possible color cursor: black, white and masked. So
890 * we map the 4th Windows color (invert the bits on the screen)
891 * to black and an additional white bit on an other place
892 * (+1,+1). This require some boolean arithmetic:
895 * And Xor Result | Bits Mask Result
896 * 0 0 black | 0 1 background
897 * 0 1 white | 1 1 foreground
898 * 1 0 no change | X 0 no change
899 * 1 1 inverted | 0 1 background
902 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
903 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
905 SelectObject( src, src_and );
906 SelectObject( dst, bits );
907 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
908 SelectObject( dst, mask );
909 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
910 SelectObject( dst, mask_inv );
911 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
912 SelectObject( src, src_xor );
913 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, SRCAND /* src & dst */ );
914 SelectObject( dst, bits );
915 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, SRCERASE /* src & ~dst */ );
916 SelectObject( dst, mask );
917 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, 0xdd0228 /* src | ~dst */ );
918 /* additional white */
919 SelectObject( src, mask_inv );
920 BitBlt( dst, 1, 1, width, height, src, 0, 0, SRCPAINT /* src | dst */);
921 SelectObject( dst, bits );
922 BitBlt( dst, 1, 1, width, height, src, 0, 0, SRCPAINT /* src | dst */ );
925 cursor = XCreatePixmapCursor( gdi_display, X11DRV_get_pixmap(bits), X11DRV_get_pixmap(mask),
926 fg, bg, hotspot_x, hotspot_y );
932 DeleteObject( bits );
933 DeleteObject( mask );
934 DeleteObject( mask_inv );
938 /***********************************************************************
941 * Create an X cursor from a Windows one.
943 static Cursor create_xlib_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
946 Cursor cursor = None;
947 HBITMAP xor_bitmap = 0;
949 unsigned int *color_bits = NULL, *ptr;
950 unsigned char *mask_bits = NULL, *xor_bits = NULL;
951 int i, x, y, has_alpha = 0;
952 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
953 unsigned int width_bytes = (width + 31) / 32 * 4;
955 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] ))))
957 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
958 info->bmiHeader.biWidth = width;
959 info->bmiHeader.biHeight = -height;
960 info->bmiHeader.biPlanes = 1;
961 info->bmiHeader.biBitCount = 1;
962 info->bmiHeader.biCompression = BI_RGB;
963 info->bmiHeader.biSizeImage = width_bytes * height;
964 info->bmiHeader.biXPelsPerMeter = 0;
965 info->bmiHeader.biYPelsPerMeter = 0;
966 info->bmiHeader.biClrUsed = 0;
967 info->bmiHeader.biClrImportant = 0;
969 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
970 if (!GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS )) goto done;
972 info->bmiHeader.biBitCount = 32;
973 info->bmiHeader.biSizeImage = width * height * 4;
974 if (!(color_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
975 if (!(xor_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, width_bytes * height ))) goto done;
976 GetDIBits( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS );
978 /* compute fg/bg color and xor bitmap based on average of the color values */
980 if (!(xor_bitmap = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
981 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
982 for (y = 0, ptr = color_bits; y < height; y++)
984 for (x = 0; x < width; x++, ptr++)
986 int red = (*ptr >> 16) & 0xff;
987 int green = (*ptr >> 8) & 0xff;
988 int blue = (*ptr >> 0) & 0xff;
989 if (red + green + blue > 0x40)
995 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1007 fg.red = rfg * 257 / fgBits;
1008 fg.green = gfg * 257 / fgBits;
1009 fg.blue = bfg * 257 / fgBits;
1011 else fg.red = fg.green = fg.blue = 0;
1012 bgBits = width * height - fgBits;
1015 bg.red = rbg * 257 / bgBits;
1016 bg.green = gbg * 257 / bgBits;
1017 bg.blue = bbg * 257 / bgBits;
1019 else bg.red = bg.green = bg.blue = 0;
1021 info->bmiHeader.biBitCount = 1;
1022 info->bmiHeader.biSizeImage = width_bytes * height;
1023 SetDIBits( hdc, xor_bitmap, 0, height, xor_bits, info, DIB_RGB_COLORS );
1025 /* generate mask from the alpha channel if we have one */
1027 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
1028 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
1032 memset( mask_bits, 0, width_bytes * height );
1033 for (y = 0, ptr = color_bits; y < height; y++)
1034 for (x = 0; x < width; x++, ptr++)
1035 if ((*ptr >> 24) > 25) /* more than 10% alpha */
1036 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1038 info->bmiHeader.biBitCount = 1;
1039 info->bmiHeader.biSizeImage = width_bytes * height;
1040 SetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS );
1043 cursor = XCreatePixmapCursor( gdi_display,
1044 X11DRV_get_pixmap(xor_bitmap),
1045 X11DRV_get_pixmap(icon->hbmMask),
1046 &fg, &bg, icon->xHotspot, icon->yHotspot );
1047 wine_tsx11_unlock();
1051 cursor = create_cursor_from_bitmaps( xor_bitmap, icon->hbmMask, width, height, 0, 0,
1052 &fg, &bg, icon->xHotspot, icon->yHotspot );
1056 DeleteObject( xor_bitmap );
1057 HeapFree( GetProcessHeap(), 0, info );
1058 HeapFree( GetProcessHeap(), 0, color_bits );
1059 HeapFree( GetProcessHeap(), 0, xor_bits );
1060 HeapFree( GetProcessHeap(), 0, mask_bits );
1064 /***********************************************************************
1067 * Create an X cursor from a Windows one.
1069 static Cursor create_cursor( HANDLE handle )
1075 if (!handle) return get_empty_cursor();
1077 info.cbSize = sizeof(info);
1078 if (!GetIconInfoExW( handle, &info )) return 0;
1080 #ifdef SONAME_LIBXCURSOR
1081 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
1083 DeleteObject( info.hbmColor );
1084 DeleteObject( info.hbmMask );
1089 GetObjectW( info.hbmMask, sizeof(bm), &bm );
1090 if (!info.hbmColor) bm.bmHeight /= 2;
1092 /* make sure hotspot is valid */
1093 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1095 info.xHotspot = bm.bmWidth / 2;
1096 info.yHotspot = bm.bmHeight / 2;
1101 HDC hdc = CreateCompatibleDC( 0 );
1104 #ifdef SONAME_LIBXCURSOR
1105 if (pXcursorImagesLoadCursor)
1106 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
1108 if (!cursor) cursor = create_xlib_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1110 DeleteObject( info.hbmColor );
1116 fg.red = fg.green = fg.blue = 0xffff;
1117 bg.red = bg.green = bg.blue = 0;
1118 cursor = create_cursor_from_bitmaps( info.hbmMask, info.hbmMask, bm.bmWidth, bm.bmHeight,
1119 bm.bmHeight, 0, &fg, &bg, info.xHotspot, info.yHotspot );
1122 DeleteObject( info.hbmMask );
1126 /***********************************************************************
1127 * DestroyCursorIcon (X11DRV.@)
1129 void CDECL X11DRV_DestroyCursorIcon( HCURSOR handle )
1134 if (cursor_context && !XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1136 TRACE( "%p xid %lx\n", handle, cursor );
1137 XFreeCursor( gdi_display, cursor );
1138 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1140 wine_tsx11_unlock();
1143 /***********************************************************************
1144 * SetCursor (X11DRV.@)
1146 void CDECL X11DRV_SetCursor( HCURSOR handle )
1148 if (InterlockedExchangePointer( (void **)&last_cursor, handle ) != handle ||
1149 GetTickCount() - last_cursor_change > 100)
1151 last_cursor_change = GetTickCount();
1152 if (clipping_cursor) set_window_cursor( init_clip_window(), handle );
1153 else if (cursor_window) SendNotifyMessageW( cursor_window, WM_X11DRV_SET_CURSOR, 0, (LPARAM)handle );
1157 /***********************************************************************
1158 * SetCursorPos (X11DRV.@)
1160 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
1162 struct x11drv_thread_data *data = x11drv_init_thread_data();
1164 if (data->xi2_state == xi_enabled) return TRUE;
1166 TRACE( "warping to (%d,%d)\n", x, y );
1169 XWarpPointer( data->display, root_window, root_window, 0, 0, 0, 0,
1170 x - virtual_screen_rect.left, y - virtual_screen_rect.top );
1171 XFlush( data->display ); /* avoids bad mouse lag in games that do their own mouse warping */
1172 wine_tsx11_unlock();
1176 /***********************************************************************
1177 * GetCursorPos (X11DRV.@)
1179 BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
1181 Display *display = thread_init_display();
1183 int rootX, rootY, winX, winY;
1184 unsigned int xstate;
1188 ret = XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &winX, &winY, &xstate );
1191 pos->x = winX + virtual_screen_rect.left;
1192 pos->y = winY + virtual_screen_rect.top;
1193 TRACE("pointer at (%d,%d)\n", pos->x, pos->y );
1195 wine_tsx11_unlock();
1199 /***********************************************************************
1200 * ClipCursor (X11DRV.@)
1202 BOOL CDECL X11DRV_ClipCursor( LPCRECT clip )
1206 ungrab_clipping_window();
1210 if (GetWindowThreadProcessId( GetDesktopWindow(), NULL ) == GetCurrentThreadId())
1211 return TRUE; /* don't clip in the desktop process */
1213 /* we are clipping if the clip rectangle is smaller than the screen */
1214 if (grab_pointer && (clip->left > virtual_screen_rect.left ||
1215 clip->right < virtual_screen_rect.right ||
1216 clip->top > virtual_screen_rect.top ||
1217 clip->bottom < virtual_screen_rect.bottom))
1220 HWND foreground = GetForegroundWindow();
1222 /* forward request to the foreground window if it's in a different thread */
1223 tid = GetWindowThreadProcessId( foreground, &pid );
1224 if (tid && tid != GetCurrentThreadId() && pid == GetCurrentProcessId())
1226 TRACE( "forwarding clip request to %p\n", foreground );
1227 if (SendMessageW( foreground, WM_X11DRV_CLIP_CURSOR, 0, 0 )) return TRUE;
1229 else if (grab_clipping_window( clip )) return TRUE;
1232 ungrab_clipping_window();
1236 /***********************************************************************
1237 * X11DRV_ButtonPress
1239 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1241 XButtonEvent *event = &xev->xbutton;
1242 int buttonNum = event->button - 1;
1245 if (buttonNum >= NB_BUTTONS) return;
1247 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1249 input.u.mi.dx = event->x;
1250 input.u.mi.dy = event->y;
1251 input.u.mi.mouseData = button_down_data[buttonNum];
1252 input.u.mi.dwFlags = button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1253 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1254 input.u.mi.dwExtraInfo = 0;
1256 update_user_time( event->time );
1257 send_mouse_input( hwnd, event->window, event->state, &input );
1261 /***********************************************************************
1262 * X11DRV_ButtonRelease
1264 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1266 XButtonEvent *event = &xev->xbutton;
1267 int buttonNum = event->button - 1;
1270 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1272 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1274 input.u.mi.dx = event->x;
1275 input.u.mi.dy = event->y;
1276 input.u.mi.mouseData = button_up_data[buttonNum];
1277 input.u.mi.dwFlags = button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1278 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1279 input.u.mi.dwExtraInfo = 0;
1281 send_mouse_input( hwnd, event->window, event->state, &input );
1285 /***********************************************************************
1286 * X11DRV_MotionNotify
1288 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1290 XMotionEvent *event = &xev->xmotion;
1293 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d\n", hwnd, event->window, event->x, event->y, event->is_hint );
1295 input.u.mi.dx = event->x;
1296 input.u.mi.dy = event->y;
1297 input.u.mi.mouseData = 0;
1298 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1299 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1300 input.u.mi.dwExtraInfo = 0;
1302 send_mouse_input( hwnd, event->window, event->state, &input );
1306 /***********************************************************************
1307 * X11DRV_EnterNotify
1309 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1311 XCrossingEvent *event = &xev->xcrossing;
1314 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd, event->window, event->x, event->y, event->detail );
1316 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1317 if (event->window == x11drv_thread_data()->grab_window) return;
1319 /* simulate a mouse motion event */
1320 input.u.mi.dx = event->x;
1321 input.u.mi.dy = event->y;
1322 input.u.mi.mouseData = 0;
1323 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1324 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1325 input.u.mi.dwExtraInfo = 0;
1327 send_mouse_input( hwnd, event->window, event->state, &input );
1330 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1332 /***********************************************************************
1333 * X11DRV_RawButtonPress
1335 static void X11DRV_RawButtonPress( XIRawEvent *event )
1337 int button = event->detail - 1;
1340 if (button >= NB_BUTTONS) return;
1342 TRACE( "button %u\n", button );
1344 input.type = INPUT_MOUSE;
1347 input.u.mi.mouseData = button_down_data[button];
1348 input.u.mi.dwFlags = button_down_flags[button];
1349 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1350 input.u.mi.dwExtraInfo = 0;
1352 update_user_time( event->time );
1353 input.type = INPUT_MOUSE;
1354 __wine_send_input( 0, &input );
1358 /***********************************************************************
1359 * X11DRV_RawButtonRelease
1361 static void X11DRV_RawButtonRelease( XIRawEvent *event )
1363 int button = event->detail - 1;
1366 if (button >= NB_BUTTONS) return;
1368 TRACE( "button %u\n", button );
1372 input.u.mi.mouseData = button_up_data[button];
1373 input.u.mi.dwFlags = button_up_flags[button];
1374 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1375 input.u.mi.dwExtraInfo = 0;
1377 input.type = INPUT_MOUSE;
1378 __wine_send_input( 0, &input );
1382 /***********************************************************************
1385 static void X11DRV_RawMotion( XIRawEvent *event )
1387 const double *values = event->valuators.values;
1390 if (!event->valuators.mask_len) return;
1394 input.u.mi.mouseData = 0;
1395 input.u.mi.dwFlags = MOUSEEVENTF_MOVE;
1396 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1397 input.u.mi.dwExtraInfo = 0;
1399 if (XIMaskIsSet( event->valuators.mask, 0 )) input.u.mi.dx = *values++;
1400 if (XIMaskIsSet( event->valuators.mask, 1 )) input.u.mi.dy = *values++;
1402 TRACE( "pos %d,%d\n", input.u.mi.dx, input.u.mi.dy );
1404 input.type = INPUT_MOUSE;
1405 __wine_send_input( 0, &input );
1408 #endif /* HAVE_X11_EXTENSIONS_XINPUT2_H */
1411 /***********************************************************************
1412 * X11DRV_XInput2_Init
1414 void X11DRV_XInput2_Init(void)
1416 #if defined(SONAME_LIBXI) && defined(HAVE_X11_EXTENSIONS_XINPUT2_H)
1418 void *libxi_handle = wine_dlopen( SONAME_LIBXI, RTLD_NOW, NULL, 0 );
1422 WARN( "couldn't load %s\n", SONAME_LIBXI );
1425 #define LOAD_FUNCPTR(f) \
1426 if (!(p##f = wine_dlsym( libxi_handle, #f, NULL, 0))) \
1428 WARN("Failed to load %s.\n", #f); \
1432 LOAD_FUNCPTR(XIFreeDeviceInfo);
1433 LOAD_FUNCPTR(XIQueryDevice);
1434 LOAD_FUNCPTR(XIQueryVersion);
1435 LOAD_FUNCPTR(XISelectEvents);
1439 xinput2_available = XQueryExtension( gdi_display, "XInputExtension", &xinput2_opcode, &event, &error );
1440 wine_tsx11_unlock();
1442 TRACE( "X Input 2 support not compiled in.\n" );
1447 /***********************************************************************
1448 * X11DRV_GenericEvent
1450 void X11DRV_GenericEvent( HWND hwnd, XEvent *xev )
1452 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1453 XGenericEventCookie *event = &xev->xcookie;
1455 if (!event->data) return;
1456 if (event->extension != xinput2_opcode) return;
1458 switch (event->evtype)
1460 case XI_RawButtonPress:
1461 X11DRV_RawButtonPress( event->data );
1464 case XI_RawButtonRelease:
1465 X11DRV_RawButtonRelease( event->data );
1469 X11DRV_RawMotion( event->data );
1473 TRACE( "Unhandled event %#x\n", event->evtype );