Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
| 7 | #include <ctype.h> |
| 8 | #include <libtsm.h> |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 9 | #include <stdio.h> |
Dominik Behr | 9389945 | 2014-08-18 22:16:21 -0700 | [diff] [blame] | 10 | #include <sys/select.h> |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 11 | #include <sys/types.h> |
| 12 | #include <sys/wait.h> |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 13 | |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 14 | #include "dbus.h" |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 15 | #include "fb.h" |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 16 | #include "font.h" |
| 17 | #include "input.h" |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 18 | #include "main.h" |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 19 | #include "shl_pty.h" |
| 20 | #include "term.h" |
| 21 | #include "util.h" |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 22 | |
Stéphane Marchesin | 08c08e7 | 2016-01-07 16:44:08 -0800 | [diff] [blame] | 23 | static terminal_t* terminals[MAX_TERMINALS]; |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 24 | static uint32_t current_terminal = 0; |
Stéphane Marchesin | 08c08e7 | 2016-01-07 16:44:08 -0800 | [diff] [blame] | 25 | |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 26 | struct term { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 27 | struct tsm_screen* screen; |
| 28 | struct tsm_vte* vte; |
| 29 | struct shl_pty* pty; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 30 | int pty_bridge; |
| 31 | int pid; |
| 32 | tsm_age_t age; |
| 33 | int char_x, char_y; |
| 34 | int pitch; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 35 | uint32_t* dst_image; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 38 | struct _terminal_t { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 39 | uint32_t background; |
| 40 | bool background_valid; |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 41 | fb_t* fb; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 42 | struct term* term; |
| 43 | bool active; |
| 44 | char** exec; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 47 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 48 | static char* interactive_cmd_line[] = { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 49 | "/sbin/agetty", |
| 50 | "-", |
| 51 | "9600", |
| 52 | "xterm", |
| 53 | NULL |
| 54 | }; |
| 55 | |
| 56 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 57 | static char* noninteractive_cmd_line[] = { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 58 | "/bin/cat", |
| 59 | NULL |
| 60 | }; |
| 61 | |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 62 | static bool in_background = false; |
| 63 | static bool hotplug_occured = false; |
| 64 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 65 | |
| 66 | static void __attribute__ ((noreturn)) term_run_child(terminal_t* terminal) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 67 | { |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 68 | /* XXX figure out how to fix "top" for xterm-256color */ |
| 69 | setenv("TERM", "xterm", 1); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 70 | execve(terminal->exec[0], terminal->exec, environ); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 71 | exit(1); |
| 72 | } |
| 73 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 74 | static int term_draw_cell(struct tsm_screen* screen, uint32_t id, |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 75 | const uint32_t* ch, size_t len, |
| 76 | unsigned int cwidth, unsigned int posx, |
| 77 | unsigned int posy, |
| 78 | const struct tsm_screen_attr* attr, |
| 79 | tsm_age_t age, void* data) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 80 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 81 | terminal_t* terminal = (terminal_t*)data; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 82 | uint32_t front_color, back_color; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 83 | uint8_t br, bb, bg; |
Stéphane Marchesin | edece33 | 2015-12-14 15:10:58 -0800 | [diff] [blame] | 84 | uint32_t luminance; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 85 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 86 | if (age && terminal->term->age && age <= terminal->term->age) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 87 | return 0; |
| 88 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 89 | if (terminal->background_valid) { |
| 90 | br = (terminal->background >> 16) & 0xFF; |
| 91 | bg = (terminal->background >> 8) & 0xFF; |
| 92 | bb = (terminal->background) & 0xFF; |
Stéphane Marchesin | edece33 | 2015-12-14 15:10:58 -0800 | [diff] [blame] | 93 | luminance = (3 * br + bb + 4 * bg) >> 3; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 94 | |
Stéphane Marchesin | edece33 | 2015-12-14 15:10:58 -0800 | [diff] [blame] | 95 | /* |
| 96 | * FIXME: black is chosen on a dark background, but it uses the |
| 97 | * default color for light backgrounds |
| 98 | */ |
| 99 | if (luminance > 128) { |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 100 | front_color = 0; |
| 101 | back_color = terminal->background; |
| 102 | } else { |
| 103 | front_color = (attr->fr << 16) | (attr->fg << 8) | attr->fb; |
| 104 | back_color = terminal->background; |
| 105 | } |
| 106 | } else { |
| 107 | front_color = (attr->fr << 16) | (attr->fg << 8) | attr->fb; |
| 108 | back_color = (attr->br << 16) | (attr->bg << 8) | attr->bb; |
| 109 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 110 | |
| 111 | if (attr->inverse) { |
| 112 | uint32_t tmp = front_color; |
| 113 | front_color = back_color; |
| 114 | back_color = tmp; |
| 115 | } |
| 116 | |
| 117 | if (len) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 118 | font_render(terminal->term->dst_image, posx, posy, terminal->term->pitch, *ch, |
| 119 | front_color, back_color); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 120 | else |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 121 | font_fillchar(terminal->term->dst_image, posx, posy, terminal->term->pitch, |
| 122 | front_color, back_color); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Stéphane Marchesin | e2a46cd | 2016-01-07 16:45:24 -0800 | [diff] [blame] | 127 | static void term_redraw(terminal_t* terminal) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 128 | { |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 129 | uint32_t* fb_buffer; |
| 130 | fb_buffer = fb_lock(terminal->fb); |
| 131 | if (fb_buffer != NULL) { |
| 132 | terminal->term->dst_image = fb_buffer; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 133 | terminal->term->age = |
| 134 | tsm_screen_draw(terminal->term->screen, term_draw_cell, terminal); |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 135 | fb_unlock(terminal->fb); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 136 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 137 | } |
| 138 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 139 | void term_key_event(terminal_t* terminal, uint32_t keysym, int32_t unicode) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 140 | { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 141 | if (tsm_vte_handle_keyboard(terminal->term->vte, keysym, 0, 0, unicode)) |
| 142 | tsm_screen_sb_reset(terminal->term->screen); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 143 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 144 | term_redraw(terminal); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 147 | static void term_read_cb(struct shl_pty* pty, char* u8, size_t len, void* data) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 148 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 149 | terminal_t* terminal = (terminal_t*)data; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 150 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 151 | tsm_vte_input(terminal->term->vte, u8, len); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 152 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 153 | term_redraw(terminal); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 156 | static void term_write_cb(struct tsm_vte* vte, const char* u8, size_t len, |
| 157 | void* data) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 158 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 159 | struct term* term = data; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 160 | int r; |
| 161 | |
| 162 | r = shl_pty_write(term->pty, u8, len); |
| 163 | if (r < 0) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 164 | LOG(ERROR, "OOM in pty-write (%d)", r); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 165 | |
| 166 | shl_pty_dispatch(term->pty); |
| 167 | } |
| 168 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 169 | static const char* sev2str_table[] = { |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 170 | "FATAL", |
| 171 | "ALERT", |
| 172 | "CRITICAL", |
| 173 | "ERROR", |
| 174 | "WARNING", |
| 175 | "NOTICE", |
| 176 | "INFO", |
| 177 | "DEBUG" |
| 178 | }; |
| 179 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 180 | static const char* sev2str(unsigned int sev) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 181 | { |
| 182 | if (sev > 7) |
| 183 | return "DEBUG"; |
| 184 | |
| 185 | return sev2str_table[sev]; |
| 186 | } |
| 187 | |
Dominik Behr | 0000350 | 2014-08-15 16:42:37 -0700 | [diff] [blame] | 188 | #ifdef __clang__ |
| 189 | __attribute__((__format__ (__printf__, 7, 0))) |
| 190 | #endif |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 191 | static void log_tsm(void* data, const char* file, int line, const char* fn, |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 192 | const char* subs, unsigned int sev, const char* format, |
| 193 | va_list args) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 194 | { |
| 195 | fprintf(stderr, "%s: %s: ", sev2str(sev), subs); |
| 196 | vfprintf(stderr, format, args); |
| 197 | fprintf(stderr, "\n"); |
| 198 | } |
| 199 | |
Stéphane Marchesin | 7831066 | 2016-01-06 16:09:39 -0800 | [diff] [blame] | 200 | static int term_resize(terminal_t* term) |
| 201 | { |
| 202 | uint32_t char_width, char_height; |
| 203 | int status; |
| 204 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 205 | font_init(fb_getscaling(term->fb)); |
Stéphane Marchesin | 7831066 | 2016-01-06 16:09:39 -0800 | [diff] [blame] | 206 | font_get_size(&char_width, &char_height); |
| 207 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 208 | term->term->char_x = fb_getwidth(term->fb) / char_width; |
| 209 | term->term->char_y = fb_getheight(term->fb) / char_height; |
| 210 | term->term->pitch = fb_getpitch(term->fb); |
Stéphane Marchesin | 7831066 | 2016-01-06 16:09:39 -0800 | [diff] [blame] | 211 | |
| 212 | status = tsm_screen_resize(term->term->screen, |
| 213 | term->term->char_x, term->term->char_y); |
| 214 | if (status < 0) { |
| 215 | font_free(); |
| 216 | return -1; |
| 217 | } |
| 218 | |
| 219 | status = shl_pty_resize(term->term->pty, term->term->char_x, |
| 220 | term->term->char_y); |
| 221 | if (status < 0) { |
| 222 | font_free(); |
| 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 229 | terminal_t* term_init(bool interactive) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 230 | { |
| 231 | const int scrollback_size = 200; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 232 | int status; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 233 | terminal_t* new_terminal; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 234 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 235 | new_terminal = (terminal_t*)calloc(1, sizeof(*new_terminal)); |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 236 | if (!new_terminal) |
| 237 | return NULL; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 238 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 239 | new_terminal->background_valid = false; |
| 240 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 241 | new_terminal->fb = fb_init(); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 242 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 243 | if (!new_terminal->fb) { |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 244 | term_close(new_terminal); |
| 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | new_terminal->term = (struct term*)calloc(1, sizeof(*new_terminal->term)); |
| 249 | if (!new_terminal->term) { |
| 250 | term_close(new_terminal); |
| 251 | return NULL; |
| 252 | } |
| 253 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 254 | if (interactive) |
| 255 | new_terminal->exec = interactive_cmd_line; |
| 256 | else |
| 257 | new_terminal->exec = noninteractive_cmd_line; |
| 258 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 259 | status = tsm_screen_new(&new_terminal->term->screen, |
| 260 | log_tsm, new_terminal->term); |
zhuo-hao | 471f1e2 | 2015-08-12 14:55:56 +0800 | [diff] [blame] | 261 | if (status < 0) { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 262 | term_close(new_terminal); |
| 263 | return NULL; |
| 264 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 265 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 266 | tsm_screen_set_max_sb(new_terminal->term->screen, scrollback_size); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 267 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 268 | status = tsm_vte_new(&new_terminal->term->vte, new_terminal->term->screen, |
| 269 | term_write_cb, new_terminal->term, log_tsm, new_terminal->term); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 270 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 271 | if (status < 0) { |
| 272 | term_close(new_terminal); |
| 273 | return NULL; |
| 274 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 275 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 276 | new_terminal->term->pty_bridge = shl_pty_bridge_new(); |
| 277 | if (new_terminal->term->pty_bridge < 0) { |
| 278 | term_close(new_terminal); |
| 279 | return NULL; |
| 280 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 281 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 282 | status = shl_pty_open(&new_terminal->term->pty, |
Stéphane Marchesin | 7831066 | 2016-01-06 16:09:39 -0800 | [diff] [blame] | 283 | term_read_cb, new_terminal, 1, 1); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 284 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 285 | if (status < 0) { |
| 286 | term_close(new_terminal); |
| 287 | return NULL; |
| 288 | } else if (status == 0) { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 289 | term_run_child(new_terminal); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 290 | exit(1); |
| 291 | } |
| 292 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 293 | status = shl_pty_bridge_add(new_terminal->term->pty_bridge, new_terminal->term->pty); |
| 294 | if (status) { |
| 295 | shl_pty_close(new_terminal->term->pty); |
| 296 | term_close(new_terminal); |
| 297 | return NULL; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 298 | } |
| 299 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 300 | new_terminal->term->pid = shl_pty_get_child(new_terminal->term->pty); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 301 | |
Stéphane Marchesin | 7831066 | 2016-01-06 16:09:39 -0800 | [diff] [blame] | 302 | status = term_resize(new_terminal); |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 303 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 304 | if (status < 0) { |
| 305 | shl_pty_close(new_terminal->term->pty); |
| 306 | term_close(new_terminal); |
| 307 | return NULL; |
| 308 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 309 | |
| 310 | if (interactive) |
| 311 | new_terminal->active = true; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 312 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 313 | return new_terminal; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 314 | } |
| 315 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 316 | void term_activate(terminal_t* terminal) |
| 317 | { |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 318 | term_set_current_to(terminal); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 319 | terminal->active = true; |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 320 | fb_setmode(terminal->fb); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 321 | term_redraw(terminal); |
| 322 | } |
| 323 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 324 | void term_deactivate(terminal_t* terminal) |
| 325 | { |
| 326 | if (!terminal->active) |
| 327 | return; |
| 328 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 329 | terminal->active = false; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 332 | void term_close(terminal_t* term) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 333 | { |
| 334 | if (!term) |
| 335 | return; |
| 336 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 337 | if (term->fb) { |
| 338 | fb_close(term->fb); |
| 339 | term->fb = NULL; |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 340 | } |
| 341 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 342 | if (term->term) { |
| 343 | free(term->term); |
| 344 | term->term = NULL; |
| 345 | } |
| 346 | |
Haixia Shi | 95d680e | 2015-04-27 20:29:17 -0700 | [diff] [blame] | 347 | font_free(); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 348 | free(term); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 349 | } |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 350 | |
| 351 | bool term_is_child_done(terminal_t* terminal) |
| 352 | { |
| 353 | int status; |
| 354 | int ret; |
| 355 | ret = waitpid(terminal->term->pid, &status, WNOHANG); |
| 356 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 357 | if ((ret == -1) && (errno == ECHILD)) { |
| 358 | return false; |
| 359 | } |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 360 | return ret != 0; |
| 361 | } |
| 362 | |
| 363 | void term_page_up(terminal_t* terminal) |
| 364 | { |
| 365 | tsm_screen_sb_page_up(terminal->term->screen, 1); |
| 366 | term_redraw(terminal); |
| 367 | } |
| 368 | |
| 369 | void term_page_down(terminal_t* terminal) |
| 370 | { |
| 371 | tsm_screen_sb_page_down(terminal->term->screen, 1); |
| 372 | term_redraw(terminal); |
| 373 | } |
| 374 | |
| 375 | void term_line_up(terminal_t* terminal) |
| 376 | { |
| 377 | tsm_screen_sb_up(terminal->term->screen, 1); |
| 378 | term_redraw(terminal); |
| 379 | } |
| 380 | |
| 381 | void term_line_down(terminal_t* terminal) |
| 382 | { |
| 383 | tsm_screen_sb_down(terminal->term->screen, 1); |
| 384 | term_redraw(terminal); |
| 385 | } |
| 386 | |
| 387 | bool term_is_valid(terminal_t* terminal) |
| 388 | { |
| 389 | return ((terminal != NULL) && (terminal->term != NULL)); |
| 390 | } |
| 391 | |
| 392 | int term_fd(terminal_t* terminal) |
| 393 | { |
| 394 | if (term_is_valid(terminal)) |
| 395 | return terminal->term->pty_bridge; |
| 396 | else |
| 397 | return -1; |
| 398 | } |
| 399 | |
| 400 | void term_dispatch_io(terminal_t* terminal, fd_set* read_set) |
| 401 | { |
| 402 | if (term_is_valid(terminal)) |
| 403 | if (FD_ISSET(terminal->term->pty_bridge, read_set)) |
| 404 | shl_pty_bridge_dispatch(terminal->term->pty_bridge, 0); |
| 405 | } |
| 406 | |
| 407 | bool term_exception(terminal_t* terminal, fd_set* exception_set) |
| 408 | { |
| 409 | if (term_is_valid(terminal)) { |
| 410 | if (terminal->term->pty_bridge >= 0) { |
| 411 | return FD_ISSET(terminal->term->pty_bridge, |
| 412 | exception_set); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | bool term_is_active(terminal_t* terminal) |
| 420 | { |
| 421 | if (term_is_valid(terminal)) |
| 422 | return terminal->active; |
| 423 | |
| 424 | return false; |
| 425 | } |
| 426 | |
Dominik Behr | b1abcba | 2016-04-14 14:57:21 -0700 | [diff] [blame] | 427 | void term_add_fds(terminal_t* terminal, fd_set* read_set, fd_set* exception_set, int* maxfd) |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 428 | { |
| 429 | if (term_is_valid(terminal)) { |
| 430 | if (terminal->term->pty_bridge >= 0) { |
Dominik Behr | d711267 | 2016-01-20 16:59:34 -0800 | [diff] [blame] | 431 | *maxfd = MAX(*maxfd, terminal->term->pty_bridge); |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 432 | FD_SET(terminal->term->pty_bridge, read_set); |
| 433 | FD_SET(terminal->term->pty_bridge, exception_set); |
| 434 | } |
| 435 | } |
| 436 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 437 | |
| 438 | const char* term_get_ptsname(terminal_t* terminal) |
| 439 | { |
| 440 | return ptsname(shl_pty_get_fd(terminal->term->pty)); |
| 441 | } |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 442 | |
| 443 | void term_set_background(terminal_t* terminal, uint32_t bg) |
| 444 | { |
| 445 | terminal->background = bg; |
| 446 | terminal->background_valid = true; |
| 447 | } |
| 448 | |
| 449 | int term_show_image(terminal_t* terminal, image_t* image) |
| 450 | { |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 451 | return image_show(image, terminal->fb); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | void term_write_message(terminal_t* terminal, char* message) |
| 455 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 456 | FILE* fp; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 457 | |
| 458 | fp = fopen(term_get_ptsname(terminal), "w"); |
| 459 | if (fp) { |
| 460 | fputs(message, fp); |
| 461 | fclose(fp); |
| 462 | } |
| 463 | } |
| 464 | |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 465 | static void term_hide_cursor(terminal_t* terminal) |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 466 | { |
| 467 | term_write_message(terminal, "\033[?25l"); |
| 468 | } |
| 469 | |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 470 | __attribute__ ((unused)) |
| 471 | static void term_show_cursor(terminal_t* terminal) |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 472 | { |
| 473 | term_write_message(terminal, "\033[?25h"); |
| 474 | } |
David Sodman | 30a94ef | 2015-07-26 17:37:12 -0700 | [diff] [blame] | 475 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 476 | fb_t* term_getfb(terminal_t* terminal) |
David Sodman | 30a94ef | 2015-07-26 17:37:12 -0700 | [diff] [blame] | 477 | { |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 478 | return terminal->fb; |
David Sodman | 30a94ef | 2015-07-26 17:37:12 -0700 | [diff] [blame] | 479 | } |
Stéphane Marchesin | 08c08e7 | 2016-01-07 16:44:08 -0800 | [diff] [blame] | 480 | |
| 481 | terminal_t* term_get_terminal(int num) |
| 482 | { |
| 483 | return terminals[num]; |
| 484 | } |
| 485 | |
| 486 | void term_set_terminal(int num, terminal_t* terminal) |
| 487 | { |
| 488 | terminals[num] = terminal; |
| 489 | } |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 490 | |
Stéphane Marchesin | 0a7ce42 | 2016-01-07 20:45:47 -0800 | [diff] [blame] | 491 | terminal_t* term_create_term(int vt) |
| 492 | { |
| 493 | terminal_t* terminal; |
| 494 | |
| 495 | terminal = term_get_terminal(vt - 1); |
| 496 | if (term_is_active(terminal)) |
| 497 | return terminal; |
| 498 | |
| 499 | if (terminal == NULL) { |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 500 | term_set_terminal(vt - 1, term_init(false)); |
Stéphane Marchesin | 0a7ce42 | 2016-01-07 20:45:47 -0800 | [diff] [blame] | 501 | terminal = term_get_terminal(vt - 1); |
| 502 | if (!term_is_valid(terminal)) { |
| 503 | LOG(ERROR, "create_term: Term init failed"); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | return terminal; |
| 508 | } |
| 509 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 510 | terminal_t* term_create_splash_term() |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 511 | { |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 512 | terminal_t* splash_terminal = term_init(false); |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 513 | term_set_terminal(SPLASH_TERMINAL, splash_terminal); |
| 514 | |
| 515 | // Hide the cursor on the splash screen |
| 516 | term_hide_cursor(splash_terminal); |
| 517 | |
| 518 | return splash_terminal; |
| 519 | } |
| 520 | |
| 521 | void term_destroy_splash_term() |
| 522 | { |
| 523 | term_set_terminal(SPLASH_TERMINAL, NULL); |
| 524 | } |
| 525 | |
Stéphane Marchesin | 92a297d | 2016-01-07 20:24:55 -0800 | [diff] [blame] | 526 | unsigned int term_get_max_terminals() |
| 527 | { |
| 528 | return MAX_STD_TERMINALS; |
| 529 | } |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 530 | |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 531 | void term_set_current(uint32_t t) |
| 532 | { |
| 533 | if (t >= MAX_TERMINALS) |
| 534 | LOG(ERROR, "set_current: larger than max"); |
| 535 | else |
| 536 | current_terminal = t; |
| 537 | } |
| 538 | |
| 539 | uint32_t term_get_current(void) |
| 540 | { |
| 541 | return current_terminal; |
| 542 | } |
| 543 | |
| 544 | terminal_t *term_get_current_terminal(void) |
| 545 | { |
| 546 | return terminals[current_terminal]; |
| 547 | } |
| 548 | |
Dominik Behr | b1abcba | 2016-04-14 14:57:21 -0700 | [diff] [blame] | 549 | void term_set_current_terminal(terminal_t* terminal) |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 550 | { |
| 551 | terminals[current_terminal] = terminal; |
| 552 | } |
| 553 | |
| 554 | void term_set_current_to(terminal_t* terminal) |
| 555 | { |
| 556 | if (!terminal) { |
| 557 | terminals[current_terminal] = NULL; |
| 558 | current_terminal = 0; |
| 559 | return; |
| 560 | } |
| 561 | |
| 562 | for (int i = 0; i < MAX_TERMINALS; i++) { |
| 563 | if (terminal == terminals[i]) { |
| 564 | current_terminal = i; |
| 565 | return; |
| 566 | } |
| 567 | } |
| 568 | LOG(ERROR, "set_current_to: terminal not in array"); |
| 569 | } |
Dominik Behr | 01a7a58 | 2016-01-28 17:02:21 -0800 | [diff] [blame] | 570 | |
| 571 | void term_monitor_hotplug(void) |
| 572 | { |
| 573 | unsigned int t; |
| 574 | |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 575 | if (in_background) { |
| 576 | hotplug_occured = true; |
| 577 | return; |
| 578 | } |
| 579 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 580 | if (!drm_rescan()) |
| 581 | return; |
| 582 | |
Dominik Behr | 01a7a58 | 2016-01-28 17:02:21 -0800 | [diff] [blame] | 583 | for (t = 0; t < MAX_TERMINALS; t++) { |
Dominik Behr | 01a7a58 | 2016-01-28 17:02:21 -0800 | [diff] [blame] | 584 | if (!terminals[t]) |
| 585 | continue; |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 586 | if (!terminals[t]->fb) |
Dominik Behr | 01a7a58 | 2016-01-28 17:02:21 -0800 | [diff] [blame] | 587 | continue; |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 588 | fb_buffer_destroy(terminals[t]->fb); |
Dominik Behr | a3f6571 | 2016-04-25 17:42:14 -0700 | [diff] [blame^] | 589 | font_free(); |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | for (t = 0; t < MAX_TERMINALS; t++) { |
| 593 | if (!terminals[t]) |
| 594 | continue; |
| 595 | if (!terminals[t]->fb) |
| 596 | continue; |
| 597 | fb_buffer_init(terminals[t]->fb); |
| 598 | term_resize(terminals[t]); |
| 599 | if (current_terminal == t && terminals[t]->active) |
| 600 | fb_setmode(terminals[t]->fb); |
| 601 | terminals[t]->term->age = 0; |
| 602 | term_redraw(terminals[t]); |
Dominik Behr | 01a7a58 | 2016-01-28 17:02:21 -0800 | [diff] [blame] | 603 | } |
| 604 | } |
Dominik Behr | b1abcba | 2016-04-14 14:57:21 -0700 | [diff] [blame] | 605 | |
| 606 | void term_redrm(terminal_t* terminal) |
| 607 | { |
| 608 | fb_buffer_destroy(terminal->fb); |
Dominik Behr | a3f6571 | 2016-04-25 17:42:14 -0700 | [diff] [blame^] | 609 | font_free(); |
Dominik Behr | b1abcba | 2016-04-14 14:57:21 -0700 | [diff] [blame] | 610 | fb_buffer_init(terminal->fb); |
| 611 | term_resize(terminal); |
| 612 | terminal->term->age = 0; |
| 613 | term_redraw(terminal); |
| 614 | } |
| 615 | |
| 616 | void term_clear(terminal_t* terminal) |
| 617 | { |
| 618 | tsm_screen_erase_screen(terminal->term->screen, false); |
| 619 | term_redraw(terminal); |
| 620 | } |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 621 | |
| 622 | void term_background(void) |
| 623 | { |
| 624 | if (in_background) |
| 625 | return; |
| 626 | in_background = true; |
| 627 | dbus_take_display_ownership(); |
| 628 | } |
| 629 | |
| 630 | void term_foreground(void) |
| 631 | { |
| 632 | if (!in_background) |
| 633 | return; |
| 634 | in_background = false; |
| 635 | if (!dbus_release_display_ownership()) { |
| 636 | LOG(ERROR, "Chrome did not release master. Frecon will try to steal it."); |
| 637 | set_drm_master_relax(); |
| 638 | } |
| 639 | if (hotplug_occured) { |
| 640 | hotplug_occured = false; |
| 641 | term_monitor_hotplug(); |
| 642 | } |
| 643 | } |