Mercurial > sdl-ios-xcode
comparison src/joystick/dc/SDL_sysjoystick.c @ 1895:c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 10 Jul 2006 21:04:37 +0000 |
parents | 92947e3a18db |
children | 99210400e8b9 |
comparison
equal
deleted
inserted
replaced
1894:c69cee13dd76 | 1895:c121d94672cb |
---|---|
29 #include "../SDL_joystick_c.h" | 29 #include "../SDL_joystick_c.h" |
30 | 30 |
31 #include <dc/maple.h> | 31 #include <dc/maple.h> |
32 #include <dc/maple/controller.h> | 32 #include <dc/maple/controller.h> |
33 | 33 |
34 #define MAX_JOYSTICKS 8 /* only 2 are supported in the multimedia API */ | 34 #define MAX_JOYSTICKS 8 /* only 2 are supported in the multimedia API */ |
35 #define MAX_AXES 6 /* each joystick can have up to 6 axes */ | 35 #define MAX_AXES 6 /* each joystick can have up to 6 axes */ |
36 #define MAX_BUTTONS 8 /* and 8 buttons */ | 36 #define MAX_BUTTONS 8 /* and 8 buttons */ |
37 #define MAX_HATS 2 | 37 #define MAX_HATS 2 |
38 | 38 |
39 #define JOYNAMELEN 8 | 39 #define JOYNAMELEN 8 |
40 | 40 |
41 /* array to hold joystick ID values */ | 41 /* array to hold joystick ID values */ |
42 static uint8 SYS_Joystick_addr[MAX_JOYSTICKS]; | 42 static uint8 SYS_Joystick_addr[MAX_JOYSTICKS]; |
43 | 43 |
44 /* The private structure used to keep track of a joystick */ | 44 /* The private structure used to keep track of a joystick */ |
45 struct joystick_hwdata | 45 struct joystick_hwdata |
46 { | 46 { |
47 cont_cond_t prev_cond; | 47 cont_cond_t prev_cond; |
48 int prev_buttons; | 48 int prev_buttons; |
49 }; | 49 }; |
50 | 50 |
51 /* Function to scan the system for joysticks. | 51 /* Function to scan the system for joysticks. |
52 * This function should set SDL_numjoysticks to the number of available | 52 * This function should set SDL_numjoysticks to the number of available |
53 * joysticks. Joystick 0 should be the system default joystick. | 53 * joysticks. Joystick 0 should be the system default joystick. |
54 * It should return 0, or -1 on an unrecoverable fatal error. | 54 * It should return 0, or -1 on an unrecoverable fatal error. |
55 */ | 55 */ |
56 int SDL_SYS_JoystickInit(void) | 56 int |
57 { | 57 SDL_SYS_JoystickInit(void) |
58 int numdevs; | 58 { |
59 | 59 int numdevs; |
60 int p,u; | 60 |
61 | 61 int p, u; |
62 numdevs = 0; | 62 |
63 for(p=0;p<MAPLE_PORT_COUNT;p++) { | 63 numdevs = 0; |
64 for(u=0;u<MAPLE_UNIT_COUNT;u++) { | 64 for (p = 0; p < MAPLE_PORT_COUNT; p++) { |
65 if (maple_device_func(p,u)&MAPLE_FUNC_CONTROLLER) { | 65 for (u = 0; u < MAPLE_UNIT_COUNT; u++) { |
66 SYS_Joystick_addr[numdevs] = maple_addr(p,u); | 66 if (maple_device_func(p, u) & MAPLE_FUNC_CONTROLLER) { |
67 numdevs++; | 67 SYS_Joystick_addr[numdevs] = maple_addr(p, u); |
68 } | 68 numdevs++; |
69 } | 69 } |
70 } | 70 } |
71 | 71 } |
72 return(numdevs); | 72 |
73 return (numdevs); | |
73 } | 74 } |
74 | 75 |
75 /* Function to get the device-dependent name of a joystick */ | 76 /* Function to get the device-dependent name of a joystick */ |
76 const char *SDL_SYS_JoystickName(int index) | 77 const char * |
77 { | 78 SDL_SYS_JoystickName(int index) |
78 maple_device_t *dev; | 79 { |
79 if (maple_compat_resolve(SYS_Joystick_addr[index],&dev,MAPLE_FUNC_CONTROLLER)!=0) return NULL; | 80 maple_device_t *dev; |
80 return dev->info.product_name; | 81 if (maple_compat_resolve |
82 (SYS_Joystick_addr[index], &dev, MAPLE_FUNC_CONTROLLER) != 0) | |
83 return NULL; | |
84 return dev->info.product_name; | |
81 } | 85 } |
82 | 86 |
83 /* Function to open a joystick for use. | 87 /* Function to open a joystick for use. |
84 The joystick to open is specified by the index field of the joystick. | 88 The joystick to open is specified by the index field of the joystick. |
85 This should fill the nbuttons and naxes fields of the joystick structure. | 89 This should fill the nbuttons and naxes fields of the joystick structure. |
86 It returns 0, or -1 if there is an error. | 90 It returns 0, or -1 if there is an error. |
87 */ | 91 */ |
88 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick) | 92 int |
89 { | 93 SDL_SYS_JoystickOpen(SDL_Joystick * joystick) |
90 /* allocate memory for system specific hardware data */ | 94 { |
91 joystick->hwdata = (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata)); | 95 /* allocate memory for system specific hardware data */ |
92 if (joystick->hwdata == NULL) | 96 joystick->hwdata = |
93 { | 97 (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata)); |
94 SDL_OutOfMemory(); | 98 if (joystick->hwdata == NULL) { |
95 return(-1); | 99 SDL_OutOfMemory(); |
96 } | 100 return (-1); |
97 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); | 101 } |
98 | 102 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); |
99 /* fill nbuttons, naxes, and nhats fields */ | 103 |
100 joystick->nbuttons = MAX_BUTTONS; | 104 /* fill nbuttons, naxes, and nhats fields */ |
101 joystick->naxes = MAX_AXES; | 105 joystick->nbuttons = MAX_BUTTONS; |
102 joystick->nhats = MAX_HATS; | 106 joystick->naxes = MAX_AXES; |
103 return(0); | 107 joystick->nhats = MAX_HATS; |
108 return (0); | |
104 } | 109 } |
105 | 110 |
106 | 111 |
107 /* Function to update the state of a joystick - called as a device poll. | 112 /* Function to update the state of a joystick - called as a device poll. |
108 * This function shouldn't update the joystick structure directly, | 113 * This function shouldn't update the joystick structure directly, |
109 * but instead should call SDL_PrivateJoystick*() to deliver events | 114 * but instead should call SDL_PrivateJoystick*() to deliver events |
110 * and update joystick device state. | 115 * and update joystick device state. |
111 */ | 116 */ |
112 | 117 |
113 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick) | 118 void |
114 { | 119 SDL_SYS_JoystickUpdate(SDL_Joystick * joystick) |
115 const int sdl_buttons[] = { | 120 { |
116 CONT_C, | 121 const int sdl_buttons[] = { |
117 CONT_B, | 122 CONT_C, |
118 CONT_A, | 123 CONT_B, |
119 CONT_START, | 124 CONT_A, |
120 CONT_Z, | 125 CONT_START, |
121 CONT_Y, | 126 CONT_Z, |
122 CONT_X, | 127 CONT_Y, |
123 CONT_D | 128 CONT_X, |
124 }; | 129 CONT_D |
125 | 130 }; |
126 uint8 addr; | 131 |
127 cont_cond_t cond,*prev_cond; | 132 uint8 addr; |
128 int buttons,prev_buttons,i,changed; | 133 cont_cond_t cond, *prev_cond; |
129 | 134 int buttons, prev_buttons, i, changed; |
130 addr = SYS_Joystick_addr[joystick->index]; | 135 |
131 if (cont_get_cond(addr,&cond)<0) return; | 136 addr = SYS_Joystick_addr[joystick->index]; |
132 | 137 if (cont_get_cond(addr, &cond) < 0) |
133 buttons = cond.buttons; | 138 return; |
134 prev_buttons = joystick->hwdata->prev_buttons; | 139 |
135 changed = buttons^prev_buttons; | 140 buttons = cond.buttons; |
136 | 141 prev_buttons = joystick->hwdata->prev_buttons; |
137 if ((changed)&(CONT_DPAD_UP|CONT_DPAD_DOWN|CONT_DPAD_LEFT|CONT_DPAD_RIGHT)) { | 142 changed = buttons ^ prev_buttons; |
138 int hat = SDL_HAT_CENTERED; | 143 |
139 if (buttons&CONT_DPAD_UP) hat|=SDL_HAT_UP; | 144 if ((changed) & |
140 if (buttons&CONT_DPAD_DOWN) hat|=SDL_HAT_DOWN; | 145 (CONT_DPAD_UP | CONT_DPAD_DOWN | CONT_DPAD_LEFT | CONT_DPAD_RIGHT)) { |
141 if (buttons&CONT_DPAD_LEFT) hat|=SDL_HAT_LEFT; | 146 int hat = SDL_HAT_CENTERED; |
142 if (buttons&CONT_DPAD_RIGHT) hat|=SDL_HAT_RIGHT; | 147 if (buttons & CONT_DPAD_UP) |
143 SDL_PrivateJoystickHat(joystick, 0, hat); | 148 hat |= SDL_HAT_UP; |
144 } | 149 if (buttons & CONT_DPAD_DOWN) |
145 if ((changed)&(CONT_DPAD2_UP|CONT_DPAD2_DOWN|CONT_DPAD2_LEFT|CONT_DPAD2_RIGHT)) { | 150 hat |= SDL_HAT_DOWN; |
146 int hat = SDL_HAT_CENTERED; | 151 if (buttons & CONT_DPAD_LEFT) |
147 if (buttons&CONT_DPAD2_UP) hat|=SDL_HAT_UP; | 152 hat |= SDL_HAT_LEFT; |
148 if (buttons&CONT_DPAD2_DOWN) hat|=SDL_HAT_DOWN; | 153 if (buttons & CONT_DPAD_RIGHT) |
149 if (buttons&CONT_DPAD2_LEFT) hat|=SDL_HAT_LEFT; | 154 hat |= SDL_HAT_RIGHT; |
150 if (buttons&CONT_DPAD2_RIGHT) hat|=SDL_HAT_RIGHT; | 155 SDL_PrivateJoystickHat(joystick, 0, hat); |
151 SDL_PrivateJoystickHat(joystick, 1, hat); | 156 } |
152 } | 157 if ((changed) & |
153 | 158 (CONT_DPAD2_UP | CONT_DPAD2_DOWN | CONT_DPAD2_LEFT | |
154 for(i=0;i<sizeof(sdl_buttons)/sizeof(sdl_buttons[0]);i++) { | 159 CONT_DPAD2_RIGHT)) { |
155 if (changed & sdl_buttons[i]) { | 160 int hat = SDL_HAT_CENTERED; |
156 SDL_PrivateJoystickButton(joystick, i, (buttons & sdl_buttons[i])?SDL_PRESSED:SDL_RELEASED); | 161 if (buttons & CONT_DPAD2_UP) |
157 } | 162 hat |= SDL_HAT_UP; |
158 } | 163 if (buttons & CONT_DPAD2_DOWN) |
159 | 164 hat |= SDL_HAT_DOWN; |
160 prev_cond = &joystick->hwdata->prev_cond; | 165 if (buttons & CONT_DPAD2_LEFT) |
161 if (cond.joyx!=prev_cond->joyx) | 166 hat |= SDL_HAT_LEFT; |
162 SDL_PrivateJoystickAxis(joystick, 0, cond.joyx-128); | 167 if (buttons & CONT_DPAD2_RIGHT) |
163 if (cond.joyy!=prev_cond->joyy) | 168 hat |= SDL_HAT_RIGHT; |
164 SDL_PrivateJoystickAxis(joystick, 1, cond.joyy-128); | 169 SDL_PrivateJoystickHat(joystick, 1, hat); |
165 if (cond.rtrig!=prev_cond->rtrig) | 170 } |
166 SDL_PrivateJoystickAxis(joystick, 2, cond.rtrig); | 171 |
167 if (cond.ltrig!=prev_cond->ltrig) | 172 for (i = 0; i < sizeof(sdl_buttons) / sizeof(sdl_buttons[0]); i++) { |
168 SDL_PrivateJoystickAxis(joystick, 3, cond.ltrig); | 173 if (changed & sdl_buttons[i]) { |
169 if (cond.joy2x!=prev_cond->joy2x) | 174 SDL_PrivateJoystickButton(joystick, i, |
170 SDL_PrivateJoystickAxis(joystick, 4, cond.joy2x-128); | 175 (buttons & sdl_buttons[i]) ? |
171 if (cond.joy2y!=prev_cond->joy2y) | 176 SDL_PRESSED : SDL_RELEASED); |
172 SDL_PrivateJoystickAxis(joystick, 5, cond.joy2y-128); | 177 } |
173 | 178 } |
174 joystick->hwdata->prev_buttons = buttons; | 179 |
175 joystick->hwdata->prev_cond = cond; | 180 prev_cond = &joystick->hwdata->prev_cond; |
181 if (cond.joyx != prev_cond->joyx) | |
182 SDL_PrivateJoystickAxis(joystick, 0, cond.joyx - 128); | |
183 if (cond.joyy != prev_cond->joyy) | |
184 SDL_PrivateJoystickAxis(joystick, 1, cond.joyy - 128); | |
185 if (cond.rtrig != prev_cond->rtrig) | |
186 SDL_PrivateJoystickAxis(joystick, 2, cond.rtrig); | |
187 if (cond.ltrig != prev_cond->ltrig) | |
188 SDL_PrivateJoystickAxis(joystick, 3, cond.ltrig); | |
189 if (cond.joy2x != prev_cond->joy2x) | |
190 SDL_PrivateJoystickAxis(joystick, 4, cond.joy2x - 128); | |
191 if (cond.joy2y != prev_cond->joy2y) | |
192 SDL_PrivateJoystickAxis(joystick, 5, cond.joy2y - 128); | |
193 | |
194 joystick->hwdata->prev_buttons = buttons; | |
195 joystick->hwdata->prev_cond = cond; | |
176 } | 196 } |
177 | 197 |
178 /* Function to close a joystick after use */ | 198 /* Function to close a joystick after use */ |
179 void SDL_SYS_JoystickClose(SDL_Joystick *joystick) | 199 void |
180 { | 200 SDL_SYS_JoystickClose(SDL_Joystick * joystick) |
181 if (joystick->hwdata != NULL) { | 201 { |
182 /* free system specific hardware data */ | 202 if (joystick->hwdata != NULL) { |
183 SDL_free(joystick->hwdata); | 203 /* free system specific hardware data */ |
184 } | 204 SDL_free(joystick->hwdata); |
205 } | |
185 } | 206 } |
186 | 207 |
187 /* Function to perform any system-specific joystick related cleanup */ | 208 /* Function to perform any system-specific joystick related cleanup */ |
188 void SDL_SYS_JoystickQuit(void) | 209 void |
189 { | 210 SDL_SYS_JoystickQuit(void) |
190 return; | 211 { |
212 return; | |
191 } | 213 } |
192 | 214 |
193 #endif /* SDL_JOYSTICK_DC */ | 215 #endif /* SDL_JOYSTICK_DC */ |
216 /* vi: set ts=4 sw=4 expandtab: */ |