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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
|
////////////////////////////////////////////////////////////////////////////////////////
//
// Nestopia - NES/Famicom emulator written in C++
//
// Copyright (C) 2003-2008 Martin Freij
//
// This file is part of Nestopia.
//
// Nestopia 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; either version 2 of the License, or
// (at your option) any later version.
//
// Nestopia 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 for more details.
//
// You should have received a copy of the GNU General Public License
// along with Nestopia; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
////////////////////////////////////////////////////////////////////////////////////////
#ifndef NST_DIRECTX_DIRECTINPUT_H
#define NST_DIRECTX_DIRECTINPUT_H
#pragma once
#define DIRECTINPUT_VERSION 0x0800
#include "NstDirectX.hpp"
#include "NstObjectPod.hpp"
#include "NstObjectHeap.hpp"
#include "NstCollectionVector.hpp"
#include <dinput.h>
namespace Nestopia
{
namespace DirectX
{
class DirectInput
{
class Keyboard;
class Joystick;
public:
explicit DirectInput(HWND);
~DirectInput();
enum
{
MAX_JOYSTICKS = 32,
NUM_KEYBOARD_KEYS = 256,
AXIS_X = 0x001,
AXIS_Y = 0x002,
AXIS_Z = 0x004,
AXIS_RX = 0x008,
AXIS_RY = 0x010,
AXIS_RZ = 0x020,
AXIS_SLIDER_0 = 0x040,
AXIS_SLIDER_1 = 0x080,
AXIS_POV_0 = 0x100,
AXIS_POV_1 = 0x200,
AXIS_POV_2 = 0x400,
AXIS_POV_3 = 0x800,
AXIS_ALL = 0xFFF,
DEADZONE_MAX = 100,
DEFAULT_AXES = AXIS_X|AXIS_Y|AXIS_Z|AXIS_RX|AXIS_RY|AXIS_RZ|AXIS_POV_0|AXIS_POV_1|AXIS_POV_2|AXIS_POV_3,
DEFAULT_DEADZONE = 50
};
enum ScanMode
{
SCAN_MODE_ALL,
SCAN_MODE_JOY
};
enum ScanResult
{
SCAN_INVALID_KEY = -1,
SCAN_NO_KEY,
SCAN_GOOD_KEY
};
class Key;
void Acquire();
void Unacquire();
void Calibrate();
void Poll();
void Build(const Key*,uint);
bool MapKey(Key&,wcstring,const System::Guid* = NULL,uint=0) const;
const HeapString GetKeyName(const Key&) const;
void BeginScanMode(HWND) const;
ScanResult ScanKey(Key&,ScanMode=SCAN_MODE_ALL);
void EndScanMode() const;
class Key
{
friend class DirectInput;
friend class Keyboard;
friend class Joystick;
public:
bool MapVirtualKey(uint,uint,uint,uint);
bool MapVirtualKey(GenericString);
bool GetVirtualKey(ACCEL&) const;
bool IsVirtualKey() const;
bool GetToggle(bool&) const;
private:
union
{
const BYTE* data;
DWORD vKey;
};
uint (*code)(const void* const);
public:
Key()
: data(NULL), code(KeyNone) {}
bool operator == (const Key& key) const
{
return data == key.data && code == key.code;
}
void Unmap()
{
data = NULL;
code = KeyNone;
}
bool Assigned() const
{
return data;
}
uint GetState() const
{
return code( data );
}
template<typename Value>
void GetState(Value& value,uint mask) const
{
value |= code( data ) & mask;
}
};
private:
class Base
{
static IDirectInput8& Create();
public:
explicit Base(HWND);
~Base();
IDirectInput8& com;
HWND const hWnd;
};
class Device
{
public:
void Enable(bool);
protected:
explicit Device(IDirectInputDevice8&);
~Device();
bool Acquire(void*,uint);
void Unacquire();
IDirectInputDevice8& com;
bool enabled;
};
class Keyboard : public Device
{
public:
explicit Keyboard(Base&);
enum
{
MAX_KEYS = NUM_KEYBOARD_KEYS
};
void Acquire();
void Unacquire();
NST_FORCE_INLINE void Poll();
bool Map(Key&,wcstring) const;
bool Map(Key&,uint) const;
void BeginScanMode(HWND) const;
bool Scan(uchar (&)[MAX_KEYS]) const;
ScanResult Scan(Key&) const;
void EndScanMode() const;
bool Assigned(const Key&) const;
wcstring GetName(const Key&) const;
private:
enum
{
COOPERATIVE_FLAGS = DISCL_FOREGROUND|DISCL_NONEXCLUSIVE|DISCL_NOWINKEY,
SCAN_COOPERATIVE_FLAGS = DISCL_BACKGROUND|DISCL_NONEXCLUSIVE
};
static BOOL CALLBACK EnumObjects(LPCDIDEVICEOBJECTINSTANCE,LPVOID);
static IDirectInputDevice8& Create(IDirectInput8&);
void SetCooperativeLevel(HWND,DWORD) const;
void Clear();
NST_NO_INLINE void OnError(HRESULT);
typedef Object::Heap<BYTE,MAX_KEYS> Buffer;
Buffer buffer;
HWND const hWnd;
static HeapString keyNames[MAX_KEYS];
public:
const uchar* GetBuffer() const
{
return buffer;
}
};
class Joystick : public Device
{
public:
Joystick(Base&,const DIDEVICEINSTANCE&);
enum
{
POV_CENTER = 0xFFFF,
POV_UPRIGHT = 45 * DI_DEGREES,
POV_DOWNRIGHT = 135 * DI_DEGREES,
POV_DOWNLEFT = 225 * DI_DEGREES,
POV_UPLEFT = 315 * DI_DEGREES
};
enum Exception
{
ERR_API
};
void Acquire();
void Unacquire();
void Calibrate();
NST_FORCE_INLINE void Poll();
bool Map(Key&,wcstring) const;
void BeginScanMode() const;
bool Scan(Key&);
void EndScanMode() const;
bool Assigned(const Key&) const;
bool SetAxisDeadZone(uint);
wcstring GetName(const Key&) const;
private:
static IDirectInputDevice8& Create(Base&,const GUID&);
void Clear();
NST_NO_INLINE void OnError(HRESULT);
class Caps
{
struct Context
{
Context(Caps&,IDirectInputDevice8&);
Caps& caps;
IDirectInputDevice8& com;
uint numButtons;
};
enum
{
AXIS_MIN_RANGE = -1000,
AXIS_MAX_RANGE = +1000
};
static BOOL CALLBACK EnumObjectsProc(LPCDIDEVICEOBJECTINSTANCE,LPVOID);
public:
Caps(IDirectInputDevice8&,const DIDEVICEINSTANCE&);
enum
{
NUM_POVS = 4,
NUM_BUTTONS = 32
};
uint axes;
const System::Guid guid;
const HeapString name;
};
class Calibrator
{
long lX;
long lY;
long lZ;
long lRx;
long lRy;
long lRz;
public:
Calibrator();
inline void Reset(DIJOYSTATE&,bool);
inline void Fix(DIJOYSTATE&) const;
};
const Caps caps;
Object::Pod<DIJOYSTATE> state;
Calibrator calibrator;
bool calibrated;
bool scanEnabled;
uint deadZone;
uint axes;
enum
{
TABLE_KEYS = 32
};
struct Lookup;
static const Lookup table[TABLE_KEYS];
public:
const System::Guid& GetGuid() const
{
return caps.guid;
}
const HeapString& GetName() const
{
return caps.name;
}
uint GetAxisDeadZone() const
{
return deadZone;
}
uint GetAvailableAxes() const
{
return caps.axes;
}
uint GetScannerAxes() const
{
return axes;
}
void ScanEnable(bool enable)
{
scanEnabled = enable;
}
bool ScanEnabled() const
{
return scanEnabled;
}
void SetScannerAxes(uint flags)
{
NST_ASSERT( flags <= AXIS_ALL );
axes = flags;
}
void SetScannerAxes(uint flags,bool on)
{
NST_ASSERT( flags <= AXIS_ALL );
axes = (on ? axes | flags : axes & ~flags);
}
};
typedef Collection::Vector<Joystick> Joysticks;
static BOOL CALLBACK EnumJoysticks(LPCDIDEVICEINSTANCE,LPVOID);
static uint KeyDown (const void* const);
static uint KeyNegDir (const void* const);
static uint KeyPosDir (const void* const);
static uint KeyPovUp (const void* const);
static uint KeyPovRight (const void* const);
static uint KeyPovDown (const void* const);
static uint KeyPovLeft (const void* const);
static uint KeyNone (const void* const);
Base base;
Keyboard keyboard;
Joysticks joysticks;
public:
bool MapKeyboard(Key& key,uint code) const
{
key.Unmap();
return keyboard.Map( key, code );
}
uint NumJoysticks() const
{
return joysticks.Size();
}
bool JoystickScanEnabled(uint index) const
{
NST_ASSERT( index < joysticks.Size() );
return joysticks[index].ScanEnabled();
}
void ScanEnableJoystick(uint index,bool enable)
{
NST_ASSERT( index < joysticks.Size() );
joysticks[index].ScanEnable( enable );
}
const System::Guid& GetJoystickGuid(uint index) const
{
NST_ASSERT( index < joysticks.Size() );
return joysticks[index].GetGuid();
}
const HeapString& GetJoystickName(uint index) const
{
NST_ASSERT( index < joysticks.Size() );
return joysticks[index].GetName();
}
void Calibrate(uint index)
{
NST_ASSERT( index < joysticks.Size() );
joysticks[index].Calibrate();
}
bool SetAxisDeadZone(uint index,uint deadZone)
{
NST_ASSERT( index < joysticks.Size() );
return joysticks[index].SetAxisDeadZone( deadZone );
}
uint GetAxisDeadZone(uint index) const
{
NST_ASSERT( index < joysticks.Size() );
return joysticks[index].GetAxisDeadZone();
}
uint GetAvailableAxes(uint index) const
{
NST_ASSERT( index < joysticks.Size() );
return joysticks[index].GetAvailableAxes();
}
void SetScannerAxes(uint index,uint axes)
{
NST_ASSERT( index < joysticks.Size() );
return joysticks[index].SetScannerAxes( axes );
}
void SetScannerAxes(uint index,uint axes,bool state)
{
NST_ASSERT( index < joysticks.Size() );
return joysticks[index].SetScannerAxes( axes, state );
}
uint GetScannerAxes(uint index) const
{
NST_ASSERT( index < joysticks.Size() );
return joysticks[index].GetScannerAxes();
}
const uchar* GetKeyboardBuffer() const
{
return keyboard.GetBuffer();
}
bool AnyPressed()
{
Key key;
return ScanKey( key ) != SCAN_NO_KEY;
}
};
}
}
#endif
|