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