2 * On-screen Display Daemon
4 * (c) 2010--2013 Martin Mares <mj@ucw.cz>
16 #include <X11/Xatom.h>
22 static struct osd_state *osd;
24 static timestamp_t now;
28 static char *font_name = "times-64";
29 static char *default_color = "green";
30 static char *default_outline_color = "black";
31 static int default_outline_width = 2;
32 static int default_duration = 1000;
33 static int default_min_duration = 250;
34 static int debug_mode;
36 static double line_spacing = 0.2;
38 static const char short_opts[] = "c:d:Df:m:o:O:s:";
44 static const struct option long_opts[] = {
45 { "color", required_argument, NULL, 'c' },
46 { "debug", no_argument, NULL, 'D' },
47 { "duration", required_argument, NULL, 'd' },
48 { "font", required_argument, NULL, 'f' },
49 { "min-duration", required_argument, NULL, 'm' },
50 { "outline-color", required_argument, NULL, 'o' },
51 { "outline-width", required_argument, NULL, 'O' },
52 { "line-spacing", required_argument, NULL, 's' },
53 { "test", no_argument, NULL, OPT_TEST }, // Undocumented test mode
60 fprintf(stderr, "Usage: osdd <options>\n\n\
62 -c, --color=<c>\t\tDefault color (#rgb, #rrggbb or a name from rgb.txt)\n\
63 -D, --debug\t\tDebugging mode (do not detach from the terminal)\n\
64 -d, --duration=<ms>\tDefault message duration in milliseconds\n\
65 -f, --font=<f>\t\tFont to use for the OSD\n\
66 -m, --min-duration=<ms>\tDefault minimum message duration in milliseconds\n\
67 -o, --outline-color=<c>\tDefault outline color\n\
68 -O, --outline-width=<n>\tDefault outline width (default=2)\n\
69 -s, --line-spacing=<n>\tSet line spacing factor (decimal fraction, default=0.2)\n\
75 parse_opts(int argc, char **argv)
78 while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
82 default_color = optarg;
85 default_duration = atoi(optarg);
94 line_spacing = atof(optarg);
97 default_min_duration = atoi(optarg);
100 default_outline_color = optarg;
103 default_outline_width = atoi(optarg);
117 /*** Displaying of messages ***/
121 timestamp_t min_light, max_light;
126 display_msg(struct msg *msg)
128 DBG("## Displaying message\n");
129 msg->min_light = now + default_min_duration;
130 msg->max_light = now + default_duration;
131 char *fg_color = default_color;
132 char *outline_color = default_outline_color;
133 int outline_width = default_outline_width;
135 char *line = msg->text;
138 // The parser it destructive, but it does not do any harm, since we display each message only once.
139 char *nl = strchr(line, '\n');
143 char *val = strchr(line, ':');
154 DBG("\t%s:%s\n", key, val);
156 struct osd_line *l = NULL;
159 l = osd_add_line(osd, OSD_TYPE_TEXT);
160 sprintf(l->u.text, "%.*s", OSD_MAX_LINE_LEN, val);
162 else if (!strcmp(key, "percentage") || !strcmp(key, "percent"))
165 // xosd_display(osd, row++, XOSD_percentage, atoi(val));
167 else if (!strcmp(key, "slider"))
170 // xosd_display(osd, row++, XOSD_slider, atoi(val));
172 else if (!strcmp(key, "duration"))
173 msg->max_light = now + atoi(val);
174 else if (!strcmp(key, "min-duration"))
175 msg->min_light = now + atoi(val);
176 else if (!strcmp(key, "color"))
178 else if (!strcmp(key, "outline-color"))
180 else if (!strcmp(key, "outline-width"))
181 outline_width = atoi(val);
185 l->fg_color = fg_color;
186 l->outline_color = outline_color;
187 l->outline_width = outline_width;
193 if (msg->min_light > msg->max_light)
194 msg->min_light = msg->max_light;
200 hide_msg(struct msg *msg)
202 DBG("## Hiding message\n");
207 /*** The message queue ***/
209 static struct msg *current_msg, *first_msg, *last_msg;
212 enqueue_msg(unsigned char *buf, int len)
214 DBG("Received: [%.*s]\n", len, buf);
215 if (!len || buf[len-1] != '\n')
218 struct msg *msg = xmalloc(sizeof(*msg) + len);
219 memcpy(msg->text, buf, len);
223 last_msg->next = msg;
231 parse_input(unsigned char *buf, int len)
233 /* The property might contain several messages concatenated. Split them. */
242 while (i < len && (buf[i] != '\n' || (i && buf[i-1] != '\n')))
252 unsigned char buf[4096];
256 while ((c = read(0, buf + len, 4096 - len)) > 0)
259 enqueue_msg(buf, len);
265 main(int argc, char **argv)
267 parse_opts(argc, argv);
268 setlocale(LC_CTYPE, "");
271 Display *dpy = XOpenDisplay(NULL);
273 die("Cannot open display");
274 Window win = DefaultRootWindow(dpy);
276 Atom pty = XInternAtom(dpy, "OSD_QUEUE", False);
278 die("Cannot intern OSD_QUEUE atom");
284 die("Cannot fork: %m");
297 XSelectInput(dpy, win, PropertyChangeMask);
298 XDeleteProperty(dpy, win, pty);
303 osd_set_font(osd, font_name, line_spacing);
305 struct pollfd pfd = {
306 .fd = ConnectionNumber(dpy),
312 now = get_current_time();
314 timestamp_t wait_until = now - 1;
315 if (!current_msg && first_msg)
317 current_msg = first_msg;
318 first_msg = first_msg->next;
319 display_msg(current_msg);
324 wait_until = current_msg->min_light;
326 wait_until = current_msg->max_light;
327 if (wait_until <= now)
329 hide_msg(current_msg);
334 if (test_mode && !current_msg)
337 DBG("... waiting for %d ms\n", (int)(wait_until - now));
338 poll(&pfd, 1, wait_until - now);
339 if (pfd.revents & POLLIN)
341 while (XPending(dpy))
344 XNextEvent(dpy, &ev);
345 if (osd_handle_event(osd, &ev))
347 if (ev.type != PropertyNotify)
349 XPropertyEvent *p = &ev.xproperty;
350 if (p->window == win && p->atom == pty)
354 unsigned long pty_items, pty_remains;
355 unsigned char *pty_buf = NULL;
356 XGetWindowProperty(dpy, win, pty, 0, 4096, True, XA_STRING, &pty_type, &pty_fmt, &pty_items, &pty_remains, &pty_buf);
357 if (pty_type == XA_STRING && pty_fmt == 8 && pty_items)
358 parse_input(pty_buf, pty_items);