Skip to content

xwayland: support HiDPI scale #6446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion src/protocols/PointerConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "../managers/input/InputManager.hpp"
#include "../render/Renderer.hpp"
#include "../helpers/Monitor.hpp"
#include "../xwayland/XWayland.hpp"

CPointerConstraint::CPointerConstraint(SP<CZwpLockedPointerV1> resource_, SP<CWLSurfaceResource> surf, wl_resource* region_, zwpPointerConstraintsV1Lifetime lifetime_) :
m_resourceLocked(resource_), m_locked(true), m_lifetime(lifetime_) {
Expand Down Expand Up @@ -37,7 +38,7 @@ CPointerConstraint::CPointerConstraint(SP<CZwpLockedPointerV1> resource_, SP<CWL
const auto PWINDOW = m_hlSurface->getWindow();
if (PWINDOW) {
const auto ISXWL = PWINDOW->m_isX11;
scale = ISXWL && *PXWLFORCESCALEZERO ? PWINDOW->m_X11SurfaceScaledBy : 1.f;
scale = ISXWL ? (*PXWLFORCESCALEZERO ? PWINDOW->m_X11SurfaceScaledBy : g_pXWayland->m_wm->getScale()) : 1.f;
}

m_positionHint = {wl_fixed_to_double(x) / scale, wl_fixed_to_double(y) / scale};
Expand Down
11 changes: 6 additions & 5 deletions src/xwayland/XSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ void CXWaylandSurface::configure(const CBox& box) {
m_geometry = box;

uint32_t mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT | XCB_CONFIG_WINDOW_BORDER_WIDTH;
uint32_t values[] = {box.x, box.y, box.width, box.height, 0};
uint32_t values[] = {g_pXWayland->m_wm->applyScale(box.x), g_pXWayland->m_wm->applyScale(box.y), g_pXWayland->m_wm->applyScale(box.width),
g_pXWayland->m_wm->applyScale(box.height), 0};
xcb_configure_window(g_pXWayland->m_wm->m_connection, m_xID, mask, values);

if (m_geometry.width == box.width && m_geometry.height == box.height) {
Expand All @@ -177,10 +178,10 @@ void CXWaylandSurface::configure(const CBox& box) {
e.response_type = XCB_CONFIGURE_NOTIFY;
e.event = m_xID;
e.window = m_xID;
e.x = box.x;
e.y = box.y;
e.width = box.width;
e.height = box.height;
e.x = g_pXWayland->m_wm->applyScale(box.x);
e.y = g_pXWayland->m_wm->applyScale(box.y);
e.width = g_pXWayland->m_wm->applyScale(box.width);
e.height = g_pXWayland->m_wm->applyScale(box.height);
e.border_width = 0;
e.above_sibling = XCB_NONE;
e.override_redirect = m_overrideRedirect;
Expand Down
53 changes: 45 additions & 8 deletions src/xwayland/XWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ void CXWM::handleCreate(xcb_create_notify_event_t* e) {
if (isWMWindow(e->window))
return;

const auto XSURF = m_surfaces.emplace_back(SP<CXWaylandSurface>(new CXWaylandSurface(e->window, CBox{e->x, e->y, e->width, e->height}, e->override_redirect)));
XSURF->m_self = XSURF;
const auto XSURF = m_surfaces.emplace_back(
SP<CXWaylandSurface>(new CXWaylandSurface(e->window, CBox{applyUnScale(e->x), applyUnScale(e->y), applyUnScale(e->width), applyUnScale(e->height)}, e->override_redirect)));
XSURF->m_self = XSURF;
Debug::log(LOG, "[xwm] New XSurface at {:x} with xid of {}", (uintptr_t)XSURF.get(), e->window);

const auto WINDOW = CWindow::create(XSURF);
Expand Down Expand Up @@ -72,9 +73,9 @@ void CXWM::handleConfigureRequest(xcb_configure_request_event_t* e) {
if (!(MASK & GEOMETRY))
return;

XSURF->m_events.configureRequest.emit(CBox{MASK & XCB_CONFIG_WINDOW_X ? e->x : XSURF->m_geometry.x, MASK & XCB_CONFIG_WINDOW_Y ? e->y : XSURF->m_geometry.y,
MASK & XCB_CONFIG_WINDOW_WIDTH ? e->width : XSURF->m_geometry.width,
MASK & XCB_CONFIG_WINDOW_HEIGHT ? e->height : XSURF->m_geometry.height});
XSURF->m_events.configureRequest.emit(CBox{
MASK & XCB_CONFIG_WINDOW_X ? applyUnScale(e->x) : XSURF->m_geometry.x, MASK & XCB_CONFIG_WINDOW_Y ? applyUnScale(e->y) : XSURF->m_geometry.y,
MASK & XCB_CONFIG_WINDOW_WIDTH ? applyUnScale(e->width) : XSURF->m_geometry.width, MASK & XCB_CONFIG_WINDOW_HEIGHT ? applyUnScale(e->height) : XSURF->m_geometry.height});
}

void CXWM::handleConfigureNotify(xcb_configure_notify_event_t* e) {
Expand All @@ -83,10 +84,11 @@ void CXWM::handleConfigureNotify(xcb_configure_notify_event_t* e) {
if (!XSURF)
return;

if (XSURF->m_geometry == CBox{e->x, e->y, e->width, e->height})
const auto GEOM = CBox{applyUnScale(e->x), applyUnScale(e->y), applyUnScale(e->width), applyUnScale(e->height)};
if (XSURF->m_geometry == GEOM)
return;

XSURF->m_geometry = {e->x, e->y, e->width, e->height};
XSURF->m_geometry = GEOM;
updateOverrideRedirect(XSURF, e->override_redirect);
XSURF->m_events.setGeometry.emit();
}
Expand Down Expand Up @@ -273,6 +275,17 @@ void CXWM::readProp(SP<CXWaylandSurface> XSURF, uint32_t atom, xcb_get_property_
std::memset(XSURF->m_sizeHints.get(), 0, sizeof(xcb_size_hints_t));
xcb_icccm_get_wm_size_hints_from_reply(XSURF->m_sizeHints.get(), reply);

XSURF->m_sizeHints->x = applyUnScale(XSURF->m_sizeHints->x);
XSURF->m_sizeHints->y = applyUnScale(XSURF->m_sizeHints->y);
XSURF->m_sizeHints->width = applyUnScale(XSURF->m_sizeHints->width);
XSURF->m_sizeHints->height = applyUnScale(XSURF->m_sizeHints->height);
XSURF->m_sizeHints->min_width = applyUnScale(XSURF->m_sizeHints->min_width);
XSURF->m_sizeHints->min_height = applyUnScale(XSURF->m_sizeHints->min_height);
XSURF->m_sizeHints->max_width = applyUnScale(XSURF->m_sizeHints->max_width);
XSURF->m_sizeHints->max_height = applyUnScale(XSURF->m_sizeHints->max_height);
XSURF->m_sizeHints->base_width = applyUnScale(XSURF->m_sizeHints->base_width);
XSURF->m_sizeHints->base_height = applyUnScale(XSURF->m_sizeHints->base_height);

const int32_t FLAGS = XSURF->m_sizeHints->flags;
const bool HASMIN = FLAGS & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE;
const bool HASBASE = FLAGS & XCB_ICCCM_SIZE_HINT_BASE_SIZE;
Expand Down Expand Up @@ -328,8 +341,20 @@ void CXWM::readProp(SP<CXWaylandSurface> XSURF, uint32_t atom, xcb_get_property_
void CXWM::handlePropertyNotify(xcb_property_notify_event_t* e) {
const auto XSURF = windowForXID(e->window);

if (!XSURF)
if (!XSURF) {
if (e->atom == HYPRATOMS["_XWAYLAND_GLOBAL_OUTPUT_SCALE"]) {
xcb_get_property_cookie_t cookie = xcb_get_property(m_connection, 0, e->window, e->atom, XCB_ATOM_ANY, 0, 2048);
xcb_get_property_reply_t* reply = xcb_get_property_reply(m_connection, cookie, nullptr);
if (!reply) {
return;
}
if (reply->type == XCB_ATOM_CARDINAL) {
m_scale = *(uint32_t*)xcb_get_property_value(reply);
}
free(reply);
}
return;
}

xcb_get_property_cookie_t cookie = xcb_get_property(m_connection, 0, XSURF->m_xID, e->atom, XCB_ATOM_ANY, 0, 2048);
xcb_get_property_reply_t* reply = xcb_get_property_reply(m_connection, cookie, nullptr);
Expand Down Expand Up @@ -1355,6 +1380,18 @@ SP<IDataOffer> CXWM::createX11DataOffer(SP<CWLSurfaceResource> surf, SP<IDataSou
return offer;
}

double CXWM::getScale() {
return m_scale;
}

double CXWM::applyScale(double val) {
return std::floor(val * m_scale);
}

double CXWM::applyUnScale(double val) {
return std::ceil(val / m_scale);
}

void SXSelection::onSelection() {
const bool isClipboard = this == &g_pXWayland->m_wm->m_clipboard;
const bool isPrimary = this == &g_pXWayland->m_wm->m_primarySelection;
Expand Down
5 changes: 5 additions & 0 deletions src/xwayland/XWM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class CXWM {
int onEvent(int fd, uint32_t mask);
SP<CX11DataDevice> getDataDevice();
SP<IDataOffer> createX11DataOffer(SP<CWLSurfaceResource> surf, SP<IDataSource> source);
double getScale();

private:
void setCursor(unsigned char* pixData, uint32_t stride, const Vector2D& size, const Vector2D& hotspot);
Expand Down Expand Up @@ -173,12 +174,16 @@ class CXWM {

SXSelection* getSelection(xcb_atom_t atom);

double applyScale(double val);
double applyUnScale(double val);

//
CXCBConnection m_connection;
xcb_errors_context_t* m_errors = nullptr;
xcb_screen_t* m_screen = nullptr;

xcb_window_t m_wmWindow;
double m_scale = 1.0;

wl_event_source* m_eventSource = nullptr;

Expand Down
1 change: 1 addition & 0 deletions src/xwayland/XWayland.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,6 @@ inline std::unordered_map<std::string, uint32_t> HYPRATOMS = {
HYPRATOM("DELETE"),
HYPRATOM("TEXT"),
HYPRATOM("INCR"),
HYPRATOM("_XWAYLAND_GLOBAL_OUTPUT_SCALE"),
#endif
};