~ctf/unity-settings-daemon/bug1389099_mic_volume_icons

« back to all changes in this revision

Viewing changes to plugins/power/gsd-backlight-helper.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-02-07 11:44:36 UTC
  • Revision ID: package-import@ubuntu.com-20140207114436-7t5u3yvwc4ul7w3e
Tags: upstream-14.04.0
ImportĀ upstreamĀ versionĀ 14.04.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2010-2011 Richard Hughes <richard@hughsie.com>
 
4
 *
 
5
 * Licensed under the GNU General Public License Version 2
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <unistd.h>
 
25
#include <glib-object.h>
 
26
#include <locale.h>
 
27
#include <sys/types.h>
 
28
#include <sys/stat.h>
 
29
#include <fcntl.h>
 
30
#include <string.h>
 
31
#include <gudev/gudev.h>
 
32
 
 
33
#include "gsd-backlight-linux.h"
 
34
 
 
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
 
40
 
 
41
static gboolean
 
42
gsd_backlight_helper_write (const gchar *filename, gint value, GError **error)
 
43
{
 
44
        gchar *text = NULL;
 
45
        gint retval;
 
46
        gint length;
 
47
        gint fd = -1;
 
48
        gboolean ret = TRUE;
 
49
 
 
50
        fd = open (filename, O_WRONLY);
 
51
        if (fd < 0) {
 
52
                ret = FALSE;
 
53
                g_set_error (error, 1, 0, "failed to open filename: %s", filename);
 
54
                goto out;
 
55
        }
 
56
 
 
57
        /* convert to text */
 
58
        text = g_strdup_printf ("%i", value);
 
59
        length = strlen (text);
 
60
 
 
61
        /* write to device file */
 
62
        retval = write (fd, text, length);
 
63
        if (retval != length) {
 
64
                ret = FALSE;
 
65
                g_set_error (error, 1, 0, "writing '%s' to %s failed", text, filename);
 
66
                goto out;
 
67
        }
 
68
out:
 
69
        if (fd >= 0)
 
70
                close (fd);
 
71
        g_free (text);
 
72
        return ret;
 
73
}
 
74
 
 
75
int
 
76
main (int argc, char *argv[])
 
77
{
 
78
        GOptionContext *context;
 
79
        gint uid;
 
80
        gint euid;
 
81
        guint retval = 0;
 
82
        GError *error = NULL;
 
83
        gboolean ret = FALSE;
 
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;
 
90
 
 
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 },
 
101
                { NULL}
 
102
        };
 
103
 
 
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);
 
109
 
 
110
#ifndef __linux__
 
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 ();
 
114
#endif
 
115
 
 
116
        /* no input */
 
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;
 
120
                goto out;
 
121
        }
 
122
 
 
123
        /* find device */
 
124
        filename = gsd_backlight_helper_get_best_backlight ();
 
125
        if (filename == NULL) {
 
126
                retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_NO_DEVICES;
 
127
                g_print ("%s: %s\n",
 
128
                         "Could not get or set the value of the backlight",
 
129
                         "No backlight devices present");
 
130
                goto out;
 
131
        }
 
132
 
 
133
        /* GetBrightness */
 
134
        if (get_brightness) {
 
135
                filename_file = g_build_filename (filename, "brightness", NULL);
 
136
                ret = g_file_get_contents (filename_file, &contents, NULL, &error);
 
137
                if (!ret) {
 
138
                        g_print ("%s: %s\n",
 
139
                                 "Could not get the value of the backlight",
 
140
                                 error->message);
 
141
                        g_error_free (error);
 
142
                        retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID;
 
143
                        goto out;
 
144
                }
 
145
 
 
146
                /* just print the contents to stdout */
 
147
                g_print ("%s", contents);
 
148
                retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_SUCCESS;
 
149
                goto out;
 
150
        }
 
151
 
 
152
        /* GetSteps */
 
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);
 
156
                if (!ret) {
 
157
                        g_print ("%s: %s\n",
 
158
                                 "Could not get the maximum value of the backlight",
 
159
                                 error->message);
 
160
                        g_error_free (error);
 
161
                        retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID;
 
162
                        goto out;
 
163
                }
 
164
 
 
165
                /* just print the contents to stdout */
 
166
                g_print ("%s", contents);
 
167
                retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_SUCCESS;
 
168
                goto out;
 
169
        }
 
170
 
 
171
        /* check calling UID */
 
172
        uid = getuid ();
 
173
        euid = geteuid ();
 
174
        if (uid != 0 || euid != 0) {
 
175
                g_print ("%s\n",
 
176
                         "This program can only be used by the root user");
 
177
                retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID;
 
178
                goto out;
 
179
        }
 
180
 
 
181
        /* SetBrightness */
 
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);
 
185
                if (!ret) {
 
186
                        g_print ("%s: %s\n",
 
187
                                 "Could not set the value of the backlight",
 
188
                                 error->message);
 
189
                        g_error_free (error);
 
190
                        retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID;
 
191
                        goto out;
 
192
                }
 
193
        }
 
194
 
 
195
        /* success */
 
196
        retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_SUCCESS;
 
197
out:
 
198
        g_free (filename);
 
199
        g_free (filename_file);
 
200
        g_free (contents);
 
201
        return retval;
 
202
}
 
203