mirror of
https://github.com/X11Libre/xf86-input-joystick.git
synced 2026-04-14 11:54:23 +00:00
Probe device once on INIT and re-use found information.
Makes reopening a device on vt-enter much smoother and throws less warnings. Remember number of found axes and buttons.
This commit is contained in:
@@ -55,12 +55,15 @@ struct jstk_bsd_hid_data {
|
||||
struct hid_item button_item[MAXBUTTONS]; /* Button HID items */
|
||||
struct hid_item hat_item[MAXAXES]; /* HID items for hats */
|
||||
int hats; /* Number of hats */
|
||||
int axes; /* Number of found axes */
|
||||
int buttons; /* Number of found buttons */
|
||||
int hotdata; /* Is unprocessed data available
|
||||
in data_buf? */
|
||||
};
|
||||
|
||||
static void jstkCloseDevice_bsd(JoystickDevPtr joystick);
|
||||
static int jstkReadData_bsd(JoystickDevPtr joystick,
|
||||
JOYSTICKEVENT *event,
|
||||
int *number);
|
||||
|
||||
|
||||
|
||||
/***********************************************************************
|
||||
@@ -74,7 +77,7 @@ struct jstk_bsd_hid_data {
|
||||
*/
|
||||
|
||||
int
|
||||
jstkOpenDevice_bsd(JoystickDevPtr joystick)
|
||||
jstkOpenDevice_bsd(JoystickDevPtr joystick, Bool probe)
|
||||
{
|
||||
int cur_axis;
|
||||
int is_joystick, report_id = 0;
|
||||
@@ -123,8 +126,8 @@ jstkOpenDevice_bsd(JoystickDevPtr joystick)
|
||||
got_something = 0;
|
||||
cur_axis = 0;
|
||||
bsddata->hats = 0;
|
||||
bsddata->axes = 0;
|
||||
bsddata->buttons = 0;
|
||||
joystick->axes = 0;
|
||||
joystick->buttons = 0;
|
||||
|
||||
for (d = hid_start_parse(rd, 1 << hid_input, report_id);
|
||||
hid_get_item(d, &h); )
|
||||
@@ -147,25 +150,25 @@ jstkOpenDevice_bsd(JoystickDevPtr joystick)
|
||||
|
||||
if (page == HUP_GENERIC_DESKTOP) {
|
||||
if (usage == HUG_HAT_SWITCH) {
|
||||
if ((bsddata->hats < MAXAXES) && (bsddata->axes <= MAXAXES-2)) {
|
||||
if ((bsddata->hats < MAXAXES) && (joystick->axes <= MAXAXES-2)) {
|
||||
got_something = 1;
|
||||
memcpy(&bsddata->hat_item[bsddata->hats], &h, sizeof(h));
|
||||
bsddata->hats++;
|
||||
bsddata->axes += 2;
|
||||
joystick->axes += 2;
|
||||
}
|
||||
} else {
|
||||
if (bsddata->axes < MAXAXES) {
|
||||
if (joystick->axes < MAXAXES) {
|
||||
got_something = 1;
|
||||
memcpy(&bsddata->axis_item[cur_axis], &h, sizeof(h));
|
||||
cur_axis++;
|
||||
bsddata->axes++;
|
||||
joystick->axes++;
|
||||
}
|
||||
}
|
||||
} else if (page == HUP_BUTTON) {
|
||||
if (bsddata->buttons < MAXBUTTONS) {
|
||||
if (joystick->buttons < MAXBUTTONS) {
|
||||
got_something = 1;
|
||||
memcpy(&bsddata->button_item[bsddata->buttons], &h, sizeof(h));
|
||||
bsddata->buttons++;
|
||||
memcpy(&bsddata->button_item[joystick->buttons], &h, sizeof(h));
|
||||
joystick->buttons++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -182,9 +185,12 @@ jstkOpenDevice_bsd(JoystickDevPtr joystick)
|
||||
|
||||
bsddata->hotdata = 0;
|
||||
joystick->devicedata = (void*) bsddata;
|
||||
xf86Msg(X_INFO, "Joystick: %d buttons, %d axes\n",
|
||||
bsddata->buttons, bsddata->axes);
|
||||
if (Probe == TRUE) {
|
||||
xf86Msg(X_INFO, "Joystick: %d buttons, %d axes\n",
|
||||
joystick->buttons, joystick->axes);
|
||||
}
|
||||
|
||||
joystick->open_proc = jstkOpenDevice_bsd;
|
||||
joystick->read_proc = jstkReadData_bsd;
|
||||
joystick->close_proc = jstkCloseDevice_bsd;
|
||||
|
||||
@@ -201,7 +207,7 @@ jstkOpenDevice_bsd(JoystickDevPtr joystick)
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
void
|
||||
static void
|
||||
jstkCloseDevice_bsd(JoystickDevPtr joystick)
|
||||
{
|
||||
if ((joystick->fd >= 0)) {
|
||||
@@ -228,7 +234,7 @@ jstkCloseDevice_bsd(JoystickDevPtr joystick)
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
int
|
||||
static int
|
||||
jstkReadData_bsd(JoystickDevPtr joystick,
|
||||
JOYSTICKEVENT *event,
|
||||
int *number)
|
||||
@@ -249,7 +255,7 @@ jstkReadData_bsd(JoystickDevPtr joystick,
|
||||
bsddata->hotdata = 1;
|
||||
}
|
||||
|
||||
for (j=0; j<bsddata->axes - (bsddata->hats * 2); j++) {
|
||||
for (j=0; j<joystick->axes - (bsddata->hats * 2); j++) {
|
||||
d = hid_get_data(bsddata->data_buf, &bsddata->axis_item[j]);
|
||||
/* Scale the range to our expected range of -32768 to 32767 */
|
||||
d = d - (bsddata->axis_item[j].logical_maximum
|
||||
@@ -273,7 +279,7 @@ jstkReadData_bsd(JoystickDevPtr joystick,
|
||||
int v2_data[9] =
|
||||
{ -32768, -32768, 0, 32767, 32767, 32767, 0, -32767, 0 };
|
||||
|
||||
a = j*2 + bsddata->axes - bsddata->hats *2;
|
||||
a = j*2 + joystick->axes - bsddata->hats *2;
|
||||
d = hid_get_data(bsddata->data_buf, &bsddata->hat_item[j])
|
||||
- bsddata->hat_item[j].logical_minimum;
|
||||
if (joystick->axis[a].value != v1_data[d]) {
|
||||
@@ -292,7 +298,7 @@ jstkReadData_bsd(JoystickDevPtr joystick,
|
||||
}
|
||||
}
|
||||
|
||||
for (j=0; j<bsddata->buttons; j++) {
|
||||
for (j=0; j<joystick->buttons; j++) {
|
||||
int pressed;
|
||||
d = hid_get_data(bsddata->data_buf, &bsddata->button_item[j]);
|
||||
pressed = (d == bsddata->button_item[j].logical_minimum) ? 0 : 1;
|
||||
|
||||
@@ -26,10 +26,6 @@
|
||||
|
||||
#include "jstk.h"
|
||||
|
||||
int jstkOpenDevice_bsd(JoystickDevPtr joystick);
|
||||
void jstkCloseDevice_bsd(JoystickDevPtr joystick);
|
||||
int jstkReadData_bsd(JoystickDevPtr joystick,
|
||||
JOYSTICKEVENT *event,
|
||||
int *number);
|
||||
int jstkOpenDevice_bsd(JoystickDevPtr joystick, Bool probe);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -61,6 +61,11 @@ struct jstk_evdev_data {
|
||||
};
|
||||
|
||||
|
||||
static void jstkCloseDevice_evdev(JoystickDevPtr joystick);
|
||||
static int jstkReadData_evdev(JoystickDevPtr joystick,
|
||||
JOYSTICKEVENT *event,
|
||||
int *number);
|
||||
|
||||
/***********************************************************************
|
||||
*
|
||||
* jstkOpenDevice --
|
||||
@@ -73,7 +78,7 @@ struct jstk_evdev_data {
|
||||
*/
|
||||
|
||||
int
|
||||
jstkOpenDevice_evdev(JoystickDevPtr joystick)
|
||||
jstkOpenDevice_evdev(JoystickDevPtr joystick, Bool probe)
|
||||
{
|
||||
int driver_version;
|
||||
char name[256];
|
||||
@@ -176,13 +181,19 @@ jstkOpenDevice_evdev(JoystickDevPtr joystick)
|
||||
if (ioctl(joystick->fd, EVIOCGUNIQ(sizeof(uniq)), uniq) == -1)
|
||||
strcpy(uniq, "No name");
|
||||
|
||||
xf86Msg(X_INFO, "Joystick: %s. bus 0x%x vendor 0x%x product 0x%x version 0x%x\n",
|
||||
name, id.bustype, id.vendor, id.product, id.version);
|
||||
xf86Msg(X_INFO, "Joystick: found %d axes, %d buttons\n", axes, buttons);
|
||||
if (probe == TRUE) {
|
||||
xf86Msg(X_INFO, "Joystick: %s. bus 0x%x vendor 0x%x product 0x%x version 0x%x\n",
|
||||
name, id.bustype, id.vendor, id.product, id.version);
|
||||
xf86Msg(X_INFO, "Joystick: found %d axes, %d buttons\n", axes, buttons);
|
||||
}
|
||||
|
||||
joystick->open_proc = jstkOpenDevice_evdev;
|
||||
joystick->read_proc = jstkReadData_evdev;
|
||||
joystick->close_proc = jstkCloseDevice_evdev;
|
||||
joystick->devicedata = (void*) evdevdata;
|
||||
|
||||
joystick->num_buttons = buttons;
|
||||
joystick->num_axes = axes;
|
||||
return joystick->fd;
|
||||
}
|
||||
|
||||
@@ -196,7 +207,7 @@ jstkOpenDevice_evdev(JoystickDevPtr joystick)
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
void
|
||||
static void
|
||||
jstkCloseDevice_evdev(JoystickDevPtr joystick)
|
||||
{
|
||||
if ((joystick->fd >= 0)) {
|
||||
@@ -222,7 +233,7 @@ jstkCloseDevice_evdev(JoystickDevPtr joystick)
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
int
|
||||
static int
|
||||
jstkReadData_evdev(JoystickDevPtr joystick,
|
||||
JOYSTICKEVENT *event,
|
||||
int *number)
|
||||
|
||||
@@ -26,10 +26,6 @@
|
||||
|
||||
#include "jstk.h"
|
||||
|
||||
int jstkOpenDevice_evdev(JoystickDevPtr joystick);
|
||||
void jstkCloseDevice_evdev(JoystickDevPtr joystick);
|
||||
int jstkReadData_evdev(JoystickDevPtr joystick,
|
||||
JOYSTICKEVENT *event,
|
||||
int *number);
|
||||
int jstkOpenDevice_evdev(JoystickDevPtr joystick, Bool probe);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -48,6 +48,12 @@
|
||||
#include "backend_joystick.h"
|
||||
|
||||
|
||||
static void jstkCloseDevice_joystick(JoystickDevPtr joystick);
|
||||
static int jstkReadData_joystick(JoystickDevPtr joystick,
|
||||
JOYSTICKEVENT *event,
|
||||
int *number);
|
||||
|
||||
|
||||
/***********************************************************************
|
||||
*
|
||||
* jstkOpenDevice --
|
||||
@@ -60,7 +66,7 @@
|
||||
*/
|
||||
|
||||
int
|
||||
jstkOpenDevice_joystick(JoystickDevPtr joystick)
|
||||
jstkOpenDevice_joystick(JoystickDevPtr joystick, Bool probe)
|
||||
{
|
||||
char joy_name[128];
|
||||
unsigned char axes, buttons;
|
||||
@@ -110,9 +116,12 @@ jstkOpenDevice_joystick(JoystickDevPtr joystick)
|
||||
return -1;
|
||||
}
|
||||
|
||||
xf86Msg(X_INFO, "Joystick: %s. %d axes, %d buttons\n",
|
||||
joy_name, axes, buttons);
|
||||
if (probe == TRUE) {
|
||||
xf86Msg(X_INFO, "Joystick: %s. %d axes, %d buttons\n",
|
||||
joy_name, axes, buttons);
|
||||
}
|
||||
|
||||
joystick->open_proc = jstkOpenDevice_joystick;
|
||||
joystick->read_proc = jstkReadData_joystick;
|
||||
joystick->close_proc = jstkCloseDevice_joystick;
|
||||
return joystick->fd;
|
||||
@@ -128,7 +137,7 @@ jstkOpenDevice_joystick(JoystickDevPtr joystick)
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
void
|
||||
static void
|
||||
jstkCloseDevice_joystick(JoystickDevPtr joystick)
|
||||
{
|
||||
if ((joystick->fd >= 0)) {
|
||||
@@ -150,7 +159,7 @@ jstkCloseDevice_joystick(JoystickDevPtr joystick)
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
int
|
||||
static int
|
||||
jstkReadData_joystick(JoystickDevPtr joystick,
|
||||
JOYSTICKEVENT *event,
|
||||
int *number)
|
||||
|
||||
@@ -26,10 +26,6 @@
|
||||
|
||||
#include "jstk.h"
|
||||
|
||||
int jstkOpenDevice_joystick(JoystickDevPtr joystick);
|
||||
void jstkCloseDevice_joystick(JoystickDevPtr joystick);
|
||||
int jstkReadData_joystick(JoystickDevPtr joystick,
|
||||
JOYSTICKEVENT *event,
|
||||
int *number);
|
||||
int jstkOpenDevice_joystick(JoystickDevPtr joystick, Bool probe);
|
||||
|
||||
#endif
|
||||
|
||||
26
src/jstk.c
26
src/jstk.c
@@ -107,22 +107,25 @@ jstkConvertProc(LocalDevicePtr local,
|
||||
***************************************************************************
|
||||
*/
|
||||
static int
|
||||
jstkOpenDevice(JoystickDevPtr priv)
|
||||
jstkOpenDevice(JoystickDevPtr priv, BOOL probe)
|
||||
{
|
||||
int fd;
|
||||
fd = -1;
|
||||
|
||||
if (probe == FALSE && priv->open_proc)
|
||||
return priv->open_proc(priv, probe);
|
||||
|
||||
#ifdef EVDEV_BACKEND
|
||||
if (fd == -1)
|
||||
fd = jstkOpenDevice_evdev(priv);
|
||||
fd = jstkOpenDevice_evdev(priv, probe);
|
||||
#endif
|
||||
#ifdef LINUX_BACKEND
|
||||
if (fd == -1)
|
||||
fd = jstkOpenDevice_joystick(priv);
|
||||
fd = jstkOpenDevice_joystick(priv, probe);
|
||||
#endif
|
||||
#ifdef BSD_BACKEND
|
||||
if (fd == -1)
|
||||
fd = jstkOpenDevice_bsd(priv);
|
||||
fd = jstkOpenDevice_bsd(priv, probe);
|
||||
#endif
|
||||
|
||||
return fd;
|
||||
@@ -339,6 +342,16 @@ jstkDeviceControlProc(DeviceIntPtr pJstk,
|
||||
case DEVICE_INIT: {
|
||||
int m;
|
||||
DBG(1, ErrorF("jstkDeviceControlProc what=INIT\n"));
|
||||
/* Probe device and return if error */
|
||||
if (jstkOpenDevice(priv, TRUE) == -1) {
|
||||
return !Success;
|
||||
} else {
|
||||
/* Success. The OpenDevice call already did some initialization
|
||||
like priv->num_buttons, priv->num_axes */
|
||||
priv->close_proc(priv);
|
||||
}
|
||||
|
||||
|
||||
if (priv->buttonmap.size != 0) {
|
||||
if (InitButtonClassDeviceStruct(pJstk, priv->buttonmap.size,
|
||||
priv->buttonmap.map) == FALSE) {
|
||||
@@ -415,7 +428,7 @@ jstkDeviceControlProc(DeviceIntPtr pJstk,
|
||||
DBG(1, ErrorF("jstkDeviceControlProc what=ON name=%s\n",
|
||||
priv->device));
|
||||
|
||||
if (jstkOpenDevice(priv) != -1) {
|
||||
if (jstkOpenDevice(priv, FALSE) != -1) {
|
||||
pJstk->public.on = TRUE;
|
||||
local->fd = priv->fd;
|
||||
AddEnabledDevice(local->fd);
|
||||
@@ -507,6 +520,9 @@ jstkCorePreInit(InputDriverPtr drv, IDevPtr dev, int flags)
|
||||
local->conf_idev = dev;
|
||||
|
||||
priv->fd = -1;
|
||||
priv->open_proc = NULL;
|
||||
priv->read_proc = NULL;
|
||||
priv->close_proc = NULL;
|
||||
priv->device = NULL;
|
||||
priv->devicedata = NULL;
|
||||
priv->timer = NULL;
|
||||
|
||||
@@ -62,6 +62,7 @@ typedef enum _JOYSTICKEVENT {
|
||||
|
||||
typedef struct _JoystickDevRec *JoystickDevPtr;
|
||||
|
||||
typedef int(*jstkOpenDeviceProc)(JoystickDevPtr joystick, Bool probe);
|
||||
typedef void(*jstkCloseDeviceProc)(JoystickDevPtr joystick);
|
||||
typedef int(*jstkReadDataProc)(JoystickDevPtr joystick,
|
||||
JOYSTICKEVENT *event, int *number);
|
||||
@@ -116,6 +117,7 @@ typedef struct _BUTTON {
|
||||
|
||||
typedef struct _JoystickDevRec {
|
||||
int fd; /* Actual file descriptor */
|
||||
jstkOpenDeviceProc open_proc; /* Call for re-open backend */
|
||||
jstkCloseDeviceProc close_proc; /* Callback for closing the backend */
|
||||
jstkReadDataProc read_proc; /* Callback for reading data from the backend */
|
||||
void *devicedata; /* Extra platform device dependend data */
|
||||
|
||||
Reference in New Issue
Block a user