Skip to content

Commit 84b92c9

Browse files
authored
Merge pull request #6 from alexmohr/dev
Fixed exit Closing fds Changed timeout to ms
2 parents ae868a4 + 2e083c6 commit 84b92c9

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

PKGBUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Maintainer: Alexander Mohr <keyboard_backlight at mohr dot io>
22

33
pkgname=tp-kb-backlight-git
4-
pkgver=1.1
4+
pkgver=1.2
55
pkgrel=1
66
pkgdesc='Automated keyboard backlight'
77
arch=('x86_64')

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ tp_kbd_backlight help
5757
This device does not re enable keyboard backlight.
5858
Separate multiple device by space.
5959
Default: use all mice and keyboard.
60-
-t configure timeout after which the backlight will be turned off
60+
-t configure timeout in seconds after which the backlight will be turned off
6161
Defaults to 30s
6262
-m configure mouse mode (0..2)
6363
0 use all mice (default)

kbd_backlight.cpp

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void help() {
6161
" This device does not re enable keyboard backlight.\n"
6262
" Separate multiple device by space.\n"
6363
" 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"
6565
" Defaults to 30s \n"
6666
" -m configure mouse mode (0..2)\n"
6767
" 0 use all mice (default)\n"
@@ -155,38 +155,39 @@ std::vector<int> open_devices(const std::vector<std::string> &input_devices) {
155155
}
156156
return fds;
157157
}
158-
159158
void brightness_control(const std::string &brightnessPath,
160-
unsigned long timeout) {
159+
unsigned long timeoutMs) {
161160
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_);
165164

166165
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());
169167
if (0 != sleepTime.count()) {
170168
std::this_thread::sleep_for(sleepTime);
171169
}
172170
}
173171

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_);
177175
#if DEBUG
178-
printf("passed: %lu\n", passedSec.count());
176+
printf("passed: %lu\n", passedMs.count());
179177
#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");
183182
printf("o: %lu c: %lu\n", originalBrightness_, currentBrightness_);
184183
#endif
184+
185185
if (currentBrightness_ != 0) {
186186
file_read_uint64(brightnessPath, &originalBrightness_);
187187
currentBrightness_ = 0;
188188

189189
file_write_uint64(brightnessPath, 0);
190+
190191
#if DEBUG
191192
printf("o: %lu c: %lu\n", originalBrightness_, currentBrightness_);
192193
printf("turning off \n");
@@ -208,6 +209,7 @@ void read_events(int devFd, const std::string &brightnessPath) {
208209
if (currentBrightness_ != originalBrightness_) {
209210
file_write_uint64(brightnessPath, originalBrightness_);
210211
currentBrightness_ = originalBrightness_;
212+
211213
#if DEBUG
212214
printf("on\n");
213215
#endif
@@ -220,13 +222,24 @@ void read_events(int devFd, const std::string &brightnessPath) {
220222
void signal_handler(int sig) {
221223
switch (sig) {
222224
case SIGTERM:
225+
case SIGKILL:
223226
end_ = true;
224227
break;
225228
default:
226229
break;
227230
}
228231
}
229232

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+
230243
void parse_opts(int argc,
231244
char *const *argv,
232245
std::vector<std::string> &ignoredDevices,
@@ -338,15 +351,11 @@ int main(int argc, char **argv) {
338351
}
339352

340353
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);
347356
}
348357

349-
if (setBrightness >= 0){
358+
if (setBrightness >= 0) {
350359
file_write_uint64(brightnessPath, setBrightness);
351360
exit(0);
352361
}
@@ -358,7 +367,7 @@ int main(int argc, char **argv) {
358367

359368
if (!foreground) {
360369
if (daemon(0, 0)) {
361-
printf("failed to daemonize");
370+
std::cout << "failed to daemonize" << std::endl;
362371
exit(EXIT_FAILURE);
363372
}
364373
}
@@ -374,8 +383,10 @@ int main(int argc, char **argv) {
374383
brightnessPath));
375384
}
376385

377-
brightness_control(brightnessPath, timeout);
386+
brightness_control(brightnessPath, timeout * 1000);
378387

379-
for (auto &t : f)
380-
t.wait();
388+
for (const auto &fd : fds) {
389+
close(fd);
390+
}
391+
exit(0);
381392
}

0 commit comments

Comments
 (0)