From 53ed66c8b896e323a5c076b41df6787d94c7ed14 Mon Sep 17 00:00:00 2001 From: Mola19 Date: Mon, 13 Mar 2023 20:56:19 +0100 Subject: [PATCH] add support for Roccat Magma to resolve #3205 --- .../RGBController_RoccatVulcanKeyboard.cpp | 9 +- .../RoccatControllerDetect.cpp | 9 +- .../RoccatVulcanKeyboardController.cpp | 237 ++++++++++++++---- .../RoccatVulcanKeyboardController.h | 5 +- .../RoccatVulcanKeyboardLayouts.h | 37 ++- 5 files changed, 231 insertions(+), 66 deletions(-) diff --git a/Controllers/RoccatController/RGBController_RoccatVulcanKeyboard.cpp b/Controllers/RoccatController/RGBController_RoccatVulcanKeyboard.cpp index 536e4529..fcc0fd69 100644 --- a/Controllers/RoccatController/RGBController_RoccatVulcanKeyboard.cpp +++ b/Controllers/RoccatController/RGBController_RoccatVulcanKeyboard.cpp @@ -48,7 +48,7 @@ RGBController_RoccatVulcanKeyboard::RGBController_RoccatVulcanKeyboard(RoccatVul Direct.flags = MODE_FLAG_HAS_PER_LED_COLOR; Direct.color_mode = MODE_COLORS_PER_LED; - if(pid == ROCCAT_VULCAN_TKL) + if(pid != ROCCAT_VULCAN_120_AIMO_PID) { Direct.flags |= MODE_FLAG_HAS_BRIGHTNESS; Direct.brightness_min = ROCCAT_VULCAN_BRIGHTNESS_MIN; @@ -105,12 +105,15 @@ void RGBController_RoccatVulcanKeyboard::SetupZones() switch(pid) { - case ROCCAT_VULCAN_120_AIMO: + case ROCCAT_VULCAN_120_AIMO_PID: keyboard_ptr = &RoccatVulcan120AimoLayouts; break; - case ROCCAT_VULCAN_TKL: + case ROCCAT_VULCAN_TKL_PID: keyboard_ptr = &RoccatVulcanTKLLayouts; break; + case ROCCAT_MAGMA_PID: + keyboard_ptr = &RoccatMagmaLayouts; + break; default: keyboard_ptr = &RoccatVulcan120AimoLayouts; } diff --git a/Controllers/RoccatController/RoccatControllerDetect.cpp b/Controllers/RoccatController/RoccatControllerDetect.cpp index f83416fb..f5deb419 100644 --- a/Controllers/RoccatController/RoccatControllerDetect.cpp +++ b/Controllers/RoccatController/RoccatControllerDetect.cpp @@ -22,6 +22,7 @@ #define ROCCAT_KONE_AIMO_16K_PID 0x2E2C #define ROCCAT_VULCAN_120_AIMO_PID 0x3098 #define ROCCAT_VULCAN_TKL_PID 0x2FEE +#define ROCCAT_MAGMA_PID 0x3124 #define ROCCAT_HORDE_AIMO_PID 0x303E #define ROCCAT_BURST_CORE_PID 0x2DE6 #define ROCCAT_BURST_PRO_PID 0x2DE1 @@ -74,6 +75,9 @@ void DetectRoccatVulcanKeyboardControllers(hid_device_info* info, const std::str std::string dev_ctrl_path; std::string dev_led_path; + int dev_led_page = (info->product_id == ROCCAT_MAGMA_PID) ? 0xFF00 : 0x0001; + int dev_ctrl_page = (info->product_id == ROCCAT_MAGMA_PID) ? 0xFF01 : 0x000B; + while(info_temp) { /*----------------------------------------------------------------------------------------*\ @@ -84,12 +88,12 @@ void DetectRoccatVulcanKeyboardControllers(hid_device_info* info, const std::str && info_temp->product_id == info->product_id && used_paths.find(info_temp->path) == used_paths.end() ) { - if(info_temp->interface_number == 1 && info_temp->usage_page == 11) + if(info_temp->interface_number == 1 && info_temp->usage_page == dev_ctrl_page) { dev_ctrl = hid_open_path(info_temp->path); dev_ctrl_path = info_temp->path; } - else if(info_temp->interface_number == 3 && info_temp->usage_page == 1) + else if(info_temp->interface_number == 3 && info_temp->usage_page == dev_led_page) { dev_led = hid_open_path(info_temp->path); dev_led_path = info_temp->path; @@ -205,6 +209,7 @@ REGISTER_HID_DETECTOR_IPU("Roccat Kone Aimo", DetectRoccatMouseCon REGISTER_HID_DETECTOR_IPU("Roccat Kone Aimo 16K", DetectRoccatMouseControllers, ROCCAT_VID, ROCCAT_KONE_AIMO_16K_PID, 0, 0x0B, 0 ); REGISTER_HID_DETECTOR_IP ("Roccat Vulcan 120-Series Aimo", DetectRoccatVulcanKeyboardControllers, ROCCAT_VID, ROCCAT_VULCAN_120_AIMO_PID, 1, 11); REGISTER_HID_DETECTOR_IP ("Roccat Vulcan TKL", DetectRoccatVulcanKeyboardControllers, ROCCAT_VID, ROCCAT_VULCAN_TKL_PID, 1, 11); +REGISTER_HID_DETECTOR_IP ("Roccat Magma", DetectRoccatVulcanKeyboardControllers, ROCCAT_VID, ROCCAT_MAGMA_PID, 1, 0xFF01); REGISTER_HID_DETECTOR_IPU("Roccat Horde Aimo", DetectRoccatHordeAimoKeyboardControllers, ROCCAT_VID, ROCCAT_HORDE_AIMO_PID, 1, 0x0B, 0 ); REGISTER_HID_DETECTOR_IPU("Roccat Burst Core", DetectRoccatBurstCoreControllers, ROCCAT_VID, ROCCAT_BURST_CORE_PID, 3, 0xFF01, 1 ); REGISTER_HID_DETECTOR_IPU("Roccat Burst Pro", DetectRoccatBurstProControllers, ROCCAT_VID, ROCCAT_BURST_PRO_PID, 3, 0xFF01, 1 ); diff --git a/Controllers/RoccatController/RoccatVulcanKeyboardController.cpp b/Controllers/RoccatController/RoccatVulcanKeyboardController.cpp index b4f00828..2c97fe12 100644 --- a/Controllers/RoccatController/RoccatVulcanKeyboardController.cpp +++ b/Controllers/RoccatController/RoccatVulcanKeyboardController.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include "LogManager.h" @@ -55,14 +56,48 @@ std::string RoccatVulcanKeyboardController::GetLocation() device_info RoccatVulcanKeyboardController::InitDeviceInfo() { - unsigned char usb_buf[8] = { 0x0F }; - hid_get_feature_report(dev_ctrl, usb_buf, 8); + uint8_t packet_length; + uint8_t report_id; - dev_info.version = std::to_string((int) floor(usb_buf[2] / 100)) + "." + std::to_string(usb_buf[2] % 100); + switch(device_pid) + { + case ROCCAT_MAGMA_PID: + packet_length = 9; + report_id = 0x09; + break; + default: + packet_length = 8; + report_id = 0x0F; + } - dev_info.layout_type = usb_buf[6]; - LOG_DEBUG("[Roccat Vulcan Keyboard]: Detected layout '0x%02X'", usb_buf[6]); + uint8_t* buf = new uint8_t[packet_length]; + memset(buf, 0x00, packet_length); + buf[0] = report_id; + hid_get_feature_report(dev_ctrl, buf, packet_length); + + char version[5]; + snprintf(version, 5, "%d.%02d", buf[2] / 100, buf[2] % 100); + + dev_info.version = version; + + if(device_pid == ROCCAT_MAGMA_PID) + { + /*---------------------------------------------------------*\ + | This device doesn't need a layout, | + | because it doesn't have per-led lighting. | + | Taking us layout as placeholder instead | + \*---------------------------------------------------------*/ + dev_info.layout_type = ROCCAT_VULCAN_LAYOUT_US; + } + else + { + dev_info.layout_type = buf[6]; + } + + LOG_DEBUG("[Roccat Vulcan Keyboard]: Detected layout '0x%02X'", buf[6]); + + delete[] buf; return dev_info; } @@ -73,43 +108,80 @@ device_info RoccatVulcanKeyboardController::GetDeviceInfo() void RoccatVulcanKeyboardController::EnableDirect(bool on_off_switch) { - unsigned char buf[3] = { 0x15, 0x00, on_off_switch }; - hid_send_feature_report(dev_ctrl, buf, 3); + uint8_t* buf; + switch(device_pid) + { + case ROCCAT_MAGMA_PID: + buf = new uint8_t[5] { 0x0E, 0x05, on_off_switch, 0x00, 0x00 }; + hid_send_feature_report(dev_ctrl, buf, 5); + break; + default: + buf = new uint8_t[3] { 0x15, 0x00, on_off_switch }; + hid_send_feature_report(dev_ctrl, buf, 3); + } + delete[] buf; } void RoccatVulcanKeyboardController::SendColors(std::vector colors) { - unsigned char bufs[7][65]; + unsigned short packet_length; + unsigned char column_length; + unsigned char header_length = 0; - for(int p = 0; p < 7; p++) + switch(device_pid) + { + case ROCCAT_MAGMA_PID: + packet_length = 64; + column_length = 5; + break; + default: + packet_length = 436; + column_length = 12; + } + + unsigned char packet_num = ceil((float) packet_length / 64); + + std::vector bufs(packet_num); + + for(int p = 0; p < packet_num; p++) { memset(bufs[p], 0x00, 65); } bufs[0][1] = 0xA1; bufs[0][2] = 0x01; - bufs[0][3] = 0x01; - bufs[0][4] = 0xB4; + + unsigned char header_length_first = (packet_length > 255) ? 4 : 3; + + if(header_length_first == 3) + { + bufs[0][3] = packet_length; + } + else + { + bufs[0][3] = packet_length / 256; + bufs[0][4] = packet_length % 256; + } + + unsigned int data_length_packet = 64 - header_length; for(unsigned int i = 0; i < colors.size(); i++) { - int coloumn = floor(colors[i].value / 12); - int row = colors[i].value % 12; + int coloumn = floor(colors[i].value / column_length); + int row = colors[i].value % column_length; - int offset = coloumn * 36 + row + 4; + int offset = coloumn * 3 * column_length + row + header_length_first; - bufs[offset / 64][offset % 64 + 1] = RGBGetRValue(colors[i].color); + bufs[offset / data_length_packet][offset % data_length_packet + header_length + 1] = RGBGetRValue(colors[i].color); - offset += 12; + offset += column_length; + bufs[offset / data_length_packet][offset % data_length_packet + header_length + 1] = RGBGetGValue(colors[i].color); - bufs[offset / 64][offset % 64 + 1] = RGBGetGValue(colors[i].color); - - offset += 12; - - bufs[offset / 64][offset % 64 + 1] = RGBGetBValue(colors[i].color); + offset += column_length; + bufs[offset / data_length_packet][offset % data_length_packet + header_length + 1] = RGBGetBValue(colors[i].color); } - for(int p = 0; p < 7; p++) + for(int p = 0; p < packet_num; p++) { hid_write(dev_led, bufs[p], 65); } @@ -118,59 +190,112 @@ void RoccatVulcanKeyboardController::SendColors(std::vector colors) AwaitResponse(20); } + void RoccatVulcanKeyboardController::SendMode(unsigned int mode, unsigned int speed, unsigned int brightness, std::vector colors) { - if(speed == 0) speed = ROCCAT_VULCAN_SPEED_DEFAULT; - if(brightness == 0) brightness = ROCCAT_VULCAN_BRIGHTNESS_DEFAULT; + if(speed == 0) speed = ROCCAT_VULCAN_SPEED_DEFAULT; + if(brightness == 0) brightness = ROCCAT_VULCAN_BRIGHTNESS_DEFAULT; - unsigned char buf[443]; + unsigned short packet_length; + unsigned char protocol_version; + unsigned char column_length; - memset(buf, 0x00, 443); - - buf[0] = 0x0D; - buf[1] = 0xBB; - buf[2] = 0x01; - buf[3] = 0x00; - buf[4] = mode; - buf[5] = speed; - buf[6] = 0x05; - buf[7] = brightness; - buf[8] = 0x00; - - if(mode == ROCCAT_VULCAN_MODE_STATIC) + switch(device_pid) { + case ROCCAT_MAGMA_PID: + protocol_version = 2; + packet_length = 26; + column_length = 5; + break; + default: + protocol_version = 1; + packet_length = 443; + column_length = 12; + } - for(unsigned int i = 0; i < colors.size(); i++) - { - int coloumn = floor(colors[i].value / 12); - int row = colors[i].value % 12; - int offset = coloumn * 36 + row + 9; + uint8_t* buf = new uint8_t[packet_length]; + memset(buf, 0x00, packet_length); - buf[offset] = RGBGetRValue(colors[i].color); + unsigned char header_length = (packet_length > 255) ? 2 : 1; - offset += 12; + buf[0] = (protocol_version == 1) ? 0x0D : 0x11; - buf[offset] = RGBGetGValue(colors[i].color); + if(header_length == 1) + { + buf[1] = packet_length; + } + else + { + buf[1] = packet_length % 256; + buf[2] = packet_length / 256; + } - offset += 12; + unsigned char offset = header_length + 1; - buf[offset] = RGBGetBValue(colors[i].color); - } + buf[0 + offset] = 0x00; + buf[1 + offset] = mode; + buf[2 + offset] = speed; + + if(protocol_version == 1) + { + buf[3 + offset] = 0x00; + buf[4 + offset] = brightness; + buf[5 + offset] = 0x00; + } + else + { + buf[3 + offset] = brightness; + buf[4 + offset] = 0x00; + buf[5 + offset] = 0x00; + } + + + for(unsigned int i = 0; i < colors.size(); i++) + { + int coloumn = floor(colors[i].value / column_length); + int row = colors[i].value % column_length; + + int offset = coloumn * 3 * column_length + row + 9; + + buf[offset] = RGBGetRValue(colors[i].color); + + offset += column_length; + + buf[offset] = RGBGetGValue(colors[i].color); + + offset += column_length; + + buf[offset] = RGBGetBValue(colors[i].color); } unsigned short total = 0; - for(int i = 0; i < 441; i++) total += buf[i]; + for(int i = 0; i < packet_length - 2; i++) total += buf[i]; - buf[441] = total & 0xFF; - buf[442] = total >> 8; + buf[packet_length - 2] = total & 0xFF; + buf[packet_length - 1] = total >> 8; - hid_send_feature_report(dev_ctrl, buf, 443); + int ret = hid_send_feature_report(dev_ctrl, buf, packet_length); + + delete[] buf; } void RoccatVulcanKeyboardController::WaitUntilReady() { - unsigned char buf[3] = { 0x04, 0x00, 0x00 }; + unsigned short packet_length; + + switch(device_pid) + { + case ROCCAT_MAGMA_PID: + packet_length = 4; + break; + default: + packet_length = 3; + } + + uint8_t* buf = new uint8_t[packet_length]; + + buf[0] = 0x04; for(unsigned char i = 0; buf[1] != 1 && i < 100; i++) { @@ -179,8 +304,10 @@ void RoccatVulcanKeyboardController::WaitUntilReady() std::this_thread::sleep_for(std::chrono::milliseconds(25)); } - hid_get_feature_report(dev_ctrl, buf, 3); + hid_get_feature_report(dev_ctrl, buf, packet_length); } + + delete[] buf; } void RoccatVulcanKeyboardController::AwaitResponse(int ms) diff --git a/Controllers/RoccatController/RoccatVulcanKeyboardController.h b/Controllers/RoccatController/RoccatVulcanKeyboardController.h index 38d37714..a9ea63b9 100644 --- a/Controllers/RoccatController/RoccatVulcanKeyboardController.h +++ b/Controllers/RoccatController/RoccatVulcanKeyboardController.h @@ -17,8 +17,9 @@ enum { - ROCCAT_VULCAN_120_AIMO = 0x3098, - ROCCAT_VULCAN_TKL = 0x2FEE, + ROCCAT_VULCAN_120_AIMO_PID = 0x3098, + ROCCAT_VULCAN_TKL_PID = 0x2FEE, + ROCCAT_MAGMA_PID = 0x3124, }; enum diff --git a/Controllers/RoccatController/RoccatVulcanKeyboardLayouts.h b/Controllers/RoccatController/RoccatVulcanKeyboardLayouts.h index 78b9b1a7..f914ff51 100644 --- a/Controllers/RoccatController/RoccatVulcanKeyboardLayouts.h +++ b/Controllers/RoccatController/RoccatVulcanKeyboardLayouts.h @@ -39,7 +39,8 @@ struct layout_info std::vector led_names; }; -static unsigned int ROCCAT_VULCAN_120_AIMO_LAYOUT_KEYS_104[6][24] = { +static unsigned int ROCCAT_VULCAN_120_AIMO_LAYOUT_KEYS_104[6][24] = +{ { 0, NA, 8, 14, 19, 24, NA, 34, 39, 44, 49, 55, 61, 66, 70, NA, 74, 78, 83, NA, NA, NA, NA, NA }, { 1, 6, 9, 15, 20, 25, 29, 35, 40, 45, 50, 56, 62, 67, NA, NA, 75, 79, 84, NA, 87, 92, 96, 101 }, { 2, NA, 10, 16, 21, 26, 30, 36, 41, 46, 51, 57, 63, 68, 71, NA, 76, 80, 85, NA, 88, 93, 97, 102 }, @@ -49,7 +50,8 @@ static unsigned int ROCCAT_VULCAN_120_AIMO_LAYOUT_KEYS_104[6][24] = { }; -static unsigned int ROCCAT_VULCAN_120_AIMO_LAYOUT_KEYS_105[6][24] = { +static unsigned int ROCCAT_VULCAN_120_AIMO_LAYOUT_KEYS_105[6][24] = +{ { 0, NA, 9, 15, 20, 25, NA, 35, 40, 45, 50, 56, 62, 67, 72, NA, 75, 79, 84, NA, NA, NA, NA, NA }, { 1, 6, 10, 16, 21, 26, 30, 36, 41, 46, 51, 57, 63, 68, NA, NA, 76, 80, 85, NA, 88, 93, 97, 102 }, { 2, NA, 11, 17, 22, 27, 31, 37, 42, 47, 52, 58, 64, 69, NA, NA, 77, 81, 86, NA, 89, 94, 98, 103 }, @@ -58,7 +60,8 @@ static unsigned int ROCCAT_VULCAN_120_AIMO_LAYOUT_KEYS_105[6][24] = { { 5, 8, 14, NA, NA, NA, 34, NA, NA, NA, 55, 61, 66, NA, 74, NA, 78, 83, 87, NA, 92, NA, 101, NA } }; -static unsigned int ROCCAT_VULCAN_TKL_LAYOUT_KEYS_104[6][19] = { +static unsigned int ROCCAT_VULCAN_TKL_LAYOUT_KEYS_104[6][19] = +{ { 0, NA, 8, 14, 19, 24, NA, 34, 39, 44, 49, 55, 61, 66, 70, NA, 74, NA, NA }, { 1, 6, 9, 15, 20, 25, 29, 35, 40, 45, 50, 56, 62, 67, NA, NA, 75, 78, 82 }, { 2, NA, 10, 16, 21, 26, 30, 36, 41, 46, 51, 57, 63, 68, 71, NA, 76, 79, 83 }, @@ -68,7 +71,8 @@ static unsigned int ROCCAT_VULCAN_TKL_LAYOUT_KEYS_104[6][19] = { }; -static unsigned int ROCCAT_VULCAN_TKL_LAYOUT_KEYS_105[6][19] = { +static unsigned int ROCCAT_VULCAN_TKL_LAYOUT_KEYS_105[6][19] = +{ { 0, NA, 9, 15, 20, 25, NA, 35, 40, 45, 50, 56, 62, 67, 72, NA, 75, NA, NA }, { 1, 6, 10, 16, 21, 26, 30, 36, 41, 46, 51, 57, 63, 68, NA, NA, 76, 79, 83 }, { 2, NA, 11, 17, 22, 27, 31, 37, 42, 47, 52, 58, 64, 69, NA, NA, 77, 80, 84 }, @@ -77,6 +81,11 @@ static unsigned int ROCCAT_VULCAN_TKL_LAYOUT_KEYS_105[6][19] = { { 5, 8, 14, NA, NA, NA, 34, NA, NA, NA, 55, 61, 66, NA, 74, NA, 78, 82, 85 } }; +static unsigned int ROCCAT_MAGMA_LAYOUT_KEYS[1][5] = +{ + { 0, 1, 2, 3, 4 }, +}; + static std::map RoccatVulcan120AimoLayouts = { { @@ -584,3 +593,23 @@ static std::map RoccatVulcanTKLLayouts = } }, }; + +static std::map RoccatMagmaLayouts = +{ + { + ROCCAT_VULCAN_LAYOUT_US, + { + *ROCCAT_MAGMA_LAYOUT_KEYS, + 5, + 1, + 5, + { + { "Keyboard LED 1", 0x00 }, + { "Keyboard LED 2", 0x01 }, + { "Keyboard LED 3", 0x02 }, + { "Keyboard LED 4", 0x03 }, + { "Keyboard LED 5", 0x04 }, + } + } + }, +};