xref: /openbmc/qemu/include/ui/console.h (revision 1b111dc1)
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 
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 TYPE_QEMU_CONSOLE "qemu-console"
97 #define QEMU_CONSOLE(obj) \
98     OBJECT_CHECK(QemuConsole, (obj), TYPE_QEMU_CONSOLE)
99 #define QEMU_CONSOLE_GET_CLASS(obj) \
100     OBJECT_GET_CLASS(QemuConsoleClass, (obj), TYPE_QEMU_CONSOLE)
101 #define QEMU_CONSOLE_CLASS(klass) \
102     OBJECT_CLASS_CHECK(QemuConsoleClass, (klass), TYPE_QEMU_CONSOLE)
103 
104 typedef struct QemuConsoleClass QemuConsoleClass;
105 
106 struct QemuConsoleClass {
107     ObjectClass parent_class;
108 };
109 
110 #define QEMU_BIG_ENDIAN_FLAG    0x01
111 #define QEMU_ALLOCATED_FLAG     0x02
112 
113 struct PixelFormat {
114     uint8_t bits_per_pixel;
115     uint8_t bytes_per_pixel;
116     uint8_t depth; /* color depth in bits */
117     uint32_t rmask, gmask, bmask, amask;
118     uint8_t rshift, gshift, bshift, ashift;
119     uint8_t rmax, gmax, bmax, amax;
120     uint8_t rbits, gbits, bbits, abits;
121 };
122 
123 struct DisplaySurface {
124     pixman_format_code_t format;
125     pixman_image_t *image;
126     uint8_t flags;
127 
128     struct PixelFormat pf;
129 };
130 
131 /* cursor data format is 32bit RGBA */
132 typedef struct QEMUCursor {
133     int                 width, height;
134     int                 hot_x, hot_y;
135     int                 refcount;
136     uint32_t            data[];
137 } QEMUCursor;
138 
139 QEMUCursor *cursor_alloc(int width, int height);
140 void cursor_get(QEMUCursor *c);
141 void cursor_put(QEMUCursor *c);
142 QEMUCursor *cursor_builtin_hidden(void);
143 QEMUCursor *cursor_builtin_left_ptr(void);
144 void cursor_print_ascii_art(QEMUCursor *c, const char *prefix);
145 int cursor_get_mono_bpl(QEMUCursor *c);
146 void cursor_set_mono(QEMUCursor *c,
147                      uint32_t foreground, uint32_t background, uint8_t *image,
148                      int transparent, uint8_t *mask);
149 void cursor_get_mono_image(QEMUCursor *c, int foreground, uint8_t *mask);
150 void cursor_get_mono_mask(QEMUCursor *c, int transparent, uint8_t *mask);
151 
152 typedef struct DisplayChangeListenerOps {
153     const char *dpy_name;
154 
155     void (*dpy_refresh)(DisplayChangeListener *dcl);
156 
157     void (*dpy_gfx_update)(DisplayChangeListener *dcl,
158                            int x, int y, int w, int h);
159     void (*dpy_gfx_switch)(DisplayChangeListener *dcl,
160                            struct DisplaySurface *new_surface);
161     void (*dpy_gfx_copy)(DisplayChangeListener *dcl,
162                          int src_x, int src_y,
163                          int dst_x, int dst_y, int w, int h);
164 
165     void (*dpy_text_cursor)(DisplayChangeListener *dcl,
166                             int x, int y);
167     void (*dpy_text_resize)(DisplayChangeListener *dcl,
168                             int w, int h);
169     void (*dpy_text_update)(DisplayChangeListener *dcl,
170                             int x, int y, int w, int h);
171 
172     void (*dpy_mouse_set)(DisplayChangeListener *dcl,
173                           int x, int y, int on);
174     void (*dpy_cursor_define)(DisplayChangeListener *dcl,
175                               QEMUCursor *cursor);
176 } DisplayChangeListenerOps;
177 
178 struct DisplayChangeListener {
179     uint64_t update_interval;
180     const DisplayChangeListenerOps *ops;
181     DisplayState *ds;
182     QemuConsole *con;
183 
184     QLIST_ENTRY(DisplayChangeListener) next;
185 };
186 
187 DisplayState *init_displaystate(void);
188 DisplaySurface* qemu_create_displaysurface_from(int width, int height, int bpp,
189                                                 int linesize, uint8_t *data,
190                                                 bool byteswap);
191 PixelFormat qemu_different_endianness_pixelformat(int bpp);
192 PixelFormat qemu_default_pixelformat(int bpp);
193 
194 DisplaySurface *qemu_create_displaysurface(int width, int height);
195 void qemu_free_displaysurface(DisplaySurface *surface);
196 
197 static inline int is_surface_bgr(DisplaySurface *surface)
198 {
199     if (surface->pf.bits_per_pixel == 32 && surface->pf.rshift == 0)
200         return 1;
201     else
202         return 0;
203 }
204 
205 static inline int is_buffer_shared(DisplaySurface *surface)
206 {
207     return !(surface->flags & QEMU_ALLOCATED_FLAG);
208 }
209 
210 void register_displaychangelistener(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(DeviceState *dev,
280                                   const GraphicHwOps *ops,
281                                   void *opaque);
282 
283 void graphic_hw_update(QemuConsole *con);
284 void graphic_hw_invalidate(QemuConsole *con);
285 void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata);
286 
287 QemuConsole *qemu_console_lookup_by_index(unsigned int index);
288 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev);
289 bool qemu_console_is_visible(QemuConsole *con);
290 bool qemu_console_is_graphic(QemuConsole *con);
291 bool qemu_console_is_fixedsize(QemuConsole *con);
292 
293 void text_consoles_set_display(DisplayState *ds);
294 void console_select(unsigned int index);
295 void console_color_init(DisplayState *ds);
296 void qemu_console_resize(QemuConsole *con, int width, int height);
297 void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
298                        int dst_x, int dst_y, int w, int h);
299 DisplaySurface *qemu_console_surface(QemuConsole *con);
300 DisplayState *qemu_console_displaystate(QemuConsole *console);
301 
302 typedef CharDriverState *(VcHandler)(ChardevVC *vc);
303 
304 CharDriverState *vc_init(ChardevVC *vc);
305 void register_vc_handler(VcHandler *handler);
306 
307 /* sdl.c */
308 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame);
309 
310 /* cocoa.m */
311 void cocoa_display_init(DisplayState *ds, int full_screen);
312 
313 /* vnc.c */
314 void vnc_display_init(DisplayState *ds);
315 void vnc_display_open(DisplayState *ds, const char *display, Error **errp);
316 void vnc_display_add_client(DisplayState *ds, int csock, bool skipauth);
317 char *vnc_display_local_addr(DisplayState *ds);
318 #ifdef CONFIG_VNC
319 int vnc_display_password(DisplayState *ds, const char *password);
320 int vnc_display_pw_expire(DisplayState *ds, time_t expires);
321 #else
322 static inline int vnc_display_password(DisplayState *ds, const char *password)
323 {
324     return -ENODEV;
325 }
326 static inline int vnc_display_pw_expire(DisplayState *ds, time_t expires)
327 {
328     return -ENODEV;
329 };
330 #endif
331 
332 /* curses.c */
333 void curses_display_init(DisplayState *ds, int full_screen);
334 
335 /* input.c */
336 int index_from_key(const char *key);
337 int index_from_keycode(int code);
338 
339 /* gtk.c */
340 void early_gtk_display_init(void);
341 void gtk_display_init(DisplayState *ds, bool full_screen);
342 
343 #endif
344