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