-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathInputGenerator.cpp
714 lines (608 loc) · 23.4 KB
/
InputGenerator.cpp
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
// SPDX-License-Identifier: Apache-2.0
#include <vtbackend/ControlCode.h>
#include <vtbackend/InputGenerator.h>
#include <vtbackend/logging.h>
#include <crispy/utils.h>
#include <fmt/format.h>
#include <array>
#include <iterator>
#include <string_view>
#include <unordered_map>
#include <libunicode/convert.h>
using namespace std;
namespace vtbackend
{
namespace mappings
{
struct KeyMapping
{
Key key;
std::string_view mapping {};
};
// TODO: implement constexpr-binary-search by:
// - adding operator<(KeyMapping a, KeyMapping b) { return a.key < b.key; }
// - constexpr-evaluated sort()ed array returned in lambda-expr to be assigned to these globals here.
// - make use of this property and let tryMap() do a std::binary_search()
#define ESC "\x1B"
#define CSI "\x1B["
#define SS3 "\x1BO"
// the modifier parameter is going to be replaced via fmt::format()
array<KeyMapping, 30> const functionKeysWithModifiers {
// clang-format off
// Note, that F1..F4 is using CSI too instead of ESC when used with modifier keys.
// XXX: Maybe I am blind when reading ctlseqs.txt, but F1..F4 with "1;{}P".. seems not to
// match what other terminal emulators send out with modifiers and I don't see how to match
// xterm's behaviour along with getting for example vim working to bind to these.
KeyMapping { Key::F1, ESC "O{}P" }, // "1;{}P"
KeyMapping { Key::F2, ESC "O{}Q" }, // "1;{}Q"
KeyMapping { Key::F3, ESC "O{}R" }, // "1;{}R"
KeyMapping { Key::F4, ESC "O{}S" }, // "1;{}S"
KeyMapping { Key::F5, CSI "15;{}~" },
KeyMapping { Key::F6, CSI "17;{}~" },
KeyMapping { Key::F7, CSI "18;{}~" },
KeyMapping { Key::F8, CSI "19;{}~" },
KeyMapping { Key::F9, CSI "20;{}~" },
KeyMapping { Key::F10, CSI "21;{}~" },
KeyMapping { Key::F11, CSI "23;{}~" },
KeyMapping { Key::F12, CSI "24;{}~" },
KeyMapping { Key::F13, CSI "25;{}~" },
KeyMapping { Key::F14, CSI "26;{}~" },
KeyMapping { Key::F15, CSI "28;{}~" },
KeyMapping { Key::F16, CSI "29;{}~" },
KeyMapping { Key::F17, CSI "31;{}~" },
KeyMapping { Key::F18, CSI "32;{}~" },
KeyMapping { Key::F19, CSI "33;{}~" },
KeyMapping { Key::F20, CSI "34;{}~" },
// cursor keys
KeyMapping { Key::UpArrow, CSI "1;{}A" },
KeyMapping { Key::DownArrow, CSI "1;{}B" },
KeyMapping { Key::RightArrow, CSI "1;{}C" },
KeyMapping { Key::LeftArrow, CSI "1;{}D" },
// 6-key editing pad
KeyMapping { Key::Insert, CSI "2;{}~" },
KeyMapping { Key::Delete, CSI "3;{}~" },
KeyMapping { Key::Home, CSI "1;{}H" },
KeyMapping { Key::End, CSI "1;{}F" },
KeyMapping { Key::PageUp, CSI "5;{}~" },
KeyMapping { Key::PageDown, CSI "6;{}~" },
// clang-format on
};
array<KeyMapping, 22> const standard {
// clang-format off
// cursor keys
KeyMapping { Key::UpArrow, CSI "A" },
KeyMapping { Key::DownArrow, CSI "B" },
KeyMapping { Key::RightArrow, CSI "C" },
KeyMapping { Key::LeftArrow, CSI "D" },
// 6-key editing pad
KeyMapping { Key::Insert, CSI "2~" },
KeyMapping { Key::Delete, CSI "3~" },
KeyMapping { Key::Home, CSI "H" },
KeyMapping { Key::End, CSI "F" },
KeyMapping { Key::PageUp, CSI "5~" },
KeyMapping { Key::PageDown, CSI "6~" },
// function keys
KeyMapping { Key::F1, ESC "OP" },
KeyMapping { Key::F2, ESC "OQ" },
KeyMapping { Key::F3, ESC "OR" },
KeyMapping { Key::F4, ESC "OS" },
KeyMapping { Key::F5, CSI "15~" },
KeyMapping { Key::F6, CSI "17~" },
KeyMapping { Key::F7, CSI "18~" },
KeyMapping { Key::F8, CSI "19~" },
KeyMapping { Key::F9, CSI "20~" },
KeyMapping { Key::F10, CSI "21~" },
KeyMapping { Key::F11, CSI "23~" },
KeyMapping { Key::F12, CSI "24~" },
// clang-format on
};
/// (DECCKM) Cursor key mode: mappings in when cursor key application mode is set.
array<KeyMapping, 6> const applicationCursorKeys {
// clang-format off
KeyMapping { Key::UpArrow, SS3 "A" },
KeyMapping { Key::DownArrow, SS3 "B" },
KeyMapping { Key::RightArrow, SS3 "C" },
KeyMapping { Key::LeftArrow, SS3 "D" },
KeyMapping { Key::Home, SS3 "H" },
KeyMapping { Key::End, SS3 "F" },
// clang-format on
};
array<KeyMapping, 21> const applicationKeypad {
// clang-format off
KeyMapping { Key::PageUp, CSI "5~" },
KeyMapping { Key::PageDown, CSI "6~" },
// KeyMapping{Key::Space, SS3 " "}, // TODO
// KeyMapping{Key::Tab, SS3 "I"}, // TODO
// KeyMapping{Key::Enter, SS3 "M"}, // TODO
// clang-format on
};
#undef ESC
#undef CSI
#undef SS3
constexpr bool operator==(KeyMapping const& km, Key key) noexcept
{
return km.key == key;
}
template <size_t N>
optional<string_view> tryMap(array<KeyMapping, N> const& mappings, Key key) noexcept
{
for (KeyMapping const& km: mappings)
if (km.key == key)
return { km.mapping };
return nullopt;
}
} // namespace mappings
string to_string(Modifier modifier)
{
return fmt::format("{}", modifier);
}
string to_string(Key key)
{
return fmt::format("{}", key);
}
string to_string(MouseButton button)
{
return fmt::format("{}", button);
}
void InputGenerator::reset()
{
_cursorKeysMode = KeyMode::Normal;
_numpadKeysMode = KeyMode::Normal;
_bracketedPaste = false;
_generateFocusEvents = false;
_mouseProtocol = std::nullopt;
_mouseTransport = MouseTransport::Default;
_mouseWheelMode = MouseWheelMode::Default;
// _pendingSequence = {};
// _currentMousePosition = {0, 0}; // current mouse position
// _currentlyPressedMouseButtons = {};
}
void InputGenerator::setCursorKeysMode(KeyMode mode)
{
inputLog()("set cursor keys mode: {}", mode);
_cursorKeysMode = mode;
}
void InputGenerator::setNumpadKeysMode(KeyMode mode)
{
inputLog()("set numpad keys mode: {}", mode);
_numpadKeysMode = mode;
}
void InputGenerator::setApplicationKeypadMode(bool enable)
{
if (enable)
_numpadKeysMode = KeyMode::Application;
else
_numpadKeysMode = KeyMode::Normal; // aka. Numeric
inputLog()("set application keypad mode: {} -> {}", enable, _numpadKeysMode);
}
bool InputGenerator::generate(char32_t characterEvent, Modifier modifier)
{
char const chr = static_cast<char>(characterEvent);
// See section "Alt and Meta Keys" in ctlseqs.txt from xterm.
if (modifier == Modifier::Alt)
// NB: There are other modes in xterm to send Alt+Key options or even send ESC on Meta key instead.
append("\033");
// Well accepted hack to distinguish between Backspace nad Ctrl+Backspace,
// - Backspace is emitting 0x7f,
// - Ctrl+Backspace is emitting 0x08
if (characterEvent == 0x08)
{
if (!modifier.control())
return append("\x7f");
else
return append("\x08");
}
if (modifier == Modifier::Shift && characterEvent == 0x09)
return append("\033[Z"); // introduced by linux_console in 1995, adopted by xterm in 2002
// raw C0 code
if (modifier == Modifier::Control && characterEvent < 32)
return append(static_cast<uint8_t>(characterEvent));
if (modifier == Modifier::Control && characterEvent == L' ')
return append('\x00');
if (modifier == Modifier::Control && crispy::ascending('A', chr, 'Z'))
return append(static_cast<char>(chr - 'A' + 1));
if (modifier == Modifier::Control && characterEvent >= '[' && characterEvent <= '_')
return append(static_cast<char>(chr - 'A' + 1)); // remaining C0 characters 0x1B .. 0x1F
if (modifier.without(Modifier::Alt).none() || modifier == Modifier::Shift)
return append(unicode::convert_to<char>(characterEvent));
if (characterEvent < 0x7F)
append(static_cast<char>(characterEvent));
else
append(unicode::convert_to<char>(characterEvent));
inputLog()("Sending {} \"{}\".", modifier, crispy::escape(unicode::convert_to<char>(characterEvent)));
return true;
}
bool InputGenerator::generate(Key key, Modifier modifier)
{
auto const logged = [key, modifier](bool success) -> bool {
if (success)
inputLog()("Sending {} {}.", modifier, key);
return success;
};
if (modifier)
{
if (auto mapping = tryMap(mappings::functionKeysWithModifiers, key); mapping)
return logged(append(crispy::replace(*mapping, "{}"sv, makeVirtualTerminalParam(modifier))));
}
if (applicationCursorKeys())
if (auto mapping = tryMap(mappings::applicationCursorKeys, key); mapping)
return logged(append(*mapping));
if (applicationKeypad())
if (auto mapping = tryMap(mappings::applicationKeypad, key); mapping)
return logged(append(*mapping));
if (auto mapping = tryMap(mappings::standard, key); mapping)
return logged(append(*mapping));
return false;
}
void InputGenerator::generatePaste(std::string_view const& text)
{
inputLog()("Sending paste of {} bytes.", text.size());
if (text.empty())
return;
if (_bracketedPaste)
append("\033[200~"sv);
append(text);
if (_bracketedPaste)
append("\033[201~"sv);
}
inline bool InputGenerator::append(std::string_view sequence)
{
_pendingSequence.insert(end(_pendingSequence), begin(sequence), end(sequence));
return true;
}
inline bool InputGenerator::append(char asciiChar)
{
_pendingSequence.push_back(asciiChar);
return true;
}
inline bool InputGenerator::append(uint8_t byte)
{
_pendingSequence.push_back(static_cast<char>(byte));
return true;
}
inline bool InputGenerator::append(unsigned int asciiChar)
{
char buf[16];
int n = snprintf(buf, sizeof(buf), "%u", asciiChar);
return append(string_view(buf, static_cast<size_t>(n)));
}
bool InputGenerator::generateFocusInEvent()
{
if (generateFocusEvents())
{
append("\033[I");
inputLog()("Sending focus-in event.");
return true;
}
return false;
}
bool InputGenerator::generateFocusOutEvent()
{
if (generateFocusEvents())
{
append("\033[O");
inputLog()("Sending focus-out event.");
return true;
}
return true;
}
bool InputGenerator::generateRaw(std::string_view const& raw)
{
append(raw);
return true;
}
// {{{ mouse handling
void InputGenerator::setMouseProtocol(MouseProtocol mouseProtocol, bool enabled)
{
if (enabled)
{
_mouseWheelMode = MouseWheelMode::Default;
_mouseProtocol = mouseProtocol;
}
else
_mouseProtocol = std::nullopt;
}
void InputGenerator::setMouseTransport(MouseTransport mouseTransport)
{
_mouseTransport = mouseTransport;
}
void InputGenerator::setMouseWheelMode(MouseWheelMode mode) noexcept
{
_mouseWheelMode = mode;
}
namespace
{
constexpr uint8_t modifierBits(Modifier modifier) noexcept
{
uint8_t mods = 0;
if (modifier.shift())
mods |= 4;
if (modifier.meta())
mods |= 8;
if (modifier.control())
mods |= 16;
return mods;
}
constexpr uint8_t buttonNumber(MouseButton button) noexcept
{
switch (button)
{
case MouseButton::Left: return 0;
case MouseButton::Middle: return 1;
case MouseButton::Right: return 2;
case MouseButton::Release: return 3;
case MouseButton::WheelUp: return 4;
case MouseButton::WheelDown: return 5;
case MouseButton::WheelRight: return 6;
case MouseButton::WheelLeft: return 7;
}
return 0; // should never happen
}
constexpr bool isMouseWheel(MouseButton button) noexcept
{
return button == MouseButton::WheelUp || button == MouseButton::WheelDown
|| button == MouseButton::WheelLeft || button == MouseButton::WheelRight;
}
constexpr uint8_t buttonX10(MouseButton button) noexcept
{
return isMouseWheel(button) ? uint8_t(buttonNumber(button) + 0x3c) : buttonNumber(button);
}
constexpr uint8_t buttonNormal(MouseButton button, InputGenerator::MouseEventType eventType) noexcept
{
return eventType == InputGenerator::MouseEventType::Release ? 3 : buttonX10(button);
}
} // namespace
bool InputGenerator::generateMouse(MouseEventType eventType,
Modifier modifier,
MouseButton button,
CellLocation pos,
PixelCoordinate pixelPosition,
bool uiHandled)
{
if (!_mouseProtocol.has_value())
return false;
// std::cout << fmt::format("generateMouse({}/{}): button:{}, modifier:{}, at:{}, type:{}\n",
// _mouseTransport, *_mouseProtocol,
// button, modifier, pos, eventType);
switch (*_mouseProtocol)
{
case MouseProtocol::X10: // Old X10 mouse protocol
if (eventType == MouseEventType::Press)
mouseTransport(
eventType, buttonX10(button), modifierBits(modifier), pos, pixelPosition, uiHandled);
return true;
case MouseProtocol::NormalTracking: // Normal tracking mode, that's X10 with mouse release events and
// modifiers
if (eventType == MouseEventType::Press || eventType == MouseEventType::Release)
{
auto const buttonValue = _mouseTransport != MouseTransport::SGR
? buttonNormal(button, eventType)
: buttonX10(button);
mouseTransport(eventType, buttonValue, modifierBits(modifier), pos, pixelPosition, uiHandled);
}
return true;
case MouseProtocol::ButtonTracking: // Button-event tracking protocol.
// like normal event tracking, but with drag events
if (eventType == MouseEventType::Press || eventType == MouseEventType::Drag
|| eventType == MouseEventType::Release)
{
auto const buttonValue = _mouseTransport != MouseTransport::SGR
? buttonNormal(button, eventType)
: buttonX10(button);
uint8_t const draggableButton =
eventType == MouseEventType::Drag ? uint8_t(buttonValue + 0x20) : buttonValue;
mouseTransport(
eventType, draggableButton, modifierBits(modifier), pos, pixelPosition, uiHandled);
return true;
}
return false;
case MouseProtocol::AnyEventTracking: // Like ButtonTracking but any motion events (not just dragging)
// TODO: make sure we can receive mouse-move events even without mouse pressed.
{
auto const buttonValue = _mouseTransport != MouseTransport::SGR
? buttonNormal(button, eventType)
: buttonX10(button);
uint8_t const draggableButton =
eventType == MouseEventType::Drag ? uint8_t(buttonValue + 0x20) : buttonValue;
mouseTransport(
eventType, draggableButton, modifierBits(modifier), pos, pixelPosition, uiHandled);
}
return true;
case MouseProtocol::HighlightTracking: // Highlight mouse tracking
return false; // TODO: do we want to implement this?
}
return false;
}
bool InputGenerator::mouseTransport(MouseEventType eventType,
uint8_t button,
uint8_t modifier,
CellLocation pos,
PixelCoordinate pixelPosition,
bool uiHandled)
{
if (pos.line.value < 0 || pos.column.value < 0)
// Negative coordinates are not supported. Avoid sending bad values.
return true;
switch (_mouseTransport)
{
case MouseTransport::Default: // mode: 9
mouseTransportX10(button, modifier, pos);
return true;
case MouseTransport::Extended: // mode: 1005
// TODO (like Default but with UTF-8 encoded coords)
mouseTransportExtended(button, modifier, pos);
return false;
case MouseTransport::SGR: // mode: 1006
return mouseTransportSGR(eventType, button, modifier, *pos.column + 1, *pos.line + 1, uiHandled);
case MouseTransport::URXVT: // mode: 1015
return mouseTransportURXVT(eventType, button, modifier, pos);
case MouseTransport::SGRPixels: // mode: 1016
return mouseTransportSGR(
eventType, button, modifier, pixelPosition.x.value, pixelPosition.y.value, uiHandled);
}
return false;
}
bool InputGenerator::mouseTransportExtended(uint8_t button, uint8_t modifier, CellLocation pos)
{
constexpr auto SkipCount = uint8_t { 0x20 }; // TODO std::numeric_limits<ControlCode>::max();
constexpr auto MaxCoordValue = 2015;
if (*pos.line < MaxCoordValue && *pos.column < MaxCoordValue)
{
auto const buttonValue = static_cast<uint8_t>(SkipCount + static_cast<uint8_t>(button | modifier));
auto const line = static_cast<char32_t>(SkipCount + *pos.line + 1);
auto const column = static_cast<char32_t>(SkipCount + *pos.column + 1);
append("\033[M");
append(buttonValue);
append(unicode::convert_to<char>(column));
append(unicode::convert_to<char>(line));
return true;
}
else
return false;
}
bool InputGenerator::mouseTransportX10(uint8_t button, uint8_t modifier, CellLocation pos)
{
constexpr uint8_t SkipCount = 0x20; // TODO std::numeric_limits<ControlCode>::max();
constexpr uint8_t MaxCoordValue = std::numeric_limits<uint8_t>::max() - SkipCount;
if (*pos.line < MaxCoordValue && *pos.column < MaxCoordValue)
{
auto const buttonValue = static_cast<uint8_t>(SkipCount + static_cast<uint8_t>(button | modifier));
auto const line = static_cast<uint8_t>(SkipCount + *pos.line + 1);
auto const column = static_cast<uint8_t>(SkipCount + *pos.column + 1);
append("\033[M");
append(buttonValue);
append(column);
append(line);
return true;
}
else
return false;
}
bool InputGenerator::mouseTransportSGR(
MouseEventType eventType, uint8_t button, uint8_t modifier, int x, int y, bool uiHandled)
{
append("\033[<");
append(static_cast<unsigned>(button | modifier));
append(';');
append(static_cast<unsigned>(x));
append(';');
append(static_cast<unsigned>(y));
if (_passiveMouseTracking)
{
append(';');
append(uiHandled ? '1' : '0');
}
append(eventType != MouseEventType::Release ? 'M' : 'm');
return true;
}
bool InputGenerator::mouseTransportURXVT(MouseEventType eventType,
uint8_t button,
uint8_t modifier,
CellLocation pos)
{
if (eventType == MouseEventType::Press)
{
append("\033[");
append(static_cast<unsigned>(button | modifier));
append(';');
append(static_cast<unsigned>(*pos.column + 1));
append(';');
append(static_cast<unsigned>(*pos.line + 1));
append('M');
}
return true;
}
bool InputGenerator::generateMousePress(
Modifier modifier, MouseButton button, CellLocation pos, PixelCoordinate pixelPosition, bool uiHandled)
{
auto const logged = [=](bool success) -> bool {
if (success)
inputLog()("Sending mouse press {} {} at {}.", button, modifier, pos);
return success;
};
_currentMousePosition = pos;
if (!_mouseProtocol.has_value())
return false;
switch (mouseWheelMode())
{
case MouseWheelMode::NormalCursorKeys:
if (_passiveMouseTracking)
break;
switch (button)
{
case MouseButton::WheelUp: return logged(append("\033[A"));
case MouseButton::WheelDown: return logged(append("\033[B"));
default: break;
}
break;
case MouseWheelMode::ApplicationCursorKeys:
if (_passiveMouseTracking)
break;
switch (button)
{
case MouseButton::WheelUp: return logged(append("\033OA"));
case MouseButton::WheelDown: return logged(append("\033OB"));
default: break;
}
break;
case MouseWheelMode::Default: break;
}
if (!isMouseWheel(button))
if (!_currentlyPressedMouseButtons.count(button))
_currentlyPressedMouseButtons.insert(button);
return logged(generateMouse(
MouseEventType::Press, modifier, button, _currentMousePosition, pixelPosition, uiHandled));
}
bool InputGenerator::generateMouseRelease(
Modifier modifier, MouseButton button, CellLocation pos, PixelCoordinate pixelPosition, bool uiHandled)
{
auto const logged = [=](bool success) -> bool {
if (success)
inputLog()("Sending mouse release {} {} at {}.", button, modifier, pos);
return success;
};
_currentMousePosition = pos;
if (auto i = _currentlyPressedMouseButtons.find(button); i != _currentlyPressedMouseButtons.end())
_currentlyPressedMouseButtons.erase(i);
return logged(generateMouse(
MouseEventType::Release, modifier, button, _currentMousePosition, pixelPosition, uiHandled));
}
bool InputGenerator::generateMouseMove(Modifier modifier,
CellLocation pos,
PixelCoordinate pixelPosition,
bool uiHandled)
{
if (pos == _currentMousePosition && _mouseTransport != MouseTransport::SGRPixels)
// Only generate a mouse move event if the coordinate of interest(!) has actually changed.
return false;
auto const logged = [&](bool success) -> bool {
if (success)
{
inputLog()("[{}:{}] Sending mouse move at {} ({}:{}).",
_mouseProtocol.value(),
_mouseTransport,
pos,
pixelPosition.x.value,
pixelPosition.y.value);
}
return success;
};
_currentMousePosition = pos;
if (!_mouseProtocol.has_value())
return false;
bool const buttonsPressed = !_currentlyPressedMouseButtons.empty();
bool const report = (_mouseProtocol.value() == MouseProtocol::ButtonTracking && buttonsPressed)
|| _mouseProtocol.value() == MouseProtocol::AnyEventTracking;
if (report)
return logged(generateMouse(
MouseEventType::Drag,
modifier,
buttonsPressed ? *_currentlyPressedMouseButtons.begin() // what if multiple are pressed?
: MouseButton::Release,
pos,
pixelPosition,
uiHandled));
return false;
}
// }}}
} // namespace vtbackend