use common;
use reply;
use serde_json as json;
use std::str::FromStr;
use event::inner::*;
#[derive(Debug)]
pub enum Event {
WorkspaceEvent(WorkspaceEventInfo),
OutputEvent(OutputEventInfo),
ModeEvent(ModeEventInfo),
WindowEvent(WindowEventInfo),
BarConfigEvent(BarConfigEventInfo),
BindingEvent(BindingEventInfo),
#[cfg(feature = "i3-4-14")]
#[cfg_attr(feature = "dox", doc(cfg(feature = "i3-4-14")))]
ShutdownEvent(ShutdownEventInfo),
}
#[derive(Debug)]
pub struct WorkspaceEventInfo {
pub change: WorkspaceChange,
pub current: Option<reply::Node>,
pub old: Option<reply::Node>,
}
impl FromStr for WorkspaceEventInfo {
type Err = json::error::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let val: json::Value = try!(json::from_str(s));
Ok(WorkspaceEventInfo {
change: match val.get("change").unwrap().as_str().unwrap() {
"focus" => WorkspaceChange::Focus,
"init" => WorkspaceChange::Init,
"empty" => WorkspaceChange::Empty,
"urgent" => WorkspaceChange::Urgent,
"rename" => WorkspaceChange::Rename,
"reload" => WorkspaceChange::Reload,
"move" => WorkspaceChange::Move,
"restored" => WorkspaceChange::Restored,
other => {
warn!(target: "i3ipc", "Unknown WorkspaceChange {}", other);
WorkspaceChange::Unknown
}
},
current: match val.get("current").unwrap().clone() {
json::Value::Null => None,
val => Some(common::build_tree(&val)),
},
old: match val.get("old") {
Some(o) => match o.clone() {
json::Value::Null => None,
val => Some(common::build_tree(&val)),
},
None => None,
},
})
}
}
#[derive(Debug)]
pub struct OutputEventInfo {
pub change: OutputChange,
}
impl FromStr for OutputEventInfo {
type Err = json::error::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let val: json::Value = try!(json::from_str(s));
Ok(OutputEventInfo {
change: match val.get("change").unwrap().as_str().unwrap() {
"unspecified" => OutputChange::Unspecified,
other => {
warn!(target: "i3ipc", "Unknown OutputChange {}", other);
OutputChange::Unknown
}
},
})
}
}
#[derive(Debug)]
pub struct ModeEventInfo {
pub change: String,
}
impl FromStr for ModeEventInfo {
type Err = json::error::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let val: json::Value = try!(json::from_str(s));
Ok(ModeEventInfo {
change: val.get("change").unwrap().as_str().unwrap().to_owned(),
})
}
}
#[derive(Debug)]
pub struct WindowEventInfo {
pub change: WindowChange,
pub container: reply::Node,
}
impl FromStr for WindowEventInfo {
type Err = json::error::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let val: json::Value = try!(json::from_str(s));
Ok(WindowEventInfo {
change: match val.get("change").unwrap().as_str().unwrap() {
"new" => WindowChange::New,
"close" => WindowChange::Close,
"focus" => WindowChange::Focus,
"title" => WindowChange::Title,
"fullscreen_mode" => WindowChange::FullscreenMode,
"move" => WindowChange::Move,
"floating" => WindowChange::Floating,
"urgent" => WindowChange::Urgent,
#[cfg(feature = "i3-4-13")]
"mark" => WindowChange::Mark,
other => {
warn!(target: "i3ipc", "Unknown WindowChange {}", other);
WindowChange::Unknown
}
},
container: common::build_tree(val.get("container").unwrap()),
})
}
}
#[derive(Debug)]
pub struct BarConfigEventInfo {
pub bar_config: reply::BarConfig,
}
impl FromStr for BarConfigEventInfo {
type Err = json::error::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let val: json::Value = try!(json::from_str(s));
Ok(BarConfigEventInfo {
bar_config: common::build_bar_config(&val),
})
}
}
#[derive(Debug)]
pub struct BindingEventInfo {
pub change: BindingChange,
pub binding: Binding,
}
impl FromStr for BindingEventInfo {
type Err = json::error::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let val: json::Value = try!(json::from_str(s));
let bind = val.get("binding").unwrap();
Ok(BindingEventInfo {
change: match val.get("change").unwrap().as_str().unwrap() {
"run" => BindingChange::Run,
other => {
warn!(target: "i3ipc", "Unknown BindingChange {}", other);
BindingChange::Unknown
}
},
binding: Binding {
command: bind.get("command").unwrap().as_str().unwrap().to_owned(),
event_state_mask: bind
.get("event_state_mask")
.unwrap()
.as_array()
.unwrap()
.iter()
.map(|m| m.as_str().unwrap().to_owned())
.collect(),
input_code: bind.get("input_code").unwrap().as_i64().unwrap() as i32,
symbol: match bind.get("symbol").unwrap().clone() {
json::Value::String(s) => Some(s),
json::Value::Null => None,
_ => unreachable!(),
},
input_type: match bind.get("input_type").unwrap().as_str().unwrap() {
"keyboard" => InputType::Keyboard,
"mouse" => InputType::Mouse,
other => {
warn!(target: "i3ipc", "Unknown InputType {}", other);
InputType::Unknown
}
},
},
})
}
}
#[derive(Debug)]
#[cfg(feature = "i3-4-14")]
#[cfg_attr(feature = "dox", doc(cfg(feature = "i3-4-14")))]
pub struct ShutdownEventInfo {
pub change: ShutdownChange,
}
#[cfg(feature = "i3-4-14")]
#[cfg_attr(feature = "dox", doc(cfg(feature = "i3-4-14")))]
impl FromStr for ShutdownEventInfo {
type Err = json::error::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let val: json::Value = try!(json::from_str(s));
let change = match val.get("change").unwrap().as_str().unwrap() {
"restart" => ShutdownChange::Restart,
"exit" => ShutdownChange::Exit,
other => {
warn!(target: "i3ipc", "Unknown ShutdownChange {}", other);
ShutdownChange::Unknown
}
};
Ok(ShutdownEventInfo { change })
}
}
pub mod inner {
#[derive(Debug, PartialEq)]
pub enum WorkspaceChange {
Focus,
Init,
Empty,
Urgent,
Rename,
Reload,
Restored,
Move,
Unknown,
}
#[derive(Debug, PartialEq)]
pub enum OutputChange {
Unspecified,
Unknown,
}
#[derive(Debug, PartialEq)]
pub enum WindowChange {
New,
Close,
Focus,
Title,
FullscreenMode,
Move,
Floating,
Urgent,
#[cfg(feature = "i3-4-13")]
#[cfg_attr(feature = "dox", doc(cfg(feature = "i3-4-13")))]
Mark,
Unknown,
}
#[derive(Debug, PartialEq)]
pub enum InputType {
Keyboard,
Mouse,
Unknown,
}
#[derive(Debug, PartialEq)]
pub struct Binding {
pub command: String,
pub event_state_mask: Vec<String>,
pub input_code: i32,
pub symbol: Option<String>,
pub input_type: InputType,
}
#[derive(Debug, PartialEq)]
pub enum BindingChange {
Run,
Unknown,
}
#[derive(Debug, PartialEq)]
#[cfg(feature = "i3-4-14")]
#[cfg_attr(feature = "dox", doc(cfg(feature = "i3-4-14")))]
pub enum ShutdownChange {
Restart,
Exit,
Unknown,
}
}