[Win32SS]
[reactos.git] / reactos / win32ss / user / ntuser / focus.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Win32k subsystem
4 * PURPOSE: Focus functions
5 * FILE: subsystems/win32/win32k/ntuser/focus.c
6 * PROGRAMER: ReactOS Team
7 */
8
9 #include <win32k.h>
10 DBG_DEFAULT_CHANNEL(UserFocus);
11
12 PUSER_MESSAGE_QUEUE gpqForeground = NULL;
13 PUSER_MESSAGE_QUEUE gpqForegroundPrev = NULL;
14 PTHREADINFO gptiForeground = NULL;
15 PPROCESSINFO gppiLockSFW = NULL;
16 ULONG guSFWLockCount = 0; // Rule #8, No menus are active. So should be zero.
17 PTHREADINFO ptiLastInput = NULL;
18
19 /*
20 Check locking of a process or one or more menus are active.
21 */
22 BOOL FASTCALL
23 IsFGLocked(VOID)
24 {
25 return (gppiLockSFW || guSFWLockCount);
26 }
27
28 /*
29 Get capture window via foreground Queue.
30 */
31 HWND FASTCALL
32 IntGetCaptureWindow(VOID)
33 {
34 PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue();
35 return ( ForegroundQueue ? (ForegroundQueue->spwndCapture ? UserHMGetHandle(ForegroundQueue->spwndCapture) : 0) : 0);
36 }
37
38 HWND FASTCALL
39 IntGetThreadFocusWindow(VOID)
40 {
41 PTHREADINFO pti;
42 PUSER_MESSAGE_QUEUE ThreadQueue;
43
44 pti = PsGetCurrentThreadWin32Thread();
45 ThreadQueue = pti->MessageQueue;
46 if (!ThreadQueue)
47 return NULL;
48 return ThreadQueue->spwndFocus ? UserHMGetHandle(ThreadQueue->spwndFocus) : 0;
49 }
50
51 BOOL FASTCALL
52 co_IntSendDeactivateMessages(HWND hWndPrev, HWND hWnd)
53 {
54 USER_REFERENCE_ENTRY RefPrev;
55 PWND WndPrev;
56 BOOL Ret = TRUE;
57
58 if (hWndPrev && (WndPrev = ValidateHwndNoErr(hWndPrev)))
59 {
60 UserRefObjectCo(WndPrev, &RefPrev);
61
62 if (co_IntSendMessageNoWait(hWndPrev, WM_NCACTIVATE, FALSE, 0)) //(LPARAM)hWnd))
63 {
64 co_IntSendMessageNoWait(hWndPrev, WM_ACTIVATE,
65 MAKEWPARAM(WA_INACTIVE, (WndPrev->style & WS_MINIMIZE) != 0),
66 (LPARAM)hWnd);
67
68 if (WndPrev)
69 WndPrev->state &= ~(WNDS_ACTIVEFRAME|WNDS_HASCAPTION);
70 }
71 else
72 {
73 ERR("Application is keeping itself Active to prevent the change!\n");
74 Ret = FALSE;
75 }
76
77 UserDerefObjectCo(WndPrev);
78 }
79 return Ret;
80 }
81
82 BOOL FASTCALL
83 co_IntMakeWindowActive(PWND Window)
84 {
85 PWND spwndOwner;
86 if (VerifyWnd(Window))
87 { // Set last active for window and it's owner.
88 spwndOwner = Window;
89 while (spwndOwner->spwndOwner)
90 {
91 spwndOwner = spwndOwner->spwndOwner;
92 }
93 spwndOwner->spwndLastActive = Window;
94 return TRUE;
95 }
96 ERR("MakeWindowActive Failed!\n");
97 return FALSE;
98 }
99
100 BOOL FASTCALL
101 co_IntSendActivateMessages(PWND WindowPrev, PWND Window, BOOL MouseActivate, BOOL Async)
102 {
103 USER_REFERENCE_ENTRY Ref, RefPrev;
104 HANDLE OldTID, NewTID;
105 PTHREADINFO pti, ptiOld, ptiNew;
106 BOOL InAAPM = FALSE;
107
108 //ERR("SendActivateMessages\n");
109
110 pti = PsGetCurrentThreadWin32Thread();
111
112 if (Window)
113 {
114 UserRefObjectCo(Window, &Ref);
115
116 if (WindowPrev) UserRefObjectCo(WindowPrev, &RefPrev);
117
118 /* Send palette messages */
119 if (gpsi->PUSIFlags & PUSIF_PALETTEDISPLAY &&
120 //co_IntPostOrSendMessage(UserHMGetHandle(Window), WM_QUERYNEWPALETTE, 0, 0))
121 co_IntSendMessage(UserHMGetHandle(Window), WM_QUERYNEWPALETTE, 0, 0))
122 {
123 UserSendNotifyMessage( HWND_BROADCAST,
124 WM_PALETTEISCHANGING,
125 (WPARAM)UserHMGetHandle(Window),
126 0);
127 }
128 //// Fixes CORE-6434.
129 if (!(Window->style & WS_CHILD))
130 {
131 PWND pwndTemp = co_GetDesktopWindow(Window)->spwndChild;
132
133 while (pwndTemp && !(pwndTemp->style & WS_VISIBLE)) pwndTemp = pwndTemp->spwndNext;
134
135 if (Window != pwndTemp || (WindowPrev && !IntIsWindowVisible(WindowPrev)))
136 {
137 if (!Async || pti->MessageQueue == gpqForeground)
138 {
139 UINT flags = SWP_NOSIZE | SWP_NOMOVE;
140 if (Window == pwndTemp) flags |= SWP_NOACTIVATE;
141 //ERR("co_IntSendActivateMessages SetWindowPos! Async %d pti Q == FGQ %d\n",Async,pti->MessageQueue == gpqForeground);
142 co_WinPosSetWindowPos(Window, HWND_TOP, 0, 0, 0, 0, flags);
143 }
144 }
145 }
146 ////
147 //// CORE-1161 and CORE-6651
148 if (Window->spwndPrev)
149 {
150 HWND *phwndTopLevel, *phwndCurrent;
151 PWND pwndCurrent, pwndDesktop;
152
153 pwndDesktop = co_GetDesktopWindow(Window);//UserGetDesktopWindow();
154 if (Window->spwndParent == pwndDesktop )
155 {
156 phwndTopLevel = IntWinListChildren(pwndDesktop);
157 phwndCurrent = phwndTopLevel;
158 while(*phwndCurrent)
159 {
160 pwndCurrent = UserGetWindowObject(*phwndCurrent);
161
162 if (pwndCurrent && pwndCurrent->spwndOwner == Window )
163 {
164 co_WinPosSetWindowPos(pwndCurrent, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
165 }
166 phwndCurrent++;
167 }
168 ExFreePoolWithTag(phwndTopLevel, USERTAG_WINDOWLIST);
169 }
170 }
171 ////
172 }
173
174 OldTID = WindowPrev ? IntGetWndThreadId(WindowPrev) : NULL;
175 NewTID = Window ? IntGetWndThreadId(Window) : NULL;
176 ptiOld = WindowPrev ? WindowPrev->head.pti : NULL;
177 ptiNew = Window ? Window->head.pti : NULL;
178
179 //ERR("SendActivateMessage Old -> %x, New -> %x\n", OldTID, NewTID);
180
181 if (!(pti->TIF_flags & TIF_INACTIVATEAPPMSG) &&
182 (OldTID != NewTID) )
183 {
184 PWND cWindow;
185 HWND *List, *phWnd;
186
187 List = IntWinListChildren(UserGetDesktopWindow());
188 if ( List )
189 {
190 if ( OldTID )
191 {
192 ptiOld->TIF_flags |= TIF_INACTIVATEAPPMSG;
193 // Note: Do not set pci flags, this does crash!
194 for (phWnd = List; *phWnd; ++phWnd)
195 {
196 cWindow = ValidateHwndNoErr(*phWnd);
197 if (cWindow && cWindow->head.pti == ptiOld)
198 { // FALSE if the window is being deactivated,
199 // ThreadId that owns the window being activated.
200 //ERR("SendActivateMessage Old\n");
201 co_IntSendMessageNoWait(*phWnd, WM_ACTIVATEAPP, FALSE, (LPARAM)NewTID);
202 }
203 }
204 ptiOld->TIF_flags &= ~TIF_INACTIVATEAPPMSG;
205 }
206 if ( NewTID )
207 { //// Prevents a resource crash due to reentrance!
208 InAAPM = TRUE;
209 pti->TIF_flags |= TIF_INACTIVATEAPPMSG;
210 ////
211 for (phWnd = List; *phWnd; ++phWnd)
212 {
213 cWindow = ValidateHwndNoErr(*phWnd);
214 if (cWindow && cWindow->head.pti == ptiNew)
215 { // TRUE if the window is being activated,
216 // ThreadId that owns the window being deactivated.
217 //ERR("SendActivateMessage New\n");
218 co_IntSendMessageNoWait(*phWnd, WM_ACTIVATEAPP, TRUE, (LPARAM)OldTID);
219 }
220 }
221 }
222 ExFreePoolWithTag(List, USERTAG_WINDOWLIST);
223 }
224 }
225
226 if (Window)
227 {
228 if (WindowPrev)
229 UserDerefObjectCo(WindowPrev); // Now allow the previous window to die.
230
231 if (Window->state & WNDS_ACTIVEFRAME)
232 { // If already active frame do not allow NCPaint.
233 //ERR("SendActivateMessage Is Active Frame!\n");
234 Window->state |= WNDS_NONCPAINT;
235 }
236
237 if (Window->style & WS_MINIMIZE)
238 {
239 TRACE("Widow was minimized\n");
240 }
241
242 co_IntMakeWindowActive(Window);
243
244 co_IntSendMessageNoWait( UserHMGetHandle(Window),
245 WM_NCACTIVATE,
246 (WPARAM)(Window == (gpqForeground ? gpqForeground->spwndActive : NULL)),
247 0); //(LPARAM)hWndPrev);
248
249 co_IntSendMessage( UserHMGetHandle(Window),
250 WM_ACTIVATE,
251 MAKEWPARAM(MouseActivate ? WA_CLICKACTIVE : WA_ACTIVE, (Window->style & WS_MINIMIZE) != 0),
252 (LPARAM)(WindowPrev ? UserHMGetHandle(WindowPrev) : 0));
253
254 if (Window->spwndParent == UserGetDesktopWindow() &&
255 Window->spwndOwner == NULL &&
256 (!(Window->ExStyle & WS_EX_TOOLWINDOW) ||
257 (Window->ExStyle & WS_EX_APPWINDOW)))
258 {
259 // FIXME lParam; The value is TRUE if the window is in full-screen mode, or FALSE otherwise.
260 co_IntShellHookNotify(HSHELL_WINDOWACTIVATED, (WPARAM) UserHMGetHandle(Window), FALSE);
261 }
262 else
263 {
264 co_IntShellHookNotify(HSHELL_WINDOWACTIVATED, 0, FALSE);
265 }
266
267 Window->state &= ~WNDS_NONCPAINT;
268
269 UserDerefObjectCo(Window);
270 }
271 return InAAPM;
272 }
273
274 VOID FASTCALL
275 IntSendFocusMessages( PTHREADINFO pti, PWND pWnd)
276 {
277 PWND pWndPrev;
278 PUSER_MESSAGE_QUEUE ThreadQueue = pti->MessageQueue; // Queue can change...
279
280 ThreadQueue->QF_flags &= ~QF_FOCUSNULLSINCEACTIVE;
281 if (!pWnd && ThreadQueue->spwndActive)
282 {
283 ThreadQueue->QF_flags |= QF_FOCUSNULLSINCEACTIVE;
284 }
285
286 pWndPrev = ThreadQueue->spwndFocus;
287
288 /* check if the specified window can be set in the input data of a given queue */
289 if (!pWnd || ThreadQueue == pWnd->head.pti->MessageQueue)
290 /* set the current thread focus window */
291 ThreadQueue->spwndFocus = pWnd;
292
293 if (pWnd)
294 {
295 if (pWndPrev)
296 {
297 //co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, (WPARAM)UserHMGetHandle(pWnd), 0);
298 co_IntSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, (WPARAM)UserHMGetHandle(pWnd), 0);
299 }
300 if (ThreadQueue->spwndFocus == pWnd)
301 {
302 IntNotifyWinEvent(EVENT_OBJECT_FOCUS, pWnd, OBJID_CLIENT, CHILDID_SELF, 0);
303 //co_IntPostOrSendMessage(UserHMGetHandle(pWnd), WM_SETFOCUS, (WPARAM)(pWndPrev ? UserHMGetHandle(pWndPrev) : NULL), 0);
304 co_IntSendMessage(UserHMGetHandle(pWnd), WM_SETFOCUS, (WPARAM)(pWndPrev ? UserHMGetHandle(pWndPrev) : NULL), 0);
305 }
306 }
307 else
308 {
309 if (pWndPrev)
310 {
311 IntNotifyWinEvent(EVENT_OBJECT_FOCUS, NULL, OBJID_CLIENT, CHILDID_SELF, 0);
312 //co_IntPostOrSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, 0, 0);
313 co_IntSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, 0, 0);
314 }
315 }
316 }
317
318 VOID FASTCALL
319 FindRemoveAsyncMsg(PWND Wnd, WPARAM wParam)
320 {
321 PTHREADINFO pti;
322 PUSER_SENT_MESSAGE Message;
323 PLIST_ENTRY Entry;
324
325 if (!Wnd) return;
326
327 pti = Wnd->head.pti;
328
329 Entry = pti->SentMessagesListHead.Flink;
330 while (Entry != &pti->SentMessagesListHead)
331 {
332 // Scan sent queue messages to see if we received async messages.
333 Message = CONTAINING_RECORD(Entry, USER_SENT_MESSAGE, ListEntry);
334 Entry = Entry->Flink;
335
336 if (Message->Msg.message == WM_ASYNC_SETACTIVEWINDOW &&
337 Message->Msg.hwnd == UserHMGetHandle(Wnd) &&
338 Message->Msg.wParam == wParam)
339 {
340 WARN("ASYNC SAW: Found one in the Sent Msg Queue! %p Activate/Deactivate %d\n", Message->Msg.hwnd, !!wParam);
341 RemoveEntryList(&Message->ListEntry); // Purge the entry.
342 ClearMsgBitsMask(pti, Message->QS_Flags);
343 InsertTailList(&usmList, &Message->ListEntry);
344 /* Notify the sender. */
345 if (Message->pkCompletionEvent != NULL)
346 {
347 KeSetEvent(Message->pkCompletionEvent, IO_NO_INCREMENT, FALSE);
348 }
349 FreeUserMessage(Message);
350 }
351 }
352 }
353
354 BOOL FASTCALL
355 ToggleFGActivate(PTHREADINFO pti)
356 {
357 BOOL Ret;
358 PPROCESSINFO ppi = pti->ppi;
359
360 Ret = !!(pti->TIF_flags & TIF_ALLOWFOREGROUNDACTIVATE);
361 if (Ret)
362 {
363 pti->TIF_flags &= ~TIF_ALLOWFOREGROUNDACTIVATE;
364 }
365 else
366 Ret = !!(ppi->W32PF_flags & W32PF_ALLOWFOREGROUNDACTIVATE);
367
368 if (Ret)
369 ppi->W32PF_flags &= ~W32PF_ALLOWFOREGROUNDACTIVATE;
370 //ERR("ToggleFGActivate is %d\n",Ret);
371 return Ret;
372 }
373
374 BOOL FASTCALL
375 IsAllowedFGActive(PTHREADINFO pti, PWND Wnd)
376 {
377 // Not allowed if one or more,,
378 if (!ToggleFGActivate(pti) || // bits not set,
379 pti->rpdesk != gpdeskInputDesktop || // not current Desktop,
380 pti->MessageQueue == gpqForeground || // if already the queue foreground,
381 IsFGLocked() || // foreground is locked,
382 Wnd->ExStyle & WS_EX_NOACTIVATE ) // or,,, does not become the foreground window when the user clicks it.
383 {
384 return FALSE;
385 }
386 //ERR("IsAllowedFGActive is TRUE\n");
387 return TRUE;
388 }
389
390 /*
391 Can the system force foreground from one or more conditions.
392 */
393 BOOL FASTCALL
394 CanForceFG(PPROCESSINFO ppi)
395 {
396 if (!ptiLastInput ||
397 ptiLastInput->ppi == ppi ||
398 !gptiForeground ||
399 gptiForeground->ppi == ppi ||
400 ppi->W32PF_flags & (W32PF_ALLOWFOREGROUNDACTIVATE | W32PF_SETFOREGROUNDALLOWED) ||
401 gppiInputProvider == ppi ||
402 !gpqForeground
403 ) return TRUE;
404 //ERR("CanForceFG is FALSE\n");
405 return FALSE;
406 }
407
408 /*
409 MSDN:
410 The system restricts which processes can set the foreground window. A process
411 can set the foreground window only if one of the following conditions is true:
412
413 * The process is the foreground process.
414 * The process was started by the foreground process.
415 * The process received the last input event.
416 * There is no foreground process.
417 * The foreground process is being debugged.
418 * The foreground is not locked (see LockSetForegroundWindow).
419 * The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
420 * No menus are active.
421 */
422 static
423 BOOL FASTCALL
424 co_IntSetForegroundAndFocusWindow(
425 _In_opt_ PWND Wnd,
426 _In_ BOOL MouseActivate)
427 {
428 HWND hWnd = Wnd ? UserHMGetHandle(Wnd) : NULL;
429 HWND hWndPrev = NULL;
430 PWND pWndPrev = NULL;
431 PUSER_MESSAGE_QUEUE PrevForegroundQueue;
432 PTHREADINFO pti;
433 BOOL fgRet = FALSE, Ret = FALSE;
434
435 if (Wnd) ASSERT_REFS_CO(Wnd);
436
437 //ERR("SetForegroundAndFocusWindow(%x, %s)\n", hWnd, (MouseActivate ? "TRUE" : "FALSE"));
438
439 PrevForegroundQueue = IntGetFocusMessageQueue(); // Use this active desktop.
440 pti = PsGetCurrentThreadWin32Thread();
441
442 if (PrevForegroundQueue)
443 { // Same Window Q as foreground just do active.
444 if (Wnd && Wnd->head.pti->MessageQueue == PrevForegroundQueue)
445 {
446 //ERR("Same Window Q as foreground just do active.\n");
447 if (pti->MessageQueue == PrevForegroundQueue)
448 { // Same WQ and TQ go active.
449 //ERR("Same WQ and TQ go active.\n");
450 Ret = co_IntSetActiveWindow(Wnd, MouseActivate, TRUE, FALSE);
451 }
452 else if (Wnd->head.pti->MessageQueue->spwndActive == Wnd)
453 { // Same WQ and it is active.
454 //ERR("Same WQ and it is active.\n");
455 Ret = TRUE;
456 }
457 else
458 { // Same WQ as FG but not the same TQ send active.
459 //ERR("Same WQ as FG but not the same TQ send active.\n");
460 co_IntSendMessage(hWnd, WM_ASYNC_SETACTIVEWINDOW, (WPARAM)Wnd, (LPARAM)MouseActivate );
461 Ret = TRUE;
462 }
463 return Ret;
464 }
465
466 hWndPrev = PrevForegroundQueue->spwndActive ? UserHMGetHandle(PrevForegroundQueue->spwndActive) : 0;
467 pWndPrev = PrevForegroundQueue->spwndActive;
468 }
469
470 if ( (( !IsFGLocked() || pti->ppi == gppiInputProvider ) &&
471 ( CanForceFG(pti->ppi) || pti->TIF_flags & (TIF_SYSTEMTHREAD|TIF_CSRSSTHREAD|TIF_ALLOWFOREGROUNDACTIVATE) )) ||
472 pti->ppi == ppiScrnSaver
473 )
474 {
475
476 //ToggleFGActivate(pti); // win.c line 2662 fail
477 if (Wnd)
478 {
479 IntSetFocusMessageQueue(Wnd->head.pti->MessageQueue);
480 gptiForeground = Wnd->head.pti;
481 //ERR("Set Foreground pti 0x%p Q 0x%p hWnd 0x%p\n",Wnd->head.pti, Wnd->head.pti->MessageQueue,Wnd->head.h);
482 }
483 else
484 {
485 IntSetFocusMessageQueue(NULL);
486 gptiForeground = NULL;
487 //ERR("Set Foreground pti 0x0 Q 0x0 hWnd 0x0\n");
488 }
489 /*
490 Henri Verbeet,
491 What happens is that we get the WM_WINE_SETACTIVEWINDOW message sent by the
492 other thread after we already changed the foreground window back to our own
493 window.
494 */
495 //ERR("SFAFW: 1\n");
496 FindRemoveAsyncMsg(Wnd, 0); // Do this to fix test_SFW todos!
497
498 fgRet = TRUE;
499 }
500
501 // Fix FG Bounce with regedit.
502 if (hWndPrev != hWnd )
503 {
504 if (PrevForegroundQueue &&
505 fgRet &&
506 PrevForegroundQueue->spwndActive)
507 {
508 //ERR("SFGW: Send NULL to 0x%x\n",hWndPrev);
509 if (pti->MessageQueue == PrevForegroundQueue)
510 {
511 //ERR("SFGW: TI same as Prev TI\n");
512 co_IntSetActiveWindow(NULL, FALSE, TRUE, FALSE);
513 }
514 else if (pWndPrev)
515 {
516 //ERR("SFGW Deactivate: TI not same as Prev TI\n");
517 // No real reason to wait here.
518 co_IntSendMessageNoWait(hWndPrev, WM_ASYNC_SETACTIVEWINDOW, 0, 0 );
519 }
520 }
521 }
522
523 if (!Wnd) return FALSE; // Always return false.
524
525 if (pti->MessageQueue == Wnd->head.pti->MessageQueue)
526 {
527 //ERR("Same PQ and WQ go active.\n");
528 Ret = co_IntSetActiveWindow(Wnd, MouseActivate, TRUE, FALSE);
529 }
530 else if (Wnd->head.pti->MessageQueue->spwndActive == Wnd)
531 {
532 //ERR("Same Active and Wnd.\n");
533 Ret = TRUE;
534 }
535 else
536 {
537 //ERR("Activate Not same PQ and WQ and Wnd.\n");
538 co_IntSendMessage(hWnd, WM_ASYNC_SETACTIVEWINDOW, (WPARAM)Wnd, (LPARAM)MouseActivate );
539 Ret = TRUE;
540 }
541 return Ret && fgRet;
542 }
543
544 BOOL FASTCALL
545 co_IntSetActiveWindow(PWND Wnd OPTIONAL, BOOL bMouse, BOOL bFocus, BOOL Async)
546 {
547 PTHREADINFO pti;
548 PUSER_MESSAGE_QUEUE ThreadQueue;
549 PWND pWndChg, WndPrev; // State changes.
550 HWND hWndPrev;
551 HWND hWnd = 0;
552 BOOL InAAPM;
553 CBTACTIVATESTRUCT cbt;
554 //ERR("co_IntSetActiveWindow 1\n");
555 if (Wnd)
556 {
557 ASSERT_REFS_CO(Wnd);
558 hWnd = UserHMGetHandle(Wnd);
559 if ((Wnd->style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
560 if (Wnd == UserGetDesktopWindow()) return FALSE;
561 //ERR("co_IntSetActiveWindow 1a hWnd 0x%p\n",hWnd);
562 }
563
564 //ERR("co_IntSetActiveWindow 2\n");
565 pti = PsGetCurrentThreadWin32Thread();
566 ThreadQueue = pti->MessageQueue;
567 ASSERT(ThreadQueue != 0);
568
569 hWndPrev = ThreadQueue->spwndActive ? UserHMGetHandle(ThreadQueue->spwndActive) : NULL;
570
571 pWndChg = ThreadQueue->spwndActive; // Keep to notify of a preemptive switch.
572
573 while (Wnd)
574 {
575 BOOL Ret, DoFG, AllowFG;
576
577 if (Wnd->state & WNDS_BEINGACTIVATED) return TRUE;
578
579 if (ThreadQueue == Wnd->head.pti->MessageQueue)
580 {
581 if (IsAllowedFGActive(pti, Wnd))
582 {
583 DoFG = TRUE;
584 }
585 else
586 {
587 //ERR("co_IntSetActiveWindow 3 Go Out!\n");
588 break;
589 }
590 AllowFG = !pti->cVisWindows; // Nothing is visable.
591 //ERR("co_IntSetActiveWindow 3a DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
592 }
593 else //if (ThreadQueue != Wnd->head.pti->MessageQueue)
594 {
595 //PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue();
596 // Rule 1 & 4, We are foreground so set this FG window or NULL foreground....
597 //if (!ForegroundQueue || ForegroundQueue == ThreadQueue)
598 if (!gpqForeground || gpqForeground == ThreadQueue)
599 {
600 DoFG = TRUE;
601 }
602 else
603 DoFG = FALSE;
604 if (DoFG)
605 {
606 if (pti->TIF_flags & TIF_ALLOWFOREGROUNDACTIVATE || pti->cVisWindows)
607 AllowFG = TRUE;
608 else
609 AllowFG = FALSE;
610 }
611 else
612 AllowFG = FALSE;
613 //ERR("co_IntSetActiveWindow 3b DoFG = %d AllowFG = %d\n",DoFG,AllowFG);
614 }
615 Ret = FALSE;
616 if (DoFG)
617 {
618 pti->TIF_flags |= TIF_ALLOWFOREGROUNDACTIVATE;
619 //ERR("co_IntSetActiveWindow 3c FG set\n");
620 Ret = co_IntSetForegroundAndFocusWindow(Wnd, bMouse);
621 if (AllowFG)
622 {
623 pti->TIF_flags |= TIF_ALLOWFOREGROUNDACTIVATE;
624 }
625 else
626 {
627 pti->TIF_flags &= ~TIF_ALLOWFOREGROUNDACTIVATE;
628 }
629 }
630 return Ret;
631 }
632
633 /* Call CBT hook chain */
634 cbt.fMouse = bMouse;
635 cbt.hWndActive = hWndPrev;
636 if (co_HOOK_CallHooks( WH_CBT, HCBT_ACTIVATE, (WPARAM)hWnd, (LPARAM)&cbt))
637 {
638 ERR("SetActiveWindow: WH_CBT Call Hook return!\n");
639 return FALSE;
640 }
641
642 if ( ThreadQueue->spwndActive && ThreadQueue->spwndActive->state & WNDS_DESTROYED )
643 ThreadQueue->spwndActive = NULL;
644 else
645 ThreadQueue->spwndActivePrev = ThreadQueue->spwndActive;
646
647 WndPrev = ThreadQueue->spwndActive; // Keep to save changing active.
648
649 if (WndPrev)
650 {
651 if (ThreadQueue == gpqForeground) gpqForegroundPrev = ThreadQueue;
652 if (!co_IntSendDeactivateMessages(UserHMGetHandle(WndPrev), hWnd)) return FALSE;
653 }
654
655 WndPrev = ThreadQueue->spwndActive; // Again keep to save changing active.
656
657 // While in calling message proc or hook:
658 // Fail if a preemptive switch was made, current active not made previous,
659 // focus window is dead or no longer the same thread queue.
660 if ( ThreadQueue->spwndActivePrev != ThreadQueue->spwndActive ||
661 pWndChg != WndPrev ||
662 (Wnd && !VerifyWnd(Wnd)) ||
663 ThreadQueue != pti->MessageQueue )
664 {
665 ERR("SetActiveWindow: Summary ERROR, active state changed!\n");
666 return FALSE;
667 }
668
669 if (!WndPrev) ThreadQueue->QF_flags &= ~QF_FOCUSNULLSINCEACTIVE;
670
671 if (Wnd) Wnd->state |= WNDS_BEINGACTIVATED;
672
673 IntNotifyWinEvent(EVENT_SYSTEM_FOREGROUND, Wnd, OBJID_WINDOW, CHILDID_SELF, WEF_SETBYWNDPTI);
674 //// Breaks Atl-Esc/Tab via User32.
675 ////FindRemoveAsyncMsg(Wnd,(WPARAM)Wnd); // Clear out activate ASYNC messages.
676
677 /* check if the specified window can be set in the input data of a given queue */
678 if ( !Wnd || ThreadQueue == Wnd->head.pti->MessageQueue)
679 {
680 /* set the current thread active window */
681 ThreadQueue->spwndActive = Wnd;
682 }
683
684 WndPrev = VerifyWnd(ThreadQueue->spwndActivePrev); // Now should be set but verify it again.
685
686 InAAPM = co_IntSendActivateMessages(WndPrev, Wnd, bMouse, Async);
687
688 /* now change focus if necessary */
689 //// Fixes CORE-6452 allows setting focus on window.
690 if (bFocus && !(ThreadQueue->QF_flags & QF_FOCUSNULLSINCEACTIVE))
691 {
692 /* Do not change focus if the window is no longer active */
693 if (pti->MessageQueue->spwndActive != IntGetNonChildAncestor(pti->MessageQueue->spwndFocus))
694 {
695 PWND pWndSend = pti->MessageQueue->spwndActive;
696 // Clear focus if the active window is minimized.
697 if (pWndSend && pti->MessageQueue->spwndActive->style & WS_MINIMIZE) pWndSend = NULL;
698 // Send focus messages and if so, set the focus.
699 IntSendFocusMessages( pti, pWndSend);
700 }
701 }
702 ////
703 if (InAAPM)
704 {
705 pti->TIF_flags &= ~TIF_INACTIVATEAPPMSG;
706 }
707
708 // FIXME: Used in the menu loop!!!
709 ThreadQueue->QF_flags |= QF_ACTIVATIONCHANGE;
710
711 //ERR("co_IntSetActiveWindow Exit\n");
712 if (Wnd) Wnd->state &= ~WNDS_BEINGACTIVATED;
713 return (ThreadQueue->spwndActive == Wnd);
714 }
715
716 BOOL FASTCALL
717 co_IntMouseActivateWindow(PWND Wnd)
718 {
719 TRACE("Mouse Active\n");
720 return co_IntSetForegroundAndFocusWindow(Wnd, TRUE);
721 }
722
723 BOOL FASTCALL
724 UserSetActiveWindow(PWND Wnd)
725 {
726 PTHREADINFO pti = PsGetCurrentThreadWin32Thread();
727
728 if (Wnd) // Must have a window!
729 {
730 if ((Wnd->style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
731
732 return co_IntSetActiveWindow(Wnd, FALSE, TRUE, FALSE);
733 }
734 /*
735 Yes your eye are not deceiving you~!
736
737 First part of wines Win.c test_SetActiveWindow:
738
739 flush_events( TRUE );
740 ShowWindow(hwnd, SW_HIDE);
741 SetFocus(0);
742 SetActiveWindow(0);
743 check_wnd_state(0, 0, 0, 0); <-- This should pass if ShowWindow does it's job!!! As of 10/28/2012 it does!
744
745 Now Handle wines Msg.c test_SetActiveWindow( 0 )...
746 */
747 TRACE("USAW: Previous active window\n");
748 if ( gpqForegroundPrev &&
749 gpqForegroundPrev->spwndActivePrev &&
750 (gpqForegroundPrev->spwndActivePrev->style & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE &&
751 !(gpqForegroundPrev->spwndActivePrev->state2 & WNDS2_BOTTOMMOST) &&
752 (Wnd = VerifyWnd(gpqForegroundPrev->spwndActivePrev)) != NULL )
753 {
754 TRACE("USAW:PAW hwnd %p\n",Wnd?Wnd->head.h:NULL);
755 return co_IntSetActiveWindow(Wnd, FALSE, TRUE, FALSE);
756 }
757
758 // Activate anyone but the active window.
759 if ( pti->MessageQueue->spwndActive &&
760 (Wnd = VerifyWnd(pti->MessageQueue->spwndActive)) != NULL )
761 {
762 ERR("USAW:AOWM hwnd %p\n",Wnd?Wnd->head.h:NULL);
763 if (!ActivateOtherWindowMin(Wnd))
764 {
765 // Okay, now go find someone else to play with!
766 ERR("USAW: Going to WPAOW\n");
767 co_WinPosActivateOtherWindow(Wnd);
768 }
769 return TRUE;
770 }
771
772 TRACE("USAW: Nothing\n");
773 return FALSE;
774 }
775
776 HWND FASTCALL
777 co_UserSetFocus(PWND Window)
778 {
779 HWND hWndPrev = 0;
780 PWND pwndTop;
781 PTHREADINFO pti;
782 PUSER_MESSAGE_QUEUE ThreadQueue;
783
784 if (Window)
785 ASSERT_REFS_CO(Window);
786
787 pti = PsGetCurrentThreadWin32Thread();
788 ThreadQueue = pti->MessageQueue;
789 ASSERT(ThreadQueue != 0);
790
791 TRACE("Enter SetFocus hWnd 0x%p pti 0x%p\n",Window ? UserHMGetHandle(Window) : 0, pti );
792
793 hWndPrev = ThreadQueue->spwndFocus ? UserHMGetHandle(ThreadQueue->spwndFocus) : 0;
794
795 if (Window != 0)
796 {
797 if (hWndPrev == UserHMGetHandle(Window))
798 {
799 return hWndPrev ? (IntIsWindow(hWndPrev) ? hWndPrev : 0) : 0; /* Nothing to do */
800 }
801
802 if (Window->head.pti->MessageQueue != ThreadQueue)
803 {
804 ERR("SetFocus Must have the same Q!\n");
805 return 0;
806 }
807
808 /* Check if we can set the focus to this window */
809 //// Fixes wine win test_SetParent both "todo" line 3710 and 3720...
810 for (pwndTop = Window; pwndTop; pwndTop = pwndTop->spwndParent)
811 {
812 if (pwndTop->style & (WS_MINIMIZED|WS_DISABLED)) return 0;
813 if ((pwndTop->style & (WS_POPUP|WS_CHILD)) != WS_CHILD) break;
814 if (pwndTop->spwndParent == NULL) break;
815 }
816 ////
817 if (co_HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, (WPARAM)Window->head.h, (LPARAM)hWndPrev))
818 {
819 ERR("SetFocus 1 WH_CBT Call Hook return!\n");
820 return 0;
821 }
822
823 /* Activate pwndTop if needed. */
824 if (pwndTop != ThreadQueue->spwndActive)
825 {
826 PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue(); // Keep it based on desktop.
827 if (ThreadQueue != ForegroundQueue && IsAllowedFGActive(pti, pwndTop)) // Rule 2 & 3.
828 {
829 //ERR("SetFocus: Set Foreground!\n");
830 if (!(pwndTop->style & WS_VISIBLE))
831 {
832 pti->ppi->W32PF_flags |= W32PF_ALLOWFOREGROUNDACTIVATE;
833 }
834 if (!co_IntSetForegroundAndFocusWindow(pwndTop, FALSE))
835 {
836 ERR("SetFocus: Set Foreground and Focus Failed!\n");
837 return 0;
838 }
839 }
840
841 /* Set Active when it is needed. */
842 if (pwndTop != ThreadQueue->spwndActive)
843 {
844 //ERR("SetFocus: Set Active!\n");
845 if (!co_IntSetActiveWindow(pwndTop, FALSE, FALSE, FALSE))
846 {
847 ERR("SetFocus: Set Active Failed!\n");
848 return 0;
849 }
850 }
851
852 /* Abort if window destroyed */
853 if (Window->state2 & WNDS2_INDESTROY) return 0;
854 /* Do not change focus if the window is no longer active */
855 if (pwndTop != ThreadQueue->spwndActive)
856 {
857 ERR("SetFocus: Top window did not go active!\n");
858 return 0;
859 }
860 }
861
862 // Check again! SetActiveWindow could have set the focus via WM_ACTIVATE.
863 hWndPrev = ThreadQueue->spwndFocus ? UserHMGetHandle(ThreadQueue->spwndFocus) : 0;
864
865 IntSendFocusMessages( pti, Window);
866
867 TRACE("Focus: %p -> %p\n", hWndPrev, Window->head.h);
868 }
869 else /* NULL hwnd passed in */
870 {
871 if (co_HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, (WPARAM)0, (LPARAM)hWndPrev))
872 {
873 ERR("SetFocus: 2 WH_CBT Call Hook return!\n");
874 return 0;
875 }
876
877 /* set the current thread focus window null */
878 IntSendFocusMessages( pti, NULL);
879 }
880 return hWndPrev ? (IntIsWindow(hWndPrev) ? hWndPrev : 0) : 0;
881 }
882
883 HWND FASTCALL
884 UserGetForegroundWindow(VOID)
885 {
886 PUSER_MESSAGE_QUEUE ForegroundQueue;
887
888 ForegroundQueue = IntGetFocusMessageQueue();
889 return( ForegroundQueue ? (ForegroundQueue->spwndActive ? UserHMGetHandle(ForegroundQueue->spwndActive) : 0) : 0);
890 }
891
892 HWND FASTCALL UserGetActiveWindow(VOID)
893 {
894 PTHREADINFO pti;
895 PUSER_MESSAGE_QUEUE ThreadQueue;
896
897 pti = PsGetCurrentThreadWin32Thread();
898 ThreadQueue = pti->MessageQueue;
899 return( ThreadQueue ? (ThreadQueue->spwndActive ? UserHMGetHandle(ThreadQueue->spwndActive) : 0) : 0);
900 }
901
902 HWND APIENTRY
903 IntGetCapture(VOID)
904 {
905 PTHREADINFO pti;
906 PUSER_MESSAGE_QUEUE ThreadQueue;
907 DECLARE_RETURN(HWND);
908
909 TRACE("Enter IntGetCapture\n");
910
911 pti = PsGetCurrentThreadWin32Thread();
912 ThreadQueue = pti->MessageQueue;
913 RETURN( ThreadQueue ? (ThreadQueue->spwndCapture ? UserHMGetHandle(ThreadQueue->spwndCapture) : 0) : 0);
914
915 CLEANUP:
916 TRACE("Leave IntGetCapture, ret=%p\n", _ret_);
917 END_CLEANUP;
918 }
919
920 HWND FASTCALL
921 co_UserSetCapture(HWND hWnd)
922 {
923 PTHREADINFO pti;
924 PUSER_MESSAGE_QUEUE ThreadQueue;
925 PWND pWnd, Window = NULL;
926 HWND hWndPrev;
927
928 pti = PsGetCurrentThreadWin32Thread();
929 ThreadQueue = pti->MessageQueue;
930
931 if (ThreadQueue->QF_flags & QF_CAPTURELOCKED)
932 return NULL;
933
934 if (hWnd && (Window = UserGetWindowObject(hWnd)))
935 {
936 if (Window->head.pti->MessageQueue != ThreadQueue)
937 {
938 ERR("Window Thread does not match Current!\n");
939 return NULL;
940 }
941 }
942
943 hWndPrev = MsqSetStateWindow(pti, MSQ_STATE_CAPTURE, hWnd);
944
945 if (hWndPrev)
946 {
947 pWnd = UserGetWindowObject(hWndPrev);
948 if (pWnd)
949 IntNotifyWinEvent(EVENT_SYSTEM_CAPTUREEND, pWnd, OBJID_WINDOW, CHILDID_SELF, WEF_SETBYWNDPTI);
950 }
951
952 if (Window)
953 IntNotifyWinEvent(EVENT_SYSTEM_CAPTURESTART, Window, OBJID_WINDOW, CHILDID_SELF, WEF_SETBYWNDPTI);
954
955 //
956 // Only send the message if we have a previous Window!
957 // Fix msg_menu tracking popup menu and win test_capture_4!!!!
958 //
959 if (hWndPrev)
960 {
961 if (ThreadQueue->MenuOwner && Window) ThreadQueue->QF_flags |= QF_CAPTURELOCKED;
962
963 co_IntSendMessage(hWndPrev, WM_CAPTURECHANGED, 0, (LPARAM)hWnd);
964
965 ThreadQueue->QF_flags &= ~QF_CAPTURELOCKED;
966 }
967
968 if (hWnd == NULL) // Release mode.
969 {
970 MOUSEINPUT mi;
971 /// These are HACKS!
972 /* Also remove other windows if not capturing anymore */
973 MsqSetStateWindow(pti, MSQ_STATE_MENUOWNER, NULL);
974 MsqSetStateWindow(pti, MSQ_STATE_MOVESIZE, NULL);
975 ///
976 /* Somebody may have missed some mouse movements */
977 mi.dx = 0;
978 mi.dy = 0;
979 mi.mouseData = 0;
980 mi.dwFlags = MOUSEEVENTF_MOVE;
981 mi.time = 0;
982 mi.dwExtraInfo = 0;
983 UserSendMouseInput(&mi, FALSE);
984 }
985 return hWndPrev;
986 }
987
988 /*
989 API Call
990 */
991 BOOL
992 FASTCALL
993 IntReleaseCapture(VOID)
994 {
995 PTHREADINFO pti;
996 PUSER_MESSAGE_QUEUE ThreadQueue;
997
998 pti = PsGetCurrentThreadWin32Thread();
999 ThreadQueue = pti->MessageQueue;
1000
1001 // Can not release inside WM_CAPTURECHANGED!!
1002 if (ThreadQueue->QF_flags & QF_CAPTURELOCKED) return FALSE;
1003
1004 co_UserSetCapture(NULL);
1005
1006 return TRUE;
1007 }
1008
1009 /*
1010 API Call
1011 */
1012 BOOL FASTCALL
1013 co_IntSetForegroundWindow(PWND Window)
1014 {
1015 if (Window) ASSERT_REFS_CO(Window);
1016
1017 return co_IntSetForegroundAndFocusWindow(Window, FALSE);
1018 }
1019
1020 /*
1021 API Call
1022 */
1023 BOOL FASTCALL
1024 co_IntSetForegroundWindowMouse(PWND Window)
1025 {
1026 if (Window) ASSERT_REFS_CO(Window);
1027
1028 return co_IntSetForegroundAndFocusWindow(Window, TRUE);
1029 }
1030
1031 /*
1032 API Call
1033 */
1034 BOOL FASTCALL
1035 IntLockSetForegroundWindow(UINT uLockCode)
1036 {
1037 ULONG Err = ERROR_ACCESS_DENIED;
1038 PPROCESSINFO ppi = PsGetCurrentProcessWin32Process();
1039 switch (uLockCode)
1040 {
1041 case LSFW_LOCK:
1042 if ( CanForceFG(ppi) && !gppiLockSFW )
1043 {
1044 gppiLockSFW = ppi;
1045 return TRUE;
1046 }
1047 break;
1048 case LSFW_UNLOCK:
1049 if ( gppiLockSFW == ppi)
1050 {
1051 gppiLockSFW = NULL;
1052 return TRUE;
1053 }
1054 break;
1055 default:
1056 Err = ERROR_INVALID_PARAMETER;
1057 }
1058 EngSetLastError(Err);
1059 return FALSE;
1060 }
1061
1062 /*
1063 API Call
1064 */
1065 BOOL FASTCALL
1066 IntAllowSetForegroundWindow(DWORD dwProcessId)
1067 {
1068 PPROCESSINFO ppi, ppiCur;
1069 PEPROCESS Process = NULL;
1070
1071 ppi = NULL;
1072 if (dwProcessId != ASFW_ANY)
1073 {
1074 if (!NT_SUCCESS(PsLookupProcessByProcessId((HANDLE)dwProcessId, &Process)))
1075 {
1076 EngSetLastError(ERROR_INVALID_PARAMETER);
1077 return FALSE;
1078 }
1079 ppi = PsGetProcessWin32Process(Process);
1080 if (!ppi)
1081 {
1082 ObDereferenceObject(Process);
1083 return FALSE;
1084 }
1085 }
1086 ppiCur = PsGetCurrentProcessWin32Process();
1087 if (!CanForceFG(ppiCur))
1088 {
1089 if (Process) ObDereferenceObject(Process);
1090 EngSetLastError(ERROR_ACCESS_DENIED);
1091 return FALSE;
1092 }
1093 if (dwProcessId == ASFW_ANY)
1094 { // All processes will be enabled to set the foreground window.
1095 //ERR("ptiLastInput is CLEARED!!\n");
1096 ptiLastInput = NULL;
1097 }
1098 else
1099 { // Rule #3, last input event in force.
1100 ERR("ptiLastInput is SET!!\n");
1101 //ptiLastInput = ppi->ptiList; // See CORE-6384 & CORE-7030.
1102 ObDereferenceObject(Process);
1103 }
1104 return TRUE;
1105 }
1106
1107 /*
1108 * @implemented
1109 */
1110 HWND APIENTRY
1111 NtUserGetForegroundWindow(VOID)
1112 {
1113 DECLARE_RETURN(HWND);
1114
1115 TRACE("Enter NtUserGetForegroundWindow\n");
1116 UserEnterExclusive();
1117
1118 RETURN( UserGetForegroundWindow());
1119
1120 CLEANUP:
1121 TRACE("Leave NtUserGetForegroundWindow, ret=%p\n",_ret_);
1122 UserLeave();
1123 END_CLEANUP;
1124 }
1125
1126 HWND APIENTRY
1127 NtUserSetActiveWindow(HWND hWnd)
1128 {
1129 USER_REFERENCE_ENTRY Ref;
1130 HWND hWndPrev;
1131 PWND Window;
1132 DECLARE_RETURN(HWND);
1133
1134 TRACE("Enter NtUserSetActiveWindow(%p)\n", hWnd);
1135 UserEnterExclusive();
1136
1137 Window = NULL;
1138 if (hWnd)
1139 {
1140 if (!(Window = UserGetWindowObject(hWnd)))
1141 {
1142 ERR("NtUserSetActiveWindow: Invalid handle 0x%p!\n",hWnd);
1143 RETURN( NULL);
1144 }
1145 }
1146
1147 if (!Window ||
1148 Window->head.pti->MessageQueue == gptiCurrent->MessageQueue)
1149 {
1150 hWndPrev = gptiCurrent->MessageQueue->spwndActive ? UserHMGetHandle(gptiCurrent->MessageQueue->spwndActive) : NULL;
1151 if (Window) UserRefObjectCo(Window, &Ref);
1152 UserSetActiveWindow(Window);
1153 if (Window) UserDerefObjectCo(Window);
1154 RETURN( hWndPrev ? (IntIsWindow(hWndPrev) ? hWndPrev : 0) : 0 );
1155 }
1156 RETURN( NULL);
1157
1158 CLEANUP:
1159 TRACE("Leave NtUserSetActiveWindow, ret=%p\n",_ret_);
1160 UserLeave();
1161 END_CLEANUP;
1162 }
1163
1164 /*
1165 * @implemented
1166 */
1167 HWND APIENTRY
1168 NtUserSetCapture(HWND hWnd)
1169 {
1170 DECLARE_RETURN(HWND);
1171
1172 TRACE("Enter NtUserSetCapture(%p)\n", hWnd);
1173 UserEnterExclusive();
1174
1175 RETURN( co_UserSetCapture(hWnd));
1176
1177 CLEANUP:
1178 TRACE("Leave NtUserSetCapture, ret=%p\n", _ret_);
1179 UserLeave();
1180 END_CLEANUP;
1181 }
1182
1183 /*
1184 * @implemented
1185 */
1186 HWND APIENTRY
1187 NtUserSetFocus(HWND hWnd)
1188 {
1189 PWND Window;
1190 USER_REFERENCE_ENTRY Ref;
1191 DECLARE_RETURN(HWND);
1192 HWND ret;
1193
1194 TRACE("Enter NtUserSetFocus(%p)\n", hWnd);
1195 UserEnterExclusive();
1196
1197 if (hWnd)
1198 {
1199 if (!(Window = UserGetWindowObject(hWnd)))
1200 {
1201 ERR("NtUserSetFocus: Invalid handle 0x%p!\n",hWnd);
1202 RETURN(NULL);
1203 }
1204
1205 UserRefObjectCo(Window, &Ref);
1206 ret = co_UserSetFocus(Window);
1207 UserDerefObjectCo(Window);
1208
1209 RETURN(ret);
1210 }
1211 else
1212 {
1213 RETURN( co_UserSetFocus(0));
1214 }
1215
1216 CLEANUP:
1217 TRACE("Leave NtUserSetFocus, ret=%p\n",_ret_);
1218 UserLeave();
1219 END_CLEANUP;
1220 }
1221
1222 /* EOF */