| 1 |
/* |
| 2 |
* Copyright (C) 2018 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 |
|
| 21 |
#include "GameClientController.h" |
| 22 |
|
| 23 |
#include "GameClientInput.h" |
| 24 |
#include "games/controllers/Controller.h" |
| 25 |
#include "games/controllers/ControllerLayout.h" |
| 26 |
#include "games/controllers/input/PhysicalFeature.h" |
| 27 |
#include "games/controllers/input/PhysicalTopology.h" |
| 28 |
|
| 29 |
#include <algorithm> |
| 30 |
#include <vector> |
| 31 |
|
| 32 |
using namespace KODI; |
| 33 |
using namespace GAME; |
| 34 |
|
| 35 |
CGameClientController::CGameClientController(CGameClientInput& input, ControllerPtr controller) |
| 36 |
: m_input(input), m_controller(std::move(controller)), m_controllerId(m_controller->ID()) |
| 37 |
{ |
| 38 |
// Generate arrays of features |
| 39 |
for (const CPhysicalFeature& feature : m_controller->Features()) |
| 40 |
{ |
| 41 |
// Skip feature if not supported by the game client |
| 42 |
if (!m_input.HasFeature(m_controller->ID(), feature.Name())) |
| 43 |
continue; |
| 44 |
|
| 45 |
// Add feature to array of the appropriate type |
| 46 |
switch (feature.Type()) |
| 47 |
{ |
| 48 |
case FEATURE_TYPE::SCALAR: |
| 49 |
{ |
| 50 |
switch (feature.InputType()) |
| 51 |
{ |
| 52 |
case JOYSTICK::INPUT_TYPE::DIGITAL: |
| 53 |
m_digitalButtons.emplace_back(const_cast<char*>(feature.Name().c_str())); |
| 54 |
break; |
| 55 |
case JOYSTICK::INPUT_TYPE::ANALOG: |
| 56 |
m_analogButtons.emplace_back(const_cast<char*>(feature.Name().c_str())); |
| 57 |
break; |
| 58 |
default: |
| 59 |
break; |
| 60 |
} |
| 61 |
break; |
| 62 |
} |
| 63 |
case FEATURE_TYPE::ANALOG_STICK: |
| 64 |
m_analogSticks.emplace_back(const_cast<char*>(feature.Name().c_str())); |
| 65 |
break; |
| 66 |
case FEATURE_TYPE::ACCELEROMETER: |
| 67 |
m_accelerometers.emplace_back(const_cast<char*>(feature.Name().c_str())); |
| 68 |
break; |
| 69 |
case FEATURE_TYPE::KEY: |
| 70 |
m_keys.emplace_back(const_cast<char*>(feature.Name().c_str())); |
| 71 |
break; |
| 72 |
case FEATURE_TYPE::RELPOINTER: |
| 73 |
m_relPointers.emplace_back(const_cast<char*>(feature.Name().c_str())); |
| 74 |
break; |
| 75 |
case FEATURE_TYPE::ABSPOINTER: |
| 76 |
m_absPointers.emplace_back(const_cast<char*>(feature.Name().c_str())); |
| 77 |
break; |
| 78 |
case FEATURE_TYPE::MOTOR: |
| 79 |
m_motors.emplace_back(const_cast<char*>(feature.Name().c_str())); |
| 80 |
break; |
| 81 |
default: |
| 82 |
break; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
//! @todo Sort vectors |
| 87 |
} |
| 88 |
|
| 89 |
game_controller_layout CGameClientController::TranslateController() const |
| 90 |
{ |
| 91 |
game_controller_layout controllerStruct{}; |
| 92 |
|
| 93 |
controllerStruct.controller_id = const_cast<char*>(m_controllerId.c_str()); |
| 94 |
controllerStruct.provides_input = m_controller->Layout().Topology().ProvidesInput(); |
| 95 |
|
| 96 |
if (!m_digitalButtons.empty()) |
| 97 |
{ |
| 98 |
controllerStruct.digital_buttons = const_cast<char**>(m_digitalButtons.data()); |
| 99 |
controllerStruct.digital_button_count = static_cast<unsigned int>(m_digitalButtons.size()); |
| 100 |
} |
| 101 |
if (!m_analogButtons.empty()) |
| 102 |
{ |
| 103 |
controllerStruct.analog_buttons = const_cast<char**>(m_analogButtons.data()); |
| 104 |
controllerStruct.analog_button_count = static_cast<unsigned int>(m_analogButtons.size()); |
| 105 |
} |
| 106 |
if (!m_analogSticks.empty()) |
| 107 |
{ |
| 108 |
controllerStruct.analog_sticks = const_cast<char**>(m_analogSticks.data()); |
| 109 |
controllerStruct.analog_stick_count = static_cast<unsigned int>(m_analogSticks.size()); |
| 110 |
} |
| 111 |
if (!m_accelerometers.empty()) |
| 112 |
{ |
| 113 |
controllerStruct.accelerometers = const_cast<char**>(m_accelerometers.data()); |
| 114 |
controllerStruct.accelerometer_count = static_cast<unsigned int>(m_accelerometers.size()); |
| 115 |
} |
| 116 |
if (!m_keys.empty()) |
| 117 |
{ |
| 118 |
controllerStruct.keys = const_cast<char**>(m_keys.data()); |
| 119 |
controllerStruct.key_count = static_cast<unsigned int>(m_keys.size()); |
| 120 |
} |
| 121 |
if (!m_relPointers.empty()) |
| 122 |
{ |
| 123 |
controllerStruct.rel_pointers = const_cast<char**>(m_relPointers.data()); |
| 124 |
controllerStruct.rel_pointer_count = static_cast<unsigned int>(m_relPointers.size()); |
| 125 |
} |
| 126 |
if (!m_absPointers.empty()) |
| 127 |
{ |
| 128 |
controllerStruct.abs_pointers = const_cast<char**>(m_absPointers.data()); |
| 129 |
controllerStruct.abs_pointer_count = static_cast<unsigned int>(m_absPointers.size()); |
| 130 |
} |
| 131 |
if (!m_motors.empty()) |
| 132 |
{ |
| 133 |
controllerStruct.motors = const_cast<char**>(m_motors.data()); |
| 134 |
controllerStruct.motor_count = static_cast<unsigned int>(m_motors.size()); |
| 135 |
} |
| 136 |
|
| 137 |
return controllerStruct; |
| 138 |
} |