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"
46 #include "wine/server.h"
47 #include "wine/library.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
52 /**********************************************************************/
55 #define Button6Mask (1<<13)
58 #define Button7Mask (1<<14)
61 #define NB_BUTTONS 7 /* Windows can handle 5 buttons and the wheel too */
63 static const UINT button_down_flags[NB_BUTTONS] =
66 MOUSEEVENTF_MIDDLEDOWN,
67 MOUSEEVENTF_RIGHTDOWN,
74 static const UINT button_up_flags[NB_BUTTONS] =
86 static DWORD last_time_modified;
87 static RECT cursor_clip; /* Cursor clipping rect */
89 BOOL X11DRV_SetCursorPos( INT x, INT y );
92 /***********************************************************************
95 * Load the Xcursor library for use.
97 void X11DRV_Xcursor_Init(void)
99 #ifdef SONAME_LIBXCURSOR
100 xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
101 if (!xcursor_handle) /* wine_dlopen failed. */
103 WARN("Xcursor failed to load. Using fallback code.\n");
106 #define LOAD_FUNCPTR(f) \
107 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
109 LOAD_FUNCPTR(XcursorImageCreate);
110 LOAD_FUNCPTR(XcursorImageDestroy);
111 LOAD_FUNCPTR(XcursorImageLoadCursor);
113 #endif /* SONAME_LIBXCURSOR */
117 /***********************************************************************
120 * get the coordinates of a mouse event
122 static inline void get_coords( HWND hwnd, int x, int y, POINT *pt )
124 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
128 pt->x = x + data->whole_rect.left;
129 pt->y = y + data->whole_rect.top;
132 /***********************************************************************
135 * Clip point to the provided rectangle
137 static inline void clip_point_to_rect( LPCRECT rect, LPPOINT pt )
139 if (pt->x < rect->left) pt->x = rect->left;
140 else if (pt->x >= rect->right) pt->x = rect->right - 1;
141 if (pt->y < rect->top) pt->y = rect->top;
142 else if (pt->y >= rect->bottom) pt->y = rect->bottom - 1;
145 /***********************************************************************
146 * update_button_state
148 * Update the button state with what X provides us
150 static inline void update_button_state( unsigned int state )
152 key_state_table[VK_LBUTTON] = (state & Button1Mask ? 0x80 : 0);
153 key_state_table[VK_MBUTTON] = (state & Button2Mask ? 0x80 : 0);
154 key_state_table[VK_RBUTTON] = (state & Button3Mask ? 0x80 : 0);
155 /* X-buttons are not reported from XQueryPointer */
159 /***********************************************************************
162 * Update the various window states on a mouse event.
164 static void update_mouse_state( HWND hwnd, Window window, int x, int y, unsigned int state, POINT *pt )
166 struct x11drv_thread_data *data = x11drv_thread_data();
168 if (window == root_window)
170 x += virtual_screen_rect.left;
171 y += virtual_screen_rect.top;
173 get_coords( hwnd, x, y, pt );
175 /* update the cursor */
177 if (data->cursor_window != window)
179 data->cursor_window = window;
181 if (data->cursor) XDefineCursor( data->display, window, data->cursor );
185 /* update the wine server Z-order */
187 if (window != data->grab_window &&
188 /* ignore event if a button is pressed, since the mouse is then grabbed too */
189 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
191 SERVER_START_REQ( update_window_zorder )
194 req->rect.left = pt->x;
195 req->rect.top = pt->y;
196 req->rect.right = pt->x + 1;
197 req->rect.bottom = pt->y + 1;
198 wine_server_call( req );
205 /***********************************************************************
208 static WORD get_key_state(void)
212 if (GetSystemMetrics( SM_SWAPBUTTON ))
214 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
215 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
219 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
220 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
222 if (key_state_table[VK_MBUTTON] & 0x80) ret |= MK_MBUTTON;
223 if (key_state_table[VK_SHIFT] & 0x80) ret |= MK_SHIFT;
224 if (key_state_table[VK_CONTROL] & 0x80) ret |= MK_CONTROL;
225 if (key_state_table[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
226 if (key_state_table[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
231 /***********************************************************************
232 * queue_raw_mouse_message
234 static void queue_raw_mouse_message( UINT message, HWND hwnd, DWORD x, DWORD y,
235 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
241 hook.mouseData = MAKELONG( 0, data );
242 hook.flags = injected_flags;
244 hook.dwExtraInfo = extra_info;
246 last_time_modified = GetTickCount();
247 if (HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE )) return;
249 SERVER_START_REQ( send_hardware_message )
251 req->id = (injected_flags & LLMHF_INJECTED) ? 0 : GetCurrentThreadId();
254 req->wparam = MAKEWPARAM( get_key_state(), data );
259 req->info = extra_info;
260 wine_server_call( req );
267 /***********************************************************************
268 * X11DRV_send_mouse_input
270 void X11DRV_send_mouse_input( HWND hwnd, DWORD flags, DWORD x, DWORD y,
271 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
275 if (flags & MOUSEEVENTF_MOVE && flags & MOUSEEVENTF_ABSOLUTE)
277 if (injected_flags & LLMHF_INJECTED)
279 pt.x = (x * screen_width) >> 16;
280 pt.y = (y * screen_height) >> 16;
287 if (cursor_pos.x == x && cursor_pos.y == y) flags &= ~MOUSEEVENTF_MOVE;
291 else if (flags & MOUSEEVENTF_MOVE)
293 int accel[3], xMult = 1, yMult = 1;
295 /* dx and dy can be negative numbers for relative movements */
296 SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
298 if (abs(x) > accel[0] && accel[2] != 0)
301 if ((abs(x) > accel[1]) && (accel[2] == 2)) xMult = 4;
303 if (abs(y) > accel[0] && accel[2] != 0)
306 if ((abs(y) > accel[1]) && (accel[2] == 2)) yMult = 4;
310 pt.x = cursor_pos.x + (long)x * xMult;
311 pt.y = cursor_pos.y + (long)y * yMult;
321 if (flags & MOUSEEVENTF_MOVE)
323 queue_raw_mouse_message( WM_MOUSEMOVE, hwnd, pt.x, pt.y, data, time,
324 extra_info, injected_flags );
325 if ((injected_flags & LLMHF_INJECTED) &&
326 ((flags & MOUSEEVENTF_ABSOLUTE) || x || y)) /* we have to actually move the cursor */
328 X11DRV_SetCursorPos( pt.x, pt.y );
333 clip_point_to_rect( &cursor_clip, &pt);
338 if (flags & MOUSEEVENTF_LEFTDOWN)
340 key_state_table[VK_LBUTTON] |= 0xc0;
341 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
342 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
344 if (flags & MOUSEEVENTF_LEFTUP)
346 key_state_table[VK_LBUTTON] &= ~0x80;
347 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONUP : WM_LBUTTONUP,
348 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
350 if (flags & MOUSEEVENTF_RIGHTDOWN)
352 key_state_table[VK_RBUTTON] |= 0xc0;
353 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
354 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
356 if (flags & MOUSEEVENTF_RIGHTUP)
358 key_state_table[VK_RBUTTON] &= ~0x80;
359 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONUP : WM_RBUTTONUP,
360 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
362 if (flags & MOUSEEVENTF_MIDDLEDOWN)
364 key_state_table[VK_MBUTTON] |= 0xc0;
365 queue_raw_mouse_message( WM_MBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
366 extra_info, injected_flags );
368 if (flags & MOUSEEVENTF_MIDDLEUP)
370 key_state_table[VK_MBUTTON] &= ~0x80;
371 queue_raw_mouse_message( WM_MBUTTONUP, hwnd, pt.x, pt.y, data, time,
372 extra_info, injected_flags );
374 if (flags & MOUSEEVENTF_WHEEL)
376 queue_raw_mouse_message( WM_MOUSEWHEEL, hwnd, pt.x, pt.y, data, time,
377 extra_info, injected_flags );
379 if (flags & MOUSEEVENTF_XDOWN)
381 key_state_table[VK_XBUTTON1 + data - 1] |= 0xc0;
382 queue_raw_mouse_message( WM_XBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
383 extra_info, injected_flags );
385 if (flags & MOUSEEVENTF_XUP)
387 key_state_table[VK_XBUTTON1 + data - 1] &= ~0x80;
388 queue_raw_mouse_message( WM_XBUTTONUP, hwnd, pt.x, pt.y, data, time,
389 extra_info, injected_flags );
394 #ifdef SONAME_LIBXCURSOR
396 /***********************************************************************
397 * create_cursor_image
399 * Create an XcursorImage from a CURSORICONINFO
401 static XcursorImage *create_cursor_image( CURSORICONINFO *ptr )
405 int and_size, xor_size;
406 unsigned char *and_bits, *and_ptr, *xor_bits, *xor_ptr;
407 int and_width_bytes, xor_width_bytes;
408 XcursorPixel *pixel_ptr;
411 ymax = (ptr->nHeight > 32) ? 32 : ptr->nHeight;
412 xmax = (ptr->nWidth > 32) ? 32 : ptr->nWidth;
414 and_width_bytes = xmax / 8;
415 xor_width_bytes = and_width_bytes * ptr->bBitsPerPixel;
417 and_size = ptr->nWidth * ptr->nHeight / 8;
418 and_ptr = and_bits = (unsigned char *)(ptr + 1);
420 xor_size = xor_width_bytes * ptr->nHeight;
421 xor_ptr = xor_bits = and_ptr + and_size;
423 image = pXcursorImageCreate( xmax, ymax );
424 pixel_ptr = image->pixels;
426 /* On windows, to calculate the color for a pixel, first an AND is done
427 * with the background and the "and" bitmap, then an XOR with the "xor"
428 * bitmap. This means that when the data in the "and" bitmap is 0, the
429 * pixel will get the color as specified in the "xor" bitmap.
430 * However, if the data in the "and" bitmap is 1, the result will be the
431 * background XOR'ed with the value in the "xor" bitmap. In case the "xor"
432 * data is completely black (0x000000) the pixel will become transparent,
433 * in case it's white (0xffffff) the pixel will become the inverse of the
436 * Since we can't support inverting colors, we map the grayscale value of
437 * the "xor" data to the alpha channel, and xor the the color with either
440 for (y = 0; y < ymax; ++y)
442 and_ptr = and_bits + (y * and_width_bytes);
443 xor_ptr = xor_bits + (y * xor_width_bytes);
445 for (x = 0; x < xmax; ++x)
447 /* Xcursor pixel data is in ARGB format, with A in the high byte */
448 switch (ptr->bBitsPerPixel)
451 /* BGRA, 8 bits each */
452 *pixel_ptr = *xor_ptr++;
453 *pixel_ptr |= *xor_ptr++ << 8;
454 *pixel_ptr |= *xor_ptr++ << 16;
455 *pixel_ptr |= *xor_ptr++ << 24;
459 /* BGR, 8 bits each */
460 *pixel_ptr = *xor_ptr++;
461 *pixel_ptr |= *xor_ptr++ << 8;
462 *pixel_ptr |= *xor_ptr++ << 16;
466 /* BGR, 5 red, 6 green, 5 blue */
467 *pixel_ptr = *xor_ptr * 0x1f;
468 *pixel_ptr |= (*xor_ptr & 0xe0) << 3;
470 *pixel_ptr |= (*xor_ptr & 0x07) << 11;
471 *pixel_ptr |= (*xor_ptr & 0xf8) << 13;
475 if (*xor_ptr & (1 << (7 - (x & 7)))) *pixel_ptr = 0xffffff;
477 if ((x & 7) == 7) ++xor_ptr;
481 FIXME("Currently no support for cursors with %d bits per pixel\n", ptr->bBitsPerPixel);
485 if (ptr->bBitsPerPixel != 32)
488 if (~*and_ptr & (1 << (7 - (x & 7)))) *pixel_ptr |= 0xff << 24;
491 int alpha = (*pixel_ptr & 0xff) * 0.30f
492 + ((*pixel_ptr & 0xff00) >> 8) * 0.55f
493 + ((*pixel_ptr & 0xff0000) >> 16) * 0.15f;
494 *pixel_ptr ^= ((x + y) % 2) ? 0xffffff : 0x000000;
495 *pixel_ptr |= alpha << 24;
497 if ((x & 7) == 7) ++and_ptr;
507 /***********************************************************************
508 * create_xcursor_cursor
510 * Use Xcursor to create an X cursor from a Windows one.
512 static Cursor create_xcursor_cursor( Display *display, CURSORICONINFO *ptr )
517 if (!ptr) /* Create an empty cursor */
519 image = pXcursorImageCreate( 1, 1 );
522 *(image->pixels) = 0;
523 cursor = pXcursorImageLoadCursor( display, image );
524 pXcursorImageDestroy( image );
529 image = create_cursor_image( ptr );
530 if (!image) return 0;
532 /* Make sure hotspot is valid */
533 image->xhot = ptr->ptHotSpot.x;
534 image->yhot = ptr->ptHotSpot.y;
535 if (image->xhot < 0 || image->xhot >= image->width ||
536 image->yhot < 0 || image->yhot >= image->height)
538 image->xhot = image->width / 2;
539 image->yhot = image->height / 2;
544 cursor = pXcursorImageLoadCursor( display, image );
545 pXcursorImageDestroy( image );
550 #endif /* SONAME_LIBXCURSOR */
553 /***********************************************************************
556 * Create an X cursor from a Windows one.
558 static Cursor create_cursor( Display *display, CURSORICONINFO *ptr )
560 Pixmap pixmapBits, pixmapMask, pixmapMaskInv = 0, pixmapAll;
562 Cursor cursor = None;
564 char *bitMask32 = NULL;
566 #ifdef SONAME_LIBXCURSOR
567 if (pXcursorImageLoadCursor) return create_xcursor_cursor( display, ptr );
570 if (!ptr) /* Create an empty cursor */
572 static const char data[] = { 0 };
574 bg.red = bg.green = bg.blue = 0x0000;
575 pixmapBits = XCreateBitmapFromData( display, root_window, data, 1, 1 );
578 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
580 XFreePixmap( display, pixmapBits );
583 else /* Create the X cursor from the bits */
588 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
589 ptr->nWidth, ptr->nHeight, ptr->bPlanes, ptr->bBitsPerPixel,
592 /* Create a pixmap and transfer all the bits to it */
594 /* NOTE: Following hack works, but only because XFree depth
595 * 1 images really use 1 bit/pixel (and so the same layout
596 * as the Windows cursor data). Perhaps use a more generic
599 /* This pixmap will be written with two bitmaps. The first is
600 * the mask and the second is the image.
602 if (!(pixmapAll = XCreatePixmap( display, root_window,
603 ptr->nWidth, ptr->nHeight * 2, 1 )))
605 if (!(image = XCreateImage( display, visual,
606 1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
607 ptr->nHeight * 2, 16, ptr->nWidthBytes/ptr->bBitsPerPixel)))
609 XFreePixmap( display, pixmapAll );
612 gc = XCreateGC( display, pixmapAll, 0, NULL );
613 XSetGraphicsExposures( display, gc, False );
614 image->byte_order = MSBFirst;
615 image->bitmap_bit_order = MSBFirst;
616 image->bitmap_unit = 16;
617 _XInitImageFuncPtrs(image);
618 if (ptr->bPlanes * ptr->bBitsPerPixel == 1)
620 /* A plain old white on black cursor. */
621 fg.red = fg.green = fg.blue = 0xffff;
622 bg.red = bg.green = bg.blue = 0x0000;
623 XPutImage( display, pixmapAll, gc, image,
624 0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
628 int rbits, gbits, bbits, red, green, blue;
629 int rfg, gfg, bfg, rbg, gbg, bbg;
630 int rscale, gscale, bscale;
631 int x, y, xmax, ymax, bitIndex, byteIndex, xorIndex;
632 unsigned char *theMask, *theImage, theChar;
633 int threshold, fgBits, bgBits, bitShifted;
634 BYTE pXorBits[128]; /* Up to 32x32 icons */
636 switch (ptr->bBitsPerPixel)
639 bitMask32 = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
640 ptr->nWidth * ptr->nHeight / 8 );
655 FIXME("Currently no support for cursors with %d bits per pixel\n",
657 XFreePixmap( display, pixmapAll );
658 XFreeGC( display, gc );
660 XDestroyImage( image );
663 /* The location of the mask. */
664 theMask = (unsigned char *)(ptr + 1);
665 /* The mask should still be 1 bit per pixel. The color image
666 * should immediately follow the mask.
668 theImage = &theMask[ptr->nWidth/8 * ptr->nHeight];
669 rfg = gfg = bfg = rbg = gbg = bbg = 0;
675 xmax = (ptr->nWidth > 32) ? 32 : ptr->nWidth;
676 if (ptr->nWidth > 32) {
677 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
678 ptr->nWidth, ptr->nHeight);
680 ymax = (ptr->nHeight > 32) ? 32 : ptr->nHeight;
682 memset(pXorBits, 0, 128);
683 for (y=0; y<ymax; y++)
685 for (x=0; x<xmax; x++)
687 red = green = blue = 0;
688 switch (ptr->bBitsPerPixel)
691 theChar = theImage[byteIndex++];
693 theChar = theImage[byteIndex++];
695 theChar = theImage[byteIndex++];
697 theChar = theImage[byteIndex++];
698 /* If the alpha channel is >5% transparent,
699 * assume that we can add it to the bitMask32.
702 *(bitMask32 + (y*xmax+x)/8) |= 1 << (x & 7);
705 theChar = theImage[byteIndex++];
707 theChar = theImage[byteIndex++];
709 theChar = theImage[byteIndex++];
713 theChar = theImage[byteIndex++];
714 blue = theChar & 0x1F;
715 green = (theChar & 0xE0) >> 5;
716 theChar = theImage[byteIndex++];
717 green |= (theChar & 0x07) << 3;
718 red = (theChar & 0xF8) >> 3;
722 if (red+green+blue > threshold)
728 pXorBits[xorIndex] |= bitShifted;
742 bitShifted = bitShifted << 1;
745 rscale = 1 << (16 - rbits);
746 gscale = 1 << (16 - gbits);
747 bscale = 1 << (16 - bbits);
750 fg.red = rfg * rscale / fgBits;
751 fg.green = gfg * gscale / fgBits;
752 fg.blue = bfg * bscale / fgBits;
754 else fg.red = fg.green = fg.blue = 0;
755 bgBits = xmax * ymax - fgBits;
758 bg.red = rbg * rscale / bgBits;
759 bg.green = gbg * gscale / bgBits;
760 bg.blue = bbg * bscale / bgBits;
762 else bg.red = bg.green = bg.blue = 0;
763 pixmapBits = XCreateBitmapFromData( display, root_window, (char *)pXorBits, xmax, ymax );
766 XFreePixmap( display, pixmapAll );
767 XFreeGC( display, gc );
769 XDestroyImage( image );
774 XPutImage( display, pixmapAll, gc, image,
775 0, 0, 0, 0, ptr->nWidth, ptr->nHeight );
776 XSetFunction( display, gc, GXcopy );
778 XCopyArea( display, pixmapBits, pixmapAll, gc,
779 0, 0, xmax, ymax, 0, ptr->nHeight );
780 XFreePixmap( display, pixmapBits );
783 XDestroyImage( image );
785 /* Now create the 2 pixmaps for bits and mask */
787 pixmapBits = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
788 if (ptr->bBitsPerPixel != 32)
790 pixmapMaskInv = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
791 pixmapMask = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
793 /* Make sure everything went OK so far */
794 if (pixmapBits && pixmapMask && pixmapMaskInv)
796 /* We have to do some magic here, as cursors are not fully
797 * compatible between Windows and X11. Under X11, there are
798 * only 3 possible color cursor: black, white and masked. So
799 * we map the 4th Windows color (invert the bits on the screen)
800 * to black and an additional white bit on an other place
801 * (+1,+1). This require some boolean arithmetic:
804 * And Xor Result | Bits Mask Result
805 * 0 0 black | 0 1 background
806 * 0 1 white | 1 1 foreground
807 * 1 0 no change | X 0 no change
808 * 1 1 inverted | 0 1 background
811 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
812 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
814 * FIXME: apparently some servers do support 'inverted' color.
815 * I don't know if it's correct per the X spec, but maybe we
816 * ought to take advantage of it. -- AJ
818 XSetFunction( display, gc, GXcopy );
819 XCopyArea( display, pixmapAll, pixmapBits, gc,
820 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
821 XCopyArea( display, pixmapAll, pixmapMask, gc,
822 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
823 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
824 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
825 XSetFunction( display, gc, GXand );
826 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
827 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
828 XSetFunction( display, gc, GXandReverse );
829 XCopyArea( display, pixmapAll, pixmapBits, gc,
830 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
831 XSetFunction( display, gc, GXorReverse );
832 XCopyArea( display, pixmapAll, pixmapMask, gc,
833 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
834 /* Additional white */
835 XSetFunction( display, gc, GXor );
836 XCopyArea( display, pixmapMaskInv, pixmapMask, gc,
837 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
838 XCopyArea( display, pixmapMaskInv, pixmapBits, gc,
839 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
840 XSetFunction( display, gc, GXcopy );
845 pixmapMask = XCreateBitmapFromData( display, root_window,
846 bitMask32, ptr->nWidth,
848 HeapFree( GetProcessHeap(), 0, bitMask32 );
851 /* Make sure hotspot is valid */
852 hotspot.x = ptr->ptHotSpot.x;
853 hotspot.y = ptr->ptHotSpot.y;
854 if (hotspot.x < 0 || hotspot.x >= ptr->nWidth ||
855 hotspot.y < 0 || hotspot.y >= ptr->nHeight)
857 hotspot.x = ptr->nWidth / 2;
858 hotspot.y = ptr->nHeight / 2;
861 if (pixmapBits && pixmapMask)
862 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
863 &fg, &bg, hotspot.x, hotspot.y );
865 /* Now free everything */
867 if (pixmapAll) XFreePixmap( display, pixmapAll );
868 if (pixmapBits) XFreePixmap( display, pixmapBits );
869 if (pixmapMask) XFreePixmap( display, pixmapMask );
870 if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
871 XFreeGC( display, gc );
877 /***********************************************************************
878 * SetCursor (X11DRV.@)
880 void X11DRV_SetCursor( CURSORICONINFO *lpCursor )
885 TRACE("%ux%u, planes %u, bpp %u\n",
886 lpCursor->nWidth, lpCursor->nHeight, lpCursor->bPlanes, lpCursor->bBitsPerPixel);
890 if (root_window != DefaultRootWindow(gdi_display))
892 /* If in desktop mode, set the cursor on the desktop window */
895 cursor = create_cursor( gdi_display, lpCursor );
898 XDefineCursor( gdi_display, root_window, cursor );
899 /* Make the change take effect immediately */
901 XFreeCursor( gdi_display, cursor );
905 else /* set the same cursor for all top-level windows of the current thread */
907 struct x11drv_thread_data *data = x11drv_thread_data();
910 cursor = create_cursor( data->display, lpCursor );
913 if (data->cursor) XFreeCursor( data->display, data->cursor );
914 data->cursor = cursor;
915 if (data->cursor_window)
917 XDefineCursor( data->display, data->cursor_window, cursor );
918 /* Make the change take effect immediately */
919 XFlush( data->display );
926 /***********************************************************************
927 * SetCursorPos (X11DRV.@)
929 BOOL X11DRV_SetCursorPos( INT x, INT y )
931 Display *display = thread_display();
934 TRACE( "warping to (%d,%d)\n", x, y );
937 if (cursor_pos.x == x && cursor_pos.y == y)
940 /* We still need to generate WM_MOUSEMOVE */
941 queue_raw_mouse_message( WM_MOUSEMOVE, NULL, x, y, 0, GetCurrentTime(), 0, 0 );
946 clip_point_to_rect( &cursor_clip, &pt);
947 XWarpPointer( display, root_window, root_window, 0, 0, 0, 0,
948 pt.x - virtual_screen_rect.left, pt.y - virtual_screen_rect.top );
949 XFlush( display ); /* avoids bad mouse lag in games that do their own mouse warping */
955 /***********************************************************************
956 * GetCursorPos (X11DRV.@)
958 BOOL X11DRV_GetCursorPos(LPPOINT pos)
960 Display *display = thread_display();
962 int rootX, rootY, winX, winY;
966 if ((GetTickCount() - last_time_modified > 100) &&
967 XQueryPointer( display, root_window, &root, &child,
968 &rootX, &rootY, &winX, &winY, &xstate ))
970 update_button_state( xstate );
971 winX += virtual_screen_rect.left;
972 winY += virtual_screen_rect.top;
973 TRACE("pointer at (%d,%d)\n", winX, winY );
983 /***********************************************************************
984 * ClipCursor (X11DRV.@)
986 * Set the cursor clipping rectangle.
988 BOOL X11DRV_ClipCursor( LPCRECT clip )
990 if (!IntersectRect( &cursor_clip, &virtual_screen_rect, clip ))
991 cursor_clip = virtual_screen_rect;
996 /***********************************************************************
999 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1001 XButtonEvent *event = &xev->xbutton;
1002 int buttonNum = event->button - 1;
1006 if (buttonNum >= NB_BUTTONS) return;
1012 wData = WHEEL_DELTA;
1015 wData = -WHEEL_DELTA;
1025 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1027 X11DRV_send_mouse_input( hwnd, button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1028 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1032 /***********************************************************************
1033 * X11DRV_ButtonRelease
1035 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1037 XButtonEvent *event = &xev->xbutton;
1038 int buttonNum = event->button - 1;
1042 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1055 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1057 X11DRV_send_mouse_input( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1058 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1062 /***********************************************************************
1063 * X11DRV_MotionNotify
1065 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1067 XMotionEvent *event = &xev->xmotion;
1070 TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);
1074 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1076 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1077 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1081 /***********************************************************************
1082 * X11DRV_EnterNotify
1084 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1086 XCrossingEvent *event = &xev->xcrossing;
1089 TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);
1092 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1094 /* simulate a mouse motion event */
1095 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1097 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1098 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );