-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathapi.zig
375 lines (332 loc) · 12.2 KB
/
api.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//! Generated by @thi.ng/wasm-api-bindgen at 2024-08-17T15:49:39.199Z
//! DO NOT EDIT!
const std = @import("std");
const bindgen = @import("wasm-api-bindgen");
pub const CreateElementOptsSlice = bindgen.Slice([]CreateElementOpts, [*]CreateElementOpts);
pub const ConstCreateElementOptsSlice = bindgen.Slice([]const CreateElementOpts, [*]const CreateElementOpts);
pub const AttribSlice = bindgen.Slice([]Attrib, [*]Attrib);
pub const ConstAttribSlice = bindgen.Slice([]const Attrib, [*]const Attrib);
/// Syntax sugar for: `ConstCreateElementOptsSlice.wrap()`
pub inline fn children(items: []const CreateElementOpts) ConstCreateElementOptsSlice {
return ConstCreateElementOptsSlice.wrap(items);
}
/// Syntax sugar for: `ConstAttribSlice.wrap()`
pub inline fn attribs(items: []const Attrib) ConstAttribSlice {
return ConstAttribSlice.wrap(items);
}
pub const EventType = enum(i32) {
unkown = -1,
drag,
focus,
input,
key,
mouse,
pointer,
scroll,
touch,
wheel,
};
pub const MouseButton = enum(u8) {
none,
primary = 1,
secondary = 2,
middle = 4,
};
pub const PointerType = enum(u8) {
mouse,
pen,
touch,
};
pub const WheelDeltaMode = enum(u8) {
pixel,
line,
page,
};
pub const KeyModifier = enum(u8) {
shift = 1,
ctrl = 2,
alt = 4,
meta = 8,
};
/// Various browser window related information [TODO]
pub const WindowInfo = extern struct {
innerWidth: u16,
innerHeight: u16,
/// Horizontal scroll offset in fractional CSS pixels
scrollX: f32,
/// Vertical scroll offset in fractional CSS pixels
scrollY: f32,
/// Current device pixel ratio
dpr: u8,
/// Encoded bitmask indicating fullscreen status / capability:
/// - 1 (bit 0): fullscreen active
/// - 2 (bit 1): fullscreen supported
fullscreen: u8,
/// Returns true if fullscreen mode is currently active (see `.fullscreen`)
pub inline fn isFullscreen(self: *const WindowInfo) bool {
return self.fullscreen & 1 != 0;
}
/// Returns true if fullscreen mode is supported (see `.fullscreen`)
pub inline fn hasFullscreen(self: *const WindowInfo) bool {
return self.fullscreen & 2 != 0;
}
};
pub const DragEvent = extern struct {
/// Mouse X position in the local space of the element's bounding rect
clientX: i16 = 0,
/// Mouse Y position in the local space of the element's bounding rect
clientY: i16 = 0,
/// If non-zero, data that is being dragged during a drag & drop operation can
/// be obtained via various DnD related API calls (only available when called
/// from event handler).
isDataTransfer: u8 = 0,
/// Encoded bitmask of currently pressed modifier keys, see `KeyModifier` enum
modifiers: u8 = 0,
/// Encoded bitmask of all currently pressed mouse buttons, see `MouseButton`
/// enum
buttons: u8 = 0,
/// Event related mouse button ID (if any)
button: MouseButton,
};
pub const InputEvent = extern struct {
/// Value of the targeted input element.
/// The memory is owned by the DOM API and will be freed immediately after the
/// event handler has returned.
value: bindgen.ConstStringPtr,
/// Length of the value string
len: u32,
/// Encoded bitmask of currently pressed modifier keys, see `KeyModifier` enum
modifiers: u8 = 0,
pub inline fn getValue(self: *const InputEvent) [:0]const u8 {
return self.value[0..self.len :0];
}
};
pub const KeyEvent = extern struct {
/// Value/name of the key pressed
key: [15:0]u8,
/// Number of characters of the `key` string
len: u8 = 0,
/// Encoded bitmask of currently pressed modifier keys, see `KeyModifier` enum
modifiers: u8 = 0,
/// Non-zero value indicates key is being held down such that it's automatically
/// repeating
repeat: u8 = 0,
pub inline fn getKey(self: *const KeyEvent) [:0]const u8 {
return self.key[0..@as(usize, self.len) :0];
}
pub inline fn hasModifier(self: *const KeyEvent, mod: KeyModifier) bool {
return self.modifiers & @intFromEnum(mod) != 0;
}
};
pub const MouseEvent = extern struct {
/// Mouse X position in the local space of the element's bounding rect
clientX: i16 = 0,
/// Mouse Y position in the local space of the element's bounding rect
clientY: i16 = 0,
/// Encoded bitmask of currently pressed modifier keys, see `KeyModifier` enum
modifiers: u8 = 0,
/// Encoded bitmask of all currently pressed mouse buttons, see `MouseButton`
/// enum
buttons: u8 = 0,
/// Event related mouse button ID (if any)
button: MouseButton,
};
pub const PointerEvent = extern struct {
/// Mouse X position in the local space of the element's bounding rect
clientX: i16 = 0,
/// Mouse Y position in the local space of the element's bounding rect
clientY: i16 = 0,
/// Unique pointer ID
id: u32 = 0,
/// Normalized pressure value 0..1
pressure: f32 = 0,
/// The plane angle (in degrees, in the range of -90 to 90) between the Y-Z
/// plane and the plane containing both the pointer (e.g. pen stylus) axis and
/// the Y axis.
tiltX: i8 = 0,
/// The plane angle (in degrees, in the range of -90 to 90) between the X-Z
/// plane and the plane containing both the pointer (e.g. pen stylus) axis and
/// the X axis.
tiltY: i8 = 0,
/// The clockwise rotation of the pointer (e.g. pen stylus) around its major
/// axis in degrees, with a value in the range 0 to 359.
twist: u16 = 0,
pointerType: PointerType,
/// Non-zero if event's pointer is the primary pointer (in a multitouch
/// scenario)
isPrimary: u8,
/// Encoded bitmask of currently pressed modifier keys, see `KeyModifier` enum
modifiers: u8 = 0,
/// Encoded bitmask of all currently pressed mouse buttons, see `MouseButton`
/// enum
buttons: u8 = 0,
/// Event related mouse button ID (if any)
button: MouseButton,
};
pub const ScrollEvent = extern struct {
/// Horizontal scroll offset in fractional CSS pixels.
scrollX: f32,
/// Vertical scroll offset in fractional CSS pixels.
scrollY: f32,
};
pub const TouchEvent = extern struct {
/// Touch X position in the local space of the element's bounding rect
clientX: i16 = 0,
/// Touch Y position in the local space of the element's bounding rect
clientY: i16 = 0,
/// Encoded bitmask of currently pressed modifier keys, see `KeyModifier` enum
modifiers: u8 = 0,
};
pub const WheelEvent = extern struct {
/// Scroll X delta
deltaX: i16 = 0,
/// Scroll Y delta
deltaY: i16 = 0,
/// Scroll Z delta
deltaZ: i16 = 0,
/// Delta mode
mode: WheelDeltaMode,
/// Encoded bitmask of currently pressed modifier keys, see `KeyModifier` enum
modifiers: u8 = 0,
/// Encoded bitmask of currently pressed mouse buttons, see `MouseButton` enum
buttons: u8 = 0,
};
pub const EventBody = extern union {
drag: DragEvent,
input: InputEvent,
key: KeyEvent,
mouse: MouseEvent,
pointer: PointerEvent,
scroll: ScrollEvent,
touch: TouchEvent,
wheel: WheelEvent,
};
pub const Event = extern struct {
id: EventType,
/// Target element ID, > 1 if a known (WASM created) element, otherwise:
///
/// - -2: = unknown
/// - -1: window
/// - 0: document.head
/// - 1: document.body
target: i32,
/// Event details / payload. Currently, only the following event types have a
/// defined body:
///
/// - drag
/// - input
/// - key
/// - mouse
/// - pointer
/// - scroll
/// - touch
/// - wheel
body: EventBody,
};
pub const EventCallback = *const fn (event: *const Event, ctx: ?bindgen.OpaquePtr) callconv(.C) void;
/// Function signature for RAF (requestAnimationFrame) event handler
pub const RAFCallback = *const fn (time: f64, ctx: ?bindgen.OpaquePtr) callconv(.C) void;
/// DOM event listener
pub const EventListener = extern struct {
/// Event listener function. Takes an event and optional pointer to user
/// supplied arbitrary context data provided when registering the handler via
/// `addListener()`
callback: EventCallback,
/// Optional type erased pointer to arbitrary user context. This pointer can be
/// cast back into the desired type using this form:
/// `@ptrCast(?*Foo, @alignCast(@alignOf(Foo), raw))`
/// Also see: `wasmapi.ptrCast()`
ctx: ?bindgen.OpaquePtr = null,
};
/// RAF event listener
pub const RAFListener = extern struct {
/// Animation frame listener function. Takes an hires timestamp and optional
/// pointer to user supplied arbitrary context data provided when registering
/// the handler via `requestAnimationFrame()`
callback: RAFCallback,
/// Optional type erased pointer to arbitrary user context. This pointer can be
/// cast back into the desired type using this form:
/// `@ptrCast(?*Foo, @alignCast(@alignOf(Foo), raw))`
/// Also see: `wasmapi.ptrCast()`
ctx: ?bindgen.OpaquePtr = null,
};
/// Data structure used for declarative creation of DOM elements / trees (passed
/// to `createElement()`)
/// Also see `CreateCanvasOpts` for canvas specific use cases
pub const CreateElementOpts = extern struct {
/// DOM element name
tag: bindgen.ConstStringPtr,
/// Namespace URI or wellknown registered alias (e.g. svg, xlink, xmlns)
ns: bindgen.ConstStringPtr = "",
/// ID attrib
id: bindgen.ConstStringPtr = "",
/// Element class attrib
class: bindgen.ConstStringPtr = "",
/// Element inner text body
text: bindgen.ConstStringPtr = "",
/// Element inner HTML body
html: bindgen.ConstStringPtr = "",
/// Parent element ID. If >=0 the new element will be attached to that parent
/// element. Set to -1 to leave new element unattached (default unless nested)
parent: i32 = -1,
/// Insertion index for new element or -1 to append (default)
index: i32 = -1,
/// Optional slice of child element specs, which will be automatically attached
/// as children to this element (their `parent` ID will be ignored)
children: ConstCreateElementOptsSlice = children(&[_]CreateElementOpts{}),
/// Optional slice of attribute definitions for this element. Also see provided
/// `Attrib` factory functions for convenience.
attribs: ConstAttribSlice = attribs(&[_]Attrib{}),
};
/// Data structure used for declarative creation of canvas elements (passed to
/// `createCanvas()`)
pub const CreateCanvasOpts = extern struct {
/// Canvas width (in CSS pixels)
width: u16,
/// Canvas height (in CSS pixels)
height: u16,
/// Element ID attrib
id: bindgen.ConstStringPtr = "",
/// Element class attrib
class: bindgen.ConstStringPtr = "",
/// Same as CreateElementOpts.parent
parent: i32,
/// Same as CreateElementOpts.index
index: i32 = -1,
/// Device pixel ratio for computing physical pixel dimensions, see
/// `getWindowInfo()`
dpr: u8 = 1,
/// Optional slice of attribute definitions for this element. Also see provided
/// `Attrib` factory functions for convenience.
attribs: ConstAttribSlice = attribs(&[_]Attrib{}),
};
/// DOM element attribute definition given as part of `CreateElementOpts`
pub const Attrib = extern struct {
name: bindgen.ConstStringPtr,
value: AttribValue,
kind: AttribType,
pub fn event(name: [*:0]const u8, callback: EventCallback, ctx: ?*anyopaque) Attrib {
return Attrib{ .name = name, .kind = .event, .value = .{ .event = .{ .callback = callback, .ctx = ctx } } };
}
pub fn flag(name: [*:0]const u8, val: bool) Attrib {
return Attrib{ .name = name, .kind = .flag, .value = .{ .flag = if (val) 1 else 0 } };
}
pub fn number(name: [*:0]const u8, val: f64) Attrib {
return Attrib{ .name = name, .kind = .num, .value = .{ .num = val } };
}
pub fn string(name: [*:0]const u8, val: [*:0]const u8) Attrib {
return Attrib{ .name = name, .kind = .str, .value = .{ .str = val } };
}
};
pub const AttribValue = extern union {
event: EventListener,
flag: u8,
num: f64,
str: bindgen.ConstStringPtr,
};
pub const AttribType = enum(u8) {
event,
flag,
num,
str,
};