/
main.m
184 lines (169 loc) · 5.76 KB
/
main.m
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
//
// main.c
// NoSleepCtrl
//
// Created by Pavel Prokofiev on 2/23/12.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#import <NoSleep/GlobalConstants.h>
#import <NoSleep/NoSleepInterfaceWrapper.h>
int verboseLevel = 1;
bool modeAC = false;
bool modeBattery = false;
bool getValue = false;
bool setNewValue = false;
char *newValue;
static void usage() {
printf("Usage: NoSleepCtrl [OPTIONS] ...\n\n");
printf("Options:\n");
printf(" -h\t\t Show this usage guide\n");
printf(" -v VRBLVL\t Verbose level. Default value is 1.\n");
printf(" \t\t 0 - minimal output,\n");
printf(" \t\t 1 - normal output,\n");
printf(" \t\t 2 - include driver output.\n");
printf(" -a\t\t Mode qualifier. Used to select AC-Adapter mode\n");
printf(" -b\t\t Mode qualifier. Used to select Battery mode\n");
printf(" \t\t Modes can be combined (-a -b).\n");
printf(" \t\t Use no qualifiers to select a current mode\n");
printf(" -g\t\t Get status for selected mode\n");
printf(" -s NVAL\t Set status for selected mode\n");
printf(" \t\t NVAL should have (%%d) or (%%d,%%d) format, depending\n");
printf(" \t\t on the specified mode (without parentheses)\n");
printf("\n");
}
int main (int argc, const char **argv)
{
int c;
opterr = 0;
while ((c = getopt (argc, (char *const *)argv, "abgs:v:h")) != -1)
switch (c)
{
case 'h':
usage();
return 0x10;
case 'v':
if(optarg != NULL) {
if(sscanf(optarg, "%d", &verboseLevel) != 1 ||
verboseLevel < 0 || verboseLevel > 2) {
fprintf (stderr, "Verbose level should be one of [0, 1, 2]\n");
return 0x11;
}
if(verboseLevel == 2) {
NoSleepVerbose = true;
}
} else
verboseLevel = 1;
break;
case 'a':
modeAC = true;
break;
case 'b':
modeBattery = true;
break;
case 's':
setNewValue = true;
newValue = optarg;
break;
case 'g':
getValue = true;
break;
case '?':
if (optopt == 's' || optopt == 'v')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 0x11;
default:
abort();
}
//for (index = optind; index < argc; index++)
// printf ("Non-option argument %s\n", argv[index]);
if(setNewValue == false && getValue == false) {
usage();
return 0x10;
}
NoSleepInterfaceWrapper *interface = [[NoSleepInterfaceWrapper alloc] init];
if(interface == NULL) {
fprintf(stderr, "NoSleep extension is not loaded.\n");
return 0x13;
}
int ret = 0;
if(setNewValue) {
bool valueAC;
bool valueBattery;
if(modeAC == false || modeBattery == false) {
if(newValue[0] != '0' && newValue[0] != '1') {
fprintf(stderr, "Error: input format should be [0, 1]\n");
ret = 0x12;
}
valueAC = newValue[0] == '1' ? true : false;
valueBattery = valueAC;
} else {
if((strlen(newValue) != 3) ||
(newValue[0] != '0' && newValue[0] != '1') ||
(newValue[1] != ',') ||
(newValue[2] != '0' && newValue[2] != '1')) {
fprintf(stderr, "Error: input format should be [0, 1],[0, 1]\n");
ret = 0x12;
}
valueAC = newValue[0] == '1' ? true : false;
valueBattery = newValue[2] == '1' ? true : false;
}
if(ret == 0) {
if(modeAC == false && modeBattery == false) {
[interface setState:valueAC forMode:kNoSleepModeCurrent];
} else {
if(modeAC == true) {
[interface setState:valueAC forMode:kNoSleepModeAC];
}
if(modeBattery == true) {
[interface setState:valueBattery forMode:kNoSleepModeBattery];
}
}
}
} else if(getValue) {
if(modeAC == false && modeBattery == false) {
bool current = [interface stateForMode:kNoSleepModeCurrent];
if(verboseLevel != 0) {
printf("currentMode:%d\n", current);
} else {
printf("%d\n", current);
}
ret = current?0:1;
} else {
if(modeAC == true) {
bool currentAC = [interface stateForMode:kNoSleepModeAC];
if(verboseLevel != 0) {
printf("acMode:%d", currentAC);
} else {
printf("%d", currentAC);
}
ret |= currentAC ? 0 : 2;
}
if(modeAC == true && modeBattery == true) {
printf(",");
}
if(modeBattery == true) {
bool currentBattery = [interface stateForMode:kNoSleepModeBattery];
if(verboseLevel != 0) {
printf("batteryMode:%d", currentBattery);
} else {
printf("%d", currentBattery);
}
ret |= currentBattery ? 0 : 1;
}
printf("\n");
}
} else {
usage();
}
[interface release];
return ret;
}