Skip to content

Commit e5d4852

Browse files
committed
Another pass on V2.
1 parent a45f0cf commit e5d4852

25 files changed

+486
-352
lines changed

Format/BaseIOConfig.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,11 @@ public BaseIOConfig(BaseIOConfig config)
4747
Parameter = config.Parameter;
4848
}
4949

50-
/// <summary>
51-
/// Constructor for an InputConfig
52-
/// </summary>
53-
/// <param name="byFlags">The flags defining the input</param>
54-
/// <param name="byVirtualKey">The value of the input</param>
55-
/// <param name="eKeyArgs">The input key arguments from user input</param>
56-
public BaseIOConfig(int nFlags, byte byVirtualKey, int nParameter)
57-
{
58-
Flags = nFlags;
59-
VirtualKey = byVirtualKey;
60-
Parameter = nParameter;
61-
}
62-
6350
public BaseIOConfig(Stream zStream)
6451
{
6552
Flags = StreamUtil.ReadIntFromStream(zStream);
6653
VirtualKey = StreamUtil.ReadByteFromStream(zStream);
67-
StreamUtil.ReadByteFromStream(zStream);
68-
StreamUtil.ReadByteFromStream(zStream);
69-
StreamUtil.ReadByteFromStream(zStream);
54+
StreamUtil.ReadBytesFromStream(zStream, 3);
7055
Parameter = StreamUtil.ReadIntFromStream(zStream);
7156
}
7257

@@ -84,6 +69,7 @@ public bool IsFlaggedAs(System.Enum eFlag)
8469

8570
public void SerializeToStream(Stream zStream)
8671
{
72+
// Due to the struct format on the C side the data is written as 3 32-bit ints
8773
StreamUtil.WriteIntToStream(zStream, Flags);
8874
zStream.WriteByte(VirtualKey);
8975
zStream.WriteByte(0);

Format/ConfigFileManager.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public class ConfigFileManager
3535
private static readonly int FILE_DATA_PREFIX = (int)0x0E0CA000;
3636
private static readonly int DATA_FORMAT_VERSION = (int)0x1;
3737

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>
3843
public void SaveFile(List<RemapEntry> listRemapEntries, string sFileName)
3944
{
4045
var zFileStream = new FileStream(sFileName, FileMode.Create, FileAccess.Write, FileShare.None);
@@ -48,6 +53,11 @@ public void SaveFile(List<RemapEntry> listRemapEntries, string sFileName)
4853
zFileStream.Close();
4954
}
5055

56+
/// <summary>
57+
/// Loads the remap entries from the specified file
58+
/// </summary>
59+
/// <param name="sFileName"></param>
60+
/// <returns></returns>
5161
public List<RemapEntry> LoadFile(string sFileName)
5262
{
5363
FileStream zFileStream = null;

Format/InputConfig.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public enum InputFlag
4646
Shift = 1 << 0,
4747
Control = 1 << 1,
4848
Alt = 1 << 2,
49-
//MAX = 1 << 7, // it's a byte! (aka if you need 8 you'll need more space)
49+
// supports up to 32 entries
5050
}
5151

5252
/// <summary>
@@ -55,14 +55,12 @@ public enum InputFlag
5555
/// <param name="byFlags">The flags defining the input</param>
5656
/// <param name="byVirtualKey">The value of the input</param>
5757
/// <param name="eKeyArgs">The input key arguments from user input</param>
58-
public InputConfig(int nFlags, byte byVirtualKey, KeyEventArgs eKeyArgs)
58+
public InputConfig(byte byVirtualKey, KeyEventArgs eKeyArgs)
5959
{
60-
Flags = nFlags;
6160
VirtualKey = byVirtualKey;
6261

6362
if (null != eKeyArgs)
6463
{
65-
#warning this is weird to have the flags further defined/or'd with the pkey args (likely only applied to outputs)
6664
Flags |= (int)(
6765
(eKeyArgs.Shift ? (int)InputFlag.Shift : (byte)0) |
6866
(eKeyArgs.Alt ? (int)InputFlag.Alt : (byte)0) |

Format/OutputConfig.cs

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
// SOFTWARE.
2323
////////////////////////////////////////////////////////////////////////////////
2424

25+
using System;
2526
using System.IO;
2627
using System.Windows.Forms;
2728
using Support.UI;
@@ -52,8 +53,9 @@ public enum OutputFlag
5253
Delay = 1 << 5,
5354
Toggle = 1 << 6,
5455
Down = 1 << 7,
55-
Up = 1 << 8
56-
//MAX = 1 << 32, // it's an int! (aka if you need 32 you'll need more space)
56+
Up = 1 << 8,
57+
// all ThreadKill ?
58+
// supports up to 32 entries
5759
}
5860

5961
public enum MouseButton
@@ -72,18 +74,17 @@ public enum MouseButton
7274
/// <param name="eKeyArgs">The input key arguments from user input</param>
7375
public OutputConfig(int nFlags, byte byVirtualKey, int nParameter, KeyEventArgs eKeyArgs)
7476
{
75-
#warning what's the chance of an input nFlags that is non-zero?
77+
Flags = nFlags;
78+
VirtualKey = byVirtualKey;
79+
Parameter = nParameter;
80+
7681
if (null != eKeyArgs)
7782
{
78-
nFlags |=
83+
Flags |=
7984
(eKeyArgs.Shift ? (int)OutputFlag.Shift : 0)
8085
| (eKeyArgs.Alt ? (int)OutputFlag.Alt : 0)
8186
| (eKeyArgs.Control ? (int)OutputFlag.Control : 0);
8287
}
83-
84-
Flags = nFlags;
85-
VirtualKey = byVirtualKey;
86-
Parameter = nParameter;
8788
}
8889

8990
/// <summary>
@@ -109,39 +110,53 @@ public OutputConfig(Stream zStream) : base(zStream) { }
109110
/// <returns>String representation of this definition</returns>
110111
public override string GetDescription()
111112
{
112-
// mouse (every other flag ignored)
113-
if (IsFlaggedAs(OutputFlag.MouseOut))
114-
{
115-
return "[" +
116-
(MouseButton)VirtualKey +
117-
getOutputDescriptionText("Mouse") +
118-
(IsFlaggedAs(OutputFlag.Toggle) ? "+Toggle" : string.Empty) +
119-
"]";
120-
}
121113
// delay (every other flag ignored)
122114
if (IsFlaggedAs(OutputFlag.Delay))
123115
{
124-
#warning todo: delay is currently not written to parameter
125116
return "[Delay: " + (int)Parameter + "]";
126117
}
118+
119+
if (IsFlaggedAs(OutputFlag.MouseOut))
120+
{
121+
return GetOutputDescriptionText((MouseButton)VirtualKey, "Mouse");
122+
}
123+
127124
// keyboard
128-
return "[" +
129-
(Keys)VirtualKey +
130-
getOutputDescriptionText("Key") +
131-
(IsFlaggedAs(OutputFlag.Shift) ? "+Shift" : string.Empty) +
132-
(IsFlaggedAs(OutputFlag.Alt) ? "+Alt" : string.Empty) +
133-
(IsFlaggedAs(OutputFlag.Control) ? "+Control" : string.Empty)+
134-
(IsFlaggedAs(OutputFlag.Toggle) ? "+Toggle" : string.Empty) +
135-
"]";
125+
return GetOutputDescriptionText((Keys)VirtualKey, "Key");
126+
}
127+
128+
/// <summary>
129+
/// Indicates if the config is assigned a valid action
130+
/// </summary>
131+
/// <returns></returns>
132+
public bool IsAssignedAction()
133+
{
134+
return IsFlaggedAs(OutputFlag.Down)
135+
|| IsFlaggedAs(OutputFlag.Up)
136+
|| IsFlaggedAs(OutputFlag.MouseOut)
137+
|| IsFlaggedAs(OutputFlag.Delay)
138+
|| IsFlaggedAs(OutputFlag.DoNothing);
136139
}
137140

138-
private string getOutputDescriptionText(string sPrefix)
141+
/// <summary>
142+
/// Gets the output description for the up/down/press based on the state of the flags
143+
/// </summary>
144+
/// <param name="sActionPrefix">The prefix indicating the type of input</param>
145+
/// <returns></returns>
146+
private string GetOutputDescriptionText(Enum eInputId, string sActionPrefix)
139147
{
140-
return (IsFlaggedAs(OutputFlag.Down) && IsFlaggedAs(OutputFlag.Up)
141-
? ":Press"
142-
: ((IsFlaggedAs(OutputFlag.Down) ? ":{0}Down".FormatString(sPrefix) : string.Empty) +
143-
(IsFlaggedAs(OutputFlag.Up) ? ":{0}Up".FormatString(sPrefix) : string.Empty))
144-
);
148+
return "[" +
149+
(Keys)VirtualKey +
150+
(IsFlaggedAs(OutputFlag.Down) && IsFlaggedAs(OutputFlag.Up)
151+
? ":Press"
152+
: ((IsFlaggedAs(OutputFlag.Down) ? ":{0}Down".FormatString(sActionPrefix) : string.Empty) +
153+
(IsFlaggedAs(OutputFlag.Up) ? ":{0}Up".FormatString(sActionPrefix) : string.Empty))
154+
) +
155+
(IsFlaggedAs(OutputFlag.Shift) ? "+Shift" : string.Empty) +
156+
(IsFlaggedAs(OutputFlag.Alt) ? "+Alt" : string.Empty) +
157+
(IsFlaggedAs(OutputFlag.Control) ? "+Control" : string.Empty) +
158+
(IsFlaggedAs(OutputFlag.Toggle) ? "+Toggle" : string.Empty) +
159+
"]";
145160
}
146161
}
147162
}

Format/RemapEntry.cs

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,16 @@ namespace KeyCap.Format
3434
/// </summary>
3535
public class RemapEntry
3636
{
37-
#warning remove the memory stream usage... just confusing
38-
/// <summary>
39-
/// Format is a byte[] [flags][value][count of outputs]([flags][value])([flags][value])([flags][value])...
40-
/// </summary>
41-
// private readonly MemoryStream m_zStream = new MemoryStream();
42-
4337
private InputConfig InputConfig { get; set; }
4438
private List<OutputConfig> OutputConfigs { get; set; }
4539

46-
private readonly int m_nHash;
47-
48-
/// <summary>
49-
/// The byte indicies for the stream representation of the class
50-
/// </summary>
51-
public enum KeyDefinitionIndices
40+
public int OutputConfigCount
5241
{
53-
Flags,
54-
Value,
55-
Count // not applicable to the output definitions
42+
get { return OutputConfigs == null ? 0 : OutputConfigs.Count; }
5643
}
5744

45+
private readonly int m_nHash;
46+
5847
/// <summary>
5948
/// Constructor based on input/output definitions
6049
/// </summary>
@@ -64,13 +53,7 @@ public RemapEntry(InputConfig zInputConfig, OutputConfig zOutputConfig)
6453
{
6554
InputConfig = zInputConfig;
6655
OutputConfigs = new List<OutputConfig>(new [] {zOutputConfig});
67-
#warning move hash calc to method
68-
m_nHash = (int)(zInputConfig.Flags & 0xFF) + (int)((zInputConfig.VirtualKey & 0xFF) << 8);
69-
#if false
70-
zInputConfig.SerializeToStream(m_zStream);
71-
m_zStream.WriteByte(1);
72-
zOutputConfig.SerializeToStream(m_zStream);
73-
#endif
56+
m_nHash = CalculateHashCode(InputConfig);
7457
}
7558

7659
/// <summary>
@@ -88,6 +71,7 @@ public RemapEntry(FileStream zFileStream)
8871
throw new Exception("Output definition length must be > 0. Invalid File!");
8972
}
9073

74+
// TODO: comment on why this is
9175
zFileStream.ReadByte();
9276
zFileStream.ReadByte();
9377
zFileStream.ReadByte();
@@ -113,10 +97,6 @@ public RemapEntry(FileStream zFileStream)
11397
/// <returns></returns>
11498
public bool AppendOutputConfig(OutputConfig zOutputConfig)
11599
{
116-
if (OutputConfigs.Count == 0xff)
117-
{
118-
return false;
119-
}
120100
OutputConfigs.Add(zOutputConfig);
121101
return true;
122102
}
@@ -126,6 +106,7 @@ public byte[] SerializeToBytes()
126106
var zStream = new MemoryStream();
127107
InputConfig.SerializeToStream(zStream);
128108
zStream.WriteByte((byte)OutputConfigs.Count);
109+
// TODO: comment on padding
129110
zStream.WriteByte(0);
130111
zStream.WriteByte(0);
131112
zStream.WriteByte(0);
@@ -159,10 +140,18 @@ public string GetOutputString()
159140
return zBuilder.ToString();
160141
}
161142

162-
#warning why is this necessary? (double definition detection it appears... so the same input isn't reused)
143+
/// <summary>
144+
/// Returns the hash code of this remap entry
145+
/// </summary>
146+
/// <returns></returns>
163147
public override int GetHashCode()
164148
{
165149
return m_nHash;
166150
}
151+
152+
protected static int CalculateHashCode(InputConfig zInputConfig)
153+
{
154+
return (int)(zInputConfig.Flags & 0xFF) + (int)((zInputConfig.VirtualKey & 0xFF) << 8);
155+
}
167156
}
168157
}

0 commit comments

Comments
 (0)