aboutsummaryrefslogtreecommitdiff
path: root/pulse/src/communication.rs
blob: 51456d93bfc0474b139036786deaed990f968b28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use std::sync::mpsc::*;
use crate::runnable::pulse::*;
use swaystatus_plugin::*;

pub enum MessagesFromMain {
    Quit,
    Refresh
}

pub struct SenderForMain {
    sender : Sender<MessagesFromMain>,
    pulse_waker : Option<PulseWakeUp>
}

impl<'p> SenderForMain {
    pub fn new(sender : Sender<MessagesFromMain>, pulse_waker : Option<PulseWakeUp>) -> Self {
        SenderForMain{
            sender,
            pulse_waker
        }
    }

    fn send(&self, message : MessagesFromMain) -> Result<(), PluginCommunicationError> {
        if let Ok(_) = self.sender.send(message) {
            //The cool thing about pulse using poll() is that poll() also wakes up if started after
            //the actual wake up call. So no need to worry about races, this is inherently sane!
            self.pulse_waker.as_ref().ok_or(PluginCommunicationError {})?.wake_up()?;
            Ok(())
        }
        else {
            Err(PluginCommunicationError{})
        }
    }
}

impl MsgMainToModule for SenderForMain {
    fn send_quit(&self) -> Result<(), PluginCommunicationError> {
        self.send(MessagesFromMain::Quit)
    }
    fn send_refresh(&self) -> Result<(), PluginCommunicationError> {
        self.send(MessagesFromMain::Refresh)
    }
}

impl From<PulseWakeUpError> for PluginCommunicationError {
    fn from(_error : PulseWakeUpError) -> Self {
        PluginCommunicationError {}
    }
}