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