~peter-pearse/ubuntu/oneiric/upower/prop001

« back to all changes in this revision

Viewing changes to src/openbsd/up-native.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-05 14:31:17 UTC
  • mfrom: (15.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20110505143117-9clyzsrw45vt5g6y
Tags: 0.9.10-1
* New upstream release:
  - Fix "unknown" battery status guessing to not be recursive. (LP: #384304)
* debian/control: Drop obsolete devicekit-power-* Conflicts/Replaces.

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) 2011 Landry Breuil <landry@openbsd.org>
 
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 "up-apm-native.h"
 
23
#include "up-native.h"
 
24
 
 
25
/* XXX why does this macro needs to be in the .c ? */
 
26
G_DEFINE_TYPE (UpApmNative, up_apm_native, G_TYPE_OBJECT)
 
27
 
 
28
static void
 
29
up_apm_native_class_init (UpApmNativeClass *klass)
 
30
{
 
31
}
 
32
 
 
33
static void
 
34
up_apm_native_init (UpApmNative *self)
 
35
{
 
36
        self->path = "empty";
 
37
}
 
38
 
 
39
UpApmNative *
 
40
up_apm_native_new(const gchar * path)
 
41
{
 
42
        UpApmNative *native;
 
43
        native = UP_APM_NATIVE (g_object_new (UP_TYPE_APM_NATIVE, NULL));
 
44
        native->path = g_strdup(path);
 
45
        return native;
 
46
}
 
47
 
 
48
const gchar *
 
49
up_apm_native_get_path(UpApmNative * native)
 
50
{
 
51
        return native->path;
 
52
}
 
53
 
 
54
/**
 
55
 * up_native_get_native_path:
 
56
 * @object: the native tracking object
 
57
 *
 
58
 * This converts a GObject used as the device data into a native path.
 
59
 *
 
60
 * Return value: The native path for the device which is unique, e.g. "/sys/class/power/BAT1"
 
61
 **/
 
62
const gchar *
 
63
up_native_get_native_path (GObject *object)
 
64
{
 
65
        return up_apm_native_get_path (UP_APM_NATIVE (object));
 
66
}
 
67
 
 
68
/**
 
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
 
71
 */
 
72
gboolean
 
73
up_native_is_laptop()
 
74
{
 
75
        int apm_fd;
 
76
        struct apm_power_info bstate;
 
77
        struct sensordev acpiac;
 
78
 
 
79
        if (up_native_get_sensordev("acpiac0", &acpiac))
 
80
                return TRUE;
 
81
 
 
82
        if ((apm_fd = open("/dev/apm", O_RDONLY)) == -1) {
 
83
                if (errno != ENXIO && errno != ENOENT)
 
84
                        g_error("cannot open device file");
 
85
        }
 
86
        if (-1 == ioctl(apm_fd, APM_IOC_GETPOWER, &bstate))
 
87
                g_error("ioctl on fd %d failed : %s", apm_fd, g_strerror(errno));
 
88
        close(apm_fd);
 
89
        return bstate.ac_state != APM_AC_UNKNOWN;
 
90
}
 
91
 
 
92
/**
 
93
 * get a sensordev by its xname (acpibatX/acpiacX)
 
94
 * returns a gboolean if found or not
 
95
 */
 
96
gboolean
 
97
up_native_get_sensordev(const char * id, struct sensordev * snsrdev)
 
98
{
 
99
        int devn;
 
100
        size_t sdlen = sizeof(struct sensordev);
 
101
        int mib[] = {CTL_HW, HW_SENSORS, 0, 0 ,0};
 
102
 
 
103
        for (devn = 0 ; ; devn++) {
 
104
                mib[2] = devn;
 
105
                if (sysctl(mib, 3, snsrdev, &sdlen, NULL, 0) == -1) {
 
106
                        if (errno == ENXIO)
 
107
                                continue;
 
108
                        if (errno == ENOENT)
 
109
                                break;
 
110
                }
 
111
                if (!strcmp(snsrdev->xname, id))
 
112
                        return TRUE;
 
113
        }
 
114
        return FALSE;
 
115
}