2 * Copyright (c) 2017, S. Gilles <sgilles@math.umd.edu>
4 * Permission to use, copy, modify, and/or distribute this software
5 * for any purpose with or without fee is hereby granted, provided
6 * that the above copyright notice and this permission notice appear
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
13 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
14 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 #define STR(x) STR2(x)
28 #define L(x) __FILE__ ":" STR(__LINE__) ": " x
30 static int16_t brightness
;
37 #define FINAL "\033[G"
38 #define BRIGHT_CELL "━"
41 static void output(void)
43 printf("%s%s", "\033[G", BAR_PRE
);
45 for (int j
= 0; j
< BAR_WIDTH
; ++j
) {
46 if (j
* 128 <= brightness
* BAR_WIDTH
) {
47 printf("%s", BRIGHT_CELL
);
49 printf("%s", DARK_CELL
);
53 printf("%s%d/128 %s", BAR_POST
, (int) brightness
, FINAL
);
61 struct termios in_t
= { 0 };
62 struct termios stored_in_t
= { 0 };
63 struct termios stored_out_t
= { 0 };
64 int in_fd
= fileno(stdin
);
65 int out_fd
= fileno(stdout
);
71 /* Get the /sys entry to manipulate */
72 if (!(sf
= fopen(BACKLIGHT_PATH
, "w+"))) {
74 perror(L("fopen(\"" BACKLIGHT_PATH
"\", \"a+\")"));
78 /* Read initial backlight value */
84 if (fread(buf
, 1, 1, sf
) > 0) {
87 brightness
= brightness
* 10 + (buf
[0] - '0');
94 fprintf(stderr
, L("impossible backlight value\n"));
98 /* Set up the terminal for pretty */
101 fprintf(stderr
, L("stdin is not a tty\n"));
105 if (tcgetattr(in_fd
, &stored_in_t
)) {
107 perror(L("tcgetattr"));
111 memcpy(&in_t
, &stored_in_t
, sizeof in_t
);
113 if (!isatty(out_fd
)) {
115 fprintf(stderr
, L("stdout is not a tty\n"));
119 if (tcgetattr(out_fd
, &stored_out_t
)) {
121 perror(L("tcgetattr"));
125 /* cfmakeraw() is nonstandard, and I want ^C anyway. */
126 in_t
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
| IGNCR
|
128 in_t
.c_oflag
&= ~OPOST
;
129 in_t
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
);
130 in_t
.c_cflag
&= ~(CSIZE
| PARENB
);
133 /* Technically, it is necessary to check these one by one. That's a pain */
134 if (tcsetattr(in_fd
, TCSANOW
, &in_t
)) {
136 perror(L("tcsetattr"));
143 while ((c
= getchar()) != EOF
) {
152 brightness
-= (2 * STEP
);
158 if (brightness
< 0) {
160 } else if (brightness
> 128) {
164 fprintf(sf
, "%d\n", (int) brightness
);
175 tcsetattr(in_fd
, TCSANOW
, &stored_in_t
);
176 printf("\033[?25h\n");