Removed CURSORICON_IconToCursor now that we support color cursors.
[wine/multimedia.git] / dlls / x11drv / mouse.c
blob5b31211aba984359b6c04792b9bd41b466eb8dc0
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 "ts_xf86dga2.h"
26 #endif
28 #include "windef.h"
29 #include "wine/winuser16.h"
31 #include "x11drv.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] =
42 MOUSEEVENTF_LEFTDOWN,
43 MOUSEEVENTF_MIDDLEDOWN,
44 MOUSEEVENTF_RIGHTDOWN,
45 MOUSEEVENTF_WHEEL,
46 MOUSEEVENTF_WHEEL
49 static const UINT button_up_flags[NB_BUTTONS] =
51 MOUSEEVENTF_LEFTUP,
52 MOUSEEVENTF_MIDDLEUP,
53 MOUSEEVENTF_RIGHTUP,
58 static BYTE *pKeyStateTable;
61 /***********************************************************************
62 * get_coords
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;
69 WND *win;
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 );
81 pt->x = x;
82 pt->y = y;
83 if (*hwnd != GetDesktopWindow())
85 ClientToScreen( *hwnd, pt );
86 *hwnd = GetAncestor( *hwnd, GA_ROOT );
91 /***********************************************************************
92 * update_key_state
94 * Update the key state with what X provides us
96 static void update_key_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);
101 pKeyStateTable[VK_SHIFT] = (state & ShiftMask ? 0x80 : 0);
102 pKeyStateTable[VK_CONTROL] = (state & ControlMask ? 0x80 : 0);
106 /***********************************************************************
107 * send_mouse_event
109 static void send_mouse_event( HWND hwnd, DWORD flags, DWORD posX, DWORD posY,
110 DWORD data, Time time )
112 INPUT input;
114 TRACE("(%04lX,%ld,%ld)\n", flags, posX, posY );
116 if (flags & MOUSEEVENTF_ABSOLUTE)
118 int width = GetSystemMetrics( SM_CXSCREEN );
119 int height = GetSystemMetrics( SM_CYSCREEN );
120 /* Relative mouse movements seem not to be scaled as absolute ones */
121 posX = (((long)posX << 16) + width-1) / width;
122 posY = (((long)posY << 16) + height-1) / height;
125 input.type = WINE_INTERNAL_INPUT_MOUSE;
126 input.u.mi.dx = posX;
127 input.u.mi.dy = posY;
128 input.u.mi.mouseData = data;
129 input.u.mi.dwFlags = flags;
130 input.u.mi.time = time - X11DRV_server_startticks;
131 input.u.mi.dwExtraInfo = (ULONG_PTR)hwnd;
132 SendInput( 1, &input, sizeof(input) );
136 /***********************************************************************
137 * X11DRV_GetCursor
139 Cursor X11DRV_GetCursor( Display *display, CURSORICONINFO *ptr )
141 Pixmap pixmapBits, pixmapMask, pixmapMaskInv, pixmapAll;
142 XColor fg, bg;
143 Cursor cursor = None;
145 if (!ptr) /* Create an empty cursor */
147 static const char data[] = { 0 };
149 bg.red = bg.green = bg.blue = 0x0000;
150 pixmapBits = XCreateBitmapFromData( display, root_window, data, 1, 1 );
151 if (pixmapBits)
153 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
154 &bg, &bg, 0, 0 );
155 XFreePixmap( display, pixmapBits );
158 else /* Create the X cursor from the bits */
160 XImage *image;
161 GC gc;
163 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
164 ptr->nWidth, ptr->nHeight, ptr->bPlanes, ptr->bBitsPerPixel,
165 ptr->nWidthBytes);
166 /* Create a pixmap and transfer all the bits to it */
168 /* NOTE: Following hack works, but only because XFree depth
169 * 1 images really use 1 bit/pixel (and so the same layout
170 * as the Windows cursor data). Perhaps use a more generic
171 * algorithm here.
173 /* This pixmap will be written with two bitmaps. The first is
174 * the mask and the second is the image.
176 if (!(pixmapAll = XCreatePixmap( display, root_window,
177 ptr->nWidth, ptr->nHeight * 2, 1 )))
178 return 0;
179 if (!(image = XCreateImage( display, visual,
180 1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
181 ptr->nHeight * 2, 16, ptr->nWidthBytes/ptr->bBitsPerPixel)))
183 XFreePixmap( display, pixmapAll );
184 return 0;
186 gc = XCreateGC( display, pixmapAll, 0, NULL );
187 XSetGraphicsExposures( display, gc, False );
188 image->byte_order = MSBFirst;
189 image->bitmap_bit_order = MSBFirst;
190 image->bitmap_unit = 16;
191 _XInitImageFuncPtrs(image);
192 if (ptr->bPlanes * ptr->bBitsPerPixel == 1)
194 /* A plain old white on black cursor. */
195 fg.red = fg.green = fg.blue = 0xffff;
196 bg.red = bg.green = bg.blue = 0x0000;
197 XPutImage( display, pixmapAll, gc, image,
198 0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
200 else
202 int rbits, gbits, bbits, red, green, blue;
203 int rfg, gfg, bfg, rbg, gbg, bbg;
204 int rscale, gscale, bscale;
205 int x, y, xmax, ymax, bitIndex, byteIndex, xorIndex;
206 unsigned char *theMask, *theImage, theChar;
207 int threshold, fgBits, bgBits, bitShifted;
208 BYTE pXorBits[128]; /* Up to 32x32 icons */
210 switch (ptr->bBitsPerPixel)
212 case 24:
213 rbits = 8;
214 gbits = 8;
215 bbits = 8;
216 threshold = 0x40;
217 break;
218 case 16:
219 rbits = 5;
220 gbits = 6;
221 bbits = 5;
222 threshold = 0x40;
223 break;
224 default:
225 FIXME("Currently no support for cursors with %d bits per pixel\n",
226 ptr->bBitsPerPixel);
227 XFreePixmap( display, pixmapAll );
228 XFreeGC( display, gc );
229 image->data = NULL;
230 XDestroyImage( image );
231 return 0;
233 /* The location of the mask. */
234 theMask = (char *)(ptr + 1);
235 /* The mask should still be 1 bit per pixel. The color image
236 * should immediately follow the mask.
238 theImage = &theMask[ptr->nWidth/8 * ptr->nHeight];
239 rfg = gfg = bfg = rbg = gbg = bbg = 0;
240 bitIndex = 0;
241 byteIndex = 0;
242 xorIndex = 0;
243 fgBits = 0;
244 bitShifted = 0x01;
245 xmax = (ptr->nWidth > 32) ? 32 : ptr->nWidth;
246 if (ptr->nWidth > 32) {
247 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
248 ptr->nWidth, ptr->nHeight);
250 ymax = (ptr->nHeight > 32) ? 32 : ptr->nHeight;
252 memset(pXorBits, 0, 128);
253 for (y=0; y<ymax; y++)
255 for (x=0; x<xmax; x++)
257 red = green = blue = 0;
258 switch (ptr->bBitsPerPixel)
260 case 24:
261 theChar = theImage[byteIndex++];
262 blue = theChar;
263 theChar = theImage[byteIndex++];
264 green = theChar;
265 theChar = theImage[byteIndex++];
266 red = theChar;
267 break;
268 case 16:
269 theChar = theImage[byteIndex++];
270 blue = theChar & 0x1F;
271 green = (theChar & 0xE0) >> 5;
272 theChar = theImage[byteIndex++];
273 green |= (theChar & 0x07) << 3;
274 red = (theChar & 0xF8) >> 3;
275 break;
278 if (red+green+blue > threshold)
280 rfg += red;
281 gfg += green;
282 bfg += blue;
283 fgBits++;
284 pXorBits[xorIndex] |= bitShifted;
286 else
288 rbg += red;
289 gbg += green;
290 bbg += blue;
292 if (x%8 == 7)
294 bitShifted = 0x01;
295 xorIndex++;
297 else
298 bitShifted = bitShifted << 1;
301 rscale = 1 << (16 - rbits);
302 gscale = 1 << (16 - gbits);
303 bscale = 1 << (16 - bbits);
304 if (fgBits)
306 fg.red = rfg * rscale / fgBits;
307 fg.green = gfg * gscale / fgBits;
308 fg.blue = bfg * bscale / fgBits;
310 else fg.red = fg.green = fg.blue = 0;
311 bgBits = xmax * ymax - fgBits;
312 if (bgBits)
314 bg.red = rbg * rscale / bgBits;
315 bg.green = gbg * gscale / bgBits;
316 bg.blue = bbg * bscale / bgBits;
318 else bg.red = bg.green = bg.blue = 0;
319 pixmapBits = XCreateBitmapFromData( display, root_window, pXorBits, xmax, ymax );
320 if (!pixmapBits)
322 XFreePixmap( display, pixmapAll );
323 XFreeGC( display, gc );
324 image->data = NULL;
325 XDestroyImage( image );
326 return 0;
329 /* Put the mask. */
330 XPutImage( display, pixmapAll, gc, image,
331 0, 0, 0, 0, ptr->nWidth, ptr->nHeight );
332 XSetFunction( display, gc, GXcopy );
333 /* Put the image */
334 XCopyArea( display, pixmapBits, pixmapAll, gc,
335 0, 0, xmax, ymax, 0, ptr->nHeight );
336 XFreePixmap( display, pixmapBits );
338 image->data = NULL;
339 XDestroyImage( image );
341 /* Now create the 2 pixmaps for bits and mask */
343 pixmapBits = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
344 pixmapMask = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
345 pixmapMaskInv = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
347 /* Make sure everything went OK so far */
349 if (pixmapBits && pixmapMask && pixmapMaskInv)
351 POINT hotspot;
353 /* We have to do some magic here, as cursors are not fully
354 * compatible between Windows and X11. Under X11, there
355 * are only 3 possible color cursor: black, white and
356 * masked. So we map the 4th Windows color (invert the
357 * bits on the screen) to black and an additional white bit on
358 * an other place (+1,+1). This require some boolean arithmetic:
360 * Windows | X11
361 * And Xor Result | Bits Mask Result
362 * 0 0 black | 0 1 background
363 * 0 1 white | 1 1 foreground
364 * 1 0 no change | X 0 no change
365 * 1 1 inverted | 0 1 background
367 * which gives:
368 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
369 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
371 * FIXME: apparently some servers do support 'inverted' color.
372 * I don't know if it's correct per the X spec, but maybe
373 * we ought to take advantage of it. -- AJ
375 XSetFunction( display, gc, GXcopy );
376 XCopyArea( display, pixmapAll, pixmapBits, gc,
377 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
378 XCopyArea( display, pixmapAll, pixmapMask, gc,
379 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
380 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
381 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
382 XSetFunction( display, gc, GXand );
383 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
384 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
385 XSetFunction( display, gc, GXandReverse );
386 XCopyArea( display, pixmapAll, pixmapBits, gc,
387 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
388 XSetFunction( display, gc, GXorReverse );
389 XCopyArea( display, pixmapAll, pixmapMask, gc,
390 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
391 /* Additional white */
392 XSetFunction( display, gc, GXor );
393 XCopyArea( display, pixmapMaskInv, pixmapMask, gc,
394 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
395 XCopyArea( display, pixmapMaskInv, pixmapBits, gc,
396 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
397 XSetFunction( display, gc, GXcopy );
399 /* Make sure hotspot is valid */
400 hotspot.x = ptr->ptHotSpot.x;
401 hotspot.y = ptr->ptHotSpot.y;
402 if (hotspot.x < 0 || hotspot.x >= ptr->nWidth ||
403 hotspot.y < 0 || hotspot.y >= ptr->nHeight)
405 hotspot.x = ptr->nWidth / 2;
406 hotspot.y = ptr->nHeight / 2;
408 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
409 &fg, &bg, hotspot.x, hotspot.y );
412 /* Now free everything */
414 if (pixmapAll) XFreePixmap( display, pixmapAll );
415 if (pixmapBits) XFreePixmap( display, pixmapBits );
416 if (pixmapMask) XFreePixmap( display, pixmapMask );
417 if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
418 XFreeGC( display, gc );
420 return cursor;
423 /* set the cursor of a window; helper for X11DRV_SetCursor */
424 static BOOL CALLBACK set_win_cursor( HWND hwnd, LPARAM cursor )
426 Window win = X11DRV_get_whole_window( hwnd );
427 if (win) TSXDefineCursor( thread_display(), win, (Cursor)cursor );
428 return TRUE;
431 /***********************************************************************
432 * SetCursor (X11DRV.@)
434 void X11DRV_SetCursor( CURSORICONINFO *lpCursor )
436 Cursor cursor;
438 if (root_window != DefaultRootWindow(gdi_display))
440 /* If in desktop mode, set the cursor on the desktop window */
442 wine_tsx11_lock();
443 cursor = X11DRV_GetCursor( gdi_display, lpCursor );
444 if (cursor)
446 XDefineCursor( gdi_display, root_window, cursor );
447 XFreeCursor( gdi_display, cursor );
449 wine_tsx11_unlock();
451 else /* set the same cursor for all top-level windows of the current thread */
453 Display *display = thread_display();
455 wine_tsx11_lock();
456 cursor = X11DRV_GetCursor( display, lpCursor );
457 wine_tsx11_unlock();
458 if (cursor)
460 /* EnumThreadWindows( GetCurrentThreadId(), set_win_cursor, (LPARAM)cursor );*/
461 EnumWindows( set_win_cursor, (LPARAM)cursor );
462 TSXFreeCursor( display, cursor );
467 /***********************************************************************
468 * SetCursorPos (X11DRV.@)
470 void X11DRV_SetCursorPos( INT x, INT y )
472 Display *display = thread_display();
474 TRACE( "warping to (%d,%d)\n", x, y );
476 wine_tsx11_lock();
477 XWarpPointer( display, root_window, root_window, 0, 0, 0, 0, x, y );
478 XFlush( display ); /* just in case */
479 wine_tsx11_unlock();
482 /***********************************************************************
483 * GetCursorPos (X11DRV.@)
485 void X11DRV_GetCursorPos(LPPOINT pos)
487 Display *display = thread_display();
488 Window root, child;
489 int rootX, rootY, winX, winY;
490 unsigned int xstate;
492 if (!TSXQueryPointer( display, root_window, &root, &child,
493 &rootX, &rootY, &winX, &winY, &xstate ))
494 return;
496 TRACE("pointer at (%d,%d)\n", winX, winY );
497 pos->x = winX;
498 pos->y = winY;
501 /***********************************************************************
502 * InitMouse (X11DRV.@)
504 void X11DRV_InitMouse( BYTE *key_state_table )
506 Window root, child;
507 int root_x, root_y, child_x, child_y;
508 unsigned int KeyState;
510 pKeyStateTable = key_state_table;
511 /* Get the current mouse position and simulate an absolute mouse
512 movement to initialize the mouse global variables */
513 TSXQueryPointer( thread_display(), root_window, &root, &child,
514 &root_x, &root_y, &child_x, &child_y, &KeyState);
515 update_key_state( KeyState );
516 send_mouse_event( 0, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
517 root_x, root_y, 0, GetTickCount() + X11DRV_server_startticks );
521 /***********************************************************************
522 * X11DRV_ButtonPress
524 void X11DRV_ButtonPress( HWND hwnd, XButtonEvent *event )
526 int buttonNum = event->button - 1;
527 WORD wData = 0;
528 POINT pt;
530 if (buttonNum >= NB_BUTTONS) return;
532 get_coords( &hwnd, event->window, event->x, event->y, &pt );
534 switch (buttonNum)
536 case 3:
537 wData = WHEEL_DELTA;
538 break;
539 case 4:
540 wData = -WHEEL_DELTA;
541 break;
543 update_key_state( event->state );
544 send_mouse_event( hwnd, button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE,
545 pt.x, pt.y, wData, event->time );
549 /***********************************************************************
550 * X11DRV_ButtonRelease
552 void X11DRV_ButtonRelease( HWND hwnd, XButtonEvent *event )
554 int buttonNum = event->button - 1;
555 POINT pt;
557 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
559 get_coords( &hwnd, event->window, event->x, event->y, &pt );
560 update_key_state( event->state );
561 send_mouse_event( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE,
562 pt.x, pt.y, 0, event->time );
566 /***********************************************************************
567 * X11DRV_MotionNotify
569 void X11DRV_MotionNotify( HWND hwnd, XMotionEvent *event )
571 POINT pt;
573 get_coords( &hwnd, event->window, event->x, event->y, &pt );
574 update_key_state( event->state );
575 send_mouse_event( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
576 pt.x, pt.y, 0, event->time );
580 #ifdef HAVE_LIBXXF86DGA2
581 /**********************************************************************
582 * X11DRV_DGAMotionEvent
584 void X11DRV_DGAMotionEvent( HWND hwnd, XDGAMotionEvent *event )
586 update_key_state( event->state );
587 send_mouse_event( hwnd, MOUSEEVENTF_MOVE, event->dx, event->dy, 0, event->time );
590 /**********************************************************************
591 * X11DRV_DGAButtonPressEvent
593 void X11DRV_DGAButtonPressEvent( HWND hwnd, XDGAButtonEvent *event )
595 int buttonNum = event->button - 1;
597 if (buttonNum >= NB_BUTTONS) return;
598 update_key_state( event->state );
599 send_mouse_event( hwnd, button_down_flags[buttonNum], 0, 0, 0, event->time );
602 /**********************************************************************
603 * X11DRV_DGAButtonReleaseEvent
605 void X11DRV_DGAButtonReleaseEvent( HWND hwnd, XDGAButtonEvent *event )
607 int buttonNum = event->button - 1;
609 if (buttonNum >= NB_BUTTONS) return;
610 update_key_state( event->state );
611 send_mouse_event( hwnd, button_up_flags[buttonNum], 0, 0, 0, event->time );
613 #endif /* HAVE_LIBXXF86DGA2 */