| 1 | /* |
| 2 | The zlib/libpng License |
| 3 | |
| 4 | Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com) |
| 5 | |
| 6 | This software is provided 'as-is', without any express or implied warranty. In no event will |
| 7 | the authors be held liable for any damages arising from the use of this software. |
| 8 | |
| 9 | Permission is granted to anyone to use this software for any purpose, including commercial |
| 10 | applications, and to alter it and redistribute it freely, subject to the following |
| 11 | restrictions: |
| 12 | |
| 13 | 1. The origin of this software must not be misrepresented; you must not claim that |
| 14 | you wrote the original software. If you use this software in a product, |
| 15 | an acknowledgment in the product documentation would be appreciated but is |
| 16 | not required. |
| 17 | |
| 18 | 2. Altered source versions must be plainly marked as such, and must not be |
| 19 | misrepresented as being the original software. |
| 20 | |
| 21 | 3. This notice may not be removed or altered from any source distribution. |
| 22 | */ |
| 23 | |
| 24 | #include "mac/CocoaInputManager.h" |
| 25 | #include "mac/CocoaKeyboard.h" |
| 26 | #include "mac/CocoaMouse.h" |
| 27 | #include "mac/MacHIDManager.h" |
| 28 | #include "OISException.h" |
| 29 | |
| 30 | using namespace std; |
| 31 | using namespace OIS; |
| 32 | |
| 33 | //--------------------------------------------------------------------------------// |
| 34 | CocoaInputManager::CocoaInputManager() : InputManager("Mac OS X Cocoa Input Manager") |
| 35 | { |
| 36 | mHideMouse = true; |
| 37 | mUseRepeat = false; |
| 38 | mWindow = nil; |
| 39 | |
| 40 | keyboardUsed = mouseUsed = false; |
| 41 | |
| 42 | //Setup our internal factories |
| 43 | mFactories.push_back(this); |
| 44 | |
| 45 | mHIDManager = new MacHIDManager(); |
| 46 | mFactories.push_back(mHIDManager); |
| 47 | } |
| 48 | |
| 49 | //--------------------------------------------------------------------------------// |
| 50 | CocoaInputManager::~CocoaInputManager() |
| 51 | { |
| 52 | delete mHIDManager; |
| 53 | } |
| 54 | |
| 55 | //--------------------------------------------------------------------------------// |
| 56 | void CocoaInputManager::_initialize( ParamList ¶mList ) |
| 57 | { |
| 58 | _parseConfigSettings( paramList ); |
| 59 | |
| 60 | //Enumerate all devices attached |
| 61 | _enumerateDevices(); |
| 62 | |
| 63 | mHIDManager->initialize(); |
| 64 | } |
| 65 | |
| 66 | //--------------------------------------------------------------------------------// |
| 67 | void CocoaInputManager::_parseConfigSettings( ParamList ¶mList ) |
| 68 | { |
| 69 | // Some carbon apps are running in a window, however full screen apps |
| 70 | // do not have a window, so we need to account for that too. |
| 71 | ParamList::iterator i = paramList.find("WINDOW"); |
| 72 | if(i != paramList.end()) |
| 73 | { |
| 74 | mWindow = (NSWindow *)strtoul(i->second.c_str(), 0, 10); |
| 75 | if(mWindow == 0) |
| 76 | { |
| 77 | mWindow = nil; |
| 78 | } |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | // else get the main active window.. user might not have access to it through some |
| 83 | // graphics libraries, if that fails then try at the application level. |
| 84 | mWindow = [[NSApplication sharedApplication] keyWindow]; |
| 85 | } |
| 86 | |
| 87 | if(mWindow == nil) |
| 88 | OIS_EXCEPT( E_General, "CocoaInputManager::_parseConfigSettings >> Unable to find a window or event target" ); |
| 89 | |
| 90 | // Keyboard |
| 91 | if(paramList.find("MacAutoRepeatOn") != paramList.end()) |
| 92 | { |
| 93 | if(paramList.find("MacAutoRepeatOn")->second == "true") |
| 94 | { |
| 95 | mUseRepeat = true; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | //--------------------------------------------------------------------------------// |
| 101 | void CocoaInputManager::_enumerateDevices() |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | //--------------------------------------------------------------------------------// |
| 106 | DeviceList CocoaInputManager::freeDeviceList() |
| 107 | { |
| 108 | DeviceList ret; |
| 109 | |
| 110 | if( keyboardUsed == false ) |
| 111 | ret.insert(std::make_pair(OISKeyboard, mInputSystemName)); |
| 112 | |
| 113 | if( mouseUsed == false ) |
| 114 | ret.insert(std::make_pair(OISMouse, mInputSystemName)); |
| 115 | |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | //--------------------------------------------------------------------------------// |
| 120 | int CocoaInputManager::totalDevices(Type iType) |
| 121 | { |
| 122 | switch(iType) |
| 123 | { |
| 124 | case OISKeyboard: return 1; |
| 125 | case OISMouse: return 1; |
| 126 | default: return 0; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | //--------------------------------------------------------------------------------// |
| 131 | int CocoaInputManager::freeDevices(Type iType) |
| 132 | { |
| 133 | switch(iType) |
| 134 | { |
| 135 | case OISKeyboard: return keyboardUsed ? 0 : 1; |
| 136 | case OISMouse: return mouseUsed ? 0 : 1; |
| 137 | default: return 0; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | //--------------------------------------------------------------------------------// |
| 142 | bool CocoaInputManager::vendorExist(Type iType, const std::string & vendor) |
| 143 | { |
| 144 | if( (iType == OISKeyboard || iType == OISMouse) && vendor == mInputSystemName ) |
| 145 | return true; |
| 146 | |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | //--------------------------------------------------------------------------------// |
| 151 | Object* CocoaInputManager::createObject(InputManager* creator, Type iType, bool bufferMode, |
| 152 | const std::string & vendor) |
| 153 | { |
| 154 | Object *obj = 0; |
| 155 | |
| 156 | switch(iType) |
| 157 | { |
| 158 | case OISKeyboard: |
| 159 | { |
| 160 | if( keyboardUsed == false ) |
| 161 | obj = new CocoaKeyboard(this, bufferMode, mUseRepeat); |
| 162 | break; |
| 163 | } |
| 164 | case OISMouse: |
| 165 | { |
| 166 | if( mouseUsed == false ) |
| 167 | obj = new CocoaMouse(this, bufferMode); |
| 168 | break; |
| 169 | } |
| 170 | default: |
| 171 | { |
| 172 | obj = mHIDManager->createObject(creator, iType, bufferMode, vendor); |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if( obj == 0 ) |
| 178 | OIS_EXCEPT(E_InputDeviceNonExistant, "No devices match requested type."); |
| 179 | |
| 180 | return obj; |
| 181 | } |
| 182 | |
| 183 | //--------------------------------------------------------------------------------// |
| 184 | void CocoaInputManager::destroyObject(Object* obj) |
| 185 | { |
| 186 | delete obj; |
| 187 | } |