|
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 {
|
|
|
209 let pl = self.bw.set_position(time_to_secs(offset)).await;
|
|
|
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
|
|
|
284 let playlist_items_result = self.bw.playlist_items(p.active_item.playlist_id.as_str(), p.active_item.index, 1, ["%title%", "%artist%", "%album%", "%discnumber%", "%tracknumber%", "%album artist%", "%path%"].to_vec()).await;
|
|
|
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
|
|
|
299 let builder = mpris_server::Metadata::builder()
|
|
|
300 .length(secs_to_time(p.active_item.duration))
|
|
|
301 .album(track.columns.get(2).unwrap())
|
|
|
302 .artist([track.columns.get(1).unwrap()])
|
|
|
303 .disc_number(track.columns.get(3).unwrap().parse::<i32>().unwrap())
|
|
|
304 .track_number(track.columns.get(4).unwrap().parse::<i32>().unwrap())
|
|
|
305 .title(track.columns.get(0).unwrap())
|
|
|
306 .album_artist([track.columns.get(5).unwrap()]);
|
|
|
307
|
|
|
308 /*
|
|
|
309 return match artwork.await {
|
|
|
310 Ok(x) => Ok(builder.art_url(urlencoding::encode(format!("file://{}", x).as_str())).build()),
|
|
|
311 _ => Ok(builder.build()),
|
|
|
312 };
|
|
|
313 */
|
|
|
314 return Ok(builder.build());
|
|
|
315 }
|
|
|
316
|
|
|
317 async fn volume(&self) -> fdo::Result<mpris_server::Volume>
|
|
|
318 {
|
|
2
|
319 let vr = self.bw.volume().await;
|
|
|
320
|
|
|
321 match vr {
|
|
|
322 Err(_) => return Err(fdo::Error::Failed("uhoh".to_string())),
|
|
|
323 _ => (),
|
|
|
324 }
|
|
|
325
|
|
|
326 let v = vr.unwrap();
|
|
|
327
|
|
|
328 /* dB -> linear */
|
|
|
329 return Ok(match v.r#type {
|
|
|
330 beefweb::VolumeType::DB => 10.0_f64.powf(v.value / 20.0),
|
|
|
331 beefweb::VolumeType::LINEAR => v.value,
|
|
|
332 beefweb::VolumeType::UPDOWN => /* ??? */ v.value,
|
|
|
333 });
|
|
0
|
334 }
|
|
|
335
|
|
|
336 async fn set_volume(&self, volume: mpris_server::Volume) -> Result<()>
|
|
|
337 {
|
|
2
|
338 /* linear -> dB */
|
|
|
339 let v = 20.0 * volume.log10();
|
|
|
340
|
|
|
341 match self.bw.set_volume(v).await {
|
|
|
342 Err(_) => return Err(zbus::Error::Failure("uhoh".to_string())),
|
|
|
343 _ => (),
|
|
|
344 }
|
|
|
345
|
|
0
|
346 return Ok(());
|
|
|
347 }
|
|
|
348
|
|
|
349 /* "can" functions -- all work */
|
|
|
350
|
|
|
351 async fn can_go_next(&self) -> fdo::Result<bool>
|
|
|
352 {
|
|
|
353 return Ok(true);
|
|
|
354 }
|
|
|
355
|
|
|
356 async fn can_go_previous(&self) -> fdo::Result<bool>
|
|
|
357 {
|
|
|
358 return Ok(true);
|
|
|
359 }
|
|
|
360
|
|
|
361 async fn can_play(&self) -> fdo::Result<bool>
|
|
|
362 {
|
|
|
363 return Ok(true);
|
|
|
364 }
|
|
|
365
|
|
|
366 async fn can_pause(&self) -> fdo::Result<bool>
|
|
|
367 {
|
|
|
368 return Ok(true);
|
|
|
369 }
|
|
|
370
|
|
|
371 async fn can_seek(&self) -> fdo::Result<bool>
|
|
|
372 {
|
|
|
373 return Ok(true);
|
|
|
374 }
|
|
|
375
|
|
|
376 async fn can_control(&self) -> fdo::Result<bool>
|
|
|
377 {
|
|
|
378 return Ok(true);
|
|
|
379 }
|
|
|
380
|
|
|
381 /* --- UNSUPPORTED */
|
|
|
382
|
|
|
383 async fn rate(&self) -> fdo::Result<mpris_server::PlaybackRate>
|
|
|
384 {
|
|
|
385 return Ok(mpris_server::PlaybackRate::default());
|
|
|
386 }
|
|
|
387
|
|
|
388 async fn set_rate(&self, rate: mpris_server::PlaybackRate) -> Result<()>
|
|
|
389 {
|
|
|
390 return Ok(());
|
|
|
391 }
|
|
|
392
|
|
|
393 async fn minimum_rate(&self) -> fdo::Result<mpris_server::PlaybackRate>
|
|
|
394 {
|
|
|
395 return Ok(mpris_server::PlaybackRate::default());
|
|
|
396 }
|
|
|
397
|
|
|
398 async fn maximum_rate(&self) -> fdo::Result<mpris_server::PlaybackRate>
|
|
|
399 {
|
|
|
400 return Ok(mpris_server::PlaybackRate::default());
|
|
|
401 }
|
|
|
402
|
|
|
403 /* WTF is this supposed to do? --paper */
|
|
|
404 async fn set_position(&self, track_id: mpris_server::TrackId, position: mpris_server::Time) -> fdo::Result<()>
|
|
|
405 {
|
|
|
406 return Ok(());
|
|
|
407 }
|
|
|
408
|
|
|
409 /* Beefweb doesn't really have this, and it would be
|
|
|
410 * pointless anyway, unless we used winepath */
|
|
|
411 async fn open_uri(&self, uri: String) -> fdo::Result<()>
|
|
|
412 {
|
|
|
413 return Ok(());
|
|
|
414 }
|
|
|
415 }
|