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
235
236
237
238
239
240
241
242
243
// MIT/Apache2 License
#[cfg(all(feature = "tokio-support", unix))]
#[path = "util/cancel.rs"]
mod cancel;
#[cfg(all(feature = "tokio-support", unix))]
use breadx::Result;
#[cfg(all(feature = "tokio-support", unix))]
mod inner {
use breadx::{
prelude::*,
protocol::{xproto, Event},
rt_support::tokio_support,
Result,
};
use tokio::io::AsyncWriteExt;
#[tokio::main(flavor = "current_thread")]
pub async fn real_main() -> Result<()> {
super::cancel::spawn_kill_thread();
// Connect to the server.
let mut connection = tokio_support::connect(None).await?;
// the events our windows receives.
let events = xproto::EventMask::EXPOSURE | xproto::EventMask::BUTTON_PRESS;
// the background color
let background = connection.default_screen().white_pixel;
// create the new window
let parent = connection.default_screen().root;
let wid = connection.generate_xid().await?;
connection
.create_window_checked(
0,
wid,
parent,
0,
0,
600,
400,
0,
xproto::WindowClass::COPY_FROM_PARENT,
0,
xproto::CreateWindowAux::new()
.event_mask(events)
.background_pixel(background),
)
.await?;
// map to screen and set title
connection.map_window_checked(wid).await?;
let title = "Hello from tokio!";
connection
.change_property_checked(
xproto::PropMode::REPLACE,
wid,
xproto::AtomEnum::WM_NAME.into(),
xproto::AtomEnum::STRING.into(),
8,
title.len() as u32,
title,
)
.await?;
// set up a GC for drawing
let gc = connection.generate_xid().await?;
connection
.create_gc_checked(
gc,
wid,
xproto::CreateGCAux::new()
.foreground(connection.default_screen().black_pixel)
.graphics_exposures(0)
.line_width(10),
)
.await?;
// create some colors
let cmap = connection.default_screen().default_colormap;
let red_color = connection.alloc_color(cmap, 0xffff, 0, 0).await?;
let green_color = connection.alloc_color(cmap, 0, 0xffff, 0).await?;
let blue_color = connection.alloc_color(cmap, 0, 0, 0xffff).await?;
connection.flush().await?;
// resolve the colors
let red_pixel = connection.wait_for_reply(red_color).await?.pixel;
let green_pixel = connection.wait_for_reply(green_color).await?.pixel;
let blue_pixel = connection.wait_for_reply(blue_color).await?.pixel;
// setup exit strategy
let wm_protocols = connection.intern_atom(false, "WM_PROTOCOLS").await?;
let wm_delete_window = connection.intern_atom(false, "WM_DELETE_WINDOW").await?;
connection.flush().await?;
let wm_protocols = connection.wait_for_reply(wm_protocols).await?.atom;
let wm_delete_window = connection.wait_for_reply(wm_delete_window).await?.atom;
connection
.change_property_checked(
xproto::PropMode::REPLACE,
wid,
wm_protocols,
xproto::AtomEnum::ATOM.into(),
32,
1,
&wm_delete_window,
)
.await?;
super::cancel::spawn_close_thread(wid);
// primary event loop
loop {
let event = connection.wait_for_event().await?;
match event {
Event::Expose(_) => {
// begin the repaint
// draw a red "X"
connection
.change_gc(gc, xproto::ChangeGCAux::new().foreground(red_pixel))
.await?;
connection
.poly_segment(
wid,
gc,
&[
xproto::Segment {
x1: 10,
y1: 10,
x2: 150,
y2: 150,
},
xproto::Segment {
x1: 150,
y1: 10,
x2: 10,
y2: 150,
},
],
)
.await?;
// draw a green circle
connection
.change_gc(gc, xproto::ChangeGCAux::new().foreground(green_pixel))
.await?;
connection
.poly_fill_arc(
wid,
gc,
&[xproto::Arc {
x: 200,
y: 10,
width: 150,
height: 150,
angle1: 0,
angle2: 360 * 64,
}],
)
.await?;
// draw a blue semicircle
connection
.change_gc(gc, xproto::ChangeGCAux::new().foreground(blue_pixel))
.await?;
connection
.poly_fill_arc(
wid,
gc,
&[xproto::Arc {
x: 200,
y: 10,
width: 150,
height: 150,
angle1: 0,
angle2: 270 * 64,
}],
)
.await?;
// draw the black outline of a circle
connection
.change_gc(
gc,
xproto::ChangeGCAux::new()
.foreground(connection.default_screen().black_pixel),
)
.await?;
connection
.poly_arc(
wid,
gc,
&[xproto::Arc {
x: 200,
y: 10,
width: 150,
height: 150,
angle1: 0,
angle2: 360 * 64,
}],
)
.await?;
// end the repaint
connection.flush().await?;
}
Event::ButtonPress(bp) => {
// indicate the button press
let mut stdout = tokio::io::stdout();
let f = format!("Detected click at ({}, {})\n", bp.event_x, bp.event_y);
stdout.write_all(f.as_bytes()).await.unwrap();
}
Event::ClientMessage(cme) => {
// check if exit msg
if cme.data.as_data32()[0] == wm_delete_window {
break;
}
}
_ => {}
}
}
Ok(())
}
}
#[cfg(all(feature = "tokio-support", unix))]
fn main() -> Result<()> {
tracing_subscriber::fmt::init();
inner::real_main()
}
#[cfg(not(all(feature = "tokio-support", unix)))]
fn main() {
println!("`tokio-support` feature needs to be enabled for this example");
}