Hyprland/src/Compositor.cpp

711 lines
24 KiB
C++
Raw Normal View History

2022-03-16 23:37:21 +03:00
#include "Compositor.hpp"
CCompositor::CCompositor() {
2022-03-20 18:51:14 +03:00
unlink("/tmp/hypr/hyprland.log");
unlink("/tmp/hypr/hyprlandd.log");
unlink("/tmp/hypr/.hyprlandrq");
2022-03-19 01:52:36 +03:00
system("mkdir -p /tmp/hypr");
2022-03-16 23:37:21 +03:00
m_sWLDisplay = wl_display_create();
m_sWLRBackend = wlr_backend_autocreate(m_sWLDisplay);
if (!m_sWLRBackend) {
Debug::log(CRIT, "m_sWLRBackend was NULL!");
RIP("m_sWLRBackend NULL!");
return;
}
2022-04-04 20:44:25 +03:00
m_iDRMFD = wlr_backend_get_drm_fd(m_sWLRBackend);
if (m_iDRMFD < 0) {
2022-03-24 17:57:46 +03:00
Debug::log(CRIT, "Couldn't query the DRM FD!");
2022-03-24 19:17:08 +03:00
RIP("DRMFD NULL!");
2022-03-24 17:57:46 +03:00
return;
}
2022-04-04 20:44:25 +03:00
m_sWLRRenderer = wlr_gles2_renderer_create_with_drm_fd(m_iDRMFD);
2022-03-16 23:37:21 +03:00
if (!m_sWLRRenderer) {
Debug::log(CRIT, "m_sWLRRenderer was NULL!");
RIP("m_sWLRRenderer NULL!");
return;
}
2022-03-17 21:03:15 +03:00
wlr_renderer_init_wl_display(m_sWLRRenderer, m_sWLDisplay);
2022-03-16 23:37:21 +03:00
m_sWLRAllocator = wlr_allocator_autocreate(m_sWLRBackend, m_sWLRRenderer);
if (!m_sWLRAllocator) {
Debug::log(CRIT, "m_sWLRAllocator was NULL!");
RIP("m_sWLRAllocator NULL!");
return;
}
2022-03-24 19:17:08 +03:00
m_sWLREGL = wlr_gles2_renderer_get_egl(m_sWLRRenderer);
if (!m_sWLREGL) {
Debug::log(CRIT, "m_sWLREGL was NULL!");
RIP("m_sWLREGL NULL!");
return;
}
2022-03-16 23:37:21 +03:00
m_sWLRCompositor = wlr_compositor_create(m_sWLDisplay, m_sWLRRenderer);
2022-03-17 21:03:15 +03:00
m_sWLRSubCompositor = wlr_subcompositor_create(m_sWLDisplay);
m_sWLRDataDevMgr = wlr_data_device_manager_create(m_sWLDisplay);
2022-04-14 22:48:12 +03:00
m_sWLRDmabuf = wlr_linux_dmabuf_v1_create(m_sWLDisplay, m_sWLRRenderer);
2022-03-19 16:09:11 +03:00
wlr_export_dmabuf_manager_v1_create(m_sWLDisplay);
wlr_screencopy_manager_v1_create(m_sWLDisplay);
wlr_data_control_manager_v1_create(m_sWLDisplay);
wlr_gamma_control_manager_v1_create(m_sWLDisplay);
wlr_primary_selection_v1_device_manager_create(m_sWLDisplay);
wlr_viewporter_create(m_sWLDisplay);
2022-03-16 23:37:21 +03:00
m_sWLROutputLayout = wlr_output_layout_create();
2022-03-17 21:03:15 +03:00
m_sWLRScene = wlr_scene_create();
wlr_scene_attach_output_layout(m_sWLRScene, m_sWLROutputLayout);
2022-03-16 23:37:21 +03:00
2022-03-17 21:03:15 +03:00
m_sWLRXDGShell = wlr_xdg_shell_create(m_sWLDisplay);
2022-03-16 23:37:21 +03:00
m_sWLRCursor = wlr_cursor_create();
wlr_cursor_attach_output_layout(m_sWLRCursor, m_sWLROutputLayout);
2022-03-17 21:03:15 +03:00
m_sWLRXCursorMgr = wlr_xcursor_manager_create(nullptr, 24);
wlr_xcursor_manager_load(m_sWLRXCursorMgr, 1);
2022-03-16 23:37:21 +03:00
2022-03-22 20:29:13 +03:00
m_sSeat.seat = wlr_seat_create(m_sWLDisplay, "seat0");
2022-03-17 21:03:15 +03:00
2022-03-17 22:22:29 +03:00
m_sWLRPresentation = wlr_presentation_create(m_sWLDisplay, m_sWLRBackend);
2022-03-18 22:42:49 +03:00
m_sWLRIdle = wlr_idle_create(m_sWLDisplay);
2022-03-19 15:54:24 +03:00
m_sWLRLayerShell = wlr_layer_shell_v1_create(m_sWLDisplay);
wlr_server_decoration_manager_set_default_mode(wlr_server_decoration_manager_create(m_sWLDisplay), WLR_SERVER_DECORATION_MANAGER_MODE_SERVER);
wlr_xdg_decoration_manager_v1_create(m_sWLDisplay);
wlr_xdg_output_manager_v1_create(m_sWLDisplay, m_sWLROutputLayout);
m_sWLROutputMgr = wlr_output_manager_v1_create(m_sWLDisplay);
2022-03-22 20:29:13 +03:00
m_sWLRInhibitMgr = wlr_input_inhibit_manager_create(m_sWLDisplay);
m_sWLRKbShInhibitMgr = wlr_keyboard_shortcuts_inhibit_v1_create(m_sWLDisplay);
m_sWLREXTWorkspaceMgr = wlr_ext_workspace_manager_v1_create(m_sWLDisplay);
m_sWLRPointerConstraints = wlr_pointer_constraints_v1_create(m_sWLDisplay);
m_sWLRRelPointerMgr = wlr_relative_pointer_manager_v1_create(m_sWLDisplay);
2022-03-16 23:37:21 +03:00
}
2022-03-17 17:53:45 +03:00
CCompositor::~CCompositor() {
cleanupExit();
}
2022-03-17 17:53:45 +03:00
void handleCritSignal(int signo) {
g_pCompositor->cleanupExit();
exit(signo);
2022-03-17 17:53:45 +03:00
}
2022-03-19 01:52:36 +03:00
void CCompositor::initAllSignals() {
2022-03-28 17:10:30 +03:00
addWLSignal(&m_sWLRBackend->events.new_output, &Events::listen_newOutput, m_sWLRBackend, "Backend");
addWLSignal(&m_sWLRXDGShell->events.new_surface, &Events::listen_newXDGSurface, m_sWLRXDGShell, "XDG Shell");
addWLSignal(&m_sWLRCursor->events.motion, &Events::listen_mouseMove, m_sWLRCursor, "WLRCursor");
addWLSignal(&m_sWLRCursor->events.motion_absolute, &Events::listen_mouseMoveAbsolute, m_sWLRCursor, "WLRCursor");
addWLSignal(&m_sWLRCursor->events.button, &Events::listen_mouseButton, m_sWLRCursor, "WLRCursor");
addWLSignal(&m_sWLRCursor->events.axis, &Events::listen_mouseAxis, m_sWLRCursor, "WLRCursor");
addWLSignal(&m_sWLRCursor->events.frame, &Events::listen_mouseFrame, m_sWLRCursor, "WLRCursor");
addWLSignal(&m_sWLRBackend->events.new_input, &Events::listen_newInput, m_sWLRBackend, "Backend");
addWLSignal(&m_sSeat.seat->events.request_set_cursor, &Events::listen_requestMouse, &m_sSeat, "Seat");
addWLSignal(&m_sSeat.seat->events.request_set_selection, &Events::listen_requestSetSel, &m_sSeat, "Seat");
addWLSignal(&m_sSeat.seat->events.request_start_drag, &Events::listen_requestDrag, &m_sSeat, "Seat");
2022-03-31 18:25:23 +03:00
addWLSignal(&m_sSeat.seat->events.start_drag, &Events::listen_startDrag, &m_sSeat, "Seat");
2022-03-28 17:10:30 +03:00
addWLSignal(&m_sWLRLayerShell->events.new_surface, &Events::listen_newLayerSurface, m_sWLRLayerShell, "LayerShell");
addWLSignal(&m_sWLROutputLayout->events.change, &Events::listen_change, m_sWLROutputLayout, "OutputLayout");
addWLSignal(&m_sWLROutputMgr->events.apply, &Events::listen_outputMgrApply, m_sWLROutputMgr, "OutputMgr");
addWLSignal(&m_sWLROutputMgr->events.test, &Events::listen_outputMgrTest, m_sWLROutputMgr, "OutputMgr");
addWLSignal(&m_sWLRInhibitMgr->events.activate, &Events::listen_InhibitActivate, m_sWLRInhibitMgr, "InhibitMgr");
addWLSignal(&m_sWLRInhibitMgr->events.deactivate, &Events::listen_InhibitDeactivate, m_sWLRInhibitMgr, "InhibitMgr");
addWLSignal(&m_sWLRPointerConstraints->events.new_constraint, &Events::listen_newConstraint, m_sWLRPointerConstraints, "PointerConstraints");
signal(SIGINT, handleCritSignal);
signal(SIGTERM, handleCritSignal);
}
void CCompositor::cleanupExit() {
if (!m_sWLDisplay)
return;
if (g_pXWaylandManager->m_sWLRXWayland) {
wlr_xwayland_destroy(g_pXWaylandManager->m_sWLRXWayland);
g_pXWaylandManager->m_sWLRXWayland = nullptr;
}
wl_display_destroy_clients(m_sWLDisplay);
wl_display_destroy(m_sWLDisplay);
m_sWLDisplay = nullptr;
2022-03-19 01:52:36 +03:00
}
2022-03-17 19:08:54 +03:00
2022-03-19 01:52:36 +03:00
void CCompositor::startCompositor() {
2022-03-17 19:08:54 +03:00
// Init all the managers BEFORE we start with the wayland server so that ALL of the stuff is initialized
// properly and we dont get any bad mem reads.
//
Debug::log(LOG, "Creating the CHyprError!");
g_pHyprError = std::make_unique<CHyprError>();
2022-03-19 19:48:18 +03:00
Debug::log(LOG, "Creating the KeybindManager!");
g_pKeybindManager = std::make_unique<CKeybindManager>();
2022-03-18 22:03:39 +03:00
Debug::log(LOG, "Creating the ConfigManager!");
2022-03-17 19:08:54 +03:00
g_pConfigManager = std::make_unique<CConfigManager>();
2022-03-18 22:03:39 +03:00
Debug::log(LOG, "Creating the ThreadManager!");
g_pThreadManager = std::make_unique<CThreadManager>();
2022-03-17 19:08:54 +03:00
Debug::log(LOG, "Creating the InputManager!");
g_pInputManager = std::make_unique<CInputManager>();
2022-03-17 22:22:29 +03:00
2022-04-04 20:44:25 +03:00
Debug::log(LOG, "Creating the CHyprOpenGLImpl!");
g_pHyprOpenGL = std::make_unique<CHyprOpenGLImpl>();
2022-03-17 22:22:29 +03:00
Debug::log(LOG, "Creating the HyprRenderer!");
g_pHyprRenderer = std::make_unique<CHyprRenderer>();
2022-03-18 22:03:39 +03:00
Debug::log(LOG, "Creating the XWaylandManager!");
g_pXWaylandManager = std::make_unique<CHyprXWaylandManager>();
Debug::log(LOG, "Creating the LayoutManager!");
g_pLayoutManager = std::make_unique<CLayoutManager>();
2022-03-24 00:01:59 +03:00
Debug::log(LOG, "Creating the AnimationManager!");
g_pAnimationManager = std::make_unique<CAnimationManager>();
2022-03-17 19:08:54 +03:00
//
//
2022-03-19 01:52:36 +03:00
initAllSignals();
// Set some env vars so that Firefox is automatically in Wayland mode
// and QT apps too
// electron needs -- flags so we can't really set them here
setenv("QT_QPA_PLATFORM", "wayland", true);
setenv("MOZ_ENABLE_WAYLAND", "1", true);
2022-03-31 20:16:00 +03:00
// Set XDG_CURRENT_DESKTOP to our compositor's name
setenv("XDG_CURRENT_DESKTOP", "hyprland", true);
2022-03-16 23:37:21 +03:00
m_szWLDisplaySocket = wl_display_add_socket_auto(m_sWLDisplay);
if (!m_szWLDisplaySocket) {
Debug::log(CRIT, "m_szWLDisplaySocket NULL!");
2022-03-17 21:03:15 +03:00
wlr_backend_destroy(m_sWLRBackend);
2022-03-16 23:37:21 +03:00
RIP("m_szWLDisplaySocket NULL!");
}
setenv("WAYLAND_DISPLAY", m_szWLDisplaySocket, 1);
signal(SIGPIPE, SIG_IGN);
2022-03-17 17:53:45 +03:00
Debug::log(LOG, "Running on WAYLAND_DISPLAY: %s", m_szWLDisplaySocket);
2022-03-16 23:37:21 +03:00
if (!wlr_backend_start(m_sWLRBackend)) {
Debug::log(CRIT, "Backend did not start!");
2022-03-17 21:03:15 +03:00
wlr_backend_destroy(m_sWLRBackend);
wl_display_destroy(m_sWLDisplay);
2022-03-16 23:37:21 +03:00
RIP("Backend did not start!");
}
wlr_xcursor_manager_set_cursor_image(m_sWLRXCursorMgr, "left_ptr", m_sWLRCursor);
// This blocks until we are done.
Debug::log(LOG, "Hyprland is ready, running the event loop!");
wl_display_run(m_sWLDisplay);
2022-03-17 22:22:29 +03:00
}
SMonitor* CCompositor::getMonitorFromID(const int& id) {
2022-03-19 00:35:51 +03:00
for (auto& m : m_lMonitors) {
2022-03-20 13:23:36 +03:00
if (m.ID == (uint64_t)id) {
2022-03-17 22:22:29 +03:00
return &m;
}
}
return nullptr;
}
SMonitor* CCompositor::getMonitorFromCursor() {
2022-03-19 22:30:21 +03:00
const auto COORDS = Vector2D(m_sWLRCursor->x, m_sWLRCursor->y);
2022-03-17 22:22:29 +03:00
const auto OUTPUT = wlr_output_layout_output_at(m_sWLROutputLayout, COORDS.x, COORDS.y);
if (!OUTPUT) {
2022-03-19 22:30:21 +03:00
Debug::log(WARN, "getMonitorFromCursor: cursor outside monitors??");
2022-03-19 00:35:51 +03:00
return &m_lMonitors.front();
2022-03-17 22:22:29 +03:00
}
2022-03-19 00:35:51 +03:00
for (auto& m : m_lMonitors) {
2022-03-17 22:22:29 +03:00
if (m.output == OUTPUT)
return &m;
}
2022-03-19 22:30:21 +03:00
Debug::log(LOG, "Monitor not in list??");
2022-03-19 00:35:51 +03:00
return &m_lMonitors.front();
}
SMonitor* CCompositor::getMonitorFromVector(const Vector2D& point) {
const auto OUTPUT = wlr_output_layout_output_at(m_sWLROutputLayout, point.x, point.y);
if (!OUTPUT) {
Debug::log(WARN, "getMonitorFromVector: vector outside monitors? Returning front");
return &m_lMonitors.front();
}
return getMonitorFromOutput(OUTPUT);
}
2022-03-19 00:35:51 +03:00
void CCompositor::removeWindowFromVectorSafe(CWindow* pWindow) {
2022-04-05 20:28:10 +03:00
if (windowExists(pWindow) && !pWindow->m_bFadingOut)
2022-03-19 01:16:15 +03:00
m_lWindows.remove(*pWindow);
2022-03-19 00:53:27 +03:00
}
bool CCompositor::windowExists(CWindow* pWindow) {
for (auto& w : m_lWindows) {
if (&w == pWindow)
return true;
}
return false;
}
CWindow* CCompositor::vectorToWindow(const Vector2D& pos) {
2022-03-20 17:55:47 +03:00
const auto PMONITOR = getMonitorFromVector(pos);
2022-04-04 17:25:30 +03:00
// first loop over floating cuz they're above, m_lWindows should be sorted bottom->top, for tiled it doesn't matter.
for (auto w = m_lWindows.rbegin(); w != m_lWindows.rend(); w++) {
wlr_box box = {w->m_vRealPosition.x, w->m_vRealPosition.y, w->m_vRealSize.x, w->m_vRealSize.y};
if (wlr_box_contains_point(&box, pos.x, pos.y) && w->m_bIsMapped && w->m_bIsFloating && isWorkspaceVisible(w->m_iWorkspaceID) && !w->m_bHidden)
return &(*w);
}
for (auto& w : m_lWindows) {
wlr_box box = {w.m_vRealPosition.x, w.m_vRealPosition.y, w.m_vRealSize.x, w.m_vRealSize.y};
if (wlr_box_contains_point(&box, pos.x, pos.y) && w.m_bIsMapped && !w.m_bIsFloating && PMONITOR->activeWorkspace == w.m_iWorkspaceID && !w.m_bHidden)
2022-03-19 00:53:27 +03:00
return &w;
}
return nullptr;
}
2022-03-20 20:31:58 +03:00
CWindow* CCompositor::vectorToWindowTiled(const Vector2D& pos) {
const auto PMONITOR = getMonitorFromVector(pos);
for (auto& w : m_lWindows) {
wlr_box box = {w.m_vPosition.x, w.m_vPosition.y, w.m_vSize.x, w.m_vSize.y};
if (w.m_bIsMapped && wlr_box_contains_point(&box, pos.x, pos.y) && w.m_iWorkspaceID == PMONITOR->activeWorkspace && !w.m_bIsFloating && !w.m_bHidden)
2022-03-20 20:31:58 +03:00
return &w;
}
return nullptr;
}
2022-03-19 19:48:18 +03:00
CWindow* CCompositor::vectorToWindowIdeal(const Vector2D& pos) {
2022-03-20 17:55:47 +03:00
const auto PMONITOR = getMonitorFromVector(pos);
2022-04-04 17:25:30 +03:00
// first loop over floating cuz they're above, m_lWindows should be sorted bottom->top, for tiled it doesn't matter.
for (auto w = m_lWindows.rbegin(); w != m_lWindows.rend(); w++) {
wlr_box box = {w->m_vRealPosition.x, w->m_vRealPosition.y, w->m_vRealSize.x, w->m_vRealSize.y};
if (w->m_bIsFloating && w->m_bIsMapped && wlr_box_contains_point(&box, m_sWLRCursor->x, m_sWLRCursor->y) && isWorkspaceVisible(w->m_iWorkspaceID) && !w->m_bHidden)
return &(*w);
2022-03-20 13:14:24 +03:00
}
2022-03-19 22:30:21 +03:00
for (auto& w : m_lWindows) {
wlr_box box = {w.m_vPosition.x, w.m_vPosition.y, w.m_vSize.x, w.m_vSize.y};
if (!w.m_bIsFloating && w.m_bIsMapped && wlr_box_contains_point(&box, pos.x, pos.y) && w.m_iWorkspaceID == PMONITOR->activeWorkspace && !w.m_bHidden)
2022-03-19 22:30:21 +03:00
return &w;
}
return nullptr;
}
CWindow* CCompositor::windowFromCursor() {
const auto PMONITOR = getMonitorFromCursor();
2022-03-20 13:14:24 +03:00
2022-04-04 17:25:30 +03:00
// first loop over floating cuz they're above, m_lWindows should be sorted bottom->top, for tiled it doesn't matter.
for (auto w = m_lWindows.rbegin(); w != m_lWindows.rend(); w++) {
wlr_box box = {w->m_vRealPosition.x, w->m_vRealPosition.y, w->m_vRealSize.x, w->m_vRealSize.y};
if (wlr_box_contains_point(&box, m_sWLRCursor->x, m_sWLRCursor->y) && w->m_bIsMapped && w->m_bIsFloating && isWorkspaceVisible(w->m_iWorkspaceID))
return &(*w);
2022-03-20 13:14:24 +03:00
}
2022-03-19 19:48:18 +03:00
for (auto& w : m_lWindows) {
wlr_box box = {w.m_vPosition.x, w.m_vPosition.y, w.m_vSize.x, w.m_vSize.y};
2022-03-30 22:18:42 +03:00
if (wlr_box_contains_point(&box, m_sWLRCursor->x, m_sWLRCursor->y) && w.m_bIsMapped && w.m_iWorkspaceID == PMONITOR->activeWorkspace)
2022-03-19 19:48:18 +03:00
return &w;
}
return nullptr;
}
2022-03-20 13:14:24 +03:00
CWindow* CCompositor::windowFloatingFromCursor() {
for (auto w = m_lWindows.rbegin(); w != m_lWindows.rend(); w++) {
wlr_box box = {w->m_vRealPosition.x, w->m_vRealPosition.y, w->m_vRealSize.x, w->m_vRealSize.y};
if (wlr_box_contains_point(&box, m_sWLRCursor->x, m_sWLRCursor->y) && w->m_bIsMapped && w->m_bIsFloating && isWorkspaceVisible(w->m_iWorkspaceID) && !w->m_bHidden)
return &(*w);
2022-03-20 13:14:24 +03:00
}
return nullptr;
}
2022-04-02 14:02:16 +03:00
wlr_surface* CCompositor::vectorWindowToSurface(const Vector2D& pos, CWindow* pWindow, Vector2D& sl) {
if (!windowValidMapped(pWindow))
return nullptr;
RASSERT(!pWindow->m_bIsX11, "Cannot call vectorWindowToSurface on an X11 window!");
const auto PSURFACE = pWindow->m_uSurface.xdg;
double subx, suby;
const auto PFOUND = wlr_xdg_surface_surface_at(PSURFACE, pos.x - pWindow->m_vRealPosition.x, pos.y - pWindow->m_vRealPosition.y, &subx, &suby);
if (PFOUND) {
sl.x = subx;
sl.y = suby;
return PFOUND;
}
sl.x = pos.x - pWindow->m_vRealPosition.x;
sl.y = pos.y - pWindow->m_vRealPosition.y;
return PSURFACE->surface;
}
2022-03-19 22:56:19 +03:00
SMonitor* CCompositor::getMonitorFromOutput(wlr_output* out) {
for (auto& m : m_lMonitors) {
if (m.output == out) {
return &m;
}
}
return nullptr;
}
2022-04-02 19:57:09 +03:00
void CCompositor::focusWindow(CWindow* pWindow, wlr_surface* pSurface) {
2022-03-19 00:53:27 +03:00
2022-04-18 18:16:01 +03:00
if (g_pCompositor->m_sSeat.exclusiveClient) {
Debug::log(LOG, "Disallowing setting focus to a window due to there being an active input inhibitor layer.");
return;
}
2022-03-27 20:16:33 +03:00
if (!pWindow || !windowValidMapped(pWindow)) {
2022-03-22 20:29:13 +03:00
wlr_seat_keyboard_notify_clear_focus(m_sSeat.seat);
2022-03-19 00:53:27 +03:00
return;
}
if (m_pLastWindow == pWindow && m_sSeat.seat->keyboard_state.focused_surface == pSurface)
2022-04-05 19:29:58 +03:00
return;
if (windowValidMapped(m_pLastWindow) && m_pLastWindow->m_bIsX11) {
wlr_seat_keyboard_notify_clear_focus(m_sSeat.seat);
wlr_seat_pointer_clear_focus(m_sSeat.seat);
}
2022-04-02 19:57:09 +03:00
const auto PWINDOWSURFACE = pSurface ? pSurface : g_pXWaylandManager->getWindowSurface(pWindow);
2022-03-19 00:53:27 +03:00
2022-04-02 19:57:09 +03:00
focusSurface(PWINDOWSURFACE, pWindow);
g_pXWaylandManager->activateWindow(pWindow, true);
// do pointer focus too
const auto POINTERLOCAL = g_pInputManager->getMouseCoordsInternal() - pWindow->m_vRealPosition;
wlr_seat_pointer_notify_enter(m_sSeat.seat, PWINDOWSURFACE, POINTERLOCAL.x, POINTERLOCAL.y);
2022-04-13 21:19:40 +03:00
2022-04-02 19:57:09 +03:00
m_pLastWindow = pWindow;
2022-03-20 16:36:55 +03:00
}
2022-04-02 14:02:16 +03:00
void CCompositor::focusSurface(wlr_surface* pSurface, CWindow* pWindowOwner) {
2022-04-18 18:16:01 +03:00
2022-04-02 14:02:16 +03:00
if (m_sSeat.seat->keyboard_state.focused_surface == pSurface || (pWindowOwner && m_sSeat.seat->keyboard_state.focused_surface == g_pXWaylandManager->getWindowSurface(pWindowOwner)))
2022-03-20 16:36:55 +03:00
return; // Don't focus when already focused on this.
2022-03-27 20:16:33 +03:00
if (!pSurface)
return;
2022-03-31 22:47:03 +03:00
// Unfocus last surface if should
2022-04-02 19:57:09 +03:00
if (m_pLastFocus && m_sSeat.seat->keyboard_state.focused_surface && wlr_surface_is_xdg_surface(m_pLastFocus))
wlr_xdg_toplevel_set_activated(wlr_xdg_surface_from_wlr_surface(m_pLastFocus)->toplevel, false);
2022-03-19 12:53:39 +03:00
2022-03-22 20:29:13 +03:00
const auto KEYBOARD = wlr_seat_get_keyboard(m_sSeat.seat);
2022-03-27 20:16:33 +03:00
if (!KEYBOARD)
return;
2022-03-22 20:29:13 +03:00
wlr_seat_keyboard_notify_enter(m_sSeat.seat, pSurface, KEYBOARD->keycodes, KEYBOARD->num_keycodes, &KEYBOARD->modifiers);
2022-03-19 00:53:27 +03:00
2022-04-02 20:09:27 +03:00
wlr_seat_keyboard_focus_change_event event = {
.seat = m_sSeat.seat,
.old_surface = m_pLastFocus,
.new_surface = pSurface,
};
wlr_signal_emit_safe(&m_sSeat.seat->keyboard_state.events.focus_change, &event);
if (pWindowOwner)
Debug::log(LOG, "Set keyboard focus to surface %x, with window name: %s", pSurface, pWindowOwner->m_szTitle.c_str());
2022-03-31 20:41:55 +03:00
else
Debug::log(LOG, "Set keyboard focus to surface %x", pSurface);
2022-03-19 01:16:15 +03:00
}
bool CCompositor::windowValidMapped(CWindow* pWindow) {
if (!windowExists(pWindow))
return false;
if (pWindow->m_bIsX11 && !pWindow->m_bMappedX11)
return false;
2022-03-22 22:53:11 +03:00
if (!pWindow->m_bIsMapped)
return false;
if (pWindow->m_bHidden)
return false;
2022-03-19 01:16:15 +03:00
if (!g_pXWaylandManager->getWindowSurface(pWindow))
return false;
return true;
2022-03-20 14:11:57 +03:00
}
2022-03-20 16:00:46 +03:00
CWindow* CCompositor::getWindowForPopup(wlr_xdg_popup* popup) {
for (auto& p : m_lXDGPopups) {
if (p.popup == popup)
return p.parentWindow;
}
2022-03-20 16:36:55 +03:00
return nullptr;
}
wlr_surface* CCompositor::vectorToLayerSurface(const Vector2D& pos, std::list<SLayerSurface*>* layerSurfaces, Vector2D* sCoords) {
for (auto& l : *layerSurfaces) {
if (!l->layerSurface->mapped)
continue;
const auto SURFACEAT = wlr_layer_surface_v1_surface_at(l->layerSurface, pos.x - l->geometry.x, pos.y - l->geometry.y, &sCoords->x, &sCoords->y);
if (SURFACEAT)
return SURFACEAT;
}
return nullptr;
}
CWindow* CCompositor::getWindowFromSurface(wlr_surface* pSurface) {
for (auto& w : m_lWindows) {
if (g_pXWaylandManager->getWindowSurface(&w) == pSurface)
return &w;
}
2022-03-20 17:55:47 +03:00
return nullptr;
}
2022-03-21 21:18:33 +03:00
CWindow* CCompositor::getFullscreenWindowOnWorkspace(const int& ID) {
for (auto& w : m_lWindows) {
if (w.m_iWorkspaceID == ID && w.m_bIsFullscreen)
return &w;
}
return nullptr;
}
2022-03-20 17:55:47 +03:00
bool CCompositor::isWorkspaceVisible(const int& w) {
for (auto& m : m_lMonitors) {
if (m.activeWorkspace == w)
return true;
}
return false;
}
CWorkspace* CCompositor::getWorkspaceByID(const int& id) {
2022-03-20 17:55:47 +03:00
for (auto& w : m_lWorkspaces) {
if (w.m_iID == id)
2022-03-20 17:55:47 +03:00
return &w;
}
return nullptr;
}
void CCompositor::sanityCheckWorkspaces() {
for (auto it = m_lWorkspaces.begin(); it != m_lWorkspaces.end(); ++it) {
if (getWindowsOnWorkspace(it->m_iID) == 0 && !isWorkspaceVisible(it->m_iID)) {
2022-03-20 17:55:47 +03:00
it = m_lWorkspaces.erase(it);
}
2022-03-20 17:55:47 +03:00
}
}
int CCompositor::getWindowsOnWorkspace(const int& id) {
int no = 0;
for (auto& w : m_lWindows) {
if (w.m_iWorkspaceID == id && w.m_bIsMapped)
2022-03-20 17:55:47 +03:00
no++;
}
return no;
}
CWindow* CCompositor::getFirstWindowOnWorkspace(const int& id) {
for (auto& w : m_lWindows) {
if (w.m_iWorkspaceID == id)
return &w;
}
2022-03-20 16:00:46 +03:00
return nullptr;
}
void CCompositor::fixXWaylandWindowsOnWorkspace(const int& id) {
const auto ISVISIBLE = isWorkspaceVisible(id);
2022-03-21 21:18:33 +03:00
const auto PWORKSPACE = g_pCompositor->getWorkspaceByID(id);
for (auto& w : m_lWindows) {
if (w.m_iWorkspaceID == id) {
// moveXWaylandWindow only moves XWayland windows
// so there is no need to check here
// if the window is XWayland or not.
if (ISVISIBLE && (!PWORKSPACE->m_bHasFullscreenWindow || w.m_bIsFullscreen))
g_pXWaylandManager->moveXWaylandWindow(&w, w.m_vRealPosition);
else
g_pXWaylandManager->moveXWaylandWindow(&w, Vector2D(42069,42069));
}
}
2022-03-22 20:29:13 +03:00
}
bool CCompositor::doesSeatAcceptInput(wlr_surface* surface) {
2022-03-22 23:59:14 +03:00
return !m_sSeat.exclusiveClient || (surface && m_sSeat.exclusiveClient == wl_resource_get_client(surface->resource));
2022-04-02 14:02:16 +03:00
}
bool CCompositor::isWindowActive(CWindow* pWindow) {
2022-04-02 19:57:09 +03:00
if (!windowValidMapped(pWindow))
return false;
2022-04-02 14:02:16 +03:00
const auto PSURFACE = g_pXWaylandManager->getWindowSurface(pWindow);
2022-04-02 19:57:09 +03:00
return PSURFACE == m_pLastFocus || pWindow == m_pLastWindow;
2022-04-04 17:25:30 +03:00
}
void CCompositor::moveWindowToTop(CWindow* pWindow) {
if (!windowValidMapped(pWindow))
return;
for (auto it = m_lWindows.begin(); it != m_lWindows.end(); ++it) {
if (&(*it) == pWindow) {
m_lWindows.splice(m_lWindows.end(), m_lWindows, it);
break;
}
}
2022-04-05 20:28:10 +03:00
}
void CCompositor::cleanupWindows() {
for (auto& w : m_lWindowsFadingOut) {
if (!w->m_bFadingOut || w->m_fAlpha == 0.f) {
if (!w->m_bReadyToDelete)
continue;
2022-04-05 21:49:15 +03:00
g_pHyprOpenGL->m_mWindowFramebuffers[w].release();
g_pHyprOpenGL->m_mWindowFramebuffers.erase(w);
m_lWindows.remove(*w);
m_lWindowsFadingOut.remove(w);
Debug::log(LOG, "Cleanup: destroyed a window");
2022-04-05 20:28:10 +03:00
return;
}
}
2022-04-09 14:26:55 +03:00
}
CWindow* CCompositor::getWindowInDirection(CWindow* pWindow, char dir) {
const auto POSA = pWindow->m_vPosition;
const auto SIZEA = pWindow->m_vSize;
auto longestIntersect = -1;
CWindow* longestIntersectWindow = nullptr;
for (auto& w : m_lWindows) {
if (&w == pWindow || !windowValidMapped(&w) || w.m_bIsFloating || w.m_iWorkspaceID != pWindow->m_iWorkspaceID)
continue;
const auto POSB = w.m_vPosition;
const auto SIZEB = w.m_vSize;
switch (dir) {
case 'l':
if (STICKS(POSA.x, POSB.x + SIZEB.x)) {
const auto INTERSECTLEN = std::max((double)0, std::min(POSA.y + SIZEA.y, POSB.y + SIZEB.y) - std::max(POSA.y, POSB.y));
if (INTERSECTLEN > longestIntersect) {
longestIntersect = INTERSECTLEN;
longestIntersectWindow = &w;
}
}
break;
case 'r':
if (STICKS(POSA.x + SIZEA.x, POSB.x)) {
const auto INTERSECTLEN = std::max((double)0, std::min(POSA.y + SIZEA.y, POSB.y + SIZEB.y) - std::max(POSA.y, POSB.y));
if (INTERSECTLEN > longestIntersect) {
longestIntersect = INTERSECTLEN;
longestIntersectWindow = &w;
}
}
break;
case 't':
case 'u':
if (STICKS(POSA.y, POSB.y + SIZEB.y)) {
const auto INTERSECTLEN = std::max((double)0, std::min(POSA.x + SIZEA.x, POSB.x + SIZEB.x) - std::max(POSA.x, POSB.x));
if (INTERSECTLEN > longestIntersect) {
longestIntersect = INTERSECTLEN;
longestIntersectWindow = &w;
}
}
break;
case 'b':
case 'd':
if (STICKS(POSA.y + SIZEA.y, POSB.y)) {
const auto INTERSECTLEN = std::max((double)0, std::min(POSA.x + SIZEA.x, POSB.x + SIZEB.x) - std::max(POSA.x, POSB.x));
if (INTERSECTLEN > longestIntersect) {
longestIntersect = INTERSECTLEN;
longestIntersectWindow = &w;
}
}
break;
}
}
if (longestIntersect != -1)
return longestIntersectWindow;
return nullptr;
}
void CCompositor::deactivateAllWLRWorkspaces() {
for (auto& w : m_lWorkspaces) {
if (w.m_pWlrHandle)
wlr_ext_workspace_handle_v1_set_active(w.m_pWlrHandle, false);
}
2022-04-13 21:45:06 +03:00
}
CWindow* CCompositor::getNextWindowOnWorkspace(CWindow* pWindow) {
bool gotToWindow = false;
for (auto& w : m_lWindows) {
if (&w != pWindow && !gotToWindow)
continue;
if (&w == pWindow) {
gotToWindow = true;
continue;
}
if (w.m_iWorkspaceID == pWindow->m_iWorkspaceID && windowValidMapped(&w))
return &w;
}
for (auto& w : m_lWindows) {
if (&w != pWindow && w.m_iWorkspaceID == pWindow->m_iWorkspaceID && windowValidMapped(&w))
return &w;
}
return nullptr;
2022-03-16 23:37:21 +03:00
}