summaryrefslogtreecommitdiff
path: root/src/gui/gui.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-18 02:38:25 -0300
committerGabriel Schneider <[email protected]>2026-11-18 03:43:34 -0300
commit392d509c8435f975ae25b64ea5d61eeecabbea90 (patch)
tree6bc00527fcade802edb071e3835cac982f83d527 /src/gui/gui.zig
parentcf5f2d75ba24c02240044027b6de7b151e0ab9df (diff)
downloadpardes-392d509c8435f975ae25b64ea5d61eeecabbea90.tar.gz
pardes-392d509c8435f975ae25b64ea5d61eeecabbea90.zip
gui: the gamepad subsystem is started once the first frame is drawn, as the loop first waits (startGamepads, the session's and a window client's loops), not in SDL_Init before the window: its scan (udev, hidapi, libusb) no longer delays the first frame; a pad plugged in already is added by it then (GAMEPAD_ADDED), so its first event comes about 40 ms after the first frame, and a key in those 40 ms waits for it; gui offscreen, cut at the first frame: 507M -> 370M instructions:u, 262 -> 221 ms best (load 0.7); a real window 518M -> 380M, 249 -> 231 ms
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src/gui/gui.zig')
-rw-r--r--src/gui/gui.zig16
1 files changed, 15 insertions, 1 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index c83b8209..4eaf7860 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -549,6 +549,8 @@ const Gui = struct {
window_slot: ?*?*c.SDL_Window = null,
gamepad: ?*c.SDL_Gamepad = null,
+ /// The gamepad subsystem started (startGamepads).
+ gamepads: bool = false,
pad_x: f32 = 0,
pad_y: f32 = 0,
pad_scroll: f32 = 0, // fractional vertical wheel ticks (left stick)
@@ -805,7 +807,8 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, detached: bool, di
_ = c.SDL_SetHint("SDL_JOYSTICK_HIDAPI", "1"); // SDL_HINT_JOYSTICK_HIDAPI
_ = c.SDL_SetHint("SDL_JOYSTICK_HIDAPI_STEAMDECK", "1"); // ..._STEAMDECK
if (comptime builtin.os.tag == .macos) _ = c.SDL_SetHint(c.SDL_HINT_MAC_SCROLL_MOMENTUM, "1");
- if (!c.SDL_Init(c.SDL_INIT_VIDEO | c.SDL_INIT_GAMEPAD)) {
+ // The gamepads after the first frame (startGamepads).
+ if (!c.SDL_Init(c.SDL_INIT_VIDEO)) {
log.err("SDL_Init: {s}", .{c.SDL_GetError()});
return error.SdlInit;
}
@@ -1460,6 +1463,7 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_
if (link.quietCheck()) |wait| ms = if (ms < 0) @intCast(wait) else @min(ms, @as(c_int, @intCast(wait)));
// Idle, its frame drawn: the next input needs no copy taken.
if (ms != 0) r.ready();
+ if (shell.surface != null) startGamepads(g);
var sev = std.mem.zeroes(c.SDL_Event);
g.input_seen = false;
var got = c.SDL_WaitEventTimeout(&sev, ms);
@@ -2780,6 +2784,7 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void {
s.took_input = false;
defer s.waited = true;
if (s.gui) |g| {
+ if (s.surface != null) startGamepads(g);
var sev = std.mem.zeroes(c.SDL_Event);
// Idle is asleep until an event: SDL's own, and the user events the
// shell's queue raises for a terminal's output, a completion, a file
@@ -3226,6 +3231,15 @@ fn present(ctx: ?*anyopaque, surface: *const pardes.Surface) void {
if (!s.presented and s.skipped_presents <= 3) core.present_skipped = true;
}
+/// The gamepad subsystem, started once the first frame is drawn, as the
+/// loop first waits: its scan (udev, hidapi, libusb) took 40 ms of a start.
+/// A pad plugged in already is added by it (GAMEPAD_ADDED).
+fn startGamepads(g: *Gui) void {
+ if (g.gamepads) return;
+ g.gamepads = true;
+ if (!c.SDL_InitSubSystem(c.SDL_INIT_GAMEPAD)) log.warn("SDL_InitSubSystem(GAMEPAD): {s}", .{c.SDL_GetError()});
+}
+
fn postPresent(ctx: ?*anyopaque) void {
const s = shellOf(ctx);
const g = s.gui orelse return;