-
Notifications
You must be signed in to change notification settings - Fork 1
/
rumble.c
281 lines (238 loc) · 7.35 KB
/
rumble.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
273
274
275
276
277
278
279
280
281
/*
*
* 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
*
* Copyright 2001-2002 Johann Deneux <deneux@ifrance.com>
* Copyright 2008 Stephen M. Cameron <smcameron@yahoo.com>
*/
/*
* 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
*
* You can contact the author by email at this address:
* Johann Deneux <deneux@ifrance.com>
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <errno.h>
#include "rumble.h"
#include "stagekit.h"
#ifdef __linux__
#define HAS_LINUX_JOYSTICK_INTERFACE 1
#endif
#ifdef HAS_LINUX_JOYSTICK_INTERFACE
#include <linux/input.h>
#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)
#define N_EFFECTS 5
char* effect_names[] = {
"Sine vibration",
"Constant Force",
"Spring Condition",
"Damping Condition",
"Strong Rumble",
"Weak Rumble"
};
#ifdef HAS_LINUX_JOYSTICK_INTERFACE
static int event_fd;
static char *default_event_file = "/dev/input/event6";
static int n_effects; /* Number of effects the device can play at the same time */
static unsigned long features[4];
static struct ff_effect effects[N_EFFECTS];
#endif /* HAS_LINUX_JOYSTICK_INTERFACE */
int stop_all_rumble_effects(void)
{
#ifdef HAS_LINUX_JOYSTICK_INTERFACE
int i;
struct input_event stop;
for (i=0; i<N_EFFECTS; ++i) {
stop.type = EV_FF;
stop.code = effects[i].id;
stop.value = 0;
if (write(event_fd, (const void*) &stop, sizeof(stop)) == -1) {
perror("Stop effect");
exit(1);
}
}
#endif
return 0;
}
int play_rumble_effect(int effect)
{
#ifdef HAS_LINUX_JOYSTICK_INTERFACE
struct input_event play;
if (effect < 0 || effect >= N_EFFECTS)
return -1;
play.type = EV_FF;
play.code = effects[effect].id;
play.value = 1;
if (write(event_fd, (const void*) &play, sizeof(play)) == -1)
return -1;
#endif
return 0;
}
int send_raw_value(unsigned short left, unsigned short right)
{
#ifdef HAS_LINUX_JOYSTICK_INTERFACE
struct input_event play;
/* download the effect */
effects[0].u.rumble.strong_magnitude = left;
effects[0].u.rumble.weak_magnitude = right;
if (ioctl(event_fd, EVIOCSFF, &effects[0]) == -1) {
printf("failed to upload effect: %s\n", strerror(errno));
;
}
play.type=EV_FF;
play.code=effects[0].id;
play.value=1;
if (write(event_fd,(const void*) &play, sizeof(play)) == -1)
return -1;
#endif
return 0;
}
void close_rumble_fd(void)
{
#ifdef HAS_LINUX_JOYSTICK_INTERFACE
close(event_fd);
#endif
}
int get_ready_to_rumble(char *filename)
{
#ifdef HAS_LINUX_JOYSTICK_INTERFACE
if (filename == NULL)
filename = default_event_file;
event_fd = open(filename, O_RDWR);
if (event_fd < 0) {
fprintf(stderr, "Can't open %s: %s\n",
filename, strerror(errno));
return -1;
}
printf("Device %s opened\n", filename);
/* Query device */
if (ioctl(event_fd, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features) == -1) {
fprintf(stderr, "Query of rumble device failed: %s:%s\n",
filename, strerror(errno));
return -1;
}
printf("Axes query: ");
if (test_bit(ABS_X, features)) printf("Axis X ");
if (test_bit(ABS_Y, features)) printf("Axis Y ");
if (test_bit(ABS_WHEEL, features)) printf("Wheel ");
printf("\nEffects: ");
if (test_bit(FF_CONSTANT, features)) printf("Constant ");
if (test_bit(FF_PERIODIC, features)) printf("Periodic ");
if (test_bit(FF_SPRING, features)) printf("Spring ");
if (test_bit(FF_FRICTION, features)) printf("Friction ");
if (test_bit(FF_RUMBLE, features)) printf("Rumble ");
printf("\nNumber of simultaneous effects: ");
if (ioctl(event_fd, EVIOCGEFFECTS, &n_effects) == -1) {
fprintf(stderr, "Query of number of simultaneous "
"effects failed, assuming 1. %s:%s\n",
filename, strerror(errno));
n_effects = 1; /* assume 1. */
}
printf("%d\n", n_effects);
/* download a lights out effect */
effects[0].type = FF_RUMBLE;
effects[0].id = -1;
effects[0].u.rumble.strong_magnitude = 0x0;
effects[0].u.rumble.weak_magnitude = STAGEKIT_ALLOFF;
effects[0].direction = 0;
effects[0].trigger.button = 0;
effects[0].trigger.interval = 0;
effects[0].replay.length = EFFECT_LENGTH;
effects[0].replay.delay = EFFECT_DELAY;
if (ioctl(event_fd, EVIOCSFF, &effects[0]) == -1) {
fprintf(stdout, "%s: failed to upload AllOff effect: %s\n",
filename, strerror(errno));
;
}
/* Load a slow strobe */
effects[1].type = FF_RUMBLE;
effects[1].id = -1;
effects[1].u.rumble.strong_magnitude = 0x0;
effects[1].u.rumble.weak_magnitude = STAGEKIT_SLOW_STROBE;
effects[1].direction = 0;
effects[1].trigger.button = 0;
effects[1].trigger.interval = 0;
effects[1].replay.length = EFFECT_LENGTH;
effects[1].replay.delay = EFFECT_DELAY;
if (ioctl(event_fd, EVIOCSFF, &effects[1]) == -1) {
fprintf(stdout, "%s: failed to upload SlowStrobe effect: %s\n",
filename, strerror(errno));
;
}
/* Load a medium strobe */
effects[2].type = FF_RUMBLE;
effects[2].id = -1;
effects[2].u.rumble.strong_magnitude = 0x0;
effects[2].u.rumble.weak_magnitude = STAGEKIT_MEDIUM_STROBE;
effects[2].direction = 0;
effects[2].trigger.button = 0;
effects[2].trigger.interval = 0;
effects[2].replay.length = EFFECT_LENGTH;
effects[2].replay.delay = EFFECT_DELAY;
if (ioctl(event_fd, EVIOCSFF, &effects[2]) == -1) {
fprintf(stdout, "%s: failed to upload MediumStrobe effect: %s\n",
filename, strerror(errno));
;
}
/*Load a fast strobe */
effects[3].type = FF_RUMBLE;
effects[3].id = -1;
effects[3].u.rumble.strong_magnitude = 0x0;
effects[3].u.rumble.weak_magnitude = STAGEKIT_FAST_STROBE;
effects[3].direction = 0;
effects[3].trigger.button = 0;
effects[3].trigger.interval = 0;
effects[3].replay.length = EFFECT_LENGTH;
effects[3].replay.delay = EFFECT_DELAY;
if (ioctl(event_fd, EVIOCSFF, &effects[3]) == -1) {
fprintf(stdout, "%s: failed to upload FastStrobe effect: %s\n",
filename, strerror(errno));
;
}
/* Load the fastest strobe */
effects[4].type = FF_RUMBLE;
effects[4].id = -1;
effects[4].u.rumble.strong_magnitude = 0x0;
effects[4].u.rumble.weak_magnitude = STAGEKIT_FASTEST_STROBE;
effects[4].direction = 0;
effects[4].trigger.button = 0;
effects[4].trigger.interval = 0;
effects[4].replay.length = EFFECT_LENGTH;
effects[4].replay.delay = EFFECT_DELAY;
if (ioctl(event_fd, EVIOCSFF, &effects[4]) == -1) {
fprintf(stdout, "%s: failed to upload FastestStrobe effect: %s\n",
filename, strerror(errno));
;
}
return 0;
#else
return -1;
#endif
}