push d2de4cbe4a771190295df18d4241bdf1a9d79e4b
[wine/hacks.git] / dlls / winex11.drv / mouse.c
blob0c1082af99bab57439924d3c62faf6e1220cb4da
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 MAKE_FUNCPTR(XcursorImagesCreate);
36 MAKE_FUNCPTR(XcursorImagesDestroy);
37 MAKE_FUNCPTR(XcursorImagesLoadCursor);
38 # undef MAKE_FUNCPTR
39 #endif /* SONAME_LIBXCURSOR */
41 #define NONAMELESSUNION
42 #define NONAMELESSSTRUCT
43 #include "windef.h"
44 #include "winbase.h"
45 #include "wine/winuser16.h"
47 #include "x11drv.h"
48 #include "wine/server.h"
49 #include "wine/library.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
54 /**********************************************************************/
56 #ifndef Button6Mask
57 #define Button6Mask (1<<13)
58 #endif
59 #ifndef Button7Mask
60 #define Button7Mask (1<<14)
61 #endif
63 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
65 static const UINT button_down_flags[NB_BUTTONS] =
67 MOUSEEVENTF_LEFTDOWN,
68 MOUSEEVENTF_MIDDLEDOWN,
69 MOUSEEVENTF_RIGHTDOWN,
70 MOUSEEVENTF_WHEEL,
71 MOUSEEVENTF_WHEEL,
72 MOUSEEVENTF_XDOWN, /* FIXME: horizontal wheel */
73 MOUSEEVENTF_XDOWN,
74 MOUSEEVENTF_XDOWN,
75 MOUSEEVENTF_XDOWN
78 static const UINT button_up_flags[NB_BUTTONS] =
80 MOUSEEVENTF_LEFTUP,
81 MOUSEEVENTF_MIDDLEUP,
82 MOUSEEVENTF_RIGHTUP,
85 MOUSEEVENTF_XUP,
86 MOUSEEVENTF_XUP,
87 MOUSEEVENTF_XUP,
88 MOUSEEVENTF_XUP
91 POINT cursor_pos;
92 static DWORD last_time_modified;
93 static RECT cursor_clip; /* Cursor clipping rect */
95 BOOL X11DRV_SetCursorPos( INT x, INT y );
98 /***********************************************************************
99 * X11DRV_Xcursor_Init
101 * Load the Xcursor library for use.
103 void X11DRV_Xcursor_Init(void)
105 #ifdef SONAME_LIBXCURSOR
106 xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
107 if (!xcursor_handle) /* wine_dlopen failed. */
109 WARN("Xcursor failed to load. Using fallback code.\n");
110 return;
112 #define LOAD_FUNCPTR(f) \
113 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
115 LOAD_FUNCPTR(XcursorImageCreate);
116 LOAD_FUNCPTR(XcursorImageDestroy);
117 LOAD_FUNCPTR(XcursorImageLoadCursor);
118 LOAD_FUNCPTR(XcursorImagesCreate);
119 LOAD_FUNCPTR(XcursorImagesDestroy);
120 LOAD_FUNCPTR(XcursorImagesLoadCursor);
121 #undef LOAD_FUNCPTR
122 #endif /* SONAME_LIBXCURSOR */
126 /***********************************************************************
127 * get_coords
129 * get the coordinates of a mouse event
131 static inline void get_coords( HWND hwnd, Window window, int x, int y, POINT *pt )
133 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
135 if (!data) return;
137 if (window == data->client_window)
139 pt->x = x + data->client_rect.left;
140 pt->y = y + data->client_rect.top;
142 else
144 pt->x = x + data->whole_rect.left;
145 pt->y = y + data->whole_rect.top;
149 /***********************************************************************
150 * clip_point_to_rect
152 * Clip point to the provided rectangle
154 static inline void clip_point_to_rect( LPCRECT rect, LPPOINT pt )
156 if (pt->x < rect->left) pt->x = rect->left;
157 else if (pt->x >= rect->right) pt->x = rect->right - 1;
158 if (pt->y < rect->top) pt->y = rect->top;
159 else if (pt->y >= rect->bottom) pt->y = rect->bottom - 1;
162 /***********************************************************************
163 * update_button_state
165 * Update the button state with what X provides us
167 static inline void update_button_state( unsigned int state )
169 key_state_table[VK_LBUTTON] = (state & Button1Mask ? 0x80 : 0);
170 key_state_table[VK_MBUTTON] = (state & Button2Mask ? 0x80 : 0);
171 key_state_table[VK_RBUTTON] = (state & Button3Mask ? 0x80 : 0);
172 /* X-buttons are not reported from XQueryPointer */
176 /***********************************************************************
177 * update_mouse_state
179 * Update the various window states on a mouse event.
181 static void update_mouse_state( HWND hwnd, Window window, int x, int y, unsigned int state, POINT *pt )
183 struct x11drv_thread_data *data = x11drv_thread_data();
185 get_coords( hwnd, window, 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;
217 /***********************************************************************
218 * get_key_state
220 static WORD get_key_state(void)
222 WORD ret = 0;
224 if (GetSystemMetrics( SM_SWAPBUTTON ))
226 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
227 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
229 else
231 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
232 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
234 if (key_state_table[VK_MBUTTON] & 0x80) ret |= MK_MBUTTON;
235 if (key_state_table[VK_SHIFT] & 0x80) ret |= MK_SHIFT;
236 if (key_state_table[VK_CONTROL] & 0x80) ret |= MK_CONTROL;
237 if (key_state_table[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
238 if (key_state_table[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
239 return ret;
243 /***********************************************************************
244 * queue_raw_mouse_message
246 static void queue_raw_mouse_message( UINT message, HWND hwnd, DWORD x, DWORD y,
247 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
249 MSLLHOOKSTRUCT hook;
251 hook.pt.x = x;
252 hook.pt.y = y;
253 hook.mouseData = MAKELONG( 0, data );
254 hook.flags = injected_flags;
255 hook.time = time;
256 hook.dwExtraInfo = extra_info;
258 last_time_modified = GetTickCount();
259 if (HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE )) return;
261 SERVER_START_REQ( send_hardware_message )
263 req->id = (injected_flags & LLMHF_INJECTED) ? 0 : GetCurrentThreadId();
264 req->win = hwnd;
265 req->msg = message;
266 req->wparam = MAKEWPARAM( get_key_state(), data );
267 req->lparam = 0;
268 req->x = x;
269 req->y = y;
270 req->time = time;
271 req->info = extra_info;
272 wine_server_call( req );
274 SERVER_END_REQ;
279 /***********************************************************************
280 * X11DRV_send_mouse_input
282 void X11DRV_send_mouse_input( HWND hwnd, DWORD flags, DWORD x, DWORD y,
283 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
285 POINT pt;
287 if (flags & MOUSEEVENTF_MOVE && flags & MOUSEEVENTF_ABSOLUTE)
289 if (injected_flags & LLMHF_INJECTED)
291 pt.x = (x * screen_width) >> 16;
292 pt.y = (y * screen_height) >> 16;
294 else
296 pt.x = x;
297 pt.y = y;
298 wine_tsx11_lock();
299 if (cursor_pos.x == x && cursor_pos.y == y &&
300 (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE)))
301 flags &= ~MOUSEEVENTF_MOVE;
302 wine_tsx11_unlock();
305 else if (flags & MOUSEEVENTF_MOVE)
307 int accel[3], xMult = 1, yMult = 1;
309 /* dx and dy can be negative numbers for relative movements */
310 SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
312 if (abs(x) > accel[0] && accel[2] != 0)
314 xMult = 2;
315 if ((abs(x) > accel[1]) && (accel[2] == 2)) xMult = 4;
317 if (abs(y) > accel[0] && accel[2] != 0)
319 yMult = 2;
320 if ((abs(y) > accel[1]) && (accel[2] == 2)) yMult = 4;
323 wine_tsx11_lock();
324 pt.x = cursor_pos.x + (long)x * xMult;
325 pt.y = cursor_pos.y + (long)y * yMult;
326 wine_tsx11_unlock();
328 else
330 wine_tsx11_lock();
331 pt = cursor_pos;
332 wine_tsx11_unlock();
335 if (flags & MOUSEEVENTF_MOVE)
337 queue_raw_mouse_message( WM_MOUSEMOVE, hwnd, pt.x, pt.y, data, time,
338 extra_info, injected_flags );
339 if ((injected_flags & LLMHF_INJECTED) &&
340 ((flags & MOUSEEVENTF_ABSOLUTE) || x || y)) /* we have to actually move the cursor */
342 X11DRV_SetCursorPos( pt.x, pt.y );
344 else
346 wine_tsx11_lock();
347 clip_point_to_rect( &cursor_clip, &pt);
348 cursor_pos = pt;
349 wine_tsx11_unlock();
352 if (flags & MOUSEEVENTF_LEFTDOWN)
354 key_state_table[VK_LBUTTON] |= 0xc0;
355 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
356 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
358 if (flags & MOUSEEVENTF_LEFTUP)
360 key_state_table[VK_LBUTTON] &= ~0x80;
361 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONUP : WM_LBUTTONUP,
362 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
364 if (flags & MOUSEEVENTF_RIGHTDOWN)
366 key_state_table[VK_RBUTTON] |= 0xc0;
367 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
368 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
370 if (flags & MOUSEEVENTF_RIGHTUP)
372 key_state_table[VK_RBUTTON] &= ~0x80;
373 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONUP : WM_RBUTTONUP,
374 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
376 if (flags & MOUSEEVENTF_MIDDLEDOWN)
378 key_state_table[VK_MBUTTON] |= 0xc0;
379 queue_raw_mouse_message( WM_MBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
380 extra_info, injected_flags );
382 if (flags & MOUSEEVENTF_MIDDLEUP)
384 key_state_table[VK_MBUTTON] &= ~0x80;
385 queue_raw_mouse_message( WM_MBUTTONUP, hwnd, pt.x, pt.y, data, time,
386 extra_info, injected_flags );
388 if (flags & MOUSEEVENTF_WHEEL)
390 queue_raw_mouse_message( WM_MOUSEWHEEL, hwnd, pt.x, pt.y, data, time,
391 extra_info, injected_flags );
393 if (flags & MOUSEEVENTF_XDOWN)
395 key_state_table[VK_XBUTTON1 + data - 1] |= 0xc0;
396 queue_raw_mouse_message( WM_XBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
397 extra_info, injected_flags );
399 if (flags & MOUSEEVENTF_XUP)
401 key_state_table[VK_XBUTTON1 + data - 1] &= ~0x80;
402 queue_raw_mouse_message( WM_XBUTTONUP, hwnd, pt.x, pt.y, data, time,
403 extra_info, injected_flags );
408 #ifdef SONAME_LIBXCURSOR
410 /***********************************************************************
411 * create_cursor_image
413 * Create an XcursorImage from a cursor_frame_t
415 static XcursorImage *create_cursor_image( cursor_frame_t *frame )
417 int x, xmax;
418 int y, ymax;
419 int and_size;
420 unsigned char *and_bits, *and_ptr, *xor_bits, *xor_ptr;
421 XcursorPixel *pixel_ptr;
422 XcursorImage *image;
423 BOOL alpha_zero = TRUE;
425 ymax = (frame->height > 32) ? 32 : frame->height;
426 xmax = (frame->width > 32) ? 32 : frame->width;
428 and_size = frame->and_width_bytes * frame->height;
429 and_ptr = and_bits = frame->bits;
431 xor_ptr = xor_bits = frame->bits + 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(frame->bpp == 32)
452 for (y = 0; alpha_zero && y < ymax; ++y)
454 xor_ptr = xor_bits + (y * frame->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 * frame->and_width_bytes);
484 xor_ptr = xor_bits + (y * frame->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 (frame->bpp)
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", frame->bpp);
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, const cursor_t *cursor_object )
555 unsigned int i;
556 Cursor cursor;
557 XcursorImage *image;
558 XcursorImages *images;
560 if (!cursor_object) /* Create an empty cursor */
562 image = pXcursorImageCreate( 1, 1 );
563 image->xhot = 0;
564 image->yhot = 0;
565 *(image->pixels) = 0;
566 cursor = pXcursorImageLoadCursor( display, image );
567 pXcursorImageDestroy( image );
569 return cursor;
572 images = pXcursorImagesCreate( cursor_object->num_frames );
573 for (i = 0; i < cursor_object->num_frames; ++i)
575 cursor_frame_t *frame = &cursor_object->frames[i];
576 image = create_cursor_image( frame );
577 if (!image) return 0;
579 /* Make sure hotspot is valid */
580 image->xhot = frame->xhot;
581 image->yhot = frame->yhot;
582 if (image->xhot >= image->width ||
583 image->yhot >= image->height)
585 image->xhot = image->width / 2;
586 image->yhot = image->height / 2;
589 image->delay = cursor_object->delay;
591 images->images[images->nimage++] = image;
594 cursor = pXcursorImagesLoadCursor( display, images );
595 pXcursorImagesDestroy( images );
597 return cursor;
600 #endif /* SONAME_LIBXCURSOR */
603 /***********************************************************************
604 * create_cursor
606 * Create an X cursor from a Windows one.
608 static Cursor create_cursor( Display *display, const cursor_t *ptr )
610 Pixmap pixmapBits, pixmapMask, pixmapMaskInv = 0, pixmapAll;
611 XColor fg, bg;
612 Cursor cursor = None;
613 POINT hotspot;
614 char *bitMask32 = NULL;
616 #ifdef SONAME_LIBXCURSOR
617 if (pXcursorImageLoadCursor) return create_xcursor_cursor( display, ptr );
618 #endif
620 if (!ptr) /* Create an empty cursor */
622 static const char data[] = { 0 };
624 bg.red = bg.green = bg.blue = 0x0000;
625 pixmapBits = XCreateBitmapFromData( display, root_window, data, 1, 1 );
626 if (pixmapBits)
628 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
629 &bg, &bg, 0, 0 );
630 XFreePixmap( display, pixmapBits );
633 else /* Create the X cursor from the bits */
635 cursor_frame_t *frame = &ptr->frames[0];
636 XImage *image;
637 GC gc;
639 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
640 frame->width, frame->height, frame->planes, frame->bpp,
641 frame->xor_width_bytes);
643 /* Create a pixmap and transfer all the bits to it */
645 /* NOTE: Following hack works, but only because XFree depth
646 * 1 images really use 1 bit/pixel (and so the same layout
647 * as the Windows cursor data). Perhaps use a more generic
648 * algorithm here.
650 /* This pixmap will be written with two bitmaps. The first is
651 * the mask and the second is the image.
653 if (!(pixmapAll = XCreatePixmap( display, root_window,
654 frame->width, frame->height * 2, 1 )))
655 return 0;
656 if (!(image = XCreateImage( display, visual,
657 1, ZPixmap, 0, (char *)frame->bits, frame->width,
658 frame->height * 2, 16, frame->xor_width_bytes/frame->bpp)))
660 XFreePixmap( display, pixmapAll );
661 return 0;
663 gc = XCreateGC( display, pixmapAll, 0, NULL );
664 XSetGraphicsExposures( display, gc, False );
665 image->byte_order = MSBFirst;
666 image->bitmap_bit_order = MSBFirst;
667 image->bitmap_unit = 16;
668 _XInitImageFuncPtrs(image);
669 if (frame->planes * frame->bpp == 1)
671 /* A plain old white on black cursor. */
672 fg.red = fg.green = fg.blue = 0xffff;
673 bg.red = bg.green = bg.blue = 0x0000;
674 XPutImage( display, pixmapAll, gc, image,
675 0, 0, 0, 0, frame->width, frame->height * 2 );
677 else
679 int rbits, gbits, bbits, red, green, blue;
680 int rfg, gfg, bfg, rbg, gbg, bbg;
681 int rscale, gscale, bscale;
682 int x, y, xmax, ymax, byteIndex, xorIndex;
683 unsigned char *theMask, *theImage, theChar;
684 int threshold, fgBits, bgBits, bitShifted;
685 BYTE pXorBits[128]; /* Up to 32x32 icons */
687 switch (frame->bpp)
689 case 32:
690 bitMask32 = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
691 frame->width * frame->height / 8 );
692 /* Fallthrough */
693 case 24:
694 rbits = 8;
695 gbits = 8;
696 bbits = 8;
697 threshold = 0x40;
698 break;
699 case 16:
700 rbits = 5;
701 gbits = 6;
702 bbits = 5;
703 threshold = 0x40;
704 break;
705 default:
706 FIXME("Currently no support for cursors with %d bits per pixel\n",
707 frame->bpp);
708 XFreePixmap( display, pixmapAll );
709 XFreeGC( display, gc );
710 image->data = NULL;
711 XDestroyImage( image );
712 return 0;
714 /* The location of the mask. */
715 theMask = frame->bits;
716 /* The mask should still be 1 bit per pixel. The color image
717 * should immediately follow the mask.
719 theImage = &theMask[frame->width/8 * frame->height];
720 rfg = gfg = bfg = rbg = gbg = bbg = 0;
721 byteIndex = 0;
722 xorIndex = 0;
723 fgBits = 0;
724 bitShifted = 0x01;
725 xmax = (frame->width > 32) ? 32 : frame->width;
726 if (frame->width > 32) {
727 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
728 frame->width, frame->height);
730 ymax = (frame->height > 32) ? 32 : frame->height;
732 memset(pXorBits, 0, 128);
733 for (y=0; y<ymax; y++)
735 for (x=0; x<xmax; x++)
737 red = green = blue = 0;
738 switch (frame->bpp)
740 case 32:
741 theChar = theImage[byteIndex++];
742 blue = theChar;
743 theChar = theImage[byteIndex++];
744 green = theChar;
745 theChar = theImage[byteIndex++];
746 red = theChar;
747 theChar = theImage[byteIndex++];
748 /* If the alpha channel is >5% transparent,
749 * assume that we can add it to the bitMask32.
751 if (theChar > 0x0D)
752 *(bitMask32 + (y*xmax+x)/8) |= 1 << (x & 7);
753 break;
754 case 24:
755 theChar = theImage[byteIndex++];
756 blue = theChar;
757 theChar = theImage[byteIndex++];
758 green = theChar;
759 theChar = theImage[byteIndex++];
760 red = theChar;
761 break;
762 case 16:
763 theChar = theImage[byteIndex++];
764 blue = theChar & 0x1F;
765 green = (theChar & 0xE0) >> 5;
766 theChar = theImage[byteIndex++];
767 green |= (theChar & 0x07) << 3;
768 red = (theChar & 0xF8) >> 3;
769 break;
772 if (red+green+blue > threshold)
774 rfg += red;
775 gfg += green;
776 bfg += blue;
777 fgBits++;
778 pXorBits[xorIndex] |= bitShifted;
780 else
782 rbg += red;
783 gbg += green;
784 bbg += blue;
786 if (x%8 == 7)
788 bitShifted = 0x01;
789 xorIndex++;
791 else
792 bitShifted = bitShifted << 1;
795 rscale = 1 << (16 - rbits);
796 gscale = 1 << (16 - gbits);
797 bscale = 1 << (16 - bbits);
798 if (fgBits)
800 fg.red = rfg * rscale / fgBits;
801 fg.green = gfg * gscale / fgBits;
802 fg.blue = bfg * bscale / fgBits;
804 else fg.red = fg.green = fg.blue = 0;
805 bgBits = xmax * ymax - fgBits;
806 if (bgBits)
808 bg.red = rbg * rscale / bgBits;
809 bg.green = gbg * gscale / bgBits;
810 bg.blue = bbg * bscale / bgBits;
812 else bg.red = bg.green = bg.blue = 0;
813 pixmapBits = XCreateBitmapFromData( display, root_window, (char *)pXorBits, xmax, ymax );
814 if (!pixmapBits)
816 HeapFree( GetProcessHeap(), 0, bitMask32 );
817 XFreePixmap( display, pixmapAll );
818 XFreeGC( display, gc );
819 image->data = NULL;
820 XDestroyImage( image );
821 return 0;
824 /* Put the mask. */
825 XPutImage( display, pixmapAll, gc, image,
826 0, 0, 0, 0, frame->width, frame->height );
827 XSetFunction( display, gc, GXcopy );
828 /* Put the image */
829 XCopyArea( display, pixmapBits, pixmapAll, gc,
830 0, 0, xmax, ymax, 0, frame->height );
831 XFreePixmap( display, pixmapBits );
833 image->data = NULL;
834 XDestroyImage( image );
836 /* Now create the 2 pixmaps for bits and mask */
838 pixmapBits = XCreatePixmap( display, root_window, frame->width, frame->height, 1 );
839 if (frame->bpp != 32)
841 pixmapMaskInv = XCreatePixmap( display, root_window, frame->width, frame->height, 1 );
842 pixmapMask = XCreatePixmap( display, root_window, frame->width, frame->height, 1 );
844 /* Make sure everything went OK so far */
845 if (pixmapBits && pixmapMask && pixmapMaskInv)
847 /* We have to do some magic here, as cursors are not fully
848 * compatible between Windows and X11. Under X11, there are
849 * only 3 possible color cursor: black, white and masked. So
850 * we map the 4th Windows color (invert the bits on the screen)
851 * to black and an additional white bit on an other place
852 * (+1,+1). This require some boolean arithmetic:
854 * Windows | X11
855 * And Xor Result | Bits Mask Result
856 * 0 0 black | 0 1 background
857 * 0 1 white | 1 1 foreground
858 * 1 0 no change | X 0 no change
859 * 1 1 inverted | 0 1 background
861 * which gives:
862 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
863 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
865 * FIXME: apparently some servers do support 'inverted' color.
866 * I don't know if it's correct per the X spec, but maybe we
867 * ought to take advantage of it. -- AJ
869 XSetFunction( display, gc, GXcopy );
870 XCopyArea( display, pixmapAll, pixmapBits, gc,
871 0, 0, frame->width, frame->height, 0, 0 );
872 XCopyArea( display, pixmapAll, pixmapMask, gc,
873 0, 0, frame->width, frame->height, 0, 0 );
874 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
875 0, 0, frame->width, frame->height, 0, 0 );
876 XSetFunction( display, gc, GXand );
877 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
878 0, frame->height, frame->width, frame->height, 0, 0 );
879 XSetFunction( display, gc, GXandReverse );
880 XCopyArea( display, pixmapAll, pixmapBits, gc,
881 0, frame->height, frame->width, frame->height, 0, 0 );
882 XSetFunction( display, gc, GXorReverse );
883 XCopyArea( display, pixmapAll, pixmapMask, gc,
884 0, frame->height, frame->width, frame->height, 0, 0 );
885 /* Additional white */
886 XSetFunction( display, gc, GXor );
887 XCopyArea( display, pixmapMaskInv, pixmapMask, gc,
888 0, 0, frame->width, frame->height, 1, 1 );
889 XCopyArea( display, pixmapMaskInv, pixmapBits, gc,
890 0, 0, frame->width, frame->height, 1, 1 );
891 XSetFunction( display, gc, GXcopy );
894 else
896 pixmapMask = XCreateBitmapFromData( display, root_window,
897 bitMask32, frame->width,
898 frame->height );
899 HeapFree( GetProcessHeap(), 0, bitMask32 );
902 /* Make sure hotspot is valid */
903 hotspot.x = frame->xhot;
904 hotspot.y = frame->yhot;
905 if (hotspot.x < 0 || hotspot.x >= frame->width ||
906 hotspot.y < 0 || hotspot.y >= frame->height)
908 hotspot.x = frame->width / 2;
909 hotspot.y = frame->height / 2;
912 if (pixmapBits && pixmapMask)
913 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
914 &fg, &bg, hotspot.x, hotspot.y );
916 /* Now free everything */
918 if (pixmapAll) XFreePixmap( display, pixmapAll );
919 if (pixmapBits) XFreePixmap( display, pixmapBits );
920 if (pixmapMask) XFreePixmap( display, pixmapMask );
921 if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
922 XFreeGC( display, gc );
924 return cursor;
928 /***********************************************************************
929 * SetCursor (X11DRV.@)
931 void X11DRV_SetCursor( const cursor_t *cursor_object )
933 struct x11drv_thread_data *data = x11drv_init_thread_data();
934 Cursor cursor;
936 if (cursor_object)
938 unsigned int i;
939 for (i = 0; i < cursor_object->num_frames; ++i)
941 cursor_frame_t *frame = cursor_object->frames + i;
942 TRACE("frame %u, %ux%u, planes %u, bpp %u\n",
943 i, frame->width, frame->height, frame->planes, frame->bpp);
945 } else TRACE("NULL\n");
947 /* set the same cursor for all top-level windows of the current thread */
949 wine_tsx11_lock();
950 cursor = create_cursor( data->display, cursor_object );
951 if (cursor)
953 if (data->cursor) XFreeCursor( data->display, data->cursor );
954 data->cursor = cursor;
955 if (data->cursor_window)
957 XDefineCursor( data->display, data->cursor_window, cursor );
958 /* Make the change take effect immediately */
959 XFlush( data->display );
962 wine_tsx11_unlock();
965 /***********************************************************************
966 * SetCursorPos (X11DRV.@)
968 BOOL X11DRV_SetCursorPos( INT x, INT y )
970 Display *display = thread_init_display();
971 POINT pt;
973 TRACE( "warping to (%d,%d)\n", x, y );
975 wine_tsx11_lock();
976 if (cursor_pos.x == x && cursor_pos.y == y)
978 wine_tsx11_unlock();
979 /* We still need to generate WM_MOUSEMOVE */
980 queue_raw_mouse_message( WM_MOUSEMOVE, NULL, x, y, 0, GetCurrentTime(), 0, 0 );
981 return TRUE;
984 pt.x = x; pt.y = y;
985 clip_point_to_rect( &cursor_clip, &pt);
986 XWarpPointer( display, root_window, root_window, 0, 0, 0, 0,
987 pt.x - virtual_screen_rect.left, pt.y - virtual_screen_rect.top );
988 XFlush( display ); /* avoids bad mouse lag in games that do their own mouse warping */
989 cursor_pos = pt;
990 wine_tsx11_unlock();
991 return TRUE;
994 /***********************************************************************
995 * GetCursorPos (X11DRV.@)
997 BOOL X11DRV_GetCursorPos(LPPOINT pos)
999 Display *display = thread_init_display();
1000 Window root, child;
1001 int rootX, rootY, winX, winY;
1002 unsigned int xstate;
1004 wine_tsx11_lock();
1005 if ((GetTickCount() - last_time_modified > 100) &&
1006 XQueryPointer( display, root_window, &root, &child,
1007 &rootX, &rootY, &winX, &winY, &xstate ))
1009 update_button_state( xstate );
1010 winX += virtual_screen_rect.left;
1011 winY += virtual_screen_rect.top;
1012 TRACE("pointer at (%d,%d)\n", winX, winY );
1013 cursor_pos.x = winX;
1014 cursor_pos.y = winY;
1016 *pos = cursor_pos;
1017 wine_tsx11_unlock();
1018 return TRUE;
1022 /***********************************************************************
1023 * ClipCursor (X11DRV.@)
1025 * Set the cursor clipping rectangle.
1027 BOOL X11DRV_ClipCursor( LPCRECT clip )
1029 if (!IntersectRect( &cursor_clip, &virtual_screen_rect, clip ))
1030 cursor_clip = virtual_screen_rect;
1032 return TRUE;
1035 /***********************************************************************
1036 * X11DRV_ButtonPress
1038 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1040 XButtonEvent *event = &xev->xbutton;
1041 int buttonNum = event->button - 1;
1042 WORD wData = 0;
1043 POINT pt;
1045 if (buttonNum >= NB_BUTTONS) return;
1046 if (!hwnd) return;
1048 switch (buttonNum)
1050 case 3:
1051 wData = WHEEL_DELTA;
1052 break;
1053 case 4:
1054 wData = -WHEEL_DELTA;
1055 break;
1056 case 5:
1057 wData = XBUTTON1;
1058 break;
1059 case 6:
1060 wData = XBUTTON2;
1061 break;
1062 case 7:
1063 wData = XBUTTON1;
1064 break;
1065 case 8:
1066 wData = XBUTTON2;
1067 break;
1070 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1072 X11DRV_send_mouse_input( hwnd, button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1073 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1077 /***********************************************************************
1078 * X11DRV_ButtonRelease
1080 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1082 XButtonEvent *event = &xev->xbutton;
1083 int buttonNum = event->button - 1;
1084 WORD wData = 0;
1085 POINT pt;
1087 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1088 if (!hwnd) return;
1090 switch (buttonNum)
1092 case 5:
1093 wData = XBUTTON1;
1094 break;
1095 case 6:
1096 wData = XBUTTON2;
1097 break;
1098 case 7:
1099 wData = XBUTTON1;
1100 break;
1101 case 8:
1102 wData = XBUTTON2;
1103 break;
1106 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1108 X11DRV_send_mouse_input( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1109 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1113 /***********************************************************************
1114 * X11DRV_MotionNotify
1116 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1118 XMotionEvent *event = &xev->xmotion;
1119 POINT pt;
1121 TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);
1123 if (!hwnd) return;
1125 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1127 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1128 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1132 /***********************************************************************
1133 * X11DRV_EnterNotify
1135 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1137 XCrossingEvent *event = &xev->xcrossing;
1138 POINT pt;
1140 TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);
1142 if (!hwnd) return;
1143 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1144 if (event->window == x11drv_thread_data()->grab_window) return;
1146 /* simulate a mouse motion event */
1147 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1149 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1150 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );