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>
29 #ifdef SONAME_LIBXCURSOR
30 # include <X11/Xcursor/Xcursor.h>
31 static void *xcursor_handle;
32 # define MAKE_FUNCPTR(f) static typeof(f) * p##f
33 MAKE_FUNCPTR(XcursorImageCreate);
34 MAKE_FUNCPTR(XcursorImageDestroy);
35 MAKE_FUNCPTR(XcursorImageLoadCursor);
36 MAKE_FUNCPTR(XcursorImagesCreate);
37 MAKE_FUNCPTR(XcursorImagesDestroy);
38 MAKE_FUNCPTR(XcursorImagesLoadCursor);
39 MAKE_FUNCPTR(XcursorLibraryLoadCursor);
41 #endif /* SONAME_LIBXCURSOR */
43 #define NONAMELESSUNION
44 #define NONAMELESSSTRUCT
51 #include "wine/server.h"
52 #include "wine/library.h"
53 #include "wine/unicode.h"
54 #include "wine/debug.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
58 /**********************************************************************/
61 #define Button6Mask (1<<13)
64 #define Button7Mask (1<<14)
67 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
69 static const UINT button_down_flags[NB_BUTTONS] =
72 MOUSEEVENTF_MIDDLEDOWN,
73 MOUSEEVENTF_RIGHTDOWN,
76 MOUSEEVENTF_XDOWN, /* FIXME: horizontal wheel */
82 static const UINT button_up_flags[NB_BUTTONS] =
95 static HWND cursor_window;
96 static DWORD last_time_modified;
97 static XContext cursor_context;
98 static Cursor create_cursor( HANDLE handle );
100 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y );
103 /***********************************************************************
104 * X11DRV_Xcursor_Init
106 * Load the Xcursor library for use.
108 void X11DRV_Xcursor_Init(void)
110 #ifdef SONAME_LIBXCURSOR
111 xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
112 if (!xcursor_handle) /* wine_dlopen failed. */
114 WARN("Xcursor failed to load. Using fallback code.\n");
117 #define LOAD_FUNCPTR(f) \
118 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
120 LOAD_FUNCPTR(XcursorImageCreate);
121 LOAD_FUNCPTR(XcursorImageDestroy);
122 LOAD_FUNCPTR(XcursorImageLoadCursor);
123 LOAD_FUNCPTR(XcursorImagesCreate);
124 LOAD_FUNCPTR(XcursorImagesDestroy);
125 LOAD_FUNCPTR(XcursorImagesLoadCursor);
126 LOAD_FUNCPTR(XcursorLibraryLoadCursor);
128 #endif /* SONAME_LIBXCURSOR */
132 /***********************************************************************
135 static Cursor get_empty_cursor(void)
137 static Cursor cursor;
138 static const char data[] = { 0 };
146 bg.red = bg.green = bg.blue = 0x0000;
147 pixmap = XCreateBitmapFromData( gdi_display, root_window, data, 1, 1 );
150 cursor = XCreatePixmapCursor( gdi_display, pixmap, pixmap, &bg, &bg, 0, 0 );
151 XFreePixmap( gdi_display, pixmap );
158 /***********************************************************************
161 void set_window_cursor( struct x11drv_win_data *data, HCURSOR handle )
166 if (!handle) cursor = get_empty_cursor();
167 else if (!cursor_context || XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
169 /* try to create it */
171 if (!(cursor = create_cursor( handle ))) return;
174 if (!cursor_context) cursor_context = XUniqueContext();
175 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&prev ))
177 /* someone else was here first */
178 XFreeCursor( gdi_display, cursor );
183 XSaveContext( gdi_display, (XID)handle, cursor_context, (char *)cursor );
184 TRACE( "cursor %p created %lx\n", handle, cursor );
188 XDefineCursor( gdi_display, data->whole_window, cursor );
189 /* make the change take effect immediately */
190 XFlush( gdi_display );
191 data->cursor = handle;
195 /***********************************************************************
198 void sync_window_cursor( struct x11drv_win_data *data )
202 SERVER_START_REQ( set_cursor )
205 wine_server_call( req );
206 cursor = reply->prev_count >= 0 ? wine_server_ptr_handle( reply->prev_handle ) : 0;
210 if (data->cursor != cursor) set_window_cursor( data, cursor );
213 /***********************************************************************
216 * Update the various window states on a mouse event.
218 static void send_mouse_input( HWND hwnd, UINT flags, Window window, int x, int y,
219 unsigned int state, DWORD mouse_data, Time time )
221 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
227 if (window == data->whole_window)
229 x += data->whole_rect.left - data->client_rect.left;
230 y += data->whole_rect.top - data->client_rect.top;
232 if (window == root_window)
234 x += virtual_screen_rect.left;
235 y += virtual_screen_rect.top;
239 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
240 pt.x = data->client_rect.right - data->client_rect.left - 1 - pt.x;
241 MapWindowPoints( hwnd, 0, &pt, 1 );
243 if (InterlockedExchangePointer( (void **)&cursor_window, hwnd ) != hwnd ||
244 GetTickCount() - last_time_modified > 100)
246 cursor_window = hwnd;
247 sync_window_cursor( data );
249 last_time_modified = GetTickCount();
251 if (hwnd != GetDesktopWindow()) hwnd = GetAncestor( hwnd, GA_ROOT );
253 /* update the wine server Z-order */
255 if (window != x11drv_thread_data()->grab_window &&
256 /* ignore event if a button is pressed, since the mouse is then grabbed too */
257 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
260 SetRect( &rect, pt.x, pt.y, pt.x + 1, pt.y + 1 );
261 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
263 SERVER_START_REQ( update_window_zorder )
265 req->window = wine_server_user_handle( hwnd );
266 req->rect.left = rect.left;
267 req->rect.top = rect.top;
268 req->rect.right = rect.right;
269 req->rect.bottom = rect.bottom;
270 wine_server_call( req );
275 input.type = INPUT_MOUSE;
276 input.u.mi.dx = pt.x;
277 input.u.mi.dy = pt.y;
278 input.u.mi.mouseData = mouse_data;
279 input.u.mi.dwFlags = flags;
280 input.u.mi.time = EVENT_x11_time_to_win32_time( time );
281 input.u.mi.dwExtraInfo = 0;
283 __wine_send_input( hwnd, &input );
286 #ifdef SONAME_LIBXCURSOR
288 /***********************************************************************
289 * create_xcursor_frame
291 * Use Xcursor to create a frame of an X cursor from a Windows one.
293 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
294 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
295 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
296 int width, int height, int istep )
298 XcursorImage *image, *ret = NULL;
299 int x, y, i, has_alpha;
303 image = pXcursorImageCreate( width, height );
307 ERR("X11 failed to produce a cursor frame!\n");
311 image->xhot = iinfo->xHotspot;
312 image->yhot = iinfo->yHotspot;
313 image->delay = 100; /* TODO: find a way to get the proper delay */
315 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
316 memset( color_bits, 0x00, color_size );
317 SelectObject( hdc, hbmColor );
318 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
320 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
323 memcpy( image->pixels, color_bits, color_size );
325 /* check if the cursor frame was drawn with an alpha channel */
326 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
327 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
329 /* if no alpha channel was drawn then generate it from the mask */
332 unsigned int width_bytes = (width + 31) / 32 * 4;
334 /* draw the cursor mask to a temporary buffer */
335 memset( mask_bits, 0xFF, mask_size );
336 SelectObject( hdc, hbmMask );
337 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
339 ERR("Failed to draw frame mask %d.\n", istep);
342 /* use the buffer to directly modify the XcursorImage alpha channel */
343 for (y = 0, ptr = image->pixels; y < height; y++)
344 for (x = 0; x < width; x++, ptr++)
345 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
351 if (ret == NULL) pXcursorImageDestroy( image );
355 /***********************************************************************
356 * create_xcursor_cursor
358 * Use Xcursor to create an X cursor from a Windows one.
360 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
362 unsigned char *color_bits, *mask_bits;
363 HBITMAP hbmColor = 0, hbmMask = 0;
364 XcursorImage **imgs, *image;
365 int color_size, mask_size;
366 BITMAPINFO *info = NULL;
367 XcursorImages *images;
371 if (!(imgs = HeapAlloc( GetProcessHeap(), 0, sizeof(XcursorImage*) ))) return 0;
373 /* Allocate all of the resources necessary to obtain a cursor frame */
374 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
375 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
376 info->bmiHeader.biWidth = width;
377 info->bmiHeader.biHeight = -height;
378 info->bmiHeader.biPlanes = 1;
379 info->bmiHeader.biCompression = BI_RGB;
380 info->bmiHeader.biXPelsPerMeter = 0;
381 info->bmiHeader.biYPelsPerMeter = 0;
382 info->bmiHeader.biClrUsed = 0;
383 info->bmiHeader.biClrImportant = 0;
384 info->bmiHeader.biBitCount = 32;
385 color_size = width * height * 4;
386 info->bmiHeader.biSizeImage = color_size;
387 hbmColor = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &color_bits, NULL, 0);
390 ERR("Failed to create DIB section for cursor color data!\n");
393 info->bmiHeader.biBitCount = 1;
394 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
395 info->bmiHeader.biSizeImage = mask_size;
396 hbmMask = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &mask_bits, NULL, 0);
399 ERR("Failed to create DIB section for cursor mask data!\n");
403 /* Create an XcursorImage for each frame of the cursor */
406 XcursorImage **imgstmp;
408 image = create_xcursor_frame( hdc, iinfo, icon,
409 hbmColor, color_bits, color_size,
410 hbmMask, mask_bits, mask_size,
411 width, height, nFrames );
412 if (!image) break; /* no more drawable frames */
414 imgs[nFrames++] = image;
415 if (!(imgstmp = HeapReAlloc( GetProcessHeap(), 0, imgs, (nFrames+1)*sizeof(XcursorImage*) ))) goto cleanup;
419 /* Build an X cursor out of all of the frames */
420 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
421 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
422 images->images[images->nimage] = imgs[images->nimage];
424 cursor = pXcursorImagesLoadCursor( gdi_display, images );
426 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
427 HeapFree( GetProcessHeap(), 0, imgs );
433 /* Failed to produce a cursor, free previously allocated frames */
434 for (nFrames--; nFrames >= 0; nFrames--)
435 pXcursorImageDestroy( imgs[nFrames] );
436 HeapFree( GetProcessHeap(), 0, imgs );
438 /* Cleanup all of the resources used to obtain the frame data */
439 if (hbmColor) DeleteObject( hbmColor );
440 if (hbmMask) DeleteObject( hbmMask );
441 HeapFree( GetProcessHeap(), 0, info );
446 struct system_cursors
452 static const struct system_cursors user32_cursors[] =
454 { OCR_NORMAL, "left_ptr" },
455 { OCR_IBEAM, "xterm" },
456 { OCR_WAIT, "watch" },
457 { OCR_CROSS, "cross" },
458 { OCR_UP, "center_ptr" },
459 { OCR_SIZE, "fleur" },
460 { OCR_SIZEALL, "fleur" },
461 { OCR_ICON, "icon" },
462 { OCR_SIZENWSE, "nwse-resize" },
463 { OCR_SIZENESW, "nesw-resize" },
464 { OCR_SIZEWE, "ew-resize" },
465 { OCR_SIZENS, "ns-resize" },
466 { OCR_NO, "not-allowed" },
467 { OCR_HAND, "hand2" },
468 { OCR_APPSTARTING, "left_ptr_watch" },
469 { OCR_HELP, "question_arrow" },
473 static const struct system_cursors comctl32_cursors[] =
478 { 106, "row-resize" },
479 { 107, "row-resize" },
481 { 135, "col-resize" },
485 static const struct system_cursors ole32_cursors[] =
494 static const struct system_cursors riched20_cursors[] =
497 { 107, "right_ptr" },
506 const struct system_cursors *cursors;
510 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
511 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
512 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
513 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
516 /***********************************************************************
517 * create_xcursor_system_cursor
519 * Create an X cursor for a system cursor.
521 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
523 static const WCHAR idW[] = {'%','h','u',0};
524 const struct system_cursors *cursors;
529 WCHAR *p, name[MAX_PATH * 2], valueW[64];
533 if (!pXcursorLibraryLoadCursor) return 0;
534 if (!info->szModName[0]) return 0;
536 p = strrchrW( info->szModName, '\\' );
537 strcpyW( name, p ? p + 1 : info->szModName );
538 p = name + strlenW( name );
540 if (info->szResName[0]) strcpyW( p, info->szResName );
541 else sprintfW( p, idW, info->wResID );
544 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
545 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver\\Cursors", &key ))
547 size = sizeof(valueW) / sizeof(WCHAR);
548 ret = RegQueryValueExW( key, name, NULL, NULL, (BYTE *)valueW, &size );
552 if (!valueW[0]) return 0; /* force standard cursor */
553 if (!WideCharToMultiByte( CP_UNIXCP, 0, valueW, -1, valueA, sizeof(valueA), NULL, NULL ))
559 if (info->szResName[0]) goto done; /* only integer resources are supported here */
560 if (!(module = GetModuleHandleW( info->szModName ))) goto done;
562 for (i = 0; i < sizeof(module_cursors)/sizeof(module_cursors[0]); i++)
563 if (GetModuleHandleW( module_cursors[i].name ) == module) break;
564 if (i == sizeof(module_cursors)/sizeof(module_cursors[0])) goto done;
566 cursors = module_cursors[i].cursors;
567 for (i = 0; cursors[i].id; i++)
568 if (cursors[i].id == info->wResID)
570 strcpy( valueA, cursors[i].name );
578 cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
580 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
581 debugstr_w(name), debugstr_a(valueA) );
583 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
587 #endif /* SONAME_LIBXCURSOR */
590 /***********************************************************************
591 * create_cursor_from_bitmaps
593 * Create an X11 cursor from source bitmaps.
595 static Cursor create_cursor_from_bitmaps( HBITMAP src_xor, HBITMAP src_and, int width, int height,
596 int xor_y, int and_y, XColor *fg, XColor *bg,
597 int hotspot_x, int hotspot_y )
599 HDC src = 0, dst = 0;
600 HBITMAP bits = 0, mask = 0, mask_inv = 0;
603 if (!(src = CreateCompatibleDC( 0 ))) goto done;
604 if (!(dst = CreateCompatibleDC( 0 ))) goto done;
606 if (!(bits = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
607 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
608 if (!(mask_inv = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
610 /* We have to do some magic here, as cursors are not fully
611 * compatible between Windows and X11. Under X11, there are
612 * only 3 possible color cursor: black, white and masked. So
613 * we map the 4th Windows color (invert the bits on the screen)
614 * to black and an additional white bit on an other place
615 * (+1,+1). This require some boolean arithmetic:
618 * And Xor Result | Bits Mask Result
619 * 0 0 black | 0 1 background
620 * 0 1 white | 1 1 foreground
621 * 1 0 no change | X 0 no change
622 * 1 1 inverted | 0 1 background
625 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
626 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
628 SelectObject( src, src_and );
629 SelectObject( dst, bits );
630 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
631 SelectObject( dst, mask );
632 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
633 SelectObject( dst, mask_inv );
634 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
635 SelectObject( src, src_xor );
636 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, SRCAND /* src & dst */ );
637 SelectObject( dst, bits );
638 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, SRCERASE /* src & ~dst */ );
639 SelectObject( dst, mask );
640 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, 0xdd0228 /* src | ~dst */ );
641 /* additional white */
642 SelectObject( src, mask_inv );
643 BitBlt( dst, 1, 1, width, height, src, 0, 0, SRCPAINT /* src | dst */);
644 SelectObject( dst, bits );
645 BitBlt( dst, 1, 1, width, height, src, 0, 0, SRCPAINT /* src | dst */ );
648 cursor = XCreatePixmapCursor( gdi_display, X11DRV_get_pixmap(bits), X11DRV_get_pixmap(mask),
649 fg, bg, hotspot_x, hotspot_y );
655 DeleteObject( bits );
656 DeleteObject( mask );
657 DeleteObject( mask_inv );
661 /***********************************************************************
664 * Create an X cursor from a Windows one.
666 static Cursor create_xlib_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
669 Cursor cursor = None;
670 HBITMAP xor_bitmap = 0;
672 unsigned int *color_bits = NULL, *ptr;
673 unsigned char *mask_bits = NULL, *xor_bits = NULL;
674 int i, x, y, has_alpha = 0;
675 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
676 unsigned int width_bytes = (width + 31) / 32 * 4;
678 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] ))))
680 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
681 info->bmiHeader.biWidth = width;
682 info->bmiHeader.biHeight = -height;
683 info->bmiHeader.biPlanes = 1;
684 info->bmiHeader.biBitCount = 1;
685 info->bmiHeader.biCompression = BI_RGB;
686 info->bmiHeader.biSizeImage = width_bytes * height;
687 info->bmiHeader.biXPelsPerMeter = 0;
688 info->bmiHeader.biYPelsPerMeter = 0;
689 info->bmiHeader.biClrUsed = 0;
690 info->bmiHeader.biClrImportant = 0;
692 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
693 if (!GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS )) goto done;
695 info->bmiHeader.biBitCount = 32;
696 info->bmiHeader.biSizeImage = width * height * 4;
697 if (!(color_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
698 if (!(xor_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, width_bytes * height ))) goto done;
699 GetDIBits( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS );
701 /* compute fg/bg color and xor bitmap based on average of the color values */
703 if (!(xor_bitmap = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
704 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
705 for (y = 0, ptr = color_bits; y < height; y++)
707 for (x = 0; x < width; x++, ptr++)
709 int red = (*ptr >> 16) & 0xff;
710 int green = (*ptr >> 8) & 0xff;
711 int blue = (*ptr >> 0) & 0xff;
712 if (red + green + blue > 0x40)
718 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
730 fg.red = rfg * 257 / fgBits;
731 fg.green = gfg * 257 / fgBits;
732 fg.blue = bfg * 257 / fgBits;
734 else fg.red = fg.green = fg.blue = 0;
735 bgBits = width * height - fgBits;
738 bg.red = rbg * 257 / bgBits;
739 bg.green = gbg * 257 / bgBits;
740 bg.blue = bbg * 257 / bgBits;
742 else bg.red = bg.green = bg.blue = 0;
744 info->bmiHeader.biBitCount = 1;
745 info->bmiHeader.biSizeImage = width_bytes * height;
746 SetDIBits( hdc, xor_bitmap, 0, height, xor_bits, info, DIB_RGB_COLORS );
748 /* generate mask from the alpha channel if we have one */
750 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
751 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
755 memset( mask_bits, 0, width_bytes * height );
756 for (y = 0, ptr = color_bits; y < height; y++)
757 for (x = 0; x < width; x++, ptr++)
758 if ((*ptr >> 24) > 25) /* more than 10% alpha */
759 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
761 info->bmiHeader.biBitCount = 1;
762 info->bmiHeader.biSizeImage = width_bytes * height;
763 SetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS );
766 cursor = XCreatePixmapCursor( gdi_display,
767 X11DRV_get_pixmap(xor_bitmap),
768 X11DRV_get_pixmap(icon->hbmMask),
769 &fg, &bg, icon->xHotspot, icon->yHotspot );
774 cursor = create_cursor_from_bitmaps( xor_bitmap, icon->hbmMask, width, height, 0, 0,
775 &fg, &bg, icon->xHotspot, icon->yHotspot );
779 DeleteObject( xor_bitmap );
780 HeapFree( GetProcessHeap(), 0, info );
781 HeapFree( GetProcessHeap(), 0, color_bits );
782 HeapFree( GetProcessHeap(), 0, xor_bits );
783 HeapFree( GetProcessHeap(), 0, mask_bits );
787 /***********************************************************************
790 * Create an X cursor from a Windows one.
792 static Cursor create_cursor( HANDLE handle )
798 if (!handle) return get_empty_cursor();
800 info.cbSize = sizeof(info);
801 if (!GetIconInfoExW( handle, &info )) return 0;
803 #ifdef SONAME_LIBXCURSOR
804 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
806 DeleteObject( info.hbmColor );
807 DeleteObject( info.hbmMask );
812 GetObjectW( info.hbmMask, sizeof(bm), &bm );
813 if (!info.hbmColor) bm.bmHeight /= 2;
815 /* make sure hotspot is valid */
816 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
818 info.xHotspot = bm.bmWidth / 2;
819 info.yHotspot = bm.bmHeight / 2;
824 HDC hdc = CreateCompatibleDC( 0 );
827 #ifdef SONAME_LIBXCURSOR
828 if (pXcursorImagesLoadCursor)
829 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
831 if (!cursor) cursor = create_xlib_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
833 DeleteObject( info.hbmColor );
839 fg.red = fg.green = fg.blue = 0xffff;
840 bg.red = bg.green = bg.blue = 0;
841 cursor = create_cursor_from_bitmaps( info.hbmMask, info.hbmMask, bm.bmWidth, bm.bmHeight,
842 bm.bmHeight, 0, &fg, &bg, info.xHotspot, info.yHotspot );
845 DeleteObject( info.hbmMask );
849 /***********************************************************************
850 * DestroyCursorIcon (X11DRV.@)
852 void CDECL X11DRV_DestroyCursorIcon( HCURSOR handle )
857 if (cursor_context && !XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
859 TRACE( "%p xid %lx\n", handle, cursor );
860 XFreeCursor( gdi_display, cursor );
861 XDeleteContext( gdi_display, (XID)handle, cursor_context );
866 /***********************************************************************
867 * SetCursor (X11DRV.@)
869 void CDECL X11DRV_SetCursor( HCURSOR handle )
871 if (cursor_window) SendNotifyMessageW( cursor_window, WM_X11DRV_SET_CURSOR, 0, (LPARAM)handle );
874 /***********************************************************************
875 * SetCursorPos (X11DRV.@)
877 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
879 Display *display = thread_init_display();
881 TRACE( "warping to (%d,%d)\n", x, y );
884 XWarpPointer( display, root_window, root_window, 0, 0, 0, 0,
885 x - virtual_screen_rect.left, y - virtual_screen_rect.top );
886 XFlush( display ); /* avoids bad mouse lag in games that do their own mouse warping */
891 /***********************************************************************
892 * GetCursorPos (X11DRV.@)
894 BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
896 Display *display = thread_init_display();
898 int rootX, rootY, winX, winY;
903 ret = XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &winX, &winY, &xstate );
906 pos->x = winX + virtual_screen_rect.left;
907 pos->y = winY + virtual_screen_rect.top;
908 TRACE("pointer at (%d,%d)\n", pos->x, pos->y );
915 /***********************************************************************
918 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
920 XButtonEvent *event = &xev->xbutton;
921 int buttonNum = event->button - 1;
924 if (buttonNum >= NB_BUTTONS) return;
932 wData = -WHEEL_DELTA;
948 update_user_time( event->time );
949 send_mouse_input( hwnd, button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
950 event->window, event->x, event->y, event->state, wData, event->time );
954 /***********************************************************************
955 * X11DRV_ButtonRelease
957 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
959 XButtonEvent *event = &xev->xbutton;
960 int buttonNum = event->button - 1;
963 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
981 send_mouse_input( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
982 event->window, event->x, event->y, event->state, wData, event->time );
986 /***********************************************************************
987 * X11DRV_MotionNotify
989 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
991 XMotionEvent *event = &xev->xmotion;
993 TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);
995 send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
996 event->window, event->x, event->y, event->state, 0, event->time );
1000 /***********************************************************************
1001 * X11DRV_EnterNotify
1003 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1005 XCrossingEvent *event = &xev->xcrossing;
1007 TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);
1009 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1010 if (event->window == x11drv_thread_data()->grab_window) return;
1012 /* simulate a mouse motion event */
1013 send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1014 event->window, event->x, event->y, event->state, 0, event->time );