/home/jpf/code/cc/project/cmil/src/SDLTranslator.cpp

Go to the documentation of this file.
00001 // =====================================================================================
00002 //       Filename:  SDLTranslator.cpp 
00003 //    Description:  
00004 //        Created:  02/15/2007 08:47:46 PM EST
00005 //       Revision:  none
00006 //       Compiler:  gcc 3.4.6
00007 // 
00008 //         Author:  John P. Feltz 
00009 //          Email:  jfeltz@gmail.com
00010 //        License:  Copyright (c) 2006-2007, John P. Feltz 
00011 //                  This program is free software; you can redistribute it and/or
00012 //                  modify it under the terms of the GNU General Public License as
00013 //                  published by the Free Software Foundation, version 2 of the
00014 //                  License.
00015 //                  This program is distributed in the hope that it will be
00016 //                  useful, but WITHOUT ANY WARRANTY; without even the implied
00017 //                  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00018 //                  PURPOSE.
00019 //                  See the GNU General Public License version 2 for more details.
00020 // 
00021 // =====================================================================================
00022 
00023 
00024 #include <System.h>
00025 
00026 #include <SDL/SDL.h>
00027 #include <SDLTranslator.h>
00028 
00029 namespace cmil {
00030   namespace SDL {
00031     Relay::Relay(System* const sys) : SystemClient(sys) {};
00032 
00033     void Relay::UpdateSystem(ModData& input) {
00034         input.setTime( getTimeRef() );
00035         update(input); 
00036     };
00037 
00038     void Relay::UpdateSystem(InputData& input) {
00039         input.setTime( getTimeRef() );
00040         update(input); 
00041     };
00042 
00043     void Relay::quit() {
00044       SystemClient::quit(); 
00045     };
00046 
00047     std::string Relay::getName() const { return "InputRelay"; };
00048 
00049 
00051     DefaultMouse::DefaultMouse(const std::string& name ) : Mouse(name, 256, 1) { init(); }; 
00052     DefaultMouse::DefaultMouse() : Mouse("Mouse", 256, 1) { init(); }; 
00053 
00054      void DefaultMouse::init() {
00056         setInput( "button1", "LeftButton"  );
00057         setInput( "button2", "MiddleButton");
00058         setInput( "button3", "RightButton" );
00059         setInput( "button4", "WheelUp"     );
00060         setInput( "button5", "WheelDown"   );
00061 
00062         setInput( "coord0", "Motion"   );
00063      }
00064   
00068     bool DefaultMouse::operator==(const Mouse& mouse) { 
00069       log_debug("(DefaultMouse) operator== Mouse& ");
00070 
00071       if(!(m_buttons == mouse.m_buttons) && (m_coords == mouse.m_coords))
00072         return false;
00073 
00074       if( m_inputs.isEqual( false, mouse.getInputs()) ) {
00075         log_debug("operator== returning true");
00076         return true;
00077       }
00078       return false;
00079     }
00080 
00081     void SDLKeyboards::setKey(SDLKey key, InputData* const input) {
00082       if(input) {
00083         if( !(m_keys.setData((unsigned)key, input)) )
00084           log_error("setKey( Key ) was unable to assign the input due to improperly setup key index");
00085       }
00086       else
00087         log_error("setKey( Key ) was unable to assign the input due to NULL parameter ");
00088     }
00089     
00090     void SDLKeyboards::assign_inputs() {
00091       if(m_keyboard) {
00093           setKey(SDLK_F1, m_keyboard->getData("F1"));  
00094           setKey(SDLK_F2, m_keyboard->getData("F2"));
00095           setKey(SDLK_F3, m_keyboard->getData("F3"));  
00096           setKey(SDLK_F4, m_keyboard->getData("F4"));
00097           setKey(SDLK_F5, m_keyboard->getData("F5"));  
00098           setKey(SDLK_F6, m_keyboard->getData("F6")); 
00099           setKey(SDLK_F7, m_keyboard->getData("F7"));  
00100           setKey(SDLK_F8, m_keyboard->getData("F8")); 
00101           setKey(SDLK_F9, m_keyboard->getData("F9"));  
00102           setKey(SDLK_F10, m_keyboard->getData("F10")); 
00103           setKey(SDLK_F11, m_keyboard->getData("F11")); 
00104           setKey(SDLK_F12, m_keyboard->getData("F12")); 
00105           setKey(SDLK_PRINT, m_keyboard->getData("Print"));
00106 
00108           setKey(SDLK_SYSREQ, m_keyboard->getData( "Print"));
00109 
00110           setKey(SDLK_SCROLLOCK, m_keyboard->getData("Scroll Lock"));
00111           setKey(SDLK_PAUSE, m_keyboard->getData("Pause"));
00112 
00114           setKey(SDLK_1, m_keyboard->getData("1"));        
00115           setKey(SDLK_EXCLAIM, m_keyboard->getData("Exclaimation Mark"));  
00116 
00117           setKey(SDLK_2, m_keyboard->getData("2"));
00118           setKey(SDLK_AT, m_keyboard->getData("@"));                  
00119 
00120           setKey(SDLK_3, m_keyboard->getData("3")); 
00121           setKey(SDLK_HASH, m_keyboard->getData("Hash"));               
00122 
00123           setKey(SDLK_4, m_keyboard->getData("4"));        
00124           setKey(SDLK_DOLLAR, m_keyboard->getData("Dollar Sign"));        
00125 
00126           setKey(SDLK_5, m_keyboard->getData("5"));  
00127 
00128           setKey(SDLK_6, m_keyboard->getData("6"));   
00129           setKey(SDLK_CARET, m_keyboard->getData("Caret"));              
00130 
00131           setKey(SDLK_7, m_keyboard->getData("7"));    
00132           setKey(SDLK_AMPERSAND, m_keyboard->getData("Ampersand"));          
00133 
00134           setKey(SDLK_8, m_keyboard->getData("8"));    
00135           setKey(SDLK_ASTERISK, m_keyboard->getData("Asterisk"));           
00136 
00137           setKey(SDLK_9, m_keyboard->getData("9"));   
00138           setKey(SDLK_LEFTPAREN, m_keyboard->getData("Left Parenthesis"));   
00139 
00140           setKey(SDLK_0, m_keyboard->getData("0"));    
00141           setKey(SDLK_RIGHTPAREN, m_keyboard->getData("Right Parenthesis"));  
00142 
00143           setKey(SDLK_MINUS, m_keyboard->getData("Minus"));   
00144           setKey(SDLK_UNDERSCORE, m_keyboard->getData("Underscore")); 
00145 
00146           setKey(SDLK_PLUS, m_keyboard->getData("Plus"));  
00147           setKey(SDLK_EQUALS, m_keyboard->getData("Equals")); 
00148 
00149           setKey(SDLK_BACKSPACE, m_keyboard->getData("Backspace"));  
00150 
00151           setKey(SDLK_q, m_keyboard->getData("Q")); 
00152           setKey(SDLK_w, m_keyboard->getData("W"));
00153           setKey(SDLK_e, m_keyboard->getData("E"));
00154           setKey(SDLK_r, m_keyboard->getData("R")); 
00155           setKey(SDLK_t, m_keyboard->getData("T")); 
00156           setKey(SDLK_y, m_keyboard->getData("Y"));
00157           setKey(SDLK_u, m_keyboard->getData("U"));
00158           setKey(SDLK_i, m_keyboard->getData("I"));
00159           setKey(SDLK_o, m_keyboard->getData("O"));
00160           setKey(SDLK_p, m_keyboard->getData("P")); 
00161           setKey(SDLK_a, m_keyboard->getData("A")); 
00162           setKey(SDLK_s, m_keyboard->getData("S"));
00163           setKey(SDLK_d, m_keyboard->getData("D")); 
00164           setKey(SDLK_f, m_keyboard->getData("F")); 
00165           setKey(SDLK_g, m_keyboard->getData("G"));
00166           setKey(SDLK_h, m_keyboard->getData("H"));
00167           setKey(SDLK_j, m_keyboard->getData("J"));
00168           setKey(SDLK_k, m_keyboard->getData("K"));
00169           setKey(SDLK_l, m_keyboard->getData("L"));
00170           setKey(SDLK_z, m_keyboard->getData("Z"));
00171           setKey(SDLK_x, m_keyboard->getData("X"));
00172           setKey(SDLK_c, m_keyboard->getData("C"));
00173           setKey(SDLK_v, m_keyboard->getData("V"));
00174           setKey(SDLK_b, m_keyboard->getData("B"));
00175           setKey(SDLK_n, m_keyboard->getData("N"));
00176           setKey(SDLK_m, m_keyboard->getData("M"));
00177      
00179           setKey(SDLK_TAB, m_keyboard->getData("Tab")); 
00180           setKey(SDLK_CAPSLOCK, m_keyboard->getData("Capslock")); 
00181 
00182           setKey(SDLK_LESS, m_keyboard->getData("Less Than"));    
00183           setKey(SDLK_COMMA, m_keyboard->getData("Comma")); 
00184           setKey(SDLK_GREATER, m_keyboard->getData("Greater Than")); 
00185           setKey(SDLK_PERIOD, m_keyboard->getData("Period")); 
00186 
00189           
00192 
00193           setKey(SDLK_LSHIFT, m_keyboard->getData("Shift")); 
00194           setKey(SDLK_RSHIFT, m_keyboard->getData("Shift")); 
00195 
00198 
00199           setKey(SDLK_LCTRL, m_keyboard->getData("Ctrl")); 
00200           setKey(SDLK_RCTRL, m_keyboard->getData("Ctrl")); 
00201 
00204 
00205           setKey(SDLK_LALT, m_keyboard->getData("Alt")); 
00206           setKey(SDLK_RALT, m_keyboard->getData("Alt")); 
00207 
00208           setKey(SDLK_RETURN, m_keyboard->getData("Return")); 
00209           setKey(SDLK_SPACE, m_keyboard->getData("Space")); 
00210 
00211           setKey(SDLK_INSERT, m_keyboard->getData("Insert"));
00212           setKey(SDLK_HOME, m_keyboard->getData("Home"));
00213           setKey(SDLK_PAGEUP, m_keyboard->getData("Page Up"));
00214           setKey(SDLK_DELETE, m_keyboard->getData("Delete"));
00215           setKey(SDLK_END, m_keyboard->getData("End"));
00216           setKey(SDLK_PAGEDOWN, m_keyboard->getData("Page Down"));
00217 
00218           setKey(SDLK_LEFT, m_keyboard->getData("Left")); 
00219           setKey(SDLK_RIGHT, m_keyboard->getData("Right")); 
00220           setKey(SDLK_DOWN, m_keyboard->getData("Down")); 
00221           setKey(SDLK_UP, m_keyboard->getData("Up")); 
00222 
00223           setKey(SDLK_NUMLOCK, m_keyboard->getData("NumLock"));
00224           setKey(SDLK_KP_DIVIDE, m_keyboard->getData("Keypad Forward Slash"));
00225           setKey(SDLK_KP_MULTIPLY, m_keyboard->getData("Keypad Asterisk")); 
00226           setKey(SDLK_KP_MINUS, m_keyboard->getData("Keypad Minus ")); 
00227           setKey(SDLK_KP_PLUS, m_keyboard->getData("Keypad Plus")); 
00228           setKey(SDLK_KP_ENTER, m_keyboard->getData("Keypad Enter")); 
00229 
00230           setKey(SDLK_KP0, m_keyboard->getData("Keypad Zero"));
00231      
00232           setKey(SDLK_KP_PERIOD, m_keyboard->getData("Keypad Period"));
00233 
00234           setKey(SDLK_KP1, m_keyboard->getData("Keypad 1"));
00235           setKey(SDLK_KP2, m_keyboard->getData("Keypad 2"));
00236           setKey(SDLK_KP3, m_keyboard->getData("Keypad 3"));
00237           setKey(SDLK_KP4, m_keyboard->getData("Keypad 4"));
00238           setKey(SDLK_KP5, m_keyboard->getData("Keypad 5"));
00239           setKey(SDLK_KP6, m_keyboard->getData("Keypad 6"));
00240           setKey(SDLK_KP7, m_keyboard->getData("Keypad 7"));
00241           setKey(SDLK_KP8, m_keyboard->getData("Keypad 8"));
00242           setKey(SDLK_KP9, m_keyboard->getData("Keypad 9"));
00243 
00246           setKey(SDLK_SLASH, m_keyboard->getData("Foward Slash"));    
00247           setKey(SDLK_QUESTION, m_keyboard->getData("Question Mark"));     
00248 
00250           setKey(SDLK_BACKSLASH, m_keyboard->getData("Back Slash")); 
00251           setKey(SDLK_RIGHTBRACKET, m_keyboard->getData("Right Bracket")); 
00252           setKey(SDLK_LEFTBRACKET, m_keyboard->getData("Left Bracket")); 
00253 
00255           setKey(SDLK_BACKQUOTE, m_keyboard->getData("Backquote")); 
00256           setKey(SDLK_COLON, m_keyboard->getData("Colon"));    
00257           setKey(SDLK_SEMICOLON, m_keyboard->getData("Semi Colon")); 
00258           setKey(SDLK_QUOTEDBL, m_keyboard->getData("Double Quote"));
00259           setKey(SDLK_QUOTE, m_keyboard->getData("Quote"));
00260 
00261           setKey(SDLK_ESCAPE, m_keyboard->getData("Escape"));
00262         }
00263         else
00264           log_error("assign_inputs m_keyboard device is NULL, unable to provide event translation");
00265     }
00266 
00267     DeviceRegistry& SDLKeyboards::getDevices() {
00270 
00271       if(m_registry.getSize())  
00272         m_registry.clear();
00273 
00274       log_debug("getDevices()");
00275       std::list<Keyboard*>::const_iterator i; 
00276       InputDevice* clone = NULL;
00277 
00278       DefaultKeyboard default_keyboard;
00279 
00280       for(i = m_custom_devices.begin() ; i != m_custom_devices.end(); ++i) {
00281         log_debug ("getDevices() loop iterating...");
00282 
00284         if(*(*i) == default_keyboard) {
00285           clone = ((*i)->clone());
00286 
00287           if(!(m_keyboard = dynamic_cast<Keyboard*>(clone))) {
00288             log_error("getDevices() casting failure of cloned InputDevice");
00289             delete clone;
00290           }
00291           else {
00292             continue;
00293           }
00294         }
00295       }
00296 
00297       if(!m_keyboard) {
00298         log_debug("getDevices() No valid custom device found, using SDL::DefaultKeyboard"); 
00299         m_keyboard = new Keyboard(default_keyboard);
00300       }
00301       else 
00302         default_keyboard.cleanup();
00303 
00304       assign_inputs();
00305     
00307       m_registry.registerDevice(*m_keyboard);
00308 
00309       return m_registry;
00310     }
00311 
00312     bool SDLKeyboards::translate(const SDL_Event& input_event) {
00313       if((input_event.type == SDL_KEYDOWN) ||  (input_event.type == SDL_KEYUP)) {
00314         Press* press = getData(input_event.key.keysym.sym);
00315 
00316         if(press) {
00317           if( input_event.key.state == SDL_PRESSED) {
00318             press->setState(pressed); 
00319           }
00320           else {
00321             press->setState(released); 
00322           }
00323 
00324           UpdateSystem(*press); 
00325 
00326           return true;
00327         }
00328         else 
00329           log_error("translate( ) unable set NULL press type ");
00330 
00331       }
00332       //log_debug("translate() returning false");
00333       return false;
00334     };
00335 
00337     SDLMice::SDLMice(System* const sys) : 
00338       tDeviceTranslator<Mouse>(sys),
00339       m_mouse(NULL),  
00340       m_positioner(NULL) {}
00341 
00342     void SDLMice::assign_inputs() {
00343       log_debug("assign_inputs()");
00344       
00345       std::ostringstream input_ss;
00346 
00347       for(unsigned int nbutton = 0; nbutton < m_mouse->m_buttons.getSize(); ++nbutton) {
00348         input_ss << "button" << nbutton;
00349         m_buttons.addData( m_mouse->getData(DeviceInputs::use_base, input_ss.str()) );
00350         input_ss.str("");
00351       }
00352 
00353       setPositioner(m_mouse->getData(DeviceInputs::use_base, "coord0"));
00354 
00355       log_debug("finishing assign_inputs()");
00356     }
00357 
00358     DeviceRegistry& SDLMice::getDevices() {
00359       if(m_registry.getSize())  
00360         m_registry.clear();
00361 
00362       log_debug("getDevices()");
00363       std::list<Mouse*>::const_iterator i; 
00364       InputDevice* clone = NULL;
00365 
00366       DefaultMouse comparison_mouse;
00367 
00368       for(i = m_custom_devices.begin() ; i != m_custom_devices.end(); ++i) {
00369         log_debug ("getDevices() loop iterating...");
00370 
00372         if(*(*i) == comparison_mouse) {
00373           log_debug("getDevices() cloning custom device");
00374           clone = ((*i)->clone());
00375 
00376           if(!(m_mouse = dynamic_cast<Mouse*>(clone))) {
00377             log_error("getDevices() casting failure of cloned InputDevice");
00378             clone->cleanup();
00379             delete clone;
00380           }
00381           else continue;
00382         }
00383         else
00384           log_error("getDevices() custom Mouse didn't match the default.");
00385       }
00386 
00387       if(!m_mouse) {
00388         log_debug("getDevices() No valid custom devices provided, using DefaultMouse"); 
00389         m_mouse = new Mouse(comparison_mouse);
00390       }
00391       else 
00392         comparison_mouse.cleanup();
00393 
00394       assign_inputs();
00395 
00396       m_registry.registerDevice(*m_mouse);
00397 
00398       return m_registry;
00399     }
00400 
00401     void  SDLMice::setPositioner( InputData* const input) {
00402       Coord* coord = NULL;
00403 
00404       if(input) {
00405         if( ( coord = dynamic_cast< Coord* >(input) ) != NULL) {
00406         m_positioner = coord;
00407         }
00408         else
00409           log_error(std::string("setPositioner() was unable to assign the input").append(input->getName()).append("due to cast type check failure.")); 
00410       }
00411       else
00412         log_error("setPositioner() was unable to set NULL Motion type");
00413     }
00414 
00415     bool SDLMice::translate(const SDL_Event& input_event) {
00416       Press* button = NULL;
00417       Coord* coord  = NULL;
00418 
00419       if(input_event.type == SDL_MOUSEMOTION) {
00420         coord = getPositioner();
00421         if(coord)
00422           coord->setCoord(input_event.motion.x, input_event.motion.y, input_event.motion.xrel, input_event.motion.yrel);
00423         else 
00424           return false;
00425 
00426         UpdateSystem(*coord);
00427         return true;
00428       }
00429       else if((input_event.type == SDL_MOUSEBUTTONDOWN) || (input_event.type == SDL_MOUSEBUTTONUP)) {
00430         button = m_buttons.getData(input_event.button.button);
00431 
00432         if(button) {
00433           if(input_event.type == SDL_MOUSEBUTTONDOWN)
00434             button->setState(pressed);
00435           else
00436             button->setState(released);
00437         }
00438         else {
00439           log_error("translate ( button)  unable to convert button due to no matching InputData available");
00440           return false;
00441         }
00442 
00443         UpdateSystem(*button);
00444         return true;
00445       }
00446 
00447       //log_debug("translate( mouse event ) event gone unhandled");
00448       return false;
00449     }
00450 
00452 
00453     SDLControllers::Device::Device( const GameController& joy, SDL_Joystick* opened ) :
00454      m_axes(joy.m_axes), 
00455      m_hats(joy.m_hats), 
00456      m_buttons(joy.m_buttons), 
00457      m_balls(joy.m_balls), 
00458      m_opened_sdl_joy(opened) 
00459     {};
00460 
00461     Axis* SDLControllers::getAxis(unsigned int which, unsigned int id) const {
00462       if(which <= m_sdljoysticks.size())
00463         return m_sdljoysticks[which]->getAxis(id); 
00464       return NULL;
00465     };
00466 
00467     Hat* SDLControllers::getHat(unsigned int which, unsigned int id) const {
00468       if(which <= m_sdljoysticks.size())
00469         return m_sdljoysticks[which]->getHat(id); 
00470       return NULL;
00471     };
00472 
00473     Press* SDLControllers::getButton(unsigned int which, unsigned int id) const {
00474       if(which <= m_sdljoysticks.size())
00475         return m_sdljoysticks[which]->getButton(id); 
00476       return NULL;
00477     };
00478 
00479     Motion* SDLControllers::getBall(unsigned int which, unsigned int id) const {
00480       if(which <= m_sdljoysticks.size())
00481         return m_sdljoysticks[which]->getBall(id); 
00482       return NULL;
00483     };
00484 
00490     
00491     DeviceRegistry& SDLControllers::getDevices() {
00492       SDL_Joystick* opened_sdl_joy = NULL; 
00493       GameController*  custom_device = NULL; 
00494       std::list<GameController*>::iterator i; 
00495 
00496       unsigned long int njoy; 
00497       unsigned long int axes, buttons, balls, hats;
00498 
00499       std::ostringstream joy_ss;
00500 
00501       if(m_registry.getSize())  
00502         m_registry.clear();
00503 
00504       if(SDL_WasInit(SDL_INIT_JOYSTICK)) {
00505 
00506         if( (njoy = (unsigned)SDL_NumJoysticks()) ) {
00507 
00508           for(unsigned int n = 0 ; n < njoy ; n++) {
00509 
00510             if( (opened_sdl_joy = SDL_JoystickOpen(n)) ) {
00512               joy_ss << "Joystick" << n;  
00513 
00514               log_debug(joy_ss.str());
00515 
00517               axes =    (unsigned)SDL_JoystickNumAxes(    opened_sdl_joy );
00518               buttons = (unsigned)SDL_JoystickNumButtons( opened_sdl_joy );
00519               balls =   (unsigned)SDL_JoystickNumBalls(   opened_sdl_joy );
00520               hats =    (unsigned)SDL_JoystickNumHats(    opened_sdl_joy );
00521 
00522               GameController comparison_joy(joy_ss.str(), axes, buttons, balls, hats);
00523 
00525               for(i = m_custom_devices.begin() ; i != m_custom_devices.end(); ++i) {
00526                 if(*(*i) == comparison_joy) {
00527                   InputDevice* clone = (*i)->clone();
00528                   if(!(custom_device = dynamic_cast<GameController*>(clone) )) {
00529                     log_error("getDevices() casting failure of cloned InputDevice");
00530                     delete clone;
00531                   }
00532                   else {
00534                     m_sdljoysticks.push_back( new Device(*custom_device, opened_sdl_joy) );
00535       
00537                     m_registry.registerDevice(*clone);
00538                     continue;
00539                   }
00540                 }
00541               }
00542 
00544               if(!custom_device) {
00545                   log_debug("getDevices() Adding generic joystick due to no usable custom ones found.");
00546                   m_sdljoysticks.push_back( new Device(comparison_joy, opened_sdl_joy) );
00547                   m_registry.registerDevice(comparison_joy);
00548               }
00549             }
00550             else
00551               log_error("getDevices() SDL provided a joystick with a null structure");
00552 
00554             custom_device = NULL;
00555           }
00556 
00557           joy_ss.str("");
00558         }
00559         else 
00560           log_error("getDevices() SDL error, no joysticks available. thus no inputs will be provided by this translator.");
00561       }
00562       else
00563         log_error("getDevices() SDL joystick subsystem not inititialized, providing no inputs.");
00564 
00565       log_debug("getDevices() returning registry");
00566       return m_registry;
00567     }
00568 
00569     bool SDLControllers::translate(const SDL_JoyAxisEvent& jaxis) { 
00570       Axis* axis = getAxis(jaxis.which, jaxis.axis);
00571       if(axis)
00572         axis->setAxis(jaxis.value);
00573       else
00574         return false;
00575 
00576       UpdateSystem(*axis);
00577       return true;
00578     };
00579 
00581     bool SDLControllers::translate(const SDL_JoyBallEvent& jball) { 
00582       Motion* motion= getBall(jball.which, jball.ball);
00583       if(motion)
00584         motion->setMotion(jball.xrel, jball.yrel);
00585       else
00586         return false;
00587 
00588       UpdateSystem(*motion);
00589       return true;
00590     };
00591 
00592     bool SDLControllers::translate(const SDL_JoyHatEvent& jhat) { 
00593       Hat* hat = getHat(jhat.which, jhat.hat);
00594 
00595       if(hat) {
00596         switch(jhat.value) {
00597           case SDL_HAT_CENTERED:
00598             hat->setPosition(HatAction::Centered);
00599           break;
00600           case SDL_HAT_UP:
00601             hat->setPosition(HatAction::Up);
00602           break;
00603           case SDL_HAT_RIGHT:
00604             hat->setPosition(HatAction::Right);
00605           break;
00606           case SDL_HAT_DOWN:
00607             hat->setPosition(HatAction::Down);
00608           break;
00609           case SDL_HAT_LEFT:
00610             hat->setPosition(HatAction::Left);
00611           break;
00612           case SDL_HAT_RIGHTUP:
00613             hat->setPosition(HatAction::RightUp);
00614           break;
00615           case SDL_HAT_RIGHTDOWN:
00616             hat->setPosition(HatAction::RightDown);
00617           break;
00618           case SDL_HAT_LEFTUP:
00619             hat->setPosition(HatAction::LeftUp);
00620           break;
00621           case SDL_HAT_LEFTDOWN:
00622             hat->setPosition(HatAction::LeftDown);
00623           break;
00624 
00625           default:
00626             log_error("Convert( Hat ) position gone unhandled");
00627             return false;
00628           break;
00629         }
00630       }
00631       else 
00632         return false;
00633 
00634       UpdateSystem(*hat);
00635       return true; 
00636     };
00637 
00638     bool SDLControllers::translate(const SDL_JoyButtonEvent& jbutton, bool movement) {
00639       Press* button = getButton(jbutton.which, jbutton.button);
00640 
00641       if(button) { 
00642         if(movement)
00643           log_debug("translate setting state to true");
00644         else
00645           log_debug("translate setting state to false");
00646 
00647         button->setState(movement);
00648         UpdateSystem(*button);
00649         return true;
00650       }
00651 
00652       return false;
00653     };
00654 
00655     bool SDLControllers::translate(const SDL_Event& input_event) {
00656       bool translated = false;
00657 
00658       switch(input_event.type) {
00659         case SDL_JOYAXISMOTION:
00660           translated = translate(input_event.jaxis); 
00661         break;
00662         case SDL_JOYHATMOTION:  
00663           translated = translate(input_event.jhat); 
00664         break;
00665         case SDL_JOYBALLMOTION: 
00666           translated = translate(input_event.jball); 
00667         break;
00668         case SDL_JOYBUTTONDOWN: 
00669           log_debug("translate joy event, calling button press");
00670           translated = translate(input_event.jbutton, pressed); 
00671         break;
00672         case SDL_JOYBUTTONUP: 
00673           log_debug("translate joy event, calling button release");
00674           translated = translate(input_event.jbutton, released); 
00675         break;
00676         default:
00677           translated = false; 
00678         break;
00679       }
00680 
00681       return translated;
00682     };
00683 
00684     bool SDLTranslator::addTranslated(DeviceTranslator* sdl_device) {
00685       for(DeviceTranslators::iterator i= m_sdltranslators.begin(); i != m_sdltranslators.end() ; i++) 
00686         if(*(*i) == *sdl_device) {
00687           log_error("addTranslated() Prevented due to name conflict with pre-existing device translator.");
00688           delete sdl_device;
00689           return false;
00690         }
00691 
00692       m_sdltranslators.push_back(sdl_device);
00693 
00695       m_devices.mergeRegistry(sdl_device->getDevices());
00696       return true;
00697     } 
00698 
00699     void SDLTranslator::translate(const SDL_Event& input_event) {
00700         if(input_event.type == SDL_QUIT) 
00701           quit();
00702         else {
00704           for(DeviceTranslators::iterator i = m_sdltranslators.begin(); i != m_sdltranslators.end() ; i++)
00705             if((*i)->translate(input_event))
00706              continue;
00707         }
00708     };
00709 
00710     void SDLTranslator::poll() {
00711       while(SDL_PollEvent(&m_event)) {
00712         translate(m_event); 
00713       }
00714     };
00715 
00716     DeviceRegistry& SDLTranslator::getDevices() {
00717       return m_devices;
00718     };
00719 
00720     DeviceTranslator* const SupportedFactory::create(const Supported type, System* const sys) {
00721       switch(type) {
00722           case Controllers:
00723             return new SDLControllers(sys);
00724           break;
00725           case Keyboards:
00726             return new SDLKeyboards(sys);
00727           break;
00728           case Mice:
00729             return new SDLMice(sys);
00730           break;
00731         default:
00732           return NULL;
00733         break;
00734       }
00735     };
00736   }
00737 }

(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