info.rs
4.13 KB
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
extern crate xcb;
use std::iter::{Iterator};
use xcb::randr;
fn main() {
let dpy = ":0";
let (conn, screen_num) = xcb::Connection::connect(Some(&dpy)).unwrap();
let setup = conn.get_setup();
let screen = setup.roots().nth(screen_num as usize).unwrap();
println!("");
println!("Informations of screen {}:", screen.root());
println!(" width..........: {}", screen.width_in_pixels());
println!(" height.........: {}", screen.height_in_pixels());
println!(" white pixel....: {:x}", screen.white_pixel());
println!(" black pixel....: {:x}", screen.black_pixel());
let window_dummy = conn.generate_id();
xcb::create_window(
&conn, 0, window_dummy, screen.root()
, 0, 0, 1, 1, 0, 0, 0, &[]);
conn.flush();
let cookie = randr::get_screen_info(&conn, window_dummy);
let reply = cookie.get_reply().unwrap();
let sizes = reply.sizes();
for (i, size) in sizes.enumerate() {
if i != 0 { println!(""); }
println!("size of screen {}:", i+1);
println!(" {} x {} ({}mm x {}mm)", size.width(), size.height(),
size.mwidth(), size.mheight());
}
// ====
let window = conn.generate_id();
let values = [
(xcb::CW_BACK_PIXEL, screen.white_pixel()),
(xcb::CW_EVENT_MASK, xcb::EVENT_MASK_EXPOSURE | xcb::EVENT_MASK_KEY_PRESS),
];
xcb::create_window(&conn,
xcb::COPY_FROM_PARENT as u8,
window,
screen.root(),
0, 0,
150, 150,
10,
xcb::WINDOW_CLASS_INPUT_OUTPUT as u16,
screen.root_visual(),
&values);
xcb::map_window(&conn, window);
let title = "Basic Window";
// setting title
xcb::change_property(&conn, xcb::PROP_MODE_REPLACE as u8, window,
xcb::ATOM_WM_NAME, xcb::ATOM_STRING, 8, title.as_bytes());
conn.flush();
// retrieving title
let cookie = xcb::get_property(&conn, false, window, xcb::ATOM_WM_NAME,
xcb::ATOM_STRING, 0, 1024);
if let Ok(reply) = cookie.get_reply() {
assert_eq!(std::str::from_utf8(reply.value()).unwrap(), title);
} else {
panic!("could not retrieve window title!");
}
// retrieving a few atoms
let (wm_state, wm_state_maxv, wm_state_maxh) = {
let cook = xcb::intern_atom(&conn, true, "_NET_WM_STATE");
let cook_maxv = xcb::intern_atom(&conn, true, "_NET_WM_STATE_MAXIMIZED_VERT");
let cook_maxh = xcb::intern_atom(&conn, true, "_NET_WM_STATE_MAXIMIZED_HORZ");
(cook.get_reply().unwrap().atom(),
cook_maxv.get_reply().unwrap().atom(),
cook_maxh.get_reply().unwrap().atom())
};
let mut maximized = false;
loop {
let event = conn.wait_for_event();
match event {
None => { break; }
Some(event) => {
let r = event.response_type();
if r == xcb::KEY_PRESS as u8 {
let key_press : &xcb::KeyPressEvent = unsafe {
xcb::cast_event(&event)
};
println!("Key '{}' pressed", key_press.detail());
if key_press.detail() == 0x3a { // M (on qwerty)
// toggle maximized
println!("toggle maximized: {} {}", wm_state_maxv, wm_state_maxh);
// ClientMessageData is a memory safe untagged union
let data = xcb::ClientMessageData::from_data32([
if maximized { 0 } else { 1 },
wm_state_maxv, wm_state_maxh,
0, 0
]);
let ev = xcb::ClientMessageEvent::new(32, window,
wm_state, data);
xcb::send_event(&conn, false, screen.root(),
xcb::EVENT_MASK_STRUCTURE_NOTIFY, &ev);
conn.flush();
maximized = !maximized;
}
else if key_press.detail() == 0x18 { // Q (on qwerty)
break;
}
}
}
}
}
}