From 80ba776bfae57f2622caa0ecf8a5eb3821ab08b0 Mon Sep 17 00:00:00 2001 From: DemonKingSwarn Date: Mon, 10 Nov 2025 15:30:15 +0530 Subject: chore: widnows support added --- Program.cs | 8 +++ hypr-wellbeing-linux.csproj | 17 +++++ hypr-wellbeing-windows.csproj | 17 +++++ hypr-wellbeing.csproj | 13 ---- src/GetWindows.cs | 113 ++++++++++++++++++++++----------- src/WatchLog.cs | 141 ++++++++++++++++++++++++++++++++---------- 6 files changed, 227 insertions(+), 82 deletions(-) create mode 100644 hypr-wellbeing-linux.csproj create mode 100644 hypr-wellbeing-windows.csproj delete mode 100644 hypr-wellbeing.csproj diff --git a/Program.cs b/Program.cs index de14dd3..7428c9f 100644 --- a/Program.cs +++ b/Program.cs @@ -1,5 +1,6 @@ using System.IO; using System.Runtime.InteropServices; +using Newtonsoft.Json; using hyprwatch.Logger; using hyprwatch.Report.Weekly; @@ -7,11 +8,18 @@ class Program { static void Main(string[] args) { + string os; + string version = "0.0.7"; string homeDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + string configDir = Path.Combine(homeDirectory, ".config", "hypr-wellbeing"); + string configFile = Path.Combine(configDir, "config.json"); string hyprwatchDirectory = Path.Combine(homeDirectory, ".cache", "hyprwatch"); string dailyDataDirectory = Path.Combine(hyprwatchDirectory, "daily_data"); Directory.CreateDirectory(dailyDataDirectory); + Directory.CreateDirectory(configDir); + + if (args.Length == 0 || args[0] != "-d" && args[0] != "--show" && args[0] != "--weekly" && args[0] != "-v") { diff --git a/hypr-wellbeing-linux.csproj b/hypr-wellbeing-linux.csproj new file mode 100644 index 0000000..9092bd9 --- /dev/null +++ b/hypr-wellbeing-linux.csproj @@ -0,0 +1,17 @@ + + + + Exe + net8.0 + enable + enable + true + true + linux-x64 + + + + + + + diff --git a/hypr-wellbeing-windows.csproj b/hypr-wellbeing-windows.csproj new file mode 100644 index 0000000..dba4017 --- /dev/null +++ b/hypr-wellbeing-windows.csproj @@ -0,0 +1,17 @@ + + + + Exe + net8.0 + enable + enable + true + true + win-x64 + + + + + + + diff --git a/hypr-wellbeing.csproj b/hypr-wellbeing.csproj deleted file mode 100644 index e4aab94..0000000 --- a/hypr-wellbeing.csproj +++ /dev/null @@ -1,13 +0,0 @@ - - - - Exe - net8.0 - enable - enable - true - true - linux-x64 - - - diff --git a/src/GetWindows.cs b/src/GetWindows.cs index ee72c39..336f8b1 100644 --- a/src/GetWindows.cs +++ b/src/GetWindows.cs @@ -1,66 +1,109 @@ namespace hyprwatch.Window { using System; + using System.Text; + using System.Runtime.InteropServices; using System.Diagnostics; using System.Text.RegularExpressions; + using Newtonsoft.Json; public partial class GetWindows { + + [DllImport("user32.dll")] + static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); + public static string ActiveWindow() { string desktopEnv = Environment.GetEnvironmentVariable("XDG_CURRENT_DESKTOP"); + string homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); string? activeWindow = null; + string? os = null; - try { - Process process = new Process(); - process.StartInfo = new ProcessStartInfo(); - - process.StartInfo.RedirectStandardOutput = true; - process.StartInfo.UseShellExecute = false; - process.StartInfo.CreateNoWindow = true; + string configFile = Path.Combine(homeDir, ".config", "hypr-wellbeing", "config.json"); + if(File.Exists(configFile)) + { + string content = File.ReadAllText(configFile); + var config = JsonConvert.DeserializeObject>(content); + + os = config["os"]; + } + else + { + os = "Linux"; + } + + if(os == "Linux") + { + try { + Process process = new Process(); + process.StartInfo = new ProcessStartInfo(); + + process.StartInfo.RedirectStandardOutput = true; + process.StartInfo.UseShellExecute = false; + process.StartInfo.CreateNoWindow = true; - if(desktopEnv == "Hyprland") - { - process.StartInfo.FileName = "hyprctl"; - process.StartInfo.Arguments = "activewindow"; - } - else if(desktopEnv == "niri") - { - process.StartInfo.FileName = "niri"; - process.StartInfo.Arguments = "msg focused-window"; - } - process.Start(); + if(desktopEnv == "Hyprland") + { + process.StartInfo.FileName = "hyprctl"; + process.StartInfo.Arguments = "activewindow"; + } + else if(desktopEnv == "niri") + { + process.StartInfo.FileName = "niri"; + process.StartInfo.Arguments = "msg focused-window"; + } - string output = process.StandardOutput.ReadToEnd(); - process.WaitForExit(); + process.Start(); - var classMatch = ClassRegex().Match(output); + string output = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); - if(desktopEnv == "niri") - { - var match = Regex.Match(output, "App ID:\\s+\"([^\"]+)\""); - if(match.Success) + var classMatch = ClassRegex().Match(output); + + if(desktopEnv == "niri") { - activeWindow = match.Groups[1].Value.Trim(); - } - } + var match = Regex.Match(output, "App ID:\\s+\"([^\"]+)\""); + if(match.Success) + { + activeWindow = match.Groups[1].Value.Trim(); + } + } - if(desktopEnv == "Hyprland") - { - if(classMatch.Success) + if(desktopEnv == "Hyprland") { - activeWindow = classMatch.Groups[1].Value.Trim(); + if(classMatch.Success) + { + activeWindow = classMatch.Groups[1].Value.Trim(); + } } - } + } + catch(Exception ex) + { + Console.WriteLine(ex.Message); + } } - catch(Exception ex) + + else if (os == "Windows") { - Console.WriteLine(ex.Message); + + IntPtr handle = GetForegroundWindow(); + if(handle != IntPtr.Zero) + { + StringBuilder className = new StringBuilder(256); + if(GetClassName(handle, className, className.Capacity) > 0) + { + activeWindow = className.ToString(); + } + } } if(activeWindow == null) diff --git a/src/WatchLog.cs b/src/WatchLog.cs index cb2aa7a..4a029d9 100644 --- a/src/WatchLog.cs +++ b/src/WatchLog.cs @@ -5,6 +5,7 @@ namespace hyprwatch.Logger using System.Threading; using System.Diagnostics; using System.Collections.Generic; + using Newtonsoft.Json; using hyprwatch.Window; using hyprwatch.Time; @@ -13,31 +14,55 @@ namespace hyprwatch.Logger public static string GetTime() { string? t = null; + string? os = null; - try + string homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + string configFile = Path.Combine(homeDir, ".config", "hypr-wellbeing", "config.json"); + + if(File.Exists(configFile)) + { + string content = File.ReadAllText(configFile); + var config = JsonConvert.DeserializeObject>(content); + + os = config["os"]; + } + else + { + os = "Linux"; + } + + if(os == "Linux") { - Process process = new Process + try { - StartInfo = new ProcessStartInfo + Process process = new Process { - FileName = "date", - Arguments = "+%T", - RedirectStandardOutput = true, - UseShellExecute = false, - CreateNoWindow = true, - } - }; + StartInfo = new ProcessStartInfo + { + FileName = "date", + Arguments = "+%T", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true, + } + }; - process.Start(); + process.Start(); - string output = process.StandardOutput.ReadToEnd(); - process.WaitForExit(); + string output = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); - t = output.Substring(0, output.Length - 1); + t = output.Substring(0, output.Length - 1); + } + catch(Exception ex) + { + Console.WriteLine(ex.Message); + } } - catch(Exception ex) + + else if(os == "Windows") { - Console.WriteLine(ex.Message); + t = DateTime.Now.ToString("HH:mm:ss"); } return t ?? string.Empty; @@ -46,31 +71,79 @@ namespace hyprwatch.Logger public static string GetDate() { string? d = null; + string? os = null; + + string homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + string configFile = Path.Combine(homeDir, ".config", "hypr-wellbeing", "config.json"); + + if(File.Exists(configFile)) + { + string content = File.ReadAllText(configFile); + var config = JsonConvert.DeserializeObject>(content); + + os = config["os"]; + } + else + { + os = "Linux"; + } - try + if(os == "Linux") { - Process process = new Process + try { - StartInfo = new ProcessStartInfo + Process process = new Process { - FileName = "date", - Arguments = "+%d-%m-%Y", - RedirectStandardOutput = true, - UseShellExecute = false, - CreateNoWindow = true, - } - }; + StartInfo = new ProcessStartInfo + { + FileName = "date", + Arguments = "+%d-%m-%Y", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true, + } + }; - process.Start(); + process.Start(); - string output = process.StandardOutput.ReadToEnd(); - process.WaitForExit(); + string output = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); - d = output.Substring(0, output.Length - 1); + d = output.Substring(0, output.Length - 1); + } + catch(Exception ex) + { + Console.WriteLine(ex.Message); + } } - catch(Exception ex) + + else if(os == "Windows") { - Console.WriteLine(ex.Message); + try + { + Process process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = "powershell", + Arguments = "-Command \"Get-Date -Format dd-MM-yyyy\"", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true, + } + }; + + process.Start(); + + string output = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + + d = output.Substring(0, output.Length - 1); + } + catch(Exception ex) + { + Console.WriteLine(ex.Message); + } } return d ?? string.Empty; @@ -78,7 +151,7 @@ namespace hyprwatch.Logger static void UpdateCSV(string date, Dictionary data) { - string homeDir = Environment.GetEnvironmentVariable("HOME") ?? throw new InvalidOperationException("HOME environment variable is not set."); + string homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); string filePath = Path.Combine(homeDir, ".cache", "hyprwatch", "daily_data", $"{date}.csv"); @@ -129,7 +202,7 @@ namespace hyprwatch.Logger public static void LogCreation() { - string homeDir = Environment.GetEnvironmentVariable("HOME"); + string homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); string currentDate = GetDate(); string filename = Path.Combine($"{homeDir}", ".cache", "hyprwatch", "daily_data", $"{currentDate}.csv"); if(!File.Exists(filename)) -- cgit v1.1