Skip to content

Commit

Permalink
New versioning, single instance
Browse files Browse the repository at this point in the history
Added license, readme
  • Loading branch information
andersforsgren committed Jan 9, 2021
1 parent a5b9440 commit d1236da
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 5 deletions.
1 change: 0 additions & 1 deletion GitInfo.txt

This file was deleted.

7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2021 Anders Forsgren

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.
34 changes: 30 additions & 4 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AudioSwitcher.AudioApi.CoreAudio;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand All @@ -10,27 +11,44 @@ namespace KeyMute
public static class ProgramInfo
{
public static string Name => System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
public static string Version => $"{ThisAssembly.Git.SemVer.Major}.{ThisAssembly.Git.SemVer.Minor}.{ThisAssembly.Git.SemVer.Patch}.{ThisAssembly.Git.Commits}";
public static string Version => $"{ThisAssembly.Git.SemVer.Major}.{ThisAssembly.Git.SemVer.Minor}.{ThisAssembly.Git.SemVer.Patch}";
public static string NameAndVersion => $"{Name} v{Version}";
}

static class Program
{
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MuteAppContext());
var ctx = MuteAppContext.Create();
if (ctx != null)
Application.Run();
else
Application.Exit();
}

private sealed class MuteAppContext : ApplicationContext
{
private readonly NotifyIcon trayIcon;
private readonly MuteForm muteForm;
private readonly SingleInstance singleInstance;

public static MuteAppContext Create()
{
var singleInst = SingleInstance.Create("KeyMute", 1000);
if (singleInst == null)
{
Debug.WriteLine("Already running.");
return null;
}
return new MuteAppContext(singleInst);
}

public MuteAppContext()
private MuteAppContext(SingleInstance singleInstance)
{
this.singleInstance = singleInstance;
muteForm = new MuteForm();
trayIcon = new NotifyIcon()
{
Expand Down Expand Up @@ -58,6 +76,14 @@ private void Exit()
muteForm.Close();
Application.Exit();
}

protected override void Dispose(bool disposing)
{
if (disposing)
singleInstance?.Dispose();

base.Dispose(disposing);
}
}
}
}
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
KeyMute
=======

Windows application to mute keys when typing. Built with [AudioSwitcher](https://github.com/xenolightning/AudioSwitcher/). Inspired by UnClack for MacOS.

NOTE: Work in progress


Installation
============

Prerequisites:
.NET Runtime 4.7.1 or later (Comes preinstalled on Win10 1709 and later)


Copy binaries and run.
NOTE:
- No installer yet
- No automatic start yet


Contributing
============

Please do

License
=======

MIT License. See `LICENSE` for more information
51 changes: 51 additions & 0 deletions SingleInstance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Threading;

namespace KeyMute
{
public sealed class SingleInstance : IDisposable
{
private Mutex mutex;

private SingleInstance(Mutex mutex)
{
this.mutex = mutex;
}

public static SingleInstance Create(string id, int timeout)
{
string mutexId = string.Format("Global\\{{{0}}}", id);

var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);
bool hasHandle;
Mutex mutex = null;
try
{
mutex = new Mutex(false, mutexId, out bool createdNew, securitySettings);
hasHandle = mutex.WaitOne(timeout, false);
}
catch (AbandonedMutexException)
{
hasHandle = true;
}

if (mutex != null && hasHandle)
return new SingleInstance(mutex);
else return null;
}

public void Dispose()
{
var m = mutex;
if (m != null)
{
m.ReleaseMutex();
mutex = null;
}
}
}
}

0 comments on commit d1236da

Please sign in to comment.