00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "inputsystemsdl.h"
00024 #include "inputdevicesdl.h"
00025 #include <kerosin/inputserver/inputserver.h>
00026 #include <zeitgeist/logserver/logserver.h>
00027 #include <SDL/SDL_thread.h>
00028 #include "timersdl.h"
00029
00030 using namespace boost;
00031 using namespace kerosin;
00032 using namespace zeitgeist;
00033
00046 InputSystemSDL *gInputSystem;
00047
00048 static int EventFilterCallback(const SDL_Event *event)
00049 {
00050 if (gInputSystem)
00051 {
00052 return gInputSystem->EventFilter(event);
00053 } else
00054 {
00055 return 1;
00056 }
00057 }
00058
00059 InputSystemSDL::InputSystemSDL() : InputSystem(), mMutex(NULL)
00060 {
00061 gInputSystem = this;
00062 }
00063
00064 InputSystemSDL::~InputSystemSDL()
00065 {
00066 SDL_LockMutex(mMutex);
00067 SDL_SetEventFilter(NULL);
00068 gInputSystem = NULL;
00069 SDL_UnlockMutex(mMutex);
00070
00071 SDL_DestroyMutex(mMutex);
00072 mMutex = NULL;
00073 }
00074
00075 bool InputSystemSDL::Init(InputServer *inputServer)
00076 {
00077 if (InputSystem::Init(inputServer) == false) return false;
00078
00079
00080
00081 if (!SDL_WasInit(SDL_INIT_VIDEO))
00082 {
00083 GetLog()->Error()
00084 << "ERROR: (InputSystemSDL) SDL not initialized!"
00085 << std::endl;
00086 return false;
00087 }
00088
00089
00090 mMutex = SDL_CreateMutex();
00091
00092
00093 SDL_SetEventFilter(EventFilterCallback);
00094
00095 return true;
00096 }
00097
00098 bool InputSystemSDL::CreateDevice(const std::string &deviceName)
00099 {
00100
00101 std::string mangledName = deviceName + "SDL";
00102
00103 shared_ptr<InputDevice> device =
00104 shared_static_cast<InputDevice>(GetCore()->New(mangledName));
00105
00106 if (device.get() == NULL)
00107 {
00108 GetLog()->Error()
00109 << "ERROR: (InputSystemSDL) Creating device '"
00110 << mangledName << "'" << std::endl;
00111 return false;
00112 }
00113
00114
00115 if (device->Init(this) == false)
00116 {
00117 GetLog()->Error()
00118 << "ERROR: (InputSystemSDL) Initializing device '"
00119 << mangledName << "'" << std::endl;
00120 return false;
00121 }
00122
00123
00124 if (mangledName.compare("TimerSDL") == 0)
00125 {
00126 mTimer = shared_static_cast<TimerSDL>(device);
00127 }
00128 else
00129 {
00130 SDL_LockMutex(mMutex);
00131
00132 if (AddChildReference(device) == false)
00133 {
00134 GetLog()->Error()
00135 << "ERROR: (InputSystemSDL) Linking device '"
00136 << mangledName << "'" << std::endl;
00137 SDL_UnlockMutex(mMutex);
00138 return false;
00139 }
00140 SDL_UnlockMutex(mMutex);
00141 }
00142
00143 return true;
00144 }
00145
00146 int InputSystemSDL::EventFilter(const SDL_Event *event)
00147 {
00148 int ret = 1;
00149 SDL_LockMutex(mMutex);
00150
00151 for (TLeafList::iterator i = mChildren.begin(); i!=mChildren.end(); ++i)
00152 {
00153 shared_ptr<InputDeviceSDL> device = shared_static_cast<InputDeviceSDL>(*i);
00154
00155
00156 if (device->EventFilter(event) == 0)
00157 {
00158 ret = 0;
00159 break;
00160 }
00161 }
00162 SDL_UnlockMutex(mMutex);
00163
00164 return ret;
00165 }
00166
00167 void InputSystemSDL::AddInput(kerosin::InputServer::Input &input)
00168 {
00169 SDL_LockMutex(mMutex);
00170 InputSystem::AddInput(input);
00171 SDL_UnlockMutex(mMutex);
00172 }
00173
00174 bool InputSystemSDL::GetInput(kerosin::InputServer::Input &input)
00175 {
00176 SDL_LockMutex(mMutex);
00177 bool ret = InputSystem::GetInput(input);
00178 SDL_UnlockMutex(mMutex);
00179
00180 return ret;
00181 }
00182
00183 bool InputSystemSDL::UpdateTimerInput(InputServer::Input &input)
00184 {
00185 if (mTimer.get() == NULL)
00186 {
00187 return false;
00188 } else
00189 {
00190 mTimer->GetInput(input);
00191 return true;
00192 }
00193 }