-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathConfig.cpp
More file actions
417 lines (325 loc) · 16.5 KB
/
Copy pathConfig.cpp
File metadata and controls
417 lines (325 loc) · 16.5 KB
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "Config.hpp"
#include <algorithm>
#include <config/shared/complex/ComplexDataType.hpp>
#include <config/shared/complex/ComplexDataTypes.hpp>
#include <hyprland/src/config/values/types/BoolValue.hpp>
#include <hyprland/src/config/values/types/ColorValue.hpp>
#include <hyprland/src/config/values/types/FloatValue.hpp>
#include <hyprland/src/config/values/types/IntValue.hpp>
#include <hyprland/src/config/values/types/StringValue.hpp>
#include <hyprland/src/config/values/types/GradientValue.hpp>
#include <hyprland/src/managers/KeybindManager.hpp>
#include <regex>
extern "C" {
#include <lauxlib.h>
#include <lua.h>
}
namespace {
ScrollOverview::Config::TGestureRegistrar g_gestureRegistrar = nullptr;
int dispatcherFactoryLua(lua_State* L, std::string_view name);
using TArgValidator = bool (*)(std::string_view);
struct SDispatcher {
std::string_view name;
std::regex argPattern;
std::string_view defaultArg;
std::string_view typeArgError;
std::string_view invalidArgError;
TArgValidator argValidator = nullptr;
ScrollOverview::Config::TDispatcher dispatcher = nullptr;
lua_CFunction luaFunction = nullptr;
bool isArgValid(const std::string_view arg) const {
if (argValidator)
return argValidator(arg);
return std::regex_match(arg.begin(), arg.end(), argPattern);
}
};
SDispatcher* findDispatcher(const std::string_view name) {
static SDispatcher registrations[] = {
{
.name = "overview",
.argPattern = std::regex{R"(^(select|(toggle|on|open|enable|off|close|disable)([ \t]+(all|[^ \t"\\]+))?)$)"},
.defaultArg = "toggle",
.typeArgError = "expected an optional string argument; did you forget quotes around it?",
.invalidArgError = "expected select or toggle/open/close [monitor|all] (aliases: on, enable, off, disable)",
.luaFunction = [](lua_State* L) { return dispatcherFactoryLua(L, "overview"); },
},
{
.name = "navigate",
.argPattern = std::regex{"^(left|right|up|down)$"},
.typeArgError = "expected a string argument",
.invalidArgError = "expected one of: left, right, up, down",
.luaFunction = [](lua_State* L) { return dispatcherFactoryLua(L, "navigate"); },
},
{
.name = "window",
.argPattern = std::regex{"^(select|close)$"},
.typeArgError = "expected a string argument",
.invalidArgError = "expected one of: select, close",
.luaFunction = [](lua_State* L) { return dispatcherFactoryLua(L, "window"); },
},
};
const auto MATCH = std::ranges::find_if(registrations, [name](const auto& registration) { return registration.name == name; });
return MATCH == std::end(registrations) ? nullptr : &*MATCH;
}
int runDispatcherNow(lua_State* L, const SDispatcher& dispatcher, const char* arg) {
if (!dispatcher.dispatcher)
return luaL_error(L, "%s: dispatcher is not registered", dispatcher.name.data());
const auto result = dispatcher.dispatcher(arg);
if (!result.success)
return luaL_error(L, "%s: %s", dispatcher.name.data(), result.error.c_str());
return 0;
}
void pushDispatcherBindAction(lua_State* L, const char* name, const char* arg) {
const std::string CODE = "return function() return hl.plugin.scrolloverview._dispatch(\"" + std::string{name} + "\", \"" + std::string{arg} + "\") end";
if (luaL_loadstring(L, CODE.c_str()) != LUA_OK)
lua_error(L);
if (lua_pcall(L, 0, 1, 0) != LUA_OK)
lua_error(L);
}
int runDispatcherActionLua(lua_State* L) {
if (lua_gettop(L) < 2 || lua_isnoneornil(L, 1) || lua_isnoneornil(L, 2))
return luaL_error(L, "_dispatch: expected dispatcher name and argument");
if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
return luaL_error(L, "_dispatch: expected string arguments");
const char* name = lua_tostring(L, 1);
const char* arg = lua_tostring(L, 2);
const auto DISPATCHER = findDispatcher(name);
if (!DISPATCHER)
return luaL_error(L, "_dispatch: unknown dispatcher '%s'", name);
if (!DISPATCHER->isArgValid(arg))
return luaL_error(L, "%s: invalid argument '%s', %s", name, arg, DISPATCHER->invalidArgError.data());
return runDispatcherNow(L, *DISPATCHER, arg);
}
int dispatcherFactoryLua(lua_State* L, std::string_view name) {
const auto DISPATCHER = findDispatcher(name);
if (!DISPATCHER)
return luaL_error(L, "%s: dispatcher metadata is not registered", name.data());
const char* arg = DISPATCHER->defaultArg.empty() ? nullptr : DISPATCHER->defaultArg.data();
if (!arg && (lua_gettop(L) < 1 || lua_isnoneornil(L, 1)))
return luaL_error(L, "%s: %s", DISPATCHER->name.data(), DISPATCHER->typeArgError.data());
if (lua_gettop(L) >= 1) {
if (lua_isnoneornil(L, 1))
return luaL_error(L, "%s: %s", DISPATCHER->name.data(), DISPATCHER->typeArgError.data());
if (!lua_isstring(L, 1))
return luaL_error(L, "%s: %s", DISPATCHER->name.data(), DISPATCHER->typeArgError.data());
arg = lua_tostring(L, 1);
}
if (!DISPATCHER->isArgValid(arg))
return luaL_error(L, "%s: invalid argument '%s', %s", DISPATCHER->name.data(), arg, DISPATCHER->invalidArgError.data());
if (g_pKeybindManager && g_pKeybindManager->m_currentKeybind && g_pKeybindManager->m_currentKeybind->handler == "__lua") {
return runDispatcherNow(L, *DISPATCHER, arg);
}
pushDispatcherBindAction(L, DISPATCHER->name.data(), arg);
return 1;
}
int configureLua(lua_State* L) {
if (!lua_istable(L, 1))
return luaL_error(L, "configure: expected a table");
const int CONFIG = lua_absindex(L, 1);
lua_getglobal(L, "hl");
if (!lua_istable(L, -1))
return luaL_error(L, "configure: global hl table is not available");
lua_getfield(L, -1, "config");
if (!lua_isfunction(L, -1)) {
lua_pop(L, 1);
return luaL_error(L, "configure: hl.config is not available");
}
lua_newtable(L);
lua_newtable(L);
lua_pushvalue(L, CONFIG);
lua_setfield(L, -2, "scrolloverview");
lua_setfield(L, -2, "plugin");
if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
const char* err = lua_tostring(L, -1);
lua_pop(L, 2);
return luaL_error(L, "configure: %s", err ? err : "hl.config failed");
}
lua_pop(L, 1);
return 0;
}
int gestureLua(lua_State* L) {
if (!g_gestureRegistrar)
return luaL_error(L, "gesture: registrar is not registered");
if (!lua_istable(L, 1))
return luaL_error(L, "gesture: expected a table, e.g. { fingers = 3, direction = \"up\" }");
lua_getfield(L, 1, "fingers");
if (!lua_isinteger(L, -1))
return luaL_error(L, "gesture: 'fingers' (integer) is required");
const size_t FINGERS = sc<size_t>(lua_tointeger(L, -1));
lua_pop(L, 1);
lua_getfield(L, 1, "direction");
if (!lua_isstring(L, -1))
return luaL_error(L, "gesture: 'direction' (string) is required");
const std::string DIRECTION = lua_tostring(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "action");
const std::string ACTION = lua_isstring(L, -1) ? lua_tostring(L, -1) : "overview";
lua_pop(L, 1);
// accept either "mods" or "mod"
lua_getfield(L, 1, "mods");
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_getfield(L, 1, "mod");
}
const std::string MODS = lua_isstring(L, -1) ? lua_tostring(L, -1) : "";
lua_pop(L, 1);
lua_getfield(L, 1, "scale");
const float SCALE = lua_isnumber(L, -1) ? sc<float>(lua_tonumber(L, -1)) : 1.F;
lua_pop(L, 1);
lua_getfield(L, 1, "disable_inhibit");
const bool DISABLE_INHIBIT = lua_toboolean(L, -1);
lua_pop(L, 1);
const auto result = g_gestureRegistrar(FINGERS, DIRECTION, ACTION, MODS, SCALE, DISABLE_INHIBIT);
if (!result.success)
return luaL_error(L, "gesture: %s", result.error.c_str());
return 0;
}
}
namespace ScrollOverview::Config {
void registerDispatcher(const std::string& name, TDispatcher dispatcher) {
HyprlandAPI::addDispatcherV2(SCROLLOVERVIEW_HANDLE, "scrolloverview:" + name, dispatcher);
if (::Config::mgr()->type() != ::Config::CONFIG_LUA)
return;
const auto DISPATCHER = findDispatcher(name);
if (!DISPATCHER)
return;
DISPATCHER->dispatcher = dispatcher;
HyprlandAPI::addLuaFunction(SCROLLOVERVIEW_HANDLE, "scrolloverview", std::string{DISPATCHER->name}, DISPATCHER->luaFunction);
}
void registerGesture(TGestureRegistrar gestureRegistrar, TGestureKeyword gestureKeyword) {
HyprlandAPI::addConfigKeyword(SCROLLOVERVIEW_HANDLE, "scrolloverview-gesture", gestureKeyword, {});
if (::Config::mgr()->type() != ::Config::CONFIG_LUA)
return;
g_gestureRegistrar = gestureRegistrar;
HyprlandAPI::addLuaFunction(SCROLLOVERVIEW_HANDLE, "scrolloverview", "gesture", ::gestureLua);
}
static void registerLuaFunctions() {
if (::Config::mgr()->type() != ::Config::CONFIG_LUA)
return;
HyprlandAPI::addLuaFunction(SCROLLOVERVIEW_HANDLE, "scrolloverview", "_dispatch", ::runDispatcherActionLua);
HyprlandAPI::addLuaFunction(SCROLLOVERVIEW_HANDLE, "scrolloverview", "configure", ::configureLua);
}
static void registerConfigValues() {
using namespace ::Config::Values;
HyprlandAPI::addConfigValueV2(SCROLLOVERVIEW_HANDLE,
makeShared<CIntValue>("plugin:scrolloverview:gesture_distance", "gesture distance in pixels", 200, SIntValueOptions{.min = 1}));
HyprlandAPI::addConfigValueV2(SCROLLOVERVIEW_HANDLE,
makeShared<CFloatValue>("plugin:scrolloverview:scale", "overview scale", 0.5F, SFloatValueOptions{.min = 0.1F, .max = 0.9F}));
HyprlandAPI::addConfigValueV2(SCROLLOVERVIEW_HANDLE,
makeShared<CIntValue>("plugin:scrolloverview:workspace_gap", "gap between overview workspaces", 0, SIntValueOptions{.min = 0}));
HyprlandAPI::addConfigValueV2(SCROLLOVERVIEW_HANDLE,
makeShared<CStringValue>("plugin:scrolloverview:layout", "overview layout", Hyprlang::STRING{"vertical"}));
HyprlandAPI::addConfigValueV2(SCROLLOVERVIEW_HANDLE,
makeShared<CIntValue>("plugin:scrolloverview:input:scroll_event_delay", "minimum delay (ms) between discrete scroll steps (wheel workspace nav and trackpad focus stepping)", 200, SIntValueOptions{.min = 0}));
HyprlandAPI::addConfigValueV2(SCROLLOVERVIEW_HANDLE,
makeShared<CFloatValue>("plugin:scrolloverview:input:touchpad_scroll_factor", "overview touchpad scroll factor", 1.F,
SFloatValueOptions{.min = 0.F}));
HyprlandAPI::addConfigValueV2(SCROLLOVERVIEW_HANDLE,
makeShared<CIntValue>("plugin:scrolloverview:input:left_handed", "overview left handed mouse buttons, 2 follows input:left_handed", 2,
SIntValueOptions{.min = 0, .max = 2}));
HyprlandAPI::addConfigValueV2(SCROLLOVERVIEW_HANDLE,
makeShared<CIntValue>("plugin:scrolloverview:input:scrolling_mode", "overview mouse wheel behavior", 0,
SIntValueOptions{.min = 0, .max = 3}));
HyprlandAPI::addConfigValueV2(SCROLLOVERVIEW_HANDLE,
makeShared<CIntValue>("plugin:scrolloverview:input:drag_mode", "overview mouse drag behavior", 0,
SIntValueOptions{.min = 0, .max = 1}));
HyprlandAPI::addConfigValueV2(SCROLLOVERVIEW_HANDLE,
makeShared<CIntValue>("plugin:scrolloverview:input:drag_threshold", "overview drag threshold", 10,
SIntValueOptions{.min = 0}));
HyprlandAPI::addConfigValueV2(SCROLLOVERVIEW_HANDLE,
makeShared<CIntValue>("plugin:scrolloverview:wallpaper", "wallpaper mode", 0, SIntValueOptions{.min = 0, .max = 2}));
HyprlandAPI::addConfigValueV2(SCROLLOVERVIEW_HANDLE, makeShared<CBoolValue>("plugin:scrolloverview:blur", "blur the overview wallpaper", false));
HyprlandAPI::addConfigValueV2(SCROLLOVERVIEW_HANDLE,
makeShared<CBoolValue>("plugin:scrolloverview:shadow:enabled", "draw a shadow around each workspace card", false));
HyprlandAPI::addConfigValueV2(SCROLLOVERVIEW_HANDLE,
makeShared<CIntValue>("plugin:scrolloverview:shadow:range", "workspace card shadow range", -1));
HyprlandAPI::addConfigValueV2(SCROLLOVERVIEW_HANDLE,
makeShared<CIntValue>("plugin:scrolloverview:shadow:render_power", "workspace card shadow render power", -1));
HyprlandAPI::addConfigValueV2(SCROLLOVERVIEW_HANDLE,
makeShared<CGradientValue>("plugin:scrolloverview:shadow:color", "workspace card shadow color", -1));
}
void registerConfig() {
registerLuaFunctions();
registerConfigValues();
HyprlandAPI::reloadConfig();
}
int getGestureDistance() {
return std::max<int>(1, getValue<int>("plugin:scrolloverview:gesture_distance"));
}
float getScale() {
return std::clamp(getValue<float>("plugin:scrolloverview:scale"), 0.1F, 0.9F);
}
int getWorkspaceGap() {
return std::max<int>(0, getValue<int>("plugin:scrolloverview:workspace_gap"));
}
ELayout getLayout() {
const auto LAYOUT = getValue<std::string>("plugin:scrolloverview:layout");
return LAYOUT == "horizontal" ? ELayout::HORIZONTAL : ELayout::VERTICAL;
}
bool getLeftHanded() {
const auto LEFT_HANDED = getValue<int>("plugin:scrolloverview:input:left_handed");
if (LEFT_HANDED <= 1)
return LEFT_HANDED != 0;
return getValue<bool>("input:left_handed");
}
int getDragMode() {
return std::clamp(getValue<int>("plugin:scrolloverview:input:drag_mode"), 0, 1);
}
int getDragThreshold() {
return std::max<int>(0, getValue<int>("plugin:scrolloverview:input:drag_threshold"));
}
float getTouchpadScrollFactor() {
static constexpr float OVERVIEWTOUCHPADSCROLLFACTOR = 1.5F;
return OVERVIEWTOUCHPADSCROLLFACTOR * std::max<float>(0.F, getValue<float>("input:touchpad:scroll_factor")) *
std::max<float>(0.F, getValue<float>("plugin:scrolloverview:input:touchpad_scroll_factor"));
}
static EScrollAction defaultVerticalScrollAction(ELayout layout) {
return layout == ELayout::HORIZONTAL ? EScrollAction::COLUMN : EScrollAction::WORKSPACE;
}
EScrollAction getVerticalScrollAction(ELayout layout) {
const auto MODE = std::clamp(getValue<int>("plugin:scrolloverview:input:scrolling_mode"), 0, 3);
switch (MODE) {
case 1: return defaultVerticalScrollAction(layout) == EScrollAction::WORKSPACE ? EScrollAction::COLUMN : EScrollAction::WORKSPACE;
case 2: return EScrollAction::WORKSPACE;
case 3: return EScrollAction::COLUMN;
case 0:
default: return defaultVerticalScrollAction(layout);
}
}
EScrollAction getHorizontalScrollAction(ELayout layout) {
return getVerticalScrollAction(layout) == EScrollAction::WORKSPACE ? EScrollAction::COLUMN : EScrollAction::WORKSPACE;
}
int getScrollEventDelay() {
return std::max<int>(0, getValue<int>("plugin:scrolloverview:input:scroll_event_delay"));
}
int getWallpaperMode() {
return std::clamp<int>(getValue<int>("plugin:scrolloverview:wallpaper"), 0, 2);
}
bool getBlur() {
return getValue<bool>("plugin:scrolloverview:blur");
}
::Config::CCssGapData getCssGapData(const std::string& name) {
auto& VALUE = valueRef<::Config::IComplexConfigValue>(name);
if (!VALUE.good())
return {};
auto* const GAPS = dc<::Config::CCssGapData*>(VALUE.ptr());
if (!GAPS)
return {};
return *GAPS;
}
int getShadowEnabled() {
return getValue<bool>("plugin:scrolloverview:shadow:enabled") ? 1 : 0;
}
int getShadowRange() {
return getValue<int>("plugin:scrolloverview:shadow:range");
}
int getShadowRenderPower() {
return getValue<int>("plugin:scrolloverview:shadow:render_power");
}
std::optional<::Config::CGradientValueData> getShadowColor() {
constexpr auto NAME = "plugin:scrolloverview:shadow:color";
if (!::Config::mgr()->getConfigValue(NAME).setByUser)
return std::nullopt;
return getValue<::Config::CGradientValueData>(NAME);
}
}