regedit: Allow importing strings with escaped NULL.
[wine.git] / dlls / winex11.drv / mouse.c
blobf1e58fe413d6a6a4a9aaefead180f6862d5834d6
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 NONAMELESSSTRUCT
48 #define OEMRESOURCE
49 #include "windef.h"
50 #include "winbase.h"
51 #include "winreg.h"
53 #include "x11drv.h"
54 #include "wine/server.h"
55 #include "wine/library.h"
56 #include "wine/unicode.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
61 /**********************************************************************/
63 #ifndef Button6Mask
64 #define Button6Mask (1<<13)
65 #endif
66 #ifndef Button7Mask
67 #define Button7Mask (1<<14)
68 #endif
70 #define NB_BUTTONS 9 /* Windows can handle 5 buttons and the wheel too */
72 static const UINT button_down_flags[NB_BUTTONS] =
74 MOUSEEVENTF_LEFTDOWN,
75 MOUSEEVENTF_MIDDLEDOWN,
76 MOUSEEVENTF_RIGHTDOWN,
77 MOUSEEVENTF_WHEEL,
78 MOUSEEVENTF_WHEEL,
79 MOUSEEVENTF_XDOWN, /* FIXME: horizontal wheel */
80 MOUSEEVENTF_XDOWN,
81 MOUSEEVENTF_XDOWN,
82 MOUSEEVENTF_XDOWN
85 static const UINT button_up_flags[NB_BUTTONS] =
87 MOUSEEVENTF_LEFTUP,
88 MOUSEEVENTF_MIDDLEUP,
89 MOUSEEVENTF_RIGHTUP,
92 MOUSEEVENTF_XUP,
93 MOUSEEVENTF_XUP,
94 MOUSEEVENTF_XUP,
95 MOUSEEVENTF_XUP
98 static const UINT button_down_data[NB_BUTTONS] =
103 WHEEL_DELTA,
104 -WHEEL_DELTA,
105 XBUTTON1,
106 XBUTTON2,
107 XBUTTON1,
108 XBUTTON2
111 static const UINT button_up_data[NB_BUTTONS] =
118 XBUTTON1,
119 XBUTTON2,
120 XBUTTON1,
121 XBUTTON2
124 XContext cursor_context = 0;
126 static HWND cursor_window;
127 static HCURSOR last_cursor;
128 static DWORD last_cursor_change;
129 static RECT clip_rect;
130 static Cursor create_cursor( HANDLE handle );
132 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
133 static BOOL xinput2_available;
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_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 );
523 /***********************************************************************
524 * send_mouse_input
526 * Update the various window states on a mouse event.
528 static void send_mouse_input( HWND hwnd, Window window, unsigned int state, INPUT *input )
530 struct x11drv_win_data *data;
531 POINT pt;
533 input->type = INPUT_MOUSE;
535 if (!hwnd)
537 struct x11drv_thread_data *thread_data = x11drv_thread_data();
538 HWND clip_hwnd = thread_data->clip_hwnd;
540 if (!clip_hwnd) return;
541 if (thread_data->clip_window != window) return;
542 if (InterlockedExchangePointer( (void **)&cursor_window, clip_hwnd ) != clip_hwnd ||
543 input->u.mi.time - last_cursor_change > 100)
545 sync_window_cursor( window );
546 last_cursor_change = input->u.mi.time;
548 input->u.mi.dx += clip_rect.left;
549 input->u.mi.dy += clip_rect.top;
550 __wine_send_input( hwnd, input );
551 return;
554 if (window != root_window)
556 pt.x = input->u.mi.dx;
557 pt.y = input->u.mi.dy;
559 else pt = root_to_virtual_screen( input->u.mi.dx, input->u.mi.dy );
561 if (!(data = get_win_data( hwnd ))) return;
563 if (window == data->whole_window)
565 pt.x += data->whole_rect.left - data->client_rect.left;
566 pt.y += data->whole_rect.top - data->client_rect.top;
569 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
570 pt.x = data->client_rect.right - data->client_rect.left - 1 - pt.x;
571 MapWindowPoints( hwnd, 0, &pt, 1 );
573 if (InterlockedExchangePointer( (void **)&cursor_window, hwnd ) != hwnd ||
574 input->u.mi.time - last_cursor_change > 100)
576 sync_window_cursor( data->whole_window );
577 last_cursor_change = input->u.mi.time;
579 release_win_data( data );
581 if (hwnd != GetDesktopWindow())
583 hwnd = GetAncestor( hwnd, GA_ROOT );
584 if ((input->u.mi.dwFlags & (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_RIGHTDOWN)) && hwnd == GetForegroundWindow())
585 clip_fullscreen_window( hwnd, FALSE );
588 /* update the wine server Z-order */
590 if (window != x11drv_thread_data()->grab_window &&
591 /* ignore event if a button is pressed, since the mouse is then grabbed too */
592 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
594 RECT rect;
595 SetRect( &rect, pt.x, pt.y, pt.x + 1, pt.y + 1 );
596 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
598 SERVER_START_REQ( update_window_zorder )
600 req->window = wine_server_user_handle( hwnd );
601 req->rect.left = rect.left;
602 req->rect.top = rect.top;
603 req->rect.right = rect.right;
604 req->rect.bottom = rect.bottom;
605 wine_server_call( req );
607 SERVER_END_REQ;
610 input->u.mi.dx = pt.x;
611 input->u.mi.dy = pt.y;
612 __wine_send_input( hwnd, input );
615 #ifdef SONAME_LIBXCURSOR
617 /***********************************************************************
618 * create_xcursor_frame
620 * Use Xcursor to create a frame of an X cursor from a Windows one.
622 static XcursorImage *create_xcursor_frame( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon,
623 HBITMAP hbmColor, unsigned char *color_bits, int color_size,
624 HBITMAP hbmMask, unsigned char *mask_bits, int mask_size,
625 int width, int height, int istep )
627 XcursorImage *image, *ret = NULL;
628 DWORD delay_jiffies, num_steps;
629 int x, y, i;
630 BOOL has_alpha = FALSE;
631 XcursorPixel *ptr;
633 image = pXcursorImageCreate( width, height );
634 if (!image)
636 ERR("X11 failed to produce a cursor frame!\n");
637 return NULL;
640 image->xhot = iinfo->xHotspot;
641 image->yhot = iinfo->yHotspot;
643 image->delay = 100; /* fallback delay, 100 ms */
644 if (GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, istep, &delay_jiffies, &num_steps) != 0)
645 image->delay = (100 * delay_jiffies) / 6; /* convert jiffies (1/60s) to milliseconds */
646 else
647 WARN("Failed to retrieve animated cursor frame-rate for frame %d.\n", istep);
649 /* draw the cursor frame to a temporary buffer then copy it into the XcursorImage */
650 memset( color_bits, 0x00, color_size );
651 SelectObject( hdc, hbmColor );
652 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_NORMAL ))
654 TRACE("Could not draw frame %d (walk past end of frames).\n", istep);
655 goto cleanup;
657 memcpy( image->pixels, color_bits, color_size );
659 /* check if the cursor frame was drawn with an alpha channel */
660 for (i = 0, ptr = image->pixels; i < width * height; i++, ptr++)
661 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
663 /* if no alpha channel was drawn then generate it from the mask */
664 if (!has_alpha)
666 unsigned int width_bytes = (width + 31) / 32 * 4;
668 /* draw the cursor mask to a temporary buffer */
669 memset( mask_bits, 0xFF, mask_size );
670 SelectObject( hdc, hbmMask );
671 if (!DrawIconEx( hdc, 0, 0, icon, width, height, istep, NULL, DI_MASK ))
673 ERR("Failed to draw frame mask %d.\n", istep);
674 goto cleanup;
676 /* use the buffer to directly modify the XcursorImage alpha channel */
677 for (y = 0, ptr = image->pixels; y < height; y++)
678 for (x = 0; x < width; x++, ptr++)
679 if (!((mask_bits[y * width_bytes + x / 8] << (x % 8)) & 0x80))
680 *ptr |= 0xff000000;
682 ret = image;
684 cleanup:
685 if (ret == NULL) pXcursorImageDestroy( image );
686 return ret;
689 /***********************************************************************
690 * create_xcursor_cursor
692 * Use Xcursor to create an X cursor from a Windows one.
694 static Cursor create_xcursor_cursor( HDC hdc, const ICONINFOEXW *iinfo, HANDLE icon, int width, int height )
696 unsigned char *color_bits, *mask_bits;
697 HBITMAP hbmColor = 0, hbmMask = 0;
698 DWORD nFrames, delay_jiffies, i;
699 int color_size, mask_size;
700 BITMAPINFO *info = NULL;
701 XcursorImages *images;
702 XcursorImage **imgs;
703 Cursor cursor = 0;
705 /* Retrieve the number of frames to render */
706 if (!GetCursorFrameInfo(icon, 0x0 /* unknown parameter */, 0, &delay_jiffies, &nFrames)) return 0;
707 if (!(imgs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XcursorImage*)*nFrames ))) return 0;
709 /* Allocate all of the resources necessary to obtain a cursor frame */
710 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto cleanup;
711 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
712 info->bmiHeader.biWidth = width;
713 info->bmiHeader.biHeight = -height;
714 info->bmiHeader.biPlanes = 1;
715 info->bmiHeader.biCompression = BI_RGB;
716 info->bmiHeader.biXPelsPerMeter = 0;
717 info->bmiHeader.biYPelsPerMeter = 0;
718 info->bmiHeader.biClrUsed = 0;
719 info->bmiHeader.biClrImportant = 0;
720 info->bmiHeader.biBitCount = 32;
721 color_size = width * height * 4;
722 info->bmiHeader.biSizeImage = color_size;
723 hbmColor = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &color_bits, NULL, 0);
724 if (!hbmColor)
726 ERR("Failed to create DIB section for cursor color data!\n");
727 goto cleanup;
729 info->bmiHeader.biBitCount = 1;
730 info->bmiColors[0].rgbRed = 0;
731 info->bmiColors[0].rgbGreen = 0;
732 info->bmiColors[0].rgbBlue = 0;
733 info->bmiColors[0].rgbReserved = 0;
734 info->bmiColors[1].rgbRed = 0xff;
735 info->bmiColors[1].rgbGreen = 0xff;
736 info->bmiColors[1].rgbBlue = 0xff;
737 info->bmiColors[1].rgbReserved = 0;
739 mask_size = ((width + 31) / 32 * 4) * height; /* width_bytes * height */
740 info->bmiHeader.biSizeImage = mask_size;
741 hbmMask = CreateDIBSection( hdc, info, DIB_RGB_COLORS, (VOID **) &mask_bits, NULL, 0);
742 if (!hbmMask)
744 ERR("Failed to create DIB section for cursor mask data!\n");
745 goto cleanup;
748 /* Create an XcursorImage for each frame of the cursor */
749 for (i=0; i<nFrames; i++)
751 imgs[i] = create_xcursor_frame( hdc, iinfo, icon,
752 hbmColor, color_bits, color_size,
753 hbmMask, mask_bits, mask_size,
754 width, height, i );
755 if (!imgs[i]) goto cleanup;
758 /* Build an X cursor out of all of the frames */
759 if (!(images = pXcursorImagesCreate( nFrames ))) goto cleanup;
760 for (images->nimage = 0; images->nimage < nFrames; images->nimage++)
761 images->images[images->nimage] = imgs[images->nimage];
762 cursor = pXcursorImagesLoadCursor( gdi_display, images );
763 pXcursorImagesDestroy( images ); /* Note: this frees each individual frame (calls XcursorImageDestroy) */
764 HeapFree( GetProcessHeap(), 0, imgs );
765 imgs = NULL;
767 cleanup:
768 if (imgs)
770 /* Failed to produce a cursor, free previously allocated frames */
771 for (i=0; i<nFrames && imgs[i]; i++)
772 pXcursorImageDestroy( imgs[i] );
773 HeapFree( GetProcessHeap(), 0, imgs );
775 /* Cleanup all of the resources used to obtain the frame data */
776 if (hbmColor) DeleteObject( hbmColor );
777 if (hbmMask) DeleteObject( hbmMask );
778 HeapFree( GetProcessHeap(), 0, info );
779 return cursor;
782 #endif /* SONAME_LIBXCURSOR */
785 struct system_cursors
787 WORD id;
788 const char *name;
791 static const struct system_cursors user32_cursors[] =
793 { OCR_NORMAL, "left_ptr" },
794 { OCR_IBEAM, "xterm" },
795 { OCR_WAIT, "watch" },
796 { OCR_CROSS, "cross" },
797 { OCR_UP, "center_ptr" },
798 { OCR_SIZE, "fleur" },
799 { OCR_SIZEALL, "fleur" },
800 { OCR_ICON, "icon" },
801 { OCR_SIZENWSE, "nwse-resize" },
802 { OCR_SIZENESW, "nesw-resize" },
803 { OCR_SIZEWE, "ew-resize" },
804 { OCR_SIZENS, "ns-resize" },
805 { OCR_NO, "not-allowed" },
806 { OCR_HAND, "hand2" },
807 { OCR_APPSTARTING, "left_ptr_watch" },
808 { OCR_HELP, "question_arrow" },
809 { 0 }
812 static const struct system_cursors comctl32_cursors[] =
814 { 102, "move" },
815 { 104, "copy" },
816 { 105, "left_ptr" },
817 { 106, "row-resize" },
818 { 107, "row-resize" },
819 { 108, "hand2" },
820 { 135, "col-resize" },
821 { 0 }
824 static const struct system_cursors ole32_cursors[] =
826 { 1, "no-drop" },
827 { 2, "move" },
828 { 3, "copy" },
829 { 4, "alias" },
830 { 0 }
833 static const struct system_cursors riched20_cursors[] =
835 { 105, "hand2" },
836 { 107, "right_ptr" },
837 { 109, "copy" },
838 { 110, "move" },
839 { 111, "no-drop" },
840 { 0 }
843 static const struct
845 const struct system_cursors *cursors;
846 WCHAR name[16];
847 } module_cursors[] =
849 { user32_cursors, {'u','s','e','r','3','2','.','d','l','l',0} },
850 { comctl32_cursors, {'c','o','m','c','t','l','3','2','.','d','l','l',0} },
851 { ole32_cursors, {'o','l','e','3','2','.','d','l','l',0} },
852 { riched20_cursors, {'r','i','c','h','e','d','2','0','.','d','l','l',0} }
855 struct cursor_font_fallback
857 const char *name;
858 unsigned int shape;
861 static const struct cursor_font_fallback fallbacks[] =
863 { "X_cursor", XC_X_cursor },
864 { "arrow", XC_arrow },
865 { "based_arrow_down", XC_based_arrow_down },
866 { "based_arrow_up", XC_based_arrow_up },
867 { "boat", XC_boat },
868 { "bogosity", XC_bogosity },
869 { "bottom_left_corner", XC_bottom_left_corner },
870 { "bottom_right_corner", XC_bottom_right_corner },
871 { "bottom_side", XC_bottom_side },
872 { "bottom_tee", XC_bottom_tee },
873 { "box_spiral", XC_box_spiral },
874 { "center_ptr", XC_center_ptr },
875 { "circle", XC_circle },
876 { "clock", XC_clock },
877 { "coffee_mug", XC_coffee_mug },
878 { "col-resize", XC_sb_v_double_arrow },
879 { "cross", XC_cross },
880 { "cross_reverse", XC_cross_reverse },
881 { "crosshair", XC_crosshair },
882 { "diamond_cross", XC_diamond_cross },
883 { "dot", XC_dot },
884 { "dotbox", XC_dotbox },
885 { "double_arrow", XC_double_arrow },
886 { "draft_large", XC_draft_large },
887 { "draft_small", XC_draft_small },
888 { "draped_box", XC_draped_box },
889 { "exchange", XC_exchange },
890 { "fleur", XC_fleur },
891 { "gobbler", XC_gobbler },
892 { "gumby", XC_gumby },
893 { "hand1", XC_hand1 },
894 { "hand2", XC_hand2 },
895 { "heart", XC_heart },
896 { "icon", XC_icon },
897 { "iron_cross", XC_iron_cross },
898 { "left_ptr", XC_left_ptr },
899 { "left_side", XC_left_side },
900 { "left_tee", XC_left_tee },
901 { "leftbutton", XC_leftbutton },
902 { "ll_angle", XC_ll_angle },
903 { "lr_angle", XC_lr_angle },
904 { "man", XC_man },
905 { "middlebutton", XC_middlebutton },
906 { "mouse", XC_mouse },
907 { "pencil", XC_pencil },
908 { "pirate", XC_pirate },
909 { "plus", XC_plus },
910 { "question_arrow", XC_question_arrow },
911 { "right_ptr", XC_right_ptr },
912 { "right_side", XC_right_side },
913 { "right_tee", XC_right_tee },
914 { "rightbutton", XC_rightbutton },
915 { "row-resize", XC_sb_h_double_arrow },
916 { "rtl_logo", XC_rtl_logo },
917 { "sailboat", XC_sailboat },
918 { "sb_down_arrow", XC_sb_down_arrow },
919 { "sb_h_double_arrow", XC_sb_h_double_arrow },
920 { "sb_left_arrow", XC_sb_left_arrow },
921 { "sb_right_arrow", XC_sb_right_arrow },
922 { "sb_up_arrow", XC_sb_up_arrow },
923 { "sb_v_double_arrow", XC_sb_v_double_arrow },
924 { "shuttle", XC_shuttle },
925 { "sizing", XC_sizing },
926 { "spider", XC_spider },
927 { "spraycan", XC_spraycan },
928 { "star", XC_star },
929 { "target", XC_target },
930 { "tcross", XC_tcross },
931 { "top_left_arrow", XC_top_left_arrow },
932 { "top_left_corner", XC_top_left_corner },
933 { "top_right_corner", XC_top_right_corner },
934 { "top_side", XC_top_side },
935 { "top_tee", XC_top_tee },
936 { "trek", XC_trek },
937 { "ul_angle", XC_ul_angle },
938 { "umbrella", XC_umbrella },
939 { "ur_angle", XC_ur_angle },
940 { "watch", XC_watch },
941 { "xterm", XC_xterm }
944 static int fallback_cmp( const void *key, const void *member )
946 const struct cursor_font_fallback *fallback = member;
947 return strcmp( key, fallback->name );
950 static int find_fallback_shape( const char *name )
952 struct cursor_font_fallback *fallback;
954 if ((fallback = bsearch( name, fallbacks, sizeof(fallbacks) / sizeof(fallbacks[0]),
955 sizeof(*fallback), fallback_cmp )))
956 return fallback->shape;
957 return -1;
960 /***********************************************************************
961 * create_xcursor_system_cursor
963 * Create an X cursor for a system cursor.
965 static Cursor create_xcursor_system_cursor( const ICONINFOEXW *info )
967 static const WCHAR idW[] = {'%','h','u',0};
968 const struct system_cursors *cursors;
969 unsigned int i;
970 Cursor cursor = 0;
971 HMODULE module;
972 HKEY key;
973 WCHAR *p, name[MAX_PATH * 2], valueW[64];
974 char valueA[64];
975 DWORD size, ret;
977 if (!info->szModName[0]) return 0;
979 p = strrchrW( info->szModName, '\\' );
980 strcpyW( name, p ? p + 1 : info->szModName );
981 p = name + strlenW( name );
982 *p++ = ',';
983 if (info->szResName[0]) strcpyW( p, info->szResName );
984 else sprintfW( p, idW, info->wResID );
985 valueA[0] = 0;
987 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Cursors */
988 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver\\Cursors", &key ))
990 size = sizeof(valueW) / sizeof(WCHAR);
991 ret = RegQueryValueExW( key, name, NULL, NULL, (BYTE *)valueW, &size );
992 RegCloseKey( key );
993 if (!ret)
995 if (!valueW[0]) return 0; /* force standard cursor */
996 if (!WideCharToMultiByte( CP_UNIXCP, 0, valueW, -1, valueA, sizeof(valueA), NULL, NULL ))
997 valueA[0] = 0;
998 goto done;
1002 if (info->szResName[0]) goto done; /* only integer resources are supported here */
1003 if (!(module = GetModuleHandleW( info->szModName ))) goto done;
1005 for (i = 0; i < sizeof(module_cursors)/sizeof(module_cursors[0]); i++)
1006 if (GetModuleHandleW( module_cursors[i].name ) == module) break;
1007 if (i == sizeof(module_cursors)/sizeof(module_cursors[0])) goto done;
1009 cursors = module_cursors[i].cursors;
1010 for (i = 0; cursors[i].id; i++)
1011 if (cursors[i].id == info->wResID)
1013 strcpy( valueA, cursors[i].name );
1014 break;
1017 done:
1018 if (valueA[0])
1020 #ifdef SONAME_LIBXCURSOR
1021 if (pXcursorLibraryLoadCursor) cursor = pXcursorLibraryLoadCursor( gdi_display, valueA );
1022 #endif
1023 if (!cursor)
1025 int shape = find_fallback_shape( valueA );
1026 if (shape != -1) cursor = XCreateFontCursor( gdi_display, shape );
1028 if (!cursor) WARN( "no system cursor found for %s mapped to %s\n",
1029 debugstr_w(name), debugstr_a(valueA) );
1031 else WARN( "no system cursor found for %s\n", debugstr_w(name) );
1032 return cursor;
1036 /***********************************************************************
1037 * create_xlib_monochrome_cursor
1039 * Create a monochrome X cursor from a Windows one.
1041 static Cursor create_xlib_monochrome_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1043 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1044 BITMAPINFO *info = (BITMAPINFO *)buffer;
1045 const int and_y = 0;
1046 const int xor_y = height;
1047 unsigned int width_bytes = (width + 31) / 32 * 4;
1048 unsigned char *mask_bits = NULL;
1049 GC gc;
1050 XColor fg, bg;
1051 XVisualInfo vis = default_visual;
1052 Pixmap src_pixmap, bits_pixmap, mask_pixmap;
1053 struct gdi_image_bits bits;
1054 Cursor cursor = 0;
1056 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1057 info->bmiHeader.biWidth = width;
1058 info->bmiHeader.biHeight = -height * 2;
1059 info->bmiHeader.biPlanes = 1;
1060 info->bmiHeader.biBitCount = 1;
1061 info->bmiHeader.biCompression = BI_RGB;
1062 info->bmiHeader.biSizeImage = width_bytes * height * 2;
1063 info->bmiHeader.biXPelsPerMeter = 0;
1064 info->bmiHeader.biYPelsPerMeter = 0;
1065 info->bmiHeader.biClrUsed = 0;
1066 info->bmiHeader.biClrImportant = 0;
1068 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1069 if (!GetDIBits( hdc, icon->hbmMask, 0, height * 2, mask_bits, info, DIB_RGB_COLORS )) goto done;
1071 vis.depth = 1;
1072 bits.ptr = mask_bits;
1073 bits.free = NULL;
1074 bits.is_copy = TRUE;
1075 if (!(src_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1077 bits_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
1078 mask_pixmap = XCreatePixmap( gdi_display, root_window, width, height, 1 );
1079 gc = XCreateGC( gdi_display, src_pixmap, 0, NULL );
1080 XSetGraphicsExposures( gdi_display, gc, False );
1082 /* We have to do some magic here, as cursors are not fully
1083 * compatible between Windows and X11. Under X11, there are
1084 * only 3 possible color cursor: black, white and masked. So
1085 * we map the 4th Windows color (invert the bits on the screen)
1086 * to black and an additional white bit on another place
1087 * (+1,+1). This require some boolean arithmetic:
1089 * Windows | X11
1090 * And Xor Result | Bits Mask Result
1091 * 0 0 black | 0 1 background
1092 * 0 1 white | 1 1 foreground
1093 * 1 0 no change | X 0 no change
1094 * 1 1 inverted | 0 1 background
1096 * which gives:
1097 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
1098 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
1100 XSetFunction( gdi_display, gc, GXcopy );
1101 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 0, 0 );
1102 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 0, 0 );
1103 XSetFunction( gdi_display, gc, GXandReverse );
1104 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, xor_y, width, height, 0, 0 );
1105 XSetFunction( gdi_display, gc, GXorReverse );
1106 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, xor_y, width, height, 0, 0 );
1107 /* additional white */
1108 XSetFunction( gdi_display, gc, GXand );
1109 XCopyArea( gdi_display, src_pixmap, src_pixmap, gc, 0, xor_y, width, height, 0, and_y );
1110 XSetFunction( gdi_display, gc, GXor );
1111 XCopyArea( gdi_display, src_pixmap, mask_pixmap, gc, 0, and_y, width, height, 1, 1 );
1112 XCopyArea( gdi_display, src_pixmap, bits_pixmap, gc, 0, and_y, width, height, 1, 1 );
1113 XFreeGC( gdi_display, gc );
1115 fg.red = fg.green = fg.blue = 0xffff;
1116 bg.red = bg.green = bg.blue = 0;
1117 cursor = XCreatePixmapCursor( gdi_display, bits_pixmap, mask_pixmap,
1118 &fg, &bg, icon->xHotspot, icon->yHotspot );
1119 XFreePixmap( gdi_display, src_pixmap );
1120 XFreePixmap( gdi_display, bits_pixmap );
1121 XFreePixmap( gdi_display, mask_pixmap );
1123 done:
1124 HeapFree( GetProcessHeap(), 0, mask_bits );
1125 return cursor;
1128 /***********************************************************************
1129 * create_xlib_color_cursor
1131 * Create a color X cursor from a Windows one.
1133 static Cursor create_xlib_color_cursor( HDC hdc, const ICONINFOEXW *icon, int width, int height )
1135 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1136 BITMAPINFO *info = (BITMAPINFO *)buffer;
1137 XColor fg, bg;
1138 Cursor cursor = None;
1139 XVisualInfo vis = default_visual;
1140 Pixmap xor_pixmap, mask_pixmap;
1141 struct gdi_image_bits bits;
1142 unsigned int *color_bits = NULL, *ptr;
1143 unsigned char *mask_bits = NULL, *xor_bits = NULL;
1144 int i, x, y;
1145 BOOL has_alpha = FALSE;
1146 int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
1147 unsigned int width_bytes = (width + 31) / 32 * 4;
1149 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1150 info->bmiHeader.biWidth = width;
1151 info->bmiHeader.biHeight = -height;
1152 info->bmiHeader.biPlanes = 1;
1153 info->bmiHeader.biBitCount = 1;
1154 info->bmiHeader.biCompression = BI_RGB;
1155 info->bmiHeader.biSizeImage = width_bytes * height;
1156 info->bmiHeader.biXPelsPerMeter = 0;
1157 info->bmiHeader.biYPelsPerMeter = 0;
1158 info->bmiHeader.biClrUsed = 0;
1159 info->bmiHeader.biClrImportant = 0;
1161 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1162 if (!GetDIBits( hdc, icon->hbmMask, 0, height, mask_bits, info, DIB_RGB_COLORS )) goto done;
1164 info->bmiHeader.biBitCount = 32;
1165 info->bmiHeader.biSizeImage = width * height * 4;
1166 if (!(color_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
1167 if (!(xor_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, width_bytes * height ))) goto done;
1168 GetDIBits( hdc, icon->hbmColor, 0, height, color_bits, info, DIB_RGB_COLORS );
1170 /* compute fg/bg color and xor bitmap based on average of the color values */
1172 rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
1173 for (y = 0, ptr = color_bits; y < height; y++)
1175 for (x = 0; x < width; x++, ptr++)
1177 int red = (*ptr >> 16) & 0xff;
1178 int green = (*ptr >> 8) & 0xff;
1179 int blue = (*ptr >> 0) & 0xff;
1180 if (red + green + blue > 0x40)
1182 rfg += red;
1183 gfg += green;
1184 bfg += blue;
1185 fgBits++;
1186 xor_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1188 else
1190 rbg += red;
1191 gbg += green;
1192 bbg += blue;
1196 if (fgBits)
1198 fg.red = rfg * 257 / fgBits;
1199 fg.green = gfg * 257 / fgBits;
1200 fg.blue = bfg * 257 / fgBits;
1202 else fg.red = fg.green = fg.blue = 0;
1203 bgBits = width * height - fgBits;
1204 if (bgBits)
1206 bg.red = rbg * 257 / bgBits;
1207 bg.green = gbg * 257 / bgBits;
1208 bg.blue = bbg * 257 / bgBits;
1210 else bg.red = bg.green = bg.blue = 0;
1212 info->bmiHeader.biBitCount = 1;
1213 info->bmiHeader.biClrUsed = 0;
1214 info->bmiHeader.biSizeImage = width_bytes * height;
1216 /* generate mask from the alpha channel if we have one */
1218 for (i = 0, ptr = color_bits; i < width * height; i++, ptr++)
1219 if ((has_alpha = (*ptr & 0xff000000) != 0)) break;
1221 if (has_alpha)
1223 memset( mask_bits, 0, width_bytes * height );
1224 for (y = 0, ptr = color_bits; y < height; y++)
1225 for (x = 0; x < width; x++, ptr++)
1226 if ((*ptr >> 24) > 25) /* more than 10% alpha */
1227 mask_bits[y * width_bytes + x / 8] |= 0x80 >> (x % 8);
1229 else /* invert the mask */
1231 unsigned int j;
1233 ptr = (unsigned int *)mask_bits;
1234 for (j = 0; j < info->bmiHeader.biSizeImage / sizeof(*ptr); j++, ptr++) *ptr ^= ~0u;
1237 vis.depth = 1;
1238 bits.ptr = xor_bits;
1239 bits.free = NULL;
1240 bits.is_copy = TRUE;
1241 if (!(xor_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS ))) goto done;
1243 bits.ptr = mask_bits;
1244 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
1246 if (mask_pixmap)
1248 cursor = XCreatePixmapCursor( gdi_display, xor_pixmap, mask_pixmap,
1249 &fg, &bg, icon->xHotspot, icon->yHotspot );
1250 XFreePixmap( gdi_display, mask_pixmap );
1252 XFreePixmap( gdi_display, xor_pixmap );
1254 done:
1255 HeapFree( GetProcessHeap(), 0, color_bits );
1256 HeapFree( GetProcessHeap(), 0, xor_bits );
1257 HeapFree( GetProcessHeap(), 0, mask_bits );
1258 return cursor;
1261 /***********************************************************************
1262 * create_cursor
1264 * Create an X cursor from a Windows one.
1266 static Cursor create_cursor( HANDLE handle )
1268 Cursor cursor = 0;
1269 ICONINFOEXW info;
1270 BITMAP bm;
1271 HDC hdc;
1273 if (!handle) return get_empty_cursor();
1275 info.cbSize = sizeof(info);
1276 if (!GetIconInfoExW( handle, &info )) return 0;
1278 if (use_system_cursors && (cursor = create_xcursor_system_cursor( &info )))
1280 DeleteObject( info.hbmColor );
1281 DeleteObject( info.hbmMask );
1282 return cursor;
1285 GetObjectW( info.hbmMask, sizeof(bm), &bm );
1286 if (!info.hbmColor) bm.bmHeight = max( 1, bm.bmHeight / 2 );
1288 /* make sure hotspot is valid */
1289 if (info.xHotspot >= bm.bmWidth || info.yHotspot >= bm.bmHeight)
1291 info.xHotspot = bm.bmWidth / 2;
1292 info.yHotspot = bm.bmHeight / 2;
1295 hdc = CreateCompatibleDC( 0 );
1297 if (info.hbmColor)
1299 #ifdef SONAME_LIBXCURSOR
1300 if (pXcursorImagesLoadCursor)
1301 cursor = create_xcursor_cursor( hdc, &info, handle, bm.bmWidth, bm.bmHeight );
1302 #endif
1303 if (!cursor) cursor = create_xlib_color_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1304 DeleteObject( info.hbmColor );
1306 else
1308 cursor = create_xlib_monochrome_cursor( hdc, &info, bm.bmWidth, bm.bmHeight );
1311 DeleteObject( info.hbmMask );
1312 DeleteDC( hdc );
1313 return cursor;
1316 /***********************************************************************
1317 * DestroyCursorIcon (X11DRV.@)
1319 void CDECL X11DRV_DestroyCursorIcon( HCURSOR handle )
1321 Cursor cursor;
1323 if (!XFindContext( gdi_display, (XID)handle, cursor_context, (char **)&cursor ))
1325 TRACE( "%p xid %lx\n", handle, cursor );
1326 XFreeCursor( gdi_display, cursor );
1327 XDeleteContext( gdi_display, (XID)handle, cursor_context );
1331 /***********************************************************************
1332 * SetCursor (X11DRV.@)
1334 void CDECL X11DRV_SetCursor( HCURSOR handle )
1336 if (InterlockedExchangePointer( (void **)&last_cursor, handle ) != handle ||
1337 GetTickCount() - last_cursor_change > 100)
1339 last_cursor_change = GetTickCount();
1340 if (cursor_window) SendNotifyMessageW( cursor_window, WM_X11DRV_SET_CURSOR, 0, (LPARAM)handle );
1344 /***********************************************************************
1345 * SetCursorPos (X11DRV.@)
1347 BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
1349 struct x11drv_thread_data *data = x11drv_init_thread_data();
1350 POINT pos = virtual_screen_to_root( x, y );
1352 XWarpPointer( data->display, root_window, root_window, 0, 0, 0, 0, pos.x, pos.y );
1353 data->warp_serial = NextRequest( data->display );
1354 XNoOp( data->display );
1355 XFlush( data->display ); /* avoids bad mouse lag in games that do their own mouse warping */
1356 TRACE( "warped to %d,%d serial %lu\n", x, y, data->warp_serial );
1357 return TRUE;
1360 /***********************************************************************
1361 * GetCursorPos (X11DRV.@)
1363 BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
1365 Display *display = thread_init_display();
1366 Window root, child;
1367 int rootX, rootY, winX, winY;
1368 unsigned int xstate;
1369 BOOL ret;
1371 ret = XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &winX, &winY, &xstate );
1372 if (ret)
1374 POINT old = *pos;
1375 *pos = root_to_virtual_screen( winX, winY );
1376 TRACE( "pointer at (%d,%d) server pos %d,%d\n", pos->x, pos->y, old.x, old.y );
1378 return ret;
1381 /***********************************************************************
1382 * ClipCursor (X11DRV.@)
1384 BOOL CDECL X11DRV_ClipCursor( LPCRECT clip )
1386 RECT virtual_rect = get_virtual_screen_rect();
1388 if (!clip) clip = &virtual_rect;
1390 if (grab_pointer)
1392 HWND foreground = GetForegroundWindow();
1394 /* we are clipping if the clip rectangle is smaller than the screen */
1395 if (clip->left > virtual_rect.left || clip->right < virtual_rect.right ||
1396 clip->top > virtual_rect.top || clip->bottom < virtual_rect.bottom)
1398 DWORD tid, pid;
1400 /* forward request to the foreground window if it's in a different thread */
1401 tid = GetWindowThreadProcessId( foreground, &pid );
1402 if (tid && tid != GetCurrentThreadId() && pid == GetCurrentProcessId())
1404 TRACE( "forwarding clip request to %p\n", foreground );
1405 SendNotifyMessageW( foreground, WM_X11DRV_CLIP_CURSOR, 0, 0 );
1406 return TRUE;
1408 else if (grab_clipping_window( clip )) return TRUE;
1410 else /* if currently clipping, check if we should switch to fullscreen clipping */
1412 struct x11drv_thread_data *data = x11drv_thread_data();
1413 if (data && data->clip_hwnd)
1415 if (EqualRect( clip, &clip_rect ) || clip_fullscreen_window( foreground, TRUE ))
1416 return TRUE;
1420 ungrab_clipping_window();
1421 return TRUE;
1424 /***********************************************************************
1425 * move_resize_window
1427 void move_resize_window( HWND hwnd, int dir )
1429 Display *display = thread_display();
1430 DWORD pt;
1431 POINT pos;
1432 int button = 0;
1433 XEvent xev;
1434 Window win, root, child;
1435 unsigned int xstate;
1437 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1439 pt = GetMessagePos();
1440 pos = virtual_screen_to_root( (short)LOWORD( pt ), (short)HIWORD( pt ) );
1442 if (GetKeyState( VK_LBUTTON ) & 0x8000) button = 1;
1443 else if (GetKeyState( VK_MBUTTON ) & 0x8000) button = 2;
1444 else if (GetKeyState( VK_RBUTTON ) & 0x8000) button = 3;
1446 TRACE( "hwnd %p/%lx, x %d, y %d, dir %d, button %d\n", hwnd, win, pos.x, pos.y, dir, button );
1448 xev.xclient.type = ClientMessage;
1449 xev.xclient.window = win;
1450 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1451 xev.xclient.serial = 0;
1452 xev.xclient.display = display;
1453 xev.xclient.send_event = True;
1454 xev.xclient.format = 32;
1455 xev.xclient.data.l[0] = pos.x; /* x coord */
1456 xev.xclient.data.l[1] = pos.y; /* y coord */
1457 xev.xclient.data.l[2] = dir; /* direction */
1458 xev.xclient.data.l[3] = button; /* button */
1459 xev.xclient.data.l[4] = 0; /* unused */
1461 /* need to ungrab the pointer that may have been automatically grabbed
1462 * with a ButtonPress event */
1463 XUngrabPointer( display, CurrentTime );
1464 XSendEvent(display, root_window, False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
1466 /* try to detect the end of the size/move by polling for the mouse button to be released */
1467 /* (some apps don't like it if we return before the size/move is done) */
1469 if (!button) return;
1470 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1472 for (;;)
1474 MSG msg;
1475 INPUT input;
1476 int x, y, rootX, rootY;
1478 if (!XQueryPointer( display, root_window, &root, &child, &rootX, &rootY, &x, &y, &xstate )) break;
1480 if (!(xstate & (Button1Mask << (button - 1))))
1482 /* fake a button release event */
1483 pos = root_to_virtual_screen( x, y );
1484 input.type = INPUT_MOUSE;
1485 input.u.mi.dx = pos.x;
1486 input.u.mi.dy = pos.y;
1487 input.u.mi.mouseData = button_up_data[button - 1];
1488 input.u.mi.dwFlags = button_up_flags[button - 1] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1489 input.u.mi.time = GetTickCount();
1490 input.u.mi.dwExtraInfo = 0;
1491 __wine_send_input( hwnd, &input );
1494 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
1496 if (!CallMsgFilterW( &msg, MSGF_SIZE ))
1498 TranslateMessage( &msg );
1499 DispatchMessageW( &msg );
1503 if (!(xstate & (Button1Mask << (button - 1)))) break;
1504 MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT );
1507 TRACE( "hwnd %p/%lx done\n", hwnd, win );
1508 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1512 /***********************************************************************
1513 * X11DRV_ButtonPress
1515 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1517 XButtonEvent *event = &xev->xbutton;
1518 int buttonNum = event->button - 1;
1519 INPUT input;
1521 if (buttonNum >= NB_BUTTONS) return;
1523 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1525 input.u.mi.dx = event->x;
1526 input.u.mi.dy = event->y;
1527 input.u.mi.mouseData = button_down_data[buttonNum];
1528 input.u.mi.dwFlags = button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1529 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1530 input.u.mi.dwExtraInfo = 0;
1532 update_user_time( event->time );
1533 send_mouse_input( hwnd, event->window, event->state, &input );
1537 /***********************************************************************
1538 * X11DRV_ButtonRelease
1540 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1542 XButtonEvent *event = &xev->xbutton;
1543 int buttonNum = event->button - 1;
1544 INPUT input;
1546 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1548 TRACE( "hwnd %p/%lx button %u pos %d,%d\n", hwnd, event->window, buttonNum, event->x, event->y );
1550 input.u.mi.dx = event->x;
1551 input.u.mi.dy = event->y;
1552 input.u.mi.mouseData = button_up_data[buttonNum];
1553 input.u.mi.dwFlags = button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
1554 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1555 input.u.mi.dwExtraInfo = 0;
1557 send_mouse_input( hwnd, event->window, event->state, &input );
1561 /***********************************************************************
1562 * X11DRV_MotionNotify
1564 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1566 XMotionEvent *event = &xev->xmotion;
1567 INPUT input;
1569 TRACE( "hwnd %p/%lx pos %d,%d is_hint %d serial %lu\n",
1570 hwnd, event->window, event->x, event->y, event->is_hint, event->serial );
1572 input.u.mi.dx = event->x;
1573 input.u.mi.dy = event->y;
1574 input.u.mi.mouseData = 0;
1575 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1576 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1577 input.u.mi.dwExtraInfo = 0;
1579 if (!hwnd)
1581 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1582 if (thread_data->warp_serial && (long)(event->serial - thread_data->warp_serial) < 0) return;
1585 send_mouse_input( hwnd, event->window, event->state, &input );
1589 /***********************************************************************
1590 * X11DRV_EnterNotify
1592 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1594 XCrossingEvent *event = &xev->xcrossing;
1595 INPUT input;
1597 TRACE( "hwnd %p/%lx pos %d,%d detail %d\n", hwnd, event->window, event->x, event->y, event->detail );
1599 if (event->detail == NotifyVirtual) return;
1600 if (event->window == x11drv_thread_data()->grab_window) return;
1602 /* simulate a mouse motion event */
1603 input.u.mi.dx = event->x;
1604 input.u.mi.dy = event->y;
1605 input.u.mi.mouseData = 0;
1606 input.u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
1607 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1608 input.u.mi.dwExtraInfo = 0;
1610 send_mouse_input( hwnd, event->window, event->state, &input );
1613 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1615 /***********************************************************************
1616 * X11DRV_RawMotion
1618 static void X11DRV_RawMotion( XGenericEventCookie *xev )
1620 XIRawEvent *event = xev->data;
1621 const double *values = event->valuators.values;
1622 RECT virtual_rect;
1623 INPUT input;
1624 int i, j;
1625 double dx = 0, dy = 0;
1626 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1627 XIDeviceInfo *devices = thread_data->xi2_devices;
1629 if (!event->valuators.mask_len) return;
1630 if (thread_data->xi2_state != xi_enabled) return;
1632 input.u.mi.mouseData = 0;
1633 input.u.mi.dwFlags = MOUSEEVENTF_MOVE;
1634 input.u.mi.time = EVENT_x11_time_to_win32_time( event->time );
1635 input.u.mi.dwExtraInfo = 0;
1636 input.u.mi.dx = 0;
1637 input.u.mi.dy = 0;
1639 virtual_rect = get_virtual_screen_rect();
1640 for (i = 0; i < thread_data->xi2_device_count; ++i)
1642 if (devices[i].deviceid != event->deviceid) continue;
1643 for (j = 0; j < devices[i].num_classes; j++)
1645 XIValuatorClassInfo *class = (XIValuatorClassInfo *)devices[i].classes[j];
1647 if (devices[i].classes[j]->type != XIValuatorClass) continue;
1648 if (XIMaskIsSet( event->valuators.mask, class->number ))
1650 double val = *values++;
1651 if (class->label == x11drv_atom( Rel_X ) ||
1652 (!class->label && class->number == 0 && class->mode == XIModeRelative))
1654 input.u.mi.dx = dx = val;
1655 if (class->min < class->max)
1656 input.u.mi.dx = val * (virtual_rect.right - virtual_rect.left)
1657 / (class->max - class->min);
1659 else if (class->label == x11drv_atom( Rel_Y ) ||
1660 (!class->label && class->number == 1 && class->mode == XIModeRelative))
1662 input.u.mi.dy = dy = val;
1663 if (class->min < class->max)
1664 input.u.mi.dy = val * (virtual_rect.bottom - virtual_rect.top)
1665 / (class->max - class->min);
1669 break;
1672 if (thread_data->warp_serial)
1674 if ((long)(xev->serial - thread_data->warp_serial) < 0)
1676 TRACE( "pos %d,%d old serial %lu, ignoring\n", input.u.mi.dx, input.u.mi.dy, xev->serial );
1677 return;
1679 thread_data->warp_serial = 0; /* we caught up now */
1682 TRACE( "pos %d,%d (event %f,%f)\n", input.u.mi.dx, input.u.mi.dy, dx, dy );
1684 input.type = INPUT_MOUSE;
1685 __wine_send_input( 0, &input );
1688 #endif /* HAVE_X11_EXTENSIONS_XINPUT2_H */
1691 /***********************************************************************
1692 * X11DRV_XInput2_Init
1694 void X11DRV_XInput2_Init(void)
1696 #if defined(SONAME_LIBXI) && defined(HAVE_X11_EXTENSIONS_XINPUT2_H)
1697 int event, error;
1698 void *libxi_handle = wine_dlopen( SONAME_LIBXI, RTLD_NOW, NULL, 0 );
1700 if (!libxi_handle)
1702 WARN( "couldn't load %s\n", SONAME_LIBXI );
1703 return;
1705 #define LOAD_FUNCPTR(f) \
1706 if (!(p##f = wine_dlsym( libxi_handle, #f, NULL, 0))) \
1708 WARN("Failed to load %s.\n", #f); \
1709 return; \
1712 LOAD_FUNCPTR(XIFreeDeviceInfo);
1713 LOAD_FUNCPTR(XIQueryDevice);
1714 LOAD_FUNCPTR(XIQueryVersion);
1715 LOAD_FUNCPTR(XISelectEvents);
1716 #undef LOAD_FUNCPTR
1718 xinput2_available = XQueryExtension( gdi_display, "XInputExtension", &xinput2_opcode, &event, &error );
1719 #else
1720 TRACE( "X Input 2 support not compiled in.\n" );
1721 #endif
1725 /***********************************************************************
1726 * X11DRV_GenericEvent
1728 void X11DRV_GenericEvent( HWND hwnd, XEvent *xev )
1730 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1731 XGenericEventCookie *event = &xev->xcookie;
1733 if (!event->data) return;
1734 if (event->extension != xinput2_opcode) return;
1736 switch (event->evtype)
1738 case XI_RawMotion:
1739 X11DRV_RawMotion( event );
1740 break;
1742 default:
1743 TRACE( "Unhandled event %#x\n", event->evtype );
1744 break;
1746 #endif