// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! A pretend player on the session bus, to try the library against real //! peers. //! //! mpris-example [seconds] //! //! It owns `org.mpris.MediaPlayer2.mprisexample` and plays a list of three //! tracks, none of them real: play, pause, next, previous, seeking, shuffle, //! loop and volume all change its state, and the state it reports, just as a //! real player's would. It serves for the seconds given, or until killed. //! //! playerctl --player=mprisexample metadata //! playerctl --player=mprisexample play-pause //! playerctl --player=mprisexample position 30 const std = @import("std"); const mpris = @import("mpris"); const mdbus = @import("mdbus"); const tracks = [_]mpris.Metadata{ .{ .trackid = "/dev/jcollie/MprisExample/Track/1", .length_us = 214_000_000, .title = "Desperado", .artists = &.{"Eagles"}, .album = "Desperado", .track_number = 1 }, .{ .trackid = "/dev/jcollie/MprisExample/Track/2", .length_us = 252_000_000, .title = "A Horse with No Name", .artists = &.{"America"}, .album = "America", .track_number = 2 }, .{ .trackid = "/dev/jcollie/MprisExample/Track/3", .length_us = 349_000_000, .title = "Dirty Laundry", .artists = &.{"Don Henley"}, .album = "I Can't Stand Still", .track_number = 3 }, }; const playlists = [_]mpris.Playlist{ .{ .id = "/dev/jcollie/MprisExample/Playlist/1", .name = "Favorites" }, .{ .id = "/dev/jcollie/MprisExample/Playlist/2", .name = "Road Trip" }, }; /// The pretend player. Its commands arrive on the server's thread and /// `update` is safe from any, so a mutex is all it needs. const Example = struct { io: std.Io, mutex: std.Io.Mutex = .init, server: *mpris.Server = undefined, index: usize = 0, status: mpris.PlaybackStatus = .Paused, loop: mpris.LoopStatus = .None, shuffle: bool = false, volume: f64 = 0.8, position_us: i64 = 0, active: ?usize = null, fn from(context: *anyopaque) *Example { return @ptrCast(@alignCast(context)); } fn snapshot(context: *anyopaque, _: std.mem.Allocator, out: *mpris.State) anyerror!void { const self = from(context); self.mutex.lockUncancelable(self.io); defer self.mutex.unlock(self.io); out.* = .{ .playback_status = self.status, .loop_status = self.loop, .shuffle = self.shuffle, .volume = self.volume, .position_us = self.position_us, .metadata = tracks[self.index], .can_go_next = true, .can_go_previous = true, .can_play = true, .can_pause = true, .can_seek = true, // Static data, which outlives the server; nothing to copy. .tracks = &tracks, .playlists = &playlists, .active_playlist = if (self.active) |i| playlists[i] else null, }; } /// Change the state under the lock, then tell the server. fn change(self: *Example, comptime f: fn (*Example) void) void { { self.mutex.lockUncancelable(self.io); defer self.mutex.unlock(self.io); f(self); } self.server.update() catch |err| std.log.err("update: {t}", .{err}); } fn cmd(comptime f: fn (*Example) void) fn (*anyopaque) void { return struct { fn g(context: *anyopaque) void { from(context).change(f); } }.g; } fn play(self: *Example) void { self.status = .Playing; } fn pause(self: *Example) void { self.status = .Paused; } fn playPause(self: *Example) void { self.status = if (self.status == .Playing) .Paused else .Playing; } fn stop(self: *Example) void { self.status = .Stopped; self.position_us = 0; } fn next(self: *Example) void { self.index = (self.index + 1) % tracks.len; self.position_us = 0; } fn previous(self: *Example) void { self.index = (self.index + tracks.len - 1) % tracks.len; self.position_us = 0; } fn setPosition(context: *anyopaque, position_us: i64) void { const self = from(context); { self.mutex.lockUncancelable(self.io); defer self.mutex.unlock(self.io); self.position_us = position_us; } self.server.update() catch {}; } fn setShuffle(context: *anyopaque, on: bool) void { const self = from(context); { self.mutex.lockUncancelable(self.io); defer self.mutex.unlock(self.io); self.shuffle = on; } self.server.update() catch {}; } fn setLoop(context: *anyopaque, status: mpris.LoopStatus) void { const self = from(context); { self.mutex.lockUncancelable(self.io); defer self.mutex.unlock(self.io); self.loop = status; } self.server.update() catch {}; } fn setVolume(context: *anyopaque, volume: f64) void { const self = from(context); { self.mutex.lockUncancelable(self.io); defer self.mutex.unlock(self.io); self.volume = volume; } self.server.update() catch {}; } fn goTo(context: *anyopaque, trackid: []const u8) void { const self = from(context); { self.mutex.lockUncancelable(self.io); defer self.mutex.unlock(self.io); for (tracks, 0..) |t, i| if (std.mem.eql(u8, t.trackid, trackid)) { self.index = i; self.position_us = 0; }; } self.server.update() catch {}; } fn activatePlaylist(context: *anyopaque, id: []const u8) void { const self = from(context); { self.mutex.lockUncancelable(self.io); defer self.mutex.unlock(self.io); for (playlists, 0..) |p, i| if (std.mem.eql(u8, p.id, id)) { self.active = i; }; } self.server.update() catch {}; } }; pub fn main(init: std.process.Init) !u8 { const io = init.io; const gpa = init.gpa; var args: std.process.Args.Iterator = .init(init.minimal.args); defer args.deinit(); _ = args.skip(); const seconds: ?i64 = if (args.next()) |s| try std.fmt.parseInt(i64, s, 10) else null; var connection = try mdbus.Connection.connectSession(io, gpa, init.minimal.environ); defer connection.deinit(); var example: Example = .{ .io = io }; const server = try mpris.Server.create(gpa, io, &connection, .{ .name = "mprisexample", .identity = "MPRIS Example", .desktop_entry = "mprisexample", }, .{ .context = &example, .snapshot = Example.snapshot, .play = Example.cmd(Example.play), .pause = Example.cmd(Example.pause), .play_pause = Example.cmd(Example.playPause), .stop = Example.cmd(Example.stop), .next = Example.cmd(Example.next), .previous = Example.cmd(Example.previous), .set_position = Example.setPosition, .set_shuffle = Example.setShuffle, .set_loop_status = Example.setLoop, .set_volume = Example.setVolume, .go_to = Example.goTo, .activate_playlist = Example.activatePlaylist, }); defer server.destroy(); example.server = server; try server.start(); std.debug.print("serving {s}\n", .{server.bus_name}); if (seconds) |s| { var runner = try io.concurrent(mpris.Server.run, .{server}); try io.sleep(.fromSeconds(s), .awake); server.stop(); try runner.await(io); } else { try server.run(); } return 0; }