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
359
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 David Herrmann <dh.herrmann@gmail.com>
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include "consoled.h"
#include "list.h"
#include "macro.h"
#include "util.h"
static int terminal_write_fn(term_screen *screen, void *userdata, const void *buf, size_t size) {
Terminal *t = userdata;
int r;
if (t->pty) {
r = pty_write(t->pty, buf, size);
if (r < 0)
return log_oom();
}
return 0;
}
static int terminal_pty_fn(Pty *pty, void *userdata, unsigned int event, const void *ptr, size_t size) {
Terminal *t = userdata;
int r;
switch (event) {
case PTY_CHILD:
log_debug("PTY child exited");
t->pty = pty_unref(t->pty);
break;
case PTY_DATA:
r = term_screen_feed_text(t->screen, ptr, size);
if (r < 0)
log_error_errno(r, "Cannot update screen state: %m");
workspace_dirty(t->workspace);
break;
}
return 0;
}
int terminal_new(Terminal **out, Workspace *w) {
_cleanup_(terminal_freep) Terminal *t = NULL;
int r;
assert(w);
t = new0(Terminal, 1);
if (!t)
return -ENOMEM;
t->workspace = w;
LIST_PREPEND(terminals_by_workspace, w->terminal_list, t);
r = term_parser_new(&t->parser, true);
if (r < 0)
return r;
r = term_screen_new(&t->screen, terminal_write_fn, t, NULL, NULL);
if (r < 0)
return r;
r = term_screen_set_answerback(t->screen, "systemd-console");
if (r < 0)
return r;
if (out)
*out = t;
t = NULL;
return 0;
}
Terminal *terminal_free(Terminal *t) {
if (!t)
return NULL;
assert(t->workspace);
if (t->pty) {
(void)pty_signal(t->pty, SIGHUP);
pty_close(t->pty);
pty_unref(t->pty);
}
term_screen_unref(t->screen);
term_parser_free(t->parser);
LIST_REMOVE(terminals_by_workspace, t->workspace->terminal_list, t);
free(t);
return NULL;
}
void terminal_resize(Terminal *t) {
uint32_t width, height, fw, fh;
int r;
assert(t);
width = t->workspace->width;
height = t->workspace->height;
fw = unifont_get_width(t->workspace->manager->uf);
fh = unifont_get_height(t->workspace->manager->uf);
width = (fw > 0) ? width / fw : 0;
height = (fh > 0) ? height / fh : 0;
if (t->pty) {
r = pty_resize(t->pty, width, height);
if (r < 0)
log_error_errno(r, "Cannot resize pty: %m");
}
r = term_screen_resize(t->screen, width, height);
if (r < 0)
log_error_errno(r, "Cannot resize screen: %m");
}
void terminal_run(Terminal *t) {
pid_t pid;
assert(t);
if (t->pty)
return;
pid = pty_fork(&t->pty,
t->workspace->manager->event,
terminal_pty_fn,
t,
term_screen_get_width(t->screen),
term_screen_get_height(t->screen));
if (pid < 0) {
log_error_errno(pid, "Cannot fork PTY: %m");
return;
} else if (pid == 0) {
/* child */
char **argv = (char*[]){
(char*)getenv("SHELL") ? : (char*)_PATH_BSHELL,
NULL
};
setenv("TERM", "xterm-256color", 1);
setenv("COLORTERM", "systemd-console", 1);
execve(argv[0], argv, environ);
log_error_errno(errno, "Cannot exec %s (%d): %m", argv[0], -errno);
_exit(1);
}
}
static void terminal_feed_keyboard(Terminal *t, idev_data *data) {
idev_data_keyboard *kdata = &data->keyboard;
int r;
if (!data->resync && (kdata->value == 1 || kdata->value == 2)) {
assert_cc(TERM_KBDMOD_CNT == (int)IDEV_KBDMOD_CNT);
assert_cc(TERM_KBDMOD_IDX_SHIFT == (int)IDEV_KBDMOD_IDX_SHIFT &&
TERM_KBDMOD_IDX_CTRL == (int)IDEV_KBDMOD_IDX_CTRL &&
TERM_KBDMOD_IDX_ALT == (int)IDEV_KBDMOD_IDX_ALT &&
TERM_KBDMOD_IDX_LINUX == (int)IDEV_KBDMOD_IDX_LINUX &&
TERM_KBDMOD_IDX_CAPS == (int)IDEV_KBDMOD_IDX_CAPS);
r = term_screen_feed_keyboard(t->screen,
kdata->keysyms,
kdata->n_syms,
kdata->ascii,
kdata->codepoints,
kdata->mods);
if (r < 0)
log_error_errno(r, "Cannot feed keyboard data to screen: %m");
}
}
void terminal_feed(Terminal *t, idev_data *data) {
switch (data->type) {
case IDEV_DATA_KEYBOARD:
terminal_feed_keyboard(t, data);
break;
}
}
static void terminal_fill(uint8_t *dst,
uint32_t width,
uint32_t height,
uint32_t stride,
uint32_t value) {
uint32_t i, j, *px;
for (j = 0; j < height; ++j) {
px = (uint32_t*)dst;
for (i = 0; i < width; ++i)
*px++ = value;
dst += stride;
}
}
static void terminal_blend(uint8_t *dst,
uint32_t width,
uint32_t height,
uint32_t dst_stride,
const uint8_t *src,
uint32_t src_stride,
uint32_t fg,
uint32_t bg) {
uint32_t i, j, *px;
for (j = 0; j < height; ++j) {
px = (uint32_t*)dst;
for (i = 0; i < width; ++i) {
if (!src || src[i / 8] & (1 << (7 - i % 8)))
*px = fg;
else
*px = bg;
++px;
}
src += src_stride;
dst += dst_stride;
}
}
typedef struct {
const grdev_display_target *target;
unifont *uf;
uint32_t cell_width;
uint32_t cell_height;
bool dirty;
} TerminalDrawContext;
static int terminal_draw_cell(term_screen *screen,
void *userdata,
unsigned int x,
unsigned int y,
const term_attr *attr,
const uint32_t *ch,
size_t n_ch,
unsigned int ch_width) {
TerminalDrawContext *ctx = userdata;
const grdev_display_target *target = ctx->target;
grdev_fb *fb = target->back;
uint32_t xpos, ypos, width, height;
uint32_t fg, bg;
unifont_glyph g;
uint8_t *dst;
int r;
if (n_ch > 0) {
r = unifont_lookup(ctx->uf, &g, *ch);
if (r < 0)
r = unifont_lookup(ctx->uf, &g, 0xfffd);
if (r < 0)
unifont_fallback(&g);
}
xpos = x * ctx->cell_width;
ypos = y * ctx->cell_height;
if (xpos >= fb->width || ypos >= fb->height)
return 0;
width = MIN(fb->width - xpos, ctx->cell_width * ch_width);
height = MIN(fb->height - ypos, ctx->cell_height);
term_attr_to_argb32(attr, &fg, &bg, NULL);
ctx->dirty = true;
dst = fb->maps[0];
dst += fb->strides[0] * ypos + sizeof(uint32_t) * xpos;
if (n_ch < 1) {
terminal_fill(dst,
width,
height,
fb->strides[0],
bg);
} else {
if (width > g.width)
terminal_fill(dst + sizeof(uint32_t) * g.width,
width - g.width,
height,
fb->strides[0],
bg);
if (height > g.height)
terminal_fill(dst + fb->strides[0] * g.height,
width,
height - g.height,
fb->strides[0],
bg);
terminal_blend(dst,
width,
height,
fb->strides[0],
g.data,
g.stride,
fg,
bg);
}
return 0;
}
bool terminal_draw(Terminal *t, const grdev_display_target *target) {
TerminalDrawContext ctx = { };
uint64_t age;
assert(t);
assert(target);
/* start up terminal on first frame */
terminal_run(t);
ctx.target = target;
ctx.uf = t->workspace->manager->uf;
ctx.cell_width = unifont_get_width(ctx.uf);
ctx.cell_height = unifont_get_height(ctx.uf);
ctx.dirty = false;
if (target->front) {
/* if the frontbuffer is new enough, no reason to redraw */
age = term_screen_get_age(t->screen);
if (age != 0 && age <= target->front->data.u64)
return false;
} else {
/* force flip if no frontbuffer is set, yet */
ctx.dirty = true;
}
term_screen_draw(t->screen, terminal_draw_cell, &ctx, &target->back->data.u64);
return ctx.dirty;
}
|