2 * On-screen Display Daemon
4 * (c) 2010 Martin Mares <mj@ucw.cz>
16 #include <X11/Xatom.h>
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 = 250;
34 static int debug_mode;
36 static const char short_opts[] = "c:d:Df:l:m:o:";
38 static const struct option long_opts[] = {
39 { "color", required_argument, NULL, 'c' },
40 { "debug", no_argument, NULL, 'D' },
41 { "duration", required_argument, NULL, 'd' },
42 { "font", required_argument, NULL, 'f' },
43 { "lines", required_argument, NULL, 'l' },
44 { "min-duration", required_argument, NULL, 'm' },
45 { "outline-color", required_argument, NULL, 'o' },
52 fprintf(stderr, "Usage: osdd <options>\n\n\
54 -c, --color=<c>\t\tDefault color (#rgb, #rrggbb or a name from rgb.txt)\n\
55 -D, --debug\t\tDebugging mode (do not detach from the terminal)\n\
56 -d, --duration=<ms>\tDefault message duration in milliseconds\n\
57 -f, --font=<f>\t\tFont to use for the OSD\n\
58 -l, --lines=<n>\t\tNumber of lines of the OSD\n\
59 -m, --min-duration=<ms>\tDefault minimum message duration in milliseconds\n\
60 -o, --outline-color=<c>\tDefault outline color\n\
66 parse_opts(int argc, char **argv)
69 while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
73 default_color = optarg;
76 default_duration = atoi(optarg);
85 num_lines = atoi(optarg);
90 default_min_duration = atoi(optarg);
93 default_outline_color = optarg;
103 /*** Displaying of messages ***/
107 timestamp_t min_light, max_light;
112 display_msg(struct msg *msg)
114 DBG("## Displaying message\n");
115 msg->min_light = now + default_min_duration;
116 msg->max_light = now + default_duration;
117 xosd_set_colour(osd, default_color);
118 xosd_set_outline_colour(osd, default_outline_color);
120 char *line = msg->text;
124 // The parser it destructive, but it does not do any harm, since we display each message only once.
125 char *nl = strchr(line, '\n');
129 char *val = strchr(line, ':');
140 DBG("\t%s:%s\n", key, val);
145 xosd_display(osd, row++, XOSD_string, val);
147 else if (!strcmp(key, "percentage") || !strcmp(key, "percent"))
150 xosd_display(osd, row++, XOSD_percentage, atoi(val));
152 else if (!strcmp(key, "slider"))
155 xosd_display(osd, row++, XOSD_slider, atoi(val));
157 else if (!strcmp(key, "duration"))
158 msg->max_light = now + atoi(val);
159 else if (!strcmp(key, "min-duration"))
160 msg->min_light = now + atoi(val);
161 else if (!strcmp(key, "color"))
162 xosd_set_colour(osd, val);
163 else if (!strcmp(key, "outline-color"))
164 xosd_set_outline_colour(osd, val);
166 xosd_display(osd, (row < num_lines ? row++ : num_lines-1), XOSD_string, "PARSE ERROR");
171 if (msg->min_light > msg->max_light)
172 msg->min_light = msg->max_light;
176 hide_msg(struct msg *msg)
178 DBG("## Hiding message\n");
179 for (int i=0; i<num_lines; i++)
180 xosd_display(osd, i, XOSD_string, "");
185 /*** The message queue ***/
187 static struct msg *current_msg, *first_msg, *last_msg;
190 enqueue_msg(unsigned char *buf, int len)
192 DBG("Received: [%.*s]\n", len, buf);
193 if (!len || buf[len-1] != '\n')
196 struct msg *msg = xmalloc(sizeof(*msg) + len);
197 memcpy(msg->text, buf, len);
201 last_msg->next = msg;
209 parse_input(unsigned char *buf, int len)
211 /* The property might contain several messages concatenated. Split them. */
220 while (i < len && (buf[i] != '\n' || (i && buf[i-1] != '\n')))
230 main(int argc, char **argv)
232 parse_opts(argc, argv);
233 setlocale(LC_CTYPE, "");
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");
249 die("Cannot fork: %m");
255 XSelectInput(dpy, win, PropertyChangeMask);
256 XDeleteProperty(dpy, win, pty);
259 osd = xosd_create(num_lines);
261 die("Cannot initialize OSD");
262 xosd_set_font(osd, font_name);
263 xosd_set_outline_offset(osd, 2);
264 xosd_set_pos(osd, XOSD_middle);
265 xosd_set_align(osd, XOSD_center);
267 struct pollfd pfd = {
268 .fd = ConnectionNumber(dpy),
274 now = get_current_time();
276 timestamp_t wait_until = now - 1;
277 if (!current_msg && first_msg)
279 current_msg = first_msg;
280 first_msg = first_msg->next;
281 display_msg(current_msg);
286 wait_until = current_msg->min_light;
288 wait_until = current_msg->max_light;
289 if (wait_until <= now)
291 hide_msg(current_msg);
297 DBG("... waiting for %d ms\n", (int)(wait_until - now));
298 poll(&pfd, 1, wait_until - now);
299 if (pfd.revents & POLLIN)
301 while (XPending(dpy))
304 XNextEvent(dpy, &ev);
305 if (ev.type != PropertyNotify)
307 XPropertyEvent *p = &ev.xproperty;
308 if (p->window == win && p->atom == pty)
312 unsigned long pty_items, pty_remains;
313 unsigned char *pty_buf = NULL;
314 XGetWindowProperty(dpy, win, pty, 0, 4096, True, XA_STRING, &pty_type, &pty_fmt, &pty_items, &pty_remains, &pty_buf);
315 if (pty_type == XA_STRING && pty_fmt == 8 && pty_items)
316 parse_input(pty_buf, pty_items);