-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheinkscreen.cpp
358 lines (291 loc) · 9.21 KB
/
einkscreen.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "einkscreen.h"
#include <QDebug>
#ifndef QT_NO_QWS_LINUXFB
//#include "qmemorymanager_qws.h"
#include "qwsdisplay_qws.h"
#include "qpixmap.h"
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/kd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <limits.h>
#include <signal.h>
#if !defined(Q_OS_DARWIN) && !defined(Q_OS_FREEBSD)
#include <linux/fb.h>
#endif
#define FB_SCREEN_REDRAW 0x46d1
#define FB_SCREEN_REDRAW_WAIT 0x46e2
QT_BEGIN_NAMESPACE
QEInkScreen::QEInkScreen(int displayId)
: QScreen(displayId)
, d_fd(-1), i_fd(-1)
, d_buffer((unsigned char *) MAP_FAILED)
, i_buffer((unsigned char *) MAP_FAILED)
, d_prebuffer(NULL)
, i_prebuffer(NULL)
{
}
bool QEInkScreen::connect(const QString &displaySpec)
{
_displaySpec = displaySpec;
const QStringList args = displaySpec.split(QLatin1Char(':'));
QString ddev = QLatin1String("/dev/fb0"), idev = QLatin1String("/dev/fb1");
int count = 0;
foreach(QString d, args) {
if (d.startsWith(QLatin1Char('/'))) {
switch (count) {
case 0:
ddev = d;
break;
case 1:
idev = d;
break;
default:
qCritical("Unknown third file argument");
return false;
}
count++;
}
}
w = h = 0;
if (!_openFramebuffer(ddev, &d_fd, &d_buffer, &d_prebuffer)) {
return false;
}
if (!_openFramebuffer(idev, &i_fd, &i_buffer, &i_prebuffer)) {
return false;
}
QRegExp mmWidthRx(QLatin1String("mmWidth=?(\\d+)"));
int dimIdxW = args.indexOf(mmWidthRx);
QRegExp mmHeightRx(QLatin1String("mmHeight=?(\\d+)"));
int dimIdxH = args.indexOf(mmHeightRx);
if (dimIdxW >= 0) {
mmWidthRx.exactMatch(args.at(dimIdxW));
physWidth = mmWidthRx.cap(1).toInt();
if (dimIdxH < 0)
physHeight = dh*physWidth/dw;
}
if (dimIdxH >= 0) {
mmHeightRx.exactMatch(args.at(dimIdxH));
physHeight = mmHeightRx.cap(1).toInt();
if (dimIdxW < 0)
physWidth = dw*physHeight/dh;
}
if (dimIdxW < 0 && dimIdxH < 0) {
if (w != 0 && h != 0) {
physWidth = w;
physHeight = h;
} else {
const int dpi = 72;
physWidth = qRound(dw * 25.4 / dpi);
physHeight = qRound(dh * 25.4 / dpi);
}
}
qDebug() << "QEInkScreen::connect - success";
return true;
}
bool QEInkScreen::initDevice()
{
#ifndef QT_NO_QWS_CURSOR
QScreenCursor::initSoftwareCursor();
#endif
blank(false);
return true;
}
void QEInkScreen::shutdownDevice()
{
}
bool QEInkScreen::_openFramebuffer(const QString &filename, int *fd, unsigned char **buffer, unsigned char **prebuffer)
{
/* open framebuffer file */
*fd = open(filename.toLatin1().constData(), O_RDWR);
if (*fd == -1) {
perror("QEInkScreen::connect");
qCritical("Error opening framebuffer device %s", qPrintable(filename));
return false;
}
/* check if screen geometry is already loaded */
if (w == 0 || h == 0) {
::fb_fix_screeninfo finfo;
::fb_var_screeninfo vinfo;
memset(&vinfo, 0, sizeof(vinfo));
memset(&finfo, 0, sizeof(finfo));
/* get fb fixed info */
if (ioctl(*fd, FBIOGET_FSCREENINFO, &finfo)) {
perror("QEinkScreen::connect");
qCritical("Error reading fixed information from device");
return false;
}
if (ioctl(*fd, FBIOGET_VSCREENINFO, &vinfo)) {
perror("QEInkScreen::connect");
qCritical("Error reading variable information from device");
return false;
}
w = dw = vinfo.xres;
h = dh = vinfo.yres;
d = vinfo.bits_per_pixel;
lstep = finfo.line_length;
if (!lstep) {
lstep = w * d / 8; // 8 bits in byte, sometimes pixels are smaller than 1 byte
}
physWidth = vinfo.width;
physHeight = vinfo.height;
data = (unsigned char *) -1;
buffer_len = finfo.smem_len;
}
/* map it to memory */
*buffer = (unsigned char *) mmap(0, buffer_len, PROT_READ | PROT_WRITE, MAP_SHARED, *fd, 0);
if (*buffer == MAP_FAILED) {
perror("QEInkScreen::connect");
qCritical("Error mapping framebuffer to memory");
return false;
}
*prebuffer = (unsigned char *) malloc(buffer_len);
if (!*prebuffer) {
perror("QEInkScreen::connect");
qCritical("Error allocating prebuffer");
return false;
}
return true;
}
void QEInkScreen::disconnect()
{
if (d_buffer != MAP_FAILED) {
munmap((char *) d_buffer, buffer_len);
}
if (i_buffer != MAP_FAILED) {
munmap((char *) i_buffer, buffer_len);
}
if (d_prebuffer) {
free(d_prebuffer);
d_prebuffer = NULL;
}
if (i_prebuffer) {
free(i_prebuffer);
i_prebuffer = NULL;
}
close(d_fd);
close(i_fd);
}
unsigned int QEInkScreen::_fbShift(int x, int y)
{
/* this fixes strange bug with Inch S5i framebuffer where first row is addressed as last */
y += 1;
if (y == h) {
y = 0;
}
return y * lstep + x / 2;
}
void QEInkScreen::_fillPoint(unsigned char colorSpec, int x, int y)
{
//qDebug() << "FillPoint()" << x << y;
unsigned int ad = _fbShift(x, y);
unsigned char c = d_prebuffer[ad];
if (x & 1) {
c &= 0xf0;
c |= colorSpec & 0xf;
} else {
c &= 0x0f;
c |= (colorSpec << 4) & 0xf0;
}
//qDebug() << "d_buffer: " << d_buffer;
//qDebug() << "i_buffer: " << i_buffer;
//qDebug() << "ad: " << ad << " / " << buffer_len;
//qDebug() << "lstep: " << lstep;
d_prebuffer[ad] = c;
i_prebuffer[ad] = (~c) & 0xff;
}
void QEInkScreen::solidFill(const QColor &color, const QRegion ®ion)
{
//qDebug() << "QEInkDisplay::solidFill " << color << region;
unsigned char colorSpec = qGray(color.rgb()) / 16;
/* let's just scan region and print specific pixels */
int x, y;
int x1, y1, x2, y2;
region.boundingRect().getCoords(&x1, &y1, &x2, &y2);
for (y = y1; y < y2; y++) {
for (x = x1; x < x2; x++) {
if (region.contains(QPoint(x, y))) {
_fillPoint(colorSpec, x, y);
}
}
}
}
void QEInkScreen::blit(const QImage &image, const QPoint &topLeft, const QRegion ®ion)
{
//qDebug() << "QEInkDisplay::blit " << image.size() << region << topLeft;
/* let's just scan region and print specific pixels */
int x, y;
int x1, y1, x2, y2;
region.boundingRect().getCoords(&x1, &y1, &x2, &y2);
if (QPoint(x1, y1) != topLeft) {
qDebug() << "Initial points differ: " << QPoint(x1, y1) << ", " << topLeft;
}
for (y = y1; y < y2; y++) {
for (x = x1; x < x2; x++) {
if (region.contains(QPoint(x, y))) {
unsigned char colorSpec = qGray(image.pixel(x - x1, y - y1)) / 16;
_fillPoint(colorSpec, x - x1 + topLeft.x(), y - y1 + topLeft.y());
}
}
}
}
void QEInkScreen::exposeRegion(QRegion region, int changing)
{
/* expose it as is and push update event to framebuffer */
QScreen::exposeRegion(region, changing);
memcpy(d_buffer, d_prebuffer, buffer_len);
memcpy(i_buffer, i_prebuffer, buffer_len);
ioctl(d_fd, FB_SCREEN_REDRAW, 0);
qDebug() << "Wait for redraw";
while (ioctl(d_fd, FB_SCREEN_REDRAW_WAIT, 0) > 0);
qDebug() << "Redraw completed";
}
/* this is stolen from QLinuxFbScreen with hope that this will not break everything */
void QEInkScreen::setMode(int nw, int nh, int nd)
{
qDebug("QEInkScreen::setMode(%d, %d, %d) - naive guys...", nw, nh, nd);
disconnect();
connect(_displaySpec);
exposeRegion(region(), 0);
return; // and let die
#if 0
if (d_fd == -1)
return;
::fb_fix_screeninfo finfo;
::fb_var_screeninfo vinfo;
//#######################
// Shut up Valgrind
memset(&vinfo, 0, sizeof(vinfo));
memset(&finfo, 0, sizeof(finfo));
//#######################
if (ioctl(d_fd, FBIOGET_VSCREENINFO, &vinfo)) {
perror("QLinuxFbScreen::setMode");
qFatal("Error reading variable information in mode change");
}
vinfo.xres=nw;
vinfo.yres=nh;
vinfo.bits_per_pixel=nd;
if (ioctl(d_fd, FBIOPUT_VSCREENINFO, &vinfo)) {
perror("QLinuxFbScreen::setMode");
qCritical("Error writing variable information in mode change");
}
if (ioctl(d_fd, FBIOGET_VSCREENINFO, &vinfo)) {
perror("QLinuxFbScreen::setMode");
qFatal("Error reading changed variable information in mode change");
}
if (ioctl(d_fd, FBIOGET_FSCREENINFO, &finfo)) {
perror("QLinuxFbScreen::setMode");
qFatal("Error reading fixed information");
}
disconnect();
connect(d_ptr->displaySpec);
exposeRegion(region(), 0);
#endif
}
#endif // QT_NO_QWS_LINUXFB