]>
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 | */ | |
e688df6b | 24 | |
e16f4c87 | 25 | #include "qemu/osdep.h" |
28ecbaee | 26 | #include "ui/console.h" |
aa2beaa1 | 27 | #include "hw/qdev-core.h" |
e688df6b | 28 | #include "qapi/error.h" |
9af23989 | 29 | #include "qapi/qapi-commands-ui.h" |
0b8fa32f | 30 | #include "qemu/module.h" |
922a01a0 | 31 | #include "qemu/option.h" |
1de7afc9 | 32 | #include "qemu/timer.h" |
4d43a603 | 33 | #include "chardev/char-fe.h" |
ac86048b | 34 | #include "trace.h" |
a77549b3 | 35 | #include "exec/memory.h" |
c5f2bce5 | 36 | #include "io/channel-file.h" |
e7f0ad58 FB |
37 | |
38 | #define DEFAULT_BACKSCROLL 512 | |
bf1bed81 | 39 | #define CONSOLE_CURSOR_PERIOD 500 |
e7f0ad58 | 40 | |
6d6f7c28 PB |
41 | typedef struct TextAttributes { |
42 | uint8_t fgcol:4; | |
43 | uint8_t bgcol:4; | |
44 | uint8_t bold:1; | |
45 | uint8_t uline:1; | |
46 | uint8_t blink:1; | |
47 | uint8_t invers:1; | |
48 | uint8_t unvisible:1; | |
49 | } TextAttributes; | |
50 | ||
e7f0ad58 FB |
51 | typedef struct TextCell { |
52 | uint8_t ch; | |
6d6f7c28 | 53 | TextAttributes t_attrib; |
e7f0ad58 FB |
54 | } TextCell; |
55 | ||
56 | #define MAX_ESC_PARAMS 3 | |
57 | ||
58 | enum TTYState { | |
59 | TTY_STATE_NORM, | |
60 | TTY_STATE_ESC, | |
61 | TTY_STATE_CSI, | |
62 | }; | |
63 | ||
e15d7371 FB |
64 | typedef struct QEMUFIFO { |
65 | uint8_t *buf; | |
66 | int buf_size; | |
67 | int count, wptr, rptr; | |
68 | } QEMUFIFO; | |
69 | ||
9596ebb7 | 70 | static int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1) |
e15d7371 FB |
71 | { |
72 | int l, len; | |
73 | ||
74 | l = f->buf_size - f->count; | |
75 | if (len1 > l) | |
76 | len1 = l; | |
77 | len = len1; | |
78 | while (len > 0) { | |
79 | l = f->buf_size - f->wptr; | |
80 | if (l > len) | |
81 | l = len; | |
82 | memcpy(f->buf + f->wptr, buf, l); | |
83 | f->wptr += l; | |
84 | if (f->wptr >= f->buf_size) | |
85 | f->wptr = 0; | |
86 | buf += l; | |
87 | len -= l; | |
88 | } | |
89 | f->count += len1; | |
90 | return len1; | |
91 | } | |
92 | ||
9596ebb7 | 93 | static int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1) |
e15d7371 FB |
94 | { |
95 | int l, len; | |
96 | ||
97 | if (len1 > f->count) | |
98 | len1 = f->count; | |
99 | len = len1; | |
100 | while (len > 0) { | |
101 | l = f->buf_size - f->rptr; | |
102 | if (l > len) | |
103 | l = len; | |
104 | memcpy(buf, f->buf + f->rptr, l); | |
105 | f->rptr += l; | |
106 | if (f->rptr >= f->buf_size) | |
107 | f->rptr = 0; | |
108 | buf += l; | |
109 | len -= l; | |
110 | } | |
111 | f->count -= len1; | |
112 | return len1; | |
113 | } | |
114 | ||
af3a9031 TS |
115 | typedef enum { |
116 | GRAPHIC_CONSOLE, | |
c21bbcfa AZ |
117 | TEXT_CONSOLE, |
118 | TEXT_CONSOLE_FIXED_SIZE | |
c227f099 | 119 | } console_type_t; |
af3a9031 | 120 | |
76ffb0b4 | 121 | struct QemuConsole { |
95be0669 GH |
122 | Object parent; |
123 | ||
f81bdefb | 124 | int index; |
c227f099 | 125 | console_type_t console_type; |
e7f0ad58 | 126 | DisplayState *ds; |
321f048d | 127 | DisplaySurface *surface; |
284d1c6b | 128 | int dcls; |
06020b95 | 129 | DisplayChangeListener *gl; |
f607867c | 130 | bool gl_block; |
b3cb21b9 | 131 | int window_id; |
76ffb0b4 | 132 | |
95219897 | 133 | /* Graphic console state. */ |
aa2beaa1 | 134 | Object *device; |
5643706a | 135 | uint32_t head; |
6f90f3d7 | 136 | QemuUIInfo ui_info; |
cf1ecc82 | 137 | QEMUTimer *ui_timer; |
380cd056 | 138 | const GraphicHwOps *hw_ops; |
95219897 | 139 | void *hw; |
76ffb0b4 GH |
140 | |
141 | /* Text console state */ | |
e7f0ad58 FB |
142 | int width; |
143 | int height; | |
144 | int total_height; | |
145 | int backscroll_height; | |
e7f0ad58 | 146 | int x, y; |
adb47967 | 147 | int x_saved, y_saved; |
e7f0ad58 FB |
148 | int y_displayed; |
149 | int y_base; | |
6d6f7c28 PB |
150 | TextAttributes t_attrib_default; /* default text attributes */ |
151 | TextAttributes t_attrib; /* currently active text attributes */ | |
e7f0ad58 | 152 | TextCell *cells; |
4d3b6f6e | 153 | int text_x[2], text_y[2], cursor_invalidate; |
4104833f | 154 | int echo; |
e7f0ad58 | 155 | |
14778c20 PB |
156 | int update_x0; |
157 | int update_y0; | |
158 | int update_x1; | |
159 | int update_y1; | |
160 | ||
e7f0ad58 FB |
161 | enum TTYState state; |
162 | int esc_params[MAX_ESC_PARAMS]; | |
163 | int nb_esc_params; | |
164 | ||
0ec7b3e7 | 165 | Chardev *chr; |
e15d7371 FB |
166 | /* fifo for key pressed */ |
167 | QEMUFIFO out_fifo; | |
168 | uint8_t out_fifo_buf[16]; | |
169 | QEMUTimer *kbd_timer; | |
cd6cd8fa GH |
170 | |
171 | QTAILQ_ENTRY(QemuConsole) next; | |
e7f0ad58 FB |
172 | }; |
173 | ||
27be5587 | 174 | struct DisplayState { |
1246b259 | 175 | QEMUTimer *gui_timer; |
0f7b2864 GH |
176 | uint64_t last_update; |
177 | uint64_t update_interval; | |
178 | bool refreshing; | |
27be5587 GH |
179 | bool have_gfx; |
180 | bool have_text; | |
181 | ||
182 | QLIST_HEAD(, DisplayChangeListener) listeners; | |
183 | }; | |
184 | ||
98b50080 | 185 | static DisplayState *display_state; |
76ffb0b4 | 186 | static QemuConsole *active_console; |
eae3eb3e | 187 | static QTAILQ_HEAD(, QemuConsole) consoles = |
cd6cd8fa | 188 | QTAILQ_HEAD_INITIALIZER(consoles); |
aea7947c GH |
189 | static bool cursor_visible_phase; |
190 | static QEMUTimer *cursor_timer; | |
e7f0ad58 | 191 | |
0ec7b3e7 | 192 | static void text_console_do_init(Chardev *chr, DisplayState *ds); |
0f7b2864 | 193 | static void dpy_refresh(DisplayState *s); |
5209089f | 194 | static DisplayState *get_alloc_displaystate(void); |
aea7947c GH |
195 | static void text_console_update_cursor_timer(void); |
196 | static void text_console_update_cursor(void *opaque); | |
46e5841c | 197 | static bool ppm_save(int fd, DisplaySurface *ds, Error **errp); |
64840c66 | 198 | |
98a9ad90 GH |
199 | static void gui_update(void *opaque) |
200 | { | |
0f7b2864 GH |
201 | uint64_t interval = GUI_REFRESH_INTERVAL_IDLE; |
202 | uint64_t dcl_interval; | |
98a9ad90 GH |
203 | DisplayState *ds = opaque; |
204 | DisplayChangeListener *dcl; | |
cd6cd8fa | 205 | QemuConsole *con; |
98a9ad90 | 206 | |
0f7b2864 | 207 | ds->refreshing = true; |
98a9ad90 | 208 | dpy_refresh(ds); |
0f7b2864 | 209 | ds->refreshing = false; |
98a9ad90 GH |
210 | |
211 | QLIST_FOREACH(dcl, &ds->listeners, next) { | |
0f7b2864 GH |
212 | dcl_interval = dcl->update_interval ? |
213 | dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT; | |
214 | if (interval > dcl_interval) { | |
215 | interval = dcl_interval; | |
98a9ad90 GH |
216 | } |
217 | } | |
0f7b2864 GH |
218 | if (ds->update_interval != interval) { |
219 | ds->update_interval = interval; | |
cd6cd8fa GH |
220 | QTAILQ_FOREACH(con, &consoles, next) { |
221 | if (con->hw_ops->update_interval) { | |
222 | con->hw_ops->update_interval(con->hw, interval); | |
dea1b0bd GH |
223 | } |
224 | } | |
0f7b2864 GH |
225 | trace_console_refresh(interval); |
226 | } | |
bc72ad67 AB |
227 | ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
228 | timer_mod(ds->gui_timer, ds->last_update + interval); | |
98a9ad90 GH |
229 | } |
230 | ||
231 | static void gui_setup_refresh(DisplayState *ds) | |
232 | { | |
233 | DisplayChangeListener *dcl; | |
234 | bool need_timer = false; | |
235 | bool have_gfx = false; | |
236 | bool have_text = false; | |
237 | ||
238 | QLIST_FOREACH(dcl, &ds->listeners, next) { | |
239 | if (dcl->ops->dpy_refresh != NULL) { | |
240 | need_timer = true; | |
241 | } | |
242 | if (dcl->ops->dpy_gfx_update != NULL) { | |
243 | have_gfx = true; | |
244 | } | |
245 | if (dcl->ops->dpy_text_update != NULL) { | |
246 | have_text = true; | |
247 | } | |
248 | } | |
249 | ||
250 | if (need_timer && ds->gui_timer == NULL) { | |
bc72ad67 AB |
251 | ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds); |
252 | timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)); | |
98a9ad90 GH |
253 | } |
254 | if (!need_timer && ds->gui_timer != NULL) { | |
bc72ad67 AB |
255 | timer_del(ds->gui_timer); |
256 | timer_free(ds->gui_timer); | |
98a9ad90 GH |
257 | ds->gui_timer = NULL; |
258 | } | |
259 | ||
260 | ds->have_gfx = have_gfx; | |
261 | ds->have_text = have_text; | |
262 | } | |
263 | ||
4d631621 MAL |
264 | void graphic_hw_update_done(QemuConsole *con) |
265 | { | |
266 | } | |
267 | ||
1dbfa005 | 268 | void graphic_hw_update(QemuConsole *con) |
95219897 | 269 | { |
4d631621 | 270 | bool async = false; |
1dbfa005 GH |
271 | if (!con) { |
272 | con = active_console; | |
273 | } | |
380cd056 GH |
274 | if (con && con->hw_ops->gfx_update) { |
275 | con->hw_ops->gfx_update(con->hw); | |
4d631621 MAL |
276 | async = con->hw_ops->gfx_update_async; |
277 | } | |
278 | if (!async) { | |
279 | graphic_hw_update_done(con); | |
1dbfa005 | 280 | } |
95219897 PB |
281 | } |
282 | ||
bba19b88 GH |
283 | void graphic_hw_gl_block(QemuConsole *con, bool block) |
284 | { | |
f607867c GH |
285 | assert(con != NULL); |
286 | ||
287 | con->gl_block = block; | |
288 | if (con->hw_ops->gl_block) { | |
bba19b88 GH |
289 | con->hw_ops->gl_block(con->hw, block); |
290 | } | |
291 | } | |
292 | ||
b3cb21b9 ST |
293 | int qemu_console_get_window_id(QemuConsole *con) |
294 | { | |
295 | return con->window_id; | |
296 | } | |
297 | ||
298 | void qemu_console_set_window_id(QemuConsole *con, int window_id) | |
299 | { | |
300 | con->window_id = window_id; | |
301 | } | |
302 | ||
1dbfa005 | 303 | void graphic_hw_invalidate(QemuConsole *con) |
95219897 | 304 | { |
1dbfa005 GH |
305 | if (!con) { |
306 | con = active_console; | |
307 | } | |
380cd056 GH |
308 | if (con && con->hw_ops->invalidate) { |
309 | con->hw_ops->invalidate(con->hw); | |
1dbfa005 | 310 | } |
95219897 PB |
311 | } |
312 | ||
46e5841c | 313 | static bool ppm_save(int fd, DisplaySurface *ds, Error **errp) |
95219897 | 314 | { |
2c62f08d GH |
315 | int width = pixman_image_get_width(ds->image); |
316 | int height = pixman_image_get_height(ds->image); | |
c5f2bce5 MAL |
317 | g_autoptr(Object) ioc = OBJECT(qio_channel_file_new_fd(fd)); |
318 | g_autofree char *header = NULL; | |
319 | g_autoptr(pixman_image_t) linebuf = NULL; | |
2c62f08d | 320 | int y; |
2c62f08d | 321 | |
46e5841c | 322 | trace_ppm_save(fd, ds); |
c5f2bce5 MAL |
323 | |
324 | header = g_strdup_printf("P6\n%d %d\n%d\n", width, height, 255); | |
325 | if (qio_channel_write_all(QIO_CHANNEL(ioc), | |
326 | header, strlen(header), errp) < 0) { | |
327 | return false; | |
2c62f08d | 328 | } |
c5f2bce5 | 329 | |
2c62f08d GH |
330 | linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width); |
331 | for (y = 0; y < height; y++) { | |
332 | qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y); | |
c5f2bce5 MAL |
333 | if (qio_channel_write_all(QIO_CHANNEL(ioc), |
334 | (char *)pixman_image_get_data(linebuf), | |
335 | pixman_image_get_stride(linebuf), errp) < 0) { | |
336 | return false; | |
337 | } | |
f81bdefb JK |
338 | } |
339 | ||
c5f2bce5 | 340 | return true; |
2c62f08d GH |
341 | } |
342 | ||
f771c544 TH |
343 | void qmp_screendump(const char *filename, bool has_device, const char *device, |
344 | bool has_head, int64_t head, Error **errp) | |
2c62f08d | 345 | { |
f771c544 | 346 | QemuConsole *con; |
2c62f08d | 347 | DisplaySurface *surface; |
46e5841c | 348 | int fd; |
2c62f08d | 349 | |
f771c544 TH |
350 | if (has_device) { |
351 | con = qemu_console_lookup_by_device_name(device, has_head ? head : 0, | |
352 | errp); | |
353 | if (!con) { | |
354 | return; | |
355 | } | |
356 | } else { | |
357 | if (has_head) { | |
358 | error_setg(errp, "'head' must be specified together with 'device'"); | |
359 | return; | |
360 | } | |
361 | con = qemu_console_lookup_by_index(0); | |
362 | if (!con) { | |
363 | error_setg(errp, "There is no console to take a screendump from"); | |
364 | return; | |
365 | } | |
33bcd98c | 366 | } |
2c62f08d GH |
367 | |
368 | graphic_hw_update(con); | |
369 | surface = qemu_console_surface(con); | |
08d9864f MP |
370 | if (!surface) { |
371 | error_setg(errp, "no surface"); | |
372 | return; | |
373 | } | |
374 | ||
46e5841c MAL |
375 | fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); |
376 | if (fd == -1) { | |
377 | error_setg(errp, "failed to open file '%s': %s", filename, | |
378 | strerror(errno)); | |
379 | return; | |
380 | } | |
381 | ||
382 | if (!ppm_save(fd, surface, errp)) { | |
53a61ecb | 383 | qemu_unlink(filename); |
46e5841c | 384 | } |
95219897 PB |
385 | } |
386 | ||
1dbfa005 | 387 | void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata) |
4d3b6f6e | 388 | { |
1dbfa005 GH |
389 | if (!con) { |
390 | con = active_console; | |
391 | } | |
380cd056 GH |
392 | if (con && con->hw_ops->text_update) { |
393 | con->hw_ops->text_update(con->hw, chardata); | |
394 | } | |
4d3b6f6e AZ |
395 | } |
396 | ||
1562e531 GH |
397 | static void vga_fill_rect(QemuConsole *con, |
398 | int posx, int posy, int width, int height, | |
e27bd65a | 399 | pixman_color_t color) |
e7f0ad58 | 400 | { |
1562e531 | 401 | DisplaySurface *surface = qemu_console_surface(con); |
68db6dc5 GH |
402 | pixman_rectangle16_t rect = { |
403 | .x = posx, .y = posy, .width = width, .height = height | |
404 | }; | |
68db6dc5 | 405 | |
68db6dc5 | 406 | pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image, |
e27bd65a | 407 | &color, 1, &rect); |
e7f0ad58 FB |
408 | } |
409 | ||
410 | /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */ | |
1562e531 GH |
411 | static void vga_bitblt(QemuConsole *con, |
412 | int xs, int ys, int xd, int yd, int w, int h) | |
e7f0ad58 | 413 | { |
1562e531 | 414 | DisplaySurface *surface = qemu_console_surface(con); |
68db6dc5 GH |
415 | |
416 | pixman_image_composite(PIXMAN_OP_SRC, | |
417 | surface->image, NULL, surface->image, | |
418 | xs, ys, 0, 0, xd, yd, w, h); | |
e7f0ad58 FB |
419 | } |
420 | ||
421 | /***********************************************************/ | |
422 | /* basic char display */ | |
423 | ||
424 | #define FONT_HEIGHT 16 | |
425 | #define FONT_WIDTH 8 | |
426 | ||
427 | #include "vgafont.h" | |
428 | ||
e27bd65a GH |
429 | #define QEMU_RGB(r, g, b) \ |
430 | { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff } | |
431 | ||
432 | static const pixman_color_t color_table_rgb[2][8] = { | |
6d6f7c28 | 433 | { /* dark */ |
4083733d OH |
434 | [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */ |
435 | [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */ | |
436 | [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */ | |
437 | [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */ | |
438 | [QEMU_COLOR_RED] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */ | |
439 | [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */ | |
440 | [QEMU_COLOR_YELLOW] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */ | |
441 | [QEMU_COLOR_WHITE] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */ | |
6d6f7c28 PB |
442 | }, |
443 | { /* bright */ | |
4083733d OH |
444 | [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */ |
445 | [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */ | |
446 | [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xff, 0x00), /* green */ | |
447 | [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */ | |
448 | [QEMU_COLOR_RED] = QEMU_RGB(0xff, 0x00, 0x00), /* red */ | |
449 | [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */ | |
450 | [QEMU_COLOR_YELLOW] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */ | |
451 | [QEMU_COLOR_WHITE] = QEMU_RGB(0xff, 0xff, 0xff), /* white */ | |
6d6f7c28 | 452 | } |
e7f0ad58 FB |
453 | }; |
454 | ||
1562e531 | 455 | static void vga_putcharxy(QemuConsole *s, int x, int y, int ch, |
6d6f7c28 | 456 | TextAttributes *t_attrib) |
e7f0ad58 | 457 | { |
7d6ba01c | 458 | static pixman_image_t *glyphs[256]; |
1562e531 | 459 | DisplaySurface *surface = qemu_console_surface(s); |
e27bd65a | 460 | pixman_color_t fgcol, bgcol; |
6d6f7c28 PB |
461 | |
462 | if (t_attrib->invers) { | |
cf6f0548 GH |
463 | bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol]; |
464 | fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol]; | |
6d6f7c28 | 465 | } else { |
cf6f0548 GH |
466 | fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol]; |
467 | bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol]; | |
6d6f7c28 | 468 | } |
e7f0ad58 | 469 | |
7d6ba01c GH |
470 | if (!glyphs[ch]) { |
471 | glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch); | |
e7f0ad58 | 472 | } |
7d6ba01c | 473 | qemu_pixman_glyph_render(glyphs[ch], surface->image, |
e27bd65a | 474 | &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT); |
e7f0ad58 FB |
475 | } |
476 | ||
76ffb0b4 | 477 | static void text_console_resize(QemuConsole *s) |
e7f0ad58 FB |
478 | { |
479 | TextCell *cells, *c, *c1; | |
480 | int w1, x, y, last_width; | |
481 | ||
482 | last_width = s->width; | |
36671fbd GH |
483 | s->width = surface_width(s->surface) / FONT_WIDTH; |
484 | s->height = surface_height(s->surface) / FONT_HEIGHT; | |
e7f0ad58 FB |
485 | |
486 | w1 = last_width; | |
487 | if (s->width < w1) | |
488 | w1 = s->width; | |
489 | ||
5b8541c6 | 490 | cells = g_new(TextCell, s->width * s->total_height + 1); |
e7f0ad58 FB |
491 | for(y = 0; y < s->total_height; y++) { |
492 | c = &cells[y * s->width]; | |
493 | if (w1 > 0) { | |
494 | c1 = &s->cells[y * last_width]; | |
495 | for(x = 0; x < w1; x++) { | |
496 | *c++ = *c1++; | |
497 | } | |
498 | } | |
499 | for(x = w1; x < s->width; x++) { | |
500 | c->ch = ' '; | |
6d6f7c28 | 501 | c->t_attrib = s->t_attrib_default; |
e7f0ad58 FB |
502 | c++; |
503 | } | |
504 | } | |
7267c094 | 505 | g_free(s->cells); |
e7f0ad58 FB |
506 | s->cells = cells; |
507 | } | |
508 | ||
76ffb0b4 | 509 | static inline void text_update_xy(QemuConsole *s, int x, int y) |
4d3b6f6e AZ |
510 | { |
511 | s->text_x[0] = MIN(s->text_x[0], x); | |
512 | s->text_x[1] = MAX(s->text_x[1], x); | |
513 | s->text_y[0] = MIN(s->text_y[0], y); | |
514 | s->text_y[1] = MAX(s->text_y[1], y); | |
515 | } | |
516 | ||
76ffb0b4 | 517 | static void invalidate_xy(QemuConsole *s, int x, int y) |
14778c20 | 518 | { |
b35e3ba0 GH |
519 | if (!qemu_console_is_visible(s)) { |
520 | return; | |
521 | } | |
14778c20 PB |
522 | if (s->update_x0 > x * FONT_WIDTH) |
523 | s->update_x0 = x * FONT_WIDTH; | |
524 | if (s->update_y0 > y * FONT_HEIGHT) | |
525 | s->update_y0 = y * FONT_HEIGHT; | |
526 | if (s->update_x1 < (x + 1) * FONT_WIDTH) | |
527 | s->update_x1 = (x + 1) * FONT_WIDTH; | |
528 | if (s->update_y1 < (y + 1) * FONT_HEIGHT) | |
529 | s->update_y1 = (y + 1) * FONT_HEIGHT; | |
530 | } | |
531 | ||
76ffb0b4 | 532 | static void update_xy(QemuConsole *s, int x, int y) |
e7f0ad58 FB |
533 | { |
534 | TextCell *c; | |
535 | int y1, y2; | |
536 | ||
1562e531 GH |
537 | if (s->ds->have_text) { |
538 | text_update_xy(s, x, y); | |
539 | } | |
4d3b6f6e | 540 | |
b35e3ba0 GH |
541 | y1 = (s->y_base + y) % s->total_height; |
542 | y2 = y1 - s->y_displayed; | |
543 | if (y2 < 0) { | |
544 | y2 += s->total_height; | |
545 | } | |
546 | if (y2 < s->height) { | |
5b8541c6 GH |
547 | if (x >= s->width) { |
548 | x = s->width - 1; | |
549 | } | |
b35e3ba0 GH |
550 | c = &s->cells[y1 * s->width + x]; |
551 | vga_putcharxy(s, x, y2, c->ch, | |
552 | &(c->t_attrib)); | |
553 | invalidate_xy(s, x, y2); | |
e7f0ad58 FB |
554 | } |
555 | } | |
556 | ||
76ffb0b4 | 557 | static void console_show_cursor(QemuConsole *s, int show) |
e7f0ad58 FB |
558 | { |
559 | TextCell *c; | |
560 | int y, y1; | |
1562e531 | 561 | int x = s->x; |
e7f0ad58 | 562 | |
1562e531 GH |
563 | if (s->ds->have_text) { |
564 | s->cursor_invalidate = 1; | |
565 | } | |
4d3b6f6e | 566 | |
b35e3ba0 GH |
567 | if (x >= s->width) { |
568 | x = s->width - 1; | |
569 | } | |
570 | y1 = (s->y_base + s->y) % s->total_height; | |
571 | y = y1 - s->y_displayed; | |
572 | if (y < 0) { | |
573 | y += s->total_height; | |
574 | } | |
575 | if (y < s->height) { | |
576 | c = &s->cells[y1 * s->width + x]; | |
aea7947c | 577 | if (show && cursor_visible_phase) { |
b35e3ba0 GH |
578 | TextAttributes t_attrib = s->t_attrib_default; |
579 | t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */ | |
580 | vga_putcharxy(s, x, y, c->ch, &t_attrib); | |
581 | } else { | |
582 | vga_putcharxy(s, x, y, c->ch, &(c->t_attrib)); | |
e7f0ad58 | 583 | } |
b35e3ba0 | 584 | invalidate_xy(s, x, y); |
e7f0ad58 FB |
585 | } |
586 | } | |
587 | ||
76ffb0b4 | 588 | static void console_refresh(QemuConsole *s) |
e7f0ad58 | 589 | { |
1562e531 | 590 | DisplaySurface *surface = qemu_console_surface(s); |
e7f0ad58 FB |
591 | TextCell *c; |
592 | int x, y, y1; | |
593 | ||
a93a4a22 | 594 | if (s->ds->have_text) { |
4d3b6f6e AZ |
595 | s->text_x[0] = 0; |
596 | s->text_y[0] = 0; | |
597 | s->text_x[1] = s->width - 1; | |
598 | s->text_y[1] = s->height - 1; | |
599 | s->cursor_invalidate = 1; | |
4d3b6f6e | 600 | } |
e7f0ad58 | 601 | |
b35e3ba0 | 602 | vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface), |
4083733d | 603 | color_table_rgb[0][QEMU_COLOR_BLACK]); |
b35e3ba0 GH |
604 | y1 = s->y_displayed; |
605 | for (y = 0; y < s->height; y++) { | |
606 | c = s->cells + y1 * s->width; | |
607 | for (x = 0; x < s->width; x++) { | |
608 | vga_putcharxy(s, x, y, c->ch, | |
609 | &(c->t_attrib)); | |
610 | c++; | |
611 | } | |
612 | if (++y1 == s->total_height) { | |
613 | y1 = 0; | |
e7f0ad58 | 614 | } |
e7f0ad58 | 615 | } |
b35e3ba0 GH |
616 | console_show_cursor(s, 1); |
617 | dpy_gfx_update(s, 0, 0, | |
618 | surface_width(surface), surface_height(surface)); | |
e7f0ad58 FB |
619 | } |
620 | ||
81c0d5a6 | 621 | static void console_scroll(QemuConsole *s, int ydelta) |
e7f0ad58 | 622 | { |
e7f0ad58 | 623 | int i, y1; |
3b46e624 | 624 | |
e7f0ad58 FB |
625 | if (ydelta > 0) { |
626 | for(i = 0; i < ydelta; i++) { | |
627 | if (s->y_displayed == s->y_base) | |
628 | break; | |
629 | if (++s->y_displayed == s->total_height) | |
630 | s->y_displayed = 0; | |
631 | } | |
632 | } else { | |
633 | ydelta = -ydelta; | |
634 | i = s->backscroll_height; | |
635 | if (i > s->total_height - s->height) | |
636 | i = s->total_height - s->height; | |
637 | y1 = s->y_base - i; | |
638 | if (y1 < 0) | |
639 | y1 += s->total_height; | |
640 | for(i = 0; i < ydelta; i++) { | |
641 | if (s->y_displayed == y1) | |
642 | break; | |
643 | if (--s->y_displayed < 0) | |
644 | s->y_displayed = s->total_height - 1; | |
645 | } | |
646 | } | |
647 | console_refresh(s); | |
648 | } | |
649 | ||
76ffb0b4 | 650 | static void console_put_lf(QemuConsole *s) |
e7f0ad58 FB |
651 | { |
652 | TextCell *c; | |
653 | int x, y1; | |
654 | ||
e7f0ad58 FB |
655 | s->y++; |
656 | if (s->y >= s->height) { | |
657 | s->y = s->height - 1; | |
6d6f7c28 | 658 | |
e7f0ad58 FB |
659 | if (s->y_displayed == s->y_base) { |
660 | if (++s->y_displayed == s->total_height) | |
661 | s->y_displayed = 0; | |
662 | } | |
663 | if (++s->y_base == s->total_height) | |
664 | s->y_base = 0; | |
665 | if (s->backscroll_height < s->total_height) | |
666 | s->backscroll_height++; | |
667 | y1 = (s->y_base + s->height - 1) % s->total_height; | |
668 | c = &s->cells[y1 * s->width]; | |
669 | for(x = 0; x < s->width; x++) { | |
670 | c->ch = ' '; | |
6d6f7c28 | 671 | c->t_attrib = s->t_attrib_default; |
e7f0ad58 FB |
672 | c++; |
673 | } | |
b35e3ba0 | 674 | if (s->y_displayed == s->y_base) { |
1562e531 | 675 | if (s->ds->have_text) { |
4d3b6f6e AZ |
676 | s->text_x[0] = 0; |
677 | s->text_y[0] = 0; | |
678 | s->text_x[1] = s->width - 1; | |
679 | s->text_y[1] = s->height - 1; | |
4d3b6f6e AZ |
680 | } |
681 | ||
b35e3ba0 GH |
682 | vga_bitblt(s, 0, FONT_HEIGHT, 0, 0, |
683 | s->width * FONT_WIDTH, | |
684 | (s->height - 1) * FONT_HEIGHT); | |
685 | vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT, | |
686 | s->width * FONT_WIDTH, FONT_HEIGHT, | |
687 | color_table_rgb[0][s->t_attrib_default.bgcol]); | |
688 | s->update_x0 = 0; | |
689 | s->update_y0 = 0; | |
690 | s->update_x1 = s->width * FONT_WIDTH; | |
691 | s->update_y1 = s->height * FONT_HEIGHT; | |
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: | |
4083733d | 741 | s->t_attrib.fgcol = QEMU_COLOR_BLACK; |
6d6f7c28 PB |
742 | break; |
743 | case 31: | |
4083733d | 744 | s->t_attrib.fgcol = QEMU_COLOR_RED; |
6d6f7c28 PB |
745 | break; |
746 | case 32: | |
4083733d | 747 | s->t_attrib.fgcol = QEMU_COLOR_GREEN; |
6d6f7c28 PB |
748 | break; |
749 | case 33: | |
4083733d | 750 | s->t_attrib.fgcol = QEMU_COLOR_YELLOW; |
6d6f7c28 PB |
751 | break; |
752 | case 34: | |
4083733d | 753 | s->t_attrib.fgcol = QEMU_COLOR_BLUE; |
6d6f7c28 PB |
754 | break; |
755 | case 35: | |
4083733d | 756 | s->t_attrib.fgcol = QEMU_COLOR_MAGENTA; |
6d6f7c28 PB |
757 | break; |
758 | case 36: | |
4083733d | 759 | s->t_attrib.fgcol = QEMU_COLOR_CYAN; |
6d6f7c28 PB |
760 | break; |
761 | case 37: | |
4083733d | 762 | s->t_attrib.fgcol = QEMU_COLOR_WHITE; |
6d6f7c28 PB |
763 | break; |
764 | /* set background color */ | |
765 | case 40: | |
4083733d | 766 | s->t_attrib.bgcol = QEMU_COLOR_BLACK; |
6d6f7c28 PB |
767 | break; |
768 | case 41: | |
4083733d | 769 | s->t_attrib.bgcol = QEMU_COLOR_RED; |
6d6f7c28 PB |
770 | break; |
771 | case 42: | |
4083733d | 772 | s->t_attrib.bgcol = QEMU_COLOR_GREEN; |
6d6f7c28 PB |
773 | break; |
774 | case 43: | |
4083733d | 775 | s->t_attrib.bgcol = QEMU_COLOR_YELLOW; |
6d6f7c28 PB |
776 | break; |
777 | case 44: | |
4083733d | 778 | s->t_attrib.bgcol = QEMU_COLOR_BLUE; |
6d6f7c28 PB |
779 | break; |
780 | case 45: | |
4083733d | 781 | s->t_attrib.bgcol = QEMU_COLOR_MAGENTA; |
6d6f7c28 PB |
782 | break; |
783 | case 46: | |
4083733d | 784 | s->t_attrib.bgcol = QEMU_COLOR_CYAN; |
6d6f7c28 PB |
785 | break; |
786 | case 47: | |
4083733d | 787 | s->t_attrib.bgcol = QEMU_COLOR_WHITE; |
6d6f7c28 PB |
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; | |
5b8541c6 GH |
796 | if (x >= s->width) { |
797 | x = s->width - 1; | |
798 | } | |
adb47967 TS |
799 | TextCell *c = &s->cells[y1 * s->width + x]; |
800 | c->ch = ' '; | |
801 | c->t_attrib = s->t_attrib_default; | |
adb47967 TS |
802 | update_xy(s, x, y); |
803 | } | |
804 | ||
58aa7d8e RK |
805 | static void console_put_one(QemuConsole *s, int ch) |
806 | { | |
807 | TextCell *c; | |
808 | int y1; | |
809 | if (s->x >= s->width) { | |
810 | /* line wrap */ | |
811 | s->x = 0; | |
812 | console_put_lf(s); | |
813 | } | |
814 | y1 = (s->y_base + s->y) % s->total_height; | |
815 | c = &s->cells[y1 * s->width + s->x]; | |
816 | c->ch = ch; | |
817 | c->t_attrib = s->t_attrib; | |
818 | update_xy(s, s->x, s->y); | |
819 | s->x++; | |
820 | } | |
821 | ||
822 | static void console_respond_str(QemuConsole *s, const char *buf) | |
823 | { | |
824 | while (*buf) { | |
825 | console_put_one(s, *buf); | |
826 | buf++; | |
827 | } | |
828 | } | |
829 | ||
3eea5498 | 830 | /* set cursor, checking bounds */ |
76ffb0b4 | 831 | static void set_cursor(QemuConsole *s, int x, int y) |
3eea5498 IC |
832 | { |
833 | if (x < 0) { | |
834 | x = 0; | |
835 | } | |
836 | if (y < 0) { | |
837 | y = 0; | |
838 | } | |
839 | if (y >= s->height) { | |
840 | y = s->height - 1; | |
841 | } | |
842 | if (x >= s->width) { | |
843 | x = s->width - 1; | |
844 | } | |
845 | ||
846 | s->x = x; | |
847 | s->y = y; | |
848 | } | |
849 | ||
76ffb0b4 | 850 | static void console_putchar(QemuConsole *s, int ch) |
e7f0ad58 | 851 | { |
58aa7d8e | 852 | int i; |
adb47967 | 853 | int x, y; |
58aa7d8e | 854 | char response[40]; |
e7f0ad58 FB |
855 | |
856 | switch(s->state) { | |
857 | case TTY_STATE_NORM: | |
858 | switch(ch) { | |
6d6f7c28 | 859 | case '\r': /* carriage return */ |
e7f0ad58 FB |
860 | s->x = 0; |
861 | break; | |
6d6f7c28 | 862 | case '\n': /* newline */ |
e7f0ad58 FB |
863 | console_put_lf(s); |
864 | break; | |
6d6f7c28 | 865 | case '\b': /* backspace */ |
5fafdf24 | 866 | if (s->x > 0) |
e15d7371 | 867 | s->x--; |
6d6f7c28 PB |
868 | break; |
869 | case '\t': /* tabspace */ | |
870 | if (s->x + (8 - (s->x % 8)) > s->width) { | |
bd468840 | 871 | s->x = 0; |
6d6f7c28 PB |
872 | console_put_lf(s); |
873 | } else { | |
874 | s->x = s->x + (8 - (s->x % 8)); | |
875 | } | |
876 | break; | |
877 | case '\a': /* alert aka. bell */ | |
878 | /* TODO: has to be implemented */ | |
879 | break; | |
adb47967 TS |
880 | case 14: |
881 | /* SI (shift in), character set 0 (ignored) */ | |
882 | break; | |
883 | case 15: | |
884 | /* SO (shift out), character set 1 (ignored) */ | |
885 | break; | |
6d6f7c28 | 886 | case 27: /* esc (introducing an escape sequence) */ |
e7f0ad58 FB |
887 | s->state = TTY_STATE_ESC; |
888 | break; | |
889 | default: | |
58aa7d8e | 890 | console_put_one(s, ch); |
e7f0ad58 FB |
891 | break; |
892 | } | |
893 | break; | |
6d6f7c28 | 894 | case TTY_STATE_ESC: /* check if it is a terminal escape sequence */ |
e7f0ad58 FB |
895 | if (ch == '[') { |
896 | for(i=0;i<MAX_ESC_PARAMS;i++) | |
897 | s->esc_params[i] = 0; | |
898 | s->nb_esc_params = 0; | |
899 | s->state = TTY_STATE_CSI; | |
900 | } else { | |
901 | s->state = TTY_STATE_NORM; | |
902 | } | |
903 | break; | |
6d6f7c28 | 904 | case TTY_STATE_CSI: /* handle escape sequence parameters */ |
e7f0ad58 FB |
905 | if (ch >= '0' && ch <= '9') { |
906 | if (s->nb_esc_params < MAX_ESC_PARAMS) { | |
c10600af LE |
907 | int *param = &s->esc_params[s->nb_esc_params]; |
908 | int digit = (ch - '0'); | |
909 | ||
910 | *param = (*param <= (INT_MAX - digit) / 10) ? | |
911 | *param * 10 + digit : INT_MAX; | |
e7f0ad58 FB |
912 | } |
913 | } else { | |
3eea5498 IC |
914 | if (s->nb_esc_params < MAX_ESC_PARAMS) |
915 | s->nb_esc_params++; | |
7c336f9f | 916 | if (ch == ';' || ch == '?') { |
e7f0ad58 | 917 | break; |
7c336f9f | 918 | } |
5d28b0e9 SW |
919 | trace_console_putchar_csi(s->esc_params[0], s->esc_params[1], |
920 | ch, s->nb_esc_params); | |
e7f0ad58 FB |
921 | s->state = TTY_STATE_NORM; |
922 | switch(ch) { | |
adb47967 TS |
923 | case 'A': |
924 | /* move cursor up */ | |
925 | if (s->esc_params[0] == 0) { | |
926 | s->esc_params[0] = 1; | |
927 | } | |
3eea5498 | 928 | set_cursor(s, s->x, s->y - s->esc_params[0]); |
adb47967 TS |
929 | break; |
930 | case 'B': | |
931 | /* move cursor down */ | |
932 | if (s->esc_params[0] == 0) { | |
933 | s->esc_params[0] = 1; | |
934 | } | |
3eea5498 | 935 | set_cursor(s, s->x, s->y + s->esc_params[0]); |
e7f0ad58 FB |
936 | break; |
937 | case 'C': | |
adb47967 TS |
938 | /* move cursor right */ |
939 | if (s->esc_params[0] == 0) { | |
940 | s->esc_params[0] = 1; | |
941 | } | |
3eea5498 | 942 | set_cursor(s, s->x + s->esc_params[0], s->y); |
e7f0ad58 | 943 | break; |
adb47967 TS |
944 | case 'D': |
945 | /* move cursor left */ | |
946 | if (s->esc_params[0] == 0) { | |
947 | s->esc_params[0] = 1; | |
948 | } | |
3eea5498 | 949 | set_cursor(s, s->x - s->esc_params[0], s->y); |
adb47967 TS |
950 | break; |
951 | case 'G': | |
952 | /* move cursor to column */ | |
3eea5498 | 953 | set_cursor(s, s->esc_params[0] - 1, s->y); |
adb47967 TS |
954 | break; |
955 | case 'f': | |
956 | case 'H': | |
957 | /* move cursor to row, column */ | |
3eea5498 | 958 | set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1); |
adb47967 TS |
959 | break; |
960 | case 'J': | |
961 | switch (s->esc_params[0]) { | |
962 | case 0: | |
963 | /* clear to end of screen */ | |
964 | for (y = s->y; y < s->height; y++) { | |
965 | for (x = 0; x < s->width; x++) { | |
966 | if (y == s->y && x < s->x) { | |
967 | continue; | |
968 | } | |
969 | console_clear_xy(s, x, y); | |
970 | } | |
971 | } | |
972 | break; | |
973 | case 1: | |
974 | /* clear from beginning of screen */ | |
975 | for (y = 0; y <= s->y; y++) { | |
976 | for (x = 0; x < s->width; x++) { | |
977 | if (y == s->y && x > s->x) { | |
978 | break; | |
979 | } | |
980 | console_clear_xy(s, x, y); | |
981 | } | |
982 | } | |
983 | break; | |
984 | case 2: | |
985 | /* clear entire screen */ | |
986 | for (y = 0; y <= s->height; y++) { | |
987 | for (x = 0; x < s->width; x++) { | |
988 | console_clear_xy(s, x, y); | |
989 | } | |
990 | } | |
f94a950f | 991 | break; |
adb47967 | 992 | } |
95d8f9f4 | 993 | break; |
e7f0ad58 | 994 | case 'K': |
adb47967 TS |
995 | switch (s->esc_params[0]) { |
996 | case 0: | |
f94a950f MA |
997 | /* clear to eol */ |
998 | for(x = s->x; x < s->width; x++) { | |
adb47967 | 999 | console_clear_xy(s, x, s->y); |
f94a950f MA |
1000 | } |
1001 | break; | |
adb47967 TS |
1002 | case 1: |
1003 | /* clear from beginning of line */ | |
5b8541c6 | 1004 | for (x = 0; x <= s->x && x < s->width; x++) { |
adb47967 TS |
1005 | console_clear_xy(s, x, s->y); |
1006 | } | |
1007 | break; | |
1008 | case 2: | |
1009 | /* clear entire line */ | |
1010 | for(x = 0; x < s->width; x++) { | |
1011 | console_clear_xy(s, x, s->y); | |
1012 | } | |
f94a950f MA |
1013 | break; |
1014 | } | |
adb47967 TS |
1015 | break; |
1016 | case 'm': | |
f94a950f MA |
1017 | console_handle_escape(s); |
1018 | break; | |
adb47967 | 1019 | case 'n': |
58aa7d8e RK |
1020 | switch (s->esc_params[0]) { |
1021 | case 5: | |
1022 | /* report console status (always succeed)*/ | |
1023 | console_respond_str(s, "\033[0n"); | |
1024 | break; | |
1025 | case 6: | |
1026 | /* report cursor position */ | |
1027 | sprintf(response, "\033[%d;%dR", | |
1028 | (s->y_base + s->y) % s->total_height + 1, | |
1029 | s->x + 1); | |
1030 | console_respond_str(s, response); | |
1031 | break; | |
1032 | } | |
adb47967 TS |
1033 | break; |
1034 | case 's': | |
1035 | /* save cursor position */ | |
1036 | s->x_saved = s->x; | |
1037 | s->y_saved = s->y; | |
1038 | break; | |
1039 | case 'u': | |
1040 | /* restore cursor position */ | |
1041 | s->x = s->x_saved; | |
1042 | s->y = s->y_saved; | |
1043 | break; | |
1044 | default: | |
5d28b0e9 | 1045 | trace_console_putchar_unhandled(ch); |
adb47967 TS |
1046 | break; |
1047 | } | |
1048 | break; | |
e7f0ad58 FB |
1049 | } |
1050 | } | |
1051 | } | |
1052 | ||
1053 | void console_select(unsigned int index) | |
1054 | { | |
284d1c6b | 1055 | DisplayChangeListener *dcl; |
76ffb0b4 | 1056 | QemuConsole *s; |
6d6f7c28 | 1057 | |
437fe106 | 1058 | trace_console_select(index); |
284d1c6b | 1059 | s = qemu_console_lookup_by_index(index); |
e7f0ad58 | 1060 | if (s) { |
7d957bd8 | 1061 | DisplayState *ds = s->ds; |
bf1bed81 | 1062 | |
e7f0ad58 | 1063 | active_console = s; |
a93a4a22 | 1064 | if (ds->have_gfx) { |
284d1c6b GH |
1065 | QLIST_FOREACH(dcl, &ds->listeners, next) { |
1066 | if (dcl->con != NULL) { | |
1067 | continue; | |
1068 | } | |
1069 | if (dcl->ops->dpy_gfx_switch) { | |
1070 | dcl->ops->dpy_gfx_switch(dcl, s->surface); | |
1071 | } | |
1072 | } | |
2e5567c9 GH |
1073 | if (s->surface) { |
1074 | dpy_gfx_update(s, 0, 0, surface_width(s->surface), | |
1075 | surface_height(s->surface)); | |
1076 | } | |
a93a4a22 GH |
1077 | } |
1078 | if (ds->have_text) { | |
c78f7137 | 1079 | dpy_text_resize(s, s->width, s->height); |
68f00996 | 1080 | } |
aea7947c | 1081 | text_console_update_cursor(NULL); |
e7f0ad58 FB |
1082 | } |
1083 | } | |
1084 | ||
0ec7b3e7 MAL |
1085 | typedef struct VCChardev { |
1086 | Chardev parent; | |
41ac54b2 | 1087 | QemuConsole *console; |
0ec7b3e7 | 1088 | } VCChardev; |
41ac54b2 | 1089 | |
777357d7 MAL |
1090 | #define TYPE_CHARDEV_VC "chardev-vc" |
1091 | #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC) | |
1092 | ||
5bf5adae | 1093 | static int vc_chr_write(Chardev *chr, const uint8_t *buf, int len) |
e7f0ad58 | 1094 | { |
777357d7 | 1095 | VCChardev *drv = VC_CHARDEV(chr); |
41ac54b2 | 1096 | QemuConsole *s = drv->console; |
e7f0ad58 FB |
1097 | int i; |
1098 | ||
b68e956a MAL |
1099 | if (!s->ds) { |
1100 | return 0; | |
1101 | } | |
1102 | ||
14778c20 PB |
1103 | s->update_x0 = s->width * FONT_WIDTH; |
1104 | s->update_y0 = s->height * FONT_HEIGHT; | |
1105 | s->update_x1 = 0; | |
1106 | s->update_y1 = 0; | |
e7f0ad58 FB |
1107 | console_show_cursor(s, 0); |
1108 | for(i = 0; i < len; i++) { | |
1109 | console_putchar(s, buf[i]); | |
1110 | } | |
1111 | console_show_cursor(s, 1); | |
a93a4a22 | 1112 | if (s->ds->have_gfx && s->update_x0 < s->update_x1) { |
c78f7137 | 1113 | dpy_gfx_update(s, s->update_x0, s->update_y0, |
a93a4a22 GH |
1114 | s->update_x1 - s->update_x0, |
1115 | s->update_y1 - s->update_y0); | |
14778c20 | 1116 | } |
e7f0ad58 FB |
1117 | return len; |
1118 | } | |
1119 | ||
e15d7371 FB |
1120 | static void kbd_send_chars(void *opaque) |
1121 | { | |
76ffb0b4 | 1122 | QemuConsole *s = opaque; |
e15d7371 FB |
1123 | int len; |
1124 | uint8_t buf[16]; | |
3b46e624 | 1125 | |
909cda12 | 1126 | len = qemu_chr_be_can_write(s->chr); |
e15d7371 FB |
1127 | if (len > s->out_fifo.count) |
1128 | len = s->out_fifo.count; | |
1129 | if (len > 0) { | |
1130 | if (len > sizeof(buf)) | |
1131 | len = sizeof(buf); | |
1132 | qemu_fifo_read(&s->out_fifo, buf, len); | |
fa5efccb | 1133 | qemu_chr_be_write(s->chr, buf, len); |
e15d7371 FB |
1134 | } |
1135 | /* characters are pending: we send them a bit later (XXX: | |
1136 | horrible, should change char device API) */ | |
1137 | if (s->out_fifo.count > 0) { | |
bc72ad67 | 1138 | timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1); |
e15d7371 FB |
1139 | } |
1140 | } | |
1141 | ||
e7f0ad58 | 1142 | /* called when an ascii key is pressed */ |
3f9a6e85 | 1143 | void kbd_put_keysym_console(QemuConsole *s, int keysym) |
e7f0ad58 | 1144 | { |
e7f0ad58 | 1145 | uint8_t buf[16], *q; |
a4afa548 | 1146 | CharBackend *be; |
e7f0ad58 FB |
1147 | int c; |
1148 | ||
af3a9031 | 1149 | if (!s || (s->console_type == GRAPHIC_CONSOLE)) |
e7f0ad58 FB |
1150 | return; |
1151 | ||
1152 | switch(keysym) { | |
1153 | case QEMU_KEY_CTRL_UP: | |
81c0d5a6 | 1154 | console_scroll(s, -1); |
e7f0ad58 FB |
1155 | break; |
1156 | case QEMU_KEY_CTRL_DOWN: | |
81c0d5a6 | 1157 | console_scroll(s, 1); |
e7f0ad58 FB |
1158 | break; |
1159 | case QEMU_KEY_CTRL_PAGEUP: | |
81c0d5a6 | 1160 | console_scroll(s, -10); |
e7f0ad58 FB |
1161 | break; |
1162 | case QEMU_KEY_CTRL_PAGEDOWN: | |
81c0d5a6 | 1163 | console_scroll(s, 10); |
e7f0ad58 FB |
1164 | break; |
1165 | default: | |
e15d7371 FB |
1166 | /* convert the QEMU keysym to VT100 key string */ |
1167 | q = buf; | |
1168 | if (keysym >= 0xe100 && keysym <= 0xe11f) { | |
1169 | *q++ = '\033'; | |
1170 | *q++ = '['; | |
1171 | c = keysym - 0xe100; | |
1172 | if (c >= 10) | |
1173 | *q++ = '0' + (c / 10); | |
1174 | *q++ = '0' + (c % 10); | |
1175 | *q++ = '~'; | |
1176 | } else if (keysym >= 0xe120 && keysym <= 0xe17f) { | |
1177 | *q++ = '\033'; | |
1178 | *q++ = '['; | |
1179 | *q++ = keysym & 0xff; | |
4104833f | 1180 | } else if (s->echo && (keysym == '\r' || keysym == '\n')) { |
5bf5adae | 1181 | vc_chr_write(s->chr, (const uint8_t *) "\r", 1); |
4104833f | 1182 | *q++ = '\n'; |
e15d7371 | 1183 | } else { |
4104833f PB |
1184 | *q++ = keysym; |
1185 | } | |
1186 | if (s->echo) { | |
5bf5adae | 1187 | vc_chr_write(s->chr, buf, q - buf); |
e15d7371 | 1188 | } |
a4afa548 MAL |
1189 | be = s->chr->be; |
1190 | if (be && be->chr_read) { | |
e15d7371 FB |
1191 | qemu_fifo_write(&s->out_fifo, buf, q - buf); |
1192 | kbd_send_chars(s); | |
e7f0ad58 FB |
1193 | } |
1194 | break; | |
1195 | } | |
1196 | } | |
1197 | ||
7fb1cf16 | 1198 | static const int qcode_to_keysym[Q_KEY_CODE__MAX] = { |
50ef4679 GH |
1199 | [Q_KEY_CODE_UP] = QEMU_KEY_UP, |
1200 | [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN, | |
1201 | [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT, | |
1202 | [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT, | |
1203 | [Q_KEY_CODE_HOME] = QEMU_KEY_HOME, | |
1204 | [Q_KEY_CODE_END] = QEMU_KEY_END, | |
1205 | [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP, | |
1206 | [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN, | |
1207 | [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE, | |
344aa283 | 1208 | [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE, |
50ef4679 GH |
1209 | }; |
1210 | ||
da024b1e GH |
1211 | static const int ctrl_qcode_to_keysym[Q_KEY_CODE__MAX] = { |
1212 | [Q_KEY_CODE_UP] = QEMU_KEY_CTRL_UP, | |
1213 | [Q_KEY_CODE_DOWN] = QEMU_KEY_CTRL_DOWN, | |
1214 | [Q_KEY_CODE_RIGHT] = QEMU_KEY_CTRL_RIGHT, | |
1215 | [Q_KEY_CODE_LEFT] = QEMU_KEY_CTRL_LEFT, | |
1216 | [Q_KEY_CODE_HOME] = QEMU_KEY_CTRL_HOME, | |
1217 | [Q_KEY_CODE_END] = QEMU_KEY_CTRL_END, | |
1218 | [Q_KEY_CODE_PGUP] = QEMU_KEY_CTRL_PAGEUP, | |
1219 | [Q_KEY_CODE_PGDN] = QEMU_KEY_CTRL_PAGEDOWN, | |
1220 | }; | |
1221 | ||
1222 | bool kbd_put_qcode_console(QemuConsole *s, int qcode, bool ctrl) | |
50ef4679 GH |
1223 | { |
1224 | int keysym; | |
1225 | ||
da024b1e | 1226 | keysym = ctrl ? ctrl_qcode_to_keysym[qcode] : qcode_to_keysym[qcode]; |
50ef4679 GH |
1227 | if (keysym == 0) { |
1228 | return false; | |
1229 | } | |
1230 | kbd_put_keysym_console(s, keysym); | |
1231 | return true; | |
1232 | } | |
1233 | ||
bdef9724 GH |
1234 | void kbd_put_string_console(QemuConsole *s, const char *str, int len) |
1235 | { | |
1236 | int i; | |
1237 | ||
1238 | for (i = 0; i < len && str[i]; i++) { | |
1239 | kbd_put_keysym_console(s, str[i]); | |
1240 | } | |
1241 | } | |
1242 | ||
3f9a6e85 GH |
1243 | void kbd_put_keysym(int keysym) |
1244 | { | |
1245 | kbd_put_keysym_console(active_console, keysym); | |
1246 | } | |
1247 | ||
4d3b6f6e AZ |
1248 | static void text_console_invalidate(void *opaque) |
1249 | { | |
76ffb0b4 | 1250 | QemuConsole *s = (QemuConsole *) opaque; |
1562e531 GH |
1251 | |
1252 | if (s->ds->have_text && s->console_type == TEXT_CONSOLE) { | |
68f00996 AL |
1253 | text_console_resize(s); |
1254 | } | |
4d3b6f6e AZ |
1255 | console_refresh(s); |
1256 | } | |
1257 | ||
c227f099 | 1258 | static void text_console_update(void *opaque, console_ch_t *chardata) |
4d3b6f6e | 1259 | { |
76ffb0b4 | 1260 | QemuConsole *s = (QemuConsole *) opaque; |
4d3b6f6e AZ |
1261 | int i, j, src; |
1262 | ||
1263 | if (s->text_x[0] <= s->text_x[1]) { | |
1264 | src = (s->y_base + s->text_y[0]) * s->width; | |
1265 | chardata += s->text_y[0] * s->width; | |
1266 | for (i = s->text_y[0]; i <= s->text_y[1]; i ++) | |
4083733d OH |
1267 | for (j = 0; j < s->width; j++, src++) { |
1268 | console_write_ch(chardata ++, | |
1269 | ATTR2CHTYPE(s->cells[src].ch, | |
1270 | s->cells[src].t_attrib.fgcol, | |
1271 | s->cells[src].t_attrib.bgcol, | |
1272 | s->cells[src].t_attrib.bold)); | |
1273 | } | |
c78f7137 | 1274 | dpy_text_update(s, s->text_x[0], s->text_y[0], |
a93a4a22 | 1275 | s->text_x[1] - s->text_x[0], i - s->text_y[0]); |
4d3b6f6e AZ |
1276 | s->text_x[0] = s->width; |
1277 | s->text_y[0] = s->height; | |
1278 | s->text_x[1] = 0; | |
1279 | s->text_y[1] = 0; | |
1280 | } | |
1281 | if (s->cursor_invalidate) { | |
c78f7137 | 1282 | dpy_text_cursor(s, s->x, s->y); |
4d3b6f6e AZ |
1283 | s->cursor_invalidate = 0; |
1284 | } | |
1285 | } | |
1286 | ||
afff2b15 KB |
1287 | static QemuConsole *new_console(DisplayState *ds, console_type_t console_type, |
1288 | uint32_t head) | |
e7f0ad58 | 1289 | { |
95be0669 | 1290 | Object *obj; |
76ffb0b4 | 1291 | QemuConsole *s; |
95219897 | 1292 | int i; |
e7f0ad58 | 1293 | |
95be0669 GH |
1294 | obj = object_new(TYPE_QEMU_CONSOLE); |
1295 | s = QEMU_CONSOLE(obj); | |
afff2b15 | 1296 | s->head = head; |
aa2beaa1 | 1297 | object_property_add_link(obj, "device", TYPE_DEVICE, |
9561fda8 | 1298 | (Object **)&s->device, |
39f72ef9 | 1299 | object_property_allow_set_link, |
d2623129 | 1300 | OBJ_PROP_LINK_STRONG); |
836e1b38 | 1301 | object_property_add_uint32_ptr(obj, "head", &s->head, |
d2623129 | 1302 | OBJ_PROP_FLAG_READ); |
aa2beaa1 | 1303 | |
af3a9031 TS |
1304 | if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) && |
1305 | (console_type == GRAPHIC_CONSOLE))) { | |
e7f0ad58 | 1306 | active_console = s; |
af3a9031 | 1307 | } |
e7f0ad58 | 1308 | s->ds = ds; |
af3a9031 | 1309 | s->console_type = console_type; |
a1d2db08 | 1310 | |
cd6cd8fa GH |
1311 | if (QTAILQ_EMPTY(&consoles)) { |
1312 | s->index = 0; | |
1313 | QTAILQ_INSERT_TAIL(&consoles, s, next); | |
1314 | } else if (console_type != GRAPHIC_CONSOLE || qdev_hotplug) { | |
eae3eb3e | 1315 | QemuConsole *last = QTAILQ_LAST(&consoles); |
cd6cd8fa GH |
1316 | s->index = last->index + 1; |
1317 | QTAILQ_INSERT_TAIL(&consoles, s, next); | |
95219897 | 1318 | } else { |
9588d67e GH |
1319 | /* |
1320 | * HACK: Put graphical consoles before text consoles. | |
1321 | * | |
1322 | * Only do that for coldplugged devices. After initial device | |
1323 | * initialization we will not renumber the consoles any more. | |
1324 | */ | |
cd6cd8fa GH |
1325 | QemuConsole *c = QTAILQ_FIRST(&consoles); |
1326 | ||
1327 | while (QTAILQ_NEXT(c, next) != NULL && | |
1328 | c->console_type == GRAPHIC_CONSOLE) { | |
1329 | c = QTAILQ_NEXT(c, next); | |
1330 | } | |
1331 | if (c->console_type == GRAPHIC_CONSOLE) { | |
1332 | /* have no text consoles */ | |
1333 | s->index = c->index + 1; | |
1334 | QTAILQ_INSERT_AFTER(&consoles, c, s, next); | |
1335 | } else { | |
1336 | s->index = c->index; | |
1337 | QTAILQ_INSERT_BEFORE(c, s, next); | |
1338 | /* renumber text consoles */ | |
1339 | for (i = s->index + 1; c != NULL; c = QTAILQ_NEXT(c, next), i++) { | |
1340 | c->index = i; | |
1341 | } | |
95219897 | 1342 | } |
95219897 PB |
1343 | } |
1344 | return s; | |
1345 | } | |
1346 | ||
30f1e661 | 1347 | static void qemu_alloc_display(DisplaySurface *surface, int width, int height) |
ffe8b821 | 1348 | { |
69c77777 GH |
1349 | qemu_pixman_image_unref(surface->image); |
1350 | surface->image = NULL; | |
69c77777 | 1351 | |
30f1e661 | 1352 | surface->format = PIXMAN_x8r8g8b8; |
69c77777 GH |
1353 | surface->image = pixman_image_create_bits(surface->format, |
1354 | width, height, | |
30f1e661 | 1355 | NULL, width * 4); |
69c77777 GH |
1356 | assert(surface->image != NULL); |
1357 | ||
30f1e661 | 1358 | surface->flags = QEMU_ALLOCATED_FLAG; |
98b50080 PB |
1359 | } |
1360 | ||
da229ef3 | 1361 | DisplaySurface *qemu_create_displaysurface(int width, int height) |
537a4391 GH |
1362 | { |
1363 | DisplaySurface *surface = g_new0(DisplaySurface, 1); | |
da229ef3 GH |
1364 | |
1365 | trace_displaysurface_create(surface, width, height); | |
30f1e661 | 1366 | qemu_alloc_display(surface, width, height); |
537a4391 GH |
1367 | return surface; |
1368 | } | |
1369 | ||
30f1e661 GH |
1370 | DisplaySurface *qemu_create_displaysurface_from(int width, int height, |
1371 | pixman_format_code_t format, | |
1372 | int linesize, uint8_t *data) | |
98b50080 | 1373 | { |
69c77777 | 1374 | DisplaySurface *surface = g_new0(DisplaySurface, 1); |
98b50080 | 1375 | |
30f1e661 GH |
1376 | trace_displaysurface_create_from(surface, width, height, format); |
1377 | surface->format = format; | |
69c77777 GH |
1378 | surface->image = pixman_image_create_bits(surface->format, |
1379 | width, height, | |
1380 | (void *)data, linesize); | |
1381 | assert(surface->image != NULL); | |
1382 | ||
98b50080 PB |
1383 | return surface; |
1384 | } | |
1385 | ||
ca58b45f GH |
1386 | DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image) |
1387 | { | |
1388 | DisplaySurface *surface = g_new0(DisplaySurface, 1); | |
1389 | ||
1390 | trace_displaysurface_create_pixman(surface); | |
1391 | surface->format = pixman_image_get_format(image); | |
1392 | surface->image = pixman_image_ref(image); | |
1393 | ||
1394 | return surface; | |
1395 | } | |
1396 | ||
2e5567c9 GH |
1397 | DisplaySurface *qemu_create_message_surface(int w, int h, |
1398 | const char *msg) | |
d3002b04 | 1399 | { |
521a580d | 1400 | DisplaySurface *surface = qemu_create_displaysurface(w, h); |
4083733d OH |
1401 | pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK]; |
1402 | pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE]; | |
d3002b04 GH |
1403 | pixman_image_t *glyph; |
1404 | int len, x, y, i; | |
1405 | ||
1406 | len = strlen(msg); | |
521a580d GH |
1407 | x = (w / FONT_WIDTH - len) / 2; |
1408 | y = (h / FONT_HEIGHT - 1) / 2; | |
d3002b04 GH |
1409 | for (i = 0; i < len; i++) { |
1410 | glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]); | |
1411 | qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg, | |
1412 | x+i, y, FONT_WIDTH, FONT_HEIGHT); | |
1413 | qemu_pixman_image_unref(glyph); | |
1414 | } | |
1415 | return surface; | |
1416 | } | |
1417 | ||
da229ef3 | 1418 | void qemu_free_displaysurface(DisplaySurface *surface) |
98b50080 | 1419 | { |
da229ef3 | 1420 | if (surface == NULL) { |
98b50080 | 1421 | return; |
187cd1d9 | 1422 | } |
da229ef3 GH |
1423 | trace_displaysurface_free(surface); |
1424 | qemu_pixman_image_unref(surface->image); | |
1425 | g_free(surface); | |
98b50080 PB |
1426 | } |
1427 | ||
06020b95 GH |
1428 | bool console_has_gl(QemuConsole *con) |
1429 | { | |
1430 | return con->gl != NULL; | |
1431 | } | |
1432 | ||
4133fa71 GH |
1433 | bool console_has_gl_dmabuf(QemuConsole *con) |
1434 | { | |
1435 | return con->gl != NULL && con->gl->ops->dpy_gl_scanout_dmabuf != NULL; | |
1436 | } | |
1437 | ||
5209089f | 1438 | void register_displaychangelistener(DisplayChangeListener *dcl) |
7c20b4a3 | 1439 | { |
521a580d GH |
1440 | static const char nodev[] = |
1441 | "This VM has no graphic display device."; | |
d3002b04 | 1442 | static DisplaySurface *dummy; |
284d1c6b GH |
1443 | QemuConsole *con; |
1444 | ||
e0665c3b MAL |
1445 | assert(!dcl->ds); |
1446 | ||
06020b95 GH |
1447 | if (dcl->ops->dpy_gl_ctx_create) { |
1448 | /* display has opengl support */ | |
1449 | assert(dcl->con); | |
1450 | if (dcl->con->gl) { | |
1451 | fprintf(stderr, "can't register two opengl displays (%s, %s)\n", | |
1452 | dcl->ops->dpy_name, dcl->con->gl->ops->dpy_name); | |
1453 | exit(1); | |
1454 | } | |
1455 | dcl->con->gl = dcl; | |
1456 | } | |
1457 | ||
7c20b4a3 | 1458 | trace_displaychangelistener_register(dcl, dcl->ops->dpy_name); |
5209089f GH |
1459 | dcl->ds = get_alloc_displaystate(); |
1460 | QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next); | |
1461 | gui_setup_refresh(dcl->ds); | |
284d1c6b GH |
1462 | if (dcl->con) { |
1463 | dcl->con->dcls++; | |
1464 | con = dcl->con; | |
1465 | } else { | |
1466 | con = active_console; | |
1467 | } | |
d3002b04 GH |
1468 | if (dcl->ops->dpy_gfx_switch) { |
1469 | if (con) { | |
1470 | dcl->ops->dpy_gfx_switch(dcl, con->surface); | |
1471 | } else { | |
1472 | if (!dummy) { | |
521a580d | 1473 | dummy = qemu_create_message_surface(640, 480, nodev); |
d3002b04 GH |
1474 | } |
1475 | dcl->ops->dpy_gfx_switch(dcl, dummy); | |
1476 | } | |
7c20b4a3 | 1477 | } |
aea7947c | 1478 | text_console_update_cursor(NULL); |
7c20b4a3 GH |
1479 | } |
1480 | ||
0f7b2864 GH |
1481 | void update_displaychangelistener(DisplayChangeListener *dcl, |
1482 | uint64_t interval) | |
1483 | { | |
1484 | DisplayState *ds = dcl->ds; | |
1485 | ||
1486 | dcl->update_interval = interval; | |
1487 | if (!ds->refreshing && ds->update_interval > interval) { | |
bc72ad67 | 1488 | timer_mod(ds->gui_timer, ds->last_update + interval); |
0f7b2864 GH |
1489 | } |
1490 | } | |
1491 | ||
7c20b4a3 GH |
1492 | void unregister_displaychangelistener(DisplayChangeListener *dcl) |
1493 | { | |
1494 | DisplayState *ds = dcl->ds; | |
1495 | trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name); | |
284d1c6b GH |
1496 | if (dcl->con) { |
1497 | dcl->con->dcls--; | |
1498 | } | |
7c20b4a3 | 1499 | QLIST_REMOVE(dcl, next); |
777c5f1e | 1500 | dcl->ds = NULL; |
7c20b4a3 GH |
1501 | gui_setup_refresh(ds); |
1502 | } | |
1503 | ||
cf1ecc82 GH |
1504 | static void dpy_set_ui_info_timer(void *opaque) |
1505 | { | |
1506 | QemuConsole *con = opaque; | |
1507 | ||
1508 | con->hw_ops->ui_info(con->hw, con->head, &con->ui_info); | |
1509 | } | |
1510 | ||
b7fb49f0 GH |
1511 | bool dpy_ui_info_supported(QemuConsole *con) |
1512 | { | |
1513 | return con->hw_ops->ui_info != NULL; | |
1514 | } | |
1515 | ||
6f90f3d7 GH |
1516 | int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info) |
1517 | { | |
1518 | assert(con != NULL); | |
1185fde4 | 1519 | |
b7fb49f0 | 1520 | if (!dpy_ui_info_supported(con)) { |
cf1ecc82 | 1521 | return -1; |
6f90f3d7 | 1522 | } |
1185fde4 GH |
1523 | if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) { |
1524 | /* nothing changed -- ignore */ | |
1525 | return 0; | |
1526 | } | |
cf1ecc82 GH |
1527 | |
1528 | /* | |
1529 | * Typically we get a flood of these as the user resizes the window. | |
1530 | * Wait until the dust has settled (one second without updates), then | |
1531 | * go notify the guest. | |
1532 | */ | |
1185fde4 | 1533 | con->ui_info = *info; |
cf1ecc82 GH |
1534 | timer_mod(con->ui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000); |
1535 | return 0; | |
6f90f3d7 GH |
1536 | } |
1537 | ||
c78f7137 | 1538 | void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h) |
7c20b4a3 | 1539 | { |
c78f7137 | 1540 | DisplayState *s = con->ds; |
284d1c6b | 1541 | DisplayChangeListener *dcl; |
06020b95 GH |
1542 | int width = w; |
1543 | int height = h; | |
7c20b4a3 | 1544 | |
06020b95 GH |
1545 | if (con->surface) { |
1546 | width = surface_width(con->surface); | |
1547 | height = surface_height(con->surface); | |
1548 | } | |
7c20b4a3 GH |
1549 | x = MAX(x, 0); |
1550 | y = MAX(y, 0); | |
1551 | x = MIN(x, width); | |
1552 | y = MIN(y, height); | |
1553 | w = MIN(w, width - x); | |
1554 | h = MIN(h, height - y); | |
1555 | ||
81c0d5a6 | 1556 | if (!qemu_console_is_visible(con)) { |
321f048d GH |
1557 | return; |
1558 | } | |
7c20b4a3 | 1559 | QLIST_FOREACH(dcl, &s->listeners, next) { |
284d1c6b GH |
1560 | if (con != (dcl->con ? dcl->con : active_console)) { |
1561 | continue; | |
1562 | } | |
7c20b4a3 | 1563 | if (dcl->ops->dpy_gfx_update) { |
bc2ed970 | 1564 | dcl->ops->dpy_gfx_update(dcl, x, y, w, h); |
7c20b4a3 GH |
1565 | } |
1566 | } | |
1567 | } | |
1568 | ||
7cd0afe6 TZ |
1569 | void dpy_gfx_update_full(QemuConsole *con) |
1570 | { | |
1571 | if (!con->surface) { | |
1572 | return; | |
1573 | } | |
1574 | dpy_gfx_update(con, 0, 0, | |
1575 | surface_width(con->surface), | |
1576 | surface_height(con->surface)); | |
1577 | } | |
1578 | ||
321f048d GH |
1579 | void dpy_gfx_replace_surface(QemuConsole *con, |
1580 | DisplaySurface *surface) | |
1581 | { | |
1582 | DisplayState *s = con->ds; | |
1583 | DisplaySurface *old_surface = con->surface; | |
284d1c6b | 1584 | DisplayChangeListener *dcl; |
321f048d | 1585 | |
15400086 | 1586 | assert(old_surface != surface || surface == NULL); |
6905b934 | 1587 | |
321f048d | 1588 | con->surface = surface; |
284d1c6b GH |
1589 | QLIST_FOREACH(dcl, &s->listeners, next) { |
1590 | if (con != (dcl->con ? dcl->con : active_console)) { | |
1591 | continue; | |
1592 | } | |
1593 | if (dcl->ops->dpy_gfx_switch) { | |
1594 | dcl->ops->dpy_gfx_switch(dcl, surface); | |
1595 | } | |
321f048d | 1596 | } |
da229ef3 | 1597 | qemu_free_displaysurface(old_surface); |
7c20b4a3 GH |
1598 | } |
1599 | ||
49743df3 BH |
1600 | bool dpy_gfx_check_format(QemuConsole *con, |
1601 | pixman_format_code_t format) | |
1602 | { | |
1603 | DisplayChangeListener *dcl; | |
1604 | DisplayState *s = con->ds; | |
1605 | ||
1606 | QLIST_FOREACH(dcl, &s->listeners, next) { | |
1607 | if (dcl->con && dcl->con != con) { | |
1608 | /* dcl bound to another console -> skip */ | |
1609 | continue; | |
1610 | } | |
1611 | if (dcl->ops->dpy_gfx_check_format) { | |
1612 | if (!dcl->ops->dpy_gfx_check_format(dcl, format)) { | |
1613 | return false; | |
1614 | } | |
1615 | } else { | |
1616 | /* default is to whitelist native 32 bpp only */ | |
1617 | if (format != qemu_default_pixman_format(32, true)) { | |
1618 | return false; | |
1619 | } | |
1620 | } | |
1621 | } | |
1622 | return true; | |
1623 | } | |
1624 | ||
6075137d | 1625 | static void dpy_refresh(DisplayState *s) |
7c20b4a3 | 1626 | { |
284d1c6b GH |
1627 | DisplayChangeListener *dcl; |
1628 | ||
7c20b4a3 GH |
1629 | QLIST_FOREACH(dcl, &s->listeners, next) { |
1630 | if (dcl->ops->dpy_refresh) { | |
3f8f1313 | 1631 | dcl->ops->dpy_refresh(dcl); |
7c20b4a3 GH |
1632 | } |
1633 | } | |
1634 | } | |
1635 | ||
c78f7137 | 1636 | void dpy_text_cursor(QemuConsole *con, int x, int y) |
7c20b4a3 | 1637 | { |
c78f7137 | 1638 | DisplayState *s = con->ds; |
284d1c6b | 1639 | DisplayChangeListener *dcl; |
321f048d | 1640 | |
81c0d5a6 | 1641 | if (!qemu_console_is_visible(con)) { |
321f048d GH |
1642 | return; |
1643 | } | |
7c20b4a3 | 1644 | QLIST_FOREACH(dcl, &s->listeners, next) { |
284d1c6b GH |
1645 | if (con != (dcl->con ? dcl->con : active_console)) { |
1646 | continue; | |
1647 | } | |
7c20b4a3 | 1648 | if (dcl->ops->dpy_text_cursor) { |
bc2ed970 | 1649 | dcl->ops->dpy_text_cursor(dcl, x, y); |
7c20b4a3 GH |
1650 | } |
1651 | } | |
1652 | } | |
1653 | ||
c78f7137 | 1654 | void dpy_text_update(QemuConsole *con, int x, int y, int w, int h) |
7c20b4a3 | 1655 | { |
c78f7137 | 1656 | DisplayState *s = con->ds; |
284d1c6b | 1657 | DisplayChangeListener *dcl; |
321f048d | 1658 | |
81c0d5a6 | 1659 | if (!qemu_console_is_visible(con)) { |
321f048d GH |
1660 | return; |
1661 | } | |
7c20b4a3 | 1662 | QLIST_FOREACH(dcl, &s->listeners, next) { |
284d1c6b GH |
1663 | if (con != (dcl->con ? dcl->con : active_console)) { |
1664 | continue; | |
1665 | } | |
7c20b4a3 | 1666 | if (dcl->ops->dpy_text_update) { |
bc2ed970 | 1667 | dcl->ops->dpy_text_update(dcl, x, y, w, h); |
7c20b4a3 GH |
1668 | } |
1669 | } | |
1670 | } | |
1671 | ||
c78f7137 | 1672 | void dpy_text_resize(QemuConsole *con, int w, int h) |
7c20b4a3 | 1673 | { |
c78f7137 | 1674 | DisplayState *s = con->ds; |
9425c004 | 1675 | DisplayChangeListener *dcl; |
321f048d | 1676 | |
81c0d5a6 | 1677 | if (!qemu_console_is_visible(con)) { |
321f048d GH |
1678 | return; |
1679 | } | |
7c20b4a3 | 1680 | QLIST_FOREACH(dcl, &s->listeners, next) { |
284d1c6b GH |
1681 | if (con != (dcl->con ? dcl->con : active_console)) { |
1682 | continue; | |
1683 | } | |
7c20b4a3 | 1684 | if (dcl->ops->dpy_text_resize) { |
bc2ed970 | 1685 | dcl->ops->dpy_text_resize(dcl, w, h); |
7c20b4a3 GH |
1686 | } |
1687 | } | |
1688 | } | |
1689 | ||
c78f7137 | 1690 | void dpy_mouse_set(QemuConsole *con, int x, int y, int on) |
7c20b4a3 | 1691 | { |
c78f7137 | 1692 | DisplayState *s = con->ds; |
284d1c6b | 1693 | DisplayChangeListener *dcl; |
321f048d | 1694 | |
81c0d5a6 | 1695 | if (!qemu_console_is_visible(con)) { |
321f048d GH |
1696 | return; |
1697 | } | |
7c20b4a3 | 1698 | QLIST_FOREACH(dcl, &s->listeners, next) { |
284d1c6b GH |
1699 | if (con != (dcl->con ? dcl->con : active_console)) { |
1700 | continue; | |
1701 | } | |
7c20b4a3 | 1702 | if (dcl->ops->dpy_mouse_set) { |
bc2ed970 | 1703 | dcl->ops->dpy_mouse_set(dcl, x, y, on); |
7c20b4a3 GH |
1704 | } |
1705 | } | |
1706 | } | |
1707 | ||
c78f7137 | 1708 | void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor) |
7c20b4a3 | 1709 | { |
c78f7137 | 1710 | DisplayState *s = con->ds; |
284d1c6b | 1711 | DisplayChangeListener *dcl; |
321f048d | 1712 | |
81c0d5a6 | 1713 | if (!qemu_console_is_visible(con)) { |
321f048d GH |
1714 | return; |
1715 | } | |
7c20b4a3 | 1716 | QLIST_FOREACH(dcl, &s->listeners, next) { |
284d1c6b GH |
1717 | if (con != (dcl->con ? dcl->con : active_console)) { |
1718 | continue; | |
1719 | } | |
7c20b4a3 | 1720 | if (dcl->ops->dpy_cursor_define) { |
bc2ed970 | 1721 | dcl->ops->dpy_cursor_define(dcl, cursor); |
7c20b4a3 GH |
1722 | } |
1723 | } | |
1724 | } | |
1725 | ||
c78f7137 | 1726 | bool dpy_cursor_define_supported(QemuConsole *con) |
7c20b4a3 | 1727 | { |
c78f7137 | 1728 | DisplayState *s = con->ds; |
284d1c6b GH |
1729 | DisplayChangeListener *dcl; |
1730 | ||
7c20b4a3 GH |
1731 | QLIST_FOREACH(dcl, &s->listeners, next) { |
1732 | if (dcl->ops->dpy_cursor_define) { | |
1733 | return true; | |
1734 | } | |
1735 | } | |
1736 | return false; | |
1737 | } | |
1738 | ||
06020b95 GH |
1739 | QEMUGLContext dpy_gl_ctx_create(QemuConsole *con, |
1740 | struct QEMUGLParams *qparams) | |
1741 | { | |
1742 | assert(con->gl); | |
1743 | return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams); | |
1744 | } | |
1745 | ||
1746 | void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx) | |
1747 | { | |
1748 | assert(con->gl); | |
1749 | con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx); | |
1750 | } | |
1751 | ||
1752 | int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx) | |
1753 | { | |
1754 | assert(con->gl); | |
1755 | return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx); | |
1756 | } | |
1757 | ||
1758 | QEMUGLContext dpy_gl_ctx_get_current(QemuConsole *con) | |
1759 | { | |
1760 | assert(con->gl); | |
1761 | return con->gl->ops->dpy_gl_ctx_get_current(con->gl); | |
1762 | } | |
1763 | ||
eaa92c76 GH |
1764 | void dpy_gl_scanout_disable(QemuConsole *con) |
1765 | { | |
1766 | assert(con->gl); | |
1767 | if (con->gl->ops->dpy_gl_scanout_disable) { | |
1768 | con->gl->ops->dpy_gl_scanout_disable(con->gl); | |
1769 | } else { | |
1770 | con->gl->ops->dpy_gl_scanout_texture(con->gl, 0, false, 0, 0, | |
1771 | 0, 0, 0, 0); | |
1772 | } | |
1773 | } | |
1774 | ||
f4c36bda GH |
1775 | void dpy_gl_scanout_texture(QemuConsole *con, |
1776 | uint32_t backing_id, | |
1777 | bool backing_y_0_top, | |
1778 | uint32_t backing_width, | |
1779 | uint32_t backing_height, | |
1780 | uint32_t x, uint32_t y, | |
1781 | uint32_t width, uint32_t height) | |
06020b95 GH |
1782 | { |
1783 | assert(con->gl); | |
f4c36bda GH |
1784 | con->gl->ops->dpy_gl_scanout_texture(con->gl, backing_id, |
1785 | backing_y_0_top, | |
1786 | backing_width, backing_height, | |
1787 | x, y, width, height); | |
06020b95 GH |
1788 | } |
1789 | ||
4133fa71 GH |
1790 | void dpy_gl_scanout_dmabuf(QemuConsole *con, |
1791 | QemuDmaBuf *dmabuf) | |
1792 | { | |
1793 | assert(con->gl); | |
1794 | con->gl->ops->dpy_gl_scanout_dmabuf(con->gl, dmabuf); | |
1795 | } | |
1796 | ||
6e1f2cb5 GH |
1797 | void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf, |
1798 | bool have_hot, uint32_t hot_x, uint32_t hot_y) | |
4133fa71 GH |
1799 | { |
1800 | assert(con->gl); | |
1801 | ||
1802 | if (con->gl->ops->dpy_gl_cursor_dmabuf) { | |
6e1f2cb5 GH |
1803 | con->gl->ops->dpy_gl_cursor_dmabuf(con->gl, dmabuf, |
1804 | have_hot, hot_x, hot_y); | |
1805 | } | |
1806 | } | |
1807 | ||
1808 | void dpy_gl_cursor_position(QemuConsole *con, | |
1809 | uint32_t pos_x, uint32_t pos_y) | |
1810 | { | |
1811 | assert(con->gl); | |
1812 | ||
1813 | if (con->gl->ops->dpy_gl_cursor_position) { | |
1814 | con->gl->ops->dpy_gl_cursor_position(con->gl, pos_x, pos_y); | |
4133fa71 GH |
1815 | } |
1816 | } | |
1817 | ||
1818 | void dpy_gl_release_dmabuf(QemuConsole *con, | |
1819 | QemuDmaBuf *dmabuf) | |
1820 | { | |
1821 | assert(con->gl); | |
1822 | ||
1823 | if (con->gl->ops->dpy_gl_release_dmabuf) { | |
1824 | con->gl->ops->dpy_gl_release_dmabuf(con->gl, dmabuf); | |
1825 | } | |
1826 | } | |
1827 | ||
06020b95 GH |
1828 | void dpy_gl_update(QemuConsole *con, |
1829 | uint32_t x, uint32_t y, uint32_t w, uint32_t h) | |
1830 | { | |
1831 | assert(con->gl); | |
1832 | con->gl->ops->dpy_gl_update(con->gl, x, y, w, h); | |
1833 | } | |
1834 | ||
98b50080 PB |
1835 | /***********************************************************/ |
1836 | /* register display */ | |
1837 | ||
64840c66 GH |
1838 | /* console.c internal use only */ |
1839 | static DisplayState *get_alloc_displaystate(void) | |
98b50080 | 1840 | { |
64840c66 GH |
1841 | if (!display_state) { |
1842 | display_state = g_new0(DisplayState, 1); | |
aea7947c GH |
1843 | cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME, |
1844 | text_console_update_cursor, NULL); | |
64840c66 GH |
1845 | } |
1846 | return display_state; | |
98b50080 PB |
1847 | } |
1848 | ||
64840c66 GH |
1849 | /* |
1850 | * Called by main(), after creating QemuConsoles | |
1851 | * and before initializing ui (sdl/vnc/...). | |
1852 | */ | |
1853 | DisplayState *init_displaystate(void) | |
98b50080 | 1854 | { |
43f420f8 | 1855 | gchar *name; |
cd6cd8fa | 1856 | QemuConsole *con; |
64840c66 | 1857 | |
333cb18f | 1858 | get_alloc_displaystate(); |
cd6cd8fa GH |
1859 | QTAILQ_FOREACH(con, &consoles, next) { |
1860 | if (con->console_type != GRAPHIC_CONSOLE && | |
1861 | con->ds == NULL) { | |
1862 | text_console_do_init(con->chr, display_state); | |
64840c66 | 1863 | } |
43f420f8 GH |
1864 | |
1865 | /* Hook up into the qom tree here (not in new_console()), once | |
1866 | * all QemuConsoles are created and the order / numbering | |
1867 | * doesn't change any more */ | |
cd6cd8fa | 1868 | name = g_strdup_printf("console[%d]", con->index); |
43f420f8 | 1869 | object_property_add_child(container_get(object_get_root(), "/backend"), |
d2623129 | 1870 | name, OBJECT(con)); |
43f420f8 | 1871 | g_free(name); |
64840c66 GH |
1872 | } |
1873 | ||
98b50080 PB |
1874 | return display_state; |
1875 | } | |
1876 | ||
1c1f9498 GH |
1877 | void graphic_console_set_hwops(QemuConsole *con, |
1878 | const GraphicHwOps *hw_ops, | |
1879 | void *opaque) | |
1880 | { | |
1881 | con->hw_ops = hw_ops; | |
1882 | con->hw = opaque; | |
1883 | } | |
1884 | ||
5643706a | 1885 | QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head, |
aa2beaa1 | 1886 | const GraphicHwOps *hw_ops, |
c78f7137 | 1887 | void *opaque) |
95219897 | 1888 | { |
521a580d GH |
1889 | static const char noinit[] = |
1890 | "Guest has not initialized the display (yet)."; | |
64840c66 GH |
1891 | int width = 640; |
1892 | int height = 480; | |
76ffb0b4 | 1893 | QemuConsole *s; |
3023f332 | 1894 | DisplayState *ds; |
9588d67e | 1895 | DisplaySurface *surface; |
f0f2f976 | 1896 | |
64840c66 | 1897 | ds = get_alloc_displaystate(); |
9588d67e GH |
1898 | s = qemu_console_lookup_unused(); |
1899 | if (s) { | |
1900 | trace_console_gfx_reuse(s->index); | |
1901 | if (s->surface) { | |
1902 | width = surface_width(s->surface); | |
1903 | height = surface_height(s->surface); | |
1904 | } | |
1905 | } else { | |
1906 | trace_console_gfx_new(); | |
1907 | s = new_console(ds, GRAPHIC_CONSOLE, head); | |
1908 | s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, | |
1909 | dpy_set_ui_info_timer, s); | |
1910 | } | |
1c1f9498 | 1911 | graphic_console_set_hwops(s, hw_ops, opaque); |
aa2beaa1 | 1912 | if (dev) { |
5325cc34 | 1913 | object_property_set_link(OBJECT(s), "device", OBJECT(dev), |
afff2b15 | 1914 | &error_abort); |
aa2beaa1 | 1915 | } |
3023f332 | 1916 | |
9588d67e GH |
1917 | surface = qemu_create_message_surface(width, height, noinit); |
1918 | dpy_gfx_replace_surface(s, surface); | |
c78f7137 | 1919 | return s; |
e7f0ad58 FB |
1920 | } |
1921 | ||
9588d67e GH |
1922 | static const GraphicHwOps unused_ops = { |
1923 | /* no callbacks */ | |
1924 | }; | |
1925 | ||
1926 | void graphic_console_close(QemuConsole *con) | |
1927 | { | |
1928 | static const char unplugged[] = | |
1929 | "Guest display has been unplugged"; | |
1930 | DisplaySurface *surface; | |
1931 | int width = 640; | |
1932 | int height = 480; | |
1933 | ||
1934 | if (con->surface) { | |
1935 | width = surface_width(con->surface); | |
1936 | height = surface_height(con->surface); | |
1937 | } | |
1938 | ||
1939 | trace_console_gfx_close(con->index); | |
5325cc34 | 1940 | object_property_set_link(OBJECT(con), "device", NULL, &error_abort); |
9588d67e GH |
1941 | graphic_console_set_hwops(con, &unused_ops, NULL); |
1942 | ||
1943 | if (con->gl) { | |
1944 | dpy_gl_scanout_disable(con); | |
1945 | } | |
1946 | surface = qemu_create_message_surface(width, height, unplugged); | |
1947 | dpy_gfx_replace_surface(con, surface); | |
1948 | } | |
1949 | ||
284d1c6b GH |
1950 | QemuConsole *qemu_console_lookup_by_index(unsigned int index) |
1951 | { | |
cd6cd8fa GH |
1952 | QemuConsole *con; |
1953 | ||
1954 | QTAILQ_FOREACH(con, &consoles, next) { | |
1955 | if (con->index == index) { | |
1956 | return con; | |
1957 | } | |
284d1c6b | 1958 | } |
cd6cd8fa | 1959 | return NULL; |
284d1c6b GH |
1960 | } |
1961 | ||
5643706a | 1962 | QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head) |
14a93649 | 1963 | { |
cd6cd8fa | 1964 | QemuConsole *con; |
14a93649 | 1965 | Object *obj; |
5643706a | 1966 | uint32_t h; |
14a93649 | 1967 | |
cd6cd8fa GH |
1968 | QTAILQ_FOREACH(con, &consoles, next) { |
1969 | obj = object_property_get_link(OBJECT(con), | |
afff2b15 | 1970 | "device", &error_abort); |
5643706a GH |
1971 | if (DEVICE(obj) != dev) { |
1972 | continue; | |
1973 | } | |
cd6cd8fa | 1974 | h = object_property_get_uint(OBJECT(con), |
ad664c1d | 1975 | "head", &error_abort); |
5643706a GH |
1976 | if (h != head) { |
1977 | continue; | |
14a93649 | 1978 | } |
cd6cd8fa | 1979 | return con; |
14a93649 GH |
1980 | } |
1981 | return NULL; | |
1982 | } | |
1983 | ||
f2c1d54c GH |
1984 | QemuConsole *qemu_console_lookup_by_device_name(const char *device_id, |
1985 | uint32_t head, Error **errp) | |
1986 | { | |
1987 | DeviceState *dev; | |
1988 | QemuConsole *con; | |
1989 | ||
1990 | dev = qdev_find_recursive(sysbus_get_default(), device_id); | |
1991 | if (dev == NULL) { | |
1992 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, | |
1993 | "Device '%s' not found", device_id); | |
1994 | return NULL; | |
1995 | } | |
1996 | ||
1997 | con = qemu_console_lookup_by_device(dev, head); | |
1998 | if (con == NULL) { | |
1999 | error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole", | |
2000 | device_id, head); | |
2001 | return NULL; | |
2002 | } | |
2003 | ||
2004 | return con; | |
2005 | } | |
2006 | ||
9588d67e GH |
2007 | QemuConsole *qemu_console_lookup_unused(void) |
2008 | { | |
cd6cd8fa | 2009 | QemuConsole *con; |
9588d67e | 2010 | Object *obj; |
9588d67e | 2011 | |
cd6cd8fa GH |
2012 | QTAILQ_FOREACH(con, &consoles, next) { |
2013 | if (con->hw_ops != &unused_ops) { | |
9588d67e GH |
2014 | continue; |
2015 | } | |
cd6cd8fa | 2016 | obj = object_property_get_link(OBJECT(con), |
9588d67e GH |
2017 | "device", &error_abort); |
2018 | if (obj != NULL) { | |
2019 | continue; | |
2020 | } | |
cd6cd8fa | 2021 | return con; |
9588d67e GH |
2022 | } |
2023 | return NULL; | |
2024 | } | |
2025 | ||
81c0d5a6 | 2026 | bool qemu_console_is_visible(QemuConsole *con) |
e7f0ad58 | 2027 | { |
284d1c6b | 2028 | return (con == active_console) || (con->dcls > 0); |
e7f0ad58 FB |
2029 | } |
2030 | ||
81c0d5a6 | 2031 | bool qemu_console_is_graphic(QemuConsole *con) |
c21bbcfa | 2032 | { |
81c0d5a6 GH |
2033 | if (con == NULL) { |
2034 | con = active_console; | |
2035 | } | |
2036 | return con && (con->console_type == GRAPHIC_CONSOLE); | |
2037 | } | |
2038 | ||
2039 | bool qemu_console_is_fixedsize(QemuConsole *con) | |
2040 | { | |
2041 | if (con == NULL) { | |
2042 | con = active_console; | |
2043 | } | |
2044 | return con && (con->console_type != TEXT_CONSOLE); | |
c21bbcfa AZ |
2045 | } |
2046 | ||
f607867c GH |
2047 | bool qemu_console_is_gl_blocked(QemuConsole *con) |
2048 | { | |
2049 | assert(con != NULL); | |
2050 | return con->gl_block; | |
2051 | } | |
2052 | ||
779ce88f GH |
2053 | char *qemu_console_get_label(QemuConsole *con) |
2054 | { | |
2055 | if (con->console_type == GRAPHIC_CONSOLE) { | |
2056 | if (con->device) { | |
2057 | return g_strdup(object_get_typename(con->device)); | |
2058 | } | |
2059 | return g_strdup("VGA"); | |
2060 | } else { | |
2061 | if (con->chr && con->chr->label) { | |
2062 | return g_strdup(con->chr->label); | |
2063 | } | |
2064 | return g_strdup_printf("vc%d", con->index); | |
2065 | } | |
2066 | } | |
2067 | ||
d4c85337 GH |
2068 | int qemu_console_get_index(QemuConsole *con) |
2069 | { | |
2070 | if (con == NULL) { | |
2071 | con = active_console; | |
2072 | } | |
2073 | return con ? con->index : -1; | |
2074 | } | |
2075 | ||
5643706a GH |
2076 | uint32_t qemu_console_get_head(QemuConsole *con) |
2077 | { | |
2078 | if (con == NULL) { | |
2079 | con = active_console; | |
2080 | } | |
2081 | return con ? con->head : -1; | |
2082 | } | |
2083 | ||
6f90f3d7 GH |
2084 | QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con) |
2085 | { | |
2086 | assert(con != NULL); | |
2087 | return &con->ui_info; | |
2088 | } | |
2089 | ||
d4c85337 GH |
2090 | int qemu_console_get_width(QemuConsole *con, int fallback) |
2091 | { | |
2092 | if (con == NULL) { | |
2093 | con = active_console; | |
2094 | } | |
2095 | return con ? surface_width(con->surface) : fallback; | |
2096 | } | |
2097 | ||
2098 | int qemu_console_get_height(QemuConsole *con, int fallback) | |
2099 | { | |
2100 | if (con == NULL) { | |
2101 | con = active_console; | |
2102 | } | |
2103 | return con ? surface_height(con->surface) : fallback; | |
2104 | } | |
2105 | ||
5bf5adae | 2106 | static void vc_chr_set_echo(Chardev *chr, bool echo) |
4104833f | 2107 | { |
777357d7 | 2108 | VCChardev *drv = VC_CHARDEV(chr); |
41ac54b2 | 2109 | QemuConsole *s = drv->console; |
4104833f PB |
2110 | |
2111 | s->echo = echo; | |
2112 | } | |
2113 | ||
aea7947c GH |
2114 | static void text_console_update_cursor_timer(void) |
2115 | { | |
2116 | timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) | |
2117 | + CONSOLE_CURSOR_PERIOD / 2); | |
2118 | } | |
2119 | ||
bf1bed81 JK |
2120 | static void text_console_update_cursor(void *opaque) |
2121 | { | |
aea7947c | 2122 | QemuConsole *s; |
cd6cd8fa | 2123 | int count = 0; |
aea7947c GH |
2124 | |
2125 | cursor_visible_phase = !cursor_visible_phase; | |
bf1bed81 | 2126 | |
cd6cd8fa | 2127 | QTAILQ_FOREACH(s, &consoles, next) { |
aea7947c GH |
2128 | if (qemu_console_is_graphic(s) || |
2129 | !qemu_console_is_visible(s)) { | |
2130 | continue; | |
2131 | } | |
2132 | count++; | |
2133 | graphic_hw_invalidate(s); | |
2134 | } | |
2135 | ||
2136 | if (count) { | |
2137 | text_console_update_cursor_timer(); | |
2138 | } | |
bf1bed81 JK |
2139 | } |
2140 | ||
380cd056 GH |
2141 | static const GraphicHwOps text_console_ops = { |
2142 | .invalidate = text_console_invalidate, | |
2143 | .text_update = text_console_update, | |
2144 | }; | |
2145 | ||
0ec7b3e7 | 2146 | static void text_console_do_init(Chardev *chr, DisplayState *ds) |
e7f0ad58 | 2147 | { |
777357d7 | 2148 | VCChardev *drv = VC_CHARDEV(chr); |
41ac54b2 | 2149 | QemuConsole *s = drv->console; |
36671fbd GH |
2150 | int g_width = 80 * FONT_WIDTH; |
2151 | int g_height = 24 * FONT_HEIGHT; | |
6d6f7c28 | 2152 | |
e15d7371 FB |
2153 | s->out_fifo.buf = s->out_fifo_buf; |
2154 | s->out_fifo.buf_size = sizeof(s->out_fifo_buf); | |
bc72ad67 | 2155 | s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s); |
3023f332 | 2156 | s->ds = ds; |
3b46e624 | 2157 | |
e7f0ad58 FB |
2158 | s->y_displayed = 0; |
2159 | s->y_base = 0; | |
2160 | s->total_height = DEFAULT_BACKSCROLL; | |
2161 | s->x = 0; | |
2162 | s->y = 0; | |
36671fbd | 2163 | if (!s->surface) { |
321f048d | 2164 | if (active_console && active_console->surface) { |
36671fbd GH |
2165 | g_width = surface_width(active_console->surface); |
2166 | g_height = surface_height(active_console->surface); | |
321f048d | 2167 | } |
36671fbd | 2168 | s->surface = qemu_create_displaysurface(g_width, g_height); |
491e114a | 2169 | } |
6d6f7c28 | 2170 | |
380cd056 | 2171 | s->hw_ops = &text_console_ops; |
4d3b6f6e AZ |
2172 | s->hw = s; |
2173 | ||
6d6f7c28 PB |
2174 | /* Set text attribute defaults */ |
2175 | s->t_attrib_default.bold = 0; | |
2176 | s->t_attrib_default.uline = 0; | |
2177 | s->t_attrib_default.blink = 0; | |
2178 | s->t_attrib_default.invers = 0; | |
2179 | s->t_attrib_default.unvisible = 0; | |
4083733d OH |
2180 | s->t_attrib_default.fgcol = QEMU_COLOR_WHITE; |
2181 | s->t_attrib_default.bgcol = QEMU_COLOR_BLACK; | |
6d6f7c28 PB |
2182 | /* set current text attributes to default */ |
2183 | s->t_attrib = s->t_attrib_default; | |
e7f0ad58 FB |
2184 | text_console_resize(s); |
2185 | ||
51bfa4d3 | 2186 | if (chr->label) { |
18595181 | 2187 | char *msg; |
51bfa4d3 | 2188 | |
4083733d | 2189 | s->t_attrib.bgcol = QEMU_COLOR_BLUE; |
18595181 GH |
2190 | msg = g_strdup_printf("%s console\r\n", chr->label); |
2191 | vc_chr_write(chr, (uint8_t *)msg, strlen(msg)); | |
2192 | g_free(msg); | |
735ba588 | 2193 | s->t_attrib = s->t_attrib_default; |
51bfa4d3 GH |
2194 | } |
2195 | ||
63618135 | 2196 | qemu_chr_be_event(chr, CHR_EVENT_OPENED); |
e7f0ad58 | 2197 | } |
c60e08d9 | 2198 | |
777357d7 MAL |
2199 | static void vc_chr_open(Chardev *chr, |
2200 | ChardevBackend *backend, | |
2201 | bool *be_opened, | |
2202 | Error **errp) | |
2796dae0 | 2203 | { |
6f974c84 | 2204 | ChardevVC *vc = backend->u.vc.data; |
777357d7 | 2205 | VCChardev *drv = VC_CHARDEV(chr); |
76ffb0b4 | 2206 | QemuConsole *s; |
702ec69c GH |
2207 | unsigned width = 0; |
2208 | unsigned height = 0; | |
2796dae0 | 2209 | |
702ec69c GH |
2210 | if (vc->has_width) { |
2211 | width = vc->width; | |
2212 | } else if (vc->has_cols) { | |
2213 | width = vc->cols * FONT_WIDTH; | |
2214 | } | |
491e114a | 2215 | |
702ec69c GH |
2216 | if (vc->has_height) { |
2217 | height = vc->height; | |
2218 | } else if (vc->has_rows) { | |
2219 | height = vc->rows * FONT_HEIGHT; | |
2220 | } | |
491e114a | 2221 | |
437fe106 | 2222 | trace_console_txt_new(width, height); |
491e114a | 2223 | if (width == 0 || height == 0) { |
afff2b15 | 2224 | s = new_console(NULL, TEXT_CONSOLE, 0); |
491e114a | 2225 | } else { |
afff2b15 | 2226 | s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0); |
36671fbd | 2227 | s->surface = qemu_create_displaysurface(width, height); |
491e114a PB |
2228 | } |
2229 | ||
2230 | if (!s) { | |
fa19d025 | 2231 | error_setg(errp, "cannot create text console"); |
777357d7 | 2232 | return; |
491e114a PB |
2233 | } |
2234 | ||
2235 | s->chr = chr; | |
41ac54b2 | 2236 | drv->console = s; |
64840c66 GH |
2237 | |
2238 | if (display_state) { | |
2239 | text_console_do_init(chr, display_state); | |
2240 | } | |
2796dae0 | 2241 | |
82878dac MAL |
2242 | /* console/chardev init sometimes completes elsewhere in a 2nd |
2243 | * stage, so defer OPENED events until they are fully initialized | |
2244 | */ | |
2245 | *be_opened = false; | |
d82831db AL |
2246 | } |
2247 | ||
c78f7137 | 2248 | void qemu_console_resize(QemuConsole *s, int width, int height) |
c60e08d9 | 2249 | { |
321f048d GH |
2250 | DisplaySurface *surface; |
2251 | ||
2252 | assert(s->console_type == GRAPHIC_CONSOLE); | |
cd958edb | 2253 | |
3ef0c573 | 2254 | if (s->surface && (s->surface->flags & QEMU_ALLOCATED_FLAG) && |
cd958edb MAL |
2255 | pixman_image_get_width(s->surface->image) == width && |
2256 | pixman_image_get_height(s->surface->image) == height) { | |
2257 | return; | |
2258 | } | |
2259 | ||
321f048d GH |
2260 | surface = qemu_create_displaysurface(width, height); |
2261 | dpy_gfx_replace_surface(s, surface); | |
c60e08d9 | 2262 | } |
38334f76 | 2263 | |
c78f7137 GH |
2264 | DisplaySurface *qemu_console_surface(QemuConsole *console) |
2265 | { | |
321f048d | 2266 | return console->surface; |
c78f7137 GH |
2267 | } |
2268 | ||
0da2ea1b | 2269 | PixelFormat qemu_default_pixelformat(int bpp) |
2270 | { | |
56bd9ea1 GH |
2271 | pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true); |
2272 | PixelFormat pf = qemu_pixelformat_from_pixman(fmt); | |
7d957bd8 AL |
2273 | return pf; |
2274 | } | |
01f45d98 | 2275 | |
db71589f GH |
2276 | static QemuDisplay *dpys[DISPLAY_TYPE__MAX]; |
2277 | ||
2278 | void qemu_display_register(QemuDisplay *ui) | |
2279 | { | |
2280 | assert(ui->type < DISPLAY_TYPE__MAX); | |
2281 | dpys[ui->type] = ui; | |
2282 | } | |
2283 | ||
898f9d41 GH |
2284 | bool qemu_display_find_default(DisplayOptions *opts) |
2285 | { | |
2286 | static DisplayType prio[] = { | |
2287 | DISPLAY_TYPE_GTK, | |
2288 | DISPLAY_TYPE_SDL, | |
2289 | DISPLAY_TYPE_COCOA | |
2290 | }; | |
2291 | int i; | |
2292 | ||
2293 | for (i = 0; i < ARRAY_SIZE(prio); i++) { | |
61b4d9a2 | 2294 | if (dpys[prio[i]] == NULL) { |
c809d1d2 | 2295 | ui_module_load_one(DisplayType_str(prio[i])); |
61b4d9a2 | 2296 | } |
898f9d41 GH |
2297 | if (dpys[prio[i]] == NULL) { |
2298 | continue; | |
2299 | } | |
2300 | opts->type = prio[i]; | |
2301 | return true; | |
2302 | } | |
2303 | return false; | |
2304 | } | |
2305 | ||
db71589f GH |
2306 | void qemu_display_early_init(DisplayOptions *opts) |
2307 | { | |
2308 | assert(opts->type < DISPLAY_TYPE__MAX); | |
2309 | if (opts->type == DISPLAY_TYPE_NONE) { | |
2310 | return; | |
2311 | } | |
61b4d9a2 | 2312 | if (dpys[opts->type] == NULL) { |
c809d1d2 | 2313 | ui_module_load_one(DisplayType_str(opts->type)); |
61b4d9a2 | 2314 | } |
db71589f GH |
2315 | if (dpys[opts->type] == NULL) { |
2316 | error_report("Display '%s' is not available.", | |
c809d1d2 | 2317 | DisplayType_str(opts->type)); |
db71589f GH |
2318 | exit(1); |
2319 | } | |
2320 | if (dpys[opts->type]->early_init) { | |
2321 | dpys[opts->type]->early_init(opts); | |
2322 | } | |
2323 | } | |
2324 | ||
2325 | void qemu_display_init(DisplayState *ds, DisplayOptions *opts) | |
2326 | { | |
2327 | assert(opts->type < DISPLAY_TYPE__MAX); | |
2328 | if (opts->type == DISPLAY_TYPE_NONE) { | |
2329 | return; | |
2330 | } | |
2331 | assert(dpys[opts->type] != NULL); | |
2332 | dpys[opts->type]->init(ds, opts); | |
2333 | } | |
2334 | ||
c388f408 TH |
2335 | void qemu_display_help(void) |
2336 | { | |
2337 | int idx; | |
2338 | ||
2339 | printf("Available display backend types:\n"); | |
a1e8853e | 2340 | printf("none\n"); |
c388f408 TH |
2341 | for (idx = DISPLAY_TYPE_NONE; idx < DISPLAY_TYPE__MAX; idx++) { |
2342 | if (!dpys[idx]) { | |
2343 | ui_module_load_one(DisplayType_str(idx)); | |
2344 | } | |
2345 | if (dpys[idx]) { | |
2346 | printf("%s\n", DisplayType_str(dpys[idx]->type)); | |
2347 | } | |
2348 | } | |
2349 | } | |
2350 | ||
6f974c84 | 2351 | void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp) |
702ec69c GH |
2352 | { |
2353 | int val; | |
21a933ea | 2354 | ChardevVC *vc; |
702ec69c | 2355 | |
0b663b7d | 2356 | backend->type = CHARDEV_BACKEND_KIND_VC; |
32bafa8f | 2357 | vc = backend->u.vc.data = g_new0(ChardevVC, 1); |
21a933ea | 2358 | qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc)); |
702ec69c GH |
2359 | |
2360 | val = qemu_opt_get_number(opts, "width", 0); | |
2361 | if (val != 0) { | |
21a933ea EB |
2362 | vc->has_width = true; |
2363 | vc->width = val; | |
702ec69c GH |
2364 | } |
2365 | ||
2366 | val = qemu_opt_get_number(opts, "height", 0); | |
2367 | if (val != 0) { | |
21a933ea EB |
2368 | vc->has_height = true; |
2369 | vc->height = val; | |
702ec69c GH |
2370 | } |
2371 | ||
2372 | val = qemu_opt_get_number(opts, "cols", 0); | |
2373 | if (val != 0) { | |
21a933ea EB |
2374 | vc->has_cols = true; |
2375 | vc->cols = val; | |
702ec69c GH |
2376 | } |
2377 | ||
2378 | val = qemu_opt_get_number(opts, "rows", 0); | |
2379 | if (val != 0) { | |
21a933ea EB |
2380 | vc->has_rows = true; |
2381 | vc->rows = val; | |
702ec69c GH |
2382 | } |
2383 | } | |
2384 | ||
95be0669 GH |
2385 | static const TypeInfo qemu_console_info = { |
2386 | .name = TYPE_QEMU_CONSOLE, | |
2387 | .parent = TYPE_OBJECT, | |
2388 | .instance_size = sizeof(QemuConsole), | |
2389 | .class_size = sizeof(QemuConsoleClass), | |
2390 | }; | |
2391 | ||
777357d7 MAL |
2392 | static void char_vc_class_init(ObjectClass *oc, void *data) |
2393 | { | |
2394 | ChardevClass *cc = CHARDEV_CLASS(oc); | |
2395 | ||
88cace9f | 2396 | cc->parse = qemu_chr_parse_vc; |
777357d7 MAL |
2397 | cc->open = vc_chr_open; |
2398 | cc->chr_write = vc_chr_write; | |
2399 | cc->chr_set_echo = vc_chr_set_echo; | |
2400 | } | |
2401 | ||
2402 | static const TypeInfo char_vc_type_info = { | |
2403 | .name = TYPE_CHARDEV_VC, | |
2404 | .parent = TYPE_CHARDEV, | |
0ec7b3e7 | 2405 | .instance_size = sizeof(VCChardev), |
777357d7 MAL |
2406 | .class_init = char_vc_class_init, |
2407 | }; | |
2408 | ||
2409 | void qemu_console_early_init(void) | |
2410 | { | |
2411 | /* set the default vc driver */ | |
2412 | if (!object_class_by_name(TYPE_CHARDEV_VC)) { | |
2413 | type_register(&char_vc_type_info); | |
777357d7 MAL |
2414 | } |
2415 | } | |
2416 | ||
01f45d98 AL |
2417 | static void register_types(void) |
2418 | { | |
95be0669 | 2419 | type_register_static(&qemu_console_info); |
01f45d98 AL |
2420 | } |
2421 | ||
2422 | type_init(register_types); |