-
Notifications
You must be signed in to change notification settings - Fork 14
/
rumble.c
289 lines (245 loc) · 8.19 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
282
283
284
285
286
287
288
289
/*
*
* 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 <errno.h>
#include "compat.h"
#ifndef __WIN32__
#include <sys/ioctl.h>
#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 6
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/event5";
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;
}
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 periodic sinusoidal effect */
effects[0].type = FF_PERIODIC;
effects[0].id = -1;
effects[0].u.periodic.waveform = FF_SINE;
effects[0].u.periodic.period = 0.1*0x100; /* 0.1 second */
effects[0].u.periodic.magnitude = 0x4000; /* 0.5 * Maximum magnitude */
effects[0].u.periodic.offset = 0;
effects[0].u.periodic.phase = 0;
effects[0].direction = 0x4000; /* Along X axis */
effects[0].u.periodic.envelope.attack_length = 0x100;
effects[0].u.periodic.envelope.attack_level = 0;
effects[0].u.periodic.envelope.fade_length = 0x100;
effects[0].u.periodic.envelope.fade_level = 0;
effects[0].trigger.button = 0;
effects[0].trigger.interval = 0;
effects[0].replay.length = 20000; /* 20 seconds */
effects[0].replay.delay = 0;
if (ioctl(event_fd, EVIOCSFF, &effects[0]) == -1) {
fprintf(stderr, "%s: failed to upload sine effect: %s\n",
filename, strerror(errno));
;
}
/* XBOX 360 controller doesn't do these, so don't bother. */
#if 0
/* download a constant effect */
effects[1].type = FF_CONSTANT;
effects[1].id = -1;
effects[1].u.constant.level = 0x2000; /* Strength : 25 % */
effects[1].direction = 0x6000; /* 135 degrees */
effects[1].u.constant.envelope.attack_length = 0x100;
effects[1].u.constant.envelope.attack_level = 0;
effects[1].u.constant.envelope.fade_length = 0x100;
effects[1].u.constant.envelope.fade_level = 0;
effects[1].trigger.button = 0;
effects[1].trigger.interval = 0;
/* effects[1].replay.length = 20000;*/ /* 20 seconds */
effects[1].replay.length = 1000; /* 1 seconds */
effects[1].replay.delay = 0;
if (ioctl(event_fd, EVIOCSFF, &effects[1]) == -1) {
fprintf(stderr, "%s: failed to upload constant effect: %s\n",
filename, strerror(errno));
}
/* download an condition spring effect */
effects[2].type = FF_SPRING;
effects[2].id = -1;
effects[2].u.condition[0].right_saturation = 0x7fff;
effects[2].u.condition[0].left_saturation = 0x7fff;
effects[2].u.condition[0].right_coeff = 0x2000;
effects[2].u.condition[0].left_coeff = 0x2000;
effects[2].u.condition[0].deadband = 0x0;
effects[2].u.condition[0].center = 0x0;
effects[2].u.condition[1] = effects[2].u.condition[0];
effects[2].trigger.button = 0;
effects[2].trigger.interval = 0;
/* effects[2].replay.length = 20000;*/ /* 20 seconds */
effects[2].replay.length = 1000; /* 1 seconds */
effects[2].replay.delay = 0;
if (ioctl(event_fd, EVIOCSFF, &effects[2]) == -1) {
fprintf(stderr, "%s: failed to upload spring effect: %s\n",
filename, strerror(errno));
}
/* download an condition damper effect */
effects[3].type = FF_DAMPER;
effects[3].id = -1;
effects[3].u.condition[0].right_saturation = 0x7fff;
effects[3].u.condition[0].left_saturation = 0x7fff;
effects[3].u.condition[0].right_coeff = 0x2000;
effects[3].u.condition[0].left_coeff = 0x2000;
effects[3].u.condition[0].deadband = 0x0;
effects[3].u.condition[0].center = 0x0;
effects[3].u.condition[1] = effects[3].u.condition[0];
effects[3].trigger.button = 0;
effects[3].trigger.interval = 0;
effects[3].replay.length = 20000; /* 20 seconds */
effects[3].replay.length = 1000; /* 1 seconds */
effects[3].replay.delay = 0;
if (ioctl(event_fd, EVIOCSFF, &effects[3]) == -1) {
fprintf(stderr, "%s: failed to upload damper effect: %s\n",
filename, strerror(errno));
}
#endif
/* a strong rumbling effect */
effects[4].type = FF_RUMBLE;
effects[4].id = -1;
effects[4].u.rumble.strong_magnitude = 0x8000;
effects[4].u.rumble.weak_magnitude = 0;
effects[4].replay.length = 250;
effects[4].replay.delay = 0;
if (ioctl(event_fd, EVIOCSFF, &effects[4]) == -1) {
fprintf(stderr, "%s: failed to upload strong rumbling effect: %s\n",
filename, strerror(errno));
}
/* a weak rumbling effect */
effects[5].type = FF_RUMBLE;
effects[5].id = -1;
effects[5].u.rumble.strong_magnitude = 0x8000;
effects[5].u.rumble.weak_magnitude = 0xc000;
effects[5].replay.length = 250;
effects[5].replay.delay = 0;
if (ioctl(event_fd, EVIOCSFF, &effects[5]) == -1) {
fprintf(stderr, "%s: failed to upload weak rumbling effect: %s\n",
filename, strerror(errno));
}
return 0;
#else
return -1;
#endif
}