Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: add the GamepadEvent interface

Francis Bouvier18df15959b95
diff --git a/src/browser/js/bridge.zig b/src/browser/js/bridge.zig
index 7bbd62a48..1e8f0d464 100644
--- a/src/browser/js/bridge.zig
+++ b/src/browser/js/bridge.zig
@@ -1094,6 +1094,7 @@ pub const PageJsApis = flattenTypes(&.{
@import("../webapi/event/BeforeUnloadEvent.zig"),
@import("../webapi/event/StorageEvent.zig"),
@import("../webapi/event/DeviceMotionEvent.zig"),
+ @import("../webapi/event/GamepadEvent.zig"),
@import("../webapi/event/DeviceOrientationEvent.zig"),
@import("../webapi/event/TouchEvent.zig"),
@import("../webapi/event/UIEvent.zig"),
diff --git a/src/browser/webapi/Event.zig b/src/browser/webapi/Event.zig
index c344a761f..0c0bad24e 100644
--- a/src/browser/webapi/Event.zig
+++ b/src/browser/webapi/Event.zig
@@ -87,6 +87,7 @@ pub const Type = union(enum) {
before_unload_event: *@import("event/BeforeUnloadEvent.zig"),
storage_event: *@import("event/StorageEvent.zig"),
device_motion_event: *@import("event/DeviceMotionEvent.zig"),
+ gamepad_event: *@import("event/GamepadEvent.zig"),
device_orientation_event: *@import("event/DeviceOrientationEvent.zig"),
ui_event: *@import("event/UIEvent.zig"),
promise_rejection_event: *@import("event/PromiseRejectionEvent.zig"),
@@ -200,6 +201,7 @@ pub fn is(self: *Event, comptime T: type) ?*T {
.before_unload_event => |e| return if (T == @import("event/BeforeUnloadEvent.zig")) e else null,
.storage_event => |e| return if (T == @import("event/StorageEvent.zig")) e else null,
.device_motion_event => |e| return if (T == @import("event/DeviceMotionEvent.zig")) e else null,
+ .gamepad_event => |e| return if (T == @import("event/GamepadEvent.zig")) e else null,
.device_orientation_event => |e| return if (T == @import("event/DeviceOrientationEvent.zig")) e else null,
.promise_rejection_event => |e| return if (T == @import("event/PromiseRejectionEvent.zig")) e else null,
.submit_event => |e| return if (T == @import("event/SubmitEvent.zig")) e else null,
diff --git a/src/browser/webapi/event/GamepadEvent.zig b/src/browser/webapi/event/GamepadEvent.zig
new file mode 100644
index 000000000..f0423fec8
--- /dev/null
+++ b/src/browser/webapi/event/GamepadEvent.zig
@@ -0,0 +1,77 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+const std = @import("std");
+const lp = @import("lightpanda");
+
+const js = @import("../../js/js.zig");
+const Frame = @import("../../Frame.zig");
+
+const Event = @import("../Event.zig");
+
+const String = lp.String;
+const Allocator = std.mem.Allocator;
+
+// https://w3c.github.io/gamepad/#gamepadevent-interface
+const GamepadEvent = @This();
+
+_proto: *Event,
+
+const GamepadEventOptions = struct {};
+
+const Options = Event.inheritOptions(GamepadEvent, GamepadEventOptions);
+
+pub fn init(typ: []const u8, _opts: ?Options, frame: *Frame) !*GamepadEvent {
+ const arena = try frame.getArena(.tiny, "GamepadEvent");
+ errdefer frame.releaseArena(arena);
+ const type_string = try String.init(arena, typ, .{});
+
+ const opts = _opts orelse Options{};
+ const event = try frame._factory.event(
+ arena,
+ type_string,
+ GamepadEvent{
+ ._proto = undefined,
+ },
+ );
+
+ Event.populatePrototypes(event, opts, false);
+ return event;
+}
+
+pub fn asEvent(self: *GamepadEvent) *Event {
+ return self._proto;
+}
+
+// There are no gamepads in a headless browser.
+pub fn getGamepad(_: *const GamepadEvent) ?bool {
+ return null;
+}
+
+pub const JsApi = struct {
+ pub const bridge = js.Bridge(GamepadEvent);
+
+ pub const Meta = struct {
+ pub const name = "GamepadEvent";
+ pub const prototype_chain = bridge.prototypeChain();
+ pub var class_id: bridge.ClassId = undefined;
+ };
+
+ pub const constructor = bridge.constructor(GamepadEvent.init, .{});
+ pub const gamepad = bridge.accessor(GamepadEvent.getGamepad, null, .{ .null_as_undefined = false });
+};