1use common;
4use reply;
5use serde_json as json;
6use std::str::FromStr;
7
8use event::inner::*;
9
10#[derive(Debug)]
12pub enum Event {
13 WorkspaceEvent(WorkspaceEventInfo),
14 OutputEvent(OutputEventInfo),
15 ModeEvent(ModeEventInfo),
16 WindowEvent(WindowEventInfo),
17 BarConfigEvent(BarConfigEventInfo),
18 BindingEvent(BindingEventInfo),
19
20 #[cfg(feature = "i3-4-14")]
21 #[cfg_attr(feature = "dox", doc(cfg(feature = "i3-4-14")))]
22 ShutdownEvent(ShutdownEventInfo),
23}
24
25#[derive(Debug)]
27pub struct WorkspaceEventInfo {
28 pub change: WorkspaceChange,
30 pub current: Option<reply::Node>,
32 pub old: Option<reply::Node>,
36}
37
38impl FromStr for WorkspaceEventInfo {
39 type Err = json::error::Error;
40 fn from_str(s: &str) -> Result<Self, Self::Err> {
41 let val: json::Value = try!(json::from_str(s));
42 Ok(WorkspaceEventInfo {
43 change: match val.get("change").unwrap().as_str().unwrap() {
44 "focus" => WorkspaceChange::Focus,
45 "init" => WorkspaceChange::Init,
46 "empty" => WorkspaceChange::Empty,
47 "urgent" => WorkspaceChange::Urgent,
48 "rename" => WorkspaceChange::Rename,
49 "reload" => WorkspaceChange::Reload,
50 "move" => WorkspaceChange::Move,
51 "restored" => WorkspaceChange::Restored,
52 other => {
53 warn!(target: "i3ipc", "Unknown WorkspaceChange {}", other);
54 WorkspaceChange::Unknown
55 }
56 },
57 current: match val.get("current").unwrap().clone() {
58 json::Value::Null => None,
59 val => Some(common::build_tree(&val)),
60 },
61 old: match val.get("old") {
62 Some(o) => match o.clone() {
63 json::Value::Null => None,
64 val => Some(common::build_tree(&val)),
65 },
66 None => None,
67 },
68 })
69 }
70}
71
72#[derive(Debug)]
74pub struct OutputEventInfo {
75 pub change: OutputChange,
77}
78
79impl FromStr for OutputEventInfo {
80 type Err = json::error::Error;
81 fn from_str(s: &str) -> Result<Self, Self::Err> {
82 let val: json::Value = try!(json::from_str(s));
83 Ok(OutputEventInfo {
84 change: match val.get("change").unwrap().as_str().unwrap() {
85 "unspecified" => OutputChange::Unspecified,
86 other => {
87 warn!(target: "i3ipc", "Unknown OutputChange {}", other);
88 OutputChange::Unknown
89 }
90 },
91 })
92 }
93}
94
95#[derive(Debug)]
97pub struct ModeEventInfo {
98 pub change: String,
101}
102
103impl FromStr for ModeEventInfo {
104 type Err = json::error::Error;
105 fn from_str(s: &str) -> Result<Self, Self::Err> {
106 let val: json::Value = try!(json::from_str(s));
107 Ok(ModeEventInfo {
108 change: val.get("change").unwrap().as_str().unwrap().to_owned(),
109 })
110 }
111}
112
113#[derive(Debug)]
115pub struct WindowEventInfo {
116 pub change: WindowChange,
118 pub container: reply::Node,
122}
123
124impl FromStr for WindowEventInfo {
125 type Err = json::error::Error;
126 fn from_str(s: &str) -> Result<Self, Self::Err> {
127 let val: json::Value = try!(json::from_str(s));
128 Ok(WindowEventInfo {
129 change: match val.get("change").unwrap().as_str().unwrap() {
130 "new" => WindowChange::New,
131 "close" => WindowChange::Close,
132 "focus" => WindowChange::Focus,
133 "title" => WindowChange::Title,
134 "fullscreen_mode" => WindowChange::FullscreenMode,
135 "move" => WindowChange::Move,
136 "floating" => WindowChange::Floating,
137 "urgent" => WindowChange::Urgent,
138
139 #[cfg(feature = "i3-4-13")]
140 "mark" => WindowChange::Mark,
141
142 other => {
143 warn!(target: "i3ipc", "Unknown WindowChange {}", other);
144 WindowChange::Unknown
145 }
146 },
147 container: common::build_tree(val.get("container").unwrap()),
148 })
149 }
150}
151
152#[derive(Debug)]
154pub struct BarConfigEventInfo {
155 pub bar_config: reply::BarConfig,
157}
158
159impl FromStr for BarConfigEventInfo {
160 type Err = json::error::Error;
161 fn from_str(s: &str) -> Result<Self, Self::Err> {
162 let val: json::Value = try!(json::from_str(s));
163 Ok(BarConfigEventInfo {
164 bar_config: common::build_bar_config(&val),
165 })
166 }
167}
168
169#[derive(Debug)]
173pub struct BindingEventInfo {
174 pub change: BindingChange,
177 pub binding: Binding,
178}
179
180impl FromStr for BindingEventInfo {
181 type Err = json::error::Error;
182 fn from_str(s: &str) -> Result<Self, Self::Err> {
183 let val: json::Value = try!(json::from_str(s));
184 let bind = val.get("binding").unwrap();
185 Ok(BindingEventInfo {
186 change: match val.get("change").unwrap().as_str().unwrap() {
187 "run" => BindingChange::Run,
188 other => {
189 warn!(target: "i3ipc", "Unknown BindingChange {}", other);
190 BindingChange::Unknown
191 }
192 },
193 binding: Binding {
194 command: bind.get("command").unwrap().as_str().unwrap().to_owned(),
195 event_state_mask: bind
196 .get("event_state_mask")
197 .unwrap()
198 .as_array()
199 .unwrap()
200 .iter()
201 .map(|m| m.as_str().unwrap().to_owned())
202 .collect(),
203 input_code: bind.get("input_code").unwrap().as_i64().unwrap() as i32,
204 symbol: match bind.get("symbol").unwrap().clone() {
205 json::Value::String(s) => Some(s),
206 json::Value::Null => None,
207 _ => unreachable!(),
208 },
209 input_type: match bind.get("input_type").unwrap().as_str().unwrap() {
210 "keyboard" => InputType::Keyboard,
211 "mouse" => InputType::Mouse,
212 other => {
213 warn!(target: "i3ipc", "Unknown InputType {}", other);
214 InputType::Unknown
215 }
216 },
217 },
218 })
219 }
220}
221
222#[derive(Debug)]
224#[cfg(feature = "i3-4-14")]
225#[cfg_attr(feature = "dox", doc(cfg(feature = "i3-4-14")))]
226pub struct ShutdownEventInfo {
227 pub change: ShutdownChange,
228}
229
230#[cfg(feature = "i3-4-14")]
231#[cfg_attr(feature = "dox", doc(cfg(feature = "i3-4-14")))]
232impl FromStr for ShutdownEventInfo {
233 type Err = json::error::Error;
234 fn from_str(s: &str) -> Result<Self, Self::Err> {
235 let val: json::Value = try!(json::from_str(s));
236 let change = match val.get("change").unwrap().as_str().unwrap() {
237 "restart" => ShutdownChange::Restart,
238 "exit" => ShutdownChange::Exit,
239 other => {
240 warn!(target: "i3ipc", "Unknown ShutdownChange {}", other);
241 ShutdownChange::Unknown
242 }
243 };
244 Ok(ShutdownEventInfo { change })
245 }
246}
247
248pub mod inner {
250 #[derive(Debug, PartialEq)]
252 pub enum WorkspaceChange {
253 Focus,
254 Init,
255 Empty,
256 Urgent,
257 Rename,
258 Reload,
259 Restored,
260 Move,
261 Unknown,
263 }
264
265 #[derive(Debug, PartialEq)]
267 pub enum OutputChange {
268 Unspecified,
269 Unknown,
271 }
272
273 #[derive(Debug, PartialEq)]
275 pub enum WindowChange {
276 New,
278 Close,
280 Focus,
282 Title,
284 FullscreenMode,
286 Move,
288 Floating,
290 Urgent,
292
293 #[cfg(feature = "i3-4-13")]
295 #[cfg_attr(feature = "dox", doc(cfg(feature = "i3-4-13")))]
296 Mark,
297
298 Unknown,
300 }
301
302 #[derive(Debug, PartialEq)]
304 pub enum InputType {
305 Keyboard,
306 Mouse,
307 Unknown,
309 }
310
311 #[derive(Debug, PartialEq)]
313 pub struct Binding {
314 pub command: String,
316
317 pub event_state_mask: Vec<String>,
319
320 pub input_code: i32,
324
325 pub symbol: Option<String>,
328
329 pub input_type: InputType,
331 }
332
333 #[derive(Debug, PartialEq)]
335 pub enum BindingChange {
336 Run,
337 Unknown,
339 }
340
341 #[derive(Debug, PartialEq)]
343 #[cfg(feature = "i3-4-14")]
344 #[cfg_attr(feature = "dox", doc(cfg(feature = "i3-4-14")))]
345 pub enum ShutdownChange {
346 Restart,
347 Exit,
348 Unknown,
350 }
351}