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
|
/**
bespoke synth, a software modular synthesizer
Copyright (C) 2021 Ryan Challinor (contact: awwbees@gmail.com)
This program 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
**/
/*
==============================================================================
SpaceMouseControl.cpp
Created: 25 Jan 2021 9:12:45pm
Author: Ryan Challinor
==============================================================================
*/
#include "SpaceMouseControl.h"
#if BESPOKE_SPACEMOUSE_SUPPORT
#include "OpenFrameworksPort.h"
#include "ModularSynth.h"
#include <Windows.h>
#include "3dxware/spwmacro.h" /* Common macros used by SpaceWare functions. */
#include "3dxware/si.h" /* Required for any SpaceWare support within an app.*/
#include "3dxware/siapp.h" /* Required for siapp.lib symbols */
#include "juce_core/juce_core.h"
#ifdef _MSC_VER
#pragma warning(disable : 4700)
#endif
struct SpaceMouseMessageWindow::Impl
{
Impl(ModularSynth& synth);
~Impl();
static LRESULT CALLBACK MyWndCBProc(HWND hwnd, UINT wm, WPARAM wParam, LPARAM lParam);
int SbInit(HWND hwndC);
void ApplyDeadZone(float& var, float deadzone);
void SbMotionEvent(SiSpwEvent* pEvent);
void SbZeroEvent();
void SbButtonPressEvent(int buttonnumber);
void SbButtonReleaseEvent(int buttonnumber);
void HandleDeviceChangeEvent(SiSpwEvent* pEvent);
LPCTSTR getClassNameFromAtom() const noexcept;
ATOM atom;
HWND hwnd;
SiHdl devHdl; /* Handle to 3D Mouse Device */
SiOpenData oData;
ModularSynth& mSynth;
bool mIsPanningOrZooming;
bool mIsTwisting;
static inline Impl* sInstance = nullptr;
};
SpaceMouseMessageWindow::SpaceMouseMessageWindow(ModularSynth& theSynth)
: d(std::make_unique<Impl>(theSynth))
{
}
SpaceMouseMessageWindow::Impl::Impl(ModularSynth& theSynth)
: mSynth(theSynth)
, mIsPanningOrZooming(false)
, mIsTwisting(false)
{
for (int i = 0; i < juce::JUCEApplication::getCommandLineParameterArray().size(); ++i)
{
juce::String element = juce::JUCEApplication::getCommandLineParameterArray()[i];
if (element == "-nospacemouse")
return;
}
sInstance = this;
juce::String className("JUCE_");
className << juce::String::toHexString(juce::Time::getHighResolutionTicks());
HMODULE moduleHandle = (HMODULE)juce::Process::getCurrentModuleInstanceHandle();
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof(wc);
wc.lpfnWndProc = MyWndCBProc;
wc.cbWndExtra = 4;
wc.hInstance = moduleHandle;
wc.lpszClassName = (LPCSTR)className.toWideCharPointer();
atom = RegisterClassEx(&wc);
jassert(atom != 0);
hwnd = CreateWindow(getClassNameFromAtom(), "SpaceMouseReader",
0, 0, 0, 0, 0, 0, 0, moduleHandle, 0);
jassert(hwnd != 0);
/* Initialise 3DxWare access / call to SbInit() */
SbInit(hwnd);
/* Implement message loop */
/*int bRet;
MSG msg; //incoming message to be evaluated
while (bRet = GetMessage(&msg, NULL, 0, 0))
{
if (bRet == -1) {
//handle the error and possibly exit
return;
}
else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}*/
}
SpaceMouseMessageWindow::Impl::~Impl()
{
DestroyWindow(hwnd);
UnregisterClass(getClassNameFromAtom(), 0);
}
//static
LRESULT CALLBACK SpaceMouseMessageWindow::Impl::MyWndCBProc(HWND hwnd, UINT wm, WPARAM wParam, LPARAM lParam)
{
SiSpwEvent Event; // SpaceWare Event
SiGetEventData EData; // SpaceWare Event Data
// initialize Window platform specific data for a call to SiGetEvent
SiGetEventWinInit(&EData, wm, wParam, lParam);
// check whether wm was a 3D mouse event and process it
//if (SiGetEvent (devHdl, SI_AVERAGE_EVENTS, &EData, &Event) == SI_IS_EVENT)
SpwRetVal retval = SiGetEvent(sInstance->devHdl, 0, &EData, &Event);
if (retval == SI_IS_EVENT)
{
if (Event.type == SI_MOTION_EVENT)
{
sInstance->SbMotionEvent(&Event); // process 3D mouse motion event
}
else if (Event.type == SI_ZERO_EVENT)
{
sInstance->SbZeroEvent(); // process 3D mouse zero event
}
else if (Event.type == SI_BUTTON_PRESS_EVENT)
{
sInstance->SbButtonPressEvent(Event.u.hwButtonEvent.buttonNumber); // process button press event
}
else if (Event.type == SI_BUTTON_RELEASE_EVENT)
{
sInstance->SbButtonReleaseEvent(Event.u.hwButtonEvent.buttonNumber); // process button release event
}
else if (Event.type == SI_DEVICE_CHANGE_EVENT)
{
//SbHandleDeviceChangeEvent(&Event); // process 3D mouse device change event
}
}
return DefWindowProc(hwnd, wm, wParam, lParam);
}
int SpaceMouseMessageWindow::Impl::SbInit(HWND hwndC)
{
int res; /* result of SiOpen, to be returned */
/*init the SpaceWare input library */
if (SiInitialize() == SPW_DLL_LOAD_ERROR)
{
std::cout << "Error: Could not load SiAppDll dll files" << std::endl;
}
else
{
//std::cout << "SiInitialize() done " << std::endl;
}
SiOpenWinInit(&oData, hwndC); /* init Win. platform specific data */
/* open data, which will check for device type and return the device handle to be used by this function */
if ((devHdl = SiOpen("AppSpaceMouse.exe", SI_ANY_DEVICE, SI_NO_MASK, SI_EVENT, &oData)) == NULL)
{
std::cout << "SiOpen error:" << std::endl;
SiTerminate(); /* called to shut down the SpaceWare input library */
std::cout << "SiTerminate()" << std::endl;
res = 0; /* could not open device */
return res;
}
SiDeviceName pname;
SiGetDeviceName(devHdl, &pname);
//std::cout << "devicename = " << pname.name << std::endl;
//SiSetUiMode(devHdl, SI_UI_ALL_CONTROLS); /* Config SoftButton Win Display */
SiGrabDevice(devHdl, SPW_TRUE); /* PREVENTS OTHER APPLICATIONS FROM RECEIVING 3D CONNEXION DATA !!! */
res = 1; /* opened device successfully */
return res;
}
void SpaceMouseMessageWindow::Impl::ApplyDeadZone(float& var, float deadzone)
{
if (abs(var) < deadzone)
var = 0;
else
var -= (var > 0 ? 1 : -1) * deadzone;
}
void SpaceMouseMessageWindow::Impl::SbMotionEvent(SiSpwEvent* pEvent)
{
const float kMax = 2100.0f;
float tx = ofClamp(pEvent->u.spwData.mData[SI_TX] / kMax, -1, 1);
float ty = ofClamp(pEvent->u.spwData.mData[SI_TY] / kMax, -1, 1);
float tz = ofClamp(pEvent->u.spwData.mData[SI_TZ] / kMax, -1, 1);
float rx = ofClamp(pEvent->u.spwData.mData[SI_RX] / kMax, -1, 1);
float ry = ofClamp(pEvent->u.spwData.mData[SI_RY] / kMax, -1, 1);
float rz = ofClamp(pEvent->u.spwData.mData[SI_RZ] / kMax, -1, 1);
float rawTwist = ry;
float rawZoom = ty;
ofVec2f rawPan(rz + tx, rx - tz);
float panMag = sqrtf(tx * tx + tz * tz);
float panAngle = atan2(-tz, tx);
float tiltMag = sqrtf(rz * rz + rx * rx);
float tiltAngle = atan2(rx, rz);
const float kPanDeadZone = .15f;
const float kTiltDeadZone = .15f;
const float kTwistDeadZone = .15f;
const float kZoomDownDeadZone = .2f;
const float kZoomUpDeadZone = .05f;
ApplyDeadZone(panMag, kPanDeadZone);
ApplyDeadZone(tiltMag, kTiltDeadZone);
ApplyDeadZone(ry, kTwistDeadZone);
if (ty < 0)
ApplyDeadZone(ty, kZoomDownDeadZone);
else
ApplyDeadZone(ty, kZoomUpDeadZone);
const float kPow = 1.5f;
panMag = pow(abs(panMag), kPow) * (panMag > 0 ? 1 : -1);
tiltMag = pow(abs(tiltMag), kPow) * (tiltMag > 0 ? 1 : -1);
ry = pow(abs(ry), kPow) * (ry > 0 ? 1 : -1);
ty = pow(abs(ty), kPow) * (ty > 0 ? 1 : -1);
tx = cos(panAngle) * panMag;
tz = -sin(panAngle) * panMag;
rz = cos(tiltAngle) * tiltMag;
rx = sin(tiltAngle) * tiltMag;
if (mIsTwisting) //if twisting, allow nothing else
tx = ty = tz = rx = rz = 0;
if (mIsPanningOrZooming) //if panning or zooming, allow no twisting
ry = 0;
//ofLog() << "TX=" << tx << " TY=" << ty << " TZ=" << tz << " RX=" << rx << " RY=" << ry << " RZ=" << rz;
const float kPanScale = -42;
const float kZoomScale = -.063f;
const float kTwistScale = -3.0f;
bool usingPan = false;
bool usingZoom = false;
bool usingTwist = false;
if (rz != 0 || tx != 0 || rx != 0 || tz != 0)
{
mSynth.PanView((rz + tx) * kPanScale, (rx - tz) * kPanScale);
usingPan = true;
mIsPanningOrZooming = true;
}
if (ty != 0)
{
mSynth.ZoomView(ty * kZoomScale, false);
usingZoom = true;
mIsPanningOrZooming = true;
}
if (ry != 0)
{
mSynth.MouseScrolled(0, ry * kTwistScale, true, false, false);
usingTwist = true;
mIsTwisting = true;
}
if (!mIsTwisting)
{
mSynth.SetRawSpaceMouseTwist(0, false);
mSynth.SetRawSpaceMouseZoom(rawZoom, usingZoom);
mSynth.SetRawSpaceMousePan(rawPan.x, rawPan.y, usingPan);
}
if (!mIsPanningOrZooming)
{
mSynth.SetRawSpaceMouseTwist(rawTwist, usingTwist);
mSynth.SetRawSpaceMouseZoom(0, false);
mSynth.SetRawSpaceMousePan(0, 0, false);
}
}
void SpaceMouseMessageWindow::Impl::SbZeroEvent()
{
mSynth.SetRawSpaceMouseTwist(0, false);
mSynth.SetRawSpaceMouseZoom(0, false);
mSynth.SetRawSpaceMousePan(0, 0, false);
mIsPanningOrZooming = false;
mIsTwisting = false;
}
void SpaceMouseMessageWindow::Impl::SbButtonPressEvent(int buttonnumber)
{
std::cout << "Buttonnumber : " << buttonnumber << std::endl;
}
void SpaceMouseMessageWindow::Impl::SbButtonReleaseEvent(int buttonnumber)
{
std::cout << "Buttonnumber : " << buttonnumber << std::endl;
}
void SpaceMouseMessageWindow::Impl::HandleDeviceChangeEvent(SiSpwEvent* pEvent)
{
std::cout << "HandleDeviceChangeEvent : " << std::endl;
}
void SpaceMouseMessageWindow::Poll()
{
int bRet;
MSG msg; //incoming message to be evaluated
while (PeekMessage(&msg, (HWND)0, 0, 0, PM_NOREMOVE))
{
if (bRet = GetMessage(&msg, NULL, 0, 0))
{
if (bRet == -1)
{
//handle the error and possibly exit
return;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
break;
}
}
}
LPCTSTR SpaceMouseMessageWindow::Impl::getClassNameFromAtom() const noexcept
{
return (LPCTSTR)(juce::pointer_sized_uint)atom;
}
#else
SpaceMouseMessageWindow::SpaceMouseMessageWindow(ModularSynth&) {}
#endif // BESPOKE_SPACEMOUSE_SUPPORT
SpaceMouseMessageWindow::~SpaceMouseMessageWindow() = default;
|