-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathscreen.cpp
340 lines (282 loc) · 9.17 KB
/
screen.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
#include "screen.h"
#include "conf.h"
using namespace rmlib;
namespace {
// TODO: move
// \{
inline int
myCeil(int val, int div) {
if (div == 0) {
return 0;
} return (val + div - 1) / div;
}
inline uint16_t
color2brightness(uint32_t color) {
int r = 0xFF & (color >> (BITS_PER_RGB * 2));
int g = 0xFF & (color >> BITS_PER_RGB);
int b = 0xFF & (color >> 0);
return (54 * r + 182 * g + 19 * b) / 255;
}
enum GrayMode {
Black = 0,
Dither = 1,
White = 2,
};
GrayMode
brightness2gray(uint16_t brightness) {
if (brightness <= 85) {
return Black;
}
if (brightness < 170) {
return Dither;
}
return White;
}
void
initMouseBuf(std::array<char, 6>& buf, Point loc) {
constexpr char esc_char = '\x1b';
loc.x /= CELL_WIDTH;
loc.y /= CELL_HEIGHT;
char cx = 33 + loc.x;
char cy = 33 + loc.y;
buf = { esc_char, '[', 'M', 32, cx, cy };
}
// \}
} // namespace
std::unique_ptr<rmlib::RenderObject>
Screen::createRenderObject() const {
return std::make_unique<ScreenRenderObject>(*this);
}
void
ScreenRenderObject::update(const Screen& newWidget) {
// TODO: correct location?
for (int i = 0; i < newWidget.term->lines; i++) {
if (newWidget.term->line_dirty[i]) {
markNeedsDraw(/* full */ false);
}
}
if (newWidget.isLandscape != widget->isLandscape) {
markNeedsLayout();
markNeedsDraw(/* full */ true);
}
widget = &newWidget;
}
rmlib::Size
ScreenRenderObject::doLayout(const rmlib::Constraints& constraints) {
const auto size = constraints.isBounded() ? constraints.max : constraints.min;
assert(size.width != 0 && size.height != 0);
if (widget->isLandscape) {
term_resize(widget->term, size.height, size.width, /* report */ true);
} else {
term_resize(widget->term, size.width, size.height, /* report */ true);
}
return size;
}
bool
ScreenRenderObject::shouldRefresh() const {
return isFullDraw() || widget->term->shouldClear ||
(widget->autoRefresh > 0 && numUpdates > widget->autoRefresh);
}
rmlib::UpdateRegion
ScreenRenderObject::doDraw(rmlib::Rect rect, rmlib::Canvas& canvas) {
auto& term = *widget->term;
if ((term.mode & MODE_CURSOR) != 0U) {
term.line_dirty[term.cursor.y] = true;
}
Rect currentRect;
const auto maybeDraw = [&](bool last = false) {
if (currentRect.empty() || shouldRefresh()) {
return;
}
bool useA2 = widget->isLandscape
? term.lines * CELL_HEIGHT <= currentRect.width()
: term.lines * CELL_HEIGHT <= currentRect.height();
fb->doUpdate(currentRect,
useA2 ? fb::Waveform::A2 : fb::Waveform::DU,
useA2 ? fb::UpdateFlags::None : fb::UpdateFlags::Priority);
currentRect = {};
numUpdates++;
};
for (int line = 0; line < term.lines; line++) {
if (isFullDraw() || term.line_dirty[line]) {
currentRect |= drawLine(canvas, rect, term, line);
} else {
maybeDraw();
}
}
maybeDraw(/* last */ true);
if (shouldRefresh()) {
term.shouldClear = false;
numUpdates = 0;
return {rect, fb::Waveform::GC16, fb::UpdateFlags::FullRefresh};
}
return {};
}
rmlib::Rect
ScreenRenderObject::drawLine(rmlib::Canvas& canvas,
rmlib::Rect rect,
terminal_t& term,
int line) const {
const bool isLandscape = widget->isLandscape;
// x in landscape, y in portrait.
int zStart =
isLandscape
? term.height - (term.marginTop + line * CELL_HEIGHT) + rect.topLeft.x
: term.marginTop + line * CELL_HEIGHT + rect.topLeft.y;
for (int col = 0; col < term.cols; col++) {
int marginLeft = term.marginLeft + col * CELL_WIDTH +
(isLandscape ? rect.topLeft.y : rect.topLeft.x);
auto& cell = term.cells[line][col];
if (cell.has_pixmap) {
// TODO
continue;
}
auto colorPair = cell.color_pair; // copy
/* check wide character or not */
int glyphWidth = (cell.width == HALF) ? CELL_WIDTH : CELL_WIDTH * 2;
int bdfPadding =
myCeil(glyphWidth, BITS_PER_BYTE) * BITS_PER_BYTE - glyphWidth;
if (cell.width == WIDE) {
bdfPadding += CELL_WIDTH;
}
/* check cursor position */
if ((((term.mode & MODE_CURSOR) != 0U) && line == term.cursor.y) &&
(col == term.cursor.x ||
(cell.width == WIDE && (col + 1) == term.cursor.x) ||
(cell.width == NEXT_TO_WIDE && (col - 1) == term.cursor.x))) {
colorPair.fg = DEFAULT_BG;
colorPair.bg = (/*!vt_active &&*/ BACKGROUND_DRAW) != 0U ? PASSIVE_CURSOR_COLOR
: ACTIVE_CURSOR_COLOR;
}
// lookup pixels
const auto bgBright = color2brightness(color_list[colorPair.bg]);
const auto fgBright = color2brightness(color_list[colorPair.fg]);
auto bgGray = brightness2gray(bgBright);
auto fgGray = brightness2gray(fgBright);
// Don't draw same color
if (fgGray == bgGray) {
if (fgBright < bgBright) {
if (fgGray != 0) {
fgGray = static_cast<GrayMode>(fgGray - 1);
} else {
bgGray = static_cast<GrayMode>(bgGray + 1);
}
} else {
if (bgGray != 0) {
bgGray = static_cast<GrayMode>(bgGray - 1);
} else {
fgGray = static_cast<GrayMode>(fgGray + 1);
}
}
}
for (int h = 0; h < CELL_HEIGHT; h++) {
/* if UNDERLINE attribute on, swap bg/fg */
if ((h == (CELL_HEIGHT - 1)) &&
((cell.attribute & attr_mask[ATTR_UNDERLINE]) != 0)) {
std::swap(bgGray, fgGray);
}
for (int w = 0; w < CELL_WIDTH; w++) {
int pos = isLandscape ? (marginLeft + w) * canvas.lineSize() +
(zStart - h) * canvas.components()
: (marginLeft + w) * canvas.components() +
(zStart + h) * canvas.lineSize();
/* set fg or bg */
const auto* glyph = (cell.attribute & ATTR_BOLD) != 0
? cell.glyph.boldp
: cell.glyph.regularp;
const auto grayMode =
(glyph->bitmap[h] & (0x01 << (bdfPadding + CELL_WIDTH - 1 - w))) != 0U
? fgGray
: bgGray;
int pixel = 0;
switch (grayMode) {
case White:
pixel = 0; // 0xFFFF;
break;
case Dither:
pixel = (h % 2) == (w % 2) ? 0x0 : 0xFFFF;
break;
case Black:
pixel = 0xFFFF; // 0;
break;
}
/* update copy buffer only */
memcpy(canvas.getMemory() + pos, &pixel, canvas.components());
}
}
}
term.line_dirty[line] =
((term.mode & MODE_CURSOR) != 0u) && term.cursor.y == line;
return isLandscape ? Rect{ { zStart - CELL_HEIGHT, 0 },
{ zStart, rect.height() - 1 } }
: Rect{ { 0, zStart },
{ rect.width() - 1, zStart + CELL_HEIGHT - 1 } };
}
template<typename Ev>
void
ScreenRenderObject::handleTouchEvent(const Ev& ev) {
if ((widget->term->mode & ALL_MOUSE_MODES) == 0) {
return;
}
// TODO: two finger scrolling?
// const auto lastFingers = kb.gestureCtrlr.getCurrentFingers();
// if constexpr (std::is_same_v<Event, TouchEvent>) {
// const auto [gestures, _] = kb.gestureCtrlr.handleEvents({ ev });
// for (const auto& gesture : gestures) {
// if (std::holds_alternative<SwipeGesture>(gesture)) {
// handleGesture(kb, std::get<SwipeGesture>(gesture));
// }
// }
// }
const auto slot = [&ev]() {
if constexpr (std::is_same_v<Ev, input::PenEvent>) {
(void)ev;
return 0x1000;
} else {
return ev.slot;
}
}();
const auto scaledLoc = ev.location - getRect().topLeft;
const auto rotatedLoc =
widget->isLandscape
? Point{ scaledLoc.y, getRect().width() - scaledLoc.x }
: scaledLoc;
std::array<char, 6> buf{};
initMouseBuf(buf, rotatedLoc);
// Mouse down on first finger if mouse is not down.
if (ev.isDown() && mouseSlot == -1 /*&& lastFingers == 0*/) {
mouseSlot = slot;
// Send mouse down code
buf[3] += 0; // mouse button 1
write(widget->term->fd, buf.data(), buf.size());
} else if ((ev.isUp() && slot == mouseSlot) /*||
(kb.mouseSlot != -1 && kb.gestureCtrlr.getCurrentFingers() > 1)*/) {
// Mouse up after finger lift or second finger down (for scrolling).
mouseSlot = -1;
// Send mouse up code
buf[3] += 3; // mouse release
write(widget->term->fd, buf.data(), buf.size());
} else if (mouseSlot == slot && lastMousePos != rotatedLoc &&
(widget->term->mode & MODE_MOUSE_MOVE) != 0) {
buf[3] += 32; // mouse move
write(widget->term->fd, buf.data(), buf.size());
}
lastMousePos = rotatedLoc;
}
void
ScreenRenderObject::handleInput(const rmlib::input::Event& ev) {
std::visit(
[this](const auto& ev) {
if constexpr (rmlib::input::is_pointer_event<
std::decay_t<decltype(ev)>>) {
if (getRect().contains(ev.location)) {
handleTouchEvent(ev);
}
}
},
ev);
}
void
ScreenRenderObject::doRebuild(AppContext& ctx, const BuildContext& /*buildContext*/) {
this->fb = &ctx.getFramebuffer();
}