aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/events/Layers.cpp
blob: 773ba9094bc4a5519e4e9265ecbeeba90c48bca6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "../Compositor.hpp"
#include "../helpers/WLClasses.hpp"
#include "../managers/input/InputManager.hpp"
#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;
        }

        Debug::log(LOG, "New LayerSurface has no preferred monitor. Assigning Monitor %s", PMONITOR->szName.c_str());

        WLRLAYERSURFACE->output = PMONITOR->output;
    }

    const auto PMONITOR = (SMonitor*)g_pCompositor->getMonitorFromOutput(WLRLAYERSURFACE->output);
    PMONITOR->m_aLayerSurfaceLists[WLRLAYERSURFACE->pending.layer].push_back(new SLayerSurface());
    SLayerSurface* layerSurface = PMONITOR->m_aLayerSurfaceLists[WLRLAYERSURFACE->pending.layer].back();

    if (!WLRLAYERSURFACE->output) {
        WLRLAYERSURFACE->output = g_pCompositor->m_vMonitors.front()->output;  // TODO: current mon
    }

    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");

    layerSurface->layerSurface = WLRLAYERSURFACE;
    layerSurface->layer = WLRLAYERSURFACE->current.layer;
    WLRLAYERSURFACE->data = layerSurface;
    layerSurface->monitorID = PMONITOR->ID;

    Debug::log(LOG, "LayerSurface %x (namespace %s layer %d) created on monitor %s", layerSurface->layerSurface, layerSurface->layerSurface->_namespace, layerSurface->layer, PMONITOR->szName.c_str());
}

void Events::listener_destroyLayerSurface(void* owner, void* data) {
    SLayerSurface* layersurface = (SLayerSurface*)owner;

    Debug::log(LOG, "LayerSurface %x destroyed", layersurface->layerSurface);

    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) {
        Debug::log(LOG, "Removing LayerSurface that wasn't mapped.");
        layersurface->alpha.setValueAndWarp(0.f);
        layersurface->fadingOut = true;
    }

    layersurface->hyprListener_commitLayerSurface.removeCallback();
    layersurface->hyprListener_destroyLayerSurface.removeCallback();
    layersurface->hyprListener_mapLayerSurface.removeCallback();
    layersurface->hyprListener_unmapLayerSurface.removeCallback();
    layersurface->hyprListener_newPopup.removeCallback();

    // rearrange to fix the reserved areas
    if (PMONITOR) {
        g_pHyprRenderer->arrangeLayersForMonitor(PMONITOR->ID);
        g_pLayoutManager->getCurrentLayout()->recalculateMonitor(PMONITOR->ID);

        // 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);
    }

    layersurface->readyToDelete = true;
    layersurface->layerSurface = nullptr;
}

void Events::listener_mapLayerSurface(void* owner, void* data) {
    SLayerSurface* layersurface = (SLayerSurface*)owner;

    Debug::log(LOG, "LayerSurface %x mapped", layersurface->layerSurface);

    layersurface->layerSurface->mapped = true;

    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);

    if (!PMONITOR)
        return;

    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);

    if (layersurface->layerSurface->current.keyboard_interactive)
        g_pCompositor->focusSurface(layersurface->layerSurface->surface);

    layersurface->position = Vector2D(layersurface->geometry.x, layersurface->geometry.y);

    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;
}

void Events::listener_unmapLayerSurface(void* owner, void* data) {
    SLayerSurface* layersurface = (SLayerSurface*)owner;

    Debug::log(LOG, "LayerSurface %x unmapped", layersurface->layerSurface);

    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;

    g_pCompositor->m_vSurfacesFadingOut.push_back(layersurface);

    if (layersurface->layerSurface->mapped)
        layersurface->layerSurface->mapped = false;

    if (layersurface->layerSurface->surface == g_pCompositor->m_pLastFocus)
        g_pCompositor->m_pLastFocus = nullptr;

    const auto PMONITOR = g_pCompositor->getMonitorFromOutput(layersurface->layerSurface->output);

    if (!PMONITOR)
        return;

    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?
}

void Events::listener_commitLayerSurface(void* owner, void* data) {
    SLayerSurface* layersurface = (SLayerSurface*)owner;

    if (!layersurface->layerSurface || !layersurface->layerSurface->output)
        return;

    const auto PMONITOR = g_pCompositor->getMonitorFromOutput(layersurface->layerSurface->output);

    if (!PMONITOR)
        return;

    wlr_box geomFixed = {layersurface->geometry.x, layersurface->geometry.y, layersurface->geometry.width, layersurface->geometry.height};
    g_pHyprRenderer->damageBox(&geomFixed);

    // 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);

        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;
        }

        g_pLayoutManager->getCurrentLayout()->recalculateMonitor(PMONITOR->ID);
    }

    layersurface->position = Vector2D(layersurface->geometry.x, layersurface->geometry.y);

    g_pHyprRenderer->damageSurface(layersurface->layerSurface->surface, layersurface->position.x, layersurface->position.y);
}