annotate src/haptic/SDL_haptic.c @ 2472:3f73c88c9abb gsoc2008_force_feedback

First commit of the SDL_haptic subsystem. Code compiles and works, very limited functionality (linux only).
author Edgar Simo <bobbens@gmail.com>
date Sun, 01 Jun 2008 11:44:25 +0000
parents
children 3f80bf1528b4
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 /*
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
86 * Opens a Haptic device
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 */
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
100 for (i = 0; SDL_haptics[i]; ++i) {
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
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
108 /* Create and initialize the haptic */
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
109 haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
110 if (haptic != NULL) {
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
111 SDL_memset(haptic, 0, (sizeof *haptic));
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
112 haptic->index = device_index;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
113 if (SDL_SYS_HapticOpen(haptic) < 0) {
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
114 SDL_free(haptic);
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
115 haptic = NULL;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
116 } else {
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
117 }
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 if (haptic) {
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
120 /* Add haptic to list */
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
121 ++haptic->ref_count;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
122 for (i = 0; SDL_haptics[i]; ++i)
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
123 /* Skip to next haptic */ ;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
124 SDL_haptics[i] = haptic;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
125 }
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
126 return haptic;
3f73c88c9abb First commit of the SDL_haptic subsystem.
Edgar Simo <bobbens@gmail.com>
parents:
diff changeset
127 }