aboutsummaryrefslogtreecommitdiff
path: root/liblux.c
blob: 8061eecd11feb79403488f1d0813e89c738f711c (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * BSD Zero Clause License
 *
 * Copyright (c) 2022 Thomas Voss
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#define _POSIX_C_SOURCE 200809L

#include <sys/types.h>

#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

#include "lux.h"

#ifdef O_PATH
	#define GETDIR_FLAGS O_PATH
#else
	#define GETDIR_FLAGS (O_RDONLY | O_DIRECTORY)
#endif

static int intlen(int);
static int getdir(void);
static FILE *getbstream(int);

void
luxfree(struct luxdisp *disp)
{
	if (disp->__dirfd != -1)
		close(disp->__dirfd);
	if (disp->__bstream != NULL)
		fclose(disp->__bstream);
}

int
luxinit(struct luxdisp *disp)
{
	/* Set the default values for the luxdisp struct.  We want to be as lazy
	 * as possible, so we don't bother with __bstream and __max yet.  We do
	 * need __dirfd though.
	 */
	*disp = (struct luxdisp) {
		.__bstream = NULL,
		.__max     = -1,
		.__dirfd   = getdir()
	};
	return disp->__dirfd;
}

int
luxmax(struct luxdisp *disp)
{
	/* The maximum brightness shouldn't change, so we can cache it in the
	 * struct.
	 */
	if (disp->__max != -1)
		return disp->__max;

	int fd = openat(disp->__dirfd, "max_brightness", O_RDONLY);
	if (fd == -1)
		return -1;
	FILE *stream = fdopen(fd, "r");
	if (stream == NULL) {
		close(fd);
		return -1;
	}

	fscanf(stream, "%d", &disp->__max);
	fclose(stream);
	return disp->__max;
}

int
luxget(struct luxdisp *disp)
{
	/* If we don't have an open stream with the brightness file yet then
	 * open one.  If we do, we instead rewind to the start of the file so
	 * that we don't just read an EOF.
	 */
	if (disp->__bstream == NULL) {
		if ((disp->__bstream = getbstream(disp->__dirfd)) == NULL)
			return -1;
	} else
		rewind(disp->__bstream);

	int n;
	fscanf(disp->__bstream, "%d", &n);
	return n;
}

int
luxset(struct luxdisp *disp, int n)
{
	/* The same deal as in luxget() */
	if (disp->__bstream == NULL) {
		if ((disp->__bstream = getbstream(disp->__dirfd)) == NULL)
			return -1;
	} else
		rewind(disp->__bstream);

	/* After writing the new brightness value to the brightness file, we
	 * need to truncate it so that luxset(disp, 1234); luxset(disp, 9);
	 * gives us a final brightness of 2 and not 9234.
	 */
	fprintf(disp->__bstream, "%d", n);
	ftruncate(fileno(disp->__bstream), intlen(n));
	return n;
}

int
luxinc(struct luxdisp *disp, int n)
{
	return luxset(disp, luxget(disp) + n);
}

int
luxdec(struct luxdisp *disp, int n)
{
	return luxinc(disp, -n);
}

double
luxgetp(struct luxdisp *disp)
{
	int cur, max;
	if ((cur = luxget(disp)) == -1 || (max = luxmax(disp)) == -1)
		return -1;
	return (double) cur / max * 100;
}

double
luxsetp(struct luxdisp *disp, double p)
{
	int max = luxmax(disp);
	return max == -1 ? -1 : luxset(disp, (int) (p / 100 * max));
}

double
luxincp(struct luxdisp *disp, double p)
{
	return luxsetp(disp, luxgetp(disp) + p);
}

double
luxdecp(struct luxdisp *disp, double p)
{
	return luxincp(disp, -p);
}

int
intlen(int n)
{
	return    n < 10         ? 1
		: n < 100        ? 2
		: n < 1000       ? 3
		: n < 10000      ? 4
		: n < 100000     ? 5
		: n < 1000000    ? 6
		: n < 10000000   ? 7
		: n < 100000000  ? 8
		: n < 1000000000 ? 9
		: 10;
}

int
getdir(void)
{
	int fd, dfd;
	struct dirent *dp;
	DIR *dir;

	/* To get the file descriptor for the directory where all the brightness
	 * files are located, we first need to open BACKLIGHT_DIR which is the
	 * directory where the backlight directories are located.  Then we need
	 * to get a DIR* of that directory and get the 3rd entry.  We need the
	 * third because the fist and second are the . and .. folders.  Finally
	 * we open the folder so that we have a file descriptor we can return.
	 */
	if ((dfd = open(BACKLIGHT_DIR, O_RDONLY)) == -1
			|| (dir = fdopendir(dfd)) == NULL
			|| (dp  = readdir(dir))   == NULL
			|| (dp  = readdir(dir))   == NULL
			|| (dp  = readdir(dir))   == NULL
			|| (fd  = openat(dfd, dp->d_name, GETDIR_FLAGS)) == -1)
		return -1;

	closedir(dir);
	return fd;
}

FILE *
getbstream(int dirfd)
{
	return fdopen(openat(dirfd, "brightness", O_RDWR), "r+");
}