|
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;
|
|
|
26 use std::io::Write;
|
|
|
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
|
|
|
240 return match p.unwrap().playback_state.as_str() {
|
|
|
241 "playing" => Ok(mpris_server::PlaybackStatus::Playing),
|
|
|
242 "stopped" => Ok(mpris_server::PlaybackStatus::Stopped),
|
|
|
243 "paused" => Ok(mpris_server::PlaybackStatus::Paused),
|
|
|
244 _ => Err(fdo::Error::Failed("deez nuts".to_string())),
|
|
|
245 };
|
|
|
246 }
|
|
|
247
|
|
|
248 async fn loop_status(&self) -> fdo::Result<mpris_server::LoopStatus>
|
|
|
249 {
|
|
|
250 return Ok(mpris_server::LoopStatus::None);
|
|
|
251 }
|
|
|
252
|
|
|
253 async fn set_loop_status(&self, loop_status: mpris_server::LoopStatus) -> Result<()>
|
|
|
254 {
|
|
|
255 return Ok(());
|
|
|
256 }
|
|
|
257
|
|
|
258 async fn shuffle(&self) -> fdo::Result<bool>
|
|
|
259 {
|
|
|
260 /* TODO */
|
|
|
261 println!("Shuffle");
|
|
|
262 return Ok(false);
|
|
|
263 }
|
|
|
264
|
|
|
265 async fn set_shuffle(&self, shuffle: bool) -> Result<()>
|
|
|
266 {
|
|
|
267 /* TODO */
|
|
|
268 println!("SetShuffle({shuffle})");
|
|
|
269 return Ok(());
|
|
|
270 }
|
|
|
271
|
|
|
272 async fn metadata(&self) -> fdo::Result<mpris_server::Metadata>
|
|
|
273 {
|
|
|
274 let pl = self.bw.player().await;
|
|
|
275
|
|
|
276 match pl {
|
|
|
277 Err(_) => return Err(fdo::Error::Failed("uhoh".to_string())),
|
|
|
278 _ => (),
|
|
|
279 };
|
|
|
280
|
|
|
281 let p = pl.unwrap();
|
|
|
282
|
|
|
283 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;
|
|
|
284
|
|
|
285 match playlist_items_result {
|
|
|
286 Err(_) => return Err(fdo::Error::Failed("uhoh".to_string())),
|
|
|
287 _ => (),
|
|
|
288 };
|
|
|
289
|
|
|
290 let playlist_items = playlist_items_result.unwrap();
|
|
|
291
|
|
|
292 let track = playlist_items.items.get(0).unwrap();
|
|
|
293
|
|
|
294 /*
|
|
|
295 let artwork = self.get_artwork(p.active_item.playlist_id.as_str(), p.active_item.index, track.columns.get(6).unwrap());
|
|
|
296 */
|
|
|
297
|
|
|
298 let builder = mpris_server::Metadata::builder()
|
|
|
299 .length(secs_to_time(p.active_item.duration))
|
|
|
300 .album(track.columns.get(2).unwrap())
|
|
|
301 .artist([track.columns.get(1).unwrap()])
|
|
|
302 .disc_number(track.columns.get(3).unwrap().parse::<i32>().unwrap())
|
|
|
303 .track_number(track.columns.get(4).unwrap().parse::<i32>().unwrap())
|
|
|
304 .title(track.columns.get(0).unwrap())
|
|
|
305 .album_artist([track.columns.get(5).unwrap()]);
|
|
|
306
|
|
|
307 /*
|
|
|
308 return match artwork.await {
|
|
|
309 Ok(x) => Ok(builder.art_url(urlencoding::encode(format!("file://{}", x).as_str())).build()),
|
|
|
310 _ => Ok(builder.build()),
|
|
|
311 };
|
|
|
312 */
|
|
|
313 return Ok(builder.build());
|
|
|
314 }
|
|
|
315
|
|
|
316 async fn volume(&self) -> fdo::Result<mpris_server::Volume>
|
|
|
317 {
|
|
|
318 return Ok(mpris_server::Volume::default());
|
|
|
319 }
|
|
|
320
|
|
|
321 async fn set_volume(&self, volume: mpris_server::Volume) -> Result<()>
|
|
|
322 {
|
|
|
323 return Ok(());
|
|
|
324 }
|
|
|
325
|
|
|
326 /* "can" functions -- all work */
|
|
|
327
|
|
|
328 async fn can_go_next(&self) -> fdo::Result<bool>
|
|
|
329 {
|
|
|
330 return Ok(true);
|
|
|
331 }
|
|
|
332
|
|
|
333 async fn can_go_previous(&self) -> fdo::Result<bool>
|
|
|
334 {
|
|
|
335 return Ok(true);
|
|
|
336 }
|
|
|
337
|
|
|
338 async fn can_play(&self) -> fdo::Result<bool>
|
|
|
339 {
|
|
|
340 return Ok(true);
|
|
|
341 }
|
|
|
342
|
|
|
343 async fn can_pause(&self) -> fdo::Result<bool>
|
|
|
344 {
|
|
|
345 return Ok(true);
|
|
|
346 }
|
|
|
347
|
|
|
348 async fn can_seek(&self) -> fdo::Result<bool>
|
|
|
349 {
|
|
|
350 return Ok(true);
|
|
|
351 }
|
|
|
352
|
|
|
353 async fn can_control(&self) -> fdo::Result<bool>
|
|
|
354 {
|
|
|
355 return Ok(true);
|
|
|
356 }
|
|
|
357
|
|
|
358 /* --- UNSUPPORTED */
|
|
|
359
|
|
|
360 async fn rate(&self) -> fdo::Result<mpris_server::PlaybackRate>
|
|
|
361 {
|
|
|
362 return Ok(mpris_server::PlaybackRate::default());
|
|
|
363 }
|
|
|
364
|
|
|
365 async fn set_rate(&self, rate: mpris_server::PlaybackRate) -> Result<()>
|
|
|
366 {
|
|
|
367 return Ok(());
|
|
|
368 }
|
|
|
369
|
|
|
370 async fn minimum_rate(&self) -> fdo::Result<mpris_server::PlaybackRate>
|
|
|
371 {
|
|
|
372 return Ok(mpris_server::PlaybackRate::default());
|
|
|
373 }
|
|
|
374
|
|
|
375 async fn maximum_rate(&self) -> fdo::Result<mpris_server::PlaybackRate>
|
|
|
376 {
|
|
|
377 return Ok(mpris_server::PlaybackRate::default());
|
|
|
378 }
|
|
|
379
|
|
|
380 /* WTF is this supposed to do? --paper */
|
|
|
381 async fn set_position(&self, track_id: mpris_server::TrackId, position: mpris_server::Time) -> fdo::Result<()>
|
|
|
382 {
|
|
|
383 return Ok(());
|
|
|
384 }
|
|
|
385
|
|
|
386 /* Beefweb doesn't really have this, and it would be
|
|
|
387 * pointless anyway, unless we used winepath */
|
|
|
388 async fn open_uri(&self, uri: String) -> fdo::Result<()>
|
|
|
389 {
|
|
|
390 return Ok(());
|
|
|
391 }
|
|
|
392 }
|