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 #include "wine/winuser16.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(cursor
);
36 /**********************************************************************/
38 #define NB_BUTTONS 5 /* Windows can handle 3 buttons and the wheel too */
40 static const UINT button_down_flags
[NB_BUTTONS
] =
43 MOUSEEVENTF_MIDDLEDOWN
,
44 MOUSEEVENTF_RIGHTDOWN
,
49 static const UINT button_up_flags
[NB_BUTTONS
] =
58 static BYTE
*pKeyStateTable
;
61 /***********************************************************************
64 * get the coordinates of a mouse event
66 static void get_coords( HWND
*hwnd
, Window window
, int x
, int y
, POINT
*pt
)
68 struct x11drv_win_data
*data
;
71 if (!(win
= WIN_GetPtr( *hwnd
)) || win
== WND_OTHER_PROCESS
) return;
72 data
= win
->pDriverData
;
74 if (window
== data
->whole_window
)
76 x
-= data
->client_rect
.left
;
77 y
-= data
->client_rect
.top
;
79 WIN_ReleasePtr( win
);
83 if (*hwnd
!= GetDesktopWindow())
85 ClientToScreen( *hwnd
, pt
);
86 *hwnd
= GetAncestor( *hwnd
, GA_ROOT
);
91 /***********************************************************************
94 * Update the button state with what X provides us
96 static void update_button_state( unsigned int state
)
98 pKeyStateTable
[VK_LBUTTON
] = (state
& Button1Mask
? 0x80 : 0);
99 pKeyStateTable
[VK_MBUTTON
] = (state
& Button2Mask
? 0x80 : 0);
100 pKeyStateTable
[VK_RBUTTON
] = (state
& Button3Mask
? 0x80 : 0);
104 /***********************************************************************
107 * Update the key state with what X provides us
109 static void update_key_state( unsigned int state
)
111 pKeyStateTable
[VK_SHIFT
] = (state
& ShiftMask
? 0x80 : 0);
112 pKeyStateTable
[VK_CONTROL
] = (state
& ControlMask
? 0x80 : 0);
116 /***********************************************************************
119 static void send_mouse_event( HWND hwnd
, DWORD flags
, DWORD posX
, DWORD posY
,
120 DWORD data
, Time time
)
124 TRACE("(%04lX,%ld,%ld)\n", flags
, posX
, posY
);
126 if (flags
& MOUSEEVENTF_ABSOLUTE
)
128 int width
= GetSystemMetrics( SM_CXSCREEN
);
129 int height
= GetSystemMetrics( SM_CYSCREEN
);
130 /* Relative mouse movements seem not to be scaled as absolute ones */
131 posX
= (((long)posX
<< 16) + width
-1) / width
;
132 posY
= (((long)posY
<< 16) + height
-1) / height
;
135 input
.type
= WINE_INTERNAL_INPUT_MOUSE
;
136 input
.u
.mi
.dx
= posX
;
137 input
.u
.mi
.dy
= posY
;
138 input
.u
.mi
.mouseData
= data
;
139 input
.u
.mi
.dwFlags
= flags
;
140 input
.u
.mi
.time
= time
- X11DRV_server_startticks
;
141 input
.u
.mi
.dwExtraInfo
= (ULONG_PTR
)hwnd
;
142 SendInput( 1, &input
, sizeof(input
) );
146 /***********************************************************************
149 * Update the cursor of a window on a mouse event.
151 static void update_cursor( HWND hwnd
, Window win
)
153 struct x11drv_thread_data
*data
= x11drv_thread_data();
155 if (win
== X11DRV_get_client_window( hwnd
))
156 win
= X11DRV_get_whole_window( hwnd
); /* always set cursor on whole window */
158 if (data
->cursor_window
!= win
)
160 data
->cursor_window
= win
;
161 if (data
->cursor
) TSXDefineCursor( data
->display
, win
, data
->cursor
);
166 /***********************************************************************
169 * Create an X cursor from a Windows one.
171 static Cursor
create_cursor( Display
*display
, CURSORICONINFO
*ptr
)
173 Pixmap pixmapBits
, pixmapMask
, pixmapMaskInv
, pixmapAll
;
175 Cursor cursor
= None
;
177 if (!ptr
) /* Create an empty cursor */
179 static const char data
[] = { 0 };
181 bg
.red
= bg
.green
= bg
.blue
= 0x0000;
182 pixmapBits
= XCreateBitmapFromData( display
, root_window
, data
, 1, 1 );
185 cursor
= XCreatePixmapCursor( display
, pixmapBits
, pixmapBits
,
187 XFreePixmap( display
, pixmapBits
);
190 else /* Create the X cursor from the bits */
195 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
196 ptr
->nWidth
, ptr
->nHeight
, ptr
->bPlanes
, ptr
->bBitsPerPixel
,
198 /* Create a pixmap and transfer all the bits to it */
200 /* NOTE: Following hack works, but only because XFree depth
201 * 1 images really use 1 bit/pixel (and so the same layout
202 * as the Windows cursor data). Perhaps use a more generic
205 /* This pixmap will be written with two bitmaps. The first is
206 * the mask and the second is the image.
208 if (!(pixmapAll
= XCreatePixmap( display
, root_window
,
209 ptr
->nWidth
, ptr
->nHeight
* 2, 1 )))
211 if (!(image
= XCreateImage( display
, visual
,
212 1, ZPixmap
, 0, (char *)(ptr
+ 1), ptr
->nWidth
,
213 ptr
->nHeight
* 2, 16, ptr
->nWidthBytes
/ptr
->bBitsPerPixel
)))
215 XFreePixmap( display
, pixmapAll
);
218 gc
= XCreateGC( display
, pixmapAll
, 0, NULL
);
219 XSetGraphicsExposures( display
, gc
, False
);
220 image
->byte_order
= MSBFirst
;
221 image
->bitmap_bit_order
= MSBFirst
;
222 image
->bitmap_unit
= 16;
223 _XInitImageFuncPtrs(image
);
224 if (ptr
->bPlanes
* ptr
->bBitsPerPixel
== 1)
226 /* A plain old white on black cursor. */
227 fg
.red
= fg
.green
= fg
.blue
= 0xffff;
228 bg
.red
= bg
.green
= bg
.blue
= 0x0000;
229 XPutImage( display
, pixmapAll
, gc
, image
,
230 0, 0, 0, 0, ptr
->nWidth
, ptr
->nHeight
* 2 );
234 int rbits
, gbits
, bbits
, red
, green
, blue
;
235 int rfg
, gfg
, bfg
, rbg
, gbg
, bbg
;
236 int rscale
, gscale
, bscale
;
237 int x
, y
, xmax
, ymax
, bitIndex
, byteIndex
, xorIndex
;
238 unsigned char *theMask
, *theImage
, theChar
;
239 int threshold
, fgBits
, bgBits
, bitShifted
;
240 BYTE pXorBits
[128]; /* Up to 32x32 icons */
242 switch (ptr
->bBitsPerPixel
)
257 FIXME("Currently no support for cursors with %d bits per pixel\n",
259 XFreePixmap( display
, pixmapAll
);
260 XFreeGC( display
, gc
);
262 XDestroyImage( image
);
265 /* The location of the mask. */
266 theMask
= (char *)(ptr
+ 1);
267 /* The mask should still be 1 bit per pixel. The color image
268 * should immediately follow the mask.
270 theImage
= &theMask
[ptr
->nWidth
/8 * ptr
->nHeight
];
271 rfg
= gfg
= bfg
= rbg
= gbg
= bbg
= 0;
277 xmax
= (ptr
->nWidth
> 32) ? 32 : ptr
->nWidth
;
278 if (ptr
->nWidth
> 32) {
279 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
280 ptr
->nWidth
, ptr
->nHeight
);
282 ymax
= (ptr
->nHeight
> 32) ? 32 : ptr
->nHeight
;
284 memset(pXorBits
, 0, 128);
285 for (y
=0; y
<ymax
; y
++)
287 for (x
=0; x
<xmax
; x
++)
289 red
= green
= blue
= 0;
290 switch (ptr
->bBitsPerPixel
)
293 theChar
= theImage
[byteIndex
++];
295 theChar
= theImage
[byteIndex
++];
297 theChar
= theImage
[byteIndex
++];
301 theChar
= theImage
[byteIndex
++];
302 blue
= theChar
& 0x1F;
303 green
= (theChar
& 0xE0) >> 5;
304 theChar
= theImage
[byteIndex
++];
305 green
|= (theChar
& 0x07) << 3;
306 red
= (theChar
& 0xF8) >> 3;
310 if (red
+green
+blue
> threshold
)
316 pXorBits
[xorIndex
] |= bitShifted
;
330 bitShifted
= bitShifted
<< 1;
333 rscale
= 1 << (16 - rbits
);
334 gscale
= 1 << (16 - gbits
);
335 bscale
= 1 << (16 - bbits
);
338 fg
.red
= rfg
* rscale
/ fgBits
;
339 fg
.green
= gfg
* gscale
/ fgBits
;
340 fg
.blue
= bfg
* bscale
/ fgBits
;
342 else fg
.red
= fg
.green
= fg
.blue
= 0;
343 bgBits
= xmax
* ymax
- fgBits
;
346 bg
.red
= rbg
* rscale
/ bgBits
;
347 bg
.green
= gbg
* gscale
/ bgBits
;
348 bg
.blue
= bbg
* bscale
/ bgBits
;
350 else bg
.red
= bg
.green
= bg
.blue
= 0;
351 pixmapBits
= XCreateBitmapFromData( display
, root_window
, pXorBits
, xmax
, ymax
);
354 XFreePixmap( display
, pixmapAll
);
355 XFreeGC( display
, gc
);
357 XDestroyImage( image
);
362 XPutImage( display
, pixmapAll
, gc
, image
,
363 0, 0, 0, 0, ptr
->nWidth
, ptr
->nHeight
);
364 XSetFunction( display
, gc
, GXcopy
);
366 XCopyArea( display
, pixmapBits
, pixmapAll
, gc
,
367 0, 0, xmax
, ymax
, 0, ptr
->nHeight
);
368 XFreePixmap( display
, pixmapBits
);
371 XDestroyImage( image
);
373 /* Now create the 2 pixmaps for bits and mask */
375 pixmapBits
= XCreatePixmap( display
, root_window
, ptr
->nWidth
, ptr
->nHeight
, 1 );
376 pixmapMask
= XCreatePixmap( display
, root_window
, ptr
->nWidth
, ptr
->nHeight
, 1 );
377 pixmapMaskInv
= XCreatePixmap( display
, root_window
, ptr
->nWidth
, ptr
->nHeight
, 1 );
379 /* Make sure everything went OK so far */
381 if (pixmapBits
&& pixmapMask
&& pixmapMaskInv
)
385 /* We have to do some magic here, as cursors are not fully
386 * compatible between Windows and X11. Under X11, there
387 * are only 3 possible color cursor: black, white and
388 * masked. So we map the 4th Windows color (invert the
389 * bits on the screen) to black and an additional white bit on
390 * an other place (+1,+1). This require some boolean arithmetic:
393 * And Xor Result | Bits Mask Result
394 * 0 0 black | 0 1 background
395 * 0 1 white | 1 1 foreground
396 * 1 0 no change | X 0 no change
397 * 1 1 inverted | 0 1 background
400 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
401 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
403 * FIXME: apparently some servers do support 'inverted' color.
404 * I don't know if it's correct per the X spec, but maybe
405 * we ought to take advantage of it. -- AJ
407 XSetFunction( display
, gc
, GXcopy
);
408 XCopyArea( display
, pixmapAll
, pixmapBits
, gc
,
409 0, 0, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
410 XCopyArea( display
, pixmapAll
, pixmapMask
, gc
,
411 0, 0, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
412 XCopyArea( display
, pixmapAll
, pixmapMaskInv
, gc
,
413 0, 0, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
414 XSetFunction( display
, gc
, GXand
);
415 XCopyArea( display
, pixmapAll
, pixmapMaskInv
, gc
,
416 0, ptr
->nHeight
, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
417 XSetFunction( display
, gc
, GXandReverse
);
418 XCopyArea( display
, pixmapAll
, pixmapBits
, gc
,
419 0, ptr
->nHeight
, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
420 XSetFunction( display
, gc
, GXorReverse
);
421 XCopyArea( display
, pixmapAll
, pixmapMask
, gc
,
422 0, ptr
->nHeight
, ptr
->nWidth
, ptr
->nHeight
, 0, 0 );
423 /* Additional white */
424 XSetFunction( display
, gc
, GXor
);
425 XCopyArea( display
, pixmapMaskInv
, pixmapMask
, gc
,
426 0, 0, ptr
->nWidth
, ptr
->nHeight
, 1, 1 );
427 XCopyArea( display
, pixmapMaskInv
, pixmapBits
, gc
,
428 0, 0, ptr
->nWidth
, ptr
->nHeight
, 1, 1 );
429 XSetFunction( display
, gc
, GXcopy
);
431 /* Make sure hotspot is valid */
432 hotspot
.x
= ptr
->ptHotSpot
.x
;
433 hotspot
.y
= ptr
->ptHotSpot
.y
;
434 if (hotspot
.x
< 0 || hotspot
.x
>= ptr
->nWidth
||
435 hotspot
.y
< 0 || hotspot
.y
>= ptr
->nHeight
)
437 hotspot
.x
= ptr
->nWidth
/ 2;
438 hotspot
.y
= ptr
->nHeight
/ 2;
440 cursor
= XCreatePixmapCursor( display
, pixmapBits
, pixmapMask
,
441 &fg
, &bg
, hotspot
.x
, hotspot
.y
);
444 /* Now free everything */
446 if (pixmapAll
) XFreePixmap( display
, pixmapAll
);
447 if (pixmapBits
) XFreePixmap( display
, pixmapBits
);
448 if (pixmapMask
) XFreePixmap( display
, pixmapMask
);
449 if (pixmapMaskInv
) XFreePixmap( display
, pixmapMaskInv
);
450 XFreeGC( display
, gc
);
456 /***********************************************************************
457 * SetCursor (X11DRV.@)
459 void X11DRV_SetCursor( CURSORICONINFO
*lpCursor
)
463 if (root_window
!= DefaultRootWindow(gdi_display
))
465 /* If in desktop mode, set the cursor on the desktop window */
468 cursor
= create_cursor( gdi_display
, lpCursor
);
471 XDefineCursor( gdi_display
, root_window
, cursor
);
472 /* Make the change take effect immediately */
474 XFreeCursor( gdi_display
, cursor
);
478 else /* set the same cursor for all top-level windows of the current thread */
480 struct x11drv_thread_data
*data
= x11drv_thread_data();
483 cursor
= create_cursor( data
->display
, lpCursor
);
486 if (data
->cursor
) XFreeCursor( data
->display
, data
->cursor
);
487 data
->cursor
= cursor
;
488 if (data
->cursor_window
)
490 XDefineCursor( data
->display
, data
->cursor_window
, cursor
);
491 /* Make the change take effect immediately */
492 XFlush( data
->display
);
499 /***********************************************************************
500 * SetCursorPos (X11DRV.@)
502 void X11DRV_SetCursorPos( INT x
, INT y
)
504 Display
*display
= thread_display();
506 TRACE( "warping to (%d,%d)\n", x
, y
);
509 XWarpPointer( display
, root_window
, root_window
, 0, 0, 0, 0, x
, y
);
510 XFlush( display
); /* just in case */
514 /***********************************************************************
515 * GetCursorPos (X11DRV.@)
517 void X11DRV_GetCursorPos(LPPOINT pos
)
519 Display
*display
= thread_display();
521 int rootX
, rootY
, winX
, winY
;
524 if (!TSXQueryPointer( display
, root_window
, &root
, &child
,
525 &rootX
, &rootY
, &winX
, &winY
, &xstate
))
528 update_key_state( xstate
);
529 update_button_state( xstate
);
530 TRACE("pointer at (%d,%d)\n", winX
, winY
);
535 /***********************************************************************
536 * InitMouse (X11DRV.@)
538 void X11DRV_InitMouse( BYTE
*key_state_table
)
540 pKeyStateTable
= key_state_table
;
544 /***********************************************************************
547 void X11DRV_ButtonPress( HWND hwnd
, XButtonEvent
*event
)
549 int buttonNum
= event
->button
- 1;
553 if (buttonNum
>= NB_BUTTONS
) return;
556 update_cursor( hwnd
, event
->window
);
557 get_coords( &hwnd
, event
->window
, event
->x
, event
->y
, &pt
);
565 wData
= -WHEEL_DELTA
;
568 update_key_state( event
->state
);
569 send_mouse_event( hwnd
, button_down_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
,
570 pt
.x
, pt
.y
, wData
, event
->time
);
574 /***********************************************************************
575 * X11DRV_ButtonRelease
577 void X11DRV_ButtonRelease( HWND hwnd
, XButtonEvent
*event
)
579 int buttonNum
= event
->button
- 1;
582 if (buttonNum
>= NB_BUTTONS
|| !button_up_flags
[buttonNum
]) return;
585 update_cursor( hwnd
, event
->window
);
586 get_coords( &hwnd
, event
->window
, event
->x
, event
->y
, &pt
);
587 update_key_state( event
->state
);
588 send_mouse_event( hwnd
, button_up_flags
[buttonNum
] | MOUSEEVENTF_ABSOLUTE
,
589 pt
.x
, pt
.y
, 0, event
->time
);
593 /***********************************************************************
594 * X11DRV_MotionNotify
596 void X11DRV_MotionNotify( HWND hwnd
, XMotionEvent
*event
)
602 update_cursor( hwnd
, event
->window
);
603 get_coords( &hwnd
, event
->window
, event
->x
, event
->y
, &pt
);
604 update_key_state( event
->state
);
605 send_mouse_event( hwnd
, MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
,
606 pt
.x
, pt
.y
, 0, event
->time
);
610 /***********************************************************************
613 void X11DRV_EnterNotify( HWND hwnd
, XCrossingEvent
*event
)
617 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
630 /**********************************************************************
631 * X11DRV_DGAMotionEvent
633 void X11DRV_DGAMotionEvent( HWND hwnd
, XDGAMotionEvent
*event
)
635 update_key_state( event
->state
);
636 send_mouse_event( hwnd
, MOUSEEVENTF_MOVE
, event
->dx
, event
->dy
, 0, event
->time
);
639 /**********************************************************************
640 * X11DRV_DGAButtonPressEvent
642 void X11DRV_DGAButtonPressEvent( HWND hwnd
, XDGAButtonEvent
*event
)
644 int buttonNum
= event
->button
- 1;
646 if (buttonNum
>= NB_BUTTONS
) return;
647 update_key_state( event
->state
);
648 send_mouse_event( hwnd
, button_down_flags
[buttonNum
], 0, 0, 0, event
->time
);
651 /**********************************************************************
652 * X11DRV_DGAButtonReleaseEvent
654 void X11DRV_DGAButtonReleaseEvent( HWND hwnd
, XDGAButtonEvent
*event
)
656 int buttonNum
= event
->button
- 1;
658 if (buttonNum
>= NB_BUTTONS
) return;
659 update_key_state( event
->state
);
660 send_mouse_event( hwnd
, button_up_flags
[buttonNum
], 0, 0, 0, event
->time
);
662 #endif /* HAVE_LIBXXF86DGA2 */