Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / winex11.drv / mouse.c
blobc49f44df94f89b34b79fb1ce2713810712bb66c0
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 HAVE_X11_XCURSOR_XCURSOR_H
29 # include <X11/Xcursor/Xcursor.h>
30 # ifndef SONAME_LIBXCURSOR
31 # define SONAME_LIBXCURSOR "libXcursor.so"
32 # endif
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);
38 # undef MAKE_FUNCPTR
39 #endif /* HAVE_X11_XCURSOR_XCURSOR_H */
41 #define NONAMELESSUNION
42 #define NONAMELESSSTRUCT
43 #include "windef.h"
44 #include "winbase.h"
45 #include "wine/winuser16.h"
47 #include "win.h"
48 #include "x11drv.h"
49 #include "wine/server.h"
50 #include "wine/library.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
55 /**********************************************************************/
57 #ifndef Button6Mask
58 #define Button6Mask (1<<13)
59 #endif
60 #ifndef Button7Mask
61 #define Button7Mask (1<<14)
62 #endif
64 #define NB_BUTTONS 7 /* Windows can handle 5 buttons and the wheel too */
66 static const UINT button_down_flags[NB_BUTTONS] =
68 MOUSEEVENTF_LEFTDOWN,
69 MOUSEEVENTF_MIDDLEDOWN,
70 MOUSEEVENTF_RIGHTDOWN,
71 MOUSEEVENTF_WHEEL,
72 MOUSEEVENTF_WHEEL,
73 MOUSEEVENTF_XDOWN,
74 MOUSEEVENTF_XDOWN
77 static const UINT button_up_flags[NB_BUTTONS] =
79 MOUSEEVENTF_LEFTUP,
80 MOUSEEVENTF_MIDDLEUP,
81 MOUSEEVENTF_RIGHTUP,
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 );
93 extern DWORD X11DRV_MsgWaitForMultipleObjectsEx( DWORD count,
94 const HANDLE *handles, DWORD timeout, DWORD mask, DWORD flags);
97 /***********************************************************************
98 * X11DRV_Xcursor_Init
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");
109 return;
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);
117 #undef LOAD_FUNCPTR
118 #endif /* HAVE_X11_XCURSOR_XCURSOR_H */
122 /***********************************************************************
123 * get_coords
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;
136 if (!data->toplevel)
138 MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, pt, 1 );
139 hwnd = GetAncestor( hwnd, GA_ROOT );
141 return hwnd;
144 /***********************************************************************
145 * clip_point_to_rect
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 /***********************************************************************
172 * update_mouse_state
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;
192 wine_tsx11_lock();
193 if (data->cursor) XDefineCursor( data->display, window, data->cursor );
194 wine_tsx11_unlock();
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 )
205 req->window = hwnd;
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 );
212 SERVER_END_REQ;
214 return hwnd;
218 /***********************************************************************
219 * get_key_state
221 static WORD get_key_state(void)
223 WORD ret = 0;
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;
230 else
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;
240 return ret;
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 )
250 MSLLHOOKSTRUCT hook;
252 hook.pt.x = x;
253 hook.pt.y = y;
254 hook.mouseData = MAKELONG( 0, data );
255 hook.flags = injected_flags;
256 hook.time = time;
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();
265 req->win = hwnd;
266 req->msg = message;
267 req->wparam = MAKEWPARAM( get_key_state(), data );
268 req->lparam = 0;
269 req->x = x;
270 req->y = y;
271 req->time = time;
272 req->info = extra_info;
273 wine_server_call( req );
275 SERVER_END_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 )
286 POINT pt;
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;
295 else
297 pt.x = x;
298 pt.y = y;
299 wine_tsx11_lock();
300 if (cursor_pos.x == x && cursor_pos.y == y) flags &= ~MOUSEEVENTF_MOVE;
301 wine_tsx11_unlock();
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)
313 xMult = 2;
314 if ((abs(x) > accel[1]) && (accel[2] == 2)) xMult = 4;
316 if (abs(y) > accel[0] && accel[2] != 0)
318 yMult = 2;
319 if ((abs(y) > accel[1]) && (accel[2] == 2)) yMult = 4;
322 wine_tsx11_lock();
323 pt.x = cursor_pos.x + (long)x * xMult;
324 pt.y = cursor_pos.y + (long)y * yMult;
325 wine_tsx11_unlock();
327 else
329 wine_tsx11_lock();
330 pt = cursor_pos;
331 wine_tsx11_unlock();
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 );
343 else
345 wine_tsx11_lock();
346 clip_point_to_rect( &cursor_clip, &pt);
347 cursor_pos = pt;
348 wine_tsx11_unlock();
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 )
416 int x, xmax;
417 int y, ymax;
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;
422 XcursorImage *image;
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
447 * background color.
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
451 * black or white.
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)
463 case 32:
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;
469 break;
471 case 24:
472 /* BGR, 8 bits each */
473 *pixel_ptr = *xor_ptr++;
474 *pixel_ptr |= *xor_ptr++ << 8;
475 *pixel_ptr |= *xor_ptr++ << 16;
476 break;
478 case 16:
479 /* BGR, 5 red, 6 green, 5 blue */
480 *pixel_ptr = *xor_ptr * 0x1f;
481 *pixel_ptr |= (*xor_ptr & 0xe0) << 3;
482 ++xor_ptr;
483 *pixel_ptr |= (*xor_ptr & 0x07) << 11;
484 *pixel_ptr |= (*xor_ptr & 0xf8) << 13;
485 break;
487 case 1:
488 if (*xor_ptr & (1 << (7 - (x & 7)))) *pixel_ptr = 0xffffff;
489 else *pixel_ptr = 0;
490 if ((x & 7) == 7) ++xor_ptr;
491 break;
493 default:
494 FIXME("Currently no support for cursors with %d bits per pixel\n", ptr->bBitsPerPixel);
495 return 0;
498 if (ptr->bBitsPerPixel != 32)
500 /* Alpha channel */
501 if (~*and_ptr & (1 << (7 - (x & 7)))) *pixel_ptr |= 0xff << 24;
502 else if (*pixel_ptr)
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;
512 ++pixel_ptr;
516 return image;
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 )
527 Cursor cursor;
528 XcursorImage *image;
530 if (!ptr) /* Create an empty cursor */
532 image = pXcursorImageCreate( 1, 1 );
533 image->xhot = 0;
534 image->yhot = 0;
535 *(image->pixels) = 0;
536 cursor = pXcursorImageLoadCursor( display, image );
537 pXcursorImageDestroy( image );
539 return cursor;
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;
555 image->delay = 0;
557 cursor = pXcursorImageLoadCursor( display, image );
558 pXcursorImageDestroy( image );
560 return cursor;
563 #endif /* HAVE_X11_XCURSOR_XCURSOR_H */
566 /***********************************************************************
567 * create_cursor
569 * Create an X cursor from a Windows one.
571 static Cursor create_cursor( Display *display, CURSORICONINFO *ptr )
573 Pixmap pixmapBits, pixmapMask, pixmapMaskInv, pixmapAll;
574 XColor fg, bg;
575 Cursor cursor = None;
577 #ifdef HAVE_X11_XCURSOR_XCURSOR_H
578 if (pXcursorImageLoadCursor) return create_xcursor_cursor( display, ptr );
579 #endif
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 );
587 if (pixmapBits)
589 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
590 &bg, &bg, 0, 0 );
591 XFreePixmap( display, pixmapBits );
594 else /* Create the X cursor from the bits */
596 XImage *image;
597 GC gc;
599 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
600 ptr->nWidth, ptr->nHeight, ptr->bPlanes, ptr->bBitsPerPixel,
601 ptr->nWidthBytes);
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
607 * algorithm here.
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 )))
614 return 0;
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 );
620 return 0;
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 );
636 else
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)
648 case 24:
649 rbits = 8;
650 gbits = 8;
651 bbits = 8;
652 threshold = 0x40;
653 break;
654 case 16:
655 rbits = 5;
656 gbits = 6;
657 bbits = 5;
658 threshold = 0x40;
659 break;
660 default:
661 FIXME("Currently no support for cursors with %d bits per pixel\n",
662 ptr->bBitsPerPixel);
663 XFreePixmap( display, pixmapAll );
664 XFreeGC( display, gc );
665 image->data = NULL;
666 XDestroyImage( image );
667 return 0;
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;
676 bitIndex = 0;
677 byteIndex = 0;
678 xorIndex = 0;
679 fgBits = 0;
680 bitShifted = 0x01;
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)
696 case 24:
697 theChar = theImage[byteIndex++];
698 blue = theChar;
699 theChar = theImage[byteIndex++];
700 green = theChar;
701 theChar = theImage[byteIndex++];
702 red = theChar;
703 break;
704 case 16:
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;
711 break;
714 if (red+green+blue > threshold)
716 rfg += red;
717 gfg += green;
718 bfg += blue;
719 fgBits++;
720 pXorBits[xorIndex] |= bitShifted;
722 else
724 rbg += red;
725 gbg += green;
726 bbg += blue;
728 if (x%8 == 7)
730 bitShifted = 0x01;
731 xorIndex++;
733 else
734 bitShifted = bitShifted << 1;
737 rscale = 1 << (16 - rbits);
738 gscale = 1 << (16 - gbits);
739 bscale = 1 << (16 - bbits);
740 if (fgBits)
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;
748 if (bgBits)
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 );
756 if (!pixmapBits)
758 XFreePixmap( display, pixmapAll );
759 XFreeGC( display, gc );
760 image->data = NULL;
761 XDestroyImage( image );
762 return 0;
765 /* Put the mask. */
766 XPutImage( display, pixmapAll, gc, image,
767 0, 0, 0, 0, ptr->nWidth, ptr->nHeight );
768 XSetFunction( display, gc, GXcopy );
769 /* Put the image */
770 XCopyArea( display, pixmapBits, pixmapAll, gc,
771 0, 0, xmax, ymax, 0, ptr->nHeight );
772 XFreePixmap( display, pixmapBits );
774 image->data = NULL;
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)
787 POINT hotspot;
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:
796 * Windows | X11
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
803 * which gives:
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 );
856 return cursor;
860 /***********************************************************************
861 * SetCursor (X11DRV.@)
863 void X11DRV_SetCursor( CURSORICONINFO *lpCursor )
865 Cursor cursor;
867 if (lpCursor)
868 TRACE("%ux%u, planes %u, bpp %u\n",
869 lpCursor->nWidth, lpCursor->nHeight, lpCursor->bPlanes, lpCursor->bBitsPerPixel);
870 else
871 TRACE("NULL\n");
873 if (root_window != DefaultRootWindow(gdi_display))
875 /* If in desktop mode, set the cursor on the desktop window */
877 wine_tsx11_lock();
878 cursor = create_cursor( gdi_display, lpCursor );
879 if (cursor)
881 XDefineCursor( gdi_display, root_window, cursor );
882 /* Make the change take effect immediately */
883 XFlush(gdi_display);
884 XFreeCursor( gdi_display, cursor );
886 wine_tsx11_unlock();
888 else /* set the same cursor for all top-level windows of the current thread */
890 struct x11drv_thread_data *data = x11drv_thread_data();
892 wine_tsx11_lock();
893 cursor = create_cursor( data->display, lpCursor );
894 if (cursor)
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 );
905 wine_tsx11_unlock();
909 /***********************************************************************
910 * SetCursorPos (X11DRV.@)
912 BOOL X11DRV_SetCursorPos( INT x, INT y )
914 Display *display = thread_display();
915 POINT pt;
917 TRACE( "warping to (%d,%d)\n", x, y );
919 wine_tsx11_lock();
920 if (cursor_pos.x == x && cursor_pos.y == y)
922 wine_tsx11_unlock();
923 /* We still need to generate WM_MOUSEMOVE */
924 queue_raw_mouse_message( WM_MOUSEMOVE, NULL, x, y, 0, GetCurrentTime(), 0, 0 );
925 return TRUE;
928 pt.x = x; pt.y = y;
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 */
933 cursor_pos = pt;
934 wine_tsx11_unlock();
935 return TRUE;
938 /***********************************************************************
939 * GetAsyncKeyState (X11DRV.@)
941 SHORT X11DRV_GetAsyncKeyState( INT key )
943 Display *display = thread_display();
944 Window root, child;
945 int rootX, rootY, winX, winY;
946 unsigned int xstate;
947 SHORT retval;
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)
954 wine_tsx11_lock();
955 if (XQueryPointer( display, root_window, &root, &child,
956 &rootX, &rootY, &winX, &winY, &xstate ))
957 update_button_state( xstate );
958 wine_tsx11_unlock();
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);
964 return retval;
967 /***********************************************************************
968 * GetCursorPos (X11DRV.@)
970 BOOL X11DRV_GetCursorPos(LPPOINT pos)
972 Display *display = thread_display();
973 Window root, child;
974 int rootX, rootY, winX, winY;
975 unsigned int xstate;
977 wine_tsx11_lock();
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 );
985 cursor_pos.x = winX;
986 cursor_pos.y = winY;
988 *pos = cursor_pos;
989 wine_tsx11_unlock();
990 return TRUE;
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;
1004 return TRUE;
1007 /***********************************************************************
1008 * X11DRV_ButtonPress
1010 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1012 XButtonEvent *event = &xev->xbutton;
1013 int buttonNum = event->button - 1;
1014 WORD wData = 0;
1015 POINT pt;
1017 if (buttonNum >= NB_BUTTONS) return;
1018 if (!hwnd) return;
1020 switch (buttonNum)
1022 case 3:
1023 wData = WHEEL_DELTA;
1024 break;
1025 case 4:
1026 wData = -WHEEL_DELTA;
1027 break;
1028 case 5:
1029 wData = XBUTTON1;
1030 break;
1031 case 6:
1032 wData = XBUTTON2;
1033 break;
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;
1050 WORD wData = 0;
1051 POINT pt;
1053 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1054 if (!hwnd) return;
1056 switch (buttonNum)
1058 case 5:
1059 wData = XBUTTON1;
1060 break;
1061 case 6:
1062 wData = XBUTTON2;
1063 break;
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;
1079 POINT pt;
1081 TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);
1083 if (!hwnd) return;
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;
1098 POINT pt;
1100 TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);
1102 if (!hwnd) return;
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 );