| 1 | + | using System; |
1 | 2 | | using System.Diagnostics; |
2 | 3 | | using System.Reactive; |
3 | 4 | | using System.Threading; |
4 | 5 | | using Avalonia.Threading; |
5 | 6 | | using DoUrVerse.Views; |
| 7 | + | using DoUrVerseLib; |
6 | 8 | | using DoUrVerseLib.Device; |
7 | 9 | | using DoUrVerseLib.Platform; |
| 10 | + | using DoUrVerseLib.SCXML; |
| 11 | + | using Microsoft.CodeAnalysis.CSharp.Syntax; |
8 | 12 | | using ReactiveUI; |
9 | 13 | | namespace DoUrVerse.ViewModels; |
10 | 14 | | |
11 | 15 | | public class DebugViewModel : ViewModelBase |
12 | 16 | | { |
| 17 | + | const float axisThreshold = 0.25f; |
| 18 | + | const float axisHighBound = 32767 * (1 + axisThreshold); |
| 19 | + | const float axisLowBound = 32767 * (1 - axisThreshold); |
13 | 20 | | string logline = ""; |
14 | 21 | | string prevlogline = ""; |
15 | | - | public ReactiveCommand<Unit, Unit> ListenCommand {get;set;} |
| 22 | + | public ReactiveCommand<Unit, Unit> ListenCommand {get;set; } |
| 23 | + | CancellationTokenSource? cancellationTokenSource = new(); |
16 | 24 | | |
17 | 25 | | string textlog = ""; |
18 | 26 | | public string TextLog |
| skipped 8 lines |
27 | 35 | | set => this.RaiseAndSetIfChanged(ref isListening, value, nameof(IsListening)); |
28 | 36 | | } |
29 | 37 | | |
30 | | - | public DebugViewModel(IPlatform platform) : base(platform) |
31 | | - | { |
32 | | - | ListenCommand = ReactiveCommand.Create(listen); |
33 | | - | } |
34 | 38 | | public DebugViewModel() : base(MainWindow.GetPlatform()) |
35 | 39 | | { |
36 | 40 | | ListenCommand = ReactiveCommand.Create(listen); |
37 | 41 | | |
38 | | - | thread = new Thread(runner); |
39 | | - | thread.Start(); |
| 42 | + | thread = new Thread(runner); |
| 43 | + | thread.Start(); |
40 | 44 | | } |
41 | 45 | | Thread thread; |
42 | 46 | | void runner() |
| skipped 44 lines |
87 | 91 | | |
88 | 92 | | void inputRecieved(object sender, DeviceInputEventArgs eventArgs) |
89 | 93 | | { |
90 | | - | |
91 | 94 | | string name = eventArgs.Source?.Name ?? "Unknown"; |
92 | 95 | | string type = eventArgs.Source?.Type.ToString() ?? "Unknown"; |
93 | 96 | | |
| skipped 4 lines |
98 | 101 | | } |
99 | 102 | | else if (eventArgs.Type == DeviceInputEventType.Axis) |
100 | 103 | | { |
101 | | - | stroke = (string) eventArgs.Axis.ToString(); |
| 104 | + | dev = (IDevice)sender; |
102 | 105 | | } |
103 | 106 | | else |
104 | 107 | | { |
105 | | - | Debug.WriteLine("wtf"); |
| 108 | + | return; |
106 | 109 | | } |
107 | 110 | | |
108 | | - | int instanceNumber = eventArgs.DeviceInstance; |
109 | | - | logline = string.Format("{0} {1}: {2}", type, instanceNumber, stroke); |
| 111 | + | logline = $"[{dev.Name}] detected unknown input: {args.Description}"; |
110 | 112 | | } |
111 | 113 | | |
| 114 | + | static InputStroke[] getStrokes(DeviceInputEventArgs args) |
| 115 | + | { |
| 116 | + | InputStroke[] strokes; |
112 | 117 | | |
113 | | - | // static void inputHandler(object sender, DeviceInputEventArgs args) |
114 | | - | // { |
115 | | - | // DeviceInput input = new(); |
116 | | - | // input.Source = args.Source; |
| 118 | + | var modifiers = getKeyboardModifiers(); |
117 | 119 | | |
118 | | - | // if (args.Type == DeviceInputEventType.Button) |
119 | | - | // { |
120 | | - | // if (args.Source.Type == DeviceType.Joystick |
121 | | - | // || args.Source.Type == DeviceType.Gamepad) |
122 | | - | // { |
123 | | - | // input.HardwareButton = args.Button; |
124 | | - | // //input.Modifiers = getKeyboardModifiers(); |
| 120 | + | strokes = new InputStroke[modifiers.Length + 1]; |
| 121 | + | for (int i = 0; i < modifiers.Length; i++) |
| 122 | + | { |
| 123 | + | InputStroke strokeModifier = new(); |
| 124 | + | strokeModifier.Source = args.Source; |
| 125 | + | strokeModifier.Type = DeviceType.Keyboard; |
| 126 | + | strokeModifier.Modifier = true; |
| 127 | + | strokeModifier.RawValue = modifiers[i]; |
| 128 | + | strokes[i] = strokeModifier; |
| 129 | + | } |
125 | 130 | | |
126 | | - | // input.Strokes = getStrokes(args); |
127 | | - | // dispatch(input); |
128 | | - | // } |
129 | | - | // else if (args.Source.Type == DeviceType.Keyboard && args.Pressed == true) |
130 | | - | // { |
131 | | - | |
132 | | - | // input.HardwareButton = args.Button; |
133 | | - | // input.Strokes = getStrokes(args); |
| 131 | + | InputStroke stroke = new(); |
| 132 | + | stroke.Source = args.Source; |
| 133 | + | stroke.Type = args.DeviceType; |
| 134 | + | stroke.EventType = args.Type; |
| 135 | + | stroke.Modifier = false; |
| 136 | + | stroke.RawValue = args.Button; |
| 137 | + | strokes[strokes.Length - 1] = stroke; |
134 | 138 | | |
135 | | - | // dispatch(input); |
| 139 | + | return strokes; |
| 140 | + | } |
136 | 141 | | |
137 | | - | // } |
138 | | - | // else if (args.Source.Type == DeviceType.Mouse && args.Pressed == true) |
139 | | - | // { |
140 | | - | // input.HardwareButton = args.Button; |
141 | | - | // input.VPCButton = Helper.HardwareButtonToButtonName(input.HardwareButton); |
142 | | - | // //input. = Helper.MouseButtonToSCAction(input.HardwareButton); |
| 142 | + | static int[] getKeyboardModifiers() |
| 143 | + | { |
| 144 | + | foreach (IDevice dev in DeviceDetector.GetDetectedDevices()) |
| 145 | + | { |
| 146 | + | if (dev is IKeyboard) |
| 147 | + | { |
| 148 | + | IKeyboard keyboard = (IKeyboard)dev; |
| 149 | + | var modifiers = keyboard.GetModifiers(); |
| 150 | + | if (modifiers.Length > 0) |
| 151 | + | { |
| 152 | + | return modifiers; |
| 153 | + | } |
| 154 | + | } |
| 155 | + | } |
| 156 | + | return new int[0]; |
| 157 | + | } |
143 | 158 | | |
144 | | - | // input.Strokes = getStrokes(args); |
145 | | - | // /*foreach (InputStroke stroke in input.Strokes) |
146 | | - | // { |
147 | | - | // int strokeValue; |
148 | | - | // int.TryParse(stroke.StrokeValue, out strokeValue); |
149 | | - | // stroke.StrokeValue = "mouse" + stroke.StrokeValue; |
150 | | - | // }*/ |
151 | | - | // dispatch(input); |
152 | | - | // } |
153 | | - | // } |
154 | | - | // else if (args.Type == DeviceInputEventType.Axis) |
155 | | - | // { |
156 | | - | // //Calculate pct of how much user is moving stick |
157 | | - | // ushort value = (ushort)args.Value; |
158 | | - | // if (value < axisLowBound || value > axisHighBound) |
159 | | - | // { |
160 | | - | // input.Axis = args.Axis; |
161 | | - | // input.Strokes = getStrokes(args); |
162 | | - | // dispatch(input); |
163 | | - | // } |
164 | | - | // } |
| 159 | + | void dispatch(DeviceInput? deviceInput) |
| 160 | + | { |
| 161 | + | if (deviceInput == null) |
| 162 | + | { |
| 163 | + | logline = $"Failed to read device input"; |
| 164 | + | return; |
| 165 | + | } |
| 166 | + | DeviceInstance? instance = Profile.Instance.GetDeviceInstanceFromDevice(deviceInput.Source); |
165 | 167 | | |
166 | | - | //} |
| 168 | + | Rebind rebind = new(); |
| 169 | + | rebind.Input = deviceInput.Setting; |
| 170 | + | var device = deviceInput.Source.Name; |
| 171 | + | logline = string.Format("[{0}] registered [{2}] read as '{1}'", device, rebind.Input, deviceInput.Name); |
| 172 | + | } |
167 | 173 | | } |