Add ChangeHostAction IPC interface

This commit is contained in:
pixl 2023-04-27 13:47:14 -04:00
parent 56dee076ea
commit 9af666f863
No known key found for this signature in database
GPG Key ID: 1866C148CD593B6E
7 changed files with 54 additions and 31 deletions

View File

@ -58,31 +58,26 @@ std::shared_ptr<Action> _makeAction(
{ {
if(name == ChangeDPI::interface_name) { if(name == ChangeDPI::interface_name) {
config = config::ChangeDPI(); config = config::ChangeDPI();
return Action::makeAction(device, config.value(), parent);
} else if(name == ChangeHostAction::interface_name) { } else if(name == ChangeHostAction::interface_name) {
config = config::ChangeHost(); config = config::ChangeHost();
return Action::makeAction(device, config.value(), parent);
} else if(name == CycleDPI::interface_name) { } else if(name == CycleDPI::interface_name) {
config = config::CycleDPI(); config = config::CycleDPI();
return Action::makeAction(device, config.value(), parent);
} else if(name == KeypressAction::interface_name) { } else if(name == KeypressAction::interface_name) {
config = config::KeypressAction(); config = config::KeypressAction();
return Action::makeAction(device, config.value(), parent);
} else if(name == NullAction::interface_name) { } else if(name == NullAction::interface_name) {
config = config::NoAction(); config = config::NoAction();
return Action::makeAction(device, config.value(), parent); } else if(name == ChangeHostAction::interface_name) {
config = config::ChangeHost();
} else if(name == ToggleHiresScroll::interface_name) { } else if(name == ToggleHiresScroll::interface_name) {
config = config::ToggleHiresScroll(); config = config::ToggleHiresScroll();
return Action::makeAction(device, config.value(), parent);
} else if(name == ToggleSmartShift::interface_name) { } else if(name == ToggleSmartShift::interface_name) {
config = config::ToggleHiresScroll(); config = config::ToggleHiresScroll();
return Action::makeAction(device, config.value(), parent);
} else if(name == "Default") { } else if(name == "Default") {
config.reset(); config.reset();
return nullptr; return nullptr;
} }
throw InvalidAction(); return Action::makeAction(device, config.value(), parent);
} }
std::shared_ptr<Action> Action::makeAction( std::shared_ptr<Action> Action::makeAction(

View File

@ -18,8 +18,6 @@
#include "ChangeDPI.h" #include "ChangeDPI.h"
#include "../Device.h" #include "../Device.h"
#include "../util/task.h" #include "../util/task.h"
#include "../util/log.h"
#include "../backend/hidpp20/Error.h"
#include "../backend/hidpp20/features/ReprogControls.h" #include "../backend/hidpp20/features/ReprogControls.h"
using namespace logid::actions; using namespace logid::actions;
@ -28,7 +26,7 @@ const char* ChangeDPI::interface_name = "ChangeDPI";
ChangeDPI::ChangeDPI( ChangeDPI::ChangeDPI(
Device *device, config::ChangeDPI& config, Device *device, config::ChangeDPI& config,
const std::shared_ptr<ipcgull::node>& parent) : [[maybe_unused]] const std::shared_ptr<ipcgull::node>& parent) :
Action(device, interface_name, { Action(device, interface_name, {
{ {
{"GetConfig", {this, &ChangeDPI::getConfig, {"change", "sensor"}}}, {"GetConfig", {this, &ChangeDPI::getConfig, {"change", "sensor"}}},
@ -44,7 +42,7 @@ ChangeDPI::ChangeDPI(
_device->hidpp20().deviceIndex()); _device->hidpp20().deviceIndex());
} }
std::tuple<int16_t, uint16_t> ChangeDPI::getConfig() std::tuple<int16_t, uint16_t> ChangeDPI::getConfig() const
{ {
return {_config.inc.value_or(0), _config.sensor.value_or(0)}; return {_config.inc.value_or(0), _config.sensor.value_or(0)};
} }

View File

@ -22,8 +22,7 @@
#include "Action.h" #include "Action.h"
#include "../features/DPI.h" #include "../features/DPI.h"
namespace logid { namespace logid::actions {
namespace actions {
class ChangeDPI : public Action class ChangeDPI : public Action
{ {
public: public:
@ -35,7 +34,7 @@ namespace logid {
virtual void press(); virtual void press();
virtual void release(); virtual void release();
std::tuple<int16_t, uint16_t> getConfig(); [[nodiscard]] std::tuple<int16_t, uint16_t> getConfig() const;
void setChange(int16_t change); void setChange(int16_t change);
void setSensor(uint8_t sensor, bool reset); void setSensor(uint8_t sensor, bool reset);
@ -45,6 +44,6 @@ namespace logid {
config::ChangeDPI& _config; config::ChangeDPI& _config;
std::shared_ptr<features::DPI> _dpi; std::shared_ptr<features::DPI> _dpi;
}; };
}} }
#endif //LOGID_ACTION_CHANGEDPI_H #endif //LOGID_ACTION_CHANGEDPI_H

View File

@ -29,12 +29,19 @@ const char* ChangeHostAction::interface_name = "ChangeHost";
ChangeHostAction::ChangeHostAction( ChangeHostAction::ChangeHostAction(
Device *device, config::ChangeHost& config, Device *device, config::ChangeHost& config,
[[maybe_unused]] const std::shared_ptr<ipcgull::node>& parent) [[maybe_unused]] const std::shared_ptr<ipcgull::node>& parent)
: Action(device, interface_name), _config (config) : Action(device, interface_name, {
{
{"GetHost", {this, &ChangeHostAction::getHost, {"host"}}},
{"SetHost", {this, &ChangeHostAction::setHost, {"host"}}}
}, {}, {}
}), _config (config)
{ {
if(std::holds_alternative<std::string>(_config.host.value())) { if (_config.host.has_value()) {
auto& host = std::get<std::string>(_config.host.value()); if(std::holds_alternative<std::string>(_config.host.value())) {
std::transform(host.begin(), host.end(), auto& host = std::get<std::string>(_config.host.value());
host.begin(), ::tolower); std::transform(host.begin(), host.end(),
host.begin(), ::tolower);
}
} }
try { try {
_change_host = std::make_shared<hidpp20::ChangeHost>(&device->hidpp20()); _change_host = std::make_shared<hidpp20::ChangeHost>(&device->hidpp20());
@ -45,6 +52,29 @@ ChangeHostAction::ChangeHostAction(
} }
} }
std::string ChangeHostAction::getHost() const
{
if(_config.host.has_value()) {
if (std::holds_alternative<std::string>(_config.host.value()))
return std::get<std::string>(_config.host.value());
else
return std::to_string(std::get<int>(_config.host.value()));
} else {
return "";
}
}
void ChangeHostAction::setHost(std::string host)
{
std::transform(host.begin(), host.end(),
host.begin(), ::tolower);
if (host == "next" || host == "prev" || host == "previous") {
_config.host = std::move(host);
} else {
_config.host = std::stoi(host);
}
}
void ChangeHostAction::press() void ChangeHostAction::press()
{ {
// Do nothing, wait until release // Do nothing, wait until release
@ -70,7 +100,7 @@ void ChangeHostAction::release()
} }
next_host %= host_info.hostCount; next_host %= host_info.hostCount;
if(next_host != host_info.currentHost) if(next_host != host_info.currentHost)
_change_host->setHost(next_host-1); _change_host->setHost(next_host);
}); });
} }
} }

View File

@ -22,8 +22,7 @@
#include "Action.h" #include "Action.h"
#include "../backend/hidpp20/features/ChangeHost.h" #include "../backend/hidpp20/features/ChangeHost.h"
namespace logid { namespace logid::actions
namespace actions
{ {
class ChangeHostAction : public Action class ChangeHostAction : public Action
{ {
@ -36,12 +35,16 @@ namespace actions
virtual void press(); virtual void press();
virtual void release(); virtual void release();
[[nodiscard]] std::string getHost() const;
void setHost(std::string host);
virtual uint8_t reprogFlags() const; virtual uint8_t reprogFlags() const;
protected: protected:
std::shared_ptr<backend::hidpp20::ChangeHost> _change_host; std::shared_ptr<backend::hidpp20::ChangeHost> _change_host;
config::ChangeHost& _config; config::ChangeHost& _config;
}; };
}} }
#endif //LOGID_ACTION_CHANGEHOSTACTION_H #endif //LOGID_ACTION_CHANGEHOSTACTION_H

View File

@ -49,9 +49,8 @@ CycleDPI::CycleDPI(Device* device, config::CycleDPI& config,
} }
} }
std::vector<int> CycleDPI::getDPIs() std::vector<int> CycleDPI::getDPIs() const
{ {
std::lock_guard<std::mutex> lock(_dpi_lock);
auto dpis = _config.dpis.value_or(std::list<int>()); auto dpis = _config.dpis.value_or(std::list<int>());
return {dpis.begin(), dpis.end()}; return {dpis.begin(), dpis.end()};
} }

View File

@ -22,8 +22,7 @@
#include "Action.h" #include "Action.h"
#include "../features/DPI.h" #include "../features/DPI.h"
namespace logid { namespace logid::actions {
namespace actions {
class CycleDPI : public Action class CycleDPI : public Action
{ {
public: public:
@ -35,7 +34,7 @@ namespace actions {
virtual void press(); virtual void press();
virtual void release(); virtual void release();
std::vector<int> getDPIs(); [[nodiscard]] std::vector<int> getDPIs() const;
void setDPIs(const std::vector<int>& dpis); void setDPIs(const std::vector<int>& dpis);
virtual uint8_t reprogFlags() const; virtual uint8_t reprogFlags() const;
@ -46,6 +45,6 @@ namespace actions {
std::shared_ptr<features::DPI> _dpi; std::shared_ptr<features::DPI> _dpi;
std::list<int>::const_iterator _current_dpi; std::list<int>::const_iterator _current_dpi;
}; };
}} }
#endif //LOGID_ACTION_CYCLEDPI_H #endif //LOGID_ACTION_CYCLEDPI_H