2 * On-screen Display Daemon
4 * (c) 2010 Martin Mares <mj@ucw.cz>
15 #include <X11/Xatom.h>
22 static timestamp_t now;
26 static int num_lines = 4;
27 static char *font_name = "-bitstream-bitstream vera sans-bold-r-normal-*-*-320-*-*-p-*-*";
28 static char *default_color = "green";
29 static char *default_outline_color = "black";
30 static int default_duration = 1000;
31 static int default_min_duration = 250;
32 static int debug_mode;
34 static const char short_opts[] = "c:d:Df:l:m:o:";
36 static const struct option long_opts[] = {
37 { "color", required_argument, NULL, 'c' },
38 { "debug", no_argument, NULL, 'D' },
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, --debug\t\tDebugging mode (do not detach from the terminal)\n\
54 -d, --duration=<ms>\tDefault message duration in milliseconds\n\
55 -f, --font=<f>\t\tFont to use for the OSD\n\
56 -l, --lines=<n>\t\tNumber of lines of the OSD\n\
57 -m, --min-duration=<ms>\tDefault minimum message duration in milliseconds\n\
58 -o, --outline-color=<c>\tDefault outline color\n\
64 parse_opts(int argc, char **argv)
67 while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
71 default_color = optarg;
74 default_duration = atoi(optarg);
83 num_lines = atoi(optarg);
88 default_min_duration = atoi(optarg);
91 default_outline_color = optarg;
101 /*** Displaying of messages ***/
105 timestamp_t min_light, max_light;
110 display_msg(struct msg *msg)
112 DBG("## Displaying message\n");
113 msg->min_light = now + default_min_duration;
114 msg->max_light = now + default_duration;
115 xosd_set_colour(osd, default_color);
116 xosd_set_outline_colour(osd, default_outline_color);
118 char *line = msg->text;
122 // The parser it destructive, but it does not do any harm, since we display each message only once.
123 char *nl = strchr(line, '\n');
127 char *val = strchr(line, ':');
138 DBG("\t%s:%s\n", key, val);
143 xosd_display(osd, row++, XOSD_string, val);
145 else if (!strcmp(key, "percentage") || !strcmp(key, "percent"))
148 xosd_display(osd, row++, XOSD_percentage, atoi(val));
150 else if (!strcmp(key, "slider"))
153 xosd_display(osd, row++, XOSD_slider, atoi(val));
155 else if (!strcmp(key, "duration"))
156 msg->max_light = now + atoi(val);
157 else if (!strcmp(key, "min-duration"))
158 msg->min_light = now + atoi(val);
159 else if (!strcmp(key, "color"))
160 xosd_set_colour(osd, val);
161 else if (!strcmp(key, "outline-color"))
162 xosd_set_outline_colour(osd, val);
164 xosd_display(osd, (row < num_lines ? row++ : num_lines-1), XOSD_string, "PARSE ERROR");
169 if (msg->min_light > msg->max_light)
170 msg->min_light = msg->max_light;
174 hide_msg(struct msg *msg)
176 DBG("## Hiding message\n");
177 for (int i=0; i<num_lines; i++)
178 xosd_display(osd, i, XOSD_string, "");
183 /*** The message queue ***/
185 static struct msg *current_msg, *first_msg, *last_msg;
188 enqueue_msg(unsigned char *buf, int len)
190 DBG("Received: [%.*s]\n", len, buf);
191 if (!len || buf[len-1] != '\n')
194 struct msg *msg = xmalloc(sizeof(*msg) + len);
195 memcpy(msg->text, buf, len);
199 last_msg->next = msg;
207 parse_input(unsigned char *buf, int len)
209 /* The property might contain several messages concatenated. Split them. */
218 while (i < len && (buf[i] != '\n' || (i && buf[i-1] != '\n')))
228 main(int argc, char **argv)
230 parse_opts(argc, argv);
233 Display *dpy = XOpenDisplay(NULL);
235 die("Cannot open display");
236 Window win = DefaultRootWindow(dpy);
238 Atom pty = XInternAtom(dpy, "OSD_QUEUE", False);
240 die("Cannot intern OSD_QUEUE atom");
246 die("Cannot fork: %m");
252 XSelectInput(dpy, win, PropertyChangeMask);
253 XDeleteProperty(dpy, win, pty);
256 osd = xosd_create(num_lines);
258 die("Cannot initialize OSD");
259 xosd_set_font(osd, font_name);
260 xosd_set_outline_offset(osd, 2);
261 xosd_set_pos(osd, XOSD_middle);
262 xosd_set_align(osd, XOSD_center);
264 struct pollfd pfd = {
265 .fd = ConnectionNumber(dpy),
271 now = get_current_time();
273 timestamp_t wait_until = now - 1;
274 if (!current_msg && first_msg)
276 current_msg = first_msg;
277 first_msg = first_msg->next;
278 display_msg(current_msg);
283 wait_until = current_msg->min_light;
285 wait_until = current_msg->max_light;
286 if (wait_until <= now)
288 hide_msg(current_msg);
294 DBG("... waiting for %d ms\n", (int)(wait_until - now));
295 poll(&pfd, 1, wait_until - now);
296 if (pfd.revents & POLLIN)
298 while (XPending(dpy))
301 XNextEvent(dpy, &ev);
302 if (ev.type != PropertyNotify)
304 XPropertyEvent *p = &ev.xproperty;
305 if (p->window == win && p->atom == pty)
309 unsigned long pty_items, pty_remains;
310 unsigned char *pty_buf = NULL;
311 XGetWindowProperty(dpy, win, pty, 0, 4096, True, XA_STRING, &pty_type, &pty_fmt, &pty_items, &pty_remains, &pty_buf);
312 if (pty_type == XA_STRING && pty_fmt == 8 && pty_items)
313 parse_input(pty_buf, pty_items);