diff options
Diffstat (limited to 'src/host/Sessions.zig')
| -rw-r--r-- | src/host/Sessions.zig | 76 |
1 files changed, 45 insertions, 31 deletions
diff --git a/src/host/Sessions.zig b/src/host/Sessions.zig index 809f160..6f10bba 100644 --- a/src/host/Sessions.zig +++ b/src/host/Sessions.zig @@ -161,6 +161,7 @@ pub fn deinit(s: *Sessions) void { pub fn close(s: *Sessions, state: []const u8, ended: u64) void { defer s.deinit(); if (s.db == .off) return; + s.stopClip() catch {}; _ = s.keyframe(.exit, state) catch {}; s.commitSynced(.{ .end = ended }) catch |err| std.log.warn("sessions: last commit failed: {t}", .{err}); } @@ -227,10 +228,12 @@ pub fn stretch(s: *Sessions, st: Stretch) !void { s.stretches += 1; } -/// Mark `name`: a keyframe of `state`, synced to disk. -pub fn mark(s: *Sessions, name: []const u8, state: []const u8) !void { - const k = try s.keyframe(.mark, state); +/// Mark `name`: a keyframe of `state` (`why`: `.mark`, or `.import`), +/// synced to disk. Returns the keyframe's index. +pub fn mark(s: *Sessions, name: []const u8, why: Why, state: []const u8) !u64 { + const k = try s.keyframe(why, state); try s.commitSynced(.{ .mark = .{ .name = name, .key = k } }); + return k; } /// Starts clip `name` at the next frame, played from keyframe `k`. @@ -366,8 +369,19 @@ fn Commit(comptime DB: type) type { // --------------------------------------------------------------------------- // Reading -pub const Found = struct { session: u64, key: u64 }; -pub const Clip = struct { session: u64, key: u64, start: u64, end: u64 }; +/// A mark (`start` and `end` 0) or a clip. +pub const Entry = struct { + session: u64, + key: u64, + start: u64 = 0, + end: u64 = 0, + bytes: [max_name]u8 = undefined, + len: usize = 0, + + pub fn name(e: *const Entry) []const u8 { + return e.bytes[0..e.len]; + } +}; /// A keyframe, its bytes `gpa`'s (`free`). pub const Loaded = struct { frame: u64, @@ -387,44 +401,44 @@ pub const Loaded = struct { } }; +pub fn markAt(s: *Sessions, i: u64) !Entry { + return s.entryAt(.marks, i); +} +pub fn clipAt(s: *Sessions, i: u64) !Entry { + return s.entryAt(.clips, i); +} /// The newest mark named `name`. -pub fn findMark(s: *Sessions, name: []const u8) !?Found { - const n = try s.count(.marks); - var i = n; - while (i > 0) { - i -= 1; - const e = try s.entry(.marks, i, name) orelse continue; - return .{ .session = e.session, .key = e.key }; - } - return null; +pub fn findMark(s: *Sessions, name: []const u8) !?Entry { + return s.findNamed(.marks, name); } - /// The newest clip named `name`. -pub fn findClip(s: *Sessions, name: []const u8) !?Clip { - var i = try s.count(.clips); +pub fn findClip(s: *Sessions, name: []const u8) !?Entry { + return s.findNamed(.clips, name); +} + +fn findNamed(s: *Sessions, which: K, name: []const u8) !?Entry { + var i = try s.count(which); while (i > 0) { i -= 1; - return try s.entry(.clips, i, name) orelse continue; + const e = try s.entryAt(which, i); + if (std.mem.eql(u8, e.name(), name)) return e; } return null; } -/// Entry `i` of `marks` or `clips` when it is named `name`. -fn entry(s: *Sessions, which: K, i: u64, name: []const u8) !?Clip { +fn entryAt(s: *Sessions, which: K, i: u64) !Entry { switch (s.db) { .off => return error.NotRecording, inline .memory, .file => |*db| { const R = Read(@TypeOf(db.*)); - const e = try R.at(try R.list(try R.root(db), which) orelse return null, i); - var buf: [max_name]u8 = undefined; - const got = (try R.field(e, .name)).readBytes(&buf) catch return null; - if (!std.mem.eql(u8, got, name)) return null; - return .{ - .session = try R.uint(e, .session), - .key = try R.uint(e, .key), - .start = if (which == .clips) try R.uint(e, .start) else 0, - .end = if (which == .clips) try R.uint(e, .end) else 0, - }; + const e = try R.at(try R.list(try R.root(db), which) orelse return error.NoSuchEntry, i); + var out: Entry = .{ .session = try R.uint(e, .session), .key = try R.uint(e, .key) }; + out.len = ((try R.field(e, .name)).readBytes(&out.bytes) catch return error.CorruptSession).len; + if (which == .clips) { + out.start = try R.uint(e, .start); + out.end = try R.uint(e, .end); + } + return out; }, } } @@ -432,7 +446,7 @@ fn entry(s: *Sessions, which: K, i: u64, name: []const u8) !?Clip { /// Entries in the root list `which`, or sessions. pub fn count(s: *Sessions, which: K) !u64 { switch (s.db) { - .off => return 0, + .off => return error.NotRecording, inline .memory, .file => |*db| { const R = Read(@TypeOf(db.*)); return if (try R.list(try R.root(db), which)) |l| try l.count() else 0; |
