| 1 | import math |
| 2 | import libvendetta |
| 3 | import net.urllib |
| 4 | import ui2 |
| 5 | import libvendetta.mpv |
| 6 | |
| 7 | // The glue between the app's player and the D-Bus service in mpris_d_mpris.v: the |
| 8 | // snapshot builder that turns the player's observed state into what MPRIS |
| 9 | // serves, the pushes that publish it, and the executor that runs the |
| 10 | // commands the desktop sends. Everything here runs on the UI thread. |
| 11 | |
| 12 | // mpris_title is what Metadata's xesam:title holds: the media title mpv |
| 13 | // reports (its own --force-media-title or the file's metadata), falling |
| 14 | // back to the entry's display name. The title observer's value rides |
| 15 | // media_title; a synchronous read here would wait on mpv's core from the |
| 16 | // render thread. |
| 17 | fn (p &Player) mpris_title() string { |
| 18 | if p.media_title.len > 0 { |
| 19 | return p.media_title |
| 20 | } |
| 21 | if p.current_path.len > 0 { |
| 22 | return display_name(p.current_path, p.current_kind) |
| 23 | } |
| 24 | return '' |
| 25 | } |
| 26 | |
| 27 | // mpris_artist is Metadata's xesam:artist, from the observed metadata key. |
| 28 | // mpv matches tag keys case-insensitively, so the usual spellings land. |
| 29 | fn (p &Player) mpris_artist() string { |
| 30 | return p.media_artist |
| 31 | } |
| 32 | |
| 33 | // mpris_url is Metadata's xesam:url. A file becomes a file:// URI with the |
| 34 | // path percent-escaped; a link stays the URL it was opened as, and the |
| 35 | // kind comes off the player's carried state instead of a fresh search of |
| 36 | // the path string. |
| 37 | fn (p &Player) mpris_url() string { |
| 38 | if p.current_path.len == 0 { |
| 39 | return '' |
| 40 | } |
| 41 | if p.current_kind == .link { |
| 42 | return p.current_path |
| 43 | } |
| 44 | escaped := urllib.path_escape(p.current_path) |
| 45 | return 'file://${escaped}' |
| 46 | } |
| 47 | |
| 48 | // mpris_snapshot builds the state the service serves reads from. |
| 49 | fn (p &Player) mpris_snapshot() &MprisSnapshot { |
| 50 | mut status := 'Stopped' |
| 51 | if p.has_file { |
| 52 | status = if p.paused { 'Paused' } else { 'Playing' } |
| 53 | } |
| 54 | count := p.playlist.len |
| 55 | // The step flags name what a click would do: mpv clamps the step |
| 56 | // commands itself, but the flags let the desktop grey out buttons that |
| 57 | // would do nothing. Both directions work whenever anything is queued, |
| 58 | // because Previous on the first entry restarts it. |
| 59 | can_step := count > 1 || (count == 1 && p.has_file) |
| 60 | return &MprisSnapshot{ |
| 61 | status: status |
| 62 | loop_status: if p.loop { |
| 63 | 'Track' |
| 64 | } else if p.loop_playlist { |
| 65 | 'Playlist' |
| 66 | } else { |
| 67 | 'None' |
| 68 | } |
| 69 | shuffle: false |
| 70 | volume: p.volume |
| 71 | has_file: p.has_file |
| 72 | can_play: p.has_file |
| 73 | can_pause: p.has_file |
| 74 | can_seek: p.has_file && p.duration > 0 |
| 75 | can_go_next: can_step |
| 76 | can_go_prev: can_step |
| 77 | position: p.pos |
| 78 | duration: p.duration |
| 79 | title: p.mpris_title() |
| 80 | artist: p.mpris_artist() |
| 81 | url: p.mpris_url() |
| 82 | playlist_pos: p.playlist_index |
| 83 | playlist_len: count |
| 84 | rate: p.speed |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // mpris_push publishes the current state to the service when it runs. The |
| 89 | // caller names what changed so the worker can send one PropertiesChanged |
| 90 | // rather than a burst; a full announce is the same path with everything |
| 91 | // compared. |
| 92 | fn (mut p Player) mpris_push() { |
| 93 | if p.mpris == unsafe { nil } || !p.media_controls { |
| 94 | return |
| 95 | } |
| 96 | p.mpris.mpris_property(p.mpris_snapshot()) |
| 97 | } |
| 98 | |
| 99 | // mpris_announce publishes everything: after the service starts and on file |
| 100 | // changes, where half a dozen properties move together anyway. |
| 101 | fn (mut p Player) mpris_announce() { |
| 102 | if p.mpris == unsafe { nil } || !p.media_controls { |
| 103 | return |
| 104 | } |
| 105 | p.mpris.mpris_announce(p.mpris_snapshot()) |
| 106 | } |
| 107 | |
| 108 | // start_media_controls spawns the service once. The worker thread owns the bus |
| 109 | // from then on, so this only starts it; the saved setting and the first |
| 110 | // turn-on both come through here. |
| 111 | fn (mut p Player) start_media_controls() { |
| 112 | if p.mpris != unsafe { nil } { |
| 113 | return |
| 114 | } |
| 115 | p.mpris = mpris_new() |
| 116 | mut worker := p.mpris |
| 117 | spawn mpris_worker(mut worker) |
| 118 | } |
| 119 | |
| 120 | // set_media_controls turns the desktop media controls on or off. On, the |
| 121 | // service takes the bus name and the pushes start; off, the name is dropped |
| 122 | // and every push afterwards is a no-op. The worker thread is spawned once, |
| 123 | // the first time the feature turns on, and reused after that. |
| 124 | fn (mut p Player) set_media_controls(on bool) { |
| 125 | if p.media_controls == on { |
| 126 | return |
| 127 | } |
| 128 | p.media_controls = on |
| 129 | if on { |
| 130 | p.start_media_controls() |
| 131 | p.mpris_announce() |
| 132 | p.notify("Media controls on: the desktop's player applet sees vendetta.") |
| 133 | } else { |
| 134 | if p.mpris != unsafe { nil } { |
| 135 | p.mpris.mpris_free() |
| 136 | } |
| 137 | p.notify('Media controls off.') |
| 138 | } |
| 139 | ui2.set_menu_bar(p.menus()) |
| 140 | } |
| 141 | |
| 142 | // mpris_on_position is the position hook: Position follows playback, and the |
| 143 | // position that a seek landed on is announced as Seeked. |
| 144 | fn (mut p Player) mpris_on_position(pos f64) { |
| 145 | if p.mpris == unsafe { nil } || !p.media_controls { |
| 146 | return |
| 147 | } |
| 148 | p.mpris.mpris_track_position(pos) |
| 149 | p.mpris.mpris_on_position(pos) |
| 150 | } |
| 151 | |
| 152 | // mpris_seek_started and mpris_seek_landed follow mpv's seek events. mpv sends |
| 153 | // them for every seek, so the desktop's seeks and the app's keys and slider |
| 154 | // are announced the same way. |
| 155 | fn (mut p Player) mpris_seek_started() { |
| 156 | if p.mpris == unsafe { nil } || !p.media_controls { |
| 157 | return |
| 158 | } |
| 159 | p.mpris.mpris_seek_started() |
| 160 | } |
| 161 | |
| 162 | fn (mut p Player) mpris_seek_landed() { |
| 163 | if p.mpris == unsafe { nil } || !p.media_controls { |
| 164 | return |
| 165 | } |
| 166 | p.mpris.mpris_seek_landed() |
| 167 | } |
| 168 | |
| 169 | // mpris_on_volume publishes volume changes mpv's own controls made. |
| 170 | fn (mut p Player) mpris_on_volume() { |
| 171 | if p.mpris == unsafe { nil } || !p.media_controls { |
| 172 | return |
| 173 | } |
| 174 | p.mpris.mpris_on_volume() |
| 175 | } |
| 176 | |
| 177 | // mpris_echo marks the position reports that follow a command this service |
| 178 | // issued, so the executor's announcement is not doubled by the property |
| 179 | // hook. The flag rides the command value: the write below and the property |
| 180 | // events it produces are all the UI thread's work, so no lock is needed. |
| 181 | fn (mut p Player) mpris_set_position(value f64) { |
| 182 | mpv.set_double_async(p.mpv_ctx, 74, 'time-pos', value) |
| 183 | } |
| 184 | |
| 185 | // mpris_execute runs one command from the service, on the UI thread, where |
| 186 | // every other mpv write happens. The replies to the Seeked/PropertiesChanged |
| 187 | // chatter are the worker's business; this side only acts. |
| 188 | fn (mut p Player) mpris_execute(cmd MprisCommand) { |
| 189 | match cmd.kind { |
| 190 | // The UI's copy of pause is what the bus reports and what the frame |
| 191 | // relay throttles on, and it only catches up when mpv's event is |
| 192 | // drained, which an idle window does not do soon. So each pause write |
| 193 | // updates the copy at once, and mpv's own event then confirms it. |
| 194 | .play { |
| 195 | if p.has_file { |
| 196 | p.paused = false |
| 197 | mpv.set_flag_async(p.mpv_ctx, 10, 'pause', false) |
| 198 | p.mpris_push() |
| 199 | } |
| 200 | } |
| 201 | .pause { |
| 202 | if p.has_file { |
| 203 | p.paused = true |
| 204 | mpv.set_flag_async(p.mpv_ctx, 10, 'pause', true) |
| 205 | p.mpris_push() |
| 206 | } |
| 207 | } |
| 208 | .toggle_play { |
| 209 | if p.has_file { |
| 210 | p.paused = !p.paused |
| 211 | mpv.set_flag_async(p.mpv_ctx, 10, 'pause', p.paused) |
| 212 | p.mpris_push() |
| 213 | } |
| 214 | } |
| 215 | .stop { |
| 216 | p.close_file() |
| 217 | } |
| 218 | .next { |
| 219 | mpv.command_async(p.mpv_ctx, 52, ['playlist-next']) |
| 220 | } |
| 221 | .previous { |
| 222 | mpv.command_async(p.mpv_ctx, 53, ['playlist-prev']) |
| 223 | } |
| 224 | .seek { |
| 225 | // The offset is relative, as the spec's Seek is. mpv reports the |
| 226 | // seek and where it lands, so Seeked follows without help here. |
| 227 | if p.has_file { |
| 228 | mpv.command_async(p.mpv_ctx, 18, ['seek', '${cmd.arg:.3f}']) |
| 229 | } |
| 230 | } |
| 231 | .set_position { |
| 232 | // The spec drops a position outside 0..length rather than clamping it. |
| 233 | if p.has_file && cmd.arg >= 0 && (p.duration <= 0 || cmd.arg <= p.duration) |
| 234 | && p.mpris_track_current(cmd.track) { |
| 235 | p.mpris_set_position(cmd.arg) |
| 236 | } |
| 237 | } |
| 238 | .open_uri { |
| 239 | p.load_or_prompt([cmd.uri]) |
| 240 | } |
| 241 | .quit { |
| 242 | ui2.quit() |
| 243 | } |
| 244 | .raise { |
| 245 | // Nothing in ui2 raises the window yet; the desktop falls back |
| 246 | // to highlighting the taskbar entry, which minimize_window's |
| 247 | // counterpart would do. Left as a no-op until ui2 grows a raise. |
| 248 | } |
| 249 | .volume { |
| 250 | // Volume 1.0 is mpv's 100. The app's max still caps the write, |
| 251 | // as it caps the slider. The mirror is set now so a read right |
| 252 | // after the write sees the new value; mpv's own event confirms it. |
| 253 | v := math.max(0.0, math.min(p.max_volume, cmd.arg * 100.0)) |
| 254 | p.volume = v |
| 255 | mpv.set_double_async(p.mpv_ctx, 12, 'volume', v) |
| 256 | p.mpris_push() |
| 257 | } |
| 258 | .loop_status { |
| 259 | match int(cmd.arg) { |
| 260 | 1 { |
| 261 | p.loop = true |
| 262 | p.loop_playlist = false |
| 263 | mpv.set_string_async(p.mpv_ctx, 15, 'loop-file', 'inf') |
| 264 | mpv.set_string_async(p.mpv_ctx, 54, 'loop-playlist', 'no') |
| 265 | } |
| 266 | 2 { |
| 267 | p.loop = false |
| 268 | p.loop_playlist = true |
| 269 | mpv.set_string_async(p.mpv_ctx, 15, 'loop-file', 'no') |
| 270 | mpv.set_string_async(p.mpv_ctx, 54, 'loop-playlist', 'inf') |
| 271 | } |
| 272 | else { |
| 273 | p.loop = false |
| 274 | p.loop_playlist = false |
| 275 | mpv.set_string_async(p.mpv_ctx, 15, 'loop-file', 'no') |
| 276 | mpv.set_string_async(p.mpv_ctx, 54, 'loop-playlist', 'no') |
| 277 | } |
| 278 | } |
| 279 | p.mpris_push() |
| 280 | p.settings_changed() |
| 281 | ui2.set_menu_bar(p.menus()) |
| 282 | } |
| 283 | .shuffle { |
| 284 | // mpv's playlist-shuffle is an action, not a state, so the |
| 285 | // property would lie: the flag stays where the queue order |
| 286 | // leaves it and the desktop's toggle shuffles once. |
| 287 | if cmd.arg > 0 { |
| 288 | mpv.command_async(p.mpv_ctx, 85, ['playlist-shuffle']) |
| 289 | } |
| 290 | p.mpris_push() |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // mpris_track_current reports whether a SetPosition's trackid names the |
| 296 | // file playing now. A stale id (the desktop answered from an old snapshot) |
| 297 | // is dropped: the spec says to ignore it, because the position it carries |
| 298 | // belongs to a track that is gone. |
| 299 | fn (p &Player) mpris_track_current(track string) bool { |
| 300 | if track.len == 0 { |
| 301 | return false |
| 302 | } |
| 303 | return track == mpris_track_path(p.mpris_url()) |
| 304 | } |
| 305 | |
| 306 | // drain_mpris pops the command queue dry. Called from the per-frame work, |
| 307 | // so a burst of commands costs one pass; each runs in order. |
| 308 | fn (mut p Player) drain_mpris() { |
| 309 | if p.mpris == unsafe { nil } { |
| 310 | return |
| 311 | } |
| 312 | for { |
| 313 | mut cmd := MprisCommand{} |
| 314 | if p.mpris.commands.try_pop(mut cmd) != .success { |
| 315 | return |
| 316 | } |
| 317 | p.mpris_execute(cmd) |
| 318 | } |
| 319 | } |
| 320 | |