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