Files
itgmania212121/stepmania/src/arch/InputHandler/InputHandler_Carbon.cpp
T

420 lines
13 KiB
C++
Raw Normal View History

#include "global.h"
#include "RageLog.h"
#include <Carbon/Carbon.h>
#include "InputHandler_Carbon.h"
#include "Foreach.h"
#include "RageUtil.h"
#include "PrefsManager.h"
2005-10-24 11:21:46 +00:00
#include "archutils/Darwin/DarwinThreadHelpers.h"
2006-01-23 03:48:03 +00:00
#include "archutils/Darwin/KeyboardDevice.h"
#include "archutils/Darwin/JoystickDevice.h"
#include "archutils/Darwin/PumpDevice.h"
2006-01-28 07:01:10 +00:00
#include <IOKit/IOMessage.h>
2005-10-19 11:23:21 +00:00
void InputHandler_Carbon::QueueCallBack( void *target, int result, void *refcon, void *sender )
{
// The result seems useless as you can't actually return anything...
2005-10-19 11:23:21 +00:00
// refcon is the Device number
RageTimer now;
InputHandler_Carbon *This = (InputHandler_Carbon *)target;
IOHIDQueueInterface **queue = (IOHIDQueueInterface **)sender;
IOHIDEventStruct event;
AbsoluteTime zeroTime = { 0, 0 };
2006-01-23 01:37:11 +00:00
HIDDevice *dev = This->m_vDevices[int( refcon )];
2006-01-23 03:48:03 +00:00
vector<pair<DeviceInput, bool> > vPresses;
2005-10-19 11:23:21 +00:00
while( (result = CALL(queue, getNextEvent, &event, zeroTime, 0)) == kIOReturnSuccess )
2006-01-23 03:48:03 +00:00
dev->GetButtonPresses( vPresses, int(event.elementCookie), int(event.value), now );
for( vector<pair<DeviceInput, bool> >::const_iterator i = vPresses.begin(); i != vPresses.end(); ++i )
This->ButtonPressed( i->first, i->second );
}
static void RunLoopStarted( CFRunLoopObserverRef o, CFRunLoopActivity a, void *sem )
{
CFRunLoopObserverInvalidate( o );
2005-12-19 01:32:36 +00:00
CFRelease( o ); // we don't need this any longer
((RageSemaphore *)sem)->Post();
}
2005-12-19 01:32:36 +00:00
int InputHandler_Carbon::Run( void *data )
{
InputHandler_Carbon *This = (InputHandler_Carbon *)data;
2006-01-23 01:30:38 +00:00
This->m_LoopRef = CFRunLoopGetCurrent();
CFRetain( This->m_LoopRef );
This->StartDevices();
2006-01-21 11:14:13 +00:00
SetThreadPrecedence( 1.0f );
2005-10-24 11:21:46 +00:00
// Add an observer for the start of the run loop
{
/* The function copies the information out of the structure, so the memory pointed
* to by context does not need to persist beyond the function call. */
CFRunLoopObserverContext context = { 0, &This->m_Sem, NULL, NULL, NULL };
CFRunLoopObserverRef o = CFRunLoopObserverCreate( kCFAllocatorDefault, kCFRunLoopEntry,
false, 0, RunLoopStarted, &context);
CFRunLoopAddObserver( This->m_LoopRef, o, kCFRunLoopDefaultMode );
}
/* Add a source for ending the run loop. This serves two purposes:
* 1. it provides a way to terminate the run loop when IH_Carbon exists, and
* 2. it ensures that CFRunLoopRun() doesn't return immediately if there are no other sources. */
{
/* Being a little tricky here, the perform callback takes a void* and returns nothing.
* CFRunLoopStop takes a CFRunLoopRef (a pointer) so cast the function and pass the loop ref. */
void *info = This->m_LoopRef;
void (*perform)(void *) = (void (*)(void *))CFRunLoopStop;
// { version, info, retain, release, copyDescription, equal, hash, schedule, cancel, perform }
CFRunLoopSourceContext context = { 0, info, NULL, NULL, NULL, NULL, NULL, NULL, NULL, perform };
// Pass 1 so that it is called after all inputs have been handled (they will have order = 0)
This->m_SourceRef = CFRunLoopSourceCreate( kCFAllocatorDefault, 1, &context );
CFRunLoopAddSource( This->m_LoopRef, This->m_SourceRef, kCFRunLoopDefaultMode );
}
CFRunLoopRun();
LOG->Trace( "Shutting down input handler thread..." );
return 0;
}
2006-01-28 07:01:10 +00:00
void InputHandler_Carbon::DeviceAdded( void *refCon, io_iterator_t )
{
InputHandler_Carbon *This = (InputHandler_Carbon *)refCon;
LockMut( This->m_ChangeLock );
This->m_bChanged = true;
}
void InputHandler_Carbon::DeviceChanged( void *refCon, io_service_t service, natural_t messageType, void *arg )
{
if( messageType == kIOMessageServiceIsTerminated )
{
InputHandler_Carbon *This = (InputHandler_Carbon *)refCon;
LockMut( This->m_ChangeLock );
This->m_bChanged = true;
}
}
2006-01-23 01:30:38 +00:00
// m_LoopRef needs to be set before this is called
void InputHandler_Carbon::StartDevices()
{
int n = 0;
2006-01-23 01:30:38 +00:00
ASSERT( m_LoopRef );
2006-01-23 01:37:11 +00:00
FOREACH( HIDDevice *, m_vDevices, i )
2006-01-23 01:30:38 +00:00
(*i)->StartQueue( m_LoopRef, InputHandler_Carbon::QueueCallBack, this, n++ );
2006-01-28 07:01:10 +00:00
CFRunLoopSourceRef runLoopSource = IONotificationPortGetRunLoopSource( m_NotifyPort );
CFRunLoopAddSource( m_LoopRef, runLoopSource, kCFRunLoopDefaultMode );
}
InputHandler_Carbon::~InputHandler_Carbon()
{
2006-01-23 01:37:11 +00:00
FOREACH( HIDDevice *, m_vDevices, i )
delete *i;
if( PREFSMAN->m_bThreadedInput )
{
CFRunLoopSourceSignal( m_SourceRef );
CFRunLoopWakeUp( m_LoopRef );
2006-01-23 01:30:38 +00:00
m_InputThread.Wait();
CFRelease( m_SourceRef );
2006-05-14 07:04:28 +00:00
CFRelease( m_LoopRef );
LOG->Trace( "Input handler thread shut down." );
}
2006-01-28 07:01:10 +00:00
FOREACH( io_iterator_t, m_vIters, i )
IOObjectRelease( *i );
IONotificationPortDestroy( m_NotifyPort );
}
2006-01-28 07:01:10 +00:00
static CFDictionaryRef GetMatchingDictionary( int usagePage, int usage )
2005-10-19 21:12:18 +00:00
{
// Build the matching dictionary.
CFMutableDictionaryRef dict;
if( (dict = IOServiceMatching(kIOHIDDeviceKey)) == NULL )
2006-01-28 07:01:10 +00:00
FAIL_M( "Couldn't create a matching dictionary." );
2005-10-19 21:12:18 +00:00
// Refine the search by only looking for joysticks
CFNumberRef usagePageRef = CFInt( usagePage );
CFNumberRef usageRef = CFInt( usage );
CFDictionarySetValue( dict, CFSTR(kIOHIDPrimaryUsagePageKey), usagePageRef );
CFDictionarySetValue( dict, CFSTR(kIOHIDPrimaryUsageKey), usageRef );
// Cleanup after ourselves
CFRelease( usagePageRef );
CFRelease( usageRef );
2006-01-28 07:01:10 +00:00
return dict;
2005-10-19 21:12:18 +00:00
}
// Factor this out because nothing else in IH_C::AddDevices() needs to know the type of the device.
static HIDDevice *MakeDevice( InputDevice id )
{
if( id == DEVICE_KEYBOARD )
return new KeyboardDevice;
2006-09-14 06:06:53 +00:00
if( IsJoystick(id) )
return new JoystickDevice;
2006-09-14 06:06:53 +00:00
if( IsPump(id) )
return new PumpDevice;
return NULL;
}
void InputHandler_Carbon::AddDevices( int usagePage, int usage, InputDevice &id )
{
2006-01-28 07:01:10 +00:00
io_iterator_t iter;
CFDictionaryRef dict = GetMatchingDictionary( usagePage, usage );
2006-01-28 07:01:10 +00:00
kern_return_t ret = IOServiceAddMatchingNotification( m_NotifyPort, kIOFirstMatchNotification, dict,
InputHandler_Carbon::DeviceAdded, this, &iter );
io_object_t device;
2006-01-28 07:01:10 +00:00
if( ret != KERN_SUCCESS )
return;
2006-01-28 07:01:10 +00:00
m_vIters.push_back( iter );
// Iterate over the devices and add them
while( (device = IOIteratorNext(iter)) )
{
HIDDevice *dev = MakeDevice( id );
2006-02-27 00:20:18 +00:00
int num;
if( !dev )
{
IOObjectRelease( device );
continue;
}
2006-02-27 00:20:18 +00:00
if( !dev->Open(device) || (num = dev->AssignIDs(id)) == -1 )
{
delete dev;
IOObjectRelease( device );
continue;
}
io_iterator_t i;
enum_add( id, num );
m_vDevices.push_back( dev );
ret = IOServiceAddInterestNotification( m_NotifyPort, device, kIOGeneralInterest,
InputHandler_Carbon::DeviceChanged, this, &i );
if( ret == KERN_SUCCESS )
m_vIters.push_back( i );
IOObjectRelease( device );
}
}
InputHandler_Carbon::InputHandler_Carbon() : m_Sem( "Input thread started" ), m_ChangeLock( "Input handler change lock" )
{
InputDevice id = DEVICE_KEYBOARD;
// Set up the notify ports.
m_NotifyPort = IONotificationPortCreate( kIOMasterPortDefault );
// Add devices.
AddDevices( kHIDPage_GenericDesktop, kHIDUsage_GD_Keyboard, id );
id = DEVICE_JOY1;
AddDevices( kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick, id );
AddDevices( kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad, id );
id = DEVICE_PUMP1;
2006-02-24 21:51:31 +00:00
AddDevices( kHIDPage_VendorDefinedStart, 0x0001, id ); // Pump pads use the first vendor specific usage page.
2006-01-28 07:01:10 +00:00
m_bChanged = false;
if( PREFSMAN->m_bThreadedInput )
{
2006-01-23 01:30:38 +00:00
m_InputThread.SetName( "Input thread" );
m_InputThread.Create( InputHandler_Carbon::Run, this );
// Wait for the run loop to start before returning.
2006-01-23 01:30:38 +00:00
m_Sem.Wait();
}
else
{
2006-01-23 01:30:38 +00:00
m_LoopRef = CFRunLoopRef( GetCFRunLoopFromEventLoop(GetMainEventLoop()) );
2006-05-14 07:04:28 +00:00
CFRetain( m_LoopRef );
StartDevices();
}
}
void InputHandler_Carbon::GetDevicesAndDescriptions( vector<InputDeviceInfo>& vDevices )
{
2006-01-23 01:37:11 +00:00
FOREACH_CONST( HIDDevice *, m_vDevices, i )
(*i)->GetDevicesAndDescriptions( vDevices );
}
RString InputHandler_Carbon::GetDeviceSpecificInputString( const DeviceInput &di )
{
if( di.device == DEVICE_KEYBOARD )
{
#define OTHER(n) (KEY_OTHER_0 + (n))
switch( di.button )
{
case KEY_DEL: return "del";
case KEY_BACK: return "delete";
case KEY_ENTER: return "return";
case KEY_LALT: return "left option";
case KEY_RALT: return "right option";
case KEY_LMETA: return "left cmd";
case KEY_RMETA: return "right cmd";
case KEY_INSERT: return "help";
case OTHER(0): return "F17";
case OTHER(1): return "F18";
case OTHER(2): return "F19";
case OTHER(3): return "F20";
case OTHER(4): return "F21";
case OTHER(5): return "F22";
case OTHER(6): return "F23";
case OTHER(7): return "F25";
case OTHER(8): return "execute";
case OTHER(9): return "select";
case OTHER(10): return "stop";
case OTHER(11): return "again";
case OTHER(12): return "undo";
case OTHER(13): return "cut";
case OTHER(14): return "copy";
case OTHER(15): return "paste";
case OTHER(16): return "find";
case OTHER(17): return "mute";
case OTHER(18): return "volume up";
case OTHER(19): return "volume down";
case OTHER(20): return "AS/400 equal";
case OTHER(21): return "international 1";
case OTHER(22): return "international 2";
case OTHER(23): return "international 3";
case OTHER(24): return "international 4";
case OTHER(25): return "international 5";
case OTHER(26): return "international 6";
case OTHER(27): return "international 7";
case OTHER(28): return "international 8";
case OTHER(29): return "international 9";
case OTHER(30): return "lang 1";
case OTHER(31): return "lang 2";
case OTHER(32): return "lang 3";
case OTHER(33): return "lang 4";
case OTHER(34): return "lang 5";
case OTHER(35): return "lang 6";
case OTHER(36): return "lang 7";
case OTHER(37): return "lang 8";
case OTHER(38): return "lang 9";
case OTHER(39): return "alt erase";
case OTHER(40): return "sys req";
case OTHER(41): return "cancel";
case OTHER(42): return "separator";
case OTHER(43): return "out";
case OTHER(44): return "oper";
case OTHER(45): return "clear/again"; // XXX huh?
case OTHER(46): return "cr sel/props"; // XXX
case OTHER(47): return "ex sel";
case OTHER(48): return "non US backslash";
case OTHER(49): return "application";
case OTHER(50): return "prior";
}
#undef OTHER
}
if( di.device == DEVICE_PUMP1 || di.device == DEVICE_PUMP2 )
{
switch( di.button )
{
case JOY_BUTTON_1: return "UL";
case JOY_BUTTON_2: return "UR";
case JOY_BUTTON_3: return "MID";
case JOY_BUTTON_4: return "DL";
case JOY_BUTTON_5: return "DR";
case JOY_BUTTON_6: return "Esc";
case JOY_BUTTON_7: return "P2 UL";
case JOY_BUTTON_8: return "P2 UR";
case JOY_BUTTON_9: return "P2 MID";
case JOY_BUTTON_10: return "P2 DL";
case JOY_BUTTON_11: return "P2 DR";
}
}
return InputHandler::GetDeviceSpecificInputString( di );
}
2006-06-11 07:12:01 +00:00
wchar_t InputHandler_Carbon::DeviceButtonToChar( DeviceButton button, bool bUseCurrentKeyModifiers )
{
// KeyTranslate maps these keys to a character. They shouldn't be mapped to any character.
switch( button )
{
default:
if( (button >= KEY_F1 && button <= KEY_F16) )
2006-09-14 07:20:05 +00:00
return L'\0';
break;
case KEY_UP:
case KEY_DOWN:
case KEY_LEFT:
case KEY_RIGHT:
case KEY_ESC:
case KEY_TAB:
2006-06-16 06:28:31 +00:00
case KEY_ENTER:
case KEY_PRTSC:
case KEY_SCRLLOCK:
case KEY_PAUSE:
case KEY_DEL:
case KEY_HOME:
case KEY_END:
case KEY_PGUP:
case KEY_PGDN:
case KEY_NUMLOCK:
case KEY_KP_ENTER:
2006-09-14 07:20:05 +00:00
return L'\0';
}
2006-06-11 07:12:01 +00:00
// Find the USB key code for this DeviceButton
UInt8 iMacVirtualKey;
if( KeyboardDevice::DeviceButtonToMacVirtualKey( button, iMacVirtualKey ) )
{
UInt32 modifiers = 0;
if( bUseCurrentKeyModifiers )
modifiers = GetCurrentKeyModifiers();
static unsigned long state = 0;
2006-09-14 07:20:05 +00:00
static Ptr keymap = NULL;
2006-06-11 07:12:01 +00:00
Ptr new_keymap;
new_keymap = (Ptr)GetScriptManagerVariable(smKCHRCache);
if( new_keymap != keymap )
{
keymap = new_keymap;
state = 0;
}
// TODO: Use UCKeyTranslate
wchar_t ch = KeyTranslate( keymap, ((int)iMacVirtualKey)|modifiers, &state ) & 0xFFFF;
return ch;
}
return InputHandler::DeviceButtonToChar( button, bUseCurrentKeyModifiers );
}
/*
2006-01-14 01:04:48 +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.
*/