From 02bce1c90ff5bd5fbac6b69830866239eb009510 Mon Sep 17 00:00:00 2001 From: Doug Torrance Date: Sun, 5 Oct 2014 10:30:05 -0500 Subject: [PATCH] wmbattery: Restore BSD support. Code that was ripped out of wmbattery for version 1.22, which introduced the dependency on libapm but dropped support for the BSDs, has been restored. --- wmbattery/Makefile | 2 -- wmbattery/apm.h | 29 ++++++++++++++++++++ wmbattery/configure.ac | 7 +++++ wmbattery/sonypi.c | 5 ++-- wmbattery/sonypi.h | 12 ++++----- wmbattery/wmbattery.c | 60 ++++++++++++++++++++++++++++++++++++++++-- 6 files changed, 102 insertions(+), 13 deletions(-) diff --git a/wmbattery/Makefile b/wmbattery/Makefile index b5262d5..e9fcce8 100644 --- a/wmbattery/Makefile +++ b/wmbattery/Makefile @@ -35,8 +35,6 @@ upower.o: upower.c $(CC) $(CPPFLAGS) $(CFLAGS) $(shell pkg-config --cflags upower-glib) -c upower.c -o upower.o endif -LIBS+=-lapm -lXext -lXpm - wmbattery: $(OBJS) $(CC) -o wmbattery $(LDFLAGS) $(OBJS) $(LIBS) diff --git a/wmbattery/apm.h b/wmbattery/apm.h index 4fd8671..381b7f7 100644 --- a/wmbattery/apm.h +++ b/wmbattery/apm.h @@ -1,4 +1,16 @@ +#include "config.h" + +#ifdef HAVE_MACHINE_APM_BIOS_H /* for FreeBSD */ +#include +#endif + +#ifdef HAVE_I386_APMVAR_H /* for NetBSD and OpenBSD */ +#include +#endif + +#ifdef HAVE_APM_H #include +#endif /* Symbolic constants for apm may be in system apm.h, or may not. */ #ifndef AC_LINE_STATUS_ON @@ -25,3 +37,20 @@ #define BATTERY_TIME_UNKNOWN (-1) #endif /* AC_LINE_STATUS_ON */ +#if defined (HAVE_MACHINE_APM_BIOS_H) || defined (HAVE_I386_APMVAR_H) /* BSD */ +typedef struct { + const char driver_version[10]; + int apm_version_major; + int apm_version_minor; + int apm_flags; + int ac_line_status; + int battery_status; + int battery_flags; + int battery_percentage; + int battery_time; + int using_minutes; +} apm_info; + +int apm_read(apm_info *i); +int apm_exists(void); +#endif diff --git a/wmbattery/configure.ac b/wmbattery/configure.ac index dc15b2c..0688913 100644 --- a/wmbattery/configure.ac +++ b/wmbattery/configure.ac @@ -4,6 +4,9 @@ AC_CONFIG_HEADER(config.h) AC_CONFIG_AUX_DIR(autoconf) +dnl Checks for the apm device other than /proc/apm. +AC_CHECK_FILES(/dev/apm) + dnl Checks for programs. AC_PROG_CC AC_PROG_INSTALL @@ -32,6 +35,10 @@ AC_CHECK_HEADERS(X11/xpm.h) AC_CHECK_HEADERS(X11/extensions/shape.h) AC_CHECK_HEADERS(getopt.h) AC_CHECK_HEADERS(apm.h) +dnl FreeBSD needs apm_bios.h +AC_CHECK_HEADERS(machine/apm_bios.h) +dnl NetBSD and OpenBSD need apmvar.h +AC_CHECK_HEADERS(i386/apmvar.h) dnl Checks for typedefs, structures, and compiler characteristics. AC_C_CONST diff --git a/wmbattery/sonypi.c b/wmbattery/sonypi.c index c3190be..92ba106 100644 --- a/wmbattery/sonypi.c +++ b/wmbattery/sonypi.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "sonypi.h" @@ -30,8 +31,8 @@ inline int sonypi_ioctl(int ioctlno, void *param) * struct. */ int sonypi_read(apm_info *info) { - __u8 batflags; - __u16 cap, rem; + uint8_t batflags; + uint16_t cap, rem; int havebatt = 0; info->using_minutes = info->battery_flags = 0; diff --git a/wmbattery/sonypi.h b/wmbattery/sonypi.h index 9e132e6..9abd2f4 100644 --- a/wmbattery/sonypi.h +++ b/wmbattery/sonypi.h @@ -4,16 +4,14 @@ int sonypi_read(apm_info *info); /* There's no good place to get these constants, so I must define them * myself. */ -#include - /* get battery full capacity/remaining capacity */ -#define SONYPI_IOCGBAT1CAP _IOR('v', 2, __u16) -#define SONYPI_IOCGBAT1REM _IOR('v', 3, __u16) -#define SONYPI_IOCGBAT2CAP _IOR('v', 4, __u16) -#define SONYPI_IOCGBAT2REM _IOR('v', 5, __u16) +#define SONYPI_IOCGBAT1CAP _IOR('v', 2, uint16_t) +#define SONYPI_IOCGBAT1REM _IOR('v', 3, uint16_t) +#define SONYPI_IOCGBAT2CAP _IOR('v', 4, uint16_t) +#define SONYPI_IOCGBAT2REM _IOR('v', 5, uint16_t) /* get battery flags: battery1/battery2/ac adapter present */ #define SONYPI_BFLAGS_B1 0x01 #define SONYPI_BFLAGS_B2 0x02 #define SONYPI_BFLAGS_AC 0x04 -#define SONYPI_IOCGBATFLAGS _IOR('v', 7, __u8) +#define SONYPI_IOCGBATFLAGS _IOR('v', 7, uint8_t) diff --git a/wmbattery/wmbattery.c b/wmbattery/wmbattery.c index 26fd283..8f9cbb5 100644 --- a/wmbattery/wmbattery.c +++ b/wmbattery/wmbattery.c @@ -20,7 +20,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -#include "config.h" #include #include #include @@ -34,12 +33,12 @@ #include #include #include +#include "wmbattery.h" #ifdef HAVE_GETOPT_H #include #endif -#include "wmbattery.h" #include "mask.xbm" #include "sonypi.h" #include "acpi.h" @@ -58,6 +57,14 @@ Display *display; GC NormalGC; int pos[2] = {0, 0}; +#ifdef HAVE__DEV_APM +#define APM_STATUS_FILE "/dev/apm" +#else +#define APM_STATUS_FILE "/proc/apm" +#endif + +char *apm_status_file = APM_STATUS_FILE; + char *crit_audio_fn = NULL; char *crit_audio; int crit_audio_size; @@ -93,6 +100,55 @@ void error(const char *fmt, ...) exit(1); } +#if defined (HAVE_MACHINE_APM_BIOS_H) || defined (HAVE_I386_APMVAR_H) /* BSD */ +int apm_read(apm_info *i) +{ + int fd; +#ifdef HAVE_MACHINE_APM_BIOS_H /* FreeBSD */ + unsigned long request = APMIO_GETINFO; + struct apm_info info; +#else /* NetBSD or OpenBSD */ + unsigned long request= APM_IOC_GETPOWER; + struct apm_power_info info; +#endif + + if ((fd = open(apm_status_file, O_RDONLY)) == -1) { + return 0; + } + if (ioctl(fd, request, &info) == -1) { + return 0; + } + close(fd); + +#ifdef HAVE_MACHINE_APM_BIOS_H /* FreeBSD */ + i->ac_line_status = info.ai_acline; + i->battery_status = info.ai_batt_stat; + i->battery_flags = (info.ai_batt_stat == 3) ? 8: 0; + i->battery_percentage = info.ai_batt_life; + i->battery_time = info.ai_batt_time; + i->using_minutes = 0; +#else /* NetBSD or OpenBSD */ + i->ac_line_status = info.ac_state; + i->battery_status = info.battery_state; + i->battery_flags = (info.battery_state == 3) ? 8: 0; + i->battery_percentage = info.battery_life; + i->battery_time = info.minutes_left; + i->using_minutes = 1; +#endif + + return 1; +} + +int apm_exists(void) +{ + apm_info i; + + if (access(apm_status_file, R_OK)) + return 0; + return apm_read(&i); +} +#endif + int apm_change(apm_info *cur) { static int ac_line_status = 0, battery_status = 0, battery_flags = 0,