Debian Codegraph @

librplay/async.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* $Id: async.c,v 1.2 1998/08/13 06:13:35 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1995 Andrew Scherpbier <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include "version.h"
27 #include "rplay.h"
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <memory.h>
37 #ifdef __STDC__
38 # include <stdarg.h>
39 #else
40 # include <varargs.h>
41 #endif
42
•43 typedef void (*cb)(int, int, char *);
44
45 typedef struct obuf_s
46 {
47 struct obuf_s *next;
48 char *data;
49 char *current;
50 int length;
•51 //cb callback;
52 cb (callback);
53 } obuf;
54
55 typedef struct ibuf_s
56 {
57 struct ibuf_s *next;
58 char *data;
59 char *current;
60 int length;
61 } ibuf;
62
63 typedef struct
64 {
65 obuf *olist;
66 obuf *otail;
67 ibuf *ilist;
68 ibuf *itail;
69 int writing;
70 int notify_mask;
•71 cb (rcallback);
72 //cb rcallback;
73 int rraw;
•74 //cb wcallback;
75 cb (wcallback);
76 int wraw;
77 } grp;
78
79
80 static grp group[FD_SETSIZE];
81 static int looping;
82 static int main_loop_return_value;
83
84
85 #ifdef __STDC__
86 static void write_proc(int rptp_fd);
87 static void do_register(int rptp_fd);
88 static void do_unregister(int rptp_fd);
89 static void notify_line(int rptp_fd, char *line);
90 static void read_proc(int rptp_fd);
91 static void process_input(int rptp_fd);
92 #else
93 static void write_proc(/* int rptp_fd */);
94 static void do_register(/* int rptp_fd */);
95 static void do_unregister(/* int rptp_fd */);
96 static void notify_line(/* int rptp_fd, char *line */);
97 static void read_proc(/* int rptp_fd */);
98 static void process_input(/* int rptp_fd */);
99 #endif
100
101
102 /*
103 * Builtin mainloop. Use this if not using another library's event handling routine
104 */
105 int
106 rptp_main_loop()
107 {
108 fd_set read_fds;
109 fd_set write_fds;
110 int i, n;
111
112 looping = 1;
113 main_loop_return_value = 0;
114 while (looping)
115 {
116 FD_ZERO(&read_fds);
117 FD_ZERO(&write_fds);
118
119 for (i = 0; i < FD_SETSIZE; i++)
120 {
121 if (group[i].olist)
122 FD_SET(i, &write_fds);
123 if (group[i].rcallback)
124 FD_SET(i, &read_fds);
125 }
126
127 n = select(FD_SETSIZE, &read_fds, &write_fds, NULL, NULL);
128 if (n < 0)
129 {
130 if (errno == EINTR)
131 continue;
132 return -1;
133 }
134
135 for (i = 0; i < FD_SETSIZE && n; i++)
136 {
137 if (FD_ISSET(i, &read_fds))
138 {
139 n--;
140 if (group[i].rcallback)
141 rptp_async_process(i, RPTP_ASYNC_READ);
142 }
143 if (FD_ISSET(i, &write_fds))
144 {
145 n--;
146 if (group[i].writing)
147 rptp_async_process(i, RPTP_ASYNC_WRITE);
148 }
149 }
150 }
151 return main_loop_return_value;
152 }
153
154
155 /*
156 * Terminate the mainloop at the earliest convenient time. The integer parameter
157 * will be returned by mainloop()
158 */
159 #ifdef __STDC__
160 void
161 rptp_stop_main_loop(int return_value)
162 #else
163 void
164 rptp_stop_main_loop(return_value)
165 int return_value;
166 #endif
167 {
168 looping = 0;
169 main_loop_return_value = return_value;
170 }
171
172
173 /*
174 * Write a line to the RPTP connection asynchronously.
175 *
176 * "\r\n" is appended to the string.
177 */
178 #ifdef __STDC__
179 int
180 rptp_async_putline(int rptp_fd, cb callback, char *fmt, ...)
181 #else
182 int
183 rptp_async_putline(va_alist)
184 va_dcl
185 #endif
186 {
187 va_list args;
188 char buf[RPTP_MAX_LINE];
189
190 #ifdef __STDC__
191 va_start(args, fmt);
192 #else
193 int rptp_fd;
194 char *fmt;
195 cb callback;
196 char *tmp;
197 va_start(args);
198 rptp_fd = va_arg(args, int);
199 tmp = va_arg(args, char *);
200 callback = (cb) tmp;
201 fmt = va_arg(args, char *);
202 #endif
203
204 if (rptp_fd < 0 || rptp_fd >= FD_SETSIZE)
205 {
206 rptp_errno = RPTP_ERROR_SOCKET;
207 return -1;
208 }
209
210 rptp_errno = RPTP_ERROR_NONE;
211
212 vsprintf(buf, fmt, args);
213 va_end(args);
214 strcat(buf, "\r\n");
215
216 return rptp_async_write(rptp_fd, callback, buf, strlen(buf)) != strlen(buf) ? -1 : 0;
217 }
218
219
220 /*
221 * Write nbytes to the RPTP connection asynchronously
222 *
223 * If a callback function is given, it will be called as soon as all
224 * nbytes have been written.
225 */
226 #ifdef __STDC__
227 int
228 rptp_async_write(int rptp_fd, cb callback, char *ptr, int nbytes)
229 #else
230 int
231 rptp_async_write(rptp_fd, callback, ptr, nbytes)
232 int rptp_fd;
233 cb callback;
234 char *ptr;
235 int nbytes;
236 #endif
237 {
238 obuf *new;
239
240 if (rptp_fd < 0 || rptp_fd >= FD_SETSIZE)
241 {
242 rptp_errno = RPTP_ERROR_SOCKET;
243 return -1;
244 }
245
246 /*
247 * Create a new obuf with a copy of the data that needs to be written
248 */
249 new = (obuf *) malloc(sizeof(obuf));
250 new->next = NULL;
251 if (nbytes > 0 && ptr)
252 {
253 new->data = malloc(nbytes);
254 memcpy(new->data, ptr, nbytes);
255 }
256 else /* allow empty data */
257 {
258 new->data = NULL;
259 }
260 new->current = new->data;
261 new->length = nbytes;
262 new->callback = callback;
263
264 /*
265 * Append the obuf to the list of obufs for the given file descriptor
266 */
267 if (group[rptp_fd].otail)
268 {
269 group[rptp_fd].otail->next = new;
270 group[rptp_fd].otail = new;
271 }
272 else
273 {
274 group[rptp_fd].otail = group[rptp_fd].olist = new;
275 }
276
277 /*
278 * If the output system is already writing we don't need to do anything else,
279 * but if not, we need to register our output function
280 */
281 if (!group[rptp_fd].writing)
282 {
283 do_register(rptp_fd);
284 }
285 }
286
287
288 /*
289 * Register a callback for I/O operations on a file descriptor.
290 * There are two modes of operation for this call. The mode
291 * depends on whether you use rptp_main_loop() or not.
292 * Case 1 (using rptp_main_loop()):
293 * Use rptp_async_register to register a callback for reading
294 * or writing to a file descriptor. In this case the
295 * what parameter is one of RPTP_ASYNC_READ or RPTP_ASYNC_WRITE | RPTP_ASYNC_RAW.
296 * A typical prototype for the callback will be something like:
297 * void callback(int fd);
298 * Case 2 (NOT using rptp_main_loop()):
299 * In this case, only RPTP_ASYNC_WRITE should be used. This will register
300 * a callback which will have different parameters than the previous case.
301 * void callback(int rptp_fd, int action);
302 * The action parameter will be one of RPTP_ASYNC_ENABLE or RPTP_ASYNC_DISABLE.
303 * If it is RPTP_ASYNC_ENABLE, the callback is supposed to register the rptp_fd
304 * file descriptor with whatever I/O processing system the application uses.
305 * For RPTP_ASYNC_DISABLE, it should be unregistered.
306 */
307 #ifdef __STDC__
308 void
309 rptp_async_register(int rptp_fd, int what, cb callback)
310 #else
311 void
•312 rptp_async_register(int rptp_fd, int what, cb callback)
313 // int rptp_fd;
314 // int what;
315 // cb callback;
316 #endif
317 {
318 if (rptp_fd < 0 || rptp_fd >= FD_SETSIZE)
319 {
320 rptp_errno = RPTP_ERROR_SOCKET;
321 return;
322 }
323
324 switch (what)
325 {
326 case RPTP_ASYNC_WRITE:
327 group[rptp_fd].wcallback = callback;
328 group[rptp_fd].wraw = 0;
329 break;
330
331 case RPTP_ASYNC_WRITE | RPTP_ASYNC_RAW:
332 group[rptp_fd].wcallback = callback;
333 group[rptp_fd].wraw = 1;
334 break;
335
336 case RPTP_ASYNC_READ:
337 group[rptp_fd].rcallback = callback;
338 group[rptp_fd].rraw = 1;
339 break;
340 }
341 }
342
343
344 /*
345 * This gets called to let us know that some I/O can be preformed.
346 * When rptp_main_loop() is not used, this function needs to be called
347 * when either data is available or data can be written to a file descriptor.
348 * The what parameter is one of RPTP_ASYNC_READ or RPTP_ASYNC_WRITE.
349 * When rptp_main_loop() is used, this function should NEVER be called
350 * by the application code.
351 */
352 #ifdef __STDC__
353 void
354 rptp_async_process(int rptp_fd, int what)
355 #else
356 void
•357 rptp_async_process(int rptp_fd, int what)
358 // int rptp_fd;
359 // int what;
360 #endif
361 {
362 if (rptp_fd < 0 || rptp_fd >= FD_SETSIZE)
363 {
364 rptp_errno = RPTP_ERROR_SOCKET;
365 return;
366 }
367
368 switch (what)
369 {
370 case RPTP_ASYNC_READ:
371 if (group[rptp_fd].rraw && group[rptp_fd].rcallback)
•372 (*group[rptp_fd].rcallback)(rptp_fd, 0, "");
373 else
374 read_proc(rptp_fd);
375 break;
376
377 case RPTP_ASYNC_WRITE:
378 if (group[rptp_fd].wraw && group[rptp_fd].wcallback)
•379 (*group[rptp_fd].wcallback)(rptp_fd, 0, "");
380 else
381 write_proc(rptp_fd);
382 break;
383 }
384 }
385
386
387 /*
388 * Register a callback which will be called when data from a RPTP connection
389 * becomes available.
390 * The mask parameter specifies what kinds of events will be looked for and is
391 * the logical OR of RPTP_EVENT_* constants.
392 * The callback function takes 3 parameters. A typical prototype is something
393 * like:
394 * void callback(int rptp_fd, int event, char *string);
395 */
396 #ifdef __STDC__
397 void
•398 rptp_async_notify(int rptp_fd, int mask, void (*callback)(int, int, char *))
399 #else
400 void
•401 rptp_async_notify(int rptp_fd, int mask, void (*callback)(int, int, char *))
402 // int rptp_fd;
403 // int mask;
404 // void (*callback)();
405 #endif
406 {
407 char command[RPTP_MAX_LINE];
408 char *p;
409
410 strcpy (command, "set notify=");
411
412 if (mask & RPTP_EVENT_CONTINUE)
413 strcat(command, "continue,");
414 if (mask & RPTP_EVENT_DONE)
415 strcat(command, "done,");
416 if (mask & RPTP_EVENT_PAUSE)
417 strcat(command, "pause,");
418 if (mask & RPTP_EVENT_PLAY)
419 strcat(command, "play,");
420 if (mask & RPTP_EVENT_SKIP)
421 strcat(command, "skip,");
422 if (mask & RPTP_EVENT_STATE)
423 strcat(command, "state,");
424 if (mask & RPTP_EVENT_STOP)
425 strcat(command, "stop,");
426 if (mask & RPTP_EVENT_VOLUME)
427 strcat(command, "volume,");
428 if (mask & RPTP_EVENT_FLOW)
429 strcat(command, "flow,");
430 if (mask & RPTP_EVENT_MODIFY)
431 strcat(command, "modify,");
432 if (mask & RPTP_EVENT_LEVEL)
433 strcat(command, "level,");
434 if (mask & RPTP_EVENT_POSITION)
435 strcat(command, "position,");
436
437 p = strrchr(command, ',');
438 if (!p)
439 strcat(command, "none");
440 else
441 *p = '\0';
442
443 rptp_async_putline(rptp_fd, NULL, command);
444
445 group[rptp_fd].rcallback = callback;
446 group[rptp_fd].notify_mask = mask;
447 group[rptp_fd].rraw = 0;
448 }
449
450
451 /*-------------------------------------------------------------------------
452 * Start of static routines. All functions below are only utility routines
453 * for this module
454 */
455
456 /*
457 * Attempt to write data from the buffers to an fd.
458 */
459 #ifdef __STDC__
460 static void
461 write_proc(int rptp_fd)
462 #else
463 static void
•464 write_proc(int rptp_fd)
465 #endif
466 {
467 obuf *current;
468 int n;
469
470 if (!group[rptp_fd].olist)
471 {
472 do_unregister(rptp_fd);
473 }
474
475 current = group[rptp_fd].olist;
476 if (current->current)
477 {
478 n = write(rptp_fd, current->current, current->length);
479 if (n < 0)
480 {
481 return;
482 }
483 }
484 else
485 {
486 n = 0;
487 }
488 current->length -= n;
489 current->current += n;
490 if (current->length <= 0)
491 {
492 if (current->callback)
493 {
•494 (*current->callback)(rptp_fd, 0, "");
495 }
496 if (current->data)
497 {
498 free(current->data);
499 }
500 group[rptp_fd].olist = current->next;
501 if (group[rptp_fd].otail == current)
502 {
503 group[rptp_fd].otail = NULL;
504 }
505 free(current);
506
507 if (!group[rptp_fd].olist)
508 {
509 do_unregister(rptp_fd);
510 }
511 }
512 }
513
514
515 /*
516 * Attempt to read data from an fd to the buffer.
517 */
518 #ifdef __STDC__
519 static void
520 read_proc(int rptp_fd)
521 #else
522 static void
•523 read_proc(int rptp_fd)
524 #endif
525 {
526 char buffer[RPTP_MAX_LINE];
527 int n;
528 ibuf *new;
529
530 n = read(rptp_fd, buffer, sizeof(buffer));
531 if (n <= 0)
532 {
533 if (group[rptp_fd].notify_mask & RPTP_EVENT_CLOSE && group[rptp_fd].rcallback)
534 {
535 (*group[rptp_fd].rcallback)(rptp_fd, RPTP_EVENT_CLOSE, "");
536 group[rptp_fd].rcallback = NULL;
537 }
538 return; /* Don't do anything when we get an error. We'll get it later */
539 }
540
541 new = (ibuf *) malloc(sizeof(ibuf));
542 new->next = NULL;
543 new->data = malloc(n);
544 memcpy(new->data, buffer, n);
545 new->length = n;
546 new->current = new->data;
547
548 if (group[rptp_fd].itail)
549 {
550 group[rptp_fd].itail->next = new;
551 group[rptp_fd].itail = new;
552 }
553 else
554 {
555 group[rptp_fd].itail = group[rptp_fd].ilist = new;
556 }
557
558 process_input(rptp_fd);
559 }
560
561
562 /*
563 * Attempt to make complete lines from the input buffers for the given
564 * file descriptor.
565 */
566 #ifdef __STDC__
567 static void
568 process_input(int rptp_fd)
569 #else
570 static void
•571 process_input(int rptp_fd)
572 #endif
573 {
574 char *p;
575 ibuf *ptr;
576 static char line[RPTP_MAX_LINE * 2] = "";
577 static int length = 0;
578
579 while ((ptr = group[rptp_fd].ilist))
580 {
581 if (p = memchr(ptr->current, '\r', ptr->length))
582 {
583 memcpy(&line[length], ptr->current, p - ptr->current);
584 length += p - ptr->current;
585 line[length] = '\0';
586 notify_line(rptp_fd, line);
587 length = 0;
588
589 p+= 2;
590 if (p < ptr->current + ptr->length)
591 {
592 /*
593 * There is still unused data in this block.
594 */
595 ptr->length -= p - ptr->current;
596 ptr->current = p;
597 }
598 else
599 {
600 free(ptr->data);
601 if (ptr == group[rptp_fd].itail)
602 {
603 group[rptp_fd].ilist = group[rptp_fd].itail = NULL;
604 }
605 else
606 {
607 group[rptp_fd].ilist = ptr->next;
608 }
609 free(ptr);
610 }
611 }
612 else
613 {
614 /*
615 * The current block of data doesn't contain any carriage
616 * returns. We'll just add it to our line and get rid of it.
617 */
618 memcpy(&line[length], ptr->current, ptr->length);
619 length += ptr->length;
620 free(ptr->data);
621 if (ptr == group[rptp_fd].itail)
622 {
623 group[rptp_fd].ilist = group[rptp_fd].itail = NULL;
624 }
625 else
626 {
627 group[rptp_fd].ilist = ptr->next;
628 }
629 free(ptr);
630 }
631 }
632 }
633
634
635 /*
636 * A mapping between event and event mask
637 */
638 static struct
639 {
640 char *event;
641 int mask;
642 } events[] =
643 {
644 {"continue", RPTP_EVENT_CONTINUE},
645 {"done", RPTP_EVENT_DONE},
646 {"pause", RPTP_EVENT_PAUSE},
647 {"play", RPTP_EVENT_PLAY},
648 {"skip", RPTP_EVENT_SKIP},
649 {"state", RPTP_EVENT_STATE},
650 {"stop", RPTP_EVENT_STOP},
651 {"volume", RPTP_EVENT_VOLUME},
652 {"flow", RPTP_EVENT_FLOW},
653 {"modify", RPTP_EVENT_MODIFY},
654 {"level", RPTP_EVENT_LEVEL},
655 {"position", RPTP_EVENT_POSITION},
656 {NULL, 0}
657 };
658
659
660 /*
661 * Tell the application that a line has arrived from a connection
662 */
663 #ifdef __STDC__
664 static void
665 notify_line(int rptp_fd, char *line)
666 #else
667 static void
•668 notify_line(int rptp_fd, char *line)
669 #endif
670 {
671 int what;
672 char *event;
673 int i;
674
675 /*
676 * Make sure that there is a callback registered. No use doing
677 * anything without one...
678 */
679 if (!group[rptp_fd].rcallback)
680 return;
681
682 switch (*line)
683 {
684 case '+':
685 what = RPTP_EVENT_OK;
686 break;
687
688 case '-':
689 what = RPTP_EVENT_ERROR;
690 break;
691
692 case '!':
693 what = RPTP_EVENT_TIMEOUT;
694 break;
695
696 case '@':
697 event = rptp_parse(line, "event");
698 what = RPTP_EVENT_OTHER;
699 for (i = 0; events[i].event; i++)
700 {
701 if (strcmp(events[i].event, event) == 0)
702 {
703 what = events[i].mask;
704 break;
705 }
706 }
707 break;
708
709 default:
710 what = RPTP_EVENT_OTHER;
711 break;
712 }
713 if (what & group[rptp_fd].notify_mask)
714 {
715 (*group[rptp_fd].rcallback)(rptp_fd, what, line);
716 }
717 #if 0
718 else
719 {
720 printf("Input line being ignored: '%s'\n", line);
721 }
722 #endif
723 }
724
725
726 /*
727 * Register us with whatever controls the write_fds on the select
728 */
729 #ifdef __STDC__
730 static void
731 do_register(int rptp_fd)
732 #else
733 static void
•734 do_register(int rptp_fd)
735 #endif
736 {
737 if (group[rptp_fd].wcallback)
738 {
•739 (*group[rptp_fd].wcallback)(rptp_fd, RPTP_ASYNC_ENABLE, "");
740 }
741 group[rptp_fd].writing = 1;
742 }
743
744
745 /*
746 * Unregister us with whatever controls the write_fds on the select
747 */
748 #ifdef __STDC__
749 static void
750 do_unregister(int rptp_fd)
751 #else
752 static void
•753 do_unregister(int rptp_fd)
754 #endif
755 {
756 if (group[rptp_fd].wcallback)
757 {
•758 (*group[rptp_fd].wcallback)(rptp_fd, RPTP_ASYNC_DISABLE, "");
759 }
760 group[rptp_fd].writing = 0;
761 }
762
763
764 /*
765 * Local variables:
766 * tab-width: 4
767 * End:
768 */