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