-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
180 lines (143 loc) · 6.31 KB
/
main.cpp
File metadata and controls
180 lines (143 loc) · 6.31 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
#define WLR_USE_UNSTABLE
#include <hyprland/src/includes.hpp>
#include <hyprland/src/Compositor.hpp>
#include <hyprland/src/helpers/Monitor.hpp>
#include <hyprland/src/helpers/MiscFunctions.hpp>
#include <hyprland/src/managers/PointerManager.hpp>
#include <hyprland/src/managers/input/InputManager.hpp>
#include <hyprland/src/desktop/state/FocusState.hpp>
#include <hyprland/src/event/EventBus.hpp>
#include "globals.hpp"
Hyprlang::CParseResult handleEdgeEffect(const char* command, const char* value) {
Hyprlang::CParseResult result;
CVarList vars(value);
if (vars[0].empty() || vars[1].empty()) {
result.setError("requires edge and dispatcher");
return result;
}
eEdge edge;
if (vars[0] == "top")
edge = TOP;
else if (vars[0] == "bottom")
edge = BOTTOM;
else if (vars[0] == "left")
edge = LEFT;
else if (vars[0] == "right")
edge = RIGHT;
else if (vars[0] == "topleft")
edge = TOPLEFT;
else if (vars[0] == "topright")
edge = TOPRIGHT;
else if (vars[0] == "bottomleft")
edge = BOTTOMLEFT;
else if (vars[0] == "bottomright")
edge = BOTTOMRIGHT;
else {
result.setError(std::format("invalid edge {}", vars[0]).c_str());
return result;
}
const auto DISPATCHER = g_pKeybindManager->m_dispatchers.find(vars[1]);
if (DISPATCHER == g_pKeybindManager->m_dispatchers.end()) {
result.setError(std::format("invalid dispatcher {}", vars[1]).c_str());
return result;
}
g_pGlobalState->edgeEffects.emplace_back(edge, vars[1], vars[2]);
return result;
}
SDispatchResult moveCursorToEdge(std::string args) {
const auto PMONITOR = g_pCompositor->getMonitorFromCursor();
const auto pos = g_pPointerManager->position();
Vector2D warpTo;
if (args == "top")
warpTo = {pos.x, PMONITOR->m_position.y + 1};
else if (args == "bottom")
warpTo = {pos.x, PMONITOR->m_position.y + PMONITOR->m_size.y - 2};
else if (args == "left")
warpTo = {PMONITOR->m_position.x + 1, pos.y};
else if (args == "right")
warpTo = {PMONITOR->m_position.x + PMONITOR->m_size.x - 2, pos.y};
else
return {.success = false, .error = std::format("hypredge:movecursortoedge: invalid edge {}", args)};
g_pCompositor->warpCursorTo(warpTo, true);
return {};
}
std::optional<eEdge> getEdge(const Vector2D localPos, const Vector2D monitorSize) {
static auto* const PCORNERBARRIER = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hypredge:corner_barrier")->getDataStaticPtr();
const auto distToTop = localPos.y;
const auto distToBottom = (monitorSize - localPos).y - 1;
const auto distToLeft = localPos.x;
const auto distToRight = (monitorSize - localPos).x - 1;
const auto validEdgeX = distToRight > **PCORNERBARRIER && distToLeft > **PCORNERBARRIER;
const auto validEdgeY = distToTop > **PCORNERBARRIER && distToBottom > **PCORNERBARRIER;
if (!distToTop && !distToLeft)
return TOPLEFT;
else if (!distToTop && !distToRight)
return TOPRIGHT;
else if (!distToBottom && !distToLeft)
return BOTTOMLEFT;
else if (!distToBottom && !distToRight)
return BOTTOMRIGHT;
if (!distToTop && validEdgeX)
return TOP;
else if (!distToBottom && validEdgeX)
return BOTTOM;
else if (!distToLeft && validEdgeY)
return LEFT;
else if (!distToRight && validEdgeY)
return RIGHT;
return std::nullopt;
}
void onMouseMove(const Vector2D pos) {
auto monitor = g_pCompositor->getMonitorFromVector(pos);
auto window = Desktop::focusState()->window();
auto localPos = pos - monitor->m_position;
auto edge = getEdge(localPos, monitor->m_size);
if (!edge.has_value()) {
g_pGlobalState->alreadyActivated = std::nullopt;
return;
}
// If we've already activated from this edge,
// then don't do it again.
if (g_pGlobalState->alreadyActivated.has_value() && g_pGlobalState->alreadyActivated.value() == edge)
return;
g_pGlobalState->alreadyActivated = edge;
// If the mouse is constrained to a window and we don't have hypredge:ignore_constraints, don't activate.
if (
window
&& (!window->m_ruleApplicator->m_otherProps.props.contains(g_pGlobalState->ignoreConstraintRuleIdx)
|| window->m_ruleApplicator->m_otherProps.props[g_pGlobalState->ignoreConstraintRuleIdx]->effect != "on")
&& g_pInputManager->isConstrained()
)
return;
for (auto edgeEffect : g_pGlobalState->edgeEffects) {
if (edgeEffect.edge != edge.value())
continue;
g_pKeybindManager->m_dispatchers[edgeEffect.dispatcher](edgeEffect.arg);
}
}
static void onPreConfigReload() {
g_pGlobalState->edgeEffects.clear();
}
// Do NOT change this function.
APICALL EXPORT std::string PLUGIN_API_VERSION() {
return HYPRLAND_API_VERSION;
}
APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) {
PHANDLE = handle;
const std::string HASH = __hyprland_api_get_hash();
const std::string CLIENT_HASH = __hyprland_api_get_client_hash();
if (HASH != CLIENT_HASH) {
HyprlandAPI::addNotification(PHANDLE, "[hyprland] Failure in initialization: Version mismatch (headers ver is not equal to running hyprland ver)",
CHyprColor{1.0, 0.2, 0.2, 1.0}, 5000);
throw std::runtime_error("[hs] Version mismatch");
}
g_pGlobalState = makeUnique<SGlobalState>();
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hypredge:corner_barrier", Hyprlang::INT{100});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hypredge:respect_constraints", Hyprlang::INT{1});
HyprlandAPI::addConfigKeyword(PHANDLE, "edge_effect", handleEdgeEffect, {false});
HyprlandAPI::addDispatcherV2(PHANDLE, "hypredge:movecursortoedge", moveCursorToEdge);
static auto mouseMovePtr = Event::bus()->m_events.input.mouse.move.listen([&](Vector2D pos, Event::SCallbackInfo& info) { onMouseMove(pos); });
static auto clearConfigPtr = Event::bus()->m_events.config.preReload.listen([&] { onPreConfigReload(); });
g_pGlobalState->ignoreConstraintRuleIdx = Desktop::Rule::windowEffects()->registerEffect("hypredge:ignore_constraints");
return {"hypredge", "Trigger dispatchers on screen edges", "CyrenArkade", "0.1"};
}