Skip to content

Commit

Permalink
Merge pull request #1 from jtsiomb/master
Browse files Browse the repository at this point in the history
Minor kbdlight improvements.
  • Loading branch information
Hugo Osvaldo Barrera committed Oct 20, 2013
2 parents 53badbd + b26e9c5 commit 7263dc8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
21 changes: 17 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
all:
gcc kbdlight.c -o kbdlight
PREFIX = /usr/local

obj = kbdlight.o
bin = kbdlight

$(bin): $(obj)
$(CC) -o $@ $(obj) $(LDFLAGS)

.PHONY: clean
clean:
rm kbdlight
rm -f $(obj) $(bin)

.PHONY: install
install:
install -Dm 4755 kbdlight ${DESTDIR}/usr/bin/kbdlight
install -Dm 4755 $(bin) $(DESTDIR)$(PREFIX)/bin/$(bin)

.PHONY: uninstall
uninstall:
rm -f $(DESTDIR)$(PREFIX)/bin/$(bin)
42 changes: 34 additions & 8 deletions kbdlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,37 @@
#include <stdlib.h>
#include <string.h>

#define BLPATH "/sys/class/leds/smc::kbd_backlight"
#define BLVALFILE BLPATH "/brightness"
#define BLMAXFILE BLPATH "/max_brightness"

void usage() {
printf("Usage: kbdlight [up|down|off|max]\n");
printf("Usage: kbdlight [up|down|off|max|get|set <value>]\n");
exit(0);
}

int main(int argc, char* argv[]) {
int current, next;
int current, next, maxval;
FILE *file;

if (argc != 2) {
if (argc < 2) {
usage();
}

file = fopen("/sys/class/leds/smc::kbd_backlight/brightness", "r");
if(!(file = fopen(BLVALFILE, "r"))) {
perror("failed to open the keyboard backlight file: " BLVALFILE);
return 1;
}
fscanf(file, "%d", &current);
fclose(file);

if((file = fopen(BLMAXFILE, "r"))) {
fscanf(file, "%d", &maxval);
fclose(file);
} else {
maxval = 255;
}

if (!strcmp(argv[1], "up"))
next = current + 16;
else if (!strcmp(argv[1], "down"))
Expand All @@ -27,11 +41,23 @@ int main(int argc, char* argv[]) {
next = 0;
else if (!strcmp(argv[1], "max"))
next = 255;
else
else if (!strcmp(argv[1], "set")) {
char *endp;
if(!argv[2] || (next = strtol(argv[2], &endp, 10), endp == argv[2])) {
fprintf(stderr, "set must be followed by a number from 0 to %d\n", maxval);
return 1;
}
} else if(!strcmp(argv[1], "get")) {
printf("%d\n", current);
return 0;
} else
usage();

file = fopen("/sys/class/leds/smc::kbd_backlight/brightness", "w");
fprintf(file, "%d", next);
fclose(file);
if((file = fopen(BLVALFILE, "w"))) {
fprintf(file, "%d", next);
fclose(file);
} else {
perror("failed to write to the keyboard backlight file: " BLVALFILE);
}
return 0;
}

0 comments on commit 7263dc8

Please sign in to comment.