[ create a new paste ] login | about

Link: http://codepad.org/rwFpj0qt    [ raw code | fork ]

Plain Text, pasted on Jan 19:
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//
// このコードの利用に際して:
//  2ちゃんねる、ニュー速VIP・プログラミングスレの連中であれば
//  自己責任において NYSL(= 煮るなり焼くなり好きにしろライセンス)
//   その他の人間はちょっと何か、スレで一言くらい言ってくれ
// 

// ------------------------------------------------------------- joy_ctrl.h
#ifndef _JOYSTICK_CTRL_H_
#define _JOYSTICK_CTRL_H_

#include <windows.h>

/**
 * JoyStick(JoyPad) コントロールクラス(PImpl) by ミングスレの名無し
 * 
 * 使用方法:
 *  WinMain 内で親ウィンドウのハンドル渡して
 *   start で放置。以後、内部の別スレッドがJoyPadを監視して
 *   WM_CHAR を飛ばす。十字キー=テンキー、その他のボタン A~
 *   大分マシになったので猫満足。なるほど味の差。
 */
class JoyStick {
private:
	void *pjc;
public:
	JoyStick(HWND hWnd);
	~JoyStick();

	// 十字ボタンのリピート、その他ボタンのリピートをそれぞれ行うか?
	// デフォルト:十字はリピートする、その他はしない
	void setRepeatParam(bool repeat_stick, bool repeat_button);

	// リピートの速さ:millisとか書いてるがそんな早くない。だってsleepしてるもの
	void setRepeatTime(DWORD millis);

	// 監視とメッセージ送信のスレッド開始
	void start();
};

#endif



// ------------------------------------------------------------- joy_ctrl.cpp

#include "joy_ctrl.h"

#define DIRECTINPUT_VERSION 0x0800
#ifndef _WIN32_DCOM
#define _WIN32_DCOM
#endif

#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "dinput8.lib")
#pragma comment(lib, "winmm.lib")

#include <dinput.h>
#include <dinputd.h>
#include <mmsystem.h>

#include <boost/thread.hpp>
#include <boost/function.hpp>

#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }

// 128個のボタンと二軸まで
#define JS_TARGET_KEY_NUM 128+2
#define JS_X_AXIS 0
#define JS_Y_AXIS 1
#define JS_BUTTONS 2

#define JS_REPEAT_TIME 200
#define JS_SLEEP_TIME 30

BOOL CALLBACK enumObjCallback( const DIDEVICEOBJECTINSTANCE* pdidoi, VOID* pContext );
BOOL CALLBACK enumJSCallback( const DIDEVICEINSTANCE* pdidInstance, VOID* pContext );

// --------------------------------------------------------------------------------
// --
struct DI_ENUM_CONTEXT
{
	DIJOYCONFIG* pJoyCfg;
	bool bJoyCfgValid;

	LPDIRECTINPUT8 *ppDI;
	LPDIRECTINPUTDEVICE8 *ppDevice;
};

struct JS_KEY_TIME_STATE {
	DWORD dwKey;
	DWORD dwTime;
};

// --------------------------------------------------------------------------------
// --
class _P_JoyCtrl {
private:
	typedef boost::mutex::scoped_lock lock;
	boost::mutex mtx;
	boost::thread *th;

	HWND hWnd;
	HRESULT hr;
	volatile bool bLoop;
	volatile bool bUseRepeat1;
	volatile bool bUseRepeat2;
	volatile DWORD dwRepeat;

	LPDIRECTINPUT8 pDI;
	LPDIRECTINPUTDEVICE8 pDevice;
	JS_KEY_TIME_STATE kts[JS_TARGET_KEY_NUM];

	HRESULT init_dinput();
	HRESULT send_dinput_state();
	void free_dinput();

	void thread_run();
public:
	_P_JoyCtrl(HWND);
	~_P_JoyCtrl();
	void start();
	void stop();

	void setRepeatTime(DWORD);
	__declspec(property(put=setRepeatTime)) DWORD repeat_time;

	void useRepeatStick(bool);
	__declspec(property(put=useRepeatStick)) bool repeat_stick;

	void useRepeatButton(bool);
	__declspec(property(put=useRepeatButton)) bool repeat_button;
};

// --------------------------------------------------------------------------------
// --
_P_JoyCtrl::_P_JoyCtrl(HWND hTarget)
	: th(NULL), pDI(NULL), pDevice(NULL)
{
	hWnd = hTarget;
	memset(kts, 0, sizeof(JS_KEY_TIME_STATE)*JS_TARGET_KEY_NUM);

	dwRepeat = JS_REPEAT_TIME;
	bUseRepeat1 = true;
	bUseRepeat2 = false;

	hr = init_dinput();
}

_P_JoyCtrl::~_P_JoyCtrl()
{
	stop();
	th->join();
	delete th;
	free_dinput();
}

void _P_JoyCtrl::setRepeatTime(DWORD millis)
{
	lock l(mtx);
	dwRepeat = millis;
}

void _P_JoyCtrl::useRepeatStick(bool bYesNo)
{
	lock l(mtx);
	bUseRepeat1 = bYesNo;
}

void _P_JoyCtrl::useRepeatButton(bool bYesNo)
{
	lock l(mtx);
	bUseRepeat2 = bYesNo;
}

void _P_JoyCtrl::start()
{
	if(FAILED(hr)) throw std::runtime_error("error: can't start");
	if(th != NULL) return;

	bLoop = true;
	boost::function<void (void)> f = boost::bind(&_P_JoyCtrl::thread_run, this);
	th = new boost::thread(f);
}

void _P_JoyCtrl::stop()
{
	lock l(mtx);
	bLoop = false;
}

void _P_JoyCtrl::thread_run()
{
	while(bLoop)
	{
		boost::thread::sleep(
			boost::get_system_time()
			+ boost::posix_time::milliseconds(JS_SLEEP_TIME)
		);

		lock l(mtx);

		if(FAILED(hr = send_dinput_state()))
		{
			bLoop = false;
			break;
		}
	}
}

HRESULT _P_JoyCtrl::init_dinput()
{
	IDirectInputJoyConfig8* pJoyConfig = NULL;
	if(
		FAILED(hr = DirectInput8Create(
			(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
			DIRECTINPUT_VERSION,
			IID_IDirectInput8, ( VOID** )&pDI, NULL
		))
		|| FAILED(hr = pDI->QueryInterface( IID_IDirectInputJoyConfig8, (void**)&pJoyConfig ))
	) return hr;

	DIJOYCONFIG prefConf = {0};

	DI_ENUM_CONTEXT enCon;
	enCon.pJoyCfg = &prefConf;
	enCon.bJoyCfgValid = false;
	enCon.ppDI = &pDI;
	enCon.ppDevice = &pDevice;

	prefConf.dwSize = sizeof( prefConf );
	if(SUCCEEDED(pJoyConfig->GetConfig(0, &prefConf, DIJC_GUIDINSTANCE) )) enCon.bJoyCfgValid = true;
	SAFE_RELEASE( pJoyConfig );

	if(FAILED(
		hr = pDI->EnumDevices(DI8DEVCLASS_GAMECTRL, enumJSCallback, &enCon, DIEDFL_ATTACHEDONLY)
	)) return hr;

	hr = E_FAIL;
	if(
		NULL == pDevice
		|| FAILED( hr = pDevice->SetDataFormat( &c_dfDIJoystick2 ) )
		|| FAILED( hr = pDevice->SetCooperativeLevel( hWnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND ) )
		|| FAILED( hr = pDevice->EnumObjects( enumObjCallback, &enCon, DIDFT_ALL ) )
	) return hr;

	return S_OK;
}

HRESULT _P_JoyCtrl::send_dinput_state()
{
	if( NULL == pDevice ) return S_OK;

	if(FAILED(hr = pDevice->Poll()))
	{
		hr = pDevice->Acquire();
		while( hr == DIERR_INPUTLOST ) hr = pDevice->Acquire();
		return S_OK;
	}

	DIJOYSTATE2 js;
	if(
		FAILED(hr = pDevice->GetDeviceState(sizeof(DIJOYSTATE2), &js))
	) return hr;

	// URLB
	// 0001
	const char KEYCHAR[] = {'0','2','4','1',  '6','3','0','0',  '8','0','7','0',  '9','0','0','0'};
	const char BTNCHAR[] = {'A','B','C','D','E',  'F','G','H','I','J'};

	DWORD dw = timeGetTime();
	BYTE blg = 0;

	if(
		kts[JS_X_AXIS].dwKey != js.lX
		|| (bUseRepeat1 && (dw - kts[JS_X_AXIS].dwTime) > dwRepeat)
	) {
		if(js.lX < LONG(-10)) blg |= 2;
		else if(js.lX > LONG(10)) blg |= 4;

		kts[JS_X_AXIS].dwKey = js.lX;
		kts[JS_X_AXIS].dwTime = dw;
	}

	if(
		kts[JS_Y_AXIS].dwKey != js.lY
		|| (bUseRepeat1 && (dw - kts[JS_Y_AXIS].dwTime) > dwRepeat)
	) {
		if(js.lY < LONG(-10)) blg |= 8;
		else if(js.lY > LONG(10)) blg |= 1;

		kts[JS_Y_AXIS].dwKey = js.lY;
		kts[JS_Y_AXIS].dwTime = dw;
	}

	if(blg) SendMessage(hWnd, WM_CHAR, WPARAM(KEYCHAR[blg]), LPARAM(0));

	for(int i=0, ln=sizeof(BTNCHAR)/sizeof(char); i<ln; i++)	// ln=128
	{
		if(js.rgbButtons[i] & 0x80)
		{
			if(
				!kts[JS_BUTTONS+i].dwKey
				|| (bUseRepeat2 && (dw - kts[JS_BUTTONS+i].dwTime) > dwRepeat)
			) {
				SendMessage(hWnd, WM_CHAR, WPARAM(BTNCHAR[i]), LPARAM(0));	// 'A'+i

				kts[JS_BUTTONS+i].dwKey = 1;
				kts[JS_BUTTONS+i].dwTime = dw;
			}
		} else kts[JS_BUTTONS+i].dwKey = 0;
	}

	return S_OK;
}

void _P_JoyCtrl::free_dinput()
{
	if( pDevice ) pDevice->Unacquire();

	SAFE_RELEASE( pDevice );
	SAFE_RELEASE( pDI );
}

// --------------------------------------------------------------------------------
// --
BOOL CALLBACK enumJSCallback(
	const DIDEVICEINSTANCE* pDDev, void* pAny
) {
	DI_ENUM_CONTEXT* pEnCon = (DI_ENUM_CONTEXT*)pAny;
	if(
		pEnCon->bJoyCfgValid
		&& !IsEqualGUID(pDDev->guidInstance, pEnCon->pJoyCfg->guidInstance)
	) return DIENUM_CONTINUE;

	HRESULT hr;
	if(FAILED(
		hr = (*(pEnCon->ppDI))->CreateDevice( pDDev->guidInstance, pEnCon->ppDevice, NULL )
	)) return DIENUM_CONTINUE;

	return DIENUM_STOP;
}

BOOL CALLBACK enumObjCallback(
	const DIDEVICEOBJECTINSTANCE* pdidoi, void* pAny
){
	DI_ENUM_CONTEXT* pEnCon = (DI_ENUM_CONTEXT*)pAny;

	if( pdidoi->dwType & DIDFT_AXIS )
	{
		DIPROPRANGE diprg;
		diprg.diph.dwSize = sizeof( DIPROPRANGE );
		diprg.diph.dwHeaderSize = sizeof( DIPROPHEADER );
		diprg.diph.dwHow = DIPH_BYID;
		diprg.diph.dwObj = pdidoi->dwType;
		diprg.lMin = -1000;
		diprg.lMax = +1000;

		if( FAILED( (*(pEnCon->ppDevice))->SetProperty( DIPROP_RANGE, &diprg.diph ) ) ) return DIENUM_STOP;
	}

	static unsigned int FLG = 0;	// TODO: 現在放置
	if( pdidoi->guidType == GUID_XAxis ) FLG |= 1;
	if( pdidoi->guidType == GUID_YAxis ) FLG |= 2;
	if( pdidoi->guidType == GUID_ZAxis ) FLG |= 4;
	if( pdidoi->guidType == GUID_RxAxis ) FLG |= 8;
	if( pdidoi->guidType == GUID_RyAxis ) FLG |= 16;
	if( pdidoi->guidType == GUID_RzAxis ) FLG |= 32;

	return DIENUM_CONTINUE;
}

// --------------------------------------------------------------------------------
// --
JoyStick::JoyStick(HWND hWnd)
{
	pjc = new _P_JoyCtrl(hWnd);
}

JoyStick::~JoyStick()
{
	delete reinterpret_cast<_P_JoyCtrl*>(pjc);
}

void JoyStick::setRepeatParam(bool repeat_stick, bool repeat_button)
{
	_P_JoyCtrl *p = reinterpret_cast<_P_JoyCtrl*>(pjc);
	p->repeat_stick = repeat_stick;
	p->repeat_button = repeat_button;
}

void JoyStick::setRepeatTime(DWORD millis)
{
	reinterpret_cast<_P_JoyCtrl*>(pjc)->repeat_time = millis;
}

void JoyStick::start()
{
	reinterpret_cast<_P_JoyCtrl*>(pjc)->start();
}



Create a new paste based on this one


Comments: