示例#1
0
void SetupDirectInput(void) {
  HRESULT hr;
  FreeDirectInput();
  hr = InitDirectInput(hInstance, stWindow);
  if(FAILED(hr)) {
    FreeDirectInput();
  }
}
示例#2
0
文件: Joystick.cpp 项目: ef65/vJoy
//-----------------------------------------------------------------------------
// Name: MainDialogProc
// Desc: Handles dialog messages
//-----------------------------------------------------------------------------
INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
{
    UNREFERENCED_PARAMETER( lParam );

    switch( msg )
    {
        case WM_INITDIALOG:
            if( FAILED( InitDirectInput( hDlg ) ) )
            {
                MessageBox( NULL, TEXT( "Error Initializing DirectInput" ),
                            TEXT( "DirectInput Sample" ), MB_ICONERROR | MB_OK );
                EndDialog( hDlg, 0 );
            }

            // Set a timer to go off 30 times a second. At every timer message
            // the input device will be read
            SetTimer( hDlg, 0, 1000 / 30, NULL );
            return TRUE;

        case WM_ACTIVATE:
            if( WA_INACTIVE != wParam && g_pJoystick )
            {
                // Make sure the device is acquired, if we are gaining focus.
                g_pJoystick->Acquire();
            }
            return TRUE;

        case WM_TIMER:
            // Update the input device every timer message
            if( FAILED( UpdateInputState( hDlg ) ) )
            {
                KillTimer( hDlg, 0 );
                MessageBox( NULL, TEXT( "Error Reading Input State. " ) \
                            TEXT( "The sample will now exit." ), TEXT( "DirectInput Sample" ),
                            MB_ICONERROR | MB_OK );
                EndDialog( hDlg, TRUE );
            }
            return TRUE;

        case WM_COMMAND:
            switch( LOWORD( wParam ) )
            {
                case IDCANCEL:
                    EndDialog( hDlg, 0 );
                    return TRUE;
            }

        case WM_DESTROY:
            // Cleanup everything
            KillTimer( hDlg, 0 );
            FreeDirectInput();
            return TRUE;
    }

    return FALSE; // Message not handled 
}
示例#3
0
void SrInputManager::Destroy()
{
	InputDevices::iterator it = m_inputDevices.begin();
	for (; it != m_inputDevices.end(); ++it)
	{
		if ( *it )
		{
			delete (*it);
		}
	}

	FreeDirectInput();
}
示例#4
0
//-----------------------------------------------------------------------------
// Name: MainDialogProc
// Desc: Handles dialog messages
//-----------------------------------------------------------------------------
INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_INITDIALOG:
            if( FAILED( InitDirectInput( hDlg ) ) )
            {
                MessageBox( NULL, TEXT( "Error Initializing DirectInput" ),
                            TEXT( "DirectInput Sample" ), MB_ICONERROR | MB_OK );
                EndDialog( hDlg, 0 );
            }

            // Set a timer to go off 30 times a second. At every timer message
            // the input device will be read
            SetTimer( hDlg, 0, 1000 / 30, NULL );
            return TRUE;

        case WM_TIMER:
            // Update the input device every timer message
            if( FAILED( UpdateInputState( hDlg ) ) )
            {
                KillTimer( hDlg, 0 );
                MessageBox( NULL, TEXT( "Error Reading Input State. " ) \
                            TEXT( "The sample will now exit." ), TEXT( "DirectInput Sample" ),
                            MB_ICONERROR | MB_OK );
                EndDialog( hDlg, TRUE );
            }
            return TRUE;

        case WM_COMMAND:
            switch( LOWORD( wParam ) )
            {
                case IDCANCEL:
                    EndDialog( hDlg, 0 );
                    return TRUE;
            }
            break;

        case WM_DESTROY:
            // Cleanup everything
            KillTimer( hDlg, 0 );
            FreeDirectInput();
            return TRUE;
    }

    return FALSE; // Message not handled 
}
示例#5
0
/******************************************************************
  Function: CloseDLL
  Purpose:  This function is called when the emulator is closing
            down allowing the dll to de-initialise.
  input:    none
  output:   none
*******************************************************************/ 
EXPORT void CALL CloseDLL (void)
{										// HACK: THIS IS BROKEN IN PJ64 1.6 (it calls CloseDLL too often)
	DebugWriteA("CALLED: CloseDLL\n");
	if( g_bRunning )
		RomClosed();
	for( int i = 0; i < 4; i++ )
	{
		freePakData( &g_pcControllers[i] );
		freeModifiers( &g_pcControllers[i] );
	}

	// ZeroMemory( g_pcControllers, sizeof(g_pcControllers) ); // why zero the memory if we're just going to close down?
	
	FreeDirectInput();

	return;
}
示例#6
0
//-----------------------------------------------------------------------------
// Name: Close_Port()
// output: none
// Desc: kills thread then frees directx objects
//-----------------------------------------------------------------------------
void Close_Port()
{
	
	if (PollingThreadHandle != NULL)
	{
		// signal thread to complete
		SetEvent(DeathEvent);
		// wait for thread to complete
		WaitForSingleObject(PollingThreadHandle, 40000);
		// free resources
		CloseHandle(DeathEvent);
		DeathEvent = NULL;
		CloseHandle(PollingThreadHandle);
		PollingThreadHandle = NULL;
	}
    FreeDirectInput();    
}
示例#7
0
//-----------------------------------------------------------------------------
// Name: MainDlgProc()
// Desc: Handles dialog messages
//-----------------------------------------------------------------------------
INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg ) 
    {
        case WM_INITDIALOG:
            OnInitDialog( hDlg );
            break;
        
        case WM_COMMAND:
            switch( LOWORD(wParam) )
            {
                case IDCANCEL:
                    EndDialog( hDlg, 0 ); 
                    break;

                case IDC_EXCLUSIVE:
                case IDC_NONEXCLUSIVE:
                case IDC_FOREGROUND:
                case IDC_BACKGROUND:
                case IDC_IMMEDIATE:
                case IDC_BUFFERED:
                    UpdateUI( hDlg );
                    break;

                case IDM_CREATEDEVICE:
                case IDM_RELEASEDEVICE:
                case IDC_CREATEDEVICE:
                    if( NULL == g_pMouse )
                    {                    
                        if( FAILED( OnCreateDevice( hDlg ) ) )
                        {
                            MessageBox( hDlg, _T("CreateDevice() failed. ")
                                              _T("The sample will now exit. "), 
                                              _T("Mouse"), MB_ICONERROR | MB_OK );
                            FreeDirectInput();
                        }

                        SetFocus( GetDlgItem( hDlg, IDC_CREATEDEVICE ) );
                    }
                    else
                    {
                        FreeDirectInput();
                    }

                    UpdateUI( hDlg );
                    break;

                default:
                    return FALSE; // Message not handled 
            }       
            break;

        case WM_ENTERMENULOOP:
            // Release the device, so if we are in exclusive mode the 
            // cursor will reappear
            if( g_pMouse )
            {
                g_pMouse->Unacquire();
                KillTimer( hDlg, 0 );  // Stop timer, so device is not re-acquired
                SetDlgItemText( hDlg, IDC_DATA, TEXT("Unacquired") );
            }
            break;

        case WM_EXITMENULOOP:
            // Make sure the device is acquired when coming out of a menu loop
            if( g_pMouse )
            {
                g_pMouse->Acquire();
                SetTimer( hDlg, 0, 1000 / 12, NULL ); // Start timer again
            }
            break;

        case WM_ACTIVATE:
            if( WA_INACTIVE != wParam && g_pMouse )
            {
                // Make sure the device is acquired, if we are gaining focus.
                g_pMouse->Acquire();
            }
            break;
        
        case WM_TIMER:
            // Update the input device every timer message
            {
                BOOL bImmediate = ( IsDlgButtonChecked( hDlg, IDC_IMMEDIATE  ) == BST_CHECKED );

                if( bImmediate )
                {
                    if( FAILED( ReadImmediateData( hDlg ) ) )
                    {
                        KillTimer( hDlg, 0 );    
                        MessageBox( NULL, _T("Error Reading Input State. ")
                                          _T("The sample will now exit. "), 
                                          _T("Mouse"), MB_ICONERROR | MB_OK );
                        EndDialog( hDlg, TRUE ); 
                    }
                }
                else
                {
                    if( FAILED( ReadBufferedData( hDlg ) ) )
                    {
                        KillTimer( hDlg, 0 );    
                        MessageBox( NULL, _T("Error Reading Input State. ")
                                          _T("The sample will now exit. "), 
                                          _T("Mouse"), MB_ICONERROR | MB_OK );
                        EndDialog( hDlg, TRUE ); 
                    }
                }
            }
            break;
        
        case WM_DESTROY:
            // Cleanup everything
            KillTimer( hDlg, 0 );    
            FreeDirectInput();    
            break;

        default:
            return FALSE; // Message not handled 
    }

    return TRUE; // Message handled 
}
示例#8
0
//-----------------------------------------------------------------------------
// Name: OnCreateDevice()
// Desc: Setups a the mouse device using the flags from the dialog.
//-----------------------------------------------------------------------------
HRESULT OnCreateDevice( HWND hDlg )
{
    HRESULT hr;
    BOOL    bExclusive;
    BOOL    bForeground;
    BOOL    bImmediate;
    DWORD   dwCoopFlags;

    // Cleanup any previous call first
    KillTimer( hDlg, 0 );    
    FreeDirectInput();

    // Detrimine where the buffer would like to be allocated 
    bExclusive         = ( IsDlgButtonChecked( hDlg, IDC_EXCLUSIVE  ) == BST_CHECKED );
    bForeground        = ( IsDlgButtonChecked( hDlg, IDC_FOREGROUND ) == BST_CHECKED );
    bImmediate         = ( IsDlgButtonChecked( hDlg, IDC_IMMEDIATE  ) == BST_CHECKED );

    if( bExclusive )
        dwCoopFlags = DISCL_EXCLUSIVE;
    else
        dwCoopFlags = DISCL_NONEXCLUSIVE;

    if( bForeground )
        dwCoopFlags |= DISCL_FOREGROUND;
    else
        dwCoopFlags |= DISCL_BACKGROUND;

    // Create a DInput object
    if( FAILED( hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION, 
                                         IID_IDirectInput8, (VOID**)&g_pDI, NULL ) ) )
        return hr;
    
    // Obtain an interface to the system mouse device.
    if( FAILED( hr = g_pDI->CreateDevice( GUID_SysMouse, &g_pMouse, NULL ) ) )
        return hr;
    
    // Set the data format to "mouse format" - a predefined data format 
    //
    // A data format specifies which controls on a device we
    // are interested in, and how they should be reported.
    //
    // This tells DirectInput that we will be passing a
    // DIMOUSESTATE2 structure to IDirectInputDevice::GetDeviceState.
    if( FAILED( hr = g_pMouse->SetDataFormat( &c_dfDIMouse2 ) ) )
        return hr;
    
    // Set the cooperativity level to let DirectInput know how
    // this device should interact with the system and with other
    // DirectInput applications.
    hr = g_pMouse->SetCooperativeLevel( hDlg, dwCoopFlags );
    if( hr == DIERR_UNSUPPORTED && !bForeground && bExclusive )
    {
        FreeDirectInput();
        MessageBox( hDlg, _T("SetCooperativeLevel() returned DIERR_UNSUPPORTED.\n")
                          _T("For security reasons, background exclusive mouse\n")
                          _T("access is not allowed."), 
                          _T("Mouse"), MB_OK );
        return S_OK;
    }

    if( FAILED(hr) )
        return hr;

    if( !bImmediate )
    {
        // IMPORTANT STEP TO USE BUFFERED DEVICE DATA!
        //
        // DirectInput uses unbuffered I/O (buffer size = 0) by default.
        // If you want to read buffered data, you need to set a nonzero
        // buffer size.
        //
        // Set the buffer size to SAMPLE_BUFFER_SIZE (defined above) elements.
        //
        // The buffer size is a DWORD property associated with the device.
        DIPROPDWORD dipdw;
        dipdw.diph.dwSize       = sizeof(DIPROPDWORD);
        dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
        dipdw.diph.dwObj        = 0;
        dipdw.diph.dwHow        = DIPH_DEVICE;
        dipdw.dwData            = SAMPLE_BUFFER_SIZE; // Arbitary buffer size

        if( FAILED( hr = g_pMouse->SetProperty( DIPROP_BUFFERSIZE, &dipdw.diph ) ) )
            return hr;
    }

    // Acquire the newly created device
    g_pMouse->Acquire();

    // Set a timer to go off 12 times a second, to read input
    // Note: Typically an application would poll the mouse
    //       much faster than this, but this slow rate is simply 
    //       for the purposes of demonstration
    SetTimer( hDlg, 0, 1000 / 12, NULL );

    return S_OK;
}
示例#9
0
//-----------------------------------------------------------------------------
// Name: MainDialogProc
// Desc: Handles dialog messages
//-----------------------------------------------------------------------------
INT_PTR CALLBACK MainDialogProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
{
    HRESULT hr;

    switch( msg ) 
    {
        case WM_INITDIALOG:
        {
            // Set the icon for this dialog.
            HICON hIcon = LoadIcon( (HINSTANCE)GetModuleHandle(NULL), 
                                    MAKEINTRESOURCE( IDI_ICON ) );
            PostMessage( hDlg, WM_SETICON, ICON_BIG,   (LPARAM)hIcon );  // Set big icon
            PostMessage( hDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIcon );  // Set small icon

            EnableWindow( GetDlgItem( hDlg, IDC_PLAY_EFFECTS ), FALSE );

            hr = InitDirectInput( hDlg );
            if( FAILED(hr) )
            {
                MessageBox( NULL, _T("Error Initializing DirectInput. ")
                                  _T("The sample will now exit."),
                                  _T("ReadFFE"), MB_ICONERROR | MB_OK );                
                EndDialog( hDlg, TRUE );
            }
            return TRUE;
        }

        case WM_COMMAND:
            switch( LOWORD(wParam) )
            {
                case IDCANCEL:
                    EndDialog( hDlg, FALSE ); 
                    return TRUE;

                case IDC_READ_FILE:
                    if( FAILED( hr = OnReadFile( hDlg ) ) )
                    {
                        MessageBox( NULL, _T("Error reading effects file."),
                                          _T("ReadFFE"), MB_ICONERROR | MB_OK );
                        EnableWindow( GetDlgItem( hDlg, IDC_PLAY_EFFECTS ), FALSE );
                    }
                    return TRUE;

                case IDC_PLAY_EFFECTS:
                    if( FAILED( hr = OnPlayEffects( hDlg ) ) )
                    {
                        MessageBox( NULL, _T("Error playing DirectInput effects. ")
                                          _T("The sample will now exit."), 
                                          _T("ReadFFE"), MB_ICONERROR | MB_OK );
                        EndDialog( hDlg, 1 );
                    }
                    return TRUE;
            }
            break;

        case WM_DESTROY:
            FreeDirectInput();   
            return TRUE;
    }

    return FALSE;
}
示例#10
0
int WINAPI WinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpCmdLine,
	int nCmdShow)
{
	RECT rc;
	int nWidth;
	int nHeight;

	HWND hWndTS = FindTSWindow();
	if(hWndTS != NULL) {
		GetWindowRect(hWndTS, &rc);
		nWidth = rc.right - rc.left;
		nHeight = rc.bottom - rc.top;
	} else {
		ExitProcess(0);
	}

	bool bUseJoystick = true;
	int nArgCount;
	LPSTR *pArgList;

	pArgList = CommandLineToArgvA(GetCommandLine(), &nArgCount);
	for (int i = 0; i < nArgCount; ++i)
	{
		if (pArgList[i][0] != '-')
			continue;

		if (strcmp(pArgList[i]+1, "j") == 0)
			bUseJoystick = !bUseJoystick;

		if (strcmp(pArgList[i]+1, "v") == 0)
			ToggleDisplaySection(0);

		if (strcmp(pArgList[i]+1, "f") == 0)
			ToggleFontOutline();

		if (strcmp(pArgList[i]+1, "s") == 0)
			ToggleSpeedLimitOnly();

		if (pArgList[i][1] == 'm')
		{
			if (isdigit(pArgList[i][2]))
			{
				int nSection = atoi(pArgList[i]+2);
				if (nSection >= 1 && nSection <= 12)
					ToggleDisplaySection(nSection);
			}
		}

		if (pArgList[i][1] == 's')
		{
			if (isdigit(pArgList[i][2]))
			{
				int nSection = atoi(pArgList[i]+2);
				if (nSection >= 1 && nSection <= 12)
					ToggleDisplaySection(nSection + 12);
			}
		}
	}

	margin.cxRightWidth = nWidth;
	margin.cyBottomHeight = nHeight;

	WNDCLASSEX wc;
	ZeroMemory(&wc, sizeof(WNDCLASSEX));

	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WindowProc;
	wc.hInstance = hInstance;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)RGB(0,0,0);
	wc.lpszClassName = "WindowClass";

	RegisterClassEx(&wc);

	hWnd = CreateWindowEx(0,
		"WindowClass",
		"TrainSim Helper",
		WS_EX_TOPMOST | WS_POPUP,
		rc.left, rc.top,
		nWidth, nHeight,
		NULL,
		NULL,
		hInstance,
		NULL);

	SetWindowLong(hWnd, GWL_EXSTYLE,(int)GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED |WS_EX_TRANSPARENT);
	SetLayeredWindowAttributes(hWnd, RGB(0,0,0), 255, ULW_COLORKEY | LWA_ALPHA);

	ShowWindow(hWnd, nCmdShow);

	InitD3D(hWnd, nWidth, nHeight);
	MSG msg;
	::SetWindowPos(hWndTS, HWND_NOTOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);

	// V for the whole overlay
	RegisterHotKey(hWnd, 0, MOD_SHIFT | MOD_ALT, 0x56 /* V key */);

	// F1-F12 for the main overlay
	for (int i = 1; i <= 12; ++i)
		RegisterHotKey(hWnd, i, MOD_SHIFT | MOD_ALT, VK_F1 + i - 1);

	// F1-F12 for the steam overlay
	for (int i = 13; i <= 24; ++i)
		RegisterHotKey(hWnd, i, MOD_SHIFT | MOD_CONTROL, VK_F1 + i - 13);

	// 0-9 digits for the countdown
	for (int i = 100; i <= 109; ++i)
		RegisterHotKey(hWnd, i, MOD_SHIFT | MOD_ALT, 0x30 + i - 100);

	// R for the countdown reset
	RegisterHotKey(hWnd, 110, MOD_SHIFT | MOD_ALT, 0x52 /* R key */);

	// D for driving direction
	RegisterHotKey(hWnd, 201, MOD_SHIFT | MOD_ALT, 0x44 /* D key */);

	// F for font outline
	RegisterHotKey(hWnd, 202, MOD_SHIFT | MOD_ALT, 0x46 /* F key */);

	// S for speed limit only
	RegisterHotKey(hWnd, 203, MOD_SHIFT | MOD_ALT, 0x53 /* S key */);

	if (bUseJoystick)
		if (FAILED(InitDirectInput()))
			ExitProcess(0);

	bool fDone = false;

	while(!fDone)
	{
		hWndTS = FindTSWindow();
		if (!hWndTS)
		{
			msg.wParam = 0;
			break;
		}

		RECT rcNew;
		GetWindowRect(hWndTS, &rcNew);
		if (rcNew.left != rc.left || rcNew.top != rc.top)
		{
			rc = rcNew;
			MoveWindow(hWnd, rc.left, rc.top, nWidth, nHeight, FALSE);
		}

		::SetWindowPos(hWnd, HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
		Sleep(10);

		RenderOverlay();
		if (bUseJoystick)
			UpdateJoystick();

		while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);

			switch(msg.message)
			{
			case WM_QUIT:
				fDone = true;
				break;
			}
		}
	}

	if (bUseJoystick)
		FreeDirectInput();

	return msg.wParam;
}
示例#11
0
	~DInputPad() { FreeDirectInput(); }
CDirectInput8Manager::~CDirectInput8Manager(void)
{
	FreeDirectInput();
}
示例#13
0
 ~impl()
 {
     FreeDirectInput();
 }
示例#14
0
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: Entry point for the application.  Since we use a simple dialog for 
//       user interaction we don't need to pump messages.
//-----------------------------------------------------------------------------
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, int )
{
    HRESULT hr;
    HWND    hWnd;
    BOOL    bDone;
    DWORD   dwResult;
    MSG     msg;

    // Initialize global varibles
    if ( FAILED( hr = InitVariables() ) )
    {
        MessageBox( NULL, _T("Error Initializing Variables"), 
                    _T("Scrawl"),  MB_ICONERROR | MB_OK );
        return TRUE;
    }

    // Display the main dialog box.
    hWnd = RegisterWindowClass( hInstance );
    if( NULL == hWnd )
    {
        MessageBox( NULL, _T("Error Creating Window"), 
                    _T("Scrawl"), MB_ICONERROR | MB_OK );
        return TRUE;
    }

    // Start message pump. Since we use notification handles, we need to use
    // MsgWaitForMultipleObjects() to wait for the event or a message, 
    // whichever comes first.

    bDone = FALSE;
    while( !bDone ) 
    {
        dwResult = MsgWaitForMultipleObjects( 1, &g_hMouseEvent, 
                                              FALSE, INFINITE, QS_ALLINPUT );

        switch( dwResult ) 
        {
            // WAIT_OBJECT_0 + 0 means that g_hevtMouse was signalled 
            case WAIT_OBJECT_0 + 0:
                OnMouseInput( hWnd );
                break;

            // WAIT_OBJECT_0 + 1 means that we have messages to process 
            case WAIT_OBJECT_0 + 1:
                while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) 
                {
                    if( msg.message == WM_QUIT ) 
                    {
                        // Stop loop if it's a quit message
                        bDone = TRUE;
                    } 
                    else 
                    {
                        TranslateMessage( &msg );
                        DispatchMessage( &msg );
                    }
                }
                break;
        }
    }

    // Clean up
    FreeDirectInput();

    // Delete bitmaps
    if( g_hDC ) 
    {
        if( g_hbmpDeselect ) 
            SelectObject( g_hDC, g_hbmpDeselect );

        DeleteDC( g_hDC );
    }

    if( g_hBitmap ) 
        DeleteObject( g_hBitmap );

    return TRUE;
}
示例#15
0
CKeyInput::~CKeyInput(void)
{
    FreeDirectInput();
}