Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Baballonia/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public override void OnFrameworkInitializationCompleted()
services.AddTransient<FirmwareView>();
services.AddTransient<OnboardingViewModel>();
services.AddTransient<OnboardingView>();
services.AddTransient<IUsbService, UsbService>();
}

ConfigurePlatformServices.Invoke(services);
Expand Down
1 change: 1 addition & 0 deletions src/Baballonia/Baballonia.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<PackageReference Include="SoundFlow" Version="1.2.1" />
<PackageReference Include="System.IO.Ports" Version="9.0.8" />
<PackageReference Include="System.Management" Version="9.0.8" />
<PackageReference Include="Usb.Events" Version="11.1.1.1" />
<PackageReference Include="VRChat.OSCQuery" Version="0.0.7" />
</ItemGroup>

Expand Down
9 changes: 9 additions & 0 deletions src/Baballonia/Contracts/IUsbService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Baballonia.Contracts;

public interface IUsbService
{
public event Action<string> OnUsbConnected;
public event Action<string> OnUsbDisconnected;
}
68 changes: 68 additions & 0 deletions src/Baballonia/Services/USBService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using Baballonia.Contracts;
using Usb.Events;

namespace Baballonia.Services;

public sealed class UsbService : IUsbService
{
public event Action<string>? OnUsbConnected;
public event Action<string>? OnUsbDisconnected;

private static readonly IUsbEventWatcher UsbEventWatcher = new UsbEventWatcher(
startImmediately: true,
addAlreadyPresentDevicesToList: true,
usePnPEntity: false,
includeTTY: true);

private readonly TimeSpan _eventThrottleInterval = TimeSpan.FromSeconds(1);
private DateTime _lastEventTime = DateTime.MinValue;
private readonly object _eventLock = new();

public UsbService()
{
UsbEventWatcher.UsbDeviceAdded += UsbDeviceAdded;
UsbEventWatcher.UsbDeviceRemoved += UsbDeviceRemoved;
}

private void UsbDeviceAdded(object? sender, UsbDevice? device)
{
if (device == null)
return;

RateLimitedAction(device.DeviceName, OnUsbConnected);
}

private void UsbDeviceRemoved(object? sender, UsbDevice? device)
{
if (device == null)
return;

RateLimitedAction(device.DeviceName, OnUsbDisconnected);
}

private void RateLimitedAction(string deviceName, Action<string>? action)
{
lock (_eventLock)
{
var now = DateTime.UtcNow;
var timeSinceLastEvent = now - _lastEventTime;

if (timeSinceLastEvent < _eventThrottleInterval)
{
return;
}

_lastEventTime = now;
}

action?.Invoke(deviceName);
}

~UsbService()
{
UsbEventWatcher.UsbDeviceAdded -= UsbDeviceAdded;
UsbEventWatcher.UsbDeviceRemoved -= UsbDeviceRemoved;
UsbEventWatcher.Dispose();
}
}
21 changes: 21 additions & 0 deletions src/Baballonia/Views/HomePageView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using Avalonia.VisualTree;
using Baballonia.Assets;
using Baballonia.Contracts;
using Baballonia.Helpers;
using Baballonia.Services;
using Baballonia.ViewModels.SplitViewPane;
using CommunityToolkit.Mvvm.DependencyInjection;

namespace Baballonia.Views;

Expand All @@ -26,6 +29,7 @@ public partial class HomePageView : UserControl

private readonly IDeviceEnumerator _deviceEnumerator;
private readonly ILocalSettingsService _localSettings;
private IUsbService _usbService;

public HomePageView(IDeviceEnumerator deviceEnumerator, ILocalSettingsService localSettings)
{
Expand Down Expand Up @@ -116,6 +120,17 @@ public HomePageView(IDeviceEnumerator deviceEnumerator, ILocalSettingsService lo

vm.SelectedCalibrationTextBlock = this.Find<TextBlock>("SelectedCalibrationTextBlockColor")!;
vm.SelectedCalibrationTextBlock.Text = Assets.Resources.Home_Eye_Calibration;

if (!Utils.IsSupportedDesktopOS) return;
_usbService = Ioc.Default.GetService<IUsbService>()!;
_usbService.OnUsbConnected += RefreshDevices;
_usbService.OnUsbDisconnected += RefreshDevices;
};
Unloaded += (_, _) =>
{
if (_usbService != null) return;
_usbService!.OnUsbConnected -= RefreshDevices;
_usbService!.OnUsbDisconnected -= RefreshDevices;
};
}

Expand Down Expand Up @@ -199,6 +214,12 @@ private void RefreshConnectedFaceDevices(object? sender, CancelEventArgs e)
//vm.FaceCamera.UpdateCameraDropDown();
}

private async void RefreshDevices(string deviceName)
{
await Dispatcher.UIThread.InvokeAsync(
() => RefreshDevices(null, null!));
}

private async void RefreshDevices(object? sender, RoutedEventArgs e)
{
if (DataContext is not HomePageViewModel vm) return;
Expand Down
Loading