1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3
* Copyright (C) 2011 Landry Breuil <landry@openbsd.org>
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.
22
#include "up-apm-native.h"
23
#include "up-native.h"
25
/* XXX why does this macro needs to be in the .c ? */
26
G_DEFINE_TYPE (UpApmNative, up_apm_native, G_TYPE_OBJECT)
29
up_apm_native_class_init (UpApmNativeClass *klass)
34
up_apm_native_init (UpApmNative *self)
40
up_apm_native_new(const gchar * path)
43
native = UP_APM_NATIVE (g_object_new (UP_TYPE_APM_NATIVE, NULL));
44
native->path = g_strdup(path);
49
up_apm_native_get_path(UpApmNative * native)
55
* up_native_get_native_path:
56
* @object: the native tracking object
58
* This converts a GObject used as the device data into a native path.
60
* Return value: The native path for the device which is unique, e.g. "/sys/class/power/BAT1"
63
up_native_get_native_path (GObject *object)
65
return up_apm_native_get_path (UP_APM_NATIVE (object));
69
* detect if we are on a desktop system or a laptop
70
* heuristic : laptop if sysctl hw.acpiac0 is present (TODO) or if apm acstate != APM_AC_UNKNOWN
76
struct apm_power_info bstate;
77
struct sensordev acpiac;
79
if (up_native_get_sensordev("acpiac0", &acpiac))
82
if ((apm_fd = open("/dev/apm", O_RDONLY)) == -1) {
83
if (errno != ENXIO && errno != ENOENT)
84
g_error("cannot open device file");
86
if (-1 == ioctl(apm_fd, APM_IOC_GETPOWER, &bstate))
87
g_error("ioctl on fd %d failed : %s", apm_fd, g_strerror(errno));
89
return bstate.ac_state != APM_AC_UNKNOWN;
93
* get a sensordev by its xname (acpibatX/acpiacX)
94
* returns a gboolean if found or not
97
up_native_get_sensordev(const char * id, struct sensordev * snsrdev)
100
size_t sdlen = sizeof(struct sensordev);
101
int mib[] = {CTL_HW, HW_SENSORS, 0, 0 ,0};
103
for (devn = 0 ; ; devn++) {
105
if (sysctl(mib, 3, snsrdev, &sdlen, NULL, 0) == -1) {
111
if (!strcmp(snsrdev->xname, id))