-
Notifications
You must be signed in to change notification settings - Fork 3
/
Device.cpp
149 lines (129 loc) · 3.54 KB
/
Device.cpp
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
138
139
140
141
142
143
144
145
146
147
148
149
#include "nil.h"
#include "nilUtil.h"
namespace nil {
Device::Device( System* system, DeviceID id, Type type ): mSystem( system ),
mID( id ), mType( type ), mStatus( Status_Pending ),
mSavedStatus( Status_Pending ), mInstance( nullptr ),
mDisconnectFlagged( false )
{
}
void Device::create()
{
if ( mInstance )
return;
if ( getHandler() == Handler_XInput )
{
XInputDevice* xDevice = dynamic_cast<XInputDevice*>( this );
if ( !xDevice )
NIL_EXCEPT( L"Dynamic cast failed for XInputDevice" );
if ( getType() == Device_Controller )
{
mInstance = new XInputController( xDevice );
}
else
NIL_EXCEPT( L"Unsupport device type for XInput; Cannot instantiate device!" );
}
else if ( getHandler() == Handler_DirectInput )
{
DirectInputDevice* diDevice = dynamic_cast<DirectInputDevice*>( this );
if ( !diDevice )
NIL_EXCEPT( L"Dynamic cast failed for DirectInputDevice" );
if ( getType() == Device_Controller )
{
mInstance = new DirectInputController( diDevice );
}
else
NIL_EXCEPT( L"Unsupported device type for DirectInput; Cannot instantiate device!" );
}
else if ( getHandler() == Handler_RawInput )
{
RawInputDevice* rawDevice = dynamic_cast<RawInputDevice*>( this );
if ( !rawDevice )
NIL_EXCEPT( L"Dynamic cast failed for RawInputDevice" );
if ( getType() == Device_Mouse )
{
mInstance = new RawInputMouse( rawDevice );
}
else if ( getType() == Device_Keyboard )
{
mInstance = new RawInputKeyboard( rawDevice );
}
else
NIL_EXCEPT( L"Unsupported device type for RawInput; cannot instantiate device!" );
}
else
NIL_EXCEPT( L"Unsupported device handler; Cannot instantiate device!" );
}
void Device::update()
{
if ( mInstance )
mInstance->update();
}
void Device::destroy()
{
SAFE_DELETE( mInstance );
}
void Device::flagDisconnected()
{
mDisconnectFlagged = true;
}
const bool Device::isDisconnectFlagged() const
{
return mDisconnectFlagged;
}
void Device::onConnect()
{
mStatus = Status_Connected;
wprintf_s( L"Connected: (%d) %s (%s %s)\r\n",
getID(),
getName().c_str(),
getHandler() == Device::Handler_XInput ? L"XInput" : getHandler() == Device::Handler_DirectInput ? L"DirectInput" : L"RawInput",
getType() == Device::Device_Mouse ? L"Mouse" : getType() == Device::Device_Keyboard ? L"Keyboard" : L"Controller" );
create();
}
void Device::onDisconnect()
{
mStatus = Status_Disconnected;
wprintf_s( L"Disconnected: %d\r\n", getID() );
mDisconnectFlagged = false;
destroy();
}
System* Device::getSystem() const
{
return mSystem;
}
const DeviceID Device::getID() const
{
return mID;
}
void Device::saveStatus()
{
mSavedStatus = mStatus;
}
const Device::Status Device::getSavedStatus()
{
return mSavedStatus;
}
const String& Device::getName() const
{
return mName;
}
const Device::Type Device::getType() const
{
return mType;
}
void Device::setStatus( Status status )
{
if ( mStatus != Status_Connected && status == Status_Connected )
create();
mStatus = status;
}
const Device::Status Device::getStatus() const
{
return mStatus;
}
Device::~Device()
{
destroy();
}
}