diff --git a/flake.lock b/flake.lock index 7a3dbbe..a112fd3 100644 --- a/flake.lock +++ b/flake.lock @@ -20,11 +20,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1742422364, - "narHash": "sha256-mNqIplmEohk5jRkqYqG19GA8MbQ/D4gQSK0Mu4LvfRQ=", + "lastModified": 1743315132, + "narHash": "sha256-6hl6L/tRnwubHcA4pfUUtk542wn2Om+D4UnDhlDW9BE=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "a84ebe20c6bc2ecbcfb000a50776219f48d134cc", + "rev": "52faf482a3889b7619003c0daec593a1912fddc1", "type": "github" }, "original": { diff --git a/src/Device.zig b/src/Device.zig index 3046893..be09517 100644 --- a/src/Device.zig +++ b/src/Device.zig @@ -1,47 +1,66 @@ const Device = @This(); const std = @import("std"); +const log = std.log.scoped(.hidapi); -const hidapi = @import("hidapi.zig"); +const ioctl = @import("ioctl.zig"); const DeviceInfo = @import("DeviceInfo.zig"); -device: *hidapi.c.hid_device, - -const Self = @This(); - -pub fn open(vendor_id: c_ushort, product_id: c_ushort, serial_number: ?[]const u8) !Device { - var buffer: [128]hidapi.c.wchar_t = undefined; - const device = hidapi.c.hid_open( - vendor_id, - product_id, - if (serial_number) |s| try hidapi.toWChar(s, &buffer) else null, - ); - if (device) |d| return .{ .device = d }; - return error.HIDApiError; -} - -pub fn openPath(path: [:0]const u8) !Device { - const device = hidapi.c.hid_open_path(path.ptr); - if (device) |d| return .{ .device = d }; - return error.HIDApiError; -} - -pub fn close(self: Self) void { - hidapi.c.hid_close(self.device); -} - -pub fn getVendorID(self: Self) !c_ushort { - const di = hidapi.c.hid_get_device_info(self.device); - return di.*.vendor_id; -} - -pub fn getProductID(self: Self) !c_ushort { - const di = hidapi.c.hid_get_device_info(self.device); - return di.*.product_id; +fd: std.os.linux.fd_t, + +pub fn open(path: []const u8) !Device { + return .{ + .fd = fd: { + const rc = std.os.linux.open( + path, + .{ + .ACCMODE = .RDWR, + .APPEND = true, + .NONBLOCK = true, + }, + 0, + ); + break :fd switch (std.os.linux.E.init(rc)) { + .SUCCESS => @as(std.os.linux.fd_t, @intCast(rc)), + else => |e| { + log.err("problem: {s} {s}", .{ path, @tagName(e) }); + return error.OpenError; + }, + }; + }, + }; +} + +pub fn close(self: Device) void { + _ = std.os.linux.close(self.fd); +} + +pub fn getVendorID(self: Device) !u16 { + var info = std.mem.zeroes(ioctl.hidraw_devinfo); + const rc = std.os.linux.ioctl(self.fd, ioctl.HIDIOCGRAWINFO, @intFromPtr(&info)); + switch (std.os.linux.E.init(rc)) { + .SUCCESS => { + return info.vendor; + }, + else => |e| { + log.err("problem: {s}", .{@tagName(e)}); + return error.HIDError; + }, + } } -pub fn getError(self: Self, alloc: std.mem.Allocator) !?[]const u8 { - return try hidapi.fromWCharAlloc(alloc, hidapi.c.hid_error(self.device)); +pub fn getProductID(self: Device) !c_ushort { + var info = std.mem.zeroes(ioctl.hidraw_devinfo); + const rc = std.os.linux.ioctl(self.fd, ioctl.HIDIOCGRAWINFO, @intFromPtr(&info)); + switch (std.os.linux.E.init(rc)) { + .SUCCESS => { + return info.vendor; + }, + else => |e| { + log.err("problem: {s}", .{@tagName(e)}); + return error.HIDError; + }, + } } /// Send a Feature report to the device. @@ -56,10 +75,18 @@ pub fn getError(self: Self, alloc: std.mem.Allocator) !?[]const u8 { /// ID (or 0x0, for devices which do not use numbered reports), followed by /// the report data (16 bytes). In this example, the length passed in would /// be 17. -pub fn sendFeatureReport(self: Self, data: []const u8) !usize { - const result = hidapi.c.hid_send_feature_report(self.device, data.ptr, data.len); - if (result < 0) return error.HIDApiError; - return @intCast(result); +pub fn sendFeatureReport(self: Device, data: []const u8) !usize { + const rc = std.os.linux.ioctl(self.fd, ioctl.HIDIOCSFEATURE(data.len), @intFromPtr(data.ptr)); + switch (std.os.linux.E.init(rc)) { + .SUCCESS => { + log.info("{} {any}", .{ rc, data[0..rc] }); + return rc; + }, + else => |e| { + log.err("problem: {s}", .{@tagName(e)}); + return error.HIDError; + }, + } } /// Get a feature report from a HID device. @@ -68,10 +95,18 @@ pub fn sendFeatureReport(self: Self, data: []const u8) !usize { /// Make sure to allow space for this extra byte in `data`. Upon return, the /// first byte will still contain the Report ID, and the report data will /// start in data[1]. -pub fn getFeatureReport(self: Self, buffer: []u8) ![]const u8 { - const result = hidapi.c.hid_get_feature_report(self.device, buffer.ptr, buffer.len); - if (result < 0) return error.HIDAPiError; - return buffer[0..@intCast(result)]; +pub fn getFeatureReport(self: Device, data: []u8) ![]const u8 { + const rc = std.os.linux.ioctl(self.fd, ioctl.HIDIOCGFEATURE(data.len), @intFromPtr(data.ptr)); + switch (std.os.linux.E.init(rc)) { + .SUCCESS => { + log.info("{} {any}", .{ rc, data[0..rc] }); + return rc; + }, + else => |e| { + log.err("problem: {s}", .{@tagName(e)}); + return error.HIDError; + }, + } } /// Get a input report from a HID device. @@ -80,10 +115,18 @@ pub fn getFeatureReport(self: Self, buffer: []u8) ![]const u8 { /// Make sure to allow space for this extra byte in `data`. Upon return, the /// first byte will still contain the Report ID, and the report data will /// start in `data[1]`. -pub fn getInputReport(self: @This(), buffer: []u8) ![]const u8 { - const result = hidapi.c.hid_get_input_report(self.device, buffer.ptr, buffer.len); - if (result < 1) return error.HIDApiError; - return buffer[0..@intCast(result)]; +pub fn getInputReport(self: Device, data: []u8) ![]const u8 { + const rc = std.os.linux.ioctl(self.fd, ioctl.HIDIOCGINPUT(data.len), @intFromPtr(data.ptr)); + switch (std.os.linux.E.init(rc)) { + .SUCCESS => { + log.info("{} {any}", .{ rc, data[0..rc] }); + return rc; + }, + else => |e| { + log.err("problem: {s}", .{@tagName(e)}); + return error.HIDError; + }, + } } /// Set the device handle to be non-blocking. @@ -93,10 +136,10 @@ pub fn getInputReport(self: @This(), buffer: []u8) ![]const u8 { /// wait (block) until there is data to read before returning. /// /// Nonblocking can be turned on and off at any time. -pub fn setNonblocking(self: @This(), nonblocking: bool) !void { - const result = hidapi.c.hid_set_nonblocking(self.device, if (nonblocking) 1 else 0); - if (result < 0) return error.HIDApiError; -} +// pub fn setNonblocking(self: @This(), nonblocking: bool) !void { +// const result = hidapi.c.hid_set_nonblocking(self.device, if (nonblocking) 1 else 0); +// if (result < 0) return error.HIDApiError; +// } /// Write an Output report to a HID device. /// @@ -112,22 +155,17 @@ pub fn setNonblocking(self: @This(), nonblocking: bool) !void { /// write() will send the data on the first OUT endpoint, if /// one exists. If it does not, it will send the data through /// the Control Endpoint (Endpoint 0). -pub fn write(self: @This(), data: []const u8) !usize { - const result = hidapi.c.hid_write(self.device, data.ptr, data.len); - if (result < 0) return error.HIDApiError; - return @intCast(result); -} - -/// Read an Input report from a HID device with timeout. -/// -/// Input reports are returned to the host through the INTERRUPT IN endpoint. -/// The first byte will contain the Report number if the device uses numbered -/// reports. -pub fn readTimeout(self: @This(), buffer: []u8, milliseconds: c_int) !?[]const u8 { - const result = hidapi.c.hid_read_timeout(self.device, buffer.ptr, buffer.len, milliseconds); - if (result < 0) return error.HIDApiError; - if (result == 0) return null; - return buffer[0..result]; +pub fn write(self: Device, data: []const u8) !usize { + const rc = std.os.linux.write(self.fd, data.ptr, data.len); + switch (std.os.linux.E.init(rc)) { + .SUCCESS => { + return rc; + }, + else => |e| { + log.err("problem: {s}", .{@tagName(e)}); + return error.HIDError; + }, + } } /// Read an Input report from a HID device. @@ -135,56 +173,62 @@ pub fn readTimeout(self: @This(), buffer: []u8, milliseconds: c_int) !?[]const u /// Input reports are returned to the host through the INTERRUPT IN endpoint. /// The first byte will contain the Report number if the device uses numbered /// reports. -pub fn read(self: @This(), buffer: []u8) !?[]const u8 { - const result = hidapi.c.hid_read(self.device, buffer.ptr, buffer.len); - if (result < 0) return error.HIDApiError; - if (result == 0) return null; - return buffer[0..@intCast(result)]; -} - -/// Get The Manufacturer String from a HID device. -pub fn getManufacturerString(self: @This(), alloc: std.mem.Allocator) ![]const u8 { - var buffer: [128]hidapi.c.wchar_t = undefined; - const result = hidapi.c.hid_get_manufacturer_string(self.device, &buffer, buffer.len); - if (result < 0) return error.HIDApiError; - return (try hidapi.fromWCharAlloc(alloc, &buffer)) orelse return error.HIDApiError; -} - -/// Get The Product String from a HID device. -pub fn getProductString(self: @This(), alloc: std.mem.Allocator) ![]const u8 { - var buffer: [128]hidapi.c.wchar_t = undefined; - const result = hidapi.c.hid_get_product_string(self.device, &buffer, buffer.len); - if (result < 0) return error.HIDApiError; - return (try hidapi.fromWCharAlloc(alloc, &buffer)) orelse return error.HIDApiError; -} - -/// Get The Serial Number String from a HID device. -pub fn getSerialNumberString(self: @This(), alloc: std.mem.Allocator) ![]const u8 { - var buffer: [128]hidapi.c.wchar_t = undefined; - const result = hidapi.c.hid_get_serial_number_string(self.device, &buffer, buffer.len); - if (result < 0) return error.HIDApiError; - return try hidapi.fromWCharAlloc(alloc, &buffer); -} - -/// Get a string from a HID device, based on its string index. -pub fn getIndexedString(self: @This(), alloc: std.mem.Allocator, string_index: c_int) ![]const u8 { - var buffer: [128]hidapi.c.wchar_t = undefined; - const result = hidapi.c.hid_get_indexed_string(self.device, string_index, &buffer, buffer.len); - if (result < 0) return error.HIDApiError; - return try hidapi.fromWCharAlloc(alloc, &buffer); -} - -/// Get a report descriptor from a HID device. -pub fn getReportDescriptor(self: @This(), alloc: std.mem.Allocator) ![]const u8 { - var buffer: [hidapi.MAX_REPORT_DESCRIPTOR_SIZE]u8 = undefined; - const result = hidapi.c.hid_get_report_descriptor(self.device, &buffer, buffer.len); - if (result < 0) return error.HIDApiError; - return try alloc.dupe(u8, buffer[0..@intCast(result)]); -} - -pub fn getDeviceInfo(self: @This(), alloc: std.mem.Allocator) !DeviceInfo { - if (hidapi.c.hid_get_device_info(self.device)) |hid_device_info| { - return DeviceInfo.init(alloc, hid_device_info); +pub fn read(self: Device, data: []u8) ![]const u8 { + const rc = std.os.linux.write(self.fd, data.ptr, data.len); + switch (std.os.linux.E.init(rc)) { + .SUCCESS => { + return data[0..rc]; + }, + else => |e| { + log.err("problem: {s}", .{@tagName(e)}); + return error.HIDError; + }, } - return error.HIDApiError; } + +// Get The Manufacturer String from a HID device. +// pub fn getManufacturerString(self: @This(), alloc: std.mem.Allocator) ![]const u8 { +// var buffer: [128]hidapi.c.wchar_t = undefined; +// const result = hidapi.c.hid_get_manufacturer_string(self.device, &buffer, buffer.len); +// if (result < 0) return error.HIDApiError; +// return (try hidapi.fromWCharAlloc(alloc, &buffer)) orelse return error.HIDApiError; +// } + +// Get The Product String from a HID device. +// pub fn getProductString(self: @This(), alloc: std.mem.Allocator) ![]const u8 { +// var buffer: [128]hidapi.c.wchar_t = undefined; +// const result = hidapi.c.hid_get_product_string(self.device, &buffer, buffer.len); +// if (result < 0) return error.HIDApiError; +// return (try hidapi.fromWCharAlloc(alloc, &buffer)) orelse return error.HIDApiError; +// } + +// Get The Serial Number String from a HID device. +// pub fn getSerialNumberString(self: @This(), alloc: std.mem.Allocator) ![]const u8 { +// var buffer: [128]hidapi.c.wchar_t = undefined; +// const result = hidapi.c.hid_get_serial_number_string(self.device, &buffer, buffer.len); +// if (result < 0) return error.HIDApiError; +// return try hidapi.fromWCharAlloc(alloc, &buffer); +// } + +// Get a string from a HID device, based on its string index. +// pub fn getIndexedString(self: @This(), alloc: std.mem.Allocator, string_index: c_int) ![]const u8 { +// var buffer: [128]hidapi.c.wchar_t = undefined; +// const result = hidapi.c.hid_get_indexed_string(self.device, string_index, &buffer, buffer.len); +// if (result < 0) return error.HIDApiError; +// return try hidapi.fromWCharAlloc(alloc, &buffer); +// } + +// Get a report descriptor from a HID device. +// pub fn getReportDescriptor(self: @This(), alloc: std.mem.Allocator) ![]const u8 { +// var buffer: [hidapi.MAX_REPORT_DESCRIPTOR_SIZE]u8 = undefined; +// const result = hidapi.c.hid_get_report_descriptor(self.device, &buffer, buffer.len); +// if (result < 0) return error.HIDApiError; +// return try alloc.dupe(u8, buffer[0..@intCast(result)]); +// } + +// pub fn getDeviceInfo(self: @This(), alloc: std.mem.Allocator) !DeviceInfo { +// if (hidapi.c.hid_get_device_info(self.device)) |hid_device_info| { +// return DeviceInfo.init(alloc, hid_device_info); +// } +// return error.HIDApiError; +// } diff --git a/src/DeviceInfo.zig b/src/DeviceInfo.zig index ee63eb2..18e45c9 100644 --- a/src/DeviceInfo.zig +++ b/src/DeviceInfo.zig @@ -1,34 +1,51 @@ const DeviceInfo = @This(); const std = @import("std"); +const log = std.log.scoped(.hidapi); -const hidapi = @import("hidapi.zig"); - -vendor_id: c_ushort, -product_id: c_ushort, -path: []u8, -serial_number: ?[]u8, -release_number: c_ushort, -manufacturer: ?[]u8, -product: ?[]u8, -usage_page: c_ushort, -usage: c_ushort, -interface_number: c_int, -bus_type: hidapi.HidBusType, - -pub fn init(alloc: std.mem.Allocator, hid_device_info: [*c]hidapi.c.hid_device_info) !DeviceInfo { - return .{ - .vendor_id = hid_device_info.*.vendor_id, - .product_id = hid_device_info.*.product_id, - .release_number = hid_device_info.*.release_number, - .path = try alloc.dupe(u8, std.mem.span(hid_device_info.*.path)), - .serial_number = try hidapi.fromWCharAlloc(alloc, hid_device_info.*.serial_number), - .manufacturer = try hidapi.fromWCharAlloc(alloc, hid_device_info.*.manufacturer_string), - .product = try hidapi.fromWCharAlloc(alloc, hid_device_info.*.product_string), - .usage_page = hid_device_info.*.usage_page, - .usage = hid_device_info.*.usage, - .interface_number = hid_device_info.*.interface_number, - .bus_type = @enumFromInt(hid_device_info.*.bus_type), +const ioctl = @import("ioctl.zig"); + +minor: std.os.linux.dev_t, +vendor_id: u16, +product_id: u16, +serial: []const u8, +buf: [32]u8 = undefined, + +pub fn init(minor: std.os.linux.dev_t) !?DeviceInfo { + const fd = fd: { + var buf: [std.fs.max_name_bytes]u8 = undefined; + const path = try std.fmt.bufPrint(&buf, "/dev/hidraw{d}", .{minor}); + + const rc = std.os.linux.open( + path, + .{ + .ACCMODE = .RDWR, + .APPEND = true, + .NONBLOCK = true, + }, + 0, + ); + break :fd switch (std.os.linux.E.init(rc)) { + .SUCCESS => break :fd @as(std.os.linux.fd_t, @intCast(rc)), + .EXIST => return null, + .ACCES => return null, + else => |e| { + log.err("problem: {s} {s}", .{ path, @tagName(e) }); + return error.OpenError; + }, + }; + }; + defer _ = std.os.linux.close(fd); + const vendor, const product = info: { + var info = std.mem.zeroes(ioctl.hidraw_devinfo); + const rc = std.os.linux.ioctl(fd, ioctl.HIDIOCGRAWINFO, @intFromPtr(&info)); + switch (std.os.linux.E.init(rc)) { + .SUCCESS => break :info .{ info.vendor, info.product }, + else => |e| { + log.err("problem: {s}", .{@tagName(e)}); + return error.HIDError; + }, + } }; } @@ -42,22 +59,22 @@ pub fn init(alloc: std.mem.Allocator, hid_device_info: [*c]hidapi.c.hid_device_i // return d; // } -pub fn format(self: *const DeviceInfo, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { - try writer.print("{x:0>4} {x:0>4} {s} '{?s}'\n", .{ self.vendor_id, self.product_id, self.path, self.serial_number }); - try writer.print("Manufacturer: {?s}\n", .{self.manufacturer}); - try writer.print("Product: {?s}\n", .{self.product}); - try writer.print("Release: 0x{x}\n", .{self.release_number}); - try writer.print("Interface: 0x{x}\n", .{self.interface_number}); - try writer.print("Usage (page): 0x{x} (0x{x})\n", .{ self.usage, self.usage_page }); - try writer.print("Bus type: {} ({})\n", .{ - self.bus_type, - @intFromEnum(self.bus_type), - }); -} +// pub fn format(self: *const DeviceInfo, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { +// try writer.print("{x:0>4} {x:0>4} {s} '{?s}'\n", .{ self.vendor_id, self.product_id, self.path, self.serial_number }); +// try writer.print("Manufacturer: {?s}\n", .{self.manufacturer}); +// try writer.print("Product: {?s}\n", .{self.product}); +// try writer.print("Release: 0x{x}\n", .{self.release_number}); +// try writer.print("Interface: 0x{x}\n", .{self.interface_number}); +// try writer.print("Usage (page): 0x{x} (0x{x})\n", .{ self.usage, self.usage_page }); +// try writer.print("Bus type: {} ({})\n", .{ +// self.bus_type, +// @intFromEnum(self.bus_type), +// }); +// } -pub fn deinit(self: *DeviceInfo, alloc: std.mem.Allocator) void { - alloc.free(self.path); - alloc.free(self.serial_number); - alloc.free(self.manufacturer); - alloc.free(self.product); -} +// pub fn deinit(self: *DeviceInfo, alloc: std.mem.Allocator) void { +// alloc.free(self.path); +// alloc.free(self.serial_number); +// alloc.free(self.manufacturer); +// alloc.free(self.product); +// } diff --git a/src/DeviceInfoIterator.zig b/src/DeviceInfoIterator.zig index 1a3b2e7..6b663dc 100644 --- a/src/DeviceInfoIterator.zig +++ b/src/DeviceInfoIterator.zig @@ -5,17 +5,16 @@ const std = @import("std"); const hidapi = @import("hidapi.zig"); const DeviceInfo = @import("DeviceInfo.zig"); -start: ?*hidapi.c.hid_device_info, -current: ?*hidapi.c.hid_device_info, +index: usize = 0, -pub fn next(self: *DeviceInfoIterator, alloc: std.mem.Allocator) !?DeviceInfo { - if (self.current) |current| { - self.current = current.next; - return try DeviceInfo.init(alloc, current); +pub fn next(self: *DeviceInfoIterator) !?DeviceInfo { + var buf: [std.fs.max_name_bytes]u8 = undefined; + if (self.index >= 64) return null; + while (self.index < 64) { + var buf: [std.fs.max_name_bytes]u8 = undefined; + try std.fmt.bufPrintZ(&buf, "/dev/hidraw{d}", .{self.index}); } return null; } -pub fn deinit(self: *DeviceInfoIterator) void { - if (self.start) |start| hidapi.c.hid_free_enumeration(start); -} +pub fn deinit(self: *DeviceInfoIterator) void {} diff --git a/src/hidapi.zig b/src/hidapi.zig index 23918bd..ce06b3a 100644 --- a/src/hidapi.zig +++ b/src/hidapi.zig @@ -1,124 +1,60 @@ const std = @import("std"); -pub const c = @cImport({ - @cInclude("hidapi/hidapi.h"); -}); - pub const Device = @import("Device.zig"); pub const DeviceInfo = @import("DeviceInfo.zig"); pub const DeviceInfoIterator = @import("DeviceInfoIterator.zig"); -pub const Errors = InitErrors || ExitErrors; - -pub const MAX_REPORT_DESCRIPTOR_SIZE = c.HID_API_MAX_REPORT_DESCRIPTOR_SIZE; +// pub const Errors = InitErrors || ExitErrors; -pub const HidBusType = enum(c.hid_bus_type) { - UNKNOWN = c.HID_API_BUS_UNKNOWN, - USB = c.HID_API_BUS_USB, - BLUETOOTH = c.HID_API_BUS_BLUETOOTH, - I2C = c.HID_API_BUS_I2C, - SPI = c.HID_API_BUS_SPI, - _, -}; +pub const HID_MAX_DESCRIPTOR_SIZE = 4096; -pub const InitErrors = error{ - HidApiInitError, -}; - -pub fn init() InitErrors!void { - const ret = c.hid_init(); - if (ret != 0) return error.HidApiInitError; -} - -pub const ExitErrors = error{ - HidApiExitError, -}; - -pub fn exit() ExitErrors!void { - const ret = c.hid_exit(); - if (ret != 0) return error.HidApiExitError; -} - -pub fn getError(alloc: std.mem.Allocator) ![]const u8 { - const err = try fromWCharAlloc(alloc, c.hid_error(null)); - if (err) |e| return e; - return ""; -} +// pub const InitErrors = error{ +// HidApiInitError, +// }; pub fn enumerate(vendor_id: c_ushort, product_id_: ?c_ushort) !DeviceInfoIterator { - const product_id = product_id_ orelse 0x0000; - const device_info: ?*c.hid_device_info = c.hid_enumerate(vendor_id, product_id); - - return .{ - .start = device_info, - .current = device_info, - }; -} - -pub fn version() std.SemanticVersion { - if (c.hid_version()) |v| { - return .{ - .major = @intCast(v.*.major), - .minor = @intCast(v.*.minor), - .patch = @intCast(v.*.patch), - }; - } - return .{ .major = 0, .minor = 0, .patch = 0 }; -} - -test "version-1" { - try std.testing.expectEqual( - std.SemanticVersion{ - .major = 0, - .minor = 14, - .patch = 0, - }, - version(), - ); -} - -pub fn version_str() []const u8 { - return std.mem.span(c.hid_version_str()); -} - -test "version-2" { - const v = version_str(); - try std.testing.expectEqualStrings("0.14.0", v); -} - -pub fn fromWCharAlloc(alloc: std.mem.Allocator, wide_string: [*c]const c.wchar_t) !?[]u8 { - if (wide_string == null) return null; - var output: std.ArrayListUnmanaged(u8) = .empty; - errdefer output.deinit(alloc); - var writer = output.writer(alloc); - var index: usize = 0; - while (wide_string[index] != 0) : (index += 1) { - var buf: [4]u8 = undefined; - const len = try std.unicode.utf8Encode(@intCast(wide_string[index]), &buf); - try writer.writeAll(buf[0..len]); - } - return try output.toOwnedSlice(alloc); -} - -pub fn toWCharAlloc(alloc: std.mem.Allocator, string: []const u8) ![*c]c.wchar_t { - var list: std.ArrayList(c.wchar_t) = .empty; - errdefer list.deinit(alloc); - var iter = (try std.unicode.Utf8View.init(string)).iterator(); - while (iter.nextCodepoint()) |codepoint| { - try list.append(alloc, @intCast(codepoint)); - } - return try list.toOwnedSliceSentinel(alloc, 0); -} - -pub fn toWChar(string: []const u8, buffer: []c.wchar_t) ![*c]c.wchar_t { - var iter = (try std.unicode.Utf8View.init(string)).iterator(); - var index: usize = 0; - while (iter.nextCodepoint()) |codepoint| : (index += 1) { - buffer[index] = @intCast(codepoint); - } - buffer[index] = 0; - return buffer.ptr; -} + // const product_id = product_id_ orelse 0x0000; + // const device_info: ?*c.hid_device_info = c.hid_enumerate(vendor_id, product_id); + + // return .{ + // .start = device_info, + // .current = device_info, + // }; +} + +// pub fn fromWCharAlloc(alloc: std.mem.Allocator, wide_string: [*c]const c.wchar_t) !?[]u8 { +// if (wide_string == null) return null; +// var output: std.ArrayListUnmanaged(u8) = .empty; +// errdefer output.deinit(alloc); +// var writer = output.writer(alloc); +// var index: usize = 0; +// while (wide_string[index] != 0) : (index += 1) { +// var buf: [4]u8 = undefined; +// const len = try std.unicode.utf8Encode(@intCast(wide_string[index]), &buf); +// try writer.writeAll(buf[0..len]); +// } +// return try output.toOwnedSlice(alloc); +// } + +// pub fn toWCharAlloc(alloc: std.mem.Allocator, string: []const u8) ![*c]c.wchar_t { +// var list: std.ArrayList(c.wchar_t) = .empty; +// errdefer list.deinit(alloc); +// var iter = (try std.unicode.Utf8View.init(string)).iterator(); +// while (iter.nextCodepoint()) |codepoint| { +// try list.append(alloc, @intCast(codepoint)); +// } +// return try list.toOwnedSliceSentinel(alloc, 0); +// } + +// pub fn toWChar(string: []const u8, buffer: []c.wchar_t) ![*c]c.wchar_t { +// var iter = (try std.unicode.Utf8View.init(string)).iterator(); +// var index: usize = 0; +// while (iter.nextCodepoint()) |codepoint| : (index += 1) { +// buffer[index] = @intCast(codepoint); +// } +// buffer[index] = 0; +// return buffer.ptr; +// } test { std.testing.refAllDecls(@This()); diff --git a/src/ioctl.zig b/src/ioctl.zig new file mode 100644 index 0000000..cef5596 --- /dev/null +++ b/src/ioctl.zig @@ -0,0 +1,149 @@ +const std = @import("std"); + +pub const BUS = enum(u32) { + PCI = 0x01, + ISAPNP = 0x02, + USB = 0x03, + HIL = 0x04, + BLUETOOTH = 0x05, + VIRTUAL = 0x06, + ISA = 0x10, + I8042 = 0x11, + XTKBD = 0x12, + RS232 = 0x13, + GAMEPORT = 0x14, + PARPORT = 0x15, + AMIGA = 0x16, + ADB = 0x17, + I2C = 0x18, + HOST = 0x19, + GSC = 0x1A, + ATARI = 0x1B, + SPI = 0x1C, + RMI = 0x1D, + CEC = 0x1E, + INTEL_ISHTP = 0x1F, + AMD_SFH = 0x20, + _, +}; + +pub const hidraw_devinfo = extern struct { + bustype: BUS, + vendor: u16, + product: u16, +}; + +comptime { + std.debug.assert(@sizeOf(hidraw_devinfo) == 8); +} + +pub const HID_MAX_DESCRIPTOR_SIZE = 4096; + +pub const hidraw_report_descriptor = extern struct { + size: u32, + value: [HID_MAX_DESCRIPTOR_SIZE]u8, + + pub fn init(size: c_int) hidraw_report_descriptor { + return .{ + .size = @intCast(size), + .value = std.mem.zeroes([HID_MAX_DESCRIPTOR_SIZE]u8), + }; + } +}; + +pub const HIDIOCGRDESCSIZE = std.os.linux.IOCTL.IOR('H', 0x01, c_int); +pub const HIDIOCGRDESC = std.os.linux.IOCTL.IOR('H', 0x02, hidraw_report_descriptor); +pub const HIDIOCGRAWINFO = std.os.linux.IOCTL.IOR('H', 0x03, hidraw_devinfo); + +const read = 2; +const write = 1; + +pub fn HIDIOCGRAWNAME(len: usize) u32 { + const request: std.os.linux.IOCTL.Request = .{ + .io_type = 'H', + .nr = 0x04, + .dir = read, + .size = @intCast(len), + }; + return @bitCast(request); +} + +pub fn HIDIOCGRAWPHYS(comptime len: usize) u32 { + const request: std.os.linux.IOCTL.Request = .{ + .io_type = 'H', + .nr = 0x05, + .dir = read, + .size = @intCast(len), + }; + return @bitCast(request); +} + +pub fn HIDIOCSFEATURE(len: usize) u32 { + const request: std.os.linux.IOCTL.Request = .{ + .io_type = 'H', + .nr = 0x06, + .dir = read | write, + .size = @intCast(len), + }; + return @bitCast(request); +} + +pub fn HIDIOCGFEATURE(len: usize) u32 { + const request: std.os.linux.IOCTL.Request = .{ + .io_type = 'H', + .nr = 0x07, + .dir = read | write, + .size = @intCast(len), + }; + return @bitCast(request); +} + +pub fn HIDIOCGRAWUNIQ(comptime len: usize) u32 { + const request: std.os.linux.IOCTL.Request = .{ + .io_type = 'H', + .nr = 0x08, + .dir = read, + .size = @intCast(len), + }; + return @bitCast(request); +} + +pub fn HIDIOCSINPUT(comptime len: usize) u32 { + const request: std.os.linux.IOCTL.Request = .{ + .io_type = 'H', + .nr = 0x09, + .dir = read | write, + .size = @intCast(len), + }; + return @bitCast(request); +} + +pub fn HIDIOCGINPUT(comptime len: usize) u32 { + const request: std.os.linux.IOCTL.Request = .{ + .io_type = 'H', + .nr = 0x0A, + .dir = read | write, + .size = @intCast(len), + }; + return @bitCast(request); +} + +pub fn HIDIOCSOUTPUT(comptime len: usize) u32 { + const request: std.os.linux.IOCTL.Request = .{ + .io_type = 'H', + .nr = 0x0B, + .dir = read | write, + .size = @intCast(len), + }; + return @bitCast(request); +} + +pub fn HIDIOCGOUTPUT(comptime len: usize) u32 { + const request: std.os.linux.IOCTL.Request = .{ + .io_type = 'H', + .nr = 0x0C, + .dir = read | write, + .size = @intCast(len), + }; + return @bitCast(request); +}