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 | |
| 204 | if (video) |
| 205 | new_terminal->video = video; |
| 206 | else |
| 207 | new_terminal->video = video_init(); |
| 208 | |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 209 | if (!new_terminal->video) { |
| 210 | term_close(new_terminal); |
| 211 | return NULL; |
| 212 | } |
| 213 | |
| 214 | new_terminal->term = (struct term*)calloc(1, sizeof(*new_terminal->term)); |
| 215 | if (!new_terminal->term) { |
| 216 | term_close(new_terminal); |
| 217 | return NULL; |
| 218 | } |
| 219 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 220 | if (interactive) |
| 221 | new_terminal->exec = interactive_cmd_line; |
| 222 | else |
| 223 | new_terminal->exec = noninteractive_cmd_line; |
| 224 | |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 225 | font_init(video_getscaling(new_terminal->video)); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 226 | font_get_size(&char_width, &char_height); |
| 227 | |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 228 | new_terminal->term->char_x = |
| 229 | video_getwidth(new_terminal->video) / char_width; |
| 230 | new_terminal->term->char_y = |
| 231 | video_getheight(new_terminal->video) / char_height; |
| 232 | new_terminal->term->pitch = video_getpitch(new_terminal->video); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 233 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 234 | status = tsm_screen_new(&new_terminal->term->screen, |
| 235 | log_tsm, new_terminal->term); |
| 236 | if (new_terminal < 0) { |
| 237 | term_close(new_terminal); |
| 238 | return NULL; |
| 239 | } |
| 240 | |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 241 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 242 | tsm_screen_set_max_sb(new_terminal->term->screen, scrollback_size); |
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 | status = tsm_vte_new(&new_terminal->term->vte, new_terminal->term->screen, |
| 245 | term_write_cb, new_terminal->term, log_tsm, new_terminal->term); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 246 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 247 | if (status < 0) { |
| 248 | term_close(new_terminal); |
| 249 | return NULL; |
| 250 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 251 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 252 | new_terminal->term->pty_bridge = shl_pty_bridge_new(); |
| 253 | if (new_terminal->term->pty_bridge < 0) { |
| 254 | term_close(new_terminal); |
| 255 | return NULL; |
| 256 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 257 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 258 | status = shl_pty_open(&new_terminal->term->pty, |
| 259 | term_read_cb, new_terminal, new_terminal->term->char_x, |
| 260 | new_terminal->term->char_y); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 261 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 262 | if (status < 0) { |
| 263 | term_close(new_terminal); |
| 264 | return NULL; |
| 265 | } else if (status == 0) { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 266 | term_run_child(new_terminal); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 267 | exit(1); |
| 268 | } |
| 269 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 270 | status = shl_pty_bridge_add(new_terminal->term->pty_bridge, new_terminal->term->pty); |
| 271 | if (status) { |
| 272 | shl_pty_close(new_terminal->term->pty); |
| 273 | term_close(new_terminal); |
| 274 | return NULL; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 275 | } |
| 276 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 277 | 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] | 278 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 279 | status = tsm_screen_resize(new_terminal->term->screen, |
| 280 | new_terminal->term->char_x, new_terminal->term->char_y); |
| 281 | if (status < 0) { |
| 282 | shl_pty_close(new_terminal->term->pty); |
| 283 | term_close(new_terminal); |
| 284 | return NULL; |
| 285 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 286 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 287 | status = shl_pty_resize(new_terminal->term->pty, new_terminal->term->char_x, new_terminal->term->char_y); |
| 288 | if (status < 0) { |
| 289 | shl_pty_close(new_terminal->term->pty); |
| 290 | term_close(new_terminal); |
| 291 | return NULL; |
| 292 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 293 | |
| 294 | if (interactive) |
| 295 | new_terminal->active = true; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 296 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 297 | return new_terminal; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 298 | } |
| 299 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 300 | void term_activate(terminal_t* terminal) |
| 301 | { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 302 | input_set_current(terminal); |
| 303 | terminal->active = true; |
| 304 | video_setmode(terminal->video); |
| 305 | term_redraw(terminal); |
| 306 | } |
| 307 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 308 | void term_deactivate(terminal_t* terminal) |
| 309 | { |
| 310 | if (!terminal->active) |
| 311 | return; |
| 312 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 313 | terminal->active = false; |
| 314 | video_release(terminal->video); |
| 315 | } |
| 316 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 317 | void term_set_dbus(terminal_t *term, dbus_t* dbus) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 318 | { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 319 | term->dbus = dbus; |
| 320 | } |
| 321 | |
| 322 | void term_close(terminal_t *term) |
| 323 | { |
| 324 | if (!term) |
| 325 | return; |
| 326 | |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 327 | if (term->video) { |
| 328 | video_close(term->video); |
| 329 | term->video = NULL; |
| 330 | } |
| 331 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 332 | if (term->term) { |
| 333 | free(term->term); |
| 334 | term->term = NULL; |
| 335 | } |
| 336 | |
Haixia Shi | 95d680e | 2015-04-27 20:29:17 -0700 | [diff] [blame] | 337 | font_free(); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 338 | free(term); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 339 | } |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 340 | |
| 341 | bool term_is_child_done(terminal_t* terminal) |
| 342 | { |
| 343 | int status; |
| 344 | int ret; |
| 345 | ret = waitpid(terminal->term->pid, &status, WNOHANG); |
| 346 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 347 | if ((ret == -1) && (errno == ECHILD)) { |
| 348 | return false; |
| 349 | } |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 350 | return ret != 0; |
| 351 | } |
| 352 | |
| 353 | void term_page_up(terminal_t* terminal) |
| 354 | { |
| 355 | tsm_screen_sb_page_up(terminal->term->screen, 1); |
| 356 | term_redraw(terminal); |
| 357 | } |
| 358 | |
| 359 | void term_page_down(terminal_t* terminal) |
| 360 | { |
| 361 | tsm_screen_sb_page_down(terminal->term->screen, 1); |
| 362 | term_redraw(terminal); |
| 363 | } |
| 364 | |
| 365 | void term_line_up(terminal_t* terminal) |
| 366 | { |
| 367 | tsm_screen_sb_up(terminal->term->screen, 1); |
| 368 | term_redraw(terminal); |
| 369 | } |
| 370 | |
| 371 | void term_line_down(terminal_t* terminal) |
| 372 | { |
| 373 | tsm_screen_sb_down(terminal->term->screen, 1); |
| 374 | term_redraw(terminal); |
| 375 | } |
| 376 | |
| 377 | bool term_is_valid(terminal_t* terminal) |
| 378 | { |
| 379 | return ((terminal != NULL) && (terminal->term != NULL)); |
| 380 | } |
| 381 | |
| 382 | int term_fd(terminal_t* terminal) |
| 383 | { |
| 384 | if (term_is_valid(terminal)) |
| 385 | return terminal->term->pty_bridge; |
| 386 | else |
| 387 | return -1; |
| 388 | } |
| 389 | |
| 390 | void term_dispatch_io(terminal_t* terminal, fd_set* read_set) |
| 391 | { |
| 392 | if (term_is_valid(terminal)) |
| 393 | if (FD_ISSET(terminal->term->pty_bridge, read_set)) |
| 394 | shl_pty_bridge_dispatch(terminal->term->pty_bridge, 0); |
| 395 | } |
| 396 | |
| 397 | bool term_exception(terminal_t* terminal, fd_set* exception_set) |
| 398 | { |
| 399 | if (term_is_valid(terminal)) { |
| 400 | if (terminal->term->pty_bridge >= 0) { |
| 401 | return FD_ISSET(terminal->term->pty_bridge, |
| 402 | exception_set); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | bool term_is_active(terminal_t* terminal) |
| 410 | { |
| 411 | if (term_is_valid(terminal)) |
| 412 | return terminal->active; |
| 413 | |
| 414 | return false; |
| 415 | } |
| 416 | |
| 417 | void term_add_fd(terminal_t* terminal, fd_set* read_set, fd_set* exception_set) |
| 418 | { |
| 419 | if (term_is_valid(terminal)) { |
| 420 | if (terminal->term->pty_bridge >= 0) { |
| 421 | FD_SET(terminal->term->pty_bridge, read_set); |
| 422 | FD_SET(terminal->term->pty_bridge, exception_set); |
| 423 | } |
| 424 | } |
| 425 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 426 | |
| 427 | const char* term_get_ptsname(terminal_t* terminal) |
| 428 | { |
| 429 | return ptsname(shl_pty_get_fd(terminal->term->pty)); |
| 430 | } |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 431 | |
| 432 | void term_set_background(terminal_t* terminal, uint32_t bg) |
| 433 | { |
| 434 | terminal->background = bg; |
| 435 | terminal->background_valid = true; |
| 436 | } |
| 437 | |
| 438 | int term_show_image(terminal_t* terminal, image_t* image) |
| 439 | { |
| 440 | return image_show(image, terminal->video); |
| 441 | } |
| 442 | |
| 443 | void term_write_message(terminal_t* terminal, char* message) |
| 444 | { |
| 445 | FILE *fp; |
| 446 | |
| 447 | fp = fopen(term_get_ptsname(terminal), "w"); |
| 448 | if (fp) { |
| 449 | fputs(message, fp); |
| 450 | fclose(fp); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | void term_hide_cursor(terminal_t* terminal) |
| 455 | { |
| 456 | term_write_message(terminal, "\033[?25l"); |
| 457 | } |
| 458 | |
| 459 | void term_show_cursor(terminal_t* terminal) |
| 460 | { |
| 461 | term_write_message(terminal, "\033[?25h"); |
| 462 | } |