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.zig67
1 files changed, 24 insertions, 43 deletions
diff --git a/src/host/root.zig b/src/host/root.zig
index 5889b42..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,25 +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 or store file,
- // nor fail on the store's lock (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");
- defer if (private) for ([_][]const u8{ "", ".store" }) |suffix| {
- var from: [std.fs.max_path_bytes + 16]u8 = undefined;
- var to: [std.fs.max_path_bytes + 16]u8 = undefined;
- const a = std.mem.printSentinel(&from, "{s}{s}", .{ options.state, suffix }, 0) catch continue;
- const b = std.mem.printSentinel(&to, "{s}{s}", .{ given, suffix }, 0) catch continue;
- _ = if (options.export_to != null) std.os.linux.unlink(a.ptr) else std.os.linux.rename(a.ptr, b.ptr);
- };
+ 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();
{
@@ -131,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)
@@ -148,10 +133,10 @@ 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 and store files
-/// an older build kept beside it are moved in, once. A file is never
+/// 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;
@@ -161,20 +146,16 @@ fn sketchbookDir(io: std.Io, state: []const u8) void {
var buf: [std.fs.max_path_bytes]u8 = undefined;
const ignore = std.fmt.bufPrint(&buf, "{s}/.gitignore", .{dir}) catch return;
cwd.writeFile(io, .{ .sub_path = ignore, .data = "*\n", .flags = .{ .exclusive = true } }) catch {};
- for ([_][]const u8{ "", ".store" }) |suffix| {
- 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}{s}", .{ parent, std.fs.path.basename(state), suffix }, 0) catch return;
- const to = std.fmt.bufPrintSentinel(&to_buf, "{s}{s}", .{ state, suffix }, 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 });
- return;
- },
- else => {},
- }
+ 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 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 }),
+ else => {},
}
}