Repository navigation
Expand file tree
/
Copy pathmouse_frontend.cpp
More file actions
167 lines (149 loc) · 5.14 KB
/
Copy pathmouse_frontend.cpp
File metadata and controls
167 lines (149 loc) · 5.14 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
/*
* Copyright (C) 2026 Shitty team
* MIT licensed
* See the file LICENSE.MIT for the full license.
*/
#include "mouse_frontend.h"
#include <algorithm>
#include <climits>
#include <cmath>
int mouseFramebufferCoordinate(double logical, double scale) {
if (!std::isfinite(logical) || !std::isfinite(scale)) {
return 0;
}
const double pixel = logical * std::max(1.0, scale);
return (int)(std::clamp(std::round(pixel), (double)(INT_MIN), (double)(INT_MAX)));
}
MouseProtocolPoint mouseProtocolPoint(MouseTrackingEnc encoding, int pixelX, int pixelY, const MouseGeometry& geometry) {
const int contentWidth = std::max(1, geometry.framebufferWidth - 2 * geometry.border);
const int contentHeight = std::max(1, geometry.framebufferHeight - 2 * geometry.border);
if (encoding == MouseTrackingEnc::SGRPixels) {
return {
std::clamp(pixelX - geometry.border + 1, 1, contentWidth),
std::clamp(pixelY - geometry.border + 1, 1, contentHeight),
};
}
const int columns = std::max(1, contentWidth / std::max(1, geometry.glyphWidth));
const int rows = std::max(1, contentHeight / std::max(1, geometry.glyphHeight));
return {
std::clamp((pixelX - geometry.border - 1) / std::max(1, geometry.glyphWidth) + 1, 1, columns),
std::clamp((pixelY - geometry.border - 1) / std::max(1, geometry.glyphHeight) + 1, 1, rows),
};
}
unsigned mouseProtocolModifiers(unsigned modifiers, bool reportAlt) {
unsigned result = 0;
if (modifiers & FrontendShift) {
result |= MouseShift;
}
if (reportAlt && (modifiers & FrontendAlt)) {
result |= MouseAlt;
}
if (modifiers & FrontendControl) {
result |= MouseControl;
}
return result;
}
int mouseTerminalButton(int button) {
switch (button) {
case 0:
return 1;
case 2:
return 2;
case 1:
return 3;
default:
return button >= 3 ? button - 3 + 8 : 0;
}
}
bool mouseButtonReportAllowed(MouseTrackingMode mode, MouseEventType type, int button) {
return button >= 1 && button <= 11 && !(type == MouseEventType::Release && button > 3) && mode != MouseTrackingMode::Disabled && !(type == MouseEventType::Release && mode == MouseTrackingMode::X10_Compat);
}
int MouseWheelAccumulator::consumeAxis(double delta, double& remainder) {
if (!std::isfinite(delta)) {
remainder = 0.0;
return 0;
}
const double total = remainder + std::clamp(delta, -100.0, 100.0);
const int steps = (int)(std::trunc(total));
remainder = total - steps;
return steps;
}
MouseWheelSteps MouseWheelAccumulator::consume(double x, double y, bool reporting) {
if (reporting != reporting_) {
reporting_ = reporting;
remainderX_ = 0.0;
remainderY_ = 0.0;
}
MouseWheelSteps steps;
steps.y = consumeAxis(y, remainderY_);
if (reporting) {
steps.x = consumeAxis(x, remainderX_);
} else {
remainderX_ = 0.0;
}
return steps;
}
void MouseWheelAccumulator::reset() {
reporting_ = false;
remainderX_ = 0.0;
remainderY_ = 0.0;
}
bool MouseFrontendState::protocolActive(unsigned modifiers, MouseTrackingMode mode) const {
return !selectionOngoing_ && !(modifiers & FrontendShift) && mode != MouseTrackingMode::Disabled;
}
void MouseFrontendState::updateButton(int button, bool pressed) {
if (button < 0 || button >= (int)(sizeof(buttons_) * CHAR_BIT)) {
return;
}
const unsigned mask = 1u << button;
if (pressed) {
buttons_ |= mask;
} else {
buttons_ &= ~mask;
}
}
int MouseFrontendState::motionButton() const {
if (buttons_ & (1u << 0)) {
return 1;
}
if (buttons_ & (1u << 2)) {
return 2;
}
if (buttons_ & (1u << 1)) {
return 3;
}
return 0;
}
bool MouseFrontendState::primaryButtonPressed() const {
return buttons_ & ((1u << 0) | (1u << 1) | (1u << 2));
}
int MouseFrontendState::registerClick(int button, double x, double y, double time) {
const double elapsed = time - lastClickTime_;
const bool repeated = button == lastButton_ && elapsed >= 0.0 && elapsed <= 0.5 && std::abs(x - lastClickX_) <= 4.0 && std::abs(y - lastClickY_) <= 4.0;
clickCount_ = repeated ? clickCount_ + 1 : 1;
lastButton_ = button;
lastClickTime_ = time;
lastClickX_ = x;
lastClickY_ = y;
return clickCount_;
}
bool MouseFrontendState::reportMotion(int column, int row, MouseTrackingMode mode, MouseTrackingEnc encoding, u32 generation) {
if (!hasMotionContext_ || mode != lastMotionMode_ || encoding != lastMotionEncoding_ || generation != lastMotionGeneration_) {
hasMotionContext_ = true;
lastMotionMode_ = mode;
lastMotionEncoding_ = encoding;
lastMotionGeneration_ = generation;
hasReportedMotion_ = false;
}
if (hasReportedMotion_ && column == lastReportColumn_ && row == lastReportRow_) {
return false;
}
hasReportedMotion_ = true;
lastReportColumn_ = column;
lastReportRow_ = row;
return true;
}
void MouseFrontendState::resetMotion() {
hasReportedMotion_ = false;
hasMotionContext_ = false;
}