Joystick.cpp 4.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
/*
 * See header for details and license.
 */

#include "linuxjoystick/Joystick.h"

#ifdef ACGL_OWN_LINUX_JOYSTICK

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

Joystick::Joystick () :
  m_init (false),
  m_axes (0),
  m_buttons (0),
  m_run (true)
{
}

Joystick::~Joystick ()
{
  this->close ();
}

bool Joystick::open (const string &device)
{
  m_fd = ::open (device.c_str(), O_RDONLY);
  if (m_fd == -1)
  {
    cerr << "Error opening joystick device!" << endl;

    return false;
  }
  else
  {
    char buttons;
    char axes;
    char name[128];

    // get number of buttons
    ioctl (m_fd, JSIOCGBUTTONS, &buttons);
    m_buttons = buttons;

    // get number of axes
    ioctl (m_fd, JSIOCGAXES, &axes);
    m_axes = axes;

    // get device name
    if (ioctl(m_fd, JSIOCGNAME (sizeof(name)), name) < 0)
    {
      m_name = "Unknown";
    }
    else
    {
      m_name = name;
    }

    /* TODO: support those if needed
     * #define JSIOCGVERSION   // get driver version
     * #define JSIOCSCORR      // set correction values
     * #define JSIOCGCORR      // get correction values
     */

    this->start();
    //thread = Glib::Thread::create (sigc::mem_fun (*this, &Joystick::loop), false);
    m_run = true;
  }

  return true;
}

bool Joystick::close ()
{
  // end thread
  m_run = false;

  // reset some values
  m_init = false;
  m_axes = 0;
  m_buttons = 0;

  return !::close (m_fd);
}

void Joystick::run ()
{
    this->setTerminationEnabled(true);
  // wait for all synthetic event until the first real event comes
  // then we've all available axis and buttons.

  while (m_run && isRunning())
  {
    EventJoystick eventJoy;

    ssize_t n = read (m_fd, &joy_event, sizeof(struct js_event));
    if (!n)
      std::cerr << "No joy_event could be read\n";

    eventJoy.time = joy_event.time;
    eventJoy.value = joy_event.value;

    switch (joy_event.type)
    {
    case JS_EVENT_BUTTON:
      if (!m_init) m_init = true;
      eventJoy.number = joy_event.number;
      eventJoy.synthetic = false;
    {
        QMutexLocker locker(&mBtnMutex);
        mEventsBtn.push(eventJoy);
        (void)locker;
    }
      //signalButton.emit (eventJoy);
      break;

    case JS_EVENT_AXIS:
      if (!m_init) m_init = true;
      eventJoy.number = joy_event.number;
      eventJoy.synthetic = false;

    {
        QMutexLocker locker(&mAxisMutex);
        mEventsAxis.push(eventJoy);
        (void)locker;
    }
      //signalAxis.emit (eventJoy);
      break;

    case JS_EVENT_BUTTON | JS_EVENT_INIT:
      if (m_init) // skip the synthetic events on driver start
      {
        eventJoy.number = joy_event.number & ~JS_EVENT_INIT;
        eventJoy.synthetic = true;

        {
            QMutexLocker locker(&mBtnMutex);
            mEventsBtn.push(eventJoy);
            (void)locker;
        }
          //signalButton.emit (eventJoy);
      }
      break;

    case JS_EVENT_AXIS | JS_EVENT_INIT:
      if (m_init) // skip the synthetic events on driver start
      {
        eventJoy.number = joy_event.number & ~JS_EVENT_INIT;
        eventJoy.synthetic = true;
        {
            QMutexLocker locker(&mAxisMutex);
            mEventsAxis.push(eventJoy);
            (void)locker;
        }
          //signalAxis.emit (eventJoy);
      }
      break;

    default: // we should never reach this point
      printf ("unknown event: %x\n", joy_event.type);
    }
  }
}

int Joystick::getNumberOfButtons ()
{
  return m_buttons;
}

int Joystick::getNumberOfAxes ()
{
  return m_axes;
}

const string &Joystick::getIdentifier ()
{
    return m_name;
}

bool Joystick::pollEventButton(EventJoystick &_event)
{
188 189 190
    if ( mEventsBtn.size() == 0 ) {
        return false;
    }
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

    QMutexLocker locker(&mBtnMutex);
    (void)locker;

    if ( mEventsBtn.size() > 0 )
    {
        EventJoystick ev = mEventsBtn.front();
        mEventsBtn.pop();
        _event = ev;
        return true;
    }

    return false;
}
bool Joystick::pollEventAxis(EventJoystick &_event)
{
    if ( mEventsAxis.size() == 0 ) return false;

    QMutexLocker locker(&mAxisMutex);
    (void)locker;

    if ( mEventsAxis.size() > 0 )
    {
        EventJoystick ev = mEventsAxis.front();
        mEventsAxis.pop();

        _event = ev;
        return true;
    }

    return false;
}

#endif // ACGL_OWN_LINUX_JOYSTICK