| 1 |
// FGHIDEventInput.cxx -- handle event driven input devices via HIDAPI |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 4 |
// SPDX-FileCopyrightText: 2017 James Turner <[email protected]> |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
#include "simgear/debug/debug_types.h" |
| 8 |
|
| 9 |
#include "FGHIDEventInput.hxx" |
| 10 |
|
| 11 |
#include <algorithm> |
| 12 |
#include <cassert> |
| 13 |
#include <cstdlib> |
| 14 |
#include <unordered_set> |
| 15 |
#include <string> |
| 16 |
|
| 17 |
#include <Main/fg_props.hxx> |
| 18 |
#include <hidapi/hidapi.h> |
| 19 |
#include <hidapi/hidparse.h> |
| 20 |
|
| 21 |
#include <simgear/debug/ErrorReportingCallback.hxx> |
| 22 |
#include <simgear/io/lowlevel.hxx> |
| 23 |
#include <simgear/misc/strutils.hxx> |
| 24 |
#include <simgear/sg_inlines.h> |
| 25 |
#include <simgear/structure/exception.hxx> |
| 26 |
|
| 27 |
#include "FGHIDUsage.hxx" |
| 28 |
|
| 29 |
using simgear::strutils::encodeHex; |
| 30 |
|
| 31 |
namespace HID { |
| 32 |
|
| 33 |
bool shouldPrefixWithAbs(uint32_t usagePage, uint32_t usage) |
| 34 |
{ |
| 35 |
const auto enumUsage = static_cast<UsagePage>(usagePage); |
| 36 |
if (enumUsage == UsagePage::GenericDesktop) { |
| 37 |
switch (usage) { |
| 38 |
case GD_Wheel: |
| 39 |
case GD_Dial: |
| 40 |
case GD_Hatswitch: |
| 41 |
case GD_Slider: |
| 42 |
case GD_Rx: |
| 43 |
case GD_Ry: |
| 44 |
case GD_Rz: |
| 45 |
case GD_X: |
| 46 |
case GD_Y: |
| 47 |
case GD_Z: |
| 48 |
return true; |
| 49 |
default: |
| 50 |
break; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
ReportType reportTypeFromString(const std::string& s) |
| 58 |
{ |
| 59 |
if (s == "input") return ReportType::In; |
| 60 |
if (s == "output") return ReportType::Out; |
| 61 |
if (s == "feature") return ReportType::Feature; |
| 62 |
return ReportType::Invalid; |
| 63 |
} |
| 64 |
} // namespace HID |
| 65 |
|
| 66 |
class FGHIDEventInput::FGHIDEventInputPrivate |
| 67 |
{ |
| 68 |
public: |
| 69 |
FGHIDEventInput* p = nullptr; |
| 70 |
|
| 71 |
void evaluateDevice(hid_device_info* deviceInfo); |
| 72 |
}; |
| 73 |
|
| 74 |
// anonymous namespace to define our device subclass |
| 75 |
namespace { |
| 76 |
|
| 77 |
class FGHIDDevice : public FGInputDevice |
| 78 |
{ |
| 79 |
public: |
| 80 |
FGHIDDevice(hid_device_info* devInfo, |
| 81 |
FGHIDEventInput* subsys); |
| 82 |
|
| 83 |
virtual ~FGHIDDevice(); |
| 84 |
|
| 85 |
bool Open() override; |
| 86 |
void Close() override; |
| 87 |
void Configure(SGPropertyNode_ptr node) override; |
| 88 |
|
| 89 |
void update(double dt) override; |
| 90 |
const char* TranslateEventName(FGEventData& eventData) override; |
| 91 |
void Send(const char* eventName, double value) override; |
| 92 |
void SendFeatureReport(unsigned int reportId, const simgear::UInt8Vector& data) override; |
| 93 |
void SendOutputReport(unsigned int reportId, const simgear::UInt8Vector& data) override; |
| 94 |
|
| 95 |
class Item |
| 96 |
{ |
| 97 |
public: |
| 98 |
Item(const std::string& n, uint32_t offset, uint8_t size) : name(n), |
| 99 |
bitOffset(offset), |
| 100 |
bitSize(size) |
| 101 |
{ |
| 102 |
} |
| 103 |
|
| 104 |
std::string name; |
| 105 |
uint32_t bitOffset = 0; // form the start of the report |
| 106 |
uint8_t bitSize = 1; |
| 107 |
bool isRelative = false; |
| 108 |
bool doSignExtend = false; |
| 109 |
int lastValue = 0; |
| 110 |
// int defaultValue = 0; |
| 111 |
// range, units, etc not needed for now |
| 112 |
// hopefully this doesn't need to be a list |
| 113 |
FGInputEvent_ptr event; |
| 114 |
}; |
| 115 |
|
| 116 |
private: |
| 117 |
class Report |
| 118 |
{ |
| 119 |
public: |
| 120 |
Report(HID::ReportType ty, uint8_t n = 0) : type(ty), number(n) {} |
| 121 |
|
| 122 |
HID::ReportType type; |
| 123 |
uint8_t number = 0; |
| 124 |
std::vector<Item*> items; |
| 125 |
|
| 126 |
uint32_t currentBitSize() const |
| 127 |
{ |
| 128 |
uint32_t size = 0; |
| 129 |
for (auto i : items) { |
| 130 |
size += i->bitSize; |
| 131 |
} |
| 132 |
return size; |
| 133 |
} |
| 134 |
}; |
| 135 |
|
| 136 |
bool parseUSBHIDDescriptor(); |
| 137 |
void parseCollection(hid_item* collection); |
| 138 |
void parseItem(hid_item* item); |
| 139 |
|
| 140 |
Report* getReport(HID::ReportType ty, uint8_t number, bool doCreate = false); |
| 141 |
|
| 142 |
void sendReport(Report* report) const; |
| 143 |
|
| 144 |
uint8_t countWithName(const std::string& name) const; |
| 145 |
std::pair<Report*, Item*> itemWithName(const std::string& name) const; |
| 146 |
|
| 147 |
void processInputReport(Report* report, unsigned char* data, size_t length, |
| 148 |
double dt, int keyModifiers); |
| 149 |
|
| 150 |
int maybeSignExtend(Item* item, int inValue); |
| 151 |
|
| 152 |
void defineReport(SGPropertyNode_ptr reportNode); |
| 153 |
|
| 154 |
void dumpRawBytes(const std::string& b) const; |
| 155 |
|
| 156 |
std::vector<Report*> _reports; |
| 157 |
std::string _hidPath; |
| 158 |
hid_device* _device = nullptr; |
| 159 |
bool _haveNumberedReports = false; |
| 160 |
bool _debugRaw = false; |
| 161 |
|
| 162 |
/// set if we parsed the device description our XML |
| 163 |
/// instead of from the USB data. Useful on Windows where the data |
| 164 |
/// is inaccessible, or devices with broken descriptors |
| 165 |
bool _haveLocalDescriptor = false; |
| 166 |
|
| 167 |
/// allow specifying the descriptor as hex bytes in XML |
| 168 |
simgear::UInt8Vector _rawXMLDescriptor; |
| 169 |
|
| 170 |
// all sets which will be send on the next update() call. |
| 171 |
std::set<Report*> _dirtyReports; |
| 172 |
}; |
| 173 |
|
| 174 |
class HIDEventData : public FGEventData |
| 175 |
{ |
| 176 |
public: |
| 177 |
// item, value, dt, keyModifiers |
| 178 |
HIDEventData(FGHIDDevice::Item* it, int value, double dt, int keyMods) : FGEventData(value, dt, keyMods), |
| 179 |
item(it) |
| 180 |
{ |
| 181 |
assert(item); |
| 182 |
} |
| 183 |
|
| 184 |
FGHIDDevice::Item* item = nullptr; |
| 185 |
}; |
| 186 |
|
| 187 |
FGHIDDevice::FGHIDDevice(hid_device_info* devInfo, FGHIDEventInput*) |
| 188 |
{ |
| 189 |
class_id = "FGHIDDevice"; |
| 190 |
_hidPath = devInfo->path; |
| 191 |
|
| 192 |
std::wstring manufacturerName, productName; |
| 193 |
productName = devInfo->product_string ? std::wstring(devInfo->product_string) |
| 194 |
: L"unknown HID device"; |
| 195 |
|
| 196 |
if (devInfo->manufacturer_string) { |
| 197 |
manufacturerName = std::wstring(devInfo->manufacturer_string); |
| 198 |
SetName(simgear::strutils::convertWStringToUtf8(manufacturerName) + " " + |
| 199 |
simgear::strutils::convertWStringToUtf8(productName)); |
| 200 |
} else { |
| 201 |
SetName(simgear::strutils::convertWStringToUtf8(productName)); |
| 202 |
} |
| 203 |
|
| 204 |
const auto serial = devInfo->serial_number; |
| 205 |
std::string path(devInfo->path); |
| 206 |
// most devices return an empty serial number, unfortunately |
| 207 |
if ((serial != nullptr) && std::wcslen(serial) > 0) { |
| 208 |
SetSerialNumber(simgear::strutils::convertWStringToUtf8(serial)); |
| 209 |
} |
| 210 |
|
| 211 |
std::string _usage = HID::nameForUsage(devInfo->usage_page, devInfo->usage); |
| 212 |
SG_LOG(SG_INPUT, SG_DEBUG, "HID device " << _hidPath << " " |
| 213 |
<< "0x" << std::hex << devInfo->vendor_id << ":0x" << std::hex << devInfo->product_id << " " |
| 214 |
<< "release " << devInfo->release_number << " " |
| 215 |
<< "usage " << _usage << "(0x" << std::hex << devInfo->usage_page |
| 216 |
<< ":0x" << std::hex << devInfo->usage << ") " |
| 217 |
<< "ifn " << devInfo->interface_number << " " |
| 218 |
<< GetName() ); |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
FGHIDDevice::~FGHIDDevice() |
| 223 |
{ |
| 224 |
if (_device) { |
| 225 |
hid_close(_device); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
void FGHIDDevice::Configure(SGPropertyNode_ptr node) |
| 230 |
{ |
| 231 |
// base class first |
| 232 |
FGInputDevice::Configure(node); |
| 233 |
|
| 234 |
if (node->hasChild("hid-descriptor")) { |
| 235 |
_haveLocalDescriptor = true; |
| 236 |
if (debugEvents) { |
| 237 |
SG_LOG(SG_INPUT, SG_INFO, GetUniqueName() << " will configure using local HID descriptor"); |
| 238 |
} |
| 239 |
|
| 240 |
for (auto report : node->getChild("hid-descriptor")->getChildren("report")) { |
| 241 |
defineReport(report); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
if (node->hasChild("hid-raw-descriptor")) { |
| 246 |
_rawXMLDescriptor = simgear::strutils::decodeHex(node->getStringValue("hid-raw-descriptor")); |
| 247 |
if (debugEvents) { |
| 248 |
SG_LOG(SG_INPUT, SG_INFO, GetUniqueName() << " will configure using XML-defined raw HID descriptor"); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
if (node->getBoolValue("hid-debug-raw")) { |
| 253 |
_debugRaw = true; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
bool FGHIDDevice::Open() |
| 258 |
{ |
| 259 |
SG_LOG(SG_INPUT, SG_INFO, "HID open " << GetUniqueName()); |
| 260 |
_device = hid_open_path(_hidPath.c_str()); |
| 261 |
if (_device == nullptr) { |
| 262 |
SG_LOG(SG_INPUT, SG_WARN, GetUniqueName() << ": HID: Failed to open:" << _hidPath); |
| 263 |
SG_LOG(SG_INPUT, SG_WARN, "\tnote on Linux you may need to adjust permissions of the device using UDev rules."); |
| 264 |
return false; |
| 265 |
} |
| 266 |
|
| 267 |
if (_rawXMLDescriptor.empty()) { |
| 268 |
_rawXMLDescriptor.resize(2048); |
| 269 |
int descriptorSize = hid_get_report_descriptor(_device, _rawXMLDescriptor.data(), _rawXMLDescriptor.size()); |
| 270 |
if (descriptorSize <= 0) { |
| 271 |
SG_LOG(SG_INPUT, SG_WARN, "HID: " << GetUniqueName() << " failed to read HID descriptor"); |
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
_rawXMLDescriptor.resize(descriptorSize); |
| 276 |
} |
| 277 |
|
| 278 |
if (!_haveLocalDescriptor) { |
| 279 |
bool ok = parseUSBHIDDescriptor(); |
| 280 |
if (!ok) |
| 281 |
return false; |
| 282 |
} |
| 283 |
|
| 284 |
for (auto& v : handledEvents) { |
| 285 |
auto reportItem = itemWithName(v.first); |
| 286 |
if (!reportItem.second) { |
| 287 |
SG_LOG(SG_INPUT, SG_WARN, "HID device:" << GetUniqueName() << " has no element for event:" << v.first); |
| 288 |
continue; |
| 289 |
} |
| 290 |
|
| 291 |
FGInputEvent_ptr event = v.second; |
| 292 |
if (debugEvents) { |
| 293 |
SG_LOG(SG_INPUT, SG_INFO, "\tfound item for event:" << v.first); |
| 294 |
} |
| 295 |
|
| 296 |
reportItem.second->event = event; |
| 297 |
} |
| 298 |
|
| 299 |
return true; |
| 300 |
} |
| 301 |
|
| 302 |
bool FGHIDDevice::parseUSBHIDDescriptor() |
| 303 |
{ |
| 304 |
#if defined(SG_WINDOWS) |
| 305 |
if (_rawXMLDescriptor.empty()) { |
| 306 |
SG_LOG(SG_INPUT, SG_ALERT, GetUniqueName() << ": on Windows, there is no way to extract the UDB-HID report descriptor. " |
| 307 |
<< "\nPlease supply the report descriptor in the device XML configuration."); |
| 308 |
SG_LOG(SG_INPUT, SG_ALERT, "See this page:<> for information on extracting the report descriptor on Windows"); |
| 309 |
return false; |
| 310 |
} |
| 311 |
#endif |
| 312 |
|
| 313 |
if (_debugRaw) { |
| 314 |
SG_LOG(SG_INPUT, SG_INFO, "\nHID: descriptor for:" << GetUniqueName() << "\n\t" << encodeHex(_rawXMLDescriptor, ':')); |
| 315 |
} |
| 316 |
|
| 317 |
hid_item* rootItem = nullptr; |
| 318 |
hid_parse_reportdesc(_rawXMLDescriptor.data(), _rawXMLDescriptor.size(), &rootItem); |
| 319 |
if (debugEvents) { |
| 320 |
SG_LOG(SG_INPUT, SG_INFO, "\nHID: scan for:" << GetUniqueName()); |
| 321 |
} |
| 322 |
|
| 323 |
parseCollection(rootItem); |
| 324 |
|
| 325 |
hid_free_reportdesc(rootItem); |
| 326 |
return true; |
| 327 |
} |
| 328 |
|
| 329 |
void FGHIDDevice::parseCollection(hid_item* c) |
| 330 |
{ |
| 331 |
for (hid_item* child = c->collection; child != nullptr; child = child->next) { |
| 332 |
if (child->collection) { |
| 333 |
parseCollection(child); |
| 334 |
} else { |
| 335 |
// leaf item |
| 336 |
parseItem(child); |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
auto FGHIDDevice::getReport(HID::ReportType ty, uint8_t number, bool doCreate) -> Report* |
| 342 |
{ |
| 343 |
if (number > 0) { |
| 344 |
_haveNumberedReports = true; |
| 345 |
} |
| 346 |
|
| 347 |
for (auto report : _reports) { |
| 348 |
if ((report->type == ty) && (report->number == number)) { |
| 349 |
return report; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
if (doCreate) { |
| 354 |
auto r = new Report{ty, number}; |
| 355 |
_reports.push_back(r); |
| 356 |
return r; |
| 357 |
} else { |
| 358 |
return nullptr; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
auto FGHIDDevice::itemWithName(const std::string& name) const -> std::pair<Report*, Item*> |
| 363 |
{ |
| 364 |
for (auto report : _reports) { |
| 365 |
for (auto item : report->items) { |
| 366 |
if (item->name == name) { |
| 367 |
return std::make_pair(report, item); |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
return std::make_pair(static_cast<Report*>(nullptr), static_cast<Item*>(nullptr)); |
| 373 |
} |
| 374 |
|
| 375 |
uint8_t FGHIDDevice::countWithName(const std::string& name) const |
| 376 |
{ |
| 377 |
uint8_t result = 0; |
| 378 |
size_t nameLength = name.length(); |
| 379 |
|
| 380 |
for (auto report : _reports) { |
| 381 |
for (auto item : report->items) { |
| 382 |
if (strncmp(name.c_str(), item->name.c_str(), nameLength) == 0) { |
| 383 |
result++; |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
return result; |
| 389 |
} |
| 390 |
|
| 391 |
void FGHIDDevice::parseItem(hid_item* item) |
| 392 |
{ |
| 393 |
std::string name = HID::nameForUsage(item->usage >> 16, item->usage & 0xffff); |
| 394 |
if (hid_parse_is_relative(item)) { |
| 395 |
name = "rel-" + name; // prefix relative names |
| 396 |
} else if (HID::shouldPrefixWithAbs(item->usage >> 16, item->usage & 0xffff)) { |
| 397 |
name = "abs-" + name; |
| 398 |
} |
| 399 |
|
| 400 |
const auto ty = static_cast<HID::ReportType>(item->type); |
| 401 |
auto existingItem = itemWithName(name); |
| 402 |
if (existingItem.second) { |
| 403 |
// type fixup |
| 404 |
const HID::ReportType existingItemType = existingItem.first->type; |
| 405 |
if (existingItemType != ty) { |
| 406 |
// might be an item named identically in input/output and feature reports |
| 407 |
// -> prefix the feature one with 'feature' |
| 408 |
if (ty == HID::ReportType::Feature) { |
| 409 |
name = "feature-" + name; |
| 410 |
} else if (existingItemType == HID::ReportType::Feature) { |
| 411 |
// rename this existing item since it's a feature |
| 412 |
existingItem.second->name = "feature-" + name; |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
// do the count now, after we did any renaming, since we might have |
| 418 |
// N > 1 for the new name |
| 419 |
int existingCount = countWithName(name); |
| 420 |
if (existingCount > 0) { |
| 421 |
if (existingCount == 1) { |
| 422 |
// rename existing item 0 to have the "-0" suffix |
| 423 |
auto existingItem = itemWithName(name); |
| 424 |
existingItem.second->name += "-0"; |
| 425 |
} |
| 426 |
|
| 427 |
// define the new nae |
| 428 |
std::stringstream os; |
| 429 |
os << name << "-" << existingCount; |
| 430 |
name = os.str(); |
| 431 |
} |
| 432 |
|
| 433 |
auto report = getReport(ty, item->report_id, true /* create */); |
| 434 |
uint32_t bitOffset = report->currentBitSize(); |
| 435 |
|
| 436 |
if (debugEvents) { |
| 437 |
SG_LOG(SG_INPUT, SG_INFO, GetUniqueName() << ": add:" << name << ", bits: " << bitOffset << ":" << (int)item->report_size << ", report=" << (int)item->report_id); |
| 438 |
} |
| 439 |
|
| 440 |
Item* itemObject = new Item{name, bitOffset, item->report_size}; |
| 441 |
itemObject->isRelative = hid_parse_is_relative(item); |
| 442 |
itemObject->doSignExtend = (item->logical_min < 0) || (item->logical_max < 0); |
| 443 |
report->items.push_back(itemObject); |
| 444 |
} |
| 445 |
|
| 446 |
void FGHIDDevice::Close() |
| 447 |
{ |
| 448 |
if (_device) { |
| 449 |
hid_close(_device); |
| 450 |
_device = nullptr; |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
void FGHIDDevice::update(double dt) |
| 455 |
{ |
| 456 |
if (!_device) { |
| 457 |
return; |
| 458 |
} |
| 459 |
|
| 460 |
uint8_t reportBuf[65]; |
| 461 |
int readCount = 0; |
| 462 |
while (true) { |
| 463 |
readCount = hid_read_timeout(_device, reportBuf, sizeof(reportBuf), 0); |
| 464 |
|
| 465 |
if (readCount <= 0) { |
| 466 |
break; |
| 467 |
} |
| 468 |
|
| 469 |
int modifiers = fgGetKeyModifiers(); |
| 470 |
const uint8_t reportNumber = _haveNumberedReports ? reportBuf[0] : 0; |
| 471 |
auto inputReport = getReport(HID::ReportType::In, reportNumber, false); |
| 472 |
if (!inputReport) { |
| 473 |
SG_LOG(SG_INPUT, SG_WARN, GetName() << ": FGHIDDevice: Unknown input report number:" << static_cast<int>(reportNumber)); |
| 474 |
} else { |
| 475 |
uint8_t* reportBytes = _haveNumberedReports ? reportBuf + 1 : reportBuf; |
| 476 |
size_t reportSize = _haveNumberedReports ? readCount - 1 : readCount; |
| 477 |
processInputReport(inputReport, reportBytes, reportSize, dt, modifiers); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
FGInputDevice::update(dt); |
| 482 |
|
| 483 |
for (auto rep : _dirtyReports) { |
| 484 |
sendReport(rep); |
| 485 |
} |
| 486 |
|
| 487 |
_dirtyReports.clear(); |
| 488 |
} |
| 489 |
|
| 490 |
void FGHIDDevice::sendReport(Report* report) const |
| 491 |
{ |
| 492 |
if (!_device) { |
| 493 |
return; |
| 494 |
} |
| 495 |
|
| 496 |
uint8_t reportBytes[65]; |
| 497 |
size_t reportLength = 0; |
| 498 |
memset(reportBytes, 0, sizeof(reportBytes)); |
| 499 |
reportBytes[0] = report->number; |
| 500 |
|
| 501 |
// fill in valid data |
| 502 |
for (auto item : report->items) { |
| 503 |
reportLength += item->bitSize; |
| 504 |
if (item->lastValue == 0) { |
| 505 |
continue; |
| 506 |
} |
| 507 |
|
| 508 |
writeBits(reportBytes + 1, item->bitOffset, item->bitSize, item->lastValue); |
| 509 |
} |
| 510 |
|
| 511 |
reportLength /= 8; |
| 512 |
|
| 513 |
if (_debugRaw) { |
| 514 |
SG_LOG(SG_INPUT, SG_INFO, "sending bytes: " << encodeHex(reportBytes, ':')); |
| 515 |
} |
| 516 |
|
| 517 |
// send the data, based on the report type |
| 518 |
if (report->type == HID::ReportType::Feature) { |
| 519 |
hid_send_feature_report(_device, reportBytes, reportLength + 1); |
| 520 |
} else { |
| 521 |
assert(report->type == HID::ReportType::Out); |
| 522 |
hid_write(_device, reportBytes, reportLength + 1); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
int FGHIDDevice::maybeSignExtend(Item* item, int inValue) |
| 527 |
{ |
| 528 |
return item->doSignExtend ? signExtend(inValue, item->bitSize) : inValue; |
| 529 |
} |
| 530 |
|
| 531 |
void FGHIDDevice::processInputReport(Report* report, unsigned char* data, |
| 532 |
size_t length, |
| 533 |
double dt, int keyModifiers) |
| 534 |
{ |
| 535 |
if (_debugRaw) { |
| 536 |
SG_LOG(SG_INPUT, SG_INFO, GetName() << " FGHIDDeivce received input report:" << (int)report->number << ", len=" << length); |
| 537 |
{ |
| 538 |
SG_LOG(SG_INPUT, SG_INFO, "\tbytes: " << encodeHex(data, length, ':')); |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
for (auto item : report->items) { |
| 543 |
int value = extractBits(data, length, item->bitOffset, item->bitSize); |
| 544 |
|
| 545 |
value = maybeSignExtend(item, value); |
| 546 |
|
| 547 |
// suppress events for values that aren't changing |
| 548 |
if (item->isRelative) { |
| 549 |
// suppress spurious 0-valued relative events |
| 550 |
if (value == 0) { |
| 551 |
continue; |
| 552 |
} |
| 553 |
} else { |
| 554 |
// suppress no-change events for absolute items |
| 555 |
if (value == item->lastValue) { |
| 556 |
continue; |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
item->lastValue = value; |
| 561 |
if (!item->event) |
| 562 |
continue; |
| 563 |
|
| 564 |
if (_debugRaw) { |
| 565 |
SG_LOG(SG_INPUT, SG_INFO, "\titem:" << item->name << " = " << value); |
| 566 |
} |
| 567 |
|
| 568 |
HIDEventData event{item, value, dt, keyModifiers}; |
| 569 |
HandleEvent(event); |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
void FGHIDDevice::SendFeatureReport(unsigned int reportId, const simgear::UInt8Vector& data) |
| 574 |
{ |
| 575 |
if (!_device) { |
| 576 |
return; |
| 577 |
} |
| 578 |
|
| 579 |
if (_debugRaw) { |
| 580 |
SG_LOG(SG_INPUT, SG_INFO, GetName() << ": FGHIDDevice: Sending feature report:" << (int)reportId << ", len=" << data.size()); |
| 581 |
SG_LOG(SG_INPUT, SG_INFO, "\tbytes: " << encodeHex(data, ':')); |
| 582 |
} |
| 583 |
|
| 584 |
uint8_t buf[65]; |
| 585 |
size_t len = std::min(data.size() + 1, sizeof(buf)); |
| 586 |
buf[0] = reportId; |
| 587 |
memcpy(buf + 1, data.data(), len - 1); |
| 588 |
size_t r = hid_send_feature_report(_device, buf, len); |
| 589 |
if (r < 0) { |
| 590 |
SG_LOG(SG_INPUT, SG_WARN, GetName() << ": FGHIDDevice: Sending feature report failed, error-string is:\n" |
| 591 |
<< simgear::strutils::error_string(errno)); |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
const char* FGHIDDevice::TranslateEventName(FGEventData& eventData) |
| 596 |
{ |
| 597 |
HIDEventData& hidEvent = static_cast<HIDEventData&>(eventData); |
| 598 |
return hidEvent.item->name.c_str(); |
| 599 |
} |
| 600 |
|
| 601 |
void FGHIDDevice::Send(const char* eventName, double value) |
| 602 |
{ |
| 603 |
// even though this called 'Send' it's really 'mark value for sending when we update', to |
| 604 |
// avoid generating multiple output reports in a single update frame. |
| 605 |
auto item = itemWithName(eventName); |
| 606 |
if (item.second == nullptr) { |
| 607 |
SG_LOG(SG_INPUT, SG_WARN, GetName() << ": FGHIDDevice:unknown item name:" << eventName); |
| 608 |
return; |
| 609 |
} |
| 610 |
|
| 611 |
int intValue = static_cast<int>(value); |
| 612 |
if (item.second->lastValue == intValue) { |
| 613 |
return; // not actually changing |
| 614 |
} |
| 615 |
|
| 616 |
lastEventName->setStringValue(eventName); |
| 617 |
lastEventValue->setDoubleValue(value); |
| 618 |
|
| 619 |
// update the stored value prior to sending |
| 620 |
item.second->lastValue = intValue; |
| 621 |
_dirtyReports.insert(item.first); |
| 622 |
} |
| 623 |
|
| 624 |
void FGHIDDevice::SendOutputReport(unsigned int reportId, const simgear::UInt8Vector& data) |
| 625 |
{ |
| 626 |
if (!_device) { |
| 627 |
return; |
| 628 |
} |
| 629 |
|
| 630 |
auto r = getReport(HID::ReportType::Out, reportId); |
| 631 |
if (!r) { |
| 632 |
SG_LOG(SG_INPUT, SG_DEV_ALERT, "HID device does not define an output report with ID:" << reportId); |
| 633 |
return; |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
if (_debugRaw) { |
| 638 |
SG_LOG(SG_INPUT, SG_INFO, GetName() << ": FGHIDDevice: output feature:" << (int)reportId << ", len=" << data.size()); |
| 639 |
SG_LOG(SG_INPUT, SG_INFO, "\tbytes: " << encodeHex(data, ':')); |
| 640 |
} |
| 641 |
|
| 642 |
uint8_t buf[65]; |
| 643 |
size_t len = std::min(data.size() + 1, sizeof(buf)); |
| 644 |
buf[0] = reportId; |
| 645 |
memcpy(buf + 1, data.data(), len - 1); |
| 646 |
|
| 647 |
auto result = hid_write(_device, buf, len); |
| 648 |
if (result < 0) { |
| 649 |
SG_LOG(SG_INPUT, SG_DEV_ALERT, GetName() << ": FGHIDDevice: Sending outpit report failed, error-string is:\n" |
| 650 |
<< simgear::strutils::error_string(errno)); |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
void FGHIDDevice::defineReport(SGPropertyNode_ptr reportNode) |
| 655 |
{ |
| 656 |
const int nChildren = reportNode->nChildren(); |
| 657 |
uint32_t bitCount = 0; |
| 658 |
const auto rty = HID::reportTypeFromString(reportNode->getStringValue("type")); |
| 659 |
if (rty == HID::ReportType::Invalid) { |
| 660 |
SG_LOG(SG_INPUT, SG_WARN, GetName() << ": FGHIDDevice: invalid report type:" << reportNode->getStringValue("type")); |
| 661 |
return; |
| 662 |
} |
| 663 |
|
| 664 |
const auto id = reportNode->getIntValue("id"); |
| 665 |
if (id > 0) { |
| 666 |
_haveNumberedReports = true; |
| 667 |
} |
| 668 |
|
| 669 |
auto report = new Report(rty, id); |
| 670 |
_reports.push_back(report); |
| 671 |
|
| 672 |
for (int c = 0; c < nChildren; ++c) { |
| 673 |
const auto n = reportNode->getChild(c); |
| 674 |
const int size = n->getIntValue("size", 1); // default to a single bit |
| 675 |
if (n->getNameString() == "unused-bits") { |
| 676 |
bitCount += size; |
| 677 |
continue; |
| 678 |
} |
| 679 |
|
| 680 |
if (n->getNameString() == "type" || n->getNameString() == "id") { |
| 681 |
continue; // already handled above |
| 682 |
} |
| 683 |
|
| 684 |
// allow repeating items |
| 685 |
uint8_t count = n->getIntValue("count", 1); |
| 686 |
std::string name = n->getNameString(); |
| 687 |
const auto lastHypen = name.rfind("-"); |
| 688 |
std::string baseName = name.substr(0, lastHypen + 1); |
| 689 |
int baseIndex = std::stoi(name.substr(lastHypen + 1)); |
| 690 |
|
| 691 |
const bool isRelative = (name.find("rel-") == 0); |
| 692 |
const bool isSigned = n->getBoolValue("is-signed", false); |
| 693 |
|
| 694 |
for (uint8_t i = 0; i < count; ++i) { |
| 695 |
std::ostringstream oss; |
| 696 |
oss << baseName << (baseIndex + i); |
| 697 |
Item* itemObject = new Item{oss.str(), bitCount, static_cast<uint8_t>(size)}; |
| 698 |
itemObject->isRelative = isRelative; |
| 699 |
itemObject->doSignExtend = isSigned; |
| 700 |
report->items.push_back(itemObject); |
| 701 |
bitCount += size; |
| 702 |
} |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
} // namespace |
| 708 |
|
| 709 |
|
| 710 |
int extractBits(uint8_t* bytes, size_t lengthInBytes, size_t bitOffset, size_t bitSize) |
| 711 |
{ |
| 712 |
const size_t wholeBytesToSkip = bitOffset >> 3; |
| 713 |
const size_t offsetInByte = bitOffset & 0x7; |
| 714 |
|
| 715 |
// work out how many whole bytes to copy |
| 716 |
const size_t bytesToCopy = std::min(sizeof(uint32_t), (offsetInByte + bitSize + 7) / 8); |
| 717 |
uint32_t v = 0; |
| 718 |
// this goes from byte alignment to word alignment safely |
| 719 |
memcpy((void*)&v, bytes + wholeBytesToSkip, bytesToCopy); |
| 720 |
|
| 721 |
// shift down so lowest bit is aligned |
| 722 |
v = v >> offsetInByte; |
| 723 |
|
| 724 |
// mask off any extraneous top bits |
| 725 |
const uint32_t mask = ~(0xffffffff << bitSize); |
| 726 |
v &= mask; |
| 727 |
|
| 728 |
return v; |
| 729 |
} |
| 730 |
|
| 731 |
int signExtend(int inValue, size_t bitSize) |
| 732 |
{ |
| 733 |
const int m = 1U << (bitSize - 1); |
| 734 |
return (inValue ^ m) - m; |
| 735 |
} |
| 736 |
|
| 737 |
void writeBits(uint8_t* bytes, size_t bitOffset, size_t bitSize, int value) |
| 738 |
{ |
| 739 |
size_t wholeBytesToSkip = bitOffset >> 3; |
| 740 |
uint8_t* dataByte = bytes + wholeBytesToSkip; |
| 741 |
size_t offsetInByte = bitOffset & 0x7; |
| 742 |
size_t bitsInByte = std::min(bitSize, 8 - offsetInByte); |
| 743 |
uint8_t mask = 0xff >> (8 - bitsInByte); |
| 744 |
|
| 745 |
*dataByte |= ((value & mask) << offsetInByte); |
| 746 |
|
| 747 |
if (bitsInByte < bitSize) { |
| 748 |
// if we have more bits to write, recurse |
| 749 |
writeBits(bytes, bitOffset + bitsInByte, bitSize - bitsInByte, value >> bitsInByte); |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
FGHIDEventInput::FGHIDEventInput() : FGEventInput("Input/HID", "/input/hid"), |
| 754 |
d(new FGHIDEventInputPrivate) |
| 755 |
{ |
| 756 |
d->p = this; // store back pointer to outer object on pimpl |
| 757 |
} |
| 758 |
|
| 759 |
FGHIDEventInput::~FGHIDEventInput() |
| 760 |
{ |
| 761 |
} |
| 762 |
|
| 763 |
void FGHIDEventInput::reinit() |
| 764 |
{ |
| 765 |
SG_LOG(SG_INPUT, SG_INFO, "Re-Initializing HID input bindings"); |
| 766 |
FGHIDEventInput::shutdown(); |
| 767 |
FGHIDEventInput::init(); |
| 768 |
FGHIDEventInput::postinit(); |
| 769 |
} |
| 770 |
|
| 771 |
void FGHIDEventInput::postinit() |
| 772 |
{ |
| 773 |
SG_LOG(SG_INPUT, SG_INFO, "HID event input starting up"); |
| 774 |
|
| 775 |
hid_init(); |
| 776 |
|
| 777 |
hid_device_info* devices = hid_enumerate(0 /* vendor ID */, 0 /* product ID */); |
| 778 |
|
| 779 |
// open each device path only once, it may appear multiple times with different usage value |
| 780 |
std::unordered_set<std::string> seenPaths; |
| 781 |
for (hid_device_info* curDev = devices; curDev != nullptr; curDev = curDev->next) { |
| 782 |
if (curDev->path) { |
| 783 |
std::string pathStr(curDev->path); |
| 784 |
if (seenPaths.find(pathStr) == seenPaths.end()) { |
| 785 |
seenPaths.insert(pathStr); |
| 786 |
d->evaluateDevice(curDev); |
| 787 |
} else { |
| 788 |
std::string _usage = HID::nameForUsage(curDev->usage_page, curDev->usage); |
| 789 |
SG_LOG(SG_INPUT, SG_DEBUG, "Skipping duplicate path " << pathStr << " " |
| 790 |
<< "usage " << _usage << "(0x" << std::hex << curDev->usage_page << ":0x" |
| 791 |
<< std::hex << curDev->usage << ")"); |
| 792 |
} |
| 793 |
} |
| 794 |
} |
| 795 |
hid_free_enumeration(devices); |
| 796 |
} |
| 797 |
|
| 798 |
void FGHIDEventInput::shutdown() |
| 799 |
{ |
| 800 |
SG_LOG(SG_INPUT, SG_INFO, "HID event input shutting down"); |
| 801 |
FGEventInput::shutdown(); |
| 802 |
|
| 803 |
hid_exit(); |
| 804 |
} |
| 805 |
|
| 806 |
// |
| 807 |
// read all elements in each input device |
| 808 |
// |
| 809 |
void FGHIDEventInput::update(double dt) |
| 810 |
{ |
| 811 |
FGEventInput::update(dt); |
| 812 |
} |
| 813 |
|
| 814 |
|
| 815 |
// Register the subsystem. |
| 816 |
SGSubsystemMgr::Registrant<FGHIDEventInput> registrantFGHIDEventInput; |
| 817 |
|
| 818 |
/////////////////////////////////////////////////////////////////////////////////////////////// |
| 819 |
|
| 820 |
void FGHIDEventInput::FGHIDEventInputPrivate::evaluateDevice(hid_device_info* deviceInfo) |
| 821 |
{ |
| 822 |
// allocate an input device, and add to the base class to see if we have |
| 823 |
// a config |
| 824 |
p->AddDevice(new FGHIDDevice(deviceInfo, p)); |
| 825 |
} |
| 826 |
|
| 827 |
/////////////////////////////////////////////////////////////////////////////////////////////// |