From b8e600b9a47443c95120a9869c77c8f694604a53 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Sun, 29 Nov 2009 14:14:06 +0100 Subject: [PATCH] convert bsd apm objects to callbacks.print --- src/Makefile.am | 4 +- src/bsdapm.c | 209 ++++++++++++++++++++++++++++++++++++++++++++++++ src/bsdapm.h | 37 +++++++++ src/conky.c | 33 -------- src/core.c | 4 + src/freebsd.c | 131 ------------------------------ src/openbsd.c | 126 ----------------------------- 7 files changed, 252 insertions(+), 292 deletions(-) create mode 100644 src/bsdapm.c create mode 100644 src/bsdapm.h diff --git a/src/Makefile.am b/src/Makefile.am index a984f64a..a0ea0ad6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -67,9 +67,9 @@ moc = moc.c moc.h xmms2 = xmms2.c xmms2.h linux = linux.c linux.h top.c top.h users.c users.h sony.c sony.h i8k.c i8k.h solaris = solaris.c -freebsd = freebsd.c freebsd.h +freebsd = freebsd.c freebsd.h bsdapm.c bsdapm.h netbsd = netbsd.c netbsd.h -openbsd = openbsd.c openbsd.h +openbsd = openbsd.c openbsd.h bsdapm.c bsdapm.h port_monitors = libtcp-portmon.c libtcp-portmon.h \ tcp-portmon.c tcp-portmon.h x11 = x11.c x11.h fonts.c fonts.h diff --git a/src/bsdapm.c b/src/bsdapm.c new file mode 100644 index 00000000..e1469ac3 --- /dev/null +++ b/src/bsdapm.c @@ -0,0 +1,209 @@ +/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*- + * vim: ts=4 sw=4 noet ai cindent syntax=c + * + * Conky, a system monitor, based on torsmo + * + * Any original torsmo code is licensed under the BSD license + * + * All code written since the fork of torsmo is licensed under the GPL + * + * Please see COPYING for details + * + * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen + * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al. + * (see AUTHORS) + * All rights reserved. + * + * 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 3 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, see . + * + */ +#include "config.h" +#include "conky.h" +#include "text_object.h" +#include +#include +#include + +#define APMDEV "/dev/apm" +#define APM_UNKNOWN 255 + +#ifndef APM_AC_OFF +# define APM_AC_OFF 0 +#endif + +#ifndef APM_AC_ON +# define APM_AC_ON 1 +#endif + +#ifndef APM_BATT_CHARGING +# define APM_BATT_CHARGING 3 +#endif + +static int apm_getinfo(int fd, apm_info_t aip) +{ +#ifdef __OpenBSD__ + if (ioctl(fd, APM_IOC_GETPOWER, aip) == -1) { +#else + if (ioctl(fd, APMIO_GETINFO, aip) == -1) { +#endif + return -1; + } + + return 0; +} + +void print_apm_adapter(struct text_object *obj, char *p, int p_max_size) +{ + int fd; + const char *out; +#ifdef __OpenBSD__ + struct apm_power_info a_info; +#else + struct apm_info a_info; +#endif + + (void)obj; + + fd = open(APMDEV, O_RDONLY); + if (fd < 0) { + snprintf(p, p_max_size, "ERR"); + return; + } + + if (apm_getinfo(fd, &a_info) != 0) { + close(fd); + snprintf(p, p_max_size, "ERR"); + return; + } + close(fd); + +#ifdef __OpenBSD__ +# define ai_acline ac_state +#endif + switch (a_info.ai_acline) { + case APM_AC_OFF: + out = "off-line"; + break; + case APM_AC_ON: +#ifdef __OpenBSD__ +# define ai_batt_stat battery_state +#endif + if (a_info.ai_batt_stat + == APM_BATT_CHARGING) { + out = "charging"; + } else { + out = "on-line"; + } + break; + default: + out = "unknown"; + break; + } + snprintf(p, p_max_size, "%s", out); +} + +void print_apm_battery_life(struct text_object *obj, char *p, int p_max_size) +{ + int fd; + u_int batt_life; + const char *out; +#ifdef __OpenBSD__ + struct apm_power_info a_info; +#else + struct apm_info a_info; +#endif + + (void)obj; + + fd = open(APMDEV, O_RDONLY); + if (fd < 0) { + snprintf(p, p_max_size, "ERR"); + return; + } + + if (apm_getinfo(fd, &a_info) != 0) { + close(fd); + snprintf(p, p_max_size, "ERR"); + return; + } + close(fd); + +#ifdef __OpenBSD__ +# define ai_batt_life battery_life +#endif + batt_life = a_info.ai_batt_life; + if (batt_life == APM_UNKNOWN) { + out = "unknown"; + } else if (batt_life <= 100) { + snprintf(p, p_max_size, "%d%%", batt_life); + return; + } else { + out = "ERR"; + } + + snprintf(p, p_max_size, "%s", out); +} + +void print_apm_battery_time(struct text_object *obj, char *p, int p_max_size) +{ + int fd; + int batt_time; +#ifdef __OpenBSD__ + int h, m; + struct apm_power_info a_info; +#else + int h, m, s; + struct apm_info a_info; +#endif + + (void)obj; + + fd = open(APMDEV, O_RDONLY); + if (fd < 0) { + snprintf(p, p_max_size, "ERR"); + return; + } + + if (apm_getinfo(fd, &a_info) != 0) { + close(fd); + snprintf(p, p_max_size, "ERR"); + return; + } + close(fd); + +#ifdef __OpenBSD__ +# define ai_batt_time minutes_left +#endif + batt_time = a_info.ai_batt_time; + + if (batt_time == -1) { + snprintf(p, p_max_size, "unknown"); + } else + +#ifdef __OpenBSD__ + { + h = batt_time / 60; + m = batt_time % 60; + snprintf(p, p_max_size, "%2d:%02d", h, m); + } +#else + { + h = batt_time; + s = h % 60; + h /= 60; + m = h % 60; + h /= 60; + snprintf(p, p_max_size, "%2d:%02d:%02d", h, m, s); + } +#endif +} diff --git a/src/bsdapm.h b/src/bsdapm.h new file mode 100644 index 00000000..afbb7cbf --- /dev/null +++ b/src/bsdapm.h @@ -0,0 +1,37 @@ +/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*- + * vim: ts=4 sw=4 noet ai cindent syntax=c + * + * Conky, a system monitor, based on torsmo + * + * Any original torsmo code is licensed under the BSD license + * + * All code written since the fork of torsmo is licensed under the GPL + * + * Please see COPYING for details + * + * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen + * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al. + * (see AUTHORS) + * All rights reserved. + * + * 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 3 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, see . + * + */ +#ifndef _BSDAPM_H +#define _BSDAPM_H + +void print_apm_adapter(struct text_object *, char *, int); +void print_apm_battery_life(struct text_object *, char *, int); +void print_apm_battery_time(struct text_object *, char *, int); + +#endif /* _BSDAPM_H */ diff --git a/src/conky.c b/src/conky.c index a2ab7cae..6b800785 100644 --- a/src/conky.c +++ b/src/conky.c @@ -118,15 +118,6 @@ #include #endif -/* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */ -#if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \ - || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__)) -int apm_getinfo(int fd, apm_info_t aip); -char *get_apm_adapter(void); -char *get_apm_battery_life(void); -char *get_apm_battery_time(void); -#endif - #ifdef CONFIG_OUTPUT #include "defconfig.h" #include "conf_cookie.h" @@ -1109,30 +1100,6 @@ void generate_text_internal(char *p, int p_max_size, DO_JUMP; } } -#if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \ - || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__)) - OBJ(apm_adapter) { - char *msg; - - msg = get_apm_adapter(); - snprintf(p, p_max_size, "%s", msg); - free(msg); - } - OBJ(apm_battery_life) { - char *msg; - - msg = get_apm_battery_life(); - snprintf(p, p_max_size, "%s", msg); - free(msg); - } - OBJ(apm_battery_time) { - char *msg; - - msg = get_apm_battery_time(); - snprintf(p, p_max_size, "%s", msg); - free(msg); - } -#endif /* __FreeBSD__ __OpenBSD__ */ #ifdef HAVE_ICONV OBJ(iconv_start) { diff --git a/src/core.c b/src/core.c index 90dad974..fb472a36 100644 --- a/src/core.c +++ b/src/core.c @@ -33,6 +33,7 @@ #include "text_object.h" #include "algebra.h" #include "build.h" +#include "bsdapm.h" #include "colours.h" #include "combine.h" #include "diskio.h" @@ -1100,8 +1101,11 @@ struct text_object *construct_text_object(const char *s, const char *arg, long #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \ || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__)) END OBJ(apm_adapter, 0) + obj->callbacks.print = &print_apm_adapter; END OBJ(apm_battery_life, 0) + obj->callbacks.print = &print_apm_battery_life; END OBJ(apm_battery_time, 0) + obj->callbacks.print = &print_apm_battery_time; #endif /* __FreeBSD__ */ END OBJ(imap_unseen, 0) parse_imap_mail_args(obj, arg); diff --git a/src/freebsd.c b/src/freebsd.c index 02cfdad3..1b443e3e 100644 --- a/src/freebsd.c +++ b/src/freebsd.c @@ -759,137 +759,6 @@ proc_find_top(struct process **cpu, struct process **mem) free(processes); } -#if defined(i386) || defined(__i386__) -#define APMDEV "/dev/apm" -#define APM_UNKNOWN 255 - -int apm_getinfo(int fd, apm_info_t aip) -{ - if (ioctl(fd, APMIO_GETINFO, aip) == -1) { - return -1; - } - - return 0; -} - -char *get_apm_adapter(void) -{ - int fd; - struct apm_info a_info; - char *out; - - out = (char *) calloc(16, sizeof(char)); - - fd = open(APMDEV, O_RDONLY); - if (fd < 0) { - strncpy(out, "ERR", 16); - return out; - } - - if (apm_getinfo(fd, &a_info) != 0) { - close(fd); - strncpy(out, "ERR", 16); - return out; - } - close(fd); - - switch (a_info.ai_acline) { - case 0: - strncpy(out, "off-line", 16); - return out; - break; - case 1: - if (a_info.ai_batt_stat == 3) { - strncpy(out, "charging", 16); - return out; - } else { - strncpy(out, "on-line", 16); - return out; - } - break; - default: - strncpy(out, "unknown", 16); - return out; - break; - } -} - -char *get_apm_battery_life(void) -{ - int fd; - u_int batt_life; - struct apm_info a_info; - char *out; - - out = (char *) calloc(16, sizeof(char)); - - fd = open(APMDEV, O_RDONLY); - if (fd < 0) { - strncpy(out, "ERR", 16); - return out; - } - - if (apm_getinfo(fd, &a_info) != 0) { - close(fd); - strncpy(out, "ERR", 16); - return out; - } - close(fd); - - batt_life = a_info.ai_batt_life; - if (batt_life == APM_UNKNOWN) { - strncpy(out, "unknown", 16); - } else if (batt_life <= 100) { - snprintf(out, 16, "%d%%", batt_life); - return out; - } else { - strncpy(out, "ERR", 16); - } - - return out; -} - -char *get_apm_battery_time(void) -{ - int fd; - int batt_time; - int h, m, s; - struct apm_info a_info; - char *out; - - out = (char *) calloc(16, sizeof(char)); - - fd = open(APMDEV, O_RDONLY); - if (fd < 0) { - strncpy(out, "ERR", 16); - return out; - } - - if (apm_getinfo(fd, &a_info) != 0) { - close(fd); - strncpy(out, "ERR", 16); - return out; - } - close(fd); - - batt_time = a_info.ai_batt_time; - - if (batt_time == -1) { - strncpy(out, "unknown", 16); - } else { - h = batt_time; - s = h % 60; - h /= 60; - m = h % 60; - h /= 60; - snprintf(out, 16, "%2d:%02d:%02d", h, m, s); - } - - return out; -} - -#endif - void get_battery_short_status(char *buffer, unsigned int n, const char *bat) { get_battery_stuff(buffer, n, bat, BATTERY_STATUS); diff --git a/src/openbsd.c b/src/openbsd.c index 56de041b..5197056f 100644 --- a/src/openbsd.c +++ b/src/openbsd.c @@ -789,132 +789,6 @@ inline void proc_find_top(struct process **cpu, struct process **mem) free(processes); } -#if defined(i386) || defined(__i386__) -#define APMDEV "/dev/apm" -#define APM_UNKNOWN 255 - -int apm_getinfo(int fd, apm_info_t aip) -{ - if (ioctl(fd, APM_IOC_GETPOWER, aip) == -1) { - return -1; - } - - return 0; -} - -char *get_apm_adapter() -{ - int fd; - struct apm_power_info info; - char *out; - - out = (char *) calloc(16, sizeof(char)); - - fd = open(APMDEV, O_RDONLY); - if (fd < 0) { - strncpy(out, "ERR", 16); - return out; - } - - if (apm_getinfo(fd, &info) != 0) { - close(fd); - strncpy(out, "ERR", 16); - return out; - } - close(fd); - - switch (info.ac_state) { - case APM_AC_OFF: - strncpy(out, "off-line", 16); - return out; - break; - case APM_AC_ON: - if (info.battery_state == APM_BATT_CHARGING) { - strncpy(out, "charging", 16); - return out; - } else { - strncpy(out, "on-line", 16); - return out; - } - break; - default: - strncpy(out, "unknown", 16); - return out; - break; - } -} - -char *get_apm_battery_life() -{ - int fd; - u_int batt_life; - struct apm_power_info info; - char *out; - - out = (char *) calloc(16, sizeof(char)); - - fd = open(APMDEV, O_RDONLY); - if (fd < 0) { - strncpy(out, "ERR", 16); - return out; - } - - if (apm_getinfo(fd, &info) != 0) { - close(fd); - strncpy(out, "ERR", 16); - return out; - } - close(fd); - - batt_life = info.battery_life; - if (batt_life <= 100) { - snprintf(out, 16, "%d%%", batt_life); - return out; - } else { - strncpy(out, "ERR", 16); - } - - return out; -} - -char *get_apm_battery_time() -{ - int fd; - int batt_time; - int h, m; - struct apm_power_info info; - char *out; - - out = (char *) calloc(16, sizeof(char)); - - fd = open(APMDEV, O_RDONLY); - if (fd < 0) { - strncpy(out, "ERR", 16); - return out; - } - - if (apm_getinfo(fd, &info) != 0) { - close(fd); - strncpy(out, "ERR", 16); - return out; - } - close(fd); - - batt_time = info.minutes_left; - - if (batt_time == -1) { - strncpy(out, "unknown", 16); - } else { - h = batt_time / 60; - m = batt_time % 60; - snprintf(out, 16, "%2d:%02d", h, m); - } - - return out; -} - -#endif - /* empty stubs so conky links */ void prepare_update() {