-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
201 lines (168 loc) · 5.01 KB
/
Copy pathMain.cpp
File metadata and controls
201 lines (168 loc) · 5.01 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
#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <chrono>
#include <thread>
HANDLE hMutex = NULL;
void timer(long long int seconds) {
if (seconds <= 0) {
std::cout << "Invalid number of seconds: " << seconds << std::endl;
return;
}
std::cout << "staying awake for " << seconds << " seconds";
auto start = std::chrono::steady_clock::now();
while (true) {
auto now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - start).count();
if (elapsed >= seconds) {
break;
}
std::cout << ".";
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
std::cout << std::endl;
}
void cleanup() {
SetThreadExecutionState(ES_CONTINUOUS);
if (hMutex) {
CloseHandle(hMutex);
hMutex = NULL;
}
std::cout << "Exiting program!" << std::endl;
}
BOOL WINAPI ConsoleHandler(DWORD signal) {
if (signal == CTRL_C_EVENT || signal == CTRL_CLOSE_EVENT || signal == CTRL_BREAK_EVENT || signal == CTRL_LOGOFF_EVENT || signal == CTRL_SHUTDOWN_EVENT) {
cleanup();
return 1;
}
return 1;
}
void pause() {
char inputkey = 0;
do {
inputkey = _getch();
} while (inputkey != '\r' && inputkey != '\n');
}
// Enables the the use of ANSI escape sequences, supported on Windows 10 version 1511 and newer.
bool EnableVTMode() {
#ifdef ENABLE_VIRTUAL_TERMINAL_PROCESSING
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE) return false;
DWORD dwMode = 0;
if (!GetConsoleMode(hOut, &dwMode)) return false;
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (!SetConsoleMode(hOut, dwMode)) return false;
return true;
#endif
return false;
}
void ResizeWindow(int cols, int rows) {
// ANSI Escape sequence: \x1b[8;ROWS;COLSt
std::cout << "\x1b[8;" << rows << ";" << cols << "t" << std::flush;
}
// Fallback if VT Mode is unsupported
bool ResizeConsole(short width, short height)
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE)
return false;
COORD bufferSize = { width, height };
if (!SetConsoleScreenBufferSize(hOut, bufferSize))
{
SMALL_RECT tiny = { 0, 0, 1, 1 };
SetConsoleWindowInfo(hOut, TRUE, &tiny);
if (!SetConsoleScreenBufferSize(hOut, bufferSize))
return false;
}
SMALL_RECT windowRect;
windowRect.Left = 0;
windowRect.Top = 0;
windowRect.Right = width - 1;
windowRect.Bottom = height - 1;
if (!SetConsoleWindowInfo(hOut, TRUE, &windowRect))
{
return false;
}
return true;
}
int main(int argc, char* argv[]) {
hMutex = CreateMutexA(NULL, FALSE, "KeepMeAwake_SingleInstance_Mutex");
if (GetLastError() == ERROR_ALREADY_EXISTS) {
std::cout << "Another instance of KeepMeAwake is already running. Please close any other instances and try again.\nPress Enter key to exit..." << std::endl;
if (hMutex) {
CloseHandle(hMutex);
}
pause();
return 1;
}
if (!SetConsoleCtrlHandler(ConsoleHandler, TRUE)) {
std::cout << "Error: Could not set console control handler." << std::endl;
return 1;
}
if (EnableVTMode()) {
ResizeWindow(70, 10);
}
else {
ResizeConsole(70, 10);
}
std::atexit(cleanup);
std::cout << "KeepMeAwake Version 1.1.1, Created by Dylan Aviles\n" << std::endl;
bool displayCanSleep = false, timed = false; long long seconds = NULL;
if (argc != 1) {
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
char option = argv[i][1];
switch (option) {
case 'h':
case 'H':
std::cout << "Command Usage: KeepMeAwake.exe [-option] [value]\n" << std::endl;
std::cout << " No Options Used: Keeps the program running until the Enter key is pressed." << std::endl;
std::cout << " -s: Allows the screen to sleep (if configured in Windows) while keeping the computer awake" << std::endl;
std::cout << " -t <seconds>: Keeps the program running for the specified number of seconds\n" << std::endl;
return 0;
case 's':
case 'S':
std::cout << "Option -s selected, now allowing the screen to sleep" << std::endl;
displayCanSleep = true;
break;
case 't':
case 'T':
if (i + 1 < argc) {
seconds = std::atoll(argv[i + 1]);
timed = true;
}
else {
std::cout << "No argument provided for option -t: seconds\n Usage: KeepMeAwake.exe -t 60\n" << std::endl;
return 1;
}
i++;
break;
default:
std::cout << "Unknown option: " << option << ", ";
return 1;
}
}
else {
std::cout << "Invalid argument: " << argv[i] << ", ";
return 1;
}
}
}
if (displayCanSleep) {
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED);
}
else {
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED | ES_AWAYMODE_REQUIRED);
}
if (timed) {
std::cout << "Option -t selected, ";
timer(seconds);
}
else {
std::cout << "Your system will now stay awake until you press the Enter key..." << std::endl;
pause();
}
return 0;
}
// Copyright (c) 2025-2026 Dylan Aviles
// Licensed under MIT (https://github.com/davil544/KeepMeAwake/blob/master/LICENSE.md)