123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2013 GarageGames, LLC
- // Portions Copyright (c) 2014 James S Urquhart
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #include "platformEmscripten/platformEmscripten.h"
- #include "platform/platformInput.h"
- #include "platform/platformVideo.h"
- #include "platform/event.h"
- #include "game/gameInterface.h"
- #include "console/console.h"
- #include "platformEmscripten/EmscriptenInputManager.h"
- #include <SDL/SDL.h>
- #ifdef LOG_INPUT
- #include <time.h>
- #include <stdarg.h>
- #include <fcntl.h>
- #endif
- // Static class variables:
- InputManager* Input::smManager = NULL;
- // smActive is not maintained under unix. Use Input::isActive()
- // instead
- bool Input::smActive = false;
- CursorManager* Input::smCursorManager = 0;
- //extern AsciiData AsciiTable[NUM_KEYS];
- #ifdef LOG_INPUT
- S32 gInputLog = -1;
- #endif
- //------------------------------------------------------------------------------
- void Input::init()
- {
- Con::printf( "Input Init:" );
- destroy();
- smActive = false;
- smManager = NULL;
- UInputManager *uInputManager = new UInputManager;
- if ( !uInputManager->enable() )
- {
- Con::errorf( " Failed to enable Input Manager." );
- delete uInputManager;
- return;
- }
- uInputManager->init();
- smManager = uInputManager;
- Con::printf(" Input initialized");
- Con::printf(" ");
- }
- //------------------------------------------------------------------------------
- ConsoleFunction( isJoystickDetected, bool, 1, 1, "isJoystickDetected()" )
- {
- argc; argv;
- UInputManager* manager = dynamic_cast<UInputManager*>(Input::getManager());
- if (manager)
- return manager->joystickDetected();
- else
- return false;
- }
- //------------------------------------------------------------------------------
- ConsoleFunction( getJoystickAxes, const char*, 2, 2, "getJoystickAxes( instance )" )
- {
- argc; argv;
- UInputManager* manager = dynamic_cast<UInputManager*>(Input::getManager());
- if (manager)
- return manager->getJoystickAxesString(dAtoi(argv[1]));
- else
- return "";
- }
- //------------------------------------------------------------------------------
- U16 Input::getKeyCode( U16 asciiCode )
- {
- U16 keyCode = 0;
- U16 i;
-
- // This is done three times so the lowerkey will always
- // be found first. Some foreign keyboards have duplicate
- // chars on some keys.
- for ( i = KEY_FIRST; i < NUM_KEYS && !keyCode; i++ )
- {
- if ( AsciiTable[i].lower.ascii == asciiCode )
- {
- keyCode = i;
- break;
- };
- }
- for ( i = KEY_FIRST; i < NUM_KEYS && !keyCode; i++ )
- {
- if ( AsciiTable[i].upper.ascii == asciiCode )
- {
- keyCode = i;
- break;
- };
- }
- for ( i = KEY_FIRST; i < NUM_KEYS && !keyCode; i++ )
- {
- if ( AsciiTable[i].goofy.ascii == asciiCode )
- {
- keyCode = i;
- break;
- };
- }
- return( keyCode );
- }
- //-----------------------------------------------------------------------------
- U16 Input::getAscii( U16 keyCode, KEY_STATE keyState )
- {
- if ( keyCode >= NUM_KEYS )
- return 0;
- switch ( keyState )
- {
- case STATE_LOWER:
- return AsciiTable[keyCode].lower.ascii;
- case STATE_UPPER:
- return AsciiTable[keyCode].upper.ascii;
- case STATE_GOOFY:
- return AsciiTable[keyCode].goofy.ascii;
- default:
- return(0);
-
- }
- }
- //------------------------------------------------------------------------------
- void Input::destroy()
- {
- if ( smManager && smManager->isEnabled() )
- {
- smManager->disable();
- delete smManager;
- smManager = NULL;
- }
- }
- //------------------------------------------------------------------------------
- bool Input::enable()
- {
- if ( smManager && !smManager->isEnabled() )
- return( smManager->enable() );
-
- return( false );
- }
- //------------------------------------------------------------------------------
- void Input::disable()
- {
- if ( smManager && smManager->isEnabled() )
- smManager->disable();
- }
- //------------------------------------------------------------------------------
- void Input::activate()
- {
- if ( smManager && smManager->isEnabled() && !isActive())
- {
- #ifdef LOG_INPUT
- Input::log( "Activating Input...\n" );
- #endif
- UInputManager* uInputManager = dynamic_cast<UInputManager*>( smManager );
- if ( uInputManager )
- uInputManager->activate();
- }
- }
- //------------------------------------------------------------------------------
- void Input::deactivate()
- {
- if ( smManager && smManager->isEnabled() && isActive() )
- {
- #ifdef LOG_INPUT
- Input::log( "Deactivating Input...\n" );
- #endif
- UInputManager* uInputManager = dynamic_cast<UInputManager*>( smManager );
- if ( uInputManager )
- uInputManager->deactivate();
- }
- }
- //------------------------------------------------------------------------------
- void Input::reactivate()
- {
- Input::deactivate();
- Input::activate();
- }
- //------------------------------------------------------------------------------
- bool Input::isEnabled()
- {
- if ( smManager )
- return smManager->isEnabled();
- return false;
- }
- //------------------------------------------------------------------------------
- bool Input::isActive()
- {
- UInputManager* uInputManager = dynamic_cast<UInputManager*>( smManager );
- if ( uInputManager )
- return uInputManager->isActive();
- return false;
- }
- //------------------------------------------------------------------------------
- void Input::process()
- {
- if ( smManager )
- smManager->process();
- }
- //------------------------------------------------------------------------------
- InputManager* Input::getManager()
- {
- return smManager;
- }
- #ifdef LOG_INPUT
- //------------------------------------------------------------------------------
- void Input::log( const char* format, ... )
- {
- }
- ConsoleFunction( inputLog, void, 2, 2, "inputLog( string )" )
- {
- argc;
- Input::log( "%s\n", argv[1] );
- }
- #endif // LOG_INPUT
- //------------------------------------------------------------------------------
- const char* Platform::getClipboard()
- {
- return "";
- }
- //------------------------------------------------------------------------------
- bool Platform::setClipboard(const char *text)
- {
- return false;
- }
- #pragma mark ---- Cursor Functions ----
- //------------------------------------------------------------------------------
- void Input::pushCursor(S32 cursorID)
- {
- CursorManager* cm = getCursorManager();
- if(cm)
- cm->pushCursor(cursorID);
- }
- //------------------------------------------------------------------------------
- void Input::popCursor()
- {
- CursorManager* cm = getCursorManager();
- if(cm)
- cm->popCursor();
- }
- //------------------------------------------------------------------------------
- void Input::refreshCursor()
- {
- CursorManager* cm = getCursorManager();
- if(cm)
- cm->refreshCursor();
- }
- #pragma mark ---- DoubleClick Functions ----
- //------------------------------------------------------------------------------
- U32 Input::getDoubleClickTime()
- {
- return ( 50 * 60.0f * 1000.0f);
- }
- //------------------------------------------------------------------------------
- S32 Input::getDoubleClickWidth()
- {
- // this is an arbitrary value.
- return 10;
- }
- //------------------------------------------------------------------------------
- S32 Input::getDoubleClickHeight()
- {
- return getDoubleClickWidth();
- }
- #pragma mark -
- //------------------------------------------------------------------------------
- void Input::setCursorPos(S32 x, S32 y)
- {
- SDL_WarpMouse((S16)x, (S16)y);
- }
|