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