Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <GAUGES.H>
- #include <FSUIPC_User.h>
- #include <sstream>
- GAUGESIMPORT ImportTable = {
- { 0x0000000f, static_cast<PPANELS>( 0 ) },
- { 0, 0 }
- };
- void FSAPI ModuleInit();
- void FSAPI ModuleCleanup();
- GAUGESLINKAGE Linkage = {
- 0x00408686,
- ModuleInit,
- ModuleCleanup,
- 0,
- 0,
- 0x900,
- 0
- };
- WNDPROC old_wnd_proc( 0 );
- HWND wnd_fs( 0 );
- bool _connection_open( false );
- enum InputMode {
- None = 0,
- Heading = 1,
- Altitude = 2,
- Speed = 3
- };
- LRESULT CALLBACK WindowProc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam ) {
- bool handled( false );
- bool invalid( false );
- static DWORD last_input( 0 );
- static InputMode input_mode( None );
- static int number( 0 );
- static unsigned char num_chars( 0 );
- if( !_connection_open ) {
- return CallWindowProc( old_wnd_proc, hwnd, msg, wparam, lparam );
- }
- switch( msg ) {
- case WM_KEYDOWN:
- // Check for delay.
- if( input_mode != None && GetTickCount() - last_input > 3000 ) {
- input_mode = None;
- }
- bool new_mode( false );
- // Activate heading input mode.
- if( wparam == 0x48 ) {
- PlaySound( "Sound/ap_hotkey/heading.wav", 0, SND_ASYNC | SND_FILENAME );
- input_mode = Heading;
- new_mode = true;
- }
- else if( wparam == 0x41 ) {
- PlaySound( "Sound/ap_hotkey/altitude.wav", 0, SND_ASYNC | SND_FILENAME );
- input_mode = Altitude;
- new_mode = true;
- }
- else if( wparam == 0x56 ) {
- PlaySound( "Sound/ap_hotkey/speed.wav", 0, SND_ASYNC | SND_FILENAME );
- input_mode = Speed;
- new_mode = true;
- }
- else if( input_mode == None ) {
- break;
- }
- // Reset number of fed characters.
- if( new_mode ) {
- num_chars = 0;
- number = 0;
- last_input = GetTickCount();
- handled = true;
- break;
- }
- // From here on only executed when input_mode != None.
- handled = true;
- last_input = GetTickCount();
- // Check for valid number.
- if( wparam < 0x30 || wparam > 0x39 ) {
- input_mode = None;
- PlaySound( "Sound/ap_hotkey/invalid.wav", 0, SND_ASYNC | SND_FILENAME );
- handled = true;
- break;
- }
- unsigned char ch( wparam - 0x30 );
- // Heading mode.
- if( input_mode == Heading ) {
- number += ch * static_cast<unsigned short>( std::pow( 10.f, 2 - num_chars ) );
- ++num_chars;
- if( num_chars >= 3 ) {
- if( number > 360 || number < 0 ) {
- invalid = true;
- }
- else {
- DWORD result( 0 );
- unsigned short heading( static_cast<unsigned short>( std::ceil( number * 65536.f / 360.f ) ) );
- FSUIPC_Write( 0x07cc, 2, &heading, &result );
- FSUIPC_Process( &result );
- }
- input_mode = None;
- }
- }
- else if( input_mode == Altitude ) {
- number += ch * static_cast<unsigned short>( pow( 10.f, 2 - num_chars ) );
- ++num_chars;
- if( num_chars >= 3 ) {
- DWORD result( 0 );
- unsigned int altitude( static_cast<unsigned int>( std::floor( number * 100.0f * 0.3048f * 65536.f + 0.5f ) ) );
- FSUIPC_Write( 0x07d4, 4, &altitude, &result );
- FSUIPC_Process( &result );
- input_mode = None;
- }
- }
- else if( input_mode == Speed ) {
- number += ch * static_cast<unsigned short>( pow( 10.f, 2 - num_chars ) );
- ++num_chars;
- if( num_chars >= 3 ) {
- DWORD result( 0 );
- unsigned short speed( number );
- FSUIPC_Write( 0x07e2, 2, &speed, &result );
- FSUIPC_Process( &result );
- input_mode = None;
- }
- }
- if( invalid ) {
- PlaySound( "Sound/ap_hotkey/invalid.wav", 0, SND_ASYNC | SND_FILENAME );
- }
- else {
- std::stringstream sstr;
- sstr << "Sound/ap_hotkey/" << static_cast<int>( ch ) << ".wav";
- PlaySound( sstr.str().c_str(), 0, SND_ASYNC | SND_FILENAME );
- }
- handled = true;
- break;
- }
- if( !handled ) {
- return CallWindowProc( old_wnd_proc, hwnd, msg, wparam, lparam );
- }
- return 0;
- }
- bool WINAPI DllMain( HINSTANCE hinst, DWORD reason, void* reserved ) {
- switch( reason ) {
- case DLL_PROCESS_ATTACH:
- wnd_fs = FindWindow( "FS98MAIN", 0 );
- old_wnd_proc = reinterpret_cast<WNDPROC>( SetWindowLong( wnd_fs, GWL_WNDPROC, reinterpret_cast<LONG>( WindowProc ) ) );
- break;
- }
- return true;
- }
- void FSAPI ModuleInit() {
- DWORD result;
- if( FSUIPC_Open( SIM_FS2K4, &result ) ) {
- _connection_open = true;
- }
- }
- void FSAPI ModuleCleanup() {
- if( _connection_open ) {
- FSUIPC_Close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement