summaryrefslogtreecommitdiffstats
path: root/xbmc/input/joysticks/DriverPrimitive.cpp
blob: 9540dfd2ede4f6844296caa0981b1c17de329de9 (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
/*
 *  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 "DriverPrimitive.h"

#include <utility>

using namespace KODI;
using namespace JOYSTICK;

CDriverPrimitive::CDriverPrimitive(void) = default;

CDriverPrimitive::CDriverPrimitive(PRIMITIVE_TYPE type, unsigned int index)
  : m_type(type), m_driverIndex(index)
{
}

CDriverPrimitive::CDriverPrimitive(unsigned int hatIndex, HAT_DIRECTION direction)
  : m_type(PRIMITIVE_TYPE::HAT), m_driverIndex(hatIndex), m_hatDirection(direction)
{
}

CDriverPrimitive::CDriverPrimitive(unsigned int axisIndex,
                                   int center,
                                   SEMIAXIS_DIRECTION direction,
                                   unsigned int range)
  : m_type(PRIMITIVE_TYPE::SEMIAXIS),
    m_driverIndex(axisIndex),
    m_center(center),
    m_semiAxisDirection(direction),
    m_range(range)
{
}

CDriverPrimitive::CDriverPrimitive(XBMCKey keycode)
  : m_type(PRIMITIVE_TYPE::KEY), m_keycode(keycode)
{
}

CDriverPrimitive::CDriverPrimitive(MOUSE::BUTTON_ID index)
  : m_type(PRIMITIVE_TYPE::MOUSE_BUTTON), m_driverIndex(static_cast<unsigned int>(index))
{
}

CDriverPrimitive::CDriverPrimitive(RELATIVE_POINTER_DIRECTION direction)
  : m_type(PRIMITIVE_TYPE::RELATIVE_POINTER), m_pointerDirection(direction)
{
}

bool CDriverPrimitive::operator==(const CDriverPrimitive& rhs) const
{
  if (m_type == rhs.m_type)
  {
    switch (m_type)
    {
      case PRIMITIVE_TYPE::BUTTON:
      case PRIMITIVE_TYPE::MOTOR:
      case PRIMITIVE_TYPE::MOUSE_BUTTON:
        return m_driverIndex == rhs.m_driverIndex;
      case PRIMITIVE_TYPE::HAT:
        return m_driverIndex == rhs.m_driverIndex && m_hatDirection == rhs.m_hatDirection;
      case PRIMITIVE_TYPE::SEMIAXIS:
        return m_driverIndex == rhs.m_driverIndex && m_center == rhs.m_center &&
               m_semiAxisDirection == rhs.m_semiAxisDirection && m_range == rhs.m_range;
      case PRIMITIVE_TYPE::KEY:
        return m_keycode == rhs.m_keycode;
      case PRIMITIVE_TYPE::RELATIVE_POINTER:
        return m_pointerDirection == rhs.m_pointerDirection;
      default:
        return true;
    }
  }
  return false;
}

bool CDriverPrimitive::operator<(const CDriverPrimitive& rhs) const
{
  if (m_type < rhs.m_type)
    return true;
  if (m_type > rhs.m_type)
    return false;

  if (m_type == PRIMITIVE_TYPE::BUTTON || m_type == PRIMITIVE_TYPE::HAT ||
      m_type == PRIMITIVE_TYPE::SEMIAXIS || m_type == PRIMITIVE_TYPE::MOTOR ||
      m_type == PRIMITIVE_TYPE::MOUSE_BUTTON)
  {
    if (m_driverIndex < rhs.m_driverIndex)
      return true;
    if (m_driverIndex > rhs.m_driverIndex)
      return false;
  }

  if (m_type == PRIMITIVE_TYPE::HAT)
  {
    if (m_hatDirection < rhs.m_hatDirection)
      return true;
    if (m_hatDirection > rhs.m_hatDirection)
      return false;
  }

  if (m_type == PRIMITIVE_TYPE::SEMIAXIS)
  {
    if (m_center < rhs.m_center)
      return true;
    if (m_center > rhs.m_center)
      return false;

    if (m_semiAxisDirection < rhs.m_semiAxisDirection)
      return true;
    if (m_semiAxisDirection > rhs.m_semiAxisDirection)
      return false;

    if (m_range < rhs.m_range)
      return true;
    if (m_range > rhs.m_range)
      return false;
  }

  if (m_type == PRIMITIVE_TYPE::KEY)
  {
    if (m_keycode < rhs.m_keycode)
      return true;
    if (m_keycode > rhs.m_keycode)
      return false;
  }

  if (m_type == PRIMITIVE_TYPE::RELATIVE_POINTER)
  {
    if (m_pointerDirection < rhs.m_pointerDirection)
      return true;
    if (m_pointerDirection > rhs.m_pointerDirection)
      return false;
  }

  return false;
}

bool CDriverPrimitive::IsValid(void) const
{
  if (m_type == PRIMITIVE_TYPE::BUTTON || m_type == PRIMITIVE_TYPE::MOTOR ||
      m_type == PRIMITIVE_TYPE::MOUSE_BUTTON)
    return true;

  if (m_type == PRIMITIVE_TYPE::HAT)
  {
    return m_hatDirection == HAT_DIRECTION::UP || m_hatDirection == HAT_DIRECTION::DOWN ||
           m_hatDirection == HAT_DIRECTION::RIGHT || m_hatDirection == HAT_DIRECTION::LEFT;
  }

  if (m_type == PRIMITIVE_TYPE::SEMIAXIS)
  {
    unsigned int maxRange = 1;

    switch (m_center)
    {
      case -1:
      {
        if (m_semiAxisDirection != SEMIAXIS_DIRECTION::POSITIVE)
          return false;
        maxRange = 2;
        break;
      }
      case 0:
      {
        if (m_semiAxisDirection != SEMIAXIS_DIRECTION::POSITIVE &&
            m_semiAxisDirection != SEMIAXIS_DIRECTION::NEGATIVE)
          return false;
        break;
      }
      case 1:
      {
        if (m_semiAxisDirection != SEMIAXIS_DIRECTION::POSITIVE)
          return false;
        maxRange = 2;
        break;
      }
      default:
        break;
    }

    return 1 <= m_range && m_range <= maxRange;
  }

  if (m_type == PRIMITIVE_TYPE::KEY)
    return m_keycode != XBMCK_UNKNOWN;

  if (m_type == PRIMITIVE_TYPE::RELATIVE_POINTER)
  {
    return m_pointerDirection == RELATIVE_POINTER_DIRECTION::UP ||
           m_pointerDirection == RELATIVE_POINTER_DIRECTION::DOWN ||
           m_pointerDirection == RELATIVE_POINTER_DIRECTION::RIGHT ||
           m_pointerDirection == RELATIVE_POINTER_DIRECTION::LEFT;
  }

  return false;
}