From 000f65252647988aefd91198b86e47dba383bda8 Mon Sep 17 00:00:00 2001 From: jmcneill Date: Thu, 27 Nov 2008 01:45:00 +0000 Subject: Add basic battery and ac_adapter support using envsys on NetBSD. Bump PKGREVISION. --- sysutils/hal/Makefile | 4 +- sysutils/hal/files/hald-netbsd/Makefile.am | 2 +- sysutils/hal/files/hald-netbsd/devinfo_misc.c | 24 +++ sysutils/hal/files/hald-netbsd/envsys.c | 228 ++++++++++++++++++++++++++ sysutils/hal/files/hald-netbsd/envsys.h | 36 ++++ sysutils/hal/files/hald-netbsd/osspec.c | 2 + 6 files changed, 293 insertions(+), 3 deletions(-) create mode 100644 sysutils/hal/files/hald-netbsd/envsys.c create mode 100644 sysutils/hal/files/hald-netbsd/envsys.h (limited to 'sysutils/hal') diff --git a/sysutils/hal/Makefile b/sysutils/hal/Makefile index 95399ac621c..de29a4889e1 100644 --- a/sysutils/hal/Makefile +++ b/sysutils/hal/Makefile @@ -1,8 +1,8 @@ -# $NetBSD: Makefile,v 1.8 2008/11/26 19:45:32 jmcneill Exp $ +# $NetBSD: Makefile,v 1.9 2008/11/27 01:45:00 jmcneill Exp $ # DISTNAME= hal-0.5.11 -PKGREVISION= 4 +PKGREVISION= 5 CATEGORIES= sysutils MASTER_SITES= http://hal.freedesktop.org/releases/ EXTRACT_SUFX= .tar.bz2 diff --git a/sysutils/hal/files/hald-netbsd/Makefile.am b/sysutils/hal/files/hald-netbsd/Makefile.am index fd46e3aca25..3ea4ef1421a 100644 --- a/sysutils/hal/files/hald-netbsd/Makefile.am +++ b/sysutils/hal/files/hald-netbsd/Makefile.am @@ -15,7 +15,7 @@ noinst_LTLIBRARIES = libhald_netbsd.la endif libhald_netbsd_la_SOURCES = \ - osspec.c drvctl.c \ + osspec.c drvctl.c envsys.c \ devinfo.c devinfo_misc.c \ hotplug.c hal-file-monitor.c # devinfo_pci.c devinfo_storage.c devinfo_usb.c diff --git a/sysutils/hal/files/hald-netbsd/devinfo_misc.c b/sysutils/hal/files/hald-netbsd/devinfo_misc.c index e2a0f09d8b6..7cc20e4b1ff 100644 --- a/sysutils/hal/files/hald-netbsd/devinfo_misc.c +++ b/sysutils/hal/files/hald-netbsd/devinfo_misc.c @@ -117,6 +117,28 @@ devinfo_cpu_add(HalDevice *parent, const char *devnode, char *devfs_path, char * return (d); } +static void +devinfo_default_apply_quirks(HalDevice *d, const char *devnode) +{ + +/* acpiacad(4) */ + if (strncmp (devnode, "acpiacad", 8) == 0) { + hal_device_add_capability (d, "ac_adapter"); + +/* acpibat(4) */ + } else if (strncmp (devnode, "acpibat", 7) == 0) { + HalDevice *computer; + + hal_device_add_capability (d, "battery"); + hal_device_property_set_string (d, "battery.type", "primary"); + + computer = hal_device_store_find(hald_get_gdl (), "/org/freedesktop/Hal/devices/computer"); + if (computer) + hal_device_property_set_string (computer, "system.formfactor", "laptop"); + } + +} + static HalDevice * devinfo_default_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type) { @@ -128,6 +150,8 @@ devinfo_default_add(HalDevice *parent, const char *devnode, char *devfs_path, ch devinfo_set_default_properties (d, parent, devnode, devnode); + devinfo_default_apply_quirks (d, devnode); + devinfo_add_enqueue (d, devnode, &devinfo_default_handler); return (d); diff --git a/sysutils/hal/files/hald-netbsd/envsys.c b/sysutils/hal/files/hald-netbsd/envsys.c new file mode 100644 index 00000000000..98d4b848aec --- /dev/null +++ b/sysutils/hal/files/hald-netbsd/envsys.c @@ -0,0 +1,228 @@ +/* $NetBSD: envsys.c,v 1.1 2008/11/27 01:45:00 jmcneill Exp $ */ + +/*- + * Copyright (c) 2008 Jared D. McNeill + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include + +#include "../osspec.h" +#include "../logger.h" +#include "../hald.h" +#include "../hald_dbus.h" +#include "../device_info.h" +#include "../util.h" +#include "devinfo.h" +#include "envsys.h" + +#define _DEV_SYSMON "/dev/sysmon" + +static int envsys_fd = -1; + +static gboolean envsys_timeout(gpointer user_data); +static void envsys_handle_devnode(const char *devnode, prop_array_t properties); + +static void envsys_acadapter_handler(HalDevice *d, prop_array_t properties); +static void envsys_battery_handler(HalDevice *d, prop_array_t properties); + +static struct envsys_devmap { + const char *capability; + void (*handler)(HalDevice *d, prop_array_t properties); +} devmap[] = { + { "ac_adapter", envsys_acadapter_handler }, + { "battery", envsys_battery_handler }, +}; + +void +envsys_init(void) +{ + envsys_fd = open (_DEV_SYSMON, O_RDONLY); + if (envsys_fd == -1) { + HAL_WARNING (("Couldn't open " _DEV_SYSMON ": %s", strerror(errno))); + return; + } + + g_timeout_add (1000, envsys_timeout, NULL); +} + +static gboolean +envsys_timeout(gpointer user_data) +{ + prop_dictionary_t global_dict; + prop_array_t global_array; + prop_object_iterator_t iter; + prop_dictionary_keysym_t keysym; + int status; + + status = prop_dictionary_recv_ioctl (envsys_fd, ENVSYS_GETDICTIONARY, &global_dict); + if (status) { + HAL_WARNING (("envsys_timeout: ENVSYS_GETDICTIONARY failed: %s", strerror(status))); + return FALSE; + } + + global_array = prop_dictionary_all_keys (global_dict); + if (global_array == NULL) { + HAL_WARNING (("envsys_timeout: prop_dictionary_all_keys returned NULL")); + return FALSE; + } + iter = prop_array_iterator (global_array); + while ((keysym = (prop_dictionary_keysym_t)prop_object_iterator_next (iter)) != NULL) { + const char *devnode; + prop_object_t obj; + + devnode = prop_dictionary_keysym_cstring_nocopy (keysym); + obj = prop_dictionary_get_keysym (global_dict, keysym); + if (obj == NULL || prop_object_type (obj) != PROP_TYPE_ARRAY) + continue; + + envsys_handle_devnode (devnode, (prop_array_t)obj); + } + prop_object_iterator_release (iter); + + prop_object_release (global_dict); + return TRUE; +} + +static void +envsys_handle_devnode(const char *devnode, prop_array_t properties) +{ + HalDevice *d; + unsigned int i; + + d = hal_device_store_match_key_value_string (hald_get_gdl (), "netbsd.device", devnode); + if (d == NULL) + return; + + for (i = 0; i < __arraycount (devmap); i++) { + if (!hal_device_has_capability (d, devmap[i].capability)) + continue; + devmap[i].handler (d, properties); + } + + return; +} + +static void +envsys_acadapter_handler(HalDevice *d, prop_array_t properties) +{ + prop_object_iterator_t iter; + prop_dictionary_t prop; + + iter = prop_array_iterator (properties); + while ((prop = (prop_dictionary_t)prop_object_iterator_next (iter)) != NULL) { + const char *descr; + uint8_t connected; + + if (prop_dictionary_get_cstring_nocopy (prop, "description", &descr) == false) + continue; + + if (strcmp (descr, "connected") != 0) + continue; + + if (prop_dictionary_get_uint8 (prop, "cur-value", &connected) == false) + continue; + + hal_device_property_set_bool (d, "ac_adapter.present", connected); + } + + prop_object_iterator_release (iter); +} + +static void +envsys_battery_handler(HalDevice *d, prop_array_t properties) +{ + prop_object_iterator_t iter; + prop_dictionary_t prop; + + device_property_atomic_update_begin (); + + hal_device_property_set_bool (d, "battery.is_rechargeable", TRUE); + + iter = prop_array_iterator (properties); + while ((prop = (prop_dictionary_t)prop_object_iterator_next (iter)) != NULL) { + const char *descr; + const char *valid; + int64_t intval; + + if (prop_dictionary_get_cstring_nocopy (prop, "description", &descr) == false) + continue; + + if (prop_dictionary_get_int64 (prop, "cur-value", &intval) == false) + continue; + + if (strcmp (descr, "present") == 0) + hal_device_property_set_bool (d, "battery.present", intval); + else if (strcmp (descr, "design cap") == 0) + hal_device_property_set_int (d, "battery.charge_level.design", intval); + else if (strcmp (descr, "last full cap") == 0) + hal_device_property_set_int (d, "battery.charge_level.last_full", intval); + else if (strcmp (descr, "charge") == 0) { + int64_t maxval; + + hal_device_property_set_int (d, "battery.charge_level.current", intval); + + if (prop_dictionary_get_int64 (prop, "max-value", &maxval) && maxval > 0) + hal_device_property_set_int (d, "battery.charge_level.percentage", intval * 100 / maxval); + else + hal_device_property_set_int (d, "battery.charge_level.percentage", 0); + } + else if (strcmp (descr, "charge rate") == 0) { + if (prop_dictionary_get_cstring_nocopy (prop, "state", &valid) == false) + continue; + if (strcmp (valid, "valid") == 0) { + hal_device_property_set_bool (d, "battery.rechargeable.is_charging", TRUE); + hal_device_property_set_bool (d, "battery.rechargeable.is_discharging", FALSE); + hal_device_property_set_int (d, "battery.charge_level.rate", intval / 3600); + } + } else if (strcmp (descr, "discharge rate") == 0) { + if (prop_dictionary_get_cstring_nocopy (prop, "state", &valid) == false) + continue; + if (strcmp (valid, "valid") == 0) { + hal_device_property_set_bool (d, "battery.rechargeable.is_charging", FALSE); + hal_device_property_set_bool (d, "battery.rechargeable.is_discharging", TRUE); + hal_device_property_set_int (d, "battery.charge_level.rate", intval / 3600); + } + } + } + + device_property_atomic_update_end (); + + prop_object_iterator_release (iter); +} diff --git a/sysutils/hal/files/hald-netbsd/envsys.h b/sysutils/hal/files/hald-netbsd/envsys.h new file mode 100644 index 00000000000..fa228f3caf1 --- /dev/null +++ b/sysutils/hal/files/hald-netbsd/envsys.h @@ -0,0 +1,36 @@ +/* $NetBSD: envsys.h,v 1.1 2008/11/27 01:45:00 jmcneill Exp $ */ + +/*- + * Copyright (c) 2008 Jared D. McNeill + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef DEVINFO_ENVSYS_H +#define DEVINFO_ENVSYS_H + +#include "devinfo.h" + +void envsys_init(void); + +#endif /* DEVINFO_ENVSYS_H */ diff --git a/sysutils/hal/files/hald-netbsd/osspec.c b/sysutils/hal/files/hald-netbsd/osspec.c index 3c88dbdad37..da891e96f72 100644 --- a/sysutils/hal/files/hald-netbsd/osspec.c +++ b/sysutils/hal/files/hald-netbsd/osspec.c @@ -32,6 +32,7 @@ #include "osspec_netbsd.h" #include "hotplug.h" #include "drvctl.h" +#include "envsys.h" #include "devinfo.h" #include "devinfo_storage.h" @@ -50,6 +51,7 @@ osspec_init (void) { ids_init (); mntinfo_event_init (); + envsys_init (); } void -- cgit v1.2.3