-
Notifications
You must be signed in to change notification settings - Fork 1
/
sdl2deviceprober.cpp
91 lines (68 loc) · 1.85 KB
/
sdl2deviceprober.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "sdl2deviceprober.h"
#include <QtWidgets/QMessageBox>
bool SDL2DeviceProber::s_SDLInited = false;
bool SDL2DeviceProber::initializeSDL()
{
int ret;
if (s_SDLInited)
return true;
ret = SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC);
if (ret == 0) {
s_SDLInited = true;
return true;
}
QMessageBox::warning(nullptr, "SDL2 error",
QString("Unable to initialize SDL2 (%1). SDL2 interface will not be available.").arg(SDL_GetError()));
return false;
}
void SDL2DeviceProber::closeAllDevices()
{
if (!s_SDLInited)
return;
return;
}
DeviceProber::DeviceList SDL2DeviceProber::listDevices()
{
DeviceProber::DeviceList devList;
int numHapt;
if (!s_SDLInited)
return devList;
numHapt = SDL_NumHaptics();
for (int idx = 0; idx < numHapt; idx++) {
DeviceProber::DeviceInfo dinfo;
dinfo.name = QString(SDL_HapticName(idx));
dinfo.id = QVariant(idx);
devList.push_back(dinfo);
}
return devList;
}
std::shared_ptr<FFBDevice> SDL2DeviceProber::openDevice(const QString& id)
{
SDL_Haptic* haptic;
std::shared_ptr<SDL2FFBDevice> device;
int idx;
int maxEffectCount;
bool ok;
if (!s_SDLInited)
return nullptr;
idx = id.toInt(&ok);
if (!ok)
return nullptr;
haptic = SDL_HapticOpen(idx);
if (haptic == nullptr)
return nullptr;
maxEffectCount = SDL_HapticNumEffects(haptic);
if (maxEffectCount < 1) {
QMessageBox::critical(nullptr, "SDL2 device error", "Maximum effect count for this device is zero.");
SDL_HapticClose(haptic);
return nullptr;
}
device = std::make_shared<SDL2FFBDevice>(haptic, maxEffectCount);
if (!device->queryDeviceCapabilities()) {
QMessageBox::critical(nullptr, "SDL2 device error", "Unable to query device capabilities.");
device->close();
return nullptr;
}
m_openedDevices.push_back(device);
return device;
}