|
1
|
1 /*
|
|
|
2 * Beefweb <-> mpris "compatibility" layer.
|
|
|
3 *
|
|
|
4 * Copyright (C) 2026 Paper
|
|
|
5 *
|
|
|
6 * This program is free software; you can redistribute it and/or
|
|
|
7 * modify it under the terms of the GNU General Public License
|
|
|
8 * as published by the Free Software Foundation; either version 2
|
|
|
9 * of the License, or (at your option) any later version.
|
|
|
10 *
|
|
|
11 * This program is distributed in the hope that it will be useful,
|
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
14 * GNU General Public License for more details.
|
|
|
15 *
|
|
|
16 * You should have received a copy of the GNU General Public License
|
|
|
17 * along with this program; if not, see
|
|
|
18 * <https://www.gnu.org/licenses/>.
|
|
|
19 */
|
|
|
20
|
|
0
|
21 use crate::beefweb;
|
|
|
22
|
|
|
23 use zbus::fdo;
|
|
|
24 use zbus::Result;
|
|
|
25 use std::collections::HashMap;
|
|
2
|
26 //use std::io::Write;
|
|
0
|
27 use std::cell::RefCell;
|
|
|
28
|
|
|
29 /* teehee */
|
|
|
30 pub struct BeefwebPlayer {
|
|
|
31 bw: beefweb::Beefweb,
|
|
|
32 artcache: String,
|
|
|
33 /* artmap
|
|
|
34 * key: %path% column from beefweb
|
|
|
35 * value: local path of the artwork
|
|
|
36 * also LOL RUST FUCK */
|
|
|
37 artmap: RefCell<HashMap<String, String>>,
|
|
|
38 }
|
|
|
39
|
|
|
40 impl BeefwebPlayer {
|
|
|
41 pub fn new(base: &str, artcache: &str) -> BeefwebPlayer
|
|
|
42 {
|
|
|
43 return BeefwebPlayer {
|
|
|
44 bw: beefweb::Beefweb::new(base),
|
|
|
45 artcache: artcache.to_string(),
|
|
|
46 artmap: RefCell::new(HashMap::new()),
|
|
|
47 };
|
|
|
48 }
|
|
|
49
|
|
|
50 /*
|
|
|
51 async fn get_artwork(&self, playlist_id: &str, index: i64, path: &str) -> fdo::Result<String>
|
|
|
52 {
|
|
|
53 match self.artmap.borrow().get(path) {
|
|
|
54 Some(x) => return Ok(x.to_string()),
|
|
|
55 _ => (),
|
|
|
56 }
|
|
|
57
|
|
|
58 /* Ok, art path isn't in the "cache". Ask beefweb for it. */
|
|
|
59 let art = self.bw.artwork(playlist_id, index);
|
|
|
60 let artpath = format!("{}/{}", self.artcache, uuid::Uuid::new_v4());
|
|
|
61
|
|
|
62 /* XXX might be a good idea to check the bytes for an extension...? */
|
|
|
63 let fr = std::fs::OpenOptions::new()
|
|
|
64 .write(true)
|
|
|
65 .open(&artpath);
|
|
|
66
|
|
|
67 match fr {
|
|
|
68 Err(_) => return Err(fdo::Error::Failed("Failed to open file!".to_string())),
|
|
|
69 _ => (),
|
|
|
70 };
|
|
|
71
|
|
|
72 let mut f = fr.unwrap();
|
|
|
73
|
|
|
74 let artaw = art.await;
|
|
|
75
|
|
|
76 match artaw {
|
|
|
77 Err(_) => return Err(fdo::Error::Failed("Uh oh".to_string())),
|
|
|
78 _ => (),
|
|
|
79 };
|
|
|
80
|
|
|
81 f.write(artaw.unwrap().as_ref());
|
|
|
82
|
|
|
83 self.artmap.borrow_mut().insert(path.to_string(), artpath.to_string());
|
|
|
84
|
|
|
85 return Ok(artpath);
|
|
|
86 }
|
|
|
87 */
|
|
|
88 }
|
|
|
89
|
|
|
90 fn secs_to_time(x: f64) -> mpris_server::Time
|
|
|
91 {
|
|
|
92 return mpris_server::Time::from_micros((x * 1000000.0).round() as i64);
|
|
|
93 }
|
|
|
94
|
|
|
95 fn time_to_secs(x: mpris_server::Time) -> f64
|
|
|
96 {
|
|
|
97 return (x.as_micros() as f64) / 1000000.0;
|
|
|
98 }
|
|
|
99
|
|
|
100 impl mpris_server::LocalRootInterface for BeefwebPlayer {
|
|
|
101 async fn raise(&self) -> fdo::Result<()>
|
|
|
102 {
|
|
|
103 /* don't care */
|
|
|
104 return Ok(());
|
|
|
105 }
|
|
|
106
|
|
|
107 async fn quit(&self) -> fdo::Result<()>
|
|
|
108 {
|
|
|
109 /* don't care */
|
|
|
110 return Ok(());
|
|
|
111 }
|
|
|
112
|
|
|
113 async fn can_quit(&self) -> fdo::Result<bool>
|
|
|
114 {
|
|
|
115 /* don't care */
|
|
|
116 return Ok(false);
|
|
|
117 }
|
|
|
118
|
|
|
119 async fn fullscreen(&self) -> fdo::Result<bool>
|
|
|
120 {
|
|
|
121 /* don't care */
|
|
|
122 return Ok(false);
|
|
|
123 }
|
|
|
124
|
|
|
125 async fn set_fullscreen(&self, _fullscreen: bool) -> Result<()>
|
|
|
126 {
|
|
|
127 /* don't care */
|
|
|
128 return Ok(());
|
|
|
129 }
|
|
|
130
|
|
|
131 async fn can_set_fullscreen(&self) -> fdo::Result<bool>
|
|
|
132 {
|
|
|
133 return Ok(false);
|
|
|
134 }
|
|
|
135
|
|
|
136 async fn can_raise(&self) -> fdo::Result<bool>
|
|
|
137 {
|
|
|
138 return Ok(false);
|
|
|
139 }
|
|
|
140
|
|
|
141 async fn has_track_list(&self) -> fdo::Result<bool>
|
|
|
142 {
|
|
|
143 /* ??? */
|
|
|
144 return Ok(false);
|
|
|
145 }
|
|
|
146
|
|
|
147 async fn identity(&self) -> fdo::Result<String>
|
|
|
148 {
|
|
|
149 /* TODO: allow changing this */
|
|
|
150 return Ok("beefweb".into());
|
|
|
151 }
|
|
|
152
|
|
|
153 async fn desktop_entry(&self) -> fdo::Result<String>
|
|
|
154 {
|
|
|
155 return Ok("foobar2000".into());
|
|
|
156 }
|
|
|
157
|
|
|
158 async fn supported_uri_schemes(&self) -> fdo::Result<Vec<String>>
|
|
|
159 {
|
|
|
160 return Ok([].to_vec());
|
|
|
161 }
|
|
|
162
|
|
|
163 async fn supported_mime_types(&self) -> fdo::Result<Vec<String>>
|
|
|
164 {
|
|
|
165 /* needs moar */
|
|
|
166 return Ok([].to_vec());
|
|
|
167 }
|
|
|
168 }
|
|
|
169
|
|
|
170 impl mpris_server::LocalPlayerInterface for BeefwebPlayer {
|
|
|
171 async fn next(&self) -> fdo::Result<()>
|
|
|
172 {
|
|
|
173 self.bw.next().await;
|
|
|
174 return Ok(());
|
|
|
175 }
|
|
|
176
|
|
|
177 async fn previous(&self) -> fdo::Result<()>
|
|
|
178 {
|
|
|
179 self.bw.previous().await;
|
|
|
180 return Ok(());
|
|
|
181 }
|
|
|
182
|
|
|
183 async fn pause(&self) -> fdo::Result<()>
|
|
|
184 {
|
|
|
185 self.bw.pause().await;
|
|
|
186 return Ok(());
|
|
|
187 }
|
|
|
188
|
|
|
189 async fn play_pause(&self) -> fdo::Result<()>
|
|
|
190 {
|
|
|
191 self.bw.play_pause().await;
|
|
|
192 return Ok(());
|
|
|
193 }
|
|
|
194
|
|
|
195 async fn stop(&self) -> fdo::Result<()>
|
|
|
196 {
|
|
|
197 self.bw.stop().await;
|
|
|
198 return Ok(());
|
|
|
199 }
|
|
|
200
|
|
|
201 async fn play(&self) -> fdo::Result<()>
|
|
|
202 {
|
|
|
203 self.bw.play().await;
|
|
|
204 return Ok(());
|
|
|
205 }
|
|
|
206
|
|
|
207 async fn seek(&self, offset: mpris_server::Time) -> fdo::Result<()>
|
|
|
208 {
|
|
4
|
209 let pl = self.bw.seek(time_to_secs(offset)).await;
|
|
0
|
210
|
|
|
211 match pl {
|
|
|
212 Err(_) => return Err(fdo::Error::Failed("uhoh".to_string())),
|
|
|
213 _ => (),
|
|
|
214 };
|
|
|
215
|
|
|
216 return Ok(());
|
|
|
217 }
|
|
|
218
|
|
|
219 async fn position(&self) -> fdo::Result<mpris_server::Time>
|
|
|
220 {
|
|
|
221 let pl = self.bw.player().await;
|
|
|
222
|
|
|
223 match pl {
|
|
|
224 Err(_) => return Err(fdo::Error::Failed("uhoh".to_string())),
|
|
|
225 _ => (),
|
|
|
226 };
|
|
|
227
|
|
|
228 return Ok(secs_to_time(pl.unwrap().active_item.position));
|
|
|
229 }
|
|
|
230
|
|
|
231 async fn playback_status(&self) -> fdo::Result<mpris_server::PlaybackStatus>
|
|
|
232 {
|
|
|
233 let p = self.bw.player().await;
|
|
|
234
|
|
|
235 match p {
|
|
|
236 Err(_) => return Err(fdo::Error::Failed("wtf".to_string())),
|
|
|
237 _ => (),
|
|
|
238 };
|
|
|
239
|
|
2
|
240 return match p.unwrap().playback_state {
|
|
|
241 beefweb::PlaybackState::PLAYING => Ok(mpris_server::PlaybackStatus::Playing),
|
|
|
242 beefweb::PlaybackState::STOPPED => Ok(mpris_server::PlaybackStatus::Stopped),
|
|
|
243 beefweb::PlaybackState::PAUSED => Ok(mpris_server::PlaybackStatus::Paused),
|
|
0
|
244 };
|
|
|
245 }
|
|
|
246
|
|
|
247 async fn loop_status(&self) -> fdo::Result<mpris_server::LoopStatus>
|
|
|
248 {
|
|
2
|
249 /* TODO -- we can do this w/ self.bw.playback_order() */
|
|
0
|
250 return Ok(mpris_server::LoopStatus::None);
|
|
|
251 }
|
|
|
252
|
|
|
253 async fn set_loop_status(&self, loop_status: mpris_server::LoopStatus) -> Result<()>
|
|
|
254 {
|
|
2
|
255 /* TODO -- implement self.bw.set_playback_order() */
|
|
0
|
256 return Ok(());
|
|
|
257 }
|
|
|
258
|
|
|
259 async fn shuffle(&self) -> fdo::Result<bool>
|
|
|
260 {
|
|
2
|
261 /* TODO -- we can do this w/ self.bw.playback_order() */
|
|
0
|
262 println!("Shuffle");
|
|
|
263 return Ok(false);
|
|
|
264 }
|
|
|
265
|
|
|
266 async fn set_shuffle(&self, shuffle: bool) -> Result<()>
|
|
|
267 {
|
|
2
|
268 /* TODO -- implement self.bw.set_playback_order() */
|
|
0
|
269 println!("SetShuffle({shuffle})");
|
|
|
270 return Ok(());
|
|
|
271 }
|
|
|
272
|
|
|
273 async fn metadata(&self) -> fdo::Result<mpris_server::Metadata>
|
|
|
274 {
|
|
|
275 let pl = self.bw.player().await;
|
|
|
276
|
|
|
277 match pl {
|
|
|
278 Err(_) => return Err(fdo::Error::Failed("uhoh".to_string())),
|
|
|
279 _ => (),
|
|
|
280 };
|
|
|
281
|
|
|
282 let p = pl.unwrap();
|
|
|
283
|
|
3
|
284 let playlist_items_result = self.bw.playlist_item(p.active_item.playlist_id.as_str(), p.active_item.index, &["%title%", "%artist%", "%album%", "%discnumber%", "%tracknumber%", "%album artist%", "%path%", "%bpm%", "%composer%", "%comment%", "%date%", "%genre%", "%lyricist%"].to_vec()).await;
|
|
0
|
285
|
|
|
286 match playlist_items_result {
|
|
|
287 Err(_) => return Err(fdo::Error::Failed("uhoh".to_string())),
|
|
|
288 _ => (),
|
|
|
289 };
|
|
|
290
|
|
|
291 let playlist_items = playlist_items_result.unwrap();
|
|
|
292
|
|
|
293 let track = playlist_items.items.get(0).unwrap();
|
|
|
294
|
|
|
295 /*
|
|
|
296 let artwork = self.get_artwork(p.active_item.playlist_id.as_str(), p.active_item.index, track.columns.get(6).unwrap());
|
|
|
297 */
|
|
|
298
|
|
3
|
299 let mut x = mpris_server::Metadata::new();
|
|
|
300
|
|
|
301 x.set_length(Some(secs_to_time(p.active_item.duration)));
|
|
|
302 if track.columns.len() >= 13 {
|
|
|
303 x.set_title(Some(track.columns.get(0).unwrap()));
|
|
|
304 /* XXX musicbrainz has %artists% we can use for a proper list */
|
|
|
305 x.set_artist(Some([track.columns.get(1).unwrap()]));
|
|
|
306 x.set_album(Some(track.columns.get(2).unwrap()));
|
|
|
307 match track.columns.get(3).unwrap().parse::<i32>() {
|
|
|
308 Ok(v) => { x.set_disc_number(Some(v)); },
|
|
|
309 _ => (),
|
|
|
310 };
|
|
|
311 match track.columns.get(4).unwrap().parse::<i32>() {
|
|
|
312 Ok(v) => { x.set_track_number(Some(v)); },
|
|
|
313 _ => (),
|
|
|
314 };
|
|
|
315 x.set_album_artist(Some([track.columns.get(5).unwrap()]));
|
|
4
|
316 x.set_trackid(format!("/org/foobar2000/foobar2000/trackids/{}", hex::encode(track.columns.get(6).unwrap())));
|
|
3
|
317 /* Why is this an i32 ??? It would make more sense as f32 or f64 */
|
|
|
318 match track.columns.get(7).unwrap().parse::<i32>() {
|
|
|
319 Ok(v) => { x.set_audio_bpm(Some(v)) },
|
|
|
320 _ => (),
|
|
|
321 };
|
|
|
322 x.set_composer(Some([track.columns.get(8).unwrap()]));
|
|
|
323 x.set_comment(Some([track.columns.get(9).unwrap()]));
|
|
|
324 x.set_content_created(Some(track.columns.get(10).unwrap()));
|
|
|
325 x.set_genre(Some(track.columns.get(11).unwrap().split(";")));
|
|
|
326 x.set_lyricist(Some([track.columns.get(12).unwrap()]));
|
|
|
327 }
|
|
0
|
328
|
|
|
329 /*
|
|
|
330 return match artwork.await {
|
|
|
331 Ok(x) => Ok(builder.art_url(urlencoding::encode(format!("file://{}", x).as_str())).build()),
|
|
|
332 _ => Ok(builder.build()),
|
|
|
333 };
|
|
|
334 */
|
|
3
|
335 return Ok(x);
|
|
0
|
336 }
|
|
|
337
|
|
|
338 async fn volume(&self) -> fdo::Result<mpris_server::Volume>
|
|
|
339 {
|
|
2
|
340 let vr = self.bw.volume().await;
|
|
|
341
|
|
|
342 match vr {
|
|
|
343 Err(_) => return Err(fdo::Error::Failed("uhoh".to_string())),
|
|
|
344 _ => (),
|
|
|
345 }
|
|
|
346
|
|
|
347 let v = vr.unwrap();
|
|
|
348
|
|
|
349 /* dB -> linear */
|
|
|
350 return Ok(match v.r#type {
|
|
|
351 beefweb::VolumeType::DB => 10.0_f64.powf(v.value / 20.0),
|
|
|
352 beefweb::VolumeType::LINEAR => v.value,
|
|
|
353 beefweb::VolumeType::UPDOWN => /* ??? */ v.value,
|
|
|
354 });
|
|
0
|
355 }
|
|
|
356
|
|
|
357 async fn set_volume(&self, volume: mpris_server::Volume) -> Result<()>
|
|
|
358 {
|
|
2
|
359 /* linear -> dB */
|
|
|
360 let v = 20.0 * volume.log10();
|
|
|
361
|
|
|
362 match self.bw.set_volume(v).await {
|
|
|
363 Err(_) => return Err(zbus::Error::Failure("uhoh".to_string())),
|
|
|
364 _ => (),
|
|
|
365 }
|
|
|
366
|
|
0
|
367 return Ok(());
|
|
|
368 }
|
|
|
369
|
|
|
370 /* "can" functions -- all work */
|
|
|
371
|
|
|
372 async fn can_go_next(&self) -> fdo::Result<bool>
|
|
|
373 {
|
|
|
374 return Ok(true);
|
|
|
375 }
|
|
|
376
|
|
|
377 async fn can_go_previous(&self) -> fdo::Result<bool>
|
|
|
378 {
|
|
|
379 return Ok(true);
|
|
|
380 }
|
|
|
381
|
|
|
382 async fn can_play(&self) -> fdo::Result<bool>
|
|
|
383 {
|
|
|
384 return Ok(true);
|
|
|
385 }
|
|
|
386
|
|
|
387 async fn can_pause(&self) -> fdo::Result<bool>
|
|
|
388 {
|
|
|
389 return Ok(true);
|
|
|
390 }
|
|
|
391
|
|
|
392 async fn can_seek(&self) -> fdo::Result<bool>
|
|
|
393 {
|
|
|
394 return Ok(true);
|
|
|
395 }
|
|
|
396
|
|
|
397 async fn can_control(&self) -> fdo::Result<bool>
|
|
|
398 {
|
|
|
399 return Ok(true);
|
|
|
400 }
|
|
|
401
|
|
|
402 /* --- UNSUPPORTED */
|
|
|
403
|
|
|
404 async fn rate(&self) -> fdo::Result<mpris_server::PlaybackRate>
|
|
|
405 {
|
|
|
406 return Ok(mpris_server::PlaybackRate::default());
|
|
|
407 }
|
|
|
408
|
|
|
409 async fn set_rate(&self, rate: mpris_server::PlaybackRate) -> Result<()>
|
|
|
410 {
|
|
|
411 return Ok(());
|
|
|
412 }
|
|
|
413
|
|
|
414 async fn minimum_rate(&self) -> fdo::Result<mpris_server::PlaybackRate>
|
|
|
415 {
|
|
|
416 return Ok(mpris_server::PlaybackRate::default());
|
|
|
417 }
|
|
|
418
|
|
|
419 async fn maximum_rate(&self) -> fdo::Result<mpris_server::PlaybackRate>
|
|
|
420 {
|
|
|
421 return Ok(mpris_server::PlaybackRate::default());
|
|
|
422 }
|
|
|
423
|
|
4
|
424 /* FIXME to implement this we would have to search through each playlist
|
|
|
425 * for the track, and if it doesn't exist, append it to the current playlist.
|
|
|
426 *
|
|
|
427 * THEN we can add it directly into the play queue. */
|
|
0
|
428 async fn set_position(&self, track_id: mpris_server::TrackId, position: mpris_server::Time) -> fdo::Result<()>
|
|
|
429 {
|
|
|
430 return Ok(());
|
|
|
431 }
|
|
|
432
|
|
4
|
433 /* TODO:
|
|
|
434 *
|
|
|
435 * We can effectively implement this "proper" by detecting a file://
|
|
|
436 * path, URL decoding it, and prepending "Z:". */
|
|
0
|
437 async fn open_uri(&self, uri: String) -> fdo::Result<()>
|
|
|
438 {
|
|
|
439 return Ok(());
|
|
|
440 }
|
|
|
441 }
|
|
4
|
442
|
|
|
443 /* -- unfinished impl, don't mind this
|
|
|
444
|
|
|
445
|
|
|
446 impl mpris_server::LocalPlaylistsInterface for BeefwebPlayer {
|
|
|
447 async fn activate_playlist(&self, playlist_id: mpris_server::PlaylistId) -> fdo::Result<()>
|
|
|
448 {
|
|
|
449 return Err(fdo::Error::Failed("uhoh".to_string()));
|
|
|
450 }
|
|
|
451
|
|
|
452 async fn get_playlists(&self, index: u32, max_count: u32, order: mpris_server::PlaylistOrdering, reverse_order: bool) -> fdo::Result<Vec<mpris_server::Playlist>>
|
|
|
453 {
|
|
|
454 let mut v: Vec<mpris_server::Playlist> = Vec::new();
|
|
|
455
|
|
|
456 let playlists = self.bw.playlists().await;
|
|
|
457
|
|
|
458 match playlists {
|
|
|
459 Ok(ref v) => (),
|
|
|
460 _ => return Err(fdo::Error::Failed("req failed".to_string())),
|
|
|
461 }
|
|
|
462
|
|
|
463 for playlist in playlists.unwrap() {
|
|
|
464
|
|
|
465 }
|
|
|
466
|
|
|
467 return Err(fdo::Error::Failed("uhoh".to_string()));
|
|
|
468 }
|
|
|
469
|
|
|
470 async fn playlist_count(&self) -> fdo::Result<u32>
|
|
|
471 {
|
|
|
472 return Err(fdo::Error::Failed("unimpl".to_string()));
|
|
|
473 }
|
|
|
474
|
|
|
475 async fn orderings(&self) -> fdo::Result<Vec<mpris_server::PlaylistOrdering>>
|
|
|
476 {
|
|
|
477 todo!()
|
|
|
478 }
|
|
|
479
|
|
|
480 async fn active_playlist(&self) -> fdo::Result<Option<mpris_server::Playlist>>
|
|
|
481 {
|
|
|
482 todo!()
|
|
|
483 }
|
|
|
484 }
|
|
|
485 */
|