push 9eb9af089d68d39110a91889d3a673043db63c4b
[wine/hacks.git] / dlls / winex11.drv / mouse.c
blob5df71a789dd896cf3675325b3b92f652c4b76fe5
1 /*
2 * X11 mouse driver
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <X11/Xlib.h>
26 #include <stdarg.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 # undef MAKE_FUNCPTR
36 #endif /* SONAME_LIBXCURSOR */
38 #define NONAMELESSUNION
39 #define NONAMELESSSTRUCT
40 #include "windef.h"
41 #include "winbase.h"
42 #include "wine/winuser16.h"
44 #include "x11drv.h"
45 #include "wine/server.h"
46 #include "wine/library.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
51 /**********************************************************************/
53 #ifndef Button6Mask
54 #define Button6Mask (1<<13)
55 #endif
56 #ifndef Button7Mask
57 #define Button7Mask (1<<14)
58 #endif
60 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
62 static const UINT button_down_flags[NB_BUTTONS] =
64 MOUSEEVENTF_LEFTDOWN,
65 MOUSEEVENTF_MIDDLEDOWN,
66 MOUSEEVENTF_RIGHTDOWN,
67 MOUSEEVENTF_WHEEL,
68 MOUSEEVENTF_WHEEL,
69 MOUSEEVENTF_XDOWN, /* FIXME: horizontal wheel */
70 MOUSEEVENTF_XDOWN,
71 MOUSEEVENTF_XDOWN,
72 MOUSEEVENTF_XDOWN
75 static const UINT button_up_flags[NB_BUTTONS] =
77 MOUSEEVENTF_LEFTUP,
78 MOUSEEVENTF_MIDDLEUP,
79 MOUSEEVENTF_RIGHTUP,
82 MOUSEEVENTF_XUP,
83 MOUSEEVENTF_XUP,
84 MOUSEEVENTF_XUP,
85 MOUSEEVENTF_XUP
88 POINT cursor_pos;
89 static DWORD last_time_modified;
90 static RECT cursor_clip; /* Cursor clipping rect */
92 BOOL X11DRV_SetCursorPos( INT x, INT y );
95 /***********************************************************************
96 * X11DRV_Xcursor_Init
98 * Load the Xcursor library for use.
100 void X11DRV_Xcursor_Init(void)
102 #ifdef SONAME_LIBXCURSOR
103 xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
104 if (!xcursor_handle) /* wine_dlopen failed. */
106 WARN("Xcursor failed to load. Using fallback code.\n");
107 return;
109 #define LOAD_FUNCPTR(f) \
110 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
112 LOAD_FUNCPTR(XcursorImageCreate);
113 LOAD_FUNCPTR(XcursorImageDestroy);
114 LOAD_FUNCPTR(XcursorImageLoadCursor);
115 #undef LOAD_FUNCPTR
116 #endif /* SONAME_LIBXCURSOR */
120 /***********************************************************************
121 * get_coords
123 * get the coordinates of a mouse event
125 static inline void get_coords( HWND hwnd, Window window, int x, int y, POINT *pt )
127 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
129 if (!data) return;
131 if (window == data->client_window)
133 pt->x = x + data->client_rect.left;
134 pt->y = y + data->client_rect.top;
136 else
138 pt->x = x + data->whole_rect.left;
139 pt->y = y + data->whole_rect.top;
143 /***********************************************************************
144 * clip_point_to_rect
146 * Clip point to the provided rectangle
148 static inline void clip_point_to_rect( LPCRECT rect, LPPOINT pt )
150 if (pt->x < rect->left) pt->x = rect->left;
151 else if (pt->x >= rect->right) pt->x = rect->right - 1;
152 if (pt->y < rect->top) pt->y = rect->top;
153 else if (pt->y >= rect->bottom) pt->y = rect->bottom - 1;
156 /***********************************************************************
157 * update_button_state
159 * Update the button state with what X provides us
161 static inline void update_button_state( unsigned int state )
163 key_state_table[VK_LBUTTON] = (state & Button1Mask ? 0x80 : 0);
164 key_state_table[VK_MBUTTON] = (state & Button2Mask ? 0x80 : 0);
165 key_state_table[VK_RBUTTON] = (state & Button3Mask ? 0x80 : 0);
166 /* X-buttons are not reported from XQueryPointer */
170 /***********************************************************************
171 * update_mouse_state
173 * Update the various window states on a mouse event.
175 static void update_mouse_state( HWND hwnd, Window window, int x, int y, unsigned int state, POINT *pt )
177 struct x11drv_thread_data *data = x11drv_thread_data();
179 get_coords( hwnd, window, x, y, pt );
181 /* update the cursor */
183 if (data->cursor_window != window)
185 data->cursor_window = window;
186 wine_tsx11_lock();
187 if (data->cursor) XDefineCursor( data->display, window, data->cursor );
188 wine_tsx11_unlock();
191 /* update the wine server Z-order */
193 if (window != data->grab_window &&
194 /* ignore event if a button is pressed, since the mouse is then grabbed too */
195 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
197 SERVER_START_REQ( update_window_zorder )
199 req->window = hwnd;
200 req->rect.left = pt->x;
201 req->rect.top = pt->y;
202 req->rect.right = pt->x + 1;
203 req->rect.bottom = pt->y + 1;
204 wine_server_call( req );
206 SERVER_END_REQ;
211 /***********************************************************************
212 * get_key_state
214 static WORD get_key_state(void)
216 WORD ret = 0;
218 if (GetSystemMetrics( SM_SWAPBUTTON ))
220 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
221 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
223 else
225 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
226 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
228 if (key_state_table[VK_MBUTTON] & 0x80) ret |= MK_MBUTTON;
229 if (key_state_table[VK_SHIFT] & 0x80) ret |= MK_SHIFT;
230 if (key_state_table[VK_CONTROL] & 0x80) ret |= MK_CONTROL;
231 if (key_state_table[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
232 if (key_state_table[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
233 return ret;
237 /***********************************************************************
238 * queue_raw_mouse_message
240 static void queue_raw_mouse_message( UINT message, HWND hwnd, DWORD x, DWORD y,
241 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
243 MSLLHOOKSTRUCT hook;
245 hook.pt.x = x;
246 hook.pt.y = y;
247 hook.mouseData = MAKELONG( 0, data );
248 hook.flags = injected_flags;
249 hook.time = time;
250 hook.dwExtraInfo = extra_info;
252 last_time_modified = GetTickCount();
253 if (HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE )) return;
255 SERVER_START_REQ( send_hardware_message )
257 req->id = (injected_flags & LLMHF_INJECTED) ? 0 : GetCurrentThreadId();
258 req->win = hwnd;
259 req->msg = message;
260 req->wparam = MAKEWPARAM( get_key_state(), data );
261 req->lparam = 0;
262 req->x = x;
263 req->y = y;
264 req->time = time;
265 req->info = extra_info;
266 wine_server_call( req );
268 SERVER_END_REQ;
273 /***********************************************************************
274 * X11DRV_send_mouse_input
276 void X11DRV_send_mouse_input( HWND hwnd, DWORD flags, DWORD x, DWORD y,
277 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
279 POINT pt;
281 if (flags & MOUSEEVENTF_MOVE && flags & MOUSEEVENTF_ABSOLUTE)
283 if (injected_flags & LLMHF_INJECTED)
285 pt.x = (x * screen_width) >> 16;
286 pt.y = (y * screen_height) >> 16;
288 else
290 pt.x = x;
291 pt.y = y;
292 wine_tsx11_lock();
293 if (cursor_pos.x == x && cursor_pos.y == y &&
294 (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE)))
295 flags &= ~MOUSEEVENTF_MOVE;
296 wine_tsx11_unlock();
299 else if (flags & MOUSEEVENTF_MOVE)
301 int accel[3], xMult = 1, yMult = 1;
303 /* dx and dy can be negative numbers for relative movements */
304 SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
306 if (abs(x) > accel[0] && accel[2] != 0)
308 xMult = 2;
309 if ((abs(x) > accel[1]) && (accel[2] == 2)) xMult = 4;
311 if (abs(y) > accel[0] && accel[2] != 0)
313 yMult = 2;
314 if ((abs(y) > accel[1]) && (accel[2] == 2)) yMult = 4;
317 wine_tsx11_lock();
318 pt.x = cursor_pos.x + (long)x * xMult;
319 pt.y = cursor_pos.y + (long)y * yMult;
320 wine_tsx11_unlock();
322 else
324 wine_tsx11_lock();
325 pt = cursor_pos;
326 wine_tsx11_unlock();
329 if (flags & MOUSEEVENTF_MOVE)
331 queue_raw_mouse_message( WM_MOUSEMOVE, hwnd, pt.x, pt.y, data, time,
332 extra_info, injected_flags );
333 if ((injected_flags & LLMHF_INJECTED) &&
334 ((flags & MOUSEEVENTF_ABSOLUTE) || x || y)) /* we have to actually move the cursor */
336 X11DRV_SetCursorPos( pt.x, pt.y );
338 else
340 wine_tsx11_lock();
341 clip_point_to_rect( &cursor_clip, &pt);
342 cursor_pos = pt;
343 wine_tsx11_unlock();
346 if (flags & MOUSEEVENTF_LEFTDOWN)
348 key_state_table[VK_LBUTTON] |= 0xc0;
349 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
350 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
352 if (flags & MOUSEEVENTF_LEFTUP)
354 key_state_table[VK_LBUTTON] &= ~0x80;
355 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONUP : WM_LBUTTONUP,
356 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
358 if (flags & MOUSEEVENTF_RIGHTDOWN)
360 key_state_table[VK_RBUTTON] |= 0xc0;
361 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
362 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
364 if (flags & MOUSEEVENTF_RIGHTUP)
366 key_state_table[VK_RBUTTON] &= ~0x80;
367 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONUP : WM_RBUTTONUP,
368 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
370 if (flags & MOUSEEVENTF_MIDDLEDOWN)
372 key_state_table[VK_MBUTTON] |= 0xc0;
373 queue_raw_mouse_message( WM_MBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
374 extra_info, injected_flags );
376 if (flags & MOUSEEVENTF_MIDDLEUP)
378 key_state_table[VK_MBUTTON] &= ~0x80;
379 queue_raw_mouse_message( WM_MBUTTONUP, hwnd, pt.x, pt.y, data, time,
380 extra_info, injected_flags );
382 if (flags & MOUSEEVENTF_WHEEL)
384 queue_raw_mouse_message( WM_MOUSEWHEEL, hwnd, pt.x, pt.y, data, time,
385 extra_info, injected_flags );
387 if (flags & MOUSEEVENTF_XDOWN)
389 key_state_table[VK_XBUTTON1 + data - 1] |= 0xc0;
390 queue_raw_mouse_message( WM_XBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
391 extra_info, injected_flags );
393 if (flags & MOUSEEVENTF_XUP)
395 key_state_table[VK_XBUTTON1 + data - 1] &= ~0x80;
396 queue_raw_mouse_message( WM_XBUTTONUP, hwnd, pt.x, pt.y, data, time,
397 extra_info, injected_flags );
402 #ifdef SONAME_LIBXCURSOR
404 /***********************************************************************
405 * create_cursor_image
407 * Create an XcursorImage from a CURSORICONINFO
409 static XcursorImage *create_cursor_image( CURSORICONINFO *ptr )
411 int x;
412 int y;
413 int and_size;
414 unsigned char *and_bits, *and_ptr, *xor_bits, *xor_ptr;
415 int and_width_bytes, xor_width_bytes;
416 XcursorPixel *pixel_ptr;
417 XcursorImage *image;
418 BOOL alpha_zero = TRUE;
420 and_width_bytes = ptr->nWidth / 8;
421 xor_width_bytes = and_width_bytes * ptr->bBitsPerPixel;
423 and_size = ptr->nWidth * ptr->nHeight / 8;
424 and_ptr = and_bits = (unsigned char *)(ptr + 1);
426 xor_ptr = xor_bits = and_ptr + and_size;
428 image = pXcursorImageCreate( ptr->nWidth, ptr->nHeight );
429 pixel_ptr = image->pixels;
431 /* Generally 32 bit bitmaps have an alpha channel which is used in favor
432 * of the AND mask. However, if all pixels have alpha = 0x00, the bitmap
433 * is treated like one without alpha and the masks are used. As soon as
434 * one pixel has alpha != 0x00, and the mask ignored as described in the
435 * docs.
437 * This is most likely for applications which create the bitmaps with
438 * CreateDIBitmap, which creates a device dependent bitmap, so the format
439 * that arrives when loading depends on the screen's bpp. Apps that were
440 * written at 8 / 16 bpp times do not know about the 32 bit alpha, so
441 * they would get a completely transparent cursor on 32 bit displays.
443 * Non-32 bit bitmaps always use the AND mask
445 if(ptr->bBitsPerPixel == 32)
447 for (y = 0; alpha_zero && y < ptr->nHeight; ++y)
449 xor_ptr = xor_bits + (y * xor_width_bytes);
450 for (x = 0; x < ptr->nWidth; ++x)
452 if (xor_ptr[3] != 0x00)
454 alpha_zero = FALSE;
455 break;
457 xor_ptr+=4;
462 /* On windows, to calculate the color for a pixel, first an AND is done
463 * with the background and the "and" bitmap, then an XOR with the "xor"
464 * bitmap. This means that when the data in the "and" bitmap is 0, the
465 * pixel will get the color as specified in the "xor" bitmap.
466 * However, if the data in the "and" bitmap is 1, the result will be the
467 * background XOR'ed with the value in the "xor" bitmap. In case the "xor"
468 * data is completely black (0x000000) the pixel will become transparent,
469 * in case it's white (0xffffff) the pixel will become the inverse of the
470 * background color.
472 * Since we can't support inverting colors, we map the grayscale value of
473 * the "xor" data to the alpha channel, and xor the color with either
474 * black or white.
476 for (y = 0; y < ptr->nHeight; ++y)
478 and_ptr = and_bits + (y * and_width_bytes);
479 xor_ptr = xor_bits + (y * xor_width_bytes);
481 for (x = 0; x < ptr->nWidth; ++x)
483 /* Xcursor pixel data is in ARGB format, with A in the high byte */
484 switch (ptr->bBitsPerPixel)
486 case 32:
487 /* BGRA, 8 bits each */
488 *pixel_ptr = *xor_ptr++;
489 *pixel_ptr |= *xor_ptr++ << 8;
490 *pixel_ptr |= *xor_ptr++ << 16;
491 *pixel_ptr |= *xor_ptr++ << 24;
492 break;
494 case 24:
495 /* BGR, 8 bits each */
496 *pixel_ptr = *xor_ptr++;
497 *pixel_ptr |= *xor_ptr++ << 8;
498 *pixel_ptr |= *xor_ptr++ << 16;
499 break;
501 case 16:
502 /* BGR, 5 red, 6 green, 5 blue */
503 *pixel_ptr = *xor_ptr * 0x1f;
504 *pixel_ptr |= (*xor_ptr & 0xe0) << 3;
505 ++xor_ptr;
506 *pixel_ptr |= (*xor_ptr & 0x07) << 11;
507 *pixel_ptr |= (*xor_ptr & 0xf8) << 13;
508 break;
510 case 1:
511 if (*xor_ptr & (1 << (7 - (x & 7)))) *pixel_ptr = 0xffffff;
512 else *pixel_ptr = 0;
513 if ((x & 7) == 7) ++xor_ptr;
514 break;
516 default:
517 FIXME("Currently no support for cursors with %d bits per pixel\n", ptr->bBitsPerPixel);
518 return 0;
521 if (alpha_zero)
523 /* Alpha channel */
524 if (~*and_ptr & (1 << (7 - (x & 7)))) *pixel_ptr |= 0xff << 24;
525 else if (*pixel_ptr)
527 int alpha = (*pixel_ptr & 0xff) * 0.30f
528 + ((*pixel_ptr & 0xff00) >> 8) * 0.55f
529 + ((*pixel_ptr & 0xff0000) >> 16) * 0.15f;
530 *pixel_ptr ^= ((x + y) % 2) ? 0xffffff : 0x000000;
531 *pixel_ptr |= alpha << 24;
533 if ((x & 7) == 7) ++and_ptr;
535 ++pixel_ptr;
539 return image;
543 /***********************************************************************
544 * create_xcursor_cursor
546 * Use Xcursor to create an X cursor from a Windows one.
548 static Cursor create_xcursor_cursor( Display *display, CURSORICONINFO *ptr )
550 Cursor cursor;
551 XcursorImage *image;
553 if (!ptr) /* Create an empty cursor */
555 image = pXcursorImageCreate( 1, 1 );
556 image->xhot = 0;
557 image->yhot = 0;
558 *(image->pixels) = 0;
559 cursor = pXcursorImageLoadCursor( display, image );
560 pXcursorImageDestroy( image );
562 return cursor;
565 image = create_cursor_image( ptr );
566 if (!image) return 0;
568 /* Make sure hotspot is valid */
569 image->xhot = ptr->ptHotSpot.x;
570 image->yhot = ptr->ptHotSpot.y;
571 if (image->xhot >= image->width ||
572 image->yhot >= image->height)
574 image->xhot = image->width / 2;
575 image->yhot = image->height / 2;
578 image->delay = 0;
580 cursor = pXcursorImageLoadCursor( display, image );
581 pXcursorImageDestroy( image );
583 return cursor;
586 #endif /* SONAME_LIBXCURSOR */
589 /***********************************************************************
590 * create_cursor
592 * Create an X cursor from a Windows one.
594 static Cursor create_cursor( Display *display, CURSORICONINFO *ptr )
596 Pixmap pixmapBits, pixmapMask, pixmapMaskInv = 0, pixmapAll;
597 XColor fg, bg;
598 Cursor cursor = None;
599 POINT hotspot;
600 char *bitMask32 = NULL;
602 #ifdef SONAME_LIBXCURSOR
603 if (pXcursorImageLoadCursor) return create_xcursor_cursor( display, ptr );
604 #endif
606 if (!ptr) /* Create an empty cursor */
608 static const char data[] = { 0 };
610 bg.red = bg.green = bg.blue = 0x0000;
611 pixmapBits = XCreateBitmapFromData( display, root_window, data, 1, 1 );
612 if (pixmapBits)
614 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
615 &bg, &bg, 0, 0 );
616 XFreePixmap( display, pixmapBits );
619 else /* Create the X cursor from the bits */
621 XImage *image;
622 GC gc;
624 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
625 ptr->nWidth, ptr->nHeight, ptr->bPlanes, ptr->bBitsPerPixel,
626 ptr->nWidthBytes);
628 /* Create a pixmap and transfer all the bits to it */
630 /* NOTE: Following hack works, but only because XFree depth
631 * 1 images really use 1 bit/pixel (and so the same layout
632 * as the Windows cursor data). Perhaps use a more generic
633 * algorithm here.
635 /* This pixmap will be written with two bitmaps. The first is
636 * the mask and the second is the image.
638 if (!(pixmapAll = XCreatePixmap( display, root_window,
639 ptr->nWidth, ptr->nHeight * 2, 1 )))
640 return 0;
641 if (!(image = XCreateImage( display, visual,
642 1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
643 ptr->nHeight * 2, 16, ptr->nWidthBytes/ptr->bBitsPerPixel)))
645 XFreePixmap( display, pixmapAll );
646 return 0;
648 gc = XCreateGC( display, pixmapAll, 0, NULL );
649 XSetGraphicsExposures( display, gc, False );
650 image->byte_order = MSBFirst;
651 image->bitmap_bit_order = MSBFirst;
652 image->bitmap_unit = 16;
653 _XInitImageFuncPtrs(image);
654 if (ptr->bPlanes * ptr->bBitsPerPixel == 1)
656 /* A plain old white on black cursor. */
657 fg.red = fg.green = fg.blue = 0xffff;
658 bg.red = bg.green = bg.blue = 0x0000;
659 XPutImage( display, pixmapAll, gc, image,
660 0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
662 else
664 int rbits, gbits, bbits, red, green, blue;
665 int rfg, gfg, bfg, rbg, gbg, bbg;
666 int rscale, gscale, bscale;
667 int x, y, xmax, ymax, byteIndex, xorIndex;
668 unsigned char *theMask, *theImage, theChar;
669 int threshold, fgBits, bgBits, bitShifted;
670 BYTE pXorBits[128]; /* Up to 32x32 icons */
672 switch (ptr->bBitsPerPixel)
674 case 32:
675 bitMask32 = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
676 ptr->nWidth * ptr->nHeight / 8 );
677 /* Fallthrough */
678 case 24:
679 rbits = 8;
680 gbits = 8;
681 bbits = 8;
682 threshold = 0x40;
683 break;
684 case 16:
685 rbits = 5;
686 gbits = 6;
687 bbits = 5;
688 threshold = 0x40;
689 break;
690 default:
691 FIXME("Currently no support for cursors with %d bits per pixel\n",
692 ptr->bBitsPerPixel);
693 XFreePixmap( display, pixmapAll );
694 XFreeGC( display, gc );
695 image->data = NULL;
696 XDestroyImage( image );
697 return 0;
699 /* The location of the mask. */
700 theMask = (unsigned char *)(ptr + 1);
701 /* The mask should still be 1 bit per pixel. The color image
702 * should immediately follow the mask.
704 theImage = &theMask[ptr->nWidth/8 * ptr->nHeight];
705 rfg = gfg = bfg = rbg = gbg = bbg = 0;
706 byteIndex = 0;
707 xorIndex = 0;
708 fgBits = 0;
709 bitShifted = 0x01;
710 xmax = (ptr->nWidth > 32) ? 32 : ptr->nWidth;
711 if (ptr->nWidth > 32) {
712 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
713 ptr->nWidth, ptr->nHeight);
715 ymax = (ptr->nHeight > 32) ? 32 : ptr->nHeight;
717 memset(pXorBits, 0, 128);
718 for (y=0; y<ymax; y++)
720 for (x=0; x<xmax; x++)
722 red = green = blue = 0;
723 switch (ptr->bBitsPerPixel)
725 case 32:
726 theChar = theImage[byteIndex++];
727 blue = theChar;
728 theChar = theImage[byteIndex++];
729 green = theChar;
730 theChar = theImage[byteIndex++];
731 red = theChar;
732 theChar = theImage[byteIndex++];
733 /* If the alpha channel is >5% transparent,
734 * assume that we can add it to the bitMask32.
736 if (theChar > 0x0D)
737 *(bitMask32 + (y*xmax+x)/8) |= 1 << (x & 7);
738 break;
739 case 24:
740 theChar = theImage[byteIndex++];
741 blue = theChar;
742 theChar = theImage[byteIndex++];
743 green = theChar;
744 theChar = theImage[byteIndex++];
745 red = theChar;
746 break;
747 case 16:
748 theChar = theImage[byteIndex++];
749 blue = theChar & 0x1F;
750 green = (theChar & 0xE0) >> 5;
751 theChar = theImage[byteIndex++];
752 green |= (theChar & 0x07) << 3;
753 red = (theChar & 0xF8) >> 3;
754 break;
757 if (red+green+blue > threshold)
759 rfg += red;
760 gfg += green;
761 bfg += blue;
762 fgBits++;
763 pXorBits[xorIndex] |= bitShifted;
765 else
767 rbg += red;
768 gbg += green;
769 bbg += blue;
771 if (x%8 == 7)
773 bitShifted = 0x01;
774 xorIndex++;
776 else
777 bitShifted = bitShifted << 1;
780 rscale = 1 << (16 - rbits);
781 gscale = 1 << (16 - gbits);
782 bscale = 1 << (16 - bbits);
783 if (fgBits)
785 fg.red = rfg * rscale / fgBits;
786 fg.green = gfg * gscale / fgBits;
787 fg.blue = bfg * bscale / fgBits;
789 else fg.red = fg.green = fg.blue = 0;
790 bgBits = xmax * ymax - fgBits;
791 if (bgBits)
793 bg.red = rbg * rscale / bgBits;
794 bg.green = gbg * gscale / bgBits;
795 bg.blue = bbg * bscale / bgBits;
797 else bg.red = bg.green = bg.blue = 0;
798 pixmapBits = XCreateBitmapFromData( display, root_window, (char *)pXorBits, xmax, ymax );
799 if (!pixmapBits)
801 HeapFree( GetProcessHeap(), 0, bitMask32 );
802 XFreePixmap( display, pixmapAll );
803 XFreeGC( display, gc );
804 image->data = NULL;
805 XDestroyImage( image );
806 return 0;
809 /* Put the mask. */
810 XPutImage( display, pixmapAll, gc, image,
811 0, 0, 0, 0, ptr->nWidth, ptr->nHeight );
812 XSetFunction( display, gc, GXcopy );
813 /* Put the image */
814 XCopyArea( display, pixmapBits, pixmapAll, gc,
815 0, 0, xmax, ymax, 0, ptr->nHeight );
816 XFreePixmap( display, pixmapBits );
818 image->data = NULL;
819 XDestroyImage( image );
821 /* Now create the 2 pixmaps for bits and mask */
823 pixmapBits = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
824 if (ptr->bBitsPerPixel != 32)
826 pixmapMaskInv = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
827 pixmapMask = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
829 /* Make sure everything went OK so far */
830 if (pixmapBits && pixmapMask && pixmapMaskInv)
832 /* We have to do some magic here, as cursors are not fully
833 * compatible between Windows and X11. Under X11, there are
834 * only 3 possible color cursor: black, white and masked. So
835 * we map the 4th Windows color (invert the bits on the screen)
836 * to black and an additional white bit on an other place
837 * (+1,+1). This require some boolean arithmetic:
839 * Windows | X11
840 * And Xor Result | Bits Mask Result
841 * 0 0 black | 0 1 background
842 * 0 1 white | 1 1 foreground
843 * 1 0 no change | X 0 no change
844 * 1 1 inverted | 0 1 background
846 * which gives:
847 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
848 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
850 * FIXME: apparently some servers do support 'inverted' color.
851 * I don't know if it's correct per the X spec, but maybe we
852 * ought to take advantage of it. -- AJ
854 XSetFunction( display, gc, GXcopy );
855 XCopyArea( display, pixmapAll, pixmapBits, gc,
856 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
857 XCopyArea( display, pixmapAll, pixmapMask, gc,
858 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
859 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
860 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
861 XSetFunction( display, gc, GXand );
862 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
863 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
864 XSetFunction( display, gc, GXandReverse );
865 XCopyArea( display, pixmapAll, pixmapBits, gc,
866 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
867 XSetFunction( display, gc, GXorReverse );
868 XCopyArea( display, pixmapAll, pixmapMask, gc,
869 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
870 /* Additional white */
871 XSetFunction( display, gc, GXor );
872 XCopyArea( display, pixmapMaskInv, pixmapMask, gc,
873 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
874 XCopyArea( display, pixmapMaskInv, pixmapBits, gc,
875 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
876 XSetFunction( display, gc, GXcopy );
879 else
881 pixmapMask = XCreateBitmapFromData( display, root_window,
882 bitMask32, ptr->nWidth,
883 ptr->nHeight );
884 HeapFree( GetProcessHeap(), 0, bitMask32 );
887 /* Make sure hotspot is valid */
888 hotspot.x = ptr->ptHotSpot.x;
889 hotspot.y = ptr->ptHotSpot.y;
890 if (hotspot.x < 0 || hotspot.x >= ptr->nWidth ||
891 hotspot.y < 0 || hotspot.y >= ptr->nHeight)
893 hotspot.x = ptr->nWidth / 2;
894 hotspot.y = ptr->nHeight / 2;
897 if (pixmapBits && pixmapMask)
898 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
899 &fg, &bg, hotspot.x, hotspot.y );
901 /* Now free everything */
903 if (pixmapAll) XFreePixmap( display, pixmapAll );
904 if (pixmapBits) XFreePixmap( display, pixmapBits );
905 if (pixmapMask) XFreePixmap( display, pixmapMask );
906 if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
907 XFreeGC( display, gc );
909 return cursor;
913 /***********************************************************************
914 * SetCursor (X11DRV.@)
916 void X11DRV_SetCursor( CURSORICONINFO *lpCursor )
918 struct x11drv_thread_data *data = x11drv_init_thread_data();
919 Cursor cursor;
921 if (lpCursor)
922 TRACE("%ux%u, planes %u, bpp %u\n",
923 lpCursor->nWidth, lpCursor->nHeight, lpCursor->bPlanes, lpCursor->bBitsPerPixel);
924 else
925 TRACE("NULL\n");
927 /* set the same cursor for all top-level windows of the current thread */
929 wine_tsx11_lock();
930 cursor = create_cursor( data->display, lpCursor );
931 if (cursor)
933 if (data->cursor) XFreeCursor( data->display, data->cursor );
934 data->cursor = cursor;
935 if (data->cursor_window)
937 XDefineCursor( data->display, data->cursor_window, cursor );
938 /* Make the change take effect immediately */
939 XFlush( data->display );
942 wine_tsx11_unlock();
945 /***********************************************************************
946 * SetCursorPos (X11DRV.@)
948 BOOL X11DRV_SetCursorPos( INT x, INT y )
950 Display *display = thread_init_display();
951 POINT pt;
953 TRACE( "warping to (%d,%d)\n", x, y );
955 wine_tsx11_lock();
956 if (cursor_pos.x == x && cursor_pos.y == y)
958 wine_tsx11_unlock();
959 /* We still need to generate WM_MOUSEMOVE */
960 queue_raw_mouse_message( WM_MOUSEMOVE, NULL, x, y, 0, GetCurrentTime(), 0, 0 );
961 return TRUE;
964 pt.x = x; pt.y = y;
965 clip_point_to_rect( &cursor_clip, &pt);
966 XWarpPointer( display, root_window, root_window, 0, 0, 0, 0,
967 pt.x - virtual_screen_rect.left, pt.y - virtual_screen_rect.top );
968 XFlush( display ); /* avoids bad mouse lag in games that do their own mouse warping */
969 cursor_pos = pt;
970 wine_tsx11_unlock();
971 return TRUE;
974 /***********************************************************************
975 * GetCursorPos (X11DRV.@)
977 BOOL X11DRV_GetCursorPos(LPPOINT pos)
979 Display *display = thread_init_display();
980 Window root, child;
981 int rootX, rootY, winX, winY;
982 unsigned int xstate;
984 wine_tsx11_lock();
985 if ((GetTickCount() - last_time_modified > 100) &&
986 XQueryPointer( display, root_window, &root, &child,
987 &rootX, &rootY, &winX, &winY, &xstate ))
989 update_button_state( xstate );
990 winX += virtual_screen_rect.left;
991 winY += virtual_screen_rect.top;
992 TRACE("pointer at (%d,%d)\n", winX, winY );
993 cursor_pos.x = winX;
994 cursor_pos.y = winY;
996 *pos = cursor_pos;
997 wine_tsx11_unlock();
998 return TRUE;
1002 /***********************************************************************
1003 * ClipCursor (X11DRV.@)
1005 * Set the cursor clipping rectangle.
1007 BOOL X11DRV_ClipCursor( LPCRECT clip )
1009 if (!IntersectRect( &cursor_clip, &virtual_screen_rect, clip ))
1010 cursor_clip = virtual_screen_rect;
1012 return TRUE;
1015 /***********************************************************************
1016 * X11DRV_ButtonPress
1018 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1020 XButtonEvent *event = &xev->xbutton;
1021 int buttonNum = event->button - 1;
1022 WORD wData = 0;
1023 POINT pt;
1025 if (buttonNum >= NB_BUTTONS) return;
1026 if (!hwnd) return;
1028 switch (buttonNum)
1030 case 3:
1031 wData = WHEEL_DELTA;
1032 break;
1033 case 4:
1034 wData = -WHEEL_DELTA;
1035 break;
1036 case 5:
1037 wData = XBUTTON1;
1038 break;
1039 case 6:
1040 wData = XBUTTON2;
1041 break;
1042 case 7:
1043 wData = XBUTTON1;
1044 break;
1045 case 8:
1046 wData = XBUTTON2;
1047 break;
1050 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1052 X11DRV_send_mouse_input( hwnd, button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1053 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1057 /***********************************************************************
1058 * X11DRV_ButtonRelease
1060 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1062 XButtonEvent *event = &xev->xbutton;
1063 int buttonNum = event->button - 1;
1064 WORD wData = 0;
1065 POINT pt;
1067 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1068 if (!hwnd) return;
1070 switch (buttonNum)
1072 case 5:
1073 wData = XBUTTON1;
1074 break;
1075 case 6:
1076 wData = XBUTTON2;
1077 break;
1078 case 7:
1079 wData = XBUTTON1;
1080 break;
1081 case 8:
1082 wData = XBUTTON2;
1083 break;
1086 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1088 X11DRV_send_mouse_input( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1089 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1093 /***********************************************************************
1094 * X11DRV_MotionNotify
1096 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1098 XMotionEvent *event = &xev->xmotion;
1099 POINT pt;
1101 TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);
1103 if (!hwnd) return;
1105 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1107 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1108 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1112 /***********************************************************************
1113 * X11DRV_EnterNotify
1115 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1117 XCrossingEvent *event = &xev->xcrossing;
1118 POINT pt;
1120 TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);
1122 if (!hwnd) return;
1123 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1124 if (event->window == x11drv_thread_data()->grab_window) return;
1126 /* simulate a mouse motion event */
1127 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1129 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1130 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );