summaryrefslogtreecommitdiffstats
path: root/xbmc/peripherals/addons/AddonButtonMap.cpp
blob: 0d76ec96a405eaf88ca4959eed24b7dfab903d7d (plain)
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/*
 *  Copyright (C) 2014-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "AddonButtonMap.h"

#include "PeripheralAddonTranslator.h"
#include "input/joysticks/JoystickUtils.h"
#include "peripherals/Peripherals.h"
#include "peripherals/devices/Peripheral.h"
#include "utils/log.h"

#include <algorithm>
#include <assert.h>
#include <mutex>
#include <vector>

using namespace KODI;
using namespace JOYSTICK;
using namespace PERIPHERALS;

CAddonButtonMap::CAddonButtonMap(CPeripheral* device,
                                 const std::weak_ptr<CPeripheralAddon>& addon,
                                 const std::string& strControllerId,
                                 CPeripherals& manager)
  : m_device(device), m_addon(addon), m_strControllerId(strControllerId), m_manager(manager)
{
  auto peripheralAddon = m_addon.lock();
  assert(peripheralAddon != nullptr);

  peripheralAddon->RegisterButtonMap(device, this);
}

CAddonButtonMap::~CAddonButtonMap(void)
{
  if (auto addon = m_addon.lock())
    addon->UnregisterButtonMap(this);
}

std::string CAddonButtonMap::Location(void) const
{
  return m_device->Location();
}

bool CAddonButtonMap::Load(void)
{
  std::string controllerAppearance;
  FeatureMap features;
  DriverMap driverMap;
  PrimitiveVector ignoredPrimitives;

  bool bSuccess = false;
  if (auto addon = m_addon.lock())
  {
    bSuccess |= addon->GetFeatures(m_device, m_strControllerId, features);
    bSuccess |= addon->GetIgnoredPrimitives(m_device, ignoredPrimitives);
  }

  if (features.empty())
  {
    // Check if we can initialize a buttonmap from the peripheral bus
    PeripheralBusPtr peripheralBus = m_manager.GetBusByType(m_device->GetBusType());
    if (peripheralBus)
    {
      CLog::Log(LOGDEBUG,
                "Buttonmap not found for {}, attempting to initialize from peripheral bus",
                m_device->Location());
      if (peripheralBus->InitializeButtonMap(*m_device, *this))
      {
        bSuccess = true;

        if (auto addon = m_addon.lock())
        {
          addon->GetFeatures(m_device, m_strControllerId, features);
          addon->GetIgnoredPrimitives(m_device, ignoredPrimitives);
        }
      }
    }
  }

  // GetFeatures() was changed to always return false if no features were
  // retrieved. Check here, just in case its contract is changed or violated in
  // the future.
  if (bSuccess && features.empty())
    bSuccess = false;

  if (bSuccess)
    driverMap = CreateLookupTable(features);
  else
    CLog::Log(LOGDEBUG, "Failed to load button map for \"{}\"", m_device->Location());

  {
    std::unique_lock<CCriticalSection> lock(m_mutex);
    m_controllerAppearance = std::move(controllerAppearance);
    m_features = std::move(features);
    m_driverMap = std::move(driverMap);
    m_ignoredPrimitives = CPeripheralAddonTranslator::TranslatePrimitives(ignoredPrimitives);
  }

  return true;
}

void CAddonButtonMap::Reset(void)
{
  if (auto addon = m_addon.lock())
    addon->ResetButtonMap(m_device, m_strControllerId);
}

bool CAddonButtonMap::IsEmpty(void) const
{
  std::unique_lock<CCriticalSection> lock(m_mutex);

  return m_driverMap.empty();
}

std::string CAddonButtonMap::GetAppearance() const
{
  std::unique_lock<CCriticalSection> lock(m_mutex);

  return m_controllerAppearance;
}

bool CAddonButtonMap::SetAppearance(const std::string& controllerId) const
{
  return false;
}

bool CAddonButtonMap::GetFeature(const CDriverPrimitive& primitive, FeatureName& feature)
{
  std::unique_lock<CCriticalSection> lock(m_mutex);

  DriverMap::const_iterator it = m_driverMap.find(primitive);
  if (it != m_driverMap.end())
  {
    feature = it->second;
    return true;
  }

  return false;
}

FEATURE_TYPE CAddonButtonMap::GetFeatureType(const FeatureName& feature)
{
  FEATURE_TYPE type = FEATURE_TYPE::UNKNOWN;

  std::unique_lock<CCriticalSection> lock(m_mutex);

  FeatureMap::const_iterator it = m_features.find(feature);
  if (it != m_features.end())
    type = CPeripheralAddonTranslator::TranslateFeatureType(it->second.Type());

  return type;
}

bool CAddonButtonMap::GetScalar(const FeatureName& feature, CDriverPrimitive& primitive)
{
  bool retVal(false);

  std::unique_lock<CCriticalSection> lock(m_mutex);

  FeatureMap::const_iterator it = m_features.find(feature);
  if (it != m_features.end())
  {
    const kodi::addon::JoystickFeature& addonFeature = it->second;

    if (addonFeature.Type() == JOYSTICK_FEATURE_TYPE_SCALAR ||
        addonFeature.Type() == JOYSTICK_FEATURE_TYPE_MOTOR)
    {
      primitive = CPeripheralAddonTranslator::TranslatePrimitive(
          addonFeature.Primitive(JOYSTICK_SCALAR_PRIMITIVE));
      retVal = true;
    }
  }

  return retVal;
}

void CAddonButtonMap::AddScalar(const FeatureName& feature, const CDriverPrimitive& primitive)
{
  const bool bMotor = (primitive.Type() == PRIMITIVE_TYPE::MOTOR);

  kodi::addon::JoystickFeature scalar(feature, bMotor ? JOYSTICK_FEATURE_TYPE_MOTOR
                                                      : JOYSTICK_FEATURE_TYPE_SCALAR);
  scalar.SetPrimitive(JOYSTICK_SCALAR_PRIMITIVE,
                      CPeripheralAddonTranslator::TranslatePrimitive(primitive));

  if (auto addon = m_addon.lock())
    addon->MapFeature(m_device, m_strControllerId, scalar);
}

bool CAddonButtonMap::GetAnalogStick(const FeatureName& feature,
                                     JOYSTICK::ANALOG_STICK_DIRECTION direction,
                                     JOYSTICK::CDriverPrimitive& primitive)
{
  bool retVal(false);

  std::unique_lock<CCriticalSection> lock(m_mutex);

  FeatureMap::const_iterator it = m_features.find(feature);
  if (it != m_features.end())
  {
    const kodi::addon::JoystickFeature& addonFeature = it->second;

    if (addonFeature.Type() == JOYSTICK_FEATURE_TYPE_ANALOG_STICK)
    {
      primitive = CPeripheralAddonTranslator::TranslatePrimitive(
          addonFeature.Primitive(GetAnalogStickIndex(direction)));
      retVal = primitive.IsValid();
    }
  }

  return retVal;
}

void CAddonButtonMap::AddAnalogStick(const FeatureName& feature,
                                     JOYSTICK::ANALOG_STICK_DIRECTION direction,
                                     const JOYSTICK::CDriverPrimitive& primitive)
{
  using namespace JOYSTICK;

  JOYSTICK_FEATURE_PRIMITIVE primitiveIndex = GetAnalogStickIndex(direction);
  kodi::addon::DriverPrimitive addonPrimitive =
      CPeripheralAddonTranslator::TranslatePrimitive(primitive);

  kodi::addon::JoystickFeature analogStick(feature, JOYSTICK_FEATURE_TYPE_ANALOG_STICK);

  {
    std::unique_lock<CCriticalSection> lock(m_mutex);
    auto it = m_features.find(feature);
    if (it != m_features.end())
      analogStick = it->second;
  }

  const bool bModified = (primitive != CPeripheralAddonTranslator::TranslatePrimitive(
                                           analogStick.Primitive(primitiveIndex)));
  if (bModified)
    analogStick.SetPrimitive(primitiveIndex, addonPrimitive);

  if (auto addon = m_addon.lock())
    addon->MapFeature(m_device, m_strControllerId, analogStick);

  // Because each direction is mapped individually, we need to refresh the
  // feature each time a new direction is mapped.
  if (bModified)
    Load();
}

bool CAddonButtonMap::GetRelativePointer(const FeatureName& feature,
                                         JOYSTICK::RELATIVE_POINTER_DIRECTION direction,
                                         JOYSTICK::CDriverPrimitive& primitive)
{
  bool retVal(false);

  std::unique_lock<CCriticalSection> lock(m_mutex);

  FeatureMap::const_iterator it = m_features.find(feature);
  if (it != m_features.end())
  {
    const kodi::addon::JoystickFeature& addonFeature = it->second;

    if (addonFeature.Type() == JOYSTICK_FEATURE_TYPE_RELPOINTER)
    {
      primitive = CPeripheralAddonTranslator::TranslatePrimitive(
          addonFeature.Primitive(GetRelativePointerIndex(direction)));
      retVal = primitive.IsValid();
    }
  }

  return retVal;
}

void CAddonButtonMap::AddRelativePointer(const FeatureName& feature,
                                         JOYSTICK::RELATIVE_POINTER_DIRECTION direction,
                                         const JOYSTICK::CDriverPrimitive& primitive)
{
  using namespace JOYSTICK;

  JOYSTICK_FEATURE_PRIMITIVE primitiveIndex = GetRelativePointerIndex(direction);
  kodi::addon::DriverPrimitive addonPrimitive =
      CPeripheralAddonTranslator::TranslatePrimitive(primitive);

  kodi::addon::JoystickFeature relPointer(feature, JOYSTICK_FEATURE_TYPE_RELPOINTER);

  {
    std::unique_lock<CCriticalSection> lock(m_mutex);
    auto it = m_features.find(feature);
    if (it != m_features.end())
      relPointer = it->second;
  }

  const bool bModified = (primitive != CPeripheralAddonTranslator::TranslatePrimitive(
                                           relPointer.Primitive(primitiveIndex)));
  if (bModified)
    relPointer.SetPrimitive(primitiveIndex, addonPrimitive);

  if (auto addon = m_addon.lock())
    addon->MapFeature(m_device, m_strControllerId, relPointer);

  // Because each direction is mapped individually, we need to refresh the
  // feature each time a new direction is mapped.
  if (bModified)
    Load();
}

bool CAddonButtonMap::GetAccelerometer(const FeatureName& feature,
                                       CDriverPrimitive& positiveX,
                                       CDriverPrimitive& positiveY,
                                       CDriverPrimitive& positiveZ)
{
  bool retVal(false);

  std::unique_lock<CCriticalSection> lock(m_mutex);

  FeatureMap::const_iterator it = m_features.find(feature);
  if (it != m_features.end())
  {
    const kodi::addon::JoystickFeature& addonFeature = it->second;

    if (addonFeature.Type() == JOYSTICK_FEATURE_TYPE_ACCELEROMETER)
    {
      positiveX = CPeripheralAddonTranslator::TranslatePrimitive(
          addonFeature.Primitive(JOYSTICK_ACCELEROMETER_POSITIVE_X));
      positiveY = CPeripheralAddonTranslator::TranslatePrimitive(
          addonFeature.Primitive(JOYSTICK_ACCELEROMETER_POSITIVE_Y));
      positiveZ = CPeripheralAddonTranslator::TranslatePrimitive(
          addonFeature.Primitive(JOYSTICK_ACCELEROMETER_POSITIVE_Z));
      retVal = true;
    }
  }

  return retVal;
}

void CAddonButtonMap::AddAccelerometer(const FeatureName& feature,
                                       const CDriverPrimitive& positiveX,
                                       const CDriverPrimitive& positiveY,
                                       const CDriverPrimitive& positiveZ)
{
  using namespace JOYSTICK;

  kodi::addon::JoystickFeature accelerometer(feature, JOYSTICK_FEATURE_TYPE_ACCELEROMETER);

  accelerometer.SetPrimitive(JOYSTICK_ACCELEROMETER_POSITIVE_X,
                             CPeripheralAddonTranslator::TranslatePrimitive(positiveX));
  accelerometer.SetPrimitive(JOYSTICK_ACCELEROMETER_POSITIVE_Y,
                             CPeripheralAddonTranslator::TranslatePrimitive(positiveY));
  accelerometer.SetPrimitive(JOYSTICK_ACCELEROMETER_POSITIVE_Z,
                             CPeripheralAddonTranslator::TranslatePrimitive(positiveZ));

  if (auto addon = m_addon.lock())
    addon->MapFeature(m_device, m_strControllerId, accelerometer);
}

bool CAddonButtonMap::GetWheel(const KODI::JOYSTICK::FeatureName& feature,
                               KODI::JOYSTICK::WHEEL_DIRECTION direction,
                               KODI::JOYSTICK::CDriverPrimitive& primitive)
{
  bool retVal(false);

  std::unique_lock<CCriticalSection> lock(m_mutex);

  FeatureMap::const_iterator it = m_features.find(feature);
  if (it != m_features.end())
  {
    const kodi::addon::JoystickFeature& addonFeature = it->second;

    if (addonFeature.Type() == JOYSTICK_FEATURE_TYPE_WHEEL)
    {
      primitive = CPeripheralAddonTranslator::TranslatePrimitive(
          addonFeature.Primitive(GetPrimitiveIndex(direction)));
      retVal = primitive.IsValid();
    }
  }

  return retVal;
}

void CAddonButtonMap::AddWheel(const KODI::JOYSTICK::FeatureName& feature,
                               KODI::JOYSTICK::WHEEL_DIRECTION direction,
                               const KODI::JOYSTICK::CDriverPrimitive& primitive)
{
  using namespace JOYSTICK;

  JOYSTICK_FEATURE_PRIMITIVE primitiveIndex = GetPrimitiveIndex(direction);
  kodi::addon::DriverPrimitive addonPrimitive =
      CPeripheralAddonTranslator::TranslatePrimitive(primitive);

  kodi::addon::JoystickFeature joystickFeature(feature, JOYSTICK_FEATURE_TYPE_WHEEL);

  {
    std::unique_lock<CCriticalSection> lock(m_mutex);
    auto it = m_features.find(feature);
    if (it != m_features.end())
      joystickFeature = it->second;
  }

  const bool bModified = (primitive != CPeripheralAddonTranslator::TranslatePrimitive(
                                           joystickFeature.Primitive(primitiveIndex)));
  if (bModified)
    joystickFeature.SetPrimitive(primitiveIndex, addonPrimitive);

  if (auto addon = m_addon.lock())
    addon->MapFeature(m_device, m_strControllerId, joystickFeature);

  // Because each direction is mapped individually, we need to refresh the
  // feature each time a new direction is mapped.
  if (bModified)
    Load();
}

bool CAddonButtonMap::GetThrottle(const KODI::JOYSTICK::FeatureName& feature,
                                  KODI::JOYSTICK::THROTTLE_DIRECTION direction,
                                  KODI::JOYSTICK::CDriverPrimitive& primitive)
{
  bool retVal(false);

  std::unique_lock<CCriticalSection> lock(m_mutex);

  FeatureMap::const_iterator it = m_features.find(feature);
  if (it != m_features.end())
  {
    const kodi::addon::JoystickFeature& addonFeature = it->second;

    if (addonFeature.Type() == JOYSTICK_FEATURE_TYPE_THROTTLE)
    {
      primitive = CPeripheralAddonTranslator::TranslatePrimitive(
          addonFeature.Primitive(GetPrimitiveIndex(direction)));
      retVal = primitive.IsValid();
    }
  }

  return retVal;
}

void CAddonButtonMap::AddThrottle(const KODI::JOYSTICK::FeatureName& feature,
                                  KODI::JOYSTICK::THROTTLE_DIRECTION direction,
                                  const KODI::JOYSTICK::CDriverPrimitive& primitive)
{
  using namespace JOYSTICK;

  JOYSTICK_FEATURE_PRIMITIVE primitiveIndex = GetPrimitiveIndex(direction);
  kodi::addon::DriverPrimitive addonPrimitive =
      CPeripheralAddonTranslator::TranslatePrimitive(primitive);

  kodi::addon::JoystickFeature joystickFeature(feature, JOYSTICK_FEATURE_TYPE_THROTTLE);

  {
    std::unique_lock<CCriticalSection> lock(m_mutex);
    auto it = m_features.find(feature);
    if (it != m_features.end())
      joystickFeature = it->second;
  }

  const bool bModified = (primitive != CPeripheralAddonTranslator::TranslatePrimitive(
                                           joystickFeature.Primitive(primitiveIndex)));
  if (bModified)
    joystickFeature.SetPrimitive(primitiveIndex, addonPrimitive);

  if (auto addon = m_addon.lock())
    addon->MapFeature(m_device, m_strControllerId, joystickFeature);

  // Because each direction is mapped individually, we need to refresh the
  // feature each time a new direction is mapped.
  if (bModified)
    Load();
}

bool CAddonButtonMap::GetKey(const FeatureName& feature, CDriverPrimitive& primitive)
{
  bool retVal(false);

  std::unique_lock<CCriticalSection> lock(m_mutex);

  FeatureMap::const_iterator it = m_features.find(feature);
  if (it != m_features.end())
  {
    const kodi::addon::JoystickFeature& addonFeature = it->second;

    if (addonFeature.Type() == JOYSTICK_FEATURE_TYPE_KEY)
    {
      primitive = CPeripheralAddonTranslator::TranslatePrimitive(
          addonFeature.Primitive(JOYSTICK_SCALAR_PRIMITIVE));
      retVal = true;
    }
  }

  return retVal;
}

void CAddonButtonMap::AddKey(const FeatureName& feature, const CDriverPrimitive& primitive)
{
  kodi::addon::JoystickFeature scalar(feature, JOYSTICK_FEATURE_TYPE_KEY);
  scalar.SetPrimitive(JOYSTICK_SCALAR_PRIMITIVE,
                      CPeripheralAddonTranslator::TranslatePrimitive(primitive));

  if (auto addon = m_addon.lock())
    addon->MapFeature(m_device, m_strControllerId, scalar);
}

void CAddonButtonMap::SetIgnoredPrimitives(
    const std::vector<JOYSTICK::CDriverPrimitive>& primitives)
{
  if (auto addon = m_addon.lock())
    addon->SetIgnoredPrimitives(m_device,
                                CPeripheralAddonTranslator::TranslatePrimitives(primitives));
}

bool CAddonButtonMap::IsIgnored(const JOYSTICK::CDriverPrimitive& primitive)
{
  return std::find(m_ignoredPrimitives.begin(), m_ignoredPrimitives.end(), primitive) !=
         m_ignoredPrimitives.end();
}

bool CAddonButtonMap::GetAxisProperties(unsigned int axisIndex, int& center, unsigned int& range)
{
  std::unique_lock<CCriticalSection> lock(m_mutex);

  for (const auto& it : m_driverMap)
  {
    const CDriverPrimitive& primitive = it.first;

    if (primitive.Type() != PRIMITIVE_TYPE::SEMIAXIS)
      continue;

    if (primitive.Index() != axisIndex)
      continue;

    center = primitive.Center();
    range = primitive.Range();
    return true;
  }

  return false;
}

void CAddonButtonMap::SaveButtonMap()
{
  if (auto addon = m_addon.lock())
    addon->SaveButtonMap(m_device);
}

void CAddonButtonMap::RevertButtonMap()
{
  if (auto addon = m_addon.lock())
    addon->RevertButtonMap(m_device);
}

CAddonButtonMap::DriverMap CAddonButtonMap::CreateLookupTable(const FeatureMap& features)
{
  using namespace JOYSTICK;

  DriverMap driverMap;

  for (const auto& it : features)
  {
    const kodi::addon::JoystickFeature& feature = it.second;

    switch (feature.Type())
    {
      case JOYSTICK_FEATURE_TYPE_SCALAR:
      case JOYSTICK_FEATURE_TYPE_KEY:
      {
        driverMap[CPeripheralAddonTranslator::TranslatePrimitive(
            feature.Primitive(JOYSTICK_SCALAR_PRIMITIVE))] = it.first;
        break;
      }

      case JOYSTICK_FEATURE_TYPE_ANALOG_STICK:
      {
        std::vector<JOYSTICK_FEATURE_PRIMITIVE> primitives = {
            JOYSTICK_ANALOG_STICK_UP,
            JOYSTICK_ANALOG_STICK_DOWN,
            JOYSTICK_ANALOG_STICK_RIGHT,
            JOYSTICK_ANALOG_STICK_LEFT,
        };

        for (auto primitive : primitives)
          driverMap[CPeripheralAddonTranslator::TranslatePrimitive(feature.Primitive(primitive))] =
              it.first;
        break;
      }

      case JOYSTICK_FEATURE_TYPE_ACCELEROMETER:
      {
        std::vector<JOYSTICK_FEATURE_PRIMITIVE> primitives = {
            JOYSTICK_ACCELEROMETER_POSITIVE_X,
            JOYSTICK_ACCELEROMETER_POSITIVE_Y,
            JOYSTICK_ACCELEROMETER_POSITIVE_Z,
        };

        for (auto primitive : primitives)
        {
          CDriverPrimitive translatedPrimitive =
              CPeripheralAddonTranslator::TranslatePrimitive(feature.Primitive(primitive));
          driverMap[translatedPrimitive] = it.first;

          // Map opposite semiaxis
          CDriverPrimitive oppositePrimitive = CDriverPrimitive(
              translatedPrimitive.Index(), 0, translatedPrimitive.SemiAxisDirection() * -1, 1);
          driverMap[oppositePrimitive] = it.first;
        }
        break;
      }

      case JOYSTICK_FEATURE_TYPE_WHEEL:
      {
        std::vector<JOYSTICK_FEATURE_PRIMITIVE> primitives = {
            JOYSTICK_WHEEL_LEFT,
            JOYSTICK_WHEEL_RIGHT,
        };

        for (auto primitive : primitives)
          driverMap[CPeripheralAddonTranslator::TranslatePrimitive(feature.Primitive(primitive))] =
              it.first;
        break;
      }

      case JOYSTICK_FEATURE_TYPE_THROTTLE:
      {
        std::vector<JOYSTICK_FEATURE_PRIMITIVE> primitives = {
            JOYSTICK_THROTTLE_UP,
            JOYSTICK_THROTTLE_DOWN,
        };

        for (auto primitive : primitives)
          driverMap[CPeripheralAddonTranslator::TranslatePrimitive(feature.Primitive(primitive))] =
              it.first;
        break;
      }

      case JOYSTICK_FEATURE_TYPE_RELPOINTER:
      {
        std::vector<JOYSTICK_FEATURE_PRIMITIVE> primitives = {
            JOYSTICK_RELPOINTER_UP,
            JOYSTICK_RELPOINTER_DOWN,
            JOYSTICK_RELPOINTER_RIGHT,
            JOYSTICK_RELPOINTER_LEFT,
        };

        for (auto primitive : primitives)
          driverMap[CPeripheralAddonTranslator::TranslatePrimitive(feature.Primitive(primitive))] =
              it.first;
        break;
      }

      default:
        break;
    }
  }

  return driverMap;
}

JOYSTICK_FEATURE_PRIMITIVE CAddonButtonMap::GetAnalogStickIndex(
    JOYSTICK::ANALOG_STICK_DIRECTION dir)
{
  using namespace JOYSTICK;

  switch (dir)
  {
    case ANALOG_STICK_DIRECTION::UP:
      return JOYSTICK_ANALOG_STICK_UP;
    case ANALOG_STICK_DIRECTION::DOWN:
      return JOYSTICK_ANALOG_STICK_DOWN;
    case ANALOG_STICK_DIRECTION::RIGHT:
      return JOYSTICK_ANALOG_STICK_RIGHT;
    case ANALOG_STICK_DIRECTION::LEFT:
      return JOYSTICK_ANALOG_STICK_LEFT;
    default:
      break;
  }

  return static_cast<JOYSTICK_FEATURE_PRIMITIVE>(0);
}

JOYSTICK_FEATURE_PRIMITIVE CAddonButtonMap::GetRelativePointerIndex(
    JOYSTICK::RELATIVE_POINTER_DIRECTION dir)
{
  using namespace JOYSTICK;

  switch (dir)
  {
    case RELATIVE_POINTER_DIRECTION::UP:
      return JOYSTICK_RELPOINTER_UP;
    case RELATIVE_POINTER_DIRECTION::DOWN:
      return JOYSTICK_RELPOINTER_DOWN;
    case RELATIVE_POINTER_DIRECTION::RIGHT:
      return JOYSTICK_RELPOINTER_RIGHT;
    case RELATIVE_POINTER_DIRECTION::LEFT:
      return JOYSTICK_RELPOINTER_LEFT;
    default:
      break;
  }

  return static_cast<JOYSTICK_FEATURE_PRIMITIVE>(0);
}

JOYSTICK_FEATURE_PRIMITIVE CAddonButtonMap::GetPrimitiveIndex(JOYSTICK::WHEEL_DIRECTION dir)
{
  using namespace JOYSTICK;

  switch (dir)
  {
    case WHEEL_DIRECTION::RIGHT:
      return JOYSTICK_WHEEL_RIGHT;
    case WHEEL_DIRECTION::LEFT:
      return JOYSTICK_WHEEL_LEFT;
    default:
      break;
  }

  return static_cast<JOYSTICK_FEATURE_PRIMITIVE>(0);
}

JOYSTICK_FEATURE_PRIMITIVE CAddonButtonMap::GetPrimitiveIndex(JOYSTICK::THROTTLE_DIRECTION dir)
{
  using namespace JOYSTICK;

  switch (dir)
  {
    case THROTTLE_DIRECTION::UP:
      return JOYSTICK_THROTTLE_UP;
    case THROTTLE_DIRECTION::DOWN:
      return JOYSTICK_THROTTLE_DOWN;
    default:
      break;
  }

  return static_cast<JOYSTICK_FEATURE_PRIMITIVE>(0);
}