blob: 43ff80b451a7af9d104ea5247f6f6d4b0cca7a67 [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"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010026#include "qemu/timer.h"
Luiz Capitulinoad39cf62012-05-24 13:48:23 -030027#include "qmp-commands.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020028#include "sysemu/char.h"
bellarde7f0ad52004-07-14 17:28:59 +000029
pbrook6d6f7c22006-03-11 15:35:30 +000030//#define DEBUG_CONSOLE
bellarde7f0ad52004-07-14 17:28:59 +000031#define DEFAULT_BACKSCROLL 512
32#define MAX_CONSOLES 12
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 {
Jan Kiszkaf81bdef2011-09-16 00:48:07 +0200116 int index;
Anthony Liguoric227f092009-10-01 16:12:16 -0500117 console_type_t console_type;
bellarde7f0ad52004-07-14 17:28:59 +0000118 DisplayState *ds;
Gerd Hoffmann321f0482013-03-12 14:39:22 +0100119 DisplaySurface *surface;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200120
pbrook95219892006-04-09 01:06:34 +0000121 /* Graphic console state. */
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100122 const GraphicHwOps *hw_ops;
pbrook95219892006-04-09 01:06:34 +0000123 void *hw;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200124
125 /* Text console state */
bellarde7f0ad52004-07-14 17:28:59 +0000126 int width;
127 int height;
128 int total_height;
129 int backscroll_height;
bellarde7f0ad52004-07-14 17:28:59 +0000130 int x, y;
thsadb47962007-01-16 23:02:36 +0000131 int x_saved, y_saved;
bellarde7f0ad52004-07-14 17:28:59 +0000132 int y_displayed;
133 int y_base;
pbrook6d6f7c22006-03-11 15:35:30 +0000134 TextAttributes t_attrib_default; /* default text attributes */
135 TextAttributes t_attrib; /* currently active text attributes */
bellarde7f0ad52004-07-14 17:28:59 +0000136 TextCell *cells;
balrog4d3b6f62008-02-10 16:33:14 +0000137 int text_x[2], text_y[2], cursor_invalidate;
Paolo Bonzini41048332010-12-23 13:42:52 +0100138 int echo;
Jan Kiszkabf1bed82012-07-10 22:00:55 +0200139 bool cursor_visible_phase;
140 QEMUTimer *cursor_timer;
bellarde7f0ad52004-07-14 17:28:59 +0000141
pbrook14778c22009-01-21 03:02:52 +0000142 int update_x0;
143 int update_y0;
144 int update_x1;
145 int update_y1;
146
bellarde7f0ad52004-07-14 17:28:59 +0000147 enum TTYState state;
148 int esc_params[MAX_ESC_PARAMS];
149 int nb_esc_params;
150
pbrooke5b0bc42007-01-27 23:46:43 +0000151 CharDriverState *chr;
bellarde15d7372006-06-25 16:26:29 +0000152 /* fifo for key pressed */
153 QEMUFIFO out_fifo;
154 uint8_t out_fifo_buf[16];
155 QEMUTimer *kbd_timer;
bellarde7f0ad52004-07-14 17:28:59 +0000156};
157
Gerd Hoffmann27be5582013-03-13 12:25:25 +0100158struct DisplayState {
159 struct QEMUTimer *gui_timer;
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100160 uint64_t last_update;
161 uint64_t update_interval;
162 bool refreshing;
Gerd Hoffmann27be5582013-03-13 12:25:25 +0100163 bool have_gfx;
164 bool have_text;
165
166 QLIST_HEAD(, DisplayChangeListener) listeners;
167};
168
Paolo Bonzini98b50082010-02-11 00:29:57 +0100169static DisplayState *display_state;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200170static QemuConsole *active_console;
171static QemuConsole *consoles[MAX_CONSOLES];
bellarde7f0ad52004-07-14 17:28:59 +0000172static int nb_consoles = 0;
173
Gerd Hoffmann64840c62013-03-07 17:08:29 +0100174static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
Gerd Hoffmann321f0482013-03-12 14:39:22 +0100175static void dpy_gfx_switch_surface(DisplayState *ds,
176 DisplaySurface *surface);
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100177static void dpy_refresh(DisplayState *s);
Gerd Hoffmann64840c62013-03-07 17:08:29 +0100178
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100179static void gui_update(void *opaque)
180{
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100181 uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
182 uint64_t dcl_interval;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100183 DisplayState *ds = opaque;
184 DisplayChangeListener *dcl;
Gerd Hoffmanndea1b0b2013-03-19 15:01:02 +0100185 int i;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100186
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100187 ds->refreshing = true;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100188 dpy_refresh(ds);
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100189 ds->refreshing = false;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100190
191 QLIST_FOREACH(dcl, &ds->listeners, next) {
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100192 dcl_interval = dcl->update_interval ?
193 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
194 if (interval > dcl_interval) {
195 interval = dcl_interval;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100196 }
197 }
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100198 if (ds->update_interval != interval) {
199 ds->update_interval = interval;
Gerd Hoffmanndea1b0b2013-03-19 15:01:02 +0100200 for (i = 0; i < nb_consoles; i++) {
201 if (consoles[i]->hw_ops->update_interval) {
202 consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval);
203 }
204 }
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100205 trace_console_refresh(interval);
206 }
207 ds->last_update = qemu_get_clock_ms(rt_clock);
208 qemu_mod_timer(ds->gui_timer, ds->last_update + interval);
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100209}
210
211static void gui_setup_refresh(DisplayState *ds)
212{
213 DisplayChangeListener *dcl;
214 bool need_timer = false;
215 bool have_gfx = false;
216 bool have_text = false;
217
218 QLIST_FOREACH(dcl, &ds->listeners, next) {
219 if (dcl->ops->dpy_refresh != NULL) {
220 need_timer = true;
221 }
222 if (dcl->ops->dpy_gfx_update != NULL) {
223 have_gfx = true;
224 }
225 if (dcl->ops->dpy_text_update != NULL) {
226 have_text = true;
227 }
228 }
229
230 if (need_timer && ds->gui_timer == NULL) {
231 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
232 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
233 }
234 if (!need_timer && ds->gui_timer != NULL) {
235 qemu_del_timer(ds->gui_timer);
236 qemu_free_timer(ds->gui_timer);
237 ds->gui_timer = NULL;
238 }
239
240 ds->have_gfx = have_gfx;
241 ds->have_text = have_text;
242}
243
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100244void graphic_hw_update(QemuConsole *con)
pbrook95219892006-04-09 01:06:34 +0000245{
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100246 if (!con) {
247 con = active_console;
248 }
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100249 if (con && con->hw_ops->gfx_update) {
250 con->hw_ops->gfx_update(con->hw);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100251 }
pbrook95219892006-04-09 01:06:34 +0000252}
253
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100254void graphic_hw_invalidate(QemuConsole *con)
pbrook95219892006-04-09 01:06:34 +0000255{
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100256 if (!con) {
257 con = active_console;
258 }
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100259 if (con && con->hw_ops->invalidate) {
260 con->hw_ops->invalidate(con->hw);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100261 }
pbrook95219892006-04-09 01:06:34 +0000262}
263
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100264static void ppm_save(const char *filename, struct DisplaySurface *ds,
265 Error **errp)
266{
267 int width = pixman_image_get_width(ds->image);
268 int height = pixman_image_get_height(ds->image);
269 FILE *f;
270 int y;
271 int ret;
272 pixman_image_t *linebuf;
273
274 trace_ppm_save(filename, ds);
275 f = fopen(filename, "wb");
276 if (!f) {
277 error_setg(errp, "failed to open file '%s': %s", filename,
278 strerror(errno));
279 return;
280 }
281 ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
282 if (ret < 0) {
283 linebuf = NULL;
284 goto write_err;
285 }
286 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
287 for (y = 0; y < height; y++) {
288 qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
289 clearerr(f);
290 ret = fwrite(pixman_image_get_data(linebuf), 1,
291 pixman_image_get_stride(linebuf), f);
292 (void)ret;
293 if (ferror(f)) {
294 goto write_err;
295 }
296 }
297
298out:
299 qemu_pixman_image_unref(linebuf);
300 fclose(f);
301 return;
302
303write_err:
304 error_setg(errp, "failed to write to file '%s': %s", filename,
305 strerror(errno));
306 unlink(filename);
307 goto out;
308}
309
Luiz Capitulinoad39cf62012-05-24 13:48:23 -0300310void qmp_screendump(const char *filename, Error **errp)
pbrook95219892006-04-09 01:06:34 +0000311{
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100312 QemuConsole *con = consoles[0];
313 DisplaySurface *surface;
balrog8571c052008-07-19 13:04:26 +0000314
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100315 if (con == NULL) {
316 error_setg(errp, "There is no QemuConsole I can screendump from.");
317 return;
Jan Kiszkaf81bdef2011-09-16 00:48:07 +0200318 }
319
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100320 graphic_hw_update(con);
321 surface = qemu_console_surface(con);
322 ppm_save(filename, surface, errp);
pbrook95219892006-04-09 01:06:34 +0000323}
324
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100325void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
balrog4d3b6f62008-02-10 16:33:14 +0000326{
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100327 if (!con) {
328 con = active_console;
329 }
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100330 if (con && con->hw_ops->text_update) {
331 con->hw_ops->text_update(con->hw, chardata);
332 }
balrog4d3b6f62008-02-10 16:33:14 +0000333}
334
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100335static void vga_fill_rect(QemuConsole *con,
336 int posx, int posy, int width, int height,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100337 pixman_color_t color)
bellarde7f0ad52004-07-14 17:28:59 +0000338{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100339 DisplaySurface *surface = qemu_console_surface(con);
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100340 pixman_rectangle16_t rect = {
341 .x = posx, .y = posy, .width = width, .height = height
342 };
ths3b46e622007-09-17 08:09:54 +0000343
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100344 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100345 &color, 1, &rect);
bellarde7f0ad52004-07-14 17:28:59 +0000346}
347
348/* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100349static void vga_bitblt(QemuConsole *con,
350 int xs, int ys, int xd, int yd, int w, int h)
bellarde7f0ad52004-07-14 17:28:59 +0000351{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100352 DisplaySurface *surface = qemu_console_surface(con);
bellarde7f0ad52004-07-14 17:28:59 +0000353
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100354 pixman_image_composite(PIXMAN_OP_SRC,
355 surface->image, NULL, surface->image,
356 xs, ys, 0, 0, xd, yd, w, h);
bellarde7f0ad52004-07-14 17:28:59 +0000357}
358
359/***********************************************************/
360/* basic char display */
361
362#define FONT_HEIGHT 16
363#define FONT_WIDTH 8
364
365#include "vgafont.h"
366
Devin J. Pohlydf00bed2011-09-07 15:44:36 -0400367#ifndef CONFIG_CURSES
pbrook6d6f7c22006-03-11 15:35:30 +0000368enum color_names {
369 COLOR_BLACK = 0,
370 COLOR_RED = 1,
371 COLOR_GREEN = 2,
372 COLOR_YELLOW = 3,
373 COLOR_BLUE = 4,
374 COLOR_MAGENTA = 5,
375 COLOR_CYAN = 6,
376 COLOR_WHITE = 7
377};
Devin J. Pohlydf00bed2011-09-07 15:44:36 -0400378#endif
pbrook6d6f7c22006-03-11 15:35:30 +0000379
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100380#define QEMU_RGB(r, g, b) \
381 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
382
383static const pixman_color_t color_table_rgb[2][8] = {
pbrook6d6f7c22006-03-11 15:35:30 +0000384 { /* dark */
bellard26489842006-06-25 17:37:36 +0000385 QEMU_RGB(0x00, 0x00, 0x00), /* black */
386 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
387 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
388 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
389 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
390 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
391 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
392 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
pbrook6d6f7c22006-03-11 15:35:30 +0000393 },
394 { /* bright */
bellard26489842006-06-25 17:37:36 +0000395 QEMU_RGB(0x00, 0x00, 0x00), /* black */
396 QEMU_RGB(0xff, 0x00, 0x00), /* red */
397 QEMU_RGB(0x00, 0xff, 0x00), /* green */
398 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
399 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
400 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
401 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
402 QEMU_RGB(0xff, 0xff, 0xff), /* white */
pbrook6d6f7c22006-03-11 15:35:30 +0000403 }
bellarde7f0ad52004-07-14 17:28:59 +0000404};
405
pbrook6d6f7c22006-03-11 15:35:30 +0000406#ifdef DEBUG_CONSOLE
407static void console_print_text_attributes(TextAttributes *t_attrib, char ch)
408{
409 if (t_attrib->bold) {
410 printf("b");
411 } else {
412 printf(" ");
413 }
414 if (t_attrib->uline) {
415 printf("u");
416 } else {
417 printf(" ");
418 }
419 if (t_attrib->blink) {
420 printf("l");
421 } else {
422 printf(" ");
423 }
424 if (t_attrib->invers) {
425 printf("i");
426 } else {
427 printf(" ");
428 }
429 if (t_attrib->unvisible) {
430 printf("n");
431 } else {
432 printf(" ");
433 }
434
435 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib->fgcol, t_attrib->bgcol, ch, ch);
436}
437#endif
bellarde7f0ad52004-07-14 17:28:59 +0000438
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100439static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
pbrook6d6f7c22006-03-11 15:35:30 +0000440 TextAttributes *t_attrib)
bellarde7f0ad52004-07-14 17:28:59 +0000441{
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100442 static pixman_image_t *glyphs[256];
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100443 DisplaySurface *surface = qemu_console_surface(s);
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100444 pixman_color_t fgcol, bgcol;
pbrook6d6f7c22006-03-11 15:35:30 +0000445
446 if (t_attrib->invers) {
Gerd Hoffmanncf6f0542013-03-06 09:50:51 +0100447 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
448 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
pbrook6d6f7c22006-03-11 15:35:30 +0000449 } else {
Gerd Hoffmanncf6f0542013-03-06 09:50:51 +0100450 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
451 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
pbrook6d6f7c22006-03-11 15:35:30 +0000452 }
bellarde7f0ad52004-07-14 17:28:59 +0000453
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100454 if (!glyphs[ch]) {
455 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
bellarde7f0ad52004-07-14 17:28:59 +0000456 }
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100457 qemu_pixman_glyph_render(glyphs[ch], surface->image,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100458 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
bellarde7f0ad52004-07-14 17:28:59 +0000459}
460
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200461static void text_console_resize(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000462{
463 TextCell *cells, *c, *c1;
464 int w1, x, y, last_width;
465
466 last_width = s->width;
Gerd Hoffmann36671fb2013-03-13 10:14:52 +0100467 s->width = surface_width(s->surface) / FONT_WIDTH;
468 s->height = surface_height(s->surface) / FONT_HEIGHT;
bellarde7f0ad52004-07-14 17:28:59 +0000469
470 w1 = last_width;
471 if (s->width < w1)
472 w1 = s->width;
473
Anthony Liguori7267c092011-08-20 22:09:37 -0500474 cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
bellarde7f0ad52004-07-14 17:28:59 +0000475 for(y = 0; y < s->total_height; y++) {
476 c = &cells[y * s->width];
477 if (w1 > 0) {
478 c1 = &s->cells[y * last_width];
479 for(x = 0; x < w1; x++) {
480 *c++ = *c1++;
481 }
482 }
483 for(x = w1; x < s->width; x++) {
484 c->ch = ' ';
pbrook6d6f7c22006-03-11 15:35:30 +0000485 c->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +0000486 c++;
487 }
488 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500489 g_free(s->cells);
bellarde7f0ad52004-07-14 17:28:59 +0000490 s->cells = cells;
491}
492
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200493static inline void text_update_xy(QemuConsole *s, int x, int y)
balrog4d3b6f62008-02-10 16:33:14 +0000494{
495 s->text_x[0] = MIN(s->text_x[0], x);
496 s->text_x[1] = MAX(s->text_x[1], x);
497 s->text_y[0] = MIN(s->text_y[0], y);
498 s->text_y[1] = MAX(s->text_y[1], y);
499}
500
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200501static void invalidate_xy(QemuConsole *s, int x, int y)
pbrook14778c22009-01-21 03:02:52 +0000502{
503 if (s->update_x0 > x * FONT_WIDTH)
504 s->update_x0 = x * FONT_WIDTH;
505 if (s->update_y0 > y * FONT_HEIGHT)
506 s->update_y0 = y * FONT_HEIGHT;
507 if (s->update_x1 < (x + 1) * FONT_WIDTH)
508 s->update_x1 = (x + 1) * FONT_WIDTH;
509 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
510 s->update_y1 = (y + 1) * FONT_HEIGHT;
511}
512
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200513static void update_xy(QemuConsole *s, int x, int y)
bellarde7f0ad52004-07-14 17:28:59 +0000514{
515 TextCell *c;
516 int y1, y2;
517
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100518 if (s != active_console) {
519 return;
520 }
balrog4d3b6f62008-02-10 16:33:14 +0000521
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100522 if (s->ds->have_text) {
523 text_update_xy(s, x, y);
524 }
525
526 if (s->ds->have_gfx) {
bellarde7f0ad52004-07-14 17:28:59 +0000527 y1 = (s->y_base + y) % s->total_height;
528 y2 = y1 - s->y_displayed;
529 if (y2 < 0)
530 y2 += s->total_height;
531 if (y2 < s->height) {
532 c = &s->cells[y1 * s->width + x];
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100533 vga_putcharxy(s, x, y2, c->ch,
pbrook6d6f7c22006-03-11 15:35:30 +0000534 &(c->t_attrib));
pbrook14778c22009-01-21 03:02:52 +0000535 invalidate_xy(s, x, y2);
bellarde7f0ad52004-07-14 17:28:59 +0000536 }
537 }
538}
539
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200540static void console_show_cursor(QemuConsole *s, int show)
bellarde7f0ad52004-07-14 17:28:59 +0000541{
542 TextCell *c;
543 int y, y1;
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100544 int x = s->x;
bellarde7f0ad52004-07-14 17:28:59 +0000545
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100546 if (s != active_console) {
547 return;
548 }
balrog4d3b6f62008-02-10 16:33:14 +0000549
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100550 if (s->ds->have_text) {
551 s->cursor_invalidate = 1;
552 }
balrog4d3b6f62008-02-10 16:33:14 +0000553
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100554 if (s->ds->have_gfx) {
thsed8276a2007-02-10 22:37:56 +0000555 if (x >= s->width) {
556 x = s->width - 1;
557 }
bellarde7f0ad52004-07-14 17:28:59 +0000558 y1 = (s->y_base + s->y) % s->total_height;
559 y = y1 - s->y_displayed;
560 if (y < 0)
561 y += s->total_height;
562 if (y < s->height) {
thsed8276a2007-02-10 22:37:56 +0000563 c = &s->cells[y1 * s->width + x];
Jan Kiszkabf1bed82012-07-10 22:00:55 +0200564 if (show && s->cursor_visible_phase) {
pbrook6d6f7c22006-03-11 15:35:30 +0000565 TextAttributes t_attrib = s->t_attrib_default;
566 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100567 vga_putcharxy(s, x, y, c->ch, &t_attrib);
bellarde7f0ad52004-07-14 17:28:59 +0000568 } else {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100569 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
bellarde7f0ad52004-07-14 17:28:59 +0000570 }
pbrook14778c22009-01-21 03:02:52 +0000571 invalidate_xy(s, x, y);
bellarde7f0ad52004-07-14 17:28:59 +0000572 }
573 }
574}
575
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200576static void console_refresh(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000577{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100578 DisplaySurface *surface = qemu_console_surface(s);
bellarde7f0ad52004-07-14 17:28:59 +0000579 TextCell *c;
580 int x, y, y1;
581
ths5fafdf22007-09-16 21:08:06 +0000582 if (s != active_console)
bellarde7f0ad52004-07-14 17:28:59 +0000583 return;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200584
585 if (s->ds->have_text) {
balrog4d3b6f62008-02-10 16:33:14 +0000586 s->text_x[0] = 0;
587 s->text_y[0] = 0;
588 s->text_x[1] = s->width - 1;
589 s->text_y[1] = s->height - 1;
590 s->cursor_invalidate = 1;
balrog4d3b6f62008-02-10 16:33:14 +0000591 }
bellarde7f0ad52004-07-14 17:28:59 +0000592
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200593 if (s->ds->have_gfx) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100594 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
Gerd Hoffmanncf6f0542013-03-06 09:50:51 +0100595 color_table_rgb[0][COLOR_BLACK]);
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200596 y1 = s->y_displayed;
597 for (y = 0; y < s->height; y++) {
598 c = s->cells + y1 * s->width;
599 for (x = 0; x < s->width; x++) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100600 vga_putcharxy(s, x, y, c->ch,
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200601 &(c->t_attrib));
602 c++;
603 }
604 if (++y1 == s->total_height) {
605 y1 = 0;
606 }
bellarde7f0ad52004-07-14 17:28:59 +0000607 }
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200608 console_show_cursor(s, 1);
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100609 dpy_gfx_update(s, 0, 0,
610 surface_width(surface), surface_height(surface));
bellarde7f0ad52004-07-14 17:28:59 +0000611 }
bellarde7f0ad52004-07-14 17:28:59 +0000612}
613
614static void console_scroll(int ydelta)
615{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200616 QemuConsole *s;
bellarde7f0ad52004-07-14 17:28:59 +0000617 int i, y1;
ths3b46e622007-09-17 08:09:54 +0000618
bellarde7f0ad52004-07-14 17:28:59 +0000619 s = active_console;
thsaf3a9032007-07-11 23:14:59 +0000620 if (!s || (s->console_type == GRAPHIC_CONSOLE))
bellarde7f0ad52004-07-14 17:28:59 +0000621 return;
622
623 if (ydelta > 0) {
624 for(i = 0; i < ydelta; i++) {
625 if (s->y_displayed == s->y_base)
626 break;
627 if (++s->y_displayed == s->total_height)
628 s->y_displayed = 0;
629 }
630 } else {
631 ydelta = -ydelta;
632 i = s->backscroll_height;
633 if (i > s->total_height - s->height)
634 i = s->total_height - s->height;
635 y1 = s->y_base - i;
636 if (y1 < 0)
637 y1 += s->total_height;
638 for(i = 0; i < ydelta; i++) {
639 if (s->y_displayed == y1)
640 break;
641 if (--s->y_displayed < 0)
642 s->y_displayed = s->total_height - 1;
643 }
644 }
645 console_refresh(s);
646}
647
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200648static void console_put_lf(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000649{
650 TextCell *c;
651 int x, y1;
652
bellarde7f0ad52004-07-14 17:28:59 +0000653 s->y++;
654 if (s->y >= s->height) {
655 s->y = s->height - 1;
pbrook6d6f7c22006-03-11 15:35:30 +0000656
bellarde7f0ad52004-07-14 17:28:59 +0000657 if (s->y_displayed == s->y_base) {
658 if (++s->y_displayed == s->total_height)
659 s->y_displayed = 0;
660 }
661 if (++s->y_base == s->total_height)
662 s->y_base = 0;
663 if (s->backscroll_height < s->total_height)
664 s->backscroll_height++;
665 y1 = (s->y_base + s->height - 1) % s->total_height;
666 c = &s->cells[y1 * s->width];
667 for(x = 0; x < s->width; x++) {
668 c->ch = ' ';
pbrook6d6f7c22006-03-11 15:35:30 +0000669 c->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +0000670 c++;
671 }
672 if (s == active_console && s->y_displayed == s->y_base) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100673 if (s->ds->have_text) {
balrog4d3b6f62008-02-10 16:33:14 +0000674 s->text_x[0] = 0;
675 s->text_y[0] = 0;
676 s->text_x[1] = s->width - 1;
677 s->text_y[1] = s->height - 1;
balrog4d3b6f62008-02-10 16:33:14 +0000678 }
679
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100680 if (s->ds->have_gfx) {
681 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
682 s->width * FONT_WIDTH,
683 (s->height - 1) * FONT_HEIGHT);
684 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
685 s->width * FONT_WIDTH, FONT_HEIGHT,
686 color_table_rgb[0][s->t_attrib_default.bgcol]);
687 s->update_x0 = 0;
688 s->update_y0 = 0;
689 s->update_x1 = s->width * FONT_WIDTH;
690 s->update_y1 = s->height * FONT_HEIGHT;
691 }
bellarde7f0ad52004-07-14 17:28:59 +0000692 }
693 }
694}
695
pbrook6d6f7c22006-03-11 15:35:30 +0000696/* Set console attributes depending on the current escape codes.
697 * NOTE: I know this code is not very efficient (checking every color for it
698 * self) but it is more readable and better maintainable.
699 */
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200700static void console_handle_escape(QemuConsole *s)
pbrook6d6f7c22006-03-11 15:35:30 +0000701{
702 int i;
703
pbrook6d6f7c22006-03-11 15:35:30 +0000704 for (i=0; i<s->nb_esc_params; i++) {
705 switch (s->esc_params[i]) {
706 case 0: /* reset all console attributes to default */
707 s->t_attrib = s->t_attrib_default;
708 break;
709 case 1:
710 s->t_attrib.bold = 1;
711 break;
712 case 4:
713 s->t_attrib.uline = 1;
714 break;
715 case 5:
716 s->t_attrib.blink = 1;
717 break;
718 case 7:
719 s->t_attrib.invers = 1;
720 break;
721 case 8:
722 s->t_attrib.unvisible = 1;
723 break;
724 case 22:
725 s->t_attrib.bold = 0;
726 break;
727 case 24:
728 s->t_attrib.uline = 0;
729 break;
730 case 25:
731 s->t_attrib.blink = 0;
732 break;
733 case 27:
734 s->t_attrib.invers = 0;
735 break;
736 case 28:
737 s->t_attrib.unvisible = 0;
738 break;
739 /* set foreground color */
740 case 30:
741 s->t_attrib.fgcol=COLOR_BLACK;
742 break;
743 case 31:
744 s->t_attrib.fgcol=COLOR_RED;
745 break;
746 case 32:
747 s->t_attrib.fgcol=COLOR_GREEN;
748 break;
749 case 33:
750 s->t_attrib.fgcol=COLOR_YELLOW;
751 break;
752 case 34:
753 s->t_attrib.fgcol=COLOR_BLUE;
754 break;
755 case 35:
756 s->t_attrib.fgcol=COLOR_MAGENTA;
757 break;
758 case 36:
759 s->t_attrib.fgcol=COLOR_CYAN;
760 break;
761 case 37:
762 s->t_attrib.fgcol=COLOR_WHITE;
763 break;
764 /* set background color */
765 case 40:
766 s->t_attrib.bgcol=COLOR_BLACK;
767 break;
768 case 41:
769 s->t_attrib.bgcol=COLOR_RED;
770 break;
771 case 42:
772 s->t_attrib.bgcol=COLOR_GREEN;
773 break;
774 case 43:
775 s->t_attrib.bgcol=COLOR_YELLOW;
776 break;
777 case 44:
778 s->t_attrib.bgcol=COLOR_BLUE;
779 break;
780 case 45:
781 s->t_attrib.bgcol=COLOR_MAGENTA;
782 break;
783 case 46:
784 s->t_attrib.bgcol=COLOR_CYAN;
785 break;
786 case 47:
787 s->t_attrib.bgcol=COLOR_WHITE;
788 break;
789 }
790 }
791}
792
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200793static void console_clear_xy(QemuConsole *s, int x, int y)
thsadb47962007-01-16 23:02:36 +0000794{
795 int y1 = (s->y_base + y) % s->total_height;
796 TextCell *c = &s->cells[y1 * s->width + x];
797 c->ch = ' ';
798 c->t_attrib = s->t_attrib_default;
thsadb47962007-01-16 23:02:36 +0000799 update_xy(s, x, y);
800}
801
Ian Campbell3eea5492012-09-04 10:26:09 -0500802/* set cursor, checking bounds */
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200803static void set_cursor(QemuConsole *s, int x, int y)
Ian Campbell3eea5492012-09-04 10:26:09 -0500804{
805 if (x < 0) {
806 x = 0;
807 }
808 if (y < 0) {
809 y = 0;
810 }
811 if (y >= s->height) {
812 y = s->height - 1;
813 }
814 if (x >= s->width) {
815 x = s->width - 1;
816 }
817
818 s->x = x;
819 s->y = y;
820}
821
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200822static void console_putchar(QemuConsole *s, int ch)
bellarde7f0ad52004-07-14 17:28:59 +0000823{
824 TextCell *c;
thsadb47962007-01-16 23:02:36 +0000825 int y1, i;
826 int x, y;
bellarde7f0ad52004-07-14 17:28:59 +0000827
828 switch(s->state) {
829 case TTY_STATE_NORM:
830 switch(ch) {
pbrook6d6f7c22006-03-11 15:35:30 +0000831 case '\r': /* carriage return */
bellarde7f0ad52004-07-14 17:28:59 +0000832 s->x = 0;
833 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000834 case '\n': /* newline */
bellarde7f0ad52004-07-14 17:28:59 +0000835 console_put_lf(s);
836 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000837 case '\b': /* backspace */
ths5fafdf22007-09-16 21:08:06 +0000838 if (s->x > 0)
bellarde15d7372006-06-25 16:26:29 +0000839 s->x--;
pbrook6d6f7c22006-03-11 15:35:30 +0000840 break;
841 case '\t': /* tabspace */
842 if (s->x + (8 - (s->x % 8)) > s->width) {
bellardbd468842006-07-14 20:24:31 +0000843 s->x = 0;
pbrook6d6f7c22006-03-11 15:35:30 +0000844 console_put_lf(s);
845 } else {
846 s->x = s->x + (8 - (s->x % 8));
847 }
848 break;
849 case '\a': /* alert aka. bell */
850 /* TODO: has to be implemented */
851 break;
thsadb47962007-01-16 23:02:36 +0000852 case 14:
853 /* SI (shift in), character set 0 (ignored) */
854 break;
855 case 15:
856 /* SO (shift out), character set 1 (ignored) */
857 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000858 case 27: /* esc (introducing an escape sequence) */
bellarde7f0ad52004-07-14 17:28:59 +0000859 s->state = TTY_STATE_ESC;
860 break;
861 default:
thsed8276a2007-02-10 22:37:56 +0000862 if (s->x >= s->width) {
863 /* line wrap */
864 s->x = 0;
865 console_put_lf(s);
thsadb47962007-01-16 23:02:36 +0000866 }
bellarde7f0ad52004-07-14 17:28:59 +0000867 y1 = (s->y_base + s->y) % s->total_height;
868 c = &s->cells[y1 * s->width + s->x];
869 c->ch = ch;
pbrook6d6f7c22006-03-11 15:35:30 +0000870 c->t_attrib = s->t_attrib;
bellarde7f0ad52004-07-14 17:28:59 +0000871 update_xy(s, s->x, s->y);
872 s->x++;
bellarde7f0ad52004-07-14 17:28:59 +0000873 break;
874 }
875 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000876 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
bellarde7f0ad52004-07-14 17:28:59 +0000877 if (ch == '[') {
878 for(i=0;i<MAX_ESC_PARAMS;i++)
879 s->esc_params[i] = 0;
880 s->nb_esc_params = 0;
881 s->state = TTY_STATE_CSI;
882 } else {
883 s->state = TTY_STATE_NORM;
884 }
885 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000886 case TTY_STATE_CSI: /* handle escape sequence parameters */
bellarde7f0ad52004-07-14 17:28:59 +0000887 if (ch >= '0' && ch <= '9') {
888 if (s->nb_esc_params < MAX_ESC_PARAMS) {
Laszlo Ersekc10600a2012-09-17 11:10:03 +0200889 int *param = &s->esc_params[s->nb_esc_params];
890 int digit = (ch - '0');
891
892 *param = (*param <= (INT_MAX - digit) / 10) ?
893 *param * 10 + digit : INT_MAX;
bellarde7f0ad52004-07-14 17:28:59 +0000894 }
895 } else {
Ian Campbell3eea5492012-09-04 10:26:09 -0500896 if (s->nb_esc_params < MAX_ESC_PARAMS)
897 s->nb_esc_params++;
bellarde7f0ad52004-07-14 17:28:59 +0000898 if (ch == ';')
899 break;
thsadb47962007-01-16 23:02:36 +0000900#ifdef DEBUG_CONSOLE
901 fprintf(stderr, "escape sequence CSI%d;%d%c, %d parameters\n",
902 s->esc_params[0], s->esc_params[1], ch, s->nb_esc_params);
903#endif
bellarde7f0ad52004-07-14 17:28:59 +0000904 s->state = TTY_STATE_NORM;
905 switch(ch) {
thsadb47962007-01-16 23:02:36 +0000906 case 'A':
907 /* move cursor up */
908 if (s->esc_params[0] == 0) {
909 s->esc_params[0] = 1;
910 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500911 set_cursor(s, s->x, s->y - s->esc_params[0]);
bellarde7f0ad52004-07-14 17:28:59 +0000912 break;
thsadb47962007-01-16 23:02:36 +0000913 case 'B':
914 /* move cursor down */
915 if (s->esc_params[0] == 0) {
916 s->esc_params[0] = 1;
917 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500918 set_cursor(s, s->x, s->y + s->esc_params[0]);
thsadb47962007-01-16 23:02:36 +0000919 break;
920 case 'C':
921 /* move cursor right */
922 if (s->esc_params[0] == 0) {
923 s->esc_params[0] = 1;
924 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500925 set_cursor(s, s->x + s->esc_params[0], s->y);
thsadb47962007-01-16 23:02:36 +0000926 break;
927 case 'D':
928 /* move cursor left */
929 if (s->esc_params[0] == 0) {
930 s->esc_params[0] = 1;
931 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500932 set_cursor(s, s->x - s->esc_params[0], s->y);
thsadb47962007-01-16 23:02:36 +0000933 break;
934 case 'G':
935 /* move cursor to column */
Ian Campbell3eea5492012-09-04 10:26:09 -0500936 set_cursor(s, s->esc_params[0] - 1, s->y);
thsadb47962007-01-16 23:02:36 +0000937 break;
938 case 'f':
939 case 'H':
940 /* move cursor to row, column */
Ian Campbell3eea5492012-09-04 10:26:09 -0500941 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
thsadb47962007-01-16 23:02:36 +0000942 break;
943 case 'J':
944 switch (s->esc_params[0]) {
945 case 0:
946 /* clear to end of screen */
947 for (y = s->y; y < s->height; y++) {
948 for (x = 0; x < s->width; x++) {
949 if (y == s->y && x < s->x) {
950 continue;
951 }
952 console_clear_xy(s, x, y);
953 }
954 }
955 break;
956 case 1:
957 /* clear from beginning of screen */
958 for (y = 0; y <= s->y; y++) {
959 for (x = 0; x < s->width; x++) {
960 if (y == s->y && x > s->x) {
961 break;
962 }
963 console_clear_xy(s, x, y);
964 }
965 }
966 break;
967 case 2:
968 /* clear entire screen */
969 for (y = 0; y <= s->height; y++) {
970 for (x = 0; x < s->width; x++) {
971 console_clear_xy(s, x, y);
972 }
973 }
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100974 break;
thsadb47962007-01-16 23:02:36 +0000975 }
Markus Armbruster95d8f9f2011-11-22 11:59:07 +0100976 break;
thsadb47962007-01-16 23:02:36 +0000977 case 'K':
978 switch (s->esc_params[0]) {
979 case 0:
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100980 /* clear to eol */
981 for(x = s->x; x < s->width; x++) {
thsadb47962007-01-16 23:02:36 +0000982 console_clear_xy(s, x, s->y);
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100983 }
984 break;
thsadb47962007-01-16 23:02:36 +0000985 case 1:
986 /* clear from beginning of line */
987 for (x = 0; x <= s->x; x++) {
988 console_clear_xy(s, x, s->y);
989 }
990 break;
991 case 2:
992 /* clear entire line */
993 for(x = 0; x < s->width; x++) {
994 console_clear_xy(s, x, s->y);
995 }
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100996 break;
997 }
thsadb47962007-01-16 23:02:36 +0000998 break;
999 case 'm':
Markus Armbrusterf94a9502011-11-22 11:59:06 +01001000 console_handle_escape(s);
1001 break;
thsadb47962007-01-16 23:02:36 +00001002 case 'n':
1003 /* report cursor position */
1004 /* TODO: send ESC[row;colR */
1005 break;
1006 case 's':
1007 /* save cursor position */
1008 s->x_saved = s->x;
1009 s->y_saved = s->y;
1010 break;
1011 case 'u':
1012 /* restore cursor position */
1013 s->x = s->x_saved;
1014 s->y = s->y_saved;
1015 break;
1016 default:
1017#ifdef DEBUG_CONSOLE
1018 fprintf(stderr, "unhandled escape character '%c'\n", ch);
1019#endif
1020 break;
1021 }
1022 break;
bellarde7f0ad52004-07-14 17:28:59 +00001023 }
1024 }
1025}
1026
1027void console_select(unsigned int index)
1028{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001029 QemuConsole *s;
pbrook6d6f7c22006-03-11 15:35:30 +00001030
bellarde7f0ad52004-07-14 17:28:59 +00001031 if (index >= MAX_CONSOLES)
1032 return;
Gerd Hoffmann437fe102013-03-07 16:04:52 +01001033
1034 trace_console_select(index);
bellarde7f0ad52004-07-14 17:28:59 +00001035 s = consoles[index];
1036 if (s) {
aliguori7d957bd2009-01-15 22:14:11 +00001037 DisplayState *ds = s->ds;
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001038
Stefan Weil8bd6b062012-08-17 15:50:44 +02001039 if (active_console && active_console->cursor_timer) {
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001040 qemu_del_timer(active_console->cursor_timer);
1041 }
bellarde7f0ad52004-07-14 17:28:59 +00001042 active_console = s;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001043 if (ds->have_gfx) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001044 dpy_gfx_switch_surface(ds, s->surface);
1045 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1046 surface_height(s->surface));
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001047 }
1048 if (ds->have_text) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001049 dpy_text_resize(s, s->width, s->height);
aliguori68f00992009-01-21 18:59:12 +00001050 }
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001051 if (s->cursor_timer) {
1052 qemu_mod_timer(s->cursor_timer,
1053 qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
1054 }
bellarde7f0ad52004-07-14 17:28:59 +00001055 }
1056}
1057
1058static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1059{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001060 QemuConsole *s = chr->opaque;
bellarde7f0ad52004-07-14 17:28:59 +00001061 int i;
1062
pbrook14778c22009-01-21 03:02:52 +00001063 s->update_x0 = s->width * FONT_WIDTH;
1064 s->update_y0 = s->height * FONT_HEIGHT;
1065 s->update_x1 = 0;
1066 s->update_y1 = 0;
bellarde7f0ad52004-07-14 17:28:59 +00001067 console_show_cursor(s, 0);
1068 for(i = 0; i < len; i++) {
1069 console_putchar(s, buf[i]);
1070 }
1071 console_show_cursor(s, 1);
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001072 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001073 dpy_gfx_update(s, s->update_x0, s->update_y0,
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001074 s->update_x1 - s->update_x0,
1075 s->update_y1 - s->update_y0);
pbrook14778c22009-01-21 03:02:52 +00001076 }
bellarde7f0ad52004-07-14 17:28:59 +00001077 return len;
1078}
1079
bellarde15d7372006-06-25 16:26:29 +00001080static void kbd_send_chars(void *opaque)
1081{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001082 QemuConsole *s = opaque;
bellarde15d7372006-06-25 16:26:29 +00001083 int len;
1084 uint8_t buf[16];
ths3b46e622007-09-17 08:09:54 +00001085
Anthony Liguori909cda12011-08-15 11:17:31 -05001086 len = qemu_chr_be_can_write(s->chr);
bellarde15d7372006-06-25 16:26:29 +00001087 if (len > s->out_fifo.count)
1088 len = s->out_fifo.count;
1089 if (len > 0) {
1090 if (len > sizeof(buf))
1091 len = sizeof(buf);
1092 qemu_fifo_read(&s->out_fifo, buf, len);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001093 qemu_chr_be_write(s->chr, buf, len);
bellarde15d7372006-06-25 16:26:29 +00001094 }
1095 /* characters are pending: we send them a bit later (XXX:
1096 horrible, should change char device API) */
1097 if (s->out_fifo.count > 0) {
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001098 qemu_mod_timer(s->kbd_timer, qemu_get_clock_ms(rt_clock) + 1);
bellarde15d7372006-06-25 16:26:29 +00001099 }
1100}
1101
bellarde7f0ad52004-07-14 17:28:59 +00001102/* called when an ascii key is pressed */
1103void kbd_put_keysym(int keysym)
1104{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001105 QemuConsole *s;
bellarde7f0ad52004-07-14 17:28:59 +00001106 uint8_t buf[16], *q;
1107 int c;
1108
1109 s = active_console;
thsaf3a9032007-07-11 23:14:59 +00001110 if (!s || (s->console_type == GRAPHIC_CONSOLE))
bellarde7f0ad52004-07-14 17:28:59 +00001111 return;
1112
1113 switch(keysym) {
1114 case QEMU_KEY_CTRL_UP:
1115 console_scroll(-1);
1116 break;
1117 case QEMU_KEY_CTRL_DOWN:
1118 console_scroll(1);
1119 break;
1120 case QEMU_KEY_CTRL_PAGEUP:
1121 console_scroll(-10);
1122 break;
1123 case QEMU_KEY_CTRL_PAGEDOWN:
1124 console_scroll(10);
1125 break;
1126 default:
bellarde15d7372006-06-25 16:26:29 +00001127 /* convert the QEMU keysym to VT100 key string */
1128 q = buf;
1129 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1130 *q++ = '\033';
1131 *q++ = '[';
1132 c = keysym - 0xe100;
1133 if (c >= 10)
1134 *q++ = '0' + (c / 10);
1135 *q++ = '0' + (c % 10);
1136 *q++ = '~';
1137 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1138 *q++ = '\033';
1139 *q++ = '[';
1140 *q++ = keysym & 0xff;
Paolo Bonzini41048332010-12-23 13:42:52 +01001141 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1142 console_puts(s->chr, (const uint8_t *) "\r", 1);
1143 *q++ = '\n';
bellarde15d7372006-06-25 16:26:29 +00001144 } else {
Paolo Bonzini41048332010-12-23 13:42:52 +01001145 *q++ = keysym;
1146 }
1147 if (s->echo) {
1148 console_puts(s->chr, buf, q - buf);
bellarde15d7372006-06-25 16:26:29 +00001149 }
pbrooke5b0bc42007-01-27 23:46:43 +00001150 if (s->chr->chr_read) {
bellarde15d7372006-06-25 16:26:29 +00001151 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1152 kbd_send_chars(s);
bellarde7f0ad52004-07-14 17:28:59 +00001153 }
1154 break;
1155 }
1156}
1157
balrog4d3b6f62008-02-10 16:33:14 +00001158static void text_console_invalidate(void *opaque)
1159{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001160 QemuConsole *s = (QemuConsole *) opaque;
Gerd Hoffmann1562e532013-03-06 13:40:47 +01001161
1162 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
aliguori68f00992009-01-21 18:59:12 +00001163 text_console_resize(s);
1164 }
balrog4d3b6f62008-02-10 16:33:14 +00001165 console_refresh(s);
1166}
1167
Anthony Liguoric227f092009-10-01 16:12:16 -05001168static void text_console_update(void *opaque, console_ch_t *chardata)
balrog4d3b6f62008-02-10 16:33:14 +00001169{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001170 QemuConsole *s = (QemuConsole *) opaque;
balrog4d3b6f62008-02-10 16:33:14 +00001171 int i, j, src;
1172
1173 if (s->text_x[0] <= s->text_x[1]) {
1174 src = (s->y_base + s->text_y[0]) * s->width;
1175 chardata += s->text_y[0] * s->width;
1176 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1177 for (j = 0; j < s->width; j ++, src ++)
1178 console_write_ch(chardata ++, s->cells[src].ch |
1179 (s->cells[src].t_attrib.fgcol << 12) |
1180 (s->cells[src].t_attrib.bgcol << 8) |
1181 (s->cells[src].t_attrib.bold << 21));
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001182 dpy_text_update(s, s->text_x[0], s->text_y[0],
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001183 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
balrog4d3b6f62008-02-10 16:33:14 +00001184 s->text_x[0] = s->width;
1185 s->text_y[0] = s->height;
1186 s->text_x[1] = 0;
1187 s->text_y[1] = 0;
1188 }
1189 if (s->cursor_invalidate) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001190 dpy_text_cursor(s, s->x, s->y);
balrog4d3b6f62008-02-10 16:33:14 +00001191 s->cursor_invalidate = 0;
1192 }
1193}
1194
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001195static QemuConsole *new_console(DisplayState *ds, console_type_t console_type)
bellarde7f0ad52004-07-14 17:28:59 +00001196{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001197 QemuConsole *s;
pbrook95219892006-04-09 01:06:34 +00001198 int i;
bellarde7f0ad52004-07-14 17:28:59 +00001199
1200 if (nb_consoles >= MAX_CONSOLES)
1201 return NULL;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001202 s = g_malloc0(sizeof(QemuConsole));
thsaf3a9032007-07-11 23:14:59 +00001203 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1204 (console_type == GRAPHIC_CONSOLE))) {
bellarde7f0ad52004-07-14 17:28:59 +00001205 active_console = s;
thsaf3a9032007-07-11 23:14:59 +00001206 }
bellarde7f0ad52004-07-14 17:28:59 +00001207 s->ds = ds;
thsaf3a9032007-07-11 23:14:59 +00001208 s->console_type = console_type;
1209 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 Hoffmannda229ef2013-02-28 10:48:02 +01001286void qemu_free_displaysurface(DisplaySurface *surface)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001287{
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001288 if (surface == NULL) {
Paolo Bonzini98b50082010-02-11 00:29:57 +01001289 return;
Gerd Hoffmann187cd1d2012-09-26 07:46:20 +02001290 }
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001291 trace_displaysurface_free(surface);
1292 qemu_pixman_image_unref(surface->image);
1293 g_free(surface);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001294}
1295
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001296void register_displaychangelistener(DisplayState *ds,
1297 DisplayChangeListener *dcl)
1298{
1299 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1300 dcl->ds = ds;
1301 QLIST_INSERT_HEAD(&ds->listeners, dcl, next);
1302 gui_setup_refresh(ds);
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001303 if (dcl->ops->dpy_gfx_switch && active_console) {
1304 dcl->ops->dpy_gfx_switch(dcl, active_console->surface);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001305 }
1306}
1307
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +01001308void update_displaychangelistener(DisplayChangeListener *dcl,
1309 uint64_t interval)
1310{
1311 DisplayState *ds = dcl->ds;
1312
1313 dcl->update_interval = interval;
1314 if (!ds->refreshing && ds->update_interval > interval) {
1315 qemu_mod_timer(ds->gui_timer, ds->last_update + interval);
1316 }
1317}
1318
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001319void unregister_displaychangelistener(DisplayChangeListener *dcl)
1320{
1321 DisplayState *ds = dcl->ds;
1322 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1323 QLIST_REMOVE(dcl, next);
1324 gui_setup_refresh(ds);
1325}
1326
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001327void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001328{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001329 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001330 struct DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001331 int width = surface_width(con->surface);
1332 int height = surface_height(con->surface);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001333
1334 x = MAX(x, 0);
1335 y = MAX(y, 0);
1336 x = MIN(x, width);
1337 y = MIN(y, height);
1338 w = MIN(w, width - x);
1339 h = MIN(h, height - y);
1340
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001341 if (con != active_console) {
1342 return;
1343 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001344 QLIST_FOREACH(dcl, &s->listeners, next) {
1345 if (dcl->ops->dpy_gfx_update) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001346 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001347 }
1348 }
1349}
1350
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001351static void dpy_gfx_switch_surface(DisplayState *ds,
1352 DisplaySurface *surface)
1353{
1354 struct DisplayChangeListener *dcl;
1355
1356 QLIST_FOREACH(dcl, &ds->listeners, next) {
1357 if (dcl->ops->dpy_gfx_switch) {
1358 dcl->ops->dpy_gfx_switch(dcl, surface);
1359 }
1360 }
1361}
1362
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001363void dpy_gfx_replace_surface(QemuConsole *con,
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001364 DisplaySurface *surface)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001365{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001366 DisplayState *s = con->ds;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001367 DisplaySurface *old_surface = con->surface;
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001368
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001369 con->surface = surface;
1370 if (con == active_console) {
1371 dpy_gfx_switch_surface(s, surface);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001372 }
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001373 qemu_free_displaysurface(old_surface);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001374}
1375
1376void dpy_refresh(DisplayState *s)
1377{
1378 struct DisplayChangeListener *dcl;
1379 QLIST_FOREACH(dcl, &s->listeners, next) {
1380 if (dcl->ops->dpy_refresh) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001381 dcl->ops->dpy_refresh(dcl);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001382 }
1383 }
1384}
1385
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001386void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1387 int dst_x, int dst_y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001388{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001389 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001390 struct DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001391
1392 if (con != active_console) {
1393 return;
1394 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001395 QLIST_FOREACH(dcl, &s->listeners, next) {
1396 if (dcl->ops->dpy_gfx_copy) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001397 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001398 } else { /* TODO */
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001399 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001400 }
1401 }
1402}
1403
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001404void dpy_text_cursor(QemuConsole *con, int x, int y)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001405{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001406 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001407 struct DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001408
1409 if (con != active_console) {
1410 return;
1411 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001412 QLIST_FOREACH(dcl, &s->listeners, next) {
1413 if (dcl->ops->dpy_text_cursor) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001414 dcl->ops->dpy_text_cursor(dcl, x, y);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001415 }
1416 }
1417}
1418
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001419void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001420{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001421 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001422 struct DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001423
1424 if (con != active_console) {
1425 return;
1426 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001427 QLIST_FOREACH(dcl, &s->listeners, next) {
1428 if (dcl->ops->dpy_text_update) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001429 dcl->ops->dpy_text_update(dcl, x, y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001430 }
1431 }
1432}
1433
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001434void dpy_text_resize(QemuConsole *con, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001435{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001436 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001437 struct DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001438
1439 if (con != active_console) {
1440 return;
1441 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001442 QLIST_FOREACH(dcl, &s->listeners, next) {
1443 if (dcl->ops->dpy_text_resize) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001444 dcl->ops->dpy_text_resize(dcl, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001445 }
1446 }
1447}
1448
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001449void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001450{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001451 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001452 struct DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001453
1454 if (con != active_console) {
1455 return;
1456 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001457 QLIST_FOREACH(dcl, &s->listeners, next) {
1458 if (dcl->ops->dpy_mouse_set) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001459 dcl->ops->dpy_mouse_set(dcl, x, y, on);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001460 }
1461 }
1462}
1463
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001464void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001465{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001466 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001467 struct DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001468
1469 if (con != active_console) {
1470 return;
1471 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001472 QLIST_FOREACH(dcl, &s->listeners, next) {
1473 if (dcl->ops->dpy_cursor_define) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001474 dcl->ops->dpy_cursor_define(dcl, cursor);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001475 }
1476 }
1477}
1478
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001479bool dpy_cursor_define_supported(QemuConsole *con)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001480{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001481 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001482 struct DisplayChangeListener *dcl;
1483 QLIST_FOREACH(dcl, &s->listeners, next) {
1484 if (dcl->ops->dpy_cursor_define) {
1485 return true;
1486 }
1487 }
1488 return false;
1489}
1490
Paolo Bonzini98b50082010-02-11 00:29:57 +01001491/***********************************************************/
1492/* register display */
1493
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001494/* console.c internal use only */
1495static DisplayState *get_alloc_displaystate(void)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001496{
1497 if (!display_state) {
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001498 display_state = g_new0(DisplayState, 1);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001499 }
1500 return display_state;
1501}
1502
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001503/*
1504 * Called by main(), after creating QemuConsoles
1505 * and before initializing ui (sdl/vnc/...).
1506 */
1507DisplayState *init_displaystate(void)
1508{
1509 int i;
1510
1511 if (!display_state) {
1512 display_state = g_new0(DisplayState, 1);
1513 }
1514
1515 for (i = 0; i < nb_consoles; i++) {
1516 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1517 consoles[i]->ds == NULL) {
1518 text_console_do_init(consoles[i]->chr, display_state);
1519 }
1520 }
1521
1522 return display_state;
1523}
1524
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001525QemuConsole *graphic_console_init(const GraphicHwOps *hw_ops,
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001526 void *opaque)
bellarde7f0ad52004-07-14 17:28:59 +00001527{
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001528 int width = 640;
1529 int height = 480;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001530 QemuConsole *s;
aliguori3023f332009-01-16 19:04:14 +00001531 DisplayState *ds;
aurel32f0f2f972009-01-16 21:13:49 +00001532
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001533 ds = get_alloc_displaystate();
Gerd Hoffmann437fe102013-03-07 16:04:52 +01001534 trace_console_gfx_new();
thsaf3a9032007-07-11 23:14:59 +00001535 s = new_console(ds, GRAPHIC_CONSOLE);
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001536 s->hw_ops = hw_ops;
pbrook95219892006-04-09 01:06:34 +00001537 s->hw = opaque;
aliguori3023f332009-01-16 19:04:14 +00001538
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001539 s->surface = qemu_create_displaysurface(width, height);
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001540 return s;
pbrook95219892006-04-09 01:06:34 +00001541}
1542
1543int is_graphic_console(void)
1544{
balrog4d3b6f62008-02-10 16:33:14 +00001545 return active_console && active_console->console_type == GRAPHIC_CONSOLE;
bellarde7f0ad52004-07-14 17:28:59 +00001546}
1547
balrogc21bbcf2008-09-24 03:32:33 +00001548int is_fixedsize_console(void)
1549{
1550 return active_console && active_console->console_type != TEXT_CONSOLE;
1551}
1552
Paolo Bonzini41048332010-12-23 13:42:52 +01001553static void text_console_set_echo(CharDriverState *chr, bool echo)
1554{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001555 QemuConsole *s = chr->opaque;
Paolo Bonzini41048332010-12-23 13:42:52 +01001556
1557 s->echo = echo;
1558}
1559
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001560static void text_console_update_cursor(void *opaque)
1561{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001562 QemuConsole *s = opaque;
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001563
1564 s->cursor_visible_phase = !s->cursor_visible_phase;
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +01001565 graphic_hw_invalidate(s);
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001566 qemu_mod_timer(s->cursor_timer,
1567 qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
1568}
1569
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001570static const GraphicHwOps text_console_ops = {
1571 .invalidate = text_console_invalidate,
1572 .text_update = text_console_update,
1573};
1574
Paolo Bonzini44b37b92010-12-23 13:42:53 +01001575static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
bellarde7f0ad52004-07-14 17:28:59 +00001576{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001577 QemuConsole *s;
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001578 int g_width = 80 * FONT_WIDTH;
1579 int g_height = 24 * FONT_HEIGHT;
pbrook6d6f7c22006-03-11 15:35:30 +00001580
Paolo Bonzini491e1142010-12-23 13:42:51 +01001581 s = chr->opaque;
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02001582
bellarde7f0ad52004-07-14 17:28:59 +00001583 chr->chr_write = console_puts;
bellard6fcfafb2004-08-01 21:48:30 +00001584
bellarde15d7372006-06-25 16:26:29 +00001585 s->out_fifo.buf = s->out_fifo_buf;
1586 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001587 s->kbd_timer = qemu_new_timer_ms(rt_clock, kbd_send_chars, s);
aliguori3023f332009-01-16 19:04:14 +00001588 s->ds = ds;
ths3b46e622007-09-17 08:09:54 +00001589
bellarde7f0ad52004-07-14 17:28:59 +00001590 s->y_displayed = 0;
1591 s->y_base = 0;
1592 s->total_height = DEFAULT_BACKSCROLL;
1593 s->x = 0;
1594 s->y = 0;
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001595 if (!s->surface) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001596 if (active_console && active_console->surface) {
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001597 g_width = surface_width(active_console->surface);
1598 g_height = surface_height(active_console->surface);
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001599 }
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001600 s->surface = qemu_create_displaysurface(g_width, g_height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001601 }
pbrook6d6f7c22006-03-11 15:35:30 +00001602
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001603 s->cursor_timer =
1604 qemu_new_timer_ms(rt_clock, text_console_update_cursor, s);
1605
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001606 s->hw_ops = &text_console_ops;
balrog4d3b6f62008-02-10 16:33:14 +00001607 s->hw = s;
1608
pbrook6d6f7c22006-03-11 15:35:30 +00001609 /* Set text attribute defaults */
1610 s->t_attrib_default.bold = 0;
1611 s->t_attrib_default.uline = 0;
1612 s->t_attrib_default.blink = 0;
1613 s->t_attrib_default.invers = 0;
1614 s->t_attrib_default.unvisible = 0;
1615 s->t_attrib_default.fgcol = COLOR_WHITE;
1616 s->t_attrib_default.bgcol = COLOR_BLACK;
pbrook6d6f7c22006-03-11 15:35:30 +00001617 /* set current text attributes to default */
1618 s->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +00001619 text_console_resize(s);
1620
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01001621 if (chr->label) {
1622 char msg[128];
1623 int len;
1624
Gerd Hoffmann735ba582009-12-08 13:11:40 +01001625 s->t_attrib.bgcol = COLOR_BLUE;
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01001626 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
1627 console_puts(chr, (uint8_t*)msg, len);
Gerd Hoffmann735ba582009-12-08 13:11:40 +01001628 s->t_attrib = s->t_attrib_default;
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01001629 }
1630
Hans de Goedefee204f2013-03-26 11:07:54 +01001631 qemu_chr_be_generic_open(chr);
aurel32ceecf1d2009-01-18 14:08:04 +00001632 if (chr->init)
1633 chr->init(chr);
bellarde7f0ad52004-07-14 17:28:59 +00001634}
pbrookc60e08d2008-07-01 16:24:38 +00001635
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001636static CharDriverState *text_console_init(ChardevVC *vc)
aliguori2796dae2009-01-16 20:23:27 +00001637{
1638 CharDriverState *chr;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001639 QemuConsole *s;
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001640 unsigned width = 0;
1641 unsigned height = 0;
aliguori2796dae2009-01-16 20:23:27 +00001642
Anthony Liguori7267c092011-08-20 22:09:37 -05001643 chr = g_malloc0(sizeof(CharDriverState));
aliguori2796dae2009-01-16 20:23:27 +00001644
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001645 if (vc->has_width) {
1646 width = vc->width;
1647 } else if (vc->has_cols) {
1648 width = vc->cols * FONT_WIDTH;
1649 }
Paolo Bonzini491e1142010-12-23 13:42:51 +01001650
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001651 if (vc->has_height) {
1652 height = vc->height;
1653 } else if (vc->has_rows) {
1654 height = vc->rows * FONT_HEIGHT;
1655 }
Paolo Bonzini491e1142010-12-23 13:42:51 +01001656
Gerd Hoffmann437fe102013-03-07 16:04:52 +01001657 trace_console_txt_new(width, height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001658 if (width == 0 || height == 0) {
1659 s = new_console(NULL, TEXT_CONSOLE);
1660 } else {
1661 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE);
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001662 s->surface = qemu_create_displaysurface(width, height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001663 }
1664
1665 if (!s) {
Stefan Weil5354d082011-10-02 18:53:09 +02001666 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001667 return NULL;
Paolo Bonzini491e1142010-12-23 13:42:51 +01001668 }
1669
1670 s->chr = chr;
Paolo Bonzini491e1142010-12-23 13:42:51 +01001671 chr->opaque = s;
Paolo Bonzini41048332010-12-23 13:42:52 +01001672 chr->chr_set_echo = text_console_set_echo;
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001673
1674 if (display_state) {
1675 text_console_do_init(chr, display_state);
1676 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001677 return chr;
aliguori2796dae2009-01-16 20:23:27 +00001678}
1679
Anthony Liguorid82831d2013-02-20 07:43:19 -06001680static VcHandler *vc_handler = text_console_init;
1681
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001682CharDriverState *vc_init(ChardevVC *vc)
Anthony Liguorid82831d2013-02-20 07:43:19 -06001683{
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001684 return vc_handler(vc);
Anthony Liguorid82831d2013-02-20 07:43:19 -06001685}
1686
1687void register_vc_handler(VcHandler *handler)
1688{
1689 vc_handler = handler;
1690}
1691
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001692void qemu_console_resize(QemuConsole *s, int width, int height)
pbrookc60e08d2008-07-01 16:24:38 +00001693{
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001694 DisplaySurface *surface;
1695
1696 assert(s->console_type == GRAPHIC_CONSOLE);
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001697 surface = qemu_create_displaysurface(width, height);
1698 dpy_gfx_replace_surface(s, surface);
pbrookc60e08d2008-07-01 16:24:38 +00001699}
balrog38334f72008-09-24 02:21:24 +00001700
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001701void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
aliguori3023f332009-01-16 19:04:14 +00001702 int dst_x, int dst_y, int w, int h)
balrogc21bbcf2008-09-24 03:32:33 +00001703{
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001704 assert(con->console_type == GRAPHIC_CONSOLE);
1705 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
balrog38334f72008-09-24 02:21:24 +00001706}
aliguori7d957bd2009-01-15 22:14:11 +00001707
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001708DisplaySurface *qemu_console_surface(QemuConsole *console)
1709{
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001710 return console->surface;
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001711}
1712
1713DisplayState *qemu_console_displaystate(QemuConsole *console)
1714{
1715 return console->ds;
1716}
1717
malc0da2ea12009-01-23 19:56:19 +00001718PixelFormat qemu_different_endianness_pixelformat(int bpp)
aliguori7d957bd2009-01-15 22:14:11 +00001719{
1720 PixelFormat pf;
1721
1722 memset(&pf, 0x00, sizeof(PixelFormat));
1723
1724 pf.bits_per_pixel = bpp;
BALATON Zoltanfeadf1a2012-08-22 17:19:42 +02001725 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
aliguori7d957bd2009-01-15 22:14:11 +00001726 pf.depth = bpp == 32 ? 24 : bpp;
1727
1728 switch (bpp) {
malc0da2ea12009-01-23 19:56:19 +00001729 case 24:
1730 pf.rmask = 0x000000FF;
1731 pf.gmask = 0x0000FF00;
1732 pf.bmask = 0x00FF0000;
1733 pf.rmax = 255;
1734 pf.gmax = 255;
1735 pf.bmax = 255;
1736 pf.rshift = 0;
1737 pf.gshift = 8;
1738 pf.bshift = 16;
aliguori90a1e3c2009-01-26 15:37:30 +00001739 pf.rbits = 8;
1740 pf.gbits = 8;
1741 pf.bbits = 8;
aliguori7d957bd2009-01-15 22:14:11 +00001742 break;
malc0da2ea12009-01-23 19:56:19 +00001743 case 32:
1744 pf.rmask = 0x0000FF00;
1745 pf.gmask = 0x00FF0000;
1746 pf.bmask = 0xFF000000;
1747 pf.amask = 0x00000000;
1748 pf.amax = 255;
1749 pf.rmax = 255;
1750 pf.gmax = 255;
1751 pf.bmax = 255;
1752 pf.ashift = 0;
1753 pf.rshift = 8;
1754 pf.gshift = 16;
1755 pf.bshift = 24;
aliguori90a1e3c2009-01-26 15:37:30 +00001756 pf.rbits = 8;
1757 pf.gbits = 8;
1758 pf.bbits = 8;
1759 pf.abits = 8;
malc0da2ea12009-01-23 19:56:19 +00001760 break;
1761 default:
1762 break;
1763 }
1764 return pf;
1765}
1766
1767PixelFormat qemu_default_pixelformat(int bpp)
1768{
1769 PixelFormat pf;
1770
1771 memset(&pf, 0x00, sizeof(PixelFormat));
1772
1773 pf.bits_per_pixel = bpp;
BALATON Zoltanfeadf1a2012-08-22 17:19:42 +02001774 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
malc0da2ea12009-01-23 19:56:19 +00001775 pf.depth = bpp == 32 ? 24 : bpp;
1776
1777 switch (bpp) {
Gerd Hoffmannb6278082010-05-21 11:59:14 +02001778 case 15:
1779 pf.bits_per_pixel = 16;
Gerd Hoffmannb6278082010-05-21 11:59:14 +02001780 pf.rmask = 0x00007c00;
1781 pf.gmask = 0x000003E0;
1782 pf.bmask = 0x0000001F;
1783 pf.rmax = 31;
1784 pf.gmax = 31;
1785 pf.bmax = 31;
1786 pf.rshift = 10;
1787 pf.gshift = 5;
1788 pf.bshift = 0;
1789 pf.rbits = 5;
1790 pf.gbits = 5;
1791 pf.bbits = 5;
1792 break;
aliguori7d957bd2009-01-15 22:14:11 +00001793 case 16:
1794 pf.rmask = 0x0000F800;
1795 pf.gmask = 0x000007E0;
1796 pf.bmask = 0x0000001F;
1797 pf.rmax = 31;
1798 pf.gmax = 63;
1799 pf.bmax = 31;
1800 pf.rshift = 11;
1801 pf.gshift = 5;
1802 pf.bshift = 0;
aliguori90a1e3c2009-01-26 15:37:30 +00001803 pf.rbits = 5;
1804 pf.gbits = 6;
1805 pf.bbits = 5;
aliguori7d957bd2009-01-15 22:14:11 +00001806 break;
1807 case 24:
aliguori7d957bd2009-01-15 22:14:11 +00001808 pf.rmask = 0x00FF0000;
1809 pf.gmask = 0x0000FF00;
1810 pf.bmask = 0x000000FF;
1811 pf.rmax = 255;
1812 pf.gmax = 255;
1813 pf.bmax = 255;
1814 pf.rshift = 16;
1815 pf.gshift = 8;
1816 pf.bshift = 0;
aliguori90a1e3c2009-01-26 15:37:30 +00001817 pf.rbits = 8;
1818 pf.gbits = 8;
1819 pf.bbits = 8;
Markus Armbruster0eba62e2011-11-22 12:56:10 +01001820 break;
malc0da2ea12009-01-23 19:56:19 +00001821 case 32:
1822 pf.rmask = 0x00FF0000;
1823 pf.gmask = 0x0000FF00;
1824 pf.bmask = 0x000000FF;
malc0da2ea12009-01-23 19:56:19 +00001825 pf.rmax = 255;
1826 pf.gmax = 255;
1827 pf.bmax = 255;
malc0da2ea12009-01-23 19:56:19 +00001828 pf.rshift = 16;
1829 pf.gshift = 8;
1830 pf.bshift = 0;
aliguori90a1e3c2009-01-26 15:37:30 +00001831 pf.rbits = 8;
1832 pf.gbits = 8;
1833 pf.bbits = 8;
aliguori7d957bd2009-01-15 22:14:11 +00001834 break;
1835 default:
1836 break;
1837 }
1838 return pf;
1839}
Anthony Liguori01f45d92013-03-05 23:21:32 +05301840
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001841static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
1842 Error **errp)
1843{
1844 int val;
1845
1846 backend->vc = g_new0(ChardevVC, 1);
1847
1848 val = qemu_opt_get_number(opts, "width", 0);
1849 if (val != 0) {
1850 backend->vc->has_width = true;
1851 backend->vc->width = val;
1852 }
1853
1854 val = qemu_opt_get_number(opts, "height", 0);
1855 if (val != 0) {
1856 backend->vc->has_height = true;
1857 backend->vc->height = val;
1858 }
1859
1860 val = qemu_opt_get_number(opts, "cols", 0);
1861 if (val != 0) {
1862 backend->vc->has_cols = true;
1863 backend->vc->cols = val;
1864 }
1865
1866 val = qemu_opt_get_number(opts, "rows", 0);
1867 if (val != 0) {
1868 backend->vc->has_rows = true;
1869 backend->vc->rows = val;
1870 }
1871}
1872
Anthony Liguori01f45d92013-03-05 23:21:32 +05301873static void register_types(void)
1874{
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001875 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC,
1876 qemu_chr_parse_vc);
Anthony Liguori01f45d92013-03-05 23:21:32 +05301877}
1878
1879type_init(register_types);