EventHelpers.cpp 11.3 KB
Newer Older
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
189
190
191
192
193
194
195
196
197
198
199
200
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
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 "linux/EventHelpers.h"
#include "linux/LinuxPrereqs.h"
#include "linux/LinuxForceFeedback.h"
#include "OISException.h"
#include "OISJoyStick.h"

#include <linux/input.h>
#include <cstring>

//#define OIS_LINUX_JOY_DEBUG

#ifdef OIS_LINUX_JOY_DEBUG
# include <iostream>
#endif

using namespace std;
using namespace OIS;

class DeviceComponentInfo
{
public:
	vector<int> buttons, relAxes, absAxes, hats;
};

bool inline isBitSet(unsigned char bits[], unsigned int bit)
{
  return (bits[(bit)/(sizeof(unsigned char)*8)] >> ((bit)%(sizeof(unsigned char)*8))) & 1;
}

//-----------------------------------------------------------------------------//
DeviceComponentInfo getComponentInfo( int deviceID )
{
	unsigned char ev_bits[1 + EV_MAX/8/sizeof(unsigned char)];
	memset( ev_bits, 0, sizeof(ev_bits) );

	//Read "all" (hence 0) components of the device
#ifdef OIS_LINUX_JOY_DEBUG
	cout << "EventUtils::getComponentInfo(" << deviceID 
		 << ") : Reading device events features" << endl;
#endif
	if (ioctl(deviceID, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1)
		OIS_EXCEPT( E_General, "Could not read device events features");

	DeviceComponentInfo components;

	for (int i = 0; i < EV_MAX; i++)
	{
		if( isBitSet(ev_bits, i) )
		{
		    // Absolute axis.
		    if(i == EV_ABS)
			{
			    unsigned char abs_bits[1 + ABS_MAX/8/sizeof(unsigned char)];
			    memset( abs_bits, 0, sizeof(abs_bits) );

#ifdef OIS_LINUX_JOY_DEBUG
				cout << "EventUtils::getComponentInfo(" << deviceID 
					 << ") : Reading device absolute axis features" << endl;
#endif

				if (ioctl(deviceID, EVIOCGBIT(i, sizeof(abs_bits)), abs_bits) == -1)
				    OIS_EXCEPT( E_General, "Could not read device absolute axis features");

				for (int j = 0; j < ABS_MAX; j++)
				{
				    if( isBitSet(abs_bits, j) )
					{
						//input_absinfo abInfo;
						//ioctl( fd, EVIOCGABS(j), abInfo );
						if( j >= ABS_HAT0X && j <= ABS_HAT3Y )
						{
							components.hats.push_back(j);
						}
						else
						{
							components.absAxes.push_back(j);
							//input_absinfo absinfo;
							//ioctl(deviceID, EVIOCGABS(j), &absinfo);
							//We cannot actually change these values :|
							//absinfo.minimum = JoyStick::MIN_AXIS;
							//absinfo.maximum = JoyStick::MAX_AXIS;
							//ioctl(deviceID, EVIOCSABS(j), &absinfo);
						}
					}
				}
			}
			else if(i == EV_REL)
			{
			    unsigned char rel_bits[1 + REL_MAX/8/sizeof(unsigned char)];
				memset( rel_bits, 0, sizeof(rel_bits) );
				
#ifdef OIS_LINUX_JOY_DEBUG
				cout << "EventUtils::getComponentInfo(" << deviceID 
					 << ") : Reading device relative axis features" << endl;
#endif

				if (ioctl(deviceID, EVIOCGBIT(i, sizeof(rel_bits)), rel_bits) == -1)
				    OIS_EXCEPT( E_General, "Could not read device relative axis features");
				
				for (int j = 0; j < REL_MAX; j++)
				{
				    if( isBitSet(rel_bits, j) )
					{
					    components.relAxes.push_back(j);
					}
				}
			}
			else if(i == EV_KEY)
			{
			    unsigned char key_bits[1 + KEY_MAX/8/sizeof(unsigned char)];
				memset( key_bits, 0, sizeof(key_bits) );
				
#ifdef OIS_LINUX_JOY_DEBUG
				cout << "EventUtils::getComponentInfo(" << deviceID 
					 << ") : Reading device buttons features" << endl;
#endif

				if (ioctl(deviceID, EVIOCGBIT(i, sizeof(key_bits)), key_bits) == -1)
				    OIS_EXCEPT( E_General, "Could not read device buttons features");
				
				for (int j = 0; j < KEY_MAX; j++)
				{
				    if( isBitSet(key_bits, j) )
					{
					    components.buttons.push_back(j);
					}
				}
			}
		}
	}

	return components;
}

//-----------------------------------------------------------------------------//
bool EventUtils::isJoyStick( int deviceID, JoyStickInfo &js )
{
	if( deviceID == -1 ) 
		OIS_EXCEPT( E_General, "Error with File Descriptor" );

	DeviceComponentInfo info = getComponentInfo( deviceID );

	int buttons = 0;
	bool joyButtonFound = false;
	js.button_map.clear();

	#ifdef OIS_LINUX_JOY_DEBUG
	cout << endl << "Displaying ButtonMapping Status:" << endl;
	#endif
	for(vector<int>::iterator i = info.buttons.begin(), e = info.buttons.end(); i != e; ++i )
	{
		//Check to ensure we find at least one joy only button
		if( (*i >= BTN_JOYSTICK && *i < BTN_GAMEPAD)  
			|| (*i >= BTN_GAMEPAD && *i < BTN_DIGI)
			|| (*i >= BTN_WHEEL && *i < KEY_OK) )
			joyButtonFound = true;

		js.button_map[*i] = buttons++;

		#ifdef OIS_LINUX_JOY_DEBUG
		  cout << "Button Mapping ID (hex): " << hex << *i 
			   << " OIS Button Num: " << dec << buttons-1 << endl;
		#endif
	}
	#ifdef OIS_LINUX_JOY_DEBUG
	cout << endl;
	#endif

	//Joy Buttons found, so it must be a joystick or pad
	if( joyButtonFound )
	{
		js.joyFileD = deviceID;
		js.vendor = getName(deviceID);
		js.buttons = buttons;
		js.axes = info.relAxes.size() + info.absAxes.size();
		js.hats = info.hats.size();
		#ifdef OIS_LINUX_JOY_DEBUG
		  cout << endl << "Device name:" << js.vendor << endl;
		  cout << "Device unique Id:" << getUniqueId(deviceID) << endl;
		  cout << "Device physical location:" << getPhysicalLocation(deviceID) << endl;
		#endif

		//Map the Axes
		#ifdef OIS_LINUX_JOY_DEBUG
		  cout << endl << "Displaying AxisMapping Status:" << endl;
		#endif
		int axes = 0;
		for(vector<int>::iterator i = info.absAxes.begin(), e = info.absAxes.end(); i != e; ++i )
		{
			js.axis_map[*i] = axes;

#ifdef OIS_LINUX_JOY_DEBUG
			cout << "EventUtils::isJoyStick(" << deviceID 
					  << ") : Reading device absolute axis #" << *i << " features" << endl;
#endif

			input_absinfo absinfo;
			if (ioctl(deviceID, EVIOCGABS(*i), &absinfo) == -1)
				OIS_EXCEPT( E_General, "Could not read device absolute axis features");
			js.axis_range[axes] = Range(absinfo.minimum, absinfo.maximum);

			#ifdef OIS_LINUX_JOY_DEBUG
			  cout << "Axis Mapping ID (hex): " << hex << *i 
				   << " OIS Axis Num: " << dec << axes << endl;
			#endif

			++axes;
		}
	}

	return joyButtonFound;
}

//-----------------------------------------------------------------------------//
string EventUtils::getName( int deviceID )
{
#ifdef OIS_LINUX_JOY_DEBUG
	cout << "EventUtils::getName(" << deviceID 
		 << ") : Reading device name" << endl;
#endif

	char name[OIS_DEVICE_NAME];
	if (ioctl(deviceID, EVIOCGNAME(OIS_DEVICE_NAME), name) == -1)
		OIS_EXCEPT( E_General, "Could not read device name");
	return string(name);
}

//-----------------------------------------------------------------------------//
string EventUtils::getUniqueId( int deviceID )
{
#ifdef OIS_LINUX_JOY_DEBUG
	cout << "EventUtils::getUniqueId(" << deviceID 
		 << ") : Reading device unique Id" << endl;
#endif

#define OIS_DEVICE_UNIQUE_ID 128
	char uId[OIS_DEVICE_UNIQUE_ID];
	if (ioctl(deviceID, EVIOCGUNIQ(OIS_DEVICE_UNIQUE_ID), uId) == -1)
		OIS_EXCEPT( E_General, "Could not read device unique Id");
	return string(uId);
}

//-----------------------------------------------------------------------------//
string EventUtils::getPhysicalLocation( int deviceID )
{
#ifdef OIS_LINUX_JOY_DEBUG
	cout << "EventUtils::getPhysicalLocation(" << deviceID 
		 << ") : Reading device physical location" << endl;
#endif

#define OIS_DEVICE_PHYSICAL_LOCATION 128
	char physLoc[OIS_DEVICE_PHYSICAL_LOCATION];
	if (ioctl(deviceID, EVIOCGPHYS(OIS_DEVICE_PHYSICAL_LOCATION), physLoc) == -1)
		OIS_EXCEPT( E_General, "Could not read device physical location");
	return string(physLoc);
}

//-----------------------------------------------------------------------------//
void EventUtils::enumerateForceFeedback( int deviceID, LinuxForceFeedback** ff )
{
	//Linux Event to OIS Event Mappings
	map<int, Effect::EType> typeMap;
	typeMap[FF_CONSTANT] = Effect::Constant;
	typeMap[FF_RAMP]     = Effect::Ramp;
	typeMap[FF_SPRING]   = Effect::Spring;
	typeMap[FF_FRICTION] = Effect::Friction;
	typeMap[FF_SQUARE]   = Effect::Square;
	typeMap[FF_TRIANGLE] = Effect::Triangle;
	typeMap[FF_SINE]     = Effect::Sine;
	typeMap[FF_SAW_UP]   = Effect::SawToothUp;
	typeMap[FF_SAW_DOWN] = Effect::SawToothDown;
	typeMap[FF_DAMPER]   = Effect::Damper;
	typeMap[FF_INERTIA]  = Effect::Inertia;
	typeMap[FF_CUSTOM]   = Effect::Custom;

	map<int, Effect::EForce> forceMap;
	forceMap[FF_CONSTANT] = Effect::ConstantForce;
	forceMap[FF_RAMP]     = Effect::RampForce;
	forceMap[FF_SPRING]   = Effect::ConditionalForce;
	forceMap[FF_FRICTION] = Effect::ConditionalForce;
	forceMap[FF_SQUARE]   = Effect::PeriodicForce;
	forceMap[FF_TRIANGLE] = Effect::PeriodicForce;
	forceMap[FF_SINE]     = Effect::PeriodicForce;
	forceMap[FF_SAW_UP]   = Effect::PeriodicForce;
	forceMap[FF_SAW_DOWN] = Effect::PeriodicForce;
	forceMap[FF_DAMPER]   = Effect::ConditionalForce;
	forceMap[FF_INERTIA]  = Effect::ConditionalForce;
	forceMap[FF_CUSTOM]   = Effect::CustomForce;

	//Remove any previously existing memory and create fresh
	removeForceFeedback( ff );
	*ff = new LinuxForceFeedback(deviceID);

	//Read overall force feedback features
	unsigned char ff_bits[1 + FF_MAX/8/sizeof(unsigned char)];
	memset(ff_bits, 0, sizeof(ff_bits));

#ifdef OIS_LINUX_JOY_DEBUG
	cout << "EventUtils::enumerateForceFeedback(" << deviceID 
		 << ") : Reading device force feedback features" << endl;
#endif

	if (ioctl(deviceID, EVIOCGBIT(EV_FF, sizeof(ff_bits)), ff_bits) == -1)
		OIS_EXCEPT( E_General, "Could not read device force feedback features");


    #ifdef OIS_LINUX_JOY_DEBUG
	cout << "FF bits: " << hex;
	for (int i = 0; i < sizeof(ff_bits); i++)
		cout << (int)ff_bits[i];
	cout << endl << dec;
    #endif

	//FF Axes
	//if( isBitSet(ff_bits, ABS_X) ) //X Axis
	//if( isBitSet(ff_bits, ABS_Y) ) //Y Axis
	//if( isBitSet(ff_bits, ABS_WHEEL) ) //Wheel

	//FF Effects
	for( int effect = FF_EFFECT_MIN; effect <= FF_WAVEFORM_MAX; effect++ )
	{
		// The RUMBLE force type is ignored, as periodic force one is more powerfull.
		// The PERIODIC force type is processed later, for each associated periodic effect type.
		if (effect == FF_RUMBLE || effect == FF_PERIODIC)
			continue;

		if(isBitSet(ff_bits, effect))
		{
			#ifdef OIS_LINUX_JOY_DEBUG
		    cout << "  Effect Type: " << Effect::getEffectTypeName(typeMap[effect]) << endl;
			#endif

			(*ff)->_addEffectTypes( forceMap[effect], typeMap[effect] );
		}
	}

	//FF device properties
	if (isBitSet(ff_bits, FF_GAIN))
		(*ff)->_setGainSupport(true);
		
	if (isBitSet(ff_bits, FF_AUTOCENTER))
		(*ff)->_setAutoCenterSupport(true);

	//Check to see if any effects were added, else destroy the pointer
	const ForceFeedback::SupportedEffectList &list = (*ff)->getSupportedEffects();
	if( list.size() == 0 )
		removeForceFeedback( ff );
}

//-----------------------------------------------------------------------------//
void EventUtils::removeForceFeedback( LinuxForceFeedback** ff )
{
	delete *ff;
	*ff = 0;
}