summaryrefslogtreecommitdiff
path: root/src/freebsd/up-backend.c
blob: 01465e242ce759353d41fb9acef93b47759e1a74 (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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2009 Joe Marcus Clarke <marcus@FreeBSD.org>
 *
 * 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 <math.h>
#include <stdio.h>
#include <string.h>
#include <kvm.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#include <sys/types.h>
#include <glib/gi18n.h>
#include <gio/gio.h>

#include "up-acpi-native.h"
#include "up-backend-acpi.h"
#include "up-devd.h"
#include "up-device-supply.h"
#include "up-util.h"

#include "egg-debug.h"

#include "up-backend.h"
#include "up-daemon.h"
#include "up-marshal.h"
#include "up-device.h"

#define UP_BACKEND_REFRESH_TIMEOUT	30	/* seconds */
#define UP_BACKEND_SUSPEND_COMMAND	"/usr/sbin/zzz"
#define UP_BACKEND_HIBERNATE_COMMAND	"/usr/sbin/acpiconf -s 4"

static void	up_backend_class_init	(UpBackendClass	*klass);
static void	up_backend_init	(UpBackend		*backend);
static void	up_backend_finalize	(GObject		*object);

static gboolean	up_backend_refresh_devices (gpointer user_data);
static gboolean	up_backend_acpi_devd_notify (UpBackend *backend, const gchar *system, const gchar *subsystem, const gchar *type, const gchar *data);
static gboolean	up_backend_create_new_device (UpBackend *backend, UpAcpiNative *native);
static void	up_backend_lid_coldplug (UpBackend *backend);
static gboolean	up_backend_supports_sleep_state (const gchar *state);

#define UP_BACKEND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_BACKEND, UpBackendPrivate))

struct UpBackendPrivate
{
	UpDaemon		*daemon;
	UpDeviceList		*device_list;
	GHashTable		*handle_map;
	guint			poll_timer_id;
};

enum {
	SIGNAL_DEVICE_ADDED,
	SIGNAL_DEVICE_REMOVED,
	SIGNAL_LAST
};

static guint signals [SIGNAL_LAST] = { 0 };

G_DEFINE_TYPE (UpBackend, up_backend, G_TYPE_OBJECT)

static const gchar *handlers[] = {
	"battery",
};

UpDevdHandler up_backend_acpi_devd_handler = {
	.notify = up_backend_acpi_devd_notify
};

/**
 * up_backend_refresh_devices:
 **/
static gboolean
up_backend_refresh_devices (gpointer user_data)
{
	UpBackend *backend;
	GPtrArray *array;
	UpDevice *device;
	guint i;

	backend = UP_BACKEND (user_data);
	array = up_device_list_get_array (backend->priv->device_list);

	for (i = 0; i < array->len; i++) {
		device = UP_DEVICE (g_ptr_array_index (array, i));
		up_device_refresh_internal (device);
	}

	g_ptr_array_unref (array);

	return TRUE;
}

/**
 * up_backend_acpi_devd_notify:
 **/
static gboolean
up_backend_acpi_devd_notify (UpBackend *backend, const gchar *system, const gchar *subsystem, const gchar *type, const gchar *data)
{
	GObject *object = NULL;
	UpAcpiNative *native = NULL;

	if (strcmp (system, "ACPI"))
		return FALSE;

	if (!strcmp (subsystem, "ACAD")) {
		native = up_acpi_native_new ("hw.acpi.acline");
		object = up_device_list_lookup (backend->priv->device_list, G_OBJECT (native));
	} else if (!strcmp (subsystem, "CMBAT")) {
		gchar *ptr;
		int unit;

		ptr = strstr (type, ".BAT");

		if (ptr != NULL && sscanf (ptr, ".BAT%i", &unit)) {
			native = up_acpi_native_new_driver_unit ("battery", unit);
			object = up_device_list_lookup (backend->priv->device_list, G_OBJECT (native));
			if (object == NULL) {
				gpointer hptr;

				hptr = g_hash_table_lookup (backend->priv->handle_map, type);
				if (hptr != NULL) {
					object = up_device_list_lookup (backend->priv->device_list, G_OBJECT (hptr));
				}
			}
		}
	} else if (!strcmp (subsystem, "Lid")) {
		gboolean is_present;
		gboolean is_closed;

		g_object_get (backend->priv->daemon,
			      "lid-is-present", &is_present, NULL);
		if (!is_present) {
			egg_warning ("received lid event without a configured lid; cold-plugging one");
			up_backend_lid_coldplug (backend);
			/* FALLTHROUGH */
		}

		is_closed = (data != NULL && !strcmp (data, "notify=0x00")) ? TRUE : FALSE;
		up_daemon_set_lid_is_closed (backend->priv->daemon, is_closed);
		goto out;
	}

	if (native == NULL)
		goto out;

	if (object == NULL) {
		egg_warning ("did not find existing %s device; cold-plugging a new one", subsystem);
		up_backend_create_new_device (backend, native);
		goto out;
	}

	up_device_refresh_internal (UP_DEVICE (object));

	if (object != NULL)
		g_object_unref (object);
out:
	if (native != NULL)
		g_object_unref (native);

	return TRUE;
}

/**
 * up_backend_create_new_device:
 **/
static gboolean
up_backend_create_new_device (UpBackend *backend, UpAcpiNative *native)
{
	UpDevice *device;
	gboolean ret;

	device = UP_DEVICE (up_device_supply_new ());
	ret = up_device_coldplug (device, backend->priv->daemon, G_OBJECT (native));
	if (!ret)
		g_object_unref (device);
	else {
		if (!strncmp (up_acpi_native_get_path (native), "dev.", strlen ("dev."))) {
			const gchar *path;

			path = up_acpi_native_get_path (native);
			if (up_has_sysctl ("%s.%%location", path)) {
				gchar *location;

				location = up_get_string_sysctl (NULL, "%s.%%location", path);
				if (location != NULL && strstr (location, "handle=") != NULL) {
					gchar *handle;

					handle = strstr (location, "handle=");
					handle += strlen ("handle=");
					g_hash_table_insert (backend->priv->handle_map, g_strdup (handle), g_object_ref (native));
				}
				g_free (location);
			}
		}

		g_signal_emit (backend, signals[SIGNAL_DEVICE_ADDED], 0, native, device);
	}

	return ret;
}

/**
 * up_backend_lid_coldplug:
 **/
static void
up_backend_lid_coldplug (UpBackend *backend)
{
	gchar *lid_state;

	lid_state = up_get_string_sysctl (NULL, "hw.acpi.lid_switch_state");
	if (lid_state && strcmp (lid_state, "NONE")) {
		up_daemon_set_lid_is_present (backend->priv->daemon, TRUE);
	}
	g_free (lid_state);
}

/**
 * up_backend_coldplug:
 * @backend: The %UpBackend class instance
 * @daemon: The %UpDaemon controlling instance
 *
 * Finds all the devices already plugged in, and emits device-add signals for
 * each of them.
 *
 * Return value: %TRUE for success
 **/
gboolean
up_backend_coldplug (UpBackend *backend, UpDaemon *daemon)
{
	UpAcpiNative *acnative;
	int i;

	backend->priv->daemon = g_object_ref (daemon);
	backend->priv->device_list = up_daemon_get_device_list (daemon);

	for (i = 0; i < (int) G_N_ELEMENTS (handlers); i++) {
		int j;

		for (j = 0; up_has_sysctl ("dev.%s.%i.%%driver", handlers[i], j); j++) {
			UpAcpiNative *native;
			UpDevice *device;
			GObject *object;

			native = up_acpi_native_new_driver_unit (handlers[i], j);
			object = up_device_list_lookup (backend->priv->device_list, G_OBJECT (native));
			if (object != NULL) {
				device = UP_DEVICE (object);
				egg_warning ("treating add event as change event on %s", up_device_get_object_path (device));
				up_device_refresh_internal (device);
			} else {
				up_backend_create_new_device (backend, native);
			}

			if (object != NULL) {
				g_object_unref (object);
			}
			if (native != NULL) {
				g_object_unref (native);
			}
		}
	}

	up_backend_lid_coldplug (backend);

	acnative = up_acpi_native_new ("hw.acpi.acline");
	up_backend_create_new_device (backend, acnative);
	g_object_unref (acnative);

	up_devd_init (backend);

	backend->priv->poll_timer_id =
		g_timeout_add_seconds (UP_BACKEND_REFRESH_TIMEOUT,
			       (GSourceFunc) up_backend_refresh_devices,
			       backend);
#if GLIB_CHECK_VERSION(2,25,8)
	g_source_set_name_by_id (backend->priv->poll_timer_id, "[FreeBSD:UpBackend] poll");
#endif

	return TRUE;
}

/**
 * up_backend_get_powersave_command:
 **/
const gchar *
up_backend_get_powersave_command (UpBackend *backend, gboolean powersave)
{
	/* XXX: Do we want to use powerd here? */
	return NULL;
}

/**
 * up_backend_get_suspend_command:
 **/
const gchar *
up_backend_get_suspend_command (UpBackend *backend)
{
	return UP_BACKEND_SUSPEND_COMMAND;
}

/**
 * up_backend_get_hibernate_command:
 **/
const gchar *
up_backend_get_hibernate_command (UpBackend *backend)
{
	return UP_BACKEND_HIBERNATE_COMMAND;
}

/**
 * up_backend_kernel_can_suspend:
 **/
gboolean
up_backend_kernel_can_suspend (UpBackend *backend)
{
	return up_backend_supports_sleep_state ("S3");
}

/**
 * up_backend_kernel_can_hibernate:
 **/
gboolean
up_backend_kernel_can_hibernate (UpBackend *backend)
{
	return up_backend_supports_sleep_state ("S4");
}

gboolean
up_backend_has_encrypted_swap (UpBackend *backend)
{
	/* XXX: Add support for GELI? */
	return FALSE;
}

/* Return value: a percentage value */
gfloat
up_backend_get_used_swap (UpBackend *backend)
{
	gfloat percent;
	kvm_t *kd;
	gchar errbuf[_POSIX2_LINE_MAX];
	int nswdev;
	struct kvm_swap kvmsw[16];

	kd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, errbuf);
	if (kd == NULL) {
		egg_warning ("failed to open kvm: '%s'", errbuf);
		return 0.0f;
	}

	nswdev = kvm_getswapinfo (kd, kvmsw, 16, 0);
	if (nswdev == 0) {
		percent = 100.0f;
		goto out;
	}
	if (nswdev < 0) {
		egg_warning ("failed to get swap info: '%s'", kvm_geterr (kd));
		percent = 0.0f;
		goto out;
	}

	percent = (gfloat) ((gfloat) ((gfloat) kvmsw[nswdev].ksw_used / (gfloat) kvmsw[nswdev].ksw_total) * 100.0f);

out:
	kvm_close (kd);

	return percent;
}

/**
 * up_backend_supports_sleep_state:
 **/
static gboolean
up_backend_supports_sleep_state (const gchar *state)
{
	gchar *sleep_states;
	gboolean ret = FALSE;

	sleep_states = up_get_string_sysctl (NULL, "hw.acpi.supported_sleep_state");
	if (sleep_states != NULL) {
		if (strstr (sleep_states, state) != NULL)
			ret = TRUE;
	}

	g_free (sleep_states);

	return ret;
}

/**
 * up_backend_class_init:
 * @klass: The UpBackendClass
 **/
static void
up_backend_class_init (UpBackendClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = up_backend_finalize;

	signals [SIGNAL_DEVICE_ADDED] =
		g_signal_new ("device-added",
			      G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (UpBackendClass, device_added),
			      NULL, NULL, up_marshal_VOID__POINTER_POINTER,
			      G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);
	signals [SIGNAL_DEVICE_REMOVED] =
		g_signal_new ("device-removed",
			      G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (UpBackendClass, device_removed),
			      NULL, NULL, up_marshal_VOID__POINTER_POINTER,
			      G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);

	g_type_class_add_private (klass, sizeof (UpBackendPrivate));
}

/**
 * up_backend_init:
 **/
static void
up_backend_init (UpBackend *backend)
{
	backend->priv = UP_BACKEND_GET_PRIVATE (backend);
	backend->priv->daemon = NULL;
	backend->priv->device_list = NULL;
	backend->priv->handle_map = g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) g_free, (GDestroyNotify) g_object_unref);
	backend->priv->poll_timer_id = 0;
}

/**
 * up_backend_finalize:
 **/
static void
up_backend_finalize (GObject *object)
{
	UpBackend *backend;

	g_return_if_fail (UP_IS_BACKEND (object));

	backend = UP_BACKEND (object);

	if (backend->priv->daemon != NULL)
		g_object_unref (backend->priv->daemon);
	if (backend->priv->device_list != NULL)
		g_object_unref (backend->priv->device_list);
	if (backend->priv->handle_map != NULL)
		g_hash_table_unref (backend->priv->handle_map);
	if (backend->priv->poll_timer_id > 0)
		g_source_remove (backend->priv->poll_timer_id);

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

/**
 * up_backend_new:
 *
 * Return value: a new %UpBackend object.
 **/
UpBackend *
up_backend_new (void)
{
	UpBackend *backend;
	backend = g_object_new (UP_TYPE_BACKEND, NULL);
	return UP_BACKEND (backend);
}