1 #ifndef __joystick_input_h__
2 #define __joystick_input_h__
4 #include "../DasherCore/DasherInput.h"
5 #include "../DasherCore/DasherInterfaceBase.h"
6 #include "../DasherCore/DasherTypes.h"
12 #include <linux/joystick.h>
14 using namespace Dasher
;
16 class CDasherJoystickInput
: public CDasherInput
{
18 CDasherJoystickInput(CEventHandler
* pEventHandler
, CSettingsStore
* pSettingsStore
, CDasherInterfaceBase
*pInterface
)
19 : CDasherInput(pEventHandler
, pSettingsStore
, 16, 0, "Joystick Input") {
21 m_pInterface
= pInterface
;
25 // Fill pCoordinates with iN coordinate values, return 0 if the
26 // values were in screen coordinates or 1 if the values were in
27 // Dasher coordinates.
29 virtual int GetCoordinates(int iN
, myint
* pCoordinates
) {
32 pCoordinates
[0] = m_iX
;
33 pCoordinates
[1] = m_iY
;
38 // Get the number of co-ordinates that this device supplies
40 virtual int GetCoordinateCount() {
44 void SetCoordinates(myint _iX
, myint _iY
) {
50 // TODO: Error handling, check versioning of kernel joystick driver
60 void UpdateCoordinates() {
63 while(read (iFd
, &sEvent
, sizeof(struct js_event
)) > 0) {
65 if(sEvent
.type
& JS_EVENT_AXIS
) {
66 switch(sEvent
.number
) {
67 case 0: // Stick 1, X axis
68 m_iX
= 2048 - (sEvent
.value
* 2048) / 32767;
70 case 1: // Stick 1, Y axis
71 m_iY
= (sEvent
.value
* 2048) / 32767 + 2048;
78 else if(sEvent
.type
& JS_EVENT_BUTTON
) {
79 // For now map everything to button 100
82 m_pInterface
->KeyDown(get_time(), 100);
84 m_pInterface
->KeyUp(get_time(), 100);
87 /* EAGAIN is returned when the queue is empty */
88 if (errno
!= EAGAIN
) {
94 const char *szDeviceName
=GetStringParameter(SP_JOYSTICK_DEVICE
).c_str();
96 iFd
= open(szDeviceName
, O_RDONLY
| O_NONBLOCK
);
106 /// File descriptor for the joystick device
109 CDasherInterfaceBase
*m_pInterface
;
113 class CDasherJoystickInputDiscrete
: public CDasherInput
{
115 CDasherJoystickInputDiscrete(CEventHandler
* pEventHandler
, CSettingsStore
* pSettingsStore
, CDasherInterfaceBase
*pInterface
)
116 : CDasherInput(pEventHandler
, pSettingsStore
, 17, 0, "Joystick Input (Discrete)") {
118 m_pInterface
= pInterface
;
122 // Fill pCoordinates with iN coordinate values, return 0 if the
123 // values were in screen coordinates or 1 if the values were in
124 // Dasher coordinates.
126 virtual int GetCoordinates(int iN
, myint
* pCoordinates
) {
129 pCoordinates
[0] = m_iX
;
130 pCoordinates
[1] = m_iY
;
135 // Get the number of co-ordinates that this device supplies
137 virtual int GetCoordinateCount() {
141 void SetCoordinates(myint _iX
, myint _iY
) {
147 // TODO: Error handling, check versioning of kernel joystick driver
157 void UpdateCoordinates() {
160 while(read (iFd
, &sEvent
, sizeof(struct js_event
)) > 0) {
162 if(sEvent
.type
& JS_EVENT_AXIS
) {
163 switch(sEvent
.number
) {
164 case 0: // Stick 1, X axis
167 case 1: // Stick 1, Y axis
175 else if(sEvent
.type
& JS_EVENT_BUTTON
) {
176 // For now map everything to button 100
178 if(sEvent
.value
== 1)
179 m_pInterface
->KeyDown(get_time(), 100);
181 m_pInterface
->KeyUp(get_time(), 100);
184 /* EAGAIN is returned when the queue is empty */
185 if (errno
!= EAGAIN
) {
189 int iNewZone
= GetZone();
191 if(iNewZone
!= iZone
) {
193 m_pInterface
->KeyUp(get_time(), iZone
);
195 m_pInterface
->KeyDown(get_time(), iNewZone
);
202 const int iBound
= 32767 / 2;
204 if((m_iX
< iBound
) && (m_iX
> -iBound
) && (m_iY
< iBound
) && (m_iY
> -iBound
))
206 else if(m_iX
> m_iY
) {
221 const char *szDeviceName
=GetStringParameter(SP_JOYSTICK_DEVICE
).c_str();
223 iFd
= open(szDeviceName
, O_RDONLY
| O_NONBLOCK
);
233 /// File descriptor for the joystick device
236 CDasherInterfaceBase
*m_pInterface
;
241 class CDasher1DJoystickInput
: public CDasherInput
{
243 CDasher1DJoystickInput(CEventHandler
* pEventHandler
, CSettingsStore
* pSettingsStore
, CDasherInterfaceBase
*pInterface
)
244 : CDasherInput(pEventHandler
, pSettingsStore
, 18, 0, "Joystick Input (1D)") {
246 m_pInterface
= pInterface
;
250 // Fill pCoordinates with iN coordinate values, return 0 if the
251 // values were in screen coordinates or 1 if the values were in
252 // Dasher coordinates.
254 virtual int GetCoordinates(int iN
, myint
* pCoordinates
) {
258 pCoordinates
[1] = m_iY
;
263 // Get the number of co-ordinates that this device supplies
265 virtual int GetCoordinateCount() {
269 void SetCoordinates(myint _iX
, myint _iY
) {
275 // TODO: Error handling, check versioning of kernel joystick driver
285 void UpdateCoordinates() {
288 while(read (iFd
, &sEvent
, sizeof(struct js_event
)) > 0) {
290 if(sEvent
.type
& JS_EVENT_AXIS
) {
291 switch(sEvent
.number
) {
292 case 0: // Stick 1, X axis
293 m_iX
= 2048 - (sEvent
.value
* 2048) / 32767;
295 case 1: // Stick 1, Y axis
296 m_iY
= (sEvent
.value
* 2048) / 32767 + 2048;
312 if(iNewZone
!= iZone
) {
313 if((iNewZone
== 0) && (iNewZone
!= -1))
314 m_pInterface
->KeyUp(get_time(), 100);
316 m_pInterface
->KeyDown(get_time(), 100);
321 else if(sEvent
.type
& JS_EVENT_BUTTON
) {
322 // For now map everything to button 100
324 if(sEvent
.value
== 1)
325 m_pInterface
->KeyDown(get_time(), 100);
327 m_pInterface
->KeyUp(get_time(), 100);
330 /* EAGAIN is returned when the queue is empty */
331 if (errno
!= EAGAIN
) {
337 const char *szDeviceName
=GetStringParameter(SP_JOYSTICK_DEVICE
).c_str();
339 iFd
= open(szDeviceName
, O_RDONLY
| O_NONBLOCK
);
349 /// File descriptor for the joystick device
354 CDasherInterfaceBase
*m_pInterface
;