bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU graphical console |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 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 | */ |
Peter Maydell | e16f4c8 | 2016-01-29 17:49:51 +0000 | [diff] [blame] | 24 | #include "qemu/osdep.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 25 | #include "qemu-common.h" |
Paolo Bonzini | 28ecbae | 2012-11-28 12:06:30 +0100 | [diff] [blame] | 26 | #include "ui/console.h" |
Gerd Hoffmann | aa2beaa | 2013-04-17 10:21:27 +0200 | [diff] [blame] | 27 | #include "hw/qdev-core.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 28 | #include "qemu/timer.h" |
Luiz Capitulino | ad39cf6 | 2012-05-24 13:48:23 -0300 | [diff] [blame] | 29 | #include "qmp-commands.h" |
Paolo Bonzini | dccfcd0 | 2013-04-08 16:55:25 +0200 | [diff] [blame] | 30 | #include "sysemu/char.h" |
Stefan Weil | ac86048 | 2013-11-10 14:20:16 +0100 | [diff] [blame] | 31 | #include "trace.h" |
Gerd Hoffmann | a77549b | 2014-06-19 08:46:08 +0200 | [diff] [blame] | 32 | #include "exec/memory.h" |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 33 | |
| 34 | #define DEFAULT_BACKSCROLL 512 |
Jan Kiszka | bf1bed8 | 2012-07-10 22:00:55 +0200 | [diff] [blame] | 35 | #define CONSOLE_CURSOR_PERIOD 500 |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 36 | |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 37 | typedef struct TextAttributes { |
| 38 | uint8_t fgcol:4; |
| 39 | uint8_t bgcol:4; |
| 40 | uint8_t bold:1; |
| 41 | uint8_t uline:1; |
| 42 | uint8_t blink:1; |
| 43 | uint8_t invers:1; |
| 44 | uint8_t unvisible:1; |
| 45 | } TextAttributes; |
| 46 | |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 47 | typedef struct TextCell { |
| 48 | uint8_t ch; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 49 | TextAttributes t_attrib; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 50 | } TextCell; |
| 51 | |
| 52 | #define MAX_ESC_PARAMS 3 |
| 53 | |
| 54 | enum TTYState { |
| 55 | TTY_STATE_NORM, |
| 56 | TTY_STATE_ESC, |
| 57 | TTY_STATE_CSI, |
| 58 | }; |
| 59 | |
bellard | e15d737 | 2006-06-25 16:26:29 +0000 | [diff] [blame] | 60 | typedef struct QEMUFIFO { |
| 61 | uint8_t *buf; |
| 62 | int buf_size; |
| 63 | int count, wptr, rptr; |
| 64 | } QEMUFIFO; |
| 65 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 66 | static int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1) |
bellard | e15d737 | 2006-06-25 16:26:29 +0000 | [diff] [blame] | 67 | { |
| 68 | int l, len; |
| 69 | |
| 70 | l = f->buf_size - f->count; |
| 71 | if (len1 > l) |
| 72 | len1 = l; |
| 73 | len = len1; |
| 74 | while (len > 0) { |
| 75 | l = f->buf_size - f->wptr; |
| 76 | if (l > len) |
| 77 | l = len; |
| 78 | memcpy(f->buf + f->wptr, buf, l); |
| 79 | f->wptr += l; |
| 80 | if (f->wptr >= f->buf_size) |
| 81 | f->wptr = 0; |
| 82 | buf += l; |
| 83 | len -= l; |
| 84 | } |
| 85 | f->count += len1; |
| 86 | return len1; |
| 87 | } |
| 88 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 89 | static int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1) |
bellard | e15d737 | 2006-06-25 16:26:29 +0000 | [diff] [blame] | 90 | { |
| 91 | int l, len; |
| 92 | |
| 93 | if (len1 > f->count) |
| 94 | len1 = f->count; |
| 95 | len = len1; |
| 96 | while (len > 0) { |
| 97 | l = f->buf_size - f->rptr; |
| 98 | if (l > len) |
| 99 | l = len; |
| 100 | memcpy(buf, f->buf + f->rptr, l); |
| 101 | f->rptr += l; |
| 102 | if (f->rptr >= f->buf_size) |
| 103 | f->rptr = 0; |
| 104 | buf += l; |
| 105 | len -= l; |
| 106 | } |
| 107 | f->count -= len1; |
| 108 | return len1; |
| 109 | } |
| 110 | |
ths | af3a903 | 2007-07-11 23:14:59 +0000 | [diff] [blame] | 111 | typedef enum { |
| 112 | GRAPHIC_CONSOLE, |
balrog | c21bbcf | 2008-09-24 03:32:33 +0000 | [diff] [blame] | 113 | TEXT_CONSOLE, |
| 114 | TEXT_CONSOLE_FIXED_SIZE |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 115 | } console_type_t; |
ths | af3a903 | 2007-07-11 23:14:59 +0000 | [diff] [blame] | 116 | |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 117 | struct QemuConsole { |
Gerd Hoffmann | 95be066 | 2013-04-17 09:45:10 +0200 | [diff] [blame] | 118 | Object parent; |
| 119 | |
Jan Kiszka | f81bdef | 2011-09-16 00:48:07 +0200 | [diff] [blame] | 120 | int index; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 121 | console_type_t console_type; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 122 | DisplayState *ds; |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 123 | DisplaySurface *surface; |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 124 | int dcls; |
Gerd Hoffmann | 06020b9 | 2014-07-11 13:56:51 +0200 | [diff] [blame] | 125 | DisplayChangeListener *gl; |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 126 | |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 127 | /* Graphic console state. */ |
Gerd Hoffmann | aa2beaa | 2013-04-17 10:21:27 +0200 | [diff] [blame] | 128 | Object *device; |
Gerd Hoffmann | 5643706 | 2014-01-24 15:35:21 +0100 | [diff] [blame] | 129 | uint32_t head; |
Gerd Hoffmann | 6f90f3d | 2014-01-24 17:38:20 +0100 | [diff] [blame] | 130 | QemuUIInfo ui_info; |
Gerd Hoffmann | cf1ecc8 | 2015-03-12 12:51:13 +0100 | [diff] [blame] | 131 | QEMUTimer *ui_timer; |
Gerd Hoffmann | 380cd05 | 2013-03-13 14:04:18 +0100 | [diff] [blame] | 132 | const GraphicHwOps *hw_ops; |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 133 | void *hw; |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 134 | |
| 135 | /* Text console state */ |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 136 | int width; |
| 137 | int height; |
| 138 | int total_height; |
| 139 | int backscroll_height; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 140 | int x, y; |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 141 | int x_saved, y_saved; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 142 | int y_displayed; |
| 143 | int y_base; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 144 | TextAttributes t_attrib_default; /* default text attributes */ |
| 145 | TextAttributes t_attrib; /* currently active text attributes */ |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 146 | TextCell *cells; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 147 | int text_x[2], text_y[2], cursor_invalidate; |
Paolo Bonzini | 4104833 | 2010-12-23 13:42:52 +0100 | [diff] [blame] | 148 | int echo; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 149 | |
pbrook | 14778c2 | 2009-01-21 03:02:52 +0000 | [diff] [blame] | 150 | int update_x0; |
| 151 | int update_y0; |
| 152 | int update_x1; |
| 153 | int update_y1; |
| 154 | |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 155 | enum TTYState state; |
| 156 | int esc_params[MAX_ESC_PARAMS]; |
| 157 | int nb_esc_params; |
| 158 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 159 | CharDriverState *chr; |
bellard | e15d737 | 2006-06-25 16:26:29 +0000 | [diff] [blame] | 160 | /* fifo for key pressed */ |
| 161 | QEMUFIFO out_fifo; |
| 162 | uint8_t out_fifo_buf[16]; |
| 163 | QEMUTimer *kbd_timer; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
Gerd Hoffmann | 27be558 | 2013-03-13 12:25:25 +0100 | [diff] [blame] | 166 | struct DisplayState { |
Stefan Weil | 1246b25 | 2013-12-01 08:49:47 +0100 | [diff] [blame] | 167 | QEMUTimer *gui_timer; |
Gerd Hoffmann | 0f7b286 | 2013-03-14 11:56:16 +0100 | [diff] [blame] | 168 | uint64_t last_update; |
| 169 | uint64_t update_interval; |
| 170 | bool refreshing; |
Gerd Hoffmann | 27be558 | 2013-03-13 12:25:25 +0100 | [diff] [blame] | 171 | bool have_gfx; |
| 172 | bool have_text; |
| 173 | |
| 174 | QLIST_HEAD(, DisplayChangeListener) listeners; |
| 175 | }; |
| 176 | |
Paolo Bonzini | 98b5008 | 2010-02-11 00:29:57 +0100 | [diff] [blame] | 177 | static DisplayState *display_state; |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 178 | static QemuConsole *active_console; |
Gerd Hoffmann | a1d2db0 | 2014-05-26 10:36:35 +0200 | [diff] [blame] | 179 | static QemuConsole **consoles; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 180 | static int nb_consoles = 0; |
Gerd Hoffmann | aea7947 | 2014-05-22 11:27:13 +0200 | [diff] [blame] | 181 | static bool cursor_visible_phase; |
| 182 | static QEMUTimer *cursor_timer; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 183 | |
Gerd Hoffmann | 64840c6 | 2013-03-07 17:08:29 +0100 | [diff] [blame] | 184 | static void text_console_do_init(CharDriverState *chr, DisplayState *ds); |
Gerd Hoffmann | 0f7b286 | 2013-03-14 11:56:16 +0100 | [diff] [blame] | 185 | static void dpy_refresh(DisplayState *s); |
Gerd Hoffmann | 5209089 | 2013-04-23 15:44:31 +0200 | [diff] [blame] | 186 | static DisplayState *get_alloc_displaystate(void); |
Gerd Hoffmann | aea7947 | 2014-05-22 11:27:13 +0200 | [diff] [blame] | 187 | static void text_console_update_cursor_timer(void); |
| 188 | static void text_console_update_cursor(void *opaque); |
Gerd Hoffmann | 64840c6 | 2013-03-07 17:08:29 +0100 | [diff] [blame] | 189 | |
Gerd Hoffmann | 98a9ad9 | 2013-03-13 12:17:13 +0100 | [diff] [blame] | 190 | static void gui_update(void *opaque) |
| 191 | { |
Gerd Hoffmann | 0f7b286 | 2013-03-14 11:56:16 +0100 | [diff] [blame] | 192 | uint64_t interval = GUI_REFRESH_INTERVAL_IDLE; |
| 193 | uint64_t dcl_interval; |
Gerd Hoffmann | 98a9ad9 | 2013-03-13 12:17:13 +0100 | [diff] [blame] | 194 | DisplayState *ds = opaque; |
| 195 | DisplayChangeListener *dcl; |
Gerd Hoffmann | dea1b0b | 2013-03-19 15:01:02 +0100 | [diff] [blame] | 196 | int i; |
Gerd Hoffmann | 98a9ad9 | 2013-03-13 12:17:13 +0100 | [diff] [blame] | 197 | |
Gerd Hoffmann | 0f7b286 | 2013-03-14 11:56:16 +0100 | [diff] [blame] | 198 | ds->refreshing = true; |
Gerd Hoffmann | 98a9ad9 | 2013-03-13 12:17:13 +0100 | [diff] [blame] | 199 | dpy_refresh(ds); |
Gerd Hoffmann | 0f7b286 | 2013-03-14 11:56:16 +0100 | [diff] [blame] | 200 | ds->refreshing = false; |
Gerd Hoffmann | 98a9ad9 | 2013-03-13 12:17:13 +0100 | [diff] [blame] | 201 | |
| 202 | QLIST_FOREACH(dcl, &ds->listeners, next) { |
Gerd Hoffmann | 0f7b286 | 2013-03-14 11:56:16 +0100 | [diff] [blame] | 203 | dcl_interval = dcl->update_interval ? |
| 204 | dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT; |
| 205 | if (interval > dcl_interval) { |
| 206 | interval = dcl_interval; |
Gerd Hoffmann | 98a9ad9 | 2013-03-13 12:17:13 +0100 | [diff] [blame] | 207 | } |
| 208 | } |
Gerd Hoffmann | 0f7b286 | 2013-03-14 11:56:16 +0100 | [diff] [blame] | 209 | if (ds->update_interval != interval) { |
| 210 | ds->update_interval = interval; |
Gerd Hoffmann | dea1b0b | 2013-03-19 15:01:02 +0100 | [diff] [blame] | 211 | for (i = 0; i < nb_consoles; i++) { |
| 212 | if (consoles[i]->hw_ops->update_interval) { |
| 213 | consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval); |
| 214 | } |
| 215 | } |
Gerd Hoffmann | 0f7b286 | 2013-03-14 11:56:16 +0100 | [diff] [blame] | 216 | trace_console_refresh(interval); |
| 217 | } |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 218 | ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
| 219 | timer_mod(ds->gui_timer, ds->last_update + interval); |
Gerd Hoffmann | 98a9ad9 | 2013-03-13 12:17:13 +0100 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static void gui_setup_refresh(DisplayState *ds) |
| 223 | { |
| 224 | DisplayChangeListener *dcl; |
| 225 | bool need_timer = false; |
| 226 | bool have_gfx = false; |
| 227 | bool have_text = false; |
| 228 | |
| 229 | QLIST_FOREACH(dcl, &ds->listeners, next) { |
| 230 | if (dcl->ops->dpy_refresh != NULL) { |
| 231 | need_timer = true; |
| 232 | } |
| 233 | if (dcl->ops->dpy_gfx_update != NULL) { |
| 234 | have_gfx = true; |
| 235 | } |
| 236 | if (dcl->ops->dpy_text_update != NULL) { |
| 237 | have_text = true; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | if (need_timer && ds->gui_timer == NULL) { |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 242 | ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds); |
| 243 | timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)); |
Gerd Hoffmann | 98a9ad9 | 2013-03-13 12:17:13 +0100 | [diff] [blame] | 244 | } |
| 245 | if (!need_timer && ds->gui_timer != NULL) { |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 246 | timer_del(ds->gui_timer); |
| 247 | timer_free(ds->gui_timer); |
Gerd Hoffmann | 98a9ad9 | 2013-03-13 12:17:13 +0100 | [diff] [blame] | 248 | ds->gui_timer = NULL; |
| 249 | } |
| 250 | |
| 251 | ds->have_gfx = have_gfx; |
| 252 | ds->have_text = have_text; |
| 253 | } |
| 254 | |
Gerd Hoffmann | 1dbfa00 | 2013-03-12 13:44:38 +0100 | [diff] [blame] | 255 | void graphic_hw_update(QemuConsole *con) |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 256 | { |
Gerd Hoffmann | 1dbfa00 | 2013-03-12 13:44:38 +0100 | [diff] [blame] | 257 | if (!con) { |
| 258 | con = active_console; |
| 259 | } |
Gerd Hoffmann | 380cd05 | 2013-03-13 14:04:18 +0100 | [diff] [blame] | 260 | if (con && con->hw_ops->gfx_update) { |
| 261 | con->hw_ops->gfx_update(con->hw); |
Gerd Hoffmann | 1dbfa00 | 2013-03-12 13:44:38 +0100 | [diff] [blame] | 262 | } |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Gerd Hoffmann | bba19b8 | 2015-12-03 12:34:25 +0100 | [diff] [blame] | 265 | void graphic_hw_gl_block(QemuConsole *con, bool block) |
| 266 | { |
| 267 | if (!con) { |
| 268 | con = active_console; |
| 269 | } |
| 270 | if (con && con->hw_ops->gl_block) { |
| 271 | con->hw_ops->gl_block(con->hw, block); |
| 272 | } |
| 273 | } |
| 274 | |
Gerd Hoffmann | 1dbfa00 | 2013-03-12 13:44:38 +0100 | [diff] [blame] | 275 | void graphic_hw_invalidate(QemuConsole *con) |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 276 | { |
Gerd Hoffmann | 1dbfa00 | 2013-03-12 13:44:38 +0100 | [diff] [blame] | 277 | if (!con) { |
| 278 | con = active_console; |
| 279 | } |
Gerd Hoffmann | 380cd05 | 2013-03-13 14:04:18 +0100 | [diff] [blame] | 280 | if (con && con->hw_ops->invalidate) { |
| 281 | con->hw_ops->invalidate(con->hw); |
Gerd Hoffmann | 1dbfa00 | 2013-03-12 13:44:38 +0100 | [diff] [blame] | 282 | } |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Chih-Min Chao | 9425c00 | 2015-04-09 02:04:13 +0800 | [diff] [blame] | 285 | static void ppm_save(const char *filename, DisplaySurface *ds, |
Gerd Hoffmann | 2c62f08 | 2013-03-12 14:48:31 +0100 | [diff] [blame] | 286 | Error **errp) |
| 287 | { |
| 288 | int width = pixman_image_get_width(ds->image); |
| 289 | int height = pixman_image_get_height(ds->image); |
Gerd Hoffmann | cdd5b93 | 2013-04-23 13:26:59 +0200 | [diff] [blame] | 290 | int fd; |
Gerd Hoffmann | 2c62f08 | 2013-03-12 14:48:31 +0100 | [diff] [blame] | 291 | FILE *f; |
| 292 | int y; |
| 293 | int ret; |
| 294 | pixman_image_t *linebuf; |
| 295 | |
| 296 | trace_ppm_save(filename, ds); |
Gerd Hoffmann | cdd5b93 | 2013-04-23 13:26:59 +0200 | [diff] [blame] | 297 | fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); |
| 298 | if (fd == -1) { |
Gerd Hoffmann | 2c62f08 | 2013-03-12 14:48:31 +0100 | [diff] [blame] | 299 | error_setg(errp, "failed to open file '%s': %s", filename, |
| 300 | strerror(errno)); |
| 301 | return; |
| 302 | } |
Gerd Hoffmann | cdd5b93 | 2013-04-23 13:26:59 +0200 | [diff] [blame] | 303 | f = fdopen(fd, "wb"); |
Gerd Hoffmann | 2c62f08 | 2013-03-12 14:48:31 +0100 | [diff] [blame] | 304 | ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255); |
| 305 | if (ret < 0) { |
| 306 | linebuf = NULL; |
| 307 | goto write_err; |
| 308 | } |
| 309 | linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width); |
| 310 | for (y = 0; y < height; y++) { |
| 311 | qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y); |
| 312 | clearerr(f); |
| 313 | ret = fwrite(pixman_image_get_data(linebuf), 1, |
| 314 | pixman_image_get_stride(linebuf), f); |
| 315 | (void)ret; |
| 316 | if (ferror(f)) { |
| 317 | goto write_err; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | out: |
| 322 | qemu_pixman_image_unref(linebuf); |
| 323 | fclose(f); |
| 324 | return; |
| 325 | |
| 326 | write_err: |
| 327 | error_setg(errp, "failed to write to file '%s': %s", filename, |
| 328 | strerror(errno)); |
| 329 | unlink(filename); |
| 330 | goto out; |
| 331 | } |
| 332 | |
Luiz Capitulino | ad39cf6 | 2012-05-24 13:48:23 -0300 | [diff] [blame] | 333 | void qmp_screendump(const char *filename, Error **errp) |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 334 | { |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 335 | QemuConsole *con = qemu_console_lookup_by_index(0); |
Gerd Hoffmann | 2c62f08 | 2013-03-12 14:48:31 +0100 | [diff] [blame] | 336 | DisplaySurface *surface; |
balrog | 8571c05 | 2008-07-19 13:04:26 +0000 | [diff] [blame] | 337 | |
Gerd Hoffmann | 2c62f08 | 2013-03-12 14:48:31 +0100 | [diff] [blame] | 338 | if (con == NULL) { |
| 339 | error_setg(errp, "There is no QemuConsole I can screendump from."); |
| 340 | return; |
Jan Kiszka | f81bdef | 2011-09-16 00:48:07 +0200 | [diff] [blame] | 341 | } |
| 342 | |
Gerd Hoffmann | 2c62f08 | 2013-03-12 14:48:31 +0100 | [diff] [blame] | 343 | graphic_hw_update(con); |
| 344 | surface = qemu_console_surface(con); |
| 345 | ppm_save(filename, surface, errp); |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Gerd Hoffmann | 1dbfa00 | 2013-03-12 13:44:38 +0100 | [diff] [blame] | 348 | void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 349 | { |
Gerd Hoffmann | 1dbfa00 | 2013-03-12 13:44:38 +0100 | [diff] [blame] | 350 | if (!con) { |
| 351 | con = active_console; |
| 352 | } |
Gerd Hoffmann | 380cd05 | 2013-03-13 14:04:18 +0100 | [diff] [blame] | 353 | if (con && con->hw_ops->text_update) { |
| 354 | con->hw_ops->text_update(con->hw, chardata); |
| 355 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Gerd Hoffmann | 1562e53 | 2013-03-06 13:40:47 +0100 | [diff] [blame] | 358 | static void vga_fill_rect(QemuConsole *con, |
| 359 | int posx, int posy, int width, int height, |
Gerd Hoffmann | e27bd65 | 2013-03-08 08:43:24 +0100 | [diff] [blame] | 360 | pixman_color_t color) |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 361 | { |
Gerd Hoffmann | 1562e53 | 2013-03-06 13:40:47 +0100 | [diff] [blame] | 362 | DisplaySurface *surface = qemu_console_surface(con); |
Gerd Hoffmann | 68db6dc | 2013-03-06 15:43:23 +0100 | [diff] [blame] | 363 | pixman_rectangle16_t rect = { |
| 364 | .x = posx, .y = posy, .width = width, .height = height |
| 365 | }; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 366 | |
Gerd Hoffmann | 68db6dc | 2013-03-06 15:43:23 +0100 | [diff] [blame] | 367 | pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image, |
Gerd Hoffmann | e27bd65 | 2013-03-08 08:43:24 +0100 | [diff] [blame] | 368 | &color, 1, &rect); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */ |
Gerd Hoffmann | 1562e53 | 2013-03-06 13:40:47 +0100 | [diff] [blame] | 372 | static void vga_bitblt(QemuConsole *con, |
| 373 | int xs, int ys, int xd, int yd, int w, int h) |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 374 | { |
Gerd Hoffmann | 1562e53 | 2013-03-06 13:40:47 +0100 | [diff] [blame] | 375 | DisplaySurface *surface = qemu_console_surface(con); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 376 | |
Gerd Hoffmann | 68db6dc | 2013-03-06 15:43:23 +0100 | [diff] [blame] | 377 | pixman_image_composite(PIXMAN_OP_SRC, |
| 378 | surface->image, NULL, surface->image, |
| 379 | xs, ys, 0, 0, xd, yd, w, h); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /***********************************************************/ |
| 383 | /* basic char display */ |
| 384 | |
| 385 | #define FONT_HEIGHT 16 |
| 386 | #define FONT_WIDTH 8 |
| 387 | |
| 388 | #include "vgafont.h" |
| 389 | |
Gerd Hoffmann | e27bd65 | 2013-03-08 08:43:24 +0100 | [diff] [blame] | 390 | #define QEMU_RGB(r, g, b) \ |
| 391 | { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff } |
| 392 | |
| 393 | static const pixman_color_t color_table_rgb[2][8] = { |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 394 | { /* dark */ |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 395 | [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */ |
| 396 | [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */ |
| 397 | [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */ |
| 398 | [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */ |
| 399 | [QEMU_COLOR_RED] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */ |
| 400 | [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */ |
| 401 | [QEMU_COLOR_YELLOW] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */ |
| 402 | [QEMU_COLOR_WHITE] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */ |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 403 | }, |
| 404 | { /* bright */ |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 405 | [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */ |
| 406 | [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */ |
| 407 | [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xff, 0x00), /* green */ |
| 408 | [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */ |
| 409 | [QEMU_COLOR_RED] = QEMU_RGB(0xff, 0x00, 0x00), /* red */ |
| 410 | [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */ |
| 411 | [QEMU_COLOR_YELLOW] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */ |
| 412 | [QEMU_COLOR_WHITE] = QEMU_RGB(0xff, 0xff, 0xff), /* white */ |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 413 | } |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 414 | }; |
| 415 | |
Gerd Hoffmann | 1562e53 | 2013-03-06 13:40:47 +0100 | [diff] [blame] | 416 | static void vga_putcharxy(QemuConsole *s, int x, int y, int ch, |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 417 | TextAttributes *t_attrib) |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 418 | { |
Gerd Hoffmann | 7d6ba01 | 2013-03-06 15:44:10 +0100 | [diff] [blame] | 419 | static pixman_image_t *glyphs[256]; |
Gerd Hoffmann | 1562e53 | 2013-03-06 13:40:47 +0100 | [diff] [blame] | 420 | DisplaySurface *surface = qemu_console_surface(s); |
Gerd Hoffmann | e27bd65 | 2013-03-08 08:43:24 +0100 | [diff] [blame] | 421 | pixman_color_t fgcol, bgcol; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 422 | |
| 423 | if (t_attrib->invers) { |
Gerd Hoffmann | cf6f054 | 2013-03-06 09:50:51 +0100 | [diff] [blame] | 424 | bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol]; |
| 425 | fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol]; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 426 | } else { |
Gerd Hoffmann | cf6f054 | 2013-03-06 09:50:51 +0100 | [diff] [blame] | 427 | fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol]; |
| 428 | bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol]; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 429 | } |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 430 | |
Gerd Hoffmann | 7d6ba01 | 2013-03-06 15:44:10 +0100 | [diff] [blame] | 431 | if (!glyphs[ch]) { |
| 432 | glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 433 | } |
Gerd Hoffmann | 7d6ba01 | 2013-03-06 15:44:10 +0100 | [diff] [blame] | 434 | qemu_pixman_glyph_render(glyphs[ch], surface->image, |
Gerd Hoffmann | e27bd65 | 2013-03-08 08:43:24 +0100 | [diff] [blame] | 435 | &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 438 | static void text_console_resize(QemuConsole *s) |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 439 | { |
| 440 | TextCell *cells, *c, *c1; |
| 441 | int w1, x, y, last_width; |
| 442 | |
| 443 | last_width = s->width; |
Gerd Hoffmann | 36671fb | 2013-03-13 10:14:52 +0100 | [diff] [blame] | 444 | s->width = surface_width(s->surface) / FONT_WIDTH; |
| 445 | s->height = surface_height(s->surface) / FONT_HEIGHT; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 446 | |
| 447 | w1 = last_width; |
| 448 | if (s->width < w1) |
| 449 | w1 = s->width; |
| 450 | |
Markus Armbruster | fedf0d3 | 2015-11-03 17:12:03 +0100 | [diff] [blame] | 451 | cells = g_new(TextCell, s->width * s->total_height); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 452 | for(y = 0; y < s->total_height; y++) { |
| 453 | c = &cells[y * s->width]; |
| 454 | if (w1 > 0) { |
| 455 | c1 = &s->cells[y * last_width]; |
| 456 | for(x = 0; x < w1; x++) { |
| 457 | *c++ = *c1++; |
| 458 | } |
| 459 | } |
| 460 | for(x = w1; x < s->width; x++) { |
| 461 | c->ch = ' '; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 462 | c->t_attrib = s->t_attrib_default; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 463 | c++; |
| 464 | } |
| 465 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 466 | g_free(s->cells); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 467 | s->cells = cells; |
| 468 | } |
| 469 | |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 470 | static inline void text_update_xy(QemuConsole *s, int x, int y) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 471 | { |
| 472 | s->text_x[0] = MIN(s->text_x[0], x); |
| 473 | s->text_x[1] = MAX(s->text_x[1], x); |
| 474 | s->text_y[0] = MIN(s->text_y[0], y); |
| 475 | s->text_y[1] = MAX(s->text_y[1], y); |
| 476 | } |
| 477 | |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 478 | static void invalidate_xy(QemuConsole *s, int x, int y) |
pbrook | 14778c2 | 2009-01-21 03:02:52 +0000 | [diff] [blame] | 479 | { |
Gerd Hoffmann | b35e3ba | 2014-05-22 11:01:42 +0200 | [diff] [blame] | 480 | if (!qemu_console_is_visible(s)) { |
| 481 | return; |
| 482 | } |
pbrook | 14778c2 | 2009-01-21 03:02:52 +0000 | [diff] [blame] | 483 | if (s->update_x0 > x * FONT_WIDTH) |
| 484 | s->update_x0 = x * FONT_WIDTH; |
| 485 | if (s->update_y0 > y * FONT_HEIGHT) |
| 486 | s->update_y0 = y * FONT_HEIGHT; |
| 487 | if (s->update_x1 < (x + 1) * FONT_WIDTH) |
| 488 | s->update_x1 = (x + 1) * FONT_WIDTH; |
| 489 | if (s->update_y1 < (y + 1) * FONT_HEIGHT) |
| 490 | s->update_y1 = (y + 1) * FONT_HEIGHT; |
| 491 | } |
| 492 | |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 493 | static void update_xy(QemuConsole *s, int x, int y) |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 494 | { |
| 495 | TextCell *c; |
| 496 | int y1, y2; |
| 497 | |
Gerd Hoffmann | 1562e53 | 2013-03-06 13:40:47 +0100 | [diff] [blame] | 498 | if (s->ds->have_text) { |
| 499 | text_update_xy(s, x, y); |
| 500 | } |
| 501 | |
Gerd Hoffmann | b35e3ba | 2014-05-22 11:01:42 +0200 | [diff] [blame] | 502 | y1 = (s->y_base + y) % s->total_height; |
| 503 | y2 = y1 - s->y_displayed; |
| 504 | if (y2 < 0) { |
| 505 | y2 += s->total_height; |
| 506 | } |
| 507 | if (y2 < s->height) { |
| 508 | c = &s->cells[y1 * s->width + x]; |
| 509 | vga_putcharxy(s, x, y2, c->ch, |
| 510 | &(c->t_attrib)); |
| 511 | invalidate_xy(s, x, y2); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 515 | static void console_show_cursor(QemuConsole *s, int show) |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 516 | { |
| 517 | TextCell *c; |
| 518 | int y, y1; |
Gerd Hoffmann | 1562e53 | 2013-03-06 13:40:47 +0100 | [diff] [blame] | 519 | int x = s->x; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 520 | |
Gerd Hoffmann | 1562e53 | 2013-03-06 13:40:47 +0100 | [diff] [blame] | 521 | if (s->ds->have_text) { |
| 522 | s->cursor_invalidate = 1; |
| 523 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 524 | |
Gerd Hoffmann | b35e3ba | 2014-05-22 11:01:42 +0200 | [diff] [blame] | 525 | if (x >= s->width) { |
| 526 | x = s->width - 1; |
| 527 | } |
| 528 | y1 = (s->y_base + s->y) % s->total_height; |
| 529 | y = y1 - s->y_displayed; |
| 530 | if (y < 0) { |
| 531 | y += s->total_height; |
| 532 | } |
| 533 | if (y < s->height) { |
| 534 | c = &s->cells[y1 * s->width + x]; |
Gerd Hoffmann | aea7947 | 2014-05-22 11:27:13 +0200 | [diff] [blame] | 535 | if (show && cursor_visible_phase) { |
Gerd Hoffmann | b35e3ba | 2014-05-22 11:01:42 +0200 | [diff] [blame] | 536 | TextAttributes t_attrib = s->t_attrib_default; |
| 537 | t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */ |
| 538 | vga_putcharxy(s, x, y, c->ch, &t_attrib); |
| 539 | } else { |
| 540 | vga_putcharxy(s, x, y, c->ch, &(c->t_attrib)); |
ths | ed8276a | 2007-02-10 22:37:56 +0000 | [diff] [blame] | 541 | } |
Gerd Hoffmann | b35e3ba | 2014-05-22 11:01:42 +0200 | [diff] [blame] | 542 | invalidate_xy(s, x, y); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 546 | static void console_refresh(QemuConsole *s) |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 547 | { |
Gerd Hoffmann | 1562e53 | 2013-03-06 13:40:47 +0100 | [diff] [blame] | 548 | DisplaySurface *surface = qemu_console_surface(s); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 549 | TextCell *c; |
| 550 | int x, y, y1; |
| 551 | |
Gerd Hoffmann | a93a4a2 | 2012-09-28 15:02:08 +0200 | [diff] [blame] | 552 | if (s->ds->have_text) { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 553 | s->text_x[0] = 0; |
| 554 | s->text_y[0] = 0; |
| 555 | s->text_x[1] = s->width - 1; |
| 556 | s->text_y[1] = s->height - 1; |
| 557 | s->cursor_invalidate = 1; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 558 | } |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 559 | |
Gerd Hoffmann | b35e3ba | 2014-05-22 11:01:42 +0200 | [diff] [blame] | 560 | vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface), |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 561 | color_table_rgb[0][QEMU_COLOR_BLACK]); |
Gerd Hoffmann | b35e3ba | 2014-05-22 11:01:42 +0200 | [diff] [blame] | 562 | y1 = s->y_displayed; |
| 563 | for (y = 0; y < s->height; y++) { |
| 564 | c = s->cells + y1 * s->width; |
| 565 | for (x = 0; x < s->width; x++) { |
| 566 | vga_putcharxy(s, x, y, c->ch, |
| 567 | &(c->t_attrib)); |
| 568 | c++; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 569 | } |
Gerd Hoffmann | b35e3ba | 2014-05-22 11:01:42 +0200 | [diff] [blame] | 570 | if (++y1 == s->total_height) { |
| 571 | y1 = 0; |
| 572 | } |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 573 | } |
Gerd Hoffmann | b35e3ba | 2014-05-22 11:01:42 +0200 | [diff] [blame] | 574 | console_show_cursor(s, 1); |
| 575 | dpy_gfx_update(s, 0, 0, |
| 576 | surface_width(surface), surface_height(surface)); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 579 | static void console_scroll(QemuConsole *s, int ydelta) |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 580 | { |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 581 | int i, y1; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 582 | |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 583 | if (ydelta > 0) { |
| 584 | for(i = 0; i < ydelta; i++) { |
| 585 | if (s->y_displayed == s->y_base) |
| 586 | break; |
| 587 | if (++s->y_displayed == s->total_height) |
| 588 | s->y_displayed = 0; |
| 589 | } |
| 590 | } else { |
| 591 | ydelta = -ydelta; |
| 592 | i = s->backscroll_height; |
| 593 | if (i > s->total_height - s->height) |
| 594 | i = s->total_height - s->height; |
| 595 | y1 = s->y_base - i; |
| 596 | if (y1 < 0) |
| 597 | y1 += s->total_height; |
| 598 | for(i = 0; i < ydelta; i++) { |
| 599 | if (s->y_displayed == y1) |
| 600 | break; |
| 601 | if (--s->y_displayed < 0) |
| 602 | s->y_displayed = s->total_height - 1; |
| 603 | } |
| 604 | } |
| 605 | console_refresh(s); |
| 606 | } |
| 607 | |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 608 | static void console_put_lf(QemuConsole *s) |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 609 | { |
| 610 | TextCell *c; |
| 611 | int x, y1; |
| 612 | |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 613 | s->y++; |
| 614 | if (s->y >= s->height) { |
| 615 | s->y = s->height - 1; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 616 | |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 617 | if (s->y_displayed == s->y_base) { |
| 618 | if (++s->y_displayed == s->total_height) |
| 619 | s->y_displayed = 0; |
| 620 | } |
| 621 | if (++s->y_base == s->total_height) |
| 622 | s->y_base = 0; |
| 623 | if (s->backscroll_height < s->total_height) |
| 624 | s->backscroll_height++; |
| 625 | y1 = (s->y_base + s->height - 1) % s->total_height; |
| 626 | c = &s->cells[y1 * s->width]; |
| 627 | for(x = 0; x < s->width; x++) { |
| 628 | c->ch = ' '; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 629 | c->t_attrib = s->t_attrib_default; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 630 | c++; |
| 631 | } |
Gerd Hoffmann | b35e3ba | 2014-05-22 11:01:42 +0200 | [diff] [blame] | 632 | if (s->y_displayed == s->y_base) { |
Gerd Hoffmann | 1562e53 | 2013-03-06 13:40:47 +0100 | [diff] [blame] | 633 | if (s->ds->have_text) { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 634 | s->text_x[0] = 0; |
| 635 | s->text_y[0] = 0; |
| 636 | s->text_x[1] = s->width - 1; |
| 637 | s->text_y[1] = s->height - 1; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Gerd Hoffmann | b35e3ba | 2014-05-22 11:01:42 +0200 | [diff] [blame] | 640 | vga_bitblt(s, 0, FONT_HEIGHT, 0, 0, |
| 641 | s->width * FONT_WIDTH, |
| 642 | (s->height - 1) * FONT_HEIGHT); |
| 643 | vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT, |
| 644 | s->width * FONT_WIDTH, FONT_HEIGHT, |
| 645 | color_table_rgb[0][s->t_attrib_default.bgcol]); |
| 646 | s->update_x0 = 0; |
| 647 | s->update_y0 = 0; |
| 648 | s->update_x1 = s->width * FONT_WIDTH; |
| 649 | s->update_y1 = s->height * FONT_HEIGHT; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 654 | /* Set console attributes depending on the current escape codes. |
| 655 | * NOTE: I know this code is not very efficient (checking every color for it |
| 656 | * self) but it is more readable and better maintainable. |
| 657 | */ |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 658 | static void console_handle_escape(QemuConsole *s) |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 659 | { |
| 660 | int i; |
| 661 | |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 662 | for (i=0; i<s->nb_esc_params; i++) { |
| 663 | switch (s->esc_params[i]) { |
| 664 | case 0: /* reset all console attributes to default */ |
| 665 | s->t_attrib = s->t_attrib_default; |
| 666 | break; |
| 667 | case 1: |
| 668 | s->t_attrib.bold = 1; |
| 669 | break; |
| 670 | case 4: |
| 671 | s->t_attrib.uline = 1; |
| 672 | break; |
| 673 | case 5: |
| 674 | s->t_attrib.blink = 1; |
| 675 | break; |
| 676 | case 7: |
| 677 | s->t_attrib.invers = 1; |
| 678 | break; |
| 679 | case 8: |
| 680 | s->t_attrib.unvisible = 1; |
| 681 | break; |
| 682 | case 22: |
| 683 | s->t_attrib.bold = 0; |
| 684 | break; |
| 685 | case 24: |
| 686 | s->t_attrib.uline = 0; |
| 687 | break; |
| 688 | case 25: |
| 689 | s->t_attrib.blink = 0; |
| 690 | break; |
| 691 | case 27: |
| 692 | s->t_attrib.invers = 0; |
| 693 | break; |
| 694 | case 28: |
| 695 | s->t_attrib.unvisible = 0; |
| 696 | break; |
| 697 | /* set foreground color */ |
| 698 | case 30: |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 699 | s->t_attrib.fgcol = QEMU_COLOR_BLACK; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 700 | break; |
| 701 | case 31: |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 702 | s->t_attrib.fgcol = QEMU_COLOR_RED; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 703 | break; |
| 704 | case 32: |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 705 | s->t_attrib.fgcol = QEMU_COLOR_GREEN; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 706 | break; |
| 707 | case 33: |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 708 | s->t_attrib.fgcol = QEMU_COLOR_YELLOW; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 709 | break; |
| 710 | case 34: |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 711 | s->t_attrib.fgcol = QEMU_COLOR_BLUE; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 712 | break; |
| 713 | case 35: |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 714 | s->t_attrib.fgcol = QEMU_COLOR_MAGENTA; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 715 | break; |
| 716 | case 36: |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 717 | s->t_attrib.fgcol = QEMU_COLOR_CYAN; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 718 | break; |
| 719 | case 37: |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 720 | s->t_attrib.fgcol = QEMU_COLOR_WHITE; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 721 | break; |
| 722 | /* set background color */ |
| 723 | case 40: |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 724 | s->t_attrib.bgcol = QEMU_COLOR_BLACK; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 725 | break; |
| 726 | case 41: |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 727 | s->t_attrib.bgcol = QEMU_COLOR_RED; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 728 | break; |
| 729 | case 42: |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 730 | s->t_attrib.bgcol = QEMU_COLOR_GREEN; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 731 | break; |
| 732 | case 43: |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 733 | s->t_attrib.bgcol = QEMU_COLOR_YELLOW; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 734 | break; |
| 735 | case 44: |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 736 | s->t_attrib.bgcol = QEMU_COLOR_BLUE; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 737 | break; |
| 738 | case 45: |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 739 | s->t_attrib.bgcol = QEMU_COLOR_MAGENTA; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 740 | break; |
| 741 | case 46: |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 742 | s->t_attrib.bgcol = QEMU_COLOR_CYAN; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 743 | break; |
| 744 | case 47: |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 745 | s->t_attrib.bgcol = QEMU_COLOR_WHITE; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 746 | break; |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 751 | static void console_clear_xy(QemuConsole *s, int x, int y) |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 752 | { |
| 753 | int y1 = (s->y_base + y) % s->total_height; |
| 754 | TextCell *c = &s->cells[y1 * s->width + x]; |
| 755 | c->ch = ' '; |
| 756 | c->t_attrib = s->t_attrib_default; |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 757 | update_xy(s, x, y); |
| 758 | } |
| 759 | |
Ren Kimura | 58aa7d8 | 2016-03-09 04:51:21 +0900 | [diff] [blame] | 760 | static void console_put_one(QemuConsole *s, int ch) |
| 761 | { |
| 762 | TextCell *c; |
| 763 | int y1; |
| 764 | if (s->x >= s->width) { |
| 765 | /* line wrap */ |
| 766 | s->x = 0; |
| 767 | console_put_lf(s); |
| 768 | } |
| 769 | y1 = (s->y_base + s->y) % s->total_height; |
| 770 | c = &s->cells[y1 * s->width + s->x]; |
| 771 | c->ch = ch; |
| 772 | c->t_attrib = s->t_attrib; |
| 773 | update_xy(s, s->x, s->y); |
| 774 | s->x++; |
| 775 | } |
| 776 | |
| 777 | static void console_respond_str(QemuConsole *s, const char *buf) |
| 778 | { |
| 779 | while (*buf) { |
| 780 | console_put_one(s, *buf); |
| 781 | buf++; |
| 782 | } |
| 783 | } |
| 784 | |
Ian Campbell | 3eea549 | 2012-09-04 10:26:09 -0500 | [diff] [blame] | 785 | /* set cursor, checking bounds */ |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 786 | static void set_cursor(QemuConsole *s, int x, int y) |
Ian Campbell | 3eea549 | 2012-09-04 10:26:09 -0500 | [diff] [blame] | 787 | { |
| 788 | if (x < 0) { |
| 789 | x = 0; |
| 790 | } |
| 791 | if (y < 0) { |
| 792 | y = 0; |
| 793 | } |
| 794 | if (y >= s->height) { |
| 795 | y = s->height - 1; |
| 796 | } |
| 797 | if (x >= s->width) { |
| 798 | x = s->width - 1; |
| 799 | } |
| 800 | |
| 801 | s->x = x; |
| 802 | s->y = y; |
| 803 | } |
| 804 | |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 805 | static void console_putchar(QemuConsole *s, int ch) |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 806 | { |
Ren Kimura | 58aa7d8 | 2016-03-09 04:51:21 +0900 | [diff] [blame] | 807 | int i; |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 808 | int x, y; |
Ren Kimura | 58aa7d8 | 2016-03-09 04:51:21 +0900 | [diff] [blame] | 809 | char response[40]; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 810 | |
| 811 | switch(s->state) { |
| 812 | case TTY_STATE_NORM: |
| 813 | switch(ch) { |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 814 | case '\r': /* carriage return */ |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 815 | s->x = 0; |
| 816 | break; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 817 | case '\n': /* newline */ |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 818 | console_put_lf(s); |
| 819 | break; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 820 | case '\b': /* backspace */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 821 | if (s->x > 0) |
bellard | e15d737 | 2006-06-25 16:26:29 +0000 | [diff] [blame] | 822 | s->x--; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 823 | break; |
| 824 | case '\t': /* tabspace */ |
| 825 | if (s->x + (8 - (s->x % 8)) > s->width) { |
bellard | bd46884 | 2006-07-14 20:24:31 +0000 | [diff] [blame] | 826 | s->x = 0; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 827 | console_put_lf(s); |
| 828 | } else { |
| 829 | s->x = s->x + (8 - (s->x % 8)); |
| 830 | } |
| 831 | break; |
| 832 | case '\a': /* alert aka. bell */ |
| 833 | /* TODO: has to be implemented */ |
| 834 | break; |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 835 | case 14: |
| 836 | /* SI (shift in), character set 0 (ignored) */ |
| 837 | break; |
| 838 | case 15: |
| 839 | /* SO (shift out), character set 1 (ignored) */ |
| 840 | break; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 841 | case 27: /* esc (introducing an escape sequence) */ |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 842 | s->state = TTY_STATE_ESC; |
| 843 | break; |
| 844 | default: |
Ren Kimura | 58aa7d8 | 2016-03-09 04:51:21 +0900 | [diff] [blame] | 845 | console_put_one(s, ch); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 846 | break; |
| 847 | } |
| 848 | break; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 849 | case TTY_STATE_ESC: /* check if it is a terminal escape sequence */ |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 850 | if (ch == '[') { |
| 851 | for(i=0;i<MAX_ESC_PARAMS;i++) |
| 852 | s->esc_params[i] = 0; |
| 853 | s->nb_esc_params = 0; |
| 854 | s->state = TTY_STATE_CSI; |
| 855 | } else { |
| 856 | s->state = TTY_STATE_NORM; |
| 857 | } |
| 858 | break; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 859 | case TTY_STATE_CSI: /* handle escape sequence parameters */ |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 860 | if (ch >= '0' && ch <= '9') { |
| 861 | if (s->nb_esc_params < MAX_ESC_PARAMS) { |
Laszlo Ersek | c10600a | 2012-09-17 11:10:03 +0200 | [diff] [blame] | 862 | int *param = &s->esc_params[s->nb_esc_params]; |
| 863 | int digit = (ch - '0'); |
| 864 | |
| 865 | *param = (*param <= (INT_MAX - digit) / 10) ? |
| 866 | *param * 10 + digit : INT_MAX; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 867 | } |
| 868 | } else { |
Ian Campbell | 3eea549 | 2012-09-04 10:26:09 -0500 | [diff] [blame] | 869 | if (s->nb_esc_params < MAX_ESC_PARAMS) |
| 870 | s->nb_esc_params++; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 871 | if (ch == ';') |
| 872 | break; |
Stefan Weil | 5d28b0e | 2013-11-10 16:04:16 +0100 | [diff] [blame] | 873 | trace_console_putchar_csi(s->esc_params[0], s->esc_params[1], |
| 874 | ch, s->nb_esc_params); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 875 | s->state = TTY_STATE_NORM; |
| 876 | switch(ch) { |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 877 | case 'A': |
| 878 | /* move cursor up */ |
| 879 | if (s->esc_params[0] == 0) { |
| 880 | s->esc_params[0] = 1; |
| 881 | } |
Ian Campbell | 3eea549 | 2012-09-04 10:26:09 -0500 | [diff] [blame] | 882 | set_cursor(s, s->x, s->y - s->esc_params[0]); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 883 | break; |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 884 | case 'B': |
| 885 | /* move cursor down */ |
| 886 | if (s->esc_params[0] == 0) { |
| 887 | s->esc_params[0] = 1; |
| 888 | } |
Ian Campbell | 3eea549 | 2012-09-04 10:26:09 -0500 | [diff] [blame] | 889 | set_cursor(s, s->x, s->y + s->esc_params[0]); |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 890 | break; |
| 891 | case 'C': |
| 892 | /* move cursor right */ |
| 893 | if (s->esc_params[0] == 0) { |
| 894 | s->esc_params[0] = 1; |
| 895 | } |
Ian Campbell | 3eea549 | 2012-09-04 10:26:09 -0500 | [diff] [blame] | 896 | set_cursor(s, s->x + s->esc_params[0], s->y); |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 897 | break; |
| 898 | case 'D': |
| 899 | /* move cursor left */ |
| 900 | if (s->esc_params[0] == 0) { |
| 901 | s->esc_params[0] = 1; |
| 902 | } |
Ian Campbell | 3eea549 | 2012-09-04 10:26:09 -0500 | [diff] [blame] | 903 | set_cursor(s, s->x - s->esc_params[0], s->y); |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 904 | break; |
| 905 | case 'G': |
| 906 | /* move cursor to column */ |
Ian Campbell | 3eea549 | 2012-09-04 10:26:09 -0500 | [diff] [blame] | 907 | set_cursor(s, s->esc_params[0] - 1, s->y); |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 908 | break; |
| 909 | case 'f': |
| 910 | case 'H': |
| 911 | /* move cursor to row, column */ |
Ian Campbell | 3eea549 | 2012-09-04 10:26:09 -0500 | [diff] [blame] | 912 | set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1); |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 913 | break; |
| 914 | case 'J': |
| 915 | switch (s->esc_params[0]) { |
| 916 | case 0: |
| 917 | /* clear to end of screen */ |
| 918 | for (y = s->y; y < s->height; y++) { |
| 919 | for (x = 0; x < s->width; x++) { |
| 920 | if (y == s->y && x < s->x) { |
| 921 | continue; |
| 922 | } |
| 923 | console_clear_xy(s, x, y); |
| 924 | } |
| 925 | } |
| 926 | break; |
| 927 | case 1: |
| 928 | /* clear from beginning of screen */ |
| 929 | for (y = 0; y <= s->y; y++) { |
| 930 | for (x = 0; x < s->width; x++) { |
| 931 | if (y == s->y && x > s->x) { |
| 932 | break; |
| 933 | } |
| 934 | console_clear_xy(s, x, y); |
| 935 | } |
| 936 | } |
| 937 | break; |
| 938 | case 2: |
| 939 | /* clear entire screen */ |
| 940 | for (y = 0; y <= s->height; y++) { |
| 941 | for (x = 0; x < s->width; x++) { |
| 942 | console_clear_xy(s, x, y); |
| 943 | } |
| 944 | } |
Markus Armbruster | f94a950 | 2011-11-22 11:59:06 +0100 | [diff] [blame] | 945 | break; |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 946 | } |
Markus Armbruster | 95d8f9f | 2011-11-22 11:59:07 +0100 | [diff] [blame] | 947 | break; |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 948 | case 'K': |
| 949 | switch (s->esc_params[0]) { |
| 950 | case 0: |
Markus Armbruster | f94a950 | 2011-11-22 11:59:06 +0100 | [diff] [blame] | 951 | /* clear to eol */ |
| 952 | for(x = s->x; x < s->width; x++) { |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 953 | console_clear_xy(s, x, s->y); |
Markus Armbruster | f94a950 | 2011-11-22 11:59:06 +0100 | [diff] [blame] | 954 | } |
| 955 | break; |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 956 | case 1: |
| 957 | /* clear from beginning of line */ |
| 958 | for (x = 0; x <= s->x; x++) { |
| 959 | console_clear_xy(s, x, s->y); |
| 960 | } |
| 961 | break; |
| 962 | case 2: |
| 963 | /* clear entire line */ |
| 964 | for(x = 0; x < s->width; x++) { |
| 965 | console_clear_xy(s, x, s->y); |
| 966 | } |
Markus Armbruster | f94a950 | 2011-11-22 11:59:06 +0100 | [diff] [blame] | 967 | break; |
| 968 | } |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 969 | break; |
| 970 | case 'm': |
Markus Armbruster | f94a950 | 2011-11-22 11:59:06 +0100 | [diff] [blame] | 971 | console_handle_escape(s); |
| 972 | break; |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 973 | case 'n': |
Ren Kimura | 58aa7d8 | 2016-03-09 04:51:21 +0900 | [diff] [blame] | 974 | switch (s->esc_params[0]) { |
| 975 | case 5: |
| 976 | /* report console status (always succeed)*/ |
| 977 | console_respond_str(s, "\033[0n"); |
| 978 | break; |
| 979 | case 6: |
| 980 | /* report cursor position */ |
| 981 | sprintf(response, "\033[%d;%dR", |
| 982 | (s->y_base + s->y) % s->total_height + 1, |
| 983 | s->x + 1); |
| 984 | console_respond_str(s, response); |
| 985 | break; |
| 986 | } |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 987 | break; |
| 988 | case 's': |
| 989 | /* save cursor position */ |
| 990 | s->x_saved = s->x; |
| 991 | s->y_saved = s->y; |
| 992 | break; |
| 993 | case 'u': |
| 994 | /* restore cursor position */ |
| 995 | s->x = s->x_saved; |
| 996 | s->y = s->y_saved; |
| 997 | break; |
| 998 | default: |
Stefan Weil | 5d28b0e | 2013-11-10 16:04:16 +0100 | [diff] [blame] | 999 | trace_console_putchar_unhandled(ch); |
ths | adb4796 | 2007-01-16 23:02:36 +0000 | [diff] [blame] | 1000 | break; |
| 1001 | } |
| 1002 | break; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | void console_select(unsigned int index) |
| 1008 | { |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1009 | DisplayChangeListener *dcl; |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 1010 | QemuConsole *s; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 1011 | |
Gerd Hoffmann | 437fe10 | 2013-03-07 16:04:52 +0100 | [diff] [blame] | 1012 | trace_console_select(index); |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1013 | s = qemu_console_lookup_by_index(index); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1014 | if (s) { |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 1015 | DisplayState *ds = s->ds; |
Jan Kiszka | bf1bed8 | 2012-07-10 22:00:55 +0200 | [diff] [blame] | 1016 | |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1017 | active_console = s; |
Gerd Hoffmann | a93a4a2 | 2012-09-28 15:02:08 +0200 | [diff] [blame] | 1018 | if (ds->have_gfx) { |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1019 | QLIST_FOREACH(dcl, &ds->listeners, next) { |
| 1020 | if (dcl->con != NULL) { |
| 1021 | continue; |
| 1022 | } |
| 1023 | if (dcl->ops->dpy_gfx_switch) { |
| 1024 | dcl->ops->dpy_gfx_switch(dcl, s->surface); |
| 1025 | } |
| 1026 | } |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 1027 | dpy_gfx_update(s, 0, 0, surface_width(s->surface), |
| 1028 | surface_height(s->surface)); |
Gerd Hoffmann | a93a4a2 | 2012-09-28 15:02:08 +0200 | [diff] [blame] | 1029 | } |
| 1030 | if (ds->have_text) { |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1031 | dpy_text_resize(s, s->width, s->height); |
aliguori | 68f0099 | 2009-01-21 18:59:12 +0000 | [diff] [blame] | 1032 | } |
Gerd Hoffmann | aea7947 | 2014-05-22 11:27:13 +0200 | [diff] [blame] | 1033 | text_console_update_cursor(NULL); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | static int console_puts(CharDriverState *chr, const uint8_t *buf, int len) |
| 1038 | { |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 1039 | QemuConsole *s = chr->opaque; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1040 | int i; |
| 1041 | |
pbrook | 14778c2 | 2009-01-21 03:02:52 +0000 | [diff] [blame] | 1042 | s->update_x0 = s->width * FONT_WIDTH; |
| 1043 | s->update_y0 = s->height * FONT_HEIGHT; |
| 1044 | s->update_x1 = 0; |
| 1045 | s->update_y1 = 0; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1046 | console_show_cursor(s, 0); |
| 1047 | for(i = 0; i < len; i++) { |
| 1048 | console_putchar(s, buf[i]); |
| 1049 | } |
| 1050 | console_show_cursor(s, 1); |
Gerd Hoffmann | a93a4a2 | 2012-09-28 15:02:08 +0200 | [diff] [blame] | 1051 | if (s->ds->have_gfx && s->update_x0 < s->update_x1) { |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1052 | dpy_gfx_update(s, s->update_x0, s->update_y0, |
Gerd Hoffmann | a93a4a2 | 2012-09-28 15:02:08 +0200 | [diff] [blame] | 1053 | s->update_x1 - s->update_x0, |
| 1054 | s->update_y1 - s->update_y0); |
pbrook | 14778c2 | 2009-01-21 03:02:52 +0000 | [diff] [blame] | 1055 | } |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1056 | return len; |
| 1057 | } |
| 1058 | |
bellard | e15d737 | 2006-06-25 16:26:29 +0000 | [diff] [blame] | 1059 | static void kbd_send_chars(void *opaque) |
| 1060 | { |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 1061 | QemuConsole *s = opaque; |
bellard | e15d737 | 2006-06-25 16:26:29 +0000 | [diff] [blame] | 1062 | int len; |
| 1063 | uint8_t buf[16]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1064 | |
Anthony Liguori | 909cda1 | 2011-08-15 11:17:31 -0500 | [diff] [blame] | 1065 | len = qemu_chr_be_can_write(s->chr); |
bellard | e15d737 | 2006-06-25 16:26:29 +0000 | [diff] [blame] | 1066 | if (len > s->out_fifo.count) |
| 1067 | len = s->out_fifo.count; |
| 1068 | if (len > 0) { |
| 1069 | if (len > sizeof(buf)) |
| 1070 | len = sizeof(buf); |
| 1071 | qemu_fifo_read(&s->out_fifo, buf, len); |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 1072 | qemu_chr_be_write(s->chr, buf, len); |
bellard | e15d737 | 2006-06-25 16:26:29 +0000 | [diff] [blame] | 1073 | } |
| 1074 | /* characters are pending: we send them a bit later (XXX: |
| 1075 | horrible, should change char device API) */ |
| 1076 | if (s->out_fifo.count > 0) { |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 1077 | timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1); |
bellard | e15d737 | 2006-06-25 16:26:29 +0000 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1081 | /* called when an ascii key is pressed */ |
Gerd Hoffmann | 3f9a6e8 | 2014-05-22 12:05:52 +0200 | [diff] [blame] | 1082 | void kbd_put_keysym_console(QemuConsole *s, int keysym) |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1083 | { |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1084 | uint8_t buf[16], *q; |
| 1085 | int c; |
| 1086 | |
ths | af3a903 | 2007-07-11 23:14:59 +0000 | [diff] [blame] | 1087 | if (!s || (s->console_type == GRAPHIC_CONSOLE)) |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1088 | return; |
| 1089 | |
| 1090 | switch(keysym) { |
| 1091 | case QEMU_KEY_CTRL_UP: |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 1092 | console_scroll(s, -1); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1093 | break; |
| 1094 | case QEMU_KEY_CTRL_DOWN: |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 1095 | console_scroll(s, 1); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1096 | break; |
| 1097 | case QEMU_KEY_CTRL_PAGEUP: |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 1098 | console_scroll(s, -10); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1099 | break; |
| 1100 | case QEMU_KEY_CTRL_PAGEDOWN: |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 1101 | console_scroll(s, 10); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1102 | break; |
| 1103 | default: |
bellard | e15d737 | 2006-06-25 16:26:29 +0000 | [diff] [blame] | 1104 | /* convert the QEMU keysym to VT100 key string */ |
| 1105 | q = buf; |
| 1106 | if (keysym >= 0xe100 && keysym <= 0xe11f) { |
| 1107 | *q++ = '\033'; |
| 1108 | *q++ = '['; |
| 1109 | c = keysym - 0xe100; |
| 1110 | if (c >= 10) |
| 1111 | *q++ = '0' + (c / 10); |
| 1112 | *q++ = '0' + (c % 10); |
| 1113 | *q++ = '~'; |
| 1114 | } else if (keysym >= 0xe120 && keysym <= 0xe17f) { |
| 1115 | *q++ = '\033'; |
| 1116 | *q++ = '['; |
| 1117 | *q++ = keysym & 0xff; |
Paolo Bonzini | 4104833 | 2010-12-23 13:42:52 +0100 | [diff] [blame] | 1118 | } else if (s->echo && (keysym == '\r' || keysym == '\n')) { |
| 1119 | console_puts(s->chr, (const uint8_t *) "\r", 1); |
| 1120 | *q++ = '\n'; |
bellard | e15d737 | 2006-06-25 16:26:29 +0000 | [diff] [blame] | 1121 | } else { |
Paolo Bonzini | 4104833 | 2010-12-23 13:42:52 +0100 | [diff] [blame] | 1122 | *q++ = keysym; |
| 1123 | } |
| 1124 | if (s->echo) { |
| 1125 | console_puts(s->chr, buf, q - buf); |
bellard | e15d737 | 2006-06-25 16:26:29 +0000 | [diff] [blame] | 1126 | } |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1127 | if (s->chr->chr_read) { |
bellard | e15d737 | 2006-06-25 16:26:29 +0000 | [diff] [blame] | 1128 | qemu_fifo_write(&s->out_fifo, buf, q - buf); |
| 1129 | kbd_send_chars(s); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1130 | } |
| 1131 | break; |
| 1132 | } |
| 1133 | } |
| 1134 | |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 1135 | static const int qcode_to_keysym[Q_KEY_CODE__MAX] = { |
Gerd Hoffmann | 50ef467 | 2014-05-27 09:28:38 +0200 | [diff] [blame] | 1136 | [Q_KEY_CODE_UP] = QEMU_KEY_UP, |
| 1137 | [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN, |
| 1138 | [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT, |
| 1139 | [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT, |
| 1140 | [Q_KEY_CODE_HOME] = QEMU_KEY_HOME, |
| 1141 | [Q_KEY_CODE_END] = QEMU_KEY_END, |
| 1142 | [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP, |
| 1143 | [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN, |
| 1144 | [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE, |
Thomas Huth | 344aa28 | 2016-08-11 09:21:00 +0200 | [diff] [blame] | 1145 | [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE, |
Gerd Hoffmann | 50ef467 | 2014-05-27 09:28:38 +0200 | [diff] [blame] | 1146 | }; |
| 1147 | |
| 1148 | bool kbd_put_qcode_console(QemuConsole *s, int qcode) |
| 1149 | { |
| 1150 | int keysym; |
| 1151 | |
| 1152 | keysym = qcode_to_keysym[qcode]; |
| 1153 | if (keysym == 0) { |
| 1154 | return false; |
| 1155 | } |
| 1156 | kbd_put_keysym_console(s, keysym); |
| 1157 | return true; |
| 1158 | } |
| 1159 | |
Gerd Hoffmann | bdef972 | 2014-05-27 09:32:36 +0200 | [diff] [blame] | 1160 | void kbd_put_string_console(QemuConsole *s, const char *str, int len) |
| 1161 | { |
| 1162 | int i; |
| 1163 | |
| 1164 | for (i = 0; i < len && str[i]; i++) { |
| 1165 | kbd_put_keysym_console(s, str[i]); |
| 1166 | } |
| 1167 | } |
| 1168 | |
Gerd Hoffmann | 3f9a6e8 | 2014-05-22 12:05:52 +0200 | [diff] [blame] | 1169 | void kbd_put_keysym(int keysym) |
| 1170 | { |
| 1171 | kbd_put_keysym_console(active_console, keysym); |
| 1172 | } |
| 1173 | |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 1174 | static void text_console_invalidate(void *opaque) |
| 1175 | { |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 1176 | QemuConsole *s = (QemuConsole *) opaque; |
Gerd Hoffmann | 1562e53 | 2013-03-06 13:40:47 +0100 | [diff] [blame] | 1177 | |
| 1178 | if (s->ds->have_text && s->console_type == TEXT_CONSOLE) { |
aliguori | 68f0099 | 2009-01-21 18:59:12 +0000 | [diff] [blame] | 1179 | text_console_resize(s); |
| 1180 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 1181 | console_refresh(s); |
| 1182 | } |
| 1183 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1184 | static void text_console_update(void *opaque, console_ch_t *chardata) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 1185 | { |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 1186 | QemuConsole *s = (QemuConsole *) opaque; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 1187 | int i, j, src; |
| 1188 | |
| 1189 | if (s->text_x[0] <= s->text_x[1]) { |
| 1190 | src = (s->y_base + s->text_y[0]) * s->width; |
| 1191 | chardata += s->text_y[0] * s->width; |
| 1192 | for (i = s->text_y[0]; i <= s->text_y[1]; i ++) |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 1193 | for (j = 0; j < s->width; j++, src++) { |
| 1194 | console_write_ch(chardata ++, |
| 1195 | ATTR2CHTYPE(s->cells[src].ch, |
| 1196 | s->cells[src].t_attrib.fgcol, |
| 1197 | s->cells[src].t_attrib.bgcol, |
| 1198 | s->cells[src].t_attrib.bold)); |
| 1199 | } |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1200 | dpy_text_update(s, s->text_x[0], s->text_y[0], |
Gerd Hoffmann | a93a4a2 | 2012-09-28 15:02:08 +0200 | [diff] [blame] | 1201 | s->text_x[1] - s->text_x[0], i - s->text_y[0]); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 1202 | s->text_x[0] = s->width; |
| 1203 | s->text_y[0] = s->height; |
| 1204 | s->text_x[1] = 0; |
| 1205 | s->text_y[1] = 0; |
| 1206 | } |
| 1207 | if (s->cursor_invalidate) { |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1208 | dpy_text_cursor(s, s->x, s->y); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 1209 | s->cursor_invalidate = 0; |
| 1210 | } |
| 1211 | } |
| 1212 | |
Kirill Batuzov | afff2b1 | 2014-04-24 18:15:58 +0400 | [diff] [blame] | 1213 | static QemuConsole *new_console(DisplayState *ds, console_type_t console_type, |
| 1214 | uint32_t head) |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1215 | { |
Gerd Hoffmann | 95be066 | 2013-04-17 09:45:10 +0200 | [diff] [blame] | 1216 | Object *obj; |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 1217 | QemuConsole *s; |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 1218 | int i; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1219 | |
Gerd Hoffmann | 95be066 | 2013-04-17 09:45:10 +0200 | [diff] [blame] | 1220 | obj = object_new(TYPE_QEMU_CONSOLE); |
| 1221 | s = QEMU_CONSOLE(obj); |
Kirill Batuzov | afff2b1 | 2014-04-24 18:15:58 +0400 | [diff] [blame] | 1222 | s->head = head; |
Gerd Hoffmann | aa2beaa | 2013-04-17 10:21:27 +0200 | [diff] [blame] | 1223 | object_property_add_link(obj, "device", TYPE_DEVICE, |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1224 | (Object **)&s->device, |
Stefan Hajnoczi | 39f72ef | 2014-03-19 08:58:56 +0100 | [diff] [blame] | 1225 | object_property_allow_set_link, |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1226 | OBJ_PROP_LINK_UNREF_ON_RELEASE, |
Kirill Batuzov | afff2b1 | 2014-04-24 18:15:58 +0400 | [diff] [blame] | 1227 | &error_abort); |
Gerd Hoffmann | 5643706 | 2014-01-24 15:35:21 +0100 | [diff] [blame] | 1228 | object_property_add_uint32_ptr(obj, "head", |
Kirill Batuzov | afff2b1 | 2014-04-24 18:15:58 +0400 | [diff] [blame] | 1229 | &s->head, &error_abort); |
Gerd Hoffmann | aa2beaa | 2013-04-17 10:21:27 +0200 | [diff] [blame] | 1230 | |
ths | af3a903 | 2007-07-11 23:14:59 +0000 | [diff] [blame] | 1231 | if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) && |
| 1232 | (console_type == GRAPHIC_CONSOLE))) { |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1233 | active_console = s; |
ths | af3a903 | 2007-07-11 23:14:59 +0000 | [diff] [blame] | 1234 | } |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1235 | s->ds = ds; |
ths | af3a903 | 2007-07-11 23:14:59 +0000 | [diff] [blame] | 1236 | s->console_type = console_type; |
Gerd Hoffmann | a1d2db0 | 2014-05-26 10:36:35 +0200 | [diff] [blame] | 1237 | |
| 1238 | consoles = g_realloc(consoles, sizeof(*consoles) * (nb_consoles+1)); |
ths | af3a903 | 2007-07-11 23:14:59 +0000 | [diff] [blame] | 1239 | if (console_type != GRAPHIC_CONSOLE) { |
Jan Kiszka | f81bdef | 2011-09-16 00:48:07 +0200 | [diff] [blame] | 1240 | s->index = nb_consoles; |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 1241 | consoles[nb_consoles++] = s; |
| 1242 | } else { |
| 1243 | /* HACK: Put graphical consoles before text consoles. */ |
| 1244 | for (i = nb_consoles; i > 0; i--) { |
ths | af3a903 | 2007-07-11 23:14:59 +0000 | [diff] [blame] | 1245 | if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE) |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 1246 | break; |
| 1247 | consoles[i] = consoles[i - 1]; |
Jan Kiszka | f81bdef | 2011-09-16 00:48:07 +0200 | [diff] [blame] | 1248 | consoles[i]->index = i; |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 1249 | } |
Jan Kiszka | f81bdef | 2011-09-16 00:48:07 +0200 | [diff] [blame] | 1250 | s->index = i; |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 1251 | consoles[i] = s; |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 1252 | nb_consoles++; |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 1253 | } |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1254 | return s; |
| 1255 | } |
| 1256 | |
Gerd Hoffmann | 30f1e66 | 2014-06-18 11:03:15 +0200 | [diff] [blame] | 1257 | static void qemu_alloc_display(DisplaySurface *surface, int width, int height) |
Jes Sorensen | ffe8b82 | 2011-03-16 13:33:30 +0100 | [diff] [blame] | 1258 | { |
Gerd Hoffmann | 69c7777 | 2012-09-26 15:20:05 +0200 | [diff] [blame] | 1259 | qemu_pixman_image_unref(surface->image); |
| 1260 | surface->image = NULL; |
Gerd Hoffmann | 69c7777 | 2012-09-26 15:20:05 +0200 | [diff] [blame] | 1261 | |
Gerd Hoffmann | 30f1e66 | 2014-06-18 11:03:15 +0200 | [diff] [blame] | 1262 | surface->format = PIXMAN_x8r8g8b8; |
Gerd Hoffmann | 69c7777 | 2012-09-26 15:20:05 +0200 | [diff] [blame] | 1263 | surface->image = pixman_image_create_bits(surface->format, |
| 1264 | width, height, |
Gerd Hoffmann | 30f1e66 | 2014-06-18 11:03:15 +0200 | [diff] [blame] | 1265 | NULL, width * 4); |
Gerd Hoffmann | 69c7777 | 2012-09-26 15:20:05 +0200 | [diff] [blame] | 1266 | assert(surface->image != NULL); |
| 1267 | |
Gerd Hoffmann | 30f1e66 | 2014-06-18 11:03:15 +0200 | [diff] [blame] | 1268 | surface->flags = QEMU_ALLOCATED_FLAG; |
Paolo Bonzini | 98b5008 | 2010-02-11 00:29:57 +0100 | [diff] [blame] | 1269 | } |
| 1270 | |
Gerd Hoffmann | da229ef | 2013-02-28 10:48:02 +0100 | [diff] [blame] | 1271 | DisplaySurface *qemu_create_displaysurface(int width, int height) |
Gerd Hoffmann | 537a439 | 2012-09-27 11:06:36 +0200 | [diff] [blame] | 1272 | { |
| 1273 | DisplaySurface *surface = g_new0(DisplaySurface, 1); |
Gerd Hoffmann | da229ef | 2013-02-28 10:48:02 +0100 | [diff] [blame] | 1274 | |
| 1275 | trace_displaysurface_create(surface, width, height); |
Gerd Hoffmann | 30f1e66 | 2014-06-18 11:03:15 +0200 | [diff] [blame] | 1276 | qemu_alloc_display(surface, width, height); |
Gerd Hoffmann | 537a439 | 2012-09-27 11:06:36 +0200 | [diff] [blame] | 1277 | return surface; |
| 1278 | } |
| 1279 | |
Gerd Hoffmann | 30f1e66 | 2014-06-18 11:03:15 +0200 | [diff] [blame] | 1280 | DisplaySurface *qemu_create_displaysurface_from(int width, int height, |
| 1281 | pixman_format_code_t format, |
| 1282 | int linesize, uint8_t *data) |
Paolo Bonzini | 98b5008 | 2010-02-11 00:29:57 +0100 | [diff] [blame] | 1283 | { |
Gerd Hoffmann | 69c7777 | 2012-09-26 15:20:05 +0200 | [diff] [blame] | 1284 | DisplaySurface *surface = g_new0(DisplaySurface, 1); |
Paolo Bonzini | 98b5008 | 2010-02-11 00:29:57 +0100 | [diff] [blame] | 1285 | |
Gerd Hoffmann | 30f1e66 | 2014-06-18 11:03:15 +0200 | [diff] [blame] | 1286 | trace_displaysurface_create_from(surface, width, height, format); |
| 1287 | surface->format = format; |
Gerd Hoffmann | 69c7777 | 2012-09-26 15:20:05 +0200 | [diff] [blame] | 1288 | surface->image = pixman_image_create_bits(surface->format, |
| 1289 | width, height, |
| 1290 | (void *)data, linesize); |
| 1291 | assert(surface->image != NULL); |
| 1292 | |
Paolo Bonzini | 98b5008 | 2010-02-11 00:29:57 +0100 | [diff] [blame] | 1293 | return surface; |
| 1294 | } |
| 1295 | |
Gerd Hoffmann | ca58b45 | 2016-04-01 10:27:20 +0200 | [diff] [blame] | 1296 | DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image) |
| 1297 | { |
| 1298 | DisplaySurface *surface = g_new0(DisplaySurface, 1); |
| 1299 | |
| 1300 | trace_displaysurface_create_pixman(surface); |
| 1301 | surface->format = pixman_image_get_format(image); |
| 1302 | surface->image = pixman_image_ref(image); |
| 1303 | |
| 1304 | return surface; |
| 1305 | } |
| 1306 | |
Gerd Hoffmann | a77549b | 2014-06-19 08:46:08 +0200 | [diff] [blame] | 1307 | static void qemu_unmap_displaysurface_guestmem(pixman_image_t *image, |
| 1308 | void *unused) |
| 1309 | { |
| 1310 | void *data = pixman_image_get_data(image); |
| 1311 | uint32_t size = pixman_image_get_stride(image) * |
| 1312 | pixman_image_get_height(image); |
| 1313 | cpu_physical_memory_unmap(data, size, 0, 0); |
| 1314 | } |
| 1315 | |
| 1316 | DisplaySurface *qemu_create_displaysurface_guestmem(int width, int height, |
| 1317 | pixman_format_code_t format, |
| 1318 | int linesize, uint64_t addr) |
| 1319 | { |
| 1320 | DisplaySurface *surface; |
| 1321 | hwaddr size; |
| 1322 | void *data; |
| 1323 | |
| 1324 | if (linesize == 0) { |
| 1325 | linesize = width * PIXMAN_FORMAT_BPP(format) / 8; |
| 1326 | } |
| 1327 | |
Gonglei | f76b84a | 2015-03-11 16:21:00 +0800 | [diff] [blame] | 1328 | size = (hwaddr)linesize * height; |
Gerd Hoffmann | a77549b | 2014-06-19 08:46:08 +0200 | [diff] [blame] | 1329 | data = cpu_physical_memory_map(addr, &size, 0); |
Gonglei | f76b84a | 2015-03-11 16:21:00 +0800 | [diff] [blame] | 1330 | if (size != (hwaddr)linesize * height) { |
Gerd Hoffmann | a77549b | 2014-06-19 08:46:08 +0200 | [diff] [blame] | 1331 | cpu_physical_memory_unmap(data, size, 0, 0); |
| 1332 | return NULL; |
| 1333 | } |
| 1334 | |
| 1335 | surface = qemu_create_displaysurface_from |
| 1336 | (width, height, format, linesize, data); |
| 1337 | pixman_image_set_destroy_function |
| 1338 | (surface->image, qemu_unmap_displaysurface_guestmem, NULL); |
| 1339 | |
| 1340 | return surface; |
| 1341 | } |
| 1342 | |
Gerd Hoffmann | 521a580 | 2013-04-25 12:10:45 +0200 | [diff] [blame] | 1343 | static DisplaySurface *qemu_create_message_surface(int w, int h, |
| 1344 | const char *msg) |
Gerd Hoffmann | d3002b0 | 2013-04-25 09:33:19 +0200 | [diff] [blame] | 1345 | { |
Gerd Hoffmann | 521a580 | 2013-04-25 12:10:45 +0200 | [diff] [blame] | 1346 | DisplaySurface *surface = qemu_create_displaysurface(w, h); |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 1347 | pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK]; |
| 1348 | pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE]; |
Gerd Hoffmann | d3002b0 | 2013-04-25 09:33:19 +0200 | [diff] [blame] | 1349 | pixman_image_t *glyph; |
| 1350 | int len, x, y, i; |
| 1351 | |
| 1352 | len = strlen(msg); |
Gerd Hoffmann | 521a580 | 2013-04-25 12:10:45 +0200 | [diff] [blame] | 1353 | x = (w / FONT_WIDTH - len) / 2; |
| 1354 | y = (h / FONT_HEIGHT - 1) / 2; |
Gerd Hoffmann | d3002b0 | 2013-04-25 09:33:19 +0200 | [diff] [blame] | 1355 | for (i = 0; i < len; i++) { |
| 1356 | glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]); |
| 1357 | qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg, |
| 1358 | x+i, y, FONT_WIDTH, FONT_HEIGHT); |
| 1359 | qemu_pixman_image_unref(glyph); |
| 1360 | } |
| 1361 | return surface; |
| 1362 | } |
| 1363 | |
Gerd Hoffmann | da229ef | 2013-02-28 10:48:02 +0100 | [diff] [blame] | 1364 | void qemu_free_displaysurface(DisplaySurface *surface) |
Paolo Bonzini | 98b5008 | 2010-02-11 00:29:57 +0100 | [diff] [blame] | 1365 | { |
Gerd Hoffmann | da229ef | 2013-02-28 10:48:02 +0100 | [diff] [blame] | 1366 | if (surface == NULL) { |
Paolo Bonzini | 98b5008 | 2010-02-11 00:29:57 +0100 | [diff] [blame] | 1367 | return; |
Gerd Hoffmann | 187cd1d | 2012-09-26 07:46:20 +0200 | [diff] [blame] | 1368 | } |
Gerd Hoffmann | da229ef | 2013-02-28 10:48:02 +0100 | [diff] [blame] | 1369 | trace_displaysurface_free(surface); |
| 1370 | qemu_pixman_image_unref(surface->image); |
| 1371 | g_free(surface); |
Paolo Bonzini | 98b5008 | 2010-02-11 00:29:57 +0100 | [diff] [blame] | 1372 | } |
| 1373 | |
Gerd Hoffmann | 06020b9 | 2014-07-11 13:56:51 +0200 | [diff] [blame] | 1374 | bool console_has_gl(QemuConsole *con) |
| 1375 | { |
| 1376 | return con->gl != NULL; |
| 1377 | } |
| 1378 | |
Gerd Hoffmann | 5209089 | 2013-04-23 15:44:31 +0200 | [diff] [blame] | 1379 | void register_displaychangelistener(DisplayChangeListener *dcl) |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1380 | { |
Gerd Hoffmann | 521a580 | 2013-04-25 12:10:45 +0200 | [diff] [blame] | 1381 | static const char nodev[] = |
| 1382 | "This VM has no graphic display device."; |
Gerd Hoffmann | d3002b0 | 2013-04-25 09:33:19 +0200 | [diff] [blame] | 1383 | static DisplaySurface *dummy; |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1384 | QemuConsole *con; |
| 1385 | |
Gerd Hoffmann | 06020b9 | 2014-07-11 13:56:51 +0200 | [diff] [blame] | 1386 | if (dcl->ops->dpy_gl_ctx_create) { |
| 1387 | /* display has opengl support */ |
| 1388 | assert(dcl->con); |
| 1389 | if (dcl->con->gl) { |
| 1390 | fprintf(stderr, "can't register two opengl displays (%s, %s)\n", |
| 1391 | dcl->ops->dpy_name, dcl->con->gl->ops->dpy_name); |
| 1392 | exit(1); |
| 1393 | } |
| 1394 | dcl->con->gl = dcl; |
| 1395 | } |
| 1396 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1397 | trace_displaychangelistener_register(dcl, dcl->ops->dpy_name); |
Gerd Hoffmann | 5209089 | 2013-04-23 15:44:31 +0200 | [diff] [blame] | 1398 | dcl->ds = get_alloc_displaystate(); |
| 1399 | QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next); |
| 1400 | gui_setup_refresh(dcl->ds); |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1401 | if (dcl->con) { |
| 1402 | dcl->con->dcls++; |
| 1403 | con = dcl->con; |
| 1404 | } else { |
| 1405 | con = active_console; |
| 1406 | } |
Gerd Hoffmann | d3002b0 | 2013-04-25 09:33:19 +0200 | [diff] [blame] | 1407 | if (dcl->ops->dpy_gfx_switch) { |
| 1408 | if (con) { |
| 1409 | dcl->ops->dpy_gfx_switch(dcl, con->surface); |
| 1410 | } else { |
| 1411 | if (!dummy) { |
Gerd Hoffmann | 521a580 | 2013-04-25 12:10:45 +0200 | [diff] [blame] | 1412 | dummy = qemu_create_message_surface(640, 480, nodev); |
Gerd Hoffmann | d3002b0 | 2013-04-25 09:33:19 +0200 | [diff] [blame] | 1413 | } |
| 1414 | dcl->ops->dpy_gfx_switch(dcl, dummy); |
| 1415 | } |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1416 | } |
Gerd Hoffmann | aea7947 | 2014-05-22 11:27:13 +0200 | [diff] [blame] | 1417 | text_console_update_cursor(NULL); |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1418 | } |
| 1419 | |
Gerd Hoffmann | 0f7b286 | 2013-03-14 11:56:16 +0100 | [diff] [blame] | 1420 | void update_displaychangelistener(DisplayChangeListener *dcl, |
| 1421 | uint64_t interval) |
| 1422 | { |
| 1423 | DisplayState *ds = dcl->ds; |
| 1424 | |
| 1425 | dcl->update_interval = interval; |
| 1426 | if (!ds->refreshing && ds->update_interval > interval) { |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 1427 | timer_mod(ds->gui_timer, ds->last_update + interval); |
Gerd Hoffmann | 0f7b286 | 2013-03-14 11:56:16 +0100 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1431 | void unregister_displaychangelistener(DisplayChangeListener *dcl) |
| 1432 | { |
| 1433 | DisplayState *ds = dcl->ds; |
| 1434 | trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name); |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1435 | if (dcl->con) { |
| 1436 | dcl->con->dcls--; |
| 1437 | } |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1438 | QLIST_REMOVE(dcl, next); |
| 1439 | gui_setup_refresh(ds); |
| 1440 | } |
| 1441 | |
Gerd Hoffmann | cf1ecc8 | 2015-03-12 12:51:13 +0100 | [diff] [blame] | 1442 | static void dpy_set_ui_info_timer(void *opaque) |
| 1443 | { |
| 1444 | QemuConsole *con = opaque; |
| 1445 | |
| 1446 | con->hw_ops->ui_info(con->hw, con->head, &con->ui_info); |
| 1447 | } |
| 1448 | |
Gerd Hoffmann | b7fb49f | 2015-03-13 12:21:14 +0100 | [diff] [blame] | 1449 | bool dpy_ui_info_supported(QemuConsole *con) |
| 1450 | { |
| 1451 | return con->hw_ops->ui_info != NULL; |
| 1452 | } |
| 1453 | |
Gerd Hoffmann | 6f90f3d | 2014-01-24 17:38:20 +0100 | [diff] [blame] | 1454 | int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info) |
| 1455 | { |
| 1456 | assert(con != NULL); |
Gerd Hoffmann | 1185fde | 2016-05-30 10:41:13 +0200 | [diff] [blame] | 1457 | |
Gerd Hoffmann | b7fb49f | 2015-03-13 12:21:14 +0100 | [diff] [blame] | 1458 | if (!dpy_ui_info_supported(con)) { |
Gerd Hoffmann | cf1ecc8 | 2015-03-12 12:51:13 +0100 | [diff] [blame] | 1459 | return -1; |
Gerd Hoffmann | 6f90f3d | 2014-01-24 17:38:20 +0100 | [diff] [blame] | 1460 | } |
Gerd Hoffmann | 1185fde | 2016-05-30 10:41:13 +0200 | [diff] [blame] | 1461 | if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) { |
| 1462 | /* nothing changed -- ignore */ |
| 1463 | return 0; |
| 1464 | } |
Gerd Hoffmann | cf1ecc8 | 2015-03-12 12:51:13 +0100 | [diff] [blame] | 1465 | |
| 1466 | /* |
| 1467 | * Typically we get a flood of these as the user resizes the window. |
| 1468 | * Wait until the dust has settled (one second without updates), then |
| 1469 | * go notify the guest. |
| 1470 | */ |
Gerd Hoffmann | 1185fde | 2016-05-30 10:41:13 +0200 | [diff] [blame] | 1471 | con->ui_info = *info; |
Gerd Hoffmann | cf1ecc8 | 2015-03-12 12:51:13 +0100 | [diff] [blame] | 1472 | timer_mod(con->ui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000); |
| 1473 | return 0; |
Gerd Hoffmann | 6f90f3d | 2014-01-24 17:38:20 +0100 | [diff] [blame] | 1474 | } |
| 1475 | |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1476 | void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h) |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1477 | { |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1478 | DisplayState *s = con->ds; |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1479 | DisplayChangeListener *dcl; |
Gerd Hoffmann | 06020b9 | 2014-07-11 13:56:51 +0200 | [diff] [blame] | 1480 | int width = w; |
| 1481 | int height = h; |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1482 | |
Gerd Hoffmann | 06020b9 | 2014-07-11 13:56:51 +0200 | [diff] [blame] | 1483 | if (con->surface) { |
| 1484 | width = surface_width(con->surface); |
| 1485 | height = surface_height(con->surface); |
| 1486 | } |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1487 | x = MAX(x, 0); |
| 1488 | y = MAX(y, 0); |
| 1489 | x = MIN(x, width); |
| 1490 | y = MIN(y, height); |
| 1491 | w = MIN(w, width - x); |
| 1492 | h = MIN(h, height - y); |
| 1493 | |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 1494 | if (!qemu_console_is_visible(con)) { |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 1495 | return; |
| 1496 | } |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1497 | QLIST_FOREACH(dcl, &s->listeners, next) { |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1498 | if (con != (dcl->con ? dcl->con : active_console)) { |
| 1499 | continue; |
| 1500 | } |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1501 | if (dcl->ops->dpy_gfx_update) { |
Gerd Hoffmann | bc2ed97 | 2013-03-01 13:03:04 +0100 | [diff] [blame] | 1502 | dcl->ops->dpy_gfx_update(dcl, x, y, w, h); |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1503 | } |
| 1504 | } |
| 1505 | } |
| 1506 | |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1507 | void dpy_gfx_replace_surface(QemuConsole *con, |
Gerd Hoffmann | da229ef | 2013-02-28 10:48:02 +0100 | [diff] [blame] | 1508 | DisplaySurface *surface) |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1509 | { |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1510 | DisplayState *s = con->ds; |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 1511 | DisplaySurface *old_surface = con->surface; |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1512 | DisplayChangeListener *dcl; |
Gerd Hoffmann | da229ef | 2013-02-28 10:48:02 +0100 | [diff] [blame] | 1513 | |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 1514 | con->surface = surface; |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1515 | QLIST_FOREACH(dcl, &s->listeners, next) { |
| 1516 | if (con != (dcl->con ? dcl->con : active_console)) { |
| 1517 | continue; |
| 1518 | } |
| 1519 | if (dcl->ops->dpy_gfx_switch) { |
| 1520 | dcl->ops->dpy_gfx_switch(dcl, surface); |
| 1521 | } |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1522 | } |
Gerd Hoffmann | da229ef | 2013-02-28 10:48:02 +0100 | [diff] [blame] | 1523 | qemu_free_displaysurface(old_surface); |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1524 | } |
| 1525 | |
Benjamin Herrenschmidt | 49743df | 2014-07-07 16:39:05 +1000 | [diff] [blame] | 1526 | bool dpy_gfx_check_format(QemuConsole *con, |
| 1527 | pixman_format_code_t format) |
| 1528 | { |
| 1529 | DisplayChangeListener *dcl; |
| 1530 | DisplayState *s = con->ds; |
| 1531 | |
| 1532 | QLIST_FOREACH(dcl, &s->listeners, next) { |
| 1533 | if (dcl->con && dcl->con != con) { |
| 1534 | /* dcl bound to another console -> skip */ |
| 1535 | continue; |
| 1536 | } |
| 1537 | if (dcl->ops->dpy_gfx_check_format) { |
| 1538 | if (!dcl->ops->dpy_gfx_check_format(dcl, format)) { |
| 1539 | return false; |
| 1540 | } |
| 1541 | } else { |
| 1542 | /* default is to whitelist native 32 bpp only */ |
| 1543 | if (format != qemu_default_pixman_format(32, true)) { |
| 1544 | return false; |
| 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | return true; |
| 1549 | } |
| 1550 | |
Stefan Weil | 6075137 | 2014-05-02 22:48:30 +0200 | [diff] [blame] | 1551 | static void dpy_refresh(DisplayState *s) |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1552 | { |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1553 | DisplayChangeListener *dcl; |
| 1554 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1555 | QLIST_FOREACH(dcl, &s->listeners, next) { |
| 1556 | if (dcl->ops->dpy_refresh) { |
Gerd Hoffmann | bc2ed97 | 2013-03-01 13:03:04 +0100 | [diff] [blame] | 1557 | dcl->ops->dpy_refresh(dcl); |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1558 | } |
| 1559 | } |
| 1560 | } |
| 1561 | |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1562 | void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y, |
| 1563 | int dst_x, int dst_y, int w, int h) |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1564 | { |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1565 | DisplayState *s = con->ds; |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1566 | DisplayChangeListener *dcl; |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 1567 | |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 1568 | if (!qemu_console_is_visible(con)) { |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 1569 | return; |
| 1570 | } |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1571 | QLIST_FOREACH(dcl, &s->listeners, next) { |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1572 | if (con != (dcl->con ? dcl->con : active_console)) { |
| 1573 | continue; |
| 1574 | } |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1575 | if (dcl->ops->dpy_gfx_copy) { |
Gerd Hoffmann | bc2ed97 | 2013-03-01 13:03:04 +0100 | [diff] [blame] | 1576 | dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h); |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1577 | } else { /* TODO */ |
Gerd Hoffmann | bc2ed97 | 2013-03-01 13:03:04 +0100 | [diff] [blame] | 1578 | dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h); |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1579 | } |
| 1580 | } |
| 1581 | } |
| 1582 | |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1583 | void dpy_text_cursor(QemuConsole *con, int x, int y) |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1584 | { |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1585 | DisplayState *s = con->ds; |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1586 | DisplayChangeListener *dcl; |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 1587 | |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 1588 | if (!qemu_console_is_visible(con)) { |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 1589 | return; |
| 1590 | } |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1591 | QLIST_FOREACH(dcl, &s->listeners, next) { |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1592 | if (con != (dcl->con ? dcl->con : active_console)) { |
| 1593 | continue; |
| 1594 | } |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1595 | if (dcl->ops->dpy_text_cursor) { |
Gerd Hoffmann | bc2ed97 | 2013-03-01 13:03:04 +0100 | [diff] [blame] | 1596 | dcl->ops->dpy_text_cursor(dcl, x, y); |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | } |
| 1600 | |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1601 | void dpy_text_update(QemuConsole *con, int x, int y, int w, int h) |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1602 | { |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1603 | DisplayState *s = con->ds; |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1604 | DisplayChangeListener *dcl; |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 1605 | |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 1606 | if (!qemu_console_is_visible(con)) { |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 1607 | return; |
| 1608 | } |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1609 | QLIST_FOREACH(dcl, &s->listeners, next) { |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1610 | if (con != (dcl->con ? dcl->con : active_console)) { |
| 1611 | continue; |
| 1612 | } |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1613 | if (dcl->ops->dpy_text_update) { |
Gerd Hoffmann | bc2ed97 | 2013-03-01 13:03:04 +0100 | [diff] [blame] | 1614 | dcl->ops->dpy_text_update(dcl, x, y, w, h); |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1615 | } |
| 1616 | } |
| 1617 | } |
| 1618 | |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1619 | void dpy_text_resize(QemuConsole *con, int w, int h) |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1620 | { |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1621 | DisplayState *s = con->ds; |
Chih-Min Chao | 9425c00 | 2015-04-09 02:04:13 +0800 | [diff] [blame] | 1622 | DisplayChangeListener *dcl; |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 1623 | |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 1624 | if (!qemu_console_is_visible(con)) { |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 1625 | return; |
| 1626 | } |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1627 | QLIST_FOREACH(dcl, &s->listeners, next) { |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1628 | if (con != (dcl->con ? dcl->con : active_console)) { |
| 1629 | continue; |
| 1630 | } |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1631 | if (dcl->ops->dpy_text_resize) { |
Gerd Hoffmann | bc2ed97 | 2013-03-01 13:03:04 +0100 | [diff] [blame] | 1632 | dcl->ops->dpy_text_resize(dcl, w, h); |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1633 | } |
| 1634 | } |
| 1635 | } |
| 1636 | |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1637 | void dpy_mouse_set(QemuConsole *con, int x, int y, int on) |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1638 | { |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1639 | DisplayState *s = con->ds; |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1640 | DisplayChangeListener *dcl; |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 1641 | |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 1642 | if (!qemu_console_is_visible(con)) { |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 1643 | return; |
| 1644 | } |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1645 | QLIST_FOREACH(dcl, &s->listeners, next) { |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1646 | if (con != (dcl->con ? dcl->con : active_console)) { |
| 1647 | continue; |
| 1648 | } |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1649 | if (dcl->ops->dpy_mouse_set) { |
Gerd Hoffmann | bc2ed97 | 2013-03-01 13:03:04 +0100 | [diff] [blame] | 1650 | dcl->ops->dpy_mouse_set(dcl, x, y, on); |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1651 | } |
| 1652 | } |
| 1653 | } |
| 1654 | |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1655 | void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor) |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1656 | { |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1657 | DisplayState *s = con->ds; |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1658 | DisplayChangeListener *dcl; |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 1659 | |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 1660 | if (!qemu_console_is_visible(con)) { |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 1661 | return; |
| 1662 | } |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1663 | QLIST_FOREACH(dcl, &s->listeners, next) { |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1664 | if (con != (dcl->con ? dcl->con : active_console)) { |
| 1665 | continue; |
| 1666 | } |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1667 | if (dcl->ops->dpy_cursor_define) { |
Gerd Hoffmann | bc2ed97 | 2013-03-01 13:03:04 +0100 | [diff] [blame] | 1668 | dcl->ops->dpy_cursor_define(dcl, cursor); |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1669 | } |
| 1670 | } |
| 1671 | } |
| 1672 | |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1673 | bool dpy_cursor_define_supported(QemuConsole *con) |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1674 | { |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1675 | DisplayState *s = con->ds; |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1676 | DisplayChangeListener *dcl; |
| 1677 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1678 | QLIST_FOREACH(dcl, &s->listeners, next) { |
| 1679 | if (dcl->ops->dpy_cursor_define) { |
| 1680 | return true; |
| 1681 | } |
| 1682 | } |
| 1683 | return false; |
| 1684 | } |
| 1685 | |
Gerd Hoffmann | 06020b9 | 2014-07-11 13:56:51 +0200 | [diff] [blame] | 1686 | QEMUGLContext dpy_gl_ctx_create(QemuConsole *con, |
| 1687 | struct QEMUGLParams *qparams) |
| 1688 | { |
| 1689 | assert(con->gl); |
| 1690 | return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams); |
| 1691 | } |
| 1692 | |
| 1693 | void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx) |
| 1694 | { |
| 1695 | assert(con->gl); |
| 1696 | con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx); |
| 1697 | } |
| 1698 | |
| 1699 | int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx) |
| 1700 | { |
| 1701 | assert(con->gl); |
| 1702 | return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx); |
| 1703 | } |
| 1704 | |
| 1705 | QEMUGLContext dpy_gl_ctx_get_current(QemuConsole *con) |
| 1706 | { |
| 1707 | assert(con->gl); |
| 1708 | return con->gl->ops->dpy_gl_ctx_get_current(con->gl); |
| 1709 | } |
| 1710 | |
| 1711 | void dpy_gl_scanout(QemuConsole *con, |
| 1712 | uint32_t backing_id, bool backing_y_0_top, |
Marc-André Lureau | 9d8256e | 2016-06-14 15:44:09 +0200 | [diff] [blame] | 1713 | uint32_t backing_width, uint32_t backing_height, |
Gerd Hoffmann | 06020b9 | 2014-07-11 13:56:51 +0200 | [diff] [blame] | 1714 | uint32_t x, uint32_t y, uint32_t width, uint32_t height) |
| 1715 | { |
| 1716 | assert(con->gl); |
| 1717 | con->gl->ops->dpy_gl_scanout(con->gl, backing_id, |
| 1718 | backing_y_0_top, |
Marc-André Lureau | 9d8256e | 2016-06-14 15:44:09 +0200 | [diff] [blame] | 1719 | backing_width, backing_height, |
Gerd Hoffmann | 06020b9 | 2014-07-11 13:56:51 +0200 | [diff] [blame] | 1720 | x, y, width, height); |
| 1721 | } |
| 1722 | |
| 1723 | void dpy_gl_update(QemuConsole *con, |
| 1724 | uint32_t x, uint32_t y, uint32_t w, uint32_t h) |
| 1725 | { |
| 1726 | assert(con->gl); |
| 1727 | con->gl->ops->dpy_gl_update(con->gl, x, y, w, h); |
| 1728 | } |
| 1729 | |
Paolo Bonzini | 98b5008 | 2010-02-11 00:29:57 +0100 | [diff] [blame] | 1730 | /***********************************************************/ |
| 1731 | /* register display */ |
| 1732 | |
Gerd Hoffmann | 64840c6 | 2013-03-07 17:08:29 +0100 | [diff] [blame] | 1733 | /* console.c internal use only */ |
| 1734 | static DisplayState *get_alloc_displaystate(void) |
Paolo Bonzini | 98b5008 | 2010-02-11 00:29:57 +0100 | [diff] [blame] | 1735 | { |
| 1736 | if (!display_state) { |
Gerd Hoffmann | 64840c6 | 2013-03-07 17:08:29 +0100 | [diff] [blame] | 1737 | display_state = g_new0(DisplayState, 1); |
Gerd Hoffmann | aea7947 | 2014-05-22 11:27:13 +0200 | [diff] [blame] | 1738 | cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME, |
| 1739 | text_console_update_cursor, NULL); |
Paolo Bonzini | 98b5008 | 2010-02-11 00:29:57 +0100 | [diff] [blame] | 1740 | } |
| 1741 | return display_state; |
| 1742 | } |
| 1743 | |
Gerd Hoffmann | 64840c6 | 2013-03-07 17:08:29 +0100 | [diff] [blame] | 1744 | /* |
| 1745 | * Called by main(), after creating QemuConsoles |
| 1746 | * and before initializing ui (sdl/vnc/...). |
| 1747 | */ |
| 1748 | DisplayState *init_displaystate(void) |
| 1749 | { |
Gerd Hoffmann | 43f420f | 2013-06-25 10:49:31 +0200 | [diff] [blame] | 1750 | gchar *name; |
Gerd Hoffmann | 64840c6 | 2013-03-07 17:08:29 +0100 | [diff] [blame] | 1751 | int i; |
| 1752 | |
Gerd Hoffmann | 333cb18 | 2014-06-02 14:07:18 +0200 | [diff] [blame] | 1753 | get_alloc_displaystate(); |
Gerd Hoffmann | 64840c6 | 2013-03-07 17:08:29 +0100 | [diff] [blame] | 1754 | for (i = 0; i < nb_consoles; i++) { |
| 1755 | if (consoles[i]->console_type != GRAPHIC_CONSOLE && |
| 1756 | consoles[i]->ds == NULL) { |
| 1757 | text_console_do_init(consoles[i]->chr, display_state); |
| 1758 | } |
Gerd Hoffmann | 43f420f | 2013-06-25 10:49:31 +0200 | [diff] [blame] | 1759 | |
| 1760 | /* Hook up into the qom tree here (not in new_console()), once |
| 1761 | * all QemuConsoles are created and the order / numbering |
| 1762 | * doesn't change any more */ |
| 1763 | name = g_strdup_printf("console[%d]", i); |
| 1764 | object_property_add_child(container_get(object_get_root(), "/backend"), |
Kirill Batuzov | afff2b1 | 2014-04-24 18:15:58 +0400 | [diff] [blame] | 1765 | name, OBJECT(consoles[i]), &error_abort); |
Gerd Hoffmann | 43f420f | 2013-06-25 10:49:31 +0200 | [diff] [blame] | 1766 | g_free(name); |
Gerd Hoffmann | 64840c6 | 2013-03-07 17:08:29 +0100 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | return display_state; |
| 1770 | } |
| 1771 | |
Gerd Hoffmann | 1c1f949 | 2014-09-24 17:05:27 +0200 | [diff] [blame] | 1772 | void graphic_console_set_hwops(QemuConsole *con, |
| 1773 | const GraphicHwOps *hw_ops, |
| 1774 | void *opaque) |
| 1775 | { |
| 1776 | con->hw_ops = hw_ops; |
| 1777 | con->hw = opaque; |
| 1778 | } |
| 1779 | |
Gerd Hoffmann | 5643706 | 2014-01-24 15:35:21 +0100 | [diff] [blame] | 1780 | QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head, |
Gerd Hoffmann | aa2beaa | 2013-04-17 10:21:27 +0200 | [diff] [blame] | 1781 | const GraphicHwOps *hw_ops, |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1782 | void *opaque) |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1783 | { |
Gerd Hoffmann | 521a580 | 2013-04-25 12:10:45 +0200 | [diff] [blame] | 1784 | static const char noinit[] = |
| 1785 | "Guest has not initialized the display (yet)."; |
Gerd Hoffmann | 64840c6 | 2013-03-07 17:08:29 +0100 | [diff] [blame] | 1786 | int width = 640; |
| 1787 | int height = 480; |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 1788 | QemuConsole *s; |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 1789 | DisplayState *ds; |
aurel32 | f0f2f97 | 2009-01-16 21:13:49 +0000 | [diff] [blame] | 1790 | |
Gerd Hoffmann | 64840c6 | 2013-03-07 17:08:29 +0100 | [diff] [blame] | 1791 | ds = get_alloc_displaystate(); |
Gerd Hoffmann | 437fe10 | 2013-03-07 16:04:52 +0100 | [diff] [blame] | 1792 | trace_console_gfx_new(); |
Kirill Batuzov | afff2b1 | 2014-04-24 18:15:58 +0400 | [diff] [blame] | 1793 | s = new_console(ds, GRAPHIC_CONSOLE, head); |
Gerd Hoffmann | cf1ecc8 | 2015-03-12 12:51:13 +0100 | [diff] [blame] | 1794 | s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, dpy_set_ui_info_timer, s); |
Gerd Hoffmann | 1c1f949 | 2014-09-24 17:05:27 +0200 | [diff] [blame] | 1795 | graphic_console_set_hwops(s, hw_ops, opaque); |
Gerd Hoffmann | aa2beaa | 2013-04-17 10:21:27 +0200 | [diff] [blame] | 1796 | if (dev) { |
Kirill Batuzov | afff2b1 | 2014-04-24 18:15:58 +0400 | [diff] [blame] | 1797 | object_property_set_link(OBJECT(s), OBJECT(dev), "device", |
| 1798 | &error_abort); |
Gerd Hoffmann | aa2beaa | 2013-04-17 10:21:27 +0200 | [diff] [blame] | 1799 | } |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 1800 | |
Gerd Hoffmann | 521a580 | 2013-04-25 12:10:45 +0200 | [diff] [blame] | 1801 | s->surface = qemu_create_message_surface(width, height, noinit); |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 1802 | return s; |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1805 | QemuConsole *qemu_console_lookup_by_index(unsigned int index) |
| 1806 | { |
Gerd Hoffmann | a1d2db0 | 2014-05-26 10:36:35 +0200 | [diff] [blame] | 1807 | if (index >= nb_consoles) { |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1808 | return NULL; |
| 1809 | } |
| 1810 | return consoles[index]; |
| 1811 | } |
| 1812 | |
Gerd Hoffmann | 5643706 | 2014-01-24 15:35:21 +0100 | [diff] [blame] | 1813 | QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head) |
Gerd Hoffmann | 14a9364 | 2013-04-18 07:30:40 +0200 | [diff] [blame] | 1814 | { |
Gerd Hoffmann | 14a9364 | 2013-04-18 07:30:40 +0200 | [diff] [blame] | 1815 | Object *obj; |
Gerd Hoffmann | 5643706 | 2014-01-24 15:35:21 +0100 | [diff] [blame] | 1816 | uint32_t h; |
Gerd Hoffmann | 14a9364 | 2013-04-18 07:30:40 +0200 | [diff] [blame] | 1817 | int i; |
| 1818 | |
| 1819 | for (i = 0; i < nb_consoles; i++) { |
| 1820 | if (!consoles[i]) { |
| 1821 | continue; |
| 1822 | } |
| 1823 | obj = object_property_get_link(OBJECT(consoles[i]), |
Kirill Batuzov | afff2b1 | 2014-04-24 18:15:58 +0400 | [diff] [blame] | 1824 | "device", &error_abort); |
Gerd Hoffmann | 5643706 | 2014-01-24 15:35:21 +0100 | [diff] [blame] | 1825 | if (DEVICE(obj) != dev) { |
| 1826 | continue; |
Gerd Hoffmann | 14a9364 | 2013-04-18 07:30:40 +0200 | [diff] [blame] | 1827 | } |
Gerd Hoffmann | 5643706 | 2014-01-24 15:35:21 +0100 | [diff] [blame] | 1828 | h = object_property_get_int(OBJECT(consoles[i]), |
Kirill Batuzov | afff2b1 | 2014-04-24 18:15:58 +0400 | [diff] [blame] | 1829 | "head", &error_abort); |
Gerd Hoffmann | 5643706 | 2014-01-24 15:35:21 +0100 | [diff] [blame] | 1830 | if (h != head) { |
| 1831 | continue; |
| 1832 | } |
| 1833 | return consoles[i]; |
Gerd Hoffmann | 14a9364 | 2013-04-18 07:30:40 +0200 | [diff] [blame] | 1834 | } |
| 1835 | return NULL; |
| 1836 | } |
| 1837 | |
Gerd Hoffmann | f2c1d54 | 2016-01-12 11:45:43 +0100 | [diff] [blame] | 1838 | QemuConsole *qemu_console_lookup_by_device_name(const char *device_id, |
| 1839 | uint32_t head, Error **errp) |
| 1840 | { |
| 1841 | DeviceState *dev; |
| 1842 | QemuConsole *con; |
| 1843 | |
| 1844 | dev = qdev_find_recursive(sysbus_get_default(), device_id); |
| 1845 | if (dev == NULL) { |
| 1846 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 1847 | "Device '%s' not found", device_id); |
| 1848 | return NULL; |
| 1849 | } |
| 1850 | |
| 1851 | con = qemu_console_lookup_by_device(dev, head); |
| 1852 | if (con == NULL) { |
| 1853 | error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole", |
| 1854 | device_id, head); |
| 1855 | return NULL; |
| 1856 | } |
| 1857 | |
| 1858 | return con; |
| 1859 | } |
| 1860 | |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 1861 | bool qemu_console_is_visible(QemuConsole *con) |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 1862 | { |
Gerd Hoffmann | 284d1c6 | 2013-03-15 15:45:54 +0100 | [diff] [blame] | 1863 | return (con == active_console) || (con->dcls > 0); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 1866 | bool qemu_console_is_graphic(QemuConsole *con) |
balrog | c21bbcf | 2008-09-24 03:32:33 +0000 | [diff] [blame] | 1867 | { |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 1868 | if (con == NULL) { |
| 1869 | con = active_console; |
| 1870 | } |
| 1871 | return con && (con->console_type == GRAPHIC_CONSOLE); |
| 1872 | } |
| 1873 | |
| 1874 | bool qemu_console_is_fixedsize(QemuConsole *con) |
| 1875 | { |
| 1876 | if (con == NULL) { |
| 1877 | con = active_console; |
| 1878 | } |
| 1879 | return con && (con->console_type != TEXT_CONSOLE); |
balrog | c21bbcf | 2008-09-24 03:32:33 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
Gerd Hoffmann | 779ce88 | 2015-02-17 10:41:08 +0100 | [diff] [blame] | 1882 | char *qemu_console_get_label(QemuConsole *con) |
| 1883 | { |
| 1884 | if (con->console_type == GRAPHIC_CONSOLE) { |
| 1885 | if (con->device) { |
| 1886 | return g_strdup(object_get_typename(con->device)); |
| 1887 | } |
| 1888 | return g_strdup("VGA"); |
| 1889 | } else { |
| 1890 | if (con->chr && con->chr->label) { |
| 1891 | return g_strdup(con->chr->label); |
| 1892 | } |
| 1893 | return g_strdup_printf("vc%d", con->index); |
| 1894 | } |
| 1895 | } |
| 1896 | |
Gerd Hoffmann | d4c8533 | 2013-11-28 09:58:18 +0100 | [diff] [blame] | 1897 | int qemu_console_get_index(QemuConsole *con) |
| 1898 | { |
| 1899 | if (con == NULL) { |
| 1900 | con = active_console; |
| 1901 | } |
| 1902 | return con ? con->index : -1; |
| 1903 | } |
| 1904 | |
Gerd Hoffmann | 5643706 | 2014-01-24 15:35:21 +0100 | [diff] [blame] | 1905 | uint32_t qemu_console_get_head(QemuConsole *con) |
| 1906 | { |
| 1907 | if (con == NULL) { |
| 1908 | con = active_console; |
| 1909 | } |
| 1910 | return con ? con->head : -1; |
| 1911 | } |
| 1912 | |
Gerd Hoffmann | 6f90f3d | 2014-01-24 17:38:20 +0100 | [diff] [blame] | 1913 | QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con) |
| 1914 | { |
| 1915 | assert(con != NULL); |
| 1916 | return &con->ui_info; |
| 1917 | } |
| 1918 | |
Gerd Hoffmann | d4c8533 | 2013-11-28 09:58:18 +0100 | [diff] [blame] | 1919 | int qemu_console_get_width(QemuConsole *con, int fallback) |
| 1920 | { |
| 1921 | if (con == NULL) { |
| 1922 | con = active_console; |
| 1923 | } |
| 1924 | return con ? surface_width(con->surface) : fallback; |
| 1925 | } |
| 1926 | |
| 1927 | int qemu_console_get_height(QemuConsole *con, int fallback) |
| 1928 | { |
| 1929 | if (con == NULL) { |
| 1930 | con = active_console; |
| 1931 | } |
| 1932 | return con ? surface_height(con->surface) : fallback; |
| 1933 | } |
| 1934 | |
Paolo Bonzini | 4104833 | 2010-12-23 13:42:52 +0100 | [diff] [blame] | 1935 | static void text_console_set_echo(CharDriverState *chr, bool echo) |
| 1936 | { |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 1937 | QemuConsole *s = chr->opaque; |
Paolo Bonzini | 4104833 | 2010-12-23 13:42:52 +0100 | [diff] [blame] | 1938 | |
| 1939 | s->echo = echo; |
| 1940 | } |
| 1941 | |
Gerd Hoffmann | aea7947 | 2014-05-22 11:27:13 +0200 | [diff] [blame] | 1942 | static void text_console_update_cursor_timer(void) |
| 1943 | { |
| 1944 | timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) |
| 1945 | + CONSOLE_CURSOR_PERIOD / 2); |
| 1946 | } |
| 1947 | |
Jan Kiszka | bf1bed8 | 2012-07-10 22:00:55 +0200 | [diff] [blame] | 1948 | static void text_console_update_cursor(void *opaque) |
| 1949 | { |
Gerd Hoffmann | aea7947 | 2014-05-22 11:27:13 +0200 | [diff] [blame] | 1950 | QemuConsole *s; |
| 1951 | int i, count = 0; |
Jan Kiszka | bf1bed8 | 2012-07-10 22:00:55 +0200 | [diff] [blame] | 1952 | |
Gerd Hoffmann | aea7947 | 2014-05-22 11:27:13 +0200 | [diff] [blame] | 1953 | cursor_visible_phase = !cursor_visible_phase; |
| 1954 | |
| 1955 | for (i = 0; i < nb_consoles; i++) { |
| 1956 | s = consoles[i]; |
| 1957 | if (qemu_console_is_graphic(s) || |
| 1958 | !qemu_console_is_visible(s)) { |
| 1959 | continue; |
| 1960 | } |
| 1961 | count++; |
| 1962 | graphic_hw_invalidate(s); |
| 1963 | } |
| 1964 | |
| 1965 | if (count) { |
| 1966 | text_console_update_cursor_timer(); |
| 1967 | } |
Jan Kiszka | bf1bed8 | 2012-07-10 22:00:55 +0200 | [diff] [blame] | 1968 | } |
| 1969 | |
Gerd Hoffmann | 380cd05 | 2013-03-13 14:04:18 +0100 | [diff] [blame] | 1970 | static const GraphicHwOps text_console_ops = { |
| 1971 | .invalidate = text_console_invalidate, |
| 1972 | .text_update = text_console_update, |
| 1973 | }; |
| 1974 | |
Paolo Bonzini | 44b37b9 | 2010-12-23 13:42:53 +0100 | [diff] [blame] | 1975 | static void text_console_do_init(CharDriverState *chr, DisplayState *ds) |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1976 | { |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 1977 | QemuConsole *s; |
Gerd Hoffmann | 36671fb | 2013-03-13 10:14:52 +0100 | [diff] [blame] | 1978 | int g_width = 80 * FONT_WIDTH; |
| 1979 | int g_height = 24 * FONT_HEIGHT; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 1980 | |
Paolo Bonzini | 491e114 | 2010-12-23 13:42:51 +0100 | [diff] [blame] | 1981 | s = chr->opaque; |
Gerd Hoffmann | 6ea314d | 2009-09-10 10:58:49 +0200 | [diff] [blame] | 1982 | |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1983 | chr->chr_write = console_puts; |
bellard | 6fcfafb | 2004-08-01 21:48:30 +0000 | [diff] [blame] | 1984 | |
bellard | e15d737 | 2006-06-25 16:26:29 +0000 | [diff] [blame] | 1985 | s->out_fifo.buf = s->out_fifo_buf; |
| 1986 | s->out_fifo.buf_size = sizeof(s->out_fifo_buf); |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 1987 | s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s); |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 1988 | s->ds = ds; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1989 | |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 1990 | s->y_displayed = 0; |
| 1991 | s->y_base = 0; |
| 1992 | s->total_height = DEFAULT_BACKSCROLL; |
| 1993 | s->x = 0; |
| 1994 | s->y = 0; |
Gerd Hoffmann | 36671fb | 2013-03-13 10:14:52 +0100 | [diff] [blame] | 1995 | if (!s->surface) { |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 1996 | if (active_console && active_console->surface) { |
Gerd Hoffmann | 36671fb | 2013-03-13 10:14:52 +0100 | [diff] [blame] | 1997 | g_width = surface_width(active_console->surface); |
| 1998 | g_height = surface_height(active_console->surface); |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 1999 | } |
Gerd Hoffmann | 36671fb | 2013-03-13 10:14:52 +0100 | [diff] [blame] | 2000 | s->surface = qemu_create_displaysurface(g_width, g_height); |
Paolo Bonzini | 491e114 | 2010-12-23 13:42:51 +0100 | [diff] [blame] | 2001 | } |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 2002 | |
Gerd Hoffmann | 380cd05 | 2013-03-13 14:04:18 +0100 | [diff] [blame] | 2003 | s->hw_ops = &text_console_ops; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 2004 | s->hw = s; |
| 2005 | |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 2006 | /* Set text attribute defaults */ |
| 2007 | s->t_attrib_default.bold = 0; |
| 2008 | s->t_attrib_default.uline = 0; |
| 2009 | s->t_attrib_default.blink = 0; |
| 2010 | s->t_attrib_default.invers = 0; |
| 2011 | s->t_attrib_default.unvisible = 0; |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 2012 | s->t_attrib_default.fgcol = QEMU_COLOR_WHITE; |
| 2013 | s->t_attrib_default.bgcol = QEMU_COLOR_BLACK; |
pbrook | 6d6f7c2 | 2006-03-11 15:35:30 +0000 | [diff] [blame] | 2014 | /* set current text attributes to default */ |
| 2015 | s->t_attrib = s->t_attrib_default; |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 2016 | text_console_resize(s); |
| 2017 | |
Gerd Hoffmann | 51bfa4d | 2009-12-08 13:11:39 +0100 | [diff] [blame] | 2018 | if (chr->label) { |
| 2019 | char msg[128]; |
| 2020 | int len; |
| 2021 | |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 2022 | s->t_attrib.bgcol = QEMU_COLOR_BLUE; |
Gerd Hoffmann | 51bfa4d | 2009-12-08 13:11:39 +0100 | [diff] [blame] | 2023 | len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label); |
| 2024 | console_puts(chr, (uint8_t*)msg, len); |
Gerd Hoffmann | 735ba58 | 2009-12-08 13:11:40 +0100 | [diff] [blame] | 2025 | s->t_attrib = s->t_attrib_default; |
Gerd Hoffmann | 51bfa4d | 2009-12-08 13:11:39 +0100 | [diff] [blame] | 2026 | } |
| 2027 | |
Hans de Goede | fee204f | 2013-03-26 11:07:54 +0100 | [diff] [blame] | 2028 | qemu_chr_be_generic_open(chr); |
aurel32 | ceecf1d | 2009-01-18 14:08:04 +0000 | [diff] [blame] | 2029 | if (chr->init) |
| 2030 | chr->init(chr); |
bellard | e7f0ad5 | 2004-07-14 17:28:59 +0000 | [diff] [blame] | 2031 | } |
pbrook | c60e08d | 2008-07-01 16:24:38 +0000 | [diff] [blame] | 2032 | |
Paolo Bonzini | fa19d02 | 2015-09-29 15:49:06 +0200 | [diff] [blame] | 2033 | static CharDriverState *text_console_init(ChardevVC *vc, Error **errp) |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 2034 | { |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 2035 | ChardevCommon *common = qapi_ChardevVC_base(vc); |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 2036 | CharDriverState *chr; |
Gerd Hoffmann | 76ffb0b | 2012-09-28 13:24:17 +0200 | [diff] [blame] | 2037 | QemuConsole *s; |
Gerd Hoffmann | 702ec69 | 2013-02-25 15:52:32 +0100 | [diff] [blame] | 2038 | unsigned width = 0; |
| 2039 | unsigned height = 0; |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 2040 | |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 2041 | chr = qemu_chr_alloc(common, errp); |
| 2042 | if (!chr) { |
| 2043 | return NULL; |
| 2044 | } |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 2045 | |
Gerd Hoffmann | 702ec69 | 2013-02-25 15:52:32 +0100 | [diff] [blame] | 2046 | if (vc->has_width) { |
| 2047 | width = vc->width; |
| 2048 | } else if (vc->has_cols) { |
| 2049 | width = vc->cols * FONT_WIDTH; |
| 2050 | } |
Paolo Bonzini | 491e114 | 2010-12-23 13:42:51 +0100 | [diff] [blame] | 2051 | |
Gerd Hoffmann | 702ec69 | 2013-02-25 15:52:32 +0100 | [diff] [blame] | 2052 | if (vc->has_height) { |
| 2053 | height = vc->height; |
| 2054 | } else if (vc->has_rows) { |
| 2055 | height = vc->rows * FONT_HEIGHT; |
| 2056 | } |
Paolo Bonzini | 491e114 | 2010-12-23 13:42:51 +0100 | [diff] [blame] | 2057 | |
Gerd Hoffmann | 437fe10 | 2013-03-07 16:04:52 +0100 | [diff] [blame] | 2058 | trace_console_txt_new(width, height); |
Paolo Bonzini | 491e114 | 2010-12-23 13:42:51 +0100 | [diff] [blame] | 2059 | if (width == 0 || height == 0) { |
Kirill Batuzov | afff2b1 | 2014-04-24 18:15:58 +0400 | [diff] [blame] | 2060 | s = new_console(NULL, TEXT_CONSOLE, 0); |
Paolo Bonzini | 491e114 | 2010-12-23 13:42:51 +0100 | [diff] [blame] | 2061 | } else { |
Kirill Batuzov | afff2b1 | 2014-04-24 18:15:58 +0400 | [diff] [blame] | 2062 | s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0); |
Gerd Hoffmann | 36671fb | 2013-03-13 10:14:52 +0100 | [diff] [blame] | 2063 | s->surface = qemu_create_displaysurface(width, height); |
Paolo Bonzini | 491e114 | 2010-12-23 13:42:51 +0100 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | if (!s) { |
Stefan Weil | 5354d08 | 2011-10-02 18:53:09 +0200 | [diff] [blame] | 2067 | g_free(chr); |
Paolo Bonzini | fa19d02 | 2015-09-29 15:49:06 +0200 | [diff] [blame] | 2068 | error_setg(errp, "cannot create text console"); |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 2069 | return NULL; |
Paolo Bonzini | 491e114 | 2010-12-23 13:42:51 +0100 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | s->chr = chr; |
Paolo Bonzini | 491e114 | 2010-12-23 13:42:51 +0100 | [diff] [blame] | 2073 | chr->opaque = s; |
Paolo Bonzini | 4104833 | 2010-12-23 13:42:52 +0100 | [diff] [blame] | 2074 | chr->chr_set_echo = text_console_set_echo; |
Michael Roth | bd5c51e | 2013-06-07 15:19:53 -0500 | [diff] [blame] | 2075 | /* console/chardev init sometimes completes elsewhere in a 2nd |
| 2076 | * stage, so defer OPENED events until they are fully initialized |
| 2077 | */ |
| 2078 | chr->explicit_be_open = true; |
Gerd Hoffmann | 64840c6 | 2013-03-07 17:08:29 +0100 | [diff] [blame] | 2079 | |
| 2080 | if (display_state) { |
| 2081 | text_console_do_init(chr, display_state); |
| 2082 | } |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 2083 | return chr; |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Anthony Liguori | d82831d | 2013-02-20 07:43:19 -0600 | [diff] [blame] | 2086 | static VcHandler *vc_handler = text_console_init; |
| 2087 | |
Paolo Bonzini | fa19d02 | 2015-09-29 15:49:06 +0200 | [diff] [blame] | 2088 | static CharDriverState *vc_init(const char *id, ChardevBackend *backend, |
| 2089 | ChardevReturn *ret, Error **errp) |
Anthony Liguori | d82831d | 2013-02-20 07:43:19 -0600 | [diff] [blame] | 2090 | { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 2091 | return vc_handler(backend->u.vc.data, errp); |
Anthony Liguori | d82831d | 2013-02-20 07:43:19 -0600 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | void register_vc_handler(VcHandler *handler) |
| 2095 | { |
| 2096 | vc_handler = handler; |
| 2097 | } |
| 2098 | |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 2099 | void qemu_console_resize(QemuConsole *s, int width, int height) |
pbrook | c60e08d | 2008-07-01 16:24:38 +0000 | [diff] [blame] | 2100 | { |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 2101 | DisplaySurface *surface; |
| 2102 | |
| 2103 | assert(s->console_type == GRAPHIC_CONSOLE); |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 2104 | surface = qemu_create_displaysurface(width, height); |
| 2105 | dpy_gfx_replace_surface(s, surface); |
pbrook | c60e08d | 2008-07-01 16:24:38 +0000 | [diff] [blame] | 2106 | } |
balrog | 38334f7 | 2008-09-24 02:21:24 +0000 | [diff] [blame] | 2107 | |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 2108 | void qemu_console_copy(QemuConsole *con, int src_x, int src_y, |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 2109 | int dst_x, int dst_y, int w, int h) |
balrog | c21bbcf | 2008-09-24 03:32:33 +0000 | [diff] [blame] | 2110 | { |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 2111 | assert(con->console_type == GRAPHIC_CONSOLE); |
| 2112 | dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h); |
balrog | 38334f7 | 2008-09-24 02:21:24 +0000 | [diff] [blame] | 2113 | } |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 2114 | |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 2115 | DisplaySurface *qemu_console_surface(QemuConsole *console) |
| 2116 | { |
Gerd Hoffmann | 321f048 | 2013-03-12 14:39:22 +0100 | [diff] [blame] | 2117 | return console->surface; |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 2118 | } |
| 2119 | |
malc | 0da2ea1 | 2009-01-23 19:56:19 +0000 | [diff] [blame] | 2120 | PixelFormat qemu_default_pixelformat(int bpp) |
| 2121 | { |
Gerd Hoffmann | 56bd9ea | 2014-06-18 11:07:50 +0200 | [diff] [blame] | 2122 | pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true); |
| 2123 | PixelFormat pf = qemu_pixelformat_from_pixman(fmt); |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 2124 | return pf; |
| 2125 | } |
Anthony Liguori | 01f45d9 | 2013-03-05 23:21:32 +0530 | [diff] [blame] | 2126 | |
Gerd Hoffmann | 702ec69 | 2013-02-25 15:52:32 +0100 | [diff] [blame] | 2127 | static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, |
| 2128 | Error **errp) |
| 2129 | { |
| 2130 | int val; |
Eric Blake | 21a933e | 2016-02-19 17:19:31 -0700 | [diff] [blame] | 2131 | ChardevVC *vc; |
Gerd Hoffmann | 702ec69 | 2013-02-25 15:52:32 +0100 | [diff] [blame] | 2132 | |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 2133 | vc = backend->u.vc.data = g_new0(ChardevVC, 1); |
Eric Blake | 21a933e | 2016-02-19 17:19:31 -0700 | [diff] [blame] | 2134 | qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc)); |
Gerd Hoffmann | 702ec69 | 2013-02-25 15:52:32 +0100 | [diff] [blame] | 2135 | |
| 2136 | val = qemu_opt_get_number(opts, "width", 0); |
| 2137 | if (val != 0) { |
Eric Blake | 21a933e | 2016-02-19 17:19:31 -0700 | [diff] [blame] | 2138 | vc->has_width = true; |
| 2139 | vc->width = val; |
Gerd Hoffmann | 702ec69 | 2013-02-25 15:52:32 +0100 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | val = qemu_opt_get_number(opts, "height", 0); |
| 2143 | if (val != 0) { |
Eric Blake | 21a933e | 2016-02-19 17:19:31 -0700 | [diff] [blame] | 2144 | vc->has_height = true; |
| 2145 | vc->height = val; |
Gerd Hoffmann | 702ec69 | 2013-02-25 15:52:32 +0100 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | val = qemu_opt_get_number(opts, "cols", 0); |
| 2149 | if (val != 0) { |
Eric Blake | 21a933e | 2016-02-19 17:19:31 -0700 | [diff] [blame] | 2150 | vc->has_cols = true; |
| 2151 | vc->cols = val; |
Gerd Hoffmann | 702ec69 | 2013-02-25 15:52:32 +0100 | [diff] [blame] | 2152 | } |
| 2153 | |
| 2154 | val = qemu_opt_get_number(opts, "rows", 0); |
| 2155 | if (val != 0) { |
Eric Blake | 21a933e | 2016-02-19 17:19:31 -0700 | [diff] [blame] | 2156 | vc->has_rows = true; |
| 2157 | vc->rows = val; |
Gerd Hoffmann | 702ec69 | 2013-02-25 15:52:32 +0100 | [diff] [blame] | 2158 | } |
| 2159 | } |
| 2160 | |
Gerd Hoffmann | 95be066 | 2013-04-17 09:45:10 +0200 | [diff] [blame] | 2161 | static const TypeInfo qemu_console_info = { |
| 2162 | .name = TYPE_QEMU_CONSOLE, |
| 2163 | .parent = TYPE_OBJECT, |
| 2164 | .instance_size = sizeof(QemuConsole), |
| 2165 | .class_size = sizeof(QemuConsoleClass), |
| 2166 | }; |
| 2167 | |
| 2168 | |
Anthony Liguori | 01f45d9 | 2013-03-05 23:21:32 +0530 | [diff] [blame] | 2169 | static void register_types(void) |
| 2170 | { |
Gerd Hoffmann | 95be066 | 2013-04-17 09:45:10 +0200 | [diff] [blame] | 2171 | type_register_static(&qemu_console_info); |
Paolo Bonzini | 4ca1728 | 2015-09-29 14:55:59 +0200 | [diff] [blame] | 2172 | register_char_driver("vc", CHARDEV_BACKEND_KIND_VC, qemu_chr_parse_vc, |
Paolo Bonzini | fa19d02 | 2015-09-29 15:49:06 +0200 | [diff] [blame] | 2173 | vc_init); |
Anthony Liguori | 01f45d9 | 2013-03-05 23:21:32 +0530 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | type_init(register_types); |