Files
niri/src/tty.rs
T

471 lines
16 KiB
Rust
Raw Normal View History

use std::collections::{HashMap, HashSet};
2023-08-09 11:03:38 +04:00
use std::os::fd::FromRawFd;
use std::path::{Path, PathBuf};
2023-08-09 11:03:38 +04:00
use anyhow::anyhow;
use smithay::backend::allocator::dmabuf::Dmabuf;
use smithay::backend::allocator::gbm::{GbmAllocator, GbmBufferFlags, GbmDevice};
use smithay::backend::allocator::{Format as DrmFormat, Fourcc};
2023-08-09 11:03:38 +04:00
use smithay::backend::drm::compositor::DrmCompositor;
use smithay::backend::drm::{DrmDevice, DrmDeviceFd, DrmEvent};
use smithay::backend::egl::{EGLContext, EGLDisplay};
use smithay::backend::libinput::{LibinputInputBackend, LibinputSessionInterface};
use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement;
use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture};
2023-08-09 11:03:38 +04:00
use smithay::backend::renderer::{Bind, ImportEgl};
use smithay::backend::session::libseat::LibSeatSession;
use smithay::backend::session::{Event as SessionEvent, Session};
use smithay::backend::udev::{self, UdevBackend, UdevEvent};
use smithay::output::{Mode, Output, OutputModeSource, PhysicalProperties, Subpixel};
use smithay::reexports::calloop::{LoopHandle, RegistrationToken};
use smithay::reexports::drm::control::{connector, crtc, ModeTypeFlags};
2023-08-09 11:03:38 +04:00
use smithay::reexports::input::Libinput;
use smithay::reexports::nix::fcntl::OFlag;
use smithay::reexports::nix::libc::dev_t;
use smithay::utils::DeviceFd;
use smithay_drm_extras::drm_scanner::{DrmScanEvent, DrmScanner};
2023-08-09 11:03:38 +04:00
use smithay_drm_extras::edid::EdidInfo;
use crate::backend::Backend;
2023-08-13 07:45:10 +04:00
use crate::input::CompositorMod;
2023-08-10 09:57:13 +04:00
use crate::niri::OutputRenderElements;
2023-08-09 11:03:38 +04:00
use crate::{LoopData, Niri};
2023-08-09 14:20:59 +04:00
const BACKGROUND_COLOR: [f32; 4] = [0.1, 0.1, 0.1, 1.];
2023-08-09 11:03:38 +04:00
const SUPPORTED_COLOR_FORMATS: &[Fourcc] = &[Fourcc::Argb8888, Fourcc::Abgr8888];
pub struct Tty {
session: LibSeatSession,
primary_gpu_path: PathBuf,
output_device: Option<OutputDevice>,
}
type GbmDrmCompositor =
DrmCompositor<GbmAllocator<DrmDeviceFd>, GbmDevice<DrmDeviceFd>, (), DrmDeviceFd>;
struct OutputDevice {
id: dev_t,
token: RegistrationToken,
drm: DrmDevice,
gbm: GbmDevice<DrmDeviceFd>,
2023-08-09 11:03:38 +04:00
gles: GlesRenderer,
formats: HashSet<DrmFormat>,
drm_scanner: DrmScanner,
surfaces: HashMap<crtc::Handle, GbmDrmCompositor>,
}
#[derive(Debug, Clone, Copy)]
struct TtyOutputState {
device_id: dev_t,
crtc: crtc::Handle,
2023-08-09 11:03:38 +04:00
}
impl Backend for Tty {
fn seat_name(&self) -> String {
self.session.seat()
}
fn renderer(&mut self) -> &mut GlesRenderer {
&mut self.output_device.as_mut().unwrap().gles
}
fn render(
&mut self,
niri: &mut Niri,
output: &Output,
2023-08-10 09:57:13 +04:00
elements: &[OutputRenderElements<
GlesRenderer,
WaylandSurfaceRenderElement<GlesRenderer>,
>],
2023-08-09 11:03:38 +04:00
) {
2023-08-10 14:12:20 +04:00
let _span = tracy_client::span!("Tty::render");
let device = self.output_device.as_mut().unwrap();
let tty_state: &TtyOutputState = output.user_data().get().unwrap();
let drm_compositor = device.surfaces.get_mut(&tty_state.crtc).unwrap();
2023-08-09 14:20:59 +04:00
match drm_compositor.render_frame::<_, _, GlesTexture>(
&mut device.gles,
2023-08-09 14:20:59 +04:00
elements,
BACKGROUND_COLOR,
) {
Ok(res) => {
assert!(!res.needs_sync());
// debug!("{:?}", res);
2023-08-09 14:20:59 +04:00
if res.damage.is_some() {
match drm_compositor.queue_frame(()) {
Ok(()) => {
niri.output_state
.get_mut(output)
.unwrap()
.waiting_for_vblank = true
}
2023-08-09 14:20:59 +04:00
Err(err) => {
error!("error queueing frame: {err}");
}
}
}
}
Err(err) => {
// Can fail if we switched to a different TTY.
error!("error rendering frame: {err}");
}
}
2023-08-09 11:03:38 +04:00
}
}
impl Tty {
pub fn new(event_loop: LoopHandle<LoopData>) -> Self {
let (session, notifier) = LibSeatSession::new().unwrap();
let seat_name = session.seat();
let mut libinput = Libinput::new_with_udev(LibinputSessionInterface::from(session.clone()));
libinput.udev_assign_seat(&seat_name).unwrap();
let input_backend = LibinputInputBackend::new(libinput.clone());
event_loop
.insert_source(input_backend, |event, _, data| {
2023-08-09 14:21:29 +04:00
let tty = data.tty.as_mut().unwrap();
let mut change_vt = |vt| tty.change_vt(vt);
2023-08-13 07:45:10 +04:00
data.niri
.process_input_event(&mut change_vt, CompositorMod::Super, event);
2023-08-09 11:03:38 +04:00
})
.unwrap();
event_loop
.insert_source(notifier, move |event, _, data| {
let tty = data.tty.as_mut().unwrap();
let niri = &mut data.niri;
match event {
SessionEvent::PauseSession => {
2023-08-09 14:19:22 +04:00
debug!("pausing session");
2023-08-09 11:03:38 +04:00
libinput.suspend();
if let Some(output_device) = &tty.output_device {
output_device.drm.pause();
}
}
SessionEvent::ActivateSession => {
2023-08-09 14:19:22 +04:00
debug!("resuming session");
2023-08-09 11:03:38 +04:00
if libinput.resume().is_err() {
error!("error resuming libinput");
}
2023-08-09 14:19:22 +04:00
if let Some(output_device) = &mut tty.output_device {
output_device.drm.activate();
for drm_compositor in output_device.surfaces.values_mut() {
if let Err(err) = drm_compositor.surface().reset_state() {
warn!("error resetting DRM surface state: {err}");
}
drm_compositor.reset_buffers();
2023-08-09 14:19:22 +04:00
}
2023-08-09 11:03:38 +04:00
}
niri.queue_redraw_all();
2023-08-09 11:03:38 +04:00
}
}
})
.unwrap();
let primary_gpu_path = udev::primary_gpu(&seat_name).unwrap().unwrap();
Self {
session,
primary_gpu_path,
output_device: None,
}
}
pub fn init(&mut self, niri: &mut Niri) {
let backend = UdevBackend::new(&self.session.seat()).unwrap();
for (device_id, path) in backend.device_list() {
if let Err(err) = self.device_added(device_id, path, niri) {
2023-08-09 11:03:38 +04:00
warn!("error adding device: {err:?}");
}
}
niri.event_loop
.insert_source(backend, move |event, _, data| {
let tty = data.tty.as_mut().unwrap();
let niri = &mut data.niri;
match event {
UdevEvent::Added { device_id, path } => {
if let Err(err) = tty.device_added(device_id, &path, niri) {
2023-08-09 11:03:38 +04:00
warn!("error adding device: {err:?}");
}
}
UdevEvent::Changed { device_id } => tty.device_changed(device_id, niri),
UdevEvent::Removed { device_id } => tty.device_removed(device_id, niri),
}
})
.unwrap();
}
fn device_added(
&mut self,
device_id: dev_t,
path: &Path,
2023-08-09 11:03:38 +04:00
niri: &mut Niri,
) -> anyhow::Result<()> {
if path != self.primary_gpu_path {
debug!("skipping non-primary device {path:?}");
return Ok(());
}
debug!("adding device {path:?}");
assert!(self.output_device.is_none());
let open_flags = OFlag::O_RDWR | OFlag::O_CLOEXEC | OFlag::O_NOCTTY | OFlag::O_NONBLOCK;
let fd = self.session.open(path, open_flags)?;
2023-08-09 11:03:38 +04:00
let device_fd = unsafe { DrmDeviceFd::new(DeviceFd::from_raw_fd(fd)) };
let (drm, drm_notifier) = DrmDevice::new(device_fd.clone(), true)?;
let gbm = GbmDevice::new(device_fd)?;
let display = EGLDisplay::new(gbm.clone())?;
let egl_context = EGLContext::new(&display)?;
let mut gles = unsafe { GlesRenderer::new(egl_context)? };
gles.bind_wl_display(&niri.display_handle)?;
let token = niri
.event_loop
.insert_source(drm_notifier, move |event, metadata, data| {
let tty = data.tty.as_mut().unwrap();
match event {
DrmEvent::VBlank(crtc) => {
2023-08-10 14:12:20 +04:00
tracy_client::Client::running()
.unwrap()
.message("vblank", 0);
2023-08-11 10:47:31 +04:00
trace!("vblank {metadata:?}");
2023-08-09 11:03:38 +04:00
let device = tty.output_device.as_mut().unwrap();
let drm_compositor = device.surfaces.get_mut(&crtc).unwrap();
2023-08-09 11:03:38 +04:00
// Mark the last frame as submitted.
if let Err(err) = drm_compositor.frame_submitted() {
2023-08-09 11:03:38 +04:00
error!("error marking frame as submitted: {err}");
}
// Send presentation time feedback.
// catacomb
// .windows
// .mark_presented(&output_device.last_render_states, metadata);
let output = data
.niri
.global_space
.outputs()
.find(|output| {
let tty_state: &TtyOutputState = output.user_data().get().unwrap();
tty_state.device_id == device.id && tty_state.crtc == crtc
})
.unwrap()
.clone();
data.niri
.output_state
.get_mut(&output)
.unwrap()
.waiting_for_vblank = false;
data.niri.queue_redraw(output);
2023-08-09 11:03:38 +04:00
}
DrmEvent::Error(error) => error!("DRM error: {error}"),
};
})
.unwrap();
let formats = Bind::<Dmabuf>::supported_formats(&gles).unwrap_or_default();
2023-08-09 11:03:38 +04:00
self.output_device = Some(OutputDevice {
id: device_id,
token,
drm,
gbm,
2023-08-09 11:03:38 +04:00
gles,
formats,
drm_scanner: DrmScanner::new(),
surfaces: HashMap::new(),
2023-08-09 11:03:38 +04:00
});
self.device_changed(device_id, niri);
2023-08-09 11:03:38 +04:00
Ok(())
}
fn device_changed(&mut self, device_id: dev_t, niri: &mut Niri) {
let Some(device) = &mut self.output_device else {
return;
};
if device.id != device_id {
return;
}
debug!("output device changed");
for event in device.drm_scanner.scan_connectors(&device.drm) {
match event {
DrmScanEvent::Connected {
connector,
crtc: Some(crtc),
} => {
if let Err(err) = self.connector_connected(niri, connector, crtc) {
warn!("error connecting connector: {err:?}");
}
2023-08-09 11:03:38 +04:00
}
DrmScanEvent::Disconnected {
connector,
crtc: Some(crtc),
} => self.connector_disconnected(niri, connector, crtc),
_ => (),
2023-08-09 11:03:38 +04:00
}
}
}
fn device_removed(&mut self, device_id: dev_t, niri: &mut Niri) {
let Some(device) = self.output_device.take() else {
return;
};
if device.id != device_id {
// It wasn't the output device, put it back in.
self.output_device = Some(device);
return;
}
let crtcs: Vec<_> = device
.drm_scanner
.crtcs()
.map(|(info, crtc)| (info.clone(), crtc))
.collect();
2023-08-09 11:03:38 +04:00
for (connector, crtc) in crtcs {
self.connector_disconnected(niri, connector, crtc);
2023-08-09 11:03:38 +04:00
}
niri.event_loop.remove(device.token);
2023-08-09 11:03:38 +04:00
}
fn connector_connected(
2023-08-09 11:03:38 +04:00
&mut self,
niri: &mut Niri,
connector: connector::Info,
crtc: crtc::Handle,
) -> anyhow::Result<()> {
let output_name = format!(
"{}-{}",
2023-08-09 11:03:38 +04:00
connector.interface().as_str(),
connector.interface_id(),
);
debug!("connecting connector: {output_name}");
let device = self.output_device.as_mut().unwrap();
2023-08-09 11:03:38 +04:00
let mut mode = connector.modes().get(0);
connector.modes().iter().for_each(|m| {
debug!("mode: {m:?}");
if m.mode_type().contains(ModeTypeFlags::PREFERRED) {
// Pick the highest refresh rate.
if mode
.map(|curr| curr.vrefresh() < m.vrefresh())
.unwrap_or(true)
{
mode = Some(m);
}
}
});
let mode = mode.ok_or_else(|| anyhow!("no mode"))?;
debug!("picking mode: {mode:?}");
2023-08-09 11:03:38 +04:00
let surface = device
.drm
.create_surface(crtc, *mode, &[connector.handle()])?;
2023-08-09 11:03:38 +04:00
// Create GBM allocator.
let gbm_flags = GbmBufferFlags::RENDERING | GbmBufferFlags::SCANOUT;
let allocator = GbmAllocator::new(device.gbm.clone(), gbm_flags);
2023-08-09 11:03:38 +04:00
// Update the output mode.
let (physical_width, physical_height) = connector.size().unwrap_or((0, 0));
let (make, model) = EdidInfo::for_connector(&device.drm, connector.handle())
2023-08-09 11:03:38 +04:00
.map(|info| (info.manufacturer, info.model))
.unwrap_or_else(|| ("Unknown".into(), "Unknown".into()));
let output = Output::new(
output_name,
PhysicalProperties {
size: (physical_width as i32, physical_height as i32).into(),
subpixel: Subpixel::Unknown,
model,
make,
},
);
let wl_mode = Mode::from(*mode);
output.change_current_state(Some(wl_mode), None, None, Some((0, 0).into()));
output.set_preferred(wl_mode);
output.user_data().insert_if_missing(|| TtyOutputState {
device_id: device.id,
crtc,
});
2023-08-09 11:03:38 +04:00
// Create the compositor.
let compositor = DrmCompositor::new(
OutputModeSource::Auto(output.clone()),
2023-08-09 11:03:38 +04:00
surface,
None,
allocator,
device.gbm.clone(),
2023-08-09 11:03:38 +04:00
SUPPORTED_COLOR_FORMATS,
device.formats.clone(),
device.drm.cursor_size(),
Some(device.gbm.clone()),
2023-08-09 11:03:38 +04:00
)?;
let res = device.surfaces.insert(crtc, compositor);
assert!(res.is_none(), "crtc must not have already existed");
niri.add_output(output.clone());
niri.queue_redraw(output);
Ok(())
}
fn connector_disconnected(
&mut self,
niri: &mut Niri,
connector: connector::Info,
crtc: crtc::Handle,
) {
debug!("disconnecting connector: {connector:?}");
let device = self.output_device.as_mut().unwrap();
if device.surfaces.remove(&crtc).is_none() {
debug!("crts wasn't enabled");
return;
}
let output = niri
.global_space
.outputs()
.find(|output| {
let tty_state: &TtyOutputState = output.user_data().get().unwrap();
tty_state.device_id == device.id && tty_state.crtc == crtc
})
.unwrap()
.clone();
niri.remove_output(&output);
2023-08-09 11:03:38 +04:00
}
2023-08-09 14:21:29 +04:00
fn change_vt(&mut self, vt: i32) {
if let Err(err) = self.session.change_vt(vt) {
error!("error changing VT: {err}");
}
}
2023-08-09 11:03:38 +04:00
}