Commit d45cd06f authored by Sam Lantinga's avatar Sam Lantinga

This properly scales axes, and adds support for sliders/wheels

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%40308
parent fab7d1c3
...@@ -52,11 +52,6 @@ static char rcsid = ...@@ -52,11 +52,6 @@ static char rcsid =
#define MAX_JOY_JOYS 2 #define MAX_JOY_JOYS 2
#define MAX_JOYS (MAX_UHID_JOYS + MAX_JOY_JOYS) #define MAX_JOYS (MAX_UHID_JOYS + MAX_JOY_JOYS)
#define SDLAXIS_UINT8(v) \
((v == 127) ? 0 : \
(v == 255) ? 32767 : \
-32767)
struct report { struct report {
struct usb_ctl_report *buf; /* Buffer */ struct usb_ctl_report *buf; /* Buffer */
size_t size; /* Buffer size */ size_t size; /* Buffer size */
...@@ -77,9 +72,20 @@ static struct { ...@@ -77,9 +72,20 @@ static struct {
{ UHID_OUTPUT_REPORT, hid_output, "output" }, { UHID_OUTPUT_REPORT, hid_output, "output" },
{ UHID_FEATURE_REPORT, hid_feature, "feature" } { UHID_FEATURE_REPORT, hid_feature, "feature" }
}; };
#define REPORT_INPUT 0
#define REPORT_OUTPUT 1 enum {
#define REPORT_FEATURE 2 REPORT_INPUT = 0,
REPORT_OUTPUT = 1,
REPORT_FEATURE = 2
};
enum {
JOYAXE_X,
JOYAXE_Y,
JOYAXE_Z,
JOYAXE_SLIDER,
JOYAXE_WHEEL
};
struct joystick_hwdata { struct joystick_hwdata {
int fd; int fd;
...@@ -90,8 +96,10 @@ struct joystick_hwdata { ...@@ -90,8 +96,10 @@ struct joystick_hwdata {
} type; } type;
struct report_desc *repdesc; struct report_desc *repdesc;
struct report inreport; struct report inreport;
int axismin[3]; #if 0
int axismax[3]; int axismin[];
int axismax[];
#endif
}; };
static char *joynames[MAX_JOYS]; static char *joynames[MAX_JOYS];
...@@ -181,7 +189,8 @@ SDL_SYS_JoystickOpen(SDL_Joystick *joy) ...@@ -181,7 +189,8 @@ SDL_SYS_JoystickOpen(SDL_Joystick *joy)
goto usberr; goto usberr;
} }
if (rep->size <= 0) { if (rep->size <= 0) {
SDL_SetError("Input report descriptor has invalid length"); SDL_SetError("%s: Input report descriptor has invalid length",
hw->path);
goto usberr; goto usberr;
} }
...@@ -222,10 +231,14 @@ SDL_SYS_JoystickOpen(SDL_Joystick *joy) ...@@ -222,10 +231,14 @@ SDL_SYS_JoystickOpen(SDL_Joystick *joy)
case HUG_X: case HUG_X:
case HUG_Y: case HUG_Y:
case HUG_Z: case HUG_Z:
case HUG_SLIDER:
case HUG_WHEEL:
#if 0
hw->axismin[joy->naxes] = hw->axismin[joy->naxes] =
hitem.logical_minimum; hitem.logical_minimum;
hw->axismax[joy->naxes] = hw->axismax[joy->naxes] =
hitem.logical_maximum; hitem.logical_maximum;
#endif
joy->naxes++; joy->naxes++;
break; break;
} }
...@@ -257,8 +270,9 @@ SDL_SYS_JoystickUpdate(SDL_Joystick *joy) ...@@ -257,8 +270,9 @@ SDL_SYS_JoystickUpdate(SDL_Joystick *joy)
{ {
static struct hid_item hitem; static struct hid_item hitem;
static struct hid_data *hdata; static struct hid_data *hdata;
static int nbutton, naxe, v, max, min;
static struct report *rep; static struct report *rep;
int nbutton, naxe;
Sint32 v;
rep = &joy->hwdata->inreport; rep = &joy->hwdata->inreport;
if (read(joy->hwdata->fd, rep->buf->data, rep->size) != rep->size) { if (read(joy->hwdata->fd, rep->buf->data, rep->size) != rep->size) {
...@@ -271,7 +285,7 @@ SDL_SYS_JoystickUpdate(SDL_Joystick *joy) ...@@ -271,7 +285,7 @@ SDL_SYS_JoystickUpdate(SDL_Joystick *joy)
return; return;
} }
for (nbutton = 0, naxe = 0; hid_get_item(hdata, &hitem) > 0;) { for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
switch (hitem.kind) { switch (hitem.kind) {
case hid_input: case hid_input:
switch (HID_PAGE(hitem.usage)) { switch (HID_PAGE(hitem.usage)) {
...@@ -280,31 +294,43 @@ SDL_SYS_JoystickUpdate(SDL_Joystick *joy) ...@@ -280,31 +294,43 @@ SDL_SYS_JoystickUpdate(SDL_Joystick *joy)
case HUP_GENERIC_DESKTOP: case HUP_GENERIC_DESKTOP:
switch (HID_USAGE(hitem.usage)) { switch (HID_USAGE(hitem.usage)) {
case HUG_X: case HUG_X:
naxe = JOYAXE_X;
goto scaleaxe;
case HUG_Y: case HUG_Y:
naxe = JOYAXE_Y;
goto scaleaxe;
case HUG_Z: case HUG_Z:
v = hid_get_data(rep->buf->data, naxe = JOYAXE_Z;
&hitem); goto scaleaxe;
case HUG_SLIDER:
/* naxe = JOYAXE_SLIDER;
* XXX revisit later. need to test goto scaleaxe;
* with more devices. case HUG_WHEEL:
*/ naxe = JOYAXE_WHEEL;
if (joy->hwdata->axismin[naxe] == 0 && goto scaleaxe;
joy->hwdata->axismax[naxe] == 255) { }
v = SDLAXIS_UINT8(v); scaleaxe:
} v = (Sint32)hid_get_data(rep->buf->data, &hitem);
if (v != 127) {
if (v != joy->axes[naxe]) { if (v < 127) {
SDL_PrivateJoystickAxis(joy, v = -(256 - v);
naxe, (Sint32)v); v <<= 7;
v++;
} else {
v++;
v <<= 7;
v--;
} }
naxe++; } else {
break; v = 0;
}
if (v != joy->axes[naxe]) {
SDL_PrivateJoystickAxis(joy, naxe, v);
} }
break; break;
case HUP_BUTTON: case HUP_BUTTON:
/* XXX assume a 0..1 range */ v = (Sint32)hid_get_data(rep->buf->data,
v = hid_get_data(rep->buf->data, &hitem); &hitem);
if (joy->buttons[nbutton] != v) { if (joy->buttons[nbutton] != v) {
SDL_PrivateJoystickButton(joy, SDL_PrivateJoystickButton(joy,
nbutton, v); nbutton, v);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment