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
);
35 MAKE_FUNCPTR(XcursorImagesCreate
);
36 MAKE_FUNCPTR(XcursorImagesDestroy
);
37 MAKE_FUNCPTR(XcursorImagesLoadCursor
);
39 #endif /* SONAME_LIBXCURSOR */
41 #define NONAMELESSUNION
42 #define NONAMELESSSTRUCT
45 #include "wine/winuser16.h"
48 #include "wine/server.h"
49 #include "wine/library.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(cursor
);
54 /**********************************************************************/
57 #define Button6Mask (1<<13)
60 #define Button7Mask (1<<14)
63 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
65 static const UINT button_down_flags
[NB_BUTTONS
] =
68 MOUSEEVENTF_MIDDLEDOWN
,
69 MOUSEEVENTF_RIGHTDOWN
,
72 MOUSEEVENTF_XDOWN
, /* FIXME: horizontal wheel */
78 static const UINT button_up_flags
[NB_BUTTONS
] =
92 static DWORD last_time_modified
;
93 static RECT cursor_clip
; /* Cursor clipping rect */
95 BOOL
X11DRV_SetCursorPos( INT x
, INT y
);
98 /***********************************************************************
101 * Load the Xcursor library for use.
103 void X11DRV_Xcursor_Init(void)
105 #ifdef SONAME_LIBXCURSOR
106 xcursor_handle
= wine_dlopen(SONAME_LIBXCURSOR
, RTLD_NOW
, NULL
, 0);
107 if (!xcursor_handle
) /* wine_dlopen failed. */
109 WARN("Xcursor failed to load. Using fallback code.\n");
112 #define LOAD_FUNCPTR(f) \
113 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
115 LOAD_FUNCPTR(XcursorImageCreate
);
116 LOAD_FUNCPTR(XcursorImageDestroy
);
117 LOAD_FUNCPTR(XcursorImageLoadCursor
);
118 LOAD_FUNCPTR(XcursorImagesCreate
);
119 LOAD_FUNCPTR(XcursorImagesDestroy
);
120 LOAD_FUNCPTR(XcursorImagesLoadCursor
);
122 #endif /* SONAME_LIBXCURSOR */
126 /***********************************************************************
129 * get the coordinates of a mouse event
131 static inline void get_coords( HWND hwnd
, Window window
, int x
, int y
, POINT
*pt
)
133 struct x11drv_win_data
*data
= X11DRV_get_win_data( hwnd
);
137 if (window
== data
->client_window
)
139 pt
->x
= x
+ data
->client_rect
.left
;
140 pt
->y
= y
+ data
->client_rect
.top
;
144 pt
->x
= x
+ data
->whole_rect
.left
;
145 pt
->y
= y
+ data
->whole_rect
.top
;
149 /***********************************************************************
152 * Clip point to the provided rectangle
154 static inline void clip_point_to_rect( LPCRECT rect
, LPPOINT pt
)
156 if (pt
->x
< rect
->left
) pt
->x
= rect
->left
;
157 else if (pt
->x
>= rect
->right
) pt
->x
= rect
->right
- 1;
158 if (pt
->y
< rect
->top
) pt
->y
= rect
->top
;
159 else if (pt
->y
>= rect
->bottom
) pt
->y
= rect
->bottom
- 1;
162 /***********************************************************************
163 * update_button_state
165 * Update the button state with what X provides us
167 static inline void update_button_state( unsigned int state
)
169 key_state_table
[VK_LBUTTON
] = (state
& Button1Mask
? 0x80 : 0);
170 key_state_table
[VK_MBUTTON
] = (state
& Button2Mask
? 0x80 : 0);
171 key_state_table
[VK_RBUTTON
] = (state
& Button3Mask
? 0x80 : 0);
172 /* X-buttons are not reported from XQueryPointer */
176 /***********************************************************************
179 * Update the various window states on a mouse event.
181 static void update_mouse_state( HWND hwnd
, Window window
, int x
, int y
, unsigned int state
, POINT
*pt
)
183 struct x11drv_thread_data
*data
= x11drv_thread_data();
185 get_coords( hwnd
, window
, x
, y
, pt
);
187 /* update the cursor */
189 if (data
->cursor_window
!= window
)
191 data
->cursor_window
= window
;
193 if (data
->cursor
) XDefineCursor( data
->display
, window
, data
->cursor
);
197 /* update the wine server Z-order */
199 if (window
!= data
->grab_window
&&
200 /* ignore event if a button is pressed, since the mouse is then grabbed too */
201 !(state
& (Button1Mask
|Button2Mask
|Button3Mask
|Button4Mask
|Button5Mask
|Button6Mask
|Button7Mask
)))
203 SERVER_START_REQ( update_window_zorder
)
206 req
->rect
.left
= pt
->x
;
207 req
->rect
.top
= pt
->y
;
208 req
->rect
.right
= pt
->x
+ 1;
209 req
->rect
.bottom
= pt
->y
+ 1;
210 wine_server_call( req
);
217 /***********************************************************************
220 static WORD
get_key_state(void)
224 if (GetSystemMetrics( SM_SWAPBUTTON
))
226 if (key_state_table
[VK_RBUTTON
] & 0x80) ret
|= MK_LBUTTON
;
227 if (key_state_table
[VK_LBUTTON
] & 0x80) ret
|= MK_RBUTTON
;
231 if (key_state_table
[VK_LBUTTON
] & 0x80) ret
|= MK_LBUTTON
;
232 if (key_state_table
[VK_RBUTTON
] & 0x80) ret
|= MK_RBUTTON
;
234 if (key_state_table
[VK_MBUTTON
] & 0x80) ret
|= MK_MBUTTON
;
235 if (key_state_table
[VK_SHIFT
] & 0x80) ret
|= MK_SHIFT
;
236 if (key_state_table
[VK_CONTROL
] & 0x80) ret
|= MK_CONTROL
;
237 if (key_state_table
[VK_XBUTTON1
] & 0x80) ret
|= MK_XBUTTON1
;
238 if (key_state_table
[VK_XBUTTON2
] & 0x80) ret
|= MK_XBUTTON2
;
243 /***********************************************************************
244 * queue_raw_mouse_message
246 static void queue_raw_mouse_message( UINT message
, HWND hwnd
, DWORD x
, DWORD y
,
247 DWORD data
, DWORD time
, DWORD extra_info
, UINT injected_flags
)
253 hook
.mouseData
= MAKELONG( 0, data
);
254 hook
.flags
= injected_flags
;
256 hook
.dwExtraInfo
= extra_info
;
258 last_time_modified
= GetTickCount();
259 if (HOOK_CallHooks( WH_MOUSE_LL
, HC_ACTION
, message
, (LPARAM
)&hook
, TRUE
)) return;
261 SERVER_START_REQ( send_hardware_message
)
263 req
->id
= (injected_flags
& LLMHF_INJECTED
) ? 0 : GetCurrentThreadId();
266 req
->wparam
= MAKEWPARAM( get_key_state(), data
);
271 req
->info
= extra_info
;
272 wine_server_call( req
);
279 /***********************************************************************
280 * X11DRV_send_mouse_input
282 void X11DRV_send_mouse_input( HWND hwnd
, DWORD flags
, DWORD x
, DWORD y
,
283 DWORD data
, DWORD time
, DWORD extra_info
, UINT injected_flags
)
287 if (flags
& MOUSEEVENTF_MOVE
&& flags
& MOUSEEVENTF_ABSOLUTE
)
289 if (injected_flags
& LLMHF_INJECTED
)
291 pt
.x
= (x
* screen_width
) >> 16;
292 pt
.y
= (y
* screen_height
) >> 16;
299 if (cursor_pos
.x
== x
&& cursor_pos
.y
== y
&&
300 (flags
& ~(MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
)))
301 flags
&= ~MOUSEEVENTF_MOVE
;
305 else if (flags
& MOUSEEVENTF_MOVE
)
307 int accel
[3], xMult
= 1, yMult
= 1;
309 /* dx and dy can be negative numbers for relative movements */
310 SystemParametersInfoW(SPI_GETMOUSE
, 0, accel
, 0);
312 if (abs(x
) > accel
[0] && accel
[2] != 0)
315 if ((abs(x
) > accel
[1]) && (accel
[2] == 2)) xMult
= 4;
317 if (abs(y
) > accel
[0] && accel
[2] != 0)
320 if ((abs(y
) > accel
[1]) && (accel
[2] == 2)) yMult
= 4;
324 pt
.x
= cursor_pos
.x
+ (long)x
* xMult
;
325 pt
.y
= cursor_pos
.y
+ (long)y
* yMult
;
335 if (flags
& MOUSEEVENTF_MOVE
)
337 queue_raw_mouse_message( WM_MOUSEMOVE
, hwnd
, pt
.x
, pt
.y
, data
, time
,
338 extra_info
, injected_flags
);
339 if ((injected_flags
& LLMHF_INJECTED
) &&
340 ((flags
& MOUSEEVENTF_ABSOLUTE
) || x
|| y
)) /* we have to actually move the cursor */
342 X11DRV_SetCursorPos( pt
.x
, pt
.y
);
347 clip_point_to_rect( &cursor_clip
, &pt
);
352 if (flags
& MOUSEEVENTF_LEFTDOWN
)
354 key_state_table
[VK_LBUTTON
] |= 0xc0;
355 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_RBUTTONDOWN
: WM_LBUTTONDOWN
,
356 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
358 if (flags
& MOUSEEVENTF_LEFTUP
)
360 key_state_table
[VK_LBUTTON
] &= ~0x80;
361 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_RBUTTONUP
: WM_LBUTTONUP
,
362 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
364 if (flags
& MOUSEEVENTF_RIGHTDOWN
)
366 key_state_table
[VK_RBUTTON
] |= 0xc0;
367 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_LBUTTONDOWN
: WM_RBUTTONDOWN
,
368 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
370 if (flags
& MOUSEEVENTF_RIGHTUP
)
372 key_state_table
[VK_RBUTTON
] &= ~0x80;
373 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_LBUTTONUP
: WM_RBUTTONUP
,
374 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
376 if (flags
& MOUSEEVENTF_MIDDLEDOWN
)
378 key_state_table
[VK_MBUTTON
] |= 0xc0;
379 queue_raw_mouse_message( WM_MBUTTONDOWN
, hwnd
, pt
.x
, pt
.y
, data
, time
,
380 extra_info
, injected_flags
);
382 if (flags
& MOUSEEVENTF_MIDDLEUP
)
384 key_state_table
[VK_MBUTTON
] &= ~0x80;
385 queue_raw_mouse_message( WM_MBUTTONUP
, hwnd
, pt
.x
, pt
.y
, data
, time
,
386 extra_info
, injected_flags
);
388 if (flags
& MOUSEEVENTF_WHEEL
)
390 queue_raw_mouse_message( WM_MOUSEWHEEL
, hwnd
, pt
.x
, pt
.y
, data
, time
,
391 extra_info
, injected_flags
);
393 if (flags
& MOUSEEVENTF_XDOWN
)
395 key_state_table
[VK_XBUTTON1
+ data
- 1] |= 0xc0;
396 queue_raw_mouse_message( WM_XBUTTONDOWN
, hwnd
, pt
.x
, pt
.y
, data
, time
,
397 extra_info
, injected_flags
);
399 if (flags
& MOUSEEVENTF_XUP
)
401 key_state_table
[VK_XBUTTON1
+ data
- 1] &= ~0x80;
402 queue_raw_mouse_message( WM_XBUTTONUP
, hwnd
, pt
.x
, pt
.y
, data
, time
,
403 extra_info
, injected_flags
);
408 #ifdef SONAME_LIBXCURSOR
410 /***********************************************************************
411 * create_cursor_image
413 * Create an XcursorImage from a cursor_frame_t
415 static XcursorImage
*create_cursor_image( cursor_frame_t
*frame
)
420 unsigned char *and_bits
, *and_ptr
, *xor_bits
, *xor_ptr
;
421 XcursorPixel
*pixel_ptr
;
423 BOOL alpha_zero
= TRUE
;
425 and_size
= frame
->and_width_bytes
* frame
->height
;
426 and_ptr
= and_bits
= frame
->bits
;
428 xor_ptr
= xor_bits
= frame
->bits
+ and_size
;
430 image
= pXcursorImageCreate( frame
->width
, frame
->height
);
431 pixel_ptr
= image
->pixels
;
433 /* Generally 32 bit bitmaps have an alpha channel which is used in favor
434 * of the AND mask. However, if all pixels have alpha = 0x00, the bitmap
435 * is treated like one without alpha and the masks are used. As soon as
436 * one pixel has alpha != 0x00, and the mask ignored as described in the
439 * This is most likely for applications which create the bitmaps with
440 * CreateDIBitmap, which creates a device dependent bitmap, so the format
441 * that arrives when loading depends on the screen's bpp. Apps that were
442 * written at 8 / 16 bpp times do not know about the 32 bit alpha, so
443 * they would get a completely transparent cursor on 32 bit displays.
445 * Non-32 bit bitmaps always use the AND mask
449 for (y
= 0; alpha_zero
&& y
< frame
->height
; ++y
)
451 xor_ptr
= xor_bits
+ (y
* frame
->xor_width_bytes
);
452 for (x
= 0; x
< frame
->width
; ++x
)
454 if (xor_ptr
[3] != 0x00)
464 /* On windows, to calculate the color for a pixel, first an AND is done
465 * with the background and the "and" bitmap, then an XOR with the "xor"
466 * bitmap. This means that when the data in the "and" bitmap is 0, the
467 * pixel will get the color as specified in the "xor" bitmap.
468 * However, if the data in the "and" bitmap is 1, the result will be the
469 * background XOR'ed with the value in the "xor" bitmap. In case the "xor"
470 * data is completely black (0x000000) the pixel will become transparent,
471 * in case it's white (0xffffff) the pixel will become the inverse of the
474 * Since we can't support inverting colors, we map the grayscale value of
475 * the "xor" data to the alpha channel, and xor the color with either
478 for (y
= 0; y
< frame
->height
; ++y
)
480 and_ptr
= and_bits
+ (y
* frame
->and_width_bytes
);
481 xor_ptr
= xor_bits
+ (y
* frame
->xor_width_bytes
);
483 for (x
= 0; x
< frame
->width
; ++x
)
485 /* Xcursor pixel data is in ARGB format, with A in the high byte */
489 /* BGRA, 8 bits each */
490 *pixel_ptr
= *xor_ptr
++;
491 *pixel_ptr
|= *xor_ptr
++ << 8;
492 *pixel_ptr
|= *xor_ptr
++ << 16;
493 *pixel_ptr
|= *xor_ptr
++ << 24;
497 /* BGR, 8 bits each */
498 *pixel_ptr
= *xor_ptr
++;
499 *pixel_ptr
|= *xor_ptr
++ << 8;
500 *pixel_ptr
|= *xor_ptr
++ << 16;
504 /* BGR, 5 red, 6 green, 5 blue */
505 *pixel_ptr
= *xor_ptr
* 0x1f;
506 *pixel_ptr
|= (*xor_ptr
& 0xe0) << 3;
508 *pixel_ptr
|= (*xor_ptr
& 0x07) << 11;
509 *pixel_ptr
|= (*xor_ptr
& 0xf8) << 13;
513 if (*xor_ptr
& (1 << (7 - (x
& 7)))) *pixel_ptr
= 0xffffff;
515 if ((x
& 7) == 7) ++xor_ptr
;
519 FIXME("Currently no support for cursors with %d bits per pixel\n", frame
->bpp
);
526 if (~*and_ptr
& (1 << (7 - (x
& 7)))) *pixel_ptr
|= 0xff << 24;
529 int alpha
= (*pixel_ptr
& 0xff) * 0.30f
530 + ((*pixel_ptr
& 0xff00) >> 8) * 0.55f
531 + ((*pixel_ptr
& 0xff0000) >> 16) * 0.15f
;
532 *pixel_ptr
^= ((x
+ y
) % 2) ? 0xffffff : 0x000000;
533 *pixel_ptr
|= alpha
<< 24;
535 if ((x
& 7) == 7) ++and_ptr
;
545 /***********************************************************************
546 * create_xcursor_cursor
548 * Use Xcursor to create an X cursor from a Windows one.
550 static Cursor
create_xcursor_cursor( Display
*display
, const cursor_t
*cursor_object
)
555 XcursorImages
*images
;
557 if (!cursor_object
) /* Create an empty cursor */
559 image
= pXcursorImageCreate( 1, 1 );
562 *(image
->pixels
) = 0;
563 cursor
= pXcursorImageLoadCursor( display
, image
);
564 pXcursorImageDestroy( image
);
569 images
= pXcursorImagesCreate( cursor_object
->num_frames
);
570 for (i
= 0; i
< cursor_object
->num_frames
; ++i
)
572 cursor_frame_t
*frame
= &cursor_object
->frames
[i
];
573 image
= create_cursor_image( frame
);
574 if (!image
) return 0;
576 /* Make sure hotspot is valid */
577 image
->xhot
= frame
->xhot
;
578 image
->yhot
= frame
->yhot
;
579 if (image
->xhot
>= image
->width
||
580 image
->yhot
>= image
->height
)
582 image
->xhot
= image
->width
/ 2;
583 image
->yhot
= image
->height
/ 2;
586 image
->delay
= cursor_object
->delay
;
588 images
->images
[images
->nimage
++] = image
;
591 cursor
= pXcursorImagesLoadCursor( display
, images
);
592 pXcursorImagesDestroy( images
);
597 #endif /* SONAME_LIBXCURSOR */
600 /***********************************************************************
603 * Create an X cursor from a Windows one.
605 static Cursor
create_cursor( Display
*display
, const cursor_t
*ptr
)
607 Pixmap pixmapBits
, pixmapMask
, pixmapMaskInv
= 0, pixmapAll
;
609 Cursor cursor
= None
;
611 char *bitMask32
= NULL
;
613 #ifdef SONAME_LIBXCURSOR
614 if (pXcursorImageLoadCursor
) return create_xcursor_cursor( display
, ptr
);
617 if (!ptr
) /* Create an empty cursor */
619 static const char data
[] = { 0 };
621 bg
.red
= bg
.green
= bg
.blue
= 0x0000;
622 pixmapBits
= XCreateBitmapFromData( display
, root_window
, data
, 1, 1 );
625 cursor
= XCreatePixmapCursor( display
, pixmapBits
, pixmapBits
,
627 XFreePixmap( display
, pixmapBits
);
630 else /* Create the X cursor from the bits */
632 cursor_frame_t
*frame
= &ptr
->frames
[0];
636 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
637 frame
->width
, frame
->height
, frame
->planes
, frame
->bpp
,
638 frame
->xor_width_bytes
);
640 /* Create a pixmap and transfer all the bits to it */
642 /* NOTE: Following hack works, but only because XFree depth
643 * 1 images really use 1 bit/pixel (and so the same layout
644 * as the Windows cursor data). Perhaps use a more generic
647 /* This pixmap will be written with two bitmaps. The first is
648 * the mask and the second is the image.
650 if (!(pixmapAll
= XCreatePixmap( display
, root_window
,
651 frame
->width
, frame
->height
* 2, 1 )))
653 if (!(image
= XCreateImage( display
, visual
,
654 1, ZPixmap
, 0, (char *)frame
->bits
, frame
->width
,
655 frame
->height
* 2, 16, frame
->xor_width_bytes
/frame
->bpp
)))
657 XFreePixmap( display
, pixmapAll
);
660 gc
= XCreateGC( display
, pixmapAll
, 0, NULL
);
661 XSetGraphicsExposures( display
, gc
, False
);
662 image
->byte_order
= MSBFirst
;
663 image
->bitmap_bit_order
= MSBFirst
;
664 image
->bitmap_unit
= 16;
665 _XInitImageFuncPtrs(image
);
666 if (frame
->planes
* frame
->bpp
== 1)
668 /* A plain old white on black cursor. */
669 fg
.red
= fg
.green
= fg
.blue
= 0xffff;
670 bg
.red
= bg
.green
= bg
.blue
= 0x0000;
671 XPutImage( display
, pixmapAll
, gc
, image
,
672 0, 0, 0, 0, frame
->width
, frame
->height
* 2 );
676 int rbits
, gbits
, bbits
, red
, green
, blue
;
677 int rfg
, gfg
, bfg
, rbg
, gbg
, bbg
;
678 int rscale
, gscale
, bscale
;
679 int x
, y
, xmax
, ymax
, byteIndex
, xorIndex
;
680 unsigned char *theMask
, *theImage
, theChar
;
681 int threshold
, fgBits
, bgBits
, bitShifted
;
682 BYTE pXorBits
[128]; /* Up to 32x32 icons */
687 bitMask32
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
688 frame
->width
* frame
->height
/ 8 );
703 FIXME("Currently no support for cursors with %d bits per pixel\n",
705 XFreePixmap( display
, pixmapAll
);
706 XFreeGC( display
, gc
);
708 XDestroyImage( image
);
711 /* The location of the mask. */
712 theMask
= frame
->bits
;
713 /* The mask should still be 1 bit per pixel. The color image
714 * should immediately follow the mask.
716 theImage
= &theMask
[frame
->width
/8 * frame
->height
];
717 rfg
= gfg
= bfg
= rbg
= gbg
= bbg
= 0;
722 xmax
= (frame
->width
> 32) ? 32 : frame
->width
;
723 if (frame
->width
> 32) {
724 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
725 frame
->width
, frame
->height
);
727 ymax
= (frame
->height
> 32) ? 32 : frame
->height
;
729 memset(pXorBits
, 0, 128);
730 for (y
=0; y
<ymax
; y
++)
732 for (x
=0; x
<xmax
; x
++)
734 red
= green
= blue
= 0;
738 theChar
= theImage
[byteIndex
++];
740 theChar
= theImage
[byteIndex
++];
742 theChar
= theImage
[byteIndex
++];
744 theChar
= theImage
[byteIndex
++];
745 /* If the alpha channel is >5% transparent,
746 * assume that we can add it to the bitMask32.
749 *(bitMask32
+ (y
*xmax
+x
)/8) |= 1 << (x
& 7);
752 theChar
= theImage
[byteIndex
++];
754 theChar
= theImage
[byteIndex
++];
756 theChar
= theImage
[byteIndex
++];
760 theChar
= theImage
[byteIndex
++];
761 blue
= theChar
& 0x1F;
762 green
= (theChar
& 0xE0) >> 5;
763 theChar
= theImage
[byteIndex
++];
764 green
|= (theChar
& 0x07) << 3;
765 red
= (theChar
& 0xF8) >> 3;
769 if (red
+green
+blue
> threshold
)
775 pXorBits
[xorIndex
] |= bitShifted
;
789 bitShifted
= bitShifted
<< 1;
792 rscale
= 1 << (16 - rbits
);
793 gscale
= 1 << (16 - gbits
);
794 bscale
= 1 << (16 - bbits
);
797 fg
.red
= rfg
* rscale
/ fgBits
;
798 fg
.green
= gfg
* gscale
/ fgBits
;
799 fg
.blue
= bfg
* bscale
/ fgBits
;
801 else fg
.red
= fg
.green
= fg
.blue
= 0;
802 bgBits
= xmax
* ymax
- fgBits
;
805 bg
.red
= rbg
* rscale
/ bgBits
;
806 bg
.green
= gbg
* gscale
/ bgBits
;
807 bg
.blue
= bbg
* bscale
/ bgBits
;
809 else bg
.red
= bg
.green
= bg
.blue
= 0;
810 pixmapBits
= XCreateBitmapFromData( display
, root_window
, (char *)pXorBits
, xmax
, ymax
);
813 HeapFree( GetProcessHeap(), 0, bitMask32
);
814 XFreePixmap( display
, pixmapAll
);
815 XFreeGC( display
, gc
);
817 XDestroyImage( image
);
822 XPutImage( display
, pixmapAll
, gc
, image
,
823 0, 0, 0, 0, frame
->width
, frame
->height
);
824 XSetFunction( display
, gc
, GXcopy
);
826 XCopyArea( display
, pixmapBits
, pixmapAll
, gc
,
827 0, 0, xmax
, ymax
, 0, frame
->height
);
828 XFreePixmap( display
, pixmapBits
);
831 XDestroyImage( image
);
833 /* Now create the 2 pixmaps for bits and mask */
835 pixmapBits
= XCreatePixmap( display
, root_window
, frame
->width
, frame
->height
, 1 );
836 if (frame
->bpp
!= 32)
838 pixmapMaskInv
= XCreatePixmap( display
, root_window
, frame
->width
, frame
->height
, 1 );
839 pixmapMask
= XCreatePixmap( display
, root_window
, frame
->width
, frame
->height
, 1 );
841 /* Make sure everything went OK so far */
842 if (pixmapBits
&& pixmapMask
&& pixmapMaskInv
)
844 /* We have to do some magic here, as cursors are not fully
845 * compatible between Windows and X11. Under X11, there are
846 * only 3 possible color cursor: black, white and masked. So
847 * we map the 4th Windows color (invert the bits on the screen)
848 * to black and an additional white bit on an other place
849 * (+1,+1). This require some boolean arithmetic:
852 * And Xor Result | Bits Mask Result
853 * 0 0 black | 0 1 background
854 * 0 1 white | 1 1 foreground
855 * 1 0 no change | X 0 no change
856 * 1 1 inverted | 0 1 background
859 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
860 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
862 * FIXME: apparently some servers do support 'inverted' color.
863 * I don't know if it's correct per the X spec, but maybe we
864 * ought to take advantage of it. -- AJ
866 XSetFunction( display
, gc
, GXcopy
);
867 XCopyArea( display
, pixmapAll
, pixmapBits
, gc
,
868 0, 0, frame
->width
, frame
->height
, 0, 0 );
869 XCopyArea( display
, pixmapAll
, pixmapMask
, gc
,
870 0, 0, frame
->width
, frame
->height
, 0, 0 );
871 XCopyArea( display
, pixmapAll
, pixmapMaskInv
, gc
,
872 0, 0, frame
->width
, frame
->height
, 0, 0 );
873 XSetFunction( display
, gc
, GXand
);
874 XCopyArea( display
, pixmapAll
, pixmapMaskInv
, gc
,
875 0, frame
->height
, frame
->width
, frame
->height
, 0, 0 );
876 XSetFunction( display
, gc
, GXandReverse
);
877 XCopyArea( display
, pixmapAll
, pixmapBits
, gc
,
878 0, frame
->height
, frame
->width
, frame
->height
, 0, 0 );
879 XSetFunction( display
, gc
, GXorReverse
);
880 XCopyArea( display
, pixmapAll
, pixmapMask
, gc
,
881 0, frame
->height
, frame
->width
, frame
->height
, 0, 0 );
882 /* Additional white */
883 XSetFunction( display
, gc
, GXor
);
884 XCopyArea( display
, pixmapMaskInv
, pixmapMask
, gc
,
885 0, 0, frame
->width
, frame
->height
, 1, 1 );
886 XCopyArea( display
, pixmapMaskInv
, pixmapBits
, gc
,
887 0, 0, frame
->width
, frame
->height
, 1, 1 );
888 XSetFunction( display
, gc
, GXcopy
);
893 pixmapMask
= XCreateBitmapFromData( display
, root_window
,
894 bitMask32
, frame
->width
,
896 HeapFree( GetProcessHeap(), 0, bitMask32
);
899 /* Make sure hotspot is valid */
900 hotspot
.x
= frame
->xhot
;
901 hotspot
.y
= frame
->yhot
;
902 if (hotspot
.x
< 0 || hotspot
.x
>= frame
->width
||
903 hotspot
.y
< 0 || hotspot
.y
>= frame
->height
)
905 hotspot
.x
= frame
->width
/ 2;
906 hotspot
.y
= frame
->height
/ 2;
909 if (pixmapBits
&& pixmapMask
)
910 cursor
= XCreatePixmapCursor( display
, pixmapBits
, pixmapMask
,
911 &fg
, &bg
, hotspot
.x
, hotspot
.y
);
913 /* Now free everything */
915 if (pixmapAll
) XFreePixmap( display
, pixmapAll
);
916 if (pixmapBits
) XFreePixmap( display
, pixmapBits
);
917 if (pixmapMask
) XFreePixmap( display
, pixmapMask
);
918 if (pixmapMaskInv
) XFreePixmap( display
, pixmapMaskInv
);
919 XFreeGC( display
, gc
);
925 /***********************************************************************
926 * SetCursor (X11DRV.@)
928 void X11DRV_SetCursor( const cursor_t
*cursor_object
)
930 struct x11drv_thread_data
*data
= x11drv_init_thread_data();
936 for (i
= 0; i
< cursor_object
->num_frames
; ++i
)
938 cursor_frame_t
*frame
= cursor_object
->frames
+ i
;
939 TRACE("frame %u, %ux%u, planes %u, bpp %u\n",
940 i
, frame
->width
, frame
->height
, frame
->planes
, frame
->bpp
);
942 } else TRACE("NULL\n");
944 /* set the same cursor for all top-level windows of the current thread */
947 cursor
= create_cursor( data
->display
, cursor_object
);
950 if (data
->cursor
) XFreeCursor( data
->display
, data
->cursor
);
951 data
->cursor
= cursor
;
952 if (data
->cursor_window
)
954 XDefineCursor( data
->display
, data
->cursor_window
, cursor
);
955 /* Make the change take effect immediately */
956 XFlush( data
->display
);
962 /***********************************************************************
963 * SetCursorPos (X11DRV.@)
965 BOOL
X11DRV_SetCursorPos( INT x
, INT y
)
967 Display
*display
= thread_init_display();
970 TRACE( "warping to (%d,%d)\n", x
, y
);
973 if (cursor_pos
.x
== x
&& cursor_pos
.y
== y
)
976 /* We still need to generate WM_MOUSEMOVE */
977 queue_raw_mouse_message( WM_MOUSEMOVE
, NULL
, x
, y
, 0, GetCurrentTime(), 0, 0 );
982 clip_point_to_rect( &cursor_clip
, &pt
);
983 XWarpPointer( display
, root_window
, root_window
, 0, 0, 0, 0,
984 pt
.x
- virtual_screen_rect
.left
, pt
.y
- virtual_screen_rect
.top
);
985 XFlush( display
); /* avoids bad mouse lag in games that do their own mouse warping */
991 /***********************************************************************
992 * GetCursorPos (X11DRV.@)
994 BOOL
X11DRV_GetCursorPos(LPPOINT pos
)
996 Display
*display
= thread_init_display();
998 int rootX
, rootY
, winX
, winY
;
1002 if ((GetTickCount() - last_time_modified
> 100) &&
1003 XQueryPointer( display
, root_window
, &root
, &child
,
1004 &rootX
, &rootY
, &winX
, &winY
, &xstate
))
1006 update_button_state( xstate
);
1007 winX
+= virtual_screen_rect
.left
;
1008 winY
+= virtual_screen_rect
.top
;
1009 TRACE("pointer at (%d,%d)\n", winX
, winY
);
1010 cursor_pos
.x
= winX
;
1011 cursor_pos
.y
= winY
;
1014 wine_tsx11_unlock();
1019 /***********************************************************************
1020 * ClipCursor (X11DRV.@)
1022 * Set the cursor clipping rectangle.
1024 BOOL
X11DRV_ClipCursor( LPCRECT clip
)
1026 if (!IntersectRect( &cursor_clip
, &virtual_screen_rect
, clip
))
1027 cursor_clip
= virtual_screen_rect
;
1032 /***********************************************************************
1033 * X11DRV_ButtonPress
1035 void X11DRV_ButtonPress( HWND hwnd
, XEvent
*xev
)
1037 XButtonEvent
*event
= &xev
->xbutton
;
1038 int buttonNum
= event
->button
- 1;
1042 if (buttonNum
>= NB_BUTTONS
) return;
1048 wData
= WHEEL_DELTA
;
1051 wData
= -WHEEL_DELTA
;
1067 update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
1069 X11DRV_send_mouse_input( hwnd
, button_down_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
,
1070 pt
.x
, pt
.y
, wData
, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );
1074 /***********************************************************************
1075 * X11DRV_ButtonRelease
1077 void X11DRV_ButtonRelease( HWND hwnd
, XEvent
*xev
)
1079 XButtonEvent
*event
= &xev
->xbutton
;
1080 int buttonNum
= event
->button
- 1;
1084 if (buttonNum
>= NB_BUTTONS
|| !button_up_flags
[buttonNum
]) return;
1103 update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
1105 X11DRV_send_mouse_input( hwnd
, button_up_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
,
1106 pt
.x
, pt
.y
, wData
, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );
1110 /***********************************************************************
1111 * X11DRV_MotionNotify
1113 void X11DRV_MotionNotify( HWND hwnd
, XEvent
*xev
)
1115 XMotionEvent
*event
= &xev
->xmotion
;
1118 TRACE("hwnd %p, event->is_hint %d\n", hwnd
, event
->is_hint
);
1122 update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
1124 X11DRV_send_mouse_input( hwnd
, MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
,
1125 pt
.x
, pt
.y
, 0, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );
1129 /***********************************************************************
1130 * X11DRV_EnterNotify
1132 void X11DRV_EnterNotify( HWND hwnd
, XEvent
*xev
)
1134 XCrossingEvent
*event
= &xev
->xcrossing
;
1137 TRACE("hwnd %p, event->detail %d\n", hwnd
, event
->detail
);
1140 if (event
->detail
== NotifyVirtual
|| event
->detail
== NotifyNonlinearVirtual
) return;
1141 if (event
->window
== x11drv_thread_data()->grab_window
) return;
1143 /* simulate a mouse motion event */
1144 update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
1146 X11DRV_send_mouse_input( hwnd
, MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
,
1147 pt
.x
, pt
.y
, 0, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );