Fixed bug 2090 - Some joystick inputs are delayed on FreeBSD

kikuchan

Some joysticks with high sampling rate need to be read() more fast,
otherwise it delay user inputs due to internal queue.
Especially, an app that issues SDL_PollEvent() not so frequent
Sam Lantinga 2013-09-06 20:54:14 -07:00
parent 10ffa28a28
commit dc9ddf1f61
1 changed files with 80 additions and 84 deletions

View File

@ -446,48 +446,47 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joy)
static int x, y, xmin = 0xffff, ymin = 0xffff, xmax = 0, ymax = 0; static int x, y, xmin = 0xffff, ymin = 0xffff, xmax = 0, ymax = 0;
if (joy->hwdata->type == BSDJOY_JOY) { if (joy->hwdata->type == BSDJOY_JOY) {
if (read(joy->hwdata->fd, &gameport, sizeof gameport) != while (read(joy->hwdata->fd, &gameport, sizeof gameport) == sizeof gameport) {
sizeof gameport) if (abs(x - gameport.x) > 8) {
return; x = gameport.x;
if (abs(x - gameport.x) > 8) { if (x < xmin) {
x = gameport.x; xmin = x;
if (x < xmin) { }
xmin = x; if (x > xmax) {
xmax = x;
}
if (xmin == xmax) {
xmin--;
xmax++;
}
v = (Sint32) x;
v -= (xmax + xmin + 1) / 2;
v *= 32768 / ((xmax - xmin + 1) / 2);
SDL_PrivateJoystickAxis(joy, 0, v);
} }
if (x > xmax) { if (abs(y - gameport.y) > 8) {
xmax = x; y = gameport.y;
if (y < ymin) {
ymin = y;
}
if (y > ymax) {
ymax = y;
}
if (ymin == ymax) {
ymin--;
ymax++;
}
v = (Sint32) y;
v -= (ymax + ymin + 1) / 2;
v *= 32768 / ((ymax - ymin + 1) / 2);
SDL_PrivateJoystickAxis(joy, 1, v);
} }
if (xmin == xmax) { if (gameport.b1 != joy->buttons[0]) {
xmin--; SDL_PrivateJoystickButton(joy, 0, gameport.b1);
xmax++;
} }
v = (Sint32) x; if (gameport.b2 != joy->buttons[1]) {
v -= (xmax + xmin + 1) / 2; SDL_PrivateJoystickButton(joy, 1, gameport.b2);
v *= 32768 / ((xmax - xmin + 1) / 2);
SDL_PrivateJoystickAxis(joy, 0, v);
}
if (abs(y - gameport.y) > 8) {
y = gameport.y;
if (y < ymin) {
ymin = y;
} }
if (y > ymax) {
ymax = y;
}
if (ymin == ymax) {
ymin--;
ymax++;
}
v = (Sint32) y;
v -= (ymax + ymin + 1) / 2;
v *= 32768 / ((ymax - ymin + 1) / 2);
SDL_PrivateJoystickAxis(joy, 1, v);
}
if (gameport.b1 != joy->buttons[0]) {
SDL_PrivateJoystickButton(joy, 0, gameport.b1);
}
if (gameport.b2 != joy->buttons[1]) {
SDL_PrivateJoystickButton(joy, 1, gameport.b2);
} }
return; return;
} }
@ -495,65 +494,62 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joy)
rep = &joy->hwdata->inreport; rep = &joy->hwdata->inreport;
if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) { while (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) == rep->size) {
return;
}
#if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_kernel_version >= 500111) || defined(__FreeBSD_kernel__) #if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_kernel_version >= 500111) || defined(__FreeBSD_kernel__)
hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid); hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid);
#else #else
hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input); hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
#endif #endif
if (hdata == NULL) { if (hdata == NULL) {
fprintf(stderr, "%s: Cannot start HID parser\n", joy->hwdata->path); /*fprintf(stderr, "%s: Cannot start HID parser\n", joy->hwdata->path);*/
return; continue;
} }
for (nbutton = 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)) {
case HUP_GENERIC_DESKTOP: case HUP_GENERIC_DESKTOP:
{ {
unsigned usage = HID_USAGE(hitem.usage); unsigned usage = HID_USAGE(hitem.usage);
int joyaxe = usage_to_joyaxe(usage); int joyaxe = usage_to_joyaxe(usage);
if (joyaxe >= 0) { if (joyaxe >= 0) {
naxe = joy->hwdata->axis_map[joyaxe]; naxe = joy->hwdata->axis_map[joyaxe];
/* scaleaxe */ /* scaleaxe */
v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem); v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
v -= (hitem.logical_maximum + v -= (hitem.logical_maximum +
hitem.logical_minimum + 1) / 2; hitem.logical_minimum + 1) / 2;
v *= 32768 / v *= 32768 /
((hitem.logical_maximum - ((hitem.logical_maximum -
hitem.logical_minimum + 1) / 2); hitem.logical_minimum + 1) / 2);
if (v != joy->axes[naxe]) { if (v != joy->axes[naxe]) {
SDL_PrivateJoystickAxis(joy, naxe, v); SDL_PrivateJoystickAxis(joy, naxe, v);
}
} else if (usage == HUG_HAT_SWITCH) {
v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
SDL_PrivateJoystickHat(joy, 0,
hatval_to_sdl(v) -
hitem.logical_minimum);
} }
} else if (usage == HUG_HAT_SWITCH) { break;
v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
SDL_PrivateJoystickHat(joy, 0,
hatval_to_sdl(v) -
hitem.logical_minimum);
} }
case HUP_BUTTON:
v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
if (joy->buttons[nbutton] != v) {
SDL_PrivateJoystickButton(joy, nbutton, v);
}
nbutton++;
break; break;
default:
continue;
} }
case HUP_BUTTON:
v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
if (joy->buttons[nbutton] != v) {
SDL_PrivateJoystickButton(joy, nbutton, v);
}
nbutton++;
break; break;
default: default:
continue; break;
} }
break;
default:
break;
} }
hid_end_parse(hdata);
} }
hid_end_parse(hdata);
return;
} }
/* Function to close a joystick after use */ /* Function to close a joystick after use */