Skip to content

Commit

Permalink
Change behavior of resource handles
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Slabinski committed May 16, 2019
1 parent 3a25ad0 commit f9cb41c
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 110 deletions.
2 changes: 1 addition & 1 deletion examples/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub use drm::control::Device as ControlDevice;
pub use drm::control::ResourceType;
pub use drm::control::ResourceHandle;
pub use drm::control::property::*;
pub use drm::Device;

Expand Down
35 changes: 20 additions & 15 deletions src/control/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,39 @@ use drm_ffi as ffi;

/// A handle to a connector
#[repr(transparent)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Handle(control::ResourceHandle);
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub struct Handle(control::RawResourceHandle);

impl Into<control::ResourceHandle> for Handle {
fn into(self) -> control::ResourceHandle {
use std::mem::transmute;
unsafe { transmute(self) }
impl Into<control::RawResourceHandle> for Handle {
fn into(self) -> control::RawResourceHandle {
self.0
}
}

impl AsRef<control::ResourceHandle> for Handle {
fn as_ref(&self) -> &control::ResourceHandle {
use std::mem::transmute;
unsafe { transmute(&self.0) }
impl Into<u32> for Handle {
fn into(self) -> u32 {
self.0.into()
}
}

impl AsMut<control::ResourceHandle> for Handle {
fn as_mut(&mut self) -> &mut control::ResourceHandle {
use std::mem::transmute;
unsafe { transmute(&mut self.0) }
impl From<control::RawResourceHandle> for Handle {
fn from(handle: control::RawResourceHandle) -> Self {
Handle(handle)
}
}

impl control::ResourceType for Handle {
impl control::ResourceHandle for Handle {
const FFI_TYPE: u32 = ffi::DRM_MODE_OBJECT_CONNECTOR;
}

impl std::fmt::Debug for Handle {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_tuple("connector::Handle")
.field(&self.0)
.finish()
}
}

/// Information about a connector
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Info {
Expand Down
35 changes: 20 additions & 15 deletions src/control/crtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,39 @@ use control;
use drm_ffi as ffi;

/// A handle to a specific CRTC
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Handle(control::ResourceHandle);
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub struct Handle(control::RawResourceHandle);

impl Into<control::ResourceHandle> for Handle {
fn into(self) -> control::ResourceHandle {
use std::mem::transmute;
unsafe { transmute(self) }
impl Into<control::RawResourceHandle> for Handle {
fn into(self) -> control::RawResourceHandle {
self.0
}
}

impl AsRef<control::ResourceHandle> for Handle {
fn as_ref(&self) -> &control::ResourceHandle {
use std::mem::transmute;
unsafe { transmute(&self.0) }
impl Into<u32> for Handle {
fn into(self) -> u32 {
self.0.into()
}
}

impl AsMut<control::ResourceHandle> for Handle {
fn as_mut(&mut self) -> &mut control::ResourceHandle {
use std::mem::transmute;
unsafe { transmute(&mut self.0) }
impl From<control::RawResourceHandle> for Handle {
fn from(handle: control::RawResourceHandle) -> Self {
Handle(handle)
}
}

impl control::ResourceType for Handle {
impl control::ResourceHandle for Handle {
const FFI_TYPE: u32 = ffi::DRM_MODE_OBJECT_CRTC;
}

impl std::fmt::Debug for Handle {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_tuple("crtc::Handle")
.field(&self.0)
.finish()
}
}

/// Information about a specific CRTC
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Info {
Expand Down
35 changes: 20 additions & 15 deletions src/control/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,39 @@ use control;
use drm_ffi as ffi;

/// A handle to an encoder
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Handle(control::ResourceHandle);
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub struct Handle(control::RawResourceHandle);

impl Into<control::ResourceHandle> for Handle {
fn into(self) -> control::ResourceHandle {
use std::mem::transmute;
unsafe { transmute(self) }
impl Into<control::RawResourceHandle> for Handle {
fn into(self) -> control::RawResourceHandle {
self.0
}
}

impl AsRef<control::ResourceHandle> for Handle {
fn as_ref(&self) -> &control::ResourceHandle {
use std::mem::transmute;
unsafe { transmute(&self.0) }
impl Into<u32> for Handle {
fn into(self) -> u32 {
self.0.into()
}
}

impl AsMut<control::ResourceHandle> for Handle {
fn as_mut(&mut self) -> &mut control::ResourceHandle {
use std::mem::transmute;
unsafe { transmute(&mut self.0) }
impl From<control::RawResourceHandle> for Handle {
fn from(handle: control::RawResourceHandle) -> Self {
Handle(handle)
}
}

impl control::ResourceType for Handle {
impl control::ResourceHandle for Handle {
const FFI_TYPE: u32 = ffi::DRM_MODE_OBJECT_ENCODER;
}

impl std::fmt::Debug for Handle {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_tuple("encoder::Handle")
.field(&self.0)
.finish()
}
}

/// Information about an encoder
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Info {
Expand Down
35 changes: 20 additions & 15 deletions src/control/framebuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,39 @@ use drm_ffi as ffi;

/// A handle to an framebuffer
#[repr(transparent)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Handle(control::ResourceHandle);
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub struct Handle(control::RawResourceHandle);

impl Into<control::ResourceHandle> for Handle {
fn into(self) -> control::ResourceHandle {
use std::mem::transmute;
unsafe { transmute(self) }
impl Into<control::RawResourceHandle> for Handle {
fn into(self) -> control::RawResourceHandle {
self.0
}
}

impl AsRef<control::ResourceHandle> for Handle {
fn as_ref(&self) -> &control::ResourceHandle {
use std::mem::transmute;
unsafe { transmute(&self.0) }
impl Into<u32> for Handle {
fn into(self) -> u32 {
self.0.into()
}
}

impl AsMut<control::ResourceHandle> for Handle {
fn as_mut(&mut self) -> &mut control::ResourceHandle {
use std::mem::transmute;
unsafe { transmute(&mut self.0) }
impl From<control::RawResourceHandle> for Handle {
fn from(handle: control::RawResourceHandle) -> Self {
Handle(handle)
}
}

impl control::ResourceType for Handle {
impl control::ResourceHandle for Handle {
const FFI_TYPE: u32 = ffi::DRM_MODE_OBJECT_FB;
}

impl std::fmt::Debug for Handle {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_tuple("framebuffer::Handle")
.field(&self.0)
.finish()
}
}

/// Information about a framebuffer
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Info {
Expand Down
46 changes: 28 additions & 18 deletions src/control/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,23 @@ pub mod property;
use std::mem;

use core::num::NonZeroU32;
type ResourceHandle = NonZeroU32;
type RawResourceHandle = NonZeroU32;

#[doc(hidden)]
pub trait ResourceType : AsRef<ResourceHandle> + AsMut<ResourceHandle> + Into<ResourceHandle> + Copy {
pub trait ResourceHandle : From<RawResourceHandle> + Into<RawResourceHandle> + Into<u32> + Copy + Sized {
const FFI_TYPE: u32;
}

fn from_u32<T: ResourceHandle>(raw: u32) -> Option<T> {
match raw {
0 => None,
n => {
let raw = unsafe { mem::transmute(n) };
Some(T::from(raw))
}
}
}

/// This trait should be implemented by any object that acts as a DRM device and
/// provides modesetting functionality.
///
Expand Down Expand Up @@ -129,7 +139,7 @@ pub trait Device: super::Device {

let ffi_info = ffi::mode::get_connector(
self.as_raw_fd(),
unsafe { mem::transmute(*handle.as_ref()) },
handle.into(),
None,
None,
None,
Expand All @@ -156,13 +166,13 @@ pub trait Device: super::Device {
fn get_encoder(&self, handle: encoder::Handle) -> Result<encoder::Info, SystemError> {
let info = ffi::mode::get_encoder(
self.as_raw_fd(),
unsafe { mem::transmute(*handle.as_ref()) },
handle.into(),
)?;

let enc = encoder::Info {
handle: handle,
enc_type: encoder::Kind::from(info.encoder_type),
crtc: unsafe { mem::transmute(info.crtc_id) },
crtc: from_u32(info.crtc_id),
pos_crtcs: info.possible_crtcs,
pos_clones: info.possible_clones,
};
Expand All @@ -174,7 +184,7 @@ pub trait Device: super::Device {
fn get_crtc(&self, handle: crtc::Handle) -> Result<crtc::Info, SystemError> {
let info = ffi::mode::get_crtc(
self.as_raw_fd(),
unsafe { mem::transmute(*handle.as_ref()) },
handle.into(),
)?;

let crtc = crtc::Info {
Expand All @@ -184,7 +194,7 @@ pub trait Device: super::Device {
0 => None,
_ => Some(Mode::from(info.mode)),
},
fb: unsafe { mem::transmute(info.fb_id) },
fb: from_u32(info.fb_id),
gamma_length: info.gamma_size,
};

Expand All @@ -198,7 +208,7 @@ pub trait Device: super::Device {
) -> Result<framebuffer::Info, SystemError> {
let info = ffi::mode::get_framebuffer(
self.as_raw_fd(),
unsafe { mem::transmute(*handle.as_ref()) },
handle.into(),
)?;

let fb = framebuffer::Info {
Expand All @@ -220,16 +230,16 @@ pub trait Device: super::Device {

let info = ffi::mode::get_plane(
self.as_raw_fd(),
unsafe { mem::transmute(*handle.as_ref()) },
handle.into(),
Some(&mut fmt_slice)
)?;

let fmt_len = fmt_slice.len();

let plane = plane::Info {
handle: handle,
crtc: unsafe { mem::transmute(info.crtc_id) },
fb: unsafe { mem::transmute(info.fb_id) },
crtc: from_u32(info.crtc_id),
fb: from_u32(info.fb_id),
pos_crtcs: info.possible_crtcs,
formats: formats,
fmt_len: fmt_len
Expand All @@ -248,7 +258,7 @@ pub trait Device: super::Device {

let info = ffi::mode::get_property(
self.as_raw_fd(),
unsafe { mem::transmute(*handle.as_ref()) },
handle.into(),
Some(&mut val_slice),
Some(&mut enum_slice)
)?;
Expand Down Expand Up @@ -314,7 +324,7 @@ pub trait Device: super::Device {
}

/// Sets a property for a specific resource.
fn set_property<T: ResourceType>(
fn set_property<T: ResourceHandle>(
&self,
handle: T,
prop: property::Handle,
Expand All @@ -323,8 +333,8 @@ pub trait Device: super::Device {

ffi::mode::set_property(
self.as_raw_fd(),
unsafe { mem::transmute(*prop.as_ref()) },
unsafe { mem::transmute(*handle.as_ref()) },
prop.into(),
handle.into(),
T::FFI_TYPE,
value
)?;
Expand All @@ -339,7 +349,7 @@ pub trait Device: super::Device {

let _ffi_info = ffi::mode::get_connector(
self.as_raw_fd(),
unsafe { mem::transmute(*handle.as_ref()) },
handle.into(),
None,
None,
Some(&mut mode_slice),
Expand All @@ -357,7 +367,7 @@ pub trait Device: super::Device {
}

/// Gets a list of property handles and values for this resource.
fn get_properties<T: ResourceType>(&self, handle: T) -> Result<PropertyValueSet, SystemError> {
fn get_properties<T: ResourceHandle>(&self, handle: T) -> Result<PropertyValueSet, SystemError> {
let mut prop_ids = [0u32; 32];
let mut prop_vals = [0u64; 32];

Expand All @@ -366,7 +376,7 @@ pub trait Device: super::Device {

ffi::mode::get_properties(
self.as_raw_fd(),
unsafe { mem::transmute(*handle.as_ref()) },
handle.into(),
T::FFI_TYPE,
Some(&mut prop_id_slice),
Some(&mut prop_val_slice),
Expand Down
Loading

0 comments on commit f9cb41c

Please sign in to comment.