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) { |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 456 | term_close(new_terminal); |
| 457 | return NULL; |
| 458 | } |
| 459 | |
| 460 | new_terminal->term = (struct term*)calloc(1, sizeof(*new_terminal->term)); |
| 461 | if (!new_terminal->term) { |
| 462 | term_close(new_terminal); |
| 463 | return NULL; |
| 464 | } |
| 465 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 466 | if (interactive) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 467 | new_terminal->exec = interactive_cmd_line; |
| 468 | else |
Dominik Behr | 32680e3 | 2016-08-16 12:18:41 -0700 | [diff] [blame] | 469 | new_terminal->exec = NULL; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 470 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 471 | status = tsm_screen_new(&new_terminal->term->screen, |
| 472 | log_tsm, new_terminal->term); |
zhuo-hao | 471f1e2 | 2015-08-12 14:55:56 +0800 | [diff] [blame] | 473 | if (status < 0) { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 474 | term_close(new_terminal); |
| 475 | return NULL; |
| 476 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 477 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 478 | tsm_screen_set_max_sb(new_terminal->term->screen, scrollback_size); |
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 | status = tsm_vte_new(&new_terminal->term->vte, new_terminal->term->screen, |
| 481 | term_write_cb, new_terminal->term, log_tsm, new_terminal->term); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 482 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 483 | if (status < 0) { |
| 484 | term_close(new_terminal); |
| 485 | return NULL; |
| 486 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 487 | |
Dominik Behr | d253090 | 2016-05-05 14:01:06 -0700 | [diff] [blame] | 488 | if (command_flags.enable_gfx) |
| 489 | tsm_vte_set_osc_cb(new_terminal->term->vte, term_osc_cb, (void *)new_terminal); |
| 490 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 491 | new_terminal->term->pty_bridge = shl_pty_bridge_new(); |
| 492 | if (new_terminal->term->pty_bridge < 0) { |
| 493 | term_close(new_terminal); |
| 494 | return NULL; |
| 495 | } |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 496 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 497 | status = shl_pty_open(&new_terminal->term->pty, |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 498 | term_read_cb, new_terminal, 1, 1, pts_fd); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 499 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 500 | if (status < 0) { |
| 501 | term_close(new_terminal); |
| 502 | return NULL; |
| 503 | } else if (status == 0) { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 504 | term_run_child(new_terminal); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 505 | exit(1); |
| 506 | } |
| 507 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 508 | status = mkdir(FRECON_RUN_DIR, S_IRWXU); |
| 509 | if (status == 0 || (status < 0 && errno == EEXIST)) { |
| 510 | char path[32]; |
| 511 | snprintf(path, sizeof(path), FRECON_VT_PATH, vt); |
Dominik Behr | b23b05b | 2016-08-15 16:29:02 -0700 | [diff] [blame^] | 512 | unlink(path); /* In case it already exists. Ignore return codes. */ |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 513 | if (symlink(ptsname(shl_pty_get_fd(new_terminal->term->pty)), path) < 0) |
| 514 | LOG(ERROR, "Failed to symlink pts name %s to %s, %d:%s", |
| 515 | path, |
| 516 | ptsname(shl_pty_get_fd(new_terminal->term->pty)), |
| 517 | errno, strerror(errno)); |
| 518 | } |
| 519 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 520 | status = shl_pty_bridge_add(new_terminal->term->pty_bridge, new_terminal->term->pty); |
| 521 | if (status) { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 522 | term_close(new_terminal); |
| 523 | return NULL; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 524 | } |
| 525 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 526 | 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] | 527 | |
Stéphane Marchesin | 7831066 | 2016-01-06 16:09:39 -0800 | [diff] [blame] | 528 | status = term_resize(new_terminal); |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 529 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 530 | if (status < 0) { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 531 | term_close(new_terminal); |
| 532 | return NULL; |
| 533 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 534 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 535 | return new_terminal; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 536 | } |
| 537 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 538 | void term_activate(terminal_t* terminal) |
| 539 | { |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 540 | term_set_current_to(terminal); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 541 | terminal->active = true; |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 542 | fb_setmode(terminal->fb); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 543 | term_redraw(terminal); |
| 544 | } |
| 545 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 546 | void term_deactivate(terminal_t* terminal) |
| 547 | { |
| 548 | if (!terminal->active) |
| 549 | return; |
| 550 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 551 | terminal->active = false; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 554 | void term_close(terminal_t* term) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 555 | { |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 556 | char path[32]; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 557 | if (!term) |
| 558 | return; |
| 559 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 560 | snprintf(path, sizeof(path), FRECON_VT_PATH, term->vt); |
| 561 | unlink(path); |
| 562 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 563 | if (term->fb) { |
| 564 | fb_close(term->fb); |
| 565 | term->fb = NULL; |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 566 | } |
| 567 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 568 | if (term->term) { |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 569 | if (term->term->pty) { |
| 570 | if (term->term->pty_bridge >= 0) { |
| 571 | shl_pty_bridge_remove(term->term->pty_bridge, term->term->pty); |
| 572 | shl_pty_bridge_free(term->term->pty_bridge); |
| 573 | term->term->pty_bridge = -1; |
| 574 | } |
| 575 | shl_pty_close(term->term->pty); |
| 576 | term->term->pty = NULL; |
| 577 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 578 | free(term->term); |
| 579 | term->term = NULL; |
| 580 | } |
| 581 | |
Haixia Shi | 95d680e | 2015-04-27 20:29:17 -0700 | [diff] [blame] | 582 | font_free(); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 583 | free(term); |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 584 | } |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 585 | |
| 586 | bool term_is_child_done(terminal_t* terminal) |
| 587 | { |
| 588 | int status; |
| 589 | int ret; |
| 590 | ret = waitpid(terminal->term->pid, &status, WNOHANG); |
| 591 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 592 | if ((ret == -1) && (errno == ECHILD)) { |
| 593 | return false; |
| 594 | } |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 595 | return ret != 0; |
| 596 | } |
| 597 | |
| 598 | void term_page_up(terminal_t* terminal) |
| 599 | { |
| 600 | tsm_screen_sb_page_up(terminal->term->screen, 1); |
| 601 | term_redraw(terminal); |
| 602 | } |
| 603 | |
| 604 | void term_page_down(terminal_t* terminal) |
| 605 | { |
| 606 | tsm_screen_sb_page_down(terminal->term->screen, 1); |
| 607 | term_redraw(terminal); |
| 608 | } |
| 609 | |
| 610 | void term_line_up(terminal_t* terminal) |
| 611 | { |
| 612 | tsm_screen_sb_up(terminal->term->screen, 1); |
| 613 | term_redraw(terminal); |
| 614 | } |
| 615 | |
| 616 | void term_line_down(terminal_t* terminal) |
| 617 | { |
| 618 | tsm_screen_sb_down(terminal->term->screen, 1); |
| 619 | term_redraw(terminal); |
| 620 | } |
| 621 | |
| 622 | bool term_is_valid(terminal_t* terminal) |
| 623 | { |
| 624 | return ((terminal != NULL) && (terminal->term != NULL)); |
| 625 | } |
| 626 | |
| 627 | int term_fd(terminal_t* terminal) |
| 628 | { |
| 629 | if (term_is_valid(terminal)) |
| 630 | return terminal->term->pty_bridge; |
| 631 | else |
| 632 | return -1; |
| 633 | } |
| 634 | |
| 635 | void term_dispatch_io(terminal_t* terminal, fd_set* read_set) |
| 636 | { |
| 637 | if (term_is_valid(terminal)) |
| 638 | if (FD_ISSET(terminal->term->pty_bridge, read_set)) |
| 639 | shl_pty_bridge_dispatch(terminal->term->pty_bridge, 0); |
| 640 | } |
| 641 | |
| 642 | bool term_exception(terminal_t* terminal, fd_set* exception_set) |
| 643 | { |
| 644 | if (term_is_valid(terminal)) { |
| 645 | if (terminal->term->pty_bridge >= 0) { |
| 646 | return FD_ISSET(terminal->term->pty_bridge, |
| 647 | exception_set); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | return false; |
| 652 | } |
| 653 | |
| 654 | bool term_is_active(terminal_t* terminal) |
| 655 | { |
| 656 | if (term_is_valid(terminal)) |
| 657 | return terminal->active; |
| 658 | |
| 659 | return false; |
| 660 | } |
| 661 | |
Dominik Behr | b1abcba | 2016-04-14 14:57:21 -0700 | [diff] [blame] | 662 | 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] | 663 | { |
| 664 | if (term_is_valid(terminal)) { |
| 665 | if (terminal->term->pty_bridge >= 0) { |
Dominik Behr | d711267 | 2016-01-20 16:59:34 -0800 | [diff] [blame] | 666 | *maxfd = MAX(*maxfd, terminal->term->pty_bridge); |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 667 | FD_SET(terminal->term->pty_bridge, read_set); |
| 668 | FD_SET(terminal->term->pty_bridge, exception_set); |
| 669 | } |
| 670 | } |
| 671 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 672 | |
| 673 | const char* term_get_ptsname(terminal_t* terminal) |
| 674 | { |
| 675 | return ptsname(shl_pty_get_fd(terminal->term->pty)); |
| 676 | } |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 677 | |
| 678 | void term_set_background(terminal_t* terminal, uint32_t bg) |
| 679 | { |
| 680 | terminal->background = bg; |
| 681 | terminal->background_valid = true; |
| 682 | } |
| 683 | |
| 684 | int term_show_image(terminal_t* terminal, image_t* image) |
| 685 | { |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 686 | return image_show(image, terminal->fb); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | void term_write_message(terminal_t* terminal, char* message) |
| 690 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 691 | FILE* fp; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 692 | |
| 693 | fp = fopen(term_get_ptsname(terminal), "w"); |
| 694 | if (fp) { |
| 695 | fputs(message, fp); |
| 696 | fclose(fp); |
| 697 | } |
| 698 | } |
| 699 | |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 700 | static void term_hide_cursor(terminal_t* terminal) |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 701 | { |
| 702 | term_write_message(terminal, "\033[?25l"); |
| 703 | } |
| 704 | |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 705 | __attribute__ ((unused)) |
| 706 | static void term_show_cursor(terminal_t* terminal) |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 707 | { |
| 708 | term_write_message(terminal, "\033[?25h"); |
| 709 | } |
David Sodman | 30a94ef | 2015-07-26 17:37:12 -0700 | [diff] [blame] | 710 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 711 | fb_t* term_getfb(terminal_t* terminal) |
David Sodman | 30a94ef | 2015-07-26 17:37:12 -0700 | [diff] [blame] | 712 | { |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 713 | return terminal->fb; |
David Sodman | 30a94ef | 2015-07-26 17:37:12 -0700 | [diff] [blame] | 714 | } |
Stéphane Marchesin | 08c08e7 | 2016-01-07 16:44:08 -0800 | [diff] [blame] | 715 | |
| 716 | terminal_t* term_get_terminal(int num) |
| 717 | { |
| 718 | return terminals[num]; |
| 719 | } |
| 720 | |
| 721 | void term_set_terminal(int num, terminal_t* terminal) |
| 722 | { |
| 723 | terminals[num] = terminal; |
| 724 | } |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 725 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 726 | int term_create_splash_term(int pts_fd) |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 727 | { |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 728 | terminal_t* terminal = term_init(TERM_SPLASH_TERMINAL, pts_fd); |
| 729 | |
| 730 | if (!terminal) { |
| 731 | LOG(ERROR, "Could not create splash term."); |
| 732 | return -1; |
| 733 | } |
| 734 | term_set_terminal(TERM_SPLASH_TERMINAL, terminal); |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 735 | |
| 736 | // Hide the cursor on the splash screen |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 737 | term_hide_cursor(terminal); |
| 738 | return 0; |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 739 | } |
| 740 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 741 | void term_destroy_splash_term(void) |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 742 | { |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 743 | terminal_t *terminal; |
| 744 | if (command_flags.enable_vt1) { |
| 745 | return; |
| 746 | } |
| 747 | terminal = term_get_terminal(TERM_SPLASH_TERMINAL); |
| 748 | term_set_terminal(TERM_SPLASH_TERMINAL, NULL); |
| 749 | term_close(terminal); |
Stéphane Marchesin | 92a297d | 2016-01-07 20:24:55 -0800 | [diff] [blame] | 750 | } |
Stéphane Marchesin | f75c851 | 2016-01-07 16:53:21 -0800 | [diff] [blame] | 751 | |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 752 | void term_set_current(uint32_t t) |
| 753 | { |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 754 | if (t >= TERM_MAX_TERMINALS) |
| 755 | LOG(ERROR, "set_current: larger than array size"); |
| 756 | else |
| 757 | if (t >= term_num_terminals) |
| 758 | LOG(ERROR, "set_current: larger than num terminals"); |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 759 | else |
| 760 | current_terminal = t; |
| 761 | } |
| 762 | |
| 763 | uint32_t term_get_current(void) |
| 764 | { |
| 765 | return current_terminal; |
| 766 | } |
| 767 | |
| 768 | terminal_t *term_get_current_terminal(void) |
| 769 | { |
| 770 | return terminals[current_terminal]; |
| 771 | } |
| 772 | |
Dominik Behr | b1abcba | 2016-04-14 14:57:21 -0700 | [diff] [blame] | 773 | void term_set_current_terminal(terminal_t* terminal) |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 774 | { |
| 775 | terminals[current_terminal] = terminal; |
| 776 | } |
| 777 | |
| 778 | void term_set_current_to(terminal_t* terminal) |
| 779 | { |
| 780 | if (!terminal) { |
| 781 | terminals[current_terminal] = NULL; |
| 782 | current_terminal = 0; |
| 783 | return; |
| 784 | } |
| 785 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 786 | for (unsigned i = 0; i < term_num_terminals; i++) { |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 787 | if (terminal == terminals[i]) { |
| 788 | current_terminal = i; |
| 789 | return; |
| 790 | } |
| 791 | } |
| 792 | LOG(ERROR, "set_current_to: terminal not in array"); |
| 793 | } |
Dominik Behr | 01a7a58 | 2016-01-28 17:02:21 -0800 | [diff] [blame] | 794 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 795 | int term_switch_to(unsigned int vt) |
| 796 | { |
| 797 | terminal_t *terminal; |
| 798 | if (vt == term_get_current()) { |
| 799 | terminal = term_get_current_terminal(); |
| 800 | if (!term_is_active(terminal)) |
| 801 | term_activate(terminal); |
| 802 | return vt; |
| 803 | } |
| 804 | |
| 805 | if (vt >= term_num_terminals) |
| 806 | return -EINVAL; |
| 807 | |
| 808 | terminal = term_get_current_terminal(); |
| 809 | if (term_is_active(terminal)) |
| 810 | term_deactivate(terminal); |
| 811 | |
| 812 | if (vt == TERM_SPLASH_TERMINAL |
| 813 | && !term_get_terminal(TERM_SPLASH_TERMINAL) |
| 814 | && !command_flags.enable_vt1) { |
| 815 | term_set_current(vt); |
| 816 | /* Splash term is already gone, returning to Chrome. */ |
| 817 | term_background(); |
| 818 | return vt; |
| 819 | } |
| 820 | |
| 821 | term_foreground(); |
| 822 | |
| 823 | term_set_current(vt); |
| 824 | terminal = term_get_current_terminal(); |
| 825 | if (!terminal) { |
| 826 | /* No terminal where we are switching to, create new one. */ |
| 827 | term_set_current_terminal(term_init(vt, -1)); |
| 828 | terminal = term_get_current_terminal(); |
| 829 | term_activate(terminal); |
| 830 | if (!term_is_valid(terminal)) { |
| 831 | LOG(ERROR, "Term init failed"); |
| 832 | return -1; |
| 833 | } |
| 834 | } else { |
| 835 | term_activate(terminal); |
| 836 | LOG(INFO, "Activated existing terminal %p on VT%u", terminal, vt); |
| 837 | } |
| 838 | |
| 839 | return vt; |
| 840 | } |
| 841 | |
Dominik Behr | 01a7a58 | 2016-01-28 17:02:21 -0800 | [diff] [blame] | 842 | void term_monitor_hotplug(void) |
| 843 | { |
| 844 | unsigned int t; |
| 845 | |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 846 | if (in_background) { |
| 847 | hotplug_occured = true; |
| 848 | return; |
| 849 | } |
| 850 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 851 | if (!drm_rescan()) |
| 852 | return; |
| 853 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 854 | for (t = 0; t < term_num_terminals; t++) { |
Dominik Behr | 01a7a58 | 2016-01-28 17:02:21 -0800 | [diff] [blame] | 855 | if (!terminals[t]) |
| 856 | continue; |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 857 | if (!terminals[t]->fb) |
Dominik Behr | 01a7a58 | 2016-01-28 17:02:21 -0800 | [diff] [blame] | 858 | continue; |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 859 | fb_buffer_destroy(terminals[t]->fb); |
Dominik Behr | a3f6571 | 2016-04-25 17:42:14 -0700 | [diff] [blame] | 860 | font_free(); |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 861 | } |
| 862 | |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 863 | for (t = 0; t < term_num_terminals; t++) { |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 864 | if (!terminals[t]) |
| 865 | continue; |
| 866 | if (!terminals[t]->fb) |
| 867 | continue; |
| 868 | fb_buffer_init(terminals[t]->fb); |
| 869 | term_resize(terminals[t]); |
| 870 | if (current_terminal == t && terminals[t]->active) |
| 871 | fb_setmode(terminals[t]->fb); |
| 872 | terminals[t]->term->age = 0; |
| 873 | term_redraw(terminals[t]); |
Dominik Behr | 01a7a58 | 2016-01-28 17:02:21 -0800 | [diff] [blame] | 874 | } |
| 875 | } |
Dominik Behr | b1abcba | 2016-04-14 14:57:21 -0700 | [diff] [blame] | 876 | |
| 877 | void term_redrm(terminal_t* terminal) |
| 878 | { |
| 879 | fb_buffer_destroy(terminal->fb); |
Dominik Behr | a3f6571 | 2016-04-25 17:42:14 -0700 | [diff] [blame] | 880 | font_free(); |
Dominik Behr | b1abcba | 2016-04-14 14:57:21 -0700 | [diff] [blame] | 881 | fb_buffer_init(terminal->fb); |
| 882 | term_resize(terminal); |
| 883 | terminal->term->age = 0; |
| 884 | term_redraw(terminal); |
| 885 | } |
| 886 | |
| 887 | void term_clear(terminal_t* terminal) |
| 888 | { |
| 889 | tsm_screen_erase_screen(terminal->term->screen, false); |
| 890 | term_redraw(terminal); |
| 891 | } |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 892 | |
| 893 | void term_background(void) |
| 894 | { |
| 895 | if (in_background) |
| 896 | return; |
| 897 | in_background = true; |
Dominik Behr | da3c010 | 2016-06-08 15:05:38 -0700 | [diff] [blame] | 898 | drm_dropmaster(NULL); |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 899 | dbus_take_display_ownership(); |
| 900 | } |
| 901 | |
| 902 | void term_foreground(void) |
| 903 | { |
Dominik Behr | da3c010 | 2016-06-08 15:05:38 -0700 | [diff] [blame] | 904 | int ret; |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 905 | if (!in_background) |
| 906 | return; |
| 907 | in_background = false; |
| 908 | if (!dbus_release_display_ownership()) { |
| 909 | LOG(ERROR, "Chrome did not release master. Frecon will try to steal it."); |
| 910 | set_drm_master_relax(); |
| 911 | } |
Dominik Behr | da3c010 | 2016-06-08 15:05:38 -0700 | [diff] [blame] | 912 | |
| 913 | ret = drm_setmaster(NULL); |
| 914 | if (ret < 0) { |
| 915 | /* |
| 916 | * In case there is high system load give Chrome some time |
| 917 | * and try again. */ |
| 918 | usleep(500 * 1000); |
| 919 | ret = drm_setmaster(NULL); |
| 920 | } |
| 921 | if (ret < 0) |
| 922 | LOG(ERROR, "Could not set master when switching to foreground %m."); |
| 923 | |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 924 | if (hotplug_occured) { |
| 925 | hotplug_occured = false; |
| 926 | term_monitor_hotplug(); |
| 927 | } |
| 928 | } |
Dominik Behr | 1883c04 | 2016-04-27 12:31:02 -0700 | [diff] [blame] | 929 | |
| 930 | void term_suspend_done(void* ignore) |
| 931 | { |
| 932 | term_monitor_hotplug(); |
| 933 | } |