From 8ba67e392dcfd3fdd653e32ad41d4c9abee55f62 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Fri, 21 Aug 2026 09:24:14 -0500 Subject: [PATCH] updates --- build.zig.zon | 30 ++--- src/Encoders.zig | 30 +++-- src/Events.zig | 36 ++--- src/Keys.zig | 120 +++++++++-------- src/StreamDeck.zig | 320 ++++++++++++++++++++++++++++++-------------- src/Touchscreen.zig | 30 +++-- src/bmp.zig | 27 ++++ src/main.zig | 73 +++++----- src/root.zig | 41 +++--- 9 files changed, 429 insertions(+), 278 deletions(-) create mode 100644 src/bmp.zig diff --git a/build.zig.zon b/build.zig.zon index b923274..3ef3d69 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,28 +1,20 @@ .{ .name = .streamdeck, .version = "0.0.0", - .fingerprint = 0x9c21b30fa359f3ff, + .fingerprint = 0xa8a2b7b3c58bf380, .dependencies = .{ - // .z2d = .{ - // .url = "git+https://github.com/vancluever/z2d.git#47fc1030c1636dcfb75ea1cc238f706dd1feb379", - // .hash = "z2d-0.12.2-pre-j5P_HtA8EQDpzu5RUtQhr_Sn9MeQF3_q7rWMw3ouesd0", - // }, - // .zignal = .{ - // .url = "git+https://github.com/bfactory-ai/zignal.git#f08a959b99ec068d800d51270ca54679b2f4a543", - // .hash = "zignal-0.10.0-OrZ3syf3JwDM3rBtoTFDXQT4iUJeoPqVaAJDHAjtM7jN", - // }, - // .adwaita_fonts = .{ - // .url = "https://gitlab.gnome.org/GNOME/adwaita-fonts/-/archive/51.0/adwaita-fonts-51.0.tar.gz", - // .hash = "N-V-__8AAFd2dgC5WoX2exL4b3WwxkbJWSOR0B2ojwXArCiV", - // }, - // .zigimg = .{ - // .url = "git+https://github.com/zigimg/zigimg.git#096115a3eeae1a4c477496d575f1f1f74865387c", - // .hash = "zigimg-0.1.0-8_eo2mHBFwDLhWjXriR17_b7VF6qML1f98Xokp7ZZbop", - // }, + .z2d = .{ + .url = "git+https://github.com/vancluever/z2d.git#47fc1030c1636dcfb75ea1cc238f706dd1feb379", + .hash = "z2d-0.12.2-pre-j5P_HtA8EQDpzu5RUtQhr_Sn9MeQF3_q7rWMw3ouesd0", + }, + .zigimg = .{ + .url = "git+https://github.com/zigimg/zigimg.git#096115a3eeae1a4c477496d575f1f1f74865387c", + .hash = "zigimg-0.1.0-8_eo2mHBFwDLhWjXriR17_b7VF6qML1f98Xokp7ZZbop", + }, .hidapi = .{ - .url = "git+https://git.ocjtech.us/jeff/zig-hidapi.git#633d78143917927ccf65dfd2316530dd7d9ee65f", - .hash = "hidapi-0.0.2-PuroMyV0AADP06bqp8FWoYzRK34GNgYSJ7WxZTujE_6w", + .url = "git+https://git.ocjtech.us/jeff/zig-hidapi.git#0e71d0fc23a4dfd588bb7520405ec5de893be929", + .hash = "hidapi-0.0.2-PuroM-tzAACiUb-jDSG7kak_taRKvbR8W7U_wLP_7i7f", }, }, diff --git a/src/Encoders.zig b/src/Encoders.zig index c701dcc..01488fd 100644 --- a/src/Encoders.zig +++ b/src/Encoders.zig @@ -4,23 +4,25 @@ const Encoders = @This(); const log = std.log.scoped(.encoders); -const StreamDeck = @import("streamdeck.zig"); +const StreamDeck = @import("StreamDeck.zig"); + +pub const init: Encoders = .{}; pub fn count(self: *const Encoders) u8 { const sd: *const StreamDeck = @alignCast(@fieldParentPtr("encoders", self)); - return switch (sd.product) { - .StreamDeckMini => 0, - .StreamDeckMiniMK2 => 0, - .StreamDeckMK2 => 0, - .StreamDeckNeo => 0, - .StreamDeckOriginal => 0, - .StreamDeckOriginalV2 => 0, - .StreamDeckPedal => 0, - .StreamDeckPlus => 4, - .StreamDeckPlusXL => 6, - .StreamDeckStudio => 0, - .StreamDeckXL => 0, - .StreamDeckXLV2 => 0, + return switch (sd.type) { + .ElgatoStreamDeckMini => 0, + .ElgatoStreamDeckMiniMK2 => 0, + .ElgatoStreamDeckMK2 => 0, + .ElgatoStreamDeckNeo => 0, + .ElgatoStreamDeckOriginal => 0, + .ElgatoStreamDeckOriginalV2 => 0, + .ElgatoStreamDeckPedal => 0, + .ElgatoStreamDeckPlus => 4, + .ElgatoStreamDeckPlusXL => 6, + .ElgatoStreamDeckStudio => 0, + .ElgatoStreamDeckXL => 0, + .ElgatoStreamDeckXLV2 => 0, }; } diff --git a/src/Events.zig b/src/Events.zig index 035fdd2..e6b1935 100644 --- a/src/Events.zig +++ b/src/Events.zig @@ -3,10 +3,12 @@ const Events = @This(); const std = @import("std"); const log = std.log.scoped(.events); -const Keys = @import("keys.zig"); -const Touchscreen = @import("touchscreen.zig"); -const Encoders = @import("encoders.zig"); -const StreamDeck = @import("streamdeck.zig"); +const Keys = @import("Keys.zig"); +const Touchscreen = @import("Touchscreen.zig"); +const Encoders = @import("Encoders.zig"); +const StreamDeck = @import("StreamDeck.zig"); + +pub const init: Events = .{}; pub const ReportType = enum(u8) { input = 1, @@ -44,19 +46,19 @@ pub const HeaderFormat = enum { pub fn headerFormat(self: *const Events) HeaderFormat { const sd: *const StreamDeck = @alignCast(@fieldParentPtr("events", self)); - return switch (sd.product) { - .StreamDeckMK2 => .v2, - .StreamDeckMini => .v1, - .StreamDeckMiniMK2 => .v1, - .StreamDeckNeo => .v2, - .StreamDeckOriginal => .v1, - .StreamDeckOriginalV2 => .v2, - .StreamDeckPedal => .v1, - .StreamDeckPlus => .v2, - .StreamDeckPlusXL => .v2, - .StreamDeckStudio => .v2, - .StreamDeckXL => .v2, - .StreamDeckXLV2 => .v2, + return switch (sd.type) { + .ElgatoStreamDeckMK2 => .v2, + .ElgatoStreamDeckMini => .v1, + .ElgatoStreamDeckMiniMK2 => .v1, + .ElgatoStreamDeckNeo => .v2, + .ElgatoStreamDeckOriginal => .v1, + .ElgatoStreamDeckOriginalV2 => .v2, + .ElgatoStreamDeckPedal => .v1, + .ElgatoStreamDeckPlus => .v2, + .ElgatoStreamDeckPlusXL => .v2, + .ElgatoStreamDeckStudio => .v2, + .ElgatoStreamDeckXL => .v2, + .ElgatoStreamDeckXLV2 => .v2, }; } diff --git a/src/Keys.zig b/src/Keys.zig index b03154b..798dac6 100644 --- a/src/Keys.zig +++ b/src/Keys.zig @@ -11,12 +11,14 @@ const ImageHeaderV1 = @import("image.zig").ImageHeaderV1; const ImageHeaderV2 = @import("image.zig").ImageHeaderV2; // const jpeg = @import("jpeg.zig"); const Rotation = @import("root.zig").ImageRotation; -const StreamDeck = @import("streamdeck.zig"); -const svg = @import("svg.zig"); -const blankImage = @import("image.zig"); +const StreamDeck = @import("StreamDeck.zig"); +// const svg = @import("svg.zig"); +const blankImage = @import("image.zig").blankImage; const log = std.log.scoped(.keys); +pub const init: Keys = .{}; + pub const State = enum(u8) { up = 0, down = 1, @@ -68,55 +70,55 @@ pub const Event = struct { pub fn cols(self: *const Keys) u5 { const sd: *const StreamDeck = @alignCast(@fieldParentPtr("keys", self)); - return switch (sd.product) { - .StreamDeckMK2 => 5, - .StreamDeckMini => 3, - .StreamDeckMiniMK2 => 3, - .StreamDeckNeo => 4, - .StreamDeckOriginal => 5, - .StreamDeckOriginalV2 => 5, - .StreamDeckPedal => 3, - .StreamDeckPlus => 4, - .StreamDeckPlusXL => 9, - .StreamDeckStudio => 16, - .StreamDeckXL => 8, - .StreamDeckXLV2 => 8, + return switch (sd.type) { + .ElgatoStreamDeckMK2 => 5, + .ElgatoStreamDeckMini => 3, + .ElgatoStreamDeckMiniMK2 => 3, + .ElgatoStreamDeckNeo => 4, + .ElgatoStreamDeckOriginal => 5, + .ElgatoStreamDeckOriginalV2 => 5, + .ElgatoStreamDeckPedal => 3, + .ElgatoStreamDeckPlus => 4, + .ElgatoStreamDeckPlusXL => 9, + .ElgatoStreamDeckStudio => 16, + .ElgatoStreamDeckXL => 8, + .ElgatoStreamDeckXLV2 => 8, }; } pub fn rows(self: *const Keys) u5 { const sd: *const StreamDeck = @alignCast(@fieldParentPtr("keys", self)); - return switch (sd.product) { - .StreamDeckMK2 => 3, - .StreamDeckMini => 2, - .StreamDeckMiniMK2 => 2, - .StreamDeckNeo => 2, - .StreamDeckOriginal => 3, - .StreamDeckOriginalV2 => 3, - .StreamDeckPedal => 1, - .StreamDeckPlus => 2, - .StreamDeckPlusXL => 4, - .StreamDeckStudio => 2, - .StreamDeckXL => 4, - .StreamDeckXLV2 => 4, + return switch (sd.type) { + .ElgatoStreamDeckMK2 => 3, + .ElgatoStreamDeckMini => 2, + .ElgatoStreamDeckMiniMK2 => 2, + .ElgatoStreamDeckNeo => 2, + .ElgatoStreamDeckOriginal => 3, + .ElgatoStreamDeckOriginalV2 => 3, + .ElgatoStreamDeckPedal => 1, + .ElgatoStreamDeckPlus => 2, + .ElgatoStreamDeckPlusXL => 4, + .ElgatoStreamDeckStudio => 2, + .ElgatoStreamDeckXL => 4, + .ElgatoStreamDeckXLV2 => 4, }; } pub fn touch(self: *const Keys) u5 { const sd: *const StreamDeck = @alignCast(@fieldParentPtr("keys", self)); - return switch (sd.product) { - .StreamDeckMK2 => 0, - .StreamDeckMini => 0, - .StreamDeckMiniMK2 => 0, - .StreamDeckNeo => 2, - .StreamDeckOriginal => 0, - .StreamDeckOriginalV2 => 0, - .StreamDeckPedal => 0, - .StreamDeckPlus => 0, - .StreamDeckPlusXL => 0, - .StreamDeckStudio => 16, - .StreamDeckXL => 0, - .StreamDeckXLV2 => 0, + return switch (sd.type) { + .ElgatoStreamDeckMK2 => 0, + .ElgatoStreamDeckMini => 0, + .ElgatoStreamDeckMiniMK2 => 0, + .ElgatoStreamDeckNeo => 2, + .ElgatoStreamDeckOriginal => 0, + .ElgatoStreamDeckOriginalV2 => 0, + .ElgatoStreamDeckPedal => 0, + .ElgatoStreamDeckPlus => 0, + .ElgatoStreamDeckPlusXL => 0, + .ElgatoStreamDeckStudio => 16, + .ElgatoStreamDeckXL => 0, + .ElgatoStreamDeckXLV2 => 0, }; } @@ -158,8 +160,8 @@ pub const KeyFeature = union(enum) { pub fn getFeature(self: *const Keys, key: u8) ?KeyFeature { const sd: *const StreamDeck = @alignCast(@fieldParentPtr("keys", self)); - return switch (sd.product) { - .StreamDeckMK2 => switch (key) { + return switch (sd.type) { + .ElgatoStreamDeckMK2 => switch (key) { 0...15 => { return .{ .image = .{ @@ -175,7 +177,7 @@ pub fn getFeature(self: *const Keys, key: u8) ?KeyFeature { }, else => return null, }, - .StreamDeckMini => switch (key) { + .ElgatoStreamDeckMini => switch (key) { 0...5 => { return .{ .image = .{ @@ -191,7 +193,7 @@ pub fn getFeature(self: *const Keys, key: u8) ?KeyFeature { }, else => return null, }, - .StreamDeckMiniMK2 => switch (key) { + .ElgatoStreamDeckMiniMK2 => switch (key) { 0...5 => { return .{ .image = .{ @@ -207,7 +209,7 @@ pub fn getFeature(self: *const Keys, key: u8) ?KeyFeature { }, else => return null, }, - .StreamDeckOriginal => switch (key) { + .ElgatoStreamDeckOriginal => switch (key) { 0...15 => { return .{ .image = .{ @@ -223,7 +225,7 @@ pub fn getFeature(self: *const Keys, key: u8) ?KeyFeature { }, else => return null, }, - .StreamDeckOriginalV2 => switch (key) { + .ElgatoStreamDeckOriginalV2 => switch (key) { 0...15 => { return .{ .image = .{ @@ -239,7 +241,7 @@ pub fn getFeature(self: *const Keys, key: u8) ?KeyFeature { }, else => return null, }, - .StreamDeckPedal => switch (key) { + .ElgatoStreamDeckPedal => switch (key) { 0...2 => { return .{ .none = {}, @@ -247,7 +249,7 @@ pub fn getFeature(self: *const Keys, key: u8) ?KeyFeature { }, else => return null, }, - .StreamDeckPlus => switch (key) { + .ElgatoStreamDeckPlus => switch (key) { 0...7 => { return .{ .image = .{ @@ -263,7 +265,7 @@ pub fn getFeature(self: *const Keys, key: u8) ?KeyFeature { }, else => return null, }, - .StreamDeckPlusXL => switch (key) { + .ElgatoStreamDeckPlusXL => switch (key) { 0...35 => { return .{ .image = .{ @@ -279,7 +281,7 @@ pub fn getFeature(self: *const Keys, key: u8) ?KeyFeature { }, else => return null, }, - .StreamDeckXL => switch (key) { + .ElgatoStreamDeckXL => switch (key) { 0...31 => { return .{ .image = .{ @@ -295,7 +297,7 @@ pub fn getFeature(self: *const Keys, key: u8) ?KeyFeature { }, else => return null, }, - .StreamDeckXLV2 => switch (key) { + .ElgatoStreamDeckXLV2 => switch (key) { 0...31 => { return .{ .image = .{ @@ -311,7 +313,7 @@ pub fn getFeature(self: *const Keys, key: u8) ?KeyFeature { }, else => return null, }, - .StreamDeckNeo => switch (key) { + .ElgatoStreamDeckNeo => switch (key) { 0...8 => { return .{ .image = .{ @@ -425,11 +427,11 @@ pub fn setImage(self: *const Keys, alloc: std.mem.Allocator, key: u8, image: zig } } -pub fn lamp(self: *const Keys, alloc: std.mem.Allocator, key: u8) !?zigimg.ImageUnmanaged { - // return try svg.render(alloc, @embedFile("lamp.svg"), width, height); - const feature = self.getImageFeature(key) orelse return null; - return try svg.text(alloc, feature.width, feature.height); -} +// pub fn lamp(self: *const Keys, alloc: std.mem.Allocator, key: u8) !?zigimg.ImageUnmanaged { +// // return try svg.render(alloc, @embedFile("lamp.svg"), width, height); +// const feature = self.getImageFeature(key) orelse return null; +// return try svg.text(alloc, feature.width, feature.height); +// } pub fn blankKeyImage(self: *const Keys, alloc: std.mem.Allocator, key: u8) !?zigimg.ImageUnmanaged { const feature = self.getImageFeature(key) orelse return null; diff --git a/src/StreamDeck.zig b/src/StreamDeck.zig index 8ac9eb7..4c5b444 100644 --- a/src/StreamDeck.zig +++ b/src/StreamDeck.zig @@ -11,46 +11,63 @@ const Touchscreen = @import("Touchscreen.zig"); const Encoders = @import("Encoders.zig"); const Events = @import("Events.zig"); -pub const Vendor = enum(u16) { - Elgato = 0x0fd9, - // Mirabox = 0x5548, -}; +pub const Type = enum(u32) { + ElgatoStreamDeckMK2 = 0x0fd90080, + ElgatoStreamDeckMini = 0x0fd90063, + ElgatoStreamDeckMiniMK2 = 0x0fd90090, + ElgatoStreamDeckNeo = 0x0fd9009a, + ElgatoStreamDeckOriginal = 0x0fd90060, + ElgatoStreamDeckOriginalV2 = 0x0fd9006d, + ElgatoStreamDeckPedal = 0x0fd90086, + ElgatoStreamDeckPlus = 0x0fd90084, + ElgatoStreamDeckPlusXL = 0x0fd900c6, + ElgatoStreamDeckStudio = 0x0fd900aa, + ElgatoStreamDeckXL = 0x0fd9006c, + ElgatoStreamDeckXLV2 = 0x0fd9008f, + // MiraboxStreamDock293S = 0x55486670, + + pub fn init(v: u16, p: u16) error{NotAStreamDeck}!Type { + log.info("{x:04} {x:04} {x:08}", .{ v, p, (@as(u32, v) << 16) + p }); + return std.enums.fromInt(Type, (@as(u32, v) << 16) + p) orelse error.NotAStreamDeck; + } + + pub fn vendor(self: Type) u16 { + return @intFromEnum(self) >> 16; + } -pub const Product = enum(u16) { - // Elgato - StreamDeckMK2 = 0x0080, - StreamDeckMini = 0x0063, - StreamDeckMiniMK2 = 0x0090, - StreamDeckNeo = 0x009a, - StreamDeckOriginal = 0x0060, - StreamDeckOriginalV2 = 0x006d, - StreamDeckPedal = 0x0086, - StreamDeckPlus = 0x0084, - StreamDeckPlusXL = 0x00c6, - StreamDeckStudio = 0x00aa, - StreamDeckXL = 0x006c, - StreamDeckXLV2 = 0x008f, - // Mirabox - // StreamDock293S = 0x6670, + pub fn product(self: Type) u16 { + return @intFromEnum(self) & 0xffff; + } }; -vendor: Vendor, -product: Product, +type: Type, device: hidapi.Device, -keys: Keys = .{}, -touchscreen: Touchscreen = .{}, -encoders: Encoders = .{}, -events: Events = .{}, +keys: Keys = .init, +touchscreen: Touchscreen = .init, +encoders: Encoders = .init, +events: Events = .init, + +pub fn initFromDeviceInfo(info: hidapi.DeviceInfo) !StreamDeck { + return .{ + .type = try .init(info.vendor, info.product), + .device = info.device, + }; +} + +pub fn initFromDevice(io: std.Io, device: hidapi.Device) !StreamDeck { + const info = try device.getDeviceInfo(io); + return .{ + .type = try .init(info.vendor, info.product), + .device = device, + }; +} pub fn open(io: std.Io, minor: u64) !StreamDeck { var device = try hidapi.Device.open(io, minor); errdefer device.close(io); const info = try device.getDeviceInfo(io); - const vendor = std.enums.fromInt(Vendor, info.vendor) orelse return error.NotAStreamDeck; - const product = std.enums.fromInt(Product, info.product) orelse return error.NotAStreamDeck; return .{ - .vendor = vendor, - .product = product, + .type = try .init(info.vendor, info.product), .device = device, }; } @@ -102,7 +119,7 @@ pub const MAX_PACKET_SIZE = 1024; // } pub fn resetKeyStream(self: *const StreamDeck, io: std.Io) !void { - if (self.product == .StreamDeckPedal) return; + if (self.type == .ElgatoStreamDeckPedal) return; var buffer = std.mem.zeroes([MAX_PACKET_SIZE]u8); buffer[0] = 0x02; _ = try self.device.write(io, &buffer); @@ -111,10 +128,10 @@ pub fn resetKeyStream(self: *const StreamDeck, io: std.Io) !void { pub fn reset(self: *const StreamDeck, io: std.Io) !void { var buf: [32]u8 = undefined; var writer = std.Io.Writer.fixed(&buf); - switch (self.product) { - .StreamDeckOriginal, - .StreamDeckMini, - .StreamDeckMiniMK2, + switch (self.type) { + .ElgatoStreamDeckOriginal, + .ElgatoStreamDeckMini, + .ElgatoStreamDeckMiniMK2, => { const packet: extern struct { byte0: u8 = 0x0b, @@ -125,15 +142,15 @@ pub fn reset(self: *const StreamDeck, io: std.Io) !void { try writer.writeStruct(packet, .little); }, - .StreamDeckOriginalV2, - .StreamDeckMK2, - .StreamDeckXL, - .StreamDeckXLV2, - .StreamDeckPlus, - .StreamDeckPlusXL, - .StreamDeckStudio, - .StreamDeckPedal, - .StreamDeckNeo, + .ElgatoStreamDeckOriginalV2, + .ElgatoStreamDeckMK2, + .ElgatoStreamDeckXL, + .ElgatoStreamDeckXLV2, + .ElgatoStreamDeckPlus, + .ElgatoStreamDeckPlusXL, + .ElgatoStreamDeckStudio, + .ElgatoStreamDeckPedal, + .ElgatoStreamDeckNeo, => { const packet: extern struct { byte0: u8 = 0x03, @@ -153,10 +170,10 @@ pub fn reset(self: *const StreamDeck, io: std.Io) !void { pub fn setBrightness(self: *const StreamDeck, io: std.Io, percent: u8) !void { var buf: [32]u8 = undefined; var writer = std.Io.Writer.fixed(&buf); - switch (self.product) { - .StreamDeckOriginal, - .StreamDeckMini, - .StreamDeckMiniMK2, + switch (self.type) { + .ElgatoStreamDeckOriginal, + .ElgatoStreamDeckMini, + .ElgatoStreamDeckMiniMK2, => { const packet: extern struct { byte0: u8 = 0x05, @@ -172,14 +189,14 @@ pub fn setBrightness(self: *const StreamDeck, io: std.Io, percent: u8) !void { std.debug.assert(@sizeOf(@TypeOf(packet)) == 17); try writer.writeStruct(packet, .little); }, - .StreamDeckMK2, - .StreamDeckNeo, - .StreamDeckOriginalV2, - .StreamDeckPlus, - .StreamDeckPlusXL, - .StreamDeckStudio, - .StreamDeckXL, - .StreamDeckXLV2, + .ElgatoStreamDeckMK2, + .ElgatoStreamDeckNeo, + .ElgatoStreamDeckOriginalV2, + .ElgatoStreamDeckPlus, + .ElgatoStreamDeckPlusXL, + .ElgatoStreamDeckStudio, + .ElgatoStreamDeckXL, + .ElgatoStreamDeckXLV2, => { const packet: extern struct { byte0: u8 = 0x03, @@ -191,45 +208,92 @@ pub fn setBrightness(self: *const StreamDeck, io: std.Io, percent: u8) !void { std.debug.assert(@sizeOf(@TypeOf(packet)) == 3); try writer.writeStruct(packet, .little); }, - .StreamDeckPedal => return, + .ElgatoStreamDeckPedal => return, } _ = try self.device.sendFeatureReport(io, writer.buffered()); } -pub fn getSerialNumber(self: *const StreamDeck, io: std.Io, alloc: std.mem.Allocator) ![]const u8 { +pub fn getSerialNumber(self: *const StreamDeck, io: std.Io, buffer: []u8) ![]const u8 { + @memset(buffer, 0); + const report = switch (self.type) { + .ElgatoStreamDeckOriginal, + .ElgatoStreamDeckMini, + => report: { + if (buffer.len < 17) return error.BufferTooSmall; + buffer[0] = 0x03; + break :report buffer[0..17]; + }, + + .ElgatoStreamDeckMiniMK2, + => report: { + if (buffer.len < 32) return error.BufferTooSmall; + buffer[0] = 0x03; + break :report buffer[0..32]; + }, + + .ElgatoStreamDeckMK2, + .ElgatoStreamDeckOriginalV2, + .ElgatoStreamDeckPedal, + .ElgatoStreamDeckPlus, + .ElgatoStreamDeckPlusXL, + .ElgatoStreamDeckStudio, + .ElgatoStreamDeckXL, + .ElgatoStreamDeckXLV2, + .ElgatoStreamDeckNeo, + => report: { + if (buffer.len < 32) return error.BufferTooSmall; + buffer[0] = 0x06; + break :report buffer[0..32]; + }, + }; + + const offset: usize = switch (self.type) { + .ElgatoStreamDeckMini => 5, + else => 2, + }; + + const result = try self.device.getFeatureReport(io, report); + + if (std.mem.indexOfScalarPos(u8, result, offset, 0)) |index| { + return result[offset..index]; + } else { + return result[offset..]; + } +} +pub fn getSerialNumberAlloc(self: *const StreamDeck, io: std.Io, alloc: std.mem.Allocator) ![]const u8 { var buffer = std.mem.zeroes([32]u8); - const report = switch (self.product) { - .StreamDeckOriginal, - .StreamDeckMini, + const report = switch (self.type) { + .ElgatoStreamDeckOriginal, + .ElgatoStreamDeckMini, => report: { buffer[0] = 0x03; break :report buffer[0..17]; }, - .StreamDeckMiniMK2, + .ElgatoStreamDeckMiniMK2, => report: { buffer[0] = 0x03; break :report buffer[0..32]; }, - .StreamDeckMK2, - .StreamDeckOriginalV2, - .StreamDeckPedal, - .StreamDeckPlus, - .StreamDeckPlusXL, - .StreamDeckStudio, - .StreamDeckXL, - .StreamDeckXLV2, - .StreamDeckNeo, + .ElgatoStreamDeckMK2, + .ElgatoStreamDeckOriginalV2, + .ElgatoStreamDeckPedal, + .ElgatoStreamDeckPlus, + .ElgatoStreamDeckPlusXL, + .ElgatoStreamDeckStudio, + .ElgatoStreamDeckXL, + .ElgatoStreamDeckXLV2, + .ElgatoStreamDeckNeo, => report: { buffer[0] = 0x06; break :report buffer[0..32]; }, }; - const offset: usize = switch (self.product) { - .StreamDeckMini => 5, + const offset: usize = switch (self.type) { + .ElgatoStreamDeckMini => 5, else => 2, }; @@ -242,48 +306,104 @@ pub fn getSerialNumber(self: *const StreamDeck, io: std.Io, alloc: std.mem.Alloc } } -pub fn getFirmwareVersion(self: *const StreamDeck, io: std.Io, alloc: std.mem.Allocator) ![]const u8 { +pub fn getFirmwareVersion(self: *const StreamDeck, io: std.Io, buffer: []u8) ![]const u8 { + @memset(buffer, 0); + const report = switch (self.type) { + .ElgatoStreamDeckOriginal, + .ElgatoStreamDeckMini, + .ElgatoStreamDeckMiniMK2, + => report: { + if (buffer.len < 17) return error.BufferTooSmall; + buffer[0] = 0x04; + break :report buffer[0..17]; + }, + .ElgatoStreamDeckMK2, + .ElgatoStreamDeckOriginalV2, + .ElgatoStreamDeckPedal, + .ElgatoStreamDeckPlus, + .ElgatoStreamDeckPlusXL, + .ElgatoStreamDeckStudio, + .ElgatoStreamDeckNeo, + => report: { + if (buffer.len < 32) return error.BufferTooSmall; + buffer[0] = 0x05; + break :report buffer[0..32]; + }, + .ElgatoStreamDeckXL, + .ElgatoStreamDeckXLV2, + => { + return buffer[0..0]; + }, + }; + + const result = try self.device.getFeatureReport(io, report); + + const offset: usize = switch (self.type) { + .ElgatoStreamDeckMK2, + .ElgatoStreamDeckNeo, + .ElgatoStreamDeckOriginalV2, + .ElgatoStreamDeckPedal, + .ElgatoStreamDeckPlus, + .ElgatoStreamDeckPlusXL, + .ElgatoStreamDeckStudio, + .ElgatoStreamDeckXL, + .ElgatoStreamDeckXLV2, + => 6, + .ElgatoStreamDeckMini, + .ElgatoStreamDeckMiniMK2, + .ElgatoStreamDeckOriginal, + => 5, + }; + + if (std.mem.indexOfScalarPos(u8, result, offset, 0)) |index| { + return result[offset..index]; + } else { + return result[offset..]; + } +} + +pub fn getFirmwareVersionAlloc(self: *const StreamDeck, io: std.Io, alloc: std.mem.Allocator) ![]const u8 { var buffer = std.mem.zeroes([32]u8); - const report = switch (self.product) { - .StreamDeckOriginal, - .StreamDeckMini, - .StreamDeckMiniMK2, + const report = switch (self.type) { + .ElgatoStreamDeckOriginal, + .ElgatoStreamDeckMini, + .ElgatoStreamDeckMiniMK2, => report: { buffer[0] = 0x04; break :report buffer[0..17]; }, - .StreamDeckMK2, - .StreamDeckOriginalV2, - .StreamDeckPedal, - .StreamDeckPlus, - .StreamDeckPlusXL, - .StreamDeckStudio, - .StreamDeckNeo, + .ElgatoStreamDeckMK2, + .ElgatoStreamDeckOriginalV2, + .ElgatoStreamDeckPedal, + .ElgatoStreamDeckPlus, + .ElgatoStreamDeckPlusXL, + .ElgatoStreamDeckStudio, + .ElgatoStreamDeckNeo, => report: { buffer[0] = 0x05; break :report buffer[0..32]; }, - .StreamDeckXL, - .StreamDeckXLV2, + .ElgatoStreamDeckXL, + .ElgatoStreamDeckXLV2, => unreachable, }; const result = try self.device.getFeatureReport(io, report); - const offset: usize = switch (self.product) { - .StreamDeckMK2, - .StreamDeckOriginalV2, - .StreamDeckPedal, - .StreamDeckPlus, - .StreamDeckPlusXL, - .StreamDeckStudio, - .StreamDeckXL, - .StreamDeckXLV2, - .StreamDeckNeo, + const offset: usize = switch (self.type) { + .ElgatoStreamDeckMK2, + .ElgatoStreamDeckOriginalV2, + .ElgatoStreamDeckPedal, + .ElgatoStreamDeckPlus, + .ElgatoStreamDeckPlusXL, + .ElgatoStreamDeckStudio, + .ElgatoStreamDeckXL, + .ElgatoStreamDeckXLV2, + .ElgatoStreamDeckNeo, => 6, - .StreamDeckMini, - .StreamDeckMiniMK2, - .StreamDeckOriginal, + .ElgatoStreamDeckMini, + .ElgatoStreamDeckMiniMK2, + .ElgatoStreamDeckOriginal, => 5, }; diff --git a/src/Touchscreen.zig b/src/Touchscreen.zig index 85c029a..4139aab 100644 --- a/src/Touchscreen.zig +++ b/src/Touchscreen.zig @@ -2,13 +2,15 @@ const std = @import("std"); const ImageFormat = @import("root.zig").ImageFormat; const ImageRotation = @import("root.zig").ImageRotation; -const StreamDeck = @import("streamdeck.zig"); +const StreamDeck = @import("StreamDeck.zig"); const ImageHeaderV3 = @import("image.zig").ImageHeaderV3; const Touchscreen = @This(); const log = std.log.scoped(.touchscreen); +pub const init: Touchscreen = .{}; + pub const EventType = enum(u8) { short = 1, long = 2, @@ -92,19 +94,19 @@ pub const Event = struct { pub fn readSize(self: *const Touchscreen) usize { const sd: *const StreamDeck = @alignCast(@fieldParentPtr("touchscreen", self)); - return switch (sd.product) { - .StreamDeckMK2 => 0, - .StreamDeckMini => 0, - .StreamDeckMiniMK2 => 0, - .StreamDeckNeo => 10, - .StreamDeckOriginal => 0, - .StreamDeckOriginalV2 => 0, - .StreamDeckPedal => 0, - .StreamDeckPlus => 0, - .StreamDeckPlusXL => 0, - .StreamDeckStudio => 0, - .StreamDeckXL => 0, - .StreamDeckXLV2 => 0, + return switch (sd.type) { + .ElgatoStreamDeckMK2 => 0, + .ElgatoStreamDeckMini => 0, + .ElgatoStreamDeckMiniMK2 => 0, + .ElgatoStreamDeckNeo => 10, + .ElgatoStreamDeckOriginal => 0, + .ElgatoStreamDeckOriginalV2 => 0, + .ElgatoStreamDeckPedal => 0, + .ElgatoStreamDeckPlus => 0, + .ElgatoStreamDeckPlusXL => 0, + .ElgatoStreamDeckStudio => 0, + .ElgatoStreamDeckXL => 0, + .ElgatoStreamDeckXLV2 => 0, }; } diff --git a/src/bmp.zig b/src/bmp.zig new file mode 100644 index 0000000..25b16e0 --- /dev/null +++ b/src/bmp.zig @@ -0,0 +1,27 @@ +const std = @import("std"); + +const log = std.log.scoped(.jpeg); + +const zigimg = @import("zigimg"); + +pub fn encode(alloc: std.mem.Allocator, image: zigimg.ImageUnmanaged) ![]const u8 { + const raw_data = image.rawBytes(); + var copy = try zigimg.ImageUnmanaged.fromRawPixels(alloc, image.width, image.height, raw_data, image.pixelFormat()); + + defer copy.deinit(alloc); + try copy.convert(alloc, .bgr24); + + const buf = try alloc.alloc(u8, raw_data.len * 2); + defer alloc.free(buf); + + const bmp_data = try copy.writeToMemory( + alloc, + buf, + .{ + .bmp = .{}, + }, + ); + errdefer alloc.free(bmp_data); + + return try alloc.dupe(u8, bmp_data); +} diff --git a/src/main.zig b/src/main.zig index 32d0fc2..65b1bd3 100644 --- a/src/main.zig +++ b/src/main.zig @@ -28,10 +28,10 @@ pub fn sd_handler(io: std.Io, alloc: std.mem.Allocator, info: streamdeck.StreamD } pub fn _sd_handler(io: std.Io, alloc: std.mem.Allocator, info: streamdeck.StreamDeckInfo) !void { - defer info.deinit(); + defer info.deinit(io); - const sd = try info.open(io); - defer sd.close(io); + const sd = info.deck; + // defer sd.close(io); { var buf: [1024]u8 = undefined; @@ -48,43 +48,46 @@ pub fn _sd_handler(io: std.Io, alloc: std.mem.Allocator, info: streamdeck.Stream // defer alloc.free(product); // try stderr.print("product : {s}\n", .{product}); - const serial = try sd.getSerialNumber(io, alloc); - defer alloc.free(serial); + var serial_buffer: [32]u8 = undefined; + const serial = try sd.getSerialNumber(io, &serial_buffer); try stderr.print("serial : {s}\n", .{serial}); - const version = try sd.getFirmwareVersion(io, alloc); - defer alloc.free(version); + var version_buffer: [32]u8 = undefined; + const version = try sd.getFirmwareVersion(io, &version_buffer); try stderr.print("firmware version: {s}\n", .{version}); } - const serial = try sd.getSerialNumber(io, alloc); - defer alloc.free(serial); - - try sd.reset(io); - try sd.resetKeyStream(io); - // try sd.setTouchscreenImage(0, 0, 800, 100, sd.blankTouchscreenImage()); - try sd.setBrightness(io, 50); - // for (0..sd.keys.count()) |i| { - // const key: u8 = @intCast(i); - // // const blank_image = try sd.keys.blankImage(alloc, key); - // // defer alloc.free(blank_image); - // // try sd.keys.setImage(key, blank_image); - // var lamp = try sd.keys.lamp(alloc, key) orelse continue; - // // defer alloc.free(lamp); - // defer lamp.deinit(alloc); - // try sd.keys.setImage(alloc, key, lamp); - // } - - while (true) { - if (try sd.events.read(io, alloc)) |event| { - // log.info("{any}", .{event}); - - defer event.deinit(alloc); - - switch (event) { - .keys => |e| log.info("{s} {any}", .{ serial, e.event }), - .encoders => |e| log.info("{s} {any}", .{ serial, e.event }), - .touchscreen => |e| log.info("{s} {any}", .{ serial, e.event }), + { + var serial_buffer: [32]u8 = undefined; + const serial = try sd.getSerialNumber(io, &serial_buffer); + // defer alloc.free(serial); + + try sd.reset(io); + try sd.resetKeyStream(io); + // try sd.setTouchscreenImage(0, 0, 800, 100, sd.blankTouchscreenImage()); + try sd.setBrightness(io, 50); + // for (0..sd.keys.count()) |i| { + // const key: u8 = @intCast(i); + // // const blank_image = try sd.keys.blankImage(alloc, key); + // // defer alloc.free(blank_image); + // // try sd.keys.setImage(key, blank_image); + // var lamp = try sd.keys.lamp(alloc, key) orelse continue; + // // defer alloc.free(lamp); + // defer lamp.deinit(alloc); + // try sd.keys.setImage(alloc, key, lamp); + // } + + while (true) { + if (try sd.events.read(io, alloc)) |event| { + // log.info("{any}", .{event}); + + defer event.deinit(alloc); + + switch (event) { + .keys => |e| log.info("{s} {any}", .{ serial, e.event }), + .encoders => |e| log.info("{s} {any}", .{ serial, e.event }), + .touchscreen => |e| log.info("{s} {any}", .{ serial, e.event }), + } } } } diff --git a/src/root.zig b/src/root.zig index e1fc4ec..924711a 100644 --- a/src/root.zig +++ b/src/root.zig @@ -5,9 +5,9 @@ const streamdeck = @This(); const log = std.log.scoped(.streamdeck); -const StreamDeck = @import("streamdeck.zig"); -const Keys = @import("keys.zig"); -const Touchscreen = @import("touchscreen.zig"); +const StreamDeck = @import("StreamDeck.zig"); +const Keys = @import("Keys.zig"); +const Touchscreen = @import("Touchscreen.zig"); pub fn init() !void {} @@ -34,15 +34,14 @@ pub const Command = enum(u8) { }; pub const StreamDeckInfo = struct { - minor: u64, - product: StreamDeck.Product, + deck: StreamDeck, pub fn open(self: *const StreamDeckInfo, io: std.Io) !StreamDeck { return try .open(io, self.minor); } - pub fn deinit(self: *const StreamDeckInfo) void { - _ = self; + pub fn deinit(self: *const StreamDeckInfo, io: std.Io) void { + self.deck.close(io); } }; @@ -72,26 +71,28 @@ pub const StreamDeckIterator = struct { pub const init: StreamDeckIterator = .{}; pub fn next(self: *StreamDeckIterator, io: std.Io) !?StreamDeckInfo { - while (try self.devices.next(io)) |device| { - var d = try device.open(io); + while (try self.devices.next(io)) |info| { + var deck: StreamDeck = StreamDeck.initFromDeviceInfo(info) catch |err| switch (err) { + error.NotAStreamDeck => continue, + else => |e| return e, + }; + errdefer deck.close(io); + { var b: [1024]u8 = undefined; - const n = try d.getRawName(io, &b); - log.info("device {d} raw name: |{s}|", .{ device.minor, n }); - log.info("device {d} raw name: |{any}|", .{ device.minor, n }); + const n = try deck.device.getRawName(io, &b); + log.info("device {d} raw name: |{s}|", .{ info.device.minor, n }); + log.info("device {d} raw name: |{any}|", .{ info.device.minor, n }); } { var b: [1024]u8 = undefined; - const n = try d.getPhysicalLocation(io, &b); - log.info("device {d} physical location: |{s}|", .{ device.minor, n }); - log.info("device {d} physical location: |{any}|", .{ device.minor, n }); + const n = try deck.device.getPhysicalLocation(io, &b); + log.info("device {d} physical location: |{s}|", .{ info.device.minor, n }); + log.info("device {d} physical location: |{any}|", .{ info.device.minor, n }); } - _ = std.enums.fromInt(StreamDeck.Vendor, device.vendor) orelse continue; - const product = std.enums.fromInt(StreamDeck.Product, device.product) orelse continue; return .{ - .minor = device.minor, - .product = product, + .deck = deck, }; } return null; @@ -107,6 +108,6 @@ pub fn enumerate() !StreamDeckIterator { return .init; } -// pub fn open(product: StreamDeck.Product, minor) !StreamDeck { +// pub fn openSerial(io: std.Io, serial: []const u8) !StreamDeck { // return try StreamDeck.open(product, serial); // } -- knotmirror 0.0.1