blob: 258af5dffff37193ff7b55914a10eba4f5f4a83e [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 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "qemu-common.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010025#include "ui/console.h"
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +020026#include "hw/qdev-core.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/timer.h"
Luiz Capitulinoad39cf62012-05-24 13:48:23 -030028#include "qmp-commands.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020029#include "sysemu/char.h"
Stefan Weilac860482013-11-10 14:20:16 +010030#include "trace.h"
Gerd Hoffmanna77549b2014-06-19 08:46:08 +020031#include "exec/memory.h"
bellarde7f0ad52004-07-14 17:28:59 +000032
33#define DEFAULT_BACKSCROLL 512
Jan Kiszkabf1bed82012-07-10 22:00:55 +020034#define CONSOLE_CURSOR_PERIOD 500
bellarde7f0ad52004-07-14 17:28:59 +000035
pbrook6d6f7c22006-03-11 15:35:30 +000036typedef struct TextAttributes {
37 uint8_t fgcol:4;
38 uint8_t bgcol:4;
39 uint8_t bold:1;
40 uint8_t uline:1;
41 uint8_t blink:1;
42 uint8_t invers:1;
43 uint8_t unvisible:1;
44} TextAttributes;
45
bellarde7f0ad52004-07-14 17:28:59 +000046typedef struct TextCell {
47 uint8_t ch;
pbrook6d6f7c22006-03-11 15:35:30 +000048 TextAttributes t_attrib;
bellarde7f0ad52004-07-14 17:28:59 +000049} TextCell;
50
51#define MAX_ESC_PARAMS 3
52
53enum TTYState {
54 TTY_STATE_NORM,
55 TTY_STATE_ESC,
56 TTY_STATE_CSI,
57};
58
bellarde15d7372006-06-25 16:26:29 +000059typedef struct QEMUFIFO {
60 uint8_t *buf;
61 int buf_size;
62 int count, wptr, rptr;
63} QEMUFIFO;
64
pbrook9596ebb2007-11-18 01:44:38 +000065static int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1)
bellarde15d7372006-06-25 16:26:29 +000066{
67 int l, len;
68
69 l = f->buf_size - f->count;
70 if (len1 > l)
71 len1 = l;
72 len = len1;
73 while (len > 0) {
74 l = f->buf_size - f->wptr;
75 if (l > len)
76 l = len;
77 memcpy(f->buf + f->wptr, buf, l);
78 f->wptr += l;
79 if (f->wptr >= f->buf_size)
80 f->wptr = 0;
81 buf += l;
82 len -= l;
83 }
84 f->count += len1;
85 return len1;
86}
87
pbrook9596ebb2007-11-18 01:44:38 +000088static int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1)
bellarde15d7372006-06-25 16:26:29 +000089{
90 int l, len;
91
92 if (len1 > f->count)
93 len1 = f->count;
94 len = len1;
95 while (len > 0) {
96 l = f->buf_size - f->rptr;
97 if (l > len)
98 l = len;
99 memcpy(buf, f->buf + f->rptr, l);
100 f->rptr += l;
101 if (f->rptr >= f->buf_size)
102 f->rptr = 0;
103 buf += l;
104 len -= l;
105 }
106 f->count -= len1;
107 return len1;
108}
109
thsaf3a9032007-07-11 23:14:59 +0000110typedef enum {
111 GRAPHIC_CONSOLE,
balrogc21bbcf2008-09-24 03:32:33 +0000112 TEXT_CONSOLE,
113 TEXT_CONSOLE_FIXED_SIZE
Anthony Liguoric227f092009-10-01 16:12:16 -0500114} console_type_t;
thsaf3a9032007-07-11 23:14:59 +0000115
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200116struct QemuConsole {
Gerd Hoffmann95be0662013-04-17 09:45:10 +0200117 Object parent;
118
Jan Kiszkaf81bdef2011-09-16 00:48:07 +0200119 int index;
Anthony Liguoric227f092009-10-01 16:12:16 -0500120 console_type_t console_type;
bellarde7f0ad52004-07-14 17:28:59 +0000121 DisplayState *ds;
Gerd Hoffmann321f0482013-03-12 14:39:22 +0100122 DisplaySurface *surface;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +0100123 int dcls;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200124
pbrook95219892006-04-09 01:06:34 +0000125 /* Graphic console state. */
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +0200126 Object *device;
Gerd Hoffmann56437062014-01-24 15:35:21 +0100127 uint32_t head;
Gerd Hoffmann6f90f3d2014-01-24 17:38:20 +0100128 QemuUIInfo ui_info;
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100129 const GraphicHwOps *hw_ops;
pbrook95219892006-04-09 01:06:34 +0000130 void *hw;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200131
132 /* Text console state */
bellarde7f0ad52004-07-14 17:28:59 +0000133 int width;
134 int height;
135 int total_height;
136 int backscroll_height;
bellarde7f0ad52004-07-14 17:28:59 +0000137 int x, y;
thsadb47962007-01-16 23:02:36 +0000138 int x_saved, y_saved;
bellarde7f0ad52004-07-14 17:28:59 +0000139 int y_displayed;
140 int y_base;
pbrook6d6f7c22006-03-11 15:35:30 +0000141 TextAttributes t_attrib_default; /* default text attributes */
142 TextAttributes t_attrib; /* currently active text attributes */
bellarde7f0ad52004-07-14 17:28:59 +0000143 TextCell *cells;
balrog4d3b6f62008-02-10 16:33:14 +0000144 int text_x[2], text_y[2], cursor_invalidate;
Paolo Bonzini41048332010-12-23 13:42:52 +0100145 int echo;
bellarde7f0ad52004-07-14 17:28:59 +0000146
pbrook14778c22009-01-21 03:02:52 +0000147 int update_x0;
148 int update_y0;
149 int update_x1;
150 int update_y1;
151
bellarde7f0ad52004-07-14 17:28:59 +0000152 enum TTYState state;
153 int esc_params[MAX_ESC_PARAMS];
154 int nb_esc_params;
155
pbrooke5b0bc42007-01-27 23:46:43 +0000156 CharDriverState *chr;
bellarde15d7372006-06-25 16:26:29 +0000157 /* fifo for key pressed */
158 QEMUFIFO out_fifo;
159 uint8_t out_fifo_buf[16];
160 QEMUTimer *kbd_timer;
bellarde7f0ad52004-07-14 17:28:59 +0000161};
162
Gerd Hoffmann27be5582013-03-13 12:25:25 +0100163struct DisplayState {
Stefan Weil1246b252013-12-01 08:49:47 +0100164 QEMUTimer *gui_timer;
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100165 uint64_t last_update;
166 uint64_t update_interval;
167 bool refreshing;
Gerd Hoffmann27be5582013-03-13 12:25:25 +0100168 bool have_gfx;
169 bool have_text;
170
171 QLIST_HEAD(, DisplayChangeListener) listeners;
172};
173
Paolo Bonzini98b50082010-02-11 00:29:57 +0100174static DisplayState *display_state;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200175static QemuConsole *active_console;
Gerd Hoffmanna1d2db02014-05-26 10:36:35 +0200176static QemuConsole **consoles;
bellarde7f0ad52004-07-14 17:28:59 +0000177static int nb_consoles = 0;
Gerd Hoffmannaea79472014-05-22 11:27:13 +0200178static bool cursor_visible_phase;
179static QEMUTimer *cursor_timer;
bellarde7f0ad52004-07-14 17:28:59 +0000180
Gerd Hoffmann64840c62013-03-07 17:08:29 +0100181static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100182static void dpy_refresh(DisplayState *s);
Gerd Hoffmann52090892013-04-23 15:44:31 +0200183static DisplayState *get_alloc_displaystate(void);
Gerd Hoffmannaea79472014-05-22 11:27:13 +0200184static void text_console_update_cursor_timer(void);
185static void text_console_update_cursor(void *opaque);
Gerd Hoffmann64840c62013-03-07 17:08:29 +0100186
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100187static void gui_update(void *opaque)
188{
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100189 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
190 uint64_t dcl_interval;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100191 DisplayState *ds = opaque;
192 DisplayChangeListener *dcl;
Gerd Hoffmanndea1b0b2013-03-19 15:01:02 +0100193 int i;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100194
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100195 ds->refreshing = true;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100196 dpy_refresh(ds);
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100197 ds->refreshing = false;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100198
199 QLIST_FOREACH(dcl, &ds->listeners, next) {
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100200 dcl_interval = dcl->update_interval ?
201 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
202 if (interval > dcl_interval) {
203 interval = dcl_interval;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100204 }
205 }
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100206 if (ds->update_interval != interval) {
207 ds->update_interval = interval;
Gerd Hoffmanndea1b0b2013-03-19 15:01:02 +0100208 for (i = 0; i < nb_consoles; i++) {
209 if (consoles[i]->hw_ops->update_interval) {
210 consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval);
211 }
212 }
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100213 trace_console_refresh(interval);
214 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100215 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
216 timer_mod(ds->gui_timer, ds->last_update + interval);
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100217}
218
219static void gui_setup_refresh(DisplayState *ds)
220{
221 DisplayChangeListener *dcl;
222 bool need_timer = false;
223 bool have_gfx = false;
224 bool have_text = false;
225
226 QLIST_FOREACH(dcl, &ds->listeners, next) {
227 if (dcl->ops->dpy_refresh != NULL) {
228 need_timer = true;
229 }
230 if (dcl->ops->dpy_gfx_update != NULL) {
231 have_gfx = true;
232 }
233 if (dcl->ops->dpy_text_update != NULL) {
234 have_text = true;
235 }
236 }
237
238 if (need_timer && ds->gui_timer == NULL) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100239 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
240 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100241 }
242 if (!need_timer && ds->gui_timer != NULL) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100243 timer_del(ds->gui_timer);
244 timer_free(ds->gui_timer);
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100245 ds->gui_timer = NULL;
246 }
247
248 ds->have_gfx = have_gfx;
249 ds->have_text = have_text;
250}
251
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100252void graphic_hw_update(QemuConsole *con)
pbrook95219892006-04-09 01:06:34 +0000253{
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100254 if (!con) {
255 con = active_console;
256 }
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100257 if (con && con->hw_ops->gfx_update) {
258 con->hw_ops->gfx_update(con->hw);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100259 }
pbrook95219892006-04-09 01:06:34 +0000260}
261
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100262void graphic_hw_invalidate(QemuConsole *con)
pbrook95219892006-04-09 01:06:34 +0000263{
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100264 if (!con) {
265 con = active_console;
266 }
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100267 if (con && con->hw_ops->invalidate) {
268 con->hw_ops->invalidate(con->hw);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100269 }
pbrook95219892006-04-09 01:06:34 +0000270}
271
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100272static void ppm_save(const char *filename, struct DisplaySurface *ds,
273 Error **errp)
274{
275 int width = pixman_image_get_width(ds->image);
276 int height = pixman_image_get_height(ds->image);
Gerd Hoffmanncdd5b932013-04-23 13:26:59 +0200277 int fd;
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100278 FILE *f;
279 int y;
280 int ret;
281 pixman_image_t *linebuf;
282
283 trace_ppm_save(filename, ds);
Gerd Hoffmanncdd5b932013-04-23 13:26:59 +0200284 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
285 if (fd == -1) {
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100286 error_setg(errp, "failed to open file '%s': %s", filename,
287 strerror(errno));
288 return;
289 }
Gerd Hoffmanncdd5b932013-04-23 13:26:59 +0200290 f = fdopen(fd, "wb");
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100291 ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
292 if (ret < 0) {
293 linebuf = NULL;
294 goto write_err;
295 }
296 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
297 for (y = 0; y < height; y++) {
298 qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
299 clearerr(f);
300 ret = fwrite(pixman_image_get_data(linebuf), 1,
301 pixman_image_get_stride(linebuf), f);
302 (void)ret;
303 if (ferror(f)) {
304 goto write_err;
305 }
306 }
307
308out:
309 qemu_pixman_image_unref(linebuf);
310 fclose(f);
311 return;
312
313write_err:
314 error_setg(errp, "failed to write to file '%s': %s", filename,
315 strerror(errno));
316 unlink(filename);
317 goto out;
318}
319
Luiz Capitulinoad39cf62012-05-24 13:48:23 -0300320void qmp_screendump(const char *filename, Error **errp)
pbrook95219892006-04-09 01:06:34 +0000321{
Gerd Hoffmann284d1c62013-03-15 15:45:54 +0100322 QemuConsole *con = qemu_console_lookup_by_index(0);
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100323 DisplaySurface *surface;
balrog8571c052008-07-19 13:04:26 +0000324
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100325 if (con == NULL) {
326 error_setg(errp, "There is no QemuConsole I can screendump from.");
327 return;
Jan Kiszkaf81bdef2011-09-16 00:48:07 +0200328 }
329
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100330 graphic_hw_update(con);
331 surface = qemu_console_surface(con);
332 ppm_save(filename, surface, errp);
pbrook95219892006-04-09 01:06:34 +0000333}
334
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100335void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
balrog4d3b6f62008-02-10 16:33:14 +0000336{
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100337 if (!con) {
338 con = active_console;
339 }
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100340 if (con && con->hw_ops->text_update) {
341 con->hw_ops->text_update(con->hw, chardata);
342 }
balrog4d3b6f62008-02-10 16:33:14 +0000343}
344
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100345static void vga_fill_rect(QemuConsole *con,
346 int posx, int posy, int width, int height,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100347 pixman_color_t color)
bellarde7f0ad52004-07-14 17:28:59 +0000348{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100349 DisplaySurface *surface = qemu_console_surface(con);
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100350 pixman_rectangle16_t rect = {
351 .x = posx, .y = posy, .width = width, .height = height
352 };
ths3b46e622007-09-17 08:09:54 +0000353
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100354 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100355 &color, 1, &rect);
bellarde7f0ad52004-07-14 17:28:59 +0000356}
357
358/* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100359static void vga_bitblt(QemuConsole *con,
360 int xs, int ys, int xd, int yd, int w, int h)
bellarde7f0ad52004-07-14 17:28:59 +0000361{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100362 DisplaySurface *surface = qemu_console_surface(con);
bellarde7f0ad52004-07-14 17:28:59 +0000363
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100364 pixman_image_composite(PIXMAN_OP_SRC,
365 surface->image, NULL, surface->image,
366 xs, ys, 0, 0, xd, yd, w, h);
bellarde7f0ad52004-07-14 17:28:59 +0000367}
368
369/***********************************************************/
370/* basic char display */
371
372#define FONT_HEIGHT 16
373#define FONT_WIDTH 8
374
375#include "vgafont.h"
376
Devin J. Pohlydf00bed2011-09-07 15:44:36 -0400377#ifndef CONFIG_CURSES
pbrook6d6f7c22006-03-11 15:35:30 +0000378enum color_names {
379 COLOR_BLACK = 0,
380 COLOR_RED = 1,
381 COLOR_GREEN = 2,
382 COLOR_YELLOW = 3,
383 COLOR_BLUE = 4,
384 COLOR_MAGENTA = 5,
385 COLOR_CYAN = 6,
386 COLOR_WHITE = 7
387};
Devin J. Pohlydf00bed2011-09-07 15:44:36 -0400388#endif
pbrook6d6f7c22006-03-11 15:35:30 +0000389
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100390#define QEMU_RGB(r, g, b) \
391 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
392
393static const pixman_color_t color_table_rgb[2][8] = {
pbrook6d6f7c22006-03-11 15:35:30 +0000394 { /* dark */
bellard26489842006-06-25 17:37:36 +0000395 QEMU_RGB(0x00, 0x00, 0x00), /* black */
396 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
397 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
398 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
399 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
400 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
401 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
402 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
pbrook6d6f7c22006-03-11 15:35:30 +0000403 },
404 { /* bright */
bellard26489842006-06-25 17:37:36 +0000405 QEMU_RGB(0x00, 0x00, 0x00), /* black */
406 QEMU_RGB(0xff, 0x00, 0x00), /* red */
407 QEMU_RGB(0x00, 0xff, 0x00), /* green */
408 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
409 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
410 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
411 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
412 QEMU_RGB(0xff, 0xff, 0xff), /* white */
pbrook6d6f7c22006-03-11 15:35:30 +0000413 }
bellarde7f0ad52004-07-14 17:28:59 +0000414};
415
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100416static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
pbrook6d6f7c22006-03-11 15:35:30 +0000417 TextAttributes *t_attrib)
bellarde7f0ad52004-07-14 17:28:59 +0000418{
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100419 static pixman_image_t *glyphs[256];
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100420 DisplaySurface *surface = qemu_console_surface(s);
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100421 pixman_color_t fgcol, bgcol;
pbrook6d6f7c22006-03-11 15:35:30 +0000422
423 if (t_attrib->invers) {
Gerd Hoffmanncf6f0542013-03-06 09:50:51 +0100424 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
425 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
pbrook6d6f7c22006-03-11 15:35:30 +0000426 } else {
Gerd Hoffmanncf6f0542013-03-06 09:50:51 +0100427 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
428 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
pbrook6d6f7c22006-03-11 15:35:30 +0000429 }
bellarde7f0ad52004-07-14 17:28:59 +0000430
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100431 if (!glyphs[ch]) {
432 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
bellarde7f0ad52004-07-14 17:28:59 +0000433 }
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100434 qemu_pixman_glyph_render(glyphs[ch], surface->image,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100435 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
bellarde7f0ad52004-07-14 17:28:59 +0000436}
437
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200438static void text_console_resize(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000439{
440 TextCell *cells, *c, *c1;
441 int w1, x, y, last_width;
442
443 last_width = s->width;
Gerd Hoffmann36671fb2013-03-13 10:14:52 +0100444 s->width = surface_width(s->surface) / FONT_WIDTH;
445 s->height = surface_height(s->surface) / FONT_HEIGHT;
bellarde7f0ad52004-07-14 17:28:59 +0000446
447 w1 = last_width;
448 if (s->width < w1)
449 w1 = s->width;
450
Anthony Liguori7267c092011-08-20 22:09:37 -0500451 cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
bellarde7f0ad52004-07-14 17:28:59 +0000452 for(y = 0; y < s->total_height; y++) {
453 c = &cells[y * s->width];
454 if (w1 > 0) {
455 c1 = &s->cells[y * last_width];
456 for(x = 0; x < w1; x++) {
457 *c++ = *c1++;
458 }
459 }
460 for(x = w1; x < s->width; x++) {
461 c->ch = ' ';
pbrook6d6f7c22006-03-11 15:35:30 +0000462 c->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +0000463 c++;
464 }
465 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500466 g_free(s->cells);
bellarde7f0ad52004-07-14 17:28:59 +0000467 s->cells = cells;
468}
469
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200470static inline void text_update_xy(QemuConsole *s, int x, int y)
balrog4d3b6f62008-02-10 16:33:14 +0000471{
472 s->text_x[0] = MIN(s->text_x[0], x);
473 s->text_x[1] = MAX(s->text_x[1], x);
474 s->text_y[0] = MIN(s->text_y[0], y);
475 s->text_y[1] = MAX(s->text_y[1], y);
476}
477
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200478static void invalidate_xy(QemuConsole *s, int x, int y)
pbrook14778c22009-01-21 03:02:52 +0000479{
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200480 if (!qemu_console_is_visible(s)) {
481 return;
482 }
pbrook14778c22009-01-21 03:02:52 +0000483 if (s->update_x0 > x * FONT_WIDTH)
484 s->update_x0 = x * FONT_WIDTH;
485 if (s->update_y0 > y * FONT_HEIGHT)
486 s->update_y0 = y * FONT_HEIGHT;
487 if (s->update_x1 < (x + 1) * FONT_WIDTH)
488 s->update_x1 = (x + 1) * FONT_WIDTH;
489 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
490 s->update_y1 = (y + 1) * FONT_HEIGHT;
491}
492
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200493static void update_xy(QemuConsole *s, int x, int y)
bellarde7f0ad52004-07-14 17:28:59 +0000494{
495 TextCell *c;
496 int y1, y2;
497
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100498 if (s->ds->have_text) {
499 text_update_xy(s, x, y);
500 }
501
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200502 y1 = (s->y_base + y) % s->total_height;
503 y2 = y1 - s->y_displayed;
504 if (y2 < 0) {
505 y2 += s->total_height;
506 }
507 if (y2 < s->height) {
508 c = &s->cells[y1 * s->width + x];
509 vga_putcharxy(s, x, y2, c->ch,
510 &(c->t_attrib));
511 invalidate_xy(s, x, y2);
bellarde7f0ad52004-07-14 17:28:59 +0000512 }
513}
514
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200515static void console_show_cursor(QemuConsole *s, int show)
bellarde7f0ad52004-07-14 17:28:59 +0000516{
517 TextCell *c;
518 int y, y1;
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100519 int x = s->x;
bellarde7f0ad52004-07-14 17:28:59 +0000520
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100521 if (s->ds->have_text) {
522 s->cursor_invalidate = 1;
523 }
balrog4d3b6f62008-02-10 16:33:14 +0000524
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200525 if (x >= s->width) {
526 x = s->width - 1;
527 }
528 y1 = (s->y_base + s->y) % s->total_height;
529 y = y1 - s->y_displayed;
530 if (y < 0) {
531 y += s->total_height;
532 }
533 if (y < s->height) {
534 c = &s->cells[y1 * s->width + x];
Gerd Hoffmannaea79472014-05-22 11:27:13 +0200535 if (show && cursor_visible_phase) {
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200536 TextAttributes t_attrib = s->t_attrib_default;
537 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
538 vga_putcharxy(s, x, y, c->ch, &t_attrib);
539 } else {
540 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
thsed8276a2007-02-10 22:37:56 +0000541 }
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200542 invalidate_xy(s, x, y);
bellarde7f0ad52004-07-14 17:28:59 +0000543 }
544}
545
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200546static void console_refresh(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000547{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100548 DisplaySurface *surface = qemu_console_surface(s);
bellarde7f0ad52004-07-14 17:28:59 +0000549 TextCell *c;
550 int x, y, y1;
551
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200552 if (s->ds->have_text) {
balrog4d3b6f62008-02-10 16:33:14 +0000553 s->text_x[0] = 0;
554 s->text_y[0] = 0;
555 s->text_x[1] = s->width - 1;
556 s->text_y[1] = s->height - 1;
557 s->cursor_invalidate = 1;
balrog4d3b6f62008-02-10 16:33:14 +0000558 }
bellarde7f0ad52004-07-14 17:28:59 +0000559
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200560 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
561 color_table_rgb[0][COLOR_BLACK]);
562 y1 = s->y_displayed;
563 for (y = 0; y < s->height; y++) {
564 c = s->cells + y1 * s->width;
565 for (x = 0; x < s->width; x++) {
566 vga_putcharxy(s, x, y, c->ch,
567 &(c->t_attrib));
568 c++;
bellarde7f0ad52004-07-14 17:28:59 +0000569 }
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200570 if (++y1 == s->total_height) {
571 y1 = 0;
572 }
bellarde7f0ad52004-07-14 17:28:59 +0000573 }
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200574 console_show_cursor(s, 1);
575 dpy_gfx_update(s, 0, 0,
576 surface_width(surface), surface_height(surface));
bellarde7f0ad52004-07-14 17:28:59 +0000577}
578
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100579static void console_scroll(QemuConsole *s, int ydelta)
bellarde7f0ad52004-07-14 17:28:59 +0000580{
bellarde7f0ad52004-07-14 17:28:59 +0000581 int i, y1;
ths3b46e622007-09-17 08:09:54 +0000582
bellarde7f0ad52004-07-14 17:28:59 +0000583 if (ydelta > 0) {
584 for(i = 0; i < ydelta; i++) {
585 if (s->y_displayed == s->y_base)
586 break;
587 if (++s->y_displayed == s->total_height)
588 s->y_displayed = 0;
589 }
590 } else {
591 ydelta = -ydelta;
592 i = s->backscroll_height;
593 if (i > s->total_height - s->height)
594 i = s->total_height - s->height;
595 y1 = s->y_base - i;
596 if (y1 < 0)
597 y1 += s->total_height;
598 for(i = 0; i < ydelta; i++) {
599 if (s->y_displayed == y1)
600 break;
601 if (--s->y_displayed < 0)
602 s->y_displayed = s->total_height - 1;
603 }
604 }
605 console_refresh(s);
606}
607
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200608static void console_put_lf(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000609{
610 TextCell *c;
611 int x, y1;
612
bellarde7f0ad52004-07-14 17:28:59 +0000613 s->y++;
614 if (s->y >= s->height) {
615 s->y = s->height - 1;
pbrook6d6f7c22006-03-11 15:35:30 +0000616
bellarde7f0ad52004-07-14 17:28:59 +0000617 if (s->y_displayed == s->y_base) {
618 if (++s->y_displayed == s->total_height)
619 s->y_displayed = 0;
620 }
621 if (++s->y_base == s->total_height)
622 s->y_base = 0;
623 if (s->backscroll_height < s->total_height)
624 s->backscroll_height++;
625 y1 = (s->y_base + s->height - 1) % s->total_height;
626 c = &s->cells[y1 * s->width];
627 for(x = 0; x < s->width; x++) {
628 c->ch = ' ';
pbrook6d6f7c22006-03-11 15:35:30 +0000629 c->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +0000630 c++;
631 }
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200632 if (s->y_displayed == s->y_base) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100633 if (s->ds->have_text) {
balrog4d3b6f62008-02-10 16:33:14 +0000634 s->text_x[0] = 0;
635 s->text_y[0] = 0;
636 s->text_x[1] = s->width - 1;
637 s->text_y[1] = s->height - 1;
balrog4d3b6f62008-02-10 16:33:14 +0000638 }
639
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200640 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
641 s->width * FONT_WIDTH,
642 (s->height - 1) * FONT_HEIGHT);
643 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
644 s->width * FONT_WIDTH, FONT_HEIGHT,
645 color_table_rgb[0][s->t_attrib_default.bgcol]);
646 s->update_x0 = 0;
647 s->update_y0 = 0;
648 s->update_x1 = s->width * FONT_WIDTH;
649 s->update_y1 = s->height * FONT_HEIGHT;
bellarde7f0ad52004-07-14 17:28:59 +0000650 }
651 }
652}
653
pbrook6d6f7c22006-03-11 15:35:30 +0000654/* Set console attributes depending on the current escape codes.
655 * NOTE: I know this code is not very efficient (checking every color for it
656 * self) but it is more readable and better maintainable.
657 */
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200658static void console_handle_escape(QemuConsole *s)
pbrook6d6f7c22006-03-11 15:35:30 +0000659{
660 int i;
661
pbrook6d6f7c22006-03-11 15:35:30 +0000662 for (i=0; i<s->nb_esc_params; i++) {
663 switch (s->esc_params[i]) {
664 case 0: /* reset all console attributes to default */
665 s->t_attrib = s->t_attrib_default;
666 break;
667 case 1:
668 s->t_attrib.bold = 1;
669 break;
670 case 4:
671 s->t_attrib.uline = 1;
672 break;
673 case 5:
674 s->t_attrib.blink = 1;
675 break;
676 case 7:
677 s->t_attrib.invers = 1;
678 break;
679 case 8:
680 s->t_attrib.unvisible = 1;
681 break;
682 case 22:
683 s->t_attrib.bold = 0;
684 break;
685 case 24:
686 s->t_attrib.uline = 0;
687 break;
688 case 25:
689 s->t_attrib.blink = 0;
690 break;
691 case 27:
692 s->t_attrib.invers = 0;
693 break;
694 case 28:
695 s->t_attrib.unvisible = 0;
696 break;
697 /* set foreground color */
698 case 30:
699 s->t_attrib.fgcol=COLOR_BLACK;
700 break;
701 case 31:
702 s->t_attrib.fgcol=COLOR_RED;
703 break;
704 case 32:
705 s->t_attrib.fgcol=COLOR_GREEN;
706 break;
707 case 33:
708 s->t_attrib.fgcol=COLOR_YELLOW;
709 break;
710 case 34:
711 s->t_attrib.fgcol=COLOR_BLUE;
712 break;
713 case 35:
714 s->t_attrib.fgcol=COLOR_MAGENTA;
715 break;
716 case 36:
717 s->t_attrib.fgcol=COLOR_CYAN;
718 break;
719 case 37:
720 s->t_attrib.fgcol=COLOR_WHITE;
721 break;
722 /* set background color */
723 case 40:
724 s->t_attrib.bgcol=COLOR_BLACK;
725 break;
726 case 41:
727 s->t_attrib.bgcol=COLOR_RED;
728 break;
729 case 42:
730 s->t_attrib.bgcol=COLOR_GREEN;
731 break;
732 case 43:
733 s->t_attrib.bgcol=COLOR_YELLOW;
734 break;
735 case 44:
736 s->t_attrib.bgcol=COLOR_BLUE;
737 break;
738 case 45:
739 s->t_attrib.bgcol=COLOR_MAGENTA;
740 break;
741 case 46:
742 s->t_attrib.bgcol=COLOR_CYAN;
743 break;
744 case 47:
745 s->t_attrib.bgcol=COLOR_WHITE;
746 break;
747 }
748 }
749}
750
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200751static void console_clear_xy(QemuConsole *s, int x, int y)
thsadb47962007-01-16 23:02:36 +0000752{
753 int y1 = (s->y_base + y) % s->total_height;
754 TextCell *c = &s->cells[y1 * s->width + x];
755 c->ch = ' ';
756 c->t_attrib = s->t_attrib_default;
thsadb47962007-01-16 23:02:36 +0000757 update_xy(s, x, y);
758}
759
Ian Campbell3eea5492012-09-04 10:26:09 -0500760/* set cursor, checking bounds */
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200761static void set_cursor(QemuConsole *s, int x, int y)
Ian Campbell3eea5492012-09-04 10:26:09 -0500762{
763 if (x < 0) {
764 x = 0;
765 }
766 if (y < 0) {
767 y = 0;
768 }
769 if (y >= s->height) {
770 y = s->height - 1;
771 }
772 if (x >= s->width) {
773 x = s->width - 1;
774 }
775
776 s->x = x;
777 s->y = y;
778}
779
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200780static void console_putchar(QemuConsole *s, int ch)
bellarde7f0ad52004-07-14 17:28:59 +0000781{
782 TextCell *c;
thsadb47962007-01-16 23:02:36 +0000783 int y1, i;
784 int x, y;
bellarde7f0ad52004-07-14 17:28:59 +0000785
786 switch(s->state) {
787 case TTY_STATE_NORM:
788 switch(ch) {
pbrook6d6f7c22006-03-11 15:35:30 +0000789 case '\r': /* carriage return */
bellarde7f0ad52004-07-14 17:28:59 +0000790 s->x = 0;
791 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000792 case '\n': /* newline */
bellarde7f0ad52004-07-14 17:28:59 +0000793 console_put_lf(s);
794 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000795 case '\b': /* backspace */
ths5fafdf22007-09-16 21:08:06 +0000796 if (s->x > 0)
bellarde15d7372006-06-25 16:26:29 +0000797 s->x--;
pbrook6d6f7c22006-03-11 15:35:30 +0000798 break;
799 case '\t': /* tabspace */
800 if (s->x + (8 - (s->x % 8)) > s->width) {
bellardbd468842006-07-14 20:24:31 +0000801 s->x = 0;
pbrook6d6f7c22006-03-11 15:35:30 +0000802 console_put_lf(s);
803 } else {
804 s->x = s->x + (8 - (s->x % 8));
805 }
806 break;
807 case '\a': /* alert aka. bell */
808 /* TODO: has to be implemented */
809 break;
thsadb47962007-01-16 23:02:36 +0000810 case 14:
811 /* SI (shift in), character set 0 (ignored) */
812 break;
813 case 15:
814 /* SO (shift out), character set 1 (ignored) */
815 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000816 case 27: /* esc (introducing an escape sequence) */
bellarde7f0ad52004-07-14 17:28:59 +0000817 s->state = TTY_STATE_ESC;
818 break;
819 default:
thsed8276a2007-02-10 22:37:56 +0000820 if (s->x >= s->width) {
821 /* line wrap */
822 s->x = 0;
823 console_put_lf(s);
thsadb47962007-01-16 23:02:36 +0000824 }
bellarde7f0ad52004-07-14 17:28:59 +0000825 y1 = (s->y_base + s->y) % s->total_height;
826 c = &s->cells[y1 * s->width + s->x];
827 c->ch = ch;
pbrook6d6f7c22006-03-11 15:35:30 +0000828 c->t_attrib = s->t_attrib;
bellarde7f0ad52004-07-14 17:28:59 +0000829 update_xy(s, s->x, s->y);
830 s->x++;
bellarde7f0ad52004-07-14 17:28:59 +0000831 break;
832 }
833 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000834 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
bellarde7f0ad52004-07-14 17:28:59 +0000835 if (ch == '[') {
836 for(i=0;i<MAX_ESC_PARAMS;i++)
837 s->esc_params[i] = 0;
838 s->nb_esc_params = 0;
839 s->state = TTY_STATE_CSI;
840 } else {
841 s->state = TTY_STATE_NORM;
842 }
843 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000844 case TTY_STATE_CSI: /* handle escape sequence parameters */
bellarde7f0ad52004-07-14 17:28:59 +0000845 if (ch >= '0' && ch <= '9') {
846 if (s->nb_esc_params < MAX_ESC_PARAMS) {
Laszlo Ersekc10600a2012-09-17 11:10:03 +0200847 int *param = &s->esc_params[s->nb_esc_params];
848 int digit = (ch - '0');
849
850 *param = (*param <= (INT_MAX - digit) / 10) ?
851 *param * 10 + digit : INT_MAX;
bellarde7f0ad52004-07-14 17:28:59 +0000852 }
853 } else {
Ian Campbell3eea5492012-09-04 10:26:09 -0500854 if (s->nb_esc_params < MAX_ESC_PARAMS)
855 s->nb_esc_params++;
bellarde7f0ad52004-07-14 17:28:59 +0000856 if (ch == ';')
857 break;
Stefan Weil5d28b0e2013-11-10 16:04:16 +0100858 trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
859 ch, s->nb_esc_params);
bellarde7f0ad52004-07-14 17:28:59 +0000860 s->state = TTY_STATE_NORM;
861 switch(ch) {
thsadb47962007-01-16 23:02:36 +0000862 case 'A':
863 /* move cursor up */
864 if (s->esc_params[0] == 0) {
865 s->esc_params[0] = 1;
866 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500867 set_cursor(s, s->x, s->y - s->esc_params[0]);
bellarde7f0ad52004-07-14 17:28:59 +0000868 break;
thsadb47962007-01-16 23:02:36 +0000869 case 'B':
870 /* move cursor down */
871 if (s->esc_params[0] == 0) {
872 s->esc_params[0] = 1;
873 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500874 set_cursor(s, s->x, s->y + s->esc_params[0]);
thsadb47962007-01-16 23:02:36 +0000875 break;
876 case 'C':
877 /* move cursor right */
878 if (s->esc_params[0] == 0) {
879 s->esc_params[0] = 1;
880 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500881 set_cursor(s, s->x + s->esc_params[0], s->y);
thsadb47962007-01-16 23:02:36 +0000882 break;
883 case 'D':
884 /* move cursor left */
885 if (s->esc_params[0] == 0) {
886 s->esc_params[0] = 1;
887 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500888 set_cursor(s, s->x - s->esc_params[0], s->y);
thsadb47962007-01-16 23:02:36 +0000889 break;
890 case 'G':
891 /* move cursor to column */
Ian Campbell3eea5492012-09-04 10:26:09 -0500892 set_cursor(s, s->esc_params[0] - 1, s->y);
thsadb47962007-01-16 23:02:36 +0000893 break;
894 case 'f':
895 case 'H':
896 /* move cursor to row, column */
Ian Campbell3eea5492012-09-04 10:26:09 -0500897 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
thsadb47962007-01-16 23:02:36 +0000898 break;
899 case 'J':
900 switch (s->esc_params[0]) {
901 case 0:
902 /* clear to end of screen */
903 for (y = s->y; y < s->height; y++) {
904 for (x = 0; x < s->width; x++) {
905 if (y == s->y && x < s->x) {
906 continue;
907 }
908 console_clear_xy(s, x, y);
909 }
910 }
911 break;
912 case 1:
913 /* clear from beginning of screen */
914 for (y = 0; y <= s->y; y++) {
915 for (x = 0; x < s->width; x++) {
916 if (y == s->y && x > s->x) {
917 break;
918 }
919 console_clear_xy(s, x, y);
920 }
921 }
922 break;
923 case 2:
924 /* clear entire screen */
925 for (y = 0; y <= s->height; y++) {
926 for (x = 0; x < s->width; x++) {
927 console_clear_xy(s, x, y);
928 }
929 }
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100930 break;
thsadb47962007-01-16 23:02:36 +0000931 }
Markus Armbruster95d8f9f2011-11-22 11:59:07 +0100932 break;
thsadb47962007-01-16 23:02:36 +0000933 case 'K':
934 switch (s->esc_params[0]) {
935 case 0:
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100936 /* clear to eol */
937 for(x = s->x; x < s->width; x++) {
thsadb47962007-01-16 23:02:36 +0000938 console_clear_xy(s, x, s->y);
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100939 }
940 break;
thsadb47962007-01-16 23:02:36 +0000941 case 1:
942 /* clear from beginning of line */
943 for (x = 0; x <= s->x; x++) {
944 console_clear_xy(s, x, s->y);
945 }
946 break;
947 case 2:
948 /* clear entire line */
949 for(x = 0; x < s->width; x++) {
950 console_clear_xy(s, x, s->y);
951 }
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100952 break;
953 }
thsadb47962007-01-16 23:02:36 +0000954 break;
955 case 'm':
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100956 console_handle_escape(s);
957 break;
thsadb47962007-01-16 23:02:36 +0000958 case 'n':
959 /* report cursor position */
960 /* TODO: send ESC[row;colR */
961 break;
962 case 's':
963 /* save cursor position */
964 s->x_saved = s->x;
965 s->y_saved = s->y;
966 break;
967 case 'u':
968 /* restore cursor position */
969 s->x = s->x_saved;
970 s->y = s->y_saved;
971 break;
972 default:
Stefan Weil5d28b0e2013-11-10 16:04:16 +0100973 trace_console_putchar_unhandled(ch);
thsadb47962007-01-16 23:02:36 +0000974 break;
975 }
976 break;
bellarde7f0ad52004-07-14 17:28:59 +0000977 }
978 }
979}
980
981void console_select(unsigned int index)
982{
Gerd Hoffmann284d1c62013-03-15 15:45:54 +0100983 DisplayChangeListener *dcl;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200984 QemuConsole *s;
pbrook6d6f7c22006-03-11 15:35:30 +0000985
Gerd Hoffmann437fe102013-03-07 16:04:52 +0100986 trace_console_select(index);
Gerd Hoffmann284d1c62013-03-15 15:45:54 +0100987 s = qemu_console_lookup_by_index(index);
bellarde7f0ad52004-07-14 17:28:59 +0000988 if (s) {
aliguori7d957bd2009-01-15 22:14:11 +0000989 DisplayState *ds = s->ds;
Jan Kiszkabf1bed82012-07-10 22:00:55 +0200990
bellarde7f0ad52004-07-14 17:28:59 +0000991 active_console = s;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200992 if (ds->have_gfx) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +0100993 QLIST_FOREACH(dcl, &ds->listeners, next) {
994 if (dcl->con != NULL) {
995 continue;
996 }
997 if (dcl->ops->dpy_gfx_switch) {
998 dcl->ops->dpy_gfx_switch(dcl, s->surface);
999 }
1000 }
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001001 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1002 surface_height(s->surface));
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001003 }
1004 if (ds->have_text) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001005 dpy_text_resize(s, s->width, s->height);
aliguori68f00992009-01-21 18:59:12 +00001006 }
Gerd Hoffmannaea79472014-05-22 11:27:13 +02001007 text_console_update_cursor(NULL);
bellarde7f0ad52004-07-14 17:28:59 +00001008 }
1009}
1010
1011static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1012{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001013 QemuConsole *s = chr->opaque;
bellarde7f0ad52004-07-14 17:28:59 +00001014 int i;
1015
pbrook14778c22009-01-21 03:02:52 +00001016 s->update_x0 = s->width * FONT_WIDTH;
1017 s->update_y0 = s->height * FONT_HEIGHT;
1018 s->update_x1 = 0;
1019 s->update_y1 = 0;
bellarde7f0ad52004-07-14 17:28:59 +00001020 console_show_cursor(s, 0);
1021 for(i = 0; i < len; i++) {
1022 console_putchar(s, buf[i]);
1023 }
1024 console_show_cursor(s, 1);
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001025 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001026 dpy_gfx_update(s, s->update_x0, s->update_y0,
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001027 s->update_x1 - s->update_x0,
1028 s->update_y1 - s->update_y0);
pbrook14778c22009-01-21 03:02:52 +00001029 }
bellarde7f0ad52004-07-14 17:28:59 +00001030 return len;
1031}
1032
bellarde15d7372006-06-25 16:26:29 +00001033static void kbd_send_chars(void *opaque)
1034{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001035 QemuConsole *s = opaque;
bellarde15d7372006-06-25 16:26:29 +00001036 int len;
1037 uint8_t buf[16];
ths3b46e622007-09-17 08:09:54 +00001038
Anthony Liguori909cda12011-08-15 11:17:31 -05001039 len = qemu_chr_be_can_write(s->chr);
bellarde15d7372006-06-25 16:26:29 +00001040 if (len > s->out_fifo.count)
1041 len = s->out_fifo.count;
1042 if (len > 0) {
1043 if (len > sizeof(buf))
1044 len = sizeof(buf);
1045 qemu_fifo_read(&s->out_fifo, buf, len);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001046 qemu_chr_be_write(s->chr, buf, len);
bellarde15d7372006-06-25 16:26:29 +00001047 }
1048 /* characters are pending: we send them a bit later (XXX:
1049 horrible, should change char device API) */
1050 if (s->out_fifo.count > 0) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001051 timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
bellarde15d7372006-06-25 16:26:29 +00001052 }
1053}
1054
bellarde7f0ad52004-07-14 17:28:59 +00001055/* called when an ascii key is pressed */
Gerd Hoffmann3f9a6e82014-05-22 12:05:52 +02001056void kbd_put_keysym_console(QemuConsole *s, int keysym)
bellarde7f0ad52004-07-14 17:28:59 +00001057{
bellarde7f0ad52004-07-14 17:28:59 +00001058 uint8_t buf[16], *q;
1059 int c;
1060
thsaf3a9032007-07-11 23:14:59 +00001061 if (!s || (s->console_type == GRAPHIC_CONSOLE))
bellarde7f0ad52004-07-14 17:28:59 +00001062 return;
1063
1064 switch(keysym) {
1065 case QEMU_KEY_CTRL_UP:
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001066 console_scroll(s, -1);
bellarde7f0ad52004-07-14 17:28:59 +00001067 break;
1068 case QEMU_KEY_CTRL_DOWN:
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001069 console_scroll(s, 1);
bellarde7f0ad52004-07-14 17:28:59 +00001070 break;
1071 case QEMU_KEY_CTRL_PAGEUP:
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001072 console_scroll(s, -10);
bellarde7f0ad52004-07-14 17:28:59 +00001073 break;
1074 case QEMU_KEY_CTRL_PAGEDOWN:
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001075 console_scroll(s, 10);
bellarde7f0ad52004-07-14 17:28:59 +00001076 break;
1077 default:
bellarde15d7372006-06-25 16:26:29 +00001078 /* convert the QEMU keysym to VT100 key string */
1079 q = buf;
1080 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1081 *q++ = '\033';
1082 *q++ = '[';
1083 c = keysym - 0xe100;
1084 if (c >= 10)
1085 *q++ = '0' + (c / 10);
1086 *q++ = '0' + (c % 10);
1087 *q++ = '~';
1088 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1089 *q++ = '\033';
1090 *q++ = '[';
1091 *q++ = keysym & 0xff;
Paolo Bonzini41048332010-12-23 13:42:52 +01001092 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1093 console_puts(s->chr, (const uint8_t *) "\r", 1);
1094 *q++ = '\n';
bellarde15d7372006-06-25 16:26:29 +00001095 } else {
Paolo Bonzini41048332010-12-23 13:42:52 +01001096 *q++ = keysym;
1097 }
1098 if (s->echo) {
1099 console_puts(s->chr, buf, q - buf);
bellarde15d7372006-06-25 16:26:29 +00001100 }
pbrooke5b0bc42007-01-27 23:46:43 +00001101 if (s->chr->chr_read) {
bellarde15d7372006-06-25 16:26:29 +00001102 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1103 kbd_send_chars(s);
bellarde7f0ad52004-07-14 17:28:59 +00001104 }
1105 break;
1106 }
1107}
1108
Gerd Hoffmann50ef4672014-05-27 09:28:38 +02001109static const int qcode_to_keysym[Q_KEY_CODE_MAX] = {
1110 [Q_KEY_CODE_UP] = QEMU_KEY_UP,
1111 [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
1112 [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
1113 [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
1114 [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
1115 [Q_KEY_CODE_END] = QEMU_KEY_END,
1116 [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
1117 [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
1118 [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
1119};
1120
1121bool kbd_put_qcode_console(QemuConsole *s, int qcode)
1122{
1123 int keysym;
1124
1125 keysym = qcode_to_keysym[qcode];
1126 if (keysym == 0) {
1127 return false;
1128 }
1129 kbd_put_keysym_console(s, keysym);
1130 return true;
1131}
1132
Gerd Hoffmannbdef9722014-05-27 09:32:36 +02001133void kbd_put_string_console(QemuConsole *s, const char *str, int len)
1134{
1135 int i;
1136
1137 for (i = 0; i < len && str[i]; i++) {
1138 kbd_put_keysym_console(s, str[i]);
1139 }
1140}
1141
Gerd Hoffmann3f9a6e82014-05-22 12:05:52 +02001142void kbd_put_keysym(int keysym)
1143{
1144 kbd_put_keysym_console(active_console, keysym);
1145}
1146
balrog4d3b6f62008-02-10 16:33:14 +00001147static void text_console_invalidate(void *opaque)
1148{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001149 QemuConsole *s = (QemuConsole *) opaque;
Gerd Hoffmann1562e532013-03-06 13:40:47 +01001150
1151 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
aliguori68f00992009-01-21 18:59:12 +00001152 text_console_resize(s);
1153 }
balrog4d3b6f62008-02-10 16:33:14 +00001154 console_refresh(s);
1155}
1156
Anthony Liguoric227f092009-10-01 16:12:16 -05001157static void text_console_update(void *opaque, console_ch_t *chardata)
balrog4d3b6f62008-02-10 16:33:14 +00001158{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001159 QemuConsole *s = (QemuConsole *) opaque;
balrog4d3b6f62008-02-10 16:33:14 +00001160 int i, j, src;
1161
1162 if (s->text_x[0] <= s->text_x[1]) {
1163 src = (s->y_base + s->text_y[0]) * s->width;
1164 chardata += s->text_y[0] * s->width;
1165 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1166 for (j = 0; j < s->width; j ++, src ++)
1167 console_write_ch(chardata ++, s->cells[src].ch |
1168 (s->cells[src].t_attrib.fgcol << 12) |
1169 (s->cells[src].t_attrib.bgcol << 8) |
1170 (s->cells[src].t_attrib.bold << 21));
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001171 dpy_text_update(s, s->text_x[0], s->text_y[0],
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001172 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
balrog4d3b6f62008-02-10 16:33:14 +00001173 s->text_x[0] = s->width;
1174 s->text_y[0] = s->height;
1175 s->text_x[1] = 0;
1176 s->text_y[1] = 0;
1177 }
1178 if (s->cursor_invalidate) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001179 dpy_text_cursor(s, s->x, s->y);
balrog4d3b6f62008-02-10 16:33:14 +00001180 s->cursor_invalidate = 0;
1181 }
1182}
1183
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001184static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
1185 uint32_t head)
bellarde7f0ad52004-07-14 17:28:59 +00001186{
Gerd Hoffmann95be0662013-04-17 09:45:10 +02001187 Object *obj;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001188 QemuConsole *s;
pbrook95219892006-04-09 01:06:34 +00001189 int i;
bellarde7f0ad52004-07-14 17:28:59 +00001190
Gerd Hoffmann95be0662013-04-17 09:45:10 +02001191 obj = object_new(TYPE_QEMU_CONSOLE);
1192 s = QEMU_CONSOLE(obj);
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001193 s->head = head;
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001194 object_property_add_link(obj, "device", TYPE_DEVICE,
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001195 (Object **)&s->device,
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001196 object_property_allow_set_link,
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001197 OBJ_PROP_LINK_UNREF_ON_RELEASE,
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001198 &error_abort);
Gerd Hoffmann56437062014-01-24 15:35:21 +01001199 object_property_add_uint32_ptr(obj, "head",
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001200 &s->head, &error_abort);
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001201
thsaf3a9032007-07-11 23:14:59 +00001202 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1203 (console_type == GRAPHIC_CONSOLE))) {
bellarde7f0ad52004-07-14 17:28:59 +00001204 active_console = s;
thsaf3a9032007-07-11 23:14:59 +00001205 }
bellarde7f0ad52004-07-14 17:28:59 +00001206 s->ds = ds;
thsaf3a9032007-07-11 23:14:59 +00001207 s->console_type = console_type;
Gerd Hoffmanna1d2db02014-05-26 10:36:35 +02001208
1209 consoles = g_realloc(consoles, sizeof(*consoles) * (nb_consoles+1));
thsaf3a9032007-07-11 23:14:59 +00001210 if (console_type != GRAPHIC_CONSOLE) {
Jan Kiszkaf81bdef2011-09-16 00:48:07 +02001211 s->index = nb_consoles;
pbrook95219892006-04-09 01:06:34 +00001212 consoles[nb_consoles++] = s;
1213 } else {
1214 /* HACK: Put graphical consoles before text consoles. */
1215 for (i = nb_consoles; i > 0; i--) {
thsaf3a9032007-07-11 23:14:59 +00001216 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
pbrook95219892006-04-09 01:06:34 +00001217 break;
1218 consoles[i] = consoles[i - 1];
Jan Kiszkaf81bdef2011-09-16 00:48:07 +02001219 consoles[i]->index = i;
pbrook95219892006-04-09 01:06:34 +00001220 }
Jan Kiszkaf81bdef2011-09-16 00:48:07 +02001221 s->index = i;
pbrook95219892006-04-09 01:06:34 +00001222 consoles[i] = s;
aliguori3023f3322009-01-16 19:04:14 +00001223 nb_consoles++;
pbrook95219892006-04-09 01:06:34 +00001224 }
bellarde7f0ad52004-07-14 17:28:59 +00001225 return s;
1226}
1227
Gerd Hoffmann30f1e662014-06-18 11:03:15 +02001228static void qemu_alloc_display(DisplaySurface *surface, int width, int height)
Jes Sorensenffe8b822011-03-16 13:33:30 +01001229{
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001230 qemu_pixman_image_unref(surface->image);
1231 surface->image = NULL;
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001232
Gerd Hoffmann30f1e662014-06-18 11:03:15 +02001233 surface->format = PIXMAN_x8r8g8b8;
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001234 surface->image = pixman_image_create_bits(surface->format,
1235 width, height,
Gerd Hoffmann30f1e662014-06-18 11:03:15 +02001236 NULL, width * 4);
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001237 assert(surface->image != NULL);
1238
Gerd Hoffmann30f1e662014-06-18 11:03:15 +02001239 surface->flags = QEMU_ALLOCATED_FLAG;
Paolo Bonzini98b50082010-02-11 00:29:57 +01001240}
1241
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001242DisplaySurface *qemu_create_displaysurface(int width, int height)
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001243{
1244 DisplaySurface *surface = g_new0(DisplaySurface, 1);
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001245
1246 trace_displaysurface_create(surface, width, height);
Gerd Hoffmann30f1e662014-06-18 11:03:15 +02001247 qemu_alloc_display(surface, width, height);
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001248 return surface;
1249}
1250
Gerd Hoffmann30f1e662014-06-18 11:03:15 +02001251DisplaySurface *qemu_create_displaysurface_from(int width, int height,
1252 pixman_format_code_t format,
1253 int linesize, uint8_t *data)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001254{
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001255 DisplaySurface *surface = g_new0(DisplaySurface, 1);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001256
Gerd Hoffmann30f1e662014-06-18 11:03:15 +02001257 trace_displaysurface_create_from(surface, width, height, format);
1258 surface->format = format;
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001259 surface->image = pixman_image_create_bits(surface->format,
1260 width, height,
1261 (void *)data, linesize);
1262 assert(surface->image != NULL);
1263
Paolo Bonzini98b50082010-02-11 00:29:57 +01001264 return surface;
1265}
1266
Gerd Hoffmanna77549b2014-06-19 08:46:08 +02001267static void qemu_unmap_displaysurface_guestmem(pixman_image_t *image,
1268 void *unused)
1269{
1270 void *data = pixman_image_get_data(image);
1271 uint32_t size = pixman_image_get_stride(image) *
1272 pixman_image_get_height(image);
1273 cpu_physical_memory_unmap(data, size, 0, 0);
1274}
1275
1276DisplaySurface *qemu_create_displaysurface_guestmem(int width, int height,
1277 pixman_format_code_t format,
1278 int linesize, uint64_t addr)
1279{
1280 DisplaySurface *surface;
1281 hwaddr size;
1282 void *data;
1283
1284 if (linesize == 0) {
1285 linesize = width * PIXMAN_FORMAT_BPP(format) / 8;
1286 }
1287
1288 size = linesize * height;
1289 data = cpu_physical_memory_map(addr, &size, 0);
1290 if (size != linesize * height) {
1291 cpu_physical_memory_unmap(data, size, 0, 0);
1292 return NULL;
1293 }
1294
1295 surface = qemu_create_displaysurface_from
1296 (width, height, format, linesize, data);
1297 pixman_image_set_destroy_function
1298 (surface->image, qemu_unmap_displaysurface_guestmem, NULL);
1299
1300 return surface;
1301}
1302
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001303static DisplaySurface *qemu_create_message_surface(int w, int h,
1304 const char *msg)
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001305{
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001306 DisplaySurface *surface = qemu_create_displaysurface(w, h);
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001307 pixman_color_t bg = color_table_rgb[0][COLOR_BLACK];
1308 pixman_color_t fg = color_table_rgb[0][COLOR_WHITE];
1309 pixman_image_t *glyph;
1310 int len, x, y, i;
1311
1312 len = strlen(msg);
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001313 x = (w / FONT_WIDTH - len) / 2;
1314 y = (h / FONT_HEIGHT - 1) / 2;
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001315 for (i = 0; i < len; i++) {
1316 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1317 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1318 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1319 qemu_pixman_image_unref(glyph);
1320 }
1321 return surface;
1322}
1323
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001324void qemu_free_displaysurface(DisplaySurface *surface)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001325{
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001326 if (surface == NULL) {
Paolo Bonzini98b50082010-02-11 00:29:57 +01001327 return;
Gerd Hoffmann187cd1d2012-09-26 07:46:20 +02001328 }
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001329 trace_displaysurface_free(surface);
1330 qemu_pixman_image_unref(surface->image);
1331 g_free(surface);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001332}
1333
Gerd Hoffmann52090892013-04-23 15:44:31 +02001334void register_displaychangelistener(DisplayChangeListener *dcl)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001335{
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001336 static const char nodev[] =
1337 "This VM has no graphic display device.";
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001338 static DisplaySurface *dummy;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001339 QemuConsole *con;
1340
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001341 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
Gerd Hoffmann52090892013-04-23 15:44:31 +02001342 dcl->ds = get_alloc_displaystate();
1343 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1344 gui_setup_refresh(dcl->ds);
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001345 if (dcl->con) {
1346 dcl->con->dcls++;
1347 con = dcl->con;
1348 } else {
1349 con = active_console;
1350 }
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001351 if (dcl->ops->dpy_gfx_switch) {
1352 if (con) {
1353 dcl->ops->dpy_gfx_switch(dcl, con->surface);
1354 } else {
1355 if (!dummy) {
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001356 dummy = qemu_create_message_surface(640, 480, nodev);
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001357 }
1358 dcl->ops->dpy_gfx_switch(dcl, dummy);
1359 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001360 }
Gerd Hoffmannaea79472014-05-22 11:27:13 +02001361 text_console_update_cursor(NULL);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001362}
1363
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +01001364void update_displaychangelistener(DisplayChangeListener *dcl,
1365 uint64_t interval)
1366{
1367 DisplayState *ds = dcl->ds;
1368
1369 dcl->update_interval = interval;
1370 if (!ds->refreshing && ds->update_interval > interval) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001371 timer_mod(ds->gui_timer, ds->last_update + interval);
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +01001372 }
1373}
1374
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001375void unregister_displaychangelistener(DisplayChangeListener *dcl)
1376{
1377 DisplayState *ds = dcl->ds;
1378 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001379 if (dcl->con) {
1380 dcl->con->dcls--;
1381 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001382 QLIST_REMOVE(dcl, next);
1383 gui_setup_refresh(ds);
1384}
1385
Gerd Hoffmann6f90f3d2014-01-24 17:38:20 +01001386int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info)
1387{
1388 assert(con != NULL);
1389 con->ui_info = *info;
1390 if (con->hw_ops->ui_info) {
1391 return con->hw_ops->ui_info(con->hw, con->head, info);
1392 }
1393 return -1;
1394}
1395
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001396void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001397{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001398 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001399 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001400 int width = surface_width(con->surface);
1401 int height = surface_height(con->surface);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001402
1403 x = MAX(x, 0);
1404 y = MAX(y, 0);
1405 x = MIN(x, width);
1406 y = MIN(y, height);
1407 w = MIN(w, width - x);
1408 h = MIN(h, height - y);
1409
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001410 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001411 return;
1412 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001413 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001414 if (con != (dcl->con ? dcl->con : active_console)) {
1415 continue;
1416 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001417 if (dcl->ops->dpy_gfx_update) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001418 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001419 }
1420 }
1421}
1422
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001423void dpy_gfx_replace_surface(QemuConsole *con,
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001424 DisplaySurface *surface)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001425{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001426 DisplayState *s = con->ds;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001427 DisplaySurface *old_surface = con->surface;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001428 DisplayChangeListener *dcl;
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001429
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001430 con->surface = surface;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001431 QLIST_FOREACH(dcl, &s->listeners, next) {
1432 if (con != (dcl->con ? dcl->con : active_console)) {
1433 continue;
1434 }
1435 if (dcl->ops->dpy_gfx_switch) {
1436 dcl->ops->dpy_gfx_switch(dcl, surface);
1437 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001438 }
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001439 qemu_free_displaysurface(old_surface);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001440}
1441
Stefan Weil60751372014-05-02 22:48:30 +02001442static void dpy_refresh(DisplayState *s)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001443{
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001444 DisplayChangeListener *dcl;
1445
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001446 QLIST_FOREACH(dcl, &s->listeners, next) {
1447 if (dcl->ops->dpy_refresh) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001448 dcl->ops->dpy_refresh(dcl);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001449 }
1450 }
1451}
1452
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001453void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1454 int dst_x, int dst_y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001455{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001456 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001457 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001458
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001459 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001460 return;
1461 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001462 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001463 if (con != (dcl->con ? dcl->con : active_console)) {
1464 continue;
1465 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001466 if (dcl->ops->dpy_gfx_copy) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001467 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001468 } else { /* TODO */
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001469 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001470 }
1471 }
1472}
1473
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001474void dpy_text_cursor(QemuConsole *con, int x, int y)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001475{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001476 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001477 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001478
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001479 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001480 return;
1481 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001482 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001483 if (con != (dcl->con ? dcl->con : active_console)) {
1484 continue;
1485 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001486 if (dcl->ops->dpy_text_cursor) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001487 dcl->ops->dpy_text_cursor(dcl, x, y);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001488 }
1489 }
1490}
1491
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001492void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001493{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001494 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001495 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001496
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001497 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001498 return;
1499 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001500 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001501 if (con != (dcl->con ? dcl->con : active_console)) {
1502 continue;
1503 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001504 if (dcl->ops->dpy_text_update) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001505 dcl->ops->dpy_text_update(dcl, x, y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001506 }
1507 }
1508}
1509
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001510void dpy_text_resize(QemuConsole *con, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001511{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001512 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001513 struct DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001514
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001515 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001516 return;
1517 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001518 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001519 if (con != (dcl->con ? dcl->con : active_console)) {
1520 continue;
1521 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001522 if (dcl->ops->dpy_text_resize) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001523 dcl->ops->dpy_text_resize(dcl, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001524 }
1525 }
1526}
1527
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001528void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001529{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001530 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001531 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001532
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001533 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001534 return;
1535 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001536 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001537 if (con != (dcl->con ? dcl->con : active_console)) {
1538 continue;
1539 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001540 if (dcl->ops->dpy_mouse_set) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001541 dcl->ops->dpy_mouse_set(dcl, x, y, on);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001542 }
1543 }
1544}
1545
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001546void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001547{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001548 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001549 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001550
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001551 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001552 return;
1553 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001554 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001555 if (con != (dcl->con ? dcl->con : active_console)) {
1556 continue;
1557 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001558 if (dcl->ops->dpy_cursor_define) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001559 dcl->ops->dpy_cursor_define(dcl, cursor);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001560 }
1561 }
1562}
1563
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001564bool dpy_cursor_define_supported(QemuConsole *con)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001565{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001566 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001567 DisplayChangeListener *dcl;
1568
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001569 QLIST_FOREACH(dcl, &s->listeners, next) {
1570 if (dcl->ops->dpy_cursor_define) {
1571 return true;
1572 }
1573 }
1574 return false;
1575}
1576
Gerd Hoffmann4c387622014-06-19 08:52:17 +02001577/*
1578 * Call dpy_gfx_update for all dirity scanlines. Works for
1579 * DisplaySurfaces backed by guest memory (i.e. the ones created
1580 * using qemu_create_displaysurface_guestmem).
1581 */
1582void dpy_gfx_update_dirty(QemuConsole *con,
1583 MemoryRegion *address_space,
1584 hwaddr base,
1585 bool invalidate)
1586{
1587 DisplaySurface *ds = qemu_console_surface(con);
1588 int width = surface_stride(ds);
1589 int height = surface_height(ds);
1590 hwaddr size = width * height;
1591 MemoryRegionSection mem_section;
1592 MemoryRegion *mem;
1593 ram_addr_t addr;
1594 int first, last, i;
1595 bool dirty;
1596
1597 mem_section = memory_region_find(address_space, base, size);
1598 mem = mem_section.mr;
1599 if (int128_get64(mem_section.size) != size ||
1600 !memory_region_is_ram(mem_section.mr)) {
1601 goto out;
1602 }
1603 assert(mem);
1604
1605 memory_region_sync_dirty_bitmap(mem);
1606 addr = mem_section.offset_within_region;
1607
1608 first = -1;
1609 last = -1;
1610 for (i = 0; i < height; i++, addr += width) {
1611 dirty = invalidate ||
1612 memory_region_get_dirty(mem, addr, width, DIRTY_MEMORY_VGA);
1613 if (dirty) {
1614 if (first == -1) {
1615 first = i;
1616 }
1617 last = i;
1618 }
1619 if (first != -1 && !dirty) {
1620 assert(last != -1 && last >= first);
1621 dpy_gfx_update(con, 0, first, surface_width(ds),
1622 last - first + 1);
1623 first = -1;
1624 }
1625 }
1626 if (first != -1) {
1627 assert(last != -1 && last >= first);
1628 dpy_gfx_update(con, 0, first, surface_width(ds),
1629 last - first + 1);
1630 }
1631
1632 memory_region_reset_dirty(mem, mem_section.offset_within_region, size,
1633 DIRTY_MEMORY_VGA);
1634out:
1635 memory_region_unref(mem);
1636}
1637
Paolo Bonzini98b50082010-02-11 00:29:57 +01001638/***********************************************************/
1639/* register display */
1640
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001641/* console.c internal use only */
1642static DisplayState *get_alloc_displaystate(void)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001643{
1644 if (!display_state) {
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001645 display_state = g_new0(DisplayState, 1);
Gerd Hoffmannaea79472014-05-22 11:27:13 +02001646 cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1647 text_console_update_cursor, NULL);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001648 }
1649 return display_state;
1650}
1651
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001652/*
1653 * Called by main(), after creating QemuConsoles
1654 * and before initializing ui (sdl/vnc/...).
1655 */
1656DisplayState *init_displaystate(void)
1657{
Gerd Hoffmann43f420f2013-06-25 10:49:31 +02001658 gchar *name;
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001659 int i;
1660
Gerd Hoffmann333cb182014-06-02 14:07:18 +02001661 get_alloc_displaystate();
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001662 for (i = 0; i < nb_consoles; i++) {
1663 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1664 consoles[i]->ds == NULL) {
1665 text_console_do_init(consoles[i]->chr, display_state);
1666 }
Gerd Hoffmann43f420f2013-06-25 10:49:31 +02001667
1668 /* Hook up into the qom tree here (not in new_console()), once
1669 * all QemuConsoles are created and the order / numbering
1670 * doesn't change any more */
1671 name = g_strdup_printf("console[%d]", i);
1672 object_property_add_child(container_get(object_get_root(), "/backend"),
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001673 name, OBJECT(consoles[i]), &error_abort);
Gerd Hoffmann43f420f2013-06-25 10:49:31 +02001674 g_free(name);
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001675 }
1676
1677 return display_state;
1678}
1679
Gerd Hoffmann1c1f9492014-09-24 17:05:27 +02001680void graphic_console_set_hwops(QemuConsole *con,
1681 const GraphicHwOps *hw_ops,
1682 void *opaque)
1683{
1684 con->hw_ops = hw_ops;
1685 con->hw = opaque;
1686}
1687
Gerd Hoffmann56437062014-01-24 15:35:21 +01001688QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001689 const GraphicHwOps *hw_ops,
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001690 void *opaque)
bellarde7f0ad52004-07-14 17:28:59 +00001691{
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001692 static const char noinit[] =
1693 "Guest has not initialized the display (yet).";
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001694 int width = 640;
1695 int height = 480;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001696 QemuConsole *s;
aliguori3023f3322009-01-16 19:04:14 +00001697 DisplayState *ds;
aurel32f0f2f972009-01-16 21:13:49 +00001698
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001699 ds = get_alloc_displaystate();
Gerd Hoffmann437fe102013-03-07 16:04:52 +01001700 trace_console_gfx_new();
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001701 s = new_console(ds, GRAPHIC_CONSOLE, head);
Gerd Hoffmann1c1f9492014-09-24 17:05:27 +02001702 graphic_console_set_hwops(s, hw_ops, opaque);
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001703 if (dev) {
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001704 object_property_set_link(OBJECT(s), OBJECT(dev), "device",
1705 &error_abort);
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001706 }
aliguori3023f3322009-01-16 19:04:14 +00001707
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001708 s->surface = qemu_create_message_surface(width, height, noinit);
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001709 return s;
pbrook95219892006-04-09 01:06:34 +00001710}
1711
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001712QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1713{
Gerd Hoffmanna1d2db02014-05-26 10:36:35 +02001714 if (index >= nb_consoles) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001715 return NULL;
1716 }
1717 return consoles[index];
1718}
1719
Gerd Hoffmann56437062014-01-24 15:35:21 +01001720QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
Gerd Hoffmann14a93642013-04-18 07:30:40 +02001721{
Gerd Hoffmann14a93642013-04-18 07:30:40 +02001722 Object *obj;
Gerd Hoffmann56437062014-01-24 15:35:21 +01001723 uint32_t h;
Gerd Hoffmann14a93642013-04-18 07:30:40 +02001724 int i;
1725
1726 for (i = 0; i < nb_consoles; i++) {
1727 if (!consoles[i]) {
1728 continue;
1729 }
1730 obj = object_property_get_link(OBJECT(consoles[i]),
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001731 "device", &error_abort);
Gerd Hoffmann56437062014-01-24 15:35:21 +01001732 if (DEVICE(obj) != dev) {
1733 continue;
Gerd Hoffmann14a93642013-04-18 07:30:40 +02001734 }
Gerd Hoffmann56437062014-01-24 15:35:21 +01001735 h = object_property_get_int(OBJECT(consoles[i]),
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001736 "head", &error_abort);
Gerd Hoffmann56437062014-01-24 15:35:21 +01001737 if (h != head) {
1738 continue;
1739 }
1740 return consoles[i];
Gerd Hoffmann14a93642013-04-18 07:30:40 +02001741 }
1742 return NULL;
1743}
1744
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001745bool qemu_console_is_visible(QemuConsole *con)
pbrook95219892006-04-09 01:06:34 +00001746{
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001747 return (con == active_console) || (con->dcls > 0);
bellarde7f0ad52004-07-14 17:28:59 +00001748}
1749
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001750bool qemu_console_is_graphic(QemuConsole *con)
balrogc21bbcf2008-09-24 03:32:33 +00001751{
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001752 if (con == NULL) {
1753 con = active_console;
1754 }
1755 return con && (con->console_type == GRAPHIC_CONSOLE);
1756}
1757
1758bool qemu_console_is_fixedsize(QemuConsole *con)
1759{
1760 if (con == NULL) {
1761 con = active_console;
1762 }
1763 return con && (con->console_type != TEXT_CONSOLE);
balrogc21bbcf2008-09-24 03:32:33 +00001764}
1765
Gerd Hoffmannd4c85332013-11-28 09:58:18 +01001766int qemu_console_get_index(QemuConsole *con)
1767{
1768 if (con == NULL) {
1769 con = active_console;
1770 }
1771 return con ? con->index : -1;
1772}
1773
Gerd Hoffmann56437062014-01-24 15:35:21 +01001774uint32_t qemu_console_get_head(QemuConsole *con)
1775{
1776 if (con == NULL) {
1777 con = active_console;
1778 }
1779 return con ? con->head : -1;
1780}
1781
Gerd Hoffmann6f90f3d2014-01-24 17:38:20 +01001782QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con)
1783{
1784 assert(con != NULL);
1785 return &con->ui_info;
1786}
1787
Gerd Hoffmannd4c85332013-11-28 09:58:18 +01001788int qemu_console_get_width(QemuConsole *con, int fallback)
1789{
1790 if (con == NULL) {
1791 con = active_console;
1792 }
1793 return con ? surface_width(con->surface) : fallback;
1794}
1795
1796int qemu_console_get_height(QemuConsole *con, int fallback)
1797{
1798 if (con == NULL) {
1799 con = active_console;
1800 }
1801 return con ? surface_height(con->surface) : fallback;
1802}
1803
Paolo Bonzini41048332010-12-23 13:42:52 +01001804static void text_console_set_echo(CharDriverState *chr, bool echo)
1805{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001806 QemuConsole *s = chr->opaque;
Paolo Bonzini41048332010-12-23 13:42:52 +01001807
1808 s->echo = echo;
1809}
1810
Gerd Hoffmannaea79472014-05-22 11:27:13 +02001811static void text_console_update_cursor_timer(void)
1812{
1813 timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
1814 + CONSOLE_CURSOR_PERIOD / 2);
1815}
1816
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001817static void text_console_update_cursor(void *opaque)
1818{
Gerd Hoffmannaea79472014-05-22 11:27:13 +02001819 QemuConsole *s;
1820 int i, count = 0;
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001821
Gerd Hoffmannaea79472014-05-22 11:27:13 +02001822 cursor_visible_phase = !cursor_visible_phase;
1823
1824 for (i = 0; i < nb_consoles; i++) {
1825 s = consoles[i];
1826 if (qemu_console_is_graphic(s) ||
1827 !qemu_console_is_visible(s)) {
1828 continue;
1829 }
1830 count++;
1831 graphic_hw_invalidate(s);
1832 }
1833
1834 if (count) {
1835 text_console_update_cursor_timer();
1836 }
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001837}
1838
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001839static const GraphicHwOps text_console_ops = {
1840 .invalidate = text_console_invalidate,
1841 .text_update = text_console_update,
1842};
1843
Paolo Bonzini44b37b92010-12-23 13:42:53 +01001844static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
bellarde7f0ad52004-07-14 17:28:59 +00001845{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001846 QemuConsole *s;
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001847 int g_width = 80 * FONT_WIDTH;
1848 int g_height = 24 * FONT_HEIGHT;
pbrook6d6f7c22006-03-11 15:35:30 +00001849
Paolo Bonzini491e1142010-12-23 13:42:51 +01001850 s = chr->opaque;
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02001851
bellarde7f0ad52004-07-14 17:28:59 +00001852 chr->chr_write = console_puts;
bellard6fcfafb2004-08-01 21:48:30 +00001853
bellarde15d7372006-06-25 16:26:29 +00001854 s->out_fifo.buf = s->out_fifo_buf;
1855 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
Alex Blighbc72ad62013-08-21 16:03:08 +01001856 s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
aliguori3023f3322009-01-16 19:04:14 +00001857 s->ds = ds;
ths3b46e622007-09-17 08:09:54 +00001858
bellarde7f0ad52004-07-14 17:28:59 +00001859 s->y_displayed = 0;
1860 s->y_base = 0;
1861 s->total_height = DEFAULT_BACKSCROLL;
1862 s->x = 0;
1863 s->y = 0;
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001864 if (!s->surface) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001865 if (active_console && active_console->surface) {
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001866 g_width = surface_width(active_console->surface);
1867 g_height = surface_height(active_console->surface);
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001868 }
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001869 s->surface = qemu_create_displaysurface(g_width, g_height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001870 }
pbrook6d6f7c22006-03-11 15:35:30 +00001871
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001872 s->hw_ops = &text_console_ops;
balrog4d3b6f62008-02-10 16:33:14 +00001873 s->hw = s;
1874
pbrook6d6f7c22006-03-11 15:35:30 +00001875 /* Set text attribute defaults */
1876 s->t_attrib_default.bold = 0;
1877 s->t_attrib_default.uline = 0;
1878 s->t_attrib_default.blink = 0;
1879 s->t_attrib_default.invers = 0;
1880 s->t_attrib_default.unvisible = 0;
1881 s->t_attrib_default.fgcol = COLOR_WHITE;
1882 s->t_attrib_default.bgcol = COLOR_BLACK;
pbrook6d6f7c22006-03-11 15:35:30 +00001883 /* set current text attributes to default */
1884 s->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +00001885 text_console_resize(s);
1886
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01001887 if (chr->label) {
1888 char msg[128];
1889 int len;
1890
Gerd Hoffmann735ba582009-12-08 13:11:40 +01001891 s->t_attrib.bgcol = COLOR_BLUE;
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01001892 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
1893 console_puts(chr, (uint8_t*)msg, len);
Gerd Hoffmann735ba582009-12-08 13:11:40 +01001894 s->t_attrib = s->t_attrib_default;
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01001895 }
1896
Hans de Goedefee204f2013-03-26 11:07:54 +01001897 qemu_chr_be_generic_open(chr);
aurel32ceecf1d2009-01-18 14:08:04 +00001898 if (chr->init)
1899 chr->init(chr);
bellarde7f0ad52004-07-14 17:28:59 +00001900}
pbrookc60e08d2008-07-01 16:24:38 +00001901
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001902static CharDriverState *text_console_init(ChardevVC *vc)
aliguori2796dae2009-01-16 20:23:27 +00001903{
1904 CharDriverState *chr;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001905 QemuConsole *s;
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001906 unsigned width = 0;
1907 unsigned height = 0;
aliguori2796dae2009-01-16 20:23:27 +00001908
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001909 chr = qemu_chr_alloc();
aliguori2796dae2009-01-16 20:23:27 +00001910
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001911 if (vc->has_width) {
1912 width = vc->width;
1913 } else if (vc->has_cols) {
1914 width = vc->cols * FONT_WIDTH;
1915 }
Paolo Bonzini491e1142010-12-23 13:42:51 +01001916
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001917 if (vc->has_height) {
1918 height = vc->height;
1919 } else if (vc->has_rows) {
1920 height = vc->rows * FONT_HEIGHT;
1921 }
Paolo Bonzini491e1142010-12-23 13:42:51 +01001922
Gerd Hoffmann437fe102013-03-07 16:04:52 +01001923 trace_console_txt_new(width, height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001924 if (width == 0 || height == 0) {
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001925 s = new_console(NULL, TEXT_CONSOLE, 0);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001926 } else {
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001927 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001928 s->surface = qemu_create_displaysurface(width, height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001929 }
1930
1931 if (!s) {
Stefan Weil5354d082011-10-02 18:53:09 +02001932 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001933 return NULL;
Paolo Bonzini491e1142010-12-23 13:42:51 +01001934 }
1935
1936 s->chr = chr;
Paolo Bonzini491e1142010-12-23 13:42:51 +01001937 chr->opaque = s;
Paolo Bonzini41048332010-12-23 13:42:52 +01001938 chr->chr_set_echo = text_console_set_echo;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001939 /* console/chardev init sometimes completes elsewhere in a 2nd
1940 * stage, so defer OPENED events until they are fully initialized
1941 */
1942 chr->explicit_be_open = true;
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001943
1944 if (display_state) {
1945 text_console_do_init(chr, display_state);
1946 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001947 return chr;
aliguori2796dae2009-01-16 20:23:27 +00001948}
1949
Anthony Liguorid82831d2013-02-20 07:43:19 -06001950static VcHandler *vc_handler = text_console_init;
1951
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001952CharDriverState *vc_init(ChardevVC *vc)
Anthony Liguorid82831d2013-02-20 07:43:19 -06001953{
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001954 return vc_handler(vc);
Anthony Liguorid82831d2013-02-20 07:43:19 -06001955}
1956
1957void register_vc_handler(VcHandler *handler)
1958{
1959 vc_handler = handler;
1960}
1961
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001962void qemu_console_resize(QemuConsole *s, int width, int height)
pbrookc60e08d2008-07-01 16:24:38 +00001963{
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001964 DisplaySurface *surface;
1965
1966 assert(s->console_type == GRAPHIC_CONSOLE);
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001967 surface = qemu_create_displaysurface(width, height);
1968 dpy_gfx_replace_surface(s, surface);
pbrookc60e08d2008-07-01 16:24:38 +00001969}
balrog38334f72008-09-24 02:21:24 +00001970
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001971void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
aliguori3023f3322009-01-16 19:04:14 +00001972 int dst_x, int dst_y, int w, int h)
balrogc21bbcf2008-09-24 03:32:33 +00001973{
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001974 assert(con->console_type == GRAPHIC_CONSOLE);
1975 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
balrog38334f72008-09-24 02:21:24 +00001976}
aliguori7d957bd2009-01-15 22:14:11 +00001977
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001978DisplaySurface *qemu_console_surface(QemuConsole *console)
1979{
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001980 return console->surface;
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001981}
1982
1983DisplayState *qemu_console_displaystate(QemuConsole *console)
1984{
1985 return console->ds;
1986}
1987
malc0da2ea12009-01-23 19:56:19 +00001988PixelFormat qemu_different_endianness_pixelformat(int bpp)
aliguori7d957bd2009-01-15 22:14:11 +00001989{
Gerd Hoffmann56bd9ea2014-06-18 11:07:50 +02001990 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, false);
1991 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
malc0da2ea12009-01-23 19:56:19 +00001992 return pf;
1993}
1994
1995PixelFormat qemu_default_pixelformat(int bpp)
1996{
Gerd Hoffmann56bd9ea2014-06-18 11:07:50 +02001997 pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
1998 PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
aliguori7d957bd2009-01-15 22:14:11 +00001999 return pf;
2000}
Anthony Liguori01f45d92013-03-05 23:21:32 +05302001
Gerd Hoffmann702ec692013-02-25 15:52:32 +01002002static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
2003 Error **errp)
2004{
2005 int val;
2006
2007 backend->vc = g_new0(ChardevVC, 1);
2008
2009 val = qemu_opt_get_number(opts, "width", 0);
2010 if (val != 0) {
2011 backend->vc->has_width = true;
2012 backend->vc->width = val;
2013 }
2014
2015 val = qemu_opt_get_number(opts, "height", 0);
2016 if (val != 0) {
2017 backend->vc->has_height = true;
2018 backend->vc->height = val;
2019 }
2020
2021 val = qemu_opt_get_number(opts, "cols", 0);
2022 if (val != 0) {
2023 backend->vc->has_cols = true;
2024 backend->vc->cols = val;
2025 }
2026
2027 val = qemu_opt_get_number(opts, "rows", 0);
2028 if (val != 0) {
2029 backend->vc->has_rows = true;
2030 backend->vc->rows = val;
2031 }
2032}
2033
Gerd Hoffmann95be0662013-04-17 09:45:10 +02002034static const TypeInfo qemu_console_info = {
2035 .name = TYPE_QEMU_CONSOLE,
2036 .parent = TYPE_OBJECT,
2037 .instance_size = sizeof(QemuConsole),
2038 .class_size = sizeof(QemuConsoleClass),
2039};
2040
2041
Anthony Liguori01f45d92013-03-05 23:21:32 +05302042static void register_types(void)
2043{
Gerd Hoffmann95be0662013-04-17 09:45:10 +02002044 type_register_static(&qemu_console_info);
Peter Maydelle4d50d42014-09-02 11:24:17 +01002045 register_char_driver("vc", CHARDEV_BACKEND_KIND_VC, qemu_chr_parse_vc);
Anthony Liguori01f45d92013-03-05 23:21:32 +05302046}
2047
2048type_init(register_types);