about summary refs log tree commit diff
path: root/src/module/i3_mode.rs
blob: d02f12da52f471bff8f1ae4110ed73c0489413b2 (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use crate::error::{ Error, Result };
use crate::facility::{ FacilityMap, FacilityType };
use crate::facility::i3::{ EventType, I3 };
use crate::module::{ CreatableModule, Module };

use std::time::Duration;

use async_channel::{ unbounded, Receiver };
use async_executor::StaticExecutor;
use async_task::Task;


pub struct I3ModeModule {
  update_task: Task<()>,
}


impl CreatableModule for I3ModeModule {
  const REQUIRED_FACILITIES: FacilityType = FacilityType::I3;

  async fn new(facilities: &mut FacilityMap,
               executor: &'static StaticExecutor)
    -> Result<I3ModeModule>
  {
    let i3: &mut I3 = facilities.get().ok_or(Error::custom(
            "I3 mode module can't access i3 facility."))?;

    let (sender, receiver) = unbounded();

    if !i3.subscribe(EventType::MODE, sender).await? {
      return Err(Error::custom("Unable to subscribe to i3 mode changes."));
    }

    let update_task = executor.spawn(update_loop(receiver));

    Ok(I3ModeModule { update_task })
  }
}

impl Module for I3ModeModule {
  fn format(&mut self, _facilities: &mut FacilityMap) -> Result<String> {
    Ok("mode goes here".to_string())
  }

  fn sleep_duration(&self) -> Option<Duration> {
    None
  }

  fn is_visible(&self) -> bool {
    true
  }
}


async fn update_loop(receiver: Receiver<(EventType, String)>) {
  loop {
    match receiver.recv().await {
      Ok((_, body)) => {
        println!("it worked??? {}", body);
      }
      Err(error) => {
        Into::<Error>::into(error).log();
      }
    }
  }
}