1 /* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */
2 
3 #ifndef ___UI_H__
4 #define ___UI_H__
5 
6 #ifndef COMPAT_LIBVTE
7 #define XDG_SHELL
8 #define ZXDG_SHELL_V6
9 #endif
10 
11 #include <wayland-client.h>
12 #include <wayland-client-protocol.h>
13 #include <wayland-cursor.h>
14 #include <wayland-egl.h>
15 #include <xkbcommon/xkbcommon.h>
16 #include "gtk-primary-selection.h"
17 #ifdef XDG_SHELL
18 #include "xdg-shell-client-protocol.h"
19 #endif
20 #ifdef ZXDG_SHELL_V6
21 #include "xdg-shell-unstable-v6-client-protocol.h"
22 #endif
23 
24 typedef int KeyCode; /* Same as type of wparam */
25 typedef int KeySym;  /* Same as type of wparam */
26 typedef unsigned long Atom; /* Same as definition in X11/X.h */
27 typedef uint32_t Time;
28 
29 typedef struct {
30   int type;
31   Time time;
32   unsigned int state;
33   KeySym ksym;
34   unsigned int keycode;
35 
36 } XKeyEvent;
37 
38 typedef struct {
39   int type;
40   Time time;
41   int x;
42   int y;
43   unsigned int state;
44   unsigned int button;
45 
46 } XButtonEvent;
47 
48 typedef struct {
49   int type;
50   Time time;
51   int x;
52   int y;
53   unsigned int state;
54 
55 } XMotionEvent;
56 
57 typedef struct {
58   int type;
59   struct ui_window *target;
60 
61 } XSelectionRequestEvent;
62 
63 typedef union {
64   int type;
65   XKeyEvent xkey;
66   XButtonEvent xbutton;
67   XMotionEvent xmotion;
68   XSelectionRequestEvent xselectionrequest;
69 
70 } XEvent;
71 
72 typedef struct {
73   struct wl_display *display;
74   struct wl_output *output;
75   struct wl_registry *registry;
76   struct wl_compositor *compositor;
77   struct wl_shm *shm;
78   struct wl_cursor_theme *cursor_theme;
79   struct wl_cursor *cursor[10];
80   struct wl_surface *cursor_surface;
81   struct wl_seat *seat;
82   struct wl_keyboard *keyboard;
83   struct wl_pointer *pointer;
84 
85   struct ui_xkb {
86     struct xkb_context *ctx;
87     struct xkb_keymap *keymap;
88     struct xkb_state *state;
89     xkb_mod_index_t ctrl;
90     xkb_mod_index_t alt;
91     xkb_mod_index_t shift;
92     xkb_mod_index_t logo;
93     unsigned int mods;
94   } * xkb;
95 
96   struct wl_surface *current_kbd_surface;
97   struct wl_surface *current_pointer_surface;
98 
99   int pointer_x;
100   int pointer_y;
101   int pointer_button;
102 
103   int current_cursor;
104 
105   int kbd_repeat_wait;
106   XKeyEvent prev_kev;
107   unsigned int kbd_repeat_count;
108 
109   struct wl_data_device_manager *data_device_manager;
110   struct wl_data_device *data_device;
111   struct wl_data_offer *dnd_offer;
112   struct wl_surface *data_surface;
113   uint32_t dnd_action;
114   struct wl_data_offer *sel_offer;
115   char *sel_offer_mime;
116   struct wl_data_source *sel_source;
117 
118   struct gtk_primary_selection_device_manager *xsel_device_manager;
119   struct gtk_primary_selection_device *xsel_device;
120   struct gtk_primary_selection_offer *xsel_offer;
121   char *xsel_offer_mime;
122   struct gtk_primary_selection_source *xsel_source;
123 
124   int32_t sel_fd;
125   uint32_t serial;
126 
127   int ref_count;
128 
129   /*
130    * These members should be placed at the end of this structure because
131    * ui_*.c except ui_display.c refers this structure correctly
132    * if COMPAT_LIBVTE is defined or not.
133    */
134 #ifdef COMPAT_LIBVTE
135   struct wl_subcompositor *subcompositor;
136   /* for sway */
137   int shell_type; /* 0: Not determined, 1: wl_shell, 2: zxdg_shell, 3: xdg_shell */
138 #else
139   struct wl_shell *shell;
140 #ifdef XDG_SHELL
141   struct xdg_wm_base *xdg_shell;
142 #endif
143 #ifdef ZXDG_SHELL_V6
144   struct zxdg_shell_v6 *zxdg_shell;
145 #endif
146 #endif
147 
148 } ui_wlserv_t;
149 
150 typedef struct {
151   ui_wlserv_t *wlserv;
152   unsigned char *fb;
153   struct wl_buffer *buffer;
154   struct wl_surface *surface;
155 
156   unsigned int bytes_per_pixel;
157   unsigned int line_length;
158 
159   int lock_state;
160 
161   struct rgb_info {
162     unsigned int r_limit;
163     unsigned int g_limit;
164     unsigned int b_limit;
165     unsigned int r_offset;
166     unsigned int g_offset;
167     unsigned int b_offset;
168 
169   } rgbinfo;
170 
171   unsigned int width;
172   unsigned int height;
173 
174   int damage_x;
175   int damage_y;
176   unsigned int damage_width;
177   unsigned int damage_height;
178 
179   int is_resizing;
180 
181   struct ui_display *parent;
182 
183   /*
184    * These members should be placed at the end of this structure because
185    * ui_*.c except ui_display.c refers this structure correctly
186    * if COMPAT_LIBVTE is defined or not.
187    */
188 #ifdef COMPAT_LIBVTE
189   struct wl_surface *parent_surface;
190   struct wl_subsurface *subsurface;
191   int x;
192   int y;
193 #else /* COMPAT_LIBVTE */
194   struct wl_shell_surface *shell_surface;
195 #ifdef ZXDG_SHELL_V6
196   struct zxdg_surface_v6 *zxdg_surface;
197   struct zxdg_toplevel_v6 *zxdg_toplevel;
198   struct zxdg_popup_v6 *zxdg_popup;
199   int zxdg_surface_configured;
200 #endif
201 #ifdef XDG_SHELL
202   struct xdg_surface *xdg_surface;
203   struct xdg_toplevel *xdg_toplevel;
204   struct xdg_popup *xdg_popup;
205   int xdg_surface_configured;
206 #endif
207 #endif /* COMPAT_LIBVTE */
208 
209 } Display;
210 
211 #define PIXEL_RED(pixel, rgbinfo) (((pixel) >> (rgbinfo).r_offset) << (rgbinfo).r_limit)
212 #define PIXEL_BLUE(pixel, rgbinfo) (((pixel) >> (rgbinfo).b_offset) << (rgbinfo).b_limit)
213 #define PIXEL_GREEN(pixel, rgbinfo) (((pixel) >> (rgbinfo).g_offset) << (rgbinfo).g_limit)
214 #define RGB_TO_PIXEL(r, g, b, rgbinfo)                  \
215   ((((r) >> (rgbinfo).r_limit) << (rgbinfo).r_offset) | \
216    (((g) >> (rgbinfo).g_limit) << (rgbinfo).g_offset) | \
217    (((b) >> (rgbinfo).b_limit) << (rgbinfo).b_offset))
218 
219 typedef int XIC; /* dummy */
220 typedef void *XID; /* dummy */
221 typedef void *Window; /* dummy */
222 typedef void *Drawable; /* dummy */
223 typedef struct {
224   unsigned char *image;
225   unsigned int width;
226   unsigned int height;
227 } * Pixmap;
228 typedef unsigned char *PixmapMask;
229 typedef int GC;
230 typedef int Font;
231 typedef int Cursor;
232 
233 typedef struct /* Same as definition in X11/X.h */
234     {
235   int max_keypermod;
236   KeyCode *modifiermap;
237 
238 } XModifierKeymap;
239 
240 typedef struct /* Same as definition in X11/X.h */
241     {
242   unsigned char byte1;
243   unsigned char byte2;
244 
245 } XChar2b;
246 
247 /*
248  * XFontStruct must be the same in fb, wayland, sdl2 and Android.
249  */
250 typedef struct _XFontStruct {
251   char *file;
252 
253   int32_t num_glyphs; /* Not used on freetype */
254   unsigned char *glyphs; /* Cast to unsigned char** on FreeType */
255   void *glyph_indeces; /* PCF: int16_t*, decsp: int16_t*, FreeType: u_int32_t* */
256 
257   /* If is_aa is true, don't use glyph_width_bytes except in next_glyph_buf(). */
258   int32_t glyph_width_bytes;
259 
260   uint16_t width;
261   /* Width of full width characters or max width of half width characters. */
262   uint16_t width_full;
263   uint16_t height;
264   uint16_t ascent;
265   uint8_t has_each_glyph_width_info;
266 
267   /* for pcf */
268   int16_t min_char_or_byte2;
269   int16_t max_char_or_byte2;
270   int16_t min_byte1;
271   int16_t max_byte1;
272   int32_t *glyph_offsets;
273 
274 #ifdef USE_FREETYPE
275   /*
276    * XXX
277    *
278    * (fontsize|FONT_BOLD|FONT_ITALIC|FONT_ROTATED) on freetype.
279    * fontsize is 0-0x1ff.
280    */
281   int32_t format;
282   void *face;
283   uint32_t num_indeces;
284   uint32_t num_glyph_bufs;
285   uint32_t glyph_buf_left;
286   int is_aa;
287 
288 #ifdef USE_FONTCONFIG
289   struct _XFontStruct **compl_xfonts;
290 #endif
291 #endif
292 
293   unsigned int ref_count;
294 
295 } XFontStruct;
296 
297 typedef int XFontSet; /* dummy */
298 
299 #define None 0L     /* Same as definition in X11/X.h */
300 #define NoSymbol 0L /* Same as definition in X11/X.h */
301 
302 #define CurrentTime 0L /* Same as definition in X11/X.h */
303 
304 /* Same as definition in X11/X.h */
305 #define NoEventMask 0L
306 #define KeyPressMask (1L << 0)
307 #define KeyReleaseMask (1L << 1)
308 #define ButtonPressMask (1L << 2)
309 #define ButtonReleaseMask (1L << 3)
310 #define EnterWindowMask (1L << 4)
311 #define LeaveWindowMask (1L << 5)
312 #define PointerMotionMask (1L << 6)
313 #define PointerMotionHintMask (1L << 7)
314 #define Button1MotionMask (1L << 8)
315 #define Button2MotionMask (1L << 9)
316 #define Button3MotionMask (1L << 10)
317 #define Button4MotionMask (1L << 11)
318 #define Button5MotionMask (1L << 12)
319 #define ButtonMotionMask (1L << 13)
320 #define KeymapStateMask (1L << 14)
321 #define ExposureMask (1L << 15)
322 #define VisibilityChangeMask (1L << 16)
323 #define StructureNotifyMask (1L << 17)
324 #define ResizeRedirectMask (1L << 18)
325 #define SubstructureNotifyMask (1L << 19)
326 #define SubstructureRedirectMask (1L << 20)
327 #define FocusChangeMask (1L << 21)
328 #define PropertyChangeMask (1L << 22)
329 #define ColormapChangeMask (1L << 23)
330 #define OwnerGrabButtonMask (1L << 24)
331 #define ShiftMask (1 << 0)
332 #define LockMask (1 << 1)
333 #define ControlMask (1 << 2)
334 #define Mod1Mask (1 << 3)
335 #define Mod2Mask (1 << 4)
336 #define Mod3Mask (1 << 5)
337 #define Mod4Mask (1 << 6)
338 #define Mod5Mask (1 << 7)
339 #define Button1Mask (1 << 8)
340 #define Button2Mask (1 << 9)
341 #define Button3Mask (1 << 10)
342 #define Button4Mask (1 << 11)
343 #define Button5Mask (1 << 12)
344 #define Button1 1
345 #define Button2 2
346 #define Button3 3
347 #define Button4 4
348 #define Button5 5
349 
350 #define XK_Super_L XKB_KEY_Super_L
351 #define XK_Super_R XKB_KEY_Super_R
352 #define XK_Hyper_L XKB_KEY_Hyper_L
353 #define XK_Hyper_R XKB_KEY_Hyper_R
354 #define XK_BackSpace XKB_KEY_BackSpace
355 #define XK_Tab XKB_KEY_Tab
356 #define XK_Clear XKB_KEY_Clear
357 #define XK_Linefeed XKB_KEY_Linefeed
358 #define XK_Return XKB_KEY_Return
359 
360 #define XK_Shift_L XKB_KEY_Shift_L
361 #define XK_Control_L XKB_KEY_Control_L
362 #define XK_Alt_L XKB_KEY_Alt_L
363 #define XK_Shift_R XKB_KEY_Shift_R
364 #define XK_Control_R XKB_KEY_Control_R
365 #define XK_Alt_R XKB_KEY_Alt_R
366 
367 #define XK_Meta_L XKB_KEY_Meta_L
368 #define XK_Meta_R XKB_KEY_Meta_R
369 
370 #define XK_Pause XKB_KEY_Pause
371 #define XK_Shift_Lock XKB_KEY_Shift_Lock
372 #define XK_Caps_Lock XKB_KEY_Caps_Lock
373 #define XK_Escape XKB_KEY_Escape
374 #define XK_Prior XKB_KEY_Prior
375 #define XK_Next XKB_KEY_Next
376 #define XK_End XKB_KEY_End
377 #define XK_Home XKB_KEY_Home
378 #define XK_Left XKB_KEY_Left
379 #define XK_Up XKB_KEY_Up
380 #define XK_Right XKB_KEY_Right
381 #define XK_Down XKB_KEY_Down
382 #define XK_Select XKB_KEY_Select
383 #define XK_Print XKB_KEY_Print
384 #define XK_Execute XKB_KEY_Execute
385 #define XK_Insert XKB_KEY_Insert
386 #define XK_Delete XKB_KEY_Delete
387 #define XK_Help XKB_KEY_Help
388 #define XK_F1 XKB_KEY_F1
389 #define XK_F2 XKB_KEY_F2
390 #define XK_F3 XKB_KEY_F3
391 #define XK_F4 XKB_KEY_F4
392 #define XK_F5 XKB_KEY_F5
393 #define XK_F6 XKB_KEY_F6
394 #define XK_F7 XKB_KEY_F7
395 #define XK_F8 XKB_KEY_F8
396 #define XK_F9 XKB_KEY_F9
397 #define XK_F10 XKB_KEY_F10
398 #define XK_F11 XKB_KEY_F11
399 #define XK_F12 XKB_KEY_F12
400 #define XK_F13 XKB_KEY_F13
401 #define XK_F14 XKB_KEY_F14
402 #define XK_F15 XKB_KEY_F15
403 #define XK_F16 XKB_KEY_F16
404 #define XK_F17 XKB_KEY_F17
405 #define XK_F18 XKB_KEY_F18
406 #define XK_F19 XKB_KEY_F19
407 #define XK_F20 XKB_KEY_F20
408 #define XK_F21 XKB_KEY_F21
409 #define XK_F22 XKB_KEY_F22
410 #define XK_F23 XKB_KEY_F23
411 #define XK_F24 XKB_KEY_F24
412 #define XK_FMAX XKB_KEY_F24
413 #define XK_Num_Lock XKB_KEY_Num_Lock
414 #define XK_Scroll_Lock XKB_KEY_Scroll_Lock
415 #define XK_Find XKB_KEY_Find
416 #define XK_Menu XKB_KEY_Menu
417 #define XK_Begin XKB_KEY_Begin
418 #define XK_Muhenkan XKB_KEY_Muhenkan
419 #define XK_Henkan_Mode XKB_KEY_Henkan_Mode
420 #define XK_Zenkaku_Hankaku XKB_KEY_Zenkaku_Hankaku
421 #define XK_Hiragana_Katakana 0xfffe /* dummy */
422 
423 #define XK_KP_Prior XKB_KEY_KP_Prior
424 #define XK_KP_Next XKB_KEY_KP_Next
425 #define XK_KP_End XKB_KEY_KP_End
426 #define XK_KP_Home XKB_KEY_KP_Home
427 #define XK_KP_Left XKB_KEY_KP_Left
428 #define XK_KP_Up XKB_KEY_KP_Up
429 #define XK_KP_Right XKB_KEY_KP_Right
430 #define XK_KP_Down XKB_KEY_KP_Down
431 #define XK_KP_Insert XKB_KEY_KP_Insert
432 #define XK_KP_Delete XKB_KEY_KP_Delete
433 #define XK_KP_F1 XKB_KEY_KP_F1
434 #define XK_KP_F2 XKB_KEY_KP_F2
435 #define XK_KP_F3 XKB_KEY_KP_F3
436 #define XK_KP_F4 XKB_KEY_KP_F4
437 #define XK_KP_Begin XKB_KEY_KP_Begin
438 #define XK_KP_Multiply XKB_KEY_KP_Multiply
439 #define XK_KP_Add XKB_KEY_KP_Add
440 #define XK_KP_Separator XKB_KEY_KP_Separator
441 #define XK_KP_Subtract XKB_KEY_KP_Subtract
442 #define XK_KP_Decimal XKB_KEY_KP_Decimal
443 #define XK_KP_Divide XKB_KEY_KP_Divide
444 #define XK_KP_0 XKB_KEY_KP_0
445 #define XK_KP_1 XKB_KEY_KP_1
446 #define XK_KP_2 XKB_KEY_KP_2
447 #define XK_KP_3 XKB_KEY_KP_3
448 #define XK_KP_4 XKB_KEY_KP_4
449 #define XK_KP_5 XKB_KEY_KP_5
450 #define XK_KP_6 XKB_KEY_KP_6
451 #define XK_KP_7 XKB_KEY_KP_7
452 #define XK_KP_8 XKB_KEY_KP_8
453 #define XK_KP_9 XKB_KEY_KP_9
454 
455 #define IsKeypadKey(ksym) (XKB_KEY_KP_Space <= (ksym) && (ksym) < XKB_KEY_F1)
456 #define IsModifierKey(ksym) (0)
457 
458 #define XK_ISO_Left_Tab XKB_KEY_ISO_Left_Tab
459 
460 typedef struct {
461   short x;
462   short y;
463 } XPoint;
464 
465 /* XXX dummy */
466 #define XKeysymToKeycode(disp, ks) (ks)
467 #define XKeycodeToKeysym(disp, kc, i) (kc)
468 #define XKeysymToString(ks) ""
469 #define DefaultScreen(disp) (0)
470 
471 #define BlackPixel(disp, screen) (0xff000000 | RGB(0, 0, 0))
472 #define WhitePixel(disp, screen) (0xff000000 | RGB(0xff, 0xff, 0xff))
473 
474 /* Same as definition in X11/cursorfont.h */
475 #define XC_xterm 152
476 #define XC_left_ptr 68
477 
478 /* Same as definition in X11/Xutil.h */
479 #define NoValue 0x0000
480 #define XValue 0x0001
481 #define YValue 0x0002
482 #define WidthValue 0x0004
483 #define HeightValue 0x0008
484 #define AllValues 0x000F
485 #define XNegative 0x0010
486 #define YNegative 0x0020
487 
488 int XParseGeometry(char *str, int *x, int *y, unsigned int *width, unsigned int *height);
489 
490 KeySym XStringToKeysym(char *str);
491 
492 /* === Platform dependent options === */
493 
494 #define UI_COLOR_HAS_RGB
495 #define SUPPORT_TRUE_TRANSPARENT_BG
496 #ifdef USE_FREETYPE
497 #define TYPE_XCORE_SCALABLE
498 #else
499 #undef TYPE_XCORE_SCALABLE
500 #endif
501 #define MANAGE_SUB_WINDOWS_BY_MYSELF
502 /* ui_im_{candidate|status}_screen.c, ui_window.c */
503 #undef MANAGE_ROOT_WINDOWS_BY_MYSELF
504 #define INLINE_PICTURE_MOVABLE_BETWEEN_DISPLAYS
505 #undef SUPPORT_POINT_SIZE_FONT
506 #undef XIM_SPOT_IS_LINE_TOP
507 #undef USE_GC
508 #undef CHANGEABLE_CURSOR
509 #define PLUGIN_MODULE_SUFFIX "wl"
510 #define KEY_REPEAT_BY_MYSELF
511 #define ROTATABLE_DISPLAY
512 #undef PSEUDO_COLOR_DISPLAY
513 #undef WALL_PICTURE_SIXEL_REPLACES_SYSTEM_PALETTE
514 #undef SUPPORT_URGENT_BELL
515 #undef FORCE_UNICODE
516 #define NEED_DISPLAY_SYNC_EVERY_TIME
517 #define DRAW_SCREEN_IN_PIXELS
518 #undef NO_DRAW_IMAGE_STRING
519 /* libpthread is not linked to mlterm explicitly for now. */
520 #undef HAVE_PTHREAD
521 #define COMPOSE_DECSP_FONT
522 #ifdef USE_FREETYPE
523 #define USE_REAL_VERTICAL_FONT
524 #else
525 #undef USE_REAL_VERTICAL_FONT
526 #endif
527 #undef NO_DISPLAY_FD
528 #undef FLICK_SCROLL
529 #undef UIWINDOW_SUPPORTS_PREEDITING
530 
531 #endif
532