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