MacJoyStick.cpp 10 KB
Newer Older
1
2
/*
 The zlib/libpng License
Ben Hymers's avatar
Ben Hymers committed
3

4
 Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)
Ben Hymers's avatar
Ben Hymers committed
5

6
7
 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.
Ben Hymers's avatar
Ben Hymers committed
8

9
10
11
 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:
Ben Hymers's avatar
Ben Hymers committed
12

13
14
15
16
 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.
Ben Hymers's avatar
Ben Hymers committed
17

18
19
 2. Altered source versions must be plainly marked as such, and must not be
 misrepresented as being the original software.
Ben Hymers's avatar
Ben Hymers committed
20

21
22
23
24
25
26
27
28
29
30
31
32
33
34
 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;

//--------------------------------------------------------------------------------------------------//
Ben Hymers's avatar
Ben Hymers committed
35
MacJoyStick::MacJoyStick(const std::string &vendor, bool buffered, HidInfo* info, InputManager* creator, int devID) :
36
37
JoyStick(vendor, buffered, devID, creator), mInfo(info)
{
Ben Hymers's avatar
Ben Hymers committed
38

39
40
41
42
43
44
}

//--------------------------------------------------------------------------------------------------//
MacJoyStick::~MacJoyStick()
{
	//TODO: check if the queue has been started first?
Ben Hymers's avatar
Ben Hymers committed
45
46
47
48
49
	//(*mQueue)->stop(mQueue);
	(*mQueue)->dispose(mQueue);
	(*mQueue)->Release(mQueue);


50
51
	//TODO: check if the interface has been opened first?
	(*mInfo->interface)->close(mInfo->interface);
Ben Hymers's avatar
Ben Hymers committed
52
	(*mInfo->interface)->Release(mInfo->interface);
53
54
55
56
57
58
59
}

//--------------------------------------------------------------------------------------------------//
void MacJoyStick::_initialize()
{
	assert(mInfo && "Given HidInfo invalid");
	assert(mInfo->interface && "Joystick interface invalid");
Ben Hymers's avatar
Ben Hymers committed
60

61
62
63
	//TODO: Is this necessary?
	//Clear old state
	mState.mAxes.clear();
Ben Hymers's avatar
Ben Hymers committed
64

65
66
	if ((*mInfo->interface)->open(mInfo->interface, 0) != KERN_SUCCESS)
		OIS_EXCEPT(E_General, "MacJoyStick::_initialize() >> Could not initialize joy device!");
Ben Hymers's avatar
Ben Hymers committed
67

68
	mState.clear();
Ben Hymers's avatar
Ben Hymers committed
69

70
	_enumerateCookies();
Ben Hymers's avatar
Ben Hymers committed
71

72
73
	mState.mButtons.resize(mInfo->numButtons);
	mState.mAxes.resize(mInfo->numAxes);
Ben Hymers's avatar
Ben Hymers committed
74

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
	mQueue = _createQueue();
}

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;
};

//--------------------------------------------------------------------------------------------------//
void MacJoyStick::capture()
{
	assert(mQueue && "Queue must be initialized before calling MacJoyStick::capture()");
Ben Hymers's avatar
Ben Hymers committed
94
95
96
97
98

	AbsoluteTime zeroTime = {0,0};

	IOHIDEventStruct event;
	IOReturn result = (*mQueue)->getNextEvent(mQueue, &event, zeroTime, 0);
99
100
101
102
103
104
105
106
107
	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);
Ben Hymers's avatar
Ben Hymers committed
108

109
110
111
112
113
114
115
116
117
118
119
120
121
122
				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:
				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);
Ben Hymers's avatar
Ben Hymers committed
123

124
125
126
127
				//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);
Ben Hymers's avatar
Ben Hymers committed
128

129
130
131
				if(mBuffered && mListener) mListener->axisMoved(JoyStickEvent(this, mState), axis);
				break;
		}
Ben Hymers's avatar
Ben Hymers committed
132

133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
		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
Ben Hymers's avatar
Ben Hymers committed
148

149
150
151
152
153
154
155
156
157
158
159
	//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");
Ben Hymers's avatar
Ben Hymers committed
160
161
162
163
164

	CFTypeRef                               object;
	long                                    number;
	IOHIDElementCookie                      cookie;
	long                                    usage;
165
166
167
168
	long                                    usagePage;
	int										min;
	int										max;

Ben Hymers's avatar
Ben Hymers committed
169
170
171
172
173
174
175
176
177
	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);

178
	if (success == kIOReturnSuccess)
Ben Hymers's avatar
Ben Hymers committed
179
	{
180
		const CFIndex numOfElements = CFArrayGetCount(elements);
Ben Hymers's avatar
Ben Hymers committed
181
182
		for (CFIndex i = 0; i < numOfElements; ++i)
		{
183
			element = static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(elements, i));
Ben Hymers's avatar
Ben Hymers committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

			//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
			//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;
Ben Hymers's avatar
Ben Hymers committed
214

215
216
217
218
219
220
221
222
			//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;
Ben Hymers's avatar
Ben Hymers committed
223
224
225
226
227
228
229
230
231
232
233
234
235
			max = number;

			//Get usage page
			object = CFDictionaryGetValue(element,
										  CFSTR(kIOHIDElementUsagePageKey));

			if (object == 0 || CFGetTypeID(object) != CFNumberGetTypeID())
				continue;

			if (!CFNumberGetValue((CFNumberRef) object, kCFNumberLongType,
								  &number))
				continue;

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
			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:
						mCookies.axisCookies.insert(std::make_pair(cookie, AxisInfo(min, max)));
						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;
Ben Hymers's avatar
Ben Hymers committed
263
			}
264
		}
Ben Hymers's avatar
Ben Hymers committed
265

266
267
268
269
		mInfo->numButtons = mCookies.buttonCookies.size();
		mInfo->numAxes = mCookies.axisCookies.size();

	}
Ben Hymers's avatar
Ben Hymers committed
270
271
272
273
274
	else
	{
		OIS_EXCEPT(E_General, "JoyStick elements could not be copied: copyMatchingElements failed with error: " + success);
	}

275
276
277
278
}

//--------------------------------------------------------------------------------------------------//
IOHIDQueueInterface** MacJoyStick::_createQueue(unsigned int depth)
Ben Hymers's avatar
Ben Hymers committed
279
{
280
281
	assert(mInfo && "Given HidInfo invalid");
	assert(mInfo->interface && "Joystick interface invalid");
Ben Hymers's avatar
Ben Hymers committed
282
283
284
285
286
287
288
289

	IOHIDQueueInterface** queue = (*mInfo->interface)->allocQueue(mInfo->interface);

	if (queue)
	{
		//create the queue
		IOReturn result = (*queue)->create(queue, 0, depth);

290
		if(result == kIOReturnSuccess)
Ben Hymers's avatar
Ben Hymers committed
291
		{
292
293
294
295
296
297
			//add elements to the queue
			std::map<IOHIDElementCookie, AxisInfo>::iterator axisIt = mCookies.axisCookies.begin();
			for(; axisIt != mCookies.axisCookies.end(); ++axisIt)
			{
				result = (*queue)->addElement(queue, axisIt->first, 0);
			}
Ben Hymers's avatar
Ben Hymers committed
298

299
300
301
302
303
304
			std::vector<IOHIDElementCookie>::iterator buttonIt = mCookies.buttonCookies.begin();
			for(; buttonIt != mCookies.buttonCookies.end(); ++buttonIt)
			{
				result = (*queue)->addElement(queue, (*buttonIt), 0);
			}

Ben Hymers's avatar
Ben Hymers committed
305
306
			//start data delivery to queue
			result = (*queue)->start(queue);
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
			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.");
	}
}