comparison src/joystick/bsd/SDL_sysjoystick.c @ 307:0185452e9f83

This properly scales axes, and adds support for sliders/wheels
author Sam Lantinga <slouken@libsdl.org>
date Sun, 10 Mar 2002 03:49:25 +0000
parents f6ffac90895c
children 1f148809d972
comparison
equal deleted inserted replaced
306:3879bed3395c 307:0185452e9f83
49 #include "SDL_joystick_c.h" 49 #include "SDL_joystick_c.h"
50 50
51 #define MAX_UHID_JOYS 4 51 #define MAX_UHID_JOYS 4
52 #define MAX_JOY_JOYS 2 52 #define MAX_JOY_JOYS 2
53 #define MAX_JOYS (MAX_UHID_JOYS + MAX_JOY_JOYS) 53 #define MAX_JOYS (MAX_UHID_JOYS + MAX_JOY_JOYS)
54
55 #define SDLAXIS_UINT8(v) \
56 ((v == 127) ? 0 : \
57 (v == 255) ? 32767 : \
58 -32767)
59 54
60 struct report { 55 struct report {
61 struct usb_ctl_report *buf; /* Buffer */ 56 struct usb_ctl_report *buf; /* Buffer */
62 size_t size; /* Buffer size */ 57 size_t size; /* Buffer size */
63 int rid; /* Report ID */ 58 int rid; /* Report ID */
75 } const repinfo[] = { 70 } const repinfo[] = {
76 { UHID_INPUT_REPORT, hid_input, "input" }, 71 { UHID_INPUT_REPORT, hid_input, "input" },
77 { UHID_OUTPUT_REPORT, hid_output, "output" }, 72 { UHID_OUTPUT_REPORT, hid_output, "output" },
78 { UHID_FEATURE_REPORT, hid_feature, "feature" } 73 { UHID_FEATURE_REPORT, hid_feature, "feature" }
79 }; 74 };
80 #define REPORT_INPUT 0 75
81 #define REPORT_OUTPUT 1 76 enum {
82 #define REPORT_FEATURE 2 77 REPORT_INPUT = 0,
78 REPORT_OUTPUT = 1,
79 REPORT_FEATURE = 2
80 };
81
82 enum {
83 JOYAXE_X,
84 JOYAXE_Y,
85 JOYAXE_Z,
86 JOYAXE_SLIDER,
87 JOYAXE_WHEEL
88 };
83 89
84 struct joystick_hwdata { 90 struct joystick_hwdata {
85 int fd; 91 int fd;
86 char *path; 92 char *path;
87 enum { 93 enum {
88 BSDJOY_UHID, /* uhid(4) */ 94 BSDJOY_UHID, /* uhid(4) */
89 BSDJOY_JOY /* joy(4) */ 95 BSDJOY_JOY /* joy(4) */
90 } type; 96 } type;
91 struct report_desc *repdesc; 97 struct report_desc *repdesc;
92 struct report inreport; 98 struct report inreport;
93 int axismin[3]; 99 #if 0
94 int axismax[3]; 100 int axismin[];
101 int axismax[];
102 #endif
95 }; 103 };
96 104
97 static char *joynames[MAX_JOYS]; 105 static char *joynames[MAX_JOYS];
98 static char *joydevnames[MAX_JOYS]; 106 static char *joydevnames[MAX_JOYS];
99 107
179 rep = &hw->inreport; 187 rep = &hw->inreport;
180 if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) { 188 if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) {
181 goto usberr; 189 goto usberr;
182 } 190 }
183 if (rep->size <= 0) { 191 if (rep->size <= 0) {
184 SDL_SetError("Input report descriptor has invalid length"); 192 SDL_SetError("%s: Input report descriptor has invalid length",
193 hw->path);
185 goto usberr; 194 goto usberr;
186 } 195 }
187 196
188 hdata = hid_start_parse(hw->repdesc, 1 << hid_input); 197 hdata = hid_start_parse(hw->repdesc, 1 << hid_input);
189 if (hdata == NULL) { 198 if (hdata == NULL) {
220 case HUP_GENERIC_DESKTOP: 229 case HUP_GENERIC_DESKTOP:
221 switch (HID_USAGE(hitem.usage)) { 230 switch (HID_USAGE(hitem.usage)) {
222 case HUG_X: 231 case HUG_X:
223 case HUG_Y: 232 case HUG_Y:
224 case HUG_Z: 233 case HUG_Z:
234 case HUG_SLIDER:
235 case HUG_WHEEL:
236 #if 0
225 hw->axismin[joy->naxes] = 237 hw->axismin[joy->naxes] =
226 hitem.logical_minimum; 238 hitem.logical_minimum;
227 hw->axismax[joy->naxes] = 239 hw->axismax[joy->naxes] =
228 hitem.logical_maximum; 240 hitem.logical_maximum;
241 #endif
229 joy->naxes++; 242 joy->naxes++;
230 break; 243 break;
231 } 244 }
232 break; 245 break;
233 case HUP_BUTTON: 246 case HUP_BUTTON:
255 void 268 void
256 SDL_SYS_JoystickUpdate(SDL_Joystick *joy) 269 SDL_SYS_JoystickUpdate(SDL_Joystick *joy)
257 { 270 {
258 static struct hid_item hitem; 271 static struct hid_item hitem;
259 static struct hid_data *hdata; 272 static struct hid_data *hdata;
260 static int nbutton, naxe, v, max, min;
261 static struct report *rep; 273 static struct report *rep;
274 int nbutton, naxe;
275 Sint32 v;
262 276
263 rep = &joy->hwdata->inreport; 277 rep = &joy->hwdata->inreport;
264 if (read(joy->hwdata->fd, rep->buf->data, rep->size) != rep->size) { 278 if (read(joy->hwdata->fd, rep->buf->data, rep->size) != rep->size) {
265 return; 279 return;
266 } 280 }
269 fprintf(stderr, "%s: Cannot start HID parser\n", 283 fprintf(stderr, "%s: Cannot start HID parser\n",
270 joy->hwdata->path); 284 joy->hwdata->path);
271 return; 285 return;
272 } 286 }
273 287
274 for (nbutton = 0, naxe = 0; hid_get_item(hdata, &hitem) > 0;) { 288 for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
275 switch (hitem.kind) { 289 switch (hitem.kind) {
276 case hid_input: 290 case hid_input:
277 switch (HID_PAGE(hitem.usage)) { 291 switch (HID_PAGE(hitem.usage)) {
278 case HUP_UNDEFINED: 292 case HUP_UNDEFINED:
279 continue; 293 continue;
280 case HUP_GENERIC_DESKTOP: 294 case HUP_GENERIC_DESKTOP:
281 switch (HID_USAGE(hitem.usage)) { 295 switch (HID_USAGE(hitem.usage)) {
282 case HUG_X: 296 case HUG_X:
297 naxe = JOYAXE_X;
298 goto scaleaxe;
283 case HUG_Y: 299 case HUG_Y:
300 naxe = JOYAXE_Y;
301 goto scaleaxe;
284 case HUG_Z: 302 case HUG_Z:
285 v = hid_get_data(rep->buf->data, 303 naxe = JOYAXE_Z;
286 &hitem); 304 goto scaleaxe;
287 305 case HUG_SLIDER:
288 /* 306 naxe = JOYAXE_SLIDER;
289 * XXX revisit later. need to test 307 goto scaleaxe;
290 * with more devices. 308 case HUG_WHEEL:
291 */ 309 naxe = JOYAXE_WHEEL;
292 if (joy->hwdata->axismin[naxe] == 0 && 310 goto scaleaxe;
293 joy->hwdata->axismax[naxe] == 255) { 311 }
294 v = SDLAXIS_UINT8(v); 312 scaleaxe:
313 v = (Sint32)hid_get_data(rep->buf->data, &hitem);
314 if (v != 127) {
315 if (v < 127) {
316 v = -(256 - v);
317 v <<= 7;
318 v++;
319 } else {
320 v++;
321 v <<= 7;
322 v--;
295 } 323 }
296 324 } else {
297 if (v != joy->axes[naxe]) { 325 v = 0;
298 SDL_PrivateJoystickAxis(joy, 326 }
299 naxe, (Sint32)v); 327 if (v != joy->axes[naxe]) {
300 } 328 SDL_PrivateJoystickAxis(joy, naxe, v);
301 naxe++;
302 break;
303 } 329 }
304 break; 330 break;
305 case HUP_BUTTON: 331 case HUP_BUTTON:
306 /* XXX assume a 0..1 range */ 332 v = (Sint32)hid_get_data(rep->buf->data,
307 v = hid_get_data(rep->buf->data, &hitem); 333 &hitem);
308 if (joy->buttons[nbutton] != v) { 334 if (joy->buttons[nbutton] != v) {
309 SDL_PrivateJoystickButton(joy, 335 SDL_PrivateJoystickButton(joy,
310 nbutton, v); 336 nbutton, v);
311 } 337 }
312 nbutton++; 338 nbutton++;