2 * On-screen Display Daemon
4 * (c) 2010 Martin Mares <mj@ucw.cz>
16 #include <X11/Xatom.h>
23 typedef uint64_t timestamp_t;
24 static timestamp_t now;
28 static int num_lines = 4;
29 static char *font_name = "-bitstream-bitstream vera sans-bold-r-normal-*-*-320-*-*-p-*-*";
30 static char *default_color = "green";
31 static char *default_outline_color = "black";
32 static int default_duration = 1000;
33 static int default_min_duration = 1000;
35 static const char short_opts[] = "c:d:f:l:m:o:";
37 static const struct option long_opts[] = {
38 { "color", required_argument, NULL, 'c' },
39 { "duration", required_argument, NULL, 'd' },
40 { "font", required_argument, NULL, 'f' },
41 { "lines", required_argument, NULL, 'l' },
42 { "min-duration", required_argument, NULL, 'm' },
43 { "outline-color", required_argument, NULL, 'o' },
50 fprintf(stderr, "Usage: osdd <options>\n\n\
52 -c, --color=<c>\t\tDefault color (#rgb, #rrggbb or a name from rgb.txt)\n\
53 -d, --duration=<ms>\tDefault message duration in milliseconds\n\
54 -f, --font=<f>\t\tFont to use for the OSD\n\
55 -l, --lines=<n>\t\tNumber of lines of the OSD\n\
56 -m, --min-duration=<ms>\tDefault minimum message duration in milliseconds\n\
57 -o, --outline-color=<c>\tDefault outline color\n\
63 parse_opts(int argc, char **argv)
66 while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
70 default_color = optarg;
73 default_duration = atoi(optarg);
79 num_lines = atoi(optarg);
84 default_min_duration = atoi(optarg);
87 default_outline_color = optarg;
97 /*** Displaying of messages ***/
101 timestamp_t min_light, max_light;
106 display_msg(struct msg *msg)
108 DBG("## Displaying message\n");
109 msg->min_light = now + default_min_duration;
110 msg->max_light = now + default_duration;
111 xosd_set_colour(osd, default_color);
112 xosd_set_outline_colour(osd, default_outline_color);
114 char *line = msg->text;
118 // The parser it destructive, but it does not do any harm, since we display each message only once.
119 char *nl = strchr(line, '\n');
123 char *val = strchr(line, ':');
134 DBG("\t%s:%s\n", key, val);
139 xosd_display(osd, row++, XOSD_string, val);
141 else if (!strcmp(key, "percentage") || !strcmp(key, "percent"))
144 xosd_display(osd, row++, XOSD_percentage, atoi(val));
146 else if (!strcmp(key, "slider"))
149 xosd_display(osd, row++, XOSD_slider, atoi(val));
151 else if (!strcmp(key, "duration"))
152 msg->max_light = now + atoi(val);
153 else if (!strcmp(key, "min-duration"))
154 msg->min_light = now + atoi(val);
155 else if (!strcmp(key, "color"))
156 xosd_set_colour(osd, val);
157 else if (!strcmp(key, "outline-color"))
158 xosd_set_outline_colour(osd, val);
160 xosd_display(osd, (row < num_lines ? row++ : num_lines-1), XOSD_string, "PARSE ERROR");
165 if (msg->min_light > msg->max_light)
166 msg->min_light = msg->max_light;
170 hide_msg(struct msg *msg)
172 DBG("## Hiding message\n");
173 for (int i=0; i<num_lines; i++)
174 xosd_display(osd, i, XOSD_string, "");
179 /*** The message queue ***/
181 static struct msg *current_msg, *first_msg, *last_msg;
184 enqueue_msg(unsigned char *buf, int len)
186 DBG("Received: [%.*s]\n", len, buf);
187 if (!len || buf[len-1] != '\n')
190 struct msg *msg = xmalloc(sizeof(*msg) + len);
191 memcpy(msg->text, buf, len);
195 last_msg->next = msg;
203 parse_input(unsigned char *buf, int len)
205 /* The property might contain several messages concatenated. Split them. */
214 while (i < len && (buf[i] != '\n' || (i && buf[i-1] != '\n')))
224 main(int argc, char **argv)
226 parse_opts(argc, argv);
229 Display *dpy = XOpenDisplay(NULL);
231 die("Cannot open display");
232 Window win = DefaultRootWindow(dpy);
234 Atom pty = XInternAtom(dpy, "OSD_QUEUE", False);
236 die("Cannot intern OSD_QUEUE atom");
238 XSelectInput(dpy, win, PropertyChangeMask);
239 XDeleteProperty(dpy, win, pty);
242 osd = xosd_create(num_lines);
244 die("Cannot initialize OSD");
245 xosd_set_font(osd, font_name);
246 xosd_set_outline_offset(osd, 2);
247 xosd_set_pos(osd, XOSD_middle);
248 xosd_set_align(osd, XOSD_center);
250 struct pollfd pfd = {
251 .fd = ConnectionNumber(dpy),
258 gettimeofday(&tv, NULL);
259 now = (timestamp_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
261 timestamp_t wait_until = now - 1;
262 if (!current_msg && first_msg)
264 current_msg = first_msg;
265 first_msg = first_msg->next;
266 display_msg(current_msg);
271 wait_until = current_msg->min_light;
273 wait_until = current_msg->max_light;
274 if (wait_until <= now)
276 hide_msg(current_msg);
282 DBG("... waiting for %d ms\n", (int)(wait_until - now));
283 poll(&pfd, 1, wait_until - now);
284 if (pfd.revents & POLLIN)
286 while (XPending(dpy))
289 XNextEvent(dpy, &ev);
290 if (ev.type != PropertyNotify)
292 XPropertyEvent *p = &ev.xproperty;
293 if (p->window == win && p->atom == pty)
297 unsigned long pty_items, pty_remains;
298 unsigned char *pty_buf = NULL;
299 XGetWindowProperty(dpy, win, pty, 0, 4096, True, XA_STRING, &pty_type, &pty_fmt, &pty_items, &pty_remains, &pty_buf);
300 if (pty_type == XA_STRING && pty_fmt == 8 && pty_items)
301 parse_input(pty_buf, pty_items);