00001 // ===================================================================================== 00002 // Filename: System.h 00003 // Description: 00004 // Version: 1.0 00005 // Created: 03/29/2007 06:17:17 PM EDT 00006 // Revision: none 00007 // Compiler: gcc 3.4.6 00008 // 00009 // Author: John P. Feltz 00010 // Email: jfeltz@gmail.com 00011 // License: Copyright (c) 2006-2007, John P. Feltz 00012 // This program is free software; you can redistribute it and/or 00013 // modify it under the terms of the GNU General Public License as 00014 // published by the Free Software Foundation, version 2 of the 00015 // License. 00016 // This program is distributed in the hope that it will be 00017 // useful, but WITHOUT ANY WARRANTY; without even the implied 00018 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00019 // PURPOSE. 00020 // See the GNU General Public License version 2 for more details. 00021 // 00022 // ===================================================================================== 00023 00024 #include <iostream> 00025 #include <deque> 00026 #include <list> 00027 #include <map> 00028 #include <vector> 00029 #include <sstream> 00030 00031 #include <util/Singleton.h> 00032 #include <util/Logger.h> 00033 00034 #include <InputAction.h> 00035 #include <InputData.h> 00036 #include <InputEv.h> 00037 #include <InputActor.h> 00038 #include <InputDevice.h> 00039 #include <InputArbitrator.h> 00040 #include <InputTranslator.h> 00041 00042 00043 namespace cmil { 00044 class InputArbitrator; 00045 00046 class TranslatorRegistry : public Logger { 00047 public: 00049 typedef std::map<const std::string, InputTranslator* const > Translators; 00050 00051 virtual ~TranslatorRegistry() { 00052 log_debug("calling TranslatorRegistry destructor"); 00053 00054 for(Translators::iterator i = m_index.begin() ; i!= m_index.end() ; ++i) 00055 { 00056 log_debug("dtor deleting translator"); 00057 delete i->second; 00058 } 00059 } 00060 00061 std::string getName() const { 00062 return "TranslatorRegistry"; 00063 }; 00064 00065 InputTranslator* const getFirstTranslator(); 00066 InputTranslator* const getTranslator(const std::string& name ); 00067 00068 virtual bool addTranslator(InputTranslator* translator); 00069 virtual void poll(); 00070 00071 private: 00072 Translators m_index; 00073 }; 00074 00075 class SystemClient; 00076 00079 00083 00086 00087 class System : public Logger { 00088 public: 00089 System(InputArbitrator* const arb); 00090 System(); 00091 00092 virtual ~System() { 00093 log_debug("dtor"); 00094 00095 if(m_arbitrator) { 00096 log_debug("deleting arbitrator"); 00097 delete m_arbitrator; 00098 } 00099 } 00100 00101 virtual void attach(InputActor* actor); 00102 00103 virtual std::string getName() const { 00104 return "System"; 00105 } 00106 00108 virtual void poll(); 00109 00110 protected: 00111 friend class SystemClient; 00112 00113 virtual const bool isActionCompatible(int id, InputAction* action) const; 00114 virtual const int getID(const Ev& event) const; 00115 virtual InputData* const getData(int id) const; 00116 virtual InputData* const getData(const Ev& event) const; 00117 00118 00119 virtual int getSize() const; 00120 00121 virtual void quit(); 00122 virtual void registerDevices(DeviceRegistry& dev); 00123 virtual void update(const InputData& input); 00124 virtual void update(const ModData& input); 00125 00126 TranslatorRegistry m_translators; 00127 00128 private: 00129 InputArbitrator* const m_arbitrator; 00130 }; 00131 00132 class SystemClient { 00133 public: 00134 SystemClient(System* const sys) : m_system(sys) { 00135 if(!sys) 00136 std::cout << "ERROR: null system passed, this will produce undefined behavior" << std::endl; 00137 }; 00138 00139 const bool isActionCompatible(int id, InputAction* action) const { 00140 return m_system->isActionCompatible(id, action); 00141 }; 00142 const int getID(const Ev& event) const { 00143 return m_system->getID(event); 00144 }; 00145 InputData* const getData(int id) const { 00146 return m_system->getData(id); 00147 }; 00148 InputData* const getData(const Ev& event) const { 00149 return m_system->getData(event); 00150 }; 00151 00152 std::string getName() const { 00153 return "SystemClient"; 00154 }; 00155 00156 int getSize() const { 00157 return m_system->getSize(); 00158 }; 00159 00160 System* getSystem() const { 00161 return m_system; 00162 }; 00163 00164 void quit() { 00165 m_system->quit(); 00166 }; 00167 00168 void registerDevices(DeviceRegistry& dev) { 00169 m_system->registerDevices(dev); 00170 } 00171 00172 void update(const InputData& input) { 00173 m_system->update(input); 00174 }; 00175 00176 void update(const ModData& input) { 00177 m_system->update(input); 00178 }; 00179 00180 private: 00181 System* const m_system; 00182 }; 00183 }