diff options
| author | Jordan Woyak <jordan.woyak@gmail.com> | 2010-06-21 03:12:16 +0000 |
|---|---|---|
| committer | Jordan Woyak <jordan.woyak@gmail.com> | 2010-06-21 03:12:16 +0000 |
| commit | 9e3b65368873fd26f21e2568b2c67d00ec96d6a5 (patch) | |
| tree | b5eff0d77ae7e4327069116f6440dcc2a70a1a5f /Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboard.cpp | |
| parent | fde15c1bc633792bead817b2f87faf54477765b5 (diff) | |
GCPad/New Wiimote Plugin: Individual keyboard and mouse devices are now listed on Windows(2 player with 2 keyboards possible). Improved the ability to map multiple inputs to the same control. Inputs from different devices can be mapped to the same button (example: Mouse Left and XInput A). More advanced mappings such as "Button 1 or 2 and NOT button 3" are possible. I hope the GUI after right clicking a button isn't too confusing(may change it to be a bit more user friendly). Hopefully, I didn't break OSX stuff by 'const'ing a few functions.
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5757 8ced0084-cf51-0410-be5f-012b33b47a6e
Diffstat (limited to 'Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboard.cpp')
| -rw-r--r-- | Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboard.cpp | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboard.cpp b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboard.cpp new file mode 100644 index 0000000000..6f39467deb --- /dev/null +++ b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputKeyboard.cpp @@ -0,0 +1,187 @@ +#include "../ControllerInterface.h"
+
+#ifdef CIFACE_USE_DIRECTINPUT_KEYBOARD
+
+#include "DirectInputKeyboard.h"
+#include "DirectInput.h"
+
+// TODO: maybe add a ClearInputState function to this device
+
+namespace ciface
+{
+namespace DirectInput
+{
+
+struct
+{
+ const BYTE code;
+ const char* const name;
+} named_keys[] =
+{
+#include "NamedKeys.h"
+};
+
+struct
+{
+ const BYTE code;
+ const char* const name;
+} named_lights[] =
+{
+ { VK_NUMLOCK, "NUM LOCK" },
+ { VK_CAPITAL, "CAPS LOCK" },
+ { VK_SCROLL, "SCROLL LOCK" }
+};
+
+void InitKeyboard(IDirectInput8* const idi8, std::vector<ControllerInterface::Device*>& devices)
+{
+ LPDIRECTINPUTDEVICE8 kb_device;
+
+ unsigned int kb_count = 0;
+
+ std::list<DIDEVICEINSTANCE> keyboards;
+ idi8->EnumDevices(DI8DEVCLASS_KEYBOARD, DIEnumDevicesCallback, (LPVOID)&keyboards, DIEDFL_ATTACHEDONLY);
+
+ // add other keyboard devices
+ std::list<DIDEVICEINSTANCE>::iterator
+ i = keyboards.begin(),
+ e = keyboards.end();
+ for (; i!=e; ++i)
+ {
+ if (SUCCEEDED(idi8->CreateDevice(i->guidInstance, &kb_device, NULL)))
+ {
+ if (SUCCEEDED(kb_device->SetDataFormat(&c_dfDIKeyboard)))
+ if (SUCCEEDED(kb_device->SetCooperativeLevel(NULL, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
+ {
+ devices.push_back(new Keyboard(kb_device, kb_count++));
+ return;
+ }
+ }
+ kb_device->Release();
+ }
+}
+
+Keyboard::~Keyboard()
+{
+ // kb
+ m_kb_device->Unacquire();
+ m_kb_device->Release();
+
+}
+
+Keyboard::Keyboard(const LPDIRECTINPUTDEVICE8 kb_device, const int index)
+ : m_kb_device(kb_device)
+ , m_index(index)
+{
+ m_kb_device->Acquire();
+
+ ZeroMemory(&m_state_in, sizeof(m_state_in));
+ ZeroMemory(m_state_out, sizeof(m_state_out));
+ ZeroMemory(&m_current_state_out, sizeof(m_current_state_out));
+
+ // KEYBOARD
+ // add keys
+ for (unsigned int i = 0; i < sizeof(named_keys)/sizeof(*named_keys); ++i)
+ AddInput(new Key(i));
+ // add lights
+ for (unsigned int i = 0; i < sizeof(named_lights)/sizeof(*named_lights); ++i)
+ AddOutput(new Light(i));
+}
+
+bool Keyboard::UpdateInput()
+{
+ HRESULT hr = m_kb_device->GetDeviceState(sizeof(m_state_in), m_state_in);
+
+ if (DIERR_INPUTLOST == hr || DIERR_NOTACQUIRED == hr)
+ m_kb_device->Acquire();
+
+ return SUCCEEDED(hr);
+}
+
+bool Keyboard::UpdateOutput()
+{
+ class KInput : public INPUT
+ {
+ public:
+ KInput( const unsigned char key, const bool up = false )
+ {
+ memset( this, 0, sizeof(*this) );
+ type = INPUT_KEYBOARD;
+ ki.wVk = key;
+ if (up) ki.dwFlags = KEYEVENTF_KEYUP;
+ }
+ };
+
+ std::vector< KInput > kbinputs;
+ for ( unsigned int i = 0; i < sizeof(m_state_out)/sizeof(*m_state_out); ++i )
+ {
+ bool want_on = false;
+ if ( m_state_out[i] )
+ want_on = m_state_out[i] > wxGetLocalTimeMillis() % 255 ; // light should flash when output is 0.5
+
+ // lights are set to their original state when output is zero
+ if ( want_on ^ m_current_state_out[i] )
+ {
+ kbinputs.push_back( KInput( named_lights[i].code ) ); // press
+ kbinputs.push_back( KInput( named_lights[i].code, true ) ); // release
+
+ m_current_state_out[i] ^= 1;
+ }
+ }
+
+ if (kbinputs.size())
+ return ( kbinputs.size() == SendInput( (UINT)kbinputs.size(), &kbinputs[0], sizeof( kbinputs[0] ) ) );
+ else
+ return true;
+}
+
+std::string Keyboard::GetName() const
+{
+ return "Keyboard";
+}
+
+int Keyboard::GetId() const
+{
+ return m_index;
+}
+
+std::string Keyboard::GetSource() const
+{
+ return DINPUT_SOURCE_NAME;
+}
+
+ControlState Keyboard::GetInputState(const ControllerInterface::Device::Input* const input) const
+{
+ return (((Input*)input)->GetState(m_state_in));
+}
+
+void Keyboard::SetOutputState(const ControllerInterface::Device::Output* const output, const ControlState state)
+{
+ ((Output*)output)->SetState(state, m_state_out);
+}
+
+// names
+std::string Keyboard::Key::GetName() const
+{
+ return named_keys[m_index].name;
+}
+
+std::string Keyboard::Light::GetName() const
+{
+ return named_lights[ m_index ].name;
+}
+
+// get/set state
+ControlState Keyboard::Key::GetState(const BYTE keys[]) const
+{
+ return (keys[named_keys[m_index].code] != 0);
+}
+
+void Keyboard::Light::SetState( const ControlState state, unsigned char* const state_out )
+{
+ state_out[m_index] = state * 255;
+}
+
+}
+}
+
+#endif
|
