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 HAVE_X11_XCURSOR_XCURSOR_H
29 # include <X11/Xcursor/Xcursor.h>
30 # ifndef SONAME_LIBXCURSOR
31 # define SONAME_LIBXCURSOR "libXcursor.so"
33 static void *xcursor_handle
;
34 # define MAKE_FUNCPTR(f) static typeof(f) * p##f
35 MAKE_FUNCPTR(XcursorImageCreate
);
36 MAKE_FUNCPTR(XcursorImageDestroy
);
37 MAKE_FUNCPTR(XcursorImageLoadCursor
);
39 #endif /* HAVE_X11_XCURSOR_XCURSOR_H */
41 #define NONAMELESSUNION
42 #define NONAMELESSSTRUCT
45 #include "wine/winuser16.h"
49 #include "wine/server.h"
50 #include "wine/library.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(cursor
);
55 /**********************************************************************/
58 #define Button6Mask (1<<13)
61 #define Button7Mask (1<<14)
64 #define NB_BUTTONS 7 /* Windows can handle 5 buttons and the wheel too */
66 static const UINT button_down_flags
[NB_BUTTONS
] =
69 MOUSEEVENTF_MIDDLEDOWN
,
70 MOUSEEVENTF_RIGHTDOWN
,
77 static const UINT button_up_flags
[NB_BUTTONS
] =
89 static DWORD last_time_modified
;
90 static RECT cursor_clip
; /* Cursor clipping rect */
92 BOOL
X11DRV_SetCursorPos( INT x
, INT y
);
93 extern DWORD
X11DRV_MsgWaitForMultipleObjectsEx( DWORD count
,
94 const HANDLE
*handles
, DWORD timeout
, DWORD mask
, DWORD flags
);
97 /***********************************************************************
100 * Load the Xcursor library for use.
102 void X11DRV_Xcursor_Init(void)
104 #ifdef HAVE_X11_XCURSOR_XCURSOR_H
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 /* HAVE_X11_XCURSOR_XCURSOR_H */
122 /***********************************************************************
125 * get the coordinates of a mouse event
127 static inline HWND
get_coords( HWND hwnd
, int x
, int y
, POINT
*pt
)
129 struct x11drv_win_data
*data
= X11DRV_get_win_data( hwnd
);
131 if (!data
) return hwnd
;
133 pt
->x
= x
+ data
->whole_rect
.left
;
134 pt
->y
= y
+ data
->whole_rect
.top
;
138 MapWindowPoints( GetAncestor( hwnd
, GA_PARENT
), 0, pt
, 1 );
139 hwnd
= GetAncestor( hwnd
, GA_ROOT
);
144 /***********************************************************************
147 * Clip point to the provided rectangle
149 static inline void clip_point_to_rect( LPCRECT rect
, LPPOINT pt
)
151 if (pt
->x
< rect
->left
) pt
->x
= rect
->left
;
152 else if (pt
->x
>= rect
->right
) pt
->x
= rect
->right
- 1;
153 if (pt
->y
< rect
->top
) pt
->y
= rect
->top
;
154 else if (pt
->y
>= rect
->bottom
) pt
->y
= rect
->bottom
- 1;
157 /***********************************************************************
158 * update_button_state
160 * Update the button state with what X provides us
162 static inline void update_button_state( unsigned int state
)
164 key_state_table
[VK_LBUTTON
] = (state
& Button1Mask
? 0x80 : 0);
165 key_state_table
[VK_MBUTTON
] = (state
& Button2Mask
? 0x80 : 0);
166 key_state_table
[VK_RBUTTON
] = (state
& Button3Mask
? 0x80 : 0);
167 /* X-buttons are not reported from XQueryPointer */
171 /***********************************************************************
174 * Update the various window states on a mouse event.
176 static HWND
update_mouse_state( HWND hwnd
, Window window
, int x
, int y
, unsigned int state
, POINT
*pt
)
178 struct x11drv_thread_data
*data
= x11drv_thread_data();
180 if (window
== root_window
)
182 x
+= virtual_screen_rect
.left
;
183 y
+= virtual_screen_rect
.top
;
185 hwnd
= get_coords( hwnd
, 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
);
218 /***********************************************************************
221 static WORD
get_key_state(void)
225 if (GetSystemMetrics( SM_SWAPBUTTON
))
227 if (key_state_table
[VK_RBUTTON
] & 0x80) ret
|= MK_LBUTTON
;
228 if (key_state_table
[VK_LBUTTON
] & 0x80) ret
|= MK_RBUTTON
;
232 if (key_state_table
[VK_LBUTTON
] & 0x80) ret
|= MK_LBUTTON
;
233 if (key_state_table
[VK_RBUTTON
] & 0x80) ret
|= MK_RBUTTON
;
235 if (key_state_table
[VK_MBUTTON
] & 0x80) ret
|= MK_MBUTTON
;
236 if (key_state_table
[VK_SHIFT
] & 0x80) ret
|= MK_SHIFT
;
237 if (key_state_table
[VK_CONTROL
] & 0x80) ret
|= MK_CONTROL
;
238 if (key_state_table
[VK_XBUTTON1
] & 0x80) ret
|= MK_XBUTTON1
;
239 if (key_state_table
[VK_XBUTTON2
] & 0x80) ret
|= MK_XBUTTON2
;
244 /***********************************************************************
245 * queue_raw_mouse_message
247 static void queue_raw_mouse_message( UINT message
, HWND hwnd
, DWORD x
, DWORD y
,
248 DWORD data
, DWORD time
, DWORD extra_info
, UINT injected_flags
)
254 hook
.mouseData
= MAKELONG( 0, data
);
255 hook
.flags
= injected_flags
;
257 hook
.dwExtraInfo
= extra_info
;
259 last_time_modified
= GetTickCount();
260 if (HOOK_CallHooks( WH_MOUSE_LL
, HC_ACTION
, message
, (LPARAM
)&hook
, TRUE
)) return;
262 SERVER_START_REQ( send_hardware_message
)
264 req
->id
= (injected_flags
& LLMHF_INJECTED
) ? 0 : GetCurrentThreadId();
267 req
->wparam
= MAKEWPARAM( get_key_state(), data
);
272 req
->info
= extra_info
;
273 wine_server_call( req
);
280 /***********************************************************************
281 * X11DRV_send_mouse_input
283 void X11DRV_send_mouse_input( HWND hwnd
, DWORD flags
, DWORD x
, DWORD y
,
284 DWORD data
, DWORD time
, DWORD extra_info
, UINT injected_flags
)
288 if (flags
& MOUSEEVENTF_MOVE
&& flags
& MOUSEEVENTF_ABSOLUTE
)
290 if (injected_flags
& LLMHF_INJECTED
)
292 pt
.x
= (x
* screen_width
) >> 16;
293 pt
.y
= (y
* screen_height
) >> 16;
300 if (cursor_pos
.x
== x
&& cursor_pos
.y
== y
) flags
&= ~MOUSEEVENTF_MOVE
;
304 else if (flags
& MOUSEEVENTF_MOVE
)
306 int accel
[3], xMult
= 1, yMult
= 1;
308 /* dx and dy can be negative numbers for relative movements */
309 SystemParametersInfoW(SPI_GETMOUSE
, 0, accel
, 0);
311 if (abs(x
) > accel
[0] && accel
[2] != 0)
314 if ((abs(x
) > accel
[1]) && (accel
[2] == 2)) xMult
= 4;
316 if (abs(y
) > accel
[0] && accel
[2] != 0)
319 if ((abs(y
) > accel
[1]) && (accel
[2] == 2)) yMult
= 4;
323 pt
.x
= cursor_pos
.x
+ (long)x
* xMult
;
324 pt
.y
= cursor_pos
.y
+ (long)y
* yMult
;
334 if (flags
& MOUSEEVENTF_MOVE
)
336 queue_raw_mouse_message( WM_MOUSEMOVE
, hwnd
, pt
.x
, pt
.y
, data
, time
,
337 extra_info
, injected_flags
);
338 if ((injected_flags
& LLMHF_INJECTED
) &&
339 ((flags
& MOUSEEVENTF_ABSOLUTE
) || x
|| y
)) /* we have to actually move the cursor */
341 X11DRV_SetCursorPos( pt
.x
, pt
.y
);
346 clip_point_to_rect( &cursor_clip
, &pt
);
351 if (flags
& MOUSEEVENTF_LEFTDOWN
)
353 key_state_table
[VK_LBUTTON
] |= 0xc0;
354 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_RBUTTONDOWN
: WM_LBUTTONDOWN
,
355 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
357 if (flags
& MOUSEEVENTF_LEFTUP
)
359 key_state_table
[VK_LBUTTON
] &= ~0x80;
360 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_RBUTTONUP
: WM_LBUTTONUP
,
361 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
363 if (flags
& MOUSEEVENTF_RIGHTDOWN
)
365 key_state_table
[VK_RBUTTON
] |= 0xc0;
366 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_LBUTTONDOWN
: WM_RBUTTONDOWN
,
367 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
369 if (flags
& MOUSEEVENTF_RIGHTUP
)
371 key_state_table
[VK_RBUTTON
] &= ~0x80;
372 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON
) ? WM_LBUTTONUP
: WM_RBUTTONUP
,
373 hwnd
, pt
.x
, pt
.y
, data
, time
, extra_info
, injected_flags
);
375 if (flags
& MOUSEEVENTF_MIDDLEDOWN
)
377 key_state_table
[VK_MBUTTON
] |= 0xc0;
378 queue_raw_mouse_message( WM_MBUTTONDOWN
, hwnd
, pt
.x
, pt
.y
, data
, time
,
379 extra_info
, injected_flags
);
381 if (flags
& MOUSEEVENTF_MIDDLEUP
)
383 key_state_table
[VK_MBUTTON
] &= ~0x80;
384 queue_raw_mouse_message( WM_MBUTTONUP
, hwnd
, pt
.x
, pt
.y
, data
, time
,
385 extra_info
, injected_flags
);
387 if (flags
& MOUSEEVENTF_WHEEL
)
389 queue_raw_mouse_message( WM_MOUSEWHEEL
, hwnd
, pt
.x
, pt
.y
, data
, time
,
390 extra_info
, injected_flags
);
392 if (flags
& MOUSEEVENTF_XDOWN
)
394 key_state_table
[VK_XBUTTON1
+ data
- 1] |= 0xc0;
395 queue_raw_mouse_message( WM_XBUTTONDOWN
, hwnd
, pt
.x
, pt
.y
, data
, time
,
396 extra_info
, injected_flags
);
398 if (flags
& MOUSEEVENTF_XUP
)
400 key_state_table
[VK_XBUTTON1
+ data
- 1] &= ~0x80;
401 queue_raw_mouse_message( WM_XBUTTONUP
, hwnd
, pt
.x
, pt
.y
, data
, time
,
402 extra_info
, injected_flags
);
407 #ifdef HAVE_X11_XCURSOR_XCURSOR_H
409 /***********************************************************************
410 * create_cursor_image
412 * Create an XcursorImage from a CURSORICONINFO
414 static XcursorImage
*create_cursor_image( CURSORICONINFO
*ptr
)
418 int and_size
, xor_size
;
419 unsigned char *and_bits
, *and_ptr
, *xor_bits
, *xor_ptr
;
420 int and_width_bytes
, xor_width_bytes
;
421 XcursorPixel
*pixel_ptr
;
424 ymax
= (ptr
->nHeight
> 32) ? 32 : ptr
->nHeight
;
425 xmax
= (ptr
->nWidth
> 32) ? 32 : ptr
->nWidth
;
427 and_width_bytes
= xmax
/ 8;
428 xor_width_bytes
= and_width_bytes
* ptr
->bBitsPerPixel
;
430 and_size
= ptr
->nWidth
* ptr
->nHeight
/ 8;
431 and_ptr
= and_bits
= (unsigned char *)(ptr
+ 1);
433 xor_size
= xor_width_bytes
* ptr
->nHeight
;
434 xor_ptr
= xor_bits
= and_ptr
+ and_size
;
436 image
= pXcursorImageCreate( xmax
, ymax
);
437 pixel_ptr
= image
->pixels
;
439 /* On windows, to calculate the color for a pixel, first an AND is done
440 * with the background and the "and" bitmap, then an XOR with the "xor"
441 * bitmap. This means that when the data in the "and" bitmap is 0, the
442 * pixel will get the color as specified in the "xor" bitmap.
443 * However, if the data in the "and" bitmap is 1, the result will be the
444 * background XOR'ed with the value in the "xor" bitmap. In case the "xor"
445 * data is completely black (0x000000) the pixel will become transparent,
446 * in case it's white (0xffffff) the pixel will become the inverse of the
449 * Since we can't support inverting colors, we map the grayscale value of
450 * the "xor" data to the alpha channel, and xor the the color with either
453 for (y
= 0; y
< ymax
; ++y
)
455 and_ptr
= and_bits
+ (y
* and_width_bytes
);
456 xor_ptr
= xor_bits
+ (y
* xor_width_bytes
);
458 for (x
= 0; x
< xmax
; ++x
)
460 /* Xcursor pixel data is in ARGB format, with A in the high byte */
461 switch (ptr
->bBitsPerPixel
)
464 /* BGRA, 8 bits each */
465 *pixel_ptr
= *xor_ptr
++;
466 *pixel_ptr
|= *xor_ptr
++ << 8;
467 *pixel_ptr
|= *xor_ptr
++ << 16;
468 *pixel_ptr
|= *xor_ptr
++ << 24;
472 /* BGR, 8 bits each */
473 *pixel_ptr
= *xor_ptr
++;
474 *pixel_ptr
|= *xor_ptr
++ << 8;
475 *pixel_ptr
|= *xor_ptr
++ << 16;
479 /* BGR, 5 red, 6 green, 5 blue */
480 *pixel_ptr
= *xor_ptr
* 0x1f;
481 *pixel_ptr
|= (*xor_ptr
& 0xe0) << 3;
483 *pixel_ptr
|= (*xor_ptr
& 0x07) << 11;
484 *pixel_ptr
|= (*xor_ptr
& 0xf8) << 13;
488 if (*xor_ptr
& (1 << (7 - (x
& 7)))) *pixel_ptr
= 0xffffff;
490 if ((x
& 7) == 7) ++xor_ptr
;
494 FIXME("Currently no support for cursors with %d bits per pixel\n", ptr
->bBitsPerPixel
);
498 if (ptr
->bBitsPerPixel
!= 32)
501 if (~*and_ptr
& (1 << (7 - (x
& 7)))) *pixel_ptr
|= 0xff << 24;
504 int alpha
= (*pixel_ptr
& 0xff) * 0.30f
505 + ((*pixel_ptr
& 0xff00) >> 8) * 0.55f
506 + ((*pixel_ptr
& 0xff0000) >> 16) * 0.15f
;
507 *pixel_ptr
^= ((x
+ y
) % 2) ? 0xffffff : 0x000000;
508 *pixel_ptr
|= alpha
<< 24;
510 if ((x
& 7) == 7) ++and_ptr
;
520 /***********************************************************************
521 * create_xcursor_cursor
523 * Use Xcursor to create an X cursor from a Windows one.
525 static Cursor
create_xcursor_cursor( Display
*display
, CURSORICONINFO
*ptr
)
530 if (!ptr
) /* Create an empty cursor */
532 image
= pXcursorImageCreate( 1, 1 );
535 *(image
->pixels
) = 0;
536 cursor
= pXcursorImageLoadCursor( display
, image
);
537 pXcursorImageDestroy( image
);
542 image
= create_cursor_image( ptr
);
543 if (!image
) return 0;
545 /* Make sure hotspot is valid */
546 image
->xhot
= ptr
->ptHotSpot
.x
;
547 image
->yhot
= ptr
->ptHotSpot
.y
;
548 if (image
->xhot
< 0 || image
->xhot
>= image
->width
||
549 image
->yhot
< 0 || image
->yhot
>= image
->height
)
551 image
->xhot
= image
->width
/ 2;
552 image
->yhot
= image
->height
/ 2;
557 cursor
= pXcursorImageLoadCursor( display
, image
);
558 pXcursorImageDestroy( image
);
563 #endif /* HAVE_X11_XCURSOR_XCURSOR_H */
566 /***********************************************************************
569 * Create an X cursor from a Windows one.
571 static Cursor
create_cursor( Display
*display
, CURSORICONINFO
*ptr
)
573 Pixmap pixmapBits
, pixmapMask
, pixmapMaskInv
, pixmapAll
;
575 Cursor cursor
= None
;
577 #ifdef HAVE_X11_XCURSOR_XCURSOR_H
578 if (pXcursorImageLoadCursor
) return create_xcursor_cursor( display
, ptr
);
581 if (!ptr
) /* Create an empty cursor */
583 static const char data
[] = { 0 };
585 bg
.red
= bg
.green
= bg
.blue
= 0x0000;
586 pixmapBits
= XCreateBitmapFromData( display
, root_window
, data
, 1, 1 );
589 cursor
= XCreatePixmapCursor( display
, pixmapBits
, pixmapBits
,
591 XFreePixmap( display
, pixmapBits
);
594 else /* Create the X cursor from the bits */
599 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
600 ptr
->nWidth
, ptr
->nHeight
, ptr
->bPlanes
, ptr
->bBitsPerPixel
,
602 /* Create a pixmap and transfer all the bits to it */
604 /* NOTE: Following hack works, but only because XFree depth
605 * 1 images really use 1 bit/pixel (and so the same layout
606 * as the Windows cursor data). Perhaps use a more generic
609 /* This pixmap will be written with two bitmaps. The first is
610 * the mask and the second is the image.
612 if (!(pixmapAll
= XCreatePixmap( display
, root_window
,
613 ptr
->nWidth
, ptr
->nHeight
* 2, 1 )))
615 if (!(image
= XCreateImage( display
, visual
,
616 1, ZPixmap
, 0, (char *)(ptr
+ 1), ptr
->nWidth
,
617 ptr
->nHeight
* 2, 16, ptr
->nWidthBytes
/ptr
->bBitsPerPixel
)))
619 XFreePixmap( display
, pixmapAll
);
622 gc
= XCreateGC( display
, pixmapAll
, 0, NULL
);
623 XSetGraphicsExposures( display
, gc
, False
);
624 image
->byte_order
= MSBFirst
;
625 image
->bitmap_bit_order
= MSBFirst
;
626 image
->bitmap_unit
= 16;
627 _XInitImageFuncPtrs(image
);
628 if (ptr
->bPlanes
* ptr
->bBitsPerPixel
== 1)
630 /* A plain old white on black cursor. */
631 fg
.red
= fg
.green
= fg
.blue
= 0xffff;
632 bg
.red
= bg
.green
= bg
.blue
= 0x0000;
633 XPutImage( display
, pixmapAll
, gc
, image
,
634 0, 0, 0, 0, ptr
->nWidth
, ptr
->nHeight
* 2 );
638 int rbits
, gbits
, bbits
, red
, green
, blue
;
639 int rfg
, gfg
, bfg
, rbg
, gbg
, bbg
;
640 int rscale
, gscale
, bscale
;
641 int x
, y
, xmax
, ymax
, bitIndex
, byteIndex
, xorIndex
;
642 unsigned char *theMask
, *theImage
, theChar
;
643 int threshold
, fgBits
, bgBits
, bitShifted
;
644 BYTE pXorBits
[128]; /* Up to 32x32 icons */
646 switch (ptr
->bBitsPerPixel
)
661 FIXME("Currently no support for cursors with %d bits per pixel\n",
663 XFreePixmap( display
, pixmapAll
);
664 XFreeGC( display
, gc
);
666 XDestroyImage( image
);
669 /* The location of the mask. */
670 theMask
= (unsigned char *)(ptr
+ 1);
671 /* The mask should still be 1 bit per pixel. The color image
672 * should immediately follow the mask.
674 theImage
= &theMask
[ptr
->nWidth
/8 * ptr
->nHeight
];
675 rfg
= gfg
= bfg
= rbg
= gbg
= bbg
= 0;
681 xmax
= (ptr
->nWidth
> 32) ? 32 : ptr
->nWidth
;
682 if (ptr
->nWidth
> 32) {
683 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
684 ptr
->nWidth
, ptr
->nHeight
);
686 ymax
= (ptr
->nHeight
> 32) ? 32 : ptr
->nHeight
;
688 memset(pXorBits
, 0, 128);
689 for (y
=0; y
<ymax
; y
++)
691 for (x
=0; x
<xmax
; x
++)
693 red
= green
= blue
= 0;
694 switch (ptr
->bBitsPerPixel
)
697 theChar
= theImage
[byteIndex
++];
699 theChar
= theImage
[byteIndex
++];
701 theChar
= theImage
[byteIndex
++];
705 theChar
= theImage
[byteIndex
++];
706 blue
= theChar
& 0x1F;
707 green
= (theChar
& 0xE0) >> 5;
708 theChar
= theImage
[byteIndex
++];
709 green
|= (theChar
& 0x07) << 3;
710 red
= (theChar
& 0xF8) >> 3;
714 if (red
+green
+blue
> threshold
)
720 pXorBits
[xorIndex
] |= bitShifted
;
734 bitShifted
= bitShifted
<< 1;
737 rscale
= 1 << (16 - rbits
);
738 gscale
= 1 << (16 - gbits
);
739 bscale
= 1 << (16 - bbits
);
742 fg
.red
= rfg
* rscale
/ fgBits
;
743 fg
.green
= gfg
* gscale
/ fgBits
;
744 fg
.blue
= bfg
* bscale
/ fgBits
;
746 else fg
.red
= fg
.green
= fg
.blue
= 0;
747 bgBits
= xmax
* ymax
- fgBits
;
750 bg
.red
= rbg
* rscale
/ bgBits
;
751 bg
.green
= gbg
* gscale
/ bgBits
;
752 bg
.blue
= bbg
* bscale
/ bgBits
;
754 else bg
.red
= bg
.green
= bg
.blue
= 0;
755 pixmapBits
= XCreateBitmapFromData( display
, root_window
, (char *)pXorBits
, xmax
, ymax
);
758 XFreePixmap( display
, pixmapAll
);
759 XFreeGC( display
, gc
);
761 XDestroyImage( image
);
766 XPutImage( display
, pixmapAll
, gc
, image
,
767 0, 0, 0, 0, ptr
->nWidth
, ptr
->nHeight
);
768 XSetFunction( display
, gc
, GXcopy
);
770 XCopyArea( display
, pixmapBits
, pixmapAll
, gc
,
771 0, 0, xmax
, ymax
, 0, ptr
->nHeight
);
772 XFreePixmap( display
, pixmapBits
);
775 XDestroyImage( image
);
777 /* Now create the 2 pixmaps for bits and mask */
779 pixmapBits
= XCreatePixmap( display
, root_window
, ptr
->nWidth
, ptr
->nHeight
, 1 );
780 pixmapMask
= XCreatePixmap( display
, root_window
, ptr
->nWidth
, ptr
->nHeight
, 1 );
781 pixmapMaskInv
= XCreatePixmap( display
, root_window
, ptr
->nWidth
, ptr
->nHeight
, 1 );
783 /* Make sure everything went OK so far */
785 if (pixmapBits
&& pixmapMask
&& pixmapMaskInv
)
789 /* We have to do some magic here, as cursors are not fully
790 * compatible between Windows and X11. Under X11, there
791 * are only 3 possible color cursor: black, white and
792 * masked. So we map the 4th Windows color (invert the
793 * bits on the screen) to black and an additional white bit on
794 * an other place (+1,+1). This require some boolean arithmetic:
797 * And Xor Result | Bits Mask Result
798 * 0 0 black | 0 1 background
799 * 0 1 white | 1 1 foreground
800 * 1 0 no change | X 0 no change
801 * 1 1 inverted | 0 1 background
804 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
805 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
807 * FIXME: apparently some servers do support 'inverted' color.
808 * I don't know if it's correct per the X spec, but maybe
809 * we ought to take advantage of it. -- AJ
811 XSetFunction( display
, gc
, GXcopy
);
812 XCopyArea( display
, pixmapAll
, pixmapBits
, gc
,
813 0, 0, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
814 XCopyArea( display
, pixmapAll
, pixmapMask
, gc
,
815 0, 0, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
816 XCopyArea( display
, pixmapAll
, pixmapMaskInv
, gc
,
817 0, 0, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
818 XSetFunction( display
, gc
, GXand
);
819 XCopyArea( display
, pixmapAll
, pixmapMaskInv
, gc
,
820 0, ptr
->nHeight
, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
821 XSetFunction( display
, gc
, GXandReverse
);
822 XCopyArea( display
, pixmapAll
, pixmapBits
, gc
,
823 0, ptr
->nHeight
, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
824 XSetFunction( display
, gc
, GXorReverse
);
825 XCopyArea( display
, pixmapAll
, pixmapMask
, gc
,
826 0, ptr
->nHeight
, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
827 /* Additional white */
828 XSetFunction( display
, gc
, GXor
);
829 XCopyArea( display
, pixmapMaskInv
, pixmapMask
, gc
,
830 0, 0, ptr
->nWidth
, ptr
->nHeight
, 1, 1 );
831 XCopyArea( display
, pixmapMaskInv
, pixmapBits
, gc
,
832 0, 0, ptr
->nWidth
, ptr
->nHeight
, 1, 1 );
833 XSetFunction( display
, gc
, GXcopy
);
835 /* Make sure hotspot is valid */
836 hotspot
.x
= ptr
->ptHotSpot
.x
;
837 hotspot
.y
= ptr
->ptHotSpot
.y
;
838 if (hotspot
.x
< 0 || hotspot
.x
>= ptr
->nWidth
||
839 hotspot
.y
< 0 || hotspot
.y
>= ptr
->nHeight
)
841 hotspot
.x
= ptr
->nWidth
/ 2;
842 hotspot
.y
= ptr
->nHeight
/ 2;
844 cursor
= XCreatePixmapCursor( display
, pixmapBits
, pixmapMask
,
845 &fg
, &bg
, hotspot
.x
, hotspot
.y
);
848 /* Now free everything */
850 if (pixmapAll
) XFreePixmap( display
, pixmapAll
);
851 if (pixmapBits
) XFreePixmap( display
, pixmapBits
);
852 if (pixmapMask
) XFreePixmap( display
, pixmapMask
);
853 if (pixmapMaskInv
) XFreePixmap( display
, pixmapMaskInv
);
854 XFreeGC( display
, gc
);
860 /***********************************************************************
861 * SetCursor (X11DRV.@)
863 void X11DRV_SetCursor( CURSORICONINFO
*lpCursor
)
868 TRACE("%ux%u, planes %u, bpp %u\n",
869 lpCursor
->nWidth
, lpCursor
->nHeight
, lpCursor
->bPlanes
, lpCursor
->bBitsPerPixel
);
873 if (root_window
!= DefaultRootWindow(gdi_display
))
875 /* If in desktop mode, set the cursor on the desktop window */
878 cursor
= create_cursor( gdi_display
, lpCursor
);
881 XDefineCursor( gdi_display
, root_window
, cursor
);
882 /* Make the change take effect immediately */
884 XFreeCursor( gdi_display
, cursor
);
888 else /* set the same cursor for all top-level windows of the current thread */
890 struct x11drv_thread_data
*data
= x11drv_thread_data();
893 cursor
= create_cursor( data
->display
, lpCursor
);
896 if (data
->cursor
) XFreeCursor( data
->display
, data
->cursor
);
897 data
->cursor
= cursor
;
898 if (data
->cursor_window
)
900 XDefineCursor( data
->display
, data
->cursor_window
, cursor
);
901 /* Make the change take effect immediately */
902 XFlush( data
->display
);
909 /***********************************************************************
910 * SetCursorPos (X11DRV.@)
912 BOOL
X11DRV_SetCursorPos( INT x
, INT y
)
914 Display
*display
= thread_display();
917 TRACE( "warping to (%d,%d)\n", x
, y
);
920 if (cursor_pos
.x
== x
&& cursor_pos
.y
== y
)
923 /* We still need to generate WM_MOUSEMOVE */
924 queue_raw_mouse_message( WM_MOUSEMOVE
, NULL
, x
, y
, 0, GetCurrentTime(), 0, 0 );
929 clip_point_to_rect( &cursor_clip
, &pt
);
930 XWarpPointer( display
, root_window
, root_window
, 0, 0, 0, 0,
931 pt
.x
- virtual_screen_rect
.left
, pt
.y
- virtual_screen_rect
.top
);
932 XFlush( display
); /* avoids bad mouse lag in games that do their own mouse warping */
938 /***********************************************************************
939 * GetAsyncKeyState (X11DRV.@)
941 SHORT
X11DRV_GetAsyncKeyState( INT key
)
943 Display
*display
= thread_display();
945 int rootX
, rootY
, winX
, winY
;
949 /* Make sure the keyboard state is up to date */
950 X11DRV_MsgWaitForMultipleObjectsEx(0,0,0,QS_KEY
,0);
952 if (key
== VK_LBUTTON
|| key
== VK_MBUTTON
|| key
== VK_RBUTTON
)
955 if (XQueryPointer( display
, root_window
, &root
, &child
,
956 &rootX
, &rootY
, &winX
, &winY
, &xstate
))
957 update_button_state( xstate
);
960 retval
= ((key_state_table
[key
] & 0x40) ? 0x0001 : 0) |
961 ((key_state_table
[key
] & 0x80) ? 0x8000 : 0);
962 key_state_table
[key
] &= ~0x40;
963 TRACE("(%x) -> %x\n", key
, retval
);
967 /***********************************************************************
968 * GetCursorPos (X11DRV.@)
970 BOOL
X11DRV_GetCursorPos(LPPOINT pos
)
972 Display
*display
= thread_display();
974 int rootX
, rootY
, winX
, winY
;
978 if ((GetTickCount() - last_time_modified
> 100) &&
979 XQueryPointer( display
, root_window
, &root
, &child
,
980 &rootX
, &rootY
, &winX
, &winY
, &xstate
))
982 winX
+= virtual_screen_rect
.left
;
983 winY
+= virtual_screen_rect
.top
;
984 TRACE("pointer at (%d,%d)\n", winX
, winY
);
994 /***********************************************************************
995 * ClipCursor (X11DRV.@)
997 * Set the cursor clipping rectangle.
999 BOOL
X11DRV_ClipCursor( LPCRECT clip
)
1001 if (!IntersectRect( &cursor_clip
, &virtual_screen_rect
, clip
))
1002 cursor_clip
= virtual_screen_rect
;
1007 /***********************************************************************
1008 * X11DRV_ButtonPress
1010 void X11DRV_ButtonPress( HWND hwnd
, XEvent
*xev
)
1012 XButtonEvent
*event
= &xev
->xbutton
;
1013 int buttonNum
= event
->button
- 1;
1017 if (buttonNum
>= NB_BUTTONS
) return;
1023 wData
= WHEEL_DELTA
;
1026 wData
= -WHEEL_DELTA
;
1036 hwnd
= update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
1038 X11DRV_send_mouse_input( hwnd
, button_down_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
,
1039 pt
.x
, pt
.y
, wData
, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );
1043 /***********************************************************************
1044 * X11DRV_ButtonRelease
1046 void X11DRV_ButtonRelease( HWND hwnd
, XEvent
*xev
)
1048 XButtonEvent
*event
= &xev
->xbutton
;
1049 int buttonNum
= event
->button
- 1;
1053 if (buttonNum
>= NB_BUTTONS
|| !button_up_flags
[buttonNum
]) return;
1066 hwnd
= update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
1068 X11DRV_send_mouse_input( hwnd
, button_up_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_MOVE
,
1069 pt
.x
, pt
.y
, wData
, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );
1073 /***********************************************************************
1074 * X11DRV_MotionNotify
1076 void X11DRV_MotionNotify( HWND hwnd
, XEvent
*xev
)
1078 XMotionEvent
*event
= &xev
->xmotion
;
1081 TRACE("hwnd %p, event->is_hint %d\n", hwnd
, event
->is_hint
);
1085 hwnd
= update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
1087 X11DRV_send_mouse_input( hwnd
, MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
,
1088 pt
.x
, pt
.y
, 0, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );
1092 /***********************************************************************
1093 * X11DRV_EnterNotify
1095 void X11DRV_EnterNotify( HWND hwnd
, XEvent
*xev
)
1097 XCrossingEvent
*event
= &xev
->xcrossing
;
1100 TRACE("hwnd %p, event->detail %d\n", hwnd
, event
->detail
);
1103 if (event
->detail
== NotifyVirtual
|| event
->detail
== NotifyNonlinearVirtual
) return;
1105 /* simulate a mouse motion event */
1106 hwnd
= update_mouse_state( hwnd
, event
->window
, event
->x
, event
->y
, event
->state
, &pt
);
1108 X11DRV_send_mouse_input( hwnd
, MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
,
1109 pt
.x
, pt
.y
, 0, EVENT_x11_time_to_win32_time(event
->time
), 0, 0 );