1 #ifndef CONSOLE_H 2 #define CONSOLE_H 3 4 #include "ui/qemu-pixman.h" 5 #include "qapi/qmp/qdict.h" 6 #include "qemu/notify.h" 7 #include "monitor/monitor.h" 8 #include "trace.h" 9 #include "qapi-types.h" 10 #include "qapi/error.h" 11 12 /* keyboard/mouse support */ 13 14 #define MOUSE_EVENT_LBUTTON 0x01 15 #define MOUSE_EVENT_RBUTTON 0x02 16 #define MOUSE_EVENT_MBUTTON 0x04 17 18 /* identical to the ps/2 keyboard bits */ 19 #define QEMU_SCROLL_LOCK_LED (1 << 0) 20 #define QEMU_NUM_LOCK_LED (1 << 1) 21 #define QEMU_CAPS_LOCK_LED (1 << 2) 22 23 /* in ms */ 24 #define GUI_REFRESH_INTERVAL_DEFAULT 30 25 #define GUI_REFRESH_INTERVAL_IDLE 3000 26 27 typedef void QEMUPutKBDEvent(void *opaque, int keycode); 28 typedef void QEMUPutLEDEvent(void *opaque, int ledstate); 29 typedef void QEMUPutMouseEvent(void *opaque, int dx, int dy, int dz, int buttons_state); 30 31 typedef struct QEMUPutMouseEntry { 32 QEMUPutMouseEvent *qemu_put_mouse_event; 33 void *qemu_put_mouse_event_opaque; 34 int qemu_put_mouse_event_absolute; 35 char *qemu_put_mouse_event_name; 36 37 int index; 38 39 /* used internally by qemu for handling mice */ 40 QTAILQ_ENTRY(QEMUPutMouseEntry) node; 41 } QEMUPutMouseEntry; 42 43 typedef struct QEMUPutLEDEntry { 44 QEMUPutLEDEvent *put_led; 45 void *opaque; 46 QTAILQ_ENTRY(QEMUPutLEDEntry) next; 47 } QEMUPutLEDEntry; 48 49 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque); 50 void qemu_remove_kbd_event_handler(void); 51 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, 52 void *opaque, int absolute, 53 const char *name); 54 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry); 55 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry); 56 57 QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func, void *opaque); 58 void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry); 59 60 void kbd_put_keycode(int keycode); 61 void kbd_put_ledstate(int ledstate); 62 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state); 63 64 /* Does the current mouse generate absolute events */ 65 int kbd_mouse_is_absolute(void); 66 void qemu_add_mouse_mode_change_notifier(Notifier *notify); 67 void qemu_remove_mouse_mode_change_notifier(Notifier *notify); 68 69 /* Of all the mice, is there one that generates absolute events */ 70 int kbd_mouse_has_absolute(void); 71 72 struct MouseTransformInfo { 73 /* Touchscreen resolution */ 74 int x; 75 int y; 76 /* Calibration values as used/generated by tslib */ 77 int a[7]; 78 }; 79 80 void do_mouse_set(Monitor *mon, const QDict *qdict); 81 82 /* keysym is a unicode code except for special keys (see QEMU_KEY_xxx 83 constants) */ 84 #define QEMU_KEY_ESC1(c) ((c) | 0xe100) 85 #define QEMU_KEY_BACKSPACE 0x007f 86 #define QEMU_KEY_UP QEMU_KEY_ESC1('A') 87 #define QEMU_KEY_DOWN QEMU_KEY_ESC1('B') 88 #define QEMU_KEY_RIGHT QEMU_KEY_ESC1('C') 89 #define QEMU_KEY_LEFT QEMU_KEY_ESC1('D') 90 #define QEMU_KEY_HOME QEMU_KEY_ESC1(1) 91 #define QEMU_KEY_END QEMU_KEY_ESC1(4) 92 #define QEMU_KEY_PAGEUP QEMU_KEY_ESC1(5) 93 #define QEMU_KEY_PAGEDOWN QEMU_KEY_ESC1(6) 94 #define QEMU_KEY_DELETE QEMU_KEY_ESC1(3) 95 96 #define QEMU_KEY_CTRL_UP 0xe400 97 #define QEMU_KEY_CTRL_DOWN 0xe401 98 #define QEMU_KEY_CTRL_LEFT 0xe402 99 #define QEMU_KEY_CTRL_RIGHT 0xe403 100 #define QEMU_KEY_CTRL_HOME 0xe404 101 #define QEMU_KEY_CTRL_END 0xe405 102 #define QEMU_KEY_CTRL_PAGEUP 0xe406 103 #define QEMU_KEY_CTRL_PAGEDOWN 0xe407 104 105 void kbd_put_keysym(int keysym); 106 107 /* consoles */ 108 109 #define QEMU_BIG_ENDIAN_FLAG 0x01 110 #define QEMU_ALLOCATED_FLAG 0x02 111 112 struct PixelFormat { 113 uint8_t bits_per_pixel; 114 uint8_t bytes_per_pixel; 115 uint8_t depth; /* color depth in bits */ 116 uint32_t rmask, gmask, bmask, amask; 117 uint8_t rshift, gshift, bshift, ashift; 118 uint8_t rmax, gmax, bmax, amax; 119 uint8_t rbits, gbits, bbits, abits; 120 }; 121 122 struct DisplaySurface { 123 pixman_format_code_t format; 124 pixman_image_t *image; 125 uint8_t flags; 126 127 struct PixelFormat pf; 128 }; 129 130 /* cursor data format is 32bit RGBA */ 131 typedef struct QEMUCursor { 132 int width, height; 133 int hot_x, hot_y; 134 int refcount; 135 uint32_t data[]; 136 } QEMUCursor; 137 138 QEMUCursor *cursor_alloc(int width, int height); 139 void cursor_get(QEMUCursor *c); 140 void cursor_put(QEMUCursor *c); 141 QEMUCursor *cursor_builtin_hidden(void); 142 QEMUCursor *cursor_builtin_left_ptr(void); 143 void cursor_print_ascii_art(QEMUCursor *c, const char *prefix); 144 int cursor_get_mono_bpl(QEMUCursor *c); 145 void cursor_set_mono(QEMUCursor *c, 146 uint32_t foreground, uint32_t background, uint8_t *image, 147 int transparent, uint8_t *mask); 148 void cursor_get_mono_image(QEMUCursor *c, int foreground, uint8_t *mask); 149 void cursor_get_mono_mask(QEMUCursor *c, int transparent, uint8_t *mask); 150 151 typedef struct DisplayChangeListenerOps { 152 const char *dpy_name; 153 154 void (*dpy_refresh)(DisplayChangeListener *dcl); 155 156 void (*dpy_gfx_update)(DisplayChangeListener *dcl, 157 int x, int y, int w, int h); 158 void (*dpy_gfx_switch)(DisplayChangeListener *dcl, 159 struct DisplaySurface *new_surface); 160 void (*dpy_gfx_copy)(DisplayChangeListener *dcl, 161 int src_x, int src_y, 162 int dst_x, int dst_y, int w, int h); 163 164 void (*dpy_text_cursor)(DisplayChangeListener *dcl, 165 int x, int y); 166 void (*dpy_text_resize)(DisplayChangeListener *dcl, 167 int w, int h); 168 void (*dpy_text_update)(DisplayChangeListener *dcl, 169 int x, int y, int w, int h); 170 171 void (*dpy_mouse_set)(DisplayChangeListener *dcl, 172 int x, int y, int on); 173 void (*dpy_cursor_define)(DisplayChangeListener *dcl, 174 QEMUCursor *cursor); 175 } DisplayChangeListenerOps; 176 177 struct DisplayChangeListener { 178 uint64_t update_interval; 179 const DisplayChangeListenerOps *ops; 180 DisplayState *ds; 181 QemuConsole *con; 182 183 QLIST_ENTRY(DisplayChangeListener) next; 184 }; 185 186 DisplayState *init_displaystate(void); 187 DisplaySurface* qemu_create_displaysurface_from(int width, int height, int bpp, 188 int linesize, uint8_t *data, 189 bool byteswap); 190 PixelFormat qemu_different_endianness_pixelformat(int bpp); 191 PixelFormat qemu_default_pixelformat(int bpp); 192 193 DisplaySurface *qemu_create_displaysurface(int width, int height); 194 void qemu_free_displaysurface(DisplaySurface *surface); 195 196 static inline int is_surface_bgr(DisplaySurface *surface) 197 { 198 if (surface->pf.bits_per_pixel == 32 && surface->pf.rshift == 0) 199 return 1; 200 else 201 return 0; 202 } 203 204 static inline int is_buffer_shared(DisplaySurface *surface) 205 { 206 return !(surface->flags & QEMU_ALLOCATED_FLAG); 207 } 208 209 void register_displaychangelistener(DisplayState *ds, 210 DisplayChangeListener *dcl); 211 void update_displaychangelistener(DisplayChangeListener *dcl, 212 uint64_t interval); 213 void unregister_displaychangelistener(DisplayChangeListener *dcl); 214 215 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h); 216 void dpy_gfx_replace_surface(QemuConsole *con, 217 DisplaySurface *surface); 218 void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y, 219 int dst_x, int dst_y, int w, int h); 220 void dpy_text_cursor(QemuConsole *con, int x, int y); 221 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h); 222 void dpy_text_resize(QemuConsole *con, int w, int h); 223 void dpy_mouse_set(QemuConsole *con, int x, int y, int on); 224 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor); 225 bool dpy_cursor_define_supported(QemuConsole *con); 226 227 static inline int surface_stride(DisplaySurface *s) 228 { 229 return pixman_image_get_stride(s->image); 230 } 231 232 static inline void *surface_data(DisplaySurface *s) 233 { 234 return pixman_image_get_data(s->image); 235 } 236 237 static inline int surface_width(DisplaySurface *s) 238 { 239 return pixman_image_get_width(s->image); 240 } 241 242 static inline int surface_height(DisplaySurface *s) 243 { 244 return pixman_image_get_height(s->image); 245 } 246 247 static inline int surface_bits_per_pixel(DisplaySurface *s) 248 { 249 int bits = PIXMAN_FORMAT_BPP(s->format); 250 return bits; 251 } 252 253 static inline int surface_bytes_per_pixel(DisplaySurface *s) 254 { 255 int bits = PIXMAN_FORMAT_BPP(s->format); 256 return (bits + 7) / 8; 257 } 258 259 #ifdef CONFIG_CURSES 260 #include <curses.h> 261 typedef chtype console_ch_t; 262 #else 263 typedef unsigned long console_ch_t; 264 #endif 265 static inline void console_write_ch(console_ch_t *dest, uint32_t ch) 266 { 267 if (!(ch & 0xff)) 268 ch |= ' '; 269 *dest = ch; 270 } 271 272 typedef struct GraphicHwOps { 273 void (*invalidate)(void *opaque); 274 void (*gfx_update)(void *opaque); 275 void (*text_update)(void *opaque, console_ch_t *text); 276 void (*update_interval)(void *opaque, uint64_t interval); 277 } GraphicHwOps; 278 279 QemuConsole *graphic_console_init(const GraphicHwOps *ops, 280 void *opaque); 281 282 void graphic_hw_update(QemuConsole *con); 283 void graphic_hw_invalidate(QemuConsole *con); 284 void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata); 285 286 QemuConsole *qemu_console_lookup_by_index(unsigned int index); 287 bool qemu_console_is_visible(QemuConsole *con); 288 bool qemu_console_is_graphic(QemuConsole *con); 289 bool qemu_console_is_fixedsize(QemuConsole *con); 290 291 void text_consoles_set_display(DisplayState *ds); 292 void console_select(unsigned int index); 293 void console_color_init(DisplayState *ds); 294 void qemu_console_resize(QemuConsole *con, int width, int height); 295 void qemu_console_copy(QemuConsole *con, int src_x, int src_y, 296 int dst_x, int dst_y, int w, int h); 297 DisplaySurface *qemu_console_surface(QemuConsole *con); 298 DisplayState *qemu_console_displaystate(QemuConsole *console); 299 300 typedef CharDriverState *(VcHandler)(ChardevVC *vc); 301 302 CharDriverState *vc_init(ChardevVC *vc); 303 void register_vc_handler(VcHandler *handler); 304 305 /* sdl.c */ 306 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame); 307 308 /* cocoa.m */ 309 void cocoa_display_init(DisplayState *ds, int full_screen); 310 311 /* vnc.c */ 312 void vnc_display_init(DisplayState *ds); 313 void vnc_display_open(DisplayState *ds, const char *display, Error **errp); 314 void vnc_display_add_client(DisplayState *ds, int csock, int skipauth); 315 char *vnc_display_local_addr(DisplayState *ds); 316 #ifdef CONFIG_VNC 317 int vnc_display_password(DisplayState *ds, const char *password); 318 int vnc_display_pw_expire(DisplayState *ds, time_t expires); 319 #else 320 static inline int vnc_display_password(DisplayState *ds, const char *password) 321 { 322 return -ENODEV; 323 } 324 static inline int vnc_display_pw_expire(DisplayState *ds, time_t expires) 325 { 326 return -ENODEV; 327 }; 328 #endif 329 330 /* curses.c */ 331 void curses_display_init(DisplayState *ds, int full_screen); 332 333 /* input.c */ 334 int index_from_key(const char *key); 335 int index_from_keycode(int code); 336 337 /* gtk.c */ 338 void early_gtk_display_init(void); 339 void gtk_display_init(DisplayState *ds); 340 341 #endif 342