summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon/Src/DirectInputBase.cpp
blob: b9a7fee33d22dab40efe450e993029787709cd0f (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (C) 2003 Dolphin Project.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License 2.0 for more details.

// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/

// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/



// Include
// -------------------
#include "DirectInputBase.h"



DInput::DInput()
	: g_pDI(NULL),
	g_pKeyboard(NULL)
{}


DInput::~DInput()
{
	Free();
}

void DInput::DIKToString(unsigned int keycode, char *keyStr)
{
	switch(keycode) {
		case DIK_RETURN:
			sprintf(keyStr, "Enter");
			break;
		case DIK_LCONTROL:
			sprintf(keyStr, "Left Ctrl");
			break;
		case DIK_RCONTROL:
			strcpy(keyStr, "Right Ctrl");
			break;
		case DIK_LSHIFT:
			sprintf(keyStr, "Left Shift");
			break;
		case DIK_RSHIFT:
			sprintf(keyStr, "Right Shift");
			break;
		case DIK_LMENU:
			sprintf(keyStr, "Left Alt");
			break;
		case DIK_RMENU:
			strcpy(keyStr, "Right Alt");
			break;
		case DIK_UP:
			sprintf(keyStr, "Up");
			break;
		case DIK_DOWN:
			sprintf(keyStr, "Down");
			break;
		case DIK_LEFT:
			sprintf(keyStr, "Left");
			break;
		case DIK_RIGHT:
			sprintf(keyStr, "Right");
			break;
		case DIK_HOME:
			strcpy(keyStr, "Home");
			break;
		case DIK_END:
			strcpy(keyStr, "End");
			break;
		case DIK_INSERT:
			strcpy(keyStr, "Ins");
			break;
		case DIK_DELETE:
			strcpy(keyStr, "Del");
			break;
		case DIK_PGUP:
			strcpy(keyStr, "PgUp");
			break;
		case DIK_PGDN:
			strcpy(keyStr, "PgDn");
			break;
		case DIK_NUMLOCK:
			strcpy(keyStr, "Num Lock");
			break;
		case DIK_NUMPAD0:
			strcpy(keyStr, "Num 0");
			break;
		case DIK_NUMPAD1:
			strcpy(keyStr, "Num 1");
			break;
		case DIK_NUMPAD2:
			strcpy(keyStr, "Num 2");
			break;
		case DIK_NUMPAD3:
			strcpy(keyStr, "Num 3");
			break;
		case DIK_NUMPAD4:
			strcpy(keyStr, "Num 4");
			break;
		case DIK_NUMPAD5:
			strcpy(keyStr, "Num 5");
			break;
		case DIK_NUMPAD6:
			strcpy(keyStr, "Num 6");
			break;
		case DIK_NUMPAD7:
			strcpy(keyStr, "Num 7");
			break;
		case DIK_NUMPAD8:
			strcpy(keyStr, "Num 8");
			break;
		case DIK_NUMPAD9:
			strcpy(keyStr, "Num 9");
			break;
		case DIK_DIVIDE:
			strcpy(keyStr, "Num /");
			break;
		case DIK_NUMPADENTER:
			strcpy(keyStr, "Num Enter");
			break;
		case DIK_DECIMAL:
			strcpy(keyStr, "Num Decimal");
			break;
		case DIK_NUMPADCOMMA:
		case DIK_ABNT_C2:
			strcpy(keyStr, "Num Separator");
			break;
		case DIK_NUMPADEQUALS:
			strcpy(keyStr, "Num =");
			break;
		default:
			// TODO: Switch to unicode GetKeyNameText?
			GetKeyNameTextA(keycode << 16, keyStr, 64);
			break;
	}
}

HRESULT DInput::Init(HWND hWnd)
{
	HRESULT hr;
	DWORD dwCoopFlags;
	dwCoopFlags = DISCL_FOREGROUND | DISCL_NOWINKEY;

	// Create a DInput object
	if (FAILED(hr = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION,
					    IID_IDirectInput8, (VOID* *)&g_pDI, NULL)))
	{
		MessageBox(0, L"Direct Input Create Failed", 0, MB_ICONERROR);
		return(hr);
	}

	if (FAILED(hr = g_pDI->CreateDevice(GUID_SysKeyboard, &g_pKeyboard, NULL)))
	{
		MessageBox(0, L"Couldn't access keyboard", 0, MB_ICONERROR);
		Free();
		return(hr);
	}

	g_pKeyboard->SetDataFormat(&c_dfDIKeyboard);
	g_pKeyboard->SetCooperativeLevel(hWnd, dwCoopFlags);
	g_pKeyboard->Acquire();

	return(S_OK);
}

void DInput::Free()
{
	if (g_pKeyboard)
	{
		g_pKeyboard->Unacquire();
		g_pKeyboard->Release();
		g_pKeyboard = 0;
	}

	if (g_pDI)
	{
		g_pDI->Release();
		g_pDI = 0;
	}
}

// Desc: Read the input device's state when in immediate mode and display it.
HRESULT DInput::Read()
{
	HRESULT hr;

	if (NULL == g_pKeyboard)
	{
		return(S_OK);
	}
	
	// Get the input's device state, and put the state in dims
	ZeroMemory(diks, sizeof(diks));
	hr = g_pKeyboard->GetDeviceState(sizeof(diks), diks);

	//for (int i=0; i<256; i++)
	//	if (diks[i]) MessageBox(0,"DSFJDKSF|",0,0);
	if (FAILED(hr))
	{
		// DirectInput may be telling us that the input stream has been
		// interrupted.  We aren't tracking any state between polls, so
		// we don't have any special reset that needs to be done.
		// We just re-acquire and try again.

		// If input is lost then acquire and keep trying
		hr = g_pKeyboard->Acquire();

		while (hr == DIERR_INPUTLOST)
		{
			hr = g_pKeyboard->Acquire();
		}

		// hr may be DIERR_OTHERAPPHASPRIO or other errors.  This
		// may occur when the app is minimized or in the process of
		// switching, so just try again later
		return(S_OK);
	}

	return(S_OK);
}