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" |
Dominik Behr | 580462b | 2014-11-20 13:50:05 -0800 | [diff] [blame] | 17 | #include "main.h" |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 18 | #include "shl_pty.h" |
| 19 | #include "term.h" |
| 20 | #include "util.h" |
| 21 | #include "video.h" |
| 22 | |
| 23 | struct term { |
| 24 | struct tsm_screen *screen; |
| 25 | struct tsm_vte *vte; |
| 26 | struct shl_pty *pty; |
| 27 | int pty_bridge; |
| 28 | int pid; |
| 29 | tsm_age_t age; |
| 30 | int char_x, char_y; |
| 31 | int pitch; |
| 32 | uint32_t *dst_image; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
Dominik Behr | 580462b | 2014-11-20 13:50:05 -0800 | [diff] [blame] | 35 | static void __attribute__ ((noreturn)) term_run_child(unsigned int term_id) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 36 | { |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 37 | printf("Welcome to frecon!\n"); |
Dominik Behr | 580462b | 2014-11-20 13:50:05 -0800 | [diff] [blame] | 38 | printf("running %s\n", command_flags.exec[term_id][0]); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 39 | /* XXX figure out how to fix "top" for xterm-256color */ |
| 40 | setenv("TERM", "xterm", 1); |
Dominik Behr | 580462b | 2014-11-20 13:50:05 -0800 | [diff] [blame] | 41 | execve(command_flags.exec[term_id][0], command_flags.exec[term_id], environ); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 42 | exit(1); |
| 43 | } |
| 44 | |
| 45 | static int term_draw_cell(struct tsm_screen *screen, uint32_t id, |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 46 | const uint32_t *ch, size_t len, |
| 47 | unsigned int cwidth, unsigned int posx, |
| 48 | unsigned int posy, |
| 49 | const struct tsm_screen_attr *attr, |
| 50 | tsm_age_t age, void *data) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 51 | { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 52 | terminal_t *terminal = (terminal_t*)data; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 53 | uint32_t front_color, back_color; |
| 54 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 55 | if (age && terminal->term->age && age <= terminal->term->age) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 56 | return 0; |
| 57 | |
| 58 | front_color = (attr->fr << 16) | (attr->fg << 8) | attr->fb; |
| 59 | back_color = (attr->br << 16) | (attr->bg << 8) | attr->bb; |
| 60 | |
| 61 | if (attr->inverse) { |
| 62 | uint32_t tmp = front_color; |
| 63 | front_color = back_color; |
| 64 | back_color = tmp; |
| 65 | } |
| 66 | |
| 67 | if (len) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 68 | font_render(terminal->term->dst_image, posx, posy, terminal->term->pitch, *ch, |
| 69 | front_color, back_color); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 70 | else |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 71 | font_fillchar(terminal->term->dst_image, posx, posy, terminal->term->pitch, |
| 72 | front_color, back_color); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 77 | void term_redraw(terminal_t *terminal) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 78 | { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 79 | uint32_t *video_buffer; |
| 80 | video_buffer = video_lock(terminal->video); |
| 81 | if (video_buffer != NULL) { |
| 82 | terminal->term->dst_image = video_buffer; |
| 83 | terminal->term->age = |
| 84 | tsm_screen_draw(terminal->term->screen, term_draw_cell, terminal); |
| 85 | video_unlock(terminal->video); |
| 86 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 87 | } |
| 88 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 89 | 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] | 90 | { |
| 91 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 92 | if (tsm_vte_handle_keyboard(terminal->term->vte, keysym, 0, 0, unicode)) |
| 93 | tsm_screen_sb_reset(terminal->term->screen); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 94 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 95 | term_redraw(terminal); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static void term_read_cb(struct shl_pty *pty, char *u8, size_t len, void *data) |
| 99 | { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 100 | terminal_t *terminal = (terminal_t*)data; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 101 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 102 | tsm_vte_input(terminal->term->vte, u8, len); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 103 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 104 | term_redraw(terminal); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | 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] | 108 | void *data) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 109 | { |
| 110 | struct term *term = data; |
| 111 | int r; |
| 112 | |
| 113 | r = shl_pty_write(term->pty, u8, len); |
| 114 | if (r < 0) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 115 | LOG(ERROR, "OOM in pty-write (%d)", r); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 116 | |
| 117 | shl_pty_dispatch(term->pty); |
| 118 | } |
| 119 | |
| 120 | static const char *sev2str_table[] = { |
| 121 | "FATAL", |
| 122 | "ALERT", |
| 123 | "CRITICAL", |
| 124 | "ERROR", |
| 125 | "WARNING", |
| 126 | "NOTICE", |
| 127 | "INFO", |
| 128 | "DEBUG" |
| 129 | }; |
| 130 | |
| 131 | static const char *sev2str(unsigned int sev) |
| 132 | { |
| 133 | if (sev > 7) |
| 134 | return "DEBUG"; |
| 135 | |
| 136 | return sev2str_table[sev]; |
| 137 | } |
| 138 | |
Dominik Behr | 0000350 | 2014-08-15 16:42:37 -0700 | [diff] [blame] | 139 | #ifdef __clang__ |
| 140 | __attribute__((__format__ (__printf__, 7, 0))) |
| 141 | #endif |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 142 | 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] | 143 | const char *subs, unsigned int sev, const char *format, |
| 144 | va_list args) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 145 | { |
| 146 | fprintf(stderr, "%s: %s: ", sev2str(sev), subs); |
| 147 | vfprintf(stderr, format, args); |
| 148 | fprintf(stderr, "\n"); |
| 149 | } |
| 150 | |
Dominik Behr | 580462b | 2014-11-20 13:50:05 -0800 | [diff] [blame] | 151 | terminal_t* term_init(unsigned int term_id) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 152 | { |
| 153 | const int scrollback_size = 200; |
| 154 | uint32_t char_width, char_height; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 155 | int status; |
| 156 | terminal_t *new_terminal; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 157 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 158 | new_terminal = (terminal_t*)calloc(1, sizeof(*new_terminal)); |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 159 | if (!new_terminal) |
| 160 | return NULL; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 161 | |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 162 | new_terminal->video = video_init(); |
| 163 | if (!new_terminal->video) { |
| 164 | term_close(new_terminal); |
| 165 | return NULL; |
| 166 | } |
| 167 | |
| 168 | new_terminal->term = (struct term*)calloc(1, sizeof(*new_terminal->term)); |
| 169 | if (!new_terminal->term) { |
| 170 | term_close(new_terminal); |
| 171 | return NULL; |
| 172 | } |
| 173 | |
| 174 | font_init(video_getscaling(new_terminal->video)); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 175 | font_get_size(&char_width, &char_height); |
| 176 | |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 177 | new_terminal->term->char_x = |
| 178 | video_getwidth(new_terminal->video) / char_width; |
| 179 | new_terminal->term->char_y = |
| 180 | video_getheight(new_terminal->video) / char_height; |
| 181 | new_terminal->term->pitch = video_getpitch(new_terminal->video); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 182 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 183 | status = tsm_screen_new(&new_terminal->term->screen, |
| 184 | log_tsm, new_terminal->term); |
| 185 | if (new_terminal < 0) { |
| 186 | term_close(new_terminal); |
| 187 | return NULL; |
| 188 | } |
| 189 | |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 190 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 191 | tsm_screen_set_max_sb(new_terminal->term->screen, scrollback_size); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 192 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 193 | status = tsm_vte_new(&new_terminal->term->vte, new_terminal->term->screen, |
| 194 | term_write_cb, new_terminal->term, log_tsm, new_terminal->term); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 195 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 196 | if (status < 0) { |
| 197 | term_close(new_terminal); |
| 198 | return NULL; |
| 199 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 200 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 201 | new_terminal->term->pty_bridge = shl_pty_bridge_new(); |
| 202 | if (new_terminal->term->pty_bridge < 0) { |
| 203 | term_close(new_terminal); |
| 204 | return NULL; |
| 205 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 206 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 207 | status = shl_pty_open(&new_terminal->term->pty, |
| 208 | term_read_cb, new_terminal, new_terminal->term->char_x, |
| 209 | new_terminal->term->char_y); |
| 210 | if (status < 0) { |
| 211 | term_close(new_terminal); |
| 212 | return NULL; |
| 213 | } else if (status == 0) { |
Dominik Behr | 580462b | 2014-11-20 13:50:05 -0800 | [diff] [blame] | 214 | term_run_child(term_id); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 215 | exit(1); |
| 216 | } |
| 217 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 218 | status = shl_pty_bridge_add(new_terminal->term->pty_bridge, new_terminal->term->pty); |
| 219 | if (status) { |
| 220 | shl_pty_close(new_terminal->term->pty); |
| 221 | term_close(new_terminal); |
| 222 | return NULL; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 223 | } |
| 224 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 225 | 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] | 226 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 227 | status = tsm_screen_resize(new_terminal->term->screen, |
| 228 | new_terminal->term->char_x, new_terminal->term->char_y); |
| 229 | if (status < 0) { |
| 230 | shl_pty_close(new_terminal->term->pty); |
| 231 | term_close(new_terminal); |
| 232 | return NULL; |
| 233 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 234 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 235 | status = shl_pty_resize(new_terminal->term->pty, new_terminal->term->char_x, new_terminal->term->char_y); |
| 236 | if (status < 0) { |
| 237 | shl_pty_close(new_terminal->term->pty); |
| 238 | term_close(new_terminal); |
| 239 | return NULL; |
| 240 | } |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 241 | new_terminal->active = true; |
| 242 | video_setmode(new_terminal->video); |
| 243 | term_redraw(new_terminal); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 244 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 245 | return new_terminal; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 246 | } |
| 247 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 248 | void term_set_dbus(terminal_t *term, dbus_t* dbus) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 249 | { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 250 | term->dbus = dbus; |
| 251 | } |
| 252 | |
| 253 | void term_close(terminal_t *term) |
| 254 | { |
| 255 | if (!term) |
| 256 | return; |
| 257 | |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 258 | if (term->video) { |
| 259 | video_close(term->video); |
| 260 | term->video = NULL; |
| 261 | } |
| 262 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 263 | if (term->term) { |
| 264 | free(term->term); |
| 265 | term->term = NULL; |
| 266 | } |
| 267 | |
| 268 | free(term); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 269 | } |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 270 | |
| 271 | bool term_is_child_done(terminal_t* terminal) |
| 272 | { |
| 273 | int status; |
| 274 | int ret; |
| 275 | ret = waitpid(terminal->term->pid, &status, WNOHANG); |
| 276 | |
| 277 | return ret != 0; |
| 278 | } |
| 279 | |
| 280 | void term_page_up(terminal_t* terminal) |
| 281 | { |
| 282 | tsm_screen_sb_page_up(terminal->term->screen, 1); |
| 283 | term_redraw(terminal); |
| 284 | } |
| 285 | |
| 286 | void term_page_down(terminal_t* terminal) |
| 287 | { |
| 288 | tsm_screen_sb_page_down(terminal->term->screen, 1); |
| 289 | term_redraw(terminal); |
| 290 | } |
| 291 | |
| 292 | void term_line_up(terminal_t* terminal) |
| 293 | { |
| 294 | tsm_screen_sb_up(terminal->term->screen, 1); |
| 295 | term_redraw(terminal); |
| 296 | } |
| 297 | |
| 298 | void term_line_down(terminal_t* terminal) |
| 299 | { |
| 300 | tsm_screen_sb_down(terminal->term->screen, 1); |
| 301 | term_redraw(terminal); |
| 302 | } |
| 303 | |
| 304 | bool term_is_valid(terminal_t* terminal) |
| 305 | { |
| 306 | return ((terminal != NULL) && (terminal->term != NULL)); |
| 307 | } |
| 308 | |
| 309 | int term_fd(terminal_t* terminal) |
| 310 | { |
| 311 | if (term_is_valid(terminal)) |
| 312 | return terminal->term->pty_bridge; |
| 313 | else |
| 314 | return -1; |
| 315 | } |
| 316 | |
| 317 | void term_dispatch_io(terminal_t* terminal, fd_set* read_set) |
| 318 | { |
| 319 | if (term_is_valid(terminal)) |
| 320 | if (FD_ISSET(terminal->term->pty_bridge, read_set)) |
| 321 | shl_pty_bridge_dispatch(terminal->term->pty_bridge, 0); |
| 322 | } |
| 323 | |
| 324 | bool term_exception(terminal_t* terminal, fd_set* exception_set) |
| 325 | { |
| 326 | if (term_is_valid(terminal)) { |
| 327 | if (terminal->term->pty_bridge >= 0) { |
| 328 | return FD_ISSET(terminal->term->pty_bridge, |
| 329 | exception_set); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | bool term_is_active(terminal_t* terminal) |
| 337 | { |
| 338 | if (term_is_valid(terminal)) |
| 339 | return terminal->active; |
| 340 | |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | void term_add_fd(terminal_t* terminal, fd_set* read_set, fd_set* exception_set) |
| 345 | { |
| 346 | if (term_is_valid(terminal)) { |
| 347 | if (terminal->term->pty_bridge >= 0) { |
| 348 | FD_SET(terminal->term->pty_bridge, read_set); |
| 349 | FD_SET(terminal->term->pty_bridge, exception_set); |
| 350 | } |
| 351 | } |
| 352 | } |