annotate src/haptic/SDL_haptic.c @ 2478:4fd783e0f34b gsoc2008_force_feedback

Added query functions for haptic devices.
author Edgar Simo <bobbens@gmail.com>
date Mon, 30 Jun 2008 17:28:34 +0000
parents 97f75ea43a93
children b9eb2cfe16cd
rev   line source
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 02110-1301 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 #include "SDL_haptic_c.h"
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
25 #include "SDL_syshaptic.h"
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
26
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
27
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
28 static Uint8 SDL_numhaptics = 0;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
29 SDL_Haptic **SDL_haptics = NULL;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
30 static SDL_Haptic *default_haptic = NULL;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
31
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
32
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
33 /*
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
34 * Initializes the Haptic devices.
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
35 */
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
36 int
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
37 SDL_HapticInit(void)
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
38 {
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
39 int arraylen;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
40 int status;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
41
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
42 SDL_numhaptics = 0;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
43 status = SDL_SYS_HapticInit();
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
44 if (status >= 0) {
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
45 arraylen = (status + 1) * sizeof(*SDL_haptics);
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
46 SDL_haptics = (SDL_Haptic **) SDL_malloc(arraylen);
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
47 if (SDL_haptics == NULL) {
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
48 SDL_numhaptics = 0;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
49 } else {
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
50 SDL_memset(SDL_haptics, 0, arraylen);
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
51 SDL_numhaptics = status;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
52 }
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
53 status = 0;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
54 }
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
55 default_haptic = NULL;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
56
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
57 return status;
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
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 * Returns the number of available devices.
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
63 */
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
64 int
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
65 SDL_NumHaptics(void)
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
66 {
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
67 return SDL_numhaptics;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
68 }
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
69
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
70
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
71 /*
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
72 * Gets the name of a Haptic device by index.
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
73 */
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
74 const char *
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
75 SDL_HapticName(int device_index)
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
76 {
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
77 if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
78 SDL_SetError("There are %d haptic devices available", SDL_numhaptics);
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
79 return NULL;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
80 }
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
81 return SDL_SYS_HapticName(device_index);
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
82 }
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
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
85 /*
2475
4b874e3a3a2c Some code cleanup.
Edgar Simo <bobbens@gmail.com>
parents: 2474
diff changeset
86 * Opens a Haptic device.
2472
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
87 */
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
88 SDL_Haptic *
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
89 SDL_HapticOpen(int device_index)
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 int i;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
92 SDL_Haptic *haptic;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
93
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
94 if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
95 SDL_SetError("There are %d haptic devices available", SDL_numhaptics);
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
96 return NULL;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
97 }
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
98
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
99 /* If the haptic is already open, return it */
2477
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
100 for (i=0; SDL_haptics[i]; i++) {
2472
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
101 if (device_index == SDL_haptics[i]->index) {
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
102 haptic = SDL_haptics[i];
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
103 ++haptic->ref_count;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
104 return haptic;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
105 }
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
106 }
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
107
2477
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
108 /* Create the haptic device */
2472
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
109 haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
2477
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
110 if (haptic == NULL) {
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
111 SDL_OutOfMemory();
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
112 return NULL;
2472
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
113 }
2477
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
114
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
115 /* Initialize the haptic device */
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
116 SDL_memset(haptic, 0, (sizeof *haptic));
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
117 haptic->index = device_index;
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
118 if (SDL_SYS_HapticOpen(haptic) < 0) {
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
119 SDL_free(haptic);
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
120 return NULL;
2472
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
121 }
2477
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
122
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
123 /* Add haptic to list */
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
124 ++haptic->ref_count;
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
125 for (i = 0; SDL_haptics[i]; ++i)
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
126 /* Skip to next haptic */ ;
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
127 SDL_haptics[i] = haptic;
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
128
2472
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
129 return haptic;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
130 }
2474
3f80bf1528b4 Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents: 2472
diff changeset
131
3f80bf1528b4 Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents: 2472
diff changeset
132
2475
4b874e3a3a2c Some code cleanup.
Edgar Simo <bobbens@gmail.com>
parents: 2474
diff changeset
133 /*
2476
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
134 * Checks to see if the haptic device is valid
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
135 */
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
136 static int
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
137 ValidHaptic(SDL_Haptic ** haptic)
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
138 {
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
139 int valid;
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
140
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
141 if (*haptic == NULL) {
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
142 SDL_SetError("Haptic device hasn't been opened yet");
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
143 valid = 0;
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
144 } else {
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
145 valid = 1;
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
146 }
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
147 return valid;
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
148 }
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
149
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
150
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
151 /*
2475
4b874e3a3a2c Some code cleanup.
Edgar Simo <bobbens@gmail.com>
parents: 2474
diff changeset
152 * Closes a SDL_Haptic device.
4b874e3a3a2c Some code cleanup.
Edgar Simo <bobbens@gmail.com>
parents: 2474
diff changeset
153 */
4b874e3a3a2c Some code cleanup.
Edgar Simo <bobbens@gmail.com>
parents: 2474
diff changeset
154 void
4b874e3a3a2c Some code cleanup.
Edgar Simo <bobbens@gmail.com>
parents: 2474
diff changeset
155 SDL_HapticClose(SDL_Haptic * haptic)
4b874e3a3a2c Some code cleanup.
Edgar Simo <bobbens@gmail.com>
parents: 2474
diff changeset
156 {
2476
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
157 int i;
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
158
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
159 /* Must be valid */
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
160 if (!ValidHaptic(&haptic)) {
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
161 return;
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
162 }
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
163
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
164 /* Check if it's still in use */
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
165 if (--haptic->ref_count < 0) {
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
166 return;
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
167 }
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
168
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
169 /* Close it */
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
170 SDL_SYS_HapticClose(haptic);
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
171
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
172 /* Remove from the list */
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
173 for (i = 0; SDL_haptics[i]; ++i) {
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
174 if (haptic == SDL_haptics[i]) {
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
175 SDL_memcpy(&SDL_haptics[i], &SDL_haptics[i + 1],
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
176 (SDL_numhaptics - i) * sizeof(haptic));
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
177 break;
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
178 }
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
179 }
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
180
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
181 /* Free */
242d8a668ebb * Implemented opening and closing of haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2475
diff changeset
182 SDL_free(haptic);
2475
4b874e3a3a2c Some code cleanup.
Edgar Simo <bobbens@gmail.com>
parents: 2474
diff changeset
183 }
4b874e3a3a2c Some code cleanup.
Edgar Simo <bobbens@gmail.com>
parents: 2474
diff changeset
184
4b874e3a3a2c Some code cleanup.
Edgar Simo <bobbens@gmail.com>
parents: 2474
diff changeset
185 /*
4b874e3a3a2c Some code cleanup.
Edgar Simo <bobbens@gmail.com>
parents: 2474
diff changeset
186 * Cleans up after the subsystem.
4b874e3a3a2c Some code cleanup.
Edgar Simo <bobbens@gmail.com>
parents: 2474
diff changeset
187 */
2474
3f80bf1528b4 Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents: 2472
diff changeset
188 void
3f80bf1528b4 Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents: 2472
diff changeset
189 SDL_HapticQuit(void)
3f80bf1528b4 Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents: 2472
diff changeset
190 {
3f80bf1528b4 Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents: 2472
diff changeset
191 SDL_numhaptics = 0;
3f80bf1528b4 Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents: 2472
diff changeset
192
3f80bf1528b4 Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents: 2472
diff changeset
193 SDL_SYS_HapticQuit();
3f80bf1528b4 Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents: 2472
diff changeset
194 if (SDL_haptics != NULL) {
3f80bf1528b4 Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents: 2472
diff changeset
195 SDL_free(SDL_haptics);
3f80bf1528b4 Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents: 2472
diff changeset
196 SDL_haptics = NULL;
3f80bf1528b4 Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents: 2472
diff changeset
197 }
3f80bf1528b4 Properly quit the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents: 2472
diff changeset
198 }
2477
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
199
2478
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
200 /*
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
201 * Returns the number of effects a haptic device has.
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
202 */
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
203 int
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
204 SDL_HapticNumEffects(SDL_Haptic * haptic)
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
205 {
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
206 if (!ValidHaptic(&haptic)) {
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
207 return -1;
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
208 }
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
209
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
210 return haptic->neffects;
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
211 }
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
212
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
213 /*
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
214 * Returns supported effects by the device.
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
215 */
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
216 unsigned int
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
217 SDL_HapticQueryEffects(SDL_Haptic * haptic)
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
218 {
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
219 if (!ValidHaptic(&haptic)) {
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
220 return -1;
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
221 }
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
222
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
223 return haptic->supported;
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
224 }
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
225
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
226 int
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
227 SDL_HapticEffectSupported(SDL_Haptic * haptic, SDL_HapticEffect * effect)
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
228 {
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
229 if (!ValidHaptic(&haptic)) {
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
230 return -1;
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
231 }
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
232
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
233 if ((haptic->supported & effect->type) != 0)
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
234 return SDL_TRUE;
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
235 return SDL_FALSE;
4fd783e0f34b Added query functions for haptic devices.
Edgar Simo <bobbens@gmail.com>
parents: 2477
diff changeset
236 }
2477
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
237
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
238 /*
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
239 * Creates a new haptic effect.
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
240 */
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
241 int
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
242 SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect)
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
243 {
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
244 int i;
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
245
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
246 /* Check for device validity. */
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
247 if (!ValidHaptic(&haptic)) {
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
248 return -1;
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
249 }
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
250
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
251 /* See if there's a free slot */
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
252 for (i=0; i<haptic->neffects; i++) {
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
253 if (haptic->effects[i].hweffect == NULL) {
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
254
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
255 /* Now let the backend create the real effect */
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
256 if (SDL_SYS_HapticNewEffect(haptic,&haptic->effects[i]) != 0) {
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
257 return -1; /* Backend failed to create effect */
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
258 }
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
259 return i;
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 }
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
262
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
263 SDL_SetError("Haptic device has no free space left.");
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
264 return -1;
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
265 }
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
266
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
267 /*
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
268 * Runs the haptic effect on the device.
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
269 */
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
270 int
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
271 SDL_HapticRunEffect(SDL_Haptic * haptic, int effect)
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
272 {
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
273 if (!ValidHaptic(&haptic)) {
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
274 return -1;
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
275 }
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
276 }
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
277
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
278 /*
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
279 * Gets rid of a haptic effect.
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
280 */
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
281 void
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
282 SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect)
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
283 {
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
284 if (!ValidHaptic(&haptic)) {
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
285 return;
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
286 }
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
287 }
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
288
97f75ea43a93 Starting to add infrastructure to handle haptic effects.
Edgar Simo <bobbens@gmail.com>
parents: 2476
diff changeset
289