1 /* 2 * QEMU graphical console 3 * 4 * Copyright (c) 2004 Fabrice Bellard 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 #include "qemu/osdep.h" 25 #include "qemu-common.h" 26 #include "ui/console.h" 27 #include "hw/qdev-core.h" 28 #include "qemu/timer.h" 29 #include "qmp-commands.h" 30 #include "sysemu/char.h" 31 #include "trace.h" 32 #include "exec/memory.h" 33 34 #define DEFAULT_BACKSCROLL 512 35 #define CONSOLE_CURSOR_PERIOD 500 36 37 typedef struct TextAttributes { 38 uint8_t fgcol:4; 39 uint8_t bgcol:4; 40 uint8_t bold:1; 41 uint8_t uline:1; 42 uint8_t blink:1; 43 uint8_t invers:1; 44 uint8_t unvisible:1; 45 } TextAttributes; 46 47 typedef struct TextCell { 48 uint8_t ch; 49 TextAttributes t_attrib; 50 } TextCell; 51 52 #define MAX_ESC_PARAMS 3 53 54 enum TTYState { 55 TTY_STATE_NORM, 56 TTY_STATE_ESC, 57 TTY_STATE_CSI, 58 }; 59 60 typedef struct QEMUFIFO { 61 uint8_t *buf; 62 int buf_size; 63 int count, wptr, rptr; 64 } QEMUFIFO; 65 66 static int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1) 67 { 68 int l, len; 69 70 l = f->buf_size - f->count; 71 if (len1 > l) 72 len1 = l; 73 len = len1; 74 while (len > 0) { 75 l = f->buf_size - f->wptr; 76 if (l > len) 77 l = len; 78 memcpy(f->buf + f->wptr, buf, l); 79 f->wptr += l; 80 if (f->wptr >= f->buf_size) 81 f->wptr = 0; 82 buf += l; 83 len -= l; 84 } 85 f->count += len1; 86 return len1; 87 } 88 89 static int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1) 90 { 91 int l, len; 92 93 if (len1 > f->count) 94 len1 = f->count; 95 len = len1; 96 while (len > 0) { 97 l = f->buf_size - f->rptr; 98 if (l > len) 99 l = len; 100 memcpy(buf, f->buf + f->rptr, l); 101 f->rptr += l; 102 if (f->rptr >= f->buf_size) 103 f->rptr = 0; 104 buf += l; 105 len -= l; 106 } 107 f->count -= len1; 108 return len1; 109 } 110 111 typedef enum { 112 GRAPHIC_CONSOLE, 113 TEXT_CONSOLE, 114 TEXT_CONSOLE_FIXED_SIZE 115 } console_type_t; 116 117 struct QemuConsole { 118 Object parent; 119 120 int index; 121 console_type_t console_type; 122 DisplayState *ds; 123 DisplaySurface *surface; 124 int dcls; 125 DisplayChangeListener *gl; 126 127 /* Graphic console state. */ 128 Object *device; 129 uint32_t head; 130 QemuUIInfo ui_info; 131 QEMUTimer *ui_timer; 132 const GraphicHwOps *hw_ops; 133 void *hw; 134 135 /* Text console state */ 136 int width; 137 int height; 138 int total_height; 139 int backscroll_height; 140 int x, y; 141 int x_saved, y_saved; 142 int y_displayed; 143 int y_base; 144 TextAttributes t_attrib_default; /* default text attributes */ 145 TextAttributes t_attrib; /* currently active text attributes */ 146 TextCell *cells; 147 int text_x[2], text_y[2], cursor_invalidate; 148 int echo; 149 150 int update_x0; 151 int update_y0; 152 int update_x1; 153 int update_y1; 154 155 enum TTYState state; 156 int esc_params[MAX_ESC_PARAMS]; 157 int nb_esc_params; 158 159 CharDriverState *chr; 160 /* fifo for key pressed */ 161 QEMUFIFO out_fifo; 162 uint8_t out_fifo_buf[16]; 163 QEMUTimer *kbd_timer; 164 }; 165 166 struct DisplayState { 167 QEMUTimer *gui_timer; 168 uint64_t last_update; 169 uint64_t update_interval; 170 bool refreshing; 171 bool have_gfx; 172 bool have_text; 173 174 QLIST_HEAD(, DisplayChangeListener) listeners; 175 }; 176 177 static DisplayState *display_state; 178 static QemuConsole *active_console; 179 static QemuConsole **consoles; 180 static int nb_consoles = 0; 181 static bool cursor_visible_phase; 182 static QEMUTimer *cursor_timer; 183 184 static void text_console_do_init(CharDriverState *chr, DisplayState *ds); 185 static void dpy_refresh(DisplayState *s); 186 static DisplayState *get_alloc_displaystate(void); 187 static void text_console_update_cursor_timer(void); 188 static void text_console_update_cursor(void *opaque); 189 190 static void gui_update(void *opaque) 191 { 192 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE; 193 uint64_t dcl_interval; 194 DisplayState *ds = opaque; 195 DisplayChangeListener *dcl; 196 int i; 197 198 ds->refreshing = true; 199 dpy_refresh(ds); 200 ds->refreshing = false; 201 202 QLIST_FOREACH(dcl, &ds->listeners, next) { 203 dcl_interval = dcl->update_interval ? 204 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT; 205 if (interval > dcl_interval) { 206 interval = dcl_interval; 207 } 208 } 209 if (ds->update_interval != interval) { 210 ds->update_interval = interval; 211 for (i = 0; i < nb_consoles; i++) { 212 if (consoles[i]->hw_ops->update_interval) { 213 consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval); 214 } 215 } 216 trace_console_refresh(interval); 217 } 218 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 219 timer_mod(ds->gui_timer, ds->last_update + interval); 220 } 221 222 static void gui_setup_refresh(DisplayState *ds) 223 { 224 DisplayChangeListener *dcl; 225 bool need_timer = false; 226 bool have_gfx = false; 227 bool have_text = false; 228 229 QLIST_FOREACH(dcl, &ds->listeners, next) { 230 if (dcl->ops->dpy_refresh != NULL) { 231 need_timer = true; 232 } 233 if (dcl->ops->dpy_gfx_update != NULL) { 234 have_gfx = true; 235 } 236 if (dcl->ops->dpy_text_update != NULL) { 237 have_text = true; 238 } 239 } 240 241 if (need_timer && ds->gui_timer == NULL) { 242 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds); 243 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)); 244 } 245 if (!need_timer && ds->gui_timer != NULL) { 246 timer_del(ds->gui_timer); 247 timer_free(ds->gui_timer); 248 ds->gui_timer = NULL; 249 } 250 251 ds->have_gfx = have_gfx; 252 ds->have_text = have_text; 253 } 254 255 void graphic_hw_update(QemuConsole *con) 256 { 257 if (!con) { 258 con = active_console; 259 } 260 if (con && con->hw_ops->gfx_update) { 261 con->hw_ops->gfx_update(con->hw); 262 } 263 } 264 265 void graphic_hw_gl_block(QemuConsole *con, bool block) 266 { 267 if (!con) { 268 con = active_console; 269 } 270 if (con && con->hw_ops->gl_block) { 271 con->hw_ops->gl_block(con->hw, block); 272 } 273 } 274 275 void graphic_hw_invalidate(QemuConsole *con) 276 { 277 if (!con) { 278 con = active_console; 279 } 280 if (con && con->hw_ops->invalidate) { 281 con->hw_ops->invalidate(con->hw); 282 } 283 } 284 285 static void ppm_save(const char *filename, DisplaySurface *ds, 286 Error **errp) 287 { 288 int width = pixman_image_get_width(ds->image); 289 int height = pixman_image_get_height(ds->image); 290 int fd; 291 FILE *f; 292 int y; 293 int ret; 294 pixman_image_t *linebuf; 295 296 trace_ppm_save(filename, ds); 297 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); 298 if (fd == -1) { 299 error_setg(errp, "failed to open file '%s': %s", filename, 300 strerror(errno)); 301 return; 302 } 303 f = fdopen(fd, "wb"); 304 ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255); 305 if (ret < 0) { 306 linebuf = NULL; 307 goto write_err; 308 } 309 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width); 310 for (y = 0; y < height; y++) { 311 qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y); 312 clearerr(f); 313 ret = fwrite(pixman_image_get_data(linebuf), 1, 314 pixman_image_get_stride(linebuf), f); 315 (void)ret; 316 if (ferror(f)) { 317 goto write_err; 318 } 319 } 320 321 out: 322 qemu_pixman_image_unref(linebuf); 323 fclose(f); 324 return; 325 326 write_err: 327 error_setg(errp, "failed to write to file '%s': %s", filename, 328 strerror(errno)); 329 unlink(filename); 330 goto out; 331 } 332 333 void qmp_screendump(const char *filename, Error **errp) 334 { 335 QemuConsole *con = qemu_console_lookup_by_index(0); 336 DisplaySurface *surface; 337 338 if (con == NULL) { 339 error_setg(errp, "There is no QemuConsole I can screendump from."); 340 return; 341 } 342 343 graphic_hw_update(con); 344 surface = qemu_console_surface(con); 345 ppm_save(filename, surface, errp); 346 } 347 348 void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata) 349 { 350 if (!con) { 351 con = active_console; 352 } 353 if (con && con->hw_ops->text_update) { 354 con->hw_ops->text_update(con->hw, chardata); 355 } 356 } 357 358 static void vga_fill_rect(QemuConsole *con, 359 int posx, int posy, int width, int height, 360 pixman_color_t color) 361 { 362 DisplaySurface *surface = qemu_console_surface(con); 363 pixman_rectangle16_t rect = { 364 .x = posx, .y = posy, .width = width, .height = height 365 }; 366 367 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image, 368 &color, 1, &rect); 369 } 370 371 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */ 372 static void vga_bitblt(QemuConsole *con, 373 int xs, int ys, int xd, int yd, int w, int h) 374 { 375 DisplaySurface *surface = qemu_console_surface(con); 376 377 pixman_image_composite(PIXMAN_OP_SRC, 378 surface->image, NULL, surface->image, 379 xs, ys, 0, 0, xd, yd, w, h); 380 } 381 382 /***********************************************************/ 383 /* basic char display */ 384 385 #define FONT_HEIGHT 16 386 #define FONT_WIDTH 8 387 388 #include "vgafont.h" 389 390 #define QEMU_RGB(r, g, b) \ 391 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff } 392 393 static const pixman_color_t color_table_rgb[2][8] = { 394 { /* dark */ 395 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */ 396 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */ 397 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */ 398 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */ 399 [QEMU_COLOR_RED] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */ 400 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */ 401 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */ 402 [QEMU_COLOR_WHITE] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */ 403 }, 404 { /* bright */ 405 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */ 406 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */ 407 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xff, 0x00), /* green */ 408 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */ 409 [QEMU_COLOR_RED] = QEMU_RGB(0xff, 0x00, 0x00), /* red */ 410 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */ 411 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */ 412 [QEMU_COLOR_WHITE] = QEMU_RGB(0xff, 0xff, 0xff), /* white */ 413 } 414 }; 415 416 static void vga_putcharxy(QemuConsole *s, int x, int y, int ch, 417 TextAttributes *t_attrib) 418 { 419 static pixman_image_t *glyphs[256]; 420 DisplaySurface *surface = qemu_console_surface(s); 421 pixman_color_t fgcol, bgcol; 422 423 if (t_attrib->invers) { 424 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol]; 425 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol]; 426 } else { 427 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol]; 428 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol]; 429 } 430 431 if (!glyphs[ch]) { 432 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch); 433 } 434 qemu_pixman_glyph_render(glyphs[ch], surface->image, 435 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT); 436 } 437 438 static void text_console_resize(QemuConsole *s) 439 { 440 TextCell *cells, *c, *c1; 441 int w1, x, y, last_width; 442 443 last_width = s->width; 444 s->width = surface_width(s->surface) / FONT_WIDTH; 445 s->height = surface_height(s->surface) / FONT_HEIGHT; 446 447 w1 = last_width; 448 if (s->width < w1) 449 w1 = s->width; 450 451 cells = g_new(TextCell, s->width * s->total_height); 452 for(y = 0; y < s->total_height; y++) { 453 c = &cells[y * s->width]; 454 if (w1 > 0) { 455 c1 = &s->cells[y * last_width]; 456 for(x = 0; x < w1; x++) { 457 *c++ = *c1++; 458 } 459 } 460 for(x = w1; x < s->width; x++) { 461 c->ch = ' '; 462 c->t_attrib = s->t_attrib_default; 463 c++; 464 } 465 } 466 g_free(s->cells); 467 s->cells = cells; 468 } 469 470 static inline void text_update_xy(QemuConsole *s, int x, int y) 471 { 472 s->text_x[0] = MIN(s->text_x[0], x); 473 s->text_x[1] = MAX(s->text_x[1], x); 474 s->text_y[0] = MIN(s->text_y[0], y); 475 s->text_y[1] = MAX(s->text_y[1], y); 476 } 477 478 static void invalidate_xy(QemuConsole *s, int x, int y) 479 { 480 if (!qemu_console_is_visible(s)) { 481 return; 482 } 483 if (s->update_x0 > x * FONT_WIDTH) 484 s->update_x0 = x * FONT_WIDTH; 485 if (s->update_y0 > y * FONT_HEIGHT) 486 s->update_y0 = y * FONT_HEIGHT; 487 if (s->update_x1 < (x + 1) * FONT_WIDTH) 488 s->update_x1 = (x + 1) * FONT_WIDTH; 489 if (s->update_y1 < (y + 1) * FONT_HEIGHT) 490 s->update_y1 = (y + 1) * FONT_HEIGHT; 491 } 492 493 static void update_xy(QemuConsole *s, int x, int y) 494 { 495 TextCell *c; 496 int y1, y2; 497 498 if (s->ds->have_text) { 499 text_update_xy(s, x, y); 500 } 501 502 y1 = (s->y_base + y) % s->total_height; 503 y2 = y1 - s->y_displayed; 504 if (y2 < 0) { 505 y2 += s->total_height; 506 } 507 if (y2 < s->height) { 508 c = &s->cells[y1 * s->width + x]; 509 vga_putcharxy(s, x, y2, c->ch, 510 &(c->t_attrib)); 511 invalidate_xy(s, x, y2); 512 } 513 } 514 515 static void console_show_cursor(QemuConsole *s, int show) 516 { 517 TextCell *c; 518 int y, y1; 519 int x = s->x; 520 521 if (s->ds->have_text) { 522 s->cursor_invalidate = 1; 523 } 524 525 if (x >= s->width) { 526 x = s->width - 1; 527 } 528 y1 = (s->y_base + s->y) % s->total_height; 529 y = y1 - s->y_displayed; 530 if (y < 0) { 531 y += s->total_height; 532 } 533 if (y < s->height) { 534 c = &s->cells[y1 * s->width + x]; 535 if (show && cursor_visible_phase) { 536 TextAttributes t_attrib = s->t_attrib_default; 537 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */ 538 vga_putcharxy(s, x, y, c->ch, &t_attrib); 539 } else { 540 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib)); 541 } 542 invalidate_xy(s, x, y); 543 } 544 } 545 546 static void console_refresh(QemuConsole *s) 547 { 548 DisplaySurface *surface = qemu_console_surface(s); 549 TextCell *c; 550 int x, y, y1; 551 552 if (s->ds->have_text) { 553 s->text_x[0] = 0; 554 s->text_y[0] = 0; 555 s->text_x[1] = s->width - 1; 556 s->text_y[1] = s->height - 1; 557 s->cursor_invalidate = 1; 558 } 559 560 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface), 561 color_table_rgb[0][QEMU_COLOR_BLACK]); 562 y1 = s->y_displayed; 563 for (y = 0; y < s->height; y++) { 564 c = s->cells + y1 * s->width; 565 for (x = 0; x < s->width; x++) { 566 vga_putcharxy(s, x, y, c->ch, 567 &(c->t_attrib)); 568 c++; 569 } 570 if (++y1 == s->total_height) { 571 y1 = 0; 572 } 573 } 574 console_show_cursor(s, 1); 575 dpy_gfx_update(s, 0, 0, 576 surface_width(surface), surface_height(surface)); 577 } 578 579 static void console_scroll(QemuConsole *s, int ydelta) 580 { 581 int i, y1; 582 583 if (ydelta > 0) { 584 for(i = 0; i < ydelta; i++) { 585 if (s->y_displayed == s->y_base) 586 break; 587 if (++s->y_displayed == s->total_height) 588 s->y_displayed = 0; 589 } 590 } else { 591 ydelta = -ydelta; 592 i = s->backscroll_height; 593 if (i > s->total_height - s->height) 594 i = s->total_height - s->height; 595 y1 = s->y_base - i; 596 if (y1 < 0) 597 y1 += s->total_height; 598 for(i = 0; i < ydelta; i++) { 599 if (s->y_displayed == y1) 600 break; 601 if (--s->y_displayed < 0) 602 s->y_displayed = s->total_height - 1; 603 } 604 } 605 console_refresh(s); 606 } 607 608 static void console_put_lf(QemuConsole *s) 609 { 610 TextCell *c; 611 int x, y1; 612 613 s->y++; 614 if (s->y >= s->height) { 615 s->y = s->height - 1; 616 617 if (s->y_displayed == s->y_base) { 618 if (++s->y_displayed == s->total_height) 619 s->y_displayed = 0; 620 } 621 if (++s->y_base == s->total_height) 622 s->y_base = 0; 623 if (s->backscroll_height < s->total_height) 624 s->backscroll_height++; 625 y1 = (s->y_base + s->height - 1) % s->total_height; 626 c = &s->cells[y1 * s->width]; 627 for(x = 0; x < s->width; x++) { 628 c->ch = ' '; 629 c->t_attrib = s->t_attrib_default; 630 c++; 631 } 632 if (s->y_displayed == s->y_base) { 633 if (s->ds->have_text) { 634 s->text_x[0] = 0; 635 s->text_y[0] = 0; 636 s->text_x[1] = s->width - 1; 637 s->text_y[1] = s->height - 1; 638 } 639 640 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0, 641 s->width * FONT_WIDTH, 642 (s->height - 1) * FONT_HEIGHT); 643 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT, 644 s->width * FONT_WIDTH, FONT_HEIGHT, 645 color_table_rgb[0][s->t_attrib_default.bgcol]); 646 s->update_x0 = 0; 647 s->update_y0 = 0; 648 s->update_x1 = s->width * FONT_WIDTH; 649 s->update_y1 = s->height * FONT_HEIGHT; 650 } 651 } 652 } 653 654 /* Set console attributes depending on the current escape codes. 655 * NOTE: I know this code is not very efficient (checking every color for it 656 * self) but it is more readable and better maintainable. 657 */ 658 static void console_handle_escape(QemuConsole *s) 659 { 660 int i; 661 662 for (i=0; i<s->nb_esc_params; i++) { 663 switch (s->esc_params[i]) { 664 case 0: /* reset all console attributes to default */ 665 s->t_attrib = s->t_attrib_default; 666 break; 667 case 1: 668 s->t_attrib.bold = 1; 669 break; 670 case 4: 671 s->t_attrib.uline = 1; 672 break; 673 case 5: 674 s->t_attrib.blink = 1; 675 break; 676 case 7: 677 s->t_attrib.invers = 1; 678 break; 679 case 8: 680 s->t_attrib.unvisible = 1; 681 break; 682 case 22: 683 s->t_attrib.bold = 0; 684 break; 685 case 24: 686 s->t_attrib.uline = 0; 687 break; 688 case 25: 689 s->t_attrib.blink = 0; 690 break; 691 case 27: 692 s->t_attrib.invers = 0; 693 break; 694 case 28: 695 s->t_attrib.unvisible = 0; 696 break; 697 /* set foreground color */ 698 case 30: 699 s->t_attrib.fgcol = QEMU_COLOR_BLACK; 700 break; 701 case 31: 702 s->t_attrib.fgcol = QEMU_COLOR_RED; 703 break; 704 case 32: 705 s->t_attrib.fgcol = QEMU_COLOR_GREEN; 706 break; 707 case 33: 708 s->t_attrib.fgcol = QEMU_COLOR_YELLOW; 709 break; 710 case 34: 711 s->t_attrib.fgcol = QEMU_COLOR_BLUE; 712 break; 713 case 35: 714 s->t_attrib.fgcol = QEMU_COLOR_MAGENTA; 715 break; 716 case 36: 717 s->t_attrib.fgcol = QEMU_COLOR_CYAN; 718 break; 719 case 37: 720 s->t_attrib.fgcol = QEMU_COLOR_WHITE; 721 break; 722 /* set background color */ 723 case 40: 724 s->t_attrib.bgcol = QEMU_COLOR_BLACK; 725 break; 726 case 41: 727 s->t_attrib.bgcol = QEMU_COLOR_RED; 728 break; 729 case 42: 730 s->t_attrib.bgcol = QEMU_COLOR_GREEN; 731 break; 732 case 43: 733 s->t_attrib.bgcol = QEMU_COLOR_YELLOW; 734 break; 735 case 44: 736 s->t_attrib.bgcol = QEMU_COLOR_BLUE; 737 break; 738 case 45: 739 s->t_attrib.bgcol = QEMU_COLOR_MAGENTA; 740 break; 741 case 46: 742 s->t_attrib.bgcol = QEMU_COLOR_CYAN; 743 break; 744 case 47: 745 s->t_attrib.bgcol = QEMU_COLOR_WHITE; 746 break; 747 } 748 } 749 } 750 751 static void console_clear_xy(QemuConsole *s, int x, int y) 752 { 753 int y1 = (s->y_base + y) % s->total_height; 754 TextCell *c = &s->cells[y1 * s->width + x]; 755 c->ch = ' '; 756 c->t_attrib = s->t_attrib_default; 757 update_xy(s, x, y); 758 } 759 760 static void console_put_one(QemuConsole *s, int ch) 761 { 762 TextCell *c; 763 int y1; 764 if (s->x >= s->width) { 765 /* line wrap */ 766 s->x = 0; 767 console_put_lf(s); 768 } 769 y1 = (s->y_base + s->y) % s->total_height; 770 c = &s->cells[y1 * s->width + s->x]; 771 c->ch = ch; 772 c->t_attrib = s->t_attrib; 773 update_xy(s, s->x, s->y); 774 s->x++; 775 } 776 777 static void console_respond_str(QemuConsole *s, const char *buf) 778 { 779 while (*buf) { 780 console_put_one(s, *buf); 781 buf++; 782 } 783 } 784 785 /* set cursor, checking bounds */ 786 static void set_cursor(QemuConsole *s, int x, int y) 787 { 788 if (x < 0) { 789 x = 0; 790 } 791 if (y < 0) { 792 y = 0; 793 } 794 if (y >= s->height) { 795 y = s->height - 1; 796 } 797 if (x >= s->width) { 798 x = s->width - 1; 799 } 800 801 s->x = x; 802 s->y = y; 803 } 804 805 static void console_putchar(QemuConsole *s, int ch) 806 { 807 int i; 808 int x, y; 809 char response[40]; 810 811 switch(s->state) { 812 case TTY_STATE_NORM: 813 switch(ch) { 814 case '\r': /* carriage return */ 815 s->x = 0; 816 break; 817 case '\n': /* newline */ 818 console_put_lf(s); 819 break; 820 case '\b': /* backspace */ 821 if (s->x > 0) 822 s->x--; 823 break; 824 case '\t': /* tabspace */ 825 if (s->x + (8 - (s->x % 8)) > s->width) { 826 s->x = 0; 827 console_put_lf(s); 828 } else { 829 s->x = s->x + (8 - (s->x % 8)); 830 } 831 break; 832 case '\a': /* alert aka. bell */ 833 /* TODO: has to be implemented */ 834 break; 835 case 14: 836 /* SI (shift in), character set 0 (ignored) */ 837 break; 838 case 15: 839 /* SO (shift out), character set 1 (ignored) */ 840 break; 841 case 27: /* esc (introducing an escape sequence) */ 842 s->state = TTY_STATE_ESC; 843 break; 844 default: 845 console_put_one(s, ch); 846 break; 847 } 848 break; 849 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */ 850 if (ch == '[') { 851 for(i=0;i<MAX_ESC_PARAMS;i++) 852 s->esc_params[i] = 0; 853 s->nb_esc_params = 0; 854 s->state = TTY_STATE_CSI; 855 } else { 856 s->state = TTY_STATE_NORM; 857 } 858 break; 859 case TTY_STATE_CSI: /* handle escape sequence parameters */ 860 if (ch >= '0' && ch <= '9') { 861 if (s->nb_esc_params < MAX_ESC_PARAMS) { 862 int *param = &s->esc_params[s->nb_esc_params]; 863 int digit = (ch - '0'); 864 865 *param = (*param <= (INT_MAX - digit) / 10) ? 866 *param * 10 + digit : INT_MAX; 867 } 868 } else { 869 if (s->nb_esc_params < MAX_ESC_PARAMS) 870 s->nb_esc_params++; 871 if (ch == ';') 872 break; 873 trace_console_putchar_csi(s->esc_params[0], s->esc_params[1], 874 ch, s->nb_esc_params); 875 s->state = TTY_STATE_NORM; 876 switch(ch) { 877 case 'A': 878 /* move cursor up */ 879 if (s->esc_params[0] == 0) { 880 s->esc_params[0] = 1; 881 } 882 set_cursor(s, s->x, s->y - s->esc_params[0]); 883 break; 884 case 'B': 885 /* move cursor down */ 886 if (s->esc_params[0] == 0) { 887 s->esc_params[0] = 1; 888 } 889 set_cursor(s, s->x, s->y + s->esc_params[0]); 890 break; 891 case 'C': 892 /* move cursor right */ 893 if (s->esc_params[0] == 0) { 894 s->esc_params[0] = 1; 895 } 896 set_cursor(s, s->x + s->esc_params[0], s->y); 897 break; 898 case 'D': 899 /* move cursor left */ 900 if (s->esc_params[0] == 0) { 901 s->esc_params[0] = 1; 902 } 903 set_cursor(s, s->x - s->esc_params[0], s->y); 904 break; 905 case 'G': 906 /* move cursor to column */ 907 set_cursor(s, s->esc_params[0] - 1, s->y); 908 break; 909 case 'f': 910 case 'H': 911 /* move cursor to row, column */ 912 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1); 913 break; 914 case 'J': 915 switch (s->esc_params[0]) { 916 case 0: 917 /* clear to end of screen */ 918 for (y = s->y; y < s->height; y++) { 919 for (x = 0; x < s->width; x++) { 920 if (y == s->y && x < s->x) { 921 continue; 922 } 923 console_clear_xy(s, x, y); 924 } 925 } 926 break; 927 case 1: 928 /* clear from beginning of screen */ 929 for (y = 0; y <= s->y; y++) { 930 for (x = 0; x < s->width; x++) { 931 if (y == s->y && x > s->x) { 932 break; 933 } 934 console_clear_xy(s, x, y); 935 } 936 } 937 break; 938 case 2: 939 /* clear entire screen */ 940 for (y = 0; y <= s->height; y++) { 941 for (x = 0; x < s->width; x++) { 942 console_clear_xy(s, x, y); 943 } 944 } 945 break; 946 } 947 break; 948 case 'K': 949 switch (s->esc_params[0]) { 950 case 0: 951 /* clear to eol */ 952 for(x = s->x; x < s->width; x++) { 953 console_clear_xy(s, x, s->y); 954 } 955 break; 956 case 1: 957 /* clear from beginning of line */ 958 for (x = 0; x <= s->x; x++) { 959 console_clear_xy(s, x, s->y); 960 } 961 break; 962 case 2: 963 /* clear entire line */ 964 for(x = 0; x < s->width; x++) { 965 console_clear_xy(s, x, s->y); 966 } 967 break; 968 } 969 break; 970 case 'm': 971 console_handle_escape(s); 972 break; 973 case 'n': 974 switch (s->esc_params[0]) { 975 case 5: 976 /* report console status (always succeed)*/ 977 console_respond_str(s, "\033[0n"); 978 break; 979 case 6: 980 /* report cursor position */ 981 sprintf(response, "\033[%d;%dR", 982 (s->y_base + s->y) % s->total_height + 1, 983 s->x + 1); 984 console_respond_str(s, response); 985 break; 986 } 987 break; 988 case 's': 989 /* save cursor position */ 990 s->x_saved = s->x; 991 s->y_saved = s->y; 992 break; 993 case 'u': 994 /* restore cursor position */ 995 s->x = s->x_saved; 996 s->y = s->y_saved; 997 break; 998 default: 999 trace_console_putchar_unhandled(ch); 1000 break; 1001 } 1002 break; 1003 } 1004 } 1005 } 1006 1007 void console_select(unsigned int index) 1008 { 1009 DisplayChangeListener *dcl; 1010 QemuConsole *s; 1011 1012 trace_console_select(index); 1013 s = qemu_console_lookup_by_index(index); 1014 if (s) { 1015 DisplayState *ds = s->ds; 1016 1017 active_console = s; 1018 if (ds->have_gfx) { 1019 QLIST_FOREACH(dcl, &ds->listeners, next) { 1020 if (dcl->con != NULL) { 1021 continue; 1022 } 1023 if (dcl->ops->dpy_gfx_switch) { 1024 dcl->ops->dpy_gfx_switch(dcl, s->surface); 1025 } 1026 } 1027 dpy_gfx_update(s, 0, 0, surface_width(s->surface), 1028 surface_height(s->surface)); 1029 } 1030 if (ds->have_text) { 1031 dpy_text_resize(s, s->width, s->height); 1032 } 1033 text_console_update_cursor(NULL); 1034 } 1035 } 1036 1037 static int console_puts(CharDriverState *chr, const uint8_t *buf, int len) 1038 { 1039 QemuConsole *s = chr->opaque; 1040 int i; 1041 1042 s->update_x0 = s->width * FONT_WIDTH; 1043 s->update_y0 = s->height * FONT_HEIGHT; 1044 s->update_x1 = 0; 1045 s->update_y1 = 0; 1046 console_show_cursor(s, 0); 1047 for(i = 0; i < len; i++) { 1048 console_putchar(s, buf[i]); 1049 } 1050 console_show_cursor(s, 1); 1051 if (s->ds->have_gfx && s->update_x0 < s->update_x1) { 1052 dpy_gfx_update(s, s->update_x0, s->update_y0, 1053 s->update_x1 - s->update_x0, 1054 s->update_y1 - s->update_y0); 1055 } 1056 return len; 1057 } 1058 1059 static void kbd_send_chars(void *opaque) 1060 { 1061 QemuConsole *s = opaque; 1062 int len; 1063 uint8_t buf[16]; 1064 1065 len = qemu_chr_be_can_write(s->chr); 1066 if (len > s->out_fifo.count) 1067 len = s->out_fifo.count; 1068 if (len > 0) { 1069 if (len > sizeof(buf)) 1070 len = sizeof(buf); 1071 qemu_fifo_read(&s->out_fifo, buf, len); 1072 qemu_chr_be_write(s->chr, buf, len); 1073 } 1074 /* characters are pending: we send them a bit later (XXX: 1075 horrible, should change char device API) */ 1076 if (s->out_fifo.count > 0) { 1077 timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1); 1078 } 1079 } 1080 1081 /* called when an ascii key is pressed */ 1082 void kbd_put_keysym_console(QemuConsole *s, int keysym) 1083 { 1084 uint8_t buf[16], *q; 1085 int c; 1086 1087 if (!s || (s->console_type == GRAPHIC_CONSOLE)) 1088 return; 1089 1090 switch(keysym) { 1091 case QEMU_KEY_CTRL_UP: 1092 console_scroll(s, -1); 1093 break; 1094 case QEMU_KEY_CTRL_DOWN: 1095 console_scroll(s, 1); 1096 break; 1097 case QEMU_KEY_CTRL_PAGEUP: 1098 console_scroll(s, -10); 1099 break; 1100 case QEMU_KEY_CTRL_PAGEDOWN: 1101 console_scroll(s, 10); 1102 break; 1103 default: 1104 /* convert the QEMU keysym to VT100 key string */ 1105 q = buf; 1106 if (keysym >= 0xe100 && keysym <= 0xe11f) { 1107 *q++ = '\033'; 1108 *q++ = '['; 1109 c = keysym - 0xe100; 1110 if (c >= 10) 1111 *q++ = '0' + (c / 10); 1112 *q++ = '0' + (c % 10); 1113 *q++ = '~'; 1114 } else if (keysym >= 0xe120 && keysym <= 0xe17f) { 1115 *q++ = '\033'; 1116 *q++ = '['; 1117 *q++ = keysym & 0xff; 1118 } else if (s->echo && (keysym == '\r' || keysym == '\n')) { 1119 console_puts(s->chr, (const uint8_t *) "\r", 1); 1120 *q++ = '\n'; 1121 } else { 1122 *q++ = keysym; 1123 } 1124 if (s->echo) { 1125 console_puts(s->chr, buf, q - buf); 1126 } 1127 if (s->chr->chr_read) { 1128 qemu_fifo_write(&s->out_fifo, buf, q - buf); 1129 kbd_send_chars(s); 1130 } 1131 break; 1132 } 1133 } 1134 1135 static const int qcode_to_keysym[Q_KEY_CODE__MAX] = { 1136 [Q_KEY_CODE_UP] = QEMU_KEY_UP, 1137 [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN, 1138 [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT, 1139 [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT, 1140 [Q_KEY_CODE_HOME] = QEMU_KEY_HOME, 1141 [Q_KEY_CODE_END] = QEMU_KEY_END, 1142 [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP, 1143 [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN, 1144 [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE, 1145 }; 1146 1147 bool kbd_put_qcode_console(QemuConsole *s, int qcode) 1148 { 1149 int keysym; 1150 1151 keysym = qcode_to_keysym[qcode]; 1152 if (keysym == 0) { 1153 return false; 1154 } 1155 kbd_put_keysym_console(s, keysym); 1156 return true; 1157 } 1158 1159 void kbd_put_string_console(QemuConsole *s, const char *str, int len) 1160 { 1161 int i; 1162 1163 for (i = 0; i < len && str[i]; i++) { 1164 kbd_put_keysym_console(s, str[i]); 1165 } 1166 } 1167 1168 void kbd_put_keysym(int keysym) 1169 { 1170 kbd_put_keysym_console(active_console, keysym); 1171 } 1172 1173 static void text_console_invalidate(void *opaque) 1174 { 1175 QemuConsole *s = (QemuConsole *) opaque; 1176 1177 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) { 1178 text_console_resize(s); 1179 } 1180 console_refresh(s); 1181 } 1182 1183 static void text_console_update(void *opaque, console_ch_t *chardata) 1184 { 1185 QemuConsole *s = (QemuConsole *) opaque; 1186 int i, j, src; 1187 1188 if (s->text_x[0] <= s->text_x[1]) { 1189 src = (s->y_base + s->text_y[0]) * s->width; 1190 chardata += s->text_y[0] * s->width; 1191 for (i = s->text_y[0]; i <= s->text_y[1]; i ++) 1192 for (j = 0; j < s->width; j++, src++) { 1193 console_write_ch(chardata ++, 1194 ATTR2CHTYPE(s->cells[src].ch, 1195 s->cells[src].t_attrib.fgcol, 1196 s->cells[src].t_attrib.bgcol, 1197 s->cells[src].t_attrib.bold)); 1198 } 1199 dpy_text_update(s, s->text_x[0], s->text_y[0], 1200 s->text_x[1] - s->text_x[0], i - s->text_y[0]); 1201 s->text_x[0] = s->width; 1202 s->text_y[0] = s->height; 1203 s->text_x[1] = 0; 1204 s->text_y[1] = 0; 1205 } 1206 if (s->cursor_invalidate) { 1207 dpy_text_cursor(s, s->x, s->y); 1208 s->cursor_invalidate = 0; 1209 } 1210 } 1211 1212 static QemuConsole *new_console(DisplayState *ds, console_type_t console_type, 1213 uint32_t head) 1214 { 1215 Object *obj; 1216 QemuConsole *s; 1217 int i; 1218 1219 obj = object_new(TYPE_QEMU_CONSOLE); 1220 s = QEMU_CONSOLE(obj); 1221 s->head = head; 1222 object_property_add_link(obj, "device", TYPE_DEVICE, 1223 (Object **)&s->device, 1224 object_property_allow_set_link, 1225 OBJ_PROP_LINK_UNREF_ON_RELEASE, 1226 &error_abort); 1227 object_property_add_uint32_ptr(obj, "head", 1228 &s->head, &error_abort); 1229 1230 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) && 1231 (console_type == GRAPHIC_CONSOLE))) { 1232 active_console = s; 1233 } 1234 s->ds = ds; 1235 s->console_type = console_type; 1236 1237 consoles = g_realloc(consoles, sizeof(*consoles) * (nb_consoles+1)); 1238 if (console_type != GRAPHIC_CONSOLE) { 1239 s->index = nb_consoles; 1240 consoles[nb_consoles++] = s; 1241 } else { 1242 /* HACK: Put graphical consoles before text consoles. */ 1243 for (i = nb_consoles; i > 0; i--) { 1244 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE) 1245 break; 1246 consoles[i] = consoles[i - 1]; 1247 consoles[i]->index = i; 1248 } 1249 s->index = i; 1250 consoles[i] = s; 1251 nb_consoles++; 1252 } 1253 return s; 1254 } 1255 1256 static void qemu_alloc_display(DisplaySurface *surface, int width, int height) 1257 { 1258 qemu_pixman_image_unref(surface->image); 1259 surface->image = NULL; 1260 1261 surface->format = PIXMAN_x8r8g8b8; 1262 surface->image = pixman_image_create_bits(surface->format, 1263 width, height, 1264 NULL, width * 4); 1265 assert(surface->image != NULL); 1266 1267 surface->flags = QEMU_ALLOCATED_FLAG; 1268 } 1269 1270 DisplaySurface *qemu_create_displaysurface(int width, int height) 1271 { 1272 DisplaySurface *surface = g_new0(DisplaySurface, 1); 1273 1274 trace_displaysurface_create(surface, width, height); 1275 qemu_alloc_display(surface, width, height); 1276 return surface; 1277 } 1278 1279 DisplaySurface *qemu_create_displaysurface_from(int width, int height, 1280 pixman_format_code_t format, 1281 int linesize, uint8_t *data) 1282 { 1283 DisplaySurface *surface = g_new0(DisplaySurface, 1); 1284 1285 trace_displaysurface_create_from(surface, width, height, format); 1286 surface->format = format; 1287 surface->image = pixman_image_create_bits(surface->format, 1288 width, height, 1289 (void *)data, linesize); 1290 assert(surface->image != NULL); 1291 1292 return surface; 1293 } 1294 1295 DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image) 1296 { 1297 DisplaySurface *surface = g_new0(DisplaySurface, 1); 1298 1299 trace_displaysurface_create_pixman(surface); 1300 surface->format = pixman_image_get_format(image); 1301 surface->image = pixman_image_ref(image); 1302 1303 return surface; 1304 } 1305 1306 static void qemu_unmap_displaysurface_guestmem(pixman_image_t *image, 1307 void *unused) 1308 { 1309 void *data = pixman_image_get_data(image); 1310 uint32_t size = pixman_image_get_stride(image) * 1311 pixman_image_get_height(image); 1312 cpu_physical_memory_unmap(data, size, 0, 0); 1313 } 1314 1315 DisplaySurface *qemu_create_displaysurface_guestmem(int width, int height, 1316 pixman_format_code_t format, 1317 int linesize, uint64_t addr) 1318 { 1319 DisplaySurface *surface; 1320 hwaddr size; 1321 void *data; 1322 1323 if (linesize == 0) { 1324 linesize = width * PIXMAN_FORMAT_BPP(format) / 8; 1325 } 1326 1327 size = (hwaddr)linesize * height; 1328 data = cpu_physical_memory_map(addr, &size, 0); 1329 if (size != (hwaddr)linesize * height) { 1330 cpu_physical_memory_unmap(data, size, 0, 0); 1331 return NULL; 1332 } 1333 1334 surface = qemu_create_displaysurface_from 1335 (width, height, format, linesize, data); 1336 pixman_image_set_destroy_function 1337 (surface->image, qemu_unmap_displaysurface_guestmem, NULL); 1338 1339 return surface; 1340 } 1341 1342 static DisplaySurface *qemu_create_message_surface(int w, int h, 1343 const char *msg) 1344 { 1345 DisplaySurface *surface = qemu_create_displaysurface(w, h); 1346 pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK]; 1347 pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE]; 1348 pixman_image_t *glyph; 1349 int len, x, y, i; 1350 1351 len = strlen(msg); 1352 x = (w / FONT_WIDTH - len) / 2; 1353 y = (h / FONT_HEIGHT - 1) / 2; 1354 for (i = 0; i < len; i++) { 1355 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]); 1356 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg, 1357 x+i, y, FONT_WIDTH, FONT_HEIGHT); 1358 qemu_pixman_image_unref(glyph); 1359 } 1360 return surface; 1361 } 1362 1363 void qemu_free_displaysurface(DisplaySurface *surface) 1364 { 1365 if (surface == NULL) { 1366 return; 1367 } 1368 trace_displaysurface_free(surface); 1369 qemu_pixman_image_unref(surface->image); 1370 g_free(surface); 1371 } 1372 1373 bool console_has_gl(QemuConsole *con) 1374 { 1375 return con->gl != NULL; 1376 } 1377 1378 void register_displaychangelistener(DisplayChangeListener *dcl) 1379 { 1380 static const char nodev[] = 1381 "This VM has no graphic display device."; 1382 static DisplaySurface *dummy; 1383 QemuConsole *con; 1384 1385 if (dcl->ops->dpy_gl_ctx_create) { 1386 /* display has opengl support */ 1387 assert(dcl->con); 1388 if (dcl->con->gl) { 1389 fprintf(stderr, "can't register two opengl displays (%s, %s)\n", 1390 dcl->ops->dpy_name, dcl->con->gl->ops->dpy_name); 1391 exit(1); 1392 } 1393 dcl->con->gl = dcl; 1394 } 1395 1396 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name); 1397 dcl->ds = get_alloc_displaystate(); 1398 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next); 1399 gui_setup_refresh(dcl->ds); 1400 if (dcl->con) { 1401 dcl->con->dcls++; 1402 con = dcl->con; 1403 } else { 1404 con = active_console; 1405 } 1406 if (dcl->ops->dpy_gfx_switch) { 1407 if (con) { 1408 dcl->ops->dpy_gfx_switch(dcl, con->surface); 1409 } else { 1410 if (!dummy) { 1411 dummy = qemu_create_message_surface(640, 480, nodev); 1412 } 1413 dcl->ops->dpy_gfx_switch(dcl, dummy); 1414 } 1415 } 1416 text_console_update_cursor(NULL); 1417 } 1418 1419 void update_displaychangelistener(DisplayChangeListener *dcl, 1420 uint64_t interval) 1421 { 1422 DisplayState *ds = dcl->ds; 1423 1424 dcl->update_interval = interval; 1425 if (!ds->refreshing && ds->update_interval > interval) { 1426 timer_mod(ds->gui_timer, ds->last_update + interval); 1427 } 1428 } 1429 1430 void unregister_displaychangelistener(DisplayChangeListener *dcl) 1431 { 1432 DisplayState *ds = dcl->ds; 1433 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name); 1434 if (dcl->con) { 1435 dcl->con->dcls--; 1436 } 1437 QLIST_REMOVE(dcl, next); 1438 gui_setup_refresh(ds); 1439 } 1440 1441 static void dpy_set_ui_info_timer(void *opaque) 1442 { 1443 QemuConsole *con = opaque; 1444 1445 con->hw_ops->ui_info(con->hw, con->head, &con->ui_info); 1446 } 1447 1448 bool dpy_ui_info_supported(QemuConsole *con) 1449 { 1450 return con->hw_ops->ui_info != NULL; 1451 } 1452 1453 int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info) 1454 { 1455 assert(con != NULL); 1456 1457 if (!dpy_ui_info_supported(con)) { 1458 return -1; 1459 } 1460 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) { 1461 /* nothing changed -- ignore */ 1462 return 0; 1463 } 1464 1465 /* 1466 * Typically we get a flood of these as the user resizes the window. 1467 * Wait until the dust has settled (one second without updates), then 1468 * go notify the guest. 1469 */ 1470 con->ui_info = *info; 1471 timer_mod(con->ui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000); 1472 return 0; 1473 } 1474 1475 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h) 1476 { 1477 DisplayState *s = con->ds; 1478 DisplayChangeListener *dcl; 1479 int width = w; 1480 int height = h; 1481 1482 if (con->surface) { 1483 width = surface_width(con->surface); 1484 height = surface_height(con->surface); 1485 } 1486 x = MAX(x, 0); 1487 y = MAX(y, 0); 1488 x = MIN(x, width); 1489 y = MIN(y, height); 1490 w = MIN(w, width - x); 1491 h = MIN(h, height - y); 1492 1493 if (!qemu_console_is_visible(con)) { 1494 return; 1495 } 1496 QLIST_FOREACH(dcl, &s->listeners, next) { 1497 if (con != (dcl->con ? dcl->con : active_console)) { 1498 continue; 1499 } 1500 if (dcl->ops->dpy_gfx_update) { 1501 dcl->ops->dpy_gfx_update(dcl, x, y, w, h); 1502 } 1503 } 1504 } 1505 1506 void dpy_gfx_replace_surface(QemuConsole *con, 1507 DisplaySurface *surface) 1508 { 1509 DisplayState *s = con->ds; 1510 DisplaySurface *old_surface = con->surface; 1511 DisplayChangeListener *dcl; 1512 1513 con->surface = surface; 1514 QLIST_FOREACH(dcl, &s->listeners, next) { 1515 if (con != (dcl->con ? dcl->con : active_console)) { 1516 continue; 1517 } 1518 if (dcl->ops->dpy_gfx_switch) { 1519 dcl->ops->dpy_gfx_switch(dcl, surface); 1520 } 1521 } 1522 qemu_free_displaysurface(old_surface); 1523 } 1524 1525 bool dpy_gfx_check_format(QemuConsole *con, 1526 pixman_format_code_t format) 1527 { 1528 DisplayChangeListener *dcl; 1529 DisplayState *s = con->ds; 1530 1531 QLIST_FOREACH(dcl, &s->listeners, next) { 1532 if (dcl->con && dcl->con != con) { 1533 /* dcl bound to another console -> skip */ 1534 continue; 1535 } 1536 if (dcl->ops->dpy_gfx_check_format) { 1537 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) { 1538 return false; 1539 } 1540 } else { 1541 /* default is to whitelist native 32 bpp only */ 1542 if (format != qemu_default_pixman_format(32, true)) { 1543 return false; 1544 } 1545 } 1546 } 1547 return true; 1548 } 1549 1550 static void dpy_refresh(DisplayState *s) 1551 { 1552 DisplayChangeListener *dcl; 1553 1554 QLIST_FOREACH(dcl, &s->listeners, next) { 1555 if (dcl->ops->dpy_refresh) { 1556 dcl->ops->dpy_refresh(dcl); 1557 } 1558 } 1559 } 1560 1561 void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y, 1562 int dst_x, int dst_y, int w, int h) 1563 { 1564 DisplayState *s = con->ds; 1565 DisplayChangeListener *dcl; 1566 1567 if (!qemu_console_is_visible(con)) { 1568 return; 1569 } 1570 QLIST_FOREACH(dcl, &s->listeners, next) { 1571 if (con != (dcl->con ? dcl->con : active_console)) { 1572 continue; 1573 } 1574 if (dcl->ops->dpy_gfx_copy) { 1575 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h); 1576 } else { /* TODO */ 1577 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h); 1578 } 1579 } 1580 } 1581 1582 void dpy_text_cursor(QemuConsole *con, int x, int y) 1583 { 1584 DisplayState *s = con->ds; 1585 DisplayChangeListener *dcl; 1586 1587 if (!qemu_console_is_visible(con)) { 1588 return; 1589 } 1590 QLIST_FOREACH(dcl, &s->listeners, next) { 1591 if (con != (dcl->con ? dcl->con : active_console)) { 1592 continue; 1593 } 1594 if (dcl->ops->dpy_text_cursor) { 1595 dcl->ops->dpy_text_cursor(dcl, x, y); 1596 } 1597 } 1598 } 1599 1600 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h) 1601 { 1602 DisplayState *s = con->ds; 1603 DisplayChangeListener *dcl; 1604 1605 if (!qemu_console_is_visible(con)) { 1606 return; 1607 } 1608 QLIST_FOREACH(dcl, &s->listeners, next) { 1609 if (con != (dcl->con ? dcl->con : active_console)) { 1610 continue; 1611 } 1612 if (dcl->ops->dpy_text_update) { 1613 dcl->ops->dpy_text_update(dcl, x, y, w, h); 1614 } 1615 } 1616 } 1617 1618 void dpy_text_resize(QemuConsole *con, int w, int h) 1619 { 1620 DisplayState *s = con->ds; 1621 DisplayChangeListener *dcl; 1622 1623 if (!qemu_console_is_visible(con)) { 1624 return; 1625 } 1626 QLIST_FOREACH(dcl, &s->listeners, next) { 1627 if (con != (dcl->con ? dcl->con : active_console)) { 1628 continue; 1629 } 1630 if (dcl->ops->dpy_text_resize) { 1631 dcl->ops->dpy_text_resize(dcl, w, h); 1632 } 1633 } 1634 } 1635 1636 void dpy_mouse_set(QemuConsole *con, int x, int y, int on) 1637 { 1638 DisplayState *s = con->ds; 1639 DisplayChangeListener *dcl; 1640 1641 if (!qemu_console_is_visible(con)) { 1642 return; 1643 } 1644 QLIST_FOREACH(dcl, &s->listeners, next) { 1645 if (con != (dcl->con ? dcl->con : active_console)) { 1646 continue; 1647 } 1648 if (dcl->ops->dpy_mouse_set) { 1649 dcl->ops->dpy_mouse_set(dcl, x, y, on); 1650 } 1651 } 1652 } 1653 1654 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor) 1655 { 1656 DisplayState *s = con->ds; 1657 DisplayChangeListener *dcl; 1658 1659 if (!qemu_console_is_visible(con)) { 1660 return; 1661 } 1662 QLIST_FOREACH(dcl, &s->listeners, next) { 1663 if (con != (dcl->con ? dcl->con : active_console)) { 1664 continue; 1665 } 1666 if (dcl->ops->dpy_cursor_define) { 1667 dcl->ops->dpy_cursor_define(dcl, cursor); 1668 } 1669 } 1670 } 1671 1672 bool dpy_cursor_define_supported(QemuConsole *con) 1673 { 1674 DisplayState *s = con->ds; 1675 DisplayChangeListener *dcl; 1676 1677 QLIST_FOREACH(dcl, &s->listeners, next) { 1678 if (dcl->ops->dpy_cursor_define) { 1679 return true; 1680 } 1681 } 1682 return false; 1683 } 1684 1685 QEMUGLContext dpy_gl_ctx_create(QemuConsole *con, 1686 struct QEMUGLParams *qparams) 1687 { 1688 assert(con->gl); 1689 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams); 1690 } 1691 1692 void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx) 1693 { 1694 assert(con->gl); 1695 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx); 1696 } 1697 1698 int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx) 1699 { 1700 assert(con->gl); 1701 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx); 1702 } 1703 1704 QEMUGLContext dpy_gl_ctx_get_current(QemuConsole *con) 1705 { 1706 assert(con->gl); 1707 return con->gl->ops->dpy_gl_ctx_get_current(con->gl); 1708 } 1709 1710 void dpy_gl_scanout(QemuConsole *con, 1711 uint32_t backing_id, bool backing_y_0_top, 1712 uint32_t x, uint32_t y, uint32_t width, uint32_t height) 1713 { 1714 assert(con->gl); 1715 con->gl->ops->dpy_gl_scanout(con->gl, backing_id, 1716 backing_y_0_top, 1717 x, y, width, height); 1718 } 1719 1720 void dpy_gl_update(QemuConsole *con, 1721 uint32_t x, uint32_t y, uint32_t w, uint32_t h) 1722 { 1723 assert(con->gl); 1724 con->gl->ops->dpy_gl_update(con->gl, x, y, w, h); 1725 } 1726 1727 /***********************************************************/ 1728 /* register display */ 1729 1730 /* console.c internal use only */ 1731 static DisplayState *get_alloc_displaystate(void) 1732 { 1733 if (!display_state) { 1734 display_state = g_new0(DisplayState, 1); 1735 cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME, 1736 text_console_update_cursor, NULL); 1737 } 1738 return display_state; 1739 } 1740 1741 /* 1742 * Called by main(), after creating QemuConsoles 1743 * and before initializing ui (sdl/vnc/...). 1744 */ 1745 DisplayState *init_displaystate(void) 1746 { 1747 gchar *name; 1748 int i; 1749 1750 get_alloc_displaystate(); 1751 for (i = 0; i < nb_consoles; i++) { 1752 if (consoles[i]->console_type != GRAPHIC_CONSOLE && 1753 consoles[i]->ds == NULL) { 1754 text_console_do_init(consoles[i]->chr, display_state); 1755 } 1756 1757 /* Hook up into the qom tree here (not in new_console()), once 1758 * all QemuConsoles are created and the order / numbering 1759 * doesn't change any more */ 1760 name = g_strdup_printf("console[%d]", i); 1761 object_property_add_child(container_get(object_get_root(), "/backend"), 1762 name, OBJECT(consoles[i]), &error_abort); 1763 g_free(name); 1764 } 1765 1766 return display_state; 1767 } 1768 1769 void graphic_console_set_hwops(QemuConsole *con, 1770 const GraphicHwOps *hw_ops, 1771 void *opaque) 1772 { 1773 con->hw_ops = hw_ops; 1774 con->hw = opaque; 1775 } 1776 1777 QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head, 1778 const GraphicHwOps *hw_ops, 1779 void *opaque) 1780 { 1781 static const char noinit[] = 1782 "Guest has not initialized the display (yet)."; 1783 int width = 640; 1784 int height = 480; 1785 QemuConsole *s; 1786 DisplayState *ds; 1787 1788 ds = get_alloc_displaystate(); 1789 trace_console_gfx_new(); 1790 s = new_console(ds, GRAPHIC_CONSOLE, head); 1791 s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, dpy_set_ui_info_timer, s); 1792 graphic_console_set_hwops(s, hw_ops, opaque); 1793 if (dev) { 1794 object_property_set_link(OBJECT(s), OBJECT(dev), "device", 1795 &error_abort); 1796 } 1797 1798 s->surface = qemu_create_message_surface(width, height, noinit); 1799 return s; 1800 } 1801 1802 QemuConsole *qemu_console_lookup_by_index(unsigned int index) 1803 { 1804 if (index >= nb_consoles) { 1805 return NULL; 1806 } 1807 return consoles[index]; 1808 } 1809 1810 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head) 1811 { 1812 Object *obj; 1813 uint32_t h; 1814 int i; 1815 1816 for (i = 0; i < nb_consoles; i++) { 1817 if (!consoles[i]) { 1818 continue; 1819 } 1820 obj = object_property_get_link(OBJECT(consoles[i]), 1821 "device", &error_abort); 1822 if (DEVICE(obj) != dev) { 1823 continue; 1824 } 1825 h = object_property_get_int(OBJECT(consoles[i]), 1826 "head", &error_abort); 1827 if (h != head) { 1828 continue; 1829 } 1830 return consoles[i]; 1831 } 1832 return NULL; 1833 } 1834 1835 QemuConsole *qemu_console_lookup_by_device_name(const char *device_id, 1836 uint32_t head, Error **errp) 1837 { 1838 DeviceState *dev; 1839 QemuConsole *con; 1840 1841 dev = qdev_find_recursive(sysbus_get_default(), device_id); 1842 if (dev == NULL) { 1843 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 1844 "Device '%s' not found", device_id); 1845 return NULL; 1846 } 1847 1848 con = qemu_console_lookup_by_device(dev, head); 1849 if (con == NULL) { 1850 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole", 1851 device_id, head); 1852 return NULL; 1853 } 1854 1855 return con; 1856 } 1857 1858 bool qemu_console_is_visible(QemuConsole *con) 1859 { 1860 return (con == active_console) || (con->dcls > 0); 1861 } 1862 1863 bool qemu_console_is_graphic(QemuConsole *con) 1864 { 1865 if (con == NULL) { 1866 con = active_console; 1867 } 1868 return con && (con->console_type == GRAPHIC_CONSOLE); 1869 } 1870 1871 bool qemu_console_is_fixedsize(QemuConsole *con) 1872 { 1873 if (con == NULL) { 1874 con = active_console; 1875 } 1876 return con && (con->console_type != TEXT_CONSOLE); 1877 } 1878 1879 char *qemu_console_get_label(QemuConsole *con) 1880 { 1881 if (con->console_type == GRAPHIC_CONSOLE) { 1882 if (con->device) { 1883 return g_strdup(object_get_typename(con->device)); 1884 } 1885 return g_strdup("VGA"); 1886 } else { 1887 if (con->chr && con->chr->label) { 1888 return g_strdup(con->chr->label); 1889 } 1890 return g_strdup_printf("vc%d", con->index); 1891 } 1892 } 1893 1894 int qemu_console_get_index(QemuConsole *con) 1895 { 1896 if (con == NULL) { 1897 con = active_console; 1898 } 1899 return con ? con->index : -1; 1900 } 1901 1902 uint32_t qemu_console_get_head(QemuConsole *con) 1903 { 1904 if (con == NULL) { 1905 con = active_console; 1906 } 1907 return con ? con->head : -1; 1908 } 1909 1910 QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con) 1911 { 1912 assert(con != NULL); 1913 return &con->ui_info; 1914 } 1915 1916 int qemu_console_get_width(QemuConsole *con, int fallback) 1917 { 1918 if (con == NULL) { 1919 con = active_console; 1920 } 1921 return con ? surface_width(con->surface) : fallback; 1922 } 1923 1924 int qemu_console_get_height(QemuConsole *con, int fallback) 1925 { 1926 if (con == NULL) { 1927 con = active_console; 1928 } 1929 return con ? surface_height(con->surface) : fallback; 1930 } 1931 1932 static void text_console_set_echo(CharDriverState *chr, bool echo) 1933 { 1934 QemuConsole *s = chr->opaque; 1935 1936 s->echo = echo; 1937 } 1938 1939 static void text_console_update_cursor_timer(void) 1940 { 1941 timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) 1942 + CONSOLE_CURSOR_PERIOD / 2); 1943 } 1944 1945 static void text_console_update_cursor(void *opaque) 1946 { 1947 QemuConsole *s; 1948 int i, count = 0; 1949 1950 cursor_visible_phase = !cursor_visible_phase; 1951 1952 for (i = 0; i < nb_consoles; i++) { 1953 s = consoles[i]; 1954 if (qemu_console_is_graphic(s) || 1955 !qemu_console_is_visible(s)) { 1956 continue; 1957 } 1958 count++; 1959 graphic_hw_invalidate(s); 1960 } 1961 1962 if (count) { 1963 text_console_update_cursor_timer(); 1964 } 1965 } 1966 1967 static const GraphicHwOps text_console_ops = { 1968 .invalidate = text_console_invalidate, 1969 .text_update = text_console_update, 1970 }; 1971 1972 static void text_console_do_init(CharDriverState *chr, DisplayState *ds) 1973 { 1974 QemuConsole *s; 1975 int g_width = 80 * FONT_WIDTH; 1976 int g_height = 24 * FONT_HEIGHT; 1977 1978 s = chr->opaque; 1979 1980 chr->chr_write = console_puts; 1981 1982 s->out_fifo.buf = s->out_fifo_buf; 1983 s->out_fifo.buf_size = sizeof(s->out_fifo_buf); 1984 s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s); 1985 s->ds = ds; 1986 1987 s->y_displayed = 0; 1988 s->y_base = 0; 1989 s->total_height = DEFAULT_BACKSCROLL; 1990 s->x = 0; 1991 s->y = 0; 1992 if (!s->surface) { 1993 if (active_console && active_console->surface) { 1994 g_width = surface_width(active_console->surface); 1995 g_height = surface_height(active_console->surface); 1996 } 1997 s->surface = qemu_create_displaysurface(g_width, g_height); 1998 } 1999 2000 s->hw_ops = &text_console_ops; 2001 s->hw = s; 2002 2003 /* Set text attribute defaults */ 2004 s->t_attrib_default.bold = 0; 2005 s->t_attrib_default.uline = 0; 2006 s->t_attrib_default.blink = 0; 2007 s->t_attrib_default.invers = 0; 2008 s->t_attrib_default.unvisible = 0; 2009 s->t_attrib_default.fgcol = QEMU_COLOR_WHITE; 2010 s->t_attrib_default.bgcol = QEMU_COLOR_BLACK; 2011 /* set current text attributes to default */ 2012 s->t_attrib = s->t_attrib_default; 2013 text_console_resize(s); 2014 2015 if (chr->label) { 2016 char msg[128]; 2017 int len; 2018 2019 s->t_attrib.bgcol = QEMU_COLOR_BLUE; 2020 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label); 2021 console_puts(chr, (uint8_t*)msg, len); 2022 s->t_attrib = s->t_attrib_default; 2023 } 2024 2025 qemu_chr_be_generic_open(chr); 2026 if (chr->init) 2027 chr->init(chr); 2028 } 2029 2030 static CharDriverState *text_console_init(ChardevVC *vc, Error **errp) 2031 { 2032 ChardevCommon *common = qapi_ChardevVC_base(vc); 2033 CharDriverState *chr; 2034 QemuConsole *s; 2035 unsigned width = 0; 2036 unsigned height = 0; 2037 2038 chr = qemu_chr_alloc(common, errp); 2039 if (!chr) { 2040 return NULL; 2041 } 2042 2043 if (vc->has_width) { 2044 width = vc->width; 2045 } else if (vc->has_cols) { 2046 width = vc->cols * FONT_WIDTH; 2047 } 2048 2049 if (vc->has_height) { 2050 height = vc->height; 2051 } else if (vc->has_rows) { 2052 height = vc->rows * FONT_HEIGHT; 2053 } 2054 2055 trace_console_txt_new(width, height); 2056 if (width == 0 || height == 0) { 2057 s = new_console(NULL, TEXT_CONSOLE, 0); 2058 } else { 2059 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0); 2060 s->surface = qemu_create_displaysurface(width, height); 2061 } 2062 2063 if (!s) { 2064 g_free(chr); 2065 error_setg(errp, "cannot create text console"); 2066 return NULL; 2067 } 2068 2069 s->chr = chr; 2070 chr->opaque = s; 2071 chr->chr_set_echo = text_console_set_echo; 2072 /* console/chardev init sometimes completes elsewhere in a 2nd 2073 * stage, so defer OPENED events until they are fully initialized 2074 */ 2075 chr->explicit_be_open = true; 2076 2077 if (display_state) { 2078 text_console_do_init(chr, display_state); 2079 } 2080 return chr; 2081 } 2082 2083 static VcHandler *vc_handler = text_console_init; 2084 2085 static CharDriverState *vc_init(const char *id, ChardevBackend *backend, 2086 ChardevReturn *ret, Error **errp) 2087 { 2088 return vc_handler(backend->u.vc.data, errp); 2089 } 2090 2091 void register_vc_handler(VcHandler *handler) 2092 { 2093 vc_handler = handler; 2094 } 2095 2096 void qemu_console_resize(QemuConsole *s, int width, int height) 2097 { 2098 DisplaySurface *surface; 2099 2100 assert(s->console_type == GRAPHIC_CONSOLE); 2101 surface = qemu_create_displaysurface(width, height); 2102 dpy_gfx_replace_surface(s, surface); 2103 } 2104 2105 void qemu_console_copy(QemuConsole *con, int src_x, int src_y, 2106 int dst_x, int dst_y, int w, int h) 2107 { 2108 assert(con->console_type == GRAPHIC_CONSOLE); 2109 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h); 2110 } 2111 2112 DisplaySurface *qemu_console_surface(QemuConsole *console) 2113 { 2114 return console->surface; 2115 } 2116 2117 PixelFormat qemu_default_pixelformat(int bpp) 2118 { 2119 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true); 2120 PixelFormat pf = qemu_pixelformat_from_pixman(fmt); 2121 return pf; 2122 } 2123 2124 static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, 2125 Error **errp) 2126 { 2127 int val; 2128 ChardevVC *vc; 2129 2130 vc = backend->u.vc.data = g_new0(ChardevVC, 1); 2131 qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc)); 2132 2133 val = qemu_opt_get_number(opts, "width", 0); 2134 if (val != 0) { 2135 vc->has_width = true; 2136 vc->width = val; 2137 } 2138 2139 val = qemu_opt_get_number(opts, "height", 0); 2140 if (val != 0) { 2141 vc->has_height = true; 2142 vc->height = val; 2143 } 2144 2145 val = qemu_opt_get_number(opts, "cols", 0); 2146 if (val != 0) { 2147 vc->has_cols = true; 2148 vc->cols = val; 2149 } 2150 2151 val = qemu_opt_get_number(opts, "rows", 0); 2152 if (val != 0) { 2153 vc->has_rows = true; 2154 vc->rows = val; 2155 } 2156 } 2157 2158 static const TypeInfo qemu_console_info = { 2159 .name = TYPE_QEMU_CONSOLE, 2160 .parent = TYPE_OBJECT, 2161 .instance_size = sizeof(QemuConsole), 2162 .class_size = sizeof(QemuConsoleClass), 2163 }; 2164 2165 2166 static void register_types(void) 2167 { 2168 type_register_static(&qemu_console_info); 2169 register_char_driver("vc", CHARDEV_BACKEND_KIND_VC, qemu_chr_parse_vc, 2170 vc_init); 2171 } 2172 2173 type_init(register_types); 2174