4 * Copyright 1998 Ulrich Weigand
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
30 #include "wine/winuser16.h"
34 #include "wine/server.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
39 /**********************************************************************/
42 #define Button6Mask (1<<13)
45 #define Button7Mask (1<<14)
48 #define NB_BUTTONS 7 /* Windows can handle 5 buttons and the wheel too */
50 static const UINT button_down_flags[NB_BUTTONS] =
53 MOUSEEVENTF_MIDDLEDOWN,
54 MOUSEEVENTF_RIGHTDOWN,
61 static const UINT button_up_flags[NB_BUTTONS] =
74 /***********************************************************************
77 * get the coordinates of a mouse event
79 static inline void get_coords( HWND hwnd, int x, int y, POINT *pt )
81 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
85 pt->x = x + data->whole_rect.left;
86 pt->y = y + data->whole_rect.top;
90 /***********************************************************************
93 * Update the button state with what X provides us
95 static inline void update_button_state( unsigned int state )
97 key_state_table[VK_LBUTTON] = (state & Button1Mask ? 0x80 : 0);
98 key_state_table[VK_MBUTTON] = (state & Button2Mask ? 0x80 : 0);
99 key_state_table[VK_RBUTTON] = (state & Button3Mask ? 0x80 : 0);
100 /* X-buttons are not reported from XQueryPointer */
104 /***********************************************************************
107 * Update the various window states on a mouse event.
109 static void update_mouse_state( HWND hwnd, Window window, int x, int y, unsigned int state, POINT *pt )
111 struct x11drv_thread_data *data = x11drv_thread_data();
113 if (window == root_window)
115 x += virtual_screen_rect.left;
116 y += virtual_screen_rect.top;
118 get_coords( hwnd, x, y, pt );
120 /* update the cursor */
122 if (data->cursor_window != window)
124 data->cursor_window = window;
126 if (data->cursor) XDefineCursor( data->display, window, data->cursor );
130 /* update the wine server Z-order */
132 if (window != data->grab_window &&
133 /* ignore event if a button is pressed, since the mouse is then grabbed too */
134 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
136 SERVER_START_REQ( update_window_zorder )
139 req->rect.left = pt->x;
140 req->rect.top = pt->y;
141 req->rect.right = pt->x + 1;
142 req->rect.bottom = pt->y + 1;
143 wine_server_call( req );
150 /***********************************************************************
153 static WORD get_key_state(void)
157 if (GetSystemMetrics( SM_SWAPBUTTON ))
159 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
160 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
164 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
165 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
167 if (key_state_table[VK_MBUTTON] & 0x80) ret |= MK_MBUTTON;
168 if (key_state_table[VK_SHIFT] & 0x80) ret |= MK_SHIFT;
169 if (key_state_table[VK_CONTROL] & 0x80) ret |= MK_CONTROL;
170 if (key_state_table[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
171 if (key_state_table[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
176 /***********************************************************************
177 * queue_raw_mouse_message
179 static void queue_raw_mouse_message( UINT message, HWND hwnd, DWORD x, DWORD y,
180 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
186 hook.mouseData = MAKELONG( 0, data );
187 hook.flags = injected_flags;
189 hook.dwExtraInfo = extra_info;
191 if (HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE )) return;
193 SERVER_START_REQ( send_hardware_message )
195 req->id = (injected_flags & LLMHF_INJECTED) ? 0 : GetCurrentThreadId();
198 req->wparam = MAKEWPARAM( get_key_state(), data );
203 req->info = extra_info;
204 wine_server_call( req );
211 /***********************************************************************
212 * X11DRV_send_mouse_input
214 void X11DRV_send_mouse_input( HWND hwnd, DWORD flags, DWORD x, DWORD y,
215 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
219 if (flags & MOUSEEVENTF_ABSOLUTE)
221 if (injected_flags & LLMHF_INJECTED)
223 pt.x = (x * screen_width) >> 16;
224 pt.y = (y * screen_height) >> 16;
235 else if (flags & MOUSEEVENTF_MOVE)
237 int accel[3], xMult = 1, yMult = 1;
239 /* dx and dy can be negative numbers for relative movements */
240 SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
242 if (abs(x) > accel[0] && accel[2] != 0)
245 if ((abs(x) > accel[1]) && (accel[2] == 2)) xMult = 4;
247 if (abs(y) > accel[0] && accel[2] != 0)
250 if ((abs(y) > accel[1]) && (accel[2] == 2)) yMult = 4;
254 pt.x = cursor_pos.x + (long)x * xMult;
255 pt.y = cursor_pos.y + (long)y * yMult;
257 /* Clip to the current screen size */
258 if (pt.x < 0) pt.x = 0;
259 else if (pt.x >= screen_width) pt.x = screen_width - 1;
260 if (pt.y < 0) pt.y = 0;
261 else if (pt.y >= screen_height) pt.y = screen_height - 1;
272 if (flags & MOUSEEVENTF_MOVE)
274 queue_raw_mouse_message( WM_MOUSEMOVE, hwnd, pt.x, pt.y, data, time,
275 extra_info, injected_flags );
276 if ((injected_flags & LLMHF_INJECTED) &&
277 ((flags & MOUSEEVENTF_ABSOLUTE) || x || y)) /* we have to actually move the cursor */
279 TRACE( "warping to (%d,%d)\n", pt.x, pt.y );
281 XWarpPointer( thread_display(), root_window, root_window, 0, 0, 0, 0,
282 pt.x - virtual_screen_rect.left, pt.y - virtual_screen_rect.top );
286 if (flags & MOUSEEVENTF_LEFTDOWN)
288 key_state_table[VK_LBUTTON] |= 0xc0;
289 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
290 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
292 if (flags & MOUSEEVENTF_LEFTUP)
294 key_state_table[VK_LBUTTON] &= ~0x80;
295 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONUP : WM_LBUTTONUP,
296 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
298 if (flags & MOUSEEVENTF_RIGHTDOWN)
300 key_state_table[VK_RBUTTON] |= 0xc0;
301 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
302 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
304 if (flags & MOUSEEVENTF_RIGHTUP)
306 key_state_table[VK_RBUTTON] &= ~0x80;
307 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONUP : WM_RBUTTONUP,
308 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
310 if (flags & MOUSEEVENTF_MIDDLEDOWN)
312 key_state_table[VK_MBUTTON] |= 0xc0;
313 queue_raw_mouse_message( WM_MBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
314 extra_info, injected_flags );
316 if (flags & MOUSEEVENTF_MIDDLEUP)
318 key_state_table[VK_MBUTTON] &= ~0x80;
319 queue_raw_mouse_message( WM_MBUTTONUP, hwnd, pt.x, pt.y, data, time,
320 extra_info, injected_flags );
322 if (flags & MOUSEEVENTF_WHEEL)
324 queue_raw_mouse_message( WM_MOUSEWHEEL, hwnd, pt.x, pt.y, data, time,
325 extra_info, injected_flags );
327 if (flags & MOUSEEVENTF_XDOWN)
329 key_state_table[VK_XBUTTON1 + data - 1] |= 0xc0;
330 queue_raw_mouse_message( WM_XBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
331 extra_info, injected_flags );
333 if (flags & MOUSEEVENTF_XUP)
335 key_state_table[VK_XBUTTON1 + data - 1] &= ~0x80;
336 queue_raw_mouse_message( WM_XBUTTONUP, hwnd, pt.x, pt.y, data, time,
337 extra_info, injected_flags );
342 /***********************************************************************
345 * Create an X cursor from a Windows one.
347 static Cursor create_cursor( Display *display, CURSORICONINFO *ptr )
349 Pixmap pixmapBits, pixmapMask, pixmapMaskInv, pixmapAll;
351 Cursor cursor = None;
353 if (!ptr) /* Create an empty cursor */
355 static const char data[] = { 0 };
357 bg.red = bg.green = bg.blue = 0x0000;
358 pixmapBits = XCreateBitmapFromData( display, root_window, data, 1, 1 );
361 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
363 XFreePixmap( display, pixmapBits );
366 else /* Create the X cursor from the bits */
371 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
372 ptr->nWidth, ptr->nHeight, ptr->bPlanes, ptr->bBitsPerPixel,
374 /* Create a pixmap and transfer all the bits to it */
376 /* NOTE: Following hack works, but only because XFree depth
377 * 1 images really use 1 bit/pixel (and so the same layout
378 * as the Windows cursor data). Perhaps use a more generic
381 /* This pixmap will be written with two bitmaps. The first is
382 * the mask and the second is the image.
384 if (!(pixmapAll = XCreatePixmap( display, root_window,
385 ptr->nWidth, ptr->nHeight * 2, 1 )))
387 if (!(image = XCreateImage( display, visual,
388 1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
389 ptr->nHeight * 2, 16, ptr->nWidthBytes/ptr->bBitsPerPixel)))
391 XFreePixmap( display, pixmapAll );
394 gc = XCreateGC( display, pixmapAll, 0, NULL );
395 XSetGraphicsExposures( display, gc, False );
396 image->byte_order = MSBFirst;
397 image->bitmap_bit_order = MSBFirst;
398 image->bitmap_unit = 16;
399 _XInitImageFuncPtrs(image);
400 if (ptr->bPlanes * ptr->bBitsPerPixel == 1)
402 /* A plain old white on black cursor. */
403 fg.red = fg.green = fg.blue = 0xffff;
404 bg.red = bg.green = bg.blue = 0x0000;
405 XPutImage( display, pixmapAll, gc, image,
406 0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
410 int rbits, gbits, bbits, red, green, blue;
411 int rfg, gfg, bfg, rbg, gbg, bbg;
412 int rscale, gscale, bscale;
413 int x, y, xmax, ymax, bitIndex, byteIndex, xorIndex;
414 unsigned char *theMask, *theImage, theChar;
415 int threshold, fgBits, bgBits, bitShifted;
416 BYTE pXorBits[128]; /* Up to 32x32 icons */
418 switch (ptr->bBitsPerPixel)
433 FIXME("Currently no support for cursors with %d bits per pixel\n",
435 XFreePixmap( display, pixmapAll );
436 XFreeGC( display, gc );
438 XDestroyImage( image );
441 /* The location of the mask. */
442 theMask = (unsigned char *)(ptr + 1);
443 /* The mask should still be 1 bit per pixel. The color image
444 * should immediately follow the mask.
446 theImage = &theMask[ptr->nWidth/8 * ptr->nHeight];
447 rfg = gfg = bfg = rbg = gbg = bbg = 0;
453 xmax = (ptr->nWidth > 32) ? 32 : ptr->nWidth;
454 if (ptr->nWidth > 32) {
455 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
456 ptr->nWidth, ptr->nHeight);
458 ymax = (ptr->nHeight > 32) ? 32 : ptr->nHeight;
460 memset(pXorBits, 0, 128);
461 for (y=0; y<ymax; y++)
463 for (x=0; x<xmax; x++)
465 red = green = blue = 0;
466 switch (ptr->bBitsPerPixel)
469 theChar = theImage[byteIndex++];
471 theChar = theImage[byteIndex++];
473 theChar = theImage[byteIndex++];
477 theChar = theImage[byteIndex++];
478 blue = theChar & 0x1F;
479 green = (theChar & 0xE0) >> 5;
480 theChar = theImage[byteIndex++];
481 green |= (theChar & 0x07) << 3;
482 red = (theChar & 0xF8) >> 3;
486 if (red+green+blue > threshold)
492 pXorBits[xorIndex] |= bitShifted;
506 bitShifted = bitShifted << 1;
509 rscale = 1 << (16 - rbits);
510 gscale = 1 << (16 - gbits);
511 bscale = 1 << (16 - bbits);
514 fg.red = rfg * rscale / fgBits;
515 fg.green = gfg * gscale / fgBits;
516 fg.blue = bfg * bscale / fgBits;
518 else fg.red = fg.green = fg.blue = 0;
519 bgBits = xmax * ymax - fgBits;
522 bg.red = rbg * rscale / bgBits;
523 bg.green = gbg * gscale / bgBits;
524 bg.blue = bbg * bscale / bgBits;
526 else bg.red = bg.green = bg.blue = 0;
527 pixmapBits = XCreateBitmapFromData( display, root_window, (char *)pXorBits, xmax, ymax );
530 XFreePixmap( display, pixmapAll );
531 XFreeGC( display, gc );
533 XDestroyImage( image );
538 XPutImage( display, pixmapAll, gc, image,
539 0, 0, 0, 0, ptr->nWidth, ptr->nHeight );
540 XSetFunction( display, gc, GXcopy );
542 XCopyArea( display, pixmapBits, pixmapAll, gc,
543 0, 0, xmax, ymax, 0, ptr->nHeight );
544 XFreePixmap( display, pixmapBits );
547 XDestroyImage( image );
549 /* Now create the 2 pixmaps for bits and mask */
551 pixmapBits = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
552 pixmapMask = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
553 pixmapMaskInv = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
555 /* Make sure everything went OK so far */
557 if (pixmapBits && pixmapMask && pixmapMaskInv)
561 /* We have to do some magic here, as cursors are not fully
562 * compatible between Windows and X11. Under X11, there
563 * are only 3 possible color cursor: black, white and
564 * masked. So we map the 4th Windows color (invert the
565 * bits on the screen) to black and an additional white bit on
566 * an other place (+1,+1). This require some boolean arithmetic:
569 * And Xor Result | Bits Mask Result
570 * 0 0 black | 0 1 background
571 * 0 1 white | 1 1 foreground
572 * 1 0 no change | X 0 no change
573 * 1 1 inverted | 0 1 background
576 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
577 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
579 * FIXME: apparently some servers do support 'inverted' color.
580 * I don't know if it's correct per the X spec, but maybe
581 * we ought to take advantage of it. -- AJ
583 XSetFunction( display, gc, GXcopy );
584 XCopyArea( display, pixmapAll, pixmapBits, gc,
585 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
586 XCopyArea( display, pixmapAll, pixmapMask, gc,
587 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
588 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
589 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
590 XSetFunction( display, gc, GXand );
591 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
592 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
593 XSetFunction( display, gc, GXandReverse );
594 XCopyArea( display, pixmapAll, pixmapBits, gc,
595 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
596 XSetFunction( display, gc, GXorReverse );
597 XCopyArea( display, pixmapAll, pixmapMask, gc,
598 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
599 /* Additional white */
600 XSetFunction( display, gc, GXor );
601 XCopyArea( display, pixmapMaskInv, pixmapMask, gc,
602 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
603 XCopyArea( display, pixmapMaskInv, pixmapBits, gc,
604 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
605 XSetFunction( display, gc, GXcopy );
607 /* Make sure hotspot is valid */
608 hotspot.x = ptr->ptHotSpot.x;
609 hotspot.y = ptr->ptHotSpot.y;
610 if (hotspot.x < 0 || hotspot.x >= ptr->nWidth ||
611 hotspot.y < 0 || hotspot.y >= ptr->nHeight)
613 hotspot.x = ptr->nWidth / 2;
614 hotspot.y = ptr->nHeight / 2;
616 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
617 &fg, &bg, hotspot.x, hotspot.y );
620 /* Now free everything */
622 if (pixmapAll) XFreePixmap( display, pixmapAll );
623 if (pixmapBits) XFreePixmap( display, pixmapBits );
624 if (pixmapMask) XFreePixmap( display, pixmapMask );
625 if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
626 XFreeGC( display, gc );
632 /***********************************************************************
633 * SetCursor (X11DRV.@)
635 void X11DRV_SetCursor( CURSORICONINFO *lpCursor )
639 if (root_window != DefaultRootWindow(gdi_display))
641 /* If in desktop mode, set the cursor on the desktop window */
644 cursor = create_cursor( gdi_display, lpCursor );
647 XDefineCursor( gdi_display, root_window, cursor );
648 /* Make the change take effect immediately */
650 XFreeCursor( gdi_display, cursor );
654 else /* set the same cursor for all top-level windows of the current thread */
656 struct x11drv_thread_data *data = x11drv_thread_data();
659 cursor = create_cursor( data->display, lpCursor );
662 if (data->cursor) XFreeCursor( data->display, data->cursor );
663 data->cursor = cursor;
664 if (data->cursor_window)
666 XDefineCursor( data->display, data->cursor_window, cursor );
667 /* Make the change take effect immediately */
668 XFlush( data->display );
675 /***********************************************************************
676 * SetCursorPos (X11DRV.@)
678 BOOL X11DRV_SetCursorPos( INT x, INT y )
680 Display *display = thread_display();
682 TRACE( "warping to (%d,%d)\n", x, y );
685 XWarpPointer( display, root_window, root_window, 0, 0, 0, 0,
686 x - virtual_screen_rect.left, y - virtual_screen_rect.top );
687 XFlush( display ); /* avoids bad mouse lag in games that do their own mouse warping */
694 /***********************************************************************
695 * GetCursorPos (X11DRV.@)
697 BOOL X11DRV_GetCursorPos(LPPOINT pos)
699 Display *display = thread_display();
701 int rootX, rootY, winX, winY;
705 if (XQueryPointer( display, root_window, &root, &child,
706 &rootX, &rootY, &winX, &winY, &xstate ))
708 update_button_state( xstate );
709 winX += virtual_screen_rect.left;
710 winY += virtual_screen_rect.top;
711 TRACE("pointer at (%d,%d)\n", winX, winY );
720 /***********************************************************************
723 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
725 XButtonEvent *event = &xev->xbutton;
726 int buttonNum = event->button - 1;
730 if (buttonNum >= NB_BUTTONS) return;
739 wData = -WHEEL_DELTA;
749 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
751 X11DRV_send_mouse_input( hwnd, button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE,
752 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
756 /***********************************************************************
757 * X11DRV_ButtonRelease
759 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
761 XButtonEvent *event = &xev->xbutton;
762 int buttonNum = event->button - 1;
766 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
779 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
781 X11DRV_send_mouse_input( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE,
782 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
786 /***********************************************************************
787 * X11DRV_MotionNotify
789 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
791 XMotionEvent *event = &xev->xmotion;
794 TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);
798 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
800 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
801 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
805 /***********************************************************************
808 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
810 XCrossingEvent *event = &xev->xcrossing;
813 TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);
816 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
818 /* simulate a mouse motion event */
819 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
821 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
822 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );