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