]>
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 | ||
1562e531 GH |
518 | if (s != active_console) { |
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 | |
1562e531 GH |
546 | if (s != active_console) { |
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 | ||
5fafdf24 | 582 | if (s != active_console) |
e7f0ad58 | 583 | return; |
a93a4a22 GH |
584 | |
585 | if (s->ds->have_text) { | |
4d3b6f6e AZ |
586 | s->text_x[0] = 0; |
587 | s->text_y[0] = 0; | |
588 | s->text_x[1] = s->width - 1; | |
589 | s->text_y[1] = s->height - 1; | |
590 | s->cursor_invalidate = 1; | |
4d3b6f6e | 591 | } |
e7f0ad58 | 592 | |
a93a4a22 | 593 | if (s->ds->have_gfx) { |
1562e531 | 594 | vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface), |
cf6f0548 | 595 | color_table_rgb[0][COLOR_BLACK]); |
a93a4a22 GH |
596 | y1 = s->y_displayed; |
597 | for (y = 0; y < s->height; y++) { | |
598 | c = s->cells + y1 * s->width; | |
599 | for (x = 0; x < s->width; x++) { | |
1562e531 | 600 | vga_putcharxy(s, x, y, c->ch, |
a93a4a22 GH |
601 | &(c->t_attrib)); |
602 | c++; | |
603 | } | |
604 | if (++y1 == s->total_height) { | |
605 | y1 = 0; | |
606 | } | |
e7f0ad58 | 607 | } |
a93a4a22 | 608 | console_show_cursor(s, 1); |
1562e531 GH |
609 | dpy_gfx_update(s, 0, 0, |
610 | surface_width(surface), surface_height(surface)); | |
e7f0ad58 | 611 | } |
e7f0ad58 FB |
612 | } |
613 | ||
614 | static void console_scroll(int ydelta) | |
615 | { | |
76ffb0b4 | 616 | QemuConsole *s; |
e7f0ad58 | 617 | int i, y1; |
3b46e624 | 618 | |
e7f0ad58 | 619 | s = active_console; |
af3a9031 | 620 | if (!s || (s->console_type == GRAPHIC_CONSOLE)) |
e7f0ad58 FB |
621 | return; |
622 | ||
623 | if (ydelta > 0) { | |
624 | for(i = 0; i < ydelta; i++) { | |
625 | if (s->y_displayed == s->y_base) | |
626 | break; | |
627 | if (++s->y_displayed == s->total_height) | |
628 | s->y_displayed = 0; | |
629 | } | |
630 | } else { | |
631 | ydelta = -ydelta; | |
632 | i = s->backscroll_height; | |
633 | if (i > s->total_height - s->height) | |
634 | i = s->total_height - s->height; | |
635 | y1 = s->y_base - i; | |
636 | if (y1 < 0) | |
637 | y1 += s->total_height; | |
638 | for(i = 0; i < ydelta; i++) { | |
639 | if (s->y_displayed == y1) | |
640 | break; | |
641 | if (--s->y_displayed < 0) | |
642 | s->y_displayed = s->total_height - 1; | |
643 | } | |
644 | } | |
645 | console_refresh(s); | |
646 | } | |
647 | ||
76ffb0b4 | 648 | static void console_put_lf(QemuConsole *s) |
e7f0ad58 FB |
649 | { |
650 | TextCell *c; | |
651 | int x, y1; | |
652 | ||
e7f0ad58 FB |
653 | s->y++; |
654 | if (s->y >= s->height) { | |
655 | s->y = s->height - 1; | |
6d6f7c28 | 656 | |
e7f0ad58 FB |
657 | if (s->y_displayed == s->y_base) { |
658 | if (++s->y_displayed == s->total_height) | |
659 | s->y_displayed = 0; | |
660 | } | |
661 | if (++s->y_base == s->total_height) | |
662 | s->y_base = 0; | |
663 | if (s->backscroll_height < s->total_height) | |
664 | s->backscroll_height++; | |
665 | y1 = (s->y_base + s->height - 1) % s->total_height; | |
666 | c = &s->cells[y1 * s->width]; | |
667 | for(x = 0; x < s->width; x++) { | |
668 | c->ch = ' '; | |
6d6f7c28 | 669 | c->t_attrib = s->t_attrib_default; |
e7f0ad58 FB |
670 | c++; |
671 | } | |
672 | if (s == active_console && s->y_displayed == s->y_base) { | |
1562e531 | 673 | if (s->ds->have_text) { |
4d3b6f6e AZ |
674 | s->text_x[0] = 0; |
675 | s->text_y[0] = 0; | |
676 | s->text_x[1] = s->width - 1; | |
677 | s->text_y[1] = s->height - 1; | |
4d3b6f6e AZ |
678 | } |
679 | ||
1562e531 GH |
680 | if (s->ds->have_gfx) { |
681 | vga_bitblt(s, 0, FONT_HEIGHT, 0, 0, | |
682 | s->width * FONT_WIDTH, | |
683 | (s->height - 1) * FONT_HEIGHT); | |
684 | vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT, | |
685 | s->width * FONT_WIDTH, FONT_HEIGHT, | |
686 | color_table_rgb[0][s->t_attrib_default.bgcol]); | |
687 | s->update_x0 = 0; | |
688 | s->update_y0 = 0; | |
689 | s->update_x1 = s->width * FONT_WIDTH; | |
690 | s->update_y1 = s->height * FONT_HEIGHT; | |
691 | } | |
e7f0ad58 FB |
692 | } |
693 | } | |
694 | } | |
695 | ||
6d6f7c28 PB |
696 | /* Set console attributes depending on the current escape codes. |
697 | * NOTE: I know this code is not very efficient (checking every color for it | |
698 | * self) but it is more readable and better maintainable. | |
699 | */ | |
76ffb0b4 | 700 | static void console_handle_escape(QemuConsole *s) |
6d6f7c28 PB |
701 | { |
702 | int i; | |
703 | ||
6d6f7c28 PB |
704 | for (i=0; i<s->nb_esc_params; i++) { |
705 | switch (s->esc_params[i]) { | |
706 | case 0: /* reset all console attributes to default */ | |
707 | s->t_attrib = s->t_attrib_default; | |
708 | break; | |
709 | case 1: | |
710 | s->t_attrib.bold = 1; | |
711 | break; | |
712 | case 4: | |
713 | s->t_attrib.uline = 1; | |
714 | break; | |
715 | case 5: | |
716 | s->t_attrib.blink = 1; | |
717 | break; | |
718 | case 7: | |
719 | s->t_attrib.invers = 1; | |
720 | break; | |
721 | case 8: | |
722 | s->t_attrib.unvisible = 1; | |
723 | break; | |
724 | case 22: | |
725 | s->t_attrib.bold = 0; | |
726 | break; | |
727 | case 24: | |
728 | s->t_attrib.uline = 0; | |
729 | break; | |
730 | case 25: | |
731 | s->t_attrib.blink = 0; | |
732 | break; | |
733 | case 27: | |
734 | s->t_attrib.invers = 0; | |
735 | break; | |
736 | case 28: | |
737 | s->t_attrib.unvisible = 0; | |
738 | break; | |
739 | /* set foreground color */ | |
740 | case 30: | |
741 | s->t_attrib.fgcol=COLOR_BLACK; | |
742 | break; | |
743 | case 31: | |
744 | s->t_attrib.fgcol=COLOR_RED; | |
745 | break; | |
746 | case 32: | |
747 | s->t_attrib.fgcol=COLOR_GREEN; | |
748 | break; | |
749 | case 33: | |
750 | s->t_attrib.fgcol=COLOR_YELLOW; | |
751 | break; | |
752 | case 34: | |
753 | s->t_attrib.fgcol=COLOR_BLUE; | |
754 | break; | |
755 | case 35: | |
756 | s->t_attrib.fgcol=COLOR_MAGENTA; | |
757 | break; | |
758 | case 36: | |
759 | s->t_attrib.fgcol=COLOR_CYAN; | |
760 | break; | |
761 | case 37: | |
762 | s->t_attrib.fgcol=COLOR_WHITE; | |
763 | break; | |
764 | /* set background color */ | |
765 | case 40: | |
766 | s->t_attrib.bgcol=COLOR_BLACK; | |
767 | break; | |
768 | case 41: | |
769 | s->t_attrib.bgcol=COLOR_RED; | |
770 | break; | |
771 | case 42: | |
772 | s->t_attrib.bgcol=COLOR_GREEN; | |
773 | break; | |
774 | case 43: | |
775 | s->t_attrib.bgcol=COLOR_YELLOW; | |
776 | break; | |
777 | case 44: | |
778 | s->t_attrib.bgcol=COLOR_BLUE; | |
779 | break; | |
780 | case 45: | |
781 | s->t_attrib.bgcol=COLOR_MAGENTA; | |
782 | break; | |
783 | case 46: | |
784 | s->t_attrib.bgcol=COLOR_CYAN; | |
785 | break; | |
786 | case 47: | |
787 | s->t_attrib.bgcol=COLOR_WHITE; | |
788 | break; | |
789 | } | |
790 | } | |
791 | } | |
792 | ||
76ffb0b4 | 793 | static void console_clear_xy(QemuConsole *s, int x, int y) |
adb47967 TS |
794 | { |
795 | int y1 = (s->y_base + y) % s->total_height; | |
796 | TextCell *c = &s->cells[y1 * s->width + x]; | |
797 | c->ch = ' '; | |
798 | c->t_attrib = s->t_attrib_default; | |
adb47967 TS |
799 | update_xy(s, x, y); |
800 | } | |
801 | ||
3eea5498 | 802 | /* set cursor, checking bounds */ |
76ffb0b4 | 803 | static void set_cursor(QemuConsole *s, int x, int y) |
3eea5498 IC |
804 | { |
805 | if (x < 0) { | |
806 | x = 0; | |
807 | } | |
808 | if (y < 0) { | |
809 | y = 0; | |
810 | } | |
811 | if (y >= s->height) { | |
812 | y = s->height - 1; | |
813 | } | |
814 | if (x >= s->width) { | |
815 | x = s->width - 1; | |
816 | } | |
817 | ||
818 | s->x = x; | |
819 | s->y = y; | |
820 | } | |
821 | ||
76ffb0b4 | 822 | static void console_putchar(QemuConsole *s, int ch) |
e7f0ad58 FB |
823 | { |
824 | TextCell *c; | |
adb47967 TS |
825 | int y1, i; |
826 | int x, y; | |
e7f0ad58 FB |
827 | |
828 | switch(s->state) { | |
829 | case TTY_STATE_NORM: | |
830 | switch(ch) { | |
6d6f7c28 | 831 | case '\r': /* carriage return */ |
e7f0ad58 FB |
832 | s->x = 0; |
833 | break; | |
6d6f7c28 | 834 | case '\n': /* newline */ |
e7f0ad58 FB |
835 | console_put_lf(s); |
836 | break; | |
6d6f7c28 | 837 | case '\b': /* backspace */ |
5fafdf24 | 838 | if (s->x > 0) |
e15d7371 | 839 | s->x--; |
6d6f7c28 PB |
840 | break; |
841 | case '\t': /* tabspace */ | |
842 | if (s->x + (8 - (s->x % 8)) > s->width) { | |
bd468840 | 843 | s->x = 0; |
6d6f7c28 PB |
844 | console_put_lf(s); |
845 | } else { | |
846 | s->x = s->x + (8 - (s->x % 8)); | |
847 | } | |
848 | break; | |
849 | case '\a': /* alert aka. bell */ | |
850 | /* TODO: has to be implemented */ | |
851 | break; | |
adb47967 TS |
852 | case 14: |
853 | /* SI (shift in), character set 0 (ignored) */ | |
854 | break; | |
855 | case 15: | |
856 | /* SO (shift out), character set 1 (ignored) */ | |
857 | break; | |
6d6f7c28 | 858 | case 27: /* esc (introducing an escape sequence) */ |
e7f0ad58 FB |
859 | s->state = TTY_STATE_ESC; |
860 | break; | |
861 | default: | |
ed8276ac TS |
862 | if (s->x >= s->width) { |
863 | /* line wrap */ | |
864 | s->x = 0; | |
865 | console_put_lf(s); | |
adb47967 | 866 | } |
e7f0ad58 FB |
867 | y1 = (s->y_base + s->y) % s->total_height; |
868 | c = &s->cells[y1 * s->width + s->x]; | |
869 | c->ch = ch; | |
6d6f7c28 | 870 | c->t_attrib = s->t_attrib; |
e7f0ad58 FB |
871 | update_xy(s, s->x, s->y); |
872 | s->x++; | |
e7f0ad58 FB |
873 | break; |
874 | } | |
875 | break; | |
6d6f7c28 | 876 | case TTY_STATE_ESC: /* check if it is a terminal escape sequence */ |
e7f0ad58 FB |
877 | if (ch == '[') { |
878 | for(i=0;i<MAX_ESC_PARAMS;i++) | |
879 | s->esc_params[i] = 0; | |
880 | s->nb_esc_params = 0; | |
881 | s->state = TTY_STATE_CSI; | |
882 | } else { | |
883 | s->state = TTY_STATE_NORM; | |
884 | } | |
885 | break; | |
6d6f7c28 | 886 | case TTY_STATE_CSI: /* handle escape sequence parameters */ |
e7f0ad58 FB |
887 | if (ch >= '0' && ch <= '9') { |
888 | if (s->nb_esc_params < MAX_ESC_PARAMS) { | |
c10600af LE |
889 | int *param = &s->esc_params[s->nb_esc_params]; |
890 | int digit = (ch - '0'); | |
891 | ||
892 | *param = (*param <= (INT_MAX - digit) / 10) ? | |
893 | *param * 10 + digit : INT_MAX; | |
e7f0ad58 FB |
894 | } |
895 | } else { | |
3eea5498 IC |
896 | if (s->nb_esc_params < MAX_ESC_PARAMS) |
897 | s->nb_esc_params++; | |
e7f0ad58 FB |
898 | if (ch == ';') |
899 | break; | |
adb47967 TS |
900 | #ifdef DEBUG_CONSOLE |
901 | fprintf(stderr, "escape sequence CSI%d;%d%c, %d parameters\n", | |
902 | s->esc_params[0], s->esc_params[1], ch, s->nb_esc_params); | |
903 | #endif | |
e7f0ad58 FB |
904 | s->state = TTY_STATE_NORM; |
905 | switch(ch) { | |
adb47967 TS |
906 | case 'A': |
907 | /* move cursor up */ | |
908 | if (s->esc_params[0] == 0) { | |
909 | s->esc_params[0] = 1; | |
910 | } | |
3eea5498 | 911 | set_cursor(s, s->x, s->y - s->esc_params[0]); |
adb47967 TS |
912 | break; |
913 | case 'B': | |
914 | /* move cursor down */ | |
915 | if (s->esc_params[0] == 0) { | |
916 | s->esc_params[0] = 1; | |
917 | } | |
3eea5498 | 918 | set_cursor(s, s->x, s->y + s->esc_params[0]); |
e7f0ad58 FB |
919 | break; |
920 | case 'C': | |
adb47967 TS |
921 | /* move cursor right */ |
922 | if (s->esc_params[0] == 0) { | |
923 | s->esc_params[0] = 1; | |
924 | } | |
3eea5498 | 925 | set_cursor(s, s->x + s->esc_params[0], s->y); |
e7f0ad58 | 926 | break; |
adb47967 TS |
927 | case 'D': |
928 | /* move cursor left */ | |
929 | if (s->esc_params[0] == 0) { | |
930 | s->esc_params[0] = 1; | |
931 | } | |
3eea5498 | 932 | set_cursor(s, s->x - s->esc_params[0], s->y); |
adb47967 TS |
933 | break; |
934 | case 'G': | |
935 | /* move cursor to column */ | |
3eea5498 | 936 | set_cursor(s, s->esc_params[0] - 1, s->y); |
adb47967 TS |
937 | break; |
938 | case 'f': | |
939 | case 'H': | |
940 | /* move cursor to row, column */ | |
3eea5498 | 941 | set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1); |
adb47967 TS |
942 | break; |
943 | case 'J': | |
944 | switch (s->esc_params[0]) { | |
945 | case 0: | |
946 | /* clear to end of screen */ | |
947 | for (y = s->y; y < s->height; y++) { | |
948 | for (x = 0; x < s->width; x++) { | |
949 | if (y == s->y && x < s->x) { | |
950 | continue; | |
951 | } | |
952 | console_clear_xy(s, x, y); | |
953 | } | |
954 | } | |
955 | break; | |
956 | case 1: | |
957 | /* clear from beginning of screen */ | |
958 | for (y = 0; y <= s->y; y++) { | |
959 | for (x = 0; x < s->width; x++) { | |
960 | if (y == s->y && x > s->x) { | |
961 | break; | |
962 | } | |
963 | console_clear_xy(s, x, y); | |
964 | } | |
965 | } | |
966 | break; | |
967 | case 2: | |
968 | /* clear entire screen */ | |
969 | for (y = 0; y <= s->height; y++) { | |
970 | for (x = 0; x < s->width; x++) { | |
971 | console_clear_xy(s, x, y); | |
972 | } | |
973 | } | |
f94a950f | 974 | break; |
adb47967 | 975 | } |
95d8f9f4 | 976 | break; |
e7f0ad58 | 977 | case 'K': |
adb47967 TS |
978 | switch (s->esc_params[0]) { |
979 | case 0: | |
f94a950f MA |
980 | /* clear to eol */ |
981 | for(x = s->x; x < s->width; x++) { | |
adb47967 | 982 | console_clear_xy(s, x, s->y); |
f94a950f MA |
983 | } |
984 | break; | |
adb47967 TS |
985 | case 1: |
986 | /* clear from beginning of line */ | |
987 | for (x = 0; x <= s->x; x++) { | |
988 | console_clear_xy(s, x, s->y); | |
989 | } | |
990 | break; | |
991 | case 2: | |
992 | /* clear entire line */ | |
993 | for(x = 0; x < s->width; x++) { | |
994 | console_clear_xy(s, x, s->y); | |
995 | } | |
f94a950f MA |
996 | break; |
997 | } | |
adb47967 TS |
998 | break; |
999 | case 'm': | |
f94a950f MA |
1000 | console_handle_escape(s); |
1001 | break; | |
adb47967 TS |
1002 | case 'n': |
1003 | /* report cursor position */ | |
1004 | /* TODO: send ESC[row;colR */ | |
1005 | break; | |
1006 | case 's': | |
1007 | /* save cursor position */ | |
1008 | s->x_saved = s->x; | |
1009 | s->y_saved = s->y; | |
1010 | break; | |
1011 | case 'u': | |
1012 | /* restore cursor position */ | |
1013 | s->x = s->x_saved; | |
1014 | s->y = s->y_saved; | |
1015 | break; | |
1016 | default: | |
1017 | #ifdef DEBUG_CONSOLE | |
1018 | fprintf(stderr, "unhandled escape character '%c'\n", ch); | |
1019 | #endif | |
1020 | break; | |
1021 | } | |
1022 | break; | |
e7f0ad58 FB |
1023 | } |
1024 | } | |
1025 | } | |
1026 | ||
1027 | void console_select(unsigned int index) | |
1028 | { | |
76ffb0b4 | 1029 | QemuConsole *s; |
6d6f7c28 | 1030 | |
e7f0ad58 FB |
1031 | if (index >= MAX_CONSOLES) |
1032 | return; | |
437fe106 GH |
1033 | |
1034 | trace_console_select(index); | |
e7f0ad58 FB |
1035 | s = consoles[index]; |
1036 | if (s) { | |
7d957bd8 | 1037 | DisplayState *ds = s->ds; |
bf1bed81 | 1038 | |
8bd6b06d | 1039 | if (active_console && active_console->cursor_timer) { |
bf1bed81 JK |
1040 | qemu_del_timer(active_console->cursor_timer); |
1041 | } | |
e7f0ad58 | 1042 | active_console = s; |
a93a4a22 | 1043 | if (ds->have_gfx) { |
321f048d GH |
1044 | dpy_gfx_switch_surface(ds, s->surface); |
1045 | dpy_gfx_update(s, 0, 0, surface_width(s->surface), | |
1046 | surface_height(s->surface)); | |
a93a4a22 GH |
1047 | } |
1048 | if (ds->have_text) { | |
c78f7137 | 1049 | dpy_text_resize(s, s->width, s->height); |
68f00996 | 1050 | } |
bf1bed81 JK |
1051 | if (s->cursor_timer) { |
1052 | qemu_mod_timer(s->cursor_timer, | |
1053 | qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2); | |
1054 | } | |
e7f0ad58 FB |
1055 | } |
1056 | } | |
1057 | ||
1058 | static int console_puts(CharDriverState *chr, const uint8_t *buf, int len) | |
1059 | { | |
76ffb0b4 | 1060 | QemuConsole *s = chr->opaque; |
e7f0ad58 FB |
1061 | int i; |
1062 | ||
14778c20 PB |
1063 | s->update_x0 = s->width * FONT_WIDTH; |
1064 | s->update_y0 = s->height * FONT_HEIGHT; | |
1065 | s->update_x1 = 0; | |
1066 | s->update_y1 = 0; | |
e7f0ad58 FB |
1067 | console_show_cursor(s, 0); |
1068 | for(i = 0; i < len; i++) { | |
1069 | console_putchar(s, buf[i]); | |
1070 | } | |
1071 | console_show_cursor(s, 1); | |
a93a4a22 | 1072 | if (s->ds->have_gfx && s->update_x0 < s->update_x1) { |
c78f7137 | 1073 | dpy_gfx_update(s, s->update_x0, s->update_y0, |
a93a4a22 GH |
1074 | s->update_x1 - s->update_x0, |
1075 | s->update_y1 - s->update_y0); | |
14778c20 | 1076 | } |
e7f0ad58 FB |
1077 | return len; |
1078 | } | |
1079 | ||
e15d7371 FB |
1080 | static void kbd_send_chars(void *opaque) |
1081 | { | |
76ffb0b4 | 1082 | QemuConsole *s = opaque; |
e15d7371 FB |
1083 | int len; |
1084 | uint8_t buf[16]; | |
3b46e624 | 1085 | |
909cda12 | 1086 | len = qemu_chr_be_can_write(s->chr); |
e15d7371 FB |
1087 | if (len > s->out_fifo.count) |
1088 | len = s->out_fifo.count; | |
1089 | if (len > 0) { | |
1090 | if (len > sizeof(buf)) | |
1091 | len = sizeof(buf); | |
1092 | qemu_fifo_read(&s->out_fifo, buf, len); | |
fa5efccb | 1093 | qemu_chr_be_write(s->chr, buf, len); |
e15d7371 FB |
1094 | } |
1095 | /* characters are pending: we send them a bit later (XXX: | |
1096 | horrible, should change char device API) */ | |
1097 | if (s->out_fifo.count > 0) { | |
7bd427d8 | 1098 | qemu_mod_timer(s->kbd_timer, qemu_get_clock_ms(rt_clock) + 1); |
e15d7371 FB |
1099 | } |
1100 | } | |
1101 | ||
e7f0ad58 FB |
1102 | /* called when an ascii key is pressed */ |
1103 | void kbd_put_keysym(int keysym) | |
1104 | { | |
76ffb0b4 | 1105 | QemuConsole *s; |
e7f0ad58 FB |
1106 | uint8_t buf[16], *q; |
1107 | int c; | |
1108 | ||
1109 | s = active_console; | |
af3a9031 | 1110 | if (!s || (s->console_type == GRAPHIC_CONSOLE)) |
e7f0ad58 FB |
1111 | return; |
1112 | ||
1113 | switch(keysym) { | |
1114 | case QEMU_KEY_CTRL_UP: | |
1115 | console_scroll(-1); | |
1116 | break; | |
1117 | case QEMU_KEY_CTRL_DOWN: | |
1118 | console_scroll(1); | |
1119 | break; | |
1120 | case QEMU_KEY_CTRL_PAGEUP: | |
1121 | console_scroll(-10); | |
1122 | break; | |
1123 | case QEMU_KEY_CTRL_PAGEDOWN: | |
1124 | console_scroll(10); | |
1125 | break; | |
1126 | default: | |
e15d7371 FB |
1127 | /* convert the QEMU keysym to VT100 key string */ |
1128 | q = buf; | |
1129 | if (keysym >= 0xe100 && keysym <= 0xe11f) { | |
1130 | *q++ = '\033'; | |
1131 | *q++ = '['; | |
1132 | c = keysym - 0xe100; | |
1133 | if (c >= 10) | |
1134 | *q++ = '0' + (c / 10); | |
1135 | *q++ = '0' + (c % 10); | |
1136 | *q++ = '~'; | |
1137 | } else if (keysym >= 0xe120 && keysym <= 0xe17f) { | |
1138 | *q++ = '\033'; | |
1139 | *q++ = '['; | |
1140 | *q++ = keysym & 0xff; | |
4104833f PB |
1141 | } else if (s->echo && (keysym == '\r' || keysym == '\n')) { |
1142 | console_puts(s->chr, (const uint8_t *) "\r", 1); | |
1143 | *q++ = '\n'; | |
e15d7371 | 1144 | } else { |
4104833f PB |
1145 | *q++ = keysym; |
1146 | } | |
1147 | if (s->echo) { | |
1148 | console_puts(s->chr, buf, q - buf); | |
e15d7371 | 1149 | } |
e5b0bc44 | 1150 | if (s->chr->chr_read) { |
e15d7371 FB |
1151 | qemu_fifo_write(&s->out_fifo, buf, q - buf); |
1152 | kbd_send_chars(s); | |
e7f0ad58 FB |
1153 | } |
1154 | break; | |
1155 | } | |
1156 | } | |
1157 | ||
4d3b6f6e AZ |
1158 | static void text_console_invalidate(void *opaque) |
1159 | { | |
76ffb0b4 | 1160 | QemuConsole *s = (QemuConsole *) opaque; |
1562e531 GH |
1161 | |
1162 | if (s->ds->have_text && s->console_type == TEXT_CONSOLE) { | |
68f00996 AL |
1163 | text_console_resize(s); |
1164 | } | |
4d3b6f6e AZ |
1165 | console_refresh(s); |
1166 | } | |
1167 | ||
c227f099 | 1168 | static void text_console_update(void *opaque, console_ch_t *chardata) |
4d3b6f6e | 1169 | { |
76ffb0b4 | 1170 | QemuConsole *s = (QemuConsole *) opaque; |
4d3b6f6e AZ |
1171 | int i, j, src; |
1172 | ||
1173 | if (s->text_x[0] <= s->text_x[1]) { | |
1174 | src = (s->y_base + s->text_y[0]) * s->width; | |
1175 | chardata += s->text_y[0] * s->width; | |
1176 | for (i = s->text_y[0]; i <= s->text_y[1]; i ++) | |
1177 | for (j = 0; j < s->width; j ++, src ++) | |
1178 | console_write_ch(chardata ++, s->cells[src].ch | | |
1179 | (s->cells[src].t_attrib.fgcol << 12) | | |
1180 | (s->cells[src].t_attrib.bgcol << 8) | | |
1181 | (s->cells[src].t_attrib.bold << 21)); | |
c78f7137 | 1182 | dpy_text_update(s, s->text_x[0], s->text_y[0], |
a93a4a22 | 1183 | s->text_x[1] - s->text_x[0], i - s->text_y[0]); |
4d3b6f6e AZ |
1184 | s->text_x[0] = s->width; |
1185 | s->text_y[0] = s->height; | |
1186 | s->text_x[1] = 0; | |
1187 | s->text_y[1] = 0; | |
1188 | } | |
1189 | if (s->cursor_invalidate) { | |
c78f7137 | 1190 | dpy_text_cursor(s, s->x, s->y); |
4d3b6f6e AZ |
1191 | s->cursor_invalidate = 0; |
1192 | } | |
1193 | } | |
1194 | ||
76ffb0b4 | 1195 | static QemuConsole *new_console(DisplayState *ds, console_type_t console_type) |
e7f0ad58 | 1196 | { |
76ffb0b4 | 1197 | QemuConsole *s; |
95219897 | 1198 | int i; |
e7f0ad58 FB |
1199 | |
1200 | if (nb_consoles >= MAX_CONSOLES) | |
1201 | return NULL; | |
76ffb0b4 | 1202 | s = g_malloc0(sizeof(QemuConsole)); |
af3a9031 TS |
1203 | if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) && |
1204 | (console_type == GRAPHIC_CONSOLE))) { | |
e7f0ad58 | 1205 | active_console = s; |
af3a9031 | 1206 | } |
e7f0ad58 | 1207 | s->ds = ds; |
af3a9031 TS |
1208 | s->console_type = console_type; |
1209 | if (console_type != GRAPHIC_CONSOLE) { | |
f81bdefb | 1210 | s->index = nb_consoles; |
95219897 PB |
1211 | consoles[nb_consoles++] = s; |
1212 | } else { | |
1213 | /* HACK: Put graphical consoles before text consoles. */ | |
1214 | for (i = nb_consoles; i > 0; i--) { | |
af3a9031 | 1215 | if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE) |
95219897 PB |
1216 | break; |
1217 | consoles[i] = consoles[i - 1]; | |
f81bdefb | 1218 | consoles[i]->index = i; |
95219897 | 1219 | } |
f81bdefb | 1220 | s->index = i; |
95219897 | 1221 | consoles[i] = s; |
3023f332 | 1222 | nb_consoles++; |
95219897 PB |
1223 | } |
1224 | return s; | |
1225 | } | |
1226 | ||
537a4391 GH |
1227 | static void qemu_alloc_display(DisplaySurface *surface, int width, int height, |
1228 | int linesize, PixelFormat pf, int newflags) | |
ffe8b821 | 1229 | { |
ffe8b821 | 1230 | surface->pf = pf; |
69c77777 GH |
1231 | |
1232 | qemu_pixman_image_unref(surface->image); | |
1233 | surface->image = NULL; | |
69c77777 GH |
1234 | |
1235 | surface->format = qemu_pixman_get_format(&pf); | |
1236 | assert(surface->format != 0); | |
1237 | surface->image = pixman_image_create_bits(surface->format, | |
1238 | width, height, | |
1239 | NULL, linesize); | |
1240 | assert(surface->image != NULL); | |
1241 | ||
ffe8b821 | 1242 | surface->flags = newflags | QEMU_ALLOCATED_FLAG; |
98b50080 | 1243 | #ifdef HOST_WORDS_BIGENDIAN |
ffe8b821 | 1244 | surface->flags |= QEMU_BIG_ENDIAN_FLAG; |
98b50080 | 1245 | #endif |
98b50080 PB |
1246 | } |
1247 | ||
da229ef3 | 1248 | DisplaySurface *qemu_create_displaysurface(int width, int height) |
537a4391 GH |
1249 | { |
1250 | DisplaySurface *surface = g_new0(DisplaySurface, 1); | |
537a4391 | 1251 | int linesize = width * 4; |
da229ef3 GH |
1252 | |
1253 | trace_displaysurface_create(surface, width, height); | |
537a4391 GH |
1254 | qemu_alloc_display(surface, width, height, linesize, |
1255 | qemu_default_pixelformat(32), 0); | |
1256 | return surface; | |
1257 | } | |
1258 | ||
187cd1d9 | 1259 | DisplaySurface *qemu_create_displaysurface_from(int width, int height, int bpp, |
b1424e03 GH |
1260 | int linesize, uint8_t *data, |
1261 | bool byteswap) | |
98b50080 | 1262 | { |
69c77777 | 1263 | DisplaySurface *surface = g_new0(DisplaySurface, 1); |
98b50080 | 1264 | |
da229ef3 | 1265 | trace_displaysurface_create_from(surface, width, height, bpp, byteswap); |
b1424e03 GH |
1266 | if (byteswap) { |
1267 | surface->pf = qemu_different_endianness_pixelformat(bpp); | |
1268 | } else { | |
1269 | surface->pf = qemu_default_pixelformat(bpp); | |
1270 | } | |
69c77777 GH |
1271 | |
1272 | surface->format = qemu_pixman_get_format(&surface->pf); | |
1273 | assert(surface->format != 0); | |
1274 | surface->image = pixman_image_create_bits(surface->format, | |
1275 | width, height, | |
1276 | (void *)data, linesize); | |
1277 | assert(surface->image != NULL); | |
1278 | ||
98b50080 PB |
1279 | #ifdef HOST_WORDS_BIGENDIAN |
1280 | surface->flags = QEMU_BIG_ENDIAN_FLAG; | |
1281 | #endif | |
98b50080 PB |
1282 | |
1283 | return surface; | |
1284 | } | |
1285 | ||
da229ef3 | 1286 | void qemu_free_displaysurface(DisplaySurface *surface) |
98b50080 | 1287 | { |
da229ef3 | 1288 | if (surface == NULL) { |
98b50080 | 1289 | return; |
187cd1d9 | 1290 | } |
da229ef3 GH |
1291 | trace_displaysurface_free(surface); |
1292 | qemu_pixman_image_unref(surface->image); | |
1293 | g_free(surface); | |
98b50080 PB |
1294 | } |
1295 | ||
7c20b4a3 GH |
1296 | void register_displaychangelistener(DisplayState *ds, |
1297 | DisplayChangeListener *dcl) | |
1298 | { | |
1299 | trace_displaychangelistener_register(dcl, dcl->ops->dpy_name); | |
1300 | dcl->ds = ds; | |
1301 | QLIST_INSERT_HEAD(&ds->listeners, dcl, next); | |
1302 | gui_setup_refresh(ds); | |
321f048d GH |
1303 | if (dcl->ops->dpy_gfx_switch && active_console) { |
1304 | dcl->ops->dpy_gfx_switch(dcl, active_console->surface); | |
7c20b4a3 GH |
1305 | } |
1306 | } | |
1307 | ||
0f7b2864 GH |
1308 | void update_displaychangelistener(DisplayChangeListener *dcl, |
1309 | uint64_t interval) | |
1310 | { | |
1311 | DisplayState *ds = dcl->ds; | |
1312 | ||
1313 | dcl->update_interval = interval; | |
1314 | if (!ds->refreshing && ds->update_interval > interval) { | |
1315 | qemu_mod_timer(ds->gui_timer, ds->last_update + interval); | |
1316 | } | |
1317 | } | |
1318 | ||
7c20b4a3 GH |
1319 | void unregister_displaychangelistener(DisplayChangeListener *dcl) |
1320 | { | |
1321 | DisplayState *ds = dcl->ds; | |
1322 | trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name); | |
1323 | QLIST_REMOVE(dcl, next); | |
1324 | gui_setup_refresh(ds); | |
1325 | } | |
1326 | ||
c78f7137 | 1327 | void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h) |
7c20b4a3 | 1328 | { |
c78f7137 | 1329 | DisplayState *s = con->ds; |
7c20b4a3 | 1330 | struct DisplayChangeListener *dcl; |
321f048d GH |
1331 | int width = surface_width(con->surface); |
1332 | int height = surface_height(con->surface); | |
7c20b4a3 GH |
1333 | |
1334 | x = MAX(x, 0); | |
1335 | y = MAX(y, 0); | |
1336 | x = MIN(x, width); | |
1337 | y = MIN(y, height); | |
1338 | w = MIN(w, width - x); | |
1339 | h = MIN(h, height - y); | |
1340 | ||
321f048d GH |
1341 | if (con != active_console) { |
1342 | return; | |
1343 | } | |
7c20b4a3 GH |
1344 | QLIST_FOREACH(dcl, &s->listeners, next) { |
1345 | if (dcl->ops->dpy_gfx_update) { | |
bc2ed970 | 1346 | dcl->ops->dpy_gfx_update(dcl, x, y, w, h); |
7c20b4a3 GH |
1347 | } |
1348 | } | |
1349 | } | |
1350 | ||
321f048d GH |
1351 | static void dpy_gfx_switch_surface(DisplayState *ds, |
1352 | DisplaySurface *surface) | |
7c20b4a3 GH |
1353 | { |
1354 | struct DisplayChangeListener *dcl; | |
da229ef3 | 1355 | |
321f048d | 1356 | QLIST_FOREACH(dcl, &ds->listeners, next) { |
c12aeb86 | 1357 | if (dcl->ops->dpy_gfx_switch) { |
bc2ed970 | 1358 | dcl->ops->dpy_gfx_switch(dcl, surface); |
7c20b4a3 GH |
1359 | } |
1360 | } | |
321f048d GH |
1361 | } |
1362 | ||
1363 | void dpy_gfx_replace_surface(QemuConsole *con, | |
1364 | DisplaySurface *surface) | |
1365 | { | |
1366 | DisplayState *s = con->ds; | |
1367 | DisplaySurface *old_surface = con->surface; | |
1368 | ||
1369 | con->surface = surface; | |
1370 | if (con == active_console) { | |
1371 | dpy_gfx_switch_surface(s, surface); | |
1372 | } | |
da229ef3 | 1373 | qemu_free_displaysurface(old_surface); |
7c20b4a3 GH |
1374 | } |
1375 | ||
1376 | void dpy_refresh(DisplayState *s) | |
1377 | { | |
1378 | struct DisplayChangeListener *dcl; | |
1379 | QLIST_FOREACH(dcl, &s->listeners, next) { | |
1380 | if (dcl->ops->dpy_refresh) { | |
bc2ed970 | 1381 | dcl->ops->dpy_refresh(dcl); |
7c20b4a3 GH |
1382 | } |
1383 | } | |
1384 | } | |
1385 | ||
c78f7137 GH |
1386 | void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y, |
1387 | int dst_x, int dst_y, int w, int h) | |
7c20b4a3 | 1388 | { |
c78f7137 | 1389 | DisplayState *s = con->ds; |
7c20b4a3 | 1390 | struct DisplayChangeListener *dcl; |
321f048d GH |
1391 | |
1392 | if (con != active_console) { | |
1393 | return; | |
1394 | } | |
7c20b4a3 GH |
1395 | QLIST_FOREACH(dcl, &s->listeners, next) { |
1396 | if (dcl->ops->dpy_gfx_copy) { | |
bc2ed970 | 1397 | dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h); |
7c20b4a3 | 1398 | } else { /* TODO */ |
bc2ed970 | 1399 | dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h); |
7c20b4a3 GH |
1400 | } |
1401 | } | |
1402 | } | |
1403 | ||
c78f7137 | 1404 | void dpy_text_cursor(QemuConsole *con, int x, int y) |
7c20b4a3 | 1405 | { |
c78f7137 | 1406 | DisplayState *s = con->ds; |
7c20b4a3 | 1407 | struct DisplayChangeListener *dcl; |
321f048d GH |
1408 | |
1409 | if (con != active_console) { | |
1410 | return; | |
1411 | } | |
7c20b4a3 GH |
1412 | QLIST_FOREACH(dcl, &s->listeners, next) { |
1413 | if (dcl->ops->dpy_text_cursor) { | |
bc2ed970 | 1414 | dcl->ops->dpy_text_cursor(dcl, x, y); |
7c20b4a3 GH |
1415 | } |
1416 | } | |
1417 | } | |
1418 | ||
c78f7137 | 1419 | void dpy_text_update(QemuConsole *con, int x, int y, int w, int h) |
7c20b4a3 | 1420 | { |
c78f7137 | 1421 | DisplayState *s = con->ds; |
7c20b4a3 | 1422 | struct DisplayChangeListener *dcl; |
321f048d GH |
1423 | |
1424 | if (con != active_console) { | |
1425 | return; | |
1426 | } | |
7c20b4a3 GH |
1427 | QLIST_FOREACH(dcl, &s->listeners, next) { |
1428 | if (dcl->ops->dpy_text_update) { | |
bc2ed970 | 1429 | dcl->ops->dpy_text_update(dcl, x, y, w, h); |
7c20b4a3 GH |
1430 | } |
1431 | } | |
1432 | } | |
1433 | ||
c78f7137 | 1434 | void dpy_text_resize(QemuConsole *con, int w, int h) |
7c20b4a3 | 1435 | { |
c78f7137 | 1436 | DisplayState *s = con->ds; |
7c20b4a3 | 1437 | struct DisplayChangeListener *dcl; |
321f048d GH |
1438 | |
1439 | if (con != active_console) { | |
1440 | return; | |
1441 | } | |
7c20b4a3 GH |
1442 | QLIST_FOREACH(dcl, &s->listeners, next) { |
1443 | if (dcl->ops->dpy_text_resize) { | |
bc2ed970 | 1444 | dcl->ops->dpy_text_resize(dcl, w, h); |
7c20b4a3 GH |
1445 | } |
1446 | } | |
1447 | } | |
1448 | ||
c78f7137 | 1449 | void dpy_mouse_set(QemuConsole *con, int x, int y, int on) |
7c20b4a3 | 1450 | { |
c78f7137 | 1451 | DisplayState *s = con->ds; |
7c20b4a3 | 1452 | struct DisplayChangeListener *dcl; |
321f048d GH |
1453 | |
1454 | if (con != active_console) { | |
1455 | return; | |
1456 | } | |
7c20b4a3 GH |
1457 | QLIST_FOREACH(dcl, &s->listeners, next) { |
1458 | if (dcl->ops->dpy_mouse_set) { | |
bc2ed970 | 1459 | dcl->ops->dpy_mouse_set(dcl, x, y, on); |
7c20b4a3 GH |
1460 | } |
1461 | } | |
1462 | } | |
1463 | ||
c78f7137 | 1464 | void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor) |
7c20b4a3 | 1465 | { |
c78f7137 | 1466 | DisplayState *s = con->ds; |
7c20b4a3 | 1467 | struct DisplayChangeListener *dcl; |
321f048d GH |
1468 | |
1469 | if (con != active_console) { | |
1470 | return; | |
1471 | } | |
7c20b4a3 GH |
1472 | QLIST_FOREACH(dcl, &s->listeners, next) { |
1473 | if (dcl->ops->dpy_cursor_define) { | |
bc2ed970 | 1474 | dcl->ops->dpy_cursor_define(dcl, cursor); |
7c20b4a3 GH |
1475 | } |
1476 | } | |
1477 | } | |
1478 | ||
c78f7137 | 1479 | bool dpy_cursor_define_supported(QemuConsole *con) |
7c20b4a3 | 1480 | { |
c78f7137 | 1481 | DisplayState *s = con->ds; |
7c20b4a3 GH |
1482 | struct DisplayChangeListener *dcl; |
1483 | QLIST_FOREACH(dcl, &s->listeners, next) { | |
1484 | if (dcl->ops->dpy_cursor_define) { | |
1485 | return true; | |
1486 | } | |
1487 | } | |
1488 | return false; | |
1489 | } | |
1490 | ||
98b50080 PB |
1491 | /***********************************************************/ |
1492 | /* register display */ | |
1493 | ||
64840c66 GH |
1494 | /* console.c internal use only */ |
1495 | static DisplayState *get_alloc_displaystate(void) | |
98b50080 | 1496 | { |
64840c66 GH |
1497 | if (!display_state) { |
1498 | display_state = g_new0(DisplayState, 1); | |
1499 | } | |
1500 | return display_state; | |
98b50080 PB |
1501 | } |
1502 | ||
64840c66 GH |
1503 | /* |
1504 | * Called by main(), after creating QemuConsoles | |
1505 | * and before initializing ui (sdl/vnc/...). | |
1506 | */ | |
1507 | DisplayState *init_displaystate(void) | |
98b50080 | 1508 | { |
64840c66 GH |
1509 | int i; |
1510 | ||
98b50080 | 1511 | if (!display_state) { |
64840c66 | 1512 | display_state = g_new0(DisplayState, 1); |
98b50080 | 1513 | } |
64840c66 GH |
1514 | |
1515 | for (i = 0; i < nb_consoles; i++) { | |
1516 | if (consoles[i]->console_type != GRAPHIC_CONSOLE && | |
1517 | consoles[i]->ds == NULL) { | |
1518 | text_console_do_init(consoles[i]->chr, display_state); | |
1519 | } | |
1520 | } | |
1521 | ||
98b50080 PB |
1522 | return display_state; |
1523 | } | |
1524 | ||
380cd056 | 1525 | QemuConsole *graphic_console_init(const GraphicHwOps *hw_ops, |
c78f7137 | 1526 | void *opaque) |
95219897 | 1527 | { |
64840c66 GH |
1528 | int width = 640; |
1529 | int height = 480; | |
76ffb0b4 | 1530 | QemuConsole *s; |
3023f332 | 1531 | DisplayState *ds; |
f0f2f976 | 1532 | |
64840c66 | 1533 | ds = get_alloc_displaystate(); |
437fe106 | 1534 | trace_console_gfx_new(); |
af3a9031 | 1535 | s = new_console(ds, GRAPHIC_CONSOLE); |
380cd056 | 1536 | s->hw_ops = hw_ops; |
95219897 | 1537 | s->hw = opaque; |
3023f332 | 1538 | |
321f048d | 1539 | s->surface = qemu_create_displaysurface(width, height); |
c78f7137 | 1540 | return s; |
e7f0ad58 FB |
1541 | } |
1542 | ||
95219897 | 1543 | int is_graphic_console(void) |
e7f0ad58 | 1544 | { |
4d3b6f6e | 1545 | return active_console && active_console->console_type == GRAPHIC_CONSOLE; |
e7f0ad58 FB |
1546 | } |
1547 | ||
c21bbcfa AZ |
1548 | int is_fixedsize_console(void) |
1549 | { | |
1550 | return active_console && active_console->console_type != TEXT_CONSOLE; | |
1551 | } | |
1552 | ||
4104833f PB |
1553 | static void text_console_set_echo(CharDriverState *chr, bool echo) |
1554 | { | |
76ffb0b4 | 1555 | QemuConsole *s = chr->opaque; |
4104833f PB |
1556 | |
1557 | s->echo = echo; | |
1558 | } | |
1559 | ||
bf1bed81 JK |
1560 | static void text_console_update_cursor(void *opaque) |
1561 | { | |
76ffb0b4 | 1562 | QemuConsole *s = opaque; |
bf1bed81 JK |
1563 | |
1564 | s->cursor_visible_phase = !s->cursor_visible_phase; | |
1dbfa005 | 1565 | graphic_hw_invalidate(s); |
bf1bed81 JK |
1566 | qemu_mod_timer(s->cursor_timer, |
1567 | qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2); | |
1568 | } | |
1569 | ||
380cd056 GH |
1570 | static const GraphicHwOps text_console_ops = { |
1571 | .invalidate = text_console_invalidate, | |
1572 | .text_update = text_console_update, | |
1573 | }; | |
1574 | ||
44b37b93 | 1575 | static void text_console_do_init(CharDriverState *chr, DisplayState *ds) |
e7f0ad58 | 1576 | { |
76ffb0b4 | 1577 | QemuConsole *s; |
36671fbd GH |
1578 | int g_width = 80 * FONT_WIDTH; |
1579 | int g_height = 24 * FONT_HEIGHT; | |
6d6f7c28 | 1580 | |
491e114a | 1581 | s = chr->opaque; |
6ea314d9 | 1582 | |
e7f0ad58 | 1583 | chr->chr_write = console_puts; |
6fcfafb7 | 1584 | |
e15d7371 FB |
1585 | s->out_fifo.buf = s->out_fifo_buf; |
1586 | s->out_fifo.buf_size = sizeof(s->out_fifo_buf); | |
7bd427d8 | 1587 | s->kbd_timer = qemu_new_timer_ms(rt_clock, kbd_send_chars, s); |
3023f332 | 1588 | s->ds = ds; |
3b46e624 | 1589 | |
e7f0ad58 FB |
1590 | s->y_displayed = 0; |
1591 | s->y_base = 0; | |
1592 | s->total_height = DEFAULT_BACKSCROLL; | |
1593 | s->x = 0; | |
1594 | s->y = 0; | |
36671fbd | 1595 | if (!s->surface) { |
321f048d | 1596 | if (active_console && active_console->surface) { |
36671fbd GH |
1597 | g_width = surface_width(active_console->surface); |
1598 | g_height = surface_height(active_console->surface); | |
321f048d | 1599 | } |
36671fbd | 1600 | s->surface = qemu_create_displaysurface(g_width, g_height); |
491e114a | 1601 | } |
6d6f7c28 | 1602 | |
bf1bed81 JK |
1603 | s->cursor_timer = |
1604 | qemu_new_timer_ms(rt_clock, text_console_update_cursor, s); | |
1605 | ||
380cd056 | 1606 | s->hw_ops = &text_console_ops; |
4d3b6f6e AZ |
1607 | s->hw = s; |
1608 | ||
6d6f7c28 PB |
1609 | /* Set text attribute defaults */ |
1610 | s->t_attrib_default.bold = 0; | |
1611 | s->t_attrib_default.uline = 0; | |
1612 | s->t_attrib_default.blink = 0; | |
1613 | s->t_attrib_default.invers = 0; | |
1614 | s->t_attrib_default.unvisible = 0; | |
1615 | s->t_attrib_default.fgcol = COLOR_WHITE; | |
1616 | s->t_attrib_default.bgcol = COLOR_BLACK; | |
6d6f7c28 PB |
1617 | /* set current text attributes to default */ |
1618 | s->t_attrib = s->t_attrib_default; | |
e7f0ad58 FB |
1619 | text_console_resize(s); |
1620 | ||
51bfa4d3 GH |
1621 | if (chr->label) { |
1622 | char msg[128]; | |
1623 | int len; | |
1624 | ||
735ba588 | 1625 | s->t_attrib.bgcol = COLOR_BLUE; |
51bfa4d3 GH |
1626 | len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label); |
1627 | console_puts(chr, (uint8_t*)msg, len); | |
735ba588 | 1628 | s->t_attrib = s->t_attrib_default; |
51bfa4d3 GH |
1629 | } |
1630 | ||
fee204fd | 1631 | qemu_chr_be_generic_open(chr); |
ceecf1d1 AJ |
1632 | if (chr->init) |
1633 | chr->init(chr); | |
e7f0ad58 | 1634 | } |
c60e08d9 | 1635 | |
702ec69c | 1636 | static CharDriverState *text_console_init(ChardevVC *vc) |
2796dae0 AL |
1637 | { |
1638 | CharDriverState *chr; | |
76ffb0b4 | 1639 | QemuConsole *s; |
702ec69c GH |
1640 | unsigned width = 0; |
1641 | unsigned height = 0; | |
2796dae0 | 1642 | |
7267c094 | 1643 | chr = g_malloc0(sizeof(CharDriverState)); |
2796dae0 | 1644 | |
702ec69c GH |
1645 | if (vc->has_width) { |
1646 | width = vc->width; | |
1647 | } else if (vc->has_cols) { | |
1648 | width = vc->cols * FONT_WIDTH; | |
1649 | } | |
491e114a | 1650 | |
702ec69c GH |
1651 | if (vc->has_height) { |
1652 | height = vc->height; | |
1653 | } else if (vc->has_rows) { | |
1654 | height = vc->rows * FONT_HEIGHT; | |
1655 | } | |
491e114a | 1656 | |
437fe106 | 1657 | trace_console_txt_new(width, height); |
491e114a PB |
1658 | if (width == 0 || height == 0) { |
1659 | s = new_console(NULL, TEXT_CONSOLE); | |
1660 | } else { | |
1661 | s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE); | |
36671fbd | 1662 | s->surface = qemu_create_displaysurface(width, height); |
491e114a PB |
1663 | } |
1664 | ||
1665 | if (!s) { | |
5354d083 | 1666 | g_free(chr); |
1f51470d | 1667 | return NULL; |
491e114a PB |
1668 | } |
1669 | ||
1670 | s->chr = chr; | |
491e114a | 1671 | chr->opaque = s; |
4104833f | 1672 | chr->chr_set_echo = text_console_set_echo; |
64840c66 GH |
1673 | |
1674 | if (display_state) { | |
1675 | text_console_do_init(chr, display_state); | |
1676 | } | |
1f51470d | 1677 | return chr; |
2796dae0 AL |
1678 | } |
1679 | ||
d82831db AL |
1680 | static VcHandler *vc_handler = text_console_init; |
1681 | ||
702ec69c | 1682 | CharDriverState *vc_init(ChardevVC *vc) |
d82831db | 1683 | { |
702ec69c | 1684 | return vc_handler(vc); |
d82831db AL |
1685 | } |
1686 | ||
1687 | void register_vc_handler(VcHandler *handler) | |
1688 | { | |
1689 | vc_handler = handler; | |
1690 | } | |
1691 | ||
c78f7137 | 1692 | void qemu_console_resize(QemuConsole *s, int width, int height) |
c60e08d9 | 1693 | { |
321f048d GH |
1694 | DisplaySurface *surface; |
1695 | ||
1696 | assert(s->console_type == GRAPHIC_CONSOLE); | |
321f048d GH |
1697 | surface = qemu_create_displaysurface(width, height); |
1698 | dpy_gfx_replace_surface(s, surface); | |
c60e08d9 | 1699 | } |
38334f76 | 1700 | |
c78f7137 | 1701 | void qemu_console_copy(QemuConsole *con, int src_x, int src_y, |
3023f332 | 1702 | int dst_x, int dst_y, int w, int h) |
c21bbcfa | 1703 | { |
321f048d GH |
1704 | assert(con->console_type == GRAPHIC_CONSOLE); |
1705 | dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h); | |
38334f76 | 1706 | } |
7d957bd8 | 1707 | |
c78f7137 GH |
1708 | DisplaySurface *qemu_console_surface(QemuConsole *console) |
1709 | { | |
321f048d | 1710 | return console->surface; |
c78f7137 GH |
1711 | } |
1712 | ||
1713 | DisplayState *qemu_console_displaystate(QemuConsole *console) | |
1714 | { | |
1715 | return console->ds; | |
1716 | } | |
1717 | ||
0da2ea1b | 1718 | PixelFormat qemu_different_endianness_pixelformat(int bpp) |
7d957bd8 AL |
1719 | { |
1720 | PixelFormat pf; | |
1721 | ||
1722 | memset(&pf, 0x00, sizeof(PixelFormat)); | |
1723 | ||
1724 | pf.bits_per_pixel = bpp; | |
feadf1a4 | 1725 | pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8); |
7d957bd8 AL |
1726 | pf.depth = bpp == 32 ? 24 : bpp; |
1727 | ||
1728 | switch (bpp) { | |
0da2ea1b | 1729 | case 24: |
1730 | pf.rmask = 0x000000FF; | |
1731 | pf.gmask = 0x0000FF00; | |
1732 | pf.bmask = 0x00FF0000; | |
1733 | pf.rmax = 255; | |
1734 | pf.gmax = 255; | |
1735 | pf.bmax = 255; | |
1736 | pf.rshift = 0; | |
1737 | pf.gshift = 8; | |
1738 | pf.bshift = 16; | |
90a1e3c0 AL |
1739 | pf.rbits = 8; |
1740 | pf.gbits = 8; | |
1741 | pf.bbits = 8; | |
7d957bd8 | 1742 | break; |
0da2ea1b | 1743 | case 32: |
1744 | pf.rmask = 0x0000FF00; | |
1745 | pf.gmask = 0x00FF0000; | |
1746 | pf.bmask = 0xFF000000; | |
1747 | pf.amask = 0x00000000; | |
1748 | pf.amax = 255; | |
1749 | pf.rmax = 255; | |
1750 | pf.gmax = 255; | |
1751 | pf.bmax = 255; | |
1752 | pf.ashift = 0; | |
1753 | pf.rshift = 8; | |
1754 | pf.gshift = 16; | |
1755 | pf.bshift = 24; | |
90a1e3c0 AL |
1756 | pf.rbits = 8; |
1757 | pf.gbits = 8; | |
1758 | pf.bbits = 8; | |
1759 | pf.abits = 8; | |
0da2ea1b | 1760 | break; |
1761 | default: | |
1762 | break; | |
1763 | } | |
1764 | return pf; | |
1765 | } | |
1766 | ||
1767 | PixelFormat qemu_default_pixelformat(int bpp) | |
1768 | { | |
1769 | PixelFormat pf; | |
1770 | ||
1771 | memset(&pf, 0x00, sizeof(PixelFormat)); | |
1772 | ||
1773 | pf.bits_per_pixel = bpp; | |
feadf1a4 | 1774 | pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8); |
0da2ea1b | 1775 | pf.depth = bpp == 32 ? 24 : bpp; |
1776 | ||
1777 | switch (bpp) { | |
b6278084 GH |
1778 | case 15: |
1779 | pf.bits_per_pixel = 16; | |
b6278084 GH |
1780 | pf.rmask = 0x00007c00; |
1781 | pf.gmask = 0x000003E0; | |
1782 | pf.bmask = 0x0000001F; | |
1783 | pf.rmax = 31; | |
1784 | pf.gmax = 31; | |
1785 | pf.bmax = 31; | |
1786 | pf.rshift = 10; | |
1787 | pf.gshift = 5; | |
1788 | pf.bshift = 0; | |
1789 | pf.rbits = 5; | |
1790 | pf.gbits = 5; | |
1791 | pf.bbits = 5; | |
1792 | break; | |
7d957bd8 AL |
1793 | case 16: |
1794 | pf.rmask = 0x0000F800; | |
1795 | pf.gmask = 0x000007E0; | |
1796 | pf.bmask = 0x0000001F; | |
1797 | pf.rmax = 31; | |
1798 | pf.gmax = 63; | |
1799 | pf.bmax = 31; | |
1800 | pf.rshift = 11; | |
1801 | pf.gshift = 5; | |
1802 | pf.bshift = 0; | |
90a1e3c0 AL |
1803 | pf.rbits = 5; |
1804 | pf.gbits = 6; | |
1805 | pf.bbits = 5; | |
7d957bd8 AL |
1806 | break; |
1807 | case 24: | |
0da2ea1b | 1808 | pf.rmask = 0x00FF0000; |
1809 | pf.gmask = 0x0000FF00; | |
1810 | pf.bmask = 0x000000FF; | |
1811 | pf.rmax = 255; | |
1812 | pf.gmax = 255; | |
1813 | pf.bmax = 255; | |
1814 | pf.rshift = 16; | |
1815 | pf.gshift = 8; | |
1816 | pf.bshift = 0; | |
90a1e3c0 AL |
1817 | pf.rbits = 8; |
1818 | pf.gbits = 8; | |
1819 | pf.bbits = 8; | |
0eba62e0 | 1820 | break; |
7d957bd8 AL |
1821 | case 32: |
1822 | pf.rmask = 0x00FF0000; | |
1823 | pf.gmask = 0x0000FF00; | |
1824 | pf.bmask = 0x000000FF; | |
1825 | pf.rmax = 255; | |
1826 | pf.gmax = 255; | |
1827 | pf.bmax = 255; | |
1828 | pf.rshift = 16; | |
1829 | pf.gshift = 8; | |
1830 | pf.bshift = 0; | |
90a1e3c0 AL |
1831 | pf.rbits = 8; |
1832 | pf.gbits = 8; | |
1833 | pf.bbits = 8; | |
7d957bd8 AL |
1834 | break; |
1835 | default: | |
1836 | break; | |
1837 | } | |
1838 | return pf; | |
1839 | } | |
01f45d98 | 1840 | |
702ec69c GH |
1841 | static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, |
1842 | Error **errp) | |
1843 | { | |
1844 | int val; | |
1845 | ||
1846 | backend->vc = g_new0(ChardevVC, 1); | |
1847 | ||
1848 | val = qemu_opt_get_number(opts, "width", 0); | |
1849 | if (val != 0) { | |
1850 | backend->vc->has_width = true; | |
1851 | backend->vc->width = val; | |
1852 | } | |
1853 | ||
1854 | val = qemu_opt_get_number(opts, "height", 0); | |
1855 | if (val != 0) { | |
1856 | backend->vc->has_height = true; | |
1857 | backend->vc->height = val; | |
1858 | } | |
1859 | ||
1860 | val = qemu_opt_get_number(opts, "cols", 0); | |
1861 | if (val != 0) { | |
1862 | backend->vc->has_cols = true; | |
1863 | backend->vc->cols = val; | |
1864 | } | |
1865 | ||
1866 | val = qemu_opt_get_number(opts, "rows", 0); | |
1867 | if (val != 0) { | |
1868 | backend->vc->has_rows = true; | |
1869 | backend->vc->rows = val; | |
1870 | } | |
1871 | } | |
1872 | ||
01f45d98 AL |
1873 | static void register_types(void) |
1874 | { | |
702ec69c GH |
1875 | register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC, |
1876 | qemu_chr_parse_vc); | |
01f45d98 AL |
1877 | } |
1878 | ||
1879 | type_init(register_types); |