]> git.saurik.com Git - wxWidgets.git/blobdiff - src/unix/joystick.cpp
[ 1635287 ] wrong cursor when dragging an image
[wxWidgets.git] / src / unix / joystick.cpp
index 49a4cad688e7349d9cfaf6381fba283e21859f08..b96c009826e6503e0a03e26a397f18836c405b66 100644 (file)
@@ -21,6 +21,8 @@
     #include "wx/window.h"
 #endif //WX_PRECOMP
 
     #include "wx/window.h"
 #endif //WX_PRECOMP
 
+#include "wx/thread.h"
+
 #include <linux/joystick.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <linux/joystick.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
 
 #include <fcntl.h>
 #include <unistd.h>
 
+#ifdef HAVE_SYS_SELECT_H
+#   include <sys/select.h>
+#endif
+
 #include "wx/unix/private.h"
 
 enum {
 #include "wx/unix/private.h"
 
 enum {
@@ -40,7 +46,9 @@ enum {
     wxJS_AXIS_V,
 
     wxJS_AXIS_MAX = 32767,
     wxJS_AXIS_V,
 
     wxJS_AXIS_MAX = 32767,
-    wxJS_AXIS_MIN = -32767
+    wxJS_AXIS_MIN = -32767,
+    wxJS_MAX_AXES = 15,
+    wxJS_MAX_BUTTONS = sizeof(int) * 8
 };
 
 
 };
 
 
@@ -58,13 +66,15 @@ public:
     void* Entry();
 
 private:
     void* Entry();
 
 private:
+    void      SendEvent(wxEventType type, long ts, int change = 0);
     int       m_device;
     int       m_joystick;
     wxPoint   m_lastposition;
     int       m_device;
     int       m_joystick;
     wxPoint   m_lastposition;
-    int       m_axe[15];
+    int       m_axe[wxJS_MAX_AXES];
     int       m_buttons;
     wxWindow* m_catchwin;
     int       m_polling;
     int       m_buttons;
     wxWindow* m_catchwin;
     int       m_polling;
+    int       m_threshold;
 
     friend class wxJoystick;
 };
 
     friend class wxJoystick;
 };
@@ -76,12 +86,24 @@ wxJoystickThread::wxJoystickThread(int device, int joystick)
       m_lastposition(wxDefaultPosition),
       m_buttons(0),
       m_catchwin(NULL),
       m_lastposition(wxDefaultPosition),
       m_buttons(0),
       m_catchwin(NULL),
-      m_polling(0)
+      m_polling(0),
+      m_threshold(0)
 {
 {
-    for (int i=0; i<15; i++)
-        m_axe[i] = 0;
+    memset(m_axe, 0, sizeof(m_axe));
 }
 
 }
 
+void wxJoystickThread::SendEvent(wxEventType type, long ts, int change)
+{
+    wxJoystickEvent jwx_event(type, m_buttons, m_joystick, change);
+
+    jwx_event.SetTimestamp(ts);
+    jwx_event.SetPosition(m_lastposition);
+    jwx_event.SetZPosition(m_axe[wxJS_AXIS_Z]);
+    jwx_event.SetEventObject(m_catchwin);
+
+    if (m_catchwin)
+        m_catchwin->AddPendingEvent(jwx_event);
+}
 
 void* wxJoystickThread::Entry()
 {
 
 void* wxJoystickThread::Entry()
 {
@@ -112,9 +134,10 @@ void* wxJoystickThread::Entry()
             //printf("time: %d\t value: %d\t type: %d\t number: %d\n",
             //       j_evt.time, j_evt.value, j_evt.type, j_evt.number);
 
             //printf("time: %d\t value: %d\t type: %d\t number: %d\n",
             //       j_evt.time, j_evt.value, j_evt.type, j_evt.number);
 
-            wxJoystickEvent jwx_event;
-
-            if (j_evt.type & JS_EVENT_AXIS)
+            if ((j_evt.type & JS_EVENT_AXIS) && (j_evt.number < wxJS_MAX_AXES))
+            {
+                if (   (m_axe[j_evt.number] + m_threshold < j_evt.value)
+                    || (m_axe[j_evt.number] - m_threshold > j_evt.value) )
             {
                 m_axe[j_evt.number] = j_evt.value;
 
             {
                 m_axe[j_evt.number] = j_evt.value;
 
@@ -122,47 +145,36 @@ void* wxJoystickThread::Entry()
                 {
                     case wxJS_AXIS_X:
                         m_lastposition.x = j_evt.value;
                 {
                     case wxJS_AXIS_X:
                         m_lastposition.x = j_evt.value;
-                        jwx_event.SetEventType(wxEVT_JOY_MOVE);
+                        SendEvent(wxEVT_JOY_MOVE, j_evt.time);
                         break;
                     case wxJS_AXIS_Y:
                         m_lastposition.y = j_evt.value;
                         break;
                     case wxJS_AXIS_Y:
                         m_lastposition.y = j_evt.value;
-                        jwx_event.SetEventType(wxEVT_JOY_MOVE);
+                        SendEvent(wxEVT_JOY_MOVE, j_evt.time);
                         break;
                     case wxJS_AXIS_Z:
                         break;
                     case wxJS_AXIS_Z:
-                        jwx_event.SetEventType(wxEVT_JOY_ZMOVE);
+                        SendEvent(wxEVT_JOY_ZMOVE, j_evt.time);
                         break;
                     default:
                         break;
                     default:
-                        jwx_event.SetEventType(wxEVT_JOY_MOVE);
+                        SendEvent(wxEVT_JOY_MOVE, j_evt.time);
                         // TODO: There should be a way to indicate that the event
                         //       is for some other axes.
                         break;
                 }
             }
                         // TODO: There should be a way to indicate that the event
                         //       is for some other axes.
                         break;
                 }
             }
+            }
 
 
-            if (j_evt.type & JS_EVENT_BUTTON)
+            if ( (j_evt.type & JS_EVENT_BUTTON) && (j_evt.number < wxJS_MAX_BUTTONS) )
             {
                 if (j_evt.value)
                 {
                     m_buttons |= (1 << j_evt.number);
             {
                 if (j_evt.value)
                 {
                     m_buttons |= (1 << j_evt.number);
-                    jwx_event.SetEventType(wxEVT_JOY_BUTTON_DOWN);
+                    SendEvent(wxEVT_JOY_BUTTON_DOWN, j_evt.time, j_evt.number);
                 }
                 else
                 {
                     m_buttons &= ~(1 << j_evt.number);
                 }
                 else
                 {
                     m_buttons &= ~(1 << j_evt.number);
-                    jwx_event.SetEventType(wxEVT_JOY_BUTTON_UP);
+                    SendEvent(wxEVT_JOY_BUTTON_UP, j_evt.time, j_evt.number);
                 }
                 }
-
-                jwx_event.SetButtonChange(j_evt.number);
-
-                jwx_event.SetTimestamp(j_evt.time);
-                jwx_event.SetJoystick(m_joystick);
-                jwx_event.SetButtonState(m_buttons);
-                jwx_event.SetPosition(m_lastposition);
-                jwx_event.SetZPosition(m_axe[3]);
-                jwx_event.SetEventObject(m_catchwin);
-
-                if (m_catchwin)
-                    m_catchwin->AddPendingEvent(jwx_event);
             }
         }
     }
             }
         }
     }
@@ -221,6 +233,13 @@ wxPoint wxJoystick::GetPosition() const
     return pos;
 }
 
     return pos;
 }
 
+int wxJoystick::GetPosition(unsigned axis) const
+{
+    if (m_thread && (axis < wxJS_MAX_AXES))
+        return m_thread->m_axe[axis];
+    return 0;
+}
+
 int wxJoystick::GetZPosition() const
 {
     if (m_thread)
 int wxJoystick::GetZPosition() const
 {
     if (m_thread)
@@ -235,6 +254,13 @@ int wxJoystick::GetButtonState() const
     return 0;
 }
 
     return 0;
 }
 
+bool wxJoystick::GetButtonState(unsigned id) const
+{
+    if (m_thread && (id < wxJS_MAX_BUTTONS))
+        return (m_thread->m_buttons & (1 << id)) != 0;
+    return false;
+}
+
 int wxJoystick::GetPOVPosition() const
 {
     return -1;
 int wxJoystick::GetPOVPosition() const
 {
     return -1;
@@ -268,11 +294,15 @@ int wxJoystick::GetVPosition() const
 
 int wxJoystick::GetMovementThreshold() const
 {
 
 int wxJoystick::GetMovementThreshold() const
 {
+    if (m_thread)
+        return m_thread->m_threshold;
     return 0;
 }
 
 void wxJoystick::SetMovementThreshold(int threshold)
 {
     return 0;
 }
 
 void wxJoystick::SetMovementThreshold(int threshold)
 {
+    if (m_thread)
+        m_thread->m_threshold = threshold;
 }
 
 ////////////////////////////////////////////////////////////////////////////
 }
 
 ////////////////////////////////////////////////////////////////////////////
@@ -366,6 +396,9 @@ int wxJoystick::GetNumberButtons() const
     if (m_device != -1)
         ioctl(m_device, JSIOCGBUTTONS, &nb);
 
     if (m_device != -1)
         ioctl(m_device, JSIOCGBUTTONS, &nb);
 
+    if ((int)nb > wxJS_MAX_BUTTONS)
+        nb = wxJS_MAX_BUTTONS;
+
     return nb;
 }
 
     return nb;
 }
 
@@ -376,17 +409,20 @@ int wxJoystick::GetNumberAxes() const
     if (m_device != -1)
         ioctl(m_device, JSIOCGAXES, &nb);
 
     if (m_device != -1)
         ioctl(m_device, JSIOCGAXES, &nb);
 
+    if ((int)nb > wxJS_MAX_AXES)
+        nb = wxJS_MAX_AXES;
+
     return nb;
 }
 
 int wxJoystick::GetMaxButtons() const
 {
     return nb;
 }
 
 int wxJoystick::GetMaxButtons() const
 {
-    return 15; // internal
+    return wxJS_MAX_BUTTONS; // internal
 }
 
 int wxJoystick::GetMaxAxes() const
 {
 }
 
 int wxJoystick::GetMaxAxes() const
 {
-    return 15; // internal
+    return wxJS_MAX_AXES; // internal
 }
 
 int wxJoystick::GetPollingMin() const
 }
 
 int wxJoystick::GetPollingMin() const