//! The runtime around a sketch: window, memory, audio, timeline and console. //! One `frame` serves both the interactive loop and test scripts, so scripts //! exercise exactly what a player does. const Host = @This(); const std = @import("std"); const c = @import("c"); const abi = @import("abi"); const memory = @import("memory.zig"); const Timeline = @import("timeline.zig").Timeline; const mapRecordings = @import("timeline.zig").mapRecordings; const gfx = @import("gfx.zig"); const sound = @import("audio.zig"); const Gpu = @import("gpu.zig"); const overlay = @import("overlay.zig"); const Console = @import("Console.zig"); const Library = @import("library.zig").Library; const Watch = @import("library.zig").Watch; pub const Options = struct { /// A hot-reloaded sketch library. Unused when a sketch is linked in. library: ?[]const u8 = null, /// State file; recordings go next to it with an `.inputs` suffix. state: []const u8 = "sketch.state", /// Offscreen video, silent audio, no vsync: for scripts and CI. headless: bool = false, /// Forget the state file's contents: scripts start from a known place. fresh: bool = false, }; gpa: std.mem.Allocator, io: std.Io, environ: *const std.process.Environ.Map, options: Options, window: *c.SDL_Window, renderer: *c.SDL_Renderer, /// Runs sketches' GPU passes, and the 2D renderer draws through it too. /// Null when the machine has no usable GPU driver. gpu: ?*c.SDL_GPUDevice, passes: ?Gpu = null, /// Headless with a GPU: the renderer has no window and draws into this /// canvas-sized texture, which screenshots read. offscreen: ?*c.SDL_Texture = null, pointer_cursor: ?*c.SDL_Cursor = null, images: gfx.Images, audio: sound.Audio, state: memory.StateFile, session: memory.Session, recordings: []align(memory.page) u8, timeline: Timeline, console: Console = .{}, sketch: abi.Sketch, library: ?Library = null, watch: ?Watch = null, reloads: usize = 0, /// Copies of the sketch's info strings, replaced on every load. info_arena: std.heap.ArenaAllocator, name: []const u8 = "", completions: []const []const u8 = &.{}, /// Frame lifetime: sketch scratch and this frame's commands. frame_arena: std.heap.ArenaAllocator, commands: std.ArrayList(abi.Command) = .empty, /// Events routed since the last frame, fed to `update` by the next one. pending: std.ArrayList(abi.Event) = .empty, input: abi.Input = .{}, files: std.StringHashMapUnmanaged([]align(memory.page) const u8) = .empty, canvas: [2]u32 = .{ 0, 0 }, cursor: abi.Cursor = .arrow, quit: bool = false, reloaded: bool = true, started: bool = true, last_now: u64 = 0, /// Saved after the next frame is drawn (scripts). screenshot: ?[:0]const u8 = null, pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std.process.Environ.Map, options: Options, linked: ?abi.Sketch) !void { if (options.headless) { // Overriding: a plain hint loses to the environment, and a user's // SDL_VIDEO_DRIVER=wayland would open real windows during tests. _ = c.SDL_SetHintWithPriority(c.SDL_HINT_VIDEO_DRIVER, "offscreen", c.SDL_HINT_OVERRIDE); _ = c.SDL_SetHintWithPriority(c.SDL_HINT_AUDIO_DRIVER, "dummy", c.SDL_HINT_OVERRIDE); _ = c.SDL_SetHintWithPriority(c.SDL_HINT_RENDER_DRIVER, "software", c.SDL_HINT_OVERRIDE); } if (!c.SDL_Init(c.SDL_INIT_VIDEO | c.SDL_INIT_EVENTS | c.SDL_INIT_AUDIO)) return sdlError("SDL_Init"); errdefer c.SDL_Quit(); var library: ?Library = null; const sketch = linked orelse blk: { const path = options.library orelse return error.NoSketch; library = try Library.openCopy(io, path, scratchDir(environ), 0); break :blk library.?.sketch; }; errdefer if (library) |*l| l.close(); const info = sketch.info(); var title_buf: [256]u8 = undefined; const title = std.fmt.bufPrintZ(&title_buf, "{s}", .{info.name}) catch "sketch"; const window = c.SDL_CreateWindow(title.ptr, @intCast(info.width), @intCast(info.height), c.SDL_WINDOW_RESIZABLE) orelse return sdlError("SDL_CreateWindow"); errdefer c.SDL_DestroyWindow(window); // The 2D renderer runs on the GPU device that also runs sketches' GPU // passes; without one, SDL picks whatever renderer works. Headless, it // gets no window (an offscreen window has no Vulkan surface everywhere) // and draws into a texture instead (`setCanvas`). var gpu = c.SDL_CreateGPUDevice(c.SDL_GPU_SHADERFORMAT_SPIRV, false, null); var renderer: ?*c.SDL_Renderer = if (gpu) |device| c.SDL_CreateGPURenderer(device, if (options.headless) null else window) else null; if (renderer == null) { if (gpu) |device| c.SDL_DestroyGPUDevice(device); gpu = null; std.log.warn("no GPU renderer ({s}); drawing without one", .{c.SDL_GetError()}); renderer = c.SDL_CreateRenderer(window, null) orelse return sdlError("SDL_CreateRenderer"); } errdefer c.SDL_DestroyRenderer(renderer); errdefer if (gpu) |device| c.SDL_DestroyGPUDevice(device); if (!options.headless) _ = c.SDL_SetRenderVSync(renderer, 1); var state = try memory.StateFile.open(gpa, options.state); errdefer state.close(); var inputs_buf: [std.fs.max_path_bytes]u8 = undefined; const recordings = try mapRecordings(try std.fmt.bufPrint(&inputs_buf, "{s}.inputs", .{options.state})); errdefer std.posix.munmap(recordings); self.* = .{ .gpa = gpa, .io = io, .environ = environ, .options = options, .window = window, .renderer = renderer.?, .gpu = gpu, .images = .{ .gpa = gpa, .renderer = renderer.? }, .audio = .{ .gpa = gpa }, .state = state, .session = .init(gpa), .recordings = recordings, .timeline = undefined, .sketch = sketch, .library = library, .info_arena = .init(gpa), .frame_arena = .init(gpa), }; self.timeline = .{ .bank = .{ .state = &self.state, .inputs = recordings } }; if (gpu) |device| self.passes = .init(gpa, device); self.audio.open() catch |err| std.log.warn("audio unavailable: {t}", .{err}); if (options.library) |path| if (linked == null) { self.watch = Watch.init(path) catch null; }; self.setCanvas(info.width, info.height); _ = c.SDL_StartTextInput(self.window); try self.adopt(sketch); if (options.fresh) { self.state.header().used = 0; @memset(self.recordings, 0); self.resetState(); } } pub fn deinit(self: *Host) void { if (self.watch) |w| w.deinit(); self.audio.deinit(); if (self.passes) |*g| g.deinit(&self.images); self.images.deinit(); if (self.pointer_cursor) |cursor| c.SDL_DestroyCursor(cursor); if (self.offscreen) |texture| c.SDL_DestroyTexture(texture); c.SDL_DestroyRenderer(self.renderer); if (self.gpu) |device| c.SDL_DestroyGPUDevice(device); c.SDL_DestroyWindow(self.window); var files = self.files.iterator(); while (files.next()) |entry| { self.gpa.free(entry.key_ptr.*); std.posix.munmap(entry.value_ptr.*); } self.files.deinit(self.gpa); std.posix.munmap(self.recordings); self.session.deinit(self.gpa); self.state.close(); self.pending.deinit(self.gpa); self.frame_arena.deinit(); self.info_arena.deinit(); if (self.library) |*l| l.close(); c.SDL_Quit(); } /// The interactive loop. pub fn run(self: *Host) !void { while (!self.quit) { try self.poll(); try self.frame(c.SDL_GetTicksNS()); } self.timeline.stop(c.SDL_GetTicksNS()); } pub fn live(self: *Host) []u8 { return self.state.slot(0); } /// One frame: reload if a build replaced the library, feed input through /// `update`, then `draw` and present. pub fn frame(self: *Host, now: u64) !void { _ = self.frame_arena.reset(.retain_capacity); self.commands = .empty; if (self.watch) |w| if (w.changed()) self.reload() catch |err| self.print("reload failed: {t}", .{err}); const dt = if (self.last_now == 0) 0 else now -| self.last_now; self.last_now = now; const h = &self.timeline; switch (h.mode) { .playing => self.update(h.tick(now)), .paused => {}, .live, .recording => { try self.pending.append(self.gpa, .{ .kind = .tick, .time = now, .x = @floatCast(@as(f64, @floatFromInt(dt)) / std.time.ns_per_s) }); for (self.pending.items) |e| h.capture(e) catch |err| self.print("Recording stopped: {t}", .{err}); self.input.now = now; self.input.dt = dt; self.update(self.pending.items); }, } self.pending.clearRetainingCapacity(); try self.draw(if (h.mode == .live or h.mode == .recording) now else h.position); } /// Feeds events to the sketch's deterministic `update`. pub fn update(self: *Host, events: []const abi.Event) void { // Input follows the events actually fed, live or replayed. for (events) |e| self.input.track(e); if (events.len > 0 and self.timeline.mode != .live and self.timeline.mode != .recording) self.input.now = events[events.len - 1].time; self.input.events = events; self.input.mode = self.timeline.mode; self.input.started = self.started; self.started = false; const ctx = self.context(); self.sketch.update(&ctx, self.live().ptr); self.input.events = &.{}; } fn draw(self: *Host, now: u64) !void { const arena = self.frame_arena.allocator(); self.input.now = now; self.input.mode = self.timeline.mode; self.input.reloaded = self.reloaded; self.reloaded = false; self.input.console = self.console.visible; self.input.width = @floatFromInt(self.canvas[0]); self.input.height = @floatFromInt(self.canvas[1]); self.input.streams = self.audio.streamStatus(); self.input.sample_rate = sound.sample_rate; self.input.frame += 1; const ctx = self.context(); self.sketch.draw(&ctx, self.live().ptr, self.session.bytes.ptr); _ = c.SDL_SetRenderClipRect(self.renderer, null); _ = c.SDL_SetRenderDrawColor(self.renderer, 0, 0, 0, 255); _ = c.SDL_RenderClear(self.renderer); self.audio.beginFrame(); for (self.commands.items) |command| switch (command) { .canvas => |size| self.setCanvas(size.w, size.h), .cursor => |kind| self.cursor = kind, .voice => |voice| self.audio.declare(voice), .audio => |a| self.audio.push(a.key, a.samples, a.clear), .quit => self.quit = true, .gpu => |pass| if (self.passes) |*g| g.run(&self.images, pass) catch |err| self.print("gpu pass: {t} ({s})", .{ err, c.SDL_GetError() }), else => gfx.draw(self.renderer, &self.images, command), }; self.audio.endFrame(); _ = c.SDL_SetRenderClipRect(self.renderer, null); var extra: std.ArrayList(abi.Command) = .empty; if (self.console.overlay_memory) try overlay.drawMemory(&extra, arena, self.state.layout, self.live(), self.input.width); if (self.console.overlay_sound) { const samples = self.audio.scope.snapshot(); try overlay.drawScope(&extra, arena, &samples, self.audio.scope.rate, self.canvas); } if (self.console.visible) try self.console.draw(self, &extra, arena); for (extra.items) |command| gfx.draw(self.renderer, &self.images, command); self.applyCursor(); if (self.screenshot) |path| { self.screenshot = null; const surface = c.SDL_RenderReadPixels(self.renderer, null) orelse return sdlError("SDL_RenderReadPixels"); defer c.SDL_DestroySurface(surface); if (!c.SDL_SavePNG(surface, path.ptr)) return sdlError("SDL_SavePNG"); } _ = c.SDL_RenderPresent(self.renderer); if (self.passes) |*g| g.endFrame(); } fn context(self: *Host) abi.Context { return .{ .host = .{ .ctx = self, .vtable = &api }, .input = &self.input, .arena = self.frame_arena.allocator(), .gpa = self.session.heap.allocator(), .commands = &self.commands, }; } /// Sends one event to the console or towards the sketch. Backtick toggles the /// console; while it is open it owns the keyboard, and pointer events over it /// become a cancel, so a half-made click never lands behind it. pub fn route(self: *Host, e: abi.Event) !void { if (e.kind == .key_down and e.code == @intFromEnum(abi.Key.grave)) { if (e.flags & abi.Event.repeat != 0) return; self.console.visible = !self.console.visible; return self.pending.append(self.gpa, .{ .kind = .cancel, .time = e.time }); } if (self.console.visible) switch (e.kind) { .text => { var buf: [4]u8 = undefined; const len = std.unicode.utf8Encode(@intCast(@min(e.value, 0x10ffff)), &buf) catch return; self.console.insert(buf[0..len]); return; }, .key_down => { const key = consoleKey(e) orelse return; if (self.console.edit(key, self.completions)) |line| self.runLine(line, e.time) catch {}; return; }, .key_up => return, .mouse_move, .mouse_down, .mouse_up, .wheel => if (e.y >= @as(f32, @floatFromInt(self.canvas[1])) - Console.panel_height) { return self.pending.append(self.gpa, .{ .kind = .cancel, .time = e.time }); }, else => {}, }; try self.pending.append(self.gpa, e); } /// Runs a console line now, replaying whatever a seek asks for. An error is /// printed to the console, and returned for scripts to fail on. pub fn runLine(self: *Host, line: []const u8, now: u64) !void { const replay = self.console.execute(self, line, now) catch |err| { // The sketch has printed its own error already. if (err != error.CommandFailed) self.console.print("{t}. Type help for usage.", .{err}); return err; }; if (self.timeline.mode == .paused) self.update(replay); } /// Offers a line to the sketch. pub fn forward(self: *Host, _: *Console, line: []const u8, now: u64) abi.Outcome { self.input.now = now; const ctx = self.context(); return self.sketch.command(&ctx, self.live().ptr, self.session.bytes.ptr, &line); } /// Fresh state: declared defaults, then the sketch's `init`. pub fn resetState(self: *Host) void { const bytes = self.live(); @memset(bytes, 0); self.sketch.defaults(.state, bytes.ptr); const ctx = self.context(); self.sketch.init(&ctx, bytes.ptr); self.state.markUsed(0, true); } /// Loads the watched library again. pub fn reload(self: *Host) !void { try self.load(self.options.library orelse return error.NoLibrary); } /// Replaces the running sketch with the library at `path`, keeping state. pub fn load(self: *Host, path: []const u8) !void { var library = try Library.openCopy(self.io, path, scratchDir(self.environ), self.reloads + 1); errdefer library.close(); try self.adopt(library.sketch); if (self.library) |*old| old.close(); self.library = library; self.reloads += 1; self.print("reload {d}: {s}", .{ self.reloads, path }); } fn adopt(self: *Host, sketch: abi.Sketch) !void { const info = sketch.info(); if (info.abi != abi.hash) return error.AbiMismatch; const adoption = try self.state.adopt(self.gpa, info.state, sketch.defaults); const session_reset = try self.session.adopt(self.gpa, info.session, sketch.defaults); _ = self.info_arena.reset(.retain_capacity); const arena = self.info_arena.allocator(); self.name = try arena.dupe(u8, info.name); const completions = try arena.alloc([]const u8, info.commands.len); for (completions, info.commands) |*to, from| to.* = try arena.dupe(u8, from); self.completions = completions; self.sketch = sketch; self.reloaded = true; switch (adoption) { .unchanged => {}, .created => {}, .migrated => |m| self.print("state layout changed: kept {d} fields, reset {d} to defaults", .{ m.kept, m.reset }), } if (!self.state.used(0)) self.resetState(); if (session_reset and self.reloads > 0) self.print("session layout changed: session reset", .{}); } fn setCanvas(self: *Host, w: u32, h: u32) void { if (w == 0 or h == 0 or (self.canvas[0] == w and self.canvas[1] == h)) return; self.canvas = .{ w, h }; if (self.options.headless and self.gpu != null) { if (self.offscreen) |texture| c.SDL_DestroyTexture(texture); self.offscreen = c.SDL_CreateTexture(self.renderer, c.SDL_PIXELFORMAT_RGBA32, c.SDL_TEXTUREACCESS_TARGET, @intCast(w), @intCast(h)); _ = c.SDL_SetRenderTarget(self.renderer, self.offscreen); } else { _ = c.SDL_SetRenderLogicalPresentation(self.renderer, @intCast(w), @intCast(h), c.SDL_LOGICAL_PRESENTATION_LETTERBOX); } _ = c.SDL_SetWindowSize(self.window, @intCast(w), @intCast(h)); } fn applyCursor(self: *Host) void { const kind: abi.Cursor = if (self.console.visible) .arrow else self.cursor; switch (kind) { .hidden => _ = c.SDL_HideCursor(), .arrow => { _ = c.SDL_SetCursor(c.SDL_GetDefaultCursor()); _ = c.SDL_ShowCursor(); }, .pointer => { if (self.pointer_cursor == null) self.pointer_cursor = c.SDL_CreateSystemCursor(c.SDL_SYSTEM_CURSOR_POINTER); _ = c.SDL_SetCursor(self.pointer_cursor orelse c.SDL_GetDefaultCursor()); _ = c.SDL_ShowCursor(); }, } } pub fn print(self: *Host, comptime fmt: []const u8, args: anytype) void { var buf: [512]u8 = undefined; const message = std.fmt.bufPrint(&buf, fmt, args) catch "(message too long)"; self.console.log(message); // Headless runs are tests: they report through their own results. if (!self.options.headless) std.debug.print("{s}\n", .{message}); } /// Translates SDL events into sketch events and routes them. pub fn poll(self: *Host) !void { var event: c.SDL_Event = undefined; while (c.SDL_PollEvent(&event)) { _ = c.SDL_ConvertEventToRenderCoordinates(self.renderer, &event); const time = event.common.timestamp; switch (event.type) { c.SDL_EVENT_QUIT => self.quit = true, c.SDL_EVENT_WINDOW_FOCUS_LOST => try self.route(.{ .kind = .cancel, .time = time }), c.SDL_EVENT_MOUSE_MOTION => try self.route(.{ .kind = .mouse_move, .time = time, .x = event.motion.x, .y = event.motion.y }), c.SDL_EVENT_MOUSE_BUTTON_DOWN, c.SDL_EVENT_MOUSE_BUTTON_UP => try self.route(.{ .kind = if (event.type == c.SDL_EVENT_MOUSE_BUTTON_DOWN) .mouse_down else .mouse_up, .time = time, .x = event.button.x, .y = event.button.y, .code = event.button.button, }), c.SDL_EVENT_MOUSE_WHEEL => { const delta = event.wheel.y * @as(f32, if (event.wheel.direction == c.SDL_MOUSEWHEEL_FLIPPED) -1 else 1); if (!std.math.isFinite(delta) or delta == 0) continue; try self.route(.{ .kind = .wheel, .time = time, .x = event.wheel.mouse_x, .y = event.wheel.mouse_y, .value = @bitCast(delta) }); }, c.SDL_EVENT_KEY_DOWN, c.SDL_EVENT_KEY_UP => try self.route(.{ .kind = if (event.type == c.SDL_EVENT_KEY_DOWN) .key_down else .key_up, .time = time, .code = @intCast(event.key.scancode), .flags = if (event.key.repeat) abi.Event.repeat else 0, .value = modifiers(event.key.mod), }), c.SDL_EVENT_TEXT_INPUT => { var it = (std.unicode.Utf8View.init(std.mem.span(event.text.text)) catch continue).iterator(); while (it.nextCodepoint()) |cp| try self.route(.{ .kind = .text, .time = time, .value = cp }); }, else => {}, } } } fn modifiers(mod: c.SDL_Keymod) u32 { var m: u32 = 0; if (mod & c.SDL_KMOD_SHIFT != 0) m |= abi.Key.Mod.shift; if (mod & c.SDL_KMOD_CTRL != 0) m |= abi.Key.Mod.ctrl; if (mod & c.SDL_KMOD_ALT != 0) m |= abi.Key.Mod.alt; if (mod & c.SDL_KMOD_GUI != 0) m |= abi.Key.Mod.gui; return m; } fn consoleKey(e: abi.Event) ?Console.Key { const key: abi.Key = @enumFromInt(@as(u8, @truncate(e.code))); if (key == .u and e.value & abi.Key.Mod.ctrl != 0) return .clear; if (e.flags & abi.Event.repeat != 0 and key.isEnter()) return null; if (key.isEnter()) return .submit; return switch (key) { .left => .left, .right => .right, .home => .home, .end => .end, .backspace => .backspace, .delete => .delete, .escape => .clear, .tab => .tab, else => null, }; } fn scratchDir(environ: *const std.process.Environ.Map) []const u8 { return environ.get("XDG_RUNTIME_DIR") orelse "/tmp"; } fn sdlError(what: []const u8) error{Sdl} { std.log.err("{s}: {s}", .{ what, c.SDL_GetError() }); return error.Sdl; } // Host services, as seen by sketches. const api: abi.Host.VTable = .{ .log = apiLog, .loadImage = apiLoadImage, .decodeImage = apiDecodeImage, .newImage = apiNewImage, .freeImage = apiFreeImage, .gpu = apiGpu, .imageSize = apiImageSize, .loadSound = apiLoadSound, .decodeSound = apiDecodeSound, .readFile = apiReadFile, .env = apiEnv, .clock = apiClock, }; fn hostOf(ctx: *anyopaque) *Host { return @ptrCast(@alignCast(ctx)); } fn apiLog(ctx: *anyopaque, message: []const u8) void { hostOf(ctx).print("{s}", .{message}); } fn apiLoadImage(ctx: *anyopaque, path: []const u8) abi.Image { return hostOf(ctx).images.load(path); } fn apiDecodeImage(ctx: *anyopaque, png: []const u8) abi.Image { return hostOf(ctx).images.decode(png); } fn apiNewImage(ctx: *anyopaque, w: u32, h: u32) abi.Image { return hostOf(ctx).images.new(w, h); } fn apiFreeImage(ctx: *anyopaque, image: abi.Image) void { const self = hostOf(ctx); if (self.passes) |*g| g.forget(&self.images, image); self.images.free(image); } fn apiGpu(ctx: *anyopaque) bool { return hostOf(ctx).gpu != null; } fn apiImageSize(ctx: *anyopaque, image: abi.Image) [2]u32 { return hostOf(ctx).images.size(image); } fn apiLoadSound(ctx: *anyopaque, path: []const u8) abi.Audio { return hostOf(ctx).audio.load(path); } fn apiDecodeSound(ctx: *anyopaque, wav: []const u8) abi.Audio { return hostOf(ctx).audio.decode(wav); } fn apiReadFile(ctx: *anyopaque, path: []const u8) []const u8 { const self = hostOf(ctx); if (self.files.get(path)) |bytes| return bytes; const bytes = memory.mapReadOnly(path); if (bytes.len == 0) return &.{}; const key = self.gpa.dupe(u8, path) catch return bytes; self.files.put(self.gpa, key, bytes) catch self.gpa.free(key); return bytes; } fn apiClock(_: *anyopaque) u64 { return c.SDL_GetTicksNS(); } fn apiEnv(ctx: *anyopaque, name: []const u8) ?[]const u8 { return hostOf(ctx).environ.get(name); }