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