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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #ifdef HAVE_LIBXXF86DGA2
25 #include <X11/extensions/xf86dga.h>
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
33 #include "wine/winuser16.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
41 /**********************************************************************/
43 #define NB_BUTTONS 5 /* Windows can handle 3 buttons and the wheel too */
45 static const UINT button_down_flags[NB_BUTTONS] =
48 MOUSEEVENTF_MIDDLEDOWN,
49 MOUSEEVENTF_RIGHTDOWN,
54 static const UINT button_up_flags[NB_BUTTONS] =
63 static BYTE *pKeyStateTable;
66 /***********************************************************************
69 * get the coordinates of a mouse event
71 static inline void get_coords( HWND hwnd, Window window, int x, int y, POINT *pt )
73 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
77 pt->x = x + data->whole_rect.left;
78 pt->y = y + data->whole_rect.top;
82 /***********************************************************************
85 * Update the button state with what X provides us
87 static void update_button_state( unsigned int state )
89 pKeyStateTable[VK_LBUTTON] = (state & Button1Mask ? 0x80 : 0);
90 pKeyStateTable[VK_MBUTTON] = (state & Button2Mask ? 0x80 : 0);
91 pKeyStateTable[VK_RBUTTON] = (state & Button3Mask ? 0x80 : 0);
95 /***********************************************************************
98 * Update the key state with what X provides us
100 static void update_key_state( unsigned int state )
102 pKeyStateTable[VK_SHIFT] = (state & ShiftMask ? 0x80 : 0);
103 pKeyStateTable[VK_CONTROL] = (state & ControlMask ? 0x80 : 0);
107 /***********************************************************************
110 static void send_mouse_event( HWND hwnd, DWORD flags, DWORD posX, DWORD posY,
111 DWORD data, Time time )
115 TRACE("(%04lX,%ld,%ld)\n", flags, posX, posY );
117 if (flags & MOUSEEVENTF_ABSOLUTE)
119 int width = GetSystemMetrics( SM_CXSCREEN );
120 int height = GetSystemMetrics( SM_CYSCREEN );
121 /* Relative mouse movements seem not to be scaled as absolute ones */
122 posX = (((long)posX << 16) + width-1) / width;
123 posY = (((long)posY << 16) + height-1) / height;
126 input.type = WINE_INTERNAL_INPUT_MOUSE;
127 input.u.mi.dx = posX;
128 input.u.mi.dy = posY;
129 input.u.mi.mouseData = data;
130 input.u.mi.dwFlags = flags;
131 input.u.mi.time = EVENT_x11_time_to_win32_time(time);
132 input.u.mi.dwExtraInfo = (ULONG_PTR)hwnd;
133 SendInput( 1, &input, sizeof(input) );
137 /***********************************************************************
140 * Update the cursor of a window on a mouse event.
142 static void update_cursor( HWND hwnd, Window win )
144 struct x11drv_thread_data *data = x11drv_thread_data();
146 if (data->cursor_window != win)
148 data->cursor_window = win;
150 if (data->cursor) XDefineCursor( data->display, win, data->cursor );
156 /***********************************************************************
159 * Create an X cursor from a Windows one.
161 static Cursor create_cursor( Display *display, CURSORICONINFO *ptr )
163 Pixmap pixmapBits, pixmapMask, pixmapMaskInv, pixmapAll;
165 Cursor cursor = None;
167 if (!ptr) /* Create an empty cursor */
169 static const char data[] = { 0 };
171 bg.red = bg.green = bg.blue = 0x0000;
172 pixmapBits = XCreateBitmapFromData( display, root_window, data, 1, 1 );
175 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
177 XFreePixmap( display, pixmapBits );
180 else /* Create the X cursor from the bits */
185 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
186 ptr->nWidth, ptr->nHeight, ptr->bPlanes, ptr->bBitsPerPixel,
188 /* Create a pixmap and transfer all the bits to it */
190 /* NOTE: Following hack works, but only because XFree depth
191 * 1 images really use 1 bit/pixel (and so the same layout
192 * as the Windows cursor data). Perhaps use a more generic
195 /* This pixmap will be written with two bitmaps. The first is
196 * the mask and the second is the image.
198 if (!(pixmapAll = XCreatePixmap( display, root_window,
199 ptr->nWidth, ptr->nHeight * 2, 1 )))
201 if (!(image = XCreateImage( display, visual,
202 1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
203 ptr->nHeight * 2, 16, ptr->nWidthBytes/ptr->bBitsPerPixel)))
205 XFreePixmap( display, pixmapAll );
208 gc = XCreateGC( display, pixmapAll, 0, NULL );
209 XSetGraphicsExposures( display, gc, False );
210 image->byte_order = MSBFirst;
211 image->bitmap_bit_order = MSBFirst;
212 image->bitmap_unit = 16;
213 _XInitImageFuncPtrs(image);
214 if (ptr->bPlanes * ptr->bBitsPerPixel == 1)
216 /* A plain old white on black cursor. */
217 fg.red = fg.green = fg.blue = 0xffff;
218 bg.red = bg.green = bg.blue = 0x0000;
219 XPutImage( display, pixmapAll, gc, image,
220 0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
224 int rbits, gbits, bbits, red, green, blue;
225 int rfg, gfg, bfg, rbg, gbg, bbg;
226 int rscale, gscale, bscale;
227 int x, y, xmax, ymax, bitIndex, byteIndex, xorIndex;
228 unsigned char *theMask, *theImage, theChar;
229 int threshold, fgBits, bgBits, bitShifted;
230 BYTE pXorBits[128]; /* Up to 32x32 icons */
232 switch (ptr->bBitsPerPixel)
247 FIXME("Currently no support for cursors with %d bits per pixel\n",
249 XFreePixmap( display, pixmapAll );
250 XFreeGC( display, gc );
252 XDestroyImage( image );
255 /* The location of the mask. */
256 theMask = (char *)(ptr + 1);
257 /* The mask should still be 1 bit per pixel. The color image
258 * should immediately follow the mask.
260 theImage = &theMask[ptr->nWidth/8 * ptr->nHeight];
261 rfg = gfg = bfg = rbg = gbg = bbg = 0;
267 xmax = (ptr->nWidth > 32) ? 32 : ptr->nWidth;
268 if (ptr->nWidth > 32) {
269 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
270 ptr->nWidth, ptr->nHeight);
272 ymax = (ptr->nHeight > 32) ? 32 : ptr->nHeight;
274 memset(pXorBits, 0, 128);
275 for (y=0; y<ymax; y++)
277 for (x=0; x<xmax; x++)
279 red = green = blue = 0;
280 switch (ptr->bBitsPerPixel)
283 theChar = theImage[byteIndex++];
285 theChar = theImage[byteIndex++];
287 theChar = theImage[byteIndex++];
291 theChar = theImage[byteIndex++];
292 blue = theChar & 0x1F;
293 green = (theChar & 0xE0) >> 5;
294 theChar = theImage[byteIndex++];
295 green |= (theChar & 0x07) << 3;
296 red = (theChar & 0xF8) >> 3;
300 if (red+green+blue > threshold)
306 pXorBits[xorIndex] |= bitShifted;
320 bitShifted = bitShifted << 1;
323 rscale = 1 << (16 - rbits);
324 gscale = 1 << (16 - gbits);
325 bscale = 1 << (16 - bbits);
328 fg.red = rfg * rscale / fgBits;
329 fg.green = gfg * gscale / fgBits;
330 fg.blue = bfg * bscale / fgBits;
332 else fg.red = fg.green = fg.blue = 0;
333 bgBits = xmax * ymax - fgBits;
336 bg.red = rbg * rscale / bgBits;
337 bg.green = gbg * gscale / bgBits;
338 bg.blue = bbg * bscale / bgBits;
340 else bg.red = bg.green = bg.blue = 0;
341 pixmapBits = XCreateBitmapFromData( display, root_window, pXorBits, xmax, ymax );
344 XFreePixmap( display, pixmapAll );
345 XFreeGC( display, gc );
347 XDestroyImage( image );
352 XPutImage( display, pixmapAll, gc, image,
353 0, 0, 0, 0, ptr->nWidth, ptr->nHeight );
354 XSetFunction( display, gc, GXcopy );
356 XCopyArea( display, pixmapBits, pixmapAll, gc,
357 0, 0, xmax, ymax, 0, ptr->nHeight );
358 XFreePixmap( display, pixmapBits );
361 XDestroyImage( image );
363 /* Now create the 2 pixmaps for bits and mask */
365 pixmapBits = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
366 pixmapMask = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
367 pixmapMaskInv = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
369 /* Make sure everything went OK so far */
371 if (pixmapBits && pixmapMask && pixmapMaskInv)
375 /* We have to do some magic here, as cursors are not fully
376 * compatible between Windows and X11. Under X11, there
377 * are only 3 possible color cursor: black, white and
378 * masked. So we map the 4th Windows color (invert the
379 * bits on the screen) to black and an additional white bit on
380 * an other place (+1,+1). This require some boolean arithmetic:
383 * And Xor Result | Bits Mask Result
384 * 0 0 black | 0 1 background
385 * 0 1 white | 1 1 foreground
386 * 1 0 no change | X 0 no change
387 * 1 1 inverted | 0 1 background
390 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
391 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
393 * FIXME: apparently some servers do support 'inverted' color.
394 * I don't know if it's correct per the X spec, but maybe
395 * we ought to take advantage of it. -- AJ
397 XSetFunction( display, gc, GXcopy );
398 XCopyArea( display, pixmapAll, pixmapBits, gc,
399 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
400 XCopyArea( display, pixmapAll, pixmapMask, gc,
401 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
402 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
403 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
404 XSetFunction( display, gc, GXand );
405 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
406 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
407 XSetFunction( display, gc, GXandReverse );
408 XCopyArea( display, pixmapAll, pixmapBits, gc,
409 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
410 XSetFunction( display, gc, GXorReverse );
411 XCopyArea( display, pixmapAll, pixmapMask, gc,
412 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
413 /* Additional white */
414 XSetFunction( display, gc, GXor );
415 XCopyArea( display, pixmapMaskInv, pixmapMask, gc,
416 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
417 XCopyArea( display, pixmapMaskInv, pixmapBits, gc,
418 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
419 XSetFunction( display, gc, GXcopy );
421 /* Make sure hotspot is valid */
422 hotspot.x = ptr->ptHotSpot.x;
423 hotspot.y = ptr->ptHotSpot.y;
424 if (hotspot.x < 0 || hotspot.x >= ptr->nWidth ||
425 hotspot.y < 0 || hotspot.y >= ptr->nHeight)
427 hotspot.x = ptr->nWidth / 2;
428 hotspot.y = ptr->nHeight / 2;
430 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
431 &fg, &bg, hotspot.x, hotspot.y );
434 /* Now free everything */
436 if (pixmapAll) XFreePixmap( display, pixmapAll );
437 if (pixmapBits) XFreePixmap( display, pixmapBits );
438 if (pixmapMask) XFreePixmap( display, pixmapMask );
439 if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
440 XFreeGC( display, gc );
446 /***********************************************************************
447 * SetCursor (X11DRV.@)
449 void X11DRV_SetCursor( CURSORICONINFO *lpCursor )
453 if (root_window != DefaultRootWindow(gdi_display))
455 /* If in desktop mode, set the cursor on the desktop window */
458 cursor = create_cursor( gdi_display, lpCursor );
461 XDefineCursor( gdi_display, root_window, cursor );
462 /* Make the change take effect immediately */
464 XFreeCursor( gdi_display, cursor );
468 else /* set the same cursor for all top-level windows of the current thread */
470 struct x11drv_thread_data *data = x11drv_thread_data();
473 cursor = create_cursor( data->display, lpCursor );
476 if (data->cursor) XFreeCursor( data->display, data->cursor );
477 data->cursor = cursor;
478 if (data->cursor_window)
480 XDefineCursor( data->display, data->cursor_window, cursor );
481 /* Make the change take effect immediately */
482 XFlush( data->display );
489 /***********************************************************************
490 * SetCursorPos (X11DRV.@)
492 void X11DRV_SetCursorPos( INT x, INT y )
494 Display *display = thread_display();
496 TRACE( "warping to (%d,%d)\n", x, y );
499 XWarpPointer( display, root_window, root_window, 0, 0, 0, 0, x, y );
500 XFlush( display ); /* just in case */
504 /***********************************************************************
505 * GetCursorPos (X11DRV.@)
507 void X11DRV_GetCursorPos(LPPOINT pos)
509 Display *display = thread_display();
511 int rootX, rootY, winX, winY;
515 if (XQueryPointer( display, root_window, &root, &child,
516 &rootX, &rootY, &winX, &winY, &xstate ))
518 update_key_state( xstate );
519 update_button_state( xstate );
520 TRACE("pointer at (%d,%d)\n", winX, winY );
527 /***********************************************************************
528 * InitMouse (X11DRV.@)
530 void X11DRV_InitMouse( BYTE *key_state_table )
532 pKeyStateTable = key_state_table;
536 /***********************************************************************
539 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
541 XButtonEvent *event = &xev->xbutton;
542 int buttonNum = event->button - 1;
546 if (buttonNum >= NB_BUTTONS) return;
549 update_cursor( hwnd, event->window );
550 get_coords( hwnd, event->window, event->x, event->y, &pt );
558 wData = -WHEEL_DELTA;
561 update_key_state( event->state );
562 send_mouse_event( hwnd, button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE,
563 pt.x, pt.y, wData, event->time );
567 /***********************************************************************
568 * X11DRV_ButtonRelease
570 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
572 XButtonEvent *event = &xev->xbutton;
573 int buttonNum = event->button - 1;
576 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
579 update_cursor( hwnd, event->window );
580 get_coords( hwnd, event->window, event->x, event->y, &pt );
581 update_key_state( event->state );
582 send_mouse_event( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE,
583 pt.x, pt.y, 0, event->time );
587 /***********************************************************************
588 * X11DRV_MotionNotify
590 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
592 XMotionEvent *event = &xev->xmotion;
595 TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);
599 update_cursor( hwnd, event->window );
600 get_coords( hwnd, event->window, event->x, event->y, &pt );
601 update_key_state( event->state );
602 send_mouse_event( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
603 pt.x, pt.y, 0, event->time );
607 /***********************************************************************
610 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
612 XCrossingEvent *event = &xev->xcrossing;
615 TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);
618 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
620 /* simulate a mouse motion event */
621 update_cursor( hwnd, event->window );
622 get_coords( hwnd, event->window, event->x, event->y, &pt );
623 update_key_state( event->state );
624 send_mouse_event( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
625 pt.x, pt.y, 0, event->time );
629 #ifdef HAVE_LIBXXF86DGA2
633 /**********************************************************************
634 * X11DRV_DGAMotionEvent
636 void X11DRV_DGAMotionEvent( HWND hwnd, XEvent *xev )
638 XDGAMotionEvent *event = (XDGAMotionEvent *)xev;
639 update_key_state( event->state );
640 send_mouse_event( DGAhwnd, MOUSEEVENTF_MOVE, event->dx, event->dy, 0, event->time );
643 /**********************************************************************
644 * X11DRV_DGAButtonPressEvent
646 void X11DRV_DGAButtonPressEvent( HWND hwnd, XEvent *xev )
648 XDGAButtonEvent *event = (XDGAButtonEvent *)xev;
649 int buttonNum = event->button - 1;
651 if (buttonNum >= NB_BUTTONS) return;
652 update_key_state( event->state );
653 send_mouse_event( DGAhwnd, button_down_flags[buttonNum], 0, 0, 0, event->time );
656 /**********************************************************************
657 * X11DRV_DGAButtonReleaseEvent
659 void X11DRV_DGAButtonReleaseEvent( HWND hwnd, XEvent *xev )
661 XDGAButtonEvent *event = (XDGAButtonEvent *)xev;
662 int buttonNum = event->button - 1;
664 if (buttonNum >= NB_BUTTONS) return;
665 update_key_state( event->state );
666 send_mouse_event( DGAhwnd, button_up_flags[buttonNum], 0, 0, 0, event->time );
669 #endif /* HAVE_LIBXXF86DGA2 */