Store Config as Rc<RefCell<>> field

This commit is contained in:
Ivan Molodetskikh
2023-09-14 22:28:26 +04:00
parent 092095ead0
commit 89f9e11f65
5 changed files with 52 additions and 34 deletions
+2 -4
View File
@@ -7,7 +7,6 @@ use smithay::backend::renderer::gles::GlesRenderer;
use smithay::output::Output; use smithay::output::Output;
use smithay::wayland::dmabuf::DmabufFeedback; use smithay::wayland::dmabuf::DmabufFeedback;
use crate::config::Config;
use crate::input::CompositorMod; use crate::input::CompositorMod;
use crate::niri::OutputRenderElements; use crate::niri::OutputRenderElements;
use crate::Niri; use crate::Niri;
@@ -47,14 +46,13 @@ impl Backend {
pub fn render( pub fn render(
&mut self, &mut self,
config: &Config,
niri: &mut Niri, niri: &mut Niri,
output: &Output, output: &Output,
elements: &[OutputRenderElements<GlesRenderer>], elements: &[OutputRenderElements<GlesRenderer>],
) -> Option<&DmabufFeedback> { ) -> Option<&DmabufFeedback> {
match self { match self {
Backend::Tty(tty) => tty.render(config, niri, output, elements), Backend::Tty(tty) => tty.render(niri, output, elements),
Backend::Winit(winit) => winit.render(config, niri, output, elements), Backend::Winit(winit) => winit.render(niri, output, elements),
} }
} }
+11 -3
View File
@@ -1,6 +1,8 @@
use std::cell::RefCell;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::os::fd::FromRawFd; use std::os::fd::FromRawFd;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::sync::{Mutex, Arc}; use std::sync::{Mutex, Arc};
use std::time::Duration; use std::time::Duration;
@@ -42,6 +44,7 @@ const BACKGROUND_COLOR: [f32; 4] = [0.1, 0.1, 0.1, 1.];
const SUPPORTED_COLOR_FORMATS: &[Fourcc] = &[Fourcc::Argb8888, Fourcc::Abgr8888]; const SUPPORTED_COLOR_FORMATS: &[Fourcc] = &[Fourcc::Argb8888, Fourcc::Abgr8888];
pub struct Tty { pub struct Tty {
config: Rc<RefCell<Config>>,
session: LibSeatSession, session: LibSeatSession,
udev_dispatcher: Dispatcher<'static, UdevBackend, LoopData>, udev_dispatcher: Dispatcher<'static, UdevBackend, LoopData>,
primary_gpu_path: PathBuf, primary_gpu_path: PathBuf,
@@ -88,7 +91,7 @@ struct Surface {
} }
impl Tty { impl Tty {
pub fn new(event_loop: LoopHandle<'static, LoopData>) -> Self { pub fn new(config: Rc<RefCell<Config>>, event_loop: LoopHandle<'static, LoopData>) -> Self {
let (session, notifier) = LibSeatSession::new().unwrap(); let (session, notifier) = LibSeatSession::new().unwrap();
let seat_name = session.seat(); let seat_name = session.seat();
@@ -237,6 +240,7 @@ impl Tty {
let primary_gpu_path = udev::primary_gpu(&seat_name).unwrap().unwrap(); let primary_gpu_path = udev::primary_gpu(&seat_name).unwrap().unwrap();
Self { Self {
config,
session, session,
udev_dispatcher, udev_dispatcher,
primary_gpu_path, primary_gpu_path,
@@ -627,7 +631,6 @@ impl Tty {
pub fn render( pub fn render(
&mut self, &mut self,
config: &Config,
niri: &mut Niri, niri: &mut Niri,
output: &Output, output: &Output,
elements: &[OutputRenderElements<GlesRenderer>], elements: &[OutputRenderElements<GlesRenderer>],
@@ -647,7 +650,12 @@ impl Tty {
Ok(res) => { Ok(res) => {
assert!(!res.needs_sync()); assert!(!res.needs_sync());
if config.debug.wait_for_frame_completion_before_queueing { if self
.config
.borrow()
.debug
.wait_for_frame_completion_before_queueing
{
if let PrimaryPlaneElement::Swapchain(element) = res.primary_element { if let PrimaryPlaneElement::Swapchain(element) = res.primary_element {
let _span = tracy_client::span!("wait for completion"); let _span = tracy_client::span!("wait for completion");
element.sync.wait(); element.sync.wait();
+11 -3
View File
@@ -1,4 +1,6 @@
use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::Duration; use std::time::Duration;
@@ -21,6 +23,7 @@ use crate::utils::get_monotonic_time;
use crate::{LoopData, Niri}; use crate::{LoopData, Niri};
pub struct Winit { pub struct Winit {
config: Rc<RefCell<Config>>,
output: Output, output: Output,
backend: WinitGraphicsBackend<GlesRenderer>, backend: WinitGraphicsBackend<GlesRenderer>,
damage_tracker: OutputDamageTracker, damage_tracker: OutputDamageTracker,
@@ -28,7 +31,7 @@ pub struct Winit {
} }
impl Winit { impl Winit {
pub fn new(event_loop: LoopHandle<LoopData>) -> Self { pub fn new(config: Rc<RefCell<Config>>, event_loop: LoopHandle<LoopData>) -> Self {
let builder = WindowBuilder::new() let builder = WindowBuilder::new()
.with_inner_size(LogicalSize::new(1280.0, 800.0)) .with_inner_size(LogicalSize::new(1280.0, 800.0))
// .with_resizable(false) // .with_resizable(false)
@@ -105,6 +108,7 @@ impl Winit {
.unwrap(); .unwrap();
Self { Self {
config,
output, output,
backend, backend,
damage_tracker, damage_tracker,
@@ -137,7 +141,6 @@ impl Winit {
pub fn render( pub fn render(
&mut self, &mut self,
config: &Config,
niri: &mut Niri, niri: &mut Niri,
output: &Output, output: &Output,
elements: &[OutputRenderElements<GlesRenderer>], elements: &[OutputRenderElements<GlesRenderer>],
@@ -151,7 +154,12 @@ impl Winit {
.render_output(self.backend.renderer(), age, elements, [0.1, 0.1, 0.1, 1.0]) .render_output(self.backend.renderer(), age, elements, [0.1, 0.1, 0.1, 1.0])
.unwrap(); .unwrap();
if let Some(damage) = res.damage { if let Some(damage) = res.damage {
if config.debug.wait_for_frame_completion_before_queueing { if self
.config
.borrow()
.debug
.wait_for_frame_completion_before_queueing
{
let _span = tracy_client::span!("wait for completion"); let _span = tracy_client::span!("wait for completion");
res.sync.wait(); res.sync.wait();
} }
+3 -2
View File
@@ -123,7 +123,8 @@ impl State {
time, time,
|self_, mods, keysym| { |self_, mods, keysym| {
if event.state() == KeyState::Pressed { if event.state() == KeyState::Pressed {
action(&self_.config, comp_mod, keysym, *mods).into() let config = self_.niri.config.borrow();
action(&config, comp_mod, keysym, *mods).into()
} else { } else {
FilterResult::Forward FilterResult::Forward
} }
@@ -743,7 +744,7 @@ impl State {
// According to Mutter code, this setting is specific to touchpads. // According to Mutter code, this setting is specific to touchpads.
let is_touchpad = device.config_tap_finger_count() > 0; let is_touchpad = device.config_tap_finger_count() > 0;
if is_touchpad { if is_touchpad {
let c = &self.config.input.touchpad; let c = &self.niri.config.borrow().input.touchpad;
let _ = device.config_tap_set_enabled(c.tap); let _ = device.config_tap_set_enabled(c.tap);
let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll); let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll);
let _ = device.config_accel_set_speed(c.accel_speed); let _ = device.config_accel_set_speed(c.accel_speed);
+25 -22
View File
@@ -1,6 +1,8 @@
use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::os::unix::io::AsRawFd; use std::os::unix::io::AsRawFd;
use std::process::Command; use std::process::Command;
use std::rc::Rc;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::Duration; use std::time::Duration;
use std::{env, thread}; use std::{env, thread};
@@ -68,6 +70,8 @@ use crate::utils::{center, get_monotonic_time, load_default_cursor};
use crate::LoopData; use crate::LoopData;
pub struct Niri { pub struct Niri {
pub config: Rc<RefCell<Config>>,
pub event_loop: LoopHandle<'static, LoopData>, pub event_loop: LoopHandle<'static, LoopData>,
pub stop_signal: LoopSignal, pub stop_signal: LoopSignal,
pub display_handle: DisplayHandle, pub display_handle: DisplayHandle,
@@ -125,7 +129,6 @@ pub struct OutputState {
} }
pub struct State { pub struct State {
pub config: Config,
pub backend: Backend, pub backend: Backend,
pub niri: Niri, pub niri: Niri,
} }
@@ -137,23 +140,21 @@ impl State {
stop_signal: LoopSignal, stop_signal: LoopSignal,
display: &mut Display<State>, display: &mut Display<State>,
) -> Self { ) -> Self {
let config = Rc::new(RefCell::new(config));
let has_display = let has_display =
env::var_os("WAYLAND_DISPLAY").is_some() || env::var_os("DISPLAY").is_some(); env::var_os("WAYLAND_DISPLAY").is_some() || env::var_os("DISPLAY").is_some();
let mut backend = if has_display { let mut backend = if has_display {
Backend::Winit(Winit::new(event_loop.clone())) Backend::Winit(Winit::new(config.clone(), event_loop.clone()))
} else { } else {
Backend::Tty(Tty::new(event_loop.clone())) Backend::Tty(Tty::new(config.clone(), event_loop.clone()))
}; };
let mut niri = Niri::new(&config, event_loop, stop_signal, display, &backend); let mut niri = Niri::new(config.clone(), event_loop, stop_signal, display, &backend);
backend.init(&mut niri); backend.init(&mut niri);
Self { Self { backend, niri }
config,
backend,
niri,
}
} }
pub fn move_cursor(&mut self, location: Point<f64, Logical>) { pub fn move_cursor(&mut self, location: Point<f64, Logical>) {
@@ -194,13 +195,14 @@ impl State {
impl Niri { impl Niri {
pub fn new( pub fn new(
config: &Config, config: Rc<RefCell<Config>>,
event_loop: LoopHandle<'static, LoopData>, event_loop: LoopHandle<'static, LoopData>,
stop_signal: LoopSignal, stop_signal: LoopSignal,
display: &mut Display<State>, display: &mut Display<State>,
backend: &Backend, backend: &Backend,
) -> Self { ) -> Self {
let display_handle = display.handle(); let display_handle = display.handle();
let config_ = config.borrow();
let compositor_state = CompositorState::new::<State>(&display_handle); let compositor_state = CompositorState::new::<State>(&display_handle);
let xdg_shell_state = XdgShellState::new_with_capabilities::<State>( let xdg_shell_state = XdgShellState::new_with_capabilities::<State>(
@@ -220,11 +222,11 @@ impl Niri {
let mut seat: Seat<State> = seat_state.new_wl_seat(&display_handle, backend.seat_name()); let mut seat: Seat<State> = seat_state.new_wl_seat(&display_handle, backend.seat_name());
let xkb = XkbConfig { let xkb = XkbConfig {
rules: &config.input.keyboard.xkb.rules, rules: &config_.input.keyboard.xkb.rules,
model: &config.input.keyboard.xkb.model, model: &config_.input.keyboard.xkb.model,
layout: config.input.keyboard.xkb.layout.as_deref().unwrap_or("us"), layout: config_.input.keyboard.xkb.layout.as_deref().unwrap_or("us"),
variant: &config.input.keyboard.xkb.variant, variant: &config_.input.keyboard.xkb.variant,
options: config.input.keyboard.xkb.options.clone(), options: config_.input.keyboard.xkb.options.clone(),
}; };
seat.add_keyboard(xkb, 400, 30).unwrap(); seat.add_keyboard(xkb, 400, 30).unwrap();
seat.add_pointer(); seat.add_pointer();
@@ -387,7 +389,7 @@ impl Niri {
) )
.unwrap(); .unwrap();
if pipewire.is_some() && !config.debug.screen_cast_in_non_session_instances { if pipewire.is_some() && !config_.debug.screen_cast_in_non_session_instances {
conn = conn conn = conn
.name("org.gnome.Mutter.ScreenCast") .name("org.gnome.Mutter.ScreenCast")
.unwrap() .unwrap()
@@ -435,7 +437,7 @@ impl Niri {
warn!("error inhibiting power key: {err:?}"); warn!("error inhibiting power key: {err:?}");
} }
} }
} else if pipewire.is_some() && config.debug.screen_cast_in_non_session_instances { } else if pipewire.is_some() && config_.debug.screen_cast_in_non_session_instances {
let conn = zbus::blocking::ConnectionBuilder::session() let conn = zbus::blocking::ConnectionBuilder::session()
.unwrap() .unwrap()
.name("org.gnome.Mutter.ScreenCast") .name("org.gnome.Mutter.ScreenCast")
@@ -466,7 +468,10 @@ impl Niri {
}) })
.unwrap(); .unwrap();
drop(config_);
Self { Self {
config,
event_loop, event_loop,
stop_signal, stop_signal,
display_handle, display_handle,
@@ -690,9 +695,7 @@ impl Niri {
// Timer::immediate() adds a millisecond of delay for some reason. // Timer::immediate() adds a millisecond of delay for some reason.
// This should be fixed in calloop v0.11: https://github.com/Smithay/calloop/issues/142 // This should be fixed in calloop v0.11: https://github.com/Smithay/calloop/issues/142
let idle = self.event_loop.insert_idle(move |data| { let idle = self.event_loop.insert_idle(move |data| {
data.state data.state.niri.redraw(&mut data.state.backend, &output);
.niri
.redraw(&data.state.config, &mut data.state.backend, &output);
}); });
state.queued_redraw = Some(idle); state.queued_redraw = Some(idle);
} }
@@ -837,7 +840,7 @@ impl Niri {
elements elements
} }
fn redraw(&mut self, config: &Config, backend: &mut Backend, output: &Output) { fn redraw(&mut self, backend: &mut Backend, output: &Output) {
let _span = tracy_client::span!("Niri::redraw"); let _span = tracy_client::span!("Niri::redraw");
let state = self.output_state.get_mut(output).unwrap(); let state = self.output_state.get_mut(output).unwrap();
@@ -854,7 +857,7 @@ impl Niri {
let elements = self.render(backend.renderer(), output); let elements = self.render(backend.renderer(), output);
// Hand it over to the backend. // Hand it over to the backend.
let dmabuf_feedback = backend.render(config, self, output, &elements); let dmabuf_feedback = backend.render(self, output, &elements);
// Send the dmabuf feedbacks. // Send the dmabuf feedbacks.
if let Some(feedback) = dmabuf_feedback { if let Some(feedback) = dmabuf_feedback {