use smithay_client_toolkit::
{
compositor::CompositorHandler,
delegate_compositor, delegate_subcompositor, delegate_foreign_toplevel_list, delegate_layer,
delegate_output, delegate_registry, delegate_seat, delegate_keyboard,
delegate_pointer, delegate_touch, delegate_shm, delegate_xdg_popup,
delegate_xdg_shell, delegate_xdg_window, delegate_session_lock,
foreign_toplevel_list::{ ForeignToplevelList, ForeignToplevelListHandler },
output::{ OutputHandler, OutputState },
registry::{ ProvidesRegistryState, RegistryState },
registry_handlers,
seat::{ Capability, SeatHandler, SeatState },
shell::
{
WaylandSurface,
wlr_layer::{ LayerShellHandler, LayerSurface, LayerSurfaceConfigure },
xdg::XdgSurface,
xdg::popup::{ Popup, PopupConfigure, PopupHandler },
xdg::window::{ Window, WindowConfigure, WindowHandler },
},
shm::{ Shm, ShmHandler },
session_lock::{ SessionLock, SessionLockHandler, SessionLockSurface, SessionLockSurfaceConfigure },
};
use smithay_client_toolkit::reexports::protocols::ext::foreign_toplevel_list::v1::client::ext_foreign_toplevel_handle_v1::ExtForeignToplevelHandleV1;
use smithay_client_toolkit::reexports::client::
{
protocol::
{
wl_callback::{ self, WlCallback },
wl_output::{ self, WlOutput },
wl_surface::WlSurface,
wl_seat::WlSeat,
},
Connection, Dispatch, Proxy, QueueHandle,
};
use wayland_protocols::wp::text_input::zv3::client::
{
zwp_text_input_manager_v3::ZwpTextInputManagerV3,
zwp_text_input_v3::{ self, ZwpTextInputV3 },
};
use crate::app::{ App, ToplevelEvent };
use super::app_data::AppData;
impl<A: App> CompositorHandler for AppData<A>
{
fn scale_factor_changed( &mut self, _: &Connection, _: &QueueHandle<Self>, surface: &WlSurface, new_factor: i32 )
{
if new_factor <= 0 { return; }
let Some( focus ) = self.focus_for_surface( surface ) else { return };
let ( pw, ph ) = {
let shm = &self.shm;
let ss = match focus
{
super::SurfaceFocus::Main => &mut self.main,
super::SurfaceFocus::Overlay( id ) => match self.overlays.get_mut( &id )
{
Some( s ) => s,
None => return,
},
};
if new_factor == ss.scale_factor { return; }
ss.scale_factor = new_factor;
surface.set_buffer_scale( new_factor );
if let Some( ref mut canvas ) = ss.canvas
{
canvas.set_dpi_scale( new_factor as f32 );
}
let pw = ss.width * new_factor as u32;
let ph = ss.height * new_factor as u32;
if let Some( ref es ) = ss.egl_surface
{
es.resize( pw as i32, ph as i32 );
} else {
ss.pool = Some(
smithay_client_toolkit::shm::slot::SlotPool::new(
( pw * ph * 4 ) as usize, shm,
).expect( "pool" ),
);
if let Some( ref mut canvas ) = ss.canvas
{
canvas.resize( pw, ph );
}
}
ss.request_redraw();
( pw, ph )
};
if matches!( focus, super::SurfaceFocus::Main )
{
self.app.on_scale_changed( new_factor as u32 );
self.app.on_resize( pw, ph );
self.dirty_caches();
}
}
fn transform_changed( &mut self, _: &Connection, _: &QueueHandle<Self>, _: &WlSurface, _: wl_output::Transform ) {}
fn frame(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_surface: &WlSurface,
_time: u32,
)
{
}
fn surface_enter( &mut self, _: &Connection, _: &QueueHandle<Self>, _: &WlSurface, _: &WlOutput ) {}
fn surface_leave( &mut self, _: &Connection, _: &QueueHandle<Self>, _: &WlSurface, _: &WlOutput ) {}
}
impl<A: App> LayerShellHandler for AppData<A>
{
fn closed(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
layer: &LayerSurface,
)
{
match self.focus_for_surface( layer.wl_surface() )
{
Some( super::SurfaceFocus::Main ) | None =>
{
if self.app.on_close_requested()
{
self.exit_requested = true;
}
}
Some( super::SurfaceFocus::Overlay( id ) ) =>
{
self.discard_overlay( id );
}
}
}
fn configure(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
layer: &LayerSurface,
configure: LayerSurfaceConfigure,
_serial: u32,
)
{
let ( w, h ) = configure.new_size;
let ( w, h ) = ( w.max( 1 ), h.max( 1 ) );
match self.focus_for_surface( layer.wl_surface() )
{
Some( super::SurfaceFocus::Main ) | None =>
{
self.on_configure( w, h );
}
Some( super::SurfaceFocus::Overlay( id ) ) =>
{
if let Some( ss ) = self.overlays.get_mut( &id )
{
ss.on_configure( &self.shm, self.egl_context.as_ref(), w, h );
}
}
}
}
}
impl<A: App> WindowHandler for AppData<A>
{
fn request_close(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_window: &Window,
)
{
if self.app.on_close_requested()
{
self.exit_requested = true;
}
}
fn configure(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
window: &Window,
configure: WindowConfigure,
_serial: u32,
)
{
if self.pending_fullscreen
{
window.set_fullscreen( None );
self.pending_fullscreen = false;
}
if self.pending_size_hint_unpin
{
window.set_max_size( None );
self.pending_size_hint_unpin = false;
}
let ( hint_w, hint_h ) = self.app.window_size_hint().unwrap_or( ( 800, 600 ) );
let w = configure.new_size.0.map( |v| v.get() ).unwrap_or( hint_w );
let h = configure.new_size.1.map( |v| v.get() ).unwrap_or( hint_h );
window.xdg_surface().set_window_geometry( 0, 0, w as i32, h as i32 );
self.on_configure( w, h );
}
}
impl<A: App> ShmHandler for AppData<A>
{
fn shm_state( &mut self ) -> &mut Shm
{
&mut self.shm
}
}
impl<A: App> OutputHandler for AppData<A>
{
fn output_state( &mut self ) -> &mut OutputState
{
&mut self.output_state
}
fn new_output( &mut self, _: &Connection, qh: &QueueHandle<Self>, output: WlOutput )
{
if let Some( ref layer_shell ) = self.layer_shell
{
self.main.surface.materialize( &self.compositor_state, layer_shell, qh, &output );
for ss in self.overlays.values_mut()
{
ss.surface.materialize( &self.compositor_state, layer_shell, qh, &output );
}
}
}
fn update_output( &mut self, _: &Connection, _: &QueueHandle<Self>, _: WlOutput ) {}
fn output_destroyed( &mut self, _: &Connection, _: &QueueHandle<Self>, _: WlOutput ) {}
}
impl<A: App> SeatHandler for AppData<A>
{
fn seat_state( &mut self ) -> &mut SeatState
{
&mut self.seat_state
}
fn new_seat(
&mut self, _conn: &Connection, qh: &QueueHandle<Self>, seat: WlSeat,
)
{
if self.data_device.is_none()
{
if let Some( ref ddm ) = self.data_device_manager
{
self.data_device = Some( ddm.get_data_device( qh, &seat ) );
}
}
}
fn new_capability(
&mut self,
_conn: &Connection,
qh: &QueueHandle<Self>,
seat: WlSeat,
capability: Capability,
)
{
match capability
{
Capability::Keyboard if self.keyboard.is_none() =>
{
self.keyboard = Some(
self.seat_state
.get_keyboard( qh, &seat, None )
.expect( "keyboard" ),
);
}
Capability::Pointer if self.pointer.is_none() =>
{
let pointer = self.seat_state
.get_pointer( qh, &seat )
.expect( "pointer" );
if let Some( ref mgr ) = self.cursor_shape_manager
{
self.cursor_shape_device = Some( mgr.get_shape_device( &pointer, qh ) );
}
self.pointer = Some( pointer );
}
Capability::Touch if self.touch.is_none() =>
{
if self.reset_touch_state()
{
eprintln!( "[ltk] touch capability re-added — cleared touch state stranded across the gap" );
}
self.touch = Some(
self.seat_state
.get_touch( qh, &seat )
.expect( "touch" ),
);
}
_ => {}
}
}
fn remove_capability(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_seat: WlSeat,
capability: Capability,
)
{
match capability
{
Capability::Keyboard => { self.keyboard = None; }
Capability::Pointer => { self.pointer = None; }
Capability::Touch =>
{
self.touch = None;
if self.reset_touch_state()
{
eprintln!( "[ltk] touch capability removed mid-gesture — reset stale touch state" );
}
}
_ => {}
}
}
fn remove_seat(
&mut self, _conn: &Connection, _qh: &QueueHandle<Self>, _seat: WlSeat,
) {}
}
impl<A: App> PopupHandler for AppData<A>
{
fn configure(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
popup: &Popup,
config: PopupConfigure,
)
{
let ( w, h ) = ( config.width.max( 1 ) as u32, config.height.max( 1 ) as u32 );
if let Some( super::SurfaceFocus::Overlay( id ) ) = self.focus_for_surface( popup.wl_surface() )
{
if let Some( ss ) = self.overlays.get_mut( &id )
{
ss.on_configure( &self.shm, self.egl_context.as_ref(), w, h );
}
}
}
fn done(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
popup: &Popup,
)
{
if let Some( super::SurfaceFocus::Overlay( id ) ) = self.focus_for_surface( popup.wl_surface() )
{
if let Some( msg ) = self.overlay_dismiss_msg( id )
{
self.pending_msgs.push( msg );
}
self.discard_overlay( id );
}
}
}
impl<A: App> SessionLockHandler for AppData<A>
{
fn locked( &mut self, _conn: &Connection, qh: &QueueHandle<Self>, session_lock: SessionLock )
{
if let Some( output ) = self.output_state.outputs().next()
{
let surface = self.compositor_state.create_surface( qh );
let lock_surface = session_lock.create_lock_surface( surface, &output, qh );
self.main.surface = super::SurfaceKind::Lock( lock_surface );
}
self.session_lock = Some( session_lock );
}
fn finished( &mut self, _conn: &Connection, _qh: &QueueHandle<Self>, _session_lock: SessionLock )
{
self.exit_requested = true;
}
fn configure(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_surface: SessionLockSurface,
configure: SessionLockSurfaceConfigure,
_serial: u32,
)
{
let ( w, h ) = configure.new_size;
self.on_configure( w.max( 1 ), h.max( 1 ) );
}
}
delegate_compositor!( @<A: App> AppData<A> );
delegate_subcompositor!( @<A: App> AppData<A> );
delegate_output!( @<A: App> AppData<A> );
delegate_shm!( @<A: App> AppData<A> );
delegate_seat!( @<A: App> AppData<A> );
delegate_keyboard!( @<A: App> AppData<A> );
delegate_pointer!( @<A: App> AppData<A> );
delegate_touch!( @<A: App> AppData<A> );
delegate_layer!( @<A: App> AppData<A> );
delegate_session_lock!( @<A: App> AppData<A> );
delegate_xdg_shell!( @<A: App> AppData<A> );
delegate_xdg_window!( @<A: App> AppData<A> );
delegate_xdg_popup!( @<A: App> AppData<A> );
delegate_foreign_toplevel_list!( @<A: App> AppData<A> );
delegate_registry!( @<A: App> AppData<A> );
smithay_client_toolkit::delegate_activation!( @<A: App> AppData<A> );
smithay_client_toolkit::delegate_data_device!( @<A: App> AppData<A> );
impl<A: App> smithay_client_toolkit::activation::ActivationHandler for AppData<A>
{
type RequestData = smithay_client_toolkit::activation::RequestData;
fn new_token( &mut self, token: String, _data: &Self::RequestData )
{
if let ( Some( ref activation ), Some( wl ) ) = ( self.activation_state.as_ref(), self.main.surface.try_wl_surface() )
{
activation.activate::<Self>( &wl, token );
}
}
}
impl<A: App> ProvidesRegistryState for AppData<A>
{
fn registry( &mut self ) -> &mut RegistryState
{
&mut self.registry_state
}
registry_handlers![ OutputState, SeatState ];
}
fn toplevel_display_id(
list: &ForeignToplevelList,
handle: &ExtForeignToplevelHandleV1,
) -> String
{
let Some( info ) = list.info( handle ) else { return String::new(); };
info.app_id
}
impl<A: App> ForeignToplevelListHandler for AppData<A>
{
fn foreign_toplevel_list_state( &mut self ) -> &mut ForeignToplevelList
{
&mut self.foreign_toplevel_list
}
fn new_toplevel(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
handle: ExtForeignToplevelHandleV1,
)
{
let id = handle.id().protocol_id();
let app_id = toplevel_display_id( &self.foreign_toplevel_list, &handle );
if let Some( msg ) = self.app.on_toplevel_event( ToplevelEvent::Opened { id, app_id } )
{
self.pending_msgs.push( msg );
}
}
fn update_toplevel(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
handle: ExtForeignToplevelHandleV1,
)
{
let id = handle.id().protocol_id();
let app_id = toplevel_display_id( &self.foreign_toplevel_list, &handle );
if let Some( msg ) = self.app.on_toplevel_event( ToplevelEvent::Opened { id, app_id } )
{
self.pending_msgs.push( msg );
}
}
fn toplevel_closed(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
handle: ExtForeignToplevelHandleV1,
)
{
let id = handle.id().protocol_id();
if let Some( msg ) = self.app.on_toplevel_event( ToplevelEvent::Closed { id } )
{
self.pending_msgs.push( msg );
}
}
}
impl<A: App> Dispatch<ZwpTextInputManagerV3, ()> for AppData<A>
{
fn event(
_state: &mut Self,
_proxy: &ZwpTextInputManagerV3,
_event: <ZwpTextInputManagerV3 as smithay_client_toolkit::reexports::client::Proxy>::Event,
_data: &(),
_conn: &Connection,
_qh: &QueueHandle<Self>,
)
{
}
}
impl<A: App> Dispatch<WlCallback, super::SurfaceFocus> for AppData<A>
{
fn event(
state: &mut Self,
_proxy: &WlCallback,
_event: wl_callback::Event,
focus: &super::SurfaceFocus,
_conn: &Connection,
_qh: &QueueHandle<Self>,
)
{
let is_animating = state.app.is_animating();
match *focus
{
super::SurfaceFocus::Main =>
{
state.main.frame_pending = false;
if is_animating
{
if state.app.subsurface_motion_only()
{
if let Some( wl ) = state.main.surface.try_wl_surface().cloned()
{
let _ = wl.frame( &state.qh, super::SurfaceFocus::Main );
wl.commit();
state.main.frame_pending = true;
}
}
else
{
state.view_dirty = true;
state.main.request_redraw();
}
}
}
super::SurfaceFocus::Overlay( id ) =>
{
if let Some( ss ) = state.overlays.get_mut( &id )
{
ss.frame_pending = false;
if is_animating
{
state.overlays_dirty = true;
ss.request_redraw();
}
}
}
}
}
}
impl<A: App> Dispatch<ZwpTextInputV3, ()> for AppData<A>
{
fn event(
state: &mut Self,
_proxy: &ZwpTextInputV3,
event: zwp_text_input_v3::Event,
_data: &(),
_conn: &Connection,
_qh: &QueueHandle<Self>,
)
{
let focus = state.keyboard_focus;
match event
{
zwp_text_input_v3::Event::CommitString { text } =>
{
if let Some( text ) = text
{
if !text.is_empty()
{
state.handle_text_insert( focus, &text );
}
}
}
zwp_text_input_v3::Event::DeleteSurroundingText { before_length, after_length } =>
{
state.handle_delete_surrounding( focus, before_length, after_length );
}
zwp_text_input_v3::Event::Done { .. } =>
{
if let Some( ss ) = state.try_surface_mut( focus )
{
ss.request_redraw();
}
}
zwp_text_input_v3::Event::Enter { .. } =>
{
state.reenable_text_input();
}
_ => {}
}
}
}