diff --git a/src/applets/battery.c b/src/applets/battery.c index 4dae323..cdbb3d7 100644 --- a/src/applets/battery.c +++ b/src/applets/battery.c @@ -20,13 +20,17 @@ #include #include #include -#ifdef __NetBSD__ +#if defined(__NetBSD__) # include # include # include # include # include # include +#elif defined(__linux__) +# include +# include +# include #endif #include #include "Panel.h" @@ -42,7 +46,7 @@ typedef struct _Battery GtkWidget * image; GtkWidget * scale; guint timeout; -#ifdef __NetBSD__ +#if defined(__NetBSD__) || defined(__linux__) int fd; #endif } Battery; @@ -86,7 +90,7 @@ static GtkWidget * _battery_init(PanelApplet * applet) applet->priv = battery; battery->helper = applet->helper; battery->timeout = 0; -#ifdef __NetBSD__ +#if defined(__NetBSD__) || defined(__linux__) battery->fd = -1; #endif hbox = gtk_hbox_new(FALSE, 0); @@ -114,7 +118,7 @@ static void _battery_destroy(PanelApplet * applet) if(battery->timeout > 0) g_source_remove(battery->timeout); -#ifdef __NetBSD__ +#if defined(__NetBSD__) || defined(__linux__) if(battery->fd != -1) close(battery->fd); #endif @@ -154,7 +158,7 @@ static void _battery_set(Battery * battery, gdouble value) /* callbacks */ /* on_timeout */ -#ifdef __NetBSD__ +#if defined(__NetBSD__) static int _get_tre(int fd, int sensor, envsys_tre_data_t * tre); static gdouble _battery_get(Battery * battery) @@ -233,6 +237,42 @@ static int _get_tre(int fd, int sensor, envsys_tre_data_t * tre) return 1; return !(tre->validflags & ENVSYS_FVALID); } +#elif defined(__linux__) +static gdouble _battery_get(Battery * battery) +{ + const char apm[] = "/proc/apm"; + char buf[80]; + ssize_t buf_cnt; + double d; + unsigned int u; + int i; + int b; + + if(battery->fd == -1 && (battery->fd = open(apm, O_RDONLY)) == -1) + { + error_set("%s: %s", apm, strerror(errno)); + return 0.0 / 0.0; + } + errno = ENODATA; + if(lseek(battery->fd, 0, SEEK_SET) != 0 + || (buf_cnt = read(battery->fd, buf, sizeof(buf))) <= 0) + { + error_set("%s: %s", apm, strerror(errno)); + close(battery->fd); + battery->fd = -1; + return 0.0 / 0.0; + } + buf[--buf_cnt] = '\0'; + if(sscanf(buf, "%lf %lf %x %x %x %x %d%% %d min", &d, &d, &u, &u, &u, + &u, &b, &i) != 8) + { + error_set("%s: %s", apm, strerror(errno)); + close(battery->fd); + battery->fd = -1; + } + d = b; + return d; +} #else static gdouble _battery_get(Battery * battery) {