Hyprland/src/managers/XWaylandManager.cpp

280 lines
12 KiB
C++
Raw Normal View History

2022-03-18 20:03:39 +01:00
#include "XWaylandManager.hpp"
#include "../Compositor.hpp"
#include "../events/Events.hpp"
CHyprXWaylandManager::CHyprXWaylandManager() {
2022-10-27 12:26:35 +02:00
#ifndef NO_XWAYLAND
m_sWLRXWayland = wlr_xwayland_create(g_pCompositor->m_sWLDisplay, g_pCompositor->m_sWLRCompositor, 1);
2022-03-18 20:03:39 +01:00
2022-10-27 12:26:35 +02:00
if (!m_sWLRXWayland) {
Debug::log(ERR, "Couldn't start up the XWaylandManager because wlr_xwayland_create returned a nullptr!");
return;
}
2022-03-18 20:03:39 +01:00
2022-10-27 12:26:35 +02:00
addWLSignal(&m_sWLRXWayland->events.ready, &Events::listen_readyXWayland, m_sWLRXWayland, "XWayland Manager");
addWLSignal(&m_sWLRXWayland->events.new_surface, &Events::listen_surfaceXWayland, m_sWLRXWayland, "XWayland Manager");
2022-03-18 20:03:39 +01:00
2022-10-27 12:26:35 +02:00
setenv("DISPLAY", m_sWLRXWayland->display_name, 1);
2022-03-18 20:03:39 +01:00
2022-10-27 12:26:35 +02:00
Debug::log(LOG, "CHyprXWaylandManager started on display %s", m_sWLRXWayland->display_name);
#else
unsetenv("DISPLAY"); // unset DISPLAY so that X11 apps do not try to start on a different/invalid DISPLAY
2022-10-27 12:26:35 +02:00
#endif
2022-03-18 20:03:39 +01:00
}
CHyprXWaylandManager::~CHyprXWaylandManager() {}
2022-03-18 20:03:39 +01:00
wlr_surface* CHyprXWaylandManager::getWindowSurface(CWindow* pWindow) {
if (pWindow->m_bIsX11)
return pWindow->m_uSurface.xwayland->surface;
return pWindow->m_uSurface.xdg->surface;
}
void CHyprXWaylandManager::activateSurface(wlr_surface* pSurface, bool activate) {
2022-08-31 11:12:46 +02:00
if (!pSurface)
return;
2022-08-21 23:16:54 +02:00
if (wlr_surface_is_xdg_surface(pSurface)) {
const auto PSURF = wlr_xdg_surface_from_wlr_surface(pSurface);
2022-08-31 11:12:46 +02:00
if (PSURF && PSURF->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL) {
2022-08-21 23:16:54 +02:00
wlr_xdg_toplevel_set_activated(PSURF->toplevel, activate);
}
} else if (wlr_surface_is_xwayland_surface(pSurface)) {
2022-03-18 20:03:39 +01:00
wlr_xwayland_surface_activate(wlr_xwayland_surface_from_wlr_surface(pSurface), activate);
2022-07-07 21:47:59 +02:00
if (activate)
2022-12-20 00:43:47 +01:00
wlr_xwayland_surface_restack(wlr_xwayland_surface_from_wlr_surface(pSurface), nullptr, XCB_STACK_MODE_ABOVE);
2022-04-05 18:29:58 +02:00
}
2022-03-18 20:03:39 +01:00
}
2022-04-02 18:57:09 +02:00
void CHyprXWaylandManager::activateWindow(CWindow* pWindow, bool activate) {
2022-04-05 18:29:58 +02:00
if (pWindow->m_bIsX11) {
2022-05-11 15:03:31 +02:00
2022-10-28 20:18:10 +02:00
if (activate) {
wlr_xwayland_surface_set_minimized(pWindow->m_uSurface.xwayland, false);
2022-12-20 00:43:47 +01:00
wlr_xwayland_surface_restack(pWindow->m_uSurface.xwayland, nullptr, XCB_STACK_MODE_ABOVE);
2022-10-28 20:18:10 +02:00
}
2022-04-02 18:57:09 +02:00
wlr_xwayland_surface_activate(pWindow->m_uSurface.xwayland, activate);
} else
2022-04-02 18:57:09 +02:00
wlr_xdg_toplevel_set_activated(pWindow->m_uSurface.xdg->toplevel, activate);
2022-10-25 15:19:24 +02:00
if (activate) {
g_pCompositor->m_pLastFocus = getWindowSurface(pWindow);
2022-10-25 15:19:24 +02:00
g_pCompositor->m_pLastWindow = pWindow;
}
2022-08-31 17:02:44 +02:00
2022-09-10 13:11:02 +02:00
if (!pWindow->m_bPinned)
g_pCompositor->getWorkspaceByID(pWindow->m_iWorkspaceID)->m_pLastFocusedWindow = pWindow;
2022-04-02 18:57:09 +02:00
}
2022-03-18 20:03:39 +01:00
void CHyprXWaylandManager::getGeometryForWindow(CWindow* pWindow, wlr_box* pbox) {
if (pWindow->m_bIsX11) {
const auto SIZEHINTS = pWindow->m_uSurface.xwayland->size_hints;
2022-10-17 15:26:18 +02:00
if (SIZEHINTS && pWindow->m_iX11Type != 2) {
pbox->x = SIZEHINTS->x;
pbox->y = SIZEHINTS->y;
pbox->width = SIZEHINTS->width;
pbox->height = SIZEHINTS->height;
} else {
pbox->x = pWindow->m_uSurface.xwayland->x;
pbox->y = pWindow->m_uSurface.xwayland->y;
pbox->width = pWindow->m_uSurface.xwayland->width;
pbox->height = pWindow->m_uSurface.xwayland->height;
}
2022-03-18 20:03:39 +01:00
} else {
wlr_xdg_surface_get_geometry(pWindow->m_uSurface.xdg, pbox);
}
}
std::string CHyprXWaylandManager::getTitle(CWindow* pWindow) {
2022-03-22 17:41:23 +01:00
try {
if (pWindow->m_bIsX11) {
2022-03-29 15:30:08 +02:00
if (pWindow->m_uSurface.xwayland && pWindow->m_uSurface.xwayland->title) {
2022-03-22 17:41:23 +01:00
return std::string(pWindow->m_uSurface.xwayland->title);
}
2022-03-24 18:22:01 +01:00
} else if (pWindow->m_uSurface.xdg) {
2022-03-29 15:30:08 +02:00
if (pWindow->m_uSurface.xdg->toplevel && pWindow->m_uSurface.xdg->toplevel->title) {
2022-03-24 18:22:01 +01:00
return std::string(pWindow->m_uSurface.xdg->toplevel->title);
}
2022-03-22 17:41:23 +01:00
} else {
return "";
2022-03-22 17:31:19 +01:00
}
} catch (...) { Debug::log(ERR, "Error in getTitle (probably null title)"); }
2022-03-22 17:41:23 +01:00
2022-03-24 18:22:01 +01:00
return "";
}
std::string CHyprXWaylandManager::getAppIDClass(CWindow* pWindow) {
try {
if (pWindow->m_bIsX11) {
if (pWindow->m_uSurface.xwayland) {
2022-06-06 15:03:06 +02:00
if (!pWindow->m_bMappedX11 || !pWindow->m_bIsMapped)
return "unmanaged X11";
2022-03-24 18:22:01 +01:00
return std::string(pWindow->m_uSurface.xwayland->_class);
}
} else if (pWindow->m_uSurface.xdg) {
2022-03-22 17:41:23 +01:00
if (pWindow->m_uSurface.xdg->toplevel) {
2022-03-24 18:22:01 +01:00
return std::string(pWindow->m_uSurface.xdg->toplevel->app_id);
2022-03-22 17:41:23 +01:00
}
} else {
return "";
2022-03-22 17:31:19 +01:00
}
} catch (std::logic_error& e) { Debug::log(ERR, "Error in getAppIDClass: %s", e.what()); }
2022-03-18 20:03:39 +01:00
2022-03-22 17:31:19 +01:00
return "";
2022-03-18 20:03:39 +01:00
}
void CHyprXWaylandManager::sendCloseWindow(CWindow* pWindow) {
if (pWindow->m_bIsX11) {
wlr_xwayland_surface_close(pWindow->m_uSurface.xwayland);
} else {
wlr_xdg_toplevel_send_close(pWindow->m_uSurface.xdg->toplevel);
}
}
void CHyprXWaylandManager::setWindowSize(CWindow* pWindow, const Vector2D& size, bool force) {
if (!force &&
((pWindow->m_vReportedSize == size && pWindow->m_vRealPosition.vec() == pWindow->m_vReportedPosition) || (pWindow->m_vReportedSize == size && !pWindow->m_bIsX11)))
return;
pWindow->m_vReportedPosition = pWindow->m_vRealPosition.vec();
pWindow->m_vReportedSize = size;
if (pWindow->m_bIsX11)
2022-04-23 14:16:02 +02:00
wlr_xwayland_surface_configure(pWindow->m_uSurface.xwayland, pWindow->m_vRealPosition.vec().x, pWindow->m_vRealPosition.vec().y, size.x, size.y);
2022-06-22 15:45:56 +02:00
else
wlr_xdg_toplevel_set_size(pWindow->m_uSurface.xdg->toplevel, size.x, size.y);
2022-03-18 20:03:39 +01:00
}
void CHyprXWaylandManager::setWindowStyleTiled(CWindow* pWindow, uint32_t edgez) {
if (!pWindow->m_bIsX11)
wlr_xdg_toplevel_set_tiled(pWindow->m_uSurface.xdg->toplevel, edgez);
}
2022-03-19 10:53:39 +01:00
wlr_surface* CHyprXWaylandManager::surfaceAt(CWindow* pWindow, const Vector2D& client, Vector2D& surface) {
if (pWindow->m_bIsX11)
return wlr_surface_surface_at(pWindow->m_uSurface.xwayland->surface, client.x, client.y, &surface.x, &surface.y);
2022-03-18 20:03:39 +01:00
2022-03-19 10:53:39 +01:00
return wlr_xdg_surface_surface_at(pWindow->m_uSurface.xdg, client.x, client.y, &surface.x, &surface.y);
2022-03-20 13:37:07 +01:00
}
bool CHyprXWaylandManager::shouldBeFloated(CWindow* pWindow) {
if (pWindow->m_bIsX11) {
for (size_t i = 0; i < pWindow->m_uSurface.xwayland->window_type_len; i++)
if (pWindow->m_uSurface.xwayland->window_type[i] == HYPRATOMS["_NET_WM_WINDOW_TYPE_DIALOG"] ||
pWindow->m_uSurface.xwayland->window_type[i] == HYPRATOMS["_NET_WM_WINDOW_TYPE_SPLASH"] ||
pWindow->m_uSurface.xwayland->window_type[i] == HYPRATOMS["_NET_WM_WINDOW_TYPE_TOOLBAR"] ||
pWindow->m_uSurface.xwayland->window_type[i] == HYPRATOMS["_NET_WM_WINDOW_TYPE_UTILITY"] ||
pWindow->m_uSurface.xwayland->window_type[i] == HYPRATOMS["_NET_WM_WINDOW_TYPE_TOOLTIP"] ||
pWindow->m_uSurface.xwayland->window_type[i] == HYPRATOMS["_NET_WM_WINDOW_TYPE_POPUP_MENU"] ||
pWindow->m_uSurface.xwayland->window_type[i] == HYPRATOMS["_NET_WM_WINDOW_TYPE_DOCK"] ||
pWindow->m_uSurface.xwayland->window_type[i] == HYPRATOMS["_NET_WM_WINDOW_TYPE_DROPDOWN_MENU"] ||
pWindow->m_uSurface.xwayland->window_type[i] == HYPRATOMS["_NET_WM_WINDOW_TYPE_MENU"]) {
2022-12-20 00:43:47 +01:00
if (pWindow->m_uSurface.xwayland->window_type[i] == HYPRATOMS["_NET_WM_WINDOW_TYPE_DROPDOWN_MENU"] ||
pWindow->m_uSurface.xwayland->window_type[i] == HYPRATOMS["_NET_WM_WINDOW_TYPE_MENU"])
pWindow->m_bX11ShouldntFocus = true;
pWindow->m_bNoInitialFocus = true;
return true;
}
2022-03-20 19:26:16 +01:00
if (pWindow->m_uSurface.xwayland->role) {
try {
std::string winrole = std::string(pWindow->m_uSurface.xwayland->role);
if (winrole.contains("pop-up") || winrole.contains("task_dialog")) {
return true;
}
} catch (std::exception& e) { Debug::log(ERR, "Error in shouldBeFloated, winrole threw %s", e.what()); }
}
2022-03-22 18:29:13 +01:00
if (pWindow->m_uSurface.xwayland->modal) {
pWindow->m_bIsModal = true;
2022-03-20 19:26:16 +01:00
return true;
2022-03-22 18:29:13 +01:00
}
2022-03-20 19:26:16 +01:00
2022-04-18 13:10:58 +02:00
if (pWindow->m_iX11Type == 2) {
return true; // override_redirect
}
2022-03-20 19:26:16 +01:00
const auto SIZEHINTS = pWindow->m_uSurface.xwayland->size_hints;
2022-03-22 21:28:57 +01:00
if (SIZEHINTS && (pWindow->m_uSurface.xwayland->parent || ((SIZEHINTS->min_width == SIZEHINTS->max_width) && (SIZEHINTS->min_height == SIZEHINTS->max_height))))
2022-03-20 19:26:16 +01:00
return true;
2022-03-21 17:24:41 +01:00
} else {
const auto PSTATE = &pWindow->m_uSurface.xdg->toplevel->current;
2022-09-25 20:07:48 +02:00
if ((PSTATE->min_width != 0 && PSTATE->min_height != 0 && (PSTATE->min_width == PSTATE->max_width || PSTATE->min_height == PSTATE->max_height)) ||
pWindow->m_uSurface.xdg->toplevel->parent)
2022-03-21 17:24:41 +01:00
return true;
2022-03-20 13:37:07 +01:00
}
return false;
}
void CHyprXWaylandManager::moveXWaylandWindow(CWindow* pWindow, const Vector2D& pos) {
if (!g_pCompositor->windowValidMapped(pWindow))
return;
2022-09-25 20:07:48 +02:00
if (pWindow->m_bIsX11) {
2022-04-23 14:16:02 +02:00
wlr_xwayland_surface_configure(pWindow->m_uSurface.xwayland, pos.x, pos.y, pWindow->m_vRealSize.vec().x, pWindow->m_vRealSize.vec().y);
}
2022-03-22 17:31:19 +01:00
}
2022-03-22 21:28:57 +01:00
void CHyprXWaylandManager::checkBorders(CWindow* pWindow) {
if (!pWindow->m_bIsX11)
return;
for (size_t i = 0; i < pWindow->m_uSurface.xwayland->window_type_len; i++) {
if (pWindow->m_uSurface.xwayland->window_type[i] == HYPRATOMS["_NET_WM_WINDOW_TYPE_POPUP_MENU"] ||
pWindow->m_uSurface.xwayland->window_type[i] == HYPRATOMS["_NET_WM_WINDOW_TYPE_NOTIFICATION"] ||
pWindow->m_uSurface.xwayland->window_type[i] == HYPRATOMS["_NET_WM_WINDOW_TYPE_DROPDOWN_MENU"] ||
pWindow->m_uSurface.xwayland->window_type[i] == HYPRATOMS["_NET_WM_WINDOW_TYPE_COMBO"] ||
pWindow->m_uSurface.xwayland->window_type[i] == HYPRATOMS["_NET_WM_WINDOW_TYPE_MENU"] ||
pWindow->m_uSurface.xwayland->window_type[i] == HYPRATOMS["_NET_WM_WINDOW_TYPE_SPLASH"] ||
2022-03-22 21:28:57 +01:00
pWindow->m_uSurface.xwayland->window_type[i] == HYPRATOMS["_NET_WM_WINDOW_TYPE_TOOLTIP"]) {
2022-09-25 20:07:48 +02:00
2022-03-22 21:28:57 +01:00
pWindow->m_bX11DoesntWantBorders = true;
return;
}
}
if (pWindow->m_iX11Type == 2) {
2022-03-22 21:28:57 +01:00
pWindow->m_bX11DoesntWantBorders = true;
}
2022-03-30 17:39:04 +02:00
}
void CHyprXWaylandManager::setWindowFullscreen(CWindow* pWindow, bool fullscreen) {
if (pWindow->m_bIsX11) {
wlr_xwayland_surface_set_fullscreen(pWindow->m_uSurface.xwayland, fullscreen);
} else {
wlr_xdg_toplevel_set_fullscreen(pWindow->m_uSurface.xdg->toplevel, fullscreen);
}
2022-05-25 18:42:38 +02:00
2022-05-29 11:24:42 +02:00
if (pWindow->m_phForeignToplevel)
wlr_foreign_toplevel_handle_v1_set_fullscreen(pWindow->m_phForeignToplevel, fullscreen);
}
Vector2D CHyprXWaylandManager::getMaxSizeForWindow(CWindow* pWindow) {
2022-08-05 19:41:50 +02:00
if (!g_pCompositor->windowValidMapped(pWindow))
return Vector2D(99999, 99999);
if ((pWindow->m_bIsX11 && !pWindow->m_uSurface.xwayland->size_hints) || (!pWindow->m_bIsX11 && !pWindow->m_uSurface.xdg->toplevel) ||
pWindow->m_sAdditionalConfigData.noMaxSize)
2022-08-05 19:41:50 +02:00
return Vector2D(99999, 99999);
auto MAXSIZE = pWindow->m_bIsX11 ? Vector2D(pWindow->m_uSurface.xwayland->size_hints->max_width, pWindow->m_uSurface.xwayland->size_hints->max_height) :
Vector2D(pWindow->m_uSurface.xdg->toplevel->current.max_width, pWindow->m_uSurface.xdg->toplevel->current.max_height);
if (MAXSIZE.x < 5)
MAXSIZE.x = 99999;
if (MAXSIZE.y < 5)
MAXSIZE.y = 99999;
return MAXSIZE;
}