From: Oliver O. <fr...@us...> - 2007-07-24 01:00:42
|
Update of /cvsroot/simspark/simspark/spark/utility/ois/mac In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv13856/mac Added Files: Tag: projectx MacHelpers.cpp MacHelpers.h MacInputManager.cpp MacInputManager.h MacKeyboard.cpp MacKeyboard.h MacMouse.cpp MacMouse.h MacPrereqs.h Log Message: added OIS Library sources --- NEW FILE: MacHelpers.h --- /* The zlib/libpng License Copyright (c) 2006 Chris Snyder 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. */ #ifndef OIS_MacHelpers_H #define OIS_MacHelpers_H #include "mac/MacPrereqs.h" #include "OISEvents.h" #include "OISKeyboard.h" #include "OISMouse.h" #include <Carbon/Carbon.h> // This is a hack needed to get the event handler working. // The carbon lib expects a "OSStatus (*)(EventHandlerCallRef, EventRef, void*)", // so I cannot give it a class member function (unless it is static which is pointless) // Instead, I just pass the class* through the last paramter that gets passed to the // callback every time an event occurs. Then I dereference it and call the member function. OSStatus KeyDownWrapper( EventHandlerCallRef nextHandler, EventRef theEvent, void* callClass ); OSStatus KeyUpWrapper( EventHandlerCallRef nextHandler, EventRef theEvent, void* callClass ); OSStatus KeyModWrapper( EventHandlerCallRef nextHandler, EventRef theEvent, void* callClass ); OSStatus MouseMoveWrapper( EventHandlerCallRef nextHandler, EventRef theEvent, void* callClass ); OSStatus MouseButtonWrapper( EventHandlerCallRef nextHandler, EventRef theEvent, void* callClass ); OSStatus MouseScrollWrapper( EventHandlerCallRef nextHandler, EventRef theEvent, void* callClass ); // This is needed for keeping an event stack for keyboard and mouse namespace OIS { // used in the eventStack to store the type enum Mac_EventType { MAC_KEYUP = 0, MAC_KEYDOWN = 1, MAC_KEYREPEAT, MAC_MOUSEDOWN, MAC_MOUSEUP, MAC_MOUSEMOVED, MAC_MOUSESCROLL}; typedef enum Mac_EventType MacEventType; // only used by MacKeyboard typedef class Mac_KeyStackEvent { friend class MacKeyboard; private: Mac_KeyStackEvent( KeyEvent event, MacEventType type ) : Event(event), Type(type) {} MacEventType Type; KeyEvent Event; } MacKeyStackEvent; // only used by MacMouse typedef class Mac_MouseStackEvent { friend class MacMouse; private: Mac_MouseStackEvent( MacEventType type, unsigned int time ) : Type(type), Time(time) {} unsigned int Time; MacEventType Type; MouseButtonID button; Axis axis; } MacMouseStackEvent; } #endif --- NEW FILE: MacInputManager.h --- /* The zlib/libpng License Copyright (c) 2006 Chris Snyder 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. */ #ifndef OIS_MacInputManager_H #define OIS_MacInputManager_H #include "OISInputManager.h" #include "mac/MacPrereqs.h" #include <Carbon/Carbon.h> namespace OIS { class MacInputManager : public InputManager { public: MacInputManager(); virtual ~MacInputManager(); virtual const std::string& inputSystemName() { return iName; } virtual int numJoysticks(); virtual int numMice(); virtual int numKeyBoards(); Object* createInputObject( Type iType, bool bufferMode ); void destroyInputObject( Object* obj ); // method for getting window WindowRef _getWindow() {return window;} protected: void _initialize( ParamList& paramList ); void _parseConfigSettings( ParamList& paramList ); void _enumerateDevices(); static const std::string iName; // Mac stuff WindowRef window; // settings bool hideMouse; bool useRepeat; }; } #endif --- NEW FILE: MacKeyboard.cpp --- /* The zlib/libpng License Copyright (c) 2006 Chris Snyder 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/MacKeyboard.h" #include "mac/MacInputManager.h" #include "mac/MacHelpers.h" #include "OISException.h" #include "OISEvents.h" #include <Carbon/Carbon.h> #include <list> #include <string> //#include <iostream> //using namespace std; using namespace OIS; //-------------------------------------------------------------------// MacKeyboard::MacKeyboard( InputManager* creator, bool buffered, bool repeat ) { mCreator = creator; keyDownEventRef = NULL; keyUpEventRef = NULL; keyModEventRef = NULL; mBuffered = buffered; useRepeat = repeat; // Get a so-called "Univeral procedure pointer" for our callback keyDownUPP = NewEventHandlerUPP( KeyDownWrapper ); keyUpUPP = NewEventHandlerUPP( KeyUpWrapper ); keyModUPP = NewEventHandlerUPP( KeyModWrapper ); // populate the conversion map populateKeyConversion(); } //-------------------------------------------------------------------// MacKeyboard::~MacKeyboard() { // Remove our handlers so that this instance doesn't get called // after it is deleted if ( keyDownEventRef != NULL ) RemoveEventHandler( keyDownEventRef ); if ( keyUpEventRef != NULL ) RemoveEventHandler( keyUpEventRef ); if ( keyModEventRef != NULL ) RemoveEventHandler( keyUpEventRef ); // dispose of our UPPs DisposeEventHandlerUPP( keyDownUPP ); DisposeEventHandlerUPP( keyUpUPP ); DisposeEventHandlerUPP( keyModUPP ); } //-------------------------------------------------------------------// void MacKeyboard::_initialize() { OSStatus status; WindowRef window = ( (MacInputManager*)mCreator)->_getWindow(); memset( &KeyBuffer, 0, 256 ); mModifiers = 0; prevModMask = 0; // just in case this gets called after the first time.. better safe if ( keyDownEventRef != NULL ) RemoveEventHandler( keyDownEventRef ); if ( keyUpEventRef != NULL ) RemoveEventHandler( keyUpEventRef ); if ( keyModEventRef != NULL ) RemoveEventHandler( keyUpEventRef ); keyDownEventRef = NULL; keyUpEventRef = NULL; keyModEventRef = NULL; EventTypeSpec DownSpec[2]; EventTypeSpec UpSpec, ModSpec, RepeatSpec; // Set the type of events we wish to handle DownSpec[0].eventClass = kEventClassKeyboard; DownSpec[0].eventKind = kEventRawKeyDown; DownSpec[1].eventClass = kEventClassKeyboard; DownSpec[1].eventKind = kEventRawKeyRepeat; UpSpec.eventClass = kEventClassKeyboard; UpSpec.eventKind = kEventRawKeyUp; // Separate events for modifiers change, because they aren't reported normally ModSpec.eventClass = kEventClassKeyboard; ModSpec.eventKind = kEventRawKeyModifiersChanged; if ( useRepeat ) { // send both elements of downspec array... second index is for repeat events status = InstallWindowEventHandler( window, keyDownUPP, 2, DownSpec, this, &keyDownEventRef ); } else { status = InstallWindowEventHandler( window, keyDownUPP, 1, DownSpec, this, &keyDownEventRef ); } if ( status != noErr ) { OIS_EXCEPT( E_General, "MacKeyboard::_initialize >> Error loading KeyDown event handler" ); } status = InstallWindowEventHandler( window, keyUpUPP, 1, &UpSpec, this, &keyUpEventRef ); if ( status != noErr ) { OIS_EXCEPT( E_General, "MacKeyboard::_initialize >> Error loading KeyUp event handler" ); } status = InstallWindowEventHandler( window, keyModUPP, 1, &ModSpec, this, &keyModEventRef ); if ( status != noErr ) { OIS_EXCEPT( E_General, "MacKeyboard::_initialize >> Error loading Keymods event handler" ); } } //-------------------------------------------------------------------// bool MacKeyboard::isKeyDown( KeyCode key ) { return (bool)KeyBuffer[key]; } //-------------------------------------------------------------------// void MacKeyboard::capture() { // if not buffered just return, we update the unbuffered automatically if ( !mBuffered || !listener ) return; // run through our event stack eventStack::iterator cur_it; for (cur_it = pendingEvents.begin(); cur_it != pendingEvents.end(); cur_it++) { if ( (*cur_it).Type == MAC_KEYDOWN || (*cur_it).Type == MAC_KEYREPEAT) { listener->keyPressed( (*cur_it).Event ); } else if ( (*cur_it).Type == MAC_KEYUP ) { listener->keyReleased( (*cur_it).Event ); } } pendingEvents.clear(); } //-------------------------------------------------------------------// std::string& MacKeyboard::getAsString( KeyCode key ) { getString = ""; return getString; } //-------------------------------------------------------------------// void MacKeyboard::setBuffered( bool buffered ) { mBuffered = buffered; } //-------------------------------------------------------------------// void MacKeyboard::_keyDownCallback( EventRef theEvent ) { UInt32 virtualKey; OSStatus status; unsigned int time = (unsigned int)GetEventTime(theEvent); status = GetEventParameter( theEvent, 'kcod', // get it in virtual keycode typeUInt32, NULL, // desired return type sizeof(UInt32), NULL, // bufsize &virtualKey ); KeyCode kc = keyConversion[virtualKey]; // record what kind of text we should pass the KeyEvent UniChar text[10]; char macChar; // TODO clean this up if (mTextMode == Unicode) { //get string size UInt32 stringsize; status = GetEventParameter( theEvent, 'kuni', typeUnicodeText, NULL, 0, &stringsize, NULL); //cout << "String length: " << stringsize << endl; status = GetEventParameter( theEvent, 'kuni', typeUnicodeText, NULL, sizeof(UniChar)*10, NULL, &text ); //wstring unitext; //for (int i=0;i<10;i++) unitext += (wchar_t)text[i]; //wcout << "Unicode out: " << unitext << endl; // for each unicode char, send an event stringsize--; // no termination char for ( int i = 0; i < stringsize; i++ ) { injectEvent( kc, time, MAC_KEYDOWN, (unsigned int)text[i] ); } } else if (mTextMode == Ascii) { status = GetEventParameter( theEvent, 'kchr', typeChar, NULL, sizeof(char), NULL, &macChar ); injectEvent( kc, time, MAC_KEYDOWN, (unsigned int)macChar ); } else { injectEvent( kc, time, MAC_KEYDOWN ); } } //-------------------------------------------------------------------// void MacKeyboard::_keyUpCallback( EventRef theEvent ) { UInt32 virtualKey; OSStatus status; status = GetEventParameter( theEvent, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &virtualKey ); KeyCode kc = keyConversion[virtualKey]; injectEvent( kc, (int)GetEventTime(theEvent), MAC_KEYUP ); } //-------------------------------------------------------------------// void MacKeyboard::_modChangeCallback( EventRef theEvent ) { UInt32 mods; OSStatus status; status = GetEventParameter( theEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &mods ); // find the changed bit UInt32 change = prevModMask ^ mods; MacEventType newstate = ((change & prevModMask) > 0) ? MAC_KEYUP : MAC_KEYDOWN; unsigned int time = (int)GetEventTime( theEvent ); //cout << "preMask: " << hex << prevModMask << endl; //cout << "ModMask: " << hex << mods << endl; //cout << "Change: " << hex << (change & prevModMask) << endl << endl; // TODO test modifiers on a full keyboard to check if different mask for left/right switch (change) { case (shiftKey): // shift mModifiers &= (newstate == MAC_KEYDOWN) ? Shift : ~Shift; injectEvent( KC_LSHIFT, time, newstate ); //injectEvent( KC_RSHIFT, time, newstate ); break; case (optionKey): // option (alt) mModifiers &= (newstate == MAC_KEYDOWN) ? Alt : -Alt; //injectEvent( KC_RMENU, time, newstate ); injectEvent( KC_LMENU, time, newstate ); break; case (controlKey): // Ctrl mModifiers += (newstate == MAC_KEYDOWN) ? Ctrl : -Ctrl; //injectEvent( KC_RCONTROL, time, newstate ); injectEvent( KC_LCONTROL, time, newstate ); break; case (cmdKey): // apple //injectEvent( KC_RWIN, time, newstate ); injectEvent( KC_LWIN, time, newstate ); break; case (kEventKeyModifierFnMask): // fn key injectEvent( KC_APPS, time, newstate ); break; case (kEventKeyModifierNumLockMask): // numlock injectEvent( KC_NUMLOCK, time, newstate ); break; case (alphaLock): // caps lock injectEvent( KC_CAPITAL, time, newstate ); break; } prevModMask = mods; } //-------------------------------------------------------------------// void MacKeyboard::injectEvent( KeyCode kc, unsigned int time, MacEventType type, unsigned int txt ) { // set to 1 if this is either a keydown or repeat KeyBuffer[kc] = ( type == MAC_KEYUP ) ? 0 : 1; if ( mBuffered && listener ) { pendingEvents.push_back( MacKeyStackEvent( KeyEvent(this, kc, txt), type) ); } } //-------------------------------------------------------------------// void MacKeyboard::copyKeyStates( char keys[256] ) { memcpy( keys, KeyBuffer, 256 ); } //-------------------------------------------------------------------// void MacKeyboard::populateKeyConversion() { // TODO finish the key mapping // Virtual Key Map to KeyCode keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x12, KC_1)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x13, KC_2)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x14, KC_3)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x15, KC_4)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x17, KC_5)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x16, KC_6)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x1A, KC_7)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x1C, KC_8)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x19, KC_9)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x1D, KC_0)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x33, KC_BACK)); // might be wrong keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x1B, KC_MINUS)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x18, KC_EQUALS)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x31, KC_SPACE)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x2B, KC_COMMA)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x2F, KC_PERIOD)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x2A, KC_BACKSLASH)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x2C, KC_SLASH)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x21, KC_LBRACKET)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x1E, KC_RBRACKET)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x35, KC_ESCAPE)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x39, KC_CAPITAL)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x30, KC_TAB)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x24, KC_RETURN)); // double check return/enter //keyConversion.insert(VirtualtoOIS_KeyMap::value_type(XK_colon, KC_COLON)); // no colon? keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x29, KC_SEMICOLON)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x27, KC_APOSTROPHE)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x32, KC_GRAVE)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x0B, KC_B)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x00, KC_A)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x08, KC_C)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x02, KC_D)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x0E, KC_E)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x03, KC_F)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x05, KC_G)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x04, KC_H)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x22, KC_I)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x26, KC_J)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x28, KC_K)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x25, KC_L)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x2E, KC_M)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x2D, KC_N)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x1F, KC_O)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x23, KC_P)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x0C, KC_Q)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x0F, KC_R)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x01, KC_S)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x11, KC_T)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x20, KC_U)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x09, KC_V)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x0D, KC_W)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x07, KC_X)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x10, KC_Y)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x06, KC_Z)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x7A, KC_F1)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x78, KC_F2)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x63, KC_F3)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x76, KC_F4)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x60, KC_F5)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x61, KC_F6)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x62, KC_F7)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x64, KC_F8)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x65, KC_F9)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x6D, KC_F10)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x67, KC_F11)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x6F, KC_F12)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x69, KC_F13)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x6B, KC_F14)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x71, KC_F15)); //Keypad keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x52, KC_NUMPAD0)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x53, KC_NUMPAD1)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x54, KC_NUMPAD2)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x55, KC_NUMPAD3)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x56, KC_NUMPAD4)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x57, KC_NUMPAD5)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x58, KC_NUMPAD6)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x59, KC_NUMPAD7)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x5B, KC_NUMPAD8)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x5C, KC_NUMPAD9)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x45, KC_ADD)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x4E, KC_SUBTRACT)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x41, KC_DECIMAL)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x51, KC_NUMPADEQUALS)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x4B, KC_DIVIDE)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x43, KC_MULTIPLY)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x4C, KC_NUMPADENTER)); //Keypad with numlock off //keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x73, KC_NUMPAD7)); // not sure of these //keyConversion.insert(VirtualtoOIS_KeyMap::value_type(XK_KP_Up, KC_NUMPAD8)); // check on a non-laptop //keyConversion.insert(VirtualtoOIS_KeyMap::value_type(XK_KP_Page_Up, KC_NUMPAD9)); //keyConversion.insert(VirtualtoOIS_KeyMap::value_type(XK_KP_Left, KC_NUMPAD4)); //keyConversion.insert(VirtualtoOIS_KeyMap::value_type(XK_KP_Begin, KC_NUMPAD5)); //keyConversion.insert(VirtualtoOIS_KeyMap::value_type(XK_KP_Right, KC_NUMPAD6)); //keyConversion.insert(VirtualtoOIS_KeyMap::value_type(XK_KP_End, KC_NUMPAD1)); //keyConversion.insert(VirtualtoOIS_KeyMap::value_type(XK_KP_Down, KC_NUMPAD2)); //keyConversion.insert(VirtualtoOIS_KeyMap::value_type(XK_KP_Page_Down, KC_NUMPAD3)); //keyConversion.insert(VirtualtoOIS_KeyMap::value_type(XK_KP_Insert, KC_NUMPAD0)); //keyConversion.insert(VirtualtoOIS_KeyMap::value_type(XK_KP_Delete, KC_DECIMAL)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x7E, KC_UP)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x7D, KC_DOWN)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x7B, KC_LEFT)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x7C, KC_RIGHT)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x74, KC_PGUP)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x79, KC_PGDOWN)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x73, KC_HOME)); keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x77, KC_END)); //keyConversion.insert(VirtualtoOIS_KeyMap::value_type(XK_Print, KC_SYSRQ)); // ?? //keyConversion.insert(VirtualtoOIS_KeyMap::value_type(XK_Scroll_Lock, KC_SCROLL)); // ?? //keyConversion.insert(VirtualtoOIS_KeyMap::value_type(XK_Pause, KC_PAUSE)); // ?? //keyConversion.insert(VirtualtoOIS_KeyMap::value_type(XK_Insert, KC_INSERT)); // ?? keyConversion.insert(VirtualtoOIS_KeyMap::value_type(0x75, KC_DELETE)); // del under help key? } --- NEW FILE: MacKeyboard.h --- /* The zlib/libpng License Copyright (c) 2006 Chris Snyder 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. */ #ifndef OIS_MacKeyboard_H #define OIS_MacKeyboard_H #include "OISKeyboard.h" #include "mac/MacHelpers.h" #include "mac/MacPrereqs.h" #include <Carbon/Carbon.h> namespace OIS { class MacKeyboard : public Keyboard { public: MacKeyboard( InputManager* creator, bool buffered, bool repeat ); virtual ~MacKeyboard(); // Sets buffered mode virtual void setBuffered( bool buffered ); // unbuffered keydown check virtual bool isKeyDown( KeyCode key ); // This will send listener events if buffered is on. // Note that in the mac implementation, unbuffered input is // automatically updated without calling this. virtual void capture(); // Copies the current key buffer virtual void copyKeyStates( char keys[256] ); // Returns a description of the given key virtual std::string& getAsString( KeyCode key ); virtual Interface* queryInterface( Interface::IType type ) { return 0; } // Public but reserved for internal use: virtual void _initialize(); void _keyDownCallback( EventRef theEvent ); void _keyUpCallback( EventRef theEvent ); void _modChangeCallback( EventRef theEvent ); protected: // just to get this out of the way void populateKeyConversion(); // updates the keybuffer and optionally the eventStack void injectEvent(KeyCode kc, unsigned int time, MacEventType type, unsigned int txt = 0 ); typedef std::map<UInt32, KeyCode> VirtualtoOIS_KeyMap; VirtualtoOIS_KeyMap keyConversion; std::string getString; char KeyBuffer[256]; UInt32 prevModMask; // "universal procedure pointers" - required reference for callbacks EventHandlerUPP keyDownUPP; EventHandlerUPP keyUpUPP; EventHandlerUPP keyModUPP; // so we can delete the handlers on destruction EventHandlerRef keyDownEventRef; EventHandlerRef keyUpEventRef; EventHandlerRef keyModEventRef; // buffered events, fifo stack typedef std::list<MacKeyStackEvent> eventStack; eventStack pendingEvents; bool useRepeat; }; } #endif --- NEW FILE: MacInputManager.cpp --- /* The zlib/libpng License Copyright (c) 2006 Chris Snyder 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/MacInputManager.h" #include "mac/MacKeyboard.h" #include "mac/MacMouse.h" #include "OISException.h" #include <Carbon/Carbon.h> #include <string> //#include <iostream> //using namespace std; using namespace OIS; const std::string MacInputManager::iName = "Mac OSX Input Manager"; //--------------------------------------------------------------------------------// MacInputManager::MacInputManager() { hideMouse = true; useRepeat = false; } //--------------------------------------------------------------------------------// MacInputManager::~MacInputManager() { } //--------------------------------------------------------------------------------// void MacInputManager::_initialize( ParamList ¶mList ) { _parseConfigSettings( paramList ); //Enumerate all devices attached _enumerateDevices(); } //--------------------------------------------------------------------------------// void MacInputManager::_parseConfigSettings( ParamList ¶mList ) { ParamList::iterator i = paramList.find("WINDOW"); if( i != paramList.end() ) { //TODO 64 bit proof this little conversion xxx wip window = (WindowRef) strtoul(i->second.c_str(), 0, 10); } // else get the main active window.. user might not have access to it through some // graphics libraries. else { window = ActiveNonFloatingWindow(); } // Keyboard i = paramList.find("MacAutoRepeatOn"); if ( i != paramList.end() ) { if ( i->second == "true" ) { useRepeat = true; } } } //--------------------------------------------------------------------------------// void MacInputManager::_enumerateDevices() { } //--------------------------------------------------------------------------------// int MacInputManager::numJoysticks() { return 0; } //--------------------------------------------------------------------------------// int MacInputManager::numMice() { return 1; } //--------------------------------------------------------------------------------// int MacInputManager::numKeyBoards() { return 1; } //----------------------------------------------------------------------------// Object* MacInputManager::createInputObject( Type iType, bool bufferMode ) { Object* obj = 0; switch( iType ) { case OISKeyboard: obj = new MacKeyboard(this, bufferMode, useRepeat); break; case OISMouse: obj = new MacMouse(this, bufferMode ); break; case OISJoyStick: return obj; break; default: return obj; break; } //Seperate initialization from construction.. as bad things happen when //throwing exceptions from constructors try { obj->_initialize(); } catch(...) { delete obj; throw; //rethrow the exception } return obj; } //----------------------------------------------------------------------------// void MacInputManager::destroyInputObject( Object* obj ) { if( obj ) { delete obj; } } --- NEW FILE: MacHelpers.cpp --- /* The zlib/libpng License Copyright (c) 2006 Chris Snyder 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/MacHelpers.h" #include "mac/MacKeyboard.h" #include "mac/MacMouse.h" #include "OISException.h" #include <Carbon/Carbon.h> using namespace OIS; //-------------------------------------------------------------------// OSStatus KeyDownWrapper( EventHandlerCallRef nextHandler, EventRef theEvent, void* callClass ) { // TODO find a better way. This cast isn't very safe if (callClass != NULL) { ((MacKeyboard*)callClass)->_keyDownCallback( theEvent ); // propagate the event down the chain return CallNextEventHandler( nextHandler, theEvent ); } else { OIS_EXCEPT(E_General, "KeyDownWrapper >> Being called by something other than our event handler!"); return noErr; } } //-------------------------------------------------------------------// OSStatus KeyUpWrapper( EventHandlerCallRef nextHandler, EventRef theEvent, void* callClass ) { if (callClass != NULL) { ((MacKeyboard*)callClass)->_keyUpCallback( theEvent ); // propagate the event down the chain return CallNextEventHandler( nextHandler, theEvent ); } else { OIS_EXCEPT(E_General, "KeyUpWrapper >> Being called by something other than our event handler!"); return noErr; } } //-------------------------------------------------------------------// OSStatus KeyModWrapper( EventHandlerCallRef nextHandler, EventRef theEvent, void* callClass ) { if (callClass != NULL) { ((MacKeyboard*)callClass)->_modChangeCallback( theEvent ); // propagate the event down the chain return CallNextEventHandler( nextHandler, theEvent ); } else { OIS_EXCEPT(E_General, "KeyModWrapper >> Being called by something other than our event handler!"); return noErr; } } //-------------------------------------------------------------------// OSStatus MouseMoveWrapper( EventHandlerCallRef nextHandler, EventRef theEvent, void* callClass ) { if (callClass != NULL) { ((MacMouse*)callClass)->_mouseMoveCallback( theEvent ); // propagate the event down the chain return CallNextEventHandler( nextHandler, theEvent ); } else { OIS_EXCEPT(E_General, "MouseMoveWrapper >> Being called by something other than our event handler!"); return noErr; } } //-------------------------------------------------------------------// OSStatus MouseScrollWrapper( EventHandlerCallRef nextHandler, EventRef theEvent, void* callClass ) { if (callClass != NULL) { ((MacMouse*)callClass)->_mouseScrollCallback( theEvent ); // propagate the event down the chain return CallNextEventHandler( nextHandler, theEvent ); } else { OIS_EXCEPT(E_General, "MouseScrollWrapper >> Being called by something other than our event handler!"); return noErr; } } //-------------------------------------------------------------------// OSStatus MouseButtonWrapper( EventHandlerCallRef nextHandler, EventRef theEvent, void* callClass ) { if (callClass != NULL) { ((MacMouse*)callClass)->_mouseButtonCallback( theEvent ); // propagate the event down the chain return CallNextEventHandler( nextHandler, theEvent ); } else { OIS_EXCEPT(E_General, "MouseButtonWrapper >> Being called by something other than our event handler!"); return noErr; } } --- NEW FILE: MacPrereqs.h --- /* The zlib/libpng License Copyright (c) 2006 Chris Snyder 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. */ #ifndef OIS_MacPrereqs_H #define OIS_MacPrereqs_H #include <string> #include <list> namespace OIS { class MacInputManager; class MacKeyboard; } #endif --- NEW FILE: MacMouse.h --- #ifndef OIS_MacMouse_H #define OIS_MacMouse_H #include "OISMouse.h" #include "mac/MacHelpers.h" #include "mac/MacPrereqs.h" #include <Carbon/Carbon.h> namespace OIS { class MacMouse : public Mouse { public: MacMouse( InputManager* creator, bool buffered ); virtual ~MacMouse(); /** @copydoc Object::setBuffered */ virtual void setBuffered(bool buffered); /** @copydoc Object::capture */ virtual void capture(); /** @copydoc Object::queryInterface */ virtual Interface* queryInterface(Interface::IType type) {return 0;} /** @copydoc Object::_initialize */ virtual void _initialize(); public: void _mouseButtonCallback( EventRef theEvent ); void _mouseMoveCallback( EventRef theEvent ); void _mouseScrollCallback( EventRef theEvent ); protected: MacMouse() {} // "universal procedure pointers" - required reference for callbacks EventHandlerUPP mouseButtonUPP; EventHandlerUPP mouseMoveUPP; EventHandlerUPP mouseScrollUPP; // so we can delete the handlers on destruction EventHandlerRef mouseButtonEventRef; EventHandlerRef mouseMoveEventRef; EventHandlerRef mouseScrollEventRef; bool mRegainFocus; }; } #endif // OIS_MacMouse_H --- NEW FILE: MacMouse.cpp --- #include "mac/MacMouse.h" #include "mac/MacInputManager.h" #include "mac/MacHelpers.h" #include "OISException.h" #include "OISEvents.h" #include <Carbon/Carbon.h> #include <list> #include <string> using namespace OIS; //-------------------------------------------------------------------// MacMouse::MacMouse( InputManager* creator, bool buffered ) : mRegainFocus( false ) { mCreator = creator; mouseButtonEventRef = NULL; mouseMoveEventRef = NULL; mouseScrollEventRef = NULL; mBuffered = buffered; mType = OISMouse; listener = 0; mouseButtonUPP = NewEventHandlerUPP( MouseButtonWrapper ); mouseMoveUPP = NewEventHandlerUPP( MouseMoveWrapper ); mouseScrollUPP = NewEventHandlerUPP( MouseScrollWrapper ); } MacMouse::~MacMouse() { if( mouseButtonEventRef != NULL ) RemoveEventHandler( mouseButtonEventRef ); if( mouseMoveEventRef != NULL ) RemoveEventHandler( mouseMoveEventRef ); if( mouseScrollEventRef != NULL ) RemoveEventHandler( mouseScrollEventRef ); DisposeEventHandlerUPP( mouseButtonUPP ); DisposeEventHandlerUPP( mouseMoveUPP ); DisposeEventHandlerUPP( mouseScrollUPP ); } void MacMouse::_initialize() { mState.clear(); mRegainFocus = false; OSStatus status; WindowRef window = (( MacInputManager* )mCreator)->_getWindow(); if( mouseButtonEventRef != NULL ) RemoveEventHandler( mouseButtonEventRef ); if( mouseMoveEventRef != NULL ) RemoveEventHandler( mouseMoveEventRef ); if( mouseScrollEventRef != NULL ) RemoveEventHandler( mouseScrollEventRef ); mouseButtonEventRef = NULL; mouseMoveEventRef = NULL; mouseScrollEventRef = NULL; const EventTypeSpec mouseButtonEvents[] = { { kEventClassMouse, kEventMouseDown }, { kEventClassMouse, kEventMouseUp } }; const EventTypeSpec mouseMoveEvents[] = { { kEventClassMouse, kEventMouseMoved } }; const EventTypeSpec mouseScrollEvents[] = { { kEventClassMouse, kEventMouseWheelMoved } }; status = InstallWindowEventHandler( window, mouseButtonUPP, GetEventTypeCount( mouseButtonEvents ), mouseButtonEvents, this, &mouseButtonEventRef ); if( status != noErr ) { OIS_EXCEPT( E_General, "MacMouse::_initialize >> Error loading MouseButton event handler" ); } status = InstallWindowEventHandler( window, mouseMoveUPP, GetEventTypeCount( mouseMoveEvents ), mouseMoveEvents, this, &mouseMoveEventRef ); if( status != noErr ) { OIS_EXCEPT( E_General, "MacMouse::_initialize >> Error loading MouseMove event handler" ); } status = InstallWindowEventHandler( window, mouseScrollUPP, GetEventTypeCount( mouseScrollEvents ), mouseScrollEvents, this, &mouseScrollEventRef ); if( status != noErr ) { OIS_EXCEPT( E_General, "MacMouse::_initialize >> Error loading MouseScroll event handler" ); } } void MacMouse::setBuffered( bool buffered ) { mBuffered = buffered; } void MacMouse::capture() { if( !mBuffered ) return; // eventStack::iterator cur_it; } void MacMouse::_mouseButtonCallback( EventRef theEvent ) { } void MacMouse::_mouseMoveCallback( EventRef theEvent ) { } void MacMouse::_mouseScrollCallback( EventRef theEvent ) { } |