| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2018 Torsten Sadowski <tsadowski[at]gmx.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
/* |
| 24 |
Development tools and related technology provided under license from 3Dconnexion. |
| 25 |
(c) 1992 - 2012 3Dconnexion. All rights reserved |
| 26 |
*/ |
| 27 |
|
| 28 |
/* |
| 29 |
Support for Qt added by David Dibben at |
| 30 |
http://www.codegardening.com/2011/02/using-3dconnexion-mouse-with-qt.html |
| 31 |
*/ |
| 32 |
|
| 33 |
/* |
| 34 |
See also: |
| 35 |
http://www.3dconnexion.com/forum/viewtopic.php?f=19&t=4968&sid=72c018bdcf0e6edc99a6effb5c0c48d9 |
| 36 |
*/ |
| 37 |
|
| 38 |
#include <FCConfig.h> |
| 39 |
|
| 40 |
#include "GuiNativeEventWin32.h" |
| 41 |
|
| 42 |
#include <QGlobalStatic> |
| 43 |
#include <QMainWindow> |
| 44 |
#include <QWidget> |
| 45 |
#include <Base/Console.h> |
| 46 |
#include "GuiApplicationNativeEventAware.h" |
| 47 |
#include "GuiRawInputEventFilter.h" |
| 48 |
|
| 49 |
Gui::GuiNativeEvent* Gui::GuiNativeEvent::gMouseInput = 0; |
| 50 |
|
| 51 |
|
| 52 |
// Windows dependencies, enumerators and global variables |
| 53 |
|
| 54 |
#include <QApplication> |
| 55 |
#include <windows.h> |
| 56 |
#include <cmath> |
| 57 |
#include <map> |
| 58 |
|
| 59 |
#define LOGITECH_VENDOR_ID 0x46d |
| 60 |
#define CONNEXION_VENDOR_ID 0x256f |
| 61 |
#define _CONSTANT_INPUT_PERIOD 0 |
| 62 |
|
| 63 |
#ifndef RIDEV_DEVNOTIFY |
| 64 |
# define RIDEV_DEVNOTIFY 0x00002000 |
| 65 |
#endif |
| 66 |
|
| 67 |
#define _TRACE_WM_INPUT_PERIOD 0 |
| 68 |
#define _TRACE_RI_TYPE 0 |
| 69 |
#define _TRACE_RIDI_DEVICENAME 0 |
| 70 |
#define _TRACE_RIDI_DEVICEINFO 0 |
| 71 |
#define _TRACE_RI_RAWDATA 0 |
| 72 |
#define _TRACE_3DINPUT_PERIOD 0 |
| 73 |
|
| 74 |
#ifdef _WIN64 |
| 75 |
typedef unsigned __int64 QWORD; |
| 76 |
#endif // _WIN64 |
| 77 |
|
| 78 |
static const int kTimeToLive = 5; |
| 79 |
|
| 80 |
/* |
| 81 |
List of all devices |
| 82 |
https://www.3dconnexion.com/nc/service/faq/faq/how-can-i-check-if-my-usb-3d-mouse-is-recognized-by-windows.html |
| 83 |
|
| 84 |
Supported: |
| 85 |
SpacePilot: USB\0x046d:0xc625 |
| 86 |
SpaceExplorer: USB\0x046d:0xc627 |
| 87 |
SpaceMouse Wireless (cabled): USB\0x256f:0xc62e |
| 88 |
SpaceMouse Wireless Receiver: USB\0x256f:0xc62f |
| 89 |
SpaceMouse Pro Wireless (cabled): USB\0x256d:0xc631 |
| 90 |
SpaceMouse Pro Wireless Receiver: USB\0x256f:0xc632 |
| 91 |
|
| 92 |
Supported (but works differently): |
| 93 |
SpaceMouse Plus USB: USB\0x046d:0xc603 |
| 94 |
SpaceMouse Plus XT USB: USB\0x046d:0xc603 |
| 95 |
|
| 96 |
Unknown status: |
| 97 |
SpaceNavigator: USB\0x046d:0xc626 |
| 98 |
SpaceNavigator for Notebooks: USB\0x046d:0xc628 |
| 99 |
SpacePilot Pro: USB\0x046d:0xc629 |
| 100 |
SpaceMouse Enterprise: USB\0x256f:0xc633 |
| 101 |
|
| 102 |
Not yet supported: |
| 103 |
SpaceMouse Compact: USB\0x256f:0xc635 |
| 104 |
|
| 105 |
Not supported: |
| 106 |
CadMan: USB\0x046d:0xc605 |
| 107 |
SpaceMouse Classic USB: USB\0x046d:0xc606 |
| 108 |
SpaceBall 5000 USB: USB\0x046d:0xc621 |
| 109 |
SpaceTraveler: USB\0x046d:0xc623 |
| 110 |
SpaceMouse Pro: USB\0x046d:0xc62b |
| 111 |
CadMouse: USB\0x256f:0xc650 |
| 112 |
CadMouse Pro Wireless: USB\0x256f:0xc654 |
| 113 |
CadMouse Pro Wireless Left: USB\0x256f:0xc657 |
| 114 |
CadMouse Wireless: USB\0x256f:0xc651 |
| 115 |
Universal Receiver: USB\0x256f:0xc652 |
| 116 |
*/ |
| 117 |
|
| 118 |
enum e3dconnexion_pid |
| 119 |
{ |
| 120 |
eSpaceMousePlusXT = 0xc603, |
| 121 |
eSpacePilot = 0xc625, |
| 122 |
eSpaceNavigator = 0xc626, |
| 123 |
eSpaceExplorer = 0xc627, |
| 124 |
eSpaceNavigatorForNotebooks = 0xc628, |
| 125 |
eSpacePilotPRO = 0xc629, |
| 126 |
eSpaceMouseWireless = 0xc62e, |
| 127 |
eSpaceMouseWirelessReceiver = 0xc62f, |
| 128 |
eSpaceMousePROWireless = 0xc631, |
| 129 |
eSpaceMousePROWirelessReceiver = 0xc632, |
| 130 |
eSpaceMouseEnterprise = 0xc633, |
| 131 |
eSpaceMouseCompact = 0xc635 |
| 132 |
}; |
| 133 |
|
| 134 |
enum e3dmouse_virtual_key |
| 135 |
{ |
| 136 |
V3DK_INVALID = 0, |
| 137 |
V3DK_MENU = 1, |
| 138 |
V3DK_FIT, |
| 139 |
V3DK_TOP, |
| 140 |
V3DK_LEFT, |
| 141 |
V3DK_RIGHT, |
| 142 |
V3DK_FRONT, |
| 143 |
V3DK_BOTTOM, |
| 144 |
V3DK_BACK, |
| 145 |
V3DK_CW, |
| 146 |
V3DK_CCW, |
| 147 |
V3DK_ISO1, |
| 148 |
V3DK_ISO2, |
| 149 |
V3DK_1, |
| 150 |
V3DK_2, |
| 151 |
V3DK_3, |
| 152 |
V3DK_4, |
| 153 |
V3DK_5, |
| 154 |
V3DK_6, |
| 155 |
V3DK_7, |
| 156 |
V3DK_8, |
| 157 |
V3DK_9, |
| 158 |
V3DK_10, |
| 159 |
V3DK_ESC, |
| 160 |
V3DK_ALT, |
| 161 |
V3DK_SHIFT, |
| 162 |
V3DK_CTRL, |
| 163 |
V3DK_ROTATE, |
| 164 |
V3DK_PANZOOM, |
| 165 |
V3DK_DOMINANT, |
| 166 |
V3DK_PLUS, |
| 167 |
V3DK_MINUS |
| 168 |
}; |
| 169 |
|
| 170 |
// SpaceMouse Enterprise: map to virtual keycodes |
| 171 |
std::map<unsigned long, unsigned short> mapEnterpriseKey = { |
| 172 |
{0x0002, 0x01} // V3DK_MENU |
| 173 |
, |
| 174 |
{0x0004, 0x02} // V3DK_FIT |
| 175 |
, |
| 176 |
{0x0006, 0x03} // V3DK_TOP |
| 177 |
, |
| 178 |
{0x0007, 0x04} // V3DK_BOTTOM |
| 179 |
, |
| 180 |
{0x000A, 0x05} // V3DK_RIGHT |
| 181 |
, |
| 182 |
{0x000B, 0x06} // V3DK_LEFT |
| 183 |
, |
| 184 |
{0x000C, 0x07} // V3DK_FRONT |
| 185 |
, |
| 186 |
{0x000D, 0x08} // V3DK_BACK |
| 187 |
, |
| 188 |
{0x0012, 0x09} // V3DK_CW |
| 189 |
, |
| 190 |
{0x0013, 0x0a} // V3DK_CCW |
| 191 |
, |
| 192 |
{0x0016, 0x0b} // V3DK_ISO1 |
| 193 |
, |
| 194 |
{0x0017, 0x0c} // V3DK_ISO2 |
| 195 |
, |
| 196 |
{0x001a, 0x0d} // V3DK_1 |
| 197 |
, |
| 198 |
{0x001c, 0x0e} // V3DK_2 |
| 199 |
, |
| 200 |
{0x001e, 0x0f} // V3DK_3 |
| 201 |
, |
| 202 |
{0x0020, 0x10} // V3DK_4 |
| 203 |
, |
| 204 |
{0x0022, 0x11} // V3DK_5 |
| 205 |
, |
| 206 |
{0x0024, 0x12} // V3DK_6 |
| 207 |
, |
| 208 |
{0x0026, 0x13} // V3DK_7 |
| 209 |
, |
| 210 |
{0x0028, 0x14} // V3DK_8 |
| 211 |
, |
| 212 |
{0x002a, 0x15} // V3DK_9 |
| 213 |
, |
| 214 |
{0x002c, 0x16} // V3DK_10 |
| 215 |
, |
| 216 |
{0x0090, 0x17} // V3DK_11 |
| 217 |
, |
| 218 |
{0x0092, 0x18} // V3DK_12 |
| 219 |
, |
| 220 |
{0x002e, 0x19} // V3DK_ESC |
| 221 |
, |
| 222 |
{0x0030, 0x1a} // V3DK_ALT |
| 223 |
, |
| 224 |
{0x0032, 0x1b} // V3DK_SHIFT |
| 225 |
, |
| 226 |
{0x0034, 0x1c} // V3DK_CTRL |
| 227 |
, |
| 228 |
{0x0048, 0x1d} // V3DK_ENTER |
| 229 |
, |
| 230 |
{0x004a, 0x1e} // V3DK_DEL |
| 231 |
, |
| 232 |
{0x015e, 0x1f} // V3DK_TAB |
| 233 |
, |
| 234 |
{0x0160, 0x20} // V3DK_SPACE |
| 235 |
}; |
| 236 |
|
| 237 |
struct tag_VirtualKeys |
| 238 |
{ |
| 239 |
e3dconnexion_pid pid; |
| 240 |
size_t nKeys; |
| 241 |
e3dmouse_virtual_key* vkeys; |
| 242 |
}; |
| 243 |
|
| 244 |
static const e3dmouse_virtual_key SpaceMouseWirelessKeys[] = { |
| 245 |
V3DK_INVALID // there is no button 0 |
| 246 |
, |
| 247 |
V3DK_1, |
| 248 |
V3DK_2, |
| 249 |
V3DK_TOP, |
| 250 |
V3DK_LEFT, |
| 251 |
V3DK_RIGHT, |
| 252 |
V3DK_FRONT, |
| 253 |
V3DK_ESC, |
| 254 |
V3DK_ALT, |
| 255 |
V3DK_SHIFT, |
| 256 |
V3DK_CTRL, |
| 257 |
V3DK_FIT, |
| 258 |
V3DK_MENU, |
| 259 |
V3DK_PLUS, |
| 260 |
V3DK_MINUS, |
| 261 |
V3DK_ROTATE |
| 262 |
}; |
| 263 |
|
| 264 |
static const e3dmouse_virtual_key SpaceMouseWirelessReceiverKeys[] = { |
| 265 |
V3DK_INVALID // there is no button 0 |
| 266 |
, |
| 267 |
V3DK_1, |
| 268 |
V3DK_2, |
| 269 |
V3DK_TOP, |
| 270 |
V3DK_LEFT, |
| 271 |
V3DK_RIGHT, |
| 272 |
V3DK_FRONT, |
| 273 |
V3DK_ESC, |
| 274 |
V3DK_ALT, |
| 275 |
V3DK_SHIFT, |
| 276 |
V3DK_CTRL, |
| 277 |
V3DK_FIT, |
| 278 |
V3DK_MENU, |
| 279 |
V3DK_PLUS, |
| 280 |
V3DK_MINUS, |
| 281 |
V3DK_ROTATE |
| 282 |
}; |
| 283 |
|
| 284 |
static const e3dmouse_virtual_key SpaceExplorerKeys[] = { |
| 285 |
V3DK_INVALID // there is no button 0 |
| 286 |
, |
| 287 |
V3DK_1, |
| 288 |
V3DK_2, |
| 289 |
V3DK_TOP, |
| 290 |
V3DK_LEFT, |
| 291 |
V3DK_RIGHT, |
| 292 |
V3DK_FRONT, |
| 293 |
V3DK_ESC, |
| 294 |
V3DK_ALT, |
| 295 |
V3DK_SHIFT, |
| 296 |
V3DK_CTRL, |
| 297 |
V3DK_FIT, |
| 298 |
V3DK_MENU, |
| 299 |
V3DK_PLUS, |
| 300 |
V3DK_MINUS, |
| 301 |
V3DK_ROTATE |
| 302 |
}; |
| 303 |
|
| 304 |
static const e3dmouse_virtual_key SpacePilotKeys[] = { |
| 305 |
V3DK_INVALID, V3DK_1, V3DK_2, V3DK_3, V3DK_4, V3DK_5, V3DK_6, |
| 306 |
V3DK_TOP, V3DK_LEFT, V3DK_RIGHT, V3DK_FRONT, V3DK_ESC, V3DK_ALT, V3DK_SHIFT, |
| 307 |
V3DK_CTRL, V3DK_FIT, V3DK_MENU, V3DK_PLUS, V3DK_MINUS, V3DK_DOMINANT, V3DK_ROTATE |
| 308 |
}; |
| 309 |
|
| 310 |
|
| 311 |
static const struct tag_VirtualKeys _3dmouseVirtualKeys[] = { |
| 312 |
eSpacePilot, |
| 313 |
sizeof(SpacePilotKeys) / sizeof(SpacePilotKeys[0]), |
| 314 |
const_cast<e3dmouse_virtual_key*>(SpacePilotKeys), |
| 315 |
eSpaceExplorer, |
| 316 |
sizeof(SpaceExplorerKeys) / sizeof(SpaceExplorerKeys[0]), |
| 317 |
const_cast<e3dmouse_virtual_key*>(SpaceExplorerKeys), |
| 318 |
eSpaceMouseWireless, |
| 319 |
sizeof(SpaceMouseWirelessKeys) / sizeof(SpaceMouseWirelessKeys[0]), |
| 320 |
const_cast<e3dmouse_virtual_key*>(SpaceMouseWirelessKeys), |
| 321 |
eSpaceMouseWirelessReceiver, |
| 322 |
sizeof(SpaceMouseWirelessReceiverKeys) / sizeof(SpaceMouseWirelessReceiverKeys[0]), |
| 323 |
const_cast<e3dmouse_virtual_key*>(SpaceMouseWirelessReceiverKeys), |
| 324 |
eSpaceMousePROWireless, |
| 325 |
sizeof(SpaceMouseWirelessKeys) / sizeof(SpaceMouseWirelessKeys[0]), |
| 326 |
const_cast<e3dmouse_virtual_key*>(SpaceMouseWirelessKeys), |
| 327 |
eSpaceMousePROWirelessReceiver, |
| 328 |
sizeof(SpaceMouseWirelessReceiverKeys) / sizeof(SpaceMouseWirelessReceiverKeys[0]), |
| 329 |
const_cast<e3dmouse_virtual_key*>(SpaceMouseWirelessReceiverKeys) |
| 330 |
}; |
| 331 |
|
| 332 |
Gui::GuiNativeEvent::GuiNativeEvent(Gui::GUIApplicationNativeEventAware* app) |
| 333 |
: GuiAbstractNativeEvent(app) |
| 334 |
{} |
| 335 |
|
| 336 |
Gui::GuiNativeEvent::~GuiNativeEvent() |
| 337 |
{ |
| 338 |
if (gMouseInput == this) { |
| 339 |
gMouseInput = 0; |
| 340 |
Base::Console().log("3Dconnexion device detached.\n"); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
void Gui::GuiNativeEvent::initSpaceball(QMainWindow* mainWindow) |
| 345 |
{ |
| 346 |
mainApp->setSpaceballPresent(Is3dmouseAttached()); |
| 347 |
|
| 348 |
if (mainApp->isSpaceballPresent()) { |
| 349 |
fLast3dmouseInputTime = 0; |
| 350 |
|
| 351 |
if (InitializeRawInput((HWND)mainWindow->winId())) { |
| 352 |
gMouseInput = this; |
| 353 |
qApp->installNativeEventFilter( |
| 354 |
new Gui::RawInputEventFilter(Gui::GuiNativeEvent::RawInputEventFilter) |
| 355 |
); |
| 356 |
Base::Console().log("3Dconnexion device initialized.\n"); |
| 357 |
} |
| 358 |
else { |
| 359 |
Base::Console().log("3Dconnexion device is attached, but not initialized.\n"); |
| 360 |
} |
| 361 |
} |
| 362 |
else { |
| 363 |
Base::Console().log("3Dconnexion device not attached.\n"); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
// Methods for windows events |
| 368 |
|
| 369 |
/*! |
| 370 |
Converts a hid device keycode (button identifier) of a pre-2009 3Dconnexion USB device to the |
| 371 |
standard 3d mouse virtual key definition. |
| 372 |
|
| 373 |
\a pid USB Product ID (PID) of 3D mouse device |
| 374 |
\a hidKeyCode Hid keycode as retrieved from a Raw Input packet |
| 375 |
|
| 376 |
\return The standard 3d mouse virtual key (button identifier) or zero if an error occurs. |
| 377 |
|
| 378 |
Converts a hid device keycode (button identifier) of a pre-2009 3Dconnexion USB device |
| 379 |
to the standard 3d mouse virtual key definition. |
| 380 |
*/ |
| 381 |
|
| 382 |
unsigned short HidToVirtualKey(unsigned long pid, unsigned short hidKeyCode) |
| 383 |
{ |
| 384 |
unsigned short virtualkey = hidKeyCode; |
| 385 |
for (size_t i = 0; i < sizeof(_3dmouseVirtualKeys) / sizeof(_3dmouseVirtualKeys[0]); ++i) { |
| 386 |
if (pid == _3dmouseVirtualKeys[i].pid) { |
| 387 |
if (hidKeyCode < _3dmouseVirtualKeys[i].nKeys) { |
| 388 |
virtualkey = _3dmouseVirtualKeys[i].vkeys[hidKeyCode]; |
| 389 |
} |
| 390 |
else { |
| 391 |
virtualkey = V3DK_INVALID; |
| 392 |
} |
| 393 |
break; |
| 394 |
} |
| 395 |
} |
| 396 |
// Remaining devices are unchanged |
| 397 |
return virtualkey; |
| 398 |
} |
| 399 |
|
| 400 |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 401 |
bool Gui::GuiNativeEvent::RawInputEventFilter(void* msg, long* result) |
| 402 |
#else |
| 403 |
bool Gui::GuiNativeEvent::RawInputEventFilter(void* msg, qintptr* result) |
| 404 |
#endif |
| 405 |
{ |
| 406 |
if (gMouseInput == 0) { |
| 407 |
return false; |
| 408 |
} |
| 409 |
|
| 410 |
MSG* message = (MSG*)(msg); |
| 411 |
|
| 412 |
if (message->message == WM_INPUT) { |
| 413 |
HRAWINPUT hRawInput = reinterpret_cast<HRAWINPUT>(message->lParam); |
| 414 |
gMouseInput->OnRawInput(RIM_INPUT, hRawInput); |
| 415 |
if (result != 0) { |
| 416 |
result = 0; |
| 417 |
} |
| 418 |
return true; |
| 419 |
} |
| 420 |
|
| 421 |
return false; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
/*! |
| 426 |
Access the mouse parameters structure |
| 427 |
*/ |
| 428 |
I3dMouseParam& Gui::GuiNativeEvent::MouseParams() |
| 429 |
{ |
| 430 |
return f3dMouseParams; |
| 431 |
} |
| 432 |
|
| 433 |
/*! |
| 434 |
Access the mouse parameters structure |
| 435 |
*/ |
| 436 |
const I3dMouseParam& Gui::GuiNativeEvent::MouseParams() const |
| 437 |
{ |
| 438 |
return f3dMouseParams; |
| 439 |
} |
| 440 |
|
| 441 |
/*! |
| 442 |
Called with the processed motion data when a 3D mouse event is received |
| 443 |
|
| 444 |
The default implementation emits a Move3d signal with the motion data |
| 445 |
*/ |
| 446 |
void Gui::GuiNativeEvent::Move3d(HANDLE device, std::vector<float>& motionData) |
| 447 |
{ |
| 448 |
Q_UNUSED(device); |
| 449 |
|
| 450 |
motionDataArray[0] = -ceil(motionData[0]); |
| 451 |
motionDataArray[1] = ceil(motionData[1]); |
| 452 |
motionDataArray[2] = ceil(motionData[2]); |
| 453 |
motionDataArray[3] = -ceil(motionData[3]); |
| 454 |
motionDataArray[4] = ceil(motionData[4]); |
| 455 |
motionDataArray[5] = ceil(motionData[5]); |
| 456 |
|
| 457 |
mainApp->postMotionEvent(motionDataArray); |
| 458 |
} |
| 459 |
|
| 460 |
/*! |
| 461 |
Called when a 3D mouse key is pressed |
| 462 |
|
| 463 |
The default implementation emits a On3dmouseKeyDown signal with the key code. |
| 464 |
*/ |
| 465 |
void Gui::GuiNativeEvent::On3dmouseKeyDown(HANDLE device, int virtualKeyCode) |
| 466 |
{ |
| 467 |
Q_UNUSED(device); |
| 468 |
|
| 469 |
mainApp->postButtonEvent(virtualKeyCode - 1, 1); |
| 470 |
} |
| 471 |
|
| 472 |
/*! |
| 473 |
Called when a 3D mouse key is released |
| 474 |
|
| 475 |
The default implementation emits a On3dmouseKeyUp signal with the key code. |
| 476 |
*/ |
| 477 |
void Gui::GuiNativeEvent::On3dmouseKeyUp(HANDLE device, int virtualKeyCode) |
| 478 |
{ |
| 479 |
Q_UNUSED(device); |
| 480 |
|
| 481 |
mainApp->postButtonEvent(virtualKeyCode - 1, 0); |
| 482 |
} |
| 483 |
|
| 484 |
/*! |
| 485 |
Get an initialized array of PRAWINPUTDEVICE for the 3D devices |
| 486 |
|
| 487 |
pNumDevices returns the number of devices to register. Currently this is always 1. |
| 488 |
*/ |
| 489 |
static PRAWINPUTDEVICE GetDevicesToRegister(unsigned int* pNumDevices) |
| 490 |
{ |
| 491 |
// Array of raw input devices to register |
| 492 |
static RAWINPUTDEVICE sRawInputDevices[] = { |
| 493 |
{0x01, 0x08, 0x00, 0x00} |
| 494 |
// Usage Page = 0x01 Generic Desktop Page, Usage Id= 0x08 Multi-axis Controller |
| 495 |
, |
| 496 |
{0x01, 0x05, 0x00, 0x00} // game pad |
| 497 |
, |
| 498 |
{0x01, 0x04, 0x00, 0x00} // joystick |
| 499 |
}; |
| 500 |
|
| 501 |
if (pNumDevices) { |
| 502 |
*pNumDevices = sizeof(sRawInputDevices) / sizeof(sRawInputDevices[0]); |
| 503 |
} |
| 504 |
|
| 505 |
return sRawInputDevices; |
| 506 |
} |
| 507 |
|
| 508 |
/*! |
| 509 |
Detect the 3D mouse |
| 510 |
*/ |
| 511 |
bool Gui::GuiNativeEvent::Is3dmouseAttached() |
| 512 |
{ |
| 513 |
unsigned int numDevicesOfInterest = 0; |
| 514 |
PRAWINPUTDEVICE devicesToRegister = GetDevicesToRegister(&numDevicesOfInterest); |
| 515 |
|
| 516 |
unsigned int nDevices = 0; |
| 517 |
|
| 518 |
if (::GetRawInputDeviceList(NULL, &nDevices, sizeof(RAWINPUTDEVICELIST)) != 0) { |
| 519 |
return false; |
| 520 |
} |
| 521 |
|
| 522 |
if (nDevices == 0) { |
| 523 |
return false; |
| 524 |
} |
| 525 |
|
| 526 |
std::vector<RAWINPUTDEVICELIST> rawInputDeviceList(nDevices); |
| 527 |
if (::GetRawInputDeviceList(&rawInputDeviceList[0], &nDevices, sizeof(RAWINPUTDEVICELIST)) |
| 528 |
== static_cast<unsigned int>(-1)) { |
| 529 |
return false; |
| 530 |
} |
| 531 |
|
| 532 |
for (unsigned int i = 0; i < nDevices; ++i) { |
| 533 |
RID_DEVICE_INFO rdi = {sizeof(rdi)}; |
| 534 |
unsigned int cbSize = sizeof(rdi); |
| 535 |
|
| 536 |
if (GetRawInputDeviceInfo(rawInputDeviceList[i].hDevice, RIDI_DEVICEINFO, &rdi, &cbSize) > 0) { |
| 537 |
// skip non HID and non logitec (3DConnexion) devices |
| 538 |
if (!(rdi.dwType == RIM_TYPEHID |
| 539 |
&& (rdi.hid.dwVendorId == LOGITECH_VENDOR_ID |
| 540 |
|| rdi.hid.dwVendorId == CONNEXION_VENDOR_ID))) { |
| 541 |
continue; |
| 542 |
} |
| 543 |
|
| 544 |
// check if devices matches Multi-axis Controller |
| 545 |
for (unsigned int j = 0; j < numDevicesOfInterest; ++j) { |
| 546 |
if (devicesToRegister[j].usUsage == rdi.hid.usUsage |
| 547 |
&& devicesToRegister[j].usUsagePage == rdi.hid.usUsagePage) { |
| 548 |
return true; |
| 549 |
} |
| 550 |
} |
| 551 |
} |
| 552 |
} |
| 553 |
return false; |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
/*! |
| 558 |
Initialize the window to receive raw-input messages |
| 559 |
|
| 560 |
This needs to be called initially so that Windows will send the messages from the 3D mouse to |
| 561 |
the window. |
| 562 |
*/ |
| 563 |
bool Gui::GuiNativeEvent::InitializeRawInput(HWND hwndTarget) |
| 564 |
{ |
| 565 |
fWindow = hwndTarget; |
| 566 |
|
| 567 |
// Simply fail if there is no window |
| 568 |
if (!hwndTarget) { |
| 569 |
return false; |
| 570 |
} |
| 571 |
|
| 572 |
unsigned int numDevices = 0; |
| 573 |
PRAWINPUTDEVICE devicesToRegister = GetDevicesToRegister(&numDevices); |
| 574 |
|
| 575 |
if (numDevices == 0) { |
| 576 |
return false; |
| 577 |
} |
| 578 |
|
| 579 |
// Get OS version. |
| 580 |
OSVERSIONINFO osvi = {sizeof(OSVERSIONINFO), 0}; |
| 581 |
::GetVersionEx(&osvi); |
| 582 |
|
| 583 |
unsigned int cbSize = sizeof(devicesToRegister[0]); |
| 584 |
for (size_t i = 0; i < numDevices; i++) { |
| 585 |
// Set the target window to use |
| 586 |
// devicesToRegister[i].hwndTarget = hwndTarget; |
| 587 |
|
| 588 |
// If Vista or newer, enable receiving the WM_INPUT_DEVICE_CHANGE message. |
| 589 |
if (osvi.dwMajorVersion >= 6) { |
| 590 |
devicesToRegister[i].dwFlags |= RIDEV_DEVNOTIFY; |
| 591 |
} |
| 592 |
} |
| 593 |
return (::RegisterRawInputDevices(devicesToRegister, numDevices, cbSize) != FALSE); |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
/*! |
| 598 |
Get the raw input data from Windows |
| 599 |
|
| 600 |
Includes workaround for incorrect alignment of the RAWINPUT structure on x64 os |
| 601 |
when running as Wow64 (copied directly from 3DConnexion code) |
| 602 |
*/ |
| 603 |
|
| 604 |
UINT Gui::GuiNativeEvent::GetRawInputBuffer(PRAWINPUT pData, PUINT pcbSize, UINT cbSizeHeader) |
| 605 |
{ |
| 606 |
#ifdef _WIN64 |
| 607 |
return ::GetRawInputBuffer(pData, pcbSize, cbSizeHeader); |
| 608 |
#else |
| 609 |
BOOL bIsWow64 = FALSE; |
| 610 |
// 0002287: Entry Point Not Found IsWow64Process for Windows 2000 |
| 611 |
//::IsWow64Process(GetCurrentProcess(), &bIsWow64); |
| 612 |
typedef BOOL(WINAPI * LPFN_ISWOW64PROCESS)(HANDLE, PBOOL); |
| 613 |
|
| 614 |
LPFN_ISWOW64PROCESS fnIsWow64Process |
| 615 |
= (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process"); |
| 616 |
|
| 617 |
if (NULL != fnIsWow64Process) { |
| 618 |
fnIsWow64Process(GetCurrentProcess(), &bIsWow64); |
| 619 |
} |
| 620 |
if (!bIsWow64 || pData == NULL) { |
| 621 |
return ::GetRawInputBuffer(pData, pcbSize, cbSizeHeader); |
| 622 |
} |
| 623 |
else { |
| 624 |
HWND hwndTarget = fWindow; // fParent->winId(); |
| 625 |
|
| 626 |
size_t cbDataSize = 0; |
| 627 |
UINT nCount = 0; |
| 628 |
PRAWINPUT pri = pData; |
| 629 |
|
| 630 |
MSG msg; |
| 631 |
while (PeekMessage(&msg, hwndTarget, WM_INPUT, WM_INPUT, PM_NOREMOVE)) { |
| 632 |
HRAWINPUT hRawInput = reinterpret_cast<HRAWINPUT>(msg.lParam); |
| 633 |
size_t cbSize = *pcbSize - cbDataSize; |
| 634 |
if (::GetRawInputData(hRawInput, RID_INPUT, pri, &cbSize, cbSizeHeader) |
| 635 |
== static_cast<UINT>(-1)) { |
| 636 |
if (nCount == 0) { |
| 637 |
return static_cast<UINT>(-1); |
| 638 |
} |
| 639 |
else { |
| 640 |
break; |
| 641 |
} |
| 642 |
} |
| 643 |
++nCount; |
| 644 |
|
| 645 |
// Remove the message for the data just read |
| 646 |
PeekMessage(&msg, hwndTarget, WM_INPUT, WM_INPUT, PM_REMOVE); |
| 647 |
|
| 648 |
pri = NEXTRAWINPUTBLOCK(pri); |
| 649 |
cbDataSize = reinterpret_cast<ULONG_PTR>(pri) - reinterpret_cast<ULONG_PTR>(pData); |
| 650 |
if (cbDataSize >= *pcbSize) { |
| 651 |
cbDataSize = *pcbSize; |
| 652 |
break; |
| 653 |
} |
| 654 |
} |
| 655 |
return nCount; |
| 656 |
} |
| 657 |
#endif |
| 658 |
} |
| 659 |
|
| 660 |
/*! |
| 661 |
Process the raw input device data |
| 662 |
|
| 663 |
On3dmouseInput() does all the preprocessing of the rawinput device data before |
| 664 |
finally calling the Move3d method. |
| 665 |
*/ |
| 666 |
|
| 667 |
void Gui::GuiNativeEvent::On3dmouseInput() |
| 668 |
{ |
| 669 |
// Don't do any data processing in background |
| 670 |
bool bIsForeground = (::GetActiveWindow() != NULL); |
| 671 |
if (!bIsForeground) { |
| 672 |
// set all cached data to zero so that a zero event is seen and the cached data deleted |
| 673 |
for (std::map<HANDLE, TInputData>::iterator it = fDevice2Data.begin(); |
| 674 |
it != fDevice2Data.end(); |
| 675 |
it++) { |
| 676 |
it->second.fAxes.assign(6, .0); |
| 677 |
it->second.fIsDirty = true; |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
DWORD dwNow = ::GetTickCount(); // Current time; |
| 682 |
DWORD dwElapsedTime; // Elapsed time since we were last here |
| 683 |
|
| 684 |
if (0 == fLast3dmouseInputTime) { |
| 685 |
dwElapsedTime = 10; // System timer resolution |
| 686 |
} |
| 687 |
else { |
| 688 |
dwElapsedTime = dwNow - fLast3dmouseInputTime; |
| 689 |
if (fLast3dmouseInputTime > dwNow) { |
| 690 |
dwElapsedTime = ~dwElapsedTime + 1; |
| 691 |
} |
| 692 |
if (dwElapsedTime < 1) { |
| 693 |
dwElapsedTime = 1; |
| 694 |
} |
| 695 |
else if (dwElapsedTime > 500) { |
| 696 |
// Check for wild numbers because the device was removed while sending data |
| 697 |
dwElapsedTime = 10; |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
#if _TRACE_3DINPUT_PERIOD |
| 702 |
qDebug("On3DmouseInput() period is %dms\n", dwElapsedTime); |
| 703 |
#endif |
| 704 |
|
| 705 |
float mouseData2Rotation; |
| 706 |
// v = w * r, we don't know r yet so lets assume r=1.) |
| 707 |
float mouseData2PanZoom; |
| 708 |
|
| 709 |
// Grab the I3dmouseParam interface |
| 710 |
I3dMouseParam& i3dmouseParam = f3dMouseParams; |
| 711 |
// Take a look at the users preferred speed setting and adjust the sensitivity accordingly |
| 712 |
I3dMouseSensor::ESpeed speedSetting = i3dmouseParam.GetSpeed(); |
| 713 |
// See "Programming for the 3D Mouse", Section 5.1.3 |
| 714 |
float speed |
| 715 |
= (speedSetting == I3dMouseSensor::kLowSpeed ? 0.25f |
| 716 |
: speedSetting == I3dMouseSensor::kHighSpeed ? 4.f |
| 717 |
: 1.f); |
| 718 |
|
| 719 |
// Multiplying by the following will convert the 3d mouse data to real world units |
| 720 |
mouseData2PanZoom = speed; |
| 721 |
mouseData2Rotation = speed; |
| 722 |
|
| 723 |
std::map<HANDLE, TInputData>::iterator iterator = fDevice2Data.begin(); |
| 724 |
while (iterator != fDevice2Data.end()) { |
| 725 |
|
| 726 |
// If we have not received data for a while send a zero event |
| 727 |
if ((--(iterator->second.fTimeToLive)) == 0) { |
| 728 |
iterator->second.fAxes.assign(6, .0); |
| 729 |
} |
| 730 |
else if (/*!t_bPoll3dmouse &&*/ !iterator->second.fIsDirty) { |
| 731 |
// If we are not polling then only handle the data that was actually received |
| 732 |
++iterator; |
| 733 |
continue; |
| 734 |
} |
| 735 |
iterator->second.fIsDirty = false; |
| 736 |
|
| 737 |
// get a copy of the device |
| 738 |
HANDLE hdevice = iterator->first; |
| 739 |
|
| 740 |
// get a copy of the motion vectors and apply the user filters |
| 741 |
std::vector<float> motionData = iterator->second.fAxes; |
| 742 |
|
| 743 |
// apply the user filters |
| 744 |
|
| 745 |
// Pan Zoom filter |
| 746 |
// See "Programming for the 3D Mouse", Section 5.1.2 |
| 747 |
if (!i3dmouseParam.IsPanZoom()) { |
| 748 |
// Pan zoom is switched off so set the translation vector values to zero |
| 749 |
motionData[0] = motionData[1] = motionData[2] = 0.; |
| 750 |
} |
| 751 |
|
| 752 |
// Rotate filter |
| 753 |
// See "Programming for the 3D Mouse", Section 5.1.1 |
| 754 |
if (!i3dmouseParam.IsRotate()) { |
| 755 |
// Rotate is switched off so set the rotation vector values to zero |
| 756 |
motionData[3] = motionData[4] = motionData[5] = 0.; |
| 757 |
} |
| 758 |
|
| 759 |
// convert the translation vector into physical data |
| 760 |
for (int axis = 0; axis < 3; axis++) { |
| 761 |
motionData[axis] *= mouseData2PanZoom; |
| 762 |
} |
| 763 |
// convert the directed Rotate vector into physical data |
| 764 |
// See "Programming for the 3D Mouse", Section 7.2.2 |
| 765 |
for (int axis = 3; axis < 6; axis++) { |
| 766 |
motionData[axis] *= mouseData2Rotation; |
| 767 |
} |
| 768 |
|
| 769 |
// Now that the data has had the filters and sensitivity settings applied |
| 770 |
// calculate the displacements since the last view update |
| 771 |
for (int axis = 0; axis < 6; axis++) { |
| 772 |
motionData[axis] *= dwElapsedTime; |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
// Now a bit of book keeping before passing on the data |
| 777 |
if (iterator->second.IsZero()) { |
| 778 |
iterator = fDevice2Data.erase(iterator); |
| 779 |
} |
| 780 |
else { |
| 781 |
++iterator; |
| 782 |
} |
| 783 |
|
| 784 |
// Work out which will be the next device |
| 785 |
HANDLE hNextDevice = 0; |
| 786 |
if (iterator != fDevice2Data.end()) { |
| 787 |
hNextDevice = iterator->first; |
| 788 |
} |
| 789 |
|
| 790 |
// Pass the 3dmouse input to the view controller |
| 791 |
Move3d(hdevice, motionData); |
| 792 |
|
| 793 |
// Because we don't know what happened in the previous call, the cache might have |
| 794 |
// changed so reload the iterator |
| 795 |
iterator = fDevice2Data.find(hNextDevice); |
| 796 |
} |
| 797 |
|
| 798 |
if (!fDevice2Data.empty()) { |
| 799 |
fLast3dmouseInputTime = dwNow; |
| 800 |
} |
| 801 |
else { |
| 802 |
fLast3dmouseInputTime = 0; |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
/*! |
| 807 |
Called when new raw input data is available |
| 808 |
*/ |
| 809 |
void Gui::GuiNativeEvent::OnRawInput(UINT nInputCode, HRAWINPUT hRawInput) |
| 810 |
{ |
| 811 |
const size_t cbSizeOfBuffer = 1024; |
| 812 |
BYTE pBuffer[cbSizeOfBuffer]; |
| 813 |
|
| 814 |
PRAWINPUT pRawInput = reinterpret_cast<PRAWINPUT>(pBuffer); |
| 815 |
UINT cbSize = cbSizeOfBuffer; |
| 816 |
|
| 817 |
if (::GetRawInputData(hRawInput, RID_INPUT, pRawInput, &cbSize, sizeof(RAWINPUTHEADER)) |
| 818 |
== static_cast<UINT>(-1)) { |
| 819 |
return; |
| 820 |
} |
| 821 |
|
| 822 |
bool b3dmouseInput = TranslateRawInputData(nInputCode, pRawInput); |
| 823 |
::DefRawInputProc(&pRawInput, 1, sizeof(RAWINPUTHEADER)); |
| 824 |
|
| 825 |
// Check for any buffered messages |
| 826 |
cbSize = cbSizeOfBuffer; |
| 827 |
UINT nCount = this->GetRawInputBuffer(pRawInput, &cbSize, sizeof(RAWINPUTHEADER)); |
| 828 |
if (nCount == (UINT)-1) { |
| 829 |
qDebug("GetRawInputBuffer returned error %d\n", GetLastError()); |
| 830 |
} |
| 831 |
|
| 832 |
while (nCount > 0 && nCount != static_cast<UINT>(-1)) { |
| 833 |
PRAWINPUT pri = pRawInput; |
| 834 |
UINT nInput; |
| 835 |
for (nInput = 0; nInput < nCount; ++nInput) { |
| 836 |
b3dmouseInput |= TranslateRawInputData(nInputCode, pri); |
| 837 |
// clean the buffer |
| 838 |
::DefRawInputProc(&pri, 1, sizeof(RAWINPUTHEADER)); |
| 839 |
|
| 840 |
pri = NEXTRAWINPUTBLOCK(pri); |
| 841 |
} |
| 842 |
cbSize = cbSizeOfBuffer; |
| 843 |
nCount = this->GetRawInputBuffer(pRawInput, &cbSize, sizeof(RAWINPUTHEADER)); |
| 844 |
} |
| 845 |
|
| 846 |
// If we have mouse input data for the app then tell the app about it |
| 847 |
if (b3dmouseInput) { |
| 848 |
On3dmouseInput(); |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
bool Gui::GuiNativeEvent::TranslateRawInputData(UINT nInputCode, PRAWINPUT pRawInput) |
| 854 |
{ |
| 855 |
#if _TRACE_RI_TYPE |
| 856 |
qDebug("Rawinput.header.dwType=0x%x\n", pRawInput->header.dwType); |
| 857 |
#endif |
| 858 |
// We are not interested in keyboard or mouse data received via raw input |
| 859 |
if (pRawInput->header.dwType != RIM_TYPEHID) { |
| 860 |
return false; |
| 861 |
} |
| 862 |
|
| 863 |
#if _TRACE_RIDI_DEVICENAME |
| 864 |
UINT dwSize = 0; |
| 865 |
if (::GetRawInputDeviceInfo(pRawInput->header.hDevice, RIDI_DEVICENAME, NULL, &dwSize) == 0) { |
| 866 |
std::vector<wchar_t> szDeviceName(dwSize + 1); |
| 867 |
if (::GetRawInputDeviceInfo(pRawInput->header.hDevice, RIDI_DEVICENAME, &szDeviceName[0], &dwSize) |
| 868 |
> 0) { |
| 869 |
qDebug( |
| 870 |
"Device Name = %s\nDevice handle = 0x%x\n", |
| 871 |
&szDeviceName[0], |
| 872 |
pRawInput->header.hDevice |
| 873 |
); |
| 874 |
} |
| 875 |
} |
| 876 |
#endif |
| 877 |
|
| 878 |
RID_DEVICE_INFO sRidDeviceInfo; |
| 879 |
sRidDeviceInfo.cbSize = sizeof(RID_DEVICE_INFO); |
| 880 |
UINT cbSize = sizeof(RID_DEVICE_INFO); |
| 881 |
|
| 882 |
if (::GetRawInputDeviceInfo(pRawInput->header.hDevice, RIDI_DEVICEINFO, &sRidDeviceInfo, &cbSize) |
| 883 |
== cbSize) { |
| 884 |
#if _TRACE_RIDI_DEVICEINFO |
| 885 |
switch (sRidDeviceInfo.dwType) { |
| 886 |
case RIM_TYPEMOUSE: |
| 887 |
qDebug("\tsRidDeviceInfo.dwType=RIM_TYPEMOUSE\n"); |
| 888 |
break; |
| 889 |
case RIM_TYPEKEYBOARD: |
| 890 |
qDebug("\tsRidDeviceInfo.dwType=RIM_TYPEKEYBOARD\n"); |
| 891 |
break; |
| 892 |
case RIM_TYPEHID: |
| 893 |
qDebug("\tsRidDeviceInfo.dwType=RIM_TYPEHID\n"); |
| 894 |
qDebug( |
| 895 |
"\tVendor=0x%x\n\tProduct=0x%x\n\tUsagePage=0x%x\n\tUsage=0x%x\n", |
| 896 |
sRidDeviceInfo.hid.dwVendorId, |
| 897 |
sRidDeviceInfo.hid.dwProductId, |
| 898 |
sRidDeviceInfo.hid.usUsagePage, |
| 899 |
sRidDeviceInfo.hid.usUsage |
| 900 |
); |
| 901 |
break; |
| 902 |
} |
| 903 |
#endif |
| 904 |
|
| 905 |
if (sRidDeviceInfo.hid.dwVendorId == LOGITECH_VENDOR_ID |
| 906 |
|| sRidDeviceInfo.hid.dwVendorId == CONNEXION_VENDOR_ID) { |
| 907 |
switch (sRidDeviceInfo.hid.dwProductId) { |
| 908 |
case eSpaceMousePlusXT: |
| 909 |
if (pRawInput->data.hid.bRawData[0] != 0x00) { |
| 910 |
return TranslateSpaceMouseNewGeneric( |
| 911 |
nInputCode, |
| 912 |
pRawInput, |
| 913 |
sRidDeviceInfo.hid.dwProductId |
| 914 |
); |
| 915 |
} |
| 916 |
else { |
| 917 |
return TranslateSpaceMouseOldGeneric( |
| 918 |
nInputCode, |
| 919 |
pRawInput, |
| 920 |
sRidDeviceInfo.hid.dwProductId |
| 921 |
); |
| 922 |
} |
| 923 |
case eSpaceMouseEnterprise: |
| 924 |
return TranslateSpaceMouseEnterprise( |
| 925 |
nInputCode, |
| 926 |
pRawInput, |
| 927 |
sRidDeviceInfo.hid.dwProductId |
| 928 |
); |
| 929 |
case eSpacePilot: |
| 930 |
case eSpaceNavigator: |
| 931 |
case eSpaceExplorer: |
| 932 |
case eSpaceNavigatorForNotebooks: |
| 933 |
case eSpacePilotPRO: |
| 934 |
case eSpaceMouseWireless: |
| 935 |
case eSpaceMousePROWireless: |
| 936 |
default: |
| 937 |
return TranslateSpaceMouseNewGeneric( |
| 938 |
nInputCode, |
| 939 |
pRawInput, |
| 940 |
sRidDeviceInfo.hid.dwProductId |
| 941 |
); |
| 942 |
} |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
return false; |
| 947 |
} |
| 948 |
|
| 949 |
bool Gui::GuiNativeEvent::TranslateSpaceMouseNewGeneric( |
| 950 |
UINT nInputCode, |
| 951 |
PRAWINPUT pRawInput, |
| 952 |
DWORD dwProductId |
| 953 |
) |
| 954 |
{ |
| 955 |
bool bIsForeground = (nInputCode == RIM_INPUT); |
| 956 |
|
| 957 |
if (pRawInput->data.hid.bRawData[0] == 0x01) { // Translation vector |
| 958 |
|
| 959 |
TInputData& deviceData = fDevice2Data[pRawInput->header.hDevice]; |
| 960 |
deviceData.fTimeToLive = kTimeToLive; |
| 961 |
if (bIsForeground) { |
| 962 |
short* pnRawData = reinterpret_cast<short*>(&pRawInput->data.hid.bRawData[1]); |
| 963 |
// Cache the pan zoom data |
| 964 |
deviceData.fAxes[0] = static_cast<float>(pnRawData[0]); |
| 965 |
deviceData.fAxes[1] = static_cast<float>(pnRawData[1]); |
| 966 |
deviceData.fAxes[2] = static_cast<float>(pnRawData[2]); |
| 967 |
|
| 968 |
#if _TRACE_RI_RAWDATA |
| 969 |
qDebug("Pan/Zoom RI Data =\t0x%x,\t0x%x,\t0x%x\n", pnRawData[0], pnRawData[1], pnRawData[2]); |
| 970 |
#endif |
| 971 |
if (pRawInput->data.hid.dwSizeHid >= 13) { // Highspeed package |
| 972 |
// Cache the rotation data |
| 973 |
deviceData.fAxes[3] = static_cast<float>(pnRawData[3]); |
| 974 |
deviceData.fAxes[4] = static_cast<float>(pnRawData[4]); |
| 975 |
deviceData.fAxes[5] = static_cast<float>(pnRawData[5]); |
| 976 |
deviceData.fIsDirty = true; |
| 977 |
#if _TRACE_RI_RAWDATA |
| 978 |
qDebug( |
| 979 |
"Rotation RI Data =\t0x%x,\t0x%x,\t0x%x\n", |
| 980 |
pnRawData[3], |
| 981 |
pnRawData[4], |
| 982 |
pnRawData[5] |
| 983 |
); |
| 984 |
#endif |
| 985 |
return true; |
| 986 |
} |
| 987 |
} |
| 988 |
else { // Zero out the data if the app is not in foreground |
| 989 |
deviceData.fAxes.assign(6, 0.f); |
| 990 |
} |
| 991 |
} |
| 992 |
else if (pRawInput->data.hid.bRawData[0] == 0x02) { // Rotation vector |
| 993 |
// If we are not in foreground do nothing |
| 994 |
// The rotation vector was zeroed out with the translation vector in the previous message |
| 995 |
if (bIsForeground) { |
| 996 |
TInputData& deviceData = fDevice2Data[pRawInput->header.hDevice]; |
| 997 |
deviceData.fTimeToLive = kTimeToLive; |
| 998 |
|
| 999 |
short* pnRawData = reinterpret_cast<short*>(&pRawInput->data.hid.bRawData[1]); |
| 1000 |
// Cache the rotation data |
| 1001 |
deviceData.fAxes[3] = static_cast<float>(pnRawData[0]); |
| 1002 |
deviceData.fAxes[4] = static_cast<float>(pnRawData[1]); |
| 1003 |
deviceData.fAxes[5] = static_cast<float>(pnRawData[2]); |
| 1004 |
deviceData.fIsDirty = true; |
| 1005 |
|
| 1006 |
#if _TRACE_RI_RAWDATA |
| 1007 |
qDebug("Rotation RI Data =\t0x%x,\t0x%x,\t0x%x\n", pnRawData[0], pnRawData[1], pnRawData[2]); |
| 1008 |
#endif |
| 1009 |
return true; |
| 1010 |
} |
| 1011 |
} |
| 1012 |
else if (pRawInput->data.hid.bRawData[0] == 0x03) { // Keystate change |
| 1013 |
// this is a package that contains 3d mouse keystate information |
| 1014 |
// bit0=key1, bit=key2 etc. |
| 1015 |
|
| 1016 |
|
| 1017 |
unsigned long dwKeystate = *reinterpret_cast<unsigned long*>(&pRawInput->data.hid.bRawData[1]); |
| 1018 |
#if _TRACE_RI_RAWDATA |
| 1019 |
qDebug("pRawInput->data.hid.bRawData[0] = 0x%x", pRawInput->data.hid.bRawData[0]); |
| 1020 |
qDebug("ButtonData =0x%x\n", dwKeystate); |
| 1021 |
#endif |
| 1022 |
// Log the keystate changes |
| 1023 |
unsigned long dwOldKeystate = fDevice2Keystate[pRawInput->header.hDevice]; |
| 1024 |
if (dwKeystate != 0) { |
| 1025 |
fDevice2Keystate[pRawInput->header.hDevice] = dwKeystate; |
| 1026 |
} |
| 1027 |
else { |
| 1028 |
fDevice2Keystate.erase(pRawInput->header.hDevice); |
| 1029 |
} |
| 1030 |
|
| 1031 |
// Only call the keystate change handlers if the app is in foreground |
| 1032 |
if (bIsForeground) { |
| 1033 |
unsigned long dwChange = dwKeystate ^ dwOldKeystate; |
| 1034 |
|
| 1035 |
for (int nKeycode = 1; nKeycode < 33; nKeycode++) { |
| 1036 |
if (dwChange & 0x01) { |
| 1037 |
int nVirtualKeyCode = HidToVirtualKey(dwProductId, nKeycode); |
| 1038 |
if (nVirtualKeyCode) { |
| 1039 |
if (dwKeystate & 0x01) { |
| 1040 |
On3dmouseKeyDown(pRawInput->header.hDevice, nVirtualKeyCode); |
| 1041 |
} |
| 1042 |
else { |
| 1043 |
On3dmouseKeyUp(pRawInput->header.hDevice, nVirtualKeyCode); |
| 1044 |
} |
| 1045 |
} |
| 1046 |
} |
| 1047 |
dwChange >>= 1; |
| 1048 |
dwKeystate >>= 1; |
| 1049 |
} |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
return false; |
| 1054 |
} |
| 1055 |
|
| 1056 |
bool Gui::GuiNativeEvent::TranslateSpaceMouseEnterprise( |
| 1057 |
UINT nInputCode, |
| 1058 |
PRAWINPUT pRawInput, |
| 1059 |
DWORD dwProductId |
| 1060 |
) |
| 1061 |
{ |
| 1062 |
bool bIsForeground = (nInputCode == RIM_INPUT); |
| 1063 |
|
| 1064 |
if (pRawInput->data.hid.bRawData[0] == 0x01) { // Translation vector |
| 1065 |
|
| 1066 |
TInputData& deviceData = fDevice2Data[pRawInput->header.hDevice]; |
| 1067 |
deviceData.fTimeToLive = kTimeToLive; |
| 1068 |
if (bIsForeground) { |
| 1069 |
short* pnRawData = reinterpret_cast<short*>(&pRawInput->data.hid.bRawData[1]); |
| 1070 |
// Cache the pan zoom data |
| 1071 |
deviceData.fAxes[0] = static_cast<float>(pnRawData[0]); |
| 1072 |
deviceData.fAxes[1] = static_cast<float>(pnRawData[1]); |
| 1073 |
deviceData.fAxes[2] = static_cast<float>(pnRawData[2]); |
| 1074 |
|
| 1075 |
#if _TRACE_RI_RAWDATA |
| 1076 |
qDebug("Pan/Zoom RI Data =\t0x%x,\t0x%x,\t0x%x\n", pnRawData[0], pnRawData[1], pnRawData[2]); |
| 1077 |
#endif |
| 1078 |
if (pRawInput->data.hid.dwSizeHid >= 13) { // Highspeed package |
| 1079 |
// Cache the rotation data |
| 1080 |
deviceData.fAxes[3] = static_cast<float>(pnRawData[3]); |
| 1081 |
deviceData.fAxes[4] = static_cast<float>(pnRawData[4]); |
| 1082 |
deviceData.fAxes[5] = static_cast<float>(pnRawData[5]); |
| 1083 |
deviceData.fIsDirty = true; |
| 1084 |
#if _TRACE_RI_RAWDATA |
| 1085 |
qDebug( |
| 1086 |
"Rotation RI Data =\t0x%x,\t0x%x,\t0x%x\n", |
| 1087 |
pnRawData[3], |
| 1088 |
pnRawData[4], |
| 1089 |
pnRawData[5] |
| 1090 |
); |
| 1091 |
#endif |
| 1092 |
return true; |
| 1093 |
} |
| 1094 |
} |
| 1095 |
else { // Zero out the data if the app is not in foreground |
| 1096 |
deviceData.fAxes.assign(6, 0.f); |
| 1097 |
} |
| 1098 |
} |
| 1099 |
else if (pRawInput->data.hid.bRawData[0] == 0x02) { // Rotation vector |
| 1100 |
// If we are not in foreground do nothing |
| 1101 |
// The rotation vector was zeroed out with the translation vector in the previous message |
| 1102 |
if (bIsForeground) { |
| 1103 |
TInputData& deviceData = fDevice2Data[pRawInput->header.hDevice]; |
| 1104 |
deviceData.fTimeToLive = kTimeToLive; |
| 1105 |
|
| 1106 |
short* pnRawData = reinterpret_cast<short*>(&pRawInput->data.hid.bRawData[1]); |
| 1107 |
// Cache the rotation data |
| 1108 |
deviceData.fAxes[3] = static_cast<float>(pnRawData[0]); |
| 1109 |
deviceData.fAxes[4] = static_cast<float>(pnRawData[1]); |
| 1110 |
deviceData.fAxes[5] = static_cast<float>(pnRawData[2]); |
| 1111 |
deviceData.fIsDirty = true; |
| 1112 |
|
| 1113 |
#if _TRACE_RI_RAWDATA |
| 1114 |
qDebug("Rotation RI Data =\t0x%x,\t0x%x,\t0x%x\n", pnRawData[0], pnRawData[1], pnRawData[2]); |
| 1115 |
#endif |
| 1116 |
return true; |
| 1117 |
} |
| 1118 |
} |
| 1119 |
else if ((pRawInput->data.hid.bRawData[0] == 0x1c) || (pRawInput->data.hid.bRawData[0] == 0x1d)) { |
| 1120 |
|
| 1121 |
#if _TRACE_RI_RAWDATA |
| 1122 |
qDebug("pRawInput->data.hid.bRawData[0] = 0x%x", pRawInput->data.hid.bRawData[0]); |
| 1123 |
qDebug( |
| 1124 |
"ButtonData = 0x%x\n", |
| 1125 |
*reinterpret_cast<unsigned long*>(&pRawInput->data.hid.bRawData[1]) |
| 1126 |
); |
| 1127 |
#endif |
| 1128 |
// calculate a KeyCode |
| 1129 |
unsigned long dwKeyCode = pRawInput->data.hid.bRawData[1] << 1; |
| 1130 |
dwKeyCode += (pRawInput->data.hid.bRawData[0] & 0x01); |
| 1131 |
|
| 1132 |
// Log the last key |
| 1133 |
unsigned long dwOldKeyCode = fDevice2Keystate[pRawInput->header.hDevice]; |
| 1134 |
if (dwKeyCode != 0) { |
| 1135 |
fDevice2Keystate[pRawInput->header.hDevice] = dwKeyCode; |
| 1136 |
} |
| 1137 |
else { |
| 1138 |
fDevice2Keystate.erase(pRawInput->header.hDevice); |
| 1139 |
} |
| 1140 |
// Only call the handlers if the app is in foreground |
| 1141 |
if (bIsForeground) { |
| 1142 |
// check for changes |
| 1143 |
if (dwKeyCode ^ dwOldKeyCode) { |
| 1144 |
// special handling of additional keys |
| 1145 |
if (dwKeyCode == 0x0036) { |
| 1146 |
// toggle rotate |
| 1147 |
f3dMouseParams.SetRotate(!f3dMouseParams.IsRotate()); |
| 1148 |
} |
| 1149 |
else if (dwKeyCode >= 0x00ce && dwKeyCode <= 0x00d2) { |
| 1150 |
// TODO: restore / save view |
| 1151 |
// , V3DK_V1LOAD = 0x00ce, V3DK_V1SAVE |
| 1152 |
// , V3DK_V2LOAD = 0x00d0, V3DK_V2SAVE |
| 1153 |
// , V3DK_V3LOAD = 0x00d2, V3DK_V3SAVE |
| 1154 |
} |
| 1155 |
else if (dwKeyCode != 0) { |
| 1156 |
// map key to virtual keycode |
| 1157 |
const auto iIndex(mapEnterpriseKey.find(dwKeyCode)); |
| 1158 |
if (iIndex != mapEnterpriseKey.end()) { |
| 1159 |
On3dmouseKeyDown(pRawInput->header.hDevice, (*iIndex).second); |
| 1160 |
} |
| 1161 |
} |
| 1162 |
else if (dwOldKeyCode != 0) { |
| 1163 |
// map key to virtual keycode |
| 1164 |
const auto iIndex(mapEnterpriseKey.find(dwOldKeyCode)); |
| 1165 |
if (iIndex != mapEnterpriseKey.end()) { |
| 1166 |
On3dmouseKeyUp(pRawInput->header.hDevice, (*iIndex).second); |
| 1167 |
} |
| 1168 |
} |
| 1169 |
} |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
return false; |
| 1174 |
} |
| 1175 |
|
| 1176 |
// ---------------------------------------------------------------------------- |
| 1177 |
|
| 1178 |
// SpaceMouse Plus XT |
| 1179 |
|
| 1180 |
// https://www.codeproject.com/Articles/185522/Using-the-Raw-Input-API-to-Process-Joystick-Input |
| 1181 |
// https://zfx.info/viewtopic.php?f=11&t=2977 |
| 1182 |
// |
| 1183 |
|
| 1184 |
#include <hidsdi.h> |
| 1185 |
#include <hidpi.h> |
| 1186 |
#include <hidusage.h> |
| 1187 |
|
| 1188 |
// #pragma comment(lib, "user32.lib") |
| 1189 |
// #pragma comment(lib, "hid.lib") |
| 1190 |
|
| 1191 |
#define MAX_BUTTONS 128 |
| 1192 |
#define CHECK(exp) \ |
| 1193 |
{ \ |
| 1194 |
if (!(exp)) \ |
| 1195 |
goto Error; \ |
| 1196 |
} |
| 1197 |
#define SAFE_FREE(p) \ |
| 1198 |
{ \ |
| 1199 |
if (p) { \ |
| 1200 |
HeapFree(hHeap, 0, p); \ |
| 1201 |
(p) = NULL; \ |
| 1202 |
} \ |
| 1203 |
} |
| 1204 |
|
| 1205 |
bool Gui::GuiNativeEvent::TranslateSpaceMouseOldGeneric(UINT nInputCode, PRAWINPUT pRawInput, DWORD /*dwProductId*/) |
| 1206 |
{ |
| 1207 |
bool processed = false; |
| 1208 |
bool bIsForeground = (nInputCode == RIM_INPUT); |
| 1209 |
|
| 1210 |
// Initialize with some random bit of data before we look up the real value later on |
| 1211 |
if (fDevice2Data.empty()) { |
| 1212 |
return false; |
| 1213 |
} |
| 1214 |
TInputData& deviceData = fDevice2Data.begin()->second; |
| 1215 |
|
| 1216 |
PHIDP_PREPARSED_DATA pPreparsedData; |
| 1217 |
HIDP_CAPS Caps; |
| 1218 |
PHIDP_BUTTON_CAPS pButtonCaps; |
| 1219 |
PHIDP_VALUE_CAPS pValueCaps; |
| 1220 |
USHORT capsLength; |
| 1221 |
UINT bufferSize; |
| 1222 |
HANDLE hHeap; |
| 1223 |
USAGE usage[MAX_BUTTONS]; |
| 1224 |
ULONG i, usageLength, value; |
| 1225 |
|
| 1226 |
BOOL bButtonStates[MAX_BUTTONS]; |
| 1227 |
|
| 1228 |
pPreparsedData = NULL; |
| 1229 |
pButtonCaps = NULL; |
| 1230 |
pValueCaps = NULL; |
| 1231 |
hHeap = GetProcessHeap(); |
| 1232 |
|
| 1233 |
#ifdef _TRACE_RI_RAWDATA |
| 1234 |
qDebug("pRawInput->data.hid.bRawData[0] = 0x%x", pRawInput->data.hid.bRawData[0]); |
| 1235 |
#endif |
| 1236 |
|
| 1237 |
// |
| 1238 |
// Get the preparsed data block |
| 1239 |
// |
| 1240 |
|
| 1241 |
CHECK(GetRawInputDeviceInfo(pRawInput->header.hDevice, RIDI_PREPARSEDDATA, NULL, &bufferSize) == 0); |
| 1242 |
CHECK(pPreparsedData = (PHIDP_PREPARSED_DATA)HeapAlloc(hHeap, 0, bufferSize)); |
| 1243 |
CHECK( |
| 1244 |
(int)GetRawInputDeviceInfo(pRawInput->header.hDevice, RIDI_PREPARSEDDATA, pPreparsedData, &bufferSize) |
| 1245 |
>= 0 |
| 1246 |
); |
| 1247 |
|
| 1248 |
// |
| 1249 |
// Get the joystick's capabilities |
| 1250 |
// |
| 1251 |
|
| 1252 |
// Button caps |
| 1253 |
CHECK(HidP_GetCaps(pPreparsedData, &Caps) == HIDP_STATUS_SUCCESS) |
| 1254 |
CHECK( |
| 1255 |
pButtonCaps = (PHIDP_BUTTON_CAPS) |
| 1256 |
HeapAlloc(hHeap, 0, sizeof(HIDP_BUTTON_CAPS) * Caps.NumberInputButtonCaps) |
| 1257 |
); |
| 1258 |
|
| 1259 |
capsLength = Caps.NumberInputButtonCaps; |
| 1260 |
CHECK(HidP_GetButtonCaps(HidP_Input, pButtonCaps, &capsLength, pPreparsedData) == HIDP_STATUS_SUCCESS) |
| 1261 |
usageLength = pButtonCaps->Range.UsageMax - pButtonCaps->Range.UsageMin + 1; |
| 1262 |
|
| 1263 |
// Value caps |
| 1264 |
CHECK( |
| 1265 |
pValueCaps |
| 1266 |
= (PHIDP_VALUE_CAPS)HeapAlloc(hHeap, 0, sizeof(HIDP_VALUE_CAPS) * Caps.NumberInputValueCaps) |
| 1267 |
); |
| 1268 |
capsLength = Caps.NumberInputValueCaps; |
| 1269 |
CHECK(HidP_GetValueCaps(HidP_Input, pValueCaps, &capsLength, pPreparsedData) == HIDP_STATUS_SUCCESS) |
| 1270 |
|
| 1271 |
processed = true; |
| 1272 |
|
| 1273 |
// |
| 1274 |
// Get the pressed buttons |
| 1275 |
// |
| 1276 |
|
| 1277 |
CHECK( |
| 1278 |
HidP_GetUsages( |
| 1279 |
HidP_Input, |
| 1280 |
pButtonCaps->UsagePage, |
| 1281 |
0, |
| 1282 |
usage, |
| 1283 |
&usageLength, |
| 1284 |
pPreparsedData, |
| 1285 |
(PCHAR)pRawInput->data.hid.bRawData, |
| 1286 |
pRawInput->data.hid.dwSizeHid |
| 1287 |
) |
| 1288 |
== HIDP_STATUS_SUCCESS |
| 1289 |
); |
| 1290 |
|
| 1291 |
ZeroMemory(bButtonStates, sizeof(bButtonStates)); |
| 1292 |
for (i = 0; i < usageLength; i++) { |
| 1293 |
bButtonStates[usage[i] - pButtonCaps->Range.UsageMin] = TRUE; |
| 1294 |
} |
| 1295 |
|
| 1296 |
// |
| 1297 |
// Get the state of discrete-valued-controls |
| 1298 |
// |
| 1299 |
|
| 1300 |
deviceData = fDevice2Data[pRawInput->header.hDevice]; |
| 1301 |
deviceData.fTimeToLive = kTimeToLive; |
| 1302 |
if (bIsForeground) { |
| 1303 |
for (i = 0; i < Caps.NumberInputValueCaps; i++) { |
| 1304 |
HidP_GetUsageValue( |
| 1305 |
HidP_Input, |
| 1306 |
pValueCaps[i].UsagePage, |
| 1307 |
0, |
| 1308 |
pValueCaps[i].Range.UsageMin, |
| 1309 |
&value, |
| 1310 |
pPreparsedData, |
| 1311 |
(PCHAR)pRawInput->data.hid.bRawData, |
| 1312 |
pRawInput->data.hid.dwSizeHid |
| 1313 |
); |
| 1314 |
|
| 1315 |
short svalue = static_cast<short>(value); |
| 1316 |
switch (pValueCaps[i].Range.UsageMin) { |
| 1317 |
case HID_USAGE_GENERIC_X: // X-axis |
| 1318 |
#ifdef _TRACE_RI_RAWDATA |
| 1319 |
qDebug("X-Axis: %d\n", svalue); |
| 1320 |
#endif |
| 1321 |
deviceData.fAxes[0] = static_cast<float>(svalue); |
| 1322 |
deviceData.fIsDirty = true; |
| 1323 |
break; |
| 1324 |
|
| 1325 |
case HID_USAGE_GENERIC_Y: // Y-axis |
| 1326 |
#ifdef _TRACE_RI_RAWDATA |
| 1327 |
qDebug("Y-Axis: %d\n", svalue); |
| 1328 |
#endif |
| 1329 |
deviceData.fAxes[1] = static_cast<float>(svalue); |
| 1330 |
deviceData.fIsDirty = true; |
| 1331 |
break; |
| 1332 |
|
| 1333 |
case HID_USAGE_GENERIC_Z: // Z-axis |
| 1334 |
#ifdef _TRACE_RI_RAWDATA |
| 1335 |
qDebug("Z-Axis: %d\n", svalue); |
| 1336 |
#endif |
| 1337 |
deviceData.fAxes[2] = static_cast<float>(svalue); |
| 1338 |
deviceData.fIsDirty = true; |
| 1339 |
break; |
| 1340 |
|
| 1341 |
case HID_USAGE_GENERIC_RX: // Rotate-X |
| 1342 |
#ifdef _TRACE_RI_RAWDATA |
| 1343 |
qDebug("X-Rotate: %d\n", svalue); |
| 1344 |
#endif |
| 1345 |
deviceData.fAxes[3] = static_cast<float>(svalue); |
| 1346 |
deviceData.fIsDirty = true; |
| 1347 |
break; |
| 1348 |
|
| 1349 |
case HID_USAGE_GENERIC_RY: // Rotate-Y |
| 1350 |
#ifdef _TRACE_RI_RAWDATA |
| 1351 |
qDebug("Y-Rotate: %d\n", svalue); |
| 1352 |
#endif |
| 1353 |
deviceData.fAxes[4] = static_cast<float>(svalue); |
| 1354 |
deviceData.fIsDirty = true; |
| 1355 |
break; |
| 1356 |
|
| 1357 |
case HID_USAGE_GENERIC_RZ: // Rotate-Z |
| 1358 |
#ifdef _TRACE_RI_RAWDATA |
| 1359 |
qDebug("Z-Rotate: %d\n", svalue); |
| 1360 |
#endif |
| 1361 |
deviceData.fAxes[5] = static_cast<float>(svalue); |
| 1362 |
deviceData.fIsDirty = true; |
| 1363 |
break; |
| 1364 |
|
| 1365 |
default: |
| 1366 |
break; |
| 1367 |
} |
| 1368 |
} |
| 1369 |
} |
| 1370 |
else { |
| 1371 |
// Zero out the data if the app is not in foreground |
| 1372 |
deviceData.fAxes.assign(6, 0.f); |
| 1373 |
deviceData.fIsDirty = true; |
| 1374 |
#ifdef _TRACE_RI_RAWDATA |
| 1375 |
qDebug("Not in foreground\n"); |
| 1376 |
#endif |
| 1377 |
} |
| 1378 |
|
| 1379 |
// |
| 1380 |
// Clean up |
| 1381 |
// |
| 1382 |
|
| 1383 |
Error: |
| 1384 |
SAFE_FREE(pPreparsedData); |
| 1385 |
SAFE_FREE(pButtonCaps); |
| 1386 |
SAFE_FREE(pValueCaps); |
| 1387 |
|
| 1388 |
return processed; |
| 1389 |
} |
| 1390 |
|
| 1391 |
#include "3Dconnexion/moc_GuiNativeEventWin32.cpp" |