ctrl k
  • DoUrVerse/ViewModels/DebugViewModel.cs
    ■ ■ ■ ■ ■ ■
     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  }
  • DoUrVerseLib/Device/DeviceDetector.cs
    ■ ■ ■ ■ ■ ■
    skipped 93 lines
    94 94   {
    95 95   //TODO: Fix this
    96 96   //device.InputReceived += DeviceInput.DeviceInputDeviceHandler;
     97 + device.Instance = 1 + DeviceCount(device.Type);
    97 98   detected.Add(device);
    98 99   device.Monitor();
    99 100  
    skipped 81 lines
    181 182  
    182 183   devices = getDevicesByType(DeviceType.Joystick);
    183 184   //devices.AddRange(getDevicesByType(DeviceType.Keyboard));
    184  - //devices.AddRange(getDevicesByType(DeviceType.Gamepad));
     185 + devices.AddRange(getDevicesByType(DeviceType.Gamepad));
    185 186   //devices.AddRange(getDevicesByType(DeviceType.Mouse));
    186 187   devices.AddRange(manualDevices);
    187 188   return devices;
    skipped 39 lines
    227 228   break;
    228 229   case SharpDX.DirectInput.DeviceType.FirstPerson:
    229 230   case SharpDX.DirectInput.DeviceType.Joystick:
     231 + case SharpDX.DirectInput.DeviceType.Gamepad:
    230 232   dxDevice = new Windows.DxJoystick(directInput, device.InstanceGuid);
    231 233   break;
    232 234   }
    skipped 95 lines
    328 330   Type = DeviceType.Mouse
    329 331   }
    330 332   };
     333 + }
     334 + public static int DeviceCount(DeviceType type)
     335 + {
     336 + int i = 0;
     337 + foreach (IDevice dev in instance.detected)
     338 + {
     339 + if (dev.Type == type) i++;
     340 + }
     341 + return i;
    331 342   }
    332 343  }
    333 344   
  • DoUrVerseLib/Device/DeviceInput.cs
    ■ ■ ■ ■ ■
    skipped 4 lines
    5 5  using DoUrVerseLib.Config;
    6 6  namespace DoUrVerseLib.Device;
    7 7  
    8  -
     8 +/// <summary>
     9 +/// A device input is a collection of one or more strokes eg. ctrl+shift+u
     10 +/// </summary>
    9 11  public class DeviceInput
    10 12  {
    11 13   string name = "";
    skipped 125 lines
  • DoUrVerseLib/Device/IDevice.cs
    ■ ■ ■ ■ ■ ■
    skipped 15 lines
    16 16   public bool IsAlive { get; }
    17 17  }
    18 18  public delegate void DeviceInputEvent(object source, DeviceInputEventArgs args);
    19  -public enum DeviceInputEventType { Unknown, Axis, Button}
    20  -public enum DeviceInputAxis { Unknown, X, Y, Z, Slider}
     19 +public enum DeviceInputEventType { Unknown, Axis, Button, Rotation}
     20 +public enum DeviceInputAxis { Unknown, X, Y, Z, Slider, Rotation}
    21 21  
    22 22  
    23 23  public class DeviceInputEventArgs : EventArgs
    skipped 22 lines
    46 46   Value = value,
    47 47   Source = source
    48 48   };
     49 + public static DeviceInputEventArgs CreateRotation(DeviceInputAxis axis, short value, IDevice source) => new()
     50 + {
     51 + DeviceType = source.Type,
     52 + Type = DeviceInputEventType.Rotation,
     53 + Axis = axis,
     54 + Value = value,
     55 + Source = source
     56 + };
     57 +}
     58 +public class UnrecognizedDeviceInputEventArgs : DeviceInputEventArgs
     59 +{
     60 + public string Description { get; set;} = string.Empty;
     61 +}
     62 +public class UnrecognizedDeviceInputEventArgs : DeviceInputEventArgs
     63 +{
     64 + public string Description { get; set;} = string.Empty;
     65 +}
     66 +public class UnrecognizedDeviceInputEventArgs : DeviceInputEventArgs
     67 +{
     68 + public string Description { get; set;} = string.Empty;
     69 +}
     70 +public class UnrecognizedDeviceInputEventArgs : DeviceInputEventArgs
     71 +{
     72 + public string Description { get; set;} = string.Empty;
    49 73  }
    50 74  public class UnrecognizedDeviceInputEventArgs : DeviceInputEventArgs
    51 75  {
    skipped 28 lines
  • DoUrVerseLib/Device/Windows/DxGamepad.cs
    ■ ■ ■ ■ ■ ■
     1 +/*using DoUrVerseLib.SCXML;
     2 +using SharpDX.DirectInput;
     3 +using SharpDX.Multimedia;
     4 +using System.Diagnostics;
     5 +using System.Text.RegularExpressions;
     6 +
     7 +namespace DoUrVerseLib.Device.Windows;
     8 +
     9 +internal class DxGamepad : DirectXDevice
     10 +{
     11 + public Thread thread;
     12 + object lockObject = new();
     13 + CancellationTokenSource source = new CancellationTokenSource();
     14 + Gamepad? gamepad;
     15 +
     16 + public override SharpDX.DirectInput.Device SharpDxDevice => gamepad ?? throw new("Missing gamepad device");
     17 + public override DeviceType Type => DeviceType.Gamepad;
     18 +
     19 + public override bool IsAlive => thread.IsAlive;
     20 +
     21 + public DxGamepad(DirectInput input, Guid deviceGuid)
     22 + {
     23 + gamepad = new Gamepad(input, deviceGuid);
     24 + gamepad.Properties.BufferSize = 128;
     25 + gamepad.Acquire();
     26 + }
     27 +
     28 + void runner(object? obj)
     29 + {
     30 + if (obj == null || obj.GetType() != typeof(CancellationToken))
     31 + {
     32 + throw new Exception("Unable to manage devices without a cancellation token");
     33 + }
     34 + CancellationToken token = (CancellationToken)obj;
     35 +
     36 + while (!token.IsCancellationRequested)
     37 + {
     38 + Thread.Sleep(60);
     39 + try
     40 + {
     41 + GamepadUpdate[]? datas;
     42 + lock (lockObject)
     43 + {
     44 + gamepad?.Poll();
     45 + datas = gamepad?.GetBufferedData();
     46 +
     47 + if (datas == null) continue;
     48 + }
     49 +
     50 + foreach (GamepadUpdate state in datas)
     51 + {
     52 + var offset = state.Offset.ToString().ToLower();
     53 + if (offset.Contains("button"))
     54 + {
     55 + byte hardwareButton = Helper.DxButtonToHardwareButton(offset);
     56 + DeviceInputEventArgs input = DeviceInputEventArgs.CreateButton(hardwareButton, true, this);
     57 + InvokeInputEvent(input);
     58 +
     59 + }
     60 + else if (offset == "x" || offset == "y" || offset == "z")
     61 + {
     62 + DeviceInputAxis axis = Enum.Parse<DeviceInputAxis>(offset.ToUpper());
     63 + DeviceInputEventArgs input = DeviceInputEventArgs.CreateAxis(axis, (short)state.Value, this);
     64 + InvokeInputEvent(input);
     65 + }
     66 + else if (offset.Contains("sliders"))
     67 + {
     68 + DeviceInputAxis axis = DeviceInputAxis.Slider;
     69 + DeviceInputEventArgs input = DeviceInputEventArgs.CreateAxis(axis, (short)state.Value, this);
     70 + int di = -1;
     71 + int.TryParse(offset.Substring(7), out di);
     72 + input.Button = di; //If the gamepad has more sliders we need to save which one it was
     73 + InvokeInputEvent(input);
     74 + }
     75 + }
     76 + }
     77 + catch (Exception e)
     78 + {
     79 + Debug.WriteLine("JOYSTICK LISTENING THREAD EXCEPTION " + e.Message);
     80 + return;
     81 + }
     82 + }
     83 + }
     84 + public override void Monitor()
     85 + {
     86 + if (gamepad == null) { return; }
     87 + CancellationToken token = source.Token;
     88 + thread = new Thread(runner);
     89 + thread.Name = "Gamepad monitor";
     90 + thread.Start(token);
     91 + }
     92 + public override bool Equals(IDevice other)
     93 + {
     94 + if (other == null) return false;
     95 + if (other.GetType() != GetType()) return false;
     96 +
     97 + return other.HardwareId == HardwareId;
     98 + }
     99 +
     100 + public override void Dispose()
     101 + {
     102 + source.Cancel();
     103 + lock (lockObject)
     104 + {
     105 + gamepad?.Dispose();
     106 + }
     107 + }
     108 +}
     109 +*/
  • DoUrVerseLib/Device/Windows/DxJoystick.cs
    ■ ■ ■ ■ ■
    skipped 48 lines
    49 49  
    50 50   foreach (JoystickUpdate state in datas)
    51 51   {
     52 + DeviceInputAxis axis;
     53 + DeviceInputEventArgs input;
     54 + switch (state.Offset)
     55 + {
     56 + case JoystickOffset.X:
     57 + case JoystickOffset.Y:
     58 + case JoystickOffset.Z:
     59 + axis = Enum.Parse<DeviceInputAxis>(state.Offset.ToString());
     60 + input = DeviceInputEventArgs.CreateAxis(axis, (short)state.Value, this);
     61 + InvokeInputEvent(input);
     62 + break;
     63 + case JoystickOffset.RotationX:
     64 + case JoystickOffset.RotationY:
     65 + case JoystickOffset.RotationZ:
     66 + axis = Enum.Parse<DeviceInputAxis>(state.Offset.ToString().Last().ToString());
     67 + input = DeviceInputEventArgs.CreateRotation(axis, (short)state.Value, this);
     68 + InvokeInputEvent(input);
     69 + break;
     70 + }
     71 +
     72 +
    52 73   var offset = state.Offset.ToString().ToLower();
    53 74   if (offset.Contains("button"))
    54 75   {
    55 76   byte hardwareButton = Helper.DxButtonToHardwareButton(offset);
    56  - DeviceInputEventArgs input = DeviceInputEventArgs.CreateButton(hardwareButton, true, this);
     77 + input = DeviceInputEventArgs.CreateButton(hardwareButton, true, this);
    57 78   InvokeInputEvent(input);
    58 79  
    59 80   }
    60 81   else if (offset == "x" || offset == "y" || offset == "z")
    61 82   {
    62  - DeviceInputAxis axis = Enum.Parse<DeviceInputAxis>(offset.ToUpper());
    63  - DeviceInputEventArgs input = DeviceInputEventArgs.CreateAxis(axis, (short)state.Value, this);
     83 + axis = Enum.Parse<DeviceInputAxis>(state.Offset.ToString());
     84 + input = DeviceInputEventArgs.CreateAxis(axis, (short)state.Value, this);
    64 85   InvokeInputEvent(input);
    65 86   }
    66 87   else if (offset.Contains("sliders"))
    67 88   {
    68  - DeviceInputAxis axis = DeviceInputAxis.Slider;
    69  - DeviceInputEventArgs input = DeviceInputEventArgs.CreateAxis(axis, (short)state.Value, this);
     89 + axis = DeviceInputAxis.Slider;
     90 + input = DeviceInputEventArgs.CreateAxis(axis, (short)state.Value, this);
    70 91   int di = -1;
    71 92   int.TryParse(offset.Substring(7), out di);
    72 93   input.Button = di; //If the joystick has more sliders we need to save which one it was
    skipped 41 lines
  • DoUrVerseLib/Platform/IPlatform.cs
    ■ ■ ■ ■ ■ ■
    skipped 13 lines
    14 14   Task<string?> OpenFileAsync(PlatformFileTypeGroup[] fileTypes);
    15 15   Task<string?> SaveFileAsync(PlatformFileTypeGroup[] fileTypes);
    16 16   IAssetManager Assets();
     17 + //IDeviceManager Devices();
    17 18  }
    18 19  
     20 +public interface IDeviceManager
     21 +{
     22 +
     23 +}
     24 +
     25 + 
  • DoUrVerseLib/SCXML/Profile.cs
    ■ ■ ■ ■ ■ ■
    skipped 809 lines
    810 810   if (deviceToInstanceMap.ContainsKey(device)) {
    811 811   return deviceToInstanceMap[device];
    812 812   }
     813 +
     814 + //Get the device based on the order they're detected by DirectX
     815 + // see https://robertsspaceindustries.com/spectrum/community/SC/forum/50259/thread/input-config-file-behaviour/4608064
     816 + int instance = 0;
     817 + foreach (IDevice dev in DeviceDetector.GetDetectedDevices())
     818 + {
     819 + if (dev.Type == device.Type)
     820 + {
     821 + instance++;
     822 + }
     823 +
     824 + if (dev.Name == device.Name)
     825 + {
     826 + DeviceInstance devInstance = DeviceInstance.FromDevice(dev);
     827 + devInstance.Instance = instance.ToString();
     828 + return devInstance;
     829 + }
     830 + }
     831 +
    813 832   return null;
    814 833   }
    815 834   void detectEligibleDevices()
    skipped 213 lines
Please wait...
Page is in error, reload to recover