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