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.zig50
1 files changed, 43 insertions, 7 deletions
diff --git a/src/host/root.zig b/src/host/root.zig
index 158f894..5889b42 100644
--- a/src/host/root.zig
+++ b/src/host/root.zig
@@ -2,10 +2,11 @@
//! that it hot-reloads whenever a build replaces it.
//!
//! sketchbook [options] [LIBRARY]
-//! --state FILE persistent state (default: <name>.state)
+//! --state FILE persistent state (default: $XDG_DATA_HOME/<name>/<name>.state;
+//! sketch.state 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: no snapshots or recordings
+//! --fresh start from default state (sessions, marks and clips are 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)
@@ -54,6 +55,7 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void {
try lines.append(gpa, args.next() orelse fatal("--console needs a line"));
} else if (std.mem.eql(u8, arg, "--script")) {
script_path = args.next() orelse fatal("--script needs a file");
+ options.script = true;
} else if (std.mem.eql(u8, arg, "--define")) {
const pair = args.next() orelse fatal("--define needs NAME=VALUE");
const eq = std.mem.indexOfScalar(u8, pair, '=') orelse fatal("--define needs NAME=VALUE");
@@ -93,15 +95,17 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void {
// 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, store or
- // recordings file, nor fail on the store's lock. An export's are
+ // 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{ "", ".inputs", ".store" }) |suffix| {
+ 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;
@@ -127,10 +131,11 @@ 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 (default: sketch.state)
+ \\ --state FILE persistent state; sessions are kept beside it (default:
+ \\ $XDG_DATA_HOME/<name>/<name>.state, sketch.state for --script, --export)
\\ --headless offscreen video, silent audio, no vsync
\\ --script FILE play a script instead of reading devices
- \\ --fresh start from default state: no snapshots or recordings
+ \\ --fresh start from default state (sessions, marks and clips are 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)
@@ -143,6 +148,36 @@ 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
+/// 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;
+ const cwd = std.Io.Dir.cwd();
+ cwd.createDirPath(io, dir) catch return;
+ 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 => {},
+ }
+ }
+}
+
fn fatal(message: []const u8) noreturn {
std.debug.print("sketchbook: {s}\n", .{message});
std.process.exit(2);
@@ -202,6 +237,7 @@ test {
_ = memory;
_ = fields;
_ = timeline;
+ _ = @import("Sessions.zig");
_ = @import("audio.zig");
_ = @import("Console.zig");
_ = @import("control.zig");