1 /* 2 * COPYRIGHT: See COPYING in the top level directory 3 * PROJECT: ReactOS Win32k subsystem 4 * PURPOSE: Focus functions 5 * FILE: win32ss/user/ntuser/focus.c 6 * PROGRAMER: ReactOS Team 7 */ 8 9 #include <win32k.h> 10 #include <ddk/immdev.h> 11 DBG_DEFAULT_CHANNEL(UserFocus); 12 13 PUSER_MESSAGE_QUEUE gpqForeground = NULL; 14 PUSER_MESSAGE_QUEUE gpqForegroundPrev = NULL; 15 PTHREADINFO gptiForeground = NULL; 16 PPROCESSINFO gppiLockSFW = NULL; 17 ULONG guSFWLockCount = 0; // Rule #8, No menus are active. So should be zero. 18 PTHREADINFO ptiLastInput = NULL; 19 HWND ghwndOldFullscreen = NULL; 20 21 /* 22 Check locking of a process or one or more menus are active. 23 */ 24 BOOL FASTCALL 25 IsFGLocked(VOID) 26 { 27 return (gppiLockSFW || guSFWLockCount); 28 } 29 30 /* 31 Get capture window via foreground Queue. 32 */ 33 HWND FASTCALL 34 IntGetCaptureWindow(VOID) 35 { 36 PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue(); 37 return ( ForegroundQueue ? (ForegroundQueue->spwndCapture ? UserHMGetHandle(ForegroundQueue->spwndCapture) : 0) : 0); 38 } 39 40 HWND FASTCALL 41 IntGetThreadFocusWindow(VOID) 42 { 43 PTHREADINFO pti; 44 PUSER_MESSAGE_QUEUE ThreadQueue; 45 46 pti = PsGetCurrentThreadWin32Thread(); 47 ThreadQueue = pti->MessageQueue; 48 if (!ThreadQueue) 49 return NULL; 50 return ThreadQueue->spwndFocus ? UserHMGetHandle(ThreadQueue->spwndFocus) : 0; 51 } 52 53 BOOL FASTCALL IntIsWindowFullscreen(PWND Window) 54 { 55 RECTL rclAnd, rclMonitor, rclWindow; 56 PMONITOR pMonitor; 57 58 if (!Window || !(Window->style & WS_VISIBLE) || (Window->style & WS_CHILD) || 59 (Window->ExStyle & WS_EX_TOOLWINDOW) || !IntGetWindowRect(Window, &rclWindow)) 60 { 61 return FALSE; 62 } 63 64 pMonitor = UserGetPrimaryMonitor(); 65 if (!pMonitor) 66 { 67 RECTL_vSetRect(&rclMonitor, 0, 0, 68 UserGetSystemMetrics(SM_CXSCREEN), UserGetSystemMetrics(SM_CYSCREEN)); 69 } 70 else 71 { 72 rclMonitor = *(LPRECTL)&pMonitor->rcMonitor; 73 } 74 75 RECTL_bIntersectRect(&rclAnd, &rclMonitor, &rclWindow); 76 return RtlEqualMemory(&rclAnd, &rclMonitor, sizeof(RECTL)); 77 } 78 79 BOOL FASTCALL IntCheckFullscreen(PWND Window) 80 { 81 HWND hWnd; 82 83 if (ghwndOldFullscreen && !IntIsWindowFullscreen(ValidateHwndNoErr(ghwndOldFullscreen))) 84 ghwndOldFullscreen = NULL; 85 86 if (!IntIsWindowFullscreen(Window)) 87 return FALSE; 88 89 hWnd = UserHMGetHandle(Window); 90 if (ghwndOldFullscreen != hWnd) 91 { 92 co_IntShellHookNotify(HSHELL_RUDEAPPACTIVATED, (WPARAM)hWnd, TRUE); 93 ghwndOldFullscreen = hWnd; 94 } 95 return TRUE; 96 } 97 98 VOID FASTCALL 99 UpdateShellHook(PWND Window) 100 { 101 if (IntCheckFullscreen(Window)) 102 return; 103 104 if ( Window->spwndParent == UserGetDesktopWindow() && 105 (!(Window->ExStyle & WS_EX_TOOLWINDOW) || 106 (Window->ExStyle & WS_EX_APPWINDOW))) 107 { 108 // FIXME lParam; The value is TRUE if the window is in full-screen mode, or FALSE otherwise. 109 co_IntShellHookNotify(HSHELL_WINDOWACTIVATED, (WPARAM) UserHMGetHandle(Window), FALSE); 110 } 111 else 112 { 113 co_IntShellHookNotify(HSHELL_WINDOWACTIVATED, 0, FALSE); 114 } 115 } 116 117 BOOL FASTCALL 118 co_IntSendDeactivateMessages(HWND hWndPrev, HWND hWnd, BOOL Clear) 119 { 120 USER_REFERENCE_ENTRY RefPrev; 121 PWND WndPrev; 122 BOOL Ret = TRUE; 123 LPARAM lParam = hWnd ? (LPARAM)hWnd : 0; 124 125 if (hWndPrev && (WndPrev = ValidateHwndNoErr(hWndPrev))) 126 { 127 UserRefObjectCo(WndPrev, &RefPrev); 128 129 if (co_IntSendMessage(hWndPrev, WM_NCACTIVATE, FALSE, lParam)) 130 { 131 co_IntSendMessage(hWndPrev, WM_ACTIVATE, 132 MAKEWPARAM(WA_INACTIVE, (WndPrev->style & WS_MINIMIZE) != 0), 133 (LPARAM)hWnd); 134 135 if (WndPrev && Clear) 136 WndPrev->state &= ~(WNDS_ACTIVEFRAME|WNDS_HASCAPTION); 137 } 138 else 139 { 140 ERR("Application is keeping itself Active to prevent the change!\n"); 141 Ret = FALSE; 142 } 143 144 UserDerefObjectCo(WndPrev); 145 } 146 return Ret; 147 } 148 149 // Win: xxxFocusSetInputContext 150 VOID IntFocusSetInputContext(PWND pWnd, BOOL bActivate, BOOL bCallback) 151 { 152 PTHREADINFO pti; 153 PWND pImeWnd; 154 USER_REFERENCE_ENTRY Ref; 155 HWND hImeWnd; 156 WPARAM wParam; 157 LPARAM lParam; 158 159 if (!pWnd || !pWnd->pcls || IS_WND_IMELIKE(pWnd)) 160 return; 161 162 pti = pWnd->head.pti; 163 if (!pti || (pti->TIF_flags & TIF_INCLEANUP)) 164 return; 165 166 pImeWnd = pti->spwndDefaultIme; 167 if (!pImeWnd) 168 return; 169 170 UserRefObjectCo(pImeWnd, &Ref); 171 172 hImeWnd = UserHMGetHandle(pImeWnd); 173 wParam = (bActivate ? IMS_IMEACTIVATE : IMS_IMEDEACTIVATE); 174 lParam = (LPARAM)UserHMGetHandle(pWnd); 175 176 if (bCallback) 177 co_IntSendMessageWithCallBack(hImeWnd, WM_IME_SYSTEM, wParam, lParam, NULL, 0, NULL); 178 else 179 co_IntSendMessage(hImeWnd, WM_IME_SYSTEM, wParam, lParam); 180 181 UserDerefObjectCo(pImeWnd); 182 } 183 184 // 185 // Deactivating the foreground message queue. 186 // 187 // Release Active, Capture and Focus Windows associated with this message queue. 188 // 189 // Win: xxxDeactivate 190 BOOL FASTCALL 191 IntDeactivateWindow(PTHREADINFO pti, HANDLE tid) 192 { 193 USER_REFERENCE_ENTRY Ref; 194 PTHREADINFO ptiPrev; 195 PWND pwndPrev; 196 BOOL InAAPM = FALSE; 197 PTHREADINFO ptiCurrent = PsGetCurrentThreadWin32Thread(); 198 199 if ( !pti->MessageQueue->spwndActive ) 200 { 201 TRACE("IDAW E : Nothing to do, Active is NULL! pti 0x%p tid 0x%p\n",pti,tid); 202 return TRUE; 203 } 204 205 TRACE("IDAW : pti 0x%p tid 0x%p\n",pti,tid); 206 207 if (ptiCurrent != pti) 208 { 209 IntReferenceThreadInfo(pti); 210 IntReferenceThreadInfo(ptiCurrent); 211 } 212 213 if (!(pti->TIF_flags & TIF_INACTIVATEAPPMSG) ) 214 { 215 pti->TIF_flags |= TIF_INACTIVATEAPPMSG; 216 InAAPM = TRUE; 217 } 218 219 // 220 // Check for Capture and release it. 221 // 222 if ( pti->MessageQueue->spwndCapture ) 223 { 224 MSG msg; 225 PWND pwndCapture = pti->MessageQueue->spwndCapture; 226 227 UserRefObjectCo(pwndCapture, &Ref); 228 co_IntSendMessage(UserHMGetHandle(pwndCapture), WM_CANCELMODE, 0, 0); 229 UserDerefObjectCo(pwndCapture); 230 231 /* Generate mouse move message */ 232 msg.message = WM_MOUSEMOVE; 233 msg.wParam = UserGetMouseButtonsState(); 234 msg.lParam = MAKELPARAM(gpsi->ptCursor.x, gpsi->ptCursor.y); 235 msg.pt = gpsi->ptCursor; 236 co_MsqInsertMouseMessage(&msg, 0, 0, TRUE); 237 } 238 239 // 240 // Check for Active and release it. 241 // 242 if ( pti->MessageQueue->spwndActive ) 243 { 244 pwndPrev = pti->MessageQueue->spwndActive; 245 ptiPrev = pwndPrev->head.pti; 246 247 if (!co_IntSendDeactivateMessages(UserHMGetHandle(pwndPrev), 0, TRUE)) 248 { 249 if (InAAPM) pti->TIF_flags &= ~TIF_INACTIVATEAPPMSG; 250 if (ptiCurrent != pti) 251 { 252 IntDereferenceThreadInfo(pti); 253 IntDereferenceThreadInfo(ptiCurrent); 254 } 255 return FALSE; 256 } 257 258 if ( pti->MessageQueue->spwndActive == pwndPrev ) 259 { 260 pti->MessageQueue->spwndActivePrev = pwndPrev; 261 pti->MessageQueue->spwndActive = NULL; 262 } 263 264 if (ptiPrev->TIF_flags & TIF_INCLEANUP) ptiPrev = NULL; 265 } 266 else 267 { 268 ptiPrev = pti; 269 pwndPrev = (PWND)-1; // Avoid zero Active window. 270 } 271 272 if ( ptiPrev ) 273 { 274 HANDLE OldTID = PsGetThreadId(ptiPrev->pEThread); 275 PWND cWindow; 276 HWND *List, *phWnd; 277 278 List = IntWinListChildren(UserGetDesktopWindow()); 279 if ( List ) 280 { 281 if ( OldTID ) 282 { 283 for (phWnd = List; *phWnd; ++phWnd) 284 { 285 cWindow = ValidateHwndNoErr(*phWnd); 286 if ( cWindow && cWindow->head.pti == ptiPrev ) 287 { // FALSE if the window is being deactivated, 288 // ThreadId that owns the window being activated. 289 //ERR("IDW : WM_ACTIVATEAPP(0) hwnd %p tid Old %p New %p\n",UserHMGetHandle(cWindow),OldTID,tid); 290 UserRefObjectCo(cWindow, &Ref); 291 co_IntSendMessage(*phWnd, WM_ACTIVATEAPP, FALSE, (LPARAM)tid); 292 UserDerefObjectCo(cWindow); 293 } 294 } 295 } 296 ExFreePoolWithTag(List, USERTAG_WINDOWLIST); 297 } 298 } 299 300 // 301 // Now check for a change (Bounce), if Active same as previous window, release it too. 302 // 303 if ( pti->MessageQueue->spwndActive == pwndPrev ) 304 { 305 if (!co_IntSendDeactivateMessages(UserHMGetHandle(pwndPrev), 0, FALSE)) 306 { 307 if (InAAPM) pti->TIF_flags &= ~TIF_INACTIVATEAPPMSG; 308 if (ptiCurrent != pti) 309 { 310 IntDereferenceThreadInfo(pti); 311 IntDereferenceThreadInfo(ptiCurrent); 312 } 313 return FALSE; 314 } 315 316 if ( pti->MessageQueue->spwndActive == pwndPrev ) 317 { 318 pti->MessageQueue->spwndActivePrev = pwndPrev; 319 pti->MessageQueue->spwndActive = NULL; 320 } 321 } 322 323 // 324 // Check for Focus and release it. 325 // 326 if ( pti->MessageQueue->spwndFocus ) 327 { 328 PWND pwndFocus = pti->MessageQueue->spwndFocus; 329 330 // 331 // Fix win.c:test_SetForegroundWindow:SetActiveWindow(0)! 332 // 333 pti->MessageQueue->spwndFocus = NULL; // Null out Focus. 334 335 UserRefObjectCo(pwndFocus, &Ref); 336 co_IntSendMessage(UserHMGetHandle(pwndFocus), WM_KILLFOCUS, 0, 0); 337 if (IS_IMM_MODE()) 338 { 339 IntFocusSetInputContext(pwndFocus, FALSE, FALSE); 340 } 341 UserDerefObjectCo(pwndFocus); 342 } 343 344 if (InAAPM) pti->TIF_flags &= ~TIF_INACTIVATEAPPMSG; 345 if (ptiCurrent != pti) 346 { 347 IntDereferenceThreadInfo(pti); 348 IntDereferenceThreadInfo(ptiCurrent); 349 } 350 return TRUE; 351 } 352 353 // 354 // Activating another threads foreground window after a switch. 355 // 356 VOID FASTCALL 357 IntActivateWindow(PWND Wnd, PTHREADINFO pti, HANDLE tid, DWORD Type) 358 { 359 USER_REFERENCE_ENTRY Ref; 360 PUSER_MESSAGE_QUEUE pmq = pti->MessageQueue; 361 362 if (Wnd) 363 { 364 Wnd = VerifyWnd(Wnd); 365 366 if (!Wnd) return; 367 368 UserRefObjectCo(Wnd, &Ref); 369 370 if (!gpqForeground) 371 { 372 // No foreground queue set. 373 co_IntSetForegroundMessageQueue( Wnd, pti, (BOOL)Type, 0); 374 } 375 else 376 { 377 // Same Active and Wnd. 378 if ( pmq->spwndActive == Wnd ) 379 { 380 WPARAM wParam = (Wnd->head.pti->MessageQueue == gpqForeground); 381 382 co_IntSendMessage( UserHMGetHandle(Wnd), WM_NCACTIVATE, wParam, 0); 383 384 if (wParam) 385 { 386 UpdateShellHook(Wnd); 387 388 co_WinPosSetWindowPos(Wnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); 389 } 390 } 391 else // Not the same, set the active Wnd. 392 { 393 co_IntSetActiveWindow(Wnd,(BOOL)Type,TRUE,TRUE); 394 } 395 } 396 UserDerefObjectCo(Wnd); 397 } 398 else // Handle no Wnd! 399 { 400 if ( tid && // TID, 401 pmq->spwndActive && // Active WND not zero, 402 gpqForeground == pmq ) // Same message queues. 403 { 404 Wnd = pmq->spwndActive; // Use active window from current queue. 405 406 UserRefObjectCo(Wnd, &Ref); 407 408 co_IntSendMessage( UserHMGetHandle(Wnd), WM_NCACTIVATE, TRUE, 0); 409 410 UpdateShellHook(Wnd); 411 412 co_WinPosSetWindowPos(Wnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); 413 414 UserDerefObjectCo(Wnd); 415 } 416 else if (gpqForeground != pmq) 417 { 418 // Not the same message queue so clear flags for foreground switching. 419 pti->TIF_flags &= ~TIF_ALLOWFOREGROUNDACTIVATE; 420 pti->ppi->W32PF_flags &= ~W32PF_ALLOWFOREGROUNDACTIVATE; 421 } 422 } 423 } 424 425 BOOL FASTCALL 426 co_IntMakeWindowActive(PWND Window) 427 { 428 PWND spwndOwner; 429 if (VerifyWnd(Window)) 430 { // Set last active for window and it's owner. 431 spwndOwner = Window; 432 while (spwndOwner->spwndOwner) 433 { 434 spwndOwner = spwndOwner->spwndOwner; 435 } 436 spwndOwner->spwndLastActive = Window; 437 return TRUE; 438 } 439 ERR("MakeWindowActive Failed!\n"); 440 return FALSE; 441 } 442 443 BOOL FASTCALL 444 co_IntSendActivateMessages(PWND WindowPrev, PWND Window, BOOL MouseActivate, BOOL Async) 445 { 446 USER_REFERENCE_ENTRY Ref, RefPrev, RefCall; 447 HANDLE OldTID, NewTID; 448 PTHREADINFO pti, ptiOld, ptiNew; 449 BOOL InAAPM = FALSE; 450 451 //ERR("SendActivateMessages\n"); 452 453 pti = PsGetCurrentThreadWin32Thread(); 454 455 if (Window) 456 { 457 UserRefObjectCo(Window, &Ref); 458 459 if (WindowPrev) UserRefObjectCo(WindowPrev, &RefPrev); 460 461 pti->MessageQueue->QF_flags &= ~QF_EVENTDEACTIVATEREMOVED; 462 463 /* Send palette messages */ 464 if (gpsi->PUSIFlags & PUSIF_PALETTEDISPLAY && 465 //co_IntPostOrSendMessage(UserHMGetHandle(Window), WM_QUERYNEWPALETTE, 0, 0)) 466 co_IntSendMessage(UserHMGetHandle(Window), WM_QUERYNEWPALETTE, 0, 0)) 467 { 468 UserSendNotifyMessage( HWND_BROADCAST, 469 WM_PALETTEISCHANGING, 470 (WPARAM)UserHMGetHandle(Window), 471 0); 472 } 473 //// Fixes CORE-6434. 474 if (!(Window->style & WS_CHILD)) 475 { 476 PWND pwndTemp = co_GetDesktopWindow(Window)->spwndChild; 477 478 while (pwndTemp && !(pwndTemp->style & WS_VISIBLE)) pwndTemp = pwndTemp->spwndNext; 479 480 if (Window != pwndTemp || (WindowPrev && !IntIsWindowVisible(WindowPrev))) 481 { 482 if (!Async || pti->MessageQueue == gpqForeground) 483 { 484 UINT flags = SWP_NOSIZE | SWP_NOMOVE; 485 if (Window == pwndTemp) flags |= SWP_NOACTIVATE; 486 //ERR("co_IntSendActivateMessages SetWindowPos! Async %d pti Q == FGQ %d\n",Async,pti->MessageQueue == gpqForeground); 487 co_WinPosSetWindowPos(Window, HWND_TOP, 0, 0, 0, 0, flags); 488 } 489 } 490 } 491 //// 492 //// CORE-1161 and CORE-6651 493 if (Window->spwndPrev) 494 { 495 HWND *phwndTopLevel, *phwndCurrent; 496 PWND pwndCurrent, pwndDesktop; 497 498 pwndDesktop = co_GetDesktopWindow(Window);//UserGetDesktopWindow(); 499 if (Window->spwndParent == pwndDesktop ) 500 { 501 phwndTopLevel = IntWinListChildren(pwndDesktop); 502 phwndCurrent = phwndTopLevel; 503 while(*phwndCurrent) 504 { 505 pwndCurrent = UserGetWindowObject(*phwndCurrent); 506 507 if (pwndCurrent && pwndCurrent->spwndOwner == Window ) 508 { 509 co_WinPosSetWindowPos(pwndCurrent, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE); 510 } 511 phwndCurrent++; 512 } 513 ExFreePoolWithTag(phwndTopLevel, USERTAG_WINDOWLIST); 514 } 515 } 516 //// 517 } 518 519 OldTID = WindowPrev ? IntGetWndThreadId(WindowPrev) : NULL; 520 NewTID = Window ? IntGetWndThreadId(Window) : NULL; 521 ptiOld = WindowPrev ? WindowPrev->head.pti : NULL; 522 ptiNew = Window ? Window->head.pti : NULL; 523 524 //ERR("SendActivateMessage Old -> %x, New -> %x\n", OldTID, NewTID); 525 526 if (!(pti->TIF_flags & TIF_INACTIVATEAPPMSG) && 527 (OldTID != NewTID) ) 528 { 529 PWND cWindow; 530 HWND *List, *phWnd; 531 532 List = IntWinListChildren(UserGetDesktopWindow()); 533 if ( List ) 534 { 535 if ( OldTID ) 536 { 537 ptiOld->TIF_flags |= TIF_INACTIVATEAPPMSG; 538 // Note: Do not set pci flags, this does crash! 539 for (phWnd = List; *phWnd; ++phWnd) 540 { 541 cWindow = ValidateHwndNoErr(*phWnd); 542 if (cWindow && cWindow->head.pti == ptiOld) 543 { // FALSE if the window is being deactivated, 544 // ThreadId that owns the window being activated. 545 //ERR("SAM : WM_ACTIVATEAPP(0) tid Old %p New %p\n",OldTID,NewTID); 546 UserRefObjectCo(cWindow, &RefCall); 547 co_IntSendMessage(*phWnd, WM_ACTIVATEAPP, FALSE, (LPARAM)NewTID); 548 UserDerefObjectCo(cWindow); 549 } 550 } 551 ptiOld->TIF_flags &= ~TIF_INACTIVATEAPPMSG; 552 } 553 if ( NewTID ) 554 { //// Prevents a resource crash due to reentrance! 555 InAAPM = TRUE; 556 pti->TIF_flags |= TIF_INACTIVATEAPPMSG; 557 //// 558 for (phWnd = List; *phWnd; ++phWnd) 559 { 560 cWindow = ValidateHwndNoErr(*phWnd); 561 if (cWindow && cWindow->head.pti == ptiNew) 562 { // TRUE if the window is being activated, 563 // ThreadId that owns the window being deactivated. 564 //ERR("SAM : WM_ACTIVATEAPP(1) hwnd %p tid New %p Old %p\n",UserHMGetHandle(cWindow),NewTID,OldTID); 565 UserRefObjectCo(cWindow, &RefCall); 566 co_IntSendMessage(*phWnd, WM_ACTIVATEAPP, TRUE, (LPARAM)OldTID); 567 UserDerefObjectCo(cWindow); 568 } 569 } 570 } 571 ExFreePoolWithTag(List, USERTAG_WINDOWLIST); 572 } 573 } 574 575 if (Window) 576 { 577 if (WindowPrev) 578 UserDerefObjectCo(WindowPrev); // Now allow the previous window to die. 579 580 if (Window->state & WNDS_ACTIVEFRAME) 581 { // If already active frame do not allow NCPaint. 582 //ERR("SendActivateMessage Is Active Frame!\n"); 583 Window->state |= WNDS_NONCPAINT; 584 } 585 586 if (Window->style & WS_MINIMIZE) 587 { 588 TRACE("Widow was minimized\n"); 589 } 590 591 co_IntMakeWindowActive(Window); 592 593 co_IntSendMessage( UserHMGetHandle(Window), 594 WM_NCACTIVATE, 595 (WPARAM)(Window == (gpqForeground ? gpqForeground->spwndActive : NULL)), 596 0); 597 598 co_IntSendMessage( UserHMGetHandle(Window), 599 WM_ACTIVATE, 600 MAKEWPARAM(MouseActivate ? WA_CLICKACTIVE : WA_ACTIVE, (Window->style & WS_MINIMIZE) != 0), 601 (LPARAM)(WindowPrev ? UserHMGetHandle(WindowPrev) : 0)); 602 603 if (Window->style & WS_VISIBLE) 604 UpdateShellHook(Window); 605 606 Window->state &= ~WNDS_NONCPAINT; 607 608 UserDerefObjectCo(Window); 609 } 610 return InAAPM; 611 } 612 613 // Win: xxxSendFocusMessages 614 VOID FASTCALL 615 IntSendFocusMessages( PTHREADINFO pti, PWND pWnd) 616 { 617 PWND pWndPrev; 618 PUSER_MESSAGE_QUEUE ThreadQueue = pti->MessageQueue; // Queue can change... 619 HWND hwndPrev; 620 USER_REFERENCE_ENTRY Ref; 621 622 ThreadQueue->QF_flags &= ~QF_FOCUSNULLSINCEACTIVE; 623 if (!pWnd && ThreadQueue->spwndActive) 624 { 625 ThreadQueue->QF_flags |= QF_FOCUSNULLSINCEACTIVE; 626 } 627 628 pWndPrev = ThreadQueue->spwndFocus; 629 if (pWndPrev) 630 UserRefObjectCo(pWndPrev, &Ref); 631 632 /* check if the specified window can be set in the input data of a given queue */ 633 if (!pWnd || ThreadQueue == pWnd->head.pti->MessageQueue) 634 /* set the current thread focus window */ 635 ThreadQueue->spwndFocus = pWnd; 636 637 if (pWnd) 638 { 639 if (pWndPrev) 640 { 641 co_IntSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, (WPARAM)UserHMGetHandle(pWnd), 0); 642 if (IS_IMM_MODE()) 643 { 644 IntFocusSetInputContext(pWndPrev, FALSE, FALSE); 645 } 646 } 647 if (ThreadQueue->spwndFocus == pWnd) 648 { 649 if (IS_IMM_MODE()) 650 { 651 IntFocusSetInputContext(pWnd, TRUE, FALSE); 652 } 653 654 IntNotifyWinEvent(EVENT_OBJECT_FOCUS, pWnd, OBJID_CLIENT, CHILDID_SELF, 0); 655 656 hwndPrev = (pWndPrev ? UserHMGetHandle(pWndPrev) : NULL); 657 co_IntSendMessage(UserHMGetHandle(pWnd), WM_SETFOCUS, (WPARAM)hwndPrev, 0); 658 } 659 } 660 else 661 { 662 if (pWndPrev) 663 { 664 IntNotifyWinEvent(EVENT_OBJECT_FOCUS, NULL, OBJID_CLIENT, CHILDID_SELF, 0); 665 666 co_IntSendMessage(UserHMGetHandle(pWndPrev), WM_KILLFOCUS, 0, 0); 667 if (IS_IMM_MODE()) 668 { 669 IntFocusSetInputContext(pWndPrev, FALSE, FALSE); 670 } 671 } 672 } 673 674 if (pWndPrev) 675 UserDerefObjectCo(pWndPrev); 676 } 677 678 BOOL FASTCALL 679 FindRemoveEventMsg(PTHREADINFO pti, DWORD Event, DWORD EventLast) 680 { 681 PUSER_MESSAGE Message; 682 PLIST_ENTRY Entry; 683 BOOL Ret = FALSE; 684 685 Entry = pti->PostedMessagesListHead.Flink; 686 while (Entry != &pti->PostedMessagesListHead) 687 { 688 // Scan posted queue messages to see if we received async messages. 689 Message = CONTAINING_RECORD(Entry, USER_MESSAGE, ListEntry); 690 Entry = Entry->Flink; 691 692 if (Message->dwQEvent == EventLast) 693 { 694 //ERR("Event D/SAW: Last Activate/Deactivate %d\n", EventLast); 695 return Ret; 696 } 697 698 if (Message->dwQEvent == Event) 699 { 700 //ERR("Event D/SAW: Found one in the Post Msg Queue! Activate/Deactivate %d\n", Event); 701 ClearMsgBitsMask(pti, Message->QS_Flags); 702 MsqDestroyMessage(Message); 703 Ret = TRUE; 704 } 705 } 706 return Ret; 707 } 708 709 BOOL FASTCALL 710 ToggleFGActivate(PTHREADINFO pti) 711 { 712 BOOL Ret; 713 PPROCESSINFO ppi = pti->ppi; 714 715 Ret = !!(pti->TIF_flags & TIF_ALLOWFOREGROUNDACTIVATE); 716 if (Ret) 717 { 718 pti->TIF_flags &= ~TIF_ALLOWFOREGROUNDACTIVATE; 719 } 720 else 721 Ret = !!(ppi->W32PF_flags & W32PF_ALLOWFOREGROUNDACTIVATE); 722 723 if (Ret) 724 ppi->W32PF_flags &= ~W32PF_ALLOWFOREGROUNDACTIVATE; 725 //ERR("ToggleFGActivate is %d\n",Ret); 726 return Ret; 727 } 728 729 BOOL FASTCALL 730 IsAllowedFGActive(PTHREADINFO pti, PWND Wnd) 731 { 732 // Not allowed if one or more,, 733 if (!ToggleFGActivate(pti) || // bits not set, 734 pti->rpdesk != gpdeskInputDesktop || // not current Desktop, 735 pti->MessageQueue == gpqForeground || // if already the queue foreground, 736 IsFGLocked() || // foreground is locked, 737 Wnd->ExStyle & WS_EX_NOACTIVATE ) // or,,, does not become the foreground window when the user clicks it. 738 { 739 return FALSE; 740 } 741 //ERR("IsAllowedFGActive is TRUE\n"); 742 return TRUE; 743 } 744 745 /* 746 Can the system force foreground from one or more conditions. 747 */ 748 BOOL FASTCALL 749 CanForceFG(PPROCESSINFO ppi) 750 { 751 if (!ptiLastInput || 752 ptiLastInput->ppi == ppi || 753 !gptiForeground || 754 gptiForeground->ppi == ppi || 755 ppi->W32PF_flags & (W32PF_ALLOWFOREGROUNDACTIVATE | W32PF_SETFOREGROUNDALLOWED) || 756 gppiInputProvider == ppi || 757 !gpqForeground 758 ) return TRUE; 759 //ERR("CanForceFG is FALSE\n"); 760 return FALSE; 761 } 762 763 // 764 // Switching out foreground message queues. 765 // 766 BOOL FASTCALL 767 co_IntSetForegroundMessageQueue( 768 _In_opt_ PWND Wnd, 769 _In_ PTHREADINFO pti, 770 _In_ BOOL MouseActivate, 771 _In_ DWORD Type ) 772 { 773 PTHREADINFO ptiChg, ptiPrev; 774 PUSER_MESSAGE_QUEUE pumq, pumqChg, pumqPrev; 775 BOOL Removed, Ret = TRUE; 776 777 if (Wnd && !VerifyWnd(Wnd)) 778 { 779 return FALSE; 780 } 781 782 if ( !gptiForeground || gptiForeground->TIF_flags & TIF_INCLEANUP ) 783 ptiPrev = NULL; 784 else 785 ptiPrev = gptiForeground; 786 787 if (Wnd) 788 { 789 ptiChg = Wnd->head.pti; 790 IntSetFocusMessageQueue(Wnd->head.pti->MessageQueue); 791 gptiForeground = Wnd->head.pti; 792 //ERR("Set Foreground pti 0x%p Q 0x%p hWnd 0x%p\n",Wnd->head.pti, Wnd->head.pti->MessageQueue,Wnd->head.h); 793 } 794 else 795 { 796 ptiChg = NULL; 797 IntSetFocusMessageQueue(NULL); 798 gptiForeground = NULL; 799 //ERR("Set Foreground pti 0x0 Q 0x0 hWnd 0x0\n"); 800 } 801 802 // 803 // Process the changing out of the message queues. 804 // 805 if (gpqForegroundPrev != gpqForeground) 806 { 807 pumqPrev = NULL; 808 if ( ptiPrev && !(ptiPrev->TIF_flags & TIF_INCLEANUP) ) 809 { 810 pumqPrev = ptiPrev->MessageQueue; 811 } 812 813 pumq = pti ? pti->MessageQueue : NULL; 814 815 // Deactivate the previous message queue. 816 if (pumqPrev) 817 { 818 if ( pumq != pumqPrev ) 819 { 820 MSG Msg; 821 HWND hWndPrev = pumqPrev->spwndActive ? UserHMGetHandle(pumqPrev->spwndActive) : NULL; 822 HANDLE tid = gptiForeground ? PsGetThreadId(gptiForeground->pEThread) : NULL; // TID from changing Window PTI. 823 824 Msg.message = WM_ASYNC_SETACTIVEWINDOW; 825 Msg.hwnd = hWndPrev; 826 Msg.wParam = (WPARAM)pumqPrev->spwndActive; 827 Msg.lParam = 0; 828 Msg.time = 0; 829 //ERR("SFWAMQ : DAW P pti 0x%p tid 0x%p hWndPrev 0x%p\n",ptiPrev,tid,hWndPrev); 830 MsqPostMessage(ptiPrev, &Msg, FALSE, QS_EVENT, POSTEVENT_DAW, (LONG_PTR)tid); 831 } 832 } 833 834 pumqChg = NULL; 835 if ( ptiChg && !(ptiChg->TIF_flags & TIF_INCLEANUP) ) 836 { 837 pumqChg = ptiChg->MessageQueue; 838 } 839 840 pumq = pti ? pti->MessageQueue : NULL; 841 842 // Activate changing message queue. 843 if (pumqChg) 844 { 845 /* 846 Henri Verbeet, 847 What happens is that we get the WM_WINE_SETACTIVEWINDOW message sent by the 848 other thread after we already changed the foreground window back to our own 849 window. 850 */ 851 //ERR("SFWAMQ : 1\n"); 852 Removed = FindRemoveEventMsg(ptiChg, POSTEVENT_DAW, POSTEVENT_NONE); 853 854 if (pumqChg != pumq) 855 { 856 MSG Msg; 857 HWND hWnd = Wnd ? UserHMGetHandle(Wnd) : NULL; 858 HANDLE tid = ptiPrev ? PsGetThreadId(ptiPrev->pEThread) : NULL; 859 860 if (Removed) pumqChg->QF_flags |= QF_EVENTDEACTIVATEREMOVED; 861 862 Msg.message = WM_ASYNC_SETACTIVEWINDOW; 863 Msg.hwnd = hWnd; 864 Msg.wParam = (WPARAM)Wnd; 865 Msg.lParam = (LPARAM)tid; //// Fixme! Type flags? 866 Msg.time = 0; 867 //ERR("SFWAMQ : SAW P pti 0x%p tid 0x%p hWnd 0x%p\n",ptiChg,tid,hWnd); 868 MsqPostMessage(ptiChg, &Msg, FALSE, QS_EVENT, POSTEVENT_SAW, (LONG_PTR)Type|MouseActivate); 869 } 870 else // Current message queue same as changed message queue. 871 { 872 if (pumq->spwndActive == Wnd) 873 { 874 co_IntSendMessage( UserHMGetHandle(Wnd), WM_NCACTIVATE, TRUE, (LPARAM)UserHMGetHandle(Wnd)); 875 876 UpdateShellHook(Wnd); 877 878 co_WinPosSetWindowPos(Wnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); 879 } 880 else 881 { 882 //ERR("SFWAMQ : SAW I pti 0x%p hWnd 0x%p\n",ptiChg,Wnd->head.h); 883 Ret = co_IntSetActiveWindow(Wnd, MouseActivate, TRUE/*Type*/, FALSE); 884 //if (!Ret) ERR("SFWAMQ : ISAW : return error\n"); 885 return Ret; 886 } 887 } 888 } 889 890 // Handle same message queue after switch out. 891 pumqPrev = NULL; 892 if ( ptiPrev && !(ptiPrev->TIF_flags & TIF_INCLEANUP) ) 893 { 894 pumqPrev = ptiPrev->MessageQueue; 895 } 896 pumq = pti ? pti->MessageQueue : NULL; 897 898 if ( pumqPrev && pumq == pumqPrev ) 899 { 900 HANDLE tid = Wnd ? PsGetThreadId(Wnd->head.pti->pEThread) : NULL; 901 //ERR("SFWAMQ : DAW I pti 0x%p tid 0x%p hWnd 0x%p\n",ptiPrev,tid,Wnd ? Wnd->head.h : 0); 902 IntDeactivateWindow(pti, tid); 903 } 904 } 905 return Ret; 906 } 907 908 /* 909 MSDN: 910 The system restricts which processes can set the foreground window. A process 911 can set the foreground window only if one of the following conditions is true: 912 913 * The process is the foreground process. 914 * The process was started by the foreground process. 915 * The process received the last input event. 916 * There is no foreground process. 917 * The foreground process is being debugged. 918 * The foreground is not locked (see LockSetForegroundWindow). 919 * The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo). 920 * No menus are active. 921 */ 922 static 923 BOOL FASTCALL 924 co_IntSetForegroundAndFocusWindow( 925 _In_opt_ PWND Wnd, 926 _In_ BOOL MouseActivate, 927 _In_ BOOL bFlash ) 928 { 929 HWND hWnd = Wnd ? UserHMGetHandle(Wnd) : NULL; 930 PUSER_MESSAGE_QUEUE PrevForegroundQueue; 931 PTHREADINFO pti; 932 BOOL Ret = FALSE; 933 934 if (Wnd) ASSERT_REFS_CO(Wnd); 935 936 TRACE("SetForegroundAndFocusWindow(%x, %s)\n", hWnd, (MouseActivate ? "TRUE" : "FALSE")); 937 938 PrevForegroundQueue = IntGetFocusMessageQueue(); // Use this active desktop. 939 pti = PsGetCurrentThreadWin32Thread(); 940 941 if (Wnd && PrevForegroundQueue) 942 { // Same Window Q as foreground just do active. 943 if (Wnd->head.pti->MessageQueue == PrevForegroundQueue) 944 { 945 //ERR("Same Window Q as foreground just do active.\n"); 946 if (pti->MessageQueue == PrevForegroundQueue) 947 { // Same WQ and TQ go active. 948 //ERR("Same WQ and TQ go active.\n"); 949 Ret = IntUserSetActiveWindow(Wnd, MouseActivate, TRUE, FALSE); 950 } 951 else if (Wnd->head.pti->MessageQueue->spwndActive == Wnd) 952 { // Same WQ and it is active. 953 //ERR("Same WQ and it is active.\n"); 954 Ret = TRUE; 955 } 956 else 957 { // Same WQ as FG but not the same TQ send active. 958 //ERR("Same WQ as FG but not the same TQ send active.\n"); 959 MSG Msg; 960 PTHREADINFO ptiNew = Wnd->head.pti; 961 962 Msg.message = WM_ASYNC_SETACTIVEWINDOW; 963 Msg.hwnd = hWnd; 964 Msg.wParam = (WPARAM)Wnd; 965 Msg.lParam = 0; 966 Msg.time = 0; 967 //ERR("SFAFW 1 : SAW P pti 0x%p hWnd 0x%p\n",ptiNew,hWnd); 968 MsqPostMessage(ptiNew, &Msg, FALSE, QS_EVENT, POSTEVENT_SAW, (LONG_PTR)MouseActivate); 969 970 Ret = TRUE; 971 } 972 return Ret; 973 } 974 } 975 976 if ( (( !IsFGLocked() || pti->ppi == gppiInputProvider ) && 977 ( CanForceFG(pti->ppi) || pti->TIF_flags & (TIF_SYSTEMTHREAD|TIF_CSRSSTHREAD|TIF_ALLOWFOREGROUNDACTIVATE) )) || 978 pti->ppi == ppiScrnSaver 979 ) 980 { 981 982 ToggleFGActivate(pti); 983 984 return co_IntSetForegroundMessageQueue( Wnd, pti, MouseActivate, 0 ); 985 } 986 987 if (!Wnd) return FALSE; // No window, always return FALSE. 988 989 //// if (bFlash) FIXME : handle flash!!! 990 991 if (pti->MessageQueue == Wnd->head.pti->MessageQueue) 992 { 993 //ERR("Same PQ and WQ go active.\n"); 994 Ret = IntUserSetActiveWindow(Wnd, MouseActivate, TRUE, FALSE); 995 //if (!Ret) ERR("ISFAFW : IUSAW : return error\n"); 996 } 997 else if (Wnd->head.pti->MessageQueue->spwndActive == Wnd) 998 { 999 TRACE("Same Active and Wnd.\n"); // Leave this for now. 1000 } 1001 else 1002 { 1003 //ERR("Activate Not same PQ and WQ and Wnd.\n"); 1004 //// CORE-10785 fix hang, ROSTESTS-208 allows test to run. 1005 MSG Msg; 1006 PTHREADINFO ptiNew = Wnd->head.pti; 1007 1008 Msg.message = WM_ASYNC_SETACTIVEWINDOW; 1009 Msg.hwnd = hWnd; 1010 Msg.wParam = (WPARAM)Wnd; 1011 Msg.lParam = 0; 1012 Msg.time = 0; 1013 //ERR("SFAFW 2 : SAW P pti 0x%p hWnd 0x%p\n",ptiNew,hWnd); 1014 MsqPostMessage(ptiNew, &Msg, FALSE, QS_EVENT, POSTEVENT_SAW, (LONG_PTR)MouseActivate); 1015 } 1016 // Always return FALSE. 1017 return FALSE; 1018 } 1019 1020 // 1021 // Set the Active Window. 1022 // 1023 BOOL FASTCALL 1024 co_IntSetActiveWindow( 1025 _In_ PWND Wnd, 1026 _In_ BOOL bMouse, 1027 _In_ BOOL bFocus, 1028 _In_ BOOL Async ) 1029 { 1030 PTHREADINFO pti; 1031 PUSER_MESSAGE_QUEUE ThreadQueue; 1032 PWND pWndChg, WndPrev; // State changes. 1033 HWND hWndPrev; 1034 HWND hWnd = 0; 1035 BOOL InAAPM; 1036 CBTACTIVATESTRUCT cbt; 1037 1038 //ERR("co_IntSetActiveWindow 1\n"); 1039 1040 pti = PsGetCurrentThreadWin32Thread(); 1041 ThreadQueue = pti->MessageQueue; 1042 ASSERT(ThreadQueue != 0); 1043 1044 hWndPrev = ThreadQueue->spwndActive ? UserHMGetHandle(ThreadQueue->spwndActive) : NULL; 1045 1046 pWndChg = ThreadQueue->spwndActive; // Keep to notify of a preemptive switch. 1047 1048 if ( !Wnd || Wnd == UserGetDesktopWindow() ) 1049 { 1050 //ERR("ISAW : NULL %p\n",Wnd); 1051 return FALSE; 1052 } 1053 1054 ASSERT_REFS_CO(Wnd); 1055 hWnd = UserHMGetHandle(Wnd); 1056 //ERR("co_IntSetActiveWindow 2 hWnd 0x%p\n",hWnd); 1057 1058 /* check if the specified window can be set in the input data of a given queue */ 1059 if ( ThreadQueue != Wnd->head.pti->MessageQueue ) 1060 { 1061 //ERR("ISAW : Must have the same Message Queue\n"); 1062 return FALSE; 1063 } 1064 1065 if (!VerifyWnd(Wnd)) 1066 { 1067 //ERR("ISAW : Window is in Destroy!\n"); 1068 return FALSE; 1069 } 1070 1071 if ( Wnd == pWndChg ) 1072 { 1073 //ERR("ISAW : Nothing to do\n"); 1074 return TRUE; // Fix CORE-8780 and CORE-11979. See CORE-11324 for breakage. 1075 } 1076 1077 if ( Wnd->state & WNDS_BEINGACTIVATED ) return TRUE; 1078 1079 /* Call CBT hook chain */ 1080 cbt.fMouse = bMouse; 1081 cbt.hWndActive = hWndPrev; 1082 if (co_HOOK_CallHooks( WH_CBT, HCBT_ACTIVATE, (WPARAM)hWnd, (LPARAM)&cbt)) 1083 { 1084 ERR("SetActiveWindow: WH_CBT Call Hook return!\n"); 1085 return FALSE; 1086 } 1087 1088 ThreadQueue->QF_flags &= ~QF_EVENTDEACTIVATEREMOVED; 1089 1090 if ( ThreadQueue->spwndActive && ThreadQueue->spwndActive->state & WNDS_DESTROYED ) 1091 ThreadQueue->spwndActive = NULL; 1092 else 1093 ThreadQueue->spwndActivePrev = ThreadQueue->spwndActive; 1094 1095 WndPrev = ThreadQueue->spwndActive; // Keep to save changing active. 1096 1097 if (WndPrev) 1098 { 1099 if (ThreadQueue == gpqForeground) gpqForegroundPrev = ThreadQueue; 1100 if (!co_IntSendDeactivateMessages(UserHMGetHandle(WndPrev), hWnd, TRUE)) return FALSE; 1101 } 1102 1103 WndPrev = ThreadQueue->spwndActive; // Again keep to save changing active. 1104 1105 // While in calling message proc or hook: 1106 // Fail if a preemptive switch was made, current active not made previous, 1107 // focus window is dead or no longer the same thread queue. 1108 if ( ThreadQueue->spwndActivePrev != ThreadQueue->spwndActive || 1109 pWndChg != WndPrev || 1110 (Wnd && !VerifyWnd(Wnd)) || 1111 ThreadQueue != pti->MessageQueue ) 1112 { 1113 ERR("SetActiveWindow: Summary ERROR, active state changed!\n"); 1114 return FALSE; 1115 } 1116 1117 if (!WndPrev) ThreadQueue->QF_flags &= ~QF_FOCUSNULLSINCEACTIVE; 1118 1119 /* set the current thread active window */ 1120 ThreadQueue->spwndActive = Wnd; 1121 1122 // Set state flag to prevent recursions. 1123 Wnd->state |= WNDS_BEINGACTIVATED; 1124 1125 IntNotifyWinEvent(EVENT_SYSTEM_FOREGROUND, Wnd, OBJID_WINDOW, CHILDID_SELF, WEF_SETBYWNDPTI); 1126 1127 // Clear out activate EVENT messages. 1128 FindRemoveEventMsg(pti, POSTEVENT_SAW, POSTEVENT_DAW); 1129 1130 WndPrev = VerifyWnd(ThreadQueue->spwndActivePrev); // Now should be set but verify it again. 1131 1132 InAAPM = co_IntSendActivateMessages(WndPrev, Wnd, bMouse, Async); 1133 1134 /* now change focus if necessary */ 1135 //// Fixes CORE-6452 allows setting focus on window. 1136 if (bFocus && !(ThreadQueue->QF_flags & QF_FOCUSNULLSINCEACTIVE)) 1137 { 1138 /* Do not change focus if the window is no longer active */ 1139 if (pti->MessageQueue->spwndActive != IntGetNonChildAncestor(pti->MessageQueue->spwndFocus)) 1140 { 1141 PWND pWndSend = pti->MessageQueue->spwndActive; 1142 // Clear focus if the active window is minimized. 1143 if (pWndSend && pti->MessageQueue->spwndActive->style & WS_MINIMIZE) pWndSend = NULL; 1144 // Send focus messages and if so, set the focus. 1145 IntSendFocusMessages( pti, pWndSend); 1146 } 1147 } 1148 //// 1149 if (InAAPM) 1150 { 1151 pti->TIF_flags &= ~TIF_INACTIVATEAPPMSG; 1152 } 1153 1154 // Checked in MENU_TrackMenu 1155 ThreadQueue->QF_flags |= QF_ACTIVATIONCHANGE; 1156 1157 //ERR("co_IntSetActiveWindow Exit\n"); 1158 Wnd->state &= ~WNDS_BEINGACTIVATED; 1159 return (ThreadQueue->spwndActive == Wnd); 1160 } 1161 1162 // 1163 // Set the Active Window. 1164 // 1165 // Window is not optional! 1166 // 1167 BOOL FASTCALL 1168 IntUserSetActiveWindow( 1169 _In_ PWND Wnd, 1170 _In_ BOOL bMouse, 1171 _In_ BOOL bFocus, 1172 _In_ BOOL Async) 1173 { 1174 PTHREADINFO pti; 1175 PUSER_MESSAGE_QUEUE ThreadQueue; 1176 1177 //ERR("IntUserSetActiveWindow 1\n"); 1178 ASSERT_REFS_CO(Wnd); 1179 if ((Wnd->style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE; 1180 //ERR("IntUserSetActiveWindow 1a hWnd 0x%p\n",UserHMGetHandle(Wnd)); 1181 1182 //ERR("IntUserSetActiveWindow 2\n"); 1183 pti = PsGetCurrentThreadWin32Thread(); 1184 ThreadQueue = pti->MessageQueue; 1185 ASSERT(ThreadQueue != 0); 1186 1187 while (Wnd) 1188 { 1189 BOOL Ret, DoFG, AllowFG; 1190 1191 if (ThreadQueue == Wnd->head.pti->MessageQueue) 1192 { 1193 if (IsAllowedFGActive(pti, Wnd)) 1194 { 1195 DoFG = TRUE; 1196 } 1197 else 1198 { 1199 //ERR("IntUserSetActiveWindow 3 Go Out!\n"); 1200 break; 1201 } 1202 AllowFG = !pti->cVisWindows; // Nothing is visable. 1203 //ERR("IntUserSetActiveWindow 3a DoFG = %d AllowFG = %d\n",DoFG,AllowFG); 1204 } 1205 else //if (ThreadQueue != Wnd->head.pti->MessageQueue) 1206 { 1207 //PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue(); 1208 // Rule 1 & 4, We are foreground so set this FG window or NULL foreground.... 1209 if (!gpqForeground || gpqForeground == ThreadQueue) 1210 { 1211 DoFG = TRUE; 1212 } 1213 else 1214 DoFG = FALSE; 1215 if (DoFG) 1216 { 1217 if (pti->TIF_flags & TIF_ALLOWFOREGROUNDACTIVATE || pti->cVisWindows) 1218 AllowFG = TRUE; 1219 else 1220 AllowFG = FALSE; 1221 } 1222 else 1223 AllowFG = FALSE; 1224 //ERR("IntUserSetActiveWindow 3b DoFG = %d AllowFG = %d\n",DoFG,AllowFG); 1225 } 1226 Ret = FALSE; 1227 if (DoFG) 1228 { 1229 pti->TIF_flags |= TIF_ALLOWFOREGROUNDACTIVATE; 1230 //ERR("IntUserSetActiveWindow 3c FG set\n"); 1231 Ret = co_IntSetForegroundAndFocusWindow(Wnd, bMouse, TRUE); 1232 if (AllowFG) 1233 { 1234 pti->TIF_flags |= TIF_ALLOWFOREGROUNDACTIVATE; 1235 } 1236 else 1237 { 1238 pti->TIF_flags &= ~TIF_ALLOWFOREGROUNDACTIVATE; 1239 } 1240 } 1241 return Ret; 1242 } 1243 1244 return co_IntSetActiveWindow(Wnd, bMouse, bFocus, Async); 1245 } 1246 1247 BOOL FASTCALL 1248 co_IntMouseActivateWindow(PWND Wnd) 1249 { 1250 TRACE("Mouse Active\n"); 1251 return co_IntSetForegroundAndFocusWindow(Wnd, TRUE, TRUE); 1252 } 1253 1254 BOOL FASTCALL 1255 UserSetActiveWindow( _In_opt_ PWND Wnd ) 1256 { 1257 PTHREADINFO pti = PsGetCurrentThreadWin32Thread(); 1258 1259 if (Wnd) 1260 { 1261 if ((Wnd->style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE; 1262 1263 return IntUserSetActiveWindow(Wnd, FALSE, TRUE, FALSE); 1264 } 1265 /* 1266 Yes your eye are not deceiving you~! 1267 1268 First part of wines Win.c test_SetActiveWindow: 1269 1270 flush_events( TRUE ); 1271 ShowWindow(hwnd, SW_HIDE); 1272 SetFocus(0); 1273 SetActiveWindow(0); 1274 check_wnd_state(0, 0, 0, 0); <-- This should pass if ShowWindow does it's job!!! As of 10/28/2012 it does! 1275 1276 Now Handle wines Msg.c test_SetActiveWindow( 0 )... 1277 */ 1278 TRACE("USAW: Previous active window\n"); 1279 if ( gpqForegroundPrev && 1280 gpqForegroundPrev->spwndActivePrev && 1281 (gpqForegroundPrev->spwndActivePrev->style & (WS_VISIBLE|WS_DISABLED)) == WS_VISIBLE && 1282 !(gpqForegroundPrev->spwndActivePrev->state2 & WNDS2_BOTTOMMOST) && 1283 (Wnd = VerifyWnd(gpqForegroundPrev->spwndActivePrev)) != NULL ) 1284 { 1285 TRACE("USAW:PAW hwnd %p\n",Wnd?Wnd->head.h:NULL); 1286 return IntUserSetActiveWindow(Wnd, FALSE, TRUE, FALSE); 1287 } 1288 1289 // Activate anyone but the active window. 1290 if ( pti->MessageQueue->spwndActive && 1291 (Wnd = VerifyWnd(pti->MessageQueue->spwndActive)) != NULL ) 1292 { 1293 //ERR("USAW:AOWM hwnd %p\n",Wnd?Wnd->head.h:NULL); 1294 if (!ActivateOtherWindowMin(Wnd)) 1295 { 1296 // Okay, now go find someone else to play with! 1297 //ERR("USAW: Going to WPAOW\n"); 1298 co_WinPosActivateOtherWindow(Wnd); 1299 } 1300 return TRUE; 1301 } 1302 1303 TRACE("USAW: Nothing\n"); 1304 return FALSE; 1305 } 1306 1307 // Win: PWND xxxSetFocus(Window) 1308 HWND FASTCALL 1309 co_UserSetFocus(PWND Window) 1310 { 1311 HWND hWndPrev = 0; 1312 PWND pwndTop; 1313 PTHREADINFO pti; 1314 PUSER_MESSAGE_QUEUE ThreadQueue; 1315 1316 if (Window) 1317 ASSERT_REFS_CO(Window); 1318 1319 pti = PsGetCurrentThreadWin32Thread(); 1320 ThreadQueue = pti->MessageQueue; 1321 ASSERT(ThreadQueue != 0); 1322 1323 TRACE("Enter SetFocus hWnd 0x%p pti 0x%p\n",Window ? UserHMGetHandle(Window) : 0, pti ); 1324 1325 hWndPrev = ThreadQueue->spwndFocus ? UserHMGetHandle(ThreadQueue->spwndFocus) : 0; 1326 1327 if (Window != 0) 1328 { 1329 if (hWndPrev == UserHMGetHandle(Window)) 1330 { 1331 return hWndPrev ? (IntIsWindow(hWndPrev) ? hWndPrev : 0) : 0; /* Nothing to do */ 1332 } 1333 1334 if (Window->head.pti->MessageQueue != ThreadQueue) 1335 { 1336 ERR("SetFocus Must have the same Q!\n"); 1337 return 0; 1338 } 1339 1340 /* Check if we can set the focus to this window */ 1341 //// Fixes wine win test_SetParent both "todo" line 3710 and 3720... 1342 for (pwndTop = Window; pwndTop; pwndTop = pwndTop->spwndParent) 1343 { 1344 if (pwndTop->style & (WS_MINIMIZED|WS_DISABLED)) return 0; 1345 if ((pwndTop->style & (WS_POPUP|WS_CHILD)) != WS_CHILD) break; 1346 if (pwndTop->spwndParent == NULL) break; 1347 } 1348 //// 1349 if (co_HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, (WPARAM)Window->head.h, (LPARAM)hWndPrev)) 1350 { 1351 ERR("SetFocus 1 WH_CBT Call Hook return!\n"); 1352 return 0; 1353 } 1354 1355 /* Activate pwndTop if needed. */ 1356 if (pwndTop != ThreadQueue->spwndActive) 1357 { 1358 PUSER_MESSAGE_QUEUE ForegroundQueue = IntGetFocusMessageQueue(); // Keep it based on desktop. 1359 if (ThreadQueue != ForegroundQueue && IsAllowedFGActive(pti, pwndTop)) // Rule 2 & 3. 1360 { 1361 //ERR("SetFocus: Set Foreground!\n"); 1362 if (!(pwndTop->style & WS_VISIBLE)) 1363 { 1364 pti->ppi->W32PF_flags |= W32PF_ALLOWFOREGROUNDACTIVATE; 1365 } 1366 if (!co_IntSetForegroundAndFocusWindow(pwndTop, FALSE, TRUE)) 1367 { 1368 ERR("SetFocus: Set Foreground and Focus Failed!\n"); 1369 return 0; 1370 } 1371 } 1372 1373 /* Set Active when it is needed. */ 1374 if (pwndTop != ThreadQueue->spwndActive) 1375 { 1376 //ERR("SetFocus: Set Active! %p\n",pwndTop?UserHMGetHandle(pwndTop):0); 1377 if (!co_IntSetActiveWindow(pwndTop, FALSE, FALSE, FALSE)) 1378 { 1379 ERR("SetFocus: Set Active Failed!\n"); 1380 return 0; 1381 } 1382 } 1383 1384 /* Abort if window destroyed */ 1385 if (Window->state2 & WNDS2_INDESTROY) return 0; 1386 /* Do not change focus if the window is no longer active */ 1387 if (pwndTop != ThreadQueue->spwndActive) 1388 { 1389 ERR("SetFocus: Top window did not go active!\n"); 1390 return 0; 1391 } 1392 } 1393 1394 // Check again! SetActiveWindow could have set the focus via WM_ACTIVATE. 1395 hWndPrev = ThreadQueue->spwndFocus ? UserHMGetHandle(ThreadQueue->spwndFocus) : 0; 1396 1397 IntSendFocusMessages( pti, Window); 1398 1399 TRACE("Focus: %p -> %p\n", hWndPrev, Window->head.h); 1400 } 1401 else /* NULL hwnd passed in */ 1402 { 1403 if (co_HOOK_CallHooks( WH_CBT, HCBT_SETFOCUS, (WPARAM)0, (LPARAM)hWndPrev)) 1404 { 1405 ERR("SetFocus: 2 WH_CBT Call Hook return!\n"); 1406 return 0; 1407 } 1408 //ERR("SetFocus: Set Focus NULL\n"); 1409 /* set the current thread focus window null */ 1410 IntSendFocusMessages( pti, NULL); 1411 } 1412 return hWndPrev ? (IntIsWindow(hWndPrev) ? hWndPrev : 0) : 0; 1413 } 1414 1415 HWND FASTCALL 1416 UserGetForegroundWindow(VOID) 1417 { 1418 PUSER_MESSAGE_QUEUE ForegroundQueue; 1419 1420 ForegroundQueue = IntGetFocusMessageQueue(); 1421 return( ForegroundQueue ? (ForegroundQueue->spwndActive ? UserHMGetHandle(ForegroundQueue->spwndActive) : 0) : 0); 1422 } 1423 1424 HWND FASTCALL UserGetActiveWindow(VOID) 1425 { 1426 PTHREADINFO pti; 1427 PUSER_MESSAGE_QUEUE ThreadQueue; 1428 1429 pti = PsGetCurrentThreadWin32Thread(); 1430 ThreadQueue = pti->MessageQueue; 1431 return( ThreadQueue ? (ThreadQueue->spwndActive ? UserHMGetHandle(ThreadQueue->spwndActive) : 0) : 0); 1432 } 1433 1434 HWND APIENTRY 1435 IntGetCapture(VOID) 1436 { 1437 PTHREADINFO pti; 1438 PUSER_MESSAGE_QUEUE ThreadQueue; 1439 DECLARE_RETURN(HWND); 1440 1441 TRACE("Enter IntGetCapture\n"); 1442 1443 pti = PsGetCurrentThreadWin32Thread(); 1444 ThreadQueue = pti->MessageQueue; 1445 RETURN( ThreadQueue ? (ThreadQueue->spwndCapture ? UserHMGetHandle(ThreadQueue->spwndCapture) : 0) : 0); 1446 1447 CLEANUP: 1448 TRACE("Leave IntGetCapture, ret=%p\n", _ret_); 1449 END_CLEANUP; 1450 } 1451 1452 HWND FASTCALL 1453 co_UserSetCapture(HWND hWnd) 1454 { 1455 PTHREADINFO pti; 1456 PUSER_MESSAGE_QUEUE ThreadQueue; 1457 PWND pWnd, Window = NULL; 1458 HWND hWndPrev; 1459 1460 pti = PsGetCurrentThreadWin32Thread(); 1461 ThreadQueue = pti->MessageQueue; 1462 1463 if (ThreadQueue->QF_flags & QF_CAPTURELOCKED) 1464 return NULL; 1465 1466 if (hWnd && (Window = UserGetWindowObject(hWnd))) 1467 { 1468 if (Window->head.pti->MessageQueue != ThreadQueue) 1469 { 1470 ERR("Window Thread does not match Current!\n"); 1471 return NULL; 1472 } 1473 } 1474 1475 hWndPrev = MsqSetStateWindow(pti, MSQ_STATE_CAPTURE, hWnd); 1476 1477 if (hWndPrev) 1478 { 1479 pWnd = UserGetWindowObject(hWndPrev); 1480 if (pWnd) 1481 IntNotifyWinEvent(EVENT_SYSTEM_CAPTUREEND, pWnd, OBJID_WINDOW, CHILDID_SELF, WEF_SETBYWNDPTI); 1482 } 1483 1484 if (Window) 1485 IntNotifyWinEvent(EVENT_SYSTEM_CAPTURESTART, Window, OBJID_WINDOW, CHILDID_SELF, WEF_SETBYWNDPTI); 1486 1487 // 1488 // Only send the message if we have a previous Window! 1489 // Fix msg_menu tracking popup menu and win test_capture_4!!!! 1490 // 1491 if (hWndPrev) 1492 { 1493 if (ThreadQueue->MenuOwner && Window) ThreadQueue->QF_flags |= QF_CAPTURELOCKED; 1494 1495 co_IntSendMessage(hWndPrev, WM_CAPTURECHANGED, 0, (LPARAM)hWnd); 1496 1497 ThreadQueue->QF_flags &= ~QF_CAPTURELOCKED; 1498 } 1499 1500 if (hWnd == NULL) // Release mode. 1501 { 1502 MOUSEINPUT mi; 1503 /// These are HACKS! 1504 /* Also remove other windows if not capturing anymore */ 1505 MsqSetStateWindow(pti, MSQ_STATE_MENUOWNER, NULL); 1506 MsqSetStateWindow(pti, MSQ_STATE_MOVESIZE, NULL); 1507 /// 1508 /* Somebody may have missed some mouse movements */ 1509 mi.dx = 0; 1510 mi.dy = 0; 1511 mi.mouseData = 0; 1512 mi.dwFlags = MOUSEEVENTF_MOVE; 1513 mi.time = 0; 1514 mi.dwExtraInfo = 0; 1515 UserSendMouseInput(&mi, FALSE); 1516 } 1517 return hWndPrev; 1518 } 1519 1520 /* 1521 API Call 1522 */ 1523 BOOL 1524 FASTCALL 1525 IntReleaseCapture(VOID) 1526 { 1527 PTHREADINFO pti; 1528 PUSER_MESSAGE_QUEUE ThreadQueue; 1529 1530 pti = PsGetCurrentThreadWin32Thread(); 1531 ThreadQueue = pti->MessageQueue; 1532 1533 // Can not release inside WM_CAPTURECHANGED!! 1534 if (ThreadQueue->QF_flags & QF_CAPTURELOCKED) return FALSE; 1535 1536 co_UserSetCapture(NULL); 1537 1538 return TRUE; 1539 } 1540 1541 /* 1542 API Call 1543 */ 1544 BOOL FASTCALL 1545 co_IntSetForegroundWindow(PWND Window) 1546 { 1547 if (Window) ASSERT_REFS_CO(Window); 1548 1549 return co_IntSetForegroundAndFocusWindow(Window, FALSE, TRUE); 1550 } 1551 1552 /* 1553 API Call 1554 */ 1555 BOOL FASTCALL 1556 co_IntSetForegroundWindowMouse(PWND Window) 1557 { 1558 if (Window) ASSERT_REFS_CO(Window); 1559 1560 return co_IntSetForegroundAndFocusWindow(Window, TRUE, FALSE); 1561 } 1562 1563 /* 1564 API Call 1565 */ 1566 BOOL FASTCALL 1567 IntLockSetForegroundWindow(UINT uLockCode) 1568 { 1569 ULONG Err = ERROR_ACCESS_DENIED; 1570 PPROCESSINFO ppi = PsGetCurrentProcessWin32Process(); 1571 switch (uLockCode) 1572 { 1573 case LSFW_LOCK: 1574 if ( CanForceFG(ppi) && !gppiLockSFW ) 1575 { 1576 gppiLockSFW = ppi; 1577 return TRUE; 1578 } 1579 break; 1580 case LSFW_UNLOCK: 1581 if ( gppiLockSFW == ppi) 1582 { 1583 gppiLockSFW = NULL; 1584 return TRUE; 1585 } 1586 break; 1587 default: 1588 Err = ERROR_INVALID_PARAMETER; 1589 } 1590 EngSetLastError(Err); 1591 return FALSE; 1592 } 1593 1594 /* 1595 API Call 1596 */ 1597 BOOL FASTCALL 1598 IntAllowSetForegroundWindow(DWORD dwProcessId) 1599 { 1600 PPROCESSINFO ppi, ppiCur; 1601 PEPROCESS Process = NULL; 1602 1603 ppi = NULL; 1604 if (dwProcessId != ASFW_ANY) 1605 { 1606 if (!NT_SUCCESS(PsLookupProcessByProcessId(UlongToHandle(dwProcessId), &Process))) 1607 { 1608 EngSetLastError(ERROR_INVALID_PARAMETER); 1609 return FALSE; 1610 } 1611 ppi = PsGetProcessWin32Process(Process); 1612 if (!ppi) 1613 { 1614 ObDereferenceObject(Process); 1615 return FALSE; 1616 } 1617 } 1618 ppiCur = PsGetCurrentProcessWin32Process(); 1619 if (!CanForceFG(ppiCur)) 1620 { 1621 if (Process) ObDereferenceObject(Process); 1622 EngSetLastError(ERROR_ACCESS_DENIED); 1623 return FALSE; 1624 } 1625 if (dwProcessId == ASFW_ANY) 1626 { // All processes will be enabled to set the foreground window. 1627 //ERR("ptiLastInput is CLEARED!!\n"); 1628 ptiLastInput = NULL; 1629 } 1630 else 1631 { // Rule #3, last input event in force. 1632 ERR("ptiLastInput is SET!!\n"); 1633 //ptiLastInput = ppi->ptiList; // See CORE-6384 & CORE-7030. 1634 ObDereferenceObject(Process); 1635 } 1636 return TRUE; 1637 } 1638 1639 /* 1640 * @implemented 1641 */ 1642 HWND APIENTRY 1643 NtUserGetForegroundWindow(VOID) 1644 { 1645 DECLARE_RETURN(HWND); 1646 1647 TRACE("Enter NtUserGetForegroundWindow\n"); 1648 UserEnterExclusive(); 1649 1650 RETURN( UserGetForegroundWindow()); 1651 1652 CLEANUP: 1653 TRACE("Leave NtUserGetForegroundWindow, ret=%p\n",_ret_); 1654 UserLeave(); 1655 END_CLEANUP; 1656 } 1657 1658 HWND APIENTRY 1659 NtUserSetActiveWindow(HWND hWnd) 1660 { 1661 USER_REFERENCE_ENTRY Ref; 1662 HWND hWndPrev; 1663 PWND Window; 1664 DECLARE_RETURN(HWND); 1665 1666 TRACE("Enter NtUserSetActiveWindow(%p)\n", hWnd); 1667 UserEnterExclusive(); 1668 1669 Window = NULL; 1670 if (hWnd) 1671 { 1672 if (!(Window = UserGetWindowObject(hWnd))) 1673 { 1674 ERR("NtUserSetActiveWindow: Invalid handle 0x%p!\n",hWnd); 1675 RETURN( NULL); 1676 } 1677 } 1678 1679 if (!Window || 1680 Window->head.pti->MessageQueue == gptiCurrent->MessageQueue) 1681 { 1682 hWndPrev = gptiCurrent->MessageQueue->spwndActive ? UserHMGetHandle(gptiCurrent->MessageQueue->spwndActive) : NULL; 1683 if (Window) UserRefObjectCo(Window, &Ref); 1684 UserSetActiveWindow(Window); 1685 if (Window) UserDerefObjectCo(Window); 1686 RETURN( hWndPrev ? (IntIsWindow(hWndPrev) ? hWndPrev : 0) : 0 ); 1687 } 1688 RETURN( NULL); 1689 1690 CLEANUP: 1691 TRACE("Leave NtUserSetActiveWindow, ret=%p\n",_ret_); 1692 UserLeave(); 1693 END_CLEANUP; 1694 } 1695 1696 /* 1697 * @implemented 1698 */ 1699 HWND APIENTRY 1700 NtUserSetCapture(HWND hWnd) 1701 { 1702 DECLARE_RETURN(HWND); 1703 1704 TRACE("Enter NtUserSetCapture(%p)\n", hWnd); 1705 UserEnterExclusive(); 1706 1707 RETURN( co_UserSetCapture(hWnd)); 1708 1709 CLEANUP: 1710 TRACE("Leave NtUserSetCapture, ret=%p\n", _ret_); 1711 UserLeave(); 1712 END_CLEANUP; 1713 } 1714 1715 /* 1716 * @implemented 1717 */ 1718 HWND APIENTRY 1719 NtUserSetFocus(HWND hWnd) 1720 { 1721 PWND Window; 1722 USER_REFERENCE_ENTRY Ref; 1723 DECLARE_RETURN(HWND); 1724 HWND ret; 1725 1726 TRACE("Enter NtUserSetFocus(%p)\n", hWnd); 1727 UserEnterExclusive(); 1728 1729 if (hWnd) 1730 { 1731 if (!(Window = UserGetWindowObject(hWnd))) 1732 { 1733 ERR("NtUserSetFocus: Invalid handle 0x%p!\n",hWnd); 1734 RETURN(NULL); 1735 } 1736 1737 UserRefObjectCo(Window, &Ref); 1738 ret = co_UserSetFocus(Window); 1739 UserDerefObjectCo(Window); 1740 1741 RETURN(ret); 1742 } 1743 else 1744 { 1745 RETURN( co_UserSetFocus(0)); 1746 } 1747 1748 CLEANUP: 1749 TRACE("Leave NtUserSetFocus, ret=%p\n",_ret_); 1750 UserLeave(); 1751 END_CLEANUP; 1752 } 1753 1754 /* EOF */ 1755