pulseaudio: Clean up locking a bit

This commit is contained in:
Kenny Levinsen
2019-08-05 23:15:36 +02:00
parent 840c8496ab
commit fbc1f058ad
+18 -24
View File
@@ -399,39 +399,37 @@ impl PulseAudioSoundDevice {
pub struct PulseAudio { pub struct PulseAudio {
device: Arc<Mutex<PulseAudioSoundDevice>>, device: Arc<Mutex<PulseAudioSoundDevice>>,
dirty: bool, dirty: Arc<Mutex<bool>>,
} }
impl PulseAudio { impl PulseAudio {
pub fn new(listener: Sender<bool>) -> Result<Arc<Mutex<PulseAudio>>, ::std::io::Error> { pub fn new(listener: Sender<bool>) -> Result<PulseAudio, ::std::io::Error> {
let (tx, rx) = channel(); let (tx, rx) = channel();
let device = PulseAudioSoundDevice::new(tx)?; let pa = PulseAudio {
let d = device.clone(); device: PulseAudioSoundDevice::new(tx)?,
let pa = Arc::new(Mutex::new(PulseAudio { dirty: Arc::new(Mutex::new(true)),
device: device, };
dirty: true, let device = pa.device.clone();
})); let dirty = pa.dirty.clone();
let p = pa.clone();
std::thread::spawn(move || loop { std::thread::spawn(move || loop {
rx.recv().unwrap(); rx.recv().unwrap();
d.lock().unwrap().get_info().unwrap(); device.lock().unwrap().get_info().unwrap();
p.lock().unwrap().dirty = true; *(dirty.lock().unwrap()) = true;
listener.send(true).unwrap(); listener.send(true).unwrap();
}); });
Ok(pa) Ok(pa)
} }
} }
impl ModuleImpl for Arc<Mutex<PulseAudio>> { impl ModuleImpl for PulseAudio {
fn draw( fn draw(
&self, &self,
buf: &mut Buffer, buf: &mut Buffer,
bg: &Color, bg: &Color,
_time: &DateTime<Local>, _time: &DateTime<Local>,
) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> { ) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> {
let s = self.lock().unwrap(); let muted = self.device.lock().unwrap().muted;
let muted = s.device.lock().unwrap().muted; let mut vol = self.device.lock().unwrap().volume();
let mut vol = s.device.lock().unwrap().volume();
buf.memset(bg); buf.memset(bg);
let c = if muted { let c = if muted {
Color::new(1.0, 1.0, 0.0, 1.0) Color::new(1.0, 1.0, 0.0, 1.0)
@@ -459,9 +457,9 @@ impl ModuleImpl for Arc<Mutex<PulseAudio>> {
} }
fn update(&mut self, _time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> { fn update(&mut self, _time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> {
let mut s = self.lock().unwrap(); let mut d = self.dirty.lock().unwrap();
if s.dirty || force { if *d || force {
s.dirty = false; *d = false;
Ok(true) Ok(true)
} else { } else {
Ok(false) Ok(false)
@@ -475,9 +473,7 @@ impl ModuleImpl for Arc<Mutex<PulseAudio>> {
x: _x, x: _x,
y, y,
} => { } => {
self.lock() self.device
.unwrap()
.device
.lock() .lock()
.unwrap() .unwrap()
.set_volume(y as f32 / 800.0) .set_volume(y as f32 / 800.0)
@@ -485,9 +481,7 @@ impl ModuleImpl for Arc<Mutex<PulseAudio>> {
} }
Input::Click { pos: _pos, button } => match button { Input::Click { pos: _pos, button } => match button {
273 => { 273 => {
self.lock() self.device
.unwrap()
.device
.lock() .lock()
.unwrap() .unwrap()
.toggle() .toggle()