-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
264 lines (239 loc) · 7.03 KB
/
main.cpp
File metadata and controls
264 lines (239 loc) · 7.03 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
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
#define NOMINMAX
#include <windows.h>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <string_view>
#include <vector>
#include <optional>
#include <charconv>
#include <algorithm>
#include <memory>
#include "finalmouse.hpp"
using std::string;
using std::string_view;
struct Args {
std::optional<unsigned> hz;
std::optional<unsigned> vid;
std::optional<unsigned> pid;
std::optional<string> path;
bool list = false;
bool help = false;
bool silent = false;
bool verbose = true;
};
static bool parse_hex16(std::string_view s, unsigned& out) {
if (s.size() > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
s.remove_prefix(2);
unsigned v = 0;
auto* f = s.data();
auto* l = f + s.size();
auto res = std::from_chars(f, l, v, 16);
if (res.ec != std::errc{} || v > 0xFFFF)
return false;
out = v;
return true;
}
static std::optional<Args> parse_args(int argc, char** argv) {
Args a{};
for (int i = 1; i < argc; ++i) {
string_view t = argv[i];
if (t == "--help" || t == "-h")
a.help = true;
else if (t == "--list")
a.list = true;
else if (t == "--silent" || t == "-s")
a.silent = true;
else if (t == "--hz" && i + 1 < argc)
a.hz = std::strtoul(argv[++i], nullptr, 10);
else if (t == "--vid" && i + 1 < argc) {
unsigned v = 0;
if (!parse_hex16(argv[++i], v))
return {};
a.vid = v;
} else if (t == "--pid" && i + 1 < argc) {
unsigned p = 0;
if (!parse_hex16(argv[++i], p))
return {};
a.pid = p;
} else if (t == "--path" && i + 1 < argc)
a.path = argv[++i];
else
return {};
}
if (!(a.help || a.list || a.hz))
return {};
return a;
}
static bool g_console_attached = false;
static bool g_console_allocated = false;
static void attach_console(bool silent) {
if (silent)
return;
if (AttachConsole(ATTACH_PARENT_PROCESS))
g_console_attached = true;
else if (AllocConsole())
g_console_allocated = true;
FILE* fp = nullptr;
freopen_s(&fp, "CONOUT$", "w", stdout);
freopen_s(&fp, "CONOUT$", "w", stderr);
freopen_s(&fp, "CONIN$", "r", stdin);
}
static void free_console() {
if (g_console_allocated)
FreeConsole();
}
struct HzParams {
uint8_t a, b;
};
static std::optional<HzParams> map_hz(unsigned hz) {
switch (hz) {
case 500:
return HzParams{244, 1};
case 1000:
return HzParams{232, 3};
case 2000:
return HzParams{208, 7};
case 4000:
return HzParams{160, 15};
case 8000:
return HzParams{64, 31};
default:
return std::nullopt;
}
}
static void dump(const AppState& app, const DeviceInfo& d) {
app.log("path: %s", d.path.c_str());
app.log("vid: 0x%04X pid: 0x%04X iface: %d", d.vid, d.pid, d.iface);
app.log("usage_page: 0x%04X usage: 0x%04X", d.usage_page, d.usage);
app.log("manuf: %s", d.manufacturer.c_str());
app.log("prod : %s", d.product.c_str());
app.log("serial: %s", d.serial.c_str());
}
static std::vector<DeviceInfo> pick_candidates(
const std::vector<DeviceInfo>& all, const std::optional<unsigned>& vid, const std::optional<unsigned>& pid, const std::optional<string>& path) {
if (path) {
auto it = std::ranges::find_if(all, [&](const DeviceInfo& d) { return d.path == *path; });
return (it != all.end()) ? std::vector<DeviceInfo>{*it} : std::vector<DeviceInfo>{};
}
if (vid && pid) {
std::vector<DeviceInfo> v;
for (auto& d : all)
if (d.vid == *vid && d.pid == *pid)
v.push_back(d);
return v;
}
std::vector<DeviceInfo> v;
for (auto& d : all)
if (icontains(d.manufacturer, "finalmouse") || icontains(d.product, "finalmouse"))
v.push_back(d);
return v;
}
static std::vector<std::string> utf8_argv_from_win32() {
int argcW = 0;
LPWSTR* wargv = CommandLineToArgvW(GetCommandLineW(), &argcW);
std::vector<std::string> out;
if (!wargv)
return out;
out.reserve(argcW);
for (int i = 0; i < argcW; ++i) {
int need = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, nullptr, 0, nullptr, nullptr);
std::string s;
s.resize(need ? need - 1 : 0);
if (need > 1)
WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, s.data(), need, nullptr, nullptr);
out.push_back(std::move(s));
}
LocalFree(wargv);
return out;
}
static void usage(const AppState& app) {
app.log("Usage:");
app.log(" --help | -h");
app.log(" --list");
app.log(" --hz 500|1000|2000|4000|8000 [--vid 0xVVVV --pid 0xPPPP]");
app.log(" --hz HZ --path \"<hid path>\"");
app.log(" --silent | -s");
}
static int run_main(int argc, char** argv) {
if (argc <= 1) {
AppState tmp;
attach_console(false);
atexit(free_console);
usage(tmp);
return 0;
}
auto parsed = parse_args(argc, argv);
if (!parsed) {
AppState tmp;
attach_console(false);
atexit(free_console);
usage(tmp);
return 1;
}
Args args = *parsed;
attach_console(args.silent);
atexit(free_console);
AppState app;
app.silent = args.silent;
app.verbose = args.verbose;
if (args.help) {
usage(app);
return 0;
}
HidSession hid(app);
auto all = hid.enumerate_all();
if (args.list) {
int idx = 0;
for (auto& d : all) {
app.log("[%d]", idx++);
dump(app, d);
}
return 0;
}
if (!args.hz) {
app.log("Missing --hz");
usage(app);
return 3;
}
auto hp = map_hz(*args.hz);
if (!hp) {
app.log("Unsupported Hz");
return 4;
}
auto candidates = pick_candidates(all, args.vid, args.pid, args.path);
if (candidates.empty()) {
app.log("No candidate HID device.");
return 5;
}
int successes = 0;
for (auto& d : candidates) {
app.log("Opening: %s", d.path.c_str());
auto h = HidSession::open_path(d.path.c_str());
if (!h) {
app.log("open failed (busy?)");
continue;
}
int r = hid.send_report_any(h.get(), hp->a, hp->b);
if (r >= 0) {
app.log("Success %d bytes on iface %d", r, d.iface);
successes++;
break;
} else
app.log("Failed to send report");
}
if (successes == 0) {
app.log("No interface accepted the report.");
return 6;
}
app.log("Done. Requested %u Hz.", *args.hz);
return 0;
}
int WINAPI WinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPSTR, _In_ int) {
auto vec = utf8_argv_from_win32();
std::vector<char*> argv;
argv.reserve(vec.size());
for (auto& s : vec)
argv.push_back(const_cast<char*>(s.c_str()));
return run_main((int)argv.size(), argv.data());
}