]> mj.ucw.cz Git - osdd.git/blobdiff - osdd.c
Bits of new display code
[osdd.git] / osdd.c
diff --git a/osdd.c b/osdd.c
index 15836ea51b7d36377197c5a94fd307bdb16b64e1..8626cbd8f96ea276a7a793e84937784bc271498c 100644 (file)
--- a/osdd.c
+++ b/osdd.c
@@ -1,37 +1,36 @@
 /*
  *     On-screen Display Daemon
  *
- *     (c) 2010 Martin Mares <mj@ucw.cz>
+ *     (c) 2010--2013 Martin Mares <mj@ucw.cz>
  */
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include <inttypes.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>
 
 #undef DEBUG
 #include "util.h"
+#include "display.h"
 
-static xosd *osd;
+static struct osd_state *osd;
 
-typedef uint64_t timestamp_t;
 static timestamp_t now;
 
 /*** Options ***/
 
-static int num_lines = 4;
-static char *font_name = "-bitstream-bitstream vera sans-bold-r-normal-*-*-320-*-*-p-*-*";
+static int num_lines = 4;      // FIXME
+static char *font_name = "-bitstream-bitstream vera sans-bold-r-normal-*-*-320-*-*-p-*-*";     // FIXME
 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:Df:l:m:o:";
@@ -115,11 +114,10 @@ display_msg(struct msg *msg)
   DBG("## Displaying message\n");
   msg->min_light = now + default_min_duration;
   msg->max_light = now + default_duration;
-  xosd_set_colour(osd, default_color);
-  xosd_set_outline_colour(osd, default_outline_color);
+  char *fg_color = default_color;
+  char *outline_color = default_outline_color;
 
   char *line = msg->text;
-  int row = 0;
   while (*line)
     {
       // The parser it destructive, but it does not do any harm, since we display each message only once.
@@ -140,31 +138,36 @@ display_msg(struct msg *msg)
        }
       DBG("\t%s:%s\n", key, val);
 
+      struct osd_line *l = NULL;
       if (!key[0])
        {
-         if (row < num_lines)
-           xosd_display(osd, row++, XOSD_string, val);
+         l = osd_add_line(osd, OSD_TYPE_TEXT);
+         sprintf(l->u.text, "%.*s", OSD_MAX_LINE_LEN, val);
        }
       else if (!strcmp(key, "percentage") || !strcmp(key, "percent"))
        {
-         if (row < num_lines)
-           xosd_display(osd, row++, XOSD_percentage, atoi(val));
+         // FIXME
+         // xosd_display(osd, row++, XOSD_percentage, atoi(val));
        }
       else if (!strcmp(key, "slider"))
        {
-         if (row < num_lines)
-           xosd_display(osd, row++, XOSD_slider, atoi(val));
+         // FIXME
+         // xsd_display(osd, row++, XOSD_slider, atoi(val));
        }
       else if (!strcmp(key, "duration"))
        msg->max_light = now + atoi(val);
       else if (!strcmp(key, "min-duration"))
        msg->min_light = now + atoi(val);
       else if (!strcmp(key, "color"))
-       xosd_set_colour(osd, val);
+       fg_color = val;                         // FIXME: Need copying!
       else if (!strcmp(key, "outline-color"))
-       xosd_set_outline_colour(osd, val);
-      else
-       xosd_display(osd, (row < num_lines ? row++ : num_lines-1), XOSD_string, "PARSE ERROR");
+       outline_color = val;                    // FIXME: Need copying!
+
+      if (l)
+       {
+         l->fg_color = fg_color;
+         l->outline_color = outline_color;
+       }
 
       line = nl;
     }
@@ -177,9 +180,8 @@ static void
 hide_msg(struct msg *msg)
 {
   DBG("## Hiding message\n");
-  for (int i=0; i<num_lines; i++)
-    xosd_display(osd, i, XOSD_string, "");
-  xosd_hide(osd);
+  osd_hide(osd);
+  // FIXME: Reset the osd state
   free(msg);
 }
 
@@ -231,6 +233,7 @@ int
 main(int argc, char **argv)
 {
   parse_opts(argc, argv);
+  setlocale(LC_CTYPE, "");
   XInitThreads();
 
   Display *dpy = XOpenDisplay(NULL);
@@ -246,10 +249,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();
@@ -259,13 +259,8 @@ main(int argc, char **argv)
   XDeleteProperty(dpy, win, pty);
   XFlush(dpy);
 
-  osd = xosd_create(num_lines);
-  if (!osd)
-    die("Cannot initialize OSD");
-  xosd_set_font(osd, font_name);
-  xosd_set_outline_offset(osd, 2);
-  xosd_set_pos(osd, XOSD_middle);
-  xosd_set_align(osd, XOSD_center);
+  osd = osd_new(dpy);
+  osd_set_font(osd, font_name);
 
   struct pollfd pfd = {
     .fd = ConnectionNumber(dpy),
@@ -274,9 +269,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)
@@ -307,6 +300,8 @@ main(int argc, char **argv)
            {
              XEvent ev;
              XNextEvent(dpy, &ev);
+             if (osd_handle_event(osd, &ev))
+               continue;
              if (ev.type != PropertyNotify)
                continue;
              XPropertyEvent *p = &ev.xproperty;