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_opcode;
134 static int xinput2_core_pointer;
135 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
136 MAKE_FUNCPTR(XIFreeDeviceInfo);
137 MAKE_FUNCPTR(XIQueryDevice);
138 MAKE_FUNCPTR(XIQueryVersion);
139 MAKE_FUNCPTR(XISelectEvents);
143 /***********************************************************************
144 * X11DRV_Xcursor_Init
146 * Load the Xcursor library for use.
148 void X11DRV_Xcursor_Init(void)
150 #ifdef SONAME_LIBXCURSOR
151 xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
152 if (!xcursor_handle) /* wine_dlopen failed. */
154 WARN("Xcursor failed to load. Using fallback code.\n");
157 #define LOAD_FUNCPTR(f) \
158 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
160 LOAD_FUNCPTR(XcursorImageCreate);
161 LOAD_FUNCPTR(XcursorImageDestroy);
162 LOAD_FUNCPTR(XcursorImageLoadCursor);
163 LOAD_FUNCPTR(XcursorImagesCreate);
164 LOAD_FUNCPTR(XcursorImagesDestroy);
165 LOAD_FUNCPTR(XcursorImagesLoadCursor);
166 LOAD_FUNCPTR(XcursorLibraryLoadCursor);
168 #endif /* SONAME_LIBXCURSOR */
172 /***********************************************************************
175 static Cursor get_empty_cursor(void)
177 static Cursor cursor;
178 static const char data[] = { 0 };
186 bg.red = bg.green = bg.blue = 0x0000;
187 pixmap = XCreateBitmapFromData( gdi_display, root_window, data, 1, 1 );
190 cursor = XCreatePixmapCursor( gdi_display, pixmap, pixmap, &bg, &bg, 0, 0 );
191 XFreePixmap( gdi_display, pixmap );
198 /***********************************************************************
201 void set_window_cursor( Window window, HCURSOR handle )
206 if (!handle) cursor = get_empty_cursor();
207 else if (!cursor_context || XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
209 /* try to create it */
211 if (!(cursor = create_cursor( handle ))) return;
214 if (!cursor_context) cursor_context = XUniqueContext();
215 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&prev ))
217 /* someone else was here first */
218 XFreeCursor( gdi_display, cursor );
223 XSaveContext( gdi_display, (XID)handle, cursor_context, (char *)cursor );
224 TRACE( "cursor %p created %lx\n", handle, cursor );
228 XDefineCursor( gdi_display, window, cursor );
229 /* make the change take effect immediately */
230 XFlush( gdi_display );
234 /***********************************************************************
237 void sync_window_cursor( Window window )
241 SERVER_START_REQ( set_cursor )
244 wine_server_call( req );
245 cursor = reply->prev_count >= 0 ? wine_server_ptr_handle( reply->prev_handle ) : 0;
249 set_window_cursor( window, cursor );
252 /***********************************************************************
255 static void enable_xinput2(void)
257 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
258 struct x11drv_thread_data *data = x11drv_thread_data();
259 XIDeviceInfo *devices;
261 unsigned char mask_bits[XIMaskLen(XI_LASTEVENT)];
264 if (!xinput2_available) return;
266 if (data->xi2_state == xi_unknown)
268 int major = 2, minor = 0;
270 if (!pXIQueryVersion( data->display, &major, &minor )) data->xi2_state = xi_disabled;
273 data->xi2_state = xi_unavailable;
274 WARN( "X Input 2 not available\n" );
278 if (data->xi2_state == xi_unavailable) return;
281 devices = pXIQueryDevice( data->display, XIAllDevices, &count );
282 for (i = 0; i < count; ++i)
284 if (devices[i].use != XIMasterPointer) continue;
285 TRACE( "Using %u (%s) as core pointer\n",
286 devices[i].deviceid, debugstr_a(devices[i].name) );
287 xinput2_core_pointer = devices[i].deviceid;
291 mask.mask = mask_bits;
292 mask.mask_len = sizeof(mask_bits);
293 memset( mask_bits, 0, sizeof(mask_bits) );
295 XISetMask( mask_bits, XI_RawButtonPress );
296 XISetMask( mask_bits, XI_RawButtonRelease );
297 XISetMask( mask_bits, XI_RawMotion );
299 for (i = 0; i < count; ++i)
301 if (devices[i].use == XISlavePointer && devices[i].attachment == xinput2_core_pointer)
303 TRACE( "Device %u (%s) is attached to the core pointer\n",
304 devices[i].deviceid, debugstr_a(devices[i].name) );
305 mask.deviceid = devices[i].deviceid;
306 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
307 data->xi2_state = xi_enabled;
311 pXIFreeDeviceInfo( devices );
316 /***********************************************************************
319 static void disable_xinput2(void)
321 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
322 struct x11drv_thread_data *data = x11drv_thread_data();
324 XIDeviceInfo *devices;
327 if (data->xi2_state != xi_enabled) return;
329 TRACE( "disabling\n" );
330 data->xi2_state = xi_disabled;
336 devices = pXIQueryDevice( data->display, XIAllDevices, &count );
337 for (i = 0; i < count; ++i)
339 if (devices[i].use == XISlavePointer && devices[i].attachment == xinput2_core_pointer)
341 mask.deviceid = devices[i].deviceid;
342 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
345 pXIFreeDeviceInfo( devices );
350 /***********************************************************************
351 * clipping_window_unmapped
353 * Turn off clipping when the window got unmapped.
355 void clipping_window_unmapped(void)
357 struct x11drv_thread_data *data = x11drv_thread_data();
360 if (data->xi2_state == xi_enabled)
363 GetClipCursor( &rect );
364 if (EqualRect( &rect, &clip_rect )) return; /* still clipped */
370 /***********************************************************************
373 * Update the various window states on a mouse event.
375 static void send_mouse_input( HWND hwnd, Window window, unsigned int state, INPUT *input )
377 struct x11drv_win_data *data;
380 input->type = INPUT_MOUSE;
382 if (!hwnd && window == clip_window)
384 input->u.mi.dx += clip_rect.left;
385 input->u.mi.dy += clip_rect.top;
386 if (x11drv_thread_data()->xi2_state != xi_enabled) __wine_send_input( hwnd, input );
390 if (!(data = X11DRV_get_win_data( hwnd ))) return;
392 if (window == data->whole_window)
394 input->u.mi.dx += data->whole_rect.left - data->client_rect.left;
395 input->u.mi.dy += data->whole_rect.top - data->client_rect.top;
397 if (window == root_window)
399 input->u.mi.dx += virtual_screen_rect.left;
400 input->u.mi.dy += virtual_screen_rect.top;
402 pt.x = input->u.mi.dx;
403 pt.y = input->u.mi.dy;
404 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
405 pt.x = data->client_rect.right - data->client_rect.left - 1 - pt.x;
406 MapWindowPoints( hwnd, 0, &pt, 1 );
408 if (InterlockedExchangePointer( (void **)&cursor_window, hwnd ) != hwnd ||
409 GetTickCount() - last_cursor_change > 100)
411 sync_window_cursor( data->whole_window );
412 last_cursor_change = GetTickCount();
415 if (hwnd != GetDesktopWindow()) hwnd = GetAncestor( hwnd, GA_ROOT );
417 /* update the wine server Z-order */
419 if (window != x11drv_thread_data()->grab_window &&
420 /* ignore event if a button is pressed, since the mouse is then grabbed too */
421 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
424 SetRect( &rect, pt.x, pt.y, pt.x + 1, pt.y + 1 );
425 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
427 SERVER_START_REQ( update_window_zorder )
429 req->window = wine_server_user_handle( hwnd );
430 req->rect.left = rect.left;
431 req->rect.top = rect.top;
432 req->rect.right = rect.right;
433 req->rect.bottom = rect.bottom;
434 wine_server_call( req );
439 input->u.mi.dx = pt.x;
440 input->u.mi.dy = pt.y;
441 __wine_send_input( hwnd, input );
444 #ifdef SONAME_LIBXCURSOR
446 /***********************************************************************
447 * create_xcursor_frame
449 * Use Xcursor to create a frame of an X cursor from a Windows one.
451 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
452 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
453 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
454 int width, int height, int istep )
456 XcursorImage *image, *ret = NULL;
457 DWORD delay_jiffies, num_steps;
458 int x, y, i, has_alpha = FALSE;
462 image = pXcursorImageCreate( width, height );
466 ERR("X11 failed to produce a cursor frame!\n");
470 image->xhot = iinfo->xHotspot;
471 image->yhot = iinfo->yHotspot;
473 image->delay = 100; /* fallback delay, 100 ms */
474 if (GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, istep, &delay_jiffies, &num_steps) != 0)
475 image->delay = (100 * delay_jiffies) / 6; /* convert jiffies (1/60s) to milliseconds */
477 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep);
479 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
480 memset( color_bits, 0x00, color_size );
481 SelectObject( hdc, hbmColor );
482 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
484 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
487 memcpy( image->pixels, color_bits, color_size );
489 /* check if the cursor frame was drawn with an alpha channel */
490 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
491 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
493 /* if no alpha channel was drawn then generate it from the mask */
496 unsigned int width_bytes = (width + 31) / 32 * 4;
498 /* draw the cursor mask to a temporary buffer */
499 memset( mask_bits, 0xFF, mask_size );
500 SelectObject( hdc, hbmMask );
501 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
503 ERR("Failed to draw frame mask %d.\n", istep);
506 /* use the buffer to directly modify the XcursorImage alpha channel */
507 for (y = 0, ptr = image->pixels; y < height; y++)
508 for (x = 0; x < width; x++, ptr++)
509 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
515 if (ret == NULL) pXcursorImageDestroy( image );
519 /***********************************************************************
520 * create_xcursor_cursor
522 * Use Xcursor to create an X cursor from a Windows one.
524 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
526 unsigned char *color_bits, *mask_bits;
527 HBITMAP hbmColor = 0, hbmMask = 0;
528 DWORD nFrames, delay_jiffies, i;
529 int color_size, mask_size;
530 BITMAPINFO *info = NULL;
531 XcursorImages *images;
535 /* Retrieve the number of frames to render */
536 if (!GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, 0, &delay_jiffies, &nFrames)) return 0;
537 if (!(imgs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XcursorImage*)*nFrames ))) return 0;
539 /* Allocate all of the resources necessary to obtain a cursor frame */
540 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
541 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
542 info->bmiHeader.biWidth = width;
543 info->bmiHeader.biHeight = -height;
544 info->bmiHeader.biPlanes = 1;
545 info->bmiHeader.biCompression = BI_RGB;
546 info->bmiHeader.biXPelsPerMeter = 0;
547 info->bmiHeader.biYPelsPerMeter = 0;
548 info->bmiHeader.biClrUsed = 0;
549 info->bmiHeader.biClrImportant = 0;
550 info->bmiHeader.biBitCount = 32;
551 color_size = width * height * 4;
552 info->bmiHeader.biSizeImage = color_size;
553 hbmColor = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &color_bits, NULL, 0);
556 ERR("Failed to create DIB section for cursor color data!\n");
559 info->bmiHeader.biBitCount = 1;
560 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
561 info->bmiHeader.biSizeImage = mask_size;
562 hbmMask = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &mask_bits, NULL, 0);
565 ERR("Failed to create DIB section for cursor mask data!\n");
569 /* Create an XcursorImage for each frame of the cursor */
570 for (i=0; i<nFrames; i++)
572 imgs[i] = create_xcursor_frame( hdc, iinfo, icon,
573 hbmColor, color_bits, color_size,
574 hbmMask, mask_bits, mask_size,
576 if (!imgs[i]) goto cleanup;
579 /* Build an X cursor out of all of the frames */
580 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
581 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
582 images->images[images->nimage] = imgs[images->nimage];
584 cursor = pXcursorImagesLoadCursor( gdi_display, images );
586 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
587 HeapFree( GetProcessHeap(), 0, imgs );
593 /* Failed to produce a cursor, free previously allocated frames */
594 for (i=0; i<nFrames && imgs[i]; i++)
595 pXcursorImageDestroy( imgs[i] );
596 HeapFree( GetProcessHeap(), 0, imgs );
598 /* Cleanup all of the resources used to obtain the frame data */
599 if (hbmColor) DeleteObject( hbmColor );
600 if (hbmMask) DeleteObject( hbmMask );
601 HeapFree( GetProcessHeap(), 0, info );
606 struct system_cursors
612 static const struct system_cursors user32_cursors[] =
614 { OCR_NORMAL, "left_ptr" },
615 { OCR_IBEAM, "xterm" },
616 { OCR_WAIT, "watch" },
617 { OCR_CROSS, "cross" },
618 { OCR_UP, "center_ptr" },
619 { OCR_SIZE, "fleur" },
620 { OCR_SIZEALL, "fleur" },
621 { OCR_ICON, "icon" },
622 { OCR_SIZENWSE, "nwse-resize" },
623 { OCR_SIZENESW, "nesw-resize" },
624 { OCR_SIZEWE, "ew-resize" },
625 { OCR_SIZENS, "ns-resize" },
626 { OCR_NO, "not-allowed" },
627 { OCR_HAND, "hand2" },
628 { OCR_APPSTARTING, "left_ptr_watch" },
629 { OCR_HELP, "question_arrow" },
633 static const struct system_cursors comctl32_cursors[] =
638 { 106, "row-resize" },
639 { 107, "row-resize" },
641 { 135, "col-resize" },
645 static const struct system_cursors ole32_cursors[] =
654 static const struct system_cursors riched20_cursors[] =
657 { 107, "right_ptr" },
666 const struct system_cursors *cursors;
670 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
671 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
672 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
673 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
676 /***********************************************************************
677 * create_xcursor_system_cursor
679 * Create an X cursor for a system cursor.
681 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
683 static const WCHAR idW[] = {'%','h','u',0};
684 const struct system_cursors *cursors;
689 WCHAR *p, name[MAX_PATH * 2], valueW[64];
693 if (!pXcursorLibraryLoadCursor) return 0;
694 if (!info->szModName[0]) return 0;
696 p = strrchrW( info->szModName, '\\' );
697 strcpyW( name, p ? p + 1 : info->szModName );
698 p = name + strlenW( name );
700 if (info->szResName[0]) strcpyW( p, info->szResName );
701 else sprintfW( p, idW, info->wResID );
704 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
705 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver\\Cursors", &key ))
707 size = sizeof(valueW) / sizeof(WCHAR);
708 ret = RegQueryValueExW( key, name, NULL, NULL, (BYTE *)valueW, &size );
712 if (!valueW[0]) return 0; /* force standard cursor */
713 if (!WideCharToMultiByte( CP_UNIXCP, 0, valueW, -1, valueA, sizeof(valueA), NULL, NULL ))
719 if (info->szResName[0]) goto done; /* only integer resources are supported here */
720 if (!(module = GetModuleHandleW( info->szModName ))) goto done;
722 for (i = 0; i < sizeof(module_cursors)/sizeof(module_cursors[0]); i++)
723 if (GetModuleHandleW( module_cursors[i].name ) == module) break;
724 if (i == sizeof(module_cursors)/sizeof(module_cursors[0])) goto done;
726 cursors = module_cursors[i].cursors;
727 for (i = 0; cursors[i].id; i++)
728 if (cursors[i].id == info->wResID)
730 strcpy( valueA, cursors[i].name );
738 cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
740 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
741 debugstr_w(name), debugstr_a(valueA) );
743 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
747 #endif /* SONAME_LIBXCURSOR */
750 /***********************************************************************
751 * create_cursor_from_bitmaps
753 * Create an X11 cursor from source bitmaps.
755 static Cursor create_cursor_from_bitmaps( HBITMAP src_xor, HBITMAP src_and, int width, int height,
756 int xor_y, int and_y, XColor *fg, XColor *bg,
757 int hotspot_x, int hotspot_y )
759 HDC src = 0, dst = 0;
760 HBITMAP bits = 0, mask = 0, mask_inv = 0;
763 if (!(src = CreateCompatibleDC( 0 ))) goto done;
764 if (!(dst = CreateCompatibleDC( 0 ))) goto done;
766 if (!(bits = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
767 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
768 if (!(mask_inv = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
770 /* We have to do some magic here, as cursors are not fully
771 * compatible between Windows and X11. Under X11, there are
772 * only 3 possible color cursor: black, white and masked. So
773 * we map the 4th Windows color (invert the bits on the screen)
774 * to black and an additional white bit on an other place
775 * (+1,+1). This require some boolean arithmetic:
778 * And Xor Result | Bits Mask Result
779 * 0 0 black | 0 1 background
780 * 0 1 white | 1 1 foreground
781 * 1 0 no change | X 0 no change
782 * 1 1 inverted | 0 1 background
785 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
786 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
788 SelectObject( src, src_and );
789 SelectObject( dst, bits );
790 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
791 SelectObject( dst, mask );
792 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
793 SelectObject( dst, mask_inv );
794 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
795 SelectObject( src, src_xor );
796 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, SRCAND /* src & dst */ );
797 SelectObject( dst, bits );
798 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, SRCERASE /* src & ~dst */ );
799 SelectObject( dst, mask );
800 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, 0xdd0228 /* src | ~dst */ );
801 /* additional white */
802 SelectObject( src, mask_inv );
803 BitBlt( dst, 1, 1, width, height, src, 0, 0, SRCPAINT /* src | dst */);
804 SelectObject( dst, bits );
805 BitBlt( dst, 1, 1, width, height, src, 0, 0, SRCPAINT /* src | dst */ );
808 cursor = XCreatePixmapCursor( gdi_display, X11DRV_get_pixmap(bits), X11DRV_get_pixmap(mask),
809 fg, bg, hotspot_x, hotspot_y );
815 DeleteObject( bits );
816 DeleteObject( mask );
817 DeleteObject( mask_inv );
821 /***********************************************************************
824 * Create an X cursor from a Windows one.
826 static Cursor create_xlib_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
829 Cursor cursor = None;
830 HBITMAP xor_bitmap = 0;
832 unsigned int *color_bits = NULL, *ptr;
833 unsigned char *mask_bits = NULL, *xor_bits = NULL;
834 int i, x, y, has_alpha = 0;
835 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
836 unsigned int width_bytes = (width + 31) / 32 * 4;
838 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] ))))
840 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
841 info->bmiHeader.biWidth = width;
842 info->bmiHeader.biHeight = -height;
843 info->bmiHeader.biPlanes = 1;
844 info->bmiHeader.biBitCount = 1;
845 info->bmiHeader.biCompression = BI_RGB;
846 info->bmiHeader.biSizeImage = width_bytes * height;
847 info->bmiHeader.biXPelsPerMeter = 0;
848 info->bmiHeader.biYPelsPerMeter = 0;
849 info->bmiHeader.biClrUsed = 0;
850 info->bmiHeader.biClrImportant = 0;
852 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
853 if (!GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS )) goto done;
855 info->bmiHeader.biBitCount = 32;
856 info->bmiHeader.biSizeImage = width * height * 4;
857 if (!(color_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
858 if (!(xor_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, width_bytes * height ))) goto done;
859 GetDIBits( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS );
861 /* compute fg/bg color and xor bitmap based on average of the color values */
863 if (!(xor_bitmap = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
864 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
865 for (y = 0, ptr = color_bits; y < height; y++)
867 for (x = 0; x < width; x++, ptr++)
869 int red = (*ptr >> 16) & 0xff;
870 int green = (*ptr >> 8) & 0xff;
871 int blue = (*ptr >> 0) & 0xff;
872 if (red + green + blue > 0x40)
878 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
890 fg.red = rfg * 257 / fgBits;
891 fg.green = gfg * 257 / fgBits;
892 fg.blue = bfg * 257 / fgBits;
894 else fg.red = fg.green = fg.blue = 0;
895 bgBits = width * height - fgBits;
898 bg.red = rbg * 257 / bgBits;
899 bg.green = gbg * 257 / bgBits;
900 bg.blue = bbg * 257 / bgBits;
902 else bg.red = bg.green = bg.blue = 0;
904 info->bmiHeader.biBitCount = 1;
905 info->bmiHeader.biSizeImage = width_bytes * height;
906 SetDIBits( hdc, xor_bitmap, 0, height, xor_bits, info, DIB_RGB_COLORS );
908 /* generate mask from the alpha channel if we have one */
910 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
911 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
915 memset( mask_bits, 0, width_bytes * height );
916 for (y = 0, ptr = color_bits; y < height; y++)
917 for (x = 0; x < width; x++, ptr++)
918 if ((*ptr >> 24) > 25) /* more than 10% alpha */
919 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
921 info->bmiHeader.biBitCount = 1;
922 info->bmiHeader.biSizeImage = width_bytes * height;
923 SetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS );
926 cursor = XCreatePixmapCursor( gdi_display,
927 X11DRV_get_pixmap(xor_bitmap),
928 X11DRV_get_pixmap(icon->hbmMask),
929 &fg, &bg, icon->xHotspot, icon->yHotspot );
934 cursor = create_cursor_from_bitmaps( xor_bitmap, icon->hbmMask, width, height, 0, 0,
935 &fg, &bg, icon->xHotspot, icon->yHotspot );
939 DeleteObject( xor_bitmap );
940 HeapFree( GetProcessHeap(), 0, info );
941 HeapFree( GetProcessHeap(), 0, color_bits );
942 HeapFree( GetProcessHeap(), 0, xor_bits );
943 HeapFree( GetProcessHeap(), 0, mask_bits );
947 /***********************************************************************
950 * Create an X cursor from a Windows one.
952 static Cursor create_cursor( HANDLE handle )
958 if (!handle) return get_empty_cursor();
960 info.cbSize = sizeof(info);
961 if (!GetIconInfoExW( handle, &info )) return 0;
963 #ifdef SONAME_LIBXCURSOR
964 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
966 DeleteObject( info.hbmColor );
967 DeleteObject( info.hbmMask );
972 GetObjectW( info.hbmMask, sizeof(bm), &bm );
973 if (!info.hbmColor) bm.bmHeight /= 2;
975 /* make sure hotspot is valid */
976 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
978 info.xHotspot = bm.bmWidth / 2;
979 info.yHotspot = bm.bmHeight / 2;
984 HDC hdc = CreateCompatibleDC( 0 );
987 #ifdef SONAME_LIBXCURSOR
988 if (pXcursorImagesLoadCursor)
989 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
991 if (!cursor) cursor = create_xlib_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
993 DeleteObject( info.hbmColor );
999 fg.red = fg.green = fg.blue = 0xffff;
1000 bg.red = bg.green = bg.blue = 0;
1001 cursor = create_cursor_from_bitmaps( info.hbmMask, info.hbmMask, bm.bmWidth, bm.bmHeight,
1002 bm.bmHeight, 0, &fg, &bg, info.xHotspot, info.yHotspot );
1005 DeleteObject( info.hbmMask );
1009 /***********************************************************************
1010 * DestroyCursorIcon (X11DRV.@)
1012 void CDECL X11DRV_DestroyCursorIcon( HCURSOR handle )
1017 if (cursor_context && !XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1019 TRACE( "%p xid %lx\n", handle, cursor );
1020 XFreeCursor( gdi_display, cursor );
1021 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1023 wine_tsx11_unlock();
1026 /***********************************************************************
1027 * SetCursor (X11DRV.@)
1029 void CDECL X11DRV_SetCursor( HCURSOR handle )
1031 if (InterlockedExchangePointer( (void **)&last_cursor, handle ) != handle ||
1032 GetTickCount() - last_cursor_change > 100)
1034 last_cursor_change = GetTickCount();
1035 if (clipping_cursor) set_window_cursor( clip_window, handle );
1036 else if (cursor_window) SendNotifyMessageW( cursor_window, WM_X11DRV_SET_CURSOR, 0, (LPARAM)handle );
1040 /***********************************************************************
1041 * SetCursorPos (X11DRV.@)
1043 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
1045 struct x11drv_thread_data *data = x11drv_init_thread_data();
1047 if (data->xi2_state == xi_enabled) return TRUE;
1049 TRACE( "warping to (%d,%d)\n", x, y );
1052 XWarpPointer( data->display, root_window, root_window, 0, 0, 0, 0,
1053 x - virtual_screen_rect.left, y - virtual_screen_rect.top );
1054 XFlush( data->display ); /* avoids bad mouse lag in games that do their own mouse warping */
1055 wine_tsx11_unlock();
1059 /***********************************************************************
1060 * GetCursorPos (X11DRV.@)
1062 BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
1064 Display *display = thread_init_display();
1066 int rootX, rootY, winX, winY;
1067 unsigned int xstate;
1071 ret = XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &winX, &winY, &xstate );
1074 pos->x = winX + virtual_screen_rect.left;
1075 pos->y = winY + virtual_screen_rect.top;
1076 TRACE("pointer at (%d,%d)\n", pos->x, pos->y );
1078 wine_tsx11_unlock();
1082 /***********************************************************************
1083 * ClipCursor (X11DRV.@)
1085 BOOL CDECL X11DRV_ClipCursor( LPCRECT clip )
1087 Display *display = thread_init_display();
1089 if (!clip_window) return TRUE;
1091 /* we are clipping if the clip rectangle is smaller than the screen */
1092 if (clip && (clip->left > virtual_screen_rect.left ||
1093 clip->right < virtual_screen_rect.right ||
1094 clip->top > virtual_screen_rect.top ||
1095 clip->bottom < virtual_screen_rect.bottom))
1097 if (GetWindowThreadProcessId( GetDesktopWindow(), NULL ) == GetCurrentThreadId())
1098 return TRUE; /* don't clip in the desktop process */
1102 TRACE( "clipping to %s\n", wine_dbgstr_rect(clip) );
1104 XUnmapWindow( display, clip_window );
1105 XMoveResizeWindow( display, clip_window,
1106 clip->left - virtual_screen_rect.left, clip->top - virtual_screen_rect.top,
1107 clip->right - clip->left, clip->bottom - clip->top );
1108 XMapWindow( display, clip_window );
1109 if (!XGrabPointer( display, clip_window, False,
1110 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1111 GrabModeAsync, GrabModeAsync, clip_window, None, CurrentTime ))
1112 clipping_cursor = 1;
1113 wine_tsx11_unlock();
1115 if (clipping_cursor)
1118 sync_window_cursor( clip_window );
1125 /* release the grab if any */
1126 TRACE( "no longer clipping\n" );
1128 XUnmapWindow( display, clip_window );
1129 wine_tsx11_unlock();
1130 clipping_cursor = 0;
1135 /***********************************************************************
1136 * X11DRV_ButtonPress
1138 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1140 XButtonEvent *event = &xev->xbutton;
1141 int buttonNum = event->button - 1;
1144 if (buttonNum >= NB_BUTTONS) return;
1146 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1148 input.u.mi.dx = event->x;
1149 input.u.mi.dy = event->y;
1150 input.u.mi.mouseData = button_down_data[buttonNum];
1151 input.u.mi.dwFlags = button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1152 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1153 input.u.mi.dwExtraInfo = 0;
1155 update_user_time( event->time );
1156 send_mouse_input( hwnd, event->window, event->state, &input );
1160 /***********************************************************************
1161 * X11DRV_ButtonRelease
1163 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1165 XButtonEvent *event = &xev->xbutton;
1166 int buttonNum = event->button - 1;
1169 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1171 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1173 input.u.mi.dx = event->x;
1174 input.u.mi.dy = event->y;
1175 input.u.mi.mouseData = button_up_data[buttonNum];
1176 input.u.mi.dwFlags = button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1177 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1178 input.u.mi.dwExtraInfo = 0;
1180 send_mouse_input( hwnd, event->window, event->state, &input );
1184 /***********************************************************************
1185 * X11DRV_MotionNotify
1187 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1189 XMotionEvent *event = &xev->xmotion;
1192 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d\n", hwnd, event->window, event->x, event->y, event->is_hint );
1194 input.u.mi.dx = event->x;
1195 input.u.mi.dy = event->y;
1196 input.u.mi.mouseData = 0;
1197 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1198 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1199 input.u.mi.dwExtraInfo = 0;
1201 send_mouse_input( hwnd, event->window, event->state, &input );
1205 /***********************************************************************
1206 * X11DRV_EnterNotify
1208 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1210 XCrossingEvent *event = &xev->xcrossing;
1213 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd, event->window, event->x, event->y, event->detail );
1215 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1216 if (event->window == x11drv_thread_data()->grab_window) return;
1218 /* simulate a mouse motion event */
1219 input.u.mi.dx = event->x;
1220 input.u.mi.dy = event->y;
1221 input.u.mi.mouseData = 0;
1222 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1223 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1224 input.u.mi.dwExtraInfo = 0;
1226 send_mouse_input( hwnd, event->window, event->state, &input );
1229 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1231 /***********************************************************************
1232 * X11DRV_RawButtonPress
1234 static void X11DRV_RawButtonPress( XIRawEvent *event )
1236 int button = event->detail - 1;
1239 if (button >= NB_BUTTONS) return;
1241 TRACE( "button %u\n", button );
1243 input.type = INPUT_MOUSE;
1246 input.u.mi.mouseData = button_down_data[button];
1247 input.u.mi.dwFlags = button_down_flags[button];
1248 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1249 input.u.mi.dwExtraInfo = 0;
1251 update_user_time( event->time );
1252 input.type = INPUT_MOUSE;
1253 __wine_send_input( 0, &input );
1257 /***********************************************************************
1258 * X11DRV_RawButtonRelease
1260 static void X11DRV_RawButtonRelease( XIRawEvent *event )
1262 int button = event->detail - 1;
1265 if (button >= NB_BUTTONS) return;
1267 TRACE( "button %u\n", button );
1271 input.u.mi.mouseData = button_up_data[button];
1272 input.u.mi.dwFlags = button_up_flags[button];
1273 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1274 input.u.mi.dwExtraInfo = 0;
1276 input.type = INPUT_MOUSE;
1277 __wine_send_input( 0, &input );
1281 /***********************************************************************
1284 static void X11DRV_RawMotion( XIRawEvent *event )
1286 const double *values = event->valuators.values;
1289 if (!event->valuators.mask_len) return;
1293 input.u.mi.mouseData = 0;
1294 input.u.mi.dwFlags = MOUSEEVENTF_MOVE;
1295 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1296 input.u.mi.dwExtraInfo = 0;
1298 if (XIMaskIsSet( event->valuators.mask, 0 )) input.u.mi.dx = *values++;
1299 if (XIMaskIsSet( event->valuators.mask, 1 )) input.u.mi.dy = *values++;
1301 TRACE( "pos %d,%d\n", input.u.mi.dx, input.u.mi.dy );
1303 input.type = INPUT_MOUSE;
1304 __wine_send_input( 0, &input );
1307 #endif /* HAVE_X11_EXTENSIONS_XINPUT2_H */
1310 /***********************************************************************
1311 * X11DRV_XInput2_Init
1313 void X11DRV_XInput2_Init(void)
1315 #if defined(SONAME_LIBXI) && defined(HAVE_X11_EXTENSIONS_XINPUT2_H)
1317 void *libxi_handle = wine_dlopen( SONAME_LIBXI, RTLD_NOW, NULL, 0 );
1321 WARN( "couldn't load %s\n", SONAME_LIBXI );
1324 #define LOAD_FUNCPTR(f) \
1325 if (!(p##f = wine_dlsym( libxi_handle, #f, NULL, 0))) \
1327 WARN("Failed to load %s.\n", #f); \
1331 LOAD_FUNCPTR(XIFreeDeviceInfo);
1332 LOAD_FUNCPTR(XIQueryDevice);
1333 LOAD_FUNCPTR(XIQueryVersion);
1334 LOAD_FUNCPTR(XISelectEvents);
1338 xinput2_available = XQueryExtension( gdi_display, "XInputExtension", &xinput2_opcode, &event, &error );
1339 wine_tsx11_unlock();
1341 TRACE( "X Input 2 support not compiled in.\n" );
1346 /***********************************************************************
1347 * X11DRV_GenericEvent
1349 void X11DRV_GenericEvent( HWND hwnd, XEvent *xev )
1351 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1352 XGenericEventCookie *event = &xev->xcookie;
1354 if (!event->data) return;
1355 if (event->extension != xinput2_opcode) return;
1357 switch (event->evtype)
1359 case XI_RawButtonPress:
1360 X11DRV_RawButtonPress( event->data );
1363 case XI_RawButtonRelease:
1364 X11DRV_RawButtonRelease( event->data );
1368 X11DRV_RawMotion( event->data );
1372 TRACE( "Unhandled event %#x\n", event->evtype );