xcb.rs
8.34 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//
// XCB implementation for drawing some stuff...
//
// Georg Hopp <georg@steffers.org>
//
// Copyright © 2019 Georg Hopp
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
extern crate xcb;
use std::sync::Arc;
use std::ptr; //::{null, null_mut};
use std::thread;
use std::sync::mpsc;
use crate::easel::{Easel, Canvas, Drawable, Coordinate, Coordinates};
#[derive(Clone)]
pub struct XcbEasel (Arc<xcb::Connection>, i32);
pub struct XcbCanvas<'a> { conn :Arc<xcb::Connection>
, width :u16
, height :u16
, window :u32
, pixmap :u32
, gc :u32
, shm :Box<&'a mut [u32]> }
impl XcbEasel {
pub fn new() -> Result<XcbEasel, xcb::ConnError> {
let (conn, num) = xcb::Connection::connect(None)?;
Ok(XcbEasel(Arc::new(conn), num))
}
pub fn setup(&self) -> xcb::Setup {
let XcbEasel(conn, _) = self;
conn.get_setup()
}
pub fn screen(&self) -> Option<xcb::Screen> {
let XcbEasel(_, num) = self;
self.setup().roots().nth(*num as usize)
}
pub fn canvas<'a>(&self, width :u16, height :u16) -> Option<XcbCanvas<'a>> {
let Self(conn, _) = self;
let conn = conn.clone();
let screen = match self.screen() {
None => return None,
Some(screen) => screen,
};
println!("root depth: {}", screen.root_depth());
let shmseg = conn.generate_id();
let gc = conn.generate_id();
let pixmap = conn.generate_id();
let window = conn.generate_id();
xcb::create_window( &conn, xcb::COPY_FROM_PARENT as u8, window
, screen.root(), 0, 0, width, width, 0
, xcb::WINDOW_CLASS_INPUT_OUTPUT as u16
, screen.root_visual()
, &[(xcb::CW_BACK_PIXEL, screen.white_pixel())] );
xcb::create_gc( &conn, gc, screen.root()
, &[ (xcb::GC_FOREGROUND, screen.black_pixel())
, (xcb::GC_GRAPHICS_EXPOSURES, 0) ] );
let (shmid, shm) = getshm((width * height) as usize);
xcb::shm::attach(&conn, shmseg, shmid as u32, false);
unsafe { libc::shmctl(shmid, libc::IPC_RMID, ptr::null_mut()); }
xcb::shm::create_pixmap( &conn, pixmap, window, width, height
, screen.root_depth(), shmseg, 0 );
xcb::map_window(&conn, window);
conn.flush();
Some(XcbCanvas{ conn: conn
, width: width
, height: height
, window: window
, pixmap: pixmap
, gc: gc
, shm: Box::new(shm) } )
}
}
impl<'a> XcbCanvas<'a> {
pub fn set_title(&self, title :&str) {
let c = xcb::change_property_checked( &self.conn
, xcb::PROP_MODE_REPLACE as u8
, self.window
, xcb::ATOM_WM_NAME
, xcb::ATOM_STRING
, 8
, title.as_bytes() );
if self.conn.has_error().is_err() || c.request_check().is_err() {
println!("Error setting title");
}
}
}
fn getshm<'a>(size :usize) -> (i32, &'a mut [u32]) {
use std::slice::from_raw_parts_mut;
unsafe {
let id = libc::shmget( libc::IPC_PRIVATE
, size * 4
, libc::IPC_CREAT | 0o744 );
let ptr = libc::shmat(id, ptr::null(), 0);
(id as i32, from_raw_parts_mut(ptr as *mut u32, size))
}
}
impl Easel for XcbEasel {}
impl<'a> Canvas for XcbCanvas<'a> {
fn init_events(&self) {
let mask = [( xcb::CW_EVENT_MASK, xcb::EVENT_MASK_EXPOSURE
| xcb::EVENT_MASK_KEY_PRESS
| xcb::EVENT_MASK_STRUCTURE_NOTIFY
| xcb::EVENT_MASK_PROPERTY_CHANGE )];
xcb::change_window_attributes(&self.conn, self.window, &mask);
self.conn.flush();
}
fn start_events(&self, tx :mpsc::Sender<i32>) {
let conn = self.conn.clone();
let window = self.window;
let pixmap = self.pixmap;
let gc = self.gc;
let width = self.width;
let height = self.height;
thread::spawn(move || {
loop {
let event = conn.wait_for_event();
match event {
None => break,
Some(event) => {
match event.response_type() & !0x80 {
xcb::PROPERTY_NOTIFY => {
let prop_notify :&xcb::PropertyNotifyEvent
= unsafe { xcb::cast_event(&event) };
if prop_notify.atom() == xcb::ATOM_WM_NAME {
// 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() {
let r = reply.value();
let r = std::str::from_utf8(r).unwrap();
println!("title changed to: {}", r);
}
}
},
xcb::EXPOSE => {
xcb::copy_area( &conn, pixmap, window, gc
, 0, 0, 0, 0
, width, height );
conn.flush();
},
xcb::KEY_PRESS => {
let key_press: &xcb::KeyPressEvent
= unsafe { xcb::cast_event(&event) };
println!( "Key '{}' pressed"
, key_press.detail() );
// Q (on qwerty)
if key_press.detail() == 0x18 {
tx.send(1).unwrap();
break;
}
},
_ => {},
}
},
}
}
});
}
fn clear(&mut self) {
unsafe {
let ptr = self.shm.as_mut_ptr();
ptr::write_bytes( ptr, 0
, self.width as usize * self.height as usize);
}
}
fn draw(&mut self, d :&dyn Drawable, ofs :Coordinate) {
let Coordinates(c) = d.plot();
let Coordinate(xofs, yofs) = ofs;
for Coordinate(x, y) in c {
let idx :usize = ((y+yofs)*(self.width as i32)+x+xofs) as usize;
self.shm[idx] = 0xFFFFFF;
}
}
fn show(&self) {
xcb::copy_area( &self.conn, self.pixmap, self.window, self.gc
, 0, 0, 0, 0
, self.width, self.height );
self.conn.flush();
}
}