Repository navigation
Expand file tree
/
Copy pathProgram.cpp
More file actions
244 lines (201 loc) · 6.4 KB
/
Copy pathProgram.cpp
File metadata and controls
244 lines (201 loc) · 6.4 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
/**
*Copyright Kira Mint
*/
#include <atomic>
#include <bitset>
#include <cerrno>
#include <chrono>
#include <csignal>
#include <cstring>
#include <exception>
#include <iostream>
#include <memory>
#include <thread>
#include <vector>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/wait.h>
#include <opencv2/opencv.hpp>
#include "driver/Eink.h"
#include "driver/X11Redirect.h"
using namespace std;
Eink *eInkInstance;
X11Redirect *redirect;
pid_t xvfbPid, appPid, parentPid;
int signalCalled = 0;
int updateRateMs = (1 / 10); // FPS
long lastPressed = 0; // Debounce
auto startTime = std::chrono::system_clock::now();
atomic<bool> cancellationToken(false);
atomic<int> xTouch(0);
atomic<int> yTouch(0);
atomic<bool> isTouch(false);
sem_t *xvfbRun; // Wait Xvfb start
sem_t *mainLoopRun; // Wait main loop start
// Quit handler
void signal_handler(int signum)
{
if (signalCalled == 0)
{
signalCalled = true;
std::cout << endl
<< "Signal: SIGINT received, terminating..." << std::endl;
cout << "Stopping event loop" << endl;
cancellationToken = true;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
try
{
cout << "Killing Eink driver" << endl;
;
if (eInkInstance != nullptr)
{
eInkInstance->destroy();
delete eInkInstance;
}
}
catch (exception &e)
{
cerr << "Exception caught:" << e.what() << endl;
}
cout << "Killing xvfb" << endl;
kill(xvfbPid, SIGINT);
cout << "Killing x11app" << endl;
kill(appPid, SIGINT);
int status;
pid_t wpid;
cout << "Waiting for xvfb and x11app die..." << endl;
while ((wpid = wait(&status)) > 0)
{
std::cout << "Parent: child " << wpid << " exited with status " << status << "\n";
}
std::this_thread::sleep_for(std::chrono::seconds(1));
exit(0);
}
if (signalCalled > 5)
{
cerr << endl
<< "Terminated." << endl;
exit(1);
}
// Force quit counter
signalCalled++;
cout << endl
<< "Closing... be patient (•́ω•̀ o) (Ctrl+C 5 times to terminate)."
<< endl;
}
// Touch Callback
void scrCallBack(vector<Eink::EinkTouchPoint> touchData)
{
long thisPress = std::chrono::duration_cast<std::chrono::milliseconds>(chrono::system_clock::now() - startTime).count();
// Debounce
if (thisPress - lastPressed > 200)
{
auto touch = touchData[0];
xTouch.store(touch.xDirect);
yTouch.store(touch.yDirect);
isTouch.store(true);
lastPressed = std::chrono::duration_cast<std::chrono::milliseconds>(chrono::system_clock::now() - startTime).count();
}
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "Usage: " << argv[0] << " <x11-Apps> [args...]"<< endl;
return 1;
}
cout << "App params: " << argv[1] << " " << &argv[1] << endl;
// Kill if unexpected quit last time
system("killall Xvfb");
system((std::string("killall ") + std::string(argv[1])).c_str());
// Unlink if unexpected quit last time
sem_unlink("/eink_xvfb_run");
sem_unlink("/eink_loop_run");
// Init semaphore
xvfbRun = sem_open("/eink_xvfb_run", O_CREAT | O_EXCL, 0644, 0);
mainLoopRun = sem_open("/eink_loop_run", O_CREAT | O_EXCL, 0644, 0);
if (xvfbRun == nullptr || mainLoopRun == nullptr)
{
std::cerr << "sem init error" << endl;
std::cerr << "Error: " << strerror(errno) << std::endl;
return -1;
}
// Handel quit
signal(SIGINT, signal_handler);
parentPid = getpid();
setenv("DISPLAY", ":99", true);
pid_t pid = fork();
if (pid == 0) // Handle Xvfp
{
const char *xvfbArgs[] = {"Xvfb", ":99", "-screen", "0", "250x122x24", nullptr};
sem_post(xvfbRun);
cout << "Xvfb start" << endl;
execvp("Xvfb", const_cast<char *const *>(xvfbArgs));
kill(parentPid, SIGINT);
exit(0);
}
if (pid > 0) // Handle Main
{
sem_wait(xvfbRun);
appPid = fork();
if (appPid == 0)
{
sem_wait(mainLoopRun);
cout << "Running x11app: " << argv[1] << " " << &argv[1] << endl;
execvp(argv[1], &argv[1]);
kill(parentPid, SIGINT);
exit(0);
}
cout << "X11App will run at pid: " << appPid << endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
cout << "X11Redirect initializing..." << endl;
redirect = new X11Redirect(":0");
cout << "Eink driver initializing..." << endl;
eInkInstance = new Eink();
auto ink = eInkInstance->getDisplay();
auto touch = eInkInstance->getTouch();
touch->registerCallback(scrCallBack);
sem_post(mainLoopRun);
cout << "Entering main event loop" << endl;
while (!cancellationToken)
{
try
{
int status;
pid_t result;
// If x11app quit itself
result = waitpid(appPid, &status, WNOHANG);
if (result != 0)
{
if (WIFEXITED(status))
{
printf("X11App exited with code %d\n", WEXITSTATUS(status));
signal_handler(SIGINT);
break;
}
}
auto frame = redirect->xCapture();
cv::imwrite("eInkDriverDebug.jpg", frame);
ink->updateImage(frame, Eink::UpdatePart);
if (isTouch.load())
{
cout << "Touch event: " << "(" << xTouch.load() << "," << yTouch.load() << ")"<<endl;
redirect->xClick(xTouch.load(), yTouch.load());
isTouch.store(false);
}
std::this_thread::sleep_for(std::chrono::milliseconds(updateRateMs));
}
catch (exception &e)
{
cout << "Exception: " << e.what() << "\n";
signal_handler(SIGINT);
}
}
// Let signal_handler to terminate.
while (true)
{
std::this_thread::sleep_for(std::chrono::seconds(60));
}
}
return 0;
};