diff options
Diffstat (limited to 'xbmc/addons/kodi-addon-dev-kit/include/kodi/kodi_peripheral_utils.hpp')
| -rw-r--r-- | xbmc/addons/kodi-addon-dev-kit/include/kodi/kodi_peripheral_utils.hpp | 663 |
1 files changed, 0 insertions, 663 deletions
diff --git a/xbmc/addons/kodi-addon-dev-kit/include/kodi/kodi_peripheral_utils.hpp b/xbmc/addons/kodi-addon-dev-kit/include/kodi/kodi_peripheral_utils.hpp deleted file mode 100644 index 2106d19..0000000 --- a/xbmc/addons/kodi-addon-dev-kit/include/kodi/kodi_peripheral_utils.hpp +++ /dev/null | |||
| @@ -1,663 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2014-2017 Team Kodi | ||
| 3 | * http://kodi.tv | ||
| 4 | * | ||
| 5 | * This Program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation; either version 2, or (at your option) | ||
| 8 | * any later version. | ||
| 9 | * | ||
| 10 | * This Program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this Program; see the file COPYING. If not, see | ||
| 17 | * <http://www.gnu.org/licenses/>. | ||
| 18 | * | ||
| 19 | */ | ||
| 20 | #pragma once | ||
| 21 | |||
| 22 | #include "kodi_peripheral_types.h" | ||
| 23 | |||
| 24 | #include <array> // Requires c++11 | ||
| 25 | #include <cstring> | ||
| 26 | #include <map> | ||
| 27 | #include <string> | ||
| 28 | #include <vector> | ||
| 29 | |||
| 30 | #define PERIPHERAL_SAFE_DELETE(x) do { delete (x); (x) = NULL; } while (0) | ||
| 31 | #define PERIPHERAL_SAFE_DELETE_ARRAY(x) do { delete[] (x); (x) = NULL; } while (0) | ||
| 32 | |||
| 33 | namespace ADDON | ||
| 34 | { | ||
| 35 | /*! | ||
| 36 | * Utility class to manipulate arrays of peripheral types. | ||
| 37 | */ | ||
| 38 | template <class THE_CLASS, typename THE_STRUCT> | ||
| 39 | class PeripheralVector | ||
| 40 | { | ||
| 41 | public: | ||
| 42 | static void ToStructs(const std::vector<THE_CLASS>& vecObjects, THE_STRUCT** pStructs) | ||
| 43 | { | ||
| 44 | if (!pStructs) | ||
| 45 | return; | ||
| 46 | |||
| 47 | if (vecObjects.empty()) | ||
| 48 | { | ||
| 49 | *pStructs = NULL; | ||
| 50 | } | ||
| 51 | else | ||
| 52 | { | ||
| 53 | (*pStructs) = new THE_STRUCT[vecObjects.size()]; | ||
| 54 | for (unsigned int i = 0; i < vecObjects.size(); i++) | ||
| 55 | vecObjects.at(i).ToStruct((*pStructs)[i]); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | static void ToStructs(const std::vector<THE_CLASS*>& vecObjects, THE_STRUCT** pStructs) | ||
| 60 | { | ||
| 61 | if (!pStructs) | ||
| 62 | return; | ||
| 63 | |||
| 64 | if (vecObjects.empty()) | ||
| 65 | { | ||
| 66 | *pStructs = NULL; | ||
| 67 | } | ||
| 68 | else | ||
| 69 | { | ||
| 70 | *pStructs = new THE_STRUCT[vecObjects.size()]; | ||
| 71 | for (unsigned int i = 0; i < vecObjects.size(); i++) | ||
| 72 | vecObjects.at(i)->ToStruct((*pStructs)[i]); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | static void FreeStructs(unsigned int structCount, THE_STRUCT* structs) | ||
| 77 | { | ||
| 78 | if (structs) | ||
| 79 | { | ||
| 80 | for (unsigned int i = 0; i < structCount; i++) | ||
| 81 | THE_CLASS::FreeStruct(structs[i]); | ||
| 82 | } | ||
| 83 | PERIPHERAL_SAFE_DELETE_ARRAY(structs); | ||
| 84 | } | ||
| 85 | }; | ||
| 86 | |||
| 87 | /*! | ||
| 88 | * ADDON::Peripheral | ||
| 89 | * | ||
| 90 | * Wrapper class providing peripheral information. Classes can extend | ||
| 91 | * Peripheral to inherit peripheral properties. | ||
| 92 | */ | ||
| 93 | class Peripheral | ||
| 94 | { | ||
| 95 | public: | ||
| 96 | Peripheral(PERIPHERAL_TYPE type = PERIPHERAL_TYPE_UNKNOWN, const std::string& strName = "") : | ||
| 97 | m_type(type), | ||
| 98 | m_strName(strName), | ||
| 99 | m_vendorId(0), | ||
| 100 | m_productId(0), | ||
| 101 | m_index(0) | ||
| 102 | { | ||
| 103 | } | ||
| 104 | |||
| 105 | Peripheral(const PERIPHERAL_INFO& info) : | ||
| 106 | m_type(info.type), | ||
| 107 | m_strName(info.name ? info.name : ""), | ||
| 108 | m_vendorId(info.vendor_id), | ||
| 109 | m_productId(info.product_id), | ||
| 110 | m_index(info.index) | ||
| 111 | { | ||
| 112 | } | ||
| 113 | |||
| 114 | virtual ~Peripheral(void) { } | ||
| 115 | |||
| 116 | PERIPHERAL_TYPE Type(void) const { return m_type; } | ||
| 117 | const std::string& Name(void) const { return m_strName; } | ||
| 118 | uint16_t VendorID(void) const { return m_vendorId; } | ||
| 119 | uint16_t ProductID(void) const { return m_productId; } | ||
| 120 | unsigned int Index(void) const { return m_index; } | ||
| 121 | |||
| 122 | // Derived property: VID and PID are 0x0000 if unknown | ||
| 123 | bool IsVidPidKnown(void) const { return m_vendorId != 0 || m_productId != 0; } | ||
| 124 | |||
| 125 | void SetType(PERIPHERAL_TYPE type) { m_type = type; } | ||
| 126 | void SetName(const std::string& strName) { m_strName = strName; } | ||
| 127 | void SetVendorID(uint16_t vendorId) { m_vendorId = vendorId; } | ||
| 128 | void SetProductID(uint16_t productId) { m_productId = productId; } | ||
| 129 | void SetIndex(unsigned int index) { m_index = index; } | ||
| 130 | |||
| 131 | void ToStruct(PERIPHERAL_INFO& info) const | ||
| 132 | { | ||
| 133 | info.type = m_type; | ||
| 134 | info.name = new char[m_strName.size() + 1]; | ||
| 135 | info.vendor_id = m_vendorId; | ||
| 136 | info.product_id = m_productId; | ||
| 137 | info.index = m_index; | ||
| 138 | |||
| 139 | std::strcpy(info.name, m_strName.c_str()); | ||
| 140 | } | ||
| 141 | |||
| 142 | static void FreeStruct(PERIPHERAL_INFO& info) | ||
| 143 | { | ||
| 144 | PERIPHERAL_SAFE_DELETE_ARRAY(info.name); | ||
| 145 | } | ||
| 146 | |||
| 147 | private: | ||
| 148 | PERIPHERAL_TYPE m_type; | ||
| 149 | std::string m_strName; | ||
| 150 | uint16_t m_vendorId; | ||
| 151 | uint16_t m_productId; | ||
| 152 | unsigned int m_index; | ||
| 153 | }; | ||
| 154 | |||
| 155 | typedef PeripheralVector<Peripheral, PERIPHERAL_INFO> Peripherals; | ||
| 156 | |||
| 157 | /*! | ||
| 158 | * ADDON::PeripheralEvent | ||
| 159 | * | ||
| 160 | * Wrapper class for peripheral events. | ||
| 161 | */ | ||
| 162 | class PeripheralEvent | ||
| 163 | { | ||
| 164 | public: | ||
| 165 | PeripheralEvent(void) : | ||
| 166 | m_event() | ||
| 167 | { | ||
| 168 | } | ||
| 169 | |||
| 170 | PeripheralEvent(unsigned int peripheralIndex, unsigned int buttonIndex, JOYSTICK_STATE_BUTTON state) : | ||
| 171 | m_event() | ||
| 172 | { | ||
| 173 | SetType(PERIPHERAL_EVENT_TYPE_DRIVER_BUTTON); | ||
| 174 | SetPeripheralIndex(peripheralIndex); | ||
| 175 | SetDriverIndex(buttonIndex); | ||
| 176 | SetButtonState(state); | ||
| 177 | } | ||
| 178 | |||
| 179 | PeripheralEvent(unsigned int peripheralIndex, unsigned int hatIndex, JOYSTICK_STATE_HAT state) : | ||
| 180 | m_event() | ||
| 181 | { | ||
| 182 | SetType(PERIPHERAL_EVENT_TYPE_DRIVER_HAT); | ||
| 183 | SetPeripheralIndex(peripheralIndex); | ||
| 184 | SetDriverIndex(hatIndex); | ||
| 185 | SetHatState(state); | ||
| 186 | } | ||
| 187 | |||
| 188 | PeripheralEvent(unsigned int peripheralIndex, unsigned int axisIndex, JOYSTICK_STATE_AXIS state) : | ||
| 189 | m_event() | ||
| 190 | { | ||
| 191 | SetType(PERIPHERAL_EVENT_TYPE_DRIVER_AXIS); | ||
| 192 | SetPeripheralIndex(peripheralIndex); | ||
| 193 | SetDriverIndex(axisIndex); | ||
| 194 | SetAxisState(state); | ||
| 195 | } | ||
| 196 | |||
| 197 | PeripheralEvent(const PERIPHERAL_EVENT& event) : | ||
| 198 | m_event(event) | ||
| 199 | { | ||
| 200 | } | ||
| 201 | |||
| 202 | PERIPHERAL_EVENT_TYPE Type(void) const { return m_event.type; } | ||
| 203 | unsigned int PeripheralIndex(void) const { return m_event.peripheral_index; } | ||
| 204 | unsigned int DriverIndex(void) const { return m_event.driver_index; } | ||
| 205 | JOYSTICK_STATE_BUTTON ButtonState(void) const { return m_event.driver_button_state; } | ||
| 206 | JOYSTICK_STATE_HAT HatState(void) const { return m_event.driver_hat_state; } | ||
| 207 | JOYSTICK_STATE_AXIS AxisState(void) const { return m_event.driver_axis_state; } | ||
| 208 | JOYSTICK_STATE_MOTOR MotorState(void) const { return m_event.motor_state; } | ||
| 209 | |||
| 210 | void SetType(PERIPHERAL_EVENT_TYPE type) { m_event.type = type; } | ||
| 211 | void SetPeripheralIndex(unsigned int index) { m_event.peripheral_index = index; } | ||
| 212 | void SetDriverIndex(unsigned int index) { m_event.driver_index = index; } | ||
| 213 | void SetButtonState(JOYSTICK_STATE_BUTTON state) { m_event.driver_button_state = state; } | ||
| 214 | void SetHatState(JOYSTICK_STATE_HAT state) { m_event.driver_hat_state = state; } | ||
| 215 | void SetAxisState(JOYSTICK_STATE_AXIS state) { m_event.driver_axis_state = state; } | ||
| 216 | void SetMotorState(JOYSTICK_STATE_MOTOR state) { m_event.motor_state = state; } | ||
| 217 | |||
| 218 | void ToStruct(PERIPHERAL_EVENT& event) const | ||
| 219 | { | ||
| 220 | event = m_event; | ||
| 221 | } | ||
| 222 | |||
| 223 | static void FreeStruct(PERIPHERAL_EVENT& event) | ||
| 224 | { | ||
| 225 | (void)event; | ||
| 226 | } | ||
| 227 | |||
| 228 | private: | ||
| 229 | PERIPHERAL_EVENT m_event; | ||
| 230 | }; | ||
| 231 | |||
| 232 | typedef PeripheralVector<PeripheralEvent, PERIPHERAL_EVENT> PeripheralEvents; | ||
| 233 | |||
| 234 | /*! | ||
| 235 | * ADDON::Joystick | ||
| 236 | * | ||
| 237 | * Wrapper class providing additional joystick information not provided by | ||
| 238 | * ADDON::Peripheral. | ||
| 239 | */ | ||
| 240 | class Joystick : public Peripheral | ||
| 241 | { | ||
| 242 | public: | ||
| 243 | Joystick(const std::string& provider = "", const std::string& strName = "") : | ||
| 244 | Peripheral(PERIPHERAL_TYPE_JOYSTICK, strName), | ||
| 245 | m_provider(provider), | ||
| 246 | m_requestedPort(NO_PORT_REQUESTED), | ||
| 247 | m_buttonCount(0), | ||
| 248 | m_hatCount(0), | ||
| 249 | m_axisCount(0), | ||
| 250 | m_motorCount(0), | ||
| 251 | m_supportsPowerOff(false) | ||
| 252 | { | ||
| 253 | } | ||
| 254 | |||
| 255 | Joystick(const Joystick& other) | ||
| 256 | { | ||
| 257 | *this = other; | ||
| 258 | } | ||
| 259 | |||
| 260 | Joystick(const JOYSTICK_INFO& info) : | ||
| 261 | Peripheral(info.peripheral), | ||
| 262 | m_provider(info.provider ? info.provider : ""), | ||
| 263 | m_requestedPort(info.requested_port), | ||
| 264 | m_buttonCount(info.button_count), | ||
| 265 | m_hatCount(info.hat_count), | ||
| 266 | m_axisCount(info.axis_count), | ||
| 267 | m_motorCount(info.motor_count), | ||
| 268 | m_supportsPowerOff(info.supports_poweroff) | ||
| 269 | { | ||
| 270 | } | ||
| 271 | |||
| 272 | virtual ~Joystick(void) { } | ||
| 273 | |||
| 274 | Joystick& operator=(const Joystick& rhs) | ||
| 275 | { | ||
| 276 | if (this != &rhs) | ||
| 277 | { | ||
| 278 | Peripheral::operator=(rhs); | ||
| 279 | |||
| 280 | m_provider = rhs.m_provider; | ||
| 281 | m_requestedPort = rhs.m_requestedPort; | ||
| 282 | m_buttonCount = rhs.m_buttonCount; | ||
| 283 | m_hatCount = rhs.m_hatCount; | ||
| 284 | m_axisCount = rhs.m_axisCount; | ||
| 285 | m_motorCount = rhs.m_motorCount; | ||
| 286 | m_supportsPowerOff = rhs.m_supportsPowerOff; | ||
| 287 | } | ||
| 288 | return *this; | ||
| 289 | } | ||
| 290 | |||
| 291 | const std::string& Provider(void) const { return m_provider; } | ||
| 292 | int RequestedPort(void) const { return m_requestedPort; } | ||
| 293 | unsigned int ButtonCount(void) const { return m_buttonCount; } | ||
| 294 | unsigned int HatCount(void) const { return m_hatCount; } | ||
| 295 | unsigned int AxisCount(void) const { return m_axisCount; } | ||
| 296 | unsigned int MotorCount(void) const { return m_motorCount; } | ||
| 297 | bool SupportsPowerOff(void) const { return m_supportsPowerOff; } | ||
| 298 | |||
| 299 | // Derived property: Counts are unknown if all are zero | ||
| 300 | bool AreElementCountsKnown(void) const { return m_buttonCount != 0 || m_hatCount != 0 || m_axisCount != 0; } | ||
| 301 | |||
| 302 | void SetProvider(const std::string& provider) { m_provider = provider; } | ||
| 303 | void SetRequestedPort(int requestedPort) { m_requestedPort = requestedPort; } | ||
| 304 | void SetButtonCount(unsigned int buttonCount) { m_buttonCount = buttonCount; } | ||
| 305 | void SetHatCount(unsigned int hatCount) { m_hatCount = hatCount; } | ||
| 306 | void SetAxisCount(unsigned int axisCount) { m_axisCount = axisCount; } | ||
| 307 | void SetMotorCount(unsigned int motorCount) { m_motorCount = motorCount; } | ||
| 308 | void SetSupportsPowerOff(bool supportsPowerOff) { m_supportsPowerOff = supportsPowerOff; } | ||
| 309 | |||
| 310 | void ToStruct(JOYSTICK_INFO& info) const | ||
| 311 | { | ||
| 312 | Peripheral::ToStruct(info.peripheral); | ||
| 313 | |||
| 314 | info.provider = new char[m_provider.size() + 1]; | ||
| 315 | info.requested_port = m_requestedPort; | ||
| 316 | info.button_count = m_buttonCount; | ||
| 317 | info.hat_count = m_hatCount; | ||
| 318 | info.axis_count = m_axisCount; | ||
| 319 | info.motor_count = m_motorCount; | ||
| 320 | info.supports_poweroff = m_supportsPowerOff; | ||
| 321 | |||
| 322 | std::strcpy(info.provider, m_provider.c_str()); | ||
| 323 | } | ||
| 324 | |||
| 325 | static void FreeStruct(JOYSTICK_INFO& info) | ||
| 326 | { | ||
| 327 | Peripheral::FreeStruct(info.peripheral); | ||
| 328 | |||
| 329 | PERIPHERAL_SAFE_DELETE_ARRAY(info.provider); | ||
| 330 | } | ||
| 331 | |||
| 332 | private: | ||
| 333 | std::string m_provider; | ||
| 334 | int m_requestedPort; | ||
| 335 | unsigned int m_buttonCount; | ||
| 336 | unsigned int m_hatCount; | ||
| 337 | unsigned int m_axisCount; | ||
| 338 | unsigned int m_motorCount; | ||
| 339 | bool m_supportsPowerOff; | ||
| 340 | }; | ||
| 341 | |||
| 342 | typedef PeripheralVector<Joystick, JOYSTICK_INFO> Joysticks; | ||
| 343 | |||
| 344 | /*! | ||
| 345 | * ADDON::DriverPrimitive | ||
| 346 | * | ||
| 347 | * Base class for joystick driver primitives. A driver primitive can be: | ||
| 348 | * | ||
| 349 | * 1) a button | ||
| 350 | * 2) a hat direction | ||
| 351 | * 3) a semiaxis (either the positive or negative half of an axis) | ||
| 352 | * 4) a motor | ||
| 353 | * | ||
| 354 | * The type determines the fields in use: | ||
| 355 | * | ||
| 356 | * Button: | ||
| 357 | * - driver index | ||
| 358 | * | ||
| 359 | * Hat direction: | ||
| 360 | * - driver index | ||
| 361 | * - hat direction | ||
| 362 | * | ||
| 363 | * Semiaxis: | ||
| 364 | * - driver index | ||
| 365 | * - center | ||
| 366 | * - semiaxis direction | ||
| 367 | * - range | ||
| 368 | * | ||
| 369 | * Motor: | ||
| 370 | * - driver index | ||
| 371 | */ | ||
| 372 | struct DriverPrimitive | ||
| 373 | { | ||
| 374 | protected: | ||
| 375 | /*! | ||
| 376 | * \brief Construct a driver primitive of the specified type | ||
| 377 | */ | ||
| 378 | DriverPrimitive(JOYSTICK_DRIVER_PRIMITIVE_TYPE type, unsigned int driverIndex) : | ||
| 379 | m_type(type), | ||
| 380 | m_driverIndex(driverIndex), | ||
| 381 | m_hatDirection(JOYSTICK_DRIVER_HAT_UNKNOWN), | ||
| 382 | m_center(0), | ||
| 383 | m_semiAxisDirection(JOYSTICK_DRIVER_SEMIAXIS_UNKNOWN), | ||
| 384 | m_range(1) | ||
| 385 | { | ||
| 386 | } | ||
| 387 | |||
| 388 | public: | ||
| 389 | /*! | ||
| 390 | * \brief Construct an invalid driver primitive | ||
| 391 | */ | ||
| 392 | DriverPrimitive(void) : | ||
| 393 | m_type(JOYSTICK_DRIVER_PRIMITIVE_TYPE_UNKNOWN), | ||
| 394 | m_driverIndex(0), | ||
| 395 | m_hatDirection(JOYSTICK_DRIVER_HAT_UNKNOWN), | ||
| 396 | m_center(0), | ||
| 397 | m_semiAxisDirection(JOYSTICK_DRIVER_SEMIAXIS_UNKNOWN), | ||
| 398 | m_range(1) | ||
| 399 | { | ||
| 400 | } | ||
| 401 | |||
| 402 | /*! | ||
| 403 | * \brief Construct a driver primitive representing a button | ||
| 404 | */ | ||
| 405 | static DriverPrimitive CreateButton(unsigned int buttonIndex) | ||
| 406 | { | ||
| 407 | return DriverPrimitive(JOYSTICK_DRIVER_PRIMITIVE_TYPE_BUTTON, buttonIndex); | ||
| 408 | } | ||
| 409 | |||
| 410 | /*! | ||
| 411 | * \brief Construct a driver primitive representing one of the four direction | ||
| 412 | * arrows on a dpad | ||
| 413 | */ | ||
| 414 | DriverPrimitive(unsigned int hatIndex, JOYSTICK_DRIVER_HAT_DIRECTION direction) : | ||
| 415 | m_type(JOYSTICK_DRIVER_PRIMITIVE_TYPE_HAT_DIRECTION), | ||
| 416 | m_driverIndex(hatIndex), | ||
| 417 | m_hatDirection(direction), | ||
| 418 | m_center(0), | ||
| 419 | m_semiAxisDirection(JOYSTICK_DRIVER_SEMIAXIS_UNKNOWN), | ||
| 420 | m_range(1) | ||
| 421 | { | ||
| 422 | } | ||
| 423 | |||
| 424 | /*! | ||
| 425 | * \brief Construct a driver primitive representing the positive or negative | ||
| 426 | * half of an axis | ||
| 427 | */ | ||
| 428 | DriverPrimitive(unsigned int axisIndex, int center, JOYSTICK_DRIVER_SEMIAXIS_DIRECTION direction, unsigned int range) : | ||
| 429 | m_type(JOYSTICK_DRIVER_PRIMITIVE_TYPE_SEMIAXIS), | ||
| 430 | m_driverIndex(axisIndex), | ||
| 431 | m_hatDirection(JOYSTICK_DRIVER_HAT_UNKNOWN), | ||
| 432 | m_center(center), | ||
| 433 | m_semiAxisDirection(direction), | ||
| 434 | m_range(range) | ||
| 435 | { | ||
| 436 | } | ||
| 437 | |||
| 438 | /*! | ||
| 439 | * \brief Construct a driver primitive representing a motor | ||
| 440 | */ | ||
| 441 | static DriverPrimitive CreateMotor(unsigned int motorIndex) | ||
| 442 | { | ||
| 443 | return DriverPrimitive(JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOTOR, motorIndex); | ||
| 444 | } | ||
| 445 | |||
| 446 | DriverPrimitive(const JOYSTICK_DRIVER_PRIMITIVE& primitive) : | ||
| 447 | m_type(primitive.type), | ||
| 448 | m_driverIndex(0), | ||
| 449 | m_hatDirection(JOYSTICK_DRIVER_HAT_UNKNOWN), | ||
| 450 | m_center(0), | ||
| 451 | m_semiAxisDirection(JOYSTICK_DRIVER_SEMIAXIS_UNKNOWN), | ||
| 452 | m_range(1) | ||
| 453 | { | ||
| 454 | switch (m_type) | ||
| 455 | { | ||
| 456 | case JOYSTICK_DRIVER_PRIMITIVE_TYPE_BUTTON: | ||
| 457 | { | ||
| 458 | m_driverIndex = primitive.button.index; | ||
| 459 | break; | ||
| 460 | } | ||
| 461 | case JOYSTICK_DRIVER_PRIMITIVE_TYPE_HAT_DIRECTION: | ||
| 462 | { | ||
| 463 | m_driverIndex = primitive.hat.index; | ||
| 464 | m_hatDirection = primitive.hat.direction; | ||
| 465 | break; | ||
| 466 | } | ||
| 467 | case JOYSTICK_DRIVER_PRIMITIVE_TYPE_SEMIAXIS: | ||
| 468 | { | ||
| 469 | m_driverIndex = primitive.semiaxis.index; | ||
| 470 | m_center = primitive.semiaxis.center; | ||
| 471 | m_semiAxisDirection = primitive.semiaxis.direction; | ||
| 472 | m_range = primitive.semiaxis.range; | ||
| 473 | break; | ||
| 474 | } | ||
| 475 | case JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOTOR: | ||
| 476 | { | ||
| 477 | m_driverIndex = primitive.motor.index; | ||
| 478 | break; | ||
| 479 | } | ||
| 480 | default: | ||
| 481 | break; | ||
| 482 | } | ||
| 483 | } | ||
| 484 | |||
| 485 | JOYSTICK_DRIVER_PRIMITIVE_TYPE Type(void) const { return m_type; } | ||
| 486 | unsigned int DriverIndex(void) const { return m_driverIndex; } | ||
| 487 | JOYSTICK_DRIVER_HAT_DIRECTION HatDirection(void) const { return m_hatDirection; } | ||
| 488 | int Center(void) const { return m_center; } | ||
| 489 | JOYSTICK_DRIVER_SEMIAXIS_DIRECTION SemiAxisDirection(void) const { return m_semiAxisDirection; } | ||
| 490 | unsigned int Range(void) const { return m_range; } | ||
| 491 | |||
| 492 | bool operator==(const DriverPrimitive& other) const | ||
| 493 | { | ||
| 494 | if (m_type == other.m_type) | ||
| 495 | { | ||
| 496 | switch (m_type) | ||
| 497 | { | ||
| 498 | case JOYSTICK_DRIVER_PRIMITIVE_TYPE_BUTTON: | ||
| 499 | case JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOTOR: | ||
| 500 | { | ||
| 501 | return m_driverIndex == other.m_driverIndex; | ||
| 502 | } | ||
| 503 | case JOYSTICK_DRIVER_PRIMITIVE_TYPE_HAT_DIRECTION: | ||
| 504 | { | ||
| 505 | return m_driverIndex == other.m_driverIndex && | ||
| 506 | m_hatDirection == other.m_hatDirection; | ||
| 507 | } | ||
| 508 | case JOYSTICK_DRIVER_PRIMITIVE_TYPE_SEMIAXIS: | ||
| 509 | { | ||
| 510 | return m_driverIndex == other.m_driverIndex && | ||
| 511 | m_center == other.m_center && | ||
| 512 | m_semiAxisDirection == other.m_semiAxisDirection && | ||
| 513 | m_range == other.m_range; | ||
| 514 | } | ||
| 515 | default: | ||
| 516 | break; | ||
| 517 | } | ||
| 518 | } | ||
| 519 | return false; | ||
| 520 | } | ||
| 521 | |||
| 522 | void ToStruct(JOYSTICK_DRIVER_PRIMITIVE& driver_primitive) const | ||
| 523 | { | ||
| 524 | driver_primitive.type = m_type; | ||
| 525 | switch (m_type) | ||
| 526 | { | ||
| 527 | case JOYSTICK_DRIVER_PRIMITIVE_TYPE_BUTTON: | ||
| 528 | { | ||
| 529 | driver_primitive.button.index = m_driverIndex; | ||
| 530 | break; | ||
| 531 | } | ||
| 532 | case JOYSTICK_DRIVER_PRIMITIVE_TYPE_HAT_DIRECTION: | ||
| 533 | { | ||
| 534 | driver_primitive.hat.index = m_driverIndex; | ||
| 535 | driver_primitive.hat.direction = m_hatDirection; | ||
| 536 | break; | ||
| 537 | } | ||
| 538 | case JOYSTICK_DRIVER_PRIMITIVE_TYPE_SEMIAXIS: | ||
| 539 | { | ||
| 540 | driver_primitive.semiaxis.index = m_driverIndex; | ||
| 541 | driver_primitive.semiaxis.center = m_center; | ||
| 542 | driver_primitive.semiaxis.direction = m_semiAxisDirection; | ||
| 543 | driver_primitive.semiaxis.range = m_range; | ||
| 544 | break; | ||
| 545 | } | ||
| 546 | case JOYSTICK_DRIVER_PRIMITIVE_TYPE_MOTOR: | ||
| 547 | { | ||
| 548 | driver_primitive.motor.index = m_driverIndex; | ||
| 549 | break; | ||
| 550 | } | ||
| 551 | default: | ||
| 552 | break; | ||
| 553 | } | ||
| 554 | } | ||
| 555 | |||
| 556 | static void FreeStruct(JOYSTICK_DRIVER_PRIMITIVE& primitive) | ||
| 557 | { | ||
| 558 | (void)primitive; | ||
| 559 | } | ||
| 560 | |||
| 561 | private: | ||
| 562 | JOYSTICK_DRIVER_PRIMITIVE_TYPE m_type; | ||
| 563 | unsigned int m_driverIndex; | ||
| 564 | JOYSTICK_DRIVER_HAT_DIRECTION m_hatDirection; | ||
| 565 | int m_center; | ||
| 566 | JOYSTICK_DRIVER_SEMIAXIS_DIRECTION m_semiAxisDirection; | ||
| 567 | unsigned int m_range; | ||
| 568 | }; | ||
| 569 | |||
| 570 | typedef PeripheralVector<DriverPrimitive, JOYSTICK_DRIVER_PRIMITIVE> DriverPrimitives; | ||
| 571 | |||
| 572 | /*! | ||
| 573 | * ADDON::JoystickFeature | ||
| 574 | * | ||
| 575 | * Class for joystick features. A feature can be: | ||
| 576 | * | ||
| 577 | * 1) scalar[1] | ||
| 578 | * 2) analog stick | ||
| 579 | * 3) accelerometer | ||
| 580 | * 4) motor | ||
| 581 | * | ||
| 582 | * [1] All three driver primitives (buttons, hats and axes) have a state that | ||
| 583 | * can be represented using a single scalar value. For this reason, | ||
| 584 | * features that map to a single primitive are called "scalar features". | ||
| 585 | */ | ||
| 586 | class JoystickFeature | ||
| 587 | { | ||
| 588 | public: | ||
| 589 | JoystickFeature(const std::string& name = "", JOYSTICK_FEATURE_TYPE type = JOYSTICK_FEATURE_TYPE_UNKNOWN) : | ||
| 590 | m_name(name), | ||
| 591 | m_type(type), | ||
| 592 | m_primitives() | ||
| 593 | { | ||
| 594 | } | ||
| 595 | |||
| 596 | JoystickFeature(const JoystickFeature& other) | ||
| 597 | { | ||
| 598 | *this = other; | ||
| 599 | } | ||
| 600 | |||
| 601 | JoystickFeature(const JOYSTICK_FEATURE& feature) : | ||
| 602 | m_name(feature.name ? feature.name : ""), | ||
| 603 | m_type(feature.type) | ||
| 604 | { | ||
| 605 | for (unsigned int i = 0; i < JOYSTICK_PRIMITIVE_MAX; i++) | ||
| 606 | m_primitives[i] = feature.primitives[i]; | ||
| 607 | } | ||
| 608 | |||
| 609 | JoystickFeature& operator=(const JoystickFeature& rhs) | ||
| 610 | { | ||
| 611 | if (this != &rhs) | ||
| 612 | { | ||
| 613 | m_name = rhs.m_name; | ||
| 614 | m_type = rhs.m_type; | ||
| 615 | m_primitives = rhs.m_primitives; | ||
| 616 | } | ||
| 617 | return *this; | ||
| 618 | } | ||
| 619 | |||
| 620 | bool operator==(const JoystickFeature& other) const | ||
| 621 | { | ||
| 622 | return m_name == other.m_name && | ||
| 623 | m_type == other.m_type && | ||
| 624 | m_primitives == other.m_primitives; | ||
| 625 | } | ||
| 626 | |||
| 627 | const std::string& Name(void) const { return m_name; } | ||
| 628 | JOYSTICK_FEATURE_TYPE Type(void) const { return m_type; } | ||
| 629 | bool IsValid() const { return m_type != JOYSTICK_FEATURE_TYPE_UNKNOWN; } | ||
| 630 | |||
| 631 | void SetName(const std::string& name) { m_name = name; } | ||
| 632 | void SetType(JOYSTICK_FEATURE_TYPE type) { m_type = type; } | ||
| 633 | void SetInvalid(void) { m_type = JOYSTICK_FEATURE_TYPE_UNKNOWN; } | ||
| 634 | |||
| 635 | const DriverPrimitive& Primitive(JOYSTICK_FEATURE_PRIMITIVE which) const { return m_primitives[which]; } | ||
| 636 | void SetPrimitive(JOYSTICK_FEATURE_PRIMITIVE which, const DriverPrimitive& primitive) { m_primitives[which] = primitive; } | ||
| 637 | |||
| 638 | std::array<DriverPrimitive, JOYSTICK_PRIMITIVE_MAX>& Primitives() { return m_primitives; } | ||
| 639 | const std::array<DriverPrimitive, JOYSTICK_PRIMITIVE_MAX>& Primitives() const { return m_primitives; } | ||
| 640 | |||
| 641 | void ToStruct(JOYSTICK_FEATURE& feature) const | ||
| 642 | { | ||
| 643 | feature.name = new char[m_name.length() + 1]; | ||
| 644 | feature.type = m_type; | ||
| 645 | for (unsigned int i = 0; i < JOYSTICK_PRIMITIVE_MAX; i++) | ||
| 646 | m_primitives[i].ToStruct(feature.primitives[i]); | ||
| 647 | |||
| 648 | std::strcpy(feature.name, m_name.c_str()); | ||
| 649 | } | ||
| 650 | |||
| 651 | static void FreeStruct(JOYSTICK_FEATURE& feature) | ||
| 652 | { | ||
| 653 | PERIPHERAL_SAFE_DELETE_ARRAY(feature.name); | ||
| 654 | } | ||
| 655 | |||
| 656 | private: | ||
| 657 | std::string m_name; | ||
| 658 | JOYSTICK_FEATURE_TYPE m_type; | ||
| 659 | std::array<DriverPrimitive, JOYSTICK_PRIMITIVE_MAX> m_primitives; | ||
| 660 | }; | ||
| 661 | |||
| 662 | typedef PeripheralVector<JoystickFeature, JOYSTICK_FEATURE> JoystickFeatures; | ||
| 663 | } | ||
