1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3
* Copyright (C) 2010-2011 Richard Hughes <richard@hughsie.com>
5
* Licensed under the GNU General Public License Version 2
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
#include <glib-object.h>
27
#include <sys/types.h>
31
#include <gudev/gudev.h>
33
#include "gsd-backlight-linux.h"
35
#define GSD_BACKLIGHT_HELPER_EXIT_CODE_SUCCESS 0
36
#define GSD_BACKLIGHT_HELPER_EXIT_CODE_FAILED 1
37
#define GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID 3
38
#define GSD_BACKLIGHT_HELPER_EXIT_CODE_INVALID_USER 4
39
#define GSD_BACKLIGHT_HELPER_EXIT_CODE_NO_DEVICES 5
42
gsd_backlight_helper_write (const gchar *filename, gint value, GError **error)
50
fd = open (filename, O_WRONLY);
53
g_set_error (error, 1, 0, "failed to open filename: %s", filename);
58
text = g_strdup_printf ("%i", value);
59
length = strlen (text);
61
/* write to device file */
62
retval = write (fd, text, length);
63
if (retval != length) {
65
g_set_error (error, 1, 0, "writing '%s' to %s failed", text, filename);
76
main (int argc, char *argv[])
78
GOptionContext *context;
84
gint set_brightness = -1;
85
gboolean get_brightness = FALSE;
86
gboolean get_max_brightness = FALSE;
87
gchar *filename = NULL;
88
gchar *filename_file = NULL;
89
gchar *contents = NULL;
91
const GOptionEntry options[] = {
92
{ "set-brightness", '\0', 0, G_OPTION_ARG_INT, &set_brightness,
93
/* command line argument */
94
"Set the current brightness", NULL },
95
{ "get-brightness", '\0', 0, G_OPTION_ARG_NONE, &get_brightness,
96
/* command line argument */
97
"Get the current brightness", NULL },
98
{ "get-max-brightness", '\0', 0, G_OPTION_ARG_NONE, &get_max_brightness,
99
/* command line argument */
100
"Get the number of brightness levels supported", NULL },
104
context = g_option_context_new (NULL);
105
g_option_context_set_summary (context, "GNOME Settings Daemon Backlight Helper");
106
g_option_context_add_main_entries (context, options, NULL);
107
g_option_context_parse (context, &argc, &argv, NULL);
108
g_option_context_free (context);
111
/* the g-s-d plugin should only call this helper on linux */
112
g_critical ("Attempting to call gsb-backlight-helper on non-Linux");
113
g_assert_not_reached ();
117
if (set_brightness == -1 && !get_brightness && !get_max_brightness) {
118
g_print ("%s\n", "No valid option was specified");
119
retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID;
124
filename = gsd_backlight_helper_get_best_backlight ();
125
if (filename == NULL) {
126
retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_NO_DEVICES;
128
"Could not get or set the value of the backlight",
129
"No backlight devices present");
134
if (get_brightness) {
135
filename_file = g_build_filename (filename, "brightness", NULL);
136
ret = g_file_get_contents (filename_file, &contents, NULL, &error);
139
"Could not get the value of the backlight",
141
g_error_free (error);
142
retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID;
146
/* just print the contents to stdout */
147
g_print ("%s", contents);
148
retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_SUCCESS;
153
if (get_max_brightness) {
154
filename_file = g_build_filename (filename, "max_brightness", NULL);
155
ret = g_file_get_contents (filename_file, &contents, NULL, &error);
158
"Could not get the maximum value of the backlight",
160
g_error_free (error);
161
retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID;
165
/* just print the contents to stdout */
166
g_print ("%s", contents);
167
retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_SUCCESS;
171
/* check calling UID */
174
if (uid != 0 || euid != 0) {
176
"This program can only be used by the root user");
177
retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID;
182
if (set_brightness != -1) {
183
filename_file = g_build_filename (filename, "brightness", NULL);
184
ret = gsd_backlight_helper_write (filename_file, set_brightness, &error);
187
"Could not set the value of the backlight",
189
g_error_free (error);
190
retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID;
196
retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_SUCCESS;
199
g_free (filename_file);