Game Accessibility Library logo SourceForge.net Logo
Game Accessibility Suite: CAT/CATDirectInput.cpp Source File

CATDirectInput.cpp

Go to the documentation of this file.
00001 /// \file  CATDirectInput.h
00002 /// \brief Basic DirectInput wrapper
00003 /// \ingroup CAT
00004 ///
00005 /// Copyright (c) 2002-2008 by Michael Ellison.
00006 /// See COPYING.txt for the \ref gaslicense License (MIT License).
00007 ///
00008 // $Author: mikeellison $
00009 // $Date: 2008-01-23 06:02:38 -0600 (Wed, 23 Jan 2008) $
00010 // $Revision:   $
00011 // $NoKeywords: $
00012 
00013 #include "CATInternal.h"
00014 #include "CATDirectInput.h"
00015 #include "CATDIJoystick.h"
00016 
00017 // Constructor
00018 CATDirectInput::CATDirectInput()
00019 {
00020     fDirectInput    = 0;    
00021     fInitialized    = false;
00022     fUserCallback   = 0;
00023     fUserParam      = 0;
00024 }
00025 
00026 // Initialize direct input
00027 bool CATDirectInput::Init()
00028 {
00029     if (DI_OK == DirectInput8Create((HINSTANCE)::GetModuleHandle(0) ,DIRECTINPUT_VERSION,IID_IDirectInput8W, (void**)&fDirectInput, NULL))
00030     {
00031         fInitialized = true;
00032     }
00033     return fInitialized;
00034 }
00035 
00036 CATDirectInput::~CATDirectInput()
00037 {
00038     if (fDirectInput)
00039         fDirectInput->Release();
00040     
00041     fDirectInput = 0;
00042 
00043     while (fJoystickList.size())
00044     {
00045         DIDEVICEINSTANCE* inputDevInfo = fJoystickList[0];
00046         delete inputDevInfo;
00047         fJoystickList.erase(fJoystickList.begin());
00048     }
00049 
00050 }
00051 
00052 // Start the control panel for direct input
00053 void CATDirectInput::DoControlPanel(HWND parent)
00054 {
00055     if (fDirectInput)
00056     {
00057         fDirectInput->RunControlPanel(parent, 0);
00058     }
00059 }
00060 
00061 // Enumerate joysticks and callback the caller
00062 void CATDirectInput::EnumJoysticks(EnumJoysticksCallback callback, void *userParam)
00063 {
00064     fUserCallback = callback;
00065     fUserParam = userParam;
00066 
00067     this->fDirectInput->EnumDevices(
00068                         DI8DEVCLASS_GAMECTRL, 
00069                         DIJoysticksCB, 
00070                         this, 
00071                         DIEDFL_ATTACHEDONLY);
00072 }
00073 
00074 
00075 // Static direct input callback
00076 BOOL CALLBACK CATDirectInput::DIJoysticksCB(    LPCDIDEVICEINSTANCE lpddi,  LPVOID pvRef )
00077 {
00078     CATDirectInput* cdi = (CATDirectInput*)pvRef;
00079     
00080     // Copy device instance data and add to list
00081     DIDEVICEINSTANCE* inputDevInfo = new DIDEVICEINSTANCE;
00082     memcpy(inputDevInfo,lpddi,sizeof(DIDEVICEINSTANCE));
00083     cdi->fJoystickList.push_back(inputDevInfo);
00084     
00085     // Call user callback
00086     if (cdi->fUserCallback)
00087     {
00088         cdi->fUserCallback(inputDevInfo->tszInstanceName, cdi->fUserParam);
00089     }
00090 
00091     return TRUE;
00092 }
00093 
00094 
00095 // Create a joystick interface
00096 CATJoystick* CATDirectInput::CreateJoystick(const CATWChar* name)
00097 {   
00098     for (CATUInt32 i = 0; i < fJoystickList.size(); i++)
00099     {
00100         DIDEVICEINSTANCE* inputDevInfo;
00101         if (inputDevInfo = fJoystickList[i])
00102         {
00103             if (0 == _wcsicmp(inputDevInfo->tszInstanceName,name))
00104             {
00105                 CATDIJoystick* joystick = 0;
00106                 joystick    = new CATDIJoystick(this->fDirectInput);
00107                 if (joystick->Init(inputDevInfo))
00108                 {
00109                     return joystick;
00110                 }               
00111                 delete joystick;
00112                 joystick = 0;
00113             }
00114         }
00115     }
00116 
00117     return 0;
00118 }

Generated on Mon Feb 11 04:09:42 2008 for Game Accessibility Suite by doxygen 1.5.4