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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
use std::{env, path::Path};
use smithay_client_toolkit::{
compositor::{CompositorHandler, CompositorState},
delegate_compositor, delegate_output, delegate_registry, delegate_shm, delegate_simple,
delegate_xdg_shell, delegate_xdg_window,
output::{OutputHandler, OutputState},
registry::{ProvidesRegistryState, RegistryState, SimpleGlobal},
registry_handlers,
shell::{
xdg::{
window::{Window, WindowConfigure, WindowDecorations, WindowHandler},
XdgShell,
},
WaylandSurface,
},
shm::{slot::SlotPool, Shm, ShmHandler},
};
use wayland_client::{
globals::registry_queue_init,
protocol::{wl_output, wl_shm, wl_surface},
Connection, Dispatch, QueueHandle,
};
use wayland_protocols::wp::viewporter::client::{
wp_viewport::{self, WpViewport},
wp_viewporter::{self, WpViewporter},
};
fn main() {
env_logger::init();
// All Wayland apps start by connecting the compositor (server).
let conn = Connection::connect_to_env().unwrap();
// Enumerate the list of globals to get the protocols the server implements.
let (globals, mut event_queue) = registry_queue_init(&conn).unwrap();
let qh = event_queue.handle();
// The compositor (not to be confused with the server which is commonly called the compositor) allows
// configuring surfaces to be presented.
let compositor = CompositorState::bind(&globals, &qh).expect("wl_compositor not available");
// For desktop platforms, the XDG shell is the standard protocol for creating desktop windows.
let xdg_shell = XdgShell::bind(&globals, &qh).expect("xdg shell is not available");
// Since we are not using the GPU in this example, we use wl_shm to allow software rendering to a buffer
// we share with the compositor process.
let shm = Shm::bind(&globals, &qh).expect("wl shm is not available.");
// In this example, we use the viewporter to allow the compositor to scale and crop presented images.
//
// Since the wp_viewporter protocol has no events, we can use SimpleGlobal.
let wp_viewporter = SimpleGlobal::<wp_viewporter::WpViewporter, 1>::bind(&globals, &qh)
.expect("wp_viewporter not available");
let mut windows = Vec::new();
let mut pool_size = 0;
for path in env::args_os().skip(1) {
let image = match image::open(&path) {
Ok(i) => i,
Err(e) => {
println!("Failed to open image {}.", path.to_string_lossy());
println!("Error was: {e:?}");
return;
}
};
// We'll need the image in RGBA for drawing it
let image = image.to_rgba8();
pool_size += image.width() * image.height() * 4;
// A window is created from a surface.
let surface = compositor.create_surface(&qh);
// And then we can create the window.
let window = xdg_shell.create_window(surface, WindowDecorations::RequestServer, &qh);
// Configure the window, this may include hints to the compositor about the desired minimum size of the
// window, app id for WM identification, the window title, etc.
// GitHub does not let projects use the `org.github` domain but the `io.github` domain is fine.
window.set_app_id("io.github.smithay.client-toolkit.ImageViewer");
window.set_min_size(Some((256, 256)));
let path: &Path = path.as_os_str().as_ref();
window.set_title(path.components().last().unwrap().as_os_str().to_string_lossy());
// In order for the window to be mapped, we need to perform an initial commit with no attached buffer.
// For more info, see WaylandSurface::commit
//
// The compositor will respond with an initial configure that we can then use to present to the window with
// the correct options.
window.commit();
// For scaling, create a viewport for the window.
let viewport = wp_viewporter.get().expect("Requires wp_viewporter").get_viewport(
window.wl_surface(),
&qh,
(),
);
windows.push(ImageViewer {
width: image.width(),
height: image.height(),
window,
viewport,
image,
first_configure: true,
damaged: true,
});
}
if windows.is_empty() {
println!("USAGE: ./image_viewer <PATH> [<PATH>]...");
return;
}
let pool = SlotPool::new(pool_size as usize, &shm).expect("Failed to create pool");
let mut state = State {
registry_state: RegistryState::new(&globals),
output_state: OutputState::new(&globals, &qh),
shm,
wp_viewporter,
pool,
windows,
};
// We don't draw immediately, the configure will notify us when to first draw.
loop {
event_queue.blocking_dispatch(&mut state).unwrap();
if state.windows.is_empty() {
println!("exiting example");
break;
}
}
}
struct State {
registry_state: RegistryState,
output_state: OutputState,
shm: Shm,
wp_viewporter: SimpleGlobal<WpViewporter, 1>,
pool: SlotPool,
windows: Vec<ImageViewer>,
}
struct ImageViewer {
window: Window,
image: image::RgbaImage,
viewport: WpViewport,
width: u32,
height: u32,
first_configure: bool,
damaged: bool,
}
impl CompositorHandler for State {
fn scale_factor_changed(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_surface: &wl_surface::WlSurface,
_new_factor: i32,
) {
// Not needed for this example.
}
fn transform_changed(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_surface: &wl_surface::WlSurface,
_new_transform: wl_output::Transform,
) {
// Not needed for this example.
}
fn frame(
&mut self,
conn: &Connection,
qh: &QueueHandle<Self>,
_surface: &wl_surface::WlSurface,
_time: u32,
) {
self.draw(conn, qh);
}
fn surface_enter(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_surface: &wl_surface::WlSurface,
_output: &wl_output::WlOutput,
) {
// Not needed for this example.
}
fn surface_leave(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_surface: &wl_surface::WlSurface,
_output: &wl_output::WlOutput,
) {
// Not needed for this example.
}
}
impl OutputHandler for State {
fn output_state(&mut self) -> &mut OutputState {
&mut self.output_state
}
fn new_output(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_output: wl_output::WlOutput,
) {
}
fn update_output(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_output: wl_output::WlOutput,
) {
}
fn output_destroyed(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_output: wl_output::WlOutput,
) {
}
}
impl WindowHandler for State {
fn request_close(&mut self, _: &Connection, _: &QueueHandle<Self>, window: &Window) {
self.windows.retain(|v| v.window != *window);
}
fn configure(
&mut self,
conn: &Connection,
qh: &QueueHandle<Self>,
window: &Window,
configure: WindowConfigure,
_serial: u32,
) {
for viewer in &mut self.windows {
if viewer.window != *window {
continue;
}
if let (Some(width), Some(height)) = configure.new_size {
viewer.width = width.get();
viewer.height = height.get();
viewer.viewport.set_destination(width.get() as _, height.get() as _);
if !viewer.first_configure {
viewer.window.commit();
}
}
// Initiate the first draw.
viewer.first_configure = false;
}
self.draw(conn, qh);
}
}
impl ShmHandler for State {
fn shm_state(&mut self) -> &mut Shm {
&mut self.shm
}
}
impl State {
pub fn draw(&mut self, _conn: &Connection, _qh: &QueueHandle<Self>) {
for viewer in &mut self.windows {
if viewer.first_configure || !viewer.damaged {
continue;
}
let window = &viewer.window;
let width = viewer.image.width();
let height = viewer.image.height();
let stride = width as i32 * 4;
let (buffer, canvas) = self
.pool
.create_buffer(width as i32, height as i32, stride, wl_shm::Format::Argb8888)
.expect("create buffer");
// Draw to the window
for (pixel, argb) in viewer.image.pixels().zip(canvas.chunks_exact_mut(4)) {
// We do this in an horribly inefficient manner, for the sake of simplicity.
// We'll send pixels to the server in ARGB8888 format (this is one of the only
// formats that are guaranteed to be supported), but image provides it in
// big-endian RGBA8888, so we need to do the conversion.
argb[3] = pixel.0[3];
argb[2] = pixel.0[0];
argb[1] = pixel.0[1];
argb[0] = pixel.0[2];
}
// Damage the entire window (using the real dimensions)
window.wl_surface().damage_buffer(0, 0, viewer.width as i32, viewer.height as i32);
viewer.damaged = false;
// Set the entire buffer as the source area for the viewport.
// Destination was set during configure.
viewer.viewport.set_source(0.0, 0.0, viewer.width as f64, viewer.height as f64);
// Attach and commit to present.
buffer.attach_to(window.wl_surface()).expect("buffer attach");
window.wl_surface().commit();
}
}
}
delegate_compositor!(State);
delegate_output!(State);
delegate_shm!(State);
delegate_xdg_shell!(State);
delegate_xdg_window!(State);
delegate_simple!(State, WpViewporter, 1);
delegate_registry!(State);
impl ProvidesRegistryState for State {
fn registry(&mut self) -> &mut RegistryState {
&mut self.registry_state
}
registry_handlers!(OutputState);
}
impl AsMut<SimpleGlobal<WpViewporter, 1>> for State {
fn as_mut(&mut self) -> &mut SimpleGlobal<WpViewporter, 1> {
&mut self.wp_viewporter
}
}
impl Dispatch<WpViewport, ()> for State {
fn event(
_: &mut State,
_: &WpViewport,
_: wp_viewport::Event,
_: &(),
_: &Connection,
_: &QueueHandle<State>,
) {
unreachable!("wp_viewport::Event is empty in version 1")
}
}
impl Drop for ImageViewer {
fn drop(&mut self) {
self.viewport.destroy()
}
}
|