-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
203 lines (169 loc) · 6.61 KB
/
Copy pathProgram.cs
File metadata and controls
203 lines (169 loc) · 6.61 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ThinkPadBacklightTray;
internal static class Program
{
private const int ATTACH_PARENT_PROCESS = -1;
[DllImport("kernel32.dll")]
private static extern bool AttachConsole(int dwProcessId);
[DllImport("kernel32.dll")]
private static extern bool AllocConsole();
[DllImport("kernel32.dll")]
private static extern bool FreeConsole();
[STAThread]
public static int Main(string[] args)
{
// Check for --debug before processing other args.
var debugMode = false;
var filteredArgs = new List<string>();
foreach (var a in args)
if (a.TrimStart('-', '/').ToLowerInvariant() == "debug")
debugMode = true;
else
filteredArgs.Add(a);
args = filteredArgs.ToArray();
if (args.Length > 0)
{
var hasConsole = AttachConsole(ATTACH_PARENT_PROCESS);
if (!hasConsole)
hasConsole = AllocConsole();
try
{
if (hasConsole)
{
// Re-open stdout/stderr after attaching to a console.
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
Console.SetError(new StreamWriter(Console.OpenStandardError()) { AutoFlush = true });
}
if (TryHandleCommand(args))
return 0;
}
finally
{
if (!debugMode) FreeConsole();
}
}
if (debugMode)
{
// Allocate a new console window for the tray session.
AllocConsole();
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
Console.SetError(new StreamWriter(Console.OpenStandardError()) { AutoFlush = true });
// Route all Debug.WriteLine calls to the console.
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out) { Name = "DebugConsole" });
Trace.AutoFlush = true;
Debug.WriteLine("=== ThinkPad Backlight Tray — debug mode ===");
}
// Enable WinForms visual styles and DPI scaling before any controls are created.
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
new App().Run();
return 0;
}
/// <summary>
/// Handle CLI switches that mirror tray menu actions.
/// Returns true if a command was handled (caller should exit).
/// </summary>
private static bool TryHandleCommand(string[] args)
{
var cmd = args[0].TrimStart('-', '/').ToLowerInvariant();
switch (cmd)
{
case "off":
InitAndSetLevel(0);
return true;
case "dim":
InitAndSetLevel(1);
return true;
case "full":
InitAndSetLevel(2);
return true;
case "restore":
InitializeSystem();
if (SessionHelper.IsConsoleSession())
{
var level = SettingsManager.GetEffectiveRestoreLevel();
BacklightController.SetBacklightLevel((BacklightController.BacklightLevel)level);
}
return true;
case "restore-to":
InitializeSystem();
if (args.Length < 2)
{
Console.Error.WriteLine("Missing argument. Usage: --restore-to <last|dim|full>");
return true;
}
switch (args[1].ToLowerInvariant())
{
case "last":
SettingsManager.SetRestoreLevel(0);
break;
case "dim":
SettingsManager.SetRestoreLevel(1);
break;
case "full":
SettingsManager.SetRestoreLevel(2);
break;
default:
Console.Error.WriteLine($"Unknown restore-to value: {args[1]}. Use last, dim, or full.");
break;
}
return true;
case "startup-on":
SettingsManager.SetRunAtStartup(true);
return true;
case "startup-off":
SettingsManager.SetRunAtStartup(false);
return true;
case "info":
InitializeSystem();
Console.WriteLine(App.BuildInfoString());
return true;
case "help":
case "h":
case "?":
PrintHelp();
return true;
default:
Console.Error.WriteLine($"Unknown command: {args[0]}");
PrintHelp();
return true;
}
}
/// <summary>
/// Initialize system components for CLI commands.
/// This ensures SettingsManager and BacklightController are ready before operations.
/// </summary>
private static void InitializeSystem()
{
SettingsManager.Initialize();
BacklightController.Initialize();
}
private static void InitAndSetLevel(int level)
{
InitializeSystem();
SettingsManager.SetBacklightLevel(level);
BacklightController.SetBacklightLevel((BacklightController.BacklightLevel)level);
}
private static void PrintHelp()
{
Console.WriteLine(
"""
ThinkPad Backlight Tray - CLI
Usage: ThinkPad-Backlight-Tray.exe [command]
Commands:
--off Set backlight to Off and exit
--dim Set backlight to Dim and exit
--full Set backlight to Full and exit
--restore Restore backlight level and exit
--restore-to <last|dim|full>
Set which level to restore to
--startup-on Enable Run at Startup and exit
--startup-off Disable Run at Startup and exit
--info Print diagnostic info and exit
--debug Launch tray with live debug logging to a console window
--help Show this help
No arguments: launch tray application.
""");
}
}