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
use crate::{
core::{
bindings::{KeyBindings, MouseBindings},
data_types::{Point, Region},
manager::WindowManager,
screen::Screen,
xconnection::{
Atom, ClientAttr, ClientConfig, ClientMessage, ClientMessageKind, Prop, Result,
WindowState, XConn, XEvent, XEventHandler, Xid,
},
},
xcb::{Api, XcbError},
};
use std::collections::HashMap;
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct XcbConnection {
check_win: Xid,
api: Api,
}
impl XcbConnection {
pub fn new() -> Result<Self> {
let api = Api::new()?;
let check_win = api.check_window();
api.set_randr_notify_mask()?;
Ok(Self { check_win, api })
}
pub fn xcb_connection(&self) -> &xcb::Connection {
&self.api.conn()
}
pub fn api(&self) -> &Api {
&self.api
}
pub fn api_mut(&mut self) -> &mut Api {
&mut self.api
}
pub fn known_atoms(&self) -> &HashMap<Atom, u32> {
&self.api.known_atoms()
}
}
impl WindowManager<XcbConnection> {
pub fn xcb_connection(&self) -> &xcb::Connection {
&self.conn().xcb_connection()
}
pub fn known_atoms(&self) -> &HashMap<Atom, u32> {
&self.conn().known_atoms()
}
}
crate::__xcb_impl_xatom_querier!(XcbConnection);
crate::__xcb_impl_xclientconfig!(XcbConnection);
crate::__xcb_impl_xclienthandler!(XcbConnection);
crate::__xcb_impl_xclientproperties!(XcbConnection);
crate::__xcb_impl_xeventhandler!(XcbConnection);
crate::__xcb_impl_xstate!(XcbConnection);
impl XConn for XcbConnection {
#[cfg(feature = "serde")]
fn hydrate(&mut self) -> Result<()> {
Ok(self.api.hydrate()?)
}
fn init(&self) -> Result<()> {
Ok(self
.api
.set_client_attributes(self.api.root(), &[ClientAttr::RootEventMask])
.map_err(|e| XcbError::Raw(format!("Unable to set root window event mask: {}", e)))?)
}
fn check_window(&self) -> Xid {
self.check_win
}
fn cleanup(&self) -> Result<()> {
self.api.ungrab_keys()?;
self.api.ungrab_mouse_buttons()?;
let net_name = Atom::NetActiveWindow.as_ref();
self.api.delete_prop(self.api.root(), net_name)?;
self.api.destroy_client(self.check_win)?;
self.api.flush();
Ok(())
}
fn grab_keys(
&self,
key_bindings: &KeyBindings<Self>,
mouse_bindings: &MouseBindings<Self>,
) -> Result<()> {
self.api
.grab_keys(&key_bindings.keys().collect::<Vec<_>>())?;
self.api.grab_mouse_buttons(
&mouse_bindings
.keys()
.map(|(_, state)| state)
.collect::<Vec<_>>(),
)?;
self.flush();
Ok(())
}
}