-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.cpp
executable file
·183 lines (150 loc) · 5.41 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
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
#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>
using namespace swtfb;
int msg_q_id = 0x2257c;
ipc::Queue MSGQ(msg_q_id);
const int BYTES_PER_PIXEL = sizeof(uint16_t);
uint16_t *shared_mem;
void doUpdate(SwtFB &fb, const swtfb_update &s) {
auto mxcfb_update = s.update;
auto rect = mxcfb_update.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: init (same as GL16)
// 1: DU - direct update, fast
// 2: GC16 - high fidelity (slow)
// 3: GL16 - what the rm is using
// 8: highlight (same as high fidelity)
int waveform = mxcfb_update.waveform_mode;
if (waveform > 3 && waveform != 8) {
waveform = 3;
fb.ClearGhosting();
}
if (waveform < 0) {
waveform = 3;
fb.ClearGhosting();
}
// full = 1, partial = 0
auto update_mode = mxcfb_update.update_mode;
auto flags = update_mode & 0x1;
// 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 (waveform == /*init*/ 0 && update_mode == /* full */ 1) {
flags |= 2;
std::cerr << "SERVER: sync" << std::endl;
} else if (rect.left == 0 && rect.top > 1800 &&
waveform == /* grayscale */ 3 && update_mode == /* full */ 1) {
std::cerr << "server sync, x2: " << rect.width << " y2: " << rect.height
<< std::endl;
flags |= 2;
}
if (waveform == /* fast */ 1 && update_mode == /* partial */ 0) {
// 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)
static void (*qImageCtor)(void *that, int x, int y, int f) = 0;
// QImage(uchar*, width, height, bytesperline, format)
static void (*qImageCtorWithBuffer)(void *that, uint8_t *, int32_t x, int32_t y,
int32_t bytes, int format, void (*)(void *),
void *) = 0;
static void _libhook_init() __attribute__((constructor));
static void _libhook_init() {
shared_mem = ipc::get_shared_buffer();
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_");
}
bool FIRST_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);
}
int server_main(int, char **, char **) {
SwtFB fb;
printf("WAITING FOR SEND UPDATE ON MSG Q\n");
while (true) {
auto buf = MSGQ.recv();
switch (buf.mtype) {
case ipc::UPDATE_t:
doUpdate(fb, buf);
break;
case ipc::XO_t: {
int width = buf.xochitl_update.x2 - buf.xochitl_update.x1 + 1;
int height = buf.xochitl_update.y2 - buf.xochitl_update.y1 + 1;
QRect rect(buf.xochitl_update.x1, buf.xochitl_update.y1, width, height);
fb.SendUpdate(rect, buf.xochitl_update.waveform,
buf.xochitl_update.flags);
} break;
case ipc::WAIT_t: {
fb.WaitForLastUpdate();
sem_t* sem = sem_open(buf.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;
}
}
}
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
const char* 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);
return func_main(server_main, argc, argv, init, fini, rtld_fini, stack_end);
};
};