MCPcopy Create free account
hub / github.com/Aloshi/EmulationStation / getInputDevices

Method getInputDevices

src/InputManager.cpp:40–118  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

38}
39
40std::vector<InputDevice> InputManager::getInputDevices() const
41{
42 std::vector<InputDevice> currentDevices;
43
44 //retrieve all input devices from system
45#if defined (__APPLE__)
46 #error TODO: Not implemented for MacOS yet!!!
47#elif defined(__linux__)
48 //open linux input devices file system
49 const std::string inputPath("/dev/input");
50 fs::directory_iterator dirIt(inputPath);
51 while (dirIt != fs::directory_iterator()) {
52 //get directory entry
53 std::string deviceName = (*dirIt).path().string();
54 //remove parent path
55 deviceName.erase(0, inputPath.length() + 1);
56 //check if it start with "js"
57 if (deviceName.length() >= 3 && deviceName.find("js") == 0) {
58 //looks like a joystick. add to devices.
59 currentDevices.push_back(InputDevice(deviceName, 0, 0));
60 }
61 ++dirIt;
62 }
63 //or dump /proc/bus/input/devices anbd search for a Handler=..."js"... entry
64#elif defined(WIN32) || defined(_WIN32)
65 RAWINPUTDEVICELIST * deviceList = nullptr;
66 UINT nrOfDevices = 0;
67 //get number of input devices
68 if (GetRawInputDeviceList(deviceList, &nrOfDevices, sizeof(RAWINPUTDEVICELIST)) != -1 && nrOfDevices > 0)
69 {
70 //get list of input devices
71 deviceList = new RAWINPUTDEVICELIST[nrOfDevices];
72 if (GetRawInputDeviceList(deviceList, &nrOfDevices, sizeof(RAWINPUTDEVICELIST)) != -1)
73 {
74 //loop through input devices
75 for (unsigned int i = 0; i < nrOfDevices; i++)
76 {
77 //get device name
78 char * rawName = new char[2048];
79 UINT rawNameSize = 2047;
80 GetRawInputDeviceInfo(deviceList[i].hDevice, RIDI_DEVICENAME, (void *)rawName, &rawNameSize);
81 //null-terminate string
82 rawName[rawNameSize] = '\0';
83 //convert to string
84 std::string deviceName = rawName;
85 delete [] rawName;
86 //get deviceType
87 RID_DEVICE_INFO deviceInfo;
88 UINT deviceInfoSize = sizeof(RID_DEVICE_INFO);
89 GetRawInputDeviceInfo(deviceList[i].hDevice, RIDI_DEVICEINFO, (void *)&deviceInfo, &deviceInfoSize);
90 //check if it is a HID. we ignore keyboards and mice...
91 if (deviceInfo.dwType == RIM_TYPEHID)
92 {
93 //check if the vendor/product already exists in list. yes. could be more elegant...
94 std::vector<InputDevice>::const_iterator cdIt = currentDevices.cbegin();
95 while (cdIt != currentDevices.cend())
96 {
97 if (cdIt->name == deviceName && cdIt->product == deviceInfo.hid.dwProductId && cdIt->vendor == deviceInfo.hid.dwVendorId)

Callers

nothing calls this directly

Calls 5

InputDeviceClass · 0.85
stringMethod · 0.80
pathMethod · 0.80
findMethod · 0.80
push_backMethod · 0.80

Tested by

no test coverage detected