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