]> mj.ucw.cz Git - osdd.git/commitdiff
Cleaned up utility functions and implemented `osd-batt --check-every'
authorMartin Mares <mj@ucw.cz>
Sat, 17 Jul 2010 18:41:37 +0000 (20:41 +0200)
committerMartin Mares <mj@ucw.cz>
Sat, 17 Jul 2010 18:41:37 +0000 (20:41 +0200)
Makefile
client.c [new file with mode: 0644]
osd-batt.c
osd.h [new file with mode: 0644]
osdc.c
osdd.c
send.c [deleted file]
send.h [deleted file]
util.c
util.h [deleted file]

index a4492c5589b030f0a71901f8b0ed10b9e5194b8f..75d67be126ccbf15262ff4cbc4faea2d6ef6f557 100644 (file)
--- 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/client.c b/client.c
new file mode 100644 (file)
index 0000000..8b439f3
--- /dev/null
+++ b/client.c
@@ -0,0 +1,110 @@
+/*
+ *     On-screen Display -- Support Functions for Clients
+ *
+ *     (c) 2010 Martin Mares <mj@ucw.cz>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <poll.h>
+#include <X11/Xlib.h>
+#include <X11/Xatom.h>
+
+#undef DEBUG
+#include "osd.h"
+
+static Display *dpy;
+static Atom pty;
+
+#define MAX_MSG_SIZE 1024
+
+struct osd_msg {
+  int cnt;
+  char buf[MAX_MSG_SIZE];
+};
+
+void
+osd_init(void)
+{
+  if (dpy)
+    return;
+
+  dpy = XOpenDisplay(NULL);
+  if (!dpy)
+    die("Cannot open display");
+
+  pty = XInternAtom(dpy, "OSD_QUEUE", False);
+  if (!pty)
+    die("Cannot intern OSD_QUEUE atom");
+}
+
+struct osd_msg *
+osd_new_msg(void)
+{
+  struct osd_msg *msg = xmalloc(sizeof(*msg));
+  msg->cnt = 0;
+  return msg;
+}
+
+void
+osd_add_line(struct osd_msg *msg, char *key, char *val)
+{
+  if (!key)
+    key = "";
+  msg->cnt += snprintf(msg->buf + msg->cnt, MAX_MSG_SIZE - msg->cnt - 1, "%s:%s\n", key, val);
+  if (msg->cnt > MAX_MSG_SIZE - 1)
+    die("OSD message too long (at most %d bytes)", MAX_MSG_SIZE);
+}
+
+void
+osd_send(struct osd_msg *msg)
+{
+  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);
+       }
+    }
+}
index df27bce8fd23bb060d19151670dfb6f8172f7afd..56bf5b916ed558164e3efd00548f14846552114a 100644 (file)
 #include <dirent.h>
 #include <getopt.h>
 
-#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 <options>\n\
 \n\
 Options:\n\
 -c, --check\t\tDisplay status only if battery is low\n\
+-e, --check-every=<sec>\tRun on background and check every <sec> seconds\n\
 -w, --warn=<sec>\tBattery is low if less than <sec> 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/osd.h b/osd.h
new file mode 100644 (file)
index 0000000..78b0a26
--- /dev/null
+++ b/osd.h
@@ -0,0 +1,38 @@
+/*
+ *     On-screen Display Daemon -- Utility Functions
+ *
+ *     (c) 2010 Martin Mares <mj@ucw.cz>
+ */
+
+#include <inttypes.h>
+
+#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
+#define DBG(f...) printf(f)
+#else
+#define DBG(f...) do { } while(0)
+#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 0f01ae745fc561624c771b6c449c10a1cebf794c..f3251d02f7a513c9e52985489ca796cadc3b1be5 100644 (file)
--- a/osdc.c
+++ b/osdc.c
@@ -11,8 +11,7 @@
 #include <X11/Xlib.h>
 #include <X11/Xatom.h>
 
-#include "util.h"
-#include "send.h"
+#include "osd.h"
 
 static struct osd_msg *msg;
 
diff --git a/osdd.c b/osdd.c
index 15836ea51b7d36377197c5a94fd307bdb16b64e1..f5cd44f776b31819ae9665805aa0329a35a21a63 100644 (file)
--- a/osdd.c
+++ b/osdd.c
@@ -8,20 +8,17 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include <inttypes.h>
 #include <poll.h>
 #include <getopt.h>
-#include <sys/time.h>
 #include <xosd.h>
 #include <X11/Xlib.h>
 #include <X11/Xatom.h>
 
 #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.c b/send.c
deleted file mode 100644 (file)
index aab11a7..0000000
--- a/send.c
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- *     On-screen Display Client -- Sending Messages
- *
- *     (c) 2010 Martin Mares <mj@ucw.cz>
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <X11/Xlib.h>
-#include <X11/Xatom.h>
-
-#include "util.h"
-#include "send.h"
-
-static Display *dpy;
-static Atom pty;
-
-#define MAX_MSG_SIZE 1024
-
-struct osd_msg {
-  int cnt;
-  char buf[MAX_MSG_SIZE];
-};
-
-void
-osd_init(void)
-{
-  dpy = XOpenDisplay(NULL);
-  if (!dpy)
-    die("Cannot open display");
-
-  pty = XInternAtom(dpy, "OSD_QUEUE", False);
-  if (!pty)
-    die("Cannot intern OSD_QUEUE atom");
-}
-
-struct osd_msg *
-osd_new_msg(void)
-{
-  struct osd_msg *msg = xmalloc(sizeof(*msg));
-  msg->cnt = 0;
-  return msg;
-}
-
-void
-osd_add_line(struct osd_msg *msg, char *key, char *val)
-{
-  if (!key)
-    key = "";
-  msg->cnt += snprintf(msg->buf + msg->cnt, MAX_MSG_SIZE - msg->cnt - 1, "%s:%s\n", key, val);
-  if (msg->cnt > MAX_MSG_SIZE - 1)
-    die("OSD message too long (at most %d bytes)", MAX_MSG_SIZE);
-}
-
-void
-osd_send(struct osd_msg *msg)
-{
-  if (!dpy)
-    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);
-}
diff --git a/send.h b/send.h
deleted file mode 100644 (file)
index e01022d..0000000
--- a/send.h
+++ /dev/null
@@ -1,12 +0,0 @@
-/*
- *     On-screen Display Client -- Sending Messages
- *
- *     (c) 2010 Martin Mares <mj@ucw.cz>
- */
-
-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 fdf8b76091f3a98571ca6681803525887159f0d9..f9cd0c2bc59a3da2502c1ca510e112c3d4fbaf54 100644 (file)
--- a/util.c
+++ b/util.c
@@ -7,8 +7,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
+#include <sys/time.h>
 
-#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;
+}
diff --git a/util.h b/util.h
deleted file mode 100644 (file)
index 23645aa..0000000
--- a/util.h
+++ /dev/null
@@ -1,18 +0,0 @@
-/*
- *     On-screen Display Daemon -- Utility Functions
- *
- *     (c) 2010 Martin Mares <mj@ucw.cz>
- */
-
-#define NONRET __attribute__((noreturn))
-#define FORMAT_CHECK(func,i,j) __attribute__((format(func,i,j)))
-
-void NONRET FORMAT_CHECK(printf,1,2) die(char *fmt, ...);
-
-#ifdef DEBUG
-#define DBG(f...) printf(f)
-#else
-#define DBG(f...) do { } while(0)
-#endif
-
-void *xmalloc(int size);