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