blob: ab8454903ca91a257fd5ae9e981286d73a0e4172 [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"
bellarde7f0ad52004-07-14 17:28:59 +000031
32#define DEFAULT_BACKSCROLL 512
Jan Kiszkabf1bed82012-07-10 22:00:55 +020033#define CONSOLE_CURSOR_PERIOD 500
bellarde7f0ad52004-07-14 17:28:59 +000034
pbrook6d6f7c22006-03-11 15:35:30 +000035typedef struct TextAttributes {
36 uint8_t fgcol:4;
37 uint8_t bgcol:4;
38 uint8_t bold:1;
39 uint8_t uline:1;
40 uint8_t blink:1;
41 uint8_t invers:1;
42 uint8_t unvisible:1;
43} TextAttributes;
44
bellarde7f0ad52004-07-14 17:28:59 +000045typedef struct TextCell {
46 uint8_t ch;
pbrook6d6f7c22006-03-11 15:35:30 +000047 TextAttributes t_attrib;
bellarde7f0ad52004-07-14 17:28:59 +000048} TextCell;
49
50#define MAX_ESC_PARAMS 3
51
52enum TTYState {
53 TTY_STATE_NORM,
54 TTY_STATE_ESC,
55 TTY_STATE_CSI,
56};
57
bellarde15d7372006-06-25 16:26:29 +000058typedef struct QEMUFIFO {
59 uint8_t *buf;
60 int buf_size;
61 int count, wptr, rptr;
62} QEMUFIFO;
63
pbrook9596ebb2007-11-18 01:44:38 +000064static int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1)
bellarde15d7372006-06-25 16:26:29 +000065{
66 int l, len;
67
68 l = f->buf_size - f->count;
69 if (len1 > l)
70 len1 = l;
71 len = len1;
72 while (len > 0) {
73 l = f->buf_size - f->wptr;
74 if (l > len)
75 l = len;
76 memcpy(f->buf + f->wptr, buf, l);
77 f->wptr += l;
78 if (f->wptr >= f->buf_size)
79 f->wptr = 0;
80 buf += l;
81 len -= l;
82 }
83 f->count += len1;
84 return len1;
85}
86
pbrook9596ebb2007-11-18 01:44:38 +000087static int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1)
bellarde15d7372006-06-25 16:26:29 +000088{
89 int l, len;
90
91 if (len1 > f->count)
92 len1 = f->count;
93 len = len1;
94 while (len > 0) {
95 l = f->buf_size - f->rptr;
96 if (l > len)
97 l = len;
98 memcpy(buf, f->buf + f->rptr, l);
99 f->rptr += l;
100 if (f->rptr >= f->buf_size)
101 f->rptr = 0;
102 buf += l;
103 len -= l;
104 }
105 f->count -= len1;
106 return len1;
107}
108
thsaf3a9032007-07-11 23:14:59 +0000109typedef enum {
110 GRAPHIC_CONSOLE,
balrogc21bbcf2008-09-24 03:32:33 +0000111 TEXT_CONSOLE,
112 TEXT_CONSOLE_FIXED_SIZE
Anthony Liguoric227f092009-10-01 16:12:16 -0500113} console_type_t;
thsaf3a9032007-07-11 23:14:59 +0000114
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200115struct QemuConsole {
Gerd Hoffmann95be0662013-04-17 09:45:10 +0200116 Object parent;
117
Jan Kiszkaf81bdef2011-09-16 00:48:07 +0200118 int index;
Anthony Liguoric227f092009-10-01 16:12:16 -0500119 console_type_t console_type;
bellarde7f0ad52004-07-14 17:28:59 +0000120 DisplayState *ds;
Gerd Hoffmann321f0482013-03-12 14:39:22 +0100121 DisplaySurface *surface;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +0100122 int dcls;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200123
pbrook95219892006-04-09 01:06:34 +0000124 /* Graphic console state. */
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +0200125 Object *device;
Gerd Hoffmann56437062014-01-24 15:35:21 +0100126 uint32_t head;
Gerd Hoffmann6f90f3d2014-01-24 17:38:20 +0100127 QemuUIInfo ui_info;
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100128 const GraphicHwOps *hw_ops;
pbrook95219892006-04-09 01:06:34 +0000129 void *hw;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200130
131 /* Text console state */
bellarde7f0ad52004-07-14 17:28:59 +0000132 int width;
133 int height;
134 int total_height;
135 int backscroll_height;
bellarde7f0ad52004-07-14 17:28:59 +0000136 int x, y;
thsadb47962007-01-16 23:02:36 +0000137 int x_saved, y_saved;
bellarde7f0ad52004-07-14 17:28:59 +0000138 int y_displayed;
139 int y_base;
pbrook6d6f7c22006-03-11 15:35:30 +0000140 TextAttributes t_attrib_default; /* default text attributes */
141 TextAttributes t_attrib; /* currently active text attributes */
bellarde7f0ad52004-07-14 17:28:59 +0000142 TextCell *cells;
balrog4d3b6f62008-02-10 16:33:14 +0000143 int text_x[2], text_y[2], cursor_invalidate;
Paolo Bonzini41048332010-12-23 13:42:52 +0100144 int echo;
bellarde7f0ad52004-07-14 17:28:59 +0000145
pbrook14778c22009-01-21 03:02:52 +0000146 int update_x0;
147 int update_y0;
148 int update_x1;
149 int update_y1;
150
bellarde7f0ad52004-07-14 17:28:59 +0000151 enum TTYState state;
152 int esc_params[MAX_ESC_PARAMS];
153 int nb_esc_params;
154
pbrooke5b0bc42007-01-27 23:46:43 +0000155 CharDriverState *chr;
bellarde15d7372006-06-25 16:26:29 +0000156 /* fifo for key pressed */
157 QEMUFIFO out_fifo;
158 uint8_t out_fifo_buf[16];
159 QEMUTimer *kbd_timer;
bellarde7f0ad52004-07-14 17:28:59 +0000160};
161
Gerd Hoffmann27be5582013-03-13 12:25:25 +0100162struct DisplayState {
Stefan Weil1246b252013-12-01 08:49:47 +0100163 QEMUTimer *gui_timer;
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100164 uint64_t last_update;
165 uint64_t update_interval;
166 bool refreshing;
Gerd Hoffmann27be5582013-03-13 12:25:25 +0100167 bool have_gfx;
168 bool have_text;
169
170 QLIST_HEAD(, DisplayChangeListener) listeners;
171};
172
Paolo Bonzini98b50082010-02-11 00:29:57 +0100173static DisplayState *display_state;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200174static QemuConsole *active_console;
Gerd Hoffmanna1d2db02014-05-26 10:36:35 +0200175static QemuConsole **consoles;
bellarde7f0ad52004-07-14 17:28:59 +0000176static int nb_consoles = 0;
Gerd Hoffmannaea79472014-05-22 11:27:13 +0200177static bool cursor_visible_phase;
178static QEMUTimer *cursor_timer;
bellarde7f0ad52004-07-14 17:28:59 +0000179
Gerd Hoffmann64840c62013-03-07 17:08:29 +0100180static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100181static void dpy_refresh(DisplayState *s);
Gerd Hoffmann52090892013-04-23 15:44:31 +0200182static DisplayState *get_alloc_displaystate(void);
Gerd Hoffmannaea79472014-05-22 11:27:13 +0200183static void text_console_update_cursor_timer(void);
184static void text_console_update_cursor(void *opaque);
Gerd Hoffmann64840c62013-03-07 17:08:29 +0100185
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100186static void gui_update(void *opaque)
187{
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100188 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
189 uint64_t dcl_interval;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100190 DisplayState *ds = opaque;
191 DisplayChangeListener *dcl;
Gerd Hoffmanndea1b0b2013-03-19 15:01:02 +0100192 int i;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100193
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100194 ds->refreshing = true;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100195 dpy_refresh(ds);
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100196 ds->refreshing = false;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100197
198 QLIST_FOREACH(dcl, &ds->listeners, next) {
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100199 dcl_interval = dcl->update_interval ?
200 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
201 if (interval > dcl_interval) {
202 interval = dcl_interval;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100203 }
204 }
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100205 if (ds->update_interval != interval) {
206 ds->update_interval = interval;
Gerd Hoffmanndea1b0b2013-03-19 15:01:02 +0100207 for (i = 0; i < nb_consoles; i++) {
208 if (consoles[i]->hw_ops->update_interval) {
209 consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval);
210 }
211 }
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100212 trace_console_refresh(interval);
213 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100214 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
215 timer_mod(ds->gui_timer, ds->last_update + interval);
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100216}
217
218static void gui_setup_refresh(DisplayState *ds)
219{
220 DisplayChangeListener *dcl;
221 bool need_timer = false;
222 bool have_gfx = false;
223 bool have_text = false;
224
225 QLIST_FOREACH(dcl, &ds->listeners, next) {
226 if (dcl->ops->dpy_refresh != NULL) {
227 need_timer = true;
228 }
229 if (dcl->ops->dpy_gfx_update != NULL) {
230 have_gfx = true;
231 }
232 if (dcl->ops->dpy_text_update != NULL) {
233 have_text = true;
234 }
235 }
236
237 if (need_timer && ds->gui_timer == NULL) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100238 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
239 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100240 }
241 if (!need_timer && ds->gui_timer != NULL) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100242 timer_del(ds->gui_timer);
243 timer_free(ds->gui_timer);
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100244 ds->gui_timer = NULL;
245 }
246
247 ds->have_gfx = have_gfx;
248 ds->have_text = have_text;
249}
250
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100251void graphic_hw_update(QemuConsole *con)
pbrook95219892006-04-09 01:06:34 +0000252{
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100253 if (!con) {
254 con = active_console;
255 }
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100256 if (con && con->hw_ops->gfx_update) {
257 con->hw_ops->gfx_update(con->hw);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100258 }
pbrook95219892006-04-09 01:06:34 +0000259}
260
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100261void graphic_hw_invalidate(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->invalidate) {
267 con->hw_ops->invalidate(con->hw);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100268 }
pbrook95219892006-04-09 01:06:34 +0000269}
270
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100271static void ppm_save(const char *filename, struct DisplaySurface *ds,
272 Error **errp)
273{
274 int width = pixman_image_get_width(ds->image);
275 int height = pixman_image_get_height(ds->image);
Gerd Hoffmanncdd5b932013-04-23 13:26:59 +0200276 int fd;
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100277 FILE *f;
278 int y;
279 int ret;
280 pixman_image_t *linebuf;
281
282 trace_ppm_save(filename, ds);
Gerd Hoffmanncdd5b932013-04-23 13:26:59 +0200283 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
284 if (fd == -1) {
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100285 error_setg(errp, "failed to open file '%s': %s", filename,
286 strerror(errno));
287 return;
288 }
Gerd Hoffmanncdd5b932013-04-23 13:26:59 +0200289 f = fdopen(fd, "wb");
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100290 ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
291 if (ret < 0) {
292 linebuf = NULL;
293 goto write_err;
294 }
295 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
296 for (y = 0; y < height; y++) {
297 qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
298 clearerr(f);
299 ret = fwrite(pixman_image_get_data(linebuf), 1,
300 pixman_image_get_stride(linebuf), f);
301 (void)ret;
302 if (ferror(f)) {
303 goto write_err;
304 }
305 }
306
307out:
308 qemu_pixman_image_unref(linebuf);
309 fclose(f);
310 return;
311
312write_err:
313 error_setg(errp, "failed to write to file '%s': %s", filename,
314 strerror(errno));
315 unlink(filename);
316 goto out;
317}
318
Luiz Capitulinoad39cf62012-05-24 13:48:23 -0300319void qmp_screendump(const char *filename, Error **errp)
pbrook95219892006-04-09 01:06:34 +0000320{
Gerd Hoffmann284d1c62013-03-15 15:45:54 +0100321 QemuConsole *con = qemu_console_lookup_by_index(0);
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100322 DisplaySurface *surface;
balrog8571c052008-07-19 13:04:26 +0000323
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100324 if (con == NULL) {
325 error_setg(errp, "There is no QemuConsole I can screendump from.");
326 return;
Jan Kiszkaf81bdef2011-09-16 00:48:07 +0200327 }
328
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100329 graphic_hw_update(con);
330 surface = qemu_console_surface(con);
331 ppm_save(filename, surface, errp);
pbrook95219892006-04-09 01:06:34 +0000332}
333
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100334void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
balrog4d3b6f62008-02-10 16:33:14 +0000335{
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100336 if (!con) {
337 con = active_console;
338 }
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100339 if (con && con->hw_ops->text_update) {
340 con->hw_ops->text_update(con->hw, chardata);
341 }
balrog4d3b6f62008-02-10 16:33:14 +0000342}
343
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100344static void vga_fill_rect(QemuConsole *con,
345 int posx, int posy, int width, int height,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100346 pixman_color_t color)
bellarde7f0ad52004-07-14 17:28:59 +0000347{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100348 DisplaySurface *surface = qemu_console_surface(con);
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100349 pixman_rectangle16_t rect = {
350 .x = posx, .y = posy, .width = width, .height = height
351 };
ths3b46e622007-09-17 08:09:54 +0000352
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100353 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100354 &color, 1, &rect);
bellarde7f0ad52004-07-14 17:28:59 +0000355}
356
357/* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100358static void vga_bitblt(QemuConsole *con,
359 int xs, int ys, int xd, int yd, int w, int h)
bellarde7f0ad52004-07-14 17:28:59 +0000360{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100361 DisplaySurface *surface = qemu_console_surface(con);
bellarde7f0ad52004-07-14 17:28:59 +0000362
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100363 pixman_image_composite(PIXMAN_OP_SRC,
364 surface->image, NULL, surface->image,
365 xs, ys, 0, 0, xd, yd, w, h);
bellarde7f0ad52004-07-14 17:28:59 +0000366}
367
368/***********************************************************/
369/* basic char display */
370
371#define FONT_HEIGHT 16
372#define FONT_WIDTH 8
373
374#include "vgafont.h"
375
Devin J. Pohlydf00bed2011-09-07 15:44:36 -0400376#ifndef CONFIG_CURSES
pbrook6d6f7c22006-03-11 15:35:30 +0000377enum color_names {
378 COLOR_BLACK = 0,
379 COLOR_RED = 1,
380 COLOR_GREEN = 2,
381 COLOR_YELLOW = 3,
382 COLOR_BLUE = 4,
383 COLOR_MAGENTA = 5,
384 COLOR_CYAN = 6,
385 COLOR_WHITE = 7
386};
Devin J. Pohlydf00bed2011-09-07 15:44:36 -0400387#endif
pbrook6d6f7c22006-03-11 15:35:30 +0000388
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100389#define QEMU_RGB(r, g, b) \
390 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
391
392static const pixman_color_t color_table_rgb[2][8] = {
pbrook6d6f7c22006-03-11 15:35:30 +0000393 { /* dark */
bellard26489842006-06-25 17:37:36 +0000394 QEMU_RGB(0x00, 0x00, 0x00), /* black */
395 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
396 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
397 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
398 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
399 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
400 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
401 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
pbrook6d6f7c22006-03-11 15:35:30 +0000402 },
403 { /* bright */
bellard26489842006-06-25 17:37:36 +0000404 QEMU_RGB(0x00, 0x00, 0x00), /* black */
405 QEMU_RGB(0xff, 0x00, 0x00), /* red */
406 QEMU_RGB(0x00, 0xff, 0x00), /* green */
407 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
408 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
409 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
410 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
411 QEMU_RGB(0xff, 0xff, 0xff), /* white */
pbrook6d6f7c22006-03-11 15:35:30 +0000412 }
bellarde7f0ad52004-07-14 17:28:59 +0000413};
414
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100415static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
pbrook6d6f7c22006-03-11 15:35:30 +0000416 TextAttributes *t_attrib)
bellarde7f0ad52004-07-14 17:28:59 +0000417{
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100418 static pixman_image_t *glyphs[256];
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100419 DisplaySurface *surface = qemu_console_surface(s);
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100420 pixman_color_t fgcol, bgcol;
pbrook6d6f7c22006-03-11 15:35:30 +0000421
422 if (t_attrib->invers) {
Gerd Hoffmanncf6f0542013-03-06 09:50:51 +0100423 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
424 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
pbrook6d6f7c22006-03-11 15:35:30 +0000425 } else {
Gerd Hoffmanncf6f0542013-03-06 09:50:51 +0100426 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
427 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
pbrook6d6f7c22006-03-11 15:35:30 +0000428 }
bellarde7f0ad52004-07-14 17:28:59 +0000429
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100430 if (!glyphs[ch]) {
431 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
bellarde7f0ad52004-07-14 17:28:59 +0000432 }
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100433 qemu_pixman_glyph_render(glyphs[ch], surface->image,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100434 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
bellarde7f0ad52004-07-14 17:28:59 +0000435}
436
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200437static void text_console_resize(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000438{
439 TextCell *cells, *c, *c1;
440 int w1, x, y, last_width;
441
442 last_width = s->width;
Gerd Hoffmann36671fb2013-03-13 10:14:52 +0100443 s->width = surface_width(s->surface) / FONT_WIDTH;
444 s->height = surface_height(s->surface) / FONT_HEIGHT;
bellarde7f0ad52004-07-14 17:28:59 +0000445
446 w1 = last_width;
447 if (s->width < w1)
448 w1 = s->width;
449
Anthony Liguori7267c092011-08-20 22:09:37 -0500450 cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
bellarde7f0ad52004-07-14 17:28:59 +0000451 for(y = 0; y < s->total_height; y++) {
452 c = &cells[y * s->width];
453 if (w1 > 0) {
454 c1 = &s->cells[y * last_width];
455 for(x = 0; x < w1; x++) {
456 *c++ = *c1++;
457 }
458 }
459 for(x = w1; x < s->width; x++) {
460 c->ch = ' ';
pbrook6d6f7c22006-03-11 15:35:30 +0000461 c->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +0000462 c++;
463 }
464 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500465 g_free(s->cells);
bellarde7f0ad52004-07-14 17:28:59 +0000466 s->cells = cells;
467}
468
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200469static inline void text_update_xy(QemuConsole *s, int x, int y)
balrog4d3b6f62008-02-10 16:33:14 +0000470{
471 s->text_x[0] = MIN(s->text_x[0], x);
472 s->text_x[1] = MAX(s->text_x[1], x);
473 s->text_y[0] = MIN(s->text_y[0], y);
474 s->text_y[1] = MAX(s->text_y[1], y);
475}
476
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200477static void invalidate_xy(QemuConsole *s, int x, int y)
pbrook14778c22009-01-21 03:02:52 +0000478{
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200479 if (!qemu_console_is_visible(s)) {
480 return;
481 }
pbrook14778c22009-01-21 03:02:52 +0000482 if (s->update_x0 > x * FONT_WIDTH)
483 s->update_x0 = x * FONT_WIDTH;
484 if (s->update_y0 > y * FONT_HEIGHT)
485 s->update_y0 = y * FONT_HEIGHT;
486 if (s->update_x1 < (x + 1) * FONT_WIDTH)
487 s->update_x1 = (x + 1) * FONT_WIDTH;
488 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
489 s->update_y1 = (y + 1) * FONT_HEIGHT;
490}
491
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200492static void update_xy(QemuConsole *s, int x, int y)
bellarde7f0ad52004-07-14 17:28:59 +0000493{
494 TextCell *c;
495 int y1, y2;
496
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100497 if (s->ds->have_text) {
498 text_update_xy(s, x, y);
499 }
500
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200501 y1 = (s->y_base + y) % s->total_height;
502 y2 = y1 - s->y_displayed;
503 if (y2 < 0) {
504 y2 += s->total_height;
505 }
506 if (y2 < s->height) {
507 c = &s->cells[y1 * s->width + x];
508 vga_putcharxy(s, x, y2, c->ch,
509 &(c->t_attrib));
510 invalidate_xy(s, x, y2);
bellarde7f0ad52004-07-14 17:28:59 +0000511 }
512}
513
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200514static void console_show_cursor(QemuConsole *s, int show)
bellarde7f0ad52004-07-14 17:28:59 +0000515{
516 TextCell *c;
517 int y, y1;
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100518 int x = s->x;
bellarde7f0ad52004-07-14 17:28:59 +0000519
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100520 if (s->ds->have_text) {
521 s->cursor_invalidate = 1;
522 }
balrog4d3b6f62008-02-10 16:33:14 +0000523
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200524 if (x >= s->width) {
525 x = s->width - 1;
526 }
527 y1 = (s->y_base + s->y) % s->total_height;
528 y = y1 - s->y_displayed;
529 if (y < 0) {
530 y += s->total_height;
531 }
532 if (y < s->height) {
533 c = &s->cells[y1 * s->width + x];
Gerd Hoffmannaea79472014-05-22 11:27:13 +0200534 if (show && cursor_visible_phase) {
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200535 TextAttributes t_attrib = s->t_attrib_default;
536 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
537 vga_putcharxy(s, x, y, c->ch, &t_attrib);
538 } else {
539 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
thsed8276a2007-02-10 22:37:56 +0000540 }
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200541 invalidate_xy(s, x, y);
bellarde7f0ad52004-07-14 17:28:59 +0000542 }
543}
544
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200545static void console_refresh(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000546{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100547 DisplaySurface *surface = qemu_console_surface(s);
bellarde7f0ad52004-07-14 17:28:59 +0000548 TextCell *c;
549 int x, y, y1;
550
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200551 if (s->ds->have_text) {
balrog4d3b6f62008-02-10 16:33:14 +0000552 s->text_x[0] = 0;
553 s->text_y[0] = 0;
554 s->text_x[1] = s->width - 1;
555 s->text_y[1] = s->height - 1;
556 s->cursor_invalidate = 1;
balrog4d3b6f62008-02-10 16:33:14 +0000557 }
bellarde7f0ad52004-07-14 17:28:59 +0000558
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200559 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
560 color_table_rgb[0][COLOR_BLACK]);
561 y1 = s->y_displayed;
562 for (y = 0; y < s->height; y++) {
563 c = s->cells + y1 * s->width;
564 for (x = 0; x < s->width; x++) {
565 vga_putcharxy(s, x, y, c->ch,
566 &(c->t_attrib));
567 c++;
bellarde7f0ad52004-07-14 17:28:59 +0000568 }
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200569 if (++y1 == s->total_height) {
570 y1 = 0;
571 }
bellarde7f0ad52004-07-14 17:28:59 +0000572 }
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200573 console_show_cursor(s, 1);
574 dpy_gfx_update(s, 0, 0,
575 surface_width(surface), surface_height(surface));
bellarde7f0ad52004-07-14 17:28:59 +0000576}
577
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100578static void console_scroll(QemuConsole *s, int ydelta)
bellarde7f0ad52004-07-14 17:28:59 +0000579{
bellarde7f0ad52004-07-14 17:28:59 +0000580 int i, y1;
ths3b46e622007-09-17 08:09:54 +0000581
bellarde7f0ad52004-07-14 17:28:59 +0000582 if (ydelta > 0) {
583 for(i = 0; i < ydelta; i++) {
584 if (s->y_displayed == s->y_base)
585 break;
586 if (++s->y_displayed == s->total_height)
587 s->y_displayed = 0;
588 }
589 } else {
590 ydelta = -ydelta;
591 i = s->backscroll_height;
592 if (i > s->total_height - s->height)
593 i = s->total_height - s->height;
594 y1 = s->y_base - i;
595 if (y1 < 0)
596 y1 += s->total_height;
597 for(i = 0; i < ydelta; i++) {
598 if (s->y_displayed == y1)
599 break;
600 if (--s->y_displayed < 0)
601 s->y_displayed = s->total_height - 1;
602 }
603 }
604 console_refresh(s);
605}
606
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200607static void console_put_lf(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000608{
609 TextCell *c;
610 int x, y1;
611
bellarde7f0ad52004-07-14 17:28:59 +0000612 s->y++;
613 if (s->y >= s->height) {
614 s->y = s->height - 1;
pbrook6d6f7c22006-03-11 15:35:30 +0000615
bellarde7f0ad52004-07-14 17:28:59 +0000616 if (s->y_displayed == s->y_base) {
617 if (++s->y_displayed == s->total_height)
618 s->y_displayed = 0;
619 }
620 if (++s->y_base == s->total_height)
621 s->y_base = 0;
622 if (s->backscroll_height < s->total_height)
623 s->backscroll_height++;
624 y1 = (s->y_base + s->height - 1) % s->total_height;
625 c = &s->cells[y1 * s->width];
626 for(x = 0; x < s->width; x++) {
627 c->ch = ' ';
pbrook6d6f7c22006-03-11 15:35:30 +0000628 c->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +0000629 c++;
630 }
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200631 if (s->y_displayed == s->y_base) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100632 if (s->ds->have_text) {
balrog4d3b6f62008-02-10 16:33:14 +0000633 s->text_x[0] = 0;
634 s->text_y[0] = 0;
635 s->text_x[1] = s->width - 1;
636 s->text_y[1] = s->height - 1;
balrog4d3b6f62008-02-10 16:33:14 +0000637 }
638
Gerd Hoffmannb35e3ba2014-05-22 11:01:42 +0200639 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
640 s->width * FONT_WIDTH,
641 (s->height - 1) * FONT_HEIGHT);
642 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
643 s->width * FONT_WIDTH, FONT_HEIGHT,
644 color_table_rgb[0][s->t_attrib_default.bgcol]);
645 s->update_x0 = 0;
646 s->update_y0 = 0;
647 s->update_x1 = s->width * FONT_WIDTH;
648 s->update_y1 = s->height * FONT_HEIGHT;
bellarde7f0ad52004-07-14 17:28:59 +0000649 }
650 }
651}
652
pbrook6d6f7c22006-03-11 15:35:30 +0000653/* Set console attributes depending on the current escape codes.
654 * NOTE: I know this code is not very efficient (checking every color for it
655 * self) but it is more readable and better maintainable.
656 */
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200657static void console_handle_escape(QemuConsole *s)
pbrook6d6f7c22006-03-11 15:35:30 +0000658{
659 int i;
660
pbrook6d6f7c22006-03-11 15:35:30 +0000661 for (i=0; i<s->nb_esc_params; i++) {
662 switch (s->esc_params[i]) {
663 case 0: /* reset all console attributes to default */
664 s->t_attrib = s->t_attrib_default;
665 break;
666 case 1:
667 s->t_attrib.bold = 1;
668 break;
669 case 4:
670 s->t_attrib.uline = 1;
671 break;
672 case 5:
673 s->t_attrib.blink = 1;
674 break;
675 case 7:
676 s->t_attrib.invers = 1;
677 break;
678 case 8:
679 s->t_attrib.unvisible = 1;
680 break;
681 case 22:
682 s->t_attrib.bold = 0;
683 break;
684 case 24:
685 s->t_attrib.uline = 0;
686 break;
687 case 25:
688 s->t_attrib.blink = 0;
689 break;
690 case 27:
691 s->t_attrib.invers = 0;
692 break;
693 case 28:
694 s->t_attrib.unvisible = 0;
695 break;
696 /* set foreground color */
697 case 30:
698 s->t_attrib.fgcol=COLOR_BLACK;
699 break;
700 case 31:
701 s->t_attrib.fgcol=COLOR_RED;
702 break;
703 case 32:
704 s->t_attrib.fgcol=COLOR_GREEN;
705 break;
706 case 33:
707 s->t_attrib.fgcol=COLOR_YELLOW;
708 break;
709 case 34:
710 s->t_attrib.fgcol=COLOR_BLUE;
711 break;
712 case 35:
713 s->t_attrib.fgcol=COLOR_MAGENTA;
714 break;
715 case 36:
716 s->t_attrib.fgcol=COLOR_CYAN;
717 break;
718 case 37:
719 s->t_attrib.fgcol=COLOR_WHITE;
720 break;
721 /* set background color */
722 case 40:
723 s->t_attrib.bgcol=COLOR_BLACK;
724 break;
725 case 41:
726 s->t_attrib.bgcol=COLOR_RED;
727 break;
728 case 42:
729 s->t_attrib.bgcol=COLOR_GREEN;
730 break;
731 case 43:
732 s->t_attrib.bgcol=COLOR_YELLOW;
733 break;
734 case 44:
735 s->t_attrib.bgcol=COLOR_BLUE;
736 break;
737 case 45:
738 s->t_attrib.bgcol=COLOR_MAGENTA;
739 break;
740 case 46:
741 s->t_attrib.bgcol=COLOR_CYAN;
742 break;
743 case 47:
744 s->t_attrib.bgcol=COLOR_WHITE;
745 break;
746 }
747 }
748}
749
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200750static void console_clear_xy(QemuConsole *s, int x, int y)
thsadb47962007-01-16 23:02:36 +0000751{
752 int y1 = (s->y_base + y) % s->total_height;
753 TextCell *c = &s->cells[y1 * s->width + x];
754 c->ch = ' ';
755 c->t_attrib = s->t_attrib_default;
thsadb47962007-01-16 23:02:36 +0000756 update_xy(s, x, y);
757}
758
Ian Campbell3eea5492012-09-04 10:26:09 -0500759/* set cursor, checking bounds */
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200760static void set_cursor(QemuConsole *s, int x, int y)
Ian Campbell3eea5492012-09-04 10:26:09 -0500761{
762 if (x < 0) {
763 x = 0;
764 }
765 if (y < 0) {
766 y = 0;
767 }
768 if (y >= s->height) {
769 y = s->height - 1;
770 }
771 if (x >= s->width) {
772 x = s->width - 1;
773 }
774
775 s->x = x;
776 s->y = y;
777}
778
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200779static void console_putchar(QemuConsole *s, int ch)
bellarde7f0ad52004-07-14 17:28:59 +0000780{
781 TextCell *c;
thsadb47962007-01-16 23:02:36 +0000782 int y1, i;
783 int x, y;
bellarde7f0ad52004-07-14 17:28:59 +0000784
785 switch(s->state) {
786 case TTY_STATE_NORM:
787 switch(ch) {
pbrook6d6f7c22006-03-11 15:35:30 +0000788 case '\r': /* carriage return */
bellarde7f0ad52004-07-14 17:28:59 +0000789 s->x = 0;
790 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000791 case '\n': /* newline */
bellarde7f0ad52004-07-14 17:28:59 +0000792 console_put_lf(s);
793 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000794 case '\b': /* backspace */
ths5fafdf22007-09-16 21:08:06 +0000795 if (s->x > 0)
bellarde15d7372006-06-25 16:26:29 +0000796 s->x--;
pbrook6d6f7c22006-03-11 15:35:30 +0000797 break;
798 case '\t': /* tabspace */
799 if (s->x + (8 - (s->x % 8)) > s->width) {
bellardbd468842006-07-14 20:24:31 +0000800 s->x = 0;
pbrook6d6f7c22006-03-11 15:35:30 +0000801 console_put_lf(s);
802 } else {
803 s->x = s->x + (8 - (s->x % 8));
804 }
805 break;
806 case '\a': /* alert aka. bell */
807 /* TODO: has to be implemented */
808 break;
thsadb47962007-01-16 23:02:36 +0000809 case 14:
810 /* SI (shift in), character set 0 (ignored) */
811 break;
812 case 15:
813 /* SO (shift out), character set 1 (ignored) */
814 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000815 case 27: /* esc (introducing an escape sequence) */
bellarde7f0ad52004-07-14 17:28:59 +0000816 s->state = TTY_STATE_ESC;
817 break;
818 default:
thsed8276a2007-02-10 22:37:56 +0000819 if (s->x >= s->width) {
820 /* line wrap */
821 s->x = 0;
822 console_put_lf(s);
thsadb47962007-01-16 23:02:36 +0000823 }
bellarde7f0ad52004-07-14 17:28:59 +0000824 y1 = (s->y_base + s->y) % s->total_height;
825 c = &s->cells[y1 * s->width + s->x];
826 c->ch = ch;
pbrook6d6f7c22006-03-11 15:35:30 +0000827 c->t_attrib = s->t_attrib;
bellarde7f0ad52004-07-14 17:28:59 +0000828 update_xy(s, s->x, s->y);
829 s->x++;
bellarde7f0ad52004-07-14 17:28:59 +0000830 break;
831 }
832 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000833 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
bellarde7f0ad52004-07-14 17:28:59 +0000834 if (ch == '[') {
835 for(i=0;i<MAX_ESC_PARAMS;i++)
836 s->esc_params[i] = 0;
837 s->nb_esc_params = 0;
838 s->state = TTY_STATE_CSI;
839 } else {
840 s->state = TTY_STATE_NORM;
841 }
842 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000843 case TTY_STATE_CSI: /* handle escape sequence parameters */
bellarde7f0ad52004-07-14 17:28:59 +0000844 if (ch >= '0' && ch <= '9') {
845 if (s->nb_esc_params < MAX_ESC_PARAMS) {
Laszlo Ersekc10600a2012-09-17 11:10:03 +0200846 int *param = &s->esc_params[s->nb_esc_params];
847 int digit = (ch - '0');
848
849 *param = (*param <= (INT_MAX - digit) / 10) ?
850 *param * 10 + digit : INT_MAX;
bellarde7f0ad52004-07-14 17:28:59 +0000851 }
852 } else {
Ian Campbell3eea5492012-09-04 10:26:09 -0500853 if (s->nb_esc_params < MAX_ESC_PARAMS)
854 s->nb_esc_params++;
bellarde7f0ad52004-07-14 17:28:59 +0000855 if (ch == ';')
856 break;
Stefan Weil5d28b0e2013-11-10 16:04:16 +0100857 trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
858 ch, s->nb_esc_params);
bellarde7f0ad52004-07-14 17:28:59 +0000859 s->state = TTY_STATE_NORM;
860 switch(ch) {
thsadb47962007-01-16 23:02:36 +0000861 case 'A':
862 /* move cursor up */
863 if (s->esc_params[0] == 0) {
864 s->esc_params[0] = 1;
865 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500866 set_cursor(s, s->x, s->y - s->esc_params[0]);
bellarde7f0ad52004-07-14 17:28:59 +0000867 break;
thsadb47962007-01-16 23:02:36 +0000868 case 'B':
869 /* move cursor down */
870 if (s->esc_params[0] == 0) {
871 s->esc_params[0] = 1;
872 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500873 set_cursor(s, s->x, s->y + s->esc_params[0]);
thsadb47962007-01-16 23:02:36 +0000874 break;
875 case 'C':
876 /* move cursor right */
877 if (s->esc_params[0] == 0) {
878 s->esc_params[0] = 1;
879 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500880 set_cursor(s, s->x + s->esc_params[0], s->y);
thsadb47962007-01-16 23:02:36 +0000881 break;
882 case 'D':
883 /* move cursor left */
884 if (s->esc_params[0] == 0) {
885 s->esc_params[0] = 1;
886 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500887 set_cursor(s, s->x - s->esc_params[0], s->y);
thsadb47962007-01-16 23:02:36 +0000888 break;
889 case 'G':
890 /* move cursor to column */
Ian Campbell3eea5492012-09-04 10:26:09 -0500891 set_cursor(s, s->esc_params[0] - 1, s->y);
thsadb47962007-01-16 23:02:36 +0000892 break;
893 case 'f':
894 case 'H':
895 /* move cursor to row, column */
Ian Campbell3eea5492012-09-04 10:26:09 -0500896 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
thsadb47962007-01-16 23:02:36 +0000897 break;
898 case 'J':
899 switch (s->esc_params[0]) {
900 case 0:
901 /* clear to end of screen */
902 for (y = s->y; y < s->height; y++) {
903 for (x = 0; x < s->width; x++) {
904 if (y == s->y && x < s->x) {
905 continue;
906 }
907 console_clear_xy(s, x, y);
908 }
909 }
910 break;
911 case 1:
912 /* clear from beginning of screen */
913 for (y = 0; y <= s->y; y++) {
914 for (x = 0; x < s->width; x++) {
915 if (y == s->y && x > s->x) {
916 break;
917 }
918 console_clear_xy(s, x, y);
919 }
920 }
921 break;
922 case 2:
923 /* clear entire screen */
924 for (y = 0; y <= s->height; y++) {
925 for (x = 0; x < s->width; x++) {
926 console_clear_xy(s, x, y);
927 }
928 }
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100929 break;
thsadb47962007-01-16 23:02:36 +0000930 }
Markus Armbruster95d8f9f2011-11-22 11:59:07 +0100931 break;
thsadb47962007-01-16 23:02:36 +0000932 case 'K':
933 switch (s->esc_params[0]) {
934 case 0:
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100935 /* clear to eol */
936 for(x = s->x; x < s->width; x++) {
thsadb47962007-01-16 23:02:36 +0000937 console_clear_xy(s, x, s->y);
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100938 }
939 break;
thsadb47962007-01-16 23:02:36 +0000940 case 1:
941 /* clear from beginning of line */
942 for (x = 0; x <= s->x; x++) {
943 console_clear_xy(s, x, s->y);
944 }
945 break;
946 case 2:
947 /* clear entire line */
948 for(x = 0; x < s->width; x++) {
949 console_clear_xy(s, x, s->y);
950 }
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100951 break;
952 }
thsadb47962007-01-16 23:02:36 +0000953 break;
954 case 'm':
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100955 console_handle_escape(s);
956 break;
thsadb47962007-01-16 23:02:36 +0000957 case 'n':
958 /* report cursor position */
959 /* TODO: send ESC[row;colR */
960 break;
961 case 's':
962 /* save cursor position */
963 s->x_saved = s->x;
964 s->y_saved = s->y;
965 break;
966 case 'u':
967 /* restore cursor position */
968 s->x = s->x_saved;
969 s->y = s->y_saved;
970 break;
971 default:
Stefan Weil5d28b0e2013-11-10 16:04:16 +0100972 trace_console_putchar_unhandled(ch);
thsadb47962007-01-16 23:02:36 +0000973 break;
974 }
975 break;
bellarde7f0ad52004-07-14 17:28:59 +0000976 }
977 }
978}
979
980void console_select(unsigned int index)
981{
Gerd Hoffmann284d1c62013-03-15 15:45:54 +0100982 DisplayChangeListener *dcl;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200983 QemuConsole *s;
pbrook6d6f7c22006-03-11 15:35:30 +0000984
Gerd Hoffmann437fe102013-03-07 16:04:52 +0100985 trace_console_select(index);
Gerd Hoffmann284d1c62013-03-15 15:45:54 +0100986 s = qemu_console_lookup_by_index(index);
bellarde7f0ad52004-07-14 17:28:59 +0000987 if (s) {
aliguori7d957bd2009-01-15 22:14:11 +0000988 DisplayState *ds = s->ds;
Jan Kiszkabf1bed82012-07-10 22:00:55 +0200989
bellarde7f0ad52004-07-14 17:28:59 +0000990 active_console = s;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200991 if (ds->have_gfx) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +0100992 QLIST_FOREACH(dcl, &ds->listeners, next) {
993 if (dcl->con != NULL) {
994 continue;
995 }
996 if (dcl->ops->dpy_gfx_switch) {
997 dcl->ops->dpy_gfx_switch(dcl, s->surface);
998 }
999 }
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001000 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1001 surface_height(s->surface));
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001002 }
1003 if (ds->have_text) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001004 dpy_text_resize(s, s->width, s->height);
aliguori68f00992009-01-21 18:59:12 +00001005 }
Gerd Hoffmannaea79472014-05-22 11:27:13 +02001006 text_console_update_cursor(NULL);
bellarde7f0ad52004-07-14 17:28:59 +00001007 }
1008}
1009
1010static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1011{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001012 QemuConsole *s = chr->opaque;
bellarde7f0ad52004-07-14 17:28:59 +00001013 int i;
1014
pbrook14778c22009-01-21 03:02:52 +00001015 s->update_x0 = s->width * FONT_WIDTH;
1016 s->update_y0 = s->height * FONT_HEIGHT;
1017 s->update_x1 = 0;
1018 s->update_y1 = 0;
bellarde7f0ad52004-07-14 17:28:59 +00001019 console_show_cursor(s, 0);
1020 for(i = 0; i < len; i++) {
1021 console_putchar(s, buf[i]);
1022 }
1023 console_show_cursor(s, 1);
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001024 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001025 dpy_gfx_update(s, s->update_x0, s->update_y0,
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001026 s->update_x1 - s->update_x0,
1027 s->update_y1 - s->update_y0);
pbrook14778c22009-01-21 03:02:52 +00001028 }
bellarde7f0ad52004-07-14 17:28:59 +00001029 return len;
1030}
1031
bellarde15d7372006-06-25 16:26:29 +00001032static void kbd_send_chars(void *opaque)
1033{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001034 QemuConsole *s = opaque;
bellarde15d7372006-06-25 16:26:29 +00001035 int len;
1036 uint8_t buf[16];
ths3b46e622007-09-17 08:09:54 +00001037
Anthony Liguori909cda12011-08-15 11:17:31 -05001038 len = qemu_chr_be_can_write(s->chr);
bellarde15d7372006-06-25 16:26:29 +00001039 if (len > s->out_fifo.count)
1040 len = s->out_fifo.count;
1041 if (len > 0) {
1042 if (len > sizeof(buf))
1043 len = sizeof(buf);
1044 qemu_fifo_read(&s->out_fifo, buf, len);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001045 qemu_chr_be_write(s->chr, buf, len);
bellarde15d7372006-06-25 16:26:29 +00001046 }
1047 /* characters are pending: we send them a bit later (XXX:
1048 horrible, should change char device API) */
1049 if (s->out_fifo.count > 0) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001050 timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
bellarde15d7372006-06-25 16:26:29 +00001051 }
1052}
1053
bellarde7f0ad52004-07-14 17:28:59 +00001054/* called when an ascii key is pressed */
Gerd Hoffmann3f9a6e82014-05-22 12:05:52 +02001055void kbd_put_keysym_console(QemuConsole *s, int keysym)
bellarde7f0ad52004-07-14 17:28:59 +00001056{
bellarde7f0ad52004-07-14 17:28:59 +00001057 uint8_t buf[16], *q;
1058 int c;
1059
thsaf3a9032007-07-11 23:14:59 +00001060 if (!s || (s->console_type == GRAPHIC_CONSOLE))
bellarde7f0ad52004-07-14 17:28:59 +00001061 return;
1062
1063 switch(keysym) {
1064 case QEMU_KEY_CTRL_UP:
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001065 console_scroll(s, -1);
bellarde7f0ad52004-07-14 17:28:59 +00001066 break;
1067 case QEMU_KEY_CTRL_DOWN:
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001068 console_scroll(s, 1);
bellarde7f0ad52004-07-14 17:28:59 +00001069 break;
1070 case QEMU_KEY_CTRL_PAGEUP:
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001071 console_scroll(s, -10);
bellarde7f0ad52004-07-14 17:28:59 +00001072 break;
1073 case QEMU_KEY_CTRL_PAGEDOWN:
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001074 console_scroll(s, 10);
bellarde7f0ad52004-07-14 17:28:59 +00001075 break;
1076 default:
bellarde15d7372006-06-25 16:26:29 +00001077 /* convert the QEMU keysym to VT100 key string */
1078 q = buf;
1079 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1080 *q++ = '\033';
1081 *q++ = '[';
1082 c = keysym - 0xe100;
1083 if (c >= 10)
1084 *q++ = '0' + (c / 10);
1085 *q++ = '0' + (c % 10);
1086 *q++ = '~';
1087 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1088 *q++ = '\033';
1089 *q++ = '[';
1090 *q++ = keysym & 0xff;
Paolo Bonzini41048332010-12-23 13:42:52 +01001091 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1092 console_puts(s->chr, (const uint8_t *) "\r", 1);
1093 *q++ = '\n';
bellarde15d7372006-06-25 16:26:29 +00001094 } else {
Paolo Bonzini41048332010-12-23 13:42:52 +01001095 *q++ = keysym;
1096 }
1097 if (s->echo) {
1098 console_puts(s->chr, buf, q - buf);
bellarde15d7372006-06-25 16:26:29 +00001099 }
pbrooke5b0bc42007-01-27 23:46:43 +00001100 if (s->chr->chr_read) {
bellarde15d7372006-06-25 16:26:29 +00001101 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1102 kbd_send_chars(s);
bellarde7f0ad52004-07-14 17:28:59 +00001103 }
1104 break;
1105 }
1106}
1107
Gerd Hoffmann50ef4672014-05-27 09:28:38 +02001108static const int qcode_to_keysym[Q_KEY_CODE_MAX] = {
1109 [Q_KEY_CODE_UP] = QEMU_KEY_UP,
1110 [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
1111 [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
1112 [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
1113 [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
1114 [Q_KEY_CODE_END] = QEMU_KEY_END,
1115 [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
1116 [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
1117 [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
1118};
1119
1120bool kbd_put_qcode_console(QemuConsole *s, int qcode)
1121{
1122 int keysym;
1123
1124 keysym = qcode_to_keysym[qcode];
1125 if (keysym == 0) {
1126 return false;
1127 }
1128 kbd_put_keysym_console(s, keysym);
1129 return true;
1130}
1131
Gerd Hoffmannbdef9722014-05-27 09:32:36 +02001132void kbd_put_string_console(QemuConsole *s, const char *str, int len)
1133{
1134 int i;
1135
1136 for (i = 0; i < len && str[i]; i++) {
1137 kbd_put_keysym_console(s, str[i]);
1138 }
1139}
1140
Gerd Hoffmann3f9a6e82014-05-22 12:05:52 +02001141void kbd_put_keysym(int keysym)
1142{
1143 kbd_put_keysym_console(active_console, keysym);
1144}
1145
balrog4d3b6f62008-02-10 16:33:14 +00001146static void text_console_invalidate(void *opaque)
1147{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001148 QemuConsole *s = (QemuConsole *) opaque;
Gerd Hoffmann1562e532013-03-06 13:40:47 +01001149
1150 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
aliguori68f00992009-01-21 18:59:12 +00001151 text_console_resize(s);
1152 }
balrog4d3b6f62008-02-10 16:33:14 +00001153 console_refresh(s);
1154}
1155
Anthony Liguoric227f092009-10-01 16:12:16 -05001156static void text_console_update(void *opaque, console_ch_t *chardata)
balrog4d3b6f62008-02-10 16:33:14 +00001157{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001158 QemuConsole *s = (QemuConsole *) opaque;
balrog4d3b6f62008-02-10 16:33:14 +00001159 int i, j, src;
1160
1161 if (s->text_x[0] <= s->text_x[1]) {
1162 src = (s->y_base + s->text_y[0]) * s->width;
1163 chardata += s->text_y[0] * s->width;
1164 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1165 for (j = 0; j < s->width; j ++, src ++)
1166 console_write_ch(chardata ++, s->cells[src].ch |
1167 (s->cells[src].t_attrib.fgcol << 12) |
1168 (s->cells[src].t_attrib.bgcol << 8) |
1169 (s->cells[src].t_attrib.bold << 21));
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001170 dpy_text_update(s, s->text_x[0], s->text_y[0],
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001171 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
balrog4d3b6f62008-02-10 16:33:14 +00001172 s->text_x[0] = s->width;
1173 s->text_y[0] = s->height;
1174 s->text_x[1] = 0;
1175 s->text_y[1] = 0;
1176 }
1177 if (s->cursor_invalidate) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001178 dpy_text_cursor(s, s->x, s->y);
balrog4d3b6f62008-02-10 16:33:14 +00001179 s->cursor_invalidate = 0;
1180 }
1181}
1182
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001183static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
1184 uint32_t head)
bellarde7f0ad52004-07-14 17:28:59 +00001185{
Gerd Hoffmann95be0662013-04-17 09:45:10 +02001186 Object *obj;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001187 QemuConsole *s;
pbrook95219892006-04-09 01:06:34 +00001188 int i;
bellarde7f0ad52004-07-14 17:28:59 +00001189
Gerd Hoffmann95be0662013-04-17 09:45:10 +02001190 obj = object_new(TYPE_QEMU_CONSOLE);
1191 s = QEMU_CONSOLE(obj);
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001192 s->head = head;
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001193 object_property_add_link(obj, "device", TYPE_DEVICE,
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001194 (Object **)&s->device,
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001195 object_property_allow_set_link,
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001196 OBJ_PROP_LINK_UNREF_ON_RELEASE,
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001197 &error_abort);
Gerd Hoffmann56437062014-01-24 15:35:21 +01001198 object_property_add_uint32_ptr(obj, "head",
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001199 &s->head, &error_abort);
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001200
thsaf3a9032007-07-11 23:14:59 +00001201 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1202 (console_type == GRAPHIC_CONSOLE))) {
bellarde7f0ad52004-07-14 17:28:59 +00001203 active_console = s;
thsaf3a9032007-07-11 23:14:59 +00001204 }
bellarde7f0ad52004-07-14 17:28:59 +00001205 s->ds = ds;
thsaf3a9032007-07-11 23:14:59 +00001206 s->console_type = console_type;
Gerd Hoffmanna1d2db02014-05-26 10:36:35 +02001207
1208 consoles = g_realloc(consoles, sizeof(*consoles) * (nb_consoles+1));
thsaf3a9032007-07-11 23:14:59 +00001209 if (console_type != GRAPHIC_CONSOLE) {
Jan Kiszkaf81bdef2011-09-16 00:48:07 +02001210 s->index = nb_consoles;
pbrook95219892006-04-09 01:06:34 +00001211 consoles[nb_consoles++] = s;
1212 } else {
1213 /* HACK: Put graphical consoles before text consoles. */
1214 for (i = nb_consoles; i > 0; i--) {
thsaf3a9032007-07-11 23:14:59 +00001215 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
pbrook95219892006-04-09 01:06:34 +00001216 break;
1217 consoles[i] = consoles[i - 1];
Jan Kiszkaf81bdef2011-09-16 00:48:07 +02001218 consoles[i]->index = i;
pbrook95219892006-04-09 01:06:34 +00001219 }
Jan Kiszkaf81bdef2011-09-16 00:48:07 +02001220 s->index = i;
pbrook95219892006-04-09 01:06:34 +00001221 consoles[i] = s;
aliguori3023f332009-01-16 19:04:14 +00001222 nb_consoles++;
pbrook95219892006-04-09 01:06:34 +00001223 }
bellarde7f0ad52004-07-14 17:28:59 +00001224 return s;
1225}
1226
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001227static void qemu_alloc_display(DisplaySurface *surface, int width, int height,
1228 int linesize, PixelFormat pf, int newflags)
Jes Sorensenffe8b822011-03-16 13:33:30 +01001229{
Jes Sorensenffe8b822011-03-16 13:33:30 +01001230 surface->pf = pf;
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001231
1232 qemu_pixman_image_unref(surface->image);
1233 surface->image = NULL;
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001234
1235 surface->format = qemu_pixman_get_format(&pf);
1236 assert(surface->format != 0);
1237 surface->image = pixman_image_create_bits(surface->format,
1238 width, height,
1239 NULL, linesize);
1240 assert(surface->image != NULL);
1241
Jes Sorensenffe8b822011-03-16 13:33:30 +01001242 surface->flags = newflags | QEMU_ALLOCATED_FLAG;
Paolo Bonzini98b50082010-02-11 00:29:57 +01001243#ifdef HOST_WORDS_BIGENDIAN
Jes Sorensenffe8b822011-03-16 13:33:30 +01001244 surface->flags |= QEMU_BIG_ENDIAN_FLAG;
Paolo Bonzini98b50082010-02-11 00:29:57 +01001245#endif
Paolo Bonzini98b50082010-02-11 00:29:57 +01001246}
1247
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001248DisplaySurface *qemu_create_displaysurface(int width, int height)
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001249{
1250 DisplaySurface *surface = g_new0(DisplaySurface, 1);
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001251 int linesize = width * 4;
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001252
1253 trace_displaysurface_create(surface, width, height);
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001254 qemu_alloc_display(surface, width, height, linesize,
1255 qemu_default_pixelformat(32), 0);
1256 return surface;
1257}
1258
Gerd Hoffmann187cd1d2012-09-26 07:46:20 +02001259DisplaySurface *qemu_create_displaysurface_from(int width, int height, int bpp,
Gerd Hoffmannb1424e02013-02-20 09:37:12 +01001260 int linesize, uint8_t *data,
1261 bool byteswap)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001262{
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001263 DisplaySurface *surface = g_new0(DisplaySurface, 1);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001264
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001265 trace_displaysurface_create_from(surface, width, height, bpp, byteswap);
Gerd Hoffmannb1424e02013-02-20 09:37:12 +01001266 if (byteswap) {
1267 surface->pf = qemu_different_endianness_pixelformat(bpp);
1268 } else {
1269 surface->pf = qemu_default_pixelformat(bpp);
1270 }
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001271
1272 surface->format = qemu_pixman_get_format(&surface->pf);
1273 assert(surface->format != 0);
1274 surface->image = pixman_image_create_bits(surface->format,
1275 width, height,
1276 (void *)data, linesize);
1277 assert(surface->image != NULL);
1278
Paolo Bonzini98b50082010-02-11 00:29:57 +01001279#ifdef HOST_WORDS_BIGENDIAN
1280 surface->flags = QEMU_BIG_ENDIAN_FLAG;
1281#endif
Paolo Bonzini98b50082010-02-11 00:29:57 +01001282
1283 return surface;
1284}
1285
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001286static DisplaySurface *qemu_create_message_surface(int w, int h,
1287 const char *msg)
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001288{
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001289 DisplaySurface *surface = qemu_create_displaysurface(w, h);
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001290 pixman_color_t bg = color_table_rgb[0][COLOR_BLACK];
1291 pixman_color_t fg = color_table_rgb[0][COLOR_WHITE];
1292 pixman_image_t *glyph;
1293 int len, x, y, i;
1294
1295 len = strlen(msg);
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001296 x = (w / FONT_WIDTH - len) / 2;
1297 y = (h / FONT_HEIGHT - 1) / 2;
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001298 for (i = 0; i < len; i++) {
1299 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1300 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1301 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1302 qemu_pixman_image_unref(glyph);
1303 }
1304 return surface;
1305}
1306
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001307void qemu_free_displaysurface(DisplaySurface *surface)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001308{
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001309 if (surface == NULL) {
Paolo Bonzini98b50082010-02-11 00:29:57 +01001310 return;
Gerd Hoffmann187cd1d2012-09-26 07:46:20 +02001311 }
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001312 trace_displaysurface_free(surface);
1313 qemu_pixman_image_unref(surface->image);
1314 g_free(surface);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001315}
1316
Gerd Hoffmann52090892013-04-23 15:44:31 +02001317void register_displaychangelistener(DisplayChangeListener *dcl)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001318{
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001319 static const char nodev[] =
1320 "This VM has no graphic display device.";
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001321 static DisplaySurface *dummy;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001322 QemuConsole *con;
1323
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001324 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
Gerd Hoffmann52090892013-04-23 15:44:31 +02001325 dcl->ds = get_alloc_displaystate();
1326 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1327 gui_setup_refresh(dcl->ds);
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001328 if (dcl->con) {
1329 dcl->con->dcls++;
1330 con = dcl->con;
1331 } else {
1332 con = active_console;
1333 }
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001334 if (dcl->ops->dpy_gfx_switch) {
1335 if (con) {
1336 dcl->ops->dpy_gfx_switch(dcl, con->surface);
1337 } else {
1338 if (!dummy) {
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001339 dummy = qemu_create_message_surface(640, 480, nodev);
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001340 }
1341 dcl->ops->dpy_gfx_switch(dcl, dummy);
1342 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001343 }
Gerd Hoffmannaea79472014-05-22 11:27:13 +02001344 text_console_update_cursor(NULL);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001345}
1346
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +01001347void update_displaychangelistener(DisplayChangeListener *dcl,
1348 uint64_t interval)
1349{
1350 DisplayState *ds = dcl->ds;
1351
1352 dcl->update_interval = interval;
1353 if (!ds->refreshing && ds->update_interval > interval) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001354 timer_mod(ds->gui_timer, ds->last_update + interval);
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +01001355 }
1356}
1357
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001358void unregister_displaychangelistener(DisplayChangeListener *dcl)
1359{
1360 DisplayState *ds = dcl->ds;
1361 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001362 if (dcl->con) {
1363 dcl->con->dcls--;
1364 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001365 QLIST_REMOVE(dcl, next);
1366 gui_setup_refresh(ds);
1367}
1368
Gerd Hoffmann6f90f3d2014-01-24 17:38:20 +01001369int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info)
1370{
1371 assert(con != NULL);
1372 con->ui_info = *info;
1373 if (con->hw_ops->ui_info) {
1374 return con->hw_ops->ui_info(con->hw, con->head, info);
1375 }
1376 return -1;
1377}
1378
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001379void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001380{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001381 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001382 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001383 int width = surface_width(con->surface);
1384 int height = surface_height(con->surface);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001385
1386 x = MAX(x, 0);
1387 y = MAX(y, 0);
1388 x = MIN(x, width);
1389 y = MIN(y, height);
1390 w = MIN(w, width - x);
1391 h = MIN(h, height - y);
1392
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001393 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001394 return;
1395 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001396 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001397 if (con != (dcl->con ? dcl->con : active_console)) {
1398 continue;
1399 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001400 if (dcl->ops->dpy_gfx_update) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001401 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001402 }
1403 }
1404}
1405
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001406void dpy_gfx_replace_surface(QemuConsole *con,
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001407 DisplaySurface *surface)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001408{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001409 DisplayState *s = con->ds;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001410 DisplaySurface *old_surface = con->surface;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001411 DisplayChangeListener *dcl;
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001412
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001413 con->surface = surface;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001414 QLIST_FOREACH(dcl, &s->listeners, next) {
1415 if (con != (dcl->con ? dcl->con : active_console)) {
1416 continue;
1417 }
1418 if (dcl->ops->dpy_gfx_switch) {
1419 dcl->ops->dpy_gfx_switch(dcl, surface);
1420 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001421 }
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001422 qemu_free_displaysurface(old_surface);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001423}
1424
Stefan Weil60751372014-05-02 22:48:30 +02001425static void dpy_refresh(DisplayState *s)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001426{
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001427 DisplayChangeListener *dcl;
1428
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001429 QLIST_FOREACH(dcl, &s->listeners, next) {
1430 if (dcl->ops->dpy_refresh) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001431 dcl->ops->dpy_refresh(dcl);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001432 }
1433 }
1434}
1435
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001436void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1437 int dst_x, int dst_y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001438{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001439 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001440 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001441
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001442 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001443 return;
1444 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001445 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001446 if (con != (dcl->con ? dcl->con : active_console)) {
1447 continue;
1448 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001449 if (dcl->ops->dpy_gfx_copy) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001450 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001451 } else { /* TODO */
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001452 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001453 }
1454 }
1455}
1456
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001457void dpy_text_cursor(QemuConsole *con, int x, int y)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001458{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001459 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001460 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001461
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001462 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001463 return;
1464 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001465 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001466 if (con != (dcl->con ? dcl->con : active_console)) {
1467 continue;
1468 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001469 if (dcl->ops->dpy_text_cursor) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001470 dcl->ops->dpy_text_cursor(dcl, x, y);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001471 }
1472 }
1473}
1474
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001475void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001476{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001477 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001478 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001479
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001480 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001481 return;
1482 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001483 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001484 if (con != (dcl->con ? dcl->con : active_console)) {
1485 continue;
1486 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001487 if (dcl->ops->dpy_text_update) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001488 dcl->ops->dpy_text_update(dcl, x, y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001489 }
1490 }
1491}
1492
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001493void dpy_text_resize(QemuConsole *con, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001494{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001495 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001496 struct DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001497
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001498 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001499 return;
1500 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001501 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001502 if (con != (dcl->con ? dcl->con : active_console)) {
1503 continue;
1504 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001505 if (dcl->ops->dpy_text_resize) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001506 dcl->ops->dpy_text_resize(dcl, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001507 }
1508 }
1509}
1510
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001511void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001512{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001513 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001514 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001515
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001516 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001517 return;
1518 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001519 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001520 if (con != (dcl->con ? dcl->con : active_console)) {
1521 continue;
1522 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001523 if (dcl->ops->dpy_mouse_set) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001524 dcl->ops->dpy_mouse_set(dcl, x, y, on);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001525 }
1526 }
1527}
1528
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001529void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
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 Hoffmann321f0482013-03-12 14:39:22 +01001533
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001534 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001535 return;
1536 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001537 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001538 if (con != (dcl->con ? dcl->con : active_console)) {
1539 continue;
1540 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001541 if (dcl->ops->dpy_cursor_define) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001542 dcl->ops->dpy_cursor_define(dcl, cursor);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001543 }
1544 }
1545}
1546
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001547bool dpy_cursor_define_supported(QemuConsole *con)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001548{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001549 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001550 DisplayChangeListener *dcl;
1551
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001552 QLIST_FOREACH(dcl, &s->listeners, next) {
1553 if (dcl->ops->dpy_cursor_define) {
1554 return true;
1555 }
1556 }
1557 return false;
1558}
1559
Paolo Bonzini98b50082010-02-11 00:29:57 +01001560/***********************************************************/
1561/* register display */
1562
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001563/* console.c internal use only */
1564static DisplayState *get_alloc_displaystate(void)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001565{
1566 if (!display_state) {
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001567 display_state = g_new0(DisplayState, 1);
Gerd Hoffmannaea79472014-05-22 11:27:13 +02001568 cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1569 text_console_update_cursor, NULL);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001570 }
1571 return display_state;
1572}
1573
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001574/*
1575 * Called by main(), after creating QemuConsoles
1576 * and before initializing ui (sdl/vnc/...).
1577 */
1578DisplayState *init_displaystate(void)
1579{
Gerd Hoffmann43f420f2013-06-25 10:49:31 +02001580 gchar *name;
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001581 int i;
1582
Gerd Hoffmann333cb182014-06-02 14:07:18 +02001583 get_alloc_displaystate();
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001584 for (i = 0; i < nb_consoles; i++) {
1585 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1586 consoles[i]->ds == NULL) {
1587 text_console_do_init(consoles[i]->chr, display_state);
1588 }
Gerd Hoffmann43f420f2013-06-25 10:49:31 +02001589
1590 /* Hook up into the qom tree here (not in new_console()), once
1591 * all QemuConsoles are created and the order / numbering
1592 * doesn't change any more */
1593 name = g_strdup_printf("console[%d]", i);
1594 object_property_add_child(container_get(object_get_root(), "/backend"),
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001595 name, OBJECT(consoles[i]), &error_abort);
Gerd Hoffmann43f420f2013-06-25 10:49:31 +02001596 g_free(name);
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001597 }
1598
1599 return display_state;
1600}
1601
Gerd Hoffmann56437062014-01-24 15:35:21 +01001602QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001603 const GraphicHwOps *hw_ops,
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001604 void *opaque)
bellarde7f0ad52004-07-14 17:28:59 +00001605{
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001606 static const char noinit[] =
1607 "Guest has not initialized the display (yet).";
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001608 int width = 640;
1609 int height = 480;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001610 QemuConsole *s;
aliguori3023f332009-01-16 19:04:14 +00001611 DisplayState *ds;
aurel32f0f2f972009-01-16 21:13:49 +00001612
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001613 ds = get_alloc_displaystate();
Gerd Hoffmann437fe102013-03-07 16:04:52 +01001614 trace_console_gfx_new();
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001615 s = new_console(ds, GRAPHIC_CONSOLE, head);
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001616 s->hw_ops = hw_ops;
pbrook95219892006-04-09 01:06:34 +00001617 s->hw = opaque;
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001618 if (dev) {
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001619 object_property_set_link(OBJECT(s), OBJECT(dev), "device",
1620 &error_abort);
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001621 }
aliguori3023f332009-01-16 19:04:14 +00001622
Gerd Hoffmann521a5802013-04-25 12:10:45 +02001623 s->surface = qemu_create_message_surface(width, height, noinit);
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001624 return s;
pbrook95219892006-04-09 01:06:34 +00001625}
1626
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001627QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1628{
Gerd Hoffmanna1d2db02014-05-26 10:36:35 +02001629 if (index >= nb_consoles) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001630 return NULL;
1631 }
1632 return consoles[index];
1633}
1634
Gerd Hoffmann56437062014-01-24 15:35:21 +01001635QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
Gerd Hoffmann14a93642013-04-18 07:30:40 +02001636{
Gerd Hoffmann14a93642013-04-18 07:30:40 +02001637 Object *obj;
Gerd Hoffmann56437062014-01-24 15:35:21 +01001638 uint32_t h;
Gerd Hoffmann14a93642013-04-18 07:30:40 +02001639 int i;
1640
1641 for (i = 0; i < nb_consoles; i++) {
1642 if (!consoles[i]) {
1643 continue;
1644 }
1645 obj = object_property_get_link(OBJECT(consoles[i]),
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001646 "device", &error_abort);
Gerd Hoffmann56437062014-01-24 15:35:21 +01001647 if (DEVICE(obj) != dev) {
1648 continue;
Gerd Hoffmann14a93642013-04-18 07:30:40 +02001649 }
Gerd Hoffmann56437062014-01-24 15:35:21 +01001650 h = object_property_get_int(OBJECT(consoles[i]),
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001651 "head", &error_abort);
Gerd Hoffmann56437062014-01-24 15:35:21 +01001652 if (h != head) {
1653 continue;
1654 }
1655 return consoles[i];
Gerd Hoffmann14a93642013-04-18 07:30:40 +02001656 }
1657 return NULL;
1658}
1659
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001660bool qemu_console_is_visible(QemuConsole *con)
pbrook95219892006-04-09 01:06:34 +00001661{
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001662 return (con == active_console) || (con->dcls > 0);
bellarde7f0ad52004-07-14 17:28:59 +00001663}
1664
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001665bool qemu_console_is_graphic(QemuConsole *con)
balrogc21bbcf2008-09-24 03:32:33 +00001666{
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001667 if (con == NULL) {
1668 con = active_console;
1669 }
1670 return con && (con->console_type == GRAPHIC_CONSOLE);
1671}
1672
1673bool qemu_console_is_fixedsize(QemuConsole *con)
1674{
1675 if (con == NULL) {
1676 con = active_console;
1677 }
1678 return con && (con->console_type != TEXT_CONSOLE);
balrogc21bbcf2008-09-24 03:32:33 +00001679}
1680
Gerd Hoffmannd4c85332013-11-28 09:58:18 +01001681int qemu_console_get_index(QemuConsole *con)
1682{
1683 if (con == NULL) {
1684 con = active_console;
1685 }
1686 return con ? con->index : -1;
1687}
1688
Gerd Hoffmann56437062014-01-24 15:35:21 +01001689uint32_t qemu_console_get_head(QemuConsole *con)
1690{
1691 if (con == NULL) {
1692 con = active_console;
1693 }
1694 return con ? con->head : -1;
1695}
1696
Gerd Hoffmann6f90f3d2014-01-24 17:38:20 +01001697QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con)
1698{
1699 assert(con != NULL);
1700 return &con->ui_info;
1701}
1702
Gerd Hoffmannd4c85332013-11-28 09:58:18 +01001703int qemu_console_get_width(QemuConsole *con, int fallback)
1704{
1705 if (con == NULL) {
1706 con = active_console;
1707 }
1708 return con ? surface_width(con->surface) : fallback;
1709}
1710
1711int qemu_console_get_height(QemuConsole *con, int fallback)
1712{
1713 if (con == NULL) {
1714 con = active_console;
1715 }
1716 return con ? surface_height(con->surface) : fallback;
1717}
1718
Paolo Bonzini41048332010-12-23 13:42:52 +01001719static void text_console_set_echo(CharDriverState *chr, bool echo)
1720{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001721 QemuConsole *s = chr->opaque;
Paolo Bonzini41048332010-12-23 13:42:52 +01001722
1723 s->echo = echo;
1724}
1725
Gerd Hoffmannaea79472014-05-22 11:27:13 +02001726static void text_console_update_cursor_timer(void)
1727{
1728 timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
1729 + CONSOLE_CURSOR_PERIOD / 2);
1730}
1731
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001732static void text_console_update_cursor(void *opaque)
1733{
Gerd Hoffmannaea79472014-05-22 11:27:13 +02001734 QemuConsole *s;
1735 int i, count = 0;
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001736
Gerd Hoffmannaea79472014-05-22 11:27:13 +02001737 cursor_visible_phase = !cursor_visible_phase;
1738
1739 for (i = 0; i < nb_consoles; i++) {
1740 s = consoles[i];
1741 if (qemu_console_is_graphic(s) ||
1742 !qemu_console_is_visible(s)) {
1743 continue;
1744 }
1745 count++;
1746 graphic_hw_invalidate(s);
1747 }
1748
1749 if (count) {
1750 text_console_update_cursor_timer();
1751 }
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001752}
1753
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001754static const GraphicHwOps text_console_ops = {
1755 .invalidate = text_console_invalidate,
1756 .text_update = text_console_update,
1757};
1758
Paolo Bonzini44b37b92010-12-23 13:42:53 +01001759static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
bellarde7f0ad52004-07-14 17:28:59 +00001760{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001761 QemuConsole *s;
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001762 int g_width = 80 * FONT_WIDTH;
1763 int g_height = 24 * FONT_HEIGHT;
pbrook6d6f7c22006-03-11 15:35:30 +00001764
Paolo Bonzini491e1142010-12-23 13:42:51 +01001765 s = chr->opaque;
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02001766
bellarde7f0ad52004-07-14 17:28:59 +00001767 chr->chr_write = console_puts;
bellard6fcfafb2004-08-01 21:48:30 +00001768
bellarde15d7372006-06-25 16:26:29 +00001769 s->out_fifo.buf = s->out_fifo_buf;
1770 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
Alex Blighbc72ad62013-08-21 16:03:08 +01001771 s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
aliguori3023f332009-01-16 19:04:14 +00001772 s->ds = ds;
ths3b46e622007-09-17 08:09:54 +00001773
bellarde7f0ad52004-07-14 17:28:59 +00001774 s->y_displayed = 0;
1775 s->y_base = 0;
1776 s->total_height = DEFAULT_BACKSCROLL;
1777 s->x = 0;
1778 s->y = 0;
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001779 if (!s->surface) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001780 if (active_console && active_console->surface) {
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001781 g_width = surface_width(active_console->surface);
1782 g_height = surface_height(active_console->surface);
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001783 }
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001784 s->surface = qemu_create_displaysurface(g_width, g_height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001785 }
pbrook6d6f7c22006-03-11 15:35:30 +00001786
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001787 s->hw_ops = &text_console_ops;
balrog4d3b6f62008-02-10 16:33:14 +00001788 s->hw = s;
1789
pbrook6d6f7c22006-03-11 15:35:30 +00001790 /* Set text attribute defaults */
1791 s->t_attrib_default.bold = 0;
1792 s->t_attrib_default.uline = 0;
1793 s->t_attrib_default.blink = 0;
1794 s->t_attrib_default.invers = 0;
1795 s->t_attrib_default.unvisible = 0;
1796 s->t_attrib_default.fgcol = COLOR_WHITE;
1797 s->t_attrib_default.bgcol = COLOR_BLACK;
pbrook6d6f7c22006-03-11 15:35:30 +00001798 /* set current text attributes to default */
1799 s->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +00001800 text_console_resize(s);
1801
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01001802 if (chr->label) {
1803 char msg[128];
1804 int len;
1805
Gerd Hoffmann735ba582009-12-08 13:11:40 +01001806 s->t_attrib.bgcol = COLOR_BLUE;
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01001807 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
1808 console_puts(chr, (uint8_t*)msg, len);
Gerd Hoffmann735ba582009-12-08 13:11:40 +01001809 s->t_attrib = s->t_attrib_default;
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01001810 }
1811
Hans de Goedefee204f2013-03-26 11:07:54 +01001812 qemu_chr_be_generic_open(chr);
aurel32ceecf1d2009-01-18 14:08:04 +00001813 if (chr->init)
1814 chr->init(chr);
bellarde7f0ad52004-07-14 17:28:59 +00001815}
pbrookc60e08d2008-07-01 16:24:38 +00001816
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001817static CharDriverState *text_console_init(ChardevVC *vc)
aliguori2796dae2009-01-16 20:23:27 +00001818{
1819 CharDriverState *chr;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001820 QemuConsole *s;
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001821 unsigned width = 0;
1822 unsigned height = 0;
aliguori2796dae2009-01-16 20:23:27 +00001823
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +02001824 chr = qemu_chr_alloc();
aliguori2796dae2009-01-16 20:23:27 +00001825
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001826 if (vc->has_width) {
1827 width = vc->width;
1828 } else if (vc->has_cols) {
1829 width = vc->cols * FONT_WIDTH;
1830 }
Paolo Bonzini491e1142010-12-23 13:42:51 +01001831
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001832 if (vc->has_height) {
1833 height = vc->height;
1834 } else if (vc->has_rows) {
1835 height = vc->rows * FONT_HEIGHT;
1836 }
Paolo Bonzini491e1142010-12-23 13:42:51 +01001837
Gerd Hoffmann437fe102013-03-07 16:04:52 +01001838 trace_console_txt_new(width, height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001839 if (width == 0 || height == 0) {
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001840 s = new_console(NULL, TEXT_CONSOLE, 0);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001841 } else {
Kirill Batuzovafff2b12014-04-24 18:15:58 +04001842 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001843 s->surface = qemu_create_displaysurface(width, height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001844 }
1845
1846 if (!s) {
Stefan Weil5354d082011-10-02 18:53:09 +02001847 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001848 return NULL;
Paolo Bonzini491e1142010-12-23 13:42:51 +01001849 }
1850
1851 s->chr = chr;
Paolo Bonzini491e1142010-12-23 13:42:51 +01001852 chr->opaque = s;
Paolo Bonzini41048332010-12-23 13:42:52 +01001853 chr->chr_set_echo = text_console_set_echo;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001854 /* console/chardev init sometimes completes elsewhere in a 2nd
1855 * stage, so defer OPENED events until they are fully initialized
1856 */
1857 chr->explicit_be_open = true;
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001858
1859 if (display_state) {
1860 text_console_do_init(chr, display_state);
1861 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001862 return chr;
aliguori2796dae2009-01-16 20:23:27 +00001863}
1864
Anthony Liguorid82831d2013-02-20 07:43:19 -06001865static VcHandler *vc_handler = text_console_init;
1866
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001867CharDriverState *vc_init(ChardevVC *vc)
Anthony Liguorid82831d2013-02-20 07:43:19 -06001868{
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001869 return vc_handler(vc);
Anthony Liguorid82831d2013-02-20 07:43:19 -06001870}
1871
1872void register_vc_handler(VcHandler *handler)
1873{
1874 vc_handler = handler;
1875}
1876
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001877void qemu_console_resize(QemuConsole *s, int width, int height)
pbrookc60e08d2008-07-01 16:24:38 +00001878{
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001879 DisplaySurface *surface;
1880
1881 assert(s->console_type == GRAPHIC_CONSOLE);
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001882 surface = qemu_create_displaysurface(width, height);
1883 dpy_gfx_replace_surface(s, surface);
pbrookc60e08d2008-07-01 16:24:38 +00001884}
balrog38334f72008-09-24 02:21:24 +00001885
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001886void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
aliguori3023f332009-01-16 19:04:14 +00001887 int dst_x, int dst_y, int w, int h)
balrogc21bbcf2008-09-24 03:32:33 +00001888{
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001889 assert(con->console_type == GRAPHIC_CONSOLE);
1890 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
balrog38334f72008-09-24 02:21:24 +00001891}
aliguori7d957bd2009-01-15 22:14:11 +00001892
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001893DisplaySurface *qemu_console_surface(QemuConsole *console)
1894{
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001895 return console->surface;
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001896}
1897
1898DisplayState *qemu_console_displaystate(QemuConsole *console)
1899{
1900 return console->ds;
1901}
1902
malc0da2ea12009-01-23 19:56:19 +00001903PixelFormat qemu_different_endianness_pixelformat(int bpp)
aliguori7d957bd2009-01-15 22:14:11 +00001904{
1905 PixelFormat pf;
1906
1907 memset(&pf, 0x00, sizeof(PixelFormat));
1908
1909 pf.bits_per_pixel = bpp;
BALATON Zoltanfeadf1a2012-08-22 17:19:42 +02001910 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
aliguori7d957bd2009-01-15 22:14:11 +00001911 pf.depth = bpp == 32 ? 24 : bpp;
1912
1913 switch (bpp) {
malc0da2ea12009-01-23 19:56:19 +00001914 case 24:
1915 pf.rmask = 0x000000FF;
1916 pf.gmask = 0x0000FF00;
1917 pf.bmask = 0x00FF0000;
1918 pf.rmax = 255;
1919 pf.gmax = 255;
1920 pf.bmax = 255;
1921 pf.rshift = 0;
1922 pf.gshift = 8;
1923 pf.bshift = 16;
aliguori90a1e3c2009-01-26 15:37:30 +00001924 pf.rbits = 8;
1925 pf.gbits = 8;
1926 pf.bbits = 8;
aliguori7d957bd2009-01-15 22:14:11 +00001927 break;
malc0da2ea12009-01-23 19:56:19 +00001928 case 32:
1929 pf.rmask = 0x0000FF00;
1930 pf.gmask = 0x00FF0000;
1931 pf.bmask = 0xFF000000;
1932 pf.amask = 0x00000000;
1933 pf.amax = 255;
1934 pf.rmax = 255;
1935 pf.gmax = 255;
1936 pf.bmax = 255;
1937 pf.ashift = 0;
1938 pf.rshift = 8;
1939 pf.gshift = 16;
1940 pf.bshift = 24;
aliguori90a1e3c2009-01-26 15:37:30 +00001941 pf.rbits = 8;
1942 pf.gbits = 8;
1943 pf.bbits = 8;
1944 pf.abits = 8;
malc0da2ea12009-01-23 19:56:19 +00001945 break;
1946 default:
1947 break;
1948 }
1949 return pf;
1950}
1951
1952PixelFormat qemu_default_pixelformat(int bpp)
1953{
1954 PixelFormat pf;
1955
1956 memset(&pf, 0x00, sizeof(PixelFormat));
1957
1958 pf.bits_per_pixel = bpp;
BALATON Zoltanfeadf1a2012-08-22 17:19:42 +02001959 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
malc0da2ea12009-01-23 19:56:19 +00001960 pf.depth = bpp == 32 ? 24 : bpp;
1961
1962 switch (bpp) {
Gerd Hoffmannb6278082010-05-21 11:59:14 +02001963 case 15:
1964 pf.bits_per_pixel = 16;
Gerd Hoffmannb6278082010-05-21 11:59:14 +02001965 pf.rmask = 0x00007c00;
1966 pf.gmask = 0x000003E0;
1967 pf.bmask = 0x0000001F;
1968 pf.rmax = 31;
1969 pf.gmax = 31;
1970 pf.bmax = 31;
1971 pf.rshift = 10;
1972 pf.gshift = 5;
1973 pf.bshift = 0;
1974 pf.rbits = 5;
1975 pf.gbits = 5;
1976 pf.bbits = 5;
1977 break;
aliguori7d957bd2009-01-15 22:14:11 +00001978 case 16:
1979 pf.rmask = 0x0000F800;
1980 pf.gmask = 0x000007E0;
1981 pf.bmask = 0x0000001F;
1982 pf.rmax = 31;
1983 pf.gmax = 63;
1984 pf.bmax = 31;
1985 pf.rshift = 11;
1986 pf.gshift = 5;
1987 pf.bshift = 0;
aliguori90a1e3c2009-01-26 15:37:30 +00001988 pf.rbits = 5;
1989 pf.gbits = 6;
1990 pf.bbits = 5;
aliguori7d957bd2009-01-15 22:14:11 +00001991 break;
1992 case 24:
aliguori7d957bd2009-01-15 22:14:11 +00001993 pf.rmask = 0x00FF0000;
1994 pf.gmask = 0x0000FF00;
1995 pf.bmask = 0x000000FF;
1996 pf.rmax = 255;
1997 pf.gmax = 255;
1998 pf.bmax = 255;
1999 pf.rshift = 16;
2000 pf.gshift = 8;
2001 pf.bshift = 0;
aliguori90a1e3c2009-01-26 15:37:30 +00002002 pf.rbits = 8;
2003 pf.gbits = 8;
2004 pf.bbits = 8;
Markus Armbruster0eba62e2011-11-22 12:56:10 +01002005 break;
malc0da2ea12009-01-23 19:56:19 +00002006 case 32:
2007 pf.rmask = 0x00FF0000;
2008 pf.gmask = 0x0000FF00;
2009 pf.bmask = 0x000000FF;
malc0da2ea12009-01-23 19:56:19 +00002010 pf.rmax = 255;
2011 pf.gmax = 255;
2012 pf.bmax = 255;
malc0da2ea12009-01-23 19:56:19 +00002013 pf.rshift = 16;
2014 pf.gshift = 8;
2015 pf.bshift = 0;
aliguori90a1e3c2009-01-26 15:37:30 +00002016 pf.rbits = 8;
2017 pf.gbits = 8;
2018 pf.bbits = 8;
aliguori7d957bd2009-01-15 22:14:11 +00002019 break;
2020 default:
2021 break;
2022 }
2023 return pf;
2024}
Anthony Liguori01f45d92013-03-05 23:21:32 +05302025
Gerd Hoffmann702ec692013-02-25 15:52:32 +01002026static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
2027 Error **errp)
2028{
2029 int val;
2030
2031 backend->vc = g_new0(ChardevVC, 1);
2032
2033 val = qemu_opt_get_number(opts, "width", 0);
2034 if (val != 0) {
2035 backend->vc->has_width = true;
2036 backend->vc->width = val;
2037 }
2038
2039 val = qemu_opt_get_number(opts, "height", 0);
2040 if (val != 0) {
2041 backend->vc->has_height = true;
2042 backend->vc->height = val;
2043 }
2044
2045 val = qemu_opt_get_number(opts, "cols", 0);
2046 if (val != 0) {
2047 backend->vc->has_cols = true;
2048 backend->vc->cols = val;
2049 }
2050
2051 val = qemu_opt_get_number(opts, "rows", 0);
2052 if (val != 0) {
2053 backend->vc->has_rows = true;
2054 backend->vc->rows = val;
2055 }
2056}
2057
Gerd Hoffmann95be0662013-04-17 09:45:10 +02002058static const TypeInfo qemu_console_info = {
2059 .name = TYPE_QEMU_CONSOLE,
2060 .parent = TYPE_OBJECT,
2061 .instance_size = sizeof(QemuConsole),
2062 .class_size = sizeof(QemuConsoleClass),
2063};
2064
2065
Anthony Liguori01f45d92013-03-05 23:21:32 +05302066static void register_types(void)
2067{
Gerd Hoffmann95be0662013-04-17 09:45:10 +02002068 type_register_static(&qemu_console_info);
Gerd Hoffmann702ec692013-02-25 15:52:32 +01002069 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC,
2070 qemu_chr_parse_vc);
Anthony Liguori01f45d92013-03-05 23:21:32 +05302071}
2072
2073type_init(register_types);