extern crate libc;
extern crate projectm_sys as ffi;
use rand::Rng;
use std::ffi::CString;
use crate::core::ProjectM;
pub struct Playlist {
playlist: *mut ffi::projectm_playlist,
rng: rand::rngs::ThreadRng,
}
impl Playlist {
pub fn create(projectm: &ProjectM) -> Playlist {
let projectm = projectm.get_instance();
let instance = projectm.borrow_mut();
let playlist;
unsafe {
playlist = ffi::projectm_playlist_create(*instance);
}
Playlist {
playlist,
rng: rand::thread_rng(),
}
}
pub fn len(&self) -> u32 {
unsafe { ffi::projectm_playlist_size(self.playlist) }
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn add_path(&self, path: &str, recursive: bool) {
unsafe {
let c_path = CString::new(path).unwrap();
ffi::projectm_playlist_add_path(self.playlist, c_path.as_ptr(), recursive, false);
}
}
pub fn play_next(&mut self) {
unsafe {
ffi::projectm_playlist_play_next(self.playlist, true);
}
}
pub fn play_prev(&mut self) {
unsafe {
ffi::projectm_playlist_play_previous(self.playlist, true);
}
}
pub fn play_random(&mut self) {
let len = self.len();
let index: u32 = self.rng.gen_range(0..len);
unsafe {
ffi::projectm_playlist_set_position(self.playlist, index, true);
}
}
pub fn set_shuffle(&self, shuffle: bool) {
unsafe {
ffi::projectm_playlist_set_shuffle(self.playlist, shuffle);
}
}
pub fn get_shuffle(&self) -> bool {
unsafe { ffi::projectm_playlist_get_shuffle(self.playlist) }
}
}
unsafe impl Send for Playlist {}
unsafe impl Sync for Playlist {}