3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * PROJECT: ReactOS user32.dll
22 * FILE: lib/user32/windows/input.c
24 * PROGRAMMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
26 * 09-05-2001 CSH Created
29 /* INCLUDES ******************************************************************/
35 /* GLOBALS *******************************************************************/
38 typedef struct __TRACKINGLIST
{
40 POINT pos
; /* center of hover rectangle */
41 INT iHoverTime
; /* elapsed time the cursor has been inside of the hover rect */
44 static _TRACKINGLIST TrackingList
[10];
45 static int iTrackMax
= 0;
46 static UINT_PTR timer
;
47 static const INT iTimerInterval
= 50; /* msec for timer interval */
50 /* FUNCTIONS *****************************************************************/
63 return NtUserDragDetect(hWnd
, pt
.x
, pt
.y
);
68 ULONG dx
= NtUserGetSystemMetrics(SM_CXDRAG
);
69 ULONG dy
= NtUserGetSystemMetrics(SM_CYDRAG
);
71 rect
.left
= pt
.x
- dx
;
72 rect
.right
= pt
.x
+ dx
;
74 rect
.bottom
= pt
.y
+ dy
;
80 while (PeekMessageW(&msg
, 0, WM_MOUSEFIRST
, WM_MOUSELAST
, PM_REMOVE
))
82 if (msg
.message
== WM_LBUTTONUP
)
87 if (msg
.message
== WM_MOUSEMOVE
)
89 tmp
.x
= LOWORD(msg
.lParam
);
90 tmp
.y
= HIWORD(msg
.lParam
);
91 if (!PtInRect(&rect
, tmp
))
109 ActivateKeyboardLayout(HKL hkl
,
121 BlockInput(BOOL fBlockIt
)
123 return NtUserBlockInput(fBlockIt
);
131 EnableWindow(HWND hWnd
,
134 LONG Style
= NtUserGetWindowLong(hWnd
, GWL_STYLE
, FALSE
);
135 Style
= bEnable
? Style
& ~WS_DISABLED
: Style
| WS_DISABLED
;
136 NtUserSetWindowLong(hWnd
, GWL_STYLE
, Style
, FALSE
);
138 SendMessageA(hWnd
, WM_ENABLE
, (LPARAM
) IsWindowEnabled(hWnd
), 0);
140 // Return nonzero if it was disabled, or zero if it wasn't:
141 return IsWindowEnabled(hWnd
);
149 GetAsyncKeyState(int vKey
)
151 return (SHORT
) NtUserGetAsyncKeyState((DWORD
) vKey
);
160 GetDoubleClickTime(VOID
)
162 return NtUserGetDoubleClickTime();
170 GetKeyboardLayout(DWORD idThread
)
172 return (HKL
)NtUserCallOneParam((DWORD
) idThread
, ONEPARAM_ROUTINE_GETKEYBOARDLAYOUT
);
190 GetKeyNameTextA(LONG lParam
,
194 LPWSTR intermediateString
=
195 HeapAlloc(GetProcessHeap(),0,nSize
* sizeof(WCHAR
));
198 BOOL defChar
= FALSE
;
200 if( !intermediateString
) return 0;
201 ret
= GetKeyNameTextW(lParam
,intermediateString
,nSize
);
202 if( ret
== 0 ) { lpString
[0] = 0; return 0; }
204 wstrLen
= wcslen( intermediateString
);
205 ret
= WideCharToMultiByte(CP_ACP
, 0,
206 intermediateString
, wstrLen
,
207 lpString
, nSize
, ".", &defChar
);
209 HeapFree(GetProcessHeap(),0,intermediateString
);
218 GetKeyNameTextW(LONG lParam
,
222 return NtUserGetKeyNameText( lParam
, lpString
, nSize
);
230 GetKeyState(int nVirtKey
)
232 return (SHORT
) NtUserGetKeyState((DWORD
) nVirtKey
);
240 GetKeyboardLayoutList(int nBuff
,
252 GetKeyboardLayoutNameA(LPSTR pwszKLID
)
254 WCHAR buf
[KL_NAMELENGTH
];
256 if (GetKeyboardLayoutNameW(buf
))
257 return WideCharToMultiByte( CP_ACP
, 0, buf
, -1, pwszKLID
, KL_NAMELENGTH
, NULL
, NULL
) != 0;
266 GetKeyboardLayoutNameW(LPWSTR pwszKLID
)
277 GetKeyboardState(PBYTE lpKeyState
)
280 return (BOOL
) NtUserGetKeyboardState((LPBYTE
) lpKeyState
);
288 GetKeyboardType(int nTypeFlag
)
290 return (int)NtUserCallOneParam((DWORD
) nTypeFlag
, ONEPARAM_ROUTINE_GETKEYBOARDTYPE
);
298 GetLastInputInfo(PLASTINPUTINFO plii
)
309 LoadKeyboardLayoutA(LPCSTR pwszKLID
,
313 UNICODE_STRING pwszKLIDW
;
315 if (pwszKLID
) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW
, pwszKLID
);
316 else pwszKLIDW
.Buffer
= NULL
;
318 ret
= LoadKeyboardLayoutW(pwszKLIDW
.Buffer
, Flags
);
319 RtlFreeUnicodeString(&pwszKLIDW
);
329 LoadKeyboardLayoutW(LPCWSTR pwszKLID
,
341 MapVirtualKeyA(UINT uCode
,
344 return MapVirtualKeyExA( uCode
, uMapType
, GetKeyboardLayout( 0 ) );
352 MapVirtualKeyExA(UINT uCode
,
356 return MapVirtualKeyExW( uCode
, uMapType
, dwhkl
);
364 MapVirtualKeyExW(UINT uCode
,
368 return NtUserMapVirtualKeyEx( uCode
, uMapType
, 0, dwhkl
);
376 MapVirtualKeyW(UINT uCode
,
379 return MapVirtualKeyExW( uCode
, uMapType
, GetKeyboardLayout( 0 ) );
387 OemKeyScan(WORD wOemChar
)
393 MultiByteToWideChar(CP_OEMCP
, 0, (PCSTR
)&wOemChar
, 1, &p
, 1);
395 Scan
= MapVirtualKeyW((Vk
& 0x00ff), 0);
398 Page 450-1, MS W2k SuperBible by SAMS. Return, low word has the
399 scan code and high word has the shift state.
401 return ((Vk
& 0xff00) << 8) | Scan
;
409 RegisterHotKey(HWND hWnd
,
414 return (BOOL
)NtUserRegisterHotKey(hWnd
,
425 SetDoubleClickTime(UINT uInterval
)
427 return (BOOL
)NtUserSystemParametersInfo(SPI_SETDOUBLECLICKTIME
,
440 return NtUserSetFocus(hWnd
);
448 SetKeyboardState(LPBYTE lpKeyState
)
450 return (BOOL
) NtUserSetKeyboardState((LPBYTE
)lpKeyState
);
462 return NtUserSwapMouseButton(fSwap
);
470 ToAscii(UINT uVirtKey
,
472 CONST PBYTE lpKeyState
,
476 return ToAsciiEx(uVirtKey
, uScanCode
, lpKeyState
, lpChar
, uFlags
, 0);
484 ToAsciiEx(UINT uVirtKey
,
486 CONST PBYTE lpKeyState
,
494 Ret
= ToUnicodeEx(uVirtKey
, uScanCode
, lpKeyState
, UniChars
, 2, uFlags
, dwhkl
);
495 CharCount
= (Ret
< 0 ? 1 : Ret
);
496 WideCharToMultiByte(CP_ACP
, 0, UniChars
, CharCount
, (LPSTR
) lpChar
, 2, NULL
, NULL
);
506 ToUnicode(UINT wVirtKey
,
508 CONST PBYTE lpKeyState
,
513 return ToUnicodeEx( wVirtKey
, wScanCode
, lpKeyState
, pwszBuff
, cchBuff
,
522 ToUnicodeEx(UINT wVirtKey
,
524 CONST PBYTE lpKeyState
,
530 return NtUserToUnicodeEx( wVirtKey
, wScanCode
, lpKeyState
, pwszBuff
, cchBuff
,
539 UnloadKeyboardLayout(HKL hkl
)
550 UnregisterHotKey(HWND hWnd
,
553 return (BOOL
)NtUserUnregisterHotKey(hWnd
, id
);
565 if (IsDBCSLeadByte(ch
)) return -1;
567 MultiByteToWideChar(CP_ACP
, 0, &ch
, 1, &wChar
, 1);
568 return VkKeyScanW(wChar
);
576 VkKeyScanExA(CHAR ch
,
581 if (IsDBCSLeadByte(ch
)) return -1;
583 MultiByteToWideChar(CP_ACP
, 0, &ch
, 1, &wChar
, 1);
584 return VkKeyScanExW(wChar
, dwhkl
);
592 VkKeyScanExW(WCHAR ch
,
595 return (SHORT
) NtUserVkKeyScanEx((DWORD
) ch
,(DWORD
) dwhkl
,(DWORD
)NULL
);
605 return VkKeyScanExW(ch
, GetKeyboardLayout(0));
619 return NtUserSendInput(nInputs
, pInputs
, cbSize
);
623 * Private call for CSRSS
627 PrivateCsrssRegisterPrimitive(VOID
)
629 NtUserCallNoParam(NOPARAM_ROUTINE_REGISTER_PRIMITIVE
);
633 * Another private call for CSRSS
637 PrivateCsrssAcquireOrReleaseInputOwnership(BOOL Release
)
639 NtUserAcquireOrReleaseInputOwnership(Release
);
651 ULONG_PTR dwExtraInfo
)
657 Input
.type
= INPUT_KEYBOARD
;
659 Input
.ki
.wScan
= bScan
;
660 Input
.ki
.dwFlags
= dwFlags
;
662 Input
.ki
.dwExtraInfo
= dwExtraInfo
;
664 NtUserSendInput(1, &Input
, sizeof(INPUT
));
678 ULONG_PTR dwExtraInfo
)
682 Input
.type
= INPUT_MOUSE
;
685 Input
.mi
.mouseData
= dwData
;
686 Input
.mi
.dwFlags
= dwFlags
;
688 Input
.mi
.dwExtraInfo
= dwExtraInfo
;
690 NtUserSendInput(1, &Input
, sizeof(INPUT
));
694 /***********************************************************************
697 static WORD
get_key_state(void)
701 if (GetSystemMetrics( SM_SWAPBUTTON
))
703 if (GetAsyncKeyState(VK_RBUTTON
) & 0x80) ret
|= MK_LBUTTON
;
704 if (GetAsyncKeyState(VK_LBUTTON
) & 0x80) ret
|= MK_RBUTTON
;
708 if (GetAsyncKeyState(VK_LBUTTON
) & 0x80) ret
|= MK_LBUTTON
;
709 if (GetAsyncKeyState(VK_RBUTTON
) & 0x80) ret
|= MK_RBUTTON
;
711 if (GetAsyncKeyState(VK_MBUTTON
) & 0x80) ret
|= MK_MBUTTON
;
712 if (GetAsyncKeyState(VK_SHIFT
) & 0x80) ret
|= MK_SHIFT
;
713 if (GetAsyncKeyState(VK_CONTROL
) & 0x80) ret
|= MK_CONTROL
;
714 if (GetAsyncKeyState(VK_XBUTTON1
) & 0x80) ret
|= MK_XBUTTON1
;
715 if (GetAsyncKeyState(VK_XBUTTON2
) & 0x80) ret
|= MK_XBUTTON2
;
720 static void CALLBACK
TrackMouseEventProc(HWND hwndUnused
, UINT uMsg
, UINT_PTR idEvent
,
728 INT hoverwidth
= 0, hoverheight
= 0;
732 hwnd
= WindowFromPoint(pos
);
734 SystemParametersInfoA(SPI_GETMOUSEHOVERWIDTH
, 0, &hoverwidth
, 0);
735 SystemParametersInfoA(SPI_GETMOUSEHOVERHEIGHT
, 0, &hoverheight
, 0);
737 /* loop through tracking events we are processing */
738 while (i
< iTrackMax
) {
739 if (TrackingList
[i
].tme
.dwFlags
& TME_NONCLIENT
) {
746 /* see if this tracking event is looking for TME_LEAVE and that the */
747 /* mouse has left the window */
748 if (TrackingList
[i
].tme
.dwFlags
& TME_LEAVE
) {
749 if (TrackingList
[i
].tme
.hwndTrack
!= hwnd
) {
751 PostMessageA(TrackingList
[i
].tme
.hwndTrack
, WM_NCMOUSELEAVE
, 0, 0);
754 PostMessageA(TrackingList
[i
].tme
.hwndTrack
, WM_MOUSELEAVE
, 0, 0);
757 /* remove the TME_LEAVE flag */
758 TrackingList
[i
].tme
.dwFlags
^= TME_LEAVE
;
761 GetClientRect(hwnd
, &client
);
762 MapWindowPoints(hwnd
, NULL
, (LPPOINT
)&client
, 2);
763 if(PtInRect(&client
, pos
)) {
765 PostMessageA(TrackingList
[i
].tme
.hwndTrack
, WM_NCMOUSELEAVE
, 0, 0);
766 /* remove the TME_LEAVE flag */
767 TrackingList
[i
].tme
.dwFlags
^= TME_LEAVE
;
772 PostMessageA(TrackingList
[i
].tme
.hwndTrack
, WM_MOUSELEAVE
, 0, 0);
773 /* remove the TME_LEAVE flag */
774 TrackingList
[i
].tme
.dwFlags
^= TME_LEAVE
;
780 /* see if we are tracking hovering for this hwnd */
781 if(TrackingList
[i
].tme
.dwFlags
& TME_HOVER
) {
782 /* add the timer interval to the hovering time */
783 TrackingList
[i
].iHoverTime
+=iTimerInterval
;
785 /* has the cursor moved outside the rectangle centered around pos? */
786 if((abs(pos
.x
- TrackingList
[i
].pos
.x
) > (hoverwidth
/ 2.0))
787 || (abs(pos
.y
- TrackingList
[i
].pos
.y
) > (hoverheight
/ 2.0)))
789 /* record this new position as the current position and reset */
790 /* the iHoverTime variable to 0 */
791 TrackingList
[i
].pos
= pos
;
792 TrackingList
[i
].iHoverTime
= 0;
795 /* has the mouse hovered long enough? */
796 if(TrackingList
[i
].iHoverTime
<= TrackingList
[i
].tme
.dwHoverTime
)
800 ScreenToClient(hwnd
, &posClient
);
802 PostMessageW(TrackingList
[i
].tme
.hwndTrack
, WM_NCMOUSEHOVER
,
803 get_key_state(), MAKELPARAM( posClient
.x
, posClient
.y
));
806 PostMessageW(TrackingList
[i
].tme
.hwndTrack
, WM_MOUSEHOVER
,
807 get_key_state(), MAKELPARAM( posClient
.x
, posClient
.y
));
810 /* stop tracking mouse hover */
811 TrackingList
[i
].tme
.dwFlags
^= TME_HOVER
;
815 /* see if we are still tracking TME_HOVER or TME_LEAVE for this entry */
816 if((TrackingList
[i
].tme
.dwFlags
& TME_HOVER
) ||
817 (TrackingList
[i
].tme
.dwFlags
& TME_LEAVE
)) {
819 } else { /* remove this entry from the tracking list */
820 TrackingList
[i
] = TrackingList
[--iTrackMax
];
824 /* stop the timer if the tracking list is empty */
832 /***********************************************************************
833 * TrackMouseEvent [USER32]
835 * Requests notification of mouse events
837 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
838 * to the hwnd specified in the ptme structure. After the event message
839 * is posted to the hwnd, the entry in the queue is removed.
841 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
842 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
843 * immediately and the TME_LEAVE flag being ignored.
846 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
859 LPTRACKMOUSEEVENT ptme
)
863 BOOL cancel
= 0, hover
= 0, leave
= 0, query
= 0, nonclient
= 0, inclient
= 0;
871 SetRectEmpty(&client
);
873 DPRINT("%lx, %lx, %p, %lx\n", ptme
->cbSize
, ptme
->dwFlags
, ptme
->hwndTrack
, ptme
->dwHoverTime
);
875 if (ptme
->cbSize
!= sizeof(TRACKMOUSEEVENT
)) {
876 DPRINT("wrong TRACKMOUSEEVENT size from app\n");
877 SetLastError(ERROR_INVALID_PARAMETER
); /* FIXME not sure if this is correct */
881 flags
= ptme
->dwFlags
;
883 /* if HOVER_DEFAULT was specified replace this with the systems current value */
884 if(ptme
->dwHoverTime
== HOVER_DEFAULT
)
885 SystemParametersInfoA(SPI_GETMOUSEHOVERTIME
, 0, &(ptme
->dwHoverTime
), 0);
888 hwnd
= WindowFromPoint(pos
);
890 if ( flags
& TME_CANCEL
) {
891 flags
&= ~ TME_CANCEL
;
895 if ( flags
& TME_HOVER
) {
896 flags
&= ~ TME_HOVER
;
900 if ( flags
& TME_LEAVE
) {
901 flags
&= ~ TME_LEAVE
;
905 if ( flags
& TME_NONCLIENT
) {
906 flags
&= ~ TME_NONCLIENT
;
910 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
911 if ( flags
& TME_QUERY
) {
912 flags
&= ~ TME_QUERY
;
916 /* Find the tracking list entry with the matching hwnd */
917 while((i
< iTrackMax
) && (TrackingList
[i
].tme
.hwndTrack
!= ptme
->hwndTrack
)) {
921 /* hwnd found, fill in the ptme struct */
923 *ptme
= TrackingList
[i
].tme
;
927 return TRUE
; /* return here, TME_QUERY is retrieving information */
931 DPRINT("Unknown flag(s) %08lx\n", flags
);
934 /* find a matching hwnd if one exists */
937 while((i
< iTrackMax
) && (TrackingList
[i
].tme
.hwndTrack
!= ptme
->hwndTrack
)) {
942 TrackingList
[i
].tme
.dwFlags
&= ~(ptme
->dwFlags
& ~TME_CANCEL
);
944 /* if we aren't tracking on hover or leave remove this entry */
945 if(!((TrackingList
[i
].tme
.dwFlags
& TME_HOVER
) ||
946 (TrackingList
[i
].tme
.dwFlags
& TME_LEAVE
)))
948 TrackingList
[i
] = TrackingList
[--iTrackMax
];
957 /* see if hwndTrack isn't the current window */
958 if(ptme
->hwndTrack
!= hwnd
) {
961 PostMessageA(ptme
->hwndTrack
, WM_NCMOUSELEAVE
, 0, 0);
964 PostMessageA(ptme
->hwndTrack
, WM_MOUSELEAVE
, 0, 0);
968 GetClientRect(ptme
->hwndTrack
, &client
);
969 MapWindowPoints(ptme
->hwndTrack
, NULL
, (LPPOINT
)&client
, 2);
970 if(PtInRect(&client
, pos
)) {
973 if(nonclient
&& inclient
) {
974 PostMessageA(ptme
->hwndTrack
, WM_NCMOUSELEAVE
, 0, 0);
977 else if(!nonclient
&& !inclient
) {
978 PostMessageA(ptme
->hwndTrack
, WM_MOUSELEAVE
, 0, 0);
982 /* See if this hwnd is already being tracked and update the tracking flags */
983 for(i
= 0; i
< iTrackMax
; i
++) {
984 if(TrackingList
[i
].tme
.hwndTrack
== ptme
->hwndTrack
) {
985 TrackingList
[i
].tme
.dwFlags
= 0;
988 TrackingList
[i
].tme
.dwFlags
|= TME_HOVER
;
989 TrackingList
[i
].tme
.dwHoverTime
= ptme
->dwHoverTime
;
993 TrackingList
[i
].tme
.dwFlags
|= TME_LEAVE
;
996 TrackingList
[i
].tme
.dwFlags
|= TME_NONCLIENT
;
998 /* reset iHoverTime as per winapi specs */
999 TrackingList
[i
].iHoverTime
= 0;
1005 /* if the tracking list is full return FALSE */
1006 if (iTrackMax
== sizeof (TrackingList
) / sizeof(*TrackingList
)) {
1010 /* Adding new mouse event to the tracking list */
1011 TrackingList
[iTrackMax
].tme
= *ptme
;
1013 /* Initialize HoverInfo variables even if not hover tracking */
1014 TrackingList
[iTrackMax
].iHoverTime
= 0;
1015 TrackingList
[iTrackMax
].pos
= pos
;
1020 timer
= SetTimer(0, 0, iTimerInterval
, TrackMouseEventProc
);