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