Skip to content

Commit

Permalink
Fixed crash
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelmayer-dev committed Oct 16, 2022
1 parent bf658a4 commit e45693f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 39 deletions.
3 changes: 1 addition & 2 deletions Macro-Deck-Stream-Deck-Connector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<LangVersion>8</LangVersion>
<Nullable>enable</Nullable>
<PackageVersion>1.2.0</PackageVersion>
</PropertyGroup>
<ItemGroup>
<None Remove=".gitignore" />
Expand All @@ -32,7 +31,7 @@

<PropertyGroup>
<DebugType>embedded</DebugType>
<Version>1.2.0</Version>
<Version>1.2.1</Version>
</PropertyGroup>

</Project>
14 changes: 9 additions & 5 deletions MacroDeckClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,16 @@ internal void Close()
button?.Dispose();
}
_websocketClient?.Dispose();
if (_frameUpdateTimer != null)
try
{
_frameUpdateTimer.Stop();
_frameUpdateTimer.Dispose();
}
catch
{
// ignored
}

_connectedDevice?.Close();
}

Expand Down Expand Up @@ -126,23 +131,22 @@ private async Task HandleMessageAsync(string message)
private bool _buttonsUpdating;
private void UpdateAllButtons()
{
if (_connectedDevice == null || _buttonsUpdating) return;
if (_buttonsUpdating) return;
_buttonsUpdating = true;
for (var row = 0; row < _connectedDevice?.Rows; row++)
{
for (var col = 0; col < _connectedDevice?.Columns; col++)
{
var keyId = row * _connectedDevice.Columns + col;
var actionButton = _buttons.ToArray().FirstOrDefault(x => x.Column == col && x.Row == row);
var actionButton = _buttons.ToArray().FirstOrDefault(x => x != null && x.Column == col && x.Row == row);
UpdateButton(keyId, actionButton);
}
}
_buttonsUpdating = false;
}

private void UpdateButton(int id, ActionButtonModel actionButton)
private void UpdateButton(int id, ActionButtonModel? actionButton)
{
if (_connectedDevice == null) return;
if (actionButton != null)
{
actionButton.FrameTick();
Expand Down
26 changes: 15 additions & 11 deletions Models/ActionButtonModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public void Dispose()

private string _labelBase64;

private Image _iconImage;
private Image? _iconImage;

private Bitmap _labelBitmap;
private Bitmap? _labelBitmap;

private int _frameIndex = 0;

Expand All @@ -96,8 +96,11 @@ public string IconBase64
if (_iconImage.RawFormat.Guid == ImageFormat.Gif.Guid)
{
var item = _iconImage.GetPropertyItem(0x5100);
_frameDelay = (item.Value[0] + item.Value[1] * 256) * 10;
_frameCount = _iconImage.GetFrameCount(FrameDimension.Time);
if (item?.Value != null)
{
_frameDelay = (item.Value[0] + item.Value[1] * 256) * 10;
_frameCount = _iconImage.GetFrameCount(FrameDimension.Time);
}
}
_frameIndex = 0;
UpdateCurrentFrame();
Expand Down Expand Up @@ -165,6 +168,7 @@ private void UpdateCurrentFrame()
public KeyBitmap? GetCurrentFrame(int size)
{
if (IsDisposed) return null;
const int iconPosition = 0;
try
{
var combined = new Bitmap(size, size, PixelFormat.Format24bppRgb);
Expand All @@ -175,9 +179,7 @@ private void UpdateCurrentFrame()
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;

const int iconPosition = 0;


using (var brush = new SolidBrush(BackgroundColor))
{
g.FillRectangle(brush, iconPosition, iconPosition, size, size);
Expand All @@ -201,14 +203,16 @@ private void UpdateCurrentFrame()

}

using var bufferStream = new MemoryStream();
combined.Save(bufferStream, ImageFormat.Png);
//using var bufferStream = new MemoryStream();
//combined.Save(bufferStream, ImageFormat.Png);

return KeyBitmap.Create.FromBitmap(combined);

return KeyBitmap.Create.FromStream(bufferStream);
//return KeyBitmap.Create.FromStream(bufferStream);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine($"Error while rendering current frame: {ex.Message}");
return null;
}
}
Expand Down
8 changes: 2 additions & 6 deletions Models/ConnectedDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using StreamDeckSharp;
using Timer = System.Timers.Timer;

namespace MacroDeck.StreamDeckConnector
namespace MacroDeck.StreamDeckConnector.Models
{
public class ConnectedDevice
{
Expand Down Expand Up @@ -82,11 +82,6 @@ public ConnectedDevice(string path)

private void StreamDeck_KeyStateChanged(object sender, KeyEventArgs e)
{
if (PressedButtonId == -1)
{
OnButtonPress?.Invoke(sender, new ButtonPressEventArgs(PressedButtonId, ButtonEventKind.UP));
}

if (e.Key > -1)
{
PressedButtonId = e.Key;
Expand All @@ -100,6 +95,7 @@ private void StreamDeck_KeyStateChanged(object sender, KeyEventArgs e)
}
else
{
OnButtonPress?.Invoke(sender, new ButtonPressEventArgs(PressedButtonId, ButtonEventKind.UP));
if (longPress)
{
buttonKind = ButtonEventKind.LONG_UP;
Expand Down
2 changes: 2 additions & 0 deletions USBHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MacroDeck.StreamDeckConnector.Models;
using MacroDeck.StreamDeckConnector.Parsers;
using StreamDeckSharp;
using Usb.Events;
Expand Down Expand Up @@ -51,6 +52,7 @@ public static void Initialize()
var devices = StreamDeck.EnumerateDevices();
foreach (var device in devices)
{
Console.WriteLine($"Found {device.DeviceName} @ {device.DevicePath}");
try
{
var connectedDevice = new ConnectedDevice(device.DevicePath);
Expand Down
26 changes: 11 additions & 15 deletions Utils/EmptyButtonImageGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,35 @@ namespace MacroDeck.StreamDeckConnector.Utils
{
public class EmptyButtonImageGenerator
{
public static KeyBitmap GetEmptyButton(int size, bool cropped = false)
public static KeyBitmap GetEmptyButton(int size)
{
try
{
Bitmap combined = new Bitmap(size, size, PixelFormat.Format24bppRgb);
var combined = new Bitmap(size, size, PixelFormat.Format24bppRgb);

using (Graphics g = Graphics.FromImage(combined))
const int iconPosition = 0;

using (var g = Graphics.FromImage(combined))
{
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;

int iconPosition = cropped ? 10 : 0;
int iconSize = cropped ? size - 20 : size;


using SolidBrush brush = new SolidBrush(Color.FromArgb(35, 35, 35));
g.FillRectangle(brush, iconPosition, iconPosition, iconSize, iconSize);


using var brush = new SolidBrush(Color.FromArgb(35, 35, 35));
g.FillRectangle(brush, iconPosition, iconPosition, size, size);
}

combined.RotateFlip(RotateFlipType.Rotate180FlipNone);


using var bufferStream = new MemoryStream();
combined.Save(bufferStream, ImageFormat.Png);

return KeyBitmap.Create.FromStream(bufferStream);
}
catch
{
return null;
return KeyBitmap.Black;
}

}

}
Expand Down

0 comments on commit e45693f

Please sign in to comment.