Skip to content

Commit 508f2e9

Browse files
committed
First commit. Complete closeMonitor, with '-t' and '-s' flags. Add gitignore file.
0 parents  commit 508f2e9

2 files changed

Lines changed: 113 additions & 0 deletions

File tree

.gitignore

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Object files
5+
*.o
6+
*.ko
7+
*.obj
8+
*.elf
9+
10+
# Linker output
11+
*.ilk
12+
*.map
13+
*.exp
14+
15+
# Precompiled Headers
16+
*.gch
17+
*.pch
18+
19+
# Libraries
20+
*.lib
21+
*.a
22+
*.la
23+
*.lo
24+
25+
# Shared objects (inc. Windows DLLs)
26+
*.dll
27+
*.so
28+
*.so.*
29+
*.dylib
30+
31+
# Executables
32+
*.exe
33+
*.out
34+
*.app
35+
*.i*86
36+
*.x86_64
37+
*.hex
38+
39+
# Debug files
40+
*.dSYM/
41+
*.su
42+
*.idb
43+
*.pdb
44+
45+
# Kernel Module Compile Results
46+
*.mod*
47+
*.cmd
48+
.tmp_versions/
49+
modules.order
50+
Module.symvers
51+
Mkfile.old
52+
dkms.conf

src/closeMonitor.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Only on Windows platform
3+
*/
4+
#include <Windows.h>
5+
#include <stdio.h>
6+
#include <errno.h>
7+
#include <string.h>
8+
#include <powrprof.h>
9+
#pragma comment(lib,"libpowrprof.lib")
10+
int main(int argc, char** argv) {
11+
int second = 1;
12+
int sleep = 0;
13+
char* pluralSuffix = "";
14+
15+
for (int i = 1; i < argc; ++i) {
16+
//-t flag, input the execution delay time.
17+
if (!strcmp(argv[i], "-t")) {
18+
if (++i < argc) {
19+
20+
// we can also use atoi() function, but it may not capture some errors.
21+
22+
second = 0;
23+
int j = 0;
24+
for (; argv[i][j] && argv[i][j] >= '0' && argv[i][j] <= '9'; ++j) {
25+
second = second * 10 + argv[i][j] - '0';
26+
if (second < 0) {
27+
errno = 22;
28+
perror("'-t' parameter overflow!");
29+
exit(-1);
30+
}
31+
}
32+
if (argv[i][j]) {
33+
errno = 22;
34+
perror("'-t' parameter should be a nonnegative integer.\n");
35+
exit(-1);
36+
}
37+
}
38+
}
39+
//-s flag, to determine whether the computer should sleep or not.
40+
else if (!strcmp(argv[i], "-s")) {
41+
sleep = 1;
42+
}
43+
}
44+
45+
if (second > 1) {
46+
pluralSuffix = "s";
47+
}
48+
49+
if (sleep) {
50+
printf("Computer will sleep in %d second%s.\n", second, pluralSuffix);
51+
Sleep(second * 1000);
52+
SetSuspendState(FALSE, FALSE, FALSE);
53+
}
54+
else {
55+
printf("Monitor will be closed in %d second%s.\n", second,pluralSuffix);
56+
Sleep(second * 1000);
57+
PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
58+
}
59+
60+
return 0;
61+
}

0 commit comments

Comments
 (0)