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 | 580462b | 2014-11-20 13:50:05 -0800 | [diff] [blame] | 14 | #include "dbus_interface.h" |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 15 | #include "font.h" |
| 16 | #include "input.h" |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 17 | #include "shl_pty.h" |
| 18 | #include "term.h" |
| 19 | #include "util.h" |
| 20 | #include "video.h" |
| 21 | |
Stéphane Marchesin | 08c08e7 | 2016-01-07 16:44:08 -0800 | [diff] [blame] | 22 | static terminal_t* terminals[MAX_TERMINALS]; |
| 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; |
| 40 | dbus_t* dbus; |
| 41 | struct term* term; |
| 42 | bool active; |
| 43 | char** exec; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 46 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 47 | static char* interactive_cmd_line[] = { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 48 | "/sbin/agetty", |
| 49 | "-", |
| 50 | "9600", |
| 51 | "xterm", |
| 52 | NULL |
| 53 | }; |
| 54 | |
| 55 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 56 | static char* noninteractive_cmd_line[] = { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 57 | "/bin/cat", |
| 58 | NULL |
| 59 | }; |
| 60 | |
| 61 | |
| 62 | static void __attribute__ ((noreturn)) term_run_child(terminal_t* terminal) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 63 | { |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 64 | /* XXX figure out how to fix "top" for xterm-256color */ |
| 65 | setenv("TERM", "xterm", 1); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 66 | execve(terminal->exec[0], terminal->exec, environ); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 67 | exit(1); |
| 68 | } |
| 69 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 70 | 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] | 71 | const uint32_t* ch, size_t len, |
| 72 | unsigned int cwidth, unsigned int posx, |
| 73 | unsigned int posy, |
| 74 | const struct tsm_screen_attr* attr, |
| 75 | tsm_age_t age, void* data) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 76 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 77 | terminal_t* terminal = (terminal_t*)data; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 78 | uint32_t front_color, back_color; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 79 | uint8_t br, bb, bg; |
Stéphane Marchesin | edece33 | 2015-12-14 15:10:58 -0800 | [diff] [blame] | 80 | uint32_t luminance; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 81 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 82 | if (age && terminal->term->age && age <= terminal->term->age) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 83 | return 0; |
| 84 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 85 | if (terminal->background_valid) { |
| 86 | br = (terminal->background >> 16) & 0xFF; |
| 87 | bg = (terminal->background >> 8) & 0xFF; |
| 88 | bb = (terminal->background) & 0xFF; |
Stéphane Marchesin | edece33 | 2015-12-14 15:10:58 -0800 | [diff] [blame] | 89 | luminance = (3 * br + bb + 4 * bg) >> 3; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 90 | |
Stéphane Marchesin | edece33 | 2015-12-14 15:10:58 -0800 | [diff] [blame] | 91 | /* |
| 92 | * FIXME: black is chosen on a dark background, but it uses the |
| 93 | * default color for light backgrounds |
| 94 | */ |
| 95 | if (luminance > 128) { |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 96 | front_color = 0; |
| 97 | back_color = terminal->background; |
| 98 | } else { |
| 99 | front_color = (attr->fr << 16) | (attr->fg << 8) | attr->fb; |
| 100 | back_color = terminal->background; |
| 101 | } |
| 102 | } else { |
| 103 | front_color = (attr->fr << 16) | (attr->fg << 8) | attr->fb; |
| 104 | back_color = (attr->br << 16) | (attr->bg << 8) | attr->bb; |
| 105 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 106 | |
| 107 | if (attr->inverse) { |
| 108 | uint32_t tmp = front_color; |
| 109 | front_color = back_color; |
| 110 | back_color = tmp; |
| 111 | } |
| 112 | |
| 113 | if (len) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 114 | font_render(terminal->term->dst_image, posx, posy, terminal->term->pitch, *ch, |
| 115 | front_color, back_color); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 116 | else |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 117 | font_fillchar(terminal->term->dst_image, posx, posy, terminal->term->pitch, |
| 118 | front_color, back_color); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
Stéphane Marchesin | e2a46cd | 2016-01-07 16:45:24 -0800 | [diff] [blame] | 123 | static void term_redraw(terminal_t* terminal) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 124 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 125 | uint32_t* video_buffer; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 126 | video_buffer = video_lock(terminal->video); |
| 127 | if (video_buffer != NULL) { |
| 128 | terminal->term->dst_image = video_buffer; |
| 129 | terminal->term->age = |
| 130 | tsm_screen_draw(terminal->term->screen, term_draw_cell, terminal); |
| 131 | video_unlock(terminal->video); |
| 132 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 133 | } |
| 134 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 135 | 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] | 136 | { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 137 | if (tsm_vte_handle_keyboard(terminal->term->vte, keysym, 0, 0, unicode)) |
| 138 | tsm_screen_sb_reset(terminal->term->screen); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 139 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 140 | term_redraw(terminal); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 143 | 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] | 144 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 145 | terminal_t* terminal = (terminal_t*)data; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 146 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 147 | tsm_vte_input(terminal->term->vte, u8, len); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 148 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 149 | term_redraw(terminal); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 152 | static void term_write_cb(struct tsm_vte* vte, const char* u8, size_t len, |
| 153 | void* data) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 154 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 155 | struct term* term = data; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 156 | int r; |
| 157 | |
| 158 | r = shl_pty_write(term->pty, u8, len); |
| 159 | if (r < 0) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 160 | LOG(ERROR, "OOM in pty-write (%d)", r); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 161 | |
| 162 | shl_pty_dispatch(term->pty); |
| 163 | } |
| 164 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 165 | static const char* sev2str_table[] = { |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 166 | "FATAL", |
| 167 | "ALERT", |
| 168 | "CRITICAL", |
| 169 | "ERROR", |
| 170 | "WARNING", |
| 171 | "NOTICE", |
| 172 | "INFO", |
| 173 | "DEBUG" |
| 174 | }; |
| 175 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 176 | static const char* sev2str(unsigned int sev) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 177 | { |
| 178 | if (sev > 7) |
| 179 | return "DEBUG"; |
| 180 | |
| 181 | return sev2str_table[sev]; |
| 182 | } |
| 183 | |
Dominik Behr | 0000350 | 2014-08-15 16:42:37 -0700 | [diff] [blame] | 184 | #ifdef __clang__ |
| 185 | __attribute__((__format__ (__printf__, 7, 0))) |
| 186 | #endif |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 187 | 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] | 188 | const char* subs, unsigned int sev, const char* format, |
| 189 | va_list args) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 190 | { |
| 191 | fprintf(stderr, "%s: %s: ", sev2str(sev), subs); |
| 192 | vfprintf(stderr, format, args); |
| 193 | fprintf(stderr, "\n"); |
| 194 | } |
| 195 | |
Stéphane Marchesin | 7831066 | 2016-01-06 16:09:39 -0800 | [diff] [blame] | 196 | static int term_resize(terminal_t* term) |
| 197 | { |
| 198 | uint32_t char_width, char_height; |
| 199 | int status; |
| 200 | |
| 201 | font_init(video_getscaling(term->video)); |
| 202 | font_get_size(&char_width, &char_height); |
| 203 | |
| 204 | term->term->char_x = video_getwidth(term->video) / char_width; |
| 205 | term->term->char_y = video_getheight(term->video) / char_height; |
| 206 | term->term->pitch = video_getpitch(term->video); |
| 207 | |
| 208 | status = tsm_screen_resize(term->term->screen, |
| 209 | term->term->char_x, term->term->char_y); |
| 210 | if (status < 0) { |
| 211 | font_free(); |
| 212 | return -1; |
| 213 | } |
| 214 | |
| 215 | status = shl_pty_resize(term->term->pty, term->term->char_x, |
| 216 | term->term->char_y); |
| 217 | if (status < 0) { |
| 218 | font_free(); |
| 219 | return -1; |
| 220 | } |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 225 | terminal_t* term_init(bool interactive, video_t* video) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 226 | { |
| 227 | const int scrollback_size = 200; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 228 | int status; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 229 | terminal_t* new_terminal; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 230 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 231 | new_terminal = (terminal_t*)calloc(1, sizeof(*new_terminal)); |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 232 | if (!new_terminal) |
| 233 | return NULL; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 234 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 235 | new_terminal->background_valid = false; |
| 236 | |
David Sodman | 30a94ef | 2015-07-26 17:37:12 -0700 | [diff] [blame] | 237 | if (video) { |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 238 | new_terminal->video = video; |
David Sodman | 30a94ef | 2015-07-26 17:37:12 -0700 | [diff] [blame] | 239 | video_addref(video); |
| 240 | } |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 241 | else |
| 242 | new_terminal->video = video_init(); |
| 243 | |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 244 | if (!new_terminal->video) { |
| 245 | term_close(new_terminal); |
| 246 | return NULL; |
| 247 | } |
| 248 | |
| 249 | new_terminal->term = (struct term*)calloc(1, sizeof(*new_terminal->term)); |
| 250 | if (!new_terminal->term) { |
| 251 | term_close(new_terminal); |
| 252 | return NULL; |
| 253 | } |
| 254 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 255 | if (interactive) |
| 256 | new_terminal->exec = interactive_cmd_line; |
| 257 | else |
| 258 | new_terminal->exec = noninteractive_cmd_line; |
| 259 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 260 | status = tsm_screen_new(&new_terminal->term->screen, |
| 261 | log_tsm, new_terminal->term); |
zhuo-hao | 471f1e2 | 2015-08-12 14:55:56 +0800 | [diff] [blame] | 262 | if (status < 0) { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 263 | term_close(new_terminal); |
| 264 | return NULL; |
| 265 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 266 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 267 | tsm_screen_set_max_sb(new_terminal->term->screen, scrollback_size); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 268 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 269 | status = tsm_vte_new(&new_terminal->term->vte, new_terminal->term->screen, |
| 270 | term_write_cb, new_terminal->term, log_tsm, new_terminal->term); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 271 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 272 | if (status < 0) { |
| 273 | term_close(new_terminal); |
| 274 | return NULL; |
| 275 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 276 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 277 | new_terminal->term->pty_bridge = shl_pty_bridge_new(); |
| 278 | if (new_terminal->term->pty_bridge < 0) { |
| 279 | term_close(new_terminal); |
| 280 | return NULL; |
| 281 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 282 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 283 | status = shl_pty_open(&new_terminal->term->pty, |
Stéphane Marchesin | 7831066 | 2016-01-06 16:09:39 -0800 | [diff] [blame] | 284 | term_read_cb, new_terminal, 1, 1); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 285 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 286 | if (status < 0) { |
| 287 | term_close(new_terminal); |
| 288 | return NULL; |
| 289 | } else if (status == 0) { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 290 | term_run_child(new_terminal); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 291 | exit(1); |
| 292 | } |
| 293 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 294 | status = shl_pty_bridge_add(new_terminal->term->pty_bridge, new_terminal->term->pty); |
| 295 | if (status) { |
| 296 | shl_pty_close(new_terminal->term->pty); |
| 297 | term_close(new_terminal); |
| 298 | return NULL; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 299 | } |
| 300 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 301 | 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] | 302 | |
Stéphane Marchesin | 7831066 | 2016-01-06 16:09:39 -0800 | [diff] [blame] | 303 | status = term_resize(new_terminal); |
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 | { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 318 | input_set_current(terminal); |
| 319 | terminal->active = true; |
| 320 | video_setmode(terminal->video); |
| 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; |
| 330 | video_release(terminal->video); |
| 331 | } |
| 332 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 333 | void term_set_dbus(terminal_t* term, dbus_t* dbus) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 334 | { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 335 | term->dbus = dbus; |
| 336 | } |
| 337 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 338 | void term_close(terminal_t* term) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 339 | { |
| 340 | if (!term) |
| 341 | return; |
| 342 | |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 343 | if (term->video) { |
| 344 | video_close(term->video); |
| 345 | term->video = NULL; |
| 346 | } |
| 347 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 348 | if (term->term) { |
| 349 | free(term->term); |
| 350 | term->term = NULL; |
| 351 | } |
| 352 | |
Haixia Shi | 95d680e | 2015-04-27 20:29:17 -0700 | [diff] [blame] | 353 | font_free(); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 354 | free(term); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 355 | } |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 356 | |
| 357 | bool term_is_child_done(terminal_t* terminal) |
| 358 | { |
| 359 | int status; |
| 360 | int ret; |
| 361 | ret = waitpid(terminal->term->pid, &status, WNOHANG); |
| 362 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 363 | if ((ret == -1) && (errno == ECHILD)) { |
| 364 | return false; |
| 365 | } |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 366 | return ret != 0; |
| 367 | } |
| 368 | |
| 369 | void term_page_up(terminal_t* terminal) |
| 370 | { |
| 371 | tsm_screen_sb_page_up(terminal->term->screen, 1); |
| 372 | term_redraw(terminal); |
| 373 | } |
| 374 | |
| 375 | void term_page_down(terminal_t* terminal) |
| 376 | { |
| 377 | tsm_screen_sb_page_down(terminal->term->screen, 1); |
| 378 | term_redraw(terminal); |
| 379 | } |
| 380 | |
| 381 | void term_line_up(terminal_t* terminal) |
| 382 | { |
| 383 | tsm_screen_sb_up(terminal->term->screen, 1); |
| 384 | term_redraw(terminal); |
| 385 | } |
| 386 | |
| 387 | void term_line_down(terminal_t* terminal) |
| 388 | { |
| 389 | tsm_screen_sb_down(terminal->term->screen, 1); |
| 390 | term_redraw(terminal); |
| 391 | } |
| 392 | |
| 393 | bool term_is_valid(terminal_t* terminal) |
| 394 | { |
| 395 | return ((terminal != NULL) && (terminal->term != NULL)); |
| 396 | } |
| 397 | |
| 398 | int term_fd(terminal_t* terminal) |
| 399 | { |
| 400 | if (term_is_valid(terminal)) |
| 401 | return terminal->term->pty_bridge; |
| 402 | else |
| 403 | return -1; |
| 404 | } |
| 405 | |
| 406 | void term_dispatch_io(terminal_t* terminal, fd_set* read_set) |
| 407 | { |
| 408 | if (term_is_valid(terminal)) |
| 409 | if (FD_ISSET(terminal->term->pty_bridge, read_set)) |
| 410 | shl_pty_bridge_dispatch(terminal->term->pty_bridge, 0); |
| 411 | } |
| 412 | |
| 413 | bool term_exception(terminal_t* terminal, fd_set* exception_set) |
| 414 | { |
| 415 | if (term_is_valid(terminal)) { |
| 416 | if (terminal->term->pty_bridge >= 0) { |
| 417 | return FD_ISSET(terminal->term->pty_bridge, |
| 418 | exception_set); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | return false; |
| 423 | } |
| 424 | |
| 425 | bool term_is_active(terminal_t* terminal) |
| 426 | { |
| 427 | if (term_is_valid(terminal)) |
| 428 | return terminal->active; |
| 429 | |
| 430 | return false; |
| 431 | } |
| 432 | |
| 433 | void term_add_fd(terminal_t* terminal, fd_set* read_set, fd_set* exception_set) |
| 434 | { |
| 435 | if (term_is_valid(terminal)) { |
| 436 | if (terminal->term->pty_bridge >= 0) { |
| 437 | FD_SET(terminal->term->pty_bridge, read_set); |
| 438 | FD_SET(terminal->term->pty_bridge, exception_set); |
| 439 | } |
| 440 | } |
| 441 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 442 | |
| 443 | const char* term_get_ptsname(terminal_t* terminal) |
| 444 | { |
| 445 | return ptsname(shl_pty_get_fd(terminal->term->pty)); |
| 446 | } |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 447 | |
| 448 | void term_set_background(terminal_t* terminal, uint32_t bg) |
| 449 | { |
| 450 | terminal->background = bg; |
| 451 | terminal->background_valid = true; |
| 452 | } |
| 453 | |
| 454 | int term_show_image(terminal_t* terminal, image_t* image) |
| 455 | { |
| 456 | return image_show(image, terminal->video); |
| 457 | } |
| 458 | |
| 459 | void term_write_message(terminal_t* terminal, char* message) |
| 460 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 461 | FILE* fp; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 462 | |
| 463 | fp = fopen(term_get_ptsname(terminal), "w"); |
| 464 | if (fp) { |
| 465 | fputs(message, fp); |
| 466 | fclose(fp); |
| 467 | } |
| 468 | } |
| 469 | |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame^] | 470 | static void term_hide_cursor(terminal_t* terminal) |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 471 | { |
| 472 | term_write_message(terminal, "\033[?25l"); |
| 473 | } |
| 474 | |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame^] | 475 | __attribute__ ((unused)) |
| 476 | static void term_show_cursor(terminal_t* terminal) |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 477 | { |
| 478 | term_write_message(terminal, "\033[?25h"); |
| 479 | } |
David Sodman | 30a94ef | 2015-07-26 17:37:12 -0700 | [diff] [blame] | 480 | |
| 481 | video_t* term_getvideo(terminal_t* terminal) |
| 482 | { |
| 483 | return terminal->video; |
| 484 | } |
Stéphane Marchesin | 08c08e7 | 2016-01-07 16:44:08 -0800 | [diff] [blame] | 485 | |
| 486 | terminal_t* term_get_terminal(int num) |
| 487 | { |
| 488 | return terminals[num]; |
| 489 | } |
| 490 | |
| 491 | void term_set_terminal(int num, terminal_t* terminal) |
| 492 | { |
| 493 | terminals[num] = terminal; |
| 494 | } |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame^] | 495 | |
| 496 | terminal_t* term_create_splash_term(video_t* video) |
| 497 | { |
| 498 | terminal_t* splash_terminal = term_init(false, video); |
| 499 | term_set_terminal(SPLASH_TERMINAL, splash_terminal); |
| 500 | |
| 501 | // Hide the cursor on the splash screen |
| 502 | term_hide_cursor(splash_terminal); |
| 503 | |
| 504 | return splash_terminal; |
| 505 | } |
| 506 | |
| 507 | void term_destroy_splash_term() |
| 508 | { |
| 509 | term_set_terminal(SPLASH_TERMINAL, NULL); |
| 510 | } |
| 511 | |
| 512 | |