12 #include <mrpt/config.h>
20 #if !defined(__GNUC__)
21 #pragma comment(lib, "WINMM.LIB")
25 #if defined(MRPT_OS_LINUX) || defined(__APPLE__)
27 #include <sys/ioctl.h>
29 #include <sys/types.h>
39 #if defined(MRPT_OS_LINUX) && defined(HAVE_LINUX_INPUT_H)
40 #include <linux/input.h>
41 #include <linux/joystick.h>
53 #if defined(MRPT_OS_LINUX) && defined(HAVE_LINUX_INPUT_H)
54 : m_joy_fd(-1), m_joy_index(-1)
65 #if defined(MRPT_OS_LINUX) && defined(HAVE_LINUX_INPUT_H)
83 return joyGetNumDevs();
84 #elif defined(MRPT_OS_LINUX) && defined(HAVE_LINUX_INPUT_H)
92 (joy_fd = open(
format(
"/dev/input/js%i", nJoys).c_str(), O_RDONLY)))
97 }
while (joy_fd != -1);
114 int nJoy,
float&
x,
float&
y,
float&
z, std::vector<bool>& buttons,
115 int* raw_x_pos,
int* raw_y_pos,
int* raw_z_pos)
121 int ID = JOYSTICKID1 + nJoy;
124 if (JOYERR_NOERROR != joyGetPos(ID, &jinfo))
return false;
137 buttons[0] = 0 != (jinfo.wButtons & JOY_BUTTON1);
138 buttons[1] = 0 != (jinfo.wButtons & JOY_BUTTON2);
139 buttons[2] = 0 != (jinfo.wButtons & JOY_BUTTON3);
140 buttons[3] = 0 != (jinfo.wButtons & JOY_BUTTON4);
142 if (raw_x_pos) *raw_x_pos = jinfo.wXpos;
143 if (raw_y_pos) *raw_y_pos = jinfo.wYpos;
144 if (raw_z_pos) *raw_z_pos = jinfo.wZpos;
147 #elif defined(MRPT_OS_LINUX) && defined(HAVE_LINUX_INPUT_H)
149 if (m_joy_index == nJoy && m_joy_fd != -1)
156 if (m_joy_fd != -1) close(m_joy_fd);
160 open(
format(
"/dev/input/js%i", nJoy).c_str(), O_RDONLY)) < 0)
167 fcntl(m_joy_fd, F_SETFL, O_NONBLOCK);
172 while (read(m_joy_fd, &js,
sizeof(
struct js_event)) ==
173 sizeof(
struct js_event))
176 if (js.type & JS_EVENT_BUTTON)
179 if (m_joystate_btns.size() < (
size_t)js.number + 1)
180 m_joystate_btns.resize(js.number + 1);
181 m_joystate_btns[js.number] = js.value != 0;
185 if (js.type & JS_EVENT_AXIS)
188 if (m_joystate_axes.size() < (
size_t)js.number + 1)
189 m_joystate_axes.resize(js.number + 1);
190 m_joystate_axes[js.number] = js.value;
203 const size_t nAxis = m_joystate_axes.size();
208 if (raw_x_pos) *raw_x_pos = m_joystate_axes[0];
215 if (raw_y_pos) *raw_y_pos = m_joystate_axes[1];
222 if (raw_z_pos) *raw_z_pos = m_joystate_axes[2];
229 buttons = m_joystate_btns;
245 int x_min,
int x_max,
int y_min,
int y_max,
int z_min,
int z_max)