+
+
+static int
+watch (Display *dpy)
+{
+ char *v = 0;
+ Window window = RootWindow (dpy, 0);
+ XWindowAttributes xgwa;
+ XEvent event;
+ CARD32 *last = 0;
+
+ if (v) free (v);
+ XGetWindowAttributes (dpy, window, &xgwa);
+ XSelectInput (dpy, window, xgwa.your_event_mask | PropertyChangeMask);
+
+ while (1)
+ {
+ XNextEvent (dpy, &event);
+ if (event.xany.type == PropertyNotify &&
+ event.xproperty.state == PropertyNewValue &&
+ event.xproperty.atom == XA_SCREENSAVER_STATUS)
+ {
+ Atom type;
+ int format;
+ unsigned long nitems, bytesafter;
+ CARD32 *data = 0;
+
+ if (XGetWindowProperty (dpy,
+ RootWindow (dpy, 0), /* always screen #0 */
+ XA_SCREENSAVER_STATUS,
+ 0, 999, False, XA_INTEGER,
+ &type, &format, &nitems, &bytesafter,
+ (unsigned char **) &data)
+ == Success
+ && type
+ && data)
+ {
+ time_t tt;
+ char *s;
+ Bool changed = False;
+ Bool running = False;
+
+ if (type != XA_INTEGER || nitems < 3)
+ {
+ STATUS_LOSE:
+ if (last) free (last);
+ if (data) free (data);
+ fprintf (stderr, "%s: bad status format on root window.\n",
+ progname);
+ return -1;
+ }
+
+ tt = (time_t) data[1];
+ if (tt <= (time_t) 666000000L) /* early 1991 */
+ goto STATUS_LOSE;
+
+ s = ctime(&tt);
+ if (s[strlen(s)-1] == '\n')
+ s[strlen(s)-1] = 0;
+
+ if (!last || data[0] != last[0])
+ {
+ /* State changed. */
+ if (data[0] == XA_BLANK)
+ printf ("BLANK %s\n", s);
+ else if (data[0] == XA_LOCK)
+ printf ("LOCK %s\n", s);
+ else if (data[0] == 0)
+ printf ("UNBLANK %s\n", s);
+ else
+ goto STATUS_LOSE;
+ }
+
+ if (!last)
+ changed = True;
+ else
+ {
+ int i;
+ for (i = 2; i < nitems; i++)
+ {
+ if (data[i] != last[i])
+ changed = True;
+ if (data[i])
+ running = True;
+ }
+ }
+
+ if (running && changed)
+ {
+ int i;
+ fprintf (stdout, "RUN", s);
+ for (i = 2; i < nitems; i++)
+ fprintf (stdout, " %d", (int) data[i]);
+ fprintf (stdout, "\n");
+ }
+
+ fflush (stdout);
+
+ if (last) free (last);
+ last = data;
+ }
+ else
+ {
+ if (last) free (last);
+ if (data) free (data);
+ fprintf (stderr, "%s: no saver status on root window.\n",
+ progname);
+ return -1;
+ }
+ }
+ }
+}