Compare commits

..

No commits in common. "0a663e97f2c0da5a39332b538800742f6b010566" and "d1eec7399a96d8641dbfdd15a293c23def03475d" have entirely different histories.

View file

@ -3,11 +3,10 @@ extern crate libpulse_binding as pulse;
use pulse::context::{Context, FlagSet as ContextFlagSet};
use pulse::mainloop::threaded::Mainloop;
use pulse::proplist::Proplist;
use pulse::volume;
use regex::Regex;
use std::cell::RefCell;
use std::ops::Deref;
use std::sync::Arc;
use std::rc::Rc;
use std::time;
pub fn run() {
@ -19,11 +18,11 @@ pub fn run() {
)
.unwrap();
let mainloop = Arc::new(RefCell::new(
let mainloop = Rc::new(RefCell::new(
Mainloop::new().expect("Failed to create mainloop"),
));
let context = Arc::new(RefCell::new(
let context = Rc::new(RefCell::new(
Context::new_with_proplist(
mainloop.borrow().deref(),
"picoKontrollerContext",
@ -34,8 +33,8 @@ pub fn run() {
// Context state change callback
{
let ml_ref = Arc::clone(&mainloop);
let context_ref = Arc::clone(&context);
let ml_ref = Rc::clone(&mainloop);
let context_ref = Rc::clone(&context);
context
.borrow_mut()
.set_state_callback(Some(Box::new(move || {
@ -83,21 +82,19 @@ pub fn run() {
mainloop.borrow_mut().unlock();
//---------------------
let ml = Arc::clone(&mainloop);
let ctx = Arc::clone(&context);
set_volume_sink_input(ml, ctx, "Firefox", 100);
let ml = Rc::clone(&mainloop);
let ctx = Rc::clone(&context);
mute_sink_input(ml, ctx, "Firefox", false);
//---------------------
loop {
//TODO: react to incoming commands here instead of infinite sleep
std::thread::sleep(time::Duration::from_secs(u64::MAX));
}
}
pub fn set_default_sink(
mainloop: Arc<RefCell<Mainloop>>,
context: Arc<RefCell<Context>>,
mainloop: Rc<RefCell<Mainloop>>,
context: Rc<RefCell<Context>>,
name: &str,
) {
mainloop.borrow_mut().lock();
@ -120,11 +117,11 @@ pub fn set_default_sink(
mainloop.borrow_mut().unlock();
}
fn for_all_sink_inputs(
mainloop: Arc<RefCell<Mainloop>>,
context: Arc<RefCell<Context>>,
pub fn mute_sink_input(
mainloop: Rc<RefCell<Mainloop>>,
context: Rc<RefCell<Context>>,
name: &str,
function: Arc<dyn Fn(Arc<RefCell<Context>>, u32, &mut volume::ChannelVolumes)>,
state: bool,
) {
mainloop.borrow_mut().lock();
let sink_filter = Regex::new(name).expect("Creating RegEx failed");
@ -134,81 +131,19 @@ fn for_all_sink_inputs(
.introspect()
.get_sink_input_info_list(move |items_result| match items_result {
pulse::callbacks::ListResult::Item(item) => {
let application_name = item
.proplist
.get_str(pulse::proplist::properties::APPLICATION_NAME)
.unwrap();
let application_name = item.proplist.get_str("application.name").unwrap();
let matches_regex = sink_filter.is_match(&application_name);
println!("{:?}", application_name);
if matches_regex {
function(
callback_context.clone(),
item.index,
&mut item.volume.clone(),
);
println!("Setting default sink to: {}", application_name);
callback_context
.borrow_mut()
.introspect()
.set_sink_input_mute(item.index, state, Option::None);
}
}
_ => {}
});
mainloop.borrow_mut().unlock();
}
pub fn mute_sink_input(
mainloop: Arc<RefCell<Mainloop>>,
context: Arc<RefCell<Context>>,
name: &str,
state: bool,
) {
for_all_sink_inputs(
mainloop,
context,
name,
Arc::new(move |ctx, index, _| {
ctx.borrow_mut()
.introspect()
.set_sink_input_mute(index, state, Option::None);
}),
)
}
pub fn set_volume_sink_input(
mainloop: Arc<RefCell<Mainloop>>,
context: Arc<RefCell<Context>>,
name: &str,
volume: i16,
) {
for_all_sink_inputs(
mainloop,
context,
name,
Arc::new(move |ctx, index, vols| {
let target = percent_to_volume(volume);
for v in vols.get_mut() {
v.0 = target;
}
ctx.borrow_mut()
.introspect()
.set_sink_input_volume(index, vols, Option::None);
}),
)
}
//taken from https://github.com/jantap/rsmixer
pub fn percent_to_volume(target_percent: i16) -> u32 {
let base_delta = (volume::Volume::NORMAL.0 as f32 - volume::Volume::MUTED.0 as f32) / 100.0;
if target_percent < 0 {
volume::Volume::MUTED.0
} else if target_percent == 100 {
volume::Volume::NORMAL.0
} else if target_percent >= 150 {
(volume::Volume::NORMAL.0 as f32 * 1.5) as u32
} else if target_percent < 100 {
volume::Volume::MUTED.0 + target_percent as u32 * base_delta as u32
} else {
volume::Volume::NORMAL.0 + (target_percent - 100) as u32 * base_delta as u32
}
}