4 * Copyright (c) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
5 * Copyright (c) 2011 University of Tuebingen
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files
9 * (the "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * A terminal gets assigned an input stream and several output objects and then
30 * runs a fully functional terminal emulation on it.
39 #include "kmscon_conf.h"
40 #include "kmscon_seat.h"
41 #include "kmscon_terminal.h"
44 #include "shl_dlist.h"
46 #include "tsm_screen.h"
48 #include "uterm_input.h"
49 #include "uterm_video.h"
51 #define LOG_SUBSYSTEM "terminal"
54 struct shl_dlist list;
55 struct kmscon_terminal *term;
56 struct uterm_display *disp;
57 struct kmscon_font *font;
58 struct kmscon_font *bold_font;
59 struct kmscon_text *txt;
65 struct kmscon_terminal {
67 struct ev_eloop *eloop;
68 struct uterm_input *input;
72 struct conf_ctx *conf_ctx;
73 struct kmscon_conf_t *conf;
74 struct kmscon_session *session;
76 struct shl_dlist screens;
77 unsigned int min_cols;
78 unsigned int min_rows;
80 struct tsm_screen *console;
82 struct kmscon_pty *pty;
86 static void do_redraw_screen(struct screen *scr)
90 if (!scr->term->awake)
94 tsm_screen_draw(scr->term->console, kmscon_text_prepare_cb,
95 kmscon_text_draw_cb, kmscon_text_render_cb, scr->txt);
96 ret = uterm_display_swap(scr->disp, false);
98 log_warning("cannot swap display %p", scr->disp);
102 scr->swapping = true;
105 static void redraw_screen(struct screen *scr)
107 if (!scr->term->awake)
113 do_redraw_screen(scr);
116 static void redraw_all(struct kmscon_terminal *term)
118 struct shl_dlist *iter;
124 shl_dlist_for_each(iter, &term->screens) {
125 scr = shl_dlist_entry(iter, struct screen, list);
130 static void redraw_all_test(struct kmscon_terminal *term)
132 struct shl_dlist *iter;
138 shl_dlist_for_each(iter, &term->screens) {
139 scr = shl_dlist_entry(iter, struct screen, list);
140 if (uterm_display_is_swapping(scr->disp))
141 scr->swapping = true;
146 static void display_event(struct uterm_display *disp,
147 struct uterm_display_event *ev, void *data)
149 struct screen *scr = data;
151 if (ev->action != UTERM_PAGE_FLIP)
154 scr->swapping = false;
156 do_redraw_screen(scr);
161 * We support multiple monitors per terminal. As some software-rendering
162 * backends to not support scaling, we always use the smalles cols/rows that are
163 * provided so wider displays will have black margins.
164 * This can be extended to support scaling but that would mean we need to check
165 * whether the text-renderer backend supports that, first (TODO).
167 * If @force is true, then the console/pty are notified even though the size did
168 * not changed. If @notify is false, then console/pty are not notified even
169 * though the size might have changed. force = true and notify = false doesn't
170 * make any sense, though.
172 static void terminal_resize(struct kmscon_terminal *term,
173 unsigned int cols, unsigned int rows,
174 bool force, bool notify)
178 if (!term->min_cols || (cols > 0 && cols < term->min_cols)) {
179 term->min_cols = cols;
182 if (!term->min_rows || (rows > 0 && rows < term->min_rows)) {
183 term->min_rows = rows;
187 if (!notify || (!resize && !force))
190 /* shrinking always succeeds */
191 tsm_screen_resize(term->console, term->min_cols, term->min_rows);
192 kmscon_pty_resize(term->pty, term->min_cols, term->min_rows);
196 static int add_display(struct kmscon_terminal *term, struct uterm_display *disp)
198 struct shl_dlist *iter;
201 unsigned int cols, rows;
202 struct kmscon_font_attr attr = { "", 0, 20, false, false, 0, 0 };
206 attr.ppi = term->conf->font_ppi;
207 attr.points = term->conf->font_size;
208 strncpy(attr.name, term->conf->font_name, KMSCON_FONT_MAX_NAME - 1);
209 attr.name[KMSCON_FONT_MAX_NAME - 1] = 0;
211 shl_dlist_for_each(iter, &term->screens) {
212 scr = shl_dlist_entry(iter, struct screen, list);
213 if (scr->disp == disp)
217 scr = malloc(sizeof(*scr));
219 log_error("cannot allocate memory for display %p", disp);
222 memset(scr, 0, sizeof(*scr));
226 ret = uterm_display_register_cb(scr->disp, display_event, scr);
228 log_error("cannot register display callback: %d", ret);
232 ret = kmscon_font_find(&scr->font, &attr, term->conf->font_engine);
234 log_error("cannot create font");
239 ret = kmscon_font_find(&scr->bold_font, &attr, term->conf->font_engine);
241 log_error("cannot create bold font");
242 scr->bold_font = scr->font;
243 kmscon_font_ref(scr->bold_font);
246 ret = uterm_display_use(scr->disp, &opengl);
247 if (term->conf->render_engine)
248 be = term->conf->render_engine;
249 else if (ret >= 0 && opengl)
254 ret = kmscon_text_new(&scr->txt, be);
256 log_error("cannot create text-renderer");
260 ret = kmscon_text_set(scr->txt, scr->font, scr->bold_font, scr->disp);
262 log_error("cannot set text-renderer parameters");
266 cols = kmscon_text_get_cols(scr->txt);
267 rows = kmscon_text_get_rows(scr->txt);
268 terminal_resize(term, cols, rows, false, true);
270 shl_dlist_link(&term->screens, &scr->list);
272 log_debug("added display %p to terminal %p", disp, term);
274 uterm_display_ref(scr->disp);
278 kmscon_text_unref(scr->txt);
280 kmscon_font_unref(scr->bold_font);
281 kmscon_font_unref(scr->font);
283 uterm_display_unregister_cb(scr->disp, display_event, scr);
289 static void free_screen(struct screen *scr, bool update)
291 struct shl_dlist *iter;
293 struct kmscon_terminal *term = scr->term;
295 log_debug("destroying terminal screen %p", scr);
296 shl_dlist_unlink(&scr->list);
297 kmscon_text_unref(scr->txt);
298 kmscon_font_unref(scr->bold_font);
299 kmscon_font_unref(scr->font);
300 uterm_display_unregister_cb(scr->disp, display_event, scr);
301 uterm_display_unref(scr->disp);
309 shl_dlist_for_each(iter, &term->screens) {
310 ent = shl_dlist_entry(iter, struct screen, list);
311 terminal_resize(term,
312 kmscon_text_get_cols(ent->txt),
313 kmscon_text_get_rows(ent->txt),
317 terminal_resize(term, 0, 0, true, true);
320 static void rm_display(struct kmscon_terminal *term, struct uterm_display *disp)
322 struct shl_dlist *iter;
325 shl_dlist_for_each(iter, &term->screens) {
326 scr = shl_dlist_entry(iter, struct screen, list);
327 if (scr->disp == disp)
331 if (iter == &term->screens)
334 log_debug("removed display %p from terminal %p", disp, term);
335 free_screen(scr, true);
338 static void input_event(struct uterm_input *input,
339 struct uterm_input_event *ev,
342 struct kmscon_terminal *term = data;
344 if (!term->opened || !term->awake || ev->handled)
347 if (conf_grab_matches(term->conf->grab_scroll_up,
348 ev->mods, ev->num_syms, ev->keysyms)) {
349 tsm_screen_sb_up(term->console, 1);
354 if (conf_grab_matches(term->conf->grab_scroll_down,
355 ev->mods, ev->num_syms, ev->keysyms)) {
356 tsm_screen_sb_down(term->console, 1);
361 if (conf_grab_matches(term->conf->grab_page_up,
362 ev->mods, ev->num_syms, ev->keysyms)) {
363 tsm_screen_sb_page_up(term->console, 1);
368 if (conf_grab_matches(term->conf->grab_page_down,
369 ev->mods, ev->num_syms, ev->keysyms)) {
370 tsm_screen_sb_page_down(term->console, 1);
376 /* TODO: xkbcommon supports multiple keysyms, but it is currently
377 * unclear how this feature will be used. There is no keymap, which
379 if (ev->num_syms > 1)
382 if (tsm_vte_handle_keyboard(term->vte, ev->keysyms[0], ev->ascii,
383 ev->mods, ev->codepoints[0])) {
384 tsm_screen_sb_reset(term->console);
390 static void rm_all_screens(struct kmscon_terminal *term)
392 struct shl_dlist *iter;
395 while ((iter = term->screens.next) != &term->screens) {
396 scr = shl_dlist_entry(iter, struct screen, list);
397 free_screen(scr, false);
404 static int terminal_open(struct kmscon_terminal *term)
407 unsigned short width, height;
412 tsm_vte_hard_reset(term->vte);
413 width = tsm_screen_get_width(term->console);
414 height = tsm_screen_get_height(term->console);
415 ret = kmscon_pty_open(term->pty, width, height);
424 static void terminal_close(struct kmscon_terminal *term)
426 kmscon_pty_close(term->pty);
427 term->opened = false;
430 static void terminal_destroy(struct kmscon_terminal *term)
432 log_debug("free terminal object %p", term);
434 terminal_close(term);
435 rm_all_screens(term);
436 uterm_input_unregister_cb(term->input, input_event, term);
437 ev_eloop_rm_fd(term->ptyfd);
438 kmscon_pty_unref(term->pty);
439 tsm_vte_unref(term->vte);
440 tsm_screen_unref(term->console);
441 uterm_input_unref(term->input);
442 ev_eloop_unref(term->eloop);
446 static int session_event(struct kmscon_session *session,
447 struct kmscon_session_event *ev, void *data)
449 struct kmscon_terminal *term = data;
452 case KMSCON_SESSION_DISPLAY_NEW:
453 add_display(term, ev->disp);
455 case KMSCON_SESSION_DISPLAY_GONE:
456 rm_display(term, ev->disp);
458 case KMSCON_SESSION_ACTIVATE:
462 redraw_all_test(term);
464 case KMSCON_SESSION_DEACTIVATE:
467 case KMSCON_SESSION_UNREGISTER:
468 terminal_destroy(term);
475 static void pty_input(struct kmscon_pty *pty, const char *u8, size_t len,
478 struct kmscon_terminal *term = data;
481 terminal_close(term);
484 tsm_vte_input(term->vte, u8, len);
489 static void pty_event(struct ev_fd *fd, int mask, void *data)
491 struct kmscon_terminal *term = data;
493 kmscon_pty_dispatch(term->pty);
496 static void write_event(struct tsm_vte *vte, const char *u8, size_t len,
499 struct kmscon_terminal *term = data;
501 kmscon_pty_write(term->pty, u8, len);
504 int kmscon_terminal_register(struct kmscon_session **out,
505 struct kmscon_seat *seat)
507 struct kmscon_terminal *term;
513 term = malloc(sizeof(*term));
517 memset(term, 0, sizeof(*term));
519 term->eloop = kmscon_seat_get_eloop(seat);
520 term->input = kmscon_seat_get_input(seat);
521 shl_dlist_init(&term->screens);
523 term->conf_ctx = kmscon_seat_get_conf(seat);
524 term->conf = conf_ctx_get_mem(term->conf_ctx);
526 ret = tsm_screen_new(&term->console, log_llog);
529 tsm_screen_set_max_sb(term->console, term->conf->sb_size);
530 if (term->conf->render_timing)
531 tsm_screen_set_opts(term->console,
532 TSM_SCREEN_OPT_RENDER_TIMING);
534 ret = tsm_vte_new(&term->vte, term->console, write_event, term,
538 tsm_vte_set_palette(term->vte, term->conf->palette);
540 ret = kmscon_pty_new(&term->pty, pty_input, term);
544 kmscon_pty_set_env_reset(term->pty, term->conf->reset_env);
546 ret = kmscon_pty_set_term(term->pty, term->conf->term);
550 ret = kmscon_pty_set_colorterm(term->pty, "kmscon");
554 ret = kmscon_pty_set_argv(term->pty, term->conf->argv);
558 ret = kmscon_pty_set_seat(term->pty, kmscon_seat_get_name(seat));
562 ret = ev_eloop_new_fd(term->eloop, &term->ptyfd,
563 kmscon_pty_get_fd(term->pty),
564 EV_READABLE, pty_event, term);
568 ret = uterm_input_register_cb(term->input, input_event, term);
572 ret = kmscon_seat_register_session(seat, &term->session, session_event,
575 log_error("cannot register session for terminal: %d", ret);
579 ev_eloop_ref(term->eloop);
580 uterm_input_ref(term->input);
581 *out = term->session;
582 log_debug("new terminal object %p", term);
586 uterm_input_unregister_cb(term->input, input_event, term);
588 ev_eloop_rm_fd(term->ptyfd);
590 kmscon_pty_unref(term->pty);
592 tsm_vte_unref(term->vte);
594 tsm_screen_unref(term->console);