Skip to content

Commit

Permalink
Merge pull request #60 from Snoothy/develop
Browse files Browse the repository at this point in the history
Release v0.6.0
  • Loading branch information
Snoothy authored Dec 3, 2018
2 parents 187af6d + 9ecd5f2 commit 673ebbc
Show file tree
Hide file tree
Showing 19 changed files with 426 additions and 102 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

## [0.6.0] - 2018-12-03

### Added

- Added bind mode for inputs

### Changed
- Updated to IOWrapper v0.9.11

## [0.5.2] - 2018-10-28
### Fixed

- Values other than 0 or 50 for DeadZone should now work properly - Maximum deflection should now be achievable again

## [0.5.1] - 2018-10-28
Expand Down
2 changes: 1 addition & 1 deletion GitVersion.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ branches:
mode: ContinuousDelivery
tag: alpha
increment: none
dev(elop)?(ment)?$:
develop:
mode: ContinuousDelivery
tag: alpha-rc
increment: Patch
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<img src="icon.png" align="right" />

# Universal Control Remapper
[![GitHub release](https://img.shields.io/badge/release-v0.5.2-blue.svg)](https://github.com/Snoothy/UCR/releases/tag/v0.5.2) [![IOWrapper version](https://img.shields.io/badge/IOWrapper-v0.8.3-blue.svg)](https://github.com/evilC/IOWrapper) [![license](https://img.shields.io/github/license/snoothy/ucr.svg)](https://github.com/Snoothy/UCR/blob/master/LICENSE) [![Github All Releases](https://img.shields.io/github/downloads/snoothy/ucr/total.svg)](https://github.com/Snoothy/UCR/releases) [![AppVeyor](https://ci.appveyor.com/api/projects/status/github/Snoothy/UCR?svg=true)](https://ci.appveyor.com/project/Snoothy/ucr) [![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=Snoothy_UCR&metric=alert_status)](https://sonarcloud.io/dashboard?id=Snoothy_UCR)
[![GitHub release](https://img.shields.io/badge/release-v0.6.0-blue.svg)](https://github.com/Snoothy/UCR/releases/tag/v0.6.0) [![IOWrapper version](https://img.shields.io/badge/IOWrapper-v0.9.11-blue.svg)](https://github.com/evilC/IOWrapper) [![license](https://img.shields.io/github/license/snoothy/ucr.svg)](https://github.com/Snoothy/UCR/blob/master/LICENSE) [![Github All Releases](https://img.shields.io/github/downloads/snoothy/ucr/total.svg)](https://github.com/Snoothy/UCR/releases) [![AppVeyor](https://ci.appveyor.com/api/projects/status/github/Snoothy/UCR?svg=true)](https://ci.appveyor.com/project/Snoothy/ucr) [![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=Snoothy_UCR&metric=alert_status)](https://sonarcloud.io/dashboard?id=Snoothy_UCR)

Universal Control Remapper is a complete rewrite of the original [UCR](https://github.com/evilC/UCR), created in collaboration with [evilC](https://github.com/evilC/).

Expand Down
22 changes: 9 additions & 13 deletions UCR.Core/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace HidWizards.UCR.Core
{
public class Context : IDisposable
public sealed class Context : IDisposable
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private const string ContextName = "context.xml";
Expand All @@ -26,18 +26,13 @@ public class Context : IDisposable
public List<DeviceGroup> OutputGroups { get; set; }

/* Runtime */
[XmlIgnore]
public Profile ActiveProfile { get; set; }
[XmlIgnore]
public ProfilesManager ProfilesManager { get; set; }
[XmlIgnore]
public DevicesManager DevicesManager { get; set; }
[XmlIgnore]
public DeviceGroupsManager DeviceGroupsManager { get; set; }
[XmlIgnore]
public SubscriptionsManager SubscriptionsManager { get; set; }
[XmlIgnore]
public PluginsManager PluginManager { get; set; }
[XmlIgnore] public Profile ActiveProfile { get; set; }
[XmlIgnore] public ProfilesManager ProfilesManager { get; set; }
[XmlIgnore] public DevicesManager DevicesManager { get; set; }
[XmlIgnore] public DeviceGroupsManager DeviceGroupsManager { get; set; }
[XmlIgnore] public SubscriptionsManager SubscriptionsManager { get; set; }
[XmlIgnore] public PluginsManager PluginManager { get; set; }
[XmlIgnore] public BindingManager BindingManager { get; set; }

public delegate void ActiveProfileChanged(Profile profile);
public event ActiveProfileChanged ActiveProfileChangedEvent;
Expand Down Expand Up @@ -65,6 +60,7 @@ private void Init()
DeviceGroupsManager = new DeviceGroupsManager(this, InputGroups, OutputGroups);
SubscriptionsManager = new SubscriptionsManager(this);
PluginManager = new PluginsManager(PluginPath);
BindingManager = new BindingManager(this);
}

private void SetCommandLineOptions()
Expand Down
162 changes: 162 additions & 0 deletions UCR.Core/Managers/BindingManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Threading;
using HidWizards.IOWrapper.DataTransferObjects;
using HidWizards.UCR.Core.Annotations;
using HidWizards.UCR.Core.Models;
using HidWizards.UCR.Core.Models.Binding;
using HidWizards.UCR.Core.Utilities;
using NLog;
using Logger = NLog.Logger;

namespace HidWizards.UCR.Core.Managers
{
public sealed class BindingManager : IDisposable, INotifyPropertyChanged
{
private double _bindModeProgress = 0;

public double BindModeProgress
{
get { return _bindModeProgress / BindModeTime * 100.0; }
set
{
_bindModeProgress = value;
OnPropertyChanged();
}
}

private static readonly double BindModeTime = 5000.0;
private static readonly int BindModeTick = 10;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly Context _context;
private List<Device> _deviceList;
private DeviceBinding _deviceBinding;
private DispatcherTimer BindingTimer;
private readonly object bindmodeLock = new object();
private bool bindmodeActive;

public delegate void EndBindModeDelegate(DeviceBinding deviceBinding);
public event EndBindModeDelegate EndBindModeHandler;

public BindingManager(Context context)
{
_context = context;
_deviceList = new List<Device>();
Logger.Debug($"Start bind mode");
}

public void BeginBindMode(DeviceBinding deviceBinding)
{
if (_deviceList.Count > 0) EndBindMode();
_deviceBinding = deviceBinding;
foreach (var device in deviceBinding.Profile.GetDeviceList(deviceBinding))
{
_context.IOController.SetDetectionMode(DetectionMode.Bind, GetProviderDescriptor(device), GetDeviceDescriptor(device), InputChanged);
_deviceList.Add(device);
}

BindingTimer = new DispatcherTimer();
BindingTimer.Tick += BindingTimerOnTick;
BindingTimer.Interval = TimeSpan.FromMilliseconds(BindModeTick);
BindModeProgress = BindModeTime;
BindingTimer.Start();
bindmodeActive = true;
}

private void BindingTimerOnTick(object sender, EventArgs e)
{
BindModeProgress = _bindModeProgress - BindModeTick;
if (BindModeProgress <= 0.0) EndBindMode();
}

private void EndBindMode()
{
lock (bindmodeLock)
{
Logger.Debug($"End bind mode");
if (!bindmodeActive) return;

EndBindModeHandler?.Invoke(_deviceBinding);
BindingTimer.Stop();

foreach (var device in _deviceList)
{
_context.IOController.SetDetectionMode(DetectionMode.Subscription, GetProviderDescriptor(device),
GetDeviceDescriptor(device));
}

_deviceList = new List<Device>();
BindingTimer.Stop();
bindmodeActive = false;
}
}

private DeviceDescriptor GetDeviceDescriptor(Device device)
{
return new DeviceDescriptor()
{
DeviceHandle = device.DeviceHandle,
DeviceInstance = device.DeviceNumber
};
}

private ProviderDescriptor GetProviderDescriptor(Device device)
{
return new ProviderDescriptor()
{
ProviderName = device.ProviderName
};
}

private void InputChanged(ProviderDescriptor providerDescriptor, DeviceDescriptor deviceDescriptor, BindingReport bindingReport, int value)
{
if (!DeviceBinding.MapCategory(bindingReport.Category).Equals(_deviceBinding.DeviceBindingCategory)) return;
if (!IsInputValid(bindingReport.Category, value)) return;

var device = FindDevice(providerDescriptor, deviceDescriptor);
_deviceBinding.SetDeviceGuid(device.Guid);
_deviceBinding.SetKeyTypeValue((int)bindingReport.BindingDescriptor.Type, bindingReport.BindingDescriptor.Index, bindingReport.BindingDescriptor.SubIndex);
EndBindMode();
}

private bool IsInputValid(BindingCategory bindingCategory, int value)
{
switch (DeviceBinding.MapCategory(bindingCategory))
{
case DeviceBindingCategory.Delta:
case DeviceBindingCategory.Event:
return true;
case DeviceBindingCategory.Momentary:
return value != 0;
case DeviceBindingCategory.Range:
return Constants.AxisMaxValue * 0.4 < Math.Abs(value)
&& Constants.AxisMaxValue * 0.6 > Math.Abs(value);
default:
return false;
}
}

private Device FindDevice(ProviderDescriptor providerDescriptor, DeviceDescriptor deviceDescriptor)
{
return _deviceList.Find(d => d.ProviderName == providerDescriptor.ProviderName
&& d.DeviceHandle == deviceDescriptor.DeviceHandle
&& d.DeviceNumber == deviceDescriptor.DeviceInstance
);
}

public void Dispose()
{
EndBindMode();
}

public event PropertyChangedEventHandler PropertyChanged;

[NotifyPropertyChangedInvocator]
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
26 changes: 25 additions & 1 deletion UCR.Core/Managers/SubscriptionsManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using HidWizards.IOWrapper.DataTransferObjects;
using HidWizards.UCR.Core.Annotations;
using HidWizards.UCR.Core.Models;
using HidWizards.UCR.Core.Models.Binding;
using HidWizards.UCR.Core.Models.Subscription;
Expand All @@ -9,10 +12,21 @@

namespace HidWizards.UCR.Core.Managers
{
public class SubscriptionsManager : IDisposable
public sealed class SubscriptionsManager : IDisposable, INotifyPropertyChanged
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

private bool _profileActive;
public bool ProfileActive
{
get => _profileActive;
set
{
_profileActive = value;
OnPropertyChanged();
}
}

internal SubscriptionState SubscriptionState { get; set; }
private readonly Context _context;

Expand Down Expand Up @@ -72,6 +86,7 @@ private void FinalizeNewState(Profile profile, SubscriptionState subscriptionSta
}

_context.OnActiveProfileChangedEvent(profile);
ProfileActive = true;
}

public bool DeactivateProfile()
Expand Down Expand Up @@ -105,6 +120,7 @@ public bool DeactivateProfile()
SubscriptionState = null;
_context.ActiveProfile = null;
_context.OnActiveProfileChangedEvent(null);
ProfileActive = false;

return success;
}
Expand Down Expand Up @@ -283,5 +299,13 @@ public void Dispose()
DeactivateProfile();
}
}

public event PropertyChangedEventHandler PropertyChanged;

[NotifyPropertyChangedInvocator]
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
37 changes: 36 additions & 1 deletion UCR.Core/Models/Binding/DeviceBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ public enum DeviceBindingCategory
public class DeviceBinding : INotifyPropertyChanged
{
/* Persistence */
public bool IsBound { get; set; }
private bool _isBound;
public bool IsBound
{
get => _isBound;
set
{
_isBound = value;
OnPropertyChanged();
}
}
// Index in its device list
public Guid DeviceGuid { get; set; }
// Subscription key
Expand All @@ -36,6 +45,18 @@ public class DeviceBinding : INotifyPropertyChanged
[XmlIgnore]
public DeviceBindingCategory DeviceBindingCategory { get; set; }

private bool _isInBindMode = false;
[XmlIgnore]
public bool IsInBindMode
{
get => _isInBindMode;
private set
{
_isInBindMode = value;
OnPropertyChanged();
}
}


public delegate void ValueChanged(long value);

Expand Down Expand Up @@ -135,6 +156,20 @@ public void WriteOutput(long value)
OutputSink?.Invoke(value);
}

public void EnterBindMode()
{
Profile.Context.BindingManager.BeginBindMode(this);
Profile.Context.BindingManager.EndBindModeHandler += OnEndBindModeHandler;
IsInBindMode = true;
}

private void OnEndBindModeHandler(DeviceBinding deviceBinding)
{
if (deviceBinding.Guid != Guid) return;
IsInBindMode = false;
Profile.Context.BindingManager.EndBindModeHandler -= OnEndBindModeHandler;
}

private void InputChanged(long value)
{
CurrentValue = value;
Expand Down
2 changes: 1 addition & 1 deletion UCR.Core/Models/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private static List<DeviceBindingNode> GetDeviceBindingMenu(List<DeviceReportNod
public string GetBindingName(DeviceBinding deviceBinding)
{
if (!deviceBinding.IsBound) return "Not bound";
return GetBindingName(deviceBinding, DeviceBindingMenu) ?? "Unknown input";
return GetBindingName(deviceBinding, GetDeviceBindingMenu(deviceBinding.Profile.Context, deviceBinding.DeviceIoType)) ?? "Unknown input";
}

private static string GetBindingName(DeviceBinding deviceBinding, List<DeviceBindingNode> deviceBindingNodes)
Expand Down
2 changes: 2 additions & 0 deletions UCR.Core/UCR.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@
<HintPath>..\packages\Trinet.Core.IO.Ntfs.4.1.1\lib\net35\Trinet.Core.IO.Ntfs.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Attributes\PluginAttribute.cs" />
<Compile Include="Attributes\PluginGuiAttribute.cs" />
<Compile Include="Attributes\PluginOutput.cs" />
<Compile Include="Attributes\PluginInput.cs" />
<Compile Include="Attributes\PluginIoAttribute.cs" />
<Compile Include="Managers\BindingManager.cs" />
<Compile Include="Managers\SubscriptionsManager.cs" />
<Compile Include="Models\Binding\DeviceBindingNode.cs" />
<Compile Include="Managers\DeviceGroupsManager.cs" />
Expand Down
2 changes: 1 addition & 1 deletion UCR/Utilities/ResourceLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void Load()
var folderName = path.Remove(0, path.LastIndexOf(Path.DirectorySeparatorChar) + 1);
if (File.Exists(Path.Combine(path, folderName + ".dll")))
{
catalog.Catalogs.Add(new DirectoryCatalog(path));
catalog.Catalogs.Add(new DirectoryCatalog(path, folderName + ".dll"));
}
}

Expand Down
Loading

0 comments on commit 673ebbc

Please sign in to comment.