-
Notifications
You must be signed in to change notification settings - Fork 0
/
joycommand.cpp
172 lines (146 loc) · 4.09 KB
/
joycommand.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
#include <SDL.h>
#include <iostream>
#include <string>
#include <stdexcept>
#include <vector>
#include <boost/variant.hpp>
#include <yaml-cpp/yaml.h>
using namespace std::string_literals;
struct Button
{
SDL_GameControllerButton button;
};
struct Axis
{
SDL_GameControllerAxis axis;
};
using PadInput = boost::variant<Button, Axis>;
struct Mapping
{
std::vector<PadInput> inputs;
std::string command;
};
PadInput stringToPadInput(std::string const& s)
{
if (s == "a") return Button{SDL_CONTROLLER_BUTTON_A};
if (s == "b") return Button{SDL_CONTROLLER_BUTTON_B};
if (s == "x") return Button{SDL_CONTROLLER_BUTTON_X};
if (s == "y") return Button{SDL_CONTROLLER_BUTTON_Y};
if (s == "leftshoulder") return Button{SDL_CONTROLLER_BUTTON_LEFTSHOULDER};
if (s == "lefttrigger") return Axis{SDL_CONTROLLER_AXIS_TRIGGERLEFT};
if (s == "rightshoulder") return Button{SDL_CONTROLLER_BUTTON_RIGHTSHOULDER};
if (s == "righttrigger") return Axis{SDL_CONTROLLER_AXIS_TRIGGERRIGHT};
if (s == "left") return Button{SDL_CONTROLLER_BUTTON_DPAD_LEFT};
if (s == "right") return Button{SDL_CONTROLLER_BUTTON_DPAD_RIGHT};
if (s == "up") return Button{SDL_CONTROLLER_BUTTON_DPAD_UP};
if (s == "down") return Button{SDL_CONTROLLER_BUTTON_DPAD_DOWN};
throw std::runtime_error("unknown button: " + s);
}
using Mappings = std::vector<Mapping>;
Mappings parse_config()
{
Mappings mappings;
YAML::Node config = YAML::LoadFile("config.yaml");
for (auto const item : config)
{
Mapping mapping;
for (auto const button : item["buttons"])
mapping.inputs.push_back(stringToPadInput(button.as<std::string>()));
mapping.command = item["command"].as<std::string>();
mappings.push_back(mapping);
}
return mappings;
}
struct DeviceCloser
{
void operator()(SDL_GameController* controller)
{
std::cout << "closed controller " << SDL_GameControllerName(controller) << std::endl;
SDL_GameControllerClose(controller);
}
};
std::map<SDL_JoystickID, std::unique_ptr<SDL_GameController, DeviceCloser>> g_gameController;
void open_controller(int index)
{
auto const controller = SDL_GameControllerOpen(index);
if (controller == nullptr)
{
std::cout << "Warning: unable to open game controller: " << SDL_GetError() << std::endl;
}
else
{
g_gameController[SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(controller))].reset(controller);
std::cout << "opened controller " << SDL_GameControllerName(controller) << std::endl;
}
}
void init()
{
if (SDL_Init(SDL_INIT_GAMECONTROLLER) < 0)
throw std::runtime_error("SDL could not initialize: "s + SDL_GetError());
}
void close()
{
g_gameController.clear();
SDL_Quit();
}
void main_loop()
{
auto const mappings = parse_config();
SDL_Event e;
while (SDL_WaitEvent(&e) != 0)
{
if (e.type == SDL_QUIT)
break;
switch (e.type) {
//case SDL_CONTROLLERAXISMOTION:
case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP:
for (auto const& controller : g_gameController)
{
bool ok;
for (auto const& mapping : mappings)
{
ok = true;
for (auto const& input : mapping.inputs)
{
if (auto const button = boost::get<Button>(&input))
if (!SDL_GameControllerGetButton(controller.second.get(), button->button))
{
ok = false;
break;
}
if (auto const axis = boost::get<Axis>(&input))
if (SDL_GameControllerGetAxis(controller.second.get(), axis->axis) == 0)
{
ok = false;
break;
}
}
if (ok)
{
system(mapping.command.c_str());
break;
}
}
if (ok)
break;
}
break;
case SDL_CONTROLLERDEVICEADDED:
open_controller(e.cdevice.which);
break;
case SDL_CONTROLLERDEVICEREMOVED:
g_gameController.erase(e.cdevice.which);
break;
//case SDL_CONTROLLERDEVICEREMAPPED:
;
}
}
}
int main(int argc, char* args[])
{
init();
main_loop();
close();
return 0;
}