Joypad Scol plugin
Joypad.h
Go to the documentation of this file.
1
2#ifndef _JOYPAD_H_
3#define _JOYPAD_H_
4
5#include <scolPlugin.h>
6#include <scolMemoryHelper.hpp>
7
8#include <boost/thread/thread.hpp>
9#include <boost/bind/bind.hpp>
10
11#ifdef _WIN32
12
13#define DIRECTINPUT_VERSION 0x0800
14#define _CRT_SECURE_NO_DEPRECATE
15#ifndef _WIN32_DCOM
16#define _WIN32_DCOM
17#endif
18
19#include <XInput.h> // Include the XInput header
20#pragma comment(lib, "XInput.lib") // Link the XInput library
21
22#include <dinput.h>
23#include <dinputd.h>
24#include <set>
25
26using namespace std;
27
28//-----------------------------------------------------------------------------
29// Function-prototypes
30//-----------------------------------------------------------------------------
31BOOL CALLBACK EnumObjectsCallback( const DIDEVICEOBJECTINSTANCE* pdidoi, VOID* pContext );
32BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance, VOID* pContext );
33
34// Needed for std::set works with GUIDs.
35inline bool operator<( const GUID & lhs, const GUID & rhs)
36{
37 return (memcmp(&lhs, &rhs, sizeof(GUID)) < 0 ? true : false);
38}
39
40struct DI_ENUM_CONTEXT
41{
42 DIJOYCONFIG* pPreferredJoyCfg;
43 bool bPreferredJoyCfgValid;
44};
45
46#else
47 typedef struct DIJOYSTATE2 {
48 LONG lX; /* x-axis position */
49 LONG lY; /* y-axis position */
50 LONG lZ; /* z-axis position */
51 LONG lRx; /* x-axis rotation */
52 LONG lRy; /* y-axis rotation */
53 LONG lRz; /* z-axis rotation */
54 LONG rglSlider[2]; /* extra axes positions */
55 DWORD rgdwPOV[4]; /* POV directions */
56 int rgbButtons[16]; /* 16 buttons */
57 LONG lVX; /* x-axis velocity */
58 LONG lVY; /* y-axis velocity */
59 LONG lVZ; /* z-axis velocity */
60 LONG lVRx; /* x-axis angular velocity */
61 LONG lVRy; /* y-axis angular velocity */
62 LONG lVRz; /* z-axis angular velocity */
63 LONG rglVSlider[2]; /* extra axes velocities */
64 LONG lAX; /* x-axis acceleration */
65 LONG lAY; /* y-axis acceleration */
66 LONG lAZ; /* z-axis acceleration */
67 LONG lARx; /* x-axis angular acceleration */
68 LONG lARy; /* y-axis angular acceleration */
69 LONG lARz; /* z-axis angular acceleration */
70 LONG rglASlider[2]; /* extra axes accelerations */
71 LONG lFX; /* x-axis force */
72 LONG lFY; /* y-axis force */
73 LONG lFZ; /* z-axis force */
74 LONG lFRx; /* x-axis torque */
75 LONG lFRy; /* y-axis torque */
76 LONG lFRz; /* z-axis torque */
77 LONG rglFSlider[2]; /* extra axes forces */
79
80#endif //_WIN32
81
82#if !defined(_WIN32) && !defined(ANDROID)
83 #include <QtGamepad>
84#endif
85
86#if defined(ANDROID) || defined(_WIN32)
87class Joypad
88{
89#else
90class Joypad: public QObject
91{
92Q_OBJECT
93
94private slots:
95 void updateDoubleData(double value);
96 void updateBoolData(bool value);
97#endif
98
99public:
100 Joypad(int index = 0);
101 ~Joypad(void);
102
103 unsigned long mTick;
105#ifdef _WIN32
106 bool mInitialized;
107 GUID mInstanceId;
108 LPDIRECTINPUTDEVICE8 pJoystick;
109 DI_ENUM_CONTEXT pEnumContext;
110 IDirectInputEffect* mRumbleEffect;
111
112 HRESULT InitDirectInput();
113 VOID FreeDirectInput();
114 HRESULT UpdateInputState();
115
116 //XInput
117 int mControllerId;
118 XINPUT_VIBRATION mRumbleState;
119 BOOL mIsXInput;
120
121#elif defined(ANDROID)
122 static void updateInputEvent(AInputEvent* event);
123 void updateEvent(AInputEvent* event);
124#endif
125
126#ifndef ANDROID
127 void run();
128#endif
129
130 bool IsConnected();
131 bool IsRunning();
132 bool StartRumble(float intensity);
133 void StopRumble();
134
135private :
136#ifndef ANDROID
137 boost::thread mThread;
138 boost::mutex mMutex;
139#endif
140
141 bool mIsrunning;
142 bool mConnected;
143 DIJOYSTATE2 mDIJoyState; // DInput joystick state
144
145#ifdef ANDROID
146 DIJOYSTATE2* initData();
147#elif !defined(_WIN32)
148 void initData();
149#endif
150
151#ifdef ANDROID
152 bool mStickMode2;
153 void configAxis();
154#endif
155
156#if defined(ANDROID) || defined(LINUX) || defined(RPI) || defined(OSX) || defined(APPLE_IOS)
157 static std::list<Joypad*> lJoypad;
158 int mDeviceId;
159#endif
160
161#if !defined(_WIN32) && !defined(ANDROID)
162 QGamepad* mGamepad;
163#endif
164};
165
166#endif
struct DIJOYSTATE2 * LPDIJOYSTATE2
void run()
Definition Joypad.cpp:885
bool IsConnected()
Definition Joypad.cpp:1021
unsigned long mTick
Definition Joypad.h:103
bool StartRumble(float intensity)
Definition Joypad.cpp:1033
void StopRumble()
Definition Joypad.cpp:1039
bool IsRunning()
Definition Joypad.cpp:1027
int mIndex
Definition Joypad.h:104
~Joypad(void)
Definition Joypad.cpp:796
LONG rglFSlider[2]
Definition Joypad.h:77
LONG rglASlider[2]
Definition Joypad.h:70
LONG lRy
Definition Joypad.h:52
LONG lFRy
Definition Joypad.h:75
LONG lVRx
Definition Joypad.h:60
LONG lVX
Definition Joypad.h:57
LONG lVRy
Definition Joypad.h:61
LONG lRx
Definition Joypad.h:51
LONG lFRx
Definition Joypad.h:74
LONG lARy
Definition Joypad.h:68
LONG lAX
Definition Joypad.h:64
LONG lFRz
Definition Joypad.h:76
LONG lAY
Definition Joypad.h:65
LONG lZ
Definition Joypad.h:50
LONG lVZ
Definition Joypad.h:59
LONG lFY
Definition Joypad.h:72
LONG lFX
Definition Joypad.h:71
DWORD rgdwPOV[4]
Definition Joypad.h:55
LONG lARz
Definition Joypad.h:69
LONG lRz
Definition Joypad.h:53
LONG lX
Definition Joypad.h:48
LONG rglSlider[2]
Definition Joypad.h:54
LONG lY
Definition Joypad.h:49
LONG lAZ
Definition Joypad.h:66
LONG lVY
Definition Joypad.h:58
LONG lARx
Definition Joypad.h:67
int rgbButtons[16]
Definition Joypad.h:56
LONG lVRz
Definition Joypad.h:62
LONG rglVSlider[2]
Definition Joypad.h:63
LONG lFZ
Definition Joypad.h:73