Play-/Source/ui_unix/unix/GamePadUtils.cpp

52 lines
1.2 KiB
C++
Raw Normal View History

#include "GamePadUtils.h"
#include <cstring>
2018-05-23 03:11:24 +03:00
#include <string>
#include <cstdio>
2018-11-28 18:37:05 -05:00
GamePadDeviceId CGamePadUtils::GetDeviceID(libevdev* dev)
{
2018-11-28 18:37:05 -05:00
GamePadDeviceId device{0};
2018-04-30 21:01:23 +01:00
if(libevdev_get_uniq(dev) != NULL)
{
if(!CGamePadUtils::ParseMAC(device, libevdev_get_uniq(dev)))
{
const char* tmp_id = libevdev_get_uniq(dev);
if(strlen(tmp_id) >= 6)
{
for(int i = 0; i < strlen(tmp_id) && i < 6; ++i)
device.at(i) = tmp_id[i];
}
}
}
2018-11-28 18:37:05 -05:00
if(device == GamePadDeviceId{0})
{
int vendor = libevdev_get_id_vendor(dev);
int product = libevdev_get_id_product(dev);
int ver = libevdev_get_id_version(dev);
device.at(0) = vendor & 0xFF;
device.at(1) = (vendor >> 8) & 0xFF;
device.at(2) = product & 0xFF;
device.at(3) = (product >> 8) & 0xFF;
device.at(4) = ver & 0xFF;
device.at(5) = (ver >> 8) & 0xFF;
}
return device;
}
2018-11-28 18:37:05 -05:00
bool CGamePadUtils::ParseMAC(GamePadDeviceId& out, std::string const& in)
2018-04-30 21:01:23 +01:00
{
uint32 bytes[6] = {0};
2018-04-30 21:01:23 +01:00
if(std::sscanf(in.c_str(),
"%02x:%02x:%02x:%02x:%02x:%02x",
&bytes[0], &bytes[1], &bytes[2],
&bytes[3], &bytes[4], &bytes[5]) != 6)
{
return false;
}
for(int i = 0; i < 6; ++i)
{
out.at(i) = bytes[i];
}
return true;
}