This repository was archived by the owner on Oct 24, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
129 lines (117 loc) · 5.82 KB
/
Copy pathmain.cpp
File metadata and controls
129 lines (117 loc) · 5.82 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
#include <hyprland/src/plugins/PluginAPI.hpp>
#include <hyprland/src/config/ConfigValue.hpp>
#include <hyprland/src/config/values/types/FloatValue.hpp>
#include <hyprland/src/managers/input/InputManager.hpp>
#include <hyprland/src/protocols/InputCapture.hpp>
#include <format>
#include <stdexcept>
#include "zoom.hpp"
namespace {
CHyprSignalListener axisListener;
SP<Config::Values::CFloatValue> sensitivity;
SP<Config::Values::CFloatValue> maximum;
SP<SHyprCtlCommand> diagnosticCommand;
uint64_t events = 0, consumed = 0;
uint32_t lastMods = 0;
double lastDelta = 0;
std::string lastReply;
std::string setZoom(double value) {
return HyprlandAPI::invokeHyprctlCommand("eval", std::format("hl.config({{cursor = {{zoom_factor = {:.6f}}}}})", value));
}
void processAxis(const IPointer::SAxisEvent& event, Event::SCallbackInfo& info, uint32_t mods) {
// Control alone; other modifier combinations pass through.
constexpr uint32_t CONTROL = 1U << 2;
if (info.cancelled || event.source != WL_POINTER_AXIS_SOURCE_FINGER ||
event.axis != WL_POINTER_AXIS_VERTICAL_SCROLL || !g_pInputManager ||
g_pInputManager->isLocked() || (PROTO::inputCapture && PROTO::inputCapture->isCaptured()) ||
mods != CONTROL)
return;
// Consume even zero-delta stop events and scroll at the zoom limits.
if (event.delta == 0) {
info.cancelled = true;
return;
}
if (!std::isfinite(event.delta))
return;
static const CConfigValue<Config::FLOAT> zoom("cursor:zoom_factor");
static const CConfigValue<Config::BOOL> naturalScroll("input:touchpad:natural_scroll");
const double current = *zoom;
const double delta = *naturalScroll ? -event.delta : event.delta;
const double next = ZoomScroll::nextZoom(current, delta, sensitivity->value(), maximum->value());
if (next != current) {
lastReply = setZoom(next);
// Do not swallow scrolling when the compositor rejects the update.
if (lastReply != "ok")
return;
}
info.cancelled = true;
}
void onAxis(const IPointer::SAxisEvent& event, Event::SCallbackInfo& info) {
++events;
lastMods = g_pInputManager ? g_pInputManager->getModsFromAllKBs() : 0;
lastDelta = event.delta;
processAxis(event, info, lastMods);
if (info.cancelled)
++consumed;
}
std::string diagnostic(eHyprCtlOutputFormat, std::string request) {
static const CConfigValue<Config::FLOAT> zoom("cursor:zoom_factor");
if (request.ends_with("selftest")) {
if (g_pInputManager->isLocked() || (PROTO::inputCapture && PROTO::inputCapture->isCaptured()))
return "selftest skipped: locked or captured";
const double original = *zoom;
if (setZoom(2) != "ok")
return "FAIL: unable to set initial zoom";
IPointer::SAxisEvent event;
event.source = WL_POINTER_AXIS_SOURCE_FINGER;
event.delta = -10;
Event::SCallbackInfo info;
processAxis(event, info, 4);
const double after = *zoom;
static const CConfigValue<Config::BOOL> naturalScroll("input:touchpad:natural_scroll");
const double expected = ZoomScroll::nextZoom(2, *naturalScroll ? 10 : -10, sensitivity->value(), maximum->value());
const bool changedCorrectly = info.cancelled && std::abs(after - expected) < 0.00001;
info.cancelled = false;
processAxis(event, info, 0);
const bool ordinaryPasses = !info.cancelled && *zoom == after;
info.cancelled = false;
processAxis(event, info, 64);
const bool commandPasses = !info.cancelled && *zoom == after;
info.cancelled = false;
event.source = WL_POINTER_AXIS_SOURCE_WHEEL;
processAxis(event, info, 4);
const bool wheelPasses = !info.cancelled && *zoom == after;
const auto restored = setZoom(original);
return std::format("{}: synthetic finger scroll 2 -> {:.6f}, ordinary={}, command={}, wheel={}, restore={}",
changedCorrectly && ordinaryPasses && commandPasses && wheelPasses && restored == "ok" ? "PASS" : "FAIL",
after, ordinaryPasses, commandPasses, wheelPasses, restored);
}
return std::format("events={} consumed={} last_mods={} last_delta={} zoom={} last_update_reply={}\nZoom modifier: Control=4 (alone). SUPER/Command=64 passes through. Self-test: hyprctl --batch 'zoomscroll selftest'",
events, consumed, lastMods, lastDelta, *zoom, lastReply);
}
}
APICALL EXPORT std::string PLUGIN_API_VERSION() {
return HYPRLAND_API_VERSION;
}
APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) {
if (std::string{__hyprland_api_get_hash()} != __hyprland_api_get_client_hash())
throw std::runtime_error("zoom-scroll: Hyprland ABI mismatch; rebuild against the running version");
sensitivity = makeShared<Config::Values::CFloatValue>(
"plugin:zoom_scroll:sensitivity", "Zoom speed per scroll unit; negative reverses direction", 0.01F,
Config::Values::SFloatValueOptions{.min = -0.1F, .max = 0.1F});
maximum = makeShared<Config::Values::CFloatValue>(
"plugin:zoom_scroll:max_zoom", "Maximum whole-screen zoom factor", 8.F,
Config::Values::SFloatValueOptions{.min = 1.F, .max = 20.F});
if (!HyprlandAPI::addConfigValueV2(handle, sensitivity) || !HyprlandAPI::addConfigValueV2(handle, maximum))
throw std::runtime_error("zoom-scroll: cannot register configuration");
axisListener = Event::bus()->m_events.input.mouse.axis.listen(onAxis);
diagnosticCommand = HyprlandAPI::registerHyprCtlCommand(handle, {"zoomscroll", false, diagnostic});
if (!diagnosticCommand)
throw std::runtime_error("zoom-scroll: cannot register diagnostics");
return {"zoom-scroll", "Control + two-finger scroll accessibility zoom", "local", "0.1.2"};
}
APICALL EXPORT void PLUGIN_EXIT() {
axisListener.reset();
sensitivity.reset();
maximum.reset();
}