Advertisement
Guest User

Untitled

a guest
Mar 30th, 2011
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.41 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <GAUGES.H>
  3. #include <FSUIPC_User.h>
  4. #include <sstream>
  5.  
  6. GAUGESIMPORT ImportTable = {
  7.     { 0x0000000f, static_cast<PPANELS>( 0 ) },
  8.     { 0, 0 }
  9. };
  10.  
  11. void FSAPI ModuleInit();
  12. void FSAPI ModuleCleanup();
  13.  
  14. GAUGESLINKAGE Linkage = {
  15.     0x00408686,
  16.     ModuleInit,
  17.     ModuleCleanup,
  18.     0,
  19.     0,
  20.     0x900,
  21.     0
  22. };
  23.  
  24. WNDPROC old_wnd_proc( 0 );
  25. HWND wnd_fs( 0 );
  26. bool _connection_open( false );
  27.  
  28. enum InputMode {
  29.     None = 0,
  30.     Heading = 1,
  31.     Altitude = 2,
  32.     Speed = 3
  33. };
  34.  
  35. LRESULT CALLBACK WindowProc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam ) {
  36.     bool handled( false );
  37.     bool invalid( false );
  38.     static DWORD last_input( 0 );
  39.     static InputMode input_mode( None );
  40.     static int number( 0 );
  41.     static unsigned char num_chars( 0 );
  42.  
  43.     if( !_connection_open ) {
  44.         return CallWindowProc( old_wnd_proc, hwnd, msg, wparam, lparam );
  45.     }
  46.  
  47.     switch( msg ) {
  48.         case WM_KEYDOWN:
  49.             // Check for delay.
  50.             if( input_mode != None && GetTickCount() - last_input > 3000 ) {
  51.                 input_mode = None;
  52.             }
  53.  
  54.             bool new_mode( false );
  55.  
  56.             // Activate heading input mode.
  57.             if( wparam == 0x48 ) {
  58.                 PlaySound( "Sound/ap_hotkey/heading.wav", 0, SND_ASYNC | SND_FILENAME );
  59.                 input_mode = Heading;
  60.                 new_mode = true;
  61.             }
  62.             else if( wparam == 0x41 ) {
  63.                 PlaySound( "Sound/ap_hotkey/altitude.wav", 0, SND_ASYNC | SND_FILENAME );
  64.                 input_mode = Altitude;
  65.                 new_mode = true;
  66.             }
  67.             else if( wparam == 0x56 ) {
  68.                 PlaySound( "Sound/ap_hotkey/speed.wav", 0, SND_ASYNC | SND_FILENAME );
  69.                 input_mode = Speed;
  70.                 new_mode = true;
  71.             }
  72.             else if( input_mode == None ) {
  73.                 break;
  74.             }
  75.  
  76.             // Reset number of fed characters.
  77.             if( new_mode ) {
  78.                 num_chars = 0;
  79.                 number = 0;
  80.                 last_input = GetTickCount();
  81.                 handled = true;
  82.                 break;
  83.             }
  84.            
  85.             // From here on only executed when input_mode != None.
  86.             handled = true;
  87.             last_input = GetTickCount();
  88.  
  89.             // Check for valid number.
  90.             if( wparam < 0x30 || wparam > 0x39 ) {
  91.                 input_mode = None;
  92.                 PlaySound( "Sound/ap_hotkey/invalid.wav", 0, SND_ASYNC | SND_FILENAME );
  93.                 handled = true;
  94.                 break;
  95.             }
  96.  
  97.             unsigned char ch( wparam - 0x30 );
  98.  
  99.             // Heading mode.
  100.             if( input_mode == Heading ) {
  101.                 number += ch * static_cast<unsigned short>( std::pow( 10.f, 2 - num_chars ) );
  102.                 ++num_chars;
  103.  
  104.                 if( num_chars >= 3 ) {
  105.                     if( number > 360 || number < 0 ) {
  106.                         invalid = true;
  107.                     }
  108.                     else {
  109.                         DWORD result( 0 );
  110.                         unsigned short heading( static_cast<unsigned short>( std::ceil( number * 65536.f / 360.f ) ) );
  111.                        
  112.                         FSUIPC_Write( 0x07cc, 2, &heading, &result );
  113.                         FSUIPC_Process( &result );
  114.                     }
  115.  
  116.                     input_mode = None;
  117.                 }
  118.             }
  119.             else if( input_mode == Altitude ) {
  120.                 number += ch * static_cast<unsigned short>( pow( 10.f, 2 - num_chars ) );
  121.                 ++num_chars;
  122.  
  123.                 if( num_chars >= 3 ) {
  124.                     DWORD result( 0 );
  125.                     unsigned int altitude( static_cast<unsigned int>( std::floor( number * 100.0f * 0.3048f * 65536.f + 0.5f ) ) );
  126.                        
  127.                     FSUIPC_Write( 0x07d4, 4, &altitude, &result );
  128.                     FSUIPC_Process( &result );
  129.  
  130.                     input_mode = None;
  131.                 }
  132.             }
  133.             else if( input_mode == Speed ) {
  134.                 number += ch * static_cast<unsigned short>( pow( 10.f, 2 - num_chars ) );
  135.                 ++num_chars;
  136.  
  137.                 if( num_chars >= 3 ) {
  138.                     DWORD result( 0 );
  139.                     unsigned short speed( number );
  140.                        
  141.                     FSUIPC_Write( 0x07e2, 2, &speed, &result );
  142.                     FSUIPC_Process( &result );
  143.  
  144.                     input_mode = None;
  145.                 }
  146.             }
  147.  
  148.             if( invalid ) {
  149.                 PlaySound( "Sound/ap_hotkey/invalid.wav", 0, SND_ASYNC | SND_FILENAME );
  150.             }
  151.             else {
  152.                 std::stringstream sstr;
  153.                 sstr << "Sound/ap_hotkey/" << static_cast<int>( ch ) << ".wav";
  154.                 PlaySound( sstr.str().c_str(), 0, SND_ASYNC | SND_FILENAME );
  155.             }
  156.  
  157.             handled = true;
  158.             break;
  159.     }
  160.  
  161.     if( !handled ) {
  162.         return CallWindowProc( old_wnd_proc, hwnd, msg, wparam, lparam );
  163.     }
  164.  
  165.     return 0;
  166. }
  167.  
  168. bool WINAPI DllMain( HINSTANCE hinst, DWORD reason, void* reserved ) {
  169.     switch( reason ) {
  170.         case DLL_PROCESS_ATTACH:
  171.             wnd_fs = FindWindow( "FS98MAIN", 0 );
  172.             old_wnd_proc = reinterpret_cast<WNDPROC>( SetWindowLong( wnd_fs, GWL_WNDPROC, reinterpret_cast<LONG>( WindowProc ) ) );
  173.             break;
  174.     }
  175.  
  176.     return true;
  177. }
  178.  
  179. void FSAPI ModuleInit() {
  180.     DWORD result;
  181.  
  182.     if( FSUIPC_Open( SIM_FS2K4, &result ) ) {
  183.         _connection_open = true;
  184.     }
  185. }
  186.  
  187. void FSAPI ModuleCleanup() {
  188.     if( _connection_open ) {
  189.         FSUIPC_Close();
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement