-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
205 lines (170 loc) · 7.62 KB
/
main.cpp
File metadata and controls
205 lines (170 loc) · 7.62 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
#include <hyprland/src/Compositor.hpp>
#include <hyprland/src/SharedDefs.hpp>
#include <hyprland/src/debug/log/Logger.hpp>
#include <hyprland/src/desktop/DesktopTypes.hpp>
#include <hyprland/src/helpers/Monitor.hpp>
#include <hyprland/src/plugins/PluginAPI.hpp>
#include <hyprland/src/protocols/core/Compositor.hpp>
#include <hyprland/src/desktop/state/FocusState.hpp>
#include <hyprland/src/event/EventBus.hpp>
#include <array>
#include <format>
#include <hyprutils/string/ConstVarList.hpp>
#include <hyprland/src/desktop/view/Window.hpp>
#include "globals.hpp"
using namespace Hyprutils::String;
// Do NOT change this function.
APICALL EXPORT std::string PLUGIN_API_VERSION() {
return HYPRLAND_API_VERSION;
}
struct SAppConfig {
std::string szClass;
float sat;
int resX = -1;
int resY = -1;
float refreshRate = -1.0f;
};
std::vector<SAppConfig> g_appConfigs;
static const SAppConfig* getAppConfig(const std::string& appClass) {
for (const auto& ac : g_appConfigs) {
if (ac.szClass != appClass)
continue;
return ∾
}
return nullptr;
}
PHLMONITORREF g_activeMonitor;
float g_activeMonitorSat;
int g_activeResX = -1;
int g_activeResY = -1;
std::optional<SMonitorRule> g_originalMonitorRule;
// Evily stoled from libvibrant
const Mat3x3 calc_ctm_matrix(float sat) {
std::array<float, 9> mat;
float coeff = (1.0 - sat) / 3.0;
for (int i = 0; i < 9; i++) {
mat[i] = coeff + (i % 4 == 0 ? sat : 0);
}
return mat;
}
static std::string buildMonitorCommand(const std::string& name, int resX, int resY, float refreshRate, const Vector2D& offset, float scale) {
return std::format("{},{}x{}@{},{}x{},{}", name, resX, resY, refreshRate, (int)offset.x, (int)offset.y, scale);
}
void onActiveWindowChange(const PHLWINDOW win) {
if (win) {
Log::logger->log(Log::TRACE, "[hyprvibr] Active window change: {} ({})", win->m_title, win->m_initialClass);
} else {
Log::logger->log(Log::TRACE, "[hyprvibr] No active window");
}
const auto CONFIG = win ? getAppConfig(win->m_initialClass) : nullptr;
auto prevMon = g_activeMonitor.lock();
PHLMONITOR newMon;
float newSat;
int newResX = -1;
int newResY = -1;
if (CONFIG == nullptr) {
g_activeMonitor = {};
newMon = {};
newSat = 0;
} else {
g_activeMonitor = win->m_monitor;
newMon = win->m_monitor.lock();
newSat = CONFIG->sat;
newResX = CONFIG->resX;
newResY = CONFIG->resY;
}
bool settingsChanged = prevMon != newMon || newSat != g_activeMonitorSat || newResX != g_activeResX || newResY != g_activeResY;
if (settingsChanged) {
if (prevMon && prevMon != newMon) {
prevMon->setCTM(Mat3x3::identity());
if (g_originalMonitorRule.has_value()) {
auto cmd = buildMonitorCommand(prevMon->m_name, (int)g_originalMonitorRule->resolution.x, (int)g_originalMonitorRule->resolution.y,
g_originalMonitorRule->refreshRate, g_originalMonitorRule->offset, g_originalMonitorRule->scale);
HyprlandAPI::invokeHyprctlCommand("keyword", "monitor " + cmd);
Log::logger->log(Log::INFO, "[hyprvibr] Restored monitor {}", prevMon->m_name);
g_originalMonitorRule.reset();
}
}
if (newMon) {
if (newSat != g_activeMonitorSat) {
newMon->setCTM(calc_ctm_matrix(CONFIG->sat));
}
if (CONFIG->resX > 0 && CONFIG->resY > 0) {
auto currentResX = (int)newMon->m_pixelSize.x;
auto currentResY = (int)newMon->m_pixelSize.y;
if (!g_originalMonitorRule.has_value()) {
g_originalMonitorRule = newMon->m_activeMonitorRule;
}
if (currentResX != CONFIG->resX || currentResY != CONFIG->resY) {
float refreshRate = CONFIG->refreshRate > 0 ? CONFIG->refreshRate : 60.0f;
auto cmd = buildMonitorCommand(newMon->m_name, CONFIG->resX, CONFIG->resY, refreshRate,
newMon->m_activeMonitorRule.offset, newMon->m_activeMonitorRule.scale);
HyprlandAPI::invokeHyprctlCommand("keyword", "monitor " + cmd);
Log::logger->log(Log::INFO, "[hyprvibr] Changed resolution to {}x{}@{} on {}", CONFIG->resX, CONFIG->resY, refreshRate, newMon->m_name);
}
} else if (g_activeResX > 0 && g_activeResY > 0 && g_originalMonitorRule.has_value()) {
auto cmd = buildMonitorCommand(newMon->m_name, (int)g_originalMonitorRule->resolution.x, (int)g_originalMonitorRule->resolution.y,
g_originalMonitorRule->refreshRate, g_originalMonitorRule->offset, g_originalMonitorRule->scale);
HyprlandAPI::invokeHyprctlCommand("keyword", "monitor " + cmd);
Log::logger->log(Log::INFO, "[hyprvibr] Restored monitor {}", newMon->m_name);
g_originalMonitorRule.reset();
}
}
g_activeMonitorSat = newSat;
g_activeResX = newResX;
g_activeResY = newResY;
}
}
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, "[hyprvibr] 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("[hyprvibr] Version mismatch");
}
static auto P = Event::bus()->m_events.window.active.listen([](PHLWINDOW WIN, Desktop::eFocusReason reason) {
onActiveWindowChange(WIN);
});
static auto P2 = Event::bus()->m_events.config.preReload.listen([]() {
g_appConfigs.clear();
});
static auto P3 = Event::bus()->m_events.config.reloaded.listen([]() {
onActiveWindowChange(Desktop::focusState()->window());
});
HyprlandAPI::addConfigKeyword(
PHANDLE, "hyprvibr-app",
[](const char* l, const char* r) -> Hyprlang::CParseResult {
const std::string str = r;
CConstVarList data(str, 0, ',', true);
Hyprlang::CParseResult result;
if (data.size() < 2 || data.size() > 5) {
result.setError("hyprvibr-app requires 2-5 params: class,sat[,resX,resY[,refreshRate]]");
return result;
}
try {
SAppConfig config;
config.szClass = data[0];
config.sat = std::stof(std::string{data[1]});
if (data.size() >= 4) {
config.resX = std::stoi(std::string{data[2]});
config.resY = std::stoi(std::string{data[3]});
}
if (data.size() >= 5) {
config.refreshRate = std::stof(std::string{data[4]});
}
g_appConfigs.emplace_back(std::move(config));
} catch (std::exception& e) {
result.setError("failed to parse line");
return result;
}
return result;
},
Hyprlang::SHandlerOptions{});
HyprlandAPI::addNotification(PHANDLE, "Hyprvibr loaded", CHyprColor{0.2, 1.0, 0.2, 1.0}, 5000);
return {"hyprvibr", "A plugin to customize monitor saturation per focused window", "devcexx", "1.0"};
}
APICALL EXPORT void PLUGIN_EXIT() {
onActiveWindowChange({});
}