|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Drawing; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using System.Windows.Forms; |
| 10 | +using Aurora.Devices.RGBNet; |
| 11 | +using Aurora.Settings; |
| 12 | +using Microsoft.Win32; |
| 13 | +using UniwillSDKDLL; |
| 14 | + |
| 15 | +namespace Aurora.Devices.Uniwill |
| 16 | +{ |
| 17 | + enum GAMECENTERTYPE |
| 18 | + { |
| 19 | + NONE = 0, |
| 20 | + GAMINGTCENTER = 1, |
| 21 | + CONTROLCENTER = 2 |
| 22 | + } |
| 23 | + |
| 24 | + public class UniwillDevice : Device |
| 25 | + { |
| 26 | + // Generic Variables |
| 27 | + private string devicename = "Uniwill"; |
| 28 | + private bool isInitialized = false; |
| 29 | + |
| 30 | + private Stopwatch watch = new Stopwatch(); |
| 31 | + private long lastUpdateTime = 0; |
| 32 | + |
| 33 | + System.Timers.Timer regTimer; |
| 34 | + const string Root = "HKEY_LOCAL_MACHINE"; |
| 35 | + const string subkey = @"SOFTWARE\OEM\Aurora"; |
| 36 | + const string keyName = Root + "\\" + subkey; |
| 37 | + int SwitchOn = 0; |
| 38 | + |
| 39 | + private AuroraInterface keyboard = null; |
| 40 | + |
| 41 | + GAMECENTERTYPE GamingCenterType = 0; |
| 42 | + |
| 43 | + float brightness = 1f; |
| 44 | + |
| 45 | + public UniwillDevice() |
| 46 | + { |
| 47 | + devicename = KeyboardFactory.GetOEMName(); |
| 48 | + ChoiceGamingCenter(); |
| 49 | + } |
| 50 | + |
| 51 | + private void ChoiceGamingCenter() |
| 52 | + { |
| 53 | + GamingCenterType = CheckGC(); |
| 54 | + |
| 55 | + if (GamingCenterType == GAMECENTERTYPE.GAMINGTCENTER) |
| 56 | + { |
| 57 | + regTimer = new System.Timers.Timer(); |
| 58 | + regTimer.Interval = 300; |
| 59 | + regTimer.Elapsed += OnRegChanged; |
| 60 | + regTimer.Stop(); |
| 61 | + regTimer.Start(); |
| 62 | + |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private GAMECENTERTYPE CheckGC() |
| 67 | + { |
| 68 | + try |
| 69 | + { |
| 70 | + int Control = (int)Registry.GetValue(keyName, "AuroraSwitch", null); |
| 71 | + GamingCenterType = GAMECENTERTYPE.GAMINGTCENTER; |
| 72 | + SwitchOn = Control; |
| 73 | + } |
| 74 | + catch |
| 75 | + { |
| 76 | + GamingCenterType = GAMECENTERTYPE.NONE; |
| 77 | + SwitchOn = 0; |
| 78 | + } |
| 79 | + return GamingCenterType; |
| 80 | + } |
| 81 | + |
| 82 | + public bool CheckGCPower() |
| 83 | + { |
| 84 | + if (GamingCenterType == GAMECENTERTYPE.GAMINGTCENTER) |
| 85 | + { |
| 86 | + int Control = (int)Registry.GetValue(keyName, "AuroraSwitch", 0); |
| 87 | + return Control != 0; |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + return true; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private void OnRegChanged(object sender, EventArgs e) |
| 96 | + { |
| 97 | + int newSwtich = (int)Registry.GetValue(keyName, "AuroraSwitch", 0); |
| 98 | + if (SwitchOn != newSwtich) |
| 99 | + { |
| 100 | + SwitchOn = newSwtich; |
| 101 | + if (CheckGCPower()) |
| 102 | + { |
| 103 | + Initialize(); |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + bRefreshOnce = true; |
| 108 | + isInitialized = false; |
| 109 | + Shutdown(); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public string GetDeviceName() |
| 115 | + { |
| 116 | + return devicename; |
| 117 | + } |
| 118 | + |
| 119 | + public string GetDeviceDetails() |
| 120 | + { |
| 121 | + if (isInitialized) |
| 122 | + { |
| 123 | + return devicename + ": Initialized"; |
| 124 | + } |
| 125 | + else |
| 126 | + { |
| 127 | + return devicename + ": Not initialized"; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public bool Initialize() |
| 132 | + { |
| 133 | + if (!isInitialized && CheckGCPower()) |
| 134 | + { |
| 135 | + try |
| 136 | + { |
| 137 | + keyboard = KeyboardFactory.CreateHIDDevice("hidkeyboard"); |
| 138 | + if (keyboard != null) |
| 139 | + { |
| 140 | + bRefreshOnce = true; |
| 141 | + isInitialized = true; |
| 142 | + //SetBrightness(); |
| 143 | + return true; |
| 144 | + } |
| 145 | + |
| 146 | + isInitialized = false; |
| 147 | + return false; |
| 148 | + } |
| 149 | + catch |
| 150 | + { |
| 151 | + Debug.WriteLine("Uniwill device error!"); |
| 152 | + } |
| 153 | + // Mark Initialized = FALSE |
| 154 | + isInitialized = false; |
| 155 | + return false; |
| 156 | + } |
| 157 | + |
| 158 | + return isInitialized; |
| 159 | + } |
| 160 | + |
| 161 | + public void Shutdown() |
| 162 | + { |
| 163 | + if (this.IsInitialized()) |
| 164 | + { |
| 165 | + if (CheckGCPower()) |
| 166 | + { |
| 167 | + keyboard?.release(); |
| 168 | + } |
| 169 | + |
| 170 | + bRefreshOnce = true; |
| 171 | + isInitialized = false; |
| 172 | + |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + public void Reset() |
| 177 | + { |
| 178 | + if (this.IsInitialized()) |
| 179 | + { |
| 180 | + if (CheckGCPower()) |
| 181 | + { |
| 182 | + keyboard?.release(); |
| 183 | + } |
| 184 | + |
| 185 | + bRefreshOnce = true; |
| 186 | + isInitialized = false; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + public bool Reconnect() |
| 191 | + { |
| 192 | + throw new NotImplementedException(); |
| 193 | + } |
| 194 | + |
| 195 | + public bool IsInitialized() |
| 196 | + { |
| 197 | + return isInitialized; |
| 198 | + } |
| 199 | + |
| 200 | + public bool IsConnected() |
| 201 | + { |
| 202 | + return isInitialized; |
| 203 | + } |
| 204 | + |
| 205 | + bool bRefreshOnce = true; // This is used to refresh effect between Row-Type and Fw-Type change or layout light level change |
| 206 | + |
| 207 | + public bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, DoWorkEventArgs e, bool forced = false) |
| 208 | + { |
| 209 | + if (e.Cancel) return false; |
| 210 | + |
| 211 | + //Alpha necessary for Global Brightness modifier |
| 212 | + var adjustedColors = keyColors.Select(kc => AdjustBrightness(kc)); |
| 213 | + |
| 214 | + bool ret = keyboard?.SetEffect(0x32, 0x00, bRefreshOnce, adjustedColors, e) ?? false; |
| 215 | + |
| 216 | + bRefreshOnce = false; |
| 217 | + |
| 218 | + return ret; |
| 219 | + } |
| 220 | + |
| 221 | + public bool UpdateDevice(DeviceColorComposition colorComposition, DoWorkEventArgs e, bool forced = false) |
| 222 | + { |
| 223 | + watch.Restart(); |
| 224 | + |
| 225 | + bool update_result = UpdateDevice(colorComposition.keyColors, e, forced); |
| 226 | + |
| 227 | + watch.Stop(); |
| 228 | + lastUpdateTime = watch.ElapsedMilliseconds; |
| 229 | + |
| 230 | + return update_result; |
| 231 | + } |
| 232 | + |
| 233 | + private KeyValuePair<DeviceKeys, Color> AdjustBrightness(KeyValuePair<DeviceKeys, Color> kc) |
| 234 | + { |
| 235 | + var newEntry = new KeyValuePair<DeviceKeys, Color>(kc.Key, Color.FromArgb(255, Utils.ColorUtils.MultiplyColorByScalar(kc.Value, (kc.Value.A / 255.0D) * brightness))); |
| 236 | + kc = newEntry; |
| 237 | + return kc; |
| 238 | + } |
| 239 | + |
| 240 | + // Device Status Methods |
| 241 | + public bool IsKeyboardConnected() |
| 242 | + { |
| 243 | + return isInitialized; |
| 244 | + } |
| 245 | + |
| 246 | + public bool IsPeripheralConnected() |
| 247 | + { |
| 248 | + return isInitialized; |
| 249 | + } |
| 250 | + |
| 251 | + public string GetDeviceUpdatePerformance() |
| 252 | + { |
| 253 | + return (isInitialized ? lastUpdateTime + " ms" : ""); |
| 254 | + } |
| 255 | + |
| 256 | + public VariableRegistry GetRegisteredVariables() |
| 257 | + { |
| 258 | + return new VariableRegistry(); |
| 259 | + } |
| 260 | + } |
| 261 | +} |
0 commit comments