blob: 5bbc891f254fcd54853ddd97a18f91d1c87d28d3 [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;
185
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100186 ds->refreshing = true;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100187 dpy_refresh(ds);
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100188 ds->refreshing = false;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100189
190 QLIST_FOREACH(dcl, &ds->listeners, next) {
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100191 dcl_interval = dcl->update_interval ?
192 dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
193 if (interval > dcl_interval) {
194 interval = dcl_interval;
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100195 }
196 }
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100197 if (ds->update_interval != interval) {
198 ds->update_interval = interval;
199 trace_console_refresh(interval);
200 }
201 ds->last_update = qemu_get_clock_ms(rt_clock);
202 qemu_mod_timer(ds->gui_timer, ds->last_update + interval);
Gerd Hoffmann98a9ad92013-03-13 12:17:13 +0100203}
204
205static void gui_setup_refresh(DisplayState *ds)
206{
207 DisplayChangeListener *dcl;
208 bool need_timer = false;
209 bool have_gfx = false;
210 bool have_text = false;
211
212 QLIST_FOREACH(dcl, &ds->listeners, next) {
213 if (dcl->ops->dpy_refresh != NULL) {
214 need_timer = true;
215 }
216 if (dcl->ops->dpy_gfx_update != NULL) {
217 have_gfx = true;
218 }
219 if (dcl->ops->dpy_text_update != NULL) {
220 have_text = true;
221 }
222 }
223
224 if (need_timer && ds->gui_timer == NULL) {
225 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
226 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
227 }
228 if (!need_timer && ds->gui_timer != NULL) {
229 qemu_del_timer(ds->gui_timer);
230 qemu_free_timer(ds->gui_timer);
231 ds->gui_timer = NULL;
232 }
233
234 ds->have_gfx = have_gfx;
235 ds->have_text = have_text;
236}
237
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100238void graphic_hw_update(QemuConsole *con)
pbrook95219892006-04-09 01:06:34 +0000239{
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100240 if (!con) {
241 con = active_console;
242 }
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100243 if (con && con->hw_ops->gfx_update) {
244 con->hw_ops->gfx_update(con->hw);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100245 }
pbrook95219892006-04-09 01:06:34 +0000246}
247
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100248void graphic_hw_invalidate(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->invalidate) {
254 con->hw_ops->invalidate(con->hw);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100255 }
pbrook95219892006-04-09 01:06:34 +0000256}
257
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100258static void ppm_save(const char *filename, struct DisplaySurface *ds,
259 Error **errp)
260{
261 int width = pixman_image_get_width(ds->image);
262 int height = pixman_image_get_height(ds->image);
263 FILE *f;
264 int y;
265 int ret;
266 pixman_image_t *linebuf;
267
268 trace_ppm_save(filename, ds);
269 f = fopen(filename, "wb");
270 if (!f) {
271 error_setg(errp, "failed to open file '%s': %s", filename,
272 strerror(errno));
273 return;
274 }
275 ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
276 if (ret < 0) {
277 linebuf = NULL;
278 goto write_err;
279 }
280 linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
281 for (y = 0; y < height; y++) {
282 qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
283 clearerr(f);
284 ret = fwrite(pixman_image_get_data(linebuf), 1,
285 pixman_image_get_stride(linebuf), f);
286 (void)ret;
287 if (ferror(f)) {
288 goto write_err;
289 }
290 }
291
292out:
293 qemu_pixman_image_unref(linebuf);
294 fclose(f);
295 return;
296
297write_err:
298 error_setg(errp, "failed to write to file '%s': %s", filename,
299 strerror(errno));
300 unlink(filename);
301 goto out;
302}
303
Luiz Capitulinoad39cf62012-05-24 13:48:23 -0300304void qmp_screendump(const char *filename, Error **errp)
pbrook95219892006-04-09 01:06:34 +0000305{
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100306 QemuConsole *con = consoles[0];
307 DisplaySurface *surface;
balrog8571c052008-07-19 13:04:26 +0000308
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100309 if (con == NULL) {
310 error_setg(errp, "There is no QemuConsole I can screendump from.");
311 return;
Jan Kiszkaf81bdef2011-09-16 00:48:07 +0200312 }
313
Gerd Hoffmann2c62f082013-03-12 14:48:31 +0100314 graphic_hw_update(con);
315 surface = qemu_console_surface(con);
316 ppm_save(filename, surface, errp);
pbrook95219892006-04-09 01:06:34 +0000317}
318
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100319void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
balrog4d3b6f62008-02-10 16:33:14 +0000320{
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100321 if (!con) {
322 con = active_console;
323 }
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100324 if (con && con->hw_ops->text_update) {
325 con->hw_ops->text_update(con->hw, chardata);
326 }
balrog4d3b6f62008-02-10 16:33:14 +0000327}
328
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100329static void vga_fill_rect(QemuConsole *con,
330 int posx, int posy, int width, int height,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100331 pixman_color_t color)
bellarde7f0ad52004-07-14 17:28:59 +0000332{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100333 DisplaySurface *surface = qemu_console_surface(con);
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100334 pixman_rectangle16_t rect = {
335 .x = posx, .y = posy, .width = width, .height = height
336 };
ths3b46e622007-09-17 08:09:54 +0000337
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100338 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100339 &color, 1, &rect);
bellarde7f0ad52004-07-14 17:28:59 +0000340}
341
342/* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100343static void vga_bitblt(QemuConsole *con,
344 int xs, int ys, int xd, int yd, int w, int h)
bellarde7f0ad52004-07-14 17:28:59 +0000345{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100346 DisplaySurface *surface = qemu_console_surface(con);
bellarde7f0ad52004-07-14 17:28:59 +0000347
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100348 pixman_image_composite(PIXMAN_OP_SRC,
349 surface->image, NULL, surface->image,
350 xs, ys, 0, 0, xd, yd, w, h);
bellarde7f0ad52004-07-14 17:28:59 +0000351}
352
353/***********************************************************/
354/* basic char display */
355
356#define FONT_HEIGHT 16
357#define FONT_WIDTH 8
358
359#include "vgafont.h"
360
Devin J. Pohlydf00bed2011-09-07 15:44:36 -0400361#ifndef CONFIG_CURSES
pbrook6d6f7c22006-03-11 15:35:30 +0000362enum color_names {
363 COLOR_BLACK = 0,
364 COLOR_RED = 1,
365 COLOR_GREEN = 2,
366 COLOR_YELLOW = 3,
367 COLOR_BLUE = 4,
368 COLOR_MAGENTA = 5,
369 COLOR_CYAN = 6,
370 COLOR_WHITE = 7
371};
Devin J. Pohlydf00bed2011-09-07 15:44:36 -0400372#endif
pbrook6d6f7c22006-03-11 15:35:30 +0000373
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100374#define QEMU_RGB(r, g, b) \
375 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
376
377static const pixman_color_t color_table_rgb[2][8] = {
pbrook6d6f7c22006-03-11 15:35:30 +0000378 { /* dark */
bellard26489842006-06-25 17:37:36 +0000379 QEMU_RGB(0x00, 0x00, 0x00), /* black */
380 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
381 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
382 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
383 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
384 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
385 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
386 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
pbrook6d6f7c22006-03-11 15:35:30 +0000387 },
388 { /* bright */
bellard26489842006-06-25 17:37:36 +0000389 QEMU_RGB(0x00, 0x00, 0x00), /* black */
390 QEMU_RGB(0xff, 0x00, 0x00), /* red */
391 QEMU_RGB(0x00, 0xff, 0x00), /* green */
392 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
393 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
394 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
395 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
396 QEMU_RGB(0xff, 0xff, 0xff), /* white */
pbrook6d6f7c22006-03-11 15:35:30 +0000397 }
bellarde7f0ad52004-07-14 17:28:59 +0000398};
399
pbrook6d6f7c22006-03-11 15:35:30 +0000400#ifdef DEBUG_CONSOLE
401static void console_print_text_attributes(TextAttributes *t_attrib, char ch)
402{
403 if (t_attrib->bold) {
404 printf("b");
405 } else {
406 printf(" ");
407 }
408 if (t_attrib->uline) {
409 printf("u");
410 } else {
411 printf(" ");
412 }
413 if (t_attrib->blink) {
414 printf("l");
415 } else {
416 printf(" ");
417 }
418 if (t_attrib->invers) {
419 printf("i");
420 } else {
421 printf(" ");
422 }
423 if (t_attrib->unvisible) {
424 printf("n");
425 } else {
426 printf(" ");
427 }
428
429 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib->fgcol, t_attrib->bgcol, ch, ch);
430}
431#endif
bellarde7f0ad52004-07-14 17:28:59 +0000432
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100433static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
pbrook6d6f7c22006-03-11 15:35:30 +0000434 TextAttributes *t_attrib)
bellarde7f0ad52004-07-14 17:28:59 +0000435{
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100436 static pixman_image_t *glyphs[256];
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100437 DisplaySurface *surface = qemu_console_surface(s);
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100438 pixman_color_t fgcol, bgcol;
pbrook6d6f7c22006-03-11 15:35:30 +0000439
440 if (t_attrib->invers) {
Gerd Hoffmanncf6f0542013-03-06 09:50:51 +0100441 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
442 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
pbrook6d6f7c22006-03-11 15:35:30 +0000443 } else {
Gerd Hoffmanncf6f0542013-03-06 09:50:51 +0100444 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
445 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
pbrook6d6f7c22006-03-11 15:35:30 +0000446 }
bellarde7f0ad52004-07-14 17:28:59 +0000447
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100448 if (!glyphs[ch]) {
449 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
bellarde7f0ad52004-07-14 17:28:59 +0000450 }
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100451 qemu_pixman_glyph_render(glyphs[ch], surface->image,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100452 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
bellarde7f0ad52004-07-14 17:28:59 +0000453}
454
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200455static void text_console_resize(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000456{
457 TextCell *cells, *c, *c1;
458 int w1, x, y, last_width;
459
460 last_width = s->width;
Gerd Hoffmann36671fb2013-03-13 10:14:52 +0100461 s->width = surface_width(s->surface) / FONT_WIDTH;
462 s->height = surface_height(s->surface) / FONT_HEIGHT;
bellarde7f0ad52004-07-14 17:28:59 +0000463
464 w1 = last_width;
465 if (s->width < w1)
466 w1 = s->width;
467
Anthony Liguori7267c092011-08-20 22:09:37 -0500468 cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
bellarde7f0ad52004-07-14 17:28:59 +0000469 for(y = 0; y < s->total_height; y++) {
470 c = &cells[y * s->width];
471 if (w1 > 0) {
472 c1 = &s->cells[y * last_width];
473 for(x = 0; x < w1; x++) {
474 *c++ = *c1++;
475 }
476 }
477 for(x = w1; x < s->width; x++) {
478 c->ch = ' ';
pbrook6d6f7c22006-03-11 15:35:30 +0000479 c->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +0000480 c++;
481 }
482 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500483 g_free(s->cells);
bellarde7f0ad52004-07-14 17:28:59 +0000484 s->cells = cells;
485}
486
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200487static inline void text_update_xy(QemuConsole *s, int x, int y)
balrog4d3b6f62008-02-10 16:33:14 +0000488{
489 s->text_x[0] = MIN(s->text_x[0], x);
490 s->text_x[1] = MAX(s->text_x[1], x);
491 s->text_y[0] = MIN(s->text_y[0], y);
492 s->text_y[1] = MAX(s->text_y[1], y);
493}
494
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200495static void invalidate_xy(QemuConsole *s, int x, int y)
pbrook14778c22009-01-21 03:02:52 +0000496{
497 if (s->update_x0 > x * FONT_WIDTH)
498 s->update_x0 = x * FONT_WIDTH;
499 if (s->update_y0 > y * FONT_HEIGHT)
500 s->update_y0 = y * FONT_HEIGHT;
501 if (s->update_x1 < (x + 1) * FONT_WIDTH)
502 s->update_x1 = (x + 1) * FONT_WIDTH;
503 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
504 s->update_y1 = (y + 1) * FONT_HEIGHT;
505}
506
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200507static void update_xy(QemuConsole *s, int x, int y)
bellarde7f0ad52004-07-14 17:28:59 +0000508{
509 TextCell *c;
510 int y1, y2;
511
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100512 if (s != active_console) {
513 return;
514 }
balrog4d3b6f62008-02-10 16:33:14 +0000515
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100516 if (s->ds->have_text) {
517 text_update_xy(s, x, y);
518 }
519
520 if (s->ds->have_gfx) {
bellarde7f0ad52004-07-14 17:28:59 +0000521 y1 = (s->y_base + y) % s->total_height;
522 y2 = y1 - s->y_displayed;
523 if (y2 < 0)
524 y2 += s->total_height;
525 if (y2 < s->height) {
526 c = &s->cells[y1 * s->width + x];
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100527 vga_putcharxy(s, x, y2, c->ch,
pbrook6d6f7c22006-03-11 15:35:30 +0000528 &(c->t_attrib));
pbrook14778c22009-01-21 03:02:52 +0000529 invalidate_xy(s, x, y2);
bellarde7f0ad52004-07-14 17:28:59 +0000530 }
531 }
532}
533
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200534static void console_show_cursor(QemuConsole *s, int show)
bellarde7f0ad52004-07-14 17:28:59 +0000535{
536 TextCell *c;
537 int y, y1;
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100538 int x = s->x;
bellarde7f0ad52004-07-14 17:28:59 +0000539
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100540 if (s != active_console) {
541 return;
542 }
balrog4d3b6f62008-02-10 16:33:14 +0000543
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100544 if (s->ds->have_text) {
545 s->cursor_invalidate = 1;
546 }
balrog4d3b6f62008-02-10 16:33:14 +0000547
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100548 if (s->ds->have_gfx) {
thsed8276a2007-02-10 22:37:56 +0000549 if (x >= s->width) {
550 x = s->width - 1;
551 }
bellarde7f0ad52004-07-14 17:28:59 +0000552 y1 = (s->y_base + s->y) % s->total_height;
553 y = y1 - s->y_displayed;
554 if (y < 0)
555 y += s->total_height;
556 if (y < s->height) {
thsed8276a2007-02-10 22:37:56 +0000557 c = &s->cells[y1 * s->width + x];
Jan Kiszkabf1bed82012-07-10 22:00:55 +0200558 if (show && s->cursor_visible_phase) {
pbrook6d6f7c22006-03-11 15:35:30 +0000559 TextAttributes t_attrib = s->t_attrib_default;
560 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100561 vga_putcharxy(s, x, y, c->ch, &t_attrib);
bellarde7f0ad52004-07-14 17:28:59 +0000562 } else {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100563 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
bellarde7f0ad52004-07-14 17:28:59 +0000564 }
pbrook14778c22009-01-21 03:02:52 +0000565 invalidate_xy(s, x, y);
bellarde7f0ad52004-07-14 17:28:59 +0000566 }
567 }
568}
569
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200570static void console_refresh(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000571{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100572 DisplaySurface *surface = qemu_console_surface(s);
bellarde7f0ad52004-07-14 17:28:59 +0000573 TextCell *c;
574 int x, y, y1;
575
ths5fafdf22007-09-16 21:08:06 +0000576 if (s != active_console)
bellarde7f0ad52004-07-14 17:28:59 +0000577 return;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200578
579 if (s->ds->have_text) {
balrog4d3b6f62008-02-10 16:33:14 +0000580 s->text_x[0] = 0;
581 s->text_y[0] = 0;
582 s->text_x[1] = s->width - 1;
583 s->text_y[1] = s->height - 1;
584 s->cursor_invalidate = 1;
balrog4d3b6f62008-02-10 16:33:14 +0000585 }
bellarde7f0ad52004-07-14 17:28:59 +0000586
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200587 if (s->ds->have_gfx) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100588 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
Gerd Hoffmanncf6f0542013-03-06 09:50:51 +0100589 color_table_rgb[0][COLOR_BLACK]);
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200590 y1 = s->y_displayed;
591 for (y = 0; y < s->height; y++) {
592 c = s->cells + y1 * s->width;
593 for (x = 0; x < s->width; x++) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100594 vga_putcharxy(s, x, y, c->ch,
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200595 &(c->t_attrib));
596 c++;
597 }
598 if (++y1 == s->total_height) {
599 y1 = 0;
600 }
bellarde7f0ad52004-07-14 17:28:59 +0000601 }
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200602 console_show_cursor(s, 1);
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100603 dpy_gfx_update(s, 0, 0,
604 surface_width(surface), surface_height(surface));
bellarde7f0ad52004-07-14 17:28:59 +0000605 }
bellarde7f0ad52004-07-14 17:28:59 +0000606}
607
608static void console_scroll(int ydelta)
609{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200610 QemuConsole *s;
bellarde7f0ad52004-07-14 17:28:59 +0000611 int i, y1;
ths3b46e622007-09-17 08:09:54 +0000612
bellarde7f0ad52004-07-14 17:28:59 +0000613 s = active_console;
thsaf3a9032007-07-11 23:14:59 +0000614 if (!s || (s->console_type == GRAPHIC_CONSOLE))
bellarde7f0ad52004-07-14 17:28:59 +0000615 return;
616
617 if (ydelta > 0) {
618 for(i = 0; i < ydelta; i++) {
619 if (s->y_displayed == s->y_base)
620 break;
621 if (++s->y_displayed == s->total_height)
622 s->y_displayed = 0;
623 }
624 } else {
625 ydelta = -ydelta;
626 i = s->backscroll_height;
627 if (i > s->total_height - s->height)
628 i = s->total_height - s->height;
629 y1 = s->y_base - i;
630 if (y1 < 0)
631 y1 += s->total_height;
632 for(i = 0; i < ydelta; i++) {
633 if (s->y_displayed == y1)
634 break;
635 if (--s->y_displayed < 0)
636 s->y_displayed = s->total_height - 1;
637 }
638 }
639 console_refresh(s);
640}
641
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200642static void console_put_lf(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000643{
644 TextCell *c;
645 int x, y1;
646
bellarde7f0ad52004-07-14 17:28:59 +0000647 s->y++;
648 if (s->y >= s->height) {
649 s->y = s->height - 1;
pbrook6d6f7c22006-03-11 15:35:30 +0000650
bellarde7f0ad52004-07-14 17:28:59 +0000651 if (s->y_displayed == s->y_base) {
652 if (++s->y_displayed == s->total_height)
653 s->y_displayed = 0;
654 }
655 if (++s->y_base == s->total_height)
656 s->y_base = 0;
657 if (s->backscroll_height < s->total_height)
658 s->backscroll_height++;
659 y1 = (s->y_base + s->height - 1) % s->total_height;
660 c = &s->cells[y1 * s->width];
661 for(x = 0; x < s->width; x++) {
662 c->ch = ' ';
pbrook6d6f7c22006-03-11 15:35:30 +0000663 c->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +0000664 c++;
665 }
666 if (s == active_console && s->y_displayed == s->y_base) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100667 if (s->ds->have_text) {
balrog4d3b6f62008-02-10 16:33:14 +0000668 s->text_x[0] = 0;
669 s->text_y[0] = 0;
670 s->text_x[1] = s->width - 1;
671 s->text_y[1] = s->height - 1;
balrog4d3b6f62008-02-10 16:33:14 +0000672 }
673
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100674 if (s->ds->have_gfx) {
675 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
676 s->width * FONT_WIDTH,
677 (s->height - 1) * FONT_HEIGHT);
678 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
679 s->width * FONT_WIDTH, FONT_HEIGHT,
680 color_table_rgb[0][s->t_attrib_default.bgcol]);
681 s->update_x0 = 0;
682 s->update_y0 = 0;
683 s->update_x1 = s->width * FONT_WIDTH;
684 s->update_y1 = s->height * FONT_HEIGHT;
685 }
bellarde7f0ad52004-07-14 17:28:59 +0000686 }
687 }
688}
689
pbrook6d6f7c22006-03-11 15:35:30 +0000690/* Set console attributes depending on the current escape codes.
691 * NOTE: I know this code is not very efficient (checking every color for it
692 * self) but it is more readable and better maintainable.
693 */
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200694static void console_handle_escape(QemuConsole *s)
pbrook6d6f7c22006-03-11 15:35:30 +0000695{
696 int i;
697
pbrook6d6f7c22006-03-11 15:35:30 +0000698 for (i=0; i<s->nb_esc_params; i++) {
699 switch (s->esc_params[i]) {
700 case 0: /* reset all console attributes to default */
701 s->t_attrib = s->t_attrib_default;
702 break;
703 case 1:
704 s->t_attrib.bold = 1;
705 break;
706 case 4:
707 s->t_attrib.uline = 1;
708 break;
709 case 5:
710 s->t_attrib.blink = 1;
711 break;
712 case 7:
713 s->t_attrib.invers = 1;
714 break;
715 case 8:
716 s->t_attrib.unvisible = 1;
717 break;
718 case 22:
719 s->t_attrib.bold = 0;
720 break;
721 case 24:
722 s->t_attrib.uline = 0;
723 break;
724 case 25:
725 s->t_attrib.blink = 0;
726 break;
727 case 27:
728 s->t_attrib.invers = 0;
729 break;
730 case 28:
731 s->t_attrib.unvisible = 0;
732 break;
733 /* set foreground color */
734 case 30:
735 s->t_attrib.fgcol=COLOR_BLACK;
736 break;
737 case 31:
738 s->t_attrib.fgcol=COLOR_RED;
739 break;
740 case 32:
741 s->t_attrib.fgcol=COLOR_GREEN;
742 break;
743 case 33:
744 s->t_attrib.fgcol=COLOR_YELLOW;
745 break;
746 case 34:
747 s->t_attrib.fgcol=COLOR_BLUE;
748 break;
749 case 35:
750 s->t_attrib.fgcol=COLOR_MAGENTA;
751 break;
752 case 36:
753 s->t_attrib.fgcol=COLOR_CYAN;
754 break;
755 case 37:
756 s->t_attrib.fgcol=COLOR_WHITE;
757 break;
758 /* set background color */
759 case 40:
760 s->t_attrib.bgcol=COLOR_BLACK;
761 break;
762 case 41:
763 s->t_attrib.bgcol=COLOR_RED;
764 break;
765 case 42:
766 s->t_attrib.bgcol=COLOR_GREEN;
767 break;
768 case 43:
769 s->t_attrib.bgcol=COLOR_YELLOW;
770 break;
771 case 44:
772 s->t_attrib.bgcol=COLOR_BLUE;
773 break;
774 case 45:
775 s->t_attrib.bgcol=COLOR_MAGENTA;
776 break;
777 case 46:
778 s->t_attrib.bgcol=COLOR_CYAN;
779 break;
780 case 47:
781 s->t_attrib.bgcol=COLOR_WHITE;
782 break;
783 }
784 }
785}
786
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200787static void console_clear_xy(QemuConsole *s, int x, int y)
thsadb47962007-01-16 23:02:36 +0000788{
789 int y1 = (s->y_base + y) % s->total_height;
790 TextCell *c = &s->cells[y1 * s->width + x];
791 c->ch = ' ';
792 c->t_attrib = s->t_attrib_default;
thsadb47962007-01-16 23:02:36 +0000793 update_xy(s, x, y);
794}
795
Ian Campbell3eea5492012-09-04 10:26:09 -0500796/* set cursor, checking bounds */
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200797static void set_cursor(QemuConsole *s, int x, int y)
Ian Campbell3eea5492012-09-04 10:26:09 -0500798{
799 if (x < 0) {
800 x = 0;
801 }
802 if (y < 0) {
803 y = 0;
804 }
805 if (y >= s->height) {
806 y = s->height - 1;
807 }
808 if (x >= s->width) {
809 x = s->width - 1;
810 }
811
812 s->x = x;
813 s->y = y;
814}
815
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200816static void console_putchar(QemuConsole *s, int ch)
bellarde7f0ad52004-07-14 17:28:59 +0000817{
818 TextCell *c;
thsadb47962007-01-16 23:02:36 +0000819 int y1, i;
820 int x, y;
bellarde7f0ad52004-07-14 17:28:59 +0000821
822 switch(s->state) {
823 case TTY_STATE_NORM:
824 switch(ch) {
pbrook6d6f7c22006-03-11 15:35:30 +0000825 case '\r': /* carriage return */
bellarde7f0ad52004-07-14 17:28:59 +0000826 s->x = 0;
827 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000828 case '\n': /* newline */
bellarde7f0ad52004-07-14 17:28:59 +0000829 console_put_lf(s);
830 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000831 case '\b': /* backspace */
ths5fafdf22007-09-16 21:08:06 +0000832 if (s->x > 0)
bellarde15d7372006-06-25 16:26:29 +0000833 s->x--;
pbrook6d6f7c22006-03-11 15:35:30 +0000834 break;
835 case '\t': /* tabspace */
836 if (s->x + (8 - (s->x % 8)) > s->width) {
bellardbd468842006-07-14 20:24:31 +0000837 s->x = 0;
pbrook6d6f7c22006-03-11 15:35:30 +0000838 console_put_lf(s);
839 } else {
840 s->x = s->x + (8 - (s->x % 8));
841 }
842 break;
843 case '\a': /* alert aka. bell */
844 /* TODO: has to be implemented */
845 break;
thsadb47962007-01-16 23:02:36 +0000846 case 14:
847 /* SI (shift in), character set 0 (ignored) */
848 break;
849 case 15:
850 /* SO (shift out), character set 1 (ignored) */
851 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000852 case 27: /* esc (introducing an escape sequence) */
bellarde7f0ad52004-07-14 17:28:59 +0000853 s->state = TTY_STATE_ESC;
854 break;
855 default:
thsed8276a2007-02-10 22:37:56 +0000856 if (s->x >= s->width) {
857 /* line wrap */
858 s->x = 0;
859 console_put_lf(s);
thsadb47962007-01-16 23:02:36 +0000860 }
bellarde7f0ad52004-07-14 17:28:59 +0000861 y1 = (s->y_base + s->y) % s->total_height;
862 c = &s->cells[y1 * s->width + s->x];
863 c->ch = ch;
pbrook6d6f7c22006-03-11 15:35:30 +0000864 c->t_attrib = s->t_attrib;
bellarde7f0ad52004-07-14 17:28:59 +0000865 update_xy(s, s->x, s->y);
866 s->x++;
bellarde7f0ad52004-07-14 17:28:59 +0000867 break;
868 }
869 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000870 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
bellarde7f0ad52004-07-14 17:28:59 +0000871 if (ch == '[') {
872 for(i=0;i<MAX_ESC_PARAMS;i++)
873 s->esc_params[i] = 0;
874 s->nb_esc_params = 0;
875 s->state = TTY_STATE_CSI;
876 } else {
877 s->state = TTY_STATE_NORM;
878 }
879 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000880 case TTY_STATE_CSI: /* handle escape sequence parameters */
bellarde7f0ad52004-07-14 17:28:59 +0000881 if (ch >= '0' && ch <= '9') {
882 if (s->nb_esc_params < MAX_ESC_PARAMS) {
Laszlo Ersekc10600a2012-09-17 11:10:03 +0200883 int *param = &s->esc_params[s->nb_esc_params];
884 int digit = (ch - '0');
885
886 *param = (*param <= (INT_MAX - digit) / 10) ?
887 *param * 10 + digit : INT_MAX;
bellarde7f0ad52004-07-14 17:28:59 +0000888 }
889 } else {
Ian Campbell3eea5492012-09-04 10:26:09 -0500890 if (s->nb_esc_params < MAX_ESC_PARAMS)
891 s->nb_esc_params++;
bellarde7f0ad52004-07-14 17:28:59 +0000892 if (ch == ';')
893 break;
thsadb47962007-01-16 23:02:36 +0000894#ifdef DEBUG_CONSOLE
895 fprintf(stderr, "escape sequence CSI%d;%d%c, %d parameters\n",
896 s->esc_params[0], s->esc_params[1], ch, s->nb_esc_params);
897#endif
bellarde7f0ad52004-07-14 17:28:59 +0000898 s->state = TTY_STATE_NORM;
899 switch(ch) {
thsadb47962007-01-16 23:02:36 +0000900 case 'A':
901 /* move cursor up */
902 if (s->esc_params[0] == 0) {
903 s->esc_params[0] = 1;
904 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500905 set_cursor(s, s->x, s->y - s->esc_params[0]);
bellarde7f0ad52004-07-14 17:28:59 +0000906 break;
thsadb47962007-01-16 23:02:36 +0000907 case 'B':
908 /* move cursor down */
909 if (s->esc_params[0] == 0) {
910 s->esc_params[0] = 1;
911 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500912 set_cursor(s, s->x, s->y + s->esc_params[0]);
thsadb47962007-01-16 23:02:36 +0000913 break;
914 case 'C':
915 /* move cursor right */
916 if (s->esc_params[0] == 0) {
917 s->esc_params[0] = 1;
918 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500919 set_cursor(s, s->x + s->esc_params[0], s->y);
thsadb47962007-01-16 23:02:36 +0000920 break;
921 case 'D':
922 /* move cursor left */
923 if (s->esc_params[0] == 0) {
924 s->esc_params[0] = 1;
925 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500926 set_cursor(s, s->x - s->esc_params[0], s->y);
thsadb47962007-01-16 23:02:36 +0000927 break;
928 case 'G':
929 /* move cursor to column */
Ian Campbell3eea5492012-09-04 10:26:09 -0500930 set_cursor(s, s->esc_params[0] - 1, s->y);
thsadb47962007-01-16 23:02:36 +0000931 break;
932 case 'f':
933 case 'H':
934 /* move cursor to row, column */
Ian Campbell3eea5492012-09-04 10:26:09 -0500935 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
thsadb47962007-01-16 23:02:36 +0000936 break;
937 case 'J':
938 switch (s->esc_params[0]) {
939 case 0:
940 /* clear to end of screen */
941 for (y = s->y; y < s->height; y++) {
942 for (x = 0; x < s->width; x++) {
943 if (y == s->y && x < s->x) {
944 continue;
945 }
946 console_clear_xy(s, x, y);
947 }
948 }
949 break;
950 case 1:
951 /* clear from beginning of screen */
952 for (y = 0; y <= s->y; y++) {
953 for (x = 0; x < s->width; x++) {
954 if (y == s->y && x > s->x) {
955 break;
956 }
957 console_clear_xy(s, x, y);
958 }
959 }
960 break;
961 case 2:
962 /* clear entire screen */
963 for (y = 0; y <= s->height; y++) {
964 for (x = 0; x < s->width; x++) {
965 console_clear_xy(s, x, y);
966 }
967 }
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100968 break;
thsadb47962007-01-16 23:02:36 +0000969 }
Markus Armbruster95d8f9f2011-11-22 11:59:07 +0100970 break;
thsadb47962007-01-16 23:02:36 +0000971 case 'K':
972 switch (s->esc_params[0]) {
973 case 0:
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100974 /* clear to eol */
975 for(x = s->x; x < s->width; x++) {
thsadb47962007-01-16 23:02:36 +0000976 console_clear_xy(s, x, s->y);
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100977 }
978 break;
thsadb47962007-01-16 23:02:36 +0000979 case 1:
980 /* clear from beginning of line */
981 for (x = 0; x <= s->x; x++) {
982 console_clear_xy(s, x, s->y);
983 }
984 break;
985 case 2:
986 /* clear entire line */
987 for(x = 0; x < s->width; x++) {
988 console_clear_xy(s, x, s->y);
989 }
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100990 break;
991 }
thsadb47962007-01-16 23:02:36 +0000992 break;
993 case 'm':
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100994 console_handle_escape(s);
995 break;
thsadb47962007-01-16 23:02:36 +0000996 case 'n':
997 /* report cursor position */
998 /* TODO: send ESC[row;colR */
999 break;
1000 case 's':
1001 /* save cursor position */
1002 s->x_saved = s->x;
1003 s->y_saved = s->y;
1004 break;
1005 case 'u':
1006 /* restore cursor position */
1007 s->x = s->x_saved;
1008 s->y = s->y_saved;
1009 break;
1010 default:
1011#ifdef DEBUG_CONSOLE
1012 fprintf(stderr, "unhandled escape character '%c'\n", ch);
1013#endif
1014 break;
1015 }
1016 break;
bellarde7f0ad52004-07-14 17:28:59 +00001017 }
1018 }
1019}
1020
1021void console_select(unsigned int index)
1022{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001023 QemuConsole *s;
pbrook6d6f7c22006-03-11 15:35:30 +00001024
bellarde7f0ad52004-07-14 17:28:59 +00001025 if (index >= MAX_CONSOLES)
1026 return;
Gerd Hoffmann437fe102013-03-07 16:04:52 +01001027
1028 trace_console_select(index);
bellarde7f0ad52004-07-14 17:28:59 +00001029 s = consoles[index];
1030 if (s) {
aliguori7d957bd2009-01-15 22:14:11 +00001031 DisplayState *ds = s->ds;
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001032
Stefan Weil8bd6b062012-08-17 15:50:44 +02001033 if (active_console && active_console->cursor_timer) {
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001034 qemu_del_timer(active_console->cursor_timer);
1035 }
bellarde7f0ad52004-07-14 17:28:59 +00001036 active_console = s;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001037 if (ds->have_gfx) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001038 dpy_gfx_switch_surface(ds, s->surface);
1039 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
1040 surface_height(s->surface));
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001041 }
1042 if (ds->have_text) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001043 dpy_text_resize(s, s->width, s->height);
aliguori68f00992009-01-21 18:59:12 +00001044 }
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001045 if (s->cursor_timer) {
1046 qemu_mod_timer(s->cursor_timer,
1047 qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
1048 }
bellarde7f0ad52004-07-14 17:28:59 +00001049 }
1050}
1051
1052static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
1053{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001054 QemuConsole *s = chr->opaque;
bellarde7f0ad52004-07-14 17:28:59 +00001055 int i;
1056
pbrook14778c22009-01-21 03:02:52 +00001057 s->update_x0 = s->width * FONT_WIDTH;
1058 s->update_y0 = s->height * FONT_HEIGHT;
1059 s->update_x1 = 0;
1060 s->update_y1 = 0;
bellarde7f0ad52004-07-14 17:28:59 +00001061 console_show_cursor(s, 0);
1062 for(i = 0; i < len; i++) {
1063 console_putchar(s, buf[i]);
1064 }
1065 console_show_cursor(s, 1);
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001066 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001067 dpy_gfx_update(s, s->update_x0, s->update_y0,
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001068 s->update_x1 - s->update_x0,
1069 s->update_y1 - s->update_y0);
pbrook14778c22009-01-21 03:02:52 +00001070 }
bellarde7f0ad52004-07-14 17:28:59 +00001071 return len;
1072}
1073
bellarde15d7372006-06-25 16:26:29 +00001074static void kbd_send_chars(void *opaque)
1075{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001076 QemuConsole *s = opaque;
bellarde15d7372006-06-25 16:26:29 +00001077 int len;
1078 uint8_t buf[16];
ths3b46e622007-09-17 08:09:54 +00001079
Anthony Liguori909cda12011-08-15 11:17:31 -05001080 len = qemu_chr_be_can_write(s->chr);
bellarde15d7372006-06-25 16:26:29 +00001081 if (len > s->out_fifo.count)
1082 len = s->out_fifo.count;
1083 if (len > 0) {
1084 if (len > sizeof(buf))
1085 len = sizeof(buf);
1086 qemu_fifo_read(&s->out_fifo, buf, len);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001087 qemu_chr_be_write(s->chr, buf, len);
bellarde15d7372006-06-25 16:26:29 +00001088 }
1089 /* characters are pending: we send them a bit later (XXX:
1090 horrible, should change char device API) */
1091 if (s->out_fifo.count > 0) {
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001092 qemu_mod_timer(s->kbd_timer, qemu_get_clock_ms(rt_clock) + 1);
bellarde15d7372006-06-25 16:26:29 +00001093 }
1094}
1095
bellarde7f0ad52004-07-14 17:28:59 +00001096/* called when an ascii key is pressed */
1097void kbd_put_keysym(int keysym)
1098{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001099 QemuConsole *s;
bellarde7f0ad52004-07-14 17:28:59 +00001100 uint8_t buf[16], *q;
1101 int c;
1102
1103 s = active_console;
thsaf3a9032007-07-11 23:14:59 +00001104 if (!s || (s->console_type == GRAPHIC_CONSOLE))
bellarde7f0ad52004-07-14 17:28:59 +00001105 return;
1106
1107 switch(keysym) {
1108 case QEMU_KEY_CTRL_UP:
1109 console_scroll(-1);
1110 break;
1111 case QEMU_KEY_CTRL_DOWN:
1112 console_scroll(1);
1113 break;
1114 case QEMU_KEY_CTRL_PAGEUP:
1115 console_scroll(-10);
1116 break;
1117 case QEMU_KEY_CTRL_PAGEDOWN:
1118 console_scroll(10);
1119 break;
1120 default:
bellarde15d7372006-06-25 16:26:29 +00001121 /* convert the QEMU keysym to VT100 key string */
1122 q = buf;
1123 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1124 *q++ = '\033';
1125 *q++ = '[';
1126 c = keysym - 0xe100;
1127 if (c >= 10)
1128 *q++ = '0' + (c / 10);
1129 *q++ = '0' + (c % 10);
1130 *q++ = '~';
1131 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1132 *q++ = '\033';
1133 *q++ = '[';
1134 *q++ = keysym & 0xff;
Paolo Bonzini41048332010-12-23 13:42:52 +01001135 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1136 console_puts(s->chr, (const uint8_t *) "\r", 1);
1137 *q++ = '\n';
bellarde15d7372006-06-25 16:26:29 +00001138 } else {
Paolo Bonzini41048332010-12-23 13:42:52 +01001139 *q++ = keysym;
1140 }
1141 if (s->echo) {
1142 console_puts(s->chr, buf, q - buf);
bellarde15d7372006-06-25 16:26:29 +00001143 }
pbrooke5b0bc42007-01-27 23:46:43 +00001144 if (s->chr->chr_read) {
bellarde15d7372006-06-25 16:26:29 +00001145 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1146 kbd_send_chars(s);
bellarde7f0ad52004-07-14 17:28:59 +00001147 }
1148 break;
1149 }
1150}
1151
balrog4d3b6f62008-02-10 16:33:14 +00001152static void text_console_invalidate(void *opaque)
1153{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001154 QemuConsole *s = (QemuConsole *) opaque;
Gerd Hoffmann1562e532013-03-06 13:40:47 +01001155
1156 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
aliguori68f00992009-01-21 18:59:12 +00001157 text_console_resize(s);
1158 }
balrog4d3b6f62008-02-10 16:33:14 +00001159 console_refresh(s);
1160}
1161
Anthony Liguoric227f092009-10-01 16:12:16 -05001162static void text_console_update(void *opaque, console_ch_t *chardata)
balrog4d3b6f62008-02-10 16:33:14 +00001163{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001164 QemuConsole *s = (QemuConsole *) opaque;
balrog4d3b6f62008-02-10 16:33:14 +00001165 int i, j, src;
1166
1167 if (s->text_x[0] <= s->text_x[1]) {
1168 src = (s->y_base + s->text_y[0]) * s->width;
1169 chardata += s->text_y[0] * s->width;
1170 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1171 for (j = 0; j < s->width; j ++, src ++)
1172 console_write_ch(chardata ++, s->cells[src].ch |
1173 (s->cells[src].t_attrib.fgcol << 12) |
1174 (s->cells[src].t_attrib.bgcol << 8) |
1175 (s->cells[src].t_attrib.bold << 21));
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001176 dpy_text_update(s, s->text_x[0], s->text_y[0],
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001177 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
balrog4d3b6f62008-02-10 16:33:14 +00001178 s->text_x[0] = s->width;
1179 s->text_y[0] = s->height;
1180 s->text_x[1] = 0;
1181 s->text_y[1] = 0;
1182 }
1183 if (s->cursor_invalidate) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001184 dpy_text_cursor(s, s->x, s->y);
balrog4d3b6f62008-02-10 16:33:14 +00001185 s->cursor_invalidate = 0;
1186 }
1187}
1188
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001189static QemuConsole *new_console(DisplayState *ds, console_type_t console_type)
bellarde7f0ad52004-07-14 17:28:59 +00001190{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001191 QemuConsole *s;
pbrook95219892006-04-09 01:06:34 +00001192 int i;
bellarde7f0ad52004-07-14 17:28:59 +00001193
1194 if (nb_consoles >= MAX_CONSOLES)
1195 return NULL;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001196 s = g_malloc0(sizeof(QemuConsole));
thsaf3a9032007-07-11 23:14:59 +00001197 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1198 (console_type == GRAPHIC_CONSOLE))) {
bellarde7f0ad52004-07-14 17:28:59 +00001199 active_console = s;
thsaf3a9032007-07-11 23:14:59 +00001200 }
bellarde7f0ad52004-07-14 17:28:59 +00001201 s->ds = ds;
thsaf3a9032007-07-11 23:14:59 +00001202 s->console_type = console_type;
1203 if (console_type != GRAPHIC_CONSOLE) {
Jan Kiszkaf81bdef2011-09-16 00:48:07 +02001204 s->index = nb_consoles;
pbrook95219892006-04-09 01:06:34 +00001205 consoles[nb_consoles++] = s;
1206 } else {
1207 /* HACK: Put graphical consoles before text consoles. */
1208 for (i = nb_consoles; i > 0; i--) {
thsaf3a9032007-07-11 23:14:59 +00001209 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
pbrook95219892006-04-09 01:06:34 +00001210 break;
1211 consoles[i] = consoles[i - 1];
Jan Kiszkaf81bdef2011-09-16 00:48:07 +02001212 consoles[i]->index = i;
pbrook95219892006-04-09 01:06:34 +00001213 }
Jan Kiszkaf81bdef2011-09-16 00:48:07 +02001214 s->index = i;
pbrook95219892006-04-09 01:06:34 +00001215 consoles[i] = s;
aliguori3023f332009-01-16 19:04:14 +00001216 nb_consoles++;
pbrook95219892006-04-09 01:06:34 +00001217 }
bellarde7f0ad52004-07-14 17:28:59 +00001218 return s;
1219}
1220
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001221static void qemu_alloc_display(DisplaySurface *surface, int width, int height,
1222 int linesize, PixelFormat pf, int newflags)
Jes Sorensenffe8b822011-03-16 13:33:30 +01001223{
Jes Sorensenffe8b822011-03-16 13:33:30 +01001224 surface->pf = pf;
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001225
1226 qemu_pixman_image_unref(surface->image);
1227 surface->image = NULL;
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001228
1229 surface->format = qemu_pixman_get_format(&pf);
1230 assert(surface->format != 0);
1231 surface->image = pixman_image_create_bits(surface->format,
1232 width, height,
1233 NULL, linesize);
1234 assert(surface->image != NULL);
1235
Jes Sorensenffe8b822011-03-16 13:33:30 +01001236 surface->flags = newflags | QEMU_ALLOCATED_FLAG;
Paolo Bonzini98b50082010-02-11 00:29:57 +01001237#ifdef HOST_WORDS_BIGENDIAN
Jes Sorensenffe8b822011-03-16 13:33:30 +01001238 surface->flags |= QEMU_BIG_ENDIAN_FLAG;
Paolo Bonzini98b50082010-02-11 00:29:57 +01001239#endif
Paolo Bonzini98b50082010-02-11 00:29:57 +01001240}
1241
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001242DisplaySurface *qemu_create_displaysurface(int width, int height)
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001243{
1244 DisplaySurface *surface = g_new0(DisplaySurface, 1);
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001245 int linesize = width * 4;
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001246
1247 trace_displaysurface_create(surface, width, height);
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001248 qemu_alloc_display(surface, width, height, linesize,
1249 qemu_default_pixelformat(32), 0);
1250 return surface;
1251}
1252
Gerd Hoffmann187cd1d2012-09-26 07:46:20 +02001253DisplaySurface *qemu_create_displaysurface_from(int width, int height, int bpp,
Gerd Hoffmannb1424e02013-02-20 09:37:12 +01001254 int linesize, uint8_t *data,
1255 bool byteswap)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001256{
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001257 DisplaySurface *surface = g_new0(DisplaySurface, 1);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001258
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001259 trace_displaysurface_create_from(surface, width, height, bpp, byteswap);
Gerd Hoffmannb1424e02013-02-20 09:37:12 +01001260 if (byteswap) {
1261 surface->pf = qemu_different_endianness_pixelformat(bpp);
1262 } else {
1263 surface->pf = qemu_default_pixelformat(bpp);
1264 }
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001265
1266 surface->format = qemu_pixman_get_format(&surface->pf);
1267 assert(surface->format != 0);
1268 surface->image = pixman_image_create_bits(surface->format,
1269 width, height,
1270 (void *)data, linesize);
1271 assert(surface->image != NULL);
1272
Paolo Bonzini98b50082010-02-11 00:29:57 +01001273#ifdef HOST_WORDS_BIGENDIAN
1274 surface->flags = QEMU_BIG_ENDIAN_FLAG;
1275#endif
Paolo Bonzini98b50082010-02-11 00:29:57 +01001276
1277 return surface;
1278}
1279
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001280void qemu_free_displaysurface(DisplaySurface *surface)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001281{
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001282 if (surface == NULL) {
Paolo Bonzini98b50082010-02-11 00:29:57 +01001283 return;
Gerd Hoffmann187cd1d2012-09-26 07:46:20 +02001284 }
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001285 trace_displaysurface_free(surface);
1286 qemu_pixman_image_unref(surface->image);
1287 g_free(surface);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001288}
1289
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001290void register_displaychangelistener(DisplayState *ds,
1291 DisplayChangeListener *dcl)
1292{
1293 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1294 dcl->ds = ds;
1295 QLIST_INSERT_HEAD(&ds->listeners, dcl, next);
1296 gui_setup_refresh(ds);
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001297 if (dcl->ops->dpy_gfx_switch && active_console) {
1298 dcl->ops->dpy_gfx_switch(dcl, active_console->surface);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001299 }
1300}
1301
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +01001302void update_displaychangelistener(DisplayChangeListener *dcl,
1303 uint64_t interval)
1304{
1305 DisplayState *ds = dcl->ds;
1306
1307 dcl->update_interval = interval;
1308 if (!ds->refreshing && ds->update_interval > interval) {
1309 qemu_mod_timer(ds->gui_timer, ds->last_update + interval);
1310 }
1311}
1312
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001313void unregister_displaychangelistener(DisplayChangeListener *dcl)
1314{
1315 DisplayState *ds = dcl->ds;
1316 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1317 QLIST_REMOVE(dcl, next);
1318 gui_setup_refresh(ds);
1319}
1320
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001321void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001322{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001323 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001324 struct DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001325 int width = surface_width(con->surface);
1326 int height = surface_height(con->surface);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001327
1328 x = MAX(x, 0);
1329 y = MAX(y, 0);
1330 x = MIN(x, width);
1331 y = MIN(y, height);
1332 w = MIN(w, width - x);
1333 h = MIN(h, height - y);
1334
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001335 if (con != active_console) {
1336 return;
1337 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001338 QLIST_FOREACH(dcl, &s->listeners, next) {
1339 if (dcl->ops->dpy_gfx_update) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001340 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001341 }
1342 }
1343}
1344
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001345static void dpy_gfx_switch_surface(DisplayState *ds,
1346 DisplaySurface *surface)
1347{
1348 struct DisplayChangeListener *dcl;
1349
1350 QLIST_FOREACH(dcl, &ds->listeners, next) {
1351 if (dcl->ops->dpy_gfx_switch) {
1352 dcl->ops->dpy_gfx_switch(dcl, surface);
1353 }
1354 }
1355}
1356
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001357void dpy_gfx_replace_surface(QemuConsole *con,
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001358 DisplaySurface *surface)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001359{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001360 DisplayState *s = con->ds;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001361 DisplaySurface *old_surface = con->surface;
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001362
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001363 con->surface = surface;
1364 if (con == active_console) {
1365 dpy_gfx_switch_surface(s, surface);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001366 }
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001367 qemu_free_displaysurface(old_surface);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001368}
1369
1370void dpy_refresh(DisplayState *s)
1371{
1372 struct DisplayChangeListener *dcl;
1373 QLIST_FOREACH(dcl, &s->listeners, next) {
1374 if (dcl->ops->dpy_refresh) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001375 dcl->ops->dpy_refresh(dcl);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001376 }
1377 }
1378}
1379
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001380void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1381 int dst_x, int dst_y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001382{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001383 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001384 struct DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001385
1386 if (con != active_console) {
1387 return;
1388 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001389 QLIST_FOREACH(dcl, &s->listeners, next) {
1390 if (dcl->ops->dpy_gfx_copy) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001391 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001392 } else { /* TODO */
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001393 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001394 }
1395 }
1396}
1397
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001398void dpy_text_cursor(QemuConsole *con, int x, int y)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001399{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001400 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001401 struct DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001402
1403 if (con != active_console) {
1404 return;
1405 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001406 QLIST_FOREACH(dcl, &s->listeners, next) {
1407 if (dcl->ops->dpy_text_cursor) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001408 dcl->ops->dpy_text_cursor(dcl, x, y);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001409 }
1410 }
1411}
1412
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001413void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001414{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001415 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001416 struct DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001417
1418 if (con != active_console) {
1419 return;
1420 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001421 QLIST_FOREACH(dcl, &s->listeners, next) {
1422 if (dcl->ops->dpy_text_update) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001423 dcl->ops->dpy_text_update(dcl, x, y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001424 }
1425 }
1426}
1427
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001428void dpy_text_resize(QemuConsole *con, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001429{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001430 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001431 struct DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001432
1433 if (con != active_console) {
1434 return;
1435 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001436 QLIST_FOREACH(dcl, &s->listeners, next) {
1437 if (dcl->ops->dpy_text_resize) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001438 dcl->ops->dpy_text_resize(dcl, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001439 }
1440 }
1441}
1442
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001443void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001444{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001445 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001446 struct DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001447
1448 if (con != active_console) {
1449 return;
1450 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001451 QLIST_FOREACH(dcl, &s->listeners, next) {
1452 if (dcl->ops->dpy_mouse_set) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001453 dcl->ops->dpy_mouse_set(dcl, x, y, on);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001454 }
1455 }
1456}
1457
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001458void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001459{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001460 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001461 struct DisplayChangeListener *dcl;
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001462
1463 if (con != active_console) {
1464 return;
1465 }
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001466 QLIST_FOREACH(dcl, &s->listeners, next) {
1467 if (dcl->ops->dpy_cursor_define) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001468 dcl->ops->dpy_cursor_define(dcl, cursor);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001469 }
1470 }
1471}
1472
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001473bool dpy_cursor_define_supported(QemuConsole *con)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001474{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001475 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001476 struct DisplayChangeListener *dcl;
1477 QLIST_FOREACH(dcl, &s->listeners, next) {
1478 if (dcl->ops->dpy_cursor_define) {
1479 return true;
1480 }
1481 }
1482 return false;
1483}
1484
Paolo Bonzini98b50082010-02-11 00:29:57 +01001485/***********************************************************/
1486/* register display */
1487
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001488/* console.c internal use only */
1489static DisplayState *get_alloc_displaystate(void)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001490{
1491 if (!display_state) {
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001492 display_state = g_new0(DisplayState, 1);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001493 }
1494 return display_state;
1495}
1496
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001497/*
1498 * Called by main(), after creating QemuConsoles
1499 * and before initializing ui (sdl/vnc/...).
1500 */
1501DisplayState *init_displaystate(void)
1502{
1503 int i;
1504
1505 if (!display_state) {
1506 display_state = g_new0(DisplayState, 1);
1507 }
1508
1509 for (i = 0; i < nb_consoles; i++) {
1510 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1511 consoles[i]->ds == NULL) {
1512 text_console_do_init(consoles[i]->chr, display_state);
1513 }
1514 }
1515
1516 return display_state;
1517}
1518
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001519QemuConsole *graphic_console_init(const GraphicHwOps *hw_ops,
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001520 void *opaque)
bellarde7f0ad52004-07-14 17:28:59 +00001521{
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001522 int width = 640;
1523 int height = 480;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001524 QemuConsole *s;
aliguori3023f332009-01-16 19:04:14 +00001525 DisplayState *ds;
aurel32f0f2f972009-01-16 21:13:49 +00001526
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001527 ds = get_alloc_displaystate();
Gerd Hoffmann437fe102013-03-07 16:04:52 +01001528 trace_console_gfx_new();
thsaf3a9032007-07-11 23:14:59 +00001529 s = new_console(ds, GRAPHIC_CONSOLE);
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001530 s->hw_ops = hw_ops;
pbrook95219892006-04-09 01:06:34 +00001531 s->hw = opaque;
aliguori3023f332009-01-16 19:04:14 +00001532
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001533 s->surface = qemu_create_displaysurface(width, height);
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001534 return s;
pbrook95219892006-04-09 01:06:34 +00001535}
1536
1537int is_graphic_console(void)
1538{
balrog4d3b6f62008-02-10 16:33:14 +00001539 return active_console && active_console->console_type == GRAPHIC_CONSOLE;
bellarde7f0ad52004-07-14 17:28:59 +00001540}
1541
balrogc21bbcf2008-09-24 03:32:33 +00001542int is_fixedsize_console(void)
1543{
1544 return active_console && active_console->console_type != TEXT_CONSOLE;
1545}
1546
Paolo Bonzini41048332010-12-23 13:42:52 +01001547static void text_console_set_echo(CharDriverState *chr, bool echo)
1548{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001549 QemuConsole *s = chr->opaque;
Paolo Bonzini41048332010-12-23 13:42:52 +01001550
1551 s->echo = echo;
1552}
1553
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001554static void text_console_update_cursor(void *opaque)
1555{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001556 QemuConsole *s = opaque;
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001557
1558 s->cursor_visible_phase = !s->cursor_visible_phase;
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +01001559 graphic_hw_invalidate(s);
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001560 qemu_mod_timer(s->cursor_timer,
1561 qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
1562}
1563
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001564static const GraphicHwOps text_console_ops = {
1565 .invalidate = text_console_invalidate,
1566 .text_update = text_console_update,
1567};
1568
Paolo Bonzini44b37b92010-12-23 13:42:53 +01001569static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
bellarde7f0ad52004-07-14 17:28:59 +00001570{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001571 QemuConsole *s;
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001572 int g_width = 80 * FONT_WIDTH;
1573 int g_height = 24 * FONT_HEIGHT;
pbrook6d6f7c22006-03-11 15:35:30 +00001574
Paolo Bonzini491e1142010-12-23 13:42:51 +01001575 s = chr->opaque;
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02001576
bellarde7f0ad52004-07-14 17:28:59 +00001577 chr->chr_write = console_puts;
bellard6fcfafb2004-08-01 21:48:30 +00001578
bellarde15d7372006-06-25 16:26:29 +00001579 s->out_fifo.buf = s->out_fifo_buf;
1580 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001581 s->kbd_timer = qemu_new_timer_ms(rt_clock, kbd_send_chars, s);
aliguori3023f332009-01-16 19:04:14 +00001582 s->ds = ds;
ths3b46e622007-09-17 08:09:54 +00001583
bellarde7f0ad52004-07-14 17:28:59 +00001584 s->y_displayed = 0;
1585 s->y_base = 0;
1586 s->total_height = DEFAULT_BACKSCROLL;
1587 s->x = 0;
1588 s->y = 0;
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001589 if (!s->surface) {
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001590 if (active_console && active_console->surface) {
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001591 g_width = surface_width(active_console->surface);
1592 g_height = surface_height(active_console->surface);
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001593 }
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001594 s->surface = qemu_create_displaysurface(g_width, g_height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001595 }
pbrook6d6f7c22006-03-11 15:35:30 +00001596
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001597 s->cursor_timer =
1598 qemu_new_timer_ms(rt_clock, text_console_update_cursor, s);
1599
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001600 s->hw_ops = &text_console_ops;
balrog4d3b6f62008-02-10 16:33:14 +00001601 s->hw = s;
1602
pbrook6d6f7c22006-03-11 15:35:30 +00001603 /* Set text attribute defaults */
1604 s->t_attrib_default.bold = 0;
1605 s->t_attrib_default.uline = 0;
1606 s->t_attrib_default.blink = 0;
1607 s->t_attrib_default.invers = 0;
1608 s->t_attrib_default.unvisible = 0;
1609 s->t_attrib_default.fgcol = COLOR_WHITE;
1610 s->t_attrib_default.bgcol = COLOR_BLACK;
pbrook6d6f7c22006-03-11 15:35:30 +00001611 /* set current text attributes to default */
1612 s->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +00001613 text_console_resize(s);
1614
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01001615 if (chr->label) {
1616 char msg[128];
1617 int len;
1618
Gerd Hoffmann735ba582009-12-08 13:11:40 +01001619 s->t_attrib.bgcol = COLOR_BLUE;
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01001620 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
1621 console_puts(chr, (uint8_t*)msg, len);
Gerd Hoffmann735ba582009-12-08 13:11:40 +01001622 s->t_attrib = s->t_attrib_default;
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01001623 }
1624
Hans de Goedefee204f2013-03-26 11:07:54 +01001625 qemu_chr_be_generic_open(chr);
aurel32ceecf1d2009-01-18 14:08:04 +00001626 if (chr->init)
1627 chr->init(chr);
bellarde7f0ad52004-07-14 17:28:59 +00001628}
pbrookc60e08d2008-07-01 16:24:38 +00001629
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001630static CharDriverState *text_console_init(ChardevVC *vc)
aliguori2796dae2009-01-16 20:23:27 +00001631{
1632 CharDriverState *chr;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001633 QemuConsole *s;
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001634 unsigned width = 0;
1635 unsigned height = 0;
aliguori2796dae2009-01-16 20:23:27 +00001636
Anthony Liguori7267c092011-08-20 22:09:37 -05001637 chr = g_malloc0(sizeof(CharDriverState));
aliguori2796dae2009-01-16 20:23:27 +00001638
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001639 if (vc->has_width) {
1640 width = vc->width;
1641 } else if (vc->has_cols) {
1642 width = vc->cols * FONT_WIDTH;
1643 }
Paolo Bonzini491e1142010-12-23 13:42:51 +01001644
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001645 if (vc->has_height) {
1646 height = vc->height;
1647 } else if (vc->has_rows) {
1648 height = vc->rows * FONT_HEIGHT;
1649 }
Paolo Bonzini491e1142010-12-23 13:42:51 +01001650
Gerd Hoffmann437fe102013-03-07 16:04:52 +01001651 trace_console_txt_new(width, height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001652 if (width == 0 || height == 0) {
1653 s = new_console(NULL, TEXT_CONSOLE);
1654 } else {
1655 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE);
Gerd Hoffmann36671fb2013-03-13 10:14:52 +01001656 s->surface = qemu_create_displaysurface(width, height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001657 }
1658
1659 if (!s) {
Stefan Weil5354d082011-10-02 18:53:09 +02001660 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001661 return NULL;
Paolo Bonzini491e1142010-12-23 13:42:51 +01001662 }
1663
1664 s->chr = chr;
Paolo Bonzini491e1142010-12-23 13:42:51 +01001665 chr->opaque = s;
Paolo Bonzini41048332010-12-23 13:42:52 +01001666 chr->chr_set_echo = text_console_set_echo;
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001667
1668 if (display_state) {
1669 text_console_do_init(chr, display_state);
1670 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001671 return chr;
aliguori2796dae2009-01-16 20:23:27 +00001672}
1673
Anthony Liguorid82831d2013-02-20 07:43:19 -06001674static VcHandler *vc_handler = text_console_init;
1675
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001676CharDriverState *vc_init(ChardevVC *vc)
Anthony Liguorid82831d2013-02-20 07:43:19 -06001677{
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001678 return vc_handler(vc);
Anthony Liguorid82831d2013-02-20 07:43:19 -06001679}
1680
1681void register_vc_handler(VcHandler *handler)
1682{
1683 vc_handler = handler;
1684}
1685
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001686void qemu_console_resize(QemuConsole *s, int width, int height)
pbrookc60e08d2008-07-01 16:24:38 +00001687{
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001688 DisplaySurface *surface;
1689
1690 assert(s->console_type == GRAPHIC_CONSOLE);
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001691 surface = qemu_create_displaysurface(width, height);
1692 dpy_gfx_replace_surface(s, surface);
pbrookc60e08d2008-07-01 16:24:38 +00001693}
balrog38334f72008-09-24 02:21:24 +00001694
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001695void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
aliguori3023f332009-01-16 19:04:14 +00001696 int dst_x, int dst_y, int w, int h)
balrogc21bbcf2008-09-24 03:32:33 +00001697{
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001698 assert(con->console_type == GRAPHIC_CONSOLE);
1699 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
balrog38334f72008-09-24 02:21:24 +00001700}
aliguori7d957bd2009-01-15 22:14:11 +00001701
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001702DisplaySurface *qemu_console_surface(QemuConsole *console)
1703{
Gerd Hoffmann321f0482013-03-12 14:39:22 +01001704 return console->surface;
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001705}
1706
1707DisplayState *qemu_console_displaystate(QemuConsole *console)
1708{
1709 return console->ds;
1710}
1711
malc0da2ea12009-01-23 19:56:19 +00001712PixelFormat qemu_different_endianness_pixelformat(int bpp)
aliguori7d957bd2009-01-15 22:14:11 +00001713{
1714 PixelFormat pf;
1715
1716 memset(&pf, 0x00, sizeof(PixelFormat));
1717
1718 pf.bits_per_pixel = bpp;
BALATON Zoltanfeadf1a2012-08-22 17:19:42 +02001719 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
aliguori7d957bd2009-01-15 22:14:11 +00001720 pf.depth = bpp == 32 ? 24 : bpp;
1721
1722 switch (bpp) {
malc0da2ea12009-01-23 19:56:19 +00001723 case 24:
1724 pf.rmask = 0x000000FF;
1725 pf.gmask = 0x0000FF00;
1726 pf.bmask = 0x00FF0000;
1727 pf.rmax = 255;
1728 pf.gmax = 255;
1729 pf.bmax = 255;
1730 pf.rshift = 0;
1731 pf.gshift = 8;
1732 pf.bshift = 16;
aliguori90a1e3c2009-01-26 15:37:30 +00001733 pf.rbits = 8;
1734 pf.gbits = 8;
1735 pf.bbits = 8;
aliguori7d957bd2009-01-15 22:14:11 +00001736 break;
malc0da2ea12009-01-23 19:56:19 +00001737 case 32:
1738 pf.rmask = 0x0000FF00;
1739 pf.gmask = 0x00FF0000;
1740 pf.bmask = 0xFF000000;
1741 pf.amask = 0x00000000;
1742 pf.amax = 255;
1743 pf.rmax = 255;
1744 pf.gmax = 255;
1745 pf.bmax = 255;
1746 pf.ashift = 0;
1747 pf.rshift = 8;
1748 pf.gshift = 16;
1749 pf.bshift = 24;
aliguori90a1e3c2009-01-26 15:37:30 +00001750 pf.rbits = 8;
1751 pf.gbits = 8;
1752 pf.bbits = 8;
1753 pf.abits = 8;
malc0da2ea12009-01-23 19:56:19 +00001754 break;
1755 default:
1756 break;
1757 }
1758 return pf;
1759}
1760
1761PixelFormat qemu_default_pixelformat(int bpp)
1762{
1763 PixelFormat pf;
1764
1765 memset(&pf, 0x00, sizeof(PixelFormat));
1766
1767 pf.bits_per_pixel = bpp;
BALATON Zoltanfeadf1a2012-08-22 17:19:42 +02001768 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
malc0da2ea12009-01-23 19:56:19 +00001769 pf.depth = bpp == 32 ? 24 : bpp;
1770
1771 switch (bpp) {
Gerd Hoffmannb6278082010-05-21 11:59:14 +02001772 case 15:
1773 pf.bits_per_pixel = 16;
Gerd Hoffmannb6278082010-05-21 11:59:14 +02001774 pf.rmask = 0x00007c00;
1775 pf.gmask = 0x000003E0;
1776 pf.bmask = 0x0000001F;
1777 pf.rmax = 31;
1778 pf.gmax = 31;
1779 pf.bmax = 31;
1780 pf.rshift = 10;
1781 pf.gshift = 5;
1782 pf.bshift = 0;
1783 pf.rbits = 5;
1784 pf.gbits = 5;
1785 pf.bbits = 5;
1786 break;
aliguori7d957bd2009-01-15 22:14:11 +00001787 case 16:
1788 pf.rmask = 0x0000F800;
1789 pf.gmask = 0x000007E0;
1790 pf.bmask = 0x0000001F;
1791 pf.rmax = 31;
1792 pf.gmax = 63;
1793 pf.bmax = 31;
1794 pf.rshift = 11;
1795 pf.gshift = 5;
1796 pf.bshift = 0;
aliguori90a1e3c2009-01-26 15:37:30 +00001797 pf.rbits = 5;
1798 pf.gbits = 6;
1799 pf.bbits = 5;
aliguori7d957bd2009-01-15 22:14:11 +00001800 break;
1801 case 24:
aliguori7d957bd2009-01-15 22:14:11 +00001802 pf.rmask = 0x00FF0000;
1803 pf.gmask = 0x0000FF00;
1804 pf.bmask = 0x000000FF;
1805 pf.rmax = 255;
1806 pf.gmax = 255;
1807 pf.bmax = 255;
1808 pf.rshift = 16;
1809 pf.gshift = 8;
1810 pf.bshift = 0;
aliguori90a1e3c2009-01-26 15:37:30 +00001811 pf.rbits = 8;
1812 pf.gbits = 8;
1813 pf.bbits = 8;
Markus Armbruster0eba62e2011-11-22 12:56:10 +01001814 break;
malc0da2ea12009-01-23 19:56:19 +00001815 case 32:
1816 pf.rmask = 0x00FF0000;
1817 pf.gmask = 0x0000FF00;
1818 pf.bmask = 0x000000FF;
malc0da2ea12009-01-23 19:56:19 +00001819 pf.rmax = 255;
1820 pf.gmax = 255;
1821 pf.bmax = 255;
malc0da2ea12009-01-23 19:56:19 +00001822 pf.rshift = 16;
1823 pf.gshift = 8;
1824 pf.bshift = 0;
aliguori90a1e3c2009-01-26 15:37:30 +00001825 pf.rbits = 8;
1826 pf.gbits = 8;
1827 pf.bbits = 8;
aliguori7d957bd2009-01-15 22:14:11 +00001828 break;
1829 default:
1830 break;
1831 }
1832 return pf;
1833}
Anthony Liguori01f45d92013-03-05 23:21:32 +05301834
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001835static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
1836 Error **errp)
1837{
1838 int val;
1839
1840 backend->vc = g_new0(ChardevVC, 1);
1841
1842 val = qemu_opt_get_number(opts, "width", 0);
1843 if (val != 0) {
1844 backend->vc->has_width = true;
1845 backend->vc->width = val;
1846 }
1847
1848 val = qemu_opt_get_number(opts, "height", 0);
1849 if (val != 0) {
1850 backend->vc->has_height = true;
1851 backend->vc->height = val;
1852 }
1853
1854 val = qemu_opt_get_number(opts, "cols", 0);
1855 if (val != 0) {
1856 backend->vc->has_cols = true;
1857 backend->vc->cols = val;
1858 }
1859
1860 val = qemu_opt_get_number(opts, "rows", 0);
1861 if (val != 0) {
1862 backend->vc->has_rows = true;
1863 backend->vc->rows = val;
1864 }
1865}
1866
Anthony Liguori01f45d92013-03-05 23:21:32 +05301867static void register_types(void)
1868{
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001869 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC,
1870 qemu_chr_parse_vc);
Anthony Liguori01f45d92013-03-05 23:21:32 +05301871}
1872
1873type_init(register_types);