Release 20030408.
[wine/gsoc-2012-control.git] / dlls / x11drv / mouse.c
blob6b6c9149f4e029ca9e005ac989d52c821f35440b
1 /*
2 * X11 mouse driver
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
21 #include "config.h"
23 #include "ts_xlib.h"
24 #ifdef HAVE_LIBXXF86DGA2
25 #include <X11/extensions/xf86dga.h>
26 #endif
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30 #include "windef.h"
31 #include "wine/winuser16.h"
33 #include "x11drv.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] =
44 MOUSEEVENTF_LEFTDOWN,
45 MOUSEEVENTF_MIDDLEDOWN,
46 MOUSEEVENTF_RIGHTDOWN,
47 MOUSEEVENTF_WHEEL,
48 MOUSEEVENTF_WHEEL
51 static const UINT button_up_flags[NB_BUTTONS] =
53 MOUSEEVENTF_LEFTUP,
54 MOUSEEVENTF_MIDDLEUP,
55 MOUSEEVENTF_RIGHTUP,
60 static BYTE *pKeyStateTable;
63 /***********************************************************************
64 * get_coords
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;
71 WND *win;
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 );
83 pt->x = x;
84 pt->y = y;
85 if (*hwnd != GetDesktopWindow())
87 ClientToScreen( *hwnd, pt );
88 *hwnd = GetAncestor( *hwnd, GA_ROOT );
93 /***********************************************************************
94 * update_button_state
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 /***********************************************************************
107 * update_key_state
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 /***********************************************************************
119 * send_mouse_event
121 static void send_mouse_event( HWND hwnd, DWORD flags, DWORD posX, DWORD posY,
122 DWORD data, Time time )
124 INPUT input;
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 /***********************************************************************
149 * update_cursor
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 /***********************************************************************
169 * create_cursor
171 * Create an X cursor from a Windows one.
173 static Cursor create_cursor( Display *display, CURSORICONINFO *ptr )
175 Pixmap pixmapBits, pixmapMask, pixmapMaskInv, pixmapAll;
176 XColor fg, bg;
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 );
185 if (pixmapBits)
187 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
188 &bg, &bg, 0, 0 );
189 XFreePixmap( display, pixmapBits );
192 else /* Create the X cursor from the bits */
194 XImage *image;
195 GC gc;
197 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
198 ptr->nWidth, ptr->nHeight, ptr->bPlanes, ptr->bBitsPerPixel,
199 ptr->nWidthBytes);
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
205 * algorithm here.
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 )))
212 return 0;
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 );
218 return 0;
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 );
234 else
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)
246 case 24:
247 rbits = 8;
248 gbits = 8;
249 bbits = 8;
250 threshold = 0x40;
251 break;
252 case 16:
253 rbits = 5;
254 gbits = 6;
255 bbits = 5;
256 threshold = 0x40;
257 break;
258 default:
259 FIXME("Currently no support for cursors with %d bits per pixel\n",
260 ptr->bBitsPerPixel);
261 XFreePixmap( display, pixmapAll );
262 XFreeGC( display, gc );
263 image->data = NULL;
264 XDestroyImage( image );
265 return 0;
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;
274 bitIndex = 0;
275 byteIndex = 0;
276 xorIndex = 0;
277 fgBits = 0;
278 bitShifted = 0x01;
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)
294 case 24:
295 theChar = theImage[byteIndex++];
296 blue = theChar;
297 theChar = theImage[byteIndex++];
298 green = theChar;
299 theChar = theImage[byteIndex++];
300 red = theChar;
301 break;
302 case 16:
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;
309 break;
312 if (red+green+blue > threshold)
314 rfg += red;
315 gfg += green;
316 bfg += blue;
317 fgBits++;
318 pXorBits[xorIndex] |= bitShifted;
320 else
322 rbg += red;
323 gbg += green;
324 bbg += blue;
326 if (x%8 == 7)
328 bitShifted = 0x01;
329 xorIndex++;
331 else
332 bitShifted = bitShifted << 1;
335 rscale = 1 << (16 - rbits);
336 gscale = 1 << (16 - gbits);
337 bscale = 1 << (16 - bbits);
338 if (fgBits)
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;
346 if (bgBits)
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 );
354 if (!pixmapBits)
356 XFreePixmap( display, pixmapAll );
357 XFreeGC( display, gc );
358 image->data = NULL;
359 XDestroyImage( image );
360 return 0;
363 /* Put the mask. */
364 XPutImage( display, pixmapAll, gc, image,
365 0, 0, 0, 0, ptr->nWidth, ptr->nHeight );
366 XSetFunction( display, gc, GXcopy );
367 /* Put the image */
368 XCopyArea( display, pixmapBits, pixmapAll, gc,
369 0, 0, xmax, ymax, 0, ptr->nHeight );
370 XFreePixmap( display, pixmapBits );
372 image->data = NULL;
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)
385 POINT hotspot;
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:
394 * Windows | X11
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
401 * which gives:
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 );
454 return cursor;
458 /***********************************************************************
459 * SetCursor (X11DRV.@)
461 void X11DRV_SetCursor( CURSORICONINFO *lpCursor )
463 Cursor cursor;
465 if (root_window != DefaultRootWindow(gdi_display))
467 /* If in desktop mode, set the cursor on the desktop window */
469 wine_tsx11_lock();
470 cursor = create_cursor( gdi_display, lpCursor );
471 if (cursor)
473 XDefineCursor( gdi_display, root_window, cursor );
474 /* Make the change take effect immediately */
475 XFlush(gdi_display);
476 XFreeCursor( gdi_display, cursor );
478 wine_tsx11_unlock();
480 else /* set the same cursor for all top-level windows of the current thread */
482 struct x11drv_thread_data *data = x11drv_thread_data();
484 wine_tsx11_lock();
485 cursor = create_cursor( data->display, lpCursor );
486 if (cursor)
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 );
497 wine_tsx11_unlock();
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 );
510 wine_tsx11_lock();
511 XWarpPointer( display, root_window, root_window, 0, 0, 0, 0, x, y );
512 XFlush( display ); /* just in case */
513 wine_tsx11_unlock();
516 /***********************************************************************
517 * GetCursorPos (X11DRV.@)
519 void X11DRV_GetCursorPos(LPPOINT pos)
521 Display *display = thread_display();
522 Window root, child;
523 int rootX, rootY, winX, winY;
524 unsigned int xstate;
526 if (!TSXQueryPointer( display, root_window, &root, &child,
527 &rootX, &rootY, &winX, &winY, &xstate ))
528 return;
530 update_key_state( xstate );
531 update_button_state( xstate );
532 TRACE("pointer at (%d,%d)\n", winX, winY );
533 pos->x = winX;
534 pos->y = winY;
537 /***********************************************************************
538 * InitMouse (X11DRV.@)
540 void X11DRV_InitMouse( BYTE *key_state_table )
542 pKeyStateTable = key_state_table;
546 /***********************************************************************
547 * X11DRV_ButtonPress
549 void X11DRV_ButtonPress( HWND hwnd, XButtonEvent *event )
551 int buttonNum = event->button - 1;
552 WORD wData = 0;
553 POINT pt;
555 if (buttonNum >= NB_BUTTONS) return;
556 if (!hwnd) return;
558 update_cursor( hwnd, event->window );
559 get_coords( &hwnd, event->window, event->x, event->y, &pt );
561 switch (buttonNum)
563 case 3:
564 wData = WHEEL_DELTA;
565 break;
566 case 4:
567 wData = -WHEEL_DELTA;
568 break;
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;
582 POINT pt;
584 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
585 if (!hwnd) 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 )
600 POINT pt;
602 if (!hwnd) return;
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 /***********************************************************************
613 * X11DRV_EnterNotify
615 void X11DRV_EnterNotify( HWND hwnd, XCrossingEvent *event )
617 POINT pt;
619 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
620 if (!hwnd) 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 */