Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using HHKBKeymapTool.Models.Definitions;
using HHKBKeymapTool.Properties;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace HHKBKeymapTool.Models
{
public class KeyboardLibrary
{
private Dictionary<string, KeyboardTypeData> keyboardTypeDatas = new Dictionary<string, KeyboardTypeData>();
private IIOManager ioManager;
private bool isLoaded;
private KeymapData keymapDataCache;
private HashSet<int> allUsageIdCache;
public KeyboardLibrary(IIOManager ioManager)
{
this.ioManager = ioManager;
}
private void LoadDatalist()
{
if (this.isLoaded)
return;
this.isLoaded = true;
try
{
this.keyboardTypeDatas = JsonConvert.DeserializeObject<Dictionary<string, KeyboardTypeData>>(this.ioManager.LoadFileAsString("KeyboardDatalist.json"));
}
catch (Exception ex)
{
throw new EnvironmentBrokenException("KeyboardDatalist.json is not found");
}
}
private void Swap<T>(ref T lhs, ref T rhs)
{
T obj = lhs;
lhs = rhs;
rhs = obj;
}
private KeymapData LoadDefaultKeymapData(string typeNumber, bool[] dipSwitch)
{
string str1 = "pack://application:,,,/Resources/KeymapDatas/";
KeyboardLayoutType keyboardLayoutType = this.GetKeyboardLayoutType(typeNumber);
string str2;
if (keyboardLayoutType == KeyboardLayoutType.English)
{
string str3 = str1 + "English";
str2 = !dipSwitch[0] || !dipSwitch[1] ? (!dipSwitch[0] ? (!dipSwitch[1] ? str3 + "00" : str3 + "01") : str3 + "10") : str3 + "00";
}
else
{
string str3 = str1 + "Japanese";
str2 = !dipSwitch[0] ? str3 + "0" : str3 + "1";
}
string uri = str2 + ".json";
KeymapData keymapData;
try
{
keymapData = JsonConvert.DeserializeObject<KeymapData>(this.ioManager.LoadResourceFileAsString(uri));
}
catch (Exception ex)
{
throw new EnvironmentBrokenException(string.Format("{0} is not found", (object) uri));
}
if (this.GetKeyboardSeries(typeNumber) == KeyboardSeries.HHKB_Classic)
{
keymapData.WithFn[7] = keymapData.Normal[7];
keymapData.WithFn[31] = keymapData.Normal[31];
keymapData.WithFn[44] = keymapData.Normal[44];
}
if (keyboardLayoutType == KeyboardLayoutType.English)
{
if (dipSwitch[2])
keymapData.Normal[32] = 42;
if (dipSwitch[3])
keymapData.Normal[4] = keymapData.WithFn[4] = 1;
if (dipSwitch[4])
{
this.Swap<int>(ref keymapData.Normal[1], ref keymapData.Normal[2]);
this.Swap<int>(ref keymapData.WithFn[1], ref keymapData.WithFn[2]);
this.Swap<int>(ref keymapData.Normal[4], ref keymapData.Normal[5]);
this.Swap<int>(ref keymapData.WithFn[4], ref keymapData.WithFn[5]);
}
}
else
{
if (dipSwitch[1])
{
keymapData.Normal[13] = keymapData.WithFn[13] = 224;
keymapData.Normal[40] = keymapData.WithFn[40] = 57;
}
if (dipSwitch[2])
this.Swap<int>(ref keymapData.Normal[55], ref keymapData.WithFn[55]);
if (dipSwitch[3])
{
this.Swap<int>(ref keymapData.Normal[1], ref keymapData.WithFn[1]);
this.Swap<int>(ref keymapData.Normal[2], ref keymapData.WithFn[2]);
this.Swap<int>(ref keymapData.Normal[3], ref keymapData.WithFn[3]);
this.Swap<int>(ref keymapData.Normal[15], ref keymapData.WithFn[15]);
}
if (dipSwitch[4])
{
this.Swap<int>(ref keymapData.Normal[10], ref keymapData.Normal[11]);
this.Swap<int>(ref keymapData.WithFn[10], ref keymapData.WithFn[11]);
}
}
return keymapData;
}
public KeymapData GetDefaultKeymapData(string typeNumber, bool[] dipSwitch)
{
this.LoadDatalist();
if (this.keymapDataCache == null)
this.keymapDataCache = this.LoadDefaultKeymapData(typeNumber, dipSwitch);
return this.keymapDataCache;
}
public void SetManualKeyboardLayoutForDebug(
string typeNumber,
KeyboardLayoutType keyboardLayoutType)
{
if (!this.keyboardTypeDatas.ContainsKey(typeNumber))
return;
this.keyboardTypeDatas[typeNumber].layoutType = keyboardLayoutType;
}
public void SetManualKeyboardLayoutNameForDebug(
string typeNumber,
KeyboardLayoutTypeName keyboardLayoutTypeName)
{
if (!this.keyboardTypeDatas.ContainsKey(typeNumber))
return;
this.keyboardTypeDatas[typeNumber].layoutTypeName = keyboardLayoutTypeName;
}
public KeyboardLayoutType GetKeyboardLayoutType(string typeNumber)
{
this.LoadDatalist();
if (this.keyboardTypeDatas.ContainsKey(typeNumber))
return this.keyboardTypeDatas[typeNumber].layoutType;
throw new UnknownTypeNumberException("TypeNumber:" + typeNumber + " is unknown.");
}
public KeyboardColorType GetKeyboardColor(string typeNumber)
{
this.LoadDatalist();
if (this.keyboardTypeDatas.ContainsKey(typeNumber))
return this.keyboardTypeDatas[typeNumber].colorType;
throw new UnknownTypeNumberException("TypeNumber:" + typeNumber + " is unknown.");
}
public bool IsKeymapChangeableFor(string typeNumber)
{
this.LoadDatalist();
if (this.keyboardTypeDatas.ContainsKey(typeNumber))
return this.keyboardTypeDatas[typeNumber].isKeymapChangeable;
throw new UnknownTypeNumberException("TypeNumber:" + typeNumber + " is unknown.");
}
public KeyboardMode GetKeyboardMode(string typeNumber, bool[] dipswitch)
{
this.LoadDatalist();
if (this.GetKeyboardLayoutType(typeNumber) == KeyboardLayoutType.Japanese)
return !dipswitch[0] ? KeyboardMode.HHK_Mode : KeyboardMode.Mac_Mode;
if (!dipswitch[0] && !dipswitch[1])
return KeyboardMode.HHK_Mode;
if (!dipswitch[0] && dipswitch[1])
return KeyboardMode.Mac_Mode;
return dipswitch[0] && !dipswitch[1] ? KeyboardMode.Lite_Mode : KeyboardMode.Secret_Mode;
}
public string GetKeyboardName(string typeNumber)
{
this.LoadDatalist();
if (!this.keyboardTypeDatas.ContainsKey(typeNumber))
throw new UnknownTypeNumberException("TypeNumber:" + typeNumber + " is unknown.");
string str = "";
switch (this.keyboardTypeDatas[typeNumber].series)
{
case KeyboardSeries.HHKB_Classic:
str += ConstDefinition.KeyboardSeriesNameForClassic;
break;
case KeyboardSeries.HHKB_Hybrid:
str += ConstDefinition.KeyboardSeriesNameForHYBRID;
break;
case KeyboardSeries.HHKB_Hybrid_TypeS:
str += ConstDefinition.KeyboardSeriesNameForHYBRIDTypeS;
break;
}
switch (this.keyboardTypeDatas[typeNumber].layoutTypeName)
{
case KeyboardLayoutTypeName.Japanese:
str += Resources.ToolUtility_KeyboardLayoutTypeName_Japanese;
break;
case KeyboardLayoutTypeName.English:
str += Resources.ToolUtility_KeyboardLayoutTypeName_English;
break;
case KeyboardLayoutTypeName.EnglishWithoutPrinted:
str += Resources.ToolUtility_KeyboardLayoutTypeName_EnglishWithoutPrinted;
break;
}
if (0 < this.keyboardTypeDatas[typeNumber].postfix.Length)
str = str + " " + this.keyboardTypeDatas[typeNumber].postfix;
return str;
}
public KeyboardSeries GetKeyboardSeries(string typeNumber)
{
this.LoadDatalist();
if (this.keyboardTypeDatas.ContainsKey(typeNumber))
return this.keyboardTypeDatas[typeNumber].series;
throw new UnknownTypeNumberException("TypeNumber:" + typeNumber + " is unknown.");
}
public int GetWithFnKeyForSpecialKey(int usageId)
{
this.LoadDatalist();
switch (usageId)
{
case 20:
return 0;
case 27:
return 27;
case 29:
return 29;
case 224:
return 0;
case 229:
return 0;
default:
return -1;
}
}
public bool IsSpecialKey(int usageId)
{
return 0 <= this.GetWithFnKeyForSpecialKey(usageId);
}
public bool IsInstallableKeyboard(string keyboardTypeNumber, string firmTypeNumber)
{
this.LoadDatalist();
if (this.keyboardTypeDatas.ContainsKey(keyboardTypeNumber))
return this.keyboardTypeDatas[keyboardTypeNumber].firmTypeNumber == firmTypeNumber;
throw new UnknownTypeNumberException("TypeNumber:" + keyboardTypeNumber + " is unknown.");
}
public int GetFirmSize(string keyboardTypeNumber)
{
this.LoadDatalist();
if (this.keyboardTypeDatas.ContainsKey(keyboardTypeNumber))
return this.keyboardTypeDatas[keyboardTypeNumber].firmDataSize;
throw new UnknownTypeNumberException("TypeNumber:" + keyboardTypeNumber + " is unknown.");
}
public bool IsMappableKey(int usageId, string typeNumber)
{
return this.GetAllUsageId(typeNumber).Contains(usageId);
}
public int[][] GetSortedJapaneseUsageIdList()
{
return new int[5][]
{
new int[22]
{
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
58,
59,
60,
61,
62,
63,
64,
65,
66,
67,
68,
69
},
new int[26]
{
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29
},
new int[26]
{
226,
230,
42,
57,
83,
224,
228,
76,
235,
77,
40,
41,
1,
74,
73,
136,
234,
78,
75,
102,
70,
72,
71,
225,
229,
120
},
new int[26]
{
43,
232,
233,
45,
46,
47,
48,
50,
51,
52,
54,
55,
56,
135,
137,
84,
85,
86,
87,
82,
80,
79,
81,
227,
231,
53
},
new int[7]{ 139, 138, 44, 101, 144, 145, 0 }
};
}
public int[][] GetSortedEnglishUsageIdList()
{
return new int[5][]
{
new int[22]
{
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
58,
59,
60,
61,
62,
63,
64,
65,
66,
67,
68,
69
},
new int[26]
{
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29
},
new int[26]
{
226,
230,
42,
57,
224,
228,
83,
76,
235,
77,
88,
41,
1,
74,
73,
234,
78,
75,
102,
70,
72,
40,
71,
225,
229,
120
},
new int[26]
{
43,
232,
233,
45,
46,
53,
47,
48,
51,
52,
54,
55,
56,
49,
50,
100,
84,
85,
86,
87,
82,
80,
79,
81,
227,
231
},
new int[5]{ 44, 101, 139, 138, 0 }
};
}
private HashSet<int> LoadAllUsageId(string typeNumber)
{
HashSet<int> intSet = new HashSet<int>();
if (this.GetKeyboardLayoutType(typeNumber) == KeyboardLayoutType.Japanese)
{
KeymapData keymapData1 = this.LoadDefaultKeymapData(typeNumber, new bool[6]);
foreach (int num in keymapData1.Normal)
intSet.Add(num);
foreach (int num in keymapData1.WithFn)
intSet.Add(num);
string typeNumber1 = typeNumber;
bool[] dipSwitch = new bool[6];
dipSwitch[0] = true;
KeymapData keymapData2 = this.LoadDefaultKeymapData(typeNumber1, dipSwitch);
foreach (int num in keymapData2.Normal)
intSet.Add(num);
foreach (int num in keymapData2.WithFn)
intSet.Add(num);
foreach (int[] sortedJapaneseUsageId in this.GetSortedJapaneseUsageIdList())
{
foreach (int num in sortedJapaneseUsageId)
intSet.Add(num);
}
}
else
{
KeymapData keymapData1 = this.LoadDefaultKeymapData(typeNumber, new bool[6]);
foreach (int num in keymapData1.Normal)
intSet.Add(num);
foreach (int num in keymapData1.WithFn)
intSet.Add(num);
string typeNumber1 = typeNumber;
bool[] dipSwitch1 = new bool[6];
dipSwitch1[0] = true;
KeymapData keymapData2 = this.LoadDefaultKeymapData(typeNumber1, dipSwitch1);
foreach (int num in keymapData2.Normal)
intSet.Add(num);
foreach (int num in keymapData2.WithFn)
intSet.Add(num);
string typeNumber2 = typeNumber;
bool[] dipSwitch2 = new bool[6];
dipSwitch2[1] = true;
KeymapData keymapData3 = this.LoadDefaultKeymapData(typeNumber2, dipSwitch2);
foreach (int num in keymapData3.Normal)
intSet.Add(num);
foreach (int num in keymapData3.WithFn)
intSet.Add(num);
foreach (int[] sortedEnglishUsageId in this.GetSortedEnglishUsageIdList())
{
foreach (int num in sortedEnglishUsageId)
intSet.Add(num);
}
}
return intSet;
}
public HashSet<int> GetAllUsageId(string typeNumber)
{
if (this.allUsageIdCache == null)
this.allUsageIdCache = this.LoadAllUsageId(typeNumber);
return this.allUsageIdCache;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment