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
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
[3], pSignaledObject
= NULL
;
136 ULONG cWaitObjects
= 0, cMaxWaitObjects
= 1;
137 MOUSE_INPUT_DATA MouseInput
;
138 KEYBOARD_INPUT_DATA KeyInput
;
140 ByteOffset
.QuadPart
= (LONGLONG
)0;
141 //WaitTimeout.QuadPart = (LONGLONG)(-10000000);
143 ptiRawInput
= GetW32ThreadInfo();
144 ptiRawInput
->TIF_flags
|= TIF_SYSTEMTHREAD
;
145 ptiRawInput
->pClientInfo
->dwTIFlags
= ptiRawInput
->TIF_flags
;
147 TRACE("Raw Input Thread %p\n", ptiRawInput
);
149 KeSetPriorityThread(&PsGetCurrentThread()->Tcb
,
150 LOW_REALTIME_PRIORITY
+ 3);
152 UserEnterExclusive();
156 NT_ASSERT(ghMouseDevice
== NULL
);
157 NT_ASSERT(ghKeyboardDevice
== NULL
);
163 /* Check if mouse device already exists */
164 Status
= OpenInputDevice(&ghMouseDevice
, &pMouDevice
, L
"\\Device\\PointerClass0" );
165 if (NT_SUCCESS(Status
))
168 TRACE("Mouse connected!\n");
171 if (!ghKeyboardDevice
)
173 /* Check if keyboard device already exists */
174 Status
= OpenInputDevice(&ghKeyboardDevice
, &pKbdDevice
, L
"\\Device\\KeyboardClass0");
175 if (NT_SUCCESS(Status
))
178 TRACE("Keyboard connected!\n");
179 // Get and load keyboard attributes.
180 UserInitKeyboard(ghKeyboardDevice
);
181 UserEnterExclusive();
182 // Register the Window hotkey.
183 UserRegisterHotKey(PWND_BOTTOM
, IDHK_WINKEY
, MOD_WIN
, 0);
184 // Register the debug hotkeys.
190 /* Reset WaitHandles array */
192 WaitObjects
[cWaitObjects
++] = MasterTimer
;
196 /* Try to read from mouse if previous reading is not pending */
197 if (MouStatus
!= STATUS_PENDING
)
199 MouStatus
= ZwReadFile(ghMouseDevice
,
205 sizeof(MOUSE_INPUT_DATA
),
210 if (MouStatus
== STATUS_PENDING
)
211 WaitObjects
[cWaitObjects
++] = &pMouDevice
->Event
;
214 if (ghKeyboardDevice
)
216 /* Try to read from keyboard if previous reading is not pending */
217 if (KbdStatus
!= STATUS_PENDING
)
219 KbdStatus
= ZwReadFile(ghKeyboardDevice
,
225 sizeof(KEYBOARD_INPUT_DATA
),
230 if (KbdStatus
== STATUS_PENDING
)
231 WaitObjects
[cWaitObjects
++] = &pKbdDevice
->Event
;
234 /* If all objects are pending, wait for them */
235 if (cWaitObjects
== cMaxWaitObjects
)
237 Status
= KeWaitForMultipleObjects(cWaitObjects
,
246 if ((Status
>= STATUS_WAIT_0
) &&
247 (Status
< (STATUS_WAIT_0
+ (LONG
)cWaitObjects
)))
249 /* Some device has finished reading */
250 pSignaledObject
= WaitObjects
[Status
- STATUS_WAIT_0
];
252 /* Check if it is mouse or keyboard and update status */
253 if ((MouStatus
== STATUS_PENDING
) &&
254 (pSignaledObject
== &pMouDevice
->Event
))
256 MouStatus
= MouIosb
.Status
;
258 else if ((KbdStatus
== STATUS_PENDING
) &&
259 (pSignaledObject
== &pKbdDevice
->Event
))
261 KbdStatus
= KbdIosb
.Status
;
263 else if (pSignaledObject
== MasterTimer
)
271 /* Have we successed reading from mouse? */
272 if (NT_SUCCESS(MouStatus
) && MouStatus
!= STATUS_PENDING
)
274 TRACE("MouseEvent\n");
276 /* Set LastInputTick */
277 IntLastInputTick(TRUE
);
280 UserEnterExclusive();
281 UserProcessMouseInput(&MouseInput
);
284 else if (MouStatus
!= STATUS_PENDING
)
285 ERR("Failed to read from mouse: %x.\n", MouStatus
);
287 /* Have we successed reading from keyboard? */
288 if (NT_SUCCESS(KbdStatus
) && KbdStatus
!= STATUS_PENDING
)
290 TRACE("KeyboardEvent: %s %04x\n",
291 (KeyInput
.Flags
& KEY_BREAK
) ? "up" : "down",
294 /* Set LastInputTick */
295 IntLastInputTick(TRUE
);
298 UserEnterExclusive();
299 UserProcessKeyboardInput(&KeyInput
);
302 else if (KbdStatus
!= STATUS_PENDING
)
303 ERR("Failed to read from keyboard: %x.\n", KbdStatus
);
305 ERR("Raw Input Thread Exit!\n");
309 * CreateSystemThreads
311 * Called form dedicated thread in CSRSS. RIT is started in context of this
312 * thread because it needs valid Win32 process with TEB initialized.
315 CreateSystemThreads(UINT Type
)
321 case 0: RawInputThreadMain(); break;
322 case 1: DesktopThreadMain(); break;
323 default: ERR("Wrong type: %x\n", Type
);
334 * Inits input implementation
341 MasterTimer
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(KTIMER
), USERTAG_SYSTEM
);
344 ERR("Failed to allocate memory\n");
346 return STATUS_UNSUCCESSFUL
;
348 KeInitializeTimer(MasterTimer
);
350 return STATUS_SUCCESS
;
354 IntBlockInput(PTHREADINFO pti
, BOOL BlockIt
)
356 PTHREADINFO OldBlock
;
359 if(!pti
->rpdesk
|| ((pti
->TIF_flags
& TIF_INCLEANUP
) && BlockIt
))
362 * Fail blocking if exiting the thread
369 * FIXME: Check access rights of the window station
370 * e.g. services running in the service window station cannot block input
372 if(!ThreadHasInputAccess(pti
) ||
373 !IntIsActiveDesktop(pti
->rpdesk
))
375 EngSetLastError(ERROR_ACCESS_DENIED
);
380 OldBlock
= pti
->rpdesk
->BlockInputThread
;
385 EngSetLastError(ERROR_ACCESS_DENIED
);
388 pti
->rpdesk
->BlockInputThread
= (BlockIt
? pti
: NULL
);
389 return OldBlock
== NULL
;
392 pti
->rpdesk
->BlockInputThread
= (BlockIt
? pti
: NULL
);
393 return OldBlock
== NULL
;
403 TRACE("Enter NtUserBlockInput\n");
404 UserEnterExclusive();
406 ret
= IntBlockInput(PsGetCurrentThreadWin32Thread(), BlockIt
);
409 TRACE("Leave NtUserBlockInput, ret=%i\n", ret
);
416 IsRemoveAttachThread(PTHREADINFO pti
)
421 PTHREADINFO ptiFrom
= NULL
, ptiTo
= NULL
;
425 if (!gpai
) return TRUE
;
427 pai
= gpai
; // Bottom of the list.
431 if (pai
->pti2
== pti
)
437 if (pai
->pti1
== pti
)
447 if (!pai
&& !ptiFrom
&& !ptiTo
) break;
449 Status
= UserAttachThreadInput(ptiFrom
, ptiTo
, FALSE
);
450 if (!NT_SUCCESS(Status
)) Ret
= FALSE
;
458 UserAttachThreadInput(PTHREADINFO ptiFrom
, PTHREADINFO ptiTo
, BOOL fAttach
)
462 PCURICON_OBJECT CurIcon
;
464 /* Can not be the same thread. */
465 if (ptiFrom
== ptiTo
) return STATUS_INVALID_PARAMETER
;
467 /* Do not attach to system threads or between different desktops. */
468 if (ptiFrom
->TIF_flags
& TIF_DONTATTACHQUEUE
||
469 ptiTo
->TIF_flags
& TIF_DONTATTACHQUEUE
||
470 ptiFrom
->rpdesk
!= ptiTo
->rpdesk
)
471 return STATUS_ACCESS_DENIED
;
474 Keyboard and mouse events received by both threads are processed by the thread specified by the idAttachTo.
477 /* If Attach set, allocate and link. */
480 pai
= ExAllocatePoolWithTag(PagedPool
, sizeof(ATTACHINFO
), USERTAG_ATTACHINFO
);
481 if (!pai
) return STATUS_NO_MEMORY
;
488 ERR("Attach Allocated! ptiFrom 0x%p ptiTo 0x%p paiCount %d\n",ptiFrom
,ptiTo
,paiCount
);
490 if (ptiTo
->MessageQueue
!= ptiFrom
->MessageQueue
)
493 ptiTo
->MessageQueue
->iCursorLevel
-= ptiFrom
->iCursorLevel
;
495 // FIXME: conditions?
496 if (ptiTo
->MessageQueue
== gpqForeground
)
498 ERR("ptiTo is Foreground\n");
502 ERR("ptiTo NOT Foreground\n");
505 if (ptiFrom
->MessageQueue
== gpqForeground
)
507 ERR("ptiFrom is Foreground\n");
508 ptiTo
->MessageQueue
->spwndActive
= ptiFrom
->MessageQueue
->spwndActive
;
509 ptiTo
->MessageQueue
->spwndFocus
= ptiFrom
->MessageQueue
->spwndFocus
;
510 ptiTo
->MessageQueue
->spwndCapture
= ptiFrom
->MessageQueue
->spwndCapture
;
511 ptiTo
->MessageQueue
->QF_flags
^= ((ptiTo
->MessageQueue
->QF_flags
^ ptiFrom
->MessageQueue
->QF_flags
) & QF_CAPTURELOCKED
);
512 ptiTo
->MessageQueue
->CaretInfo
= ptiFrom
->MessageQueue
->CaretInfo
;
513 IntSetFocusMessageQueue(NULL
);
514 IntSetFocusMessageQueue(ptiTo
->MessageQueue
);
515 gptiForeground
= ptiTo
;
519 ERR("ptiFrom NOT Foreground\n");
522 CurIcon
= ptiFrom
->MessageQueue
->CursorObject
;
524 MsqDestroyMessageQueue(ptiFrom
);
526 if (CurIcon
&& UserObjectInDestroy(UserHMGetHandle(CurIcon
)))
531 ptiFrom
->MessageQueue
= ptiTo
->MessageQueue
;
533 // Pass cursor From if To is null. Pass test_SetCursor parent_id == current pti ID.
534 if (CurIcon
&& ptiTo
->MessageQueue
->CursorObject
== NULL
)
536 ptiTo
->MessageQueue
->CursorObject
= CurIcon
;
537 UserReferenceObject(CurIcon
);
540 ptiFrom
->MessageQueue
->cThreads
++;
541 ERR("ptiTo S Share count %u\n", ptiFrom
->MessageQueue
->cThreads
);
543 IntReferenceMessageQueue(ptiTo
->MessageQueue
);
547 ERR("Attach Threads are already associated!\n");
550 else /* If clear, unlink and free it. */
555 if (!gpai
) return STATUS_INVALID_PARAMETER
;
557 /* Search list and free if found or return false. */
559 while (*ppai
!= NULL
)
561 if ( (*ppai
)->pti2
== ptiTo
&& (*ppai
)->pti1
== ptiFrom
)
564 /* Remove it from the list */
565 *ppai
= (*ppai
)->paiNext
;
566 ExFreePoolWithTag(pai
, USERTAG_ATTACHINFO
);
571 ppai
= &((*ppai
)->paiNext
);
574 if (!Hit
) return STATUS_INVALID_PARAMETER
;
576 ERR("Attach Free! ptiFrom 0x%p ptiTo 0x%p paiCount %d\n",ptiFrom
,ptiTo
,paiCount
);
578 if (ptiTo
->MessageQueue
== ptiFrom
->MessageQueue
)
580 if (gptiForeground
== ptiFrom
)
582 ERR("ptiTo is now pti FG.\n");
583 // MessageQueue foreground is set so switch threads.
584 gptiForeground
= ptiTo
;
586 ptiTo
->MessageQueue
->cThreads
--;
587 ERR("ptiTo E Share count %u\n", ptiTo
->MessageQueue
->cThreads
);
588 ASSERT(ptiTo
->MessageQueue
->cThreads
>= 1);
590 IntDereferenceMessageQueue(ptiTo
->MessageQueue
);
592 ptiFrom
->MessageQueue
= MsqCreateMessageQueue(ptiFrom
);
594 ptiTo
->MessageQueue
->iCursorLevel
-= ptiFrom
->iCursorLevel
;
598 ERR("Detaching Threads are not associated!\n");
601 /* Note that key state, which can be ascertained by calls to the GetKeyState
602 or GetKeyboardState function, is reset after a call to AttachThreadInput.
605 RtlCopyMemory(ptiTo
->MessageQueue
->afKeyState
, gafAsyncKeyState
, sizeof(gafAsyncKeyState
));
607 /* Generate mouse move message */
608 msg
.message
= WM_MOUSEMOVE
;
609 msg
.wParam
= UserGetMouseButtonsState();
610 msg
.lParam
= MAKELPARAM(gpsi
->ptCursor
.x
, gpsi
->ptCursor
.y
);
611 msg
.pt
= gpsi
->ptCursor
;
612 co_MsqInsertMouseMessage(&msg
, 0, 0, TRUE
);
614 return STATUS_SUCCESS
;
619 NtUserAttachThreadInput(
625 PTHREADINFO pti
, ptiTo
;
628 UserEnterExclusive();
629 TRACE("Enter NtUserAttachThreadInput %s\n",(fAttach
? "TRUE" : "FALSE" ));
631 pti
= IntTID2PTI((HANDLE
)idAttach
);
632 ptiTo
= IntTID2PTI((HANDLE
)idAttachTo
);
634 if ( !pti
|| !ptiTo
)
636 TRACE("AttachThreadInput pti or ptiTo NULL.\n");
637 EngSetLastError(ERROR_INVALID_PARAMETER
);
641 Status
= UserAttachThreadInput( pti
, ptiTo
, fAttach
);
642 if (!NT_SUCCESS(Status
))
644 TRACE("AttachThreadInput Error Status 0x%x. \n",Status
);
645 EngSetLastError(RtlNtStatusToDosError(Status
));
650 TRACE("Leave NtUserAttachThreadInput, ret=%d\n",Ret
);
658 * Generates input events from software
670 TRACE("Enter NtUserSendInput\n");
671 UserEnterExclusive();
673 pti
= PsGetCurrentThreadWin32Thread();
681 if (!nInputs
|| !pInput
|| cbSize
!= sizeof(INPUT
))
683 EngSetLastError(ERROR_INVALID_PARAMETER
);
688 * FIXME: Check access rights of the window station
689 * e.g. services running in the service window station cannot block input
691 if (!ThreadHasInputAccess(pti
) ||
692 !IntIsActiveDesktop(pti
->rpdesk
))
694 EngSetLastError(ERROR_ACCESS_DENIED
);
703 Status
= MmCopyFromCaller(&SafeInput
, pInput
++, sizeof(INPUT
));
704 if (!NT_SUCCESS(Status
))
706 SetLastNtError(Status
);
710 switch (SafeInput
.type
)
713 if (UserSendMouseInput(&SafeInput
.mi
, TRUE
))
717 if (UserSendKeyboardInput(&SafeInput
.ki
, TRUE
))
721 FIXME("INPUT_HARDWARE not supported!");
724 ERR("SendInput(): Invalid input type: 0x%x\n", SafeInput
.type
);
730 TRACE("Leave NtUserSendInput, ret=%u\n", uRet
);