* Return early when opening the device failed and just return B_ERROR as

documented in the BeBook. Sadly the success return is specified as "a positive
  integer", so I didn't change it to B_OK.
* We actually want non-blocking mode, so don't reset the O_NONBLOCK flag. It was
  ignored before anyway though, so this doesn't change anything.
* The legacy buttons 1 and 2 are in the pressed state when false, so initialize
  them to true instead.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@41876 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Lotz
2011-06-03 12:15:46 +00:00
parent 30e429b4e4
commit 2682d2fbdd
+31 -43
View File
@@ -4,16 +4,11 @@
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#include <Joystick.h> #include <Joystick.h>
#include <JoystickTweaker.h> #include <JoystickTweaker.h>
#include <new> #include <new>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <Debug.h> #include <Debug.h>
@@ -53,8 +48,8 @@ BJoystick::BJoystick()
timestamp(0), timestamp(0),
horizontal(0), horizontal(0),
vertical(0), vertical(0),
button1(0), button1(true),
button2(0), button2(true),
fBeBoxMode(false), fBeBoxMode(false),
fFD(-1), fFD(-1),
@@ -129,49 +124,42 @@ BJoystick::Open(const char *portName, bool enhanced)
// TODO: BeOS don't use O_EXCL, and this seems to lead to some issues. I // TODO: BeOS don't use O_EXCL, and this seems to lead to some issues. I
// added this flag having read some comments by Marco Nelissen on the // added this flag having read some comments by Marco Nelissen on the
// annotated BeBook. I think BeOS uses O_RDWR|O_NONBLOCK here. // annotated BeBook. I think BeOS uses O_RDWR | O_NONBLOCK here.
fFD = open(nameBuffer, O_RDWR | O_NONBLOCK | O_EXCL); fFD = open(nameBuffer, O_RDWR | O_NONBLOCK | O_EXCL);
if (fFD < 0)
return B_ERROR;
if (fFD >= 0) { // read the Joystick Description file for this port/joystick
// we used open() with O_NONBLOCK flag to let it return immediately, _BJoystickTweaker joystickTweaker(*this);
// but we want read/write operations to block if needed, so we clear joystickTweaker.GetInfo(fJoystickInfo, portName);
// that bit here.
int flags = fcntl(fFD, F_GETFL);
fcntl(fFD, F_SETFL, flags & ~O_NONBLOCK);
// read the Joystick Description file for this port/joystick LOG("ioctl - %d\n", fJoystickInfo->module_info.num_buttons);
_BJoystickTweaker joystickTweaker(*this); ioctl(fFD, B_JOYSTICK_SET_DEVICE_MODULE, &fJoystickInfo->module_info,
joystickTweaker.GetInfo(fJoystickInfo, portName); sizeof(joystick_module_info));
ioctl(fFD, B_JOYSTICK_GET_DEVICE_MODULE, &fJoystickInfo->module_info,
sizeof(joystick_module_info));
LOG("ioctl - %d\n", fJoystickInfo->module_info.num_buttons);
LOG("ioctl - %d\n", fJoystickInfo->module_info.num_buttons); // Allocate the extended_joystick structures to hold the info for each
ioctl(fFD, B_JOYSTICK_SET_DEVICE_MODULE, &fJoystickInfo->module_info, // "stick". Note that the whole num_sticks thing seems a bit bogus, as
sizeof(joystick_module_info)); // all sticks would be required to have exactly the same attributes,
ioctl(fFD, B_JOYSTICK_GET_DEVICE_MODULE, &fJoystickInfo->module_info, // i.e. axis, hat and button counts, since there is only one global
sizeof(joystick_module_info)); // joystick_info for the whole device. What's implemented here is a
LOG("ioctl - %d\n", fJoystickInfo->module_info.num_buttons); // "best guess", using the read position in Update() to select the
// stick for which an extended_joystick structure shall be returned.
for (uint16 i = 0; i < fJoystickInfo->module_info.num_sticks; i++) {
extended_joystick *extendedJoystick
= new(std::nothrow) extended_joystick;
if (extendedJoystick == NULL)
return B_NO_MEMORY;
// Allocate the extended_joystick structures to hold the info for each if (!fExtendedJoystick->AddItem(extendedJoystick)) {
// "stick". Note that the whole num_sticks thing seems a bit bogus, as delete extendedJoystick;
// all sticks would be required to have exactly the same attributes, return B_NO_MEMORY;
// i.e. axis, hat and button counts, since there is only one global
// joystick_info for the whole device. What's implemented here is a
// "best guess", using the read position in Update() to select the
// stick for which an extended_joystick structure shall be returned.
for (uint16 i = 0; i < fJoystickInfo->module_info.num_sticks; i++) {
extended_joystick *extendedJoystick
= new(std::nothrow) extended_joystick;
if (extendedJoystick == NULL)
return B_NO_MEMORY;
if (!fExtendedJoystick->AddItem(extendedJoystick)) {
delete extendedJoystick;
return B_NO_MEMORY;
}
} }
}
return fFD; return fFD;
} else
return errno;
} }