author  Edgar Simo <bobbens@gmail.com> 
Tue, 01 Jul 2008 10:44:42 +0000  
branch  gsoc2008_force_feedback 
changeset 2481  5d0ea4576f20 
parent 2480  b883974445fc 
child 2482  b51ad78812d5 
permissions  rwrr 
2472
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

1 
/* 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

2 
SDL  Simple DirectMedia Layer 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

3 
Copyright (C) 2008 Edgar Simo 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

4 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

5 
This library is free software; you can redistribute it and/or 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

6 
modify it under the terms of the GNU Lesser General Public 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

7 
License as published by the Free Software Foundation; either 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

8 
version 2.1 of the License, or (at your option) any later version. 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

9 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

10 
This library is distributed in the hope that it will be useful, 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

11 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

12 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

13 
Lesser General Public License for more details. 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

14 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

15 
You should have received a copy of the GNU Lesser General Public 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

16 
License along with this library; if not, write to the Free Software 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

17 
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 021101301 USA 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

18 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

19 
Sam Lantinga 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

20 
slouken@libsdl.org 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

21 
*/ 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

22 
#include "SDL_config.h" 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

23 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

24 
#ifdef SDL_JOYSTICK_LINUX 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

25 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

26 
#include "SDL_haptic.h" 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

27 
#include "../SDL_haptic_c.h" 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

28 
#include "../SDL_syshaptic.h" 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

29 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

30 
#include <unistd.h> /* close */ 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

31 
#include <linux/input.h> 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

32 
#include <sys/ioctl.h> 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

33 
#include <sys/types.h> 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

34 
#include <sys/stat.h> 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

35 
#include <fcntl.h> 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

36 
#include <linux/limits.h> 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

37 
#include <string.h> 
2480  38 
#include <errno.h> 
2472
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

39 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

40 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

41 
#define MAX_HAPTICS 32 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

42 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

43 

2475  44 
/* 
45 
* List of available haptic devices. 

46 
*/ 

2472
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

47 
static struct 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

48 
{ 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

49 
char *fname; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

50 
SDL_Haptic *haptic; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

51 
} SDL_hapticlist[MAX_HAPTICS]; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

52 

2475  53 

54 
/* 

55 
* Haptic system hardware data. 

56 
*/ 

2472
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

57 
struct haptic_hwdata 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

58 
{ 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

59 
int fd; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

60 
}; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

61 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

62 

2477
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

63 
/* 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

64 
* Haptic system effect data. 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

65 
*/ 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

66 
struct haptic_hweffect 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

67 
{ 
2479
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

68 
struct ff_effect effect; 
2477
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

69 
}; 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

70 

97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

71 

2475  72 

2472
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

73 
#define test_bit(nr, addr) \ 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

74 
(((1UL << ((nr) & 31)) & (((const unsigned int *) addr)[(nr) >> 5])) != 0) 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

75 
#define EV_TEST(ev,f) \ 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

76 
if (test_bit((ev), features)) ret = (f); 
2475  77 
/* 
78 
* Test whether a device has haptic properties. 

79 
* Returns available properties or 0 if there are none. 

80 
*/ 

2472
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

81 
static int 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

82 
EV_IsHaptic(int fd) 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

83 
{ 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

84 
unsigned int ret; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

85 
unsigned long features[1 + FF_MAX/sizeof(unsigned long)]; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

86 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

87 
ret = 0; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

88 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

89 
ioctl(fd, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

90 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

91 
EV_TEST(FF_CONSTANT, SDL_HAPTIC_CONSTANT); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

92 
EV_TEST(FF_PERIODIC, SDL_HAPTIC_PERIODIC); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

93 
EV_TEST(FF_RAMP, SDL_HAPTIC_RAMP); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

94 
EV_TEST(FF_SPRING, SDL_HAPTIC_SPRING); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

95 
EV_TEST(FF_FRICTION, SDL_HAPTIC_FRICTION); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

96 
EV_TEST(FF_DAMPER, SDL_HAPTIC_DAMPER); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

97 
EV_TEST(FF_RUMBLE, SDL_HAPTIC_RUMBLE); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

98 
EV_TEST(FF_INERTIA, SDL_HAPTIC_INERTIA); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

99 
EV_TEST(FF_GAIN, SDL_HAPTIC_GAIN); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

100 
EV_TEST(FF_AUTOCENTER, SDL_HAPTIC_AUTOCENTER); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

101 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

102 
return ret; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

103 
} 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

104 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

105 
int 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

106 
SDL_SYS_HapticInit(void) 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

107 
{ 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

108 
const char joydev_pattern[] = "/dev/input/event%d"; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

109 
dev_t dev_nums[MAX_HAPTICS]; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

110 
char path[PATH_MAX]; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

111 
struct stat sb; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

112 
int fd; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

113 
int i, j, k; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

114 
int duplicate; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

115 
int numhaptics; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

116 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

117 
numhaptics = 0; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

118 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

119 
i = 0; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

120 
for (j = 0; j < MAX_HAPTICS; ++j) { 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

121 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

122 
snprintf(path, PATH_MAX, joydev_pattern, i++); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

123 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

124 
/* check to see if file exists */ 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

125 
if (stat(path,&sb) != 0) 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

126 
break; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

127 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

128 
/* check for duplicates */ 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

129 
duplicate = 0; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

130 
for (k = 0; (k < numhaptics) && !duplicate; ++k) { 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

131 
if (sb.st_rdev == dev_nums[k]) { 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

132 
duplicate = 1; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

133 
} 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

134 
} 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

135 
if (duplicate) { 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

136 
continue; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

137 
} 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

138 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

139 
/* try to open */ 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

140 
fd = open(path, O_RDWR, 0); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

141 
if (fd < 0) continue; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

142 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

143 
#ifdef DEBUG_INPUT_EVENTS 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

144 
printf("Checking %s\n", path); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

145 
#endif 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

146 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

147 
/* see if it works */ 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

148 
if (EV_IsHaptic(fd)!=0) { 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

149 
SDL_hapticlist[numhaptics].fname = SDL_strdup(path); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

150 
SDL_hapticlist[numhaptics].haptic = NULL; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

151 
dev_nums[numhaptics] = sb.st_rdev; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

152 
++numhaptics; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

153 
} 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

154 
close(fd); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

155 
} 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

156 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

157 
return numhaptics; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

158 
} 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

159 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

160 

2475  161 
/* 
162 
* Return the name of a haptic device, does not need to be opened. 

163 
*/ 

2472
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

164 
const char * 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

165 
SDL_SYS_HapticName(int index) 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

166 
{ 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

167 
int fd; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

168 
static char namebuf[128]; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

169 
char *name; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

170 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

171 
name = NULL; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

172 
fd = open(SDL_hapticlist[index].fname, O_RDONLY, 0); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

173 
if (fd >= 0) { 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

174 
if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) { 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

175 
name = SDL_hapticlist[index].fname; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

176 
} 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

177 
else { 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

178 
name = namebuf; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

179 
} 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

180 
} 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

181 
close(fd); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

182 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

183 
return name; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

184 
} 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

185 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

186 

2475  187 
/* 
188 
* Opens a haptic device for usage. 

189 
*/ 

2472
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

190 
int 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

191 
SDL_SYS_HapticOpen(SDL_Haptic * haptic) 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

192 
{ 
2477
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

193 
int i; 
2472
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

194 
int fd; 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

195 

2476
242d8a668ebb
* Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2475
diff
changeset

196 
/* Open the character device */ 
2472
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

197 
fd = open(SDL_hapticlist[haptic>index].fname, O_RDWR, 0); 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

198 
if (fd < 0) { 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

199 
SDL_SetError("Unable to open %s\n", SDL_hapticlist[haptic>index]); 
2477
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

200 
return 1; 
2472
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

201 
} 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

202 

2476
242d8a668ebb
* Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2475
diff
changeset

203 
/* Allocate the hwdata */ 
242d8a668ebb
* Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2475
diff
changeset

204 
haptic>hwdata = (struct haptic_hwdata *) 
2477
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

205 
SDL_malloc(sizeof(*haptic>hwdata)); 
2476
242d8a668ebb
* Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2475
diff
changeset

206 
if (haptic>hwdata == NULL) { 
242d8a668ebb
* Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2475
diff
changeset

207 
SDL_OutOfMemory(); 
2477
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

208 
goto open_err; 
2476
242d8a668ebb
* Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2475
diff
changeset

209 
} 
242d8a668ebb
* Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2475
diff
changeset

210 
SDL_memset(haptic>hwdata, 0, sizeof(*haptic>hwdata)); 
2478
4fd783e0f34b
Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2477
diff
changeset

211 
/* Set the data */ 
2476
242d8a668ebb
* Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2475
diff
changeset

212 
haptic>hwdata>fd = fd; 
2478
4fd783e0f34b
Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2477
diff
changeset

213 
haptic>supported = EV_IsHaptic(fd); 
2472
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

214 

2477
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

215 
/* Set the effects */ 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

216 
if (ioctl(fd, EVIOCGEFFECTS, &haptic>neffects) < 0) { 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

217 
SDL_SetError("Unable to query haptic device memory."); 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

218 
goto open_err; 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

219 
} 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

220 
haptic>effects = (struct haptic_effect *) 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

221 
SDL_malloc(sizeof(struct haptic_effect) * haptic>neffects); 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

222 
if (haptic>effects == NULL) { 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

223 
SDL_OutOfMemory(); 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

224 
goto open_err; 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

225 
} 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

226 

2472
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

227 
return 0; 
2477
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

228 

97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

229 
/* Error handling */ 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

230 
open_err: 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

231 
close(fd); 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

232 
if (haptic>hwdata != NULL) { 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

233 
free(haptic>hwdata); 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

234 
haptic>hwdata = NULL; 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

235 
} 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

236 
return 1; 
2472
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

237 
} 
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

238 

3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

239 

2475  240 
/* 
241 
* Closes the haptic device. 

242 
*/ 

243 
void 

244 
SDL_SYS_HapticClose(SDL_Haptic * haptic) 

245 
{ 

2476
242d8a668ebb
* Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2475
diff
changeset

246 
if (haptic>hwdata) { 
242d8a668ebb
* Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2475
diff
changeset

247 

242d8a668ebb
* Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2475
diff
changeset

248 
/* Clean up */ 
242d8a668ebb
* Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2475
diff
changeset

249 
close(haptic>hwdata>fd); 
242d8a668ebb
* Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2475
diff
changeset

250 

242d8a668ebb
* Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2475
diff
changeset

251 
/* Free */ 
242d8a668ebb
* Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2475
diff
changeset

252 
SDL_free(haptic>hwdata); 
242d8a668ebb
* Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2475
diff
changeset

253 
haptic>hwdata = NULL; 
2477
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

254 
SDL_free(haptic>effects); 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

255 
haptic>neffects = 0; 
2476
242d8a668ebb
* Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents:
2475
diff
changeset

256 
} 
2475  257 
} 
258 

259 

2477
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

260 
/* 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

261 
* Clean up after system specific haptic stuff 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

262 
*/ 
2474
3f80bf1528b4
Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
2472
diff
changeset

263 
void 
3f80bf1528b4
Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
2472
diff
changeset

264 
SDL_SYS_HapticQuit(void) 
3f80bf1528b4
Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
2472
diff
changeset

265 
{ 
3f80bf1528b4
Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
2472
diff
changeset

266 
int i; 
3f80bf1528b4
Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
2472
diff
changeset

267 

3f80bf1528b4
Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
2472
diff
changeset

268 
for (i=0; SDL_hapticlist[i].fname != NULL; i++) { 
3f80bf1528b4
Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
2472
diff
changeset

269 
SDL_free(SDL_hapticlist[i].fname); 
3f80bf1528b4
Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
2472
diff
changeset

270 
} 
3f80bf1528b4
Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
2472
diff
changeset

271 
SDL_hapticlist[0].fname = NULL; 
3f80bf1528b4
Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
2472
diff
changeset

272 
} 
3f80bf1528b4
Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
2472
diff
changeset

273 

2481  274 
#define CLAMP(x) (((x) > 32767) ? 32767 : x) 
2477
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

275 
/* 
2479
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

276 
* Initializes the linux effect struct from a haptic_effect. 
2481  277 
* Values above 32767 (for unsigned) are unspecified so we must clamp. 
2479
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

278 
*/ 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

279 
static int 
2480  280 
SDL_SYS_ToFFEffect( struct ff_effect * dest, SDL_HapticEffect * src ) 
2479
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

281 
{ 
2480  282 
SDL_HapticConstant *constant; 
283 
SDL_HapticPeriodic *periodic; 

284 

285 
/* Clear up */ 

2479
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

286 
SDL_memset(dest, 0, sizeof(struct ff_effect)); 
2480  287 

288 
switch (src>type) { 

2479
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

289 
case SDL_HAPTIC_CONSTANT: 
2480  290 
constant = &src>constant; 
291 

292 
/* Header */ 

2479
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

293 
dest>type = FF_CONSTANT; 
2481  294 
dest>direction = CLAMP(constant>direction); 
2480  295 

296 
/* Replay */ 

2481  297 
dest>replay.length = CLAMP(constant>length); 
298 
dest>replay.delay = CLAMP(constant>delay); 

2480  299 

300 
/* Trigger */ 

2481  301 
dest>trigger.button = CLAMP(constant>button); 
302 
dest>trigger.interval = CLAMP(constant>interval); 

2480  303 

304 
/* Constant */ 

305 
dest>u.constant.level = constant>level; 

306 

307 
/* Envelope */ 

2481  308 
dest>u.constant.envelope.attack_length = CLAMP(constant>attack_length); 
309 
dest>u.constant.envelope.attack_level = CLAMP(constant>attack_level); 

310 
dest>u.constant.envelope.fade_length = CLAMP(constant>fade_length); 

311 
dest>u.constant.envelope.fade_level = CLAMP(constant>fade_level); 

2480  312 

313 
break; 

314 

315 
case SDL_HAPTIC_PERIODIC: 

316 
periodic = &src>periodic; 

317 

318 
/* Header */ 

319 
dest>type = FF_PERIODIC; 

2481  320 
dest>direction = CLAMP(periodic>direction); 
2480  321 

322 
/* Replay */ 

2481  323 
dest>replay.length = CLAMP(periodic>length); 
324 
dest>replay.delay = CLAMP(periodic>delay); 

2480  325 

326 
/* Trigger */ 

2481  327 
dest>trigger.button = CLAMP(periodic>button); 
328 
dest>trigger.interval = CLAMP(periodic>interval); 

2480  329 

330 
/* Constant */ 

2481  331 
switch (periodic>waveform) { 
332 
case SDL_WAVEFORM_SINE: 

333 
dest>u.periodic.waveform = FF_SINE; 

334 
break; 

335 
case SDL_WAVEFORM_SQUARE: 

336 
dest>u.periodic.waveform = FF_SQUARE; 

337 
break; 

338 
case SDL_WAVEFORM_TRIANGLE: 

339 
dest>u.periodic.waveform = FF_TRIANGLE; 

340 
break; 

341 
case SDL_WAVEFORM_SAWTOOTHUP: 

342 
dest>u.periodic.waveform = FF_SAW_UP; 

343 
break; 

344 
case SDL_WAVEFORM_SAWTOOTHDOWN: 

345 
dest>u.periodic.waveform = FF_SAW_DOWN; 

346 
break; 

347 

348 
default: 

349 
SDL_SetError("Unknown waveform."); 

350 
return 1; 

351 
} 

352 
dest>u.periodic.period = CLAMP(periodic>period); 

2480  353 
dest>u.periodic.magnitude = periodic>magnitude; 
354 
dest>u.periodic.offset = periodic>offset; 

2481  355 
dest>u.periodic.phase = CLAMP(periodic>phase); 
2480  356 

357 
/* Envelope */ 

2481  358 
dest>u.periodic.envelope.attack_length = CLAMP(periodic>attack_length); 
359 
dest>u.periodic.envelope.attack_level = CLAMP(periodic>attack_level); 

360 
dest>u.periodic.envelope.fade_length = CLAMP(periodic>fade_length); 

361 
dest>u.periodic.envelope.fade_level = CLAMP(periodic>fade_level); 

2479
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

362 

b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

363 
break; 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

364 

b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

365 
default: 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

366 
SDL_SetError("Unknown haptic effect type."); 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

367 
return 1; 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

368 
} 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

369 

b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

370 
return 0; 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

371 
} 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

372 

b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

373 
/* 
2477
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

374 
* Creates a new haptic effect. 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

375 
*/ 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

376 
int 
2480  377 
SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect * effect, 
378 
SDL_HapticEffect * base) 

2477
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

379 
{ 
2480  380 
struct ff_effect * linux_effect; 
381 

2479
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

382 
/* Allocate the hardware effect */ 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

383 
effect>hweffect = (struct haptic_hweffect *) 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

384 
SDL_malloc(sizeof(struct haptic_hweffect)); 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

385 
if (effect>hweffect == NULL) { 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

386 
SDL_OutOfMemory(); 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

387 
return 1; 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

388 
} 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

389 

b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

390 
/* Prepare the ff_effect */ 
2480  391 
linux_effect = &effect>hweffect>effect; 
392 
if (SDL_SYS_ToFFEffect( linux_effect, base ) != 0) { 

2479
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

393 
return 1; 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

394 
} 
2480  395 
linux_effect>id = 1; /* Have the kernel give it an id */ 
2479
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

396 

b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

397 
/* Upload the effect */ 
2480  398 
if (ioctl(haptic>hwdata>fd, EVIOCSFF, linux_effect) < 0) { 
399 
SDL_SetError("Error uploading effect to the haptic device: %s", 

400 
strerror(errno)); 

2479
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

401 
return 1; 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

402 
} 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

403 

b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

404 
return 0; 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

405 
} 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

406 

b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

407 

b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

408 
/* 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

409 
* Runs an effect. 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

410 
*/ 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

411 
int 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

412 
SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect * effect) 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

413 
{ 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

414 
struct input_event run; 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

415 

b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

416 
/* Prepare to run the effect */ 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

417 
run.type = EV_FF; 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

418 
run.code = effect>hweffect>effect.id; 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

419 
run.value = 1; 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

420 

b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

421 
if (write(haptic>hwdata>fd, (const void*) &run, sizeof(run)) < 0) { 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

422 
SDL_SetError("Unable to run the haptic effect."); 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

423 
return 1; 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

424 
} 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

425 

b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

426 
return 0; 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

427 
} 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

428 

b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

429 

b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

430 
/* 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

431 
* Frees the effect 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

432 
*/ 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

433 
void 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

434 
SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect * effect) 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

435 
{ 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

436 
if (ioctl(haptic>hwdata>fd, EVIOCRMFF, effect>hweffect>effect.id) < 0) { 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

437 
SDL_SetError("Error removing the effect from the haptic device."); 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

438 
} 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

439 
SDL_free(effect>hweffect); 
b9eb2cfe16cd
Added some preliminary support for haptic effect control.
Edgar Simo <bobbens@gmail.com>
parents:
2478
diff
changeset

440 
effect>hweffect = NULL; 
2477
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

441 
} 
97f75ea43a93
Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents:
2476
diff
changeset

442 

2474
3f80bf1528b4
Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
2472
diff
changeset

443 

2472
3f73c88c9abb
First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff
changeset

444 
#endif /* SDL_HAPTIC_LINUX */ 