Commit 1383f28f authored by Akinwale Ariwodola's avatar Akinwale Ariwodola
Browse files

Created GhostMainframe project. Added Device and InputDevices classes.

parent 8ccbad85
/*
* Copyright (c) 2017 Akinwale Ariwodola <akinwale@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace GhostMainframe.Common
{
/// <summary>
/// The Linux input event codes corresponding to uapi/linux/input-event-codes.h.
/// </summary>
public enum InputEventCode
{
KeyBack = 0x09e, // 158
KeyHomepage = 0x0ac, // 172
// Gamepad event codes
BtnGamepad = 0x130, // 304
BtnSouth = 0x130, // 304
BtnA = BtnSouth,
BtnEast = 0x131, // 305
BtnB = BtnEast,
BtnC = 0x132, // 306
BtnNorth = 0x133, // 307
BtnX = BtnNorth,
BtnWest = 0x134, // 308
BtnY = BtnWest,
BtnZ = 0x135, // 309
BtnTL = 0x136, // 310
BtnTR = 0x137, // 311
BtnTL2 = 0x138, // 312
BtnTR2 = 0x139, // 313
BtnSelect = 0x13a, // 314
BtnStart = 0x13b, // 315
BtnMode = 0x13c, // 316
BtnThumbL = 0x13d, // 317
BtnThumbR = 0x13e // 318
}
}
/*
* Copyright (c) 2017 Akinwale Ariwodola <akinwale@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace GhostMainframe.Control.Input
{
/// <summary>
/// Represents a basic input device with a name and its evdev file path.
/// </summary>
public class Device
{
public string Name { get; set; }
public string EvdevPath { get; set; }
public bool IsGamepad { get; set; }
public Device()
{
}
}
}
/*
* Copyright (c) 2017 Akinwale Ariwodola <akinwale@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using GhostMainframe.Common;
namespace GhostMainframe.Control.Input
{
public class InputDevices
{
const string InputDevicesFile = "/proc/bus/input/devices";
/// <summary>
/// Detects the first gamepad device connected to the system.
/// </summary>
/// <returns>the first gamepad found in /proc/bus/input/devices, or null if no gamepad was found</returns>
public static Device DetectGamepad()
{
Device gamepad = null;
using (StreamReader reader = new StreamReader(new FileStream(
InputDevicesFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
Device device = new Device();
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
// Get the device name
if (line.StartsWith("N", StringComparison.InvariantCulture))
{
device.Name = line.Substring(line.IndexOf('"') + 1, line.Length - line.IndexOf('"') - 2);
}
// Get event* handler (for /dev/input/event*)
if (line.StartsWith("H", StringComparison.InvariantCulture)) // handlers
{
string[] handlers = line.Substring(line.IndexOf('=') + 1).Trim().Split(' ');
foreach (string handler in handlers)
{
if (handler.StartsWith("event", StringComparison.InvariantCulture))
{
device.EvdevPath = string.Format("/dev/input/{0}", handler);
}
}
}
// Find the KEY bitmap and parse (if present)
if (line.StartsWith("B", StringComparison.InvariantCulture) && line.Contains("KEY"))
{
StringBuilder sb = new StringBuilder();
string key = line.Substring(line.IndexOf('=') + 1);
string[] groups = key.Trim().Split(' ');
foreach (string grp in groups)
{
if (grp == "0")
{
sb.Append(string.Empty.PadLeft(64, '0'));
}
else
{
StringBuilder grpBin = new StringBuilder();
foreach (char c in grp)
{
grpBin.Append(Convert.ToString(Convert.ToInt32(c.ToString(), 16), 2).PadLeft(4, '0'));
}
sb.Append(grpBin.ToString().PadLeft(64, '0'));
}
}
string bin = sb.ToString();
List<int> keybits = new List<int>();
// Count the bits with j starting from right to left
for (int i = bin.Length - 1, j = 0; i > -1; i--, j++)
{
if (bin[i] == '1')
{
keybits.Add(j);
}
}
// BtnSouth / BtnGamepad key indicates that the device is a gamepad
if (keybits.Contains((int) InputEventCode.BtnGamepad))
{
device.IsGamepad = true;
gamepad = device;
break;
}
// Next device
if (line.Trim().Length == 0)
{
device = new Device();
}
}
}
}
return gamepad;
}
public InputDevices()
{
}
}
}
/*
* Copyright (c) 2017 Akinwale Ariwodola <akinwale@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using GhostMainframe.Control.Input;
namespace GhostMainframe
{
public class Core
{
public static void Main(string[] args)
{
Device gamepad = InputDevices.DetectGamepad();
if (gamepad != null)
{
Console.WriteLine("Gamepad={0}, Path={1}", gamepad.Name, gamepad.EvdevPath);
}
else
{
Console.WriteLine("No gamepad device found.");
}
Console.ReadLine();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProjectGuid>{72FB1682-03B9-4D3F-9FA4-5E35AC682225}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>GhostMainframe</RootNamespace>
<AssemblyName>GhostMainframe</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Folder Include="Navigation\" />
<Folder Include="Control\" />
<Folder Include="Core\" />
<Folder Include="Control\Input\" />
<Folder Include="Common\" />
</ItemGroup>
<ItemGroup>
<Reference Include="Nanite">
<HintPath>..\Nanite\bin\Debug\Nanite.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Core\Core.cs" />
<Compile Include="Control\Input\InputDevices.cs" />
<Compile Include="Control\Input\Device.cs" />
<Compile Include="Common\InputEventCode.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file
......@@ -13,6 +13,7 @@
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Runtime.InteropServices;
using Nanite.Exceptions;
......
......@@ -5,6 +5,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NaniteIoSample", "NaniteIoS
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nanite", "Nanite\Nanite.csproj", "{50B2EC05-312E-4E4B-8293-11626E7E7790}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GhostMainframe", "GhostMainframe\GhostMainframe.csproj", "{72FB1682-03B9-4D3F-9FA4-5E35AC682225}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
......@@ -19,8 +21,42 @@ Global
{50B2EC05-312E-4E4B-8293-11626E7E7790}.Debug|x86.Build.0 = Debug|Any CPU
{50B2EC05-312E-4E4B-8293-11626E7E7790}.Release|x86.ActiveCfg = Release|Any CPU
{50B2EC05-312E-4E4B-8293-11626E7E7790}.Release|x86.Build.0 = Release|Any CPU
{72FB1682-03B9-4D3F-9FA4-5E35AC682225}.Debug|x86.ActiveCfg = Debug|x86
{72FB1682-03B9-4D3F-9FA4-5E35AC682225}.Debug|x86.Build.0 = Debug|x86
{72FB1682-03B9-4D3F-9FA4-5E35AC682225}.Release|x86.ActiveCfg = Release|x86
{72FB1682-03B9-4D3F-9FA4-5E35AC682225}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = NaniteIoSample\NaniteIoSample.csproj
Policies = $0
$0.TextStylePolicy = $1
$1.inheritsSet = VisualStudio
$1.scope = text/plain
$1.FileWidth = 120
$1.inheritsScope = text/plain
$0.CSharpFormattingPolicy = $2
$2.IndentSwitchSection = True
$2.NewLinesForBracesInProperties = True
$2.NewLinesForBracesInAccessors = True
$2.NewLinesForBracesInAnonymousMethods = True
$2.NewLinesForBracesInControlBlocks = True
$2.NewLinesForBracesInAnonymousTypes = True
$2.NewLinesForBracesInObjectCollectionArrayInitializers = True
$2.NewLinesForBracesInLambdaExpressionBody = True
$2.NewLineForElse = True
$2.NewLineForCatch = True
$2.NewLineForFinally = True
$2.NewLineForMembersInObjectInit = True
$2.NewLineForMembersInAnonymousTypes = True
$2.NewLineForClausesInQuery = True
$2.SpacingAfterMethodDeclarationName = False
$2.SpaceAfterMethodCallName = False
$2.SpaceBeforeOpenSquareBracket = False
$2.inheritsSet = Mono
$2.inheritsScope = text/x-csharp
$2.scope = text/x-csharp
$0.StandardHeader = $3
$3.Text =
$3.IncludeInNewFiles = True
EndGlobalSection
EndGlobal
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment