push 014043c4937c940c54cd1214c96e33a3b3c8cf7d
[wine/hacks.git] / dlls / winex11.drv / mouse.c
blob8ee27683a886036774997fa2aa121fe824b28678
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 7 /* 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,
73 MOUSEEVENTF_XDOWN
76 static const UINT button_up_flags[NB_BUTTONS] =
78 MOUSEEVENTF_LEFTUP,
79 MOUSEEVENTF_MIDDLEUP,
80 MOUSEEVENTF_RIGHTUP,
83 MOUSEEVENTF_XUP,
84 MOUSEEVENTF_XUP
87 POINT cursor_pos;
88 static DWORD last_time_modified;
89 static RECT cursor_clip; /* Cursor clipping rect */
91 BOOL X11DRV_SetCursorPos( INT x, INT y );
94 /***********************************************************************
95 * X11DRV_Xcursor_Init
97 * Load the Xcursor library for use.
99 void X11DRV_Xcursor_Init(void)
101 #ifdef SONAME_LIBXCURSOR
102 xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
103 if (!xcursor_handle) /* wine_dlopen failed. */
105 WARN("Xcursor failed to load. Using fallback code.\n");
106 return;
108 #define LOAD_FUNCPTR(f) \
109 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
111 LOAD_FUNCPTR(XcursorImageCreate);
112 LOAD_FUNCPTR(XcursorImageDestroy);
113 LOAD_FUNCPTR(XcursorImageLoadCursor);
114 LOAD_FUNCPTR(XcursorImagesCreate);
115 LOAD_FUNCPTR(XcursorImagesDestroy);
116 LOAD_FUNCPTR(XcursorImagesLoadCursor);
117 #undef LOAD_FUNCPTR
118 #endif /* SONAME_LIBXCURSOR */
122 /***********************************************************************
123 * get_coords
125 * get the coordinates of a mouse event
127 static inline void get_coords( HWND hwnd, Window window, int x, int y, POINT *pt )
129 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
131 if (!data) return;
133 if (window == data->client_window)
135 pt->x = x + data->client_rect.left;
136 pt->y = y + data->client_rect.top;
138 else
140 pt->x = x + data->whole_rect.left;
141 pt->y = y + data->whole_rect.top;
145 /***********************************************************************
146 * clip_point_to_rect
148 * Clip point to the provided rectangle
150 static inline void clip_point_to_rect( LPCRECT rect, LPPOINT pt )
152 if (pt->x < rect->left) pt->x = rect->left;
153 else if (pt->x >= rect->right) pt->x = rect->right - 1;
154 if (pt->y < rect->top) pt->y = rect->top;
155 else if (pt->y >= rect->bottom) pt->y = rect->bottom - 1;
158 /***********************************************************************
159 * update_button_state
161 * Update the button state with what X provides us
163 static inline void update_button_state( unsigned int state )
165 key_state_table[VK_LBUTTON] = (state & Button1Mask ? 0x80 : 0);
166 key_state_table[VK_MBUTTON] = (state & Button2Mask ? 0x80 : 0);
167 key_state_table[VK_RBUTTON] = (state & Button3Mask ? 0x80 : 0);
168 /* X-buttons are not reported from XQueryPointer */
172 /***********************************************************************
173 * update_mouse_state
175 * Update the various window states on a mouse event.
177 static void update_mouse_state( HWND hwnd, Window window, int x, int y, unsigned int state, POINT *pt )
179 struct x11drv_thread_data *data = x11drv_thread_data();
181 if (window == root_window)
183 x += virtual_screen_rect.left;
184 y += virtual_screen_rect.top;
186 get_coords( hwnd, window, x, y, pt );
188 /* update the cursor */
190 if (data->cursor_window != window)
192 data->cursor_window = window;
193 wine_tsx11_lock();
194 if (data->cursor) XDefineCursor( data->display, window, data->cursor );
195 wine_tsx11_unlock();
198 /* update the wine server Z-order */
200 if (window != data->grab_window &&
201 /* ignore event if a button is pressed, since the mouse is then grabbed too */
202 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
204 SERVER_START_REQ( update_window_zorder )
206 req->window = hwnd;
207 req->rect.left = pt->x;
208 req->rect.top = pt->y;
209 req->rect.right = pt->x + 1;
210 req->rect.bottom = pt->y + 1;
211 wine_server_call( req );
213 SERVER_END_REQ;
218 /***********************************************************************
219 * get_key_state
221 static WORD get_key_state(void)
223 WORD ret = 0;
225 if (GetSystemMetrics( SM_SWAPBUTTON ))
227 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
228 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
230 else
232 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
233 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
235 if (key_state_table[VK_MBUTTON] & 0x80) ret |= MK_MBUTTON;
236 if (key_state_table[VK_SHIFT] & 0x80) ret |= MK_SHIFT;
237 if (key_state_table[VK_CONTROL] & 0x80) ret |= MK_CONTROL;
238 if (key_state_table[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
239 if (key_state_table[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
240 return ret;
244 /***********************************************************************
245 * queue_raw_mouse_message
247 static void queue_raw_mouse_message( UINT message, HWND hwnd, DWORD x, DWORD y,
248 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
250 MSLLHOOKSTRUCT hook;
252 hook.pt.x = x;
253 hook.pt.y = y;
254 hook.mouseData = MAKELONG( 0, data );
255 hook.flags = injected_flags;
256 hook.time = time;
257 hook.dwExtraInfo = extra_info;
259 last_time_modified = GetTickCount();
260 if (HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE )) return;
262 SERVER_START_REQ( send_hardware_message )
264 req->id = (injected_flags & LLMHF_INJECTED) ? 0 : GetCurrentThreadId();
265 req->win = hwnd;
266 req->msg = message;
267 req->wparam = MAKEWPARAM( get_key_state(), data );
268 req->lparam = 0;
269 req->x = x;
270 req->y = y;
271 req->time = time;
272 req->info = extra_info;
273 wine_server_call( req );
275 SERVER_END_REQ;
280 /***********************************************************************
281 * X11DRV_send_mouse_input
283 void X11DRV_send_mouse_input( HWND hwnd, DWORD flags, DWORD x, DWORD y,
284 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
286 POINT pt;
288 if (flags & MOUSEEVENTF_MOVE && flags & MOUSEEVENTF_ABSOLUTE)
290 if (injected_flags & LLMHF_INJECTED)
292 pt.x = (x * screen_width) >> 16;
293 pt.y = (y * screen_height) >> 16;
295 else
297 pt.x = x;
298 pt.y = y;
299 wine_tsx11_lock();
300 if (cursor_pos.x == x && cursor_pos.y == y &&
301 (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE)))
302 flags &= ~MOUSEEVENTF_MOVE;
303 wine_tsx11_unlock();
306 else if (flags & MOUSEEVENTF_MOVE)
308 int accel[3], xMult = 1, yMult = 1;
310 /* dx and dy can be negative numbers for relative movements */
311 SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
313 if (abs(x) > accel[0] && accel[2] != 0)
315 xMult = 2;
316 if ((abs(x) > accel[1]) && (accel[2] == 2)) xMult = 4;
318 if (abs(y) > accel[0] && accel[2] != 0)
320 yMult = 2;
321 if ((abs(y) > accel[1]) && (accel[2] == 2)) yMult = 4;
324 wine_tsx11_lock();
325 pt.x = cursor_pos.x + (long)x * xMult;
326 pt.y = cursor_pos.y + (long)y * yMult;
327 wine_tsx11_unlock();
329 else
331 wine_tsx11_lock();
332 pt = cursor_pos;
333 wine_tsx11_unlock();
336 if (flags & MOUSEEVENTF_MOVE)
338 queue_raw_mouse_message( WM_MOUSEMOVE, hwnd, pt.x, pt.y, data, time,
339 extra_info, injected_flags );
340 if ((injected_flags & LLMHF_INJECTED) &&
341 ((flags & MOUSEEVENTF_ABSOLUTE) || x || y)) /* we have to actually move the cursor */
343 X11DRV_SetCursorPos( pt.x, pt.y );
345 else
347 wine_tsx11_lock();
348 clip_point_to_rect( &cursor_clip, &pt);
349 cursor_pos = pt;
350 wine_tsx11_unlock();
353 if (flags & MOUSEEVENTF_LEFTDOWN)
355 key_state_table[VK_LBUTTON] |= 0xc0;
356 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
357 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
359 if (flags & MOUSEEVENTF_LEFTUP)
361 key_state_table[VK_LBUTTON] &= ~0x80;
362 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONUP : WM_LBUTTONUP,
363 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
365 if (flags & MOUSEEVENTF_RIGHTDOWN)
367 key_state_table[VK_RBUTTON] |= 0xc0;
368 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
369 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
371 if (flags & MOUSEEVENTF_RIGHTUP)
373 key_state_table[VK_RBUTTON] &= ~0x80;
374 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONUP : WM_RBUTTONUP,
375 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
377 if (flags & MOUSEEVENTF_MIDDLEDOWN)
379 key_state_table[VK_MBUTTON] |= 0xc0;
380 queue_raw_mouse_message( WM_MBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
381 extra_info, injected_flags );
383 if (flags & MOUSEEVENTF_MIDDLEUP)
385 key_state_table[VK_MBUTTON] &= ~0x80;
386 queue_raw_mouse_message( WM_MBUTTONUP, hwnd, pt.x, pt.y, data, time,
387 extra_info, injected_flags );
389 if (flags & MOUSEEVENTF_WHEEL)
391 queue_raw_mouse_message( WM_MOUSEWHEEL, hwnd, pt.x, pt.y, data, time,
392 extra_info, injected_flags );
394 if (flags & MOUSEEVENTF_XDOWN)
396 key_state_table[VK_XBUTTON1 + data - 1] |= 0xc0;
397 queue_raw_mouse_message( WM_XBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
398 extra_info, injected_flags );
400 if (flags & MOUSEEVENTF_XUP)
402 key_state_table[VK_XBUTTON1 + data - 1] &= ~0x80;
403 queue_raw_mouse_message( WM_XBUTTONUP, hwnd, pt.x, pt.y, data, time,
404 extra_info, injected_flags );
409 #ifdef SONAME_LIBXCURSOR
411 /***********************************************************************
412 * create_cursor_image
414 * Create an XcursorImage from a cursor_frame_t
416 static XcursorImage *create_cursor_image( cursor_frame_t *frame )
418 int x, xmax;
419 int y, ymax;
420 int and_size, xor_size;
421 unsigned char *and_bits, *and_ptr, *xor_bits, *xor_ptr;
422 XcursorPixel *pixel_ptr;
423 XcursorImage *image;
424 BOOL alpha_zero = TRUE;
426 ymax = (frame->height > 32) ? 32 : frame->height;
427 xmax = (frame->width > 32) ? 32 : frame->width;
429 and_size = frame->and_width_bytes * frame->height;
430 and_ptr = and_bits = frame->bits;
432 xor_size = frame->xor_width_bytes * frame->height;
433 xor_ptr = xor_bits = frame->bits + and_size;
435 image = pXcursorImageCreate( xmax, ymax );
436 pixel_ptr = image->pixels;
438 /* Generally 32 bit bitmaps have an alpha channel which is used in favor
439 * of the AND mask. However, if all pixels have alpha = 0x00, the bitmap
440 * is treated like one without alpha and the masks are used. As soon as
441 * one pixel has alpha != 0x00, and the mask ignored as described in the
442 * docs.
444 * This is most likely for applications which create the bitmaps with
445 * CreateDIBitmap, which creates a device dependent bitmap, so the format
446 * that arrives when loading depends on the screen's bpp. Apps that were
447 * written at 8 / 16 bpp times do not know about the 32 bit alpha, so
448 * they would get a completely transparent cursor on 32 bit displays.
450 * Non-32 bit bitmaps always use the AND mask
452 if(frame->bpp == 32)
454 for (y = 0; alpha_zero && y < ymax; ++y)
456 xor_ptr = xor_bits + (y * frame->xor_width_bytes);
457 for (x = 0; x < xmax; ++x)
459 if (xor_ptr[3] != 0x00)
461 alpha_zero = FALSE;
462 break;
464 xor_ptr+=4;
469 /* On windows, to calculate the color for a pixel, first an AND is done
470 * with the background and the "and" bitmap, then an XOR with the "xor"
471 * bitmap. This means that when the data in the "and" bitmap is 0, the
472 * pixel will get the color as specified in the "xor" bitmap.
473 * However, if the data in the "and" bitmap is 1, the result will be the
474 * background XOR'ed with the value in the "xor" bitmap. In case the "xor"
475 * data is completely black (0x000000) the pixel will become transparent,
476 * in case it's white (0xffffff) the pixel will become the inverse of the
477 * background color.
479 * Since we can't support inverting colors, we map the grayscale value of
480 * the "xor" data to the alpha channel, and xor the color with either
481 * black or white.
483 for (y = 0; y < ymax; ++y)
485 and_ptr = and_bits + (y * frame->and_width_bytes);
486 xor_ptr = xor_bits + (y * frame->xor_width_bytes);
488 for (x = 0; x < xmax; ++x)
490 /* Xcursor pixel data is in ARGB format, with A in the high byte */
491 switch (frame->bpp)
493 case 32:
494 /* BGRA, 8 bits each */
495 *pixel_ptr = *xor_ptr++;
496 *pixel_ptr |= *xor_ptr++ << 8;
497 *pixel_ptr |= *xor_ptr++ << 16;
498 *pixel_ptr |= *xor_ptr++ << 24;
499 break;
501 case 24:
502 /* BGR, 8 bits each */
503 *pixel_ptr = *xor_ptr++;
504 *pixel_ptr |= *xor_ptr++ << 8;
505 *pixel_ptr |= *xor_ptr++ << 16;
506 break;
508 case 16:
509 /* BGR, 5 red, 6 green, 5 blue */
510 *pixel_ptr = *xor_ptr * 0x1f;
511 *pixel_ptr |= (*xor_ptr & 0xe0) << 3;
512 ++xor_ptr;
513 *pixel_ptr |= (*xor_ptr & 0x07) << 11;
514 *pixel_ptr |= (*xor_ptr & 0xf8) << 13;
515 break;
517 case 1:
518 if (*xor_ptr & (1 << (7 - (x & 7)))) *pixel_ptr = 0xffffff;
519 else *pixel_ptr = 0;
520 if ((x & 7) == 7) ++xor_ptr;
521 break;
523 default:
524 FIXME("Currently no support for cursors with %d bits per pixel\n", frame->bpp);
525 return 0;
528 if (alpha_zero)
530 /* Alpha channel */
531 if (~*and_ptr & (1 << (7 - (x & 7)))) *pixel_ptr |= 0xff << 24;
532 else if (*pixel_ptr)
534 int alpha = (*pixel_ptr & 0xff) * 0.30f
535 + ((*pixel_ptr & 0xff00) >> 8) * 0.55f
536 + ((*pixel_ptr & 0xff0000) >> 16) * 0.15f;
537 *pixel_ptr ^= ((x + y) % 2) ? 0xffffff : 0x000000;
538 *pixel_ptr |= alpha << 24;
540 if ((x & 7) == 7) ++and_ptr;
542 ++pixel_ptr;
546 return image;
550 /***********************************************************************
551 * create_xcursor_cursor
553 * Use Xcursor to create an X cursor from a Windows one.
555 static Cursor create_xcursor_cursor( Display *display, const cursor_t *cursor_object )
557 unsigned int i;
558 Cursor cursor;
559 XcursorImage *image;
560 XcursorImages *images;
562 if (!cursor_object) /* Create an empty cursor */
564 image = pXcursorImageCreate( 1, 1 );
565 image->xhot = 0;
566 image->yhot = 0;
567 *(image->pixels) = 0;
568 cursor = pXcursorImageLoadCursor( display, image );
569 pXcursorImageDestroy( image );
571 return cursor;
574 images = pXcursorImagesCreate( cursor_object->num_frames );
575 for (i = 0; i < cursor_object->num_frames; ++i)
577 cursor_frame_t *frame = &cursor_object->frames[i];
578 image = create_cursor_image( frame );
579 if (!image) return 0;
581 /* Make sure hotspot is valid */
582 image->xhot = frame->xhot;
583 image->yhot = frame->yhot;
584 if (image->xhot >= image->width ||
585 image->yhot >= image->height)
587 image->xhot = image->width / 2;
588 image->yhot = image->height / 2;
591 image->delay = cursor_object->delay;
593 images->images[images->nimage++] = image;
596 cursor = pXcursorImagesLoadCursor( display, images );
597 pXcursorImagesDestroy( images );
599 return cursor;
602 #endif /* SONAME_LIBXCURSOR */
605 /***********************************************************************
606 * create_cursor
608 * Create an X cursor from a Windows one.
610 static Cursor create_cursor( Display *display, const cursor_t *ptr )
612 Pixmap pixmapBits, pixmapMask, pixmapMaskInv = 0, pixmapAll;
613 XColor fg, bg;
614 Cursor cursor = None;
615 POINT hotspot;
616 char *bitMask32 = NULL;
618 #ifdef SONAME_LIBXCURSOR
619 if (pXcursorImageLoadCursor) return create_xcursor_cursor( display, ptr );
620 #endif
622 if (!ptr) /* Create an empty cursor */
624 static const char data[] = { 0 };
626 bg.red = bg.green = bg.blue = 0x0000;
627 pixmapBits = XCreateBitmapFromData( display, root_window, data, 1, 1 );
628 if (pixmapBits)
630 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
631 &bg, &bg, 0, 0 );
632 XFreePixmap( display, pixmapBits );
635 else /* Create the X cursor from the bits */
637 cursor_frame_t *frame = &ptr->frames[0];
638 XImage *image;
639 GC gc;
641 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
642 frame->width, frame->height, frame->planes, frame->bpp,
643 frame->xor_width_bytes);
645 /* Create a pixmap and transfer all the bits to it */
647 /* NOTE: Following hack works, but only because XFree depth
648 * 1 images really use 1 bit/pixel (and so the same layout
649 * as the Windows cursor data). Perhaps use a more generic
650 * algorithm here.
652 /* This pixmap will be written with two bitmaps. The first is
653 * the mask and the second is the image.
655 if (!(pixmapAll = XCreatePixmap( display, root_window,
656 frame->width, frame->height * 2, 1 )))
657 return 0;
658 if (!(image = XCreateImage( display, visual,
659 1, ZPixmap, 0, (char *)frame->bits, frame->width,
660 frame->height * 2, 16, frame->xor_width_bytes/frame->bpp)))
662 XFreePixmap( display, pixmapAll );
663 return 0;
665 gc = XCreateGC( display, pixmapAll, 0, NULL );
666 XSetGraphicsExposures( display, gc, False );
667 image->byte_order = MSBFirst;
668 image->bitmap_bit_order = MSBFirst;
669 image->bitmap_unit = 16;
670 _XInitImageFuncPtrs(image);
671 if (frame->planes * frame->bpp == 1)
673 /* A plain old white on black cursor. */
674 fg.red = fg.green = fg.blue = 0xffff;
675 bg.red = bg.green = bg.blue = 0x0000;
676 XPutImage( display, pixmapAll, gc, image,
677 0, 0, 0, 0, frame->width, frame->height * 2 );
679 else
681 int rbits, gbits, bbits, red, green, blue;
682 int rfg, gfg, bfg, rbg, gbg, bbg;
683 int rscale, gscale, bscale;
684 int x, y, xmax, ymax, bitIndex, byteIndex, xorIndex;
685 unsigned char *theMask, *theImage, theChar;
686 int threshold, fgBits, bgBits, bitShifted;
687 BYTE pXorBits[128]; /* Up to 32x32 icons */
689 switch (frame->bpp)
691 case 32:
692 bitMask32 = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
693 frame->width * frame->height / 8 );
694 /* Fallthrough */
695 case 24:
696 rbits = 8;
697 gbits = 8;
698 bbits = 8;
699 threshold = 0x40;
700 break;
701 case 16:
702 rbits = 5;
703 gbits = 6;
704 bbits = 5;
705 threshold = 0x40;
706 break;
707 default:
708 FIXME("Currently no support for cursors with %d bits per pixel\n",
709 frame->bpp);
710 XFreePixmap( display, pixmapAll );
711 XFreeGC( display, gc );
712 image->data = NULL;
713 XDestroyImage( image );
714 return 0;
716 /* The location of the mask. */
717 theMask = frame->bits;
718 /* The mask should still be 1 bit per pixel. The color image
719 * should immediately follow the mask.
721 theImage = &theMask[frame->width/8 * frame->height];
722 rfg = gfg = bfg = rbg = gbg = bbg = 0;
723 bitIndex = 0;
724 byteIndex = 0;
725 xorIndex = 0;
726 fgBits = 0;
727 bitShifted = 0x01;
728 xmax = (frame->width > 32) ? 32 : frame->width;
729 if (frame->width > 32) {
730 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
731 frame->width, frame->height);
733 ymax = (frame->height > 32) ? 32 : frame->height;
735 memset(pXorBits, 0, 128);
736 for (y=0; y<ymax; y++)
738 for (x=0; x<xmax; x++)
740 red = green = blue = 0;
741 switch (frame->bpp)
743 case 32:
744 theChar = theImage[byteIndex++];
745 blue = theChar;
746 theChar = theImage[byteIndex++];
747 green = theChar;
748 theChar = theImage[byteIndex++];
749 red = theChar;
750 theChar = theImage[byteIndex++];
751 /* If the alpha channel is >5% transparent,
752 * assume that we can add it to the bitMask32.
754 if (theChar > 0x0D)
755 *(bitMask32 + (y*xmax+x)/8) |= 1 << (x & 7);
756 break;
757 case 24:
758 theChar = theImage[byteIndex++];
759 blue = theChar;
760 theChar = theImage[byteIndex++];
761 green = theChar;
762 theChar = theImage[byteIndex++];
763 red = theChar;
764 break;
765 case 16:
766 theChar = theImage[byteIndex++];
767 blue = theChar & 0x1F;
768 green = (theChar & 0xE0) >> 5;
769 theChar = theImage[byteIndex++];
770 green |= (theChar & 0x07) << 3;
771 red = (theChar & 0xF8) >> 3;
772 break;
775 if (red+green+blue > threshold)
777 rfg += red;
778 gfg += green;
779 bfg += blue;
780 fgBits++;
781 pXorBits[xorIndex] |= bitShifted;
783 else
785 rbg += red;
786 gbg += green;
787 bbg += blue;
789 if (x%8 == 7)
791 bitShifted = 0x01;
792 xorIndex++;
794 else
795 bitShifted = bitShifted << 1;
798 rscale = 1 << (16 - rbits);
799 gscale = 1 << (16 - gbits);
800 bscale = 1 << (16 - bbits);
801 if (fgBits)
803 fg.red = rfg * rscale / fgBits;
804 fg.green = gfg * gscale / fgBits;
805 fg.blue = bfg * bscale / fgBits;
807 else fg.red = fg.green = fg.blue = 0;
808 bgBits = xmax * ymax - fgBits;
809 if (bgBits)
811 bg.red = rbg * rscale / bgBits;
812 bg.green = gbg * gscale / bgBits;
813 bg.blue = bbg * bscale / bgBits;
815 else bg.red = bg.green = bg.blue = 0;
816 pixmapBits = XCreateBitmapFromData( display, root_window, (char *)pXorBits, xmax, ymax );
817 if (!pixmapBits)
819 HeapFree( GetProcessHeap(), 0, bitMask32 );
820 XFreePixmap( display, pixmapAll );
821 XFreeGC( display, gc );
822 image->data = NULL;
823 XDestroyImage( image );
824 return 0;
827 /* Put the mask. */
828 XPutImage( display, pixmapAll, gc, image,
829 0, 0, 0, 0, frame->width, frame->height );
830 XSetFunction( display, gc, GXcopy );
831 /* Put the image */
832 XCopyArea( display, pixmapBits, pixmapAll, gc,
833 0, 0, xmax, ymax, 0, frame->height );
834 XFreePixmap( display, pixmapBits );
836 image->data = NULL;
837 XDestroyImage( image );
839 /* Now create the 2 pixmaps for bits and mask */
841 pixmapBits = XCreatePixmap( display, root_window, frame->width, frame->height, 1 );
842 if (frame->bpp != 32)
844 pixmapMaskInv = XCreatePixmap( display, root_window, frame->width, frame->height, 1 );
845 pixmapMask = XCreatePixmap( display, root_window, frame->width, frame->height, 1 );
847 /* Make sure everything went OK so far */
848 if (pixmapBits && pixmapMask && pixmapMaskInv)
850 /* We have to do some magic here, as cursors are not fully
851 * compatible between Windows and X11. Under X11, there are
852 * only 3 possible color cursor: black, white and masked. So
853 * we map the 4th Windows color (invert the bits on the screen)
854 * to black and an additional white bit on an other place
855 * (+1,+1). This require some boolean arithmetic:
857 * Windows | X11
858 * And Xor Result | Bits Mask Result
859 * 0 0 black | 0 1 background
860 * 0 1 white | 1 1 foreground
861 * 1 0 no change | X 0 no change
862 * 1 1 inverted | 0 1 background
864 * which gives:
865 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
866 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
868 * FIXME: apparently some servers do support 'inverted' color.
869 * I don't know if it's correct per the X spec, but maybe we
870 * ought to take advantage of it. -- AJ
872 XSetFunction( display, gc, GXcopy );
873 XCopyArea( display, pixmapAll, pixmapBits, gc,
874 0, 0, frame->width, frame->height, 0, 0 );
875 XCopyArea( display, pixmapAll, pixmapMask, gc,
876 0, 0, frame->width, frame->height, 0, 0 );
877 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
878 0, 0, frame->width, frame->height, 0, 0 );
879 XSetFunction( display, gc, GXand );
880 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
881 0, frame->height, frame->width, frame->height, 0, 0 );
882 XSetFunction( display, gc, GXandReverse );
883 XCopyArea( display, pixmapAll, pixmapBits, gc,
884 0, frame->height, frame->width, frame->height, 0, 0 );
885 XSetFunction( display, gc, GXorReverse );
886 XCopyArea( display, pixmapAll, pixmapMask, gc,
887 0, frame->height, frame->width, frame->height, 0, 0 );
888 /* Additional white */
889 XSetFunction( display, gc, GXor );
890 XCopyArea( display, pixmapMaskInv, pixmapMask, gc,
891 0, 0, frame->width, frame->height, 1, 1 );
892 XCopyArea( display, pixmapMaskInv, pixmapBits, gc,
893 0, 0, frame->width, frame->height, 1, 1 );
894 XSetFunction( display, gc, GXcopy );
897 else
899 pixmapMask = XCreateBitmapFromData( display, root_window,
900 bitMask32, frame->width,
901 frame->height );
902 HeapFree( GetProcessHeap(), 0, bitMask32 );
905 /* Make sure hotspot is valid */
906 hotspot.x = frame->xhot;
907 hotspot.y = frame->yhot;
908 if (hotspot.x < 0 || hotspot.x >= frame->width ||
909 hotspot.y < 0 || hotspot.y >= frame->height)
911 hotspot.x = frame->width / 2;
912 hotspot.y = frame->height / 2;
915 if (pixmapBits && pixmapMask)
916 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
917 &fg, &bg, hotspot.x, hotspot.y );
919 /* Now free everything */
921 if (pixmapAll) XFreePixmap( display, pixmapAll );
922 if (pixmapBits) XFreePixmap( display, pixmapBits );
923 if (pixmapMask) XFreePixmap( display, pixmapMask );
924 if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
925 XFreeGC( display, gc );
927 return cursor;
931 /***********************************************************************
932 * SetCursor (X11DRV.@)
934 void X11DRV_SetCursor( const cursor_t *cursor_object )
936 struct x11drv_thread_data *data = x11drv_thread_data();
937 Cursor cursor;
939 if (cursor_object)
941 unsigned int i;
942 for (i = 0; i < cursor_object->num_frames; ++i)
944 cursor_frame_t *frame = cursor_object->frames + i;
945 TRACE("frame %u, %ux%u, planes %u, bpp %u\n",
946 i, frame->width, frame->height, frame->planes, frame->bpp);
948 } else TRACE("NULL\n");
950 /* set the same cursor for all top-level windows of the current thread */
952 wine_tsx11_lock();
953 cursor = create_cursor( data->display, cursor_object );
954 if (cursor)
956 if (data->cursor) XFreeCursor( data->display, data->cursor );
957 data->cursor = cursor;
958 if (data->cursor_window)
960 XDefineCursor( data->display, data->cursor_window, cursor );
961 /* Make the change take effect immediately */
962 XFlush( data->display );
965 wine_tsx11_unlock();
968 /***********************************************************************
969 * SetCursorPos (X11DRV.@)
971 BOOL X11DRV_SetCursorPos( INT x, INT y )
973 Display *display = thread_display();
974 POINT pt;
976 TRACE( "warping to (%d,%d)\n", x, y );
978 wine_tsx11_lock();
979 if (cursor_pos.x == x && cursor_pos.y == y)
981 wine_tsx11_unlock();
982 /* We still need to generate WM_MOUSEMOVE */
983 queue_raw_mouse_message( WM_MOUSEMOVE, NULL, x, y, 0, GetCurrentTime(), 0, 0 );
984 return TRUE;
987 pt.x = x; pt.y = y;
988 clip_point_to_rect( &cursor_clip, &pt);
989 XWarpPointer( display, root_window, root_window, 0, 0, 0, 0,
990 pt.x - virtual_screen_rect.left, pt.y - virtual_screen_rect.top );
991 XFlush( display ); /* avoids bad mouse lag in games that do their own mouse warping */
992 cursor_pos = pt;
993 wine_tsx11_unlock();
994 return TRUE;
997 /***********************************************************************
998 * GetCursorPos (X11DRV.@)
1000 BOOL X11DRV_GetCursorPos(LPPOINT pos)
1002 Display *display = thread_display();
1003 Window root, child;
1004 int rootX, rootY, winX, winY;
1005 unsigned int xstate;
1007 wine_tsx11_lock();
1008 if ((GetTickCount() - last_time_modified > 100) &&
1009 XQueryPointer( display, root_window, &root, &child,
1010 &rootX, &rootY, &winX, &winY, &xstate ))
1012 update_button_state( xstate );
1013 winX += virtual_screen_rect.left;
1014 winY += virtual_screen_rect.top;
1015 TRACE("pointer at (%d,%d)\n", winX, winY );
1016 cursor_pos.x = winX;
1017 cursor_pos.y = winY;
1019 *pos = cursor_pos;
1020 wine_tsx11_unlock();
1021 return TRUE;
1025 /***********************************************************************
1026 * ClipCursor (X11DRV.@)
1028 * Set the cursor clipping rectangle.
1030 BOOL X11DRV_ClipCursor( LPCRECT clip )
1032 if (!IntersectRect( &cursor_clip, &virtual_screen_rect, clip ))
1033 cursor_clip = virtual_screen_rect;
1035 return TRUE;
1038 /***********************************************************************
1039 * X11DRV_ButtonPress
1041 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1043 XButtonEvent *event = &xev->xbutton;
1044 int buttonNum = event->button - 1;
1045 WORD wData = 0;
1046 POINT pt;
1048 if (buttonNum >= NB_BUTTONS) return;
1049 if (!hwnd) return;
1051 switch (buttonNum)
1053 case 3:
1054 wData = WHEEL_DELTA;
1055 break;
1056 case 4:
1057 wData = -WHEEL_DELTA;
1058 break;
1059 case 5:
1060 wData = XBUTTON1;
1061 break;
1062 case 6:
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;
1097 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1099 X11DRV_send_mouse_input( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1100 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1104 /***********************************************************************
1105 * X11DRV_MotionNotify
1107 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1109 XMotionEvent *event = &xev->xmotion;
1110 POINT pt;
1112 TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);
1114 if (!hwnd) return;
1116 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1118 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1119 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1123 /***********************************************************************
1124 * X11DRV_EnterNotify
1126 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1128 XCrossingEvent *event = &xev->xcrossing;
1129 POINT pt;
1131 TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);
1133 if (!hwnd) return;
1134 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1136 /* simulate a mouse motion event */
1137 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1139 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1140 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );