Files
itgmania212121/stepmania/src/archutils/Darwin/HIDDevice.h
T

149 lines
5.3 KiB
C++
Raw Normal View History

2006-01-23 03:48:03 +00:00
#ifndef HIDDEVICE_H
#define HIDDEVICE_H
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/hid/IOHIDLib.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/hid/IOHIDUsageTables.h>
#include <IOKit/usb/USB.h>
#include <mach/mach.h>
#include <mach/mach_error.h>
#include <vector>
#include <utility>
#include "RageLog.h"
#include "RageInputDevice.h"
/* A few helper functions. */
// The result needs to be released.
inline CFNumberRef CFInt( int n )
2006-01-23 03:48:03 +00:00
{
return CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &n );
}
inline void PrintIOErr( IOReturn err, const char *s )
2006-01-23 03:48:03 +00:00
{
LOG->Warn( "%s - %s(%x,%d)", s, mach_error_string(err), err, err & 0xFFFFFF );
}
inline Boolean IntValue( CFTypeRef o, int &n )
2006-01-23 03:48:03 +00:00
{
2006-02-10 09:41:42 +00:00
if( !o || CFGetTypeID(o) != CFNumberGetTypeID() )
return false;
return CFNumberGetValue( CFNumberRef(o), kCFNumberIntType, &n );
2006-01-23 03:48:03 +00:00
}
/*
* This is just awful, these aren't objects, treating them as such leads
* to: (*object)->function(object [, argument]...)
* Instead, do: CALL(object, function [, argument]...)
*/
#define CALL(o,f,...) (*(o))->f((o), ## __VA_ARGS__)
class HIDDevice
{
private:
IOHIDDeviceInterface **m_Interface;
IOHIDQueueInterface **m_Queue;
bool m_bRunning;
RString m_sDescription;
static void AddLogicalDevice( const void *value, void *context );
static void AddElement( const void *value, void *context );
protected:
2006-02-10 09:21:20 +00:00
/*
* Each physical device has zero or more logical devices. If this device allows
* a logical device of type (usagePage, usage), then allocated storage as necessary
* and return true, otherwise, return false.
*/
2006-01-23 03:48:03 +00:00
virtual bool AddLogicalDevice( int usagePage, int usage ) = 0;
2006-02-10 09:21:20 +00:00
/*
* If the most recently added logical device cares about the state of an element of type
* (usagePage, usage), store the cookie.
*/
virtual void AddElement( int usagePage, int usage, int cookie, const CFDictionaryRef properties ) = 0;
/*
* Add any elements to the queue by calling AddElementToQueue() with the stored cookies.
*/
2006-01-23 03:48:03 +00:00
virtual void Open() = 0;
/*
* Optional. Subclasses can return false if they do not support the (vid, pid) pair.
*/
virtual bool SupportsVidPid( int vid, int pid ) { return true; }
2006-02-10 09:21:20 +00:00
// This adds the element with the given cookie to the queue to be notified of state changes.
2006-01-23 03:48:03 +00:00
inline void AddElementToQueue( int cookie )
{
CALL( m_Queue, addElement, IOHIDElementCookie(cookie), 0 );
}
2006-02-27 00:44:30 +00:00
// Perform a synchronous set report on the HID interface.
inline IOReturn SetReport( IOHIDReportType type, UInt32 reportID, void *buffer, UInt32 size, UInt32 timeoutMS )
{
return CALL( m_Interface, setReport, type, reportID, buffer, size, timeoutMS, NULL, NULL, NULL );
}
2006-01-23 03:48:03 +00:00
public:
HIDDevice();
virtual ~HIDDevice();
bool Open( io_object_t device );
void StartQueue( CFRunLoopRef loopRef, IOHIDCallbackFunction callback, void *target, int refCon );
inline const RString& GetDescription() const { return m_sDescription; }
2006-02-10 09:21:20 +00:00
/*
* Add button presses (or releases) to vPresses for the given cookie. More than one DeviceInput
* can be added at a time. For example, Two axes presses may be generated by a single element.
* The value of the element is passed to determine if this is a push or a release. The time
* is provided as an optimization.
*/
2006-01-23 03:48:03 +00:00
virtual void GetButtonPresses( vector<pair<DeviceInput, bool> >& vPresses, int cookie,
int value, const RageTimer& now ) const = 0;
/*
* Returns the number of IDs assigned starting from startID. This is not meaningful for devices like
* keyboards that all share the same InputDevice id. If a particular device has multiple logical
2006-02-27 00:20:18 +00:00
* devices, then it must ensure that AssignIDs does not assign an ID outside of its range. Return
* -1 to indicate that the device does not share the same InputDevice and none could be assigned.
2006-01-23 03:48:03 +00:00
*/
virtual int AssignIDs( InputDevice startID ) { return 0; }
2006-02-10 09:21:20 +00:00
/*
* Add a device and a description for each logical device.
*/
virtual void GetDevicesAndDescriptions( vector<InputDeviceInfo>& vDevices ) const = 0;
2006-01-23 03:48:03 +00:00
};
#endif
2006-01-27 10:33:07 +00:00
/*
* (c) 2005-2006 Steve Checkoway
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/