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 POINT cursor_pos
;
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 /***********************************************************************
150 static Cursor
get_empty_cursor(void)
152 static Cursor cursor
;
153 static const char data
[] = { 0 };
161 bg
.red
= bg
.green
= bg
.blue
= 0x0000;
162 pixmap
= XCreateBitmapFromData( gdi_display
, root_window
, data
, 1, 1 );
165 cursor
= XCreatePixmapCursor( gdi_display
, pixmap
, pixmap
, &bg
, &bg
, 0, 0 );
166 XFreePixmap( gdi_display
, pixmap
);
173 /***********************************************************************
176 void set_window_cursor( struct x11drv_win_data
*data
, HCURSOR handle
)
181 if (!handle
) cursor
= get_empty_cursor();
182 else if (!cursor_context
|| XFindContext( gdi_display
, (XID
)handle
, cursor_context
, (char **)&cursor
))
184 /* try to create it */
186 if (!(cursor
= create_cursor( handle
))) return;
189 if (!cursor_context
) cursor_context
= XUniqueContext();
190 if (!XFindContext( gdi_display
, (XID
)handle
, cursor_context
, (char **)&prev
))
192 /* someone else was here first */
193 XFreeCursor( gdi_display
, cursor
);
198 XSaveContext( gdi_display
, (XID
)handle
, cursor_context
, (char *)cursor
);
199 TRACE( "cursor %p created %lx\n", handle
, cursor
);
203 XDefineCursor( gdi_display
, data
->whole_window
, cursor
);
204 /* make the change take effect immediately */
205 XFlush( gdi_display
);
206 data
->cursor
= handle
;
210 /***********************************************************************
213 void sync_window_cursor( struct x11drv_win_data
*data
)
217 SERVER_START_REQ( set_cursor
)
220 wine_server_call( req
);
221 cursor
= reply
->prev_count
>= 0 ? wine_server_ptr_handle( reply
->prev_handle
) : 0;
225 if (data
->cursor
!= cursor
) set_window_cursor( data
, cursor
);
228 /***********************************************************************
231 * Update the various window states on a mouse event.
233 static HWND
update_mouse_state( HWND hwnd
, Window window
, int x
, int y
, unsigned int state
, POINT
*pt
)
235 struct x11drv_win_data
*data
= X11DRV_get_win_data( hwnd
);
239 if (window
== data
->whole_window
)
241 x
+= data
->whole_rect
.left
- data
->client_rect
.left
;
242 y
+= data
->whole_rect
.top
- data
->client_rect
.top
;
244 if (window
== root_window
)
246 x
+= virtual_screen_rect
.left
;
247 y
+= virtual_screen_rect
.top
;
251 if (GetWindowLongW( data
->hwnd
, GWL_EXSTYLE
) & WS_EX_LAYOUTRTL
)
252 pt
->x
= data
->client_rect
.right
- data
->client_rect
.left
- 1 - pt
->x
;
253 MapWindowPoints( hwnd
, 0, pt
, 1 );
255 if (InterlockedExchangePointer( (void **)&cursor_window
, hwnd
) != hwnd
||
256 GetTickCount() - last_time_modified
> 100)
258 cursor_window
= hwnd
;
259 sync_window_cursor( data
);
261 if (hwnd
!= GetDesktopWindow()) hwnd
= GetAncestor( hwnd
, GA_ROOT
);
263 /* update the wine server Z-order */
265 if (window
!= x11drv_thread_data()->grab_window
&&
266 /* ignore event if a button is pressed, since the mouse is then grabbed too */
267 !(state
& (Button1Mask
|Button2Mask
|Button3Mask
|Button4Mask
|Button5Mask
|Button6Mask
|Button7Mask
)))
270 SetRect( &rect
, pt
->x
, pt
->y
, pt
->x
+ 1, pt
->y
+ 1 );
271 MapWindowPoints( 0, hwnd
, (POINT
*)&rect
, 2 );
273 SERVER_START_REQ( update_window_zorder
)
275 req
->window
= wine_server_user_handle( hwnd
);
276 req
->rect
.left
= rect
.left
;
277 req
->rect
.top
= rect
.top
;
278 req
->rect
.right
= rect
.right
;
279 req
->rect
.bottom
= rect
.bottom
;
280 wine_server_call( req
);
288 /***********************************************************************
289 * queue_raw_mouse_message
291 static void queue_raw_mouse_message( UINT message
, HWND hwnd
, DWORD x
, DWORD y
,
292 DWORD data
, DWORD time
, DWORD extra_info
, UINT injected_flags
)
298 hook
.mouseData
= MAKELONG( 0, data
);
299 hook
.flags
= injected_flags
;
301 hook
.dwExtraInfo
= extra_info
;
303 last_time_modified
= GetTickCount();
304 if (HOOK_CallHooks( WH_MOUSE_LL
, HC_ACTION
, message
, (LPARAM
)&hook
, TRUE
))
305 message
= 0; /* ignore it */
307 SERVER_START_REQ( send_hardware_message
)
309 req
->win
= wine_server_user_handle( hwnd
);
311 req
->wparam
= MAKEWPARAM( 0, data
);
316 req
->info
= extra_info
;
317 wine_server_call( req
);
323 /***********************************************************************
324 * X11DRV_send_mouse_input
326 void X11DRV_send_mouse_input( HWND hwnd
, DWORD flags
, DWORD x
, DWORD y
,
327 DWORD data
, DWORD time
, DWORD extra_info
, UINT injected_flags
)
331 if (!time
) time
= GetTickCount();
333 if (flags
& MOUSEEVENTF_MOVE
&& flags
& MOUSEEVENTF_ABSOLUTE
)
335 if (injected_flags
& LLMHF_INJECTED
)
337 pt
.x
= (x
* screen_width
) >> 16;
338 pt
.y
= (y
* screen_height
) >> 16;
345 if (cursor_pos
.x
== x
&& cursor_pos
.y
== y
&&
346 (flags
& ~(MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
)))
347 flags
&= ~MOUSEEVENTF_MOVE
;
351 else if (flags
& MOUSEEVENTF_MOVE
)
353 int accel
[3], xMult
= 1, yMult
= 1;
355 /* dx and dy can be negative numbers for relative movements */
356 SystemParametersInfoW(SPI_GETMOUSE
, 0, accel
, 0);
358 if (abs(x
) > accel
[0] && accel
[2] != 0)
361 if ((abs(x
) > accel
[1]) && (accel
[2] == 2)) xMult
= 4;
363 if (abs(y
) > accel
[0] && accel
[2] != 0)
366 if ((abs(y
) > accel
[1]) && (accel
[2] == 2)) yMult
= 4;
370 pt
.x
= cursor_pos
.x
+ (long)x
* xMult
;
371 pt
.y
= cursor_pos
.y
+ (long)y
* yMult
;
381 if (flags
& MOUSEEVENTF_MOVE
)
383 queue_raw_mouse_message( WM_MOUSEMOVE
, hwnd
, pt
.x
, pt
.y
, data
, time
,
384 extra_info
, injected_flags
);
385 if ((injected_flags
& LLMHF_INJECTED
) &&
386 ((flags
& MOUSEEVENTF_ABSOLUTE
) || x
|| y
)) /* we have to actually move the cursor */
388 clip_point_to_rect( &cursor_clip
, &pt
);
389 X11DRV_SetCursorPos( pt
.x
, pt
.y
);
394 clip_point_to_rect( &cursor_clip
, &pt
);
399 if (flags
& MOUSEEVENTF_LEFTDOWN
)
400 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_RBUTTONDOWN
: WM_LBUTTONDOWN
,
401 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
402 if (flags
& MOUSEEVENTF_LEFTUP
)
403 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_RBUTTONUP
: WM_LBUTTONUP
,
404 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
405 if (flags
& MOUSEEVENTF_RIGHTDOWN
)
406 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_LBUTTONDOWN
: WM_RBUTTONDOWN
,
407 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
408 if (flags
& MOUSEEVENTF_RIGHTUP
)
409 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_LBUTTONUP
: WM_RBUTTONUP
,
410 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
411 if (flags
& MOUSEEVENTF_MIDDLEDOWN
)
412 queue_raw_mouse_message( WM_MBUTTONDOWN
, hwnd
, pt
.x
, pt
.y
,
413 data
, time
, extra_info
, injected_flags
);
414 if (flags
& MOUSEEVENTF_MIDDLEUP
)
415 queue_raw_mouse_message( WM_MBUTTONUP
, hwnd
, pt
.x
, pt
.y
,
416 data
, time
, extra_info
, injected_flags
);
417 if (flags
& MOUSEEVENTF_WHEEL
)
418 queue_raw_mouse_message( WM_MOUSEWHEEL
, hwnd
, pt
.x
, pt
.y
,
419 data
, time
, extra_info
, injected_flags
);
420 if (flags
& MOUSEEVENTF_XDOWN
)
421 queue_raw_mouse_message( WM_XBUTTONDOWN
, hwnd
, pt
.x
, pt
.y
,
422 data
, time
, extra_info
, injected_flags
);
423 if (flags
& MOUSEEVENTF_XUP
)
424 queue_raw_mouse_message( WM_XBUTTONUP
, hwnd
, pt
.x
, pt
.y
,
425 data
, time
, extra_info
, injected_flags
);
428 #ifdef SONAME_LIBXCURSOR
430 /***********************************************************************
431 * create_xcursor_frame
433 * Use Xcursor to create a frame of an X cursor from a Windows one.
435 static XcursorImage
*create_xcursor_frame( HDC hdc
, const ICONINFOEXW
*iinfo
, HANDLE icon
,
436 HBITMAP hbmColor
, unsigned char *color_bits
, int color_size
,
437 HBITMAP hbmMask
, unsigned char *mask_bits
, int mask_size
,
438 int width
, int height
, int istep
)
440 XcursorImage
*image
, *ret
= NULL
;
441 int x
, y
, i
, has_alpha
;
445 image
= pXcursorImageCreate( width
, height
);
449 ERR("X11 failed to produce a cursor frame!\n");
453 image
->xhot
= iinfo
->xHotspot
;
454 image
->yhot
= iinfo
->yHotspot
;
455 image
->delay
= 100; /* TODO: find a way to get the proper delay */
457 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
458 memset( color_bits
, 0x00, color_size
);
459 SelectObject( hdc
, hbmColor
);
460 if (!DrawIconEx( hdc
, 0, 0, icon
, width
, height
, istep
, NULL
, DI_NORMAL
))
462 TRACE("Could not draw frame %d (walk past end of frames).\n", istep
);
465 memcpy( image
->pixels
, color_bits
, color_size
);
467 /* check if the cursor frame was drawn with an alpha channel */
468 for (i
= 0, ptr
= image
->pixels
; i
< width
* height
; i
++, ptr
++)
469 if ((has_alpha
= (*ptr
& 0xff000000) != 0)) break;
471 /* if no alpha channel was drawn then generate it from the mask */
474 unsigned int width_bytes
= (width
+ 31) / 32 * 4;
476 /* draw the cursor mask to a temporary buffer */
477 memset( mask_bits
, 0xFF, mask_size
);
478 SelectObject( hdc
, hbmMask
);
479 if (!DrawIconEx( hdc
, 0, 0, icon
, width
, height
, istep
, NULL
, DI_MASK
))
481 ERR("Failed to draw frame mask %d.\n", istep
);
484 /* use the buffer to directly modify the XcursorImage alpha channel */
485 for (y
= 0, ptr
= image
->pixels
; y
< height
; y
++)
486 for (x
= 0; x
< width
; x
++, ptr
++)
487 if (!((mask_bits
[y
* width_bytes
+ x
/ 8] << (x
% 8)) & 0x80))
493 if (ret
== NULL
) pXcursorImageDestroy( image
);
497 /***********************************************************************
498 * create_xcursor_cursor
500 * Use Xcursor to create an X cursor from a Windows one.
502 static Cursor
create_xcursor_cursor( HDC hdc
, const ICONINFOEXW
*iinfo
, HANDLE icon
, int width
, int height
)
504 unsigned char *color_bits
, *mask_bits
;
505 HBITMAP hbmColor
= 0, hbmMask
= 0;
506 XcursorImage
**imgs
, *image
;
507 int color_size
, mask_size
;
508 BITMAPINFO
*info
= NULL
;
509 XcursorImages
*images
;
513 if (!(imgs
= HeapAlloc( GetProcessHeap(), 0, sizeof(XcursorImage
*) ))) return 0;
515 /* Allocate all of the resources necessary to obtain a cursor frame */
516 if (!(info
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )))) goto cleanup
;
517 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
518 info
->bmiHeader
.biWidth
= width
;
519 info
->bmiHeader
.biHeight
= -height
;
520 info
->bmiHeader
.biPlanes
= 1;
521 info
->bmiHeader
.biCompression
= BI_RGB
;
522 info
->bmiHeader
.biXPelsPerMeter
= 0;
523 info
->bmiHeader
.biYPelsPerMeter
= 0;
524 info
->bmiHeader
.biClrUsed
= 0;
525 info
->bmiHeader
.biClrImportant
= 0;
526 info
->bmiHeader
.biBitCount
= 32;
527 color_size
= width
* height
* 4;
528 info
->bmiHeader
.biSizeImage
= color_size
;
529 hbmColor
= CreateDIBSection( hdc
, info
, DIB_RGB_COLORS
, (VOID
**) &color_bits
, NULL
, 0);
532 ERR("Failed to create DIB section for cursor color data!\n");
535 info
->bmiHeader
.biBitCount
= 1;
536 mask_size
= ((width
+ 31) / 32 * 4) * height
; /* width_bytes * height */
537 info
->bmiHeader
.biSizeImage
= mask_size
;
538 hbmMask
= CreateDIBSection( hdc
, info
, DIB_RGB_COLORS
, (VOID
**) &mask_bits
, NULL
, 0);
541 ERR("Failed to create DIB section for cursor mask data!\n");
545 /* Create an XcursorImage for each frame of the cursor */
548 XcursorImage
**imgstmp
;
550 image
= create_xcursor_frame( hdc
, iinfo
, icon
,
551 hbmColor
, color_bits
, color_size
,
552 hbmMask
, mask_bits
, mask_size
,
553 width
, height
, nFrames
);
554 if (!image
) break; /* no more drawable frames */
556 imgs
[nFrames
++] = image
;
557 if (!(imgstmp
= HeapReAlloc( GetProcessHeap(), 0, imgs
, (nFrames
+1)*sizeof(XcursorImage
*) ))) goto cleanup
;
561 /* Build an X cursor out of all of the frames */
562 if (!(images
= pXcursorImagesCreate( nFrames
))) goto cleanup
;
563 for (images
->nimage
= 0; images
->nimage
< nFrames
; images
->nimage
++)
564 images
->images
[images
->nimage
] = imgs
[images
->nimage
];
566 cursor
= pXcursorImagesLoadCursor( gdi_display
, images
);
568 pXcursorImagesDestroy( images
); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
569 HeapFree( GetProcessHeap(), 0, imgs
);
575 /* Failed to produce a cursor, free previously allocated frames */
576 for (nFrames
--; nFrames
>= 0; nFrames
--)
577 pXcursorImageDestroy( imgs
[nFrames
] );
578 HeapFree( GetProcessHeap(), 0, imgs
);
580 /* Cleanup all of the resources used to obtain the frame data */
581 if (hbmColor
) DeleteObject( hbmColor
);
582 if (hbmMask
) DeleteObject( hbmMask
);
583 HeapFree( GetProcessHeap(), 0, info
);
588 struct system_cursors
594 static const struct system_cursors user32_cursors
[] =
596 { OCR_NORMAL
, "left_ptr" },
597 { OCR_IBEAM
, "xterm" },
598 { OCR_WAIT
, "watch" },
599 { OCR_CROSS
, "cross" },
600 { OCR_UP
, "center_ptr" },
601 { OCR_SIZE
, "fleur" },
602 { OCR_SIZEALL
, "fleur" },
603 { OCR_ICON
, "icon" },
604 { OCR_SIZENWSE
, "nwse-resize" },
605 { OCR_SIZENESW
, "nesw-resize" },
606 { OCR_SIZEWE
, "ew-resize" },
607 { OCR_SIZENS
, "ns-resize" },
608 { OCR_NO
, "not-allowed" },
609 { OCR_HAND
, "hand2" },
610 { OCR_APPSTARTING
, "left_ptr_watch" },
611 { OCR_HELP
, "question_arrow" },
615 static const struct system_cursors comctl32_cursors
[] =
620 { 106, "row-resize" },
621 { 107, "row-resize" },
623 { 135, "col-resize" },
627 static const struct system_cursors ole32_cursors
[] =
636 static const struct system_cursors riched20_cursors
[] =
639 { 107, "right_ptr" },
648 const struct system_cursors
*cursors
;
652 { user32_cursors
, {'u','s','e','r','3','2','.','d','l','l',0} },
653 { comctl32_cursors
, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
654 { ole32_cursors
, {'o','l','e','3','2','.','d','l','l',0} },
655 { riched20_cursors
, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
658 /***********************************************************************
659 * create_xcursor_system_cursor
661 * Create an X cursor for a system cursor.
663 static Cursor
create_xcursor_system_cursor( const ICONINFOEXW
*info
)
665 static const WCHAR idW
[] = {'%','h','u',0};
666 const struct system_cursors
*cursors
;
671 WCHAR
*p
, name
[MAX_PATH
* 2], valueW
[64];
675 if (!pXcursorLibraryLoadCursor
) return 0;
676 if (!info
->szModName
[0]) return 0;
678 p
= strrchrW( info
->szModName
, '\\' );
679 strcpyW( name
, p
? p
+ 1 : info
->szModName
);
680 p
= name
+ strlenW( name
);
682 if (info
->szResName
[0]) strcpyW( p
, info
->szResName
);
683 else sprintfW( p
, idW
, info
->wResID
);
686 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
687 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\X11 Driver\\Cursors", &key
))
689 size
= sizeof(valueW
) / sizeof(WCHAR
);
690 ret
= RegQueryValueExW( key
, name
, NULL
, NULL
, (BYTE
*)valueW
, &size
);
694 if (!valueW
[0]) return 0; /* force standard cursor */
695 if (!WideCharToMultiByte( CP_UNIXCP
, 0, valueW
, -1, valueA
, sizeof(valueA
), NULL
, NULL
))
701 if (info
->szResName
[0]) goto done
; /* only integer resources are supported here */
702 if (!(module
= GetModuleHandleW( info
->szModName
))) goto done
;
704 for (i
= 0; i
< sizeof(module_cursors
)/sizeof(module_cursors
[0]); i
++)
705 if (GetModuleHandleW( module_cursors
[i
].name
) == module
) break;
706 if (i
== sizeof(module_cursors
)/sizeof(module_cursors
[0])) goto done
;
708 cursors
= module_cursors
[i
].cursors
;
709 for (i
= 0; cursors
[i
].id
; i
++)
710 if (cursors
[i
].id
== info
->wResID
)
712 strcpy( valueA
, cursors
[i
].name
);
720 cursor
= pXcursorLibraryLoadCursor( gdi_display
, valueA
);
722 if (!cursor
) WARN( "no system cursor found for %s mapped to %s\n",
723 debugstr_w(name
), debugstr_a(valueA
) );
725 else WARN( "no system cursor found for %s\n", debugstr_w(name
) );
729 #endif /* SONAME_LIBXCURSOR */
732 /***********************************************************************
733 * create_cursor_from_bitmaps
735 * Create an X11 cursor from source bitmaps.
737 static Cursor
create_cursor_from_bitmaps( HBITMAP src_xor
, HBITMAP src_and
, int width
, int height
,
738 int xor_y
, int and_y
, XColor
*fg
, XColor
*bg
,
739 int hotspot_x
, int hotspot_y
)
741 HDC src
= 0, dst
= 0;
742 HBITMAP bits
= 0, mask
= 0, mask_inv
= 0;
745 if (!(src
= CreateCompatibleDC( 0 ))) goto done
;
746 if (!(dst
= CreateCompatibleDC( 0 ))) goto done
;
748 if (!(bits
= CreateBitmap( width
, height
, 1, 1, NULL
))) goto done
;
749 if (!(mask
= CreateBitmap( width
, height
, 1, 1, NULL
))) goto done
;
750 if (!(mask_inv
= CreateBitmap( width
, height
, 1, 1, NULL
))) goto done
;
752 /* We have to do some magic here, as cursors are not fully
753 * compatible between Windows and X11. Under X11, there are
754 * only 3 possible color cursor: black, white and masked. So
755 * we map the 4th Windows color (invert the bits on the screen)
756 * to black and an additional white bit on an other place
757 * (+1,+1). This require some boolean arithmetic:
760 * And Xor Result | Bits Mask Result
761 * 0 0 black | 0 1 background
762 * 0 1 white | 1 1 foreground
763 * 1 0 no change | X 0 no change
764 * 1 1 inverted | 0 1 background
767 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
768 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
770 SelectObject( src
, src_and
);
771 SelectObject( dst
, bits
);
772 BitBlt( dst
, 0, 0, width
, height
, src
, 0, and_y
, SRCCOPY
);
773 SelectObject( dst
, mask
);
774 BitBlt( dst
, 0, 0, width
, height
, src
, 0, and_y
, SRCCOPY
);
775 SelectObject( dst
, mask_inv
);
776 BitBlt( dst
, 0, 0, width
, height
, src
, 0, and_y
, SRCCOPY
);
777 SelectObject( src
, src_xor
);
778 BitBlt( dst
, 0, 0, width
, height
, src
, 0, xor_y
, SRCAND
/* src & dst */ );
779 SelectObject( dst
, bits
);
780 BitBlt( dst
, 0, 0, width
, height
, src
, 0, xor_y
, SRCERASE
/* src & ~dst */ );
781 SelectObject( dst
, mask
);
782 BitBlt( dst
, 0, 0, width
, height
, src
, 0, xor_y
, 0xdd0228 /* src | ~dst */ );
783 /* additional white */
784 SelectObject( src
, mask_inv
);
785 BitBlt( dst
, 1, 1, width
, height
, src
, 0, 0, SRCPAINT
/* src | dst */);
786 SelectObject( dst
, bits
);
787 BitBlt( dst
, 1, 1, width
, height
, src
, 0, 0, SRCPAINT
/* src | dst */ );
790 cursor
= XCreatePixmapCursor( gdi_display
, X11DRV_get_pixmap(bits
), X11DRV_get_pixmap(mask
),
791 fg
, bg
, hotspot_x
, hotspot_y
);
797 DeleteObject( bits
);
798 DeleteObject( mask
);
799 DeleteObject( mask_inv
);
803 /***********************************************************************
806 * Create an X cursor from a Windows one.
808 static Cursor
create_xlib_cursor( HDC hdc
, const ICONINFOEXW
*icon
, int width
, int height
)
811 Cursor cursor
= None
;
812 HBITMAP xor_bitmap
= 0;
814 unsigned int *color_bits
= NULL
, *ptr
;
815 unsigned char *mask_bits
= NULL
, *xor_bits
= NULL
;
816 int i
, x
, y
, has_alpha
= 0;
817 int rfg
, gfg
, bfg
, rbg
, gbg
, bbg
, fgBits
, bgBits
;
818 unsigned int width_bytes
= (width
+ 31) / 32 * 4;
820 if (!(info
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] ))))
822 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
823 info
->bmiHeader
.biWidth
= width
;
824 info
->bmiHeader
.biHeight
= -height
;
825 info
->bmiHeader
.biPlanes
= 1;
826 info
->bmiHeader
.biBitCount
= 1;
827 info
->bmiHeader
.biCompression
= BI_RGB
;
828 info
->bmiHeader
.biSizeImage
= width_bytes
* height
;
829 info
->bmiHeader
.biXPelsPerMeter
= 0;
830 info
->bmiHeader
.biYPelsPerMeter
= 0;
831 info
->bmiHeader
.biClrUsed
= 0;
832 info
->bmiHeader
.biClrImportant
= 0;
834 if (!(mask_bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
))) goto done
;
835 if (!GetDIBits( hdc
, icon
->hbmMask
, 0, height
, mask_bits
, info
, DIB_RGB_COLORS
)) goto done
;
837 info
->bmiHeader
.biBitCount
= 32;
838 info
->bmiHeader
.biSizeImage
= width
* height
* 4;
839 if (!(color_bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
))) goto done
;
840 if (!(xor_bits
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, width_bytes
* height
))) goto done
;
841 GetDIBits( hdc
, icon
->hbmColor
, 0, height
, color_bits
, info
, DIB_RGB_COLORS
);
843 /* compute fg/bg color and xor bitmap based on average of the color values */
845 if (!(xor_bitmap
= CreateBitmap( width
, height
, 1, 1, NULL
))) goto done
;
846 rfg
= gfg
= bfg
= rbg
= gbg
= bbg
= fgBits
= 0;
847 for (y
= 0, ptr
= color_bits
; y
< height
; y
++)
849 for (x
= 0; x
< width
; x
++, ptr
++)
851 int red
= (*ptr
>> 16) & 0xff;
852 int green
= (*ptr
>> 8) & 0xff;
853 int blue
= (*ptr
>> 0) & 0xff;
854 if (red
+ green
+ blue
> 0x40)
860 xor_bits
[y
* width_bytes
+ x
/ 8] |= 0x80 >> (x
% 8);
872 fg
.red
= rfg
* 257 / fgBits
;
873 fg
.green
= gfg
* 257 / fgBits
;
874 fg
.blue
= bfg
* 257 / fgBits
;
876 else fg
.red
= fg
.green
= fg
.blue
= 0;
877 bgBits
= width
* height
- fgBits
;
880 bg
.red
= rbg
* 257 / bgBits
;
881 bg
.green
= gbg
* 257 / bgBits
;
882 bg
.blue
= bbg
* 257 / bgBits
;
884 else bg
.red
= bg
.green
= bg
.blue
= 0;
886 info
->bmiHeader
.biBitCount
= 1;
887 info
->bmiHeader
.biSizeImage
= width_bytes
* height
;
888 SetDIBits( hdc
, xor_bitmap
, 0, height
, xor_bits
, info
, DIB_RGB_COLORS
);
890 /* generate mask from the alpha channel if we have one */
892 for (i
= 0, ptr
= color_bits
; i
< width
* height
; i
++, ptr
++)
893 if ((has_alpha
= (*ptr
& 0xff000000) != 0)) break;
897 memset( mask_bits
, 0, width_bytes
* height
);
898 for (y
= 0, ptr
= color_bits
; y
< height
; y
++)
899 for (x
= 0; x
< width
; x
++, ptr
++)
900 if ((*ptr
>> 24) > 25) /* more than 10% alpha */
901 mask_bits
[y
* width_bytes
+ x
/ 8] |= 0x80 >> (x
% 8);
903 info
->bmiHeader
.biBitCount
= 1;
904 info
->bmiHeader
.biSizeImage
= width_bytes
* height
;
905 SetDIBits( hdc
, icon
->hbmMask
, 0, height
, mask_bits
, info
, DIB_RGB_COLORS
);
908 cursor
= XCreatePixmapCursor( gdi_display
,
909 X11DRV_get_pixmap(xor_bitmap
),
910 X11DRV_get_pixmap(icon
->hbmMask
),
911 &fg
, &bg
, icon
->xHotspot
, icon
->yHotspot
);
916 cursor
= create_cursor_from_bitmaps( xor_bitmap
, icon
->hbmMask
, width
, height
, 0, 0,
917 &fg
, &bg
, icon
->xHotspot
, icon
->yHotspot
);
921 DeleteObject( xor_bitmap
);
922 HeapFree( GetProcessHeap(), 0, info
);
923 HeapFree( GetProcessHeap(), 0, color_bits
);
924 HeapFree( GetProcessHeap(), 0, xor_bits
);
925 HeapFree( GetProcessHeap(), 0, mask_bits
);
929 /***********************************************************************
932 * Create an X cursor from a Windows one.
934 static Cursor
create_cursor( HANDLE handle
)
940 if (!handle
) return get_empty_cursor();
942 info
.cbSize
= sizeof(info
);
943 if (!GetIconInfoExW( handle
, &info
)) return 0;
945 #ifdef SONAME_LIBXCURSOR
946 if (use_system_cursors
&& (cursor
= create_xcursor_system_cursor( &info
)))
948 DeleteObject( info
.hbmColor
);
949 DeleteObject( info
.hbmMask
);
954 GetObjectW( info
.hbmMask
, sizeof(bm
), &bm
);
955 if (!info
.hbmColor
) bm
.bmHeight
/= 2;
957 /* make sure hotspot is valid */
958 if (info
.xHotspot
>= bm
.bmWidth
|| info
.yHotspot
>= bm
.bmHeight
)
960 info
.xHotspot
= bm
.bmWidth
/ 2;
961 info
.yHotspot
= bm
.bmHeight
/ 2;
966 HDC hdc
= CreateCompatibleDC( 0 );
969 #ifdef SONAME_LIBXCURSOR
970 if (pXcursorImagesLoadCursor
)
971 cursor
= create_xcursor_cursor( hdc
, &info
, handle
, bm
.bmWidth
, bm
.bmHeight
);
973 if (!cursor
) cursor
= create_xlib_cursor( hdc
, &info
, bm
.bmWidth
, bm
.bmHeight
);
975 DeleteObject( info
.hbmColor
);
981 fg
.red
= fg
.green
= fg
.blue
= 0xffff;
982 bg
.red
= bg
.green
= bg
.blue
= 0;
983 cursor
= create_cursor_from_bitmaps( info
.hbmMask
, info
.hbmMask
, bm
.bmWidth
, bm
.bmHeight
,
984 bm
.bmHeight
, 0, &fg
, &bg
, info
.xHotspot
, info
.yHotspot
);
987 DeleteObject( info
.hbmMask
);
991 /***********************************************************************
992 * DestroyCursorIcon (X11DRV.@)
994 void CDECL
X11DRV_DestroyCursorIcon( HCURSOR handle
)
999 if (cursor_context
&& !XFindContext( gdi_display
, (XID
)handle
, cursor_context
, (char **)&cursor
))
1001 TRACE( "%p xid %lx\n", handle
, cursor
);
1002 XFreeCursor( gdi_display
, cursor
);
1003 XDeleteContext( gdi_display
, (XID
)handle
, cursor_context
);
1005 wine_tsx11_unlock();
1008 /***********************************************************************
1009 * SetCursor (X11DRV.@)
1011 void CDECL
X11DRV_SetCursor( HCURSOR handle
)
1013 if (cursor_window
) SendNotifyMessageW( cursor_window
, WM_X11DRV_SET_CURSOR
, 0, (LPARAM
)handle
);
1016 /***********************************************************************
1017 * SetCursorPos (X11DRV.@)
1019 BOOL CDECL
X11DRV_SetCursorPos( INT x
, INT y
)
1021 Display
*display
= thread_init_display();
1023 TRACE( "warping to (%d,%d)\n", x
, y
);
1026 XWarpPointer( display
, root_window
, root_window
, 0, 0, 0, 0,
1027 x
- virtual_screen_rect
.left
, y
- virtual_screen_rect
.top
);
1028 XFlush( display
); /* avoids bad mouse lag in games that do their own mouse warping */
1031 wine_tsx11_unlock();
1035 /***********************************************************************
1036 * GetCursorPos (X11DRV.@)
1038 BOOL CDECL
X11DRV_GetCursorPos(LPPOINT pos
)
1040 Display
*display
= thread_init_display();
1042 int rootX
, rootY
, winX
, winY
;
1043 unsigned int xstate
;
1047 if ((GetTickCount() - last_time_modified
> 100) &&
1048 XQueryPointer( display
, root_window
, &root
, &child
,
1049 &rootX
, &rootY
, &winX
, &winY
, &xstate
))
1051 winX
+= virtual_screen_rect
.left
;
1052 winY
+= virtual_screen_rect
.top
;
1053 TRACE("pointer at (%d,%d)\n", winX
, winY
);
1058 wine_tsx11_unlock();
1063 /***********************************************************************
1064 * ClipCursor (X11DRV.@)
1066 * Set the cursor clipping rectangle.
1068 BOOL CDECL
X11DRV_ClipCursor( LPCRECT clip
)
1070 if (!IntersectRect( &cursor_clip
, &virtual_screen_rect
, clip
))
1071 cursor_clip
= virtual_screen_rect
;
1076 /***********************************************************************
1077 * X11DRV_ButtonPress
1079 void X11DRV_ButtonPress( HWND hwnd
, XEvent
*xev
)
1081 XButtonEvent
*event
= &xev
->xbutton
;
1082 int buttonNum
= event
->button
- 1;
1086 if (buttonNum
>= NB_BUTTONS
) return;
1091 wData
= WHEEL_DELTA
;
1094 wData
= -WHEEL_DELTA
;
1110 update_user_time( event
->time
);
1111 hwnd
= update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
1114 X11DRV_send_mouse_input( hwnd
, button_down_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
,
1115 pt
.x
, pt
.y
, wData
, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );
1119 /***********************************************************************
1120 * X11DRV_ButtonRelease
1122 void X11DRV_ButtonRelease( HWND hwnd
, XEvent
*xev
)
1124 XButtonEvent
*event
= &xev
->xbutton
;
1125 int buttonNum
= event
->button
- 1;
1129 if (buttonNum
>= NB_BUTTONS
|| !button_up_flags
[buttonNum
]) return;
1147 hwnd
= update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
1150 X11DRV_send_mouse_input( hwnd
, button_up_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
,
1151 pt
.x
, pt
.y
, wData
, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );
1155 /***********************************************************************
1156 * X11DRV_MotionNotify
1158 void X11DRV_MotionNotify( HWND hwnd
, XEvent
*xev
)
1160 XMotionEvent
*event
= &xev
->xmotion
;
1163 TRACE("hwnd %p, event->is_hint %d\n", hwnd
, event
->is_hint
);
1165 hwnd
= update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
1168 X11DRV_send_mouse_input( hwnd
, MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
,
1169 pt
.x
, pt
.y
, 0, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );
1173 /***********************************************************************
1174 * X11DRV_EnterNotify
1176 void X11DRV_EnterNotify( HWND hwnd
, XEvent
*xev
)
1178 XCrossingEvent
*event
= &xev
->xcrossing
;
1181 TRACE("hwnd %p, event->detail %d\n", hwnd
, event
->detail
);
1183 if (event
->detail
== NotifyVirtual
|| event
->detail
== NotifyNonlinearVirtual
) return;
1184 if (event
->window
== x11drv_thread_data()->grab_window
) return;
1186 /* simulate a mouse motion event */
1187 hwnd
= update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
1190 X11DRV_send_mouse_input( hwnd
, MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
,
1191 pt
.x
, pt
.y
, 0, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );