]> mj.ucw.cz Git - checkmail.git/commitdiff
Added basic support for on-screen display via OSDD
authorMartin Mares <mj@ucw.cz>
Sun, 18 Jul 2010 09:34:57 +0000 (11:34 +0200)
committerMartin Mares <mj@ucw.cz>
Sun, 18 Jul 2010 09:34:57 +0000 (11:34 +0200)
Makefile
cm.c

index 9a26d0ac75ca0aba87f470c5b618bce59df82f53..18d093b7dec313a8672d53eea1446cea3222a6e8 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,8 @@
 # Define if you want support for wide characters (needs libncursesw)
 CONFIG_WIDE_CURSES=1
 
-# Define if you want XKB led controls
-CONFIG_X_LEDS=1
+# Define if you want XKB led controls and on-screen display via OSDD
+CONFIG_X11=1
 
 #DEBUG=-ggdb
 CFLAGS=-O2 -Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -Winline $(DEBUG) -std=gnu99 -DVERSION=$(VERSION) -DYEAR=$(YEAR)
@@ -14,9 +14,9 @@ else
 LDFLAGS=-lncurses
 endif
 
-ifdef CONFIG_X_LEDS
+ifdef CONFIG_X11
 LDFLAGS+=-lX11
-CFLAGS+=-DCONFIG_X_LEDS=1
+CFLAGS+=-DCONFIG_X11=1
 endif
 
 VERSION=1.3
diff --git a/cm.c b/cm.c
index a574e7f2b11767ccca15f2a5ca60eb4c966a8b2a..aa5fc7a0538c00c47af2405737ef18b42b301acd 100644 (file)
--- a/cm.c
+++ b/cm.c
@@ -1,7 +1,7 @@
 /*
  *     Incoming Mail Checker
  *
- *     (c) 2005--2008 Martin Mares <mj@ucw.cz>
+ *     (c) 2005--2010 Martin Mares <mj@ucw.cz>
  */
 
 #define _GNU_SOURCE
@@ -34,6 +34,7 @@
 static int check_interval = 30;
 static int force_refresh;
 static int allow_bells = 1;
+static int allow_osd = 1;
 static int minimum_priority;
 static time_t last_scan_time;
 static char *run_cmd = "mutt -f %s";
@@ -50,6 +51,7 @@ struct options {
   int sender_mbox;
   int hotkey;
   int led;
+  int osd;
 };
 
 struct option_node {
@@ -134,6 +136,7 @@ init_options(struct options *o)
   o->sender_mbox = -1;
   o->hotkey = -1;
   o->led = -1;
+  o->osd = -1;
 }
 
 static void
@@ -156,6 +159,7 @@ setup_options(struct mbox *b)
        MERGE(sender_mbox);
        MERGE(hotkey);
        MERGE(led);
+       MERGE(osd);
       }
 }
 
@@ -577,34 +581,51 @@ scan(void)
   rethink_display();
 }
 
-#ifdef CONFIG_X_LEDS
+#ifdef CONFIG_X11
 
 #include <X11/Xlib.h>
+#include <X11/Xatom.h>
 
-static Display *leds_dpy;
+static Display *x11_dpy;
 static unsigned leds_care, leds_have, leds_want;
+static unsigned osd_care, osd_have, osd_want;
+static Atom osd_pty;
 
 static void
-leds_init(void)
+x11_init(void)
 {
-  leds_care = 0;
+  leds_care = (global_options.led >= 0 ? (1 << global_options.led) : 0);
+  osd_care = (global_options.osd >= 0);
   CLIST_FOR_EACH(struct option_node *, o, options)
-    if (o->o.led > 0)
-      leds_care |= (1 << o->o.led);
-  if (!leds_care)
     {
-      debug("LEDS: No mailbox wants them\n");
+      if (o->o.led > 0)
+       leds_care |= (1 << o->o.led);
+      if (o->o.osd > 0)
+       osd_care = 1;
+    }
+
+  if (!leds_care && !osd_care)
+    {
+      debug("X11: No mailbox wants LEDs or OSD\n");
       return;
     }
   if (!getenv("DISPLAY"))
     {
-      debug("LEDS: Do not have X display\n");
+      debug("X11: Do not have X display\n");
       return;
     }
-  if (!(leds_dpy = XOpenDisplay(NULL)))
+  if (!(x11_dpy = XOpenDisplay(NULL)))
     die("Cannot open X display, although the DISPLAY variable is set");
 
+  if (osd_care)
+    {
+      osd_pty = XInternAtom(x11_dpy, "OSD_QUEUE", False);
+      if (!osd_pty)
+       die("Cannot intern OSD_QUEUE atom");
+    }
+
   leds_have = ~0U;
+  debug("X11: Initialized\n");
 }
 
 static void
@@ -620,29 +641,52 @@ sync_leds(void)
        XKeyboardControl cc;
        cc.led = i;
        cc.led_mode = (leds_want & (1 << i)) ? LedModeOn : LedModeOff;
-       XChangeKeyboardControl(leds_dpy, KBLed | KBLedMode, &cc);
+       XChangeKeyboardControl(x11_dpy, KBLed | KBLedMode, &cc);
       }
-  XFlush(leds_dpy);
+  XFlush(x11_dpy);
   leds_have = leds_want;
 }
 
+static void
+sync_osd(void)
+{
+  if (!osd_want || !allow_osd)
+    {
+      osd_have = 0;
+      return;
+    }
+  if (osd_have)
+    return;
+  debug("OSD: Displaying\n");
+
+  char msg[] = ":You have new mail\n\n";
+  XChangeProperty(x11_dpy, DefaultRootWindow(x11_dpy), osd_pty, XA_STRING, 8, PropModeAppend, (unsigned char *) msg, sizeof(msg)-1);
+  XFlush(x11_dpy);
+}
+
 static void
 rethink_leds(void)
 {
-  if (!leds_dpy)
+  if (!x11_dpy)
     return;
 
   leds_want = 0;
+  osd_want = 0;
   CLIST_FOR_EACH(struct mbox *, b, mboxes)
-    if (b->o.led > 0 && b->new)
-      leds_want |= (1 << b->o.led);
+    {
+      if (b->o.led > 0 && b->new)
+       leds_want |= (1 << b->o.led);
+      if (b->o.osd > 0 && b->new)
+       osd_want = 1;
+    }
   sync_leds();
+  sync_osd();
 }
 
 static void
-leds_cleanup(void)
+x11_cleanup(void)
 {
-  if (!leds_dpy)
+  if (!x11_dpy)
     return;
 
   leds_want = 0;
@@ -651,9 +695,9 @@ leds_cleanup(void)
 
 #else
 
-static void leds_init(void) { }
+static void x11_init(void) { }
 static void rethink_leds(void) { }
-static void leds_cleanup(void) { }
+static void x11_cleanup(void) { }
 
 #endif
 
@@ -958,6 +1002,7 @@ Options:\n\
 Mailbox options (set with `-o', use upper case to negate):\n\
 0-9\t\t\tSet mailbox priority (0=default)\n\
 b\t\t\tBeep when a message arrives\n\
+d\t\t\tSend an on-screen-display message (requires OSDD)\n\
 e\t\t\tHide from display if empty\n\
 f\t\t\tShow flagged messages if there are no new ones\n\
 h\t\t\tHide from display\n\
@@ -1008,6 +1053,9 @@ parse_options(char *c)
          case 'b':
            o->beep = value;
            break;
+         case 'd':
+           o->osd = value;
+           break;
          case 'e':
            o->hide_if_empty = value;
            break;
@@ -1075,7 +1123,7 @@ main(int argc, char **argv)
 
   charset_init();
   term_init();
-  leds_init();
+  x11_init();
   scan_and_redraw();
   next_active(0, 1);
 
@@ -1150,6 +1198,14 @@ restart:
              allow_bells = 0;
              print_status("Bells and whistles are now disabled. Pssst!");
              break;
+           case 'd':
+             allow_osd = 1;
+             print_status("On-screen display is now enabled.");
+             break;
+           case 'D':
+             allow_osd = 0;
+             print_status("On-screen display is now disabled. Watch your step.");
+             break;
            default:
              if (ch >= '0' && ch <= '9')
                {
@@ -1163,7 +1219,7 @@ restart:
        }
     }
 
-  leds_cleanup();
+  x11_cleanup();
   term_cleanup();
   return 0;
 }