-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstagekit.c
182 lines (161 loc) · 4.99 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
/*
*
* 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
*
* Copyright 2001-2002 Johann Deneux <[email protected]>
* Copyright 2008 Stephen M. Cameron <[email protected]>
* Copyright 2010 Wayne M. Galen <[email protected]>
*/
/*
* 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:
* Wayne M. Galen <[email protected]>
*/
#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 <dirent.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)
#ifdef HAS_LINUX_JOYSTICK_INTERFACE
static int event_fd;
static char *default_event_file = "/dev/input/event6";
static unsigned long features[4];
static struct ff_effect effect;
#endif /* HAS_LINUX_JOYSTICK_INTERFACE */
int send_raw_value(unsigned short left, unsigned short right)
{
#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) {
printf("failed to upload effect: %s\n", strerror(errno));
;
}
play.type=EV_FF;
play.code=effect.id;
play.value=1;
if (write(event_fd,(const void*) &play, sizeof(play)) == -1)
return -1;
#endif
return 0;
}
void sk_close(void)
{
#ifdef HAS_LINUX_JOYSTICK_INTERFACE
close(event_fd);
#endif
}
int sk_init(char *filename)
{
#ifdef HAS_LINUX_JOYSTICK_INTERFACE
if (filename == NULL)
{
int i;
struct dirent *dp;
const char *dir_path="/dev/input";
DIR *dir = opendir(dir_path);
printf("No event interface file passed. Probing for stagekit.\n");
struct input_id device_info;
while((dp=readdir(dir))!=NULL)
{
if (strspn(dp->d_name,"event"))
{
char tryfile[256]="";
//printf("%i. %s\n", i, dp->d_name);
strcpy(tryfile, dir_path);
strcat(tryfile,"/");
strcat(tryfile,dp->d_name);
printf("Looking for stage kit on %s\n",tryfile);
event_fd=open(tryfile, O_RDWR);
if (event_fd < 0) {
fprintf(stderr, "Can't open %s: %s\n",
tryfile, strerror(errno));
}
else
{
ioctl(event_fd, EVIOCGID, &device_info);
//printf("vendor %04hx product %04hx version %04hx", device_info.vendor, device_info.product, device_info.version);
if ((device_info.vendor == 0x0e6f) && (device_info.product == 0x0103))
{
printf("Stage kit found on %s\n",tryfile);
break;
}
else
{
close(event_fd);
}
}
}
}
}
else
{
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;
}
/* 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) {
fprintf(stdout, "%s: failed to allocate effect: %s\n",
filename, strerror(errno));
}
//sk_alloff();
return 0;
#else
return -1;
#endif
}