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 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 if (window
== data
->whole_window
)
79 x
-= data
->client_rect
.left
;
80 y
-= data
->client_rect
.top
;
84 if (*hwnd
!= GetDesktopWindow())
86 ClientToScreen( *hwnd
, pt
);
87 *hwnd
= GetAncestor( *hwnd
, GA_ROOT
);
92 /***********************************************************************
95 * Update the button state with what X provides us
97 static void update_button_state( unsigned int state
)
99 pKeyStateTable
[VK_LBUTTON
] = (state
& Button1Mask
? 0x80 : 0);
100 pKeyStateTable
[VK_MBUTTON
] = (state
& Button2Mask
? 0x80 : 0);
101 pKeyStateTable
[VK_RBUTTON
] = (state
& Button3Mask
? 0x80 : 0);
105 /***********************************************************************
108 * Update the key state with what X provides us
110 static void update_key_state( unsigned int state
)
112 pKeyStateTable
[VK_SHIFT
] = (state
& ShiftMask
? 0x80 : 0);
113 pKeyStateTable
[VK_CONTROL
] = (state
& ControlMask
? 0x80 : 0);
117 /***********************************************************************
120 static void send_mouse_event( HWND hwnd
, DWORD flags
, DWORD posX
, DWORD posY
,
121 DWORD data
, Time time
)
125 TRACE("(%04lX,%ld,%ld)\n", flags
, posX
, posY
);
127 if (flags
& MOUSEEVENTF_ABSOLUTE
)
129 int width
= GetSystemMetrics( SM_CXSCREEN
);
130 int height
= GetSystemMetrics( SM_CYSCREEN
);
131 /* Relative mouse movements seem not to be scaled as absolute ones */
132 posX
= (((long)posX
<< 16) + width
-1) / width
;
133 posY
= (((long)posY
<< 16) + height
-1) / height
;
136 input
.type
= WINE_INTERNAL_INPUT_MOUSE
;
137 input
.u
.mi
.dx
= posX
;
138 input
.u
.mi
.dy
= posY
;
139 input
.u
.mi
.mouseData
= data
;
140 input
.u
.mi
.dwFlags
= flags
;
141 input
.u
.mi
.time
= EVENT_x11_time_to_win32_time(time
);
142 input
.u
.mi
.dwExtraInfo
= (ULONG_PTR
)hwnd
;
143 SendInput( 1, &input
, sizeof(input
) );
147 /***********************************************************************
150 * Update the cursor of a window on a mouse event.
152 static void update_cursor( HWND hwnd
, Window win
)
154 struct x11drv_thread_data
*data
= x11drv_thread_data();
156 if (win
== X11DRV_get_client_window( hwnd
))
157 win
= X11DRV_get_whole_window( hwnd
); /* always set cursor on whole window */
159 if (data
->cursor_window
!= win
)
161 data
->cursor_window
= win
;
163 if (data
->cursor
) XDefineCursor( data
->display
, win
, data
->cursor
);
169 /***********************************************************************
172 * Create an X cursor from a Windows one.
174 static Cursor
create_cursor( Display
*display
, CURSORICONINFO
*ptr
)
176 Pixmap pixmapBits
, pixmapMask
, pixmapMaskInv
, pixmapAll
;
178 Cursor cursor
= None
;
180 if (!ptr
) /* Create an empty cursor */
182 static const char data
[] = { 0 };
184 bg
.red
= bg
.green
= bg
.blue
= 0x0000;
185 pixmapBits
= XCreateBitmapFromData( display
, root_window
, data
, 1, 1 );
188 cursor
= XCreatePixmapCursor( display
, pixmapBits
, pixmapBits
,
190 XFreePixmap( display
, pixmapBits
);
193 else /* Create the X cursor from the bits */
198 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
199 ptr
->nWidth
, ptr
->nHeight
, ptr
->bPlanes
, ptr
->bBitsPerPixel
,
201 /* Create a pixmap and transfer all the bits to it */
203 /* NOTE: Following hack works, but only because XFree depth
204 * 1 images really use 1 bit/pixel (and so the same layout
205 * as the Windows cursor data). Perhaps use a more generic
208 /* This pixmap will be written with two bitmaps. The first is
209 * the mask and the second is the image.
211 if (!(pixmapAll
= XCreatePixmap( display
, root_window
,
212 ptr
->nWidth
, ptr
->nHeight
* 2, 1 )))
214 if (!(image
= XCreateImage( display
, visual
,
215 1, ZPixmap
, 0, (char *)(ptr
+ 1), ptr
->nWidth
,
216 ptr
->nHeight
* 2, 16, ptr
->nWidthBytes
/ptr
->bBitsPerPixel
)))
218 XFreePixmap( display
, pixmapAll
);
221 gc
= XCreateGC( display
, pixmapAll
, 0, NULL
);
222 XSetGraphicsExposures( display
, gc
, False
);
223 image
->byte_order
= MSBFirst
;
224 image
->bitmap_bit_order
= MSBFirst
;
225 image
->bitmap_unit
= 16;
226 _XInitImageFuncPtrs(image
);
227 if (ptr
->bPlanes
* ptr
->bBitsPerPixel
== 1)
229 /* A plain old white on black cursor. */
230 fg
.red
= fg
.green
= fg
.blue
= 0xffff;
231 bg
.red
= bg
.green
= bg
.blue
= 0x0000;
232 XPutImage( display
, pixmapAll
, gc
, image
,
233 0, 0, 0, 0, ptr
->nWidth
, ptr
->nHeight
* 2 );
237 int rbits
, gbits
, bbits
, red
, green
, blue
;
238 int rfg
, gfg
, bfg
, rbg
, gbg
, bbg
;
239 int rscale
, gscale
, bscale
;
240 int x
, y
, xmax
, ymax
, bitIndex
, byteIndex
, xorIndex
;
241 unsigned char *theMask
, *theImage
, theChar
;
242 int threshold
, fgBits
, bgBits
, bitShifted
;
243 BYTE pXorBits
[128]; /* Up to 32x32 icons */
245 switch (ptr
->bBitsPerPixel
)
260 FIXME("Currently no support for cursors with %d bits per pixel\n",
262 XFreePixmap( display
, pixmapAll
);
263 XFreeGC( display
, gc
);
265 XDestroyImage( image
);
268 /* The location of the mask. */
269 theMask
= (char *)(ptr
+ 1);
270 /* The mask should still be 1 bit per pixel. The color image
271 * should immediately follow the mask.
273 theImage
= &theMask
[ptr
->nWidth
/8 * ptr
->nHeight
];
274 rfg
= gfg
= bfg
= rbg
= gbg
= bbg
= 0;
280 xmax
= (ptr
->nWidth
> 32) ? 32 : ptr
->nWidth
;
281 if (ptr
->nWidth
> 32) {
282 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
283 ptr
->nWidth
, ptr
->nHeight
);
285 ymax
= (ptr
->nHeight
> 32) ? 32 : ptr
->nHeight
;
287 memset(pXorBits
, 0, 128);
288 for (y
=0; y
<ymax
; y
++)
290 for (x
=0; x
<xmax
; x
++)
292 red
= green
= blue
= 0;
293 switch (ptr
->bBitsPerPixel
)
296 theChar
= theImage
[byteIndex
++];
298 theChar
= theImage
[byteIndex
++];
300 theChar
= theImage
[byteIndex
++];
304 theChar
= theImage
[byteIndex
++];
305 blue
= theChar
& 0x1F;
306 green
= (theChar
& 0xE0) >> 5;
307 theChar
= theImage
[byteIndex
++];
308 green
|= (theChar
& 0x07) << 3;
309 red
= (theChar
& 0xF8) >> 3;
313 if (red
+green
+blue
> threshold
)
319 pXorBits
[xorIndex
] |= bitShifted
;
333 bitShifted
= bitShifted
<< 1;
336 rscale
= 1 << (16 - rbits
);
337 gscale
= 1 << (16 - gbits
);
338 bscale
= 1 << (16 - bbits
);
341 fg
.red
= rfg
* rscale
/ fgBits
;
342 fg
.green
= gfg
* gscale
/ fgBits
;
343 fg
.blue
= bfg
* bscale
/ fgBits
;
345 else fg
.red
= fg
.green
= fg
.blue
= 0;
346 bgBits
= xmax
* ymax
- fgBits
;
349 bg
.red
= rbg
* rscale
/ bgBits
;
350 bg
.green
= gbg
* gscale
/ bgBits
;
351 bg
.blue
= bbg
* bscale
/ bgBits
;
353 else bg
.red
= bg
.green
= bg
.blue
= 0;
354 pixmapBits
= XCreateBitmapFromData( display
, root_window
, pXorBits
, xmax
, ymax
);
357 XFreePixmap( display
, pixmapAll
);
358 XFreeGC( display
, gc
);
360 XDestroyImage( image
);
365 XPutImage( display
, pixmapAll
, gc
, image
,
366 0, 0, 0, 0, ptr
->nWidth
, ptr
->nHeight
);
367 XSetFunction( display
, gc
, GXcopy
);
369 XCopyArea( display
, pixmapBits
, pixmapAll
, gc
,
370 0, 0, xmax
, ymax
, 0, ptr
->nHeight
);
371 XFreePixmap( display
, pixmapBits
);
374 XDestroyImage( image
);
376 /* Now create the 2 pixmaps for bits and mask */
378 pixmapBits
= XCreatePixmap( display
, root_window
, ptr
->nWidth
, ptr
->nHeight
, 1 );
379 pixmapMask
= XCreatePixmap( display
, root_window
, ptr
->nWidth
, ptr
->nHeight
, 1 );
380 pixmapMaskInv
= XCreatePixmap( display
, root_window
, ptr
->nWidth
, ptr
->nHeight
, 1 );
382 /* Make sure everything went OK so far */
384 if (pixmapBits
&& pixmapMask
&& pixmapMaskInv
)
388 /* We have to do some magic here, as cursors are not fully
389 * compatible between Windows and X11. Under X11, there
390 * are only 3 possible color cursor: black, white and
391 * masked. So we map the 4th Windows color (invert the
392 * bits on the screen) to black and an additional white bit on
393 * an other place (+1,+1). This require some boolean arithmetic:
396 * And Xor Result | Bits Mask Result
397 * 0 0 black | 0 1 background
398 * 0 1 white | 1 1 foreground
399 * 1 0 no change | X 0 no change
400 * 1 1 inverted | 0 1 background
403 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
404 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
406 * FIXME: apparently some servers do support 'inverted' color.
407 * I don't know if it's correct per the X spec, but maybe
408 * we ought to take advantage of it. -- AJ
410 XSetFunction( display
, gc
, GXcopy
);
411 XCopyArea( display
, pixmapAll
, pixmapBits
, gc
,
412 0, 0, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
413 XCopyArea( display
, pixmapAll
, pixmapMask
, gc
,
414 0, 0, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
415 XCopyArea( display
, pixmapAll
, pixmapMaskInv
, gc
,
416 0, 0, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
417 XSetFunction( display
, gc
, GXand
);
418 XCopyArea( display
, pixmapAll
, pixmapMaskInv
, gc
,
419 0, ptr
->nHeight
, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
420 XSetFunction( display
, gc
, GXandReverse
);
421 XCopyArea( display
, pixmapAll
, pixmapBits
, gc
,
422 0, ptr
->nHeight
, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
423 XSetFunction( display
, gc
, GXorReverse
);
424 XCopyArea( display
, pixmapAll
, pixmapMask
, gc
,
425 0, ptr
->nHeight
, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
426 /* Additional white */
427 XSetFunction( display
, gc
, GXor
);
428 XCopyArea( display
, pixmapMaskInv
, pixmapMask
, gc
,
429 0, 0, ptr
->nWidth
, ptr
->nHeight
, 1, 1 );
430 XCopyArea( display
, pixmapMaskInv
, pixmapBits
, gc
,
431 0, 0, ptr
->nWidth
, ptr
->nHeight
, 1, 1 );
432 XSetFunction( display
, gc
, GXcopy
);
434 /* Make sure hotspot is valid */
435 hotspot
.x
= ptr
->ptHotSpot
.x
;
436 hotspot
.y
= ptr
->ptHotSpot
.y
;
437 if (hotspot
.x
< 0 || hotspot
.x
>= ptr
->nWidth
||
438 hotspot
.y
< 0 || hotspot
.y
>= ptr
->nHeight
)
440 hotspot
.x
= ptr
->nWidth
/ 2;
441 hotspot
.y
= ptr
->nHeight
/ 2;
443 cursor
= XCreatePixmapCursor( display
, pixmapBits
, pixmapMask
,
444 &fg
, &bg
, hotspot
.x
, hotspot
.y
);
447 /* Now free everything */
449 if (pixmapAll
) XFreePixmap( display
, pixmapAll
);
450 if (pixmapBits
) XFreePixmap( display
, pixmapBits
);
451 if (pixmapMask
) XFreePixmap( display
, pixmapMask
);
452 if (pixmapMaskInv
) XFreePixmap( display
, pixmapMaskInv
);
453 XFreeGC( display
, gc
);
459 /***********************************************************************
460 * SetCursor (X11DRV.@)
462 void X11DRV_SetCursor( CURSORICONINFO
*lpCursor
)
466 if (root_window
!= DefaultRootWindow(gdi_display
))
468 /* If in desktop mode, set the cursor on the desktop window */
471 cursor
= create_cursor( gdi_display
, lpCursor
);
474 XDefineCursor( gdi_display
, root_window
, cursor
);
475 /* Make the change take effect immediately */
477 XFreeCursor( gdi_display
, cursor
);
481 else /* set the same cursor for all top-level windows of the current thread */
483 struct x11drv_thread_data
*data
= x11drv_thread_data();
486 cursor
= create_cursor( data
->display
, lpCursor
);
489 if (data
->cursor
) XFreeCursor( data
->display
, data
->cursor
);
490 data
->cursor
= cursor
;
491 if (data
->cursor_window
)
493 XDefineCursor( data
->display
, data
->cursor_window
, cursor
);
494 /* Make the change take effect immediately */
495 XFlush( data
->display
);
502 /***********************************************************************
503 * SetCursorPos (X11DRV.@)
505 void X11DRV_SetCursorPos( INT x
, INT y
)
507 Display
*display
= thread_display();
509 TRACE( "warping to (%d,%d)\n", x
, y
);
512 XWarpPointer( display
, root_window
, root_window
, 0, 0, 0, 0, x
, y
);
513 XFlush( display
); /* just in case */
517 /***********************************************************************
518 * GetCursorPos (X11DRV.@)
520 void X11DRV_GetCursorPos(LPPOINT pos
)
522 Display
*display
= thread_display();
524 int rootX
, rootY
, winX
, winY
;
528 if (XQueryPointer( display
, root_window
, &root
, &child
,
529 &rootX
, &rootY
, &winX
, &winY
, &xstate
))
531 update_key_state( xstate
);
532 update_button_state( xstate
);
533 TRACE("pointer at (%d,%d)\n", winX
, winY
);
540 /***********************************************************************
541 * InitMouse (X11DRV.@)
543 void X11DRV_InitMouse( BYTE
*key_state_table
)
545 pKeyStateTable
= key_state_table
;
549 /***********************************************************************
552 void X11DRV_ButtonPress( HWND hwnd
, XButtonEvent
*event
)
554 int buttonNum
= event
->button
- 1;
558 if (buttonNum
>= NB_BUTTONS
) return;
561 update_cursor( hwnd
, event
->window
);
562 get_coords( &hwnd
, event
->window
, event
->x
, event
->y
, &pt
);
570 wData
= -WHEEL_DELTA
;
573 update_key_state( event
->state
);
574 send_mouse_event( hwnd
, button_down_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
,
575 pt
.x
, pt
.y
, wData
, event
->time
);
579 /***********************************************************************
580 * X11DRV_ButtonRelease
582 void X11DRV_ButtonRelease( HWND hwnd
, XButtonEvent
*event
)
584 int buttonNum
= event
->button
- 1;
587 if (buttonNum
>= NB_BUTTONS
|| !button_up_flags
[buttonNum
]) return;
590 update_cursor( hwnd
, event
->window
);
591 get_coords( &hwnd
, event
->window
, event
->x
, event
->y
, &pt
);
592 update_key_state( event
->state
);
593 send_mouse_event( hwnd
, button_up_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
,
594 pt
.x
, pt
.y
, 0, event
->time
);
598 /***********************************************************************
599 * X11DRV_MotionNotify
601 void X11DRV_MotionNotify( HWND hwnd
, XMotionEvent
*event
)
605 TRACE("hwnd %p, event->is_hint %d\n", hwnd
, event
->is_hint
);
609 update_cursor( hwnd
, event
->window
);
610 get_coords( &hwnd
, event
->window
, event
->x
, event
->y
, &pt
);
611 update_key_state( event
->state
);
612 send_mouse_event( hwnd
, MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
,
613 pt
.x
, pt
.y
, 0, event
->time
);
617 /***********************************************************************
620 void X11DRV_EnterNotify( HWND hwnd
, XCrossingEvent
*event
)
624 TRACE("hwnd %p, event->detail %d\n", hwnd
, event
->detail
);
627 if (event
->detail
== NotifyVirtual
|| event
->detail
== NotifyNonlinearVirtual
) return;
629 /* simulate a mouse motion event */
630 update_cursor( hwnd
, event
->window
);
631 get_coords( &hwnd
, event
->window
, event
->x
, event
->y
, &pt
);
632 update_key_state( event
->state
);
633 send_mouse_event( hwnd
, MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
,
634 pt
.x
, pt
.y
, 0, event
->time
);
638 #ifdef HAVE_LIBXXF86DGA2
639 /**********************************************************************
640 * X11DRV_DGAMotionEvent
642 void X11DRV_DGAMotionEvent( HWND hwnd
, XDGAMotionEvent
*event
)
644 update_key_state( event
->state
);
645 send_mouse_event( hwnd
, MOUSEEVENTF_MOVE
, event
->dx
, event
->dy
, 0, event
->time
);
648 /**********************************************************************
649 * X11DRV_DGAButtonPressEvent
651 void X11DRV_DGAButtonPressEvent( HWND hwnd
, XDGAButtonEvent
*event
)
653 int buttonNum
= event
->button
- 1;
655 if (buttonNum
>= NB_BUTTONS
) return;
656 update_key_state( event
->state
);
657 send_mouse_event( hwnd
, button_down_flags
[buttonNum
], 0, 0, 0, event
->time
);
660 /**********************************************************************
661 * X11DRV_DGAButtonReleaseEvent
663 void X11DRV_DGAButtonReleaseEvent( HWND hwnd
, XDGAButtonEvent
*event
)
665 int buttonNum
= event
->button
- 1;
667 if (buttonNum
>= NB_BUTTONS
) return;
668 update_key_state( event
->state
);
669 send_mouse_event( hwnd
, button_up_flags
[buttonNum
], 0, 0, 0, event
->time
);
671 #endif /* HAVE_LIBXXF86DGA2 */