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