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