-
Notifications
You must be signed in to change notification settings - Fork 1
/
evince-restorer.cpp
207 lines (189 loc) · 5.66 KB
/
evince-restorer.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
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
#include <signal.h>
#include <unistd.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstdio>
#include <fstream>
#include <future>
#include <iostream>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
#include <thread>
#include <vector>
using namespace std::chrono_literals;
using Clock = std::chrono::steady_clock;
const char logData[]{"./logData.txt"};
const auto timeOut = 15s;
static std::array<std::string, 2> logs;
static unsigned int idx = 0;
std::string exec(const char *cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
std::vector<int> readPids(const std::string &s) {
std::vector<int> pids;
int x;
std::stringstream ss{s};
while ((ss >> x)) {
pids.push_back(x);
}
return pids;
}
void replaceAll(std::string &str, const std::string &from,
const std::string &to) {
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos +=
to.length(); // Handles case where 'to' is a substring of 'from'
}
}
static inline void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
[](int ch) { return !std::isspace(ch); })
.base(),
s.end());
}
std::string getOpenedPdfs() {
std::string cmd{
"lsof -p 0,$(pidof evince | sed \"s| |,|g\") | grep -o \"/.*.pdf$\""};
auto s = exec(cmd.c_str());
rtrim(s);
std::cout << "getOpened:" << s << std::endl;
return s;
}
std::string getOpenedPdfs(const char *path) {
std::string result, buffer;
std::ifstream logFile(path);
if (logFile.is_open()) {
while (std::getline(logFile, buffer)) {
if (result.size() > 0) {
result += '\n';
}
result += buffer;
}
} else {
std::cout << "can't open file " << path << std::endl;
result = "";
}
return result;
}
void writeOpenedPdfs(const char *path, const std::string &pdfs) {
std::ofstream logFile(path);
if (logFile.is_open()) {
logFile << pdfs;
std::cout << "written:\n[[[" << pdfs << "]]]\n";
} else {
std::cout << "can't open file " << path << std::endl;
}
}
void launchEvince(std::string pdfs) {
std::cout << "restoring last evince session:" << std::endl;
std::cout << "pdfs:\n[[[" << pdfs << "]]]\n";
if (pdfs.size() == 0) {
system("evince &");
std::cout << "empty session" << std::endl;
} else {
replaceAll(pdfs, "\'", "\'\"\'\"\'");
replaceAll(pdfs, "\n", "\' & evince \'");
pdfs = "evince \'" + pdfs + "\' &";
std::cout << "calling:\n" << pdfs << std::endl;
system(pdfs.c_str());
}
auto openedPdfs = getOpenedPdfs();
while (openedPdfs.size() <= 0) {
openedPdfs = getOpenedPdfs();
std::this_thread::sleep_for(0.5s);
}
std::cout << "at least one evince windows launched, openedPdfs:\n"
<< openedPdfs << std::endl;
}
decltype(auto) restore(const char *path) {
auto pdfs = getOpenedPdfs();
auto savedPdfs = getOpenedPdfs(path);
if (pdfs.size() == 0 || savedPdfs.size() > 0) {
return savedPdfs;
} else {
return pdfs;
}
}
void log(std::chrono::seconds timeOut, const char *path, std::string &&pdfs) {
logs[0] = std::move(pdfs);
logs[1] = logs[0];
std::mutex mtx;
std::unique_lock<std::mutex> lck(mtx);
for (pdfs = getOpenedPdfs(); pdfs.size() > 0; pdfs = getOpenedPdfs()) {
logs[idx] = std::move(pdfs);
idx ^= 1;
std::this_thread::sleep_for(timeOut);
}
writeOpenedPdfs(path, logs[idx]);
}
void intHandler(int signum) {
std::cout << "signal: " << signum << std::endl;
auto pdfs = getOpenedPdfs();
writeOpenedPdfs(logData, pdfs);
auto eviPids = readPids(exec("pidof evince"));
for (auto &v : eviPids) {
std::cout << "detected evince: " << v << std::endl;
std::cout << "killing: " << v << std::endl;
kill(v, SIGTERM);
}
eviPids = readPids(exec("pidof evince"));
for (auto &v : eviPids) {
std::cout << "detected evince: " << v << std::endl;
std::cout << "killing: " << v << std::endl;
kill(v, SIGKILL);
}
exit(0);
}
void termHandler(int signum) {
std::cout << "signal: " << signum << std::endl;
auto pdfs = getOpenedPdfs();
if (pdfs.size() > 0) {
// store and close session
intHandler(SIGINT);
} else {
// fast reopen
auto pdfs = logs[idx];
std::cout << "fast reopen:\n[[[" << pdfs << "]]]" << std::endl;
launchEvince(pdfs);
}
}
void detectAndKill() {
auto resPids = readPids(exec("pidof evince-restorer"));
if (resPids.size() > 1) {
auto thisPid = getpid();
for (auto &v : resPids) {
std::cout << "detected restorer: " << v << std::endl;
if (v != thisPid) {
std::cout << "send kill: " << v << std::endl;
kill(v, SIGTERM);
}
}
std::cout << "exit: " << thisPid << std::endl;
exit(0);
}
}
int main() {
signal(SIGINT, intHandler);
signal(SIGTERM, termHandler);
detectAndKill();
auto pdfs = restore(logData);
launchEvince(pdfs);
log(timeOut, logData, std::move(pdfs));
return 0;
}