iphlpapi: Add partial implementation of GetIfTable2Ex.
[wine.git] / dlls / winex11.drv / mouse.c
blob259263d567371818e50ab847178f07d2ba5ca476
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 <X11/cursorfont.h>
27 #include <stdarg.h>
28 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
29 #include <X11/extensions/XInput2.h>
30 #endif
32 #ifdef SONAME_LIBXCURSOR
33 # include <X11/Xcursor/Xcursor.h>
34 static void *xcursor_handle;
35 # define MAKE_FUNCPTR(f) static typeof(f) * p##f
36 MAKE_FUNCPTR(XcursorImageCreate);
37 MAKE_FUNCPTR(XcursorImageDestroy);
38 MAKE_FUNCPTR(XcursorImageLoadCursor);
39 MAKE_FUNCPTR(XcursorImagesCreate);
40 MAKE_FUNCPTR(XcursorImagesDestroy);
41 MAKE_FUNCPTR(XcursorImagesLoadCursor);
42 MAKE_FUNCPTR(XcursorLibraryLoadCursor);
43 # undef MAKE_FUNCPTR
44 #endif /* SONAME_LIBXCURSOR */
46 #define NONAMELESSUNION
47 #define OEMRESOURCE
48 #include "windef.h"
49 #include "winbase.h"
50 #include "winreg.h"
52 #include "x11drv.h"
53 #include "wine/server.h"
54 #include "wine/library.h"
55 #include "wine/unicode.h"
56 #include "wine/debug.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
60 /**********************************************************************/
62 #ifndef Button6Mask
63 #define Button6Mask (1<<13)
64 #endif
65 #ifndef Button7Mask
66 #define Button7Mask (1<<14)
67 #endif
69 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
71 static const UINT button_down_flags[NB_BUTTONS] =
73 MOUSEEVENTF_LEFTDOWN,
74 MOUSEEVENTF_MIDDLEDOWN,
75 MOUSEEVENTF_RIGHTDOWN,
76 MOUSEEVENTF_WHEEL,
77 MOUSEEVENTF_WHEEL,
78 MOUSEEVENTF_XDOWN, /* FIXME: horizontal wheel */
79 MOUSEEVENTF_XDOWN,
80 MOUSEEVENTF_XDOWN,
81 MOUSEEVENTF_XDOWN
84 static const UINT button_up_flags[NB_BUTTONS] =
86 MOUSEEVENTF_LEFTUP,
87 MOUSEEVENTF_MIDDLEUP,
88 MOUSEEVENTF_RIGHTUP,
91 MOUSEEVENTF_XUP,
92 MOUSEEVENTF_XUP,
93 MOUSEEVENTF_XUP,
94 MOUSEEVENTF_XUP
97 static const UINT button_down_data[NB_BUTTONS] =
102 WHEEL_DELTA,
103 -WHEEL_DELTA,
104 XBUTTON1,
105 XBUTTON2,
106 XBUTTON1,
107 XBUTTON2
110 static const UINT button_up_data[NB_BUTTONS] =
117 XBUTTON1,
118 XBUTTON2,
119 XBUTTON1,
120 XBUTTON2
123 XContext cursor_context = 0;
125 static HWND cursor_window;
126 static HCURSOR last_cursor;
127 static DWORD last_cursor_change;
128 static RECT clip_rect;
129 static Cursor create_cursor( HANDLE handle );
131 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
132 static BOOL xinput2_available;
133 static BOOL broken_rawevents;
134 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
135 MAKE_FUNCPTR(XIGetClientPointer);
136 MAKE_FUNCPTR(XIFreeDeviceInfo);
137 MAKE_FUNCPTR(XIQueryDevice);
138 MAKE_FUNCPTR(XIQueryVersion);
139 MAKE_FUNCPTR(XISelectEvents);
140 #undef MAKE_FUNCPTR
141 #endif
143 /***********************************************************************
144 * X11DRV_Xcursor_Init
146 * Load the Xcursor library for use.
148 void X11DRV_Xcursor_Init(void)
150 #ifdef SONAME_LIBXCURSOR
151 xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
152 if (!xcursor_handle) /* wine_dlopen failed. */
154 WARN("Xcursor failed to load. Using fallback code.\n");
155 return;
157 #define LOAD_FUNCPTR(f) \
158 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
160 LOAD_FUNCPTR(XcursorImageCreate);
161 LOAD_FUNCPTR(XcursorImageDestroy);
162 LOAD_FUNCPTR(XcursorImageLoadCursor);
163 LOAD_FUNCPTR(XcursorImagesCreate);
164 LOAD_FUNCPTR(XcursorImagesDestroy);
165 LOAD_FUNCPTR(XcursorImagesLoadCursor);
166 LOAD_FUNCPTR(XcursorLibraryLoadCursor);
167 #undef LOAD_FUNCPTR
168 #endif /* SONAME_LIBXCURSOR */
172 /***********************************************************************
173 * get_empty_cursor
175 static Cursor get_empty_cursor(void)
177 static Cursor cursor;
178 static const char data[] = { 0 };
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 new = XCreatePixmapCursor( gdi_display, pixmap, pixmap, &bg, &bg, 0, 0 );
190 if (InterlockedCompareExchangePointer( (void **)&cursor, (void *)new, 0 ))
191 XFreeCursor( gdi_display, new );
192 XFreePixmap( gdi_display, pixmap );
195 return cursor;
198 /***********************************************************************
199 * set_window_cursor
201 void set_window_cursor( Window window, HCURSOR handle )
203 Cursor cursor, prev;
205 if (!handle) cursor = get_empty_cursor();
206 else if (XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
208 /* try to create it */
209 if (!(cursor = create_cursor( handle ))) return;
211 XLockDisplay( gdi_display );
212 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&prev ))
214 /* someone else was here first */
215 XFreeCursor( gdi_display, cursor );
216 cursor = prev;
218 else
220 XSaveContext( gdi_display, (XID)handle, cursor_context, (char *)cursor );
221 TRACE( "cursor %p created %lx\n", handle, cursor );
223 XUnlockDisplay( gdi_display );
226 XDefineCursor( gdi_display, window, cursor );
227 /* make the change take effect immediately */
228 XFlush( gdi_display );
231 /***********************************************************************
232 * sync_window_cursor
234 void sync_window_cursor( Window window )
236 HCURSOR cursor;
238 SERVER_START_REQ( set_cursor )
240 req->flags = 0;
241 wine_server_call( req );
242 cursor = reply->prev_count >= 0 ? wine_server_ptr_handle( reply->prev_handle ) : 0;
244 SERVER_END_REQ;
246 set_window_cursor( window, cursor );
249 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
250 /***********************************************************************
251 * update_relative_valuators
253 static void update_relative_valuators(XIAnyClassInfo **valuators, int n_valuators)
255 struct x11drv_thread_data *thread_data = x11drv_thread_data();
256 int i;
258 thread_data->x_rel_valuator.number = -1;
259 thread_data->y_rel_valuator.number = -1;
261 for (i = 0; i < n_valuators; i++)
263 XIValuatorClassInfo *class = (XIValuatorClassInfo *)valuators[i];
264 struct x11drv_valuator_data *valuator_data = NULL;
266 if (valuators[i]->type != XIValuatorClass) continue;
267 if (class->label == x11drv_atom( Rel_X ) ||
268 (!class->label && class->number == 0 && class->mode == XIModeRelative))
270 valuator_data = &thread_data->x_rel_valuator;
272 else if (class->label == x11drv_atom( Rel_Y ) ||
273 (!class->label && class->number == 1 && class->mode == XIModeRelative))
275 valuator_data = &thread_data->y_rel_valuator;
278 if (valuator_data) {
279 valuator_data->number = class->number;
280 valuator_data->min = class->min;
281 valuator_data->max = class->max;
285 #endif
288 /***********************************************************************
289 * enable_xinput2
291 static void enable_xinput2(void)
293 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
294 struct x11drv_thread_data *data = x11drv_thread_data();
295 XIEventMask mask;
296 XIDeviceInfo *pointer_info;
297 unsigned char mask_bits[XIMaskLen(XI_LASTEVENT)];
298 int count;
300 if (!xinput2_available) return;
302 if (data->xi2_state == xi_unknown)
304 int major = 2, minor = 0;
305 if (!pXIQueryVersion( data->display, &major, &minor )) data->xi2_state = xi_disabled;
306 else
308 data->xi2_state = xi_unavailable;
309 WARN( "X Input 2 not available\n" );
312 if (data->xi2_state == xi_unavailable) return;
313 if (!pXIGetClientPointer( data->display, None, &data->xi2_core_pointer )) return;
315 mask.mask = mask_bits;
316 mask.mask_len = sizeof(mask_bits);
317 mask.deviceid = XIAllDevices;
318 memset( mask_bits, 0, sizeof(mask_bits) );
319 XISetMask( mask_bits, XI_DeviceChanged );
320 XISetMask( mask_bits, XI_RawMotion );
321 XISetMask( mask_bits, XI_ButtonPress );
323 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
325 pointer_info = pXIQueryDevice( data->display, data->xi2_core_pointer, &count );
326 update_relative_valuators( pointer_info->classes, pointer_info->num_classes );
327 pXIFreeDeviceInfo( pointer_info );
329 /* This device info list is only used to find the initial current slave if
330 * no XI_DeviceChanged events happened. If any hierarchy change occurred that
331 * might be relevant here (eg. user switching mice after (un)plugging), a
332 * XI_DeviceChanged event will point us to the right slave. So this list is
333 * safe to be obtained statically at enable_xinput2() time.
335 if (data->xi2_devices) pXIFreeDeviceInfo( data->xi2_devices );
336 data->xi2_devices = pXIQueryDevice( data->display, XIAllDevices, &data->xi2_device_count );
337 data->xi2_current_slave = 0;
339 data->xi2_state = xi_enabled;
340 #endif
343 /***********************************************************************
344 * disable_xinput2
346 static void disable_xinput2(void)
348 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
349 struct x11drv_thread_data *data = x11drv_thread_data();
350 XIEventMask mask;
352 if (data->xi2_state != xi_enabled) return;
354 TRACE( "disabling\n" );
355 data->xi2_state = xi_disabled;
357 mask.mask = NULL;
358 mask.mask_len = 0;
359 mask.deviceid = XIAllDevices;
361 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
362 pXIFreeDeviceInfo( data->xi2_devices );
363 data->x_rel_valuator.number = -1;
364 data->y_rel_valuator.number = -1;
365 data->xi2_devices = NULL;
366 data->xi2_core_pointer = 0;
367 data->xi2_current_slave = 0;
368 #endif
372 /***********************************************************************
373 * grab_clipping_window
375 * Start a pointer grab on the clip window.
377 static BOOL grab_clipping_window( const RECT *clip )
379 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
380 struct x11drv_thread_data *data = x11drv_thread_data();
381 Window clip_window;
382 HWND msg_hwnd = 0;
383 POINT pos;
385 if (GetWindowThreadProcessId( GetDesktopWindow(), NULL ) == GetCurrentThreadId())
386 return TRUE; /* don't clip in the desktop process */
388 if (!data) return FALSE;
389 if (!(clip_window = init_clip_window())) return TRUE;
391 if (!(msg_hwnd = CreateWindowW( messageW, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0,
392 GetModuleHandleW(0), NULL )))
393 return TRUE;
395 /* enable XInput2 unless we are already clipping */
396 if (!data->clip_hwnd) enable_xinput2();
398 if (data->xi2_state != xi_enabled)
400 WARN( "XInput2 not supported, refusing to clip to %s\n", wine_dbgstr_rect(clip) );
401 DestroyWindow( msg_hwnd );
402 ClipCursor( NULL );
403 return TRUE;
406 TRACE( "clipping to %s win %lx\n", wine_dbgstr_rect(clip), clip_window );
408 if (!data->clip_hwnd) XUnmapWindow( data->display, clip_window );
409 pos = virtual_screen_to_root( clip->left, clip->top );
410 XMoveResizeWindow( data->display, clip_window, pos.x, pos.y,
411 max( 1, clip->right - clip->left ), max( 1, clip->bottom - clip->top ) );
412 XMapWindow( data->display, clip_window );
414 /* if the rectangle is shrinking we may get a pointer warp */
415 if (!data->clip_hwnd || clip->left > clip_rect.left || clip->top > clip_rect.top ||
416 clip->right < clip_rect.right || clip->bottom < clip_rect.bottom)
417 data->warp_serial = NextRequest( data->display );
419 if (!XGrabPointer( data->display, clip_window, False,
420 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
421 GrabModeAsync, GrabModeAsync, clip_window, None, CurrentTime ))
422 clipping_cursor = TRUE;
424 if (!clipping_cursor)
426 disable_xinput2();
427 DestroyWindow( msg_hwnd );
428 return FALSE;
430 clip_rect = *clip;
431 if (!data->clip_hwnd) sync_window_cursor( clip_window );
432 InterlockedExchangePointer( (void **)&cursor_window, msg_hwnd );
433 data->clip_hwnd = msg_hwnd;
434 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, (LPARAM)msg_hwnd );
435 return TRUE;
438 /***********************************************************************
439 * ungrab_clipping_window
441 * Release the pointer grab on the clip window.
443 void ungrab_clipping_window(void)
445 Display *display = thread_init_display();
446 Window clip_window = init_clip_window();
448 if (!clip_window) return;
450 TRACE( "no longer clipping\n" );
451 XUnmapWindow( display, clip_window );
452 clipping_cursor = FALSE;
453 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, 0 );
456 /***********************************************************************
457 * reset_clipping_window
459 * Forcibly reset the window clipping on external events.
461 void reset_clipping_window(void)
463 ungrab_clipping_window();
464 ClipCursor( NULL ); /* make sure the clip rectangle is reset too */
467 /***********************************************************************
468 * clip_cursor_notify
470 * Notification function called upon receiving a WM_X11DRV_CLIP_CURSOR.
472 LRESULT clip_cursor_notify( HWND hwnd, HWND new_clip_hwnd )
474 struct x11drv_thread_data *data = x11drv_init_thread_data();
476 if (hwnd == GetDesktopWindow()) /* change the clip window stored in the desktop process */
478 static HWND clip_hwnd;
480 HWND prev = clip_hwnd;
481 clip_hwnd = new_clip_hwnd;
482 if (prev || new_clip_hwnd) TRACE( "clip hwnd changed from %p to %p\n", prev, new_clip_hwnd );
483 if (prev) SendNotifyMessageW( prev, WM_X11DRV_CLIP_CURSOR, 0, 0 );
485 else if (hwnd == data->clip_hwnd) /* this is a notification that clipping has been reset */
487 TRACE( "clip hwnd reset from %p\n", hwnd );
488 data->clip_hwnd = 0;
489 data->clip_reset = GetTickCount();
490 disable_xinput2();
491 DestroyWindow( hwnd );
493 else if (hwnd == GetForegroundWindow()) /* request to clip */
495 RECT clip, virtual_rect = get_virtual_screen_rect();
497 GetClipCursor( &clip );
498 if (clip.left > virtual_rect.left || clip.right < virtual_rect.right ||
499 clip.top > virtual_rect.top || clip.bottom < virtual_rect.bottom)
500 return grab_clipping_window( &clip );
502 return 0;
505 /***********************************************************************
506 * clip_fullscreen_window
508 * Turn on clipping if the active window is fullscreen.
510 BOOL clip_fullscreen_window( HWND hwnd, BOOL reset )
512 struct x11drv_win_data *data;
513 struct x11drv_thread_data *thread_data;
514 RECT rect;
515 DWORD style;
516 BOOL fullscreen;
518 if (hwnd == GetDesktopWindow()) return FALSE;
519 style = GetWindowLongW( hwnd, GWL_STYLE );
520 if (!(style & WS_VISIBLE)) return FALSE;
521 if ((style & (WS_POPUP | WS_CHILD)) == WS_CHILD) return FALSE;
522 /* maximized windows don't count as full screen */
523 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION) return FALSE;
524 if (!(data = get_win_data( hwnd ))) return FALSE;
525 fullscreen = is_window_rect_fullscreen( &data->whole_rect );
526 release_win_data( data );
527 if (!fullscreen) return FALSE;
528 if (!(thread_data = x11drv_thread_data())) return FALSE;
529 if (GetTickCount() - thread_data->clip_reset < 1000) return FALSE;
530 if (!reset && clipping_cursor && thread_data->clip_hwnd) return FALSE; /* already clipping */
531 rect = get_primary_monitor_rect();
532 if (!grab_fullscreen)
534 RECT virtual_rect = get_virtual_screen_rect();
535 if (!EqualRect( &rect, &virtual_rect )) return FALSE;
536 if (root_window != DefaultRootWindow( gdi_display )) return FALSE;
538 TRACE( "win %p clipping fullscreen\n", hwnd );
539 return grab_clipping_window( &rect );
543 /***********************************************************************
544 * is_old_motion_event
546 static BOOL is_old_motion_event( unsigned long serial )
548 struct x11drv_thread_data *thread_data = x11drv_thread_data();
550 if (!thread_data->warp_serial) return FALSE;
551 if ((long)(serial - thread_data->warp_serial) < 0) return TRUE;
552 thread_data->warp_serial = 0; /* we caught up now */
553 return FALSE;
557 /***********************************************************************
558 * send_mouse_input
560 * Update the various window states on a mouse event.
562 static void send_mouse_input( HWND hwnd, Window window, unsigned int state, INPUT *input )
564 struct x11drv_win_data *data;
565 POINT pt;
567 input->type = INPUT_MOUSE;
569 if (!hwnd)
571 struct x11drv_thread_data *thread_data = x11drv_thread_data();
572 HWND clip_hwnd = thread_data->clip_hwnd;
574 if (!clip_hwnd) return;
575 if (thread_data->clip_window != window) return;
576 if (InterlockedExchangePointer( (void **)&cursor_window, clip_hwnd ) != clip_hwnd ||
577 input->u.mi.time - last_cursor_change > 100)
579 sync_window_cursor( window );
580 last_cursor_change = input->u.mi.time;
582 input->u.mi.dx += clip_rect.left;
583 input->u.mi.dy += clip_rect.top;
584 __wine_send_input( hwnd, input );
585 return;
588 if (window != root_window)
590 pt.x = input->u.mi.dx;
591 pt.y = input->u.mi.dy;
593 else pt = root_to_virtual_screen( input->u.mi.dx, input->u.mi.dy );
595 if (!(data = get_win_data( hwnd ))) return;
597 if (window == data->whole_window)
599 pt.x += data->whole_rect.left - data->client_rect.left;
600 pt.y += data->whole_rect.top - data->client_rect.top;
603 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
604 pt.x = data->client_rect.right - data->client_rect.left - 1 - pt.x;
605 MapWindowPoints( hwnd, 0, &pt, 1 );
607 if (InterlockedExchangePointer( (void **)&cursor_window, hwnd ) != hwnd ||
608 input->u.mi.time - last_cursor_change > 100)
610 sync_window_cursor( data->whole_window );
611 last_cursor_change = input->u.mi.time;
613 release_win_data( data );
615 if (hwnd != GetDesktopWindow())
617 hwnd = GetAncestor( hwnd, GA_ROOT );
618 if ((input->u.mi.dwFlags & (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_RIGHTDOWN)) && hwnd == GetForegroundWindow())
619 clip_fullscreen_window( hwnd, FALSE );
622 /* update the wine server Z-order */
624 if (hwnd != x11drv_thread_data()->grab_hwnd &&
625 /* ignore event if a button is pressed, since the mouse is then grabbed too */
626 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
628 RECT rect;
629 SetRect( &rect, pt.x, pt.y, pt.x + 1, pt.y + 1 );
630 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
632 SERVER_START_REQ( update_window_zorder )
634 req->window = wine_server_user_handle( hwnd );
635 req->rect.left = rect.left;
636 req->rect.top = rect.top;
637 req->rect.right = rect.right;
638 req->rect.bottom = rect.bottom;
639 wine_server_call( req );
641 SERVER_END_REQ;
644 input->u.mi.dx = pt.x;
645 input->u.mi.dy = pt.y;
646 __wine_send_input( hwnd, input );
649 #ifdef SONAME_LIBXCURSOR
651 /***********************************************************************
652 * create_xcursor_frame
654 * Use Xcursor to create a frame of an X cursor from a Windows one.
656 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
657 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
658 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
659 int width, int height, int istep )
661 XcursorImage *image, *ret = NULL;
662 DWORD delay_jiffies, num_steps;
663 int x, y, i;
664 BOOL has_alpha = FALSE;
665 XcursorPixel *ptr;
667 image = pXcursorImageCreate( width, height );
668 if (!image)
670 ERR("X11 failed to produce a cursor frame!\n");
671 return NULL;
674 image->xhot = iinfo->xHotspot;
675 image->yhot = iinfo->yHotspot;
677 image->delay = 100; /* fallback delay, 100 ms */
678 if (GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, istep, &delay_jiffies, &num_steps) != 0)
679 image->delay = (100 * delay_jiffies) / 6; /* convert jiffies (1/60s) to milliseconds */
680 else
681 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep);
683 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
684 memset( color_bits, 0x00, color_size );
685 SelectObject( hdc, hbmColor );
686 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
688 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
689 goto cleanup;
691 memcpy( image->pixels, color_bits, color_size );
693 /* check if the cursor frame was drawn with an alpha channel */
694 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
695 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
697 /* if no alpha channel was drawn then generate it from the mask */
698 if (!has_alpha)
700 unsigned int width_bytes = (width + 31) / 32 * 4;
702 /* draw the cursor mask to a temporary buffer */
703 memset( mask_bits, 0xFF, mask_size );
704 SelectObject( hdc, hbmMask );
705 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
707 ERR("Failed to draw frame mask %d.\n", istep);
708 goto cleanup;
710 /* use the buffer to directly modify the XcursorImage alpha channel */
711 for (y = 0, ptr = image->pixels; y < height; y++)
712 for (x = 0; x < width; x++, ptr++)
713 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
714 *ptr |= 0xff000000;
716 ret = image;
718 cleanup:
719 if (ret == NULL) pXcursorImageDestroy( image );
720 return ret;
723 /***********************************************************************
724 * create_xcursor_cursor
726 * Use Xcursor to create an X cursor from a Windows one.
728 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
730 unsigned char *color_bits, *mask_bits;
731 HBITMAP hbmColor = 0, hbmMask = 0;
732 DWORD nFrames, delay_jiffies, i;
733 int color_size, mask_size;
734 BITMAPINFO *info = NULL;
735 XcursorImages *images;
736 XcursorImage **imgs;
737 Cursor cursor = 0;
739 /* Retrieve the number of frames to render */
740 if (!GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, 0, &delay_jiffies, &nFrames)) return 0;
741 if (!(imgs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XcursorImage*)*nFrames ))) return 0;
743 /* Allocate all of the resources necessary to obtain a cursor frame */
744 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
745 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
746 info->bmiHeader.biWidth = width;
747 info->bmiHeader.biHeight = -height;
748 info->bmiHeader.biPlanes = 1;
749 info->bmiHeader.biCompression = BI_RGB;
750 info->bmiHeader.biXPelsPerMeter = 0;
751 info->bmiHeader.biYPelsPerMeter = 0;
752 info->bmiHeader.biClrUsed = 0;
753 info->bmiHeader.biClrImportant = 0;
754 info->bmiHeader.biBitCount = 32;
755 color_size = width * height * 4;
756 info->bmiHeader.biSizeImage = color_size;
757 hbmColor = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &color_bits, NULL, 0);
758 if (!hbmColor)
760 ERR("Failed to create DIB section for cursor color data!\n");
761 goto cleanup;
763 info->bmiHeader.biBitCount = 1;
764 info->bmiColors[0].rgbRed = 0;
765 info->bmiColors[0].rgbGreen = 0;
766 info->bmiColors[0].rgbBlue = 0;
767 info->bmiColors[0].rgbReserved = 0;
768 info->bmiColors[1].rgbRed = 0xff;
769 info->bmiColors[1].rgbGreen = 0xff;
770 info->bmiColors[1].rgbBlue = 0xff;
771 info->bmiColors[1].rgbReserved = 0;
773 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
774 info->bmiHeader.biSizeImage = mask_size;
775 hbmMask = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &mask_bits, NULL, 0);
776 if (!hbmMask)
778 ERR("Failed to create DIB section for cursor mask data!\n");
779 goto cleanup;
782 /* Create an XcursorImage for each frame of the cursor */
783 for (i=0; i<nFrames; i++)
785 imgs[i] = create_xcursor_frame( hdc, iinfo, icon,
786 hbmColor, color_bits, color_size,
787 hbmMask, mask_bits, mask_size,
788 width, height, i );
789 if (!imgs[i]) goto cleanup;
792 /* Build an X cursor out of all of the frames */
793 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
794 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
795 images->images[images->nimage] = imgs[images->nimage];
796 cursor = pXcursorImagesLoadCursor( gdi_display, images );
797 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
798 HeapFree( GetProcessHeap(), 0, imgs );
799 imgs = NULL;
801 cleanup:
802 if (imgs)
804 /* Failed to produce a cursor, free previously allocated frames */
805 for (i=0; i<nFrames && imgs[i]; i++)
806 pXcursorImageDestroy( imgs[i] );
807 HeapFree( GetProcessHeap(), 0, imgs );
809 /* Cleanup all of the resources used to obtain the frame data */
810 if (hbmColor) DeleteObject( hbmColor );
811 if (hbmMask) DeleteObject( hbmMask );
812 HeapFree( GetProcessHeap(), 0, info );
813 return cursor;
816 #endif /* SONAME_LIBXCURSOR */
819 struct system_cursors
821 WORD id;
822 const char *name;
825 static const struct system_cursors user32_cursors[] =
827 { OCR_NORMAL, "left_ptr" },
828 { OCR_IBEAM, "xterm" },
829 { OCR_WAIT, "watch" },
830 { OCR_CROSS, "cross" },
831 { OCR_UP, "center_ptr" },
832 { OCR_SIZE, "fleur" },
833 { OCR_SIZEALL, "fleur" },
834 { OCR_ICON, "icon" },
835 { OCR_SIZENWSE, "nwse-resize" },
836 { OCR_SIZENESW, "nesw-resize" },
837 { OCR_SIZEWE, "ew-resize" },
838 { OCR_SIZENS, "ns-resize" },
839 { OCR_NO, "not-allowed" },
840 { OCR_HAND, "hand2" },
841 { OCR_APPSTARTING, "left_ptr_watch" },
842 { OCR_HELP, "question_arrow" },
843 { 0 }
846 static const struct system_cursors comctl32_cursors[] =
848 { 102, "move" },
849 { 104, "copy" },
850 { 105, "left_ptr" },
851 { 106, "col-resize" },
852 { 107, "col-resize" },
853 { 108, "hand2" },
854 { 135, "row-resize" },
855 { 0 }
858 static const struct system_cursors ole32_cursors[] =
860 { 1, "no-drop" },
861 { 2, "move" },
862 { 3, "copy" },
863 { 4, "alias" },
864 { 0 }
867 static const struct system_cursors riched20_cursors[] =
869 { 105, "hand2" },
870 { 107, "right_ptr" },
871 { 109, "copy" },
872 { 110, "move" },
873 { 111, "no-drop" },
874 { 0 }
877 static const struct
879 const struct system_cursors *cursors;
880 WCHAR name[16];
881 } module_cursors[] =
883 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
884 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
885 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
886 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
889 struct cursor_font_fallback
891 const char *name;
892 unsigned int shape;
895 static const struct cursor_font_fallback fallbacks[] =
897 { "X_cursor", XC_X_cursor },
898 { "arrow", XC_arrow },
899 { "based_arrow_down", XC_based_arrow_down },
900 { "based_arrow_up", XC_based_arrow_up },
901 { "boat", XC_boat },
902 { "bogosity", XC_bogosity },
903 { "bottom_left_corner", XC_bottom_left_corner },
904 { "bottom_right_corner", XC_bottom_right_corner },
905 { "bottom_side", XC_bottom_side },
906 { "bottom_tee", XC_bottom_tee },
907 { "box_spiral", XC_box_spiral },
908 { "center_ptr", XC_center_ptr },
909 { "circle", XC_circle },
910 { "clock", XC_clock },
911 { "coffee_mug", XC_coffee_mug },
912 { "col-resize", XC_sb_h_double_arrow },
913 { "cross", XC_cross },
914 { "cross_reverse", XC_cross_reverse },
915 { "crosshair", XC_crosshair },
916 { "diamond_cross", XC_diamond_cross },
917 { "dot", XC_dot },
918 { "dotbox", XC_dotbox },
919 { "double_arrow", XC_double_arrow },
920 { "draft_large", XC_draft_large },
921 { "draft_small", XC_draft_small },
922 { "draped_box", XC_draped_box },
923 { "exchange", XC_exchange },
924 { "fleur", XC_fleur },
925 { "gobbler", XC_gobbler },
926 { "gumby", XC_gumby },
927 { "hand1", XC_hand1 },
928 { "hand2", XC_hand2 },
929 { "heart", XC_heart },
930 { "icon", XC_icon },
931 { "iron_cross", XC_iron_cross },
932 { "left_ptr", XC_left_ptr },
933 { "left_side", XC_left_side },
934 { "left_tee", XC_left_tee },
935 { "leftbutton", XC_leftbutton },
936 { "ll_angle", XC_ll_angle },
937 { "lr_angle", XC_lr_angle },
938 { "man", XC_man },
939 { "middlebutton", XC_middlebutton },
940 { "mouse", XC_mouse },
941 { "pencil", XC_pencil },
942 { "pirate", XC_pirate },
943 { "plus", XC_plus },
944 { "question_arrow", XC_question_arrow },
945 { "right_ptr", XC_right_ptr },
946 { "right_side", XC_right_side },
947 { "right_tee", XC_right_tee },
948 { "rightbutton", XC_rightbutton },
949 { "row-resize", XC_sb_v_double_arrow },
950 { "rtl_logo", XC_rtl_logo },
951 { "sailboat", XC_sailboat },
952 { "sb_down_arrow", XC_sb_down_arrow },
953 { "sb_h_double_arrow", XC_sb_h_double_arrow },
954 { "sb_left_arrow", XC_sb_left_arrow },
955 { "sb_right_arrow", XC_sb_right_arrow },
956 { "sb_up_arrow", XC_sb_up_arrow },
957 { "sb_v_double_arrow", XC_sb_v_double_arrow },
958 { "shuttle", XC_shuttle },
959 { "sizing", XC_sizing },
960 { "spider", XC_spider },
961 { "spraycan", XC_spraycan },
962 { "star", XC_star },
963 { "target", XC_target },
964 { "tcross", XC_tcross },
965 { "top_left_arrow", XC_top_left_arrow },
966 { "top_left_corner", XC_top_left_corner },
967 { "top_right_corner", XC_top_right_corner },
968 { "top_side", XC_top_side },
969 { "top_tee", XC_top_tee },
970 { "trek", XC_trek },
971 { "ul_angle", XC_ul_angle },
972 { "umbrella", XC_umbrella },
973 { "ur_angle", XC_ur_angle },
974 { "watch", XC_watch },
975 { "xterm", XC_xterm }
978 static int fallback_cmp( const void *key, const void *member )
980 const struct cursor_font_fallback *fallback = member;
981 return strcmp( key, fallback->name );
984 static int find_fallback_shape( const char *name )
986 struct cursor_font_fallback *fallback;
988 if ((fallback = bsearch( name, fallbacks, sizeof(fallbacks) / sizeof(fallbacks[0]),
989 sizeof(*fallback), fallback_cmp )))
990 return fallback->shape;
991 return -1;
994 /***********************************************************************
995 * create_xcursor_system_cursor
997 * Create an X cursor for a system cursor.
999 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
1001 static const WCHAR idW[] = {'%','h','u',0};
1002 const struct system_cursors *cursors;
1003 unsigned int i;
1004 Cursor cursor = 0;
1005 HMODULE module;
1006 HKEY key;
1007 WCHAR *p, name[MAX_PATH * 2], valueW[64];
1008 char valueA[64];
1009 DWORD ret;
1011 if (!info->szModName[0]) return 0;
1013 p = strrchrW( info->szModName, '\\' );
1014 strcpyW( name, p ? p + 1 : info->szModName );
1015 p = name + strlenW( name );
1016 *p++ = ',';
1017 if (info->szResName[0]) strcpyW( p, info->szResName );
1018 else sprintfW( p, idW, info->wResID );
1019 valueA[0] = 0;
1021 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
1022 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver\\Cursors", &key ))
1024 DWORD size = sizeof(valueW);
1025 ret = RegQueryValueExW( key, name, NULL, NULL, (BYTE *)valueW, &size );
1026 RegCloseKey( key );
1027 if (!ret)
1029 if (!valueW[0]) return 0; /* force standard cursor */
1030 if (!WideCharToMultiByte( CP_UNIXCP, 0, valueW, -1, valueA, sizeof(valueA), NULL, NULL ))
1031 valueA[0] = 0;
1032 goto done;
1036 if (info->szResName[0]) goto done; /* only integer resources are supported here */
1037 if (!(module = GetModuleHandleW( info->szModName ))) goto done;
1039 for (i = 0; i < sizeof(module_cursors)/sizeof(module_cursors[0]); i++)
1040 if (GetModuleHandleW( module_cursors[i].name ) == module) break;
1041 if (i == sizeof(module_cursors)/sizeof(module_cursors[0])) goto done;
1043 cursors = module_cursors[i].cursors;
1044 for (i = 0; cursors[i].id; i++)
1045 if (cursors[i].id == info->wResID)
1047 strcpy( valueA, cursors[i].name );
1048 break;
1051 done:
1052 if (valueA[0])
1054 #ifdef SONAME_LIBXCURSOR
1055 if (pXcursorLibraryLoadCursor) cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
1056 #endif
1057 if (!cursor)
1059 int shape = find_fallback_shape( valueA );
1060 if (shape != -1) cursor = XCreateFontCursor( gdi_display, shape );
1062 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
1063 debugstr_w(name), debugstr_a(valueA) );
1065 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
1066 return cursor;
1070 /***********************************************************************
1071 * create_xlib_monochrome_cursor
1073 * Create a monochrome X cursor from a Windows one.
1075 static Cursor create_xlib_monochrome_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1077 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1078 BITMAPINFO *info = (BITMAPINFO *)buffer;
1079 const int and_y = 0;
1080 const int xor_y = height;
1081 unsigned int width_bytes = (width + 31) / 32 * 4;
1082 unsigned char *mask_bits = NULL;
1083 GC gc;
1084 XColor fg, bg;
1085 XVisualInfo vis = default_visual;
1086 Pixmap src_pixmap, bits_pixmap, mask_pixmap;
1087 struct gdi_image_bits bits;
1088 Cursor cursor = 0;
1090 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1091 info->bmiHeader.biWidth = width;
1092 info->bmiHeader.biHeight = -height * 2;
1093 info->bmiHeader.biPlanes = 1;
1094 info->bmiHeader.biBitCount = 1;
1095 info->bmiHeader.biCompression = BI_RGB;
1096 info->bmiHeader.biSizeImage = width_bytes * height * 2;
1097 info->bmiHeader.biXPelsPerMeter = 0;
1098 info->bmiHeader.biYPelsPerMeter = 0;
1099 info->bmiHeader.biClrUsed = 0;
1100 info->bmiHeader.biClrImportant = 0;
1102 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1103 if (!GetDIBits( hdc, icon->hbmMask, 0, height * 2, mask_bits, info, DIB_RGB_COLORS )) goto done;
1105 vis.depth = 1;
1106 bits.ptr = mask_bits;
1107 bits.free = NULL;
1108 bits.is_copy = TRUE;
1109 if (!(src_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1111 bits_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
1112 mask_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
1113 gc = XCreateGC( gdi_display, src_pixmap, 0, NULL );
1114 XSetGraphicsExposures( gdi_display, gc, False );
1116 /* We have to do some magic here, as cursors are not fully
1117 * compatible between Windows and X11. Under X11, there are
1118 * only 3 possible color cursor: black, white and masked. So
1119 * we map the 4th Windows color (invert the bits on the screen)
1120 * to black and an additional white bit on another place
1121 * (+1,+1). This require some boolean arithmetic:
1123 * Windows | X11
1124 * And Xor Result | Bits Mask Result
1125 * 0 0 black | 0 1 background
1126 * 0 1 white | 1 1 foreground
1127 * 1 0 no change | X 0 no change
1128 * 1 1 inverted | 0 1 background
1130 * which gives:
1131 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
1132 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
1134 XSetFunction( gdi_display, gc, GXcopy );
1135 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 0, 0 );
1136 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 0, 0 );
1137 XSetFunction( gdi_display, gc, GXandReverse );
1138 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, xor_y, width, height, 0, 0 );
1139 XSetFunction( gdi_display, gc, GXorReverse );
1140 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, xor_y, width, height, 0, 0 );
1141 /* additional white */
1142 XSetFunction( gdi_display, gc, GXand );
1143 XCopyArea( gdi_display, src_pixmap, src_pixmap, gc, 0, xor_y, width, height, 0, and_y );
1144 XSetFunction( gdi_display, gc, GXor );
1145 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 1, 1 );
1146 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 1, 1 );
1147 XFreeGC( gdi_display, gc );
1149 fg.red = fg.green = fg.blue = 0xffff;
1150 bg.red = bg.green = bg.blue = 0;
1151 cursor = XCreatePixmapCursor( gdi_display, bits_pixmap, mask_pixmap,
1152 &fg, &bg, icon->xHotspot, icon->yHotspot );
1153 XFreePixmap( gdi_display, src_pixmap );
1154 XFreePixmap( gdi_display, bits_pixmap );
1155 XFreePixmap( gdi_display, mask_pixmap );
1157 done:
1158 HeapFree( GetProcessHeap(), 0, mask_bits );
1159 return cursor;
1162 /***********************************************************************
1163 * create_xlib_color_cursor
1165 * Create a color X cursor from a Windows one.
1167 static Cursor create_xlib_color_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1169 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1170 BITMAPINFO *info = (BITMAPINFO *)buffer;
1171 XColor fg, bg;
1172 Cursor cursor = None;
1173 XVisualInfo vis = default_visual;
1174 Pixmap xor_pixmap, mask_pixmap;
1175 struct gdi_image_bits bits;
1176 unsigned int *color_bits = NULL, *ptr;
1177 unsigned char *mask_bits = NULL, *xor_bits = NULL;
1178 int i, x, y;
1179 BOOL has_alpha = FALSE;
1180 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
1181 unsigned int width_bytes = (width + 31) / 32 * 4;
1183 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1184 info->bmiHeader.biWidth = width;
1185 info->bmiHeader.biHeight = -height;
1186 info->bmiHeader.biPlanes = 1;
1187 info->bmiHeader.biBitCount = 1;
1188 info->bmiHeader.biCompression = BI_RGB;
1189 info->bmiHeader.biSizeImage = width_bytes * height;
1190 info->bmiHeader.biXPelsPerMeter = 0;
1191 info->bmiHeader.biYPelsPerMeter = 0;
1192 info->bmiHeader.biClrUsed = 0;
1193 info->bmiHeader.biClrImportant = 0;
1195 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1196 if (!GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS )) goto done;
1198 info->bmiHeader.biBitCount = 32;
1199 info->bmiHeader.biSizeImage = width * height * 4;
1200 if (!(color_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1201 if (!(xor_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, width_bytes * height ))) goto done;
1202 GetDIBits( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS );
1204 /* compute fg/bg color and xor bitmap based on average of the color values */
1206 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
1207 for (y = 0, ptr = color_bits; y < height; y++)
1209 for (x = 0; x < width; x++, ptr++)
1211 int red = (*ptr >> 16) & 0xff;
1212 int green = (*ptr >> 8) & 0xff;
1213 int blue = (*ptr >> 0) & 0xff;
1214 if (red + green + blue > 0x40)
1216 rfg += red;
1217 gfg += green;
1218 bfg += blue;
1219 fgBits++;
1220 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1222 else
1224 rbg += red;
1225 gbg += green;
1226 bbg += blue;
1230 if (fgBits)
1232 fg.red = rfg * 257 / fgBits;
1233 fg.green = gfg * 257 / fgBits;
1234 fg.blue = bfg * 257 / fgBits;
1236 else fg.red = fg.green = fg.blue = 0;
1237 bgBits = width * height - fgBits;
1238 if (bgBits)
1240 bg.red = rbg * 257 / bgBits;
1241 bg.green = gbg * 257 / bgBits;
1242 bg.blue = bbg * 257 / bgBits;
1244 else bg.red = bg.green = bg.blue = 0;
1246 info->bmiHeader.biBitCount = 1;
1247 info->bmiHeader.biClrUsed = 0;
1248 info->bmiHeader.biSizeImage = width_bytes * height;
1250 /* generate mask from the alpha channel if we have one */
1252 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
1253 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
1255 if (has_alpha)
1257 memset( mask_bits, 0, width_bytes * height );
1258 for (y = 0, ptr = color_bits; y < height; y++)
1259 for (x = 0; x < width; x++, ptr++)
1260 if ((*ptr >> 24) > 25) /* more than 10% alpha */
1261 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1263 else /* invert the mask */
1265 unsigned int j;
1267 ptr = (unsigned int *)mask_bits;
1268 for (j = 0; j < info->bmiHeader.biSizeImage / sizeof(*ptr); j++, ptr++) *ptr ^= ~0u;
1271 vis.depth = 1;
1272 bits.ptr = xor_bits;
1273 bits.free = NULL;
1274 bits.is_copy = TRUE;
1275 if (!(xor_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1277 bits.ptr = mask_bits;
1278 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
1280 if (mask_pixmap)
1282 cursor = XCreatePixmapCursor( gdi_display, xor_pixmap, mask_pixmap,
1283 &fg, &bg, icon->xHotspot, icon->yHotspot );
1284 XFreePixmap( gdi_display, mask_pixmap );
1286 XFreePixmap( gdi_display, xor_pixmap );
1288 done:
1289 HeapFree( GetProcessHeap(), 0, color_bits );
1290 HeapFree( GetProcessHeap(), 0, xor_bits );
1291 HeapFree( GetProcessHeap(), 0, mask_bits );
1292 return cursor;
1295 /***********************************************************************
1296 * create_cursor
1298 * Create an X cursor from a Windows one.
1300 static Cursor create_cursor( HANDLE handle )
1302 Cursor cursor = 0;
1303 ICONINFOEXW info;
1304 BITMAP bm;
1305 HDC hdc;
1307 if (!handle) return get_empty_cursor();
1309 info.cbSize = sizeof(info);
1310 if (!GetIconInfoExW( handle, &info )) return 0;
1312 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
1314 DeleteObject( info.hbmColor );
1315 DeleteObject( info.hbmMask );
1316 return cursor;
1319 GetObjectW( info.hbmMask, sizeof(bm), &bm );
1320 if (!info.hbmColor) bm.bmHeight = max( 1, bm.bmHeight / 2 );
1322 /* make sure hotspot is valid */
1323 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1325 info.xHotspot = bm.bmWidth / 2;
1326 info.yHotspot = bm.bmHeight / 2;
1329 hdc = CreateCompatibleDC( 0 );
1331 if (info.hbmColor)
1333 #ifdef SONAME_LIBXCURSOR
1334 if (pXcursorImagesLoadCursor)
1335 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
1336 #endif
1337 if (!cursor) cursor = create_xlib_color_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1338 DeleteObject( info.hbmColor );
1340 else
1342 cursor = create_xlib_monochrome_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1345 DeleteObject( info.hbmMask );
1346 DeleteDC( hdc );
1347 return cursor;
1350 /***********************************************************************
1351 * DestroyCursorIcon (X11DRV.@)
1353 void CDECL X11DRV_DestroyCursorIcon( HCURSOR handle )
1355 Cursor cursor;
1357 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1359 TRACE( "%p xid %lx\n", handle, cursor );
1360 XFreeCursor( gdi_display, cursor );
1361 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1365 /***********************************************************************
1366 * SetCursor (X11DRV.@)
1368 void CDECL X11DRV_SetCursor( HCURSOR handle )
1370 if (InterlockedExchangePointer( (void **)&last_cursor, handle ) != handle ||
1371 GetTickCount() - last_cursor_change > 100)
1373 last_cursor_change = GetTickCount();
1374 if (cursor_window) SendNotifyMessageW( cursor_window, WM_X11DRV_SET_CURSOR, 0, (LPARAM)handle );
1378 /***********************************************************************
1379 * SetCursorPos (X11DRV.@)
1381 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
1383 struct x11drv_thread_data *data = x11drv_init_thread_data();
1384 POINT pos = virtual_screen_to_root( x, y );
1386 XWarpPointer( data->display, root_window, root_window, 0, 0, 0, 0, pos.x, pos.y );
1387 data->warp_serial = NextRequest( data->display );
1388 XNoOp( data->display );
1389 XFlush( data->display ); /* avoids bad mouse lag in games that do their own mouse warping */
1390 TRACE( "warped to %d,%d serial %lu\n", x, y, data->warp_serial );
1391 return TRUE;
1394 /***********************************************************************
1395 * GetCursorPos (X11DRV.@)
1397 BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
1399 Display *display = thread_init_display();
1400 Window root, child;
1401 int rootX, rootY, winX, winY;
1402 unsigned int xstate;
1403 BOOL ret;
1405 ret = XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &winX, &winY, &xstate );
1406 if (ret)
1408 POINT old = *pos;
1409 *pos = root_to_virtual_screen( winX, winY );
1410 TRACE( "pointer at %s server pos %s\n", wine_dbgstr_point(pos), wine_dbgstr_point(&old) );
1412 return ret;
1415 /***********************************************************************
1416 * ClipCursor (X11DRV.@)
1418 BOOL CDECL X11DRV_ClipCursor( LPCRECT clip )
1420 RECT virtual_rect = get_virtual_screen_rect();
1422 if (!clip) clip = &virtual_rect;
1424 if (grab_pointer)
1426 HWND foreground = GetForegroundWindow();
1428 /* we are clipping if the clip rectangle is smaller than the screen */
1429 if (clip->left > virtual_rect.left || clip->right < virtual_rect.right ||
1430 clip->top > virtual_rect.top || clip->bottom < virtual_rect.bottom)
1432 DWORD tid, pid;
1434 /* forward request to the foreground window if it's in a different thread */
1435 tid = GetWindowThreadProcessId( foreground, &pid );
1436 if (tid && tid != GetCurrentThreadId() && pid == GetCurrentProcessId())
1438 TRACE( "forwarding clip request to %p\n", foreground );
1439 SendNotifyMessageW( foreground, WM_X11DRV_CLIP_CURSOR, 0, 0 );
1440 return TRUE;
1442 else if (grab_clipping_window( clip )) return TRUE;
1444 else /* if currently clipping, check if we should switch to fullscreen clipping */
1446 struct x11drv_thread_data *data = x11drv_thread_data();
1447 if (data && data->clip_hwnd)
1449 if (EqualRect( clip, &clip_rect ) || clip_fullscreen_window( foreground, TRUE ))
1450 return TRUE;
1454 ungrab_clipping_window();
1455 return TRUE;
1458 /***********************************************************************
1459 * move_resize_window
1461 void move_resize_window( HWND hwnd, int dir )
1463 Display *display = thread_display();
1464 DWORD pt;
1465 POINT pos;
1466 int button = 0;
1467 XEvent xev;
1468 Window win, root, child;
1469 unsigned int xstate;
1471 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1473 pt = GetMessagePos();
1474 pos = virtual_screen_to_root( (short)LOWORD( pt ), (short)HIWORD( pt ) );
1476 if (GetKeyState( VK_LBUTTON ) & 0x8000) button = 1;
1477 else if (GetKeyState( VK_MBUTTON ) & 0x8000) button = 2;
1478 else if (GetKeyState( VK_RBUTTON ) & 0x8000) button = 3;
1480 TRACE( "hwnd %p/%lx, pos %s, dir %d, button %d\n", hwnd, win, wine_dbgstr_point(&pos), dir, button );
1482 xev.xclient.type = ClientMessage;
1483 xev.xclient.window = win;
1484 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1485 xev.xclient.serial = 0;
1486 xev.xclient.display = display;
1487 xev.xclient.send_event = True;
1488 xev.xclient.format = 32;
1489 xev.xclient.data.l[0] = pos.x; /* x coord */
1490 xev.xclient.data.l[1] = pos.y; /* y coord */
1491 xev.xclient.data.l[2] = dir; /* direction */
1492 xev.xclient.data.l[3] = button; /* button */
1493 xev.xclient.data.l[4] = 0; /* unused */
1495 /* need to ungrab the pointer that may have been automatically grabbed
1496 * with a ButtonPress event */
1497 XUngrabPointer( display, CurrentTime );
1498 XSendEvent(display, root_window, False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
1500 /* try to detect the end of the size/move by polling for the mouse button to be released */
1501 /* (some apps don't like it if we return before the size/move is done) */
1503 if (!button) return;
1504 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1506 for (;;)
1508 MSG msg;
1509 INPUT input;
1510 int x, y, rootX, rootY;
1512 if (!XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &x, &y, &xstate )) break;
1514 if (!(xstate & (Button1Mask << (button - 1))))
1516 /* fake a button release event */
1517 pos = root_to_virtual_screen( x, y );
1518 input.type = INPUT_MOUSE;
1519 input.u.mi.dx = pos.x;
1520 input.u.mi.dy = pos.y;
1521 input.u.mi.mouseData = button_up_data[button - 1];
1522 input.u.mi.dwFlags = button_up_flags[button - 1] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1523 input.u.mi.time = GetTickCount();
1524 input.u.mi.dwExtraInfo = 0;
1525 __wine_send_input( hwnd, &input );
1528 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
1530 if (!CallMsgFilterW( &msg, MSGF_SIZE ))
1532 TranslateMessage( &msg );
1533 DispatchMessageW( &msg );
1537 if (!(xstate & (Button1Mask << (button - 1)))) break;
1538 MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT );
1541 TRACE( "hwnd %p/%lx done\n", hwnd, win );
1542 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1546 /***********************************************************************
1547 * X11DRV_ButtonPress
1549 BOOL X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1551 XButtonEvent *event = &xev->xbutton;
1552 int buttonNum = event->button - 1;
1553 INPUT input;
1555 if (buttonNum >= NB_BUTTONS) return FALSE;
1557 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1559 input.u.mi.dx = event->x;
1560 input.u.mi.dy = event->y;
1561 input.u.mi.mouseData = button_down_data[buttonNum];
1562 input.u.mi.dwFlags = button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1563 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1564 input.u.mi.dwExtraInfo = 0;
1566 update_user_time( event->time );
1567 send_mouse_input( hwnd, event->window, event->state, &input );
1568 return TRUE;
1572 /***********************************************************************
1573 * X11DRV_ButtonRelease
1575 BOOL X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1577 XButtonEvent *event = &xev->xbutton;
1578 int buttonNum = event->button - 1;
1579 INPUT input;
1581 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return FALSE;
1583 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1585 input.u.mi.dx = event->x;
1586 input.u.mi.dy = event->y;
1587 input.u.mi.mouseData = button_up_data[buttonNum];
1588 input.u.mi.dwFlags = button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1589 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1590 input.u.mi.dwExtraInfo = 0;
1592 send_mouse_input( hwnd, event->window, event->state, &input );
1593 return TRUE;
1597 /***********************************************************************
1598 * X11DRV_MotionNotify
1600 BOOL X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1602 XMotionEvent *event = &xev->xmotion;
1603 INPUT input;
1605 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d serial %lu\n",
1606 hwnd, event->window, event->x, event->y, event->is_hint, event->serial );
1608 input.u.mi.dx = event->x;
1609 input.u.mi.dy = event->y;
1610 input.u.mi.mouseData = 0;
1611 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1612 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1613 input.u.mi.dwExtraInfo = 0;
1615 if (!hwnd && is_old_motion_event( event->serial ))
1617 TRACE( "pos %d,%d old serial %lu, ignoring\n", input.u.mi.dx, input.u.mi.dy, event->serial );
1618 return FALSE;
1620 send_mouse_input( hwnd, event->window, event->state, &input );
1621 return TRUE;
1625 /***********************************************************************
1626 * X11DRV_EnterNotify
1628 BOOL X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1630 XCrossingEvent *event = &xev->xcrossing;
1631 INPUT input;
1633 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd, event->window, event->x, event->y, event->detail );
1635 if (event->detail == NotifyVirtual) return FALSE;
1636 if (hwnd == x11drv_thread_data()->grab_hwnd) return FALSE;
1638 /* simulate a mouse motion event */
1639 input.u.mi.dx = event->x;
1640 input.u.mi.dy = event->y;
1641 input.u.mi.mouseData = 0;
1642 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1643 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1644 input.u.mi.dwExtraInfo = 0;
1646 if (is_old_motion_event( event->serial ))
1648 TRACE( "pos %d,%d old serial %lu, ignoring\n", input.u.mi.dx, input.u.mi.dy, event->serial );
1649 return FALSE;
1651 send_mouse_input( hwnd, event->window, event->state, &input );
1652 return TRUE;
1655 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1657 /***********************************************************************
1658 * X11DRV_DeviceChanged
1660 static BOOL X11DRV_DeviceChanged( XGenericEventCookie *xev )
1662 XIDeviceChangedEvent *event = xev->data;
1663 struct x11drv_thread_data *data = x11drv_thread_data();
1665 if (event->deviceid != data->xi2_core_pointer) return FALSE;
1666 if (event->reason != XISlaveSwitch) return FALSE;
1668 update_relative_valuators( event->classes, event->num_classes );
1669 data->xi2_current_slave = event->sourceid;
1670 return TRUE;
1673 /***********************************************************************
1674 * X11DRV_RawMotion
1676 static BOOL X11DRV_RawMotion( XGenericEventCookie *xev )
1678 XIRawEvent *event = xev->data;
1679 const double *values = event->valuators.values;
1680 RECT virtual_rect;
1681 INPUT input;
1682 int i;
1683 double dx = 0, dy = 0, val;
1684 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1685 struct x11drv_valuator_data *x_rel, *y_rel;
1687 if (thread_data->x_rel_valuator.number < 0 || thread_data->y_rel_valuator.number < 0) return FALSE;
1688 if (!event->valuators.mask_len) return FALSE;
1689 if (thread_data->xi2_state != xi_enabled) return FALSE;
1691 /* If there is no slave currently detected, no previous motion nor device
1692 * change events were received. Look it up now on the device list in this
1693 * case.
1695 if (!thread_data->xi2_current_slave)
1697 XIDeviceInfo *devices = thread_data->xi2_devices;
1699 for (i = 0; i < thread_data->xi2_device_count; i++)
1701 if (devices[i].use != XISlavePointer) continue;
1702 if (devices[i].deviceid != event->deviceid) continue;
1703 if (devices[i].attachment != thread_data->xi2_core_pointer) continue;
1704 thread_data->xi2_current_slave = event->deviceid;
1705 break;
1709 if (event->deviceid != thread_data->xi2_current_slave) return FALSE;
1711 x_rel = &thread_data->x_rel_valuator;
1712 y_rel = &thread_data->y_rel_valuator;
1714 input.u.mi.mouseData = 0;
1715 input.u.mi.dwFlags = MOUSEEVENTF_MOVE;
1716 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1717 input.u.mi.dwExtraInfo = 0;
1718 input.u.mi.dx = 0;
1719 input.u.mi.dy = 0;
1721 virtual_rect = get_virtual_screen_rect();
1723 for (i = 0; i <= max ( x_rel->number, y_rel->number ); i++)
1725 if (!XIMaskIsSet( event->valuators.mask, i )) continue;
1726 val = *values++;
1727 if (i == x_rel->number)
1729 input.u.mi.dx = dx = val;
1730 if (x_rel->min < x_rel->max)
1731 input.u.mi.dx = val * (virtual_rect.right - virtual_rect.left)
1732 / (x_rel->max - x_rel->min);
1734 if (i == y_rel->number)
1736 input.u.mi.dy = dy = val;
1737 if (y_rel->min < y_rel->max)
1738 input.u.mi.dy = val * (virtual_rect.bottom - virtual_rect.top)
1739 / (y_rel->max - y_rel->min);
1743 if (broken_rawevents && is_old_motion_event( xev->serial ))
1745 TRACE( "pos %d,%d old serial %lu, ignoring\n", input.u.mi.dx, input.u.mi.dy, xev->serial );
1746 return FALSE;
1749 TRACE( "pos %d,%d (event %f,%f)\n", input.u.mi.dx, input.u.mi.dy, dx, dy );
1751 input.type = INPUT_MOUSE;
1752 __wine_send_input( 0, &input );
1753 return TRUE;
1756 #endif /* HAVE_X11_EXTENSIONS_XINPUT2_H */
1759 /***********************************************************************
1760 * X11DRV_XInput2_Init
1762 void X11DRV_XInput2_Init(void)
1764 #if defined(SONAME_LIBXI) && defined(HAVE_X11_EXTENSIONS_XINPUT2_H)
1765 int event, error;
1766 void *libxi_handle = wine_dlopen( SONAME_LIBXI, RTLD_NOW, NULL, 0 );
1768 if (!libxi_handle)
1770 WARN( "couldn't load %s\n", SONAME_LIBXI );
1771 return;
1773 #define LOAD_FUNCPTR(f) \
1774 if (!(p##f = wine_dlsym( libxi_handle, #f, NULL, 0))) \
1776 WARN("Failed to load %s.\n", #f); \
1777 return; \
1780 LOAD_FUNCPTR(XIGetClientPointer);
1781 LOAD_FUNCPTR(XIFreeDeviceInfo);
1782 LOAD_FUNCPTR(XIQueryDevice);
1783 LOAD_FUNCPTR(XIQueryVersion);
1784 LOAD_FUNCPTR(XISelectEvents);
1785 #undef LOAD_FUNCPTR
1787 xinput2_available = XQueryExtension( gdi_display, "XInputExtension", &xinput2_opcode, &event, &error );
1789 /* Until version 1.10.4 rawinput was broken in XOrg, see
1790 * https://bugs.freedesktop.org/show_bug.cgi?id=30068 */
1791 broken_rawevents = strstr(XServerVendor( gdi_display ), "X.Org") &&
1792 XVendorRelease( gdi_display ) < 11004000;
1794 #else
1795 TRACE( "X Input 2 support not compiled in.\n" );
1796 #endif
1800 /***********************************************************************
1801 * X11DRV_GenericEvent
1803 BOOL X11DRV_GenericEvent( HWND hwnd, XEvent *xev )
1805 BOOL ret = FALSE;
1806 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1807 XGenericEventCookie *event = &xev->xcookie;
1809 if (!event->data) return FALSE;
1810 if (event->extension != xinput2_opcode) return FALSE;
1812 switch (event->evtype)
1814 case XI_DeviceChanged:
1815 ret = X11DRV_DeviceChanged( event );
1816 break;
1817 case XI_RawMotion:
1818 ret = X11DRV_RawMotion( event );
1819 break;
1821 default:
1822 TRACE( "Unhandled event %#x\n", event->evtype );
1823 break;
1825 #endif
1826 return ret;