Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions 3rdparty/wlroots/xwayland/xwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,48 @@ static bool validate_focus_serial(uint16_t last_focus_seq, uint16_t event_seq) {
return true;
}

// Returns true if `child` is located inside `parent`'s subtree (not `parent`
// itself), walking at most `max_depth` levels up from `child`. Used to tell
// apart in-application focus moves into XEmbed plug windows (which a real X11
// window manager never interferes with) from true cross-application focus
// stealing. Walking up from `child` needs at most `max_depth` synchronous X
// queries, while a top-down search has to visit every window of `parent`'s
// subtree (unbounded in width) before it can answer "no" -- which is the
// common case for cross-application focus changes.
static bool window_is_descendant(struct wlr_xwm *xwm, xcb_window_t parent,
xcb_window_t child, unsigned int max_depth) {
xcb_window_t window = child;
unsigned int queries = 0;
bool found = false;

while (window != parent && queries < max_depth) {
xcb_query_tree_cookie_t cookie =
xcb_query_tree(xwm->xcb_conn, window);
xcb_query_tree_reply_t *reply =
xcb_query_tree_reply(xwm->xcb_conn, cookie, NULL);
if (reply == NULL) {
break;
}
xcb_window_t ancestor = reply->parent;
free(reply);
queries++;

if (ancestor == parent) {
found = true;
break;
}
if (ancestor == XCB_NONE || ancestor == xwm->screen->root) {
break;
}
window = ancestor;
}

wlr_log(WLR_DEBUG, "focus descendant check: child=0x%" PRIx32
" parent=0x%" PRIx32 " queries=%u result=%d",
child, parent, queries, found);
return found;
}

static void xwm_handle_focus_in(struct wlr_xwm *xwm,
xcb_focus_in_event_t *ev) {
// Ignore pointer focus change events
Expand Down Expand Up @@ -1951,6 +1993,16 @@ static void xwm_handle_focus_in(struct wlr_xwm *xwm,
(xwm->offered_focus && xsurface->pid == xwm->offered_focus->pid))) {
xwm_set_focused_window(xwm, xsurface);
wl_signal_emit_mutable(&xsurface->events.focus_in, NULL);
} else if (xwm->focus_surface && ev->event != XCB_NONE &&
window_is_descendant(xwm, xwm->focus_surface->window_id,
ev->event, 8)) {
// The newly focused window is a subwindow of the currently focused
// surface's hierarchy: an in-client focus move (e.g. XEmbed plug
// windows used by WPS Office and other embedded-toolkit apps).
// A real X11 window manager leaves these alone; refocusing here
// would strip input focus from the embedder's plug and break
// anything inside that window gated on real X focus (notably
// input-method activation via XIM).
} else {
// Try to prevent clients from changing focus between
// applications, by refocusing the previous surface.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ ExecCondition=/bin/sh -c 'test "$XDG_SESSION_DESKTOP" = "Treeland" || exit 2'
Type=notify
Sockets=treeland-xwayland.socket
UnsetEnvironment=DISPLAY
UnsetEnvironment=XAUTHORITY
ExecStart=@CMAKE_INSTALL_FULL_LIBEXECDIR@/treeland-sd --type xwayland
ExecStop=-/usr/bin/systemctl --user unset-environment DISPLAY
ExecStop=-/usr/bin/systemctl --user unset-environment DISPLAY XAUTHORITY
Restart=on-failure
RestartSec=3s
Slice=session.slice
36 changes: 34 additions & 2 deletions src/core/shellhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,10 @@ void ShellHandler::init(WServer *server, WSeat *seat)
&WInputMethodHelper::inputPopupSurfaceV2Removed,
this,
&ShellHandler::onInputPopupSurfaceV2Removed);
connect(m_inputMethodHelper,
&WInputMethodHelper::textInputFocusSurfaceChanged,
this,
&ShellHandler::onTextInputFocusSurfaceChanged);

auto *overlay = QQuickOverlay::overlay(m_rootSurfaceContainer->window());
overlay->setZ(RootSurfaceContainer::GlobalOverlayZOrder);
Expand Down Expand Up @@ -1236,29 +1240,57 @@ void ShellHandler::updateLayerSurfaceContainer(SurfaceWrapper *surface)

void ShellHandler::onInputPopupSurfaceV2Added(WInputPopupSurface *surface)
{
auto parent = surface->parentSurface();
auto parentWrapper = m_rootSurfaceContainer->getSurface(parent);
if (!parentWrapper) {
// The anchoring text input surface is gone (client teardown): there is
// nothing to attach the candidate window to.
qCWarning(lcTlShell) << "[popup] skip input popup without a parent wrapper"
<< "surface=" << surface
<< "parent=" << parent;
return;
}

auto wrapper = new SurfaceWrapper(Helper::instance()->qmlEngine(),
surface,
SurfaceWrapper::Type::InputPopup);
auto parent = surface->parentSurface();
auto parentWrapper = m_rootSurfaceContainer->getSurface(parent);
parentWrapper->addSubSurface(wrapper);
m_popupContainer->addSurface(wrapper);
// m_popupContainer is a simple SurfaceContainer, so input popups need the
// same explicit initialization marker as xdg popups for their first layout.
wrapper->setHasInitializeContainer(true);
wrapper->setOwnsOutput(parentWrapper->ownsOutput());
Q_ASSERT(wrapper->parentItem());
m_inputPopupWrappers.append(wrapper);
Q_EMIT surfaceWrapperAdded(wrapper);
}

void ShellHandler::onInputPopupSurfaceV2Removed(WInputPopupSurface *surface)
{
auto wrapper = m_rootSurfaceContainer->getSurface(surface->surface());
if (!wrapper) {
// onInputPopupSurfaceV2Added() skipped this popup (no parent wrapper
// left), so there is no wrapper to detach here either.
return;
}
m_inputPopupWrappers.removeAll(wrapper);
Q_EMIT surfaceWrapperAboutToRemove(wrapper);
wrapper->setHasInitializeContainer(false);
m_rootSurfaceContainer->destroyForSurface(wrapper);
}

void ShellHandler::onTextInputFocusSurfaceChanged()
{
m_inputPopupWrappers.removeIf([](const QPointer<SurfaceWrapper> &popup) {
return popup.isNull();
});

for (const auto &popup : std::as_const(m_inputPopupWrappers)) {
if (auto *output = popup->ownsOutput())
output->retargetInputPopupSurface(popup);
}
}

void ShellHandler::setupSurfaceWindowMenu(SurfaceWrapper *wrapper)
{
Q_ASSERT(m_windowMenu);
Expand Down
4 changes: 4 additions & 0 deletions src/core/shellhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ private Q_SLOTS:

void setupDockPreview();
void onInputPopupSurfaceV2Removed(WAYLIB_SERVER_NAMESPACE::WInputPopupSurface *surface);
void onTextInputFocusSurfaceChanged();
void onWindowMenuClosed();

private:
Expand Down Expand Up @@ -192,6 +193,9 @@ private Q_SLOTS:
SurfaceContainer *m_popupContainer = nullptr;
SurfaceContainer *m_privilegedOverlayContainer = nullptr;
IMCandidatePanelManager *m_imCandidatePanelManager = nullptr;
// Input method popup (candidate window) wrappers, re-arranged whenever the
// text input that owns the input method focus moves.
QList<QPointer<SurfaceWrapper>> m_inputPopupWrappers;
QObject *m_windowMenu = nullptr;
// Prelaunch wrappers created before binding to a real shell surface
QList<SurfaceWrapper *> m_prelaunchWrappers;
Expand Down
107 changes: 81 additions & 26 deletions src/output/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
#include <wscopedvalue.h>
#include "output.h"

#include "cmdline.h"

Check warning on line 7 in src/output/output.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "cmdline.h" not found.
#include "common/treelandlogging.h"

Check warning on line 8 in src/output/output.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "common/treelandlogging.h" not found.
#include "core/rootsurfacecontainer.h"

Check warning on line 9 in src/output/output.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "core/rootsurfacecontainer.h" not found.
#include "core/shellhandler.h"

Check warning on line 10 in src/output/output.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "core/shellhandler.h" not found.
#include "core/dconfigmanager.h"

Check warning on line 11 in src/output/output.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "core/dconfigmanager.h" not found.
#include "outputconfig.hpp"

Check warning on line 12 in src/output/output.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "outputconfig.hpp" not found.
#include "seat/helper.h"

Check warning on line 13 in src/output/output.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "seat/helper.h" not found.
#include "surface/surfacewrapper.h"
#include "treelandconfig.hpp"
#include "treelanduserconfig.hpp"
#include "workspace/workspace.h"

Check warning on line 17 in src/output/output.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "workspace/workspace.h" not found.
#include "wallpapermanager.h"

Check warning on line 18 in src/output/output.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "wallpapermanager.h" not found.

#include <WInputMethodHelper>

Check warning on line 20 in src/output/output.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <WInputMethodHelper> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <wcursor.h>
#include <winputpopupsurface.h>
#include <winputpopupsurfaceitem.h>
Expand Down Expand Up @@ -848,7 +850,7 @@
// - Input popups: cursorRect is in raw surface coordinates; adjust by
// content geometry offset (e.g., CSD title bar) to match the rendering
// position of WSurfaceItem's contentContainer.
std::optional<QPointF> popupDPos(SurfaceWrapper *surface)
std::optional<QPointF> popupDPos(SurfaceWrapper *surface, SurfaceWrapper *placementParent)
{
using Type = SurfaceWrapper::Type;
if (surface->type() == Type::XdgPopup) {
Expand All @@ -861,9 +863,8 @@
auto *shell = qobject_cast<WInputPopupSurface *>(surface->shellSurface());
if (shell) {
QPointF dPos = shell->cursorRect().bottomLeft();
auto parent = surface->parentSurface();
if (parent && parent->shellSurface()) {
const QPoint offset = parent->shellSurface()->getContentGeometry().topLeft();
if (placementParent && placementParent->shellSurface()) {
const QPoint offset = placementParent->shellSurface()->getContentGeometry().topLeft();
dPos -= QPointF(offset.x(), offset.y());
}
return dPos;
Expand All @@ -873,22 +874,45 @@
qCWarning(lcTlOutput) << " Invalid popup surface type:" << surface->type();
return std::nullopt;
}

// The input method's panel (candidate window) belongs to the text input that
// currently owns the input method focus. The popup wrapper's own parentSurface()
// only records the surface that happened to be focused when the input method
// created its panel surface, so it goes stale as soon as the same application
// moves the text input focus, e.g. to another of its windows.
SurfaceWrapper *inputPopupPlacementParent(SurfaceWrapper *popup)
{
auto *helper = Helper::instance();
if (!helper)
return popup->parentSurface();

auto *shellHandler = helper->shellHandler();
auto *inputMethodHelper = shellHandler ? shellHandler->inputMethodHelper() : nullptr;
auto *rootContainer = helper->rootSurfaceContainer();
if (inputMethodHelper && rootContainer) {
if (auto *focusSurface = inputMethodHelper->textInputFocusSurface()) {
if (auto *focusWrapper = rootContainer->getSurface(focusSurface))
return focusWrapper;
}
}

return popup->parentSurface();
}
} // namespace

QPointF Output::calculateBasePosition(SurfaceWrapper *surface, const QPointF &dPos) const
QPointF Output::calculateBasePosition(SurfaceWrapper *placementParent, const QPointF &dPos) const
{
auto parent = surface->parentSurface();
if (!parent || !parent->surfaceItem()) {
if (!placementParent || !placementParent->surfaceItem()) {
qCWarning(lcTlOutput) << " Invalid parent surface or surface item!";
return QPointF();
}

const qreal titlebarOffset = parent->titlebarGeometry().isNull()
const qreal titlebarOffset = placementParent->titlebarGeometry().isNull()
? 0.0
: parent->titlebarGeometry().height();
: placementParent->titlebarGeometry().height();

return QPointF(parent->x() + parent->surfaceItem()->x() + dPos.x(),
parent->y() + parent->surfaceItem()->y() + dPos.y() + titlebarOffset);
return QPointF(placementParent->x() + placementParent->surfaceItem()->x() + dPos.x(),
placementParent->y() + placementParent->surfaceItem()->y() + dPos.y() + titlebarOffset);
}

void Output::adjustToOutputBounds(QPointF &pos, const QRectF &normalGeo, const QRectF &outputRect) const
Expand All @@ -908,19 +932,19 @@
}
}

void Output::handleLayerShellPopup(SurfaceWrapper *surface, const QRectF &normalGeo)
void Output::handleLayerShellPopup(SurfaceWrapper *surface, SurfaceWrapper *placementParent, const QRectF &normalGeo)
{
if (!surface->parentSurface() || !surface->parentSurface()->ownsOutput()) {
if (!placementParent || !placementParent->ownsOutput()) {
qCWarning(lcTlOutput) << " Invalid LayerShell parent surface!";
return;
}

auto parentOutput = surface->parentSurface()->ownsOutput()->outputItem();
auto dPos = popupDPos(surface);
auto parentOutput = placementParent->ownsOutput()->outputItem();
auto dPos = popupDPos(surface, placementParent);
if (!dPos.has_value())
return;

QPointF pos = calculateBasePosition(surface, dPos.value());
QPointF pos = calculateBasePosition(placementParent, dPos.value());
if (pos.isNull()) {
return;
}
Expand All @@ -930,17 +954,17 @@
surface->moveNormalGeometryInOutput(pos);
}

void Output::handleRegularPopup(SurfaceWrapper *surface, const QRectF &normalGeo, WOutputItem *targetOutput)
void Output::handleRegularPopup(SurfaceWrapper *surface, SurfaceWrapper *placementParent, const QRectF &normalGeo, WOutputItem *targetOutput)
{
if (normalGeo.isEmpty()) {
return;
}

auto dPos = popupDPos(surface);
auto dPos = popupDPos(surface, placementParent);
if (!dPos.has_value())
return;

QPointF pos = calculateBasePosition(surface, dPos.value());
QPointF pos = calculateBasePosition(placementParent, dPos.value());
if (pos.isNull()) {
return;
}
Expand Down Expand Up @@ -976,8 +1000,15 @@

void Output::arrangePopupSurface(SurfaceWrapper *surface)
{
SurfaceWrapper *parentSurfaceWrapper = surface->parentSurface();
if (!parentSurfaceWrapper) {
SurfaceWrapper *placementParent = surface->parentSurface();
if (surface->type() == SurfaceWrapper::Type::InputPopup)
placementParent = inputPopupPlacementParent(surface);
arrangePopupSurfaceWith(surface, placementParent);
}

void Output::arrangePopupSurfaceWith(SurfaceWrapper *surface, SurfaceWrapper *placementParent)
{
if (!placementParent) {
// When an input popup is still alive while its parent text-input client is being torn down,
// arrangePopupSurface() can run in a transient state where parentSurface is temporarily unavailable.
qCWarning(lcTlSurface) << "[popup] skip arrangePopupSurface: missing parent surface"
Expand All @@ -996,24 +1027,48 @@
auto *outputAtCursor = Helper::instance()->getOutputAtCursor();
targetOutput = outputAtCursor ? outputAtCursor->outputItem() : nullptr;
} else if (surface->isInputPopupLike()) {
auto *parentOutput = parentSurfaceWrapper->ownsOutput();
auto *parentOutput = placementParent->ownsOutput();
targetOutput = parentOutput ? parentOutput->outputItem() : nullptr;
}

if (!targetOutput) {
qCInfo(lcTlSurface) << "[popup] skip arrangePopupSurface: missing target output"
<< "surface=" << surface
<< "parentSurface=" << parentSurfaceWrapper;
<< "parentSurface=" << placementParent;
return;
}

if (parentSurfaceWrapper->type() == SurfaceWrapper::Type::Layer) {
handleLayerShellPopup(surface, normalGeo);
if (placementParent->type() == SurfaceWrapper::Type::Layer) {
handleLayerShellPopup(surface, placementParent, normalGeo);
} else {
handleRegularPopup(surface, normalGeo, targetOutput);
handleRegularPopup(surface, placementParent, normalGeo, targetOutput);
}
}

void Output::retargetInputPopupSurface(SurfaceWrapper *popup)
{
if (!popup || popup->type() != SurfaceWrapper::Type::InputPopup)
return;

auto *placementParent = inputPopupPlacementParent(popup);
if (placementParent) {
if (auto *placementOutput = placementParent->ownsOutput();
placementOutput && placementOutput != popup->ownsOutput()) {
// Detaches the popup from this output (dropping its cached position)
// and registers it with the new one.
popup->setOwnsOutput(placementOutput);
}
}

qCDebug(lcTlSurface) << "[popup] retarget input popup"
<< "popup=" << popup
<< "placementParent=" << placementParent
<< "ownsOutput=" << popup->ownsOutput();
// Reuse the parent resolved above: it walks the root surface container,
// which arrangePopupSurface() would otherwise repeat for input popups.
arrangePopupSurfaceWith(popup, placementParent);
}

void Output::arrangeNonLayerSurfaces(ArrangeReason reason)
{
for (SurfaceWrapper *surface : std::as_const(surfaces())) {
Expand Down
Loading
Loading