chiark / gitweb /
update control buttons when disconnection detected
[disorder] / driver / disorder.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2005, 2007 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 /** @file driver/disorder.c
21  * @brief libao driver used by DisOrder
22  *
23  * The output from this driver is expected to be fed to @c
24  * disorder-normalize to convert to the confnigured target format.
25  */
26
27 #include <config.h>
28 #include "types.h"
29
30 #include <string.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <poll.h>
35 #include <ao/ao.h>
36 #include <ao/plugin.h>
37
38 #include "speaker-protocol.h"
39
40 /* extra declarations to help out lazy <ao/plugin.h> */
41 int ao_plugin_test(void);
42 ao_info *ao_plugin_driver_info(void);
43 char *ao_plugin_file_extension(void);
44
45 /** @brief Private data structure for this driver */
46 struct internal {
47   int fd;                               /* output file descriptor */
48   int exit_on_error;                    /* exit on write error */
49
50   /** @brief Record of sample format */
51   struct stream_header header;
52
53 };
54
55 /* like write() but never returns EINTR/EAGAIN or short */
56 static int do_write(int fd, const void *ptr, size_t n) {
57   size_t written = 0;
58   int ret;
59   struct pollfd ufd;
60
61   memset(&ufd, 0, sizeof ufd);
62   ufd.fd = fd;
63   ufd.events = POLLOUT;
64   while(written < n) {
65     ret = write(fd, (const char *)ptr + written, n - written);
66     if(ret < 0) {
67       switch(errno) {
68       case EINTR: break;
69       case EAGAIN:
70         /* Someone sneakily gave us a nonblocking file descriptor, wait until
71          * we can write again */
72         ret = poll(&ufd, 1, -1);
73         if(ret < 0 && errno != EINTR) return -1;
74         break;
75       default:
76         return -1;
77       }
78     } else
79       written += ret;
80   }
81   return written;
82 }
83
84 /* return 1 if this driver can be opened */
85 int ao_plugin_test(void) {
86   return 1;
87 }
88
89 /* return info about this driver */
90 ao_info *ao_plugin_driver_info(void) {
91   static const char *options[] = { "fd" };
92   static const ao_info info = {
93     AO_TYPE_LIVE,                       /* type */
94     (char *)"DisOrder format driver",   /* name */
95     (char *)"disorder",                 /* short_name */
96     (char *)"http://www.greenend.org.uk/rjk/disorder/", /* comment */
97     (char *)"Richard Kettlewell",       /* author */
98     AO_FMT_NATIVE,                      /* preferred_byte_format */
99     0,                                  /* priority */
100     (char **)options,                   /* options */
101     1,                                  /* option_count */
102   };
103   return (ao_info *)&info;
104 }
105
106 /* initialize the private data structure */
107 int ao_plugin_device_init(ao_device *device) {
108   struct internal *i = malloc(sizeof (struct internal));
109   const char *e;
110
111   if(!i) return 0;
112   memset(i, 0, sizeof *i);
113   if((e = getenv("DISORDER_RAW_FD")))
114     i->fd = atoi(e);
115   else
116     i->fd = 1;
117   device->internal = i;
118   return 1;
119 }
120
121 /* set an option */
122 int ao_plugin_set_option(ao_device *device,
123                          const char *key,
124                          const char *value) {
125   struct internal *i = device->internal;
126
127   if(!strcmp(key, "fd"))
128     i->fd = atoi(value);
129   else if(!strcmp(key, "fragile"))
130     i->exit_on_error = atoi(value);
131   /* unknown options are required to be ignored */
132   return 1;
133 }
134
135 /* open the device */
136 int ao_plugin_open(ao_device *device, ao_sample_format *format) {
137   struct internal *i = device->internal;
138   
139   /* we would like native-order samples */
140   device->driver_byte_format = AO_FMT_NATIVE;
141   i->header.rate = format->rate;
142   i->header.channels = format->channels;
143   i->header.bits = format->bits;
144   i->header.endian = ENDIAN_NATIVE;
145   return 1;
146 }
147
148 /* play some samples */
149 int ao_plugin_play(ao_device *device, const char *output_samples, 
150                    uint_32 num_bytes) {
151   struct internal *i = device->internal;
152
153   /* Fill in and write the header */
154   i->header.nbytes = num_bytes;
155   if(do_write(i->fd, &i->header, sizeof i->header) < 0) {
156     if(i->exit_on_error) _exit(-1);
157     return 0;
158   }
159
160   /* Write the sample data */
161   if(do_write(i->fd, output_samples, num_bytes) < 0) {
162     if(i->exit_on_error) _exit(-1);
163     return 0;
164   }
165   return 1;
166 }
167
168 /* close the device */
169 int ao_plugin_close(ao_device attribute((unused)) *device) {
170   return 1;
171 }
172
173 /* delete private data structures */
174 void ao_plugin_device_clear(ao_device *device) {
175   free(device->internal);
176   device->internal = 0;
177 }
178
179 /* report preferred filename extension */
180 char *ao_plugin_file_extension(void) {
181   return 0;
182 }
183
184 /*
185 Local Variables:
186 c-basic-offset:2
187 comment-column:40
188 End:
189 */