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;
242 if (window == root_window)
244 x += virtual_screen_rect.left;
245 y += virtual_screen_rect.top;
249 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
250 pt->x = data->client_rect.right - data->client_rect.left - 1 - pt->x;
251 MapWindowPoints( hwnd, 0, pt, 1 );
253 cursor_window = hwnd;
254 if (hwnd != GetDesktopWindow()) hwnd = GetAncestor( hwnd, GA_ROOT );
256 /* update the wine server Z-order */
258 if (window != x11drv_thread_data()->grab_window &&
259 /* ignore event if a button is pressed, since the mouse is then grabbed too */
260 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
263 SetRect( &rect, pt->x, pt->y, pt->x + 1, pt->y + 1 );
264 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
266 SERVER_START_REQ( update_window_zorder )
268 req->window = wine_server_user_handle( hwnd );
269 req->rect.left = rect.left;
270 req->rect.top = rect.top;
271 req->rect.right = rect.right;
272 req->rect.bottom = rect.bottom;
273 wine_server_call( req );
281 /***********************************************************************
284 static WORD get_key_state(void)
288 if (GetSystemMetrics( SM_SWAPBUTTON ))
290 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
291 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
295 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
296 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
298 if (key_state_table[VK_MBUTTON] & 0x80) ret |= MK_MBUTTON;
299 if (key_state_table[VK_SHIFT] & 0x80) ret |= MK_SHIFT;
300 if (key_state_table[VK_CONTROL] & 0x80) ret |= MK_CONTROL;
301 if (key_state_table[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
302 if (key_state_table[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
307 /***********************************************************************
308 * queue_raw_mouse_message
310 static void queue_raw_mouse_message( UINT message, HWND top_hwnd, HWND cursor_hwnd, DWORD x, DWORD y,
311 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
318 hook.mouseData = MAKELONG( 0, data );
319 hook.flags = injected_flags;
321 hook.dwExtraInfo = extra_info;
323 last_time_modified = GetTickCount();
324 if (HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE ))
325 message = 0; /* ignore it */
327 SERVER_START_REQ( send_hardware_message )
329 req->id = (injected_flags & LLMHF_INJECTED) ? 0 : GetCurrentThreadId();
330 req->win = wine_server_user_handle( top_hwnd );
332 req->wparam = MAKEWPARAM( get_key_state(), data );
337 req->info = extra_info;
338 wine_server_call( req );
339 cursor = (reply->count >= 0) ? wine_server_ptr_handle(reply->cursor) : 0;
345 struct x11drv_win_data *data = X11DRV_get_win_data( cursor_hwnd );
346 if (data && cursor != data->cursor) set_window_cursor( cursor_hwnd, cursor );
351 /***********************************************************************
352 * X11DRV_send_mouse_input
354 void X11DRV_send_mouse_input( HWND top_hwnd, HWND cursor_hwnd, DWORD flags, DWORD x, DWORD y,
355 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
359 if (!time) time = GetTickCount();
361 if (flags & MOUSEEVENTF_MOVE && flags & MOUSEEVENTF_ABSOLUTE)
363 if (injected_flags & LLMHF_INJECTED)
365 pt.x = (x * screen_width) >> 16;
366 pt.y = (y * screen_height) >> 16;
373 if (cursor_pos.x == x && cursor_pos.y == y &&
374 (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE)))
375 flags &= ~MOUSEEVENTF_MOVE;
379 else if (flags & MOUSEEVENTF_MOVE)
381 int accel[3], xMult = 1, yMult = 1;
383 /* dx and dy can be negative numbers for relative movements */
384 SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
386 if (abs(x) > accel[0] && accel[2] != 0)
389 if ((abs(x) > accel[1]) && (accel[2] == 2)) xMult = 4;
391 if (abs(y) > accel[0] && accel[2] != 0)
394 if ((abs(y) > accel[1]) && (accel[2] == 2)) yMult = 4;
398 pt.x = cursor_pos.x + (long)x * xMult;
399 pt.y = cursor_pos.y + (long)y * yMult;
409 if (flags & MOUSEEVENTF_MOVE)
411 queue_raw_mouse_message( WM_MOUSEMOVE, top_hwnd, cursor_hwnd, pt.x, pt.y, data, time,
412 extra_info, injected_flags );
413 if ((injected_flags & LLMHF_INJECTED) &&
414 ((flags & MOUSEEVENTF_ABSOLUTE) || x || y)) /* we have to actually move the cursor */
416 X11DRV_SetCursorPos( pt.x, pt.y );
421 clip_point_to_rect( &cursor_clip, &pt);
426 if (flags & MOUSEEVENTF_LEFTDOWN)
428 key_state_table[VK_LBUTTON] |= 0xc0;
429 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
430 top_hwnd, cursor_hwnd, pt.x, pt.y,
431 data, time, extra_info, injected_flags );
433 if (flags & MOUSEEVENTF_LEFTUP)
435 key_state_table[VK_LBUTTON] &= ~0x80;
436 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONUP : WM_LBUTTONUP,
437 top_hwnd, cursor_hwnd, pt.x, pt.y,
438 data, time, extra_info, injected_flags );
440 if (flags & MOUSEEVENTF_RIGHTDOWN)
442 key_state_table[VK_RBUTTON] |= 0xc0;
443 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
444 top_hwnd, cursor_hwnd, pt.x, pt.y,
445 data, time, extra_info, injected_flags );
447 if (flags & MOUSEEVENTF_RIGHTUP)
449 key_state_table[VK_RBUTTON] &= ~0x80;
450 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONUP : WM_RBUTTONUP,
451 top_hwnd, cursor_hwnd, pt.x, pt.y,
452 data, time, extra_info, injected_flags );
454 if (flags & MOUSEEVENTF_MIDDLEDOWN)
456 key_state_table[VK_MBUTTON] |= 0xc0;
457 queue_raw_mouse_message( WM_MBUTTONDOWN, top_hwnd, cursor_hwnd, pt.x, pt.y,
458 data, time, extra_info, injected_flags );
460 if (flags & MOUSEEVENTF_MIDDLEUP)
462 key_state_table[VK_MBUTTON] &= ~0x80;
463 queue_raw_mouse_message( WM_MBUTTONUP, top_hwnd, cursor_hwnd, pt.x, pt.y,
464 data, time, extra_info, injected_flags );
466 if (flags & MOUSEEVENTF_WHEEL)
468 queue_raw_mouse_message( WM_MOUSEWHEEL, top_hwnd, cursor_hwnd, pt.x, pt.y,
469 data, time, extra_info, injected_flags );
471 if (flags & MOUSEEVENTF_XDOWN)
473 key_state_table[VK_XBUTTON1 + data - 1] |= 0xc0;
474 queue_raw_mouse_message( WM_XBUTTONDOWN, top_hwnd, cursor_hwnd, pt.x, pt.y,
475 data, time, extra_info, injected_flags );
477 if (flags & MOUSEEVENTF_XUP)
479 key_state_table[VK_XBUTTON1 + data - 1] &= ~0x80;
480 queue_raw_mouse_message( WM_XBUTTONUP, top_hwnd, cursor_hwnd, pt.x, pt.y,
481 data, time, extra_info, injected_flags );
485 #ifdef SONAME_LIBXCURSOR
487 /***********************************************************************
488 * create_xcursor_frame
490 * Use Xcursor to create a frame of an X cursor from a Windows one.
492 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
493 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
494 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
495 int width, int height, int istep )
497 XcursorImage *image, *ret = NULL;
498 int x, y, i, has_alpha;
502 image = pXcursorImageCreate( width, height );
506 ERR("X11 failed to produce a cursor frame!\n");
510 image->xhot = iinfo->xHotspot;
511 image->yhot = iinfo->yHotspot;
512 image->delay = 100; /* TODO: find a way to get the proper delay */
514 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
515 memset( color_bits, 0x00, color_size );
516 SelectObject( hdc, hbmColor );
517 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
519 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
522 memcpy( image->pixels, color_bits, color_size );
524 /* check if the cursor frame was drawn with an alpha channel */
525 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
526 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
528 /* if no alpha channel was drawn then generate it from the mask */
531 unsigned int width_bytes = (width + 31) / 32 * 4;
533 /* draw the cursor mask to a temporary buffer */
534 memset( mask_bits, 0xFF, mask_size );
535 SelectObject( hdc, hbmMask );
536 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
538 ERR("Failed to draw frame mask %d.\n", istep);
541 /* use the buffer to directly modify the XcursorImage alpha channel */
542 for (y = 0, ptr = image->pixels; y < height; y++)
543 for (x = 0; x < width; x++, ptr++)
544 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
550 if (ret == NULL) pXcursorImageDestroy( image );
554 /***********************************************************************
555 * create_xcursor_cursor
557 * Use Xcursor to create an X cursor from a Windows one.
559 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
561 unsigned char *color_bits, *mask_bits;
562 HBITMAP hbmColor = 0, hbmMask = 0;
563 XcursorImage **imgs, *image;
564 int color_size, mask_size;
565 BITMAPINFO *info = NULL;
566 XcursorImages *images;
570 if (!(imgs = HeapAlloc( GetProcessHeap(), 0, sizeof(XcursorImage*) ))) return 0;
572 /* Allocate all of the resources necessary to obtain a cursor frame */
573 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
574 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
575 info->bmiHeader.biWidth = width;
576 info->bmiHeader.biHeight = -height;
577 info->bmiHeader.biPlanes = 1;
578 info->bmiHeader.biCompression = BI_RGB;
579 info->bmiHeader.biXPelsPerMeter = 0;
580 info->bmiHeader.biYPelsPerMeter = 0;
581 info->bmiHeader.biClrUsed = 0;
582 info->bmiHeader.biClrImportant = 0;
583 info->bmiHeader.biBitCount = 32;
584 color_size = width * height * 4;
585 info->bmiHeader.biSizeImage = color_size;
586 hbmColor = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &color_bits, NULL, 0);
589 ERR("Failed to create DIB section for cursor color data!\n");
592 info->bmiHeader.biBitCount = 1;
593 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
594 info->bmiHeader.biSizeImage = mask_size;
595 hbmMask = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &mask_bits, NULL, 0);
598 ERR("Failed to create DIB section for cursor mask data!\n");
602 /* Create an XcursorImage for each frame of the cursor */
605 XcursorImage **imgstmp;
607 image = create_xcursor_frame( hdc, iinfo, icon,
608 hbmColor, color_bits, color_size,
609 hbmMask, mask_bits, mask_size,
610 width, height, nFrames );
611 if (!image) break; /* no more drawable frames */
613 imgs[nFrames++] = image;
614 if (!(imgstmp = HeapReAlloc( GetProcessHeap(), 0, imgs, (nFrames+1)*sizeof(XcursorImage*) ))) goto cleanup;
618 /* Build an X cursor out of all of the frames */
619 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
620 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
621 images->images[images->nimage] = imgs[images->nimage];
623 cursor = pXcursorImagesLoadCursor( gdi_display, images );
625 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
626 HeapFree( GetProcessHeap(), 0, imgs );
632 /* Failed to produce a cursor, free previously allocated frames */
633 for (nFrames--; nFrames >= 0; nFrames--)
634 pXcursorImageDestroy( imgs[nFrames] );
635 HeapFree( GetProcessHeap(), 0, imgs );
637 /* Cleanup all of the resources used to obtain the frame data */
638 if (hbmColor) DeleteObject( hbmColor );
639 if (hbmMask) DeleteObject( hbmMask );
640 HeapFree( GetProcessHeap(), 0, info );
645 struct system_cursors
651 static const struct system_cursors user32_cursors[] =
653 { OCR_NORMAL, "left_ptr" },
654 { OCR_IBEAM, "xterm" },
655 { OCR_WAIT, "watch" },
656 { OCR_CROSS, "cross" },
657 { OCR_UP, "center_ptr" },
658 { OCR_SIZE, "fleur" },
659 { OCR_SIZEALL, "fleur" },
660 { OCR_ICON, "icon" },
661 { OCR_SIZENWSE, "nwse-resize" },
662 { OCR_SIZENESW, "nesw-resize" },
663 { OCR_SIZEWE, "ew-resize" },
664 { OCR_SIZENS, "ns-resize" },
665 { OCR_NO, "not-allowed" },
666 { OCR_HAND, "hand2" },
667 { OCR_APPSTARTING, "left_ptr_watch" },
668 { OCR_HELP, "question_arrow" },
672 static const struct system_cursors comctl32_cursors[] =
677 { 106, "row-resize" },
678 { 107, "row-resize" },
680 { 135, "col-resize" },
684 static const struct system_cursors ole32_cursors[] =
693 static const struct system_cursors riched20_cursors[] =
696 { 107, "right_ptr" },
705 const struct system_cursors *cursors;
709 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
710 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
711 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
712 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
715 /***********************************************************************
716 * create_xcursor_system_cursor
718 * Create an X cursor for a system cursor.
720 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
722 static const WCHAR idW[] = {'%','h','u',0};
723 const struct system_cursors *cursors;
728 WCHAR *p, name[MAX_PATH * 2], valueW[64];
732 if (!pXcursorLibraryLoadCursor) return 0;
733 if (!info->szModName[0]) return 0;
735 p = strrchrW( info->szModName, '\\' );
736 strcpyW( name, p ? p + 1 : info->szModName );
737 p = name + strlenW( name );
739 if (info->szResName[0]) strcpyW( p, info->szResName );
740 else sprintfW( p, idW, info->wResID );
743 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
744 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver\\Cursors", &key ))
746 size = sizeof(valueW) / sizeof(WCHAR);
747 ret = RegQueryValueExW( key, name, NULL, NULL, (BYTE *)valueW, &size );
751 if (!valueW[0]) return 0; /* force standard cursor */
752 if (!WideCharToMultiByte( CP_UNIXCP, 0, valueW, -1, valueA, sizeof(valueA), NULL, NULL ))
758 if (info->szResName[0]) goto done; /* only integer resources are supported here */
759 if (!(module = GetModuleHandleW( info->szModName ))) goto done;
761 for (i = 0; i < sizeof(module_cursors)/sizeof(module_cursors[0]); i++)
762 if (GetModuleHandleW( module_cursors[i].name ) == module) break;
763 if (i == sizeof(module_cursors)/sizeof(module_cursors[0])) goto done;
765 cursors = module_cursors[i].cursors;
766 for (i = 0; cursors[i].id; i++)
767 if (cursors[i].id == info->wResID)
769 strcpy( valueA, cursors[i].name );
777 cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
779 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
780 debugstr_w(name), debugstr_a(valueA) );
782 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
786 #endif /* SONAME_LIBXCURSOR */
789 /***********************************************************************
790 * create_cursor_from_bitmaps
792 * Create an X11 cursor from source bitmaps.
794 static Cursor create_cursor_from_bitmaps( HBITMAP src_xor, HBITMAP src_and, int width, int height,
795 int xor_y, int and_y, XColor *fg, XColor *bg,
796 int hotspot_x, int hotspot_y )
798 HDC src = 0, dst = 0;
799 HBITMAP bits = 0, mask = 0, mask_inv = 0;
802 if (!(src = CreateCompatibleDC( 0 ))) goto done;
803 if (!(dst = CreateCompatibleDC( 0 ))) goto done;
805 if (!(bits = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
806 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
807 if (!(mask_inv = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
809 /* We have to do some magic here, as cursors are not fully
810 * compatible between Windows and X11. Under X11, there are
811 * only 3 possible color cursor: black, white and masked. So
812 * we map the 4th Windows color (invert the bits on the screen)
813 * to black and an additional white bit on an other place
814 * (+1,+1). This require some boolean arithmetic:
817 * And Xor Result | Bits Mask Result
818 * 0 0 black | 0 1 background
819 * 0 1 white | 1 1 foreground
820 * 1 0 no change | X 0 no change
821 * 1 1 inverted | 0 1 background
824 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
825 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
827 SelectObject( src, src_and );
828 SelectObject( dst, bits );
829 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
830 SelectObject( dst, mask );
831 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
832 SelectObject( dst, mask_inv );
833 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
834 SelectObject( src, src_xor );
835 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, SRCAND /* src & dst */ );
836 SelectObject( dst, bits );
837 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, SRCERASE /* src & ~dst */ );
838 SelectObject( dst, mask );
839 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, 0xdd0228 /* src | ~dst */ );
840 /* additional white */
841 SelectObject( src, mask_inv );
842 BitBlt( dst, 1, 1, width, height, src, 0, 0, SRCPAINT /* src | dst */);
843 SelectObject( dst, bits );
844 BitBlt( dst, 1, 1, width, height, src, 0, 0, SRCPAINT /* src | dst */ );
847 cursor = XCreatePixmapCursor( gdi_display, X11DRV_get_pixmap(bits), X11DRV_get_pixmap(mask),
848 fg, bg, hotspot_x, hotspot_y );
854 DeleteObject( bits );
855 DeleteObject( mask );
856 DeleteObject( mask_inv );
860 /***********************************************************************
863 * Create an X cursor from a Windows one.
865 static Cursor create_xlib_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
868 Cursor cursor = None;
869 HBITMAP xor_bitmap = 0;
871 unsigned int *color_bits = NULL, *ptr;
872 unsigned char *mask_bits = NULL, *xor_bits = NULL;
873 int i, x, y, has_alpha = 0;
874 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
875 unsigned int width_bytes = (width + 31) / 32 * 4;
877 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] ))))
879 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
880 info->bmiHeader.biWidth = width;
881 info->bmiHeader.biHeight = -height;
882 info->bmiHeader.biPlanes = 1;
883 info->bmiHeader.biBitCount = 1;
884 info->bmiHeader.biCompression = BI_RGB;
885 info->bmiHeader.biSizeImage = width_bytes * height;
886 info->bmiHeader.biXPelsPerMeter = 0;
887 info->bmiHeader.biYPelsPerMeter = 0;
888 info->bmiHeader.biClrUsed = 0;
889 info->bmiHeader.biClrImportant = 0;
891 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
892 if (!GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS )) goto done;
894 info->bmiHeader.biBitCount = 32;
895 info->bmiHeader.biSizeImage = width * height * 4;
896 if (!(color_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
897 if (!(xor_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, width_bytes * height ))) goto done;
898 GetDIBits( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS );
900 /* compute fg/bg color and xor bitmap based on average of the color values */
902 if (!(xor_bitmap = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
903 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
904 for (y = 0, ptr = color_bits; y < height; y++)
906 for (x = 0; x < width; x++, ptr++)
908 int red = (*ptr >> 16) & 0xff;
909 int green = (*ptr >> 8) & 0xff;
910 int blue = (*ptr >> 0) & 0xff;
911 if (red + green + blue > 0x40)
917 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
929 fg.red = rfg * 257 / fgBits;
930 fg.green = gfg * 257 / fgBits;
931 fg.blue = bfg * 257 / fgBits;
933 else fg.red = fg.green = fg.blue = 0;
934 bgBits = width * height - fgBits;
937 bg.red = rbg * 257 / bgBits;
938 bg.green = gbg * 257 / bgBits;
939 bg.blue = bbg * 257 / bgBits;
941 else bg.red = bg.green = bg.blue = 0;
943 info->bmiHeader.biBitCount = 1;
944 info->bmiHeader.biSizeImage = width_bytes * height;
945 SetDIBits( hdc, xor_bitmap, 0, height, xor_bits, info, DIB_RGB_COLORS );
947 /* generate mask from the alpha channel if we have one */
949 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
950 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
954 memset( mask_bits, 0, width_bytes * height );
955 for (y = 0, ptr = color_bits; y < height; y++)
956 for (x = 0; x < width; x++, ptr++)
957 if ((*ptr >> 24) > 25) /* more than 10% alpha */
958 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
960 info->bmiHeader.biBitCount = 1;
961 info->bmiHeader.biSizeImage = width_bytes * height;
962 SetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS );
965 cursor = XCreatePixmapCursor( gdi_display,
966 X11DRV_get_pixmap(xor_bitmap),
967 X11DRV_get_pixmap(icon->hbmMask),
968 &fg, &bg, icon->xHotspot, icon->yHotspot );
973 cursor = create_cursor_from_bitmaps( xor_bitmap, icon->hbmMask, width, height, 0, 0,
974 &fg, &bg, icon->xHotspot, icon->yHotspot );
978 DeleteObject( xor_bitmap );
979 HeapFree( GetProcessHeap(), 0, info );
980 HeapFree( GetProcessHeap(), 0, color_bits );
981 HeapFree( GetProcessHeap(), 0, xor_bits );
982 HeapFree( GetProcessHeap(), 0, mask_bits );
986 /***********************************************************************
989 * Create an X cursor from a Windows one.
991 static Cursor create_cursor( HANDLE handle )
997 if (!handle) return get_empty_cursor();
999 info.cbSize = sizeof(info);
1000 if (!GetIconInfoExW( handle, &info )) return 0;
1002 #ifdef SONAME_LIBXCURSOR
1003 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
1005 DeleteObject( info.hbmColor );
1006 DeleteObject( info.hbmMask );
1011 GetObjectW( info.hbmMask, sizeof(bm), &bm );
1012 if (!info.hbmColor) bm.bmHeight /= 2;
1014 /* make sure hotspot is valid */
1015 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1017 info.xHotspot = bm.bmWidth / 2;
1018 info.yHotspot = bm.bmHeight / 2;
1023 HDC hdc = CreateCompatibleDC( 0 );
1026 #ifdef SONAME_LIBXCURSOR
1027 if (pXcursorImagesLoadCursor)
1028 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
1030 if (!cursor) cursor = create_xlib_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1032 DeleteObject( info.hbmColor );
1038 fg.red = fg.green = fg.blue = 0xffff;
1039 bg.red = bg.green = bg.blue = 0;
1040 cursor = create_cursor_from_bitmaps( info.hbmMask, info.hbmMask, bm.bmWidth, bm.bmHeight,
1041 bm.bmHeight, 0, &fg, &bg, info.xHotspot, info.yHotspot );
1044 DeleteObject( info.hbmMask );
1048 /***********************************************************************
1049 * DestroyCursorIcon (X11DRV.@)
1051 void CDECL X11DRV_DestroyCursorIcon( HCURSOR handle )
1056 if (cursor_context && !XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1058 TRACE( "%p xid %lx\n", handle, cursor );
1059 XFreeCursor( gdi_display, cursor );
1060 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1062 wine_tsx11_unlock();
1065 /***********************************************************************
1066 * SetCursor (X11DRV.@)
1068 void CDECL X11DRV_SetCursor( HCURSOR handle )
1070 if (cursor_window) SendNotifyMessageW( cursor_window, WM_X11DRV_SET_CURSOR, 0, (LPARAM)handle );
1073 /***********************************************************************
1074 * SetCursorPos (X11DRV.@)
1076 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
1078 Display *display = thread_init_display();
1081 TRACE( "warping to (%d,%d)\n", x, y );
1084 if (cursor_pos.x == x && cursor_pos.y == y)
1086 wine_tsx11_unlock();
1087 /* We still need to generate WM_MOUSEMOVE */
1088 queue_raw_mouse_message( WM_MOUSEMOVE, 0, 0, x, y, 0, GetCurrentTime(), 0, 0 );
1093 clip_point_to_rect( &cursor_clip, &pt);
1094 XWarpPointer( display, root_window, root_window, 0, 0, 0, 0,
1095 pt.x - virtual_screen_rect.left, pt.y - virtual_screen_rect.top );
1096 XFlush( display ); /* avoids bad mouse lag in games that do their own mouse warping */
1098 wine_tsx11_unlock();
1102 /***********************************************************************
1103 * GetCursorPos (X11DRV.@)
1105 BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
1107 Display *display = thread_init_display();
1109 int rootX, rootY, winX, winY;
1110 unsigned int xstate;
1113 if ((GetTickCount() - last_time_modified > 100) &&
1114 XQueryPointer( display, root_window, &root, &child,
1115 &rootX, &rootY, &winX, &winY, &xstate ))
1117 update_button_state( xstate );
1118 winX += virtual_screen_rect.left;
1119 winY += virtual_screen_rect.top;
1120 TRACE("pointer at (%d,%d)\n", winX, winY );
1121 cursor_pos.x = winX;
1122 cursor_pos.y = winY;
1125 wine_tsx11_unlock();
1130 /***********************************************************************
1131 * ClipCursor (X11DRV.@)
1133 * Set the cursor clipping rectangle.
1135 BOOL CDECL X11DRV_ClipCursor( LPCRECT clip )
1137 if (!IntersectRect( &cursor_clip, &virtual_screen_rect, clip ))
1138 cursor_clip = virtual_screen_rect;
1143 /***********************************************************************
1144 * X11DRV_ButtonPress
1146 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1148 XButtonEvent *event = &xev->xbutton;
1149 int buttonNum = event->button - 1;
1154 if (buttonNum >= NB_BUTTONS) return;
1159 wData = WHEEL_DELTA;
1162 wData = -WHEEL_DELTA;
1178 update_user_time( event->time );
1179 top_hwnd = update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1180 if (!top_hwnd) return;
1182 X11DRV_send_mouse_input( top_hwnd, hwnd,
1183 button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1184 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1188 /***********************************************************************
1189 * X11DRV_ButtonRelease
1191 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1193 XButtonEvent *event = &xev->xbutton;
1194 int buttonNum = event->button - 1;
1199 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1217 top_hwnd = update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1218 if (!top_hwnd) return;
1220 X11DRV_send_mouse_input( top_hwnd, hwnd,
1221 button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1222 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1226 /***********************************************************************
1227 * X11DRV_MotionNotify
1229 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1231 XMotionEvent *event = &xev->xmotion;
1235 TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);
1237 top_hwnd = update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1238 if (!top_hwnd) return;
1240 X11DRV_send_mouse_input( top_hwnd, hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1241 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1245 /***********************************************************************
1246 * X11DRV_EnterNotify
1248 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1250 XCrossingEvent *event = &xev->xcrossing;
1254 TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);
1256 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1257 if (event->window == x11drv_thread_data()->grab_window) return;
1259 /* simulate a mouse motion event */
1260 top_hwnd = update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1261 if (!top_hwnd) return;
1263 X11DRV_send_mouse_input( top_hwnd, hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1264 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );