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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
|
/*
* 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 <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include "conf.h"
#include "eloop.h"
#include "kmscon_conf.h"
#include "kmscon_seat.h"
#include "kmscon_terminal.h"
#include "pty.h"
#include "shl_dlist.h"
#include "shl_log.h"
#include "text.h"
#include "tsm_screen.h"
#include "tsm_vte.h"
#include "uterm_input.h"
#include "uterm_video.h"
#define LOG_SUBSYSTEM "terminal"
struct screen {
struct shl_dlist list;
struct kmscon_terminal *term;
struct uterm_display *disp;
struct kmscon_text *txt;
bool swapping;
bool pending;
};
struct kmscon_terminal {
unsigned long ref;
struct ev_eloop *eloop;
struct uterm_input *input;
bool opened;
bool awake;
struct conf_ctx *conf_ctx;
struct kmscon_conf_t *conf;
struct kmscon_session *session;
struct shl_dlist screens;
unsigned int min_cols;
unsigned int min_rows;
struct tsm_screen *console;
struct tsm_vte *vte;
struct kmscon_pty *pty;
struct ev_fd *ptyfd;
struct kmscon_font_attr font_attr;
struct kmscon_font *font;
struct kmscon_font *bold_font;
};
static void do_clear_margins(struct screen *scr)
{
unsigned int w, h, sw, sh;
struct uterm_mode *mode;
int dw, dh;
mode = uterm_display_get_current(scr->disp);
if (!mode)
return;
sw = uterm_mode_get_width(mode);
sh = uterm_mode_get_height(mode);
w = scr->txt->font->attr.width * scr->txt->cols;
h = scr->txt->font->attr.height * scr->txt->rows;
dw = sw - w;
dh = sh - h;
if (dw > 0)
uterm_display_fill(scr->disp, 0, 0, 0,
w, 0,
dw, h);
if (dh > 0)
uterm_display_fill(scr->disp, 0, 0, 0,
0, h,
sw, dh);
}
static void do_redraw_screen(struct screen *scr)
{
int ret;
if (!scr->term->awake)
return;
scr->pending = false;
do_clear_margins(scr);
tsm_screen_draw(scr->term->console, kmscon_text_prepare_cb,
kmscon_text_draw_cb, kmscon_text_render_cb, scr->txt);
ret = uterm_display_swap(scr->disp, false);
if (ret) {
log_warning("cannot swap display %p", scr->disp);
return;
}
scr->swapping = true;
}
static void redraw_screen(struct screen *scr)
{
if (!scr->term->awake)
return;
if (scr->swapping)
scr->pending = true;
else
do_redraw_screen(scr);
}
static void redraw_all(struct kmscon_terminal *term)
{
struct shl_dlist *iter;
struct screen *scr;
if (!term->awake)
return;
shl_dlist_for_each(iter, &term->screens) {
scr = shl_dlist_entry(iter, struct screen, list);
redraw_screen(scr);
}
}
static void redraw_all_test(struct kmscon_terminal *term)
{
struct shl_dlist *iter;
struct screen *scr;
if (!term->awake)
return;
shl_dlist_for_each(iter, &term->screens) {
scr = shl_dlist_entry(iter, struct screen, list);
if (uterm_display_is_swapping(scr->disp))
scr->swapping = true;
redraw_screen(scr);
}
}
static void display_event(struct uterm_display *disp,
struct uterm_display_event *ev, void *data)
{
struct screen *scr = data;
if (ev->action != UTERM_PAGE_FLIP)
return;
scr->swapping = false;
if (scr->pending)
do_redraw_screen(scr);
}
/*
* Resize terminal
* We support multiple monitors per terminal. As some software-rendering
* backends to not support scaling, we always use the smallest 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;
if (!term->min_cols || !term->min_rows)
return;
tsm_screen_resize(term->console, term->min_cols, term->min_rows);
kmscon_pty_resize(term->pty, term->min_cols, term->min_rows);
redraw_all(term);
}
static int font_set(struct kmscon_terminal *term)
{
int ret;
struct kmscon_font *font, *bold_font;
struct shl_dlist *iter;
struct screen *ent;
term->font_attr.bold = false;
ret = kmscon_font_find(&font, &term->font_attr,
term->conf->font_engine);
if (ret)
return ret;
term->font_attr.bold = true;
ret = kmscon_font_find(&bold_font, &term->font_attr,
term->conf->font_engine);
if (ret) {
log_warning("cannot create bold font: %d", ret);
bold_font = font;
kmscon_font_ref(bold_font);
}
kmscon_font_unref(term->bold_font);
kmscon_font_unref(term->font);
term->font = font;
term->bold_font = bold_font;
term->min_cols = 0;
term->min_rows = 0;
shl_dlist_for_each(iter, &term->screens) {
ent = shl_dlist_entry(iter, struct screen, list);
ret = kmscon_text_set(ent->txt, font, bold_font, ent->disp);
if (ret)
log_warning("cannot change text-renderer font: %d",
ret);
terminal_resize(term,
kmscon_text_get_cols(ent->txt),
kmscon_text_get_rows(ent->txt),
false, false);
}
terminal_resize(term, 0, 0, true, true);
return 0;
}
static int add_display(struct kmscon_terminal *term, struct uterm_display *disp)
{
struct shl_dlist *iter;
struct screen *scr;
int ret;
const char *be;
bool opengl;
shl_dlist_for_each(iter, &term->screens) {
scr = shl_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", disp);
return -ENOMEM;
}
memset(scr, 0, sizeof(*scr));
scr->term = term;
scr->disp = disp;
ret = uterm_display_register_cb(scr->disp, display_event, scr);
if (ret) {
log_error("cannot register display callback: %d", ret);
goto err_free;
}
ret = uterm_display_use(scr->disp, &opengl);
if (term->conf->render_engine)
be = term->conf->render_engine;
else if (ret >= 0 && opengl)
be = "gltex";
else
be = "bbulk";
ret = kmscon_text_new(&scr->txt, be);
if (ret) {
log_error("cannot create text-renderer");
goto err_cb;
}
ret = kmscon_text_set(scr->txt, term->font, term->bold_font,
scr->disp);
if (ret) {
log_error("cannot set text-renderer parameters");
goto err_text;
}
terminal_resize(term,
kmscon_text_get_cols(scr->txt),
kmscon_text_get_rows(scr->txt),
false, true);
shl_dlist_link(&term->screens, &scr->list);
log_debug("added display %p to terminal %p", disp, term);
redraw_screen(scr);
uterm_display_ref(scr->disp);
return 0;
err_text:
kmscon_text_unref(scr->txt);
err_cb:
uterm_display_unregister_cb(scr->disp, display_event, scr);
err_free:
free(scr);
return ret;
}
static void free_screen(struct screen *scr, bool update)
{
struct shl_dlist *iter;
struct screen *ent;
struct kmscon_terminal *term = scr->term;
log_debug("destroying terminal screen %p", scr);
shl_dlist_unlink(&scr->list);
kmscon_text_unref(scr->txt);
uterm_display_unregister_cb(scr->disp, display_event, scr);
uterm_display_unref(scr->disp);
free(scr);
if (!update)
return;
term->min_cols = 0;
term->min_rows = 0;
shl_dlist_for_each(iter, &term->screens) {
ent = shl_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 shl_dlist *iter;
struct screen *scr;
shl_dlist_for_each(iter, &term->screens) {
scr = shl_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(scr, true);
}
static void input_event(struct uterm_input *input,
struct uterm_input_event *ev,
void *data)
{
struct kmscon_terminal *term = data;
if (!term->opened || !term->awake || ev->handled)
return;
if (conf_grab_matches(term->conf->grab_scroll_up,
ev->mods, ev->num_syms, ev->keysyms)) {
tsm_screen_sb_up(term->console, 1);
redraw_all(term);
ev->handled = true;
return;
}
if (conf_grab_matches(term->conf->grab_scroll_down,
ev->mods, ev->num_syms, ev->keysyms)) {
tsm_screen_sb_down(term->console, 1);
redraw_all(term);
ev->handled = true;
return;
}
if (conf_grab_matches(term->conf->grab_page_up,
ev->mods, ev->num_syms, ev->keysyms)) {
tsm_screen_sb_page_up(term->console, 1);
redraw_all(term);
ev->handled = true;
return;
}
if (conf_grab_matches(term->conf->grab_page_down,
ev->mods, ev->num_syms, ev->keysyms)) {
tsm_screen_sb_page_down(term->console, 1);
redraw_all(term);
ev->handled = true;
return;
}
if (conf_grab_matches(term->conf->grab_zoom_in,
ev->mods, ev->num_syms, ev->keysyms)) {
ev->handled = true;
if (term->font_attr.points + 1 < term->font_attr.points)
return;
++term->font_attr.points;
if (font_set(term))
--term->font_attr.points;
return;
}
if (conf_grab_matches(term->conf->grab_zoom_out,
ev->mods, ev->num_syms, ev->keysyms)) {
ev->handled = true;
if (term->font_attr.points <= 1)
return;
--term->font_attr.points;
if (font_set(term))
++term->font_attr.points;
return;
}
/* TODO: xkbcommon supports multiple keysyms, but it is currently
* unclear how this feature will be used. There is no keymap, which
* uses this, yet. */
if (ev->num_syms > 1)
return;
if (tsm_vte_handle_keyboard(term->vte, ev->keysyms[0], ev->ascii,
ev->mods, ev->codepoints[0])) {
tsm_screen_sb_reset(term->console);
redraw_all(term);
ev->handled = true;
}
}
static void rm_all_screens(struct kmscon_terminal *term)
{
struct shl_dlist *iter;
struct screen *scr;
while ((iter = term->screens.next) != &term->screens) {
scr = shl_dlist_entry(iter, struct screen, list);
free_screen(scr, false);
}
term->min_cols = 0;
term->min_rows = 0;
}
static int terminal_open(struct kmscon_terminal *term)
{
int ret;
unsigned short width, height;
if (term->opened)
return -EALREADY;
tsm_vte_hard_reset(term->vte);
width = tsm_screen_get_width(term->console);
height = tsm_screen_get_height(term->console);
ret = kmscon_pty_open(term->pty, width, height);
if (ret)
return ret;
term->opened = true;
redraw_all(term);
return 0;
}
static void terminal_close(struct kmscon_terminal *term)
{
kmscon_pty_close(term->pty);
term->opened = false;
}
static void terminal_destroy(struct kmscon_terminal *term)
{
log_debug("free terminal object %p", term);
terminal_close(term);
rm_all_screens(term);
uterm_input_unregister_cb(term->input, input_event, term);
ev_eloop_rm_fd(term->ptyfd);
kmscon_pty_unref(term->pty);
kmscon_font_unref(term->bold_font);
kmscon_font_unref(term->font);
tsm_vte_unref(term->vte);
tsm_screen_unref(term->console);
uterm_input_unref(term->input);
ev_eloop_unref(term->eloop);
free(term);
}
static int session_event(struct kmscon_session *session,
struct kmscon_session_event *ev, void *data)
{
struct kmscon_terminal *term = data;
switch (ev->type) {
case KMSCON_SESSION_DISPLAY_NEW:
add_display(term, ev->disp);
break;
case KMSCON_SESSION_DISPLAY_GONE:
rm_display(term, ev->disp);
break;
case KMSCON_SESSION_ACTIVATE:
term->awake = true;
if (!term->opened)
terminal_open(term);
redraw_all_test(term);
break;
case KMSCON_SESSION_DEACTIVATE:
term->awake = false;
break;
case KMSCON_SESSION_UNREGISTER:
terminal_destroy(term);
break;
}
return 0;
}
static void pty_input(struct kmscon_pty *pty, const char *u8, size_t len,
void *data)
{
struct kmscon_terminal *term = data;
if (!len) {
terminal_close(term);
terminal_open(term);
} else {
tsm_vte_input(term->vte, u8, len);
redraw_all(term);
}
}
static void pty_event(struct ev_fd *fd, int mask, void *data)
{
struct kmscon_terminal *term = data;
kmscon_pty_dispatch(term->pty);
}
static void write_event(struct tsm_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_register(struct kmscon_session **out,
struct kmscon_seat *seat)
{
struct kmscon_terminal *term;
int ret;
if (!out || !seat)
return -EINVAL;
term = malloc(sizeof(*term));
if (!term)
return -ENOMEM;
memset(term, 0, sizeof(*term));
term->ref = 1;
term->eloop = kmscon_seat_get_eloop(seat);
term->input = kmscon_seat_get_input(seat);
shl_dlist_init(&term->screens);
term->conf_ctx = kmscon_seat_get_conf(seat);
term->conf = conf_ctx_get_mem(term->conf_ctx);
strncpy(term->font_attr.name, term->conf->font_name,
KMSCON_FONT_MAX_NAME - 1);
term->font_attr.ppi = term->conf->font_ppi;
term->font_attr.points = term->conf->font_size;
ret = tsm_screen_new(&term->console, log_llog, NULL);
if (ret)
goto err_free;
tsm_screen_set_max_sb(term->console, term->conf->sb_size);
if (term->conf->render_timing)
tsm_screen_set_opts(term->console,
TSM_SCREEN_OPT_RENDER_TIMING);
ret = tsm_vte_new(&term->vte, term->console, write_event, term,
log_llog, NULL);
if (ret)
goto err_con;
tsm_vte_set_palette(term->vte, term->conf->palette);
ret = font_set(term);
if (ret)
goto err_vte;
ret = kmscon_pty_new(&term->pty, pty_input, term);
if (ret)
goto err_font;
kmscon_pty_set_env_reset(term->pty, term->conf->reset_env);
ret = kmscon_pty_set_term(term->pty, term->conf->term);
if (ret)
goto err_pty;
ret = kmscon_pty_set_colorterm(term->pty, "kmscon");
if (ret)
goto err_pty;
ret = kmscon_pty_set_argv(term->pty, term->conf->argv);
if (ret)
goto err_pty;
ret = kmscon_pty_set_seat(term->pty, kmscon_seat_get_name(seat));
if (ret)
goto err_pty;
ret = ev_eloop_new_fd(term->eloop, &term->ptyfd,
kmscon_pty_get_fd(term->pty),
EV_READABLE, pty_event, term);
if (ret)
goto err_pty;
ret = uterm_input_register_cb(term->input, input_event, term);
if (ret)
goto err_ptyfd;
ret = kmscon_seat_register_session(seat, &term->session, session_event,
term);
if (ret) {
log_error("cannot register session for terminal: %d", ret);
goto err_input;
}
ev_eloop_ref(term->eloop);
uterm_input_ref(term->input);
*out = term->session;
log_debug("new terminal object %p", term);
return 0;
err_input:
uterm_input_unregister_cb(term->input, input_event, term);
err_ptyfd:
ev_eloop_rm_fd(term->ptyfd);
err_pty:
kmscon_pty_unref(term->pty);
err_font:
kmscon_font_unref(term->bold_font);
kmscon_font_unref(term->font);
err_vte:
tsm_vte_unref(term->vte);
err_con:
tsm_screen_unref(term->console);
err_free:
free(term);
return ret;
}
|