xref: /openbmc/qemu/include/ui/console.h (revision 61b9251a)
1 #ifndef CONSOLE_H
2 #define CONSOLE_H
3 
4 #include "ui/qemu-pixman.h"
5 #include "qom/object.h"
6 #include "qapi/qmp/qdict.h"
7 #include "qemu/notify.h"
8 #include "qemu/typedefs.h"
9 #include "qapi-types.h"
10 #include "qapi/error.h"
11 
12 #ifdef CONFIG_OPENGL
13 # include <epoxy/gl.h>
14 #endif
15 
16 /* keyboard/mouse support */
17 
18 #define MOUSE_EVENT_LBUTTON 0x01
19 #define MOUSE_EVENT_RBUTTON 0x02
20 #define MOUSE_EVENT_MBUTTON 0x04
21 #define MOUSE_EVENT_WHEELUP 0x08
22 #define MOUSE_EVENT_WHEELDN 0x10
23 
24 /* identical to the ps/2 keyboard bits */
25 #define QEMU_SCROLL_LOCK_LED (1 << 0)
26 #define QEMU_NUM_LOCK_LED    (1 << 1)
27 #define QEMU_CAPS_LOCK_LED   (1 << 2)
28 
29 /* in ms */
30 #define GUI_REFRESH_INTERVAL_DEFAULT    30
31 #define GUI_REFRESH_INTERVAL_IDLE     3000
32 
33 typedef void QEMUPutKBDEvent(void *opaque, int keycode);
34 typedef void QEMUPutLEDEvent(void *opaque, int ledstate);
35 typedef void QEMUPutMouseEvent(void *opaque, int dx, int dy, int dz, int buttons_state);
36 
37 typedef struct QEMUPutMouseEntry QEMUPutMouseEntry;
38 typedef struct QEMUPutKbdEntry QEMUPutKbdEntry;
39 typedef struct QEMUPutLEDEntry QEMUPutLEDEntry;
40 
41 QEMUPutKbdEntry *qemu_add_kbd_event_handler(QEMUPutKBDEvent *func,
42                                             void *opaque);
43 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
44                                                 void *opaque, int absolute,
45                                                 const char *name);
46 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry);
47 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry);
48 
49 QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func, void *opaque);
50 void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry);
51 
52 void kbd_put_ledstate(int ledstate);
53 
54 struct MouseTransformInfo {
55     /* Touchscreen resolution */
56     int x;
57     int y;
58     /* Calibration values as used/generated by tslib */
59     int a[7];
60 };
61 
62 void hmp_mouse_set(Monitor *mon, const QDict *qdict);
63 
64 /* keysym is a unicode code except for special keys (see QEMU_KEY_xxx
65    constants) */
66 #define QEMU_KEY_ESC1(c) ((c) | 0xe100)
67 #define QEMU_KEY_BACKSPACE  0x007f
68 #define QEMU_KEY_UP         QEMU_KEY_ESC1('A')
69 #define QEMU_KEY_DOWN       QEMU_KEY_ESC1('B')
70 #define QEMU_KEY_RIGHT      QEMU_KEY_ESC1('C')
71 #define QEMU_KEY_LEFT       QEMU_KEY_ESC1('D')
72 #define QEMU_KEY_HOME       QEMU_KEY_ESC1(1)
73 #define QEMU_KEY_END        QEMU_KEY_ESC1(4)
74 #define QEMU_KEY_PAGEUP     QEMU_KEY_ESC1(5)
75 #define QEMU_KEY_PAGEDOWN   QEMU_KEY_ESC1(6)
76 #define QEMU_KEY_DELETE     QEMU_KEY_ESC1(3)
77 
78 #define QEMU_KEY_CTRL_UP         0xe400
79 #define QEMU_KEY_CTRL_DOWN       0xe401
80 #define QEMU_KEY_CTRL_LEFT       0xe402
81 #define QEMU_KEY_CTRL_RIGHT      0xe403
82 #define QEMU_KEY_CTRL_HOME       0xe404
83 #define QEMU_KEY_CTRL_END        0xe405
84 #define QEMU_KEY_CTRL_PAGEUP     0xe406
85 #define QEMU_KEY_CTRL_PAGEDOWN   0xe407
86 
87 void kbd_put_keysym_console(QemuConsole *s, int keysym);
88 bool kbd_put_qcode_console(QemuConsole *s, int qcode);
89 void kbd_put_string_console(QemuConsole *s, const char *str, int len);
90 void kbd_put_keysym(int keysym);
91 
92 /* consoles */
93 
94 #define TYPE_QEMU_CONSOLE "qemu-console"
95 #define QEMU_CONSOLE(obj) \
96     OBJECT_CHECK(QemuConsole, (obj), TYPE_QEMU_CONSOLE)
97 #define QEMU_CONSOLE_GET_CLASS(obj) \
98     OBJECT_GET_CLASS(QemuConsoleClass, (obj), TYPE_QEMU_CONSOLE)
99 #define QEMU_CONSOLE_CLASS(klass) \
100     OBJECT_CLASS_CHECK(QemuConsoleClass, (klass), TYPE_QEMU_CONSOLE)
101 
102 typedef struct QemuConsoleClass QemuConsoleClass;
103 
104 struct QemuConsoleClass {
105     ObjectClass parent_class;
106 };
107 
108 #define QEMU_ALLOCATED_FLAG     0x01
109 
110 struct PixelFormat {
111     uint8_t bits_per_pixel;
112     uint8_t bytes_per_pixel;
113     uint8_t depth; /* color depth in bits */
114     uint32_t rmask, gmask, bmask, amask;
115     uint8_t rshift, gshift, bshift, ashift;
116     uint8_t rmax, gmax, bmax, amax;
117     uint8_t rbits, gbits, bbits, abits;
118 };
119 
120 struct DisplaySurface {
121     pixman_format_code_t format;
122     pixman_image_t *image;
123     uint8_t flags;
124 #ifdef CONFIG_OPENGL
125     GLenum glformat;
126     GLenum gltype;
127     GLuint texture;
128 #endif
129 };
130 
131 typedef struct QemuUIInfo {
132     /* geometry */
133     int       xoff;
134     int       yoff;
135     uint32_t  width;
136     uint32_t  height;
137 } QemuUIInfo;
138 
139 /* cursor data format is 32bit RGBA */
140 typedef struct QEMUCursor {
141     int                 width, height;
142     int                 hot_x, hot_y;
143     int                 refcount;
144     uint32_t            data[];
145 } QEMUCursor;
146 
147 QEMUCursor *cursor_alloc(int width, int height);
148 void cursor_get(QEMUCursor *c);
149 void cursor_put(QEMUCursor *c);
150 QEMUCursor *cursor_builtin_hidden(void);
151 QEMUCursor *cursor_builtin_left_ptr(void);
152 void cursor_print_ascii_art(QEMUCursor *c, const char *prefix);
153 int cursor_get_mono_bpl(QEMUCursor *c);
154 void cursor_set_mono(QEMUCursor *c,
155                      uint32_t foreground, uint32_t background, uint8_t *image,
156                      int transparent, uint8_t *mask);
157 void cursor_get_mono_image(QEMUCursor *c, int foreground, uint8_t *mask);
158 void cursor_get_mono_mask(QEMUCursor *c, int transparent, uint8_t *mask);
159 
160 typedef void *QEMUGLContext;
161 typedef struct QEMUGLParams QEMUGLParams;
162 
163 struct QEMUGLParams {
164     int major_ver;
165     int minor_ver;
166 };
167 
168 typedef struct DisplayChangeListenerOps {
169     const char *dpy_name;
170 
171     void (*dpy_refresh)(DisplayChangeListener *dcl);
172 
173     void (*dpy_gfx_update)(DisplayChangeListener *dcl,
174                            int x, int y, int w, int h);
175     void (*dpy_gfx_switch)(DisplayChangeListener *dcl,
176                            struct DisplaySurface *new_surface);
177     void (*dpy_gfx_copy)(DisplayChangeListener *dcl,
178                          int src_x, int src_y,
179                          int dst_x, int dst_y, int w, int h);
180     bool (*dpy_gfx_check_format)(DisplayChangeListener *dcl,
181                                  pixman_format_code_t format);
182 
183     void (*dpy_text_cursor)(DisplayChangeListener *dcl,
184                             int x, int y);
185     void (*dpy_text_resize)(DisplayChangeListener *dcl,
186                             int w, int h);
187     void (*dpy_text_update)(DisplayChangeListener *dcl,
188                             int x, int y, int w, int h);
189 
190     void (*dpy_mouse_set)(DisplayChangeListener *dcl,
191                           int x, int y, int on);
192     void (*dpy_cursor_define)(DisplayChangeListener *dcl,
193                               QEMUCursor *cursor);
194 
195     QEMUGLContext (*dpy_gl_ctx_create)(DisplayChangeListener *dcl,
196                                        QEMUGLParams *params);
197     void (*dpy_gl_ctx_destroy)(DisplayChangeListener *dcl,
198                                QEMUGLContext ctx);
199     int (*dpy_gl_ctx_make_current)(DisplayChangeListener *dcl,
200                                    QEMUGLContext ctx);
201     QEMUGLContext (*dpy_gl_ctx_get_current)(DisplayChangeListener *dcl);
202 
203     void (*dpy_gl_scanout)(DisplayChangeListener *dcl,
204                            uint32_t backing_id, bool backing_y_0_top,
205                            uint32_t x, uint32_t y, uint32_t w, uint32_t h);
206     void (*dpy_gl_update)(DisplayChangeListener *dcl,
207                           uint32_t x, uint32_t y, uint32_t w, uint32_t h);
208 
209 } DisplayChangeListenerOps;
210 
211 struct DisplayChangeListener {
212     uint64_t update_interval;
213     const DisplayChangeListenerOps *ops;
214     DisplayState *ds;
215     QemuConsole *con;
216 
217     QLIST_ENTRY(DisplayChangeListener) next;
218 };
219 
220 DisplayState *init_displaystate(void);
221 DisplaySurface *qemu_create_displaysurface_from(int width, int height,
222                                                 pixman_format_code_t format,
223                                                 int linesize, uint8_t *data);
224 DisplaySurface *qemu_create_displaysurface_guestmem(int width, int height,
225                                                     pixman_format_code_t format,
226                                                     int linesize,
227                                                     uint64_t addr);
228 PixelFormat qemu_default_pixelformat(int bpp);
229 
230 DisplaySurface *qemu_create_displaysurface(int width, int height);
231 void qemu_free_displaysurface(DisplaySurface *surface);
232 
233 static inline int is_surface_bgr(DisplaySurface *surface)
234 {
235     if (PIXMAN_FORMAT_BPP(surface->format) == 32 &&
236         PIXMAN_FORMAT_TYPE(surface->format) == PIXMAN_TYPE_ABGR) {
237         return 1;
238     } else {
239         return 0;
240     }
241 }
242 
243 static inline int is_buffer_shared(DisplaySurface *surface)
244 {
245     return !(surface->flags & QEMU_ALLOCATED_FLAG);
246 }
247 
248 void register_displaychangelistener(DisplayChangeListener *dcl);
249 void update_displaychangelistener(DisplayChangeListener *dcl,
250                                   uint64_t interval);
251 void unregister_displaychangelistener(DisplayChangeListener *dcl);
252 
253 bool dpy_ui_info_supported(QemuConsole *con);
254 int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info);
255 
256 void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h);
257 void dpy_gfx_replace_surface(QemuConsole *con,
258                              DisplaySurface *surface);
259 void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
260                   int dst_x, int dst_y, int w, int h);
261 void dpy_text_cursor(QemuConsole *con, int x, int y);
262 void dpy_text_update(QemuConsole *con, int x, int y, int w, int h);
263 void dpy_text_resize(QemuConsole *con, int w, int h);
264 void dpy_mouse_set(QemuConsole *con, int x, int y, int on);
265 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor);
266 bool dpy_cursor_define_supported(QemuConsole *con);
267 bool dpy_gfx_check_format(QemuConsole *con,
268                           pixman_format_code_t format);
269 
270 void dpy_gl_scanout(QemuConsole *con,
271                     uint32_t backing_id, bool backing_y_0_top,
272                     uint32_t x, uint32_t y, uint32_t w, uint32_t h);
273 void dpy_gl_update(QemuConsole *con,
274                    uint32_t x, uint32_t y, uint32_t w, uint32_t h);
275 
276 QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
277                                 QEMUGLParams *params);
278 void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx);
279 int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx);
280 QEMUGLContext dpy_gl_ctx_get_current(QemuConsole *con);
281 
282 bool console_has_gl(QemuConsole *con);
283 
284 static inline int surface_stride(DisplaySurface *s)
285 {
286     return pixman_image_get_stride(s->image);
287 }
288 
289 static inline void *surface_data(DisplaySurface *s)
290 {
291     return pixman_image_get_data(s->image);
292 }
293 
294 static inline int surface_width(DisplaySurface *s)
295 {
296     return pixman_image_get_width(s->image);
297 }
298 
299 static inline int surface_height(DisplaySurface *s)
300 {
301     return pixman_image_get_height(s->image);
302 }
303 
304 static inline int surface_bits_per_pixel(DisplaySurface *s)
305 {
306     int bits = PIXMAN_FORMAT_BPP(s->format);
307     return bits;
308 }
309 
310 static inline int surface_bytes_per_pixel(DisplaySurface *s)
311 {
312     int bits = PIXMAN_FORMAT_BPP(s->format);
313     return (bits + 7) / 8;
314 }
315 
316 static inline pixman_format_code_t surface_format(DisplaySurface *s)
317 {
318     return s->format;
319 }
320 
321 #ifdef CONFIG_CURSES
322 #include <curses.h>
323 typedef chtype console_ch_t;
324 extern chtype vga_to_curses[];
325 #else
326 typedef unsigned long console_ch_t;
327 #endif
328 static inline void console_write_ch(console_ch_t *dest, uint32_t ch)
329 {
330     uint8_t c = ch;
331 #ifdef CONFIG_CURSES
332     if (vga_to_curses[c]) {
333         ch &= ~(console_ch_t)0xff;
334         ch |= vga_to_curses[c];
335     }
336 #else
337     if (c == '\0') {
338         ch |= ' ';
339     }
340 #endif
341     *dest = ch;
342 }
343 
344 typedef struct GraphicHwOps {
345     void (*invalidate)(void *opaque);
346     void (*gfx_update)(void *opaque);
347     void (*text_update)(void *opaque, console_ch_t *text);
348     void (*update_interval)(void *opaque, uint64_t interval);
349     int (*ui_info)(void *opaque, uint32_t head, QemuUIInfo *info);
350 } GraphicHwOps;
351 
352 QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
353                                   const GraphicHwOps *ops,
354                                   void *opaque);
355 void graphic_console_set_hwops(QemuConsole *con,
356                                const GraphicHwOps *hw_ops,
357                                void *opaque);
358 
359 void graphic_hw_update(QemuConsole *con);
360 void graphic_hw_invalidate(QemuConsole *con);
361 void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata);
362 
363 QemuConsole *qemu_console_lookup_by_index(unsigned int index);
364 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head);
365 bool qemu_console_is_visible(QemuConsole *con);
366 bool qemu_console_is_graphic(QemuConsole *con);
367 bool qemu_console_is_fixedsize(QemuConsole *con);
368 char *qemu_console_get_label(QemuConsole *con);
369 int qemu_console_get_index(QemuConsole *con);
370 uint32_t qemu_console_get_head(QemuConsole *con);
371 QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con);
372 int qemu_console_get_width(QemuConsole *con, int fallback);
373 int qemu_console_get_height(QemuConsole *con, int fallback);
374 
375 void text_consoles_set_display(DisplayState *ds);
376 void console_select(unsigned int index);
377 void console_color_init(DisplayState *ds);
378 void qemu_console_resize(QemuConsole *con, int width, int height);
379 void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
380                        int dst_x, int dst_y, int w, int h);
381 DisplaySurface *qemu_console_surface(QemuConsole *con);
382 
383 /* console-gl.c */
384 typedef struct ConsoleGLState ConsoleGLState;
385 #ifdef CONFIG_OPENGL
386 ConsoleGLState *console_gl_init_context(void);
387 void console_gl_fini_context(ConsoleGLState *gls);
388 bool console_gl_check_format(DisplayChangeListener *dcl,
389                              pixman_format_code_t format);
390 void surface_gl_create_texture(ConsoleGLState *gls,
391                                DisplaySurface *surface);
392 void surface_gl_update_texture(ConsoleGLState *gls,
393                                DisplaySurface *surface,
394                                int x, int y, int w, int h);
395 void surface_gl_render_texture(ConsoleGLState *gls,
396                                DisplaySurface *surface);
397 void surface_gl_destroy_texture(ConsoleGLState *gls,
398                                DisplaySurface *surface);
399 void surface_gl_setup_viewport(ConsoleGLState *gls,
400                                DisplaySurface *surface,
401                                int ww, int wh);
402 #endif
403 
404 /* sdl.c */
405 void sdl_display_early_init(int opengl);
406 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame);
407 
408 /* cocoa.m */
409 void cocoa_display_init(DisplayState *ds, int full_screen);
410 
411 /* vnc.c */
412 void vnc_display_init(const char *id);
413 void vnc_display_open(const char *id, Error **errp);
414 void vnc_display_add_client(const char *id, int csock, bool skipauth);
415 char *vnc_display_local_addr(const char *id);
416 #ifdef CONFIG_VNC
417 int vnc_display_password(const char *id, const char *password);
418 int vnc_display_pw_expire(const char *id, time_t expires);
419 QemuOpts *vnc_parse(const char *str, Error **errp);
420 int vnc_init_func(void *opaque, QemuOpts *opts, Error **errp);
421 #else
422 static inline int vnc_display_password(const char *id, const char *password)
423 {
424     return -ENODEV;
425 }
426 static inline int vnc_display_pw_expire(const char *id, time_t expires)
427 {
428     return -ENODEV;
429 };
430 #endif
431 
432 /* curses.c */
433 void curses_display_init(DisplayState *ds, int full_screen);
434 
435 /* input.c */
436 int index_from_key(const char *key);
437 
438 /* gtk.c */
439 void early_gtk_display_init(int opengl);
440 void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover);
441 
442 #endif
443