582{
583 if (!_enabled)
584 return true;
585
586 std::unique_lock lock(_windowsLock);
587
588
589 if (_sentWorkArea.w == 0)
590 {
591 SDL_Rect usable{};
592 if (SDL_GetDisplayUsableBounds(SDL_GetPrimaryDisplay(), &usable))
593 sendWorkArea(usable);
594 }
595
596
597
598
599
600
601
602
603
604
605
606 std::vector<std::pair<size_t, SdlRailWindow*>> release;
607 for (auto& [wid, win] : _windows)
608 {
609 if (!win.window())
610 continue;
611 size_t depth = 0;
612 bool orphan = false;
613 bool ownerDeleted = false;
614 for (SDL_Window* p = SDL_GetWindowParent(win.window()); p; p = SDL_GetWindowParent(p))
615 {
616 depth++;
617 auto* owner = getWindowBySdlId(SDL_GetWindowID(p));
618 if (!owner)
619 orphan = true;
620 else if (owner->isDeleted())
621 {
622 orphan = true;
623 if (owner->id() == win.owner())
624 ownerDeleted = true;
625 }
626 }
627 if (orphan && !win.isDeleted() && (ownerDeleted || !win.isPopup()))
628 win.markDeleted();
629 if (orphan || win.isDeleted())
630 release.emplace_back(depth, &win);
631 }
632 for (auto& [wid, win] : _deadWindows)
633 {
634 if (!win.window())
635 continue;
636 size_t depth = 0;
637 for (SDL_Window* p = SDL_GetWindowParent(win.window()); p; p = SDL_GetWindowParent(p))
638 depth++;
639 release.emplace_back(depth, &win);
640 }
641 std::sort(release.begin(), release.end(),
642 [](const auto& a, const auto& b) { return a.first > b.first; });
643 for (auto& [depth, win] : release)
644 win->releaseWindow();
645
646
647 for (auto it = _windows.begin(); it != _windows.end();)
648 it = it->second.isDeleted() ? _windows.erase(it) : std::next(it);
649
650 _deadWindows.clear();
651
652
653 for (auto& it : _windows)
654 {
655 auto& win = it.second;
656 if (win.isPopup())
657 continue;
658
659 SDL_Window* parent = nullptr;
660 SDL_Rect parentRect{};
661
662
663
664 auto* owner = resolveParent(win.owner());
665 if (owner && !descendsFrom(*owner, win))
666 {
667 parent = owner->window();
668 parentRect = owner->outerRect();
669 }
670 const bool realized = win.window() != nullptr;
671 win.paint(primary, fallbackFormat, damage, parent, parentRect);
672 if (!realized && win.window())
673 _context->railWindowInfoChanged(win.id());
674
675 SDL_Rect refusedOuter{};
676 if (win.takeWmOverride(refusedOuter))
677 {
678 const SDL_Rect rect = win.serverRect(refusedOuter);
679 const SDL_Rect cur = win.windowRect();
681 "wm-refused adopt id=0x%08" PRIx32
682 " outer=%dx%d -> srv=%d,%d %dx%d (was %dx%d)",
683 static_cast<uint32_t>(win.id()), refusedOuter.w, refusedOuter.h, rect.x,
684 rect.y, rect.w, rect.h, cur.w, cur.h);
685 if (!SDL_RectsEqual(&rect, &cur))
686 sendClientWindowMove(&win, rect);
687 win.adoptLocalGeometry(rect);
688 _context->railWindowInfoChanged(win.id());
689 }
690
691 SDL_Rect unconfirmed{};
692 if (win.takeUnconfirmedReport(unconfirmed) && !win.railMinimized() &&
693 !win.effectivelyMaximized())
694 {
695 WLog_DBG(
TAG,
"size resent id=0x%08" PRIx32
" %dx%d (not confirmed)",
696 static_cast<uint32_t>(win.id()), unconfirmed.w, unconfirmed.h);
697 sendClientWindowMove(&win, unconfirmed);
698 }
699 if (!win.effectivelyMaximized())
700 followDisplay(&win, false);
701 if (win.takeMaximizeAfterEcho() && win.effectivelyMaximized())
702 sendSystemCommand(&win, SC_MAXIMIZE);
703 if (win.takeRestoreRetry())
704 {
705 WLog_DBG(
TAG,
"restore resent id=0x%08" PRIx32,
static_cast<uint32_t
>(win.id()));
706 sendSystemCommand(&win, SC_RESTORE);
707 }
708 }
709
710
711 if (!_refreshSent)
712 {
713 for (auto& it : _windows)
714 {
715 if (it.second.isPopup() || !it.second.window())
716 continue;
717 auto* ctx = &_context->common()->context;
718
719 if (freerdp_timer_add(ctx, 1, refreshRectTimer, nullptr, true) == 0)
720 WLog_WARN(
TAG,
"could not queue the refresh-rect");
721 _refreshSent = true;
722 WLog_DBG(
TAG,
"refresh-rect queued (first app window realized)");
723 break;
724 }
725 }
726
727 for (auto& it : _windows)
728 {
729 auto& popup = it.second;
730 if (!popup.isPopup())
731 continue;
732
733
734 if (!popup.isFrame() && isShadowFrame(popup))
735 popup.setFrame(true);
736
737
738 if (popup.isLayered())
739 {
740 constexpr int reach = 48;
741 const SDL_Rect sr = popup.windowRect();
742 bool anchored = false;
743 for (auto& other : _windows)
744 {
745 auto& cand = other.second;
746 if ((&cand == &popup) || !cand.isPopup() || cand.isLayered() || !cand.window() ||
747 ((SDL_GetWindowFlags(cand.window()) & SDL_WINDOW_HIDDEN) != 0))
748 continue;
749 SDL_Rect zone = cand.windowRect();
750 zone.x -= reach;
751 zone.y -= reach;
752 zone.w += 2 * reach;
753 zone.h += 2 * reach;
754
755
756 const bool inside = (sr.x >= zone.x) && (sr.y >= zone.y) &&
757 (sr.x + sr.w <= zone.x + zone.w) &&
758 (sr.y + sr.h <= zone.y + zone.h);
759 if (inside)
760 {
761 anchored = true;
762 break;
763 }
764 }
765 popup.setShadowAnchored(anchored);
766 }
767
768 SdlRailWindow* chosen = nullptr;
769
770 if (auto* pw = popup.window())
771 {
772 SDL_Window* p = SDL_GetWindowParent(pw);
773 chosen = p ? getWindowBySdlId(SDL_GetWindowID(p)) : nullptr;
774 }
775 else
776 {
777 chosen = resolvePopupParent(popup);
778 if (!chosen)
779 WLog_DBG(
TAG,
"popup id=0x%08" PRIx32
" has no parent app window",
780 static_cast<UINT32>(popup.id()));
781 }
782
783 SDL_Window* parent = nullptr;
784 SDL_Rect parentRect{};
785 if (chosen)
786 {
787 parent = chosen->window();
788 parentRect = chosen->outerRect();
789 }
790 const bool realized = popup.window() != nullptr;
791 popup.paint(primary, fallbackFormat, damage, parent, parentRect);
792 if (!realized && popup.window())
793 _context->railWindowInfoChanged(popup.id());
794 }
795
796
797 applyZOrder();
798 return true;
799}