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
42 #include "wine/winuser16.h"
45 #include "wine/server.h"
46 #include "wine/library.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
51 /**********************************************************************/
54 #define Button6Mask (1<<13)
57 #define Button7Mask (1<<14)
60 #define NB_BUTTONS 7 /* Windows can handle 5 buttons and the wheel too */
62 static const UINT button_down_flags[NB_BUTTONS] =
65 MOUSEEVENTF_MIDDLEDOWN,
66 MOUSEEVENTF_RIGHTDOWN,
73 static const UINT button_up_flags[NB_BUTTONS] =
85 static DWORD last_time_modified;
86 static RECT cursor_clip; /* Cursor clipping rect */
88 BOOL X11DRV_SetCursorPos( INT x, INT y );
91 /***********************************************************************
94 * Load the Xcursor library for use.
96 void X11DRV_Xcursor_Init(void)
98 #ifdef SONAME_LIBXCURSOR
99 xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
100 if (!xcursor_handle) /* wine_dlopen failed. */
102 WARN("Xcursor failed to load. Using fallback code.\n");
105 #define LOAD_FUNCPTR(f) \
106 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
108 LOAD_FUNCPTR(XcursorImageCreate);
109 LOAD_FUNCPTR(XcursorImageDestroy);
110 LOAD_FUNCPTR(XcursorImageLoadCursor);
112 #endif /* SONAME_LIBXCURSOR */
116 /***********************************************************************
119 * get the coordinates of a mouse event
121 static inline void get_coords( HWND hwnd, Window window, int x, int y, POINT *pt )
123 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
127 if (window == data->client_window)
129 pt->x = x + data->client_rect.left;
130 pt->y = y + data->client_rect.top;
134 pt->x = x + data->whole_rect.left;
135 pt->y = y + data->whole_rect.top;
139 /***********************************************************************
142 * Clip point to the provided rectangle
144 static inline void clip_point_to_rect( LPCRECT rect, LPPOINT pt )
146 if (pt->x < rect->left) pt->x = rect->left;
147 else if (pt->x >= rect->right) pt->x = rect->right - 1;
148 if (pt->y < rect->top) pt->y = rect->top;
149 else if (pt->y >= rect->bottom) pt->y = rect->bottom - 1;
152 /***********************************************************************
153 * update_button_state
155 * Update the button state with what X provides us
157 static inline void update_button_state( unsigned int state )
159 key_state_table[VK_LBUTTON] = (state & Button1Mask ? 0x80 : 0);
160 key_state_table[VK_MBUTTON] = (state & Button2Mask ? 0x80 : 0);
161 key_state_table[VK_RBUTTON] = (state & Button3Mask ? 0x80 : 0);
162 /* X-buttons are not reported from XQueryPointer */
166 /***********************************************************************
169 * Update the various window states on a mouse event.
171 static void update_mouse_state( HWND hwnd, Window window, int x, int y, unsigned int state, POINT *pt )
173 struct x11drv_thread_data *data = x11drv_thread_data();
175 get_coords( hwnd, window, x, y, pt );
177 /* update the cursor */
179 if (data->cursor_window != window)
181 data->cursor_window = window;
183 if (data->cursor) XDefineCursor( data->display, window, data->cursor );
187 /* update the wine server Z-order */
189 if (window != data->grab_window &&
190 /* ignore event if a button is pressed, since the mouse is then grabbed too */
191 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
193 SERVER_START_REQ( update_window_zorder )
196 req->rect.left = pt->x;
197 req->rect.top = pt->y;
198 req->rect.right = pt->x + 1;
199 req->rect.bottom = pt->y + 1;
200 wine_server_call( req );
207 /***********************************************************************
210 static WORD get_key_state(void)
214 if (GetSystemMetrics( SM_SWAPBUTTON ))
216 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
217 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
221 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
222 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
224 if (key_state_table[VK_MBUTTON] & 0x80) ret |= MK_MBUTTON;
225 if (key_state_table[VK_SHIFT] & 0x80) ret |= MK_SHIFT;
226 if (key_state_table[VK_CONTROL] & 0x80) ret |= MK_CONTROL;
227 if (key_state_table[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
228 if (key_state_table[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
233 /***********************************************************************
234 * queue_raw_mouse_message
236 static void queue_raw_mouse_message( UINT message, HWND hwnd, DWORD x, DWORD y,
237 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
243 hook.mouseData = MAKELONG( 0, data );
244 hook.flags = injected_flags;
246 hook.dwExtraInfo = extra_info;
248 last_time_modified = GetTickCount();
249 if (HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE )) return;
251 SERVER_START_REQ( send_hardware_message )
253 req->id = (injected_flags & LLMHF_INJECTED) ? 0 : GetCurrentThreadId();
256 req->wparam = MAKEWPARAM( get_key_state(), data );
261 req->info = extra_info;
262 wine_server_call( req );
269 /***********************************************************************
270 * X11DRV_send_mouse_input
272 void X11DRV_send_mouse_input( HWND hwnd, DWORD flags, DWORD x, DWORD y,
273 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
277 if (flags & MOUSEEVENTF_MOVE && flags & MOUSEEVENTF_ABSOLUTE)
279 if (injected_flags & LLMHF_INJECTED)
281 pt.x = (x * screen_width) >> 16;
282 pt.y = (y * screen_height) >> 16;
289 if (cursor_pos.x == x && cursor_pos.y == y &&
290 (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE)))
291 flags &= ~MOUSEEVENTF_MOVE;
295 else if (flags & MOUSEEVENTF_MOVE)
297 int accel[3], xMult = 1, yMult = 1;
299 /* dx and dy can be negative numbers for relative movements */
300 SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
302 if (abs(x) > accel[0] && accel[2] != 0)
305 if ((abs(x) > accel[1]) && (accel[2] == 2)) xMult = 4;
307 if (abs(y) > accel[0] && accel[2] != 0)
310 if ((abs(y) > accel[1]) && (accel[2] == 2)) yMult = 4;
314 pt.x = cursor_pos.x + (long)x * xMult;
315 pt.y = cursor_pos.y + (long)y * yMult;
325 if (flags & MOUSEEVENTF_MOVE)
327 queue_raw_mouse_message( WM_MOUSEMOVE, hwnd, pt.x, pt.y, data, time,
328 extra_info, injected_flags );
329 if ((injected_flags & LLMHF_INJECTED) &&
330 ((flags & MOUSEEVENTF_ABSOLUTE) || x || y)) /* we have to actually move the cursor */
332 X11DRV_SetCursorPos( pt.x, pt.y );
337 clip_point_to_rect( &cursor_clip, &pt);
342 if (flags & MOUSEEVENTF_LEFTDOWN)
344 key_state_table[VK_LBUTTON] |= 0xc0;
345 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
346 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
348 if (flags & MOUSEEVENTF_LEFTUP)
350 key_state_table[VK_LBUTTON] &= ~0x80;
351 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONUP : WM_LBUTTONUP,
352 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
354 if (flags & MOUSEEVENTF_RIGHTDOWN)
356 key_state_table[VK_RBUTTON] |= 0xc0;
357 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
358 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
360 if (flags & MOUSEEVENTF_RIGHTUP)
362 key_state_table[VK_RBUTTON] &= ~0x80;
363 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONUP : WM_RBUTTONUP,
364 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
366 if (flags & MOUSEEVENTF_MIDDLEDOWN)
368 key_state_table[VK_MBUTTON] |= 0xc0;
369 queue_raw_mouse_message( WM_MBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
370 extra_info, injected_flags );
372 if (flags & MOUSEEVENTF_MIDDLEUP)
374 key_state_table[VK_MBUTTON] &= ~0x80;
375 queue_raw_mouse_message( WM_MBUTTONUP, hwnd, pt.x, pt.y, data, time,
376 extra_info, injected_flags );
378 if (flags & MOUSEEVENTF_WHEEL)
380 queue_raw_mouse_message( WM_MOUSEWHEEL, hwnd, pt.x, pt.y, data, time,
381 extra_info, injected_flags );
383 if (flags & MOUSEEVENTF_XDOWN)
385 key_state_table[VK_XBUTTON1 + data - 1] |= 0xc0;
386 queue_raw_mouse_message( WM_XBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
387 extra_info, injected_flags );
389 if (flags & MOUSEEVENTF_XUP)
391 key_state_table[VK_XBUTTON1 + data - 1] &= ~0x80;
392 queue_raw_mouse_message( WM_XBUTTONUP, hwnd, pt.x, pt.y, data, time,
393 extra_info, injected_flags );
398 #ifdef SONAME_LIBXCURSOR
400 /***********************************************************************
401 * create_cursor_image
403 * Create an XcursorImage from a CURSORICONINFO
405 static XcursorImage *create_cursor_image( CURSORICONINFO *ptr )
410 unsigned char *and_bits, *and_ptr, *xor_bits, *xor_ptr;
411 int and_width_bytes, xor_width_bytes;
412 XcursorPixel *pixel_ptr;
414 BOOL alpha_zero = TRUE;
416 ymax = (ptr->nHeight > 32) ? 32 : ptr->nHeight;
417 xmax = (ptr->nWidth > 32) ? 32 : ptr->nWidth;
419 and_width_bytes = xmax / 8;
420 xor_width_bytes = and_width_bytes * ptr->bBitsPerPixel;
422 and_size = ptr->nWidth * ptr->nHeight / 8;
423 and_ptr = and_bits = (unsigned char *)(ptr + 1);
425 xor_ptr = xor_bits = and_ptr + and_size;
427 image = pXcursorImageCreate( xmax, ymax );
428 pixel_ptr = image->pixels;
430 /* Generally 32 bit bitmaps have an alpha channel which is used in favor
431 * of the AND mask. However, if all pixels have alpha = 0x00, the bitmap
432 * is treated like one without alpha and the masks are used. As soon as
433 * one pixel has alpha != 0x00, and the mask ignored as described in the
436 * This is most likely for applications which create the bitmaps with
437 * CreateDIBitmap, which creates a device dependent bitmap, so the format
438 * that arrives when loading depends on the screen's bpp. Apps that were
439 * written at 8 / 16 bpp times do not know about the 32 bit alpha, so
440 * they would get a completely transparent cursor on 32 bit displays.
442 * Non-32 bit bitmaps always use the AND mask
444 if(ptr->bBitsPerPixel == 32)
446 for (y = 0; alpha_zero && y < ymax; ++y)
448 xor_ptr = xor_bits + (y * xor_width_bytes);
449 for (x = 0; x < xmax; ++x)
451 if (xor_ptr[3] != 0x00)
461 /* On windows, to calculate the color for a pixel, first an AND is done
462 * with the background and the "and" bitmap, then an XOR with the "xor"
463 * bitmap. This means that when the data in the "and" bitmap is 0, the
464 * pixel will get the color as specified in the "xor" bitmap.
465 * However, if the data in the "and" bitmap is 1, the result will be the
466 * background XOR'ed with the value in the "xor" bitmap. In case the "xor"
467 * data is completely black (0x000000) the pixel will become transparent,
468 * in case it's white (0xffffff) the pixel will become the inverse of the
471 * Since we can't support inverting colors, we map the grayscale value of
472 * the "xor" data to the alpha channel, and xor the color with either
475 for (y = 0; y < ymax; ++y)
477 and_ptr = and_bits + (y * and_width_bytes);
478 xor_ptr = xor_bits + (y * xor_width_bytes);
480 for (x = 0; x < xmax; ++x)
482 /* Xcursor pixel data is in ARGB format, with A in the high byte */
483 switch (ptr->bBitsPerPixel)
486 /* BGRA, 8 bits each */
487 *pixel_ptr = *xor_ptr++;
488 *pixel_ptr |= *xor_ptr++ << 8;
489 *pixel_ptr |= *xor_ptr++ << 16;
490 *pixel_ptr |= *xor_ptr++ << 24;
494 /* BGR, 8 bits each */
495 *pixel_ptr = *xor_ptr++;
496 *pixel_ptr |= *xor_ptr++ << 8;
497 *pixel_ptr |= *xor_ptr++ << 16;
501 /* BGR, 5 red, 6 green, 5 blue */
502 *pixel_ptr = *xor_ptr * 0x1f;
503 *pixel_ptr |= (*xor_ptr & 0xe0) << 3;
505 *pixel_ptr |= (*xor_ptr & 0x07) << 11;
506 *pixel_ptr |= (*xor_ptr & 0xf8) << 13;
510 if (*xor_ptr & (1 << (7 - (x & 7)))) *pixel_ptr = 0xffffff;
512 if ((x & 7) == 7) ++xor_ptr;
516 FIXME("Currently no support for cursors with %d bits per pixel\n", ptr->bBitsPerPixel);
523 if (~*and_ptr & (1 << (7 - (x & 7)))) *pixel_ptr |= 0xff << 24;
526 int alpha = (*pixel_ptr & 0xff) * 0.30f
527 + ((*pixel_ptr & 0xff00) >> 8) * 0.55f
528 + ((*pixel_ptr & 0xff0000) >> 16) * 0.15f;
529 *pixel_ptr ^= ((x + y) % 2) ? 0xffffff : 0x000000;
530 *pixel_ptr |= alpha << 24;
532 if ((x & 7) == 7) ++and_ptr;
542 /***********************************************************************
543 * create_xcursor_cursor
545 * Use Xcursor to create an X cursor from a Windows one.
547 static Cursor create_xcursor_cursor( Display *display, CURSORICONINFO *ptr )
552 if (!ptr) /* Create an empty cursor */
554 image = pXcursorImageCreate( 1, 1 );
557 *(image->pixels) = 0;
558 cursor = pXcursorImageLoadCursor( display, image );
559 pXcursorImageDestroy( image );
564 image = create_cursor_image( ptr );
565 if (!image) return 0;
567 /* Make sure hotspot is valid */
568 image->xhot = ptr->ptHotSpot.x;
569 image->yhot = ptr->ptHotSpot.y;
570 if (image->xhot >= image->width ||
571 image->yhot >= image->height)
573 image->xhot = image->width / 2;
574 image->yhot = image->height / 2;
579 cursor = pXcursorImageLoadCursor( display, image );
580 pXcursorImageDestroy( image );
585 #endif /* SONAME_LIBXCURSOR */
588 /***********************************************************************
591 * Create an X cursor from a Windows one.
593 static Cursor create_cursor( Display *display, CURSORICONINFO *ptr )
595 Pixmap pixmapBits, pixmapMask, pixmapMaskInv = 0, pixmapAll;
597 Cursor cursor = None;
599 char *bitMask32 = NULL;
601 #ifdef SONAME_LIBXCURSOR
602 if (pXcursorImageLoadCursor) return create_xcursor_cursor( display, ptr );
605 if (!ptr) /* Create an empty cursor */
607 static const char data[] = { 0 };
609 bg.red = bg.green = bg.blue = 0x0000;
610 pixmapBits = XCreateBitmapFromData( display, root_window, data, 1, 1 );
613 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
615 XFreePixmap( display, pixmapBits );
618 else /* Create the X cursor from the bits */
623 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
624 ptr->nWidth, ptr->nHeight, ptr->bPlanes, ptr->bBitsPerPixel,
627 /* Create a pixmap and transfer all the bits to it */
629 /* NOTE: Following hack works, but only because XFree depth
630 * 1 images really use 1 bit/pixel (and so the same layout
631 * as the Windows cursor data). Perhaps use a more generic
634 /* This pixmap will be written with two bitmaps. The first is
635 * the mask and the second is the image.
637 if (!(pixmapAll = XCreatePixmap( display, root_window,
638 ptr->nWidth, ptr->nHeight * 2, 1 )))
640 if (!(image = XCreateImage( display, visual,
641 1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
642 ptr->nHeight * 2, 16, ptr->nWidthBytes/ptr->bBitsPerPixel)))
644 XFreePixmap( display, pixmapAll );
647 gc = XCreateGC( display, pixmapAll, 0, NULL );
648 XSetGraphicsExposures( display, gc, False );
649 image->byte_order = MSBFirst;
650 image->bitmap_bit_order = MSBFirst;
651 image->bitmap_unit = 16;
652 _XInitImageFuncPtrs(image);
653 if (ptr->bPlanes * ptr->bBitsPerPixel == 1)
655 /* A plain old white on black cursor. */
656 fg.red = fg.green = fg.blue = 0xffff;
657 bg.red = bg.green = bg.blue = 0x0000;
658 XPutImage( display, pixmapAll, gc, image,
659 0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
663 int rbits, gbits, bbits, red, green, blue;
664 int rfg, gfg, bfg, rbg, gbg, bbg;
665 int rscale, gscale, bscale;
666 int x, y, xmax, ymax, byteIndex, xorIndex;
667 unsigned char *theMask, *theImage, theChar;
668 int threshold, fgBits, bgBits, bitShifted;
669 BYTE pXorBits[128]; /* Up to 32x32 icons */
671 switch (ptr->bBitsPerPixel)
674 bitMask32 = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
675 ptr->nWidth * ptr->nHeight / 8 );
690 FIXME("Currently no support for cursors with %d bits per pixel\n",
692 XFreePixmap( display, pixmapAll );
693 XFreeGC( display, gc );
695 XDestroyImage( image );
698 /* The location of the mask. */
699 theMask = (unsigned char *)(ptr + 1);
700 /* The mask should still be 1 bit per pixel. The color image
701 * should immediately follow the mask.
703 theImage = &theMask[ptr->nWidth/8 * ptr->nHeight];
704 rfg = gfg = bfg = rbg = gbg = bbg = 0;
709 xmax = (ptr->nWidth > 32) ? 32 : ptr->nWidth;
710 if (ptr->nWidth > 32) {
711 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
712 ptr->nWidth, ptr->nHeight);
714 ymax = (ptr->nHeight > 32) ? 32 : ptr->nHeight;
716 memset(pXorBits, 0, 128);
717 for (y=0; y<ymax; y++)
719 for (x=0; x<xmax; x++)
721 red = green = blue = 0;
722 switch (ptr->bBitsPerPixel)
725 theChar = theImage[byteIndex++];
727 theChar = theImage[byteIndex++];
729 theChar = theImage[byteIndex++];
731 theChar = theImage[byteIndex++];
732 /* If the alpha channel is >5% transparent,
733 * assume that we can add it to the bitMask32.
736 *(bitMask32 + (y*xmax+x)/8) |= 1 << (x & 7);
739 theChar = theImage[byteIndex++];
741 theChar = theImage[byteIndex++];
743 theChar = theImage[byteIndex++];
747 theChar = theImage[byteIndex++];
748 blue = theChar & 0x1F;
749 green = (theChar & 0xE0) >> 5;
750 theChar = theImage[byteIndex++];
751 green |= (theChar & 0x07) << 3;
752 red = (theChar & 0xF8) >> 3;
756 if (red+green+blue > threshold)
762 pXorBits[xorIndex] |= bitShifted;
776 bitShifted = bitShifted << 1;
779 rscale = 1 << (16 - rbits);
780 gscale = 1 << (16 - gbits);
781 bscale = 1 << (16 - bbits);
784 fg.red = rfg * rscale / fgBits;
785 fg.green = gfg * gscale / fgBits;
786 fg.blue = bfg * bscale / fgBits;
788 else fg.red = fg.green = fg.blue = 0;
789 bgBits = xmax * ymax - fgBits;
792 bg.red = rbg * rscale / bgBits;
793 bg.green = gbg * gscale / bgBits;
794 bg.blue = bbg * bscale / bgBits;
796 else bg.red = bg.green = bg.blue = 0;
797 pixmapBits = XCreateBitmapFromData( display, root_window, (char *)pXorBits, xmax, ymax );
800 HeapFree( GetProcessHeap(), 0, bitMask32 );
801 XFreePixmap( display, pixmapAll );
802 XFreeGC( display, gc );
804 XDestroyImage( image );
809 XPutImage( display, pixmapAll, gc, image,
810 0, 0, 0, 0, ptr->nWidth, ptr->nHeight );
811 XSetFunction( display, gc, GXcopy );
813 XCopyArea( display, pixmapBits, pixmapAll, gc,
814 0, 0, xmax, ymax, 0, ptr->nHeight );
815 XFreePixmap( display, pixmapBits );
818 XDestroyImage( image );
820 /* Now create the 2 pixmaps for bits and mask */
822 pixmapBits = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
823 if (ptr->bBitsPerPixel != 32)
825 pixmapMaskInv = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
826 pixmapMask = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
828 /* Make sure everything went OK so far */
829 if (pixmapBits && pixmapMask && pixmapMaskInv)
831 /* We have to do some magic here, as cursors are not fully
832 * compatible between Windows and X11. Under X11, there are
833 * only 3 possible color cursor: black, white and masked. So
834 * we map the 4th Windows color (invert the bits on the screen)
835 * to black and an additional white bit on an other place
836 * (+1,+1). This require some boolean arithmetic:
839 * And Xor Result | Bits Mask Result
840 * 0 0 black | 0 1 background
841 * 0 1 white | 1 1 foreground
842 * 1 0 no change | X 0 no change
843 * 1 1 inverted | 0 1 background
846 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
847 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
849 * FIXME: apparently some servers do support 'inverted' color.
850 * I don't know if it's correct per the X spec, but maybe we
851 * ought to take advantage of it. -- AJ
853 XSetFunction( display, gc, GXcopy );
854 XCopyArea( display, pixmapAll, pixmapBits, gc,
855 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
856 XCopyArea( display, pixmapAll, pixmapMask, gc,
857 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
858 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
859 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
860 XSetFunction( display, gc, GXand );
861 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
862 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
863 XSetFunction( display, gc, GXandReverse );
864 XCopyArea( display, pixmapAll, pixmapBits, gc,
865 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
866 XSetFunction( display, gc, GXorReverse );
867 XCopyArea( display, pixmapAll, pixmapMask, gc,
868 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
869 /* Additional white */
870 XSetFunction( display, gc, GXor );
871 XCopyArea( display, pixmapMaskInv, pixmapMask, gc,
872 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
873 XCopyArea( display, pixmapMaskInv, pixmapBits, gc,
874 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
875 XSetFunction( display, gc, GXcopy );
880 pixmapMask = XCreateBitmapFromData( display, root_window,
881 bitMask32, ptr->nWidth,
883 HeapFree( GetProcessHeap(), 0, bitMask32 );
886 /* Make sure hotspot is valid */
887 hotspot.x = ptr->ptHotSpot.x;
888 hotspot.y = ptr->ptHotSpot.y;
889 if (hotspot.x < 0 || hotspot.x >= ptr->nWidth ||
890 hotspot.y < 0 || hotspot.y >= ptr->nHeight)
892 hotspot.x = ptr->nWidth / 2;
893 hotspot.y = ptr->nHeight / 2;
896 if (pixmapBits && pixmapMask)
897 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
898 &fg, &bg, hotspot.x, hotspot.y );
900 /* Now free everything */
902 if (pixmapAll) XFreePixmap( display, pixmapAll );
903 if (pixmapBits) XFreePixmap( display, pixmapBits );
904 if (pixmapMask) XFreePixmap( display, pixmapMask );
905 if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
906 XFreeGC( display, gc );
912 /***********************************************************************
913 * SetCursor (X11DRV.@)
915 void X11DRV_SetCursor( CURSORICONINFO *lpCursor )
917 struct x11drv_thread_data *data = x11drv_thread_data();
921 TRACE("%ux%u, planes %u, bpp %u\n",
922 lpCursor->nWidth, lpCursor->nHeight, lpCursor->bPlanes, lpCursor->bBitsPerPixel);
926 /* set the same cursor for all top-level windows of the current thread */
929 cursor = create_cursor( data->display, lpCursor );
932 if (data->cursor) XFreeCursor( data->display, data->cursor );
933 data->cursor = cursor;
934 if (data->cursor_window)
936 XDefineCursor( data->display, data->cursor_window, cursor );
937 /* Make the change take effect immediately */
938 XFlush( data->display );
944 /***********************************************************************
945 * SetCursorPos (X11DRV.@)
947 BOOL X11DRV_SetCursorPos( INT x, INT y )
949 Display *display = thread_display();
952 TRACE( "warping to (%d,%d)\n", x, y );
955 if (cursor_pos.x == x && cursor_pos.y == y)
958 /* We still need to generate WM_MOUSEMOVE */
959 queue_raw_mouse_message( WM_MOUSEMOVE, NULL, x, y, 0, GetCurrentTime(), 0, 0 );
964 clip_point_to_rect( &cursor_clip, &pt);
965 XWarpPointer( display, root_window, root_window, 0, 0, 0, 0,
966 pt.x - virtual_screen_rect.left, pt.y - virtual_screen_rect.top );
967 XFlush( display ); /* avoids bad mouse lag in games that do their own mouse warping */
973 /***********************************************************************
974 * GetCursorPos (X11DRV.@)
976 BOOL X11DRV_GetCursorPos(LPPOINT pos)
978 Display *display = thread_display();
980 int rootX, rootY, winX, winY;
984 if ((GetTickCount() - last_time_modified > 100) &&
985 XQueryPointer( display, root_window, &root, &child,
986 &rootX, &rootY, &winX, &winY, &xstate ))
988 update_button_state( xstate );
989 winX += virtual_screen_rect.left;
990 winY += virtual_screen_rect.top;
991 TRACE("pointer at (%d,%d)\n", winX, winY );
1001 /***********************************************************************
1002 * ClipCursor (X11DRV.@)
1004 * Set the cursor clipping rectangle.
1006 BOOL X11DRV_ClipCursor( LPCRECT clip )
1008 if (!IntersectRect( &cursor_clip, &virtual_screen_rect, clip ))
1009 cursor_clip = virtual_screen_rect;
1014 /***********************************************************************
1015 * X11DRV_ButtonPress
1017 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1019 XButtonEvent *event = &xev->xbutton;
1020 int buttonNum = event->button - 1;
1024 if (buttonNum >= NB_BUTTONS) return;
1030 wData = WHEEL_DELTA;
1033 wData = -WHEEL_DELTA;
1043 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1045 X11DRV_send_mouse_input( hwnd, button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1046 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1050 /***********************************************************************
1051 * X11DRV_ButtonRelease
1053 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1055 XButtonEvent *event = &xev->xbutton;
1056 int buttonNum = event->button - 1;
1060 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1073 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1075 X11DRV_send_mouse_input( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1076 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1080 /***********************************************************************
1081 * X11DRV_MotionNotify
1083 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1085 XMotionEvent *event = &xev->xmotion;
1088 TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);
1092 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1094 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1095 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1099 /***********************************************************************
1100 * X11DRV_EnterNotify
1102 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1104 XCrossingEvent *event = &xev->xcrossing;
1107 TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);
1110 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1111 if (event->window == x11drv_thread_data()->grab_window) return;
1113 /* simulate a mouse motion event */
1114 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1116 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1117 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );