summaryrefslogtreecommitdiffstats
path: root/plugins/power/gsd-backlight-helper.c
blob: c370c7911445399a581fef3173b30b0f4dc59d76 (plain)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2018 Benjamin Berg <bberg@redhat.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <libgen.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define GSD_BACKLIGHT_HELPER_EXIT_CODE_SUCCESS			0
#define GSD_BACKLIGHT_HELPER_EXIT_CODE_FAILED			1
#define GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID	3
#define GSD_BACKLIGHT_HELPER_EXIT_CODE_INVALID_USER		4

#ifndef __linux__
#error "gsd-backlight-helper does not work on non-Linux"
#endif

static void
usage(int argc, char *argv[])
{
	fprintf (stderr, "Usage: %s device brightness\n", argv[0]);
	fprintf (stderr, "  device:      The backlight directory starting with \"/sys/class/backlight/\"\n");
	fprintf (stderr, "  brightness:  The new brightness to write\n");
}

int
main (int argc, char *argv[])
{
	char tmp[512];
	char *device = NULL;
	int fd, len, res;
	int uid, euid;
	int brightness;
	int result = GSD_BACKLIGHT_HELPER_EXIT_CODE_FAILED;
	DIR *dp = NULL;
	struct dirent *ep;

	/* check calling UID */
	uid = getuid ();
	euid = geteuid ();
	if (uid != 0 || euid != 0) {
		fprintf (stderr, "This program can only be used by the root user\n");
		result = GSD_BACKLIGHT_HELPER_EXIT_CODE_INVALID_USER;
		goto done;
	}

	if (argc != 3) {
		fprintf (stderr, "Error: Need to be called with exactly two arguments\n");
		usage (argc, argv);
		result = GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID;
		goto done;
	}

	errno = 0;
	brightness = strtol (argv[2], NULL, 0);
	if (errno) {
		fprintf (stderr, "Error: Invalid brightness argument (%d: %s)\n", errno, strerror (errno));
		usage (argc, argv);
		goto done;
	}

	device = realpath (argv[1], NULL);
	if (device == NULL) {
		fprintf (stderr, "Error: Could not canonicalize given path (%d: %s)\n", errno, strerror (errno));
		result = GSD_BACKLIGHT_HELPER_EXIT_CODE_FAILED;
		goto done;
	}

	dp = opendir ("/sys/class/backlight");
	if (dp == NULL) {
		fprintf (stderr, "Error: Could not open /sys/class/backlight (%d: %s)\n", errno, strerror (errno));
		result = GSD_BACKLIGHT_HELPER_EXIT_CODE_FAILED;
		goto done;
	}

	while ((ep = readdir (dp))) {
		char *path;

		if (ep->d_name[0] == '.')
			continue;

		/* Leave room for "/brightness" */
		snprintf (tmp, sizeof(tmp) - 11, "/sys/class/backlight/%s", ep->d_name);
		path = realpath (tmp, NULL);
		if (strcmp (path, device) == 0) {
			free (path);
			strcat (tmp, "/brightness");

			fd = open (tmp, O_WRONLY);
			if (fd < 0) {
				fprintf (stderr, "Error: Could not open brightness sysfs file (%d: %s)\n", errno, strerror(errno));
				result = GSD_BACKLIGHT_HELPER_EXIT_CODE_FAILED;
				goto done;
			}

			len = snprintf (tmp, sizeof(tmp), "%d", brightness);
			if ((res = write (fd, tmp, len)) != len) {
				if (res == -1)
					fprintf (stderr, "Error: Writing to file (%d: %s)\n", errno, strerror(errno));
				else
					fprintf (stderr, "Error: Wrote the wrong length (%d of %d bytes)!\n", res, len);

				close (fd);
				result = GSD_BACKLIGHT_HELPER_EXIT_CODE_FAILED;
				goto done;
			}
			close (fd);

			result = GSD_BACKLIGHT_HELPER_EXIT_CODE_SUCCESS;
			goto done;
		} else {
			free (path);
		}
	}

	result = GSD_BACKLIGHT_HELPER_EXIT_CODE_FAILED;
	fprintf (stderr, "Error: Could not find the specified backlight \"%s\"\n", argv[1]);

done:
	if (device)
		free (device);
	if (dp)
		closedir (dp);

	return result;
}