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 int num_lines = 4; // FIXME
29 static char *font_name = "-bitstream-bitstream vera sans-bold-r-normal-*-*-320-*-*-p-*-*"; // FIXME
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 char *fg_color = default_color;
118 char *outline_color = default_outline_color;
120 char *line = msg->text;
123 // The parser it destructive, but it does not do any harm, since we display each message only once.
124 char *nl = strchr(line, '\n');
128 char *val = strchr(line, ':');
139 DBG("\t%s:%s\n", key, val);
141 struct osd_line *l = NULL;
144 l = osd_add_line(osd, OSD_TYPE_TEXT);
145 sprintf(l->u.text, "%.*s", OSD_MAX_LINE_LEN, 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 // xsd_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 fg_color = val; // FIXME: Need copying!
163 else if (!strcmp(key, "outline-color"))
164 outline_color = val; // FIXME: Need copying!
168 l->fg_color = fg_color;
169 l->outline_color = outline_color;
175 if (msg->min_light > msg->max_light)
176 msg->min_light = msg->max_light;
180 hide_msg(struct msg *msg)
182 DBG("## Hiding message\n");
184 // FIXME: Reset the osd state
188 /*** The message queue ***/
190 static struct msg *current_msg, *first_msg, *last_msg;
193 enqueue_msg(unsigned char *buf, int len)
195 DBG("Received: [%.*s]\n", len, buf);
196 if (!len || buf[len-1] != '\n')
199 struct msg *msg = xmalloc(sizeof(*msg) + len);
200 memcpy(msg->text, buf, len);
204 last_msg->next = msg;
212 parse_input(unsigned char *buf, int len)
214 /* The property might contain several messages concatenated. Split them. */
223 while (i < len && (buf[i] != '\n' || (i && buf[i-1] != '\n')))
233 main(int argc, char **argv)
235 parse_opts(argc, argv);
236 setlocale(LC_CTYPE, "");
239 Display *dpy = XOpenDisplay(NULL);
241 die("Cannot open display");
242 Window win = DefaultRootWindow(dpy);
244 Atom pty = XInternAtom(dpy, "OSD_QUEUE", False);
246 die("Cannot intern OSD_QUEUE atom");
252 die("Cannot fork: %m");
258 XSelectInput(dpy, win, PropertyChangeMask);
259 XDeleteProperty(dpy, win, pty);
263 osd_set_font(osd, font_name);
265 struct pollfd pfd = {
266 .fd = ConnectionNumber(dpy),
272 now = get_current_time();
274 timestamp_t wait_until = now - 1;
275 if (!current_msg && first_msg)
277 current_msg = first_msg;
278 first_msg = first_msg->next;
279 display_msg(current_msg);
284 wait_until = current_msg->min_light;
286 wait_until = current_msg->max_light;
287 if (wait_until <= now)
289 hide_msg(current_msg);
295 DBG("... waiting for %d ms\n", (int)(wait_until - now));
296 poll(&pfd, 1, wait_until - now);
297 if (pfd.revents & POLLIN)
299 while (XPending(dpy))
302 XNextEvent(dpy, &ev);
303 if (osd_handle_event(osd, &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);