mshtml: Added nsIURI::Equals implementation on URIs without necko interface associated.
[wine.git] / dlls / winex11.drv / mouse.c
blob189ca7b779733923715b8870602235936e0f4f8a
1 /*
2 * X11 mouse driver
4 * Copyright 1998 Ulrich Weigand
5 * Copyright 2007 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <X11/Xlib.h>
26 #include <stdarg.h>
28 #ifdef SONAME_LIBXCURSOR
29 # include <X11/Xcursor/Xcursor.h>
30 static void *xcursor_handle;
31 # define MAKE_FUNCPTR(f) static typeof(f) * p##f
32 MAKE_FUNCPTR(XcursorImageCreate);
33 MAKE_FUNCPTR(XcursorImageDestroy);
34 MAKE_FUNCPTR(XcursorImageLoadCursor);
35 # undef MAKE_FUNCPTR
36 #endif /* SONAME_LIBXCURSOR */
38 #define NONAMELESSUNION
39 #define NONAMELESSSTRUCT
40 #include "windef.h"
41 #include "winbase.h"
42 #include "wine/winuser16.h"
44 #include "win.h"
45 #include "x11drv.h"
46 #include "wine/server.h"
47 #include "wine/library.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
52 /**********************************************************************/
54 #ifndef Button6Mask
55 #define Button6Mask (1<<13)
56 #endif
57 #ifndef Button7Mask
58 #define Button7Mask (1<<14)
59 #endif
61 #define NB_BUTTONS 7 /* Windows can handle 5 buttons and the wheel too */
63 static const UINT button_down_flags[NB_BUTTONS] =
65 MOUSEEVENTF_LEFTDOWN,
66 MOUSEEVENTF_MIDDLEDOWN,
67 MOUSEEVENTF_RIGHTDOWN,
68 MOUSEEVENTF_WHEEL,
69 MOUSEEVENTF_WHEEL,
70 MOUSEEVENTF_XDOWN,
71 MOUSEEVENTF_XDOWN
74 static const UINT button_up_flags[NB_BUTTONS] =
76 MOUSEEVENTF_LEFTUP,
77 MOUSEEVENTF_MIDDLEUP,
78 MOUSEEVENTF_RIGHTUP,
81 MOUSEEVENTF_XUP,
82 MOUSEEVENTF_XUP
85 POINT cursor_pos;
86 static DWORD last_time_modified;
87 static RECT cursor_clip; /* Cursor clipping rect */
89 BOOL X11DRV_SetCursorPos( INT x, INT y );
92 /***********************************************************************
93 * X11DRV_Xcursor_Init
95 * Load the Xcursor library for use.
97 void X11DRV_Xcursor_Init(void)
99 #ifdef SONAME_LIBXCURSOR
100 xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
101 if (!xcursor_handle) /* wine_dlopen failed. */
103 WARN("Xcursor failed to load. Using fallback code.\n");
104 return;
106 #define LOAD_FUNCPTR(f) \
107 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
109 LOAD_FUNCPTR(XcursorImageCreate);
110 LOAD_FUNCPTR(XcursorImageDestroy);
111 LOAD_FUNCPTR(XcursorImageLoadCursor);
112 #undef LOAD_FUNCPTR
113 #endif /* SONAME_LIBXCURSOR */
117 /***********************************************************************
118 * get_coords
120 * get the coordinates of a mouse event
122 static inline void get_coords( HWND hwnd, Window window, int x, int y, POINT *pt )
124 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
126 if (!data) return;
128 if (window == data->client_window)
130 pt->x = x + data->client_rect.left;
131 pt->y = y + data->client_rect.top;
133 else
135 pt->x = x + data->whole_rect.left;
136 pt->y = y + data->whole_rect.top;
140 /***********************************************************************
141 * clip_point_to_rect
143 * Clip point to the provided rectangle
145 static inline void clip_point_to_rect( LPCRECT rect, LPPOINT pt )
147 if (pt->x < rect->left) pt->x = rect->left;
148 else if (pt->x >= rect->right) pt->x = rect->right - 1;
149 if (pt->y < rect->top) pt->y = rect->top;
150 else if (pt->y >= rect->bottom) pt->y = rect->bottom - 1;
153 /***********************************************************************
154 * update_button_state
156 * Update the button state with what X provides us
158 static inline void update_button_state( unsigned int state )
160 key_state_table[VK_LBUTTON] = (state & Button1Mask ? 0x80 : 0);
161 key_state_table[VK_MBUTTON] = (state & Button2Mask ? 0x80 : 0);
162 key_state_table[VK_RBUTTON] = (state & Button3Mask ? 0x80 : 0);
163 /* X-buttons are not reported from XQueryPointer */
167 /***********************************************************************
168 * update_mouse_state
170 * Update the various window states on a mouse event.
172 static void update_mouse_state( HWND hwnd, Window window, int x, int y, unsigned int state, POINT *pt )
174 struct x11drv_thread_data *data = x11drv_thread_data();
176 if (window == root_window)
178 x += virtual_screen_rect.left;
179 y += virtual_screen_rect.top;
181 get_coords( hwnd, window, x, y, pt );
183 /* update the cursor */
185 if (data->cursor_window != window)
187 data->cursor_window = window;
188 wine_tsx11_lock();
189 if (data->cursor) XDefineCursor( data->display, window, data->cursor );
190 wine_tsx11_unlock();
193 /* update the wine server Z-order */
195 if (window != data->grab_window &&
196 /* ignore event if a button is pressed, since the mouse is then grabbed too */
197 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
199 SERVER_START_REQ( update_window_zorder )
201 req->window = hwnd;
202 req->rect.left = pt->x;
203 req->rect.top = pt->y;
204 req->rect.right = pt->x + 1;
205 req->rect.bottom = pt->y + 1;
206 wine_server_call( req );
208 SERVER_END_REQ;
213 /***********************************************************************
214 * get_key_state
216 static WORD get_key_state(void)
218 WORD ret = 0;
220 if (GetSystemMetrics( SM_SWAPBUTTON ))
222 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
223 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
225 else
227 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
228 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
230 if (key_state_table[VK_MBUTTON] & 0x80) ret |= MK_MBUTTON;
231 if (key_state_table[VK_SHIFT] & 0x80) ret |= MK_SHIFT;
232 if (key_state_table[VK_CONTROL] & 0x80) ret |= MK_CONTROL;
233 if (key_state_table[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
234 if (key_state_table[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
235 return ret;
239 /***********************************************************************
240 * queue_raw_mouse_message
242 static void queue_raw_mouse_message( UINT message, HWND hwnd, DWORD x, DWORD y,
243 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
245 MSLLHOOKSTRUCT hook;
247 hook.pt.x = x;
248 hook.pt.y = y;
249 hook.mouseData = MAKELONG( 0, data );
250 hook.flags = injected_flags;
251 hook.time = time;
252 hook.dwExtraInfo = extra_info;
254 last_time_modified = GetTickCount();
255 if (HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE )) return;
257 SERVER_START_REQ( send_hardware_message )
259 req->id = (injected_flags & LLMHF_INJECTED) ? 0 : GetCurrentThreadId();
260 req->win = hwnd;
261 req->msg = message;
262 req->wparam = MAKEWPARAM( get_key_state(), data );
263 req->lparam = 0;
264 req->x = x;
265 req->y = y;
266 req->time = time;
267 req->info = extra_info;
268 wine_server_call( req );
270 SERVER_END_REQ;
275 /***********************************************************************
276 * X11DRV_send_mouse_input
278 void X11DRV_send_mouse_input( HWND hwnd, DWORD flags, DWORD x, DWORD y,
279 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
281 POINT pt;
283 if (flags & MOUSEEVENTF_MOVE && flags & MOUSEEVENTF_ABSOLUTE)
285 if (injected_flags & LLMHF_INJECTED)
287 pt.x = (x * screen_width) >> 16;
288 pt.y = (y * screen_height) >> 16;
290 else
292 pt.x = x;
293 pt.y = y;
294 wine_tsx11_lock();
295 if (cursor_pos.x == x && cursor_pos.y == y &&
296 (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE)))
297 flags &= ~MOUSEEVENTF_MOVE;
298 wine_tsx11_unlock();
301 else if (flags & MOUSEEVENTF_MOVE)
303 int accel[3], xMult = 1, yMult = 1;
305 /* dx and dy can be negative numbers for relative movements */
306 SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
308 if (abs(x) > accel[0] && accel[2] != 0)
310 xMult = 2;
311 if ((abs(x) > accel[1]) && (accel[2] == 2)) xMult = 4;
313 if (abs(y) > accel[0] && accel[2] != 0)
315 yMult = 2;
316 if ((abs(y) > accel[1]) && (accel[2] == 2)) yMult = 4;
319 wine_tsx11_lock();
320 pt.x = cursor_pos.x + (long)x * xMult;
321 pt.y = cursor_pos.y + (long)y * yMult;
322 wine_tsx11_unlock();
324 else
326 wine_tsx11_lock();
327 pt = cursor_pos;
328 wine_tsx11_unlock();
331 if (flags & MOUSEEVENTF_MOVE)
333 queue_raw_mouse_message( WM_MOUSEMOVE, hwnd, pt.x, pt.y, data, time,
334 extra_info, injected_flags );
335 if ((injected_flags & LLMHF_INJECTED) &&
336 ((flags & MOUSEEVENTF_ABSOLUTE) || x || y)) /* we have to actually move the cursor */
338 X11DRV_SetCursorPos( pt.x, pt.y );
340 else
342 wine_tsx11_lock();
343 clip_point_to_rect( &cursor_clip, &pt);
344 cursor_pos = pt;
345 wine_tsx11_unlock();
348 if (flags & MOUSEEVENTF_LEFTDOWN)
350 key_state_table[VK_LBUTTON] |= 0xc0;
351 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
352 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
354 if (flags & MOUSEEVENTF_LEFTUP)
356 key_state_table[VK_LBUTTON] &= ~0x80;
357 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONUP : WM_LBUTTONUP,
358 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
360 if (flags & MOUSEEVENTF_RIGHTDOWN)
362 key_state_table[VK_RBUTTON] |= 0xc0;
363 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
364 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
366 if (flags & MOUSEEVENTF_RIGHTUP)
368 key_state_table[VK_RBUTTON] &= ~0x80;
369 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONUP : WM_RBUTTONUP,
370 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
372 if (flags & MOUSEEVENTF_MIDDLEDOWN)
374 key_state_table[VK_MBUTTON] |= 0xc0;
375 queue_raw_mouse_message( WM_MBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
376 extra_info, injected_flags );
378 if (flags & MOUSEEVENTF_MIDDLEUP)
380 key_state_table[VK_MBUTTON] &= ~0x80;
381 queue_raw_mouse_message( WM_MBUTTONUP, hwnd, pt.x, pt.y, data, time,
382 extra_info, injected_flags );
384 if (flags & MOUSEEVENTF_WHEEL)
386 queue_raw_mouse_message( WM_MOUSEWHEEL, hwnd, pt.x, pt.y, data, time,
387 extra_info, injected_flags );
389 if (flags & MOUSEEVENTF_XDOWN)
391 key_state_table[VK_XBUTTON1 + data - 1] |= 0xc0;
392 queue_raw_mouse_message( WM_XBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
393 extra_info, injected_flags );
395 if (flags & MOUSEEVENTF_XUP)
397 key_state_table[VK_XBUTTON1 + data - 1] &= ~0x80;
398 queue_raw_mouse_message( WM_XBUTTONUP, hwnd, pt.x, pt.y, data, time,
399 extra_info, injected_flags );
404 #ifdef SONAME_LIBXCURSOR
406 /***********************************************************************
407 * create_cursor_image
409 * Create an XcursorImage from a CURSORICONINFO
411 static XcursorImage *create_cursor_image( CURSORICONINFO *ptr )
413 int x, xmax;
414 int y, ymax;
415 int and_size, xor_size;
416 unsigned char *and_bits, *and_ptr, *xor_bits, *xor_ptr;
417 int and_width_bytes, xor_width_bytes;
418 XcursorPixel *pixel_ptr;
419 XcursorImage *image;
420 BOOL alpha_zero = TRUE;
422 ymax = (ptr->nHeight > 32) ? 32 : ptr->nHeight;
423 xmax = (ptr->nWidth > 32) ? 32 : ptr->nWidth;
425 and_width_bytes = xmax / 8;
426 xor_width_bytes = and_width_bytes * ptr->bBitsPerPixel;
428 and_size = ptr->nWidth * ptr->nHeight / 8;
429 and_ptr = and_bits = (unsigned char *)(ptr + 1);
431 xor_size = xor_width_bytes * ptr->nHeight;
432 xor_ptr = xor_bits = and_ptr + and_size;
434 image = pXcursorImageCreate( xmax, ymax );
435 pixel_ptr = image->pixels;
437 /* Generally 32 bit bitmaps have an alpha channel which is used in favor
438 * of the AND mask. However, if all pixels have alpha = 0x00, the bitmap
439 * is treated like one without alpha and the masks are used. As soon as
440 * one pixel has alpha != 0x00, and the mask ignored as described in the
441 * docs.
443 * This is most likely for applications which create the bitmaps with
444 * CreateDIBitmap, which creates a device dependent bitmap, so the format
445 * that arrives when loading depends on the screen's bpp. Apps that were
446 * written at 8 / 16 bpp times do not know about the 32 bit alpha, so
447 * they would get a completely transparent cursor on 32 bit displays.
449 * Non-32 bit bitmaps always use the AND mask
451 if(ptr->bBitsPerPixel == 32)
453 for (y = 0; alpha_zero && y < ymax; ++y)
455 xor_ptr = xor_bits + (y * xor_width_bytes);
456 for (x = 0; x < xmax; ++x)
458 if (xor_ptr[3] != 0x00)
460 alpha_zero = FALSE;
461 break;
463 xor_ptr+=4;
468 /* On windows, to calculate the color for a pixel, first an AND is done
469 * with the background and the "and" bitmap, then an XOR with the "xor"
470 * bitmap. This means that when the data in the "and" bitmap is 0, the
471 * pixel will get the color as specified in the "xor" bitmap.
472 * However, if the data in the "and" bitmap is 1, the result will be the
473 * background XOR'ed with the value in the "xor" bitmap. In case the "xor"
474 * data is completely black (0x000000) the pixel will become transparent,
475 * in case it's white (0xffffff) the pixel will become the inverse of the
476 * background color.
478 * Since we can't support inverting colors, we map the grayscale value of
479 * the "xor" data to the alpha channel, and xor the color with either
480 * black or white.
482 for (y = 0; y < ymax; ++y)
484 and_ptr = and_bits + (y * and_width_bytes);
485 xor_ptr = xor_bits + (y * xor_width_bytes);
487 for (x = 0; x < xmax; ++x)
489 /* Xcursor pixel data is in ARGB format, with A in the high byte */
490 switch (ptr->bBitsPerPixel)
492 case 32:
493 /* BGRA, 8 bits each */
494 *pixel_ptr = *xor_ptr++;
495 *pixel_ptr |= *xor_ptr++ << 8;
496 *pixel_ptr |= *xor_ptr++ << 16;
497 *pixel_ptr |= *xor_ptr++ << 24;
498 break;
500 case 24:
501 /* BGR, 8 bits each */
502 *pixel_ptr = *xor_ptr++;
503 *pixel_ptr |= *xor_ptr++ << 8;
504 *pixel_ptr |= *xor_ptr++ << 16;
505 break;
507 case 16:
508 /* BGR, 5 red, 6 green, 5 blue */
509 *pixel_ptr = *xor_ptr * 0x1f;
510 *pixel_ptr |= (*xor_ptr & 0xe0) << 3;
511 ++xor_ptr;
512 *pixel_ptr |= (*xor_ptr & 0x07) << 11;
513 *pixel_ptr |= (*xor_ptr & 0xf8) << 13;
514 break;
516 case 1:
517 if (*xor_ptr & (1 << (7 - (x & 7)))) *pixel_ptr = 0xffffff;
518 else *pixel_ptr = 0;
519 if ((x & 7) == 7) ++xor_ptr;
520 break;
522 default:
523 FIXME("Currently no support for cursors with %d bits per pixel\n", ptr->bBitsPerPixel);
524 return 0;
527 if (alpha_zero)
529 /* Alpha channel */
530 if (~*and_ptr & (1 << (7 - (x & 7)))) *pixel_ptr |= 0xff << 24;
531 else if (*pixel_ptr)
533 int alpha = (*pixel_ptr & 0xff) * 0.30f
534 + ((*pixel_ptr & 0xff00) >> 8) * 0.55f
535 + ((*pixel_ptr & 0xff0000) >> 16) * 0.15f;
536 *pixel_ptr ^= ((x + y) % 2) ? 0xffffff : 0x000000;
537 *pixel_ptr |= alpha << 24;
539 if ((x & 7) == 7) ++and_ptr;
541 ++pixel_ptr;
545 return image;
549 /***********************************************************************
550 * create_xcursor_cursor
552 * Use Xcursor to create an X cursor from a Windows one.
554 static Cursor create_xcursor_cursor( Display *display, CURSORICONINFO *ptr )
556 Cursor cursor;
557 XcursorImage *image;
559 if (!ptr) /* Create an empty cursor */
561 image = pXcursorImageCreate( 1, 1 );
562 image->xhot = 0;
563 image->yhot = 0;
564 *(image->pixels) = 0;
565 cursor = pXcursorImageLoadCursor( display, image );
566 pXcursorImageDestroy( image );
568 return cursor;
571 image = create_cursor_image( ptr );
572 if (!image) return 0;
574 /* Make sure hotspot is valid */
575 image->xhot = ptr->ptHotSpot.x;
576 image->yhot = ptr->ptHotSpot.y;
577 if (image->xhot >= image->width ||
578 image->yhot >= image->height)
580 image->xhot = image->width / 2;
581 image->yhot = image->height / 2;
584 image->delay = 0;
586 cursor = pXcursorImageLoadCursor( display, image );
587 pXcursorImageDestroy( image );
589 return cursor;
592 #endif /* SONAME_LIBXCURSOR */
595 /***********************************************************************
596 * create_cursor
598 * Create an X cursor from a Windows one.
600 static Cursor create_cursor( Display *display, CURSORICONINFO *ptr )
602 Pixmap pixmapBits, pixmapMask, pixmapMaskInv = 0, pixmapAll;
603 XColor fg, bg;
604 Cursor cursor = None;
605 POINT hotspot;
606 char *bitMask32 = NULL;
608 #ifdef SONAME_LIBXCURSOR
609 if (pXcursorImageLoadCursor) return create_xcursor_cursor( display, ptr );
610 #endif
612 if (!ptr) /* Create an empty cursor */
614 static const char data[] = { 0 };
616 bg.red = bg.green = bg.blue = 0x0000;
617 pixmapBits = XCreateBitmapFromData( display, root_window, data, 1, 1 );
618 if (pixmapBits)
620 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
621 &bg, &bg, 0, 0 );
622 XFreePixmap( display, pixmapBits );
625 else /* Create the X cursor from the bits */
627 XImage *image;
628 GC gc;
630 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
631 ptr->nWidth, ptr->nHeight, ptr->bPlanes, ptr->bBitsPerPixel,
632 ptr->nWidthBytes);
634 /* Create a pixmap and transfer all the bits to it */
636 /* NOTE: Following hack works, but only because XFree depth
637 * 1 images really use 1 bit/pixel (and so the same layout
638 * as the Windows cursor data). Perhaps use a more generic
639 * algorithm here.
641 /* This pixmap will be written with two bitmaps. The first is
642 * the mask and the second is the image.
644 if (!(pixmapAll = XCreatePixmap( display, root_window,
645 ptr->nWidth, ptr->nHeight * 2, 1 )))
646 return 0;
647 if (!(image = XCreateImage( display, visual,
648 1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
649 ptr->nHeight * 2, 16, ptr->nWidthBytes/ptr->bBitsPerPixel)))
651 XFreePixmap( display, pixmapAll );
652 return 0;
654 gc = XCreateGC( display, pixmapAll, 0, NULL );
655 XSetGraphicsExposures( display, gc, False );
656 image->byte_order = MSBFirst;
657 image->bitmap_bit_order = MSBFirst;
658 image->bitmap_unit = 16;
659 _XInitImageFuncPtrs(image);
660 if (ptr->bPlanes * ptr->bBitsPerPixel == 1)
662 /* A plain old white on black cursor. */
663 fg.red = fg.green = fg.blue = 0xffff;
664 bg.red = bg.green = bg.blue = 0x0000;
665 XPutImage( display, pixmapAll, gc, image,
666 0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
668 else
670 int rbits, gbits, bbits, red, green, blue;
671 int rfg, gfg, bfg, rbg, gbg, bbg;
672 int rscale, gscale, bscale;
673 int x, y, xmax, ymax, bitIndex, byteIndex, xorIndex;
674 unsigned char *theMask, *theImage, theChar;
675 int threshold, fgBits, bgBits, bitShifted;
676 BYTE pXorBits[128]; /* Up to 32x32 icons */
678 switch (ptr->bBitsPerPixel)
680 case 32:
681 bitMask32 = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
682 ptr->nWidth * ptr->nHeight / 8 );
683 /* Fallthrough */
684 case 24:
685 rbits = 8;
686 gbits = 8;
687 bbits = 8;
688 threshold = 0x40;
689 break;
690 case 16:
691 rbits = 5;
692 gbits = 6;
693 bbits = 5;
694 threshold = 0x40;
695 break;
696 default:
697 FIXME("Currently no support for cursors with %d bits per pixel\n",
698 ptr->bBitsPerPixel);
699 XFreePixmap( display, pixmapAll );
700 XFreeGC( display, gc );
701 image->data = NULL;
702 XDestroyImage( image );
703 return 0;
705 /* The location of the mask. */
706 theMask = (unsigned char *)(ptr + 1);
707 /* The mask should still be 1 bit per pixel. The color image
708 * should immediately follow the mask.
710 theImage = &theMask[ptr->nWidth/8 * ptr->nHeight];
711 rfg = gfg = bfg = rbg = gbg = bbg = 0;
712 bitIndex = 0;
713 byteIndex = 0;
714 xorIndex = 0;
715 fgBits = 0;
716 bitShifted = 0x01;
717 xmax = (ptr->nWidth > 32) ? 32 : ptr->nWidth;
718 if (ptr->nWidth > 32) {
719 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
720 ptr->nWidth, ptr->nHeight);
722 ymax = (ptr->nHeight > 32) ? 32 : ptr->nHeight;
724 memset(pXorBits, 0, 128);
725 for (y=0; y<ymax; y++)
727 for (x=0; x<xmax; x++)
729 red = green = blue = 0;
730 switch (ptr->bBitsPerPixel)
732 case 32:
733 theChar = theImage[byteIndex++];
734 blue = theChar;
735 theChar = theImage[byteIndex++];
736 green = theChar;
737 theChar = theImage[byteIndex++];
738 red = theChar;
739 theChar = theImage[byteIndex++];
740 /* If the alpha channel is >5% transparent,
741 * assume that we can add it to the bitMask32.
743 if (theChar > 0x0D)
744 *(bitMask32 + (y*xmax+x)/8) |= 1 << (x & 7);
745 break;
746 case 24:
747 theChar = theImage[byteIndex++];
748 blue = theChar;
749 theChar = theImage[byteIndex++];
750 green = theChar;
751 theChar = theImage[byteIndex++];
752 red = theChar;
753 break;
754 case 16:
755 theChar = theImage[byteIndex++];
756 blue = theChar & 0x1F;
757 green = (theChar & 0xE0) >> 5;
758 theChar = theImage[byteIndex++];
759 green |= (theChar & 0x07) << 3;
760 red = (theChar & 0xF8) >> 3;
761 break;
764 if (red+green+blue > threshold)
766 rfg += red;
767 gfg += green;
768 bfg += blue;
769 fgBits++;
770 pXorBits[xorIndex] |= bitShifted;
772 else
774 rbg += red;
775 gbg += green;
776 bbg += blue;
778 if (x%8 == 7)
780 bitShifted = 0x01;
781 xorIndex++;
783 else
784 bitShifted = bitShifted << 1;
787 rscale = 1 << (16 - rbits);
788 gscale = 1 << (16 - gbits);
789 bscale = 1 << (16 - bbits);
790 if (fgBits)
792 fg.red = rfg * rscale / fgBits;
793 fg.green = gfg * gscale / fgBits;
794 fg.blue = bfg * bscale / fgBits;
796 else fg.red = fg.green = fg.blue = 0;
797 bgBits = xmax * ymax - fgBits;
798 if (bgBits)
800 bg.red = rbg * rscale / bgBits;
801 bg.green = gbg * gscale / bgBits;
802 bg.blue = bbg * bscale / bgBits;
804 else bg.red = bg.green = bg.blue = 0;
805 pixmapBits = XCreateBitmapFromData( display, root_window, (char *)pXorBits, xmax, ymax );
806 if (!pixmapBits)
808 HeapFree( GetProcessHeap(), 0, bitMask32 );
809 XFreePixmap( display, pixmapAll );
810 XFreeGC( display, gc );
811 image->data = NULL;
812 XDestroyImage( image );
813 return 0;
816 /* Put the mask. */
817 XPutImage( display, pixmapAll, gc, image,
818 0, 0, 0, 0, ptr->nWidth, ptr->nHeight );
819 XSetFunction( display, gc, GXcopy );
820 /* Put the image */
821 XCopyArea( display, pixmapBits, pixmapAll, gc,
822 0, 0, xmax, ymax, 0, ptr->nHeight );
823 XFreePixmap( display, pixmapBits );
825 image->data = NULL;
826 XDestroyImage( image );
828 /* Now create the 2 pixmaps for bits and mask */
830 pixmapBits = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
831 if (ptr->bBitsPerPixel != 32)
833 pixmapMaskInv = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
834 pixmapMask = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
836 /* Make sure everything went OK so far */
837 if (pixmapBits && pixmapMask && pixmapMaskInv)
839 /* We have to do some magic here, as cursors are not fully
840 * compatible between Windows and X11. Under X11, there are
841 * only 3 possible color cursor: black, white and masked. So
842 * we map the 4th Windows color (invert the bits on the screen)
843 * to black and an additional white bit on an other place
844 * (+1,+1). This require some boolean arithmetic:
846 * Windows | X11
847 * And Xor Result | Bits Mask Result
848 * 0 0 black | 0 1 background
849 * 0 1 white | 1 1 foreground
850 * 1 0 no change | X 0 no change
851 * 1 1 inverted | 0 1 background
853 * which gives:
854 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
855 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
857 * FIXME: apparently some servers do support 'inverted' color.
858 * I don't know if it's correct per the X spec, but maybe we
859 * ought to take advantage of it. -- AJ
861 XSetFunction( display, gc, GXcopy );
862 XCopyArea( display, pixmapAll, pixmapBits, gc,
863 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
864 XCopyArea( display, pixmapAll, pixmapMask, gc,
865 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
866 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
867 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
868 XSetFunction( display, gc, GXand );
869 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
870 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
871 XSetFunction( display, gc, GXandReverse );
872 XCopyArea( display, pixmapAll, pixmapBits, gc,
873 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
874 XSetFunction( display, gc, GXorReverse );
875 XCopyArea( display, pixmapAll, pixmapMask, gc,
876 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
877 /* Additional white */
878 XSetFunction( display, gc, GXor );
879 XCopyArea( display, pixmapMaskInv, pixmapMask, gc,
880 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
881 XCopyArea( display, pixmapMaskInv, pixmapBits, gc,
882 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
883 XSetFunction( display, gc, GXcopy );
886 else
888 pixmapMask = XCreateBitmapFromData( display, root_window,
889 bitMask32, ptr->nWidth,
890 ptr->nHeight );
891 HeapFree( GetProcessHeap(), 0, bitMask32 );
894 /* Make sure hotspot is valid */
895 hotspot.x = ptr->ptHotSpot.x;
896 hotspot.y = ptr->ptHotSpot.y;
897 if (hotspot.x < 0 || hotspot.x >= ptr->nWidth ||
898 hotspot.y < 0 || hotspot.y >= ptr->nHeight)
900 hotspot.x = ptr->nWidth / 2;
901 hotspot.y = ptr->nHeight / 2;
904 if (pixmapBits && pixmapMask)
905 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
906 &fg, &bg, hotspot.x, hotspot.y );
908 /* Now free everything */
910 if (pixmapAll) XFreePixmap( display, pixmapAll );
911 if (pixmapBits) XFreePixmap( display, pixmapBits );
912 if (pixmapMask) XFreePixmap( display, pixmapMask );
913 if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
914 XFreeGC( display, gc );
916 return cursor;
920 /***********************************************************************
921 * SetCursor (X11DRV.@)
923 void X11DRV_SetCursor( CURSORICONINFO *lpCursor )
925 struct x11drv_thread_data *data = x11drv_thread_data();
926 Cursor cursor;
928 if (lpCursor)
929 TRACE("%ux%u, planes %u, bpp %u\n",
930 lpCursor->nWidth, lpCursor->nHeight, lpCursor->bPlanes, lpCursor->bBitsPerPixel);
931 else
932 TRACE("NULL\n");
934 /* set the same cursor for all top-level windows of the current thread */
936 wine_tsx11_lock();
937 cursor = create_cursor( data->display, lpCursor );
938 if (cursor)
940 if (data->cursor) XFreeCursor( data->display, data->cursor );
941 data->cursor = cursor;
942 if (data->cursor_window)
944 XDefineCursor( data->display, data->cursor_window, cursor );
945 /* Make the change take effect immediately */
946 XFlush( data->display );
949 wine_tsx11_unlock();
952 /***********************************************************************
953 * SetCursorPos (X11DRV.@)
955 BOOL X11DRV_SetCursorPos( INT x, INT y )
957 Display *display = thread_display();
958 POINT pt;
960 TRACE( "warping to (%d,%d)\n", x, y );
962 wine_tsx11_lock();
963 if (cursor_pos.x == x && cursor_pos.y == y)
965 wine_tsx11_unlock();
966 /* We still need to generate WM_MOUSEMOVE */
967 queue_raw_mouse_message( WM_MOUSEMOVE, NULL, x, y, 0, GetCurrentTime(), 0, 0 );
968 return TRUE;
971 pt.x = x; pt.y = y;
972 clip_point_to_rect( &cursor_clip, &pt);
973 XWarpPointer( display, root_window, root_window, 0, 0, 0, 0,
974 pt.x - virtual_screen_rect.left, pt.y - virtual_screen_rect.top );
975 XFlush( display ); /* avoids bad mouse lag in games that do their own mouse warping */
976 cursor_pos = pt;
977 wine_tsx11_unlock();
978 return TRUE;
981 /***********************************************************************
982 * GetCursorPos (X11DRV.@)
984 BOOL X11DRV_GetCursorPos(LPPOINT pos)
986 Display *display = thread_display();
987 Window root, child;
988 int rootX, rootY, winX, winY;
989 unsigned int xstate;
991 wine_tsx11_lock();
992 if ((GetTickCount() - last_time_modified > 100) &&
993 XQueryPointer( display, root_window, &root, &child,
994 &rootX, &rootY, &winX, &winY, &xstate ))
996 update_button_state( xstate );
997 winX += virtual_screen_rect.left;
998 winY += virtual_screen_rect.top;
999 TRACE("pointer at (%d,%d)\n", winX, winY );
1000 cursor_pos.x = winX;
1001 cursor_pos.y = winY;
1003 *pos = cursor_pos;
1004 wine_tsx11_unlock();
1005 return TRUE;
1009 /***********************************************************************
1010 * ClipCursor (X11DRV.@)
1012 * Set the cursor clipping rectangle.
1014 BOOL X11DRV_ClipCursor( LPCRECT clip )
1016 if (!IntersectRect( &cursor_clip, &virtual_screen_rect, clip ))
1017 cursor_clip = virtual_screen_rect;
1019 return TRUE;
1022 /***********************************************************************
1023 * X11DRV_ButtonPress
1025 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1027 XButtonEvent *event = &xev->xbutton;
1028 int buttonNum = event->button - 1;
1029 WORD wData = 0;
1030 POINT pt;
1032 if (buttonNum >= NB_BUTTONS) return;
1033 if (!hwnd) return;
1035 switch (buttonNum)
1037 case 3:
1038 wData = WHEEL_DELTA;
1039 break;
1040 case 4:
1041 wData = -WHEEL_DELTA;
1042 break;
1043 case 5:
1044 wData = XBUTTON1;
1045 break;
1046 case 6:
1047 wData = XBUTTON2;
1048 break;
1051 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1053 X11DRV_send_mouse_input( hwnd, button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1054 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1058 /***********************************************************************
1059 * X11DRV_ButtonRelease
1061 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1063 XButtonEvent *event = &xev->xbutton;
1064 int buttonNum = event->button - 1;
1065 WORD wData = 0;
1066 POINT pt;
1068 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1069 if (!hwnd) return;
1071 switch (buttonNum)
1073 case 5:
1074 wData = XBUTTON1;
1075 break;
1076 case 6:
1077 wData = XBUTTON2;
1078 break;
1081 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1083 X11DRV_send_mouse_input( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1084 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1088 /***********************************************************************
1089 * X11DRV_MotionNotify
1091 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1093 XMotionEvent *event = &xev->xmotion;
1094 POINT pt;
1096 TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);
1098 if (!hwnd) return;
1100 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1102 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1103 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1107 /***********************************************************************
1108 * X11DRV_EnterNotify
1110 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1112 XCrossingEvent *event = &xev->xcrossing;
1113 POINT pt;
1115 TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);
1117 if (!hwnd) return;
1118 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1120 /* simulate a mouse motion event */
1121 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1123 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1124 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );