sad

simple audio daemon
git clone git@git.2f30.org/sad.git
Log | Files | Refs | LICENSE

fifo.c (1055B)


      1 #include <sys/select.h>
      2 #include <sys/stat.h>
      3 #include <sys/types.h>
      4 
      5 #include <err.h>
      6 #include <fcntl.h>
      7 #include <limits.h>
      8 #include <stdio.h>
      9 #include <unistd.h>
     10 
     11 #include "sad.h"
     12 
     13 static int fifofd = -1;
     14 
     15 static int
     16 fifovol(int vol)
     17 {
     18 	(void)vol;
     19 
     20 	return 0;
     21 }
     22 
     23 static int
     24 fifoopen(Format *fmt)
     25 {
     26 	(void)fmt;
     27 
     28 	unlink("/tmp/sad-fifo");
     29 	if (mkfifo("/tmp/sad-fifo", 0644) < 0) {
     30 		warn("mkfifo /tmp/sad-fifo");
     31 		return -1;
     32 	}
     33 
     34 	fifofd = open("/tmp/sad-fifo", O_RDWR | O_NONBLOCK);
     35 	if (fifofd < 0) {
     36 		warn("open /tmp/sad-fifo");
     37 		return -1;
     38 	}
     39 	return 0;
     40 }
     41 
     42 static int
     43 fifoplay(void *buf, size_t nbytes)
     44 {
     45 	ssize_t n, wrote;
     46 	char    *p = buf;
     47 
     48 	wrote = 0;
     49 	while (nbytes > 0) {
     50 		n = write(fifofd, &p[wrote], nbytes);
     51 		if (n < 0)
     52 			return nbytes; /* ignore errors */
     53 		else if (n == 0)
     54 			break;
     55 		wrote += n;
     56 		nbytes -= n;
     57 	}
     58 	return wrote;
     59 }
     60 
     61 static int
     62 fifoclose(void)
     63 {
     64 	if (fifofd != -1) {
     65 		close(fifofd);
     66 		fifofd = -1;
     67 	}
     68 	return 0;
     69 }
     70 
     71 Output fifooutput = {
     72 	.vol = fifovol,
     73 	.open = fifoopen,
     74 	.play = fifoplay,
     75 	.close = fifoclose,
     76 };