ApolloInput Nova 0.2.0
Standalone virtual gamepad library
Loading...
Searching...
No Matches
backend.cpp
Go to the documentation of this file.
1
6
8
9#include <algorithm>
10#include <array>
11#include <cctype>
12#include <string_view>
13#include <utility>
14#include <vector>
15
16namespace apollo::input {
17 namespace {
18 struct backend_name_t {
19 BackendKind kind;
20 std::string_view name;
21 };
22
23 constexpr std::array BACKEND_NAMES {
24 backend_name_t {BackendKind::automatic, "automatic"},
25 backend_name_t {BackendKind::vigem, "vigem"},
26 backend_name_t {BackendKind::uinput, "uinput"},
27 backend_name_t {BackendKind::null_backend, "null"},
28 };
29
30 struct backend_alias_t {
31 std::string_view alias;
32 BackendKind kind;
33 };
34
35 constexpr std::array BACKEND_ALIASES {
36 backend_alias_t {"automatic", BackendKind::automatic},
37 backend_alias_t {"auto", BackendKind::automatic},
38 backend_alias_t {"vigem", BackendKind::vigem},
39 backend_alias_t {"vigembus", BackendKind::vigem},
40 backend_alias_t {"viGEmBus", BackendKind::vigem},
41 backend_alias_t {"uinput", BackendKind::uinput},
42 backend_alias_t {"linux_uinput", BackendKind::uinput},
43 backend_alias_t {"null", BackendKind::null_backend},
44 backend_alias_t {"none", BackendKind::null_backend},
45 };
46
54 bool equals_ignore_case(std::string_view left, std::string_view right) {
55 return left.size() == right.size() && std::equal(left.begin(), left.end(), right.begin(), [](char lhs, char rhs) {
56 return std::tolower(static_cast<unsigned char>(lhs)) == std::tolower(static_cast<unsigned char>(rhs));
57 });
58 }
59 } // namespace
60
61 const char *to_string(BackendKind kind) {
62 for (const auto &entry : BACKEND_NAMES) {
63 if (entry.kind == kind) {
64 return entry.name.data();
65 }
66 }
67 return "unknown";
68 }
69
70 bool backend_from_string(const std::string &name, BackendKind &kind) {
71 for (const auto &entry : BACKEND_ALIASES) {
72 if (equals_ignore_case(entry.alias, name)) {
73 kind = entry.kind;
74 return true;
75 }
76 }
77 return false;
78 }
79
80 std::unique_ptr<Device> Backend::create_compatible(Profile profile, const std::string &name, std::string *error) {
81 std::string last_error;
82 for (const auto candidate : profile_compatibility_chain(profile)) {
83 std::string candidate_error;
84 if (auto device = create(candidate, name, &candidate_error)) {
85 if (error) {
86 error->clear();
87 }
88 return device;
89 }
90 if (!candidate_error.empty()) {
91 last_error = std::move(candidate_error);
92 }
93 }
94
95 if (error) {
96 *error = last_error.empty() ? std::string {"No compatible gamepad profile is available"} : std::move(last_error);
97 }
98 return nullptr;
99 }
100
101 std::unique_ptr<Backend> create_backend(BackendKind kind) {
102 if (kind == BackendKind::null_backend) {
104 }
105
106#if defined(_WIN32) && defined(APOLLO_INPUT_WITH_VIGEM)
107 if (kind == BackendKind::automatic || kind == BackendKind::vigem) {
109 }
110#elif defined(__linux__)
111 if (kind == BackendKind::automatic || kind == BackendKind::uinput) {
112 return detail::create_uinput_backend();
113 }
114#endif
115
117 }
118} // namespace apollo::input
Backend and device interfaces for the Apollo input library.
Internal backend factory declarations.
virtual std::unique_ptr< Device > create(Profile profile, const std::string &name, std::string *error=nullptr)=0
Create a virtual device.
std::unique_ptr< Device > create_compatible(Profile profile, const std::string &name, std::string *error=nullptr)
Create a device, falling back to a simpler compatible profile.
Definition backend.cpp:80
virtual const char * name() const =0
Stable backend name.
std::unique_ptr< Backend > create_vigem_backend()
std::unique_ptr< Backend > create_null_backend()
Create the null backend.
bool backend_from_string(const std::string &name, BackendKind &kind)
Parse a backend kind from its short name or a compatible alias.
Definition backend.cpp:70
BackendKind
Kind of backend selected by the caller.
Definition backend.hpp:19
@ vigem
ViGEmBus on Windows.
Definition backend.hpp:21
@ null_backend
No-op backend useful for tests and previews.
Definition backend.hpp:23
@ automatic
Best backend for the current platform.
Definition backend.hpp:20
@ uinput
Linux uinput.
Definition backend.hpp:22
Profile
Virtual controller profile exposed by a backend.
Definition gamepad.hpp:16
const char * to_string(BackendKind kind)
Convert a backend kind to a stable short name.
Definition backend.cpp:61
std::vector< Profile > profile_compatibility_chain(Profile profile)
Return a profile followed by progressively simpler alternatives.
Definition gamepad.cpp:337
std::unique_ptr< Backend > create_backend(BackendKind kind=BackendKind::automatic)
Create a backend for the current platform.
Definition backend.cpp:101