forked from wgalen/stagekit
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstagekit.c
272 lines (250 loc) · 7.06 KB
/
stagekit.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
*
* This code is modified version of the fftest.c program
* which Tests the force feedback driver by Johan Deneux.
* Modifications to incorporate into Word War vi
* by Stephen M.Cameron
*
* Additional modifications to create a support library for the Rock Band Stage Kit from PDP
* by Wayne M. Galen and David Maher
*
* Copyright 2001-2002 Johann Deneux <[email protected]>
* Copyright 2008 Stephen M. Cameron <[email protected]>
* Copyright 2010 Wayne M. Galen <[email protected]>
* Copyright 2019-2020 David Maher
*/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <stdbool.h>
#include <stdio.h>
#include <errno.h>
#include "stagekit.h"
#ifdef __linux__
#define HAS_LINUX_JOYSTICK_INTERFACE 1
#endif
#ifdef HAS_LINUX_JOYSTICK_INTERFACE
#include <linux/input.h>
static int event_fd;
static unsigned long features[4];
static struct ff_effect effect;
#endif
#define BITS_PER_LONG (sizeof(long) * 8)
#define OFF(x) ((x) % BITS_PER_LONG)
#define BIT(x) (1UL << OFF(x))
#define LONG(x) ((x) / BITS_PER_LONG)
#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
int send_raw_value(unsigned short left, unsigned short right, char** errorStr)
{
#ifdef HAS_LINUX_JOYSTICK_INTERFACE
struct input_event play;
// download the effect
effect.u.rumble.strong_magnitude = left;
effect.u.rumble.weak_magnitude = right;
if (ioctl(event_fd, EVIOCSFF, &effect) == -1)
{
char errorMsg[256];
snprintf(errorMsg, 255, "failed to upload effect: %s\n", strerror(errno));
*errorStr = strdup(errorMsg);
return -1;
}
play.type = EV_FF;
play.code = effect.id;
play.value = 1;
if (write(event_fd, (const void*)&play, sizeof(play)) == -1)
{
*errorStr = strdup("write returned -1");
return -1;
}
return 0;
#else
return -10;
#endif
}
int sk_close(char** errorStr)
{
#ifdef HAS_LINUX_JOYSTICK_INTERFACE
close(event_fd);
return 0;
#else
return -10;
#endif
}
int sk_init(char* filename, char** foundfile, char** errorStr)
{
#ifdef HAS_LINUX_JOYSTICK_INTERFACE
if (filename == NULL)
{
struct dirent* dp;
const char* dir_path = "/dev/input";
DIR* dir = opendir(dir_path);
// No event interface file passed. Probing for stagekit.
struct input_id device_info;
bool found = false;
while ((dp = readdir(dir)) != NULL)
{
if (strspn(dp->d_name, "event"))
{
char tryfile[256];
strcpy(tryfile, dir_path);
strcat(tryfile, "/");
strcat(tryfile, dp->d_name);
event_fd = open(tryfile, O_RDWR);
if (!(event_fd < 0))
{
ioctl(event_fd, EVIOCGID, &device_info);
if ((device_info.vendor == 0x0e6f) && (device_info.product == 0x0103))
{
// Stage kit found
filename = tryfile;
found = true;
break;
}
else
{
close(event_fd);
}
}
}
}
if (!found)
{
*errorStr = strdup("No StageKit found");
return -1;
}
}
else
{
event_fd = open(filename, O_RDWR);
if (event_fd < 0)
{
char errorMsg[256];
snprintf(errorMsg, 255, "Can't open %s: %s", filename, strerror(errno));
*errorStr = strdup(errorMsg);
return -1;
}
}
// Query device
if (ioctl(event_fd, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features) == -1)
{
char errorMsg[256];
snprintf(errorMsg, 255, "Query of rumble device failed: %s:%s", filename, strerror(errno));
*errorStr = strdup(errorMsg);
return -1;
}
// download a lights out effect
effect.type = FF_RUMBLE;
effect.id = -1;
effect.direction = 0;
effect.trigger.button = 0;
effect.trigger.interval = 0;
if (ioctl(event_fd, EVIOCSFF, &effect) == -1)
{
char errorMsg[256];
snprintf(errorMsg, 255, "%s: failed to allocate effect: %s", filename, strerror(errno));
*errorStr = strdup(errorMsg);
return -1;
}
*foundfile = strdup(filename);
return 0;
#else
return -10;
#endif
}
int sk_alloff(char** errorStr)
{
int error = send_raw_value(0, STAGEKIT_ALLOFF, errorStr);
usleep(10000);
if (error != 0)
{
return error;
}
// sending this twice ensures the command is recieved. in testing, it would sometimes get stuck with only one off call. no clue why
error = send_raw_value(0, STAGEKIT_ALLOFF, errorStr);
usleep(10000);
return error;
}
int sk_setstrobe(unsigned short speed, char** errorStr)
{
int error;
if (speed == 0)
{
error = send_raw_value(0, STAGEKIT_NO_STROBE, errorStr);
}
else if (speed == 1)
{
error = send_raw_value(0, STAGEKIT_SLOW_STROBE, errorStr);
}
else if (speed == 2)
{
error = send_raw_value(0, STAGEKIT_MEDIUM_STROBE, errorStr);
}
else if (speed == 3)
{
error = send_raw_value(0, STAGEKIT_FAST_STROBE, errorStr);
}
else if (speed == 4)
{
error = send_raw_value(0, STAGEKIT_FASTEST_STROBE, errorStr);
}
else
{
return -1;
}
usleep(10000);
return error;
}
int sk_setfog(bool fog, char** errorStr)
{
int error;
if (fog)
{
error = send_raw_value(0, STAGEKIT_FOG_ON, errorStr);
}
else
{
error = send_raw_value(0, STAGEKIT_FOG_OFF, errorStr);
}
usleep(10000);
return error;
}
int sk_setred(unsigned short red, char** errorStr)
{
int error = send_raw_value(red << 8, STAGEKIT_RED, errorStr);
usleep(10000);
return error;
}
int sk_setyellow(unsigned short yellow, char** errorStr)
{
int error = send_raw_value(yellow << 8, STAGEKIT_YELLOW, errorStr);
usleep(10000);
return error;
}
int sk_setgreen(unsigned short green, char** errorStr)
{
int error = send_raw_value(green << 8, STAGEKIT_GREEN, errorStr);
usleep(10000);
return error;
}
int sk_setblue(unsigned short blue, char** errorStr)
{
int error = send_raw_value(blue << 8, STAGEKIT_BLUE, errorStr);
usleep(10000);
return error;
}