Something went wrong. Try again.
A Zig library for driving Elgato Stream Deck devices over USB HID.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802// SPDX-FileCopyrightText: © 2025 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
const std = @import("std");
const z2d = @import("z2d");
const ImageFormat = @import("root.zig").ImageFormat;const ImageRotation = @import("root.zig").ImageRotation;const StreamDeck = @import("StreamDeck.zig");const ImageHeaderV3 = @import("image.zig").ImageHeaderV3;const ImageHeaderV4 = @import("image.zig").ImageHeaderV4;
const bmp = @import("bmp.zig");const image = @import("image.zig");const jpeg = @import("jpeg.zig");
const Touchscreen = @This();
pub const init: Touchscreen = .{};
pub const EventType = enum(u8) { short = 1, long = 2, drag = 3,};
pub const Event = struct { timestamp: std.Io.Timestamp, event: union(EventType) { short: struct { x: u16, y: u16, }, long: struct { x: u16, y: u16, }, drag: struct { in: struct { x: u16, y: u16, }, out: struct { x: u16, y: u16, }, }, },
/// Parse the touchscreen portion of an input report. Fails with /// `ReportError.ShortReport` below `report_size` bytes, which is /// what the positions need, and `ReportError.UnknownEventType` if /// the first byte is not one this library knows. pub fn init(timestamp: std.Io.Timestamp, data: []const u8) !Event { if (data.len < report_size) return error.ShortReport; const event_type = std.enums.fromInt(EventType, data[0]) orelse return error.UnknownEventType; return switch (event_type) { .short => .{ .timestamp = timestamp, .event = .{ .short = .{ .x = std.mem.readVarInt(u16, data[2..4], .little), .y = std.mem.readVarInt(u16, data[4..6], .little), }, }, }, .long => .{ .timestamp = timestamp, .event = .{ .long = .{ .x = std.mem.readVarInt(u16, data[2..4], .little), .y = std.mem.readVarInt(u16, data[4..6], .little), }, }, }, .drag => .{ .timestamp = timestamp, .event = .{ .drag = .{ .in = .{ .x = std.mem.readVarInt(u16, data[2..4], .little), .y = std.mem.readVarInt(u16, data[4..6], .little), }, .out = .{ .x = std.mem.readVarInt(u16, data[6..8], .little), .y = std.mem.readVarInt(u16, data[8..10], .little), }, }, }, }, }; }
pub fn deinit(self: *const Event, alloc: std.mem.Allocator) void { _ = self; _ = alloc; }};
/// The number of bytes a touchscreen report occupies in the body of an/// input report: an event type, a byte of padding, and then either one/// or two positions of two `u16`s each. `Event.init` will not parse/// anything shorter, so this is the size a model with a touchscreen has/// to claim in order for `Events.bodySize` to leave room for one.pub const report_size = 10;
/// The number of bytes that touchscreen state occupies in the body of/// an input report. `Events.bodySize` takes the largest of these across/// all of the control groups to size its read buffer, so a model that/// reports zero here can never have a touchscreen event parsed.pub fn readSize(self: *const Touchscreen) usize { const sd: *const StreamDeck = @alignCast(@fieldParentPtr("touchscreen", self)); return switch (sd.type) { // The Galleon's screen does not report touches. It was assumed // to, because it takes partial writes the way the Plus's does // and one implementation described it as a touchscreen -- but // a device held in Stream Deck mode through a run that caught // both encoders turning, both encoders pressed, and eight key // presses was touched and dragged across repeatedly and sent // nothing whatever. It is a display, as a Neo's strip is. .CorsairGalleonK100 => 0, .ElgatoStreamDeckMK2 => 0, .ElgatoStreamDeckMK2Scissor => 0, .ElgatoStreamDeckMini => 0, .ElgatoStreamDeckMiniDiscord => 0, .ElgatoStreamDeckMiniMK2 => 0, .ElgatoStreamDeckModule15 => 0, .ElgatoStreamDeckModule32 => 0, .ElgatoStreamDeckModule6 => 0, // The Neo has a screen but it is not a touchscreen. Its two // touch keys report through the key control, as keys 8 and 9 of // an ordinary key report -- confirmed against the hardware -- // so no touchscreen report ever arrives to make room for. .ElgatoStreamDeckNeo => 0, .ElgatoStreamDeckOriginal => 0, .ElgatoStreamDeckOriginalV2 => 0, .ElgatoStreamDeckPedal => 0, .ElgatoStreamDeckPlus => report_size, // A Plus XL declares **fourteen** bytes rather than ten. The // four past the second position are zero in every report seen // and ten is what the two positions need, so this stays at what // the parser reads rather than at what the device sends. It is // room this library does not use rather than something it is // missing -- but if a third position ever turns up, that is // where it will be. .ElgatoStreamDeckPlusXL => report_size, .ElgatoStreamDeckStudio => 0, .ElgatoStreamDeckXL => 0, .ElgatoStreamDeckXLV2 => 0, };}
/// The layout of the header that prefixes each page of image data sent/// to a screen. The Plus and the Plus XL take a `v3` header, which/// carries the rectangle being written; the Neo takes a `v4`, which/// does not, because its strip is only ever written whole.pub const TouchScreenImageHeaderFormat = enum { v3, v4,
/// The size of the header in bytes, which is also how much of each /// packet is unavailable for image data. pub fn headerSize(self: TouchScreenImageHeaderFormat) usize { return switch (self) { .v3 => @sizeOf(ImageHeaderV3), .v4 => @sizeOf(ImageHeaderV4), }; }};
/// Everything needed to put an image on a device's screen. `touch`/// separates the two kinds: the Plus and the Plus XL have a screen that/// reports where it was touched, and the Neo has one that only displays.pub const TouchScreenFeature = struct { header_format: TouchScreenImageHeaderFormat, image_format: ImageFormat, width: u16, height: u16, rotation: ?ImageRotation, flip_left_right: bool, flip_top_bottom: bool, /// Whether the screen reports touch events. When false it is a /// display and nothing more, and no `Event` will ever arrive from /// it. touch: bool,};
pub fn getFeature(self: *const Touchscreen) ?TouchScreenFeature { const sd: *const StreamDeck = @alignCast(@fieldParentPtr("touchscreen", self));
return switch (sd.type) { // 720x384, filling the top of a portrait panel above the keys. // The size has two sources that agree and is corroborated by // the device: a whole-panel image reaches this strip as the top // of one 720x1280 sheet, with the image's top-left corner // landing at the strip's. // // `touch` is false because it was measured false. Both sources // call this a touchscreen and this entry used to as well; // dragging across it produces no report at all on a device // being held in Stream Deck mode, so it displays and nothing // more. // // **The strip takes a direct write, which it used not to be // known to.** greendeck draws a four-quarter dashboard here // with `setImage` at 0,0 and 720x384 -- a `v3` write of its own // and not part of a whole-panel one -- and it arrives where it // was put: the sheet is filled, the quarters land square, and // nothing is clipped at any edge. Photographed on firmware // 3.05.005. So the size and the header format are measured now // rather than claimed. // // What is *still* only a claim is a **partial** write. The one // rectangle ever sent covers the whole strip, so whether a // smaller one lands where its header says -- which is the // interesting thing a `v3` header buys, and which a Neo's strip // does not do -- remains untested. // // The strip is only the host's while the device is held in // Stream Deck mode; see `StreamDeck.keepAlive`. A program that // writes it once and then waits watches the keyboard take it // back. .CorsairGalleonK100 => .{ .header_format = .v3, .image_format = .JPEG, .width = 720, .height = 384, .rotation = null, .flip_left_right = false, .flip_top_bottom = false, .touch = false, }, .ElgatoStreamDeckPlus => .{ .header_format = .v3, .image_format = .JPEG, .width = 800, .height = 100, .rotation = null, .flip_left_right = false, .flip_top_bottom = false, .touch = true, }, // The turn this entry used to lack. Elgato's documentation // gives this model a quarter turn anticlockwise for the keys, // the panel *and* the touchscreen; `Keys` and `Panel` carried // it and this did not, which was noticed only when the device // was asked to show something and showed nothing at all. Both // halves were wrong together: without the turn the pixels went // out the wrong way round *and* the rectangle in the header // claimed 1200 columns of a screen the device thinks is 100 // wide. See `deviceRect`. // // The dimensions stay as the screen is looked at, 1200 by 100, // because that is what a caller draws and what a touch reports // -- the device's own 100 by 1200 is what `deviceRect` and // `encodeImage` turn it into on the way out. // // Confirmed: with both halves in place the screen draws a // figure across its full width, the right way up, where before // it drew nothing at all. .ElgatoStreamDeckPlusXL => .{ .header_format = .v3, .image_format = .JPEG, .width = 1200, .height = 100, .rotation = .@"270", .flip_left_right = false, .flip_top_bottom = false, .touch = true, }, // The Neo's info strip, which sits between its two touch keys. // It displays and nothing else: the touch keys report through // the key control, as keys 8 and 9. .ElgatoStreamDeckNeo => .{ .header_format = .v4, .image_format = .JPEG, .width = 248, .height = 58, .rotation = null, .flip_left_right = true, .flip_top_bottom = true, .touch = false, }, else => null, };}
/// A black surface the size of this device's screen, ready to be drawn/// on and handed to `setImage`. Null if the device has no screen. The/// caller owns the result.pub fn blankImage(self: *const Touchscreen, alloc: std.mem.Allocator) !?z2d.Surface { const feature = self.getFeature() orelse return null; return try image.blankImage(alloc, feature.width, feature.height);}
/// Encode a surface the way a screen wants it: rotated and flipped as/// that screen is mounted, then as BMP or JPEG bytes. The half of/// `setImage` that does not touch the device, split out for the same/// reason as `Keys.encodeImage`. The caller owns the result.pub fn encodeImage( alloc: std.mem.Allocator, surface: z2d.Surface, feature: TouchScreenFeature,) ![]const u8 { var turned = try image.transform(alloc, surface, .{ .rotation = feature.rotation, .flip_left_right = feature.flip_left_right, .flip_top_bottom = feature.flip_top_bottom, }); defer turned.deinit(alloc);
const image_data = switch (feature.image_format) { .BMP => try bmp.encode(alloc, turned), .JPEG => try jpeg.encode(alloc, turned, .{}), }; errdefer alloc.free(image_data);
switch (feature.image_format) { .BMP => if (!std.mem.eql(u8, image_data[0..2], &.{ 0x42, 0x4d })) return error.ImageFormatError, .JPEG => if (!std.mem.eql(u8, image_data[0..2], &.{ 0xff, 0xd8 })) return error.ImageFormatError, }
return image_data;}
/// Display `surface` on the device's screen, with its top left corner/// at `x`, `y`. Does nothing on a device with no screen.////// A screen whose header carries no rectangle -- the Neo's info strip/// -- can only be written whole, so `x` and `y` must be zero and the/// surface must be the size the screen is. Anything else is/// `error.PartialWriteUnsupported` rather than a wrong-looking strip.pub fn setImage( self: *const Touchscreen, io: std.Io, alloc: std.mem.Allocator, x: u16, y: u16, surface: z2d.Surface,) !void { const feature = self.getFeature() orelse return;
const width: u16 = @intCast(surface.getWidth()); const height: u16 = @intCast(surface.getHeight());
switch (feature.header_format) { .v3 => {}, .v4 => if (x != 0 or y != 0 or width != feature.width or height != feature.height) { return error.PartialWriteUnsupported; }, }
const image_data = try encodeImage(alloc, surface, feature); defer alloc.free(image_data);
// The pixels are turned and flipped on the way out, so the // rectangle has to be too: the header describes where the image // lands in the device's own coordinates, not the caller's. const rect = deviceRect(feature, x, y, width, height);
try self.writeImage(io, rect.x, rect.y, rect.width, rect.height, image_data);}
/// Where a rectangle given in the coordinates a screen is *looked at*/// lands in the coordinates the device addresses it by.////// These are the same on every model but one, and on that one nothing/// worked. A Stream Deck + XL's glass is mounted a quarter turn round:/// `encodeImage` duly turns 1200x100 of pixels into 1200 tall and 100/// wide, and the header went on claiming a 1200 wide rectangle on a/// screen the device thinks is 100 wide. That is out of bounds, and the/// device's answer to out of bounds is to accept the write and display/// nothing -- which is what it did, silently, for as long as anyone had/// been writing that screen.////// The mapping is the inverse of the one `image.transform` applies,/// derived from it rather than guessed at, so the two cannot drift/// apart without a test noticing.fn deviceRect(feature: TouchScreenFeature, x: u16, y: u16, w: u16, h: u16) struct { x: u16, y: u16, width: u16, height: u16,} { const src_w = feature.width; const src_h = feature.height;
var rx: u16 = x; var ry: u16 = y; var rw: u16 = w; var rh: u16 = h;
if (feature.rotation) |rotation| switch (rotation) { // `transform` sends src(sx, sy) to dst(H-1-sy, sx). .@"90" => { rx = src_h - y - h; ry = x; rw = h; rh = w; }, .@"180" => { rx = src_w - x - w; ry = src_h - y - h; }, // `transform` sends src(sx, sy) to dst(sy, W-1-sx). .@"270" => { rx = y; ry = src_w - x - w; rw = h; rh = w; }, };
// The flips come after the rotation, so they act on the turned // rectangle and on the turned screen. const quarter_turn = if (feature.rotation) |r| r != .@"180" else false; const dst_w = if (quarter_turn) src_h else src_w; const dst_h = if (quarter_turn) src_w else src_h; if (feature.flip_left_right) rx = dst_w - rx - rw; if (feature.flip_top_bottom) ry = dst_h - ry - rh;
return .{ .x = rx, .y = ry, .width = rw, .height = rh };}
/// Page encoded image data out to the device. `image_data` must be in/// whichever format `getFeature` reports; it is split into pages small/// enough to fit in a packet alongside their header and written one at/// a time.fn writeImage(self: *const Touchscreen, io: std.Io, x: u16, y: u16, width: u16, height: u16, data: []const u8) !void { const sd: *const StreamDeck = @alignCast(@fieldParentPtr("touchscreen", self));
const feature = self.getFeature() orelse return;
const max = StreamDeck.MAX_PACKET_SIZE - feature.header_format.headerSize(); var iter = std.mem.window(u8, data, max, max); var page_number: u16 = 0; while (iter.next()) |page| : (page_number += 1) { var packet = std.mem.zeroes([StreamDeck.MAX_PACKET_SIZE]u8); var writer = std.Io.Writer.fixed(&packet); const last = page.len < max;
switch (feature.header_format) { .v3 => try writer.writeStruct(ImageHeaderV3{ .x = x, .y = y, .width = width, .height = height, .last = last, .page_number = page_number, .length = @intCast(page.len), }, .little),
// The Neo's strip takes no rectangle; `setImage` is what // refuses a partial write, since this is reached only // through it. .v4 => { try writer.writeStruct(ImageHeaderV4{ .last = last, .length = @intCast(page.len), .page_number = page_number, }, .little); }, }
try writer.writeAll(page); _ = try sd.device.write(io, &packet); }}
/// A `StreamDeck` of the given model with nothing behind it; see the/// note on `Keys.testDeck`.fn testDeck(t: StreamDeck.Type) StreamDeck { return .{ .type = t, .device = .none };}
test getFeature { // Only the two Plus models have a touchscreen, and they differ in // how wide it is. var plus = testDeck(.ElgatoStreamDeckPlus); try std.testing.expectEqual(800, plus.touchscreen.getFeature().?.width); try std.testing.expectEqual(100, plus.touchscreen.getFeature().?.height); try std.testing.expectEqual(ImageFormat.JPEG, plus.touchscreen.getFeature().?.image_format);
var plus_xl = testDeck(.ElgatoStreamDeckPlusXL); try std.testing.expectEqual(1200, plus_xl.touchscreen.getFeature().?.width);
var mk2 = testDeck(.ElgatoStreamDeckMK2); try std.testing.expectEqual(null, mk2.touchscreen.getFeature());}
test readSize { // A model that reports a touchscreen has to claim room for a // report of one, or `Events.bodySize` sizes the read buffer too // small and every touchscreen event is truncated and then refused // by `Event.init` as too short. That is what used to happen to the // Plus, which is the model that actually has a touchscreen. inline for (comptime std.enums.values(StreamDeck.Type)) |t| { var sd = testDeck(t); const feature = sd.touchscreen.getFeature();
if (feature != null and feature.?.touch) { // A screen that reports touches needs room for the report. try std.testing.expect(sd.touchscreen.readSize() >= report_size); } else { // And one that does not, or no screen at all, needs none. try std.testing.expectEqual(0, sd.touchscreen.readSize()); } }
var plus = testDeck(.ElgatoStreamDeckPlus); try std.testing.expectEqual(report_size, plus.touchscreen.readSize());
// The Neo has a screen and no touch on it, so it claims nothing // here even though `getFeature` describes the strip. var neo = testDeck(.ElgatoStreamDeckNeo); try std.testing.expect(neo.touchscreen.getFeature() != null); try std.testing.expectEqual(false, neo.touchscreen.getFeature().?.touch); try std.testing.expectEqual(0, neo.touchscreen.readSize());
var mk2 = testDeck(.ElgatoStreamDeckMK2); try std.testing.expectEqual(null, mk2.touchscreen.getFeature()); try std.testing.expectEqual(0, mk2.touchscreen.readSize());}
test deviceRect { // A screen that is not turned addresses a rectangle exactly as the // caller gave it, which is every model here but one. var plus = testDeck(.ElgatoStreamDeckPlus); const flat = plus.touchscreen.getFeature().?; const same = deviceRect(flat, 10, 20, 30, 40); try std.testing.expectEqual(10, same.x); try std.testing.expectEqual(20, same.y); try std.testing.expectEqual(30, same.width); try std.testing.expectEqual(40, same.height);
// The Plus XL's whole screen: 1200 wide and 100 tall to look at, // 100 wide and 1200 tall to address. Writing it as the former is // what made this screen show nothing at all. var xl = testDeck(.ElgatoStreamDeckPlusXL); const turned = xl.touchscreen.getFeature().?; const whole = deviceRect(turned, 0, 0, turned.width, turned.height); try std.testing.expectEqual(0, whole.x); try std.testing.expectEqual(0, whole.y); try std.testing.expectEqual(100, whole.width); try std.testing.expectEqual(1200, whole.height);
// And a rectangle that is not the whole screen, where getting the // turn backwards would still put the width and height right. const part = deviceRect(turned, 100, 20, 50, 30); try std.testing.expectEqual(20, part.x); try std.testing.expectEqual(1050, part.y); try std.testing.expectEqual(30, part.width); try std.testing.expectEqual(50, part.height);}
test "deviceRect agrees with where transform actually puts the pixels" { // The rectangle and the pixels are turned by two different pieces // of code, and a screen written with them disagreeing is a screen // that shows the right image in the wrong place -- or, as this // model demonstrated, nothing whatever. So rather than restate the // mapping, this puts one pixel through `image.transform` and asks // `deviceRect` where that same pixel went. const alloc = std.testing.allocator;
inline for (.{ .ElgatoStreamDeckPlus, .ElgatoStreamDeckPlusXL, }) |model| { var sd = testDeck(model); const feature = sd.touchscreen.getFeature().?;
for ([_][2]u16{ .{ 0, 0 }, .{ 7, 3 }, .{ feature.width - 1, feature.height - 1 } }) |p| { var surface: z2d.Surface = try .init( .image_surface_rgb, alloc, feature.width, feature.height, ); defer surface.deinit(alloc); surface.putPixel(p[0], p[1], .{ .rgb = .{ .r = 255, .g = 255, .b = 255 } });
var turned = try image.transform(alloc, surface, .{ .rotation = feature.rotation, .flip_left_right = feature.flip_left_right, .flip_top_bottom = feature.flip_top_bottom, }); defer turned.deinit(alloc);
const rect = deviceRect(feature, p[0], p[1], 1, 1); const moved = turned.getPixel(@intCast(rect.x), @intCast(rect.y)).?; try std.testing.expectEqual(255, moved.rgb.r); } }}
test "a screen's header carries a rectangle only if partial writes work" { // The `v3` header names the rectangle being written and the `v4` // does not, so which header a screen takes decides whether part of // it can be redrawn. inline for (comptime std.enums.values(StreamDeck.Type)) |t| { var sd = testDeck(t);
if (sd.touchscreen.getFeature()) |feature| { try std.testing.expect(feature.width > 0); try std.testing.expect(feature.height > 0);
// A page of image data has to fit in a packet with its // header. try std.testing.expect( feature.header_format.headerSize() < StreamDeck.MAX_PACKET_SIZE, ); } }
var plus = testDeck(.ElgatoStreamDeckPlus); try std.testing.expectEqual( TouchScreenImageHeaderFormat.v3, plus.touchscreen.getFeature().?.header_format, ); try std.testing.expectEqual(16, TouchScreenImageHeaderFormat.v3.headerSize());
var neo = testDeck(.ElgatoStreamDeckNeo); try std.testing.expectEqual( TouchScreenImageHeaderFormat.v4, neo.touchscreen.getFeature().?.header_format, ); try std.testing.expectEqual(8, TouchScreenImageHeaderFormat.v4.headerSize());}
test TouchScreenImageHeaderFormat { try std.testing.expectEqual( @sizeOf(ImageHeaderV3), TouchScreenImageHeaderFormat.v3.headerSize(), ); // The header has to fit in a packet with room left for image data. try std.testing.expect(TouchScreenImageHeaderFormat.v3.headerSize() < StreamDeck.MAX_PACKET_SIZE);}
test Event { // A short tap reports one position. { const event = try Event.init(.{ .nanoseconds = 0 }, &.{ 1, 0, 0x20, 0x01, 0x30, 0x00, 0, 0, 0, 0, }); try std.testing.expectEqual(0x0120, event.event.short.x); try std.testing.expectEqual(0x0030, event.event.short.y); }
// A long press reports one position under a different tag. { const event = try Event.init(.{ .nanoseconds = 0 }, &.{ 2, 0, 0x20, 0x01, 0x30, 0x00, 0, 0, 0, 0, }); try std.testing.expectEqual(0x0120, event.event.long.x); }
// A drag reports where it started and where it ended. { const event = try Event.init(.{ .nanoseconds = 0 }, &.{ 3, 0, 0x10, 0x00, 0x20, 0x00, 0x30, 0x00, 0x40, 0x00, }); try std.testing.expectEqual(0x0010, event.event.drag.in.x); try std.testing.expectEqual(0x0020, event.event.drag.in.y); try std.testing.expectEqual(0x0030, event.event.drag.out.x); try std.testing.expectEqual(0x0040, event.event.drag.out.y); }}
test "a short or unknown touchscreen report is refused" { const saved = std.testing.log_level; std.testing.log_level = .err; defer std.testing.log_level = saved;
// Nine bytes is one short of what the positions need. try std.testing.expectError( error.ShortReport, Event.init(.{ .nanoseconds = 0 }, &.{ 1, 0, 0, 0, 0, 0, 0, 0, 0 }), );
try std.testing.expectError( error.UnknownEventType, Event.init(.{ .nanoseconds = 0 }, &.{ 0x7f, 0, 0, 0, 0, 0, 0, 0, 0, 0 }), );}
test blankImage { const alloc = std.testing.allocator;
// A screen comes back the size `getFeature` reports. var neo = testDeck(.ElgatoStreamDeckNeo); var strip = (try neo.touchscreen.blankImage(alloc)).?; defer strip.deinit(alloc); try std.testing.expectEqual(@as(i32, 248), strip.getWidth()); try std.testing.expectEqual(@as(i32, 58), strip.getHeight());
var plus = testDeck(.ElgatoStreamDeckPlus); var screen = (try plus.touchscreen.blankImage(alloc)).?; defer screen.deinit(alloc); try std.testing.expectEqual(@as(i32, 800), screen.getWidth()); try std.testing.expectEqual(@as(i32, 100), screen.getHeight());
// And a device with no screen gets nothing rather than a blank. var mk2 = testDeck(.ElgatoStreamDeckMK2); try std.testing.expectEqual(null, try mk2.touchscreen.blankImage(alloc));}
test encodeImage { const alloc = std.testing.allocator;
// Every model with a screen encodes to the format it asked for, at // the size it asked for. inline for (comptime std.enums.values(StreamDeck.Type)) |t| { var sd = testDeck(t);
if (sd.touchscreen.getFeature()) |feature| { var surface = (try sd.touchscreen.blankImage(alloc)).?; defer surface.deinit(alloc);
const data = try encodeImage(alloc, surface, feature); defer alloc.free(data);
switch (feature.image_format) { .BMP => try std.testing.expectEqualSlices(u8, &.{ 0x42, 0x4d }, data[0..2]), .JPEG => try std.testing.expectEqualSlices(u8, &.{ 0xff, 0xd8 }, data[0..2]), }
// It has to be pageable: at least one page, and the header // leaves room for data in each. try std.testing.expect(data.len > 0); try std.testing.expect(feature.header_format.headerSize() < StreamDeck.MAX_PACKET_SIZE); } }}
test "a screen that takes no rectangle refuses a partial write" { const alloc = std.testing.allocator;
// The Neo's strip is written whole or not at all, and saying so is // better than sending a header the device will read as a full // update and then filling only part of it. var neo = testDeck(.ElgatoStreamDeckNeo); const feature = neo.touchscreen.getFeature().?; try std.testing.expectEqual(TouchScreenImageHeaderFormat.v4, feature.header_format);
// A surface smaller than the strip, at the origin. var small = try image.blankImage(alloc, 8, 8); defer small.deinit(alloc); try std.testing.expectError( error.PartialWriteUnsupported, neo.touchscreen.setImage(undefined, alloc, 0, 0, small), );
// And the whole strip, but offset. var whole = (try neo.touchscreen.blankImage(alloc)).?; defer whole.deinit(alloc); try std.testing.expectError( error.PartialWriteUnsupported, neo.touchscreen.setImage(undefined, alloc, 4, 0, whole), );
// The Plus takes a rectangle, so the same partial write is fine // there -- it gets as far as needing a device, which is where a // test has to stop. var plus = testDeck(.ElgatoStreamDeckPlus); try std.testing.expectEqual( TouchScreenImageHeaderFormat.v3, plus.touchscreen.getFeature().?.header_format, );}
test "encoding a screen image cleans up after a failed allocation" { inline for (.{ StreamDeck.Type.ElgatoStreamDeckNeo, StreamDeck.Type.ElgatoStreamDeckPlus, }) |t| { try std.testing.checkAllAllocationFailures(std.testing.allocator, struct { fn run(alloc: std.mem.Allocator) !void { var sd: StreamDeck = .{ .type = t, .device = .none }; const feature = sd.touchscreen.getFeature().?;
var surface = (try sd.touchscreen.blankImage(alloc)).?; defer surface.deinit(alloc);
const data = try encodeImage(alloc, surface, feature); defer alloc.free(data); } }.run, .{}); }}
test { // `setImage` needs a device to talk to, so it cannot be run here. // Referring to it still puts it in front of the compiler, which is // the point: Zig does not check a function nothing calls, and this // file is where that had left a `sd.product` field that does not // exist and a header with a field never set. std.testing.refAllDecls(@This()); std.testing.refAllDecls(Event);}