extern crate smithay_client_toolkit as sctk; use sctk::reexports::client::protocol::{wl_shm, wl_surface}; use sctk::shm::AutoMemPool; use sctk::window::{Event as WEvent, FallbackFrame}; sctk::default_environment!(SeasonalClock, desktop); fn main() { let (env, _display, mut queue) = sctk::new_default_environment!(SeasonalClock, desktop) .expect("Unable to connect to a Wayland compositor"); let surface = env .create_surface_with_scale_callback(|dpi, _surface, _dispatch_data| { println!("dpi changed to {}", dpi); }) .detach(); let mut next_action = None::; let mut window = env .create_window::( surface, None, (100, 100), move |evt, mut dispatch_data| { let next_action = dispatch_data.get::>().unwrap(); let replace = matches!( (&evt, &*next_action), (_, &None) | (_, &Some(WEvent::Refresh)) | (&WEvent::Configure { .. }, &Some(WEvent::Configure { .. })) | (&WEvent::Close, _) ); if replace { *next_action = Some(evt); } }, ) .expect("Failed to create a window !"); window.set_title("Seasonal Hours Clock".to_string()); let mut pool = env.create_auto_pool().expect("Failed to create the memory pool."); let mut need_redraw = false; let mut dimensions = (100, 100); if !env.get_shell().unwrap().needs_configure() { redraw(&mut pool, window.surface(), dimensions) .expect("Failed to draw"); window.refresh() } loop { match next_action.take() { Some(WEvent::Close) => break, Some(WEvent::Refresh) => { window.refresh(); window.surface().commit(); } Some(WEvent::Configure { new_size, states: _ }) => { if let Some((w, h)) = new_size { if dimensions != (w, h) { dimensions = (w, h); } } window.resize(dimensions.0, dimensions.1); window.refresh(); need_redraw = true; } None => {} } if need_redraw { need_redraw = false; redraw( &mut pool, window.surface(), dimensions, ) .expect("Failed to draw") } queue.dispatch(&mut next_action, |_, _, _| {}).unwrap(); } } fn redraw( pool: &mut AutoMemPool, surface: &wl_surface::WlSurface, (buf_x, buf_y): (u32, u32), ) -> Result<(), ::std::io::Error> { let (canvas, new_buffer) = pool.buffer( buf_x as i32, buf_y as i32, 4 * buf_x as i32, wl_shm::Format::Argb8888, )?; // We do not have anything to draw yet, so draw an empty surface for dst_pixel in canvas.chunks_exact_mut(4) { dst_pixel[0] = 0x00; dst_pixel[1] = 0x00; dst_pixel[2] = 0x00; dst_pixel[3] = 0xFF; } surface.attach(Some(&new_buffer), 0, 0); if surface.as_ref().version() >= 4 { surface.damage_buffer(0, 0, buf_x as i32, buf_y as i32); } else { surface.damage(0, 0, buf_x as i32, buf_y as i32); } surface.commit(); Ok(()) }