v / vlib / sokol / sapp / sapp_linux.c.v
1167 lines · 986 sloc · 35.45 KB · f0680224c242a81b3a418ed0e83f9ffcb2e4487f
Raw
1@[has_globals]
2module sapp
3
4$if linux_wayland_session ? {
5 $if !sokol_wayland ? {
6 $compile_error('`gg`/`sokol.sapp` cannot run in a Wayland-only Linux session without `-d sokol_wayland`.')
7 }
8}
9
10#preinclude <X11/Xlib.h>
11#preinclude <X11/Xatom.h>
12#preinclude <X11/Xutil.h>
13#preinclude <X11/Xresource.h>
14#preinclude <X11/keysym.h>
15#preinclude <X11/XKBlib.h>
16#preinclude <X11/Xcursor/Xcursor.h>
17#preinclude <X11/extensions/XInput2.h>
18
19// C library bindings for Linux Wayland/X11 backends.
20// These declare the external C types and functions from:
21// - wayland-client, wayland-egl, wayland-cursor (when SOKOL_WAYLAND is defined)
22// - xkbcommon
23// - EGL
24// - Wayland protocol generated headers (when SOKOL_WAYLAND is defined)
25// - Linux syscalls (timerfd, mmap)
26
27$if sokol_wayland ? {
28 // === wayland-client core types ===
29
30 pub struct C.wl_display {}
31
32 pub struct C.wl_registry {}
33
34 pub struct C.wl_compositor {}
35
36 pub struct C.wl_surface {}
37
38 pub struct C.wl_seat {}
39
40 pub struct C.wl_pointer {}
41
42 pub struct C.wl_keyboard {}
43
44 pub struct C.wl_touch {}
45
46 pub struct C.wl_shm {}
47
48 pub struct C.wl_output {}
49
50 pub struct C.wl_data_device_manager {}
51
52 pub struct C.wl_data_device {}
53
54 pub struct C.wl_data_source {}
55
56 pub struct C.wl_data_offer {}
57
58 pub struct C.wl_proxy {}
59
60 pub struct C.wl_interface {}
61
62 pub struct C.wl_array {
63 size usize
64 alloc usize
65 data voidptr
66 }
67
68 // === wayland-client functions ===
69
70 fn C.wl_display_connect(name &char) &C.wl_display
71 fn C.wl_display_disconnect(display &C.wl_display)
72 fn C.wl_display_dispatch(display &C.wl_display) int
73 fn C.wl_display_dispatch_pending(display &C.wl_display) int
74 fn C.wl_display_roundtrip(display &C.wl_display) int
75 fn C.wl_display_flush(display &C.wl_display) int
76 fn C.wl_display_get_registry(display &C.wl_display) &C.wl_registry
77
78 fn C.wl_registry_bind(registry &C.wl_registry, name u32, iface &C.wl_interface, version u32) voidptr
79 fn C.wl_registry_destroy(registry &C.wl_registry)
80
81 fn C.wl_compositor_create_surface(compositor &C.wl_compositor) &C.wl_surface
82 fn C.wl_compositor_destroy(compositor &C.wl_compositor)
83
84 fn C.wl_surface_destroy(surface &C.wl_surface)
85 fn C.wl_surface_commit(surface &C.wl_surface)
86 fn C.wl_surface_set_buffer_scale(surface &C.wl_surface, scale i32)
87
88 fn C.wl_seat_get_pointer(seat &C.wl_seat) &C.wl_pointer
89 fn C.wl_seat_get_keyboard(seat &C.wl_seat) &C.wl_keyboard
90 fn C.wl_seat_get_touch(seat &C.wl_seat) &C.wl_touch
91 fn C.wl_seat_destroy(seat &C.wl_seat)
92
93 fn C.wl_pointer_destroy(pointer &C.wl_pointer)
94 fn C.wl_keyboard_destroy(keyboard &C.wl_keyboard)
95 fn C.wl_touch_destroy(touch &C.wl_touch)
96 fn C.wl_shm_destroy(shm &C.wl_shm)
97
98 fn C.wl_data_device_manager_get_data_device(manager &C.wl_data_device_manager, seat &C.wl_seat) &C.wl_data_device
99 fn C.wl_data_device_manager_create_data_source(manager &C.wl_data_device_manager) &C.wl_data_source
100 fn C.wl_data_device_destroy(device &C.wl_data_device)
101 fn C.wl_data_source_destroy(source &C.wl_data_source)
102 fn C.wl_data_source_offer(source &C.wl_data_source, mime_type &char)
103 fn C.wl_data_offer_destroy(offer &C.wl_data_offer)
104 fn C.wl_data_offer_receive(offer &C.wl_data_offer, mime_type &char, fd int)
105 fn C.wl_data_offer_accept(offer &C.wl_data_offer, serial u32, mime_type &char)
106 fn C.wl_data_offer_set_actions(offer &C.wl_data_offer, dnd_actions u32, preferred_action u32)
107 fn C.wl_data_offer_finish(offer &C.wl_data_offer)
108 fn C.wl_data_device_manager_destroy(manager &C.wl_data_device_manager)
109
110 fn C.wl_proxy_add_listener(proxy &C.wl_proxy, implementation voidptr, data voidptr) int
111 fn C.wl_proxy_get_version(proxy &C.wl_proxy) u32
112
113 fn C.wl_fixed_to_double(f i32) f64
114
115 // wl_registry_add_listener, wl_seat_add_listener, etc. are static inline functions
116 // that call wl_proxy_add_listener. We declare them as C functions since they're
117 // in the included headers.
118 fn C.wl_registry_add_listener(registry &C.wl_registry, listener voidptr, data voidptr) int
119 fn C.wl_seat_add_listener(seat &C.wl_seat, listener voidptr, data voidptr) int
120 fn C.wl_pointer_add_listener(pointer &C.wl_pointer, listener voidptr, data voidptr) int
121 fn C.wl_keyboard_add_listener(keyboard &C.wl_keyboard, listener voidptr, data voidptr) int
122 fn C.wl_data_device_add_listener(device &C.wl_data_device, listener voidptr, data voidptr) int
123 fn C.wl_data_offer_add_listener(offer &C.wl_data_offer, listener voidptr, data voidptr) int
124
125 // === wayland-egl ===
126
127 pub struct C.wl_egl_window {}
128
129 fn C.wl_egl_window_create(surface &C.wl_surface, width int, height int) &C.wl_egl_window
130 fn C.wl_egl_window_destroy(window &C.wl_egl_window)
131 fn C.wl_egl_window_resize(window &C.wl_egl_window, width int, height int, dx int, dy int)
132
133 // === wayland-cursor ===
134
135 pub struct C.wl_cursor_theme {}
136
137 pub struct C.wl_cursor {
138 image_count u32
139 images &&C.wl_cursor_image
140 name &char
141 }
142
143 pub struct C.wl_cursor_image {
144 width u32
145 height u32
146 hotspot_x u32
147 hotspot_y u32
148 delay u32
149 }
150
151 fn C.wl_cursor_theme_load(name &char, size int, shm &C.wl_shm) &C.wl_cursor_theme
152 fn C.wl_cursor_theme_destroy(theme &C.wl_cursor_theme)
153 fn C.wl_cursor_theme_get_cursor(theme &C.wl_cursor_theme, name &char) &C.wl_cursor
154 fn C.wl_cursor_image_get_buffer(image &C.wl_cursor_image) &C.wl_buffer
155
156 pub struct C.wl_buffer {}
157
158 fn C.wl_surface_attach(surface &C.wl_surface, buffer &C.wl_buffer, x i32, y i32)
159
160 // === Wayland protocol interfaces (extern globals from generated .c files) ===
161
162 #include "xdg-shell-client-protocol.h"
163 #include "fractional-scale-v1-client-protocol.h"
164 #include "cursor-shape-v1-client-protocol.h"
165 #include "pointer-constraints-unstable-v1-client-protocol.h"
166 #include "relative-pointer-unstable-v1-client-protocol.h"
167 #include "viewporter-client-protocol.h"
168 #include "xdg-decoration-unstable-v1-client-protocol.h"
169
170 // Protocol object types
171 pub struct C.xdg_wm_base {}
172
173 pub struct C.xdg_surface {}
174
175 pub struct C.xdg_toplevel {}
176
177 pub struct C.wp_fractional_scale_manager_v1 {}
178
179 pub struct C.wp_fractional_scale_v1 {}
180
181 pub struct C.wp_viewporter {}
182
183 pub struct C.wp_viewport {}
184
185 pub struct C.wp_cursor_shape_manager_v1 {}
186
187 pub struct C.wp_cursor_shape_device_v1 {}
188
189 pub struct C.zwp_pointer_constraints_v1 {}
190
191 pub struct C.zwp_locked_pointer_v1 {}
192
193 pub struct C.zwp_relative_pointer_manager_v1 {}
194
195 pub struct C.zwp_relative_pointer_v1 {}
196
197 pub struct C.zxdg_decoration_manager_v1 {}
198
199 pub struct C.zxdg_toplevel_decoration_v1 {}
200
201 // Protocol interface globals (extern const struct wl_interface defined in generated .c and system headers)
202 __global C.wl_compositor_interface C.wl_interface
203 __global C.wl_seat_interface C.wl_interface
204 __global C.wl_shm_interface C.wl_interface
205 __global C.wl_output_interface C.wl_interface
206 __global C.wl_data_device_manager_interface C.wl_interface
207 __global C.xdg_wm_base_interface C.wl_interface
208 __global C.xdg_surface_interface C.wl_interface
209 __global C.wp_fractional_scale_manager_v1_interface C.wl_interface
210 __global C.wp_viewporter_interface C.wl_interface
211 __global C.wp_cursor_shape_manager_v1_interface C.wl_interface
212 __global C.zwp_pointer_constraints_v1_interface C.wl_interface
213 __global C.zwp_relative_pointer_manager_v1_interface C.wl_interface
214 __global C.zxdg_decoration_manager_v1_interface C.wl_interface
215
216 // Protocol functions (static inline in generated headers)
217 fn C.xdg_wm_base_get_xdg_surface(wm_base &C.xdg_wm_base, surface &C.wl_surface) &C.xdg_surface
218 fn C.xdg_wm_base_destroy(wm_base &C.xdg_wm_base)
219 fn C.xdg_wm_base_pong(wm_base &C.xdg_wm_base, serial u32)
220
221 fn C.xdg_surface_get_toplevel(xdg_surface &C.xdg_surface) &C.xdg_toplevel
222 fn C.xdg_surface_ack_configure(xdg_surface &C.xdg_surface, serial u32)
223 fn C.xdg_surface_destroy(xdg_surface &C.xdg_surface)
224 fn C.xdg_surface_add_listener(xdg_surface &C.xdg_surface, listener voidptr, data voidptr) int
225
226 fn C.xdg_toplevel_set_title(toplevel &C.xdg_toplevel, title &char)
227 fn C.xdg_toplevel_set_app_id(toplevel &C.xdg_toplevel, app_id &char)
228 fn C.xdg_toplevel_set_fullscreen(toplevel &C.xdg_toplevel, output &C.wl_output)
229 fn C.xdg_toplevel_unset_fullscreen(toplevel &C.xdg_toplevel)
230 fn C.xdg_toplevel_set_min_size(toplevel &C.xdg_toplevel, width i32, height i32)
231 fn C.xdg_toplevel_set_max_size(toplevel &C.xdg_toplevel, width i32, height i32)
232 fn C.xdg_toplevel_destroy(toplevel &C.xdg_toplevel)
233 fn C.xdg_toplevel_add_listener(toplevel &C.xdg_toplevel, listener voidptr, data voidptr) int
234
235 fn C.wp_fractional_scale_manager_v1_get_fractional_scale(manager &C.wp_fractional_scale_manager_v1, surface &C.wl_surface) &C.wp_fractional_scale_v1
236 fn C.wp_fractional_scale_manager_v1_destroy(manager &C.wp_fractional_scale_manager_v1)
237 fn C.wp_fractional_scale_v1_add_listener(scale &C.wp_fractional_scale_v1, listener voidptr, data voidptr) int
238 fn C.wp_fractional_scale_v1_destroy(scale &C.wp_fractional_scale_v1)
239
240 fn C.wp_viewporter_get_viewport(viewporter &C.wp_viewporter, surface &C.wl_surface) &C.wp_viewport
241 fn C.wp_viewporter_destroy(viewporter &C.wp_viewporter)
242 fn C.wp_viewport_set_destination(viewport &C.wp_viewport, width i32, height i32)
243 fn C.wp_viewport_destroy(viewport &C.wp_viewport)
244
245 fn C.wp_cursor_shape_manager_v1_get_pointer(manager &C.wp_cursor_shape_manager_v1, pointer &C.wl_pointer) &C.wp_cursor_shape_device_v1
246 fn C.wp_cursor_shape_manager_v1_destroy(manager &C.wp_cursor_shape_manager_v1)
247 fn C.wp_cursor_shape_device_v1_set_shape(device &C.wp_cursor_shape_device_v1, serial u32, shape u32)
248 fn C.wp_cursor_shape_device_v1_destroy(device &C.wp_cursor_shape_device_v1)
249
250 fn C.zwp_pointer_constraints_v1_lock_pointer(constraints &C.zwp_pointer_constraints_v1, surface &C.wl_surface, pointer &C.wl_pointer, region voidptr, lifetime u32) &C.zwp_locked_pointer_v1
251 fn C.zwp_pointer_constraints_v1_destroy(constraints &C.zwp_pointer_constraints_v1)
252 fn C.zwp_locked_pointer_v1_destroy(locked &C.zwp_locked_pointer_v1)
253
254 fn C.zwp_relative_pointer_manager_v1_get_relative_pointer(manager &C.zwp_relative_pointer_manager_v1, pointer &C.wl_pointer) &C.zwp_relative_pointer_v1
255 fn C.zwp_relative_pointer_manager_v1_destroy(manager &C.zwp_relative_pointer_manager_v1)
256 fn C.zwp_relative_pointer_v1_destroy(rp &C.zwp_relative_pointer_v1)
257 fn C.zwp_relative_pointer_v1_add_listener(rp &C.zwp_relative_pointer_v1, listener voidptr, data voidptr) int
258
259 fn C.zxdg_decoration_manager_v1_get_toplevel_decoration(manager &C.zxdg_decoration_manager_v1, toplevel &C.xdg_toplevel) &C.zxdg_toplevel_decoration_v1
260 fn C.zxdg_decoration_manager_v1_destroy(manager &C.zxdg_decoration_manager_v1)
261 fn C.zxdg_toplevel_decoration_v1_set_mode(decoration &C.zxdg_toplevel_decoration_v1, mode u32)
262 fn C.zxdg_toplevel_decoration_v1_destroy(decoration &C.zxdg_toplevel_decoration_v1)
263
264 // === xkbcommon ===
265
266 pub struct C.xkb_context {}
267
268 pub struct C.xkb_keymap {}
269
270 pub struct C.xkb_state {}
271
272 pub struct C.xkb_compose_table {}
273
274 pub struct C.xkb_compose_state {}
275
276 pub type Xkb_keysym_t = u32
277
278 fn C.xkb_context_new(flags int) &C.xkb_context
279 fn C.xkb_context_unref(ctx &C.xkb_context)
280
281 fn C.xkb_keymap_new_from_string(ctx &C.xkb_context, str &char, format int, flags int) &C.xkb_keymap
282 fn C.xkb_keymap_unref(keymap &C.xkb_keymap)
283
284 fn C.xkb_state_new(keymap &C.xkb_keymap) &C.xkb_state
285 fn C.xkb_state_unref(state &C.xkb_state)
286 fn C.xkb_state_key_get_one_sym(state &C.xkb_state, key u32) Xkb_keysym_t
287 fn C.xkb_state_key_get_utf8(state &C.xkb_state, key u32, buf &char, size usize) int
288 fn C.xkb_state_update_mask(state &C.xkb_state, depressed u32, latched u32, locked u32, dep_group u32, lat_group u32, lock_group u32) int
289 fn C.xkb_state_mod_name_is_active(state &C.xkb_state, name &char, @type int) int
290
291 fn C.xkb_compose_table_new_from_locale(ctx &C.xkb_context, locale &char, flags int) &C.xkb_compose_table
292 fn C.xkb_compose_table_unref(table &C.xkb_compose_table)
293 fn C.xkb_compose_state_new(table &C.xkb_compose_table, flags int) &C.xkb_compose_state
294 fn C.xkb_compose_state_unref(state &C.xkb_compose_state)
295
296 // xkbcommon constants
297 const xkb_context_no_flags = 0
298 const xkb_keymap_format_text_v1 = 1
299 const xkb_keymap_compile_no_flags = 0
300 const xkb_state_mods_effective = 8
301 const xkb_mod_name_shift = c'Shift'
302 const xkb_mod_name_ctrl = c'Control'
303 const xkb_mod_name_alt = c'Mod1'
304 const xkb_mod_name_logo = c'Mod4'
305
306 // Wayland keyboard state constants
307 const wl_keyboard_keymap_format_xkb_v1 = u32(1)
308 const wl_keyboard_key_state_pressed = u32(1)
309 const wl_keyboard_key_state_released = u32(0)
310 const wl_pointer_button_state_pressed = u32(1)
311 const wl_pointer_button_state_released = u32(0)
312 const wl_pointer_axis_vertical_scroll = u32(0)
313 const wl_pointer_axis_horizontal_scroll = u32(1)
314
315 // wl_seat capabilities
316 const wl_seat_capability_pointer = u32(1)
317 const wl_seat_capability_keyboard = u32(2)
318 const wl_seat_capability_touch = u32(4)
319
320 // XDG toplevel states
321 const xdg_toplevel_state_maximized = u32(1)
322 const xdg_toplevel_state_fullscreen = u32(2)
323 const xdg_toplevel_state_resizing = u32(3)
324 const xdg_toplevel_state_activated = u32(4)
325
326 // zxdg_toplevel_decoration_v1 modes
327 const zxdg_toplevel_decoration_v1_mode_client_side = u32(1)
328 const zxdg_toplevel_decoration_v1_mode_server_side = u32(2)
329
330 // zwp_pointer_constraints_v1 lifetime
331 const zwp_pointer_constraints_v1_lifetime_persistent = u32(2)
332
333 // wl_data_device_manager DND actions
334 const wl_data_device_manager_dnd_action_copy = u32(1)
335
336 // wp_cursor_shape constants (matching sapp MouseCursor values)
337 const wp_cursor_shape_device_v1_shape_default = u32(1)
338 const wp_cursor_shape_device_v1_shape_text = u32(5)
339 const wp_cursor_shape_device_v1_shape_crosshair = u32(3)
340 const wp_cursor_shape_device_v1_shape_pointer = u32(29)
341 const wp_cursor_shape_device_v1_shape_ew_resize = u32(17)
342 const wp_cursor_shape_device_v1_shape_ns_resize = u32(19)
343 const wp_cursor_shape_device_v1_shape_nwse_resize = u32(14)
344 const wp_cursor_shape_device_v1_shape_nesw_resize = u32(12)
345 const wp_cursor_shape_device_v1_shape_all_scroll = u32(21)
346 const wp_cursor_shape_device_v1_shape_not_allowed = u32(25)
347
348 // MAP_FAILED
349 const map_failed = voidptr(usize(~u64(0)))
350
351 // Linux input event codes (from linux/input-event-codes.h)
352 const btn_left = 0x110
353 const btn_right = 0x111
354 const btn_middle = 0x112
355
356 // timerfd
357 #include <sys/timerfd.h>
358
359 pub struct C.itimerspec {
360 it_interval C.timespec
361 it_value C.timespec
362 }
363
364 pub struct C.timespec {
365 tv_sec i64
366 tv_nsec i64
367 }
368
369 fn C.timerfd_create(clockid int, flags int) int
370 fn C.timerfd_settime(fd int, flags int, new_value &C.itimerspec, old_value &C.itimerspec) int
371
372 const clock_monotonic = 1
373 const tfd_cloexec = 0o2000000
374 const tfd_nonblock = 0o4000
375}
376
377// === EGL (shared between Wayland and X11) ===
378
379pub type EGLDisplay = voidptr
380pub type EGLContext = voidptr
381pub type EGLSurface = voidptr
382pub type EGLConfig = voidptr
383pub type EGLNativeWindowType = voidptr
384pub type EGLint = i32
385
386fn C.eglGetDisplay(native_display voidptr) EGLDisplay
387fn C.eglInitialize(display EGLDisplay, major &EGLint, minor &EGLint) int
388fn C.eglTerminate(display EGLDisplay) int
389fn C.eglChooseConfig(display EGLDisplay, attribs &EGLint, configs &EGLConfig, config_size EGLint, num_configs &EGLint) int
390fn C.eglCreateContext(display EGLDisplay, config EGLConfig, share_ctx EGLContext, attribs &EGLint) EGLContext
391fn C.eglDestroyContext(display EGLDisplay, ctx EGLContext) int
392fn C.eglCreateWindowSurface(display EGLDisplay, config EGLConfig, window EGLNativeWindowType, attribs &EGLint) EGLSurface
393fn C.eglDestroySurface(display EGLDisplay, surface EGLSurface) int
394fn C.eglMakeCurrent(display EGLDisplay, draw EGLSurface, read EGLSurface, ctx EGLContext) int
395fn C.eglSwapBuffers(display EGLDisplay, surface EGLSurface) int
396fn C.eglSwapInterval(display EGLDisplay, interval EGLint) int
397fn C.eglGetError() EGLint
398fn C.eglBindAPI(api u32) int
399fn C.eglGetConfigAttrib(display EGLDisplay, config EGLConfig, attribute EGLint, value &EGLint) int
400
401// OpenGL
402fn C.glGetIntegerv(pname u32, data &i32)
403
404// EGL constants
405const egl_opengl_api = u32(0x30A2)
406const egl_opengl_es_api = u32(0x30A0)
407const egl_no_display = EGLDisplay(0)
408const egl_no_context = EGLContext(0)
409const egl_no_surface = EGLSurface(0)
410const egl_none = EGLint(0x3038)
411const egl_red_size = EGLint(0x3024)
412const egl_green_size = EGLint(0x3023)
413const egl_blue_size = EGLint(0x3022)
414const egl_alpha_size = EGLint(0x3021)
415const egl_depth_size = EGLint(0x3025)
416const egl_stencil_size = EGLint(0x3026)
417const egl_sample_buffers = EGLint(0x3032)
418const egl_samples = EGLint(0x3031)
419const egl_surface_type = EGLint(0x3033)
420const egl_window_bit = EGLint(0x0004)
421const egl_renderable_type = EGLint(0x3040)
422const egl_opengl_bit = EGLint(0x0008)
423const egl_opengl_es3_bit = EGLint(0x0040)
424const egl_context_major_version = EGLint(0x3098)
425const egl_context_minor_version = EGLint(0x30FB)
426const egl_context_opengl_profile_mask = EGLint(0x30FD)
427const egl_context_opengl_core_profile_bit = EGLint(0x00000001)
428const egl_true = 1
429const egl_false = 0
430
431// GL constants
432const gl_framebuffer_binding = u32(0x8CA6)
433
434// === Linux syscalls (shared) ===
435
436fn C.mmap(addr voidptr, length usize, prot int, flags int, fd int, offset i64) voidptr
437fn C.munmap(addr voidptr, length usize) int
438fn C.close(fd int) int
439fn C.read(fd int, buf voidptr, count usize) isize
440fn C.pipe(fds &int) int
441fn C.strcmp(s1 &char, s2 &char) int
442fn C.strncmp(s1 &char, s2 &char, n usize) int
443fn C.strlen(s &char) usize
444fn C.strtok(str &char, delim &char) &char
445fn C.strtol(str &char, endptr &&char, base int) i64
446fn C.memset(s voidptr, c int, n usize) voidptr
447fn C.atof(s &char) f64
448
449// mmap constants
450const prot_read = 1
451const map_private = 2
452
453// === X11 / Xlib types and functions ===
454
455#include <X11/Xlib.h>
456#include <X11/Xutil.h>
457#include <X11/Xresource.h>
458#include <X11/Xatom.h>
459#include <X11/keysym.h>
460#include <X11/XKBlib.h>
461#include <X11/extensions/XInput2.h>
462#include <X11/Xcursor/Xcursor.h>
463
464// X11 types
465pub type XID = u64
466pub type Atom = u64
467pub type Window = u64
468pub type Colormap = u64
469pub type Cursor = u64
470pub type KeySym = u64
471pub type Time = u64
472pub type VisualID = u64
473
474// X11 types are defined by X11 headers (#preinclude above)
475// Forward declarations for V type checking only
476@[typedef]
477pub struct C.Display {
478pub mut:
479 _ int // opaque - actual definition from X11 headers
480}
481
482@[typedef]
483pub struct C.Visual {
484pub mut:
485 _ int // opaque - actual definition from X11 headers
486}
487
488@[typedef]
489pub struct C.Screen {
490pub mut:
491 _ int // opaque - actual definition from X11 headers
492}
493
494@[typedef]
495pub struct C.XSelectionEvent {
496pub mut:
497 type int
498 display &C.Display = unsafe { nil }
499 requestor Window
500 selection Atom
501 target Atom
502 property Atom
503 time int
504}
505
506@[typedef]
507pub struct C.XSelectionClearEvent {
508pub mut:
509 window Window
510 selection Atom
511}
512
513@[typedef]
514pub struct C.XSelectionRequestEvent {
515pub mut:
516 display &C.Display = unsafe { nil }
517 owner Window
518 requestor Window
519 selection Atom
520 target Atom
521 property Atom
522 time int
523}
524
525@[typedef]
526pub struct C.XDestroyWindowEvent {
527pub mut:
528 window Window
529}
530
531@[typedef]
532pub struct C.XPropertyEvent {
533pub mut:
534 state int
535 atom Atom
536}
537
538@[typedef]
539pub struct C.XClientMessageEvent {
540pub mut:
541 window Window
542 format int
543 message_type Atom
544 data C.XClientMessageData
545}
546
547@[typedef]
548pub union C.XClientMessageData {
549pub mut:
550 b [20]u8
551 s [10]i16
552 l [5]i64
553}
554
555@[typedef]
556pub struct C.XGenericEventCookie {
557pub mut:
558 extension int
559 evtype int
560 data voidptr
561}
562
563@[typedef]
564pub struct C.XKeyEvent {
565pub mut:
566 keycode u32
567 state u32
568}
569
570@[typedef]
571pub struct C.XButtonEvent {
572pub mut:
573 button u32
574 state u32
575 x int
576 y int
577}
578
579@[typedef]
580pub struct C.XMotionEvent {
581pub mut:
582 x int
583 y int
584 state u32
585}
586
587@[typedef]
588pub struct C.XCrossingEvent {
589pub mut:
590 x int
591 y int
592 state u32
593}
594
595@[typedef]
596pub struct C.XFocusChangeEvent {
597pub mut:
598 mode int
599}
600
601@[typedef]
602pub union C.XEvent {
603pub mut:
604 type int
605 xclient C.XClientMessageEvent
606 xkey C.XKeyEvent
607 xbutton C.XButtonEvent
608 xmotion C.XMotionEvent
609 xcrossing C.XCrossingEvent
610 xfocus C.XFocusChangeEvent
611 xproperty C.XPropertyEvent
612 xselection C.XSelectionEvent
613 xselectionrequest C.XSelectionRequestEvent
614 xselectionclear C.XSelectionClearEvent
615 xdestroywindow C.XDestroyWindowEvent
616 xcookie C.XGenericEventCookie
617}
618
619pub struct C.XrmDatabase__rec {}
620
621pub type XrmDatabase = &C.XrmDatabase__rec
622
623pub struct C.XSetWindowAttributes {
624mut:
625 colormap Colormap
626 border_pixel u64
627 event_mask u64
628}
629
630@[typedef]
631pub struct C.XWindowAttributes {
632mut:
633 width int
634 height int
635 map_state int
636}
637
638pub struct C.XSizeHintsAspect {
639mut:
640 x int
641 y int
642}
643
644@[typedef]
645pub struct C.XSizeHints {
646mut:
647 flags i64
648 x int
649 y int
650 width int
651 height int
652 min_width int
653 min_height int
654 max_width int
655 max_height int
656 width_inc int
657 height_inc int
658 min_aspect C.XSizeHintsAspect
659 max_aspect C.XSizeHintsAspect
660 base_width int
661 base_height int
662 win_gravity int
663}
664
665pub struct C.XVisualInfo {
666mut:
667 visual &C.Visual = unsafe { nil }
668 visualid VisualID
669 depth int
670}
671
672// X11 event types are defined in vlib/x/x11/x11.v
673// XEvent union is defined here since it's used extensively in sokol
674
675pub union C.XEvent {
676pub mut:
677 @type int
678 xclient C.XClientMessageEvent
679 xkey C.XKeyEvent
680 xbutton C.XButtonEvent
681 xmotion C.XMotionEvent
682 xcrossing C.XCrossingEvent
683 xfocus C.XFocusChangeEvent
684 xproperty C.XPropertyEvent
685 xselection C.XSelectionEvent
686 xselectionrequest C.XSelectionRequestEvent
687 xselectionclear C.XSelectionClearEvent
688 xdestroywindow C.XDestroyWindowEvent
689 xcookie C.XGenericEventCookie
690}
691
692pub struct C.XKeyEvent {
693pub mut:
694 keycode u32
695 state u32
696}
697
698pub struct C.XButtonEvent {
699pub mut:
700 button u32
701 state u32
702 x int
703 y int
704}
705
706pub struct C.XMotionEvent {
707pub mut:
708 x int
709 y int
710 state u32
711}
712
713pub struct C.XCrossingEvent {
714pub mut:
715 x int
716 y int
717 state u32
718}
719
720pub struct C.XFocusChangeEvent {
721pub mut:
722 mode int
723}
724
725pub struct C.XPropertyEvent {
726pub mut:
727 state int
728 atom Atom
729}
730
731// XSelection* structs are forward-declared above
732// Full definitions come from X11 headers or clipboard module
733
734pub struct C.XClientMessageEvent {
735pub mut:
736 window Window
737 format int
738 message_type Atom
739 data C.XClientMessageData
740}
741
742pub union C.XClientMessageData {
743pub mut:
744 l [5]i64
745}
746
747pub struct C.XGenericEventCookie {
748pub mut:
749 extension int
750 evtype int
751 data voidptr
752}
753
754pub struct C.XrmValue {
755 addr &char = unsafe { nil }
756}
757
758// XKB types
759pub struct C.XkbDescRec {
760mut:
761 min_key_code u8
762 max_key_code u8
763 names &C.XkbNamesRec = unsafe { nil }
764}
765
766pub type XkbDescPtr = &C.XkbDescRec
767
768pub struct C.XkbNamesRec {
769mut:
770 keys &C.XkbKeyNameRec = unsafe { nil }
771 key_aliases &C.XkbKeyAliasRec = unsafe { nil }
772 num_key_aliases u8
773}
774
775pub struct C.XkbKeyNameRec {
776mut:
777 name [4]u8
778}
779
780pub struct C.XkbKeyAliasRec {
781mut:
782 real [4]u8
783 alias [4]u8
784}
785
786// XInput2 types
787pub struct C.XIEventMask {
788mut:
789 deviceid int
790 mask_len int
791 mask &u8 = unsafe { nil }
792}
793
794pub struct C.XIRawEvent {
795mut:
796 valuators C.XIValuatorState
797 raw_values &f64 = unsafe { nil }
798}
799
800pub struct C.XIValuatorState {
801mut:
802 mask_len int
803 mask &u8 = unsafe { nil }
804}
805
806// Xcursor types
807pub struct C.XcursorImage {
808mut:
809 width u32
810 height u32
811 xhot u32
812 yhot u32
813 pixels &u32 = unsafe { nil }
814}
815
816pub type XcursorPixel = u32
817
818// === Xlib functions ===
819
820fn C.XOpenDisplay(name &char) &C.Display
821fn C.XCloseDisplay(display &C.Display) int
822fn C.XDefaultScreen(display &C.Display) int
823fn C.XDefaultRootWindow(display &C.Display) Window
824fn C.XDefaultVisual(display &C.Display, screen int) &C.Visual
825fn C.XDefaultDepth(display &C.Display, screen int) int
826fn C.XDisplayWidth(display &C.Display, screen int) int
827fn C.XDisplayHeight(display &C.Display, screen int) int
828fn C.XDisplayWidthMM(display &C.Display, screen int) int
829fn C.XInternAtom(display &C.Display, name &char, only_if_exists int) Atom
830fn C.XCreateColormap(display &C.Display, window Window, visual &C.Visual, alloc int) Colormap
831fn C.XFreeColormap(display &C.Display, colormap Colormap) int
832fn C.XCreateWindow(display &C.Display, parent Window, x int, y int, width u32, height u32, border_width u32, depth int, class_ u32, visual &C.Visual, valuemask u64, attributes &C.XSetWindowAttributes) Window
833fn C.XDestroyWindow(display &C.Display, window Window) int
834fn C.XMapWindow(display &C.Display, window Window) int
835fn C.XUnmapWindow(display &C.Display, window Window) int
836fn C.XRaiseWindow(display &C.Display, window Window) int
837fn C.XFlush(display &C.Display) int
838fn C.XSync(display &C.Display, discard int) int
839fn C.XPending(display &C.Display) int
840fn C.XNextEvent(display &C.Display, event &C.XEvent) int
841fn C.XCheckTypedWindowEvent(display &C.Display, window Window, event_type int, event &C.XEvent) int
842fn C.XSendEvent(display &C.Display, window Window, propagate int, event_mask i64, event &C.XEvent) int
843fn C.XFilterEvent(event &C.XEvent, window Window) int
844fn C.XSetWMProtocols(display &C.Display, window Window, protocols &Atom, count int) int
845fn C.XSetWMNormalHints(display &C.Display, window Window, hints &C.XSizeHints) int
846fn C.XAllocSizeHints() &C.XSizeHints
847fn C.XGetWindowAttributes(display &C.Display, window Window, attrs &C.XWindowAttributes) int
848fn C.XGetWindowProperty(display &C.Display, window Window, property Atom, long_offset i64, long_length i64, delete int, req_type Atom, actual_type &Atom, actual_format &int, nitems &u64, bytes_after &u64, prop &&&u8) int
849fn C.XChangeProperty(display &C.Display, window Window, property Atom, @type Atom, format int, mode int, data &u8, nelements int) int
850fn C.Xutf8SetWMProperties(display &C.Display, window Window, window_name &char, icon_name &char, argv &&char, argc int, normal_hints voidptr, wm_hints voidptr, class_hints voidptr)
851fn C.XSetSelectionOwner(display &C.Display, selection Atom, owner Window, time Time)
852fn C.XGetSelectionOwner(display &C.Display, selection Atom) Window
853fn C.XConvertSelection(display &C.Display, selection Atom, target Atom, property Atom, requestor Window, time Time)
854fn C.XFree(data voidptr) int
855fn C.XDefineCursor(display &C.Display, window Window, cursor Cursor) int
856fn C.XUndefineCursor(display &C.Display, window Window) int
857fn C.XCreateFontCursor(display &C.Display, shape u32) Cursor
858fn C.XFreeCursor(display &C.Display, cursor Cursor) int
859fn C.XGrabPointer(display &C.Display, grab_window Window, owner_events int, event_mask u32, pointer_mode int, keyboard_mode int, confine_to Window, cursor Cursor, time Time) int
860fn C.XUngrabPointer(display &C.Display, time Time) int
861fn C.XWarpPointer(display &C.Display, src_w Window, dst_w Window, src_x int, src_y int, src_width u32, src_height u32, dst_x int, dst_y int) int
862fn C.XSetErrorHandler(handler voidptr) voidptr
863fn C.XGetKeyboardMapping(display &C.Display, first_keycode u8, keycode_count int, keysyms_per_keycode &int) &KeySym
864fn C.XLookupString(event &C.XKeyEvent, buf &char, buf_len int, keysym &KeySym, status voidptr) int
865fn C.XQueryExtension(display &C.Display, name &char, major_opcode &int, first_event &int, first_error &int) int
866fn C.XResourceManagerString(display &C.Display) &char
867fn C.XGetVisualInfo(display &C.Display, vinfo_mask i64, vinfo_template &C.XVisualInfo, nitems &int) &C.XVisualInfo
868fn C.XInitThreads() int
869fn C.XrmInitialize()
870fn C.XrmGetStringDatabase(data &char) XrmDatabase
871fn C.XrmDestroyDatabase(db XrmDatabase)
872fn C.XrmGetResource(db XrmDatabase, str_name &char, str_class &char, str_type &&char, value &C.XrmValue) int
873fn C.XGetEventData(display &C.Display, cookie &C.XGenericEventCookie) int
874fn C.XFreeEventData(display &C.Display, cookie &C.XGenericEventCookie)
875
876// XKB functions
877fn C.XkbGetMap(display &C.Display, which u32, device_spec u32) XkbDescPtr
878fn C.XkbGetNames(display &C.Display, which u32, desc XkbDescPtr) int
879fn C.XkbFreeNames(desc XkbDescPtr, which u32, free_map int) int
880fn C.XkbFreeKeyboard(desc XkbDescPtr, which u32, free_all int) int
881fn C.XkbSetDetectableAutoRepeat(display &C.Display, detectable int, supported &int) int
882
883// XInput2 functions
884fn C.XIQueryVersion(display &C.Display, major &int, minor &int) int
885fn C.XISelectEvents(display &C.Display, window Window, masks &C.XIEventMask, num_masks int) int
886
887// Xcursor functions
888fn C.XcursorImageCreate(width int, height int) &C.XcursorImage
889fn C.XcursorImageDestroy(image &C.XcursorImage)
890fn C.XcursorImageLoadCursor(display &C.Display, image &C.XcursorImage) Cursor
891fn C.XcursorLibraryLoadImage(name &char, theme &char, size int) &C.XcursorImage
892fn C.XcursorGetTheme(display &C.Display) &char
893fn C.XcursorGetDefaultSize(display &C.Display) int
894
895// poll
896#include <poll.h>
897
898pub struct C.pollfd {
899mut:
900 fd int
901 events i16
902 revents i16
903}
904
905fn C.poll(fds &C.pollfd, nfds u64, timeout int) int
906
907fn C.ConnectionNumber(display &C.Display) int
908
909fn C.atof(str &char) f64
910fn C.strncmp(s1 &char, s2 &char, n usize) int
911
912// X11 constants
913const x_false = 0
914const x_true = 1
915const x_none = Atom(0)
916const xa_atom = Atom(4)
917const xa_cardinal = Atom(6)
918const alloc_none = 0
919const cw_border_pixel = u64(1 << 11)
920const cw_colormap = u64(1 << 13)
921const cw_event_mask = u64(1 << 11) // this is actually CWEventMask
922const input_output = u32(1)
923const structure_notify_mask = i64(1 << 17)
924const key_press_mask = i64(1 << 0)
925const key_release_mask = i64(1 << 1)
926const pointer_motion_mask = i64(1 << 6)
927const button_press_mask = i64(1 << 2)
928const button_release_mask = i64(1 << 3)
929const exposure_mask = i64(1 << 15)
930const focus_change_mask = i64(1 << 21)
931const visibility_change_mask = i64(1 << 16)
932const enter_window_mask = i64(1 << 4)
933const leave_window_mask = i64(1 << 5)
934const property_change_mask = i64(1 << 22)
935const substructure_notify_mask = i64(1 << 19)
936const substructure_redirect_mask = i64(1 << 20)
937const no_event_mask = i64(0)
938
939const prop_mode_replace = 0
940const pw_gravity = i64(1 << 9) // PWinGravity flag
941const center_gravity = 5
942
943const is_viewable = 2
944const visibility_notify = 15
945const normal_state = 1
946const iconic_state = 3
947const withdrawn_state = 0
948
949const current_time = Time(0)
950const grab_mode_async = 1
951
952// Event types
953const x_generic_event = 35
954const x_focus_in = 9
955const x_focus_out = 10
956const x_key_press = 2
957const x_key_release = 3
958const x_button_press = 4
959const x_button_release = 5
960const x_enter_notify = 7
961const x_leave_notify = 8
962const x_motion_notify = 6
963const x_property_notify = 28
964const x_selection_notify = 31
965const x_selection_request = 30
966const x_destroy_notify = 17
967const x_client_message = 33
968const property_new_value = 0
969
970const notify_grab = 1
971const notify_ungrab = 2
972
973// Mouse buttons
974const x_button1 = u32(1)
975const x_button2 = u32(2)
976const x_button3 = u32(3)
977
978// Modifier masks
979const shift_mask = u32(1 << 0)
980const control_mask = u32(1 << 2)
981const mod1_mask = u32(1 << 3) // Alt
982const mod4_mask = u32(1 << 6) // Super
983const button1_mask = u32(1 << 8)
984const button2_mask = u32(1 << 9)
985const button3_mask = u32(1 << 10)
986
987// Cursor shapes for XCreateFontCursor
988const xc_left_ptr = u32(68)
989const xc_xterm = u32(152)
990const xc_crosshair = u32(34)
991const xc_hand2 = u32(60)
992const xc_sb_h_double_arrow = u32(108)
993const xc_sb_v_double_arrow = u32(116)
994const xc_fleur = u32(52)
995
996// XKB constants
997const xkb_use_core_kbd = u32(0x0100)
998const xkb_key_names_mask = u32(1 << 9)
999const xkb_key_aliases_mask = u32(1 << 10)
1000const xkb_key_name_length = 4
1001
1002// XInput2 constants
1003const xi_all_master_devices = -1
1004const xi_raw_motion = 17
1005const pollin = i16(0x001)
1006
1007// XDnD
1008const x11_xdnd_version = 5
1009
1010// KeySym constants
1011const xk_escape = KeySym(0xff1b)
1012const xk_tab = KeySym(0xff09)
1013const xk_shift_l = KeySym(0xffe1)
1014const xk_shift_r = KeySym(0xffe2)
1015const xk_control_l = KeySym(0xffe3)
1016const xk_control_r = KeySym(0xffe4)
1017const xk_meta_l = KeySym(0xffe7)
1018const xk_alt_l = KeySym(0xffe9)
1019const xk_mode_switch = KeySym(0xff7e)
1020const xk_iso_level3_shift = KeySym(0xfe03)
1021const xk_meta_r = KeySym(0xffe8)
1022const xk_alt_r = KeySym(0xffea)
1023const xk_super_l = KeySym(0xffeb)
1024const xk_super_r = KeySym(0xffec)
1025const xk_menu = KeySym(0xff67)
1026const xk_num_lock = KeySym(0xff7f)
1027const xk_caps_lock = KeySym(0xffe5)
1028const xk_print = KeySym(0xff61)
1029const xk_scroll_lock = KeySym(0xff14)
1030const xk_pause = KeySym(0xff13)
1031const xk_delete = KeySym(0xffff)
1032const xk_backspace = KeySym(0xff08)
1033const xk_return = KeySym(0xff0d)
1034const xk_home = KeySym(0xff50)
1035const xk_end = KeySym(0xff57)
1036const xk_page_up = KeySym(0xff55)
1037const xk_page_down = KeySym(0xff56)
1038const xk_insert = KeySym(0xff63)
1039const xk_left = KeySym(0xff51)
1040const xk_right = KeySym(0xff53)
1041const xk_down = KeySym(0xff54)
1042const xk_up = KeySym(0xff52)
1043const xk_f1 = KeySym(0xffbe)
1044const xk_f2 = KeySym(0xffbf)
1045const xk_f3 = KeySym(0xffc0)
1046const xk_f4 = KeySym(0xffc1)
1047const xk_f5 = KeySym(0xffc2)
1048const xk_f6 = KeySym(0xffc3)
1049const xk_f7 = KeySym(0xffc4)
1050const xk_f8 = KeySym(0xffc5)
1051const xk_f9 = KeySym(0xffc6)
1052const xk_f10 = KeySym(0xffc7)
1053const xk_f11 = KeySym(0xffc8)
1054const xk_f12 = KeySym(0xffc9)
1055const xk_f13 = KeySym(0xffca)
1056const xk_f14 = KeySym(0xffcb)
1057const xk_f15 = KeySym(0xffcc)
1058const xk_f16 = KeySym(0xffcd)
1059const xk_f17 = KeySym(0xffce)
1060const xk_f18 = KeySym(0xffcf)
1061const xk_f19 = KeySym(0xffd0)
1062const xk_f20 = KeySym(0xffd1)
1063const xk_f21 = KeySym(0xffd2)
1064const xk_f22 = KeySym(0xffd3)
1065const xk_f23 = KeySym(0xffd4)
1066const xk_f24 = KeySym(0xffd5)
1067const xk_f25 = KeySym(0xffd6)
1068const xk_kp_divide = KeySym(0xffaf)
1069const xk_kp_multiply = KeySym(0xffaa)
1070const xk_kp_subtract = KeySym(0xffad)
1071const xk_kp_add = KeySym(0xffab)
1072const xk_kp_0 = KeySym(0xffb0)
1073const xk_kp_1 = KeySym(0xffb1)
1074const xk_kp_2 = KeySym(0xffb2)
1075const xk_kp_3 = KeySym(0xffb3)
1076const xk_kp_4 = KeySym(0xffb4)
1077const xk_kp_5 = KeySym(0xffb5)
1078const xk_kp_6 = KeySym(0xffb6)
1079const xk_kp_7 = KeySym(0xffb7)
1080const xk_kp_8 = KeySym(0xffb8)
1081const xk_kp_9 = KeySym(0xffb9)
1082const xk_kp_separator = KeySym(0xffac)
1083const xk_kp_decimal = KeySym(0xffae)
1084const xk_kp_equal = KeySym(0xffbd)
1085const xk_kp_enter = KeySym(0xff8d)
1086const xk_kp_insert = KeySym(0xff9e)
1087const xk_kp_end = KeySym(0xff9c)
1088const xk_kp_down = KeySym(0xff99)
1089const xk_kp_page_down = KeySym(0xff9b)
1090const xk_kp_left = KeySym(0xff96)
1091const xk_kp_right = KeySym(0xff98)
1092const xk_kp_home = KeySym(0xff95)
1093const xk_kp_up = KeySym(0xff97)
1094const xk_kp_page_up = KeySym(0xff9a)
1095const xk_kp_delete = KeySym(0xff9f)
1096const xk_a = KeySym(0x0061)
1097const xk_b = KeySym(0x0062)
1098const xk_c = KeySym(0x0063)
1099const xk_d = KeySym(0x0064)
1100const xk_e = KeySym(0x0065)
1101const xk_f = KeySym(0x0066)
1102const xk_g = KeySym(0x0067)
1103const xk_h = KeySym(0x0068)
1104const xk_i = KeySym(0x0069)
1105const xk_j = KeySym(0x006a)
1106const xk_k = KeySym(0x006b)
1107const xk_l = KeySym(0x006c)
1108const xk_m = KeySym(0x006d)
1109const xk_n = KeySym(0x006e)
1110const xk_o = KeySym(0x006f)
1111const xk_p = KeySym(0x0070)
1112const xk_q = KeySym(0x0071)
1113const xk_r = KeySym(0x0072)
1114const xk_s = KeySym(0x0073)
1115const xk_t = KeySym(0x0074)
1116const xk_u = KeySym(0x0075)
1117const xk_v = KeySym(0x0076)
1118const xk_w = KeySym(0x0077)
1119const xk_x = KeySym(0x0078)
1120const xk_y = KeySym(0x0079)
1121const xk_z = KeySym(0x007a)
1122const xk_0 = KeySym(0x0030)
1123const xk_1 = KeySym(0x0031)
1124const xk_2 = KeySym(0x0032)
1125const xk_3 = KeySym(0x0033)
1126const xk_4 = KeySym(0x0034)
1127const xk_5 = KeySym(0x0035)
1128const xk_6 = KeySym(0x0036)
1129const xk_7 = KeySym(0x0037)
1130const xk_8 = KeySym(0x0038)
1131const xk_9 = KeySym(0x0039)
1132const xk_space = KeySym(0x0020)
1133const xk_minus = KeySym(0x002d)
1134const xk_equal = KeySym(0x003d)
1135const xk_bracketleft = KeySym(0x005b)
1136const xk_bracketright = KeySym(0x005d)
1137const xk_backslash = KeySym(0x005c)
1138const xk_semicolon = KeySym(0x003b)
1139const xk_apostrophe = KeySym(0x0027)
1140const xk_grave = KeySym(0x0060)
1141const xk_comma = KeySym(0x002c)
1142const xk_period = KeySym(0x002e)
1143const xk_slash = KeySym(0x002f)
1144const xk_less = KeySym(0x003c)
1145
1146// XIMaskLen macro equivalent
1147fn xi_mask_len(event int) int {
1148 return (event >> 3) + 1
1149}
1150
1151// XISetMask macro equivalent
1152fn xi_set_mask(mask &u8, event int) {
1153 unsafe {
1154 mut p := mask + (event >> 3)
1155 *p = *p | u8(1 << (event & 7))
1156 }
1157}
1158
1159// XIMaskIsSet macro equivalent
1160fn xi_mask_is_set(mask &u8, event int) bool {
1161 unsafe {
1162 return (mask[event >> 3] & u8(1 << (event & 7))) != 0
1163 }
1164}
1165
1166// VisualIDMask for XGetVisualInfo
1167const visual_id_mask = i64(0x1)
1168