]> git.proxmox.com Git - qemu.git/blob - ui/console.c
console: displaystate init revamp
[qemu.git] / ui / console.c
1 /*
2 * QEMU graphical console
3 *
4 * Copyright (c) 2004 Fabrice Bellard
5 *
6 * 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 */
24 #include "qemu-common.h"
25 #include "ui/console.h"
26 #include "qemu/timer.h"
27 #include "qmp-commands.h"
28 #include "sysemu/char.h"
29
30 //#define DEBUG_CONSOLE
31 #define DEFAULT_BACKSCROLL 512
32 #define MAX_CONSOLES 12
33 #define CONSOLE_CURSOR_PERIOD 500
34
35 typedef 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
45 typedef struct TextCell {
46 uint8_t ch;
47 TextAttributes t_attrib;
48 } TextCell;
49
50 #define MAX_ESC_PARAMS 3
51
52 enum TTYState {
53 TTY_STATE_NORM,
54 TTY_STATE_ESC,
55 TTY_STATE_CSI,
56 };
57
58 typedef struct QEMUFIFO {
59 uint8_t *buf;
60 int buf_size;
61 int count, wptr, rptr;
62 } QEMUFIFO;
63
64 static int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1)
65 {
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
87 static int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1)
88 {
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
109 typedef enum {
110 GRAPHIC_CONSOLE,
111 TEXT_CONSOLE,
112 TEXT_CONSOLE_FIXED_SIZE
113 } console_type_t;
114
115 struct QemuConsole {
116 int index;
117 console_type_t console_type;
118 DisplayState *ds;
119
120 /* 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;
124 vga_hw_text_update_ptr hw_text_update;
125 void *hw;
126 int g_width, g_height;
127
128 /* Text console state */
129 int width;
130 int height;
131 int total_height;
132 int backscroll_height;
133 int x, y;
134 int x_saved, y_saved;
135 int y_displayed;
136 int y_base;
137 TextAttributes t_attrib_default; /* default text attributes */
138 TextAttributes t_attrib; /* currently active text attributes */
139 TextCell *cells;
140 int text_x[2], text_y[2], cursor_invalidate;
141 int echo;
142 bool cursor_visible_phase;
143 QEMUTimer *cursor_timer;
144
145 int update_x0;
146 int update_y0;
147 int update_x1;
148 int update_y1;
149
150 enum TTYState state;
151 int esc_params[MAX_ESC_PARAMS];
152 int nb_esc_params;
153
154 CharDriverState *chr;
155 /* fifo for key pressed */
156 QEMUFIFO out_fifo;
157 uint8_t out_fifo_buf[16];
158 QEMUTimer *kbd_timer;
159 };
160
161 static DisplayState *display_state;
162 static QemuConsole *active_console;
163 static QemuConsole *consoles[MAX_CONSOLES];
164 static int nb_consoles = 0;
165
166 static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
167
168 void vga_hw_update(void)
169 {
170 if (active_console && active_console->hw_update)
171 active_console->hw_update(active_console->hw);
172 }
173
174 void vga_hw_invalidate(void)
175 {
176 if (active_console && active_console->hw_invalidate)
177 active_console->hw_invalidate(active_console->hw);
178 }
179
180 void qmp_screendump(const char *filename, Error **errp)
181 {
182 QemuConsole *previous_active_console;
183 bool cswitch;
184
185 previous_active_console = active_console;
186 cswitch = previous_active_console && previous_active_console->index != 0;
187
188 /* There is currently no way of specifying which screen we want to dump,
189 so always dump the first one. */
190 if (cswitch) {
191 console_select(0);
192 }
193 if (consoles[0] && consoles[0]->hw_screen_dump) {
194 consoles[0]->hw_screen_dump(consoles[0]->hw, filename, cswitch, errp);
195 } else {
196 error_setg(errp, "device doesn't support screendump");
197 }
198
199 if (cswitch) {
200 console_select(previous_active_console->index);
201 }
202 }
203
204 void vga_hw_text_update(console_ch_t *chardata)
205 {
206 if (active_console && active_console->hw_text_update)
207 active_console->hw_text_update(active_console->hw, chardata);
208 }
209
210 static void vga_fill_rect(QemuConsole *con,
211 int posx, int posy, int width, int height,
212 pixman_color_t color)
213 {
214 DisplaySurface *surface = qemu_console_surface(con);
215 pixman_rectangle16_t rect = {
216 .x = posx, .y = posy, .width = width, .height = height
217 };
218
219 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
220 &color, 1, &rect);
221 }
222
223 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
224 static void vga_bitblt(QemuConsole *con,
225 int xs, int ys, int xd, int yd, int w, int h)
226 {
227 DisplaySurface *surface = qemu_console_surface(con);
228
229 pixman_image_composite(PIXMAN_OP_SRC,
230 surface->image, NULL, surface->image,
231 xs, ys, 0, 0, xd, yd, w, h);
232 }
233
234 /***********************************************************/
235 /* basic char display */
236
237 #define FONT_HEIGHT 16
238 #define FONT_WIDTH 8
239
240 #include "vgafont.h"
241
242 #ifndef CONFIG_CURSES
243 enum 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 };
253 #endif
254
255 #define QEMU_RGB(r, g, b) \
256 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
257
258 static const pixman_color_t color_table_rgb[2][8] = {
259 { /* dark */
260 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 */
268 },
269 { /* bright */
270 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 */
278 }
279 };
280
281 #ifdef DEBUG_CONSOLE
282 static 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
313
314 static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
315 TextAttributes *t_attrib)
316 {
317 static pixman_image_t *glyphs[256];
318 DisplaySurface *surface = qemu_console_surface(s);
319 pixman_color_t fgcol, bgcol;
320
321 if (t_attrib->invers) {
322 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
323 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
324 } else {
325 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
326 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
327 }
328
329 if (!glyphs[ch]) {
330 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
331 }
332 qemu_pixman_glyph_render(glyphs[ch], surface->image,
333 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
334 }
335
336 static void text_console_resize(QemuConsole *s)
337 {
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
349 cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
350 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 = ' ';
360 c->t_attrib = s->t_attrib_default;
361 c++;
362 }
363 }
364 g_free(s->cells);
365 s->cells = cells;
366 }
367
368 static inline void text_update_xy(QemuConsole *s, int x, int y)
369 {
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
376 static void invalidate_xy(QemuConsole *s, int x, int y)
377 {
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
388 static void update_xy(QemuConsole *s, int x, int y)
389 {
390 TextCell *c;
391 int y1, y2;
392
393 if (s != active_console) {
394 return;
395 }
396
397 if (s->ds->have_text) {
398 text_update_xy(s, x, y);
399 }
400
401 if (s->ds->have_gfx) {
402 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];
408 vga_putcharxy(s, x, y2, c->ch,
409 &(c->t_attrib));
410 invalidate_xy(s, x, y2);
411 }
412 }
413 }
414
415 static void console_show_cursor(QemuConsole *s, int show)
416 {
417 TextCell *c;
418 int y, y1;
419 int x = s->x;
420
421 if (s != active_console) {
422 return;
423 }
424
425 if (s->ds->have_text) {
426 s->cursor_invalidate = 1;
427 }
428
429 if (s->ds->have_gfx) {
430 if (x >= s->width) {
431 x = s->width - 1;
432 }
433 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) {
438 c = &s->cells[y1 * s->width + x];
439 if (show && s->cursor_visible_phase) {
440 TextAttributes t_attrib = s->t_attrib_default;
441 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
442 vga_putcharxy(s, x, y, c->ch, &t_attrib);
443 } else {
444 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
445 }
446 invalidate_xy(s, x, y);
447 }
448 }
449 }
450
451 static void console_refresh(QemuConsole *s)
452 {
453 DisplaySurface *surface = qemu_console_surface(s);
454 TextCell *c;
455 int x, y, y1;
456
457 if (s != active_console)
458 return;
459
460 if (s->ds->have_text) {
461 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;
466 }
467
468 if (s->ds->have_gfx) {
469 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
470 color_table_rgb[0][COLOR_BLACK]);
471 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++) {
475 vga_putcharxy(s, x, y, c->ch,
476 &(c->t_attrib));
477 c++;
478 }
479 if (++y1 == s->total_height) {
480 y1 = 0;
481 }
482 }
483 console_show_cursor(s, 1);
484 dpy_gfx_update(s, 0, 0,
485 surface_width(surface), surface_height(surface));
486 }
487 }
488
489 static void console_scroll(int ydelta)
490 {
491 QemuConsole *s;
492 int i, y1;
493
494 s = active_console;
495 if (!s || (s->console_type == GRAPHIC_CONSOLE))
496 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
523 static void console_put_lf(QemuConsole *s)
524 {
525 TextCell *c;
526 int x, y1;
527
528 s->y++;
529 if (s->y >= s->height) {
530 s->y = s->height - 1;
531
532 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 = ' ';
544 c->t_attrib = s->t_attrib_default;
545 c++;
546 }
547 if (s == active_console && s->y_displayed == s->y_base) {
548 if (s->ds->have_text) {
549 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;
553 }
554
555 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 }
567 }
568 }
569 }
570
571 /* 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 */
575 static void console_handle_escape(QemuConsole *s)
576 {
577 int i;
578
579 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
668 static void console_clear_xy(QemuConsole *s, int x, int y)
669 {
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;
674 update_xy(s, x, y);
675 }
676
677 /* set cursor, checking bounds */
678 static void set_cursor(QemuConsole *s, int x, int y)
679 {
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
697 static void console_putchar(QemuConsole *s, int ch)
698 {
699 TextCell *c;
700 int y1, i;
701 int x, y;
702
703 switch(s->state) {
704 case TTY_STATE_NORM:
705 switch(ch) {
706 case '\r': /* carriage return */
707 s->x = 0;
708 break;
709 case '\n': /* newline */
710 console_put_lf(s);
711 break;
712 case '\b': /* backspace */
713 if (s->x > 0)
714 s->x--;
715 break;
716 case '\t': /* tabspace */
717 if (s->x + (8 - (s->x % 8)) > s->width) {
718 s->x = 0;
719 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;
727 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;
733 case 27: /* esc (introducing an escape sequence) */
734 s->state = TTY_STATE_ESC;
735 break;
736 default:
737 if (s->x >= s->width) {
738 /* line wrap */
739 s->x = 0;
740 console_put_lf(s);
741 }
742 y1 = (s->y_base + s->y) % s->total_height;
743 c = &s->cells[y1 * s->width + s->x];
744 c->ch = ch;
745 c->t_attrib = s->t_attrib;
746 update_xy(s, s->x, s->y);
747 s->x++;
748 break;
749 }
750 break;
751 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
752 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;
761 case TTY_STATE_CSI: /* handle escape sequence parameters */
762 if (ch >= '0' && ch <= '9') {
763 if (s->nb_esc_params < MAX_ESC_PARAMS) {
764 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;
769 }
770 } else {
771 if (s->nb_esc_params < MAX_ESC_PARAMS)
772 s->nb_esc_params++;
773 if (ch == ';')
774 break;
775 #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
779 s->state = TTY_STATE_NORM;
780 switch(ch) {
781 case 'A':
782 /* move cursor up */
783 if (s->esc_params[0] == 0) {
784 s->esc_params[0] = 1;
785 }
786 set_cursor(s, s->x, s->y - s->esc_params[0]);
787 break;
788 case 'B':
789 /* move cursor down */
790 if (s->esc_params[0] == 0) {
791 s->esc_params[0] = 1;
792 }
793 set_cursor(s, s->x, s->y + s->esc_params[0]);
794 break;
795 case 'C':
796 /* move cursor right */
797 if (s->esc_params[0] == 0) {
798 s->esc_params[0] = 1;
799 }
800 set_cursor(s, s->x + s->esc_params[0], s->y);
801 break;
802 case 'D':
803 /* move cursor left */
804 if (s->esc_params[0] == 0) {
805 s->esc_params[0] = 1;
806 }
807 set_cursor(s, s->x - s->esc_params[0], s->y);
808 break;
809 case 'G':
810 /* move cursor to column */
811 set_cursor(s, s->esc_params[0] - 1, s->y);
812 break;
813 case 'f':
814 case 'H':
815 /* move cursor to row, column */
816 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
817 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 }
849 break;
850 }
851 break;
852 case 'K':
853 switch (s->esc_params[0]) {
854 case 0:
855 /* clear to eol */
856 for(x = s->x; x < s->width; x++) {
857 console_clear_xy(s, x, s->y);
858 }
859 break;
860 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 }
871 break;
872 }
873 break;
874 case 'm':
875 console_handle_escape(s);
876 break;
877 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;
898 }
899 }
900 }
901
902 void console_select(unsigned int index)
903 {
904 DisplaySurface *surface;
905 QemuConsole *s;
906
907 if (index >= MAX_CONSOLES)
908 return;
909
910 trace_console_select(index);
911 if (active_console) {
912 surface = qemu_console_surface(active_console);
913 active_console->g_width = surface_width(surface);
914 active_console->g_height = surface_height(surface);
915 }
916 s = consoles[index];
917 if (s) {
918 DisplayState *ds = s->ds;
919
920 if (active_console && active_console->cursor_timer) {
921 qemu_del_timer(active_console->cursor_timer);
922 }
923 active_console = s;
924 if (ds->have_gfx) {
925 surface = qemu_create_displaysurface(s->g_width, s->g_height);
926 dpy_gfx_replace_surface(s, surface);
927 }
928 if (ds->have_text) {
929 dpy_text_resize(s, s->width, s->height);
930 }
931 if (s->cursor_timer) {
932 qemu_mod_timer(s->cursor_timer,
933 qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
934 }
935 vga_hw_invalidate();
936 }
937 }
938
939 static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
940 {
941 QemuConsole *s = chr->opaque;
942 int i;
943
944 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;
948 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);
953 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
954 dpy_gfx_update(s, s->update_x0, s->update_y0,
955 s->update_x1 - s->update_x0,
956 s->update_y1 - s->update_y0);
957 }
958 return len;
959 }
960
961 static void kbd_send_chars(void *opaque)
962 {
963 QemuConsole *s = opaque;
964 int len;
965 uint8_t buf[16];
966
967 len = qemu_chr_be_can_write(s->chr);
968 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);
974 qemu_chr_be_write(s->chr, buf, len);
975 }
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) {
979 qemu_mod_timer(s->kbd_timer, qemu_get_clock_ms(rt_clock) + 1);
980 }
981 }
982
983 /* called when an ascii key is pressed */
984 void kbd_put_keysym(int keysym)
985 {
986 QemuConsole *s;
987 uint8_t buf[16], *q;
988 int c;
989
990 s = active_console;
991 if (!s || (s->console_type == GRAPHIC_CONSOLE))
992 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:
1008 /* 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;
1022 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1023 console_puts(s->chr, (const uint8_t *) "\r", 1);
1024 *q++ = '\n';
1025 } else {
1026 *q++ = keysym;
1027 }
1028 if (s->echo) {
1029 console_puts(s->chr, buf, q - buf);
1030 }
1031 if (s->chr->chr_read) {
1032 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1033 kbd_send_chars(s);
1034 }
1035 break;
1036 }
1037 }
1038
1039 static void text_console_invalidate(void *opaque)
1040 {
1041 QemuConsole *s = (QemuConsole *) opaque;
1042 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);
1047 text_console_resize(s);
1048 }
1049 console_refresh(s);
1050 }
1051
1052 static void text_console_update(void *opaque, console_ch_t *chardata)
1053 {
1054 QemuConsole *s = (QemuConsole *) opaque;
1055 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));
1066 dpy_text_update(s, s->text_x[0], s->text_y[0],
1067 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
1068 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) {
1074 dpy_text_cursor(s, s->x, s->y);
1075 s->cursor_invalidate = 0;
1076 }
1077 }
1078
1079 static QemuConsole *new_console(DisplayState *ds, console_type_t console_type)
1080 {
1081 QemuConsole *s;
1082 int i;
1083
1084 if (nb_consoles >= MAX_CONSOLES)
1085 return NULL;
1086 s = g_malloc0(sizeof(QemuConsole));
1087 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1088 (console_type == GRAPHIC_CONSOLE))) {
1089 active_console = s;
1090 }
1091 s->ds = ds;
1092 s->console_type = console_type;
1093 if (console_type != GRAPHIC_CONSOLE) {
1094 s->index = nb_consoles;
1095 consoles[nb_consoles++] = s;
1096 } else {
1097 /* HACK: Put graphical consoles before text consoles. */
1098 for (i = nb_consoles; i > 0; i--) {
1099 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
1100 break;
1101 consoles[i] = consoles[i - 1];
1102 consoles[i]->index = i;
1103 }
1104 s->index = i;
1105 consoles[i] = s;
1106 nb_consoles++;
1107 }
1108 return s;
1109 }
1110
1111 static void qemu_alloc_display(DisplaySurface *surface, int width, int height,
1112 int linesize, PixelFormat pf, int newflags)
1113 {
1114 surface->pf = pf;
1115
1116 qemu_pixman_image_unref(surface->image);
1117 surface->image = NULL;
1118
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
1126 surface->flags = newflags | QEMU_ALLOCATED_FLAG;
1127 #ifdef HOST_WORDS_BIGENDIAN
1128 surface->flags |= QEMU_BIG_ENDIAN_FLAG;
1129 #endif
1130 }
1131
1132 DisplaySurface *qemu_create_displaysurface(int width, int height)
1133 {
1134 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1135 int linesize = width * 4;
1136
1137 trace_displaysurface_create(surface, width, height);
1138 qemu_alloc_display(surface, width, height, linesize,
1139 qemu_default_pixelformat(32), 0);
1140 return surface;
1141 }
1142
1143 DisplaySurface *qemu_create_displaysurface_from(int width, int height, int bpp,
1144 int linesize, uint8_t *data,
1145 bool byteswap)
1146 {
1147 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1148
1149 trace_displaysurface_create_from(surface, width, height, bpp, byteswap);
1150 if (byteswap) {
1151 surface->pf = qemu_different_endianness_pixelformat(bpp);
1152 } else {
1153 surface->pf = qemu_default_pixelformat(bpp);
1154 }
1155
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
1163 #ifdef HOST_WORDS_BIGENDIAN
1164 surface->flags = QEMU_BIG_ENDIAN_FLAG;
1165 #endif
1166
1167 return surface;
1168 }
1169
1170 void qemu_free_displaysurface(DisplaySurface *surface)
1171 {
1172 if (surface == NULL) {
1173 return;
1174 }
1175 trace_displaysurface_free(surface);
1176 qemu_pixman_image_unref(surface->image);
1177 g_free(surface);
1178 }
1179
1180 void 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);
1187 if (dcl->ops->dpy_gfx_switch) {
1188 dcl->ops->dpy_gfx_switch(dcl, ds->surface);
1189 }
1190 }
1191
1192 void 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
1200 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
1201 {
1202 DisplayState *s = con->ds;
1203 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) {
1216 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
1217 }
1218 }
1219 }
1220
1221 void dpy_gfx_replace_surface(QemuConsole *con,
1222 DisplaySurface *surface)
1223 {
1224 DisplayState *s = con->ds;
1225 DisplaySurface *old_surface = s->surface;
1226 struct DisplayChangeListener *dcl;
1227
1228 s->surface = surface;
1229 QLIST_FOREACH(dcl, &s->listeners, next) {
1230 if (dcl->ops->dpy_gfx_switch) {
1231 dcl->ops->dpy_gfx_switch(dcl, surface);
1232 }
1233 }
1234 qemu_free_displaysurface(old_surface);
1235 }
1236
1237 void dpy_refresh(DisplayState *s)
1238 {
1239 struct DisplayChangeListener *dcl;
1240 QLIST_FOREACH(dcl, &s->listeners, next) {
1241 if (dcl->ops->dpy_refresh) {
1242 dcl->ops->dpy_refresh(dcl);
1243 }
1244 }
1245 }
1246
1247 void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1248 int dst_x, int dst_y, int w, int h)
1249 {
1250 DisplayState *s = con->ds;
1251 struct DisplayChangeListener *dcl;
1252 QLIST_FOREACH(dcl, &s->listeners, next) {
1253 if (dcl->ops->dpy_gfx_copy) {
1254 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
1255 } else { /* TODO */
1256 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
1257 }
1258 }
1259 }
1260
1261 void dpy_text_cursor(QemuConsole *con, int x, int y)
1262 {
1263 DisplayState *s = con->ds;
1264 struct DisplayChangeListener *dcl;
1265 QLIST_FOREACH(dcl, &s->listeners, next) {
1266 if (dcl->ops->dpy_text_cursor) {
1267 dcl->ops->dpy_text_cursor(dcl, x, y);
1268 }
1269 }
1270 }
1271
1272 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
1273 {
1274 DisplayState *s = con->ds;
1275 struct DisplayChangeListener *dcl;
1276 QLIST_FOREACH(dcl, &s->listeners, next) {
1277 if (dcl->ops->dpy_text_update) {
1278 dcl->ops->dpy_text_update(dcl, x, y, w, h);
1279 }
1280 }
1281 }
1282
1283 void dpy_text_resize(QemuConsole *con, int w, int h)
1284 {
1285 DisplayState *s = con->ds;
1286 struct DisplayChangeListener *dcl;
1287 QLIST_FOREACH(dcl, &s->listeners, next) {
1288 if (dcl->ops->dpy_text_resize) {
1289 dcl->ops->dpy_text_resize(dcl, w, h);
1290 }
1291 }
1292 }
1293
1294 void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
1295 {
1296 DisplayState *s = con->ds;
1297 struct DisplayChangeListener *dcl;
1298 QLIST_FOREACH(dcl, &s->listeners, next) {
1299 if (dcl->ops->dpy_mouse_set) {
1300 dcl->ops->dpy_mouse_set(dcl, x, y, on);
1301 }
1302 }
1303 }
1304
1305 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
1306 {
1307 DisplayState *s = con->ds;
1308 struct DisplayChangeListener *dcl;
1309 QLIST_FOREACH(dcl, &s->listeners, next) {
1310 if (dcl->ops->dpy_cursor_define) {
1311 dcl->ops->dpy_cursor_define(dcl, cursor);
1312 }
1313 }
1314 }
1315
1316 bool dpy_cursor_define_supported(QemuConsole *con)
1317 {
1318 DisplayState *s = con->ds;
1319 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
1328 /***********************************************************/
1329 /* register display */
1330
1331 /* console.c internal use only */
1332 static DisplayState *get_alloc_displaystate(void)
1333 {
1334 if (!display_state) {
1335 display_state = g_new0(DisplayState, 1);
1336 }
1337 return display_state;
1338 }
1339
1340 /*
1341 * Called by main(), after creating QemuConsoles
1342 * and before initializing ui (sdl/vnc/...).
1343 */
1344 DisplayState *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
1362 QemuConsole *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)
1367 {
1368 int width = 640;
1369 int height = 480;
1370 QemuConsole *s;
1371 DisplayState *ds;
1372
1373 ds = get_alloc_displaystate();
1374 trace_console_gfx_new();
1375 s = new_console(ds, GRAPHIC_CONSOLE);
1376 s->hw_update = update;
1377 s->hw_invalidate = invalidate;
1378 s->hw_screen_dump = screen_dump;
1379 s->hw_text_update = text_update;
1380 s->hw = opaque;
1381
1382 if (!ds->surface) {
1383 ds->surface = qemu_create_displaysurface(width, height);
1384 }
1385 return s;
1386 }
1387
1388 int is_graphic_console(void)
1389 {
1390 return active_console && active_console->console_type == GRAPHIC_CONSOLE;
1391 }
1392
1393 int is_fixedsize_console(void)
1394 {
1395 return active_console && active_console->console_type != TEXT_CONSOLE;
1396 }
1397
1398 static void text_console_set_echo(CharDriverState *chr, bool echo)
1399 {
1400 QemuConsole *s = chr->opaque;
1401
1402 s->echo = echo;
1403 }
1404
1405 static void text_console_update_cursor(void *opaque)
1406 {
1407 QemuConsole *s = opaque;
1408
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
1415 static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
1416 {
1417 QemuConsole *s;
1418
1419 s = chr->opaque;
1420
1421 chr->chr_write = console_puts;
1422
1423 s->out_fifo.buf = s->out_fifo_buf;
1424 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
1425 s->kbd_timer = qemu_new_timer_ms(rt_clock, kbd_send_chars, s);
1426 s->ds = ds;
1427
1428 s->y_displayed = 0;
1429 s->y_base = 0;
1430 s->total_height = DEFAULT_BACKSCROLL;
1431 s->x = 0;
1432 s->y = 0;
1433 if (s->console_type == TEXT_CONSOLE) {
1434 s->g_width = surface_width(s->ds->surface);
1435 s->g_height = surface_height(s->ds->surface);
1436 }
1437
1438 s->cursor_timer =
1439 qemu_new_timer_ms(rt_clock, text_console_update_cursor, s);
1440
1441 s->hw_invalidate = text_console_invalidate;
1442 s->hw_text_update = text_console_update;
1443 s->hw = s;
1444
1445 /* 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;
1453 /* set current text attributes to default */
1454 s->t_attrib = s->t_attrib_default;
1455 text_console_resize(s);
1456
1457 if (chr->label) {
1458 char msg[128];
1459 int len;
1460
1461 s->t_attrib.bgcol = COLOR_BLUE;
1462 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
1463 console_puts(chr, (uint8_t*)msg, len);
1464 s->t_attrib = s->t_attrib_default;
1465 }
1466
1467 qemu_chr_be_generic_open(chr);
1468 if (chr->init)
1469 chr->init(chr);
1470 }
1471
1472 static CharDriverState *text_console_init(ChardevVC *vc)
1473 {
1474 CharDriverState *chr;
1475 QemuConsole *s;
1476 unsigned width = 0;
1477 unsigned height = 0;
1478
1479 chr = g_malloc0(sizeof(CharDriverState));
1480
1481 if (vc->has_width) {
1482 width = vc->width;
1483 } else if (vc->has_cols) {
1484 width = vc->cols * FONT_WIDTH;
1485 }
1486
1487 if (vc->has_height) {
1488 height = vc->height;
1489 } else if (vc->has_rows) {
1490 height = vc->rows * FONT_HEIGHT;
1491 }
1492
1493 trace_console_txt_new(width, height);
1494 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) {
1501 g_free(chr);
1502 return NULL;
1503 }
1504
1505 s->chr = chr;
1506 s->g_width = width;
1507 s->g_height = height;
1508 chr->opaque = s;
1509 chr->chr_set_echo = text_console_set_echo;
1510
1511 if (display_state) {
1512 text_console_do_init(chr, display_state);
1513 }
1514 return chr;
1515 }
1516
1517 static VcHandler *vc_handler = text_console_init;
1518
1519 CharDriverState *vc_init(ChardevVC *vc)
1520 {
1521 return vc_handler(vc);
1522 }
1523
1524 void register_vc_handler(VcHandler *handler)
1525 {
1526 vc_handler = handler;
1527 }
1528
1529 void qemu_console_resize(QemuConsole *s, int width, int height)
1530 {
1531 s->g_width = width;
1532 s->g_height = height;
1533 if (is_graphic_console()) {
1534 DisplaySurface *surface;
1535 surface = qemu_create_displaysurface(width, height);
1536 dpy_gfx_replace_surface(s, surface);
1537 }
1538 }
1539
1540 void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
1541 int dst_x, int dst_y, int w, int h)
1542 {
1543 if (is_graphic_console()) {
1544 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
1545 }
1546 }
1547
1548 DisplaySurface *qemu_console_surface(QemuConsole *console)
1549 {
1550 return console->ds->surface;
1551 }
1552
1553 DisplayState *qemu_console_displaystate(QemuConsole *console)
1554 {
1555 return console->ds;
1556 }
1557
1558 PixelFormat qemu_different_endianness_pixelformat(int bpp)
1559 {
1560 PixelFormat pf;
1561
1562 memset(&pf, 0x00, sizeof(PixelFormat));
1563
1564 pf.bits_per_pixel = bpp;
1565 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
1566 pf.depth = bpp == 32 ? 24 : bpp;
1567
1568 switch (bpp) {
1569 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;
1579 pf.rbits = 8;
1580 pf.gbits = 8;
1581 pf.bbits = 8;
1582 break;
1583 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;
1596 pf.rbits = 8;
1597 pf.gbits = 8;
1598 pf.bbits = 8;
1599 pf.abits = 8;
1600 break;
1601 default:
1602 break;
1603 }
1604 return pf;
1605 }
1606
1607 PixelFormat qemu_default_pixelformat(int bpp)
1608 {
1609 PixelFormat pf;
1610
1611 memset(&pf, 0x00, sizeof(PixelFormat));
1612
1613 pf.bits_per_pixel = bpp;
1614 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
1615 pf.depth = bpp == 32 ? 24 : bpp;
1616
1617 switch (bpp) {
1618 case 15:
1619 pf.bits_per_pixel = 16;
1620 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;
1633 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;
1643 pf.rbits = 5;
1644 pf.gbits = 6;
1645 pf.bbits = 5;
1646 break;
1647 case 24:
1648 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;
1657 pf.rbits = 8;
1658 pf.gbits = 8;
1659 pf.bbits = 8;
1660 break;
1661 case 32:
1662 pf.rmask = 0x00FF0000;
1663 pf.gmask = 0x0000FF00;
1664 pf.bmask = 0x000000FF;
1665 pf.rmax = 255;
1666 pf.gmax = 255;
1667 pf.bmax = 255;
1668 pf.rshift = 16;
1669 pf.gshift = 8;
1670 pf.bshift = 0;
1671 pf.rbits = 8;
1672 pf.gbits = 8;
1673 pf.bbits = 8;
1674 break;
1675 default:
1676 break;
1677 }
1678 return pf;
1679 }
1680
1681 static 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
1713 static void register_types(void)
1714 {
1715 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC,
1716 qemu_chr_parse_vc);
1717 }
1718
1719 type_init(register_types);