2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Win32k subsystem
4 * PURPOSE: General input functions
5 * FILE: win32ss/user/ntuser/input.c
6 * PROGRAMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * Rafal Harabien (rafalh@reactos.org)
11 DBG_DEFAULT_CHANNEL(UserInput
);
13 /* GLOBALS *******************************************************************/
15 PTHREADINFO ptiRawInput
;
16 PKTIMER MasterTimer
= NULL
;
17 PATTACHINFO gpai
= NULL
;
19 HANDLE ghKeyboardDevice
= NULL
;
21 static DWORD LastInputTick
= 0;
22 static HANDLE ghMouseDevice
;
24 /* FUNCTIONS *****************************************************************/
29 * Updates or gets last input tick count
32 IntLastInputTick(BOOL bUpdate
)
36 LARGE_INTEGER TickCount
;
37 KeQueryTickCount(&TickCount
);
38 LastInputTick
= MsqCalculateMessageTime(&TickCount
);
39 if (gpsi
) gpsi
->dwLastRITEventTickCount
= LastInputTick
;
47 * Check if scrensaver should be started and sends message to SAS window
50 DoTheScreenSaver(VOID
)
52 LARGE_INTEGER TickCount
;
55 if (gspv
.iScrSaverTimeout
> 0) // Zero means Off.
57 KeQueryTickCount(&TickCount
);
58 Test
= MsqCalculateMessageTime(&TickCount
);
59 Test
= Test
- LastInputTick
;
60 TO
= 1000 * gspv
.iScrSaverTimeout
;
63 TRACE("Screensaver Message Start! Tick %lu Timeout %d \n", Test
, gspv
.iScrSaverTimeout
);
65 if (ppiScrnSaver
) // We are or we are not the screensaver, prevent reentry...
67 if (!(ppiScrnSaver
->W32PF_flags
& W32PF_IDLESCREENSAVER
))
69 ppiScrnSaver
->W32PF_flags
|= W32PF_IDLESCREENSAVER
;
70 ERR("Screensaver is Idle\n");
75 PUSER_MESSAGE_QUEUE ForegroundQueue
= IntGetFocusMessageQueue();
76 if (ForegroundQueue
&& ForegroundQueue
->spwndActive
)
77 UserPostMessage(hwndSAS
, WM_LOGONNOTIFY
, LN_START_SCREENSAVE
, 1); // lParam 1 == Secure
79 UserPostMessage(hwndSAS
, WM_LOGONNOTIFY
, LN_START_SCREENSAVE
, 0);
88 * Opens input device for asynchronous access
92 OpenInputDevice(PHANDLE pHandle
, PFILE_OBJECT
*ppObject
, CONST WCHAR
*pszDeviceName
)
94 UNICODE_STRING DeviceName
;
95 OBJECT_ATTRIBUTES ObjectAttributes
;
99 RtlInitUnicodeString(&DeviceName
, pszDeviceName
);
101 InitializeObjectAttributes(&ObjectAttributes
,
107 Status
= ZwOpenFile(pHandle
,
113 if (NT_SUCCESS(Status
) && ppObject
)
115 Status
= ObReferenceObjectByHandle(*pHandle
, SYNCHRONIZE
, NULL
, KernelMode
, (PVOID
*)ppObject
, NULL
);
116 ASSERT(NT_SUCCESS(Status
));
125 * Reads data from input devices and supports win32 timers
128 RawInputThreadMain(VOID
)
130 NTSTATUS MouStatus
= STATUS_UNSUCCESSFUL
, KbdStatus
= STATUS_UNSUCCESSFUL
, Status
;
131 IO_STATUS_BLOCK MouIosb
, KbdIosb
;
132 PFILE_OBJECT pKbdDevice
= NULL
, pMouDevice
= NULL
;
133 LARGE_INTEGER ByteOffset
;
134 //LARGE_INTEGER WaitTimeout;
135 PVOID WaitObjects
[4], pSignaledObject
= NULL
;
136 KWAIT_BLOCK WaitBlockArray
[RTL_NUMBER_OF(WaitObjects
)];
137 ULONG cWaitObjects
= 0, cMaxWaitObjects
= 2;
138 MOUSE_INPUT_DATA MouseInput
;
139 KEYBOARD_INPUT_DATA KeyInput
;
143 ByteOffset
.QuadPart
= (LONGLONG
)0;
144 //WaitTimeout.QuadPart = (LONGLONG)(-10000000);
146 ptiRawInput
= GetW32ThreadInfo();
147 ptiRawInput
->TIF_flags
|= TIF_SYSTEMTHREAD
;
148 ptiRawInput
->pClientInfo
->dwTIFlags
= ptiRawInput
->TIF_flags
;
150 TRACE("Raw Input Thread %p\n", ptiRawInput
);
152 KeSetPriorityThread(&PsGetCurrentThread()->Tcb
,
153 LOW_REALTIME_PRIORITY
+ 3);
155 Status
= ObOpenObjectByPointer(InputWindowStation
,
159 ExWindowStationObjectType
,
162 if (NT_SUCCESS(Status
))
164 UserSetProcessWindowStation(hWinSta
);
169 /* Failed to open the interactive winsta! What now? */
172 UserEnterExclusive();
176 NT_ASSERT(ghMouseDevice
== NULL
);
177 NT_ASSERT(ghKeyboardDevice
== NULL
);
179 PoRequestShutdownEvent(&ShutdownEvent
);
184 /* Check if mouse device already exists */
185 Status
= OpenInputDevice(&ghMouseDevice
, &pMouDevice
, L
"\\Device\\PointerClass0" );
186 if (NT_SUCCESS(Status
))
189 TRACE("Mouse connected!\n");
192 if (!ghKeyboardDevice
)
194 /* Check if keyboard device already exists */
195 Status
= OpenInputDevice(&ghKeyboardDevice
, &pKbdDevice
, L
"\\Device\\KeyboardClass0");
196 if (NT_SUCCESS(Status
))
199 TRACE("Keyboard connected!\n");
200 // Get and load keyboard attributes.
201 UserInitKeyboard(ghKeyboardDevice
);
202 UserEnterExclusive();
203 // Register the Window hotkey.
204 UserRegisterHotKey(PWND_BOTTOM
, IDHK_WINKEY
, MOD_WIN
, 0);
205 // Register the debug hotkeys.
211 /* Reset WaitHandles array */
213 WaitObjects
[cWaitObjects
++] = ShutdownEvent
;
214 WaitObjects
[cWaitObjects
++] = MasterTimer
;
218 /* Try to read from mouse if previous reading is not pending */
219 if (MouStatus
!= STATUS_PENDING
)
221 MouStatus
= ZwReadFile(ghMouseDevice
,
227 sizeof(MOUSE_INPUT_DATA
),
232 if (MouStatus
== STATUS_PENDING
)
233 WaitObjects
[cWaitObjects
++] = &pMouDevice
->Event
;
236 if (ghKeyboardDevice
)
238 /* Try to read from keyboard if previous reading is not pending */
239 if (KbdStatus
!= STATUS_PENDING
)
241 KbdStatus
= ZwReadFile(ghKeyboardDevice
,
247 sizeof(KEYBOARD_INPUT_DATA
),
252 if (KbdStatus
== STATUS_PENDING
)
253 WaitObjects
[cWaitObjects
++] = &pKbdDevice
->Event
;
256 /* If all objects are pending, wait for them */
257 if (cWaitObjects
== cMaxWaitObjects
)
259 Status
= KeWaitForMultipleObjects(cWaitObjects
,
268 if ((Status
>= STATUS_WAIT_0
) &&
269 (Status
< (STATUS_WAIT_0
+ (LONG
)cWaitObjects
)))
271 /* Some device has finished reading */
272 pSignaledObject
= WaitObjects
[Status
- STATUS_WAIT_0
];
274 /* Check if it is mouse or keyboard and update status */
275 if ((MouStatus
== STATUS_PENDING
) &&
276 (pSignaledObject
== &pMouDevice
->Event
))
278 MouStatus
= MouIosb
.Status
;
280 else if ((KbdStatus
== STATUS_PENDING
) &&
281 (pSignaledObject
== &pKbdDevice
->Event
))
283 KbdStatus
= KbdIosb
.Status
;
285 else if (pSignaledObject
== MasterTimer
)
289 else if (pSignaledObject
== ShutdownEvent
)
297 /* Have we successed reading from mouse? */
298 if (NT_SUCCESS(MouStatus
) && MouStatus
!= STATUS_PENDING
)
300 TRACE("MouseEvent\n");
302 /* Set LastInputTick */
303 IntLastInputTick(TRUE
);
306 UserEnterExclusive();
307 UserProcessMouseInput(&MouseInput
);
310 else if (MouStatus
!= STATUS_PENDING
)
311 ERR("Failed to read from mouse: %x.\n", MouStatus
);
313 /* Have we successed reading from keyboard? */
314 if (NT_SUCCESS(KbdStatus
) && KbdStatus
!= STATUS_PENDING
)
316 TRACE("KeyboardEvent: %s %04x\n",
317 (KeyInput
.Flags
& KEY_BREAK
) ? "up" : "down",
320 /* Set LastInputTick */
321 IntLastInputTick(TRUE
);
324 UserEnterExclusive();
325 UserProcessKeyboardInput(&KeyInput
);
328 else if (KbdStatus
!= STATUS_PENDING
)
329 ERR("Failed to read from keyboard: %x.\n", KbdStatus
);
334 (void)ZwCancelIoFile(ghMouseDevice
, &MouIosb
);
335 ObCloseHandle(ghMouseDevice
, KernelMode
);
336 ObDereferenceObject(pMouDevice
);
337 ghMouseDevice
= NULL
;
340 if (ghKeyboardDevice
)
342 (void)ZwCancelIoFile(ghKeyboardDevice
, &KbdIosb
);
343 ObCloseHandle(ghKeyboardDevice
, KernelMode
);
344 ObDereferenceObject(pKbdDevice
);
345 ghKeyboardDevice
= NULL
;
348 ERR("Raw Input Thread Exit!\n");
354 * Inits input implementation
361 MasterTimer
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(KTIMER
), USERTAG_SYSTEM
);
364 ERR("Failed to allocate memory\n");
366 return STATUS_UNSUCCESSFUL
;
368 KeInitializeTimer(MasterTimer
);
370 return STATUS_SUCCESS
;
374 IntBlockInput(PTHREADINFO pti
, BOOL BlockIt
)
376 PTHREADINFO OldBlock
;
379 if(!pti
->rpdesk
|| ((pti
->TIF_flags
& TIF_INCLEANUP
) && BlockIt
))
382 * Fail blocking if exiting the thread
389 * FIXME: Check access rights of the window station
390 * e.g. services running in the service window station cannot block input
392 if(!ThreadHasInputAccess(pti
) ||
393 !IntIsActiveDesktop(pti
->rpdesk
))
395 EngSetLastError(ERROR_ACCESS_DENIED
);
400 OldBlock
= pti
->rpdesk
->BlockInputThread
;
405 EngSetLastError(ERROR_ACCESS_DENIED
);
408 pti
->rpdesk
->BlockInputThread
= (BlockIt
? pti
: NULL
);
409 return OldBlock
== NULL
;
412 pti
->rpdesk
->BlockInputThread
= (BlockIt
? pti
: NULL
);
413 return OldBlock
== NULL
;
423 TRACE("Enter NtUserBlockInput\n");
424 UserEnterExclusive();
426 ret
= IntBlockInput(PsGetCurrentThreadWin32Thread(), BlockIt
);
429 TRACE("Leave NtUserBlockInput, ret=%i\n", ret
);
436 IsRemoveAttachThread(PTHREADINFO pti
)
441 PTHREADINFO ptiFrom
= NULL
, ptiTo
= NULL
;
445 if (!gpai
) return TRUE
;
447 pai
= gpai
; // Bottom of the list.
451 if (pai
->pti2
== pti
)
457 if (pai
->pti1
== pti
)
467 if (!pai
&& !ptiFrom
&& !ptiTo
) break;
469 Status
= UserAttachThreadInput(ptiFrom
, ptiTo
, FALSE
);
470 if (!NT_SUCCESS(Status
)) Ret
= FALSE
;
478 UserAttachThreadInput(PTHREADINFO ptiFrom
, PTHREADINFO ptiTo
, BOOL fAttach
)
482 PCURICON_OBJECT CurIcon
;
484 /* Can not be the same thread. */
485 if (ptiFrom
== ptiTo
) return STATUS_INVALID_PARAMETER
;
487 /* Do not attach to system threads or between different desktops. */
488 if (ptiFrom
->TIF_flags
& TIF_DONTATTACHQUEUE
||
489 ptiTo
->TIF_flags
& TIF_DONTATTACHQUEUE
||
490 ptiFrom
->rpdesk
!= ptiTo
->rpdesk
)
491 return STATUS_ACCESS_DENIED
;
494 Keyboard and mouse events received by both threads are processed by the thread specified by the idAttachTo.
497 /* If Attach set, allocate and link. */
500 pai
= ExAllocatePoolWithTag(PagedPool
, sizeof(ATTACHINFO
), USERTAG_ATTACHINFO
);
501 if (!pai
) return STATUS_NO_MEMORY
;
508 ERR("Attach Allocated! ptiFrom 0x%p ptiTo 0x%p paiCount %d\n",ptiFrom
,ptiTo
,paiCount
);
510 if (ptiTo
->MessageQueue
!= ptiFrom
->MessageQueue
)
513 ptiTo
->MessageQueue
->iCursorLevel
-= ptiFrom
->iCursorLevel
;
515 if (ptiFrom
->MessageQueue
== gpqForeground
)
517 ERR("ptiFrom is Foreground\n");
518 ptiTo
->MessageQueue
->spwndActive
= ptiFrom
->MessageQueue
->spwndActive
;
519 ptiTo
->MessageQueue
->spwndFocus
= ptiFrom
->MessageQueue
->spwndFocus
;
520 ptiTo
->MessageQueue
->spwndCapture
= ptiFrom
->MessageQueue
->spwndCapture
;
521 ptiTo
->MessageQueue
->QF_flags
^= ((ptiTo
->MessageQueue
->QF_flags
^ ptiFrom
->MessageQueue
->QF_flags
) & QF_CAPTURELOCKED
);
522 RtlCopyMemory(&ptiTo
->MessageQueue
->CaretInfo
,
523 &ptiFrom
->MessageQueue
->CaretInfo
,
524 sizeof(ptiTo
->MessageQueue
->CaretInfo
));
525 IntSetFocusMessageQueue(NULL
);
526 IntSetFocusMessageQueue(ptiTo
->MessageQueue
);
527 gptiForeground
= ptiTo
;
531 ERR("ptiFrom NOT Foreground\n");
532 if ( ptiTo
->MessageQueue
->spwndActive
== 0 )
533 ptiTo
->MessageQueue
->spwndActive
= ptiFrom
->MessageQueue
->spwndActive
;
534 if ( ptiTo
->MessageQueue
->spwndFocus
== 0 )
535 ptiTo
->MessageQueue
->spwndFocus
= ptiFrom
->MessageQueue
->spwndFocus
;
538 CurIcon
= ptiFrom
->MessageQueue
->CursorObject
;
540 MsqDestroyMessageQueue(ptiFrom
);
544 // Could be global. Keep it above the water line!
545 UserReferenceObject(CurIcon
);
548 if (CurIcon
&& UserObjectInDestroy(UserHMGetHandle(CurIcon
)))
550 UserDereferenceObject(CurIcon
);
554 ptiFrom
->MessageQueue
= ptiTo
->MessageQueue
;
556 // Pass cursor From if To is null. Pass test_SetCursor parent_id == current pti ID.
557 if (CurIcon
&& ptiTo
->MessageQueue
->CursorObject
== NULL
)
559 ERR("ptiTo receiving ptiFrom Cursor\n");
560 ptiTo
->MessageQueue
->CursorObject
= CurIcon
;
563 ptiFrom
->MessageQueue
->cThreads
++;
564 ERR("ptiTo S Share count %u\n", ptiFrom
->MessageQueue
->cThreads
);
566 IntReferenceMessageQueue(ptiTo
->MessageQueue
);
570 ERR("Attach Threads are already associated!\n");
573 else /* If clear, unlink and free it. */
578 if (!gpai
) return STATUS_INVALID_PARAMETER
;
580 /* Search list and free if found or return false. */
582 while (*ppai
!= NULL
)
584 if ( (*ppai
)->pti2
== ptiTo
&& (*ppai
)->pti1
== ptiFrom
)
587 /* Remove it from the list */
588 *ppai
= (*ppai
)->paiNext
;
589 ExFreePoolWithTag(pai
, USERTAG_ATTACHINFO
);
594 ppai
= &((*ppai
)->paiNext
);
597 if (!Hit
) return STATUS_INVALID_PARAMETER
;
599 ERR("Attach Free! ptiFrom 0x%p ptiTo 0x%p paiCount %d\n",ptiFrom
,ptiTo
,paiCount
);
601 if (ptiTo
->MessageQueue
== ptiFrom
->MessageQueue
)
603 PWND spwndActive
= ptiTo
->MessageQueue
->spwndActive
;
604 PWND spwndFocus
= ptiTo
->MessageQueue
->spwndFocus
;
606 if (gptiForeground
== ptiFrom
)
608 ERR("ptiTo is now pti FG.\n");
609 // MessageQueue foreground is set so switch threads.
610 gptiForeground
= ptiTo
;
612 ptiTo
->MessageQueue
->cThreads
--;
613 ERR("ptiTo E Share count %u\n", ptiTo
->MessageQueue
->cThreads
);
614 ASSERT(ptiTo
->MessageQueue
->cThreads
>= 1);
616 IntDereferenceMessageQueue(ptiTo
->MessageQueue
);
618 ptiFrom
->MessageQueue
= MsqCreateMessageQueue(ptiFrom
);
622 if (spwndActive
->head
.pti
== ptiFrom
)
624 ptiFrom
->MessageQueue
->spwndActive
= spwndActive
;
625 ptiTo
->MessageQueue
->spwndActive
= 0;
630 if (spwndFocus
->head
.pti
== ptiFrom
)
632 ptiFrom
->MessageQueue
->spwndFocus
= spwndFocus
;
633 ptiTo
->MessageQueue
->spwndFocus
= 0;
636 ptiTo
->MessageQueue
->iCursorLevel
-= ptiFrom
->iCursorLevel
;
640 ERR("Detaching Threads are not associated!\n");
643 /* Note that key state, which can be ascertained by calls to the GetKeyState
644 or GetKeyboardState function, is reset after a call to AttachThreadInput.
647 RtlCopyMemory(ptiTo
->MessageQueue
->afKeyState
, gafAsyncKeyState
, sizeof(gafAsyncKeyState
));
649 ptiTo
->MessageQueue
->msgDblClk
.message
= 0;
651 /* Generate mouse move message */
652 msg
.message
= WM_MOUSEMOVE
;
653 msg
.wParam
= UserGetMouseButtonsState();
654 msg
.lParam
= MAKELPARAM(gpsi
->ptCursor
.x
, gpsi
->ptCursor
.y
);
655 msg
.pt
= gpsi
->ptCursor
;
656 co_MsqInsertMouseMessage(&msg
, 0, 0, TRUE
);
658 return STATUS_SUCCESS
;
663 NtUserAttachThreadInput(
669 PTHREADINFO pti
, ptiTo
;
672 UserEnterExclusive();
673 TRACE("Enter NtUserAttachThreadInput %s\n",(fAttach
? "TRUE" : "FALSE" ));
675 pti
= IntTID2PTI(UlongToHandle(idAttach
));
676 ptiTo
= IntTID2PTI(UlongToHandle(idAttachTo
));
678 if ( !pti
|| !ptiTo
)
680 TRACE("AttachThreadInput pti or ptiTo NULL.\n");
681 EngSetLastError(ERROR_INVALID_PARAMETER
);
685 Status
= UserAttachThreadInput( pti
, ptiTo
, fAttach
);
686 if (!NT_SUCCESS(Status
))
688 TRACE("AttachThreadInput Error Status 0x%x. \n",Status
);
689 EngSetLastError(RtlNtStatusToDosError(Status
));
694 TRACE("Leave NtUserAttachThreadInput, ret=%d\n",Ret
);
702 * Generates input events from software
714 TRACE("Enter NtUserSendInput\n");
715 UserEnterExclusive();
717 pti
= PsGetCurrentThreadWin32Thread();
725 if (!nInputs
|| !pInput
|| cbSize
!= sizeof(INPUT
))
727 EngSetLastError(ERROR_INVALID_PARAMETER
);
732 * FIXME: Check access rights of the window station
733 * e.g. services running in the service window station cannot block input
735 if (!ThreadHasInputAccess(pti
) ||
736 !IntIsActiveDesktop(pti
->rpdesk
))
738 EngSetLastError(ERROR_ACCESS_DENIED
);
747 Status
= MmCopyFromCaller(&SafeInput
, pInput
++, sizeof(INPUT
));
748 if (!NT_SUCCESS(Status
))
750 SetLastNtError(Status
);
754 switch (SafeInput
.type
)
757 if (UserSendMouseInput(&SafeInput
.mi
, TRUE
))
761 if (UserSendKeyboardInput(&SafeInput
.ki
, TRUE
))
765 FIXME("INPUT_HARDWARE not supported!");
768 ERR("SendInput(): Invalid input type: 0x%x\n", SafeInput
.type
);
774 TRACE("Leave NtUserSendInput, ret=%u\n", uRet
);