-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper.mm
77 lines (66 loc) · 1.66 KB
/
helper.mm
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
#include <iostream>
#include <string>
#include "json.hpp"
using json = nlohmann::json;
void ClipboardWait();
std::string paste();
int pid();
void focus(int);
int main() {
std::cout.setf(std::ios_base::unitbuf);
while (true) {
unsigned int ch, inMsgLen = 0;
std::string message = "";
// Read 4 bytes for data length
std::cin.read((char*)&inMsgLen, 4);
if (inMsgLen == 0) {
break;
}
else {
for (unsigned int i=0; i < inMsgLen; i++) {
ch = getchar();
message += ch;
}
}
auto request = json::parse(message);
// write message
json response;
if (request["method"] == "wait") {
ClipboardWait();
response["result"] = true;
}
else if (request["method"] == "read" || request["method"] == "read-next") {
if (request["method"] == "read-next") {
ClipboardWait();
}
std::string value = paste();
// limit to 1M
unsigned int max = 1024 * 1024 - 5000;
if (request.find("max") != request.end()) {
max = request["max"];
}
if (value.length() > max) {
response["error"] = "clipboard maximum size reached";
}
else {
response["result"] = value;
}
}
else if (request["method"] == "pid") {
response["result"] = pid();
}
else if (request["method"] == "focus") {
focus(request["pid"]);
}
else {
throw (request["method"], " method is not supported");
}
message = response.dump();
unsigned int len = message.length();
// Send 4 bytes of data length
std::cout.write((char*)&len, 4);
// Send the data
std::cout << message;
}
return 0;
}