aboutsummaryrefslogtreecommitdiff
path: root/src/GetWindows.cs
blob: 336f8b142097467d58d8156fa0a69aff582a8716 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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;

      string configFile = Path.Combine(homeDir, ".config", "hypr-wellbeing", "config.json");

      if(File.Exists(configFile))
      {
        string content = File.ReadAllText(configFile);
        var config = JsonConvert.DeserializeObject<Dictionary<string, string>>(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();

          string output = process.StandardOutput.ReadToEnd();
          process.WaitForExit();

          var classMatch = ClassRegex().Match(output);

          if(desktopEnv == "niri")
            {
              var match = Regex.Match(output, "App ID:\\s+\"([^\"]+)\"");
              if(match.Success)
              {
                activeWindow = match.Groups[1].Value.Trim();
              }
          }


          if(desktopEnv == "Hyprland")
          {
            if(classMatch.Success)
            {
              activeWindow = classMatch.Groups[1].Value.Trim();
            }
          }


        }
        catch(Exception ex)
        {
          Console.WriteLine(ex.Message);
        }
      }

      else if (os == "Windows")
      {
        
        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)
      {
        activeWindow = "Home-Screen";
      }

      return activeWindow ?? string.Empty;
    }

        [GeneratedRegex(@"class:(.+)")]
        private static partial Regex ClassRegex();
    }
}