input.c 27.6 KB
Newer Older
1 2 3 4
/*
 * USER Input processing
 *
 * Copyright 1993 Bob Amstadt
5
 * Copyright 1996 Albrecht Kleine
6 7 8 9
 * Copyright 1997 David Faure
 * Copyright 1998 Morten Welinder
 * Copyright 1998 Ulrich Weigand
 *
10 11 12 13 14 15 16 17 18 19 20 21
 * 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
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 24
 */

Steven Edwards's avatar
Steven Edwards committed
25 26 27
#include "config.h"
#include "wine/port.h"

28 29
#include <stdlib.h>
#include <string.h>
30
#include <stdarg.h>
31
#include <stdio.h>
32 33 34
#include <ctype.h>
#include <assert.h>

35 36
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
37
#include "windef.h"
38
#include "winbase.h"
39
#include "wingdi.h"
40
#include "winuser.h"
41
#include "winnls.h"
42
#include "winternl.h"
43
#include "winerror.h"
44 45 46 47
#include "win.h"
#include "user_private.h"
#include "wine/server.h"
#include "wine/debug.h"
48

49
WINE_DEFAULT_DEBUG_CHANNEL(win);
50
WINE_DECLARE_DEBUG_CHANNEL(keyboard);
51

52

53 54 55 56 57 58 59
/***********************************************************************
 *           get_key_state
 */
static WORD get_key_state(void)
{
    WORD ret = 0;

60
    if (GetSystemMetrics( SM_SWAPBUTTON ))
61
    {
62 63
        if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_LBUTTON;
        if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_RBUTTON;
64 65 66
    }
    else
    {
67 68
        if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_LBUTTON;
        if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_RBUTTON;
69
    }
70 71 72 73 74
    if (GetAsyncKeyState(VK_MBUTTON) & 0x80)  ret |= MK_MBUTTON;
    if (GetAsyncKeyState(VK_SHIFT) & 0x80)    ret |= MK_SHIFT;
    if (GetAsyncKeyState(VK_CONTROL) & 0x80)  ret |= MK_CONTROL;
    if (GetAsyncKeyState(VK_XBUTTON1) & 0x80) ret |= MK_XBUTTON1;
    if (GetAsyncKeyState(VK_XBUTTON2) & 0x80) ret |= MK_XBUTTON2;
75 76 77 78
    return ret;
}


79 80 81 82 83
/***********************************************************************
 *		SendInput  (USER32.@)
 */
UINT WINAPI SendInput( UINT count, LPINPUT inputs, int size )
{
84 85 86 87 88 89 90 91 92
    if (TRACE_ON(win))
    {
        UINT i;

        for (i = 0; i < count; i++)
        {
            switch(inputs[i].type)
            {
            case INPUT_MOUSE:
93
                TRACE("mouse: dx %d, dy %d, data %x, flags %x, time %u, info %lx\n",
94 95 96 97 98
                      inputs[i].u.mi.dx, inputs[i].u.mi.dy, inputs[i].u.mi.mouseData,
                      inputs[i].u.mi.dwFlags, inputs[i].u.mi.time, inputs[i].u.mi.dwExtraInfo);
                break;

            case INPUT_KEYBOARD:
99
                TRACE("keyboard: vk %x, scan %x, flags %x, time %u, info %lx\n",
100 101 102 103 104
                      inputs[i].u.ki.wVk, inputs[i].u.ki.wScan, inputs[i].u.ki.dwFlags,
                      inputs[i].u.ki.time, inputs[i].u.ki.dwExtraInfo);
                break;

            case INPUT_HARDWARE:
105
                TRACE("hardware: msg %d, wParamL %x, wParamH %x\n",
106 107 108 109
                      inputs[i].u.hi.uMsg, inputs[i].u.hi.wParamL, inputs[i].u.hi.wParamH);
                break;

            default:
110
                FIXME("unknown input type %u\n", inputs[i].type);
111 112 113 114 115
                break;
            }
        }
    }

116
    return USER_Driver->pSendInput( count, inputs, size );
117 118 119 120 121 122 123
}


/***********************************************************************
 *		keybd_event (USER32.@)
 */
void WINAPI keybd_event( BYTE bVk, BYTE bScan,
124
                         DWORD dwFlags, ULONG_PTR dwExtraInfo )
125 126 127 128 129 130 131
{
    INPUT input;

    input.type = INPUT_KEYBOARD;
    input.u.ki.wVk = bVk;
    input.u.ki.wScan = bScan;
    input.u.ki.dwFlags = dwFlags;
132 133
    input.u.ki.time = GetTickCount();
    input.u.ki.dwExtraInfo = dwExtraInfo;
134 135 136 137
    SendInput( 1, &input, sizeof(input) );
}


138
/***********************************************************************
139
 *		mouse_event (USER32.@)
140 141
 */
void WINAPI mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
142
                         DWORD dwData, ULONG_PTR dwExtraInfo )
143
{
144 145 146 147 148 149 150
    INPUT input;

    input.type = INPUT_MOUSE;
    input.u.mi.dx = dx;
    input.u.mi.dy = dy;
    input.u.mi.mouseData = dwData;
    input.u.mi.dwFlags = dwFlags;
151 152 153
    input.u.mi.time = GetCurrentTime();
    input.u.mi.dwExtraInfo = dwExtraInfo;
    SendInput( 1, &input, sizeof(input) );
154 155
}

156

157
/***********************************************************************
158
 *		GetCursorPos (USER32.@)
159 160 161
 */
BOOL WINAPI GetCursorPos( POINT *pt )
{
162 163
    if (!pt) return FALSE;
    return USER_Driver->pGetCursorPos( pt );
164 165 166
}


Per Nystrom's avatar
Per Nystrom committed
167 168 169 170 171 172
/***********************************************************************
 *		GetCursorInfo (USER32.@)
 */
BOOL WINAPI GetCursorInfo( PCURSORINFO pci )
{
    if (!pci) return 0;
173
    if (get_user_thread_info()->cursor_count >= 0) pci->flags = CURSOR_SHOWING;
174
    else pci->flags = 0;
Per Nystrom's avatar
Per Nystrom committed
175 176 177 178 179
    GetCursorPos(&pci->ptScreenPos);
    return 1;
}


180 181 182 183 184
/***********************************************************************
 *		SetCursorPos (USER32.@)
 */
BOOL WINAPI SetCursorPos( INT x, INT y )
{
185
    return USER_Driver->pSetCursorPos( x, y );
186 187 188
}


189
/**********************************************************************
190
 *		SetCapture (USER32.@)
191
 */
192
HWND WINAPI SetCapture( HWND hwnd )
193
{
194
    HWND previous = 0;
195

196
    SERVER_START_REQ( set_capture_window )
197
    {
198 199 200
        req->handle = hwnd;
        req->flags  = 0;
        if (!wine_server_call_err( req ))
201
        {
202 203
            previous = reply->previous;
            hwnd = reply->full_handle;
204 205
        }
    }
206
    SERVER_END_REQ;
207

208 209 210
    if (previous && previous != hwnd)
        SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
    return previous;
211 212 213 214
}


/**********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
215
 *		ReleaseCapture (USER32.@)
216
 */
217
BOOL WINAPI ReleaseCapture(void)
218
{
219 220 221 222 223 224 225 226 227 228 229 230
    BOOL ret;
    HWND previous = 0;

    SERVER_START_REQ( set_capture_window )
    {
        req->handle = 0;
        req->flags  = 0;
        if ((ret = !wine_server_call_err( req ))) previous = reply->previous;
    }
    SERVER_END_REQ;

    if (previous) SendMessageW( previous, WM_CAPTURECHANGED, 0, 0 );
231 232 233 234

    /* Somebody may have missed some mouse movements */
    mouse_event( MOUSEEVENTF_MOVE, 0, 0, 0, 0 );

235
    return ret;
236 237 238 239
}


/**********************************************************************
240
 *		GetCapture (USER32.@)
241
 */
242
HWND WINAPI GetCapture(void)
243
{
244 245 246 247 248 249 250 251 252
    HWND ret = 0;

    SERVER_START_REQ( get_thread_input )
    {
        req->tid = GetCurrentThreadId();
        if (!wine_server_call_err( req )) ret = reply->capture;
    }
    SERVER_END_REQ;
    return ret;
253 254
}

255

256
/**********************************************************************
257
 *		GetAsyncKeyState (USER32.@)
258
 *
259
 *	Determine if a key is or was pressed.  retval has high-order
260 261 262
 * bit set to 1 if currently pressed, low-order bit set to 1 if key has
 * been pressed.
 */
263
SHORT WINAPI GetAsyncKeyState(INT nKey)
264
{
265
    return USER_Driver->pGetAsyncKeyState( nKey );
266 267
}

Ulrich Weigand's avatar
Ulrich Weigand committed
268

269 270 271 272 273 274 275
/***********************************************************************
 *		GetQueueStatus (USER32.@)
 */
DWORD WINAPI GetQueueStatus( UINT flags )
{
    DWORD ret = 0;

276 277 278 279 280
    if (flags & ~(QS_ALLINPUT | QS_ALLPOSTMESSAGE | QS_SMRESULT))
    {
        SetLastError( ERROR_INVALID_FLAGS );
        return 0;
    }
281

282
    /* check for pending X events */
283
    USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, flags, 0 );
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

    SERVER_START_REQ( get_queue_status )
    {
        req->clear = 1;
        wine_server_call( req );
        ret = MAKELONG( reply->changed_bits & flags, reply->wake_bits & flags );
    }
    SERVER_END_REQ;
    return ret;
}


/***********************************************************************
 *		GetInputState   (USER32.@)
 */
BOOL WINAPI GetInputState(void)
{
    DWORD ret = 0;

    /* check for pending X events */
304
    USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_INPUT, 0 );
305 306 307 308 309 310 311 312 313 314 315 316

    SERVER_START_REQ( get_queue_status )
    {
        req->clear = 0;
        wine_server_call( req );
        ret = reply->wake_bits & (QS_KEY | QS_MOUSEBUTTON);
    }
    SERVER_END_REQ;
    return ret;
}


317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
/******************************************************************
 *              GetLastInputInfo (USER32.@)
 */
BOOL WINAPI GetLastInputInfo(PLASTINPUTINFO plii)
{
    BOOL ret;

    TRACE("%p\n", plii);

    if (plii->cbSize != sizeof (*plii) )
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }

    SERVER_START_REQ( get_last_input_time )
    {
        ret = !wine_server_call_err( req );
        if (ret)
            plii->dwTime = reply->time;
    }
    SERVER_END_REQ;
    return ret;
}


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 387 388 389 390 391 392 393 394 395 396 397 398 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
/**********************************************************************
 *		AttachThreadInput (USER32.@)
 *
 * Attaches the input processing mechanism of one thread to that of
 * another thread.
 */
BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
{
    BOOL ret;

    SERVER_START_REQ( attach_thread_input )
    {
        req->tid_from = from;
        req->tid_to   = to;
        req->attach   = attach;
        ret = !wine_server_call_err( req );
    }
    SERVER_END_REQ;
    return ret;
}


/**********************************************************************
 *		GetKeyState (USER32.@)
 *
 * An application calls the GetKeyState function in response to a
 * keyboard-input message.  This function retrieves the state of the key
 * at the time the input message was generated.
 */
SHORT WINAPI GetKeyState(INT vkey)
{
    SHORT retval = 0;

    SERVER_START_REQ( get_key_state )
    {
        req->tid = GetCurrentThreadId();
        req->key = vkey;
        if (!wine_server_call( req )) retval = (signed char)reply->state;
    }
    SERVER_END_REQ;
    TRACE("key (0x%x) -> %x\n", vkey, retval);
    return retval;
}


/**********************************************************************
 *		GetKeyboardState (USER32.@)
 */
BOOL WINAPI GetKeyboardState( LPBYTE state )
{
    BOOL ret;

    TRACE("(%p)\n", state);

    memset( state, 0, 256 );
    SERVER_START_REQ( get_key_state )
    {
        req->tid = GetCurrentThreadId();
        req->key = -1;
        wine_server_set_reply( req, state, 256 );
        ret = !wine_server_call_err( req );
    }
    SERVER_END_REQ;
    return ret;
}


/**********************************************************************
 *		SetKeyboardState (USER32.@)
 */
BOOL WINAPI SetKeyboardState( LPBYTE state )
{
    BOOL ret;

    TRACE("(%p)\n", state);

    SERVER_START_REQ( set_key_state )
    {
        req->tid = GetCurrentThreadId();
        wine_server_add_data( req, state, 256 );
        ret = !wine_server_call_err( req );
    }
    SERVER_END_REQ;
    return ret;
}


430
/**********************************************************************
431
 *		VkKeyScanA (USER32.@)
432 433 434 435 436 437 438 439 440 441 442 443 444 445
 *
 * VkKeyScan translates an ANSI character to a virtual-key and shift code
 * for the current keyboard.
 * high-order byte yields :
 *	0	Unshifted
 *	1	Shift
 *	2	Ctrl
 *	3-5	Shift-key combinations that are not used for characters
 *	6	Ctrl-Alt
 *	7	Ctrl-Alt-Shift
 *	I.e. :	Shift = 1, Ctrl = 2, Alt = 4.
 * FIXME : works ok except for dead chars :
 * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
 * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
446
 */
447
SHORT WINAPI VkKeyScanA(CHAR cChar)
448
{
449 450 451 452 453 454
    WCHAR wChar;

    if (IsDBCSLeadByte(cChar)) return -1;

    MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
    return VkKeyScanW(wChar);
455 456 457
}

/******************************************************************************
458
 *		VkKeyScanW (USER32.@)
459
 */
460
SHORT WINAPI VkKeyScanW(WCHAR cChar)
461
{
462
    return VkKeyScanExW(cChar, GetKeyboardLayout(0));
463 464
}

465
/**********************************************************************
466
 *		VkKeyScanExA (USER32.@)
467 468 469
 */
WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
{
470 471 472 473 474 475
    WCHAR wChar;

    if (IsDBCSLeadByte(cChar)) return -1;

    MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
    return VkKeyScanExW(wChar, dwhkl);
476 477 478
}

/******************************************************************************
479
 *		VkKeyScanExW (USER32.@)
480 481 482
 */
WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
{
483
    return USER_Driver->pVkKeyScanEx(cChar, dwhkl);
484
}
485

486 487 488 489 490 491 492 493 494
/**********************************************************************
 *		OemKeyScan (USER32.@)
 */
DWORD WINAPI OemKeyScan(WORD wOemChar)
{
    TRACE("(%d)\n", wOemChar);
    return wOemChar;
}

495
/******************************************************************************
496
 *		GetKeyboardType (USER32.@)
497
 */
498
INT WINAPI GetKeyboardType(INT nTypeFlag)
499
{
500 501 502 503 504 505 506 507 508 509 510 511 512
    TRACE_(keyboard)("(%d)\n", nTypeFlag);
    switch(nTypeFlag)
    {
    case 0:      /* Keyboard type */
        return 4;    /* AT-101 */
    case 1:      /* Keyboard Subtype */
        return 0;    /* There are no defined subtypes */
    case 2:      /* Number of F-keys */
        return 12;   /* We're doing an 101 for now, so return 12 F-keys */
    default:
        WARN_(keyboard)("Unknown type\n");
        return 0;    /* The book says 0 here, so 0 */
    }
513 514 515
}

/******************************************************************************
516
 *		MapVirtualKeyA (USER32.@)
517
 */
518
UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
519
{
520
    return MapVirtualKeyExA( code, maptype, GetKeyboardLayout(0) );
521 522 523
}

/******************************************************************************
524
 *		MapVirtualKeyW (USER32.@)
525
 */
526
UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
527
{
528
    return MapVirtualKeyExW(code, maptype, GetKeyboardLayout(0));
529 530
}

531
/******************************************************************************
532
 *		MapVirtualKeyExA (USER32.@)
533
 */
534
UINT WINAPI MapVirtualKeyExA(UINT code, UINT maptype, HKL hkl)
535
{
536
    return MapVirtualKeyExW(code, maptype, hkl);
537 538
}

539
/******************************************************************************
540
 *		MapVirtualKeyExW (USER32.@)
541 542 543
 */
UINT WINAPI MapVirtualKeyExW(UINT code, UINT maptype, HKL hkl)
{
544 545
    TRACE_(keyboard)("(%d, %d, %p)\n", code, maptype, hkl);

546
    return USER_Driver->pMapVirtualKeyEx(code, maptype, hkl);
547 548
}

549
/****************************************************************************
550
 *		GetKBCodePage (USER32.@)
551
 */
552
UINT WINAPI GetKBCodePage(void)
553
{
554
    return GetOEMCP();
555 556
}

557
/***********************************************************************
558
 *		GetKeyboardLayout (USER32.@)
559
 *
560
 *        - device handle for keyboard layout defaulted to
561 562 563 564 565
 *          the language id. This is the way Windows default works.
 *        - the thread identifier (dwLayout) is also ignored.
 */
HKL WINAPI GetKeyboardLayout(DWORD dwLayout)
{
566
    return USER_Driver->pGetKeyboardLayout(dwLayout);
567 568
}

569
/****************************************************************************
570
 *		GetKeyboardLayoutNameA (USER32.@)
571
 */
572
BOOL WINAPI GetKeyboardLayoutNameA(LPSTR pszKLID)
573
{
574 575 576 577 578
    WCHAR buf[KL_NAMELENGTH];

    if (GetKeyboardLayoutNameW(buf))
        return WideCharToMultiByte( CP_ACP, 0, buf, -1, pszKLID, KL_NAMELENGTH, NULL, NULL ) != 0;
    return FALSE;
579 580 581
}

/****************************************************************************
582
 *		GetKeyboardLayoutNameW (USER32.@)
583
 */
584
BOOL WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
585
{
586
    return USER_Driver->pGetKeyboardLayoutName(pwszKLID);
587 588 589
}

/****************************************************************************
590
 *		GetKeyNameTextA (USER32.@)
591
 */
592
INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
593
{
594 595 596 597
    WCHAR buf[256];
    INT ret;

    if (!GetKeyNameTextW(lParam, buf, 256))
598 599
    {
        lpBuffer[0] = 0;
600
        return 0;
601
    }
602 603 604 605 606 607 608
    ret = WideCharToMultiByte(CP_ACP, 0, buf, -1, lpBuffer, nSize, NULL, NULL);
    if (!ret && nSize)
    {
        ret = nSize - 1;
        lpBuffer[ret] = 0;
    }
    return ret;
609 610 611
}

/****************************************************************************
612
 *		GetKeyNameTextW (USER32.@)
613
 */
614
INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
615
{
616
    return USER_Driver->pGetKeyNameText( lParam, lpBuffer, nSize );
617 618
}

619
/****************************************************************************
620
 *		ToUnicode (USER32.@)
621 622 623 624
 */
INT WINAPI ToUnicode(UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
		     LPWSTR lpwStr, int size, UINT flags)
{
625
    return ToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, GetKeyboardLayout(0));
626 627 628
}

/****************************************************************************
629
 *		ToUnicodeEx (USER32.@)
630 631 632 633
 */
INT WINAPI ToUnicodeEx(UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
		       LPWSTR lpwStr, int size, UINT flags, HKL hkl)
{
634
    return USER_Driver->pToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, hkl);
635 636
}

637
/****************************************************************************
638
 *		ToAscii (USER32.@)
639
 */
640 641
INT WINAPI ToAscii( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
                    LPWORD lpChar, UINT flags )
642
{
643
    return ToAsciiEx(virtKey, scanCode, lpKeyState, lpChar, flags, GetKeyboardLayout(0));
644 645
}

646
/****************************************************************************
647
 *		ToAsciiEx (USER32.@)
648 649 650 651
 */
INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
                      LPWORD lpChar, UINT flags, HKL dwhkl )
{
652 653 654 655 656 657 658 659
    WCHAR uni_chars[2];
    INT ret, n_ret;

    ret = ToUnicodeEx(virtKey, scanCode, lpKeyState, uni_chars, 2, flags, dwhkl);
    if (ret < 0) n_ret = 1; /* FIXME: make ToUnicode return 2 for dead chars */
    else n_ret = ret;
    WideCharToMultiByte(CP_ACP, 0, uni_chars, n_ret, (LPSTR)lpChar, 2, NULL, NULL);
    return ret;
660 661
}

662
/**********************************************************************
663
 *		ActivateKeyboardLayout (USER32.@)
664
 */
665
HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
666
{
667
    TRACE_(keyboard)("(%p, %d)\n", hLayout, flags);
668

669
    return USER_Driver->pActivateKeyboardLayout(hLayout, flags);
670 671
}

672 673 674 675 676 677 678 679 680 681
/**********************************************************************
 *		BlockInput (USER32.@)
 */
BOOL WINAPI BlockInput(BOOL fBlockIt)
{
    FIXME_(keyboard)("(%d): stub\n", fBlockIt);
    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);

    return FALSE;
}
682

683
/***********************************************************************
684
 *		GetKeyboardLayoutList (USER32.@)
685
 *
686
 * Return number of values available if either input parm is
687
 *  0, per MS documentation.
688
 */
689
UINT WINAPI GetKeyboardLayoutList(INT nBuff, HKL *layouts)
690
{
691 692
    TRACE_(keyboard)("(%d,%p)\n",nBuff,layouts);

693
    return USER_Driver->pGetKeyboardLayoutList(nBuff, layouts);
694 695 696 697
}


/***********************************************************************
698
 *		RegisterHotKey (USER32.@)
699
 */
700 701 702 703
BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk)
{
    FIXME_(keyboard)("(%p,%d,0x%08x,%d): stub\n",hwnd,id,modifiers,vk);
    return TRUE;
704 705 706
}

/***********************************************************************
707
 *		UnregisterHotKey (USER32.@)
708
 */
709 710 711 712
BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id)
{
    FIXME_(keyboard)("(%p,%d): stub\n",hwnd,id);
    return TRUE;
713 714
}

715
/***********************************************************************
716
 *		LoadKeyboardLayoutW (USER32.@)
717
 */
718
HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
719
{
720
    TRACE_(keyboard)("(%s, %d)\n", debugstr_w(pwszKLID), Flags);
721

722
    return USER_Driver->pLoadKeyboardLayout(pwszKLID, Flags);
723 724 725
}

/***********************************************************************
726
 *		LoadKeyboardLayoutA (USER32.@)
727
 */
728
HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
729
{
730 731
    HKL ret;
    UNICODE_STRING pwszKLIDW;
732

733 734 735 736 737 738
    if (pwszKLID) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW, pwszKLID);
    else pwszKLIDW.Buffer = NULL;

    ret = LoadKeyboardLayoutW(pwszKLIDW.Buffer, Flags);
    RtlFreeUnicodeString(&pwszKLIDW);
    return ret;
739
}
740 741


742 743 744 745 746 747 748
/***********************************************************************
 *		UnloadKeyboardLayout (USER32.@)
 */
BOOL WINAPI UnloadKeyboardLayout(HKL hkl)
{
    TRACE_(keyboard)("(%p)\n", hkl);

749
    return USER_Driver->pUnloadKeyboardLayout(hkl);
750 751
}

752 753 754
typedef struct __TRACKINGLIST {
    TRACKMOUSEEVENT tme;
    POINT pos; /* center of hover rectangle */
755
} _TRACKINGLIST;
756

757 758
/* FIXME: move tracking stuff into a per thread data */
static _TRACKINGLIST tracking_info;
759 760
static UINT_PTR timer;

761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
static void check_mouse_leave(HWND hwnd, int hittest)
{
    if (tracking_info.tme.hwndTrack != hwnd)
    {
        if (tracking_info.tme.dwFlags & TME_NONCLIENT)
            PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
        else
            PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);

        /* remove the TME_LEAVE flag */
        tracking_info.tme.dwFlags &= ~TME_LEAVE;
    }
    else
    {
        if (hittest == HTCLIENT)
        {
            if (tracking_info.tme.dwFlags & TME_NONCLIENT)
            {
                PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
                /* remove the TME_LEAVE flag */
                tracking_info.tme.dwFlags &= ~TME_LEAVE;
            }
        }
        else
        {
            if (!(tracking_info.tme.dwFlags & TME_NONCLIENT))
            {
                PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
                /* remove the TME_LEAVE flag */
                tracking_info.tme.dwFlags &= ~TME_LEAVE;
            }
        }
    }
}

796 797
static void CALLBACK TrackMouseEventProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent,
                                         DWORD dwTime)
798 799
{
    POINT pos;
800 801
    INT hoverwidth = 0, hoverheight = 0, hittest;

802
    TRACE("hwnd %p, msg %04x, id %04x, time %u\n", hwnd, uMsg, idEvent, dwTime);
803 804

    GetCursorPos(&pos);
805 806 807
    hwnd = WINPOS_WindowFromPoint(hwnd, pos, &hittest);

    TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos), hwnd, hittest);
808

809 810
    SystemParametersInfoW(SPI_GETMOUSEHOVERWIDTH, 0, &hoverwidth, 0);
    SystemParametersInfoW(SPI_GETMOUSEHOVERHEIGHT, 0, &hoverheight, 0);
811

812 813 814 815
    TRACE("tracked pos %s, current pos %s, hover width %d, hover height %d\n",
           wine_dbgstr_point(&tracking_info.pos), wine_dbgstr_point(&pos),
           hoverwidth, hoverheight);

816 817 818 819
    /* see if this tracking event is looking for TME_LEAVE and that the */
    /* mouse has left the window */
    if (tracking_info.tme.dwFlags & TME_LEAVE)
    {
820
        check_mouse_leave(hwnd, hittest);
821
    }
822

823 824 825 826 827 828
    if (tracking_info.tme.hwndTrack != hwnd)
    {
        /* mouse is gone, stop tracking mouse hover */
        tracking_info.tme.dwFlags &= ~TME_HOVER;
    }

829 830 831 832
    /* see if we are tracking hovering for this hwnd */
    if (tracking_info.tme.dwFlags & TME_HOVER)
    {
        /* has the cursor moved outside the rectangle centered around pos? */
833 834
        if ((abs(pos.x - tracking_info.pos.x) > (hoverwidth / 2)) ||
            (abs(pos.y - tracking_info.pos.y) > (hoverheight / 2)))
835
        {
836
            /* record this new position as the current position */
837 838 839 840
            tracking_info.pos = pos;
        }
        else
        {
841 842 843 844
            if (hittest == HTCLIENT)
            {
                ScreenToClient(hwnd, &pos);
                TRACE("client cursor pos %s\n", wine_dbgstr_point(&pos));
845 846

                PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSEHOVER,
847 848 849 850 851 852 853 854
                             get_key_state(), MAKELPARAM( pos.x, pos.y ));
            }
            else
            {
                if (tracking_info.tme.dwFlags & TME_NONCLIENT)
                    PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSEHOVER,
                                 hittest, MAKELPARAM( pos.x, pos.y ));
            }
855 856 857

            /* stop tracking mouse hover */
            tracking_info.tme.dwFlags &= ~TME_HOVER;
858 859
        }
    }
860

861
    /* stop the timer if the tracking list is empty */
862 863
    if (!(tracking_info.tme.dwFlags & (TME_HOVER | TME_LEAVE)))
    {
864
        KillSystemTimer(tracking_info.tme.hwndTrack, timer);
865
        timer = 0;
866 867 868
        tracking_info.tme.hwndTrack = 0;
        tracking_info.tme.dwFlags = 0;
        tracking_info.tme.dwHoverTime = 0;
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
    }
}


/***********************************************************************
 * TrackMouseEvent [USER32]
 *
 * Requests notification of mouse events
 *
 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
 * to the hwnd specified in the ptme structure.  After the event message
 * is posted to the hwnd, the entry in the queue is removed.
 *
 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
 * immediately and the TME_LEAVE flag being ignored.
 *
 * PARAMS
 *     ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
 *
 * RETURNS
 *     Success: non-zero
 *     Failure: zero
 *
 */

BOOL WINAPI
TrackMouseEvent (TRACKMOUSEEVENT *ptme)
{
    HWND hwnd;
    POINT pos;
900
    DWORD hover_time;
901
    INT hittest;
902

903
    TRACE("%x, %x, %p, %u\n", ptme->cbSize, ptme->dwFlags, ptme->hwndTrack, ptme->dwHoverTime);
904 905 906

    if (ptme->cbSize != sizeof(TRACKMOUSEEVENT)) {
        WARN("wrong TRACKMOUSEEVENT size from app\n");
907
        SetLastError(ERROR_INVALID_PARAMETER);
908 909 910
        return FALSE;
    }

911 912 913 914
    /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
    if (ptme->dwFlags & TME_QUERY )
    {
        *ptme = tracking_info.tme;
915 916
        /* set cbSize in the case it's not initialized yet */
        ptme->cbSize = sizeof(TRACKMOUSEEVENT);
917

918
        return TRUE; /* return here, TME_QUERY is retrieving information */
919 920
    }

921 922 923 924
    if (!IsWindow(ptme->hwndTrack))
    {
        SetLastError(ERROR_INVALID_WINDOW_HANDLE);
        return FALSE;
925 926
    }

927
    hover_time = ptme->dwHoverTime;
928

929 930 931
    /* if HOVER_DEFAULT was specified replace this with the systems current value.
     * TME_LEAVE doesn't need to specify hover time so use default */
    if (hover_time == HOVER_DEFAULT || hover_time == 0 || !(ptme->dwHoverTime&TME_HOVER))
932
        SystemParametersInfoW(SPI_GETMOUSEHOVERTIME, 0, &hover_time, 0);
933

934
    GetCursorPos(&pos);
935 936
    hwnd = WINPOS_WindowFromPoint(ptme->hwndTrack, pos, &hittest);
    TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos), hwnd, hittest);
937

938
    if (ptme->dwFlags & ~(TME_CANCEL | TME_HOVER | TME_LEAVE | TME_NONCLIENT))
939
        FIXME("Unknown flag(s) %08x\n", ptme->dwFlags & ~(TME_CANCEL | TME_HOVER | TME_LEAVE | TME_NONCLIENT));
940

941 942 943 944 945
    if (ptme->dwFlags & TME_CANCEL)
    {
        if (tracking_info.tme.hwndTrack == ptme->hwndTrack)
        {
            tracking_info.tme.dwFlags &= ~(ptme->dwFlags & ~TME_CANCEL);
946 947

            /* if we aren't tracking on hover or leave remove this entry */
948
            if (!(tracking_info.tme.dwFlags & (TME_HOVER | TME_LEAVE)))
949
            {
950
                KillSystemTimer(tracking_info.tme.hwndTrack, timer);
951
                timer = 0;
952 953 954
                tracking_info.tme.hwndTrack = 0;
                tracking_info.tme.dwFlags = 0;
                tracking_info.tme.dwHoverTime = 0;
955 956 957
            }
        }
    } else {
958 959 960 961 962 963
        /* In our implementation it's possible that another window will receive a
         * WM_MOUSEMOVE and call TrackMouseEvent before TrackMouseEventProc is
         * called. In such a situation post the WM_MOUSELEAVE now */
        if (tracking_info.tme.dwFlags & TME_LEAVE && tracking_info.tme.hwndTrack != NULL)
            check_mouse_leave(hwnd, hittest);

964 965 966 967 968 969 970 971 972
        if (timer)
        {
            KillSystemTimer(tracking_info.tme.hwndTrack, timer);
            timer = 0;
            tracking_info.tme.hwndTrack = 0;
            tracking_info.tme.dwFlags = 0;
            tracking_info.tme.dwHoverTime = 0;
        }

973 974
        if (ptme->hwndTrack == hwnd)
        {
975
            /* Adding new mouse event to the tracking list */
976 977
            tracking_info.tme = *ptme;
            tracking_info.tme.dwHoverTime = hover_time;
978 979

            /* Initialize HoverInfo variables even if not hover tracking */
980
            tracking_info.pos = pos;
981

982
            timer = SetSystemTimer(tracking_info.tme.hwndTrack, (UINT_PTR)&tracking_info.tme, hover_time, TrackMouseEventProc);
983 984 985 986 987
        }
    }

    return TRUE;
}