Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

inputsystemsdl.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2002,2003 Koblenz University
00006    Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: inputsystemsdl.cpp,v 1.7 2004/12/06 08:44:08 rollmark Exp $
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; version 2 of the License.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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     // here we check whether SDL has been initialized prior to adding this
00080     // input is part of the video subsystem (because of event loops, etc..)
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     // we need a mutex object
00090     mMutex = SDL_CreateMutex();
00091 
00092     // setup the filtering function
00093     SDL_SetEventFilter(EventFilterCallback);
00094 
00095     return true;
00096 }
00097 
00098 bool InputSystemSDL::CreateDevice(const std::string &deviceName)
00099 {
00100     // first, we mangle the deviceName to avoid nameclashes
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     // initialize the device
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     // some special case handling for the timer (FIXME)
00124     if (mangledName.compare("TimerSDL") == 0)
00125         {
00126             mTimer = shared_static_cast<TimerSDL>(device);
00127         }
00128     else
00129         {
00130             SDL_LockMutex(mMutex);
00131             // try to link the device into the inputserver
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     // loop through all children
00151     for (TLeafList::iterator i = mChildren.begin(); i!=mChildren.end(); ++i)
00152         {
00153             shared_ptr<InputDeviceSDL> device = shared_static_cast<InputDeviceSDL>(*i);
00154             // every device gets a chance to filter the event, the first one
00155             // who claims it, gets it
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 }

Generated on Thu Apr 6 15:25:38 2006 for rcssserver3d by  doxygen 1.4.4