-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamepad.cpp
181 lines (165 loc) · 4.89 KB
/
gamepad.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "detect_os.hpp"
#ifndef PAZ_LINUX
#include "gamepad.hpp"
#include "controller_db.hpp"
#include <sstream>
#ifdef PAZ_MACOS
static const std::string Platform = "Mac OS X";
#elif defined(PAZ_WINDOWS)
static const std::string Platform = "Windows";
#endif
#define CASE0(a, b) {#a, paz::GamepadButton::b},
#define CASE1(a, b) {#a, b},
static bool parse_mapping(const std::string& str, paz::GamepadMapping& m)
{
static const std::unordered_map<std::string, int> axisIndices =
{
CASE1(leftx, 0)
CASE1(lefty, 1)
CASE1(rightx, 2)
CASE1(righty, 3)
CASE1(lefttrigger, 4)
CASE1(righttrigger, 5)
};
static const std::unordered_map<std::string, paz::GamepadButton>
buttonNames =
{
CASE0(a, A)
CASE0(b, B)
CASE0(x, X)
CASE0(y, Y)
CASE0(back, Back)
CASE0(start, Start)
CASE0(guide, Guide)
CASE0(leftshoulder, LeftBumper)
CASE0(rightshoulder, RightBumper)
CASE0(leftstick, LeftThumb)
CASE0(rightstick, RightThumb)
};
static const std::unordered_map<std::string, paz::GamepadButton> hatNames =
{
CASE0(dpup, Up)
CASE0(dpright, Right)
CASE0(dpdown, Down)
CASE0(dpleft, Left)
};
// Check platform.
if(str.substr(str.find("platform:") + 9, Platform.size()) != Platform)
{
return false;
}
std::stringstream ss(str);
std::string line;
// Skip name.
std::getline(ss, line, ',');
// Get mapping.
while(std::getline(ss, line, ','))
{
// GLFW does not currently support input modifiers, so neither will we.
if(line.empty() || line[0] == '-' || line[0] == '+')
{
return false;
}
const std::size_t splitPos = line.find(':');
if(splitPos == std::string::npos)
{
return false;
}
const std::string key = line.substr(0, splitPos);
const std::string val = line.substr(splitPos + 1);
if(val.size() < 2)
{
return false;
}
int min = -1;
int max = 1;
std::size_t startPos = 0;
if(val[0] == '-')
{
min = 0;
++startPos;
}
if(val[0] == '+')
{
max = 0;
++startPos;
}
if(val[startPos] == 'a')
{
if(axisIndices.count(key))
{
const std::size_t endPos = val.find('~');
const int idx = axisIndices.at(key);
m.axes[idx].type = paz::GamepadElementType::Axis;
m.axes[idx].idx = std::stoi(val.substr(startPos + 1, endPos));
if(max == min)
{
return false;
}
m.axes[idx].axisScale = 2/(max - min);
m.axes[idx].axisOffset = -(max + min);
if(endPos != std::string::npos)
{
m.axes[idx].axisScale = -m.axes[idx].axisScale;
m.axes[idx].axisOffset = -m.axes[idx].axisOffset;
}
}
}
else if(val[startPos] == 'b')
{
if(buttonNames.count(key))
{
const int idx = static_cast<int>(buttonNames.at(key));
m.buttons[idx].type = paz::GamepadElementType::Button;
m.buttons[idx].idx = std::stoi(val.substr(startPos + 1));
}
}
else if(val[startPos] == 'h')
{
if(hatNames.count(key))
{
const int idx = static_cast<int>(hatNames.at(key));
m.buttons[idx].type = paz::GamepadElementType::HatBit;
std::size_t pos = val.find('.');
if(pos == std::string::npos)
{
return false;
}
const unsigned long hat = std::stoi(val.substr(startPos + 1, pos
- 1));
const unsigned long bit = std::stoi(val.substr(pos + 1));
m.buttons[idx].idx = static_cast<std::uint8_t>((hat << 4)|bit);
}
}
}
return true;
}
const std::unordered_map<std::string, paz::GamepadMapping>& paz::
gamepad_mappings()
{
static const auto res = []()
{
std::unordered_map<std::string, GamepadMapping> mappings;
std::stringstream iss(ControllerDb);
std::string line;
while(std::getline(iss, line))
{
if(line.empty() || line[0] == '#')
{
continue;
}
const std::string guid = line.substr(0, 32);
if(!mappings.count(guid))
{
GamepadMapping m;
if(parse_mapping(line.substr(33), m))
{
mappings[guid] = m;
}
}
}
return mappings;
}();
return res;
}
#endif