-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
148 lines (133 loc) · 3.31 KB
/
main.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
#include <iostream>
#include <AnalogueKeyboard.hpp>
#include <DigitalKeyboard.hpp>
#include <HidScancode.hpp>
#include <Server.hpp>
#include <ServerWebService.hpp>
#include <Socket.hpp>
#include <Task.hpp>
#include <Thread.hpp>
#include <WebSocketConnection.hpp>
using namespace soup;
struct Subscriber {};
struct AnnounceStateTask : public Task
{
std::string state;
AnnounceStateTask(std::string&& state)
: state(std::move(state))
{
}
void onTick() final
{
for (const auto& w : Scheduler::get()->workers)
{
if (w->type == Worker::SOCKET
&& static_cast<Socket*>(w.get())->custom_data.isStructInMap(Subscriber)
)
{
ServerWebService::wsSendText(*static_cast<Socket*>(w.get()), state);
}
}
setWorkDone();
}
};
static Server serv;
int main()
{
ServerWebService srv;
srv.should_accept_websocket_connection = [](Socket& s, const HttpRequest&, ServerWebService&)
{
s.custom_data.addStructToMap(Subscriber, Subscriber{});
return true;
};
if (!serv.bind(32312, &srv))
{
std::cout << "Failed to bind TCP/32312" << std::endl;
return 1;
}
std::cout << "Listening on TCP/32312" << std::endl;
Thread t([](Capture&&)
{
AnalogueKeyboard akbd; akbd.disconnected = true;
DigitalKeyboard dkbd;
std::vector<AnalogueKeyboard::ActiveKey> analogue_state;
std::string prev_state;
std::unordered_set<Key> activated_keys;
while (true)
{
if (akbd.disconnected)
{
for (auto& a : AnalogueKeyboard::getAll())
{
akbd = std::move(a);
break;
}
continue;
}
#if SOUP_WINDOWS
// Not blocking on Windows because DigitalKeyboard may be behind AnalogueKeyboard.
if (akbd.isPoll())
{
analogue_state = akbd.getActiveKeys();
}
else while (akbd.hid.hasReport())
{
analogue_state = akbd.getActiveKeys();
}
#else
analogue_state = akbd.getActiveKeys();
#endif
dkbd.update();
std::string state;
for (const auto& ak : analogue_state)
{
state.push_back('(');
state.append(std::to_string(static_cast<unsigned int>(ak.getHidScancode())));
state.push_back(':');
state.append(std::to_string(ak.getFValue()));
state.push_back(':');
state.push_back(dkbd.keys[ak.sk] ? '1' : '0');
state.push_back(')');
activated_keys.emplace(ak.sk);
}
if (state != prev_state)
{
// Explicitly state when a key has been released
std::string release;
for (const auto& ak : activated_keys)
{
bool still_active = false;
for (const auto& ak2 : analogue_state)
{
if (ak == ak2.sk)
{
still_active = true;
break;
}
}
if (!still_active)
{
release.push_back('(');
release.append(std::to_string(static_cast<unsigned int>(soup_key_to_hid_scancode(ak))));
release.append(":0:0)");
}
}
if (!release.empty())
{
serv.add<AnnounceStateTask>(std::move(release));
// Re-init activated_keys with only keys that were active this tick
activated_keys.clear();
for (const auto& ak : analogue_state)
{
activated_keys.emplace(ak.sk);
}
}
prev_state = state;
serv.add<AnnounceStateTask>(std::move(state));
}
}
});
serv.reduceAddWorkerDelay();
serv.run();
return 0;
}