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>
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
31 #include "wine/winuser16.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(cursor
);
38 /**********************************************************************/
40 #define NB_BUTTONS 5 /* Windows can handle 3 buttons and the wheel too */
42 static const UINT button_down_flags
[NB_BUTTONS
] =
45 MOUSEEVENTF_MIDDLEDOWN
,
46 MOUSEEVENTF_RIGHTDOWN
,
51 static const UINT button_up_flags
[NB_BUTTONS
] =
60 static BYTE
*pKeyStateTable
;
63 /***********************************************************************
66 * get the coordinates of a mouse event
68 static void get_coords( HWND
*hwnd
, Window window
, int x
, int y
, POINT
*pt
)
70 struct x11drv_win_data
*data
;
73 if (!(win
= WIN_GetPtr( *hwnd
)) || win
== WND_OTHER_PROCESS
) return;
74 data
= win
->pDriverData
;
76 if (window
== data
->whole_window
)
78 x
-= data
->client_rect
.left
;
79 y
-= data
->client_rect
.top
;
81 WIN_ReleasePtr( win
);
85 if (*hwnd
!= GetDesktopWindow())
87 ClientToScreen( *hwnd
, pt
);
88 *hwnd
= GetAncestor( *hwnd
, GA_ROOT
);
93 /***********************************************************************
96 * Update the button state with what X provides us
98 static void update_button_state( unsigned int state
)
100 pKeyStateTable
[VK_LBUTTON
] = (state
& Button1Mask
? 0x80 : 0);
101 pKeyStateTable
[VK_MBUTTON
] = (state
& Button2Mask
? 0x80 : 0);
102 pKeyStateTable
[VK_RBUTTON
] = (state
& Button3Mask
? 0x80 : 0);
106 /***********************************************************************
109 * Update the key state with what X provides us
111 static void update_key_state( unsigned int state
)
113 pKeyStateTable
[VK_SHIFT
] = (state
& ShiftMask
? 0x80 : 0);
114 pKeyStateTable
[VK_CONTROL
] = (state
& ControlMask
? 0x80 : 0);
118 /***********************************************************************
121 static void send_mouse_event( HWND hwnd
, DWORD flags
, DWORD posX
, DWORD posY
,
122 DWORD data
, Time time
)
126 TRACE("(%04lX,%ld,%ld)\n", flags
, posX
, posY
);
128 if (flags
& MOUSEEVENTF_ABSOLUTE
)
130 int width
= GetSystemMetrics( SM_CXSCREEN
);
131 int height
= GetSystemMetrics( SM_CYSCREEN
);
132 /* Relative mouse movements seem not to be scaled as absolute ones */
133 posX
= (((long)posX
<< 16) + width
-1) / width
;
134 posY
= (((long)posY
<< 16) + height
-1) / height
;
137 input
.type
= WINE_INTERNAL_INPUT_MOUSE
;
138 input
.u
.mi
.dx
= posX
;
139 input
.u
.mi
.dy
= posY
;
140 input
.u
.mi
.mouseData
= data
;
141 input
.u
.mi
.dwFlags
= flags
;
142 input
.u
.mi
.time
= time
- X11DRV_server_startticks
;
143 input
.u
.mi
.dwExtraInfo
= (ULONG_PTR
)hwnd
;
144 SendInput( 1, &input
, sizeof(input
) );
148 /***********************************************************************
151 * Update the cursor of a window on a mouse event.
153 static void update_cursor( HWND hwnd
, Window win
)
155 struct x11drv_thread_data
*data
= x11drv_thread_data();
157 if (win
== X11DRV_get_client_window( hwnd
))
158 win
= X11DRV_get_whole_window( hwnd
); /* always set cursor on whole window */
160 if (data
->cursor_window
!= win
)
162 data
->cursor_window
= win
;
163 if (data
->cursor
) TSXDefineCursor( data
->display
, win
, data
->cursor
);
168 /***********************************************************************
171 * Create an X cursor from a Windows one.
173 static Cursor
create_cursor( Display
*display
, CURSORICONINFO
*ptr
)
175 Pixmap pixmapBits
, pixmapMask
, pixmapMaskInv
, pixmapAll
;
177 Cursor cursor
= None
;
179 if (!ptr
) /* Create an empty cursor */
181 static const char data
[] = { 0 };
183 bg
.red
= bg
.green
= bg
.blue
= 0x0000;
184 pixmapBits
= XCreateBitmapFromData( display
, root_window
, data
, 1, 1 );
187 cursor
= XCreatePixmapCursor( display
, pixmapBits
, pixmapBits
,
189 XFreePixmap( display
, pixmapBits
);
192 else /* Create the X cursor from the bits */
197 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
198 ptr
->nWidth
, ptr
->nHeight
, ptr
->bPlanes
, ptr
->bBitsPerPixel
,
200 /* Create a pixmap and transfer all the bits to it */
202 /* NOTE: Following hack works, but only because XFree depth
203 * 1 images really use 1 bit/pixel (and so the same layout
204 * as the Windows cursor data). Perhaps use a more generic
207 /* This pixmap will be written with two bitmaps. The first is
208 * the mask and the second is the image.
210 if (!(pixmapAll
= XCreatePixmap( display
, root_window
,
211 ptr
->nWidth
, ptr
->nHeight
* 2, 1 )))
213 if (!(image
= XCreateImage( display
, visual
,
214 1, ZPixmap
, 0, (char *)(ptr
+ 1), ptr
->nWidth
,
215 ptr
->nHeight
* 2, 16, ptr
->nWidthBytes
/ptr
->bBitsPerPixel
)))
217 XFreePixmap( display
, pixmapAll
);
220 gc
= XCreateGC( display
, pixmapAll
, 0, NULL
);
221 XSetGraphicsExposures( display
, gc
, False
);
222 image
->byte_order
= MSBFirst
;
223 image
->bitmap_bit_order
= MSBFirst
;
224 image
->bitmap_unit
= 16;
225 _XInitImageFuncPtrs(image
);
226 if (ptr
->bPlanes
* ptr
->bBitsPerPixel
== 1)
228 /* A plain old white on black cursor. */
229 fg
.red
= fg
.green
= fg
.blue
= 0xffff;
230 bg
.red
= bg
.green
= bg
.blue
= 0x0000;
231 XPutImage( display
, pixmapAll
, gc
, image
,
232 0, 0, 0, 0, ptr
->nWidth
, ptr
->nHeight
* 2 );
236 int rbits
, gbits
, bbits
, red
, green
, blue
;
237 int rfg
, gfg
, bfg
, rbg
, gbg
, bbg
;
238 int rscale
, gscale
, bscale
;
239 int x
, y
, xmax
, ymax
, bitIndex
, byteIndex
, xorIndex
;
240 unsigned char *theMask
, *theImage
, theChar
;
241 int threshold
, fgBits
, bgBits
, bitShifted
;
242 BYTE pXorBits
[128]; /* Up to 32x32 icons */
244 switch (ptr
->bBitsPerPixel
)
259 FIXME("Currently no support for cursors with %d bits per pixel\n",
261 XFreePixmap( display
, pixmapAll
);
262 XFreeGC( display
, gc
);
264 XDestroyImage( image
);
267 /* The location of the mask. */
268 theMask
= (char *)(ptr
+ 1);
269 /* The mask should still be 1 bit per pixel. The color image
270 * should immediately follow the mask.
272 theImage
= &theMask
[ptr
->nWidth
/8 * ptr
->nHeight
];
273 rfg
= gfg
= bfg
= rbg
= gbg
= bbg
= 0;
279 xmax
= (ptr
->nWidth
> 32) ? 32 : ptr
->nWidth
;
280 if (ptr
->nWidth
> 32) {
281 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
282 ptr
->nWidth
, ptr
->nHeight
);
284 ymax
= (ptr
->nHeight
> 32) ? 32 : ptr
->nHeight
;
286 memset(pXorBits
, 0, 128);
287 for (y
=0; y
<ymax
; y
++)
289 for (x
=0; x
<xmax
; x
++)
291 red
= green
= blue
= 0;
292 switch (ptr
->bBitsPerPixel
)
295 theChar
= theImage
[byteIndex
++];
297 theChar
= theImage
[byteIndex
++];
299 theChar
= theImage
[byteIndex
++];
303 theChar
= theImage
[byteIndex
++];
304 blue
= theChar
& 0x1F;
305 green
= (theChar
& 0xE0) >> 5;
306 theChar
= theImage
[byteIndex
++];
307 green
|= (theChar
& 0x07) << 3;
308 red
= (theChar
& 0xF8) >> 3;
312 if (red
+green
+blue
> threshold
)
318 pXorBits
[xorIndex
] |= bitShifted
;
332 bitShifted
= bitShifted
<< 1;
335 rscale
= 1 << (16 - rbits
);
336 gscale
= 1 << (16 - gbits
);
337 bscale
= 1 << (16 - bbits
);
340 fg
.red
= rfg
* rscale
/ fgBits
;
341 fg
.green
= gfg
* gscale
/ fgBits
;
342 fg
.blue
= bfg
* bscale
/ fgBits
;
344 else fg
.red
= fg
.green
= fg
.blue
= 0;
345 bgBits
= xmax
* ymax
- fgBits
;
348 bg
.red
= rbg
* rscale
/ bgBits
;
349 bg
.green
= gbg
* gscale
/ bgBits
;
350 bg
.blue
= bbg
* bscale
/ bgBits
;
352 else bg
.red
= bg
.green
= bg
.blue
= 0;
353 pixmapBits
= XCreateBitmapFromData( display
, root_window
, pXorBits
, xmax
, ymax
);
356 XFreePixmap( display
, pixmapAll
);
357 XFreeGC( display
, gc
);
359 XDestroyImage( image
);
364 XPutImage( display
, pixmapAll
, gc
, image
,
365 0, 0, 0, 0, ptr
->nWidth
, ptr
->nHeight
);
366 XSetFunction( display
, gc
, GXcopy
);
368 XCopyArea( display
, pixmapBits
, pixmapAll
, gc
,
369 0, 0, xmax
, ymax
, 0, ptr
->nHeight
);
370 XFreePixmap( display
, pixmapBits
);
373 XDestroyImage( image
);
375 /* Now create the 2 pixmaps for bits and mask */
377 pixmapBits
= XCreatePixmap( display
, root_window
, ptr
->nWidth
, ptr
->nHeight
, 1 );
378 pixmapMask
= XCreatePixmap( display
, root_window
, ptr
->nWidth
, ptr
->nHeight
, 1 );
379 pixmapMaskInv
= XCreatePixmap( display
, root_window
, ptr
->nWidth
, ptr
->nHeight
, 1 );
381 /* Make sure everything went OK so far */
383 if (pixmapBits
&& pixmapMask
&& pixmapMaskInv
)
387 /* We have to do some magic here, as cursors are not fully
388 * compatible between Windows and X11. Under X11, there
389 * are only 3 possible color cursor: black, white and
390 * masked. So we map the 4th Windows color (invert the
391 * bits on the screen) to black and an additional white bit on
392 * an other place (+1,+1). This require some boolean arithmetic:
395 * And Xor Result | Bits Mask Result
396 * 0 0 black | 0 1 background
397 * 0 1 white | 1 1 foreground
398 * 1 0 no change | X 0 no change
399 * 1 1 inverted | 0 1 background
402 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
403 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
405 * FIXME: apparently some servers do support 'inverted' color.
406 * I don't know if it's correct per the X spec, but maybe
407 * we ought to take advantage of it. -- AJ
409 XSetFunction( display
, gc
, GXcopy
);
410 XCopyArea( display
, pixmapAll
, pixmapBits
, gc
,
411 0, 0, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
412 XCopyArea( display
, pixmapAll
, pixmapMask
, gc
,
413 0, 0, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
414 XCopyArea( display
, pixmapAll
, pixmapMaskInv
, gc
,
415 0, 0, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
416 XSetFunction( display
, gc
, GXand
);
417 XCopyArea( display
, pixmapAll
, pixmapMaskInv
, gc
,
418 0, ptr
->nHeight
, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
419 XSetFunction( display
, gc
, GXandReverse
);
420 XCopyArea( display
, pixmapAll
, pixmapBits
, gc
,
421 0, ptr
->nHeight
, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
422 XSetFunction( display
, gc
, GXorReverse
);
423 XCopyArea( display
, pixmapAll
, pixmapMask
, gc
,
424 0, ptr
->nHeight
, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
425 /* Additional white */
426 XSetFunction( display
, gc
, GXor
);
427 XCopyArea( display
, pixmapMaskInv
, pixmapMask
, gc
,
428 0, 0, ptr
->nWidth
, ptr
->nHeight
, 1, 1 );
429 XCopyArea( display
, pixmapMaskInv
, pixmapBits
, gc
,
430 0, 0, ptr
->nWidth
, ptr
->nHeight
, 1, 1 );
431 XSetFunction( display
, gc
, GXcopy
);
433 /* Make sure hotspot is valid */
434 hotspot
.x
= ptr
->ptHotSpot
.x
;
435 hotspot
.y
= ptr
->ptHotSpot
.y
;
436 if (hotspot
.x
< 0 || hotspot
.x
>= ptr
->nWidth
||
437 hotspot
.y
< 0 || hotspot
.y
>= ptr
->nHeight
)
439 hotspot
.x
= ptr
->nWidth
/ 2;
440 hotspot
.y
= ptr
->nHeight
/ 2;
442 cursor
= XCreatePixmapCursor( display
, pixmapBits
, pixmapMask
,
443 &fg
, &bg
, hotspot
.x
, hotspot
.y
);
446 /* Now free everything */
448 if (pixmapAll
) XFreePixmap( display
, pixmapAll
);
449 if (pixmapBits
) XFreePixmap( display
, pixmapBits
);
450 if (pixmapMask
) XFreePixmap( display
, pixmapMask
);
451 if (pixmapMaskInv
) XFreePixmap( display
, pixmapMaskInv
);
452 XFreeGC( display
, gc
);
458 /***********************************************************************
459 * SetCursor (X11DRV.@)
461 void X11DRV_SetCursor( CURSORICONINFO
*lpCursor
)
465 if (root_window
!= DefaultRootWindow(gdi_display
))
467 /* If in desktop mode, set the cursor on the desktop window */
470 cursor
= create_cursor( gdi_display
, lpCursor
);
473 XDefineCursor( gdi_display
, root_window
, cursor
);
474 /* Make the change take effect immediately */
476 XFreeCursor( gdi_display
, cursor
);
480 else /* set the same cursor for all top-level windows of the current thread */
482 struct x11drv_thread_data
*data
= x11drv_thread_data();
485 cursor
= create_cursor( data
->display
, lpCursor
);
488 if (data
->cursor
) XFreeCursor( data
->display
, data
->cursor
);
489 data
->cursor
= cursor
;
490 if (data
->cursor_window
)
492 XDefineCursor( data
->display
, data
->cursor_window
, cursor
);
493 /* Make the change take effect immediately */
494 XFlush( data
->display
);
501 /***********************************************************************
502 * SetCursorPos (X11DRV.@)
504 void X11DRV_SetCursorPos( INT x
, INT y
)
506 Display
*display
= thread_display();
508 TRACE( "warping to (%d,%d)\n", x
, y
);
511 XWarpPointer( display
, root_window
, root_window
, 0, 0, 0, 0, x
, y
);
512 XFlush( display
); /* just in case */
516 /***********************************************************************
517 * GetCursorPos (X11DRV.@)
519 void X11DRV_GetCursorPos(LPPOINT pos
)
521 Display
*display
= thread_display();
523 int rootX
, rootY
, winX
, winY
;
526 if (!TSXQueryPointer( display
, root_window
, &root
, &child
,
527 &rootX
, &rootY
, &winX
, &winY
, &xstate
))
530 update_key_state( xstate
);
531 update_button_state( xstate
);
532 TRACE("pointer at (%d,%d)\n", winX
, winY
);
537 /***********************************************************************
538 * InitMouse (X11DRV.@)
540 void X11DRV_InitMouse( BYTE
*key_state_table
)
542 pKeyStateTable
= key_state_table
;
546 /***********************************************************************
549 void X11DRV_ButtonPress( HWND hwnd
, XButtonEvent
*event
)
551 int buttonNum
= event
->button
- 1;
555 if (buttonNum
>= NB_BUTTONS
) return;
558 update_cursor( hwnd
, event
->window
);
559 get_coords( &hwnd
, event
->window
, event
->x
, event
->y
, &pt
);
567 wData
= -WHEEL_DELTA
;
570 update_key_state( event
->state
);
571 send_mouse_event( hwnd
, button_down_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
,
572 pt
.x
, pt
.y
, wData
, event
->time
);
576 /***********************************************************************
577 * X11DRV_ButtonRelease
579 void X11DRV_ButtonRelease( HWND hwnd
, XButtonEvent
*event
)
581 int buttonNum
= event
->button
- 1;
584 if (buttonNum
>= NB_BUTTONS
|| !button_up_flags
[buttonNum
]) return;
587 update_cursor( hwnd
, event
->window
);
588 get_coords( &hwnd
, event
->window
, event
->x
, event
->y
, &pt
);
589 update_key_state( event
->state
);
590 send_mouse_event( hwnd
, button_up_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
,
591 pt
.x
, pt
.y
, 0, event
->time
);
595 /***********************************************************************
596 * X11DRV_MotionNotify
598 void X11DRV_MotionNotify( HWND hwnd
, XMotionEvent
*event
)
604 update_cursor( hwnd
, event
->window
);
605 get_coords( &hwnd
, event
->window
, event
->x
, event
->y
, &pt
);
606 update_key_state( event
->state
);
607 send_mouse_event( hwnd
, MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
,
608 pt
.x
, pt
.y
, 0, event
->time
);
612 /***********************************************************************
615 void X11DRV_EnterNotify( HWND hwnd
, XCrossingEvent
*event
)
619 if (event
->detail
== NotifyVirtual
|| event
->detail
== NotifyNonlinearVirtual
) return;
622 /* simulate a mouse motion event */
623 update_cursor( hwnd
, event
->window
);
624 get_coords( &hwnd
, event
->window
, event
->x
, event
->y
, &pt
);
625 update_key_state( event
->state
);
626 send_mouse_event( hwnd
, MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
,
627 pt
.x
, pt
.y
, 0, event
->time
);
631 #ifdef HAVE_LIBXXF86DGA2
632 /**********************************************************************
633 * X11DRV_DGAMotionEvent
635 void X11DRV_DGAMotionEvent( HWND hwnd
, XDGAMotionEvent
*event
)
637 update_key_state( event
->state
);
638 send_mouse_event( hwnd
, MOUSEEVENTF_MOVE
, event
->dx
, event
->dy
, 0, event
->time
);
641 /**********************************************************************
642 * X11DRV_DGAButtonPressEvent
644 void X11DRV_DGAButtonPressEvent( HWND hwnd
, XDGAButtonEvent
*event
)
646 int buttonNum
= event
->button
- 1;
648 if (buttonNum
>= NB_BUTTONS
) return;
649 update_key_state( event
->state
);
650 send_mouse_event( hwnd
, button_down_flags
[buttonNum
], 0, 0, 0, event
->time
);
653 /**********************************************************************
654 * X11DRV_DGAButtonReleaseEvent
656 void X11DRV_DGAButtonReleaseEvent( HWND hwnd
, XDGAButtonEvent
*event
)
658 int buttonNum
= event
->button
- 1;
660 if (buttonNum
>= NB_BUTTONS
) return;
661 update_key_state( event
->state
);
662 send_mouse_event( hwnd
, button_up_flags
[buttonNum
], 0, 0, 0, event
->time
);
664 #endif /* HAVE_LIBXXF86DGA2 */