Something went wrong. Try again.
MPRIS for Zig: a media player's side of the D-Bus remote control interfaces, on mdbus and zig-dbus-service.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! What a player is doing, as MPRIS describes it.//!//! A program fills in a `State` whenever it is asked -- see `Player.snapshot`//! -- and the server works out from one snapshot to the next what changed//! and says so on the bus. Nothing here does any I/O, and nothing here knows//! D-Bus: the names are the specification's, the types are Zig's.
const std = @import("std");
pub const PlaybackStatus = enum { Playing, Paused, Stopped,};
pub const LoopStatus = enum { /// Play to the end of the list and stop. None, /// Play the current track over and over. Track, /// Play the whole list over and over. Playlist,};
/// The object path that says there is no track.pub const no_track = "/org/mpris/MediaPlayer2/TrackList/NoTrack";
/// One track, as `Metadata` and `GetTracksMetadata` describe it. Only/// `trackid` is required; everything left null or empty is left out.pub const Metadata = struct { /// An object path naming the track, unique within the player -- not a /// URI, and not the same thing as `url`. trackid: []const u8 = no_track, /// Microseconds. length_us: ?i64 = null, art_url: ?[]const u8 = null, title: ?[]const u8 = null, artists: []const []const u8 = &.{}, album: ?[]const u8 = null, album_artists: []const []const u8 = &.{}, track_number: ?i32 = null, disc_number: ?i32 = null, /// Where the track's audio is, if it has an address worth giving. url: ?[]const u8 = null, /// 0 to 1: how much the user likes it. A player with favorites rather /// than ratings says 1 for a favorite, and leaves it out otherwise. user_rating: ?f64 = null,
pub fn eql(a: Metadata, b: Metadata) bool { return std.mem.eql(u8, a.trackid, b.trackid) and a.length_us == b.length_us and optEql(a.art_url, b.art_url) and optEql(a.title, b.title) and listEql(a.artists, b.artists) and optEql(a.album, b.album) and listEql(a.album_artists, b.album_artists) and a.track_number == b.track_number and a.disc_number == b.disc_number and optEql(a.url, b.url) and a.user_rating == b.user_rating; }};
pub const Playlist = struct { /// An object path naming the playlist, unique within the player. id: []const u8, name: []const u8, /// A URI for an icon, or empty. icon: []const u8 = "",
pub fn eql(a: Playlist, b: Playlist) bool { return std.mem.eql(u8, a.id, b.id) and std.mem.eql(u8, a.name, b.name) and std.mem.eql(u8, a.icon, b.icon); }};
pub const State = struct { playback_status: PlaybackStatus = .Stopped, loop_status: LoopStatus = .None, shuffle: bool = false, /// 0 to 1; more than 1 means amplification. volume: f64 = 1, /// Microseconds into the current track, at the moment the snapshot was /// taken. The server moves it on by itself while `Playing`. position_us: i64 = 0, metadata: Metadata = .{},
can_go_next: bool = false, can_go_previous: bool = false, can_play: bool = false, can_pause: bool = false, can_seek: bool = false,
/// The tracks around the current one, for `org.mpris.MediaPlayer2.TrackList`. tracks: []const Metadata = &.{},
/// The player's playlists, for `org.mpris.MediaPlayer2.Playlists`. playlists: []const Playlist = &.{}, active_playlist: ?Playlist = null,};
fn optEql(a: ?[]const u8, b: ?[]const u8) bool { if (a == null or b == null) return a == null and b == null; return std.mem.eql(u8, a.?, b.?);}
fn listEql(a: []const []const u8, b: []const []const u8) bool { if (a.len != b.len) return false; for (a, b) |x, y| if (!std.mem.eql(u8, x, y)) return false; return true;}
pub fn tracksEql(a: []const Metadata, b: []const Metadata) bool { if (a.len != b.len) return false; for (a, b) |x, y| if (!x.eql(y)) return false; return true;}
pub fn playlistsEql(a: []const Playlist, b: []const Playlist) bool { if (a.len != b.len) return false; for (a, b) |x, y| if (!x.eql(y)) return false; return true;}
/// A copy of `state` whose strings and lists all live in `arena`, so a/// snapshot can be kept after whatever it was taken from has changed.pub fn clone(arena: std.mem.Allocator, state: State) std.mem.Allocator.Error!State { var out = state; out.metadata = try cloneMetadata(arena, state.metadata); const tracks = try arena.alloc(Metadata, state.tracks.len); for (state.tracks, tracks) |t, *o| o.* = try cloneMetadata(arena, t); out.tracks = tracks; const playlists = try arena.alloc(Playlist, state.playlists.len); for (state.playlists, playlists) |p, *o| o.* = try clonePlaylist(arena, p); out.playlists = playlists; if (state.active_playlist) |p| out.active_playlist = try clonePlaylist(arena, p); return out;}
fn cloneMetadata(arena: std.mem.Allocator, m: Metadata) !Metadata { var out = m; out.trackid = try arena.dupe(u8, m.trackid); out.art_url = try dupeOpt(arena, m.art_url); out.title = try dupeOpt(arena, m.title); out.artists = try dupeList(arena, m.artists); out.album = try dupeOpt(arena, m.album); out.album_artists = try dupeList(arena, m.album_artists); out.url = try dupeOpt(arena, m.url); return out;}
fn clonePlaylist(arena: std.mem.Allocator, p: Playlist) !Playlist { return .{ .id = try arena.dupe(u8, p.id), .name = try arena.dupe(u8, p.name), .icon = try arena.dupe(u8, p.icon), };}
fn dupeOpt(arena: std.mem.Allocator, s: ?[]const u8) !?[]const u8 { return if (s) |v| try arena.dupe(u8, v) else null;}
fn dupeList(arena: std.mem.Allocator, list: []const []const u8) ![]const []const u8 { const out = try arena.alloc([]const u8, list.len); for (list, out) |s, *o| o.* = try arena.dupe(u8, s); return out;}
/// A track id for a player's own id of a track, which can be anything --/// a UUID, a URI -- where an object path allows only `[A-Za-z0-9_]` in each/// element. Hex of the bytes is always valid and always reversible, so two/// different ids never become one path.pub fn trackId(buf: []u8, prefix: []const u8, id: []const u8) ![]const u8 { var w: std.Io.Writer = .fixed(buf); try w.writeAll(prefix); try w.writeAll("/t"); for (id) |c| try w.print("{x:0>2}", .{c}); return w.buffered();}
test "a track id is a valid path for any id" { var buf: [128]u8 = undefined; try std.testing.expectEqualStrings( "/dev/example/Track/t6c6962726172793a2f2f747261636b2f37", try trackId(&buf, "/dev/example/Track", "library://track/7"), );}
test "a clone owns everything it holds" { var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); var title = "Desperado".*; const artists = [_][]const u8{"Eagles"}; const s: State = .{ .metadata = .{ .trackid = "/t/1", .title = &title, .artists = &artists } }; const c = try clone(arena.allocator(), s); title[0] = 'X'; try std.testing.expectEqualStrings("Desperado", c.metadata.title.?); try std.testing.expect(c.metadata.eql(.{ .trackid = "/t/1", .title = "Desperado", .artists = &artists }));}