@@ -61,7 +61,7 @@ void help() {
61
61
" This device does not re enable keyboard backlight.\n "
62
62
" Separate multiple device by space.\n "
63
63
" Default: use all mice and keyboard.\n "
64
- " -t configure timeout after which the backlight will be turned off\n "
64
+ " -t configure timeout in seconds after which the backlight will be turned off\n "
65
65
" Defaults to 30s \n "
66
66
" -m configure mouse mode (0..2)\n "
67
67
" 0 use all mice (default)\n "
@@ -155,38 +155,39 @@ std::vector<int> open_devices(const std::vector<std::string> &input_devices) {
155
155
}
156
156
return fds;
157
157
}
158
-
159
158
void brightness_control (const std::string &brightnessPath,
160
- unsigned long timeout ) {
159
+ unsigned long timeoutMs ) {
161
160
while (!end_) {
162
- auto passedSec =
163
- std::chrono::duration_cast<std::chrono::seconds >(
164
- std::chrono::system_clock::now () - lastEvent_);
161
+ auto passedMs = std::chrono::duration_cast<
162
+ std::chrono::milliseconds >(
163
+ std::chrono::system_clock::now () - lastEvent_);
165
164
166
165
if (lastEvent_ < std::chrono::system_clock::now ()) {
167
- auto sleepTime = std::chrono::milliseconds (
168
- (timeout - passedSec.count ()) * 1000 );
166
+ auto sleepTime = std::chrono::milliseconds (timeoutMs - passedMs.count ());
169
167
if (0 != sleepTime.count ()) {
170
168
std::this_thread::sleep_for (sleepTime);
171
169
}
172
170
}
173
171
174
- passedSec =
175
- std::chrono::duration_cast<std::chrono::seconds >(
176
- std::chrono::system_clock::now () - lastEvent_);
172
+ passedMs = std::chrono::duration_cast<
173
+ std::chrono::milliseconds >(
174
+ std::chrono::system_clock::now () - lastEvent_);
177
175
#if DEBUG
178
- printf (" passed: %lu\n " , passedSec .count ());
176
+ printf (" passed: %lu\n " , passedMs .count ());
179
177
#endif
180
- if (passedSec.count () >= static_cast <long >(timeout)) {
181
- #if __DEBU
182
- printf (" timeout reached \n " );
178
+ if (passedMs.count () >= static_cast <long >(timeoutMs)) {
179
+
180
+ #if DEBUG
181
+ printf (" timeoutMs reached \n " );
183
182
printf (" o: %lu c: %lu\n " , originalBrightness_, currentBrightness_);
184
183
#endif
184
+
185
185
if (currentBrightness_ != 0 ) {
186
186
file_read_uint64 (brightnessPath, &originalBrightness_);
187
187
currentBrightness_ = 0 ;
188
188
189
189
file_write_uint64 (brightnessPath, 0 );
190
+
190
191
#if DEBUG
191
192
printf (" o: %lu c: %lu\n " , originalBrightness_, currentBrightness_);
192
193
printf (" turning off \n " );
@@ -208,6 +209,7 @@ void read_events(int devFd, const std::string &brightnessPath) {
208
209
if (currentBrightness_ != originalBrightness_) {
209
210
file_write_uint64 (brightnessPath, originalBrightness_);
210
211
currentBrightness_ = originalBrightness_;
212
+
211
213
#if DEBUG
212
214
printf (" on\n " );
213
215
#endif
@@ -220,13 +222,24 @@ void read_events(int devFd, const std::string &brightnessPath) {
220
222
void signal_handler (int sig) {
221
223
switch (sig) {
222
224
case SIGTERM:
225
+ case SIGKILL:
223
226
end_ = true ;
224
227
break ;
225
228
default :
226
229
break ;
227
230
}
228
231
}
229
232
233
+ bool is_brightness_writable (const std::string &brightnessPath) {
234
+ if (!file_read_uint64 (brightnessPath, &originalBrightness_)
235
+ || !file_write_uint64 (brightnessPath, originalBrightness_)) {
236
+ std::cout << " Write access to brightness device descriptor failed.\n "
237
+ " Please run with root privileges" << std::endl;
238
+ return false ;
239
+ }
240
+ return true ;
241
+ }
242
+
230
243
void parse_opts (int argc,
231
244
char *const *argv,
232
245
std::vector<std::string> &ignoredDevices,
@@ -338,15 +351,11 @@ int main(int argc, char **argv) {
338
351
}
339
352
340
353
std::string brightnessPath = backlightPath + " brightness" ;
341
- if (!file_read_uint64 (brightnessPath, &originalBrightness_)
342
- || !file_write_uint64 (brightnessPath, originalBrightness_)
343
- ) {
344
- std::cout << " Write access to brightness device descriptor failed.\n "
345
- " Please run with root privileges" << std::endl;
346
- exit (EXIT_FAILURE);
354
+ if (!is_brightness_writable (brightnessPath)){
355
+ exit (EXIT_FAILURE);
347
356
}
348
357
349
- if (setBrightness >= 0 ){
358
+ if (setBrightness >= 0 ) {
350
359
file_write_uint64 (brightnessPath, setBrightness);
351
360
exit (0 );
352
361
}
@@ -358,7 +367,7 @@ int main(int argc, char **argv) {
358
367
359
368
if (!foreground) {
360
369
if (daemon (0 , 0 )) {
361
- printf ( " failed to daemonize" ) ;
370
+ std::cout << " failed to daemonize" << std::endl ;
362
371
exit (EXIT_FAILURE);
363
372
}
364
373
}
@@ -374,8 +383,10 @@ int main(int argc, char **argv) {
374
383
brightnessPath));
375
384
}
376
385
377
- brightness_control (brightnessPath, timeout);
386
+ brightness_control (brightnessPath, timeout * 1000 );
378
387
379
- for (auto &t : f)
380
- t.wait ();
388
+ for (const auto &fd : fds) {
389
+ close (fd);
390
+ }
391
+ exit (0 );
381
392
}
0 commit comments