File: WPEGamepadManette.cpp

package info (click to toggle)
wpewebkit 2.50.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 438,332 kB
  • sloc: cpp: 3,777,828; javascript: 197,888; ansic: 156,951; python: 49,119; asm: 21,987; ruby: 18,540; perl: 16,722; xml: 4,623; yacc: 2,360; sh: 2,096; java: 2,019; lex: 1,327; pascal: 366; makefile: 90
file content (190 lines) | stat: -rw-r--r-- 7,088 bytes parent folder | download
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
/*
 * Copyright (C) 2025 Igalia S.L.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "WPEGamepadManette.h"

#include <linux/input-event-codes.h>
#include <optional>
#include <wtf/glib/GRefPtr.h>
#include <wtf/glib/WTFGType.h>

static constexpr size_t wpeGamepadAxisCount = static_cast<size_t>(WPE_GAMEPAD_AXIS_RIGHT_Y) + 1;
static constexpr size_t wpeGamepadButtonCount = static_cast<size_t>(WPE_GAMEPAD_BUTTON_CENTER_CLUSTER_CENTER) + 1;

struct _WPEGamepadManettePrivate {
    GRefPtr<ManetteDevice> device;
};

WEBKIT_DEFINE_FINAL_TYPE(WPEGamepadManette, wpe_gamepad_manette, WPE_TYPE_GAMEPAD, WPEGamepad)

enum {
    PROP_0,

    PROP_DEVICE,

    N_PROPERTIES
};

static std::array<GParamSpec*, N_PROPERTIES> sObjProperties;

static void wpeGamepadManetteDispose(GObject* object)
{
    auto* priv = WPE_GAMEPAD_MANETTE(object)->priv;
    priv->device = nullptr;

    G_OBJECT_CLASS(wpe_gamepad_manette_parent_class)->dispose(object);
}

static void wpeGamepadManetteSetProperty(GObject* object, guint propId, const GValue* value, GParamSpec* paramSpec)
{
    auto* gamepad = WPE_GAMEPAD_MANETTE(object);

    switch (propId) {
    case PROP_DEVICE:
        gamepad->priv->device = MANETTE_DEVICE(g_value_get_object(value));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
    }
}

static WPEGamepadButton wpeGamepadButton(uint16_t button)
{
    switch (button) {
    case BTN_SOUTH:
        return WPE_GAMEPAD_BUTTON_RIGHT_CLUSTER_BOTTOM;
    case BTN_EAST:
        return WPE_GAMEPAD_BUTTON_RIGHT_CLUSTER_RIGHT;
    case BTN_NORTH:
        return WPE_GAMEPAD_BUTTON_RIGHT_CLUSTER_TOP;
    case BTN_WEST:
        return WPE_GAMEPAD_BUTTON_RIGHT_CLUSTER_LEFT;
    case BTN_TL:
        return WPE_GAMEPAD_BUTTON_LEFT_SHOULDER_FRONT;
    case BTN_TR:
        return WPE_GAMEPAD_BUTTON_RIGHT_SHOULDER_FRONT;
    case BTN_TL2:
        return WPE_GAMEPAD_BUTTON_LEFT_SHOULDER_BACK;
    case BTN_TR2:
        return WPE_GAMEPAD_BUTTON_RIGHT_SHOULDER_BACK;
    case BTN_SELECT:
        return WPE_GAMEPAD_BUTTON_CENTER_CLUSTER_LEFT;
    case BTN_START:
        return WPE_GAMEPAD_BUTTON_CENTER_CLUSTER_RIGHT;
    case BTN_THUMBL:
        return WPE_GAMEPAD_BUTTON_LEFT_THUMB;
    case BTN_THUMBR:
        return WPE_GAMEPAD_BUTTON_RIGHT_THUMB;
    case BTN_DPAD_UP:
        return WPE_GAMEPAD_BUTTON_LEFT_CLUSTER_TOP;
    case BTN_DPAD_DOWN:
        return WPE_GAMEPAD_BUTTON_LEFT_CLUSTER_BOTTOM;
    case BTN_DPAD_LEFT:
        return WPE_GAMEPAD_BUTTON_LEFT_CLUSTER_LEFT;
    case BTN_DPAD_RIGHT:
        return WPE_GAMEPAD_BUTTON_LEFT_CLUSTER_RIGHT;
    case BTN_MODE:
        return WPE_GAMEPAD_BUTTON_CENTER_CLUSTER_CENTER;
    default:
        break;
    }
    // In case could not map button to a WPE_GAMEPAD_BUTTON value, return
    // a valid WPE_GAMEPAD_BUTTON value anyway.
    return static_cast<WPEGamepadButton>(button % wpeGamepadButtonCount);
}

static WPEGamepadAxis wpeGamepadAxis(uint16_t axis)
{
    switch (axis) {
    case ABS_X:
        return WPE_GAMEPAD_AXIS_LEFT_X;
    case ABS_Y:
        return WPE_GAMEPAD_AXIS_LEFT_Y;
    case ABS_RX:
        return WPE_GAMEPAD_AXIS_RIGHT_X;
    case ABS_RY:
        return WPE_GAMEPAD_AXIS_RIGHT_Y;
    }
    // In case could not map axis to a WPE_GAMEPAD_AXIS value, return
    // a valid WPE_GAMEPAD_AXIS value anyway.
    return static_cast<WPEGamepadAxis>(axis % wpeGamepadAxisCount);
}

static void wpeGamepadManetteStartInputMonitor(WPEGamepad* gamepad)
{
    auto* priv = WPE_GAMEPAD_MANETTE(gamepad)->priv;

    g_signal_connect_object(priv->device.get(), "button-press-event", G_CALLBACK(+[](WPEGamepad* gamepad, ManetteEvent* event) {
        uint16_t button;
        if (!manette_event_get_button(event, &button))
            return;
        wpe_gamepad_button_event(gamepad, wpeGamepadButton(button), TRUE);
    }), gamepad, G_CONNECT_SWAPPED);
    g_signal_connect_object(priv->device.get(), "button-release-event", G_CALLBACK(+[](WPEGamepad* gamepad, ManetteEvent* event) {
        uint16_t button;
        if (!manette_event_get_button(event, &button))
            return;
        wpe_gamepad_button_event(gamepad, wpeGamepadButton(button), FALSE);
    }), gamepad, G_CONNECT_SWAPPED);
    g_signal_connect_object(priv->device.get(), "absolute-axis-event", G_CALLBACK(+[](WPEGamepad* gamepad, ManetteEvent* event) {
        uint16_t axis;
        double value;
        if (!manette_event_get_absolute(event, &axis, &value))
            return;
        wpe_gamepad_axis_event(gamepad, wpeGamepadAxis(axis), value);
    }), gamepad, G_CONNECT_SWAPPED);
}

static void wpeGamepadManetteStopInputMonitor(WPEGamepad* gamepad)
{
    auto* priv = WPE_GAMEPAD_MANETTE(gamepad)->priv;
    g_signal_handlers_disconnect_by_data(priv->device.get(), gamepad);
}

static void wpe_gamepad_manette_class_init(WPEGamepadManetteClass* gamepadManetteClass)
{
    GObjectClass* objectClass = G_OBJECT_CLASS(gamepadManetteClass);
    objectClass->set_property = wpeGamepadManetteSetProperty;
    objectClass->dispose = wpeGamepadManetteDispose;

    WPEGamepadClass* gamepadClass = WPE_GAMEPAD_CLASS(gamepadManetteClass);
    gamepadClass->start_input_monitor = wpeGamepadManetteStartInputMonitor;
    gamepadClass->stop_input_monitor = wpeGamepadManetteStopInputMonitor;

    sObjProperties[PROP_DEVICE] =
        g_param_spec_object(
            "device",
            nullptr, nullptr,
            MANETTE_TYPE_DEVICE,
            static_cast<GParamFlags>(G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));

    g_object_class_install_properties(objectClass, N_PROPERTIES, sObjProperties.data());
}

WPEGamepad* wpeGamepadManetteCreate(ManetteDevice* device)
{
    return WPE_GAMEPAD(g_object_new(WPE_TYPE_GAMEPAD_MANETTE, "name", manette_device_get_name(device), "device", device, nullptr));
}