b344335f485aca6f6b37cfde5099c556f7dbdd73
[wine/hacks.git] / dlls / winex11.drv / mouse.c
blobb344335f485aca6f6b37cfde5099c556f7dbdd73
1 /*
2 * X11 mouse driver
4 * Copyright 1998 Ulrich Weigand
5 * Copyright 2007 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <X11/Xlib.h>
26 #include <stdarg.h>
28 #ifdef SONAME_LIBXCURSOR
29 # include <X11/Xcursor/Xcursor.h>
30 static void *xcursor_handle;
31 # define MAKE_FUNCPTR(f) static typeof(f) * p##f
32 MAKE_FUNCPTR(XcursorImageCreate);
33 MAKE_FUNCPTR(XcursorImageDestroy);
34 MAKE_FUNCPTR(XcursorImageLoadCursor);
35 MAKE_FUNCPTR(XcursorImagesCreate);
36 MAKE_FUNCPTR(XcursorImagesDestroy);
37 MAKE_FUNCPTR(XcursorImagesLoadCursor);
38 # undef MAKE_FUNCPTR
39 #endif /* SONAME_LIBXCURSOR */
41 #define NONAMELESSUNION
42 #define NONAMELESSSTRUCT
43 #include "windef.h"
44 #include "winbase.h"
45 #include "wine/winuser16.h"
47 #include "win.h"
48 #include "x11drv.h"
49 #include "wine/server.h"
50 #include "wine/library.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
55 /**********************************************************************/
57 #ifndef Button6Mask
58 #define Button6Mask (1<<13)
59 #endif
60 #ifndef Button7Mask
61 #define Button7Mask (1<<14)
62 #endif
64 #define NB_BUTTONS 7 /* Windows can handle 5 buttons and the wheel too */
66 static const UINT button_down_flags[NB_BUTTONS] =
68 MOUSEEVENTF_LEFTDOWN,
69 MOUSEEVENTF_MIDDLEDOWN,
70 MOUSEEVENTF_RIGHTDOWN,
71 MOUSEEVENTF_WHEEL,
72 MOUSEEVENTF_WHEEL,
73 MOUSEEVENTF_XDOWN,
74 MOUSEEVENTF_XDOWN
77 static const UINT button_up_flags[NB_BUTTONS] =
79 MOUSEEVENTF_LEFTUP,
80 MOUSEEVENTF_MIDDLEUP,
81 MOUSEEVENTF_RIGHTUP,
84 MOUSEEVENTF_XUP,
85 MOUSEEVENTF_XUP
88 POINT cursor_pos;
89 static DWORD last_time_modified;
90 static RECT cursor_clip; /* Cursor clipping rect */
92 BOOL X11DRV_SetCursorPos( INT x, INT y );
95 /***********************************************************************
96 * X11DRV_Xcursor_Init
98 * Load the Xcursor library for use.
100 void X11DRV_Xcursor_Init(void)
102 #ifdef SONAME_LIBXCURSOR
103 xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
104 if (!xcursor_handle) /* wine_dlopen failed. */
106 WARN("Xcursor failed to load. Using fallback code.\n");
107 return;
109 #define LOAD_FUNCPTR(f) \
110 p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)
112 LOAD_FUNCPTR(XcursorImageCreate);
113 LOAD_FUNCPTR(XcursorImageDestroy);
114 LOAD_FUNCPTR(XcursorImageLoadCursor);
115 LOAD_FUNCPTR(XcursorImagesCreate);
116 LOAD_FUNCPTR(XcursorImagesDestroy);
117 LOAD_FUNCPTR(XcursorImagesLoadCursor);
118 #undef LOAD_FUNCPTR
119 #endif /* SONAME_LIBXCURSOR */
123 /***********************************************************************
124 * get_coords
126 * get the coordinates of a mouse event
128 static inline void get_coords( HWND hwnd, int x, int y, POINT *pt )
130 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
132 if (!data) return;
134 pt->x = x + data->whole_rect.left;
135 pt->y = y + data->whole_rect.top;
138 /***********************************************************************
139 * clip_point_to_rect
141 * Clip point to the provided rectangle
143 static inline void clip_point_to_rect( LPCRECT rect, LPPOINT pt )
145 if (pt->x < rect->left) pt->x = rect->left;
146 else if (pt->x >= rect->right) pt->x = rect->right - 1;
147 if (pt->y < rect->top) pt->y = rect->top;
148 else if (pt->y >= rect->bottom) pt->y = rect->bottom - 1;
151 /***********************************************************************
152 * update_button_state
154 * Update the button state with what X provides us
156 static inline void update_button_state( unsigned int state )
158 key_state_table[VK_LBUTTON] = (state & Button1Mask ? 0x80 : 0);
159 key_state_table[VK_MBUTTON] = (state & Button2Mask ? 0x80 : 0);
160 key_state_table[VK_RBUTTON] = (state & Button3Mask ? 0x80 : 0);
161 /* X-buttons are not reported from XQueryPointer */
165 /***********************************************************************
166 * update_mouse_state
168 * Update the various window states on a mouse event.
170 static void update_mouse_state( HWND hwnd, Window window, int x, int y, unsigned int state, POINT *pt )
172 struct x11drv_thread_data *data = x11drv_thread_data();
174 if (window == root_window)
176 x += virtual_screen_rect.left;
177 y += virtual_screen_rect.top;
179 get_coords( hwnd, x, y, pt );
181 /* update the cursor */
183 if (data->cursor_window != window)
185 data->cursor_window = window;
186 wine_tsx11_lock();
187 if (data->cursor) XDefineCursor( data->display, window, data->cursor );
188 wine_tsx11_unlock();
191 /* update the wine server Z-order */
193 if (window != data->grab_window &&
194 /* ignore event if a button is pressed, since the mouse is then grabbed too */
195 !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
197 SERVER_START_REQ( update_window_zorder )
199 req->window = hwnd;
200 req->rect.left = pt->x;
201 req->rect.top = pt->y;
202 req->rect.right = pt->x + 1;
203 req->rect.bottom = pt->y + 1;
204 wine_server_call( req );
206 SERVER_END_REQ;
211 /***********************************************************************
212 * get_key_state
214 static WORD get_key_state(void)
216 WORD ret = 0;
218 if (GetSystemMetrics( SM_SWAPBUTTON ))
220 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
221 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
223 else
225 if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
226 if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
228 if (key_state_table[VK_MBUTTON] & 0x80) ret |= MK_MBUTTON;
229 if (key_state_table[VK_SHIFT] & 0x80) ret |= MK_SHIFT;
230 if (key_state_table[VK_CONTROL] & 0x80) ret |= MK_CONTROL;
231 if (key_state_table[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
232 if (key_state_table[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
233 return ret;
237 /***********************************************************************
238 * queue_raw_mouse_message
240 static void queue_raw_mouse_message( UINT message, HWND hwnd, DWORD x, DWORD y,
241 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
243 MSLLHOOKSTRUCT hook;
245 hook.pt.x = x;
246 hook.pt.y = y;
247 hook.mouseData = MAKELONG( 0, data );
248 hook.flags = injected_flags;
249 hook.time = time;
250 hook.dwExtraInfo = extra_info;
252 last_time_modified = GetTickCount();
253 if (HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE )) return;
255 SERVER_START_REQ( send_hardware_message )
257 req->id = (injected_flags & LLMHF_INJECTED) ? 0 : GetCurrentThreadId();
258 req->win = hwnd;
259 req->msg = message;
260 req->wparam = MAKEWPARAM( get_key_state(), data );
261 req->lparam = 0;
262 req->x = x;
263 req->y = y;
264 req->time = time;
265 req->info = extra_info;
266 wine_server_call( req );
268 SERVER_END_REQ;
273 /***********************************************************************
274 * X11DRV_send_mouse_input
276 void X11DRV_send_mouse_input( HWND hwnd, DWORD flags, DWORD x, DWORD y,
277 DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
279 POINT pt;
281 if (flags & MOUSEEVENTF_MOVE && flags & MOUSEEVENTF_ABSOLUTE)
283 if (injected_flags & LLMHF_INJECTED)
285 pt.x = (x * screen_width) >> 16;
286 pt.y = (y * screen_height) >> 16;
288 else
290 pt.x = x;
291 pt.y = y;
292 wine_tsx11_lock();
293 if (cursor_pos.x == x && cursor_pos.y == y) flags &= ~MOUSEEVENTF_MOVE;
294 wine_tsx11_unlock();
297 else if (flags & MOUSEEVENTF_MOVE)
299 int accel[3], xMult = 1, yMult = 1;
301 /* dx and dy can be negative numbers for relative movements */
302 SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
304 if (abs(x) > accel[0] && accel[2] != 0)
306 xMult = 2;
307 if ((abs(x) > accel[1]) && (accel[2] == 2)) xMult = 4;
309 if (abs(y) > accel[0] && accel[2] != 0)
311 yMult = 2;
312 if ((abs(y) > accel[1]) && (accel[2] == 2)) yMult = 4;
315 wine_tsx11_lock();
316 pt.x = cursor_pos.x + (long)x * xMult;
317 pt.y = cursor_pos.y + (long)y * yMult;
318 wine_tsx11_unlock();
320 else
322 wine_tsx11_lock();
323 pt = cursor_pos;
324 wine_tsx11_unlock();
327 if (flags & MOUSEEVENTF_MOVE)
329 queue_raw_mouse_message( WM_MOUSEMOVE, hwnd, pt.x, pt.y, data, time,
330 extra_info, injected_flags );
331 if ((injected_flags & LLMHF_INJECTED) &&
332 ((flags & MOUSEEVENTF_ABSOLUTE) || x || y)) /* we have to actually move the cursor */
334 X11DRV_SetCursorPos( pt.x, pt.y );
336 else
338 wine_tsx11_lock();
339 clip_point_to_rect( &cursor_clip, &pt);
340 cursor_pos = pt;
341 wine_tsx11_unlock();
344 if (flags & MOUSEEVENTF_LEFTDOWN)
346 key_state_table[VK_LBUTTON] |= 0xc0;
347 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
348 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
350 if (flags & MOUSEEVENTF_LEFTUP)
352 key_state_table[VK_LBUTTON] &= ~0x80;
353 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONUP : WM_LBUTTONUP,
354 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
356 if (flags & MOUSEEVENTF_RIGHTDOWN)
358 key_state_table[VK_RBUTTON] |= 0xc0;
359 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
360 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
362 if (flags & MOUSEEVENTF_RIGHTUP)
364 key_state_table[VK_RBUTTON] &= ~0x80;
365 queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONUP : WM_RBUTTONUP,
366 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
368 if (flags & MOUSEEVENTF_MIDDLEDOWN)
370 key_state_table[VK_MBUTTON] |= 0xc0;
371 queue_raw_mouse_message( WM_MBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
372 extra_info, injected_flags );
374 if (flags & MOUSEEVENTF_MIDDLEUP)
376 key_state_table[VK_MBUTTON] &= ~0x80;
377 queue_raw_mouse_message( WM_MBUTTONUP, hwnd, pt.x, pt.y, data, time,
378 extra_info, injected_flags );
380 if (flags & MOUSEEVENTF_WHEEL)
382 queue_raw_mouse_message( WM_MOUSEWHEEL, hwnd, pt.x, pt.y, data, time,
383 extra_info, injected_flags );
385 if (flags & MOUSEEVENTF_XDOWN)
387 key_state_table[VK_XBUTTON1 + data - 1] |= 0xc0;
388 queue_raw_mouse_message( WM_XBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
389 extra_info, injected_flags );
391 if (flags & MOUSEEVENTF_XUP)
393 key_state_table[VK_XBUTTON1 + data - 1] &= ~0x80;
394 queue_raw_mouse_message( WM_XBUTTONUP, hwnd, pt.x, pt.y, data, time,
395 extra_info, injected_flags );
400 #ifdef SONAME_LIBXCURSOR
402 /***********************************************************************
403 * create_cursor_image
405 * Create an XcursorImage from a cursor_frame_t
407 static XcursorImage *create_cursor_image( cursor_frame_t *frame )
409 int x, xmax;
410 int y, ymax;
411 int and_size, xor_size;
412 unsigned char *and_bits, *and_ptr, *xor_bits, *xor_ptr;
413 XcursorPixel *pixel_ptr;
414 XcursorImage *image;
416 ymax = (frame->height > 32) ? 32 : frame->height;
417 xmax = (frame->width > 32) ? 32 : frame->width;
419 and_size = frame->and_width_bytes * frame->height;
420 and_ptr = and_bits = frame->bits;
422 xor_size = frame->xor_width_bytes * frame->height;
423 xor_ptr = xor_bits = frame->bits + and_size;
425 image = pXcursorImageCreate( xmax, ymax );
426 pixel_ptr = image->pixels;
428 /* On windows, to calculate the color for a pixel, first an AND is done
429 * with the background and the "and" bitmap, then an XOR with the "xor"
430 * bitmap. This means that when the data in the "and" bitmap is 0, the
431 * pixel will get the color as specified in the "xor" bitmap.
432 * However, if the data in the "and" bitmap is 1, the result will be the
433 * background XOR'ed with the value in the "xor" bitmap. In case the "xor"
434 * data is completely black (0x000000) the pixel will become transparent,
435 * in case it's white (0xffffff) the pixel will become the inverse of the
436 * background color.
438 * Since we can't support inverting colors, we map the grayscale value of
439 * the "xor" data to the alpha channel, and xor the the color with either
440 * black or white.
442 for (y = 0; y < ymax; ++y)
444 and_ptr = and_bits + (y * frame->and_width_bytes);
445 xor_ptr = xor_bits + (y * frame->xor_width_bytes);
447 for (x = 0; x < xmax; ++x)
449 /* Xcursor pixel data is in ARGB format, with A in the high byte */
450 switch (frame->bpp)
452 case 32:
453 /* BGRA, 8 bits each */
454 *pixel_ptr = *xor_ptr++;
455 *pixel_ptr |= *xor_ptr++ << 8;
456 *pixel_ptr |= *xor_ptr++ << 16;
457 *pixel_ptr |= *xor_ptr++ << 24;
458 break;
460 case 24:
461 /* BGR, 8 bits each */
462 *pixel_ptr = *xor_ptr++;
463 *pixel_ptr |= *xor_ptr++ << 8;
464 *pixel_ptr |= *xor_ptr++ << 16;
465 break;
467 case 16:
468 /* BGR, 5 red, 6 green, 5 blue */
469 *pixel_ptr = *xor_ptr * 0x1f;
470 *pixel_ptr |= (*xor_ptr & 0xe0) << 3;
471 ++xor_ptr;
472 *pixel_ptr |= (*xor_ptr & 0x07) << 11;
473 *pixel_ptr |= (*xor_ptr & 0xf8) << 13;
474 break;
476 case 1:
477 if (*xor_ptr & (1 << (7 - (x & 7)))) *pixel_ptr = 0xffffff;
478 else *pixel_ptr = 0;
479 if ((x & 7) == 7) ++xor_ptr;
480 break;
482 default:
483 FIXME("Currently no support for cursors with %d bits per pixel\n", frame->bpp);
484 return 0;
487 if (frame->bpp != 32)
489 /* Alpha channel */
490 if (~*and_ptr & (1 << (7 - (x & 7)))) *pixel_ptr |= 0xff << 24;
491 else if (*pixel_ptr)
493 int alpha = (*pixel_ptr & 0xff) * 0.30f
494 + ((*pixel_ptr & 0xff00) >> 8) * 0.55f
495 + ((*pixel_ptr & 0xff0000) >> 16) * 0.15f;
496 *pixel_ptr ^= ((x + y) % 2) ? 0xffffff : 0x000000;
497 *pixel_ptr |= alpha << 24;
499 if ((x & 7) == 7) ++and_ptr;
501 ++pixel_ptr;
505 return image;
509 /***********************************************************************
510 * create_xcursor_cursor
512 * Use Xcursor to create an X cursor from a Windows one.
514 static Cursor create_xcursor_cursor( Display *display, const cursor_t *cursor_object )
516 unsigned int i;
517 Cursor cursor;
518 XcursorImage *image;
519 XcursorImages *images;
521 if (!cursor_object) /* Create an empty cursor */
523 image = pXcursorImageCreate( 1, 1 );
524 image->xhot = 0;
525 image->yhot = 0;
526 *(image->pixels) = 0;
527 cursor = pXcursorImageLoadCursor( display, image );
528 pXcursorImageDestroy( image );
530 return cursor;
533 images = pXcursorImagesCreate( cursor_object->num_frames );
534 for (i = 0; i < cursor_object->num_frames; ++i)
536 cursor_frame_t *frame = &cursor_object->frames[i];
537 image = create_cursor_image( frame );
538 if (!image) return 0;
540 /* Make sure hotspot is valid */
541 image->xhot = frame->xhot;
542 image->yhot = frame->yhot;
543 if (image->xhot < 0 || image->xhot >= image->width ||
544 image->yhot < 0 || image->yhot >= image->height)
546 image->xhot = image->width / 2;
547 image->yhot = image->height / 2;
550 image->delay = cursor_object->delay;
552 images->images[images->nimage++] = image;
555 cursor = pXcursorImagesLoadCursor( display, images );
556 pXcursorImagesDestroy( images );
558 return cursor;
561 #endif /* SONAME_LIBXCURSOR */
564 /***********************************************************************
565 * create_cursor
567 * Create an X cursor from a Windows one.
569 static Cursor create_cursor( Display *display, const cursor_t *ptr )
571 Pixmap pixmapBits, pixmapMask, pixmapMaskInv = 0, pixmapAll;
572 XColor fg, bg;
573 Cursor cursor = None;
574 POINT hotspot;
575 char *bitMask32 = NULL;
577 #ifdef SONAME_LIBXCURSOR
578 if (pXcursorImageLoadCursor) return create_xcursor_cursor( display, ptr );
579 #endif
581 if (!ptr) /* Create an empty cursor */
583 static const char data[] = { 0 };
585 bg.red = bg.green = bg.blue = 0x0000;
586 pixmapBits = XCreateBitmapFromData( display, root_window, data, 1, 1 );
587 if (pixmapBits)
589 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
590 &bg, &bg, 0, 0 );
591 XFreePixmap( display, pixmapBits );
594 else /* Create the X cursor from the bits */
596 cursor_frame_t *frame = &ptr->frames[0];
597 XImage *image;
598 GC gc;
600 TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
601 frame->width, frame->height, frame->planes, frame->bpp,
602 frame->xor_width_bytes);
604 /* Create a pixmap and transfer all the bits to it */
606 /* NOTE: Following hack works, but only because XFree depth
607 * 1 images really use 1 bit/pixel (and so the same layout
608 * as the Windows cursor data). Perhaps use a more generic
609 * algorithm here.
611 /* This pixmap will be written with two bitmaps. The first is
612 * the mask and the second is the image.
614 if (!(pixmapAll = XCreatePixmap( display, root_window,
615 frame->width, frame->height * 2, 1 )))
616 return 0;
617 if (!(image = XCreateImage( display, visual,
618 1, ZPixmap, 0, (char *)frame->bits, frame->width,
619 frame->height * 2, 16, frame->xor_width_bytes/frame->bpp)))
621 XFreePixmap( display, pixmapAll );
622 return 0;
624 gc = XCreateGC( display, pixmapAll, 0, NULL );
625 XSetGraphicsExposures( display, gc, False );
626 image->byte_order = MSBFirst;
627 image->bitmap_bit_order = MSBFirst;
628 image->bitmap_unit = 16;
629 _XInitImageFuncPtrs(image);
630 if (frame->planes * frame->bpp == 1)
632 /* A plain old white on black cursor. */
633 fg.red = fg.green = fg.blue = 0xffff;
634 bg.red = bg.green = bg.blue = 0x0000;
635 XPutImage( display, pixmapAll, gc, image,
636 0, 0, 0, 0, frame->width, frame->height * 2 );
638 else
640 int rbits, gbits, bbits, red, green, blue;
641 int rfg, gfg, bfg, rbg, gbg, bbg;
642 int rscale, gscale, bscale;
643 int x, y, xmax, ymax, bitIndex, byteIndex, xorIndex;
644 unsigned char *theMask, *theImage, theChar;
645 int threshold, fgBits, bgBits, bitShifted;
646 BYTE pXorBits[128]; /* Up to 32x32 icons */
648 switch (frame->bpp)
650 case 32:
651 bitMask32 = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
652 frame->width * frame->height / 8 );
653 /* Fallthrough */
654 case 24:
655 rbits = 8;
656 gbits = 8;
657 bbits = 8;
658 threshold = 0x40;
659 break;
660 case 16:
661 rbits = 5;
662 gbits = 6;
663 bbits = 5;
664 threshold = 0x40;
665 break;
666 default:
667 FIXME("Currently no support for cursors with %d bits per pixel\n",
668 frame->bpp);
669 XFreePixmap( display, pixmapAll );
670 XFreeGC( display, gc );
671 image->data = NULL;
672 XDestroyImage( image );
673 return 0;
675 /* The location of the mask. */
676 theMask = frame->bits;
677 /* The mask should still be 1 bit per pixel. The color image
678 * should immediately follow the mask.
680 theImage = &theMask[frame->width/8 * frame->height];
681 rfg = gfg = bfg = rbg = gbg = bbg = 0;
682 bitIndex = 0;
683 byteIndex = 0;
684 xorIndex = 0;
685 fgBits = 0;
686 bitShifted = 0x01;
687 xmax = (frame->width > 32) ? 32 : frame->width;
688 if (frame->width > 32) {
689 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
690 frame->width, frame->height);
692 ymax = (frame->height > 32) ? 32 : frame->height;
694 memset(pXorBits, 0, 128);
695 for (y=0; y<ymax; y++)
697 for (x=0; x<xmax; x++)
699 red = green = blue = 0;
700 switch (frame->bpp)
702 case 32:
703 theChar = theImage[byteIndex++];
704 blue = theChar;
705 theChar = theImage[byteIndex++];
706 green = theChar;
707 theChar = theImage[byteIndex++];
708 red = theChar;
709 theChar = theImage[byteIndex++];
710 /* If the alpha channel is >5% transparent,
711 * assume that we can add it to the bitMask32.
713 if (theChar > 0x0D)
714 *(bitMask32 + (y*xmax+x)/8) |= 1 << (x & 7);
715 break;
716 case 24:
717 theChar = theImage[byteIndex++];
718 blue = theChar;
719 theChar = theImage[byteIndex++];
720 green = theChar;
721 theChar = theImage[byteIndex++];
722 red = theChar;
723 break;
724 case 16:
725 theChar = theImage[byteIndex++];
726 blue = theChar & 0x1F;
727 green = (theChar & 0xE0) >> 5;
728 theChar = theImage[byteIndex++];
729 green |= (theChar & 0x07) << 3;
730 red = (theChar & 0xF8) >> 3;
731 break;
734 if (red+green+blue > threshold)
736 rfg += red;
737 gfg += green;
738 bfg += blue;
739 fgBits++;
740 pXorBits[xorIndex] |= bitShifted;
742 else
744 rbg += red;
745 gbg += green;
746 bbg += blue;
748 if (x%8 == 7)
750 bitShifted = 0x01;
751 xorIndex++;
753 else
754 bitShifted = bitShifted << 1;
757 rscale = 1 << (16 - rbits);
758 gscale = 1 << (16 - gbits);
759 bscale = 1 << (16 - bbits);
760 if (fgBits)
762 fg.red = rfg * rscale / fgBits;
763 fg.green = gfg * gscale / fgBits;
764 fg.blue = bfg * bscale / fgBits;
766 else fg.red = fg.green = fg.blue = 0;
767 bgBits = xmax * ymax - fgBits;
768 if (bgBits)
770 bg.red = rbg * rscale / bgBits;
771 bg.green = gbg * gscale / bgBits;
772 bg.blue = bbg * bscale / bgBits;
774 else bg.red = bg.green = bg.blue = 0;
775 pixmapBits = XCreateBitmapFromData( display, root_window, (char *)pXorBits, xmax, ymax );
776 if (!pixmapBits)
778 XFreePixmap( display, pixmapAll );
779 XFreeGC( display, gc );
780 image->data = NULL;
781 XDestroyImage( image );
782 return 0;
785 /* Put the mask. */
786 XPutImage( display, pixmapAll, gc, image,
787 0, 0, 0, 0, frame->width, frame->height );
788 XSetFunction( display, gc, GXcopy );
789 /* Put the image */
790 XCopyArea( display, pixmapBits, pixmapAll, gc,
791 0, 0, xmax, ymax, 0, frame->height );
792 XFreePixmap( display, pixmapBits );
794 image->data = NULL;
795 XDestroyImage( image );
797 /* Now create the 2 pixmaps for bits and mask */
799 pixmapBits = XCreatePixmap( display, root_window, frame->width, frame->height, 1 );
800 if (frame->bpp != 32)
802 pixmapMaskInv = XCreatePixmap( display, root_window, frame->width, frame->height, 1 );
803 pixmapMask = XCreatePixmap( display, root_window, frame->width, frame->height, 1 );
805 /* Make sure everything went OK so far */
806 if (pixmapBits && pixmapMask && pixmapMaskInv)
808 /* We have to do some magic here, as cursors are not fully
809 * compatible between Windows and X11. Under X11, there are
810 * only 3 possible color cursor: black, white and masked. So
811 * we map the 4th Windows color (invert the bits on the screen)
812 * to black and an additional white bit on an other place
813 * (+1,+1). This require some boolean arithmetic:
815 * Windows | X11
816 * And Xor Result | Bits Mask Result
817 * 0 0 black | 0 1 background
818 * 0 1 white | 1 1 foreground
819 * 1 0 no change | X 0 no change
820 * 1 1 inverted | 0 1 background
822 * which gives:
823 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
824 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
826 * FIXME: apparently some servers do support 'inverted' color.
827 * I don't know if it's correct per the X spec, but maybe we
828 * ought to take advantage of it. -- AJ
830 XSetFunction( display, gc, GXcopy );
831 XCopyArea( display, pixmapAll, pixmapBits, gc,
832 0, 0, frame->width, frame->height, 0, 0 );
833 XCopyArea( display, pixmapAll, pixmapMask, gc,
834 0, 0, frame->width, frame->height, 0, 0 );
835 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
836 0, 0, frame->width, frame->height, 0, 0 );
837 XSetFunction( display, gc, GXand );
838 XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
839 0, frame->height, frame->width, frame->height, 0, 0 );
840 XSetFunction( display, gc, GXandReverse );
841 XCopyArea( display, pixmapAll, pixmapBits, gc,
842 0, frame->height, frame->width, frame->height, 0, 0 );
843 XSetFunction( display, gc, GXorReverse );
844 XCopyArea( display, pixmapAll, pixmapMask, gc,
845 0, frame->height, frame->width, frame->height, 0, 0 );
846 /* Additional white */
847 XSetFunction( display, gc, GXor );
848 XCopyArea( display, pixmapMaskInv, pixmapMask, gc,
849 0, 0, frame->width, frame->height, 1, 1 );
850 XCopyArea( display, pixmapMaskInv, pixmapBits, gc,
851 0, 0, frame->width, frame->height, 1, 1 );
852 XSetFunction( display, gc, GXcopy );
855 else
857 pixmapMask = XCreateBitmapFromData( display, root_window,
858 bitMask32, frame->width,
859 frame->height );
860 HeapFree( GetProcessHeap(), 0, bitMask32 );
863 /* Make sure hotspot is valid */
864 hotspot.x = frame->xhot;
865 hotspot.y = frame->yhot;
866 if (hotspot.x < 0 || hotspot.x >= frame->width ||
867 hotspot.y < 0 || hotspot.y >= frame->height)
869 hotspot.x = frame->width / 2;
870 hotspot.y = frame->height / 2;
873 if (pixmapBits && pixmapMask)
874 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
875 &fg, &bg, hotspot.x, hotspot.y );
877 /* Now free everything */
879 if (pixmapAll) XFreePixmap( display, pixmapAll );
880 if (pixmapBits) XFreePixmap( display, pixmapBits );
881 if (pixmapMask) XFreePixmap( display, pixmapMask );
882 if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
883 XFreeGC( display, gc );
885 return cursor;
889 /***********************************************************************
890 * SetCursor (X11DRV.@)
892 void X11DRV_SetCursor( const cursor_t *cursor_object )
894 struct x11drv_thread_data *data = x11drv_thread_data();
895 Cursor cursor;
897 if (cursor_object)
899 unsigned int i;
900 for (i = 0; i < cursor_object->num_frames; ++i)
902 cursor_frame_t *frame = cursor_object->frames + i;
903 TRACE("frame %u, %ux%u, planes %u, bpp %u\n",
904 i, frame->width, frame->height, frame->planes, frame->bpp);
906 } else TRACE("NULL\n");
908 /* set the same cursor for all top-level windows of the current thread */
910 wine_tsx11_lock();
911 cursor = create_cursor( data->display, cursor_object );
912 if (cursor)
914 if (data->cursor) XFreeCursor( data->display, data->cursor );
915 data->cursor = cursor;
916 if (data->cursor_window)
918 XDefineCursor( data->display, data->cursor_window, cursor );
919 /* Make the change take effect immediately */
920 XFlush( data->display );
923 wine_tsx11_unlock();
926 /***********************************************************************
927 * SetCursorPos (X11DRV.@)
929 BOOL X11DRV_SetCursorPos( INT x, INT y )
931 Display *display = thread_display();
932 POINT pt;
934 TRACE( "warping to (%d,%d)\n", x, y );
936 wine_tsx11_lock();
937 if (cursor_pos.x == x && cursor_pos.y == y)
939 wine_tsx11_unlock();
940 /* We still need to generate WM_MOUSEMOVE */
941 queue_raw_mouse_message( WM_MOUSEMOVE, NULL, x, y, 0, GetCurrentTime(), 0, 0 );
942 return TRUE;
945 pt.x = x; pt.y = y;
946 clip_point_to_rect( &cursor_clip, &pt);
947 XWarpPointer( display, root_window, root_window, 0, 0, 0, 0,
948 pt.x - virtual_screen_rect.left, pt.y - virtual_screen_rect.top );
949 XFlush( display ); /* avoids bad mouse lag in games that do their own mouse warping */
950 cursor_pos = pt;
951 wine_tsx11_unlock();
952 return TRUE;
955 /***********************************************************************
956 * GetCursorPos (X11DRV.@)
958 BOOL X11DRV_GetCursorPos(LPPOINT pos)
960 Display *display = thread_display();
961 Window root, child;
962 int rootX, rootY, winX, winY;
963 unsigned int xstate;
965 wine_tsx11_lock();
966 if ((GetTickCount() - last_time_modified > 100) &&
967 XQueryPointer( display, root_window, &root, &child,
968 &rootX, &rootY, &winX, &winY, &xstate ))
970 update_button_state( xstate );
971 winX += virtual_screen_rect.left;
972 winY += virtual_screen_rect.top;
973 TRACE("pointer at (%d,%d)\n", winX, winY );
974 cursor_pos.x = winX;
975 cursor_pos.y = winY;
977 *pos = cursor_pos;
978 wine_tsx11_unlock();
979 return TRUE;
983 /***********************************************************************
984 * ClipCursor (X11DRV.@)
986 * Set the cursor clipping rectangle.
988 BOOL X11DRV_ClipCursor( LPCRECT clip )
990 if (!IntersectRect( &cursor_clip, &virtual_screen_rect, clip ))
991 cursor_clip = virtual_screen_rect;
993 return TRUE;
996 /***********************************************************************
997 * X11DRV_ButtonPress
999 void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1001 XButtonEvent *event = &xev->xbutton;
1002 int buttonNum = event->button - 1;
1003 WORD wData = 0;
1004 POINT pt;
1006 if (buttonNum >= NB_BUTTONS) return;
1007 if (!hwnd) return;
1009 switch (buttonNum)
1011 case 3:
1012 wData = WHEEL_DELTA;
1013 break;
1014 case 4:
1015 wData = -WHEEL_DELTA;
1016 break;
1017 case 5:
1018 wData = XBUTTON1;
1019 break;
1020 case 6:
1021 wData = XBUTTON2;
1022 break;
1025 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1027 X11DRV_send_mouse_input( hwnd, button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1028 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1032 /***********************************************************************
1033 * X11DRV_ButtonRelease
1035 void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1037 XButtonEvent *event = &xev->xbutton;
1038 int buttonNum = event->button - 1;
1039 WORD wData = 0;
1040 POINT pt;
1042 if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1043 if (!hwnd) return;
1045 switch (buttonNum)
1047 case 5:
1048 wData = XBUTTON1;
1049 break;
1050 case 6:
1051 wData = XBUTTON2;
1052 break;
1055 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1057 X11DRV_send_mouse_input( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1058 pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1062 /***********************************************************************
1063 * X11DRV_MotionNotify
1065 void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1067 XMotionEvent *event = &xev->xmotion;
1068 POINT pt;
1070 TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);
1072 if (!hwnd) return;
1074 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1076 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1077 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1081 /***********************************************************************
1082 * X11DRV_EnterNotify
1084 void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1086 XCrossingEvent *event = &xev->xcrossing;
1087 POINT pt;
1089 TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);
1091 if (!hwnd) return;
1092 if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1094 /* simulate a mouse motion event */
1095 update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );
1097 X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
1098 pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );