summaryrefslogtreecommitdiff
path: root/src/config/lua/objects/LuaWindow.cpp
blob: bf33258455963f046658ff107568728c8b5a8b21 (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
#include "LuaWindow.hpp"
#include "LuaWorkspace.hpp"
#include "LuaMonitor.hpp"
#include "LuaObjectHelpers.hpp"

#include "../../../desktop/view/Window.hpp"
#include "../../../desktop/view/Group.hpp"
#include "../../../desktop/Workspace.hpp"
#include "../../../desktop/state/FocusState.hpp"
#include "../../../desktop/history/WindowHistoryTracker.hpp"
#include "../../../layout/algorithm/Algorithm.hpp"
#include "../../../layout/algorithm/tiled/master/MasterAlgorithm.hpp"
#include "../../../layout/algorithm/tiled/scrolling/ScrollingAlgorithm.hpp"
#include "../../../layout/algorithm/tiled/dwindle/DwindleAlgorithm.hpp"
#include "../../../layout/space/Space.hpp"
#include "../../../layout/supplementary/WorkspaceAlgoMatcher.hpp"
#include "../../../managers/input/InputManager.hpp"

#include <format>
#include <string_view>

using namespace Config::Lua;

static constexpr const char* MT = "HL.Window";

//
static int getFocusHistoryID(PHLWINDOW wnd) {
    const auto& history = Desktop::History::windowTracker()->fullHistory();
    for (size_t i = 0; i < history.size(); ++i) {
        if (history[i].lock() == wnd)
            return sc<int>(history.size() - i - 1); // reverse order for backwards compat
    }

    return -1;
}

static int windowEq(lua_State* L) {
    const auto* lhs = sc<PHLWINDOWREF*>(luaL_checkudata(L, 1, MT));
    const auto* rhs = sc<PHLWINDOWREF*>(luaL_checkudata(L, 2, MT));

    lua_pushboolean(L, lhs->lock() == rhs->lock());
    return 1;
}

static int windowToString(lua_State* L) {
    const auto* ref = sc<PHLWINDOWREF*>(luaL_checkudata(L, 1, MT));
    const auto  w   = ref->lock();

    if (!w)
        lua_pushstring(L, "HL.Window(expired)");
    else
        lua_pushfstring(L, "HL.Window(%p)", w.get());

    return 1;
}

static int windowIndex(lua_State* L) {
    auto*      ref = sc<PHLWINDOWREF*>(luaL_checkudata(L, 1, MT));
    const auto w   = ref->lock();
    if (!w) {
        Log::logger->log(Log::DEBUG, "[lua] Tried to access an expired object");
        lua_pushnil(L);
        return 1;
    }

    const std::string_view key = luaL_checkstring(L, 2);

    if (key == "address")
        lua_pushstring(L, std::format("0x{:x}", reinterpret_cast<uintptr_t>(w.get())).c_str());
    else if (key == "mapped")
        lua_pushboolean(L, w->m_isMapped);
    else if (key == "hidden")
        lua_pushboolean(L, w->isHidden());
    else if (key == "at") {
        lua_newtable(L);
        lua_pushinteger(L, sc<int>(w->m_realPosition->goal().x));
        lua_setfield(L, -2, "x");
        lua_pushinteger(L, sc<int>(w->m_realPosition->goal().y));
        lua_setfield(L, -2, "y");
    } else if (key == "size") {
        lua_newtable(L);
        lua_pushinteger(L, sc<int>(w->m_realSize->goal().x));
        lua_setfield(L, -2, "x");
        lua_pushinteger(L, sc<int>(w->m_realSize->goal().y));
        lua_setfield(L, -2, "y");
    } else if (key == "workspace") {
        if (w->m_workspace)
            Objects::CLuaWorkspace::push(L, w->m_workspace);
        else
            lua_pushnil(L);
    } else if (key == "floating")
        lua_pushboolean(L, w->m_isFloating);
    else if (key == "monitor") {
        const auto mon = w->m_monitor.lock();
        if (mon)
            Objects::CLuaMonitor::push(L, mon);
        else
            lua_pushnil(L);
    } else if (key == "class")
        lua_pushstring(L, w->m_class.c_str());
    else if (key == "title")
        lua_pushstring(L, w->m_title.c_str());
    else if (key == "initial_class")
        lua_pushstring(L, w->m_initialClass.c_str());
    else if (key == "initial_title")
        lua_pushstring(L, w->m_initialTitle.c_str());
    else if (key == "pid")
        lua_pushinteger(L, sc<lua_Integer>(w->getPID()));
    else if (key == "xwayland")
        lua_pushboolean(L, w->m_isX11);
    else if (key == "pinned")
        lua_pushboolean(L, w->m_pinned);
    else if (key == "fullscreen")
        lua_pushinteger(L, sc<lua_Integer>(sc<uint8_t>(w->m_fullscreenState.internal)));
    else if (key == "fullscreen_client")
        lua_pushinteger(L, sc<lua_Integer>(sc<uint8_t>(w->m_fullscreenState.client)));
    else if (key == "over_fullscreen")
        lua_pushboolean(L, w->m_createdOverFullscreen);
    else if (key == "group") {
        if (!w->m_group) {
            lua_pushnil(L);
            return 1;
        }

        lua_newtable(L);

        lua_pushboolean(L, w->m_group->locked());
        lua_setfield(L, -2, "locked");

        lua_pushboolean(L, w->m_group->denied());
        lua_setfield(L, -2, "denied");

        lua_pushinteger(L, sc<lua_Integer>(w->m_group->size()));
        lua_setfield(L, -2, "size");

        lua_pushinteger(L, sc<lua_Integer>(w->m_group->getCurrentIdx()) + 1);
        lua_setfield(L, -2, "current_index");

        const auto current = w->m_group->current();
        if (current)
            Objects::CLuaWindow::push(L, current);
        else
            lua_pushnil(L);
        lua_setfield(L, -2, "current");

        lua_newtable(L);
        int i = 1;
        for (const auto& grouped : w->m_group->windows()) {
            const auto groupedWindow = grouped.lock();
            if (!groupedWindow)
                continue;

            Objects::CLuaWindow::push(L, groupedWindow);
            lua_rawseti(L, -2, i++);
        }
        lua_setfield(L, -2, "members");
    } else if (key == "tags") {
        lua_newtable(L);

        int i = 1;
        for (const auto& tag : w->m_ruleApplicator->m_tagKeeper.getTags()) {
            lua_pushstring(L, tag.c_str());
            lua_rawseti(L, -2, i++);
        }
    } else if (key == "swallowing") {
        const auto swallowed = w->m_swallowed.lock();
        if (swallowed)
            Objects::CLuaWindow::push(L, swallowed);
        else
            lua_pushnil(L);
    } else if (key == "focus_history_id")
        lua_pushinteger(L, sc<lua_Integer>(getFocusHistoryID(w)));
    else if (key == "inhibiting_idle")
        lua_pushboolean(L, g_pInputManager && g_pInputManager->isWindowInhibiting(w, false));
    else if (key == "xdg_tag") {
        const auto xdgTag = w->xdgTag();
        if (xdgTag)
            lua_pushstring(L, xdgTag->c_str());
        else
            lua_pushnil(L);
    } else if (key == "xdg_description") {
        const auto xdgDescription = w->xdgDescription();
        if (xdgDescription)
            lua_pushstring(L, xdgDescription->c_str());
        else
            lua_pushnil(L);
    } else if (key == "content_type")
        lua_pushstring(L, NContentType::toString(w->getContentType()).c_str());
    else if (key == "stable_id")
        lua_pushinteger(L, sc<lua_Integer>(w->m_stableID));
    else if (key == "layout") {
        const auto target = w->layoutTarget();
        if (!target || target->floating() || !w->m_workspace || !w->m_workspace->m_space) {
            lua_pushnil(L);
            return 1;
        }

        const auto& algo = w->m_workspace->m_space->algorithm();
        if (!algo || !algo->tiledAlgo()) {
            lua_pushnil(L);
            return 1;
        }

        const auto&       tiledAlgo = algo->tiledAlgo();
        const std::string name      = Layout::Supplementary::algoMatcher()->getNameForTiledAlgo(&typeid(*tiledAlgo.get()));

        lua_newtable(L);
        lua_pushstring(L, name.c_str());
        lua_setfield(L, -2, "name");

        if (const auto* master = dynamic_cast<Layout::Tiled::CMasterAlgorithm*>(tiledAlgo.get())) {
            const auto node = master->getNodeFromTarget(target);
            if (node) {
                lua_pushboolean(L, node->isMaster);
                lua_setfield(L, -2, "is_master");

                lua_pushnumber(L, node->percMaster);
                lua_setfield(L, -2, "perc_master");

                lua_pushnumber(L, node->percSize);
                lua_setfield(L, -2, "perc_size");
            }
        } else if (auto* scrolling = dynamic_cast<Layout::Tiled::CScrollingAlgorithm*>(tiledAlgo.get())) {
            const auto data = scrolling->dataFor(target);
            if (data) {
                const auto col = data->column.lock();
                if (col) {
                    const auto scrollingData = col->scrollingData.lock();

                    lua_newtable(L);

                    if (scrollingData) {
                        lua_pushinteger(L, sc<lua_Integer>(scrollingData->idx(col)));
                        lua_setfield(L, -2, "index");
                    }

                    lua_pushnumber(L, col->getColumnWidth());
                    lua_setfield(L, -2, "width");

                    lua_newtable(L);
                    int i = 1;
                    for (const auto& td : col->targetDatas) {
                        const auto t = td->target.lock();
                        if (t) {
                            const auto win = t->window();
                            if (win) {
                                Objects::CLuaWindow::push(L, win);
                                lua_rawseti(L, -2, i++);
                            }
                        }
                    }
                    lua_setfield(L, -2, "windows");

                    lua_setfield(L, -2, "column");

                    lua_pushinteger(L, sc<lua_Integer>(col->idx(target)));
                    lua_setfield(L, -2, "index_in_column");
                }
            }
        }
    } else if (key == "active") {
        lua_pushboolean(L, w == Desktop::focusState()->window());
    } else
        lua_pushnil(L);

    return 1;
}

void Objects::CLuaWindow::setup(lua_State* L) {
    registerMetatable(L, MT, windowIndex, gcRef<PHLWINDOWREF>, windowEq, windowToString);
}

void Objects::CLuaWindow::push(lua_State* L, PHLWINDOW w) {
    new (lua_newuserdata(L, sizeof(PHLWINDOWREF))) PHLWINDOWREF(w ? w->m_self : nullptr);
    luaL_getmetatable(L, MT);
    lua_setmetatable(L, -2);
}