MacJoyStick.cpp 10.5 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
/*
 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 "mac/MacJoyStick.h"
#include "mac/MacHIDManager.h"
#include "mac/MacInputManager.h"
#include "OISEvents.h"
#include "OISException.h"

#include <cassert>

using namespace OIS;

//--------------------------------------------------------------------------------------------------//
35
36
MacJoyStick::MacJoyStick(const std::string &vendor, bool buffered, HidInfo* info, InputManager* creator, int devID) : 
JoyStick(vendor, buffered, devID, creator), mInfo(info)
Phillip Castaneda's avatar
Phillip Castaneda committed
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
{
	
}

//--------------------------------------------------------------------------------------------------//
MacJoyStick::~MacJoyStick()
{
	//TODO: check if the queue has been started first?
	//(*mQueue)->stop(mQueue); 
	(*mQueue)->dispose(mQueue); 
	(*mQueue)->Release(mQueue); 
	
	
	//TODO: check if the interface has been opened first?
	(*mInfo->interface)->close(mInfo->interface);
	(*mInfo->interface)->Release(mInfo->interface); 
}

//--------------------------------------------------------------------------------------------------//
void MacJoyStick::_initialize()
{
	assert(mInfo && "Given HidInfo invalid");
	assert(mInfo->interface && "Joystick interface invalid");
	
	//TODO: Is this necessary?
	//Clear old state
	mState.mAxes.clear();
	
	if ((*mInfo->interface)->open(mInfo->interface, 0) != KERN_SUCCESS)
		OIS_EXCEPT(E_General, "MacJoyStick::_initialize() >> Could not initialize joy device!");
	
	mState.clear();
	
	_enumerateCookies();
	
	mState.mButtons.resize(mInfo->numButtons);
	mState.mAxes.resize(mInfo->numAxes);
	
	mQueue = _createQueue();
}

78
79
80
81
82
83
84
85
86
87
88
89
class FindAxisCookie : public std::unary_function<std::pair<IOHIDElementCookie, AxisInfo>&, bool>
{
public:
	FindAxisCookie(IOHIDElementCookie cookie) : m_Cookie(cookie) {}
	bool operator()(const std::pair<IOHIDElementCookie, AxisInfo>& pair) const
	{
		return pair.first == m_Cookie;
	}
private:
	IOHIDElementCookie m_Cookie;
};

Phillip Castaneda's avatar
Phillip Castaneda committed
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
//--------------------------------------------------------------------------------------------------//
void MacJoyStick::capture()
{
	assert(mQueue && "Queue must be initialized before calling MacJoyStick::capture()");
	
	AbsoluteTime zeroTime = {0,0}; 
	
	IOHIDEventStruct event; 
	IOReturn result = (*mQueue)->getNextEvent(mQueue, &event, zeroTime, 0); 
	while(result == kIOReturnSuccess)
	{
		switch(event.type)
		{
			case kIOHIDElementTypeInput_Button:
			{
				std::vector<IOHIDElementCookie>::iterator buttonIt = std::find(mCookies.buttonCookies.begin(), mCookies.buttonCookies.end(), event.elementCookie);
				int button = std::distance(mCookies.buttonCookies.begin(), buttonIt);
				mState.mButtons[button] = (event.value == 1);
				
				if(mBuffered && mListener)
				{
					if(event.value == 0)
						mListener->buttonPressed(JoyStickEvent(this, mState), button);
					else if(event.value == 1)
						mListener->buttonReleased(JoyStickEvent(this, mState), button);
				}
				break;
			}
			case kIOHIDElementTypeInput_Misc:
				//TODO: It's an axis! - kind of - for gamepads - or should this be a pov?
			case kIOHIDElementTypeInput_Axis:
121
122
123
124
125
126
127
128
129
				std::map<IOHIDElementCookie, AxisInfo>::iterator axisIt = std::find_if(mCookies.axisCookies.begin(), mCookies.axisCookies.end(), FindAxisCookie(event.elementCookie));
				int axis = std::distance(mCookies.axisCookies.begin(), axisIt);
				
				//Copied from LinuxJoyStickEvents.cpp, line 149
				const AxisInfo& axisInfo = axisIt->second;
				float proportion = (float) (event.value - axisInfo.max) / (float) (axisInfo.min - axisInfo.max);
				mState.mAxes[axis].abs = -JoyStick::MIN_AXIS - (JoyStick::MAX_AXIS * 2 * proportion);
				
				if(mBuffered && mListener) mListener->axisMoved(JoyStickEvent(this, mState), axis);
Phillip Castaneda's avatar
Phillip Castaneda committed
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
				break;
		}
		
		result = (*mQueue)->getNextEvent(mQueue, &event, zeroTime, 0);
	}
}

//--------------------------------------------------------------------------------------------------//
void MacJoyStick::setBuffered(bool buffered)
{
	mBuffered = buffered;
}

//--------------------------------------------------------------------------------------------------//
Interface* MacJoyStick::queryInterface(Interface::IType type)
{
	//Thought about using covariant return type here.. however,
	//some devices may allow LED light changing, or other interface stuff
	
	//f( ff_device && type == Interface::ForceFeedback )
	//return ff_device;
	//else
	return 0;
}

//--------------------------------------------------------------------------------------------------//
void MacJoyStick::_enumerateCookies()
{
	assert(mInfo && "Given HidInfo invalid");
	assert(mInfo->interface && "Joystick interface invalid");
	
	CFTypeRef                               object; 
	long                                    number; 
	IOHIDElementCookie                      cookie; 
	long                                    usage; 
165
166
167
	long                                    usagePage;
	int										min;
	int										max;
Phillip Castaneda's avatar
Phillip Castaneda committed
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

	CFDictionaryRef                         element; 
	
	// Copy all elements, since we're grabbing most of the elements 
	// for this device anyway, and thus, it's faster to iterate them 
	// ourselves. When grabbing only one or two elements, a matching 
	// dictionary should be passed in here instead of NULL. 
	CFArrayRef elements; 
	IOReturn success = reinterpret_cast<IOHIDDeviceInterface122*>(*mInfo->interface)->copyMatchingElements(mInfo->interface, NULL, &elements); 
	
	if (success == kIOReturnSuccess)
	{ 
		const CFIndex numOfElements = CFArrayGetCount(elements);
		for (CFIndex i = 0; i < numOfElements; ++i) 
		{ 
			element = static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(elements, i));
			
			//Get cookie 
			object = (CFDictionaryGetValue(element, 
										   CFSTR(kIOHIDElementCookieKey))); 
			if (object == 0 || CFGetTypeID(object) != CFNumberGetTypeID()) 
				continue; 
			if(!CFNumberGetValue((CFNumberRef) object, kCFNumberLongType, 
								 &number)) 
				continue; 
			cookie = (IOHIDElementCookie) number; 
			
			//Get usage 
			object = CFDictionaryGetValue(element, 
										  CFSTR(kIOHIDElementUsageKey)); 
			if (object == 0 || CFGetTypeID(object) != CFNumberGetTypeID()) 
				continue; 
			if (!CFNumberGetValue((CFNumberRef) object, kCFNumberLongType, 
								  &number)) 
				continue; 
			usage = number; 
			
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
			//Get min
			object = CFDictionaryGetValue(element,
										  CFSTR(kIOHIDElementMinKey)); // kIOHIDElementMinKey or kIOHIDElementScaledMinKey?, no idea ...
			if (object == 0 || CFGetTypeID(object) != CFNumberGetTypeID())
				continue;
			if (!CFNumberGetValue((CFNumberRef) object, kCFNumberIntType,
								  &number))
				continue;
			min = number;
			
			//Get max
			object = CFDictionaryGetValue(element,
										  CFSTR(kIOHIDElementMaxKey)); // kIOHIDElementMaxKey or kIOHIDElementScaledMaxKey?, no idea ...
			if (object == 0 || CFGetTypeID(object) != CFNumberGetTypeID())
				continue;
			if (!CFNumberGetValue((CFNumberRef) object, kCFNumberIntType,
								  &number))
				continue;
			max = number;			
Phillip Castaneda's avatar
Phillip Castaneda committed
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
			
			//Get usage page 
			object = CFDictionaryGetValue(element, 
										  CFSTR(kIOHIDElementUsagePageKey)); 
			
			if (object == 0 || CFGetTypeID(object) != CFNumberGetTypeID()) 
				continue; 
			
			if (!CFNumberGetValue((CFNumberRef) object, kCFNumberLongType, 
								  &number)) 
				continue; 
			
			usagePage = number;
			switch(usagePage)
			{
				case kHIDPage_GenericDesktop:
					switch(usage)
				{
					case kHIDUsage_GD_Pointer:
						break;
					case kHIDUsage_GD_X:
					case kHIDUsage_GD_Y:
					case kHIDUsage_GD_Z:
					case kHIDUsage_GD_Rx:
					case kHIDUsage_GD_Ry:
					case kHIDUsage_GD_Rz:
250
						mCookies.axisCookies.insert(std::make_pair(cookie, AxisInfo(min, max)));
Phillip Castaneda's avatar
Phillip Castaneda committed
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
						break;
					case kHIDUsage_GD_Slider:
					case kHIDUsage_GD_Dial:
					case kHIDUsage_GD_Wheel:
						break;
					case kHIDUsage_GD_Hatswitch:
						break;
				}
					break;
				case kHIDPage_Button:
					mCookies.buttonCookies.push_back(cookie);
					break;
			}		
		}
		
		mInfo->numButtons = mCookies.buttonCookies.size();
		mInfo->numAxes = mCookies.axisCookies.size();

	} 
	else 
	{ 
		OIS_EXCEPT(E_General, "JoyStick elements could not be copied: copyMatchingElements failed with error: " + success); 
	}
	
}

//--------------------------------------------------------------------------------------------------//
IOHIDQueueInterface** MacJoyStick::_createQueue(unsigned int depth)
{	
	assert(mInfo && "Given HidInfo invalid");
	assert(mInfo->interface && "Joystick interface invalid");
	
	IOHIDQueueInterface** queue = (*mInfo->interface)->allocQueue(mInfo->interface); 
	
	if (queue) 
	{		
		//create the queue 
		IOReturn result = (*queue)->create(queue, 0, depth); 
		
		if(result == kIOReturnSuccess)
		{		
			//add elements to the queue
293
294
			std::map<IOHIDElementCookie, AxisInfo>::iterator axisIt = mCookies.axisCookies.begin();
			for(; axisIt != mCookies.axisCookies.end(); ++axisIt)
Phillip Castaneda's avatar
Phillip Castaneda committed
295
			{
296
				result = (*queue)->addElement(queue, axisIt->first, 0);
Phillip Castaneda's avatar
Phillip Castaneda committed
297
298
			}
			
299
300
			std::vector<IOHIDElementCookie>::iterator buttonIt = mCookies.buttonCookies.begin();
			for(; buttonIt != mCookies.buttonCookies.end(); ++buttonIt)
Phillip Castaneda's avatar
Phillip Castaneda committed
301
			{
302
303
				result = (*queue)->addElement(queue, (*buttonIt), 0);
			}
Phillip Castaneda's avatar
Phillip Castaneda committed
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325

			//start data delivery to queue 
			result = (*queue)->start(queue); 
			if(result == kIOReturnSuccess)
			{
				return queue;
			}
			else
			{
				OIS_EXCEPT(E_General, "Queue could not be started.");
			}
		}
		else
		{
			OIS_EXCEPT(E_General, "Queue could not be created.");
		}
	}
	else
	{
		OIS_EXCEPT(E_General, "Queue allocation failed.");
	}
}