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"
28 #ifdef SONAME_LIBXCURSOR
29 # include <X11/Xcursor/Xcursor.h>
30 static void *xcursor_handle
;
31 # define MAKE_FUNCPTR(f) static typeof(f) * p##f
32 MAKE_FUNCPTR(XcursorImageCreate
);
33 MAKE_FUNCPTR(XcursorImageDestroy
);
34 MAKE_FUNCPTR(XcursorImageLoadCursor
);
36 #endif /* SONAME_LIBXCURSOR */
38 #define NONAMELESSUNION
39 #define NONAMELESSSTRUCT
44 #include "wine/server.h"
45 #include "wine/library.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(cursor
);
50 /**********************************************************************/
53 #define Button6Mask (1<<13)
56 #define Button7Mask (1<<14)
59 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
61 static const UINT button_down_flags
[NB_BUTTONS
] =
64 MOUSEEVENTF_MIDDLEDOWN
,
65 MOUSEEVENTF_RIGHTDOWN
,
68 MOUSEEVENTF_XDOWN
, /* FIXME: horizontal wheel */
74 static const UINT button_up_flags
[NB_BUTTONS
] =
88 static HWND cursor_window
;
89 static DWORD last_time_modified
;
90 static RECT cursor_clip
; /* Cursor clipping rect */
91 static XContext cursor_context
;
92 static Cursor
create_cursor( HANDLE handle
);
94 BOOL CDECL
X11DRV_SetCursorPos( INT x
, INT y
);
97 /***********************************************************************
100 * Load the Xcursor library for use.
102 void X11DRV_Xcursor_Init(void)
104 #ifdef SONAME_LIBXCURSOR
105 xcursor_handle
= wine_dlopen(SONAME_LIBXCURSOR
, RTLD_NOW
, NULL
, 0);
106 if (!xcursor_handle
) /* wine_dlopen failed. */
108 WARN("Xcursor failed to load. Using fallback code.\n");
111 #define LOAD_FUNCPTR(f) \
112 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
114 LOAD_FUNCPTR(XcursorImageCreate
);
115 LOAD_FUNCPTR(XcursorImageDestroy
);
116 LOAD_FUNCPTR(XcursorImageLoadCursor
);
118 #endif /* SONAME_LIBXCURSOR */
122 /***********************************************************************
125 * get the coordinates of a mouse event
127 static inline void get_coords( HWND hwnd
, Window window
, int x
, int y
, POINT
*pt
)
129 struct x11drv_win_data
*data
= X11DRV_get_win_data( hwnd
);
133 if (window
== data
->client_window
)
135 pt
->x
= x
+ data
->client_rect
.left
;
136 pt
->y
= y
+ data
->client_rect
.top
;
140 pt
->x
= x
+ data
->whole_rect
.left
;
141 pt
->y
= y
+ data
->whole_rect
.top
;
145 /***********************************************************************
148 * Clip point to the provided rectangle
150 static inline void clip_point_to_rect( LPCRECT rect
, LPPOINT pt
)
152 if (pt
->x
< rect
->left
) pt
->x
= rect
->left
;
153 else if (pt
->x
>= rect
->right
) pt
->x
= rect
->right
- 1;
154 if (pt
->y
< rect
->top
) pt
->y
= rect
->top
;
155 else if (pt
->y
>= rect
->bottom
) pt
->y
= rect
->bottom
- 1;
158 /***********************************************************************
159 * update_button_state
161 * Update the button state with what X provides us
163 static inline void update_button_state( unsigned int state
)
165 key_state_table
[VK_LBUTTON
] = (state
& Button1Mask
? 0x80 : 0);
166 key_state_table
[VK_MBUTTON
] = (state
& Button2Mask
? 0x80 : 0);
167 key_state_table
[VK_RBUTTON
] = (state
& Button3Mask
? 0x80 : 0);
168 /* X-buttons are not reported from XQueryPointer */
171 /***********************************************************************
174 static Cursor
get_empty_cursor(void)
176 static Cursor cursor
;
177 static const char data
[] = { 0 };
185 bg
.red
= bg
.green
= bg
.blue
= 0x0000;
186 pixmap
= XCreateBitmapFromData( gdi_display
, root_window
, data
, 1, 1 );
189 cursor
= XCreatePixmapCursor( gdi_display
, pixmap
, pixmap
, &bg
, &bg
, 0, 0 );
190 XFreePixmap( gdi_display
, pixmap
);
197 /***********************************************************************
200 void set_window_cursor( HWND hwnd
, HCURSOR handle
)
202 struct x11drv_win_data
*data
;
205 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
208 if (!handle
) cursor
= get_empty_cursor();
209 else if (!cursor_context
|| XFindContext( gdi_display
, (XID
)handle
, cursor_context
, (char **)&cursor
))
211 /* try to create it */
213 if (!(cursor
= create_cursor( handle
))) return;
216 if (!cursor_context
) cursor_context
= XUniqueContext();
217 if (!XFindContext( gdi_display
, (XID
)handle
, cursor_context
, (char **)&prev
))
219 /* someone else was here first */
220 XFreeCursor( gdi_display
, cursor
);
225 XSaveContext( gdi_display
, (XID
)handle
, cursor_context
, (char *)cursor
);
226 TRACE( "cursor %p created %lx\n", handle
, cursor
);
230 XDefineCursor( gdi_display
, data
->whole_window
, cursor
);
231 /* make the change take effect immediately */
232 XFlush( gdi_display
);
233 data
->cursor
= handle
;
237 /***********************************************************************
240 * Update the various window states on a mouse event.
242 static void update_mouse_state( HWND hwnd
, Window window
, int x
, int y
, unsigned int state
, POINT
*pt
)
244 struct x11drv_thread_data
*data
= x11drv_thread_data();
246 get_coords( hwnd
, window
, x
, y
, pt
);
248 cursor_window
= hwnd
;
250 /* update the wine server Z-order */
252 if (window
!= data
->grab_window
&&
253 /* ignore event if a button is pressed, since the mouse is then grabbed too */
254 !(state
& (Button1Mask
|Button2Mask
|Button3Mask
|Button4Mask
|Button5Mask
|Button6Mask
|Button7Mask
)))
256 SERVER_START_REQ( update_window_zorder
)
258 req
->window
= wine_server_user_handle( hwnd
);
259 req
->rect
.left
= pt
->x
;
260 req
->rect
.top
= pt
->y
;
261 req
->rect
.right
= pt
->x
+ 1;
262 req
->rect
.bottom
= pt
->y
+ 1;
263 wine_server_call( req
);
270 /***********************************************************************
273 static WORD
get_key_state(void)
277 if (GetSystemMetrics( SM_SWAPBUTTON
))
279 if (key_state_table
[VK_RBUTTON
] & 0x80) ret
|= MK_LBUTTON
;
280 if (key_state_table
[VK_LBUTTON
] & 0x80) ret
|= MK_RBUTTON
;
284 if (key_state_table
[VK_LBUTTON
] & 0x80) ret
|= MK_LBUTTON
;
285 if (key_state_table
[VK_RBUTTON
] & 0x80) ret
|= MK_RBUTTON
;
287 if (key_state_table
[VK_MBUTTON
] & 0x80) ret
|= MK_MBUTTON
;
288 if (key_state_table
[VK_SHIFT
] & 0x80) ret
|= MK_SHIFT
;
289 if (key_state_table
[VK_CONTROL
] & 0x80) ret
|= MK_CONTROL
;
290 if (key_state_table
[VK_XBUTTON1
] & 0x80) ret
|= MK_XBUTTON1
;
291 if (key_state_table
[VK_XBUTTON2
] & 0x80) ret
|= MK_XBUTTON2
;
296 /***********************************************************************
297 * queue_raw_mouse_message
299 static void queue_raw_mouse_message( UINT message
, HWND hwnd
, DWORD x
, DWORD y
,
300 DWORD data
, DWORD time
, DWORD extra_info
, UINT injected_flags
)
307 hook
.mouseData
= MAKELONG( 0, data
);
308 hook
.flags
= injected_flags
;
310 hook
.dwExtraInfo
= extra_info
;
312 last_time_modified
= GetTickCount();
313 if (HOOK_CallHooks( WH_MOUSE_LL
, HC_ACTION
, message
, (LPARAM
)&hook
, TRUE
))
314 message
= 0; /* ignore it */
316 SERVER_START_REQ( send_hardware_message
)
318 req
->id
= (injected_flags
& LLMHF_INJECTED
) ? 0 : GetCurrentThreadId();
319 req
->win
= wine_server_user_handle( hwnd
);
321 req
->wparam
= MAKEWPARAM( get_key_state(), data
);
326 req
->info
= extra_info
;
327 wine_server_call( req
);
328 cursor
= (reply
->count
>= 0) ? wine_server_ptr_handle(reply
->cursor
) : 0;
334 struct x11drv_win_data
*data
= X11DRV_get_win_data( hwnd
);
335 if (data
&& cursor
!= data
->cursor
) set_window_cursor( hwnd
, cursor
);
340 /***********************************************************************
341 * X11DRV_send_mouse_input
343 void X11DRV_send_mouse_input( HWND hwnd
, DWORD flags
, DWORD x
, DWORD y
,
344 DWORD data
, DWORD time
, DWORD extra_info
, UINT injected_flags
)
348 if (flags
& MOUSEEVENTF_MOVE
&& flags
& MOUSEEVENTF_ABSOLUTE
)
350 if (injected_flags
& LLMHF_INJECTED
)
352 pt
.x
= (x
* screen_width
) >> 16;
353 pt
.y
= (y
* screen_height
) >> 16;
360 if (cursor_pos
.x
== x
&& cursor_pos
.y
== y
&&
361 (flags
& ~(MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
)))
362 flags
&= ~MOUSEEVENTF_MOVE
;
366 else if (flags
& MOUSEEVENTF_MOVE
)
368 int accel
[3], xMult
= 1, yMult
= 1;
370 /* dx and dy can be negative numbers for relative movements */
371 SystemParametersInfoW(SPI_GETMOUSE
, 0, accel
, 0);
373 if (abs(x
) > accel
[0] && accel
[2] != 0)
376 if ((abs(x
) > accel
[1]) && (accel
[2] == 2)) xMult
= 4;
378 if (abs(y
) > accel
[0] && accel
[2] != 0)
381 if ((abs(y
) > accel
[1]) && (accel
[2] == 2)) yMult
= 4;
385 pt
.x
= cursor_pos
.x
+ (long)x
* xMult
;
386 pt
.y
= cursor_pos
.y
+ (long)y
* yMult
;
396 if (flags
& MOUSEEVENTF_MOVE
)
398 queue_raw_mouse_message( WM_MOUSEMOVE
, hwnd
, pt
.x
, pt
.y
, data
, time
,
399 extra_info
, injected_flags
);
400 if ((injected_flags
& LLMHF_INJECTED
) &&
401 ((flags
& MOUSEEVENTF_ABSOLUTE
) || x
|| y
)) /* we have to actually move the cursor */
403 X11DRV_SetCursorPos( pt
.x
, pt
.y
);
408 clip_point_to_rect( &cursor_clip
, &pt
);
413 if (flags
& MOUSEEVENTF_LEFTDOWN
)
415 key_state_table
[VK_LBUTTON
] |= 0xc0;
416 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_RBUTTONDOWN
: WM_LBUTTONDOWN
,
417 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
419 if (flags
& MOUSEEVENTF_LEFTUP
)
421 key_state_table
[VK_LBUTTON
] &= ~0x80;
422 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_RBUTTONUP
: WM_LBUTTONUP
,
423 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
425 if (flags
& MOUSEEVENTF_RIGHTDOWN
)
427 key_state_table
[VK_RBUTTON
] |= 0xc0;
428 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_LBUTTONDOWN
: WM_RBUTTONDOWN
,
429 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
431 if (flags
& MOUSEEVENTF_RIGHTUP
)
433 key_state_table
[VK_RBUTTON
] &= ~0x80;
434 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_LBUTTONUP
: WM_RBUTTONUP
,
435 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
437 if (flags
& MOUSEEVENTF_MIDDLEDOWN
)
439 key_state_table
[VK_MBUTTON
] |= 0xc0;
440 queue_raw_mouse_message( WM_MBUTTONDOWN
, hwnd
, pt
.x
, pt
.y
, data
, time
,
441 extra_info
, injected_flags
);
443 if (flags
& MOUSEEVENTF_MIDDLEUP
)
445 key_state_table
[VK_MBUTTON
] &= ~0x80;
446 queue_raw_mouse_message( WM_MBUTTONUP
, hwnd
, pt
.x
, pt
.y
, data
, time
,
447 extra_info
, injected_flags
);
449 if (flags
& MOUSEEVENTF_WHEEL
)
451 queue_raw_mouse_message( WM_MOUSEWHEEL
, hwnd
, pt
.x
, pt
.y
, data
, time
,
452 extra_info
, injected_flags
);
454 if (flags
& MOUSEEVENTF_XDOWN
)
456 key_state_table
[VK_XBUTTON1
+ data
- 1] |= 0xc0;
457 queue_raw_mouse_message( WM_XBUTTONDOWN
, hwnd
, pt
.x
, pt
.y
, data
, time
,
458 extra_info
, injected_flags
);
460 if (flags
& MOUSEEVENTF_XUP
)
462 key_state_table
[VK_XBUTTON1
+ data
- 1] &= ~0x80;
463 queue_raw_mouse_message( WM_XBUTTONUP
, hwnd
, pt
.x
, pt
.y
, data
, time
,
464 extra_info
, injected_flags
);
468 #ifdef SONAME_LIBXCURSOR
470 /***********************************************************************
471 * create_xcursor_cursor
473 * Use Xcursor to create an X cursor from a Windows one.
475 static Cursor
create_xcursor_cursor( HDC hdc
, ICONINFO
*icon
, int width
, int height
)
477 int x
, y
, i
, has_alpha
;
483 if (!(info
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] )))) return 0;
486 image
= pXcursorImageCreate( width
, height
);
490 HeapFree( GetProcessHeap(), 0, info
);
494 image
->xhot
= icon
->xHotspot
;
495 image
->yhot
= icon
->yHotspot
;
498 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
499 info
->bmiHeader
.biWidth
= width
;
500 info
->bmiHeader
.biHeight
= -height
;
501 info
->bmiHeader
.biPlanes
= 1;
502 info
->bmiHeader
.biBitCount
= 32;
503 info
->bmiHeader
.biCompression
= BI_RGB
;
504 info
->bmiHeader
.biSizeImage
= width
* height
* 4;
505 info
->bmiHeader
.biXPelsPerMeter
= 0;
506 info
->bmiHeader
.biYPelsPerMeter
= 0;
507 info
->bmiHeader
.biClrUsed
= 0;
508 info
->bmiHeader
.biClrImportant
= 0;
509 GetDIBits( hdc
, icon
->hbmColor
, 0, height
, image
->pixels
, info
, DIB_RGB_COLORS
);
511 for (i
= 0, ptr
= image
->pixels
; i
< width
* height
; i
++, ptr
++)
512 if ((has_alpha
= (*ptr
& 0xff000000) != 0)) break;
516 unsigned char *mask_bits
;
517 unsigned int width_bytes
= (width
+ 31) / 32 * 4;
519 /* generate alpha channel from the mask */
520 info
->bmiHeader
.biBitCount
= 1;
521 info
->bmiHeader
.biSizeImage
= width_bytes
* height
;
522 if ((mask_bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
)))
524 GetDIBits( hdc
, icon
->hbmMask
, 0, height
, mask_bits
, info
, DIB_RGB_COLORS
);
525 for (y
= 0, ptr
= image
->pixels
; y
< height
; y
++)
526 for (x
= 0; x
< width
; x
++, ptr
++)
527 if (!((mask_bits
[y
* width_bytes
+ x
/ 8] << (x
% 8)) & 0x80))
529 HeapFree( GetProcessHeap(), 0, mask_bits
);
532 HeapFree( GetProcessHeap(), 0, info
);
535 cursor
= pXcursorImageLoadCursor( gdi_display
, image
);
536 pXcursorImageDestroy( image
);
541 #endif /* SONAME_LIBXCURSOR */
544 /***********************************************************************
545 * create_cursor_from_bitmaps
547 * Create an X11 cursor from source bitmaps.
549 static Cursor
create_cursor_from_bitmaps( HBITMAP src_xor
, HBITMAP src_and
, int width
, int height
,
550 int xor_y
, int and_y
, XColor
*fg
, XColor
*bg
,
551 int hotspot_x
, int hotspot_y
)
553 HDC src
= 0, dst
= 0;
554 HBITMAP bits
= 0, mask
= 0, mask_inv
= 0;
557 if (!(src
= CreateCompatibleDC( 0 ))) goto done
;
558 if (!(dst
= CreateCompatibleDC( 0 ))) goto done
;
560 if (!(bits
= CreateBitmap( width
, height
, 1, 1, NULL
))) goto done
;
561 if (!(mask
= CreateBitmap( width
, height
, 1, 1, NULL
))) goto done
;
562 if (!(mask_inv
= CreateBitmap( width
, height
, 1, 1, NULL
))) goto done
;
564 /* We have to do some magic here, as cursors are not fully
565 * compatible between Windows and X11. Under X11, there are
566 * only 3 possible color cursor: black, white and masked. So
567 * we map the 4th Windows color (invert the bits on the screen)
568 * to black and an additional white bit on an other place
569 * (+1,+1). This require some boolean arithmetic:
572 * And Xor Result | Bits Mask Result
573 * 0 0 black | 0 1 background
574 * 0 1 white | 1 1 foreground
575 * 1 0 no change | X 0 no change
576 * 1 1 inverted | 0 1 background
579 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
580 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
582 SelectObject( src
, src_and
);
583 SelectObject( dst
, bits
);
584 BitBlt( dst
, 0, 0, width
, height
, src
, 0, and_y
, SRCCOPY
);
585 SelectObject( dst
, mask
);
586 BitBlt( dst
, 0, 0, width
, height
, src
, 0, and_y
, SRCCOPY
);
587 SelectObject( dst
, mask_inv
);
588 BitBlt( dst
, 0, 0, width
, height
, src
, 0, and_y
, SRCCOPY
);
589 SelectObject( src
, src_xor
);
590 BitBlt( dst
, 0, 0, width
, height
, src
, 0, xor_y
, SRCAND
/* src & dst */ );
591 SelectObject( dst
, bits
);
592 BitBlt( dst
, 0, 0, width
, height
, src
, 0, xor_y
, SRCERASE
/* src & ~dst */ );
593 SelectObject( dst
, mask
);
594 BitBlt( dst
, 0, 0, width
, height
, src
, 0, xor_y
, 0xdd0228 /* src | ~dst */ );
595 /* additional white */
596 SelectObject( src
, mask_inv
);
597 BitBlt( dst
, 1, 1, width
, height
, src
, 0, 0, SRCPAINT
/* src | dst */);
598 SelectObject( dst
, bits
);
599 BitBlt( dst
, 1, 1, width
, height
, src
, 0, 0, SRCPAINT
/* src | dst */ );
602 cursor
= XCreatePixmapCursor( gdi_display
, X11DRV_get_pixmap(bits
), X11DRV_get_pixmap(mask
),
603 fg
, bg
, hotspot_x
, hotspot_y
);
609 DeleteObject( bits
);
610 DeleteObject( mask
);
611 DeleteObject( mask_inv
);
615 /***********************************************************************
618 * Create an X cursor from a Windows one.
620 static Cursor
create_xlib_cursor( HDC hdc
, ICONINFO
*icon
, int width
, int height
)
623 Cursor cursor
= None
;
624 HBITMAP xor_bitmap
= 0;
626 unsigned int *color_bits
= NULL
, *ptr
;
627 unsigned char *mask_bits
= NULL
, *xor_bits
= NULL
;
628 int i
, x
, y
, has_alpha
= 0;
629 int rfg
, gfg
, bfg
, rbg
, gbg
, bbg
, fgBits
, bgBits
;
630 unsigned int width_bytes
= (width
+ 31) / 32 * 4;
632 if (!(info
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] ))))
634 info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
635 info
->bmiHeader
.biWidth
= width
;
636 info
->bmiHeader
.biHeight
= -height
;
637 info
->bmiHeader
.biPlanes
= 1;
638 info
->bmiHeader
.biBitCount
= 1;
639 info
->bmiHeader
.biCompression
= BI_RGB
;
640 info
->bmiHeader
.biSizeImage
= width_bytes
* height
;
641 info
->bmiHeader
.biXPelsPerMeter
= 0;
642 info
->bmiHeader
.biYPelsPerMeter
= 0;
643 info
->bmiHeader
.biClrUsed
= 0;
644 info
->bmiHeader
.biClrImportant
= 0;
646 if (!(mask_bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
))) goto done
;
647 if (!GetDIBits( hdc
, icon
->hbmMask
, 0, height
, mask_bits
, info
, DIB_RGB_COLORS
)) goto done
;
649 info
->bmiHeader
.biBitCount
= 32;
650 info
->bmiHeader
.biSizeImage
= width
* height
* 4;
651 if (!(color_bits
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
))) goto done
;
652 if (!(xor_bits
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, width_bytes
* height
))) goto done
;
653 GetDIBits( hdc
, icon
->hbmColor
, 0, height
, color_bits
, info
, DIB_RGB_COLORS
);
655 /* compute fg/bg color and xor bitmap based on average of the color values */
657 if (!(xor_bitmap
= CreateBitmap( width
, height
, 1, 1, NULL
))) goto done
;
658 rfg
= gfg
= bfg
= rbg
= gbg
= bbg
= fgBits
= 0;
659 for (y
= 0, ptr
= color_bits
; y
< height
; y
++)
661 for (x
= 0; x
< width
; x
++, ptr
++)
663 int red
= (*ptr
>> 16) & 0xff;
664 int green
= (*ptr
>> 8) & 0xff;
665 int blue
= (*ptr
>> 0) & 0xff;
666 if (red
+ green
+ blue
> 0x40)
672 xor_bits
[y
* width_bytes
+ x
/ 8] |= 0x80 >> (x
% 8);
684 fg
.red
= rfg
* 257 / fgBits
;
685 fg
.green
= gfg
* 257 / fgBits
;
686 fg
.blue
= bfg
* 257 / fgBits
;
688 else fg
.red
= fg
.green
= fg
.blue
= 0;
689 bgBits
= width
* height
- fgBits
;
692 bg
.red
= rbg
* 257 / bgBits
;
693 bg
.green
= gbg
* 257 / bgBits
;
694 bg
.blue
= bbg
* 257 / bgBits
;
696 else bg
.red
= bg
.green
= bg
.blue
= 0;
698 info
->bmiHeader
.biBitCount
= 1;
699 info
->bmiHeader
.biSizeImage
= width_bytes
* height
;
700 SetDIBits( hdc
, xor_bitmap
, 0, height
, xor_bits
, info
, DIB_RGB_COLORS
);
702 /* generate mask from the alpha channel if we have one */
704 for (i
= 0, ptr
= color_bits
; i
< width
* height
; i
++, ptr
++)
705 if ((has_alpha
= (*ptr
& 0xff000000) != 0)) break;
709 memset( mask_bits
, 0, width_bytes
* height
);
710 for (y
= 0, ptr
= color_bits
; y
< height
; y
++)
711 for (x
= 0; x
< width
; x
++, ptr
++)
712 if ((*ptr
>> 24) > 25) /* more than 10% alpha */
713 mask_bits
[y
* width_bytes
+ x
/ 8] |= 0x80 >> (x
% 8);
715 info
->bmiHeader
.biBitCount
= 1;
716 info
->bmiHeader
.biSizeImage
= width_bytes
* height
;
717 SetDIBits( hdc
, icon
->hbmMask
, 0, height
, mask_bits
, info
, DIB_RGB_COLORS
);
720 cursor
= XCreatePixmapCursor( gdi_display
,
721 X11DRV_get_pixmap(xor_bitmap
),
722 X11DRV_get_pixmap(icon
->hbmMask
),
723 &fg
, &bg
, icon
->xHotspot
, icon
->yHotspot
);
728 cursor
= create_cursor_from_bitmaps( xor_bitmap
, icon
->hbmMask
, width
, height
, 0, 0,
729 &fg
, &bg
, icon
->xHotspot
, icon
->yHotspot
);
733 DeleteObject( xor_bitmap
);
734 HeapFree( GetProcessHeap(), 0, info
);
735 HeapFree( GetProcessHeap(), 0, color_bits
);
736 HeapFree( GetProcessHeap(), 0, xor_bits
);
737 HeapFree( GetProcessHeap(), 0, mask_bits
);
741 /***********************************************************************
744 * Create an X cursor from a Windows one.
746 static Cursor
create_cursor( HANDLE handle
)
753 if (!handle
) return get_empty_cursor();
755 if (!(hdc
= CreateCompatibleDC( 0 ))) return 0;
756 if (!GetIconInfo( handle
, &info
))
762 GetObjectW( info
.hbmMask
, sizeof(bm
), &bm
);
763 if (!info
.hbmColor
) bm
.bmHeight
/= 2;
765 /* make sure hotspot is valid */
766 if (info
.xHotspot
>= bm
.bmWidth
|| info
.yHotspot
>= bm
.bmHeight
)
768 info
.xHotspot
= bm
.bmWidth
/ 2;
769 info
.yHotspot
= bm
.bmHeight
/ 2;
774 #ifdef SONAME_LIBXCURSOR
775 if (pXcursorImageLoadCursor
) cursor
= create_xcursor_cursor( hdc
, &info
, bm
.bmWidth
, bm
.bmHeight
);
777 if (!cursor
) cursor
= create_xlib_cursor( hdc
, &info
, bm
.bmWidth
, bm
.bmHeight
);
778 DeleteObject( info
.hbmColor
);
783 fg
.red
= fg
.green
= fg
.blue
= 0xffff;
784 bg
.red
= bg
.green
= bg
.blue
= 0;
785 cursor
= create_cursor_from_bitmaps( info
.hbmMask
, info
.hbmMask
, bm
.bmWidth
, bm
.bmHeight
,
786 bm
.bmHeight
, 0, &fg
, &bg
, info
.xHotspot
, info
.yHotspot
);
789 DeleteObject( info
.hbmMask
);
794 /***********************************************************************
795 * DestroyCursorIcon (X11DRV.@)
797 void CDECL
X11DRV_DestroyCursorIcon( HCURSOR handle
)
802 if (cursor_context
&& !XFindContext( gdi_display
, (XID
)handle
, cursor_context
, (char **)&cursor
))
804 TRACE( "%p xid %lx\n", handle
, cursor
);
805 XFreeCursor( gdi_display
, cursor
);
806 XDeleteContext( gdi_display
, (XID
)handle
, cursor_context
);
811 /***********************************************************************
812 * SetCursor (X11DRV.@)
814 void CDECL
X11DRV_SetCursor( HCURSOR handle
)
816 if (cursor_window
) SendNotifyMessageW( cursor_window
, WM_X11DRV_SET_CURSOR
, 0, (LPARAM
)handle
);
819 /***********************************************************************
820 * SetCursorPos (X11DRV.@)
822 BOOL CDECL
X11DRV_SetCursorPos( INT x
, INT y
)
824 Display
*display
= thread_init_display();
827 TRACE( "warping to (%d,%d)\n", x
, y
);
830 if (cursor_pos
.x
== x
&& cursor_pos
.y
== y
)
833 /* We still need to generate WM_MOUSEMOVE */
834 queue_raw_mouse_message( WM_MOUSEMOVE
, NULL
, x
, y
, 0, GetCurrentTime(), 0, 0 );
839 clip_point_to_rect( &cursor_clip
, &pt
);
840 XWarpPointer( display
, root_window
, root_window
, 0, 0, 0, 0,
841 pt
.x
- virtual_screen_rect
.left
, pt
.y
- virtual_screen_rect
.top
);
842 XFlush( display
); /* avoids bad mouse lag in games that do their own mouse warping */
848 /***********************************************************************
849 * GetCursorPos (X11DRV.@)
851 BOOL CDECL
X11DRV_GetCursorPos(LPPOINT pos
)
853 Display
*display
= thread_init_display();
855 int rootX
, rootY
, winX
, winY
;
859 if ((GetTickCount() - last_time_modified
> 100) &&
860 XQueryPointer( display
, root_window
, &root
, &child
,
861 &rootX
, &rootY
, &winX
, &winY
, &xstate
))
863 update_button_state( xstate
);
864 winX
+= virtual_screen_rect
.left
;
865 winY
+= virtual_screen_rect
.top
;
866 TRACE("pointer at (%d,%d)\n", winX
, winY
);
876 /***********************************************************************
877 * ClipCursor (X11DRV.@)
879 * Set the cursor clipping rectangle.
881 BOOL CDECL
X11DRV_ClipCursor( LPCRECT clip
)
883 if (!IntersectRect( &cursor_clip
, &virtual_screen_rect
, clip
))
884 cursor_clip
= virtual_screen_rect
;
889 /***********************************************************************
892 void X11DRV_ButtonPress( HWND hwnd
, XEvent
*xev
)
894 XButtonEvent
*event
= &xev
->xbutton
;
895 int buttonNum
= event
->button
- 1;
899 if (buttonNum
>= NB_BUTTONS
) return;
908 wData
= -WHEEL_DELTA
;
924 update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
926 X11DRV_send_mouse_input( hwnd
, button_down_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
,
927 pt
.x
, pt
.y
, wData
, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );
931 /***********************************************************************
932 * X11DRV_ButtonRelease
934 void X11DRV_ButtonRelease( HWND hwnd
, XEvent
*xev
)
936 XButtonEvent
*event
= &xev
->xbutton
;
937 int buttonNum
= event
->button
- 1;
941 if (buttonNum
>= NB_BUTTONS
|| !button_up_flags
[buttonNum
]) return;
960 update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
962 X11DRV_send_mouse_input( hwnd
, button_up_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
,
963 pt
.x
, pt
.y
, wData
, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );
967 /***********************************************************************
968 * X11DRV_MotionNotify
970 void X11DRV_MotionNotify( HWND hwnd
, XEvent
*xev
)
972 XMotionEvent
*event
= &xev
->xmotion
;
975 TRACE("hwnd %p, event->is_hint %d\n", hwnd
, event
->is_hint
);
979 update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
981 X11DRV_send_mouse_input( hwnd
, MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
,
982 pt
.x
, pt
.y
, 0, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );
986 /***********************************************************************
989 void X11DRV_EnterNotify( HWND hwnd
, XEvent
*xev
)
991 XCrossingEvent
*event
= &xev
->xcrossing
;
994 TRACE("hwnd %p, event->detail %d\n", hwnd
, event
->detail
);
997 if (event
->detail
== NotifyVirtual
|| event
->detail
== NotifyNonlinearVirtual
) return;
998 if (event
->window
== x11drv_thread_data()->grab_window
) return;
1000 /* simulate a mouse motion event */
1001 update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
1003 X11DRV_send_mouse_input( hwnd
, MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
,
1004 pt
.x
, pt
.y
, 0, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );