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