blob: e100593ffdd448b18a44e36e36e1c7c4eb96c680 [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 Hoffmann76ffb0b2012-09-28 13:24:17 +0200119
pbrook95219892006-04-09 01:06:34 +0000120 /* Graphic console state. */
121 vga_hw_update_ptr hw_update;
122 vga_hw_invalidate_ptr hw_invalidate;
123 vga_hw_screen_dump_ptr hw_screen_dump;
balrog4d3b6f62008-02-10 16:33:14 +0000124 vga_hw_text_update_ptr hw_text_update;
pbrook95219892006-04-09 01:06:34 +0000125 void *hw;
bellarde7f0ad52004-07-14 17:28:59 +0000126 int g_width, g_height;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200127
128 /* Text console state */
bellarde7f0ad52004-07-14 17:28:59 +0000129 int width;
130 int height;
131 int total_height;
132 int backscroll_height;
bellarde7f0ad52004-07-14 17:28:59 +0000133 int x, y;
thsadb47962007-01-16 23:02:36 +0000134 int x_saved, y_saved;
bellarde7f0ad52004-07-14 17:28:59 +0000135 int y_displayed;
136 int y_base;
pbrook6d6f7c22006-03-11 15:35:30 +0000137 TextAttributes t_attrib_default; /* default text attributes */
138 TextAttributes t_attrib; /* currently active text attributes */
bellarde7f0ad52004-07-14 17:28:59 +0000139 TextCell *cells;
balrog4d3b6f62008-02-10 16:33:14 +0000140 int text_x[2], text_y[2], cursor_invalidate;
Paolo Bonzini41048332010-12-23 13:42:52 +0100141 int echo;
Jan Kiszkabf1bed82012-07-10 22:00:55 +0200142 bool cursor_visible_phase;
143 QEMUTimer *cursor_timer;
bellarde7f0ad52004-07-14 17:28:59 +0000144
pbrook14778c22009-01-21 03:02:52 +0000145 int update_x0;
146 int update_y0;
147 int update_x1;
148 int update_y1;
149
bellarde7f0ad52004-07-14 17:28:59 +0000150 enum TTYState state;
151 int esc_params[MAX_ESC_PARAMS];
152 int nb_esc_params;
153
pbrooke5b0bc42007-01-27 23:46:43 +0000154 CharDriverState *chr;
bellarde15d7372006-06-25 16:26:29 +0000155 /* fifo for key pressed */
156 QEMUFIFO out_fifo;
157 uint8_t out_fifo_buf[16];
158 QEMUTimer *kbd_timer;
bellarde7f0ad52004-07-14 17:28:59 +0000159};
160
Paolo Bonzini98b50082010-02-11 00:29:57 +0100161static DisplayState *display_state;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200162static QemuConsole *active_console;
163static QemuConsole *consoles[MAX_CONSOLES];
bellarde7f0ad52004-07-14 17:28:59 +0000164static int nb_consoles = 0;
165
Gerd Hoffmann64840c62013-03-07 17:08:29 +0100166static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
167
pbrook95219892006-04-09 01:06:34 +0000168void vga_hw_update(void)
169{
thsadb47962007-01-16 23:02:36 +0000170 if (active_console && active_console->hw_update)
pbrook95219892006-04-09 01:06:34 +0000171 active_console->hw_update(active_console->hw);
172}
173
174void vga_hw_invalidate(void)
175{
Gerd Hoffmann26572b82010-05-20 15:23:06 +0200176 if (active_console && active_console->hw_invalidate)
pbrook95219892006-04-09 01:06:34 +0000177 active_console->hw_invalidate(active_console->hw);
178}
179
Luiz Capitulinoad39cf62012-05-24 13:48:23 -0300180void qmp_screendump(const char *filename, Error **errp)
pbrook95219892006-04-09 01:06:34 +0000181{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200182 QemuConsole *previous_active_console;
Gerd Hoffmann45efb162012-02-24 12:43:45 +0100183 bool cswitch;
balrog8571c052008-07-19 13:04:26 +0000184
185 previous_active_console = active_console;
Gerd Hoffmann45efb162012-02-24 12:43:45 +0100186 cswitch = previous_active_console && previous_active_console->index != 0;
Jan Kiszkaf81bdef2011-09-16 00:48:07 +0200187
balrog8571c052008-07-19 13:04:26 +0000188 /* There is currently no way of specifying which screen we want to dump,
aurel327b455222008-09-02 00:09:16 +0000189 so always dump the first one. */
Gerd Hoffmann45efb162012-02-24 12:43:45 +0100190 if (cswitch) {
191 console_select(0);
192 }
Jan Kiszkaf81bdef2011-09-16 00:48:07 +0200193 if (consoles[0] && consoles[0]->hw_screen_dump) {
Luiz Capitulinoad39cf62012-05-24 13:48:23 -0300194 consoles[0]->hw_screen_dump(consoles[0]->hw, filename, cswitch, errp);
Gerd Hoffmann16735102012-02-24 12:43:44 +0100195 } else {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100196 error_setg(errp, "device doesn't support screendump");
Jan Kiszkaf81bdef2011-09-16 00:48:07 +0200197 }
198
Gerd Hoffmann45efb162012-02-24 12:43:45 +0100199 if (cswitch) {
Alexander Graf33bcd982011-11-18 16:41:59 +0100200 console_select(previous_active_console->index);
201 }
pbrook95219892006-04-09 01:06:34 +0000202}
203
Anthony Liguoric227f092009-10-01 16:12:16 -0500204void vga_hw_text_update(console_ch_t *chardata)
balrog4d3b6f62008-02-10 16:33:14 +0000205{
206 if (active_console && active_console->hw_text_update)
207 active_console->hw_text_update(active_console->hw, chardata);
208}
209
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100210static void vga_fill_rect(QemuConsole *con,
211 int posx, int posy, int width, int height,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100212 pixman_color_t color)
bellarde7f0ad52004-07-14 17:28:59 +0000213{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100214 DisplaySurface *surface = qemu_console_surface(con);
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100215 pixman_rectangle16_t rect = {
216 .x = posx, .y = posy, .width = width, .height = height
217 };
ths3b46e622007-09-17 08:09:54 +0000218
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100219 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100220 &color, 1, &rect);
bellarde7f0ad52004-07-14 17:28:59 +0000221}
222
223/* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100224static void vga_bitblt(QemuConsole *con,
225 int xs, int ys, int xd, int yd, int w, int h)
bellarde7f0ad52004-07-14 17:28:59 +0000226{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100227 DisplaySurface *surface = qemu_console_surface(con);
bellarde7f0ad52004-07-14 17:28:59 +0000228
Gerd Hoffmann68db6dc2013-03-06 15:43:23 +0100229 pixman_image_composite(PIXMAN_OP_SRC,
230 surface->image, NULL, surface->image,
231 xs, ys, 0, 0, xd, yd, w, h);
bellarde7f0ad52004-07-14 17:28:59 +0000232}
233
234/***********************************************************/
235/* basic char display */
236
237#define FONT_HEIGHT 16
238#define FONT_WIDTH 8
239
240#include "vgafont.h"
241
Devin J. Pohlydf00bed2011-09-07 15:44:36 -0400242#ifndef CONFIG_CURSES
pbrook6d6f7c22006-03-11 15:35:30 +0000243enum color_names {
244 COLOR_BLACK = 0,
245 COLOR_RED = 1,
246 COLOR_GREEN = 2,
247 COLOR_YELLOW = 3,
248 COLOR_BLUE = 4,
249 COLOR_MAGENTA = 5,
250 COLOR_CYAN = 6,
251 COLOR_WHITE = 7
252};
Devin J. Pohlydf00bed2011-09-07 15:44:36 -0400253#endif
pbrook6d6f7c22006-03-11 15:35:30 +0000254
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100255#define QEMU_RGB(r, g, b) \
256 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
257
258static const pixman_color_t color_table_rgb[2][8] = {
pbrook6d6f7c22006-03-11 15:35:30 +0000259 { /* dark */
bellard26489842006-06-25 17:37:36 +0000260 QEMU_RGB(0x00, 0x00, 0x00), /* black */
261 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
262 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
263 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
264 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
265 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
266 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
267 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
pbrook6d6f7c22006-03-11 15:35:30 +0000268 },
269 { /* bright */
bellard26489842006-06-25 17:37:36 +0000270 QEMU_RGB(0x00, 0x00, 0x00), /* black */
271 QEMU_RGB(0xff, 0x00, 0x00), /* red */
272 QEMU_RGB(0x00, 0xff, 0x00), /* green */
273 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
274 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
275 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
276 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
277 QEMU_RGB(0xff, 0xff, 0xff), /* white */
pbrook6d6f7c22006-03-11 15:35:30 +0000278 }
bellarde7f0ad52004-07-14 17:28:59 +0000279};
280
pbrook6d6f7c22006-03-11 15:35:30 +0000281#ifdef DEBUG_CONSOLE
282static void console_print_text_attributes(TextAttributes *t_attrib, char ch)
283{
284 if (t_attrib->bold) {
285 printf("b");
286 } else {
287 printf(" ");
288 }
289 if (t_attrib->uline) {
290 printf("u");
291 } else {
292 printf(" ");
293 }
294 if (t_attrib->blink) {
295 printf("l");
296 } else {
297 printf(" ");
298 }
299 if (t_attrib->invers) {
300 printf("i");
301 } else {
302 printf(" ");
303 }
304 if (t_attrib->unvisible) {
305 printf("n");
306 } else {
307 printf(" ");
308 }
309
310 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib->fgcol, t_attrib->bgcol, ch, ch);
311}
312#endif
bellarde7f0ad52004-07-14 17:28:59 +0000313
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100314static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
pbrook6d6f7c22006-03-11 15:35:30 +0000315 TextAttributes *t_attrib)
bellarde7f0ad52004-07-14 17:28:59 +0000316{
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100317 static pixman_image_t *glyphs[256];
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100318 DisplaySurface *surface = qemu_console_surface(s);
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100319 pixman_color_t fgcol, bgcol;
pbrook6d6f7c22006-03-11 15:35:30 +0000320
321 if (t_attrib->invers) {
Gerd Hoffmanncf6f0542013-03-06 09:50:51 +0100322 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
323 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
pbrook6d6f7c22006-03-11 15:35:30 +0000324 } else {
Gerd Hoffmanncf6f0542013-03-06 09:50:51 +0100325 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
326 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
pbrook6d6f7c22006-03-11 15:35:30 +0000327 }
bellarde7f0ad52004-07-14 17:28:59 +0000328
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100329 if (!glyphs[ch]) {
330 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
bellarde7f0ad52004-07-14 17:28:59 +0000331 }
Gerd Hoffmann7d6ba012013-03-06 15:44:10 +0100332 qemu_pixman_glyph_render(glyphs[ch], surface->image,
Gerd Hoffmanne27bd652013-03-08 08:43:24 +0100333 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
bellarde7f0ad52004-07-14 17:28:59 +0000334}
335
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200336static void text_console_resize(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000337{
338 TextCell *cells, *c, *c1;
339 int w1, x, y, last_width;
340
341 last_width = s->width;
342 s->width = s->g_width / FONT_WIDTH;
343 s->height = s->g_height / FONT_HEIGHT;
344
345 w1 = last_width;
346 if (s->width < w1)
347 w1 = s->width;
348
Anthony Liguori7267c092011-08-20 22:09:37 -0500349 cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
bellarde7f0ad52004-07-14 17:28:59 +0000350 for(y = 0; y < s->total_height; y++) {
351 c = &cells[y * s->width];
352 if (w1 > 0) {
353 c1 = &s->cells[y * last_width];
354 for(x = 0; x < w1; x++) {
355 *c++ = *c1++;
356 }
357 }
358 for(x = w1; x < s->width; x++) {
359 c->ch = ' ';
pbrook6d6f7c22006-03-11 15:35:30 +0000360 c->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +0000361 c++;
362 }
363 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500364 g_free(s->cells);
bellarde7f0ad52004-07-14 17:28:59 +0000365 s->cells = cells;
366}
367
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200368static inline void text_update_xy(QemuConsole *s, int x, int y)
balrog4d3b6f62008-02-10 16:33:14 +0000369{
370 s->text_x[0] = MIN(s->text_x[0], x);
371 s->text_x[1] = MAX(s->text_x[1], x);
372 s->text_y[0] = MIN(s->text_y[0], y);
373 s->text_y[1] = MAX(s->text_y[1], y);
374}
375
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200376static void invalidate_xy(QemuConsole *s, int x, int y)
pbrook14778c22009-01-21 03:02:52 +0000377{
378 if (s->update_x0 > x * FONT_WIDTH)
379 s->update_x0 = x * FONT_WIDTH;
380 if (s->update_y0 > y * FONT_HEIGHT)
381 s->update_y0 = y * FONT_HEIGHT;
382 if (s->update_x1 < (x + 1) * FONT_WIDTH)
383 s->update_x1 = (x + 1) * FONT_WIDTH;
384 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
385 s->update_y1 = (y + 1) * FONT_HEIGHT;
386}
387
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200388static void update_xy(QemuConsole *s, int x, int y)
bellarde7f0ad52004-07-14 17:28:59 +0000389{
390 TextCell *c;
391 int y1, y2;
392
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100393 if (s != active_console) {
394 return;
395 }
balrog4d3b6f62008-02-10 16:33:14 +0000396
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100397 if (s->ds->have_text) {
398 text_update_xy(s, x, y);
399 }
400
401 if (s->ds->have_gfx) {
bellarde7f0ad52004-07-14 17:28:59 +0000402 y1 = (s->y_base + y) % s->total_height;
403 y2 = y1 - s->y_displayed;
404 if (y2 < 0)
405 y2 += s->total_height;
406 if (y2 < s->height) {
407 c = &s->cells[y1 * s->width + x];
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100408 vga_putcharxy(s, x, y2, c->ch,
pbrook6d6f7c22006-03-11 15:35:30 +0000409 &(c->t_attrib));
pbrook14778c22009-01-21 03:02:52 +0000410 invalidate_xy(s, x, y2);
bellarde7f0ad52004-07-14 17:28:59 +0000411 }
412 }
413}
414
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200415static void console_show_cursor(QemuConsole *s, int show)
bellarde7f0ad52004-07-14 17:28:59 +0000416{
417 TextCell *c;
418 int y, y1;
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100419 int x = s->x;
bellarde7f0ad52004-07-14 17:28:59 +0000420
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100421 if (s != active_console) {
422 return;
423 }
balrog4d3b6f62008-02-10 16:33:14 +0000424
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100425 if (s->ds->have_text) {
426 s->cursor_invalidate = 1;
427 }
balrog4d3b6f62008-02-10 16:33:14 +0000428
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100429 if (s->ds->have_gfx) {
thsed8276a2007-02-10 22:37:56 +0000430 if (x >= s->width) {
431 x = s->width - 1;
432 }
bellarde7f0ad52004-07-14 17:28:59 +0000433 y1 = (s->y_base + s->y) % s->total_height;
434 y = y1 - s->y_displayed;
435 if (y < 0)
436 y += s->total_height;
437 if (y < s->height) {
thsed8276a2007-02-10 22:37:56 +0000438 c = &s->cells[y1 * s->width + x];
Jan Kiszkabf1bed82012-07-10 22:00:55 +0200439 if (show && s->cursor_visible_phase) {
pbrook6d6f7c22006-03-11 15:35:30 +0000440 TextAttributes t_attrib = s->t_attrib_default;
441 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100442 vga_putcharxy(s, x, y, c->ch, &t_attrib);
bellarde7f0ad52004-07-14 17:28:59 +0000443 } else {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100444 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
bellarde7f0ad52004-07-14 17:28:59 +0000445 }
pbrook14778c22009-01-21 03:02:52 +0000446 invalidate_xy(s, x, y);
bellarde7f0ad52004-07-14 17:28:59 +0000447 }
448 }
449}
450
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200451static void console_refresh(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000452{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100453 DisplaySurface *surface = qemu_console_surface(s);
bellarde7f0ad52004-07-14 17:28:59 +0000454 TextCell *c;
455 int x, y, y1;
456
ths5fafdf22007-09-16 21:08:06 +0000457 if (s != active_console)
bellarde7f0ad52004-07-14 17:28:59 +0000458 return;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200459
460 if (s->ds->have_text) {
balrog4d3b6f62008-02-10 16:33:14 +0000461 s->text_x[0] = 0;
462 s->text_y[0] = 0;
463 s->text_x[1] = s->width - 1;
464 s->text_y[1] = s->height - 1;
465 s->cursor_invalidate = 1;
balrog4d3b6f62008-02-10 16:33:14 +0000466 }
bellarde7f0ad52004-07-14 17:28:59 +0000467
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200468 if (s->ds->have_gfx) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100469 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
Gerd Hoffmanncf6f0542013-03-06 09:50:51 +0100470 color_table_rgb[0][COLOR_BLACK]);
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200471 y1 = s->y_displayed;
472 for (y = 0; y < s->height; y++) {
473 c = s->cells + y1 * s->width;
474 for (x = 0; x < s->width; x++) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100475 vga_putcharxy(s, x, y, c->ch,
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200476 &(c->t_attrib));
477 c++;
478 }
479 if (++y1 == s->total_height) {
480 y1 = 0;
481 }
bellarde7f0ad52004-07-14 17:28:59 +0000482 }
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200483 console_show_cursor(s, 1);
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100484 dpy_gfx_update(s, 0, 0,
485 surface_width(surface), surface_height(surface));
bellarde7f0ad52004-07-14 17:28:59 +0000486 }
bellarde7f0ad52004-07-14 17:28:59 +0000487}
488
489static void console_scroll(int ydelta)
490{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200491 QemuConsole *s;
bellarde7f0ad52004-07-14 17:28:59 +0000492 int i, y1;
ths3b46e622007-09-17 08:09:54 +0000493
bellarde7f0ad52004-07-14 17:28:59 +0000494 s = active_console;
thsaf3a9032007-07-11 23:14:59 +0000495 if (!s || (s->console_type == GRAPHIC_CONSOLE))
bellarde7f0ad52004-07-14 17:28:59 +0000496 return;
497
498 if (ydelta > 0) {
499 for(i = 0; i < ydelta; i++) {
500 if (s->y_displayed == s->y_base)
501 break;
502 if (++s->y_displayed == s->total_height)
503 s->y_displayed = 0;
504 }
505 } else {
506 ydelta = -ydelta;
507 i = s->backscroll_height;
508 if (i > s->total_height - s->height)
509 i = s->total_height - s->height;
510 y1 = s->y_base - i;
511 if (y1 < 0)
512 y1 += s->total_height;
513 for(i = 0; i < ydelta; i++) {
514 if (s->y_displayed == y1)
515 break;
516 if (--s->y_displayed < 0)
517 s->y_displayed = s->total_height - 1;
518 }
519 }
520 console_refresh(s);
521}
522
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200523static void console_put_lf(QemuConsole *s)
bellarde7f0ad52004-07-14 17:28:59 +0000524{
525 TextCell *c;
526 int x, y1;
527
bellarde7f0ad52004-07-14 17:28:59 +0000528 s->y++;
529 if (s->y >= s->height) {
530 s->y = s->height - 1;
pbrook6d6f7c22006-03-11 15:35:30 +0000531
bellarde7f0ad52004-07-14 17:28:59 +0000532 if (s->y_displayed == s->y_base) {
533 if (++s->y_displayed == s->total_height)
534 s->y_displayed = 0;
535 }
536 if (++s->y_base == s->total_height)
537 s->y_base = 0;
538 if (s->backscroll_height < s->total_height)
539 s->backscroll_height++;
540 y1 = (s->y_base + s->height - 1) % s->total_height;
541 c = &s->cells[y1 * s->width];
542 for(x = 0; x < s->width; x++) {
543 c->ch = ' ';
pbrook6d6f7c22006-03-11 15:35:30 +0000544 c->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +0000545 c++;
546 }
547 if (s == active_console && s->y_displayed == s->y_base) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100548 if (s->ds->have_text) {
balrog4d3b6f62008-02-10 16:33:14 +0000549 s->text_x[0] = 0;
550 s->text_y[0] = 0;
551 s->text_x[1] = s->width - 1;
552 s->text_y[1] = s->height - 1;
balrog4d3b6f62008-02-10 16:33:14 +0000553 }
554
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100555 if (s->ds->have_gfx) {
556 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
557 s->width * FONT_WIDTH,
558 (s->height - 1) * FONT_HEIGHT);
559 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
560 s->width * FONT_WIDTH, FONT_HEIGHT,
561 color_table_rgb[0][s->t_attrib_default.bgcol]);
562 s->update_x0 = 0;
563 s->update_y0 = 0;
564 s->update_x1 = s->width * FONT_WIDTH;
565 s->update_y1 = s->height * FONT_HEIGHT;
566 }
bellarde7f0ad52004-07-14 17:28:59 +0000567 }
568 }
569}
570
pbrook6d6f7c22006-03-11 15:35:30 +0000571/* Set console attributes depending on the current escape codes.
572 * NOTE: I know this code is not very efficient (checking every color for it
573 * self) but it is more readable and better maintainable.
574 */
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200575static void console_handle_escape(QemuConsole *s)
pbrook6d6f7c22006-03-11 15:35:30 +0000576{
577 int i;
578
pbrook6d6f7c22006-03-11 15:35:30 +0000579 for (i=0; i<s->nb_esc_params; i++) {
580 switch (s->esc_params[i]) {
581 case 0: /* reset all console attributes to default */
582 s->t_attrib = s->t_attrib_default;
583 break;
584 case 1:
585 s->t_attrib.bold = 1;
586 break;
587 case 4:
588 s->t_attrib.uline = 1;
589 break;
590 case 5:
591 s->t_attrib.blink = 1;
592 break;
593 case 7:
594 s->t_attrib.invers = 1;
595 break;
596 case 8:
597 s->t_attrib.unvisible = 1;
598 break;
599 case 22:
600 s->t_attrib.bold = 0;
601 break;
602 case 24:
603 s->t_attrib.uline = 0;
604 break;
605 case 25:
606 s->t_attrib.blink = 0;
607 break;
608 case 27:
609 s->t_attrib.invers = 0;
610 break;
611 case 28:
612 s->t_attrib.unvisible = 0;
613 break;
614 /* set foreground color */
615 case 30:
616 s->t_attrib.fgcol=COLOR_BLACK;
617 break;
618 case 31:
619 s->t_attrib.fgcol=COLOR_RED;
620 break;
621 case 32:
622 s->t_attrib.fgcol=COLOR_GREEN;
623 break;
624 case 33:
625 s->t_attrib.fgcol=COLOR_YELLOW;
626 break;
627 case 34:
628 s->t_attrib.fgcol=COLOR_BLUE;
629 break;
630 case 35:
631 s->t_attrib.fgcol=COLOR_MAGENTA;
632 break;
633 case 36:
634 s->t_attrib.fgcol=COLOR_CYAN;
635 break;
636 case 37:
637 s->t_attrib.fgcol=COLOR_WHITE;
638 break;
639 /* set background color */
640 case 40:
641 s->t_attrib.bgcol=COLOR_BLACK;
642 break;
643 case 41:
644 s->t_attrib.bgcol=COLOR_RED;
645 break;
646 case 42:
647 s->t_attrib.bgcol=COLOR_GREEN;
648 break;
649 case 43:
650 s->t_attrib.bgcol=COLOR_YELLOW;
651 break;
652 case 44:
653 s->t_attrib.bgcol=COLOR_BLUE;
654 break;
655 case 45:
656 s->t_attrib.bgcol=COLOR_MAGENTA;
657 break;
658 case 46:
659 s->t_attrib.bgcol=COLOR_CYAN;
660 break;
661 case 47:
662 s->t_attrib.bgcol=COLOR_WHITE;
663 break;
664 }
665 }
666}
667
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200668static void console_clear_xy(QemuConsole *s, int x, int y)
thsadb47962007-01-16 23:02:36 +0000669{
670 int y1 = (s->y_base + y) % s->total_height;
671 TextCell *c = &s->cells[y1 * s->width + x];
672 c->ch = ' ';
673 c->t_attrib = s->t_attrib_default;
thsadb47962007-01-16 23:02:36 +0000674 update_xy(s, x, y);
675}
676
Ian Campbell3eea5492012-09-04 10:26:09 -0500677/* set cursor, checking bounds */
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200678static void set_cursor(QemuConsole *s, int x, int y)
Ian Campbell3eea5492012-09-04 10:26:09 -0500679{
680 if (x < 0) {
681 x = 0;
682 }
683 if (y < 0) {
684 y = 0;
685 }
686 if (y >= s->height) {
687 y = s->height - 1;
688 }
689 if (x >= s->width) {
690 x = s->width - 1;
691 }
692
693 s->x = x;
694 s->y = y;
695}
696
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200697static void console_putchar(QemuConsole *s, int ch)
bellarde7f0ad52004-07-14 17:28:59 +0000698{
699 TextCell *c;
thsadb47962007-01-16 23:02:36 +0000700 int y1, i;
701 int x, y;
bellarde7f0ad52004-07-14 17:28:59 +0000702
703 switch(s->state) {
704 case TTY_STATE_NORM:
705 switch(ch) {
pbrook6d6f7c22006-03-11 15:35:30 +0000706 case '\r': /* carriage return */
bellarde7f0ad52004-07-14 17:28:59 +0000707 s->x = 0;
708 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000709 case '\n': /* newline */
bellarde7f0ad52004-07-14 17:28:59 +0000710 console_put_lf(s);
711 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000712 case '\b': /* backspace */
ths5fafdf22007-09-16 21:08:06 +0000713 if (s->x > 0)
bellarde15d7372006-06-25 16:26:29 +0000714 s->x--;
pbrook6d6f7c22006-03-11 15:35:30 +0000715 break;
716 case '\t': /* tabspace */
717 if (s->x + (8 - (s->x % 8)) > s->width) {
bellardbd468842006-07-14 20:24:31 +0000718 s->x = 0;
pbrook6d6f7c22006-03-11 15:35:30 +0000719 console_put_lf(s);
720 } else {
721 s->x = s->x + (8 - (s->x % 8));
722 }
723 break;
724 case '\a': /* alert aka. bell */
725 /* TODO: has to be implemented */
726 break;
thsadb47962007-01-16 23:02:36 +0000727 case 14:
728 /* SI (shift in), character set 0 (ignored) */
729 break;
730 case 15:
731 /* SO (shift out), character set 1 (ignored) */
732 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000733 case 27: /* esc (introducing an escape sequence) */
bellarde7f0ad52004-07-14 17:28:59 +0000734 s->state = TTY_STATE_ESC;
735 break;
736 default:
thsed8276a2007-02-10 22:37:56 +0000737 if (s->x >= s->width) {
738 /* line wrap */
739 s->x = 0;
740 console_put_lf(s);
thsadb47962007-01-16 23:02:36 +0000741 }
bellarde7f0ad52004-07-14 17:28:59 +0000742 y1 = (s->y_base + s->y) % s->total_height;
743 c = &s->cells[y1 * s->width + s->x];
744 c->ch = ch;
pbrook6d6f7c22006-03-11 15:35:30 +0000745 c->t_attrib = s->t_attrib;
bellarde7f0ad52004-07-14 17:28:59 +0000746 update_xy(s, s->x, s->y);
747 s->x++;
bellarde7f0ad52004-07-14 17:28:59 +0000748 break;
749 }
750 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000751 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
bellarde7f0ad52004-07-14 17:28:59 +0000752 if (ch == '[') {
753 for(i=0;i<MAX_ESC_PARAMS;i++)
754 s->esc_params[i] = 0;
755 s->nb_esc_params = 0;
756 s->state = TTY_STATE_CSI;
757 } else {
758 s->state = TTY_STATE_NORM;
759 }
760 break;
pbrook6d6f7c22006-03-11 15:35:30 +0000761 case TTY_STATE_CSI: /* handle escape sequence parameters */
bellarde7f0ad52004-07-14 17:28:59 +0000762 if (ch >= '0' && ch <= '9') {
763 if (s->nb_esc_params < MAX_ESC_PARAMS) {
Laszlo Ersekc10600a2012-09-17 11:10:03 +0200764 int *param = &s->esc_params[s->nb_esc_params];
765 int digit = (ch - '0');
766
767 *param = (*param <= (INT_MAX - digit) / 10) ?
768 *param * 10 + digit : INT_MAX;
bellarde7f0ad52004-07-14 17:28:59 +0000769 }
770 } else {
Ian Campbell3eea5492012-09-04 10:26:09 -0500771 if (s->nb_esc_params < MAX_ESC_PARAMS)
772 s->nb_esc_params++;
bellarde7f0ad52004-07-14 17:28:59 +0000773 if (ch == ';')
774 break;
thsadb47962007-01-16 23:02:36 +0000775#ifdef DEBUG_CONSOLE
776 fprintf(stderr, "escape sequence CSI%d;%d%c, %d parameters\n",
777 s->esc_params[0], s->esc_params[1], ch, s->nb_esc_params);
778#endif
bellarde7f0ad52004-07-14 17:28:59 +0000779 s->state = TTY_STATE_NORM;
780 switch(ch) {
thsadb47962007-01-16 23:02:36 +0000781 case 'A':
782 /* move cursor up */
783 if (s->esc_params[0] == 0) {
784 s->esc_params[0] = 1;
785 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500786 set_cursor(s, s->x, s->y - s->esc_params[0]);
bellarde7f0ad52004-07-14 17:28:59 +0000787 break;
thsadb47962007-01-16 23:02:36 +0000788 case 'B':
789 /* move cursor down */
790 if (s->esc_params[0] == 0) {
791 s->esc_params[0] = 1;
792 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500793 set_cursor(s, s->x, s->y + s->esc_params[0]);
thsadb47962007-01-16 23:02:36 +0000794 break;
795 case 'C':
796 /* move cursor right */
797 if (s->esc_params[0] == 0) {
798 s->esc_params[0] = 1;
799 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500800 set_cursor(s, s->x + s->esc_params[0], s->y);
thsadb47962007-01-16 23:02:36 +0000801 break;
802 case 'D':
803 /* move cursor left */
804 if (s->esc_params[0] == 0) {
805 s->esc_params[0] = 1;
806 }
Ian Campbell3eea5492012-09-04 10:26:09 -0500807 set_cursor(s, s->x - s->esc_params[0], s->y);
thsadb47962007-01-16 23:02:36 +0000808 break;
809 case 'G':
810 /* move cursor to column */
Ian Campbell3eea5492012-09-04 10:26:09 -0500811 set_cursor(s, s->esc_params[0] - 1, s->y);
thsadb47962007-01-16 23:02:36 +0000812 break;
813 case 'f':
814 case 'H':
815 /* move cursor to row, column */
Ian Campbell3eea5492012-09-04 10:26:09 -0500816 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
thsadb47962007-01-16 23:02:36 +0000817 break;
818 case 'J':
819 switch (s->esc_params[0]) {
820 case 0:
821 /* clear to end of screen */
822 for (y = s->y; y < s->height; y++) {
823 for (x = 0; x < s->width; x++) {
824 if (y == s->y && x < s->x) {
825 continue;
826 }
827 console_clear_xy(s, x, y);
828 }
829 }
830 break;
831 case 1:
832 /* clear from beginning of screen */
833 for (y = 0; y <= s->y; y++) {
834 for (x = 0; x < s->width; x++) {
835 if (y == s->y && x > s->x) {
836 break;
837 }
838 console_clear_xy(s, x, y);
839 }
840 }
841 break;
842 case 2:
843 /* clear entire screen */
844 for (y = 0; y <= s->height; y++) {
845 for (x = 0; x < s->width; x++) {
846 console_clear_xy(s, x, y);
847 }
848 }
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100849 break;
thsadb47962007-01-16 23:02:36 +0000850 }
Markus Armbruster95d8f9f2011-11-22 11:59:07 +0100851 break;
thsadb47962007-01-16 23:02:36 +0000852 case 'K':
853 switch (s->esc_params[0]) {
854 case 0:
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100855 /* clear to eol */
856 for(x = s->x; x < s->width; x++) {
thsadb47962007-01-16 23:02:36 +0000857 console_clear_xy(s, x, s->y);
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100858 }
859 break;
thsadb47962007-01-16 23:02:36 +0000860 case 1:
861 /* clear from beginning of line */
862 for (x = 0; x <= s->x; x++) {
863 console_clear_xy(s, x, s->y);
864 }
865 break;
866 case 2:
867 /* clear entire line */
868 for(x = 0; x < s->width; x++) {
869 console_clear_xy(s, x, s->y);
870 }
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100871 break;
872 }
thsadb47962007-01-16 23:02:36 +0000873 break;
874 case 'm':
Markus Armbrusterf94a9502011-11-22 11:59:06 +0100875 console_handle_escape(s);
876 break;
thsadb47962007-01-16 23:02:36 +0000877 case 'n':
878 /* report cursor position */
879 /* TODO: send ESC[row;colR */
880 break;
881 case 's':
882 /* save cursor position */
883 s->x_saved = s->x;
884 s->y_saved = s->y;
885 break;
886 case 'u':
887 /* restore cursor position */
888 s->x = s->x_saved;
889 s->y = s->y_saved;
890 break;
891 default:
892#ifdef DEBUG_CONSOLE
893 fprintf(stderr, "unhandled escape character '%c'\n", ch);
894#endif
895 break;
896 }
897 break;
bellarde7f0ad52004-07-14 17:28:59 +0000898 }
899 }
900}
901
902void console_select(unsigned int index)
903{
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100904 DisplaySurface *surface;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200905 QemuConsole *s;
pbrook6d6f7c22006-03-11 15:35:30 +0000906
bellarde7f0ad52004-07-14 17:28:59 +0000907 if (index >= MAX_CONSOLES)
908 return;
Gerd Hoffmann437fe102013-03-07 16:04:52 +0100909
910 trace_console_select(index);
Stefan Hajnoczi358664c2010-09-20 14:11:19 +0100911 if (active_console) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +0100912 surface = qemu_console_surface(active_console);
913 active_console->g_width = surface_width(surface);
914 active_console->g_height = surface_height(surface);
Stefan Hajnoczi358664c2010-09-20 14:11:19 +0100915 }
bellarde7f0ad52004-07-14 17:28:59 +0000916 s = consoles[index];
917 if (s) {
aliguori7d957bd2009-01-15 22:14:11 +0000918 DisplayState *ds = s->ds;
Jan Kiszkabf1bed82012-07-10 22:00:55 +0200919
Stefan Weil8bd6b062012-08-17 15:50:44 +0200920 if (active_console && active_console->cursor_timer) {
Jan Kiszkabf1bed82012-07-10 22:00:55 +0200921 qemu_del_timer(active_console->cursor_timer);
922 }
bellarde7f0ad52004-07-14 17:28:59 +0000923 active_console = s;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200924 if (ds->have_gfx) {
Gerd Hoffmannda229ef2013-02-28 10:48:02 +0100925 surface = qemu_create_displaysurface(s->g_width, s->g_height);
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100926 dpy_gfx_replace_surface(s, surface);
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200927 }
928 if (ds->have_text) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100929 dpy_text_resize(s, s->width, s->height);
aliguori68f00992009-01-21 18:59:12 +0000930 }
Jan Kiszkabf1bed82012-07-10 22:00:55 +0200931 if (s->cursor_timer) {
932 qemu_mod_timer(s->cursor_timer,
933 qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
934 }
balrog4d3b6f62008-02-10 16:33:14 +0000935 vga_hw_invalidate();
bellarde7f0ad52004-07-14 17:28:59 +0000936 }
937}
938
939static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
940{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200941 QemuConsole *s = chr->opaque;
bellarde7f0ad52004-07-14 17:28:59 +0000942 int i;
943
pbrook14778c22009-01-21 03:02:52 +0000944 s->update_x0 = s->width * FONT_WIDTH;
945 s->update_y0 = s->height * FONT_HEIGHT;
946 s->update_x1 = 0;
947 s->update_y1 = 0;
bellarde7f0ad52004-07-14 17:28:59 +0000948 console_show_cursor(s, 0);
949 for(i = 0; i < len; i++) {
950 console_putchar(s, buf[i]);
951 }
952 console_show_cursor(s, 1);
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200953 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100954 dpy_gfx_update(s, s->update_x0, s->update_y0,
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200955 s->update_x1 - s->update_x0,
956 s->update_y1 - s->update_y0);
pbrook14778c22009-01-21 03:02:52 +0000957 }
bellarde7f0ad52004-07-14 17:28:59 +0000958 return len;
959}
960
bellarde15d7372006-06-25 16:26:29 +0000961static void kbd_send_chars(void *opaque)
962{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200963 QemuConsole *s = opaque;
bellarde15d7372006-06-25 16:26:29 +0000964 int len;
965 uint8_t buf[16];
ths3b46e622007-09-17 08:09:54 +0000966
Anthony Liguori909cda12011-08-15 11:17:31 -0500967 len = qemu_chr_be_can_write(s->chr);
bellarde15d7372006-06-25 16:26:29 +0000968 if (len > s->out_fifo.count)
969 len = s->out_fifo.count;
970 if (len > 0) {
971 if (len > sizeof(buf))
972 len = sizeof(buf);
973 qemu_fifo_read(&s->out_fifo, buf, len);
Anthony Liguorifa5efcc2011-08-15 11:17:30 -0500974 qemu_chr_be_write(s->chr, buf, len);
bellarde15d7372006-06-25 16:26:29 +0000975 }
976 /* characters are pending: we send them a bit later (XXX:
977 horrible, should change char device API) */
978 if (s->out_fifo.count > 0) {
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100979 qemu_mod_timer(s->kbd_timer, qemu_get_clock_ms(rt_clock) + 1);
bellarde15d7372006-06-25 16:26:29 +0000980 }
981}
982
bellarde7f0ad52004-07-14 17:28:59 +0000983/* called when an ascii key is pressed */
984void kbd_put_keysym(int keysym)
985{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +0200986 QemuConsole *s;
bellarde7f0ad52004-07-14 17:28:59 +0000987 uint8_t buf[16], *q;
988 int c;
989
990 s = active_console;
thsaf3a9032007-07-11 23:14:59 +0000991 if (!s || (s->console_type == GRAPHIC_CONSOLE))
bellarde7f0ad52004-07-14 17:28:59 +0000992 return;
993
994 switch(keysym) {
995 case QEMU_KEY_CTRL_UP:
996 console_scroll(-1);
997 break;
998 case QEMU_KEY_CTRL_DOWN:
999 console_scroll(1);
1000 break;
1001 case QEMU_KEY_CTRL_PAGEUP:
1002 console_scroll(-10);
1003 break;
1004 case QEMU_KEY_CTRL_PAGEDOWN:
1005 console_scroll(10);
1006 break;
1007 default:
bellarde15d7372006-06-25 16:26:29 +00001008 /* convert the QEMU keysym to VT100 key string */
1009 q = buf;
1010 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1011 *q++ = '\033';
1012 *q++ = '[';
1013 c = keysym - 0xe100;
1014 if (c >= 10)
1015 *q++ = '0' + (c / 10);
1016 *q++ = '0' + (c % 10);
1017 *q++ = '~';
1018 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1019 *q++ = '\033';
1020 *q++ = '[';
1021 *q++ = keysym & 0xff;
Paolo Bonzini41048332010-12-23 13:42:52 +01001022 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1023 console_puts(s->chr, (const uint8_t *) "\r", 1);
1024 *q++ = '\n';
bellarde15d7372006-06-25 16:26:29 +00001025 } else {
Paolo Bonzini41048332010-12-23 13:42:52 +01001026 *q++ = keysym;
1027 }
1028 if (s->echo) {
1029 console_puts(s->chr, buf, q - buf);
bellarde15d7372006-06-25 16:26:29 +00001030 }
pbrooke5b0bc42007-01-27 23:46:43 +00001031 if (s->chr->chr_read) {
bellarde15d7372006-06-25 16:26:29 +00001032 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1033 kbd_send_chars(s);
bellarde7f0ad52004-07-14 17:28:59 +00001034 }
1035 break;
1036 }
1037}
1038
balrog4d3b6f62008-02-10 16:33:14 +00001039static void text_console_invalidate(void *opaque)
1040{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001041 QemuConsole *s = (QemuConsole *) opaque;
Gerd Hoffmann1562e532013-03-06 13:40:47 +01001042 DisplaySurface *surface = qemu_console_surface(s);
1043
1044 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
1045 s->g_width = surface_width(surface);
1046 s->g_height = surface_height(surface);
aliguori68f00992009-01-21 18:59:12 +00001047 text_console_resize(s);
1048 }
balrog4d3b6f62008-02-10 16:33:14 +00001049 console_refresh(s);
1050}
1051
Anthony Liguoric227f092009-10-01 16:12:16 -05001052static void text_console_update(void *opaque, console_ch_t *chardata)
balrog4d3b6f62008-02-10 16:33:14 +00001053{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001054 QemuConsole *s = (QemuConsole *) opaque;
balrog4d3b6f62008-02-10 16:33:14 +00001055 int i, j, src;
1056
1057 if (s->text_x[0] <= s->text_x[1]) {
1058 src = (s->y_base + s->text_y[0]) * s->width;
1059 chardata += s->text_y[0] * s->width;
1060 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1061 for (j = 0; j < s->width; j ++, src ++)
1062 console_write_ch(chardata ++, s->cells[src].ch |
1063 (s->cells[src].t_attrib.fgcol << 12) |
1064 (s->cells[src].t_attrib.bgcol << 8) |
1065 (s->cells[src].t_attrib.bold << 21));
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001066 dpy_text_update(s, s->text_x[0], s->text_y[0],
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02001067 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
balrog4d3b6f62008-02-10 16:33:14 +00001068 s->text_x[0] = s->width;
1069 s->text_y[0] = s->height;
1070 s->text_x[1] = 0;
1071 s->text_y[1] = 0;
1072 }
1073 if (s->cursor_invalidate) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001074 dpy_text_cursor(s, s->x, s->y);
balrog4d3b6f62008-02-10 16:33:14 +00001075 s->cursor_invalidate = 0;
1076 }
1077}
1078
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001079static QemuConsole *new_console(DisplayState *ds, console_type_t console_type)
bellarde7f0ad52004-07-14 17:28:59 +00001080{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001081 QemuConsole *s;
pbrook95219892006-04-09 01:06:34 +00001082 int i;
bellarde7f0ad52004-07-14 17:28:59 +00001083
1084 if (nb_consoles >= MAX_CONSOLES)
1085 return NULL;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001086 s = g_malloc0(sizeof(QemuConsole));
thsaf3a9032007-07-11 23:14:59 +00001087 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1088 (console_type == GRAPHIC_CONSOLE))) {
bellarde7f0ad52004-07-14 17:28:59 +00001089 active_console = s;
thsaf3a9032007-07-11 23:14:59 +00001090 }
bellarde7f0ad52004-07-14 17:28:59 +00001091 s->ds = ds;
thsaf3a9032007-07-11 23:14:59 +00001092 s->console_type = console_type;
1093 if (console_type != GRAPHIC_CONSOLE) {
Jan Kiszkaf81bdef2011-09-16 00:48:07 +02001094 s->index = nb_consoles;
pbrook95219892006-04-09 01:06:34 +00001095 consoles[nb_consoles++] = s;
1096 } else {
1097 /* HACK: Put graphical consoles before text consoles. */
1098 for (i = nb_consoles; i > 0; i--) {
thsaf3a9032007-07-11 23:14:59 +00001099 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
pbrook95219892006-04-09 01:06:34 +00001100 break;
1101 consoles[i] = consoles[i - 1];
Jan Kiszkaf81bdef2011-09-16 00:48:07 +02001102 consoles[i]->index = i;
pbrook95219892006-04-09 01:06:34 +00001103 }
Jan Kiszkaf81bdef2011-09-16 00:48:07 +02001104 s->index = i;
pbrook95219892006-04-09 01:06:34 +00001105 consoles[i] = s;
aliguori3023f332009-01-16 19:04:14 +00001106 nb_consoles++;
pbrook95219892006-04-09 01:06:34 +00001107 }
bellarde7f0ad52004-07-14 17:28:59 +00001108 return s;
1109}
1110
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001111static void qemu_alloc_display(DisplaySurface *surface, int width, int height,
1112 int linesize, PixelFormat pf, int newflags)
Jes Sorensenffe8b822011-03-16 13:33:30 +01001113{
Jes Sorensenffe8b822011-03-16 13:33:30 +01001114 surface->pf = pf;
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001115
1116 qemu_pixman_image_unref(surface->image);
1117 surface->image = NULL;
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001118
1119 surface->format = qemu_pixman_get_format(&pf);
1120 assert(surface->format != 0);
1121 surface->image = pixman_image_create_bits(surface->format,
1122 width, height,
1123 NULL, linesize);
1124 assert(surface->image != NULL);
1125
Jes Sorensenffe8b822011-03-16 13:33:30 +01001126 surface->flags = newflags | QEMU_ALLOCATED_FLAG;
Paolo Bonzini98b50082010-02-11 00:29:57 +01001127#ifdef HOST_WORDS_BIGENDIAN
Jes Sorensenffe8b822011-03-16 13:33:30 +01001128 surface->flags |= QEMU_BIG_ENDIAN_FLAG;
Paolo Bonzini98b50082010-02-11 00:29:57 +01001129#endif
Paolo Bonzini98b50082010-02-11 00:29:57 +01001130}
1131
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001132DisplaySurface *qemu_create_displaysurface(int width, int height)
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001133{
1134 DisplaySurface *surface = g_new0(DisplaySurface, 1);
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001135 int linesize = width * 4;
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001136
1137 trace_displaysurface_create(surface, width, height);
Gerd Hoffmann537a4392012-09-27 11:06:36 +02001138 qemu_alloc_display(surface, width, height, linesize,
1139 qemu_default_pixelformat(32), 0);
1140 return surface;
1141}
1142
Gerd Hoffmann187cd1d2012-09-26 07:46:20 +02001143DisplaySurface *qemu_create_displaysurface_from(int width, int height, int bpp,
Gerd Hoffmannb1424e02013-02-20 09:37:12 +01001144 int linesize, uint8_t *data,
1145 bool byteswap)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001146{
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001147 DisplaySurface *surface = g_new0(DisplaySurface, 1);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001148
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001149 trace_displaysurface_create_from(surface, width, height, bpp, byteswap);
Gerd Hoffmannb1424e02013-02-20 09:37:12 +01001150 if (byteswap) {
1151 surface->pf = qemu_different_endianness_pixelformat(bpp);
1152 } else {
1153 surface->pf = qemu_default_pixelformat(bpp);
1154 }
Gerd Hoffmann69c77772012-09-26 15:20:05 +02001155
1156 surface->format = qemu_pixman_get_format(&surface->pf);
1157 assert(surface->format != 0);
1158 surface->image = pixman_image_create_bits(surface->format,
1159 width, height,
1160 (void *)data, linesize);
1161 assert(surface->image != NULL);
1162
Paolo Bonzini98b50082010-02-11 00:29:57 +01001163#ifdef HOST_WORDS_BIGENDIAN
1164 surface->flags = QEMU_BIG_ENDIAN_FLAG;
1165#endif
Paolo Bonzini98b50082010-02-11 00:29:57 +01001166
1167 return surface;
1168}
1169
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001170void qemu_free_displaysurface(DisplaySurface *surface)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001171{
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001172 if (surface == NULL) {
Paolo Bonzini98b50082010-02-11 00:29:57 +01001173 return;
Gerd Hoffmann187cd1d2012-09-26 07:46:20 +02001174 }
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001175 trace_displaysurface_free(surface);
1176 qemu_pixman_image_unref(surface->image);
1177 g_free(surface);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001178}
1179
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001180void register_displaychangelistener(DisplayState *ds,
1181 DisplayChangeListener *dcl)
1182{
1183 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1184 dcl->ds = ds;
1185 QLIST_INSERT_HEAD(&ds->listeners, dcl, next);
1186 gui_setup_refresh(ds);
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001187 if (dcl->ops->dpy_gfx_switch) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001188 dcl->ops->dpy_gfx_switch(dcl, ds->surface);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001189 }
1190}
1191
1192void unregister_displaychangelistener(DisplayChangeListener *dcl)
1193{
1194 DisplayState *ds = dcl->ds;
1195 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1196 QLIST_REMOVE(dcl, next);
1197 gui_setup_refresh(ds);
1198}
1199
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001200void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001201{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001202 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001203 struct DisplayChangeListener *dcl;
1204 int width = pixman_image_get_width(s->surface->image);
1205 int height = pixman_image_get_height(s->surface->image);
1206
1207 x = MAX(x, 0);
1208 y = MAX(y, 0);
1209 x = MIN(x, width);
1210 y = MIN(y, height);
1211 w = MIN(w, width - x);
1212 h = MIN(h, height - y);
1213
1214 QLIST_FOREACH(dcl, &s->listeners, next) {
1215 if (dcl->ops->dpy_gfx_update) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001216 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001217 }
1218 }
1219}
1220
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001221void dpy_gfx_replace_surface(QemuConsole *con,
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001222 DisplaySurface *surface)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001223{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001224 DisplayState *s = con->ds;
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001225 DisplaySurface *old_surface = s->surface;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001226 struct DisplayChangeListener *dcl;
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001227
1228 s->surface = surface;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001229 QLIST_FOREACH(dcl, &s->listeners, next) {
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001230 if (dcl->ops->dpy_gfx_switch) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001231 dcl->ops->dpy_gfx_switch(dcl, surface);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001232 }
1233 }
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001234 qemu_free_displaysurface(old_surface);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001235}
1236
1237void dpy_refresh(DisplayState *s)
1238{
1239 struct DisplayChangeListener *dcl;
1240 QLIST_FOREACH(dcl, &s->listeners, next) {
1241 if (dcl->ops->dpy_refresh) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001242 dcl->ops->dpy_refresh(dcl);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001243 }
1244 }
1245}
1246
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001247void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1248 int dst_x, int dst_y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001249{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001250 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001251 struct DisplayChangeListener *dcl;
1252 QLIST_FOREACH(dcl, &s->listeners, next) {
1253 if (dcl->ops->dpy_gfx_copy) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001254 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001255 } else { /* TODO */
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001256 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001257 }
1258 }
1259}
1260
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001261void dpy_text_cursor(QemuConsole *con, int x, int y)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001262{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001263 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001264 struct DisplayChangeListener *dcl;
1265 QLIST_FOREACH(dcl, &s->listeners, next) {
1266 if (dcl->ops->dpy_text_cursor) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001267 dcl->ops->dpy_text_cursor(dcl, x, y);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001268 }
1269 }
1270}
1271
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001272void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001273{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001274 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001275 struct DisplayChangeListener *dcl;
1276 QLIST_FOREACH(dcl, &s->listeners, next) {
1277 if (dcl->ops->dpy_text_update) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001278 dcl->ops->dpy_text_update(dcl, x, y, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001279 }
1280 }
1281}
1282
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001283void dpy_text_resize(QemuConsole *con, int w, int h)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001284{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001285 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001286 struct DisplayChangeListener *dcl;
1287 QLIST_FOREACH(dcl, &s->listeners, next) {
1288 if (dcl->ops->dpy_text_resize) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001289 dcl->ops->dpy_text_resize(dcl, w, h);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001290 }
1291 }
1292}
1293
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001294void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001295{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001296 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001297 struct DisplayChangeListener *dcl;
1298 QLIST_FOREACH(dcl, &s->listeners, next) {
1299 if (dcl->ops->dpy_mouse_set) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001300 dcl->ops->dpy_mouse_set(dcl, x, y, on);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001301 }
1302 }
1303}
1304
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001305void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001306{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001307 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001308 struct DisplayChangeListener *dcl;
1309 QLIST_FOREACH(dcl, &s->listeners, next) {
1310 if (dcl->ops->dpy_cursor_define) {
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001311 dcl->ops->dpy_cursor_define(dcl, cursor);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001312 }
1313 }
1314}
1315
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001316bool dpy_cursor_define_supported(QemuConsole *con)
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001317{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001318 DisplayState *s = con->ds;
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001319 struct DisplayChangeListener *dcl;
1320 QLIST_FOREACH(dcl, &s->listeners, next) {
1321 if (dcl->ops->dpy_cursor_define) {
1322 return true;
1323 }
1324 }
1325 return false;
1326}
1327
Paolo Bonzini98b50082010-02-11 00:29:57 +01001328/***********************************************************/
1329/* register display */
1330
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001331/* console.c internal use only */
1332static DisplayState *get_alloc_displaystate(void)
Paolo Bonzini98b50082010-02-11 00:29:57 +01001333{
1334 if (!display_state) {
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001335 display_state = g_new0(DisplayState, 1);
Paolo Bonzini98b50082010-02-11 00:29:57 +01001336 }
1337 return display_state;
1338}
1339
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001340/*
1341 * Called by main(), after creating QemuConsoles
1342 * and before initializing ui (sdl/vnc/...).
1343 */
1344DisplayState *init_displaystate(void)
1345{
1346 int i;
1347
1348 if (!display_state) {
1349 display_state = g_new0(DisplayState, 1);
1350 }
1351
1352 for (i = 0; i < nb_consoles; i++) {
1353 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1354 consoles[i]->ds == NULL) {
1355 text_console_do_init(consoles[i]->chr, display_state);
1356 }
1357 }
1358
1359 return display_state;
1360}
1361
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001362QemuConsole *graphic_console_init(vga_hw_update_ptr update,
1363 vga_hw_invalidate_ptr invalidate,
1364 vga_hw_screen_dump_ptr screen_dump,
1365 vga_hw_text_update_ptr text_update,
1366 void *opaque)
bellarde7f0ad52004-07-14 17:28:59 +00001367{
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001368 int width = 640;
1369 int height = 480;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001370 QemuConsole *s;
aliguori3023f332009-01-16 19:04:14 +00001371 DisplayState *ds;
aurel32f0f2f972009-01-16 21:13:49 +00001372
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001373 ds = get_alloc_displaystate();
Gerd Hoffmann437fe102013-03-07 16:04:52 +01001374 trace_console_gfx_new();
thsaf3a9032007-07-11 23:14:59 +00001375 s = new_console(ds, GRAPHIC_CONSOLE);
pbrook95219892006-04-09 01:06:34 +00001376 s->hw_update = update;
1377 s->hw_invalidate = invalidate;
1378 s->hw_screen_dump = screen_dump;
balrog4d3b6f62008-02-10 16:33:14 +00001379 s->hw_text_update = text_update;
pbrook95219892006-04-09 01:06:34 +00001380 s->hw = opaque;
aliguori3023f332009-01-16 19:04:14 +00001381
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001382 if (!ds->surface) {
1383 ds->surface = qemu_create_displaysurface(width, height);
1384 }
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001385 return s;
pbrook95219892006-04-09 01:06:34 +00001386}
1387
1388int is_graphic_console(void)
1389{
balrog4d3b6f62008-02-10 16:33:14 +00001390 return active_console && active_console->console_type == GRAPHIC_CONSOLE;
bellarde7f0ad52004-07-14 17:28:59 +00001391}
1392
balrogc21bbcf2008-09-24 03:32:33 +00001393int is_fixedsize_console(void)
1394{
1395 return active_console && active_console->console_type != TEXT_CONSOLE;
1396}
1397
Paolo Bonzini41048332010-12-23 13:42:52 +01001398static void text_console_set_echo(CharDriverState *chr, bool echo)
1399{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001400 QemuConsole *s = chr->opaque;
Paolo Bonzini41048332010-12-23 13:42:52 +01001401
1402 s->echo = echo;
1403}
1404
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001405static void text_console_update_cursor(void *opaque)
1406{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001407 QemuConsole *s = opaque;
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001408
1409 s->cursor_visible_phase = !s->cursor_visible_phase;
1410 vga_hw_invalidate();
1411 qemu_mod_timer(s->cursor_timer,
1412 qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
1413}
1414
Paolo Bonzini44b37b92010-12-23 13:42:53 +01001415static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
bellarde7f0ad52004-07-14 17:28:59 +00001416{
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001417 QemuConsole *s;
pbrook6d6f7c22006-03-11 15:35:30 +00001418
Paolo Bonzini491e1142010-12-23 13:42:51 +01001419 s = chr->opaque;
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +02001420
bellarde7f0ad52004-07-14 17:28:59 +00001421 chr->chr_write = console_puts;
bellard6fcfafb2004-08-01 21:48:30 +00001422
bellarde15d7372006-06-25 16:26:29 +00001423 s->out_fifo.buf = s->out_fifo_buf;
1424 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001425 s->kbd_timer = qemu_new_timer_ms(rt_clock, kbd_send_chars, s);
aliguori3023f332009-01-16 19:04:14 +00001426 s->ds = ds;
ths3b46e622007-09-17 08:09:54 +00001427
bellarde7f0ad52004-07-14 17:28:59 +00001428 s->y_displayed = 0;
1429 s->y_base = 0;
1430 s->total_height = DEFAULT_BACKSCROLL;
1431 s->x = 0;
1432 s->y = 0;
Paolo Bonzini491e1142010-12-23 13:42:51 +01001433 if (s->console_type == TEXT_CONSOLE) {
Gerd Hoffmann1562e532013-03-06 13:40:47 +01001434 s->g_width = surface_width(s->ds->surface);
1435 s->g_height = surface_height(s->ds->surface);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001436 }
pbrook6d6f7c22006-03-11 15:35:30 +00001437
Jan Kiszkabf1bed82012-07-10 22:00:55 +02001438 s->cursor_timer =
1439 qemu_new_timer_ms(rt_clock, text_console_update_cursor, s);
1440
balrog4d3b6f62008-02-10 16:33:14 +00001441 s->hw_invalidate = text_console_invalidate;
1442 s->hw_text_update = text_console_update;
1443 s->hw = s;
1444
pbrook6d6f7c22006-03-11 15:35:30 +00001445 /* Set text attribute defaults */
1446 s->t_attrib_default.bold = 0;
1447 s->t_attrib_default.uline = 0;
1448 s->t_attrib_default.blink = 0;
1449 s->t_attrib_default.invers = 0;
1450 s->t_attrib_default.unvisible = 0;
1451 s->t_attrib_default.fgcol = COLOR_WHITE;
1452 s->t_attrib_default.bgcol = COLOR_BLACK;
pbrook6d6f7c22006-03-11 15:35:30 +00001453 /* set current text attributes to default */
1454 s->t_attrib = s->t_attrib_default;
bellarde7f0ad52004-07-14 17:28:59 +00001455 text_console_resize(s);
1456
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01001457 if (chr->label) {
1458 char msg[128];
1459 int len;
1460
Gerd Hoffmann735ba582009-12-08 13:11:40 +01001461 s->t_attrib.bgcol = COLOR_BLUE;
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01001462 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
1463 console_puts(chr, (uint8_t*)msg, len);
Gerd Hoffmann735ba582009-12-08 13:11:40 +01001464 s->t_attrib = s->t_attrib_default;
Gerd Hoffmann51bfa4d2009-12-08 13:11:39 +01001465 }
1466
Hans de Goedefee204f2013-03-26 11:07:54 +01001467 qemu_chr_be_generic_open(chr);
aurel32ceecf1d2009-01-18 14:08:04 +00001468 if (chr->init)
1469 chr->init(chr);
bellarde7f0ad52004-07-14 17:28:59 +00001470}
pbrookc60e08d2008-07-01 16:24:38 +00001471
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001472static CharDriverState *text_console_init(ChardevVC *vc)
aliguori2796dae2009-01-16 20:23:27 +00001473{
1474 CharDriverState *chr;
Gerd Hoffmann76ffb0b2012-09-28 13:24:17 +02001475 QemuConsole *s;
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001476 unsigned width = 0;
1477 unsigned height = 0;
aliguori2796dae2009-01-16 20:23:27 +00001478
Anthony Liguori7267c092011-08-20 22:09:37 -05001479 chr = g_malloc0(sizeof(CharDriverState));
aliguori2796dae2009-01-16 20:23:27 +00001480
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001481 if (vc->has_width) {
1482 width = vc->width;
1483 } else if (vc->has_cols) {
1484 width = vc->cols * FONT_WIDTH;
1485 }
Paolo Bonzini491e1142010-12-23 13:42:51 +01001486
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001487 if (vc->has_height) {
1488 height = vc->height;
1489 } else if (vc->has_rows) {
1490 height = vc->rows * FONT_HEIGHT;
1491 }
Paolo Bonzini491e1142010-12-23 13:42:51 +01001492
Gerd Hoffmann437fe102013-03-07 16:04:52 +01001493 trace_console_txt_new(width, height);
Paolo Bonzini491e1142010-12-23 13:42:51 +01001494 if (width == 0 || height == 0) {
1495 s = new_console(NULL, TEXT_CONSOLE);
1496 } else {
1497 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE);
1498 }
1499
1500 if (!s) {
Stefan Weil5354d082011-10-02 18:53:09 +02001501 g_free(chr);
Markus Armbruster1f514702012-02-07 15:09:08 +01001502 return NULL;
Paolo Bonzini491e1142010-12-23 13:42:51 +01001503 }
1504
1505 s->chr = chr;
1506 s->g_width = width;
1507 s->g_height = height;
1508 chr->opaque = s;
Paolo Bonzini41048332010-12-23 13:42:52 +01001509 chr->chr_set_echo = text_console_set_echo;
Gerd Hoffmann64840c62013-03-07 17:08:29 +01001510
1511 if (display_state) {
1512 text_console_do_init(chr, display_state);
1513 }
Markus Armbruster1f514702012-02-07 15:09:08 +01001514 return chr;
aliguori2796dae2009-01-16 20:23:27 +00001515}
1516
Anthony Liguorid82831d2013-02-20 07:43:19 -06001517static VcHandler *vc_handler = text_console_init;
1518
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001519CharDriverState *vc_init(ChardevVC *vc)
Anthony Liguorid82831d2013-02-20 07:43:19 -06001520{
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001521 return vc_handler(vc);
Anthony Liguorid82831d2013-02-20 07:43:19 -06001522}
1523
1524void register_vc_handler(VcHandler *handler)
1525{
1526 vc_handler = handler;
1527}
1528
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001529void qemu_console_resize(QemuConsole *s, int width, int height)
pbrookc60e08d2008-07-01 16:24:38 +00001530{
aliguori3023f332009-01-16 19:04:14 +00001531 s->g_width = width;
1532 s->g_height = height;
1533 if (is_graphic_console()) {
Gerd Hoffmannda229ef2013-02-28 10:48:02 +01001534 DisplaySurface *surface;
1535 surface = qemu_create_displaysurface(width, height);
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001536 dpy_gfx_replace_surface(s, surface);
pbrookc60e08d2008-07-01 16:24:38 +00001537 }
1538}
balrog38334f72008-09-24 02:21:24 +00001539
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001540void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
aliguori3023f332009-01-16 19:04:14 +00001541 int dst_x, int dst_y, int w, int h)
balrogc21bbcf2008-09-24 03:32:33 +00001542{
aliguori3023f332009-01-16 19:04:14 +00001543 if (is_graphic_console()) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001544 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
balrog38334f72008-09-24 02:21:24 +00001545 }
1546}
aliguori7d957bd2009-01-15 22:14:11 +00001547
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001548DisplaySurface *qemu_console_surface(QemuConsole *console)
1549{
1550 return console->ds->surface;
1551}
1552
1553DisplayState *qemu_console_displaystate(QemuConsole *console)
1554{
1555 return console->ds;
1556}
1557
malc0da2ea12009-01-23 19:56:19 +00001558PixelFormat qemu_different_endianness_pixelformat(int bpp)
aliguori7d957bd2009-01-15 22:14:11 +00001559{
1560 PixelFormat pf;
1561
1562 memset(&pf, 0x00, sizeof(PixelFormat));
1563
1564 pf.bits_per_pixel = bpp;
BALATON Zoltanfeadf1a2012-08-22 17:19:42 +02001565 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
aliguori7d957bd2009-01-15 22:14:11 +00001566 pf.depth = bpp == 32 ? 24 : bpp;
1567
1568 switch (bpp) {
malc0da2ea12009-01-23 19:56:19 +00001569 case 24:
1570 pf.rmask = 0x000000FF;
1571 pf.gmask = 0x0000FF00;
1572 pf.bmask = 0x00FF0000;
1573 pf.rmax = 255;
1574 pf.gmax = 255;
1575 pf.bmax = 255;
1576 pf.rshift = 0;
1577 pf.gshift = 8;
1578 pf.bshift = 16;
aliguori90a1e3c2009-01-26 15:37:30 +00001579 pf.rbits = 8;
1580 pf.gbits = 8;
1581 pf.bbits = 8;
aliguori7d957bd2009-01-15 22:14:11 +00001582 break;
malc0da2ea12009-01-23 19:56:19 +00001583 case 32:
1584 pf.rmask = 0x0000FF00;
1585 pf.gmask = 0x00FF0000;
1586 pf.bmask = 0xFF000000;
1587 pf.amask = 0x00000000;
1588 pf.amax = 255;
1589 pf.rmax = 255;
1590 pf.gmax = 255;
1591 pf.bmax = 255;
1592 pf.ashift = 0;
1593 pf.rshift = 8;
1594 pf.gshift = 16;
1595 pf.bshift = 24;
aliguori90a1e3c2009-01-26 15:37:30 +00001596 pf.rbits = 8;
1597 pf.gbits = 8;
1598 pf.bbits = 8;
1599 pf.abits = 8;
malc0da2ea12009-01-23 19:56:19 +00001600 break;
1601 default:
1602 break;
1603 }
1604 return pf;
1605}
1606
1607PixelFormat qemu_default_pixelformat(int bpp)
1608{
1609 PixelFormat pf;
1610
1611 memset(&pf, 0x00, sizeof(PixelFormat));
1612
1613 pf.bits_per_pixel = bpp;
BALATON Zoltanfeadf1a2012-08-22 17:19:42 +02001614 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
malc0da2ea12009-01-23 19:56:19 +00001615 pf.depth = bpp == 32 ? 24 : bpp;
1616
1617 switch (bpp) {
Gerd Hoffmannb6278082010-05-21 11:59:14 +02001618 case 15:
1619 pf.bits_per_pixel = 16;
Gerd Hoffmannb6278082010-05-21 11:59:14 +02001620 pf.rmask = 0x00007c00;
1621 pf.gmask = 0x000003E0;
1622 pf.bmask = 0x0000001F;
1623 pf.rmax = 31;
1624 pf.gmax = 31;
1625 pf.bmax = 31;
1626 pf.rshift = 10;
1627 pf.gshift = 5;
1628 pf.bshift = 0;
1629 pf.rbits = 5;
1630 pf.gbits = 5;
1631 pf.bbits = 5;
1632 break;
aliguori7d957bd2009-01-15 22:14:11 +00001633 case 16:
1634 pf.rmask = 0x0000F800;
1635 pf.gmask = 0x000007E0;
1636 pf.bmask = 0x0000001F;
1637 pf.rmax = 31;
1638 pf.gmax = 63;
1639 pf.bmax = 31;
1640 pf.rshift = 11;
1641 pf.gshift = 5;
1642 pf.bshift = 0;
aliguori90a1e3c2009-01-26 15:37:30 +00001643 pf.rbits = 5;
1644 pf.gbits = 6;
1645 pf.bbits = 5;
aliguori7d957bd2009-01-15 22:14:11 +00001646 break;
1647 case 24:
aliguori7d957bd2009-01-15 22:14:11 +00001648 pf.rmask = 0x00FF0000;
1649 pf.gmask = 0x0000FF00;
1650 pf.bmask = 0x000000FF;
1651 pf.rmax = 255;
1652 pf.gmax = 255;
1653 pf.bmax = 255;
1654 pf.rshift = 16;
1655 pf.gshift = 8;
1656 pf.bshift = 0;
aliguori90a1e3c2009-01-26 15:37:30 +00001657 pf.rbits = 8;
1658 pf.gbits = 8;
1659 pf.bbits = 8;
Markus Armbruster0eba62e2011-11-22 12:56:10 +01001660 break;
malc0da2ea12009-01-23 19:56:19 +00001661 case 32:
1662 pf.rmask = 0x00FF0000;
1663 pf.gmask = 0x0000FF00;
1664 pf.bmask = 0x000000FF;
malc0da2ea12009-01-23 19:56:19 +00001665 pf.rmax = 255;
1666 pf.gmax = 255;
1667 pf.bmax = 255;
malc0da2ea12009-01-23 19:56:19 +00001668 pf.rshift = 16;
1669 pf.gshift = 8;
1670 pf.bshift = 0;
aliguori90a1e3c2009-01-26 15:37:30 +00001671 pf.rbits = 8;
1672 pf.gbits = 8;
1673 pf.bbits = 8;
aliguori7d957bd2009-01-15 22:14:11 +00001674 break;
1675 default:
1676 break;
1677 }
1678 return pf;
1679}
Anthony Liguori01f45d92013-03-05 23:21:32 +05301680
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001681static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
1682 Error **errp)
1683{
1684 int val;
1685
1686 backend->vc = g_new0(ChardevVC, 1);
1687
1688 val = qemu_opt_get_number(opts, "width", 0);
1689 if (val != 0) {
1690 backend->vc->has_width = true;
1691 backend->vc->width = val;
1692 }
1693
1694 val = qemu_opt_get_number(opts, "height", 0);
1695 if (val != 0) {
1696 backend->vc->has_height = true;
1697 backend->vc->height = val;
1698 }
1699
1700 val = qemu_opt_get_number(opts, "cols", 0);
1701 if (val != 0) {
1702 backend->vc->has_cols = true;
1703 backend->vc->cols = val;
1704 }
1705
1706 val = qemu_opt_get_number(opts, "rows", 0);
1707 if (val != 0) {
1708 backend->vc->has_rows = true;
1709 backend->vc->rows = val;
1710 }
1711}
1712
Anthony Liguori01f45d92013-03-05 23:21:32 +05301713static void register_types(void)
1714{
Gerd Hoffmann702ec692013-02-25 15:52:32 +01001715 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC,
1716 qemu_chr_parse_vc);
Anthony Liguori01f45d92013-03-05 23:21:32 +05301717}
1718
1719type_init(register_types);