summaryrefslogtreecommitdiff
path: root/src/events/Popups.cpp
blob: bcdb18083c2fc9a8f3e2f9a5891168e9e5374fc7 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "Events.hpp"

#include "../Compositor.hpp"
#include "../helpers/WLClasses.hpp"
#include "../managers/input/InputManager.hpp"
#include "../render/Renderer.hpp"

// --------------------------------------------- //
//   _____   ____  _____  _    _ _____   _____   //
//  |  __ \ / __ \|  __ \| |  | |  __ \ / ____|  //
//  | |__) | |  | | |__) | |  | | |__) | (___    //
//  |  ___/| |  | |  ___/| |  | |  ___/ \___ \   //
//  | |    | |__| | |    | |__| | |     ____) |  //
//  |_|     \____/|_|     \____/|_|    |_____/   //
//                                               //
// --------------------------------------------- //

void addPopupGlobalCoords(void* pPopup, int* x, int* y) {
    SXDGPopup* const PPOPUP = (SXDGPopup*)pPopup;

    auto             curPopup = PPOPUP;

    int              px = 0;
    int              py = 0;

    while (true) {
        px += curPopup->popup->current.geometry.x;
        py += curPopup->popup->current.geometry.y;

        if (curPopup == PPOPUP && PPOPUP->parentWindow) {
            px -= curPopup->popup->base->current.geometry.x;
            py -= curPopup->popup->base->current.geometry.y;
        }

        if (curPopup->popup && !curPopup->parentPopup && !curPopup->parentWindow) {
            const auto EXTENTSSURFACE = pixman_region32_extents(&curPopup->popup->base->surface->input_region);
            px -= EXTENTSSURFACE->x1;
            py -= EXTENTSSURFACE->y1;
        }

        if (curPopup->parentPopup) {
            curPopup = curPopup->parentPopup;
        } else {
            break;
        }
    }

    px += PPOPUP->lx;
    py += PPOPUP->ly;

    *x += px;
    *y += py;
}

void createNewPopup(wlr_xdg_popup* popup, SXDGPopup* pHyprPopup) {
    pHyprPopup->popup = popup;

    pHyprPopup->hyprListener_destroyPopupXDG.initCallback(&popup->base->events.destroy, &Events::listener_destroyPopupXDG, pHyprPopup, "HyprPopup");
    pHyprPopup->hyprListener_mapPopupXDG.initCallback(&popup->base->surface->events.map, &Events::listener_mapPopupXDG, pHyprPopup, "HyprPopup");
    pHyprPopup->hyprListener_unmapPopupXDG.initCallback(&popup->base->surface->events.unmap, &Events::listener_unmapPopupXDG, pHyprPopup, "HyprPopup");
    pHyprPopup->hyprListener_newPopupFromPopupXDG.initCallback(&popup->base->events.new_popup, &Events::listener_newPopupFromPopupXDG, pHyprPopup, "HyprPopup");
    pHyprPopup->hyprListener_commitPopupXDG.initCallback(&popup->base->surface->events.commit, &Events::listener_commitPopupXDG, pHyprPopup, "HyprPopup");
    pHyprPopup->hyprListener_repositionPopupXDG.initCallback(&popup->events.reposition, &Events::listener_repositionPopupXDG, pHyprPopup, "HyprPopup");

    const auto PMONITOR = g_pCompositor->m_pLastMonitor;

    CBox       box = {PMONITOR->vecPosition.x - pHyprPopup->lx, PMONITOR->vecPosition.y - pHyprPopup->ly, PMONITOR->vecSize.x, PMONITOR->vecSize.y};

    wlr_xdg_popup_unconstrain_from_box(popup, box.pWlr());

    pHyprPopup->monitor = PMONITOR;

    Debug::log(LOG, "Popup: Unconstrained from lx ly: {:j5}, pHyprPopup lx ly: {:.5f} {:.5f}", PMONITOR->vecPosition, (float)pHyprPopup->lx, (float)pHyprPopup->ly);
}

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

    ASSERT(layersurface);

    Debug::log(LOG, "New layer popup created from surface {:x}", (uintptr_t)layersurface);

    const auto WLRPOPUP = (wlr_xdg_popup*)data;

    const auto PNEWPOPUP = g_pCompositor->m_vXDGPopups.emplace_back(std::make_unique<SXDGPopup>()).get();

    const auto PMONITOR = g_pCompositor->getMonitorFromID(layersurface->monitorID);

    PNEWPOPUP->popup    = WLRPOPUP;
    PNEWPOPUP->lx       = layersurface->position.x;
    PNEWPOPUP->ly       = layersurface->position.y;
    PNEWPOPUP->monitor  = PMONITOR;
    PNEWPOPUP->parentLS = layersurface;
    createNewPopup(WLRPOPUP, PNEWPOPUP);
}

void Events::listener_newPopupXDG(void* owner, void* data) {
    CWindow* PWINDOW = (CWindow*)owner;

    ASSERT(PWINDOW);

    if (!PWINDOW->m_bIsMapped)
        return;

    Debug::log(LOG, "New layer popup created from XDG window {}", PWINDOW);

    const auto WLRPOPUP = (wlr_xdg_popup*)data;

    const auto PNEWPOPUP = g_pCompositor->m_vXDGPopups.emplace_back(std::make_unique<SXDGPopup>()).get();

    const auto PMONITOR = g_pCompositor->getMonitorFromID(PWINDOW->m_iMonitorID);

    PNEWPOPUP->popup        = WLRPOPUP;
    PNEWPOPUP->lx           = PWINDOW->m_vRealPosition.goalv().x;
    PNEWPOPUP->ly           = PWINDOW->m_vRealPosition.goalv().y;
    PNEWPOPUP->parentWindow = PWINDOW;
    PNEWPOPUP->monitor      = PMONITOR;
    createNewPopup(WLRPOPUP, PNEWPOPUP);
}

void Events::listener_newPopupFromPopupXDG(void* owner, void* data) {
    SXDGPopup* PPOPUP = (SXDGPopup*)owner;

    ASSERT(PPOPUP);

    if (PPOPUP->parentWindow)
        Debug::log(LOG, "New popup created from XDG Window popup {:x} -> {}", (uintptr_t)PPOPUP, PPOPUP->parentWindow);
    else
        Debug::log(LOG, "New popup created from Non-Window popup {:x}", (uintptr_t)PPOPUP);

    const auto WLRPOPUP = (wlr_xdg_popup*)data;

    const auto PNEWPOPUP = g_pCompositor->m_vXDGPopups.emplace_back(std::make_unique<SXDGPopup>()).get();

    PNEWPOPUP->popup        = WLRPOPUP;
    PNEWPOPUP->parentPopup  = PPOPUP;
    PNEWPOPUP->lx           = PPOPUP->lx;
    PNEWPOPUP->ly           = PPOPUP->ly;
    PNEWPOPUP->parentWindow = PPOPUP->parentWindow;
    PNEWPOPUP->monitor      = PPOPUP->monitor;

    createNewPopup(WLRPOPUP, PNEWPOPUP);
}

void Events::listener_mapPopupXDG(void* owner, void* data) {
    SXDGPopup* PPOPUP = (SXDGPopup*)owner;

    ASSERT(PPOPUP);

    Debug::log(LOG, "New XDG Popup mapped at {} {}", (int)PPOPUP->lx, (int)PPOPUP->ly);

    if (PPOPUP->parentWindow)
        PPOPUP->parentWindow->m_lPopupSurfaces.emplace_back(PPOPUP->popup->base->surface);
    else if (PPOPUP->parentLS)
        PPOPUP->parentLS->popupSurfaces.emplace_back(PPOPUP->popup->base->surface);

    PPOPUP->pSurfaceTree = SubsurfaceTree::createTreeRoot(PPOPUP->popup->base->surface, addPopupGlobalCoords, PPOPUP, PPOPUP->parentWindow);

    int lx = 0, ly = 0;
    addPopupGlobalCoords(PPOPUP, &lx, &ly);

    CBox extents;
    wlr_surface_get_extends(PPOPUP->popup->base->surface, extents.pWlr());
    extents.applyFromWlr();

    g_pHyprRenderer->damageBox(lx - extents.x, ly - extents.y, extents.width + 2, extents.height + 2);

    if (PPOPUP->monitor) {
        g_pCompositor->setPreferredScaleForSurface(PPOPUP->popup->base->surface, PPOPUP->monitor->scale);
        g_pCompositor->setPreferredTransformForSurface(PPOPUP->popup->base->surface, PPOPUP->monitor->transform);
    }

    Debug::log(LOG, "XDG Popup got assigned a surfaceTreeNode {:x}", (uintptr_t)PPOPUP->pSurfaceTree);
}

void Events::listener_repositionPopupXDG(void* owner, void* data) {
    SXDGPopup* PPOPUP = (SXDGPopup*)owner;

    Debug::log(LOG, "XDG Popup {:x} asks for a reposition", (uintptr_t)PPOPUP);

    int lx = 0, ly = 0;
    addPopupGlobalCoords(PPOPUP, &lx, &ly);

    CBox extents;
    wlr_surface_get_extends(PPOPUP->popup->base->surface, extents.pWlr());
    extents.applyFromWlr();

    PPOPUP->lastPos             = {lx - extents.x, ly - extents.y};
    PPOPUP->repositionRequested = true;

    const auto PMONITOR = g_pCompositor->m_pLastMonitor;

    CBox       box = {PMONITOR->vecPosition.x - lx + PPOPUP->popup->current.geometry.x, PMONITOR->vecPosition.y - ly + PPOPUP->popup->current.geometry.y, PMONITOR->vecSize.x,
                      PMONITOR->vecSize.y};
    wlr_xdg_popup_unconstrain_from_box(PPOPUP->popup, box.pWlr());
}

void Events::listener_unmapPopupXDG(void* owner, void* data) {
    SXDGPopup* PPOPUP = (SXDGPopup*)owner;
    Debug::log(LOG, "XDG Popup unmapped");

    ASSERT(PPOPUP);

    if (PPOPUP->popup->base->surface == g_pCompositor->m_pLastFocus)
        g_pInputManager->releaseAllMouseButtons();

    SubsurfaceTree::destroySurfaceTree(PPOPUP->pSurfaceTree);

    int lx = 0, ly = 0;
    addPopupGlobalCoords(PPOPUP, &lx, &ly);

    CBox extents;
    wlr_surface_get_extends(PPOPUP->popup->base->surface, extents.pWlr());
    extents.applyFromWlr();

    g_pHyprRenderer->damageBox(lx - extents.x, ly - extents.y, extents.width + 2, extents.height + 2);

    if (PPOPUP->parentWindow)
        std::erase(PPOPUP->parentWindow->m_lPopupSurfaces, PPOPUP->popup->base->surface);
    else if (PPOPUP->parentLS)
        std::erase(PPOPUP->parentLS->popupSurfaces, PPOPUP->popup->base->surface);

    PPOPUP->pSurfaceTree = nullptr;

    g_pInputManager->simulateMouseMovement(); // to focus and return back to an appropriate surface
}

void Events::listener_commitPopupXDG(void* owner, void* data) {
    SXDGPopup* PPOPUP = (SXDGPopup*)owner;

    if (g_pCompositor->windowValidMapped(PPOPUP->parentWindow)) {
        PPOPUP->lx = PPOPUP->parentWindow->m_vRealPosition.vec().x;
        PPOPUP->ly = PPOPUP->parentWindow->m_vRealPosition.vec().y;
    }

    int lx = 0, ly = 0;
    addPopupGlobalCoords(PPOPUP, &lx, &ly);

    CBox extents;
    wlr_surface_get_extends(PPOPUP->popup->base->surface, extents.pWlr());
    extents.applyFromWlr();

    if (PPOPUP->repositionRequested)
        g_pHyprRenderer->damageBox(PPOPUP->lastPos.x, PPOPUP->lastPos.y, extents.width + 2, extents.height + 2);

    PPOPUP->repositionRequested = false;

    g_pHyprRenderer->damageSurface(PPOPUP->popup->base->surface, lx, ly);
}

void Events::listener_destroyPopupXDG(void* owner, void* data) {
    SXDGPopup* PPOPUP = (SXDGPopup*)owner;

    ASSERT(PPOPUP);

    Debug::log(LOG, "Destroyed popup XDG {:x}", (uintptr_t)PPOPUP);

    if (PPOPUP->pSurfaceTree) {
        SubsurfaceTree::destroySurfaceTree(PPOPUP->pSurfaceTree);
        PPOPUP->pSurfaceTree = nullptr;
    }

    std::erase_if(g_pCompositor->m_vXDGPopups, [&](std::unique_ptr<SXDGPopup>& el) { return el.get() == PPOPUP; });
}