Skip to content

Commit f8895af

Browse files
Fefedu973CalcProgrammer1
authored andcommitted
Add support for the StreamDeck
1 parent 4664a27 commit f8895af

7 files changed

Lines changed: 2034 additions & 1 deletion

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*---------------------------------------------------------*\
2+
| ElgatoStreamDeckController.cpp |
3+
| |
4+
| Driver for Elgato Stream Deck MK.2 |
5+
| |
6+
| Ferréol DUBOIS COLI (Fefe_du_973) 23 Jan 2025 |
7+
| |
8+
| This file is part of the OpenRGB project |
9+
| SPDX-License-Identifier: GPL-2.0-only |
10+
\*---------------------------------------------------------*/
11+
12+
#include <cstring>
13+
#include <vector>
14+
#include "ElgatoStreamDeckController.h"
15+
#include "StringUtils.h"
16+
17+
ElgatoStreamDeckController::ElgatoStreamDeckController(hid_device* dev_handle, const char* path) :
18+
dev(dev_handle), location(path)
19+
{
20+
}
21+
22+
ElgatoStreamDeckController::~ElgatoStreamDeckController()
23+
{
24+
hid_close(dev);
25+
}
26+
27+
std::string ElgatoStreamDeckController::GetLocation()
28+
{
29+
return location;
30+
}
31+
32+
std::string ElgatoStreamDeckController::GetSerialString()
33+
{
34+
wchar_t serial[256];
35+
if(hid_get_serial_number_string(dev, serial, 256) >= 0)
36+
{
37+
std::wstring ws(serial);
38+
return StringUtils::wstring_to_string(ws);
39+
}
40+
return "";
41+
}
42+
43+
void ElgatoStreamDeckController::SetBrightness(unsigned char brightness)
44+
{
45+
unsigned char buffer[32] = {0x03, 0x08, brightness};
46+
hid_send_feature_report(dev, buffer, sizeof(buffer));
47+
}
48+
49+
void ElgatoStreamDeckController::SendFullFrame(const std::vector<std::vector<unsigned char>>& buttonImages)
50+
{
51+
for(int btnIdx = 0; btnIdx < 15; btnIdx++)
52+
{
53+
if(btnIdx < buttonImages.size())
54+
{
55+
SendButtonImage(btnIdx, buttonImages[btnIdx]);
56+
}
57+
}
58+
}
59+
60+
void ElgatoStreamDeckController::SendButtonImage(int buttonIndex, const std::vector<unsigned char>& jpegData)
61+
{
62+
const size_t headerSize = 8;
63+
const size_t packetSize = 1024;
64+
unsigned char buffer[packetSize] = {0};
65+
66+
buffer[0] = 0x02;
67+
buffer[1] = 0x07;
68+
buffer[2] = buttonIndex;
69+
buffer[3] = 0x01;
70+
buffer[4] = jpegData.size() & 0xFF;
71+
buffer[5] = (jpegData.size() >> 8) & 0xFF;
72+
buffer[6] = 0x00;
73+
buffer[7] = 0x00;
74+
75+
size_t bytesToCopy = std::min(jpegData.size(), packetSize - headerSize);
76+
memcpy(buffer + headerSize, jpegData.data(), bytesToCopy);
77+
78+
hid_write(dev, buffer, packetSize);
79+
}
80+
81+
void ElgatoStreamDeckController::Reset()
82+
{
83+
unsigned char resetBuffer[32] = {0x03, 0x02};
84+
hid_send_feature_report(dev, resetBuffer, sizeof(resetBuffer));
85+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*---------------------------------------------------------*\
2+
| ElgatoStreamDeckController.h |
3+
| |
4+
| Driver for Elgato Stream Deck MK.2 |
5+
| |
6+
| Ferréol DUBOIS COLI (Fefe_du_973) 23 Jan 2025 |
7+
| |
8+
| This file is part of the OpenRGB project |
9+
| SPDX-License-Identifier: GPL-2.0-only |
10+
\*---------------------------------------------------------*/
11+
12+
#pragma once
13+
14+
#include <hidapi.h>
15+
#include <string>
16+
#include <vector>
17+
18+
class ElgatoStreamDeckController
19+
{
20+
public:
21+
ElgatoStreamDeckController(hid_device* dev_handle, const char* path);
22+
~ElgatoStreamDeckController();
23+
24+
std::string GetLocation();
25+
std::string GetSerialString();
26+
void SetBrightness(unsigned char brightness);
27+
void SendFullFrame(const std::vector<std::vector<unsigned char>>& buttonImages);
28+
void SendButtonImage(int buttonIndex, const std::vector<unsigned char>& jpegData);
29+
30+
private:
31+
hid_device* dev;
32+
std::string location;
33+
34+
void Reset();
35+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*---------------------------------------------------------*\
2+
| ElgatoStreamDeckControllerDetect.cpp |
3+
| |
4+
| Detector for Elgato Stream Deck MK.2 |
5+
| |
6+
| Ferréol DUBOIS COLI (Fefe_du_973) 23 Jan 2025 |
7+
| |
8+
| This file is part of the OpenRGB project |
9+
| SPDX-License-Identifier: GPL-2.0-only |
10+
\*---------------------------------------------------------*/
11+
12+
#include "Detector.h"
13+
#include "ElgatoStreamDeckController.h"
14+
#include "RGBController_ElgatoStreamDeck.h"
15+
16+
#define ELGATO_VID 0x0FD9
17+
#define STREAMDECK_MK2_PID 0x0080
18+
19+
void DetectElgatoStreamDeckControllers(hid_device_info* info, const std::string&)
20+
{
21+
if(info->interface_number == 0)
22+
{
23+
hid_device* dev = hid_open_path(info->path);
24+
if(dev)
25+
{
26+
ElgatoStreamDeckController* controller = new ElgatoStreamDeckController(dev, info->path);
27+
RGBController_ElgatoStreamDeck* rgb_controller = new RGBController_ElgatoStreamDeck(controller);
28+
ResourceManager::get()->RegisterRGBController(rgb_controller);
29+
}
30+
}
31+
}
32+
33+
REGISTER_HID_DETECTOR("Elgato Stream Deck MK.2", DetectElgatoStreamDeckControllers, ELGATO_VID, STREAMDECK_MK2_PID);
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*---------------------------------------------------------*\
2+
| RGBController_ElgatoStreamDeck.cpp |
3+
| |
4+
| RGBController for Elgato Stream Deck MK.2 |
5+
| |
6+
| Ferréol DUBOIS COLI (Fefe_du_973) 23 Jan 2025 |
7+
| |
8+
| This file is part of the OpenRGB project |
9+
| SPDX-License-Identifier: GPL-2.0-only |
10+
\*---------------------------------------------------------*/
11+
#define STB_IMAGE_WRITE_IMPLEMENTATION
12+
#include "RGBController_ElgatoStreamDeck.h"
13+
#include "stb_image_write.h"
14+
15+
/**------------------------------------------------------------------*\
16+
@name Elgato Stream Deck MK.2 15 Buttons
17+
@category Accessory
18+
@type USB
19+
@save :x:
20+
@direct :white_check_mark:
21+
@effects :x:
22+
@detectors DetectElgatoStreamDeckControllers
23+
@comment
24+
\*-------------------------------------------------------------------*/
25+
26+
RGBController_ElgatoStreamDeck::RGBController_ElgatoStreamDeck(ElgatoStreamDeckController *controller_ptr) : controller(controller_ptr)
27+
{
28+
name = "Elgato Stream Deck MK.2";
29+
vendor = "Elgato";
30+
type = DEVICE_TYPE_ACCESSORY;
31+
description = "Stream Deck MK.2 Controller";
32+
location = controller->GetLocation();
33+
34+
mode Direct;
35+
Direct.name = "Direct";
36+
Direct.value = 0;
37+
Direct.flags = MODE_FLAG_HAS_PER_LED_COLOR;
38+
Direct.color_mode = MODE_COLORS_PER_LED;
39+
modes.push_back(Direct);
40+
41+
SetupZones();
42+
}
43+
44+
RGBController_ElgatoStreamDeck::~RGBController_ElgatoStreamDeck()
45+
{
46+
delete controller;
47+
}
48+
49+
void RGBController_ElgatoStreamDeck::SetupZones()
50+
{
51+
zone deck_zone;
52+
deck_zone.name = "Button Matrix";
53+
deck_zone.type = ZONE_TYPE_MATRIX;
54+
deck_zone.leds_min = 15;
55+
deck_zone.leds_max = 15;
56+
deck_zone.leds_count = 15;
57+
deck_zone.matrix_map = new matrix_map_type;
58+
deck_zone.matrix_map->height = 3;
59+
deck_zone.matrix_map->width = 5;
60+
deck_zone.matrix_map->map = new unsigned int[15]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
61+
62+
zones.push_back(deck_zone);
63+
64+
for(unsigned int i = 0; i < 15; i++)
65+
{
66+
led new_led;
67+
new_led.name = "Button " + std::to_string(i + 1);
68+
leds.push_back(new_led);
69+
}
70+
71+
SetupColors();
72+
}
73+
74+
std::vector<unsigned char> RGBController_ElgatoStreamDeck::CreateButtonImage(const RGBColor &color)
75+
{
76+
const int width = 72;
77+
const int height = 72;
78+
std::vector<unsigned char> pixels(width * height * 3);
79+
80+
unsigned char r = RGBGetRValue(color);
81+
unsigned char g = RGBGetGValue(color);
82+
unsigned char b = RGBGetBValue(color);
83+
84+
for(int i = 0; i < width * height; i++)
85+
{
86+
pixels[i * 3 + 0] = r;
87+
pixels[i * 3 + 1] = g;
88+
pixels[i * 3 + 2] = b;
89+
}
90+
91+
std::vector<unsigned char> jpegData;
92+
stbi_write_jpg_to_func([](void *context, void *data, int size)
93+
{
94+
std::vector<unsigned char>* vec = static_cast<std::vector<unsigned char>*>(context);
95+
vec->insert(vec->end(), static_cast<unsigned char*>(data), static_cast<unsigned char*>(data) + size); }, &jpegData, width, height, 3, pixels.data(), 95); // Quality 95
96+
97+
return jpegData;
98+
}
99+
100+
void RGBController_ElgatoStreamDeck::DeviceUpdateLEDs()
101+
{
102+
std::vector<std::vector<unsigned char>> buttonImages;
103+
for(unsigned int i = 0; i < leds.size(); i++)
104+
{
105+
buttonImages.push_back(CreateButtonImage(colors[i]));
106+
}
107+
controller->SendFullFrame(buttonImages);
108+
}
109+
110+
void RGBController_ElgatoStreamDeck::UpdateZoneLEDs(int zone)
111+
{
112+
DeviceUpdateLEDs();
113+
}
114+
115+
void RGBController_ElgatoStreamDeck::UpdateSingleLED(int led)
116+
{
117+
DeviceUpdateLEDs();
118+
}
119+
120+
void RGBController_ElgatoStreamDeck::ResizeZone(int, int) {}
121+
void RGBController_ElgatoStreamDeck::DeviceUpdateMode() {}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*---------------------------------------------------------*\
2+
| RGBController_ElgatoStreamDeck.h |
3+
| |
4+
| RGBController for Elgato Stream Deck MK.2 |
5+
| |
6+
| Ferréol DUBOIS COLI (Fefe_du_973) 23 Jan 2025 |
7+
| |
8+
| This file is part of the OpenRGB project |
9+
| SPDX-License-Identifier: GPL-2.0-only |
10+
\*---------------------------------------------------------*/
11+
12+
#pragma once
13+
14+
#include "ElgatoStreamDeckController.h"
15+
#include "RGBController.h"
16+
17+
class RGBController_ElgatoStreamDeck : public RGBController
18+
{
19+
public:
20+
explicit RGBController_ElgatoStreamDeck(ElgatoStreamDeckController* controller_ptr);
21+
~RGBController_ElgatoStreamDeck() override;
22+
23+
void SetupZones() override;
24+
void ResizeZone(int zone, int new_size) override;
25+
void DeviceUpdateLEDs() override;
26+
void UpdateZoneLEDs(int zone) override;
27+
void UpdateSingleLED(int led) override;
28+
void DeviceUpdateMode() override;
29+
30+
private:
31+
ElgatoStreamDeckController* controller;
32+
33+
std::vector<unsigned char> CreateButtonImage(const RGBColor& color);
34+
};

OpenRGB.pro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ INCLUDEPATH +=
164164
RGBController/ \
165165
qt/ \
166166
SPDAccessor/ \
167-
SuspendResume/
167+
SuspendResume/ \
168+
dependencies/stb/
168169

169170
HEADERS += \
170171
$$GUI_H \

0 commit comments

Comments
 (0)