]> mj.ucw.cz Git - osdd.git/blobdiff - osdd.c
Makefile: Link libX11 explicitly
[osdd.git] / osdd.c
diff --git a/osdd.c b/osdd.c
index 8626cbd8f96ea276a7a793e84937784bc271498c..a3364fc8526306a91996b5fdd65a551b3d78a9cf 100644 (file)
--- a/osdd.c
+++ b/osdd.c
@@ -1,7 +1,7 @@
 /*
  *     On-screen Display Daemon
  *
- *     (c) 2010--2013 Martin Mares <mj@ucw.cz>
+ *     (c) 2010--2014 Martin Mares <mj@ucw.cz>
  */
 
 #include <stdio.h>
@@ -11,7 +11,6 @@
 #include <poll.h>
 #include <getopt.h>
 #include <locale.h>
-#include <xosd.h>
 #include <X11/Xlib.h>
 #include <X11/Xatom.h>
 
@@ -25,24 +24,32 @@ static timestamp_t now;
 
 /*** Options ***/
 
-static int num_lines = 4;      // FIXME
-static char *font_name = "-bitstream-bitstream vera sans-bold-r-normal-*-*-320-*-*-p-*-*";     // FIXME
+static char *font_name = "times-64:bold";
 static char *default_color = "green";
 static char *default_outline_color = "black";
+static int default_outline_width = 2;
 static int default_duration = 1000;
 static int default_min_duration = 250;
 static int debug_mode;
+static int test_mode;
+static double line_spacing = 0.2;
 
-static const char short_opts[] = "c:d:Df:l:m:o:";
+static const char short_opts[] = "c:d:Df:m:o:O:s:";
+
+enum long_opt {
+  OPT_TEST = 256,
+};
 
 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' },
+  { "outline-width",   required_argument,      NULL,   'O' },
+  { "line-spacing",    required_argument,      NULL,   's' },
+  { "test",            no_argument,            NULL,   OPT_TEST },     // Undocumented test mode
   { NULL,              0,                      NULL,   0   },
 };
 
@@ -55,9 +62,10 @@ Options:\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\
 -m, --min-duration=<ms>\tDefault minimum message duration in milliseconds\n\
 -o, --outline-color=<c>\tDefault outline color\n\
+-O, --outline-width=<n>\tDefault outline width (default=2)\n\
+-s, --line-spacing=<n>\tSet line spacing factor (decimal fraction, default=0.2)\n\
 ");
   exit(1);
 }
@@ -82,9 +90,7 @@ parse_opts(int argc, char **argv)
        font_name = optarg;
        break;
       case 'l':
-       num_lines = atoi(optarg);
-       if (num_lines < 1)
-         usage();
+       line_spacing = atof(optarg);
        break;
       case 'm':
        default_min_duration = atoi(optarg);
@@ -92,6 +98,13 @@ parse_opts(int argc, char **argv)
       case 'o':
        default_outline_color = optarg;
        break;
+      case 'O':
+       default_outline_width = atoi(optarg);
+       break;
+      case OPT_TEST:
+       test_mode = 1;
+       debug_mode = 1;
+       break;
       default:
        usage();
       }
@@ -116,6 +129,7 @@ display_msg(struct msg *msg)
   msg->max_light = now + default_duration;
   char *fg_color = default_color;
   char *outline_color = default_outline_color;
+  int outline_width = default_outline_width;
 
   char *line = msg->text;
   while (*line)
@@ -146,27 +160,30 @@ display_msg(struct msg *msg)
        }
       else if (!strcmp(key, "percentage") || !strcmp(key, "percent"))
        {
-         // FIXME
-         // xosd_display(osd, row++, XOSD_percentage, atoi(val));
+         l = osd_add_line(osd, OSD_TYPE_PERCENTAGE);
+         l->u.percent = atoi(val);
        }
       else if (!strcmp(key, "slider"))
        {
-         // FIXME
-         // xsd_display(osd, row++, XOSD_slider, atoi(val));
+         l = osd_add_line(osd, OSD_TYPE_SLIDER);
+         l->u.percent = 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"))
-       fg_color = val;                         // FIXME: Need copying!
+       fg_color = val;
       else if (!strcmp(key, "outline-color"))
-       outline_color = val;                    // FIXME: Need copying!
+       outline_color = val;
+      else if (!strcmp(key, "outline-width"))
+       outline_width = atoi(val);
 
       if (l)
        {
          l->fg_color = fg_color;
          l->outline_color = outline_color;
+         l->outline_width = outline_width;
        }
 
       line = nl;
@@ -174,14 +191,15 @@ display_msg(struct msg *msg)
 
   if (msg->min_light > msg->max_light)
     msg->min_light = msg->max_light;
+
+  osd_show(osd);
 }
 
 static void
 hide_msg(struct msg *msg)
 {
   DBG("## Hiding message\n");
-  osd_hide(osd);
-  // FIXME: Reset the osd state
+  osd_clear(osd);
   free(msg);
 }
 
@@ -227,6 +245,19 @@ parse_input(unsigned char *buf, int len)
     }
 }
 
+static void
+do_test(void)
+{
+  unsigned char buf[4096];
+  int len = 0;
+  int c;
+
+  while ((c = read(0, buf + len, 4096 - len)) > 0)
+    len += c;
+  if (len)
+    enqueue_msg(buf, len);
+}
+
 /*** Main loop ***/
 
 int
@@ -255,12 +286,20 @@ main(int argc, char **argv)
       setsid();
     }
 
-  XSelectInput(dpy, win, PropertyChangeMask);
-  XDeleteProperty(dpy, win, pty);
-  XFlush(dpy);
+  if (test_mode)
+    {
+      do_test();
+      pty = 0;
+    }
+  else
+    {
+      XSelectInput(dpy, win, PropertyChangeMask);
+      XDeleteProperty(dpy, win, pty);
+      XFlush(dpy);
+    }
 
   osd = osd_new(dpy);
-  osd_set_font(osd, font_name);
+  osd_set_font(osd, font_name, line_spacing);
 
   struct pollfd pfd = {
     .fd = ConnectionNumber(dpy),
@@ -291,6 +330,8 @@ main(int argc, char **argv)
              continue;
            }
        }
+      if (test_mode && !current_msg)
+       break;
 
       DBG("... waiting for %d ms\n", (int)(wait_until - now));
       poll(&pfd, 1, wait_until - now);