2 * On-screen Display -- Support Functions for Clients
4 * (c) 2010 Martin Mares <mj@ucw.cz>
13 #include <X11/Xatom.h>
21 #define MAX_MSG_SIZE 1024
25 char buf[MAX_MSG_SIZE];
34 dpy = XOpenDisplay(NULL);
36 die("Cannot open display");
38 pty = XInternAtom(dpy, "OSD_QUEUE", False);
40 die("Cannot intern OSD_QUEUE atom");
46 struct osd_msg *msg = xmalloc(sizeof(*msg));
52 osd_add_line(struct osd_msg *msg, char *key, char *val)
56 msg->cnt += snprintf(msg->buf + msg->cnt, MAX_MSG_SIZE - msg->cnt - 1, "%s:%s\n", key, val);
57 if (msg->cnt > MAX_MSG_SIZE - 1)
58 die("OSD message too long (at most %d bytes)", MAX_MSG_SIZE);
62 osd_send(struct osd_msg *msg)
65 msg->buf[msg->cnt++] = '\n';
66 if (!XChangeProperty(dpy, DefaultRootWindow(dpy), pty, XA_STRING, 8, PropModeAppend, (unsigned char *) msg->buf, msg->cnt))
67 die("XChangeProperty failed");
78 die("Cannot fork: %m");
87 DBG("Waiting for %d seconds\n", delay);
88 timestamp_t wait_until = get_current_time() + delay*1000;
91 .fd = ConnectionNumber(dpy),
97 timestamp_t now = get_current_time();
98 if (now >= wait_until)
101 DBG("... waiting for %d ms\n", (int)(wait_until - now));
102 poll(&pfd, 1, wait_until - now);
103 if (pfd.revents & POLLIN)
105 // We use the event loop only to detect that the X server has been shut down.
106 // In such cases, xlib raises an error and exits.
108 while (XPending(dpy))
109 XNextEvent(dpy, &ev);