private static void UpdateLeds(ICollection<KeyValuePair<int, CorsairLed>> ledsToUpdate) { ledsToUpdate = ledsToUpdate.Where(x => x.Value.Color != Color.Transparent).ToList(); if (!ledsToUpdate.Any()) return; // CUE seems to crash if 'CorsairSetLedsColors' is called with a zero length array int structSize = Marshal.SizeOf(typeof(_CorsairLedColor)); IntPtr ptr = Marshal.AllocHGlobal(structSize * ledsToUpdate.Count); IntPtr addPtr = new IntPtr(ptr.ToInt64()); foreach (KeyValuePair<int, CorsairLed> led in ledsToUpdate) { _CorsairLedColor color = new _CorsairLedColor { ledId = led.Key, r = led.Value.Color.R, g = led.Value.Color.G, b = led.Value.Color.B }; Marshal.StructureToPtr(color, addPtr, false); addPtr = new IntPtr(addPtr.ToInt64() + structSize); } _CUESDK.CorsairSetLedsColors(ledsToUpdate.Count, ptr); Marshal.FreeHGlobal(ptr); }
private void UpdateLeds(ICollection<LedUpateRequest> updateRequests) { updateRequests = updateRequests.Where(x => x.Color != CorsairColor.Transparent).ToList(); OnLedsUpdating(updateRequests); if (updateRequests.Any()) // CUE seems to crash if 'CorsairSetLedsColors' is called with a zero length array { int structSize = Marshal.SizeOf(typeof(_CorsairLedColor)); IntPtr ptr = Marshal.AllocHGlobal(structSize * updateRequests.Count); IntPtr addPtr = new IntPtr(ptr.ToInt64()); foreach (LedUpateRequest ledUpdateRequest in updateRequests) { _CorsairLedColor color = new _CorsairLedColor { ledId = (int)ledUpdateRequest.LedId, r = ledUpdateRequest.Color.R, g = ledUpdateRequest.Color.G, b = ledUpdateRequest.Color.B }; Marshal.StructureToPtr(color, addPtr, false); addPtr = new IntPtr(addPtr.ToInt64() + structSize); } _CUESDK.CorsairSetLedsColors(updateRequests.Count, ptr); Marshal.FreeHGlobal(ptr); } OnLedsUpdated(updateRequests); }