blob: aad4fc9a5775dfdeb9b9dc7222c7a93011e18817 [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"
bellarde7f0ad52004-07-14 17:28:59 +000030
pbrook6d6f7c22006-03-11 15:35:30 +000031//#define DEBUG_CONSOLE
bellarde7f0ad52004-07-14 17:28:59 +000032#define DEFAULT_BACKSCROLL 512
33#define MAX_CONSOLES 12
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 Hoffmann380cd052013-03-13 14:04:18 +0100127 const GraphicHwOps *hw_ops;
pbrook95219892006-04-09 01:06:34 +0000128 void *hw;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200129
130 /* Text console state */
bellarde7f0ad52004-07-14 17:28:59 +0000131 int width;
132 int height;
133 int total_height;
134 int backscroll_height;
bellarde7f0ad52004-07-14 17:28:59 +0000135 int x, y;
thsadb47962007-01-16 23:02:36 +0000136 int x_saved, y_saved;
bellarde7f0ad52004-07-14 17:28:59 +0000137 int y_displayed;
138 int y_base;
pbrook6d6f7c22006-03-11 15:35:30 +0000139 TextAttributes t_attrib_default; /* default text attributes */
140 TextAttributes t_attrib; /* currently active text attributes */
bellarde7f0ad52004-07-14 17:28:59 +0000141 TextCell *cells;
balrog4d3b6f62008-02-10 16:33:14 +0000142 int text_x[2], text_y[2], cursor_invalidate;
Paolo Bonzini41048332010-12-23 13:42:52 +0100143 int echo;
Jan Kiszkabf1bed82012-07-10 22:00:55 +0200144 bool cursor_visible_phase;
145 QEMUTimer *cursor_timer;
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 {
164 struct 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;
176static QemuConsole *consoles[MAX_CONSOLES];
bellarde7f0ad52004-07-14 17:28:59 +0000177static int nb_consoles = 0;
178
Gerd Hoffmann64840c62013-03-07 17:08:29 +0100179static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100180static void dpy_refresh(DisplayState *s);
Gerd Hoffmann52090892013-04-23 15:44:31 +0200181static DisplayState *get_alloc_displaystate(void);
Gerd Hoffmann64840c62013-03-07 17:08:29 +0100182
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100183static void gui_update(void *opaque)
184{
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100185 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
186 uint64_t dcl_interval;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100187 DisplayState *ds = opaque;
188 DisplayChangeListener *dcl;
Gerd Hoffmanndea1b0b2013-03-19 15:01:02 +0100189 int i;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100190
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100191 ds->refreshing = true;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100192 dpy_refresh(ds);
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100193 ds->refreshing = false;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100194
195 QLIST_FOREACH(dcl, &ds->listeners, next) {
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100196 dcl_interval = dcl->update_interval ?
197 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
198 if (interval > dcl_interval) {
199 interval = dcl_interval;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100200 }
201 }
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100202 if (ds->update_interval != interval) {
203 ds->update_interval = interval;
Gerd Hoffmanndea1b0b2013-03-19 15:01:02 +0100204 for (i = 0; i < nb_consoles; i++) {
205 if (consoles[i]->hw_ops->update_interval) {
206 consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval);
207 }
208 }
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100209 trace_console_refresh(interval);
210 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100211 ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
212 timer_mod(ds->gui_timer, ds->last_update + interval);
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100213}
214
215static void gui_setup_refresh(DisplayState *ds)
216{
217 DisplayChangeListener *dcl;
218 bool need_timer = false;
219 bool have_gfx = false;
220 bool have_text = false;
221
222 QLIST_FOREACH(dcl, &ds->listeners, next) {
223 if (dcl->ops->dpy_refresh != NULL) {
224 need_timer = true;
225 }
226 if (dcl->ops->dpy_gfx_update != NULL) {
227 have_gfx = true;
228 }
229 if (dcl->ops->dpy_text_update != NULL) {
230 have_text = true;
231 }
232 }
233
234 if (need_timer && ds->gui_timer == NULL) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100235 ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
236 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100237 }
238 if (!need_timer && ds->gui_timer != NULL) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100239 timer_del(ds->gui_timer);
240 timer_free(ds->gui_timer);
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100241 ds->gui_timer = NULL;
242 }
243
244 ds->have_gfx = have_gfx;
245 ds->have_text = have_text;
246}
247
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100248void graphic_hw_update(QemuConsole *con)
pbrook95219892006-04-09 01:06:34 +0000249{
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100250 if (!con) {
251 con = active_console;
252 }
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100253 if (con && con->hw_ops->gfx_update) {
254 con->hw_ops->gfx_update(con->hw);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100255 }
pbrook95219892006-04-09 01:06:34 +0000256}
257
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100258void graphic_hw_invalidate(QemuConsole *con)
pbrook95219892006-04-09 01:06:34 +0000259{
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100260 if (!con) {
261 con = active_console;
262 }
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100263 if (con && con->hw_ops->invalidate) {
264 con->hw_ops->invalidate(con->hw);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100265 }
pbrook95219892006-04-09 01:06:34 +0000266}
267
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100268static void ppm_save(const char *filename, struct DisplaySurface *ds,
269 Error **errp)
270{
271 int width = pixman_image_get_width(ds->image);
272 int height = pixman_image_get_height(ds->image);
Gerd Hoffmanncdd5b932013-04-23 13:26:59 +0200273 int fd;
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100274 FILE *f;
275 int y;
276 int ret;
277 pixman_image_t *linebuf;
278
279 trace_ppm_save(filename, ds);
Gerd Hoffmanncdd5b932013-04-23 13:26:59 +0200280 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
281 if (fd == -1) {
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100282 error_setg(errp, "failed to open file '%s': %s", filename,
283 strerror(errno));
284 return;
285 }
Gerd Hoffmanncdd5b932013-04-23 13:26:59 +0200286 f = fdopen(fd, "wb");
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100287 ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
288 if (ret < 0) {
289 linebuf = NULL;
290 goto write_err;
291 }
292 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
293 for (y = 0; y < height; y++) {
294 qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
295 clearerr(f);
296 ret = fwrite(pixman_image_get_data(linebuf), 1,
297 pixman_image_get_stride(linebuf), f);
298 (void)ret;
299 if (ferror(f)) {
300 goto write_err;
301 }
302 }
303
304out:
305 qemu_pixman_image_unref(linebuf);
306 fclose(f);
307 return;
308
309write_err:
310 error_setg(errp, "failed to write to file '%s': %s", filename,
311 strerror(errno));
312 unlink(filename);
313 goto out;
314}
315
Luiz Capitulinoad39cf62012-05-24 13:48:23 -0300316void qmp_screendump(const char *filename, Error **errp)
pbrook95219892006-04-09 01:06:34 +0000317{
Gerd Hoffmann284d1c62013-03-15 15:45:54 +0100318 QemuConsole *con = qemu_console_lookup_by_index(0);
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100319 DisplaySurface *surface;
balrog8571c052008-07-19 13:04:26 +0000320
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100321 if (con == NULL) {
322 error_setg(errp, "There is no QemuConsole I can screendump from.");
323 return;
Jan Kiszkaf81bdef2011-09-16 00:48:07 +0200324 }
325
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100326 graphic_hw_update(con);
327 surface = qemu_console_surface(con);
328 ppm_save(filename, surface, errp);
pbrook95219892006-04-09 01:06:34 +0000329}
330
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100331void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
balrog4d3b6f62008-02-10 16:33:14 +0000332{
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100333 if (!con) {
334 con = active_console;
335 }
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100336 if (con && con->hw_ops->text_update) {
337 con->hw_ops->text_update(con->hw, chardata);
338 }
balrog4d3b6f62008-02-10 16:33:14 +0000339}
340
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100341static void vga_fill_rect(QemuConsole *con,
342 int posx, int posy, int width, int height,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100343 pixman_color_t color)
bellarde7f0ad52004-07-14 17:28:59 +0000344{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100345 DisplaySurface *surface = qemu_console_surface(con);
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100346 pixman_rectangle16_t rect = {
347 .x = posx, .y = posy, .width = width, .height = height
348 };
ths3b46e622007-09-17 08:09:54 +0000349
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100350 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100351 &color, 1, &rect);
bellarde7f0ad52004-07-14 17:28:59 +0000352}
353
354/* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100355static void vga_bitblt(QemuConsole *con,
356 int xs, int ys, int xd, int yd, int w, int h)
bellarde7f0ad52004-07-14 17:28:59 +0000357{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100358 DisplaySurface *surface = qemu_console_surface(con);
bellarde7f0ad52004-07-14 17:28:59 +0000359
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100360 pixman_image_composite(PIXMAN_OP_SRC,
361 surface->image, NULL, surface->image,
362 xs, ys, 0, 0, xd, yd, w, h);
bellarde7f0ad52004-07-14 17:28:59 +0000363}
364
365/***********************************************************/
366/* basic char display */
367
368#define FONT_HEIGHT 16
369#define FONT_WIDTH 8
370
371#include "vgafont.h"
372
Devin J. Pohlydf00bed2011-09-07 15:44:36 -0400373#ifndef CONFIG_CURSES
pbrook6d6f7c22006-03-11 15:35:30 +0000374enum color_names {
375 COLOR_BLACK = 0,
376 COLOR_RED = 1,
377 COLOR_GREEN = 2,
378 COLOR_YELLOW = 3,
379 COLOR_BLUE = 4,
380 COLOR_MAGENTA = 5,
381 COLOR_CYAN = 6,
382 COLOR_WHITE = 7
383};
Devin J. Pohlydf00bed2011-09-07 15:44:36 -0400384#endif
pbrook6d6f7c22006-03-11 15:35:30 +0000385
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100386#define QEMU_RGB(r, g, b) \
387 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
388
389static const pixman_color_t color_table_rgb[2][8] = {
pbrook6d6f7c22006-03-11 15:35:30 +0000390 { /* dark */
bellard26489842006-06-25 17:37:36 +0000391 QEMU_RGB(0x00, 0x00, 0x00), /* black */
392 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
393 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
394 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
395 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
396 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
397 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
398 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
pbrook6d6f7c22006-03-11 15:35:30 +0000399 },
400 { /* bright */
bellard26489842006-06-25 17:37:36 +0000401 QEMU_RGB(0x00, 0x00, 0x00), /* black */
402 QEMU_RGB(0xff, 0x00, 0x00), /* red */
403 QEMU_RGB(0x00, 0xff, 0x00), /* green */
404 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
405 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
406 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
407 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
408 QEMU_RGB(0xff, 0xff, 0xff), /* white */
pbrook6d6f7c22006-03-11 15:35:30 +0000409 }
bellarde7f0ad52004-07-14 17:28:59 +0000410};
411
pbrook6d6f7c22006-03-11 15:35:30 +0000412#ifdef DEBUG_CONSOLE
413static void console_print_text_attributes(TextAttributes *t_attrib, char ch)
414{
415 if (t_attrib->bold) {
416 printf("b");
417 } else {
418 printf(" ");
419 }
420 if (t_attrib->uline) {
421 printf("u");
422 } else {
423 printf(" ");
424 }
425 if (t_attrib->blink) {
426 printf("l");
427 } else {
428 printf(" ");
429 }
430 if (t_attrib->invers) {
431 printf("i");
432 } else {
433 printf(" ");
434 }
435 if (t_attrib->unvisible) {
436 printf("n");
437 } else {
438 printf(" ");
439 }
440
441 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib->fgcol, t_attrib->bgcol, ch, ch);
442}
443#endif
bellarde7f0ad52004-07-14 17:28:59 +0000444
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100445static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
pbrook6d6f7c22006-03-11 15:35:30 +0000446 TextAttributes *t_attrib)
bellarde7f0ad52004-07-14 17:28:59 +0000447{
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100448 static pixman_image_t *glyphs[256];
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100449 DisplaySurface *surface = qemu_console_surface(s);
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100450 pixman_color_t fgcol, bgcol;
pbrook6d6f7c22006-03-11 15:35:30 +0000451
452 if (t_attrib->invers) {
Gerd Hoffmanncf6f0542013-03-06 09:50:51 +0100453 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
454 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
pbrook6d6f7c22006-03-11 15:35:30 +0000455 } else {
Gerd Hoffmanncf6f0542013-03-06 09:50:51 +0100456 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
457 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
pbrook6d6f7c22006-03-11 15:35:30 +0000458 }
bellarde7f0ad52004-07-14 17:28:59 +0000459
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100460 if (!glyphs[ch]) {
461 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
bellarde7f0ad52004-07-14 17:28:59 +0000462 }
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100463 qemu_pixman_glyph_render(glyphs[ch], surface->image,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100464 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
bellarde7f0ad52004-07-14 17:28:59 +0000465}
466
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200467static void text_console_resize(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000468{
469 TextCell *cells, *c, *c1;
470 int w1, x, y, last_width;
471
472 last_width = s->width;
Gerd Hoffmann36671fb2013-03-13 10:14:52 +0100473 s->width = surface_width(s->surface) / FONT_WIDTH;
474 s->height = surface_height(s->surface) / FONT_HEIGHT;
bellarde7f0ad52004-07-14 17:28:59 +0000475
476 w1 = last_width;
477 if (s->width < w1)
478 w1 = s->width;
479
Anthony Liguori7267c092011-08-20 22:09:37 -0500480 cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
bellarde7f0ad52004-07-14 17:28:59 +0000481 for(y = 0; y < s->total_height; y++) {
482 c = &cells[y * s->width];
483 if (w1 > 0) {
484 c1 = &s->cells[y * last_width];
485 for(x = 0; x < w1; x++) {
486 *c++ = *c1++;
487 }
488 }
489 for(x = w1; x < s->width; x++) {
490 c->ch = ' ';
pbrook6d6f7c22006-03-11 15:35:30 +0000491 c->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +0000492 c++;
493 }
494 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500495 g_free(s->cells);
bellarde7f0ad52004-07-14 17:28:59 +0000496 s->cells = cells;
497}
498
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200499static inline void text_update_xy(QemuConsole *s, int x, int y)
balrog4d3b6f62008-02-10 16:33:14 +0000500{
501 s->text_x[0] = MIN(s->text_x[0], x);
502 s->text_x[1] = MAX(s->text_x[1], x);
503 s->text_y[0] = MIN(s->text_y[0], y);
504 s->text_y[1] = MAX(s->text_y[1], y);
505}
506
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200507static void invalidate_xy(QemuConsole *s, int x, int y)
pbrook14778c22009-01-21 03:02:52 +0000508{
509 if (s->update_x0 > x * FONT_WIDTH)
510 s->update_x0 = x * FONT_WIDTH;
511 if (s->update_y0 > y * FONT_HEIGHT)
512 s->update_y0 = y * FONT_HEIGHT;
513 if (s->update_x1 < (x + 1) * FONT_WIDTH)
514 s->update_x1 = (x + 1) * FONT_WIDTH;
515 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
516 s->update_y1 = (y + 1) * FONT_HEIGHT;
517}
518
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200519static void update_xy(QemuConsole *s, int x, int y)
bellarde7f0ad52004-07-14 17:28:59 +0000520{
521 TextCell *c;
522 int y1, y2;
523
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100524 if (!qemu_console_is_visible(s)) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100525 return;
526 }
balrog4d3b6f62008-02-10 16:33:14 +0000527
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100528 if (s->ds->have_text) {
529 text_update_xy(s, x, y);
530 }
531
532 if (s->ds->have_gfx) {
bellarde7f0ad52004-07-14 17:28:59 +0000533 y1 = (s->y_base + y) % s->total_height;
534 y2 = y1 - s->y_displayed;
535 if (y2 < 0)
536 y2 += s->total_height;
537 if (y2 < s->height) {
538 c = &s->cells[y1 * s->width + x];
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100539 vga_putcharxy(s, x, y2, c->ch,
pbrook6d6f7c22006-03-11 15:35:30 +0000540 &(c->t_attrib));
pbrook14778c22009-01-21 03:02:52 +0000541 invalidate_xy(s, x, y2);
bellarde7f0ad52004-07-14 17:28:59 +0000542 }
543 }
544}
545
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200546static void console_show_cursor(QemuConsole *s, int show)
bellarde7f0ad52004-07-14 17:28:59 +0000547{
548 TextCell *c;
549 int y, y1;
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100550 int x = s->x;
bellarde7f0ad52004-07-14 17:28:59 +0000551
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100552 if (!qemu_console_is_visible(s)) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100553 return;
554 }
balrog4d3b6f62008-02-10 16:33:14 +0000555
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100556 if (s->ds->have_text) {
557 s->cursor_invalidate = 1;
558 }
balrog4d3b6f62008-02-10 16:33:14 +0000559
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100560 if (s->ds->have_gfx) {
thsed8276a2007-02-10 22:37:56 +0000561 if (x >= s->width) {
562 x = s->width - 1;
563 }
bellarde7f0ad52004-07-14 17:28:59 +0000564 y1 = (s->y_base + s->y) % s->total_height;
565 y = y1 - s->y_displayed;
566 if (y < 0)
567 y += s->total_height;
568 if (y < s->height) {
thsed8276a2007-02-10 22:37:56 +0000569 c = &s->cells[y1 * s->width + x];
Jan Kiszkabf1bed82012-07-10 22:00:55 +0200570 if (show && s->cursor_visible_phase) {
pbrook6d6f7c22006-03-11 15:35:30 +0000571 TextAttributes t_attrib = s->t_attrib_default;
572 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100573 vga_putcharxy(s, x, y, c->ch, &t_attrib);
bellarde7f0ad52004-07-14 17:28:59 +0000574 } else {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100575 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
bellarde7f0ad52004-07-14 17:28:59 +0000576 }
pbrook14778c22009-01-21 03:02:52 +0000577 invalidate_xy(s, x, y);
bellarde7f0ad52004-07-14 17:28:59 +0000578 }
579 }
580}
581
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200582static void console_refresh(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000583{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100584 DisplaySurface *surface = qemu_console_surface(s);
bellarde7f0ad52004-07-14 17:28:59 +0000585 TextCell *c;
586 int x, y, y1;
587
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100588 if (!qemu_console_is_visible(s)) {
bellarde7f0ad52004-07-14 17:28:59 +0000589 return;
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100590 }
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200591
592 if (s->ds->have_text) {
balrog4d3b6f62008-02-10 16:33:14 +0000593 s->text_x[0] = 0;
594 s->text_y[0] = 0;
595 s->text_x[1] = s->width - 1;
596 s->text_y[1] = s->height - 1;
597 s->cursor_invalidate = 1;
balrog4d3b6f62008-02-10 16:33:14 +0000598 }
bellarde7f0ad52004-07-14 17:28:59 +0000599
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200600 if (s->ds->have_gfx) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100601 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
Gerd Hoffmanncf6f0542013-03-06 09:50:51 +0100602 color_table_rgb[0][COLOR_BLACK]);
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200603 y1 = s->y_displayed;
604 for (y = 0; y < s->height; y++) {
605 c = s->cells + y1 * s->width;
606 for (x = 0; x < s->width; x++) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100607 vga_putcharxy(s, x, y, c->ch,
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200608 &(c->t_attrib));
609 c++;
610 }
611 if (++y1 == s->total_height) {
612 y1 = 0;
613 }
bellarde7f0ad52004-07-14 17:28:59 +0000614 }
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200615 console_show_cursor(s, 1);
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100616 dpy_gfx_update(s, 0, 0,
617 surface_width(surface), surface_height(surface));
bellarde7f0ad52004-07-14 17:28:59 +0000618 }
bellarde7f0ad52004-07-14 17:28:59 +0000619}
620
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100621static void console_scroll(QemuConsole *s, int ydelta)
bellarde7f0ad52004-07-14 17:28:59 +0000622{
bellarde7f0ad52004-07-14 17:28:59 +0000623 int i, y1;
ths3b46e622007-09-17 08:09:54 +0000624
bellarde7f0ad52004-07-14 17:28:59 +0000625 if (ydelta > 0) {
626 for(i = 0; i < ydelta; i++) {
627 if (s->y_displayed == s->y_base)
628 break;
629 if (++s->y_displayed == s->total_height)
630 s->y_displayed = 0;
631 }
632 } else {
633 ydelta = -ydelta;
634 i = s->backscroll_height;
635 if (i > s->total_height - s->height)
636 i = s->total_height - s->height;
637 y1 = s->y_base - i;
638 if (y1 < 0)
639 y1 += s->total_height;
640 for(i = 0; i < ydelta; i++) {
641 if (s->y_displayed == y1)
642 break;
643 if (--s->y_displayed < 0)
644 s->y_displayed = s->total_height - 1;
645 }
646 }
647 console_refresh(s);
648}
649
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200650static void console_put_lf(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000651{
652 TextCell *c;
653 int x, y1;
654
bellarde7f0ad52004-07-14 17:28:59 +0000655 s->y++;
656 if (s->y >= s->height) {
657 s->y = s->height - 1;
pbrook6d6f7c22006-03-11 15:35:30 +0000658
bellarde7f0ad52004-07-14 17:28:59 +0000659 if (s->y_displayed == s->y_base) {
660 if (++s->y_displayed == s->total_height)
661 s->y_displayed = 0;
662 }
663 if (++s->y_base == s->total_height)
664 s->y_base = 0;
665 if (s->backscroll_height < s->total_height)
666 s->backscroll_height++;
667 y1 = (s->y_base + s->height - 1) % s->total_height;
668 c = &s->cells[y1 * s->width];
669 for(x = 0; x < s->width; x++) {
670 c->ch = ' ';
pbrook6d6f7c22006-03-11 15:35:30 +0000671 c->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +0000672 c++;
673 }
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100674 if (qemu_console_is_visible(s) && s->y_displayed == s->y_base) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100675 if (s->ds->have_text) {
balrog4d3b6f62008-02-10 16:33:14 +0000676 s->text_x[0] = 0;
677 s->text_y[0] = 0;
678 s->text_x[1] = s->width - 1;
679 s->text_y[1] = s->height - 1;
balrog4d3b6f62008-02-10 16:33:14 +0000680 }
681
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100682 if (s->ds->have_gfx) {
683 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
684 s->width * FONT_WIDTH,
685 (s->height - 1) * FONT_HEIGHT);
686 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
687 s->width * FONT_WIDTH, FONT_HEIGHT,
688 color_table_rgb[0][s->t_attrib_default.bgcol]);
689 s->update_x0 = 0;
690 s->update_y0 = 0;
691 s->update_x1 = s->width * FONT_WIDTH;
692 s->update_y1 = s->height * FONT_HEIGHT;
693 }
bellarde7f0ad52004-07-14 17:28:59 +0000694 }
695 }
696}
697
pbrook6d6f7c22006-03-11 15:35:30 +0000698/* Set console attributes depending on the current escape codes.
699 * NOTE: I know this code is not very efficient (checking every color for it
700 * self) but it is more readable and better maintainable.
701 */
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200702static void console_handle_escape(QemuConsole *s)
pbrook6d6f7c22006-03-11 15:35:30 +0000703{
704 int i;
705
pbrook6d6f7c22006-03-11 15:35:30 +0000706 for (i=0; i<s->nb_esc_params; i++) {
707 switch (s->esc_params[i]) {
708 case 0: /* reset all console attributes to default */
709 s->t_attrib = s->t_attrib_default;
710 break;
711 case 1:
712 s->t_attrib.bold = 1;
713 break;
714 case 4:
715 s->t_attrib.uline = 1;
716 break;
717 case 5:
718 s->t_attrib.blink = 1;
719 break;
720 case 7:
721 s->t_attrib.invers = 1;
722 break;
723 case 8:
724 s->t_attrib.unvisible = 1;
725 break;
726 case 22:
727 s->t_attrib.bold = 0;
728 break;
729 case 24:
730 s->t_attrib.uline = 0;
731 break;
732 case 25:
733 s->t_attrib.blink = 0;
734 break;
735 case 27:
736 s->t_attrib.invers = 0;
737 break;
738 case 28:
739 s->t_attrib.unvisible = 0;
740 break;
741 /* set foreground color */
742 case 30:
743 s->t_attrib.fgcol=COLOR_BLACK;
744 break;
745 case 31:
746 s->t_attrib.fgcol=COLOR_RED;
747 break;
748 case 32:
749 s->t_attrib.fgcol=COLOR_GREEN;
750 break;
751 case 33:
752 s->t_attrib.fgcol=COLOR_YELLOW;
753 break;
754 case 34:
755 s->t_attrib.fgcol=COLOR_BLUE;
756 break;
757 case 35:
758 s->t_attrib.fgcol=COLOR_MAGENTA;
759 break;
760 case 36:
761 s->t_attrib.fgcol=COLOR_CYAN;
762 break;
763 case 37:
764 s->t_attrib.fgcol=COLOR_WHITE;
765 break;
766 /* set background color */
767 case 40:
768 s->t_attrib.bgcol=COLOR_BLACK;
769 break;
770 case 41:
771 s->t_attrib.bgcol=COLOR_RED;
772 break;
773 case 42:
774 s->t_attrib.bgcol=COLOR_GREEN;
775 break;
776 case 43:
777 s->t_attrib.bgcol=COLOR_YELLOW;
778 break;
779 case 44:
780 s->t_attrib.bgcol=COLOR_BLUE;
781 break;
782 case 45:
783 s->t_attrib.bgcol=COLOR_MAGENTA;
784 break;
785 case 46:
786 s->t_attrib.bgcol=COLOR_CYAN;
787 break;
788 case 47:
789 s->t_attrib.bgcol=COLOR_WHITE;
790 break;
791 }
792 }
793}
794
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200795static void console_clear_xy(QemuConsole *s, int x, int y)
thsadb47962007-01-16 23:02:36 +0000796{
797 int y1 = (s->y_base + y) % s->total_height;
798 TextCell *c = &s->cells[y1 * s->width + x];
799 c->ch = ' ';
800 c->t_attrib = s->t_attrib_default;
thsadb47962007-01-16 23:02:36 +0000801 update_xy(s, x, y);
802}
803
Ian Campbell3eea5492012-09-04 10:26:09 -0500804/* set cursor, checking bounds */
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200805static void set_cursor(QemuConsole *s, int x, int y)
Ian Campbell3eea5492012-09-04 10:26:09 -0500806{
807 if (x < 0) {
808 x = 0;
809 }
810 if (y < 0) {
811 y = 0;
812 }
813 if (y >= s->height) {
814 y = s->height - 1;
815 }
816 if (x >= s->width) {
817 x = s->width - 1;
818 }
819
820 s->x = x;
821 s->y = y;
822}
823
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200824static void console_putchar(QemuConsole *s, int ch)
bellarde7f0ad52004-07-14 17:28:59 +0000825{
826 TextCell *c;
thsadb47962007-01-16 23:02:36 +0000827 int y1, i;
828 int x, y;
bellarde7f0ad52004-07-14 17:28:59 +0000829
830 switch(s->state) {
831 case TTY_STATE_NORM:
832 switch(ch) {
pbrook6d6f7c22006-03-11 15:35:30 +0000833 case '\r': /* carriage return */
bellarde7f0ad52004-07-14 17:28:59 +0000834 s->x = 0;
835 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000836 case '\n': /* newline */
bellarde7f0ad52004-07-14 17:28:59 +0000837 console_put_lf(s);
838 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000839 case '\b': /* backspace */
ths5fafdf22007-09-16 21:08:06 +0000840 if (s->x > 0)
bellarde15d7372006-06-25 16:26:29 +0000841 s->x--;
pbrook6d6f7c22006-03-11 15:35:30 +0000842 break;
843 case '\t': /* tabspace */
844 if (s->x + (8 - (s->x % 8)) > s->width) {
bellardbd468842006-07-14 20:24:31 +0000845 s->x = 0;
pbrook6d6f7c22006-03-11 15:35:30 +0000846 console_put_lf(s);
847 } else {
848 s->x = s->x + (8 - (s->x % 8));
849 }
850 break;
851 case '\a': /* alert aka. bell */
852 /* TODO: has to be implemented */
853 break;
thsadb47962007-01-16 23:02:36 +0000854 case 14:
855 /* SI (shift in), character set 0 (ignored) */
856 break;
857 case 15:
858 /* SO (shift out), character set 1 (ignored) */
859 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000860 case 27: /* esc (introducing an escape sequence) */
bellarde7f0ad52004-07-14 17:28:59 +0000861 s->state = TTY_STATE_ESC;
862 break;
863 default:
thsed8276a2007-02-10 22:37:56 +0000864 if (s->x >= s->width) {
865 /* line wrap */
866 s->x = 0;
867 console_put_lf(s);
thsadb47962007-01-16 23:02:36 +0000868 }
bellarde7f0ad52004-07-14 17:28:59 +0000869 y1 = (s->y_base + s->y) % s->total_height;
870 c = &s->cells[y1 * s->width + s->x];
871 c->ch = ch;
pbrook6d6f7c22006-03-11 15:35:30 +0000872 c->t_attrib = s->t_attrib;
bellarde7f0ad52004-07-14 17:28:59 +0000873 update_xy(s, s->x, s->y);
874 s->x++;
bellarde7f0ad52004-07-14 17:28:59 +0000875 break;
876 }
877 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000878 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
bellarde7f0ad52004-07-14 17:28:59 +0000879 if (ch == '[') {
880 for(i=0;i<MAX_ESC_PARAMS;i++)
881 s->esc_params[i] = 0;
882 s->nb_esc_params = 0;
883 s->state = TTY_STATE_CSI;
884 } else {
885 s->state = TTY_STATE_NORM;
886 }
887 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000888 case TTY_STATE_CSI: /* handle escape sequence parameters */
bellarde7f0ad52004-07-14 17:28:59 +0000889 if (ch >= '0' && ch <= '9') {
890 if (s->nb_esc_params < MAX_ESC_PARAMS) {
Laszlo Ersekc10600a2012-09-17 11:10:03 +0200891 int *param = &s->esc_params[s->nb_esc_params];
892 int digit = (ch - '0');
893
894 *param = (*param <= (INT_MAX - digit) / 10) ?
895 *param * 10 + digit : INT_MAX;
bellarde7f0ad52004-07-14 17:28:59 +0000896 }
897 } else {
Ian Campbell3eea5492012-09-04 10:26:09 -0500898 if (s->nb_esc_params < MAX_ESC_PARAMS)
899 s->nb_esc_params++;
bellarde7f0ad52004-07-14 17:28:59 +0000900 if (ch == ';')
901 break;
thsadb47962007-01-16 23:02:36 +0000902#ifdef DEBUG_CONSOLE
903 fprintf(stderr, "escape sequence CSI%d;%d%c, %d parameters\n",
904 s->esc_params[0], s->esc_params[1], ch, s->nb_esc_params);
905#endif
bellarde7f0ad52004-07-14 17:28:59 +0000906 s->state = TTY_STATE_NORM;
907 switch(ch) {
thsadb47962007-01-16 23:02:36 +0000908 case 'A':
909 /* move cursor up */
910 if (s->esc_params[0] == 0) {
911 s->esc_params[0] = 1;
912 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500913 set_cursor(s, s->x, s->y - s->esc_params[0]);
bellarde7f0ad52004-07-14 17:28:59 +0000914 break;
thsadb47962007-01-16 23:02:36 +0000915 case 'B':
916 /* move cursor down */
917 if (s->esc_params[0] == 0) {
918 s->esc_params[0] = 1;
919 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500920 set_cursor(s, s->x, s->y + s->esc_params[0]);
thsadb47962007-01-16 23:02:36 +0000921 break;
922 case 'C':
923 /* move cursor right */
924 if (s->esc_params[0] == 0) {
925 s->esc_params[0] = 1;
926 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500927 set_cursor(s, s->x + s->esc_params[0], s->y);
thsadb47962007-01-16 23:02:36 +0000928 break;
929 case 'D':
930 /* move cursor left */
931 if (s->esc_params[0] == 0) {
932 s->esc_params[0] = 1;
933 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500934 set_cursor(s, s->x - s->esc_params[0], s->y);
thsadb47962007-01-16 23:02:36 +0000935 break;
936 case 'G':
937 /* move cursor to column */
Ian Campbell3eea5492012-09-04 10:26:09 -0500938 set_cursor(s, s->esc_params[0] - 1, s->y);
thsadb47962007-01-16 23:02:36 +0000939 break;
940 case 'f':
941 case 'H':
942 /* move cursor to row, column */
Ian Campbell3eea5492012-09-04 10:26:09 -0500943 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
thsadb47962007-01-16 23:02:36 +0000944 break;
945 case 'J':
946 switch (s->esc_params[0]) {
947 case 0:
948 /* clear to end of screen */
949 for (y = s->y; y < s->height; y++) {
950 for (x = 0; x < s->width; x++) {
951 if (y == s->y && x < s->x) {
952 continue;
953 }
954 console_clear_xy(s, x, y);
955 }
956 }
957 break;
958 case 1:
959 /* clear from beginning of screen */
960 for (y = 0; y <= s->y; y++) {
961 for (x = 0; x < s->width; x++) {
962 if (y == s->y && x > s->x) {
963 break;
964 }
965 console_clear_xy(s, x, y);
966 }
967 }
968 break;
969 case 2:
970 /* clear entire screen */
971 for (y = 0; y <= s->height; y++) {
972 for (x = 0; x < s->width; x++) {
973 console_clear_xy(s, x, y);
974 }
975 }
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100976 break;
thsadb47962007-01-16 23:02:36 +0000977 }
Markus Armbruster95d8f9f2011-11-22 11:59:07 +0100978 break;
thsadb47962007-01-16 23:02:36 +0000979 case 'K':
980 switch (s->esc_params[0]) {
981 case 0:
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100982 /* clear to eol */
983 for(x = s->x; x < s->width; x++) {
thsadb47962007-01-16 23:02:36 +0000984 console_clear_xy(s, x, s->y);
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100985 }
986 break;
thsadb47962007-01-16 23:02:36 +0000987 case 1:
988 /* clear from beginning of line */
989 for (x = 0; x <= s->x; x++) {
990 console_clear_xy(s, x, s->y);
991 }
992 break;
993 case 2:
994 /* clear entire line */
995 for(x = 0; x < s->width; x++) {
996 console_clear_xy(s, x, s->y);
997 }
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100998 break;
999 }
thsadb47962007-01-16 23:02:36 +00001000 break;
1001 case 'm':
Markus Armbrusterf94a9502011-11-22 11:59:06 +01001002 console_handle_escape(s);
1003 break;
thsadb47962007-01-16 23:02:36 +00001004 case 'n':
1005 /* report cursor position */
1006 /* TODO: send ESC[row;colR */
1007 break;
1008 case 's':
1009 /* save cursor position */
1010 s->x_saved = s->x;
1011 s->y_saved = s->y;
1012 break;
1013 case 'u':
1014 /* restore cursor position */
1015 s->x = s->x_saved;
1016 s->y = s->y_saved;
1017 break;
1018 default:
1019#ifdef DEBUG_CONSOLE
1020 fprintf(stderr, "unhandled escape character '%c'\n", ch);
1021#endif
1022 break;
1023 }
1024 break;
bellarde7f0ad52004-07-14 17:28:59 +00001025 }
1026 }
1027}
1028
1029void console_select(unsigned int index)
1030{
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001031 DisplayChangeListener *dcl;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001032 QemuConsole *s;
pbrook6d6f7c22006-03-11 15:35:30 +00001033
bellarde7f0ad52004-07-14 17:28:59 +00001034 if (index >= MAX_CONSOLES)
1035 return;
Gerd Hoffmann437fe102013-03-07 16:04:52 +01001036
1037 trace_console_select(index);
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001038 s = qemu_console_lookup_by_index(index);
bellarde7f0ad52004-07-14 17:28:59 +00001039 if (s) {
aliguori7d957bd2009-01-15 22:14:11 +00001040 DisplayState *ds = s->ds;
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001041
Stefan Weil8bd6b062012-08-17 15:50:44 +02001042 if (active_console && active_console->cursor_timer) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001043 timer_del(active_console->cursor_timer);
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001044 }
bellarde7f0ad52004-07-14 17:28:59 +00001045 active_console = s;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001046 if (ds->have_gfx) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001047 QLIST_FOREACH(dcl, &ds->listeners, next) {
1048 if (dcl->con != NULL) {
1049 continue;
1050 }
1051 if (dcl->ops->dpy_gfx_switch) {
1052 dcl->ops->dpy_gfx_switch(dcl, s->surface);
1053 }
1054 }
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001055 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1056 surface_height(s->surface));
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001057 }
1058 if (ds->have_text) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001059 dpy_text_resize(s, s->width, s->height);
aliguori68f00992009-01-21 18:59:12 +00001060 }
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001061 if (s->cursor_timer) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001062 timer_mod(s->cursor_timer,
1063 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + CONSOLE_CURSOR_PERIOD / 2);
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001064 }
bellarde7f0ad52004-07-14 17:28:59 +00001065 }
1066}
1067
1068static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1069{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001070 QemuConsole *s = chr->opaque;
bellarde7f0ad52004-07-14 17:28:59 +00001071 int i;
1072
pbrook14778c22009-01-21 03:02:52 +00001073 s->update_x0 = s->width * FONT_WIDTH;
1074 s->update_y0 = s->height * FONT_HEIGHT;
1075 s->update_x1 = 0;
1076 s->update_y1 = 0;
bellarde7f0ad52004-07-14 17:28:59 +00001077 console_show_cursor(s, 0);
1078 for(i = 0; i < len; i++) {
1079 console_putchar(s, buf[i]);
1080 }
1081 console_show_cursor(s, 1);
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001082 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001083 dpy_gfx_update(s, s->update_x0, s->update_y0,
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001084 s->update_x1 - s->update_x0,
1085 s->update_y1 - s->update_y0);
pbrook14778c22009-01-21 03:02:52 +00001086 }
bellarde7f0ad52004-07-14 17:28:59 +00001087 return len;
1088}
1089
bellarde15d7372006-06-25 16:26:29 +00001090static void kbd_send_chars(void *opaque)
1091{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001092 QemuConsole *s = opaque;
bellarde15d7372006-06-25 16:26:29 +00001093 int len;
1094 uint8_t buf[16];
ths3b46e622007-09-17 08:09:54 +00001095
Anthony Liguori909cda12011-08-15 11:17:31 -05001096 len = qemu_chr_be_can_write(s->chr);
bellarde15d7372006-06-25 16:26:29 +00001097 if (len > s->out_fifo.count)
1098 len = s->out_fifo.count;
1099 if (len > 0) {
1100 if (len > sizeof(buf))
1101 len = sizeof(buf);
1102 qemu_fifo_read(&s->out_fifo, buf, len);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001103 qemu_chr_be_write(s->chr, buf, len);
bellarde15d7372006-06-25 16:26:29 +00001104 }
1105 /* characters are pending: we send them a bit later (XXX:
1106 horrible, should change char device API) */
1107 if (s->out_fifo.count > 0) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001108 timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
bellarde15d7372006-06-25 16:26:29 +00001109 }
1110}
1111
bellarde7f0ad52004-07-14 17:28:59 +00001112/* called when an ascii key is pressed */
1113void kbd_put_keysym(int keysym)
1114{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001115 QemuConsole *s;
bellarde7f0ad52004-07-14 17:28:59 +00001116 uint8_t buf[16], *q;
1117 int c;
1118
1119 s = active_console;
thsaf3a9032007-07-11 23:14:59 +00001120 if (!s || (s->console_type == GRAPHIC_CONSOLE))
bellarde7f0ad52004-07-14 17:28:59 +00001121 return;
1122
1123 switch(keysym) {
1124 case QEMU_KEY_CTRL_UP:
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001125 console_scroll(s, -1);
bellarde7f0ad52004-07-14 17:28:59 +00001126 break;
1127 case QEMU_KEY_CTRL_DOWN:
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001128 console_scroll(s, 1);
bellarde7f0ad52004-07-14 17:28:59 +00001129 break;
1130 case QEMU_KEY_CTRL_PAGEUP:
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001131 console_scroll(s, -10);
bellarde7f0ad52004-07-14 17:28:59 +00001132 break;
1133 case QEMU_KEY_CTRL_PAGEDOWN:
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001134 console_scroll(s, 10);
bellarde7f0ad52004-07-14 17:28:59 +00001135 break;
1136 default:
bellarde15d7372006-06-25 16:26:29 +00001137 /* convert the QEMU keysym to VT100 key string */
1138 q = buf;
1139 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1140 *q++ = '\033';
1141 *q++ = '[';
1142 c = keysym - 0xe100;
1143 if (c >= 10)
1144 *q++ = '0' + (c / 10);
1145 *q++ = '0' + (c % 10);
1146 *q++ = '~';
1147 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1148 *q++ = '\033';
1149 *q++ = '[';
1150 *q++ = keysym & 0xff;
Paolo Bonzini41048332010-12-23 13:42:52 +01001151 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1152 console_puts(s->chr, (const uint8_t *) "\r", 1);
1153 *q++ = '\n';
bellarde15d7372006-06-25 16:26:29 +00001154 } else {
Paolo Bonzini41048332010-12-23 13:42:52 +01001155 *q++ = keysym;
1156 }
1157 if (s->echo) {
1158 console_puts(s->chr, buf, q - buf);
bellarde15d7372006-06-25 16:26:29 +00001159 }
pbrooke5b0bc42007-01-27 23:46:43 +00001160 if (s->chr->chr_read) {
bellarde15d7372006-06-25 16:26:29 +00001161 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1162 kbd_send_chars(s);
bellarde7f0ad52004-07-14 17:28:59 +00001163 }
1164 break;
1165 }
1166}
1167
balrog4d3b6f62008-02-10 16:33:14 +00001168static void text_console_invalidate(void *opaque)
1169{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001170 QemuConsole *s = (QemuConsole *) opaque;
Gerd Hoffmann1562e532013-03-06 13:40:47 +01001171
1172 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
aliguori68f00992009-01-21 18:59:12 +00001173 text_console_resize(s);
1174 }
balrog4d3b6f62008-02-10 16:33:14 +00001175 console_refresh(s);
1176}
1177
Anthony Liguoric227f092009-10-01 16:12:16 -05001178static void text_console_update(void *opaque, console_ch_t *chardata)
balrog4d3b6f62008-02-10 16:33:14 +00001179{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001180 QemuConsole *s = (QemuConsole *) opaque;
balrog4d3b6f62008-02-10 16:33:14 +00001181 int i, j, src;
1182
1183 if (s->text_x[0] <= s->text_x[1]) {
1184 src = (s->y_base + s->text_y[0]) * s->width;
1185 chardata += s->text_y[0] * s->width;
1186 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1187 for (j = 0; j < s->width; j ++, src ++)
1188 console_write_ch(chardata ++, s->cells[src].ch |
1189 (s->cells[src].t_attrib.fgcol << 12) |
1190 (s->cells[src].t_attrib.bgcol << 8) |
1191 (s->cells[src].t_attrib.bold << 21));
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001192 dpy_text_update(s, s->text_x[0], s->text_y[0],
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001193 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
balrog4d3b6f62008-02-10 16:33:14 +00001194 s->text_x[0] = s->width;
1195 s->text_y[0] = s->height;
1196 s->text_x[1] = 0;
1197 s->text_y[1] = 0;
1198 }
1199 if (s->cursor_invalidate) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001200 dpy_text_cursor(s, s->x, s->y);
balrog4d3b6f62008-02-10 16:33:14 +00001201 s->cursor_invalidate = 0;
1202 }
1203}
1204
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001205static QemuConsole *new_console(DisplayState *ds, console_type_t console_type)
bellarde7f0ad52004-07-14 17:28:59 +00001206{
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001207 Error *local_err = NULL;
Gerd Hoffmann95be0662013-04-17 09:45:10 +02001208 Object *obj;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001209 QemuConsole *s;
pbrook95219892006-04-09 01:06:34 +00001210 int i;
bellarde7f0ad52004-07-14 17:28:59 +00001211
1212 if (nb_consoles >= MAX_CONSOLES)
1213 return NULL;
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001214
Gerd Hoffmann95be0662013-04-17 09:45:10 +02001215 obj = object_new(TYPE_QEMU_CONSOLE);
1216 s = QEMU_CONSOLE(obj);
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001217 object_property_add_link(obj, "device", TYPE_DEVICE,
1218 (Object **)&s->device, &local_err);
1219
thsaf3a9032007-07-11 23:14:59 +00001220 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1221 (console_type == GRAPHIC_CONSOLE))) {
bellarde7f0ad52004-07-14 17:28:59 +00001222 active_console = s;
thsaf3a9032007-07-11 23:14:59 +00001223 }
bellarde7f0ad52004-07-14 17:28:59 +00001224 s->ds = ds;
thsaf3a9032007-07-11 23:14:59 +00001225 s->console_type = console_type;
1226 if (console_type != GRAPHIC_CONSOLE) {
Jan Kiszkaf81bdef2011-09-16 00:48:07 +02001227 s->index = nb_consoles;
pbrook95219892006-04-09 01:06:34 +00001228 consoles[nb_consoles++] = s;
1229 } else {
1230 /* HACK: Put graphical consoles before text consoles. */
1231 for (i = nb_consoles; i > 0; i--) {
thsaf3a9032007-07-11 23:14:59 +00001232 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
pbrook95219892006-04-09 01:06:34 +00001233 break;
1234 consoles[i] = consoles[i - 1];
Jan Kiszkaf81bdef2011-09-16 00:48:07 +02001235 consoles[i]->index = i;
pbrook95219892006-04-09 01:06:34 +00001236 }
Jan Kiszkaf81bdef2011-09-16 00:48:07 +02001237 s->index = i;
pbrook95219892006-04-09 01:06:34 +00001238 consoles[i] = s;
aliguori3023f332009-01-16 19:04:14 +00001239 nb_consoles++;
pbrook95219892006-04-09 01:06:34 +00001240 }
bellarde7f0ad52004-07-14 17:28:59 +00001241 return s;
1242}
1243
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001244static void qemu_alloc_display(DisplaySurface *surface, int width, int height,
1245 int linesize, PixelFormat pf, int newflags)
Jes Sorensenffe8b822011-03-16 13:33:30 +01001246{
Jes Sorensenffe8b822011-03-16 13:33:30 +01001247 surface->pf = pf;
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001248
1249 qemu_pixman_image_unref(surface->image);
1250 surface->image = NULL;
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001251
1252 surface->format = qemu_pixman_get_format(&pf);
1253 assert(surface->format != 0);
1254 surface->image = pixman_image_create_bits(surface->format,
1255 width, height,
1256 NULL, linesize);
1257 assert(surface->image != NULL);
1258
Jes Sorensenffe8b822011-03-16 13:33:30 +01001259 surface->flags = newflags | QEMU_ALLOCATED_FLAG;
Paolo Bonzini98b50082010-02-11 00:29:57 +01001260#ifdef HOST_WORDS_BIGENDIAN
Jes Sorensenffe8b822011-03-16 13:33:30 +01001261 surface->flags |= QEMU_BIG_ENDIAN_FLAG;
Paolo Bonzini98b50082010-02-11 00:29:57 +01001262#endif
Paolo Bonzini98b50082010-02-11 00:29:57 +01001263}
1264
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001265DisplaySurface *qemu_create_displaysurface(int width, int height)
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001266{
1267 DisplaySurface *surface = g_new0(DisplaySurface, 1);
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001268 int linesize = width * 4;
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001269
1270 trace_displaysurface_create(surface, width, height);
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001271 qemu_alloc_display(surface, width, height, linesize,
1272 qemu_default_pixelformat(32), 0);
1273 return surface;
1274}
1275
Gerd Hoffmann187cd1d2012-09-26 07:46:20 +02001276DisplaySurface *qemu_create_displaysurface_from(int width, int height, int bpp,
Gerd Hoffmannb1424e02013-02-20 09:37:12 +01001277 int linesize, uint8_t *data,
1278 bool byteswap)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001279{
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001280 DisplaySurface *surface = g_new0(DisplaySurface, 1);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001281
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001282 trace_displaysurface_create_from(surface, width, height, bpp, byteswap);
Gerd Hoffmannb1424e02013-02-20 09:37:12 +01001283 if (byteswap) {
1284 surface->pf = qemu_different_endianness_pixelformat(bpp);
1285 } else {
1286 surface->pf = qemu_default_pixelformat(bpp);
1287 }
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001288
1289 surface->format = qemu_pixman_get_format(&surface->pf);
1290 assert(surface->format != 0);
1291 surface->image = pixman_image_create_bits(surface->format,
1292 width, height,
1293 (void *)data, linesize);
1294 assert(surface->image != NULL);
1295
Paolo Bonzini98b50082010-02-11 00:29:57 +01001296#ifdef HOST_WORDS_BIGENDIAN
1297 surface->flags = QEMU_BIG_ENDIAN_FLAG;
1298#endif
Paolo Bonzini98b50082010-02-11 00:29:57 +01001299
1300 return surface;
1301}
1302
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001303static DisplaySurface *qemu_create_dummy_surface(void)
1304{
1305 static const char msg[] =
1306 "This VM has no graphic display device.";
1307 DisplaySurface *surface = qemu_create_displaysurface(640, 480);
1308 pixman_color_t bg = color_table_rgb[0][COLOR_BLACK];
1309 pixman_color_t fg = color_table_rgb[0][COLOR_WHITE];
1310 pixman_image_t *glyph;
1311 int len, x, y, i;
1312
1313 len = strlen(msg);
1314 x = (640/FONT_WIDTH - len) / 2;
1315 y = (480/FONT_HEIGHT - 1) / 2;
1316 for (i = 0; i < len; i++) {
1317 glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
1318 qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
1319 x+i, y, FONT_WIDTH, FONT_HEIGHT);
1320 qemu_pixman_image_unref(glyph);
1321 }
1322 return surface;
1323}
1324
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001325void qemu_free_displaysurface(DisplaySurface *surface)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001326{
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001327 if (surface == NULL) {
Paolo Bonzini98b50082010-02-11 00:29:57 +01001328 return;
Gerd Hoffmann187cd1d2012-09-26 07:46:20 +02001329 }
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001330 trace_displaysurface_free(surface);
1331 qemu_pixman_image_unref(surface->image);
1332 g_free(surface);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001333}
1334
Gerd Hoffmann52090892013-04-23 15:44:31 +02001335void register_displaychangelistener(DisplayChangeListener *dcl)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001336{
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001337 static DisplaySurface *dummy;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001338 QemuConsole *con;
1339
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001340 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
Gerd Hoffmann52090892013-04-23 15:44:31 +02001341 dcl->ds = get_alloc_displaystate();
1342 QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
1343 gui_setup_refresh(dcl->ds);
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001344 if (dcl->con) {
1345 dcl->con->dcls++;
1346 con = dcl->con;
1347 } else {
1348 con = active_console;
1349 }
Gerd Hoffmannd3002b02013-04-25 09:33:19 +02001350 if (dcl->ops->dpy_gfx_switch) {
1351 if (con) {
1352 dcl->ops->dpy_gfx_switch(dcl, con->surface);
1353 } else {
1354 if (!dummy) {
1355 dummy = qemu_create_dummy_surface();
1356 }
1357 dcl->ops->dpy_gfx_switch(dcl, dummy);
1358 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001359 }
1360}
1361
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +01001362void update_displaychangelistener(DisplayChangeListener *dcl,
1363 uint64_t interval)
1364{
1365 DisplayState *ds = dcl->ds;
1366
1367 dcl->update_interval = interval;
1368 if (!ds->refreshing && ds->update_interval > interval) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001369 timer_mod(ds->gui_timer, ds->last_update + interval);
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +01001370 }
1371}
1372
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001373void unregister_displaychangelistener(DisplayChangeListener *dcl)
1374{
1375 DisplayState *ds = dcl->ds;
1376 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001377 if (dcl->con) {
1378 dcl->con->dcls--;
1379 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001380 QLIST_REMOVE(dcl, next);
1381 gui_setup_refresh(ds);
1382}
1383
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001384void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001385{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001386 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001387 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001388 int width = surface_width(con->surface);
1389 int height = surface_height(con->surface);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001390
1391 x = MAX(x, 0);
1392 y = MAX(y, 0);
1393 x = MIN(x, width);
1394 y = MIN(y, height);
1395 w = MIN(w, width - x);
1396 h = MIN(h, height - y);
1397
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001398 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001399 return;
1400 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001401 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001402 if (con != (dcl->con ? dcl->con : active_console)) {
1403 continue;
1404 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001405 if (dcl->ops->dpy_gfx_update) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001406 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001407 }
1408 }
1409}
1410
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001411void dpy_gfx_replace_surface(QemuConsole *con,
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001412 DisplaySurface *surface)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001413{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001414 DisplayState *s = con->ds;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001415 DisplaySurface *old_surface = con->surface;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001416 DisplayChangeListener *dcl;
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001417
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001418 con->surface = surface;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001419 QLIST_FOREACH(dcl, &s->listeners, next) {
1420 if (con != (dcl->con ? dcl->con : active_console)) {
1421 continue;
1422 }
1423 if (dcl->ops->dpy_gfx_switch) {
1424 dcl->ops->dpy_gfx_switch(dcl, surface);
1425 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001426 }
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001427 qemu_free_displaysurface(old_surface);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001428}
1429
1430void dpy_refresh(DisplayState *s)
1431{
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001432 DisplayChangeListener *dcl;
1433
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001434 QLIST_FOREACH(dcl, &s->listeners, next) {
1435 if (dcl->ops->dpy_refresh) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001436 dcl->ops->dpy_refresh(dcl);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001437 }
1438 }
1439}
1440
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001441void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1442 int dst_x, int dst_y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001443{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001444 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001445 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001446
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001447 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001448 return;
1449 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001450 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001451 if (con != (dcl->con ? dcl->con : active_console)) {
1452 continue;
1453 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001454 if (dcl->ops->dpy_gfx_copy) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001455 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001456 } else { /* TODO */
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001457 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001458 }
1459 }
1460}
1461
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001462void dpy_text_cursor(QemuConsole *con, int x, int y)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001463{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001464 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001465 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001466
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001467 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001468 return;
1469 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001470 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001471 if (con != (dcl->con ? dcl->con : active_console)) {
1472 continue;
1473 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001474 if (dcl->ops->dpy_text_cursor) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001475 dcl->ops->dpy_text_cursor(dcl, x, y);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001476 }
1477 }
1478}
1479
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001480void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001481{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001482 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001483 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001484
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001485 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001486 return;
1487 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001488 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001489 if (con != (dcl->con ? dcl->con : active_console)) {
1490 continue;
1491 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001492 if (dcl->ops->dpy_text_update) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001493 dcl->ops->dpy_text_update(dcl, x, y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001494 }
1495 }
1496}
1497
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001498void dpy_text_resize(QemuConsole *con, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001499{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001500 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001501 struct DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001502
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001503 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001504 return;
1505 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001506 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001507 if (con != (dcl->con ? dcl->con : active_console)) {
1508 continue;
1509 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001510 if (dcl->ops->dpy_text_resize) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001511 dcl->ops->dpy_text_resize(dcl, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001512 }
1513 }
1514}
1515
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001516void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001517{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001518 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001519 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001520
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001521 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001522 return;
1523 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001524 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001525 if (con != (dcl->con ? dcl->con : active_console)) {
1526 continue;
1527 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001528 if (dcl->ops->dpy_mouse_set) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001529 dcl->ops->dpy_mouse_set(dcl, x, y, on);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001530 }
1531 }
1532}
1533
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001534void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001535{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001536 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001537 DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001538
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +01001539 if (!qemu_console_is_visible(con)) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001540 return;
1541 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001542 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001543 if (con != (dcl->con ? dcl->con : active_console)) {
1544 continue;
1545 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001546 if (dcl->ops->dpy_cursor_define) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001547 dcl->ops->dpy_cursor_define(dcl, cursor);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001548 }
1549 }
1550}
1551
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001552bool dpy_cursor_define_supported(QemuConsole *con)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001553{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001554 DisplayState *s = con->ds;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001555 DisplayChangeListener *dcl;
1556
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001557 QLIST_FOREACH(dcl, &s->listeners, next) {
1558 if (dcl->ops->dpy_cursor_define) {
1559 return true;
1560 }
1561 }
1562 return false;
1563}
1564
Paolo Bonzini98b50082010-02-11 00:29:57 +01001565/***********************************************************/
1566/* register display */
1567
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001568/* console.c internal use only */
1569static DisplayState *get_alloc_displaystate(void)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001570{
1571 if (!display_state) {
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001572 display_state = g_new0(DisplayState, 1);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001573 }
1574 return display_state;
1575}
1576
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001577/*
1578 * Called by main(), after creating QemuConsoles
1579 * and before initializing ui (sdl/vnc/...).
1580 */
1581DisplayState *init_displaystate(void)
1582{
Gerd Hoffmann43f420f2013-06-25 10:49:31 +02001583 Error *local_err = NULL;
1584 gchar *name;
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001585 int i;
1586
1587 if (!display_state) {
1588 display_state = g_new0(DisplayState, 1);
1589 }
1590
1591 for (i = 0; i < nb_consoles; i++) {
1592 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1593 consoles[i]->ds == NULL) {
1594 text_console_do_init(consoles[i]->chr, display_state);
1595 }
Gerd Hoffmann43f420f2013-06-25 10:49:31 +02001596
1597 /* Hook up into the qom tree here (not in new_console()), once
1598 * all QemuConsoles are created and the order / numbering
1599 * doesn't change any more */
1600 name = g_strdup_printf("console[%d]", i);
1601 object_property_add_child(container_get(object_get_root(), "/backend"),
1602 name, OBJECT(consoles[i]), &local_err);
1603 g_free(name);
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001604 }
1605
1606 return display_state;
1607}
1608
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001609QemuConsole *graphic_console_init(DeviceState *dev,
1610 const GraphicHwOps *hw_ops,
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001611 void *opaque)
bellarde7f0ad52004-07-14 17:28:59 +00001612{
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001613 Error *local_err = NULL;
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001614 int width = 640;
1615 int height = 480;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001616 QemuConsole *s;
aliguori3023f332009-01-16 19:04:14 +00001617 DisplayState *ds;
aurel32f0f2f972009-01-16 21:13:49 +00001618
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001619 ds = get_alloc_displaystate();
Gerd Hoffmann437fe102013-03-07 16:04:52 +01001620 trace_console_gfx_new();
thsaf3a9032007-07-11 23:14:59 +00001621 s = new_console(ds, GRAPHIC_CONSOLE);
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001622 s->hw_ops = hw_ops;
pbrook95219892006-04-09 01:06:34 +00001623 s->hw = opaque;
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001624 if (dev) {
1625 object_property_set_link(OBJECT(s), OBJECT(dev),
1626 "device", &local_err);
1627 }
aliguori3023f332009-01-16 19:04:14 +00001628
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001629 s->surface = qemu_create_displaysurface(width, height);
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001630 return s;
pbrook95219892006-04-09 01:06:34 +00001631}
1632
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01001633QemuConsole *qemu_console_lookup_by_index(unsigned int index)
1634{
1635 if (index >= MAX_CONSOLES) {
1636 return NULL;
1637 }
1638 return consoles[index];
1639}
1640
Gerd Hoffmann14a93642013-04-18 07:30:40 +02001641QemuConsole *qemu_console_lookup_by_device(DeviceState *dev)
1642{
1643 Error *local_err = NULL;
1644 Object *obj;
1645 int i;
1646
1647 for (i = 0; i < nb_consoles; i++) {
1648 if (!consoles[i]) {
1649 continue;
1650 }
1651 obj = object_property_get_link(OBJECT(consoles[i]),
1652 "device", &local_err);
1653 if (DEVICE(obj) == dev) {
1654 return consoles[i];
1655 }
1656 }
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
Paolo Bonzini41048332010-12-23 13:42:52 +01001681static void text_console_set_echo(CharDriverState *chr, bool echo)
1682{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001683 QemuConsole *s = chr->opaque;
Paolo Bonzini41048332010-12-23 13:42:52 +01001684
1685 s->echo = echo;
1686}
1687
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001688static void text_console_update_cursor(void *opaque)
1689{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001690 QemuConsole *s = opaque;
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001691
1692 s->cursor_visible_phase = !s->cursor_visible_phase;
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +01001693 graphic_hw_invalidate(s);
Alex Blighbc72ad62013-08-21 16:03:08 +01001694 timer_mod(s->cursor_timer,
1695 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + CONSOLE_CURSOR_PERIOD / 2);
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001696}
1697
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001698static const GraphicHwOps text_console_ops = {
1699 .invalidate = text_console_invalidate,
1700 .text_update = text_console_update,
1701};
1702
Paolo Bonzini44b37b92010-12-23 13:42:53 +01001703static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
bellarde7f0ad52004-07-14 17:28:59 +00001704{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001705 QemuConsole *s;
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001706 int g_width = 80 * FONT_WIDTH;
1707 int g_height = 24 * FONT_HEIGHT;
pbrook6d6f7c22006-03-11 15:35:30 +00001708
Paolo Bonzini491e1142010-12-23 13:42:51 +01001709 s = chr->opaque;
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02001710
bellarde7f0ad52004-07-14 17:28:59 +00001711 chr->chr_write = console_puts;
bellard6fcfafb2004-08-01 21:48:30 +00001712
bellarde15d7372006-06-25 16:26:29 +00001713 s->out_fifo.buf = s->out_fifo_buf;
1714 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
Alex Blighbc72ad62013-08-21 16:03:08 +01001715 s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
aliguori3023f332009-01-16 19:04:14 +00001716 s->ds = ds;
ths3b46e622007-09-17 08:09:54 +00001717
bellarde7f0ad52004-07-14 17:28:59 +00001718 s->y_displayed = 0;
1719 s->y_base = 0;
1720 s->total_height = DEFAULT_BACKSCROLL;
1721 s->x = 0;
1722 s->y = 0;
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001723 if (!s->surface) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001724 if (active_console && active_console->surface) {
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001725 g_width = surface_width(active_console->surface);
1726 g_height = surface_height(active_console->surface);
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001727 }
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001728 s->surface = qemu_create_displaysurface(g_width, g_height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001729 }
pbrook6d6f7c22006-03-11 15:35:30 +00001730
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001731 s->cursor_timer =
Alex Blighbc72ad62013-08-21 16:03:08 +01001732 timer_new_ms(QEMU_CLOCK_REALTIME, text_console_update_cursor, s);
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001733
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001734 s->hw_ops = &text_console_ops;
balrog4d3b6f62008-02-10 16:33:14 +00001735 s->hw = s;
1736
pbrook6d6f7c22006-03-11 15:35:30 +00001737 /* Set text attribute defaults */
1738 s->t_attrib_default.bold = 0;
1739 s->t_attrib_default.uline = 0;
1740 s->t_attrib_default.blink = 0;
1741 s->t_attrib_default.invers = 0;
1742 s->t_attrib_default.unvisible = 0;
1743 s->t_attrib_default.fgcol = COLOR_WHITE;
1744 s->t_attrib_default.bgcol = COLOR_BLACK;
pbrook6d6f7c22006-03-11 15:35:30 +00001745 /* set current text attributes to default */
1746 s->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +00001747 text_console_resize(s);
1748
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01001749 if (chr->label) {
1750 char msg[128];
1751 int len;
1752
Gerd Hoffmann735ba582009-12-08 13:11:40 +01001753 s->t_attrib.bgcol = COLOR_BLUE;
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01001754 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
1755 console_puts(chr, (uint8_t*)msg, len);
Gerd Hoffmann735ba582009-12-08 13:11:40 +01001756 s->t_attrib = s->t_attrib_default;
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01001757 }
1758
Hans de Goedefee204f2013-03-26 11:07:54 +01001759 qemu_chr_be_generic_open(chr);
aurel32ceecf1d2009-01-18 14:08:04 +00001760 if (chr->init)
1761 chr->init(chr);
bellarde7f0ad52004-07-14 17:28:59 +00001762}
pbrookc60e08d2008-07-01 16:24:38 +00001763
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001764static CharDriverState *text_console_init(ChardevVC *vc)
aliguori2796dae2009-01-16 20:23:27 +00001765{
1766 CharDriverState *chr;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001767 QemuConsole *s;
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001768 unsigned width = 0;
1769 unsigned height = 0;
aliguori2796dae2009-01-16 20:23:27 +00001770
Anthony Liguori7267c092011-08-20 22:09:37 -05001771 chr = g_malloc0(sizeof(CharDriverState));
aliguori2796dae2009-01-16 20:23:27 +00001772
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001773 if (vc->has_width) {
1774 width = vc->width;
1775 } else if (vc->has_cols) {
1776 width = vc->cols * FONT_WIDTH;
1777 }
Paolo Bonzini491e1142010-12-23 13:42:51 +01001778
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001779 if (vc->has_height) {
1780 height = vc->height;
1781 } else if (vc->has_rows) {
1782 height = vc->rows * FONT_HEIGHT;
1783 }
Paolo Bonzini491e1142010-12-23 13:42:51 +01001784
Gerd Hoffmann437fe102013-03-07 16:04:52 +01001785 trace_console_txt_new(width, height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001786 if (width == 0 || height == 0) {
1787 s = new_console(NULL, TEXT_CONSOLE);
1788 } else {
1789 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE);
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001790 s->surface = qemu_create_displaysurface(width, height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001791 }
1792
1793 if (!s) {
Stefan Weil5354d082011-10-02 18:53:09 +02001794 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001795 return NULL;
Paolo Bonzini491e1142010-12-23 13:42:51 +01001796 }
1797
1798 s->chr = chr;
Paolo Bonzini491e1142010-12-23 13:42:51 +01001799 chr->opaque = s;
Paolo Bonzini41048332010-12-23 13:42:52 +01001800 chr->chr_set_echo = text_console_set_echo;
Michael Rothbd5c51e2013-06-07 15:19:53 -05001801 /* console/chardev init sometimes completes elsewhere in a 2nd
1802 * stage, so defer OPENED events until they are fully initialized
1803 */
1804 chr->explicit_be_open = true;
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001805
1806 if (display_state) {
1807 text_console_do_init(chr, display_state);
1808 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001809 return chr;
aliguori2796dae2009-01-16 20:23:27 +00001810}
1811
Anthony Liguorid82831d2013-02-20 07:43:19 -06001812static VcHandler *vc_handler = text_console_init;
1813
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001814CharDriverState *vc_init(ChardevVC *vc)
Anthony Liguorid82831d2013-02-20 07:43:19 -06001815{
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001816 return vc_handler(vc);
Anthony Liguorid82831d2013-02-20 07:43:19 -06001817}
1818
1819void register_vc_handler(VcHandler *handler)
1820{
1821 vc_handler = handler;
1822}
1823
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001824void qemu_console_resize(QemuConsole *s, int width, int height)
pbrookc60e08d2008-07-01 16:24:38 +00001825{
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001826 DisplaySurface *surface;
1827
1828 assert(s->console_type == GRAPHIC_CONSOLE);
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001829 surface = qemu_create_displaysurface(width, height);
1830 dpy_gfx_replace_surface(s, surface);
pbrookc60e08d2008-07-01 16:24:38 +00001831}
balrog38334f72008-09-24 02:21:24 +00001832
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001833void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
aliguori3023f332009-01-16 19:04:14 +00001834 int dst_x, int dst_y, int w, int h)
balrogc21bbcf2008-09-24 03:32:33 +00001835{
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001836 assert(con->console_type == GRAPHIC_CONSOLE);
1837 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
balrog38334f72008-09-24 02:21:24 +00001838}
aliguori7d957bd2009-01-15 22:14:11 +00001839
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001840DisplaySurface *qemu_console_surface(QemuConsole *console)
1841{
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001842 return console->surface;
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001843}
1844
1845DisplayState *qemu_console_displaystate(QemuConsole *console)
1846{
1847 return console->ds;
1848}
1849
malc0da2ea12009-01-23 19:56:19 +00001850PixelFormat qemu_different_endianness_pixelformat(int bpp)
aliguori7d957bd2009-01-15 22:14:11 +00001851{
1852 PixelFormat pf;
1853
1854 memset(&pf, 0x00, sizeof(PixelFormat));
1855
1856 pf.bits_per_pixel = bpp;
BALATON Zoltanfeadf1a2012-08-22 17:19:42 +02001857 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
aliguori7d957bd2009-01-15 22:14:11 +00001858 pf.depth = bpp == 32 ? 24 : bpp;
1859
1860 switch (bpp) {
malc0da2ea12009-01-23 19:56:19 +00001861 case 24:
1862 pf.rmask = 0x000000FF;
1863 pf.gmask = 0x0000FF00;
1864 pf.bmask = 0x00FF0000;
1865 pf.rmax = 255;
1866 pf.gmax = 255;
1867 pf.bmax = 255;
1868 pf.rshift = 0;
1869 pf.gshift = 8;
1870 pf.bshift = 16;
aliguori90a1e3c2009-01-26 15:37:30 +00001871 pf.rbits = 8;
1872 pf.gbits = 8;
1873 pf.bbits = 8;
aliguori7d957bd2009-01-15 22:14:11 +00001874 break;
malc0da2ea12009-01-23 19:56:19 +00001875 case 32:
1876 pf.rmask = 0x0000FF00;
1877 pf.gmask = 0x00FF0000;
1878 pf.bmask = 0xFF000000;
1879 pf.amask = 0x00000000;
1880 pf.amax = 255;
1881 pf.rmax = 255;
1882 pf.gmax = 255;
1883 pf.bmax = 255;
1884 pf.ashift = 0;
1885 pf.rshift = 8;
1886 pf.gshift = 16;
1887 pf.bshift = 24;
aliguori90a1e3c2009-01-26 15:37:30 +00001888 pf.rbits = 8;
1889 pf.gbits = 8;
1890 pf.bbits = 8;
1891 pf.abits = 8;
malc0da2ea12009-01-23 19:56:19 +00001892 break;
1893 default:
1894 break;
1895 }
1896 return pf;
1897}
1898
1899PixelFormat qemu_default_pixelformat(int bpp)
1900{
1901 PixelFormat pf;
1902
1903 memset(&pf, 0x00, sizeof(PixelFormat));
1904
1905 pf.bits_per_pixel = bpp;
BALATON Zoltanfeadf1a2012-08-22 17:19:42 +02001906 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
malc0da2ea12009-01-23 19:56:19 +00001907 pf.depth = bpp == 32 ? 24 : bpp;
1908
1909 switch (bpp) {
Gerd Hoffmannb6278082010-05-21 11:59:14 +02001910 case 15:
1911 pf.bits_per_pixel = 16;
Gerd Hoffmannb6278082010-05-21 11:59:14 +02001912 pf.rmask = 0x00007c00;
1913 pf.gmask = 0x000003E0;
1914 pf.bmask = 0x0000001F;
1915 pf.rmax = 31;
1916 pf.gmax = 31;
1917 pf.bmax = 31;
1918 pf.rshift = 10;
1919 pf.gshift = 5;
1920 pf.bshift = 0;
1921 pf.rbits = 5;
1922 pf.gbits = 5;
1923 pf.bbits = 5;
1924 break;
aliguori7d957bd2009-01-15 22:14:11 +00001925 case 16:
1926 pf.rmask = 0x0000F800;
1927 pf.gmask = 0x000007E0;
1928 pf.bmask = 0x0000001F;
1929 pf.rmax = 31;
1930 pf.gmax = 63;
1931 pf.bmax = 31;
1932 pf.rshift = 11;
1933 pf.gshift = 5;
1934 pf.bshift = 0;
aliguori90a1e3c2009-01-26 15:37:30 +00001935 pf.rbits = 5;
1936 pf.gbits = 6;
1937 pf.bbits = 5;
aliguori7d957bd2009-01-15 22:14:11 +00001938 break;
1939 case 24:
aliguori7d957bd2009-01-15 22:14:11 +00001940 pf.rmask = 0x00FF0000;
1941 pf.gmask = 0x0000FF00;
1942 pf.bmask = 0x000000FF;
1943 pf.rmax = 255;
1944 pf.gmax = 255;
1945 pf.bmax = 255;
1946 pf.rshift = 16;
1947 pf.gshift = 8;
1948 pf.bshift = 0;
aliguori90a1e3c2009-01-26 15:37:30 +00001949 pf.rbits = 8;
1950 pf.gbits = 8;
1951 pf.bbits = 8;
Markus Armbruster0eba62e2011-11-22 12:56:10 +01001952 break;
malc0da2ea12009-01-23 19:56:19 +00001953 case 32:
1954 pf.rmask = 0x00FF0000;
1955 pf.gmask = 0x0000FF00;
1956 pf.bmask = 0x000000FF;
malc0da2ea12009-01-23 19:56:19 +00001957 pf.rmax = 255;
1958 pf.gmax = 255;
1959 pf.bmax = 255;
malc0da2ea12009-01-23 19:56:19 +00001960 pf.rshift = 16;
1961 pf.gshift = 8;
1962 pf.bshift = 0;
aliguori90a1e3c2009-01-26 15:37:30 +00001963 pf.rbits = 8;
1964 pf.gbits = 8;
1965 pf.bbits = 8;
aliguori7d957bd2009-01-15 22:14:11 +00001966 break;
1967 default:
1968 break;
1969 }
1970 return pf;
1971}
Anthony Liguori01f45d92013-03-05 23:21:32 +05301972
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001973static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
1974 Error **errp)
1975{
1976 int val;
1977
1978 backend->vc = g_new0(ChardevVC, 1);
1979
1980 val = qemu_opt_get_number(opts, "width", 0);
1981 if (val != 0) {
1982 backend->vc->has_width = true;
1983 backend->vc->width = val;
1984 }
1985
1986 val = qemu_opt_get_number(opts, "height", 0);
1987 if (val != 0) {
1988 backend->vc->has_height = true;
1989 backend->vc->height = val;
1990 }
1991
1992 val = qemu_opt_get_number(opts, "cols", 0);
1993 if (val != 0) {
1994 backend->vc->has_cols = true;
1995 backend->vc->cols = val;
1996 }
1997
1998 val = qemu_opt_get_number(opts, "rows", 0);
1999 if (val != 0) {
2000 backend->vc->has_rows = true;
2001 backend->vc->rows = val;
2002 }
2003}
2004
Gerd Hoffmann95be0662013-04-17 09:45:10 +02002005static const TypeInfo qemu_console_info = {
2006 .name = TYPE_QEMU_CONSOLE,
2007 .parent = TYPE_OBJECT,
2008 .instance_size = sizeof(QemuConsole),
2009 .class_size = sizeof(QemuConsoleClass),
2010};
2011
2012
Anthony Liguori01f45d92013-03-05 23:21:32 +05302013static void register_types(void)
2014{
Gerd Hoffmann95be0662013-04-17 09:45:10 +02002015 type_register_static(&qemu_console_info);
Gerd Hoffmann702ec692013-02-25 15:52:32 +01002016 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC,
2017 qemu_chr_parse_vc);
Anthony Liguori01f45d92013-03-05 23:21:32 +05302018}
2019
2020type_init(register_types);