winex11: Reimplement creation of Xlib cursors to use the bitmaps returned by GetIconInfo.
[wine/multimedia.git] / dlls / winex11.drv / mouse.c
blob9e3a5cdddd7ab335527da792268b50739e67c472
1 /*
2 * X11 mouse driver
4 * Copyright 1998 Ulrich Weigand
5 * Copyright 2007 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <X11/Xlib.h>
26 #include <stdarg.h>
28 #ifdef SONAME_LIBXCURSOR
29 # include <X11/Xcursor/Xcursor.h>
30 static void *xcursor_handle;
31 # define MAKE_FUNCPTR(f) static typeof(f) * p##f
32 MAKE_FUNCPTR(XcursorImageCreate);
33 MAKE_FUNCPTR(XcursorImageDestroy);
34 MAKE_FUNCPTR(XcursorImageLoadCursor);
35 # undef MAKE_FUNCPTR
36 #endif /* SONAME_LIBXCURSOR */
38 #define NONAMELESSUNION
39 #define NONAMELESSSTRUCT
40 #include "windef.h"
41 #include "winbase.h"
42 #include "wine/winuser16.h"
44 #include "x11drv.h"
45 #include "wine/server.h"
46 #include "wine/library.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
51 /**********************************************************************/
53 #ifndef Button6Mask
54 #define Button6Mask (1<<13)
55 #endif
56 #ifndef Button7Mask
57 #define Button7Mask (1<<14)
58 #endif
60 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
62 static const UINT button_down_flags[NB_BUTTONS] =
64 MOUSEEVENTF_LEFTDOWN,
65 MOUSEEVENTF_MIDDLEDOWN,
66 MOUSEEVENTF_RIGHTDOWN,
67 MOUSEEVENTF_WHEEL,
68 MOUSEEVENTF_WHEEL,
69 MOUSEEVENTF_XDOWN, /* FIXME: horizontal wheel */
70 MOUSEEVENTF_XDOWN,
71 MOUSEEVENTF_XDOWN,
72 MOUSEEVENTF_XDOWN
75 static const UINT button_up_flags[NB_BUTTONS] =
77 MOUSEEVENTF_LEFTUP,
78 MOUSEEVENTF_MIDDLEUP,
79 MOUSEEVENTF_RIGHTUP,
82 MOUSEEVENTF_XUP,
83 MOUSEEVENTF_XUP,
84 MOUSEEVENTF_XUP,
85 MOUSEEVENTF_XUP
88 POINT cursor_pos;
89 static HWND cursor_window;
90 static DWORD last_time_modified;
91 static RECT cursor_clip; /* Cursor clipping rect */
92 static XContext cursor_context;
94 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y );
97 /***********************************************************************
98 * X11DRV_Xcursor_Init
100 * Load the Xcursor library for use.
102 void X11DRV_Xcursor_Init(void)
104 #ifdef SONAME_LIBXCURSOR
105 xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
106 if (!xcursor_handle) /* wine_dlopen failed. */
108 WARN("Xcursor failed to load. Using fallback code.\n");
109 return;
111 #define LOAD_FUNCPTR(f) \
112 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
114 LOAD_FUNCPTR(XcursorImageCreate);
115 LOAD_FUNCPTR(XcursorImageDestroy);
116 LOAD_FUNCPTR(XcursorImageLoadCursor);
117 #undef LOAD_FUNCPTR
118 #endif /* SONAME_LIBXCURSOR */
122 /***********************************************************************
123 * get_coords
125 * get the coordinates of a mouse event
127 static inline void get_coords( HWND hwnd, Window window, int x, int y, POINT *pt )
129 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
131 if (!data) return;
133 if (window == data->client_window)
135 pt->x = x + data->client_rect.left;
136 pt->y = y + data->client_rect.top;
138 else
140 pt->x = x + data->whole_rect.left;
141 pt->y = y + data->whole_rect.top;
145 /***********************************************************************
146 * clip_point_to_rect
148 * Clip point to the provided rectangle
150 static inline void clip_point_to_rect( LPCRECT rect, LPPOINT pt )
152 if (pt->x < rect->left) pt->x = rect->left;
153 else if (pt->x >= rect->right) pt->x = rect->right - 1;
154 if (pt->y < rect->top) pt->y = rect->top;
155 else if (pt->y >= rect->bottom) pt->y = rect->bottom - 1;
158 /***********************************************************************
159 * update_button_state
161 * Update the button state with what X provides us
163 static inline void update_button_state( unsigned int state )
165 key_state_table[VK_LBUTTON] = (state & Button1Mask ? 0x80 : 0);
166 key_state_table[VK_MBUTTON] = (state & Button2Mask ? 0x80 : 0);
167 key_state_table[VK_RBUTTON] = (state & Button3Mask ? 0x80 : 0);
168 /* X-buttons are not reported from XQueryPointer */
171 /***********************************************************************
172 * get_empty_cursor
174 static Cursor get_empty_cursor(void)
176 static Cursor cursor;
177 static const char data[] = { 0 };
179 wine_tsx11_lock();
180 if (!cursor)
182 XColor bg;
183 Pixmap pixmap;
185 bg.red = bg.green = bg.blue = 0x0000;
186 pixmap = XCreateBitmapFromData( gdi_display, root_window, data, 1, 1 );
187 if (pixmap)
189 cursor = XCreatePixmapCursor( gdi_display, pixmap, pixmap, &bg, &bg, 0, 0 );
190 XFreePixmap( gdi_display, pixmap );
193 wine_tsx11_unlock();
194 return cursor;
197 /***********************************************************************
198 * get_x11_cursor
200 Cursor get_x11_cursor( HCURSOR handle )
202 Cursor cursor;
204 if (!handle) return get_empty_cursor();
206 if (cursor_context && !XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
207 return cursor;
208 return 0;
211 /***********************************************************************
212 * set_window_cursor
214 void set_window_cursor( HWND hwnd, HCURSOR handle )
216 struct x11drv_win_data *data;
217 Cursor cursor;
219 if (!(data = X11DRV_get_win_data( hwnd ))) return;
221 wine_tsx11_lock();
222 if ((cursor = get_x11_cursor( handle )))
224 TRACE( "%p xid %lx\n", handle, cursor );
225 XDefineCursor( gdi_display, data->whole_window, cursor );
226 /* Make the change take effect immediately */
227 XFlush( gdi_display );
228 data->cursor = handle;
230 wine_tsx11_unlock();
233 /***********************************************************************
234 * update_mouse_state
236 * Update the various window states on a mouse event.
238 static void update_mouse_state( HWND hwnd, Window window, int x, int y, unsigned int state, POINT *pt )
240 struct x11drv_thread_data *data = x11drv_thread_data();
242 get_coords( hwnd, window, x, y, pt );
244 cursor_window = hwnd;
246 /* update the wine server Z-order */
248 if (window != data->grab_window &&
249 /* ignore event if a button is pressed, since the mouse is then grabbed too */
250 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
252 SERVER_START_REQ( update_window_zorder )
254 req->window = wine_server_user_handle( hwnd );
255 req->rect.left = pt->x;
256 req->rect.top = pt->y;
257 req->rect.right = pt->x + 1;
258 req->rect.bottom = pt->y + 1;
259 wine_server_call( req );
261 SERVER_END_REQ;
266 /***********************************************************************
267 * get_key_state
269 static WORD get_key_state(void)
271 WORD ret = 0;
273 if (GetSystemMetrics( SM_SWAPBUTTON ))
275 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
276 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
278 else
280 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
281 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
283 if (key_state_table[VK_MBUTTON] & 0x80) ret |= MK_MBUTTON;
284 if (key_state_table[VK_SHIFT] & 0x80) ret |= MK_SHIFT;
285 if (key_state_table[VK_CONTROL] & 0x80) ret |= MK_CONTROL;
286 if (key_state_table[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
287 if (key_state_table[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
288 return ret;
292 /***********************************************************************
293 * queue_raw_mouse_message
295 static void queue_raw_mouse_message( UINT message, HWND hwnd, DWORD x, DWORD y,
296 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
298 MSLLHOOKSTRUCT hook;
299 HCURSOR cursor;
301 hook.pt.x = x;
302 hook.pt.y = y;
303 hook.mouseData = MAKELONG( 0, data );
304 hook.flags = injected_flags;
305 hook.time = time;
306 hook.dwExtraInfo = extra_info;
308 last_time_modified = GetTickCount();
309 if (HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE ))
310 message = 0; /* ignore it */
312 SERVER_START_REQ( send_hardware_message )
314 req->id = (injected_flags & LLMHF_INJECTED) ? 0 : GetCurrentThreadId();
315 req->win = wine_server_user_handle( hwnd );
316 req->msg = message;
317 req->wparam = MAKEWPARAM( get_key_state(), data );
318 req->lparam = 0;
319 req->x = x;
320 req->y = y;
321 req->time = time;
322 req->info = extra_info;
323 wine_server_call( req );
324 cursor = (reply->count >= 0) ? wine_server_ptr_handle(reply->cursor) : 0;
326 SERVER_END_REQ;
328 if (hwnd)
330 Cursor xcursor;
331 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
332 if (data && cursor != data->cursor)
334 wine_tsx11_lock();
335 if ((xcursor = get_x11_cursor( cursor )))
336 XDefineCursor( gdi_display, data->whole_window, xcursor );
337 data->cursor = cursor;
338 wine_tsx11_unlock();
344 /***********************************************************************
345 * X11DRV_send_mouse_input
347 void X11DRV_send_mouse_input( HWND hwnd, DWORD flags, DWORD x, DWORD y,
348 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
350 POINT pt;
352 if (flags & MOUSEEVENTF_MOVE && flags & MOUSEEVENTF_ABSOLUTE)
354 if (injected_flags & LLMHF_INJECTED)
356 pt.x = (x * screen_width) >> 16;
357 pt.y = (y * screen_height) >> 16;
359 else
361 pt.x = x;
362 pt.y = y;
363 wine_tsx11_lock();
364 if (cursor_pos.x == x && cursor_pos.y == y &&
365 (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE)))
366 flags &= ~MOUSEEVENTF_MOVE;
367 wine_tsx11_unlock();
370 else if (flags & MOUSEEVENTF_MOVE)
372 int accel[3], xMult = 1, yMult = 1;
374 /* dx and dy can be negative numbers for relative movements */
375 SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
377 if (abs(x) > accel[0] && accel[2] != 0)
379 xMult = 2;
380 if ((abs(x) > accel[1]) && (accel[2] == 2)) xMult = 4;
382 if (abs(y) > accel[0] && accel[2] != 0)
384 yMult = 2;
385 if ((abs(y) > accel[1]) && (accel[2] == 2)) yMult = 4;
388 wine_tsx11_lock();
389 pt.x = cursor_pos.x + (long)x * xMult;
390 pt.y = cursor_pos.y + (long)y * yMult;
391 wine_tsx11_unlock();
393 else
395 wine_tsx11_lock();
396 pt = cursor_pos;
397 wine_tsx11_unlock();
400 if (flags & MOUSEEVENTF_MOVE)
402 queue_raw_mouse_message( WM_MOUSEMOVE, hwnd, pt.x, pt.y, data, time,
403 extra_info, injected_flags );
404 if ((injected_flags & LLMHF_INJECTED) &&
405 ((flags & MOUSEEVENTF_ABSOLUTE) || x || y)) /* we have to actually move the cursor */
407 X11DRV_SetCursorPos( pt.x, pt.y );
409 else
411 wine_tsx11_lock();
412 clip_point_to_rect( &cursor_clip, &pt);
413 cursor_pos = pt;
414 wine_tsx11_unlock();
417 if (flags & MOUSEEVENTF_LEFTDOWN)
419 key_state_table[VK_LBUTTON] |= 0xc0;
420 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
421 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
423 if (flags & MOUSEEVENTF_LEFTUP)
425 key_state_table[VK_LBUTTON] &= ~0x80;
426 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONUP : WM_LBUTTONUP,
427 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
429 if (flags & MOUSEEVENTF_RIGHTDOWN)
431 key_state_table[VK_RBUTTON] |= 0xc0;
432 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
433 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
435 if (flags & MOUSEEVENTF_RIGHTUP)
437 key_state_table[VK_RBUTTON] &= ~0x80;
438 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONUP : WM_RBUTTONUP,
439 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
441 if (flags & MOUSEEVENTF_MIDDLEDOWN)
443 key_state_table[VK_MBUTTON] |= 0xc0;
444 queue_raw_mouse_message( WM_MBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
445 extra_info, injected_flags );
447 if (flags & MOUSEEVENTF_MIDDLEUP)
449 key_state_table[VK_MBUTTON] &= ~0x80;
450 queue_raw_mouse_message( WM_MBUTTONUP, hwnd, pt.x, pt.y, data, time,
451 extra_info, injected_flags );
453 if (flags & MOUSEEVENTF_WHEEL)
455 queue_raw_mouse_message( WM_MOUSEWHEEL, hwnd, pt.x, pt.y, data, time,
456 extra_info, injected_flags );
458 if (flags & MOUSEEVENTF_XDOWN)
460 key_state_table[VK_XBUTTON1 + data - 1] |= 0xc0;
461 queue_raw_mouse_message( WM_XBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
462 extra_info, injected_flags );
464 if (flags & MOUSEEVENTF_XUP)
466 key_state_table[VK_XBUTTON1 + data - 1] &= ~0x80;
467 queue_raw_mouse_message( WM_XBUTTONUP, hwnd, pt.x, pt.y, data, time,
468 extra_info, injected_flags );
472 #ifdef SONAME_LIBXCURSOR
474 /***********************************************************************
475 * create_xcursor_cursor
477 * Use Xcursor to create an X cursor from a Windows one.
479 static Cursor create_xcursor_cursor( HDC hdc, ICONINFO *icon, int width, int height )
481 int x, y, i, has_alpha;
482 BITMAPINFO *info;
483 Cursor cursor;
484 XcursorImage *image;
485 XcursorPixel *ptr;
487 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return 0;
489 wine_tsx11_lock();
490 image = pXcursorImageCreate( width, height );
491 wine_tsx11_unlock();
492 if (!image)
494 HeapFree( GetProcessHeap(), 0, info );
495 return 0;
498 image->xhot = icon->xHotspot;
499 image->yhot = icon->yHotspot;
500 image->delay = 0;
502 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
503 info->bmiHeader.biWidth = width;
504 info->bmiHeader.biHeight = -height;
505 info->bmiHeader.biPlanes = 1;
506 info->bmiHeader.biBitCount = 32;
507 info->bmiHeader.biCompression = BI_RGB;
508 info->bmiHeader.biSizeImage = width * height * 4;
509 info->bmiHeader.biXPelsPerMeter = 0;
510 info->bmiHeader.biYPelsPerMeter = 0;
511 info->bmiHeader.biClrUsed = 0;
512 info->bmiHeader.biClrImportant = 0;
513 GetDIBits( hdc, icon->hbmColor, 0, height, image->pixels, info, DIB_RGB_COLORS );
515 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
516 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
518 if (!has_alpha)
520 unsigned char *mask_bits;
521 unsigned int width_bytes = (width + 31) / 32 * 4;
523 /* generate alpha channel from the mask */
524 info->bmiHeader.biBitCount = 1;
525 info->bmiHeader.biSizeImage = width_bytes * height;
526 if ((mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage )))
528 GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS );
529 for (y = 0, ptr = image->pixels; y < height; y++)
530 for (x = 0; x < width; x++, ptr++)
531 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
532 *ptr |= 0xff000000;
533 HeapFree( GetProcessHeap(), 0, mask_bits );
536 HeapFree( GetProcessHeap(), 0, info );
538 wine_tsx11_lock();
539 cursor = pXcursorImageLoadCursor( gdi_display, image );
540 pXcursorImageDestroy( image );
541 wine_tsx11_unlock();
542 return cursor;
545 #endif /* SONAME_LIBXCURSOR */
548 /***********************************************************************
549 * create_cursor_from_bitmaps
551 * Create an X11 cursor from source bitmaps.
553 static Cursor create_cursor_from_bitmaps( HBITMAP src_xor, HBITMAP src_and, int width, int height,
554 int xor_y, int and_y, XColor *fg, XColor *bg,
555 int hotspot_x, int hotspot_y )
557 HDC src = 0, dst = 0;
558 HBITMAP bits = 0, mask = 0, mask_inv = 0;
559 Cursor cursor = 0;
561 if (!(src = CreateCompatibleDC( 0 ))) goto done;
562 if (!(dst = CreateCompatibleDC( 0 ))) goto done;
564 if (!(bits = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
565 if (!(mask = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
566 if (!(mask_inv = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
568 /* We have to do some magic here, as cursors are not fully
569 * compatible between Windows and X11. Under X11, there are
570 * only 3 possible color cursor: black, white and masked. So
571 * we map the 4th Windows color (invert the bits on the screen)
572 * to black and an additional white bit on an other place
573 * (+1,+1). This require some boolean arithmetic:
575 * Windows | X11
576 * And Xor Result | Bits Mask Result
577 * 0 0 black | 0 1 background
578 * 0 1 white | 1 1 foreground
579 * 1 0 no change | X 0 no change
580 * 1 1 inverted | 0 1 background
582 * which gives:
583 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
584 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
586 SelectObject( src, src_and );
587 SelectObject( dst, bits );
588 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
589 SelectObject( dst, mask );
590 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
591 SelectObject( dst, mask_inv );
592 BitBlt( dst, 0, 0, width, height, src, 0, and_y, SRCCOPY );
593 SelectObject( src, src_xor );
594 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, SRCAND /* src & dst */ );
595 SelectObject( dst, bits );
596 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, SRCERASE /* src & ~dst */ );
597 SelectObject( dst, mask );
598 BitBlt( dst, 0, 0, width, height, src, 0, xor_y, 0xdd0228 /* src | ~dst */ );
599 /* additional white */
600 SelectObject( src, mask_inv );
601 BitBlt( dst, 1, 1, width, height, src, 0, 0, SRCPAINT /* src | dst */);
602 SelectObject( dst, bits );
603 BitBlt( dst, 1, 1, width, height, src, 0, 0, SRCPAINT /* src | dst */ );
605 wine_tsx11_lock();
606 cursor = XCreatePixmapCursor( gdi_display, X11DRV_get_pixmap(bits), X11DRV_get_pixmap(mask),
607 fg, bg, hotspot_x, hotspot_y );
608 wine_tsx11_unlock();
610 done:
611 DeleteDC( src );
612 DeleteDC( dst );
613 DeleteObject( bits );
614 DeleteObject( mask );
615 DeleteObject( mask_inv );
616 return cursor;
619 /***********************************************************************
620 * create_xlib_cursor
622 * Create an X cursor from a Windows one.
624 static Cursor create_xlib_cursor( HDC hdc, ICONINFO *icon, int width, int height )
626 XColor fg, bg;
627 Cursor cursor = None;
628 HBITMAP xor_bitmap = 0;
629 BITMAPINFO *info;
630 unsigned int *color_bits = NULL, *ptr;
631 unsigned char *mask_bits = NULL, *xor_bits = NULL;
632 int i, x, y, has_alpha = 0;
633 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
634 unsigned int width_bytes = (width + 31) / 32 * 4;
636 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] ))))
637 return FALSE;
638 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
639 info->bmiHeader.biWidth = width;
640 info->bmiHeader.biHeight = -height;
641 info->bmiHeader.biPlanes = 1;
642 info->bmiHeader.biBitCount = 1;
643 info->bmiHeader.biCompression = BI_RGB;
644 info->bmiHeader.biSizeImage = width_bytes * height;
645 info->bmiHeader.biXPelsPerMeter = 0;
646 info->bmiHeader.biYPelsPerMeter = 0;
647 info->bmiHeader.biClrUsed = 0;
648 info->bmiHeader.biClrImportant = 0;
650 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
651 if (!GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS )) goto done;
653 info->bmiHeader.biBitCount = 32;
654 info->bmiHeader.biSizeImage = width * height * 4;
655 if (!(color_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
656 if (!(xor_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, width_bytes * height ))) goto done;
657 GetDIBits( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS );
659 /* compute fg/bg color and xor bitmap based on average of the color values */
661 if (!(xor_bitmap = CreateBitmap( width, height, 1, 1, NULL ))) goto done;
662 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
663 for (y = 0, ptr = color_bits; y < height; y++)
665 for (x = 0; x < width; x++, ptr++)
667 int red = (*ptr >> 16) & 0xff;
668 int green = (*ptr >> 8) & 0xff;
669 int blue = (*ptr >> 0) & 0xff;
670 if (red + green + blue > 0x40)
672 rfg += red;
673 gfg += green;
674 bfg += blue;
675 fgBits++;
676 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
678 else
680 rbg += red;
681 gbg += green;
682 bbg += blue;
686 if (fgBits)
688 fg.red = rfg * 257 / fgBits;
689 fg.green = gfg * 257 / fgBits;
690 fg.blue = bfg * 257 / fgBits;
692 else fg.red = fg.green = fg.blue = 0;
693 bgBits = width * height - fgBits;
694 if (bgBits)
696 bg.red = rbg * 257 / bgBits;
697 bg.green = gbg * 257 / bgBits;
698 bg.blue = bbg * 257 / bgBits;
700 else bg.red = bg.green = bg.blue = 0;
702 info->bmiHeader.biBitCount = 1;
703 info->bmiHeader.biSizeImage = width_bytes * height;
704 SetDIBits( hdc, xor_bitmap, 0, height, xor_bits, info, DIB_RGB_COLORS );
706 /* generate mask from the alpha channel if we have one */
708 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
709 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
711 if (has_alpha)
713 memset( mask_bits, 0, width_bytes * height );
714 for (y = 0, ptr = color_bits; y < height; y++)
715 for (x = 0; x < width; x++, ptr++)
716 if ((*ptr >> 24) > 25) /* more than 10% alpha */
717 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
719 info->bmiHeader.biBitCount = 1;
720 info->bmiHeader.biSizeImage = width_bytes * height;
721 SetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS );
723 wine_tsx11_lock();
724 cursor = XCreatePixmapCursor( gdi_display,
725 X11DRV_get_pixmap(xor_bitmap),
726 X11DRV_get_pixmap(icon->hbmMask),
727 &fg, &bg, icon->xHotspot, icon->yHotspot );
728 wine_tsx11_unlock();
730 else
732 cursor = create_cursor_from_bitmaps( xor_bitmap, icon->hbmMask, width, height, 0, 0,
733 &fg, &bg, icon->xHotspot, icon->yHotspot );
736 done:
737 DeleteObject( xor_bitmap );
738 HeapFree( GetProcessHeap(), 0, info );
739 HeapFree( GetProcessHeap(), 0, color_bits );
740 HeapFree( GetProcessHeap(), 0, xor_bits );
741 HeapFree( GetProcessHeap(), 0, mask_bits );
742 return cursor;
745 /***********************************************************************
746 * create_cursor
748 * Create an X cursor from a Windows one.
750 static Cursor create_cursor( HANDLE handle )
752 Cursor cursor = 0;
753 HDC hdc;
754 ICONINFO info;
755 BITMAP bm;
757 if (!handle) return get_empty_cursor();
759 if (!(hdc = CreateCompatibleDC( 0 ))) return 0;
760 if (!GetIconInfo( handle, &info ))
762 DeleteDC( hdc );
763 return 0;
766 GetObjectW( info.hbmMask, sizeof(bm), &bm );
767 if (!info.hbmColor) bm.bmHeight /= 2;
769 /* make sure hotspot is valid */
770 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
772 info.xHotspot = bm.bmWidth / 2;
773 info.yHotspot = bm.bmHeight / 2;
776 if (info.hbmColor)
778 #ifdef SONAME_LIBXCURSOR
779 if (pXcursorImageLoadCursor) cursor = create_xcursor_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
780 #endif
781 if (!cursor) cursor = create_xlib_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
782 DeleteObject( info.hbmColor );
784 else
786 XColor fg, bg;
787 fg.red = fg.green = fg.blue = 0xffff;
788 bg.red = bg.green = bg.blue = 0;
789 cursor = create_cursor_from_bitmaps( info.hbmMask, info.hbmMask, bm.bmWidth, bm.bmHeight,
790 bm.bmHeight, 0, &fg, &bg, info.xHotspot, info.yHotspot );
793 DeleteObject( info.hbmMask );
794 DeleteDC( hdc );
795 return cursor;
798 /***********************************************************************
799 * CreateCursorIcon (X11DRV.@)
801 void CDECL X11DRV_CreateCursorIcon( HCURSOR handle, CURSORICONINFO *info )
803 static const WORD ICON_HOTSPOT = 0x4242;
804 Cursor cursor, prev;
806 /* ignore icons (FIXME: shouldn't use magic hotspot value) */
807 if (info->ptHotSpot.x == ICON_HOTSPOT && info->ptHotSpot.y == ICON_HOTSPOT) return;
809 cursor = create_cursor( handle );
810 if (cursor)
812 wine_tsx11_lock();
813 if (!cursor_context) cursor_context = XUniqueContext();
814 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&prev ))
816 /* someone else was here first */
817 XFreeCursor( gdi_display, cursor );
818 cursor = prev;
820 else
821 XSaveContext( gdi_display, (XID)handle, cursor_context, (char *)cursor );
822 wine_tsx11_unlock();
823 TRACE( "cursor %p %ux%u, planes %u, bpp %u -> xid %lx\n",
824 handle, info->nWidth, info->nHeight, info->bPlanes, info->bBitsPerPixel, cursor );
828 /***********************************************************************
829 * DestroyCursorIcon (X11DRV.@)
831 void CDECL X11DRV_DestroyCursorIcon( HCURSOR handle )
833 Cursor cursor;
835 wine_tsx11_lock();
836 if ((cursor = get_x11_cursor( handle )))
838 TRACE( "%p xid %lx\n", handle, cursor );
839 XFreeCursor( gdi_display, cursor );
840 XDeleteContext( gdi_display, (XID)handle, cursor_context );
842 wine_tsx11_unlock();
845 /***********************************************************************
846 * SetCursor (X11DRV.@)
848 void CDECL X11DRV_SetCursor( HCURSOR handle )
850 if (cursor_window) SendNotifyMessageW( cursor_window, WM_X11DRV_SET_CURSOR, 0, (LPARAM)handle );
853 /***********************************************************************
854 * SetCursorPos (X11DRV.@)
856 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
858 Display *display = thread_init_display();
859 POINT pt;
861 TRACE( "warping to (%d,%d)\n", x, y );
863 wine_tsx11_lock();
864 if (cursor_pos.x == x && cursor_pos.y == y)
866 wine_tsx11_unlock();
867 /* We still need to generate WM_MOUSEMOVE */
868 queue_raw_mouse_message( WM_MOUSEMOVE, NULL, x, y, 0, GetCurrentTime(), 0, 0 );
869 return TRUE;
872 pt.x = x; pt.y = y;
873 clip_point_to_rect( &cursor_clip, &pt);
874 XWarpPointer( display, root_window, root_window, 0, 0, 0, 0,
875 pt.x - virtual_screen_rect.left, pt.y - virtual_screen_rect.top );
876 XFlush( display ); /* avoids bad mouse lag in games that do their own mouse warping */
877 cursor_pos = pt;
878 wine_tsx11_unlock();
879 return TRUE;
882 /***********************************************************************
883 * GetCursorPos (X11DRV.@)
885 BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
887 Display *display = thread_init_display();
888 Window root, child;
889 int rootX, rootY, winX, winY;
890 unsigned int xstate;
892 wine_tsx11_lock();
893 if ((GetTickCount() - last_time_modified > 100) &&
894 XQueryPointer( display, root_window, &root, &child,
895 &rootX, &rootY, &winX, &winY, &xstate ))
897 update_button_state( xstate );
898 winX += virtual_screen_rect.left;
899 winY += virtual_screen_rect.top;
900 TRACE("pointer at (%d,%d)\n", winX, winY );
901 cursor_pos.x = winX;
902 cursor_pos.y = winY;
904 *pos = cursor_pos;
905 wine_tsx11_unlock();
906 return TRUE;
910 /***********************************************************************
911 * ClipCursor (X11DRV.@)
913 * Set the cursor clipping rectangle.
915 BOOL CDECL X11DRV_ClipCursor( LPCRECT clip )
917 if (!IntersectRect( &cursor_clip, &virtual_screen_rect, clip ))
918 cursor_clip = virtual_screen_rect;
920 return TRUE;
923 /***********************************************************************
924 * X11DRV_ButtonPress
926 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
928 XButtonEvent *event = &xev->xbutton;
929 int buttonNum = event->button - 1;
930 WORD wData = 0;
931 POINT pt;
933 if (buttonNum >= NB_BUTTONS) return;
934 if (!hwnd) return;
936 switch (buttonNum)
938 case 3:
939 wData = WHEEL_DELTA;
940 break;
941 case 4:
942 wData = -WHEEL_DELTA;
943 break;
944 case 5:
945 wData = XBUTTON1;
946 break;
947 case 6:
948 wData = XBUTTON2;
949 break;
950 case 7:
951 wData = XBUTTON1;
952 break;
953 case 8:
954 wData = XBUTTON2;
955 break;
958 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
960 X11DRV_send_mouse_input( hwnd, button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
961 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
965 /***********************************************************************
966 * X11DRV_ButtonRelease
968 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
970 XButtonEvent *event = &xev->xbutton;
971 int buttonNum = event->button - 1;
972 WORD wData = 0;
973 POINT pt;
975 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
976 if (!hwnd) return;
978 switch (buttonNum)
980 case 5:
981 wData = XBUTTON1;
982 break;
983 case 6:
984 wData = XBUTTON2;
985 break;
986 case 7:
987 wData = XBUTTON1;
988 break;
989 case 8:
990 wData = XBUTTON2;
991 break;
994 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
996 X11DRV_send_mouse_input( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
997 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1001 /***********************************************************************
1002 * X11DRV_MotionNotify
1004 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1006 XMotionEvent *event = &xev->xmotion;
1007 POINT pt;
1009 TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);
1011 if (!hwnd) return;
1013 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1015 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1016 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1020 /***********************************************************************
1021 * X11DRV_EnterNotify
1023 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1025 XCrossingEvent *event = &xev->xcrossing;
1026 POINT pt;
1028 TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);
1030 if (!hwnd) return;
1031 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1032 if (event->window == x11drv_thread_data()->grab_window) return;
1034 /* simulate a mouse motion event */
1035 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1037 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1038 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );