Permalink
Browse files

Added guide button support.

  • Loading branch information...
1 parent a79ff19 commit 806c73fdaf2a2dd643f5d7698035eb85689bd58b @00kyle00 committed Dec 6, 2012
Showing with 30 additions and 10 deletions.
  1. +1 −0 directinput/constants.lua
  2. +23 −7 directinput/controller.cpp
  3. +6 −3 directinput/controller.lua
@@ -18,6 +18,7 @@ CB_UP = 10
CB_DOWN = 11
CB_LEFT = 12
CB_RIGHT = 13
+CB_GUIDE = 14
-- keyboard virtual keys
-- see http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx
View
@@ -6,11 +6,10 @@
#include <cassert>
#include "virtual_devices.h"
#include "state.h"
-
#include <Xinput.h>
-#pragma comment(lib, "dinput8.lib")
+
#pragma comment(lib, "dxguid.lib")
-#pragma comment(lib, "xinput9_1_0.lib")
+#pragma comment(lib, "dinput8.lib")
namespace {
@@ -113,12 +112,12 @@ void input_from_device_state(const XINPUT_STATE& istate, InputState& ostate) {
ostate.triggers[0] = istate.Gamepad.bLeftTrigger / 255.0f;
ostate.triggers[1] = istate.Gamepad.bRightTrigger / 255.0f;
- WORD masks[] = {
+ const WORD masks[16] = {
XINPUT_GAMEPAD_A, XINPUT_GAMEPAD_B, XINPUT_GAMEPAD_X, XINPUT_GAMEPAD_Y,
XINPUT_GAMEPAD_LEFT_SHOULDER, XINPUT_GAMEPAD_RIGHT_SHOULDER, XINPUT_GAMEPAD_BACK,
XINPUT_GAMEPAD_START, XINPUT_GAMEPAD_LEFT_THUMB, XINPUT_GAMEPAD_RIGHT_THUMB,
XINPUT_GAMEPAD_DPAD_UP, XINPUT_GAMEPAD_DPAD_DOWN, XINPUT_GAMEPAD_DPAD_LEFT,
- XINPUT_GAMEPAD_DPAD_RIGHT };
+ XINPUT_GAMEPAD_DPAD_RIGHT, 1024 }; //1024 for guide button
for(int i = 0; i < 16; ++i)
ostate.buttons[i] = (istate.Gamepad.wButtons & masks[i]) != 0;
@@ -160,17 +159,34 @@ class ControllerDirectInput : public Controller {
std::shared_ptr<void> acquire_guard_;
};
+namespace {
+template<class T>
+void set_ptr(T& obj, void* value) {
+ obj = (T) value;
+}
+HMODULE xinput;
+int (__stdcall *XInputOrdinal100)(DWORD, XINPUT_STATE*);
+int (__stdcall *XInputSetState)(DWORD, XINPUT_VIBRATION*);
+}
+
class ControllerXInput : public Controller {
public:
ControllerXInput(int id) : id_(id) {
if(id < 0 || id > 3)
throw std::runtime_error("bad xinput controller id");
+
+ if(xinput == 0) {
+ xinput = LoadLibrary(L"xinput1_3.dll");
+ set_ptr(XInputOrdinal100, GetProcAddress(xinput, (LPCSTR)100));
+ set_ptr(XInputSetState, GetProcAddress(xinput, "XInputSetState"));
+ if( XInputOrdinal100 == 0 || XInputSetState == 0)
+ throw std::runtime_error("failed to load xinput funcitons");
+ }
}
void query_state(InputState& input) {
XINPUT_STATE state = {};
- XInputGetState(id_, &state);
-
+ XInputOrdinal100(id_, &state);
input_from_device_state(state, input);
}
@@ -2,12 +2,15 @@ require "constants"
-- Function is invoked whenever state of controller button is changed
function button_changed(btn)
- local btn_state = Controller.button(btn);
+ local btn_state = Controller.button(btn)
+ local value = btn_state and 1 or 0
+
if btn == CB_A then Mouse.press_release(MB_L, btn_state)
elseif btn == CB_B then Mouse.press_release(MB_R, btn_state)
elseif btn == CB_BCK then Keyboard.press_release(VK_ESCAPE, btn_state)
elseif btn == CB_X then Mouse.press_release(MB_M, btn_state)
elseif btn == CB_Y then Keyboard.press_release(VK_BROWSER_REFRESH, btn_state)
+ elseif btn == CB_GUIDE then Controller.rumble(value, value)
end
end
@@ -16,8 +19,8 @@ end
function tick()
Mouse.move(Controller.stick(0), 0.15, 5);
Mouse.move(Controller.stick(1), 0.15, 15);
- --Mouse.wheel(Controller.trigger(0) - Controller.trigger(1), 0.01, 50)
- Controller.rumble(Controller.trigger(0), Controller.trigger(1))
+ Mouse.wheel(Controller.trigger(0) - Controller.trigger(1), 0.01, 50)
+ --Controller.rumble(Controller.trigger(0), Controller.trigger(1))
end
--[[

0 comments on commit 806c73f

Please sign in to comment.