LinuxJoyStickEvents.cpp 8.82 KB
Newer Older
Phillip Castaneda's avatar
Phillip Castaneda committed
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
/*
The zlib/libpng License

Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)

This software is provided 'as-is', without any express or implied warranty. In no event will
the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial
applications, and to alter it and redistribute it freely, subject to the following
restrictions:

    1. The origin of this software must not be misrepresented; you must not claim that
		you wrote the original software. If you use this software in a product,
		an acknowledgment in the product documentation would be appreciated but is
		not required.

    2. Altered source versions must be plainly marked as such, and must not be
		misrepresented as being the original software.

    3. This notice may not be removed or altered from any source distribution.
*/
#include "OISConfig.h"

#include "linux/LinuxJoyStickEvents.h"
#include "linux/LinuxInputManager.h"
#include "linux/LinuxForceFeedback.h"
#include "linux/EventHelpers.h"

#include "OISEvents.h"
#include "OISException.h"

#include <fcntl.h>        //Needed to Open a file descriptor
#include <cassert>	
#include <linux/input.h>


#include <sstream>
# include <iostream>
using namespace std;

using namespace OIS;

//#define OIS_LINUX_JOY_DEBUG

//-------------------------------------------------------------------//
LinuxJoyStick::LinuxJoyStick(InputManager* creator, bool buffered, const JoyStickInfo& js)
	: JoyStick(js.vendor, buffered, js.devId, creator)
{
	mJoyStick = js.joyFileD;

	mState.mAxes.clear();
	mState.mAxes.resize(js.axes);
	mState.mButtons.clear();
	mState.mButtons.resize(js.buttons);

	mPOVs = js.hats;

	mButtonMap = js.button_map;
	mAxisMap = js.axis_map;
	mRanges = js.axis_range;

	ff_effect = 0;
}

//-------------------------------------------------------------------//
LinuxJoyStick::~LinuxJoyStick()
{
	EventUtils::removeForceFeedback( &ff_effect );
}

//-------------------------------------------------------------------//
void LinuxJoyStick::_initialize()
{
	//Clear old joy state
	mState.mAxes.resize(mAxisMap.size());
	mState.clear();

	//This will create and new us a force feedback structure if it exists
	EventUtils::enumerateForceFeedback( mJoyStick, &ff_effect );

	if( mJoyStick == -1 )
		OIS_EXCEPT(E_InputDeviceNonExistant, "LinuxJoyStick::_initialize() >> JoyStick Not Found!");
}

//-------------------------------------------------------------------//
void LinuxJoyStick::capture()
{
	static const short POV_MASK[8] = {0,0,1,1,2,2,3,3};

	//Used to determine if an axis has been changed and needs an event
	bool axisMoved[32] = {false, false, false, false, false, false, false, false, false, false, false, false, false,
						  false, false, false, false, false, false, false, false, false, false, false, false, false,
						  false, false, false, false, false, false};

	//We are in non blocking mode - we just read once, and try to fill up buffer
	input_event js[JOY_BUFFERSIZE];
98
	while(true)
Phillip Castaneda's avatar
Phillip Castaneda committed
99
	{
100
101
		int ret = read(mJoyStick, &js, sizeof(struct input_event) * JOY_BUFFERSIZE);
        if( ret < 0 )
Phillip Castaneda's avatar
Phillip Castaneda committed
102
103
			break;

104
105
106
		//Determine how many whole events re read up
		ret /= sizeof(struct input_event);
		for(int i = 0; i < ret; ++i)
Phillip Castaneda's avatar
Phillip Castaneda committed
107
		{
108
109
110
			switch(js[i].type)
			{
			case EV_KEY:  //Button
Phillip Castaneda's avatar
Phillip Castaneda committed
111
			{
112
				int button = mButtonMap[js[i].code];
Phillip Castaneda's avatar
Phillip Castaneda committed
113

114
115
116
				#ifdef OIS_LINUX_JOY_DEBUG
				  cout << "\nButton Code: " << js[i].code << ", OIS Value: " << button << endl;
				#endif
Phillip Castaneda's avatar
Phillip Castaneda committed
117

118
119
120
121
122
123
				//Check to see whether push or released event...
				if(js[i].value)
				{
					mState.mButtons[button] = true;
					if( mBuffered && mListener )
						if(!mListener->buttonPressed(JoyStickEvent(this,mState), button)) return;
Phillip Castaneda's avatar
Phillip Castaneda committed
124
125
				}
				else
126
127
128
129
				{
					mState.mButtons[button] = false;
					if( mBuffered && mListener )
						if(!mListener->buttonReleased(JoyStickEvent(this,mState), button)) return;
Phillip Castaneda's avatar
Phillip Castaneda committed
130
				}
131
				break;
Phillip Castaneda's avatar
Phillip Castaneda committed
132
133
			}

134
135
136
137
			case EV_ABS:  //Absolute Axis
			{
				//A Stick (BrakeDefine is the highest possible Axis)
				if( js[i].code <= ABS_BRAKE )
Phillip Castaneda's avatar
Phillip Castaneda committed
138
				{
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
					int axis = mAxisMap[js[i].code];
					assert( axis < 32 && "Too many axes (Max supported is 32). Report this to OIS forums!" );

					axisMoved[axis] = true;

					//check for rescaling:
					if( mRanges[axis].min == JoyStick::MIN_AXIS && mRanges[axis].max != JoyStick::MAX_AXIS )
					{	//Scale is perfect
						mState.mAxes[axis].abs = js[i].value;
					}
					else
					{	//Rescale
						float proportion = (float)(js[i].value-mRanges[axis].max)/(float)(mRanges[axis].min-mRanges[axis].max);
						mState.mAxes[axis].abs = (int)(32767.0f - (65535.0f * proportion));
					}
Phillip Castaneda's avatar
Phillip Castaneda committed
154
				}
155
				else if( js[i].code <= ABS_HAT3Y ) //A POV - Max four POVs allowed
Phillip Castaneda's avatar
Phillip Castaneda committed
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
188
					//Normalise the POV to between 0-7
					//Even is X Axis, Odd is Y Axis
					unsigned char LinuxPovNumber = js[i].code - 16;
					short OIS_POVIndex = POV_MASK[LinuxPovNumber];

					//Handle X Axis first (Even) (left right)
					if((LinuxPovNumber & 0x0001) == 0)
					{
						//Why do this? Because, we use a bit field, and when this axis is east,
						//it can't possibly be west too. So clear out the two X axes, then refil
						//it in with the new direction bit.
						//Clear the East/West Bit Flags first
						mState.mPOV[OIS_POVIndex].direction &= 0x11110011;
						if( js[i].value == -1 )	//Left
							mState.mPOV[OIS_POVIndex].direction |= Pov::West;
						else if( js[i].value == 1 ) //Right
							mState.mPOV[OIS_POVIndex].direction |= Pov::East;
					}
					//Handle Y Axis (Odd) (up down)
					else
					{
						//Clear the North/South Bit Flags first
						mState.mPOV[OIS_POVIndex].direction &= 0x11111100;
						if( js[i].value == -1 )	//Up
							mState.mPOV[OIS_POVIndex].direction |= Pov::North;
						else if( js[i].value == 1 ) //Down
							mState.mPOV[OIS_POVIndex].direction |= Pov::South;
					}

					if( mBuffered && mListener )
						if( mListener->povMoved( JoyStickEvent(this,mState), OIS_POVIndex) == false )
							return;
Phillip Castaneda's avatar
Phillip Castaneda committed
189
				}
190
				break;
Phillip Castaneda's avatar
Phillip Castaneda committed
191
192
			}

193
194
195
196
197
198
199
200
			
			case EV_REL: //Relative Axes (Do any joystick actually have a relative axis?)
	#ifdef OIS_LINUX_JOY_DEBUG
				cout << "\nWarning: Relatives axes not supported yet" << endl;
	#endif
				break;
			default: break;
			}
Phillip Castaneda's avatar
Phillip Castaneda committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
		}
	}

	//All axes and POVs are combined into one movement per pair per captured frame
	if( mBuffered && mListener )
	{
		for( int i = 0; i < 32; ++i )
			if( axisMoved[i] )
				if( mListener->axisMoved( JoyStickEvent(this,mState), i) == false )
					return;
	}
}

//-------------------------------------------------------------------//
void LinuxJoyStick::setBuffered(bool buffered)
{
	if( buffered != mBuffered )
	{
		mBuffered = buffered;
		_initialize();
	}
}

//-------------------------------------------------------------------//
JoyStickInfo LinuxJoyStick::_getJoyInfo()
{
	JoyStickInfo js;

	js.devId = mDevID;
	js.joyFileD = mJoyStick;
	js.vendor = mVendor;
	js.axes = (int)mState.mAxes.size();
	js.buttons = (int)mState.mButtons.size();
	js.hats = mPOVs;
	js.button_map = mButtonMap;
	js.axis_map = mAxisMap;
	js.axis_range = mRanges;

	return js;
}

//-------------------------------------------------------------------//
JoyStickInfoList LinuxJoyStick::_scanJoys()
{
	JoyStickInfoList joys;

	//Search through all of the event devices.. and identify which ones are joysticks
	//xxx move this to InputManager, as it can also scan all other events
	for(int i = 0; i < 64; ++i )
	{
		stringstream s;
		s << "/dev/input/event" << i;
		int fd = open( s.str().c_str(), O_RDWR |O_NONBLOCK );
		if(fd == -1)
			continue;

        #ifdef OIS_LINUX_JOY_DEBUG
		  cout << "Opening " << s.str() << "..." << endl;
        #endif
		try
		{
			JoyStickInfo js;
			if( EventUtils::isJoyStick(fd, js) )
			{
				joys.push_back(js);
                #ifdef OIS_LINUX_JOY_DEBUG
                  cout << "=> Joystick added to list." << endl;
                #endif
			}
			else
			{
                #ifdef OIS_LINUX_JOY_DEBUG
                  cout << "=> Not a joystick." << endl;
                #endif
				close(fd);
			}
		}
		catch(...)
		{
            #ifdef OIS_LINUX_JOY_DEBUG
              cout << "Exception caught!!" << endl;
            #endif
			close(fd);
		}
	}

	return joys;
}

//-------------------------------------------------------------------//
void LinuxJoyStick::_clearJoys(JoyStickInfoList &joys)
{
	for(JoyStickInfoList::iterator i = joys.begin(); i != joys.end(); ++i)
		close(i->joyFileD);
	joys.clear();
}

//-------------------------------------------------------------------//
Interface* LinuxJoyStick::queryInterface(Interface::IType type)
{
	if( ff_effect && type == Interface::ForceFeedback )
		return ff_effect;

	return 0;
}