diff --git a/Controllers/MSILaptopController/MSILaptopController.cpp b/Controllers/MSILaptopController/MSILaptopController.cpp new file mode 100644 index 000000000..0abd6e5be --- /dev/null +++ b/Controllers/MSILaptopController/MSILaptopController.cpp @@ -0,0 +1,89 @@ +/*---------------------------------------------------------*\ +| MSILaptopController.cpp | +| | +| Driver for MSI laptop SteelSeries KLC/ALC RGB devices | +| | +| This file is part of the OpenRGB project | +| SPDX-License-Identifier: GPL-2.0-or-later | +\*---------------------------------------------------------*/ + +#include +#include "MSILaptopController.h" +#include "StringUtils.h" + +#define MSI_LAPTOP_REPORT_ID 0x00 +#define MSI_LAPTOP_COMMAND 0x0C +#define MSI_LAPTOP_KLC_PACKET_ID 0x66 +#define MSI_LAPTOP_ALC_PACKET_ID 0x06 +#define MSI_LAPTOP_PACKET_SIZE 525 +#define MSI_LAPTOP_PAYLOAD_OFFSET 5 + +MSILaptopController::MSILaptopController(hid_device* dev_handle, const char* path, std::string dev_name, msi_laptop_device device_type) +{ + dev = dev_handle; + location = path; + name = dev_name; + type = device_type; +} + +MSILaptopController::~MSILaptopController() +{ + hid_close(dev); +} + +std::string MSILaptopController::GetDeviceLocation() +{ + return("HID: " + location); +} + +std::string MSILaptopController::GetDeviceName() +{ + return(name); +} + +std::string MSILaptopController::GetSerialString() +{ + wchar_t serial_string[128]; + int ret = hid_get_serial_number_string(dev, serial_string, 128); + + if(ret != 0) + { + return(""); + } + + return(StringUtils::wstring_to_string(serial_string)); +} + +msi_laptop_device MSILaptopController::GetDeviceType() +{ + return(type); +} + +void MSILaptopController::SetLEDs(std::vector leds, std::vector colors) +{ + unsigned char buf[MSI_LAPTOP_PACKET_SIZE]; + unsigned int led_count = (leds.size() < colors.size()) ? leds.size() : colors.size(); + + memset(buf, 0x00, sizeof(buf)); + + buf[0x00] = MSI_LAPTOP_REPORT_ID; + buf[0x01] = MSI_LAPTOP_COMMAND; + buf[0x03] = (type == MSI_LAPTOP_KLC) ? MSI_LAPTOP_KLC_PACKET_ID : MSI_LAPTOP_ALC_PACKET_ID; + + for(unsigned int led_idx = 0; led_idx < led_count; led_idx++) + { + unsigned int offset = MSI_LAPTOP_PAYLOAD_OFFSET + (led_idx * 4); + + if((offset + 3) >= sizeof(buf)) + { + break; + } + + buf[offset + 0] = leds[led_idx].value; + buf[offset + 1] = RGBGetRValue(colors[led_idx]); + buf[offset + 2] = RGBGetGValue(colors[led_idx]); + buf[offset + 3] = RGBGetBValue(colors[led_idx]); + } + + hid_send_feature_report(dev, buf, sizeof(buf)); +} diff --git a/Controllers/MSILaptopController/MSILaptopController.h b/Controllers/MSILaptopController/MSILaptopController.h new file mode 100644 index 000000000..d30480394 --- /dev/null +++ b/Controllers/MSILaptopController/MSILaptopController.h @@ -0,0 +1,67 @@ +/*---------------------------------------------------------*\ +| MSILaptopController.h | +| | +| Driver for MSI laptop SteelSeries KLC/ALC RGB devices | +| | +| This file is part of the OpenRGB project | +| SPDX-License-Identifier: GPL-2.0-or-later | +\*---------------------------------------------------------*/ + +#pragma once + +#include +#include +#include +#include "RGBController.h" + +typedef enum +{ + MSI_LAPTOP_KLC, + MSI_LAPTOP_ALC, + +} msi_laptop_device; + +typedef struct +{ + const char* name; + unsigned char id; + +} msi_laptop_led; + +struct MSILaptopModel +{ + const char* sys_vendor; + const char* product_name; + + /* Keyboard layout */ + const msi_laptop_led* klc_leds; + unsigned int klc_leds_count; + unsigned int klc_matrix_height; + unsigned int klc_matrix_width; + const unsigned int* klc_matrix_map; + + /* Lightbar layout */ + const msi_laptop_led* alc_leds; + unsigned int alc_leds_count; + unsigned int alc_lightbar_leds; +}; + +class MSILaptopController +{ +public: + MSILaptopController(hid_device* dev_handle, const char* path, std::string dev_name, msi_laptop_device device_type); + ~MSILaptopController(); + + std::string GetDeviceLocation(); + std::string GetDeviceName(); + std::string GetSerialString(); + msi_laptop_device GetDeviceType(); + + void SetLEDs(std::vector leds, std::vector colors); + +private: + hid_device* dev; + std::string location; + std::string name; + msi_laptop_device type; +}; diff --git a/Controllers/MSILaptopController/MSILaptopControllerDetect.cpp b/Controllers/MSILaptopController/MSILaptopControllerDetect.cpp new file mode 100644 index 000000000..d37ca510a --- /dev/null +++ b/Controllers/MSILaptopController/MSILaptopControllerDetect.cpp @@ -0,0 +1,225 @@ +/*---------------------------------------------------------*\ +| MSILaptopControllerDetect.cpp | +| | +| Detector for MSI laptop SteelSeries RGB devices | +| | +| This file is part of the OpenRGB project | +| SPDX-License-Identifier: GPL-2.0-or-later | +\*---------------------------------------------------------*/ + +#include "Detector.h" +#include "RGBControllerKeyNames.h" +#include "RGBController_MSILaptop.h" +#include "MSILaptopController.h" +#include "dmiinfo.h" + +#define MSI_LAPTOP_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +#define STEELSERIES_VID 0x1038 +#define STEELSERIES_MSI_RAIDER_A18_KLC_PID 0x1122 +#define STEELSERIES_MSI_RAIDER_A18_ALC_PID 0x1161 + +#define NA 0xFFFFFFFF +#define MSI_LAPTOP_KLC_MATRIX_HEIGHT 6 +#define MSI_LAPTOP_KLC_MATRIX_WIDTH 23 +#define MSI_LAPTOP_ALC_LIGHTBAR_LEDS 5 + +static const msi_laptop_led msi_raider_a18_klc_leds[] = +{ + { KEY_EN_A, 0x04 }, + { KEY_EN_B, 0x05 }, + { KEY_EN_C, 0x06 }, + { KEY_EN_D, 0x07 }, + { KEY_EN_E, 0x08 }, + { KEY_EN_F, 0x09 }, + { KEY_EN_G, 0x0A }, + { KEY_EN_H, 0x0B }, + { KEY_EN_I, 0x0C }, + { KEY_EN_J, 0x0D }, + { KEY_EN_K, 0x0E }, + { KEY_EN_L, 0x0F }, + { KEY_EN_M, 0x10 }, + { KEY_EN_N, 0x11 }, + { KEY_EN_O, 0x12 }, + { KEY_EN_P, 0x13 }, + { KEY_EN_Q, 0x14 }, + { KEY_EN_R, 0x15 }, + { KEY_EN_S, 0x16 }, + { KEY_EN_T, 0x17 }, + { KEY_EN_U, 0x18 }, + { KEY_EN_V, 0x19 }, + { KEY_EN_W, 0x1A }, + { KEY_EN_X, 0x1B }, + { KEY_EN_Y, 0x1C }, + { KEY_EN_Z, 0x1D }, + { KEY_EN_1, 0x1E }, + { KEY_EN_2, 0x1F }, + { KEY_EN_3, 0x20 }, + { KEY_EN_4, 0x21 }, + { KEY_EN_5, 0x22 }, + { KEY_EN_6, 0x23 }, + { KEY_EN_7, 0x24 }, + { KEY_EN_8, 0x25 }, + { KEY_EN_9, 0x26 }, + { KEY_EN_0, 0x27 }, + { KEY_EN_ESCAPE, 0x29 }, + { KEY_EN_TAB, 0x2B }, + { KEY_EN_SPACE, 0x2C }, + { KEY_EN_MINUS, 0x2D }, + { KEY_EN_EQUALS, 0x2E }, + { KEY_EN_LEFT_BRACKET, 0x2F }, + { KEY_EN_RIGHT_BRACKET, 0x30 }, + { KEY_EN_SEMICOLON, 0x33 }, + { KEY_EN_QUOTE, 0x34 }, + { KEY_EN_BACK_TICK, 0x35 }, + { KEY_EN_COMMA, 0x36 }, + { KEY_EN_PERIOD, 0x37 }, + { KEY_EN_FORWARD_SLASH, 0x38 }, + { KEY_EN_CAPS_LOCK, 0x39 }, + { KEY_EN_F1, 0x3A }, + { KEY_EN_F2, 0x3B }, + { KEY_EN_F3, 0x3C }, + { KEY_EN_F4, 0x3D }, + { KEY_EN_F5, 0x3E }, + { KEY_EN_F6, 0x3F }, + { KEY_EN_F7, 0x40 }, + { KEY_EN_F8, 0x41 }, + { KEY_EN_F9, 0x42 }, + { KEY_EN_F10, 0x43 }, + { KEY_EN_F11, 0x44 }, + { KEY_EN_F12, 0x45 }, + { KEY_EN_PRINT_SCREEN, 0x46 }, + { KEY_EN_SCROLL_LOCK, 0x47 }, + { KEY_EN_INSERT, 0x49 }, + { "Home/Page Up", 0x4B }, + { KEY_EN_DELETE, 0x4C }, + { KEY_EN_PAGE_DOWN, 0x4E }, + { KEY_EN_RIGHT_ARROW, 0x4F }, + { KEY_EN_LEFT_ARROW, 0x50 }, + { KEY_EN_DOWN_ARROW, 0x51 }, + { KEY_EN_UP_ARROW, 0x52 }, + { KEY_EN_NUMPAD_LOCK, 0x53 }, + { KEY_EN_NUMPAD_DIVIDE, 0x54 }, + { KEY_EN_NUMPAD_TIMES, 0x55 }, + { KEY_EN_NUMPAD_MINUS, 0x56 }, + { KEY_EN_NUMPAD_PLUS, 0x57 }, + { KEY_EN_NUMPAD_ENTER, 0x58 }, + { KEY_EN_NUMPAD_1, 0x59 }, + { KEY_EN_NUMPAD_2, 0x5A }, + { KEY_EN_NUMPAD_3, 0x5B }, + { KEY_EN_NUMPAD_4, 0x5C }, + { KEY_EN_NUMPAD_5, 0x5D }, + { KEY_EN_NUMPAD_6, 0x5E }, + { KEY_EN_NUMPAD_7, 0x5F }, + { KEY_EN_NUMPAD_8, 0x60 }, + { KEY_EN_NUMPAD_9, 0x61 }, + { KEY_EN_NUMPAD_0, 0x62 }, + { KEY_EN_NUMPAD_PERIOD, 0x63 }, + { KEY_EN_POWER, 0x66 }, + { KEY_EN_LEFT_CONTROL, 0xE0 }, + { KEY_EN_LEFT_SHIFT, 0xE1 }, + { KEY_EN_LEFT_ALT, 0xE2 }, + { KEY_EN_LEFT_WINDOWS, 0xE3 }, + { KEY_EN_RIGHT_WINDOWS, 0xE4 }, + { KEY_EN_RIGHT_FUNCTION, 0xF0 }, + { KEY_EN_ANSI_ENTER, 0x28 }, + { KEY_EN_BACKSPACE, 0x2A }, + { KEY_EN_BACK_SLASH, 0x31 }, + { KEY_EN_ISO_BACK_SLASH, 0x64 }, + { KEY_EN_RIGHT_SHIFT, 0xE5 }, + { KEY_EN_RIGHT_ALT, 0xE6 }, +}; + +static unsigned int msi_raider_a18_klc_matrix_map[MSI_LAPTOP_KLC_MATRIX_HEIGHT][MSI_LAPTOP_KLC_MATRIX_WIDTH] = +{ + { 36, NA, 50, 51, 52, 53, NA, 54, 55, 56, 57, NA, 58, 59, 60, 61, 62, 63, NA, NA, NA, NA, 89 }, + { 45, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 39, 40, 97, NA, 64, 65, NA, 72, 73, 74, 75, NA }, + { 37, 16, 22, 4, 17, 19, 24, 20, 8, 14, 15, 41, 42, 98, NA, 66, 67, NA, 84, 85, 86, 76, NA }, + { 49, 0, 18, 3, 5, 6, 7, 9, 10, 11, 43, 44, 96, NA, NA, NA, NA, NA, 81, 82, 83, NA, NA }, + { 91, 25, 23, 2, 21, 1, 13, 12, 46, 47, 48,100, NA, NA, NA, NA, 71, NA, 78, 79, 80, 77, NA }, + { 90, 93, 92, NA, NA, NA, 38, NA, NA, NA,101, 94, 95, NA, 69, 70, 68, NA, 87, NA, 88, NA, NA }, +}; + +static const msi_laptop_led msi_raider_a18_alc_leds[] = +{ + { "Lightbar 1", 0x00 }, + { "Lightbar 2", 0x01 }, + { "Lightbar 3", 0x02 }, + { "Lightbar 4", 0x03 }, + { "Lightbar 5", 0x04 }, + { "Logo", 0x05 }, +}; + +static const MSILaptopModel msi_laptop_models[] = +{ + { + "Micro-Star International Co., Ltd.", + "Raider A18 HX A9WJG", + + /* Keyboard layout */ + msi_raider_a18_klc_leds, + MSI_LAPTOP_ARRAY_SIZE(msi_raider_a18_klc_leds), + MSI_LAPTOP_KLC_MATRIX_HEIGHT, + MSI_LAPTOP_KLC_MATRIX_WIDTH, + (const unsigned int*)msi_raider_a18_klc_matrix_map, + + /* Lightbar layout */ + msi_raider_a18_alc_leds, + MSI_LAPTOP_ARRAY_SIZE(msi_raider_a18_alc_leds), + MSI_LAPTOP_ALC_LIGHTBAR_LEDS, + }, +}; + +static const MSILaptopModel* GetMSILaptopModelDMI() +{ + DMIInfo dmi; + + for(unsigned int i = 0; i < MSI_LAPTOP_ARRAY_SIZE(msi_laptop_models); i++) + { + if((dmi.getManufacturer() == msi_laptop_models[i].sys_vendor) && + (dmi.getProductName() == msi_laptop_models[i].product_name)) + { + return &msi_laptop_models[i]; + } + } + + return nullptr; +} + +void DetectMSILaptop(hid_device_info* info, const std::string& name) +{ + const MSILaptopModel* model = GetMSILaptopModelDMI(); + if(!model) + { + return; + } + + msi_laptop_device device_type; + + if(info->product_id == STEELSERIES_MSI_RAIDER_A18_KLC_PID) + { + device_type = MSI_LAPTOP_KLC; + } + else if(info->product_id == STEELSERIES_MSI_RAIDER_A18_ALC_PID) + { + device_type = MSI_LAPTOP_ALC; + } + else + { + return; + } + + hid_device* dev = hid_open_path(info->path); + + if(dev) + { + MSILaptopController* controller = new MSILaptopController(dev, info->path, name, device_type); + RGBController_MSILaptop* rgb_controller = new RGBController_MSILaptop(controller, model); + + ResourceManager::get()->RegisterRGBController(rgb_controller); + } +} + +REGISTER_HID_DETECTOR_I("MSI Laptop Keyboard", DetectMSILaptop, STEELSERIES_VID, STEELSERIES_MSI_RAIDER_A18_KLC_PID, 0); +REGISTER_HID_DETECTOR_I("MSI Laptop Lightbar", DetectMSILaptop, STEELSERIES_VID, STEELSERIES_MSI_RAIDER_A18_ALC_PID, 0); + diff --git a/Controllers/MSILaptopController/RGBController_MSILaptop.cpp b/Controllers/MSILaptopController/RGBController_MSILaptop.cpp new file mode 100644 index 000000000..e6e62769c --- /dev/null +++ b/Controllers/MSILaptopController/RGBController_MSILaptop.cpp @@ -0,0 +1,141 @@ +/*---------------------------------------------------------*\ +| RGBController_MSILaptop.cpp | +| | +| RGBController for MSI laptop SteelSeries RGB devices | +| | +| This file is part of the OpenRGB project | +| SPDX-License-Identifier: GPL-2.0-or-later | +\*---------------------------------------------------------*/ + +#include "RGBControllerKeyNames.h" +#include "RGBController_MSILaptop.h" + +#define NA 0xFFFFFFFF +/**------------------------------------------------------------------*\ + @name MSI Laptop SteelSeries RGB + @category Keyboard + @type USB + @save :x: + @direct :white_check_mark: + @effects :x: + @detectors DetectMSILaptop + @comment +\*-------------------------------------------------------------------*/ + +RGBController_MSILaptop::RGBController_MSILaptop(MSILaptopController* controller_ptr, const MSILaptopModel* model_ptr) +{ + controller = controller_ptr; + model = model_ptr; + + name = controller->GetDeviceName(); + vendor = "SteelSeries"; + description = std::string(model->sys_vendor) + " " + std::string(model->product_name) + " RGB Device"; + location = controller->GetDeviceLocation(); + serial = controller->GetSerialString(); + type = (controller->GetDeviceType() == MSI_LAPTOP_KLC) ? DEVICE_TYPE_KEYBOARD : DEVICE_TYPE_LEDSTRIP; + + mode Direct; + Direct.name = "Direct"; + Direct.flags = MODE_FLAG_HAS_PER_LED_COLOR; + Direct.color_mode = MODE_COLORS_PER_LED; + modes.push_back(Direct); + + SetupZones(); +} + +RGBController_MSILaptop::~RGBController_MSILaptop() +{ + for(unsigned int zone_idx = 0; zone_idx < zones.size(); zone_idx++) + { + if(zones[zone_idx].matrix_map != NULL) + { + delete zones[zone_idx].matrix_map; + } + } + + delete controller; +} + +void RGBController_MSILaptop::SetupZones() +{ + if(controller->GetDeviceType() == MSI_LAPTOP_KLC) + { + zone keyboard_zone; + + keyboard_zone.name = ZONE_EN_KEYBOARD; + keyboard_zone.type = ZONE_TYPE_MATRIX; + keyboard_zone.leds_min = model->klc_leds_count; + keyboard_zone.leds_max = model->klc_leds_count; + keyboard_zone.leds_count = model->klc_leds_count; + keyboard_zone.matrix_map = new matrix_map_type; + keyboard_zone.matrix_map->height = model->klc_matrix_height; + keyboard_zone.matrix_map->width = model->klc_matrix_width; + keyboard_zone.matrix_map->map = (unsigned int *)model->klc_matrix_map; + + zones.push_back(keyboard_zone); + + for(unsigned int led_idx = 0; led_idx < model->klc_leds_count; led_idx++) + { + led new_led; + new_led.name = model->klc_leds[led_idx].name; + new_led.value = model->klc_leds[led_idx].id; + leds.push_back(new_led); + } + } + else + { + zone lightbar_zone; + lightbar_zone.name = "Lightbar"; + lightbar_zone.type = ZONE_TYPE_LINEAR; + lightbar_zone.start_idx = 0; + lightbar_zone.leds_min = model->alc_lightbar_leds; + lightbar_zone.leds_max = model->alc_lightbar_leds; + lightbar_zone.leds_count = model->alc_lightbar_leds; + lightbar_zone.matrix_map = NULL; + zones.push_back(lightbar_zone); + + zone logo_zone; + logo_zone.name = "Logo"; + logo_zone.type = ZONE_TYPE_SINGLE; + logo_zone.start_idx = model->alc_lightbar_leds; + logo_zone.leds_min = 1; + logo_zone.leds_max = 1; + logo_zone.leds_count = 1; + logo_zone.matrix_map = NULL; + zones.push_back(logo_zone); + + for(unsigned int led_idx = 0; led_idx < model->alc_leds_count; led_idx++) + { + led new_led; + new_led.name = model->alc_leds[led_idx].name; + new_led.value = model->alc_leds[led_idx].id; + leds.push_back(new_led); + } + } + + SetupColors(); +} + +void RGBController_MSILaptop::ResizeZone(int /*zone*/, int /*new_size*/) +{ +} + +void RGBController_MSILaptop::DeviceUpdateLEDs() +{ + controller->SetLEDs(leds, colors); +} + +void RGBController_MSILaptop::UpdateZoneLEDs(int /*zone*/) +{ + DeviceUpdateLEDs(); +} + +void RGBController_MSILaptop::UpdateSingleLED(int /*led*/) +{ + DeviceUpdateLEDs(); +} + +void RGBController_MSILaptop::DeviceUpdateMode() +{ + DeviceUpdateLEDs(); +} diff --git a/Controllers/MSILaptopController/RGBController_MSILaptop.h b/Controllers/MSILaptopController/RGBController_MSILaptop.h new file mode 100644 index 000000000..c7004a540 --- /dev/null +++ b/Controllers/MSILaptopController/RGBController_MSILaptop.h @@ -0,0 +1,34 @@ +/*---------------------------------------------------------*\ +| RGBController_MSILaptop.h | +| | +| RGBController for MSI laptop SteelSeries RGB devices | +| | +| This file is part of the OpenRGB project | +| SPDX-License-Identifier: GPL-2.0-or-later | +\*---------------------------------------------------------*/ + +#pragma once + +#include "RGBController.h" +#include "MSILaptopController.h" + +class RGBController_MSILaptop : public RGBController +{ +public: + RGBController_MSILaptop(MSILaptopController* controller_ptr, const MSILaptopModel* model_ptr); + ~RGBController_MSILaptop(); + + void SetupZones(); + + void ResizeZone(int zone, int new_size); + + void DeviceUpdateLEDs(); + void UpdateZoneLEDs(int zone); + void UpdateSingleLED(int led); + + void DeviceUpdateMode(); + +private: + MSILaptopController* controller; + const MSILaptopModel* model; +};