Repository navigation
Expand file tree
/
Copy pathmouse_protocol.cpp
More file actions
128 lines (119 loc) · 3.73 KB
/
Copy pathmouse_protocol.cpp
File metadata and controls
128 lines (119 loc) · 3.73 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
/*
* Copyright (C) 2026 Shitty team
* MIT licensed
* See the file LICENSE.MIT for the full license.
*/
#include "mouse_protocol.h"
#include "utf8.h"
#include <std/str/builder.h>
#include <std/str/view.h>
#include <algorithm>
using namespace stl;
bool encodeMouseProtocol(StringBuilder& output, MouseTrackingEnc encoding, MouseEventType type, unsigned modifiers, int motionButton, int button, int column, int row) {
int code = 0;
if (type == MouseEventType::Motion) {
switch (motionButton) {
case 1:
code = 32;
break;
case 2:
code = 33;
break;
case 3:
code = 34;
break;
default:
code = 35;
break;
}
} else if (type == MouseEventType::Release && encoding != MouseTrackingEnc::SGR) {
code = 3;
} else {
switch (button) {
case 1:
code = 0;
break;
case 2:
code = 1;
break;
case 3:
code = 2;
break;
case 4:
code = 64;
break;
case 5:
code = 65;
break;
case 6:
code = 66;
break;
case 7:
code = 67;
break;
case 8:
code = 128;
break;
case 9:
code = 129;
break;
case 10:
code = 130;
break;
case 11:
code = 131;
break;
default:
return false;
}
}
if (modifiers & MouseShift) {
code += 4;
}
if (modifiers & MouseAlt) {
code += 8;
}
if (modifiers & MouseControl) {
code += 16;
}
switch (encoding) {
case MouseTrackingEnc::Default:
column = std::clamp(column, 1, 223);
row = std::clamp(row, 1, 223);
output << StringView(u8"\x1b[M");
{
const u8 bytes[] = {(u8)(32 + code), (u8)(32 + column), (u8)(32 + row)};
output.append(bytes, sizeof(bytes));
}
break;
case MouseTrackingEnc::UTF8:
column = std::clamp(column, 1, 2015);
row = std::clamp(row, 1, 2015);
output << StringView(u8"\x1b[M");
Utf8Encoder::pushUnicode(32 + code, [&output](char ch) {
output.append(&ch, 1);
});
Utf8Encoder::pushUnicode(32 + column, [&output](char ch) {
output.append(&ch, 1);
});
Utf8Encoder::pushUnicode(32 + row, [&output](char ch) {
output.append(&ch, 1);
});
break;
case MouseTrackingEnc::SGR:
case MouseTrackingEnc::SGRPixels:
output << StringView(u8"\x1b[<") << code << StringView(u8";") << column << StringView(u8";") << row << (type == MouseEventType::Release ? StringView(u8"m") : StringView(u8"M"));
break;
case MouseTrackingEnc::URXVT:
output << StringView(u8"\x1b[") << code + 32 << StringView(u8";") << column << StringView(u8";") << row << StringView(u8"M");
break;
}
return true;
}
std::string encodeMouseProtocol(MouseTrackingEnc encoding, MouseEventType type, unsigned modifiers, int motionButton, int button, int column, int row) {
StringBuilder output;
if (!encodeMouseProtocol(output, encoding, type, modifiers, motionButton, button, column, row)) {
return {};
}
return std::string((const char*)(output.data()), output.used());
}