From a5c4a26a87fc78260b5b7b410f4c5c80c129985d Mon Sep 17 00:00:00 2001 From: Matus Koprda Date: Tue, 17 Nov 2020 21:13:17 +0100 Subject: [PATCH] Add support for white/blacklist modes, add configuration loader, update UI --- StayFocused/Config.cs | 106 +++++++++++++++++++++++++++++++++ StayFocused/Form1.Designer.cs | 41 +++++++++---- StayFocused/Form1.cs | 46 +++++++------- StayFocused/StayFocused.csproj | 1 + 4 files changed, 157 insertions(+), 37 deletions(-) create mode 100644 StayFocused/Config.cs diff --git a/StayFocused/Config.cs b/StayFocused/Config.cs new file mode 100644 index 0000000..a0991e1 --- /dev/null +++ b/StayFocused/Config.cs @@ -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 whitelist; + public HashSet 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(); + blacklist = new HashSet(); + 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("---"); + } + } +} diff --git a/StayFocused/Form1.Designer.cs b/StayFocused/Form1.Designer.cs index 5a1e31c..33862ad 100644 --- a/StayFocused/Form1.Designer.cs +++ b/StayFocused/Form1.Designer.cs @@ -17,7 +17,8 @@ private void InitializeComponent () { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); this.textBoxLog = new System.Windows.Forms.TextBox(); this.buttonExit = new System.Windows.Forms.Button(); - this.label1 = new System.Windows.Forms.Label(); + this.linkHomepage = new System.Windows.Forms.LinkLabel(); + this.buttonGotoConfig = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBoxLog @@ -44,23 +45,36 @@ private void InitializeComponent () { this.buttonExit.UseVisualStyleBackColor = true; this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click); // - // label1 + // linkHomepage // - this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.label1.AutoSize = true; - this.label1.ForeColor = System.Drawing.SystemColors.ControlDarkDark; - this.label1.Location = new System.Drawing.Point(12, 251); - this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(172, 13); - this.label1.TabIndex = 2; - this.label1.Text = "Send feedback to focus@blade.sk"; + this.linkHomepage.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.linkHomepage.AutoSize = true; + this.linkHomepage.Location = new System.Drawing.Point(12, 251); + this.linkHomepage.Name = "linkHomepage"; + this.linkHomepage.Size = new System.Drawing.Size(95, 13); + this.linkHomepage.TabIndex = 2; + this.linkHomepage.TabStop = true; + this.linkHomepage.Text = "Github/Homepage"; + this.linkHomepage.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkHomepage_LinkClicked); + // + // buttonGotoConfig + // + this.buttonGotoConfig.Anchor = System.Windows.Forms.AnchorStyles.Bottom; + this.buttonGotoConfig.Location = new System.Drawing.Point(179, 246); + this.buttonGotoConfig.Name = "buttonGotoConfig"; + this.buttonGotoConfig.Size = new System.Drawing.Size(106, 23); + this.buttonGotoConfig.TabIndex = 3; + this.buttonGotoConfig.Text = "Open Config File"; + this.buttonGotoConfig.UseVisualStyleBackColor = true; + this.buttonGotoConfig.Click += new System.EventHandler(this.buttonGotoConfig_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(464, 281); - this.Controls.Add(this.label1); + this.Controls.Add(this.buttonGotoConfig); + this.Controls.Add(this.linkHomepage); this.Controls.Add(this.buttonExit); this.Controls.Add(this.textBoxLog); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); @@ -68,7 +82,7 @@ private void InitializeComponent () { this.MinimizeBox = false; this.MinimumSize = new System.Drawing.Size(300, 200); this.Name = "Form1"; - this.Text = "Stay Focused (Beta)"; + this.Text = "Stay Focused (Beta3)"; this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed); this.ResumeLayout(false); this.PerformLayout(); @@ -79,7 +93,8 @@ private void InitializeComponent () { private System.Windows.Forms.TextBox textBoxLog; private System.Windows.Forms.Button buttonExit; - private System.Windows.Forms.Label label1; + private System.Windows.Forms.LinkLabel linkHomepage; + private System.Windows.Forms.Button buttonGotoConfig; } } diff --git a/StayFocused/Form1.cs b/StayFocused/Form1.cs index aa55d86..41f6e7c 100644 --- a/StayFocused/Form1.cs +++ b/StayFocused/Form1.cs @@ -1,16 +1,9 @@ //#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 { @@ -18,7 +11,9 @@ public partial class Form1 : Form { WindowWatcher winWatcher; uint ownPid; - HashSet ignoredExes; + + Config config; + static Form1 instance; delegate void LogCallback (params object[] entries); @@ -34,19 +29,8 @@ public Form1 () { } void OnInit (object sender, EventArgs e) { - ignoredExes = new HashSet { - "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); } @@ -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) @@ -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) @@ -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(); @@ -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); @@ -187,6 +185,6 @@ void OnExitClicked (Object o, EventArgs evtArgs) { Close(); Dispose(); } -#endregion + #endregion } } diff --git a/StayFocused/StayFocused.csproj b/StayFocused/StayFocused.csproj index c729a72..947f328 100644 --- a/StayFocused/StayFocused.csproj +++ b/StayFocused/StayFocused.csproj @@ -71,6 +71,7 @@ + Form