Skip to content

Commit

Permalink
MP1-4766: HID profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Slion committed Apr 22, 2016
1 parent 8ea99ba commit 47ef1db
Show file tree
Hide file tree
Showing 9 changed files with 782 additions and 139 deletions.
Binary file modified mediaportal/Configuration/Resources/remote_MceHp.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
165 changes: 110 additions & 55 deletions mediaportal/Configuration/Sections/Remote.cs

Large diffs are not rendered by default.

Large diffs are not rendered by default.

73 changes: 54 additions & 19 deletions mediaportal/RemotePlugins/InputMapper/HidInputMappingForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,19 @@ namespace MediaPortal.InputDevices
/// </summary>
public class HidInputMappingForm : MPConfigForm
{
/// <summary>
/// Name of the HID profile we are configuring
/// </summary>
public string ProfileName{ get; private set;}

private readonly ArrayList actionList = new ArrayList();

/// <summary>
/// Required designer variable.
/// </summary>
private readonly Container components = null;

private readonly string[] fullScreenList = {"Fullscreen", "No Fullscreen"};
private readonly string inputClassName;
private readonly string[] fullScreenList = {"Fullscreen", "No Fullscreen"};
private readonly string[] layerList = {"all", "1", "2"};
private readonly Array nativeActionList = Enum.GetValues(typeof (Action.ActionType));
private readonly string[] nativePlayerList = {"TV", "DVD", "MEDIA", "MUSIC"};
Expand All @@ -64,7 +68,7 @@ public class HidInputMappingForm : MPConfigForm
private readonly string[] processList = {"Close Process", "Kill Process"};
private readonly string[] soundList = {"none", "back.wav", "click.wav", "cursor.wav"};
private readonly ArrayList windowsList = new ArrayList();
private readonly ArrayList windowsListFiltered = new ArrayList();
private readonly ArrayList windowsListFiltered = new ArrayList();
private MPButton buttonNew;
private bool changedSettings;

Expand All @@ -80,7 +84,7 @@ public class HidInputMappingForm : MPConfigForm
"{ADD}", "{SUBTRACT}", "{MULTIPLY}", "{DIVIDE}"
};

public HidInputMappingForm(string name)
public HidInputMappingForm(string aProfileName)
{
//
// Required for Windows Form Designer support
Expand Down Expand Up @@ -124,9 +128,9 @@ public HidInputMappingForm(string name)

comboBoxSound.DataSource = soundList;
comboBoxLayer.DataSource = layerList;
inputClassName = name;
LoadMapping(inputClassName + ".xml", false);
headerLabel.Caption = inputClassName;
ProfileName = aProfileName;
LoadMapping(ProfileName, false);
headerLabel.Caption = ProfileName;
}

/// <summary>
Expand Down Expand Up @@ -680,10 +684,10 @@ private void CloseThread()
Close();
}

private void LoadMapping(string xmlFile, bool defaults)
private void LoadMapping(string aProfileName, bool defaults)
{
var pathDefault = Path.Combine(InputHandler.DefaultsDirectory, xmlFile);
var pathCustom = Path.Combine(InputHandler.CustomizedMappingsDirectory, xmlFile);
var pathDefault = HidProfiles.GetDefaultProfilePath(aProfileName);
var pathCustom = HidProfiles.GetCustomProfilePath(aProfileName);

try
{
Expand All @@ -700,7 +704,7 @@ private void LoadMapping(string xmlFile, bool defaults)
if (!File.Exists(path))
{
MessageBox.Show(
"Can't locate mapping file " + xmlFile + "\n\nMake sure it exists in /InputDeviceMappings/defaults",
"Can't locate mapping file " + aProfileName + "\n\nMake sure it exists in /InputDeviceMappings/defaults",
"Mapping file missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
buttonUp.Enabled =
buttonDown.Enabled =
Expand Down Expand Up @@ -946,7 +950,7 @@ private void LoadMapping(string xmlFile, bool defaults)
{
//Possibly corrupted custom configuration
//Try loading the defaults then
LoadMapping(xmlFile, true);
LoadMapping("classic", true);
}
else
{
Expand All @@ -957,10 +961,40 @@ private void LoadMapping(string xmlFile, bool defaults)
}
}

private bool SaveMapping(string xmlFile)
/// <summary>
///
/// </summary>
/// <param name="aProfileName"></param>
/// <returns></returns>
private bool SaveMapping(string aProfileName)
{
var pathDefault = Path.Combine(InputHandler.DefaultsDirectory, xmlFile);
var pathCustom = Path.Combine(InputHandler.CustomizedMappingsDirectory, xmlFile);
string pathDefault = HidProfiles.GetDefaultProfilePath(aProfileName);
string pathCustom = HidProfiles.GetCustomProfilePath(aProfileName);

//If default profile exists and custom profile was not yet created
if (File.Exists(pathDefault) && !File.Exists(pathCustom))
{
//Prevent occluding default profiles
string newProfileName = aProfileName + ".user";
pathCustom = HidProfiles.GetCustomProfilePath(newProfileName);
//Do not overwrite existing file after customizing default profile
if (File.Exists(pathCustom))
{
//Make up a file name that does not exists for our new custom profile
uint i = 0;
do
{
i++;
aProfileName = newProfileName + i.ToString();
pathCustom = HidProfiles.GetCustomProfilePath(aProfileName);
}
while (File.Exists(pathCustom));
}
else
{
aProfileName=newProfileName;
}
}

#if !DEBUG
try
Expand Down Expand Up @@ -1114,6 +1148,7 @@ private bool SaveMapping(string xmlFile)
writer.WriteEndDocument();
writer.Close();
changedSettings = false;
ProfileName = aProfileName;
return true;
}
#if !DEBUG
Expand Down Expand Up @@ -1659,7 +1694,7 @@ private void buttonOk_Click(object sender, EventArgs e)
{
if (changedSettings)
{
SaveMapping(inputClassName + ".xml");
SaveMapping(ProfileName);
}
Close();
}
Expand All @@ -1668,7 +1703,7 @@ private void buttonApply_Click(object sender, EventArgs e)
{
if (changedSettings)
{
SaveMapping(inputClassName + ".xml");
SaveMapping(ProfileName);
}
}

Expand Down Expand Up @@ -1833,12 +1868,12 @@ private void buttonNew_Click(object sender, EventArgs e)

private void buttonDefault_Click(object sender, EventArgs e)
{
var pathCustom = Path.Combine(InputHandler.CustomizedMappingsDirectory, inputClassName + ".xml");
var pathCustom = Path.Combine(InputHandler.CustomizedMappingsDirectory, ProfileName );
if (File.Exists(pathCustom))
{
File.Delete(pathCustom);
}
LoadMapping(inputClassName + ".xml", true);
LoadMapping(ProfileName, true);
}

private void textBoxKeyCode_KeyPress(object sender, KeyPressEventArgs e)
Expand Down
1 change: 1 addition & 0 deletions mediaportal/RemotePlugins/RemotePlugins.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
<Compile Include="InputDevices.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Remotes\Hid\HidProfiles.cs" />
<Compile Include="Remotes\Hid\HidUsageAction.cs" />
<Compile Include="Remotes\IrTrans\IrTRans.cs" />
<Compile Include="ControlDevices\Devices\USBUIRT\USBUIRTPlugin.cs" />
Expand Down
62 changes: 3 additions & 59 deletions mediaportal/RemotePlugins/Remotes/Hid/HidHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,21 @@ public class HidHandler
/// Constructor: Initializes mappings from XML file
/// </summary>
/// <param name="deviceXmlName">Input device name</param>
public HidHandler(string deviceXmlName)
public HidHandler(string aProfileName)
{
IsLoaded = false;
//We will need one usage/action mapping for each HID UsagePage/UsageCollection we are listening too
_usageActions = new Dictionary<UInt32, HidUsageAction>();

var xmlPath = GetXmlPath(deviceXmlName);
var xmlPath = HidProfiles.GetExistingProfilePath(aProfileName);

LoadMapping(xmlPath);
}

#endregion Constructor

#region Private Fields

private const int KXmlVersion = 1;

private const int KCurrentLayer = 1;

private readonly Dictionary<UInt32, HidUsageAction> _usageActions;
Expand Down Expand Up @@ -92,61 +91,6 @@ public int CurrentLayer

#region Implementation

/// <summary>
/// Get version of XML mapping file
/// </summary>
/// <param name="xmlPath">Path to XML file</param>
/// Possible exceptions: System.Xml.XmlException
public int GetXmlVersion(string xmlPath)
{
var doc = new XmlDocument();
doc.Load(xmlPath);
return Convert.ToInt32(doc.DocumentElement.SelectSingleNode("/HidHandler").Attributes["version"].Value);
}

/// <summary>
/// Check if XML file exists and version is current
/// </summary>
/// <param name="xmlPath">Path to XML file</param>
/// Possible exceptions: System.IO.FileNotFoundException
/// System.Xml.XmlException
/// ApplicationException("XML version mismatch")
public bool CheckXmlFile(string xmlPath)
{
if (!File.Exists(xmlPath) || (GetXmlVersion(xmlPath) != KXmlVersion))
{
Log.Error("HID: File does not exists or version mismatch {0}", xmlPath);
return false;
}
return true;
}

/// <summary>
/// Get path to XML mapping file for given device name
/// </summary>
/// <param name="deviceXmlName">Input device name</param>
/// <returns>Path to XML file</returns>
/// Possible exceptions: System.IO.FileNotFoundException
/// System.Xml.XmlException
/// ApplicationException("XML version mismatch")
public string GetXmlPath(string deviceXmlName)
{
var path = string.Empty;
var pathDefault = Path.Combine(InputHandler.DefaultsDirectory, deviceXmlName + ".xml");
var pathCustom = Path.Combine(InputHandler.CustomizedMappingsDirectory, deviceXmlName + ".xml");

if (File.Exists(pathCustom) && CheckXmlFile(pathCustom))
{
path = pathCustom;
Log.Info("MAP: using custom mappings for {0}", deviceXmlName);
}
else if (File.Exists(pathDefault) && CheckXmlFile(pathDefault))
{
path = pathDefault;
Log.Info("MAP: using default mappings for {0}", deviceXmlName);
}
return path;
}

/// <summary>
/// Try parsing the given string as a decimal or hexadecimal if 0x prefix is found.
Expand Down
17 changes: 11 additions & 6 deletions mediaportal/RemotePlugins/Remotes/Hid/HidListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class HidListener : IInputDevice
/// <summary>
/// Tells whether verbose logs are enabled.
/// </summary>
static public bool Verbose{get; private set;}
static public bool Verbose {get; private set;}

/// <summary>
///
Expand Down Expand Up @@ -71,13 +71,18 @@ private void Init()
//Since HID now replaces the retired MCE implementation we test both setting options to migrate users nicely to HID
//Please also note that HID is now enabled by default on new installation.
_controlEnabled = xmlreader.GetValueAsBool("remote", "HidEnabled", true) || xmlreader.GetValueAsBool("remote", "MCE", false);
Verbose = xmlreader.GetValueAsBool("remote", "HidVerbose", false) || xmlreader.GetValueAsBool("remote", "MCEVerboseLog", false);
Verbose = xmlreader.GetValueAsBool("remote", "HidVerbose", false) || xmlreader.GetValueAsBool("remote", "MCEVerboseLog", false);

//Get the name of our current HID profile, default to legacy Generic-HID.
string hidProfile = xmlreader.GetValueAsString("remote", "HidProfile", "Generic-HID");

if (_controlEnabled)
{
_hidHandler = new HidHandler(hidProfile);
}

}

if (_controlEnabled)
{
_hidHandler = new HidHandler("Generic-HID");
}
}

/// <summary>
Expand Down
Loading

0 comments on commit 47ef1db

Please sign in to comment.