blob: 6d2282d3e9072c89ecb198aca7bd00b04d9facca [file] [log] [blame]
bellarde7f0ad52004-07-14 17:28:59 +00001/*
2 * QEMU graphical console
ths5fafdf22007-09-16 21:08:06 +00003 *
bellarde7f0ad52004-07-14 17:28:59 +00004 * Copyright (c) 2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellarde7f0ad52004-07-14 17:28:59 +00006 * 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 */
Markus Armbrustere688df62018-02-01 12:18:31 +010024
Peter Maydelle16f4c82016-01-29 17:49:51 +000025#include "qemu/osdep.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010026#include "ui/console.h"
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +020027#include "hw/qdev-core.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010028#include "qapi/error.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010029#include "qapi/qapi-commands-ui.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010030#include "qemu/option.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010031#include "qemu/timer.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040032#include "chardev/char-fe.h"
Stefan Weilac860482013-11-10 14:20:16 +010033#include "trace.h"
Gerd Hoffmanna77549b2014-06-19 08:46:08 +020034#include "exec/memory.h"
bellarde7f0ad52004-07-14 17:28:59 +000035
36#define DEFAULT_BACKSCROLL 512
Jan Kiszkabf1bed82012-07-10 22:00:55 +020037#define CONSOLE_CURSOR_PERIOD 500
bellarde7f0ad52004-07-14 17:28:59 +000038
pbrook6d6f7c22006-03-11 15:35:30 +000039typedef struct TextAttributes {
40 uint8_t fgcol:4;
41 uint8_t bgcol:4;
42 uint8_t bold:1;
43 uint8_t uline:1;
44 uint8_t blink:1;
45 uint8_t invers:1;
46 uint8_t unvisible:1;
47} TextAttributes;
48
bellarde7f0ad52004-07-14 17:28:59 +000049typedef struct TextCell {
50 uint8_t ch;
pbrook6d6f7c22006-03-11 15:35:30 +000051 TextAttributes t_attrib;
bellarde7f0ad52004-07-14 17:28:59 +000052} TextCell;
53
54#define MAX_ESC_PARAMS 3
55
56enum TTYState {
57 TTY_STATE_NORM,
58 TTY_STATE_ESC,
59 TTY_STATE_CSI,
60};
61
bellarde15d7372006-06-25 16:26:29 +000062typedef struct QEMUFIFO {
63 uint8_t *buf;
64 int buf_size;
65 int count, wptr, rptr;
66} QEMUFIFO;
67
pbrook9596ebb2007-11-18 01:44:38 +000068static int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1)
bellarde15d7372006-06-25 16:26:29 +000069{
70 int l, len;
71
72 l = f->buf_size - f->count;
73 if (len1 > l)
74 len1 = l;
75 len = len1;
76 while (len > 0) {
77 l = f->buf_size - f->wptr;
78 if (l > len)
79 l = len;
80 memcpy(f->buf + f->wptr, buf, l);
81 f->wptr += l;
82 if (f->wptr >= f->buf_size)
83 f->wptr = 0;
84 buf += l;
85 len -= l;
86 }
87 f->count += len1;
88 return len1;
89}
90
pbrook9596ebb2007-11-18 01:44:38 +000091static int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1)
bellarde15d7372006-06-25 16:26:29 +000092{
93 int l, len;
94
95 if (len1 > f->count)
96 len1 = f->count;
97 len = len1;
98 while (len > 0) {
99 l = f->buf_size - f->rptr;
100 if (l > len)
101 l = len;
102 memcpy(buf, f->buf + f->rptr, l);
103 f->rptr += l;
104 if (f->rptr >= f->buf_size)
105 f->rptr = 0;
106 buf += l;
107 len -= l;
108 }
109 f->count -= len1;
110 return len1;
111}
112
thsaf3a9032007-07-11 23:14:59 +0000113typedef enum {
114 GRAPHIC_CONSOLE,
balrogc21bbcf2008-09-24 03:32:33 +0000115 TEXT_CONSOLE,
116 TEXT_CONSOLE_FIXED_SIZE
Anthony Liguoric227f092009-10-01 16:12:16 -0500117} console_type_t;
thsaf3a9032007-07-11 23:14:59 +0000118
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200119struct QemuConsole {
Gerd Hoffmann95be0662013-04-17 09:45:10 +0200120 Object parent;
121
Jan Kiszkaf81bdef2011-09-16 00:48:07 +0200122 int index;
Anthony Liguoric227f092009-10-01 16:12:16 -0500123 console_type_t console_type;
bellarde7f0ad52004-07-14 17:28:59 +0000124 DisplayState *ds;
Gerd Hoffmann321f0482013-03-12 14:39:22 +0100125 DisplaySurface *surface;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +0100126 int dcls;
Gerd Hoffmann06020b92014-07-11 13:56:51 +0200127 DisplayChangeListener *gl;
Gerd Hoffmannf6078672016-09-23 09:50:27 +0200128 bool gl_block;
Samuel Thibaultb3cb21b2016-12-21 01:38:04 +0100129 int window_id;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200130
pbrook95219892006-04-09 01:06:34 +0000131 /* Graphic console state. */
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +0200132 Object *device;
Gerd Hoffmann56437062014-01-24 15:35:21 +0100133 uint32_t head;
Gerd Hoffmann6f90f3d2014-01-24 17:38:20 +0100134 QemuUIInfo ui_info;
Gerd Hoffmanncf1ecc82015-03-12 12:51:13 +0100135 QEMUTimer *ui_timer;
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100136 const GraphicHwOps *hw_ops;
pbrook95219892006-04-09 01:06:34 +0000137 void *hw;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200138
139 /* Text console state */
bellarde7f0ad52004-07-14 17:28:59 +0000140 int width;
141 int height;
142 int total_height;
143 int backscroll_height;
bellarde7f0ad52004-07-14 17:28:59 +0000144 int x, y;
thsadb47962007-01-16 23:02:36 +0000145 int x_saved, y_saved;
bellarde7f0ad52004-07-14 17:28:59 +0000146 int y_displayed;
147 int y_base;
pbrook6d6f7c22006-03-11 15:35:30 +0000148 TextAttributes t_attrib_default; /* default text attributes */
149 TextAttributes t_attrib; /* currently active text attributes */
bellarde7f0ad52004-07-14 17:28:59 +0000150 TextCell *cells;
balrog4d3b6f62008-02-10 16:33:14 +0000151 int text_x[2], text_y[2], cursor_invalidate;
Paolo Bonzini41048332010-12-23 13:42:52 +0100152 int echo;
bellarde7f0ad52004-07-14 17:28:59 +0000153
pbrook14778c22009-01-21 03:02:52 +0000154 int update_x0;
155 int update_y0;
156 int update_x1;
157 int update_y1;
158
bellarde7f0ad52004-07-14 17:28:59 +0000159 enum TTYState state;
160 int esc_params[MAX_ESC_PARAMS];
161 int nb_esc_params;
162
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300163 Chardev *chr;
bellarde15d7372006-06-25 16:26:29 +0000164 /* fifo for key pressed */
165 QEMUFIFO out_fifo;
166 uint8_t out_fifo_buf[16];
167 QEMUTimer *kbd_timer;
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +0200168
169 QTAILQ_ENTRY(QemuConsole) next;
bellarde7f0ad52004-07-14 17:28:59 +0000170};
171
Gerd Hoffmann27be5582013-03-13 12:25:25 +0100172struct DisplayState {
Stefan Weil1246b252013-12-01 08:49:47 +0100173 QEMUTimer *gui_timer;
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100174 uint64_t last_update;
175 uint64_t update_interval;
176 bool refreshing;
Gerd Hoffmann27be5582013-03-13 12:25:25 +0100177 bool have_gfx;
178 bool have_text;
179
180 QLIST_HEAD(, DisplayChangeListener) listeners;
181};
182
Paolo Bonzini98b50082010-02-11 00:29:57 +0100183static DisplayState *display_state;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200184static QemuConsole *active_console;
Paolo Bonzinieae3eb32018-12-06 13:10:34 +0100185static QTAILQ_HEAD(, QemuConsole) consoles =
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +0200186 QTAILQ_HEAD_INITIALIZER(consoles);
Gerd Hoffmannaea79472014-05-22 11:27:13 +0200187static bool cursor_visible_phase;
188static QEMUTimer *cursor_timer;
bellarde7f0ad52004-07-14 17:28:59 +0000189
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300190static void text_console_do_init(Chardev *chr, DisplayState *ds);
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100191static void dpy_refresh(DisplayState *s);
Gerd Hoffmann52090892013-04-23 15:44:31 +0200192static DisplayState *get_alloc_displaystate(void);
Gerd Hoffmannaea79472014-05-22 11:27:13 +0200193static void text_console_update_cursor_timer(void);
194static void text_console_update_cursor(void *opaque);
Gerd Hoffmann64840c62013-03-07 17:08:29 +0100195
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100196static void gui_update(void *opaque)
197{
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100198 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
199 uint64_t dcl_interval;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100200 DisplayState *ds = opaque;
201 DisplayChangeListener *dcl;
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +0200202 QemuConsole *con;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100203
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100204 ds->refreshing = true;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100205 dpy_refresh(ds);
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100206 ds->refreshing = false;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100207
208 QLIST_FOREACH(dcl, &ds->listeners, next) {
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100209 dcl_interval = dcl->update_interval ?
210 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
211 if (interval > dcl_interval) {
212 interval = dcl_interval;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100213 }
214 }
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100215 if (ds->update_interval != interval) {
216 ds->update_interval = interval;
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +0200217 QTAILQ_FOREACH(con, &consoles, next) {
218 if (con->hw_ops->update_interval) {
219 con->hw_ops->update_interval(con->hw, interval);
Gerd Hoffmanndea1b0b2013-03-19 15:01:02 +0100220 }
221 }
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100222 trace_console_refresh(interval);
223 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100224 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
225 timer_mod(ds->gui_timer, ds->last_update + interval);
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100226}
227
228static void gui_setup_refresh(DisplayState *ds)
229{
230 DisplayChangeListener *dcl;
231 bool need_timer = false;
232 bool have_gfx = false;
233 bool have_text = false;
234
235 QLIST_FOREACH(dcl, &ds->listeners, next) {
236 if (dcl->ops->dpy_refresh != NULL) {
237 need_timer = true;
238 }
239 if (dcl->ops->dpy_gfx_update != NULL) {
240 have_gfx = true;
241 }
242 if (dcl->ops->dpy_text_update != NULL) {
243 have_text = true;
244 }
245 }
246
247 if (need_timer && ds->gui_timer == NULL) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100248 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
249 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100250 }
251 if (!need_timer && ds->gui_timer != NULL) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100252 timer_del(ds->gui_timer);
253 timer_free(ds->gui_timer);
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100254 ds->gui_timer = NULL;
255 }
256
257 ds->have_gfx = have_gfx;
258 ds->have_text = have_text;
259}
260
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100261void graphic_hw_update(QemuConsole *con)
pbrook95219892006-04-09 01:06:34 +0000262{
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100263 if (!con) {
264 con = active_console;
265 }
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100266 if (con && con->hw_ops->gfx_update) {
267 con->hw_ops->gfx_update(con->hw);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100268 }
pbrook95219892006-04-09 01:06:34 +0000269}
270
Gerd Hoffmannbba19b82015-12-03 12:34:25 +0100271void graphic_hw_gl_block(QemuConsole *con, bool block)
272{
Gerd Hoffmannf6078672016-09-23 09:50:27 +0200273 assert(con != NULL);
274
275 con->gl_block = block;
276 if (con->hw_ops->gl_block) {
Gerd Hoffmannbba19b82015-12-03 12:34:25 +0100277 con->hw_ops->gl_block(con->hw, block);
278 }
279}
280
Samuel Thibaultb3cb21b2016-12-21 01:38:04 +0100281int qemu_console_get_window_id(QemuConsole *con)
282{
283 return con->window_id;
284}
285
286void qemu_console_set_window_id(QemuConsole *con, int window_id)
287{
288 con->window_id = window_id;
289}
290
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100291void graphic_hw_invalidate(QemuConsole *con)
pbrook95219892006-04-09 01:06:34 +0000292{
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100293 if (!con) {
294 con = active_console;
295 }
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100296 if (con && con->hw_ops->invalidate) {
297 con->hw_ops->invalidate(con->hw);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100298 }
pbrook95219892006-04-09 01:06:34 +0000299}
300
Chih-Min Chao9425c002015-04-09 02:04:13 +0800301static void ppm_save(const char *filename, DisplaySurface *ds,
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100302 Error **errp)
303{
304 int width = pixman_image_get_width(ds->image);
305 int height = pixman_image_get_height(ds->image);
Gerd Hoffmanncdd5b932013-04-23 13:26:59 +0200306 int fd;
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100307 FILE *f;
308 int y;
309 int ret;
310 pixman_image_t *linebuf;
311
312 trace_ppm_save(filename, ds);
Gerd Hoffmanncdd5b932013-04-23 13:26:59 +0200313 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
314 if (fd == -1) {
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100315 error_setg(errp, "failed to open file '%s': %s", filename,
316 strerror(errno));
317 return;
318 }
Gerd Hoffmanncdd5b932013-04-23 13:26:59 +0200319 f = fdopen(fd, "wb");
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100320 ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
321 if (ret < 0) {
322 linebuf = NULL;
323 goto write_err;
324 }
325 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
326 for (y = 0; y < height; y++) {
327 qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
328 clearerr(f);
329 ret = fwrite(pixman_image_get_data(linebuf), 1,
330 pixman_image_get_stride(linebuf), f);
331 (void)ret;
332 if (ferror(f)) {
333 goto write_err;
334 }
335 }
336
337out:
338 qemu_pixman_image_unref(linebuf);
339 fclose(f);
340 return;
341
342write_err:
343 error_setg(errp, "failed to write to file '%s': %s", filename,
344 strerror(errno));
345 unlink(filename);
346 goto out;
347}
348
Thomas Huthf771c542018-03-05 17:37:48 +0100349void qmp_screendump(const char *filename, bool has_device, const char *device,
350 bool has_head, int64_t head, Error **errp)
pbrook95219892006-04-09 01:06:34 +0000351{
Thomas Huthf771c542018-03-05 17:37:48 +0100352 QemuConsole *con;
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100353 DisplaySurface *surface;
balrog8571c052008-07-19 13:04:26 +0000354
Thomas Huthf771c542018-03-05 17:37:48 +0100355 if (has_device) {
356 con = qemu_console_lookup_by_device_name(device, has_head ? head : 0,
357 errp);
358 if (!con) {
359 return;
360 }
361 } else {
362 if (has_head) {
363 error_setg(errp, "'head' must be specified together with 'device'");
364 return;
365 }
366 con = qemu_console_lookup_by_index(0);
367 if (!con) {
368 error_setg(errp, "There is no console to take a screendump from");
369 return;
370 }
Jan Kiszkaf81bdef2011-09-16 00:48:07 +0200371 }
372
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100373 graphic_hw_update(con);
374 surface = qemu_console_surface(con);
Michal Privoznik08d98642018-05-17 17:00:11 +0200375 if (!surface) {
376 error_setg(errp, "no surface");
377 return;
378 }
379
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100380 ppm_save(filename, surface, errp);
pbrook95219892006-04-09 01:06:34 +0000381}
382
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100383void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
balrog4d3b6f62008-02-10 16:33:14 +0000384{
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100385 if (!con) {
386 con = active_console;
387 }
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100388 if (con && con->hw_ops->text_update) {
389 con->hw_ops->text_update(con->hw, chardata);
390 }
balrog4d3b6f62008-02-10 16:33:14 +0000391}
392
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100393static void vga_fill_rect(QemuConsole *con,
394 int posx, int posy, int width, int height,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100395 pixman_color_t color)
bellarde7f0ad52004-07-14 17:28:59 +0000396{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100397 DisplaySurface *surface = qemu_console_surface(con);
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100398 pixman_rectangle16_t rect = {
399 .x = posx, .y = posy, .width = width, .height = height
400 };
ths3b46e622007-09-17 08:09:54 +0000401
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100402 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100403 &color, 1, &rect);
bellarde7f0ad52004-07-14 17:28:59 +0000404}
405
406/* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100407static void vga_bitblt(QemuConsole *con,
408 int xs, int ys, int xd, int yd, int w, int h)
bellarde7f0ad52004-07-14 17:28:59 +0000409{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100410 DisplaySurface *surface = qemu_console_surface(con);
bellarde7f0ad52004-07-14 17:28:59 +0000411
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100412 pixman_image_composite(PIXMAN_OP_SRC,
413 surface->image, NULL, surface->image,
414 xs, ys, 0, 0, xd, yd, w, h);
bellarde7f0ad52004-07-14 17:28:59 +0000415}
416
417/***********************************************************/
418/* basic char display */
419
420#define FONT_HEIGHT 16
421#define FONT_WIDTH 8
422
423#include "vgafont.h"
424
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100425#define QEMU_RGB(r, g, b) \
426 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
427
428static const pixman_color_t color_table_rgb[2][8] = {
pbrook6d6f7c22006-03-11 15:35:30 +0000429 { /* dark */
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900430 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
431 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
432 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
433 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
434 [QEMU_COLOR_RED] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
435 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
436 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
437 [QEMU_COLOR_WHITE] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
pbrook6d6f7c22006-03-11 15:35:30 +0000438 },
439 { /* bright */
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900440 [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
441 [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
442 [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
443 [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
444 [QEMU_COLOR_RED] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
445 [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
446 [QEMU_COLOR_YELLOW] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
447 [QEMU_COLOR_WHITE] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
pbrook6d6f7c22006-03-11 15:35:30 +0000448 }
bellarde7f0ad52004-07-14 17:28:59 +0000449};
450
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100451static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
pbrook6d6f7c22006-03-11 15:35:30 +0000452 TextAttributes *t_attrib)
bellarde7f0ad52004-07-14 17:28:59 +0000453{
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100454 static pixman_image_t *glyphs[256];
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100455 DisplaySurface *surface = qemu_console_surface(s);
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100456 pixman_color_t fgcol, bgcol;
pbrook6d6f7c22006-03-11 15:35:30 +0000457
458 if (t_attrib->invers) {
Gerd Hoffmanncf6f0542013-03-06 09:50:51 +0100459 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
460 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
pbrook6d6f7c22006-03-11 15:35:30 +0000461 } else {
Gerd Hoffmanncf6f0542013-03-06 09:50:51 +0100462 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
463 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
pbrook6d6f7c22006-03-11 15:35:30 +0000464 }
bellarde7f0ad52004-07-14 17:28:59 +0000465
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100466 if (!glyphs[ch]) {
467 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
bellarde7f0ad52004-07-14 17:28:59 +0000468 }
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100469 qemu_pixman_glyph_render(glyphs[ch], surface->image,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100470 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
bellarde7f0ad52004-07-14 17:28:59 +0000471}
472
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200473static void text_console_resize(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000474{
475 TextCell *cells, *c, *c1;
476 int w1, x, y, last_width;
477
478 last_width = s->width;
Gerd Hoffmann36671fb2013-03-13 10:14:52 +0100479 s->width = surface_width(s->surface) / FONT_WIDTH;
480 s->height = surface_height(s->surface) / FONT_HEIGHT;
bellarde7f0ad52004-07-14 17:28:59 +0000481
482 w1 = last_width;
483 if (s->width < w1)
484 w1 = s->width;
485
Markus Armbrusterfedf0d32015-11-03 17:12:03 +0100486 cells = g_new(TextCell, s->width * s->total_height);
bellarde7f0ad52004-07-14 17:28:59 +0000487 for(y = 0; y < s->total_height; y++) {
488 c = &cells[y * s->width];
489 if (w1 > 0) {
490 c1 = &s->cells[y * last_width];
491 for(x = 0; x < w1; x++) {
492 *c++ = *c1++;
493 }
494 }
495 for(x = w1; x < s->width; x++) {
496 c->ch = ' ';
pbrook6d6f7c22006-03-11 15:35:30 +0000497 c->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +0000498 c++;
499 }
500 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500501 g_free(s->cells);
bellarde7f0ad52004-07-14 17:28:59 +0000502 s->cells = cells;
503}
504
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200505static inline void text_update_xy(QemuConsole *s, int x, int y)
balrog4d3b6f62008-02-10 16:33:14 +0000506{
507 s->text_x[0] = MIN(s->text_x[0], x);
508 s->text_x[1] = MAX(s->text_x[1], x);
509 s->text_y[0] = MIN(s->text_y[0], y);
510 s->text_y[1] = MAX(s->text_y[1], y);
511}
512
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200513static void invalidate_xy(QemuConsole *s, int x, int y)
pbrook14778c22009-01-21 03:02:52 +0000514{
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200515 if (!qemu_console_is_visible(s)) {
516 return;
517 }
pbrook14778c22009-01-21 03:02:52 +0000518 if (s->update_x0 > x * FONT_WIDTH)
519 s->update_x0 = x * FONT_WIDTH;
520 if (s->update_y0 > y * FONT_HEIGHT)
521 s->update_y0 = y * FONT_HEIGHT;
522 if (s->update_x1 < (x + 1) * FONT_WIDTH)
523 s->update_x1 = (x + 1) * FONT_WIDTH;
524 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
525 s->update_y1 = (y + 1) * FONT_HEIGHT;
526}
527
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200528static void update_xy(QemuConsole *s, int x, int y)
bellarde7f0ad52004-07-14 17:28:59 +0000529{
530 TextCell *c;
531 int y1, y2;
532
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100533 if (s->ds->have_text) {
534 text_update_xy(s, x, y);
535 }
536
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200537 y1 = (s->y_base + y) % s->total_height;
538 y2 = y1 - s->y_displayed;
539 if (y2 < 0) {
540 y2 += s->total_height;
541 }
542 if (y2 < s->height) {
543 c = &s->cells[y1 * s->width + x];
544 vga_putcharxy(s, x, y2, c->ch,
545 &(c->t_attrib));
546 invalidate_xy(s, x, y2);
bellarde7f0ad52004-07-14 17:28:59 +0000547 }
548}
549
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200550static void console_show_cursor(QemuConsole *s, int show)
bellarde7f0ad52004-07-14 17:28:59 +0000551{
552 TextCell *c;
553 int y, y1;
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100554 int x = s->x;
bellarde7f0ad52004-07-14 17:28:59 +0000555
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100556 if (s->ds->have_text) {
557 s->cursor_invalidate = 1;
558 }
balrog4d3b6f62008-02-10 16:33:14 +0000559
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200560 if (x >= s->width) {
561 x = s->width - 1;
562 }
563 y1 = (s->y_base + s->y) % s->total_height;
564 y = y1 - s->y_displayed;
565 if (y < 0) {
566 y += s->total_height;
567 }
568 if (y < s->height) {
569 c = &s->cells[y1 * s->width + x];
Gerd Hoffmannaea79472014-05-22 11:27:13 +0200570 if (show && cursor_visible_phase) {
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200571 TextAttributes t_attrib = s->t_attrib_default;
572 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
573 vga_putcharxy(s, x, y, c->ch, &t_attrib);
574 } else {
575 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
thsed8276a2007-02-10 22:37:56 +0000576 }
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200577 invalidate_xy(s, x, y);
bellarde7f0ad52004-07-14 17:28:59 +0000578 }
579}
580
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200581static void console_refresh(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000582{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100583 DisplaySurface *surface = qemu_console_surface(s);
bellarde7f0ad52004-07-14 17:28:59 +0000584 TextCell *c;
585 int x, y, y1;
586
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200587 if (s->ds->have_text) {
balrog4d3b6f62008-02-10 16:33:14 +0000588 s->text_x[0] = 0;
589 s->text_y[0] = 0;
590 s->text_x[1] = s->width - 1;
591 s->text_y[1] = s->height - 1;
592 s->cursor_invalidate = 1;
balrog4d3b6f62008-02-10 16:33:14 +0000593 }
bellarde7f0ad52004-07-14 17:28:59 +0000594
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200595 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900596 color_table_rgb[0][QEMU_COLOR_BLACK]);
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200597 y1 = s->y_displayed;
598 for (y = 0; y < s->height; y++) {
599 c = s->cells + y1 * s->width;
600 for (x = 0; x < s->width; x++) {
601 vga_putcharxy(s, x, y, c->ch,
602 &(c->t_attrib));
603 c++;
bellarde7f0ad52004-07-14 17:28:59 +0000604 }
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200605 if (++y1 == s->total_height) {
606 y1 = 0;
607 }
bellarde7f0ad52004-07-14 17:28:59 +0000608 }
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200609 console_show_cursor(s, 1);
610 dpy_gfx_update(s, 0, 0,
611 surface_width(surface), surface_height(surface));
bellarde7f0ad52004-07-14 17:28:59 +0000612}
613
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100614static void console_scroll(QemuConsole *s, int ydelta)
bellarde7f0ad52004-07-14 17:28:59 +0000615{
bellarde7f0ad52004-07-14 17:28:59 +0000616 int i, y1;
ths3b46e622007-09-17 08:09:54 +0000617
bellarde7f0ad52004-07-14 17:28:59 +0000618 if (ydelta > 0) {
619 for(i = 0; i < ydelta; i++) {
620 if (s->y_displayed == s->y_base)
621 break;
622 if (++s->y_displayed == s->total_height)
623 s->y_displayed = 0;
624 }
625 } else {
626 ydelta = -ydelta;
627 i = s->backscroll_height;
628 if (i > s->total_height - s->height)
629 i = s->total_height - s->height;
630 y1 = s->y_base - i;
631 if (y1 < 0)
632 y1 += s->total_height;
633 for(i = 0; i < ydelta; i++) {
634 if (s->y_displayed == y1)
635 break;
636 if (--s->y_displayed < 0)
637 s->y_displayed = s->total_height - 1;
638 }
639 }
640 console_refresh(s);
641}
642
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200643static void console_put_lf(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000644{
645 TextCell *c;
646 int x, y1;
647
bellarde7f0ad52004-07-14 17:28:59 +0000648 s->y++;
649 if (s->y >= s->height) {
650 s->y = s->height - 1;
pbrook6d6f7c22006-03-11 15:35:30 +0000651
bellarde7f0ad52004-07-14 17:28:59 +0000652 if (s->y_displayed == s->y_base) {
653 if (++s->y_displayed == s->total_height)
654 s->y_displayed = 0;
655 }
656 if (++s->y_base == s->total_height)
657 s->y_base = 0;
658 if (s->backscroll_height < s->total_height)
659 s->backscroll_height++;
660 y1 = (s->y_base + s->height - 1) % s->total_height;
661 c = &s->cells[y1 * s->width];
662 for(x = 0; x < s->width; x++) {
663 c->ch = ' ';
pbrook6d6f7c22006-03-11 15:35:30 +0000664 c->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +0000665 c++;
666 }
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200667 if (s->y_displayed == s->y_base) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100668 if (s->ds->have_text) {
balrog4d3b6f62008-02-10 16:33:14 +0000669 s->text_x[0] = 0;
670 s->text_y[0] = 0;
671 s->text_x[1] = s->width - 1;
672 s->text_y[1] = s->height - 1;
balrog4d3b6f62008-02-10 16:33:14 +0000673 }
674
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200675 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
676 s->width * FONT_WIDTH,
677 (s->height - 1) * FONT_HEIGHT);
678 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
679 s->width * FONT_WIDTH, FONT_HEIGHT,
680 color_table_rgb[0][s->t_attrib_default.bgcol]);
681 s->update_x0 = 0;
682 s->update_y0 = 0;
683 s->update_x1 = s->width * FONT_WIDTH;
684 s->update_y1 = s->height * FONT_HEIGHT;
bellarde7f0ad52004-07-14 17:28:59 +0000685 }
686 }
687}
688
pbrook6d6f7c22006-03-11 15:35:30 +0000689/* Set console attributes depending on the current escape codes.
690 * NOTE: I know this code is not very efficient (checking every color for it
691 * self) but it is more readable and better maintainable.
692 */
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200693static void console_handle_escape(QemuConsole *s)
pbrook6d6f7c22006-03-11 15:35:30 +0000694{
695 int i;
696
pbrook6d6f7c22006-03-11 15:35:30 +0000697 for (i=0; i<s->nb_esc_params; i++) {
698 switch (s->esc_params[i]) {
699 case 0: /* reset all console attributes to default */
700 s->t_attrib = s->t_attrib_default;
701 break;
702 case 1:
703 s->t_attrib.bold = 1;
704 break;
705 case 4:
706 s->t_attrib.uline = 1;
707 break;
708 case 5:
709 s->t_attrib.blink = 1;
710 break;
711 case 7:
712 s->t_attrib.invers = 1;
713 break;
714 case 8:
715 s->t_attrib.unvisible = 1;
716 break;
717 case 22:
718 s->t_attrib.bold = 0;
719 break;
720 case 24:
721 s->t_attrib.uline = 0;
722 break;
723 case 25:
724 s->t_attrib.blink = 0;
725 break;
726 case 27:
727 s->t_attrib.invers = 0;
728 break;
729 case 28:
730 s->t_attrib.unvisible = 0;
731 break;
732 /* set foreground color */
733 case 30:
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900734 s->t_attrib.fgcol = QEMU_COLOR_BLACK;
pbrook6d6f7c22006-03-11 15:35:30 +0000735 break;
736 case 31:
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900737 s->t_attrib.fgcol = QEMU_COLOR_RED;
pbrook6d6f7c22006-03-11 15:35:30 +0000738 break;
739 case 32:
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900740 s->t_attrib.fgcol = QEMU_COLOR_GREEN;
pbrook6d6f7c22006-03-11 15:35:30 +0000741 break;
742 case 33:
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900743 s->t_attrib.fgcol = QEMU_COLOR_YELLOW;
pbrook6d6f7c22006-03-11 15:35:30 +0000744 break;
745 case 34:
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900746 s->t_attrib.fgcol = QEMU_COLOR_BLUE;
pbrook6d6f7c22006-03-11 15:35:30 +0000747 break;
748 case 35:
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900749 s->t_attrib.fgcol = QEMU_COLOR_MAGENTA;
pbrook6d6f7c22006-03-11 15:35:30 +0000750 break;
751 case 36:
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900752 s->t_attrib.fgcol = QEMU_COLOR_CYAN;
pbrook6d6f7c22006-03-11 15:35:30 +0000753 break;
754 case 37:
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900755 s->t_attrib.fgcol = QEMU_COLOR_WHITE;
pbrook6d6f7c22006-03-11 15:35:30 +0000756 break;
757 /* set background color */
758 case 40:
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900759 s->t_attrib.bgcol = QEMU_COLOR_BLACK;
pbrook6d6f7c22006-03-11 15:35:30 +0000760 break;
761 case 41:
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900762 s->t_attrib.bgcol = QEMU_COLOR_RED;
pbrook6d6f7c22006-03-11 15:35:30 +0000763 break;
764 case 42:
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900765 s->t_attrib.bgcol = QEMU_COLOR_GREEN;
pbrook6d6f7c22006-03-11 15:35:30 +0000766 break;
767 case 43:
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900768 s->t_attrib.bgcol = QEMU_COLOR_YELLOW;
pbrook6d6f7c22006-03-11 15:35:30 +0000769 break;
770 case 44:
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900771 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
pbrook6d6f7c22006-03-11 15:35:30 +0000772 break;
773 case 45:
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900774 s->t_attrib.bgcol = QEMU_COLOR_MAGENTA;
pbrook6d6f7c22006-03-11 15:35:30 +0000775 break;
776 case 46:
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900777 s->t_attrib.bgcol = QEMU_COLOR_CYAN;
pbrook6d6f7c22006-03-11 15:35:30 +0000778 break;
779 case 47:
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900780 s->t_attrib.bgcol = QEMU_COLOR_WHITE;
pbrook6d6f7c22006-03-11 15:35:30 +0000781 break;
782 }
783 }
784}
785
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200786static void console_clear_xy(QemuConsole *s, int x, int y)
thsadb47962007-01-16 23:02:36 +0000787{
788 int y1 = (s->y_base + y) % s->total_height;
789 TextCell *c = &s->cells[y1 * s->width + x];
790 c->ch = ' ';
791 c->t_attrib = s->t_attrib_default;
thsadb47962007-01-16 23:02:36 +0000792 update_xy(s, x, y);
793}
794
Ren Kimura58aa7d82016-03-09 04:51:21 +0900795static void console_put_one(QemuConsole *s, int ch)
796{
797 TextCell *c;
798 int y1;
799 if (s->x >= s->width) {
800 /* line wrap */
801 s->x = 0;
802 console_put_lf(s);
803 }
804 y1 = (s->y_base + s->y) % s->total_height;
805 c = &s->cells[y1 * s->width + s->x];
806 c->ch = ch;
807 c->t_attrib = s->t_attrib;
808 update_xy(s, s->x, s->y);
809 s->x++;
810}
811
812static void console_respond_str(QemuConsole *s, const char *buf)
813{
814 while (*buf) {
815 console_put_one(s, *buf);
816 buf++;
817 }
818}
819
Ian Campbell3eea5492012-09-04 10:26:09 -0500820/* set cursor, checking bounds */
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200821static void set_cursor(QemuConsole *s, int x, int y)
Ian Campbell3eea5492012-09-04 10:26:09 -0500822{
823 if (x < 0) {
824 x = 0;
825 }
826 if (y < 0) {
827 y = 0;
828 }
829 if (y >= s->height) {
830 y = s->height - 1;
831 }
832 if (x >= s->width) {
833 x = s->width - 1;
834 }
835
836 s->x = x;
837 s->y = y;
838}
839
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200840static void console_putchar(QemuConsole *s, int ch)
bellarde7f0ad52004-07-14 17:28:59 +0000841{
Ren Kimura58aa7d82016-03-09 04:51:21 +0900842 int i;
thsadb47962007-01-16 23:02:36 +0000843 int x, y;
Ren Kimura58aa7d82016-03-09 04:51:21 +0900844 char response[40];
bellarde7f0ad52004-07-14 17:28:59 +0000845
846 switch(s->state) {
847 case TTY_STATE_NORM:
848 switch(ch) {
pbrook6d6f7c22006-03-11 15:35:30 +0000849 case '\r': /* carriage return */
bellarde7f0ad52004-07-14 17:28:59 +0000850 s->x = 0;
851 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000852 case '\n': /* newline */
bellarde7f0ad52004-07-14 17:28:59 +0000853 console_put_lf(s);
854 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000855 case '\b': /* backspace */
ths5fafdf22007-09-16 21:08:06 +0000856 if (s->x > 0)
bellarde15d7372006-06-25 16:26:29 +0000857 s->x--;
pbrook6d6f7c22006-03-11 15:35:30 +0000858 break;
859 case '\t': /* tabspace */
860 if (s->x + (8 - (s->x % 8)) > s->width) {
bellardbd468842006-07-14 20:24:31 +0000861 s->x = 0;
pbrook6d6f7c22006-03-11 15:35:30 +0000862 console_put_lf(s);
863 } else {
864 s->x = s->x + (8 - (s->x % 8));
865 }
866 break;
867 case '\a': /* alert aka. bell */
868 /* TODO: has to be implemented */
869 break;
thsadb47962007-01-16 23:02:36 +0000870 case 14:
871 /* SI (shift in), character set 0 (ignored) */
872 break;
873 case 15:
874 /* SO (shift out), character set 1 (ignored) */
875 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000876 case 27: /* esc (introducing an escape sequence) */
bellarde7f0ad52004-07-14 17:28:59 +0000877 s->state = TTY_STATE_ESC;
878 break;
879 default:
Ren Kimura58aa7d82016-03-09 04:51:21 +0900880 console_put_one(s, ch);
bellarde7f0ad52004-07-14 17:28:59 +0000881 break;
882 }
883 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000884 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
bellarde7f0ad52004-07-14 17:28:59 +0000885 if (ch == '[') {
886 for(i=0;i<MAX_ESC_PARAMS;i++)
887 s->esc_params[i] = 0;
888 s->nb_esc_params = 0;
889 s->state = TTY_STATE_CSI;
890 } else {
891 s->state = TTY_STATE_NORM;
892 }
893 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000894 case TTY_STATE_CSI: /* handle escape sequence parameters */
bellarde7f0ad52004-07-14 17:28:59 +0000895 if (ch >= '0' && ch <= '9') {
896 if (s->nb_esc_params < MAX_ESC_PARAMS) {
Laszlo Ersekc10600a2012-09-17 11:10:03 +0200897 int *param = &s->esc_params[s->nb_esc_params];
898 int digit = (ch - '0');
899
900 *param = (*param <= (INT_MAX - digit) / 10) ?
901 *param * 10 + digit : INT_MAX;
bellarde7f0ad52004-07-14 17:28:59 +0000902 }
903 } else {
Ian Campbell3eea5492012-09-04 10:26:09 -0500904 if (s->nb_esc_params < MAX_ESC_PARAMS)
905 s->nb_esc_params++;
Alexander Graf7c336f92017-08-29 13:38:18 +0200906 if (ch == ';' || ch == '?') {
bellarde7f0ad52004-07-14 17:28:59 +0000907 break;
Alexander Graf7c336f92017-08-29 13:38:18 +0200908 }
Stefan Weil5d28b0e2013-11-10 16:04:16 +0100909 trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
910 ch, s->nb_esc_params);
bellarde7f0ad52004-07-14 17:28:59 +0000911 s->state = TTY_STATE_NORM;
912 switch(ch) {
thsadb47962007-01-16 23:02:36 +0000913 case 'A':
914 /* move cursor up */
915 if (s->esc_params[0] == 0) {
916 s->esc_params[0] = 1;
917 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500918 set_cursor(s, s->x, s->y - s->esc_params[0]);
bellarde7f0ad52004-07-14 17:28:59 +0000919 break;
thsadb47962007-01-16 23:02:36 +0000920 case 'B':
921 /* move cursor down */
922 if (s->esc_params[0] == 0) {
923 s->esc_params[0] = 1;
924 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500925 set_cursor(s, s->x, s->y + s->esc_params[0]);
thsadb47962007-01-16 23:02:36 +0000926 break;
927 case 'C':
928 /* move cursor right */
929 if (s->esc_params[0] == 0) {
930 s->esc_params[0] = 1;
931 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500932 set_cursor(s, s->x + s->esc_params[0], s->y);
thsadb47962007-01-16 23:02:36 +0000933 break;
934 case 'D':
935 /* move cursor left */
936 if (s->esc_params[0] == 0) {
937 s->esc_params[0] = 1;
938 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500939 set_cursor(s, s->x - s->esc_params[0], s->y);
thsadb47962007-01-16 23:02:36 +0000940 break;
941 case 'G':
942 /* move cursor to column */
Ian Campbell3eea5492012-09-04 10:26:09 -0500943 set_cursor(s, s->esc_params[0] - 1, s->y);
thsadb47962007-01-16 23:02:36 +0000944 break;
945 case 'f':
946 case 'H':
947 /* move cursor to row, column */
Ian Campbell3eea5492012-09-04 10:26:09 -0500948 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
thsadb47962007-01-16 23:02:36 +0000949 break;
950 case 'J':
951 switch (s->esc_params[0]) {
952 case 0:
953 /* clear to end of screen */
954 for (y = s->y; y < s->height; y++) {
955 for (x = 0; x < s->width; x++) {
956 if (y == s->y && x < s->x) {
957 continue;
958 }
959 console_clear_xy(s, x, y);
960 }
961 }
962 break;
963 case 1:
964 /* clear from beginning of screen */
965 for (y = 0; y <= s->y; y++) {
966 for (x = 0; x < s->width; x++) {
967 if (y == s->y && x > s->x) {
968 break;
969 }
970 console_clear_xy(s, x, y);
971 }
972 }
973 break;
974 case 2:
975 /* clear entire screen */
976 for (y = 0; y <= s->height; y++) {
977 for (x = 0; x < s->width; x++) {
978 console_clear_xy(s, x, y);
979 }
980 }
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100981 break;
thsadb47962007-01-16 23:02:36 +0000982 }
Markus Armbruster95d8f9f2011-11-22 11:59:07 +0100983 break;
thsadb47962007-01-16 23:02:36 +0000984 case 'K':
985 switch (s->esc_params[0]) {
986 case 0:
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100987 /* clear to eol */
988 for(x = s->x; x < s->width; x++) {
thsadb47962007-01-16 23:02:36 +0000989 console_clear_xy(s, x, s->y);
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100990 }
991 break;
thsadb47962007-01-16 23:02:36 +0000992 case 1:
993 /* clear from beginning of line */
994 for (x = 0; x <= s->x; x++) {
995 console_clear_xy(s, x, s->y);
996 }
997 break;
998 case 2:
999 /* clear entire line */
1000 for(x = 0; x < s->width; x++) {
1001 console_clear_xy(s, x, s->y);
1002 }
Markus Armbrusterf94a9502011-11-22 11:59:06 +01001003 break;
1004 }
thsadb47962007-01-16 23:02:36 +00001005 break;
1006 case 'm':
Markus Armbrusterf94a9502011-11-22 11:59:06 +01001007 console_handle_escape(s);
1008 break;
thsadb47962007-01-16 23:02:36 +00001009 case 'n':
Ren Kimura58aa7d82016-03-09 04:51:21 +09001010 switch (s->esc_params[0]) {
1011 case 5:
1012 /* report console status (always succeed)*/
1013 console_respond_str(s, "\033[0n");
1014 break;
1015 case 6:
1016 /* report cursor position */
1017 sprintf(response, "\033[%d;%dR",
1018 (s->y_base + s->y) % s->total_height + 1,
1019 s->x + 1);
1020 console_respond_str(s, response);
1021 break;
1022 }
thsadb47962007-01-16 23:02:36 +00001023 break;
1024 case 's':
1025 /* save cursor position */
1026 s->x_saved = s->x;
1027 s->y_saved = s->y;
1028 break;
1029 case 'u':
1030 /* restore cursor position */
1031 s->x = s->x_saved;
1032 s->y = s->y_saved;
1033 break;
1034 default:
Stefan Weil5d28b0e2013-11-10 16:04:16 +01001035 trace_console_putchar_unhandled(ch);
thsadb47962007-01-16 23:02:36 +00001036 break;
1037 }
1038 break;
bellarde7f0ad52004-07-14 17:28:59 +00001039 }
1040 }
1041}
1042
1043void console_select(unsigned int index)
1044{
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001045 DisplayChangeListener *dcl;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001046 QemuConsole *s;
pbrook6d6f7c22006-03-11 15:35:30 +00001047
Gerd Hoffmann437fe102013-03-07 16:04:52 +01001048 trace_console_select(index);
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001049 s = qemu_console_lookup_by_index(index);
bellarde7f0ad52004-07-14 17:28:59 +00001050 if (s) {
aliguori7d957bd2009-01-15 22:14:11 +00001051 DisplayState *ds = s->ds;
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001052
bellarde7f0ad52004-07-14 17:28:59 +00001053 active_console = s;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001054 if (ds->have_gfx) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001055 QLIST_FOREACH(dcl, &ds->listeners, next) {
1056 if (dcl->con != NULL) {
1057 continue;
1058 }
1059 if (dcl->ops->dpy_gfx_switch) {
1060 dcl->ops->dpy_gfx_switch(dcl, s->surface);
1061 }
1062 }
Gerd Hoffmann2e5567c2018-03-08 17:18:03 +01001063 if (s->surface) {
1064 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1065 surface_height(s->surface));
1066 }
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001067 }
1068 if (ds->have_text) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001069 dpy_text_resize(s, s->width, s->height);
aliguori68f00992009-01-21 18:59:12 +00001070 }
Gerd Hoffmannaea79472014-05-22 11:27:13 +02001071 text_console_update_cursor(NULL);
bellarde7f0ad52004-07-14 17:28:59 +00001072 }
1073}
1074
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001075typedef struct VCChardev {
1076 Chardev parent;
Marc-André Lureau41ac54b2016-10-21 23:44:44 +03001077 QemuConsole *console;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001078} VCChardev;
Marc-André Lureau41ac54b2016-10-21 23:44:44 +03001079
Marc-André Lureau777357d2016-12-07 18:39:10 +03001080#define TYPE_CHARDEV_VC "chardev-vc"
1081#define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
1082
Marc-André Lureau5bf5ada2017-01-05 17:30:29 +01001083static int vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
bellarde7f0ad52004-07-14 17:28:59 +00001084{
Marc-André Lureau777357d2016-12-07 18:39:10 +03001085 VCChardev *drv = VC_CHARDEV(chr);
Marc-André Lureau41ac54b2016-10-21 23:44:44 +03001086 QemuConsole *s = drv->console;
bellarde7f0ad52004-07-14 17:28:59 +00001087 int i;
1088
Marc-André Lureaub68e9562016-10-21 20:49:37 +03001089 if (!s->ds) {
1090 return 0;
1091 }
1092
pbrook14778c22009-01-21 03:02:52 +00001093 s->update_x0 = s->width * FONT_WIDTH;
1094 s->update_y0 = s->height * FONT_HEIGHT;
1095 s->update_x1 = 0;
1096 s->update_y1 = 0;
bellarde7f0ad52004-07-14 17:28:59 +00001097 console_show_cursor(s, 0);
1098 for(i = 0; i < len; i++) {
1099 console_putchar(s, buf[i]);
1100 }
1101 console_show_cursor(s, 1);
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001102 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001103 dpy_gfx_update(s, s->update_x0, s->update_y0,
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001104 s->update_x1 - s->update_x0,
1105 s->update_y1 - s->update_y0);
pbrook14778c22009-01-21 03:02:52 +00001106 }
bellarde7f0ad52004-07-14 17:28:59 +00001107 return len;
1108}
1109
bellarde15d7372006-06-25 16:26:29 +00001110static void kbd_send_chars(void *opaque)
1111{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001112 QemuConsole *s = opaque;
bellarde15d7372006-06-25 16:26:29 +00001113 int len;
1114 uint8_t buf[16];
ths3b46e622007-09-17 08:09:54 +00001115
Anthony Liguori909cda12011-08-15 11:17:31 -05001116 len = qemu_chr_be_can_write(s->chr);
bellarde15d7372006-06-25 16:26:29 +00001117 if (len > s->out_fifo.count)
1118 len = s->out_fifo.count;
1119 if (len > 0) {
1120 if (len > sizeof(buf))
1121 len = sizeof(buf);
1122 qemu_fifo_read(&s->out_fifo, buf, len);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001123 qemu_chr_be_write(s->chr, buf, len);
bellarde15d7372006-06-25 16:26:29 +00001124 }
1125 /* characters are pending: we send them a bit later (XXX:
1126 horrible, should change char device API) */
1127 if (s->out_fifo.count > 0) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001128 timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
bellarde15d7372006-06-25 16:26:29 +00001129 }
1130}
1131
bellarde7f0ad52004-07-14 17:28:59 +00001132/* called when an ascii key is pressed */
Gerd Hoffmann3f9a6e82014-05-22 12:05:52 +02001133void kbd_put_keysym_console(QemuConsole *s, int keysym)
bellarde7f0ad52004-07-14 17:28:59 +00001134{
bellarde7f0ad52004-07-14 17:28:59 +00001135 uint8_t buf[16], *q;
Marc-André Lureaua4afa542016-10-22 12:53:01 +03001136 CharBackend *be;
bellarde7f0ad52004-07-14 17:28:59 +00001137 int c;
1138
thsaf3a9032007-07-11 23:14:59 +00001139 if (!s || (s->console_type == GRAPHIC_CONSOLE))
bellarde7f0ad52004-07-14 17:28:59 +00001140 return;
1141
1142 switch(keysym) {
1143 case QEMU_KEY_CTRL_UP:
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001144 console_scroll(s, -1);
bellarde7f0ad52004-07-14 17:28:59 +00001145 break;
1146 case QEMU_KEY_CTRL_DOWN:
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001147 console_scroll(s, 1);
bellarde7f0ad52004-07-14 17:28:59 +00001148 break;
1149 case QEMU_KEY_CTRL_PAGEUP:
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001150 console_scroll(s, -10);
bellarde7f0ad52004-07-14 17:28:59 +00001151 break;
1152 case QEMU_KEY_CTRL_PAGEDOWN:
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001153 console_scroll(s, 10);
bellarde7f0ad52004-07-14 17:28:59 +00001154 break;
1155 default:
bellarde15d7372006-06-25 16:26:29 +00001156 /* convert the QEMU keysym to VT100 key string */
1157 q = buf;
1158 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1159 *q++ = '\033';
1160 *q++ = '[';
1161 c = keysym - 0xe100;
1162 if (c >= 10)
1163 *q++ = '0' + (c / 10);
1164 *q++ = '0' + (c % 10);
1165 *q++ = '~';
1166 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1167 *q++ = '\033';
1168 *q++ = '[';
1169 *q++ = keysym & 0xff;
Paolo Bonzini41048332010-12-23 13:42:52 +01001170 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
Marc-André Lureau5bf5ada2017-01-05 17:30:29 +01001171 vc_chr_write(s->chr, (const uint8_t *) "\r", 1);
Paolo Bonzini41048332010-12-23 13:42:52 +01001172 *q++ = '\n';
bellarde15d7372006-06-25 16:26:29 +00001173 } else {
Paolo Bonzini41048332010-12-23 13:42:52 +01001174 *q++ = keysym;
1175 }
1176 if (s->echo) {
Marc-André Lureau5bf5ada2017-01-05 17:30:29 +01001177 vc_chr_write(s->chr, buf, q - buf);
bellarde15d7372006-06-25 16:26:29 +00001178 }
Marc-André Lureaua4afa542016-10-22 12:53:01 +03001179 be = s->chr->be;
1180 if (be && be->chr_read) {
bellarde15d7372006-06-25 16:26:29 +00001181 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1182 kbd_send_chars(s);
bellarde7f0ad52004-07-14 17:28:59 +00001183 }
1184 break;
1185 }
1186}
1187
Eric Blake7fb1cf12015-11-18 01:52:57 -07001188static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
Gerd Hoffmann50ef4672014-05-27 09:28:38 +02001189 [Q_KEY_CODE_UP] = QEMU_KEY_UP,
1190 [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
1191 [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
1192 [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
1193 [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
1194 [Q_KEY_CODE_END] = QEMU_KEY_END,
1195 [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
1196 [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
1197 [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
Thomas Huth344aa282016-08-11 09:21:00 +02001198 [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE,
Gerd Hoffmann50ef4672014-05-27 09:28:38 +02001199};
1200
Gerd Hoffmannda024b12018-03-21 14:50:36 +01001201static const int ctrl_qcode_to_keysym[Q_KEY_CODE__MAX] = {
1202 [Q_KEY_CODE_UP] = QEMU_KEY_CTRL_UP,
1203 [Q_KEY_CODE_DOWN] = QEMU_KEY_CTRL_DOWN,
1204 [Q_KEY_CODE_RIGHT] = QEMU_KEY_CTRL_RIGHT,
1205 [Q_KEY_CODE_LEFT] = QEMU_KEY_CTRL_LEFT,
1206 [Q_KEY_CODE_HOME] = QEMU_KEY_CTRL_HOME,
1207 [Q_KEY_CODE_END] = QEMU_KEY_CTRL_END,
1208 [Q_KEY_CODE_PGUP] = QEMU_KEY_CTRL_PAGEUP,
1209 [Q_KEY_CODE_PGDN] = QEMU_KEY_CTRL_PAGEDOWN,
1210};
1211
1212bool kbd_put_qcode_console(QemuConsole *s, int qcode, bool ctrl)
Gerd Hoffmann50ef4672014-05-27 09:28:38 +02001213{
1214 int keysym;
1215
Gerd Hoffmannda024b12018-03-21 14:50:36 +01001216 keysym = ctrl ? ctrl_qcode_to_keysym[qcode] : qcode_to_keysym[qcode];
Gerd Hoffmann50ef4672014-05-27 09:28:38 +02001217 if (keysym == 0) {
1218 return false;
1219 }
1220 kbd_put_keysym_console(s, keysym);
1221 return true;
1222}
1223
Gerd Hoffmannbdef9722014-05-27 09:32:36 +02001224void kbd_put_string_console(QemuConsole *s, const char *str, int len)
1225{
1226 int i;
1227
1228 for (i = 0; i < len && str[i]; i++) {
1229 kbd_put_keysym_console(s, str[i]);
1230 }
1231}
1232
Gerd Hoffmann3f9a6e82014-05-22 12:05:52 +02001233void kbd_put_keysym(int keysym)
1234{
1235 kbd_put_keysym_console(active_console, keysym);
1236}
1237
balrog4d3b6f62008-02-10 16:33:14 +00001238static void text_console_invalidate(void *opaque)
1239{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001240 QemuConsole *s = (QemuConsole *) opaque;
Gerd Hoffmann1562e532013-03-06 13:40:47 +01001241
1242 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
aliguori68f00992009-01-21 18:59:12 +00001243 text_console_resize(s);
1244 }
balrog4d3b6f62008-02-10 16:33:14 +00001245 console_refresh(s);
1246}
1247
Anthony Liguoric227f092009-10-01 16:12:16 -05001248static void text_console_update(void *opaque, console_ch_t *chardata)
balrog4d3b6f62008-02-10 16:33:14 +00001249{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001250 QemuConsole *s = (QemuConsole *) opaque;
balrog4d3b6f62008-02-10 16:33:14 +00001251 int i, j, src;
1252
1253 if (s->text_x[0] <= s->text_x[1]) {
1254 src = (s->y_base + s->text_y[0]) * s->width;
1255 chardata += s->text_y[0] * s->width;
1256 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
OGAWA Hirofumi40837332015-11-29 22:28:24 +09001257 for (j = 0; j < s->width; j++, src++) {
1258 console_write_ch(chardata ++,
1259 ATTR2CHTYPE(s->cells[src].ch,
1260 s->cells[src].t_attrib.fgcol,
1261 s->cells[src].t_attrib.bgcol,
1262 s->cells[src].t_attrib.bold));
1263 }
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001264 dpy_text_update(s, s->text_x[0], s->text_y[0],
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001265 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
balrog4d3b6f62008-02-10 16:33:14 +00001266 s->text_x[0] = s->width;
1267 s->text_y[0] = s->height;
1268 s->text_x[1] = 0;
1269 s->text_y[1] = 0;
1270 }
1271 if (s->cursor_invalidate) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001272 dpy_text_cursor(s, s->x, s->y);
balrog4d3b6f62008-02-10 16:33:14 +00001273 s->cursor_invalidate = 0;
1274 }
1275}
1276
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001277static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
1278 uint32_t head)
bellarde7f0ad52004-07-14 17:28:59 +00001279{
Gerd Hoffmann95be0662013-04-17 09:45:10 +02001280 Object *obj;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001281 QemuConsole *s;
pbrook95219892006-04-09 01:06:34 +00001282 int i;
bellarde7f0ad52004-07-14 17:28:59 +00001283
Gerd Hoffmann95be0662013-04-17 09:45:10 +02001284 obj = object_new(TYPE_QEMU_CONSOLE);
1285 s = QEMU_CONSOLE(obj);
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001286 s->head = head;
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001287 object_property_add_link(obj, "device", TYPE_DEVICE,
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001288 (Object **)&s->device,
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001289 object_property_allow_set_link,
Marc-André Lureau265b5782018-05-31 21:51:17 +02001290 OBJ_PROP_LINK_STRONG,
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001291 &error_abort);
Gerd Hoffmann56437062014-01-24 15:35:21 +01001292 object_property_add_uint32_ptr(obj, "head",
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001293 &s->head, &error_abort);
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001294
thsaf3a9032007-07-11 23:14:59 +00001295 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1296 (console_type == GRAPHIC_CONSOLE))) {
bellarde7f0ad52004-07-14 17:28:59 +00001297 active_console = s;
thsaf3a9032007-07-11 23:14:59 +00001298 }
bellarde7f0ad52004-07-14 17:28:59 +00001299 s->ds = ds;
thsaf3a9032007-07-11 23:14:59 +00001300 s->console_type = console_type;
Gerd Hoffmanna1d2db02014-05-26 10:36:35 +02001301
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02001302 if (QTAILQ_EMPTY(&consoles)) {
1303 s->index = 0;
1304 QTAILQ_INSERT_TAIL(&consoles, s, next);
1305 } else if (console_type != GRAPHIC_CONSOLE || qdev_hotplug) {
Paolo Bonzinieae3eb32018-12-06 13:10:34 +01001306 QemuConsole *last = QTAILQ_LAST(&consoles);
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02001307 s->index = last->index + 1;
1308 QTAILQ_INSERT_TAIL(&consoles, s, next);
pbrook95219892006-04-09 01:06:34 +00001309 } else {
Gerd Hoffmann9588d672018-03-13 11:17:29 -06001310 /*
1311 * HACK: Put graphical consoles before text consoles.
1312 *
1313 * Only do that for coldplugged devices. After initial device
1314 * initialization we will not renumber the consoles any more.
1315 */
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02001316 QemuConsole *c = QTAILQ_FIRST(&consoles);
1317
1318 while (QTAILQ_NEXT(c, next) != NULL &&
1319 c->console_type == GRAPHIC_CONSOLE) {
1320 c = QTAILQ_NEXT(c, next);
pbrook95219892006-04-09 01:06:34 +00001321 }
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02001322 if (c->console_type == GRAPHIC_CONSOLE) {
1323 /* have no text consoles */
1324 s->index = c->index + 1;
1325 QTAILQ_INSERT_AFTER(&consoles, c, s, next);
1326 } else {
1327 s->index = c->index;
1328 QTAILQ_INSERT_BEFORE(c, s, next);
1329 /* renumber text consoles */
1330 for (i = s->index + 1; c != NULL; c = QTAILQ_NEXT(c, next), i++) {
1331 c->index = i;
1332 }
1333 }
pbrook95219892006-04-09 01:06:34 +00001334 }
bellarde7f0ad52004-07-14 17:28:59 +00001335 return s;
1336}
1337
Gerd Hoffmann30f1e662014-06-18 11:03:15 +02001338static void qemu_alloc_display(DisplaySurface *surface, int width, int height)
Jes Sorensenffe8b822011-03-16 13:33:30 +01001339{
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001340 qemu_pixman_image_unref(surface->image);
1341 surface->image = NULL;
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001342
Gerd Hoffmann30f1e662014-06-18 11:03:15 +02001343 surface->format = PIXMAN_x8r8g8b8;
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001344 surface->image = pixman_image_create_bits(surface->format,
1345 width, height,
Gerd Hoffmann30f1e662014-06-18 11:03:15 +02001346 NULL, width * 4);
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001347 assert(surface->image != NULL);
1348
Gerd Hoffmann30f1e662014-06-18 11:03:15 +02001349 surface->flags = QEMU_ALLOCATED_FLAG;
Paolo Bonzini98b50082010-02-11 00:29:57 +01001350}
1351
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001352DisplaySurface *qemu_create_displaysurface(int width, int height)
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001353{
1354 DisplaySurface *surface = g_new0(DisplaySurface, 1);
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001355
1356 trace_displaysurface_create(surface, width, height);
Gerd Hoffmann30f1e662014-06-18 11:03:15 +02001357 qemu_alloc_display(surface, width, height);
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001358 return surface;
1359}
1360
Gerd Hoffmann30f1e662014-06-18 11:03:15 +02001361DisplaySurface *qemu_create_displaysurface_from(int width, int height,
1362 pixman_format_code_t format,
1363 int linesize, uint8_t *data)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001364{
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001365 DisplaySurface *surface = g_new0(DisplaySurface, 1);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001366
Gerd Hoffmann30f1e662014-06-18 11:03:15 +02001367 trace_displaysurface_create_from(surface, width, height, format);
1368 surface->format = format;
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001369 surface->image = pixman_image_create_bits(surface->format,
1370 width, height,
1371 (void *)data, linesize);
1372 assert(surface->image != NULL);
1373
Paolo Bonzini98b50082010-02-11 00:29:57 +01001374 return surface;
1375}
1376
Gerd Hoffmannca58b452016-04-01 10:27:20 +02001377DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
1378{
1379 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1380
1381 trace_displaysurface_create_pixman(surface);
1382 surface->format = pixman_image_get_format(image);
1383 surface->image = pixman_image_ref(image);
1384
1385 return surface;
1386}
1387
Gerd Hoffmann2e5567c2018-03-08 17:18:03 +01001388DisplaySurface *qemu_create_message_surface(int w, int h,
1389 const char *msg)
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001390{
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001391 DisplaySurface *surface = qemu_create_displaysurface(w, h);
OGAWA Hirofumi40837332015-11-29 22:28:24 +09001392 pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK];
1393 pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE];
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001394 pixman_image_t *glyph;
1395 int len, x, y, i;
1396
1397 len = strlen(msg);
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001398 x = (w / FONT_WIDTH - len) / 2;
1399 y = (h / FONT_HEIGHT - 1) / 2;
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001400 for (i = 0; i < len; i++) {
1401 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1402 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1403 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1404 qemu_pixman_image_unref(glyph);
1405 }
1406 return surface;
1407}
1408
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001409void qemu_free_displaysurface(DisplaySurface *surface)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001410{
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001411 if (surface == NULL) {
Paolo Bonzini98b50082010-02-11 00:29:57 +01001412 return;
Gerd Hoffmann187cd1d2012-09-26 07:46:20 +02001413 }
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001414 trace_displaysurface_free(surface);
1415 qemu_pixman_image_unref(surface->image);
1416 g_free(surface);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001417}
1418
Gerd Hoffmann06020b92014-07-11 13:56:51 +02001419bool console_has_gl(QemuConsole *con)
1420{
1421 return con->gl != NULL;
1422}
1423
Gerd Hoffmann4133fa72017-10-10 15:54:48 +02001424bool console_has_gl_dmabuf(QemuConsole *con)
1425{
1426 return con->gl != NULL && con->gl->ops->dpy_gl_scanout_dmabuf != NULL;
1427}
1428
Gerd Hoffmann52090892013-04-23 15:44:31 +02001429void register_displaychangelistener(DisplayChangeListener *dcl)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001430{
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001431 static const char nodev[] =
1432 "This VM has no graphic display device.";
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001433 static DisplaySurface *dummy;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001434 QemuConsole *con;
1435
Marc-André Lureaue0665c32017-04-06 14:05:12 +02001436 assert(!dcl->ds);
1437
Gerd Hoffmann06020b92014-07-11 13:56:51 +02001438 if (dcl->ops->dpy_gl_ctx_create) {
1439 /* display has opengl support */
1440 assert(dcl->con);
1441 if (dcl->con->gl) {
1442 fprintf(stderr, "can't register two opengl displays (%s, %s)\n",
1443 dcl->ops->dpy_name, dcl->con->gl->ops->dpy_name);
1444 exit(1);
1445 }
1446 dcl->con->gl = dcl;
1447 }
1448
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001449 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
Gerd Hoffmann52090892013-04-23 15:44:31 +02001450 dcl->ds = get_alloc_displaystate();
1451 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1452 gui_setup_refresh(dcl->ds);
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001453 if (dcl->con) {
1454 dcl->con->dcls++;
1455 con = dcl->con;
1456 } else {
1457 con = active_console;
1458 }
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001459 if (dcl->ops->dpy_gfx_switch) {
1460 if (con) {
1461 dcl->ops->dpy_gfx_switch(dcl, con->surface);
1462 } else {
1463 if (!dummy) {
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001464 dummy = qemu_create_message_surface(640, 480, nodev);
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001465 }
1466 dcl->ops->dpy_gfx_switch(dcl, dummy);
1467 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001468 }
Gerd Hoffmannaea79472014-05-22 11:27:13 +02001469 text_console_update_cursor(NULL);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001470}
1471
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +01001472void update_displaychangelistener(DisplayChangeListener *dcl,
1473 uint64_t interval)
1474{
1475 DisplayState *ds = dcl->ds;
1476
1477 dcl->update_interval = interval;
1478 if (!ds->refreshing && ds->update_interval > interval) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001479 timer_mod(ds->gui_timer, ds->last_update + interval);
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +01001480 }
1481}
1482
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001483void unregister_displaychangelistener(DisplayChangeListener *dcl)
1484{
1485 DisplayState *ds = dcl->ds;
1486 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001487 if (dcl->con) {
1488 dcl->con->dcls--;
1489 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001490 QLIST_REMOVE(dcl, next);
Gerd Hoffmann777c5f12017-11-09 11:51:54 +01001491 dcl->ds = NULL;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001492 gui_setup_refresh(ds);
1493}
1494
Gerd Hoffmanncf1ecc82015-03-12 12:51:13 +01001495static void dpy_set_ui_info_timer(void *opaque)
1496{
1497 QemuConsole *con = opaque;
1498
1499 con->hw_ops->ui_info(con->hw, con->head, &con->ui_info);
1500}
1501
Gerd Hoffmannb7fb49f2015-03-13 12:21:14 +01001502bool dpy_ui_info_supported(QemuConsole *con)
1503{
1504 return con->hw_ops->ui_info != NULL;
1505}
1506
Gerd Hoffmann6f90f3d2014-01-24 17:38:20 +01001507int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info)
1508{
1509 assert(con != NULL);
Gerd Hoffmann1185fde2016-05-30 10:41:13 +02001510
Gerd Hoffmannb7fb49f2015-03-13 12:21:14 +01001511 if (!dpy_ui_info_supported(con)) {
Gerd Hoffmanncf1ecc82015-03-12 12:51:13 +01001512 return -1;
Gerd Hoffmann6f90f3d2014-01-24 17:38:20 +01001513 }
Gerd Hoffmann1185fde2016-05-30 10:41:13 +02001514 if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
1515 /* nothing changed -- ignore */
1516 return 0;
1517 }
Gerd Hoffmanncf1ecc82015-03-12 12:51:13 +01001518
1519 /*
1520 * Typically we get a flood of these as the user resizes the window.
1521 * Wait until the dust has settled (one second without updates), then
1522 * go notify the guest.
1523 */
Gerd Hoffmann1185fde2016-05-30 10:41:13 +02001524 con->ui_info = *info;
Gerd Hoffmanncf1ecc82015-03-12 12:51:13 +01001525 timer_mod(con->ui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1526 return 0;
Gerd Hoffmann6f90f3d2014-01-24 17:38:20 +01001527}
1528
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001529void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001530{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001531 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001532 DisplayChangeListener *dcl;
Gerd Hoffmann06020b92014-07-11 13:56:51 +02001533 int width = w;
1534 int height = h;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001535
Gerd Hoffmann06020b92014-07-11 13:56:51 +02001536 if (con->surface) {
1537 width = surface_width(con->surface);
1538 height = surface_height(con->surface);
1539 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001540 x = MAX(x, 0);
1541 y = MAX(y, 0);
1542 x = MIN(x, width);
1543 y = MIN(y, height);
1544 w = MIN(w, width - x);
1545 h = MIN(h, height - y);
1546
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001547 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001548 return;
1549 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001550 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001551 if (con != (dcl->con ? dcl->con : active_console)) {
1552 continue;
1553 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001554 if (dcl->ops->dpy_gfx_update) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001555 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001556 }
1557 }
1558}
1559
Tina Zhang7cd0afe2018-04-27 17:11:05 +08001560void dpy_gfx_update_full(QemuConsole *con)
1561{
1562 if (!con->surface) {
1563 return;
1564 }
1565 dpy_gfx_update(con, 0, 0,
1566 surface_width(con->surface),
1567 surface_height(con->surface));
1568}
1569
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001570void dpy_gfx_replace_surface(QemuConsole *con,
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001571 DisplaySurface *surface)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001572{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001573 DisplayState *s = con->ds;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001574 DisplaySurface *old_surface = con->surface;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001575 DisplayChangeListener *dcl;
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001576
Gerd Hoffmann15400082017-09-06 16:21:09 +02001577 assert(old_surface != surface || surface == NULL);
Marc-André Lureau6905b932017-04-06 14:05:11 +02001578
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001579 con->surface = surface;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001580 QLIST_FOREACH(dcl, &s->listeners, next) {
1581 if (con != (dcl->con ? dcl->con : active_console)) {
1582 continue;
1583 }
1584 if (dcl->ops->dpy_gfx_switch) {
1585 dcl->ops->dpy_gfx_switch(dcl, surface);
1586 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001587 }
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001588 qemu_free_displaysurface(old_surface);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001589}
1590
Benjamin Herrenschmidt49743df2014-07-07 16:39:05 +10001591bool dpy_gfx_check_format(QemuConsole *con,
1592 pixman_format_code_t format)
1593{
1594 DisplayChangeListener *dcl;
1595 DisplayState *s = con->ds;
1596
1597 QLIST_FOREACH(dcl, &s->listeners, next) {
1598 if (dcl->con && dcl->con != con) {
1599 /* dcl bound to another console -> skip */
1600 continue;
1601 }
1602 if (dcl->ops->dpy_gfx_check_format) {
1603 if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
1604 return false;
1605 }
1606 } else {
1607 /* default is to whitelist native 32 bpp only */
1608 if (format != qemu_default_pixman_format(32, true)) {
1609 return false;
1610 }
1611 }
1612 }
1613 return true;
1614}
1615
Stefan Weil60751372014-05-02 22:48:30 +02001616static void dpy_refresh(DisplayState *s)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001617{
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001618 DisplayChangeListener *dcl;
1619
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001620 QLIST_FOREACH(dcl, &s->listeners, next) {
1621 if (dcl->ops->dpy_refresh) {
Gerd Hoffmann3f8f1312017-06-14 10:45:38 +02001622 dcl->ops->dpy_refresh(dcl);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001623 }
1624 }
1625}
1626
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001627void dpy_text_cursor(QemuConsole *con, int x, int y)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001628{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001629 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001630 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001631
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001632 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001633 return;
1634 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001635 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001636 if (con != (dcl->con ? dcl->con : active_console)) {
1637 continue;
1638 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001639 if (dcl->ops->dpy_text_cursor) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001640 dcl->ops->dpy_text_cursor(dcl, x, y);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001641 }
1642 }
1643}
1644
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001645void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001646{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001647 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001648 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001649
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001650 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001651 return;
1652 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001653 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001654 if (con != (dcl->con ? dcl->con : active_console)) {
1655 continue;
1656 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001657 if (dcl->ops->dpy_text_update) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001658 dcl->ops->dpy_text_update(dcl, x, y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001659 }
1660 }
1661}
1662
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001663void dpy_text_resize(QemuConsole *con, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001664{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001665 DisplayState *s = con->ds;
Chih-Min Chao9425c002015-04-09 02:04:13 +08001666 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001667
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001668 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001669 return;
1670 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001671 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001672 if (con != (dcl->con ? dcl->con : active_console)) {
1673 continue;
1674 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001675 if (dcl->ops->dpy_text_resize) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001676 dcl->ops->dpy_text_resize(dcl, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001677 }
1678 }
1679}
1680
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001681void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001682{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001683 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001684 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001685
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001686 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001687 return;
1688 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001689 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001690 if (con != (dcl->con ? dcl->con : active_console)) {
1691 continue;
1692 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001693 if (dcl->ops->dpy_mouse_set) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001694 dcl->ops->dpy_mouse_set(dcl, x, y, on);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001695 }
1696 }
1697}
1698
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001699void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001700{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001701 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001702 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001703
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001704 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001705 return;
1706 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001707 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001708 if (con != (dcl->con ? dcl->con : active_console)) {
1709 continue;
1710 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001711 if (dcl->ops->dpy_cursor_define) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001712 dcl->ops->dpy_cursor_define(dcl, cursor);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001713 }
1714 }
1715}
1716
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001717bool dpy_cursor_define_supported(QemuConsole *con)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001718{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001719 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001720 DisplayChangeListener *dcl;
1721
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001722 QLIST_FOREACH(dcl, &s->listeners, next) {
1723 if (dcl->ops->dpy_cursor_define) {
1724 return true;
1725 }
1726 }
1727 return false;
1728}
1729
Gerd Hoffmann06020b92014-07-11 13:56:51 +02001730QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
1731 struct QEMUGLParams *qparams)
1732{
1733 assert(con->gl);
1734 return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
1735}
1736
1737void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
1738{
1739 assert(con->gl);
1740 con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
1741}
1742
1743int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
1744{
1745 assert(con->gl);
1746 return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
1747}
1748
1749QEMUGLContext dpy_gl_ctx_get_current(QemuConsole *con)
1750{
1751 assert(con->gl);
1752 return con->gl->ops->dpy_gl_ctx_get_current(con->gl);
1753}
1754
Gerd Hoffmanneaa92c72017-02-21 10:37:17 +01001755void dpy_gl_scanout_disable(QemuConsole *con)
1756{
1757 assert(con->gl);
1758 if (con->gl->ops->dpy_gl_scanout_disable) {
1759 con->gl->ops->dpy_gl_scanout_disable(con->gl);
1760 } else {
1761 con->gl->ops->dpy_gl_scanout_texture(con->gl, 0, false, 0, 0,
1762 0, 0, 0, 0);
1763 }
1764}
1765
Gerd Hoffmannf4c36bd2017-02-21 10:37:16 +01001766void dpy_gl_scanout_texture(QemuConsole *con,
1767 uint32_t backing_id,
1768 bool backing_y_0_top,
1769 uint32_t backing_width,
1770 uint32_t backing_height,
1771 uint32_t x, uint32_t y,
1772 uint32_t width, uint32_t height)
Gerd Hoffmann06020b92014-07-11 13:56:51 +02001773{
1774 assert(con->gl);
Gerd Hoffmannf4c36bd2017-02-21 10:37:16 +01001775 con->gl->ops->dpy_gl_scanout_texture(con->gl, backing_id,
1776 backing_y_0_top,
1777 backing_width, backing_height,
1778 x, y, width, height);
Gerd Hoffmann06020b92014-07-11 13:56:51 +02001779}
1780
Gerd Hoffmann4133fa72017-10-10 15:54:48 +02001781void dpy_gl_scanout_dmabuf(QemuConsole *con,
1782 QemuDmaBuf *dmabuf)
1783{
1784 assert(con->gl);
1785 con->gl->ops->dpy_gl_scanout_dmabuf(con->gl, dmabuf);
1786}
1787
Gerd Hoffmann6e1f2cb2018-02-20 12:04:31 +01001788void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
1789 bool have_hot, uint32_t hot_x, uint32_t hot_y)
Gerd Hoffmann4133fa72017-10-10 15:54:48 +02001790{
1791 assert(con->gl);
1792
1793 if (con->gl->ops->dpy_gl_cursor_dmabuf) {
Gerd Hoffmann6e1f2cb2018-02-20 12:04:31 +01001794 con->gl->ops->dpy_gl_cursor_dmabuf(con->gl, dmabuf,
1795 have_hot, hot_x, hot_y);
1796 }
1797}
1798
1799void dpy_gl_cursor_position(QemuConsole *con,
1800 uint32_t pos_x, uint32_t pos_y)
1801{
1802 assert(con->gl);
1803
1804 if (con->gl->ops->dpy_gl_cursor_position) {
1805 con->gl->ops->dpy_gl_cursor_position(con->gl, pos_x, pos_y);
Gerd Hoffmann4133fa72017-10-10 15:54:48 +02001806 }
1807}
1808
1809void dpy_gl_release_dmabuf(QemuConsole *con,
1810 QemuDmaBuf *dmabuf)
1811{
1812 assert(con->gl);
1813
1814 if (con->gl->ops->dpy_gl_release_dmabuf) {
1815 con->gl->ops->dpy_gl_release_dmabuf(con->gl, dmabuf);
1816 }
1817}
1818
Gerd Hoffmann06020b92014-07-11 13:56:51 +02001819void dpy_gl_update(QemuConsole *con,
1820 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1821{
1822 assert(con->gl);
1823 con->gl->ops->dpy_gl_update(con->gl, x, y, w, h);
1824}
1825
Paolo Bonzini98b50082010-02-11 00:29:57 +01001826/***********************************************************/
1827/* register display */
1828
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001829/* console.c internal use only */
1830static DisplayState *get_alloc_displaystate(void)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001831{
1832 if (!display_state) {
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001833 display_state = g_new0(DisplayState, 1);
Gerd Hoffmannaea79472014-05-22 11:27:13 +02001834 cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1835 text_console_update_cursor, NULL);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001836 }
1837 return display_state;
1838}
1839
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001840/*
1841 * Called by main(), after creating QemuConsoles
1842 * and before initializing ui (sdl/vnc/...).
1843 */
1844DisplayState *init_displaystate(void)
1845{
Gerd Hoffmann43f420f2013-06-25 10:49:31 +02001846 gchar *name;
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02001847 QemuConsole *con;
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001848
Gerd Hoffmann333cb182014-06-02 14:07:18 +02001849 get_alloc_displaystate();
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02001850 QTAILQ_FOREACH(con, &consoles, next) {
1851 if (con->console_type != GRAPHIC_CONSOLE &&
1852 con->ds == NULL) {
1853 text_console_do_init(con->chr, display_state);
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001854 }
Gerd Hoffmann43f420f2013-06-25 10:49:31 +02001855
1856 /* Hook up into the qom tree here (not in new_console()), once
1857 * all QemuConsoles are created and the order / numbering
1858 * doesn't change any more */
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02001859 name = g_strdup_printf("console[%d]", con->index);
Gerd Hoffmann43f420f2013-06-25 10:49:31 +02001860 object_property_add_child(container_get(object_get_root(), "/backend"),
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02001861 name, OBJECT(con), &error_abort);
Gerd Hoffmann43f420f2013-06-25 10:49:31 +02001862 g_free(name);
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001863 }
1864
1865 return display_state;
1866}
1867
Gerd Hoffmann1c1f9492014-09-24 17:05:27 +02001868void graphic_console_set_hwops(QemuConsole *con,
1869 const GraphicHwOps *hw_ops,
1870 void *opaque)
1871{
1872 con->hw_ops = hw_ops;
1873 con->hw = opaque;
1874}
1875
Gerd Hoffmann56437062014-01-24 15:35:21 +01001876QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001877 const GraphicHwOps *hw_ops,
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001878 void *opaque)
bellarde7f0ad52004-07-14 17:28:59 +00001879{
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001880 static const char noinit[] =
1881 "Guest has not initialized the display (yet).";
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001882 int width = 640;
1883 int height = 480;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001884 QemuConsole *s;
aliguori3023f332009-01-16 19:04:14 +00001885 DisplayState *ds;
Gerd Hoffmann9588d672018-03-13 11:17:29 -06001886 DisplaySurface *surface;
aurel32f0f2f972009-01-16 21:13:49 +00001887
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001888 ds = get_alloc_displaystate();
Gerd Hoffmann9588d672018-03-13 11:17:29 -06001889 s = qemu_console_lookup_unused();
1890 if (s) {
1891 trace_console_gfx_reuse(s->index);
1892 if (s->surface) {
1893 width = surface_width(s->surface);
1894 height = surface_height(s->surface);
1895 }
1896 } else {
1897 trace_console_gfx_new();
1898 s = new_console(ds, GRAPHIC_CONSOLE, head);
1899 s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1900 dpy_set_ui_info_timer, s);
1901 }
Gerd Hoffmann1c1f9492014-09-24 17:05:27 +02001902 graphic_console_set_hwops(s, hw_ops, opaque);
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001903 if (dev) {
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001904 object_property_set_link(OBJECT(s), OBJECT(dev), "device",
1905 &error_abort);
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001906 }
aliguori3023f332009-01-16 19:04:14 +00001907
Gerd Hoffmann9588d672018-03-13 11:17:29 -06001908 surface = qemu_create_message_surface(width, height, noinit);
1909 dpy_gfx_replace_surface(s, surface);
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001910 return s;
pbrook95219892006-04-09 01:06:34 +00001911}
1912
Gerd Hoffmann9588d672018-03-13 11:17:29 -06001913static const GraphicHwOps unused_ops = {
1914 /* no callbacks */
1915};
1916
1917void graphic_console_close(QemuConsole *con)
1918{
1919 static const char unplugged[] =
1920 "Guest display has been unplugged";
1921 DisplaySurface *surface;
1922 int width = 640;
1923 int height = 480;
1924
1925 if (con->surface) {
1926 width = surface_width(con->surface);
1927 height = surface_height(con->surface);
1928 }
1929
1930 trace_console_gfx_close(con->index);
1931 object_property_set_link(OBJECT(con), NULL, "device", &error_abort);
1932 graphic_console_set_hwops(con, &unused_ops, NULL);
1933
1934 if (con->gl) {
1935 dpy_gl_scanout_disable(con);
1936 }
1937 surface = qemu_create_message_surface(width, height, unplugged);
1938 dpy_gfx_replace_surface(con, surface);
1939}
1940
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001941QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1942{
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02001943 QemuConsole *con;
1944
1945 QTAILQ_FOREACH(con, &consoles, next) {
1946 if (con->index == index) {
1947 return con;
1948 }
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001949 }
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02001950 return NULL;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001951}
1952
Gerd Hoffmann56437062014-01-24 15:35:21 +01001953QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
Gerd Hoffmann14a93642013-04-18 07:30:40 +02001954{
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02001955 QemuConsole *con;
Gerd Hoffmann14a93642013-04-18 07:30:40 +02001956 Object *obj;
Gerd Hoffmann56437062014-01-24 15:35:21 +01001957 uint32_t h;
Gerd Hoffmann14a93642013-04-18 07:30:40 +02001958
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02001959 QTAILQ_FOREACH(con, &consoles, next) {
1960 obj = object_property_get_link(OBJECT(con),
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001961 "device", &error_abort);
Gerd Hoffmann56437062014-01-24 15:35:21 +01001962 if (DEVICE(obj) != dev) {
1963 continue;
Gerd Hoffmann14a93642013-04-18 07:30:40 +02001964 }
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02001965 h = object_property_get_uint(OBJECT(con),
Marc-André Lureauad664c12017-06-07 20:36:32 +04001966 "head", &error_abort);
Gerd Hoffmann56437062014-01-24 15:35:21 +01001967 if (h != head) {
1968 continue;
1969 }
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02001970 return con;
Gerd Hoffmann14a93642013-04-18 07:30:40 +02001971 }
1972 return NULL;
1973}
1974
Gerd Hoffmannf2c1d542016-01-12 11:45:43 +01001975QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
1976 uint32_t head, Error **errp)
1977{
1978 DeviceState *dev;
1979 QemuConsole *con;
1980
1981 dev = qdev_find_recursive(sysbus_get_default(), device_id);
1982 if (dev == NULL) {
1983 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1984 "Device '%s' not found", device_id);
1985 return NULL;
1986 }
1987
1988 con = qemu_console_lookup_by_device(dev, head);
1989 if (con == NULL) {
1990 error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
1991 device_id, head);
1992 return NULL;
1993 }
1994
1995 return con;
1996}
1997
Gerd Hoffmann9588d672018-03-13 11:17:29 -06001998QemuConsole *qemu_console_lookup_unused(void)
1999{
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02002000 QemuConsole *con;
Gerd Hoffmann9588d672018-03-13 11:17:29 -06002001 Object *obj;
Gerd Hoffmann9588d672018-03-13 11:17:29 -06002002
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02002003 QTAILQ_FOREACH(con, &consoles, next) {
2004 if (con->hw_ops != &unused_ops) {
Gerd Hoffmann9588d672018-03-13 11:17:29 -06002005 continue;
2006 }
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02002007 obj = object_property_get_link(OBJECT(con),
Gerd Hoffmann9588d672018-03-13 11:17:29 -06002008 "device", &error_abort);
2009 if (obj != NULL) {
2010 continue;
2011 }
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02002012 return con;
Gerd Hoffmann9588d672018-03-13 11:17:29 -06002013 }
2014 return NULL;
2015}
2016
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01002017bool qemu_console_is_visible(QemuConsole *con)
pbrook95219892006-04-09 01:06:34 +00002018{
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01002019 return (con == active_console) || (con->dcls > 0);
bellarde7f0ad52004-07-14 17:28:59 +00002020}
2021
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01002022bool qemu_console_is_graphic(QemuConsole *con)
balrogc21bbcf2008-09-24 03:32:33 +00002023{
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01002024 if (con == NULL) {
2025 con = active_console;
2026 }
2027 return con && (con->console_type == GRAPHIC_CONSOLE);
2028}
2029
2030bool qemu_console_is_fixedsize(QemuConsole *con)
2031{
2032 if (con == NULL) {
2033 con = active_console;
2034 }
2035 return con && (con->console_type != TEXT_CONSOLE);
balrogc21bbcf2008-09-24 03:32:33 +00002036}
2037
Gerd Hoffmannf6078672016-09-23 09:50:27 +02002038bool qemu_console_is_gl_blocked(QemuConsole *con)
2039{
2040 assert(con != NULL);
2041 return con->gl_block;
2042}
2043
Gerd Hoffmann779ce882015-02-17 10:41:08 +01002044char *qemu_console_get_label(QemuConsole *con)
2045{
2046 if (con->console_type == GRAPHIC_CONSOLE) {
2047 if (con->device) {
2048 return g_strdup(object_get_typename(con->device));
2049 }
2050 return g_strdup("VGA");
2051 } else {
2052 if (con->chr && con->chr->label) {
2053 return g_strdup(con->chr->label);
2054 }
2055 return g_strdup_printf("vc%d", con->index);
2056 }
2057}
2058
Gerd Hoffmannd4c85332013-11-28 09:58:18 +01002059int qemu_console_get_index(QemuConsole *con)
2060{
2061 if (con == NULL) {
2062 con = active_console;
2063 }
2064 return con ? con->index : -1;
2065}
2066
Gerd Hoffmann56437062014-01-24 15:35:21 +01002067uint32_t qemu_console_get_head(QemuConsole *con)
2068{
2069 if (con == NULL) {
2070 con = active_console;
2071 }
2072 return con ? con->head : -1;
2073}
2074
Gerd Hoffmann6f90f3d2014-01-24 17:38:20 +01002075QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con)
2076{
2077 assert(con != NULL);
2078 return &con->ui_info;
2079}
2080
Gerd Hoffmannd4c85332013-11-28 09:58:18 +01002081int qemu_console_get_width(QemuConsole *con, int fallback)
2082{
2083 if (con == NULL) {
2084 con = active_console;
2085 }
2086 return con ? surface_width(con->surface) : fallback;
2087}
2088
2089int qemu_console_get_height(QemuConsole *con, int fallback)
2090{
2091 if (con == NULL) {
2092 con = active_console;
2093 }
2094 return con ? surface_height(con->surface) : fallback;
2095}
2096
Marc-André Lureau5bf5ada2017-01-05 17:30:29 +01002097static void vc_chr_set_echo(Chardev *chr, bool echo)
Paolo Bonzini41048332010-12-23 13:42:52 +01002098{
Marc-André Lureau777357d2016-12-07 18:39:10 +03002099 VCChardev *drv = VC_CHARDEV(chr);
Marc-André Lureau41ac54b2016-10-21 23:44:44 +03002100 QemuConsole *s = drv->console;
Paolo Bonzini41048332010-12-23 13:42:52 +01002101
2102 s->echo = echo;
2103}
2104
Gerd Hoffmannaea79472014-05-22 11:27:13 +02002105static void text_console_update_cursor_timer(void)
2106{
2107 timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
2108 + CONSOLE_CURSOR_PERIOD / 2);
2109}
2110
Jan Kiszkabf1bed82012-07-10 22:00:55 +02002111static void text_console_update_cursor(void *opaque)
2112{
Gerd Hoffmannaea79472014-05-22 11:27:13 +02002113 QemuConsole *s;
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02002114 int count = 0;
Jan Kiszkabf1bed82012-07-10 22:00:55 +02002115
Gerd Hoffmannaea79472014-05-22 11:27:13 +02002116 cursor_visible_phase = !cursor_visible_phase;
2117
Gerd Hoffmanncd6cd8f2018-05-07 11:54:24 +02002118 QTAILQ_FOREACH(s, &consoles, next) {
Gerd Hoffmannaea79472014-05-22 11:27:13 +02002119 if (qemu_console_is_graphic(s) ||
2120 !qemu_console_is_visible(s)) {
2121 continue;
2122 }
2123 count++;
2124 graphic_hw_invalidate(s);
2125 }
2126
2127 if (count) {
2128 text_console_update_cursor_timer();
2129 }
Jan Kiszkabf1bed82012-07-10 22:00:55 +02002130}
2131
Gerd Hoffmann380cd052013-03-13 14:04:18 +01002132static const GraphicHwOps text_console_ops = {
2133 .invalidate = text_console_invalidate,
2134 .text_update = text_console_update,
2135};
2136
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03002137static void text_console_do_init(Chardev *chr, DisplayState *ds)
bellarde7f0ad52004-07-14 17:28:59 +00002138{
Marc-André Lureau777357d2016-12-07 18:39:10 +03002139 VCChardev *drv = VC_CHARDEV(chr);
Marc-André Lureau41ac54b2016-10-21 23:44:44 +03002140 QemuConsole *s = drv->console;
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01002141 int g_width = 80 * FONT_WIDTH;
2142 int g_height = 24 * FONT_HEIGHT;
pbrook6d6f7c22006-03-11 15:35:30 +00002143
bellarde15d7372006-06-25 16:26:29 +00002144 s->out_fifo.buf = s->out_fifo_buf;
2145 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
Alex Blighbc72ad62013-08-21 16:03:08 +01002146 s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
aliguori3023f332009-01-16 19:04:14 +00002147 s->ds = ds;
ths3b46e622007-09-17 08:09:54 +00002148
bellarde7f0ad52004-07-14 17:28:59 +00002149 s->y_displayed = 0;
2150 s->y_base = 0;
2151 s->total_height = DEFAULT_BACKSCROLL;
2152 s->x = 0;
2153 s->y = 0;
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01002154 if (!s->surface) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01002155 if (active_console && active_console->surface) {
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01002156 g_width = surface_width(active_console->surface);
2157 g_height = surface_height(active_console->surface);
Gerd Hoffmann321f0482013-03-12 14:39:22 +01002158 }
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01002159 s->surface = qemu_create_displaysurface(g_width, g_height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01002160 }
pbrook6d6f7c22006-03-11 15:35:30 +00002161
Gerd Hoffmann380cd052013-03-13 14:04:18 +01002162 s->hw_ops = &text_console_ops;
balrog4d3b6f62008-02-10 16:33:14 +00002163 s->hw = s;
2164
pbrook6d6f7c22006-03-11 15:35:30 +00002165 /* Set text attribute defaults */
2166 s->t_attrib_default.bold = 0;
2167 s->t_attrib_default.uline = 0;
2168 s->t_attrib_default.blink = 0;
2169 s->t_attrib_default.invers = 0;
2170 s->t_attrib_default.unvisible = 0;
OGAWA Hirofumi40837332015-11-29 22:28:24 +09002171 s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
2172 s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
pbrook6d6f7c22006-03-11 15:35:30 +00002173 /* set current text attributes to default */
2174 s->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +00002175 text_console_resize(s);
2176
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01002177 if (chr->label) {
2178 char msg[128];
2179 int len;
2180
OGAWA Hirofumi40837332015-11-29 22:28:24 +09002181 s->t_attrib.bgcol = QEMU_COLOR_BLUE;
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01002182 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
Marc-André Lureau5bf5ada2017-01-05 17:30:29 +01002183 vc_chr_write(chr, (uint8_t *)msg, len);
Gerd Hoffmann735ba582009-12-08 13:11:40 +01002184 s->t_attrib = s->t_attrib_default;
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01002185 }
2186
Marc-André Lureau63618132016-12-14 14:23:02 +03002187 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
bellarde7f0ad52004-07-14 17:28:59 +00002188}
pbrookc60e08d2008-07-01 16:24:38 +00002189
Marc-André Lureau777357d2016-12-07 18:39:10 +03002190static void vc_chr_open(Chardev *chr,
2191 ChardevBackend *backend,
2192 bool *be_opened,
2193 Error **errp)
aliguori2796dae2009-01-16 20:23:27 +00002194{
Marc-André Lureau6f974c82016-12-07 15:13:50 +03002195 ChardevVC *vc = backend->u.vc.data;
Marc-André Lureau777357d2016-12-07 18:39:10 +03002196 VCChardev *drv = VC_CHARDEV(chr);
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02002197 QemuConsole *s;
Gerd Hoffmann702ec692013-02-25 15:52:32 +01002198 unsigned width = 0;
2199 unsigned height = 0;
aliguori2796dae2009-01-16 20:23:27 +00002200
Gerd Hoffmann702ec692013-02-25 15:52:32 +01002201 if (vc->has_width) {
2202 width = vc->width;
2203 } else if (vc->has_cols) {
2204 width = vc->cols * FONT_WIDTH;
2205 }
Paolo Bonzini491e1142010-12-23 13:42:51 +01002206
Gerd Hoffmann702ec692013-02-25 15:52:32 +01002207 if (vc->has_height) {
2208 height = vc->height;
2209 } else if (vc->has_rows) {
2210 height = vc->rows * FONT_HEIGHT;
2211 }
Paolo Bonzini491e1142010-12-23 13:42:51 +01002212
Gerd Hoffmann437fe102013-03-07 16:04:52 +01002213 trace_console_txt_new(width, height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01002214 if (width == 0 || height == 0) {
Kirill Batuzovafff2b12014-04-24 18:15:58 +04002215 s = new_console(NULL, TEXT_CONSOLE, 0);
Paolo Bonzini491e1142010-12-23 13:42:51 +01002216 } else {
Kirill Batuzovafff2b12014-04-24 18:15:58 +04002217 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01002218 s->surface = qemu_create_displaysurface(width, height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01002219 }
2220
2221 if (!s) {
Paolo Bonzinifa19d022015-09-29 15:49:06 +02002222 error_setg(errp, "cannot create text console");
Marc-André Lureau777357d2016-12-07 18:39:10 +03002223 return;
Paolo Bonzini491e1142010-12-23 13:42:51 +01002224 }
2225
2226 s->chr = chr;
Marc-André Lureau41ac54b2016-10-21 23:44:44 +03002227 drv->console = s;
Gerd Hoffmann64840c62013-03-07 17:08:29 +01002228
2229 if (display_state) {
2230 text_console_do_init(chr, display_state);
2231 }
aliguori2796dae2009-01-16 20:23:27 +00002232
Marc-André Lureau82878da2016-10-22 13:09:43 +03002233 /* console/chardev init sometimes completes elsewhere in a 2nd
2234 * stage, so defer OPENED events until they are fully initialized
2235 */
2236 *be_opened = false;
Anthony Liguorid82831d2013-02-20 07:43:19 -06002237}
2238
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01002239void qemu_console_resize(QemuConsole *s, int width, int height)
pbrookc60e08d2008-07-01 16:24:38 +00002240{
Gerd Hoffmann321f0482013-03-12 14:39:22 +01002241 DisplaySurface *surface;
2242
2243 assert(s->console_type == GRAPHIC_CONSOLE);
Marc-André Lureaucd958ed2016-08-26 13:47:11 +04002244
Gerd Hoffmann3ef0c572017-01-24 12:10:39 +01002245 if (s->surface && (s->surface->flags & QEMU_ALLOCATED_FLAG) &&
Marc-André Lureaucd958ed2016-08-26 13:47:11 +04002246 pixman_image_get_width(s->surface->image) == width &&
2247 pixman_image_get_height(s->surface->image) == height) {
2248 return;
2249 }
2250
Gerd Hoffmann321f0482013-03-12 14:39:22 +01002251 surface = qemu_create_displaysurface(width, height);
2252 dpy_gfx_replace_surface(s, surface);
pbrookc60e08d2008-07-01 16:24:38 +00002253}
balrog38334f72008-09-24 02:21:24 +00002254
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01002255DisplaySurface *qemu_console_surface(QemuConsole *console)
2256{
Gerd Hoffmann321f0482013-03-12 14:39:22 +01002257 return console->surface;
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01002258}
2259
malc0da2ea12009-01-23 19:56:19 +00002260PixelFormat qemu_default_pixelformat(int bpp)
2261{
Gerd Hoffmann56bd9ea2014-06-18 11:07:50 +02002262 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
2263 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
aliguori7d957bd2009-01-15 22:14:11 +00002264 return pf;
2265}
Anthony Liguori01f45d92013-03-05 23:21:32 +05302266
Gerd Hoffmanndb715892018-03-01 11:05:35 +01002267static QemuDisplay *dpys[DISPLAY_TYPE__MAX];
2268
2269void qemu_display_register(QemuDisplay *ui)
2270{
2271 assert(ui->type < DISPLAY_TYPE__MAX);
2272 dpys[ui->type] = ui;
2273}
2274
Gerd Hoffmann898f9d42018-03-01 11:05:40 +01002275bool qemu_display_find_default(DisplayOptions *opts)
2276{
2277 static DisplayType prio[] = {
2278 DISPLAY_TYPE_GTK,
2279 DISPLAY_TYPE_SDL,
2280 DISPLAY_TYPE_COCOA
2281 };
2282 int i;
2283
2284 for (i = 0; i < ARRAY_SIZE(prio); i++) {
2285 if (dpys[prio[i]] == NULL) {
Marc-André Lureauc809d1d2018-08-01 11:25:08 +02002286 ui_module_load_one(DisplayType_str(prio[i]));
Gerd Hoffmann61b4d9a2018-03-01 11:05:41 +01002287 }
2288 if (dpys[prio[i]] == NULL) {
Gerd Hoffmann898f9d42018-03-01 11:05:40 +01002289 continue;
2290 }
2291 opts->type = prio[i];
2292 return true;
2293 }
2294 return false;
2295}
2296
Gerd Hoffmanndb715892018-03-01 11:05:35 +01002297void qemu_display_early_init(DisplayOptions *opts)
2298{
2299 assert(opts->type < DISPLAY_TYPE__MAX);
2300 if (opts->type == DISPLAY_TYPE_NONE) {
2301 return;
2302 }
2303 if (dpys[opts->type] == NULL) {
Marc-André Lureauc809d1d2018-08-01 11:25:08 +02002304 ui_module_load_one(DisplayType_str(opts->type));
Gerd Hoffmann61b4d9a2018-03-01 11:05:41 +01002305 }
2306 if (dpys[opts->type] == NULL) {
Gerd Hoffmanndb715892018-03-01 11:05:35 +01002307 error_report("Display '%s' is not available.",
Marc-André Lureauc809d1d2018-08-01 11:25:08 +02002308 DisplayType_str(opts->type));
Gerd Hoffmanndb715892018-03-01 11:05:35 +01002309 exit(1);
2310 }
2311 if (dpys[opts->type]->early_init) {
2312 dpys[opts->type]->early_init(opts);
2313 }
2314}
2315
2316void qemu_display_init(DisplayState *ds, DisplayOptions *opts)
2317{
2318 assert(opts->type < DISPLAY_TYPE__MAX);
2319 if (opts->type == DISPLAY_TYPE_NONE) {
2320 return;
2321 }
2322 assert(dpys[opts->type] != NULL);
2323 dpys[opts->type]->init(ds, opts);
2324}
2325
Marc-André Lureau6f974c82016-12-07 15:13:50 +03002326void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp)
Gerd Hoffmann702ec692013-02-25 15:52:32 +01002327{
2328 int val;
Eric Blake21a933e2016-02-19 17:19:31 -07002329 ChardevVC *vc;
Gerd Hoffmann702ec692013-02-25 15:52:32 +01002330
Marc-André Lureau0b663b72016-12-09 11:04:51 +03002331 backend->type = CHARDEV_BACKEND_KIND_VC;
Eric Blake32bafa82016-03-17 16:48:37 -06002332 vc = backend->u.vc.data = g_new0(ChardevVC, 1);
Eric Blake21a933e2016-02-19 17:19:31 -07002333 qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
Gerd Hoffmann702ec692013-02-25 15:52:32 +01002334
2335 val = qemu_opt_get_number(opts, "width", 0);
2336 if (val != 0) {
Eric Blake21a933e2016-02-19 17:19:31 -07002337 vc->has_width = true;
2338 vc->width = val;
Gerd Hoffmann702ec692013-02-25 15:52:32 +01002339 }
2340
2341 val = qemu_opt_get_number(opts, "height", 0);
2342 if (val != 0) {
Eric Blake21a933e2016-02-19 17:19:31 -07002343 vc->has_height = true;
2344 vc->height = val;
Gerd Hoffmann702ec692013-02-25 15:52:32 +01002345 }
2346
2347 val = qemu_opt_get_number(opts, "cols", 0);
2348 if (val != 0) {
Eric Blake21a933e2016-02-19 17:19:31 -07002349 vc->has_cols = true;
2350 vc->cols = val;
Gerd Hoffmann702ec692013-02-25 15:52:32 +01002351 }
2352
2353 val = qemu_opt_get_number(opts, "rows", 0);
2354 if (val != 0) {
Eric Blake21a933e2016-02-19 17:19:31 -07002355 vc->has_rows = true;
2356 vc->rows = val;
Gerd Hoffmann702ec692013-02-25 15:52:32 +01002357 }
2358}
2359
Gerd Hoffmann95be0662013-04-17 09:45:10 +02002360static const TypeInfo qemu_console_info = {
2361 .name = TYPE_QEMU_CONSOLE,
2362 .parent = TYPE_OBJECT,
2363 .instance_size = sizeof(QemuConsole),
2364 .class_size = sizeof(QemuConsoleClass),
2365};
2366
Marc-André Lureau777357d2016-12-07 18:39:10 +03002367static void char_vc_class_init(ObjectClass *oc, void *data)
2368{
2369 ChardevClass *cc = CHARDEV_CLASS(oc);
2370
Marc-André Lureau88cace92016-12-09 00:50:12 +03002371 cc->parse = qemu_chr_parse_vc;
Marc-André Lureau777357d2016-12-07 18:39:10 +03002372 cc->open = vc_chr_open;
2373 cc->chr_write = vc_chr_write;
2374 cc->chr_set_echo = vc_chr_set_echo;
2375}
2376
2377static const TypeInfo char_vc_type_info = {
2378 .name = TYPE_CHARDEV_VC,
2379 .parent = TYPE_CHARDEV,
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03002380 .instance_size = sizeof(VCChardev),
Marc-André Lureau777357d2016-12-07 18:39:10 +03002381 .class_init = char_vc_class_init,
2382};
2383
2384void qemu_console_early_init(void)
2385{
2386 /* set the default vc driver */
2387 if (!object_class_by_name(TYPE_CHARDEV_VC)) {
2388 type_register(&char_vc_type_info);
Marc-André Lureau777357d2016-12-07 18:39:10 +03002389 }
2390}
2391
Anthony Liguori01f45d92013-03-05 23:21:32 +05302392static void register_types(void)
2393{
Gerd Hoffmann95be0662013-04-17 09:45:10 +02002394 type_register_static(&qemu_console_info);
Anthony Liguori01f45d92013-03-05 23:21:32 +05302395}
2396
2397type_init(register_types);