From 07aa2410584b2e593bc879468799de6404290539 Mon Sep 17 00:00:00 2001 From: Kevin Pearson Date: Fri, 26 May 2017 10:11:16 -0400 Subject: [PATCH 1/2] Move handling of NativeEffectPart::all to library Raw use of 0xFF as the effectpart byte is device-undefined and was implemented as a wrapper around mutliplle native effects calls as well as handling the indicators. Logic was previously implemented in the application, allowing for library use in this particular case to have undefined behavior. Signed-off-by: Kevin Pearson --- src/classes/Keyboard.cpp | 35 +++++++++++++++++++++++++++++---- src/main.cpp | 42 ++++++---------------------------------- 2 files changed, 37 insertions(+), 40 deletions(-) diff --git a/src/classes/Keyboard.cpp b/src/classes/Keyboard.cpp index 472e45a..51be1bd 100644 --- a/src/classes/Keyboard.cpp +++ b/src/classes/Keyboard.cpp @@ -692,7 +692,34 @@ bool LedKeyboard::setStartupMode(StartupMode startupMode) { bool LedKeyboard::setNativeEffect(NativeEffect effect, NativeEffectPart part, uint8_t speed, Color color) { uint8_t protocolByte = 0; - + + // NativeEffectPart::all is not in the device protocol, but an alias for both keys and logo, plus indicators + if (part == LedKeyboard::NativeEffectPart::all) { + switch (effect) { + case LedKeyboard::NativeEffect::color: + if (! setGroupKeys(LedKeyboard::KeyGroup::indicators, color)) return false; + if (! commit()) return false; + break; + case LedKeyboard::NativeEffect::breathing: + if (! setGroupKeys(LedKeyboard::KeyGroup::indicators, color)) return false;; + if (! commit()) return false;; + break; + case LedKeyboard::NativeEffect::cycle: + case LedKeyboard::NativeEffect::hwave: + case LedKeyboard::NativeEffect::vwave: + case LedKeyboard::NativeEffect::cwave: + if (! setGroupKeys( + LedKeyboard::KeyGroup::indicators, + LedKeyboard::Color({0xff, 0xff, 0xff})) + ) return false; + if (! commit()) return false; + break; + } + return ( + setNativeEffect(effect, LedKeyboard::NativeEffectPart::keys, speed, color) && + setNativeEffect(effect, LedKeyboard::NativeEffectPart::logo, speed, color)); + } + switch (currentDevice.model) { case KeyboardModel::g213: protocolByte = 0x0c; @@ -710,11 +737,11 @@ bool LedKeyboard::setNativeEffect(NativeEffect effect, NativeEffectPart part, ui default: return false; } - + byte_buffer_t data; - + switch (effect) { - + case NativeEffect::color: data = { 0x11, 0xff, protocolByte, 0x3c, (uint8_t)part, 0x01, color.red, color.green, color.blue, 0x02 }; break; diff --git a/src/main.cpp b/src/main.cpp index 06c76db..467589a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -110,7 +110,7 @@ int setFX(LedKeyboard &kbd, std::string arg2, std::string arg3, std::string arg4 LedKeyboard::Color color; if (! utils::parseNativeEffect(arg2, effect)) return 1; if (! utils::parseNativeEffectPart(arg3, effectPart)) return 1; - + switch (effect) { case LedKeyboard::NativeEffect::color: if (! utils::parseColor(arg4, color)) return 1; @@ -127,42 +127,12 @@ int setFX(LedKeyboard &kbd, std::string arg2, std::string arg3, std::string arg4 if (! utils::parseSpeed(arg4, speed)) return 1; break; } - + if (! kbd.open()) return 1; - - int retval = 0; - - switch (effectPart) { - case LedKeyboard::NativeEffectPart::all: - switch (effect) { - case LedKeyboard::NativeEffect::color: - if (! kbd.setGroupKeys(LedKeyboard::KeyGroup::indicators, color)) retval = 1; - if (! kbd.commit()) retval = 1; - break; - case LedKeyboard::NativeEffect::breathing: - if (! kbd.setGroupKeys(LedKeyboard::KeyGroup::indicators, color)) retval = 1; - if (! kbd.commit()) retval = 1; - break; - case LedKeyboard::NativeEffect::cycle: - case LedKeyboard::NativeEffect::hwave: - case LedKeyboard::NativeEffect::vwave: - case LedKeyboard::NativeEffect::cwave: - if (! kbd.setGroupKeys( - LedKeyboard::KeyGroup::indicators, - LedKeyboard::Color({0xff, 0xff, 0xff})) - ) retval = 1; - if (! kbd.commit()) retval = 1; - break; - } - if (! kbd.setNativeEffect(effect, LedKeyboard::NativeEffectPart::keys, speed, color)) retval = 1; - if (! kbd.setNativeEffect(effect, LedKeyboard::NativeEffectPart::logo, speed, color)) retval = 1; - break; - default: - if (! kbd.setNativeEffect(effect, effectPart, speed, color)) retval = 1; - break; - } - - return retval; + + if (! kbd.setNativeEffect(effect, effectPart, speed, color)) return 1; + + return 0; } From 7d409924896f37b464442313f0603b2cba92e883 Mon Sep 17 00:00:00 2001 From: MatMoul Date: Fri, 26 May 2017 17:26:43 +0200 Subject: [PATCH 2/2] patch multiple keyboards lost support --- src/classes/Keyboard.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/classes/Keyboard.cpp b/src/classes/Keyboard.cpp index 51be1bd..0a20607 100644 --- a/src/classes/Keyboard.cpp +++ b/src/classes/Keyboard.cpp @@ -810,7 +810,7 @@ bool LedKeyboard::setNativeEffect(NativeEffect effect, NativeEffectPart part, ui bool LedKeyboard::sendDataInternal(byte_buffer_t &data) { if (data.size() > 0) { #if defined(hidapi) - if (! open()) return false; + if (! open(currentDevice.vendorID, currentDevice.productID, currentDevice.serialNumber)) return false; data.insert(data.begin(), 0x00); if (hid_write(m_hidHandle, const_cast(data.data()), data.size()) < 0) { std::cout<<"Error: Can not write to hidraw, try with the libusb version"<