Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
BarRaider committed Jan 11, 2019
1 parent 086646a commit 0ae4f9e
Show file tree
Hide file tree
Showing 26 changed files with 2,323 additions and 0 deletions.
Binary file added Install/com.barraider.stopwatch.streamDeckPlugin
Binary file not shown.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
# streamdeck-stopwatch
A C# Stopwatch implementation for the Elgato Stream Deck device.

# streamdeck-client-csharp
A C# client library for the Elgato Stream Deck device.

## Dependencies
This library uses the [streamdeck-client-csharp](https://github.com/TyrenDe/streamdeck-client-csharp) wrapper

## Current functionality
- Multiple Stopwatches suppport
- Stopping/Starting a watch
- Option to not reset the watch after it's restarted
- Two display styles

## Feature roadmap
Always open to more suggestions.

## How do I get started using it?
Install by clicking the com.barraider.stopwatch.streamDeckPlugin file in the Install folder.

## I found a bug, who do I contact?
Just head over to the issues page and create a new issue.

## License
MIT License

Copyright (c) 2019

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 changes: 25 additions & 0 deletions Stopwatch.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.106
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stopwatch", "Stopwatch\Stopwatch.csproj", "{A35FB18D-7867-4F77-A9F3-863BF6F18C6F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A35FB18D-7867-4F77-A9F3-863BF6F18C6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A35FB18D-7867-4F77-A9F3-863BF6F18C6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A35FB18D-7867-4F77-A9F3-863BF6F18C6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A35FB18D-7867-4F77-A9F3-863BF6F18C6F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9597026C-118D-4C3B-A099-1941ABF9A6F3}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions Stopwatch/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
Binary file added Stopwatch/Images/action1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Stopwatch/Images/action1@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Stopwatch/Images/bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Stopwatch/Images/bg@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Stopwatch/Images/categoryIcon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Stopwatch/Images/categoryIcon@2x - Copy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Stopwatch/Images/categoryIcon@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Stopwatch/Images/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Stopwatch/Images/icon@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Stopwatch/Images/pluginIcon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Stopwatch/Images/pluginIcon@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
147 changes: 147 additions & 0 deletions Stopwatch/PluginContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
using Newtonsoft.Json;
using streamdeck_client_csharp;
using streamdeck_client_csharp.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Stopwatch
{
class PluginContainer
{
private StreamDeckConnection connection;
private ManualResetEvent connectEvent = new ManualResetEvent(false);
private ManualResetEvent disconnectEvent = new ManualResetEvent(false);
private SemaphoreSlim instancesLock = new SemaphoreSlim(1);

// Holds all instances of plugin
private static Dictionary<string, StopwatchTimer> instances = new Dictionary<string, StopwatchTimer>();


public void Run(StreamDeckOptions options)
{
connection = new StreamDeckConnection(options.Port, options.PluginUUID, options.RegisterEvent);

// Register for events
connection.OnConnected += Connection_OnConnected;
connection.OnDisconnected += Connection_OnDisconnected;
connection.OnKeyDown += Connection_OnKeyDown;
connection.OnWillAppear += Connection_OnWillAppear;
connection.OnWillDisappear += Connection_OnWillDisappear;

// Settings changed
connection.OnSendToPlugin += Connection_OnSendToPlugin;

// Start the connection
connection.Run();

// Wait for up to 10 seconds to connect
if (connectEvent.WaitOne(TimeSpan.FromSeconds(10)))
{
// We connected, loop every second until we disconnect
while (!disconnectEvent.WaitOne(TimeSpan.FromMilliseconds(1000)))
{
RunTick();
}
}
}

// Button pressed
private async void Connection_OnKeyDown(object sender, StreamDeckEventReceivedEventArgs<KeyDownEvent> e)
{
await instancesLock.WaitAsync();
try
{
if (instances.ContainsKey(e.Event.Context))
{
instances[e.Event.Context].TriggerStopwatch();
}
}
finally
{
instancesLock.Release();
}
}

// Function runs every second, used to update UI
private async void RunTick()
{
await instancesLock.WaitAsync();
try
{
foreach (KeyValuePair<string, StopwatchTimer> kvp in instances.ToArray())
{
_ = connection.SetTitleAsync(kvp.Value.GetCurrentStopwatchValue(), kvp.Key, SDKTarget.HardwareAndSoftware);
}
}
finally
{
instancesLock.Release();
}
}

// Stopwatch instance created
private async void Connection_OnWillAppear(object sender, StreamDeckEventReceivedEventArgs<WillAppearEvent> e)
{
await instancesLock.WaitAsync();
try
{
instances[e.Event.Context] = new StopwatchTimer(connection, e.Event.Action, e.Event.Context, e.Event.Payload.Settings);
}
finally
{
instancesLock.Release();
}
}

// Stopwatch instance no longer shown
private async void Connection_OnWillDisappear(object sender, StreamDeckEventReceivedEventArgs<WillDisappearEvent> e)
{
await instancesLock.WaitAsync();
try
{
if (instances.ContainsKey(e.Event.Context))
{
instances.Remove(e.Event.Context);
}
}
finally
{
instancesLock.Release();
}
}

// Settings updated
private async void Connection_OnSendToPlugin(object sender, StreamDeckEventReceivedEventArgs<SendToPluginEvent> e)
{

await instancesLock.WaitAsync();
try
{
if (instances.ContainsKey(e.Event.Context))
{
instances[e.Event.Context].UpdateSettings(e.Event.Payload);
}
}
finally
{
instancesLock.Release();
}
System.Diagnostics.Debug.WriteLine($"PLUGIN: {JsonConvert.SerializeObject(e.Event)}");

}

private void Connection_OnConnected(object sender, EventArgs e)
{
connectEvent.Set();
}

private void Connection_OnDisconnected(object sender, EventArgs e)
{
disconnectEvent.Set();
}
}
}
53 changes: 53 additions & 0 deletions Stopwatch/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using CommandLine;
using System;

namespace Stopwatch
{
class Program
{
/************************************************************************
* Initial configuration copied from TyrenDe's streamdeck-client-csharp example:
* https://github.com/TyrenDe/streamdeck-client-csharp
* and SaviorXTanren's MixItUp.StreamDeckPlugin:
* https://github.com/SaviorXTanren/mixer-mixitup/
*************************************************************************/

// Handles all the communication with the plugin
private static PluginContainer container;

// StreamDeck launches the plugin with these details
// -port [number] -pluginUUID [GUID] -registerEvent [string?] -info [json]
static void Main(string[] args)
{
// Uncomment this line of code to allow for debugging
//while (!System.Diagnostics.Debugger.IsAttached) { System.Threading.Thread.Sleep(100); }

// The command line args parser expects all args to use `--`, so, let's append
for (int count = 0; count < args.Length; count++)
{
if (args[count].StartsWith("-") && !args[count].StartsWith("--"))
{
args[count] = $"-{args[count]}";
}
}

Parser parser = new Parser((with) =>
{
with.EnableDashDash = true;
with.CaseInsensitiveEnumValues = true;
with.CaseSensitive = false;
with.IgnoreUnknownArguments = true;
with.HelpWriter = Console.Error;
});

ParserResult<StreamDeckOptions> options = parser.ParseArguments<StreamDeckOptions>(args);
options.WithParsed<StreamDeckOptions>(o => RunPlugin(o));
}

static void RunPlugin(StreamDeckOptions options)
{
container = new PluginContainer();
container.Run(options);
}
}
}
36 changes: 36 additions & 0 deletions Stopwatch/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Stopwatch")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Stopwatch")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("a35fb18d-7867-4f77-a9f3-863bf6f18c6f")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading

0 comments on commit 0ae4f9e

Please sign in to comment.