diff --git a/g13.hpp b/g13.hpp index c616959..69a58a0 100644 --- a/g13.hpp +++ b/g13.hpp @@ -19,7 +19,8 @@ namespace G13 { // const size_t G13_INTERFACE = 0; static const size_t G13_KEY_ENDPOINT = 1; static const size_t G13_LCD_ENDPOINT = 2; - // const size_t G13_KEY_READ_TIMEOUT = 0; + static const size_t G13_KEY_READ_TIMEOUT = 100; + static const size_t G13_KEY_READ_MIN_TIME = 0; // ************************************************************************* diff --git a/g13_action.cpp b/g13_action.cpp index 085140c..8e745d5 100644 --- a/g13_action.cpp +++ b/g13_action.cpp @@ -49,6 +49,7 @@ void G13_Action_Keys::act(G13_Device &g13, bool is_down) { G13_LOG(log4cpp::Priority::DEBUG << "sending KEY DOWN " << _key); } if (!_keysup.empty()) for (int i = _keys.size() - 1; i >= 0; i--) { + g13.SendEvent(EV_SYN, SYN_REPORT, 0); g13.SendEvent(EV_KEY, _keys[i], !is_down); G13_LOG(log4cpp::Priority::DEBUG << "sending KEY UP " << _keys[i]); } @@ -61,6 +62,7 @@ void G13_Action_Keys::act(G13_Device &g13, bool is_down) { g13.SendEvent(EV_KEY, _key, !is_down); G13_LOG(log4cpp::Priority::DEBUG << "sending KEY DOWN " << _key); } + g13.SendEvent(EV_SYN, SYN_REPORT, 0); for (int i = _keysup.size() - 1; i >= 0; i--) { g13.SendEvent(EV_KEY, _keysup[i], is_down); G13_LOG(log4cpp::Priority::DEBUG << "sending KEY UP " << _keysup[i]); diff --git a/g13_device.cpp b/g13_device.cpp index 6a63c8c..32eca56 100644 --- a/g13_device.cpp +++ b/g13_device.cpp @@ -12,6 +12,8 @@ #include "logo.hpp" #include #include +#include +#include namespace G13 { // ************************************************************************* @@ -172,7 +174,7 @@ int G13_Device::ReadKeypresses() { int size = 0; int error = libusb_interrupt_transfer(handle, LIBUSB_ENDPOINT_IN | G13_KEY_ENDPOINT, - buffer, G13_REPORT_SIZE, &size, 100); + buffer, G13_REPORT_SIZE, &size, G13_KEY_READ_TIMEOUT); if (error && error != LIBUSB_ERROR_TIMEOUT) { G13_ERR("Error while reading keys: " << DescribeLibusbErrorCode(error)); @@ -186,6 +188,8 @@ int G13_Device::ReadKeypresses() { m_currentProfile->ParseKeys(buffer); SendEvent(EV_SYN, SYN_REPORT, 0); } + if (G13_KEY_READ_MIN_TIME && !error) + std::this_thread::sleep_for(std::chrono::milliseconds(G13_KEY_READ_MIN_TIME)); return 0; } @@ -435,8 +439,13 @@ void G13_Device::InitCommands() { if (sscanf(remainder, "%lf %lf %lf %lf", &x1, &y1, &x2, &y2) != 4) { throw G13_CommandException("bad bounds format"); } - zone->set_bounds(G13_ZoneBounds(x1, y1, x2, y2)); - + // TODO: Add center offset and bounds (or update zone) + zone->set_bounds(G13_ZoneBounds( + (unsigned char)(x1 * UCHAR_MAX), + (unsigned char)(y1 * UCHAR_MAX), + (unsigned char)(x2 * UCHAR_MAX), + (unsigned char)(y2 * UCHAR_MAX) + )); } else if (operation == "del") { m_stick.RemoveZone(*zone); } else { diff --git a/g13_manager.cpp b/g13_manager.cpp index 8aeed04..af1bed9 100644 --- a/g13_manager.cpp +++ b/g13_manager.cpp @@ -283,4 +283,4 @@ void G13_Manager::setLogoFilename(const std::string &newLogoFilename) { logoFilename = newLogoFilename; } -} // namespace G13 \ No newline at end of file +} // namespace G13 diff --git a/g13_stick.cpp b/g13_stick.cpp index f3a421b..3d5572a 100644 --- a/g13_stick.cpp +++ b/g13_stick.cpp @@ -92,19 +92,8 @@ void G13_StickZone::dump(std::ostream &out) const { } void G13_StickZone::test(const G13_ZoneCoord &loc) { - if (!_action) - return; - bool prior_active = _active; - _active = _bounds.contains(loc); - if (!_active) { - if (prior_active) { - // cout << "exit stick zone " << m_name << std::endl; - _action->act(false); - } - } else { - // cout << "in stick zone " << m_name << std::endl; - _action->act(true); - } + if (_action && _active != _bounds.contains(loc)) + _action->act(_active = !_active); } G13_StickZone::G13_StickZone(G13_Stick &stick, const std::string &name, @@ -120,6 +109,11 @@ void G13_Stick::ParseJoystick(const unsigned char *buf) { // update targets if we're in calibration mode switch (m_stick_mode) { + case STICK_ABSOLUTE: + break; + case STICK_KEYS: + break; + case STICK_CALCENTER: m_center_pos = m_current_pos; return; @@ -131,13 +125,10 @@ void G13_Stick::ParseJoystick(const unsigned char *buf) { case STICK_CALBOUNDS: m_bounds.expand(m_current_pos); return; - - case STICK_ABSOLUTE: - break; - case STICK_KEYS: - break; } - +/* TODO: Transform zone so we're not constantly transforming position + * Is this even necessary? Zones could just be tweaked manually and + * north is currently unused. Four-point zones would be more powerful // determine our normalized position double dx; // = 0.5 if (m_current_pos.x <= m_center_pos.x) { @@ -160,7 +151,7 @@ void G13_Stick::ParseJoystick(const unsigned char *buf) { G13_DBG("x=" << m_current_pos.x << " y=" << m_current_pos.y << " dx=" << dx << " dy=" << dy); - G13_ZoneCoord jpos(dx, dy); + G13_ZoneCoord jpos(dx, dy);*/ if (m_stick_mode == STICK_ABSOLUTE) { _keypad.SendEvent(EV_ABS, ABS_X, m_current_pos.x); _keypad.SendEvent(EV_ABS, ABS_Y, m_current_pos.y); @@ -168,7 +159,7 @@ void G13_Stick::ParseJoystick(const unsigned char *buf) { } else if (m_stick_mode == STICK_KEYS) { // BOOST_FOREACH (G13_StickZone& zone, m_zones) { zone.test(jpos); } for (auto &zone : m_zones) { - zone.test(jpos); + zone.test(G13_ZoneCoord(m_current_pos.x, m_current_pos.y)); } return; diff --git a/g13_stick.hpp b/g13_stick.hpp index 51c9729..0a778b2 100644 --- a/g13_stick.hpp +++ b/g13_stick.hpp @@ -13,8 +13,8 @@ class G13_Device; typedef Helper::Coord G13_StickCoord; typedef Helper::Bounds G13_StickBounds; -typedef Helper::Coord G13_ZoneCoord; -typedef Helper::Bounds G13_ZoneBounds; +typedef Helper::Coord G13_ZoneCoord; +typedef Helper::Bounds G13_ZoneBounds; // *************************************************************************