12 #include <mrpt/config.h>    21 #if !defined(__GNUC__)    22 #pragma comment(lib, "WINMM.LIB")    26 #if defined(MRPT_OS_LINUX) || defined(__APPLE__)    29 #include <sys/ioctl.h>    31 #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>    58 #if defined(MRPT_OS_LINUX) && defined(HAVE_LINUX_INPUT_H)    76     return joyGetNumDevs();
    77 #elif defined(MRPT_OS_LINUX) && defined(HAVE_LINUX_INPUT_H)    85             (joy_fd = open(
format(
"/dev/input/js%i", nJoys).c_str(), O_RDONLY)))
    90     } 
while (joy_fd != -1);
   107     int nJoy, 
float& x, 
float& y, 
float& z, std::vector<bool>& buttons,
   108     int* raw_x_pos, 
int* raw_y_pos, 
int* raw_z_pos)
   114     int ID = JOYSTICKID1 + nJoy;
   117     if (JOYERR_NOERROR != joyGetPos(ID, &jinfo)) 
return false;  
   130     buttons[0] = 0 != (jinfo.wButtons & JOY_BUTTON1);
   131     buttons[1] = 0 != (jinfo.wButtons & JOY_BUTTON2);
   132     buttons[2] = 0 != (jinfo.wButtons & JOY_BUTTON3);
   133     buttons[3] = 0 != (jinfo.wButtons & JOY_BUTTON4);
   135     if (raw_x_pos) *raw_x_pos = jinfo.wXpos;
   136     if (raw_y_pos) *raw_y_pos = jinfo.wYpos;
   137     if (raw_z_pos) *raw_z_pos = jinfo.wZpos;
   140 #elif defined(MRPT_OS_LINUX) && defined(HAVE_LINUX_INPUT_H)   142     if (m_joy_index == nJoy && m_joy_fd != -1)
   149         if (m_joy_fd != -1) close(m_joy_fd);
   153                  open(
format(
"/dev/input/js%i", nJoy).c_str(), O_RDONLY)) < 0)
   160         fcntl(m_joy_fd, F_SETFL, O_NONBLOCK);
   165     while (read(m_joy_fd, &js, 
sizeof(
struct js_event)) ==
   166            sizeof(
struct js_event))
   169         if (js.type & JS_EVENT_BUTTON)
   172             if (m_joystate_btns.size() < (size_t)js.number + 1)
   173                 m_joystate_btns.resize(js.number + 1);
   174             m_joystate_btns[js.number] = js.value != 0;
   178         if (js.type & JS_EVENT_AXIS)
   181             if (m_joystate_axes.size() < (size_t)js.number + 1)
   182                 m_joystate_axes.resize(js.number + 1);
   183             m_joystate_axes[js.number] = js.value;
   196     const size_t nAxis = m_joystate_axes.size();
   201         if (raw_x_pos) *raw_x_pos = m_joystate_axes[0];
   208         if (raw_y_pos) *raw_y_pos = m_joystate_axes[1];
   215         if (raw_z_pos) *raw_z_pos = m_joystate_axes[2];
   222     buttons = m_joystate_btns;
   238     int x_min, 
int x_max, 
int y_min, 
int y_max, 
int z_min, 
int z_max)
 
std::string std::string format(std::string_view fmt, ARGS &&... args)
 
Contains classes for various device interfaces. 
 
int m_x_min
The axis limits: 
 
void setLimits(int x_min=0, int x_max=0xFFFF, int y_min=0, int y_max=0xFFFF, int z_min=0, int z_max=0xFFFF)
Set the axis limit values, for computing a [-1,1] position index easily (Only required to calibrate a...
 
static int getJoysticksCount()
Returns the number of Joysticks in the computer. 
 
bool getJoystickPosition(int nJoy, float &x, float &y, float &z, std::vector< bool > &buttons, int *raw_x_pos=nullptr, int *raw_y_pos=nullptr, int *raw_z_pos=nullptr)
Gets joystick information. 
 
virtual ~CJoystick()
Destructor.