use std::{ptr::NonNull, sync::Arc}; use raw_window_handle::{ RawDisplayHandle, RawWindowHandle, WaylandDisplayHandle, WaylandWindowHandle, }; use log::debug; use smithay_client_toolkit::{ compositor::CompositorHandler, compositor::CompositorState, delegate_compositor, delegate_layer, delegate_output, delegate_registry, delegate_seat, output::{OutputHandler, OutputState}, registry::{ProvidesRegistryState, RegistryState}, registry_handlers, seat::{Capability, SeatHandler, SeatState}, shell::wlr_layer::{Anchor, KeyboardInteractivity, Layer, LayerShell}, shell::{ WaylandSurface, wlr_layer::{LayerShellHandler, LayerSurface, LayerSurfaceConfigure}, }, }; use smol::channel::Sender; use vk_graph::prelude::*; use wayland_client::{ Connection, EventQueue, Proxy, QueueHandle, globals::registry_queue_init, protocol::{wl_output, wl_seat, wl_surface}, }; use crate::Window; pub struct BarState { pub registry_state: RegistryState, pub seat_state: SeatState, pub output_state: OutputState, pub width: u32, pub height: u32, pub exit: bool, pub layer: LayerSurface, pub display: Display, pub pool: HashPool, pub device: Arc, pub update_sender: Sender<()>, } impl BarState { pub fn new(height: u32, tx: Sender<()>) -> (Self, EventQueue) { let conn = Connection::connect_to_env().unwrap(); let (globals, event_queue) = registry_queue_init(&conn).unwrap(); let qh: QueueHandle = event_queue.handle(); // Initialize xdg_shell handlers so we can select the correct adapter let compositor_state = CompositorState::bind(&globals, &qh).expect("wl_compositor not available"); let layer_shell = LayerShell::bind(&globals, &qh).expect("layer shell not available"); let surface = compositor_state.create_surface(&qh); // Create the window for adapter selection let layer = layer_shell.create_layer_surface(&qh, surface, Layer::Bottom, Some("conduit"), None); layer.set_anchor(Anchor::TOP); layer.set_keyboard_interactivity(KeyboardInteractivity::None); // Create the raw window handle for the surface. let raw_display_handle = RawDisplayHandle::Wayland(WaylandDisplayHandle::new( NonNull::new(conn.backend().display_ptr() as *mut _).unwrap(), )); let raw_window_handle = RawWindowHandle::Wayland(WaylandWindowHandle::new( NonNull::new(layer.wl_surface().id().as_ptr() as *mut _).unwrap(), )); let window = Window { display_handle: raw_display_handle, window_handle: raw_window_handle, }; let s13_device = Arc::new( Device::create_display( DeviceInfoBuilder::default() .select_physical_device(Box::new(DeviceInfo::integrated_gpu)) .debug(true), &window, ) .unwrap(), ); let s13_surface = Surface::create(&s13_device, &window).unwrap(); let formats = Surface::formats(&s13_surface).unwrap(); let format = Surface::srgb(&formats).unwrap(); let s13_swapchain = Swapchain::new( &s13_device, s13_surface, SwapchainInfoBuilder::default() .width(0) .height(20) .surface(format) .present_modes(vec![vk::PresentModeKHR::MAILBOX]), ) .unwrap(); let s13_display = vk_graph::Display::new(&s13_device, s13_swapchain, DisplayInfo::default()).unwrap(); let s13_pool = HashPool::new(&s13_device); ( BarState { registry_state: RegistryState::new(&globals), seat_state: SeatState::new(&globals, &qh), output_state: OutputState::new(&globals, &qh), width: 0, height, exit: false, layer, display: s13_display, pool: s13_pool, device: s13_device, update_sender: tx, }, event_queue, ) } } impl CompositorHandler for BarState { fn scale_factor_changed( &mut self, _conn: &Connection, _qh: &QueueHandle, _surface: &wl_surface::WlSurface, _new_factor: i32, ) { // Not needed for this example. debug!("compositor: scale factor changed"); } fn transform_changed( &mut self, _conn: &Connection, _qh: &QueueHandle, _surface: &wl_surface::WlSurface, _new_transform: wl_output::Transform, ) { // Not needed for this example. debug!("compositor: transform changed"); } fn frame( &mut self, _conn: &Connection, _qh: &QueueHandle, _surface: &wl_surface::WlSurface, _time: u32, ) { // Not needed for this example. debug!("compositor: frame changed"); } fn surface_enter( &mut self, _conn: &Connection, _qh: &QueueHandle, _surface: &wl_surface::WlSurface, _output: &wl_output::WlOutput, ) { // Not needed for this example. debug!("compositor: surface enter"); } fn surface_leave( &mut self, _conn: &Connection, _qh: &QueueHandle, _surface: &wl_surface::WlSurface, _output: &wl_output::WlOutput, ) { // Not needed for this example. debug!("compositor: surface leave"); } } impl OutputHandler for BarState { fn output_state(&mut self) -> &mut OutputState { &mut self.output_state } fn new_output( &mut self, _conn: &Connection, _qh: &QueueHandle, _output: wl_output::WlOutput, ) { debug!("output: new output"); let size = self .output_state .info(&_output) .and_then(|v| v.logical_size) .unwrap(); self.width = size.0 as u32; self.layer.set_size(self.width, self.height); self.layer.set_exclusive_zone(self.height as i32); self.layer.commit(); } fn update_output( &mut self, _conn: &Connection, _qh: &QueueHandle, _output: wl_output::WlOutput, ) { debug!("output: update output"); } fn output_destroyed( &mut self, _conn: &Connection, _qh: &QueueHandle, _output: wl_output::WlOutput, ) { debug!("output: output destroyed"); } } impl SeatHandler for BarState { fn seat_state(&mut self) -> &mut SeatState { &mut self.seat_state } fn new_seat(&mut self, _: &Connection, _: &QueueHandle, _: wl_seat::WlSeat) { debug!("seat: new seat"); } fn new_capability( &mut self, _conn: &Connection, _qh: &QueueHandle, _seat: wl_seat::WlSeat, _capability: Capability, ) { debug!("seat: new capability"); } fn remove_capability( &mut self, _conn: &Connection, _: &QueueHandle, _: wl_seat::WlSeat, _capability: Capability, ) { debug!("seat: remove capability"); } fn remove_seat(&mut self, _: &Connection, _: &QueueHandle, _: wl_seat::WlSeat) { debug!("seat: remove seat"); } } impl LayerShellHandler for BarState { fn closed(&mut self, _conn: &Connection, _qh: &QueueHandle, _layer: &LayerSurface) { debug!("layer shell: closed"); self.exit = true; } fn configure( &mut self, _conn: &Connection, _qh: &QueueHandle, _layer: &LayerSurface, _configure: LayerSurfaceConfigure, _serial: u32, ) { debug!("layer shell: configure"); self.display.set_swapchain_info( self.display .swapchain_info() .to_builder() .width(self.width) .height(self.height), ); smol::block_on(self.update_sender.send(())).unwrap(); } } delegate_compositor!(BarState); delegate_output!(BarState); delegate_seat!(BarState); delegate_layer!(BarState); delegate_registry!(BarState); impl ProvidesRegistryState for BarState { fn registry(&mut self) -> &mut RegistryState { &mut self.registry_state } registry_handlers![OutputState]; }