File size: 12,338 Bytes
18a519f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#if UNITY_EDITOR || UNITY_STANDALONE_LINUX
using System;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.InputSystem.Utilities;
using System.Text;
using UnityEngine.InputSystem.Layouts;

namespace UnityEngine.InputSystem.Linux
{
    [Serializable]
    internal class SDLLayoutBuilder
    {
        [SerializeField] private string m_ParentLayout;
        [SerializeField] private SDLDeviceDescriptor m_Descriptor;

        internal static string OnFindLayoutForDevice(ref InputDeviceDescription description, string matchedLayout,
            InputDeviceExecuteCommandDelegate executeCommandDelegate)
        {
            if (description.interfaceName != LinuxSupport.kInterfaceName)
                return null;

            if (string.IsNullOrEmpty(description.capabilities))
                return null;

            // Try to parse the SDL descriptor.
            SDLDeviceDescriptor deviceDescriptor;
            try
            {
                deviceDescriptor = SDLDeviceDescriptor.FromJson(description.capabilities);
            }
            catch (Exception exception)
            {
                Debug.LogError($"{exception} while trying to parse descriptor for SDL device: {description.capabilities}");
                return null;
            }

            if (deviceDescriptor == null)
                return null;

            string layoutName;
            if (string.IsNullOrEmpty(description.manufacturer))
            {
                layoutName = $"{SanitizeName(description.interfaceName)}::{SanitizeName(description.product)}";
            }
            else
            {
                layoutName =
                    $"{SanitizeName(description.interfaceName)}::{SanitizeName(description.manufacturer)}::{SanitizeName(description.product)}";
            }

            var layout = new SDLLayoutBuilder { m_Descriptor = deviceDescriptor, m_ParentLayout = matchedLayout };
            InputSystem.RegisterLayoutBuilder(() => layout.Build(), layoutName, matchedLayout);

            return layoutName;
        }

        private static string SanitizeName(string originalName)
        {
            var stringLength = originalName.Length;
            var sanitizedName = new StringBuilder(stringLength);
            for (var i = 0; i < stringLength; i++)
            {
                var letter = originalName[i];
                if (char.IsUpper(letter) || char.IsLower(letter) || char.IsDigit(letter))
                    sanitizedName.Append(letter);
            }
            return sanitizedName.ToString();
        }

        private static bool IsAxis(SDLFeatureDescriptor feature, SDLAxisUsage axis)
        {
            return feature.featureType == JoystickFeatureType.Axis
                && feature.usageHint == (int)axis;
        }

        private static void BuildStickFeature(ref InputControlLayout.Builder builder, SDLFeatureDescriptor xFeature, SDLFeatureDescriptor yFeature)
        {
            int byteOffset;
            if (xFeature.offset <= yFeature.offset)
                byteOffset = xFeature.offset;
            else
                byteOffset = yFeature.offset;

            const string stickName = "Stick";
            builder.AddControl(stickName)
                .WithLayout("Stick")
                .WithByteOffset((uint)byteOffset)
                .WithSizeInBits((uint)xFeature.featureSize * 8 + (uint)yFeature.featureSize * 8)
                .WithUsages(CommonUsages.Primary2DMotion);

            builder.AddControl(stickName + "/x")
                .WithFormat(InputStateBlock.FormatInt)
                .WithByteOffset(0)
                .WithSizeInBits((uint)xFeature.featureSize * 8)
                .WithParameters("clamp=1,clampMin=-1,clampMax=1,scale,scaleFactor=65538");

            builder.AddControl(stickName + "/y")
                .WithFormat(InputStateBlock.FormatInt)
                .WithByteOffset(4)
                .WithSizeInBits((uint)xFeature.featureSize * 8)
                .WithParameters("clamp=1,clampMin=-1,clampMax=1,scale,scaleFactor=65538,invert");

            builder.AddControl(stickName + "/up")
                .WithParameters("clamp=1,clampMin=-1,clampMax=0,scale,scaleFactor=65538,invert");

            builder.AddControl(stickName + "/down")
                .WithParameters("clamp=1,clampMin=0,clampMax=1,scale,scaleFactor=65538,invert=false");

            builder.AddControl(stickName + "/left")
                .WithParameters("clamp=1,clampMin=-1,clampMax=0,scale,scaleFactor=65538,invert");

            builder.AddControl(stickName + "/right")
                .WithParameters("clamp=1,clampMin=0,clampMax=1,scale,scaleFactor=65538");
        }

        private static bool IsHatX(SDLFeatureDescriptor feature)
        {
            return feature.featureType == JoystickFeatureType.Hat
                && (feature.usageHint == (int)SDLAxisUsage.Hat0X
                    ||  feature.usageHint == (int)SDLAxisUsage.Hat1X
                    ||  feature.usageHint == (int)SDLAxisUsage.Hat2X
                    ||  feature.usageHint == (int)SDLAxisUsage.Hat3X);
        }

        private static bool IsHatY(SDLFeatureDescriptor feature)
        {
            return feature.featureType == JoystickFeatureType.Hat
                && (feature.usageHint == (int)SDLAxisUsage.Hat0Y
                    ||  feature.usageHint == (int)SDLAxisUsage.Hat1Y
                    ||  feature.usageHint == (int)SDLAxisUsage.Hat2Y
                    ||  feature.usageHint == (int)SDLAxisUsage.Hat3Y);
        }

        private static int HatNumber(SDLFeatureDescriptor feature)
        {
            Debug.Assert(feature.featureType == JoystickFeatureType.Hat);
            return 1 + (feature.usageHint - (int)SDLAxisUsage.Hat0X) / 2;
        }

        private static void BuildHatFeature(ref InputControlLayout.Builder builder, SDLFeatureDescriptor xFeature, SDLFeatureDescriptor yFeature)
        {
            Debug.Assert(xFeature.offset < yFeature.offset, "Order of features must be X followed by Y");

            var hat = HatNumber(xFeature);
            var hatName = hat > 1 ? $"Hat{hat}" : "Hat";

            builder.AddControl(hatName)
                .WithLayout("Dpad")
                .WithByteOffset((uint)xFeature.offset)
                .WithSizeInBits((uint)xFeature.featureSize * 8 + (uint)yFeature.featureSize * 8)
                .WithUsages(CommonUsages.Hatswitch);

            builder.AddControl(hatName + "/up")
                .WithFormat(InputStateBlock.FormatInt)
                .WithParameters("scale,scaleFactor=2147483647,clamp,clampMin=-1,clampMax=0,invert")
                .WithByteOffset(4)
                .WithBitOffset(0)
                .WithSizeInBits((uint)yFeature.featureSize * 8);

            builder.AddControl(hatName + "/down")
                .WithFormat(InputStateBlock.FormatInt)
                .WithParameters("scale,scaleFactor=2147483647,clamp,clampMin=0,clampMax=1")
                .WithByteOffset(4)
                .WithBitOffset(0)
                .WithSizeInBits((uint)yFeature.featureSize * 8);

            builder.AddControl(hatName + "/left")
                .WithFormat(InputStateBlock.FormatInt)
                .WithParameters("scale,scaleFactor=2147483647,clamp,clampMin=-1,clampMax=0,invert")
                .WithByteOffset(0)
                .WithBitOffset(0)
                .WithSizeInBits((uint)xFeature.featureSize * 8);

            builder.AddControl(hatName + "/right")
                .WithFormat(InputStateBlock.FormatInt)
                .WithParameters("scale,scaleFactor=2147483647,clamp,clampMin=0,clampMax=1")
                .WithByteOffset(0)
                .WithBitOffset(0)
                .WithSizeInBits((uint)xFeature.featureSize * 8);
        }

        internal InputControlLayout Build()
        {
            var builder = new InputControlLayout.Builder
            {
                stateFormat = new FourCC('L', 'J', 'O', 'Y'),
                extendsLayout = m_ParentLayout
            };

            for (var i = 0; i < m_Descriptor.controls.LengthSafe(); i++)
            {
                var feature = m_Descriptor.controls[i];
                switch (feature.featureType)
                {
                    case JoystickFeatureType.Axis:
                    {
                        var usage = (SDLAxisUsage)feature.usageHint;
                        var featureName = LinuxSupport.GetAxisNameFromUsage(usage);
                        var parameters = "scale,scaleFactor=65538,clamp=1,clampMin=-1,clampMax=1";

                        // If X is followed by Y, build a stick out of the two.
                        if (IsAxis(feature, SDLAxisUsage.X) && i + 1 < m_Descriptor.controls.Length)
                        {
                            var nextFeature = m_Descriptor.controls[i + 1];
                            if (IsAxis(nextFeature, SDLAxisUsage.Y))
                            {
                                BuildStickFeature(ref builder, feature, nextFeature);
                                ++i;
                                continue;
                            }
                        }

                        if (IsAxis(feature, SDLAxisUsage.Y))
                            parameters += ",invert";

                        var control = builder.AddControl(featureName)
                            .WithLayout("Analog")
                            .WithByteOffset((uint)feature.offset)
                            .WithFormat(InputStateBlock.FormatInt)
                            .WithParameters(parameters);

                        if (IsAxis(feature, SDLAxisUsage.RotateZ))
                            control.WithUsages(CommonUsages.Twist);
                        break;
                    }

                    case JoystickFeatureType.Ball:
                    {
                        //TODO
                        break;
                    }

                    case JoystickFeatureType.Button:
                    {
                        var usage = (SDLButtonUsage)feature.usageHint;
                        var featureName = LinuxSupport.GetButtonNameFromUsage(usage);
                        if (featureName != null)
                        {
                            builder.AddControl(featureName)
                                .WithLayout("Button")
                                .WithByteOffset((uint)feature.offset)
                                .WithBitOffset((uint)feature.bit)
                                .WithFormat(InputStateBlock.FormatBit);
                        }
                        break;
                    }

                    case JoystickFeatureType.Hat:
                    {
                        var usage = (SDLAxisUsage)feature.usageHint;
                        var featureName = LinuxSupport.GetAxisNameFromUsage(usage);
                        var parameters = "scale,scaleFactor=2147483647,clamp=1,clampMin=-1,clampMax=1";

                        if (i + 1 < m_Descriptor.controls.Length)
                        {
                            var nextFeature = m_Descriptor.controls[i + 1];
                            if (IsHatY(nextFeature) && HatNumber(feature) == HatNumber(nextFeature))
                            {
                                BuildHatFeature(ref builder, feature, nextFeature);
                                ++i;
                                continue;
                            }
                        }

                        if (IsHatY(feature))
                            parameters += ",invert";

                        builder.AddControl(featureName)
                            .WithLayout("Analog")
                            .WithByteOffset((uint)feature.offset)
                            .WithFormat(InputStateBlock.FormatInt)
                            .WithParameters(parameters);
                        break;
                    }

                    default:
                    {
                        throw new NotImplementedException(
                            $"SDLLayoutBuilder.Build: Trying to build an SDL device with an unknown feature of type {feature.featureType}.");
                    }
                }
            }

            return builder.Build();
        }
    }
}
#endif // UNITY_EDITOR || UNITY_STANDALONE_LINUX