Hyprland/src/events/Layers.cpp

224 lines
10 KiB
C++
Raw Normal View History

2022-03-21 15:17:04 +01:00
#include "../Compositor.hpp"
#include "../helpers/WLClasses.hpp"
2022-06-09 12:46:55 +02:00
#include "../managers/input/InputManager.hpp"
2022-03-21 15:17:04 +01:00
#include "../render/Renderer.hpp"
#include "Events.hpp"
// --------------------------------------------- //
// _ __ ________ _____ _____ //
// | | /\\ \ / / ____| __ \ / ____| //
// | | / \\ \_/ /| |__ | |__) | (___ //
// | | / /\ \\ / | __| | _ / \___ \ //
// | |____ / ____ \| | | |____| | \ \ ____) | //
// |______/_/ \_\_| |______|_| \_\_____/ //
// //
// --------------------------------------------- //
void Events::listener_newLayerSurface(wl_listener* listener, void* data) {
const auto WLRLAYERSURFACE = (wlr_layer_surface_v1*)data;
if (!WLRLAYERSURFACE->output) {
const auto PMONITOR = g_pCompositor->getMonitorFromCursor();
if (!PMONITOR) {
Debug::log(ERR, "No monitor at cursor on new layer without a monitor. Ignoring.");
wlr_layer_surface_v1_destroy(WLRLAYERSURFACE);
return;
}
2022-06-09 17:32:58 +02:00
Debug::log(LOG, "New LayerSurface has no preferred monitor. Assigning Monitor %s", PMONITOR->szName.c_str());
2022-03-21 15:17:04 +01:00
WLRLAYERSURFACE->output = PMONITOR->output;
}
const auto PMONITOR = (SMonitor*)g_pCompositor->getMonitorFromOutput(WLRLAYERSURFACE->output);
2022-07-06 21:57:35 +02:00
SLayerSurface* layerSurface = PMONITOR->m_aLayerSurfaceLists[WLRLAYERSURFACE->pending.layer].emplace_back(new SLayerSurface());
layerSurface->szNamespace = WLRLAYERSURFACE->_namespace;
2022-03-21 15:17:04 +01:00
if (!WLRLAYERSURFACE->output) {
2022-06-30 15:44:26 +02:00
WLRLAYERSURFACE->output = g_pCompositor->m_vMonitors.front()->output; // TODO: current mon
2022-03-21 15:17:04 +01:00
}
2022-03-28 22:31:39 +02:00
layerSurface->hyprListener_commitLayerSurface.initCallback(&WLRLAYERSURFACE->surface->events.commit, &Events::listener_commitLayerSurface, layerSurface, "layerSurface");
layerSurface->hyprListener_destroyLayerSurface.initCallback(&WLRLAYERSURFACE->surface->events.destroy, &Events::listener_destroyLayerSurface, layerSurface, "layerSurface");
layerSurface->hyprListener_mapLayerSurface.initCallback(&WLRLAYERSURFACE->events.map, &Events::listener_mapLayerSurface, layerSurface, "layerSurface");
layerSurface->hyprListener_unmapLayerSurface.initCallback(&WLRLAYERSURFACE->events.unmap, &Events::listener_unmapLayerSurface, layerSurface, "layerSurface");
layerSurface->hyprListener_newPopup.initCallback(&WLRLAYERSURFACE->events.new_popup, &Events::listener_newPopup, layerSurface, "layerSurface");
2022-03-21 15:17:04 +01:00
layerSurface->layerSurface = WLRLAYERSURFACE;
2022-03-21 22:34:25 +01:00
layerSurface->layer = WLRLAYERSURFACE->current.layer;
2022-03-21 15:17:04 +01:00
WLRLAYERSURFACE->data = layerSurface;
layerSurface->monitorID = PMONITOR->ID;
2022-07-06 22:12:03 +02:00
layerSurface->forceBlur = g_pConfigManager->shouldBlurLS(layerSurface->szNamespace);
2022-03-22 21:59:14 +01:00
Debug::log(LOG, "LayerSurface %x (namespace %s layer %d) created on monitor %s", layerSurface->layerSurface, layerSurface->layerSurface->_namespace, layerSurface->layer, PMONITOR->szName.c_str());
2022-03-21 15:17:04 +01:00
}
2022-03-28 22:31:39 +02:00
void Events::listener_destroyLayerSurface(void* owner, void* data) {
SLayerSurface* layersurface = (SLayerSurface*)owner;
2022-03-21 15:17:04 +01:00
2022-03-24 21:34:24 +01:00
Debug::log(LOG, "LayerSurface %x destroyed", layersurface->layerSurface);
2022-05-30 17:11:35 +02:00
const auto PMONITOR = g_pCompositor->getMonitorFromID(layersurface->monitorID);
if (!g_pCompositor->getMonitorFromID(layersurface->monitorID))
Debug::log(WARN, "Layersurface destroyed on an invalid monitor (removed?)");
if (!layersurface->fadingOut && PMONITOR) {
2022-05-17 18:37:14 +02:00
Debug::log(LOG, "Removing LayerSurface that wasn't mapped.");
layersurface->alpha.setValueAndWarp(0.f);
layersurface->fadingOut = true;
2022-05-15 11:25:42 +02:00
}
2022-03-28 22:31:39 +02:00
layersurface->hyprListener_commitLayerSurface.removeCallback();
layersurface->hyprListener_destroyLayerSurface.removeCallback();
layersurface->hyprListener_mapLayerSurface.removeCallback();
layersurface->hyprListener_unmapLayerSurface.removeCallback();
layersurface->hyprListener_newPopup.removeCallback();
2022-03-21 15:17:04 +01:00
// rearrange to fix the reserved areas
if (PMONITOR) {
g_pHyprRenderer->arrangeLayersForMonitor(PMONITOR->ID);
g_pLayoutManager->getCurrentLayout()->recalculateMonitor(PMONITOR->ID);
2022-04-24 12:04:16 +02:00
// and damage
wlr_box geomFixed = {layersurface->geometry.x + PMONITOR->vecPosition.x, layersurface->geometry.y + PMONITOR->vecPosition.y, layersurface->geometry.width, layersurface->geometry.height};
g_pHyprRenderer->damageBox(&geomFixed);
}
2022-05-14 11:10:50 +02:00
layersurface->readyToDelete = true;
layersurface->layerSurface = nullptr;
2022-03-21 15:17:04 +01:00
}
2022-03-28 22:31:39 +02:00
void Events::listener_mapLayerSurface(void* owner, void* data) {
SLayerSurface* layersurface = (SLayerSurface*)owner;
2022-03-21 15:17:04 +01:00
2022-03-24 21:34:24 +01:00
Debug::log(LOG, "LayerSurface %x mapped", layersurface->layerSurface);
layersurface->layerSurface->mapped = true;
2022-03-21 15:17:04 +01:00
wlr_surface_send_enter(layersurface->layerSurface->surface, layersurface->layerSurface->output);
// fix if it changed its mon
const auto PMONITOR = g_pCompositor->getMonitorFromOutput(layersurface->layerSurface->output);
2022-04-24 12:04:16 +02:00
if (!PMONITOR)
return;
2022-03-21 15:17:04 +01:00
if ((uint64_t)layersurface->monitorID != PMONITOR->ID) {
const auto POLDMON = g_pCompositor->getMonitorFromID(layersurface->monitorID);
POLDMON->m_aLayerSurfaceLists[layersurface->layer].remove(layersurface);
PMONITOR->m_aLayerSurfaceLists[layersurface->layer].push_back(layersurface);
layersurface->monitorID = PMONITOR->ID;
g_pLayoutManager->getCurrentLayout()->recalculateMonitor(POLDMON->ID);
g_pHyprRenderer->arrangeLayersForMonitor(POLDMON->ID);
}
g_pHyprRenderer->arrangeLayersForMonitor(PMONITOR->ID);
2022-07-11 22:51:20 +02:00
if (layersurface->layerSurface->current.keyboard_interactive && (!g_pCompositor->m_sSeat.mouse || !g_pCompositor->m_sSeat.mouse->currentConstraint)) // don't focus if constrained
2022-03-21 15:17:04 +01:00
g_pCompositor->focusSurface(layersurface->layerSurface->surface);
2022-03-27 17:25:20 +02:00
2022-07-08 13:19:57 +02:00
// mouse enter always, keeb only when needed
const auto LOCAL = g_pInputManager->getMouseCoordsInternal() - Vector2D(layersurface->geometry.x + PMONITOR->vecPosition.x, layersurface->geometry.y + PMONITOR->vecPosition.y);
wlr_seat_pointer_notify_enter(g_pCompositor->m_sSeat.seat, layersurface->layerSurface->surface, LOCAL.x, LOCAL.y);
wlr_seat_pointer_notify_motion(g_pCompositor->m_sSeat.seat, 0, LOCAL.x, LOCAL.y);
2022-03-27 17:25:20 +02:00
layersurface->position = Vector2D(layersurface->geometry.x, layersurface->geometry.y);
2022-04-24 12:04:16 +02:00
wlr_box geomFixed = {layersurface->geometry.x + PMONITOR->vecPosition.x, layersurface->geometry.y + PMONITOR->vecPosition.y, layersurface->geometry.width, layersurface->geometry.height};
g_pHyprRenderer->damageBox(&geomFixed);
layersurface->alpha.setValue(0);
layersurface->alpha = 255.f;
layersurface->readyToDelete = false;
layersurface->fadingOut = false;
2022-03-21 15:17:04 +01:00
}
2022-03-28 22:31:39 +02:00
void Events::listener_unmapLayerSurface(void* owner, void* data) {
SLayerSurface* layersurface = (SLayerSurface*)owner;
2022-03-21 15:17:04 +01:00
2022-03-24 21:34:24 +01:00
Debug::log(LOG, "LayerSurface %x unmapped", layersurface->layerSurface);
2022-05-30 17:11:35 +02:00
if (!g_pCompositor->getMonitorFromID(layersurface->monitorID)) {
Debug::log(WARN, "Layersurface unmapping on invalid monitor (removed?) ignoring.");
return;
}
// make a snapshot and start fade
g_pHyprOpenGL->makeLayerSnapshot(layersurface);
layersurface->alpha = 0.f;
layersurface->fadingOut = true;
2022-06-30 15:44:26 +02:00
g_pCompositor->m_vSurfacesFadingOut.push_back(layersurface);
2022-03-21 15:17:04 +01:00
if (layersurface->layerSurface->mapped)
layersurface->layerSurface->mapped = false;
2022-03-21 15:17:04 +01:00
2022-04-24 12:04:16 +02:00
const auto PMONITOR = g_pCompositor->getMonitorFromOutput(layersurface->layerSurface->output);
if (!PMONITOR)
return;
2022-07-08 13:19:57 +02:00
// refocus if needed
if (layersurface->layerSurface->surface == g_pCompositor->m_pLastFocus) {
g_pCompositor->m_pLastFocus = nullptr;
g_pInputManager->refocus();
}
2022-04-24 12:04:16 +02:00
wlr_box geomFixed = {layersurface->geometry.x + PMONITOR->vecPosition.x, layersurface->geometry.y + PMONITOR->vecPosition.y, layersurface->geometry.width, layersurface->geometry.height};
g_pHyprRenderer->damageBox(&geomFixed);
geomFixed = {layersurface->geometry.x + (int)PMONITOR->vecPosition.x, layersurface->geometry.y + (int)PMONITOR->vecPosition.y, (int)layersurface->layerSurface->surface->current.width, (int)layersurface->layerSurface->surface->current.height};
g_pHyprRenderer->damageBox(&geomFixed);
geomFixed = {layersurface->geometry.x, layersurface->geometry.y, (int)layersurface->layerSurface->surface->current.width, (int)layersurface->layerSurface->surface->current.height};
layersurface->geometry = geomFixed; // because the surface can overflow... for some reason?
2022-03-21 15:17:04 +01:00
}
2022-03-28 22:31:39 +02:00
void Events::listener_commitLayerSurface(void* owner, void* data) {
SLayerSurface* layersurface = (SLayerSurface*)owner;
2022-03-21 15:17:04 +01:00
if (!layersurface->layerSurface || !layersurface->layerSurface->output)
return;
const auto PMONITOR = g_pCompositor->getMonitorFromOutput(layersurface->layerSurface->output);
if (!PMONITOR)
return;
2022-07-01 22:49:12 +02:00
wlr_box geomFixed = {layersurface->geometry.x, layersurface->geometry.y, layersurface->geometry.width, layersurface->geometry.height};
2022-04-24 12:04:16 +02:00
g_pHyprRenderer->damageBox(&geomFixed);
2022-03-21 15:17:04 +01:00
// fix if it changed its mon
if ((uint64_t)layersurface->monitorID != PMONITOR->ID) {
const auto POLDMON = g_pCompositor->getMonitorFromID(layersurface->monitorID);
POLDMON->m_aLayerSurfaceLists[layersurface->layer].remove(layersurface);
PMONITOR->m_aLayerSurfaceLists[layersurface->layer].push_back(layersurface);
layersurface->monitorID = PMONITOR->ID;
g_pLayoutManager->getCurrentLayout()->recalculateMonitor(POLDMON->ID);
g_pHyprRenderer->arrangeLayersForMonitor(POLDMON->ID);
}
if (layersurface->layerSurface->current.committed != 0) {
g_pHyprRenderer->arrangeLayersForMonitor(PMONITOR->ID);
2022-03-21 15:17:04 +01:00
if (layersurface->layer != layersurface->layerSurface->current.layer) {
PMONITOR->m_aLayerSurfaceLists[layersurface->layer].remove(layersurface);
PMONITOR->m_aLayerSurfaceLists[layersurface->layerSurface->current.layer].push_back(layersurface);
layersurface->layer = layersurface->layerSurface->current.layer;
}
2022-03-21 15:17:04 +01:00
g_pLayoutManager->getCurrentLayout()->recalculateMonitor(PMONITOR->ID);
}
2022-03-21 15:17:04 +01:00
2022-03-27 17:25:20 +02:00
layersurface->position = Vector2D(layersurface->geometry.x, layersurface->geometry.y);
2022-04-14 20:22:14 +02:00
// update geom if it changed
layersurface->geometry = {layersurface->geometry.x, layersurface->geometry.y, layersurface->layerSurface->surface->current.width, layersurface->layerSurface->surface->current.height};
g_pHyprRenderer->damageSurface(layersurface->layerSurface->surface, layersurface->position.x, layersurface->position.y);
2022-03-21 15:17:04 +01:00
}