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