summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputJoystick.h
blob: a42a7cb4a993ea97bd93b54ae442a5829be01d21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifndef _CIFACE_DIRECTINPUT_JOYSTICK_H_
#define _CIFACE_DIRECTINPUT_JOYSTICK_H_

#include "../ControllerInterface.h"

#define DIRECTINPUT_VERSION 0x0800
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#include <dinput.h>

#ifdef CIFACE_USE_XINPUT
	// this takes so long, idk if it should be enabled :(
	#define NO_DUPLICATE_DINPUT_XINPUT
	#include <wbemidl.h>
	#include <oleauto.h>
#endif

namespace ciface
{
namespace DirectInput
{

void InitJoystick( IDirectInput8* const idi8, std::vector<ControllerInterface::Device*>& devices/*, HWND hwnd*/ );

class Joystick : public ControllerInterface::Device
{
	friend class ControllerInterface;
	friend class ControllerInterface::ControlReference;

protected:

	struct EffectState
	{
		EffectState( LPDIRECTINPUTEFFECT eff ) : changed(0), iface(eff) {}
		LPDIRECTINPUTEFFECT			iface;
		ControlState				magnitude;
		bool						changed;
	};

	class Input : public ControllerInterface::Device::Input
	{
		friend class Joystick;
	protected:
		virtual ControlState GetState( const DIJOYSTATE* const joystate ) = 0;
	};

	// can probably eliminate this base class
	class Output : public ControllerInterface::Device::Output
	{
		friend class Joystick;
	protected:
		virtual void SetState( const ControlState state, EffectState* const joystate ) = 0;
	};

	class Button : public Input
	{
		friend class Joystick;
	public:
		std::string GetName() const;
	protected:
		Button( const unsigned int index ) : m_index(index) {}
		ControlState GetState( const DIJOYSTATE* const joystate );
	private:
		const unsigned int	m_index;
	};

	class Axis : public Input
	{
		friend class Joystick;
	public:
		std::string GetName() const;
	protected:
		Axis( const unsigned int index, const LONG base, const LONG range ) : m_index(index), m_base(base), m_range(range) {}
		ControlState GetState( const DIJOYSTATE* const joystate );
	private:
		const unsigned int	m_index;
		const LONG			m_base;
		const LONG			m_range;
	};

	class Hat : public Input
	{
		friend class Joystick;
	public:
		std::string GetName() const;
	protected:
		Hat( const unsigned int index, const unsigned int direction ) : m_index(index), m_direction(direction) {}
		ControlState GetState( const DIJOYSTATE* const joystate );
	private:
		const unsigned int	m_index;
		const unsigned int	m_direction;
	};

	class Force : public Output
	{
		friend class Joystick;
	public:
		std::string GetName() const;
	protected:
		Force( const unsigned int index ) : m_index(index) {}
		void SetState( const ControlState state, EffectState* const joystate );
	private:
		const unsigned int	m_index;
	};

	bool UpdateInput();
	bool UpdateOutput();

	ControlState GetInputState( const ControllerInterface::Device::Input* const input );
	void SetOutputState( const ControllerInterface::Device::Output* const input, const ControlState state );

	void ClearInputState();

public:
	Joystick( /*const LPCDIDEVICEINSTANCE lpddi, */const LPDIRECTINPUTDEVICE8 device, const unsigned int index );
	~Joystick();
	
	std::string GetName() const;
	int GetId() const;
	std::string GetSource() const;

private:
	const LPDIRECTINPUTDEVICE8			m_device;
	const unsigned int					m_index;
	//const std::string					m_name;

	DIJOYSTATE							m_state_in;
	std::vector<EffectState>			m_state_out;

	bool								m_must_poll;
};

}
}

#endif