]> mj.ucw.cz Git - osdd.git/blob - osdd.c
Bits of new display code
[osdd.git] / osdd.c
1 /*
2  *      On-screen Display Daemon
3  *
4  *      (c) 2010--2013 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <poll.h>
12 #include <getopt.h>
13 #include <locale.h>
14 #include <xosd.h>
15 #include <X11/Xlib.h>
16 #include <X11/Xatom.h>
17
18 #undef DEBUG
19 #include "util.h"
20 #include "display.h"
21
22 static struct osd_state *osd;
23
24 static timestamp_t now;
25
26 /*** Options ***/
27
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;
35
36 static const char short_opts[] = "c:d:Df:l:m:o:";
37
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' },
46   { NULL,               0,                      NULL,   0   },
47 };
48
49 static void NONRET
50 usage(void)
51 {
52   fprintf(stderr, "Usage: osdd <options>\n\n\
53 Options:\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\
61 ");
62   exit(1);
63 }
64
65 static void
66 parse_opts(int argc, char **argv)
67 {
68   int opt;
69   while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
70     switch (opt)
71       {
72       case 'c':
73         default_color = optarg;
74         break;
75       case 'd':
76         default_duration = atoi(optarg);
77         break;
78       case 'D':
79         debug_mode = 1;
80         break;
81       case 'f':
82         font_name = optarg;
83         break;
84       case 'l':
85         num_lines = atoi(optarg);
86         if (num_lines < 1)
87           usage();
88         break;
89       case 'm':
90         default_min_duration = atoi(optarg);
91         break;
92       case 'o':
93         default_outline_color = optarg;
94         break;
95       default:
96         usage();
97       }
98
99   if (optind < argc)
100     usage();
101 }
102
103 /*** Displaying of messages ***/
104
105 struct msg {
106   struct msg *next;
107   timestamp_t min_light, max_light;
108   char text[1];
109 };
110
111 static void
112 display_msg(struct msg *msg)
113 {
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;
119
120   char *line = msg->text;
121   while (*line)
122     {
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');
125       *nl++ = 0;
126
127       char *key;
128       char *val = strchr(line, ':');
129       if (val)
130         {
131           key = line;
132           *val++ = 0;
133         }
134       else
135         {
136           key = "";
137           val = line;
138         }
139       DBG("\t%s:%s\n", key, val);
140
141       struct osd_line *l = NULL;
142       if (!key[0])
143         {
144           l = osd_add_line(osd, OSD_TYPE_TEXT);
145           sprintf(l->u.text, "%.*s", OSD_MAX_LINE_LEN, val);
146         }
147       else if (!strcmp(key, "percentage") || !strcmp(key, "percent"))
148         {
149           // FIXME
150           // xosd_display(osd, row++, XOSD_percentage, atoi(val));
151         }
152       else if (!strcmp(key, "slider"))
153         {
154           // FIXME
155           // xsd_display(osd, row++, XOSD_slider, atoi(val));
156         }
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!
165
166       if (l)
167         {
168           l->fg_color = fg_color;
169           l->outline_color = outline_color;
170         }
171
172       line = nl;
173     }
174
175   if (msg->min_light > msg->max_light)
176     msg->min_light = msg->max_light;
177 }
178
179 static void
180 hide_msg(struct msg *msg)
181 {
182   DBG("## Hiding message\n");
183   osd_hide(osd);
184   // FIXME: Reset the osd state
185   free(msg);
186 }
187
188 /*** The message queue ***/
189
190 static struct msg *current_msg, *first_msg, *last_msg;
191
192 static void
193 enqueue_msg(unsigned char *buf, int len)
194 {
195   DBG("Received: [%.*s]\n", len, buf);
196   if (!len || buf[len-1] != '\n')
197     return;
198
199   struct msg *msg = xmalloc(sizeof(*msg) + len);
200   memcpy(msg->text, buf, len);
201   msg->text[len] = 0;
202
203   if (first_msg)
204     last_msg->next = msg;
205   else
206     first_msg = msg;
207   last_msg = msg;
208   msg->next = NULL;
209 }
210
211 static void
212 parse_input(unsigned char *buf, int len)
213 {
214   /* The property might contain several messages concatenated. Split them. */
215   while (len > 0)
216     {
217       if (buf[0] == '\n')
218         {
219           buf++, len--;
220           continue;
221         }
222       int i = 0;
223       while (i < len && (buf[i] != '\n' || (i && buf[i-1] != '\n')))
224         i++;
225       enqueue_msg(buf, i);
226       buf += i, len -= i;
227     }
228 }
229
230 /*** Main loop ***/
231
232 int
233 main(int argc, char **argv)
234 {
235   parse_opts(argc, argv);
236   setlocale(LC_CTYPE, "");
237   XInitThreads();
238
239   Display *dpy = XOpenDisplay(NULL);
240   if (!dpy)
241     die("Cannot open display");
242   Window win = DefaultRootWindow(dpy);
243
244   Atom pty = XInternAtom(dpy, "OSD_QUEUE", False);
245   if (!pty)
246     die("Cannot intern OSD_QUEUE atom");
247
248   if (!debug_mode)
249     {
250       pid_t pid = fork();
251       if (pid < 0)
252         die("Cannot fork: %m");
253       if (pid > 0)
254         return 0;
255       setsid();
256     }
257
258   XSelectInput(dpy, win, PropertyChangeMask);
259   XDeleteProperty(dpy, win, pty);
260   XFlush(dpy);
261
262   osd = osd_new(dpy);
263   osd_set_font(osd, font_name);
264
265   struct pollfd pfd = {
266     .fd = ConnectionNumber(dpy),
267     .events = POLLIN,
268   };
269
270   for (;;)
271     {
272       now = get_current_time();
273
274       timestamp_t wait_until = now - 1;
275       if (!current_msg && first_msg)
276         {
277           current_msg = first_msg;
278           first_msg = first_msg->next;
279           display_msg(current_msg);
280         }
281       if (current_msg)
282         {
283           if (first_msg)
284             wait_until = current_msg->min_light;
285           else
286             wait_until = current_msg->max_light;
287           if (wait_until <= now)
288             {
289               hide_msg(current_msg);
290               current_msg = NULL;
291               continue;
292             }
293         }
294
295       DBG("... waiting for %d ms\n", (int)(wait_until - now));
296       poll(&pfd, 1, wait_until - now);
297       if (pfd.revents & POLLIN)
298         {
299           while (XPending(dpy))
300             {
301               XEvent ev;
302               XNextEvent(dpy, &ev);
303               if (osd_handle_event(osd, &ev))
304                 continue;
305               if (ev.type != PropertyNotify)
306                 continue;
307               XPropertyEvent *p = &ev.xproperty;
308               if (p->window == win && p->atom == pty)
309                 {
310                   Atom pty_type;
311                   int pty_fmt;
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);
317                   if (pty_buf)
318                     XFree(pty_buf);
319                 }
320             }
321         }
322     }
323 }