//////////////////////////////////////////////////////////////////////////////////
// Project Name: [ CDX Class Library - CDX.lib ]
// Source File:  [ CDXInput Implementation ]
// Author:       [ Danny Farley - danny@jags.co.uk ]
// Revision:     [ 1.4 ]
/////////////////////////////////////////////////////////////////////////////////

unit CDXInputX;

interface

uses Windows, DirectInput8, Classes;

type
  LPDIRECTINPUT = IDirectInput8;
  LPDIRECTINPUTDEVICE = IDirectInputDevice8;

//////////////////////////////////////////////////////////////////////////////////
// CDXInput Class
//////////////////////////////////////////////////////////////////////////////////
  CDXInput = class

  constructor CreateCDXInput;
  destructor DestroyCDXInput;

  function Create(hInst,Window:Integer) : BOOL;
  procedure Restore;
  procedure Release;
  procedure Update;
  public
   m_lpDI : LPDIRECTINPUT;
   m_lpDIDKeyboard : LPDIRECTINPUTDEVICE;
   m_lpDIDMouse : LPDIRECTINPUTDEVICE;

   Mouse : TPOINT;
   MouseLB, MouseMB, MouseRB : Byte;
   Keys : array [0..255] of byte;
end;

implementation

procedure ReleaseObject(x : IUnknown);
begin
  if x <> nil then x := nil;
end;
//////////////////////////////////////////////////////////////////////////////////
// CDXInput Constructor
//////////////////////////////////////////////////////////////////////////////////
constructor CDXInput.CreateCDXInput;
begin
  m_lpDI := nil;
  m_lpDIDKeyboard := nil;
  m_lpDIDMouse := nil;
end;

//////////////////////////////////////////////////////////////////////////////////
// CDXInput Destructor
//////////////////////////////////////////////////////////////////////////////////
destructor CDXInput.DestroyCDXInput;
begin
  m_lpDIDKeyboard.Unacquire;
  ReleaseObject(m_lpDIDKeyboard);
  m_lpDIDMouse.Unacquire;
  ReleaseObject(m_lpDIDMouse);
  ReleaseObject(m_lpDI);
end;

//////////////////////////////////////////////////////////////////////////////////
// CDXInput Create
//////////////////////////////////////////////////////////////////////////////////
function CDXInput.Create(hInst, Window : Integer):BOOL;
var
  rval : HRESULT;
  //mr:MMRESULT;
  Flag : BOOL;
begin
  Flag := False;
  rval := DirectInput8Create(hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, m_lpDI, nil);

  if rval = DI_OK then begin
     m_lpDI.CreateDevice(GUID_SysMouse,m_lpDIDMouse,nil);
     m_lpDI.CreateDevice(GUID_SysKeyboard,m_lpDIDKeyboard,nil);

     m_lpDIDMouse.SetCooperativeLevel(Window,DISCL_NONEXCLUSIVE+DISCL_BACKGROUND);
     m_lpDIDKeyboard.SetCooperativeLevel(Window,DISCL_NONEXCLUSIVE+DISCL_BACKGROUND);

     m_lpDIDMouse.SetDataFormat(c_dfDIMouse);
     m_lpDIDKeyboard.SetDataFormat(c_dfDIKeyboard);
     Flag := True;
     rval := m_lpDIDMouse.Acquire;
     if rval <> DI_OK then Flag := False;

     rval := m_lpDIDKeyboard.Acquire;
     if rval <> DI_OK then Flag := False;
  end;
  Create := Flag;
end;

//////////////////////////////////////////////////////////////////////////////////
// CDXInput Restore
//////////////////////////////////////////////////////////////////////////////////
procedure CDXInput.Restore;
begin
  m_lpDIDMouse.Acquire;
  m_lpDIDKeyboard.Acquire;
end;

//////////////////////////////////////////////////////////////////////////////////
// CDXInput Release
//////////////////////////////////////////////////////////////////////////////////
procedure CDXInput.Release;
begin
  m_lpDIDMouse.Unacquire;
  m_lpDIDKeyboard.Unacquire;
end;

/////////////////////////////////////////////////////////////////////////////////////////////
// CDXInput Update
/////////////////////////////////////////////////////////////////////////////////////////////
procedure CDXInput.Update;
var
  rval : HRESULT;
  MouseState : TDIMOUSESTATE;
begin
  rval := m_lpDIDKeyboard.GetDeviceState(256,@Keys);
  if rval = (DIERR_INPUTLOST or DIERR_NOTACQUIRED) then Restore;
  if (m_lpDIDMouse.GetDeviceState(sizeof(MouseState),@MouseState)=(DIERR_INPUTLOST or DIERR_NOTACQUIRED))
     then Restore;
  Mouse.X := MouseState.lX;
  Mouse.Y := MouseState.lY;
  MouseLB := MouseState.rgbButtons[0];
  MouseRB := MouseState.rgbButtons[1];
  MouseMB := MouseState.rgbButtons[2];
end;

end.

