]> git.proxmox.com Git - qemu.git/blob - ui/console.c
console: give each QemuConsole its own DisplaySurface
[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 DisplaySurface *surface;
120
121 /* Graphic console state. */
122 graphic_hw_update_ptr hw_update;
123 graphic_hw_invalidate_ptr hw_invalidate;
124 graphic_hw_screen_dump_ptr hw_screen_dump;
125 graphic_hw_text_update_ptr hw_text_update;
126 void *hw;
127 int g_width, g_height;
128
129 /* Text console state */
130 int width;
131 int height;
132 int total_height;
133 int backscroll_height;
134 int x, y;
135 int x_saved, y_saved;
136 int y_displayed;
137 int y_base;
138 TextAttributes t_attrib_default; /* default text attributes */
139 TextAttributes t_attrib; /* currently active text attributes */
140 TextCell *cells;
141 int text_x[2], text_y[2], cursor_invalidate;
142 int echo;
143 bool cursor_visible_phase;
144 QEMUTimer *cursor_timer;
145
146 int update_x0;
147 int update_y0;
148 int update_x1;
149 int update_y1;
150
151 enum TTYState state;
152 int esc_params[MAX_ESC_PARAMS];
153 int nb_esc_params;
154
155 CharDriverState *chr;
156 /* fifo for key pressed */
157 QEMUFIFO out_fifo;
158 uint8_t out_fifo_buf[16];
159 QEMUTimer *kbd_timer;
160 };
161
162 static DisplayState *display_state;
163 static QemuConsole *active_console;
164 static QemuConsole *consoles[MAX_CONSOLES];
165 static int nb_consoles = 0;
166
167 static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
168 static void dpy_gfx_switch_surface(DisplayState *ds,
169 DisplaySurface *surface);
170
171 void graphic_hw_update(QemuConsole *con)
172 {
173 if (!con) {
174 con = active_console;
175 }
176 if (con && con->hw_update) {
177 con->hw_update(con->hw);
178 }
179 }
180
181 void graphic_hw_invalidate(QemuConsole *con)
182 {
183 if (!con) {
184 con = active_console;
185 }
186 if (con && con->hw_invalidate) {
187 con->hw_invalidate(con->hw);
188 }
189 }
190
191 void qmp_screendump(const char *filename, Error **errp)
192 {
193 QemuConsole *previous_active_console;
194 bool cswitch;
195
196 previous_active_console = active_console;
197 cswitch = previous_active_console && previous_active_console->index != 0;
198
199 /* There is currently no way of specifying which screen we want to dump,
200 so always dump the first one. */
201 if (cswitch) {
202 console_select(0);
203 }
204 if (consoles[0] && consoles[0]->hw_screen_dump) {
205 consoles[0]->hw_screen_dump(consoles[0]->hw, filename, cswitch, errp);
206 } else {
207 error_setg(errp, "device doesn't support screendump");
208 }
209
210 if (cswitch) {
211 console_select(previous_active_console->index);
212 }
213 }
214
215 void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
216 {
217 if (!con) {
218 con = active_console;
219 }
220 if (con && con->hw_text_update)
221 con->hw_text_update(con->hw, chardata);
222 }
223
224 static void vga_fill_rect(QemuConsole *con,
225 int posx, int posy, int width, int height,
226 pixman_color_t color)
227 {
228 DisplaySurface *surface = qemu_console_surface(con);
229 pixman_rectangle16_t rect = {
230 .x = posx, .y = posy, .width = width, .height = height
231 };
232
233 pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
234 &color, 1, &rect);
235 }
236
237 /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
238 static void vga_bitblt(QemuConsole *con,
239 int xs, int ys, int xd, int yd, int w, int h)
240 {
241 DisplaySurface *surface = qemu_console_surface(con);
242
243 pixman_image_composite(PIXMAN_OP_SRC,
244 surface->image, NULL, surface->image,
245 xs, ys, 0, 0, xd, yd, w, h);
246 }
247
248 /***********************************************************/
249 /* basic char display */
250
251 #define FONT_HEIGHT 16
252 #define FONT_WIDTH 8
253
254 #include "vgafont.h"
255
256 #ifndef CONFIG_CURSES
257 enum color_names {
258 COLOR_BLACK = 0,
259 COLOR_RED = 1,
260 COLOR_GREEN = 2,
261 COLOR_YELLOW = 3,
262 COLOR_BLUE = 4,
263 COLOR_MAGENTA = 5,
264 COLOR_CYAN = 6,
265 COLOR_WHITE = 7
266 };
267 #endif
268
269 #define QEMU_RGB(r, g, b) \
270 { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
271
272 static const pixman_color_t color_table_rgb[2][8] = {
273 { /* dark */
274 QEMU_RGB(0x00, 0x00, 0x00), /* black */
275 QEMU_RGB(0xaa, 0x00, 0x00), /* red */
276 QEMU_RGB(0x00, 0xaa, 0x00), /* green */
277 QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
278 QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
279 QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
280 QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
281 QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
282 },
283 { /* bright */
284 QEMU_RGB(0x00, 0x00, 0x00), /* black */
285 QEMU_RGB(0xff, 0x00, 0x00), /* red */
286 QEMU_RGB(0x00, 0xff, 0x00), /* green */
287 QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
288 QEMU_RGB(0x00, 0x00, 0xff), /* blue */
289 QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
290 QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
291 QEMU_RGB(0xff, 0xff, 0xff), /* white */
292 }
293 };
294
295 #ifdef DEBUG_CONSOLE
296 static void console_print_text_attributes(TextAttributes *t_attrib, char ch)
297 {
298 if (t_attrib->bold) {
299 printf("b");
300 } else {
301 printf(" ");
302 }
303 if (t_attrib->uline) {
304 printf("u");
305 } else {
306 printf(" ");
307 }
308 if (t_attrib->blink) {
309 printf("l");
310 } else {
311 printf(" ");
312 }
313 if (t_attrib->invers) {
314 printf("i");
315 } else {
316 printf(" ");
317 }
318 if (t_attrib->unvisible) {
319 printf("n");
320 } else {
321 printf(" ");
322 }
323
324 printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib->fgcol, t_attrib->bgcol, ch, ch);
325 }
326 #endif
327
328 static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
329 TextAttributes *t_attrib)
330 {
331 static pixman_image_t *glyphs[256];
332 DisplaySurface *surface = qemu_console_surface(s);
333 pixman_color_t fgcol, bgcol;
334
335 if (t_attrib->invers) {
336 bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
337 fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
338 } else {
339 fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
340 bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
341 }
342
343 if (!glyphs[ch]) {
344 glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
345 }
346 qemu_pixman_glyph_render(glyphs[ch], surface->image,
347 &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
348 }
349
350 static void text_console_resize(QemuConsole *s)
351 {
352 TextCell *cells, *c, *c1;
353 int w1, x, y, last_width;
354
355 last_width = s->width;
356 s->width = s->g_width / FONT_WIDTH;
357 s->height = s->g_height / FONT_HEIGHT;
358
359 w1 = last_width;
360 if (s->width < w1)
361 w1 = s->width;
362
363 cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
364 for(y = 0; y < s->total_height; y++) {
365 c = &cells[y * s->width];
366 if (w1 > 0) {
367 c1 = &s->cells[y * last_width];
368 for(x = 0; x < w1; x++) {
369 *c++ = *c1++;
370 }
371 }
372 for(x = w1; x < s->width; x++) {
373 c->ch = ' ';
374 c->t_attrib = s->t_attrib_default;
375 c++;
376 }
377 }
378 g_free(s->cells);
379 s->cells = cells;
380 }
381
382 static inline void text_update_xy(QemuConsole *s, int x, int y)
383 {
384 s->text_x[0] = MIN(s->text_x[0], x);
385 s->text_x[1] = MAX(s->text_x[1], x);
386 s->text_y[0] = MIN(s->text_y[0], y);
387 s->text_y[1] = MAX(s->text_y[1], y);
388 }
389
390 static void invalidate_xy(QemuConsole *s, int x, int y)
391 {
392 if (s->update_x0 > x * FONT_WIDTH)
393 s->update_x0 = x * FONT_WIDTH;
394 if (s->update_y0 > y * FONT_HEIGHT)
395 s->update_y0 = y * FONT_HEIGHT;
396 if (s->update_x1 < (x + 1) * FONT_WIDTH)
397 s->update_x1 = (x + 1) * FONT_WIDTH;
398 if (s->update_y1 < (y + 1) * FONT_HEIGHT)
399 s->update_y1 = (y + 1) * FONT_HEIGHT;
400 }
401
402 static void update_xy(QemuConsole *s, int x, int y)
403 {
404 TextCell *c;
405 int y1, y2;
406
407 if (s != active_console) {
408 return;
409 }
410
411 if (s->ds->have_text) {
412 text_update_xy(s, x, y);
413 }
414
415 if (s->ds->have_gfx) {
416 y1 = (s->y_base + y) % s->total_height;
417 y2 = y1 - s->y_displayed;
418 if (y2 < 0)
419 y2 += s->total_height;
420 if (y2 < s->height) {
421 c = &s->cells[y1 * s->width + x];
422 vga_putcharxy(s, x, y2, c->ch,
423 &(c->t_attrib));
424 invalidate_xy(s, x, y2);
425 }
426 }
427 }
428
429 static void console_show_cursor(QemuConsole *s, int show)
430 {
431 TextCell *c;
432 int y, y1;
433 int x = s->x;
434
435 if (s != active_console) {
436 return;
437 }
438
439 if (s->ds->have_text) {
440 s->cursor_invalidate = 1;
441 }
442
443 if (s->ds->have_gfx) {
444 if (x >= s->width) {
445 x = s->width - 1;
446 }
447 y1 = (s->y_base + s->y) % s->total_height;
448 y = y1 - s->y_displayed;
449 if (y < 0)
450 y += s->total_height;
451 if (y < s->height) {
452 c = &s->cells[y1 * s->width + x];
453 if (show && s->cursor_visible_phase) {
454 TextAttributes t_attrib = s->t_attrib_default;
455 t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
456 vga_putcharxy(s, x, y, c->ch, &t_attrib);
457 } else {
458 vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
459 }
460 invalidate_xy(s, x, y);
461 }
462 }
463 }
464
465 static void console_refresh(QemuConsole *s)
466 {
467 DisplaySurface *surface = qemu_console_surface(s);
468 TextCell *c;
469 int x, y, y1;
470
471 if (s != active_console)
472 return;
473
474 if (s->ds->have_text) {
475 s->text_x[0] = 0;
476 s->text_y[0] = 0;
477 s->text_x[1] = s->width - 1;
478 s->text_y[1] = s->height - 1;
479 s->cursor_invalidate = 1;
480 }
481
482 if (s->ds->have_gfx) {
483 vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
484 color_table_rgb[0][COLOR_BLACK]);
485 y1 = s->y_displayed;
486 for (y = 0; y < s->height; y++) {
487 c = s->cells + y1 * s->width;
488 for (x = 0; x < s->width; x++) {
489 vga_putcharxy(s, x, y, c->ch,
490 &(c->t_attrib));
491 c++;
492 }
493 if (++y1 == s->total_height) {
494 y1 = 0;
495 }
496 }
497 console_show_cursor(s, 1);
498 dpy_gfx_update(s, 0, 0,
499 surface_width(surface), surface_height(surface));
500 }
501 }
502
503 static void console_scroll(int ydelta)
504 {
505 QemuConsole *s;
506 int i, y1;
507
508 s = active_console;
509 if (!s || (s->console_type == GRAPHIC_CONSOLE))
510 return;
511
512 if (ydelta > 0) {
513 for(i = 0; i < ydelta; i++) {
514 if (s->y_displayed == s->y_base)
515 break;
516 if (++s->y_displayed == s->total_height)
517 s->y_displayed = 0;
518 }
519 } else {
520 ydelta = -ydelta;
521 i = s->backscroll_height;
522 if (i > s->total_height - s->height)
523 i = s->total_height - s->height;
524 y1 = s->y_base - i;
525 if (y1 < 0)
526 y1 += s->total_height;
527 for(i = 0; i < ydelta; i++) {
528 if (s->y_displayed == y1)
529 break;
530 if (--s->y_displayed < 0)
531 s->y_displayed = s->total_height - 1;
532 }
533 }
534 console_refresh(s);
535 }
536
537 static void console_put_lf(QemuConsole *s)
538 {
539 TextCell *c;
540 int x, y1;
541
542 s->y++;
543 if (s->y >= s->height) {
544 s->y = s->height - 1;
545
546 if (s->y_displayed == s->y_base) {
547 if (++s->y_displayed == s->total_height)
548 s->y_displayed = 0;
549 }
550 if (++s->y_base == s->total_height)
551 s->y_base = 0;
552 if (s->backscroll_height < s->total_height)
553 s->backscroll_height++;
554 y1 = (s->y_base + s->height - 1) % s->total_height;
555 c = &s->cells[y1 * s->width];
556 for(x = 0; x < s->width; x++) {
557 c->ch = ' ';
558 c->t_attrib = s->t_attrib_default;
559 c++;
560 }
561 if (s == active_console && s->y_displayed == s->y_base) {
562 if (s->ds->have_text) {
563 s->text_x[0] = 0;
564 s->text_y[0] = 0;
565 s->text_x[1] = s->width - 1;
566 s->text_y[1] = s->height - 1;
567 }
568
569 if (s->ds->have_gfx) {
570 vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
571 s->width * FONT_WIDTH,
572 (s->height - 1) * FONT_HEIGHT);
573 vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
574 s->width * FONT_WIDTH, FONT_HEIGHT,
575 color_table_rgb[0][s->t_attrib_default.bgcol]);
576 s->update_x0 = 0;
577 s->update_y0 = 0;
578 s->update_x1 = s->width * FONT_WIDTH;
579 s->update_y1 = s->height * FONT_HEIGHT;
580 }
581 }
582 }
583 }
584
585 /* Set console attributes depending on the current escape codes.
586 * NOTE: I know this code is not very efficient (checking every color for it
587 * self) but it is more readable and better maintainable.
588 */
589 static void console_handle_escape(QemuConsole *s)
590 {
591 int i;
592
593 for (i=0; i<s->nb_esc_params; i++) {
594 switch (s->esc_params[i]) {
595 case 0: /* reset all console attributes to default */
596 s->t_attrib = s->t_attrib_default;
597 break;
598 case 1:
599 s->t_attrib.bold = 1;
600 break;
601 case 4:
602 s->t_attrib.uline = 1;
603 break;
604 case 5:
605 s->t_attrib.blink = 1;
606 break;
607 case 7:
608 s->t_attrib.invers = 1;
609 break;
610 case 8:
611 s->t_attrib.unvisible = 1;
612 break;
613 case 22:
614 s->t_attrib.bold = 0;
615 break;
616 case 24:
617 s->t_attrib.uline = 0;
618 break;
619 case 25:
620 s->t_attrib.blink = 0;
621 break;
622 case 27:
623 s->t_attrib.invers = 0;
624 break;
625 case 28:
626 s->t_attrib.unvisible = 0;
627 break;
628 /* set foreground color */
629 case 30:
630 s->t_attrib.fgcol=COLOR_BLACK;
631 break;
632 case 31:
633 s->t_attrib.fgcol=COLOR_RED;
634 break;
635 case 32:
636 s->t_attrib.fgcol=COLOR_GREEN;
637 break;
638 case 33:
639 s->t_attrib.fgcol=COLOR_YELLOW;
640 break;
641 case 34:
642 s->t_attrib.fgcol=COLOR_BLUE;
643 break;
644 case 35:
645 s->t_attrib.fgcol=COLOR_MAGENTA;
646 break;
647 case 36:
648 s->t_attrib.fgcol=COLOR_CYAN;
649 break;
650 case 37:
651 s->t_attrib.fgcol=COLOR_WHITE;
652 break;
653 /* set background color */
654 case 40:
655 s->t_attrib.bgcol=COLOR_BLACK;
656 break;
657 case 41:
658 s->t_attrib.bgcol=COLOR_RED;
659 break;
660 case 42:
661 s->t_attrib.bgcol=COLOR_GREEN;
662 break;
663 case 43:
664 s->t_attrib.bgcol=COLOR_YELLOW;
665 break;
666 case 44:
667 s->t_attrib.bgcol=COLOR_BLUE;
668 break;
669 case 45:
670 s->t_attrib.bgcol=COLOR_MAGENTA;
671 break;
672 case 46:
673 s->t_attrib.bgcol=COLOR_CYAN;
674 break;
675 case 47:
676 s->t_attrib.bgcol=COLOR_WHITE;
677 break;
678 }
679 }
680 }
681
682 static void console_clear_xy(QemuConsole *s, int x, int y)
683 {
684 int y1 = (s->y_base + y) % s->total_height;
685 TextCell *c = &s->cells[y1 * s->width + x];
686 c->ch = ' ';
687 c->t_attrib = s->t_attrib_default;
688 update_xy(s, x, y);
689 }
690
691 /* set cursor, checking bounds */
692 static void set_cursor(QemuConsole *s, int x, int y)
693 {
694 if (x < 0) {
695 x = 0;
696 }
697 if (y < 0) {
698 y = 0;
699 }
700 if (y >= s->height) {
701 y = s->height - 1;
702 }
703 if (x >= s->width) {
704 x = s->width - 1;
705 }
706
707 s->x = x;
708 s->y = y;
709 }
710
711 static void console_putchar(QemuConsole *s, int ch)
712 {
713 TextCell *c;
714 int y1, i;
715 int x, y;
716
717 switch(s->state) {
718 case TTY_STATE_NORM:
719 switch(ch) {
720 case '\r': /* carriage return */
721 s->x = 0;
722 break;
723 case '\n': /* newline */
724 console_put_lf(s);
725 break;
726 case '\b': /* backspace */
727 if (s->x > 0)
728 s->x--;
729 break;
730 case '\t': /* tabspace */
731 if (s->x + (8 - (s->x % 8)) > s->width) {
732 s->x = 0;
733 console_put_lf(s);
734 } else {
735 s->x = s->x + (8 - (s->x % 8));
736 }
737 break;
738 case '\a': /* alert aka. bell */
739 /* TODO: has to be implemented */
740 break;
741 case 14:
742 /* SI (shift in), character set 0 (ignored) */
743 break;
744 case 15:
745 /* SO (shift out), character set 1 (ignored) */
746 break;
747 case 27: /* esc (introducing an escape sequence) */
748 s->state = TTY_STATE_ESC;
749 break;
750 default:
751 if (s->x >= s->width) {
752 /* line wrap */
753 s->x = 0;
754 console_put_lf(s);
755 }
756 y1 = (s->y_base + s->y) % s->total_height;
757 c = &s->cells[y1 * s->width + s->x];
758 c->ch = ch;
759 c->t_attrib = s->t_attrib;
760 update_xy(s, s->x, s->y);
761 s->x++;
762 break;
763 }
764 break;
765 case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
766 if (ch == '[') {
767 for(i=0;i<MAX_ESC_PARAMS;i++)
768 s->esc_params[i] = 0;
769 s->nb_esc_params = 0;
770 s->state = TTY_STATE_CSI;
771 } else {
772 s->state = TTY_STATE_NORM;
773 }
774 break;
775 case TTY_STATE_CSI: /* handle escape sequence parameters */
776 if (ch >= '0' && ch <= '9') {
777 if (s->nb_esc_params < MAX_ESC_PARAMS) {
778 int *param = &s->esc_params[s->nb_esc_params];
779 int digit = (ch - '0');
780
781 *param = (*param <= (INT_MAX - digit) / 10) ?
782 *param * 10 + digit : INT_MAX;
783 }
784 } else {
785 if (s->nb_esc_params < MAX_ESC_PARAMS)
786 s->nb_esc_params++;
787 if (ch == ';')
788 break;
789 #ifdef DEBUG_CONSOLE
790 fprintf(stderr, "escape sequence CSI%d;%d%c, %d parameters\n",
791 s->esc_params[0], s->esc_params[1], ch, s->nb_esc_params);
792 #endif
793 s->state = TTY_STATE_NORM;
794 switch(ch) {
795 case 'A':
796 /* move cursor up */
797 if (s->esc_params[0] == 0) {
798 s->esc_params[0] = 1;
799 }
800 set_cursor(s, s->x, s->y - s->esc_params[0]);
801 break;
802 case 'B':
803 /* move cursor down */
804 if (s->esc_params[0] == 0) {
805 s->esc_params[0] = 1;
806 }
807 set_cursor(s, s->x, s->y + s->esc_params[0]);
808 break;
809 case 'C':
810 /* move cursor right */
811 if (s->esc_params[0] == 0) {
812 s->esc_params[0] = 1;
813 }
814 set_cursor(s, s->x + s->esc_params[0], s->y);
815 break;
816 case 'D':
817 /* move cursor left */
818 if (s->esc_params[0] == 0) {
819 s->esc_params[0] = 1;
820 }
821 set_cursor(s, s->x - s->esc_params[0], s->y);
822 break;
823 case 'G':
824 /* move cursor to column */
825 set_cursor(s, s->esc_params[0] - 1, s->y);
826 break;
827 case 'f':
828 case 'H':
829 /* move cursor to row, column */
830 set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
831 break;
832 case 'J':
833 switch (s->esc_params[0]) {
834 case 0:
835 /* clear to end of screen */
836 for (y = s->y; y < s->height; y++) {
837 for (x = 0; x < s->width; x++) {
838 if (y == s->y && x < s->x) {
839 continue;
840 }
841 console_clear_xy(s, x, y);
842 }
843 }
844 break;
845 case 1:
846 /* clear from beginning of screen */
847 for (y = 0; y <= s->y; y++) {
848 for (x = 0; x < s->width; x++) {
849 if (y == s->y && x > s->x) {
850 break;
851 }
852 console_clear_xy(s, x, y);
853 }
854 }
855 break;
856 case 2:
857 /* clear entire screen */
858 for (y = 0; y <= s->height; y++) {
859 for (x = 0; x < s->width; x++) {
860 console_clear_xy(s, x, y);
861 }
862 }
863 break;
864 }
865 break;
866 case 'K':
867 switch (s->esc_params[0]) {
868 case 0:
869 /* clear to eol */
870 for(x = s->x; x < s->width; x++) {
871 console_clear_xy(s, x, s->y);
872 }
873 break;
874 case 1:
875 /* clear from beginning of line */
876 for (x = 0; x <= s->x; x++) {
877 console_clear_xy(s, x, s->y);
878 }
879 break;
880 case 2:
881 /* clear entire line */
882 for(x = 0; x < s->width; x++) {
883 console_clear_xy(s, x, s->y);
884 }
885 break;
886 }
887 break;
888 case 'm':
889 console_handle_escape(s);
890 break;
891 case 'n':
892 /* report cursor position */
893 /* TODO: send ESC[row;colR */
894 break;
895 case 's':
896 /* save cursor position */
897 s->x_saved = s->x;
898 s->y_saved = s->y;
899 break;
900 case 'u':
901 /* restore cursor position */
902 s->x = s->x_saved;
903 s->y = s->y_saved;
904 break;
905 default:
906 #ifdef DEBUG_CONSOLE
907 fprintf(stderr, "unhandled escape character '%c'\n", ch);
908 #endif
909 break;
910 }
911 break;
912 }
913 }
914 }
915
916 void console_select(unsigned int index)
917 {
918 DisplaySurface *surface;
919 QemuConsole *s;
920
921 if (index >= MAX_CONSOLES)
922 return;
923
924 trace_console_select(index);
925 if (active_console) {
926 surface = qemu_console_surface(active_console);
927 active_console->g_width = surface_width(surface);
928 active_console->g_height = surface_height(surface);
929 }
930 s = consoles[index];
931 if (s) {
932 DisplayState *ds = s->ds;
933
934 if (active_console && active_console->cursor_timer) {
935 qemu_del_timer(active_console->cursor_timer);
936 }
937 active_console = s;
938 if (ds->have_gfx) {
939 dpy_gfx_switch_surface(ds, s->surface);
940 dpy_gfx_update(s, 0, 0, surface_width(s->surface),
941 surface_height(s->surface));
942 }
943 if (ds->have_text) {
944 dpy_text_resize(s, s->width, s->height);
945 }
946 if (s->cursor_timer) {
947 qemu_mod_timer(s->cursor_timer,
948 qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
949 }
950 }
951 }
952
953 static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
954 {
955 QemuConsole *s = chr->opaque;
956 int i;
957
958 s->update_x0 = s->width * FONT_WIDTH;
959 s->update_y0 = s->height * FONT_HEIGHT;
960 s->update_x1 = 0;
961 s->update_y1 = 0;
962 console_show_cursor(s, 0);
963 for(i = 0; i < len; i++) {
964 console_putchar(s, buf[i]);
965 }
966 console_show_cursor(s, 1);
967 if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
968 dpy_gfx_update(s, s->update_x0, s->update_y0,
969 s->update_x1 - s->update_x0,
970 s->update_y1 - s->update_y0);
971 }
972 return len;
973 }
974
975 static void kbd_send_chars(void *opaque)
976 {
977 QemuConsole *s = opaque;
978 int len;
979 uint8_t buf[16];
980
981 len = qemu_chr_be_can_write(s->chr);
982 if (len > s->out_fifo.count)
983 len = s->out_fifo.count;
984 if (len > 0) {
985 if (len > sizeof(buf))
986 len = sizeof(buf);
987 qemu_fifo_read(&s->out_fifo, buf, len);
988 qemu_chr_be_write(s->chr, buf, len);
989 }
990 /* characters are pending: we send them a bit later (XXX:
991 horrible, should change char device API) */
992 if (s->out_fifo.count > 0) {
993 qemu_mod_timer(s->kbd_timer, qemu_get_clock_ms(rt_clock) + 1);
994 }
995 }
996
997 /* called when an ascii key is pressed */
998 void kbd_put_keysym(int keysym)
999 {
1000 QemuConsole *s;
1001 uint8_t buf[16], *q;
1002 int c;
1003
1004 s = active_console;
1005 if (!s || (s->console_type == GRAPHIC_CONSOLE))
1006 return;
1007
1008 switch(keysym) {
1009 case QEMU_KEY_CTRL_UP:
1010 console_scroll(-1);
1011 break;
1012 case QEMU_KEY_CTRL_DOWN:
1013 console_scroll(1);
1014 break;
1015 case QEMU_KEY_CTRL_PAGEUP:
1016 console_scroll(-10);
1017 break;
1018 case QEMU_KEY_CTRL_PAGEDOWN:
1019 console_scroll(10);
1020 break;
1021 default:
1022 /* convert the QEMU keysym to VT100 key string */
1023 q = buf;
1024 if (keysym >= 0xe100 && keysym <= 0xe11f) {
1025 *q++ = '\033';
1026 *q++ = '[';
1027 c = keysym - 0xe100;
1028 if (c >= 10)
1029 *q++ = '0' + (c / 10);
1030 *q++ = '0' + (c % 10);
1031 *q++ = '~';
1032 } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
1033 *q++ = '\033';
1034 *q++ = '[';
1035 *q++ = keysym & 0xff;
1036 } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
1037 console_puts(s->chr, (const uint8_t *) "\r", 1);
1038 *q++ = '\n';
1039 } else {
1040 *q++ = keysym;
1041 }
1042 if (s->echo) {
1043 console_puts(s->chr, buf, q - buf);
1044 }
1045 if (s->chr->chr_read) {
1046 qemu_fifo_write(&s->out_fifo, buf, q - buf);
1047 kbd_send_chars(s);
1048 }
1049 break;
1050 }
1051 }
1052
1053 static void text_console_invalidate(void *opaque)
1054 {
1055 QemuConsole *s = (QemuConsole *) opaque;
1056 DisplaySurface *surface = qemu_console_surface(s);
1057
1058 if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
1059 s->g_width = surface_width(surface);
1060 s->g_height = surface_height(surface);
1061 text_console_resize(s);
1062 }
1063 console_refresh(s);
1064 }
1065
1066 static void text_console_update(void *opaque, console_ch_t *chardata)
1067 {
1068 QemuConsole *s = (QemuConsole *) opaque;
1069 int i, j, src;
1070
1071 if (s->text_x[0] <= s->text_x[1]) {
1072 src = (s->y_base + s->text_y[0]) * s->width;
1073 chardata += s->text_y[0] * s->width;
1074 for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
1075 for (j = 0; j < s->width; j ++, src ++)
1076 console_write_ch(chardata ++, s->cells[src].ch |
1077 (s->cells[src].t_attrib.fgcol << 12) |
1078 (s->cells[src].t_attrib.bgcol << 8) |
1079 (s->cells[src].t_attrib.bold << 21));
1080 dpy_text_update(s, s->text_x[0], s->text_y[0],
1081 s->text_x[1] - s->text_x[0], i - s->text_y[0]);
1082 s->text_x[0] = s->width;
1083 s->text_y[0] = s->height;
1084 s->text_x[1] = 0;
1085 s->text_y[1] = 0;
1086 }
1087 if (s->cursor_invalidate) {
1088 dpy_text_cursor(s, s->x, s->y);
1089 s->cursor_invalidate = 0;
1090 }
1091 }
1092
1093 static QemuConsole *new_console(DisplayState *ds, console_type_t console_type)
1094 {
1095 QemuConsole *s;
1096 int i;
1097
1098 if (nb_consoles >= MAX_CONSOLES)
1099 return NULL;
1100 s = g_malloc0(sizeof(QemuConsole));
1101 if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
1102 (console_type == GRAPHIC_CONSOLE))) {
1103 active_console = s;
1104 }
1105 s->ds = ds;
1106 s->console_type = console_type;
1107 if (console_type != GRAPHIC_CONSOLE) {
1108 s->index = nb_consoles;
1109 consoles[nb_consoles++] = s;
1110 } else {
1111 /* HACK: Put graphical consoles before text consoles. */
1112 for (i = nb_consoles; i > 0; i--) {
1113 if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
1114 break;
1115 consoles[i] = consoles[i - 1];
1116 consoles[i]->index = i;
1117 }
1118 s->index = i;
1119 consoles[i] = s;
1120 nb_consoles++;
1121 }
1122 return s;
1123 }
1124
1125 static void qemu_alloc_display(DisplaySurface *surface, int width, int height,
1126 int linesize, PixelFormat pf, int newflags)
1127 {
1128 surface->pf = pf;
1129
1130 qemu_pixman_image_unref(surface->image);
1131 surface->image = NULL;
1132
1133 surface->format = qemu_pixman_get_format(&pf);
1134 assert(surface->format != 0);
1135 surface->image = pixman_image_create_bits(surface->format,
1136 width, height,
1137 NULL, linesize);
1138 assert(surface->image != NULL);
1139
1140 surface->flags = newflags | QEMU_ALLOCATED_FLAG;
1141 #ifdef HOST_WORDS_BIGENDIAN
1142 surface->flags |= QEMU_BIG_ENDIAN_FLAG;
1143 #endif
1144 }
1145
1146 DisplaySurface *qemu_create_displaysurface(int width, int height)
1147 {
1148 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1149 int linesize = width * 4;
1150
1151 trace_displaysurface_create(surface, width, height);
1152 qemu_alloc_display(surface, width, height, linesize,
1153 qemu_default_pixelformat(32), 0);
1154 return surface;
1155 }
1156
1157 DisplaySurface *qemu_create_displaysurface_from(int width, int height, int bpp,
1158 int linesize, uint8_t *data,
1159 bool byteswap)
1160 {
1161 DisplaySurface *surface = g_new0(DisplaySurface, 1);
1162
1163 trace_displaysurface_create_from(surface, width, height, bpp, byteswap);
1164 if (byteswap) {
1165 surface->pf = qemu_different_endianness_pixelformat(bpp);
1166 } else {
1167 surface->pf = qemu_default_pixelformat(bpp);
1168 }
1169
1170 surface->format = qemu_pixman_get_format(&surface->pf);
1171 assert(surface->format != 0);
1172 surface->image = pixman_image_create_bits(surface->format,
1173 width, height,
1174 (void *)data, linesize);
1175 assert(surface->image != NULL);
1176
1177 #ifdef HOST_WORDS_BIGENDIAN
1178 surface->flags = QEMU_BIG_ENDIAN_FLAG;
1179 #endif
1180
1181 return surface;
1182 }
1183
1184 void qemu_free_displaysurface(DisplaySurface *surface)
1185 {
1186 if (surface == NULL) {
1187 return;
1188 }
1189 trace_displaysurface_free(surface);
1190 qemu_pixman_image_unref(surface->image);
1191 g_free(surface);
1192 }
1193
1194 void register_displaychangelistener(DisplayState *ds,
1195 DisplayChangeListener *dcl)
1196 {
1197 trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
1198 dcl->ds = ds;
1199 QLIST_INSERT_HEAD(&ds->listeners, dcl, next);
1200 gui_setup_refresh(ds);
1201 if (dcl->ops->dpy_gfx_switch && active_console) {
1202 dcl->ops->dpy_gfx_switch(dcl, active_console->surface);
1203 }
1204 }
1205
1206 void unregister_displaychangelistener(DisplayChangeListener *dcl)
1207 {
1208 DisplayState *ds = dcl->ds;
1209 trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
1210 QLIST_REMOVE(dcl, next);
1211 gui_setup_refresh(ds);
1212 }
1213
1214 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
1215 {
1216 DisplayState *s = con->ds;
1217 struct DisplayChangeListener *dcl;
1218 int width = surface_width(con->surface);
1219 int height = surface_height(con->surface);
1220
1221 x = MAX(x, 0);
1222 y = MAX(y, 0);
1223 x = MIN(x, width);
1224 y = MIN(y, height);
1225 w = MIN(w, width - x);
1226 h = MIN(h, height - y);
1227
1228 if (con != active_console) {
1229 return;
1230 }
1231 QLIST_FOREACH(dcl, &s->listeners, next) {
1232 if (dcl->ops->dpy_gfx_update) {
1233 dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
1234 }
1235 }
1236 }
1237
1238 static void dpy_gfx_switch_surface(DisplayState *ds,
1239 DisplaySurface *surface)
1240 {
1241 struct DisplayChangeListener *dcl;
1242
1243 QLIST_FOREACH(dcl, &ds->listeners, next) {
1244 if (dcl->ops->dpy_gfx_switch) {
1245 dcl->ops->dpy_gfx_switch(dcl, surface);
1246 }
1247 }
1248 }
1249
1250 void dpy_gfx_replace_surface(QemuConsole *con,
1251 DisplaySurface *surface)
1252 {
1253 DisplayState *s = con->ds;
1254 DisplaySurface *old_surface = con->surface;
1255
1256 con->surface = surface;
1257 if (con == active_console) {
1258 dpy_gfx_switch_surface(s, surface);
1259 }
1260 qemu_free_displaysurface(old_surface);
1261 }
1262
1263 void dpy_refresh(DisplayState *s)
1264 {
1265 struct DisplayChangeListener *dcl;
1266 QLIST_FOREACH(dcl, &s->listeners, next) {
1267 if (dcl->ops->dpy_refresh) {
1268 dcl->ops->dpy_refresh(dcl);
1269 }
1270 }
1271 }
1272
1273 void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
1274 int dst_x, int dst_y, int w, int h)
1275 {
1276 DisplayState *s = con->ds;
1277 struct DisplayChangeListener *dcl;
1278
1279 if (con != active_console) {
1280 return;
1281 }
1282 QLIST_FOREACH(dcl, &s->listeners, next) {
1283 if (dcl->ops->dpy_gfx_copy) {
1284 dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
1285 } else { /* TODO */
1286 dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
1287 }
1288 }
1289 }
1290
1291 void dpy_text_cursor(QemuConsole *con, int x, int y)
1292 {
1293 DisplayState *s = con->ds;
1294 struct DisplayChangeListener *dcl;
1295
1296 if (con != active_console) {
1297 return;
1298 }
1299 QLIST_FOREACH(dcl, &s->listeners, next) {
1300 if (dcl->ops->dpy_text_cursor) {
1301 dcl->ops->dpy_text_cursor(dcl, x, y);
1302 }
1303 }
1304 }
1305
1306 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
1307 {
1308 DisplayState *s = con->ds;
1309 struct DisplayChangeListener *dcl;
1310
1311 if (con != active_console) {
1312 return;
1313 }
1314 QLIST_FOREACH(dcl, &s->listeners, next) {
1315 if (dcl->ops->dpy_text_update) {
1316 dcl->ops->dpy_text_update(dcl, x, y, w, h);
1317 }
1318 }
1319 }
1320
1321 void dpy_text_resize(QemuConsole *con, int w, int h)
1322 {
1323 DisplayState *s = con->ds;
1324 struct DisplayChangeListener *dcl;
1325
1326 if (con != active_console) {
1327 return;
1328 }
1329 QLIST_FOREACH(dcl, &s->listeners, next) {
1330 if (dcl->ops->dpy_text_resize) {
1331 dcl->ops->dpy_text_resize(dcl, w, h);
1332 }
1333 }
1334 }
1335
1336 void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
1337 {
1338 DisplayState *s = con->ds;
1339 struct DisplayChangeListener *dcl;
1340
1341 if (con != active_console) {
1342 return;
1343 }
1344 QLIST_FOREACH(dcl, &s->listeners, next) {
1345 if (dcl->ops->dpy_mouse_set) {
1346 dcl->ops->dpy_mouse_set(dcl, x, y, on);
1347 }
1348 }
1349 }
1350
1351 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
1352 {
1353 DisplayState *s = con->ds;
1354 struct DisplayChangeListener *dcl;
1355
1356 if (con != active_console) {
1357 return;
1358 }
1359 QLIST_FOREACH(dcl, &s->listeners, next) {
1360 if (dcl->ops->dpy_cursor_define) {
1361 dcl->ops->dpy_cursor_define(dcl, cursor);
1362 }
1363 }
1364 }
1365
1366 bool dpy_cursor_define_supported(QemuConsole *con)
1367 {
1368 DisplayState *s = con->ds;
1369 struct DisplayChangeListener *dcl;
1370 QLIST_FOREACH(dcl, &s->listeners, next) {
1371 if (dcl->ops->dpy_cursor_define) {
1372 return true;
1373 }
1374 }
1375 return false;
1376 }
1377
1378 /***********************************************************/
1379 /* register display */
1380
1381 /* console.c internal use only */
1382 static DisplayState *get_alloc_displaystate(void)
1383 {
1384 if (!display_state) {
1385 display_state = g_new0(DisplayState, 1);
1386 }
1387 return display_state;
1388 }
1389
1390 /*
1391 * Called by main(), after creating QemuConsoles
1392 * and before initializing ui (sdl/vnc/...).
1393 */
1394 DisplayState *init_displaystate(void)
1395 {
1396 int i;
1397
1398 if (!display_state) {
1399 display_state = g_new0(DisplayState, 1);
1400 }
1401
1402 for (i = 0; i < nb_consoles; i++) {
1403 if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
1404 consoles[i]->ds == NULL) {
1405 text_console_do_init(consoles[i]->chr, display_state);
1406 }
1407 }
1408
1409 return display_state;
1410 }
1411
1412 QemuConsole *graphic_console_init(graphic_hw_update_ptr update,
1413 graphic_hw_invalidate_ptr invalidate,
1414 graphic_hw_screen_dump_ptr screen_dump,
1415 graphic_hw_text_update_ptr text_update,
1416 void *opaque)
1417 {
1418 int width = 640;
1419 int height = 480;
1420 QemuConsole *s;
1421 DisplayState *ds;
1422
1423 ds = get_alloc_displaystate();
1424 trace_console_gfx_new();
1425 s = new_console(ds, GRAPHIC_CONSOLE);
1426 s->hw_update = update;
1427 s->hw_invalidate = invalidate;
1428 s->hw_screen_dump = screen_dump;
1429 s->hw_text_update = text_update;
1430 s->hw = opaque;
1431
1432 s->surface = qemu_create_displaysurface(width, height);
1433 return s;
1434 }
1435
1436 int is_graphic_console(void)
1437 {
1438 return active_console && active_console->console_type == GRAPHIC_CONSOLE;
1439 }
1440
1441 int is_fixedsize_console(void)
1442 {
1443 return active_console && active_console->console_type != TEXT_CONSOLE;
1444 }
1445
1446 static void text_console_set_echo(CharDriverState *chr, bool echo)
1447 {
1448 QemuConsole *s = chr->opaque;
1449
1450 s->echo = echo;
1451 }
1452
1453 static void text_console_update_cursor(void *opaque)
1454 {
1455 QemuConsole *s = opaque;
1456
1457 s->cursor_visible_phase = !s->cursor_visible_phase;
1458 graphic_hw_invalidate(s);
1459 qemu_mod_timer(s->cursor_timer,
1460 qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
1461 }
1462
1463 static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
1464 {
1465 QemuConsole *s;
1466
1467 s = chr->opaque;
1468
1469 chr->chr_write = console_puts;
1470
1471 s->out_fifo.buf = s->out_fifo_buf;
1472 s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
1473 s->kbd_timer = qemu_new_timer_ms(rt_clock, kbd_send_chars, s);
1474 s->ds = ds;
1475
1476 s->y_displayed = 0;
1477 s->y_base = 0;
1478 s->total_height = DEFAULT_BACKSCROLL;
1479 s->x = 0;
1480 s->y = 0;
1481 if (s->console_type == TEXT_CONSOLE) {
1482 if (active_console && active_console->surface) {
1483 s->g_width = surface_width(active_console->surface);
1484 s->g_height = surface_height(active_console->surface);
1485 } else {
1486 s->g_width = 80 * FONT_WIDTH;
1487 s->g_height = 24 * FONT_HEIGHT;
1488 }
1489 }
1490 s->surface = qemu_create_displaysurface(s->g_width, s->g_height);
1491
1492 s->cursor_timer =
1493 qemu_new_timer_ms(rt_clock, text_console_update_cursor, s);
1494
1495 s->hw_invalidate = text_console_invalidate;
1496 s->hw_text_update = text_console_update;
1497 s->hw = s;
1498
1499 /* Set text attribute defaults */
1500 s->t_attrib_default.bold = 0;
1501 s->t_attrib_default.uline = 0;
1502 s->t_attrib_default.blink = 0;
1503 s->t_attrib_default.invers = 0;
1504 s->t_attrib_default.unvisible = 0;
1505 s->t_attrib_default.fgcol = COLOR_WHITE;
1506 s->t_attrib_default.bgcol = COLOR_BLACK;
1507 /* set current text attributes to default */
1508 s->t_attrib = s->t_attrib_default;
1509 text_console_resize(s);
1510
1511 if (chr->label) {
1512 char msg[128];
1513 int len;
1514
1515 s->t_attrib.bgcol = COLOR_BLUE;
1516 len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
1517 console_puts(chr, (uint8_t*)msg, len);
1518 s->t_attrib = s->t_attrib_default;
1519 }
1520
1521 qemu_chr_be_generic_open(chr);
1522 if (chr->init)
1523 chr->init(chr);
1524 }
1525
1526 static CharDriverState *text_console_init(ChardevVC *vc)
1527 {
1528 CharDriverState *chr;
1529 QemuConsole *s;
1530 unsigned width = 0;
1531 unsigned height = 0;
1532
1533 chr = g_malloc0(sizeof(CharDriverState));
1534
1535 if (vc->has_width) {
1536 width = vc->width;
1537 } else if (vc->has_cols) {
1538 width = vc->cols * FONT_WIDTH;
1539 }
1540
1541 if (vc->has_height) {
1542 height = vc->height;
1543 } else if (vc->has_rows) {
1544 height = vc->rows * FONT_HEIGHT;
1545 }
1546
1547 trace_console_txt_new(width, height);
1548 if (width == 0 || height == 0) {
1549 s = new_console(NULL, TEXT_CONSOLE);
1550 } else {
1551 s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE);
1552 }
1553
1554 if (!s) {
1555 g_free(chr);
1556 return NULL;
1557 }
1558
1559 s->chr = chr;
1560 s->g_width = width;
1561 s->g_height = height;
1562 chr->opaque = s;
1563 chr->chr_set_echo = text_console_set_echo;
1564
1565 if (display_state) {
1566 text_console_do_init(chr, display_state);
1567 }
1568 return chr;
1569 }
1570
1571 static VcHandler *vc_handler = text_console_init;
1572
1573 CharDriverState *vc_init(ChardevVC *vc)
1574 {
1575 return vc_handler(vc);
1576 }
1577
1578 void register_vc_handler(VcHandler *handler)
1579 {
1580 vc_handler = handler;
1581 }
1582
1583 void qemu_console_resize(QemuConsole *s, int width, int height)
1584 {
1585 DisplaySurface *surface;
1586
1587 assert(s->console_type == GRAPHIC_CONSOLE);
1588 s->g_width = width;
1589 s->g_height = height;
1590 surface = qemu_create_displaysurface(width, height);
1591 dpy_gfx_replace_surface(s, surface);
1592 }
1593
1594 void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
1595 int dst_x, int dst_y, int w, int h)
1596 {
1597 assert(con->console_type == GRAPHIC_CONSOLE);
1598 dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
1599 }
1600
1601 DisplaySurface *qemu_console_surface(QemuConsole *console)
1602 {
1603 return console->surface;
1604 }
1605
1606 DisplayState *qemu_console_displaystate(QemuConsole *console)
1607 {
1608 return console->ds;
1609 }
1610
1611 PixelFormat qemu_different_endianness_pixelformat(int bpp)
1612 {
1613 PixelFormat pf;
1614
1615 memset(&pf, 0x00, sizeof(PixelFormat));
1616
1617 pf.bits_per_pixel = bpp;
1618 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
1619 pf.depth = bpp == 32 ? 24 : bpp;
1620
1621 switch (bpp) {
1622 case 24:
1623 pf.rmask = 0x000000FF;
1624 pf.gmask = 0x0000FF00;
1625 pf.bmask = 0x00FF0000;
1626 pf.rmax = 255;
1627 pf.gmax = 255;
1628 pf.bmax = 255;
1629 pf.rshift = 0;
1630 pf.gshift = 8;
1631 pf.bshift = 16;
1632 pf.rbits = 8;
1633 pf.gbits = 8;
1634 pf.bbits = 8;
1635 break;
1636 case 32:
1637 pf.rmask = 0x0000FF00;
1638 pf.gmask = 0x00FF0000;
1639 pf.bmask = 0xFF000000;
1640 pf.amask = 0x00000000;
1641 pf.amax = 255;
1642 pf.rmax = 255;
1643 pf.gmax = 255;
1644 pf.bmax = 255;
1645 pf.ashift = 0;
1646 pf.rshift = 8;
1647 pf.gshift = 16;
1648 pf.bshift = 24;
1649 pf.rbits = 8;
1650 pf.gbits = 8;
1651 pf.bbits = 8;
1652 pf.abits = 8;
1653 break;
1654 default:
1655 break;
1656 }
1657 return pf;
1658 }
1659
1660 PixelFormat qemu_default_pixelformat(int bpp)
1661 {
1662 PixelFormat pf;
1663
1664 memset(&pf, 0x00, sizeof(PixelFormat));
1665
1666 pf.bits_per_pixel = bpp;
1667 pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
1668 pf.depth = bpp == 32 ? 24 : bpp;
1669
1670 switch (bpp) {
1671 case 15:
1672 pf.bits_per_pixel = 16;
1673 pf.rmask = 0x00007c00;
1674 pf.gmask = 0x000003E0;
1675 pf.bmask = 0x0000001F;
1676 pf.rmax = 31;
1677 pf.gmax = 31;
1678 pf.bmax = 31;
1679 pf.rshift = 10;
1680 pf.gshift = 5;
1681 pf.bshift = 0;
1682 pf.rbits = 5;
1683 pf.gbits = 5;
1684 pf.bbits = 5;
1685 break;
1686 case 16:
1687 pf.rmask = 0x0000F800;
1688 pf.gmask = 0x000007E0;
1689 pf.bmask = 0x0000001F;
1690 pf.rmax = 31;
1691 pf.gmax = 63;
1692 pf.bmax = 31;
1693 pf.rshift = 11;
1694 pf.gshift = 5;
1695 pf.bshift = 0;
1696 pf.rbits = 5;
1697 pf.gbits = 6;
1698 pf.bbits = 5;
1699 break;
1700 case 24:
1701 pf.rmask = 0x00FF0000;
1702 pf.gmask = 0x0000FF00;
1703 pf.bmask = 0x000000FF;
1704 pf.rmax = 255;
1705 pf.gmax = 255;
1706 pf.bmax = 255;
1707 pf.rshift = 16;
1708 pf.gshift = 8;
1709 pf.bshift = 0;
1710 pf.rbits = 8;
1711 pf.gbits = 8;
1712 pf.bbits = 8;
1713 break;
1714 case 32:
1715 pf.rmask = 0x00FF0000;
1716 pf.gmask = 0x0000FF00;
1717 pf.bmask = 0x000000FF;
1718 pf.rmax = 255;
1719 pf.gmax = 255;
1720 pf.bmax = 255;
1721 pf.rshift = 16;
1722 pf.gshift = 8;
1723 pf.bshift = 0;
1724 pf.rbits = 8;
1725 pf.gbits = 8;
1726 pf.bbits = 8;
1727 break;
1728 default:
1729 break;
1730 }
1731 return pf;
1732 }
1733
1734 static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
1735 Error **errp)
1736 {
1737 int val;
1738
1739 backend->vc = g_new0(ChardevVC, 1);
1740
1741 val = qemu_opt_get_number(opts, "width", 0);
1742 if (val != 0) {
1743 backend->vc->has_width = true;
1744 backend->vc->width = val;
1745 }
1746
1747 val = qemu_opt_get_number(opts, "height", 0);
1748 if (val != 0) {
1749 backend->vc->has_height = true;
1750 backend->vc->height = val;
1751 }
1752
1753 val = qemu_opt_get_number(opts, "cols", 0);
1754 if (val != 0) {
1755 backend->vc->has_cols = true;
1756 backend->vc->cols = val;
1757 }
1758
1759 val = qemu_opt_get_number(opts, "rows", 0);
1760 if (val != 0) {
1761 backend->vc->has_rows = true;
1762 backend->vc->rows = val;
1763 }
1764 }
1765
1766 static void register_types(void)
1767 {
1768 register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC,
1769 qemu_chr_parse_vc);
1770 }
1771
1772 type_init(register_types);