546{
547 if (!_enabled)
548 return true;
549
550 std::unique_lock lock(_windowsLock);
551
552
553 if (_sentWorkArea.w == 0)
554 {
555 SDL_Rect usable{};
556 if (SDL_GetDisplayUsableBounds(SDL_GetPrimaryDisplay(), &usable))
557 sendWorkArea(usable);
558 }
559
560
561
562
563
564
565
566
567
568
569
570 std::vector<std::pair<size_t, SdlRailWindow*>> release;
571 for (auto& [wid, win] : _windows)
572 {
573 if (!win.window())
574 continue;
575 size_t depth = 0;
576 bool orphan = false;
577 bool ownerDeleted = false;
578 for (SDL_Window* p = SDL_GetWindowParent(win.window()); p; p = SDL_GetWindowParent(p))
579 {
580 depth++;
581 auto* owner = getWindowBySdlId(SDL_GetWindowID(p));
582 if (!owner)
583 orphan = true;
584 else if (owner->isDeleted())
585 {
586 orphan = true;
587 if (owner->id() == win.owner())
588 ownerDeleted = true;
589 }
590 }
591 if (orphan && !win.isDeleted() && (ownerDeleted || !win.isPopup()))
592 win.markDeleted();
593 if (orphan || win.isDeleted())
594 release.emplace_back(depth, &win);
595 }
596 for (auto& [wid, win] : _deadWindows)
597 {
598 if (!win.window())
599 continue;
600 size_t depth = 0;
601 for (SDL_Window* p = SDL_GetWindowParent(win.window()); p; p = SDL_GetWindowParent(p))
602 depth++;
603 release.emplace_back(depth, &win);
604 }
605 std::sort(release.begin(), release.end(),
606 [](const auto& a, const auto& b) { return a.first > b.first; });
607 for (auto& [depth, win] : release)
608 win->releaseWindow();
609
610
611 for (auto it = _windows.begin(); it != _windows.end();)
612 it = it->second.isDeleted() ? _windows.erase(it) : std::next(it);
613
614 _deadWindows.clear();
615
616
617 for (auto& it : _windows)
618 {
619 auto& win = it.second;
620 if (win.isPopup())
621 continue;
622
623 SDL_Window* parent = nullptr;
624 SDL_Rect parentRect{};
625 auto* owner = resolveParent(win.owner());
626 if (owner && (owner != &win))
627 {
628 parent = owner->window();
629 parentRect = owner->outerRect();
630 }
631 const bool realized = win.window() != nullptr;
632 win.paint(primary, fallbackFormat, damage, parent, parentRect);
633 if (!realized && win.window())
634 _context->railWindowInfoChanged(win.id());
635
636 SDL_Rect refusedOuter{};
637 if (win.takeWmOverride(refusedOuter))
638 {
639 const SDL_Rect rect = win.serverRect(refusedOuter);
640 const SDL_Rect cur = win.windowRect();
642 "wm-refused adopt id=0x%08" PRIx32
643 " outer=%dx%d -> srv=%d,%d %dx%d (was %dx%d)",
644 static_cast<uint32_t>(win.id()), refusedOuter.w, refusedOuter.h, rect.x,
645 rect.y, rect.w, rect.h, cur.w, cur.h);
646 if (!SDL_RectsEqual(&rect, &cur))
647 sendClientWindowMove(&win, rect);
648 win.adoptLocalGeometry(rect);
649 _context->railWindowInfoChanged(win.id());
650 }
651
652 SDL_Rect unconfirmed{};
653 if (win.takeUnconfirmedReport(unconfirmed) && !win.railMinimized() &&
654 !win.effectivelyMaximized())
655 {
656 WLog_DBG(
TAG,
"size resent id=0x%08" PRIx32
" %dx%d (not confirmed)",
657 static_cast<uint32_t>(win.id()), unconfirmed.w, unconfirmed.h);
658 sendClientWindowMove(&win, unconfirmed);
659 }
660 if (!win.effectivelyMaximized())
661 followDisplay(&win, false);
662 if (win.takeMaximizeAfterEcho() && win.effectivelyMaximized())
663 sendSystemCommand(&win, SC_MAXIMIZE);
664 if (win.takeRestoreRetry())
665 {
666 WLog_DBG(
TAG,
"restore resent id=0x%08" PRIx32,
static_cast<uint32_t
>(win.id()));
667 sendSystemCommand(&win, SC_RESTORE);
668 }
669 }
670
671
672 if (!_refreshSent)
673 {
674 for (auto& it : _windows)
675 {
676 if (it.second.isPopup() || !it.second.window())
677 continue;
678 auto* ctx = &_context->common()->context;
679 const auto dw = static_cast<UINT16>(
681 const auto dh = static_cast<UINT16>(
683 const RECTANGLE_16 all = { 0, 0, dw, dh };
684 if (ctx->update && ctx->update->RefreshRect)
685 (void)ctx->update->RefreshRect(ctx, 1, &all);
686 _refreshSent = true;
687 WLog_DBG(
TAG,
"refresh-rect sent (first app window realized)");
688 break;
689 }
690 }
691
692 for (auto& it : _windows)
693 {
694 auto& popup = it.second;
695 if (!popup.isPopup())
696 continue;
697
698
699 if (!popup.isFrame() && isShadowFrame(popup))
700 popup.setFrame(true);
701
702
703 if (popup.isLayered())
704 {
705 constexpr int reach = 48;
706 const SDL_Rect sr = popup.windowRect();
707 bool anchored = false;
708 for (auto& other : _windows)
709 {
710 auto& cand = other.second;
711 if ((&cand == &popup) || !cand.isPopup() || cand.isLayered() || !cand.window() ||
712 ((SDL_GetWindowFlags(cand.window()) & SDL_WINDOW_HIDDEN) != 0))
713 continue;
714 SDL_Rect zone = cand.windowRect();
715 zone.x -= reach;
716 zone.y -= reach;
717 zone.w += 2 * reach;
718 zone.h += 2 * reach;
719
720
721 const bool inside = (sr.x >= zone.x) && (sr.y >= zone.y) &&
722 (sr.x + sr.w <= zone.x + zone.w) &&
723 (sr.y + sr.h <= zone.y + zone.h);
724 if (inside)
725 {
726 anchored = true;
727 break;
728 }
729 }
730 popup.setShadowAnchored(anchored);
731 }
732
733 SdlRailWindow* chosen = nullptr;
734
735 if (auto* pw = popup.window())
736 {
737 SDL_Window* p = SDL_GetWindowParent(pw);
738 chosen = p ? getWindowBySdlId(SDL_GetWindowID(p)) : nullptr;
739 }
740 else
741 {
742 chosen = resolvePopupParent(popup);
743 if (!chosen)
744 WLog_DBG(
TAG,
"popup id=0x%08" PRIx32
" has no parent app window",
745 static_cast<UINT32>(popup.id()));
746 }
747
748 SDL_Window* parent = nullptr;
749 SDL_Rect parentRect{};
750 if (chosen)
751 {
752 parent = chosen->window();
753 parentRect = chosen->outerRect();
754 }
755 const bool realized = popup.window() != nullptr;
756 popup.paint(primary, fallbackFormat, damage, parent, parentRect);
757 if (!realized && popup.window())
758 _context->railWindowInfoChanged(popup.id());
759 }
760
761
762 applyZOrder();
763 return true;
764}