Skip to content

Commit

Permalink
Add support for white/blacklist modes, add configuration loader, upda…
Browse files Browse the repository at this point in the history
…te UI
  • Loading branch information
bladeSk committed Nov 17, 2020
1 parent eff6e20 commit a5c4a26
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 37 deletions.
106 changes: 106 additions & 0 deletions StayFocused/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StayFocused
{
enum Modes
{
Blacklist,
Whitelist
}

class Config
{
private static readonly string[] DEFAULT_CONFIG = {
"# valid modes: blacklist, whitelist",
"[config]",
"mode = whitelist",
"",
"# hook ONLY these programs, when mode = whitelist",
"[whitelist]",
"Photoshop.exe",
"",
"# hook ALL programs except the following, when mode = blacklist",
"[blacklist]",
"explorer.exe",
"svchost.exe",
"taskhostw.exe",
"RuntimeBroker.exe",
"SearchUI.exe",
"ShellExperienceHost.exe",
"chrome.exe",
"conhost.exe",
};

public Modes mode = Modes.Whitelist;
public HashSet<string> whitelist;
public HashSet<string> blacklist;

public Config() {
var filename = GetConfigPath();
if (!File.Exists(filename)) {
Directory.CreateDirectory(Path.GetDirectoryName(filename));
File.WriteAllLines(filename, DEFAULT_CONFIG);
}

var config = File.ReadAllLines(filename);
whitelist = new HashSet<string>();
blacklist = new HashSet<string>();
ParseConfig(config);
}

public string GetConfigPath() {
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StayFocused.ini");
}

void ParseConfig(string[] lines) {
string section = "config";

foreach (var lineRaw in lines) {
string line = lineRaw.Trim().ToLowerInvariant();

if (line.StartsWith("#") || line.Length == 0) {
// comment line - skip
continue;
}

if (line.StartsWith("[")) {
// section line - change section
section = line.Substring(1, line.Length - 2);
continue;
}

if (section == "config") {
if (line.StartsWith("mode = ")) {
var parsedMode = line.Substring(7);
if (parsedMode == "blacklist") mode = Modes.Blacklist;
}
continue;
}

if (section == "whitelist") {
whitelist.Add(line);
continue;
}

if (section == "blacklist") {
blacklist.Add(line);
continue;
}
}

if (mode == Modes.Blacklist) {
Form1.Log("Config loaded • mode = blacklist • blacklisted exes:");
Form1.Log(String.Join(", ", blacklist));
} else {
Form1.Log("Config loaded • mode = whitelist • whitelisted exes:");
Form1.Log(String.Join(", ", whitelist));
}
Form1.Log("---");
}
}
}
41 changes: 28 additions & 13 deletions StayFocused/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 22 additions & 24 deletions StayFocused/Form1.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
//#define TESTMODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace StayFocused
{
public partial class Form1 : Form
{
WindowWatcher winWatcher;
uint ownPid;
HashSet<string> ignoredExes;

Config config;

static Form1 instance;

delegate void LogCallback (params object[] entries);
Expand All @@ -34,19 +29,8 @@ public Form1 () {
}

void OnInit (object sender, EventArgs e) {
ignoredExes = new HashSet<string> {
"explorer.exe",
"svchost.exe",
"SearchUI.exe",
"chrome.exe",
"foobar2000.exe",
"8's hotkeys2.1.exe",
"7+ taskbar tweaker.ex2",
"ShellExperienceHost.exe",
};

ownPid = (uint)Process.GetCurrentProcess().Id;

config = new Config();
winWatcher = new WindowWatcher(OnWindowCreated);
}

Expand All @@ -68,8 +52,10 @@ void OnWindowCreated (Process proc) {

try {
string exeName = proc.MainModule.ModuleName;
string exeNameLower = exeName.ToLowerInvariant();

if (ignoredExes.Contains(exeName.ToLowerInvariant())) return;
if (config.mode == Modes.Whitelist && !config.whitelist.Contains(exeNameLower)) return;
if (config.mode == Modes.Blacklist && config.blacklist.Contains(exeNameLower)) return;

Log("Hooking", exeName, "(" + proc.Id + ")");
#if (!TESTMODE)
Expand All @@ -88,8 +74,10 @@ void UnloadHooks () {

try {
string exeName = proc.MainModule.ModuleName;
string exeNameLower = exeName.ToLowerInvariant();

if (ignoredExes.Contains(exeName.ToLowerInvariant())) continue;
if (config.mode == Modes.Whitelist && !config.whitelist.Contains(exeNameLower)) return;
if (config.mode == Modes.Blacklist && config.blacklist.Contains(exeNameLower)) return;

Log("Unloading from", exeName, "(" + proc.Id + ")");
#if (!TESTMODE)
Expand All @@ -111,6 +99,16 @@ private void buttonExit_Click (object sender, EventArgs e) {
OnExitClicked(null, null);
}

private void buttonGotoConfig_Click (object sender, EventArgs e) {
string args = "/select, \"" + config.GetConfigPath() + "\"";

Process.Start("explorer.exe", args);
}

private void linkHomepage_LinkClicked (object sender, LinkLabelLinkClickedEventArgs e) {
Process.Start("https://github.com/bladeSk/StayFocused");
}

protected override void Dispose (bool isDisposing) {
if (trayIcon != null) {
trayIcon.Dispose();
Expand All @@ -129,7 +127,7 @@ protected override void Dispose (bool isDisposing) {
bool minimizeOnClose = true;

void InitTrayIcon () {
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(GetType());
ComponentResourceManager resources = new ComponentResourceManager(GetType());

var trayMenu = new ContextMenu();
var openItem = new MenuItem("Open", OnOpenClicked);
Expand Down Expand Up @@ -187,6 +185,6 @@ void OnExitClicked (Object o, EventArgs evtArgs) {
Close();
Dispose();
}
#endregion
#endregion
}
}
1 change: 1 addition & 0 deletions StayFocused/StayFocused.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Config.cs" />
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
Expand Down

0 comments on commit a5c4a26

Please sign in to comment.