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