Add support for the StreamDeck

This commit is contained in:
Fefedu973
2025-09-14 05:59:48 +00:00
committed by Adam Honse
parent 4664a27d6b
commit f8895af960
7 changed files with 2034 additions and 1 deletions

View File

@@ -0,0 +1,85 @@
/*---------------------------------------------------------*\
| ElgatoStreamDeckController.cpp |
| |
| Driver for Elgato Stream Deck MK.2 |
| |
| Ferréol DUBOIS COLI (Fefe_du_973) 23 Jan 2025 |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-only |
\*---------------------------------------------------------*/
#include <cstring>
#include <vector>
#include "ElgatoStreamDeckController.h"
#include "StringUtils.h"
ElgatoStreamDeckController::ElgatoStreamDeckController(hid_device* dev_handle, const char* path) :
dev(dev_handle), location(path)
{
}
ElgatoStreamDeckController::~ElgatoStreamDeckController()
{
hid_close(dev);
}
std::string ElgatoStreamDeckController::GetLocation()
{
return location;
}
std::string ElgatoStreamDeckController::GetSerialString()
{
wchar_t serial[256];
if(hid_get_serial_number_string(dev, serial, 256) >= 0)
{
std::wstring ws(serial);
return StringUtils::wstring_to_string(ws);
}
return "";
}
void ElgatoStreamDeckController::SetBrightness(unsigned char brightness)
{
unsigned char buffer[32] = {0x03, 0x08, brightness};
hid_send_feature_report(dev, buffer, sizeof(buffer));
}
void ElgatoStreamDeckController::SendFullFrame(const std::vector<std::vector<unsigned char>>& buttonImages)
{
for(int btnIdx = 0; btnIdx < 15; btnIdx++)
{
if(btnIdx < buttonImages.size())
{
SendButtonImage(btnIdx, buttonImages[btnIdx]);
}
}
}
void ElgatoStreamDeckController::SendButtonImage(int buttonIndex, const std::vector<unsigned char>& jpegData)
{
const size_t headerSize = 8;
const size_t packetSize = 1024;
unsigned char buffer[packetSize] = {0};
buffer[0] = 0x02;
buffer[1] = 0x07;
buffer[2] = buttonIndex;
buffer[3] = 0x01;
buffer[4] = jpegData.size() & 0xFF;
buffer[5] = (jpegData.size() >> 8) & 0xFF;
buffer[6] = 0x00;
buffer[7] = 0x00;
size_t bytesToCopy = std::min(jpegData.size(), packetSize - headerSize);
memcpy(buffer + headerSize, jpegData.data(), bytesToCopy);
hid_write(dev, buffer, packetSize);
}
void ElgatoStreamDeckController::Reset()
{
unsigned char resetBuffer[32] = {0x03, 0x02};
hid_send_feature_report(dev, resetBuffer, sizeof(resetBuffer));
}

View File

@@ -0,0 +1,35 @@
/*---------------------------------------------------------*\
| ElgatoStreamDeckController.h |
| |
| Driver for Elgato Stream Deck MK.2 |
| |
| Ferréol DUBOIS COLI (Fefe_du_973) 23 Jan 2025 |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-only |
\*---------------------------------------------------------*/
#pragma once
#include <hidapi.h>
#include <string>
#include <vector>
class ElgatoStreamDeckController
{
public:
ElgatoStreamDeckController(hid_device* dev_handle, const char* path);
~ElgatoStreamDeckController();
std::string GetLocation();
std::string GetSerialString();
void SetBrightness(unsigned char brightness);
void SendFullFrame(const std::vector<std::vector<unsigned char>>& buttonImages);
void SendButtonImage(int buttonIndex, const std::vector<unsigned char>& jpegData);
private:
hid_device* dev;
std::string location;
void Reset();
};

View File

@@ -0,0 +1,33 @@
/*---------------------------------------------------------*\
| ElgatoStreamDeckControllerDetect.cpp |
| |
| Detector for Elgato Stream Deck MK.2 |
| |
| Ferréol DUBOIS COLI (Fefe_du_973) 23 Jan 2025 |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-only |
\*---------------------------------------------------------*/
#include "Detector.h"
#include "ElgatoStreamDeckController.h"
#include "RGBController_ElgatoStreamDeck.h"
#define ELGATO_VID 0x0FD9
#define STREAMDECK_MK2_PID 0x0080
void DetectElgatoStreamDeckControllers(hid_device_info* info, const std::string&)
{
if(info->interface_number == 0)
{
hid_device* dev = hid_open_path(info->path);
if(dev)
{
ElgatoStreamDeckController* controller = new ElgatoStreamDeckController(dev, info->path);
RGBController_ElgatoStreamDeck* rgb_controller = new RGBController_ElgatoStreamDeck(controller);
ResourceManager::get()->RegisterRGBController(rgb_controller);
}
}
}
REGISTER_HID_DETECTOR("Elgato Stream Deck MK.2", DetectElgatoStreamDeckControllers, ELGATO_VID, STREAMDECK_MK2_PID);

View File

@@ -0,0 +1,121 @@
/*---------------------------------------------------------*\
| RGBController_ElgatoStreamDeck.cpp |
| |
| RGBController for Elgato Stream Deck MK.2 |
| |
| Ferréol DUBOIS COLI (Fefe_du_973) 23 Jan 2025 |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-only |
\*---------------------------------------------------------*/
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "RGBController_ElgatoStreamDeck.h"
#include "stb_image_write.h"
/**------------------------------------------------------------------*\
@name Elgato Stream Deck MK.2 15 Buttons
@category Accessory
@type USB
@save :x:
@direct :white_check_mark:
@effects :x:
@detectors DetectElgatoStreamDeckControllers
@comment
\*-------------------------------------------------------------------*/
RGBController_ElgatoStreamDeck::RGBController_ElgatoStreamDeck(ElgatoStreamDeckController *controller_ptr) : controller(controller_ptr)
{
name = "Elgato Stream Deck MK.2";
vendor = "Elgato";
type = DEVICE_TYPE_ACCESSORY;
description = "Stream Deck MK.2 Controller";
location = controller->GetLocation();
mode Direct;
Direct.name = "Direct";
Direct.value = 0;
Direct.flags = MODE_FLAG_HAS_PER_LED_COLOR;
Direct.color_mode = MODE_COLORS_PER_LED;
modes.push_back(Direct);
SetupZones();
}
RGBController_ElgatoStreamDeck::~RGBController_ElgatoStreamDeck()
{
delete controller;
}
void RGBController_ElgatoStreamDeck::SetupZones()
{
zone deck_zone;
deck_zone.name = "Button Matrix";
deck_zone.type = ZONE_TYPE_MATRIX;
deck_zone.leds_min = 15;
deck_zone.leds_max = 15;
deck_zone.leds_count = 15;
deck_zone.matrix_map = new matrix_map_type;
deck_zone.matrix_map->height = 3;
deck_zone.matrix_map->width = 5;
deck_zone.matrix_map->map = new unsigned int[15]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
zones.push_back(deck_zone);
for(unsigned int i = 0; i < 15; i++)
{
led new_led;
new_led.name = "Button " + std::to_string(i + 1);
leds.push_back(new_led);
}
SetupColors();
}
std::vector<unsigned char> RGBController_ElgatoStreamDeck::CreateButtonImage(const RGBColor &color)
{
const int width = 72;
const int height = 72;
std::vector<unsigned char> pixels(width * height * 3);
unsigned char r = RGBGetRValue(color);
unsigned char g = RGBGetGValue(color);
unsigned char b = RGBGetBValue(color);
for(int i = 0; i < width * height; i++)
{
pixels[i * 3 + 0] = r;
pixels[i * 3 + 1] = g;
pixels[i * 3 + 2] = b;
}
std::vector<unsigned char> jpegData;
stbi_write_jpg_to_func([](void *context, void *data, int size)
{
std::vector<unsigned char>* vec = static_cast<std::vector<unsigned char>*>(context);
vec->insert(vec->end(), static_cast<unsigned char*>(data), static_cast<unsigned char*>(data) + size); }, &jpegData, width, height, 3, pixels.data(), 95); // Quality 95
return jpegData;
}
void RGBController_ElgatoStreamDeck::DeviceUpdateLEDs()
{
std::vector<std::vector<unsigned char>> buttonImages;
for(unsigned int i = 0; i < leds.size(); i++)
{
buttonImages.push_back(CreateButtonImage(colors[i]));
}
controller->SendFullFrame(buttonImages);
}
void RGBController_ElgatoStreamDeck::UpdateZoneLEDs(int zone)
{
DeviceUpdateLEDs();
}
void RGBController_ElgatoStreamDeck::UpdateSingleLED(int led)
{
DeviceUpdateLEDs();
}
void RGBController_ElgatoStreamDeck::ResizeZone(int, int) {}
void RGBController_ElgatoStreamDeck::DeviceUpdateMode() {}

View File

@@ -0,0 +1,34 @@
/*---------------------------------------------------------*\
| RGBController_ElgatoStreamDeck.h |
| |
| RGBController for Elgato Stream Deck MK.2 |
| |
| Ferréol DUBOIS COLI (Fefe_du_973) 23 Jan 2025 |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-only |
\*---------------------------------------------------------*/
#pragma once
#include "ElgatoStreamDeckController.h"
#include "RGBController.h"
class RGBController_ElgatoStreamDeck : public RGBController
{
public:
explicit RGBController_ElgatoStreamDeck(ElgatoStreamDeckController* controller_ptr);
~RGBController_ElgatoStreamDeck() override;
void SetupZones() override;
void ResizeZone(int zone, int new_size) override;
void DeviceUpdateLEDs() override;
void UpdateZoneLEDs(int zone) override;
void UpdateSingleLED(int led) override;
void DeviceUpdateMode() override;
private:
ElgatoStreamDeckController* controller;
std::vector<unsigned char> CreateButtonImage(const RGBColor& color);
};

View File

@@ -164,7 +164,8 @@ INCLUDEPATH +=
RGBController/ \
qt/ \
SPDAccessor/ \
SuspendResume/
SuspendResume/ \
dependencies/stb/
HEADERS += \
$$GUI_H \

1724
dependencies/stb/stb_image_write.h vendored Normal file
View File

File diff suppressed because it is too large Load Diff