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 | |
| 14 | #include "font.h" |
| 15 | #include "input.h" |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 16 | #include "shl_pty.h" |
| 17 | #include "term.h" |
| 18 | #include "util.h" |
| 19 | #include "video.h" |
| 20 | |
Stéphane Marchesin | 08c08e7 | 2016-01-07 16:44:08 -0800 | [diff] [blame] | 21 | static terminal_t* terminals[MAX_TERMINALS]; |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame^] | 22 | static uint32_t current_terminal = 0; |
Stéphane Marchesin | 08c08e7 | 2016-01-07 16:44:08 -0800 | [diff] [blame] | 23 | |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 24 | struct term { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 25 | struct tsm_screen* screen; |
| 26 | struct tsm_vte* vte; |
| 27 | struct shl_pty* pty; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 28 | int pty_bridge; |
| 29 | int pid; |
| 30 | tsm_age_t age; |
| 31 | int char_x, char_y; |
| 32 | int pitch; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 33 | uint32_t* dst_image; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 36 | struct _terminal_t { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 37 | uint32_t background; |
| 38 | bool background_valid; |
| 39 | video_t* video; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 40 | struct term* term; |
| 41 | bool active; |
| 42 | char** exec; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 45 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 46 | static char* interactive_cmd_line[] = { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 47 | "/sbin/agetty", |
| 48 | "-", |
| 49 | "9600", |
| 50 | "xterm", |
| 51 | NULL |
| 52 | }; |
| 53 | |
| 54 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 55 | static char* noninteractive_cmd_line[] = { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 56 | "/bin/cat", |
| 57 | NULL |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | static void __attribute__ ((noreturn)) term_run_child(terminal_t* terminal) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 62 | { |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 63 | /* XXX figure out how to fix "top" for xterm-256color */ |
| 64 | setenv("TERM", "xterm", 1); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 65 | execve(terminal->exec[0], terminal->exec, environ); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 66 | exit(1); |
| 67 | } |
| 68 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 69 | 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] | 70 | const uint32_t* ch, size_t len, |
| 71 | unsigned int cwidth, unsigned int posx, |
| 72 | unsigned int posy, |
| 73 | const struct tsm_screen_attr* attr, |
| 74 | tsm_age_t age, void* data) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 75 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 76 | terminal_t* terminal = (terminal_t*)data; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 77 | uint32_t front_color, back_color; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 78 | uint8_t br, bb, bg; |
Stéphane Marchesin | edece33 | 2015-12-14 15:10:58 -0800 | [diff] [blame] | 79 | uint32_t luminance; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 80 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 81 | if (age && terminal->term->age && age <= terminal->term->age) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 82 | return 0; |
| 83 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 84 | if (terminal->background_valid) { |
| 85 | br = (terminal->background >> 16) & 0xFF; |
| 86 | bg = (terminal->background >> 8) & 0xFF; |
| 87 | bb = (terminal->background) & 0xFF; |
Stéphane Marchesin | edece33 | 2015-12-14 15:10:58 -0800 | [diff] [blame] | 88 | luminance = (3 * br + bb + 4 * bg) >> 3; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 89 | |
Stéphane Marchesin | edece33 | 2015-12-14 15:10:58 -0800 | [diff] [blame] | 90 | /* |
| 91 | * FIXME: black is chosen on a dark background, but it uses the |
| 92 | * default color for light backgrounds |
| 93 | */ |
| 94 | if (luminance > 128) { |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 95 | front_color = 0; |
| 96 | back_color = terminal->background; |
| 97 | } else { |
| 98 | front_color = (attr->fr << 16) | (attr->fg << 8) | attr->fb; |
| 99 | back_color = terminal->background; |
| 100 | } |
| 101 | } else { |
| 102 | front_color = (attr->fr << 16) | (attr->fg << 8) | attr->fb; |
| 103 | back_color = (attr->br << 16) | (attr->bg << 8) | attr->bb; |
| 104 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 105 | |
| 106 | if (attr->inverse) { |
| 107 | uint32_t tmp = front_color; |
| 108 | front_color = back_color; |
| 109 | back_color = tmp; |
| 110 | } |
| 111 | |
| 112 | if (len) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 113 | font_render(terminal->term->dst_image, posx, posy, terminal->term->pitch, *ch, |
| 114 | front_color, back_color); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 115 | else |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 116 | font_fillchar(terminal->term->dst_image, posx, posy, terminal->term->pitch, |
| 117 | front_color, back_color); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
Stéphane Marchesin | e2a46cd | 2016-01-07 16:45:24 -0800 | [diff] [blame] | 122 | static void term_redraw(terminal_t* terminal) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 123 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 124 | uint32_t* video_buffer; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 125 | video_buffer = video_lock(terminal->video); |
| 126 | if (video_buffer != NULL) { |
| 127 | terminal->term->dst_image = video_buffer; |
| 128 | terminal->term->age = |
| 129 | tsm_screen_draw(terminal->term->screen, term_draw_cell, terminal); |
| 130 | video_unlock(terminal->video); |
| 131 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 132 | } |
| 133 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 134 | 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] | 135 | { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 136 | if (tsm_vte_handle_keyboard(terminal->term->vte, keysym, 0, 0, unicode)) |
| 137 | tsm_screen_sb_reset(terminal->term->screen); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 138 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 139 | term_redraw(terminal); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 142 | 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] | 143 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 144 | terminal_t* terminal = (terminal_t*)data; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 145 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 146 | tsm_vte_input(terminal->term->vte, u8, len); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 147 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 148 | term_redraw(terminal); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 151 | static void term_write_cb(struct tsm_vte* vte, const char* u8, size_t len, |
| 152 | void* data) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 153 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 154 | struct term* term = data; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 155 | int r; |
| 156 | |
| 157 | r = shl_pty_write(term->pty, u8, len); |
| 158 | if (r < 0) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 159 | LOG(ERROR, "OOM in pty-write (%d)", r); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 160 | |
| 161 | shl_pty_dispatch(term->pty); |
| 162 | } |
| 163 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 164 | static const char* sev2str_table[] = { |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 165 | "FATAL", |
| 166 | "ALERT", |
| 167 | "CRITICAL", |
| 168 | "ERROR", |
| 169 | "WARNING", |
| 170 | "NOTICE", |
| 171 | "INFO", |
| 172 | "DEBUG" |
| 173 | }; |
| 174 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 175 | static const char* sev2str(unsigned int sev) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 176 | { |
| 177 | if (sev > 7) |
| 178 | return "DEBUG"; |
| 179 | |
| 180 | return sev2str_table[sev]; |
| 181 | } |
| 182 | |
Dominik Behr | 0000350 | 2014-08-15 16:42:37 -0700 | [diff] [blame] | 183 | #ifdef __clang__ |
| 184 | __attribute__((__format__ (__printf__, 7, 0))) |
| 185 | #endif |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 186 | 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] | 187 | const char* subs, unsigned int sev, const char* format, |
| 188 | va_list args) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 189 | { |
| 190 | fprintf(stderr, "%s: %s: ", sev2str(sev), subs); |
| 191 | vfprintf(stderr, format, args); |
| 192 | fprintf(stderr, "\n"); |
| 193 | } |
| 194 | |
Stéphane Marchesin | 7831066 | 2016-01-06 16:09:39 -0800 | [diff] [blame] | 195 | static int term_resize(terminal_t* term) |
| 196 | { |
| 197 | uint32_t char_width, char_height; |
| 198 | int status; |
| 199 | |
| 200 | font_init(video_getscaling(term->video)); |
| 201 | font_get_size(&char_width, &char_height); |
| 202 | |
| 203 | term->term->char_x = video_getwidth(term->video) / char_width; |
| 204 | term->term->char_y = video_getheight(term->video) / char_height; |
| 205 | term->term->pitch = video_getpitch(term->video); |
| 206 | |
| 207 | status = tsm_screen_resize(term->term->screen, |
| 208 | term->term->char_x, term->term->char_y); |
| 209 | if (status < 0) { |
| 210 | font_free(); |
| 211 | return -1; |
| 212 | } |
| 213 | |
| 214 | status = shl_pty_resize(term->term->pty, term->term->char_x, |
| 215 | term->term->char_y); |
| 216 | if (status < 0) { |
| 217 | font_free(); |
| 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 224 | terminal_t* term_init(bool interactive, video_t* video) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 225 | { |
| 226 | const int scrollback_size = 200; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 227 | int status; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 228 | terminal_t* new_terminal; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 229 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 230 | new_terminal = (terminal_t*)calloc(1, sizeof(*new_terminal)); |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 231 | if (!new_terminal) |
| 232 | return NULL; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 233 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 234 | new_terminal->background_valid = false; |
| 235 | |
David Sodman | 30a94ef | 2015-07-26 17:37:12 -0700 | [diff] [blame] | 236 | if (video) { |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 237 | new_terminal->video = video; |
David Sodman | 30a94ef | 2015-07-26 17:37:12 -0700 | [diff] [blame] | 238 | video_addref(video); |
| 239 | } |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 240 | else |
| 241 | new_terminal->video = video_init(); |
| 242 | |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 243 | if (!new_terminal->video) { |
| 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); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 303 | if (status < 0) { |
| 304 | shl_pty_close(new_terminal->term->pty); |
| 305 | term_close(new_terminal); |
| 306 | return NULL; |
| 307 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 308 | |
| 309 | if (interactive) |
| 310 | new_terminal->active = true; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 311 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 312 | return new_terminal; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 313 | } |
| 314 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 315 | void term_activate(terminal_t* terminal) |
| 316 | { |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame^] | 317 | term_set_current_to(terminal); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 318 | terminal->active = true; |
| 319 | video_setmode(terminal->video); |
| 320 | term_redraw(terminal); |
| 321 | } |
| 322 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 323 | void term_deactivate(terminal_t* terminal) |
| 324 | { |
| 325 | if (!terminal->active) |
| 326 | return; |
| 327 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 328 | terminal->active = false; |
| 329 | video_release(terminal->video); |
| 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 | |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 337 | if (term->video) { |
| 338 | video_close(term->video); |
| 339 | term->video = NULL; |
| 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 | |
Stéphane Marchesin | 905a6f2 | 2016-01-07 22:02:07 -0800 | [diff] [blame] | 427 | int term_add_fds(terminal_t* terminal, fd_set* read_set, fd_set* exception_set) |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 428 | { |
Stéphane Marchesin | 905a6f2 | 2016-01-07 22:02:07 -0800 | [diff] [blame] | 429 | int maxfd = 0; |
| 430 | |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 431 | if (term_is_valid(terminal)) { |
| 432 | if (terminal->term->pty_bridge >= 0) { |
Stéphane Marchesin | 905a6f2 | 2016-01-07 22:02:07 -0800 | [diff] [blame] | 433 | maxfd = MAX(maxfd, terminal->term->pty_bridge); |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 434 | FD_SET(terminal->term->pty_bridge, read_set); |
| 435 | FD_SET(terminal->term->pty_bridge, exception_set); |
| 436 | } |
| 437 | } |
Stéphane Marchesin | 905a6f2 | 2016-01-07 22:02:07 -0800 | [diff] [blame] | 438 | return maxfd; |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 439 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 440 | |
| 441 | const char* term_get_ptsname(terminal_t* terminal) |
| 442 | { |
| 443 | return ptsname(shl_pty_get_fd(terminal->term->pty)); |
| 444 | } |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 445 | |
| 446 | void term_set_background(terminal_t* terminal, uint32_t bg) |
| 447 | { |
| 448 | terminal->background = bg; |
| 449 | terminal->background_valid = true; |
| 450 | } |
| 451 | |
| 452 | int term_show_image(terminal_t* terminal, image_t* image) |
| 453 | { |
| 454 | return image_show(image, terminal->video); |
| 455 | } |
| 456 | |
| 457 | void term_write_message(terminal_t* terminal, char* message) |
| 458 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 459 | FILE* fp; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 460 | |
| 461 | fp = fopen(term_get_ptsname(terminal), "w"); |
| 462 | if (fp) { |
| 463 | fputs(message, fp); |
| 464 | fclose(fp); |
| 465 | } |
| 466 | } |
| 467 | |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 468 | static void term_hide_cursor(terminal_t* terminal) |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 469 | { |
| 470 | term_write_message(terminal, "\033[?25l"); |
| 471 | } |
| 472 | |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 473 | __attribute__ ((unused)) |
| 474 | static void term_show_cursor(terminal_t* terminal) |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 475 | { |
| 476 | term_write_message(terminal, "\033[?25h"); |
| 477 | } |
David Sodman | 30a94ef | 2015-07-26 17:37:12 -0700 | [diff] [blame] | 478 | |
| 479 | video_t* term_getvideo(terminal_t* terminal) |
| 480 | { |
| 481 | return terminal->video; |
| 482 | } |
Stéphane Marchesin | 08c08e7 | 2016-01-07 16:44:08 -0800 | [diff] [blame] | 483 | |
| 484 | terminal_t* term_get_terminal(int num) |
| 485 | { |
| 486 | return terminals[num]; |
| 487 | } |
| 488 | |
| 489 | void term_set_terminal(int num, terminal_t* terminal) |
| 490 | { |
| 491 | terminals[num] = terminal; |
| 492 | } |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 493 | |
Stéphane Marchesin | 0a7ce42 | 2016-01-07 20:45:47 -0800 | [diff] [blame] | 494 | terminal_t* term_create_term(int vt) |
| 495 | { |
| 496 | terminal_t* terminal; |
| 497 | |
| 498 | terminal = term_get_terminal(vt - 1); |
| 499 | if (term_is_active(terminal)) |
| 500 | return terminal; |
| 501 | |
| 502 | if (terminal == NULL) { |
| 503 | term_set_terminal(vt - 1, term_init(false, NULL)); |
| 504 | terminal = term_get_terminal(vt - 1); |
| 505 | if (!term_is_valid(terminal)) { |
| 506 | LOG(ERROR, "create_term: Term init failed"); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | return terminal; |
| 511 | } |
| 512 | |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 513 | terminal_t* term_create_splash_term(video_t* video) |
| 514 | { |
| 515 | terminal_t* splash_terminal = term_init(false, video); |
| 516 | term_set_terminal(SPLASH_TERMINAL, splash_terminal); |
| 517 | |
| 518 | // Hide the cursor on the splash screen |
| 519 | term_hide_cursor(splash_terminal); |
| 520 | |
| 521 | return splash_terminal; |
| 522 | } |
| 523 | |
| 524 | void term_destroy_splash_term() |
| 525 | { |
| 526 | term_set_terminal(SPLASH_TERMINAL, NULL); |
| 527 | } |
| 528 | |
Stéphane Marchesin | 92a297d | 2016-01-07 20:24:55 -0800 | [diff] [blame] | 529 | unsigned int term_get_max_terminals() |
| 530 | { |
| 531 | return MAX_STD_TERMINALS; |
| 532 | } |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 533 | |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame^] | 534 | void term_set_current(uint32_t t) |
| 535 | { |
| 536 | if (t >= MAX_TERMINALS) |
| 537 | LOG(ERROR, "set_current: larger than max"); |
| 538 | else |
| 539 | current_terminal = t; |
| 540 | } |
| 541 | |
| 542 | uint32_t term_get_current(void) |
| 543 | { |
| 544 | return current_terminal; |
| 545 | } |
| 546 | |
| 547 | terminal_t *term_get_current_terminal(void) |
| 548 | { |
| 549 | return terminals[current_terminal]; |
| 550 | } |
| 551 | |
| 552 | void term_set_current_terminal(terminal_t *terminal) |
| 553 | { |
| 554 | terminals[current_terminal] = terminal; |
| 555 | } |
| 556 | |
| 557 | void term_set_current_to(terminal_t* terminal) |
| 558 | { |
| 559 | if (!terminal) { |
| 560 | terminals[current_terminal] = NULL; |
| 561 | current_terminal = 0; |
| 562 | return; |
| 563 | } |
| 564 | |
| 565 | for (int i = 0; i < MAX_TERMINALS; i++) { |
| 566 | if (terminal == terminals[i]) { |
| 567 | current_terminal = i; |
| 568 | return; |
| 569 | } |
| 570 | } |
| 571 | LOG(ERROR, "set_current_to: terminal not in array"); |
| 572 | } |