aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Window.hpp
blob: 7e4c73b8adf98ab595fdf49703d4faeecd3eb958 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#pragma once

#include "defines.hpp"
#include "helpers/SubsurfaceTree.hpp"
#include "helpers/AnimatedVariable.hpp"
#include "render/decorations/IHyprWindowDecoration.hpp"
#include <deque>
#include "config/ConfigDataValues.hpp"
#include "helpers/Vector2D.hpp"
#include "helpers/WLSurface.hpp"
#include "macros.hpp"
#include "managers/XWaylandManager.hpp"

enum eIdleInhibitMode {
    IDLEINHIBIT_NONE = 0,
    IDLEINHIBIT_ALWAYS,
    IDLEINHIBIT_FULLSCREEN,
    IDLEINHIBIT_FOCUS
};

template <typename T>
class CWindowOverridableVar {
  public:
    CWindowOverridableVar(T val) {
        value = val;
    }

    ~CWindowOverridableVar() = default;

    CWindowOverridableVar<T>& operator=(CWindowOverridableVar<T> other) {
        if (locked)
            return *this;

        locked = other.locked;
        value  = other.value;

        return *this;
    }

    T operator=(T& other) {
        if (locked)
            return value;
        value = other;
        return other;
    }

    void forceSetIgnoreLocked(T val, bool lock = false) {
        value  = val;
        locked = lock;
    }

    T operator*(T& other) {
        return value * other;
    }

    T operator+(T& other) {
        return value + other;
    }

    bool operator==(T& other) {
        return other == value;
    }

    bool operator>=(T& other) {
        return value >= other;
    }

    bool operator<=(T& other) {
        return value <= other;
    }

    bool operator>(T& other) {
        return value > other;
    }

    bool operator<(T& other) {
        return value < other;
    }

    explicit operator bool() {
        return static_cast<bool>(value);
    }

    T toUnderlying() {
        return value;
    }

    bool locked = false;

  private:
    T value;
};

struct SWindowSpecialRenderData {
    CWindowOverridableVar<bool>    alphaOverride         = false;
    CWindowOverridableVar<float>   alpha                 = 1.f;
    CWindowOverridableVar<bool>    alphaInactiveOverride = false;
    CWindowOverridableVar<float>   alphaInactive         = -1.f; // -1 means unset

    CWindowOverridableVar<int64_t> activeBorderColor   = -1; // -1 means unset
    CWindowOverridableVar<int64_t> inactiveBorderColor = -1; // -1 means unset

    // set by the layout
    CWindowOverridableVar<int> borderSize = -1; // -1 means unset
    bool                       rounding   = true;
    bool                       border     = true;
    bool                       decorate   = true;
    bool                       shadow     = true;
};

struct SWindowAdditionalConfigData {
    std::string                 animationStyle        = std::string("");
    CWindowOverridableVar<int>  rounding              = -1; // -1 means no
    CWindowOverridableVar<bool> forceNoBlur           = false;
    CWindowOverridableVar<bool> forceOpaque           = false;
    CWindowOverridableVar<bool> forceOpaqueOverridden = false; // if true, a rule will not change the forceOpaque state. This is for the force opaque dispatcher.
    CWindowOverridableVar<bool> forceAllowsInput      = false;
    CWindowOverridableVar<bool> forceNoAnims          = false;
    CWindowOverridableVar<bool> forceNoBorder         = false;
    CWindowOverridableVar<bool> forceNoShadow         = false;
    CWindowOverridableVar<bool> forceNoDim            = false;
    CWindowOverridableVar<bool> windowDanceCompat     = false;
    CWindowOverridableVar<bool> noMaxSize             = false;
    CWindowOverridableVar<bool> dimAround             = false;
    CWindowOverridableVar<bool> forceRGBX             = false;
    CWindowOverridableVar<bool> keepAspectRatio       = false;
    CWindowOverridableVar<int>  xray                  = -1; // -1 means unset, takes precedence over the renderdata one
    CWindowOverridableVar<int>  borderSize            = -1; // -1 means unset, takes precedence over the renderdata one
};

struct SWindowRule {
    std::string szRule;
    std::string szValue;

    bool        v2 = false;
    std::string szTitle;
    std::string szClass;
    int         bX11        = -1; // -1 means "ANY"
    int         bFloating   = -1;
    int         bFullscreen = -1;
    int         bPinned     = -1;
    std::string szWorkspace = ""; // empty means any
};

class CWindow {
  public:
    CWindow();
    ~CWindow();

    DYNLISTENER(commitWindow);
    DYNLISTENER(mapWindow);
    DYNLISTENER(unmapWindow);
    DYNLISTENER(destroyWindow);
    DYNLISTENER(setTitleWindow);
    DYNLISTENER(setGeometryX11U);
    DYNLISTENER(fullscreenWindow);
    DYNLISTENER(newPopupXDG);
    DYNLISTENER(requestMove);
    DYNLISTENER(requestMinimize);
    DYNLISTENER(requestMaximize);
    DYNLISTENER(requestResize);
    DYNLISTENER(activateX11);
    DYNLISTENER(configureX11);
    DYNLISTENER(toplevelClose);
    DYNLISTENER(toplevelActivate);
    DYNLISTENER(toplevelFullscreen);
    DYNLISTENER(setOverrideRedirect);
    DYNLISTENER(associateX11);
    DYNLISTENER(dissociateX11);
    // DYNLISTENER(newSubsurfaceWindow);

    CWLSurface            m_pWLSurface;
    std::list<CWLSurface> m_lPopupSurfaces;

    union {
        wlr_xdg_surface*      xdg;
        wlr_xwayland_surface* xwayland;
    } m_uSurface;

    // this is the position and size of the "bounding box"
    Vector2D m_vPosition = Vector2D(0, 0);
    Vector2D m_vSize     = Vector2D(0, 0);

    // this is the real position and size used to draw the thing
    CAnimatedVariable m_vRealPosition;
    CAnimatedVariable m_vRealSize;

    // for not spamming the protocols
    Vector2D m_vReportedPosition;
    Vector2D m_vReportedSize;

    // for restoring floating statuses
    Vector2D m_vLastFloatingSize;
    Vector2D m_vLastFloatingPosition;

    // this is used for pseudotiling
    bool        m_bIsPseudotiled = false;
    Vector2D    m_vPseudoSize    = Vector2D(0, 0);

    bool        m_bFirstMap      = false; // for layouts
    bool        m_bIsFloating    = false;
    bool        m_bDraggingTiled = false; // for dragging around tiled windows
    bool        m_bIsFullscreen  = false;
    bool        m_bWasMaximized  = false;
    uint64_t    m_iMonitorID     = -1;
    std::string m_szTitle        = "";
    std::string m_szInitialTitle = "";
    std::string m_szInitialClass = "";
    int         m_iWorkspaceID   = -1;

    bool        m_bIsMapped = false;

    bool        m_bRequestsFloat = false;

    // This is for fullscreen apps
    bool m_bCreatedOverFullscreen = false;

    // XWayland stuff
    bool     m_bIsX11                = false;
    bool     m_bMappedX11            = false;
    CWindow* m_pX11Parent            = nullptr;
    uint64_t m_iX11Type              = 0;
    bool     m_bIsModal              = false;
    bool     m_bX11DoesntWantBorders = false;
    bool     m_bX11ShouldntFocus     = false;
    float    m_fX11SurfaceScaledBy   = 1.f;
    //

    // For nofocus
    bool m_bNoFocus        = false;
    bool m_bNoInitialFocus = false;

    // Fullscreen and Maximize
    bool              m_bWantsInitialFullscreen = false;
    bool              m_bNoFullscreenRequest    = false;
    bool              m_bNoMaximizeRequest      = false;

    SSurfaceTreeNode* m_pSurfaceTree = nullptr;

    // Animated border
    CGradientValueData m_cRealBorderColor         = {0};
    CGradientValueData m_cRealBorderColorPrevious = {0};
    CAnimatedVariable  m_fBorderFadeAnimationProgress;
    CAnimatedVariable  m_fBorderAngleAnimationProgress;

    // Fade in-out
    CAnimatedVariable        m_fAlpha;
    bool                     m_bFadingOut     = false;
    bool                     m_bReadyToDelete = false;
    Vector2D                 m_vOriginalClosedPos;  // these will be used for calculations later on in
    Vector2D                 m_vOriginalClosedSize; // drawing the closing animations
    SWindowDecorationExtents m_eOriginalClosedExtents;

    // For pinned (sticky) windows
    bool m_bPinned = false;

    // urgency hint
    bool m_bIsUrgent = false;

    // fakefullscreen
    bool m_bFakeFullscreenState = false;

    // for proper cycling. While cycling we can't just move the pointers, so we need to keep track of the last cycled window.
    CWindow* m_pLastCycledWindow = nullptr;

    // Foreign Toplevel proto
    wlr_foreign_toplevel_handle_v1* m_phForeignToplevel = nullptr;

    // Window decorations
    std::deque<std::unique_ptr<IHyprWindowDecoration>> m_dWindowDecorations;
    std::vector<IHyprWindowDecoration*>                m_vDecosToRemove;

    // Special render data, rules, etc
    SWindowSpecialRenderData    m_sSpecialRenderData;
    SWindowAdditionalConfigData m_sAdditionalConfigData;

    // for alpha
    CAnimatedVariable m_fActiveInactiveAlpha;

    // animated shadow color
    CAnimatedVariable m_cRealShadowColor;

    // animated tint
    CAnimatedVariable m_fDimPercent;

    // swallowing
    CWindow* m_pSwallowed = nullptr;

    // focus stuff
    bool m_bStayFocused = false;

    // for toplevel monitor events
    uint64_t m_iLastToplevelMonitorID = -1;
    uint64_t m_iLastSurfaceMonitorID  = -1;

    // for idle inhibiting windows
    eIdleInhibitMode m_eIdleInhibitMode = IDLEINHIBIT_NONE;

    // for groups
    struct SGroupData {
        CWindow* pNextWindow = nullptr; // nullptr means no grouping. Self means single group.
        bool     head        = false;
        bool     locked      = false;
    } m_sGroupData;

    // For the list lookup
    bool operator==(const CWindow& rhs) {
        return m_uSurface.xdg == rhs.m_uSurface.xdg && m_uSurface.xwayland == rhs.m_uSurface.xwayland && m_vPosition == rhs.m_vPosition && m_vSize == rhs.m_vSize &&
            m_bFadingOut == rhs.m_bFadingOut;
    }

    // methods
    wlr_box                  getFullWindowBoundingBox();
    SWindowDecorationExtents getFullWindowExtents();
    wlr_box                  getWindowInputBox();
    wlr_box                  getWindowIdealBoundingBoxIgnoreReserved();
    void                     updateWindowDecos();
    pid_t                    getPID();
    IHyprWindowDecoration*   getDecorationByType(eDecorationType);
    void                     removeDecorationByType(eDecorationType);
    void                     createToplevelHandle();
    void                     destroyToplevelHandle();
    void                     updateToplevel();
    void                     updateSurfaceOutputs();
    void                     moveToWorkspace(int);
    CWindow*                 X11TransientFor();
    void                     onUnmap();
    void                     onMap();
    void                     setHidden(bool hidden);
    bool                     isHidden();
    void                     applyDynamicRule(const SWindowRule& r);
    void                     updateDynamicRules();
    SWindowDecorationExtents getFullWindowReservedArea();
    Vector2D                 middle();
    bool                     opaque();
    float                    rounding();

    int                      getRealBorderSize();
    void                     updateSpecialRenderData();

    void                     onBorderAngleAnimEnd(void* ptr);
    bool                     isInCurvedCorner(double x, double y);
    bool                     hasPopupAt(const Vector2D& pos);

    CWindow*                 getGroupHead();
    CWindow*                 getGroupTail();
    CWindow*                 getGroupCurrent();
    CWindow*                 getGroupPrevious();
    CWindow*                 getGroupWindowByIndex(int);
    int                      getGroupSize();
    void                     setGroupCurrent(CWindow* pWindow);
    void                     insertWindowToGroup(CWindow* pWindow);
    void                     updateGroupOutputs();
    void                     switchWithWindowInGroup(CWindow* pWindow);

  private:
    // For hidden windows and stuff
    bool m_bHidden = false;
};

/**
    format specification
    - 'x', only address, equivalent of (uintpr_t)CWindow*
    - 'm', with monitor id
    - 'w', with workspace id
    - 'c', with application class
*/

template <typename CharT>
struct std::formatter<CWindow*, CharT> : std::formatter<CharT> {
    bool formatAddressOnly = false;
    bool formatWorkspace   = false;
    bool formatMonitor     = false;
    bool formatClass       = false;
    FORMAT_PARSE(                           //
        FORMAT_FLAG('x', formatAddressOnly) //
        FORMAT_FLAG('m', formatMonitor)     //
        FORMAT_FLAG('w', formatWorkspace)   //
        FORMAT_FLAG('c', formatClass),
        CWindow*)

    template <typename FormatContext>
    auto format(CWindow* const& w, FormatContext& ctx) const {
        auto&& out = ctx.out();
        if (formatAddressOnly)
            return std::format_to(out, "{:x}", (uintptr_t)w);
        if (!w)
            return std::format_to(out, "[Window nullptr]");

        std::format_to(out, "[");
        std::format_to(out, "Window {:x}: title: \"{}\"", (uintptr_t)w, w->m_szTitle);
        if (formatWorkspace)
            std::format_to(out, ", workspace: {}", w->m_iWorkspaceID);
        if (formatMonitor)
            std::format_to(out, ", monitor: {}", w->m_iMonitorID);
        if (formatClass)
            std::format_to(out, ", class: {}", g_pXWaylandManager->getAppIDClass(w));
        return std::format_to(out, "]");
    }
};