MercuryInputEE.cpp

Go to the documentation of this file.
00001 #include "MercuryInputEE.h"
00002 #include <libpad.h>
00003 #include "MercuryUtil.h"
00004 #include "MercuryLog.h"
00005 #include <sifrpc.h>
00006 #include <loadfile.h>
00007 #define ROM_PADMAN
00008 
00009 CursorDevice * DefaultCursorDevice = new SimPadMouse;
00010 
00011 bool EE_PAD1Responce = AutoInputDeviceTracker.AddDevice( "EEPAD1", new DeviceEEPAD );
00012 
00013 //NOTE: I have not tested two pads usage.  Use this wearly.
00014 //bool EE_PAD2Responce = AutoInputDeviceTracker.AddDevice( "EEPAD2", new DeviceEEPAD );
00015 
00016 bool bInit = false;
00017 
00018 int iMouseXBase = 127;
00019 int iMouseYBase = 127;
00020 
00021 char buf[256] __attribute__((aligned(64)));
00022 struct padButtonStatus data;
00023 
00024 void DeviceEEPAD::Init( MString Name ) 
00025 {
00026     int ret;
00027     LOG.Log( "Pad driver under Free Academic License 2.0 Help from Pukko and Gustavo" );
00028     //We have to assume SInitRpc() has already been called.
00029     if ( !bInit )
00030     {
00031         LOG.Log("Loading one-time-only modules for input." );
00032         SifInitRpc(0);
00033         ret = SifLoadModule( "rom0:SIO2MAN", 0, NULL );
00034         if( ret < 0 )
00035         {
00036             LOG.Warn( ssprintf( "COULD NOT LOAD SIO! ERROR CODE: %d", ret ) );
00037             return;
00038         }
00039         ret = SifLoadModule( "rom0:PADMAN", 0, NULL );
00040         if( ret < 0 )
00041         {
00042             LOG.Warn( ssprintf( "COULD NOT LOAD PADMAN! ERROR CODE: %d", ret ) );
00043             return;
00044         }
00045         LOG.Log("Done loading one-time-only modules for input." );
00046 
00047         ret = padInit(0);
00048 
00049         LOG.Log(ssprintf("Received pad init code %d",ret));
00050     }
00051     if ( Name.length() < 6 )
00052     {
00053         LOG.Warn("Could not deduce proper name for input driver \""+Name+"\".");
00054         return;
00055     }
00056     m_iDeviceNumber = atoi( Name.substr(5).c_str() );
00057     LOG.Log( ssprintf( "Setting up PAD device %d.",m_iDeviceNumber) );
00058     ret = padPortOpen( 0, m_iDeviceNumber-1, buf );
00059     LOG.Log( ssprintf( "Got port open code: %d.", ret ) );
00060     ret = padRead( 0, m_iDeviceNumber-1, &data );
00061     LOG.Log( ssprintf( "First pad read Responce: %d", ret ) );
00062     m_iAllKeys = data.btns;
00063     LOG.Log( ssprintf( "Pad Init() complete, first code: %d",m_iAllKeys) );
00064 }
00065 
00066 bool DeviceEEPAD::IsButtonDown( int ButtonNumber ) 
00067 {
00068     if ( ( ButtonNumber > 15 ) || ( ButtonNumber < 0 ) )
00069         return false;
00070 
00071     return !((m_iAllKeys>>ButtonNumber)&1);
00072 }
00073 
00074 int DeviceEEPAD::NumButtons() 
00075 {
00076     return 16;
00077 }
00078 
00079 void DeviceEEPAD::Update( ) 
00080 {
00081     padRead( 0, m_iDeviceNumber-1, &data );
00082     unsigned long iCurKeys = data.btns;
00083     for ( int j = 0; j < 16; j++ )
00084         if ( ((m_iAllKeys>>j)&1) != ((iCurKeys>>j)&1) )
00085         {
00086             if ( (iCurKeys>>j)&1 )
00087             {
00088                 InputEvent ie = InputEvent( 0, j, IET_DOWN );
00089                 m_sEvents.push( ie );
00090             } else {
00091                 InputEvent ie = InputEvent( 0, j, IET_RELEASE );
00092                 m_sEvents.push( ie );
00093             }
00094         }
00095     m_iAllKeys = data.btns;
00096     iMouseXBase = data.rjoy_h;
00097     iMouseYBase = data.rjoy_v;
00098 }
00099 
00100 InputEvent DeviceEEPAD::PopLastEvent() 
00101 { 
00102     if ( m_sEvents.empty() )
00103         return InputEvent( 0,0,IET_NONE);
00104     InputEvent ret = m_sEvents.top();
00105     m_sEvents.pop();
00106     return ret;
00107 }
00108 
00109 SimPadMouse::SimPadMouse( )
00110 {
00111     m_x = 0;
00112     m_y = 0;
00113 }
00114 
00115 SimPadMouse::~SimPadMouse( )
00116 {
00117 }
00118 
00119 void SimPadMouse::GetPosition( int &x, int &y )
00120 {
00121     x=m_x;y=m_y;
00122 }
00123 
00124 void SimPadMouse::Update(  )
00125 {
00126     m_x += (iMouseXBase - 127)/5;
00127     m_y += (iMouseYBase - 127)/5;
00128 }
00129 
00130 
00131 /* 
00132  * Copyright (c) 2006, Charles Lohr
00133  * All rights reserved.
00134  *
00135  * Redistribution and use in source and binary forms, with or
00136  * without modification, are permitted provided that the following
00137  * conditions are met:
00138  *  -   Redistributions of source code must retain the above
00139  *      copyright notice, this list of conditions and the following disclaimer.
00140  *  -   Redistributions in binary form must reproduce the above copyright
00141  *      notice, this list of conditions and the following disclaimer in
00142  *      the documentation and/or other materials provided with the distribution.
00143  *  -   Neither the name of the <ORGANIZATION> nor the names of its
00144  *      contributors may be used to endorse or promote products derived from
00145  *      this software without specific prior written permission.
00146  *
00147  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00148  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00149  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00150  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00151  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00152  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00153  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00154  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00155  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00156  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00157  */

Hosted by SourceForge.net Logo