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