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