gdi32: Properly handle SetDIBits failure in StretchDIBits.
[wine.git] / dlls / winex11.drv / mouse.c
blob736c5d98a2f1691de3f16038bcdd9092f393a860
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 7 /* 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,
70 MOUSEEVENTF_XDOWN
73 static const UINT button_up_flags[NB_BUTTONS] =
75 MOUSEEVENTF_LEFTUP,
76 MOUSEEVENTF_MIDDLEUP,
77 MOUSEEVENTF_RIGHTUP,
80 MOUSEEVENTF_XUP,
81 MOUSEEVENTF_XUP
84 POINT cursor_pos;
85 static DWORD last_time_modified;
86 static RECT cursor_clip; /* Cursor clipping rect */
88 BOOL X11DRV_SetCursorPos( INT x, INT y );
91 /***********************************************************************
92 * X11DRV_Xcursor_Init
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");
103 return;
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);
111 #undef LOAD_FUNCPTR
112 #endif /* SONAME_LIBXCURSOR */
116 /***********************************************************************
117 * get_coords
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 );
125 if (!data) return;
127 if (window == data->client_window)
129 pt->x = x + data->client_rect.left;
130 pt->y = y + data->client_rect.top;
132 else
134 pt->x = x + data->whole_rect.left;
135 pt->y = y + data->whole_rect.top;
139 /***********************************************************************
140 * clip_point_to_rect
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 /***********************************************************************
167 * update_mouse_state
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 if (window == root_window)
177 x += virtual_screen_rect.left;
178 y += virtual_screen_rect.top;
180 get_coords( hwnd, window, x, y, pt );
182 /* update the cursor */
184 if (data->cursor_window != window)
186 data->cursor_window = window;
187 wine_tsx11_lock();
188 if (data->cursor) XDefineCursor( data->display, window, data->cursor );
189 wine_tsx11_unlock();
192 /* update the wine server Z-order */
194 if (window != data->grab_window &&
195 /* ignore event if a button is pressed, since the mouse is then grabbed too */
196 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
198 SERVER_START_REQ( update_window_zorder )
200 req->window = hwnd;
201 req->rect.left = pt->x;
202 req->rect.top = pt->y;
203 req->rect.right = pt->x + 1;
204 req->rect.bottom = pt->y + 1;
205 wine_server_call( req );
207 SERVER_END_REQ;
212 /***********************************************************************
213 * get_key_state
215 static WORD get_key_state(void)
217 WORD ret = 0;
219 if (GetSystemMetrics( SM_SWAPBUTTON ))
221 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
222 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
224 else
226 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
227 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
229 if (key_state_table[VK_MBUTTON] & 0x80) ret |= MK_MBUTTON;
230 if (key_state_table[VK_SHIFT] & 0x80) ret |= MK_SHIFT;
231 if (key_state_table[VK_CONTROL] & 0x80) ret |= MK_CONTROL;
232 if (key_state_table[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
233 if (key_state_table[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
234 return ret;
238 /***********************************************************************
239 * queue_raw_mouse_message
241 static void queue_raw_mouse_message( UINT message, HWND hwnd, DWORD x, DWORD y,
242 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
244 MSLLHOOKSTRUCT hook;
246 hook.pt.x = x;
247 hook.pt.y = y;
248 hook.mouseData = MAKELONG( 0, data );
249 hook.flags = injected_flags;
250 hook.time = time;
251 hook.dwExtraInfo = extra_info;
253 last_time_modified = GetTickCount();
254 if (HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE )) return;
256 SERVER_START_REQ( send_hardware_message )
258 req->id = (injected_flags & LLMHF_INJECTED) ? 0 : GetCurrentThreadId();
259 req->win = hwnd;
260 req->msg = message;
261 req->wparam = MAKEWPARAM( get_key_state(), data );
262 req->lparam = 0;
263 req->x = x;
264 req->y = y;
265 req->time = time;
266 req->info = extra_info;
267 wine_server_call( req );
269 SERVER_END_REQ;
274 /***********************************************************************
275 * X11DRV_send_mouse_input
277 void X11DRV_send_mouse_input( HWND hwnd, DWORD flags, DWORD x, DWORD y,
278 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
280 POINT pt;
282 if (flags & MOUSEEVENTF_MOVE && flags & MOUSEEVENTF_ABSOLUTE)
284 if (injected_flags & LLMHF_INJECTED)
286 pt.x = (x * screen_width) >> 16;
287 pt.y = (y * screen_height) >> 16;
289 else
291 pt.x = x;
292 pt.y = y;
293 wine_tsx11_lock();
294 if (cursor_pos.x == x && cursor_pos.y == y &&
295 (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE)))
296 flags &= ~MOUSEEVENTF_MOVE;
297 wine_tsx11_unlock();
300 else if (flags & MOUSEEVENTF_MOVE)
302 int accel[3], xMult = 1, yMult = 1;
304 /* dx and dy can be negative numbers for relative movements */
305 SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
307 if (abs(x) > accel[0] && accel[2] != 0)
309 xMult = 2;
310 if ((abs(x) > accel[1]) && (accel[2] == 2)) xMult = 4;
312 if (abs(y) > accel[0] && accel[2] != 0)
314 yMult = 2;
315 if ((abs(y) > accel[1]) && (accel[2] == 2)) yMult = 4;
318 wine_tsx11_lock();
319 pt.x = cursor_pos.x + (long)x * xMult;
320 pt.y = cursor_pos.y + (long)y * yMult;
321 wine_tsx11_unlock();
323 else
325 wine_tsx11_lock();
326 pt = cursor_pos;
327 wine_tsx11_unlock();
330 if (flags & MOUSEEVENTF_MOVE)
332 queue_raw_mouse_message( WM_MOUSEMOVE, hwnd, pt.x, pt.y, data, time,
333 extra_info, injected_flags );
334 if ((injected_flags & LLMHF_INJECTED) &&
335 ((flags & MOUSEEVENTF_ABSOLUTE) || x || y)) /* we have to actually move the cursor */
337 X11DRV_SetCursorPos( pt.x, pt.y );
339 else
341 wine_tsx11_lock();
342 clip_point_to_rect( &cursor_clip, &pt);
343 cursor_pos = pt;
344 wine_tsx11_unlock();
347 if (flags & MOUSEEVENTF_LEFTDOWN)
349 key_state_table[VK_LBUTTON] |= 0xc0;
350 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
351 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
353 if (flags & MOUSEEVENTF_LEFTUP)
355 key_state_table[VK_LBUTTON] &= ~0x80;
356 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONUP : WM_LBUTTONUP,
357 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
359 if (flags & MOUSEEVENTF_RIGHTDOWN)
361 key_state_table[VK_RBUTTON] |= 0xc0;
362 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
363 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
365 if (flags & MOUSEEVENTF_RIGHTUP)
367 key_state_table[VK_RBUTTON] &= ~0x80;
368 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONUP : WM_RBUTTONUP,
369 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
371 if (flags & MOUSEEVENTF_MIDDLEDOWN)
373 key_state_table[VK_MBUTTON] |= 0xc0;
374 queue_raw_mouse_message( WM_MBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
375 extra_info, injected_flags );
377 if (flags & MOUSEEVENTF_MIDDLEUP)
379 key_state_table[VK_MBUTTON] &= ~0x80;
380 queue_raw_mouse_message( WM_MBUTTONUP, hwnd, pt.x, pt.y, data, time,
381 extra_info, injected_flags );
383 if (flags & MOUSEEVENTF_WHEEL)
385 queue_raw_mouse_message( WM_MOUSEWHEEL, hwnd, pt.x, pt.y, data, time,
386 extra_info, injected_flags );
388 if (flags & MOUSEEVENTF_XDOWN)
390 key_state_table[VK_XBUTTON1 + data - 1] |= 0xc0;
391 queue_raw_mouse_message( WM_XBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
392 extra_info, injected_flags );
394 if (flags & MOUSEEVENTF_XUP)
396 key_state_table[VK_XBUTTON1 + data - 1] &= ~0x80;
397 queue_raw_mouse_message( WM_XBUTTONUP, hwnd, pt.x, pt.y, data, time,
398 extra_info, injected_flags );
403 #ifdef SONAME_LIBXCURSOR
405 /***********************************************************************
406 * create_cursor_image
408 * Create an XcursorImage from a CURSORICONINFO
410 static XcursorImage *create_cursor_image( CURSORICONINFO *ptr )
412 int x, xmax;
413 int y, ymax;
414 int and_size, xor_size;
415 unsigned char *and_bits, *and_ptr, *xor_bits, *xor_ptr;
416 int and_width_bytes, xor_width_bytes;
417 XcursorPixel *pixel_ptr;
418 XcursorImage *image;
419 BOOL alpha_zero = TRUE;
421 ymax = (ptr->nHeight > 32) ? 32 : ptr->nHeight;
422 xmax = (ptr->nWidth > 32) ? 32 : ptr->nWidth;
424 and_width_bytes = xmax / 8;
425 xor_width_bytes = and_width_bytes * ptr->bBitsPerPixel;
427 and_size = ptr->nWidth * ptr->nHeight / 8;
428 and_ptr = and_bits = (unsigned char *)(ptr + 1);
430 xor_size = xor_width_bytes * ptr->nHeight;
431 xor_ptr = xor_bits = and_ptr + and_size;
433 image = pXcursorImageCreate( xmax, ymax );
434 pixel_ptr = image->pixels;
436 /* Generally 32 bit bitmaps have an alpha channel which is used in favor
437 * of the AND mask. However, if all pixels have alpha = 0x00, the bitmap
438 * is treated like one without alpha and the masks are used. As soon as
439 * one pixel has alpha != 0x00, and the mask ignored as described in the
440 * docs.
442 * This is most likely for applications which create the bitmaps with
443 * CreateDIBitmap, which creates a device dependent bitmap, so the format
444 * that arrives when loading depends on the screen's bpp. Apps that were
445 * written at 8 / 16 bpp times do not know about the 32 bit alpha, so
446 * they would get a completely transparent cursor on 32 bit displays.
448 * Non-32 bit bitmaps always use the AND mask
450 if(ptr->bBitsPerPixel == 32)
452 for (y = 0; alpha_zero && y < ymax; ++y)
454 xor_ptr = xor_bits + (y * xor_width_bytes);
455 for (x = 0; x < xmax; ++x)
457 if (xor_ptr[3] != 0x00)
459 alpha_zero = FALSE;
460 break;
462 xor_ptr+=4;
467 /* On windows, to calculate the color for a pixel, first an AND is done
468 * with the background and the "and" bitmap, then an XOR with the "xor"
469 * bitmap. This means that when the data in the "and" bitmap is 0, the
470 * pixel will get the color as specified in the "xor" bitmap.
471 * However, if the data in the "and" bitmap is 1, the result will be the
472 * background XOR'ed with the value in the "xor" bitmap. In case the "xor"
473 * data is completely black (0x000000) the pixel will become transparent,
474 * in case it's white (0xffffff) the pixel will become the inverse of the
475 * background color.
477 * Since we can't support inverting colors, we map the grayscale value of
478 * the "xor" data to the alpha channel, and xor the color with either
479 * black or white.
481 for (y = 0; y < ymax; ++y)
483 and_ptr = and_bits + (y * and_width_bytes);
484 xor_ptr = xor_bits + (y * xor_width_bytes);
486 for (x = 0; x < xmax; ++x)
488 /* Xcursor pixel data is in ARGB format, with A in the high byte */
489 switch (ptr->bBitsPerPixel)
491 case 32:
492 /* BGRA, 8 bits each */
493 *pixel_ptr = *xor_ptr++;
494 *pixel_ptr |= *xor_ptr++ << 8;
495 *pixel_ptr |= *xor_ptr++ << 16;
496 *pixel_ptr |= *xor_ptr++ << 24;
497 break;
499 case 24:
500 /* BGR, 8 bits each */
501 *pixel_ptr = *xor_ptr++;
502 *pixel_ptr |= *xor_ptr++ << 8;
503 *pixel_ptr |= *xor_ptr++ << 16;
504 break;
506 case 16:
507 /* BGR, 5 red, 6 green, 5 blue */
508 *pixel_ptr = *xor_ptr * 0x1f;
509 *pixel_ptr |= (*xor_ptr & 0xe0) << 3;
510 ++xor_ptr;
511 *pixel_ptr |= (*xor_ptr & 0x07) << 11;
512 *pixel_ptr |= (*xor_ptr & 0xf8) << 13;
513 break;
515 case 1:
516 if (*xor_ptr & (1 << (7 - (x & 7)))) *pixel_ptr = 0xffffff;
517 else *pixel_ptr = 0;
518 if ((x & 7) == 7) ++xor_ptr;
519 break;
521 default:
522 FIXME("Currently no support for cursors with %d bits per pixel\n", ptr->bBitsPerPixel);
523 return 0;
526 if (alpha_zero)
528 /* Alpha channel */
529 if (~*and_ptr & (1 << (7 - (x & 7)))) *pixel_ptr |= 0xff << 24;
530 else if (*pixel_ptr)
532 int alpha = (*pixel_ptr & 0xff) * 0.30f
533 + ((*pixel_ptr & 0xff00) >> 8) * 0.55f
534 + ((*pixel_ptr & 0xff0000) >> 16) * 0.15f;
535 *pixel_ptr ^= ((x + y) % 2) ? 0xffffff : 0x000000;
536 *pixel_ptr |= alpha << 24;
538 if ((x & 7) == 7) ++and_ptr;
540 ++pixel_ptr;
544 return image;
548 /***********************************************************************
549 * create_xcursor_cursor
551 * Use Xcursor to create an X cursor from a Windows one.
553 static Cursor create_xcursor_cursor( Display *display, CURSORICONINFO *ptr )
555 Cursor cursor;
556 XcursorImage *image;
558 if (!ptr) /* Create an empty cursor */
560 image = pXcursorImageCreate( 1, 1 );
561 image->xhot = 0;
562 image->yhot = 0;
563 *(image->pixels) = 0;
564 cursor = pXcursorImageLoadCursor( display, image );
565 pXcursorImageDestroy( image );
567 return cursor;
570 image = create_cursor_image( ptr );
571 if (!image) return 0;
573 /* Make sure hotspot is valid */
574 image->xhot = ptr->ptHotSpot.x;
575 image->yhot = ptr->ptHotSpot.y;
576 if (image->xhot >= image->width ||
577 image->yhot >= image->height)
579 image->xhot = image->width / 2;
580 image->yhot = image->height / 2;
583 image->delay = 0;
585 cursor = pXcursorImageLoadCursor( display, image );
586 pXcursorImageDestroy( image );
588 return cursor;
591 #endif /* SONAME_LIBXCURSOR */
594 /***********************************************************************
595 * create_cursor
597 * Create an X cursor from a Windows one.
599 static Cursor create_cursor( Display *display, CURSORICONINFO *ptr )
601 Pixmap pixmapBits, pixmapMask, pixmapMaskInv = 0, pixmapAll;
602 XColor fg, bg;
603 Cursor cursor = None;
604 POINT hotspot;
605 char *bitMask32 = NULL;
607 #ifdef SONAME_LIBXCURSOR
608 if (pXcursorImageLoadCursor) return create_xcursor_cursor( display, ptr );
609 #endif
611 if (!ptr) /* Create an empty cursor */
613 static const char data[] = { 0 };
615 bg.red = bg.green = bg.blue = 0x0000;
616 pixmapBits = XCreateBitmapFromData( display, root_window, data, 1, 1 );
617 if (pixmapBits)
619 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
620 &bg, &bg, 0, 0 );
621 XFreePixmap( display, pixmapBits );
624 else /* Create the X cursor from the bits */
626 XImage *image;
627 GC gc;
629 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
630 ptr->nWidth, ptr->nHeight, ptr->bPlanes, ptr->bBitsPerPixel,
631 ptr->nWidthBytes);
633 /* Create a pixmap and transfer all the bits to it */
635 /* NOTE: Following hack works, but only because XFree depth
636 * 1 images really use 1 bit/pixel (and so the same layout
637 * as the Windows cursor data). Perhaps use a more generic
638 * algorithm here.
640 /* This pixmap will be written with two bitmaps. The first is
641 * the mask and the second is the image.
643 if (!(pixmapAll = XCreatePixmap( display, root_window,
644 ptr->nWidth, ptr->nHeight * 2, 1 )))
645 return 0;
646 if (!(image = XCreateImage( display, visual,
647 1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
648 ptr->nHeight * 2, 16, ptr->nWidthBytes/ptr->bBitsPerPixel)))
650 XFreePixmap( display, pixmapAll );
651 return 0;
653 gc = XCreateGC( display, pixmapAll, 0, NULL );
654 XSetGraphicsExposures( display, gc, False );
655 image->byte_order = MSBFirst;
656 image->bitmap_bit_order = MSBFirst;
657 image->bitmap_unit = 16;
658 _XInitImageFuncPtrs(image);
659 if (ptr->bPlanes * ptr->bBitsPerPixel == 1)
661 /* A plain old white on black cursor. */
662 fg.red = fg.green = fg.blue = 0xffff;
663 bg.red = bg.green = bg.blue = 0x0000;
664 XPutImage( display, pixmapAll, gc, image,
665 0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
667 else
669 int rbits, gbits, bbits, red, green, blue;
670 int rfg, gfg, bfg, rbg, gbg, bbg;
671 int rscale, gscale, bscale;
672 int x, y, xmax, ymax, bitIndex, byteIndex, xorIndex;
673 unsigned char *theMask, *theImage, theChar;
674 int threshold, fgBits, bgBits, bitShifted;
675 BYTE pXorBits[128]; /* Up to 32x32 icons */
677 switch (ptr->bBitsPerPixel)
679 case 32:
680 bitMask32 = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
681 ptr->nWidth * ptr->nHeight / 8 );
682 /* Fallthrough */
683 case 24:
684 rbits = 8;
685 gbits = 8;
686 bbits = 8;
687 threshold = 0x40;
688 break;
689 case 16:
690 rbits = 5;
691 gbits = 6;
692 bbits = 5;
693 threshold = 0x40;
694 break;
695 default:
696 FIXME("Currently no support for cursors with %d bits per pixel\n",
697 ptr->bBitsPerPixel);
698 XFreePixmap( display, pixmapAll );
699 XFreeGC( display, gc );
700 image->data = NULL;
701 XDestroyImage( image );
702 return 0;
704 /* The location of the mask. */
705 theMask = (unsigned char *)(ptr + 1);
706 /* The mask should still be 1 bit per pixel. The color image
707 * should immediately follow the mask.
709 theImage = &theMask[ptr->nWidth/8 * ptr->nHeight];
710 rfg = gfg = bfg = rbg = gbg = bbg = 0;
711 bitIndex = 0;
712 byteIndex = 0;
713 xorIndex = 0;
714 fgBits = 0;
715 bitShifted = 0x01;
716 xmax = (ptr->nWidth > 32) ? 32 : ptr->nWidth;
717 if (ptr->nWidth > 32) {
718 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
719 ptr->nWidth, ptr->nHeight);
721 ymax = (ptr->nHeight > 32) ? 32 : ptr->nHeight;
723 memset(pXorBits, 0, 128);
724 for (y=0; y<ymax; y++)
726 for (x=0; x<xmax; x++)
728 red = green = blue = 0;
729 switch (ptr->bBitsPerPixel)
731 case 32:
732 theChar = theImage[byteIndex++];
733 blue = theChar;
734 theChar = theImage[byteIndex++];
735 green = theChar;
736 theChar = theImage[byteIndex++];
737 red = theChar;
738 theChar = theImage[byteIndex++];
739 /* If the alpha channel is >5% transparent,
740 * assume that we can add it to the bitMask32.
742 if (theChar > 0x0D)
743 *(bitMask32 + (y*xmax+x)/8) |= 1 << (x & 7);
744 break;
745 case 24:
746 theChar = theImage[byteIndex++];
747 blue = theChar;
748 theChar = theImage[byteIndex++];
749 green = theChar;
750 theChar = theImage[byteIndex++];
751 red = theChar;
752 break;
753 case 16:
754 theChar = theImage[byteIndex++];
755 blue = theChar & 0x1F;
756 green = (theChar & 0xE0) >> 5;
757 theChar = theImage[byteIndex++];
758 green |= (theChar & 0x07) << 3;
759 red = (theChar & 0xF8) >> 3;
760 break;
763 if (red+green+blue > threshold)
765 rfg += red;
766 gfg += green;
767 bfg += blue;
768 fgBits++;
769 pXorBits[xorIndex] |= bitShifted;
771 else
773 rbg += red;
774 gbg += green;
775 bbg += blue;
777 if (x%8 == 7)
779 bitShifted = 0x01;
780 xorIndex++;
782 else
783 bitShifted = bitShifted << 1;
786 rscale = 1 << (16 - rbits);
787 gscale = 1 << (16 - gbits);
788 bscale = 1 << (16 - bbits);
789 if (fgBits)
791 fg.red = rfg * rscale / fgBits;
792 fg.green = gfg * gscale / fgBits;
793 fg.blue = bfg * bscale / fgBits;
795 else fg.red = fg.green = fg.blue = 0;
796 bgBits = xmax * ymax - fgBits;
797 if (bgBits)
799 bg.red = rbg * rscale / bgBits;
800 bg.green = gbg * gscale / bgBits;
801 bg.blue = bbg * bscale / bgBits;
803 else bg.red = bg.green = bg.blue = 0;
804 pixmapBits = XCreateBitmapFromData( display, root_window, (char *)pXorBits, xmax, ymax );
805 if (!pixmapBits)
807 HeapFree( GetProcessHeap(), 0, bitMask32 );
808 XFreePixmap( display, pixmapAll );
809 XFreeGC( display, gc );
810 image->data = NULL;
811 XDestroyImage( image );
812 return 0;
815 /* Put the mask. */
816 XPutImage( display, pixmapAll, gc, image,
817 0, 0, 0, 0, ptr->nWidth, ptr->nHeight );
818 XSetFunction( display, gc, GXcopy );
819 /* Put the image */
820 XCopyArea( display, pixmapBits, pixmapAll, gc,
821 0, 0, xmax, ymax, 0, ptr->nHeight );
822 XFreePixmap( display, pixmapBits );
824 image->data = NULL;
825 XDestroyImage( image );
827 /* Now create the 2 pixmaps for bits and mask */
829 pixmapBits = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
830 if (ptr->bBitsPerPixel != 32)
832 pixmapMaskInv = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
833 pixmapMask = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
835 /* Make sure everything went OK so far */
836 if (pixmapBits && pixmapMask && pixmapMaskInv)
838 /* We have to do some magic here, as cursors are not fully
839 * compatible between Windows and X11. Under X11, there are
840 * only 3 possible color cursor: black, white and masked. So
841 * we map the 4th Windows color (invert the bits on the screen)
842 * to black and an additional white bit on an other place
843 * (+1,+1). This require some boolean arithmetic:
845 * Windows | X11
846 * And Xor Result | Bits Mask Result
847 * 0 0 black | 0 1 background
848 * 0 1 white | 1 1 foreground
849 * 1 0 no change | X 0 no change
850 * 1 1 inverted | 0 1 background
852 * which gives:
853 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
854 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
856 * FIXME: apparently some servers do support 'inverted' color.
857 * I don't know if it's correct per the X spec, but maybe we
858 * ought to take advantage of it. -- AJ
860 XSetFunction( display, gc, GXcopy );
861 XCopyArea( display, pixmapAll, pixmapBits, gc,
862 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
863 XCopyArea( display, pixmapAll, pixmapMask, gc,
864 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
865 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
866 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
867 XSetFunction( display, gc, GXand );
868 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
869 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
870 XSetFunction( display, gc, GXandReverse );
871 XCopyArea( display, pixmapAll, pixmapBits, gc,
872 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
873 XSetFunction( display, gc, GXorReverse );
874 XCopyArea( display, pixmapAll, pixmapMask, gc,
875 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
876 /* Additional white */
877 XSetFunction( display, gc, GXor );
878 XCopyArea( display, pixmapMaskInv, pixmapMask, gc,
879 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
880 XCopyArea( display, pixmapMaskInv, pixmapBits, gc,
881 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
882 XSetFunction( display, gc, GXcopy );
885 else
887 pixmapMask = XCreateBitmapFromData( display, root_window,
888 bitMask32, ptr->nWidth,
889 ptr->nHeight );
890 HeapFree( GetProcessHeap(), 0, bitMask32 );
893 /* Make sure hotspot is valid */
894 hotspot.x = ptr->ptHotSpot.x;
895 hotspot.y = ptr->ptHotSpot.y;
896 if (hotspot.x < 0 || hotspot.x >= ptr->nWidth ||
897 hotspot.y < 0 || hotspot.y >= ptr->nHeight)
899 hotspot.x = ptr->nWidth / 2;
900 hotspot.y = ptr->nHeight / 2;
903 if (pixmapBits && pixmapMask)
904 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
905 &fg, &bg, hotspot.x, hotspot.y );
907 /* Now free everything */
909 if (pixmapAll) XFreePixmap( display, pixmapAll );
910 if (pixmapBits) XFreePixmap( display, pixmapBits );
911 if (pixmapMask) XFreePixmap( display, pixmapMask );
912 if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
913 XFreeGC( display, gc );
915 return cursor;
919 /***********************************************************************
920 * SetCursor (X11DRV.@)
922 void X11DRV_SetCursor( CURSORICONINFO *lpCursor )
924 struct x11drv_thread_data *data = x11drv_thread_data();
925 Cursor cursor;
927 if (lpCursor)
928 TRACE("%ux%u, planes %u, bpp %u\n",
929 lpCursor->nWidth, lpCursor->nHeight, lpCursor->bPlanes, lpCursor->bBitsPerPixel);
930 else
931 TRACE("NULL\n");
933 /* set the same cursor for all top-level windows of the current thread */
935 wine_tsx11_lock();
936 cursor = create_cursor( data->display, lpCursor );
937 if (cursor)
939 if (data->cursor) XFreeCursor( data->display, data->cursor );
940 data->cursor = cursor;
941 if (data->cursor_window)
943 XDefineCursor( data->display, data->cursor_window, cursor );
944 /* Make the change take effect immediately */
945 XFlush( data->display );
948 wine_tsx11_unlock();
951 /***********************************************************************
952 * SetCursorPos (X11DRV.@)
954 BOOL X11DRV_SetCursorPos( INT x, INT y )
956 Display *display = thread_display();
957 POINT pt;
959 TRACE( "warping to (%d,%d)\n", x, y );
961 wine_tsx11_lock();
962 if (cursor_pos.x == x && cursor_pos.y == y)
964 wine_tsx11_unlock();
965 /* We still need to generate WM_MOUSEMOVE */
966 queue_raw_mouse_message( WM_MOUSEMOVE, NULL, x, y, 0, GetCurrentTime(), 0, 0 );
967 return TRUE;
970 pt.x = x; pt.y = y;
971 clip_point_to_rect( &cursor_clip, &pt);
972 XWarpPointer( display, root_window, root_window, 0, 0, 0, 0,
973 pt.x - virtual_screen_rect.left, pt.y - virtual_screen_rect.top );
974 XFlush( display ); /* avoids bad mouse lag in games that do their own mouse warping */
975 cursor_pos = pt;
976 wine_tsx11_unlock();
977 return TRUE;
980 /***********************************************************************
981 * GetCursorPos (X11DRV.@)
983 BOOL X11DRV_GetCursorPos(LPPOINT pos)
985 Display *display = thread_display();
986 Window root, child;
987 int rootX, rootY, winX, winY;
988 unsigned int xstate;
990 wine_tsx11_lock();
991 if ((GetTickCount() - last_time_modified > 100) &&
992 XQueryPointer( display, root_window, &root, &child,
993 &rootX, &rootY, &winX, &winY, &xstate ))
995 update_button_state( xstate );
996 winX += virtual_screen_rect.left;
997 winY += virtual_screen_rect.top;
998 TRACE("pointer at (%d,%d)\n", winX, winY );
999 cursor_pos.x = winX;
1000 cursor_pos.y = winY;
1002 *pos = cursor_pos;
1003 wine_tsx11_unlock();
1004 return TRUE;
1008 /***********************************************************************
1009 * ClipCursor (X11DRV.@)
1011 * Set the cursor clipping rectangle.
1013 BOOL X11DRV_ClipCursor( LPCRECT clip )
1015 if (!IntersectRect( &cursor_clip, &virtual_screen_rect, clip ))
1016 cursor_clip = virtual_screen_rect;
1018 return TRUE;
1021 /***********************************************************************
1022 * X11DRV_ButtonPress
1024 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1026 XButtonEvent *event = &xev->xbutton;
1027 int buttonNum = event->button - 1;
1028 WORD wData = 0;
1029 POINT pt;
1031 if (buttonNum >= NB_BUTTONS) return;
1032 if (!hwnd) return;
1034 switch (buttonNum)
1036 case 3:
1037 wData = WHEEL_DELTA;
1038 break;
1039 case 4:
1040 wData = -WHEEL_DELTA;
1041 break;
1042 case 5:
1043 wData = XBUTTON1;
1044 break;
1045 case 6:
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;
1080 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1082 X11DRV_send_mouse_input( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1083 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1087 /***********************************************************************
1088 * X11DRV_MotionNotify
1090 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1092 XMotionEvent *event = &xev->xmotion;
1093 POINT pt;
1095 TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);
1097 if (!hwnd) return;
1099 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1101 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1102 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1106 /***********************************************************************
1107 * X11DRV_EnterNotify
1109 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1111 XCrossingEvent *event = &xev->xcrossing;
1112 POINT pt;
1114 TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);
1116 if (!hwnd) return;
1117 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1119 /* simulate a mouse motion event */
1120 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1122 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1123 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );