Uglify. Ignore 80 column rule without going too far past.
Use inline functions instead of the two macros. No other nonspacing changes.
This commit is contained in:
@@ -19,23 +19,28 @@
|
||||
using namespace std;
|
||||
using __gnu_cxx::hash_map;
|
||||
|
||||
static void AddElement(const void *value, void *context);
|
||||
static void AddElement( const void *value, void *context );
|
||||
|
||||
// Simple helper, still need to release it
|
||||
static inline CFNumberRef CFInt(int n)
|
||||
static inline CFNumberRef CFInt( int n )
|
||||
{
|
||||
return CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &n);
|
||||
return CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &n );
|
||||
}
|
||||
|
||||
static inline void PrintIOErr(IOReturn err, const char *s)
|
||||
static inline void PrintIOErr( IOReturn err, const char *s )
|
||||
{
|
||||
LOG->Warn("%s - %s(%x,%d)", s, mach_error_string(err), err, err & 0xFFFFFF);
|
||||
LOG->Warn( "%s - %s(%x,%d)", s, mach_error_string(err), err, err & 0xFFFFFF );
|
||||
}
|
||||
|
||||
// These gets absurdly long
|
||||
#define DictValue(d, k) CFDictionaryGetValue(d, CFSTR(k))
|
||||
#define IntValue(o, n) \
|
||||
CFNumberGetValue(CFNumberRef(o), kCFNumberIntType, n)
|
||||
static inline CFTypeRef DictValue( CFDictionaryRef d, const char *k )
|
||||
{
|
||||
return CFDictionaryGetValue( d, k );
|
||||
}
|
||||
|
||||
static inline int IntValue( const void *o, int *n )
|
||||
{
|
||||
return CFNumberGetValue( CFNumberRef(o), kCFNumberIntType, n );
|
||||
}
|
||||
|
||||
/* This is just awful, these aren't objects, treating them as such leads
|
||||
* to: (*object)->function(object [, argument]...)
|
||||
@@ -55,12 +60,12 @@ struct Joystick
|
||||
Joystick();
|
||||
};
|
||||
|
||||
Joystick::Joystick() : id(DEVICE_NONE),
|
||||
x_axis(JOYSTICK_BUTTON_INVALID),
|
||||
y_axis(JOYSTICK_BUTTON_INVALID),
|
||||
z_axis(JOYSTICK_BUTTON_INVALID),
|
||||
x_min(0), y_min(0), z_min(0),
|
||||
x_max(0), y_max(0), z_max(0)
|
||||
Joystick::Joystick() : id( DEVICE_NONE ),
|
||||
x_axis( JOYSTICK_BUTTON_INVALID ),
|
||||
y_axis( JOYSTICK_BUTTON_INVALID ),
|
||||
z_axis( JOYSTICK_BUTTON_INVALID ),
|
||||
x_min( 0 ), y_min( 0 ), z_min( 0 ),
|
||||
x_max( 0 ), y_max( 0 ), z_max( 0 )
|
||||
{
|
||||
}
|
||||
|
||||
@@ -73,38 +78,34 @@ private:
|
||||
bool mRunning;
|
||||
CString mDescription;
|
||||
|
||||
static void AddJoystick(const void *value, void *context);
|
||||
static void AddJoystick( const void *value, void *context );
|
||||
|
||||
public:
|
||||
JoystickDevice();
|
||||
~JoystickDevice();
|
||||
|
||||
bool Open(io_object_t device, int num, InputHandler_Carbon *handler);
|
||||
bool Open( io_object_t device, int num, InputHandler_Carbon *handler );
|
||||
// returns the number of IDs assigned starting from startID
|
||||
int AssignJoystickIDs(int startID);
|
||||
int AssignJoystickIDs( int startID );
|
||||
inline int NumberOfSticks() const { return mSticks.size(); }
|
||||
inline const Joystick& GetStick(int index) const { return mSticks[index]; }
|
||||
inline const Joystick& GetStick( int index ) const { return mSticks[index]; }
|
||||
inline const CString& GetDescription() const { return mDescription; }
|
||||
};
|
||||
|
||||
JoystickDevice::JoystickDevice() : mInterface(NULL), mQueue(NULL),
|
||||
mRunning(false)
|
||||
JoystickDevice::JoystickDevice() : mInterface(NULL), mQueue(NULL), mRunning(false)
|
||||
{
|
||||
}
|
||||
|
||||
bool JoystickDevice::Open(io_object_t device, int num,
|
||||
InputHandler_Carbon *handler)
|
||||
bool JoystickDevice::Open( io_object_t device, int num, InputHandler_Carbon *handler )
|
||||
{
|
||||
IOReturn ret;
|
||||
CFMutableDictionaryRef properties;
|
||||
kern_return_t result;
|
||||
|
||||
result = IORegistryEntryCreateCFProperties(device, &properties,
|
||||
kCFAllocatorDefault,
|
||||
kNilOptions);
|
||||
if (result != KERN_SUCCESS || !properties)
|
||||
result = IORegistryEntryCreateCFProperties( device, &properties, kCFAllocatorDefault, kNilOptions );
|
||||
if ( result != KERN_SUCCESS || !properties )
|
||||
{
|
||||
LOG->Warn("Couldn't get properties.");
|
||||
LOG->Warn( "Couldn't get properties." );
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -113,52 +114,48 @@ bool JoystickDevice::Open(io_object_t device, int num,
|
||||
|
||||
CFArrayRef sticks;
|
||||
|
||||
if (!(sticks = (CFArrayRef)DictValue(properties, kIOHIDElementKey)))
|
||||
if ( !(sticks = (CFArrayRef)DictValue(properties, kIOHIDElementKey)) )
|
||||
{
|
||||
CFRelease(properties);
|
||||
CFRelease( properties );
|
||||
return false;
|
||||
}
|
||||
size_t count = CFArrayGetCount(sticks);
|
||||
CFRange r = {0, count};
|
||||
size_t count = CFArrayGetCount( sticks );
|
||||
CFRange r = { 0, count };
|
||||
|
||||
mSticks.reserve(count);
|
||||
CFArrayApplyFunction(sticks, r, JoystickDevice::AddJoystick, this);
|
||||
mSticks.reserve( count );
|
||||
CFArrayApplyFunction( sticks, r, JoystickDevice::AddJoystick, this );
|
||||
|
||||
CFRelease(properties);
|
||||
CFRelease( properties );
|
||||
|
||||
// Create the interface
|
||||
IOCFPlugInInterface **plugInInterface;
|
||||
// Ugh. HRESULT an awful return type
|
||||
HRESULT hresult;
|
||||
SInt32 score;
|
||||
|
||||
ret = IOCreatePlugInInterfaceForService(device,
|
||||
kIOHIDDeviceUserClientTypeID,
|
||||
kIOCFPlugInInterfaceID,
|
||||
&plugInInterface, &score);
|
||||
if (ret != kIOReturnSuccess)
|
||||
ret = IOCreatePlugInInterfaceForService( device, kIOHIDDeviceUserClientTypeID,
|
||||
kIOCFPlugInInterfaceID, &plugInInterface, &score );
|
||||
if( ret != kIOReturnSuccess )
|
||||
{
|
||||
PrintIOErr(ret, "Failed to create plugin interface.");
|
||||
PrintIOErr( ret, "Failed to create plugin interface." );
|
||||
return false;
|
||||
}
|
||||
|
||||
// Call a method of the plugin to create the device interface
|
||||
CFUUIDBytes bytes = CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID);
|
||||
CFUUIDBytes bytes = CFUUIDGetUUIDBytes( kIOHIDDeviceInterfaceID );
|
||||
|
||||
hresult = CALL(plugInInterface, QueryInterface, bytes,
|
||||
(void **)&mInterface);
|
||||
hresult = CALL( plugInInterface, QueryInterface, bytes, (void **)&mInterface );
|
||||
|
||||
CALL(plugInInterface, Release);
|
||||
CALL( plugInInterface, Release );
|
||||
|
||||
if (hresult != S_OK)
|
||||
if( hresult != S_OK )
|
||||
{
|
||||
LOG->Warn("Couldn't get device interface from plugin interface.");
|
||||
LOG->Warn( "Couldn't get device interface from plugin interface." );
|
||||
mInterface = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
// open the interface
|
||||
if ((ret = CALL(mInterface, open, 0)) != kIOReturnSuccess)
|
||||
if( (ret = CALL(mInterface, open, 0)) != kIOReturnSuccess )
|
||||
{
|
||||
PrintIOErr(ret, "Failed to open the interface.");
|
||||
CALL(mInterface, Release);
|
||||
@@ -168,13 +165,13 @@ bool JoystickDevice::Open(io_object_t device, int num,
|
||||
|
||||
// alloc/create queue
|
||||
mQueue = CALL(mInterface, allocQueue);
|
||||
if (!mQueue)
|
||||
if( !mQueue )
|
||||
{
|
||||
LOG->Warn("Couldn't allocate a queue.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((ret = CALL(mQueue, create, 0, 32)) != kIOReturnSuccess)
|
||||
if( (ret = CALL(mQueue, create, 0, 32)) != kIOReturnSuccess )
|
||||
{
|
||||
PrintIOErr(ret, "Failed to create the queue.");
|
||||
CALL(mQueue, Release);
|
||||
@@ -183,61 +180,52 @@ bool JoystickDevice::Open(io_object_t device, int num,
|
||||
}
|
||||
|
||||
// Add elements to the queue for each Joystick
|
||||
for (vector<Joystick>::const_iterator i = mSticks.begin();
|
||||
i != mSticks.end(); ++i)
|
||||
for( vector<Joystick>::const_iterator i = mSticks.begin(); i != mSticks.end(); ++i )
|
||||
{
|
||||
const Joystick& js = *i;
|
||||
|
||||
if (js.x_axis)
|
||||
CALL(mQueue, addElement, IOHIDElementCookie(js.x_axis), 0);
|
||||
if (js.y_axis)
|
||||
CALL(mQueue, addElement, IOHIDElementCookie(js.y_axis), 0);
|
||||
if (js.z_axis)
|
||||
CALL(mQueue, addElement, IOHIDElementCookie(js.z_axis), 0);
|
||||
if( js.x_axis )
|
||||
CALL( mQueue, addElement, IOHIDElementCookie(js.x_axis), 0 );
|
||||
if( js.y_axis )
|
||||
CALL( mQueue, addElement, IOHIDElementCookie(js.y_axis), 0 );
|
||||
if( js.z_axis )
|
||||
CALL( mQueue, addElement, IOHIDElementCookie(js.z_axis), 0 );
|
||||
|
||||
for (hash_map<int, int>::const_iterator j = js.mapping.begin();
|
||||
j != js.mapping.end(); ++j)
|
||||
{
|
||||
CALL(mQueue, addElement, IOHIDElementCookie(j->first), 0);
|
||||
}
|
||||
for( hash_map<int, int>::const_iterator j = js.mapping.begin(); j != js.mapping.end(); ++j)
|
||||
CALL( mQueue, addElement, IOHIDElementCookie(j->first), 0 );
|
||||
}
|
||||
// add the callback
|
||||
CFRunLoopSourceRef runLoopSource;
|
||||
|
||||
ret = CALL(mQueue, createAsyncEventSource, &runLoopSource);
|
||||
ret = CALL( mQueue, createAsyncEventSource, &runLoopSource );
|
||||
|
||||
if (ret != kIOReturnSuccess)
|
||||
if( ret != kIOReturnSuccess )
|
||||
{
|
||||
PrintIOErr(ret, "Failed to create async event source.");
|
||||
PrintIOErr( ret, "Failed to create async event source." );
|
||||
return false;
|
||||
}
|
||||
|
||||
CFRunLoopRef runLoop;
|
||||
|
||||
runLoop = CFRunLoopRef(GetCFRunLoopFromEventLoop(GetMainEventLoop()));
|
||||
runLoop = CFRunLoopRef( GetCFRunLoopFromEventLoop(GetMainEventLoop()) );
|
||||
|
||||
if (!CFRunLoopContainsSource(runLoop, runLoopSource,
|
||||
kCFRunLoopDefaultMode))
|
||||
if( !CFRunLoopContainsSource(runLoop, runLoopSource, kCFRunLoopDefaultMode) )
|
||||
CFRunLoopAddSource( runLoop, runLoopSource, kCFRunLoopDefaultMode );
|
||||
|
||||
CALL( mQueue, setEventCallout, InputHandler_Carbon::QueueCallBack, handler, (void *)num );
|
||||
|
||||
if( ret != kIOReturnSuccess )
|
||||
{
|
||||
CFRunLoopAddSource(runLoop, runLoopSource,
|
||||
kCFRunLoopDefaultMode);
|
||||
}
|
||||
|
||||
CALL(mQueue, setEventCallout, InputHandler_Carbon::QueueCallBack,
|
||||
handler, (void *)num);
|
||||
|
||||
if (ret != kIOReturnSuccess)
|
||||
{
|
||||
PrintIOErr(ret, "Failed to set the call back.");
|
||||
PrintIOErr( ret, "Failed to set the call back." );
|
||||
return false;
|
||||
}
|
||||
|
||||
// start the queue
|
||||
ret = CALL(mQueue, start);
|
||||
ret = CALL( mQueue, start );
|
||||
|
||||
if (ret != kIOReturnSuccess)
|
||||
if( ret != kIOReturnSuccess )
|
||||
{
|
||||
PrintIOErr(ret, "Failed to start the queue.");
|
||||
PrintIOErr( ret, "Failed to start the queue." );
|
||||
return false;
|
||||
}
|
||||
mRunning = true;
|
||||
@@ -246,43 +234,39 @@ bool JoystickDevice::Open(io_object_t device, int num,
|
||||
|
||||
JoystickDevice::~JoystickDevice()
|
||||
{
|
||||
if (mQueue)
|
||||
if( mQueue )
|
||||
{
|
||||
CFRunLoopSourceRef runLoopSource;
|
||||
|
||||
if (mRunning)
|
||||
CALL(mQueue, stop);
|
||||
if ((runLoopSource = CALL(mQueue, getAsyncEventSource)))
|
||||
if( mRunning )
|
||||
CALL( mQueue, stop );
|
||||
if( (runLoopSource = CALL(mQueue, getAsyncEventSource)) )
|
||||
{
|
||||
CFRunLoopRef ref;
|
||||
|
||||
ref = CFRunLoopRef(GetCFRunLoopFromEventLoop(GetMainEventLoop()));
|
||||
ref = CFRunLoopRef( GetCFRunLoopFromEventLoop(GetMainEventLoop()) );
|
||||
|
||||
if (CFRunLoopContainsSource(ref, runLoopSource,
|
||||
kCFRunLoopDefaultMode))
|
||||
{
|
||||
CFRunLoopRemoveSource(ref, runLoopSource,
|
||||
kCFRunLoopDefaultMode);
|
||||
}
|
||||
CFRelease(runLoopSource);
|
||||
if( CFRunLoopContainsSource(ref, runLoopSource, kCFRunLoopDefaultMode) )
|
||||
CFRunLoopRemoveSource( ref, runLoopSource, kCFRunLoopDefaultMode );
|
||||
CFRelease( runLoopSource );
|
||||
}
|
||||
|
||||
CALL(mQueue, dispose);
|
||||
CALL(mQueue, Release);
|
||||
CALL( mQueue, dispose );
|
||||
CALL( mQueue, Release );
|
||||
}
|
||||
if (mInterface)
|
||||
if( mInterface )
|
||||
{
|
||||
CALL(mInterface, close);
|
||||
CALL(mInterface, Release);
|
||||
CALL( mInterface, close );
|
||||
CALL( mInterface, Release );
|
||||
}
|
||||
}
|
||||
|
||||
void JoystickDevice::AddJoystick(const void *value, void *context)
|
||||
void JoystickDevice::AddJoystick( const void *value, void *context )
|
||||
{
|
||||
if (CFGetTypeID(CFTypeRef(value)) != CFDictionaryGetTypeID())
|
||||
if( CFGetTypeID(CFTypeRef(value)) != CFDictionaryGetTypeID() )
|
||||
return;
|
||||
|
||||
CFDictionaryRef dict = CFDictionaryRef(value);
|
||||
CFDictionaryRef dict = CFDictionaryRef( value );
|
||||
JoystickDevice *This = (JoystickDevice *)context;
|
||||
CFArrayRef elements;
|
||||
CFTypeRef object;
|
||||
@@ -290,55 +274,43 @@ void JoystickDevice::AddJoystick(const void *value, void *context)
|
||||
CFTypeID numID = CFNumberGetTypeID();
|
||||
|
||||
// Get usage page
|
||||
object = DictValue(dict, kIOHIDElementUsagePageKey);
|
||||
if (!object || CFGetTypeID(object) != numID ||
|
||||
!IntValue(object, &usagePage))
|
||||
{
|
||||
object = DictValue( dict, kIOHIDElementUsagePageKey );
|
||||
if( !object || CFGetTypeID(object) != numID || !IntValue(object, &usagePage) )
|
||||
return;
|
||||
}
|
||||
|
||||
// Get usage
|
||||
object = DictValue(dict, kIOHIDElementUsageKey);
|
||||
if (!object || CFGetTypeID(object) != numID ||
|
||||
!IntValue(object, &usage))
|
||||
{
|
||||
object = DictValue( dict, kIOHIDElementUsageKey );
|
||||
if( !object || CFGetTypeID(object) != numID || !IntValue(object, &usage) )
|
||||
return;
|
||||
}
|
||||
|
||||
if (usagePage != kHIDPage_GenericDesktop ||
|
||||
usage != kHIDUsage_GD_Joystick)
|
||||
{
|
||||
if( usagePage != kHIDPage_GenericDesktop || usage != kHIDUsage_GD_Joystick )
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(elements = (CFArrayRef)DictValue(dict, kIOHIDElementKey)))
|
||||
if( !(elements = (CFArrayRef)DictValue(dict, kIOHIDElementKey)) )
|
||||
return;
|
||||
|
||||
Joystick js;
|
||||
CFRange r = {0, CFArrayGetCount(elements)};
|
||||
CFRange r = { 0, CFArrayGetCount(elements) };
|
||||
|
||||
CFArrayApplyFunction(elements, r, AddElement, &js);
|
||||
This->mSticks.push_back(js);
|
||||
CFArrayApplyFunction( elements, r, AddElement, &js );
|
||||
This->mSticks.push_back( js );
|
||||
}
|
||||
|
||||
|
||||
int JoystickDevice::AssignJoystickIDs(int startID)
|
||||
int JoystickDevice::AssignJoystickIDs( int startID )
|
||||
{
|
||||
for (vector<Joystick>::iterator i = mSticks.begin();
|
||||
i != mSticks.end(); ++i)
|
||||
{
|
||||
i->id = InputDevice(startID++);
|
||||
}
|
||||
for( vector<Joystick>::iterator i = mSticks.begin(); i != mSticks.end(); ++i )
|
||||
i->id = InputDevice( startID++ );
|
||||
return mSticks.size();
|
||||
}
|
||||
|
||||
// Callback
|
||||
static void AddElement(const void *value, void *context)
|
||||
static void AddElement( const void *value, void *context )
|
||||
{
|
||||
if (CFGetTypeID(CFTypeRef(value)) != CFDictionaryGetTypeID())
|
||||
if( CFGetTypeID(CFTypeRef(value)) != CFDictionaryGetTypeID() )
|
||||
return;
|
||||
|
||||
CFDictionaryRef dict = CFDictionaryRef(value);
|
||||
CFDictionaryRef dict = CFDictionaryRef( value );
|
||||
Joystick& js = *(Joystick *)context;
|
||||
CFTypeRef object;
|
||||
CFTypeID numID = CFNumberGetTypeID();
|
||||
@@ -346,54 +318,45 @@ static void AddElement(const void *value, void *context)
|
||||
CFArrayRef elements;
|
||||
|
||||
// Recursively add elements
|
||||
if ((elements = (CFArrayRef)DictValue(dict, kIOHIDElementKey)))
|
||||
if( (elements = (CFArrayRef)DictValue(dict, kIOHIDElementKey)) )
|
||||
{
|
||||
CFRange r = {0, CFArrayGetCount(elements)};
|
||||
CFRange r = { 0, CFArrayGetCount(elements) };
|
||||
|
||||
CFArrayApplyFunction(elements, r, AddElement, context);
|
||||
CFArrayApplyFunction( elements, r, AddElement, context );
|
||||
}
|
||||
|
||||
// Get usage page
|
||||
object = DictValue(dict, kIOHIDElementUsagePageKey);
|
||||
if (!object || CFGetTypeID(object) != numID ||
|
||||
!IntValue(object, &usagePage))
|
||||
{
|
||||
object = DictValue( dict, kIOHIDElementUsagePageKey );
|
||||
if( !object || CFGetTypeID(object) != numID || !IntValue(object, &usagePage) )
|
||||
return;
|
||||
}
|
||||
|
||||
// Get usage
|
||||
object = DictValue(dict, kIOHIDElementUsageKey);
|
||||
if (!object || CFGetTypeID(object) != numID ||
|
||||
!IntValue(object, &usage))
|
||||
{
|
||||
object = DictValue( dict, kIOHIDElementUsageKey );
|
||||
if( !object || CFGetTypeID(object) != numID || !IntValue(object, &usage) )
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Get cookie
|
||||
object = DictValue(dict, kIOHIDElementCookieKey);
|
||||
if (!object || CFGetTypeID(object) != numID ||
|
||||
!IntValue(object, &cookie))
|
||||
{
|
||||
object = DictValue( dict, kIOHIDElementCookieKey );
|
||||
if( !object || CFGetTypeID(object) != numID || !IntValue(object, &cookie) )
|
||||
return;
|
||||
}
|
||||
|
||||
switch (usagePage)
|
||||
switch( usagePage )
|
||||
{
|
||||
case kHIDPage_GenericDesktop:
|
||||
{
|
||||
int min = 0;
|
||||
int max = 0;
|
||||
|
||||
object = DictValue(dict, kIOHIDElementMinKey);
|
||||
if (object && CFGetTypeID(object) == numID)
|
||||
IntValue(object, &min);
|
||||
object = DictValue( dict, kIOHIDElementMinKey );
|
||||
if( object && CFGetTypeID(object) == numID )
|
||||
IntValue( object, &min );
|
||||
|
||||
object = DictValue(dict, kIOHIDElementMaxKey);
|
||||
if (object && CFGetTypeID(object) == numID)
|
||||
IntValue(object, &max);
|
||||
object = DictValue( dict, kIOHIDElementMaxKey );
|
||||
if( object && CFGetTypeID(object) == numID )
|
||||
IntValue( object, &max );
|
||||
|
||||
switch (usage)
|
||||
switch( usage )
|
||||
{
|
||||
case kHIDUsage_GD_X:
|
||||
js.x_axis = cookie;
|
||||
@@ -433,7 +396,7 @@ static void AddElement(const void *value, void *context)
|
||||
// button 1 = JOY_BUTTON_1
|
||||
const int buttonID = usage + JOY_BUTTON_1 - 1;
|
||||
|
||||
if (buttonID < NUM_JOYSTICK_BUTTONS)
|
||||
if( buttonID < NUM_JOYSTICK_BUTTONS )
|
||||
js.mapping[cookie] = buttonID;
|
||||
break;
|
||||
}
|
||||
@@ -442,8 +405,7 @@ static void AddElement(const void *value, void *context)
|
||||
} // end switch (usagePage)
|
||||
}
|
||||
|
||||
void InputHandler_Carbon::QueueCallBack(void *target, int result,
|
||||
void *refcon, void *sender)
|
||||
void InputHandler_Carbon::QueueCallBack( void *target, int result, void *refcon, void *sender )
|
||||
{
|
||||
// The result seems useless as you can't actually return anything...
|
||||
// refcon is the JoystickDevice number
|
||||
@@ -452,44 +414,43 @@ void InputHandler_Carbon::QueueCallBack(void *target, int result,
|
||||
InputHandler_Carbon *This = (InputHandler_Carbon *)target;
|
||||
IOHIDQueueInterface **queue = (IOHIDQueueInterface **)sender;
|
||||
IOHIDEventStruct event;
|
||||
AbsoluteTime zeroTime = {0,0};
|
||||
JoystickDevice *jd = This->mDevices[int(refcon)];
|
||||
AbsoluteTime zeroTime = { 0, 0 };
|
||||
JoystickDevice *jd = This->mDevices[int( refcon )];
|
||||
|
||||
while ((result = CALL(queue, getNextEvent, &event, zeroTime, 0))
|
||||
== kIOReturnSuccess)
|
||||
while( (result = CALL(queue, getNextEvent, &event, zeroTime, 0)) == kIOReturnSuccess )
|
||||
{
|
||||
int cookie = int(event.elementCookie);
|
||||
int cookie = int( event.elementCookie );
|
||||
int value = event.value;
|
||||
|
||||
for (int i = 0; i < jd->NumberOfSticks(); ++i)
|
||||
for( int i = 0; i < jd->NumberOfSticks(); ++i )
|
||||
{
|
||||
const Joystick& js = jd->GetStick(i);
|
||||
const Joystick& js = jd->GetStick( i );
|
||||
|
||||
if (js.x_axis == cookie)
|
||||
if( js.x_axis == cookie )
|
||||
{
|
||||
float level = SCALE(value, js.x_min, js.x_max, -1.0f, 1.0f);
|
||||
float level = SCALE( value, js.x_min, js.x_max, -1.0f, 1.0f );
|
||||
|
||||
LOG->Trace("(%d) x-axis: %f\n", int(js.id), level);
|
||||
This->ButtonPressed(DeviceInput(js.id, JOY_LEFT, max(-level, 0.0f), now), level < -0.5f);
|
||||
This->ButtonPressed(DeviceInput(js.id, JOY_RIGHT, max(level, 0.0f), now), level > 0.5f);
|
||||
LOG->Trace( "(%d) x-axis: %f\n", int(js.id), level );
|
||||
This->ButtonPressed( DeviceInput(js.id, JOY_LEFT, max(-level, 0.0f), now), level < -0.5f );
|
||||
This->ButtonPressed( DeviceInput(js.id, JOY_RIGHT, max(level, 0.0f), now), level > 0.5f );
|
||||
break;
|
||||
}
|
||||
else if (js.y_axis == cookie)
|
||||
else if( js.y_axis == cookie )
|
||||
{
|
||||
float level = SCALE(value, js.y_min, js.y_max, -1.0f, 1.0f);
|
||||
float level = SCALE( value, js.y_min, js.y_max, -1.0f, 1.0f );
|
||||
|
||||
LOG->Trace("(%d) y-axis: %f\n", int(js.id), level);
|
||||
This->ButtonPressed(DeviceInput(js.id, JOY_UP, max(-level, 0.0f), now), level < -0.5f);
|
||||
This->ButtonPressed(DeviceInput(js.id, JOY_DOWN, max(level, 0.0f), now), level > 0.5f);
|
||||
LOG->Trace( "(%d) y-axis: %f\n", int(js.id), level );
|
||||
This->ButtonPressed( DeviceInput(js.id, JOY_UP, max(-level, 0.0f), now), level < -0.5f );
|
||||
This->ButtonPressed( DeviceInput(js.id, JOY_DOWN, max(level, 0.0f), now), level > 0.5f );
|
||||
break;
|
||||
}
|
||||
else if (js.z_axis == cookie)
|
||||
else if( js.z_axis == cookie )
|
||||
{
|
||||
float level = SCALE(value, js.z_min, js.z_max, -1.0f, 1.0f);
|
||||
float level = SCALE( value, js.z_min, js.z_max, -1.0f, 1.0f );
|
||||
|
||||
LOG->Trace("(%d) z-axis: %f\n", int(js.id), level);
|
||||
This->ButtonPressed(DeviceInput(js.id, JOY_Z_UP, max(-level, 0.0f), now), level < -0.5f);
|
||||
This->ButtonPressed(DeviceInput(js.id, JOY_Z_DOWN, max(level, 0.0f), now), level > 0.5f);
|
||||
LOG->Trace( "(%d) z-axis: %f\n", int(js.id), level );
|
||||
This->ButtonPressed( DeviceInput(js.id, JOY_Z_UP, max(-level, 0.0f), now), level < -0.5f );
|
||||
This->ButtonPressed( DeviceInput(js.id, JOY_Z_DOWN, max(level, 0.0f), now), level > 0.5f );
|
||||
break;
|
||||
}
|
||||
else
|
||||
@@ -497,12 +458,11 @@ void InputHandler_Carbon::QueueCallBack(void *target, int result,
|
||||
// hash_map<T,U>::operator[] is not const
|
||||
hash_map<int, int>::const_iterator iter;
|
||||
|
||||
iter = js.mapping.find(cookie);
|
||||
if (iter != js.mapping.end())
|
||||
iter = js.mapping.find( cookie );
|
||||
if( iter != js.mapping.end() )
|
||||
{
|
||||
LOG->Trace("(%d) Button %d: %s\n", int(js.id),
|
||||
iter->second, (value ? "down" : "up"));
|
||||
This->ButtonPressed(DeviceInput(js.id, iter->second, value, now), value);
|
||||
LOG->Trace( "(%d) Button %d: %s\n", int(js.id), iter->second, (value ? "down" : "up") );
|
||||
This->ButtonPressed( DeviceInput(js.id, iter->second, value, now), value );
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -510,22 +470,19 @@ void InputHandler_Carbon::QueueCallBack(void *target, int result,
|
||||
}
|
||||
}
|
||||
|
||||
OSStatus InputHandler_Carbon::EventHandler(EventHandlerCallRef callRef,
|
||||
EventRef event, void *data)
|
||||
OSStatus InputHandler_Carbon::EventHandler( EventHandlerCallRef callRef, EventRef event, void *data )
|
||||
{
|
||||
InputHandler_Carbon *This = (InputHandler_Carbon *)data;
|
||||
UInt32 kind = GetEventKind(event);
|
||||
UInt32 keyCode;
|
||||
char charCode;
|
||||
|
||||
GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL,
|
||||
sizeof(keyCode), NULL, &keyCode);
|
||||
GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL,
|
||||
sizeof(charCode), NULL, &charCode);
|
||||
GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(keyCode), NULL, &keyCode );
|
||||
GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(charCode), NULL, &charCode );
|
||||
|
||||
const char *type;
|
||||
|
||||
switch (kind)
|
||||
switch( kind )
|
||||
{
|
||||
case kEventRawKeyDown:
|
||||
type = "down";
|
||||
@@ -539,47 +496,41 @@ OSStatus InputHandler_Carbon::EventHandler(EventHandlerCallRef callRef,
|
||||
type = "unknown";
|
||||
}
|
||||
|
||||
LOG->Trace("(0) %u %c: %s\n", unsigned(keyCode), charCode, type);
|
||||
LOG->Trace( "(0) %u %c: %s\n", unsigned(keyCode), charCode, type );
|
||||
return 0;
|
||||
}
|
||||
|
||||
InputHandler_Carbon::~InputHandler_Carbon()
|
||||
{
|
||||
for (vector<JoystickDevice *>::iterator i = mDevices.begin();
|
||||
i != mDevices.end(); ++i)
|
||||
{
|
||||
for( vector<JoystickDevice *>::iterator i = mDevices.begin(); i != mDevices.end(); ++i)
|
||||
delete *i;
|
||||
}
|
||||
if (mMasterPort)
|
||||
mach_port_deallocate(mach_task_self(), mMasterPort);
|
||||
RemoveEventHandler(mEventHandlerRef);
|
||||
DisposeEventHandlerUPP(mEventHandlerUPP);
|
||||
if( mMasterPort )
|
||||
mach_port_deallocate( mach_task_self(), mMasterPort );
|
||||
RemoveEventHandler( mEventHandlerRef );
|
||||
DisposeEventHandlerUPP( mEventHandlerUPP );
|
||||
}
|
||||
|
||||
InputHandler_Carbon::InputHandler_Carbon()
|
||||
{
|
||||
// Install a Carbon Event handler
|
||||
mEventHandlerUPP = NewEventHandlerUPP(EventHandler);
|
||||
mEventHandlerUPP = NewEventHandlerUPP( EventHandler );
|
||||
EventTypeSpec typeList[] = {
|
||||
{ kEventClassKeyboard, kEventRawKeyDown },
|
||||
{ kEventClassKeyboard, kEventRawKeyRepeat },
|
||||
{ kEventClassKeyboard, kEventRawKeyUp } };
|
||||
|
||||
|
||||
if (InstallEventHandler(GetApplicationEventTarget(), mEventHandlerUPP,
|
||||
3, typeList, this, &mEventHandlerRef))
|
||||
{
|
||||
if( InstallEventHandler(GetApplicationEventTarget(), mEventHandlerUPP, 3, typeList, this, &mEventHandlerRef) )
|
||||
LOG->Warn("Failed to install the Event Handler.");
|
||||
}
|
||||
|
||||
// Get a Mach port to initiate communication with I/O Kit.
|
||||
mach_port_t masterPort;
|
||||
|
||||
IOReturn ret = IOMasterPort(MACH_PORT_NULL, &masterPort);
|
||||
IOReturn ret = IOMasterPort( MACH_PORT_NULL, &masterPort );
|
||||
|
||||
if (ret != kIOReturnSuccess)
|
||||
if( ret != kIOReturnSuccess )
|
||||
{
|
||||
PrintIOErr(ret, "Couldn't create a master I/O Kit port.");
|
||||
PrintIOErr( ret, "Couldn't create a master I/O Kit port." );
|
||||
return;
|
||||
}
|
||||
mMasterPort = masterPort;
|
||||
@@ -587,20 +538,19 @@ InputHandler_Carbon::InputHandler_Carbon()
|
||||
// Build the matching dictionary.
|
||||
CFMutableDictionaryRef dict;
|
||||
|
||||
if ((dict = IOServiceMatching(kIOHIDDeviceKey)) == NULL)
|
||||
if( (dict = IOServiceMatching(kIOHIDDeviceKey)) == NULL )
|
||||
{
|
||||
LOG->Warn("Couldn't create a matching dictionary.");
|
||||
LOG->Warn( "Couldn't create a matching dictionary." );
|
||||
mach_port_deallocate(mach_task_self(), masterPort);
|
||||
return;
|
||||
}
|
||||
// Refine the search by only looking for joysticks
|
||||
CFNumberRef usagePage = CFInt(kHIDPage_GenericDesktop);
|
||||
CFNumberRef usage = CFInt(kHIDUsage_GD_Joystick);
|
||||
CFNumberRef usagePage = CFInt( kHIDPage_GenericDesktop );
|
||||
CFNumberRef usage = CFInt( kHIDUsage_GD_Joystick );
|
||||
|
||||
CFDictionarySetValue(dict, CFSTR(kIOHIDPrimaryUsagePageKey),
|
||||
usagePage);
|
||||
CFDictionarySetValue(dict, CFSTR(kIOHIDPrimaryUsageKey),
|
||||
usage);
|
||||
CFDictionarySetValue( dict, CFSTR(kIOHIDPrimaryUsagePageKey), usagePage );
|
||||
CFDictionarySetValue( dict, CFSTR(kIOHIDPrimaryUsageKey), usage);
|
||||
|
||||
// Cleanup after ourselves
|
||||
CFRelease(usagePage);
|
||||
CFRelease(usage);
|
||||
@@ -608,15 +558,14 @@ InputHandler_Carbon::InputHandler_Carbon()
|
||||
// Find the HID devices.
|
||||
io_iterator_t iter;
|
||||
|
||||
|
||||
/* Get an iterator to the matching devies
|
||||
* This consumes a reference to the dictionary so we don't
|
||||
* have to Release() later.
|
||||
*/
|
||||
ret = IOServiceGetMatchingServices(masterPort, dict, &iter);
|
||||
if ((ret != kIOReturnSuccess) || !iter)
|
||||
ret = IOServiceGetMatchingServices( masterPort, dict, &iter );
|
||||
if( (ret != kIOReturnSuccess) || !iter )
|
||||
{
|
||||
mach_port_deallocate(mach_task_self(), masterPort);
|
||||
mach_port_deallocate( mach_task_self(), masterPort );
|
||||
masterPort = 0;
|
||||
return;
|
||||
}
|
||||
@@ -625,37 +574,37 @@ InputHandler_Carbon::InputHandler_Carbon()
|
||||
io_object_t device;
|
||||
int id = DEVICE_JOY1;
|
||||
|
||||
while ((device = IOIteratorNext(iter)))
|
||||
while( (device = IOIteratorNext(iter)) )
|
||||
{
|
||||
JoystickDevice *jd = new JoystickDevice;
|
||||
|
||||
if (jd->Open(device, mDevices.size(), this))
|
||||
if( jd->Open(device, mDevices.size(), this) )
|
||||
{
|
||||
id += jd->AssignJoystickIDs(id);
|
||||
mDevices.push_back(jd);
|
||||
puts("Added device.");
|
||||
id += jd->AssignJoystickIDs( id );
|
||||
mDevices.push_back( jd );
|
||||
puts( "Added device." );
|
||||
}
|
||||
else
|
||||
{
|
||||
delete jd;
|
||||
}
|
||||
|
||||
IOObjectRelease(device);
|
||||
IOObjectRelease( device );
|
||||
|
||||
}
|
||||
IOObjectRelease(iter);
|
||||
IOObjectRelease( iter );
|
||||
}
|
||||
|
||||
void InputHandler_Carbon::GetDevicesAndDescriptions(vector<InputDevice>& dev,
|
||||
vector<CString>& desc)
|
||||
void InputHandler_Carbon::GetDevicesAndDescriptions( vector<InputDevice>& dev, vector<CString>& desc )
|
||||
{
|
||||
for (vector<JoystickDevice *>::const_iterator i = mDevices.begin();
|
||||
i != mDevices.end(); ++i)
|
||||
for( vector<JoystickDevice *>::const_iterator i = mDevices.begin(); i != mDevices.end(); ++i )
|
||||
{
|
||||
const JoystickDevice *jd = *i;
|
||||
|
||||
for (int j = 0; j < jd->NumberOfSticks(); ++j)
|
||||
for( int j = 0; j < jd->NumberOfSticks(); ++j )
|
||||
{
|
||||
dev.push_back(jd->GetStick(j).id);
|
||||
desc.push_back(jd->GetDescription());
|
||||
dev.push_back( jd->GetStick(j).id );
|
||||
desc.push_back( jd->GetDescription() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,11 +10,7 @@ typedef struct OpaqueEventRef *EventRef;
|
||||
typedef struct OpaqueEventHandlerRef *EventHandlerRef;
|
||||
typedef struct OpaqueEventTargetRef *EventTargetRef;
|
||||
typedef long int OSStatus;
|
||||
typedef OSStatus (*EventHandlerProcPtr) (
|
||||
EventHandlerCallRef inHandlerCallRef,
|
||||
EventRef inEvent,
|
||||
void *inUserData
|
||||
);
|
||||
typedef OSStatus (*EventHandlerProcPtr) ( EventHandlerCallRef, EventRef, void * );
|
||||
typedef EventHandlerProcPtr EventHandlerUPP;
|
||||
|
||||
class JoystickDevice;
|
||||
@@ -31,13 +27,11 @@ public:
|
||||
InputHandler_Carbon();
|
||||
~InputHandler_Carbon();
|
||||
|
||||
void GetDevicesAndDescriptions(vector<InputDevice>& vDevicesOut,
|
||||
vector<CString>& vDescriptionsOut);
|
||||
void GetDevicesAndDescriptions( vector<InputDevice>& vDevicesOut,
|
||||
vector<CString>& vDescriptionsOut );
|
||||
|
||||
static void QueueCallBack(void *target, int result,
|
||||
void *refcon, void *sender);
|
||||
static OSStatus EventHandler(EventHandlerCallRef callRef, EventRef event,
|
||||
void *data);
|
||||
static void QueueCallBack( void *target, int result, void *refcon, void *sender );
|
||||
static OSStatus EventHandler( EventHandlerCallRef callRef, EventRef event, void *data );
|
||||
};
|
||||
#define USE_INPUT_HANDLER_CARBON
|
||||
|
||||
|
||||
Reference in New Issue
Block a user