mouse.c 38.3 KB
Newer Older
1
/*
2
 * X11 mouse driver
3
 *
4
 * Copyright 1998 Ulrich Weigand
5
 * Copyright 2007 Henri Verbeet
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
20 21 22
 */

#include "config.h"
23
#include "wine/port.h"
24

25
#include <X11/Xlib.h>
26
#include <stdarg.h>
27

28
#ifdef SONAME_LIBXCURSOR
29 30 31 32 33 34 35
# include <X11/Xcursor/Xcursor.h>
static void *xcursor_handle;
# define MAKE_FUNCPTR(f) static typeof(f) * p##f
MAKE_FUNCPTR(XcursorImageCreate);
MAKE_FUNCPTR(XcursorImageDestroy);
MAKE_FUNCPTR(XcursorImageLoadCursor);
# undef MAKE_FUNCPTR
36
#endif /* SONAME_LIBXCURSOR */
37

38 39
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
40
#include "windef.h"
41
#include "winbase.h"
42 43
#include "wine/winuser16.h"

44
#include "x11drv.h"
45
#include "wine/server.h"
46
#include "wine/library.h"
47
#include "wine/debug.h"
48

49
WINE_DEFAULT_DEBUG_CHANNEL(cursor);
50

51 52
/**********************************************************************/

53 54 55 56 57 58 59
#ifndef Button6Mask
#define Button6Mask (1<<13)
#endif
#ifndef Button7Mask
#define Button7Mask (1<<14)
#endif

60
#define NB_BUTTONS   9     /* Windows can handle 5 buttons and the wheel too */
61 62 63 64 65 66 67

static const UINT button_down_flags[NB_BUTTONS] =
{
    MOUSEEVENTF_LEFTDOWN,
    MOUSEEVENTF_MIDDLEDOWN,
    MOUSEEVENTF_RIGHTDOWN,
    MOUSEEVENTF_WHEEL,
68
    MOUSEEVENTF_WHEEL,
69 70
    MOUSEEVENTF_XDOWN,  /* FIXME: horizontal wheel */
    MOUSEEVENTF_XDOWN,
71 72
    MOUSEEVENTF_XDOWN,
    MOUSEEVENTF_XDOWN
73 74 75 76 77 78 79 80
};

static const UINT button_up_flags[NB_BUTTONS] =
{
    MOUSEEVENTF_LEFTUP,
    MOUSEEVENTF_MIDDLEUP,
    MOUSEEVENTF_RIGHTUP,
    0,
81 82
    0,
    MOUSEEVENTF_XUP,
83 84
    MOUSEEVENTF_XUP,
    MOUSEEVENTF_XUP,
85
    MOUSEEVENTF_XUP
86 87
};

88
POINT cursor_pos;
89
static DWORD last_time_modified;
90
static RECT cursor_clip; /* Cursor clipping rect */
91

92
BOOL CDECL X11DRV_SetCursorPos( INT x, INT y );
93

94 95 96 97 98 99 100 101

/***********************************************************************
 *		X11DRV_Xcursor_Init
 *
 * Load the Xcursor library for use.
 */
void X11DRV_Xcursor_Init(void)
{
102
#ifdef SONAME_LIBXCURSOR
103 104 105 106 107 108 109 110 111 112 113 114 115
    xcursor_handle = wine_dlopen(SONAME_LIBXCURSOR, RTLD_NOW, NULL, 0);
    if (!xcursor_handle)  /* wine_dlopen failed. */
    {
        WARN("Xcursor failed to load.  Using fallback code.\n");
        return;
    }
#define LOAD_FUNCPTR(f) \
        p##f = wine_dlsym(xcursor_handle, #f, NULL, 0)

    LOAD_FUNCPTR(XcursorImageCreate);
    LOAD_FUNCPTR(XcursorImageDestroy);
    LOAD_FUNCPTR(XcursorImageLoadCursor);
#undef LOAD_FUNCPTR
116
#endif /* SONAME_LIBXCURSOR */
117 118 119
}


120 121 122 123 124
/***********************************************************************
 *		get_coords
 *
 * get the coordinates of a mouse event
 */
125
static inline void get_coords( HWND hwnd, Window window, int x, int y, POINT *pt )
126
{
127
    struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
128

129
    if (!data) return;
130

131 132 133 134 135 136 137 138 139 140
    if (window == data->client_window)
    {
        pt->x = x + data->client_rect.left;
        pt->y = y + data->client_rect.top;
    }
    else
    {
        pt->x = x + data->whole_rect.left;
        pt->y = y + data->whole_rect.top;
    }
141 142
}

143 144 145 146 147 148 149 150 151 152 153 154
/***********************************************************************
 *		clip_point_to_rect
 *
 * Clip point to the provided rectangle
 */
static inline void clip_point_to_rect( LPCRECT rect, LPPOINT pt )
{
    if      (pt->x <  rect->left)   pt->x = rect->left;
    else if (pt->x >= rect->right)  pt->x = rect->right - 1;
    if      (pt->y <  rect->top)    pt->y = rect->top;
    else if (pt->y >= rect->bottom) pt->y = rect->bottom - 1;
}
155 156

/***********************************************************************
157
 *		update_button_state
158
 *
159
 * Update the button state with what X provides us
160
 */
161
static inline void update_button_state( unsigned int state )
162
{
163 164 165
    key_state_table[VK_LBUTTON] = (state & Button1Mask ? 0x80 : 0);
    key_state_table[VK_MBUTTON] = (state & Button2Mask ? 0x80 : 0);
    key_state_table[VK_RBUTTON] = (state & Button3Mask ? 0x80 : 0);
166
    /* X-buttons are not reported from XQueryPointer */
167 168 169
}


170 171 172 173 174 175 176 177 178
/***********************************************************************
 *		update_mouse_state
 *
 * Update the various window states on a mouse event.
 */
static void update_mouse_state( HWND hwnd, Window window, int x, int y, unsigned int state, POINT *pt )
{
    struct x11drv_thread_data *data = x11drv_thread_data();

179
    get_coords( hwnd, window, x, y, pt );
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

    /* update the cursor */

    if (data->cursor_window != window)
    {
        data->cursor_window = window;
        wine_tsx11_lock();
        if (data->cursor) XDefineCursor( data->display, window, data->cursor );
        wine_tsx11_unlock();
    }

    /* update the wine server Z-order */

    if (window != data->grab_window &&
        /* ignore event if a button is pressed, since the mouse is then grabbed too */
195
        !(state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask|Button6Mask|Button7Mask)))
196 197 198
    {
        SERVER_START_REQ( update_window_zorder )
        {
199
            req->window      = wine_server_user_handle( hwnd );
200 201 202 203 204 205 206 207 208 209 210
            req->rect.left   = pt->x;
            req->rect.top    = pt->y;
            req->rect.right  = pt->x + 1;
            req->rect.bottom = pt->y + 1;
            wine_server_call( req );
        }
        SERVER_END_REQ;
    }
}


211
/***********************************************************************
212
 *           get_key_state
213
 */
214
static WORD get_key_state(void)
215
{
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    WORD ret = 0;

    if (GetSystemMetrics( SM_SWAPBUTTON ))
    {
        if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
        if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
    }
    else
    {
        if (key_state_table[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
        if (key_state_table[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
    }
    if (key_state_table[VK_MBUTTON] & 0x80)  ret |= MK_MBUTTON;
    if (key_state_table[VK_SHIFT] & 0x80)    ret |= MK_SHIFT;
    if (key_state_table[VK_CONTROL] & 0x80)  ret |= MK_CONTROL;
    if (key_state_table[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
    if (key_state_table[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
    return ret;
}


/***********************************************************************
 *           queue_raw_mouse_message
 */
static void queue_raw_mouse_message( UINT message, HWND hwnd, DWORD x, DWORD y,
                                     DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
{
    MSLLHOOKSTRUCT hook;

    hook.pt.x        = x;
    hook.pt.y        = y;
    hook.mouseData   = MAKELONG( 0, data );
    hook.flags       = injected_flags;
    hook.time        = time;
    hook.dwExtraInfo = extra_info;

252
    last_time_modified = GetTickCount();
253 254
    if (HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE )) return;

255
    SERVER_START_REQ( send_hardware_message )
256
    {
257
        req->id       = (injected_flags & LLMHF_INJECTED) ? 0 : GetCurrentThreadId();
258
        req->win      = wine_server_user_handle( hwnd );
259 260 261 262 263 264 265 266 267 268 269 270
        req->msg      = message;
        req->wparam   = MAKEWPARAM( get_key_state(), data );
        req->lparam   = 0;
        req->x        = x;
        req->y        = y;
        req->time     = time;
        req->info     = extra_info;
        wine_server_call( req );
    }
    SERVER_END_REQ;

}
271

272 273 274 275 276 277 278 279

/***********************************************************************
 *		X11DRV_send_mouse_input
 */
void X11DRV_send_mouse_input( HWND hwnd, DWORD flags, DWORD x, DWORD y,
                              DWORD data, DWORD time, DWORD extra_info, UINT injected_flags )
{
    POINT pt;
280

281
    if (flags & MOUSEEVENTF_MOVE && flags & MOUSEEVENTF_ABSOLUTE)
282
    {
283 284 285 286 287 288 289 290 291
        if (injected_flags & LLMHF_INJECTED)
        {
            pt.x = (x * screen_width) >> 16;
            pt.y = (y * screen_height) >> 16;
        }
        else
        {
            pt.x = x;
            pt.y = y;
292 293 294 295 296
            wine_tsx11_lock();
            if (cursor_pos.x == x && cursor_pos.y == y &&
                (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE)))
                flags &= ~MOUSEEVENTF_MOVE;
            wine_tsx11_unlock();
297 298 299 300 301 302 303 304 305
        }
    }
    else if (flags & MOUSEEVENTF_MOVE)
    {
        int accel[3], xMult = 1, yMult = 1;

        /* dx and dy can be negative numbers for relative movements */
        SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);

306
        if (abs(x) > accel[0] && accel[2] != 0)
307 308
        {
            xMult = 2;
309
            if ((abs(x) > accel[1]) && (accel[2] == 2)) xMult = 4;
310
        }
311
        if (abs(y) > accel[0] && accel[2] != 0)
312 313
        {
            yMult = 2;
314
            if ((abs(y) > accel[1]) && (accel[2] == 2)) yMult = 4;
315 316 317 318 319 320 321 322 323 324 325 326
        }

        wine_tsx11_lock();
        pt.x = cursor_pos.x + (long)x * xMult;
        pt.y = cursor_pos.y + (long)y * yMult;
        wine_tsx11_unlock();
    }
    else
    {
        wine_tsx11_lock();
        pt = cursor_pos;
        wine_tsx11_unlock();
327 328
    }

329 330 331 332
    if (flags & MOUSEEVENTF_MOVE)
    {
        queue_raw_mouse_message( WM_MOUSEMOVE, hwnd, pt.x, pt.y, data, time,
                                 extra_info, injected_flags );
333 334
        if ((injected_flags & LLMHF_INJECTED) &&
            ((flags & MOUSEEVENTF_ABSOLUTE) || x || y))  /* we have to actually move the cursor */
335
        {
336 337 338 339
            X11DRV_SetCursorPos( pt.x, pt.y );
        }
        else
        {
340
            wine_tsx11_lock();
341
            clip_point_to_rect( &cursor_clip, &pt);
342
            cursor_pos = pt;
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
            wine_tsx11_unlock();
        }
    }
    if (flags & MOUSEEVENTF_LEFTDOWN)
    {
        key_state_table[VK_LBUTTON] |= 0xc0;
        queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
                                 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
    }
    if (flags & MOUSEEVENTF_LEFTUP)
    {
        key_state_table[VK_LBUTTON] &= ~0x80;
        queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_RBUTTONUP : WM_LBUTTONUP,
                                 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
    }
    if (flags & MOUSEEVENTF_RIGHTDOWN)
    {
        key_state_table[VK_RBUTTON] |= 0xc0;
        queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
                                 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
    }
    if (flags & MOUSEEVENTF_RIGHTUP)
    {
        key_state_table[VK_RBUTTON] &= ~0x80;
        queue_raw_mouse_message( GetSystemMetrics(SM_SWAPBUTTON) ? WM_LBUTTONUP : WM_RBUTTONUP,
                                 hwnd, pt.x, pt.y, data, time, extra_info, injected_flags );
    }
    if (flags & MOUSEEVENTF_MIDDLEDOWN)
    {
        key_state_table[VK_MBUTTON] |= 0xc0;
        queue_raw_mouse_message( WM_MBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
                                 extra_info, injected_flags );
    }
    if (flags & MOUSEEVENTF_MIDDLEUP)
    {
        key_state_table[VK_MBUTTON] &= ~0x80;
        queue_raw_mouse_message( WM_MBUTTONUP, hwnd, pt.x, pt.y, data, time,
                                 extra_info, injected_flags );
    }
    if (flags & MOUSEEVENTF_WHEEL)
    {
        queue_raw_mouse_message( WM_MOUSEWHEEL, hwnd, pt.x, pt.y, data, time,
                                 extra_info, injected_flags );
    }
387 388 389 390 391 392 393 394 395 396 397 398
    if (flags & MOUSEEVENTF_XDOWN)
    {
        key_state_table[VK_XBUTTON1 + data - 1] |= 0xc0;
        queue_raw_mouse_message( WM_XBUTTONDOWN, hwnd, pt.x, pt.y, data, time,
                                 extra_info, injected_flags );
    }
    if (flags & MOUSEEVENTF_XUP)
    {
        key_state_table[VK_XBUTTON1 + data - 1] &= ~0x80;
        queue_raw_mouse_message( WM_XBUTTONUP, hwnd, pt.x, pt.y, data, time,
                                 extra_info, injected_flags );
    }
399 400
}

401

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
/***********************************************************************
 *              check_alpha_zero
 *
 * Generally 32 bit bitmaps have an alpha channel which is used in favor of the
 * AND mask.  However, if all pixels have alpha = 0x00, the bitmap is treated
 * like one without alpha and the masks are used.  As soon as one pixel has
 * alpha != 0x00, and the mask ignored as described in the docs.
 *
 * This is most likely for applications which create the bitmaps with
 * CreateDIBitmap, which creates a device dependent bitmap, so the format that
 * arrives when loading depends on the screen's bpp.  Apps that were written at
 * 8 / 16 bpp times do not know about the 32 bit alpha, so they would get a
 * completely transparent cursor on 32 bit displays.
 *
 * Non-32 bit bitmaps always use the AND mask.
 */
static BOOL check_alpha_zero(CURSORICONINFO *ptr, unsigned char *xor_bits)
{
    int x, y;
    unsigned char *xor_ptr;

    if (ptr->bBitsPerPixel == 32)
    {
        for (y = 0; y < ptr->nHeight; ++y)
        {
            xor_ptr = xor_bits + (y * ptr->nWidthBytes);
            for (x = 0; x < ptr->nWidth; ++x)
            {
                if (xor_ptr[3] != 0x00)
                {
                    return FALSE;
                }
                xor_ptr+=4;
            }
        }
    }

    return TRUE;
}


443
#ifdef SONAME_LIBXCURSOR
444 445 446 447 448 449 450 451

/***********************************************************************
 *              create_cursor_image
 *
 * Create an XcursorImage from a CURSORICONINFO
 */
static XcursorImage *create_cursor_image( CURSORICONINFO *ptr )
{
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    static const unsigned char convert_5to8[] =
    {
        0x00, 0x08, 0x10, 0x19, 0x21, 0x29, 0x31, 0x3a,
        0x42, 0x4a, 0x52, 0x5a, 0x63, 0x6b, 0x73, 0x7b,
        0x84, 0x8c, 0x94, 0x9c, 0xa5, 0xad, 0xb5, 0xbd,
        0xc5, 0xce, 0xd6, 0xde, 0xe6, 0xef, 0xf7, 0xff,
    };
    static const unsigned char convert_6to8[] =
    {
        0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x18, 0x1c,
        0x20, 0x24, 0x28, 0x2d, 0x31, 0x35, 0x39, 0x3d,
        0x41, 0x45, 0x49, 0x4d, 0x51, 0x55, 0x59, 0x5d,
        0x61, 0x65, 0x69, 0x6d, 0x71, 0x75, 0x79, 0x7d,
        0x82, 0x86, 0x8a, 0x8e, 0x92, 0x96, 0x9a, 0x9e,
        0xa2, 0xa6, 0xaa, 0xae, 0xb2, 0xb6, 0xba, 0xbe,
        0xc2, 0xc6, 0xca, 0xce, 0xd2, 0xd7, 0xdb, 0xdf,
        0xe3, 0xe7, 0xeb, 0xef, 0xf3, 0xf7, 0xfb, 0xff,
    };
470 471
    int x;
    int y;
472
    int and_size;
473 474 475 476
    unsigned char *and_bits, *and_ptr, *xor_bits, *xor_ptr;
    int and_width_bytes, xor_width_bytes;
    XcursorPixel *pixel_ptr;
    XcursorImage *image;
477
    unsigned char tmp;
478
    BOOL alpha_zero;
479

480 481
    and_width_bytes = 2 * ((ptr->nWidth+15) / 16);
    xor_width_bytes = ptr->nWidthBytes;
482

483
    and_size = ptr->nHeight * and_width_bytes;
484 485 486 487
    and_ptr = and_bits = (unsigned char *)(ptr + 1);

    xor_ptr = xor_bits = and_ptr + and_size;

488
    image = pXcursorImageCreate( ptr->nWidth, ptr->nHeight );
489 490
    pixel_ptr = image->pixels;

491
    alpha_zero = check_alpha_zero(ptr, xor_bits);
492

493 494 495 496 497 498 499 500 501 502 503
    /* On windows, to calculate the color for a pixel, first an AND is done
     * with the background and the "and" bitmap, then an XOR with the "xor"
     * bitmap. This means that when the data in the "and" bitmap is 0, the
     * pixel will get the color as specified in the "xor" bitmap.
     * However, if the data in the "and" bitmap is 1, the result will be the
     * background XOR'ed with the value in the "xor" bitmap. In case the "xor"
     * data is completely black (0x000000) the pixel will become transparent,
     * in case it's white (0xffffff) the pixel will become the inverse of the
     * background color.
     *
     * Since we can't support inverting colors, we map the grayscale value of
504
     * the "xor" data to the alpha channel, and xor the color with either
505 506
     * black or white.
     */
507
    for (y = 0; y < ptr->nHeight; ++y)
508 509 510 511
    {
        and_ptr = and_bits + (y * and_width_bytes);
        xor_ptr = xor_bits + (y * xor_width_bytes);

512
        for (x = 0; x < ptr->nWidth; ++x)
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
        {
            /* Xcursor pixel data is in ARGB format, with A in the high byte */
            switch (ptr->bBitsPerPixel)
            {
                case 32:
                    /* BGRA, 8 bits each */
                    *pixel_ptr = *xor_ptr++;
                    *pixel_ptr |= *xor_ptr++ << 8;
                    *pixel_ptr |= *xor_ptr++ << 16;
                    *pixel_ptr |= *xor_ptr++ << 24;
                    break;

                case 24:
                    /* BGR, 8 bits each */
                    *pixel_ptr = *xor_ptr++;
                    *pixel_ptr |= *xor_ptr++ << 8;
                    *pixel_ptr |= *xor_ptr++ << 16;
                    break;

                case 16:
                    /* BGR, 5 red, 6 green, 5 blue */
534 535 536 537 538 539
                    /* [gggbbbbb][rrrrrggg] -> [xxxxxxxx][rrrrrrrr][gggggggg][bbbbbbbb] */
                    *pixel_ptr = convert_5to8[*xor_ptr & 0x1f];
                    tmp = (*xor_ptr++ & 0xe0) >> 5;
                    tmp |= (*xor_ptr & 0x07) << 3;
                    *pixel_ptr |= convert_6to8[tmp] << 16;
                    *pixel_ptr |= convert_5to8[*xor_ptr & 0xf8] << 24;
540 541 542 543 544 545 546 547 548 549 550 551 552
                    break;

                case 1:
                    if (*xor_ptr & (1 << (7 - (x & 7)))) *pixel_ptr = 0xffffff;
                    else *pixel_ptr = 0;
                    if ((x & 7) == 7) ++xor_ptr;
                    break;

                default:
                    FIXME("Currently no support for cursors with %d bits per pixel\n", ptr->bBitsPerPixel);
                    return 0;
            }

553
            if (alpha_zero)
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
            {
                /* Alpha channel */
                if (~*and_ptr & (1 << (7 - (x & 7)))) *pixel_ptr |= 0xff << 24;
                else if (*pixel_ptr)
                {
                    int alpha = (*pixel_ptr & 0xff) * 0.30f
                            + ((*pixel_ptr & 0xff00) >> 8) * 0.55f
                            + ((*pixel_ptr & 0xff0000) >> 16) * 0.15f;
                    *pixel_ptr ^= ((x + y) % 2) ? 0xffffff : 0x000000;
                    *pixel_ptr |= alpha << 24;
                }
                if ((x & 7) == 7) ++and_ptr;
            }
            ++pixel_ptr;
        }
    }

    return image;
}


/***********************************************************************
 *              create_xcursor_cursor
 *
 * Use Xcursor to create an X cursor from a Windows one.
 */
static Cursor create_xcursor_cursor( Display *display, CURSORICONINFO *ptr )
{
    Cursor cursor;
    XcursorImage *image;

    if (!ptr) /* Create an empty cursor */
    {
        image = pXcursorImageCreate( 1, 1 );
        image->xhot = 0;
        image->yhot = 0;
        *(image->pixels) = 0;
        cursor = pXcursorImageLoadCursor( display, image );
        pXcursorImageDestroy( image );

        return cursor;
    }

    image = create_cursor_image( ptr );
    if (!image) return 0;

    /* Make sure hotspot is valid */
    image->xhot = ptr->ptHotSpot.x;
    image->yhot = ptr->ptHotSpot.y;
603 604
    if (image->xhot >= image->width ||
        image->yhot >= image->height)
605 606 607 608 609 610 611 612 613 614 615 616 617
    {
        image->xhot = image->width / 2;
        image->yhot = image->height / 2;
    }

    image->delay = 0;

    cursor = pXcursorImageLoadCursor( display, image );
    pXcursorImageDestroy( image );

    return cursor;
}

618
#endif /* SONAME_LIBXCURSOR */
619 620


621 622 623 624 625 626
/***********************************************************************
 *		create_cursor
 *
 * Create an X cursor from a Windows one.
 */
static Cursor create_cursor( Display *display, CURSORICONINFO *ptr )
627
{
628
    Pixmap pixmapBits, pixmapMask, pixmapMaskInv = 0, pixmapAll;
629 630
    XColor fg, bg;
    Cursor cursor = None;
631 632
    POINT hotspot;
    char *bitMask32 = NULL;
633
    BOOL alpha_zero = TRUE;
634

635
#ifdef SONAME_LIBXCURSOR
636 637 638
    if (pXcursorImageLoadCursor) return create_xcursor_cursor( display, ptr );
#endif

639 640 641 642 643
    if (!ptr)  /* Create an empty cursor */
    {
        static const char data[] = { 0 };

        bg.red = bg.green = bg.blue = 0x0000;
644
        pixmapBits = XCreateBitmapFromData( display, root_window, data, 1, 1 );
645 646 647 648 649 650 651 652 653 654
        if (pixmapBits)
        {
            cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
                                          &bg, &bg, 0, 0 );
            XFreePixmap( display, pixmapBits );
        }
    }
    else  /* Create the X cursor from the bits */
    {
        XImage *image;
655
        GC gc;
656

657 658 659
        TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
            ptr->nWidth, ptr->nHeight, ptr->bPlanes, ptr->bBitsPerPixel,
            ptr->nWidthBytes);
660

661 662 663 664 665 666 667
        /* Create a pixmap and transfer all the bits to it */

        /* NOTE: Following hack works, but only because XFree depth
         *       1 images really use 1 bit/pixel (and so the same layout
         *       as the Windows cursor data). Perhaps use a more generic
         *       algorithm here.
         */
668 669 670
        /* This pixmap will be written with two bitmaps. The first is
         *  the mask and the second is the image.
         */
671
        if (!(pixmapAll = XCreatePixmap( display, root_window,
672
                  ptr->nWidth, ptr->nHeight * 2, 1 )))
673
            return 0;
674
        if (!(image = XCreateImage( display, visual,
675
                1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
676
                ptr->nHeight * 2, 16, ptr->nWidthBytes/ptr->bBitsPerPixel)))
677 678 679 680
        {
            XFreePixmap( display, pixmapAll );
            return 0;
        }
681 682 683 684 685 686
        gc = XCreateGC( display, pixmapAll, 0, NULL );
        XSetGraphicsExposures( display, gc, False );
        image->byte_order = MSBFirst;
        image->bitmap_bit_order = MSBFirst;
        image->bitmap_unit = 16;
        _XInitImageFuncPtrs(image);
687 688 689 690 691 692 693 694 695 696 697 698 699
        if (ptr->bPlanes * ptr->bBitsPerPixel == 1)
        {
            /* A plain old white on black cursor. */
            fg.red = fg.green = fg.blue = 0xffff;
            bg.red = bg.green = bg.blue = 0x0000;
            XPutImage( display, pixmapAll, gc, image,
                0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
        }
        else
        {
            int     rbits, gbits, bbits, red, green, blue;
            int     rfg, gfg, bfg, rbg, gbg, bbg;
            int     rscale, gscale, bscale;
700
            int     x, y, xmax, ymax, byteIndex, xorIndex;
701 702 703
            unsigned char *theMask, *theImage, theChar;
            int     threshold, fgBits, bgBits, bitShifted;
            BYTE    pXorBits[128];   /* Up to 32x32 icons */
704

705 706
            switch (ptr->bBitsPerPixel)
            {
707 708 709 710
            case 32:
                bitMask32 = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
                                       ptr->nWidth * ptr->nHeight / 8 );
                /* Fallthrough */
711 712 713 714 715 716
            case 24:
                rbits = 8;
                gbits = 8;
                bbits = 8;
                threshold = 0x40;
                break;
717 718 719 720 721 722
            case 16:
                rbits = 5;
                gbits = 6;
                bbits = 5;
                threshold = 0x40;
                break;
723 724 725 726 727 728 729 730 731 732
            default:
                FIXME("Currently no support for cursors with %d bits per pixel\n",
                  ptr->bBitsPerPixel);
                XFreePixmap( display, pixmapAll );
                XFreeGC( display, gc );
                image->data = NULL;
                XDestroyImage( image );
                return 0;
            }
            /* The location of the mask. */
Mike McCormack's avatar
Mike McCormack committed
733
            theMask = (unsigned char *)(ptr + 1);
734
            /* The mask should still be 1 bit per pixel. The color image
735
             * should immediately follow the mask.
736 737 738 739 740 741 742 743 744 745 746 747 748
             */
            theImage = &theMask[ptr->nWidth/8 * ptr->nHeight];
            rfg = gfg = bfg = rbg = gbg = bbg = 0;
            byteIndex = 0;
            xorIndex = 0;
            fgBits = 0;
            bitShifted = 0x01;
            xmax = (ptr->nWidth > 32) ? 32 : ptr->nWidth;
            if (ptr->nWidth > 32) {
                ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
                  ptr->nWidth, ptr->nHeight);
            }
            ymax = (ptr->nHeight > 32) ? 32 : ptr->nHeight;
749
            alpha_zero = check_alpha_zero(ptr, theImage);
750

751 752 753 754 755
            memset(pXorBits, 0, 128);
            for (y=0; y<ymax; y++)
            {
                for (x=0; x<xmax; x++)
                {
756 757 758
                   	red = green = blue = 0;
                   	switch (ptr->bBitsPerPixel)
                   	{
759 760 761 762 763 764 765 766 767 768 769 770 771 772
			case 32:
			    theChar = theImage[byteIndex++];
			    blue = theChar;
			    theChar = theImage[byteIndex++];
			    green = theChar;
			    theChar = theImage[byteIndex++];
			    red = theChar;
			    theChar = theImage[byteIndex++];
                            /* If the alpha channel is >5% transparent,
                             * assume that we can add it to the bitMask32.
                             */
                            if (theChar > 0x0D)
                                *(bitMask32 + (y*xmax+x)/8) |= 1 << (x & 7);
			    break;
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
                   	case 24:
                   	    theChar = theImage[byteIndex++];
                   	    blue = theChar;
                   	    theChar = theImage[byteIndex++];
                   	    green = theChar;
                   	    theChar = theImage[byteIndex++];
                   	    red = theChar;
                   	    break;
                   	case 16:
                   	    theChar = theImage[byteIndex++];
                   	    blue = theChar & 0x1F;
                   	    green = (theChar & 0xE0) >> 5;
                   	    theChar = theImage[byteIndex++];
                   	    green |= (theChar & 0x07) << 3;
                   	    red = (theChar & 0xF8) >> 3;
                   	    break;
                   	}

791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
                    if (red+green+blue > threshold)
                    {
                        rfg += red;
                        gfg += green;
                        bfg += blue;
                        fgBits++;
                        pXorBits[xorIndex] |= bitShifted;
                    }
                    else
                    {
                        rbg += red;
                        gbg += green;
                        bbg += blue;
                    }
                    if (x%8 == 7)
                    {
                        bitShifted = 0x01;
                        xorIndex++;
                    }
                    else
                        bitShifted = bitShifted << 1;
                }
            }
            rscale = 1 << (16 - rbits);
            gscale = 1 << (16 - gbits);
            bscale = 1 << (16 - bbits);
817 818 819 820 821 822 823
            if (fgBits)
            {
                fg.red   = rfg * rscale / fgBits;
                fg.green = gfg * gscale / fgBits;
                fg.blue  = bfg * bscale / fgBits;
            }
            else fg.red = fg.green = fg.blue = 0;
824
            bgBits = xmax * ymax - fgBits;
825 826 827 828 829 830 831
            if (bgBits)
            {
                bg.red   = rbg * rscale / bgBits;
                bg.green = gbg * gscale / bgBits;
                bg.blue  = bbg * bscale / bgBits;
            }
            else bg.red = bg.green = bg.blue = 0;
832
            pixmapBits = XCreateBitmapFromData( display, root_window, (char *)pXorBits, xmax, ymax );
833 834
            if (!pixmapBits)
            {
835
                HeapFree( GetProcessHeap(), 0, bitMask32 );
836 837 838 839 840 841
                XFreePixmap( display, pixmapAll );
                XFreeGC( display, gc );
                image->data = NULL;
                XDestroyImage( image );
                return 0;
            }
842

843 844 845 846 847 848 849 850 851
            /* Put the mask. */
            XPutImage( display, pixmapAll, gc, image,
                   0, 0, 0, 0, ptr->nWidth, ptr->nHeight );
            XSetFunction( display, gc, GXcopy );
            /* Put the image */
            XCopyArea( display, pixmapBits, pixmapAll, gc,
                       0, 0, xmax, ymax, 0, ptr->nHeight );
            XFreePixmap( display, pixmapBits );
        }
852 853
        image->data = NULL;
        XDestroyImage( image );
854 855 856

        /* Now create the 2 pixmaps for bits and mask */

857
        pixmapBits = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
858
        if (alpha_zero)
859
        {
860 861
            pixmapMaskInv = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
            pixmapMask = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
862

863 864
            /* Make sure everything went OK so far */
            if (pixmapBits && pixmapMask && pixmapMaskInv)
865
            {
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
                /* We have to do some magic here, as cursors are not fully
                 * compatible between Windows and X11. Under X11, there are
                 * only 3 possible color cursor: black, white and masked. So
                 * we map the 4th Windows color (invert the bits on the screen)
                 * to black and an additional white bit on an other place
                 * (+1,+1). This require some boolean arithmetic:
                 *
                 *         Windows          |          X11
                 * And    Xor      Result   |   Bits     Mask     Result
                 *  0      0     black      |    0        1     background
                 *  0      1     white      |    1        1     foreground
                 *  1      0     no change  |    X        0     no change
                 *  1      1     inverted   |    0        1     background
                 *
                 * which gives:
                 *  Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
                 *  Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
                 *
                 * FIXME: apparently some servers do support 'inverted' color.
                 * I don't know if it's correct per the X spec, but maybe we
                 * ought to take advantage of it.  -- AJ
                 */
                XSetFunction( display, gc, GXcopy );
                XCopyArea( display, pixmapAll, pixmapBits, gc,
                           0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
                XCopyArea( display, pixmapAll, pixmapMask, gc,
                           0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
                XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
                           0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
                XSetFunction( display, gc, GXand );
                XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
                           0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
                XSetFunction( display, gc, GXandReverse );
                XCopyArea( display, pixmapAll, pixmapBits, gc,
                           0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
                XSetFunction( display, gc, GXorReverse );
                XCopyArea( display, pixmapAll, pixmapMask, gc,
                           0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
                /* Additional white */
                XSetFunction( display, gc, GXor );
                XCopyArea( display, pixmapMaskInv, pixmapMask, gc,
                           0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
                XCopyArea( display, pixmapMaskInv, pixmapBits, gc,
                           0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
                XSetFunction( display, gc, GXcopy );
911
            }
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
        }
        else
        {
            pixmapMask = XCreateBitmapFromData( display, root_window,
                                                bitMask32, ptr->nWidth,
                                                ptr->nHeight );
        }

        /* Make sure hotspot is valid */
        hotspot.x = ptr->ptHotSpot.x;
        hotspot.y = ptr->ptHotSpot.y;
        if (hotspot.x < 0 || hotspot.x >= ptr->nWidth ||
            hotspot.y < 0 || hotspot.y >= ptr->nHeight)
        {
            hotspot.x = ptr->nWidth / 2;
            hotspot.y = ptr->nHeight / 2;
        }

        if (pixmapBits && pixmapMask)
931
            cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
932
                                          &fg, &bg, hotspot.x, hotspot.y );
933 934 935 936 937 938

        /* Now free everything */

        if (pixmapAll) XFreePixmap( display, pixmapAll );
        if (pixmapBits) XFreePixmap( display, pixmapBits );
        if (pixmapMask) XFreePixmap( display, pixmapMask );
939
        if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
940
        HeapFree( GetProcessHeap(), 0, bitMask32 );
941
        XFreeGC( display, gc );
942
    }
943 944
    return cursor;
}
945

946 947

/***********************************************************************
948
 *		SetCursor (X11DRV.@)
949
 */
950
void CDECL X11DRV_SetCursor( CURSORICONINFO *lpCursor )
951
{
952
    struct x11drv_thread_data *data = x11drv_init_thread_data();
953
    Cursor cursor;
954

955 956 957 958 959 960
    if (lpCursor)
        TRACE("%ux%u, planes %u, bpp %u\n",
              lpCursor->nWidth, lpCursor->nHeight, lpCursor->bPlanes, lpCursor->bBitsPerPixel);
    else
        TRACE("NULL\n");

961
    /* set the same cursor for all top-level windows of the current thread */
962

963 964 965
    wine_tsx11_lock();
    cursor = create_cursor( data->display, lpCursor );
    if (cursor)
966
    {
967 968 969
        if (data->cursor) XFreeCursor( data->display, data->cursor );
        data->cursor = cursor;
        if (data->cursor_window)
970
        {
971 972 973
            XDefineCursor( data->display, data->cursor_window, cursor );
            /* Make the change take effect immediately */
            XFlush( data->display );
974 975
        }
    }
976
    wine_tsx11_unlock();
977 978 979
}

/***********************************************************************
980
 *		SetCursorPos (X11DRV.@)
981
 */
982
BOOL CDECL X11DRV_SetCursorPos( INT x, INT y )
983
{
984
    Display *display = thread_init_display();
985
    POINT pt;
986

987
    TRACE( "warping to (%d,%d)\n", x, y );
988

989
    wine_tsx11_lock();
990 991 992 993 994 995 996 997
    if (cursor_pos.x == x && cursor_pos.y == y)
    {
        wine_tsx11_unlock();
        /* We still need to generate WM_MOUSEMOVE */
        queue_raw_mouse_message( WM_MOUSEMOVE, NULL, x, y, 0, GetCurrentTime(), 0, 0 );
        return TRUE;
    }

998 999
    pt.x = x; pt.y = y;
    clip_point_to_rect( &cursor_clip, &pt);
1000
    XWarpPointer( display, root_window, root_window, 0, 0, 0, 0,
1001
                  pt.x - virtual_screen_rect.left, pt.y - virtual_screen_rect.top );
1002
    XFlush( display ); /* avoids bad mouse lag in games that do their own mouse warping */
1003
    cursor_pos = pt;
1004
    wine_tsx11_unlock();
1005
    return TRUE;
1006 1007 1008 1009 1010
}

/***********************************************************************
 *		GetCursorPos (X11DRV.@)
 */
1011
BOOL CDECL X11DRV_GetCursorPos(LPPOINT pos)
1012
{
1013
    Display *display = thread_init_display();
1014 1015 1016
    Window root, child;
    int rootX, rootY, winX, winY;
    unsigned int xstate;
1017

1018
    wine_tsx11_lock();
1019 1020
    if ((GetTickCount() - last_time_modified > 100) &&
        XQueryPointer( display, root_window, &root, &child,
1021 1022 1023
                       &rootX, &rootY, &winX, &winY, &xstate ))
    {
        update_button_state( xstate );
1024 1025
        winX += virtual_screen_rect.left;
        winY += virtual_screen_rect.top;
1026 1027 1028 1029 1030 1031 1032
        TRACE("pointer at (%d,%d)\n", winX, winY );
        cursor_pos.x = winX;
        cursor_pos.y = winY;
    }
    *pos = cursor_pos;
    wine_tsx11_unlock();
    return TRUE;
1033 1034
}

1035 1036 1037 1038 1039 1040

/***********************************************************************
 *		ClipCursor (X11DRV.@)
 *
 * Set the cursor clipping rectangle.
 */
1041
BOOL CDECL X11DRV_ClipCursor( LPCRECT clip )
1042 1043 1044 1045 1046 1047 1048
{
    if (!IntersectRect( &cursor_clip, &virtual_screen_rect, clip ))
        cursor_clip = virtual_screen_rect;

    return TRUE;
}

1049 1050 1051
/***********************************************************************
 *           X11DRV_ButtonPress
 */
1052
void X11DRV_ButtonPress( HWND hwnd, XEvent *xev )
1053
{
1054
    XButtonEvent *event = &xev->xbutton;
1055 1056 1057
    int buttonNum = event->button - 1;
    WORD wData = 0;
    POINT pt;
1058

1059
    if (buttonNum >= NB_BUTTONS) return;
1060
    if (!hwnd) return;
1061

1062
    switch (buttonNum)
1063
    {
1064 1065 1066 1067 1068 1069
    case 3:
        wData = WHEEL_DELTA;
        break;
    case 4:
        wData = -WHEEL_DELTA;
        break;
1070 1071 1072 1073 1074 1075
    case 5:
        wData = XBUTTON1;
        break;
    case 6:
        wData = XBUTTON2;
        break;
1076 1077 1078 1079 1080 1081
    case 7:
        wData = XBUTTON1;
        break;
    case 8:
        wData = XBUTTON2;
        break;
1082
    }
1083 1084 1085

    update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );

1086
    X11DRV_send_mouse_input( hwnd, button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1087
                             pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1088 1089
}

1090

1091
/***********************************************************************
1092
 *           X11DRV_ButtonRelease
1093
 */
1094
void X11DRV_ButtonRelease( HWND hwnd, XEvent *xev )
1095
{
1096
    XButtonEvent *event = &xev->xbutton;
1097
    int buttonNum = event->button - 1;
1098
    WORD wData = 0;
1099
    POINT pt;
1100

1101
    if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
1102
    if (!hwnd) return;
1103

1104 1105 1106 1107 1108 1109 1110 1111
    switch (buttonNum)
    {
    case 5:
        wData = XBUTTON1;
        break;
    case 6:
        wData = XBUTTON2;
        break;
1112 1113 1114 1115 1116 1117
    case 7:
        wData = XBUTTON1;
        break;
    case 8:
        wData = XBUTTON2;
        break;
1118 1119
    }

1120 1121
    update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );

1122
    X11DRV_send_mouse_input( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
1123
                             pt.x, pt.y, wData, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1124
}
1125 1126


1127 1128 1129
/***********************************************************************
 *           X11DRV_MotionNotify
 */
1130
void X11DRV_MotionNotify( HWND hwnd, XEvent *xev )
1131
{
1132
    XMotionEvent *event = &xev->xmotion;
1133 1134
    POINT pt;

1135 1136
    TRACE("hwnd %p, event->is_hint %d\n", hwnd, event->is_hint);

1137 1138
    if (!hwnd) return;

1139 1140
    update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );

1141 1142
    X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
                             pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1143 1144 1145 1146 1147 1148
}


/***********************************************************************
 *           X11DRV_EnterNotify
 */
1149
void X11DRV_EnterNotify( HWND hwnd, XEvent *xev )
1150
{
1151
    XCrossingEvent *event = &xev->xcrossing;
1152 1153
    POINT pt;

1154 1155
    TRACE("hwnd %p, event->detail %d\n", hwnd, event->detail);

1156
    if (!hwnd) return;
1157
    if (event->detail == NotifyVirtual || event->detail == NotifyNonlinearVirtual) return;
1158
    if (event->window == x11drv_thread_data()->grab_window) return;
1159 1160

    /* simulate a mouse motion event */
1161 1162
    update_mouse_state( hwnd, event->window, event->x, event->y, event->state, &pt );

1163 1164
    X11DRV_send_mouse_input( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
                             pt.x, pt.y, 0, EVENT_x11_time_to_win32_time(event->time), 0, 0 );
1165
}