-
Notifications
You must be signed in to change notification settings - Fork 0
/
OTDIPC-TestClient.cpp
141 lines (126 loc) · 3.37 KB
/
OTDIPC-TestClient.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
/*
* Copyright (c) 2022 Fred Emmott <[email protected]>
*
* SPDX-License-Identifier: MIT
*/
#define WIN32_LEAN_AND_MEAN 1
#define NOMINMAX 1
#define UNICODE 1
#define _UNICODE 1
#include <iostream>
#include <format>
#include <Windows.h>
#include <Unknwn.h>
#include <winrt/base.h>
#include <OTD-IPC/DeviceInfo.h>
#include <OTD-IPC/Ping.h>
#include <OTD-IPC/NamedPipePath.h>
#include <OTD-IPC/State.h>
void DumpMessage(const OTDIPC::Messages::DeviceInfo* const info) {
if (!info->isValid) {
std::cout << "Received invalid deviceInfo packet" << std::endl;
return;
}
std::wcout << std::format(
L"Device: {}\n "
L" VID {:04x} PID {:04x}\n"
L" {}x{}\n"
L" max pressure: {}",
info->name,
info->vid,
info->pid,
info->maxX,
info->maxY,
info->maxPressure) << std::endl;
}
void DumpMessage(const OTDIPC::Messages::State* const state) {
std::string penButtons;
std::string auxButtons;
for (int i = 0; i < 32; ++i) {
if (state->penButtons & (1 << i)) {
if (!penButtons.empty()) {
penButtons += " ";
}
penButtons += std::to_string(i);
}
if (state->auxButtons & (1 << i)) {
if (!auxButtons.empty()) {
auxButtons += " ";
}
auxButtons += std::to_string(i);
}
}
std::cout << std::format(
"{:04x}-{:04x} -> ({}, {}, {}) {} (near: {})\n"
" Pen: {}\n"
" Aux: {}",
state->vid,
state->pid,
state->x,
state->y,
state->hoverDistance,
state->pressure,
state->nearProximity,
penButtons,
auxButtons) << std::endl;
}
void DumpMessage(const OTDIPC::Messages::Ping* const msg) {
std::cout << std::format(
"{:04x}-{:04x} Ping {:#016x}",
msg->vid,
msg->pid,
msg->sequenceNumber) << std::endl;
}
template<std::derived_from<OTDIPC::Messages::Header> T>
void DumpMessage(const OTDIPC::Messages::Header* const header) {
if (header->size < sizeof(T)) {
std::cerr << std::format(
"Received message type {} of invalid size {} - expected {}",
static_cast<std::underlying_type_t<const OTDIPC::Messages::MessageType>>(header->messageType),
header->size,
sizeof(T)) << std::endl;
return;
}
DumpMessage(reinterpret_cast<const T* const>(header));
}
int main()
{
winrt::file_handle connection{
CreateFileW(
OTDIPC::NamedPipePathW,
GENERIC_READ,
0,
nullptr,
OPEN_EXISTING,
0,
NULL)};
if (!connection) {
std::cerr << std::format("Failed to open pipe: {}", GetLastError()) << std::endl;
return 1;
}
char buffer[1024];
using namespace OTDIPC::Messages;
static_assert(sizeof(buffer) >= sizeof(DeviceInfo));
static_assert(sizeof(buffer) >= sizeof(State));
auto header = reinterpret_cast<const Header* const>(buffer);
DWORD bytesRead {};
while (ReadFile(connection.get(), buffer, sizeof(buffer), &bytesRead, nullptr)) {
if (bytesRead != header->size) {
std::cerr << "header->size != packet size - is named pipe in message mode?" << std::endl;
return 1;
}
switch (header->messageType) {
case MessageType::DeviceInfo:
DumpMessage<DeviceInfo>(header);
break;
case MessageType::State:
DumpMessage<State>(header);
break;
case MessageType::Ping:
DumpMessage<Ping>(header);
break;
}
}
std::cerr << std::format("Read failed: {}", GetLastError()) << std::endl;
return 0;
}