import math import libvendetta import net.urllib import ui2 import libvendetta.mpv // The glue between the app's player and the D-Bus service in mpris_d_mpris.v: the // snapshot builder that turns the player's observed state into what MPRIS // serves, the pushes that publish it, and the executor that runs the // commands the desktop sends. Everything here runs on the UI thread. // mpris_title is what Metadata's xesam:title holds: the media title mpv // reports (its own --force-media-title or the file's metadata), falling // back to the entry's display name. The title observer's value rides // media_title; a synchronous read here would wait on mpv's core from the // render thread. fn (p &Player) mpris_title() string { if p.media_title.len > 0 { return p.media_title } if p.current_path.len > 0 { return display_name(p.current_path, p.current_kind) } return '' } // mpris_artist is Metadata's xesam:artist, from the observed metadata key. // mpv matches tag keys case-insensitively, so the usual spellings land. fn (p &Player) mpris_artist() string { return p.media_artist } // mpris_url is Metadata's xesam:url. A file becomes a file:// URI with the // path percent-escaped; a link stays the URL it was opened as, and the // kind comes off the player's carried state instead of a fresh search of // the path string. fn (p &Player) mpris_url() string { if p.current_path.len == 0 { return '' } if p.current_kind == .link { return p.current_path } escaped := urllib.path_escape(p.current_path) return 'file://${escaped}' } // mpris_snapshot builds the state the service serves reads from. fn (p &Player) mpris_snapshot() &MprisSnapshot { mut status := 'Stopped' if p.has_file { status = if p.paused { 'Paused' } else { 'Playing' } } count := p.playlist.len // The step flags name what a click would do: mpv clamps the step // commands itself, but the flags let the desktop grey out buttons that // would do nothing. Both directions work whenever anything is queued, // because Previous on the first entry restarts it. can_step := count > 1 || (count == 1 && p.has_file) return &MprisSnapshot{ status: status loop_status: if p.loop { 'Track' } else if p.loop_playlist { 'Playlist' } else { 'None' } shuffle: false volume: p.volume has_file: p.has_file can_play: p.has_file can_pause: p.has_file can_seek: p.has_file && p.duration > 0 can_go_next: can_step can_go_prev: can_step position: p.pos duration: p.duration title: p.mpris_title() artist: p.mpris_artist() url: p.mpris_url() playlist_pos: p.playlist_index playlist_len: count rate: p.speed } } // mpris_push publishes the current state to the service when it runs. The // caller names what changed so the worker can send one PropertiesChanged // rather than a burst; a full announce is the same path with everything // compared. fn (mut p Player) mpris_push() { if p.mpris == unsafe { nil } || !p.media_controls { return } p.mpris.mpris_property(p.mpris_snapshot()) } // mpris_announce publishes everything: after the service starts and on file // changes, where half a dozen properties move together anyway. fn (mut p Player) mpris_announce() { if p.mpris == unsafe { nil } || !p.media_controls { return } p.mpris.mpris_announce(p.mpris_snapshot()) } // start_media_controls spawns the service once. The worker thread owns the bus // from then on, so this only starts it; the saved setting and the first // turn-on both come through here. fn (mut p Player) start_media_controls() { if p.mpris != unsafe { nil } { return } p.mpris = mpris_new() mut worker := p.mpris spawn mpris_worker(mut worker) } // set_media_controls turns the desktop media controls on or off. On, the // service takes the bus name and the pushes start; off, the name is dropped // and every push afterwards is a no-op. The worker thread is spawned once, // the first time the feature turns on, and reused after that. fn (mut p Player) set_media_controls(on bool) { if p.media_controls == on { return } p.media_controls = on if on { p.start_media_controls() p.mpris_announce() p.notify("Media controls on: the desktop's player applet sees vendetta.") } else { if p.mpris != unsafe { nil } { p.mpris.mpris_free() } p.notify('Media controls off.') } ui2.set_menu_bar(p.menus()) } // mpris_on_position is the position hook: Position follows playback, and the // position that a seek landed on is announced as Seeked. fn (mut p Player) mpris_on_position(pos f64) { if p.mpris == unsafe { nil } || !p.media_controls { return } p.mpris.mpris_track_position(pos) p.mpris.mpris_on_position(pos) } // mpris_seek_started and mpris_seek_landed follow mpv's seek events. mpv sends // them for every seek, so the desktop's seeks and the app's keys and slider // are announced the same way. fn (mut p Player) mpris_seek_started() { if p.mpris == unsafe { nil } || !p.media_controls { return } p.mpris.mpris_seek_started() } fn (mut p Player) mpris_seek_landed() { if p.mpris == unsafe { nil } || !p.media_controls { return } p.mpris.mpris_seek_landed() } // mpris_on_volume publishes volume changes mpv's own controls made. fn (mut p Player) mpris_on_volume() { if p.mpris == unsafe { nil } || !p.media_controls { return } p.mpris.mpris_on_volume() } // mpris_echo marks the position reports that follow a command this service // issued, so the executor's announcement is not doubled by the property // hook. The flag rides the command value: the write below and the property // events it produces are all the UI thread's work, so no lock is needed. fn (mut p Player) mpris_set_position(value f64) { mpv.set_double_async(p.mpv_ctx, 74, 'time-pos', value) } // mpris_execute runs one command from the service, on the UI thread, where // every other mpv write happens. The replies to the Seeked/PropertiesChanged // chatter are the worker's business; this side only acts. fn (mut p Player) mpris_execute(cmd MprisCommand) { match cmd.kind { // The UI's copy of pause is what the bus reports and what the frame // relay throttles on, and it only catches up when mpv's event is // drained, which an idle window does not do soon. So each pause write // updates the copy at once, and mpv's own event then confirms it. .play { if p.has_file { p.paused = false mpv.set_flag_async(p.mpv_ctx, 10, 'pause', false) p.mpris_push() } } .pause { if p.has_file { p.paused = true mpv.set_flag_async(p.mpv_ctx, 10, 'pause', true) p.mpris_push() } } .toggle_play { if p.has_file { p.paused = !p.paused mpv.set_flag_async(p.mpv_ctx, 10, 'pause', p.paused) p.mpris_push() } } .stop { p.close_file() } .next { mpv.command_async(p.mpv_ctx, 52, ['playlist-next']) } .previous { mpv.command_async(p.mpv_ctx, 53, ['playlist-prev']) } .seek { // The offset is relative, as the spec's Seek is. mpv reports the // seek and where it lands, so Seeked follows without help here. if p.has_file { mpv.command_async(p.mpv_ctx, 18, ['seek', '${cmd.arg:.3f}']) } } .set_position { // The spec drops a position outside 0..length rather than clamping it. if p.has_file && cmd.arg >= 0 && (p.duration <= 0 || cmd.arg <= p.duration) && p.mpris_track_current(cmd.track) { p.mpris_set_position(cmd.arg) } } .open_uri { p.load_or_prompt([cmd.uri]) } .quit { ui2.quit() } .raise { // Nothing in ui2 raises the window yet; the desktop falls back // to highlighting the taskbar entry, which minimize_window's // counterpart would do. Left as a no-op until ui2 grows a raise. } .volume { // Volume 1.0 is mpv's 100. The app's max still caps the write, // as it caps the slider. The mirror is set now so a read right // after the write sees the new value; mpv's own event confirms it. v := math.max(0.0, math.min(p.max_volume, cmd.arg * 100.0)) p.volume = v mpv.set_double_async(p.mpv_ctx, 12, 'volume', v) p.mpris_push() } .loop_status { match int(cmd.arg) { 1 { p.loop = true p.loop_playlist = false mpv.set_string_async(p.mpv_ctx, 15, 'loop-file', 'inf') mpv.set_string_async(p.mpv_ctx, 54, 'loop-playlist', 'no') } 2 { p.loop = false p.loop_playlist = true mpv.set_string_async(p.mpv_ctx, 15, 'loop-file', 'no') mpv.set_string_async(p.mpv_ctx, 54, 'loop-playlist', 'inf') } else { p.loop = false p.loop_playlist = false mpv.set_string_async(p.mpv_ctx, 15, 'loop-file', 'no') mpv.set_string_async(p.mpv_ctx, 54, 'loop-playlist', 'no') } } p.mpris_push() p.settings_changed() ui2.set_menu_bar(p.menus()) } .shuffle { // mpv's playlist-shuffle is an action, not a state, so the // property would lie: the flag stays where the queue order // leaves it and the desktop's toggle shuffles once. if cmd.arg > 0 { mpv.command_async(p.mpv_ctx, 85, ['playlist-shuffle']) } p.mpris_push() } } } // mpris_track_current reports whether a SetPosition's trackid names the // file playing now. A stale id (the desktop answered from an old snapshot) // is dropped: the spec says to ignore it, because the position it carries // belongs to a track that is gone. fn (p &Player) mpris_track_current(track string) bool { if track.len == 0 { return false } return track == mpris_track_path(p.mpris_url()) } // drain_mpris pops the command queue dry. Called from the per-frame work, // so a burst of commands costs one pass; each runs in order. fn (mut p Player) drain_mpris() { if p.mpris == unsafe { nil } { return } for { mut cmd := MprisCommand{} if p.mpris.commands.try_pop(mut cmd) != .success { return } p.mpris_execute(cmd) } }