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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};
use crossbeam_channel::Sender;
use dbus::blocking::LocalConnection;
use dbus::strings::Signature;
use dbus::tree::Factory;
use serde_derive::Deserialize;
use crate::blocks::{Block, ConfigBlock, Update};
use crate::config::SharedConfig;
use crate::errors::*;
use crate::scheduler::Task;
use crate::widgets::text::TextWidget;
use crate::widgets::{I3BarWidget, State};
#[derive(Clone)]
struct CustomDBusStatus {
content: String,
icon: String,
state: State,
}
pub struct CustomDBus {
id: usize,
text: TextWidget,
status: Arc<Mutex<CustomDBusStatus>>,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct CustomDBusConfig {
pub name: String,
}
impl ConfigBlock for CustomDBus {
type Config = CustomDBusConfig;
fn new(
id: usize,
block_config: Self::Config,
shared_config: SharedConfig,
send: Sender<Task>,
) -> Result<Self> {
let status_original = Arc::new(Mutex::new(CustomDBusStatus {
content: String::from("??"),
icon: String::from(""),
state: State::Idle,
}));
let status = status_original.clone();
let name = block_config.name;
thread::Builder::new()
.name("custom_dbus".into())
.spawn(move || {
let c = LocalConnection::new_session()
.expect("Failed to establish DBus connection in thread");
c.request_name("i3.status.rs", false, true, false)
.expect("Failed to request bus name");
let f = Factory::new_fn::<()>();
let tree = f
.tree(())
.add(
f.object_path(format!("/{}", name), ())
.introspectable()
.add(
f.interface("i3.status.rs", ()).add_m(
f.method("SetStatus", (), move |m| {
let args = m.msg.get3::<&str, &str, &str>();
let mut status = status_original.lock().unwrap();
if let Some(new_content) = args.0 {
status.content = String::from(new_content);
}
if let Some(new_icon) = args.1 {
status.icon = String::from(new_icon);
}
if let Some(new_state) = args.2 {
status.state =
State::from_str(new_state).unwrap_or(status.state);
}
send.send(Task {
id,
update_time: Instant::now(),
})
.unwrap();
Ok(vec![m.msg.method_return()])
})
.in_args(vec![
("name", Signature::make::<&str>()),
("icon", Signature::make::<&str>()),
("state", Signature::make::<&str>()),
]),
),
),
)
.add(f.object_path("/", ()).introspectable());
tree.start_receive(&c);
loop {
c.process(Duration::from_millis(1000)).unwrap();
}
})
.unwrap();
let text = TextWidget::new(id, 0, shared_config).with_text("CustomDBus");
Ok(CustomDBus { id, text, status })
}
}
impl Block for CustomDBus {
fn id(&self) -> usize {
self.id
}
fn update(&mut self) -> Result<Option<Update>> {
let status = (*self
.status
.lock()
.block_error("custom_dbus", "failed to acquire lock")?)
.clone();
self.text.set_text(status.content);
self.text.set_icon(&status.icon)?;
self.text.set_state(status.state);
Ok(None)
}
fn view(&self) -> Vec<&dyn I3BarWidget> {
vec![&self.text]
}
}