Skip to content

Commit

Permalink
Added vJoy support.
Browse files Browse the repository at this point in the history
Cleaned up scripts a little.
Added oculus_touch_vjoy.ahk, which emulates a simple gamepad. The grip triggers are bound as buttons, index triggers as the Z and RZ axes.
  • Loading branch information
rajetic committed Sep 5, 2018
1 parent cfe732d commit 246cce7
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 29 deletions.
10 changes: 6 additions & 4 deletions auto_oculus_touch/auto_oculus_touch.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_WINDOWS;_USRDLL;AUTOOCULUSTOUCH_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>D:\sdk\OculusSDK126\LibOVR\Include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>D:\sdk\OculusSDK126\LibOVR\Include;D:\sdk\vjoy\inc</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>D:\sdk\OculusSDK126\LibOVR\Lib\Windows\x64\Release\VS2015;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>D:\sdk\OculusSDK126\LibOVR\Lib\Windows\x64\Release\VS2015;D:\sdk\vjoy\lib\amd64;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>vJoyInterface.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -78,14 +79,15 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_WINDOWS;_USRDLL;AUTOOCULUSTOUCH_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>D:\sdk\OculusSDK126\LibOVR\Include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>D:\sdk\OculusSDK126\LibOVR\Include;D:\sdk\vjoy\inc</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>D:\sdk\OculusSDK126\LibOVR\Lib\Windows\x64\Release\VS2015;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>D:\sdk\OculusSDK126\LibOVR\Lib\Windows\x64\Release\VS2015;D:\sdk\vjoy\lib\amd64;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>vJoyInterface.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
62 changes: 62 additions & 0 deletions auto_oculus_touch/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
// https://opensource.org/licenses/MIT
///////////////////////////////////////////////////////////////////////////////

#define ROBUST
#include "stdafx.h"
#include "OVR_CAPI.h"
#include "cmath"
#include "Extras\OVR_Math.h"
#include "public.h"
#include "vjoyinterface.h"

#pragma comment(lib,"LibOVR")

Expand All @@ -34,6 +37,8 @@ ovrVector3f g_yAxis = { 0,1,0 }; // Y axis of reset tracking coordinate system
ovrVector3f g_zAxis = { 0,0,1 }; // Z axis of reset tracking coordinate system
ovrVector3f g_origin = { 0,0,0 }; // Origin of reset tracking coordinate system

// vJoy
int g_vjoy = -1;

// Functions exported to AutoHotkey
extern "C"
Expand Down Expand Up @@ -64,6 +69,7 @@ extern "C"
return 0;

result = ovr_Create(&g_HMD, &g_luid);

return g_HMD ? 1 : 0;
}

Expand Down Expand Up @@ -99,6 +105,57 @@ extern "C"
}
}

// Initialise a vJoy device. Currently only a single device can be used by a single auto_oculus_touch script.
// device - index from 1 to the number of vJoy devices
// return - 0 if init failed, 1 if succeeded
__declspec(dllexport) int initvJoy(unsigned int device)
{
g_vjoy = -1;
if (vJoyEnabled())
{
WORD VerDll, VerDrv;
if (DriverMatch(&VerDll, &VerDrv))
{
VjdStat status = GetVJDStatus(device);
if (status == VJD_STAT_FREE)
{
if(AcquireVJD(device))
{
g_vjoy = device;
}
}
}
}
return g_vjoy > -1 ? 1 : 0;
}

// Set the state of a vJoy axis.
// value - a float value from -1 to 1
// hid - the HID usage code of the axis. These are listed in the auto_oculus_touch.ahk file as HID_USAGE_*
__declspec(dllexport) void setvJoyAxis(float value, unsigned int hid)
{
if (g_vjoy > -1)
{
if (value < -1.0f)
value = -1.0f;
if (value > 1.0f)
value = 1.0f;
long v = long((value*0.5f + 0.5f) * 0x7999) + 1;
SetAxis(v, g_vjoy, hid);
}
}

// Set the state of a vJoy button.
// value - 0==not pressed, 1==pressed
// button - an index from 1 to the number of buttons set in vJoy
__declspec(dllexport) void setvJoyButton(unsigned int value, unsigned int button)
{
if (g_vjoy > -1)
{
SetBtn(value, g_vjoy, button);
}
}

// Sets the current desired vibration pattern.
// controller - 0==left, 1==right
// frequency - vibration at 1==320Hz, 2==160Hz, 3==106.7Hz, 4=80Hz
Expand Down Expand Up @@ -304,6 +361,11 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReser
case DLL_PROCESS_DETACH:
if (g_HMD)
{
if (g_vjoy > -1)
{
RelinquishVJD(g_vjoy);
g_vjoy = -1;
}
ovr_Destroy(g_HMD);
}
ovr_Shutdown();
Expand Down
68 changes: 65 additions & 3 deletions bin/auto_oculus_touch.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,59 @@ HandTrigger := 1
XAxis := 0
YAxis := 1

; vJoy defines
HID_USAGE_X := 0x30 ; Left gamepad thumbstick x
HID_USAGE_Y := 0x31 ; Left gamepad thumbstick y
HID_USAGE_Z := 0x32 ; Left gamepad trigger
HID_USAGE_RX := 0x33 ; Right gamepad thumbstick x
HID_USAGE_RY := 0x34 ; Right gamepad thumbstick y
HID_USAGE_RZ := 0x35 ; Right gamepad trigger
HID_USAGE_SL0 := 0x36
HID_USAGE_SL1 := 0x37
HID_USAGE_WHL := 0x38
HID_USAGE_POV := 0x39


InitOculus()
{
DllCall("auto_oculus_touch\initOculus")
return DllCall("auto_oculus_touch\initOculus", "UInt")
}

Poll()
{
DllCall("auto_oculus_touch\poll")
}

GetButtonsDown()
{
return DllCall("auto_oculus_touch\getButtonsDown")
}

GetButtonsReleased()
{
return DllCall("auto_oculus_touch\getButtonsReleased")
}

GetButtonsPressed()
{
return DllCall("auto_oculus_touch\getButtonsPressed")
}

GetTouchDown()
{
return DllCall("auto_oculus_touch\getTouchDown")
}

GetTouchPressed()
{
return DllCall("auto_oculus_touch\getTouchPressed")
}

GetTouchReleased()
{
return DllCall("auto_oculus_touch\getTouchReleased")
}

GetTrigger(hand, trigger)
{
return DllCall("auto_oculus_touch\getTrigger", "Int", hand, "Int", trigger, "Float")
Expand Down Expand Up @@ -104,7 +147,26 @@ ResetFacing(controller)
DllCall("auto_oculus_touch\resetFacing", "UInt", controller)
}

; Grab the library.
hModule := DllCall("LoadLibrary", "Str", "auto_oculus_touch.dll", "Ptr")
InitvJoy(device)
{
return DllCall("auto_oculus_touch\initvJoy", "UInt", device, "UInt")
}

SetvJoyAxis(axis, value)
{
return DllCall("auto_oculus_touch\setvJoyAxis", "Float", value, "UInt", axis)
}

SetvJoyAxisU(axis, value)
{
return DllCall("auto_oculus_touch\setvJoyAxis", "Float", value*2-1, "UInt", axis)
}

SetvJoyButton(button, value)
{
return DllCall("auto_oculus_touch\setvJoyButton", "UInt", value, "UInt", button)
}


; Grab the library.
hModule := DllCall("LoadLibrary", "Str", "auto_oculus_touch.dll", "Ptr")
14 changes: 7 additions & 7 deletions bin/oculus_remote_mouse.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ speedY := 1
; Main polling loop.
Loop {
; Grab the latest Oculus input state (Touch, Remote and Xbox One).
DllCall("auto_oculus_touch\poll")
Poll()

; Get button states.
; Down is the current state. If you test with this, you get a key every poll it is down. Repeating.
; Pressed is set if transitioned to down in the last poll. Non repeating.
; Released is set if transitioned to up in the last poll. Non repeating.
down := DllCall("auto_oculus_touch\getButtonsDown")
pressed := DllCall("auto_oculus_touch\getButtonsPressed")
released := DllCall("auto_oculus_touch\getButtonsReleased")
touchDown := DllCall("auto_oculus_touch\getTouchDown")
touchPressed := DllCall("auto_oculus_touch\getTouchPressed")
touchReleased := DllCall("auto_oculus_touch\getTouchReleased")
down := GetButtonsDown()
pressed := GetButtonsPressed()
released := GetButtonsReleased()
touchDown := GetTouchDown()
touchPressed := GetTouchPressed()
touchReleased := GetTouchReleased()

; Now to do something with them.

Expand Down
8 changes: 7 additions & 1 deletion bin/oculus_remote_spotify.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ Loop {
Poll()

; Pressed is set if transitioned to down in the last poll. Non repeating.
pressed := DllCall("auto_oculus_touch\getButtonsPressed")
down := GetButtonsDown()
pressed := GetButtonsPressed()
released := GetButtonsReleased()
touchDown := GetTouchDown()
touchPressed := GetTouchPressed()
touchReleased := GetTouchReleased()


; Use the up/down on the Oculus remote to cycle media tracks and the centre button to play/pause.
if released & ovrDown
Expand Down
14 changes: 7 additions & 7 deletions bin/oculus_touch_mouse.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ InitOculus()
; Main polling loop.
Loop {
; Grab the latest Oculus input state (Touch, Remote and Xbox One).
DllCall("auto_oculus_touch\poll")
Poll()

; Get the various analog values. Triggers are 0.0-1.0, thumbsticks are -1.0-1.0
rightIndexTrigger := GetTrigger(RightHand, IndexTrigger)
Expand All @@ -21,12 +21,12 @@ Loop {
; Down is the current state. If you test with this, you get a key every poll it is down. Repeating.
; Pressed is set if transitioned to down in the last poll. Non repeating.
; Released is set if transitioned to up in the last poll. Non repeating.
down := DllCall("auto_oculus_touch\getButtonsDown")
pressed := DllCall("auto_oculus_touch\getButtonsPressed")
released := DllCall("auto_oculus_touch\getButtonsReleased")
touchDown := DllCall("auto_oculus_touch\getTouchDown")
touchPressed := DllCall("auto_oculus_touch\getTouchPressed")
touchReleased := DllCall("auto_oculus_touch\getTouchReleased")
down := GetButtonsDown()
pressed := GetButtonsPressed()
released := GetButtonsReleased()
touchDown := GetTouchDown()
touchPressed := GetTouchPressed()
touchReleased := GetTouchReleased()

; Now to do something with them.

Expand Down
14 changes: 7 additions & 7 deletions bin/oculus_touch_test.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ ResetFacing(1)

Loop {
; Grab the latest Oculus input state (Touch, Remote and Xbox One).
DllCall("auto_oculus_touch\poll")
Poll()

; Get the various analog values. Triggers are 0.0-1.0, thumbsticks are -1.0-1.0
leftIndexTrigger := GetTrigger(LeftHand, IndexTrigger)
Expand All @@ -66,12 +66,12 @@ Loop {
; Down is the current state. If you test with this, you get a key every poll it is down. Repeating.
; Pressed is set if transitioned to down in the last poll. Non repeating.
; Released is set if transitioned to up in the last poll. Non repeating.
down := DllCall("auto_oculus_touch\getButtonsDown")
pressed := DllCall("auto_oculus_touch\getButtonsPressed")
released := DllCall("auto_oculus_touch\getButtonsReleased")
touchDown := DllCall("auto_oculus_touch\getTouchDown")
touchPressed := DllCall("auto_oculus_touch\getTouchPressed")
touchReleased := DllCall("auto_oculus_touch\getTouchReleased")
down := GetButtonsDown()
pressed := GetButtonsPressed()
released := GetButtonsReleased()
touchDown := GetTouchDown()
touchPressed := GetTouchPressed()
touchReleased := GetTouchReleased()
lx := leftX*50+50
ly := leftY*50+50
rx := rightX*50+50
Expand Down
77 changes: 77 additions & 0 deletions bin/oculus_touch_vjoy.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include auto_oculus_touch.ahk

; This is used to treat the trigger like a button. We need to remember the old state.
oldTrigger:=0

; Start the Oculus sdk.
InitOculus()
InitvJoy(1)

; Main polling loop.
Loop {
; Grab the latest Oculus input state (Touch, Remote and Xbox One).
Poll()

; Get the various analog values. Triggers are 0.0-1.0, thumbsticks are -1.0-1.0
leftIndexTrigger := GetTrigger(LeftHand, IndexTrigger)
leftHandTrigger := GetTrigger(LeftHand, HandTrigger)
leftX := GetThumbStick(LeftHand, XAxis)
leftY := GetThumbStick(LeftHand, YAxis)
rightIndexTrigger := GetTrigger(RightHand, IndexTrigger)
rightHandTrigger := GetTrigger(RightHand, HandTrigger)
rightX := GetThumbStick(RightHand, XAxis)
rightY := GetThumbStick(RightHand, YAxis)

; Get button states.
; Down is the current state. If you test with this, you get a key every poll it is down. Repeating.
; Pressed is set if transitioned to down in the last poll. Non repeating.
; Released is set if transitioned to up in the last poll. Non repeating.
down := GetButtonsDown()
pressed := GetButtonsPressed()
released := GetButtonsReleased()
touchDown := GetTouchDown()
touchPressed := GetTouchPressed()
touchReleased := GetTouchReleased()

; Now to do something with them.
SetvJoyAxis(HID_USAGE_X, leftX)
SetvJoyAxis(HID_USAGE_Y, -leftY)
SetvJoyAxis(HID_USAGE_RX, rightX)
SetvJoyAxis(HID_USAGE_RY, -rightY)
SetvJoyAxisU(HID_USAGE_Z, leftIndexTrigger)
SetvJoyAxisU(HID_USAGE_RZ, rightIndexTrigger)


if pressed & ovrA
SetvJoyButton(1,1)
if released & ovrA
SetvJoyButton(1,0)
if pressed & ovrB
SetvJoyButton(2,1)
if released & ovrB
SetvJoyButton(2,0)
if pressed & ovrX
SetvJoyButton(3,1)
if released & ovrX
SetvJoyButton(3,0)
if pressed & ovrY
SetvJoyButton(4,1)
if released & ovrY
SetvJoyButton(4,0)
if pressed & ovrEnter
SetvJoyButton(5,1)
if released & ovrEnter
SetvJoyButton(5,0)

if leftHandTrigger > 0.7
SetvJoyButton(6,1)
else
SetvJoyButton(6,0)
if rightHandTrigger > 0.7
SetvJoyButton(7,1)
else
SetvJoyButton(7,0)

Sleep, 10
}

Loading

0 comments on commit 246cce7

Please sign in to comment.