diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | keybl.c | 108 |
3 files changed, 111 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4486c4a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +keybl diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..86b6f26 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +all: + gcc -o keybl keybl.c @@ -0,0 +1,108 @@ +/* + * Copyright © 2012 Mike Beattie <mike@ethernal.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +#define KBDLED "/sys/class/leds/samsung::kbd_backlight/brightness" +#define MIN 0 +#define MAX 4 + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <sys/fcntl.h> + +int get_current() { + int fd; + char buf[8]; + ssize_t numread; + + int value; + char *endp; + + fd = open(KBDLED, O_RDONLY); + if (fd < 0) { perror("Unable to open "KBDLED); return -1; } + + numread = read(fd, buf, sizeof(buf) - 1); + if (numread < 1) { + close(fd); + return -1; + } + close(fd); + + buf[numread] = '\0'; + + value = strtol(buf, &endp, 0); + if (*endp == 0 || value < MIN || value > MAX) return -1; + + return value; +} + +int main(int argc, char **argv) { + int fd; + char *endp; + int val = get_current(); + char buf[8]; + + if (val == -1) { + fprintf(stderr, "Error reading from %s\n", KBDLED); + return 1; + } + + // Check args + if (argc < 2) { printf("Current setting: %d\n", val); return 0; }; + + if (strncmp(argv[1], "-h", 2) == 0) { + fprintf(stderr, "Usage: %s [up|down|<0-8>]\n", argv[0]); + fprintf(stderr, " up: increase brightness\n"); + fprintf(stderr, " down: decrease brightness\n"); + fprintf(stderr, " <0-8>: set explicit brigtness\n"); + return 1; + } else if (strncmp(argv[1], "up", 2) == 0) { + if (val < MAX) { + val++; + } else { + fprintf(stderr, "Brightness at max\n"); + return 1; + } + } else if (strncmp(argv[1], "down", 4) == 0) { + if (val > MIN) { + val--; + } else { + fprintf(stderr, "Brightness at min\n"); + return 1; + } + } else { + val = strtol(argv[1], &endp, 0); + if (*endp != 0) { fprintf(stderr, "Error: supplied brightness not an integer\n"); return 1; }; + if (val < MIN || val > MAX) { fprintf(stderr, "Error: brightness must be in range 0-8\n"); return 1; }; + } + + // Create string to write + sprintf(buf, "%d", val); + + // Open sysfs file + fd = open(KBDLED, O_WRONLY); + if (fd < 0) { perror("Unable to open "KBDLED); return 1; } + + // Write the argument + write(fd, buf, strlen(buf)); + + // Clean up + close(fd); + + return 0; +} + |