// SPDX-FileCopyrightText: © 2025 Jeffrey C. Ollie // SPDX-License-Identifier: MIT const std = @import("std"); const hidapi = @import("hidapi"); const streamdeck = @This(); /// A single device: the open handle, which model it is, and the control /// groups below. pub const StreamDeck = @import("StreamDeck.zig"); /// The grid of push buttons, and the images shown on them. pub const Keys = @import("Keys.zig"); /// The screens: a touchscreen on the Plus models, an info strip on the /// Neo. pub const Touchscreen = @import("Touchscreen.zig"); /// The rotary encoders, on the models that have them. pub const Encoders = @import("Encoders.zig"); /// The single screen behind the keys, which the key images are cut-outs /// of, and which can be painted whole. pub const Panel = @import("Panel.zig"); /// Decoding of the input reports a device sends when something on it is /// pressed, touched or turned. pub const Events = @import("Events.zig"); /// How a device is reached: a HID device, or a network connection. pub const Transport = @import("Transport.zig"); /// A Stream Deck reached over the network: the Studio and the Network /// Dock. pub const Tcp = @import("Tcp.zig"); /// The framing those devices speak, with the socket taken out of it. pub const framing = @import("framing.zig"); /// The udev rules a Linux system wants before an unprivileged process /// can open one of these devices, generated from the device table /// rather than written down beside it. pub const udev = @import("udev.zig"); pub fn init() !void {} pub fn deinit() void {} /// What can be wrong with a report a device sent. /// /// These are the reasons a report cannot be turned into an `Event`. /// They are errors rather than a null return with a log line beside it /// because a library has no business deciding whether a thing is worth /// printing: the caller knows whether it is running a daemon or a /// one-shot tool, and can tell these apart from each other, which a /// bare null cannot. /// /// None of them mean the device is broken. A Stream Deck can be /// unplugged mid-report, and a model whose tables this library has /// wrong will report control and event types it does not expect. pub const ReportError = error{ /// Fewer bytes than the report's own layout requires. ShortReport, /// The first byte of the report named a report type this library /// does not know. UnknownReportType, /// The report named a control -- keys, encoders, touchscreen -- /// that this library does not know. UnknownControlType, /// The control's own portion of the report named an event type /// this library does not know. UnknownEventType, }; pub const ImageFormat = enum { BMP, JPEG, }; pub const Direction = enum { LTR, RTL, }; /// Images for working out what a device is doing, rather than for /// driving one. See `diagnostic.keyIndexImage`. pub const diagnostic = @import("diagnostic.zig"); pub const ImageRotation = enum { @"90", @"180", @"270", }; pub const Command = enum(u8) { reset_key_stream = 0x02, }; /// One Stream Deck found by an `Enumerator`: the open device, and /// everything the system said about it without being asked. /// /// `info` is worth keeping. It carries the serial number, the product /// string and where the device sits on the bus, all of which the /// enumeration already knew -- asking the open device for them again /// answers less, because that asks the device rather than the system. pub const StreamDeckInfo = struct { deck: StreamDeck, info: hidapi.DeviceInfo, pub fn deinit(self: *const StreamDeckInfo, io: std.Io, alloc: std.mem.Allocator) void { self.deck.close(io, alloc); } }; /// An iterator over the positions of a key grid `rows` down by `cols` /// across, in the order the keys are numbered: left to right along the /// first row, then the next. Rows and columns are counted from zero, as /// keys are everywhere else in this library. pub fn KeyIterator(comptime rows: u8, comptime cols: u8) type { return struct { index: u16 = 0, pub const Position = struct { row: u8, col: u8 }; pub const init: @This() = .{}; pub fn next(self: *@This()) ?Position { if (self.index >= @as(u16, rows) * cols) return null; defer self.index += 1; return .{ .row = @intCast(self.index / cols), .col = @intCast(self.index % cols), }; } }; } test KeyIterator { var it: KeyIterator(3, 2) = .init; var seen: std.ArrayList(KeyIterator(3, 2).Position) = .empty; defer seen.deinit(std.testing.allocator); while (it.next()) |p| try seen.append(std.testing.allocator, p); try std.testing.expectEqualSlices(KeyIterator(3, 2).Position, &.{ .{ .row = 0, .col = 0 }, .{ .row = 0, .col = 1 }, .{ .row = 1, .col = 0 }, .{ .row = 1, .col = 1 }, .{ .row = 2, .col = 0 }, .{ .row = 2, .col = 1 }, }, seen.items); } /// Walks the Stream Decks attached to the system. /// /// Initialized in place rather than returned, because it holds the /// scratch the enumeration works in and a `DeviceInfo` that the /// underlying iterator hands back pointers into -- so it must not move /// once started: /// /// ``` /// var decks: streamdeck.Enumerator = undefined; /// try decks.init(io, alloc); /// defer decks.deinit(io); /// /// while (try decks.next(io, alloc)) |found| { /// defer found.deinit(io, alloc); /// // ... /// } /// ``` /// /// Enumerating needs no special permission. Opening does, and `next` /// opens each deck it finds, so a process without access to /// `/dev/hidraw*` gets `error.AccessDenied` from `next` rather than an /// empty list -- which is a better answer than the silence it used to /// give. See the README. pub const Enumerator = struct { devices: hidapi.Enumerator, scratch: []u8, /// Begin enumerating. `alloc` is used for the enumeration's own /// scratch and is released by `deinit`. pub fn init(self: *Enumerator, io: std.Io, alloc: std.mem.Allocator) !void { self.scratch = try alloc.alloc(u8, hidapi.Enumerator.recommended_scratch); errdefer alloc.free(self.scratch); // Usage pages cost a report descriptor read per device on // Linux and say nothing this library uses -- a Stream Deck is // identified by its vendor and product ids. The strings are // worth having, since `StreamDeckInfo` hands them on. try self.devices.init(io, self.scratch, .{ .usages = false, .strings = true }); } /// Release the enumeration. `alloc` has to be the one `init` was /// given. pub fn deinit(self: *Enumerator, io: std.Io, alloc: std.mem.Allocator) void { self.devices.deinit(io); alloc.free(self.scratch); } /// The next Stream Deck, already open, or null when there are no /// more. Devices that are not Stream Decks are skipped without /// being opened, as are the HID interfaces of a composite device /// that are not the panel. pub fn next(self: *Enumerator, io: std.Io, alloc: std.mem.Allocator) !?StreamDeckInfo { while (try self.devices.next(io)) |info| { // The model is decided from the ids the enumeration already // reported, so something that is not a Stream Deck is // passed over without being opened -- which matters, // because opening is the part that needs permission. _ = StreamDeck.Type.init(info.vendor_id, info.product_id) catch |err| switch (err) { error.NotAStreamDeck => continue, }; // A composite device is enumerated once per HID interface, // all of them under the same ids, and only one of those // interfaces is the panel. Every Elgato deck is a device of // its own with a single interface, so the ids were enough // until the Galleon: its `1b1c:2b18` function puts the // Stream Deck on interface 0 and an ordinary keyboard and a // consumer control on interfaces 1 and 2. Without this the // library reports one keyboard as three Stream Decks and // then talks the panel protocol at two of them. // // `interface_number` is null wherever the backend cannot // say, which is every transport but USB. A null is let // through rather than guessed at: a device that reports no // interface has only the one. if (info.interface_number) |n| if (n != 0) continue; const deck = try StreamDeck.open(io, alloc, info); return .{ .deck = deck, .info = info.* }; } return null; } }; /// Open a Stream Deck reached over the network -- a Studio, or a /// Network Dock -- at the given address. /// /// These devices are not enumerated: they are somewhere on a network /// rather than plugged into this machine, and `enumerate` will never /// find one. There is no discovery here either, so the address is the /// caller's to know; `Tcp.default_port` is the port they listen on. /// /// The caller owns the result and must release it with `close`. pub fn connect( io: std.Io, alloc: std.mem.Allocator, address: std.Io.net.IpAddress, ) !StreamDeck { return StreamDeck.connect(io, alloc, address); } test StreamDeckInfo { // Everything here needs a device, so this only keeps `deinit` // compiled. std.testing.refAllDecls(StreamDeckInfo); } test { std.testing.refAllDecls(@This()); std.testing.refAllDecls(Enumerator); std.testing.refAllDecls(KeyIterator(3, 2)); // Naming each file here is what makes its tests part of the // module's test binary; a file that nothing imports contributes no // tests however many it holds. _ = @import("bmp.zig"); _ = @import("udev.zig"); _ = @import("devices.zig"); _ = @import("diagnostic.zig"); _ = @import("Encoders.zig"); _ = @import("Events.zig"); _ = @import("framing.zig"); _ = @import("image.zig"); _ = @import("jpeg.zig"); _ = @import("Keys.zig"); _ = @import("Panel.zig"); _ = @import("StreamDeck.zig"); _ = @import("Tcp.zig"); _ = @import("Transport.zig"); _ = @import("Touchscreen.zig"); }