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] =
96 static HWND cursor_window;
97 static DWORD last_time_modified;
98 static RECT cursor_clip; /* Cursor clipping rect */
99 static XContext cursor_context;
100 static Cursor create_cursor( HANDLE handle );
102 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y );
105 /***********************************************************************
106 * X11DRV_Xcursor_Init
108 * Load the Xcursor library for use.
110 void X11DRV_Xcursor_Init(void)
112 #ifdef SONAME_LIBXCURSOR
113 xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
114 if (!xcursor_handle) /* wine_dlopen failed. */
116 WARN("Xcursor failed to load. Using fallback code.\n");
119 #define LOAD_FUNCPTR(f) \
120 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
122 LOAD_FUNCPTR(XcursorImageCreate);
123 LOAD_FUNCPTR(XcursorImageDestroy);
124 LOAD_FUNCPTR(XcursorImageLoadCursor);
125 LOAD_FUNCPTR(XcursorImagesCreate);
126 LOAD_FUNCPTR(XcursorImagesDestroy);
127 LOAD_FUNCPTR(XcursorImagesLoadCursor);
128 LOAD_FUNCPTR(XcursorLibraryLoadCursor);
130 #endif /* SONAME_LIBXCURSOR */
134 /***********************************************************************
137 * Clip point to the provided rectangle
139 static inline void clip_point_to_rect( LPCRECT rect, LPPOINT pt )
141 if (pt->x < rect->left) pt->x = rect->left;
142 else if (pt->x >= rect->right) pt->x = rect->right - 1;
143 if (pt->y < rect->top) pt->y = rect->top;
144 else if (pt->y >= rect->bottom) pt->y = rect->bottom - 1;
147 /***********************************************************************
148 * update_button_state
150 * Update the button state with what X provides us
152 static inline void update_button_state( unsigned int state )
154 key_state_table[VK_LBUTTON] = (state & Button1Mask ? 0x80 : 0);
155 key_state_table[VK_MBUTTON] = (state & Button2Mask ? 0x80 : 0);
156 key_state_table[VK_RBUTTON] = (state & Button3Mask ? 0x80 : 0);
157 /* X-buttons are not reported from XQueryPointer */
160 /***********************************************************************
163 static Cursor get_empty_cursor(void)
165 static Cursor cursor;
166 static const char data[] = { 0 };
174 bg.red = bg.green = bg.blue = 0x0000;
175 pixmap = XCreateBitmapFromData( gdi_display, root_window, data, 1, 1 );
178 cursor = XCreatePixmapCursor( gdi_display, pixmap, pixmap, &bg, &bg, 0, 0 );
179 XFreePixmap( gdi_display, pixmap );
186 /***********************************************************************
189 void set_window_cursor( HWND hwnd, HCURSOR handle )
191 struct x11drv_win_data *data;
194 if (!(data = X11DRV_get_win_data( hwnd ))) return;
197 if (!handle) cursor = get_empty_cursor();
198 else if (!cursor_context || XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
200 /* try to create it */
202 if (!(cursor = create_cursor( handle ))) return;
205 if (!cursor_context) cursor_context = XUniqueContext();
206 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&prev ))
208 /* someone else was here first */
209 XFreeCursor( gdi_display, cursor );
214 XSaveContext( gdi_display, (XID)handle, cursor_context, (char *)cursor );
215 TRACE( "cursor %p created %lx\n", handle, cursor );
219 XDefineCursor( gdi_display, data->whole_window, cursor );
220 /* make the change take effect immediately */
221 XFlush( gdi_display );
222 data->cursor = handle;
226 /***********************************************************************
229 * Update the various window states on a mouse event.
231 static HWND update_mouse_state( HWND hwnd, Window window, int x, int y, unsigned int state, POINT *pt )
233 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
237 if (window == data->whole_window)
239 x += data->whole_rect.left - data->client_rect.left;
240 y += data->whole_rect.top - data->client_rect.top;
244 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
245 pt->x = data->client_rect.right - data->client_rect.left - 1 - pt->x;
246 MapWindowPoints( hwnd, 0, pt, 1 );
248 cursor_window = hwnd;
249 if (hwnd != GetDesktopWindow()) hwnd = GetAncestor( hwnd, GA_ROOT );
251 /* update the wine server Z-order */
253 if (window != x11drv_thread_data()->grab_window &&
254 /* ignore event if a button is pressed, since the mouse is then grabbed too */
255 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
258 SetRect( &rect, pt->x, pt->y, pt->x + 1, pt->y + 1 );
259 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
261 SERVER_START_REQ( update_window_zorder )
263 req->window = wine_server_user_handle( hwnd );
264 req->rect.left = rect.left;
265 req->rect.top = rect.top;
266 req->rect.right = rect.right;
267 req->rect.bottom = rect.bottom;
268 wine_server_call( req );
276 /***********************************************************************
279 static WORD get_key_state(void)
283 if (GetSystemMetrics( SM_SWAPBUTTON ))
285 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
286 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
290 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
291 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
293 if (key_state_table[VK_MBUTTON] & 0x80) ret |= MK_MBUTTON;
294 if (key_state_table[VK_SHIFT] & 0x80) ret |= MK_SHIFT;
295 if (key_state_table[VK_CONTROL] & 0x80) ret |= MK_CONTROL;
296 if (key_state_table[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
297 if (key_state_table[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
302 /***********************************************************************
303 * queue_raw_mouse_message
305 static void queue_raw_mouse_message( UINT message, HWND top_hwnd, HWND cursor_hwnd, DWORD x, DWORD y,
306 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
313 hook.mouseData = MAKELONG( 0, data );
314 hook.flags = injected_flags;
316 hook.dwExtraInfo = extra_info;
318 last_time_modified = GetTickCount();
319 if (HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE ))
320 message = 0; /* ignore it */
322 SERVER_START_REQ( send_hardware_message )
324 req->id = (injected_flags & LLMHF_INJECTED) ? 0 : GetCurrentThreadId();
325 req->win = wine_server_user_handle( top_hwnd );
327 req->wparam = MAKEWPARAM( get_key_state(), data );
332 req->info = extra_info;
333 wine_server_call( req );
334 cursor = (reply->count >= 0) ? wine_server_ptr_handle(reply->cursor) : 0;
340 struct x11drv_win_data *data = X11DRV_get_win_data( cursor_hwnd );
341 if (data && cursor != data->cursor) set_window_cursor( cursor_hwnd, cursor );
346 /***********************************************************************
347 * X11DRV_send_mouse_input
349 void X11DRV_send_mouse_input( HWND top_hwnd, HWND cursor_hwnd, DWORD flags, DWORD x, DWORD y,
350 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
354 if (!time) time = GetTickCount();
356 if (flags & MOUSEEVENTF_MOVE && flags & MOUSEEVENTF_ABSOLUTE)
358 if (injected_flags & LLMHF_INJECTED)
360 pt.x = (x * screen_width) >> 16;
361 pt.y = (y * screen_height) >> 16;
368 if (cursor_pos.x == x && cursor_pos.y == y &&
369 (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE)))
370 flags &= ~MOUSEEVENTF_MOVE;
374 else if (flags & MOUSEEVENTF_MOVE)
376 int accel[3], xMult = 1, yMult = 1;
378 /* dx and dy can be negative numbers for relative movements */
379 SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
381 if (abs(x) > accel[0] && accel[2] != 0)
384 if ((abs(x) > accel[1]) && (accel[2] == 2)) xMult = 4;
386 if (abs(y) > accel[0] && accel[2] != 0)
389 if ((abs(y) > accel[1]) && (accel[2] == 2)) yMult = 4;
393 pt.x = cursor_pos.x + (long)x * xMult;
394 pt.y = cursor_pos.y + (long)y * yMult;
404 if (flags & MOUSEEVENTF_MOVE)
406 queue_raw_mouse_message( WM_MOUSEMOVE, top_hwnd, cursor_hwnd, pt.x, pt.y, data, time,
407 extra_info, injected_flags );
408 if ((injected_flags & LLMHF_INJECTED) &&
409 ((flags & MOUSEEVENTF_ABSOLUTE) || x || y)) /* we have to actually move the cursor */
411 X11DRV_SetCursorPos( pt.x, pt.y );
416 clip_point_to_rect( &cursor_clip, &pt);
421 if (flags & MOUSEEVENTF_LEFTDOWN)
423 key_state_table[VK_LBUTTON] |= 0xc0;
424 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
425 top_hwnd, cursor_hwnd, pt.x, pt.y,
426 data, time, extra_info, injected_flags );
428 if (flags & MOUSEEVENTF_LEFTUP)
430 key_state_table[VK_LBUTTON] &= ~0x80;
431 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONUP : WM_LBUTTONUP,
432 top_hwnd, cursor_hwnd, pt.x, pt.y,
433 data, time, extra_info, injected_flags );
435 if (flags & MOUSEEVENTF_RIGHTDOWN)
437 key_state_table[VK_RBUTTON] |= 0xc0;
438 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
439 top_hwnd, cursor_hwnd, pt.x, pt.y,
440 data, time, extra_info, injected_flags );
442 if (flags & MOUSEEVENTF_RIGHTUP)
444 key_state_table[VK_RBUTTON] &= ~0x80;
445 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONUP : WM_RBUTTONUP,
446 top_hwnd, cursor_hwnd, pt.x, pt.y,
447 data, time, extra_info, injected_flags );
449 if (flags & MOUSEEVENTF_MIDDLEDOWN)
451 key_state_table[VK_MBUTTON] |= 0xc0;
452 queue_raw_mouse_message( WM_MBUTTONDOWN, top_hwnd, cursor_hwnd, pt.x, pt.y,
453 data, time, extra_info, injected_flags );
455 if (flags & MOUSEEVENTF_MIDDLEUP)
457 key_state_table[VK_MBUTTON] &= ~0x80;
458 queue_raw_mouse_message( WM_MBUTTONUP, top_hwnd, cursor_hwnd, pt.x, pt.y,
459 data, time, extra_info, injected_flags );
461 if (flags & MOUSEEVENTF_WHEEL)
463 queue_raw_mouse_message( WM_MOUSEWHEEL, top_hwnd, cursor_hwnd, pt.x, pt.y,
464 data, time, extra_info, injected_flags );
466 if (flags & MOUSEEVENTF_XDOWN)
468 key_state_table[VK_XBUTTON1 + data - 1] |= 0xc0;
469 queue_raw_mouse_message( WM_XBUTTONDOWN, top_hwnd, cursor_hwnd, pt.x, pt.y,
470 data, time, extra_info, injected_flags );
472 if (flags & MOUSEEVENTF_XUP)
474 key_state_table[VK_XBUTTON1 + data - 1] &= ~0x80;
475 queue_raw_mouse_message( WM_XBUTTONUP, top_hwnd, cursor_hwnd, pt.x, pt.y,
476 data, time, extra_info, injected_flags );
480 #ifdef SONAME_LIBXCURSOR
482 /***********************************************************************
483 * create_xcursor_frame
485 * Use Xcursor to create a frame of an X cursor from a Windows one.
487 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
488 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
489 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
490 int width, int height, int istep )
492 XcursorImage *image, *ret = NULL;
493 int x, y, i, has_alpha;
497 image = pXcursorImageCreate( width, height );
501 ERR("X11 failed to produce a cursor frame!\n");
505 image->xhot = iinfo->xHotspot;
506 image->yhot = iinfo->yHotspot;
507 image->delay = 100; /* TODO: find a way to get the proper delay */
509 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
510 memset( color_bits, 0x00, color_size );
511 SelectObject( hdc, hbmColor );
512 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
514 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
517 memcpy( image->pixels, color_bits, color_size );
519 /* check if the cursor frame was drawn with an alpha channel */
520 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
521 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
523 /* if no alpha channel was drawn then generate it from the mask */
526 unsigned int width_bytes = (width + 31) / 32 * 4;
528 /* draw the cursor mask to a temporary buffer */
529 memset( mask_bits, 0xFF, mask_size );
530 SelectObject( hdc, hbmMask );
531 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
533 ERR("Failed to draw frame mask %d.\n", istep);
536 /* use the buffer to directly modify the XcursorImage alpha channel */
537 for (y = 0, ptr = image->pixels; y < height; y++)
538 for (x = 0; x < width; x++, ptr++)
539 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
545 if (ret == NULL) pXcursorImageDestroy( image );
549 /***********************************************************************
550 * create_xcursor_cursor
552 * Use Xcursor to create an X cursor from a Windows one.
554 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
556 unsigned char *color_bits, *mask_bits;
557 HBITMAP hbmColor = 0, hbmMask = 0;
558 XcursorImage **imgs, *image;
559 int color_size, mask_size;
560 BITMAPINFO *info = NULL;
561 XcursorImages *images;
565 if (!(imgs = HeapAlloc( GetProcessHeap(), 0, sizeof(XcursorImage*) ))) return 0;
567 /* Allocate all of the resources necessary to obtain a cursor frame */
568 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
569 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
570 info->bmiHeader.biWidth = width;
571 info->bmiHeader.biHeight = -height;
572 info->bmiHeader.biPlanes = 1;
573 info->bmiHeader.biCompression = BI_RGB;
574 info->bmiHeader.biXPelsPerMeter = 0;
575 info->bmiHeader.biYPelsPerMeter = 0;
576 info->bmiHeader.biClrUsed = 0;
577 info->bmiHeader.biClrImportant = 0;
578 info->bmiHeader.biBitCount = 32;
579 color_size = width * height * 4;
580 info->bmiHeader.biSizeImage = color_size;
581 hbmColor = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &color_bits, NULL, 0);
584 ERR("Failed to create DIB section for cursor color data!\n");
587 info->bmiHeader.biBitCount = 1;
588 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
589 info->bmiHeader.biSizeImage = mask_size;
590 hbmMask = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &mask_bits, NULL, 0);
593 ERR("Failed to create DIB section for cursor mask data!\n");
597 /* Create an XcursorImage for each frame of the cursor */
600 XcursorImage **imgstmp;
602 image = create_xcursor_frame( hdc, iinfo, icon,
603 hbmColor, color_bits, color_size,
604 hbmMask, mask_bits, mask_size,
605 width, height, nFrames );
606 if (!image) break; /* no more drawable frames */
608 imgs[nFrames++] = image;
609 if (!(imgstmp = HeapReAlloc( GetProcessHeap(), 0, imgs, (nFrames+1)*sizeof(XcursorImage*) ))) goto cleanup;
613 /* Build an X cursor out of all of the frames */
614 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
615 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
616 images->images[images->nimage] = imgs[images->nimage];
618 cursor = pXcursorImagesLoadCursor( gdi_display, images );
620 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
621 HeapFree( GetProcessHeap(), 0, imgs );
627 /* Failed to produce a cursor, free previously allocated frames */
628 for (nFrames--; nFrames >= 0; nFrames--)
629 pXcursorImageDestroy( imgs[nFrames] );
630 HeapFree( GetProcessHeap(), 0, imgs );
632 /* Cleanup all of the resources used to obtain the frame data */
633 if (hbmColor) DeleteObject( hbmColor );
634 if (hbmMask) DeleteObject( hbmMask );
635 HeapFree( GetProcessHeap(), 0, info );
640 struct system_cursors
646 static const struct system_cursors user32_cursors[] =
648 { OCR_NORMAL, "left_ptr" },
649 { OCR_IBEAM, "xterm" },
650 { OCR_WAIT, "watch" },
651 { OCR_CROSS, "cross" },
652 { OCR_UP, "center_ptr" },
653 { OCR_SIZE, "fleur" },
654 { OCR_SIZEALL, "fleur" },
655 { OCR_ICON, "icon" },
656 { OCR_SIZENWSE, "nwse-resize" },
657 { OCR_SIZENESW, "nesw-resize" },
658 { OCR_SIZEWE, "ew-resize" },
659 { OCR_SIZENS, "ns-resize" },
660 { OCR_NO, "not-allowed" },
661 { OCR_HAND, "hand2" },
662 { OCR_APPSTARTING, "left_ptr_watch" },
663 { OCR_HELP, "question_arrow" },
667 static const struct system_cursors comctl32_cursors[] =
672 { 106, "row-resize" },
673 { 107, "row-resize" },
675 { 135, "col-resize" },
679 static const struct system_cursors ole32_cursors[] =
688 static const struct system_cursors riched20_cursors[] =
691 { 107, "right_ptr" },
700 const struct system_cursors *cursors;
704 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
705 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
706 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
707 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
710 /***********************************************************************
711 * create_xcursor_system_cursor
713 * Create an X cursor for a system cursor.
715 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
717 static const WCHAR idW[] = {'%','h','u',0};
718 const struct system_cursors *cursors;
723 WCHAR *p, name[MAX_PATH * 2], valueW[64];
727 if (!pXcursorLibraryLoadCursor) return 0;
728 if (!info->szModName[0]) return 0;
730 p = strrchrW( info->szModName, '\\' );
731 strcpyW( name, p ? p + 1 : info->szModName );
732 p = name + strlenW( name );
734 if (info->szResName[0]) strcpyW( p, info->szResName );
735 else sprintfW( p, idW, info->wResID );
738 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
739 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver\\Cursors", &key ))
741 size = sizeof(valueW) / sizeof(WCHAR);
742 ret = RegQueryValueExW( key, name, NULL, NULL, (BYTE *)valueW, &size );
746 if (!valueW[0]) return 0; /* force standard cursor */
747 if (!WideCharToMultiByte( CP_UNIXCP, 0, valueW, -1, valueA, sizeof(valueA), NULL, NULL ))
753 if (info->szResName[0]) goto done; /* only integer resources are supported here */
754 if (!(module = GetModuleHandleW( info->szModName ))) goto done;
756 for (i = 0; i < sizeof(module_cursors)/sizeof(module_cursors[0]); i++)
757 if (GetModuleHandleW( module_cursors[i].name ) == module) break;
758 if (i == sizeof(module_cursors)/sizeof(module_cursors[0])) goto done;
760 cursors = module_cursors[i].cursors;
761 for (i = 0; cursors[i].id; i++)
762 if (cursors[i].id == info->wResID)
764 strcpy( valueA, cursors[i].name );
772 cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
774 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
775 debugstr_w(name), debugstr_a(valueA) );
777 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
781 #endif /* SONAME_LIBXCURSOR */
784 /***********************************************************************
785 * create_cursor_from_bitmaps
787 * Create an X11 cursor from source bitmaps.
789 static Cursor create_cursor_from_bitmaps( HBITMAP src_xor, HBITMAP src_and, int width, int height,
790 int xor_y, int and_y, XColor *fg, XColor *bg,
791 int hotspot_x, int hotspot_y )
793 HDC src = 0, dst = 0;
794 HBITMAP bits = 0, mask = 0, mask_inv = 0;
797 if (!(src = CreateCompatibleDC( 0 ))) goto done;
798 if (!(dst = CreateCompatibleDC( 0 ))) goto done;
800 if (!(bits = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
801 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
802 if (!(mask_inv = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
804 /* We have to do some magic here, as cursors are not fully
805 * compatible between Windows and X11. Under X11, there are
806 * only 3 possible color cursor: black, white and masked. So
807 * we map the 4th Windows color (invert the bits on the screen)
808 * to black and an additional white bit on an other place
809 * (+1,+1). This require some boolean arithmetic:
812 * And Xor Result | Bits Mask Result
813 * 0 0 black | 0 1 background
814 * 0 1 white | 1 1 foreground
815 * 1 0 no change | X 0 no change
816 * 1 1 inverted | 0 1 background
819 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
820 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
822 SelectObject( src, src_and );
823 SelectObject( dst, bits );
824 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
825 SelectObject( dst, mask );
826 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
827 SelectObject( dst, mask_inv );
828 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
829 SelectObject( src, src_xor );
830 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, SRCAND /* src & dst */ );
831 SelectObject( dst, bits );
832 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, SRCERASE /* src & ~dst */ );
833 SelectObject( dst, mask );
834 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, 0xdd0228 /* src | ~dst */ );
835 /* additional white */
836 SelectObject( src, mask_inv );
837 BitBlt( dst, 1, 1, width, height, src, 0, 0, SRCPAINT /* src | dst */);
838 SelectObject( dst, bits );
839 BitBlt( dst, 1, 1, width, height, src, 0, 0, SRCPAINT /* src | dst */ );
842 cursor = XCreatePixmapCursor( gdi_display, X11DRV_get_pixmap(bits), X11DRV_get_pixmap(mask),
843 fg, bg, hotspot_x, hotspot_y );
849 DeleteObject( bits );
850 DeleteObject( mask );
851 DeleteObject( mask_inv );
855 /***********************************************************************
858 * Create an X cursor from a Windows one.
860 static Cursor create_xlib_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
863 Cursor cursor = None;
864 HBITMAP xor_bitmap = 0;
866 unsigned int *color_bits = NULL, *ptr;
867 unsigned char *mask_bits = NULL, *xor_bits = NULL;
868 int i, x, y, has_alpha = 0;
869 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
870 unsigned int width_bytes = (width + 31) / 32 * 4;
872 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] ))))
874 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
875 info->bmiHeader.biWidth = width;
876 info->bmiHeader.biHeight = -height;
877 info->bmiHeader.biPlanes = 1;
878 info->bmiHeader.biBitCount = 1;
879 info->bmiHeader.biCompression = BI_RGB;
880 info->bmiHeader.biSizeImage = width_bytes * height;
881 info->bmiHeader.biXPelsPerMeter = 0;
882 info->bmiHeader.biYPelsPerMeter = 0;
883 info->bmiHeader.biClrUsed = 0;
884 info->bmiHeader.biClrImportant = 0;
886 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
887 if (!GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS )) goto done;
889 info->bmiHeader.biBitCount = 32;
890 info->bmiHeader.biSizeImage = width * height * 4;
891 if (!(color_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
892 if (!(xor_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, width_bytes * height ))) goto done;
893 GetDIBits( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS );
895 /* compute fg/bg color and xor bitmap based on average of the color values */
897 if (!(xor_bitmap = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
898 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
899 for (y = 0, ptr = color_bits; y < height; y++)
901 for (x = 0; x < width; x++, ptr++)
903 int red = (*ptr >> 16) & 0xff;
904 int green = (*ptr >> 8) & 0xff;
905 int blue = (*ptr >> 0) & 0xff;
906 if (red + green + blue > 0x40)
912 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
924 fg.red = rfg * 257 / fgBits;
925 fg.green = gfg * 257 / fgBits;
926 fg.blue = bfg * 257 / fgBits;
928 else fg.red = fg.green = fg.blue = 0;
929 bgBits = width * height - fgBits;
932 bg.red = rbg * 257 / bgBits;
933 bg.green = gbg * 257 / bgBits;
934 bg.blue = bbg * 257 / bgBits;
936 else bg.red = bg.green = bg.blue = 0;
938 info->bmiHeader.biBitCount = 1;
939 info->bmiHeader.biSizeImage = width_bytes * height;
940 SetDIBits( hdc, xor_bitmap, 0, height, xor_bits, info, DIB_RGB_COLORS );
942 /* generate mask from the alpha channel if we have one */
944 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
945 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
949 memset( mask_bits, 0, width_bytes * height );
950 for (y = 0, ptr = color_bits; y < height; y++)
951 for (x = 0; x < width; x++, ptr++)
952 if ((*ptr >> 24) > 25) /* more than 10% alpha */
953 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
955 info->bmiHeader.biBitCount = 1;
956 info->bmiHeader.biSizeImage = width_bytes * height;
957 SetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS );
960 cursor = XCreatePixmapCursor( gdi_display,
961 X11DRV_get_pixmap(xor_bitmap),
962 X11DRV_get_pixmap(icon->hbmMask),
963 &fg, &bg, icon->xHotspot, icon->yHotspot );
968 cursor = create_cursor_from_bitmaps( xor_bitmap, icon->hbmMask, width, height, 0, 0,
969 &fg, &bg, icon->xHotspot, icon->yHotspot );
973 DeleteObject( xor_bitmap );
974 HeapFree( GetProcessHeap(), 0, info );
975 HeapFree( GetProcessHeap(), 0, color_bits );
976 HeapFree( GetProcessHeap(), 0, xor_bits );
977 HeapFree( GetProcessHeap(), 0, mask_bits );
981 /***********************************************************************
984 * Create an X cursor from a Windows one.
986 static Cursor create_cursor( HANDLE handle )
992 if (!handle) return get_empty_cursor();
994 info.cbSize = sizeof(info);
995 if (!GetIconInfoExW( handle, &info )) return 0;
997 #ifdef SONAME_LIBXCURSOR
998 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
1000 DeleteObject( info.hbmColor );
1001 DeleteObject( info.hbmMask );
1006 GetObjectW( info.hbmMask, sizeof(bm), &bm );
1007 if (!info.hbmColor) bm.bmHeight /= 2;
1009 /* make sure hotspot is valid */
1010 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1012 info.xHotspot = bm.bmWidth / 2;
1013 info.yHotspot = bm.bmHeight / 2;
1018 HDC hdc = CreateCompatibleDC( 0 );
1021 #ifdef SONAME_LIBXCURSOR
1022 if (pXcursorImagesLoadCursor)
1023 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
1025 if (!cursor) cursor = create_xlib_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1027 DeleteObject( info.hbmColor );
1033 fg.red = fg.green = fg.blue = 0xffff;
1034 bg.red = bg.green = bg.blue = 0;
1035 cursor = create_cursor_from_bitmaps( info.hbmMask, info.hbmMask, bm.bmWidth, bm.bmHeight,
1036 bm.bmHeight, 0, &fg, &bg, info.xHotspot, info.yHotspot );
1039 DeleteObject( info.hbmMask );
1043 /***********************************************************************
1044 * DestroyCursorIcon (X11DRV.@)
1046 void CDECL X11DRV_DestroyCursorIcon( HCURSOR handle )
1051 if (cursor_context && !XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1053 TRACE( "%p xid %lx\n", handle, cursor );
1054 XFreeCursor( gdi_display, cursor );
1055 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1057 wine_tsx11_unlock();
1060 /***********************************************************************
1061 * SetCursor (X11DRV.@)
1063 void CDECL X11DRV_SetCursor( HCURSOR handle )
1065 if (cursor_window) SendNotifyMessageW( cursor_window, WM_X11DRV_SET_CURSOR, 0, (LPARAM)handle );
1068 /***********************************************************************
1069 * SetCursorPos (X11DRV.@)
1071 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
1073 Display *display = thread_init_display();
1076 TRACE( "warping to (%d,%d)\n", x, y );
1079 if (cursor_pos.x == x && cursor_pos.y == y)
1081 wine_tsx11_unlock();
1082 /* We still need to generate WM_MOUSEMOVE */
1083 queue_raw_mouse_message( WM_MOUSEMOVE, 0, 0, x, y, 0, GetCurrentTime(), 0, 0 );
1088 clip_point_to_rect( &cursor_clip, &pt);
1089 XWarpPointer( display, root_window, root_window, 0, 0, 0, 0,
1090 pt.x - virtual_screen_rect.left, pt.y - virtual_screen_rect.top );
1091 XFlush( display ); /* avoids bad mouse lag in games that do their own mouse warping */
1093 wine_tsx11_unlock();
1097 /***********************************************************************
1098 * GetCursorPos (X11DRV.@)
1100 BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
1102 Display *display = thread_init_display();
1104 int rootX, rootY, winX, winY;
1105 unsigned int xstate;
1108 if ((GetTickCount() - last_time_modified > 100) &&
1109 XQueryPointer( display, root_window, &root, &child,
1110 &rootX, &rootY, &winX, &winY, &xstate ))
1112 update_button_state( xstate );
1113 winX += virtual_screen_rect.left;
1114 winY += virtual_screen_rect.top;
1115 TRACE("pointer at (%d,%d)\n", winX, winY );
1116 cursor_pos.x = winX;
1117 cursor_pos.y = winY;
1120 wine_tsx11_unlock();
1125 /***********************************************************************
1126 * ClipCursor (X11DRV.@)
1128 * Set the cursor clipping rectangle.
1130 BOOL CDECL X11DRV_ClipCursor( LPCRECT clip )
1132 if (!IntersectRect( &cursor_clip, &virtual_screen_rect, clip ))
1133 cursor_clip = virtual_screen_rect;
1138 /***********************************************************************
1139 * X11DRV_ButtonPress
1141 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1143 XButtonEvent *event = &xev->xbutton;
1144 int buttonNum = event->button - 1;
1149 if (buttonNum >= NB_BUTTONS) return;
1154 wData = WHEEL_DELTA;
1157 wData = -WHEEL_DELTA;
1173 update_user_time( event->time );
1174 top_hwnd = update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1175 if (!top_hwnd) return;
1177 X11DRV_send_mouse_input( top_hwnd, hwnd,
1178 button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1179 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1183 /***********************************************************************
1184 * X11DRV_ButtonRelease
1186 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1188 XButtonEvent *event = &xev->xbutton;
1189 int buttonNum = event->button - 1;
1194 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1212 top_hwnd = update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1213 if (!top_hwnd) return;
1215 X11DRV_send_mouse_input( top_hwnd, hwnd,
1216 button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1217 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1221 /***********************************************************************
1222 * X11DRV_MotionNotify
1224 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1226 XMotionEvent *event = &xev->xmotion;
1230 TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);
1232 top_hwnd = update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1233 if (!top_hwnd) return;
1235 X11DRV_send_mouse_input( top_hwnd, hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1236 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1240 /***********************************************************************
1241 * X11DRV_EnterNotify
1243 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1245 XCrossingEvent *event = &xev->xcrossing;
1249 TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);
1251 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1252 if (event->window == x11drv_thread_data()->grab_window) return;
1254 /* simulate a mouse motion event */
1255 top_hwnd = update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1256 if (!top_hwnd) return;
1258 X11DRV_send_mouse_input( top_hwnd, hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1259 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );