summaryrefslogtreecommitdiff
path: root/keybl.c
blob: 8c9b7b3fef45e35dd300733872ef8c21ae05a1d4 (plain) (blame)
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
/*
 * 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;
}