tagging release
[dasher.git] / Src / Gtk2 / joystick_input.h
blobf95a7c7e050c70a32095115de74e9ac08609cb4f
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"
7 #include "Timer.h"
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <iostream>
12 #include <linux/joystick.h>
14 using namespace Dasher;
16 class CDasherJoystickInput : public CDasherInput {
17 public:
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) {
30 UpdateCoordinates();
32 pCoordinates[0] = m_iX;
33 pCoordinates[1] = m_iY;
35 return 1;
38 // Get the number of co-ordinates that this device supplies
40 virtual int GetCoordinateCount() {
41 return 2;
44 void SetCoordinates(myint _iX, myint _iY) {
45 m_iX = _iX;
46 m_iY = _iY;
49 void Activate() {
50 // TODO: Error handling, check versioning of kernel joystick driver
51 OpenDevice();
52 UpdateCoordinates();
55 void Deactivate() {
56 CloseDevice();
59 private:
60 void UpdateCoordinates() {
61 js_event sEvent;
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;
69 break;
70 case 1: // Stick 1, Y axis
71 m_iY = (sEvent.value * 2048) / 32767 + 2048;
72 break;
73 default:
74 // Ignore other axes
75 break;
78 else if(sEvent.type & JS_EVENT_BUTTON) {
79 // For now map everything to button 100
81 if(sEvent.value == 1)
82 m_pInterface->KeyDown(get_time(), 100);
83 else
84 m_pInterface->KeyUp(get_time(), 100);
87 /* EAGAIN is returned when the queue is empty */
88 if (errno != EAGAIN) {
89 /* error */
93 void OpenDevice() {
94 const char *szDeviceName=GetStringParameter(SP_JOYSTICK_DEVICE).c_str();
96 iFd = open(szDeviceName, O_RDONLY | O_NONBLOCK);
99 void CloseDevice() {
100 close(iFd);
103 myint m_iX;
104 myint m_iY;
106 /// File descriptor for the joystick device
107 int iFd;
109 CDasherInterfaceBase *m_pInterface;
113 class CDasherJoystickInputDiscrete : public CDasherInput {
114 public:
115 CDasherJoystickInputDiscrete(CEventHandler * pEventHandler, CSettingsStore * pSettingsStore, CDasherInterfaceBase *pInterface)
116 : CDasherInput(pEventHandler, pSettingsStore, 17, 0, "Joystick Input (Discrete)") {
118 m_pInterface = pInterface;
119 iZone = -1;
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) {
127 UpdateCoordinates();
129 pCoordinates[0] = m_iX;
130 pCoordinates[1] = m_iY;
132 return 1;
135 // Get the number of co-ordinates that this device supplies
137 virtual int GetCoordinateCount() {
138 return 2;
141 void SetCoordinates(myint _iX, myint _iY) {
142 m_iX = _iX;
143 m_iY = _iY;
146 void Activate() {
147 // TODO: Error handling, check versioning of kernel joystick driver
148 OpenDevice();
149 UpdateCoordinates();
152 void Deactivate() {
153 CloseDevice();
156 private:
157 void UpdateCoordinates() {
158 js_event sEvent;
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
165 m_iX = sEvent.value;
166 break;
167 case 1: // Stick 1, Y axis
168 m_iY = sEvent.value;
169 break;
170 default:
171 // Ignore other axes
172 break;
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);
180 else
181 m_pInterface->KeyUp(get_time(), 100);
184 /* EAGAIN is returned when the queue is empty */
185 if (errno != EAGAIN) {
186 /* error */
189 int iNewZone = GetZone();
191 if(iNewZone != iZone) {
192 if(iZone > 0)
193 m_pInterface->KeyUp(get_time(), iZone);
194 if(iNewZone > 0)
195 m_pInterface->KeyDown(get_time(), iNewZone);
198 iZone = iNewZone;
201 int GetZone() {
202 const int iBound = 32767 / 2;
204 if((m_iX < iBound) && (m_iX > -iBound) && (m_iY < iBound) && (m_iY > -iBound))
205 return 0;
206 else if(m_iX > m_iY) {
207 if(m_iX > - m_iY)
208 return 3;
209 else
210 return 2;
212 else {
213 if(m_iX > - m_iY)
214 return 4;
215 else
216 return 1;
220 void OpenDevice() {
221 const char *szDeviceName=GetStringParameter(SP_JOYSTICK_DEVICE).c_str();
223 iFd = open(szDeviceName, O_RDONLY | O_NONBLOCK);
226 void CloseDevice() {
227 close(iFd);
230 myint m_iX;
231 myint m_iY;
233 /// File descriptor for the joystick device
234 int iFd;
236 CDasherInterfaceBase *m_pInterface;
237 int iZone;
241 class CDasher1DJoystickInput : public CDasherInput {
242 public:
243 CDasher1DJoystickInput(CEventHandler * pEventHandler, CSettingsStore * pSettingsStore, CDasherInterfaceBase *pInterface)
244 : CDasherInput(pEventHandler, pSettingsStore, 18, 0, "Joystick Input (1D)") {
246 m_pInterface = pInterface;
247 iZone = -1;
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) {
255 UpdateCoordinates();
257 pCoordinates[0] = 0;
258 pCoordinates[1] = m_iY;
260 return 1;
263 // Get the number of co-ordinates that this device supplies
265 virtual int GetCoordinateCount() {
266 return 2;
269 void SetCoordinates(myint _iX, myint _iY) {
270 m_iX = _iX;
271 m_iY = _iY;
274 void Activate() {
275 // TODO: Error handling, check versioning of kernel joystick driver
276 OpenDevice();
277 UpdateCoordinates();
280 void Deactivate() {
281 CloseDevice();
284 private:
285 void UpdateCoordinates() {
286 js_event sEvent;
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;
294 break;
295 case 1: // Stick 1, Y axis
296 m_iY = (sEvent.value * 2048) / 32767 + 2048;
297 break;
298 default:
299 // Ignore other axes
300 break;
303 int iNewZone;
305 if(m_iX > 3072)
306 iNewZone = 1;
307 else if(m_iX < 1024)
308 iNewZone = 2;
309 else
310 iNewZone = 0;
312 if(iNewZone != iZone) {
313 if((iNewZone == 0) && (iNewZone != -1))
314 m_pInterface->KeyUp(get_time(), 100);
315 else
316 m_pInterface->KeyDown(get_time(), 100);
319 iZone = iNewZone;
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);
326 else
327 m_pInterface->KeyUp(get_time(), 100);
330 /* EAGAIN is returned when the queue is empty */
331 if (errno != EAGAIN) {
332 /* error */
336 void OpenDevice() {
337 const char *szDeviceName=GetStringParameter(SP_JOYSTICK_DEVICE).c_str();
339 iFd = open(szDeviceName, O_RDONLY | O_NONBLOCK);
342 void CloseDevice() {
343 close(iFd);
346 myint m_iX;
347 myint m_iY;
349 /// File descriptor for the joystick device
350 int iFd;
352 int iZone;
354 CDasherInterfaceBase *m_pInterface;
358 #endif