2 * On-screen Display Daemon
4 * (c) 2010 Martin Mares <mj@ucw.cz>
17 #include <X11/Xatom.h>
24 typedef uint64_t timestamp_t;
25 static timestamp_t now;
29 static int num_lines = 4;
30 static char *font_name = "-bitstream-bitstream vera sans-bold-r-normal-*-*-320-*-*-p-*-*";
31 static char *default_color = "green";
32 static char *default_outline_color = "black";
33 static int default_duration = 1000;
34 static int default_min_duration = 1000;
35 static int debug_mode;
37 static const char short_opts[] = "c:d:Df:l:m:o:";
39 static const struct option long_opts[] = {
40 { "color", required_argument, NULL, 'c' },
41 { "debug", no_argument, NULL, 'D' },
42 { "duration", required_argument, NULL, 'd' },
43 { "font", required_argument, NULL, 'f' },
44 { "lines", required_argument, NULL, 'l' },
45 { "min-duration", required_argument, NULL, 'm' },
46 { "outline-color", required_argument, NULL, 'o' },
53 fprintf(stderr, "Usage: osdd <options>\n\n\
55 -c, --color=<c>\t\tDefault color (#rgb, #rrggbb or a name from rgb.txt)\n\
56 -D, --debug\t\tDebugging mode (do not detach from the terminal)\n\
57 -d, --duration=<ms>\tDefault message duration in milliseconds\n\
58 -f, --font=<f>\t\tFont to use for the OSD\n\
59 -l, --lines=<n>\t\tNumber of lines of the OSD\n\
60 -m, --min-duration=<ms>\tDefault minimum message duration in milliseconds\n\
61 -o, --outline-color=<c>\tDefault outline color\n\
67 parse_opts(int argc, char **argv)
70 while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
74 default_color = optarg;
77 default_duration = atoi(optarg);
86 num_lines = atoi(optarg);
91 default_min_duration = atoi(optarg);
94 default_outline_color = optarg;
104 /*** Displaying of messages ***/
108 timestamp_t min_light, max_light;
113 display_msg(struct msg *msg)
115 DBG("## Displaying message\n");
116 msg->min_light = now + default_min_duration;
117 msg->max_light = now + default_duration;
118 xosd_set_colour(osd, default_color);
119 xosd_set_outline_colour(osd, default_outline_color);
121 char *line = msg->text;
125 // The parser it destructive, but it does not do any harm, since we display each message only once.
126 char *nl = strchr(line, '\n');
130 char *val = strchr(line, ':');
141 DBG("\t%s:%s\n", key, val);
146 xosd_display(osd, row++, XOSD_string, val);
148 else if (!strcmp(key, "percentage") || !strcmp(key, "percent"))
151 xosd_display(osd, row++, XOSD_percentage, atoi(val));
153 else if (!strcmp(key, "slider"))
156 xosd_display(osd, row++, XOSD_slider, atoi(val));
158 else if (!strcmp(key, "duration"))
159 msg->max_light = now + atoi(val);
160 else if (!strcmp(key, "min-duration"))
161 msg->min_light = now + atoi(val);
162 else if (!strcmp(key, "color"))
163 xosd_set_colour(osd, val);
164 else if (!strcmp(key, "outline-color"))
165 xosd_set_outline_colour(osd, val);
167 xosd_display(osd, (row < num_lines ? row++ : num_lines-1), XOSD_string, "PARSE ERROR");
172 if (msg->min_light > msg->max_light)
173 msg->min_light = msg->max_light;
177 hide_msg(struct msg *msg)
179 DBG("## Hiding message\n");
180 for (int i=0; i<num_lines; i++)
181 xosd_display(osd, i, XOSD_string, "");
186 /*** The message queue ***/
188 static struct msg *current_msg, *first_msg, *last_msg;
191 enqueue_msg(unsigned char *buf, int len)
193 DBG("Received: [%.*s]\n", len, buf);
194 if (!len || buf[len-1] != '\n')
197 struct msg *msg = xmalloc(sizeof(*msg) + len);
198 memcpy(msg->text, buf, len);
202 last_msg->next = msg;
210 parse_input(unsigned char *buf, int len)
212 /* The property might contain several messages concatenated. Split them. */
221 while (i < len && (buf[i] != '\n' || (i && buf[i-1] != '\n')))
231 main(int argc, char **argv)
233 parse_opts(argc, argv);
236 Display *dpy = XOpenDisplay(NULL);
238 die("Cannot open display");
239 Window win = DefaultRootWindow(dpy);
241 Atom pty = XInternAtom(dpy, "OSD_QUEUE", False);
243 die("Cannot intern OSD_QUEUE atom");
250 fprintf(stderr, "batt: Cannot fork: %m\n");
258 XSelectInput(dpy, win, PropertyChangeMask);
259 XDeleteProperty(dpy, win, pty);
262 osd = xosd_create(num_lines);
264 die("Cannot initialize OSD");
265 xosd_set_font(osd, font_name);
266 xosd_set_outline_offset(osd, 2);
267 xosd_set_pos(osd, XOSD_middle);
268 xosd_set_align(osd, XOSD_center);
270 struct pollfd pfd = {
271 .fd = ConnectionNumber(dpy),
278 gettimeofday(&tv, NULL);
279 now = (timestamp_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
281 timestamp_t wait_until = now - 1;
282 if (!current_msg && first_msg)
284 current_msg = first_msg;
285 first_msg = first_msg->next;
286 display_msg(current_msg);
291 wait_until = current_msg->min_light;
293 wait_until = current_msg->max_light;
294 if (wait_until <= now)
296 hide_msg(current_msg);
302 DBG("... waiting for %d ms\n", (int)(wait_until - now));
303 poll(&pfd, 1, wait_until - now);
304 if (pfd.revents & POLLIN)
306 while (XPending(dpy))
309 XNextEvent(dpy, &ev);
310 if (ev.type != PropertyNotify)
312 XPropertyEvent *p = &ev.xproperty;
313 if (p->window == win && p->atom == pty)
317 unsigned long pty_items, pty_remains;
318 unsigned char *pty_buf = NULL;
319 XGetWindowProperty(dpy, win, pty, 0, 4096, True, XA_STRING, &pty_type, &pty_fmt, &pty_items, &pty_remains, &pty_buf);
320 if (pty_type == XA_STRING && pty_fmt == 8 && pty_items)
321 parse_input(pty_buf, pty_items);