/home/jpf/code/cc/project/cmil/include/SDLTranslator.h

Go to the documentation of this file.
00001 // =====================================================================================
00002 //       Filename:  SDLTranslator.h 
00003 //    Description:  
00004 //        Created:  02/15/2007 08:47:46 PM EST
00005 //       Revision:  none
00006 //       Compiler:  gcc 3.4.6
00007 //         Author:  John P. Feltz 
00008 //          Email:  jfeltz@gmail.com
00009 //        License:  Copyright (c) 2006-2007, John P. Feltz 
00010 //                  This program is free software; you can redistribute it and/or
00011 //                  modify it under the terms of the GNU General Public License as
00012 //                  published by the Free Software Foundation, version 2 of the
00013 //                  License.
00014 //                  This program is distributed in the hope that it will be
00015 //                  useful, but WITHOUT ANY WARRANTY; without even the implied
00016 //                  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00017 //                  PURPOSE.
00018 //                  See the GNU General Public License version 2 for more details.
00019 // 
00020 // =====================================================================================
00021 
00022 
00023 namespace cmil {
00024   namespace SDL {
00025     class Relay : public SystemClient, 
00026                    public Logger {
00027       public:
00028         Relay(System* const sys);
00029         virtual unsigned long int getTimeRef() const = 0; 
00030 
00032         void UpdateSystem(ModData& input);
00033         void UpdateSystem(InputData& input);
00034 
00035         void quit(); 
00036         virtual std::string getName() const;
00037      };
00038 
00041 
00042     class DefaultMouse : public Mouse {
00043       public:
00044         DefaultMouse(const std::string& name ); 
00045         DefaultMouse(); 
00046         virtual ~DefaultMouse() {}
00047         
00048         bool operator==(const Mouse& mouse);
00049       private:
00050        void init(); 
00051     };
00052   
00053    class DefaultKeyboard : public Keyboard {
00054      public:
00055        DefaultKeyboard() : Keyboard("Keyboard") {
00057         removeInput("Percent");            
00058        }
00059 
00060        virtual ~DefaultKeyboard() {}
00061    }; 
00062 
00063    class DeviceTranslator : public Relay {
00064      public:
00065        DeviceTranslator(System* const sys) : Relay(sys) {}  
00066        virtual ~DeviceTranslator() {};
00067 
00068        virtual std::string getName() const = 0;
00069 
00071        virtual bool translate(const SDL_Event& input_event) = 0;
00072        
00076        virtual DeviceRegistry& getDevices() = 0;
00077 
00078        virtual unsigned long int getTimeRef() const = 0;
00079 
00080        virtual bool addCustomDevice(InputDevice* device) = 0; 
00081        
00083        virtual bool operator==(const DeviceTranslator& rhs) const {
00084         log_debug("calling operator==");
00085         return (getName() == rhs.getName());
00086        }  
00087    }; 
00088 
00090     template <typename DeviceBase> class tDeviceTranslator :  public DeviceTranslator {
00091       public:
00092         tDeviceTranslator(System* const sys) : DeviceTranslator(sys) {}; 
00094         virtual bool translate(const SDL_Event& input_event) = 0;
00095 
00096         bool addCustomDevice(InputDevice* device) {
00097           DeviceBase* tmp_derived_ptr;
00098 
00099           if(!(tmp_derived_ptr= dynamic_cast<DeviceBase*>(device))) {
00100             log_error("addCustomDevice() not adding custom device due to cast check failure.");
00101             device->cleanup();
00102             delete device;
00103           }
00104           else {
00105             for(typename std::list<DeviceBase*>::iterator i = m_custom_devices.begin(); 
00106                 i != m_custom_devices.end() ; ++i)
00107               if(*(*i) == *tmp_derived_ptr) {
00108                 log_error("addCustomDevice() tried to add pre-existing controller type"); 
00109                 return false;
00110               }
00111 
00112             log_debug(std::string("addCustomDevice() adding device type with name: ").append(device->getName())); 
00113             m_custom_devices.push_back( tmp_derived_ptr);
00114             return true;
00115           }
00116 
00117           return false;
00118         } 
00119         
00120         virtual DeviceRegistry& getDevices() = 0;
00121 
00122         virtual std::string getName() const {
00123           return "tDeviceTranslator";
00124         }; 
00125 
00126         unsigned long int getTimeRef() const {
00127           return (unsigned long)SDL_GetTicks(); 
00128         } 
00129         
00130       protected:
00132         friend class SDLTranslator;
00133 
00134         virtual ~tDeviceTranslator() {
00135           for(typename std::list<DeviceBase*>::iterator i = m_custom_devices.begin(); i != m_custom_devices.end() ; ++i) {
00136             if(*i) { (*i)->cleanup(); delete (*i); }
00137           }
00138         }
00139 
00140         std::list<DeviceBase*> m_custom_devices;
00141     };
00142 
00145 
00146     class SDLKeyboards : public tDeviceTranslator<Keyboard> {
00147       public:
00148         SDLKeyboards(System* const sys) : 
00149           tDeviceTranslator<Keyboard>(sys), 
00150           m_keyboard(NULL), 
00151           m_keys(SDLK_LAST) {}
00152 
00153         virtual ~SDLKeyboards() { 
00154           log_debug("dtor");
00155           if(m_keyboard) {
00156             log_debug("deleting keyboard");
00157             delete m_keyboard; 
00158           }
00159         };
00160 
00162         void setKey(SDLKey key, InputData* const press);
00163 
00165         Press* getData(SDLKey key) const { return m_keys.getData(key); }
00166 
00167         std::string getName() const { return "SDLKeyboards"; }
00168 
00169         bool translate(const SDL_Event& input_event);
00170 
00171         DeviceRegistry& getDevices();
00172 
00173       private:
00174         void assign_inputs();
00175 
00176         InputDevice* m_keyboard;
00177         DeviceRegistry m_registry; 
00178 
00180         tInputIndex<Press> m_keys;
00181     };
00182 
00183     class SDLMice: public tDeviceTranslator<Mouse> {  
00184       public:
00185         SDLMice(System* const sys);
00186 
00187         virtual ~SDLMice() {
00188           if(m_mouse) {
00189             delete m_mouse;
00190           }
00191         };
00192 
00193         Coord* getPositioner() const { return m_positioner; }
00194         void setPositioner( InputData* const coord);
00195 
00196         DeviceRegistry& getDevices(); 
00197 
00198         std::string getName() const {
00199           return "SDLMice";
00200         }
00201 
00202         bool translate(const SDL_Event& input_event);
00203 
00204       private:
00206         void assign_inputs();
00207 
00208         Mouse* m_mouse;
00209         DeviceRegistry m_registry; 
00210         tInputIndex<Press> m_buttons; 
00211 
00213         Coord* m_positioner;
00214     };
00215 
00216     class SDLControllers : public tDeviceTranslator<GameController> { 
00217       public:
00218         virtual ~SDLControllers() {
00219           for(std::vector<Device* >::iterator i = m_sdljoysticks.begin(); i != m_sdljoysticks.end(); ++i)
00220             if(*i) delete (*i);
00221         } 
00222 
00223         SDLControllers(System* const sys) : tDeviceTranslator<GameController>(sys) {}
00224 
00226         class Device {
00227           public:
00228             Device( const GameController& joy, SDL_Joystick* opened ); 
00229 
00230             virtual ~Device() {
00231               if(SDL_WasInit(SDL_INIT_JOYSTICK)) 
00232                 if(m_opened_sdl_joy)
00233                   SDL_JoystickClose(m_opened_sdl_joy);
00234             }
00235 
00236             Axis* getAxis(unsigned int id) const {
00237               return m_axes.getData(id); 
00238             }
00239 
00240             Hat* getHat(unsigned int id) const {
00241               return m_hats.getData(id); 
00242             }
00243 
00244             Press* getButton(unsigned int id) const {
00245               return m_buttons.getData(id); 
00246             }
00247 
00248             Motion* getBall(unsigned int id) const {
00249               return m_balls.getData(id); 
00250             }
00251 
00252             std::string getName() const { return "SDLControllers::Device"; }
00253 
00254           private:
00255             tInputIndex<Axis>   m_axes;
00256             tInputIndex<Hat>    m_hats;
00257             tInputIndex<Press>  m_buttons;
00258             tInputIndex<Motion> m_balls;
00259 
00260             SDL_Joystick* m_opened_sdl_joy;
00261         };
00262 
00263         Axis*   getAxis(unsigned int which, unsigned int id) const; 
00264         Hat*    getHat(unsigned int which, unsigned int id) const;
00265         Press*  getButton(unsigned int which, unsigned int id) const;
00266         Motion* getBall(unsigned int which, unsigned int id) const; 
00267 
00268         bool addController(const GameController* controller);
00269 
00270         DeviceRegistry& getDevices();
00271         std::string getName() const { return "SDLControllers"; }
00272 
00273         bool translate(const SDL_Event& input_event);
00274 
00275       private:
00276         bool translate(const SDL_JoyAxisEvent& jaxis);
00277         bool translate(const SDL_JoyBallEvent& jball);
00278         bool translate(const SDL_JoyHatEvent& jhat);
00279         bool translate(const SDL_JoyButtonEvent& jbutton, bool movement);
00280 
00283         std::vector<Device* > m_sdljoysticks;
00284         DeviceRegistry m_registry;
00285     };
00286 
00287     
00289 
00290     class SDLTranslator : public InputTranslator, SystemClient, Logger {
00291       public:
00292 
00293         SDLTranslator(System* const sys) : SystemClient(sys) {} 
00294 
00295         typedef std::list<DeviceTranslator*> DeviceTranslators;
00296 
00297         virtual bool addTranslated(DeviceTranslator* sdl_device);
00298         
00300         //TODO protect against duplicate calling
00301         DeviceRegistry& getDevices();
00302 
00303         virtual void poll();
00304         virtual void translate(const SDL_Event& input_event);
00305 
00306         virtual ~SDLTranslator() {
00307           log_debug("dtor");
00308           for(DeviceTranslators::iterator i = m_sdltranslators.begin(); i != m_sdltranslators.end() ; ++i)
00309             if(*i) { 
00310               log_debug("dtor : deleting device translator");
00311               delete (*i); 
00312             }
00313         }
00314         
00315 
00316         std::string getName() const { return "SDLTranslator"; }
00317         
00318       private:
00319 
00321         DeviceRegistry m_devices;
00322           
00324         DeviceTranslators m_sdltranslators;
00325         
00326         SDL_Event m_event;
00327     };
00328 
00329     enum Supported {
00330       Keyboards,
00331       Mice,
00332       Controllers 
00333     }; 
00334 
00335     class SupportedFactory: public Logger {
00336       public:
00337         std::string getName() const { return "DeviceTranslatorFactory"; }
00338         static DeviceTranslator* const create(const Supported type, System* const sys);
00339     };
00340 
00341   }
00342 }

(c) 2006-2007 John P. Feltz
Generated on Wed Jul 25 16:08:02 2007 for Common Media Input Layer by doxygen 1.4.7

SourceForge.net Logo