MercuryInputWin32.cpp

Go to the documentation of this file.
00001 #include "MercuryInputWin32.h"
00002 #include "MercuryLog.h"
00003 #include "MercuryMessages.h"
00004 #include "MercuryDisplay.h"
00005 #include <windows.h>
00006 
00007 extern int g_iWindowX, g_iWindowY;
00008 
00009 MDeque < unsigned char > DeviceKeyboardW32::m_qRepeats;
00010 CursorDevice * DefaultCursorDevice = new Win32Mouse;
00011 bool Win32Mouse::m_bHasFocus;
00012 
00013 bool W32KBResponce = AutoInputDeviceTracker.AddDevice( "KB", new DeviceKeyboardW32 );
00014 
00015 InputEvent RemapW32KB( InputEvent & ie )
00016 {
00017     if ( ie.ButtonNumber >= 96 && ie.ButtonNumber < 106 )
00018         ie.ButtonNumber += 160;
00019     
00020     switch ( ie.ButtonNumber )
00021     {
00022     case 33:    ie.ButtonNumber = 280;  break;
00023     case 34:    ie.ButtonNumber = 281;  break;
00024     case 35:    ie.ButtonNumber = 279;  break;
00025     case 36:    ie.ButtonNumber = 278;  break;
00026     case 37:    ie.ButtonNumber = 276;  break;
00027     case 38:    ie.ButtonNumber = 273;  break;
00028     case 39:    ie.ButtonNumber = 275;  break;
00029     case 40:    ie.ButtonNumber = 274;  break;
00030     case 44:    ie.ButtonNumber = 316;  break;
00031     case 45:    ie.ButtonNumber = 277;  break;
00032     case 46:    ie.ButtonNumber = 127;  break;
00033     case 110:   ie.ButtonNumber = 266;  break;
00034     case 111:   ie.ButtonNumber = 267;  break;
00035     case 109:   ie.ButtonNumber = 269;  break;
00036     case 106:   ie.ButtonNumber = 268;  break;
00037     case 107:   ie.ButtonNumber = 270;  break;
00038     case 187:   ie.ButtonNumber = '=';  break;
00039     case 189:   ie.ButtonNumber = '-';  break;
00040     case 220:   ie.ButtonNumber = '\\'; break;
00041     case 192:   ie.ButtonNumber = '`';  break;
00042     case 219:   ie.ButtonNumber = '[';  break;
00043     case 221:   ie.ButtonNumber = ']';  break;
00044     case 186:   ie.ButtonNumber = ';';  break;
00045     case 222:   ie.ButtonNumber = '\''; break;
00046     case 188:   ie.ButtonNumber = ',';  break;
00047     case 190:   ie.ButtonNumber = '.';  break;
00048     case 191:   ie.ButtonNumber = '/';  break;
00049     default:
00050         break;
00051         //Do nothing
00052     }
00053     if ( ie.ButtonNumber >= 112 && ie.ButtonNumber <= 123 )
00054         ie.ButtonNumber += 170;
00055 
00056     if ( ie.ButtonNumber >= 'A' && ie.ButtonNumber <= 'Z' )
00057         ie.ButtonNumber += 32;
00058 
00059     return ie;
00060 }
00061 
00062 void DeviceKeyboardW32::Init( MString Name ) 
00063 {
00064 //  InputDevice::Init(Name); //Pure virtual
00065 
00066     GetKeyboardState( m_vKeys );
00067     
00068     memset( m_bAllKeys, 0, 512 );
00069     for ( int i = 0; i < 256; i++ )
00070     {
00071         InputEvent c( 0, i, IET_DOWN );
00072         RemapW32KB( c );
00073 
00074         if ( m_vKeys[i] >> 7 )
00075             m_bAllKeys[c.ButtonNumber] = true;
00076         else
00077             m_bAllKeys[c.ButtonNumber] = false;
00078     }
00079 }
00080 
00081 bool DeviceKeyboardW32::IsButtonDown( int ButtonNumber ) 
00082 {
00083     if ( ( ButtonNumber > 511 ) || ( ButtonNumber < 0 ) )
00084         return false;
00085 
00086     return m_bAllKeys[ButtonNumber];
00087 }
00088 
00089 int DeviceKeyboardW32::NumButtons() 
00090 {
00091     return 512;
00092 }
00093 
00094 void DeviceKeyboardW32::Update( ) 
00095 {
00096     if( !DISPLAY->GetWindow()->HasFocus() )
00097         return;
00098 
00099     unsigned char curKeys[256];
00100     GetKeyboardState( curKeys );
00101 
00102     if ( GetKeyState(VK_CAPITAL) > 0 )
00103         curKeys[15] = 128;
00104     else
00105         curKeys[15] = 0;
00106 
00107     for ( int i = 0; i < 256; i++ )
00108     {
00109         if ( ( curKeys[i] >> 7 ) && !( m_vKeys[i] >> 7 ) )
00110         {
00111             InputEvent ie = InputEvent( 0,i,IET_DOWN);
00112             ie = RemapW32KB( ie );
00113 //          InputEvent ie = RemapW32KB( InputEvent( 0,i,IET_DOWN) );
00114             m_sEvents.push_back( ie );
00115             m_bAllKeys[ie.ButtonNumber] = true;
00116         }
00117         if ( !( curKeys[i] >> 7 ) && ( m_vKeys[i] >> 7 ) )
00118         {
00119             InputEvent ie = InputEvent( 0,i,IET_RELEASE);
00120             ie = RemapW32KB( ie );
00121 //          InputEvent ie = RemapW32KB( InputEvent( 0,i,IET_RELEASE) );
00122             m_sEvents.push_back( ie );
00123             m_bAllKeys[ie.ButtonNumber] = false;
00124         }
00125     }
00126 
00127     //clear all fast repeats
00128     while( !m_qRepeats.empty() )
00129     {
00130         InputEvent ie = InputEvent( 0,m_qRepeats.front(),IET_REPEAT);
00131         ie = RemapW32KB( ie );
00132         m_sEvents.push_back( ie );
00133         m_qRepeats.pop_front();
00134     }
00135 
00136     memcpy( m_vKeys, curKeys, sizeof(curKeys) );
00137 }
00138 
00139 InputEvent DeviceKeyboardW32::PopLastEvent() 
00140 { 
00141     if ( m_sEvents.empty() )
00142         return InputEvent( 0,0,IET_NONE);
00143     InputEvent ret = m_sEvents.front();
00144     m_sEvents.pop_front();
00145     return ret;
00146 }
00147 
00148 Win32Mouse::Win32Mouse( )
00149 {
00150     m_x = 0;
00151     m_y = 0;
00152     SetCursorPos( g_iWindowX + 200, g_iWindowY + 200 );
00153     m_bHasFocus = true;
00154     m_bSubscribed = false;
00155 }
00156 
00157 Win32Mouse::~Win32Mouse( )
00158 {
00159     if ( !m_bSubscribed )
00160         MESSAGEMAN->Unsubscribe("changefocus", MercuryCallback(UpdateFocus,this) );
00161 }
00162 
00163 void Win32Mouse::GetPosition( int &x, int &y )
00164 {
00165     x=m_x;y=m_y;
00166 }
00167 
00168 void Win32Mouse::Update(  )
00169 {
00170     if ( !m_bSubscribed )
00171     {
00172         MESSAGEMAN->Subscribe( "changefocus", MercuryCallback(UpdateFocus,this) );
00173         m_bSubscribed = true;
00174     }
00175 
00176     if ( !m_bHasFocus )
00177         return;
00178 
00179     POINT * ptrPT = new POINT;
00180 
00181     GetCursorPos( ptrPT );
00182         
00183     m_x += ptrPT->x - (g_iWindowX+200);
00184     m_y += ptrPT->y - (g_iWindowY+200);
00185 
00186     SetCursorPos( g_iWindowX+200, g_iWindowY+200 );
00187 
00188     SAFE_DELETE(ptrPT);
00189 }
00190 
00191 void Win32Mouse::UpdateFocus( const MString &message, void * data, PStack & info )
00192 {
00193     bool hasfocus = false;
00194     if ( !info.PopItem().GetValueB( hasfocus ) )
00195     {
00196         LOG.Warn( "Cannot parse paramater 1 to boolean in function " + message );
00197         return;
00198     }
00199     if ( !hasfocus )
00200     {
00201         ShowCursor( true );
00202         m_bHasFocus = false;
00203     } else {
00204         ShowCursor( false );
00205         m_bHasFocus = true;
00206     }
00207 }
00208 
00209 /* 
00210  * Copyright (c) 2005-2006, Charles Lohr
00211  * All rights reserved.
00212  *
00213  * Redistribution and use in source and binary forms, with or
00214  * without modification, are permitted provided that the following
00215  * conditions are met:
00216  *  -   Redistributions of source code must retain the above
00217  *      copyright notice, this list of conditions and the following disclaimer.
00218  *  -   Redistributions in binary form must reproduce the above copyright
00219  *      notice, this list of conditions and the following disclaimer in
00220  *      the documentation and/or other materials provided with the distribution.
00221  *  -   Neither the name of the Mercury Engine nor the names of its
00222  *      contributors may be used to endorse or promote products derived from
00223  *      this software without specific prior written permission.
00224  *
00225  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00226  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00227  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00228  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00229  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00230  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00231  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00232  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00233  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00234  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00235  */

Hosted by SourceForge.net Logo