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> |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 8 | #include <fcntl.h> |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 9 | #include <libtsm.h> |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 10 | #include <stdio.h> |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 11 | #include <stdlib.h> |
Dominik Behr | 9389945 | 2014-08-18 22:16:21 -0700 | [diff] [blame] | 12 | #include <sys/select.h> |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 13 | #include <sys/stat.h> |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 14 | #include <sys/types.h> |
| 15 | #include <sys/wait.h> |
Dominik Behr | 32680e3 | 2016-08-16 12:18:41 -0700 | [diff] [blame] | 16 | #include <unistd.h> |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 17 | |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 18 | #include "dbus.h" |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 19 | #include "fb.h" |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 20 | #include "font.h" |
Dominik Behr | d253090 | 2016-05-05 14:01:06 -0700 | [diff] [blame] | 21 | #include "image.h" |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 22 | #include "input.h" |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 23 | #include "main.h" |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 24 | #include "shl_pty.h" |
| 25 | #include "term.h" |
| 26 | #include "util.h" |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 27 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 28 | unsigned int term_num_terminals = 4; |
| 29 | static terminal_t* terminals[TERM_MAX_TERMINALS]; |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 30 | static uint32_t current_terminal = 0; |
Stéphane Marchesin | 08c08e7 | 2016-01-07 16:44:08 -0800 | [diff] [blame] | 31 | |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 32 | struct term { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 33 | struct tsm_screen* screen; |
| 34 | struct tsm_vte* vte; |
| 35 | struct shl_pty* pty; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 36 | int pty_bridge; |
| 37 | int pid; |
| 38 | tsm_age_t age; |
| 39 | int char_x, char_y; |
| 40 | int pitch; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 41 | uint32_t* dst_image; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 44 | struct _terminal_t { |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 45 | unsigned vt; |
| 46 | bool active; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 47 | uint32_t background; |
| 48 | bool background_valid; |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 49 | fb_t* fb; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 50 | struct term* term; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 51 | char** exec; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 54 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 55 | static char* interactive_cmd_line[] = { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 56 | "/sbin/agetty", |
| 57 | "-", |
| 58 | "9600", |
| 59 | "xterm", |
| 60 | NULL |
| 61 | }; |
| 62 | |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 63 | static bool in_background = false; |
| 64 | static bool hotplug_occured = false; |
| 65 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 66 | |
| 67 | static void __attribute__ ((noreturn)) term_run_child(terminal_t* terminal) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 68 | { |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 69 | /* XXX figure out how to fix "top" for xterm-256color */ |
| 70 | setenv("TERM", "xterm", 1); |
Dominik Behr | 32680e3 | 2016-08-16 12:18:41 -0700 | [diff] [blame] | 71 | if (terminal->exec) { |
| 72 | execve(terminal->exec[0], terminal->exec, environ); |
| 73 | exit(1); |
| 74 | } else { |
| 75 | while (1) |
| 76 | sleep(1000000); |
| 77 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 80 | 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] | 81 | const uint32_t* ch, size_t len, |
| 82 | unsigned int cwidth, unsigned int posx, |
| 83 | unsigned int posy, |
| 84 | const struct tsm_screen_attr* attr, |
| 85 | tsm_age_t age, void* data) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 86 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 87 | terminal_t* terminal = (terminal_t*)data; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 88 | uint32_t front_color, back_color; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 89 | uint8_t br, bb, bg; |
Stéphane Marchesin | edece33 | 2015-12-14 15:10:58 -0800 | [diff] [blame] | 90 | uint32_t luminance; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 91 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 92 | if (age && terminal->term->age && age <= terminal->term->age) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 93 | return 0; |
| 94 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 95 | if (terminal->background_valid) { |
| 96 | br = (terminal->background >> 16) & 0xFF; |
| 97 | bg = (terminal->background >> 8) & 0xFF; |
| 98 | bb = (terminal->background) & 0xFF; |
Stéphane Marchesin | edece33 | 2015-12-14 15:10:58 -0800 | [diff] [blame] | 99 | luminance = (3 * br + bb + 4 * bg) >> 3; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 100 | |
Stéphane Marchesin | edece33 | 2015-12-14 15:10:58 -0800 | [diff] [blame] | 101 | /* |
| 102 | * FIXME: black is chosen on a dark background, but it uses the |
| 103 | * default color for light backgrounds |
| 104 | */ |
| 105 | if (luminance > 128) { |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 106 | front_color = 0; |
| 107 | back_color = terminal->background; |
| 108 | } else { |
| 109 | front_color = (attr->fr << 16) | (attr->fg << 8) | attr->fb; |
| 110 | back_color = terminal->background; |
| 111 | } |
| 112 | } else { |
| 113 | front_color = (attr->fr << 16) | (attr->fg << 8) | attr->fb; |
| 114 | back_color = (attr->br << 16) | (attr->bg << 8) | attr->bb; |
| 115 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 116 | |
| 117 | if (attr->inverse) { |
| 118 | uint32_t tmp = front_color; |
| 119 | front_color = back_color; |
| 120 | back_color = tmp; |
| 121 | } |
| 122 | |
| 123 | if (len) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 124 | font_render(terminal->term->dst_image, posx, posy, terminal->term->pitch, *ch, |
| 125 | front_color, back_color); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 126 | else |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 127 | font_fillchar(terminal->term->dst_image, posx, posy, terminal->term->pitch, |
| 128 | front_color, back_color); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Stéphane Marchesin | e2a46cd | 2016-01-07 16:45:24 -0800 | [diff] [blame] | 133 | static void term_redraw(terminal_t* terminal) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 134 | { |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 135 | uint32_t* fb_buffer; |
| 136 | fb_buffer = fb_lock(terminal->fb); |
| 137 | if (fb_buffer != NULL) { |
| 138 | terminal->term->dst_image = fb_buffer; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 139 | terminal->term->age = |
| 140 | tsm_screen_draw(terminal->term->screen, term_draw_cell, terminal); |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 141 | fb_unlock(terminal->fb); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 142 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 143 | } |
| 144 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 145 | 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] | 146 | { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 147 | if (tsm_vte_handle_keyboard(terminal->term->vte, keysym, 0, 0, unicode)) |
| 148 | tsm_screen_sb_reset(terminal->term->screen); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 149 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 150 | term_redraw(terminal); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 153 | 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] | 154 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 155 | terminal_t* terminal = (terminal_t*)data; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 156 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 157 | tsm_vte_input(terminal->term->vte, u8, len); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 158 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 159 | term_redraw(terminal); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 162 | static void term_write_cb(struct tsm_vte* vte, const char* u8, size_t len, |
| 163 | void* data) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 164 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 165 | struct term* term = data; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 166 | int r; |
| 167 | |
| 168 | r = shl_pty_write(term->pty, u8, len); |
| 169 | if (r < 0) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 170 | LOG(ERROR, "OOM in pty-write (%d)", r); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 171 | |
| 172 | shl_pty_dispatch(term->pty); |
| 173 | } |
| 174 | |
Dominik Behr | d253090 | 2016-05-05 14:01:06 -0700 | [diff] [blame] | 175 | static void term_esc_show_image(terminal_t* terminal, char* params) |
| 176 | { |
| 177 | char* tok; |
| 178 | image_t* image; |
| 179 | int status; |
| 180 | |
| 181 | image = image_create(); |
| 182 | if (!image) { |
| 183 | LOG(ERROR, "Out of memory when creating an image.\n"); |
| 184 | return; |
| 185 | } |
| 186 | for (tok = strtok(params, ";"); tok; tok = strtok(NULL, ";")) { |
| 187 | if (strncmp("file=", tok, 5) == 0) { |
| 188 | image_set_filename(image, tok + 5); |
| 189 | } else if (strncmp("location=", tok, 9) == 0) { |
| 190 | uint32_t x, y; |
| 191 | if (sscanf(tok + 9, "%u,%u", &x, &y) != 2) { |
| 192 | LOG(ERROR, "Error parsing image location.\n"); |
| 193 | goto done; |
| 194 | } |
| 195 | image_set_location(image, x, y); |
| 196 | } else if (strncmp("offset=", tok, 7) == 0) { |
| 197 | int32_t x, y; |
| 198 | if (sscanf(tok + 7, "%d,%d", &x, &y) != 2) { |
| 199 | LOG(ERROR, "Error parsing image offset.\n"); |
| 200 | goto done; |
| 201 | } |
| 202 | image_set_offset(image, x, y); |
| 203 | } else if (strncmp("scale=", tok, 6) == 0) { |
| 204 | uint32_t s; |
| 205 | if (sscanf(tok + 6, "%u", &s) != 1) { |
| 206 | LOG(ERROR, "Error parsing image scale.\n"); |
| 207 | goto done; |
| 208 | } |
| 209 | if (s == 0) |
| 210 | s = image_get_auto_scale(term_getfb(terminal)); |
| 211 | image_set_scale(image, s); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | status = image_load_image_from_file(image); |
| 216 | if (status != 0) { |
| 217 | LOG(WARNING, "Term ESC image_load_image_from_file %s failed: %d:%s.", |
| 218 | image_get_filename(image), status, strerror(status)); |
| 219 | } else { |
| 220 | term_show_image(terminal, image); |
| 221 | } |
| 222 | done: |
| 223 | image_destroy(image); |
| 224 | } |
| 225 | |
| 226 | static void term_esc_draw_box(terminal_t* terminal, char* params) |
| 227 | { |
| 228 | char* tok; |
| 229 | uint32_t color = 0; |
| 230 | uint32_t w = 1; |
| 231 | uint32_t h = 1; |
| 232 | uint32_t locx, locy; |
| 233 | bool use_location = false; |
| 234 | int32_t offx, offy; |
| 235 | bool use_offset = false; |
| 236 | uint32_t scale = 1; |
| 237 | uint32_t* buffer; |
| 238 | int32_t startx, starty; |
| 239 | uint32_t pitch4; |
| 240 | |
| 241 | for (tok = strtok(params, ";"); tok; tok = strtok(NULL, ";")) { |
| 242 | if (strncmp("color=", tok, 6) == 0) { |
| 243 | color = strtoul(tok + 6, NULL, 0); |
| 244 | } else if (strncmp("size=", tok, 5) == 0) { |
| 245 | if (sscanf(tok + 5, "%u,%u", &w, &h) != 2) { |
| 246 | LOG(ERROR, "Error parsing box size.\n"); |
| 247 | goto done; |
| 248 | } |
| 249 | } else if (strncmp("location=", tok, 9) == 0) { |
| 250 | if (sscanf(tok + 9, "%u,%u", &locx, &locy) != 2) { |
| 251 | LOG(ERROR, "Error parsing box location.\n"); |
| 252 | goto done; |
| 253 | } |
| 254 | use_location = true; |
| 255 | } else if (strncmp("offset=", tok, 7) == 0) { |
| 256 | if (sscanf(tok + 7, "%d,%d", &offx, &offy) != 2) { |
| 257 | LOG(ERROR, "Error parsing box offset.\n"); |
| 258 | goto done; |
| 259 | } |
| 260 | use_offset = true; |
| 261 | } else if (strncmp("scale=", tok, 6) == 0) { |
| 262 | if (sscanf(tok + 6, "%u", &scale) != 1) { |
| 263 | LOG(ERROR, "Error parsing box scale.\n"); |
| 264 | goto done; |
| 265 | } |
| 266 | if (scale == 0) |
| 267 | scale = image_get_auto_scale(term_getfb(terminal)); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | w *= scale; |
| 272 | h *= scale; |
| 273 | offx *= scale; |
| 274 | offy *= scale; |
| 275 | |
| 276 | buffer = fb_lock(terminal->fb); |
| 277 | if (buffer == NULL) |
| 278 | goto done; |
| 279 | |
| 280 | if (use_offset && use_location) { |
| 281 | LOG(WARNING, "Box offset and location set, using location."); |
| 282 | use_offset = false; |
| 283 | } |
| 284 | |
| 285 | if (use_location) { |
| 286 | startx = locx; |
| 287 | starty = locy; |
| 288 | } else { |
| 289 | startx = (fb_getwidth(terminal->fb) - (int32_t)w)/2; |
| 290 | starty = (fb_getheight(terminal->fb) - (int32_t)h)/2; |
| 291 | } |
| 292 | |
| 293 | if (use_offset) { |
| 294 | startx += offx; |
| 295 | starty += offy; |
| 296 | } |
| 297 | |
| 298 | pitch4 = fb_getpitch(terminal->fb) / 4; |
| 299 | |
| 300 | /* Completely outside buffer, do nothing */ |
| 301 | if (startx + w <= 0 || startx >= fb_getwidth(terminal->fb)) |
| 302 | goto done_fb; |
| 303 | if (starty + h <= 0 || starty >= fb_getheight(terminal->fb)) |
| 304 | goto done_fb; |
| 305 | /* Make sure we are inside buffer. */ |
| 306 | if (startx < 0) |
| 307 | startx = 0; |
| 308 | if (startx + (int32_t)w > fb_getwidth(terminal->fb)) |
| 309 | w = fb_getwidth(terminal->fb) - startx; |
| 310 | if (starty < 0) |
| 311 | starty = 0; |
| 312 | if (starty + (int32_t)h > fb_getheight(terminal->fb)) |
| 313 | h = fb_getheight(terminal->fb) - starty; |
| 314 | |
| 315 | for (uint32_t y = 0; y < h; y++) { |
| 316 | uint32_t *o = buffer + (starty + y) * pitch4 |
| 317 | + startx; |
| 318 | for (uint32_t x = 0; x < w; x++) |
| 319 | o[x] = color; |
| 320 | } |
| 321 | done_fb: |
| 322 | fb_unlock(terminal->fb); |
| 323 | done: |
| 324 | ; |
| 325 | } |
| 326 | |
| 327 | static void term_osc_cb(struct tsm_vte *vte, const uint32_t *osc_string, |
| 328 | size_t osc_len, void *data) |
| 329 | { |
| 330 | terminal_t* terminal = (terminal_t*)data; |
| 331 | size_t i; |
| 332 | char *osc; |
| 333 | |
| 334 | for (i = 0; i < osc_len; i++) |
| 335 | if (osc_string[i] >= 128) |
| 336 | return; /* we only want to deal with ASCII */ |
| 337 | |
| 338 | osc = malloc(osc_len + 1); |
| 339 | if (!osc) { |
| 340 | LOG(WARNING, "Out of memory when processing OSC\n"); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | for (i = 0; i < osc_len; i++) |
| 345 | osc[i] = (char)osc_string[i]; |
| 346 | osc[i] = '\0'; |
| 347 | |
| 348 | if (strncmp(osc, "image:", 6) == 0) |
| 349 | term_esc_show_image(terminal, osc + 6); |
| 350 | else if (strncmp(osc, "box:", 4) == 0) |
| 351 | term_esc_draw_box(terminal, osc + 4); |
| 352 | else |
| 353 | LOG(WARNING, "Unknown OSC escape sequence \"%s\", ignoring.", osc); |
| 354 | |
| 355 | free(osc); |
| 356 | } |
| 357 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 358 | static const char* sev2str_table[] = { |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 359 | "FATAL", |
| 360 | "ALERT", |
| 361 | "CRITICAL", |
| 362 | "ERROR", |
| 363 | "WARNING", |
| 364 | "NOTICE", |
| 365 | "INFO", |
| 366 | "DEBUG" |
| 367 | }; |
| 368 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 369 | static const char* sev2str(unsigned int sev) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 370 | { |
| 371 | if (sev > 7) |
| 372 | return "DEBUG"; |
| 373 | |
| 374 | return sev2str_table[sev]; |
| 375 | } |
| 376 | |
Dominik Behr | 0000350 | 2014-08-15 16:42:37 -0700 | [diff] [blame] | 377 | #ifdef __clang__ |
| 378 | __attribute__((__format__ (__printf__, 7, 0))) |
| 379 | #endif |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 380 | 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] | 381 | const char* subs, unsigned int sev, const char* format, |
| 382 | va_list args) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 383 | { |
| 384 | fprintf(stderr, "%s: %s: ", sev2str(sev), subs); |
| 385 | vfprintf(stderr, format, args); |
| 386 | fprintf(stderr, "\n"); |
| 387 | } |
| 388 | |
Stéphane Marchesin | 7831066 | 2016-01-06 16:09:39 -0800 | [diff] [blame] | 389 | static int term_resize(terminal_t* term) |
| 390 | { |
| 391 | uint32_t char_width, char_height; |
| 392 | int status; |
| 393 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 394 | font_init(fb_getscaling(term->fb)); |
Stéphane Marchesin | 7831066 | 2016-01-06 16:09:39 -0800 | [diff] [blame] | 395 | font_get_size(&char_width, &char_height); |
| 396 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 397 | term->term->char_x = fb_getwidth(term->fb) / char_width; |
| 398 | term->term->char_y = fb_getheight(term->fb) / char_height; |
| 399 | term->term->pitch = fb_getpitch(term->fb); |
Stéphane Marchesin | 7831066 | 2016-01-06 16:09:39 -0800 | [diff] [blame] | 400 | |
| 401 | status = tsm_screen_resize(term->term->screen, |
| 402 | term->term->char_x, term->term->char_y); |
| 403 | if (status < 0) { |
| 404 | font_free(); |
| 405 | return -1; |
| 406 | } |
| 407 | |
| 408 | status = shl_pty_resize(term->term->pty, term->term->char_x, |
| 409 | term->term->char_y); |
| 410 | if (status < 0) { |
| 411 | font_free(); |
| 412 | return -1; |
| 413 | } |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 418 | void term_set_num_terminals(unsigned new_num) |
| 419 | { |
| 420 | if (new_num < 1) |
| 421 | term_num_terminals = 1; |
| 422 | else if (new_num > TERM_MAX_TERMINALS) |
| 423 | term_num_terminals = TERM_MAX_TERMINALS; |
| 424 | else |
| 425 | term_num_terminals = new_num; |
| 426 | } |
| 427 | |
| 428 | static bool term_is_interactive(unsigned int vt) |
| 429 | { |
| 430 | if (command_flags.no_login) |
| 431 | return false; |
| 432 | |
| 433 | if (vt == TERM_SPLASH_TERMINAL) |
| 434 | return command_flags.enable_vt1; |
| 435 | |
| 436 | return true; |
| 437 | } |
| 438 | |
| 439 | terminal_t* term_init(unsigned vt, int pts_fd) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 440 | { |
| 441 | const int scrollback_size = 200; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 442 | int status; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 443 | terminal_t* new_terminal; |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 444 | bool interactive = term_is_interactive(vt); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 445 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 446 | new_terminal = (terminal_t*)calloc(1, sizeof(*new_terminal)); |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 447 | if (!new_terminal) |
| 448 | return NULL; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 449 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 450 | new_terminal->vt = vt; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 451 | new_terminal->background_valid = false; |
| 452 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 453 | new_terminal->fb = fb_init(); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 454 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 455 | if (!new_terminal->fb) { |
Dominik Behr | f932991 | 2016-08-25 17:31:52 -0700 | [diff] [blame] | 456 | LOG(ERROR, "Failed to create fb on VT%u.", vt); |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 457 | term_close(new_terminal); |
| 458 | return NULL; |
| 459 | } |
| 460 | |
| 461 | new_terminal->term = (struct term*)calloc(1, sizeof(*new_terminal->term)); |
| 462 | if (!new_terminal->term) { |
| 463 | term_close(new_terminal); |
| 464 | return NULL; |
| 465 | } |
| 466 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 467 | if (interactive) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 468 | new_terminal->exec = interactive_cmd_line; |
| 469 | else |
Dominik Behr | 32680e3 | 2016-08-16 12:18:41 -0700 | [diff] [blame] | 470 | new_terminal->exec = NULL; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 471 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 472 | status = tsm_screen_new(&new_terminal->term->screen, |
| 473 | log_tsm, new_terminal->term); |
zhuo-hao | 471f1e2 | 2015-08-12 14:55:56 +0800 | [diff] [blame] | 474 | if (status < 0) { |
Dominik Behr | f932991 | 2016-08-25 17:31:52 -0700 | [diff] [blame] | 475 | LOG(ERROR, "Failed to create new screen on VT%u.", vt); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 476 | term_close(new_terminal); |
| 477 | return NULL; |
| 478 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 479 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 480 | tsm_screen_set_max_sb(new_terminal->term->screen, scrollback_size); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 481 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 482 | status = tsm_vte_new(&new_terminal->term->vte, new_terminal->term->screen, |
| 483 | term_write_cb, new_terminal->term, log_tsm, new_terminal->term); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 484 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 485 | if (status < 0) { |
Dominik Behr | f932991 | 2016-08-25 17:31:52 -0700 | [diff] [blame] | 486 | LOG(ERROR, "Failed to create new VT%u.", vt); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 487 | term_close(new_terminal); |
| 488 | return NULL; |
| 489 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 490 | |
Dominik Behr | d253090 | 2016-05-05 14:01:06 -0700 | [diff] [blame] | 491 | if (command_flags.enable_gfx) |
| 492 | tsm_vte_set_osc_cb(new_terminal->term->vte, term_osc_cb, (void *)new_terminal); |
| 493 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 494 | new_terminal->term->pty_bridge = shl_pty_bridge_new(); |
| 495 | if (new_terminal->term->pty_bridge < 0) { |
Dominik Behr | f932991 | 2016-08-25 17:31:52 -0700 | [diff] [blame] | 496 | LOG(ERROR, "Failed to create pty bridge on VT%u.", vt); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 497 | term_close(new_terminal); |
| 498 | return NULL; |
| 499 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 500 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 501 | status = shl_pty_open(&new_terminal->term->pty, |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 502 | term_read_cb, new_terminal, 1, 1, pts_fd); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 503 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 504 | if (status < 0) { |
Dominik Behr | f932991 | 2016-08-25 17:31:52 -0700 | [diff] [blame] | 505 | LOG(ERROR, "Failed to open pty on VT%u.", vt); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 506 | term_close(new_terminal); |
| 507 | return NULL; |
| 508 | } else if (status == 0) { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 509 | term_run_child(new_terminal); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 510 | exit(1); |
| 511 | } |
| 512 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 513 | status = mkdir(FRECON_RUN_DIR, S_IRWXU); |
| 514 | if (status == 0 || (status < 0 && errno == EEXIST)) { |
| 515 | char path[32]; |
| 516 | snprintf(path, sizeof(path), FRECON_VT_PATH, vt); |
Dominik Behr | b23b05b | 2016-08-15 16:29:02 -0700 | [diff] [blame] | 517 | unlink(path); /* In case it already exists. Ignore return codes. */ |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 518 | if (symlink(ptsname(shl_pty_get_fd(new_terminal->term->pty)), path) < 0) |
| 519 | LOG(ERROR, "Failed to symlink pts name %s to %s, %d:%s", |
| 520 | path, |
| 521 | ptsname(shl_pty_get_fd(new_terminal->term->pty)), |
| 522 | errno, strerror(errno)); |
| 523 | } |
| 524 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 525 | status = shl_pty_bridge_add(new_terminal->term->pty_bridge, new_terminal->term->pty); |
| 526 | if (status) { |
Dominik Behr | f932991 | 2016-08-25 17:31:52 -0700 | [diff] [blame] | 527 | LOG(ERROR, "Failed to add pty bridge on VT%u.", vt); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 528 | term_close(new_terminal); |
| 529 | return NULL; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 530 | } |
| 531 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 532 | 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] | 533 | |
Stéphane Marchesin | 7831066 | 2016-01-06 16:09:39 -0800 | [diff] [blame] | 534 | status = term_resize(new_terminal); |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 535 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 536 | if (status < 0) { |
Dominik Behr | f932991 | 2016-08-25 17:31:52 -0700 | [diff] [blame] | 537 | LOG(ERROR, "Failed to resize VT%u.", vt); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 538 | term_close(new_terminal); |
| 539 | return NULL; |
| 540 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 541 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 542 | return new_terminal; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 543 | } |
| 544 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 545 | void term_activate(terminal_t* terminal) |
| 546 | { |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 547 | term_set_current_to(terminal); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 548 | terminal->active = true; |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 549 | fb_setmode(terminal->fb); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 550 | term_redraw(terminal); |
| 551 | } |
| 552 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 553 | void term_deactivate(terminal_t* terminal) |
| 554 | { |
| 555 | if (!terminal->active) |
| 556 | return; |
| 557 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 558 | terminal->active = false; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 561 | void term_close(terminal_t* term) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 562 | { |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 563 | char path[32]; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 564 | if (!term) |
| 565 | return; |
| 566 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 567 | snprintf(path, sizeof(path), FRECON_VT_PATH, term->vt); |
| 568 | unlink(path); |
| 569 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 570 | if (term->fb) { |
| 571 | fb_close(term->fb); |
| 572 | term->fb = NULL; |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 573 | } |
| 574 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 575 | if (term->term) { |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 576 | if (term->term->pty) { |
| 577 | if (term->term->pty_bridge >= 0) { |
| 578 | shl_pty_bridge_remove(term->term->pty_bridge, term->term->pty); |
| 579 | shl_pty_bridge_free(term->term->pty_bridge); |
| 580 | term->term->pty_bridge = -1; |
| 581 | } |
| 582 | shl_pty_close(term->term->pty); |
| 583 | term->term->pty = NULL; |
| 584 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 585 | free(term->term); |
| 586 | term->term = NULL; |
| 587 | } |
| 588 | |
Haixia Shi | 95d680e | 2015-04-27 20:29:17 -0700 | [diff] [blame] | 589 | font_free(); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 590 | free(term); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 591 | } |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 592 | |
| 593 | bool term_is_child_done(terminal_t* terminal) |
| 594 | { |
| 595 | int status; |
| 596 | int ret; |
| 597 | ret = waitpid(terminal->term->pid, &status, WNOHANG); |
| 598 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 599 | if ((ret == -1) && (errno == ECHILD)) { |
| 600 | return false; |
| 601 | } |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 602 | return ret != 0; |
| 603 | } |
| 604 | |
| 605 | void term_page_up(terminal_t* terminal) |
| 606 | { |
| 607 | tsm_screen_sb_page_up(terminal->term->screen, 1); |
| 608 | term_redraw(terminal); |
| 609 | } |
| 610 | |
| 611 | void term_page_down(terminal_t* terminal) |
| 612 | { |
| 613 | tsm_screen_sb_page_down(terminal->term->screen, 1); |
| 614 | term_redraw(terminal); |
| 615 | } |
| 616 | |
| 617 | void term_line_up(terminal_t* terminal) |
| 618 | { |
| 619 | tsm_screen_sb_up(terminal->term->screen, 1); |
| 620 | term_redraw(terminal); |
| 621 | } |
| 622 | |
| 623 | void term_line_down(terminal_t* terminal) |
| 624 | { |
| 625 | tsm_screen_sb_down(terminal->term->screen, 1); |
| 626 | term_redraw(terminal); |
| 627 | } |
| 628 | |
| 629 | bool term_is_valid(terminal_t* terminal) |
| 630 | { |
| 631 | return ((terminal != NULL) && (terminal->term != NULL)); |
| 632 | } |
| 633 | |
| 634 | int term_fd(terminal_t* terminal) |
| 635 | { |
| 636 | if (term_is_valid(terminal)) |
| 637 | return terminal->term->pty_bridge; |
| 638 | else |
| 639 | return -1; |
| 640 | } |
| 641 | |
| 642 | void term_dispatch_io(terminal_t* terminal, fd_set* read_set) |
| 643 | { |
| 644 | if (term_is_valid(terminal)) |
| 645 | if (FD_ISSET(terminal->term->pty_bridge, read_set)) |
| 646 | shl_pty_bridge_dispatch(terminal->term->pty_bridge, 0); |
| 647 | } |
| 648 | |
| 649 | bool term_exception(terminal_t* terminal, fd_set* exception_set) |
| 650 | { |
| 651 | if (term_is_valid(terminal)) { |
| 652 | if (terminal->term->pty_bridge >= 0) { |
| 653 | return FD_ISSET(terminal->term->pty_bridge, |
| 654 | exception_set); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | return false; |
| 659 | } |
| 660 | |
| 661 | bool term_is_active(terminal_t* terminal) |
| 662 | { |
| 663 | if (term_is_valid(terminal)) |
| 664 | return terminal->active; |
| 665 | |
| 666 | return false; |
| 667 | } |
| 668 | |
Dominik Behr | b1abcba | 2016-04-14 14:57:21 -0700 | [diff] [blame] | 669 | void term_add_fds(terminal_t* terminal, fd_set* read_set, fd_set* exception_set, int* maxfd) |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 670 | { |
| 671 | if (term_is_valid(terminal)) { |
| 672 | if (terminal->term->pty_bridge >= 0) { |
Dominik Behr | d711267 | 2016-01-20 16:59:34 -0800 | [diff] [blame] | 673 | *maxfd = MAX(*maxfd, terminal->term->pty_bridge); |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 674 | FD_SET(terminal->term->pty_bridge, read_set); |
| 675 | FD_SET(terminal->term->pty_bridge, exception_set); |
| 676 | } |
| 677 | } |
| 678 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 679 | |
| 680 | const char* term_get_ptsname(terminal_t* terminal) |
| 681 | { |
| 682 | return ptsname(shl_pty_get_fd(terminal->term->pty)); |
| 683 | } |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 684 | |
| 685 | void term_set_background(terminal_t* terminal, uint32_t bg) |
| 686 | { |
| 687 | terminal->background = bg; |
| 688 | terminal->background_valid = true; |
| 689 | } |
| 690 | |
| 691 | int term_show_image(terminal_t* terminal, image_t* image) |
| 692 | { |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 693 | return image_show(image, terminal->fb); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | void term_write_message(terminal_t* terminal, char* message) |
| 697 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 698 | FILE* fp; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 699 | |
| 700 | fp = fopen(term_get_ptsname(terminal), "w"); |
| 701 | if (fp) { |
| 702 | fputs(message, fp); |
| 703 | fclose(fp); |
| 704 | } |
| 705 | } |
| 706 | |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 707 | static void term_hide_cursor(terminal_t* terminal) |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 708 | { |
| 709 | term_write_message(terminal, "\033[?25l"); |
| 710 | } |
| 711 | |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 712 | __attribute__ ((unused)) |
| 713 | static void term_show_cursor(terminal_t* terminal) |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 714 | { |
| 715 | term_write_message(terminal, "\033[?25h"); |
| 716 | } |
David Sodman | 30a94ef | 2015-07-26 17:37:12 -0700 | [diff] [blame] | 717 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 718 | fb_t* term_getfb(terminal_t* terminal) |
David Sodman | 30a94ef | 2015-07-26 17:37:12 -0700 | [diff] [blame] | 719 | { |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 720 | return terminal->fb; |
David Sodman | 30a94ef | 2015-07-26 17:37:12 -0700 | [diff] [blame] | 721 | } |
Stéphane Marchesin | 08c08e7 | 2016-01-07 16:44:08 -0800 | [diff] [blame] | 722 | |
| 723 | terminal_t* term_get_terminal(int num) |
| 724 | { |
| 725 | return terminals[num]; |
| 726 | } |
| 727 | |
| 728 | void term_set_terminal(int num, terminal_t* terminal) |
| 729 | { |
| 730 | terminals[num] = terminal; |
| 731 | } |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 732 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 733 | int term_create_splash_term(int pts_fd) |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 734 | { |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 735 | terminal_t* terminal = term_init(TERM_SPLASH_TERMINAL, pts_fd); |
| 736 | |
| 737 | if (!terminal) { |
| 738 | LOG(ERROR, "Could not create splash term."); |
| 739 | return -1; |
| 740 | } |
| 741 | term_set_terminal(TERM_SPLASH_TERMINAL, terminal); |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 742 | |
| 743 | // Hide the cursor on the splash screen |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 744 | term_hide_cursor(terminal); |
| 745 | return 0; |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 746 | } |
| 747 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 748 | void term_destroy_splash_term(void) |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 749 | { |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 750 | terminal_t *terminal; |
| 751 | if (command_flags.enable_vt1) { |
| 752 | return; |
| 753 | } |
| 754 | terminal = term_get_terminal(TERM_SPLASH_TERMINAL); |
| 755 | term_set_terminal(TERM_SPLASH_TERMINAL, NULL); |
| 756 | term_close(terminal); |
Stéphane Marchesin | 92a297d | 2016-01-07 20:24:55 -0800 | [diff] [blame] | 757 | } |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 758 | |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 759 | void term_set_current(uint32_t t) |
| 760 | { |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 761 | if (t >= TERM_MAX_TERMINALS) |
| 762 | LOG(ERROR, "set_current: larger than array size"); |
| 763 | else |
| 764 | if (t >= term_num_terminals) |
| 765 | LOG(ERROR, "set_current: larger than num terminals"); |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 766 | else |
| 767 | current_terminal = t; |
| 768 | } |
| 769 | |
| 770 | uint32_t term_get_current(void) |
| 771 | { |
| 772 | return current_terminal; |
| 773 | } |
| 774 | |
| 775 | terminal_t *term_get_current_terminal(void) |
| 776 | { |
| 777 | return terminals[current_terminal]; |
| 778 | } |
| 779 | |
Dominik Behr | b1abcba | 2016-04-14 14:57:21 -0700 | [diff] [blame] | 780 | void term_set_current_terminal(terminal_t* terminal) |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 781 | { |
| 782 | terminals[current_terminal] = terminal; |
| 783 | } |
| 784 | |
| 785 | void term_set_current_to(terminal_t* terminal) |
| 786 | { |
| 787 | if (!terminal) { |
| 788 | terminals[current_terminal] = NULL; |
| 789 | current_terminal = 0; |
| 790 | return; |
| 791 | } |
| 792 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 793 | for (unsigned i = 0; i < term_num_terminals; i++) { |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 794 | if (terminal == terminals[i]) { |
| 795 | current_terminal = i; |
| 796 | return; |
| 797 | } |
| 798 | } |
| 799 | LOG(ERROR, "set_current_to: terminal not in array"); |
| 800 | } |
Dominik Behr | 01a7a58 | 2016-01-28 17:02:21 -0800 | [diff] [blame] | 801 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 802 | int term_switch_to(unsigned int vt) |
| 803 | { |
| 804 | terminal_t *terminal; |
| 805 | if (vt == term_get_current()) { |
| 806 | terminal = term_get_current_terminal(); |
Dominik Behr | f932991 | 2016-08-25 17:31:52 -0700 | [diff] [blame] | 807 | if (term_is_valid(terminal)) { |
| 808 | if (!term_is_active(terminal)) |
| 809 | term_activate(terminal); |
| 810 | return vt; |
| 811 | } |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | if (vt >= term_num_terminals) |
| 815 | return -EINVAL; |
| 816 | |
| 817 | terminal = term_get_current_terminal(); |
| 818 | if (term_is_active(terminal)) |
| 819 | term_deactivate(terminal); |
| 820 | |
| 821 | if (vt == TERM_SPLASH_TERMINAL |
| 822 | && !term_get_terminal(TERM_SPLASH_TERMINAL) |
| 823 | && !command_flags.enable_vt1) { |
| 824 | term_set_current(vt); |
| 825 | /* Splash term is already gone, returning to Chrome. */ |
| 826 | term_background(); |
| 827 | return vt; |
| 828 | } |
| 829 | |
| 830 | term_foreground(); |
| 831 | |
| 832 | term_set_current(vt); |
| 833 | terminal = term_get_current_terminal(); |
| 834 | if (!terminal) { |
| 835 | /* No terminal where we are switching to, create new one. */ |
| 836 | term_set_current_terminal(term_init(vt, -1)); |
| 837 | terminal = term_get_current_terminal(); |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 838 | if (!term_is_valid(terminal)) { |
Dominik Behr | f932991 | 2016-08-25 17:31:52 -0700 | [diff] [blame] | 839 | LOG(ERROR, "Term init failed VT%u.", vt); |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 840 | return -1; |
| 841 | } |
Dominik Behr | f932991 | 2016-08-25 17:31:52 -0700 | [diff] [blame] | 842 | term_activate(terminal); |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 843 | } else { |
| 844 | term_activate(terminal); |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | return vt; |
| 848 | } |
| 849 | |
Dominik Behr | 01a7a58 | 2016-01-28 17:02:21 -0800 | [diff] [blame] | 850 | void term_monitor_hotplug(void) |
| 851 | { |
| 852 | unsigned int t; |
| 853 | |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 854 | if (in_background) { |
| 855 | hotplug_occured = true; |
| 856 | return; |
| 857 | } |
| 858 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 859 | if (!drm_rescan()) |
| 860 | return; |
| 861 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 862 | for (t = 0; t < term_num_terminals; t++) { |
Dominik Behr | 01a7a58 | 2016-01-28 17:02:21 -0800 | [diff] [blame] | 863 | if (!terminals[t]) |
| 864 | continue; |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 865 | if (!terminals[t]->fb) |
Dominik Behr | 01a7a58 | 2016-01-28 17:02:21 -0800 | [diff] [blame] | 866 | continue; |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 867 | fb_buffer_destroy(terminals[t]->fb); |
Dominik Behr | a3f6571 | 2016-04-25 17:42:14 -0700 | [diff] [blame] | 868 | font_free(); |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 871 | for (t = 0; t < term_num_terminals; t++) { |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 872 | if (!terminals[t]) |
| 873 | continue; |
| 874 | if (!terminals[t]->fb) |
| 875 | continue; |
| 876 | fb_buffer_init(terminals[t]->fb); |
| 877 | term_resize(terminals[t]); |
| 878 | if (current_terminal == t && terminals[t]->active) |
| 879 | fb_setmode(terminals[t]->fb); |
| 880 | terminals[t]->term->age = 0; |
| 881 | term_redraw(terminals[t]); |
Dominik Behr | 01a7a58 | 2016-01-28 17:02:21 -0800 | [diff] [blame] | 882 | } |
| 883 | } |
Dominik Behr | b1abcba | 2016-04-14 14:57:21 -0700 | [diff] [blame] | 884 | |
| 885 | void term_redrm(terminal_t* terminal) |
| 886 | { |
| 887 | fb_buffer_destroy(terminal->fb); |
Dominik Behr | a3f6571 | 2016-04-25 17:42:14 -0700 | [diff] [blame] | 888 | font_free(); |
Dominik Behr | b1abcba | 2016-04-14 14:57:21 -0700 | [diff] [blame] | 889 | fb_buffer_init(terminal->fb); |
| 890 | term_resize(terminal); |
| 891 | terminal->term->age = 0; |
| 892 | term_redraw(terminal); |
| 893 | } |
| 894 | |
| 895 | void term_clear(terminal_t* terminal) |
| 896 | { |
| 897 | tsm_screen_erase_screen(terminal->term->screen, false); |
| 898 | term_redraw(terminal); |
| 899 | } |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 900 | |
| 901 | void term_background(void) |
| 902 | { |
| 903 | if (in_background) |
| 904 | return; |
| 905 | in_background = true; |
Dominik Behr | da3c010 | 2016-06-08 15:05:38 -0700 | [diff] [blame] | 906 | drm_dropmaster(NULL); |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 907 | dbus_take_display_ownership(); |
| 908 | } |
| 909 | |
| 910 | void term_foreground(void) |
| 911 | { |
Dominik Behr | da3c010 | 2016-06-08 15:05:38 -0700 | [diff] [blame] | 912 | int ret; |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 913 | if (!in_background) |
| 914 | return; |
| 915 | in_background = false; |
| 916 | if (!dbus_release_display_ownership()) { |
| 917 | LOG(ERROR, "Chrome did not release master. Frecon will try to steal it."); |
| 918 | set_drm_master_relax(); |
| 919 | } |
Dominik Behr | da3c010 | 2016-06-08 15:05:38 -0700 | [diff] [blame] | 920 | |
| 921 | ret = drm_setmaster(NULL); |
| 922 | if (ret < 0) { |
| 923 | /* |
| 924 | * In case there is high system load give Chrome some time |
| 925 | * and try again. */ |
| 926 | usleep(500 * 1000); |
| 927 | ret = drm_setmaster(NULL); |
| 928 | } |
| 929 | if (ret < 0) |
| 930 | LOG(ERROR, "Could not set master when switching to foreground %m."); |
| 931 | |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 932 | if (hotplug_occured) { |
| 933 | hotplug_occured = false; |
| 934 | term_monitor_hotplug(); |
| 935 | } |
| 936 | } |
Dominik Behr | 1883c04 | 2016-04-27 12:31:02 -0700 | [diff] [blame] | 937 | |
| 938 | void term_suspend_done(void* ignore) |
| 939 | { |
| 940 | term_monitor_hotplug(); |
| 941 | } |