1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of OGRE
|
---|
4 | (Object-oriented Graphics Rendering Engine)
|
---|
5 | For the latest info, see http://www.ogre3d.org/
|
---|
6 |
|
---|
7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under
|
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
13 | version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
23 | -----------------------------------------------------------------------------
|
---|
24 | */
|
---|
25 | #include "OgreWin32Input8.h"
|
---|
26 |
|
---|
27 | #ifndef OGRE_NO_DX_INPUT
|
---|
28 | #ifndef DX7INPUTONLY
|
---|
29 |
|
---|
30 | #include "OgreRenderWindow.h"
|
---|
31 | #include "OgreLogManager.h"
|
---|
32 | #include "OgreException.h"
|
---|
33 | #include "OgreRoot.h"
|
---|
34 | #include "OgreRenderSystem.h"
|
---|
35 | #include "OgreMouseEvent.h"
|
---|
36 | #include "OgreInputEvent.h"
|
---|
37 | #include "OgreEventQueue.h"
|
---|
38 | #include "OgreCursor.h"
|
---|
39 | #include <dxerr8.h>
|
---|
40 |
|
---|
41 | #define DINPUT_BUFFERSIZE 64
|
---|
42 | //#define DIPROP_BUFFERSIZE 256
|
---|
43 |
|
---|
44 | namespace Ogre {
|
---|
45 | //-----------------------------------------------------------------------
|
---|
46 | Win32Input8::Win32Input8() :
|
---|
47 | InputReader()
|
---|
48 | {
|
---|
49 | mlpDI = 0;
|
---|
50 | mlpDIKeyboard = 0;
|
---|
51 | mlpDIMouse = 0;
|
---|
52 | mEventQueue = 0;
|
---|
53 | mMouseScale = 0.001;
|
---|
54 |
|
---|
55 | memset(mKeyboardBuffer,0,256);
|
---|
56 | }
|
---|
57 | //-----------------------------------------------------------------------
|
---|
58 | Win32Input8::~Win32Input8()
|
---|
59 | {
|
---|
60 | // Shutdown
|
---|
61 | if (mlpDIKeyboard)
|
---|
62 | {
|
---|
63 | mlpDIKeyboard->Unacquire();
|
---|
64 | mlpDIKeyboard->Release();
|
---|
65 | mlpDIKeyboard = 0;
|
---|
66 | }
|
---|
67 | if (mlpDIMouse)
|
---|
68 | {
|
---|
69 | mlpDIMouse->Unacquire();
|
---|
70 | mlpDIMouse->Release();
|
---|
71 | mlpDIMouse = 0;
|
---|
72 | }
|
---|
73 | if (mlpDI)
|
---|
74 | {
|
---|
75 | mlpDI->Release();
|
---|
76 | mlpDI = 0;
|
---|
77 | }
|
---|
78 |
|
---|
79 | }
|
---|
80 |
|
---|
81 | //-----------------------------------------------------------------------
|
---|
82 | void Win32Input8::initialiseBufferedKeyboard()
|
---|
83 | {
|
---|
84 |
|
---|
85 | HRESULT hr;
|
---|
86 | LogManager::getSingleton().logMessage("Win32Input8: Establishing keyboard input.");
|
---|
87 |
|
---|
88 | // Create keyboard device
|
---|
89 | hr = mlpDI->CreateDevice(GUID_SysKeyboard, &mlpDIKeyboard, NULL);
|
---|
90 |
|
---|
91 |
|
---|
92 | if (FAILED(hr))
|
---|
93 | throw Exception(hr, "Unable to create DirectInput keyboard device.",
|
---|
94 | "Win32Input8 - initialise");
|
---|
95 |
|
---|
96 | // Set data format
|
---|
97 | hr = mlpDIKeyboard->SetDataFormat(&c_dfDIKeyboard);
|
---|
98 | if (FAILED(hr))
|
---|
99 | throw Exception(hr, "Unable to set DirectInput keyboard device data format.",
|
---|
100 | "Win32Input8 - initialise");
|
---|
101 |
|
---|
102 | // Make the window grab keyboard behaviour when foreground
|
---|
103 | hr = mlpDIKeyboard->SetCooperativeLevel(mHWnd,
|
---|
104 | DISCL_FOREGROUND | DISCL_EXCLUSIVE);
|
---|
105 | if (FAILED(hr))
|
---|
106 | throw Exception(hr, "Unable to set DirectInput keyboard device co-operative level.",
|
---|
107 | "Win32Input8 - initialise");
|
---|
108 |
|
---|
109 |
|
---|
110 | // IMPORTANT STEP TO USE BUFFERED DEVICE DATA!
|
---|
111 | //
|
---|
112 | // DirectInput uses unbuffered I/O (buffer size = 0) by default.
|
---|
113 | // If you want to read buffered data, you need to set a nonzero
|
---|
114 | // buffer size.
|
---|
115 | //
|
---|
116 | // Set the buffer size to SAMPLE_BUFFER_SIZE (defined above) elements.
|
---|
117 | //
|
---|
118 | // The buffer size is a DWORD property associated with the device.
|
---|
119 | DIPROPDWORD dipdw;
|
---|
120 | dipdw.diph.dwSize = sizeof(DIPROPDWORD);
|
---|
121 | dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
|
---|
122 | dipdw.diph.dwObj = 0;
|
---|
123 | dipdw.diph.dwHow = DIPH_DEVICE;
|
---|
124 | dipdw.dwData = DINPUT_BUFFERSIZE; // Arbitary buffer size
|
---|
125 |
|
---|
126 | hr = mlpDIKeyboard->SetProperty( DIPROP_BUFFERSIZE, &dipdw.diph );
|
---|
127 |
|
---|
128 | if (FAILED(hr))
|
---|
129 | throw Exception(hr, "Unable to create DirectInput keyboard buffer.",
|
---|
130 | "Win32Input8 - initialise");
|
---|
131 |
|
---|
132 | // Acquire input... we could have lost focus if the
|
---|
133 | // user tabbed away during init or perhaps we're in
|
---|
134 | // the debugger. In either case when the input is
|
---|
135 | // checked we will try to acquire again.
|
---|
136 | hr = mlpDIKeyboard->Acquire();
|
---|
137 | if (FAILED(hr) && hr != DIERR_OTHERAPPHASPRIO)
|
---|
138 | throw Exception(hr, "Unable to set aquire DirectInput keyboard device.",
|
---|
139 | "Win32Input8 - initialise");
|
---|
140 |
|
---|
141 | LogManager::getSingleton().logMessage("Win32Input8: Keyboard input established.");
|
---|
142 | }
|
---|
143 |
|
---|
144 | //-----------------------------------------------------------------------
|
---|
145 | void Win32Input8::initialiseImmediateKeyboard()
|
---|
146 | {
|
---|
147 | HRESULT hr;
|
---|
148 | LogManager::getSingleton().logMessage("Win32Input8: Establishing keyboard input.");
|
---|
149 |
|
---|
150 | // Create keyboard device
|
---|
151 | hr = mlpDI->CreateDevice(GUID_SysKeyboard, &mlpDIKeyboard, NULL);
|
---|
152 |
|
---|
153 |
|
---|
154 | if (FAILED(hr))
|
---|
155 | throw Exception(hr, "Unable to create DirectInput keyboard device.",
|
---|
156 | "Win32Input8 - initialise");
|
---|
157 |
|
---|
158 | // Set data format
|
---|
159 | hr = mlpDIKeyboard->SetDataFormat(&c_dfDIKeyboard);
|
---|
160 | if (FAILED(hr))
|
---|
161 | throw Exception(hr, "Unable to set DirectInput keyboard device data format.",
|
---|
162 | "Win32Input8 - initialise");
|
---|
163 |
|
---|
164 | // Make the window grab keyboard behaviour when foreground
|
---|
165 | // NB Keyboard is never exclusive
|
---|
166 | hr = mlpDIKeyboard->SetCooperativeLevel(mHWnd,
|
---|
167 | DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
|
---|
168 | if (FAILED(hr))
|
---|
169 | throw Exception(hr, "Unable to set DirectInput keyboard device co-operative level.",
|
---|
170 | "Win32Input8 - initialise");
|
---|
171 |
|
---|
172 | // IMPORTANT STEP TO USE BUFFERED DEVICE DATA!
|
---|
173 | //
|
---|
174 | // DirectInput uses unbuffered I/O (buffer size = 0) by default.
|
---|
175 | // If you want to read buffered data, you need to set a nonzero
|
---|
176 | // buffer size.
|
---|
177 | //
|
---|
178 | // Set the buffer size to DINPUT_BUFFERSIZE (defined above) elements.
|
---|
179 | //
|
---|
180 | // The buffer size is a DWORD property associated with the device.
|
---|
181 | DIPROPDWORD dipdw;
|
---|
182 |
|
---|
183 | dipdw.diph.dwSize = sizeof(DIPROPDWORD);
|
---|
184 | dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
|
---|
185 | dipdw.diph.dwObj = 0;
|
---|
186 | dipdw.diph.dwHow = DIPH_DEVICE;
|
---|
187 | dipdw.dwData = DINPUT_BUFFERSIZE ; // Arbitary buffer size
|
---|
188 |
|
---|
189 | hr = mlpDIKeyboard->SetProperty( DIPROP_BUFFERSIZE, &dipdw.diph ) ;
|
---|
190 |
|
---|
191 | if (FAILED(hr))
|
---|
192 | throw Exception(hr, "Unable to create DirectInput keyboard buffer.",
|
---|
193 | "Win32Input8 - initialise");
|
---|
194 |
|
---|
195 |
|
---|
196 | // Acquire input... we could have lost focus if the
|
---|
197 | // user tabbed away during init or perhaps we're in
|
---|
198 | // the debugger. In either case when the input is
|
---|
199 | // checked we will try to acquire again. hr = mlpDIKeyboard->Acquire();
|
---|
200 | if (FAILED(hr) && hr != DIERR_OTHERAPPHASPRIO)
|
---|
201 | throw Exception(hr, "Unable to set aquire DirectInput keyboard device.",
|
---|
202 | "Win32Input8 - initialise");
|
---|
203 |
|
---|
204 | LogManager::getSingleton().logMessage("Win32Input8: Keyboard input established.");
|
---|
205 | }
|
---|
206 | //-----------------------------------------------------------------------
|
---|
207 | void Win32Input8::initialiseImmediateMouse()
|
---|
208 | {
|
---|
209 | OgreGuard( "Win32Input8::initialiseImmediateMouse" );
|
---|
210 |
|
---|
211 | HRESULT hr;
|
---|
212 | DIPROPDWORD dipdw;
|
---|
213 | LogManager::getSingleton().logMessage( "Win32Input8: Initializing mouse input in immediate mode." );
|
---|
214 |
|
---|
215 | dipdw.diph.dwSize = sizeof(DIPROPDWORD);
|
---|
216 | dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
|
---|
217 | dipdw.diph.dwObj = 0;
|
---|
218 | dipdw.diph.dwHow = DIPH_DEVICE;
|
---|
219 | dipdw.dwData = DIPROPAXISMODE_ABS;
|
---|
220 |
|
---|
221 | if( /* Create the DI Device. */
|
---|
222 | FAILED( hr = mlpDI->CreateDevice( GUID_SysMouse, &mlpDIMouse, NULL ) ) ||
|
---|
223 | /* Set the data format so that it knows it's a mouse. */
|
---|
224 | FAILED( hr = mlpDIMouse->SetDataFormat( &c_dfDIMouse2 ) ) ||
|
---|
225 | /* Absolute mouse input. We can derive the relative input from this. */
|
---|
226 | FAILED( hr = mlpDIMouse->SetProperty( DIPROP_AXISMODE, &dipdw.diph ) ) ||
|
---|
227 | /* Exclusive when in foreground, steps back when in background. */
|
---|
228 | FAILED( hr = mlpDIMouse->SetCooperativeLevel( mHWnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE ) ) )
|
---|
229 | {
|
---|
230 | OGRE_EXCEPT( hr, "Unable to initialise mouse", "Win32Input8::initialiseImmediateMouse" );
|
---|
231 | }
|
---|
232 | /* Note that we did not acquire the mouse in the code above, since the call may fail (ie you're in the
|
---|
233 | debugger) and an exception would be thrown. Acquisition happens in the captureMouse() function. */
|
---|
234 |
|
---|
235 | /* Get initial mouse data. We might as well fail this initial attempt, so no biggie. */
|
---|
236 | captureMouse();
|
---|
237 |
|
---|
238 | /* Clear any relative mouse data. */
|
---|
239 | mMouseState.Xrel = mMouseState.Yrel = mMouseState.Zrel = 0;
|
---|
240 |
|
---|
241 | LogManager::getSingleton().logMessage( "Win32Input8: Mouse input in immediate mode initialized." );
|
---|
242 |
|
---|
243 | OgreUnguard();
|
---|
244 | }
|
---|
245 |
|
---|
246 | //-----------------------------------------------------------------------
|
---|
247 | void Win32Input8::initialiseBufferedMouse()
|
---|
248 | {
|
---|
249 | HRESULT hr;
|
---|
250 | LogManager::getSingleton().logMessage("Win32Input8: Establishing mouse input.");
|
---|
251 |
|
---|
252 | // Create mouse device
|
---|
253 | hr = mlpDI->CreateDevice(GUID_SysMouse, &mlpDIMouse, NULL);
|
---|
254 |
|
---|
255 |
|
---|
256 | if (FAILED(hr))
|
---|
257 | throw Exception(hr, "Unable to create DirectInput mouse device.",
|
---|
258 | "Win32Input8 - initialise");
|
---|
259 |
|
---|
260 | // Set data format
|
---|
261 | hr = mlpDIMouse->SetDataFormat(&c_dfDIMouse2);
|
---|
262 | if (FAILED(hr))
|
---|
263 | throw Exception(hr, "Unable to set DirectInput mouse device data format.",
|
---|
264 | "Win32Input8 - initialise");
|
---|
265 |
|
---|
266 | // Make the window grab mouse behaviour when foreground
|
---|
267 | hr = mlpDIMouse->SetCooperativeLevel(mHWnd,
|
---|
268 | DISCL_FOREGROUND | DISCL_EXCLUSIVE);
|
---|
269 | if (FAILED(hr))
|
---|
270 | throw Exception(hr, "Unable to set DirectInput mouse device co-operative level.",
|
---|
271 | "Win32Input8 - initialise");
|
---|
272 |
|
---|
273 |
|
---|
274 | // IMPORTANT STEP TO USE BUFFERED DEVICE DATA!
|
---|
275 | //
|
---|
276 | // DirectInput uses unbuffered I/O (buffer size = 0) by default.
|
---|
277 | // If you want to read buffered data, you need to set a nonzero
|
---|
278 | // buffer size.
|
---|
279 | //
|
---|
280 | // Set the buffer size to SAMPLE_BUFFER_SIZE (defined above) elements.
|
---|
281 | //
|
---|
282 | // The buffer size is a DWORD property associated with the device.
|
---|
283 | DIPROPDWORD dipdw;
|
---|
284 | dipdw.diph.dwSize = sizeof(DIPROPDWORD);
|
---|
285 | dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
|
---|
286 | dipdw.diph.dwObj = 0;
|
---|
287 | dipdw.diph.dwHow = DIPH_DEVICE;
|
---|
288 | dipdw.dwData = DINPUT_BUFFERSIZE; // Arbitary buffer size
|
---|
289 |
|
---|
290 | hr = mlpDIMouse->SetProperty( DIPROP_BUFFERSIZE, &dipdw.diph );
|
---|
291 |
|
---|
292 | if (FAILED(hr))
|
---|
293 | throw Exception(hr, "Unable to create DirectInput mouse buffer.",
|
---|
294 | "Win32Input8 - initialise");
|
---|
295 |
|
---|
296 | // Acquire input... we could have lost focus if the
|
---|
297 | // user tabbed away during init or perhaps we're in
|
---|
298 | // the debugger. In either case when the input is
|
---|
299 | // checked we will try to acquire again.
|
---|
300 | hr = mlpDIMouse->Acquire();
|
---|
301 | if (FAILED(hr) && hr != DIERR_OTHERAPPHASPRIO)
|
---|
302 | throw Exception(hr, "Unable to set aquire DirectInput mouse device.",
|
---|
303 | "Win32Input8 - initialise");
|
---|
304 |
|
---|
305 | LogManager::getSingleton().logMessage("Win32Input8: Mouse input established.");
|
---|
306 |
|
---|
307 | }
|
---|
308 |
|
---|
309 | //-----------------------------------------------------------------------
|
---|
310 | void Win32Input8::initialise(RenderWindow* pWindow, bool useKeyboard, bool useMouse, bool useGameController)
|
---|
311 | {
|
---|
312 | HRESULT hr;
|
---|
313 |
|
---|
314 | mUseKeyboard = useKeyboard;
|
---|
315 | mUseMouse = useMouse;
|
---|
316 | LogManager::getSingleton().logMessage("Win32Input8: DirectInput Activation Starts");
|
---|
317 |
|
---|
318 | // Get HINST
|
---|
319 | HINSTANCE hInst = GetModuleHandle(OGRE_PLATFORM_LIB);
|
---|
320 |
|
---|
321 | // Get HWND
|
---|
322 | HWND hWnd = GetActiveWindow();
|
---|
323 |
|
---|
324 | mHWnd = hWnd;
|
---|
325 |
|
---|
326 | ShowCursor(FALSE);
|
---|
327 |
|
---|
328 |
|
---|
329 | // Register with the DirectInput subsystem and get a pointer
|
---|
330 | // to a IDirectInput interface we can use.
|
---|
331 | // Create a DInput object
|
---|
332 | hr = DirectInput8Create( hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&mlpDI, NULL );
|
---|
333 | if (FAILED(hr))
|
---|
334 | throw Exception(hr, "Unable to initialise DirectInput.",
|
---|
335 | "Win32Input8 - initialise");
|
---|
336 |
|
---|
337 | if (useKeyboard)
|
---|
338 | {
|
---|
339 | if (mUseBufferedKeys)
|
---|
340 | {
|
---|
341 | initialiseBufferedKeyboard();
|
---|
342 | }
|
---|
343 | else
|
---|
344 | {
|
---|
345 | initialiseImmediateKeyboard();
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | if (useMouse)
|
---|
350 | {
|
---|
351 | if (mUseBufferedMouse)
|
---|
352 | {
|
---|
353 | initialiseBufferedMouse();
|
---|
354 | }
|
---|
355 | else
|
---|
356 | {
|
---|
357 | initialiseImmediateMouse();
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 |
|
---|
362 | LogManager::getSingleton().logMessage("Win32Input8: DirectInput OK.");
|
---|
363 |
|
---|
364 | }
|
---|
365 |
|
---|
366 | /* void Win32Input8::setBufferedInput(bool keys, bool mouse)
|
---|
367 | {
|
---|
368 | flushAllBuffers();
|
---|
369 | InputReader::setBufferedInput(keys, mouse);
|
---|
370 | }
|
---|
371 | */
|
---|
372 | void Win32Input8::flushAllBuffers()
|
---|
373 | {
|
---|
374 |
|
---|
375 | DWORD dwItems = INFINITE;
|
---|
376 | HRESULT hr = mlpDIKeyboard->GetDeviceData( sizeof(DIDEVICEOBJECTDATA),
|
---|
377 | NULL, &dwItems, 0 );
|
---|
378 | hr = mlpDIMouse->GetDeviceData( sizeof(DIDEVICEOBJECTDATA),
|
---|
379 | NULL, &dwItems, 0 );
|
---|
380 | }
|
---|
381 |
|
---|
382 | //-----------------------------------------------------------------------
|
---|
383 |
|
---|
384 | // this function is not needed at the moment because we are making everything buffered
|
---|
385 | void Win32Input8::setBufferedInput(bool keys, bool mouse)
|
---|
386 | {
|
---|
387 | if (mUseKeyboard && mUseBufferedKeys != keys)
|
---|
388 | {
|
---|
389 | if (mlpDIKeyboard)
|
---|
390 | {
|
---|
391 | mlpDIKeyboard->Unacquire();
|
---|
392 | mlpDIKeyboard->Release();
|
---|
393 | mlpDIKeyboard = 0;
|
---|
394 | }
|
---|
395 | if (keys)
|
---|
396 | {
|
---|
397 | initialiseBufferedKeyboard();
|
---|
398 | }
|
---|
399 | else
|
---|
400 | {
|
---|
401 | initialiseImmediateKeyboard();
|
---|
402 | }
|
---|
403 |
|
---|
404 | }
|
---|
405 | if (mUseMouse && mUseBufferedMouse != mouse)
|
---|
406 | {
|
---|
407 | if (mlpDIMouse)
|
---|
408 | {
|
---|
409 | mlpDIMouse->Unacquire();
|
---|
410 | mlpDIMouse->Release();
|
---|
411 | mlpDIMouse= 0;
|
---|
412 | }
|
---|
413 | if (mouse)
|
---|
414 | {
|
---|
415 | initialiseBufferedMouse();
|
---|
416 | }
|
---|
417 | else
|
---|
418 | {
|
---|
419 | initialiseImmediateMouse();
|
---|
420 | }
|
---|
421 |
|
---|
422 | }
|
---|
423 | InputReader::setBufferedInput(keys,mouse);
|
---|
424 | }
|
---|
425 |
|
---|
426 | //-----------------------------------------------------------------------
|
---|
427 | void Win32Input8::capture(void)
|
---|
428 | {
|
---|
429 | if (mUseKeyboard)
|
---|
430 | {
|
---|
431 | if (mUseBufferedKeys )
|
---|
432 | {
|
---|
433 | readBufferedKeyboardData();
|
---|
434 | }
|
---|
435 | else
|
---|
436 | {
|
---|
437 | mModifiers = getKeyModifiers();
|
---|
438 | captureKeyboard();
|
---|
439 | }
|
---|
440 | }
|
---|
441 | if (mUseMouse)
|
---|
442 | {
|
---|
443 | if (mUseBufferedMouse )
|
---|
444 | {
|
---|
445 | readBufferedMouseData();
|
---|
446 | }
|
---|
447 | else
|
---|
448 | {
|
---|
449 | captureMouse();
|
---|
450 | }
|
---|
451 | }
|
---|
452 |
|
---|
453 | }
|
---|
454 | //-----------------------------------------------------------------------
|
---|
455 | void Win32Input8::captureKeyboard(void)
|
---|
456 | {
|
---|
457 | HRESULT hr;
|
---|
458 |
|
---|
459 | // Get keyboard state
|
---|
460 | hr = mlpDIKeyboard->GetDeviceState(sizeof(mKeyboardBuffer),(LPVOID)&mKeyboardBuffer);
|
---|
461 | if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED)
|
---|
462 | {
|
---|
463 | hr = mlpDIKeyboard->Acquire();
|
---|
464 | if (hr == DIERR_OTHERAPPHASPRIO)
|
---|
465 | {
|
---|
466 | hr = 0;
|
---|
467 | }
|
---|
468 | else
|
---|
469 | {
|
---|
470 | hr = mlpDIKeyboard->GetDeviceState(sizeof(mKeyboardBuffer),(LPVOID)&mKeyboardBuffer);
|
---|
471 | }
|
---|
472 | }
|
---|
473 | else if (hr == DIERR_OTHERAPPHASPRIO)
|
---|
474 | {
|
---|
475 | // We've gone into the background - ignore
|
---|
476 | hr = 0;
|
---|
477 | }
|
---|
478 | else if (hr == DIERR_NOTINITIALIZED)
|
---|
479 | {
|
---|
480 | hr = 0;
|
---|
481 | }
|
---|
482 | else if (hr == E_PENDING)
|
---|
483 | {
|
---|
484 | hr = 0;
|
---|
485 | }
|
---|
486 | else if (FAILED(hr))
|
---|
487 | {
|
---|
488 | // Ignore for now
|
---|
489 | // TODO - sort this out
|
---|
490 | hr = 0;
|
---|
491 | }
|
---|
492 |
|
---|
493 | }
|
---|
494 |
|
---|
495 | //-----------------------------------------------------------------------
|
---|
496 | void Win32Input8::captureMouse(void)
|
---|
497 | {
|
---|
498 | DIMOUSESTATE2 mouseState;
|
---|
499 | HRESULT hr;
|
---|
500 |
|
---|
501 | // Get mouse state
|
---|
502 | hr = mlpDIMouse->GetDeviceState( sizeof( DIMOUSESTATE2 ), (LPVOID)&mouseState );
|
---|
503 |
|
---|
504 | if( SUCCEEDED( hr ) ||
|
---|
505 | ( ( hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED ) &&
|
---|
506 | SUCCEEDED( mlpDIMouse->Acquire() ) &&
|
---|
507 | SUCCEEDED( mlpDIMouse->GetDeviceState( sizeof( DIMOUSESTATE2 ), (LPVOID)&mouseState ) ) ) )
|
---|
508 | {
|
---|
509 | /* Register the new 'origin'. */
|
---|
510 | mMouseCenterX = mMouseState.Xabs;
|
---|
511 | mMouseCenterY = mMouseState.Yabs;
|
---|
512 | mMouseCenterZ = mMouseState.Zabs;
|
---|
513 |
|
---|
514 | /* Get the new absolute position. */
|
---|
515 | mMouseState.Xabs = mouseState.lX;
|
---|
516 | mMouseState.Yabs = mouseState.lY;
|
---|
517 | mMouseState.Zabs = mouseState.lZ;
|
---|
518 |
|
---|
519 | /* Compute the new relative position. */
|
---|
520 | mMouseState.Xrel = mMouseState.Xabs - mMouseCenterX;
|
---|
521 | mMouseState.Yrel = mMouseState.Yabs - mMouseCenterY;
|
---|
522 | mMouseState.Zrel = mMouseState.Zabs - mMouseCenterZ;
|
---|
523 |
|
---|
524 | /* Get the mouse buttons. This for loop can be unwrapped for speed. */
|
---|
525 | mMouseState.Buttons = 0;
|
---|
526 | for( size_t i = 0; i < 8; i++ )
|
---|
527 | if( mouseState.rgbButtons[ i ] & 0x80 )
|
---|
528 | mMouseState.Buttons |= ( 1 << i );
|
---|
529 | }
|
---|
530 | else if (hr == DIERR_OTHERAPPHASPRIO)
|
---|
531 | {
|
---|
532 | // We've gone into the background - ignore
|
---|
533 | hr = 0;
|
---|
534 | }
|
---|
535 | else if (hr == DIERR_NOTINITIALIZED)
|
---|
536 | {
|
---|
537 | hr = 0;
|
---|
538 | }
|
---|
539 | else if (hr == E_PENDING)
|
---|
540 | {
|
---|
541 | hr = 0;
|
---|
542 | }
|
---|
543 | else if (FAILED(hr))
|
---|
544 | {
|
---|
545 | // Ignore for now
|
---|
546 | // TODO - sort this out
|
---|
547 | hr = 0;
|
---|
548 | }
|
---|
549 |
|
---|
550 | }
|
---|
551 |
|
---|
552 |
|
---|
553 |
|
---|
554 | //-----------------------------------------------------------------------------
|
---|
555 | // Name: readBufferedData()
|
---|
556 | // Desc: Read the input device's state when in buffered mode and display it.
|
---|
557 | //-----------------------------------------------------------------------------
|
---|
558 | bool Win32Input8::readBufferedKeyboardData()
|
---|
559 | {
|
---|
560 | DIDEVICEOBJECTDATA didod[ DINPUT_BUFFERSIZE ]; // Receives buffered data
|
---|
561 | DWORD dwElements;
|
---|
562 | HRESULT hr;
|
---|
563 |
|
---|
564 | if( NULL == mlpDIKeyboard )
|
---|
565 | return true;
|
---|
566 |
|
---|
567 | dwElements = DINPUT_BUFFERSIZE;
|
---|
568 |
|
---|
569 | hr = mlpDIKeyboard->GetDeviceData( sizeof(DIDEVICEOBJECTDATA),
|
---|
570 | didod, &dwElements, 0 );
|
---|
571 | if( FAILED(hr) )
|
---|
572 | {
|
---|
573 | // Error
|
---|
574 | hr = mlpDIKeyboard->Acquire();
|
---|
575 | while( hr == DIERR_INPUTLOST )
|
---|
576 | hr = mlpDIKeyboard->Acquire();
|
---|
577 |
|
---|
578 | // hr may be DIERR_OTHERAPPHASPRIO or other errors. This
|
---|
579 | // may occur when the app is minimized or in the process of
|
---|
580 | // switching, so just try again later
|
---|
581 | return !FAILED(hr);
|
---|
582 | }
|
---|
583 |
|
---|
584 | // DI_BUFFEROVERFLOW we don't do anything with since we still want to
|
---|
585 | // process what's in the buffer
|
---|
586 |
|
---|
587 | for(unsigned int i = 0; i < dwElements; i++ )
|
---|
588 | {
|
---|
589 | keyChanged( didod[ i ].dwOfs, (didod[ i ].dwData & 0x80) != 0);
|
---|
590 | }
|
---|
591 | return true;
|
---|
592 | }
|
---|
593 |
|
---|
594 | //-----------------------------------------------------------------------------
|
---|
595 | // Name: readBufferedData()
|
---|
596 | // Desc: Read the input device's state when in buffered mode and display it.
|
---|
597 | //-----------------------------------------------------------------------------
|
---|
598 | bool Win32Input8::readBufferedMouseData()
|
---|
599 | {
|
---|
600 | DIDEVICEOBJECTDATA didod[ DINPUT_BUFFERSIZE ]; // Receives buffered data
|
---|
601 | DWORD dwElements;
|
---|
602 | HRESULT hr;
|
---|
603 |
|
---|
604 | if( NULL == mlpDIMouse )
|
---|
605 | return true;
|
---|
606 |
|
---|
607 | dwElements = DINPUT_BUFFERSIZE;
|
---|
608 |
|
---|
609 | // Try to read the data. Continue normally on success (DI_OK
|
---|
610 | // or DI_BUFFEROVERFLOW) as we have to process the read data
|
---|
611 | // we got.
|
---|
612 | hr = mlpDIMouse->GetDeviceData( sizeof(DIDEVICEOBJECTDATA),
|
---|
613 | didod, &dwElements, 0 );
|
---|
614 | if( FAILED(hr) )
|
---|
615 | {
|
---|
616 | // No need to handle DI_BUFFEROVERFLOW in ERROR handling.
|
---|
617 | // So moved it to later.
|
---|
618 | //
|
---|
619 | // We got an error.
|
---|
620 | //
|
---|
621 | // It means that continuous contact with the
|
---|
622 | // device has been lost, either due to an external
|
---|
623 | // interruption.
|
---|
624 | //
|
---|
625 |
|
---|
626 | hr = mlpDIMouse->Acquire();
|
---|
627 | while( hr == DIERR_INPUTLOST )
|
---|
628 | hr = mlpDIMouse->Acquire();
|
---|
629 |
|
---|
630 | // hr may be DIERR_OTHERAPPHASPRIO or other errors. This
|
---|
631 | // may occur when the app is minimized or in the process of
|
---|
632 | // switching, so just try again later
|
---|
633 | return !FAILED(hr);
|
---|
634 | }
|
---|
635 |
|
---|
636 | // DI_BUFFEROVERFLOW we don't do anything with since we still want to
|
---|
637 | // process what's in the buffer
|
---|
638 |
|
---|
639 | bool xSet = false;
|
---|
640 | bool ySet = false;
|
---|
641 | bool zSet = false;
|
---|
642 | /// Redefine FIELD_OFFSET in case of GCC
|
---|
643 | #ifdef __GNUC__
|
---|
644 | #undef FIELD_OFFSET
|
---|
645 | #define FIELD_OFFSET offsetof
|
---|
646 | #endif // __GNUC__
|
---|
647 |
|
---|
648 | for(unsigned int i = 0; i < dwElements; i++ )
|
---|
649 | {
|
---|
650 | int nMouseCode = -1; // not set
|
---|
651 |
|
---|
652 | // this will display then scan code of the key
|
---|
653 | // plus a 'D' - meaning the key was pressed
|
---|
654 | // or a 'U' - meaning the key was released
|
---|
655 | switch( didod [ i ].dwOfs )
|
---|
656 | {
|
---|
657 | case DIMOFS_BUTTON0:
|
---|
658 | nMouseCode = InputEvent::BUTTON0_MASK;
|
---|
659 | break;
|
---|
660 |
|
---|
661 | case DIMOFS_BUTTON1:
|
---|
662 | nMouseCode = InputEvent::BUTTON1_MASK;
|
---|
663 | break;
|
---|
664 |
|
---|
665 | case DIMOFS_BUTTON2:
|
---|
666 | nMouseCode = InputEvent::BUTTON2_MASK;
|
---|
667 | break;
|
---|
668 |
|
---|
669 | case DIMOFS_BUTTON3:
|
---|
670 | nMouseCode = InputEvent::BUTTON3_MASK;
|
---|
671 | break;
|
---|
672 |
|
---|
673 | case DIMOFS_X:
|
---|
674 | if (xSet)
|
---|
675 | { // process the last X move since we have a new one
|
---|
676 | mouseMoved();
|
---|
677 | xSet = false;
|
---|
678 | }
|
---|
679 | mCursor->addToX(getScaled(didod[i].dwData));
|
---|
680 | xSet = true;
|
---|
681 | break;
|
---|
682 |
|
---|
683 | case DIMOFS_Y:
|
---|
684 | if (ySet)
|
---|
685 | {
|
---|
686 | mouseMoved();
|
---|
687 | ySet = false;
|
---|
688 | }
|
---|
689 | mCursor->addToY(getScaled(didod[i].dwData));
|
---|
690 | ySet = true;
|
---|
691 | break;
|
---|
692 |
|
---|
693 | case DIMOFS_Z:
|
---|
694 | if (zSet)
|
---|
695 | {
|
---|
696 | mouseMoved();
|
---|
697 | zSet = false;
|
---|
698 | }
|
---|
699 | mCursor->addToZ(getScaled(didod[i].dwData));
|
---|
700 | zSet = true;
|
---|
701 | break;
|
---|
702 |
|
---|
703 | default:
|
---|
704 | break;
|
---|
705 | }
|
---|
706 | if (nMouseCode != -1)
|
---|
707 | {
|
---|
708 | triggerMouseButton(nMouseCode, (didod [ i ].dwData & 0x80) != 0);
|
---|
709 | }
|
---|
710 | if (xSet && ySet) // don't create 2 mousemove events for an single X and Y move, just create 1.
|
---|
711 | {
|
---|
712 | mouseMoved();
|
---|
713 | ySet = false;
|
---|
714 | xSet = false;
|
---|
715 | }
|
---|
716 |
|
---|
717 |
|
---|
718 | }
|
---|
719 | if (zSet || xSet || ySet) // check for last moved at end
|
---|
720 | {
|
---|
721 | mouseMoved();
|
---|
722 | }
|
---|
723 |
|
---|
724 | return true;
|
---|
725 | }
|
---|
726 | //-----------------------------------------------------------------------
|
---|
727 |
|
---|
728 | Real Win32Input8::getScaled(DWORD dwVal) const
|
---|
729 | {
|
---|
730 | return (Real)((int)dwVal) * mMouseScale;
|
---|
731 | }
|
---|
732 |
|
---|
733 | //-----------------------------------------------------------------------
|
---|
734 | bool Win32Input8::isKeyDownImmediate(KeyCode kc) const
|
---|
735 | {
|
---|
736 | return ( mKeyboardBuffer[ kc ] & 0x80 ) != 0;
|
---|
737 | }
|
---|
738 |
|
---|
739 | //---------------------------------------------------------------------------------------------
|
---|
740 | long Win32Input8::getMouseRelX() const
|
---|
741 | {
|
---|
742 | return mMouseState.Xrel;
|
---|
743 | }
|
---|
744 |
|
---|
745 | //---------------------------------------------------------------------------------------------
|
---|
746 | long Win32Input8::getMouseRelY() const
|
---|
747 | {
|
---|
748 | return mMouseState.Yrel;
|
---|
749 | }
|
---|
750 |
|
---|
751 | //---------------------------------------------------------------------------------------------
|
---|
752 | long Win32Input8::getMouseRelZ() const
|
---|
753 | {
|
---|
754 | return mMouseState.Zrel;
|
---|
755 | }
|
---|
756 |
|
---|
757 | long Win32Input8::getMouseAbsX() const
|
---|
758 | {
|
---|
759 | return mMouseState.Xabs;
|
---|
760 | }
|
---|
761 |
|
---|
762 | long Win32Input8::getMouseAbsY() const
|
---|
763 | {
|
---|
764 | return mMouseState.Yabs;
|
---|
765 | }
|
---|
766 |
|
---|
767 | long Win32Input8::getMouseAbsZ() const
|
---|
768 | {
|
---|
769 | return mMouseState.Zabs;
|
---|
770 | }
|
---|
771 |
|
---|
772 | //---------------------------------------------------------------------------------------------
|
---|
773 | bool Win32Input8::getMouseButton( uchar button ) const
|
---|
774 | {
|
---|
775 | return mMouseState.isButtonDown( button ) != 0;
|
---|
776 | }
|
---|
777 |
|
---|
778 | //---------------------------------------------------------------------------------------------
|
---|
779 | void Win32Input8::getMouseState( MouseState& state ) const
|
---|
780 | {
|
---|
781 | memcpy( &state, &mMouseState, sizeof( MouseState ) );
|
---|
782 | }
|
---|
783 |
|
---|
784 | //---------------------------------------------------------------------------------------------
|
---|
785 | long Win32Input8::getKeyModifiers() const
|
---|
786 | {
|
---|
787 | long ret = mModifiers;
|
---|
788 |
|
---|
789 | if (isKeyDown(KC_LMENU) || isKeyDown(KC_RMENU))
|
---|
790 | {
|
---|
791 | ret |= InputEvent::ALT_MASK;
|
---|
792 | }
|
---|
793 | else
|
---|
794 | {
|
---|
795 | ret &= ~InputEvent::ALT_MASK;
|
---|
796 | }
|
---|
797 |
|
---|
798 | if (isKeyDown(KC_LSHIFT) || isKeyDown(KC_RSHIFT))
|
---|
799 | {
|
---|
800 | ret |= InputEvent::SHIFT_MASK;
|
---|
801 | }
|
---|
802 | else
|
---|
803 | {
|
---|
804 | ret &= ~InputEvent::SHIFT_MASK;
|
---|
805 | }
|
---|
806 |
|
---|
807 | if (isKeyDown(KC_LCONTROL) || isKeyDown(KC_LCONTROL))
|
---|
808 | {
|
---|
809 | ret |= InputEvent::CTRL_MASK;
|
---|
810 | }
|
---|
811 | else
|
---|
812 | {
|
---|
813 | ret &= ~InputEvent::CTRL_MASK;
|
---|
814 | }
|
---|
815 |
|
---|
816 | return ret;
|
---|
817 | }
|
---|
818 |
|
---|
819 | } // namespace
|
---|
820 | #endif
|
---|
821 | #endif
|
---|