- Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmain.cpp
More file actions
Latest commit
executable file
·211 lines (173 loc) · 5.96 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·211 lines (173 loc) · 5.96 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
#include<fcntl.h>
#include"../shared/ipc.cpp"
#include"../shared/swtfb.cpp"
#include<linux/input.h>
#include<sys/poll.h>
#include<sys/prctl.h>
#include<unistd.h>
#ifndef _GNU_SOURCE
#define_GNU_SOURCE
#endif
#include<cstdio>
#include<dlfcn.h>
#include<mutex>
#include<stdint.h>
#include<semaphore.h>
#include<thread>
#include<QCoreApplication>
#include<QTouchEvent>
#include<systemd/sd-daemon.h>
usingnamespaceswtfb;
int msg_q_id = 0x2257c;
ipc::Queue MSGQ(msg_q_id);
constintBYTES_PER_PIXEL = sizeof(uint16_t);
uint16_t *shared_mem;
voiddoUpdate(SwtFB &fb, const swtfb_update &s) {
// Taken from KOreader
staticconstintWAVEFORM_MODE_INIT = 0;
staticconstintWAVEFORM_MODE_DU = 1;
staticconstintWAVEFORM_MODE_GC16 = 2;
staticconstintWAVEFORM_MODE_GL16 = 3;
staticconstintWAVEFORM_MODE_A2 = 4;
auto data = s.mdata.update;
constauto& rect = data.update_region;
#ifdef DEBUG_DIRTY
std::cerr << "Dirty Region: " << rect.left << "" << rect.top << ""
<< rect.width << "" << rect.height << endl;
#endif
// There are three update modes on the rm2. But they are mapped to the five
// rm1 modes as follows:
//
// 0: DU
// 1: GC16: High fidelity / init with full refresh flag.
// 2: GL16: Faster grayscale
// 3: A2?: Pan & Zoom mode, 3.3+ GC16 mode
// full = 1, partial = 0
int flags = data.update_mode == UPDATE_MODE_FULL ? 0x1 : 0x0;
// old translation
int waveform = [&] {
switch (data.waveform_mode) {
caseWAVEFORM_MODE_DU:
return1;
caseWAVEFORM_MODE_GC16:
if (fb.remapWave2to5) {
return5;
}
return2;
default:
fb.ClearGhosting();
return3;
case8:
return8;
}
}();
// TODO: Get sync from client (wait for second ioctl? or look at stack?)
// There are only two occasions when the original rm1 library sets sync to
// true. Currently we detect them by the other data. Ideally we should
// correctly handle the corresponding ioctl (empty rect and flags == 2?).
if (data.waveform_mode == WAVEFORM_MODE_INIT &&
data.update_mode == UPDATE_MODE_FULL) {
flags |= 2;
std::cerr << "SERVER: sync" << std::endl;
} elseif (rect.left == 0 && rect.top > 1800 &&
(data.waveform_mode == WAVEFORM_MODE_GL16 ||
data.waveform_mode == WAVEFORM_MODE_GC16) &&
data.update_mode == UPDATE_MODE_FULL) {
std::cerr << "server sync, x2: " << rect.width << " y2: " << rect.height
<< std::endl;
flags |= 2;
}
if (data.waveform_mode == WAVEFORM_MODE_DU &&
data.update_mode == UPDATE_MODE_PARTIAL) {
// fast draw
flags |= 4;
}
#ifdef DEBUG
std::cerr << "doUpdate " << endl;
cerr << "mxc: waveform_mode " << mxcfb_update.waveform_mode << endl;
cerr << "mxc: update mode " << mxcfb_update.update_mode << endl;
cerr << "mxc: update marker " << mxcfb_update.update_marker << endl;
cerr << "final: waveform " << waveform;
cerr << " flags " << flags << endl << endl;
#endif
fb.DrawRaw(rect.left, rect.top, rect.width, rect.height, waveform, flags);
}
extern"C" {
// QImage(width, height, format)
staticvoid (*qImageCtor)(void *that, int x, int y, int f) = 0;
// QImage(uchar*, width, height, bytesperline, format)
staticvoid (*qImageCtorWithBuffer)(void *that, uint8_t *, int32_t x, int32_t y,
int32_t bytes, int format, void (*)(void *),
void *) = 0;
staticvoid_libhook_init() __attribute__((constructor));
staticvoid_libhook_init() {
qImageCtor = (void (*)(void *, int, int, int))dlsym(
RTLD_NEXT, "_ZN6QImageC1EiiNS_6FormatE");
qImageCtorWithBuffer = (void (*)(
void *, uint8_t *, int32_t, int32_t, int32_t, int, void (*)(void *),
void *))dlsym(RTLD_NEXT, "_ZN6QImageC1EPhiiiNS_6FormatEPFvPvES2_");
}
boolFIRST_ALLOC = true;
void_ZN6QImageC1EiiNS_6FormatE(void *that, int x, int y, int f) {
if (x == WIDTH && y == HEIGHT && FIRST_ALLOC) {
fprintf(stderr, "REPLACING THE IMAGE with shared memory\n");
FIRST_ALLOC = false;
qImageCtorWithBuffer(that, (uint8_t *)shared_mem, WIDTH, HEIGHT,
WIDTH * BYTES_PER_PIXEL, f, nullptr, nullptr);
return;
}
qImageCtor(that, x, y, f);
}
intserver_main(int, char **, char **) {
SwtFB fb;
if (!fb.setFunc()) {
return255;
}
shared_mem = ipc::get_shared_buffer();
fb.initQT();
fprintf(stderr, "WAITING FOR SEND UPDATE ON MSG Q\n");
sd_notify(0, "READY=1");
while (true) {
auto buf = MSGQ.recv();
switch (buf.mtype) {
case ipc::UPDATE_t:
doUpdate(fb, buf);
break;
case ipc::XO_t: {
const xochitl_data& data = buf.mdata.xochitl_update;
int width = data.x2 - data.x1 + 1;
int height = data.y2 - data.y1 + 1;
QRect rect(data.x1, data.y1, width, height);
fb.SendUpdate(rect, data.waveform, data.flags);
} break;
case ipc::WAIT_t: {
fb.WaitForLastUpdate();
sem_t* sem = sem_open(buf.mdata.wait_update.sem_name, O_CREAT, 0644, 0);
if (sem != NULL) {
sem_post(sem);
sem_close(sem);
}
} break;
default:
std::cerr << "Error, unknown message type" << std::endl;
}
}
return0;
}
int__libc_start_main(int (*)(int, char **, char **), int argc,
char **argv, int (*init)(int, char **, char **),
void (*fini)(void), void (*rtld_fini)(void),
void *stack_end) {
printf("STARTING RM2FB\n");
auto func_main =
(decltype(&__libc_start_main))dlsym(RTLD_NEXT, "__libc_start_main");
// Since we preload the library in the Xochitl binary, the process will
// be called 'xochitl' by default. Change this to avoid confusing launchers
constchar* proc_name = "rm2fb-server";
size_t argv0_len = strlen(argv[0]);
strncpy(argv[0], proc_name, argv0_len);
argv[0][argv0_len] = 0;
prctl(PR_SET_NAME, proc_name);
returnfunc_main(server_main, argc, argv, init, fini, rtld_fini, stack_end);
};
};