summaryrefslogtreecommitdiff
path: root/src/up-kbd-backlight.c
blob: f9663e799594e823102d8176c827a0245250cf39 (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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
 *               2010 Alex Murray <murray.alex@gmail.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 <glib.h>
#include <glib/gi18n.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>

#include "up-kbd-backlight.h"
#include "up-daemon.h"
#include "up-types.h"

static void     up_kbd_backlight_finalize   (GObject	*object);

struct UpKbdBacklightPrivate
{
	gint			 fd;
	gint			 fd_hw_changed;
	GIOChannel		*channel_hw_changed;
	gint			 max_brightness;
};

G_DEFINE_TYPE_WITH_PRIVATE (UpKbdBacklight, up_kbd_backlight, UP_TYPE_EXPORTED_KBD_BACKLIGHT_SKELETON)

/**
 * up_kbd_backlight_emit_change:
 **/
static void
up_kbd_backlight_emit_change(UpKbdBacklight *kbd_backlight, int value, const char *source)
{
	up_exported_kbd_backlight_emit_brightness_changed (UP_EXPORTED_KBD_BACKLIGHT (kbd_backlight), value);
	up_exported_kbd_backlight_emit_brightness_changed_with_source (UP_EXPORTED_KBD_BACKLIGHT (kbd_backlight), value, source);
}

/**
 * up_kbd_backlight_brightness_read:
 **/
static gint
up_kbd_backlight_brightness_read (UpKbdBacklight *kbd_backlight, int fd)
{
	gchar buf[16];
	gchar *end = NULL;
	ssize_t len;
	gint64 brightness = -1;

	g_return_val_if_fail (fd >= 0, brightness);

	lseek (fd, 0, SEEK_SET);
	len = read (fd, buf, G_N_ELEMENTS (buf) - 1);

	if (len > 0) {
		buf[len] = '\0';
		brightness = g_ascii_strtoll (buf, &end, 10);

		if (brightness < 0 ||
		    brightness > kbd_backlight->priv->max_brightness ||
		    end == buf) {
			brightness = -1;
			g_warning ("failed to convert brightness: %s", buf);
		}
	}

	return brightness;
}

/**
 * up_kbd_backlight_brightness_write:
 **/
static gboolean
up_kbd_backlight_brightness_write (UpKbdBacklight *kbd_backlight, gint value)
{
	gchar *text = NULL;
	gint retval;
	gint length;
	gboolean ret = TRUE;

	/* write new values to backlight */
	if (kbd_backlight->priv->fd < 0) {
		g_warning ("cannot write to kbd_backlight as file not open");
		ret = FALSE;
		goto out;
	}

	/* limit to between 0 and max */
	value = CLAMP (value, 0, kbd_backlight->priv->max_brightness);

	/* convert to text */
	text = g_strdup_printf ("%i", value);
	length = strlen (text);

	/* write to file */
	lseek (kbd_backlight->priv->fd, 0, SEEK_SET);
	retval = write (kbd_backlight->priv->fd, text, length);
	if (retval != length) {
		g_warning ("writing '%s' to device failed", text);
		ret = FALSE;
		goto out;
	}

	/* emit signal */
	up_kbd_backlight_emit_change (kbd_backlight, value, "external");

out:
	g_free (text);
	return ret;
}

/**
 * up_kbd_backlight_get_brightness:
 *
 * Gets the current brightness
 **/
static gboolean
up_kbd_backlight_get_brightness (UpExportedKbdBacklight *skeleton,
				 GDBusMethodInvocation *invocation,
				 UpKbdBacklight *kbd_backlight)
{
	gint brightness;

	brightness = up_kbd_backlight_brightness_read (kbd_backlight, kbd_backlight->priv->fd);

	if (brightness >= 0) {
		up_exported_kbd_backlight_complete_get_brightness (skeleton, invocation,
								   brightness);
	} else {
		g_dbus_method_invocation_return_error (invocation,
						       UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL,
						       "error reading brightness");
	}

	return TRUE;
}

/**
 * up_kbd_backlight_get_max_brightness:
 *
 * Gets the max brightness
 **/
static gboolean
up_kbd_backlight_get_max_brightness (UpExportedKbdBacklight *skeleton,
				     GDBusMethodInvocation *invocation,
				     UpKbdBacklight *kbd_backlight)
{
	up_exported_kbd_backlight_complete_get_max_brightness (skeleton, invocation,
							       kbd_backlight->priv->max_brightness);
	return TRUE;
}

/**
 * up_kbd_backlight_set_brightness:
 **/
static gboolean
up_kbd_backlight_set_brightness (UpExportedKbdBacklight *skeleton,
				 GDBusMethodInvocation *invocation,
				 gint value,
				 UpKbdBacklight *kbd_backlight)
{
	gboolean ret = FALSE;

	g_debug ("setting brightness to %i", value);
	ret = up_kbd_backlight_brightness_write (kbd_backlight, value);

	if (ret) {
		up_exported_kbd_backlight_complete_set_brightness (skeleton, invocation);
	} else {
		g_dbus_method_invocation_return_error (invocation,
						       UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL,
						       "error writing brightness %d", value);
	}

	return TRUE;
}

/**
 * up_kbd_backlight_class_init:
 **/
static void
up_kbd_backlight_class_init (UpKbdBacklightClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = up_kbd_backlight_finalize;
}

/**
 * up_kbd_backlight_event_io:
 **/
static gboolean
up_kbd_backlight_event_io (GIOChannel *channel, GIOCondition condition, gpointer data)
{
	UpKbdBacklight *kbd_backlight = (UpKbdBacklight*) data;
	gint brightness;

	if (!(condition & G_IO_PRI))
		return FALSE;

	brightness = up_kbd_backlight_brightness_read (kbd_backlight, kbd_backlight->priv->fd_hw_changed);
	if (brightness < 0 && errno == ENODEV)
		return FALSE;

	if (brightness >= 0)
		up_kbd_backlight_emit_change (kbd_backlight, brightness, "internal");

	return TRUE;
}

/**
 * up_kbd_backlight_find:
 **/
static gboolean
up_kbd_backlight_find (UpKbdBacklight *kbd_backlight)
{
	gboolean ret;
	gboolean found = FALSE;
	GDir *dir;
	const gchar *filename;
	gchar *end = NULL;
	gchar *dir_path = NULL;
	gchar *path_max = NULL;
	gchar *path_now = NULL;
	gchar *path_hw_changed = NULL;
	gchar *buf_max = NULL;
	gchar *buf_now = NULL;
	GError *error = NULL;

	kbd_backlight->priv->fd = -1;

	/* open directory */
	dir = g_dir_open ("/sys/class/leds", 0, &error);
	if (dir == NULL) {
		if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
			g_warning ("failed to open directory: %s", error->message);
		g_error_free (error);
		goto out;
	}

	/* find a led device that is a keyboard device */
	while ((filename = g_dir_read_name (dir)) != NULL) {
		if (g_strstr_len (filename, -1, "kbd_backlight") != NULL) {
			dir_path = g_build_filename ("/sys/class/leds",
						    filename, NULL);
			break;
		}
	}

	/* nothing found */
	if (dir_path == NULL)
		goto out;

	/* read max brightness */
	path_max = g_build_filename (dir_path, "max_brightness", NULL);
	ret = g_file_get_contents (path_max, &buf_max, NULL, &error);
	if (!ret) {
		g_warning ("failed to get max brightness: %s", error->message);
		g_error_free (error);
		goto out;
	}
	kbd_backlight->priv->max_brightness = g_ascii_strtoull (buf_max, &end, 10);
	if (kbd_backlight->priv->max_brightness == 0 && end == buf_max) {
		g_warning ("failed to convert max brightness: %s", buf_max);
		goto out;
	}

	/* open the brightness file for read and write operations */
	path_now = g_build_filename (dir_path, "brightness", NULL);
	kbd_backlight->priv->fd = open (path_now, O_RDWR);

	/* read brightness and check if it has an acceptable value */
	if (up_kbd_backlight_brightness_read (kbd_backlight, kbd_backlight->priv->fd) < 0)
		goto out;

	path_hw_changed = g_build_filename (dir_path, "brightness_hw_changed", NULL);
	kbd_backlight->priv->fd_hw_changed = open (path_hw_changed, O_RDONLY);
	if (kbd_backlight->priv->fd_hw_changed >= 0) {
		kbd_backlight->priv->channel_hw_changed = g_io_channel_unix_new (kbd_backlight->priv->fd_hw_changed);
		g_io_add_watch (kbd_backlight->priv->channel_hw_changed,
				G_IO_PRI, up_kbd_backlight_event_io, kbd_backlight);
	}

	/* success */
	found = TRUE;
out:
	if (dir != NULL)
		g_dir_close (dir);
	g_free (dir_path);
	g_free (path_max);
	g_free (path_now);
	g_free (path_hw_changed);
	g_free (buf_max);
	g_free (buf_now);
	return found;
}

/**
 * up_kbd_backlight_init:
 **/
static void
up_kbd_backlight_init (UpKbdBacklight *kbd_backlight)
{
	kbd_backlight->priv = up_kbd_backlight_get_instance_private (kbd_backlight);

	g_signal_connect (kbd_backlight, "handle-get-brightness",
			  G_CALLBACK (up_kbd_backlight_get_brightness), kbd_backlight);
	g_signal_connect (kbd_backlight, "handle-get-max-brightness",
			  G_CALLBACK (up_kbd_backlight_get_max_brightness), kbd_backlight);
	g_signal_connect (kbd_backlight, "handle-set-brightness",
			  G_CALLBACK (up_kbd_backlight_set_brightness), kbd_backlight);
}

/**
 * up_kbd_backlight_finalize:
 **/
static void
up_kbd_backlight_finalize (GObject *object)
{
	UpKbdBacklight *kbd_backlight;

	g_return_if_fail (object != NULL);
	g_return_if_fail (UP_IS_KBD_BACKLIGHT (object));

	kbd_backlight = UP_KBD_BACKLIGHT (object);
	kbd_backlight->priv = up_kbd_backlight_get_instance_private (kbd_backlight);

	if (kbd_backlight->priv->channel_hw_changed) {
		g_io_channel_shutdown (kbd_backlight->priv->channel_hw_changed, FALSE, NULL);
		g_io_channel_unref (kbd_backlight->priv->channel_hw_changed);
	}

	if (kbd_backlight->priv->fd_hw_changed >= 0)
		close (kbd_backlight->priv->fd_hw_changed);

	/* close file */
	if (kbd_backlight->priv->fd >= 0)
		close (kbd_backlight->priv->fd);

	G_OBJECT_CLASS (up_kbd_backlight_parent_class)->finalize (object);
}

/**
 * up_kbd_backlight_new:
 **/
UpKbdBacklight *
up_kbd_backlight_new (void)
{
	return g_object_new (UP_TYPE_KBD_BACKLIGHT, NULL);
}

void
up_kbd_backlight_register (UpKbdBacklight *kbd_backlight,
			   GDBusConnection *connection)
{
	GError *error = NULL;

	/* find a kbd backlight in sysfs */
	if (!up_kbd_backlight_find (kbd_backlight)) {
		g_debug ("cannot find a keyboard backlight");
		return;
	}

	g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (kbd_backlight),
					  connection,
					  "/org/freedesktop/UPower/KbdBacklight",
					  &error);

	if (error != NULL) {
		g_warning ("Cannot export KbdBacklight object to bus: %s", error->message);
		g_error_free (error);
	}
}