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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
/*
* kmscon - Terminal
*
* Copyright (c) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
* Copyright (c) 2011 University of Tuebingen
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* Terminal
* A terminal gets assigned an input stream and several output objects and then
* runs a fully functional terminal emulation on it.
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "conf.h"
#include "console.h"
#include "eloop.h"
#include "log.h"
#include "pty.h"
#include "static_misc.h"
#include "terminal.h"
#include "text.h"
#include "unicode.h"
#include "uterm.h"
#include "vte.h"
#define LOG_SUBSYSTEM "terminal"
struct screen {
struct kmscon_dlist list;
struct uterm_display *disp;
struct uterm_screen *screen;
struct kmscon_font *font;
struct kmscon_text *txt;
};
struct kmscon_terminal {
unsigned long ref;
struct ev_eloop *eloop;
struct uterm_video *video;
struct uterm_input *input;
bool opened;
struct kmscon_dlist screens;
unsigned int min_cols;
unsigned int min_rows;
bool redraw;
struct kmscon_console *console;
struct kmscon_vte *vte;
struct kmscon_pty *pty;
kmscon_terminal_event_cb cb;
void *data;
};
static void draw_all(struct ev_eloop *eloop, void *unused, void *data)
{
struct kmscon_terminal *term = data;
struct uterm_screen *screen;
struct kmscon_dlist *iter;
struct screen *ent;
ev_eloop_unregister_idle_cb(term->eloop, draw_all, term);
term->redraw = false;
kmscon_dlist_for_each(iter, &term->screens) {
ent = kmscon_dlist_entry(iter, struct screen, list);
screen = ent->screen;
kmscon_console_draw(term->console, ent->txt);
uterm_screen_swap(screen);
}
}
static void schedule_redraw(struct kmscon_terminal *term)
{
int ret;
if (term->redraw)
return;
ret = ev_eloop_register_idle_cb(term->eloop, draw_all, term);
if (ret)
log_warn("cannot schedule redraw");
else
term->redraw = true;
}
/*
* Resize terminal
* We support multiple monitors per terminal. As some software-rendering
* backends to not support scaling, we always use the smalles cols/rows that are
* provided so wider displays will have black margins.
* This can be extended to support scaling but that would mean we need to check
* whether the text-renderer backend supports that, first (TODO).
*
* If @force is true, then the console/pty are notified even though the size did
* not changed. If @notify is false, then console/pty are not notified even
* though the size might have changed. force = true and notify = false doesn't
* make any sense, though.
*/
static void terminal_resize(struct kmscon_terminal *term,
unsigned int cols, unsigned int rows,
bool force, bool notify)
{
bool resize = false;
if (!term->min_cols || (cols > 0 && cols < term->min_cols)) {
term->min_cols = cols;
resize = true;
}
if (!term->min_rows || (rows > 0 && rows < term->min_rows)) {
term->min_rows = rows;
resize = true;
}
if (!notify || (!resize && !force))
return;
/* shrinking always succeeds */
kmscon_console_resize(term->console, term->min_cols, term->min_rows);
kmscon_pty_resize(term->pty, term->min_cols, term->min_rows);
}
static int add_display(struct kmscon_terminal *term, struct uterm_display *disp)
{
struct kmscon_dlist *iter;
struct screen *scr;
int ret;
unsigned int cols, rows;
const struct kmscon_font_attr attr = { "", 0, 20, false, false, 0, 0 };
kmscon_dlist_for_each(iter, &term->screens) {
scr = kmscon_dlist_entry(iter, struct screen, list);
if (scr->disp == disp)
return 0;
}
scr = malloc(sizeof(*scr));
if (!scr) {
log_error("cannot allocate memory for display %p");
return -ENOMEM;
}
memset(scr, 0, sizeof(*scr));
scr->disp = disp;
ret = uterm_screen_new_single(&scr->screen, disp);
if (ret) {
log_error("cannot create screen for display %p", scr->disp);
goto err_free;
}
ret = kmscon_font_find(&scr->font, &attr, conf_global.font_engine);
if (ret) {
log_error("cannot create font");
goto err_screen;
}
ret = kmscon_text_new(&scr->txt, NULL);
if (ret) {
log_error("cannot create text-renderer");
goto err_font;
}
kmscon_text_set(scr->txt, scr->font, scr->screen);
cols = kmscon_text_get_cols(scr->txt);
rows = kmscon_text_get_rows(scr->txt);
terminal_resize(term, cols, rows, false, true);
kmscon_dlist_link(&term->screens, &scr->list);
log_debug("added display %p to terminal %p", disp, term);
schedule_redraw(term);
uterm_display_ref(scr->disp);
return 0;
err_font:
kmscon_font_unref(scr->font);
err_screen:
uterm_screen_unref(scr->screen);
err_free:
free(scr);
return ret;
}
static void free_screen(struct kmscon_terminal *term, struct screen *scr,
bool update)
{
struct kmscon_dlist *iter;
struct screen *ent;
log_debug("destroying terminal screen %p", scr);
kmscon_dlist_unlink(&scr->list);
kmscon_text_unref(scr->txt);
kmscon_font_unref(scr->font);
uterm_screen_unref(scr->screen);
uterm_display_unref(scr->disp);
free(scr);
if (!update)
return;
term->min_cols = 0;
term->min_rows = 0;
kmscon_dlist_for_each(iter, &term->screens) {
ent = kmscon_dlist_entry(iter, struct screen, list);
terminal_resize(term,
kmscon_text_get_cols(ent->txt),
kmscon_text_get_rows(ent->txt),
false, false);
}
terminal_resize(term, 0, 0, true, true);
}
static void rm_display(struct kmscon_terminal *term, struct uterm_display *disp)
{
struct kmscon_dlist *iter;
struct screen *scr;
kmscon_dlist_for_each(iter, &term->screens) {
scr = kmscon_dlist_entry(iter, struct screen, list);
if (scr->disp == disp)
break;
}
if (iter == &term->screens)
return;
log_debug("removed display %p from terminal %p", disp, term);
free_screen(term, scr, true);
if (kmscon_dlist_empty(&term->screens) && term->cb)
term->cb(term, KMSCON_TERMINAL_NO_DISPLAY, term->data);
}
static void rm_all_screens(struct kmscon_terminal *term)
{
struct kmscon_dlist *iter;
struct screen *scr;
while ((iter = term->screens.next) != &term->screens) {
scr = kmscon_dlist_entry(iter, struct screen, list);
free_screen(term, scr, false);
}
term->min_cols = 0;
term->min_rows = 0;
}
static void pty_input(struct kmscon_pty *pty, const char *u8, size_t len,
void *data)
{
struct kmscon_terminal *term = data;
if (!len) {
if (term->cb)
term->cb(term, KMSCON_TERMINAL_HUP, term->data);
} else {
kmscon_vte_input(term->vte, u8, len);
schedule_redraw(term);
}
}
static void input_event(struct uterm_input *input,
struct uterm_input_event *ev,
void *data)
{
struct kmscon_terminal *term = data;
if (!term->opened)
return;
kmscon_vte_handle_keyboard(term->vte, ev);
}
static void write_event(struct kmscon_vte *vte, const char *u8, size_t len,
void *data)
{
struct kmscon_terminal *term = data;
kmscon_pty_write(term->pty, u8, len);
}
int kmscon_terminal_new(struct kmscon_terminal **out,
struct ev_eloop *loop,
struct uterm_video *video,
struct uterm_input *input)
{
struct kmscon_terminal *term;
int ret;
if (!out || !loop || !video || !input)
return -EINVAL;
term = malloc(sizeof(*term));
if (!term)
return -ENOMEM;
memset(term, 0, sizeof(*term));
term->ref = 1;
term->eloop = loop;
term->video = video;
term->input = input;
kmscon_dlist_init(&term->screens);
ret = kmscon_console_new(&term->console);
if (ret)
goto err_free;
ret = kmscon_vte_new(&term->vte, term->console, write_event, term);
if (ret)
goto err_con;
ret = kmscon_pty_new(&term->pty, term->eloop, pty_input, term);
if (ret)
goto err_vte;
ret = uterm_input_register_cb(term->input, input_event, term);
if (ret)
goto err_pty;
ev_eloop_ref(term->eloop);
uterm_video_ref(term->video);
uterm_input_ref(term->input);
*out = term;
log_debug("new terminal object %p", term);
return 0;
err_pty:
kmscon_pty_unref(term->pty);
err_vte:
kmscon_vte_unref(term->vte);
err_con:
kmscon_console_unref(term->console);
err_free:
free(term);
return ret;
}
void kmscon_terminal_ref(struct kmscon_terminal *term)
{
if (!term)
return;
term->ref++;
}
void kmscon_terminal_unref(struct kmscon_terminal *term)
{
if (!term || !term->ref)
return;
if (--term->ref)
return;
log_debug("free terminal object %p", term);
kmscon_terminal_close(term);
rm_all_screens(term);
uterm_input_unregister_cb(term->input, input_event, term);
kmscon_pty_unref(term->pty);
kmscon_vte_unref(term->vte);
kmscon_console_unref(term->console);
if (term->redraw)
ev_eloop_unregister_idle_cb(term->eloop, draw_all, term);
uterm_input_unref(term->input);
uterm_video_unref(term->video);
ev_eloop_unref(term->eloop);
free(term);
}
int kmscon_terminal_open(struct kmscon_terminal *term,
kmscon_terminal_event_cb cb, void *data)
{
int ret;
unsigned short width, height;
if (!term)
return -EINVAL;
kmscon_pty_close(term->pty);
width = kmscon_console_get_width(term->console);
height = kmscon_console_get_height(term->console);
ret = kmscon_pty_open(term->pty, width, height);
if (ret)
return ret;
term->opened = true;
term->cb = cb;
term->data = data;
return 0;
}
void kmscon_terminal_close(struct kmscon_terminal *term)
{
if (!term)
return;
kmscon_pty_close(term->pty);
term->data = NULL;
term->cb = NULL;
term->opened = false;
}
void kmscon_terminal_redraw(struct kmscon_terminal *term)
{
if (!term)
return;
schedule_redraw(term);
}
int kmscon_terminal_add_display(struct kmscon_terminal *term,
struct uterm_display *disp)
{
if (!term || !disp)
return -EINVAL;
return add_display(term, disp);
}
void kmscon_terminal_remove_display(struct kmscon_terminal *term,
struct uterm_display *disp)
{
if (!term || !disp)
return;
rm_display(term, disp);
}
|