Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
222 lines (174 loc) · 4.7 KB

SDL_syshaptic.c

File metadata and controls

222 lines (174 loc) · 4.7 KB
 
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
/*
SDL - Simple DirectMedia Layer
Copyright (C) 2008 Edgar Simo
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
#ifdef SDL_JOYSTICK_LINUX
#include "SDL_haptic.h"
#include "../SDL_haptic_c.h"
#include "../SDL_syshaptic.h"
#include <unistd.h> /* close */
#include <linux/input.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/limits.h>
#include <string.h>
#define MAX_HAPTICS 32
Jun 1, 2008
Jun 1, 2008
43
44
45
/*
* List of available haptic devices.
*/
46
47
48
49
50
51
static struct
{
char *fname;
SDL_Haptic *haptic;
} SDL_hapticlist[MAX_HAPTICS];
Jun 1, 2008
Jun 1, 2008
52
53
54
55
/*
* Haptic system hardware data.
*/
56
57
58
59
60
61
struct haptic_hwdata
{
int fd;
};
Jun 1, 2008
Jun 1, 2008
62
63
64
65
66
#define test_bit(nr, addr) \
(((1UL << ((nr) & 31)) & (((const unsigned int *) addr)[(nr) >> 5])) != 0)
#define EV_TEST(ev,f) \
if (test_bit((ev), features)) ret |= (f);
Jun 1, 2008
Jun 1, 2008
67
68
69
70
/*
* Test whether a device has haptic properties.
* Returns available properties or 0 if there are none.
*/
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
static int
EV_IsHaptic(int fd)
{
unsigned int ret;
unsigned long features[1 + FF_MAX/sizeof(unsigned long)];
ret = 0;
ioctl(fd, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features);
EV_TEST(FF_CONSTANT, SDL_HAPTIC_CONSTANT);
EV_TEST(FF_PERIODIC, SDL_HAPTIC_PERIODIC);
EV_TEST(FF_RAMP, SDL_HAPTIC_RAMP);
EV_TEST(FF_SPRING, SDL_HAPTIC_SPRING);
EV_TEST(FF_FRICTION, SDL_HAPTIC_FRICTION);
EV_TEST(FF_DAMPER, SDL_HAPTIC_DAMPER);
EV_TEST(FF_RUMBLE, SDL_HAPTIC_RUMBLE);
EV_TEST(FF_INERTIA, SDL_HAPTIC_INERTIA);
EV_TEST(FF_GAIN, SDL_HAPTIC_GAIN);
EV_TEST(FF_AUTOCENTER, SDL_HAPTIC_AUTOCENTER);
return ret;
}
int
SDL_SYS_HapticInit(void)
{
const char joydev_pattern[] = "/dev/input/event%d";
dev_t dev_nums[MAX_HAPTICS];
char path[PATH_MAX];
struct stat sb;
int fd;
int i, j, k;
int duplicate;
int numhaptics;
numhaptics = 0;
i = 0;
for (j = 0; j < MAX_HAPTICS; ++j) {
snprintf(path, PATH_MAX, joydev_pattern, i++);
/* check to see if file exists */
if (stat(path,&sb) != 0)
break;
/* check for duplicates */
duplicate = 0;
for (k = 0; (k < numhaptics) && !duplicate; ++k) {
if (sb.st_rdev == dev_nums[k]) {
duplicate = 1;
}
}
if (duplicate) {
continue;
}
/* try to open */
fd = open(path, O_RDWR, 0);
if (fd < 0) continue;
#ifdef DEBUG_INPUT_EVENTS
printf("Checking %s\n", path);
#endif
/* see if it works */
if (EV_IsHaptic(fd)!=0) {
SDL_hapticlist[numhaptics].fname = SDL_strdup(path);
SDL_hapticlist[numhaptics].haptic = NULL;
dev_nums[numhaptics] = sb.st_rdev;
++numhaptics;
}
close(fd);
}
return numhaptics;
}
Jun 1, 2008
Jun 1, 2008
151
152
153
/*
* Return the name of a haptic device, does not need to be opened.
*/
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const char *
SDL_SYS_HapticName(int index)
{
int fd;
static char namebuf[128];
char *name;
name = NULL;
fd = open(SDL_hapticlist[index].fname, O_RDONLY, 0);
if (fd >= 0) {
if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) {
name = SDL_hapticlist[index].fname;
}
else {
name = namebuf;
}
}
close(fd);
return name;
}
Jun 1, 2008
Jun 1, 2008
177
178
179
/*
* Opens a haptic device for usage.
*/
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
int
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
{
/* TODO finish
int fd;
fd = open(SDL_hapticlist[haptic->index].fname, O_RDWR, 0);
if (fd < 0) {
SDL_SetError("Unable to open %s\n", SDL_hapticlist[haptic->index]);
return (-1);
}
return 0;
*/
}
Jun 1, 2008
Jun 1, 2008
200
201
202
203
204
205
206
207
208
/*
* Closes the haptic device.
*/
void
SDL_SYS_HapticClose(SDL_Haptic * haptic)
{
}
Jun 1, 2008
Jun 1, 2008
209
210
211
212
213
214
215
216
217
218
219
220
221
/* Clean up after system specific haptic stuff */
void
SDL_SYS_HapticQuit(void)
{
int i;
for (i=0; SDL_hapticlist[i].fname != NULL; i++) {
SDL_free(SDL_hapticlist[i].fname);
}
SDL_hapticlist[0].fname = NULL;
}
222
#endif /* SDL_HAPTIC_LINUX */