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
->id
= (injected_flags
& LLMHF_INJECTED
) ? 0 : GetCurrentThreadId();
310 req
->win
= wine_server_user_handle( hwnd
);
312 req
->wparam
= MAKEWPARAM( 0, data
);
317 req
->info
= extra_info
;
318 wine_server_call( req
);
324 /***********************************************************************
325 * X11DRV_send_mouse_input
327 void X11DRV_send_mouse_input( HWND hwnd
, DWORD flags
, DWORD x
, DWORD y
,
328 DWORD data
, DWORD time
, DWORD extra_info
, UINT injected_flags
)
332 if (!time
) time
= GetTickCount();
334 if (flags
& MOUSEEVENTF_MOVE
&& flags
& MOUSEEVENTF_ABSOLUTE
)
336 if (injected_flags
& LLMHF_INJECTED
)
338 pt
.x
= (x
* screen_width
) >> 16;
339 pt
.y
= (y
* screen_height
) >> 16;
346 if (cursor_pos
.x
== x
&& cursor_pos
.y
== y
&&
347 (flags
& ~(MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
)))
348 flags
&= ~MOUSEEVENTF_MOVE
;
352 else if (flags
& MOUSEEVENTF_MOVE
)
354 int accel
[3], xMult
= 1, yMult
= 1;
356 /* dx and dy can be negative numbers for relative movements */
357 SystemParametersInfoW(SPI_GETMOUSE
, 0, accel
, 0);
359 if (abs(x
) > accel
[0] && accel
[2] != 0)
362 if ((abs(x
) > accel
[1]) && (accel
[2] == 2)) xMult
= 4;
364 if (abs(y
) > accel
[0] && accel
[2] != 0)
367 if ((abs(y
) > accel
[1]) && (accel
[2] == 2)) yMult
= 4;
371 pt
.x
= cursor_pos
.x
+ (long)x
* xMult
;
372 pt
.y
= cursor_pos
.y
+ (long)y
* yMult
;
382 if (flags
& MOUSEEVENTF_MOVE
)
384 queue_raw_mouse_message( WM_MOUSEMOVE
, hwnd
, pt
.x
, pt
.y
, data
, time
,
385 extra_info
, injected_flags
);
386 if ((injected_flags
& LLMHF_INJECTED
) &&
387 ((flags
& MOUSEEVENTF_ABSOLUTE
) || x
|| y
)) /* we have to actually move the cursor */
389 clip_point_to_rect( &cursor_clip
, &pt
);
390 X11DRV_SetCursorPos( pt
.x
, pt
.y
);
395 clip_point_to_rect( &cursor_clip
, &pt
);
400 if (flags
& MOUSEEVENTF_LEFTDOWN
)
401 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_RBUTTONDOWN
: WM_LBUTTONDOWN
,
402 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
403 if (flags
& MOUSEEVENTF_LEFTUP
)
404 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_RBUTTONUP
: WM_LBUTTONUP
,
405 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
406 if (flags
& MOUSEEVENTF_RIGHTDOWN
)
407 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_LBUTTONDOWN
: WM_RBUTTONDOWN
,
408 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
409 if (flags
& MOUSEEVENTF_RIGHTUP
)
410 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_LBUTTONUP
: WM_RBUTTONUP
,
411 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
412 if (flags
& MOUSEEVENTF_MIDDLEDOWN
)
413 queue_raw_mouse_message( WM_MBUTTONDOWN
, hwnd
, pt
.x
, pt
.y
,
414 data
, time
, extra_info
, injected_flags
);
415 if (flags
& MOUSEEVENTF_MIDDLEUP
)
416 queue_raw_mouse_message( WM_MBUTTONUP
, hwnd
, pt
.x
, pt
.y
,
417 data
, time
, extra_info
, injected_flags
);
418 if (flags
& MOUSEEVENTF_WHEEL
)
419 queue_raw_mouse_message( WM_MOUSEWHEEL
, hwnd
, pt
.x
, pt
.y
,
420 data
, time
, extra_info
, injected_flags
);
421 if (flags
& MOUSEEVENTF_XDOWN
)
422 queue_raw_mouse_message( WM_XBUTTONDOWN
, hwnd
, pt
.x
, pt
.y
,
423 data
, time
, extra_info
, injected_flags
);
424 if (flags
& MOUSEEVENTF_XUP
)
425 queue_raw_mouse_message( WM_XBUTTONUP
, hwnd
, pt
.x
, pt
.y
,
426 data
, time
, extra_info
, injected_flags
);
429 #ifdef SONAME_LIBXCURSOR
431 /***********************************************************************
432 * create_xcursor_frame
434 * Use Xcursor to create a frame of an X cursor from a Windows one.
436 static XcursorImage
*create_xcursor_frame( HDC hdc
, const ICONINFOEXW
*iinfo
, HANDLE icon
,
437 HBITMAP hbmColor
, unsigned char *color_bits
, int color_size
,
438 HBITMAP hbmMask
, unsigned char *mask_bits
, int mask_size
,
439 int width
, int height
, int istep
)
441 XcursorImage
*image
, *ret
= NULL
;
442 int x
, y
, i
, has_alpha
;
446 image
= pXcursorImageCreate( width
, height
);
450 ERR("X11 failed to produce a cursor frame!\n");
454 image
->xhot
= iinfo
->xHotspot
;
455 image
->yhot
= iinfo
->yHotspot
;
456 image
->delay
= 100; /* TODO: find a way to get the proper delay */
458 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
459 memset( color_bits
, 0x00, color_size
);
460 SelectObject( hdc
, hbmColor
);
461 if (!DrawIconEx( hdc
, 0, 0, icon
, width
, height
, istep
, NULL
, DI_NORMAL
))
463 TRACE("Could not draw frame %d (walk past end of frames).\n", istep
);
466 memcpy( image
->pixels
, color_bits
, color_size
);
468 /* check if the cursor frame was drawn with an alpha channel */
469 for (i
= 0, ptr
= image
->pixels
; i
< width
* height
; i
++, ptr
++)
470 if ((has_alpha
= (*ptr
& 0xff000000) != 0)) break;
472 /* if no alpha channel was drawn then generate it from the mask */
475 unsigned int width_bytes
= (width
+ 31) / 32 * 4;
477 /* draw the cursor mask to a temporary buffer */
478 memset( mask_bits
, 0xFF, mask_size
);
479 SelectObject( hdc
, hbmMask
);
480 if (!DrawIconEx( hdc
, 0, 0, icon
, width
, height
, istep
, NULL
, DI_MASK
))
482 ERR("Failed to draw frame mask %d.\n", istep
);
485 /* use the buffer to directly modify the XcursorImage alpha channel */
486 for (y
= 0, ptr
= image
->pixels
; y
< height
; y
++)
487 for (x
= 0; x
< width
; x
++, ptr
++)
488 if (!((mask_bits
[y
* width_bytes
+ x
/ 8] << (x
% 8)) & 0x80))
494 if (ret
== NULL
) pXcursorImageDestroy( image
);
498 /***********************************************************************
499 * create_xcursor_cursor
501 * Use Xcursor to create an X cursor from a Windows one.
503 static Cursor
create_xcursor_cursor( HDC hdc
, const ICONINFOEXW
*iinfo
, HANDLE icon
, int width
, int height
)
505 unsigned char *color_bits
, *mask_bits
;
506 HBITMAP hbmColor
= 0, hbmMask
= 0;
507 XcursorImage
**imgs
, *image
;
508 int color_size
, mask_size
;
509 BITMAPINFO
*info
= NULL
;
510 XcursorImages
*images
;
514 if (!(imgs
= HeapAlloc( GetProcessHeap(), 0, sizeof(XcursorImage
*) ))) return 0;
516 /* Allocate all of the resources necessary to obtain a cursor frame */
517 if (!(info
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )))) goto cleanup
;
518 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
519 info
->bmiHeader
.biWidth
= width
;
520 info
->bmiHeader
.biHeight
= -height
;
521 info
->bmiHeader
.biPlanes
= 1;
522 info
->bmiHeader
.biCompression
= BI_RGB
;
523 info
->bmiHeader
.biXPelsPerMeter
= 0;
524 info
->bmiHeader
.biYPelsPerMeter
= 0;
525 info
->bmiHeader
.biClrUsed
= 0;
526 info
->bmiHeader
.biClrImportant
= 0;
527 info
->bmiHeader
.biBitCount
= 32;
528 color_size
= width
* height
* 4;
529 info
->bmiHeader
.biSizeImage
= color_size
;
530 hbmColor
= CreateDIBSection( hdc
, info
, DIB_RGB_COLORS
, (VOID
**) &color_bits
, NULL
, 0);
533 ERR("Failed to create DIB section for cursor color data!\n");
536 info
->bmiHeader
.biBitCount
= 1;
537 mask_size
= ((width
+ 31) / 32 * 4) * height
; /* width_bytes * height */
538 info
->bmiHeader
.biSizeImage
= mask_size
;
539 hbmMask
= CreateDIBSection( hdc
, info
, DIB_RGB_COLORS
, (VOID
**) &mask_bits
, NULL
, 0);
542 ERR("Failed to create DIB section for cursor mask data!\n");
546 /* Create an XcursorImage for each frame of the cursor */
549 XcursorImage
**imgstmp
;
551 image
= create_xcursor_frame( hdc
, iinfo
, icon
,
552 hbmColor
, color_bits
, color_size
,
553 hbmMask
, mask_bits
, mask_size
,
554 width
, height
, nFrames
);
555 if (!image
) break; /* no more drawable frames */
557 imgs
[nFrames
++] = image
;
558 if (!(imgstmp
= HeapReAlloc( GetProcessHeap(), 0, imgs
, (nFrames
+1)*sizeof(XcursorImage
*) ))) goto cleanup
;
562 /* Build an X cursor out of all of the frames */
563 if (!(images
= pXcursorImagesCreate( nFrames
))) goto cleanup
;
564 for (images
->nimage
= 0; images
->nimage
< nFrames
; images
->nimage
++)
565 images
->images
[images
->nimage
] = imgs
[images
->nimage
];
567 cursor
= pXcursorImagesLoadCursor( gdi_display
, images
);
569 pXcursorImagesDestroy( images
); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
570 HeapFree( GetProcessHeap(), 0, imgs
);
576 /* Failed to produce a cursor, free previously allocated frames */
577 for (nFrames
--; nFrames
>= 0; nFrames
--)
578 pXcursorImageDestroy( imgs
[nFrames
] );
579 HeapFree( GetProcessHeap(), 0, imgs
);
581 /* Cleanup all of the resources used to obtain the frame data */
582 if (hbmColor
) DeleteObject( hbmColor
);
583 if (hbmMask
) DeleteObject( hbmMask
);
584 HeapFree( GetProcessHeap(), 0, info
);
589 struct system_cursors
595 static const struct system_cursors user32_cursors
[] =
597 { OCR_NORMAL
, "left_ptr" },
598 { OCR_IBEAM
, "xterm" },
599 { OCR_WAIT
, "watch" },
600 { OCR_CROSS
, "cross" },
601 { OCR_UP
, "center_ptr" },
602 { OCR_SIZE
, "fleur" },
603 { OCR_SIZEALL
, "fleur" },
604 { OCR_ICON
, "icon" },
605 { OCR_SIZENWSE
, "nwse-resize" },
606 { OCR_SIZENESW
, "nesw-resize" },
607 { OCR_SIZEWE
, "ew-resize" },
608 { OCR_SIZENS
, "ns-resize" },
609 { OCR_NO
, "not-allowed" },
610 { OCR_HAND
, "hand2" },
611 { OCR_APPSTARTING
, "left_ptr_watch" },
612 { OCR_HELP
, "question_arrow" },
616 static const struct system_cursors comctl32_cursors
[] =
621 { 106, "row-resize" },
622 { 107, "row-resize" },
624 { 135, "col-resize" },
628 static const struct system_cursors ole32_cursors
[] =
637 static const struct system_cursors riched20_cursors
[] =
640 { 107, "right_ptr" },
649 const struct system_cursors
*cursors
;
653 { user32_cursors
, {'u','s','e','r','3','2','.','d','l','l',0} },
654 { comctl32_cursors
, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
655 { ole32_cursors
, {'o','l','e','3','2','.','d','l','l',0} },
656 { riched20_cursors
, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
659 /***********************************************************************
660 * create_xcursor_system_cursor
662 * Create an X cursor for a system cursor.
664 static Cursor
create_xcursor_system_cursor( const ICONINFOEXW
*info
)
666 static const WCHAR idW
[] = {'%','h','u',0};
667 const struct system_cursors
*cursors
;
672 WCHAR
*p
, name
[MAX_PATH
* 2], valueW
[64];
676 if (!pXcursorLibraryLoadCursor
) return 0;
677 if (!info
->szModName
[0]) return 0;
679 p
= strrchrW( info
->szModName
, '\\' );
680 strcpyW( name
, p
? p
+ 1 : info
->szModName
);
681 p
= name
+ strlenW( name
);
683 if (info
->szResName
[0]) strcpyW( p
, info
->szResName
);
684 else sprintfW( p
, idW
, info
->wResID
);
687 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
688 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\X11 Driver\\Cursors", &key
))
690 size
= sizeof(valueW
) / sizeof(WCHAR
);
691 ret
= RegQueryValueExW( key
, name
, NULL
, NULL
, (BYTE
*)valueW
, &size
);
695 if (!valueW
[0]) return 0; /* force standard cursor */
696 if (!WideCharToMultiByte( CP_UNIXCP
, 0, valueW
, -1, valueA
, sizeof(valueA
), NULL
, NULL
))
702 if (info
->szResName
[0]) goto done
; /* only integer resources are supported here */
703 if (!(module
= GetModuleHandleW( info
->szModName
))) goto done
;
705 for (i
= 0; i
< sizeof(module_cursors
)/sizeof(module_cursors
[0]); i
++)
706 if (GetModuleHandleW( module_cursors
[i
].name
) == module
) break;
707 if (i
== sizeof(module_cursors
)/sizeof(module_cursors
[0])) goto done
;
709 cursors
= module_cursors
[i
].cursors
;
710 for (i
= 0; cursors
[i
].id
; i
++)
711 if (cursors
[i
].id
== info
->wResID
)
713 strcpy( valueA
, cursors
[i
].name
);
721 cursor
= pXcursorLibraryLoadCursor( gdi_display
, valueA
);
723 if (!cursor
) WARN( "no system cursor found for %s mapped to %s\n",
724 debugstr_w(name
), debugstr_a(valueA
) );
726 else WARN( "no system cursor found for %s\n", debugstr_w(name
) );
730 #endif /* SONAME_LIBXCURSOR */
733 /***********************************************************************
734 * create_cursor_from_bitmaps
736 * Create an X11 cursor from source bitmaps.
738 static Cursor
create_cursor_from_bitmaps( HBITMAP src_xor
, HBITMAP src_and
, int width
, int height
,
739 int xor_y
, int and_y
, XColor
*fg
, XColor
*bg
,
740 int hotspot_x
, int hotspot_y
)
742 HDC src
= 0, dst
= 0;
743 HBITMAP bits
= 0, mask
= 0, mask_inv
= 0;
746 if (!(src
= CreateCompatibleDC( 0 ))) goto done
;
747 if (!(dst
= CreateCompatibleDC( 0 ))) goto done
;
749 if (!(bits
= CreateBitmap( width
, height
, 1, 1, NULL
))) goto done
;
750 if (!(mask
= CreateBitmap( width
, height
, 1, 1, NULL
))) goto done
;
751 if (!(mask_inv
= CreateBitmap( width
, height
, 1, 1, NULL
))) goto done
;
753 /* We have to do some magic here, as cursors are not fully
754 * compatible between Windows and X11. Under X11, there are
755 * only 3 possible color cursor: black, white and masked. So
756 * we map the 4th Windows color (invert the bits on the screen)
757 * to black and an additional white bit on an other place
758 * (+1,+1). This require some boolean arithmetic:
761 * And Xor Result | Bits Mask Result
762 * 0 0 black | 0 1 background
763 * 0 1 white | 1 1 foreground
764 * 1 0 no change | X 0 no change
765 * 1 1 inverted | 0 1 background
768 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
769 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
771 SelectObject( src
, src_and
);
772 SelectObject( dst
, bits
);
773 BitBlt( dst
, 0, 0, width
, height
, src
, 0, and_y
, SRCCOPY
);
774 SelectObject( dst
, mask
);
775 BitBlt( dst
, 0, 0, width
, height
, src
, 0, and_y
, SRCCOPY
);
776 SelectObject( dst
, mask_inv
);
777 BitBlt( dst
, 0, 0, width
, height
, src
, 0, and_y
, SRCCOPY
);
778 SelectObject( src
, src_xor
);
779 BitBlt( dst
, 0, 0, width
, height
, src
, 0, xor_y
, SRCAND
/* src & dst */ );
780 SelectObject( dst
, bits
);
781 BitBlt( dst
, 0, 0, width
, height
, src
, 0, xor_y
, SRCERASE
/* src & ~dst */ );
782 SelectObject( dst
, mask
);
783 BitBlt( dst
, 0, 0, width
, height
, src
, 0, xor_y
, 0xdd0228 /* src | ~dst */ );
784 /* additional white */
785 SelectObject( src
, mask_inv
);
786 BitBlt( dst
, 1, 1, width
, height
, src
, 0, 0, SRCPAINT
/* src | dst */);
787 SelectObject( dst
, bits
);
788 BitBlt( dst
, 1, 1, width
, height
, src
, 0, 0, SRCPAINT
/* src | dst */ );
791 cursor
= XCreatePixmapCursor( gdi_display
, X11DRV_get_pixmap(bits
), X11DRV_get_pixmap(mask
),
792 fg
, bg
, hotspot_x
, hotspot_y
);
798 DeleteObject( bits
);
799 DeleteObject( mask
);
800 DeleteObject( mask_inv
);
804 /***********************************************************************
807 * Create an X cursor from a Windows one.
809 static Cursor
create_xlib_cursor( HDC hdc
, const ICONINFOEXW
*icon
, int width
, int height
)
812 Cursor cursor
= None
;
813 HBITMAP xor_bitmap
= 0;
815 unsigned int *color_bits
= NULL
, *ptr
;
816 unsigned char *mask_bits
= NULL
, *xor_bits
= NULL
;
817 int i
, x
, y
, has_alpha
= 0;
818 int rfg
, gfg
, bfg
, rbg
, gbg
, bbg
, fgBits
, bgBits
;
819 unsigned int width_bytes
= (width
+ 31) / 32 * 4;
821 if (!(info
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] ))))
823 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
824 info
->bmiHeader
.biWidth
= width
;
825 info
->bmiHeader
.biHeight
= -height
;
826 info
->bmiHeader
.biPlanes
= 1;
827 info
->bmiHeader
.biBitCount
= 1;
828 info
->bmiHeader
.biCompression
= BI_RGB
;
829 info
->bmiHeader
.biSizeImage
= width_bytes
* height
;
830 info
->bmiHeader
.biXPelsPerMeter
= 0;
831 info
->bmiHeader
.biYPelsPerMeter
= 0;
832 info
->bmiHeader
.biClrUsed
= 0;
833 info
->bmiHeader
.biClrImportant
= 0;
835 if (!(mask_bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
))) goto done
;
836 if (!GetDIBits( hdc
, icon
->hbmMask
, 0, height
, mask_bits
, info
, DIB_RGB_COLORS
)) goto done
;
838 info
->bmiHeader
.biBitCount
= 32;
839 info
->bmiHeader
.biSizeImage
= width
* height
* 4;
840 if (!(color_bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
))) goto done
;
841 if (!(xor_bits
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, width_bytes
* height
))) goto done
;
842 GetDIBits( hdc
, icon
->hbmColor
, 0, height
, color_bits
, info
, DIB_RGB_COLORS
);
844 /* compute fg/bg color and xor bitmap based on average of the color values */
846 if (!(xor_bitmap
= CreateBitmap( width
, height
, 1, 1, NULL
))) goto done
;
847 rfg
= gfg
= bfg
= rbg
= gbg
= bbg
= fgBits
= 0;
848 for (y
= 0, ptr
= color_bits
; y
< height
; y
++)
850 for (x
= 0; x
< width
; x
++, ptr
++)
852 int red
= (*ptr
>> 16) & 0xff;
853 int green
= (*ptr
>> 8) & 0xff;
854 int blue
= (*ptr
>> 0) & 0xff;
855 if (red
+ green
+ blue
> 0x40)
861 xor_bits
[y
* width_bytes
+ x
/ 8] |= 0x80 >> (x
% 8);
873 fg
.red
= rfg
* 257 / fgBits
;
874 fg
.green
= gfg
* 257 / fgBits
;
875 fg
.blue
= bfg
* 257 / fgBits
;
877 else fg
.red
= fg
.green
= fg
.blue
= 0;
878 bgBits
= width
* height
- fgBits
;
881 bg
.red
= rbg
* 257 / bgBits
;
882 bg
.green
= gbg
* 257 / bgBits
;
883 bg
.blue
= bbg
* 257 / bgBits
;
885 else bg
.red
= bg
.green
= bg
.blue
= 0;
887 info
->bmiHeader
.biBitCount
= 1;
888 info
->bmiHeader
.biSizeImage
= width_bytes
* height
;
889 SetDIBits( hdc
, xor_bitmap
, 0, height
, xor_bits
, info
, DIB_RGB_COLORS
);
891 /* generate mask from the alpha channel if we have one */
893 for (i
= 0, ptr
= color_bits
; i
< width
* height
; i
++, ptr
++)
894 if ((has_alpha
= (*ptr
& 0xff000000) != 0)) break;
898 memset( mask_bits
, 0, width_bytes
* height
);
899 for (y
= 0, ptr
= color_bits
; y
< height
; y
++)
900 for (x
= 0; x
< width
; x
++, ptr
++)
901 if ((*ptr
>> 24) > 25) /* more than 10% alpha */
902 mask_bits
[y
* width_bytes
+ x
/ 8] |= 0x80 >> (x
% 8);
904 info
->bmiHeader
.biBitCount
= 1;
905 info
->bmiHeader
.biSizeImage
= width_bytes
* height
;
906 SetDIBits( hdc
, icon
->hbmMask
, 0, height
, mask_bits
, info
, DIB_RGB_COLORS
);
909 cursor
= XCreatePixmapCursor( gdi_display
,
910 X11DRV_get_pixmap(xor_bitmap
),
911 X11DRV_get_pixmap(icon
->hbmMask
),
912 &fg
, &bg
, icon
->xHotspot
, icon
->yHotspot
);
917 cursor
= create_cursor_from_bitmaps( xor_bitmap
, icon
->hbmMask
, width
, height
, 0, 0,
918 &fg
, &bg
, icon
->xHotspot
, icon
->yHotspot
);
922 DeleteObject( xor_bitmap
);
923 HeapFree( GetProcessHeap(), 0, info
);
924 HeapFree( GetProcessHeap(), 0, color_bits
);
925 HeapFree( GetProcessHeap(), 0, xor_bits
);
926 HeapFree( GetProcessHeap(), 0, mask_bits
);
930 /***********************************************************************
933 * Create an X cursor from a Windows one.
935 static Cursor
create_cursor( HANDLE handle
)
941 if (!handle
) return get_empty_cursor();
943 info
.cbSize
= sizeof(info
);
944 if (!GetIconInfoExW( handle
, &info
)) return 0;
946 #ifdef SONAME_LIBXCURSOR
947 if (use_system_cursors
&& (cursor
= create_xcursor_system_cursor( &info
)))
949 DeleteObject( info
.hbmColor
);
950 DeleteObject( info
.hbmMask
);
955 GetObjectW( info
.hbmMask
, sizeof(bm
), &bm
);
956 if (!info
.hbmColor
) bm
.bmHeight
/= 2;
958 /* make sure hotspot is valid */
959 if (info
.xHotspot
>= bm
.bmWidth
|| info
.yHotspot
>= bm
.bmHeight
)
961 info
.xHotspot
= bm
.bmWidth
/ 2;
962 info
.yHotspot
= bm
.bmHeight
/ 2;
967 HDC hdc
= CreateCompatibleDC( 0 );
970 #ifdef SONAME_LIBXCURSOR
971 if (pXcursorImagesLoadCursor
)
972 cursor
= create_xcursor_cursor( hdc
, &info
, handle
, bm
.bmWidth
, bm
.bmHeight
);
974 if (!cursor
) cursor
= create_xlib_cursor( hdc
, &info
, bm
.bmWidth
, bm
.bmHeight
);
976 DeleteObject( info
.hbmColor
);
982 fg
.red
= fg
.green
= fg
.blue
= 0xffff;
983 bg
.red
= bg
.green
= bg
.blue
= 0;
984 cursor
= create_cursor_from_bitmaps( info
.hbmMask
, info
.hbmMask
, bm
.bmWidth
, bm
.bmHeight
,
985 bm
.bmHeight
, 0, &fg
, &bg
, info
.xHotspot
, info
.yHotspot
);
988 DeleteObject( info
.hbmMask
);
992 /***********************************************************************
993 * DestroyCursorIcon (X11DRV.@)
995 void CDECL
X11DRV_DestroyCursorIcon( HCURSOR handle
)
1000 if (cursor_context
&& !XFindContext( gdi_display
, (XID
)handle
, cursor_context
, (char **)&cursor
))
1002 TRACE( "%p xid %lx\n", handle
, cursor
);
1003 XFreeCursor( gdi_display
, cursor
);
1004 XDeleteContext( gdi_display
, (XID
)handle
, cursor_context
);
1006 wine_tsx11_unlock();
1009 /***********************************************************************
1010 * SetCursor (X11DRV.@)
1012 void CDECL
X11DRV_SetCursor( HCURSOR handle
)
1014 if (cursor_window
) SendNotifyMessageW( cursor_window
, WM_X11DRV_SET_CURSOR
, 0, (LPARAM
)handle
);
1017 /***********************************************************************
1018 * SetCursorPos (X11DRV.@)
1020 BOOL CDECL
X11DRV_SetCursorPos( INT x
, INT y
)
1022 Display
*display
= thread_init_display();
1024 TRACE( "warping to (%d,%d)\n", x
, y
);
1027 XWarpPointer( display
, root_window
, root_window
, 0, 0, 0, 0,
1028 x
- virtual_screen_rect
.left
, y
- virtual_screen_rect
.top
);
1029 XFlush( display
); /* avoids bad mouse lag in games that do their own mouse warping */
1032 wine_tsx11_unlock();
1036 /***********************************************************************
1037 * GetCursorPos (X11DRV.@)
1039 BOOL CDECL
X11DRV_GetCursorPos(LPPOINT pos
)
1041 Display
*display
= thread_init_display();
1043 int rootX
, rootY
, winX
, winY
;
1044 unsigned int xstate
;
1048 if ((GetTickCount() - last_time_modified
> 100) &&
1049 XQueryPointer( display
, root_window
, &root
, &child
,
1050 &rootX
, &rootY
, &winX
, &winY
, &xstate
))
1052 winX
+= virtual_screen_rect
.left
;
1053 winY
+= virtual_screen_rect
.top
;
1054 TRACE("pointer at (%d,%d)\n", winX
, winY
);
1059 wine_tsx11_unlock();
1064 /***********************************************************************
1065 * ClipCursor (X11DRV.@)
1067 * Set the cursor clipping rectangle.
1069 BOOL CDECL
X11DRV_ClipCursor( LPCRECT clip
)
1071 if (!IntersectRect( &cursor_clip
, &virtual_screen_rect
, clip
))
1072 cursor_clip
= virtual_screen_rect
;
1077 /***********************************************************************
1078 * X11DRV_ButtonPress
1080 void X11DRV_ButtonPress( HWND hwnd
, XEvent
*xev
)
1082 XButtonEvent
*event
= &xev
->xbutton
;
1083 int buttonNum
= event
->button
- 1;
1087 if (buttonNum
>= NB_BUTTONS
) return;
1092 wData
= WHEEL_DELTA
;
1095 wData
= -WHEEL_DELTA
;
1111 update_user_time( event
->time
);
1112 hwnd
= update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
1115 X11DRV_send_mouse_input( hwnd
, button_down_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
,
1116 pt
.x
, pt
.y
, wData
, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );
1120 /***********************************************************************
1121 * X11DRV_ButtonRelease
1123 void X11DRV_ButtonRelease( HWND hwnd
, XEvent
*xev
)
1125 XButtonEvent
*event
= &xev
->xbutton
;
1126 int buttonNum
= event
->button
- 1;
1130 if (buttonNum
>= NB_BUTTONS
|| !button_up_flags
[buttonNum
]) return;
1148 hwnd
= update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
1151 X11DRV_send_mouse_input( hwnd
, button_up_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
,
1152 pt
.x
, pt
.y
, wData
, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );
1156 /***********************************************************************
1157 * X11DRV_MotionNotify
1159 void X11DRV_MotionNotify( HWND hwnd
, XEvent
*xev
)
1161 XMotionEvent
*event
= &xev
->xmotion
;
1164 TRACE("hwnd %p, event->is_hint %d\n", hwnd
, event
->is_hint
);
1166 hwnd
= update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
1169 X11DRV_send_mouse_input( hwnd
, MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
,
1170 pt
.x
, pt
.y
, 0, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );
1174 /***********************************************************************
1175 * X11DRV_EnterNotify
1177 void X11DRV_EnterNotify( HWND hwnd
, XEvent
*xev
)
1179 XCrossingEvent
*event
= &xev
->xcrossing
;
1182 TRACE("hwnd %p, event->detail %d\n", hwnd
, event
->detail
);
1184 if (event
->detail
== NotifyVirtual
|| event
->detail
== NotifyNonlinearVirtual
) return;
1185 if (event
->window
== x11drv_thread_data()->grab_window
) return;
1187 /* simulate a mouse motion event */
1188 hwnd
= update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
1191 X11DRV_send_mouse_input( hwnd
, MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
,
1192 pt
.x
, pt
.y
, 0, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );