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