mirror of
https://github.com/niri-wm/niri.git
synced 2026-06-23 02:05:33 +07:00
tty: Extract other event handlers into functions
This commit is contained in:
+76
-72
@@ -48,6 +48,7 @@ pub struct Tty {
|
|||||||
config: Rc<RefCell<Config>>,
|
config: Rc<RefCell<Config>>,
|
||||||
session: LibSeatSession,
|
session: LibSeatSession,
|
||||||
udev_dispatcher: Dispatcher<'static, UdevBackend, State>,
|
udev_dispatcher: Dispatcher<'static, UdevBackend, State>,
|
||||||
|
libinput: Libinput,
|
||||||
primary_gpu_path: PathBuf,
|
primary_gpu_path: PathBuf,
|
||||||
output_device: Option<OutputDevice>,
|
output_device: Option<OutputDevice>,
|
||||||
connectors: Arc<Mutex<HashMap<String, Output>>>,
|
connectors: Arc<Mutex<HashMap<String, Output>>>,
|
||||||
@@ -100,37 +101,7 @@ impl Tty {
|
|||||||
|
|
||||||
let udev_backend = UdevBackend::new(session.seat()).unwrap();
|
let udev_backend = UdevBackend::new(session.seat()).unwrap();
|
||||||
let udev_dispatcher = Dispatcher::new(udev_backend, move |event, _, state: &mut State| {
|
let udev_dispatcher = Dispatcher::new(udev_backend, move |event, _, state: &mut State| {
|
||||||
let tty = state.backend.tty();
|
state.backend.tty().on_udev_event(&mut state.niri, event);
|
||||||
let niri = &mut state.niri;
|
|
||||||
|
|
||||||
match event {
|
|
||||||
UdevEvent::Added { device_id, path } => {
|
|
||||||
if !tty.session.is_active() {
|
|
||||||
debug!("skipping UdevEvent::Added as session is inactive");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Err(err) = tty.device_added(device_id, &path, niri) {
|
|
||||||
warn!("error adding device: {err:?}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
UdevEvent::Changed { device_id } => {
|
|
||||||
if !tty.session.is_active() {
|
|
||||||
debug!("skipping UdevEvent::Changed as session is inactive");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
tty.device_changed(device_id, niri)
|
|
||||||
}
|
|
||||||
UdevEvent::Removed { device_id } => {
|
|
||||||
if !tty.session.is_active() {
|
|
||||||
debug!("skipping UdevEvent::Removed as session is inactive");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
tty.device_removed(device_id, niri)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
event_loop
|
event_loop
|
||||||
.register_dispatcher(udev_dispatcher.clone())
|
.register_dispatcher(udev_dispatcher.clone())
|
||||||
@@ -147,56 +118,110 @@ impl Tty {
|
|||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let udev_dispatcher_c = udev_dispatcher.clone();
|
|
||||||
event_loop
|
event_loop
|
||||||
.insert_source(notifier, move |event, _, state| {
|
.insert_source(notifier, move |event, _, state| {
|
||||||
let tty = state.backend.tty();
|
state.backend.tty().on_session_event(&mut state.niri, event);
|
||||||
let niri = &mut state.niri;
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let primary_gpu_path = udev::primary_gpu(&seat_name).unwrap().unwrap();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
config,
|
||||||
|
session,
|
||||||
|
udev_dispatcher,
|
||||||
|
libinput,
|
||||||
|
primary_gpu_path,
|
||||||
|
output_device: None,
|
||||||
|
connectors: Arc::new(Mutex::new(HashMap::new())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init(&mut self, niri: &mut Niri) {
|
||||||
|
for (device_id, path) in self.udev_dispatcher.clone().as_source_ref().device_list() {
|
||||||
|
if let Err(err) = self.device_added(device_id, path, niri) {
|
||||||
|
warn!("error adding device: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_udev_event(&mut self, niri: &mut Niri, event: UdevEvent) {
|
||||||
|
match event {
|
||||||
|
UdevEvent::Added { device_id, path } => {
|
||||||
|
if !self.session.is_active() {
|
||||||
|
debug!("skipping UdevEvent::Added as session is inactive");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Err(err) = self.device_added(device_id, &path, niri) {
|
||||||
|
warn!("error adding device: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
UdevEvent::Changed { device_id } => {
|
||||||
|
if !self.session.is_active() {
|
||||||
|
debug!("skipping UdevEvent::Changed as session is inactive");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.device_changed(device_id, niri)
|
||||||
|
}
|
||||||
|
UdevEvent::Removed { device_id } => {
|
||||||
|
if !self.session.is_active() {
|
||||||
|
debug!("skipping UdevEvent::Removed as session is inactive");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.device_removed(device_id, niri)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_session_event(&mut self, niri: &mut Niri, event: SessionEvent) {
|
||||||
match event {
|
match event {
|
||||||
SessionEvent::PauseSession => {
|
SessionEvent::PauseSession => {
|
||||||
debug!("pausing session");
|
debug!("pausing session");
|
||||||
|
|
||||||
libinput.suspend();
|
self.libinput.suspend();
|
||||||
|
|
||||||
if let Some(output_device) = &tty.output_device {
|
if let Some(output_device) = &self.output_device {
|
||||||
output_device.drm.pause();
|
output_device.drm.pause();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SessionEvent::ActivateSession => {
|
SessionEvent::ActivateSession => {
|
||||||
debug!("resuming session");
|
debug!("resuming session");
|
||||||
|
|
||||||
if libinput.resume().is_err() {
|
if self.libinput.resume().is_err() {
|
||||||
error!("error resuming libinput");
|
error!("error resuming libinput");
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(output_device) = &mut tty.output_device {
|
if let Some(output_device) = &mut self.output_device {
|
||||||
// We had an output device, check if it's been removed.
|
// We had an output device, check if it's been removed.
|
||||||
let output_device_id = output_device.id;
|
let output_device_id = output_device.id;
|
||||||
if !udev_dispatcher_c
|
if !self
|
||||||
|
.udev_dispatcher
|
||||||
.as_source_ref()
|
.as_source_ref()
|
||||||
.device_list()
|
.device_list()
|
||||||
.any(|(device_id, _)| device_id == output_device_id)
|
.any(|(device_id, _)| device_id == output_device_id)
|
||||||
{
|
{
|
||||||
// The output device, if we had any, has been removed.
|
// The output device, if we had any, has been removed.
|
||||||
tty.device_removed(output_device_id, niri);
|
self.device_removed(output_device_id, niri);
|
||||||
} else {
|
} else {
|
||||||
// It hasn't been removed, update its state as usual.
|
// It hasn't been removed, update its state as usual.
|
||||||
output_device.drm.activate();
|
output_device.drm.activate();
|
||||||
|
|
||||||
// HACK: force reset the connectors to make resuming work across
|
// HACK: force reset the connectors to make resuming work across
|
||||||
// sleep.
|
// sleep.
|
||||||
let output_device = tty.output_device.as_mut().unwrap();
|
let output_device = self.output_device.as_mut().unwrap();
|
||||||
let crtcs: Vec<_> = output_device
|
let crtcs: Vec<_> = output_device
|
||||||
.drm_scanner
|
.drm_scanner
|
||||||
.crtcs()
|
.crtcs()
|
||||||
.map(|(conn, crtc)| (conn.clone(), crtc))
|
.map(|(conn, crtc)| (conn.clone(), crtc))
|
||||||
.collect();
|
.collect();
|
||||||
for (conn, crtc) in crtcs {
|
for (conn, crtc) in crtcs {
|
||||||
tty.connector_disconnected(niri, conn, crtc);
|
self.connector_disconnected(niri, conn, crtc);
|
||||||
}
|
}
|
||||||
|
|
||||||
let output_device = tty.output_device.as_mut().unwrap();
|
let output_device = self.output_device.as_mut().unwrap();
|
||||||
let _ = output_device
|
let _ = output_device
|
||||||
.drm_scanner
|
.drm_scanner
|
||||||
.scan_connectors(&output_device.drm);
|
.scan_connectors(&output_device.drm);
|
||||||
@@ -206,16 +231,16 @@ impl Tty {
|
|||||||
.map(|(conn, crtc)| (conn.clone(), crtc))
|
.map(|(conn, crtc)| (conn.clone(), crtc))
|
||||||
.collect();
|
.collect();
|
||||||
for (conn, crtc) in crtcs {
|
for (conn, crtc) in crtcs {
|
||||||
if let Err(err) = tty.connector_connected(niri, conn, crtc) {
|
if let Err(err) = self.connector_connected(niri, conn, crtc) {
|
||||||
warn!("error connecting connector: {err:?}");
|
warn!("error connecting connector: {err:?}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// // Refresh the connectors.
|
// // Refresh the connectors.
|
||||||
// tty.device_changed(output_device_id, niri);
|
// self.device_changed(output_device_id, niri);
|
||||||
|
|
||||||
// // Refresh the state on unchanged connectors.
|
// // Refresh the state on unchanged connectors.
|
||||||
// let output_device = tty.output_device.as_mut().unwrap();
|
// let output_device = self.output_device.as_mut().unwrap();
|
||||||
// for drm_compositor in output_device.surfaces.values_mut() {
|
// for drm_compositor in output_device.surfaces.values_mut() {
|
||||||
// if let Err(err) = drm_compositor.surface().reset_state() {
|
// if let Err(err) = drm_compositor.surface().reset_state() {
|
||||||
// warn!("error resetting DRM surface state: {err}");
|
// warn!("error resetting DRM surface state: {err}");
|
||||||
@@ -227,37 +252,16 @@ impl Tty {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// We didn't have an output device, check if it's been added.
|
// We didn't have an output device, check if it's been added.
|
||||||
for (device_id, path) in udev_dispatcher_c.as_source_ref().device_list()
|
let udev_dispatcher = self.udev_dispatcher.clone();
|
||||||
{
|
for (device_id, path) in udev_dispatcher.as_source_ref().device_list() {
|
||||||
if let Err(err) = tty.device_added(device_id, path, niri) {
|
|
||||||
warn!("error adding device: {err:?}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let primary_gpu_path = udev::primary_gpu(&seat_name).unwrap().unwrap();
|
|
||||||
|
|
||||||
Self {
|
|
||||||
config,
|
|
||||||
session,
|
|
||||||
udev_dispatcher,
|
|
||||||
primary_gpu_path,
|
|
||||||
output_device: None,
|
|
||||||
connectors: Arc::new(Mutex::new(HashMap::new())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init(&mut self, niri: &mut Niri) {
|
|
||||||
for (device_id, path) in self.udev_dispatcher.clone().as_source_ref().device_list() {
|
|
||||||
if let Err(err) = self.device_added(device_id, path, niri) {
|
if let Err(err) = self.device_added(device_id, path, niri) {
|
||||||
warn!("error adding device: {err:?}");
|
warn!("error adding device: {err:?}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn device_added(
|
fn device_added(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
|||||||
Reference in New Issue
Block a user