Windowsロック&ディスプレイの電源をオフにするツール
[program/Application/Windows/tools/lock_and_display_off.git] / lock_and_display_off.c
1 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
2    #undef _WIN32_WINNT
3    #define _WIN32_WINNT 0x0500
4 #endif
5 #include <windows.h>
6 #include <stdio.h>
7 #include <string.h>
8
9 #if _MSC_VER
10 #pragma comment(lib,"user32.lib")
11 #endif
12
13 /*
14
15  Windowsのロックと、ディスプレイの電源オフを行います。
16  ディスプレイの電源オフに関しては、http://www.knonline.net/d/?date=20080123 を参照しています。
17
18  usage:
19    lock_and_display_off
20      Windowsをロックし、ディスプレイの電源をオフにします
21    オプション:
22      -h
23        ヘルプを表示します
24      -lock
25        Windowsをロックします(デフォルト)
26      -nolock
27        Windowsをロックしません
28      -off
29        ディスプレイの電源をオフにします(デフォルト)
30      -nooff
31        ディスプレイの電源をオフにしません
32      -lockfirst
33        最初にWindowsをロックし、次にディスプレイの電源をオフにします(デフォルト)
34      -offfirst
35        最初にディスプレイの電源をオフにし、次にWindowsをロックします
36
37      隠しオプション
38        -wait1st ミリ秒
39          1個目の処理前に指定したミリ秒だけSleepします 有効範囲 0(デフォルト:Sleepしない)~WAIT_MSEC_MAX
40          これを指定しなくてもEXEC_WAITミリ秒(1秒)は強制Sleepしています。
41        -wait2nd ミリ秒
42          2個目の処理前に指定したミリ秒だけSleepします 有効範囲 0(デフォルト:Sleepしない)~WAIT_MSEC_MAX
43        -waitDisplayOffDelay ミリ秒
44          ディスプレイの電源をオフにする際、省電力モード→オフの処理間に指定したミリ秒だけSleepします
45          有効範囲 0(デフォルト:Sleepしない)~WAIT_MSEC_MAX
46
47 */
48
49 #define WAIT_MSEC_MAX (60*1000)
50 #define EXEC_WAIT 1000
51
52 int cmdRet(const char* str)
53 {
54  if(str) fprintf(stderr,"%s\n",str);
55  /* print usage */
56  fprintf(stderr,"usage: lock_and_display_off (-h) ([ -lock | -nolock ]) ([ -off | -nooff ])\n");
57  fprintf(stderr,"                            ([ -lockfirst | -offfirst ])\n");
58  fprintf(stderr,"\toptions\n");
59  fprintf(stderr,"\t\t-h         : show usage(this infomation printout)\n");
60  fprintf(stderr,"\t\t-lock      : LockWindows(default)\n");
61  fprintf(stderr,"\t\t-nolock    : no LockWindows\n");
62  fprintf(stderr,"\t\t-off       : Display Power Off(default)\n");
63  fprintf(stderr,"\t\t-nooff     : no Display Power Off\n");
64  fprintf(stderr,"\t\t-lockfirst : 1st LockWindows, next Display Power Off(default)\n");
65  fprintf(stderr,"\t\t-offfirst  : 1st Display Power Off, next LockWindows\n");
66  fprintf(stderr,"\texample\n");
67  fprintf(stderr,"\t\thelp> lock_and_display_off -h\n");
68  fprintf(stderr,"\t\tLock and Display off> lock_and_display_off\n");
69  fprintf(stderr,"\t\tLock Only> lock_and_display_off -nooff\n");
70  fprintf(stderr,"\t\tDisplay off Only> lock_and_display_off -nolock\n");
71  fprintf(stderr,"\n");
72  fprintf(stderr,"\tversion 0.0.2011080412000\n");
73  fprintf(stderr,"\tLisence: ??? (c) copyright 2011 Ituki Kirihara/NI\n");
74  return 1;
75 }
76
77 int LockWindows(void)
78 {
79  if (LockWorkStation() == 0){
80    return cmdRet("LockWindows:API:LockWorkStation Fail...");
81  }
82  return 0;
83 }
84
85 int DisplayOff(DWORD waitmsec)
86 {
87  if (PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 1) == 0){
88    return cmdRet("DisplayOff:API:PostMessage 1st stage Fail...");
89  }
90  if (waitmsec > 0) Sleep(waitmsec);
91  if (PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2) == 0){
92    return cmdRet("DisplayOff:API:PostMessage 2nd stage Fail...");
93  }
94  return 0;
95 }
96
97 int main(int argc,char* argv[])
98 {
99  int i;
100  int enableLock = 1;
101  int enableDisplayOff = 1;
102  int LockFirst = 1;
103  DWORD wait1st = 0;
104  DWORD wait2nd = 0;
105  DWORD waitDisplayOffDelay = 0;
106  char* nptr=NULL;
107  if (argc < 1 || argv == NULL) return cmdRet("unknown main function arg...");
108  for(i=1;i<argc;++i){
109    if(argv[i] == NULL) return cmdRet("unknown main function arg. argv is null...");
110    if(argv[i][0] != '-' || argv[i][1] == '\0') return cmdRet("unknown option");
111    if(strcmp(argv[i],"-h") == 0){
112      if (i != 1 || argc != 2){
113        return cmdRet("-h option : only this option can be specified.");
114      }else{
115        cmdRet(NULL);
116        return 0;
117      }
118    }else if(strcmp(argv[i],"-lock") == 0){
119      enableLock = 1;
120    }else if(strcmp(argv[i],"-nolock") == 0){
121      enableLock = 0;
122    }else if(strcmp(argv[i],"-off") == 0){
123      enableDisplayOff = 1;
124    }else if(strcmp(argv[i],"-nooff") == 0){
125      enableDisplayOff = 0;
126    }else if(strcmp(argv[i],"-lockfirst") == 0){
127      LockFirst = 1;
128    }else if(strcmp(argv[i],"-offfirst") == 0){
129      LockFirst = 0;
130    }else if(strcmp(argv[i],"-wait1st") == 0){
131      if (i+1<argc){
132        nptr = NULL;
133        wait1st = strtoul(argv[i+1],&nptr,0);
134        if (nptr == NULL || *nptr != '\0') return cmdRet("-wait1st option : msec is not number");
135        if (wait1st > WAIT_MSEC_MAX) return cmdRet("-wait1st option : msec time so long");
136      }else{
137        return cmdRet("-wait1st option : msec not set");
138      }
139      ++i;
140    }else if(strcmp(argv[i],"-wait2nd") == 0){
141      if (i+1<argc){
142        nptr = NULL;
143        wait2nd = strtoul(argv[i+1],&nptr,0);
144        if (nptr == NULL || *nptr != '\0') return cmdRet("-wait2nd option : msec is not number");
145        if (wait2nd > WAIT_MSEC_MAX) return cmdRet("-wait2nd option : msec time so long");
146      }else{
147        return cmdRet("-wait2nd option : msec not set");
148      }
149      ++i;
150    }else if(strcmp(argv[i],"-waitDisplayOffDelay") == 0){
151      if (i+1<argc){
152        nptr = NULL;
153        waitDisplayOffDelay = strtoul(argv[i+1],&nptr,0);
154        if (nptr == NULL || *nptr != '\0') return cmdRet("-waitDisplayOffDelay option : msec is not number");
155        if (waitDisplayOffDelay > WAIT_MSEC_MAX) return cmdRet("-waitDisplayOffDelay option : msec time so long");
156      }else{
157        return cmdRet("-waitDisplayOffDelay option : msec not set");
158      }
159      ++i;
160    }else{
161      return cmdRet("unknown option");
162    }
163  }
164  Sleep(EXEC_WAIT);
165  if (wait1st > 0) Sleep(wait1st);
166  if (LockFirst){
167    if (enableLock) LockWindows();
168    if (wait2nd > 0) Sleep(wait2nd);
169    if (enableDisplayOff) DisplayOff(waitDisplayOffDelay);
170  }else{
171    if (enableDisplayOff) DisplayOff(waitDisplayOffDelay);
172    if (wait2nd > 0) Sleep(wait2nd);
173    if (enableLock) LockWindows();
174  }
175  return 0;
176 }