summaryrefslogtreecommitdiff
path: root/src/host/root.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/host/root.zig')
-rw-r--r--src/host/root.zig56
1 files changed, 18 insertions, 38 deletions
diff --git a/src/host/root.zig b/src/host/root.zig
index 629c466..9c7ad77 100644
--- a/src/host/root.zig
+++ b/src/host/root.zig
@@ -2,11 +2,11 @@
//! that it hot-reloads whenever a build replaces it.
//!
//! sketchbook [options] [LIBRARY]
-//! --state FILE persistent state (default: $XDG_DATA_HOME/<name>/<name>.state;
-//! sketch.state for a script or an export)
+//! --state FILE the save (default: $XDG_DATA_HOME/<name>/<name>.xitdb;
+//! sketch.xitdb for a script or an export)
//! --headless offscreen video, silent audio
//! --script FILE play a script instead of reading devices
-//! --fresh start from default state (sessions, marks and clips are kept)
+//! --fresh start from default state (the save's history is kept)
//! --timing-marker encode frame ID and draw sample time for video analysis
//! --profile-gpu time each GPU command alone, print the times at exit
//! --console LINE run a console line once the sketch is loaded (repeatable)
@@ -93,31 +93,9 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void {
} else if (try std.Io.File.stdout().isTty(init.io)) fatal("--export -: stdout is a terminal; pipe it to ffmpeg");
}
- // A scripted run from fresh state works on files of its own, moved to
- // the given path at exit: two runs of one build step (two `zig build`s
- // at once share its output path) never share a state file, nor the
- // files the sketch appends to (its sessions are in memory). An export's
- // are removed instead: nothing reads them, and a player may have the
- // given ones open.
- var private_buf: [std.fs.max_path_bytes]u8 = undefined;
- if (options.state.len == 0 and (script_path != null or options.export_to != null)) options.state = "sketch.state";
- const given = options.state;
- const private = (script_path != null or options.export_to != null) and options.fresh;
- if (!private) sketchbookDir(init.io, given);
- if (private) options.state = std.mem.print(&private_buf, "{s}.{d}", .{ given, std.os.linux.getpid() }) catch fatal("--state path too long");
- // Its files are `<private>` and `<private>.<name>`, beside the given one.
- defer if (private) moved: {
- const dir = std.Io.Dir.cwd().openDir(init.io, std.fs.path.dirname(given) orelse ".", .{ .iterate = true }) catch break :moved;
- defer dir.close(init.io);
- const from = std.fs.path.basename(options.state);
- var it = dir.iterate();
- while (it.next(init.io) catch null) |entry| {
- if (!std.mem.startsWith(u8, entry.name, from) or (entry.name.len > from.len and entry.name[from.len] != '.')) continue;
- var to: [std.fs.max_path_bytes]u8 = undefined;
- const name = std.mem.print(&to, "{s}{s}", .{ std.fs.path.basename(given), entry.name[from.len..] }) catch continue;
- if (options.export_to != null) dir.deleteFile(init.io, entry.name) catch {} else dir.rename(entry.name, dir, name, init.io) catch {};
- }
- };
+ if (options.state.len == 0 and (script_path != null or options.export_to != null)) options.state = "sketch.xitdb";
+ // A scripted run from fresh state, or an export, touches no save.
+ if (!((script_path != null or options.export_to != null) and options.fresh)) sketchbookDir(init.io, options.state);
sdl_heap.install();
{
@@ -137,11 +115,12 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void {
const usage =
\\usage: sketchbook [options] [LIBRARY]
\\ --9p SOCKET local 9P controls: ctl, status, log, screen.png
- \\ --state FILE persistent state; sessions are kept beside it (default:
- \\ $XDG_DATA_HOME/<name>/<name>.state, sketch.state for --script, --export)
+ \\ --state FILE the save, one file: FILE, or FILE.xitdb for a FILE.state (where a
+ \\ state file from before is imported from, once); default:
+ \\ $XDG_DATA_HOME/<name>/<name>.xitdb, sketch.xitdb for --script, --export
\\ --headless offscreen video, silent audio, no vsync
\\ --script FILE play a script instead of reading devices
- \\ --fresh start from default state (sessions, marks and clips are kept)
+ \\ --fresh start from default state (the save's history is kept)
\\ --timing-marker encode frame ID and draw sample time for video analysis
\\ --profile-gpu time each GPU command alone, print the times at exit
\\ --console LINE run a console line once the sketch is loaded (repeatable)
@@ -154,11 +133,11 @@ const usage =
\\
;
-/// A state file in a directory named `.sketchbook` (`zig build run`'s):
-/// the directory is made, with a `.gitignore` of `*` (its sessions database
-/// grows, and a jj snapshot would commit it), and the state file an older
-/// build kept beside it is moved in, once. A file is never replaced: when
-/// both exist, nothing moves.
+/// A save in a directory named `.sketchbook` (`zig build run`'s): the
+/// directory is made, with a `.gitignore` of `*` (the save grows, and a jj
+/// snapshot would commit it), and the state file an older build kept beside
+/// it (`<stem>.state`) is moved in, once, to be imported. A file is never
+/// replaced: when both exist, nothing moves.
fn sketchbookDir(io: std.Io, state: []const u8) void {
const dir = std.fs.path.dirname(state) orelse return;
if (!std.mem.eql(u8, std.fs.path.basename(dir), ".sketchbook")) return;
@@ -170,8 +149,9 @@ fn sketchbookDir(io: std.Io, state: []const u8) void {
var from_buf: [std.fs.max_path_bytes]u8 = undefined;
var to_buf: [std.fs.max_path_bytes]u8 = undefined;
const parent = std.fs.path.dirname(dir) orelse ".";
- const from = std.fmt.bufPrintSentinel(&from_buf, "{s}/{s}", .{ parent, std.fs.path.basename(state) }, 0) catch return;
- const to = std.fmt.bufPrintSentinel(&to_buf, "{s}", .{state}, 0) catch return;
+ const stem = Host.stemOf(std.fs.path.basename(state));
+ const from = std.fmt.bufPrintSentinel(&from_buf, "{s}/{s}.state", .{ parent, stem }, 0) catch return;
+ const to = std.fmt.bufPrintSentinel(&to_buf, "{s}/{s}.state", .{ dir, stem }, 0) catch return;
switch (std.os.linux.errno(std.os.linux.renameat2(std.os.linux.AT.FDCWD, from.ptr, std.os.linux.AT.FDCWD, to.ptr, .{ .NOREPLACE = true }))) {
.SUCCESS => std.debug.print("sketchbook: moved {s} to {s}\n", .{ from, to }),
.EXIST => std.debug.print("sketchbook: {s} and {s} both exist: nothing moved, {s} is used\n", .{ from, to, to }),