Skip to content

Commit ee9806c

Browse files
committed
openbsd: implement openbsd /dev/dri/* interface
1 parent 8678f19 commit ee9806c

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ rustix = { version = "0.38.22", features = ["mm", "fs"] }
1919
[target.'cfg(target_os = "freebsd")'.dependencies]
2020
libc = "0.2"
2121

22+
[target.'cfg(target_os = "openbsd")'.dependencies]
23+
libc = "1.0.0" # XXX Not yet
24+
2225
[dev-dependencies]
2326
image = { version = "0.24", default-features = false, features = ["png"] }
2427
rustix = { version = "0.38.22", features = ["event", "mm"] }

src/node/mod.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,34 @@ fn devname(dev: dev_t) -> Option<String> {
231231
Some(String::from_utf8(dev_name).expect("Returned device name is not valid utf8"))
232232
}
233233

234+
#[cfg(target_os = "openbsd")]
235+
fn devname(dev: dev_t) -> Option<String> {
236+
use std::ffi::CStr;
237+
use std::os::raw::{c_char, c_int};
238+
239+
let buf = unsafe {
240+
libc::devname(
241+
dev,
242+
libc::S_IFCHR, // Must be S_IFCHR or S_IFBLK
243+
)
244+
};
245+
246+
if buf.is_null() {
247+
return None;
248+
}
249+
250+
let dev_str =
251+
unsafe { CStr::from_ptr(buf).to_str() }.expect("Returned device name is not valid utf8");
252+
253+
// If no device matches the specified values, or no information is
254+
// available, a pointer to the string "??" is returned.
255+
if dev_str == "??" {
256+
return None;
257+
}
258+
259+
Some(dev_str.to_owned())
260+
}
261+
234262
/// Returns if the given device by major:minor pair is a DRM device.
235263
#[cfg(target_os = "linux")]
236264
pub fn is_device_drm(dev: dev_t) -> bool {
@@ -241,7 +269,7 @@ pub fn is_device_drm(dev: dev_t) -> bool {
241269
}
242270

243271
/// Returns if the given device by major:minor pair is a DRM device.
244-
#[cfg(target_os = "freebsd")]
272+
#[cfg(any(target_os = "freebsd", target_os = "openbsd"))]
245273
pub fn is_device_drm(dev: dev_t) -> bool {
246274
devname(dev).map_or(false, |dev_name| {
247275
dev_name.starts_with("drm/")
@@ -252,7 +280,7 @@ pub fn is_device_drm(dev: dev_t) -> bool {
252280
}
253281

254282
/// Returns if the given device by major:minor pair is a DRM device.
255-
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
283+
#[cfg(not(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd")))]
256284
pub fn is_device_drm(dev: dev_t) -> bool {
257285
major(dev) == DRM_MAJOR
258286
}
@@ -313,7 +341,7 @@ pub fn dev_path(dev: dev_t, ty: NodeType) -> io::Result<PathBuf> {
313341
}
314342

315343
/// Returns the path of a specific type of node from the DRM device described by major and minor device numbers.
316-
#[cfg(target_os = "freebsd")]
344+
#[cfg(any(target_os = "freebsd", target_os = "openbsd"))]
317345
pub fn dev_path(dev: dev_t, ty: NodeType) -> io::Result<PathBuf> {
318346
// Based on libdrm `drmGetMinorNameForFD`. Should be updated if the code
319347
// there is replaced with anything more sensible...
@@ -351,7 +379,7 @@ pub fn dev_path(dev: dev_t, ty: NodeType) -> io::Result<PathBuf> {
351379
}
352380

353381
/// Returns the path of a specific type of node from the DRM device described by major and minor device numbers.
354-
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
382+
#[cfg(not(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd")))]
355383
pub fn dev_path(dev: dev_t, ty: NodeType) -> io::Result<PathBuf> {
356384
use std::io::ErrorKind;
357385

0 commit comments

Comments
 (0)