feat: open Card for dev_t.
This commit is contained in:
+17
-2
@@ -17,7 +17,7 @@ pub fn open(io: std.Io, path: []const u8) OpenError!Card {
|
||||
if (std.fs.path.isAbsolute(path))
|
||||
return Card{ .handle = try std.Io.Dir.openFileAbsolute(io, path, .{ .mode = .read_write }) };
|
||||
|
||||
const dir = try std.Io.Dir.openDirAbsolute(io, "/dev/dri", .{});
|
||||
const dir = try std.Io.Dir.openDirAbsolute(io, drm.dir_name, .{});
|
||||
defer dir.close(io);
|
||||
|
||||
return Card{ .handle = try dir.openFile(io, path, .{ .mode = .read_write }) };
|
||||
@@ -26,7 +26,7 @@ pub fn open(io: std.Io, path: []const u8) OpenError!Card {
|
||||
pub const OpenAutoError = OpenError || std.Io.Dir.Iterator.Error || error{NoDevicesFound};
|
||||
|
||||
pub fn openAuto(io: std.Io, target_type: ?drm.Device.NodeType) OpenAutoError!Card {
|
||||
const dir = try std.Io.Dir.openDirAbsolute(io, "/dev/dri", .{ .iterate = true });
|
||||
const dir = try std.Io.Dir.openDirAbsolute(io, drm.dir_name, .{ .iterate = true });
|
||||
defer dir.close(io);
|
||||
|
||||
var it = dir.iterateAssumeFirstIteration();
|
||||
@@ -38,6 +38,21 @@ pub fn openAuto(io: std.Io, target_type: ?drm.Device.NodeType) OpenAutoError!Car
|
||||
return error.NoDevicesFound;
|
||||
}
|
||||
|
||||
pub fn openForDev(io: std.Io, dev: std.posix.dev_t) OpenAutoError!Card {
|
||||
const dir = try std.Io.Dir.openDirAbsolute(io, drm.dir_name, .{ .iterate = true });
|
||||
defer dir.close(io);
|
||||
|
||||
var it = dir.iterateAssumeFirstIteration();
|
||||
while (try it.next(io)) |entry| if (entry.kind == .character_device) {
|
||||
const posix_name = std.posix.toPosixPath(entry.name);
|
||||
const stat = util.stat(dir.handle, &posix_name, 0) catch continue;
|
||||
if (drm.makeDev(stat.rdev_major, stat.rdev_minor) == dev)
|
||||
return Card{ .handle = try dir.openFile(io, entry.name, .{ .mode = .read_write }) };
|
||||
};
|
||||
|
||||
return error.NoDevicesFound;
|
||||
}
|
||||
|
||||
pub fn close(self: Card, io: std.Io) void {
|
||||
self.handle.close(io);
|
||||
}
|
||||
|
||||
+3
-3
@@ -2,14 +2,14 @@ const std = @import("std");
|
||||
|
||||
pub fn statPath(path: []const u8) !std.os.linux.Statx {
|
||||
const posix_path = try std.posix.toPosixPath(path);
|
||||
return statxInner(std.os.linux.AT.FDCWD, &posix_path, 0);
|
||||
return stat(std.os.linux.AT.FDCWD, &posix_path, 0);
|
||||
}
|
||||
|
||||
pub fn statFile(fd: std.posix.fd_t) !std.os.linux.Statx {
|
||||
return statxInner(fd, "", std.os.linux.AT.EMPTY_PATH);
|
||||
return stat(fd, "", std.os.linux.AT.EMPTY_PATH);
|
||||
}
|
||||
|
||||
fn statxInner(fd: std.posix.fd_t, path: [*:0]const u8, flags: u32) !std.os.linux.Statx {
|
||||
pub fn stat(fd: std.posix.fd_t, path: [*:0]const u8, flags: u32) !std.os.linux.Statx {
|
||||
var stat_buf: std.os.linux.Statx = undefined;
|
||||
const rc = std.posix.system.statx(fd, path, flags, .{}, &stat_buf);
|
||||
return switch (std.posix.errno(rc)) {
|
||||
|
||||
Reference in New Issue
Block a user