Skip to content

Commit b0fd74f

Browse files
committed
Merge branch 'v2'
2 parents 074f0ee + 5644ad5 commit b0fd74f

40 files changed

+2468
-1097
lines changed

Format/BaseIOConfig.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
// The MIT License (MIT)
3+
//
4+
// Copyright (c) 2019 Tim Stair
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
////////////////////////////////////////////////////////////////////////////////
24+
25+
using System.IO;
26+
using KeyCap.Util;
27+
28+
namespace KeyCap.Format
29+
{
30+
public abstract class BaseIOConfig
31+
{
32+
public int Flags { get; set; }
33+
// https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
34+
public byte VirtualKey { get; set; }
35+
public int Parameter { get; set; }
36+
37+
public BaseIOConfig() { }
38+
39+
/// <summary>
40+
/// Clone constructor
41+
/// </summary>
42+
/// <param name="config"></param>
43+
public BaseIOConfig(BaseIOConfig config)
44+
{
45+
Flags = config.Flags;
46+
VirtualKey = config.VirtualKey;
47+
Parameter = config.Parameter;
48+
}
49+
50+
public BaseIOConfig(Stream zStream)
51+
{
52+
Flags = StreamUtil.ReadIntFromStream(zStream);
53+
VirtualKey = StreamUtil.ReadByteFromStream(zStream);
54+
StreamUtil.ReadBytesFromStream(zStream, 3);
55+
Parameter = StreamUtil.ReadIntFromStream(zStream);
56+
}
57+
58+
/// <summary>
59+
/// Determines if the input type is flagged (as the there are multiple flags)
60+
/// </summary>
61+
/// <param name="eFlag">Flag to check for</param>
62+
/// <returns>true if the flag bit is 1, false otherwise</returns>
63+
public bool IsFlaggedAs(System.Enum eFlag)
64+
{
65+
return BitUtil.IsFlagged(Flags, eFlag);
66+
}
67+
68+
public abstract string GetDescription();
69+
70+
public void SerializeToStream(Stream zStream)
71+
{
72+
// Due to the struct format on the C side the data is written as 3 32-bit ints
73+
StreamUtil.WriteIntToStream(zStream, Flags);
74+
zStream.WriteByte(VirtualKey);
75+
zStream.WriteByte(0);
76+
zStream.WriteByte(0);
77+
zStream.WriteByte(0);
78+
StreamUtil.WriteIntToStream(zStream, Parameter);
79+
}
80+
}
81+
}

Format/ConfigFileManager.cs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
// The MIT License (MIT)
3+
//
4+
// Copyright (c) 2019 Tim Stair
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
////////////////////////////////////////////////////////////////////////////////
24+
25+
using System;
26+
using System.Collections.Generic;
27+
using System.IO;
28+
using System.Windows.Forms;
29+
using Support.UI;
30+
31+
namespace KeyCap.Format
32+
{
33+
public class ConfigFileManager
34+
{
35+
private static readonly int FILE_DATA_PREFIX = (int)0x0E0CA000;
36+
private static readonly int DATA_FORMAT_VERSION = (int)0x1;
37+
38+
/// <summary>
39+
/// Saves the remap entries to a versioned file format
40+
/// </summary>
41+
/// <param name="listRemapEntries">The entries to persist</param>
42+
/// <param name="sFileName">The name of the file to save to</param>
43+
public void SaveFile(List<RemapEntry> listRemapEntries, string sFileName)
44+
{
45+
var zFileStream = new FileStream(sFileName, FileMode.Create, FileAccess.Write, FileShare.None);
46+
StreamUtil.WriteIntToStream(zFileStream, FILE_DATA_PREFIX);
47+
StreamUtil.WriteIntToStream(zFileStream, DATA_FORMAT_VERSION);
48+
listRemapEntries.ForEach(zEntry =>
49+
{
50+
var arrayBytes = zEntry.SerializeToBytes();
51+
zFileStream.Write(arrayBytes, 0, arrayBytes.Length);
52+
});
53+
zFileStream.Close();
54+
}
55+
56+
/// <summary>
57+
/// Loads the remap entries from the specified file
58+
/// </summary>
59+
/// <param name="sFileName"></param>
60+
/// <returns></returns>
61+
public List<RemapEntry> LoadFile(string sFileName)
62+
{
63+
FileStream zFileStream = null;
64+
var listConfigs = new List<RemapEntry>();
65+
try
66+
{
67+
zFileStream = new FileStream(sFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
68+
var nPrefix = StreamUtil.ReadIntFromStream(zFileStream);
69+
if (nPrefix != FILE_DATA_PREFIX)
70+
{
71+
throw new Exception("{} does not have the correct data prefix. This is likely an unsupported format.".FormatString(sFileName));
72+
}
73+
74+
var nDataFormatVersion = StreamUtil.ReadIntFromStream(zFileStream);
75+
if (nDataFormatVersion != DATA_FORMAT_VERSION)
76+
{
77+
throw new Exception("{} indicates an unsupported data format.".FormatString(sFileName));
78+
}
79+
80+
while (zFileStream.Position < zFileStream.Length)
81+
{
82+
listConfigs.Add(new RemapEntry(zFileStream));
83+
}
84+
}
85+
catch (Exception e)
86+
{
87+
MessageBox.Show(e.Message);
88+
}
89+
finally
90+
{
91+
zFileStream?.Close();
92+
}
93+
94+
return listConfigs;
95+
}
96+
}
97+
}

Format/IODefinition.cs

Lines changed: 0 additions & 150 deletions
This file was deleted.

0 commit comments

Comments
 (0)