Skip to content
3 changes: 2 additions & 1 deletion g13.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

// *************************************************************************

Expand Down
2 changes: 2 additions & 0 deletions g13_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand All @@ -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]);
Expand Down
15 changes: 12 additions & 3 deletions g13_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "logo.hpp"
#include <fstream>
#include <unistd.h>
#include <chrono>
#include <thread>

namespace G13 {
// *************************************************************************
Expand Down Expand Up @@ -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));
Expand All @@ -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;
}

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion g13_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,4 @@ void G13_Manager::setLogoFilename(const std::string &newLogoFilename) {
logoFilename = newLogoFilename;
}

} // namespace G13
} // namespace G13
33 changes: 12 additions & 21 deletions g13_stick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -160,15 +151,15 @@ 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);

} 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;

Expand Down
4 changes: 2 additions & 2 deletions g13_stick.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class G13_Device;

typedef Helper::Coord<int> G13_StickCoord;
typedef Helper::Bounds<int> G13_StickBounds;
typedef Helper::Coord<double> G13_ZoneCoord;
typedef Helper::Bounds<double> G13_ZoneBounds;
typedef Helper::Coord<unsigned char> G13_ZoneCoord;
typedef Helper::Bounds<unsigned char> G13_ZoneBounds;

// *************************************************************************

Expand Down