summaryrefslogtreecommitdiffstats
path: root/src/input_common/gcadapter/gc_poller.cpp
blob: 6d0c333eeff331195cd69f149e824343a5567a99 (plain) (blame)
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
// Copyright 2020 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <atomic>
#include <list>
#include <mutex>
#include <utility>
#include "common/assert.h"
#include "common/threadsafe_queue.h"
#include "input_common/gcadapter/gc_adapter.h"
#include "input_common/gcadapter/gc_poller.h"

namespace InputCommon {

class GCButton final : public Input::ButtonDevice {
public:
    explicit GCButton(u32 port_, s32 button_, const GCAdapter::Adapter* adapter)
        : port(port_), button(button_), gcadapter(adapter) {}

    ~GCButton() override;

    bool GetStatus() const override {
        if (gcadapter->DeviceConnected(port)) {
            return (gcadapter->GetPadState(port).buttons & button) != 0;
        }
        return false;
    }

private:
    const u32 port;
    const s32 button;
    const GCAdapter::Adapter* gcadapter;
};

class GCAxisButton final : public Input::ButtonDevice {
public:
    explicit GCAxisButton(u32 port_, u32 axis_, float threshold_, bool trigger_if_greater_,
                          const GCAdapter::Adapter* adapter)
        : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_),
          gcadapter(adapter) {}

    bool GetStatus() const override {
        if (gcadapter->DeviceConnected(port)) {
            const float current_axis_value = gcadapter->GetPadState(port).axis_values.at(axis);
            const float axis_value = current_axis_value / 128.0f;
            if (trigger_if_greater) {
                // TODO: Might be worthwile to set a slider for the trigger threshold. It is
                // currently always set to 0.5 in configure_input_player.cpp ZL/ZR HandleClick
                return axis_value > threshold;
            }
            return axis_value < -threshold;
        }
        return false;
    }

private:
    const u32 port;
    const u32 axis;
    float threshold;
    bool trigger_if_greater;
    const GCAdapter::Adapter* gcadapter;
};

GCButtonFactory::GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_)
    : adapter(std::move(adapter_)) {}

GCButton::~GCButton() = default;

std::unique_ptr<Input::ButtonDevice> GCButtonFactory::Create(const Common::ParamPackage& params) {
    const auto button_id = params.Get("button", 0);
    const auto port = static_cast<u32>(params.Get("port", 0));

    constexpr s32 PAD_STICK_ID = static_cast<s32>(GCAdapter::PadButton::Stick);

    // button is not an axis/stick button
    if (button_id != PAD_STICK_ID) {
        return std::make_unique<GCButton>(port, button_id, adapter.get());
    }

    // For Axis buttons, used by the binary sticks.
    if (button_id == PAD_STICK_ID) {
        const int axis = params.Get("axis", 0);
        const float threshold = params.Get("threshold", 0.25f);
        const std::string direction_name = params.Get("direction", "");
        bool trigger_if_greater;
        if (direction_name == "+") {
            trigger_if_greater = true;
        } else if (direction_name == "-") {
            trigger_if_greater = false;
        } else {
            trigger_if_greater = true;
            LOG_ERROR(Input, "Unknown direction {}", direction_name);
        }
        return std::make_unique<GCAxisButton>(port, axis, threshold, trigger_if_greater,
                                              adapter.get());
    }

    return nullptr;
}

Common::ParamPackage GCButtonFactory::GetNextInput() const {
    Common::ParamPackage params;
    GCAdapter::GCPadStatus pad;
    auto& queue = adapter->GetPadQueue();
    while (queue.Pop(pad)) {
        // This while loop will break on the earliest detected button
        params.Set("engine", "gcpad");
        params.Set("port", static_cast<s32>(pad.port));
        if (pad.button != GCAdapter::PadButton::Undefined) {
            params.Set("button", static_cast<u16>(pad.button));
        }

        // For Axis button implementation
        if (pad.axis != GCAdapter::PadAxes::Undefined) {
            params.Set("axis", static_cast<u8>(pad.axis));
            params.Set("button", static_cast<u16>(GCAdapter::PadButton::Stick));
            params.Set("threshold", "0.25");
            if (pad.axis_value > 0) {
                params.Set("direction", "+");
            } else {
                params.Set("direction", "-");
            }
            break;
        }
    }
    return params;
}

void GCButtonFactory::BeginConfiguration() {
    polling = true;
    adapter->BeginConfiguration();
}

void GCButtonFactory::EndConfiguration() {
    polling = false;
    adapter->EndConfiguration();
}

class GCAnalog final : public Input::AnalogDevice {
public:
    explicit GCAnalog(u32 port_, u32 axis_x_, u32 axis_y_, float deadzone_,
                      const GCAdapter::Adapter* adapter, float range_)
        : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter),
          range(range_) {}

    float GetAxis(u32 axis) const {
        if (gcadapter->DeviceConnected(port)) {
            std::lock_guard lock{mutex};
            const auto axis_value =
                static_cast<float>(gcadapter->GetPadState(port).axis_values.at(axis));
            return (axis_value) / (100.0f * range);
        }
        return 0.0f;
    }

    std::pair<float, float> GetAnalog(u32 analog_axis_x, u32 analog_axis_y) const {
        float x = GetAxis(analog_axis_x);
        float y = GetAxis(analog_axis_y);

        // Make sure the coordinates are in the unit circle,
        // otherwise normalize it.
        float r = x * x + y * y;
        if (r > 1.0f) {
            r = std::sqrt(r);
            x /= r;
            y /= r;
        }

        return {x, y};
    }

    std::tuple<float, float> GetStatus() const override {
        const auto [x, y] = GetAnalog(axis_x, axis_y);
        const float r = std::sqrt((x * x) + (y * y));
        if (r > deadzone) {
            return {x / r * (r - deadzone) / (1 - deadzone),
                    y / r * (r - deadzone) / (1 - deadzone)};
        }
        return {0.0f, 0.0f};
    }

    bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override {
        const auto [x, y] = GetStatus();
        const float directional_deadzone = 0.5f;
        switch (direction) {
        case Input::AnalogDirection::RIGHT:
            return x > directional_deadzone;
        case Input::AnalogDirection::LEFT:
            return x < -directional_deadzone;
        case Input::AnalogDirection::UP:
            return y > directional_deadzone;
        case Input::AnalogDirection::DOWN:
            return y < -directional_deadzone;
        }
        return false;
    }

private:
    const u32 port;
    const u32 axis_x;
    const u32 axis_y;
    const float deadzone;
    const GCAdapter::Adapter* gcadapter;
    const float range;
    mutable std::mutex mutex;
};

/// An analog device factory that creates analog devices from GC Adapter
GCAnalogFactory::GCAnalogFactory(std::shared_ptr<GCAdapter::Adapter> adapter_)
    : adapter(std::move(adapter_)) {}

/**
 * Creates analog device from joystick axes
 * @param params contains parameters for creating the device:
 *     - "port": the nth gcpad on the adapter
 *     - "axis_x": the index of the axis to be bind as x-axis
 *     - "axis_y": the index of the axis to be bind as y-axis
 */
std::unique_ptr<Input::AnalogDevice> GCAnalogFactory::Create(const Common::ParamPackage& params) {
    const auto port = static_cast<u32>(params.Get("port", 0));
    const auto axis_x = static_cast<u32>(params.Get("axis_x", 0));
    const auto axis_y = static_cast<u32>(params.Get("axis_y", 1));
    const auto deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f);
    const auto range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f);

    return std::make_unique<GCAnalog>(port, axis_x, axis_y, deadzone, adapter.get(), range);
}

void GCAnalogFactory::BeginConfiguration() {
    polling = true;
    adapter->BeginConfiguration();
}

void GCAnalogFactory::EndConfiguration() {
    polling = false;
    adapter->EndConfiguration();
}

Common::ParamPackage GCAnalogFactory::GetNextInput() {
    GCAdapter::GCPadStatus pad;
    Common::ParamPackage params;
    auto& queue = adapter->GetPadQueue();
    while (queue.Pop(pad)) {
        if (pad.button != GCAdapter::PadButton::Undefined) {
            params.Set("engine", "gcpad");
            params.Set("port", static_cast<s32>(pad.port));
            params.Set("button", static_cast<u16>(pad.button));
            return params;
        }
        if (pad.axis == GCAdapter::PadAxes::Undefined ||
            std::abs(static_cast<float>(pad.axis_value) / 128.0f) < 0.1f) {
            continue;
        }
        // An analog device needs two axes, so we need to store the axis for later and wait for
        // a second input event. The axes also must be from the same joystick.
        const u8 axis = static_cast<u8>(pad.axis);
        if (axis == 0 || axis == 1) {
            analog_x_axis = 0;
            analog_y_axis = 1;
            controller_number = static_cast<s32>(pad.port);
            break;
        }
        if (axis == 2 || axis == 3) {
            analog_x_axis = 2;
            analog_y_axis = 3;
            controller_number = static_cast<s32>(pad.port);
            break;
        }

        if (analog_x_axis == -1) {
            analog_x_axis = axis;
            controller_number = static_cast<s32>(pad.port);
        } else if (analog_y_axis == -1 && analog_x_axis != axis &&
                   controller_number == static_cast<s32>(pad.port)) {
            analog_y_axis = axis;
            break;
        }
    }
    if (analog_x_axis != -1 && analog_y_axis != -1) {
        params.Set("engine", "gcpad");
        params.Set("port", controller_number);
        params.Set("axis_x", analog_x_axis);
        params.Set("axis_y", analog_y_axis);
        analog_x_axis = -1;
        analog_y_axis = -1;
        controller_number = -1;
        return params;
    }
    return params;
}

class GCVibration final : public Input::VibrationDevice {
public:
    explicit GCVibration(u32 port_, GCAdapter::Adapter* adapter)
        : port(port_), gcadapter(adapter) {}

    u8 GetStatus() const override {
        return gcadapter->RumblePlay(port, 0);
    }

    bool SetRumblePlay(f32 amp_low, [[maybe_unused]] f32 freq_low, [[maybe_unused]] f32 amp_high,
                       f32 freq_high) const override {
        const auto mean_amplitude = (amp_low + amp_high) * 0.5f;
        const auto processed_amplitude =
            static_cast<u8>((mean_amplitude + std::pow(mean_amplitude, 0.3f)) * 0.5f * 0x8);

        return gcadapter->RumblePlay(port, processed_amplitude);
    }

private:
    const u32 port;
    GCAdapter::Adapter* gcadapter;
};

/// An vibration device factory that creates vibration devices from GC Adapter
GCVibrationFactory::GCVibrationFactory(std::shared_ptr<GCAdapter::Adapter> adapter_)
    : adapter(std::move(adapter_)) {}

/**
 * Creates a vibration device from a joystick
 * @param params contains parameters for creating the device:
 *     - "port": the nth gcpad on the adapter
 */
std::unique_ptr<Input::VibrationDevice> GCVibrationFactory::Create(
    const Common::ParamPackage& params) {
    const auto port = static_cast<u32>(params.Get("port", 0));

    return std::make_unique<GCVibration>(port, adapter.get());
}

} // namespace InputCommon