| [1505] | 1 | /* | 
|---|
 | 2 |  The zlib/libpng License | 
|---|
 | 3 |   | 
|---|
 | 4 |  Copyright (c) 2006 Chris Snyder  | 
|---|
 | 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 | #include "mac/MacInputManager.h" | 
|---|
 | 24 | #include "mac/MacKeyboard.h" | 
|---|
 | 25 | #include "mac/MacMouse.h" | 
|---|
 | 26 | #include "mac/MacHIDManager.h" | 
|---|
 | 27 | #include "OISException.h" | 
|---|
 | 28 |  | 
|---|
 | 29 | #include <Carbon/Carbon.h> | 
|---|
 | 30 |  | 
|---|
 | 31 | #include <iostream> | 
|---|
 | 32 | using namespace std; | 
|---|
 | 33 |  | 
|---|
 | 34 | using namespace OIS; | 
|---|
 | 35 |  | 
|---|
 | 36 | //--------------------------------------------------------------------------------// | 
|---|
 | 37 | MacInputManager::MacInputManager() : InputManager("Mac OSX Input Manager") | 
|---|
 | 38 | { | 
|---|
 | 39 |     mHideMouse = true; | 
|---|
 | 40 |     mUseRepeat = false; | 
|---|
 | 41 |     mEventTargetRef = NULL; | 
|---|
 | 42 |         mWindow = NULL; | 
|---|
 | 43 |  | 
|---|
 | 44 |         keyboardUsed = mouseUsed = false; | 
|---|
 | 45 |  | 
|---|
 | 46 |         //Setup our internal factories | 
|---|
 | 47 |         mFactories.push_back(this); | 
|---|
 | 48 |  | 
|---|
 | 49 |         mHIDManager = new MacHIDManager(); | 
|---|
 | 50 |         mFactories.push_back(mHIDManager); | 
|---|
 | 51 | } | 
|---|
 | 52 |  | 
|---|
 | 53 | //--------------------------------------------------------------------------------// | 
|---|
 | 54 | MacInputManager::~MacInputManager() | 
|---|
 | 55 | { | 
|---|
 | 56 |         delete mHIDManager; | 
|---|
 | 57 | } | 
|---|
 | 58 |  | 
|---|
 | 59 | //--------------------------------------------------------------------------------// | 
|---|
 | 60 | void MacInputManager::_initialize( ParamList ¶mList ) | 
|---|
 | 61 | { | 
|---|
 | 62 |         _parseConfigSettings( paramList ); | 
|---|
 | 63 |      | 
|---|
 | 64 |         //Enumerate all devices attached | 
|---|
 | 65 |         _enumerateDevices(); | 
|---|
 | 66 |          | 
|---|
 | 67 |         mHIDManager->initialize(); | 
|---|
 | 68 | } | 
|---|
 | 69 |  | 
|---|
 | 70 | //--------------------------------------------------------------------------------// | 
|---|
 | 71 | void MacInputManager::_parseConfigSettings( ParamList ¶mList ) | 
|---|
 | 72 | { | 
|---|
 | 73 |     // Some carbon apps are running in a window, however full screen apps | 
|---|
 | 74 |         // do not have a window, so we need to account for that too. | 
|---|
 | 75 |         ParamList::iterator i = paramList.find("WINDOW"); | 
|---|
 | 76 |         if(i != paramList.end()) | 
|---|
 | 77 |         { | 
|---|
 | 78 |                 mWindow = (WindowRef)strtoul(i->second.c_str(), 0, 10); | 
|---|
 | 79 |                 if(mWindow == 0) | 
|---|
 | 80 |                 { | 
|---|
 | 81 |                         mWindow = NULL; | 
|---|
 | 82 |                         mEventTargetRef = GetApplicationEventTarget(); | 
|---|
 | 83 |                 } | 
|---|
 | 84 |                 else | 
|---|
 | 85 |                 { | 
|---|
 | 86 |                         //mEventTargetRef = GetWindowEventTarget(mWindow); | 
|---|
 | 87 |                         mEventTargetRef = GetApplicationEventTarget(); | 
|---|
 | 88 |                 } | 
|---|
 | 89 |     } | 
|---|
 | 90 |         else | 
|---|
 | 91 |         { | 
|---|
 | 92 |                 // else get the main active window.. user might not have access to it through some | 
|---|
 | 93 |                 // graphics libraries, if that fails then try at the application level. | 
|---|
 | 94 |                 mWindow = ActiveNonFloatingWindow(); | 
|---|
 | 95 |                 if(mWindow == NULL) | 
|---|
 | 96 |                 { | 
|---|
 | 97 |                         mEventTargetRef = GetApplicationEventTarget(); | 
|---|
 | 98 |                 } | 
|---|
 | 99 |                 else | 
|---|
 | 100 |                 { | 
|---|
 | 101 |                         //mEventTargetRef = GetWindowEventTarget(mWindow); | 
|---|
 | 102 |                         mEventTargetRef = GetApplicationEventTarget(); | 
|---|
 | 103 |                 } | 
|---|
 | 104 |         } | 
|---|
 | 105 |          | 
|---|
 | 106 |         if(mEventTargetRef == NULL) | 
|---|
 | 107 |                 OIS_EXCEPT( E_General, "MacInputManager::_parseConfigSettings >> Unable to find a window or event target" ); | 
|---|
 | 108 |      | 
|---|
 | 109 |     // Keyboard | 
|---|
 | 110 |     if(paramList.find("MacAutoRepeatOn") != paramList.end()) | 
|---|
 | 111 |         { | 
|---|
 | 112 |         if(paramList.find("MacAutoRepeatOn")->second == "true") | 
|---|
 | 113 |                 { | 
|---|
 | 114 |             mUseRepeat = true; | 
|---|
 | 115 |         } | 
|---|
 | 116 |     } | 
|---|
 | 117 | } | 
|---|
 | 118 |  | 
|---|
 | 119 | //--------------------------------------------------------------------------------// | 
|---|
 | 120 | void MacInputManager::_enumerateDevices() | 
|---|
 | 121 | { | 
|---|
 | 122 | } | 
|---|
 | 123 |  | 
|---|
 | 124 | //--------------------------------------------------------------------------------// | 
|---|
 | 125 | DeviceList MacInputManager::freeDeviceList() | 
|---|
 | 126 | { | 
|---|
 | 127 |         DeviceList ret; | 
|---|
 | 128 |  | 
|---|
 | 129 |         if( keyboardUsed == false ) | 
|---|
 | 130 |                 ret.insert(std::make_pair(OISKeyboard, mInputSystemName)); | 
|---|
 | 131 |  | 
|---|
 | 132 |         if( mouseUsed == false ) | 
|---|
 | 133 |                 ret.insert(std::make_pair(OISMouse, mInputSystemName)); | 
|---|
 | 134 |  | 
|---|
 | 135 |         return ret; | 
|---|
 | 136 | } | 
|---|
 | 137 |  | 
|---|
 | 138 | //--------------------------------------------------------------------------------// | 
|---|
 | 139 | int MacInputManager::totalDevices(Type iType) | 
|---|
 | 140 | { | 
|---|
 | 141 |         switch(iType) | 
|---|
 | 142 |         { | 
|---|
 | 143 |         case OISKeyboard: return 1; | 
|---|
 | 144 |         case OISMouse: return 1; | 
|---|
 | 145 |         default: return 0; | 
|---|
 | 146 |         } | 
|---|
 | 147 | } | 
|---|
 | 148 |  | 
|---|
 | 149 | //--------------------------------------------------------------------------------// | 
|---|
 | 150 | int MacInputManager::freeDevices(Type iType) | 
|---|
 | 151 | { | 
|---|
 | 152 |         switch(iType) | 
|---|
 | 153 |         { | 
|---|
 | 154 |         case OISKeyboard: return keyboardUsed ? 0 : 1; | 
|---|
 | 155 |         case OISMouse: return mouseUsed ? 0 : 1; | 
|---|
 | 156 |         default: return 0; | 
|---|
 | 157 |         } | 
|---|
 | 158 | } | 
|---|
 | 159 |  | 
|---|
 | 160 | //--------------------------------------------------------------------------------// | 
|---|
 | 161 | bool MacInputManager::vendorExist(Type iType, const std::string & vendor) | 
|---|
 | 162 | { | 
|---|
 | 163 |         if( (iType == OISKeyboard || iType == OISMouse) && vendor == mInputSystemName ) | 
|---|
 | 164 |                 return true; | 
|---|
 | 165 |  | 
|---|
 | 166 |         return false; | 
|---|
 | 167 | } | 
|---|
 | 168 |  | 
|---|
 | 169 | //--------------------------------------------------------------------------------// | 
|---|
 | 170 | Object* MacInputManager::createObject(InputManager* creator, Type iType, bool bufferMode,  | 
|---|
 | 171 |                                                                           const std::string & vendor) | 
|---|
 | 172 | { | 
|---|
 | 173 |         Object *obj = 0; | 
|---|
 | 174 |  | 
|---|
 | 175 |         switch(iType) | 
|---|
 | 176 |         { | 
|---|
 | 177 |         case OISKeyboard:  | 
|---|
 | 178 |         { | 
|---|
 | 179 |                 if( keyboardUsed == false ) | 
|---|
 | 180 |                         obj = new MacKeyboard(this, bufferMode, mUseRepeat); | 
|---|
 | 181 |                 break; | 
|---|
 | 182 |         } | 
|---|
 | 183 |         case OISMouse: | 
|---|
 | 184 |         { | 
|---|
 | 185 |                 if( mouseUsed == false ) | 
|---|
 | 186 |                         obj = new MacMouse(this, bufferMode); | 
|---|
 | 187 |                 break; | 
|---|
 | 188 |         } | 
|---|
 | 189 |         default: | 
|---|
 | 190 |                 break; | 
|---|
 | 191 |         } | 
|---|
 | 192 |  | 
|---|
 | 193 |         if( obj == 0 ) | 
|---|
 | 194 |                 OIS_EXCEPT(E_InputDeviceNonExistant, "No devices match requested type."); | 
|---|
 | 195 |  | 
|---|
 | 196 |         return obj; | 
|---|
 | 197 | } | 
|---|
 | 198 |  | 
|---|
 | 199 | //--------------------------------------------------------------------------------// | 
|---|
 | 200 | void MacInputManager::destroyObject(Object* obj) | 
|---|
 | 201 | { | 
|---|
 | 202 |         delete obj; | 
|---|
 | 203 | } | 
|---|