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
extern crate env_logger;
extern crate libc;
#[macro_use]
extern crate log;
#[cfg(feature = "pledge")]
#[macro_use]
extern crate pledge;
extern crate xcb;
#[cfg(feature = "pledge")]
use pledge::{pledge, Promise, ToPromiseString};
use std::env::remove_var;
use std::ptr::null_mut;
use std::mem::{transmute, uninitialized};
pub mod wm;
use wm::config::*;
use wm::err::*;
use wm::window_system::Wm;
use xcb::base::*;
extern "C" fn sigchld_action(_: libc::c_int) {
unsafe {
loop {
let pid = libc::waitpid(-1, null_mut(), libc::WNOHANG);
if pid <= 0 {
return;
}
}
}
}
#[cfg(feature = "pledge")]
fn pledge_promise() {
match pledge![Stdio, RPath, Proc, Exec, Unix] {
Err(_) => error!("calling pledge() failed"),
_ => (),
}
}
#[cfg(not(feature = "pledge"))]
fn pledge_promise() {}
fn main() {
if env_logger::init().is_err() {
handle_logger_error();
}
pledge_promise();
unsafe {
let mut act = uninitialized::<libc::sigaction>();
let f_ptr: *const libc::c_void = transmute(sigchld_action as extern "C" fn(libc::c_int));
act.sa_sigaction = f_ptr as libc::sighandler_t;
libc::sigemptyset(&mut act.sa_mask);
act.sa_flags = libc::SA_RESTART;
if libc::sigaction(libc::SIGCHLD, &act, null_mut()) == -1 {
WmError::CouldNotEstablishHandlers.handle();
}
}
remove_var("RUST_LOG");
let config = generate_config();
let (con, screen_num) = match Connection::connect(None) {
Ok(c) => c,
Err(e) => WmError::CouldNotConnect(e).handle(),
};
let mut wm = match Wm::new(&con, screen_num, config) {
Ok(w) => w,
Err(e) => e.handle(),
};
if let Err(e) = wm.register() {
e.handle();
}
if let Err(e) = wm.init_randr() {
e.handle();
}
setup_wm(&mut wm);
wm.init_clients();
if let Err(e) = wm.run() {
e.handle();
}
}