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