-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
195 lines (165 loc) · 5.56 KB
/
main.c
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#define _XOPEN_SOURCE 700
#include "main.h"
int main(int argc, char* argv[]) {
init();
if (argc > 1) load_mappings(argv[1]);
printf("Listening for gamepads...\n");
while (1) {
SDL_WaitEvent(NULL);
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit();
}
else if (event.type == SDL_CONTROLLERDEVICEADDED) {
printf("Gamepad connected (index: %d)\n", event.cdevice.which);
add_controller(event.cdevice.which);
}
else if (event.type == SDL_CONTROLLERDEVICEREMOVED) {
printf("Gamepad disconnected (instance id: %d)\n",
event.cdevice.which);
// Close the controller and free its slot in our array (if it
// is currently in our array)
remove_controller(event.cdevice.which);
}
else if (event.type == SDL_CONTROLLERBUTTONDOWN) {
process_button(event.cbutton.which, event.cbutton.button);
}
}
}
return 0;
}
void add_controller(int32_t deviceIndex) {
// Find an empty slot in the controller array
for (int i = 0; i < MAX_CONTROLLERS; i++) {
if (controllers[i] == NULL) {
// Open the controller
controllers[i] = SDL_GameControllerOpen(deviceIndex);
return;
}
}
printf("No more controllers can be added\n");
}
SDL_GameController* get_controller(SDL_JoystickID id) {
// Search the controller array for the controller that has the specified
// SDL_JoystickID
for (int i = 0; i < MAX_CONTROLLERS; i++) {
if (controllers[i]) {
if (get_joystick_id(controllers[i]) == id) {
return controllers[i];
}
}
}
return NULL;
}
SDL_JoystickID get_joystick_id(SDL_GameController* controller) {
// Get the joystick underlying the controller
SDL_Joystick* joystick = SDL_GameControllerGetJoystick(controller);
// Return the instance ID of the joystick
return SDL_JoystickInstanceID(joystick);
}
uint8_t guide_is_down(SDL_GameController* controller) {
return SDL_GameControllerGetButton(
controller,
SDL_CONTROLLER_BUTTON_GUIDE);
}
void init() {
init_pid_file();
if (SDL_Init(SDL_INIT_GAMECONTROLLER) != 0) {
fprintf(stderr, "\nUnable to initialize SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(remove_pid_file);
atexit(SDL_Quit);
MAX_CONTROLLERS = 4;
controllers = malloc(sizeof(SDL_GameController*) * MAX_CONTROLLERS);
for (int i = 0; i < MAX_CONTROLLERS; i++) {
controllers[i] = NULL;
}
}
void init_pid_file() {
PID_FILENAME = "/tmp/gamepad-macro-daemon.pid";
// Open the PID file for writing (or create it if it does not exist)
pidFileStream = fopen(PID_FILENAME, "w");
// If the PID file was not opened successfully
if (pidFileStream == NULL) {
fprintf(stderr, "Cannot open PID file %s for writing\n", PID_FILENAME);
exit(1);
}
// If the PID file is locked
if (lockf(fileno(pidFileStream), F_TEST, 0) == -1) {
fprintf(stderr, "Another instance is already running\n");
exit(1);
}
else {
// Lock the PID file
lockf(fileno(pidFileStream), F_LOCK, 0);
// Write the PID to the PID file
fprintf(pidFileStream, "%d\n", getpid());
fflush(pidFileStream);
}
}
void load_mappings(const char* filename) {
int result = SDL_GameControllerAddMappingsFromFile(filename);
if (result == -1) {
printf("Error loading controller mappings\n");
}
else {
printf("Loaded %d controller mappings\n", result);
}
}
void process_button(SDL_JoystickID jsId, SDL_GameControllerButton button) {
SDL_GameController* controller = get_controller(jsId);
// If a controller with the specified joystick ID was found in our array
if (controller) {
// If the "guide" button is down on this controller
if (guide_is_down(controller)) {
switch (button) {
case SDL_CONTROLLER_BUTTON_LEFTSHOULDER:
printf("Launching Steam\n");
system("/usr/bin/steam");
break;
case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER:
printf("Launching Kodi\n");
system("/usr/bin/kodi");
break;
case SDL_CONTROLLER_BUTTON_LEFTSTICK:
printf("Launching RetroArch\n");
system("/usr/bin/retroarch");
break;
default:
break;
}
}
}
}
void quit() {
free(controllers);
exit(0);
}
void remove_controller(SDL_JoystickID id) {
SDL_GameController* controller = get_controller(id);
// If the device that was just removed is an existing controller in our
// array
if (controller) {
// Close the controller
SDL_GameControllerClose(controller);
vacate_controller_array_slot(controller);
}
}
void remove_pid_file() {
// Close the PID file (and release the lock)
fclose(pidFileStream);
// Delete the PID file
if (unlink(PID_FILENAME) != 0) {
fprintf(stderr, "Cannot delete PID file %s\n", PID_FILENAME);
}
}
void vacate_controller_array_slot(SDL_GameController* controller) {
for (int i = 0; i < MAX_CONTROLLERS; i++) {
if (controllers[i] == controller) {
controllers[i] = NULL;
return;
}
}
}