inetcomm: Implement MimeOleGetPropertySchema.
[wine.git] / dlls / winex11.drv / mouse.c
blob677acfa0b9bb0d5b44fbab92074ba602ad633969
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(XIFreeDeviceInfo);
136 MAKE_FUNCPTR(XIQueryDevice);
137 MAKE_FUNCPTR(XIQueryVersion);
138 MAKE_FUNCPTR(XISelectEvents);
139 #undef MAKE_FUNCPTR
140 #endif
142 /***********************************************************************
143 * X11DRV_Xcursor_Init
145 * Load the Xcursor library for use.
147 void X11DRV_Xcursor_Init(void)
149 #ifdef SONAME_LIBXCURSOR
150 xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
151 if (!xcursor_handle) /* wine_dlopen failed. */
153 WARN("Xcursor failed to load. Using fallback code.\n");
154 return;
156 #define LOAD_FUNCPTR(f) \
157 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
159 LOAD_FUNCPTR(XcursorImageCreate);
160 LOAD_FUNCPTR(XcursorImageDestroy);
161 LOAD_FUNCPTR(XcursorImageLoadCursor);
162 LOAD_FUNCPTR(XcursorImagesCreate);
163 LOAD_FUNCPTR(XcursorImagesDestroy);
164 LOAD_FUNCPTR(XcursorImagesLoadCursor);
165 LOAD_FUNCPTR(XcursorLibraryLoadCursor);
166 #undef LOAD_FUNCPTR
167 #endif /* SONAME_LIBXCURSOR */
171 /***********************************************************************
172 * get_empty_cursor
174 static Cursor get_empty_cursor(void)
176 static Cursor cursor;
177 static const char data[] = { 0 };
179 if (!cursor)
181 XColor bg;
182 Pixmap pixmap;
184 bg.red = bg.green = bg.blue = 0x0000;
185 pixmap = XCreateBitmapFromData( gdi_display, root_window, data, 1, 1 );
186 if (pixmap)
188 Cursor new = XCreatePixmapCursor( gdi_display, pixmap, pixmap, &bg, &bg, 0, 0 );
189 if (InterlockedCompareExchangePointer( (void **)&cursor, (void *)new, 0 ))
190 XFreeCursor( gdi_display, new );
191 XFreePixmap( gdi_display, pixmap );
194 return cursor;
197 /***********************************************************************
198 * set_window_cursor
200 void set_window_cursor( Window window, HCURSOR handle )
202 Cursor cursor, prev;
204 if (!handle) cursor = get_empty_cursor();
205 else if (XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
207 /* try to create it */
208 if (!(cursor = create_cursor( handle ))) return;
210 XLockDisplay( gdi_display );
211 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&prev ))
213 /* someone else was here first */
214 XFreeCursor( gdi_display, cursor );
215 cursor = prev;
217 else
219 XSaveContext( gdi_display, (XID)handle, cursor_context, (char *)cursor );
220 TRACE( "cursor %p created %lx\n", handle, cursor );
222 XUnlockDisplay( gdi_display );
225 XDefineCursor( gdi_display, window, cursor );
226 /* make the change take effect immediately */
227 XFlush( gdi_display );
230 /***********************************************************************
231 * sync_window_cursor
233 void sync_window_cursor( Window window )
235 HCURSOR cursor;
237 SERVER_START_REQ( set_cursor )
239 req->flags = 0;
240 wine_server_call( req );
241 cursor = reply->prev_count >= 0 ? wine_server_ptr_handle( reply->prev_handle ) : 0;
243 SERVER_END_REQ;
245 set_window_cursor( window, cursor );
248 /***********************************************************************
249 * enable_xinput2
251 static void enable_xinput2(void)
253 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
254 struct x11drv_thread_data *data = x11drv_thread_data();
255 XIEventMask mask;
256 XIDeviceInfo *devices;
257 unsigned char mask_bits[XIMaskLen(XI_LASTEVENT)];
258 int i, j, count;
260 if (!xinput2_available) return;
262 if (data->xi2_state == xi_unknown)
264 int major = 2, minor = 0;
265 if (!pXIQueryVersion( data->display, &major, &minor )) data->xi2_state = xi_disabled;
266 else
268 data->xi2_state = xi_unavailable;
269 WARN( "X Input 2 not available\n" );
272 if (data->xi2_state == xi_unavailable) return;
274 if (data->xi2_devices) pXIFreeDeviceInfo( data->xi2_devices );
275 data->xi2_devices = devices = pXIQueryDevice( data->display, XIAllDevices, &data->xi2_device_count );
276 for (i = 0; i < data->xi2_device_count; ++i)
278 if (devices[i].use != XIMasterPointer) continue;
279 for (j = count = 0; j < devices[i].num_classes; j++)
281 XIValuatorClassInfo *class = (XIValuatorClassInfo *)devices[i].classes[j];
283 if (devices[i].classes[j]->type != XIValuatorClass) continue;
284 TRACE( "Device %u (%s) num %u %f,%f res %u mode %u label %s\n",
285 devices[i].deviceid, debugstr_a(devices[i].name),
286 class->number, class->min, class->max, class->resolution, class->mode,
287 XGetAtomName( data->display, class->label ));
288 if (class->label == x11drv_atom( Rel_X ) || class->label == x11drv_atom( Rel_Y )) count++;
289 /* workaround for drivers that don't provide labels */
290 if (!class->label && class->number <= 1 && class->mode == XIModeRelative) count++;
292 if (count < 2) continue;
293 TRACE( "Using %u (%s) as core pointer\n",
294 devices[i].deviceid, debugstr_a(devices[i].name) );
295 data->xi2_core_pointer = devices[i].deviceid;
296 break;
299 mask.mask = mask_bits;
300 mask.mask_len = sizeof(mask_bits);
301 memset( mask_bits, 0, sizeof(mask_bits) );
302 XISetMask( mask_bits, XI_RawMotion );
303 XISetMask( mask_bits, XI_ButtonPress );
305 for (i = 0; i < data->xi2_device_count; ++i)
307 if (devices[i].use == XISlavePointer && devices[i].attachment == data->xi2_core_pointer)
309 TRACE( "Device %u (%s) is attached to the core pointer\n",
310 devices[i].deviceid, debugstr_a(devices[i].name) );
311 mask.deviceid = devices[i].deviceid;
312 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
313 data->xi2_state = xi_enabled;
316 #endif
319 /***********************************************************************
320 * disable_xinput2
322 static void disable_xinput2(void)
324 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
325 struct x11drv_thread_data *data = x11drv_thread_data();
326 XIDeviceInfo *devices = data->xi2_devices;
327 XIEventMask mask;
328 int i;
330 if (data->xi2_state != xi_enabled) return;
332 TRACE( "disabling\n" );
333 data->xi2_state = xi_disabled;
335 mask.mask = NULL;
336 mask.mask_len = 0;
338 for (i = 0; i < data->xi2_device_count; ++i)
340 if (devices[i].use == XISlavePointer && devices[i].attachment == data->xi2_core_pointer)
342 mask.deviceid = devices[i].deviceid;
343 pXISelectEvents( data->display, DefaultRootWindow( data->display ), &mask, 1 );
346 pXIFreeDeviceInfo( devices );
347 data->xi2_devices = NULL;
348 data->xi2_device_count = 0;
349 #endif
353 /***********************************************************************
354 * grab_clipping_window
356 * Start a pointer grab on the clip window.
358 static BOOL grab_clipping_window( const RECT *clip )
360 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
361 struct x11drv_thread_data *data = x11drv_thread_data();
362 Window clip_window;
363 HWND msg_hwnd = 0;
364 POINT pos;
366 if (GetWindowThreadProcessId( GetDesktopWindow(), NULL ) == GetCurrentThreadId())
367 return TRUE; /* don't clip in the desktop process */
369 if (!data) return FALSE;
370 if (!(clip_window = init_clip_window())) return TRUE;
372 if (!(msg_hwnd = CreateWindowW( messageW, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0,
373 GetModuleHandleW(0), NULL )))
374 return TRUE;
376 /* enable XInput2 unless we are already clipping */
377 if (!data->clip_hwnd) enable_xinput2();
379 if (data->xi2_state != xi_enabled)
381 WARN( "XInput2 not supported, refusing to clip to %s\n", wine_dbgstr_rect(clip) );
382 DestroyWindow( msg_hwnd );
383 ClipCursor( NULL );
384 return TRUE;
387 TRACE( "clipping to %s win %lx\n", wine_dbgstr_rect(clip), clip_window );
389 if (!data->clip_hwnd) XUnmapWindow( data->display, clip_window );
390 pos = virtual_screen_to_root( clip->left, clip->top );
391 XMoveResizeWindow( data->display, clip_window, pos.x, pos.y,
392 max( 1, clip->right - clip->left ), max( 1, clip->bottom - clip->top ) );
393 XMapWindow( data->display, clip_window );
395 /* if the rectangle is shrinking we may get a pointer warp */
396 if (!data->clip_hwnd || clip->left > clip_rect.left || clip->top > clip_rect.top ||
397 clip->right < clip_rect.right || clip->bottom < clip_rect.bottom)
398 data->warp_serial = NextRequest( data->display );
400 if (!XGrabPointer( data->display, clip_window, False,
401 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
402 GrabModeAsync, GrabModeAsync, clip_window, None, CurrentTime ))
403 clipping_cursor = TRUE;
405 if (!clipping_cursor)
407 disable_xinput2();
408 DestroyWindow( msg_hwnd );
409 return FALSE;
411 clip_rect = *clip;
412 if (!data->clip_hwnd) sync_window_cursor( clip_window );
413 InterlockedExchangePointer( (void **)&cursor_window, msg_hwnd );
414 data->clip_hwnd = msg_hwnd;
415 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, (LPARAM)msg_hwnd );
416 return TRUE;
419 /***********************************************************************
420 * ungrab_clipping_window
422 * Release the pointer grab on the clip window.
424 void ungrab_clipping_window(void)
426 Display *display = thread_init_display();
427 Window clip_window = init_clip_window();
429 if (!clip_window) return;
431 TRACE( "no longer clipping\n" );
432 XUnmapWindow( display, clip_window );
433 clipping_cursor = FALSE;
434 SendMessageW( GetDesktopWindow(), WM_X11DRV_CLIP_CURSOR, 0, 0 );
437 /***********************************************************************
438 * reset_clipping_window
440 * Forcibly reset the window clipping on external events.
442 void reset_clipping_window(void)
444 ungrab_clipping_window();
445 ClipCursor( NULL ); /* make sure the clip rectangle is reset too */
448 /***********************************************************************
449 * clip_cursor_notify
451 * Notification function called upon receiving a WM_X11DRV_CLIP_CURSOR.
453 LRESULT clip_cursor_notify( HWND hwnd, HWND new_clip_hwnd )
455 struct x11drv_thread_data *data = x11drv_init_thread_data();
457 if (hwnd == GetDesktopWindow()) /* change the clip window stored in the desktop process */
459 static HWND clip_hwnd;
461 HWND prev = clip_hwnd;
462 clip_hwnd = new_clip_hwnd;
463 if (prev || new_clip_hwnd) TRACE( "clip hwnd changed from %p to %p\n", prev, new_clip_hwnd );
464 if (prev) SendNotifyMessageW( prev, WM_X11DRV_CLIP_CURSOR, 0, 0 );
466 else if (hwnd == data->clip_hwnd) /* this is a notification that clipping has been reset */
468 TRACE( "clip hwnd reset from %p\n", hwnd );
469 data->clip_hwnd = 0;
470 data->clip_reset = GetTickCount();
471 disable_xinput2();
472 DestroyWindow( hwnd );
474 else if (hwnd == GetForegroundWindow()) /* request to clip */
476 RECT clip, virtual_rect = get_virtual_screen_rect();
478 GetClipCursor( &clip );
479 if (clip.left > virtual_rect.left || clip.right < virtual_rect.right ||
480 clip.top > virtual_rect.top || clip.bottom < virtual_rect.bottom)
481 return grab_clipping_window( &clip );
483 return 0;
486 /***********************************************************************
487 * clip_fullscreen_window
489 * Turn on clipping if the active window is fullscreen.
491 BOOL clip_fullscreen_window( HWND hwnd, BOOL reset )
493 struct x11drv_win_data *data;
494 struct x11drv_thread_data *thread_data;
495 RECT rect;
496 DWORD style;
497 BOOL fullscreen;
499 if (hwnd == GetDesktopWindow()) return FALSE;
500 style = GetWindowLongW( hwnd, GWL_STYLE );
501 if (!(style & WS_VISIBLE)) return FALSE;
502 if ((style & (WS_POPUP | WS_CHILD)) == WS_CHILD) return FALSE;
503 /* maximized windows don't count as full screen */
504 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION) return FALSE;
505 if (!(data = get_win_data( hwnd ))) return FALSE;
506 fullscreen = is_window_rect_fullscreen( &data->whole_rect );
507 release_win_data( data );
508 if (!fullscreen) return FALSE;
509 if (!(thread_data = x11drv_thread_data())) return FALSE;
510 if (GetTickCount() - thread_data->clip_reset < 1000) return FALSE;
511 if (!reset && clipping_cursor && thread_data->clip_hwnd) return FALSE; /* already clipping */
512 rect = get_primary_monitor_rect();
513 if (!grab_fullscreen)
515 RECT virtual_rect = get_virtual_screen_rect();
516 if (!EqualRect( &rect, &virtual_rect )) return FALSE;
517 if (root_window != DefaultRootWindow( gdi_display )) return FALSE;
519 TRACE( "win %p clipping fullscreen\n", hwnd );
520 return grab_clipping_window( &rect );
524 /***********************************************************************
525 * is_old_motion_event
527 static BOOL is_old_motion_event( unsigned long serial )
529 struct x11drv_thread_data *thread_data = x11drv_thread_data();
531 if (!thread_data->warp_serial) return FALSE;
532 if ((long)(serial - thread_data->warp_serial) < 0) return TRUE;
533 thread_data->warp_serial = 0; /* we caught up now */
534 return FALSE;
538 /***********************************************************************
539 * send_mouse_input
541 * Update the various window states on a mouse event.
543 static void send_mouse_input( HWND hwnd, Window window, unsigned int state, INPUT *input )
545 struct x11drv_win_data *data;
546 POINT pt;
548 input->type = INPUT_MOUSE;
550 if (!hwnd)
552 struct x11drv_thread_data *thread_data = x11drv_thread_data();
553 HWND clip_hwnd = thread_data->clip_hwnd;
555 if (!clip_hwnd) return;
556 if (thread_data->clip_window != window) return;
557 if (InterlockedExchangePointer( (void **)&cursor_window, clip_hwnd ) != clip_hwnd ||
558 input->u.mi.time - last_cursor_change > 100)
560 sync_window_cursor( window );
561 last_cursor_change = input->u.mi.time;
563 input->u.mi.dx += clip_rect.left;
564 input->u.mi.dy += clip_rect.top;
565 __wine_send_input( hwnd, input );
566 return;
569 if (window != root_window)
571 pt.x = input->u.mi.dx;
572 pt.y = input->u.mi.dy;
574 else pt = root_to_virtual_screen( input->u.mi.dx, input->u.mi.dy );
576 if (!(data = get_win_data( hwnd ))) return;
578 if (window == data->whole_window)
580 pt.x += data->whole_rect.left - data->client_rect.left;
581 pt.y += data->whole_rect.top - data->client_rect.top;
584 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
585 pt.x = data->client_rect.right - data->client_rect.left - 1 - pt.x;
586 MapWindowPoints( hwnd, 0, &pt, 1 );
588 if (InterlockedExchangePointer( (void **)&cursor_window, hwnd ) != hwnd ||
589 input->u.mi.time - last_cursor_change > 100)
591 sync_window_cursor( data->whole_window );
592 last_cursor_change = input->u.mi.time;
594 release_win_data( data );
596 if (hwnd != GetDesktopWindow())
598 hwnd = GetAncestor( hwnd, GA_ROOT );
599 if ((input->u.mi.dwFlags & (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_RIGHTDOWN)) && hwnd == GetForegroundWindow())
600 clip_fullscreen_window( hwnd, FALSE );
603 /* update the wine server Z-order */
605 if (hwnd != x11drv_thread_data()->grab_hwnd &&
606 /* ignore event if a button is pressed, since the mouse is then grabbed too */
607 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
609 RECT rect;
610 SetRect( &rect, pt.x, pt.y, pt.x + 1, pt.y + 1 );
611 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
613 SERVER_START_REQ( update_window_zorder )
615 req->window = wine_server_user_handle( hwnd );
616 req->rect.left = rect.left;
617 req->rect.top = rect.top;
618 req->rect.right = rect.right;
619 req->rect.bottom = rect.bottom;
620 wine_server_call( req );
622 SERVER_END_REQ;
625 input->u.mi.dx = pt.x;
626 input->u.mi.dy = pt.y;
627 __wine_send_input( hwnd, input );
630 #ifdef SONAME_LIBXCURSOR
632 /***********************************************************************
633 * create_xcursor_frame
635 * Use Xcursor to create a frame of an X cursor from a Windows one.
637 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
638 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
639 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
640 int width, int height, int istep )
642 XcursorImage *image, *ret = NULL;
643 DWORD delay_jiffies, num_steps;
644 int x, y, i;
645 BOOL has_alpha = FALSE;
646 XcursorPixel *ptr;
648 image = pXcursorImageCreate( width, height );
649 if (!image)
651 ERR("X11 failed to produce a cursor frame!\n");
652 return NULL;
655 image->xhot = iinfo->xHotspot;
656 image->yhot = iinfo->yHotspot;
658 image->delay = 100; /* fallback delay, 100 ms */
659 if (GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, istep, &delay_jiffies, &num_steps) != 0)
660 image->delay = (100 * delay_jiffies) / 6; /* convert jiffies (1/60s) to milliseconds */
661 else
662 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep);
664 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
665 memset( color_bits, 0x00, color_size );
666 SelectObject( hdc, hbmColor );
667 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
669 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
670 goto cleanup;
672 memcpy( image->pixels, color_bits, color_size );
674 /* check if the cursor frame was drawn with an alpha channel */
675 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
676 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
678 /* if no alpha channel was drawn then generate it from the mask */
679 if (!has_alpha)
681 unsigned int width_bytes = (width + 31) / 32 * 4;
683 /* draw the cursor mask to a temporary buffer */
684 memset( mask_bits, 0xFF, mask_size );
685 SelectObject( hdc, hbmMask );
686 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
688 ERR("Failed to draw frame mask %d.\n", istep);
689 goto cleanup;
691 /* use the buffer to directly modify the XcursorImage alpha channel */
692 for (y = 0, ptr = image->pixels; y < height; y++)
693 for (x = 0; x < width; x++, ptr++)
694 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
695 *ptr |= 0xff000000;
697 ret = image;
699 cleanup:
700 if (ret == NULL) pXcursorImageDestroy( image );
701 return ret;
704 /***********************************************************************
705 * create_xcursor_cursor
707 * Use Xcursor to create an X cursor from a Windows one.
709 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
711 unsigned char *color_bits, *mask_bits;
712 HBITMAP hbmColor = 0, hbmMask = 0;
713 DWORD nFrames, delay_jiffies, i;
714 int color_size, mask_size;
715 BITMAPINFO *info = NULL;
716 XcursorImages *images;
717 XcursorImage **imgs;
718 Cursor cursor = 0;
720 /* Retrieve the number of frames to render */
721 if (!GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, 0, &delay_jiffies, &nFrames)) return 0;
722 if (!(imgs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XcursorImage*)*nFrames ))) return 0;
724 /* Allocate all of the resources necessary to obtain a cursor frame */
725 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
726 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
727 info->bmiHeader.biWidth = width;
728 info->bmiHeader.biHeight = -height;
729 info->bmiHeader.biPlanes = 1;
730 info->bmiHeader.biCompression = BI_RGB;
731 info->bmiHeader.biXPelsPerMeter = 0;
732 info->bmiHeader.biYPelsPerMeter = 0;
733 info->bmiHeader.biClrUsed = 0;
734 info->bmiHeader.biClrImportant = 0;
735 info->bmiHeader.biBitCount = 32;
736 color_size = width * height * 4;
737 info->bmiHeader.biSizeImage = color_size;
738 hbmColor = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &color_bits, NULL, 0);
739 if (!hbmColor)
741 ERR("Failed to create DIB section for cursor color data!\n");
742 goto cleanup;
744 info->bmiHeader.biBitCount = 1;
745 info->bmiColors[0].rgbRed = 0;
746 info->bmiColors[0].rgbGreen = 0;
747 info->bmiColors[0].rgbBlue = 0;
748 info->bmiColors[0].rgbReserved = 0;
749 info->bmiColors[1].rgbRed = 0xff;
750 info->bmiColors[1].rgbGreen = 0xff;
751 info->bmiColors[1].rgbBlue = 0xff;
752 info->bmiColors[1].rgbReserved = 0;
754 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
755 info->bmiHeader.biSizeImage = mask_size;
756 hbmMask = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &mask_bits, NULL, 0);
757 if (!hbmMask)
759 ERR("Failed to create DIB section for cursor mask data!\n");
760 goto cleanup;
763 /* Create an XcursorImage for each frame of the cursor */
764 for (i=0; i<nFrames; i++)
766 imgs[i] = create_xcursor_frame( hdc, iinfo, icon,
767 hbmColor, color_bits, color_size,
768 hbmMask, mask_bits, mask_size,
769 width, height, i );
770 if (!imgs[i]) goto cleanup;
773 /* Build an X cursor out of all of the frames */
774 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
775 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
776 images->images[images->nimage] = imgs[images->nimage];
777 cursor = pXcursorImagesLoadCursor( gdi_display, images );
778 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
779 HeapFree( GetProcessHeap(), 0, imgs );
780 imgs = NULL;
782 cleanup:
783 if (imgs)
785 /* Failed to produce a cursor, free previously allocated frames */
786 for (i=0; i<nFrames && imgs[i]; i++)
787 pXcursorImageDestroy( imgs[i] );
788 HeapFree( GetProcessHeap(), 0, imgs );
790 /* Cleanup all of the resources used to obtain the frame data */
791 if (hbmColor) DeleteObject( hbmColor );
792 if (hbmMask) DeleteObject( hbmMask );
793 HeapFree( GetProcessHeap(), 0, info );
794 return cursor;
797 #endif /* SONAME_LIBXCURSOR */
800 struct system_cursors
802 WORD id;
803 const char *name;
806 static const struct system_cursors user32_cursors[] =
808 { OCR_NORMAL, "left_ptr" },
809 { OCR_IBEAM, "xterm" },
810 { OCR_WAIT, "watch" },
811 { OCR_CROSS, "cross" },
812 { OCR_UP, "center_ptr" },
813 { OCR_SIZE, "fleur" },
814 { OCR_SIZEALL, "fleur" },
815 { OCR_ICON, "icon" },
816 { OCR_SIZENWSE, "nwse-resize" },
817 { OCR_SIZENESW, "nesw-resize" },
818 { OCR_SIZEWE, "ew-resize" },
819 { OCR_SIZENS, "ns-resize" },
820 { OCR_NO, "not-allowed" },
821 { OCR_HAND, "hand2" },
822 { OCR_APPSTARTING, "left_ptr_watch" },
823 { OCR_HELP, "question_arrow" },
824 { 0 }
827 static const struct system_cursors comctl32_cursors[] =
829 { 102, "move" },
830 { 104, "copy" },
831 { 105, "left_ptr" },
832 { 106, "col-resize" },
833 { 107, "col-resize" },
834 { 108, "hand2" },
835 { 135, "row-resize" },
836 { 0 }
839 static const struct system_cursors ole32_cursors[] =
841 { 1, "no-drop" },
842 { 2, "move" },
843 { 3, "copy" },
844 { 4, "alias" },
845 { 0 }
848 static const struct system_cursors riched20_cursors[] =
850 { 105, "hand2" },
851 { 107, "right_ptr" },
852 { 109, "copy" },
853 { 110, "move" },
854 { 111, "no-drop" },
855 { 0 }
858 static const struct
860 const struct system_cursors *cursors;
861 WCHAR name[16];
862 } module_cursors[] =
864 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
865 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
866 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
867 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
870 struct cursor_font_fallback
872 const char *name;
873 unsigned int shape;
876 static const struct cursor_font_fallback fallbacks[] =
878 { "X_cursor", XC_X_cursor },
879 { "arrow", XC_arrow },
880 { "based_arrow_down", XC_based_arrow_down },
881 { "based_arrow_up", XC_based_arrow_up },
882 { "boat", XC_boat },
883 { "bogosity", XC_bogosity },
884 { "bottom_left_corner", XC_bottom_left_corner },
885 { "bottom_right_corner", XC_bottom_right_corner },
886 { "bottom_side", XC_bottom_side },
887 { "bottom_tee", XC_bottom_tee },
888 { "box_spiral", XC_box_spiral },
889 { "center_ptr", XC_center_ptr },
890 { "circle", XC_circle },
891 { "clock", XC_clock },
892 { "coffee_mug", XC_coffee_mug },
893 { "col-resize", XC_sb_h_double_arrow },
894 { "cross", XC_cross },
895 { "cross_reverse", XC_cross_reverse },
896 { "crosshair", XC_crosshair },
897 { "diamond_cross", XC_diamond_cross },
898 { "dot", XC_dot },
899 { "dotbox", XC_dotbox },
900 { "double_arrow", XC_double_arrow },
901 { "draft_large", XC_draft_large },
902 { "draft_small", XC_draft_small },
903 { "draped_box", XC_draped_box },
904 { "exchange", XC_exchange },
905 { "fleur", XC_fleur },
906 { "gobbler", XC_gobbler },
907 { "gumby", XC_gumby },
908 { "hand1", XC_hand1 },
909 { "hand2", XC_hand2 },
910 { "heart", XC_heart },
911 { "icon", XC_icon },
912 { "iron_cross", XC_iron_cross },
913 { "left_ptr", XC_left_ptr },
914 { "left_side", XC_left_side },
915 { "left_tee", XC_left_tee },
916 { "leftbutton", XC_leftbutton },
917 { "ll_angle", XC_ll_angle },
918 { "lr_angle", XC_lr_angle },
919 { "man", XC_man },
920 { "middlebutton", XC_middlebutton },
921 { "mouse", XC_mouse },
922 { "pencil", XC_pencil },
923 { "pirate", XC_pirate },
924 { "plus", XC_plus },
925 { "question_arrow", XC_question_arrow },
926 { "right_ptr", XC_right_ptr },
927 { "right_side", XC_right_side },
928 { "right_tee", XC_right_tee },
929 { "rightbutton", XC_rightbutton },
930 { "row-resize", XC_sb_v_double_arrow },
931 { "rtl_logo", XC_rtl_logo },
932 { "sailboat", XC_sailboat },
933 { "sb_down_arrow", XC_sb_down_arrow },
934 { "sb_h_double_arrow", XC_sb_h_double_arrow },
935 { "sb_left_arrow", XC_sb_left_arrow },
936 { "sb_right_arrow", XC_sb_right_arrow },
937 { "sb_up_arrow", XC_sb_up_arrow },
938 { "sb_v_double_arrow", XC_sb_v_double_arrow },
939 { "shuttle", XC_shuttle },
940 { "sizing", XC_sizing },
941 { "spider", XC_spider },
942 { "spraycan", XC_spraycan },
943 { "star", XC_star },
944 { "target", XC_target },
945 { "tcross", XC_tcross },
946 { "top_left_arrow", XC_top_left_arrow },
947 { "top_left_corner", XC_top_left_corner },
948 { "top_right_corner", XC_top_right_corner },
949 { "top_side", XC_top_side },
950 { "top_tee", XC_top_tee },
951 { "trek", XC_trek },
952 { "ul_angle", XC_ul_angle },
953 { "umbrella", XC_umbrella },
954 { "ur_angle", XC_ur_angle },
955 { "watch", XC_watch },
956 { "xterm", XC_xterm }
959 static int fallback_cmp( const void *key, const void *member )
961 const struct cursor_font_fallback *fallback = member;
962 return strcmp( key, fallback->name );
965 static int find_fallback_shape( const char *name )
967 struct cursor_font_fallback *fallback;
969 if ((fallback = bsearch( name, fallbacks, sizeof(fallbacks) / sizeof(fallbacks[0]),
970 sizeof(*fallback), fallback_cmp )))
971 return fallback->shape;
972 return -1;
975 /***********************************************************************
976 * create_xcursor_system_cursor
978 * Create an X cursor for a system cursor.
980 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
982 static const WCHAR idW[] = {'%','h','u',0};
983 const struct system_cursors *cursors;
984 unsigned int i;
985 Cursor cursor = 0;
986 HMODULE module;
987 HKEY key;
988 WCHAR *p, name[MAX_PATH * 2], valueW[64];
989 char valueA[64];
990 DWORD size, ret;
992 if (!info->szModName[0]) return 0;
994 p = strrchrW( info->szModName, '\\' );
995 strcpyW( name, p ? p + 1 : info->szModName );
996 p = name + strlenW( name );
997 *p++ = ',';
998 if (info->szResName[0]) strcpyW( p, info->szResName );
999 else sprintfW( p, idW, info->wResID );
1000 valueA[0] = 0;
1002 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
1003 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver\\Cursors", &key ))
1005 size = sizeof(valueW) / sizeof(WCHAR);
1006 ret = RegQueryValueExW( key, name, NULL, NULL, (BYTE *)valueW, &size );
1007 RegCloseKey( key );
1008 if (!ret)
1010 if (!valueW[0]) return 0; /* force standard cursor */
1011 if (!WideCharToMultiByte( CP_UNIXCP, 0, valueW, -1, valueA, sizeof(valueA), NULL, NULL ))
1012 valueA[0] = 0;
1013 goto done;
1017 if (info->szResName[0]) goto done; /* only integer resources are supported here */
1018 if (!(module = GetModuleHandleW( info->szModName ))) goto done;
1020 for (i = 0; i < sizeof(module_cursors)/sizeof(module_cursors[0]); i++)
1021 if (GetModuleHandleW( module_cursors[i].name ) == module) break;
1022 if (i == sizeof(module_cursors)/sizeof(module_cursors[0])) goto done;
1024 cursors = module_cursors[i].cursors;
1025 for (i = 0; cursors[i].id; i++)
1026 if (cursors[i].id == info->wResID)
1028 strcpy( valueA, cursors[i].name );
1029 break;
1032 done:
1033 if (valueA[0])
1035 #ifdef SONAME_LIBXCURSOR
1036 if (pXcursorLibraryLoadCursor) cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
1037 #endif
1038 if (!cursor)
1040 int shape = find_fallback_shape( valueA );
1041 if (shape != -1) cursor = XCreateFontCursor( gdi_display, shape );
1043 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
1044 debugstr_w(name), debugstr_a(valueA) );
1046 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
1047 return cursor;
1051 /***********************************************************************
1052 * create_xlib_monochrome_cursor
1054 * Create a monochrome X cursor from a Windows one.
1056 static Cursor create_xlib_monochrome_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1058 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1059 BITMAPINFO *info = (BITMAPINFO *)buffer;
1060 const int and_y = 0;
1061 const int xor_y = height;
1062 unsigned int width_bytes = (width + 31) / 32 * 4;
1063 unsigned char *mask_bits = NULL;
1064 GC gc;
1065 XColor fg, bg;
1066 XVisualInfo vis = default_visual;
1067 Pixmap src_pixmap, bits_pixmap, mask_pixmap;
1068 struct gdi_image_bits bits;
1069 Cursor cursor = 0;
1071 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1072 info->bmiHeader.biWidth = width;
1073 info->bmiHeader.biHeight = -height * 2;
1074 info->bmiHeader.biPlanes = 1;
1075 info->bmiHeader.biBitCount = 1;
1076 info->bmiHeader.biCompression = BI_RGB;
1077 info->bmiHeader.biSizeImage = width_bytes * height * 2;
1078 info->bmiHeader.biXPelsPerMeter = 0;
1079 info->bmiHeader.biYPelsPerMeter = 0;
1080 info->bmiHeader.biClrUsed = 0;
1081 info->bmiHeader.biClrImportant = 0;
1083 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1084 if (!GetDIBits( hdc, icon->hbmMask, 0, height * 2, mask_bits, info, DIB_RGB_COLORS )) goto done;
1086 vis.depth = 1;
1087 bits.ptr = mask_bits;
1088 bits.free = NULL;
1089 bits.is_copy = TRUE;
1090 if (!(src_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1092 bits_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
1093 mask_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
1094 gc = XCreateGC( gdi_display, src_pixmap, 0, NULL );
1095 XSetGraphicsExposures( gdi_display, gc, False );
1097 /* We have to do some magic here, as cursors are not fully
1098 * compatible between Windows and X11. Under X11, there are
1099 * only 3 possible color cursor: black, white and masked. So
1100 * we map the 4th Windows color (invert the bits on the screen)
1101 * to black and an additional white bit on another place
1102 * (+1,+1). This require some boolean arithmetic:
1104 * Windows | X11
1105 * And Xor Result | Bits Mask Result
1106 * 0 0 black | 0 1 background
1107 * 0 1 white | 1 1 foreground
1108 * 1 0 no change | X 0 no change
1109 * 1 1 inverted | 0 1 background
1111 * which gives:
1112 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
1113 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
1115 XSetFunction( gdi_display, gc, GXcopy );
1116 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 0, 0 );
1117 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 0, 0 );
1118 XSetFunction( gdi_display, gc, GXandReverse );
1119 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, xor_y, width, height, 0, 0 );
1120 XSetFunction( gdi_display, gc, GXorReverse );
1121 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, xor_y, width, height, 0, 0 );
1122 /* additional white */
1123 XSetFunction( gdi_display, gc, GXand );
1124 XCopyArea( gdi_display, src_pixmap, src_pixmap, gc, 0, xor_y, width, height, 0, and_y );
1125 XSetFunction( gdi_display, gc, GXor );
1126 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 1, 1 );
1127 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 1, 1 );
1128 XFreeGC( gdi_display, gc );
1130 fg.red = fg.green = fg.blue = 0xffff;
1131 bg.red = bg.green = bg.blue = 0;
1132 cursor = XCreatePixmapCursor( gdi_display, bits_pixmap, mask_pixmap,
1133 &fg, &bg, icon->xHotspot, icon->yHotspot );
1134 XFreePixmap( gdi_display, src_pixmap );
1135 XFreePixmap( gdi_display, bits_pixmap );
1136 XFreePixmap( gdi_display, mask_pixmap );
1138 done:
1139 HeapFree( GetProcessHeap(), 0, mask_bits );
1140 return cursor;
1143 /***********************************************************************
1144 * create_xlib_color_cursor
1146 * Create a color X cursor from a Windows one.
1148 static Cursor create_xlib_color_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1150 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1151 BITMAPINFO *info = (BITMAPINFO *)buffer;
1152 XColor fg, bg;
1153 Cursor cursor = None;
1154 XVisualInfo vis = default_visual;
1155 Pixmap xor_pixmap, mask_pixmap;
1156 struct gdi_image_bits bits;
1157 unsigned int *color_bits = NULL, *ptr;
1158 unsigned char *mask_bits = NULL, *xor_bits = NULL;
1159 int i, x, y;
1160 BOOL has_alpha = FALSE;
1161 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
1162 unsigned int width_bytes = (width + 31) / 32 * 4;
1164 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1165 info->bmiHeader.biWidth = width;
1166 info->bmiHeader.biHeight = -height;
1167 info->bmiHeader.biPlanes = 1;
1168 info->bmiHeader.biBitCount = 1;
1169 info->bmiHeader.biCompression = BI_RGB;
1170 info->bmiHeader.biSizeImage = width_bytes * height;
1171 info->bmiHeader.biXPelsPerMeter = 0;
1172 info->bmiHeader.biYPelsPerMeter = 0;
1173 info->bmiHeader.biClrUsed = 0;
1174 info->bmiHeader.biClrImportant = 0;
1176 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1177 if (!GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS )) goto done;
1179 info->bmiHeader.biBitCount = 32;
1180 info->bmiHeader.biSizeImage = width * height * 4;
1181 if (!(color_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1182 if (!(xor_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, width_bytes * height ))) goto done;
1183 GetDIBits( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS );
1185 /* compute fg/bg color and xor bitmap based on average of the color values */
1187 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
1188 for (y = 0, ptr = color_bits; y < height; y++)
1190 for (x = 0; x < width; x++, ptr++)
1192 int red = (*ptr >> 16) & 0xff;
1193 int green = (*ptr >> 8) & 0xff;
1194 int blue = (*ptr >> 0) & 0xff;
1195 if (red + green + blue > 0x40)
1197 rfg += red;
1198 gfg += green;
1199 bfg += blue;
1200 fgBits++;
1201 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1203 else
1205 rbg += red;
1206 gbg += green;
1207 bbg += blue;
1211 if (fgBits)
1213 fg.red = rfg * 257 / fgBits;
1214 fg.green = gfg * 257 / fgBits;
1215 fg.blue = bfg * 257 / fgBits;
1217 else fg.red = fg.green = fg.blue = 0;
1218 bgBits = width * height - fgBits;
1219 if (bgBits)
1221 bg.red = rbg * 257 / bgBits;
1222 bg.green = gbg * 257 / bgBits;
1223 bg.blue = bbg * 257 / bgBits;
1225 else bg.red = bg.green = bg.blue = 0;
1227 info->bmiHeader.biBitCount = 1;
1228 info->bmiHeader.biClrUsed = 0;
1229 info->bmiHeader.biSizeImage = width_bytes * height;
1231 /* generate mask from the alpha channel if we have one */
1233 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
1234 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
1236 if (has_alpha)
1238 memset( mask_bits, 0, width_bytes * height );
1239 for (y = 0, ptr = color_bits; y < height; y++)
1240 for (x = 0; x < width; x++, ptr++)
1241 if ((*ptr >> 24) > 25) /* more than 10% alpha */
1242 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1244 else /* invert the mask */
1246 unsigned int j;
1248 ptr = (unsigned int *)mask_bits;
1249 for (j = 0; j < info->bmiHeader.biSizeImage / sizeof(*ptr); j++, ptr++) *ptr ^= ~0u;
1252 vis.depth = 1;
1253 bits.ptr = xor_bits;
1254 bits.free = NULL;
1255 bits.is_copy = TRUE;
1256 if (!(xor_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1258 bits.ptr = mask_bits;
1259 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
1261 if (mask_pixmap)
1263 cursor = XCreatePixmapCursor( gdi_display, xor_pixmap, mask_pixmap,
1264 &fg, &bg, icon->xHotspot, icon->yHotspot );
1265 XFreePixmap( gdi_display, mask_pixmap );
1267 XFreePixmap( gdi_display, xor_pixmap );
1269 done:
1270 HeapFree( GetProcessHeap(), 0, color_bits );
1271 HeapFree( GetProcessHeap(), 0, xor_bits );
1272 HeapFree( GetProcessHeap(), 0, mask_bits );
1273 return cursor;
1276 /***********************************************************************
1277 * create_cursor
1279 * Create an X cursor from a Windows one.
1281 static Cursor create_cursor( HANDLE handle )
1283 Cursor cursor = 0;
1284 ICONINFOEXW info;
1285 BITMAP bm;
1286 HDC hdc;
1288 if (!handle) return get_empty_cursor();
1290 info.cbSize = sizeof(info);
1291 if (!GetIconInfoExW( handle, &info )) return 0;
1293 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
1295 DeleteObject( info.hbmColor );
1296 DeleteObject( info.hbmMask );
1297 return cursor;
1300 GetObjectW( info.hbmMask, sizeof(bm), &bm );
1301 if (!info.hbmColor) bm.bmHeight = max( 1, bm.bmHeight / 2 );
1303 /* make sure hotspot is valid */
1304 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1306 info.xHotspot = bm.bmWidth / 2;
1307 info.yHotspot = bm.bmHeight / 2;
1310 hdc = CreateCompatibleDC( 0 );
1312 if (info.hbmColor)
1314 #ifdef SONAME_LIBXCURSOR
1315 if (pXcursorImagesLoadCursor)
1316 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
1317 #endif
1318 if (!cursor) cursor = create_xlib_color_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1319 DeleteObject( info.hbmColor );
1321 else
1323 cursor = create_xlib_monochrome_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1326 DeleteObject( info.hbmMask );
1327 DeleteDC( hdc );
1328 return cursor;
1331 /***********************************************************************
1332 * DestroyCursorIcon (X11DRV.@)
1334 void CDECL X11DRV_DestroyCursorIcon( HCURSOR handle )
1336 Cursor cursor;
1338 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1340 TRACE( "%p xid %lx\n", handle, cursor );
1341 XFreeCursor( gdi_display, cursor );
1342 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1346 /***********************************************************************
1347 * SetCursor (X11DRV.@)
1349 void CDECL X11DRV_SetCursor( HCURSOR handle )
1351 if (InterlockedExchangePointer( (void **)&last_cursor, handle ) != handle ||
1352 GetTickCount() - last_cursor_change > 100)
1354 last_cursor_change = GetTickCount();
1355 if (cursor_window) SendNotifyMessageW( cursor_window, WM_X11DRV_SET_CURSOR, 0, (LPARAM)handle );
1359 /***********************************************************************
1360 * SetCursorPos (X11DRV.@)
1362 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
1364 struct x11drv_thread_data *data = x11drv_init_thread_data();
1365 POINT pos = virtual_screen_to_root( x, y );
1367 XWarpPointer( data->display, root_window, root_window, 0, 0, 0, 0, pos.x, pos.y );
1368 data->warp_serial = NextRequest( data->display );
1369 XNoOp( data->display );
1370 XFlush( data->display ); /* avoids bad mouse lag in games that do their own mouse warping */
1371 TRACE( "warped to %d,%d serial %lu\n", x, y, data->warp_serial );
1372 return TRUE;
1375 /***********************************************************************
1376 * GetCursorPos (X11DRV.@)
1378 BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
1380 Display *display = thread_init_display();
1381 Window root, child;
1382 int rootX, rootY, winX, winY;
1383 unsigned int xstate;
1384 BOOL ret;
1386 ret = XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &winX, &winY, &xstate );
1387 if (ret)
1389 POINT old = *pos;
1390 *pos = root_to_virtual_screen( winX, winY );
1391 TRACE( "pointer at (%d,%d) server pos %d,%d\n", pos->x, pos->y, old.x, old.y );
1393 return ret;
1396 /***********************************************************************
1397 * ClipCursor (X11DRV.@)
1399 BOOL CDECL X11DRV_ClipCursor( LPCRECT clip )
1401 RECT virtual_rect = get_virtual_screen_rect();
1403 if (!clip) clip = &virtual_rect;
1405 if (grab_pointer)
1407 HWND foreground = GetForegroundWindow();
1409 /* we are clipping if the clip rectangle is smaller than the screen */
1410 if (clip->left > virtual_rect.left || clip->right < virtual_rect.right ||
1411 clip->top > virtual_rect.top || clip->bottom < virtual_rect.bottom)
1413 DWORD tid, pid;
1415 /* forward request to the foreground window if it's in a different thread */
1416 tid = GetWindowThreadProcessId( foreground, &pid );
1417 if (tid && tid != GetCurrentThreadId() && pid == GetCurrentProcessId())
1419 TRACE( "forwarding clip request to %p\n", foreground );
1420 SendNotifyMessageW( foreground, WM_X11DRV_CLIP_CURSOR, 0, 0 );
1421 return TRUE;
1423 else if (grab_clipping_window( clip )) return TRUE;
1425 else /* if currently clipping, check if we should switch to fullscreen clipping */
1427 struct x11drv_thread_data *data = x11drv_thread_data();
1428 if (data && data->clip_hwnd)
1430 if (EqualRect( clip, &clip_rect ) || clip_fullscreen_window( foreground, TRUE ))
1431 return TRUE;
1435 ungrab_clipping_window();
1436 return TRUE;
1439 /***********************************************************************
1440 * move_resize_window
1442 void move_resize_window( HWND hwnd, int dir )
1444 Display *display = thread_display();
1445 DWORD pt;
1446 POINT pos;
1447 int button = 0;
1448 XEvent xev;
1449 Window win, root, child;
1450 unsigned int xstate;
1452 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1454 pt = GetMessagePos();
1455 pos = virtual_screen_to_root( (short)LOWORD( pt ), (short)HIWORD( pt ) );
1457 if (GetKeyState( VK_LBUTTON ) & 0x8000) button = 1;
1458 else if (GetKeyState( VK_MBUTTON ) & 0x8000) button = 2;
1459 else if (GetKeyState( VK_RBUTTON ) & 0x8000) button = 3;
1461 TRACE( "hwnd %p/%lx, x %d, y %d, dir %d, button %d\n", hwnd, win, pos.x, pos.y, dir, button );
1463 xev.xclient.type = ClientMessage;
1464 xev.xclient.window = win;
1465 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1466 xev.xclient.serial = 0;
1467 xev.xclient.display = display;
1468 xev.xclient.send_event = True;
1469 xev.xclient.format = 32;
1470 xev.xclient.data.l[0] = pos.x; /* x coord */
1471 xev.xclient.data.l[1] = pos.y; /* y coord */
1472 xev.xclient.data.l[2] = dir; /* direction */
1473 xev.xclient.data.l[3] = button; /* button */
1474 xev.xclient.data.l[4] = 0; /* unused */
1476 /* need to ungrab the pointer that may have been automatically grabbed
1477 * with a ButtonPress event */
1478 XUngrabPointer( display, CurrentTime );
1479 XSendEvent(display, root_window, False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
1481 /* try to detect the end of the size/move by polling for the mouse button to be released */
1482 /* (some apps don't like it if we return before the size/move is done) */
1484 if (!button) return;
1485 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1487 for (;;)
1489 MSG msg;
1490 INPUT input;
1491 int x, y, rootX, rootY;
1493 if (!XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &x, &y, &xstate )) break;
1495 if (!(xstate & (Button1Mask << (button - 1))))
1497 /* fake a button release event */
1498 pos = root_to_virtual_screen( x, y );
1499 input.type = INPUT_MOUSE;
1500 input.u.mi.dx = pos.x;
1501 input.u.mi.dy = pos.y;
1502 input.u.mi.mouseData = button_up_data[button - 1];
1503 input.u.mi.dwFlags = button_up_flags[button - 1] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1504 input.u.mi.time = GetTickCount();
1505 input.u.mi.dwExtraInfo = 0;
1506 __wine_send_input( hwnd, &input );
1509 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
1511 if (!CallMsgFilterW( &msg, MSGF_SIZE ))
1513 TranslateMessage( &msg );
1514 DispatchMessageW( &msg );
1518 if (!(xstate & (Button1Mask << (button - 1)))) break;
1519 MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT );
1522 TRACE( "hwnd %p/%lx done\n", hwnd, win );
1523 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1527 /***********************************************************************
1528 * X11DRV_ButtonPress
1530 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1532 XButtonEvent *event = &xev->xbutton;
1533 int buttonNum = event->button - 1;
1534 INPUT input;
1536 if (buttonNum >= NB_BUTTONS) return;
1538 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1540 input.u.mi.dx = event->x;
1541 input.u.mi.dy = event->y;
1542 input.u.mi.mouseData = button_down_data[buttonNum];
1543 input.u.mi.dwFlags = button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1544 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1545 input.u.mi.dwExtraInfo = 0;
1547 update_user_time( event->time );
1548 send_mouse_input( hwnd, event->window, event->state, &input );
1552 /***********************************************************************
1553 * X11DRV_ButtonRelease
1555 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1557 XButtonEvent *event = &xev->xbutton;
1558 int buttonNum = event->button - 1;
1559 INPUT input;
1561 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1563 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1565 input.u.mi.dx = event->x;
1566 input.u.mi.dy = event->y;
1567 input.u.mi.mouseData = button_up_data[buttonNum];
1568 input.u.mi.dwFlags = button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1569 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1570 input.u.mi.dwExtraInfo = 0;
1572 send_mouse_input( hwnd, event->window, event->state, &input );
1576 /***********************************************************************
1577 * X11DRV_MotionNotify
1579 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1581 XMotionEvent *event = &xev->xmotion;
1582 INPUT input;
1584 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d serial %lu\n",
1585 hwnd, event->window, event->x, event->y, event->is_hint, event->serial );
1587 input.u.mi.dx = event->x;
1588 input.u.mi.dy = event->y;
1589 input.u.mi.mouseData = 0;
1590 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1591 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1592 input.u.mi.dwExtraInfo = 0;
1594 if (!hwnd && is_old_motion_event( event->serial ))
1596 TRACE( "pos %d,%d old serial %lu, ignoring\n", input.u.mi.dx, input.u.mi.dy, event->serial );
1597 return;
1599 send_mouse_input( hwnd, event->window, event->state, &input );
1603 /***********************************************************************
1604 * X11DRV_EnterNotify
1606 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1608 XCrossingEvent *event = &xev->xcrossing;
1609 INPUT input;
1611 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd, event->window, event->x, event->y, event->detail );
1613 if (event->detail == NotifyVirtual) return;
1614 if (hwnd == x11drv_thread_data()->grab_hwnd) return;
1616 /* simulate a mouse motion event */
1617 input.u.mi.dx = event->x;
1618 input.u.mi.dy = event->y;
1619 input.u.mi.mouseData = 0;
1620 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1621 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1622 input.u.mi.dwExtraInfo = 0;
1624 if (is_old_motion_event( event->serial ))
1626 TRACE( "pos %d,%d old serial %lu, ignoring\n", input.u.mi.dx, input.u.mi.dy, event->serial );
1627 return;
1629 send_mouse_input( hwnd, event->window, event->state, &input );
1632 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1634 /***********************************************************************
1635 * X11DRV_RawMotion
1637 static void X11DRV_RawMotion( XGenericEventCookie *xev )
1639 XIRawEvent *event = xev->data;
1640 const double *values = event->valuators.values;
1641 RECT virtual_rect;
1642 INPUT input;
1643 int i, j;
1644 double dx = 0, dy = 0;
1645 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1646 XIDeviceInfo *devices = thread_data->xi2_devices;
1648 if (!event->valuators.mask_len) return;
1649 if (thread_data->xi2_state != xi_enabled) return;
1651 input.u.mi.mouseData = 0;
1652 input.u.mi.dwFlags = MOUSEEVENTF_MOVE;
1653 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1654 input.u.mi.dwExtraInfo = 0;
1655 input.u.mi.dx = 0;
1656 input.u.mi.dy = 0;
1658 virtual_rect = get_virtual_screen_rect();
1659 for (i = 0; i < thread_data->xi2_device_count; ++i)
1661 if (devices[i].deviceid != event->deviceid) continue;
1662 for (j = 0; j < devices[i].num_classes; j++)
1664 XIValuatorClassInfo *class = (XIValuatorClassInfo *)devices[i].classes[j];
1666 if (devices[i].classes[j]->type != XIValuatorClass) continue;
1667 if (XIMaskIsSet( event->valuators.mask, class->number ))
1669 double val = *values++;
1670 if (class->label == x11drv_atom( Rel_X ) ||
1671 (!class->label && class->number == 0 && class->mode == XIModeRelative))
1673 input.u.mi.dx = dx = val;
1674 if (class->min < class->max)
1675 input.u.mi.dx = val * (virtual_rect.right - virtual_rect.left)
1676 / (class->max - class->min);
1678 else if (class->label == x11drv_atom( Rel_Y ) ||
1679 (!class->label && class->number == 1 && class->mode == XIModeRelative))
1681 input.u.mi.dy = dy = val;
1682 if (class->min < class->max)
1683 input.u.mi.dy = val * (virtual_rect.bottom - virtual_rect.top)
1684 / (class->max - class->min);
1688 break;
1691 if (broken_rawevents && is_old_motion_event( xev->serial ))
1693 TRACE( "pos %d,%d old serial %lu, ignoring\n", input.u.mi.dx, input.u.mi.dy, xev->serial );
1694 return;
1697 TRACE( "pos %d,%d (event %f,%f)\n", input.u.mi.dx, input.u.mi.dy, dx, dy );
1699 input.type = INPUT_MOUSE;
1700 __wine_send_input( 0, &input );
1703 #endif /* HAVE_X11_EXTENSIONS_XINPUT2_H */
1706 /***********************************************************************
1707 * X11DRV_XInput2_Init
1709 void X11DRV_XInput2_Init(void)
1711 #if defined(SONAME_LIBXI) && defined(HAVE_X11_EXTENSIONS_XINPUT2_H)
1712 int event, error;
1713 void *libxi_handle = wine_dlopen( SONAME_LIBXI, RTLD_NOW, NULL, 0 );
1715 if (!libxi_handle)
1717 WARN( "couldn't load %s\n", SONAME_LIBXI );
1718 return;
1720 #define LOAD_FUNCPTR(f) \
1721 if (!(p##f = wine_dlsym( libxi_handle, #f, NULL, 0))) \
1723 WARN("Failed to load %s.\n", #f); \
1724 return; \
1727 LOAD_FUNCPTR(XIFreeDeviceInfo);
1728 LOAD_FUNCPTR(XIQueryDevice);
1729 LOAD_FUNCPTR(XIQueryVersion);
1730 LOAD_FUNCPTR(XISelectEvents);
1731 #undef LOAD_FUNCPTR
1733 xinput2_available = XQueryExtension( gdi_display, "XInputExtension", &xinput2_opcode, &event, &error );
1735 /* Until version 1.10.4 rawinput was broken in XOrg, see
1736 * https://bugs.freedesktop.org/show_bug.cgi?id=30068 */
1737 broken_rawevents = strstr(XServerVendor( gdi_display ), "X.Org") &&
1738 XVendorRelease( gdi_display ) < 11004000;
1740 #else
1741 TRACE( "X Input 2 support not compiled in.\n" );
1742 #endif
1746 /***********************************************************************
1747 * X11DRV_GenericEvent
1749 void X11DRV_GenericEvent( HWND hwnd, XEvent *xev )
1751 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1752 XGenericEventCookie *event = &xev->xcookie;
1754 if (!event->data) return;
1755 if (event->extension != xinput2_opcode) return;
1757 switch (event->evtype)
1759 case XI_RawMotion:
1760 X11DRV_RawMotion( event );
1761 break;
1763 default:
1764 TRACE( "Unhandled event %#x\n", event->evtype );
1765 break;
1767 #endif