Something went wrong. Try again.
A Zig library for driving Elgato Stream Deck devices over USB HID.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200// SPDX-FileCopyrightText: © 2025 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! How a `StreamDeck` reaches the device it drives.//!//! There are two ways: a HID device, which is every model that plugs//! into USB, and a TCP connection, which is the Studio and the Network//! Dock. Both carry the very same HID reports,//! so everything above this -- the key tables, the image paging, the//! event decoding -- is written once and does not know which it is//! talking to.//!//! The four verbs below are the whole of what the rest of the library//! asks of a device. That the list is this short is why a second//! transport was worth adding at all.//!//! ## Why a union and not a vtable//!//! Most of this library is decided by which model a device is rather//! than by what is behind it, and all of that can be tested with no//! device at all -- which the tests do, by building a `StreamDeck`//! whose transport is `.none`. A vtable would make that a pointer//! nobody set, and calling through it a jump into nowhere; a union tag//! is checked by the switch, and `.none` answers every call with//! `error.NoTransport`.
const std = @import("std");
const hidapi = @import("hidapi");
const Tcp = @import("Tcp.zig");
const Transport = @This();
/// No device at all.////// This is what the tests use for the parts of the library that never/// touch the hardware, and what a `StreamDeck` describing a model/// rather than driving one carries. Every operation on it fails.pub const none: Transport = .{ .value = .{ .none = {} } };
pub const Kind = enum { none, hid, tcp };
value: Value,
pub const Value = union(Kind) { none: void, /// An open HID device: every model that plugs into USB. Held by /// pointer because `hidapi.Device` is opened in place and every /// operation on it takes a mutable pointer, so it has to stay put. /// Owned by this transport and released by `close`. hid: *hidapi.Device, /// An open TCP connection: the Studio and the Network Dock. Owned /// by this transport and released by `close`. tcp: *Tcp,};
/// Build a transport over an open HID device, taking ownership of it.pub fn fromHid(device: *hidapi.Device) Transport { return .{ .value = .{ .hid = device } };}
/// Build a transport over an open TCP connection, taking ownership of/// it.pub fn fromTcp(connection: *Tcp) Transport { return .{ .value = .{ .tcp = connection } };}
/// Which kind of transport this is. `StreamDeck` asks, because a/// handful of operations genuinely differ between the two -- reading/// the serial number is one.pub fn kind(self: Transport) Kind { return std.meta.activeTag(self.value);}
/// The TCP connection behind this transport, or null if it is not one.pub fn tcp(self: Transport) ?*Tcp { return switch (self.value) { .tcp => |t| t, else => null, };}
/// The HID device behind this transport, or null if it is not one.////// This exists for the few things that are meaningful only over USB,/// which belong to a caller that knows it is driving a local device/// rather than to this library.pub fn hid(self: Transport) ?*hidapi.Device { return switch (self.value) { .hid => |d| d, else => null, };}
/// Write an output report: a page of image data, or a command that/// takes no answer. The first byte is the report id.pub fn write(self: Transport, io: std.Io, buf: []const u8) !usize { return switch (self.value) { .none => error.NoTransport, .hid => |d| d.write(io, buf), .tcp => |t| t.write(io, buf), };}
/// Wait for an input report and copy it into `buf`.////// Both transports block until the device says something. Over TCP/// this is also what answers the device's keepalives, so a caller that/// stops reading for several seconds will be disconnected; see the note/// at the top of `Tcp.zig`.pub fn read(self: Transport, io: std.Io, buf: []u8) ![]const u8 { return switch (self.value) { .none => error.NoTransport, .hid => |d| d.read(io, buf), .tcp => |t| t.read(io, buf), };}
/// Wait for an input report, giving up after `timeout`. Null means the/// time ran out with nothing to report, which is not a failure.////// Only the HID transport can do this today: `hidapi` grew a timed read/// and `std.Io.Threaded` has no read timeout to build one on for a/// socket. The TCP transport answers `error.Unsupported` rather than/// quietly waiting forever, which would be the worse answer.pub fn readTimeout( self: Transport, io: std.Io, buf: []u8, timeout: std.Io.Timeout,) !?[]const u8 { return switch (self.value) { .none => error.NoTransport, .hid => |d| d.readTimeout(io, buf, timeout), .tcp => error.Unsupported, };}
/// Send a feature report. The first byte is the report id.pub fn sendFeatureReport(self: Transport, io: std.Io, data: []const u8) !usize { return switch (self.value) { .none => error.NoTransport, .hid => |d| d.sendFeatureReport(io, data), .tcp => |t| t.sendFeatureReport(io, data), };}
/// Read a feature report. `buf[0]` names the report wanted on the way/// in, and the report is written over it on the way out.pub fn getFeatureReport(self: Transport, io: std.Io, buf: []u8) ![]const u8 { return switch (self.value) { .none => error.NoTransport, .hid => |d| d.getFeatureReport(io, buf), .tcp => |t| t.getFeatureReport(io, buf), };}
/// Release the device, freeing whatever was allocated to hold it./// Nothing on this transport may be used afterwards.////// `alloc` has to be the one the device was opened with. A TCP/// connection carries its own and ignores this, because it is reached/// through a pointer it allocated itself; a HID device does not, so/// this is where it is freed.pub fn close(self: Transport, io: std.Io, alloc: std.mem.Allocator) void { switch (self.value) { .none => {}, .hid => |d| { d.close(io); alloc.destroy(d); }, .tcp => |t| t.close(io), }}
test none { const io = std.testing.io;
// A transport with nothing behind it answers rather than crashing, // which is what makes it safe for the tests that describe a model // without driving one. var buf: [8]u8 = undefined; try std.testing.expectEqual(Kind.none, none.kind()); try std.testing.expectEqual(null, none.tcp()); try std.testing.expectEqual(null, none.hid()); try std.testing.expectError(error.NoTransport, none.write(io, &buf)); try std.testing.expectError(error.NoTransport, none.read(io, &buf)); try std.testing.expectError(error.NoTransport, none.sendFeatureReport(io, &buf)); try std.testing.expectError(error.NoTransport, none.getFeatureReport(io, &buf));
// And closing one does nothing rather than dereferencing something // that was never set. none.close(io, std.testing.allocator);}
test { std.testing.refAllDecls(@This());}