push 0e883ac4a03c91e56787e1ec12e001b6558b4b62
[wine/hacks.git] / dlls / winex11.drv / mouse.c
blob9f51be7db93b30b2add964c49d00cd1cb38cf458
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;
418 int y;
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 and_size = frame->and_width_bytes * frame->height;
426 and_ptr = and_bits = frame->bits;
428 xor_ptr = xor_bits = frame->bits + and_size;
430 image = pXcursorImageCreate( frame->width, frame->height );
431 pixel_ptr = image->pixels;
433 /* Generally 32 bit bitmaps have an alpha channel which is used in favor
434 * of the AND mask. However, if all pixels have alpha = 0x00, the bitmap
435 * is treated like one without alpha and the masks are used. As soon as
436 * one pixel has alpha != 0x00, and the mask ignored as described in the
437 * docs.
439 * This is most likely for applications which create the bitmaps with
440 * CreateDIBitmap, which creates a device dependent bitmap, so the format
441 * that arrives when loading depends on the screen's bpp. Apps that were
442 * written at 8 / 16 bpp times do not know about the 32 bit alpha, so
443 * they would get a completely transparent cursor on 32 bit displays.
445 * Non-32 bit bitmaps always use the AND mask
447 if(frame->bpp == 32)
449 for (y = 0; alpha_zero && y < frame->height; ++y)
451 xor_ptr = xor_bits + (y * frame->xor_width_bytes);
452 for (x = 0; x < frame->width; ++x)
454 if (xor_ptr[3] != 0x00)
456 alpha_zero = FALSE;
457 break;
459 xor_ptr+=4;
464 /* On windows, to calculate the color for a pixel, first an AND is done
465 * with the background and the "and" bitmap, then an XOR with the "xor"
466 * bitmap. This means that when the data in the "and" bitmap is 0, the
467 * pixel will get the color as specified in the "xor" bitmap.
468 * However, if the data in the "and" bitmap is 1, the result will be the
469 * background XOR'ed with the value in the "xor" bitmap. In case the "xor"
470 * data is completely black (0x000000) the pixel will become transparent,
471 * in case it's white (0xffffff) the pixel will become the inverse of the
472 * background color.
474 * Since we can't support inverting colors, we map the grayscale value of
475 * the "xor" data to the alpha channel, and xor the color with either
476 * black or white.
478 for (y = 0; y < frame->height; ++y)
480 and_ptr = and_bits + (y * frame->and_width_bytes);
481 xor_ptr = xor_bits + (y * frame->xor_width_bytes);
483 for (x = 0; x < frame->width; ++x)
485 /* Xcursor pixel data is in ARGB format, with A in the high byte */
486 switch (frame->bpp)
488 case 32:
489 /* BGRA, 8 bits each */
490 *pixel_ptr = *xor_ptr++;
491 *pixel_ptr |= *xor_ptr++ << 8;
492 *pixel_ptr |= *xor_ptr++ << 16;
493 *pixel_ptr |= *xor_ptr++ << 24;
494 break;
496 case 24:
497 /* BGR, 8 bits each */
498 *pixel_ptr = *xor_ptr++;
499 *pixel_ptr |= *xor_ptr++ << 8;
500 *pixel_ptr |= *xor_ptr++ << 16;
501 break;
503 case 16:
504 /* BGR, 5 red, 6 green, 5 blue */
505 *pixel_ptr = *xor_ptr * 0x1f;
506 *pixel_ptr |= (*xor_ptr & 0xe0) << 3;
507 ++xor_ptr;
508 *pixel_ptr |= (*xor_ptr & 0x07) << 11;
509 *pixel_ptr |= (*xor_ptr & 0xf8) << 13;
510 break;
512 case 1:
513 if (*xor_ptr & (1 << (7 - (x & 7)))) *pixel_ptr = 0xffffff;
514 else *pixel_ptr = 0;
515 if ((x & 7) == 7) ++xor_ptr;
516 break;
518 default:
519 FIXME("Currently no support for cursors with %d bits per pixel\n", frame->bpp);
520 return 0;
523 if (alpha_zero)
525 /* Alpha channel */
526 if (~*and_ptr & (1 << (7 - (x & 7)))) *pixel_ptr |= 0xff << 24;
527 else if (*pixel_ptr)
529 int alpha = (*pixel_ptr & 0xff) * 0.30f
530 + ((*pixel_ptr & 0xff00) >> 8) * 0.55f
531 + ((*pixel_ptr & 0xff0000) >> 16) * 0.15f;
532 *pixel_ptr ^= ((x + y) % 2) ? 0xffffff : 0x000000;
533 *pixel_ptr |= alpha << 24;
535 if ((x & 7) == 7) ++and_ptr;
537 ++pixel_ptr;
541 return image;
545 /***********************************************************************
546 * create_xcursor_cursor
548 * Use Xcursor to create an X cursor from a Windows one.
550 static Cursor create_xcursor_cursor( Display *display, const cursor_t *cursor_object )
552 unsigned int i;
553 Cursor cursor;
554 XcursorImage *image;
555 XcursorImages *images;
557 if (!cursor_object) /* Create an empty cursor */
559 image = pXcursorImageCreate( 1, 1 );
560 image->xhot = 0;
561 image->yhot = 0;
562 *(image->pixels) = 0;
563 cursor = pXcursorImageLoadCursor( display, image );
564 pXcursorImageDestroy( image );
566 return cursor;
569 images = pXcursorImagesCreate( cursor_object->num_frames );
570 for (i = 0; i < cursor_object->num_frames; ++i)
572 cursor_frame_t *frame = &cursor_object->frames[i];
573 image = create_cursor_image( frame );
574 if (!image) return 0;
576 /* Make sure hotspot is valid */
577 image->xhot = frame->xhot;
578 image->yhot = frame->yhot;
579 if (image->xhot >= image->width ||
580 image->yhot >= image->height)
582 image->xhot = image->width / 2;
583 image->yhot = image->height / 2;
586 image->delay = cursor_object->delay;
588 images->images[images->nimage++] = image;
591 cursor = pXcursorImagesLoadCursor( display, images );
592 pXcursorImagesDestroy( images );
594 return cursor;
597 #endif /* SONAME_LIBXCURSOR */
600 /***********************************************************************
601 * create_cursor
603 * Create an X cursor from a Windows one.
605 static Cursor create_cursor( Display *display, const cursor_t *ptr )
607 Pixmap pixmapBits, pixmapMask, pixmapMaskInv = 0, pixmapAll;
608 XColor fg, bg;
609 Cursor cursor = None;
610 POINT hotspot;
611 char *bitMask32 = NULL;
613 #ifdef SONAME_LIBXCURSOR
614 if (pXcursorImageLoadCursor) return create_xcursor_cursor( display, ptr );
615 #endif
617 if (!ptr) /* Create an empty cursor */
619 static const char data[] = { 0 };
621 bg.red = bg.green = bg.blue = 0x0000;
622 pixmapBits = XCreateBitmapFromData( display, root_window, data, 1, 1 );
623 if (pixmapBits)
625 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
626 &bg, &bg, 0, 0 );
627 XFreePixmap( display, pixmapBits );
630 else /* Create the X cursor from the bits */
632 cursor_frame_t *frame = &ptr->frames[0];
633 XImage *image;
634 GC gc;
636 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
637 frame->width, frame->height, frame->planes, frame->bpp,
638 frame->xor_width_bytes);
640 /* Create a pixmap and transfer all the bits to it */
642 /* NOTE: Following hack works, but only because XFree depth
643 * 1 images really use 1 bit/pixel (and so the same layout
644 * as the Windows cursor data). Perhaps use a more generic
645 * algorithm here.
647 /* This pixmap will be written with two bitmaps. The first is
648 * the mask and the second is the image.
650 if (!(pixmapAll = XCreatePixmap( display, root_window,
651 frame->width, frame->height * 2, 1 )))
652 return 0;
653 if (!(image = XCreateImage( display, visual,
654 1, ZPixmap, 0, (char *)frame->bits, frame->width,
655 frame->height * 2, 16, frame->xor_width_bytes/frame->bpp)))
657 XFreePixmap( display, pixmapAll );
658 return 0;
660 gc = XCreateGC( display, pixmapAll, 0, NULL );
661 XSetGraphicsExposures( display, gc, False );
662 image->byte_order = MSBFirst;
663 image->bitmap_bit_order = MSBFirst;
664 image->bitmap_unit = 16;
665 _XInitImageFuncPtrs(image);
666 if (frame->planes * frame->bpp == 1)
668 /* A plain old white on black cursor. */
669 fg.red = fg.green = fg.blue = 0xffff;
670 bg.red = bg.green = bg.blue = 0x0000;
671 XPutImage( display, pixmapAll, gc, image,
672 0, 0, 0, 0, frame->width, frame->height * 2 );
674 else
676 int rbits, gbits, bbits, red, green, blue;
677 int rfg, gfg, bfg, rbg, gbg, bbg;
678 int rscale, gscale, bscale;
679 int x, y, xmax, ymax, byteIndex, xorIndex;
680 unsigned char *theMask, *theImage, theChar;
681 int threshold, fgBits, bgBits, bitShifted;
682 BYTE pXorBits[128]; /* Up to 32x32 icons */
684 switch (frame->bpp)
686 case 32:
687 bitMask32 = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
688 frame->width * frame->height / 8 );
689 /* Fallthrough */
690 case 24:
691 rbits = 8;
692 gbits = 8;
693 bbits = 8;
694 threshold = 0x40;
695 break;
696 case 16:
697 rbits = 5;
698 gbits = 6;
699 bbits = 5;
700 threshold = 0x40;
701 break;
702 default:
703 FIXME("Currently no support for cursors with %d bits per pixel\n",
704 frame->bpp);
705 XFreePixmap( display, pixmapAll );
706 XFreeGC( display, gc );
707 image->data = NULL;
708 XDestroyImage( image );
709 return 0;
711 /* The location of the mask. */
712 theMask = frame->bits;
713 /* The mask should still be 1 bit per pixel. The color image
714 * should immediately follow the mask.
716 theImage = &theMask[frame->width/8 * frame->height];
717 rfg = gfg = bfg = rbg = gbg = bbg = 0;
718 byteIndex = 0;
719 xorIndex = 0;
720 fgBits = 0;
721 bitShifted = 0x01;
722 xmax = (frame->width > 32) ? 32 : frame->width;
723 if (frame->width > 32) {
724 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
725 frame->width, frame->height);
727 ymax = (frame->height > 32) ? 32 : frame->height;
729 memset(pXorBits, 0, 128);
730 for (y=0; y<ymax; y++)
732 for (x=0; x<xmax; x++)
734 red = green = blue = 0;
735 switch (frame->bpp)
737 case 32:
738 theChar = theImage[byteIndex++];
739 blue = theChar;
740 theChar = theImage[byteIndex++];
741 green = theChar;
742 theChar = theImage[byteIndex++];
743 red = theChar;
744 theChar = theImage[byteIndex++];
745 /* If the alpha channel is >5% transparent,
746 * assume that we can add it to the bitMask32.
748 if (theChar > 0x0D)
749 *(bitMask32 + (y*xmax+x)/8) |= 1 << (x & 7);
750 break;
751 case 24:
752 theChar = theImage[byteIndex++];
753 blue = theChar;
754 theChar = theImage[byteIndex++];
755 green = theChar;
756 theChar = theImage[byteIndex++];
757 red = theChar;
758 break;
759 case 16:
760 theChar = theImage[byteIndex++];
761 blue = theChar & 0x1F;
762 green = (theChar & 0xE0) >> 5;
763 theChar = theImage[byteIndex++];
764 green |= (theChar & 0x07) << 3;
765 red = (theChar & 0xF8) >> 3;
766 break;
769 if (red+green+blue > threshold)
771 rfg += red;
772 gfg += green;
773 bfg += blue;
774 fgBits++;
775 pXorBits[xorIndex] |= bitShifted;
777 else
779 rbg += red;
780 gbg += green;
781 bbg += blue;
783 if (x%8 == 7)
785 bitShifted = 0x01;
786 xorIndex++;
788 else
789 bitShifted = bitShifted << 1;
792 rscale = 1 << (16 - rbits);
793 gscale = 1 << (16 - gbits);
794 bscale = 1 << (16 - bbits);
795 if (fgBits)
797 fg.red = rfg * rscale / fgBits;
798 fg.green = gfg * gscale / fgBits;
799 fg.blue = bfg * bscale / fgBits;
801 else fg.red = fg.green = fg.blue = 0;
802 bgBits = xmax * ymax - fgBits;
803 if (bgBits)
805 bg.red = rbg * rscale / bgBits;
806 bg.green = gbg * gscale / bgBits;
807 bg.blue = bbg * bscale / bgBits;
809 else bg.red = bg.green = bg.blue = 0;
810 pixmapBits = XCreateBitmapFromData( display, root_window, (char *)pXorBits, xmax, ymax );
811 if (!pixmapBits)
813 HeapFree( GetProcessHeap(), 0, bitMask32 );
814 XFreePixmap( display, pixmapAll );
815 XFreeGC( display, gc );
816 image->data = NULL;
817 XDestroyImage( image );
818 return 0;
821 /* Put the mask. */
822 XPutImage( display, pixmapAll, gc, image,
823 0, 0, 0, 0, frame->width, frame->height );
824 XSetFunction( display, gc, GXcopy );
825 /* Put the image */
826 XCopyArea( display, pixmapBits, pixmapAll, gc,
827 0, 0, xmax, ymax, 0, frame->height );
828 XFreePixmap( display, pixmapBits );
830 image->data = NULL;
831 XDestroyImage( image );
833 /* Now create the 2 pixmaps for bits and mask */
835 pixmapBits = XCreatePixmap( display, root_window, frame->width, frame->height, 1 );
836 if (frame->bpp != 32)
838 pixmapMaskInv = XCreatePixmap( display, root_window, frame->width, frame->height, 1 );
839 pixmapMask = XCreatePixmap( display, root_window, frame->width, frame->height, 1 );
841 /* Make sure everything went OK so far */
842 if (pixmapBits && pixmapMask && pixmapMaskInv)
844 /* We have to do some magic here, as cursors are not fully
845 * compatible between Windows and X11. Under X11, there are
846 * only 3 possible color cursor: black, white and masked. So
847 * we map the 4th Windows color (invert the bits on the screen)
848 * to black and an additional white bit on an other place
849 * (+1,+1). This require some boolean arithmetic:
851 * Windows | X11
852 * And Xor Result | Bits Mask Result
853 * 0 0 black | 0 1 background
854 * 0 1 white | 1 1 foreground
855 * 1 0 no change | X 0 no change
856 * 1 1 inverted | 0 1 background
858 * which gives:
859 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
860 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
862 * FIXME: apparently some servers do support 'inverted' color.
863 * I don't know if it's correct per the X spec, but maybe we
864 * ought to take advantage of it. -- AJ
866 XSetFunction( display, gc, GXcopy );
867 XCopyArea( display, pixmapAll, pixmapBits, gc,
868 0, 0, frame->width, frame->height, 0, 0 );
869 XCopyArea( display, pixmapAll, pixmapMask, gc,
870 0, 0, frame->width, frame->height, 0, 0 );
871 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
872 0, 0, frame->width, frame->height, 0, 0 );
873 XSetFunction( display, gc, GXand );
874 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
875 0, frame->height, frame->width, frame->height, 0, 0 );
876 XSetFunction( display, gc, GXandReverse );
877 XCopyArea( display, pixmapAll, pixmapBits, gc,
878 0, frame->height, frame->width, frame->height, 0, 0 );
879 XSetFunction( display, gc, GXorReverse );
880 XCopyArea( display, pixmapAll, pixmapMask, gc,
881 0, frame->height, frame->width, frame->height, 0, 0 );
882 /* Additional white */
883 XSetFunction( display, gc, GXor );
884 XCopyArea( display, pixmapMaskInv, pixmapMask, gc,
885 0, 0, frame->width, frame->height, 1, 1 );
886 XCopyArea( display, pixmapMaskInv, pixmapBits, gc,
887 0, 0, frame->width, frame->height, 1, 1 );
888 XSetFunction( display, gc, GXcopy );
891 else
893 pixmapMask = XCreateBitmapFromData( display, root_window,
894 bitMask32, frame->width,
895 frame->height );
896 HeapFree( GetProcessHeap(), 0, bitMask32 );
899 /* Make sure hotspot is valid */
900 hotspot.x = frame->xhot;
901 hotspot.y = frame->yhot;
902 if (hotspot.x < 0 || hotspot.x >= frame->width ||
903 hotspot.y < 0 || hotspot.y >= frame->height)
905 hotspot.x = frame->width / 2;
906 hotspot.y = frame->height / 2;
909 if (pixmapBits && pixmapMask)
910 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
911 &fg, &bg, hotspot.x, hotspot.y );
913 /* Now free everything */
915 if (pixmapAll) XFreePixmap( display, pixmapAll );
916 if (pixmapBits) XFreePixmap( display, pixmapBits );
917 if (pixmapMask) XFreePixmap( display, pixmapMask );
918 if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
919 XFreeGC( display, gc );
921 return cursor;
925 /***********************************************************************
926 * SetCursor (X11DRV.@)
928 void X11DRV_SetCursor( const cursor_t *cursor_object )
930 struct x11drv_thread_data *data = x11drv_init_thread_data();
931 Cursor cursor;
933 if (cursor_object)
935 unsigned int i;
936 for (i = 0; i < cursor_object->num_frames; ++i)
938 cursor_frame_t *frame = cursor_object->frames + i;
939 TRACE("frame %u, %ux%u, planes %u, bpp %u\n",
940 i, frame->width, frame->height, frame->planes, frame->bpp);
942 } else TRACE("NULL\n");
944 /* set the same cursor for all top-level windows of the current thread */
946 wine_tsx11_lock();
947 cursor = create_cursor( data->display, cursor_object );
948 if (cursor)
950 if (data->cursor) XFreeCursor( data->display, data->cursor );
951 data->cursor = cursor;
952 if (data->cursor_window)
954 XDefineCursor( data->display, data->cursor_window, cursor );
955 /* Make the change take effect immediately */
956 XFlush( data->display );
959 wine_tsx11_unlock();
962 /***********************************************************************
963 * SetCursorPos (X11DRV.@)
965 BOOL X11DRV_SetCursorPos( INT x, INT y )
967 Display *display = thread_init_display();
968 POINT pt;
970 TRACE( "warping to (%d,%d)\n", x, y );
972 wine_tsx11_lock();
973 if (cursor_pos.x == x && cursor_pos.y == y)
975 wine_tsx11_unlock();
976 /* We still need to generate WM_MOUSEMOVE */
977 queue_raw_mouse_message( WM_MOUSEMOVE, NULL, x, y, 0, GetCurrentTime(), 0, 0 );
978 return TRUE;
981 pt.x = x; pt.y = y;
982 clip_point_to_rect( &cursor_clip, &pt);
983 XWarpPointer( display, root_window, root_window, 0, 0, 0, 0,
984 pt.x - virtual_screen_rect.left, pt.y - virtual_screen_rect.top );
985 XFlush( display ); /* avoids bad mouse lag in games that do their own mouse warping */
986 cursor_pos = pt;
987 wine_tsx11_unlock();
988 return TRUE;
991 /***********************************************************************
992 * GetCursorPos (X11DRV.@)
994 BOOL X11DRV_GetCursorPos(LPPOINT pos)
996 Display *display = thread_init_display();
997 Window root, child;
998 int rootX, rootY, winX, winY;
999 unsigned int xstate;
1001 wine_tsx11_lock();
1002 if ((GetTickCount() - last_time_modified > 100) &&
1003 XQueryPointer( display, root_window, &root, &child,
1004 &rootX, &rootY, &winX, &winY, &xstate ))
1006 update_button_state( xstate );
1007 winX += virtual_screen_rect.left;
1008 winY += virtual_screen_rect.top;
1009 TRACE("pointer at (%d,%d)\n", winX, winY );
1010 cursor_pos.x = winX;
1011 cursor_pos.y = winY;
1013 *pos = cursor_pos;
1014 wine_tsx11_unlock();
1015 return TRUE;
1019 /***********************************************************************
1020 * ClipCursor (X11DRV.@)
1022 * Set the cursor clipping rectangle.
1024 BOOL X11DRV_ClipCursor( LPCRECT clip )
1026 if (!IntersectRect( &cursor_clip, &virtual_screen_rect, clip ))
1027 cursor_clip = virtual_screen_rect;
1029 return TRUE;
1032 /***********************************************************************
1033 * X11DRV_ButtonPress
1035 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1037 XButtonEvent *event = &xev->xbutton;
1038 int buttonNum = event->button - 1;
1039 WORD wData = 0;
1040 POINT pt;
1042 if (buttonNum >= NB_BUTTONS) return;
1043 if (!hwnd) return;
1045 switch (buttonNum)
1047 case 3:
1048 wData = WHEEL_DELTA;
1049 break;
1050 case 4:
1051 wData = -WHEEL_DELTA;
1052 break;
1053 case 5:
1054 wData = XBUTTON1;
1055 break;
1056 case 6:
1057 wData = XBUTTON2;
1058 break;
1059 case 7:
1060 wData = XBUTTON1;
1061 break;
1062 case 8:
1063 wData = XBUTTON2;
1064 break;
1067 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1069 X11DRV_send_mouse_input( hwnd, button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1070 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1074 /***********************************************************************
1075 * X11DRV_ButtonRelease
1077 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1079 XButtonEvent *event = &xev->xbutton;
1080 int buttonNum = event->button - 1;
1081 WORD wData = 0;
1082 POINT pt;
1084 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1085 if (!hwnd) return;
1087 switch (buttonNum)
1089 case 5:
1090 wData = XBUTTON1;
1091 break;
1092 case 6:
1093 wData = XBUTTON2;
1094 break;
1095 case 7:
1096 wData = XBUTTON1;
1097 break;
1098 case 8:
1099 wData = XBUTTON2;
1100 break;
1103 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1105 X11DRV_send_mouse_input( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1106 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1110 /***********************************************************************
1111 * X11DRV_MotionNotify
1113 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1115 XMotionEvent *event = &xev->xmotion;
1116 POINT pt;
1118 TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);
1120 if (!hwnd) return;
1122 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1124 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1125 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1129 /***********************************************************************
1130 * X11DRV_EnterNotify
1132 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1134 XCrossingEvent *event = &xev->xcrossing;
1135 POINT pt;
1137 TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);
1139 if (!hwnd) return;
1140 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1141 if (event->window == x11drv_thread_data()->grab_window) return;
1143 /* simulate a mouse motion event */
1144 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1146 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1147 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );