refactor: minor on windowing system

WIP/draft
drendog 2024-08-20 23:35:27 +02:00
parent 96da484043
commit a90f3d80c2
Signed by: dwenya
GPG Key ID: 8DD77074645332D0
2 changed files with 47 additions and 49 deletions

View File

@ -26,7 +26,7 @@ macro_rules! bind_globals {
let $name: $interface = $global_list.bind($queue_handle, $version, ()) let $name: $interface = $global_list.bind($queue_handle, $version, ())
.with_context(|| format!("Failed to bind {}", stringify!($name)))?; .with_context(|| format!("Failed to bind {}", stringify!($name)))?;
)+ )+
Ok(($($name),+)) Ok::<($($interface,)+), anyhow::Error>(($($name,)+))
} }
}; };
} }

View File

@ -40,27 +40,21 @@ impl WindowingSystem {
fn new(config: &mut config::WindowConfig) -> Result<Self> { fn new(config: &mut config::WindowConfig) -> Result<Self> {
info!("Initializing WindowingSystem"); info!("Initializing WindowingSystem");
let connection = Rc::new(Connection::connect_to_env()?); let connection = Rc::new(Connection::connect_to_env()?);
let global_list = Self::initialize_registry(&connection)?;
let event_queue = connection.new_event_queue(); let event_queue = connection.new_event_queue();
let (compositor, output, layer_shell, seat) = let (compositor, output, layer_shell, seat) =
Self::bind_globals(&global_list, &event_queue.handle())?; Self::initialize_globals(&connection, &event_queue.handle())?;
let surface = Rc::new(compositor.create_surface(&event_queue.handle(), ())); let (surface, layer_surface) = Self::setup_surface(
let layer_surface = Rc::new(layer_shell.get_layer_surface( &compositor,
&surface, &output,
Some(&output), &layer_shell,
config.layer,
config.namespace.clone(),
&event_queue.handle(), &event_queue.handle(),
(), config,
)); )?;
let pointer = Rc::new(seat.get_pointer(&event_queue.handle(), ())); let pointer = Rc::new(seat.get_pointer(&event_queue.handle(), ()));
Self::configure_layer_surface(&layer_surface, &surface, config);
let mut state_builder = WindowStateBuilder::new() let mut state_builder = WindowStateBuilder::new()
.component_definition(config.component_definition.take().unwrap()) .component_definition(config.component_definition.take().unwrap())
.surface(Rc::clone(&surface)) .surface(Rc::clone(&surface))
@ -70,14 +64,10 @@ impl WindowingSystem {
.height(config.height) .height(config.height)
.exclusive_zone(config.exclusive_zone); .exclusive_zone(config.exclusive_zone);
//Self::wait_for_configure(&mut event_queue, &mut state_builder)?; let window = Self::initialize_renderer(&state_builder, &connection.display(), config)?;
let display = connection.display();
let window = Self::initialize_renderer(&state_builder, &display, config)?;
state_builder = state_builder.window(window); state_builder = state_builder.window(window);
let state = state_builder.build()?; let state = state_builder.build()?;
let event_loop = EventLoop::try_new().context("Failed to create event loop")?; let event_loop = EventLoop::try_new().context("Failed to create event loop")?;
Ok(Self { Ok(Self {
@ -88,24 +78,46 @@ impl WindowingSystem {
}) })
} }
fn initialize_registry(connection: &Connection) -> Result<GlobalList> { fn initialize_globals(
registry_queue_init::<WindowState>(connection) connection: &Connection,
.map(|(global_list, _)| global_list)
.context("Failed to initialize registry")
}
fn bind_globals(
global_list: &GlobalList,
queue_handle: &QueueHandle<WindowState>, queue_handle: &QueueHandle<WindowState>,
) -> Result<(WlCompositor, WlOutput, ZwlrLayerShellV1, WlSeat)> { ) -> Result<(WlCompositor, WlOutput, ZwlrLayerShellV1, WlSeat)> {
bind_globals!( let global_list = registry_queue_init::<WindowState>(connection)
global_list, .map(|(global_list, _)| global_list)
.context("Failed to initialize registry")?;
let (compositor, output, layer_shell, seat) = bind_globals!(
&global_list,
queue_handle, queue_handle,
(WlCompositor, compositor, 1..=1), (WlCompositor, compositor, 1..=1),
(WlOutput, output, 1..=1), (WlOutput, output, 1..=1),
(ZwlrLayerShellV1, layer_shell, 1..=1), (ZwlrLayerShellV1, layer_shell, 1..=1),
(WlSeat, seat, 1..=1) (WlSeat, seat, 1..=1)
) )?;
Ok((compositor, output, layer_shell, seat))
}
fn setup_surface(
compositor: &WlCompositor,
output: &WlOutput,
layer_shell: &ZwlrLayerShellV1,
queue_handle: &QueueHandle<WindowState>,
config: &config::WindowConfig,
) -> Result<(Rc<WlSurface>, Rc<ZwlrLayerSurfaceV1>)> {
let surface = Rc::new(compositor.create_surface(queue_handle, ()));
let layer_surface = Rc::new(layer_shell.get_layer_surface(
&surface,
Some(output),
config.layer,
config.namespace.clone(),
queue_handle,
(),
));
Self::configure_layer_surface(&layer_surface, &surface, config);
Ok((surface, layer_surface))
} }
fn configure_layer_surface( fn configure_layer_surface(
@ -127,44 +139,30 @@ impl WindowingSystem {
surface.commit(); surface.commit();
} }
fn create_renderer( fn initialize_renderer(
state_builder: &WindowStateBuilder, state_builder: &WindowStateBuilder,
display: &WlDisplay, display: &WlDisplay,
) -> Result<FemtoVGRenderer> { config: &config::WindowConfig,
) -> Result<Rc<FemtoVGWindow>> {
let size = state_builder.size.unwrap_or(PhysicalSize::new(1, 1)); let size = state_builder.size.unwrap_or(PhysicalSize::new(1, 1));
let surface = state_builder let surface = state_builder
.surface .surface
.as_ref() .as_ref()
.ok_or_else(|| anyhow::anyhow!("Failed to get surface"))?; .ok_or_else(|| anyhow::anyhow!("Failed to get surface"))?;
debug!("Creating EGL context with size: {:?}", size);
let context = EGLContext::builder() let context = EGLContext::builder()
.with_display_id(display.id()) .with_display_id(display.id())
.with_surface_id(surface.id()) .with_surface_id(surface.id())
.with_size(size) .with_size(size)
.build() .build()?;
.map_err(|e| anyhow::anyhow!("Failed to create EGL context: {:?}", e))?;
debug!("Creating FemtoVGRenderer"); let renderer = FemtoVGRenderer::new(context).context("Failed to create FemtoVGRenderer")?;
FemtoVGRenderer::new(context).context("Failed to create FemtoVGRenderer")
}
fn initialize_renderer(
state_builder: &WindowStateBuilder,
display: &WlDisplay,
config: &config::WindowConfig,
) -> Result<Rc<FemtoVGWindow>> {
let renderer = Self::create_renderer(state_builder, display)?;
let femtovg_window = FemtoVGWindow::new(renderer); let femtovg_window = FemtoVGWindow::new(renderer);
let size = state_builder.size.unwrap_or_default();
info!("Initializing UI with size: {:?}", size);
femtovg_window.set_size(slint::WindowSize::Physical(size)); femtovg_window.set_size(slint::WindowSize::Physical(size));
femtovg_window.set_scale_factor(config.scale_factor); femtovg_window.set_scale_factor(config.scale_factor);
femtovg_window.set_position(LogicalPosition::new(0., 0.)); femtovg_window.set_position(LogicalPosition::new(0., 0.));
debug!("Creating Slint component instance");
Ok(femtovg_window) Ok(femtovg_window)
} }