]> mj.ucw.cz Git - osdd.git/blobdiff - osdd.c
osd-batt: Rewritten to use sysfs instead of /proc/acpi/
[osdd.git] / osdd.c
diff --git a/osdd.c b/osdd.c
index 98a35a6d09d23285afed741cc992664e8b264b32..ba279b292816f88fffdb26fe45d666c26f077664 100644 (file)
--- a/osdd.c
+++ b/osdd.c
@@ -7,20 +7,19 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <inttypes.h>
+#include <unistd.h>
 #include <poll.h>
 #include <getopt.h>
-#include <sys/time.h>
+#include <locale.h>
 #include <xosd.h>
 #include <X11/Xlib.h>
 #include <X11/Xatom.h>
 
-#define DEBUG
-#include "util.h"
+#undef DEBUG
+#include "osd.h"
 
 static xosd *osd;
 
-typedef uint64_t timestamp_t;
 static timestamp_t now;
 
 /*** Options ***/
@@ -30,18 +29,20 @@ static char *font_name = "-bitstream-bitstream vera sans-bold-r-normal-*-*-320-*
 static char *default_color = "green";
 static char *default_outline_color = "black";
 static int default_duration = 1000;
-static int default_min_duration = 1000;
+static int default_min_duration = 250;
+static int debug_mode;
 
-static const char short_opts[] = "c:d:f:l:m:o:";
+static const char short_opts[] = "c:d:Df:l:m:o:";
 
 static const struct option long_opts[] = {
   { "color",           required_argument,      NULL,   'c' },
+  { "debug",           no_argument,            NULL,   'D' },
   { "duration",                required_argument,      NULL,   'd' },
   { "font",            required_argument,      NULL,   'f' },
   { "lines",           required_argument,      NULL,   'l' },
   { "min-duration",    required_argument,      NULL,   'm' },
   { "outline-color",   required_argument,      NULL,   'o' },
-  { NULL,      0,                      NULL,   0   },
+  { NULL,              0,                      NULL,   0   },
 };
 
 static void NONRET
@@ -50,6 +51,7 @@ usage(void)
   fprintf(stderr, "Usage: osdd <options>\n\n\
 Options:\n\
 -c, --color=<c>\t\tDefault color (#rgb, #rrggbb or a name from rgb.txt)\n\
+-D, --debug\t\tDebugging mode (do not detach from the terminal)\n\
 -d, --duration=<ms>\tDefault message duration in milliseconds\n\
 -f, --font=<f>\t\tFont to use for the OSD\n\
 -l, --lines=<n>\t\tNumber of lines of the OSD\n\
@@ -72,6 +74,9 @@ parse_opts(int argc, char **argv)
       case 'd':
        default_duration = atoi(optarg);
        break;
+      case 'D':
+       debug_mode = 1;
+       break;
       case 'f':
        font_name = optarg;
        break;
@@ -157,7 +162,7 @@ display_msg(struct msg *msg)
       else if (!strcmp(key, "outline-color"))
        xosd_set_outline_colour(osd, val);
       else
-       DBG("\tPARSE ERROR\n");
+       xosd_display(osd, (row < num_lines ? row++ : num_lines-1), XOSD_string, "PARSE ERROR");
 
       line = nl;
     }
@@ -224,6 +229,7 @@ int
 main(int argc, char **argv)
 {
   parse_opts(argc, argv);
+  setlocale(LC_CTYPE, "");
   XInitThreads();
 
   Display *dpy = XOpenDisplay(NULL);
@@ -235,6 +241,16 @@ main(int argc, char **argv)
   if (!pty)
     die("Cannot intern OSD_QUEUE atom");
 
+  if (!debug_mode)
+    {
+      pid_t pid = fork();
+      if (pid < 0)
+       die("Cannot fork: %m");
+      if (pid > 0)
+        return 0;
+      setsid();
+    }
+
   XSelectInput(dpy, win, PropertyChangeMask);
   XDeleteProperty(dpy, win, pty);
   XFlush(dpy);
@@ -254,9 +270,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)