[WIN32K:NTUSER]
[reactos.git] / reactos / win32ss / user / ntuser / input.c
1 /*
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)
8 */
9
10 #include <win32k.h>
11 DBG_DEFAULT_CHANNEL(UserInput);
12
13 /* GLOBALS *******************************************************************/
14
15 PTHREADINFO ptiRawInput;
16 PKTIMER MasterTimer = NULL;
17 PATTACHINFO gpai = NULL;
18 INT paiCount = 0;
19 HANDLE ghKeyboardDevice = NULL;
20
21 static DWORD LastInputTick = 0;
22 static HANDLE ghMouseDevice;
23
24 /* FUNCTIONS *****************************************************************/
25
26 /*
27 * IntLastInputTick
28 *
29 * Updates or gets last input tick count
30 */
31 static DWORD FASTCALL
32 IntLastInputTick(BOOL bUpdate)
33 {
34 if (bUpdate)
35 {
36 LARGE_INTEGER TickCount;
37 KeQueryTickCount(&TickCount);
38 LastInputTick = MsqCalculateMessageTime(&TickCount);
39 if (gpsi) gpsi->dwLastRITEventTickCount = LastInputTick;
40 }
41 return LastInputTick;
42 }
43
44 /*
45 * DoTheScreenSaver
46 *
47 * Check if scrensaver should be started and sends message to SAS window
48 */
49 VOID FASTCALL
50 DoTheScreenSaver(VOID)
51 {
52 LARGE_INTEGER TickCount;
53 DWORD Test, TO;
54
55 if (gspv.iScrSaverTimeout > 0) // Zero means Off.
56 {
57 KeQueryTickCount(&TickCount);
58 Test = MsqCalculateMessageTime(&TickCount);
59 Test = Test - LastInputTick;
60 TO = 1000 * gspv.iScrSaverTimeout;
61 if (Test > TO)
62 {
63 TRACE("Screensaver Message Start! Tick %lu Timeout %d \n", Test, gspv.iScrSaverTimeout);
64
65 if (ppiScrnSaver) // We are or we are not the screensaver, prevent reentry...
66 {
67 if (!(ppiScrnSaver->W32PF_flags & W32PF_IDLESCREENSAVER))
68 {
69 ppiScrnSaver->W32PF_flags |= W32PF_IDLESCREENSAVER;
70 ERR("Screensaver is Idle\n");
71 }
72 }
73 else
74 {
75 PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue();
76 if (ForegroundQueue && ForegroundQueue->spwndActive)
77 UserPostMessage(hwndSAS, WM_LOGONNOTIFY, LN_START_SCREENSAVE, 1); // lParam 1 == Secure
78 else
79 UserPostMessage(hwndSAS, WM_LOGONNOTIFY, LN_START_SCREENSAVE, 0);
80 }
81 }
82 }
83 }
84
85 /*
86 * OpenInputDevice
87 *
88 * Opens input device for asynchronous access
89 */
90 static
91 NTSTATUS NTAPI
92 OpenInputDevice(PHANDLE pHandle, PFILE_OBJECT *ppObject, CONST WCHAR *pszDeviceName)
93 {
94 UNICODE_STRING DeviceName;
95 OBJECT_ATTRIBUTES ObjectAttributes;
96 NTSTATUS Status;
97 IO_STATUS_BLOCK Iosb;
98
99 RtlInitUnicodeString(&DeviceName, pszDeviceName);
100
101 InitializeObjectAttributes(&ObjectAttributes,
102 &DeviceName,
103 OBJ_KERNEL_HANDLE,
104 NULL,
105 NULL);
106
107 Status = ZwOpenFile(pHandle,
108 FILE_ALL_ACCESS,
109 &ObjectAttributes,
110 &Iosb,
111 0,
112 0);
113 if (NT_SUCCESS(Status) && ppObject)
114 {
115 Status = ObReferenceObjectByHandle(*pHandle, SYNCHRONIZE, NULL, KernelMode, (PVOID*)ppObject, NULL);
116 ASSERT(NT_SUCCESS(Status));
117 }
118
119 return Status;
120 }
121
122 /*
123 * RawInputThreadMain
124 *
125 * Reads data from input devices and supports win32 timers
126 */
127 VOID NTAPI
128 RawInputThreadMain(VOID)
129 {
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;
140 PVOID ShutdownEvent;
141
142 ByteOffset.QuadPart = (LONGLONG)0;
143 //WaitTimeout.QuadPart = (LONGLONG)(-10000000);
144
145 ptiRawInput = GetW32ThreadInfo();
146 ptiRawInput->TIF_flags |= TIF_SYSTEMTHREAD;
147 ptiRawInput->pClientInfo->dwTIFlags = ptiRawInput->TIF_flags;
148
149 TRACE("Raw Input Thread %p\n", ptiRawInput);
150
151 KeSetPriorityThread(&PsGetCurrentThread()->Tcb,
152 LOW_REALTIME_PRIORITY + 3);
153
154 UserEnterExclusive();
155 StartTheTimers();
156 UserLeave();
157
158 NT_ASSERT(ghMouseDevice == NULL);
159 NT_ASSERT(ghKeyboardDevice == NULL);
160
161 PoRequestShutdownEvent(&ShutdownEvent);
162 for (;;)
163 {
164 if (!ghMouseDevice)
165 {
166 /* Check if mouse device already exists */
167 Status = OpenInputDevice(&ghMouseDevice, &pMouDevice, L"\\Device\\PointerClass0" );
168 if (NT_SUCCESS(Status))
169 {
170 ++cMaxWaitObjects;
171 TRACE("Mouse connected!\n");
172 }
173 }
174 if (!ghKeyboardDevice)
175 {
176 /* Check if keyboard device already exists */
177 Status = OpenInputDevice(&ghKeyboardDevice, &pKbdDevice, L"\\Device\\KeyboardClass0");
178 if (NT_SUCCESS(Status))
179 {
180 ++cMaxWaitObjects;
181 TRACE("Keyboard connected!\n");
182 // Get and load keyboard attributes.
183 UserInitKeyboard(ghKeyboardDevice);
184 UserEnterExclusive();
185 // Register the Window hotkey.
186 UserRegisterHotKey(PWND_BOTTOM, IDHK_WINKEY, MOD_WIN, 0);
187 // Register the debug hotkeys.
188 StartDebugHotKeys();
189 UserLeave();
190 }
191 }
192
193 /* Reset WaitHandles array */
194 cWaitObjects = 0;
195 WaitObjects[cWaitObjects++] = ShutdownEvent;
196 WaitObjects[cWaitObjects++] = MasterTimer;
197
198 if (ghMouseDevice)
199 {
200 /* Try to read from mouse if previous reading is not pending */
201 if (MouStatus != STATUS_PENDING)
202 {
203 MouStatus = ZwReadFile(ghMouseDevice,
204 NULL,
205 NULL,
206 NULL,
207 &MouIosb,
208 &MouseInput,
209 sizeof(MOUSE_INPUT_DATA),
210 &ByteOffset,
211 NULL);
212 }
213
214 if (MouStatus == STATUS_PENDING)
215 WaitObjects[cWaitObjects++] = &pMouDevice->Event;
216 }
217
218 if (ghKeyboardDevice)
219 {
220 /* Try to read from keyboard if previous reading is not pending */
221 if (KbdStatus != STATUS_PENDING)
222 {
223 KbdStatus = ZwReadFile(ghKeyboardDevice,
224 NULL,
225 NULL,
226 NULL,
227 &KbdIosb,
228 &KeyInput,
229 sizeof(KEYBOARD_INPUT_DATA),
230 &ByteOffset,
231 NULL);
232
233 }
234 if (KbdStatus == STATUS_PENDING)
235 WaitObjects[cWaitObjects++] = &pKbdDevice->Event;
236 }
237
238 /* If all objects are pending, wait for them */
239 if (cWaitObjects == cMaxWaitObjects)
240 {
241 Status = KeWaitForMultipleObjects(cWaitObjects,
242 WaitObjects,
243 WaitAny,
244 UserRequest,
245 KernelMode,
246 TRUE,
247 NULL,//&WaitTimeout,
248 WaitBlockArray);
249
250 if ((Status >= STATUS_WAIT_0) &&
251 (Status < (STATUS_WAIT_0 + (LONG)cWaitObjects)))
252 {
253 /* Some device has finished reading */
254 pSignaledObject = WaitObjects[Status - STATUS_WAIT_0];
255
256 /* Check if it is mouse or keyboard and update status */
257 if ((MouStatus == STATUS_PENDING) &&
258 (pSignaledObject == &pMouDevice->Event))
259 {
260 MouStatus = MouIosb.Status;
261 }
262 else if ((KbdStatus == STATUS_PENDING) &&
263 (pSignaledObject == &pKbdDevice->Event))
264 {
265 KbdStatus = KbdIosb.Status;
266 }
267 else if (pSignaledObject == MasterTimer)
268 {
269 ProcessTimers();
270 }
271 else if (pSignaledObject == ShutdownEvent)
272 {
273 break;
274 }
275 else ASSERT(FALSE);
276 }
277 }
278
279 /* Have we successed reading from mouse? */
280 if (NT_SUCCESS(MouStatus) && MouStatus != STATUS_PENDING)
281 {
282 TRACE("MouseEvent\n");
283
284 /* Set LastInputTick */
285 IntLastInputTick(TRUE);
286
287 /* Process data */
288 UserEnterExclusive();
289 UserProcessMouseInput(&MouseInput);
290 UserLeave();
291 }
292 else if (MouStatus != STATUS_PENDING)
293 ERR("Failed to read from mouse: %x.\n", MouStatus);
294
295 /* Have we successed reading from keyboard? */
296 if (NT_SUCCESS(KbdStatus) && KbdStatus != STATUS_PENDING)
297 {
298 TRACE("KeyboardEvent: %s %04x\n",
299 (KeyInput.Flags & KEY_BREAK) ? "up" : "down",
300 KeyInput.MakeCode);
301
302 /* Set LastInputTick */
303 IntLastInputTick(TRUE);
304
305 /* Process data */
306 UserEnterExclusive();
307 UserProcessKeyboardInput(&KeyInput);
308 UserLeave();
309 }
310 else if (KbdStatus != STATUS_PENDING)
311 ERR("Failed to read from keyboard: %x.\n", KbdStatus);
312 }
313
314 if (ghMouseDevice)
315 {
316 (void)ZwCancelIoFile(ghMouseDevice, &MouIosb);
317 ObCloseHandle(ghMouseDevice, KernelMode);
318 ObDereferenceObject(pMouDevice);
319 ghMouseDevice = NULL;
320 }
321
322 if (ghKeyboardDevice)
323 {
324 (void)ZwCancelIoFile(ghKeyboardDevice, &KbdIosb);
325 ObCloseHandle(ghKeyboardDevice, KernelMode);
326 ObDereferenceObject(pKbdDevice);
327 ghKeyboardDevice = NULL;
328 }
329
330 ERR("Raw Input Thread Exit!\n");
331 }
332
333 /*
334 * CreateSystemThreads
335 *
336 * Called form dedicated thread in CSRSS. RIT is started in context of this
337 * thread because it needs valid Win32 process with TEB initialized.
338 */
339 DWORD NTAPI
340 CreateSystemThreads(UINT Type)
341 {
342 UserLeave();
343
344 switch (Type)
345 {
346 case 0: RawInputThreadMain(); break;
347 case 1: DesktopThreadMain(); break;
348 default: ERR("Wrong type: %x\n", Type);
349 }
350
351 UserEnterShared();
352
353 return 0;
354 }
355
356 /*
357 * InitInputImpl
358 *
359 * Inits input implementation
360 */
361 INIT_FUNCTION
362 NTSTATUS
363 NTAPI
364 InitInputImpl(VOID)
365 {
366 MasterTimer = ExAllocatePoolWithTag(NonPagedPool, sizeof(KTIMER), USERTAG_SYSTEM);
367 if (!MasterTimer)
368 {
369 ERR("Failed to allocate memory\n");
370 ASSERT(FALSE);
371 return STATUS_UNSUCCESSFUL;
372 }
373 KeInitializeTimer(MasterTimer);
374
375 return STATUS_SUCCESS;
376 }
377
378 BOOL FASTCALL
379 IntBlockInput(PTHREADINFO pti, BOOL BlockIt)
380 {
381 PTHREADINFO OldBlock;
382 ASSERT(pti);
383
384 if(!pti->rpdesk || ((pti->TIF_flags & TIF_INCLEANUP) && BlockIt))
385 {
386 /*
387 * Fail blocking if exiting the thread
388 */
389
390 return FALSE;
391 }
392
393 /*
394 * FIXME: Check access rights of the window station
395 * e.g. services running in the service window station cannot block input
396 */
397 if(!ThreadHasInputAccess(pti) ||
398 !IntIsActiveDesktop(pti->rpdesk))
399 {
400 EngSetLastError(ERROR_ACCESS_DENIED);
401 return FALSE;
402 }
403
404 ASSERT(pti->rpdesk);
405 OldBlock = pti->rpdesk->BlockInputThread;
406 if(OldBlock)
407 {
408 if(OldBlock != pti)
409 {
410 EngSetLastError(ERROR_ACCESS_DENIED);
411 return FALSE;
412 }
413 pti->rpdesk->BlockInputThread = (BlockIt ? pti : NULL);
414 return OldBlock == NULL;
415 }
416
417 pti->rpdesk->BlockInputThread = (BlockIt ? pti : NULL);
418 return OldBlock == NULL;
419 }
420
421 BOOL
422 APIENTRY
423 NtUserBlockInput(
424 BOOL BlockIt)
425 {
426 BOOL ret;
427
428 TRACE("Enter NtUserBlockInput\n");
429 UserEnterExclusive();
430
431 ret = IntBlockInput(PsGetCurrentThreadWin32Thread(), BlockIt);
432
433 UserLeave();
434 TRACE("Leave NtUserBlockInput, ret=%i\n", ret);
435
436 return ret;
437 }
438
439 BOOL
440 FASTCALL
441 IsRemoveAttachThread(PTHREADINFO pti)
442 {
443 NTSTATUS Status;
444 PATTACHINFO pai;
445 BOOL Ret = TRUE;
446 PTHREADINFO ptiFrom = NULL, ptiTo = NULL;
447
448 do
449 {
450 if (!gpai) return TRUE;
451
452 pai = gpai; // Bottom of the list.
453
454 do
455 {
456 if (pai->pti2 == pti)
457 {
458 ptiFrom = pai->pti1;
459 ptiTo = pti;
460 break;
461 }
462 if (pai->pti1 == pti)
463 {
464 ptiFrom = pti;
465 ptiTo = pai->pti2;
466 break;
467 }
468 pai = pai->paiNext;
469
470 } while (pai);
471
472 if (!pai && !ptiFrom && !ptiTo) break;
473
474 Status = UserAttachThreadInput(ptiFrom, ptiTo, FALSE);
475 if (!NT_SUCCESS(Status)) Ret = FALSE;
476
477 } while (Ret);
478
479 return Ret;
480 }
481
482 NTSTATUS FASTCALL
483 UserAttachThreadInput(PTHREADINFO ptiFrom, PTHREADINFO ptiTo, BOOL fAttach)
484 {
485 MSG msg;
486 PATTACHINFO pai;
487 PCURICON_OBJECT CurIcon;
488
489 /* Can not be the same thread. */
490 if (ptiFrom == ptiTo) return STATUS_INVALID_PARAMETER;
491
492 /* Do not attach to system threads or between different desktops. */
493 if (ptiFrom->TIF_flags & TIF_DONTATTACHQUEUE ||
494 ptiTo->TIF_flags & TIF_DONTATTACHQUEUE ||
495 ptiFrom->rpdesk != ptiTo->rpdesk)
496 return STATUS_ACCESS_DENIED;
497
498 /* MSDN Note:
499 Keyboard and mouse events received by both threads are processed by the thread specified by the idAttachTo.
500 */
501
502 /* If Attach set, allocate and link. */
503 if (fAttach)
504 {
505 pai = ExAllocatePoolWithTag(PagedPool, sizeof(ATTACHINFO), USERTAG_ATTACHINFO);
506 if (!pai) return STATUS_NO_MEMORY;
507
508 pai->paiNext = gpai;
509 pai->pti1 = ptiFrom;
510 pai->pti2 = ptiTo;
511 gpai = pai;
512 paiCount++;
513 ERR("Attach Allocated! ptiFrom 0x%p ptiTo 0x%p paiCount %d\n",ptiFrom,ptiTo,paiCount);
514
515 if (ptiTo->MessageQueue != ptiFrom->MessageQueue)
516 {
517
518 ptiTo->MessageQueue->iCursorLevel -= ptiFrom->iCursorLevel;
519
520 if (ptiFrom->MessageQueue == gpqForeground)
521 {
522 ERR("ptiFrom is Foreground\n");
523 ptiTo->MessageQueue->spwndActive = ptiFrom->MessageQueue->spwndActive;
524 ptiTo->MessageQueue->spwndFocus = ptiFrom->MessageQueue->spwndFocus;
525 ptiTo->MessageQueue->spwndCapture = ptiFrom->MessageQueue->spwndCapture;
526 ptiTo->MessageQueue->QF_flags ^= ((ptiTo->MessageQueue->QF_flags ^ ptiFrom->MessageQueue->QF_flags) & QF_CAPTURELOCKED);
527 ptiTo->MessageQueue->CaretInfo = ptiFrom->MessageQueue->CaretInfo;
528 IntSetFocusMessageQueue(NULL);
529 IntSetFocusMessageQueue(ptiTo->MessageQueue);
530 gptiForeground = ptiTo;
531 }
532 else
533 {
534 ERR("ptiFrom NOT Foreground\n");
535 if ( ptiTo->MessageQueue->spwndActive == 0 )
536 ptiTo->MessageQueue->spwndActive = ptiFrom->MessageQueue->spwndActive;
537 if ( ptiTo->MessageQueue->spwndFocus == 0 )
538 ptiTo->MessageQueue->spwndFocus = ptiFrom->MessageQueue->spwndFocus;
539 }
540
541 CurIcon = ptiFrom->MessageQueue->CursorObject;
542
543 MsqDestroyMessageQueue(ptiFrom);
544
545 if (CurIcon)
546 {
547 // Could be global. Keep it above the water line!
548 UserReferenceObject(CurIcon);
549 }
550
551 if (CurIcon && UserObjectInDestroy(UserHMGetHandle(CurIcon)))
552 {
553 UserDereferenceObject(CurIcon);
554 CurIcon = NULL;
555 }
556
557 ptiFrom->MessageQueue = ptiTo->MessageQueue;
558
559 // Pass cursor From if To is null. Pass test_SetCursor parent_id == current pti ID.
560 if (CurIcon && ptiTo->MessageQueue->CursorObject == NULL)
561 {
562 ERR("ptiTo receiving ptiFrom Cursor\n");
563 ptiTo->MessageQueue->CursorObject = CurIcon;
564 }
565
566 ptiFrom->MessageQueue->cThreads++;
567 ERR("ptiTo S Share count %u\n", ptiFrom->MessageQueue->cThreads);
568
569 IntReferenceMessageQueue(ptiTo->MessageQueue);
570 }
571 else
572 {
573 ERR("Attach Threads are already associated!\n");
574 }
575 }
576 else /* If clear, unlink and free it. */
577 {
578 BOOL Hit = FALSE;
579 PATTACHINFO *ppai;
580
581 if (!gpai) return STATUS_INVALID_PARAMETER;
582
583 /* Search list and free if found or return false. */
584 ppai = &gpai;
585 while (*ppai != NULL)
586 {
587 if ( (*ppai)->pti2 == ptiTo && (*ppai)->pti1 == ptiFrom )
588 {
589 pai = *ppai;
590 /* Remove it from the list */
591 *ppai = (*ppai)->paiNext;
592 ExFreePoolWithTag(pai, USERTAG_ATTACHINFO);
593 paiCount--;
594 Hit = TRUE;
595 break;
596 }
597 ppai = &((*ppai)->paiNext);
598 }
599
600 if (!Hit) return STATUS_INVALID_PARAMETER;
601
602 ERR("Attach Free! ptiFrom 0x%p ptiTo 0x%p paiCount %d\n",ptiFrom,ptiTo,paiCount);
603
604 if (ptiTo->MessageQueue == ptiFrom->MessageQueue)
605 {
606 PWND spwndActive = ptiTo->MessageQueue->spwndActive;
607 PWND spwndFocus = ptiTo->MessageQueue->spwndFocus;
608
609 if (gptiForeground == ptiFrom)
610 {
611 ERR("ptiTo is now pti FG.\n");
612 // MessageQueue foreground is set so switch threads.
613 gptiForeground = ptiTo;
614 }
615 ptiTo->MessageQueue->cThreads--;
616 ERR("ptiTo E Share count %u\n", ptiTo->MessageQueue->cThreads);
617 ASSERT(ptiTo->MessageQueue->cThreads >= 1);
618
619 IntDereferenceMessageQueue(ptiTo->MessageQueue);
620
621 ptiFrom->MessageQueue = MsqCreateMessageQueue(ptiFrom);
622
623 if (spwndActive)
624 {
625 if (spwndActive->head.pti == ptiFrom)
626 {
627 ptiFrom->MessageQueue->spwndActive = spwndActive;
628 ptiTo->MessageQueue->spwndActive = 0;
629 }
630 }
631 if (spwndFocus)
632 {
633 if (spwndFocus->head.pti == ptiFrom)
634 {
635 ptiFrom->MessageQueue->spwndFocus = spwndFocus;
636 ptiTo->MessageQueue->spwndFocus = 0;
637 }
638 }
639 ptiTo->MessageQueue->iCursorLevel -= ptiFrom->iCursorLevel;
640 }
641 else
642 {
643 ERR("Detaching Threads are not associated!\n");
644 }
645 }
646 /* Note that key state, which can be ascertained by calls to the GetKeyState
647 or GetKeyboardState function, is reset after a call to AttachThreadInput.
648 ATM which one?
649 */
650 RtlCopyMemory(ptiTo->MessageQueue->afKeyState, gafAsyncKeyState, sizeof(gafAsyncKeyState));
651
652 ptiTo->MessageQueue->msgDblClk.message = 0;
653
654 /* Generate mouse move message */
655 msg.message = WM_MOUSEMOVE;
656 msg.wParam = UserGetMouseButtonsState();
657 msg.lParam = MAKELPARAM(gpsi->ptCursor.x, gpsi->ptCursor.y);
658 msg.pt = gpsi->ptCursor;
659 co_MsqInsertMouseMessage(&msg, 0, 0, TRUE);
660
661 return STATUS_SUCCESS;
662 }
663
664 BOOL
665 APIENTRY
666 NtUserAttachThreadInput(
667 IN DWORD idAttach,
668 IN DWORD idAttachTo,
669 IN BOOL fAttach)
670 {
671 NTSTATUS Status;
672 PTHREADINFO pti, ptiTo;
673 BOOL Ret = FALSE;
674
675 UserEnterExclusive();
676 TRACE("Enter NtUserAttachThreadInput %s\n",(fAttach ? "TRUE" : "FALSE" ));
677
678 pti = IntTID2PTI((HANDLE)idAttach);
679 ptiTo = IntTID2PTI((HANDLE)idAttachTo);
680
681 if ( !pti || !ptiTo )
682 {
683 TRACE("AttachThreadInput pti or ptiTo NULL.\n");
684 EngSetLastError(ERROR_INVALID_PARAMETER);
685 goto Exit;
686 }
687
688 Status = UserAttachThreadInput( pti, ptiTo, fAttach);
689 if (!NT_SUCCESS(Status))
690 {
691 TRACE("AttachThreadInput Error Status 0x%x. \n",Status);
692 EngSetLastError(RtlNtStatusToDosError(Status));
693 }
694 else Ret = TRUE;
695
696 Exit:
697 TRACE("Leave NtUserAttachThreadInput, ret=%d\n",Ret);
698 UserLeave();
699 return Ret;
700 }
701
702 /*
703 * NtUserSendInput
704 *
705 * Generates input events from software
706 */
707 UINT
708 APIENTRY
709 NtUserSendInput(
710 UINT nInputs,
711 LPINPUT pInput,
712 INT cbSize)
713 {
714 PTHREADINFO pti;
715 UINT uRet = 0;
716
717 TRACE("Enter NtUserSendInput\n");
718 UserEnterExclusive();
719
720 pti = PsGetCurrentThreadWin32Thread();
721 ASSERT(pti);
722
723 if (!pti->rpdesk)
724 {
725 goto cleanup;
726 }
727
728 if (!nInputs || !pInput || cbSize != sizeof(INPUT))
729 {
730 EngSetLastError(ERROR_INVALID_PARAMETER);
731 goto cleanup;
732 }
733
734 /*
735 * FIXME: Check access rights of the window station
736 * e.g. services running in the service window station cannot block input
737 */
738 if (!ThreadHasInputAccess(pti) ||
739 !IntIsActiveDesktop(pti->rpdesk))
740 {
741 EngSetLastError(ERROR_ACCESS_DENIED);
742 goto cleanup;
743 }
744
745 while (nInputs--)
746 {
747 INPUT SafeInput;
748 NTSTATUS Status;
749
750 Status = MmCopyFromCaller(&SafeInput, pInput++, sizeof(INPUT));
751 if (!NT_SUCCESS(Status))
752 {
753 SetLastNtError(Status);
754 goto cleanup;
755 }
756
757 switch (SafeInput.type)
758 {
759 case INPUT_MOUSE:
760 if (UserSendMouseInput(&SafeInput.mi, TRUE))
761 uRet++;
762 break;
763 case INPUT_KEYBOARD:
764 if (UserSendKeyboardInput(&SafeInput.ki, TRUE))
765 uRet++;
766 break;
767 case INPUT_HARDWARE:
768 FIXME("INPUT_HARDWARE not supported!");
769 break;
770 default:
771 ERR("SendInput(): Invalid input type: 0x%x\n", SafeInput.type);
772 break;
773 }
774 }
775
776 cleanup:
777 TRACE("Leave NtUserSendInput, ret=%u\n", uRet);
778 UserLeave();
779 return uRet;
780 }
781
782 /* EOF */