From 0ab80500fd3a7e262417c707022dffbcd8655ba6 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 17 Jul 2010 20:41:37 +0200 Subject: [PATCH] Cleaned up utility functions and implemented `osd-batt --check-every' --- Makefile | 4 ++-- send.c => client.c | 55 +++++++++++++++++++++++++++++++++++++++++----- osd-batt.c | 32 +++++++++++++++++++++++---- util.h => osd.h | 20 +++++++++++++++++ osdc.c | 3 +-- osdd.c | 14 +++--------- send.h | 12 ---------- util.c | 11 +++++++++- 8 files changed, 113 insertions(+), 38 deletions(-) rename send.c => client.c (57%) rename util.h => osd.h (52%) delete mode 100644 send.h diff --git a/Makefile b/Makefile index a4492c5..75d67be 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,8 @@ CFLAGS=-O2 -Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -W all: osdd osdc osd-batt osdd: osdd.o util.o -osdc: osdc.o util.o send.o -osd-batt: osd-batt.o util.o send.o +osdc: osdc.o util.o client.o +osd-batt: osd-batt.o util.o client.o loop.o osdd.o: CFLAGS+=$(shell xosd-config --cflags) osdd: LDFLAGS+=$(shell xosd-config --libs) diff --git a/send.c b/client.c similarity index 57% rename from send.c rename to client.c index aab11a7..8b439f3 100644 --- a/send.c +++ b/client.c @@ -1,5 +1,5 @@ /* - * On-screen Display Client -- Sending Messages + * On-screen Display -- Support Functions for Clients * * (c) 2010 Martin Mares */ @@ -7,11 +7,13 @@ #include #include #include +#include +#include #include #include -#include "util.h" -#include "send.h" +#undef DEBUG +#include "osd.h" static Display *dpy; static Atom pty; @@ -26,6 +28,9 @@ struct osd_msg { void osd_init(void) { + if (dpy) + return; + dpy = XOpenDisplay(NULL); if (!dpy) die("Cannot open display"); @@ -56,12 +61,50 @@ osd_add_line(struct osd_msg *msg, char *key, char *val) void osd_send(struct osd_msg *msg) { - if (!dpy) - osd_init(); - + osd_init(); msg->buf[msg->cnt++] = '\n'; if (!XChangeProperty(dpy, DefaultRootWindow(dpy), pty, XA_STRING, 8, PropModeAppend, (unsigned char *) msg->buf, msg->cnt)) die("XChangeProperty failed"); XFlush(dpy); free(msg); } + +void +osd_fork(void) +{ + osd_init(); + pid_t pid = fork(); + if (pid < 0) + die("Cannot fork: %m"); + if (pid > 0) + exit(0); + setsid(); +} + +void +osd_wait(int delay) +{ + DBG("Waiting for %d seconds\n", delay); + timestamp_t wait_until = get_current_time() + delay*1000; + + struct pollfd pfd = { + .fd = ConnectionNumber(dpy), + .events = POLLIN, + }; + + for (;;) + { + timestamp_t now = get_current_time(); + if (now >= wait_until) + return; + + DBG("... waiting for %d ms\n", (int)(wait_until - now)); + poll(&pfd, 1, wait_until - now); + if (pfd.revents & POLLIN) + { + XEvent ev; + while (XPending(dpy)) + XNextEvent(dpy, &ev); + } + } +} diff --git a/osd-batt.c b/osd-batt.c index df27bce..56bf5b9 100644 --- a/osd-batt.c +++ b/osd-batt.c @@ -11,10 +11,10 @@ #include #include -#include "util.h" -#include "send.h" +#include "osd.h" static int check_mode; +static int check_every; static int warn_threshold = 600; static int total_full, total_capa, discharge_rate; @@ -202,6 +202,12 @@ static void show(void) osd_send(msg); } +static void show_if_warn(void) +{ + if (discharge_mask && discharge_time < warn_threshold) + show(); +} + static void NONRET usage(void) { @@ -210,15 +216,17 @@ Usage: osd-batt \n\ \n\ Options:\n\ -c, --check\t\tDisplay status only if battery is low\n\ +-e, --check-every=\tRun on background and check every seconds\n\ -w, --warn=\tBattery is low if less than seconds remain (default: 600)\n\ "); exit(1); } -static const char short_opts[] = "cw:"; +static const char short_opts[] = "ce:w:"; static const struct option long_opts[] = { { "check", no_argument, NULL, 'c' }, + { "check-every", required_argument, NULL, 'e' }, { "warn", required_argument, NULL, 'w' }, { NULL, 0, NULL, 0 }, }; @@ -232,6 +240,9 @@ int main(int argc, char **argv) case 'c': check_mode++; break; + case 'e': + check_every = atoi(optarg); + break; case 'w': warn_threshold = atoi(optarg); break; @@ -241,8 +252,21 @@ int main(int argc, char **argv) if (optind < argc) usage(); + if (check_every) + { + osd_fork(); + for (;;) + { + scan(); + show_if_warn(); + osd_wait(check_every); + } + } + scan(); - if (!check_mode || (discharge_mask && discharge_time < warn_threshold)) + if (check_mode) + show_if_warn(); + else show(); return 0; diff --git a/util.h b/osd.h similarity index 52% rename from util.h rename to osd.h index 23645aa..78b0a26 100644 --- a/util.h +++ b/osd.h @@ -4,9 +4,15 @@ * (c) 2010 Martin Mares */ +#include + #define NONRET __attribute__((noreturn)) #define FORMAT_CHECK(func,i,j) __attribute__((format(func,i,j))) +typedef uint64_t timestamp_t; + +/* util.c */ + void NONRET FORMAT_CHECK(printf,1,2) die(char *fmt, ...); #ifdef DEBUG @@ -16,3 +22,17 @@ void NONRET FORMAT_CHECK(printf,1,2) die(char *fmt, ...); #endif void *xmalloc(int size); + +timestamp_t get_current_time(void); + +/* client.c */ + +void osd_init(void); + +struct osd_msg; +struct osd_msg *osd_new_msg(void); +void osd_add_line(struct osd_msg *msg, char *key, char *val); +void osd_send(struct osd_msg *msg); + +void osd_fork(void); +void osd_wait(int seconds); diff --git a/osdc.c b/osdc.c index 0f01ae7..f3251d0 100644 --- a/osdc.c +++ b/osdc.c @@ -11,8 +11,7 @@ #include #include -#include "util.h" -#include "send.h" +#include "osd.h" static struct osd_msg *msg; diff --git a/osdd.c b/osdd.c index 15836ea..f5cd44f 100644 --- a/osdd.c +++ b/osdd.c @@ -8,20 +8,17 @@ #include #include #include -#include #include #include -#include #include #include #include #undef DEBUG -#include "util.h" +#include "osd.h" static xosd *osd; -typedef uint64_t timestamp_t; static timestamp_t now; /*** Options ***/ @@ -246,10 +243,7 @@ main(int argc, char **argv) { pid_t pid = fork(); if (pid < 0) - { - fprintf(stderr, "batt: Cannot fork: %m\n"); - return 1; - } + die("Cannot fork: %m"); if (pid > 0) return 0; setsid(); @@ -274,9 +268,7 @@ main(int argc, char **argv) for (;;) { - struct timeval tv; - gettimeofday(&tv, NULL); - now = (timestamp_t) tv.tv_sec * 1000 + tv.tv_usec / 1000; + now = get_current_time(); timestamp_t wait_until = now - 1; if (!current_msg && first_msg) diff --git a/send.h b/send.h deleted file mode 100644 index e01022d..0000000 --- a/send.h +++ /dev/null @@ -1,12 +0,0 @@ -/* - * On-screen Display Client -- Sending Messages - * - * (c) 2010 Martin Mares - */ - -void osd_init(void); - -struct osd_msg; -struct osd_msg *osd_new_msg(void); -void osd_add_line(struct osd_msg *msg, char *key, char *val); -void osd_send(struct osd_msg *msg); diff --git a/util.c b/util.c index fdf8b76..f9cd0c2 100644 --- a/util.c +++ b/util.c @@ -7,8 +7,9 @@ #include #include #include +#include -#include "util.h" +#include "osd.h" void __attribute__((noreturn)) __attribute__((format(printf,1,2))) die(char *fmt, ...) @@ -29,3 +30,11 @@ xmalloc(int size) die("Failed to allocate %d bytes of memory", size); return p; } + +timestamp_t +get_current_time(void) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return (timestamp_t) tv.tv_sec * 1000 + tv.tv_usec / 1000; +} -- 2.39.2