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
|
#include "InputManager.hpp"
#include "../../Compositor.hpp"
#include "../../protocols/Tablet.hpp"
#include "../../devices/Tablet.hpp"
#include "../../managers/PointerManager.hpp"
#include "../../managers/SeatManager.hpp"
#include "../../protocols/PointerConstraints.hpp"
static void unfocusTool(SP<CTabletTool> tool) {
if (!tool->getSurface())
return;
tool->setSurface(nullptr);
if (tool->isDown)
PROTO::tablet->up(tool);
for (auto const& b : tool->buttonsDown) {
PROTO::tablet->buttonTool(tool, b, false);
}
PROTO::tablet->proximityOut(tool);
}
static void focusTool(SP<CTabletTool> tool, SP<CTablet> tablet, SP<CWLSurfaceResource> surf) {
if (tool->getSurface() == surf || !surf)
return;
if (tool->getSurface() && tool->getSurface() != surf)
unfocusTool(tool);
tool->setSurface(surf);
PROTO::tablet->proximityIn(tool, tablet, surf);
if (tool->isDown)
PROTO::tablet->down(tool);
for (auto const& b : tool->buttonsDown) {
PROTO::tablet->buttonTool(tool, b, true);
}
}
static void refocusTablet(SP<CTablet> tab, SP<CTabletTool> tool, bool motion = false) {
const auto LASTHLSURFACE = CWLSurface::fromResource(g_pSeatManager->state.pointerFocus.lock());
if (!LASTHLSURFACE || !tool->active) {
if (tool->getSurface())
unfocusTool(tool);
return;
}
const auto BOX = LASTHLSURFACE->getSurfaceBoxGlobal();
if (!BOX.has_value()) {
if (tool->getSurface())
unfocusTool(tool);
return;
}
const auto CURSORPOS = g_pInputManager->getMouseCoordsInternal();
focusTool(tool, tab, g_pSeatManager->state.pointerFocus.lock());
if (!motion)
return;
if (LASTHLSURFACE->constraint() && tool->aq()->type != Aquamarine::ITabletTool::AQ_TABLET_TOOL_TYPE_MOUSE) {
// cursor logic will completely break here as the cursor will be locked.
// let's just "map" the desired position to the constraint area.
Vector2D local;
// yes, this technically ignores any regions set by the app. Too bad!
if (LASTHLSURFACE->getWindow())
local = tool->absolutePos * LASTHLSURFACE->getWindow()->m_vRealSize.goal();
else
local = tool->absolutePos * BOX->size();
if (LASTHLSURFACE->getWindow() && LASTHLSURFACE->getWindow()->m_bIsX11)
local = local * LASTHLSURFACE->getWindow()->m_fX11SurfaceScaledBy;
PROTO::tablet->motion(tool, local);
return;
}
auto local = CURSORPOS - BOX->pos();
if (LASTHLSURFACE->getWindow() && LASTHLSURFACE->getWindow()->m_bIsX11)
local = local * LASTHLSURFACE->getWindow()->m_fX11SurfaceScaledBy;
PROTO::tablet->motion(tool, local);
}
void CInputManager::onTabletAxis(CTablet::SAxisEvent e) {
const auto PTAB = e.tablet;
const auto PTOOL = ensureTabletToolPresent(e.tool);
if (PTOOL->active && (e.updatedAxes & (CTablet::eTabletToolAxes::HID_TABLET_TOOL_AXIS_X | CTablet::eTabletToolAxes::HID_TABLET_TOOL_AXIS_Y))) {
double x = (e.updatedAxes & CTablet::eTabletToolAxes::HID_TABLET_TOOL_AXIS_X) ? e.axis.x : NAN;
double dx = (e.updatedAxes & CTablet::eTabletToolAxes::HID_TABLET_TOOL_AXIS_X) ? e.axisDelta.x : NAN;
double y = (e.updatedAxes & CTablet::eTabletToolAxes::HID_TABLET_TOOL_AXIS_Y) ? e.axis.y : NAN;
double dy = (e.updatedAxes & CTablet::eTabletToolAxes::HID_TABLET_TOOL_AXIS_Y) ? e.axisDelta.y : NAN;
Vector2D delta = {std::isnan(dx) ? 0.0 : dx, std::isnan(dy) ? 0.0 : dy};
switch (e.tool->type) {
case Aquamarine::ITabletTool::AQ_TABLET_TOOL_TYPE_MOUSE: {
g_pPointerManager->move(delta);
break;
}
default: {
if (!std::isnan(x))
PTOOL->absolutePos.x = x;
if (!std::isnan(y))
PTOOL->absolutePos.y = y;
if (PTAB->relativeInput)
g_pPointerManager->move(delta);
else {
//Calculate transformations if active area is set
if (!PTAB->activeArea.empty()) {
if (!std::isnan(x))
x = (x - PTAB->activeArea.x) / (PTAB->activeArea.w - PTAB->activeArea.x);
if (!std::isnan(y))
y = (y - PTAB->activeArea.y) / (PTAB->activeArea.h - PTAB->activeArea.y);
}
g_pPointerManager->warpAbsolute({x, y}, PTAB);
}
break;
}
}
refocusTablet(PTAB, PTOOL, true);
m_tmrLastCursorMovement.reset();
}
if (e.updatedAxes & CTablet::eTabletToolAxes::HID_TABLET_TOOL_AXIS_PRESSURE)
PROTO::tablet->pressure(PTOOL, e.pressure);
if (e.updatedAxes & CTablet::eTabletToolAxes::HID_TABLET_TOOL_AXIS_DISTANCE)
PROTO::tablet->distance(PTOOL, e.distance);
if (e.updatedAxes & CTablet::eTabletToolAxes::HID_TABLET_TOOL_AXIS_ROTATION)
PROTO::tablet->rotation(PTOOL, e.rotation);
if (e.updatedAxes & CTablet::eTabletToolAxes::HID_TABLET_TOOL_AXIS_SLIDER)
PROTO::tablet->slider(PTOOL, e.slider);
if (e.updatedAxes & CTablet::eTabletToolAxes::HID_TABLET_TOOL_AXIS_WHEEL)
PROTO::tablet->wheel(PTOOL, e.wheelDelta);
if (e.updatedAxes & CTablet::eTabletToolAxes::HID_TABLET_TOOL_AXIS_TILT_X)
PTOOL->tilt.x = e.tilt.x;
if (e.updatedAxes & CTablet::eTabletToolAxes::HID_TABLET_TOOL_AXIS_TILT_Y)
PTOOL->tilt.y = e.tilt.y;
if (e.updatedAxes & (CTablet::eTabletToolAxes::HID_TABLET_TOOL_AXIS_TILT_X | CTablet::eTabletToolAxes::HID_TABLET_TOOL_AXIS_TILT_Y))
PROTO::tablet->tilt(PTOOL, PTOOL->tilt);
}
void CInputManager::onTabletTip(CTablet::STipEvent e) {
const auto PTAB = e.tablet;
const auto PTOOL = ensureTabletToolPresent(e.tool);
if (e.in) {
simulateMouseMovement();
refocusTablet(PTAB, PTOOL);
PROTO::tablet->down(PTOOL);
} else
PROTO::tablet->up(PTOOL);
PTOOL->isDown = e.in;
}
void CInputManager::onTabletButton(CTablet::SButtonEvent e) {
const auto PTOOL = ensureTabletToolPresent(e.tool);
PROTO::tablet->buttonTool(PTOOL, e.button, e.down);
if (e.down)
PTOOL->buttonsDown.push_back(e.button);
else
std::erase(PTOOL->buttonsDown, e.button);
}
void CInputManager::onTabletProximity(CTablet::SProximityEvent e) {
const auto PTAB = e.tablet;
const auto PTOOL = ensureTabletToolPresent(e.tool);
PTOOL->active = e.in;
if (!e.in) {
if (PTOOL->getSurface())
unfocusTool(PTOOL);
} else {
simulateMouseMovement();
refocusTablet(PTAB, PTOOL);
}
}
void CInputManager::newTablet(SP<Aquamarine::ITablet> pDevice) {
const auto PNEWTABLET = m_vTablets.emplace_back(CTablet::create(pDevice));
m_vHIDs.push_back(PNEWTABLET);
try {
PNEWTABLET->hlName = deviceNameToInternalString(pDevice->getName());
} catch (std::exception& e) {
Debug::log(ERR, "Tablet had no name???"); // logic error
}
g_pPointerManager->attachTablet(PNEWTABLET);
PNEWTABLET->events.destroy.registerStaticListener(
[this](void* owner, std::any d) {
auto TABLET = ((CTablet*)owner)->self;
destroyTablet(TABLET.lock());
},
PNEWTABLET.get());
setTabletConfigs();
}
SP<CTabletTool> CInputManager::ensureTabletToolPresent(SP<Aquamarine::ITabletTool> pTool) {
for (auto const& t : m_vTabletTools) {
if (t->aq() == pTool)
return t;
}
const auto PTOOL = m_vTabletTools.emplace_back(CTabletTool::create(pTool));
m_vHIDs.push_back(PTOOL);
PTOOL->events.destroy.registerStaticListener(
[this](void* owner, std::any d) {
auto TOOL = ((CTabletTool*)owner)->self;
destroyTabletTool(TOOL.lock());
},
PTOOL.get());
return PTOOL;
}
void CInputManager::newTabletPad(SP<Aquamarine::ITabletPad> pDevice) {
const auto PNEWPAD = m_vTabletPads.emplace_back(CTabletPad::create(pDevice));
m_vHIDs.push_back(PNEWPAD);
try {
PNEWPAD->hlName = deviceNameToInternalString(pDevice->getName());
} catch (std::exception& e) {
Debug::log(ERR, "Pad had no name???"); // logic error
}
// clang-format off
PNEWPAD->events.destroy.registerStaticListener([this](void* owner, std::any d) {
auto PAD = ((CTabletPad*)owner)->self;
destroyTabletPad(PAD.lock());
}, PNEWPAD.get());
PNEWPAD->padEvents.button.registerStaticListener([](void* owner, std::any e) {
const auto E = std::any_cast<CTabletPad::SButtonEvent>(e);
const auto PPAD = ((CTabletPad*)owner)->self.lock();
PROTO::tablet->mode(PPAD, 0, E.mode, E.timeMs);
PROTO::tablet->buttonPad(PPAD, E.button, E.timeMs, E.down);
}, PNEWPAD.get());
PNEWPAD->padEvents.strip.registerStaticListener([](void* owner, std::any e) {
const auto E = std::any_cast<CTabletPad::SStripEvent>(e);
const auto PPAD = ((CTabletPad*)owner)->self.lock();
PROTO::tablet->strip(PPAD, E.strip, E.position, E.finger, E.timeMs);
}, PNEWPAD.get());
PNEWPAD->padEvents.ring.registerStaticListener([](void* owner, std::any e) {
const auto E = std::any_cast<CTabletPad::SRingEvent>(e);
const auto PPAD = ((CTabletPad*)owner)->self.lock();
PROTO::tablet->ring(PPAD, E.ring, E.position, E.finger, E.timeMs);
}, PNEWPAD.get());
PNEWPAD->padEvents.attach.registerStaticListener([](void* owner, std::any e) {
const auto PPAD = ((CTabletPad*)owner)->self.lock();
const auto TOOL = std::any_cast<SP<CTabletTool>>(e);
PPAD->parent = TOOL;
}, PNEWPAD.get());
// clang-format on
}
|