Merge lp:~fourdollars/unity-settings-daemon/fix-lowest-brightness into lp:unity-settings-daemon/14.04

Proposed by Shih-Yuan Lee
Status: Rejected
Rejected by: Iain Lane
Proposed branch: lp:~fourdollars/unity-settings-daemon/fix-lowest-brightness
Merge into: lp:unity-settings-daemon/14.04
Diff against target: 264 lines (+107/-23)
4 files modified
plugins/power/gpm-common.c (+1/-1)
plugins/power/gsd-backlight-helper.c (+86/-17)
plugins/power/gsd-backlight-linux.c (+13/-4)
plugins/power/gsd-backlight-linux.h (+7/-1)
To merge this branch: bzr merge lp:~fourdollars/unity-settings-daemon/fix-lowest-brightness
Reviewer Review Type Date Requested Status
Unity Settings Daemon Development Team Pending
Review via email: mp+278108@code.launchpad.net

Commit message

power: On raw backlight types, clamp the value to a minumum

Description of the change

Please help to review this change.
This change has been applied in xenial.
I am backporting it into trusty.

To post a comment you must log in.
Revision history for this message
Iain Lane (laney) wrote :

You said on the bug that this isn't necessary, so closing it

Unmerged revisions

4049. By Shih-Yuan Lee

power: On raw backlight types, clamp the value to a minumum

Backlight class brightness 0 is to disable the backlight since kernel 3.18.
This behavior is by design for the backlight control in i915 kernel module.
http://lists.freedesktop.org/archives/intel-gfx/2014-August/050642.html

However gnome-settings-daemon has included some changeset to fix this issue.
https://bugzilla.gnome.org/show_bug.cgi?id=744278

unity-settings-daemon should include the same changeset to fix this issue. (LP: #1381625)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'plugins/power/gpm-common.c'
2--- plugins/power/gpm-common.c 2013-12-04 23:55:26 +0000
3+++ plugins/power/gpm-common.c 2015-11-20 08:06:29 +0000
4@@ -1248,7 +1248,7 @@
5 #endif
6 if (get_primary_output (rr_screen) != NULL)
7 return TRUE;
8- path = gsd_backlight_helper_get_best_backlight ();
9+ path = gsd_backlight_helper_get_best_backlight (NULL);
10 if (path == NULL)
11 return FALSE;
12
13
14=== modified file 'plugins/power/gsd-backlight-helper.c'
15--- plugins/power/gsd-backlight-helper.c 2013-01-23 22:53:38 +0000
16+++ plugins/power/gsd-backlight-helper.c 2015-11-20 08:06:29 +0000
17@@ -21,6 +21,7 @@
18
19 #include "config.h"
20
21+#include <stdlib.h>
22 #include <unistd.h>
23 #include <glib-object.h>
24 #include <locale.h>
25@@ -41,13 +42,16 @@
26 static gboolean
27 gsd_backlight_helper_write (const gchar *filename, gint value, GError **error)
28 {
29+ gchar *filename_path = NULL;
30 gchar *text = NULL;
31 gint retval;
32 gint length;
33 gint fd = -1;
34 gboolean ret = TRUE;
35
36- fd = open (filename, O_WRONLY);
37+ filename_path = g_build_filename (filename, "brightness", NULL);
38+
39+ fd = open (filename_path, O_WRONLY);
40 if (fd < 0) {
41 ret = FALSE;
42 g_set_error (error, 1, 0, "failed to open filename: %s", filename);
43@@ -69,9 +73,64 @@
44 if (fd >= 0)
45 close (fd);
46 g_free (text);
47+ g_free (filename_path);
48 return ret;
49 }
50
51+static gint
52+gsd_backlight_helper_read_value (const gchar *filename, GError **error)
53+{
54+ gchar *contents = NULL;
55+ gint value;
56+
57+ if (g_file_get_contents (filename, &contents, NULL, error))
58+ value = atoi (contents);
59+ else
60+ value = -1;
61+ g_free (contents);
62+
63+ return value;
64+}
65+
66+static gint
67+gsd_backlight_helper_get (const gchar *filename, GError **error)
68+{
69+ gchar *filename_path = NULL;
70+ gint value;
71+
72+ filename_path = g_build_filename (filename, "brightness", NULL);
73+ value = gsd_backlight_helper_read_value (filename_path, error);
74+ g_free (filename_path);
75+ return value;
76+}
77+
78+static gint
79+gsd_backlight_helper_get_max (const gchar *filename, GError **error)
80+{
81+ gchar *filename_path = NULL;
82+ gint value;
83+
84+ filename_path = g_build_filename (filename, "max_brightness", NULL);
85+ value = gsd_backlight_helper_read_value (filename_path, error);
86+ g_free (filename_path);
87+ return value;
88+}
89+
90+static gint
91+clamp_minimum (gint max, gint value)
92+{
93+ gint minimum;
94+ /* If the interface has less than 100 possible values, it's
95+ * likely that 0 doesn't turn the backlight off so we let 0 be
96+ * set in that case. */
97+ if (max > 99)
98+ minimum = 1;
99+ else
100+ minimum = 0;
101+
102+ return MAX (value, minimum);
103+}
104+
105 int
106 main (int argc, char *argv[])
107 {
108@@ -80,13 +139,11 @@
109 gint euid;
110 guint retval = 0;
111 GError *error = NULL;
112- gboolean ret = FALSE;
113 gint set_brightness = -1;
114 gboolean get_brightness = FALSE;
115 gboolean get_max_brightness = FALSE;
116 gchar *filename = NULL;
117- gchar *filename_file = NULL;
118- gchar *contents = NULL;
119+ GsdBacklightType type;
120
121 const GOptionEntry options[] = {
122 { "set-brightness", '\0', 0, G_OPTION_ARG_INT, &set_brightness,
123@@ -121,7 +178,7 @@
124 }
125
126 /* find device */
127- filename = gsd_backlight_helper_get_best_backlight ();
128+ filename = gsd_backlight_helper_get_best_backlight (&type);
129 if (filename == NULL) {
130 retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_NO_DEVICES;
131 g_print ("%s: %s\n",
132@@ -132,9 +189,9 @@
133
134 /* GetBrightness */
135 if (get_brightness) {
136- filename_file = g_build_filename (filename, "brightness", NULL);
137- ret = g_file_get_contents (filename_file, &contents, NULL, &error);
138- if (!ret) {
139+ gint value;
140+ value = gsd_backlight_helper_get (filename, &error);
141+ if (value < 0) {
142 g_print ("%s: %s\n",
143 "Could not get the value of the backlight",
144 error->message);
145@@ -144,16 +201,16 @@
146 }
147
148 /* just print the contents to stdout */
149- g_print ("%s", contents);
150+ g_print ("%d", value);
151 retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_SUCCESS;
152 goto out;
153 }
154
155 /* GetSteps */
156 if (get_max_brightness) {
157- filename_file = g_build_filename (filename, "max_brightness", NULL);
158- ret = g_file_get_contents (filename_file, &contents, NULL, &error);
159- if (!ret) {
160+ gint value;
161+ value = gsd_backlight_helper_get_max (filename, &error);
162+ if (value < 0) {
163 g_print ("%s: %s\n",
164 "Could not get the maximum value of the backlight",
165 error->message);
166@@ -163,7 +220,7 @@
167 }
168
169 /* just print the contents to stdout */
170- g_print ("%s", contents);
171+ g_print ("%d", value);
172 retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_SUCCESS;
173 goto out;
174 }
175@@ -180,8 +237,22 @@
176
177 /* SetBrightness */
178 if (set_brightness != -1) {
179- filename_file = g_build_filename (filename, "brightness", NULL);
180- ret = gsd_backlight_helper_write (filename_file, set_brightness, &error);
181+ gboolean ret = FALSE;
182+ gint max = gsd_backlight_helper_get_max (filename, &error);
183+
184+ if (max < 0) {
185+ g_print ("%s: %s\n",
186+ "Could not get the maximum value of the backlight",
187+ error->message);
188+ g_error_free (error);
189+ retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID;
190+ goto out;
191+ }
192+
193+ if (type == GSD_BACKLIGHT_TYPE_RAW)
194+ set_brightness = clamp_minimum (max, set_brightness);
195+
196+ ret = gsd_backlight_helper_write (filename, set_brightness, &error);
197 if (!ret) {
198 g_print ("%s: %s\n",
199 "Could not set the value of the backlight",
200@@ -196,8 +267,6 @@
201 retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_SUCCESS;
202 out:
203 g_free (filename);
204- g_free (filename_file);
205- g_free (contents);
206 return retval;
207 }
208
209
210=== modified file 'plugins/power/gsd-backlight-linux.c'
211--- plugins/power/gsd-backlight-linux.c 2013-03-27 11:59:18 +0000
212+++ plugins/power/gsd-backlight-linux.c 2015-11-20 08:06:29 +0000
213@@ -44,7 +44,7 @@
214 #endif /* HAVE_GUDEV */
215
216 char *
217-gsd_backlight_helper_get_best_backlight (void)
218+gsd_backlight_helper_get_best_backlight (GsdBacklightType *type)
219 {
220 #ifdef HAVE_GUDEV
221 gchar *path = NULL;
222@@ -59,14 +59,23 @@
223 /* search the backlight devices and prefer the types:
224 * firmware -> platform -> raw */
225 path = gsd_backlight_helper_get_type (devices, "firmware");
226- if (path != NULL)
227+ if (path != NULL) {
228+ if (type)
229+ *type = GSD_BACKLIGHT_TYPE_FIRMWARE;
230 goto out;
231+ }
232 path = gsd_backlight_helper_get_type (devices, "platform");
233- if (path != NULL)
234+ if (path != NULL) {
235+ if (type)
236+ *type = GSD_BACKLIGHT_TYPE_PLATFORM;
237 goto out;
238+ }
239 path = gsd_backlight_helper_get_type (devices, "raw");
240- if (path != NULL)
241+ if (path != NULL) {
242+ if (type)
243+ *type = GSD_BACKLIGHT_TYPE_RAW;
244 goto out;
245+ }
246 out:
247 g_object_unref (client);
248 g_list_foreach (devices, (GFunc) g_object_unref, NULL);
249
250=== modified file 'plugins/power/gsd-backlight-linux.h'
251--- plugins/power/gsd-backlight-linux.h 2013-01-23 22:53:38 +0000
252+++ plugins/power/gsd-backlight-linux.h 2015-11-20 08:06:29 +0000
253@@ -19,4 +19,10 @@
254 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
255 */
256
257-char *gsd_backlight_helper_get_best_backlight (void);
258+typedef enum {
259+ GSD_BACKLIGHT_TYPE_FIRMWARE,
260+ GSD_BACKLIGHT_TYPE_PLATFORM,
261+ GSD_BACKLIGHT_TYPE_RAW,
262+} GsdBacklightType;
263+
264+char *gsd_backlight_helper_get_best_backlight (GsdBacklightType *type);

Subscribers

People subscribed via source and target branches