autocommit 2022-03-11 18:15:21 CET
This commit is contained in:
parent
0399772467
commit
b1890894b2
30 changed files with 2909 additions and 504 deletions
|
|
@ -2,14 +2,12 @@ use i4config::embedded::grab_input_device;
|
|||
use i4config::keyboard::mods::{Modifiers, ALT, CTRL, SHIFT};
|
||||
use i4config::keyboard::syms::{
|
||||
SYM_Super_L, SYM_b, SYM_comma, SYM_d, SYM_f, SYM_h, SYM_j, SYM_k, SYM_l, SYM_p, SYM_period,
|
||||
SYM_r, SYM_t, SYM_v, SYM_y,
|
||||
SYM_q, SYM_r, SYM_t, SYM_v, SYM_y,
|
||||
};
|
||||
use i4config::theme::{get_title_height, set_title_color, set_title_height, Color};
|
||||
use i4config::Axis::{Horizontal, Vertical};
|
||||
use i4config::Direction::{Down, Left, Right, Up};
|
||||
use i4config::{
|
||||
config, create_seat, input_devices, on_new_input_device, Command, Seat,
|
||||
};
|
||||
use i4config::{config, create_seat, input_devices, on_new_input_device, quit, Command, Seat};
|
||||
use rand::Rng;
|
||||
|
||||
const MOD: Modifiers = ALT;
|
||||
|
|
@ -71,6 +69,8 @@ fn configure_seat(s: Seat) {
|
|||
|
||||
s.bind(MOD | SYM_p, || Command::new("xeyes").spawn());
|
||||
|
||||
s.bind(MOD | SYM_q, || quit());
|
||||
|
||||
fn do_grab(s: Seat, grab: bool) {
|
||||
for device in s.input_devices() {
|
||||
log::info!(
|
||||
|
|
@ -97,9 +97,7 @@ pub fn configure() {
|
|||
for device in input_devices() {
|
||||
device.set_seat(seat);
|
||||
}
|
||||
on_new_input_device(move |device| {
|
||||
device.set_seat(seat)
|
||||
});
|
||||
on_new_input_device(move |device| device.set_seat(seat));
|
||||
}
|
||||
|
||||
config!(configure);
|
||||
|
|
|
|||
|
|
@ -284,6 +284,10 @@ impl Client {
|
|||
*self.on_new_seat.borrow_mut() = Some(Rc::new(f));
|
||||
}
|
||||
|
||||
pub fn quit(&self) {
|
||||
self.send(&ClientMessage::Quit)
|
||||
}
|
||||
|
||||
pub fn on_new_input_device<F: Fn(InputDevice) + 'static>(&self, f: F) {
|
||||
*self.on_new_input_device.borrow_mut() = Some(Rc::new(f));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ pub enum ClientMessage<'a> {
|
|||
CreateSeat {
|
||||
name: &'a str,
|
||||
},
|
||||
Quit,
|
||||
SetSeat {
|
||||
device: InputDevice,
|
||||
seat: Seat,
|
||||
|
|
|
|||
|
|
@ -163,6 +163,10 @@ pub fn on_new_input_device<F: Fn(InputDevice) + 'static>(f: F) {
|
|||
get!().on_new_input_device(f)
|
||||
}
|
||||
|
||||
pub fn quit() {
|
||||
get!().quit()
|
||||
}
|
||||
|
||||
pub struct Command {
|
||||
prog: String,
|
||||
args: Vec<String>,
|
||||
|
|
|
|||
|
|
@ -1,22 +1,27 @@
|
|||
mod input;
|
||||
mod monitor;
|
||||
mod video;
|
||||
|
||||
use crate::async_engine::AsyncFd;
|
||||
use crate::backend::{Backend, InputDevice, InputDeviceId, InputEvent};
|
||||
use crate::dbus::DbusError;
|
||||
use crate::drm::drm::DrmError;
|
||||
use crate::drm::gbm::GbmError;
|
||||
use crate::libinput::device::RegisteredDevice;
|
||||
use crate::libinput::{LibInput, LibInputAdapter, LibInputError};
|
||||
use crate::logind::{LogindError, Session};
|
||||
use crate::metal::video::{MetalDrmDevice, PendingDrmDevice};
|
||||
use crate::udev::{UdevError, UdevMonitor};
|
||||
use crate::utils::copyhashmap::CopyHashMap;
|
||||
use crate::{CloneCell, State, Udev};
|
||||
use crate::utils::oserror::OsError;
|
||||
use crate::utils::syncqueue::SyncQueue;
|
||||
use crate::{CloneCell, RenderError, State, Udev};
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::future::pending;
|
||||
use std::rc::Rc;
|
||||
use thiserror::Error;
|
||||
use uapi::{c, OwnedFd};
|
||||
use crate::backend::{Backend, InputDevice, InputDeviceId, InputEvent};
|
||||
use crate::utils::syncqueue::SyncQueue;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum MetalError {
|
||||
|
|
@ -36,6 +41,34 @@ pub enum MetalError {
|
|||
Dup(#[source] crate::utils::oserror::OsError),
|
||||
#[error("Metal backend terminated unexpectedly")]
|
||||
UnexpectedTermination,
|
||||
#[error("Could not create GBM device")]
|
||||
GbmDevice(#[source] GbmError),
|
||||
#[error("Could not create a render context")]
|
||||
CreateRenderContex(#[source] RenderError),
|
||||
#[error("Cannot initialize connector because no CRTC is available")]
|
||||
NoCrtcForConnector,
|
||||
#[error("Cannot initialize connector because no primary plane is available")]
|
||||
NoPrimaryPlaneForConnector,
|
||||
#[error("Cannot initialize connector because no mode is available")]
|
||||
NoModeForConnector,
|
||||
#[error("Could not allocate scanout buffer")]
|
||||
ScanoutBuffer(#[source] GbmError),
|
||||
#[error("Could not create a framebuffer")]
|
||||
Framebuffer(#[source] DrmError),
|
||||
#[error("Could not import a framebuffer into EGL")]
|
||||
ImportFb(#[source] RenderError),
|
||||
#[error("Could not configure connector chain")]
|
||||
Configure(#[source] DrmError),
|
||||
#[error("Could not enable atomic modesetting")]
|
||||
AtomicModesetting(#[source] OsError),
|
||||
#[error("Could not inspect a plane")]
|
||||
CreatePlane(#[source] DrmError),
|
||||
#[error("Could not inspect a crtc")]
|
||||
CreateCrtc(#[source] DrmError),
|
||||
#[error("Could not inspect an encoder")]
|
||||
CreateEncoder(#[source] DrmError),
|
||||
#[error(transparent)]
|
||||
DrmError(#[from] DrmError),
|
||||
}
|
||||
|
||||
pub async fn run(state: Rc<State>) -> MetalError {
|
||||
|
|
@ -45,6 +78,8 @@ pub async fn run(state: Rc<State>) -> MetalError {
|
|||
}
|
||||
}
|
||||
|
||||
linear_ids!(DrmIds, DrmId);
|
||||
|
||||
struct MetalBackend {
|
||||
state: Rc<State>,
|
||||
udev: Rc<Udev>,
|
||||
|
|
@ -54,11 +89,10 @@ struct MetalBackend {
|
|||
libinput_fd: AsyncFd,
|
||||
device_holder: Rc<DeviceHolder>,
|
||||
session: Session,
|
||||
drm_ids: DrmIds,
|
||||
}
|
||||
|
||||
impl Backend for MetalBackend {
|
||||
|
||||
}
|
||||
impl Backend for MetalBackend {}
|
||||
|
||||
async fn run_(state: Rc<State>) -> Result<(), MetalError> {
|
||||
let socket = match state.dbus.system() {
|
||||
|
|
@ -73,12 +107,15 @@ async fn run_(state: Rc<State>) -> Result<(), MetalError> {
|
|||
return Err(MetalError::TakeControl(e));
|
||||
}
|
||||
let device_holder = Rc::new(DeviceHolder {
|
||||
devices: Default::default(),
|
||||
input_devices: Default::default(),
|
||||
input_devices_: Default::default(),
|
||||
drm_devices: Default::default(),
|
||||
pending_drm_devices: Default::default(),
|
||||
});
|
||||
let udev = Rc::new(Udev::new()?);
|
||||
let monitor = Rc::new(udev.create_monitor()?);
|
||||
monitor.add_match_subsystem_devtype(Some("input"), None)?;
|
||||
monitor.add_match_subsystem_devtype(Some("drm"), None)?;
|
||||
monitor.enable_receiving()?;
|
||||
let libinput = Rc::new(LibInput::new(device_holder.clone())?);
|
||||
let monitor_fd = match uapi::fcntl_dupfd_cloexec(monitor.fd(), 0) {
|
||||
|
|
@ -98,7 +135,22 @@ async fn run_(state: Rc<State>) -> Result<(), MetalError> {
|
|||
libinput_fd,
|
||||
device_holder,
|
||||
session,
|
||||
drm_ids: Default::default(),
|
||||
});
|
||||
let _pause_handler = {
|
||||
let mtl = metal.clone();
|
||||
metal
|
||||
.session
|
||||
.on_pause(move |p| mtl.handle_device_pause(p))
|
||||
.unwrap()
|
||||
};
|
||||
let _resume_handler = {
|
||||
let mtl = metal.clone();
|
||||
metal
|
||||
.session
|
||||
.on_resume(move |p| mtl.handle_device_resume(p))
|
||||
.unwrap()
|
||||
};
|
||||
let _monitor = state.eng.spawn(metal.clone().monitor_devices());
|
||||
let _events = state.eng.spawn(metal.clone().handle_libinput_events());
|
||||
if let Err(e) = metal.enumerate_devices() {
|
||||
|
|
@ -107,7 +159,7 @@ async fn run_(state: Rc<State>) -> Result<(), MetalError> {
|
|||
pending().await
|
||||
}
|
||||
|
||||
struct MetalDevice {
|
||||
struct MetalInputDevice {
|
||||
slot: usize,
|
||||
id: InputDeviceId,
|
||||
devnum: c::dev_t,
|
||||
|
|
@ -120,9 +172,17 @@ struct MetalDevice {
|
|||
cb: CloneCell<Option<Rc<dyn Fn()>>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum MetalDevice {
|
||||
Input(Rc<MetalInputDevice>),
|
||||
Drm(Rc<MetalDrmDevice>),
|
||||
}
|
||||
|
||||
struct DeviceHolder {
|
||||
input_devices: CopyHashMap<c::dev_t, Rc<MetalDevice>>,
|
||||
input_devices_: RefCell<Vec<Option<Rc<MetalDevice>>>>,
|
||||
devices: CopyHashMap<c::dev_t, MetalDevice>,
|
||||
input_devices: RefCell<Vec<Option<Rc<MetalInputDevice>>>>,
|
||||
drm_devices: CopyHashMap<c::dev_t, Rc<MetalDrmDevice>>,
|
||||
pending_drm_devices: CopyHashMap<c::dev_t, PendingDrmDevice>,
|
||||
}
|
||||
|
||||
impl LibInputAdapter for DeviceHolder {
|
||||
|
|
@ -131,8 +191,8 @@ impl LibInputAdapter for DeviceHolder {
|
|||
Ok(s) => s,
|
||||
Err(e) => return Err(LibInputError::Stat(e.into())),
|
||||
};
|
||||
match self.input_devices.get(&stat.st_rdev) {
|
||||
Some(d) => match d.fd.get() {
|
||||
match self.devices.get(&stat.st_rdev) {
|
||||
Some(MetalDevice::Input(d)) => match d.fd.get() {
|
||||
Some(fd) => match uapi::fcntl_dupfd_cloexec(fd.raw(), 0) {
|
||||
Ok(fd) => Ok(fd),
|
||||
Err(e) => Err(LibInputError::DupFd(e.into())),
|
||||
|
|
@ -144,7 +204,7 @@ impl LibInputAdapter for DeviceHolder {
|
|||
}
|
||||
}
|
||||
|
||||
impl InputDevice for MetalDevice {
|
||||
impl InputDevice for MetalInputDevice {
|
||||
fn id(&self) -> InputDeviceId {
|
||||
self.id
|
||||
}
|
||||
|
|
@ -166,7 +226,7 @@ impl InputDevice for MetalDevice {
|
|||
}
|
||||
}
|
||||
|
||||
impl MetalDevice {
|
||||
impl MetalInputDevice {
|
||||
fn event(&self, event: InputEvent) {
|
||||
self.events.push(event);
|
||||
if let Some(cb) = self.cb.get() {
|
||||
|
|
|
|||
|
|
@ -1,35 +1,38 @@
|
|||
use crate::async_engine::FdStatus;
|
||||
use crate::backend::{InputEvent, KeyState};
|
||||
use crate::libinput::consts::LIBINPUT_KEY_STATE_PRESSED;
|
||||
use crate::libinput::event::LibInputEvent;
|
||||
use crate::metal::MetalBackend;
|
||||
use crate::ErrorFmt;
|
||||
use std::rc::Rc;
|
||||
use crate::backend::{InputEvent, KeyState};
|
||||
use crate::libinput::consts::LIBINPUT_KEY_STATE_PRESSED;
|
||||
|
||||
macro_rules! unpack {
|
||||
($slf:expr, $ev:expr) => {
|
||||
($slf:expr, $ev:expr) => {{
|
||||
let slot = match $ev.device().slot() {
|
||||
Some(s) => s,
|
||||
_ => return,
|
||||
};
|
||||
let data = match $slf
|
||||
.device_holder
|
||||
.input_devices
|
||||
.borrow_mut()
|
||||
.get(slot)
|
||||
.cloned()
|
||||
.and_then(|v| v)
|
||||
{
|
||||
let slot = match $ev.device().slot() {
|
||||
Some(s) => s,
|
||||
_ => return,
|
||||
};
|
||||
let data = match $slf.device_holder.input_devices_.borrow_mut().get(slot).cloned().and_then(|v| v) {
|
||||
Some(d) => d,
|
||||
_ => return,
|
||||
};
|
||||
data
|
||||
}
|
||||
};
|
||||
($slf:expr, $ev:expr, $conv:ident) => {
|
||||
{
|
||||
let event = match $ev.$conv() {
|
||||
Some(e) => e,
|
||||
_ => return,
|
||||
};
|
||||
let data = unpack!($slf, $ev);
|
||||
(event, data)
|
||||
}
|
||||
};
|
||||
Some(d) => d,
|
||||
_ => return,
|
||||
};
|
||||
data
|
||||
}};
|
||||
($slf:expr, $ev:expr, $conv:ident) => {{
|
||||
let event = match $ev.$conv() {
|
||||
Some(e) => e,
|
||||
_ => return,
|
||||
};
|
||||
let data = unpack!($slf, $ev);
|
||||
(event, data)
|
||||
}};
|
||||
}
|
||||
|
||||
impl MetalBackend {
|
||||
|
|
@ -65,7 +68,7 @@ impl MetalBackend {
|
|||
|
||||
match event.ty() {
|
||||
c::LIBINPUT_EVENT_DEVICE_ADDED => self.handle_device_added(event),
|
||||
c::LIBINPUT_EVENT_DEVICE_REMOVED => self.handle_device_removed(event),
|
||||
c::LIBINPUT_EVENT_DEVICE_REMOVED => self.handle_li_device_removed(event),
|
||||
c::LIBINPUT_EVENT_KEYBOARD_KEY => self.handle_keyboard_key(event),
|
||||
c::LIBINPUT_EVENT_POINTER_MOTION => self.handle_pointer_motion(event),
|
||||
_ => {}
|
||||
|
|
@ -76,10 +79,9 @@ impl MetalBackend {
|
|||
// let dev = unpack!(self, event);
|
||||
}
|
||||
|
||||
fn handle_device_removed(self: &Rc<Self>, event: LibInputEvent) {
|
||||
fn handle_li_device_removed(self: &Rc<Self>, event: LibInputEvent) {
|
||||
let dev = unpack!(self, event);
|
||||
self.device_holder.input_devices.remove(&dev.devnum);
|
||||
self.device_holder.input_devices_.borrow_mut()[dev.slot] = None;
|
||||
dev.inputdev.set(None);
|
||||
event.device().unset_slot();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,29 @@
|
|||
use std::cell::Cell;
|
||||
use crate::async_engine::FdStatus;
|
||||
use crate::backend::BackendEvent;
|
||||
use crate::dbus::TRUE;
|
||||
use crate::metal::{MetalBackend, MetalDevice, MetalError};
|
||||
use crate::drm::drm::DrmMaster;
|
||||
use crate::metal::video::PendingDrmDevice;
|
||||
use crate::metal::{MetalBackend, MetalDevice, MetalDrmDevice, MetalError, MetalInputDevice};
|
||||
use crate::org::freedesktop::login1::session::{PauseDevice, ResumeDevice};
|
||||
use crate::udev::UdevDevice;
|
||||
use crate::ErrorFmt;
|
||||
use bstr::ByteSlice;
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
use crate::backend::BackendEvent;
|
||||
use uapi::{c, OwnedFd};
|
||||
|
||||
const DRM: &[u8] = b"drm";
|
||||
const INPUT: &[u8] = b"input";
|
||||
const EVENT: &[u8] = b"event";
|
||||
|
||||
const CARD: &[u8] = b"card";
|
||||
|
||||
fn is_primary_node(n: &[u8]) -> bool {
|
||||
match n.strip_prefix(CARD) {
|
||||
Some(r) => r.iter().copied().all(|c| matches!(c, b'0'..=b'9')),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
impl MetalBackend {
|
||||
pub async fn monitor_devices(self: Rc<Self>) {
|
||||
|
|
@ -25,58 +43,215 @@ impl MetalBackend {
|
|||
_ => {}
|
||||
}
|
||||
while let Some(dev) = self.monitor.receive_device() {
|
||||
log::info!("x {:?}", dev.devnode());
|
||||
let action = match dev.action() {
|
||||
Some(c) => c,
|
||||
_ => continue,
|
||||
};
|
||||
match action.to_bytes() {
|
||||
b"add" => self.handle_device_add(dev),
|
||||
b"change" => self.handle_device_change(dev),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
}
|
||||
log::error!("Monitor task exited. Future hotplug events will be ignored.");
|
||||
}
|
||||
|
||||
pub fn handle_device_pause(self: &Rc<Self>, pause: PauseDevice) {
|
||||
if pause.ty == "pause" {
|
||||
self.session.device_paused(pause.major, pause.minor);
|
||||
}
|
||||
let dev = uapi::makedev(pause.major as _, pause.minor as _);
|
||||
if pause.ty == "gone" {
|
||||
self.handle_device_removed(dev);
|
||||
} else {
|
||||
self.handle_device_paused(dev);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_device_resume(self: &Rc<Self>, resume: ResumeDevice) {
|
||||
let dev = uapi::makedev(resume.major as _, resume.minor as _);
|
||||
let dev = match self.device_holder.devices.get(&dev) {
|
||||
Some(d) => d,
|
||||
_ => return,
|
||||
};
|
||||
match dev {
|
||||
MetalDevice::Input(id) => self.handle_input_device_resume(&id, resume.fd),
|
||||
MetalDevice::Drm(dd) => self.handle_drm_device_resume(&dd, resume.fd),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_drm_device_resume(self: &Rc<Self>, dev: &Rc<MetalDrmDevice>, fd: Rc<OwnedFd>) {
|
||||
log::info!("Device resumed: {}", dev.dev.devnode.to_bytes().as_bstr());
|
||||
}
|
||||
|
||||
fn handle_input_device_resume(self: &Rc<Self>, dev: &Rc<MetalInputDevice>, fd: Rc<OwnedFd>) {
|
||||
log::info!("Device resumed: {}", dev.devnode.to_bytes().as_bstr());
|
||||
dev.fd.set(Some(fd));
|
||||
let inputdev = match self.libinput.open(dev.devnode.as_c_str()) {
|
||||
Ok(d) => d,
|
||||
Err(_) => return,
|
||||
};
|
||||
inputdev.device().set_slot(dev.slot);
|
||||
dev.inputdev.set(Some(inputdev));
|
||||
}
|
||||
|
||||
fn handle_device_removed(self: &Rc<Self>, dev: c::dev_t) {
|
||||
let dev = match self.device_holder.devices.remove(&dev) {
|
||||
Some(d) => d,
|
||||
_ => return,
|
||||
};
|
||||
match dev {
|
||||
MetalDevice::Input(id) => self.handle_input_device_removed(&id),
|
||||
MetalDevice::Drm(dd) => self.handle_drm_device_removed(&dd),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_drm_device_removed(self: &Rc<Self>, dev: &Rc<MetalDrmDevice>) {
|
||||
log::info!("Device removed: {}", dev.dev.devnode.to_bytes().as_bstr());
|
||||
}
|
||||
|
||||
fn handle_input_device_removed(self: &Rc<Self>, dev: &Rc<MetalInputDevice>) {
|
||||
log::info!("Device removed: {}", dev.devnode.to_bytes().as_bstr());
|
||||
self.device_holder.input_devices.borrow_mut()[dev.slot] = None;
|
||||
dev.fd.set(None);
|
||||
if let Some(rd) = dev.inputdev.take() {
|
||||
rd.device().unset_slot();
|
||||
}
|
||||
dev.removed.set(true);
|
||||
if let Some(cb) = dev.cb.take() {
|
||||
cb();
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_device_paused(self: &Rc<Self>, dev: c::dev_t) {
|
||||
let dev = match self.device_holder.devices.get(&dev) {
|
||||
Some(d) => d,
|
||||
_ => return,
|
||||
};
|
||||
match dev {
|
||||
MetalDevice::Input(id) => self.handle_input_device_paused(&id),
|
||||
MetalDevice::Drm(dd) => self.handle_drm_device_paused(&dd),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_drm_device_paused(self: &Rc<Self>, dev: &Rc<MetalDrmDevice>) {
|
||||
log::info!("Device paused: {}", dev.dev.devnode.to_bytes().as_bstr());
|
||||
}
|
||||
|
||||
fn handle_input_device_paused(self: &Rc<Self>, dev: &Rc<MetalInputDevice>) {
|
||||
log::info!("Device paused: {}", dev.devnode.to_bytes().as_bstr());
|
||||
if let Some(rd) = dev.inputdev.take() {
|
||||
rd.device().unset_slot();
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_device_add(self: &Rc<Self>, dev: UdevDevice) -> Option<()> {
|
||||
let ss = dev.subsystem()?;
|
||||
match ss.to_bytes() {
|
||||
INPUT => self.handle_input_device_add(dev),
|
||||
DRM => self.handle_drm_add(dev),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_input_device_add(self: &Rc<Self>, dev: UdevDevice) -> Option<()> {
|
||||
let sysname = dev.sysname()?;
|
||||
if sysname.to_bytes().starts_with(EVENT) {
|
||||
self.add_input_device(&dev);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn handle_drm_add(self: &Rc<Self>, dev: UdevDevice) -> Option<()> {
|
||||
let sysname = dev.sysname()?;
|
||||
if !is_primary_node(sysname.to_bytes()) {
|
||||
return None;
|
||||
}
|
||||
let devnum = dev.devnum();
|
||||
let devnode = dev.devnode()?;
|
||||
let id = self.drm_ids.next();
|
||||
log::info!("Device added: {}", devnode.to_bytes().as_bstr());
|
||||
let dev = PendingDrmDevice {
|
||||
id,
|
||||
devnum,
|
||||
devnode: devnode.to_owned(),
|
||||
};
|
||||
self.device_holder.pending_drm_devices.set(devnum, dev);
|
||||
let slf = self.clone();
|
||||
self.session.get_device(devnum, move |res| {
|
||||
let dev = match slf.device_holder.pending_drm_devices.remove(&devnum) {
|
||||
Some(d) if d.id == id => d,
|
||||
_ => return,
|
||||
};
|
||||
let res = match res {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
log::error!("Could not take control of drm device: {}", ErrorFmt(e));
|
||||
return;
|
||||
}
|
||||
};
|
||||
if res.inactive == TRUE {
|
||||
return;
|
||||
}
|
||||
let master = Rc::new(DrmMaster::new(res.fd.clone()));
|
||||
let dev = match slf.creat_drm_device(dev, &master) {
|
||||
Ok(d) => Rc::new(d),
|
||||
Err(e) => {
|
||||
log::error!("Could not initialize drm device: {}", ErrorFmt(e));
|
||||
return;
|
||||
}
|
||||
};
|
||||
slf.init_drm_device(&dev);
|
||||
slf.device_holder
|
||||
.drm_devices
|
||||
.set(dev.dev.devnum, dev.clone());
|
||||
slf.device_holder
|
||||
.devices
|
||||
.set(dev.dev.devnum, MetalDevice::Drm(dev.clone()));
|
||||
});
|
||||
None
|
||||
}
|
||||
|
||||
fn handle_device_change(self: &Rc<Self>, dev: UdevDevice) -> Option<()> {
|
||||
let ss = dev.subsystem()?;
|
||||
log::info!("Device changed: {}", dev.devnode()?.to_bytes().as_bstr());
|
||||
match ss.to_bytes() {
|
||||
DRM => self.handle_drm_change(dev),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_drm_change(self: &Rc<Self>, dev: UdevDevice) -> Option<()> {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn enumerate_devices(self: &Rc<Self>) -> Result<(), MetalError> {
|
||||
let mut enumerate = self.udev.create_enumerate()?;
|
||||
enumerate.add_match_subsystem("input")?;
|
||||
enumerate.add_match_subsystem(INPUT)?;
|
||||
enumerate.add_match_subsystem(DRM)?;
|
||||
enumerate.scan_devices()?;
|
||||
let mut entry_opt = enumerate.get_list_entry()?;
|
||||
while let Some(entry) = entry_opt.take() {
|
||||
'inner: {
|
||||
let device = match self.udev.create_device_from_syspath(entry.name()) {
|
||||
Ok(d) => d,
|
||||
_ => break 'inner,
|
||||
};
|
||||
let sysname = match device.sysname() {
|
||||
Ok(s) => s,
|
||||
_ => break 'inner,
|
||||
};
|
||||
if sysname.to_bytes().starts_with(b"event") {
|
||||
self.add_input_device(&device);
|
||||
}
|
||||
if let Ok(dev) = self.udev.create_device_from_syspath(entry.name()) {
|
||||
self.handle_device_add(dev);
|
||||
}
|
||||
entry_opt = entry.next();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn add_input_device(self: &Rc<Self>, dev: &UdevDevice) {
|
||||
fn add_input_device(self: &Rc<Self>, dev: &UdevDevice) -> Option<()> {
|
||||
if !dev.is_initialized() {
|
||||
return;
|
||||
return None;
|
||||
}
|
||||
let slf = self.clone();
|
||||
let device_id = self.state.input_device_ids.next();
|
||||
let devnum = dev.devnum();
|
||||
let devnode = match dev.devnode() {
|
||||
Ok(n) => n,
|
||||
Err(e) => {
|
||||
log::error!("Could not retrieve devnode of udev device: {}", ErrorFmt(e));
|
||||
return;
|
||||
}
|
||||
};
|
||||
let sysname = match dev.sysname() {
|
||||
Ok(n) => n,
|
||||
Err(e) => {
|
||||
log::error!("Could not retrieve sysname of udev device: {}", ErrorFmt(e));
|
||||
return;
|
||||
}
|
||||
};
|
||||
let mut slots = self.device_holder.input_devices_.borrow_mut();
|
||||
let devnode = dev.devnode()?;
|
||||
let sysname = dev.sysname()?;
|
||||
log::info!("Device added: {}", devnode.to_bytes().as_bstr());
|
||||
let mut slots = self.device_holder.input_devices.borrow_mut();
|
||||
let slot = 'slot: {
|
||||
for (i, s) in slots.iter().enumerate() {
|
||||
if s.is_none() {
|
||||
|
|
@ -86,7 +261,7 @@ impl MetalBackend {
|
|||
slots.push(None);
|
||||
slots.len() - 1
|
||||
};
|
||||
let dev = Rc::new(MetalDevice {
|
||||
let dev = Rc::new(MetalInputDevice {
|
||||
slot,
|
||||
id: device_id,
|
||||
devnum,
|
||||
|
|
@ -99,12 +274,14 @@ impl MetalBackend {
|
|||
cb: Default::default(),
|
||||
});
|
||||
slots[slot] = Some(dev.clone());
|
||||
self.device_holder.input_devices.set(devnum, dev);
|
||||
self.device_holder
|
||||
.devices
|
||||
.set(devnum, MetalDevice::Input(dev));
|
||||
self.session.get_device(devnum, move |res| {
|
||||
let id = &slf.device_holder.input_devices;
|
||||
let mut slots = slf.device_holder.input_devices_.borrow_mut();
|
||||
let id = &slf.device_holder.devices;
|
||||
let mut slots = slf.device_holder.input_devices.borrow_mut();
|
||||
let dev = 'dev: {
|
||||
if let Some(dev) = id.get(&devnum) {
|
||||
if let Some(dev) = slots[slot].clone() {
|
||||
if dev.id == device_id {
|
||||
break 'dev dev;
|
||||
}
|
||||
|
|
@ -126,15 +303,14 @@ impl MetalBackend {
|
|||
dev.fd.set(Some(res.fd.clone()));
|
||||
let inputdev = match slf.libinput.open(dev.devnode.as_c_str()) {
|
||||
Ok(d) => d,
|
||||
Err(_) => {
|
||||
slots[dev.slot] = None;
|
||||
id.remove(&devnum);
|
||||
return;
|
||||
}
|
||||
Err(_) => return,
|
||||
};
|
||||
inputdev.device().set_slot(slot);
|
||||
dev.inputdev.set(Some(inputdev));
|
||||
slf.state.backend_events.push(BackendEvent::NewInputDevice(dev.clone()));
|
||||
slf.state
|
||||
.backend_events
|
||||
.push(BackendEvent::NewInputDevice(dev.clone()));
|
||||
});
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
|
|||
615
src/backends/metal/video.rs
Normal file
615
src/backends/metal/video.rs
Normal file
|
|
@ -0,0 +1,615 @@
|
|||
use crate::backend::{BackendEvent, Output, OutputId};
|
||||
use crate::drm::drm::{
|
||||
ConnectorStatus, ConnectorType, DrmBlob, DrmConnector, DrmCrtc, DrmEncoder, DrmError, DrmFb,
|
||||
DrmFramebuffer, DrmMaster, DrmModeInfo, DrmObject, DrmPlane, DrmProperty,
|
||||
DrmPropertyDefinition, DrmPropertyType, PropBlob, DRM_CLIENT_CAP_ATOMIC,
|
||||
DRM_MODE_ATOMIC_ALLOW_MODESET,
|
||||
};
|
||||
use crate::drm::gbm::{GbmDevice, GBM_BO_USE_RENDERING, GBM_BO_USE_SCANOUT};
|
||||
use crate::drm::{ModifiedFormat, INVALID_MODIFIER};
|
||||
use crate::format::{Format, XRGB8888};
|
||||
use crate::metal::{DrmId, MetalBackend, MetalError};
|
||||
use crate::render::{Framebuffer, RenderContext};
|
||||
use crate::utils::bitflags::BitflagsExt;
|
||||
use crate::{CloneCell, ErrorFmt, State};
|
||||
use ahash::AHashMap;
|
||||
use bstr::{BString, ByteSlice};
|
||||
use std::cell::Cell;
|
||||
use std::ffi::CString;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::rc::Rc;
|
||||
use uapi::c;
|
||||
|
||||
pub struct PendingDrmDevice {
|
||||
pub id: DrmId,
|
||||
pub devnum: c::dev_t,
|
||||
pub devnode: CString,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MetalDrmDeviceStatic {
|
||||
pub id: DrmId,
|
||||
pub devnum: c::dev_t,
|
||||
pub devnode: CString,
|
||||
pub master: Rc<DrmMaster>,
|
||||
pub crtcs: AHashMap<DrmCrtc, Rc<MetalCrtc>>,
|
||||
pub encoders: AHashMap<DrmEncoder, Rc<MetalEncoder>>,
|
||||
pub planes: AHashMap<DrmPlane, Rc<MetalPlane>>,
|
||||
pub min_width: u32,
|
||||
pub max_width: u32,
|
||||
pub min_height: u32,
|
||||
pub max_height: u32,
|
||||
pub gbm: GbmDevice,
|
||||
pub egl: Rc<RenderContext>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MetalDrmDevice {
|
||||
pub dev: Rc<MetalDrmDeviceStatic>,
|
||||
pub connectors: AHashMap<DrmConnector, Rc<MetalConnector>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MetalConnector {
|
||||
pub id: DrmConnector,
|
||||
pub master: Rc<DrmMaster>,
|
||||
|
||||
pub output_id: OutputId,
|
||||
|
||||
pub crtcs: AHashMap<DrmCrtc, Rc<MetalCrtc>>,
|
||||
pub modes: Vec<DrmModeInfo>,
|
||||
pub mode: CloneCell<Option<Rc<DrmModeInfo>>>,
|
||||
|
||||
pub connector_type: ConnectorType,
|
||||
pub connector_type_id: u32,
|
||||
|
||||
pub connection: ConnectorStatus,
|
||||
pub mm_width: u32,
|
||||
pub mm_height: u32,
|
||||
pub subpixel: u32,
|
||||
|
||||
pub crtc_id: MutableProperty<DrmCrtc>,
|
||||
|
||||
pub egl_fb: CloneCell<Option<Rc<Framebuffer>>>,
|
||||
|
||||
pub on_change: OnChange,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct OnChange {
|
||||
pub on_change: CloneCell<Option<Rc<dyn Fn()>>>,
|
||||
}
|
||||
|
||||
impl Debug for OnChange {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self.on_change.get() {
|
||||
None => f.write_str("None"),
|
||||
Some(_) => f.write_str("Some"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Output for MetalConnector {
|
||||
fn id(&self) -> OutputId {
|
||||
self.output_id
|
||||
}
|
||||
|
||||
fn removed(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn width(&self) -> i32 {
|
||||
match self.mode.get() {
|
||||
Some(m) => m.hdisplay as _,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn height(&self) -> i32 {
|
||||
match self.mode.get() {
|
||||
Some(m) => m.vdisplay as _,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn on_change(&self, cb: Rc<dyn Fn()>) {
|
||||
self.on_change.on_change.set(Some(cb));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MetalCrtc {
|
||||
pub id: DrmCrtc,
|
||||
pub idx: usize,
|
||||
pub master: Rc<DrmMaster>,
|
||||
|
||||
pub possible_planes: AHashMap<DrmPlane, Rc<MetalPlane>>,
|
||||
|
||||
pub connector: Cell<DrmConnector>,
|
||||
|
||||
pub primary_plane: Cell<DrmPlane>,
|
||||
pub cursor_plane: Cell<DrmPlane>,
|
||||
|
||||
pub active: MutableProperty<bool>,
|
||||
pub mode_id: MutableProperty<DrmBlob>,
|
||||
|
||||
pub mode_blob: CloneCell<Option<Rc<PropBlob>>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MetalEncoder {
|
||||
pub id: DrmEncoder,
|
||||
pub crtcs: AHashMap<DrmCrtc, Rc<MetalCrtc>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||
pub enum PlaneType {
|
||||
Overlay,
|
||||
Primary,
|
||||
Cursor,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MetalPlane {
|
||||
pub id: DrmPlane,
|
||||
pub master: Rc<DrmMaster>,
|
||||
|
||||
pub ty: PlaneType,
|
||||
|
||||
pub possible_crtcs: u32,
|
||||
pub formats: AHashMap<u32, &'static Format>,
|
||||
|
||||
pub fb: CloneCell<Option<Rc<DrmFramebuffer>>>,
|
||||
|
||||
pub fb_id: MutableProperty<DrmFb>,
|
||||
pub crtc_id: MutableProperty<DrmCrtc>,
|
||||
pub crtc_x: MutableProperty<i32>,
|
||||
pub crtc_y: MutableProperty<i32>,
|
||||
pub crtc_w: MutableProperty<i32>,
|
||||
pub crtc_h: MutableProperty<i32>,
|
||||
pub src_x: MutableProperty<u32>,
|
||||
pub src_y: MutableProperty<u32>,
|
||||
pub src_w: MutableProperty<u32>,
|
||||
pub src_h: MutableProperty<u32>,
|
||||
}
|
||||
|
||||
impl MetalDrmDevice {}
|
||||
|
||||
fn get_connectors(
|
||||
state: &State,
|
||||
dev: &MetalDrmDeviceStatic,
|
||||
ids: &[DrmConnector],
|
||||
) -> Result<AHashMap<DrmConnector, Rc<MetalConnector>>, DrmError> {
|
||||
let mut connectors = AHashMap::new();
|
||||
for connector in ids {
|
||||
match create_connector(state, *connector, dev) {
|
||||
Ok(e) => {
|
||||
connectors.insert(e.id, Rc::new(e));
|
||||
}
|
||||
Err(e) => return Err(DrmError::CreateConnector(Box::new(e))),
|
||||
}
|
||||
}
|
||||
Ok(connectors)
|
||||
}
|
||||
|
||||
fn create_connector(
|
||||
state: &State,
|
||||
connector: DrmConnector,
|
||||
dev: &MetalDrmDeviceStatic,
|
||||
) -> Result<MetalConnector, DrmError> {
|
||||
let info = dev.master.get_connector_info(connector, true)?;
|
||||
let mut crtcs = AHashMap::new();
|
||||
for encoder in info.encoders {
|
||||
if let Some(encoder) = dev.encoders.get(&encoder) {
|
||||
for (_, crtc) in &encoder.crtcs {
|
||||
crtcs.insert(crtc.id, crtc.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
let props = collect_properties(&dev.master, connector)?;
|
||||
Ok(MetalConnector {
|
||||
id: connector,
|
||||
master: dev.master.clone(),
|
||||
output_id: state.output_ids.next(),
|
||||
crtcs,
|
||||
modes: info.modes,
|
||||
mode: Default::default(),
|
||||
connector_type: info.connector_type.into(),
|
||||
connector_type_id: info.connector_type_id,
|
||||
connection: info.connection.into(),
|
||||
mm_width: info.mm_width,
|
||||
mm_height: info.mm_height,
|
||||
subpixel: info.subpixel,
|
||||
crtc_id: props.get("CRTC_ID")?.map(|v| DrmCrtc(v as _)),
|
||||
egl_fb: Default::default(),
|
||||
on_change: Default::default(),
|
||||
})
|
||||
}
|
||||
|
||||
fn create_encoder(
|
||||
encoder: DrmEncoder,
|
||||
master: &Rc<DrmMaster>,
|
||||
crtcs: &AHashMap<DrmCrtc, Rc<MetalCrtc>>,
|
||||
) -> Result<MetalEncoder, DrmError> {
|
||||
let info = master.get_encoder_info(encoder)?;
|
||||
let mut possible = AHashMap::new();
|
||||
for crtc in crtcs.values() {
|
||||
if info.possible_crtcs.contains(1 << crtc.idx) {
|
||||
possible.insert(crtc.id, crtc.clone());
|
||||
}
|
||||
}
|
||||
Ok(MetalEncoder {
|
||||
id: encoder,
|
||||
crtcs: possible,
|
||||
})
|
||||
}
|
||||
|
||||
fn create_crtc(
|
||||
crtc: DrmCrtc,
|
||||
idx: usize,
|
||||
master: &Rc<DrmMaster>,
|
||||
planes: &AHashMap<DrmPlane, Rc<MetalPlane>>,
|
||||
) -> Result<MetalCrtc, DrmError> {
|
||||
let mask = 1 << idx;
|
||||
let mut possible_planes = AHashMap::new();
|
||||
for plane in planes.values() {
|
||||
if plane.possible_crtcs.contains(mask) {
|
||||
possible_planes.insert(plane.id, plane.clone());
|
||||
}
|
||||
}
|
||||
let props = collect_properties(master, crtc)?;
|
||||
Ok(MetalCrtc {
|
||||
id: crtc,
|
||||
idx,
|
||||
master: master.clone(),
|
||||
possible_planes,
|
||||
connector: Cell::new(DrmConnector::NONE),
|
||||
primary_plane: Cell::new(DrmPlane::NONE),
|
||||
cursor_plane: Cell::new(DrmPlane::NONE),
|
||||
active: props.get("ACTIVE")?.map(|v| v == 1),
|
||||
mode_id: props.get("MODE_ID")?.map(|v| DrmBlob(v as u32)),
|
||||
mode_blob: Default::default(),
|
||||
})
|
||||
}
|
||||
|
||||
fn create_plane(plane: DrmPlane, master: &Rc<DrmMaster>) -> Result<MetalPlane, DrmError> {
|
||||
let info = master.get_plane_info(plane)?;
|
||||
let mut formats = AHashMap::new();
|
||||
for format in info.format_types {
|
||||
if let Some(f) = crate::format::formats().get(&format) {
|
||||
formats.insert(format, *f);
|
||||
} else {
|
||||
// log::warn!(
|
||||
// "{:?} supports unknown format '{:?}'",
|
||||
// plane,
|
||||
// crate::format::debug(format)
|
||||
// );
|
||||
}
|
||||
}
|
||||
let props = collect_properties(master, plane)?;
|
||||
let ty = match props.props.get(b"type".as_bstr()) {
|
||||
Some((def, val)) => match &def.ty {
|
||||
DrmPropertyType::Enum { values, .. } => 'ty: {
|
||||
for v in values {
|
||||
if v.value == *val {
|
||||
match v.name.as_bytes() {
|
||||
b"Overlay" => break 'ty PlaneType::Overlay,
|
||||
b"Primary" => break 'ty PlaneType::Primary,
|
||||
b"Cursor" => break 'ty PlaneType::Cursor,
|
||||
_ => return Err(DrmError::UnknownPlaneType(v.name.to_owned())),
|
||||
}
|
||||
}
|
||||
}
|
||||
return Err(DrmError::InvalidPlaneType(*val));
|
||||
}
|
||||
_ => return Err(DrmError::InvalidPlaneTypeProperty),
|
||||
},
|
||||
_ => {
|
||||
return Err(DrmError::MissingProperty(
|
||||
"type".to_string().into_boxed_str(),
|
||||
))
|
||||
}
|
||||
};
|
||||
Ok(MetalPlane {
|
||||
id: plane,
|
||||
master: master.clone(),
|
||||
ty,
|
||||
possible_crtcs: info.possible_crtcs,
|
||||
formats,
|
||||
fb: Default::default(),
|
||||
fb_id: props.get("FB_ID")?.map(|v| DrmFb(v as _)),
|
||||
crtc_id: props.get("CRTC_ID")?.map(|v| DrmCrtc(v as _)),
|
||||
crtc_x: props.get("CRTC_X")?.map(|v| v as i32),
|
||||
crtc_y: props.get("CRTC_Y")?.map(|v| v as i32),
|
||||
crtc_w: props.get("CRTC_W")?.map(|v| v as i32),
|
||||
crtc_h: props.get("CRTC_H")?.map(|v| v as i32),
|
||||
src_x: props.get("SRC_X")?.map(|v| v as u32),
|
||||
src_y: props.get("SRC_Y")?.map(|v| v as u32),
|
||||
src_w: props.get("SRC_W")?.map(|v| v as u32),
|
||||
src_h: props.get("SRC_H")?.map(|v| v as u32),
|
||||
})
|
||||
}
|
||||
|
||||
fn collect_properties<T: DrmObject>(
|
||||
master: &Rc<DrmMaster>,
|
||||
t: T,
|
||||
) -> Result<CollectedProperties, DrmError> {
|
||||
let mut props = AHashMap::new();
|
||||
for prop in master.get_properties(t)? {
|
||||
let def = master.get_property(prop.id)?;
|
||||
props.insert(def.name.clone(), (def, prop.value));
|
||||
}
|
||||
Ok(CollectedProperties { props })
|
||||
}
|
||||
|
||||
struct CollectedProperties {
|
||||
props: AHashMap<BString, (DrmPropertyDefinition, u64)>,
|
||||
}
|
||||
|
||||
impl CollectedProperties {
|
||||
fn get(&self, name: &str) -> Result<MutableProperty<u64>, DrmError> {
|
||||
match self.props.get(name.as_bytes().as_bstr()) {
|
||||
Some((def, value)) => Ok(MutableProperty {
|
||||
id: def.id,
|
||||
value: Cell::new(*value),
|
||||
}),
|
||||
_ => Err(DrmError::MissingProperty(name.to_string().into_boxed_str())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MutableProperty<T: Copy> {
|
||||
pub id: DrmProperty,
|
||||
pub value: Cell<T>,
|
||||
}
|
||||
|
||||
impl<T: Copy> MutableProperty<T> {
|
||||
fn map<U: Copy, F>(self, f: F) -> MutableProperty<U>
|
||||
where
|
||||
F: FnOnce(T) -> U,
|
||||
{
|
||||
MutableProperty {
|
||||
id: self.id,
|
||||
value: Cell::new(f(self.value.into_inner())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MetalBackend {
|
||||
pub fn creat_drm_device(
|
||||
&self,
|
||||
pending: PendingDrmDevice,
|
||||
master: &Rc<DrmMaster>,
|
||||
) -> Result<MetalDrmDevice, MetalError> {
|
||||
if let Err(e) = master.set_client_cap(DRM_CLIENT_CAP_ATOMIC, 2) {
|
||||
return Err(MetalError::AtomicModesetting(e));
|
||||
}
|
||||
let resources = master.get_resources()?;
|
||||
|
||||
let mut planes = AHashMap::new();
|
||||
for plane in master.get_planes()? {
|
||||
match create_plane(plane, master) {
|
||||
Ok(p) => {
|
||||
planes.insert(p.id, Rc::new(p));
|
||||
}
|
||||
Err(e) => return Err(MetalError::CreatePlane(e)),
|
||||
}
|
||||
}
|
||||
|
||||
let mut crtcs = AHashMap::new();
|
||||
for (idx, crtc) in resources.crtcs.iter().copied().enumerate() {
|
||||
match create_crtc(crtc, idx, master, &planes) {
|
||||
Ok(c) => {
|
||||
crtcs.insert(c.id, Rc::new(c));
|
||||
}
|
||||
Err(e) => return Err(MetalError::CreateCrtc(e)),
|
||||
}
|
||||
}
|
||||
|
||||
let mut encoders = AHashMap::new();
|
||||
for encoder in resources.encoders {
|
||||
match create_encoder(encoder, master, &crtcs) {
|
||||
Ok(e) => {
|
||||
encoders.insert(e.id, Rc::new(e));
|
||||
}
|
||||
Err(e) => return Err(MetalError::CreateEncoder(e)),
|
||||
}
|
||||
}
|
||||
|
||||
let gbm = match GbmDevice::new(master) {
|
||||
Ok(g) => g,
|
||||
Err(e) => return Err(MetalError::GbmDevice(e)),
|
||||
};
|
||||
let egl = match RenderContext::from_drm_device(master) {
|
||||
Ok(r) => Rc::new(r),
|
||||
Err(e) => return Err(MetalError::CreateRenderContex(e)),
|
||||
};
|
||||
|
||||
let dev = MetalDrmDeviceStatic {
|
||||
id: pending.id,
|
||||
devnum: pending.devnum,
|
||||
devnode: pending.devnode,
|
||||
master: master.clone(),
|
||||
crtcs,
|
||||
encoders,
|
||||
planes,
|
||||
min_width: resources.min_width,
|
||||
max_width: resources.max_width,
|
||||
min_height: resources.min_height,
|
||||
max_height: resources.max_height,
|
||||
gbm,
|
||||
egl,
|
||||
};
|
||||
|
||||
let connectors = get_connectors(&self.state, &dev, &resources.connectors)?;
|
||||
|
||||
let slf = MetalDrmDevice {
|
||||
dev: Rc::new(dev),
|
||||
connectors,
|
||||
};
|
||||
|
||||
self.reset_drm_device(&slf)?;
|
||||
|
||||
Ok(slf)
|
||||
}
|
||||
|
||||
pub fn refresh_drm_device(&self, dev: MetalDrmDevice) -> Result<MetalDrmDevice, DrmError> {
|
||||
let resources = dev.dev.master.get_resources()?;
|
||||
let connectors = get_connectors(&self.state, &dev.dev, &resources.connectors)?;
|
||||
Ok(MetalDrmDevice {
|
||||
dev: dev.dev.clone(),
|
||||
connectors,
|
||||
})
|
||||
}
|
||||
|
||||
fn reset_drm_device(&self, dev: &MetalDrmDevice) -> Result<(), DrmError> {
|
||||
let mut changes = dev.dev.master.change(DRM_MODE_ATOMIC_ALLOW_MODESET);
|
||||
for connector in dev.connectors.values() {
|
||||
if connector.crtc_id.value.take().is_some() {
|
||||
changes.change_object(connector.id, |c| {
|
||||
c.change(connector.crtc_id.id, 0);
|
||||
})
|
||||
}
|
||||
}
|
||||
for plane in dev.dev.planes.values() {
|
||||
changes.change_object(plane.id, |c| {
|
||||
if plane.crtc_id.value.take().is_some() {
|
||||
c.change(plane.crtc_id.id, 0);
|
||||
}
|
||||
if plane.fb_id.value.take().is_some() {
|
||||
c.change(plane.fb_id.id, 0);
|
||||
}
|
||||
})
|
||||
}
|
||||
for crtc in dev.dev.crtcs.values() {
|
||||
changes.change_object(crtc.id, |c| {
|
||||
if crtc.active.value.take() {
|
||||
c.change(crtc.active.id, 0);
|
||||
}
|
||||
if crtc.mode_id.value.take().is_some() {
|
||||
c.change(crtc.mode_id.id, 0);
|
||||
}
|
||||
})
|
||||
}
|
||||
if let Err(e) = changes.commit(0) {
|
||||
return Err(DrmError::ResetFailed(Box::new(e)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn init_drm_device(&self, dev: &Rc<MetalDrmDevice>) {
|
||||
for connector in dev.connectors.values() {
|
||||
if let Err(e) = self.init_drm_connector(dev, connector) {
|
||||
log::error!("Could not initialize drm connector: {}", ErrorFmt(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn init_drm_connector(
|
||||
&self,
|
||||
dev: &Rc<MetalDrmDevice>,
|
||||
connector: &Rc<MetalConnector>,
|
||||
) -> Result<(), MetalError> {
|
||||
if connector.connection != ConnectorStatus::Connected {
|
||||
return Ok(());
|
||||
}
|
||||
let crtc = 'crtc: {
|
||||
for crtc in connector.crtcs.values() {
|
||||
if crtc.connector.get().is_none() {
|
||||
break 'crtc crtc.clone();
|
||||
}
|
||||
}
|
||||
return Err(MetalError::NoCrtcForConnector);
|
||||
};
|
||||
let primary_plane = 'plane: {
|
||||
for plane in crtc.possible_planes.values() {
|
||||
if plane.ty == PlaneType::Primary
|
||||
&& plane.crtc_id.value.get().is_none()
|
||||
&& plane.formats.contains_key(&XRGB8888.drm)
|
||||
{
|
||||
break 'plane plane.clone();
|
||||
}
|
||||
}
|
||||
return Err(MetalError::NoPrimaryPlaneForConnector);
|
||||
};
|
||||
let mode = match connector.modes.first() {
|
||||
Some(m) => m,
|
||||
_ => return Err(MetalError::NoModeForConnector),
|
||||
};
|
||||
let mode_blob = mode.create_blob(&connector.master)?;
|
||||
let format = ModifiedFormat {
|
||||
format: XRGB8888,
|
||||
modifier: INVALID_MODIFIER,
|
||||
};
|
||||
let bo = dev.dev.gbm.create_bo(
|
||||
mode.hdisplay as i32,
|
||||
mode.vdisplay as i32,
|
||||
&format,
|
||||
GBM_BO_USE_RENDERING | GBM_BO_USE_SCANOUT,
|
||||
);
|
||||
let bo = match bo {
|
||||
Ok(b) => b,
|
||||
Err(e) => return Err(MetalError::ScanoutBuffer(e)),
|
||||
};
|
||||
let drm_fb = match connector.master.add_fb(&bo) {
|
||||
Ok(fb) => Rc::new(fb),
|
||||
Err(e) => return Err(MetalError::Framebuffer(e)),
|
||||
};
|
||||
let egl_fb = match dev.dev.egl.dmabuf_fb(&bo.dma()) {
|
||||
Ok(fb) => fb,
|
||||
Err(e) => return Err(MetalError::ImportFb(e)),
|
||||
};
|
||||
let mut changes = connector.master.change(DRM_MODE_ATOMIC_ALLOW_MODESET);
|
||||
changes.change_object(connector.id, |c| {
|
||||
c.change(connector.crtc_id.id, crtc.id.0 as _);
|
||||
});
|
||||
changes.change_object(crtc.id, |c| {
|
||||
c.change(crtc.active.id, 1);
|
||||
c.change(crtc.mode_id.id, mode_blob.id().0 as _);
|
||||
});
|
||||
changes.change_object(primary_plane.id, |c| {
|
||||
c.change(primary_plane.fb_id.id, drm_fb.id().0 as _);
|
||||
c.change(primary_plane.crtc_id.id, crtc.id.0 as _);
|
||||
c.change(primary_plane.crtc_x.id, 0);
|
||||
c.change(primary_plane.crtc_y.id, 0);
|
||||
c.change(primary_plane.crtc_w.id, mode.hdisplay as _);
|
||||
c.change(primary_plane.crtc_h.id, mode.vdisplay as _);
|
||||
c.change(primary_plane.src_x.id, 0);
|
||||
c.change(primary_plane.src_y.id, 0);
|
||||
c.change(primary_plane.src_w.id, (mode.hdisplay as u64) << 16);
|
||||
c.change(primary_plane.src_h.id, (mode.vdisplay as u64) << 16);
|
||||
});
|
||||
if let Err(e) = changes.commit(0) {
|
||||
return Err(MetalError::Configure(e));
|
||||
}
|
||||
connector.crtc_id.value.set(crtc.id);
|
||||
connector.egl_fb.set(Some(egl_fb));
|
||||
connector.mode.set(Some(Rc::new(mode.clone())));
|
||||
crtc.connector.set(connector.id);
|
||||
crtc.active.value.set(true);
|
||||
crtc.mode_id.value.set(mode_blob.id());
|
||||
crtc.mode_blob.set(Some(Rc::new(mode_blob)));
|
||||
primary_plane.fb_id.value.set(drm_fb.id());
|
||||
primary_plane.fb.set(Some(drm_fb));
|
||||
primary_plane.crtc_id.value.set(crtc.id);
|
||||
primary_plane.crtc_x.value.set(0);
|
||||
primary_plane.crtc_y.value.set(0);
|
||||
primary_plane.crtc_w.value.set(mode.hdisplay as _);
|
||||
primary_plane.crtc_h.value.set(mode.vdisplay as _);
|
||||
primary_plane.src_x.value.set(0);
|
||||
primary_plane.src_y.value.set(0);
|
||||
primary_plane.src_w.value.set((mode.hdisplay as u32) << 16);
|
||||
primary_plane.src_h.value.set((mode.vdisplay as u32) << 16);
|
||||
self.state
|
||||
.backend_events
|
||||
.push(BackendEvent::NewOutput(connector.clone()));
|
||||
log::info!(
|
||||
"Initialized connector {}-{} with mode {:?}",
|
||||
connector.connector_type,
|
||||
connector.connector_type_id,
|
||||
mode
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,7 @@
|
|||
use crate::backend::{Backend, BackendEvent, KeyState, InputEvent, Output, OutputId, ScrollAxis, InputDeviceId, InputDevice};
|
||||
use crate::backend::{
|
||||
Backend, BackendEvent, InputDevice, InputDeviceId, InputEvent, KeyState, Output, OutputId,
|
||||
ScrollAxis,
|
||||
};
|
||||
use crate::drm::drm::{Drm, DrmError};
|
||||
use crate::drm::gbm::{GbmDevice, GbmError, GBM_BO_USE_RENDERING};
|
||||
use crate::drm::{ModifiedFormat, INVALID_MODIFIER};
|
||||
|
|
@ -97,7 +100,7 @@ impl XcbCon {
|
|||
|
||||
let c = xcb.xcb_connect(ptr::null(), ptr::null_mut());
|
||||
match xcb.xcb_connection_has_error(c) {
|
||||
0 => { },
|
||||
0 => {}
|
||||
n => return Err(XorgBackendError::CannotConnect(n.into())),
|
||||
}
|
||||
let errors = XcbErrorParser::new(&xcb, c);
|
||||
|
|
@ -231,7 +234,7 @@ fn get_drm(con: &XcbCon) -> Result<Drm, XorgBackendError> {
|
|||
assert!(res.nfd == 1);
|
||||
let fd = *con.dri.xcb_dri3_open_reply_fds(con.c, &mut *res);
|
||||
let fd = OwnedFd::new(fd);
|
||||
Ok(Drm::new(fd.raw(), true)?)
|
||||
Ok(Drm::reopen(fd.raw(), true)?)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -574,10 +577,14 @@ impl XorgBackend {
|
|||
self.mouse_seats.set(info.attachment, seat.clone());
|
||||
self.state
|
||||
.backend_events
|
||||
.push(BackendEvent::NewInputDevice(Rc::new(XorgSeatMouse(seat.clone()))));
|
||||
.push(BackendEvent::NewInputDevice(Rc::new(XorgSeatMouse(
|
||||
seat.clone(),
|
||||
))));
|
||||
self.state
|
||||
.backend_events
|
||||
.push(BackendEvent::NewInputDevice(Rc::new(XorgSeatKeyboard(seat.clone()))));
|
||||
.push(BackendEvent::NewInputDevice(Rc::new(XorgSeatKeyboard(
|
||||
seat.clone(),
|
||||
))));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1118,7 +1125,11 @@ impl InputDevice for XorgSeatKeyboard {
|
|||
}
|
||||
};
|
||||
if res.status != ffi::XCB_GRAB_STATUS_SUCCESS as _ {
|
||||
log::error!("Could not grab device {}: status = {}", self.0.kb, res.status);
|
||||
log::error!(
|
||||
"Could not grab device {}: status = {}",
|
||||
self.0.kb,
|
||||
res.status
|
||||
);
|
||||
}
|
||||
} else {
|
||||
let cookie = con
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
mod handler;
|
||||
|
||||
use crate::backend::InputDeviceId;
|
||||
use crate::config::handler::ConfigProxyHandler;
|
||||
use crate::ifs::wl_seat::SeatId;
|
||||
use crate::utils::ptr_ext::PtrExt;
|
||||
|
|
@ -13,7 +14,6 @@ use std::cell::Cell;
|
|||
use std::ptr;
|
||||
use std::rc::Rc;
|
||||
use thiserror::Error;
|
||||
use crate::backend::InputDeviceId;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ConfigError {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
use crate::backend::InputDeviceId;
|
||||
use crate::ifs::wl_seat::{SeatId, WlSeatGlobal};
|
||||
use crate::state::DeviceHandlerData;
|
||||
use crate::tree::walker::NodeVisitorBase;
|
||||
|
|
@ -19,7 +20,6 @@ use log::Level;
|
|||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
use thiserror::Error;
|
||||
use crate::backend::InputDeviceId;
|
||||
|
||||
pub(super) struct ConfigProxyHandler {
|
||||
pub client_data: Cell<*const u8>,
|
||||
|
|
@ -157,11 +157,11 @@ impl ConfigProxyHandler {
|
|||
device: InputDevice,
|
||||
) -> Result<Rc<DeviceHandlerData>, CphError> {
|
||||
let data = self
|
||||
.state
|
||||
.input_device_handlers
|
||||
.borrow_mut()
|
||||
.get(&InputDeviceId::from_raw(device.0 as _))
|
||||
.map(|d| d.data.clone());
|
||||
.state
|
||||
.input_device_handlers
|
||||
.borrow_mut()
|
||||
.get(&InputDeviceId::from_raw(device.0 as _))
|
||||
.map(|d| d.data.clone());
|
||||
match data {
|
||||
Some(d) => Ok(d),
|
||||
_ => Err(CphError::DeviceDoesNotExist(device)),
|
||||
|
|
@ -332,6 +332,11 @@ impl ConfigProxyHandler {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_quit(&self) {
|
||||
log::info!("Quitting");
|
||||
self.state.el.stop();
|
||||
}
|
||||
|
||||
fn handle_toggle_floating(&self, seat: Seat) -> Result<(), FocusParentError> {
|
||||
let seat = self.get_seat(seat)?;
|
||||
seat.toggle_floating();
|
||||
|
|
@ -466,6 +471,7 @@ impl ConfigProxyHandler {
|
|||
ClientMessage::CreateSplit { seat, axis } => self.handle_create_split(seat, axis)?,
|
||||
ClientMessage::FocusParent { seat } => self.handle_focus_parent(seat)?,
|
||||
ClientMessage::ToggleFloating { seat } => self.handle_toggle_floating(seat)?,
|
||||
ClientMessage::Quit => self.handle_quit(),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
1057
src/drm/drm.rs
1057
src/drm/drm.rs
File diff suppressed because it is too large
Load diff
1034
src/drm/drm/sys.rs
Normal file
1034
src/drm/drm/sys.rs
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -2,10 +2,12 @@ use crate::drm::dma::{DmaBuf, DmaBufPlane};
|
|||
use crate::drm::drm::{Drm, DrmError};
|
||||
use crate::drm::{ModifiedFormat, INVALID_MODIFIER};
|
||||
use crate::format::formats;
|
||||
use crate::utils::oserror::OsError;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::ptr;
|
||||
use std::rc::Rc;
|
||||
use thiserror::Error;
|
||||
use uapi::{c, OwnedFd};
|
||||
use uapi::{c, Errno, OwnedFd};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum GbmError {
|
||||
|
|
@ -19,12 +21,13 @@ pub enum GbmError {
|
|||
UnknownFormat,
|
||||
#[error("Could not retrieve a drm-buf fd")]
|
||||
DrmFd,
|
||||
#[error("Could not retrieve a GEM handle")]
|
||||
GemHandle(#[source] OsError),
|
||||
}
|
||||
|
||||
type Device = u8;
|
||||
type Bo = u8;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub const GBM_BO_USE_SCANOUT: u32 = 1 << 0;
|
||||
#[allow(dead_code)]
|
||||
pub const GBM_BO_USE_CURSOR: u32 = 1 << 1;
|
||||
|
|
@ -36,6 +39,16 @@ pub const GBM_BO_USE_LINEAR: u32 = 1 << 4;
|
|||
#[allow(dead_code)]
|
||||
pub const GBM_BO_USE_PROTECTED: u32 = 1 << 5;
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[repr(C)]
|
||||
union gbm_bo_handle {
|
||||
ptr: *mut u8,
|
||||
s32: i32,
|
||||
u32: u32,
|
||||
s64: i64,
|
||||
u64: u64,
|
||||
}
|
||||
|
||||
#[link(name = "gbm")]
|
||||
extern "C" {
|
||||
fn gbm_create_device(fd: c::c_int) -> *mut Device;
|
||||
|
|
@ -59,6 +72,7 @@ extern "C" {
|
|||
fn gbm_bo_get_modifier(bo: *mut Bo) -> u64;
|
||||
fn gbm_bo_get_stride_for_plane(bo: *mut Bo, plane: c::c_int) -> u32;
|
||||
fn gbm_bo_get_fd_for_plane(bo: *mut Bo, plane: c::c_int) -> c::c_int;
|
||||
fn gbm_bo_get_handle_for_plane(bo: *mut Bo, plane: c::c_int) -> gbm_bo_handle;
|
||||
fn gbm_bo_get_offset(bo: *mut Bo, plane: c::c_int) -> u32;
|
||||
fn gbm_bo_get_format(bo: *mut Bo) -> u32;
|
||||
#[allow(dead_code)]
|
||||
|
|
@ -70,6 +84,12 @@ pub struct GbmDevice {
|
|||
dev: *mut Device,
|
||||
}
|
||||
|
||||
impl Debug for GbmDevice {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("GbmDevice").finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
struct BoHolder {
|
||||
bo: *mut Bo,
|
||||
}
|
||||
|
|
@ -77,6 +97,7 @@ struct BoHolder {
|
|||
pub struct GbmBo {
|
||||
_bo: BoHolder,
|
||||
dma: DmaBuf,
|
||||
handles: Vec<u32>,
|
||||
}
|
||||
|
||||
unsafe fn export_bo(bo: *mut Bo) -> Result<DmaBuf, GbmError> {
|
||||
|
|
@ -111,6 +132,18 @@ unsafe fn export_bo(bo: *mut Bo) -> Result<DmaBuf, GbmError> {
|
|||
})
|
||||
}
|
||||
|
||||
unsafe fn export_handles(bo: *mut Bo) -> Result<Vec<u32>, GbmError> {
|
||||
let mut planes = vec![];
|
||||
for plane in 0..gbm_bo_get_plane_count(bo) {
|
||||
let handle = gbm_bo_get_handle_for_plane(bo, plane);
|
||||
if handle.s32 < 0 {
|
||||
return Err(GbmError::GemHandle(Errno::default().into()));
|
||||
}
|
||||
planes.push(handle.u32);
|
||||
}
|
||||
Ok(planes)
|
||||
}
|
||||
|
||||
impl GbmDevice {
|
||||
pub fn new(drm: &Drm) -> Result<Self, GbmError> {
|
||||
let drm = drm.dup_unprivileged()?;
|
||||
|
|
@ -149,7 +182,12 @@ impl GbmDevice {
|
|||
}
|
||||
let bo = BoHolder { bo };
|
||||
let dma = export_bo(bo.bo)?;
|
||||
Ok(GbmBo { _bo: bo, dma })
|
||||
let handles = export_handles(bo.bo)?;
|
||||
Ok(GbmBo {
|
||||
_bo: bo,
|
||||
dma,
|
||||
handles,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -166,6 +204,10 @@ impl GbmBo {
|
|||
pub fn dma(&self) -> &DmaBuf {
|
||||
&self.dma
|
||||
}
|
||||
|
||||
pub fn gem(&self) -> &[u32] {
|
||||
&self.handles
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for BoHolder {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
use crate::pixman;
|
||||
use crate::render::sys::{GLint, GL_BGRA_EXT, GL_UNSIGNED_BYTE};
|
||||
use crate::utils::debug_fn::debug_fn;
|
||||
use ahash::AHashMap;
|
||||
use once_cell::sync::Lazy;
|
||||
use std::fmt::{Debug, Write};
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
pub struct Format {
|
||||
|
|
@ -32,6 +34,16 @@ const fn fourcc_code(a: char, b: char, c: char, d: char) -> u32 {
|
|||
(a as u32) | ((b as u32) << 8) | ((c as u32) << 16) | ((d as u32) << 24)
|
||||
}
|
||||
|
||||
pub fn debug(fourcc: u32) -> impl Debug {
|
||||
debug_fn(move |fmt| {
|
||||
fmt.write_char(fourcc as u8 as char)?;
|
||||
fmt.write_char((fourcc >> 8) as u8 as char)?;
|
||||
fmt.write_char((fourcc >> 16) as u8 as char)?;
|
||||
fmt.write_char((fourcc >> 24) as u8 as char)?;
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
const ARGB8888_ID: u32 = 0;
|
||||
const ARGB8888_DRM: u32 = fourcc_code('A', 'R', '2', '4');
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::backend::{KeyState, InputEvent, OutputId, ScrollAxis};
|
||||
use crate::backend::{InputEvent, KeyState, OutputId, ScrollAxis};
|
||||
use crate::client::{Client, ClientId};
|
||||
use crate::fixed::Fixed;
|
||||
use crate::ifs::ipc;
|
||||
|
|
@ -136,7 +136,6 @@ impl WlSeatGlobal {
|
|||
}
|
||||
|
||||
fn motion_event(self: &Rc<Self>, dx: Fixed, dy: Fixed) {
|
||||
log::info!("motion: {}x{}", dx, dy);
|
||||
let (x, y) = self.pos.get();
|
||||
self.set_new_position(x + dx, y + dy);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
use crate::libinput::sys::{libinput_device, libinput_device_get_user_data, libinput_device_set_user_data, libinput_device_unref, libinput_path_remove_device};
|
||||
use crate::libinput::sys::{
|
||||
libinput_device, libinput_device_get_user_data, libinput_device_set_user_data,
|
||||
libinput_device_unref, libinput_path_remove_device,
|
||||
};
|
||||
use crate::libinput::LibInput;
|
||||
use std::marker::PhantomData;
|
||||
use std::rc::Rc;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,13 @@
|
|||
use crate::libinput::consts::{EventType, KeyState};
|
||||
use crate::libinput::device::LibInputDevice;
|
||||
use crate::libinput::sys::{libinput_event, libinput_event_destroy, libinput_event_get_device, libinput_event_get_keyboard_event, libinput_event_get_pointer_event, libinput_event_get_type, libinput_event_keyboard, libinput_event_keyboard_get_key, libinput_event_keyboard_get_key_state, libinput_event_keyboard_get_time_usec, libinput_event_pointer, libinput_event_pointer_get_dx, libinput_event_pointer_get_dy, libinput_event_pointer_get_time_usec};
|
||||
use crate::libinput::sys::{
|
||||
libinput_event, libinput_event_destroy, libinput_event_get_device,
|
||||
libinput_event_get_keyboard_event, libinput_event_get_pointer_event, libinput_event_get_type,
|
||||
libinput_event_keyboard, libinput_event_keyboard_get_key,
|
||||
libinput_event_keyboard_get_key_state, libinput_event_keyboard_get_time_usec,
|
||||
libinput_event_pointer, libinput_event_pointer_get_dx, libinput_event_pointer_get_dy,
|
||||
libinput_event_pointer_get_time_usec,
|
||||
};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub struct LibInputEvent<'a> {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use crate::dbus::{DbusError, DbusSocket};
|
||||
use crate::org::freedesktop::login1::session::TakeDeviceReply;
|
||||
use crate::dbus::{DbusError, DbusSocket, SignalHandler};
|
||||
use crate::org::freedesktop::login1::session::{PauseDevice, ResumeDevice, TakeDeviceReply};
|
||||
use crate::{org, FALSE};
|
||||
use std::rc::Rc;
|
||||
use thiserror::Error;
|
||||
|
|
@ -91,4 +91,36 @@ impl Session {
|
|||
move |r| f(r),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn on_pause<F>(&self, f: F) -> Result<SignalHandler, DbusError>
|
||||
where
|
||||
F: for<'b> Fn(PauseDevice<'b>) + 'static,
|
||||
{
|
||||
self.socket
|
||||
.handle_signal::<org::freedesktop::login1::session::PauseDevice, _>(
|
||||
Some(LOGIND_NAME),
|
||||
Some(&self.session_path),
|
||||
move |v| f(v),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn on_resume<F>(&self, f: F) -> Result<SignalHandler, DbusError>
|
||||
where
|
||||
F: Fn(ResumeDevice) + 'static,
|
||||
{
|
||||
self.socket
|
||||
.handle_signal::<org::freedesktop::login1::session::ResumeDevice, _>(
|
||||
Some(LOGIND_NAME),
|
||||
Some(&self.session_path),
|
||||
move |v| f(v),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn device_paused(&self, major: u32, minor: u32) {
|
||||
self.socket.call_noreply(
|
||||
LOGIND_NAME,
|
||||
&self.session_path,
|
||||
org::freedesktop::login1::session::PauseDeviceComplete { major, minor },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::drm::drm::DrmDevice;
|
||||
use crate::drm::drm::NodeType;
|
||||
use crate::render::egl::device::EglDevice;
|
||||
use crate::render::egl::sys::{
|
||||
eglBindAPI, EGLAttrib, EGLLabelKHR, EGLenum, EGLint, EGL_DEBUG_MSG_CRITICAL_KHR,
|
||||
|
|
@ -8,10 +8,11 @@ use crate::render::egl::sys::{
|
|||
use crate::render::ext::{get_client_ext, get_device_ext, ClientExt, DeviceExt};
|
||||
use crate::render::proc::ExtProc;
|
||||
use crate::render::RenderError;
|
||||
use ahash::AHashMap;
|
||||
use bstr::ByteSlice;
|
||||
use log::Level;
|
||||
use once_cell::sync::Lazy;
|
||||
use std::ffi::CStr;
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::ptr;
|
||||
use sys::{
|
||||
EGL_BAD_ACCESS, EGL_BAD_ALLOC, EGL_BAD_ATTRIBUTE, EGL_BAD_CONFIG, EGL_BAD_CONTEXT,
|
||||
|
|
@ -63,12 +64,14 @@ pub fn init() -> Result<(), RenderError> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn find_drm_device(drm_dev: &DrmDevice) -> Result<Option<EglDevice>, RenderError> {
|
||||
pub fn find_drm_device(
|
||||
drm_dev: &AHashMap<NodeType, CString>,
|
||||
) -> Result<Option<EglDevice>, RenderError> {
|
||||
for device in query_devices()? {
|
||||
if device.exts.contains(DeviceExt::EXT_DEVICE_DRM) {
|
||||
let device_file = device.query_string(EGL_DRM_DEVICE_FILE_EXT)?;
|
||||
for (_, name) in drm_dev.nodes() {
|
||||
if device_file == name {
|
||||
for name in drm_dev.values() {
|
||||
if device_file == &**name {
|
||||
return Ok(Some(device));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use crate::drm::dma::DmaBuf;
|
||||
use crate::drm::drm::{Drm, DRM_NODE_RENDER};
|
||||
use crate::drm::drm::{Drm, NodeType};
|
||||
use crate::format::{Format, XRGB8888};
|
||||
use crate::render::egl::context::EglContext;
|
||||
use crate::render::egl::find_drm_device;
|
||||
|
|
@ -15,6 +15,7 @@ use ahash::AHashMap;
|
|||
use renderdoc::{RenderDoc, V100};
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::ffi::CString;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::rc::Rc;
|
||||
use uapi::ustr;
|
||||
|
||||
|
|
@ -51,14 +52,20 @@ pub struct RenderContext {
|
|||
pub(super) fill_prog_color: GLint,
|
||||
}
|
||||
|
||||
impl Debug for RenderContext {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("RenderContext").finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderContext {
|
||||
pub fn from_drm_device(drm: &Drm) -> Result<Self, RenderError> {
|
||||
let drm_dev = drm.get_device()?;
|
||||
let node = match drm_dev.nodes().find(|(ty, _)| *ty == DRM_NODE_RENDER) {
|
||||
let nodes = drm.get_nodes()?;
|
||||
let node = match nodes.get(&NodeType::Render) {
|
||||
None => return Err(RenderError::NoRenderNode),
|
||||
Some((_, n)) => Rc::new(n.to_owned()),
|
||||
Some(path) => Rc::new(path.to_owned()),
|
||||
};
|
||||
let egl_dev = match find_drm_device(&drm_dev)? {
|
||||
let egl_dev = match find_drm_device(&nodes)? {
|
||||
Some(d) => d,
|
||||
None => return Err(RenderError::UnknownDrmDevice),
|
||||
};
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ use crate::render::renderer::renderer::Renderer;
|
|||
use crate::render::sys::{glBlendFunc, GL_ONE, GL_ONE_MINUS_SRC_ALPHA};
|
||||
use crate::tree::Node;
|
||||
use crate::State;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::ptr;
|
||||
use std::rc::Rc;
|
||||
|
||||
|
|
@ -16,6 +17,12 @@ pub struct Framebuffer {
|
|||
pub(super) gl: GlFrameBuffer,
|
||||
}
|
||||
|
||||
impl Debug for Framebuffer {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Framebuffer").finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
impl Framebuffer {
|
||||
pub fn render(&self, node: &dyn Node, state: &State, cursor_rect: Option<Rect>) {
|
||||
let _ = self.ctx.ctx.with_current(|| {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
use crate::async_engine::{AsyncEngine, SpawnedFuture};
|
||||
use crate::backend::{BackendEvent, InputDevice, InputDeviceId, InputDeviceIds, OutputId, OutputIds};
|
||||
use crate::backend::{
|
||||
BackendEvent, InputDevice, InputDeviceId, InputDeviceIds, OutputId, OutputIds,
|
||||
};
|
||||
use crate::client::{Client, Clients};
|
||||
use crate::config::ConfigProxy;
|
||||
use crate::cursor::ServerCursors;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::backend::{InputDevice};
|
||||
use crate::backend::InputDevice;
|
||||
use crate::state::{DeviceHandlerData, InputDeviceData};
|
||||
use crate::utils::asyncevent::AsyncEvent;
|
||||
use crate::State;
|
||||
|
|
@ -64,6 +64,9 @@ impl DeviceHandler {
|
|||
if let Some(config) = self.state.config.get() {
|
||||
config.del_input_device(self.dev.id());
|
||||
}
|
||||
self.state.input_device_handlers.borrow_mut().remove(&self.dev.id());
|
||||
self.state
|
||||
.input_device_handlers
|
||||
.borrow_mut()
|
||||
.remove(&self.dev.id());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ mod start_backend;
|
|||
use crate::tasks::backend::BackendEventHandler;
|
||||
use crate::tasks::slow_clients::SlowClientHandler;
|
||||
use crate::State;
|
||||
use std::rc::Rc;
|
||||
pub use start_backend::start_backend;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub async fn handle_backend_events(state: Rc<State>) {
|
||||
let mut beh = BackendEventHandler { state };
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use crate::{metal, ErrorFmt, State, XorgBackend};
|
||||
use std::future::pending;
|
||||
use std::rc::Rc;
|
||||
use crate::{ErrorFmt, metal, State, XorgBackend};
|
||||
|
||||
pub async fn start_backend(state: Rc<State>) {
|
||||
log::info!("Trying to start X backend");
|
||||
|
|
|
|||
42
src/udev.rs
42
src/udev.rs
|
|
@ -47,7 +47,10 @@ extern "C" {
|
|||
fn udev_device_get_sysname(udev_device: *mut udev_device) -> *const c::c_char;
|
||||
fn udev_device_get_is_initialized(udev_device: *mut udev_device) -> c::c_int;
|
||||
fn udev_device_get_devnode(udev_device: *mut udev_device) -> *const c::c_char;
|
||||
fn udev_device_get_devtype(udev_device: *mut udev_device) -> *const c::c_char;
|
||||
fn udev_device_get_devnum(udev_device: *mut udev_device) -> c::dev_t;
|
||||
fn udev_device_get_action(udev_device: *mut udev_device) -> *const c::c_char;
|
||||
fn udev_device_get_subsystem(udev_device: *mut udev_device) -> *const c::c_char;
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
|
|
@ -70,10 +73,6 @@ pub enum UdevError {
|
|||
ScanDevices(#[source] crate::utils::oserror::OsError),
|
||||
#[error("Could not create a udev_device from a syspath")]
|
||||
DeviceFromSyspath(#[source] crate::utils::oserror::OsError),
|
||||
#[error("Could not retrieve the sysname of a udev_device")]
|
||||
GetSysname(#[source] crate::utils::oserror::OsError),
|
||||
#[error("Could not retrieve the devnode of a udev_device")]
|
||||
GetDevnode(#[source] crate::utils::oserror::OsError),
|
||||
}
|
||||
|
||||
pub struct Udev {
|
||||
|
|
@ -215,7 +214,7 @@ impl Drop for UdevMonitor {
|
|||
}
|
||||
|
||||
impl UdevEnumerate {
|
||||
pub fn add_match_subsystem(&self, subsystem: &str) -> Result<(), UdevError> {
|
||||
pub fn add_match_subsystem(&self, subsystem: &[u8]) -> Result<(), UdevError> {
|
||||
let subsystem = subsystem.into_ustr();
|
||||
let res = unsafe { udev_enumerate_add_match_subsystem(self.enumerate, subsystem.as_ptr()) };
|
||||
if res < 0 {
|
||||
|
|
@ -283,24 +282,25 @@ impl<'a> UdevListEntry<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl UdevDevice {
|
||||
pub fn sysname(&self) -> Result<&CStr, UdevError> {
|
||||
let res = unsafe { udev_device_get_sysname(self.device) };
|
||||
if res.is_null() {
|
||||
Err(UdevError::GetSysname(Errno::default().into()))
|
||||
} else {
|
||||
unsafe { Ok(CStr::from_ptr(res)) }
|
||||
macro_rules! strfn {
|
||||
($f:ident, $raw:ident) => {
|
||||
pub fn $f(&self) -> Option<&CStr> {
|
||||
let res = unsafe { $raw(self.device) };
|
||||
if res.is_null() {
|
||||
None
|
||||
} else {
|
||||
unsafe { Some(CStr::from_ptr(res)) }
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub fn devnode(&self) -> Result<&CStr, UdevError> {
|
||||
let res = unsafe { udev_device_get_devnode(self.device) };
|
||||
if res.is_null() {
|
||||
Err(UdevError::GetDevnode(Errno::default().into()))
|
||||
} else {
|
||||
unsafe { Ok(CStr::from_ptr(res)) }
|
||||
}
|
||||
}
|
||||
impl UdevDevice {
|
||||
strfn!(sysname, udev_device_get_sysname);
|
||||
strfn!(devnode, udev_device_get_devnode);
|
||||
strfn!(devtype, udev_device_get_devtype);
|
||||
strfn!(action, udev_device_get_action);
|
||||
strfn!(subsystem, udev_device_get_subsystem);
|
||||
|
||||
pub fn devnum(&self) -> c::dev_t {
|
||||
unsafe { udev_device_get_devnum(self.device) }
|
||||
|
|
|
|||
|
|
@ -16,8 +16,8 @@ pub mod queue;
|
|||
pub mod run_toplevel;
|
||||
pub mod smallmap;
|
||||
pub mod stack;
|
||||
pub mod syncqueue;
|
||||
pub mod tri;
|
||||
pub mod vasprintf;
|
||||
pub mod vec_ext;
|
||||
pub mod vecstorage;
|
||||
pub mod syncqueue;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
use once_cell::sync::Lazy;
|
||||
use std::error::Error;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use uapi::c::c_int;
|
||||
use uapi::{c, Errno};
|
||||
|
||||
static ERRORS: Lazy<&'static [Option<&'static str>]> = Lazy::new(|| {
|
||||
|
|
@ -168,6 +169,21 @@ impl From<Errno> for OsError {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<c::c_int> for OsError {
|
||||
fn from(v: c_int) -> Self {
|
||||
Self(v)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for OsError {
|
||||
fn from(v: std::io::Error) -> Self {
|
||||
match v.raw_os_error() {
|
||||
Some(v) => Self(v),
|
||||
None => Self(c::EINVAL),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for OsError {}
|
||||
|
||||
impl Display for OsError {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use crate::utils::ptr_ext::MutPtrExt;
|
||||
use std::cell::UnsafeCell;
|
||||
use std::collections::VecDeque;
|
||||
use crate::utils::ptr_ext::MutPtrExt;
|
||||
|
||||
pub struct SyncQueue<T> {
|
||||
el: UnsafeCell<VecDeque<T>>,
|
||||
|
|
@ -22,8 +22,6 @@ impl<T> SyncQueue<T> {
|
|||
}
|
||||
|
||||
pub fn pop(&self) -> Option<T> {
|
||||
unsafe {
|
||||
self.el.get().deref_mut().pop_front()
|
||||
}
|
||||
unsafe { self.el.get().deref_mut().pop_front() }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue