cmus-remote: Read command results
[cmus.git] / sun.c
blobf48166be3978e249e87b246e5f4ff68eb48f3c66
1 /*
2 * Copyright 2004-2005 Timo Hirvonen
4 * sun.c by alex <pukpuk@gmx.de>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
22 #include <sys/types.h>
23 #include <sys/ioctl.h>
24 #include <sys/audioio.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include "debug.h"
31 #include "op.h"
32 #include "sf.h"
33 #include "xmalloc.h"
35 static sample_format_t sun_sf;
36 static int sun_fd = -1;
38 static char *sun_audio_device = NULL;
40 static int sun_reset(void);
41 static int sun_set_sf(sample_format_t);
42 static int sun_device_exists(const char *);
43 static int sun_init(void);
44 static int sun_exit(void);
45 static int sun_open(sample_format_t);
46 static int sun_close(void);
47 static int sun_write(const char *, int);
48 static int sun_pause(void);
49 static int sun_unpause(void);
50 static int sun_buffer_space(void);
51 static int op_sun_set_option(int, const char *);
52 static int op_sun_get_option(int, char **);
54 static int sun_reset(void)
56 if (ioctl(sun_fd, AUDIO_FLUSH, NULL) == -1)
57 return -1;
59 return 0;
62 static int sun_set_sf(sample_format_t sf)
64 struct audio_info ainf;
66 AUDIO_INITINFO(&ainf);
68 sun_reset();
69 sun_sf = sf;
71 ainf.play.channels = sf_get_channels(sun_sf);
72 ainf.play.sample_rate = sf_get_rate(sun_sf);
73 ainf.play.pause = 0;
74 ainf.mode = AUMODE_PLAY;
76 switch (sf_get_bits(sun_sf)) {
77 case 16:
78 ainf.play.precision = 16;
79 if (sf_get_signed(sun_sf)) {
80 if (sf_get_bigendian(sun_sf))
81 ainf.play.encoding = AUDIO_ENCODING_SLINEAR_BE;
82 else
83 ainf.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
84 } else {
85 if (sf_get_bigendian(sun_sf))
86 ainf.play.encoding = AUDIO_ENCODING_ULINEAR_BE;
87 else
88 ainf.play.encoding = AUDIO_ENCODING_ULINEAR_LE;
90 break;
91 case 8:
92 ainf.play.precision = 8;
93 if (sf_get_signed(sun_sf))
94 ainf.play.encoding = AUDIO_ENCODING_SLINEAR;
95 else
96 ainf.play.encoding = AUDIO_ENCODING_ULINEAR;
97 break;
98 default:
99 d_print("sun: bits is neither 8 nor 16\n");
100 return -1;
103 if (ioctl(sun_fd, AUDIO_SETINFO, &ainf) == -1) {
104 d_print("sun: AUDIO_SETINFO failed: %s", strerror(errno));
105 return -1;
108 if (ioctl(sun_fd, AUDIO_GETINFO, &ainf) == -1)
109 return -1;
110 /* XXX: check if sample rate is supported */
111 d_print("sun: sample_rate: %d chanels: %d\n", ainf.play.sample_rate,
112 ainf.play.channels);
114 return 0;
117 static int sun_device_exists(const char *dev)
119 struct stat s;
121 if (stat(dev, &s) == 0) {
122 d_print("device %s exists\n", dev);
123 return 1;
125 d_print("device %s does not exist\n", dev);
127 return 0;
130 static int sun_init(void)
132 const char *audio_dev = "/dev/audio";
134 if (sun_audio_device != NULL) {
135 if (sun_device_exists(sun_audio_device))
136 return 0;
137 free(sun_audio_device);
138 sun_audio_device = NULL;
139 return -1;
141 if (sun_device_exists(audio_dev)) {
142 sun_audio_device = xstrdup(audio_dev);
143 return 0;
146 return -1;
149 static int sun_exit(void)
151 if (sun_audio_device != NULL) {
152 free(sun_audio_device);
153 sun_audio_device = NULL;
156 return 0;
159 static int sun_open(sample_format_t sf)
161 sun_fd = open(sun_audio_device, O_WRONLY);
162 if (sun_fd == -1)
163 return -1;
164 if (sun_set_sf(sf) == -1) {
165 sun_close();
166 return -1;
169 return 0;
172 static int sun_close(void)
174 if (sun_fd != -1) {
175 close(sun_fd);
176 sun_fd = -1;
179 return 0;
182 static int sun_write(const char *buf, int cnt)
184 int rc;
185 const char *t;
187 cnt /= 4;
188 cnt *= 4;
189 t = buf;
190 while (cnt > 0) {
191 if ((rc = write(sun_fd, buf, cnt)) == -1) {
192 if (errno == EINTR)
193 continue;
194 else
195 return rc;
197 buf += rc;
198 cnt -= rc;
201 return (buf - t);
204 static int sun_pause(void)
206 struct audio_info ainf;
208 AUDIO_INITINFO(&ainf);
210 ainf.play.pause = 1;
211 if (ioctl(sun_fd, AUDIO_SETINFO, &ainf) == -1)
212 return -1;
214 return 0;
217 static int sun_unpause(void)
219 struct audio_info ainf;
221 AUDIO_INITINFO(&ainf);
223 ainf.play.pause = 0;
224 if (ioctl(sun_fd, AUDIO_SETINFO, &ainf) == -1)
225 return -1;
227 return 0;
230 static int sun_buffer_space(void)
232 struct audio_info ainf;
233 int sp;
235 AUDIO_INITINFO(&ainf);
237 if (ioctl(sun_fd, AUDIO_GETINFO, &ainf) == -1)
238 return -1;
239 sp = ainf.play.buffer_size;
241 return sp;
244 static int op_sun_set_option(int key, const char *val)
246 switch (key) {
247 case 0:
248 free(sun_audio_device);
249 sun_audio_device = xstrdup(val);
250 break;
251 default:
252 return -OP_ERROR_NOT_OPTION;
255 return 0;
258 static int op_sun_get_option(int key, char **val)
260 switch (key) {
261 case 0:
262 if (sun_audio_device)
263 *val = xstrdup(sun_audio_device);
264 break;
265 default:
266 return -OP_ERROR_NOT_OPTION;
269 return 0;
272 const struct output_plugin_ops op_pcm_ops = {
273 .init = sun_init,
274 .exit = sun_exit,
275 .open = sun_open,
276 .close = sun_close,
277 .write = sun_write,
278 .pause = sun_pause,
279 .unpause = sun_unpause,
280 .buffer_space = sun_buffer_space,
281 .set_option = op_sun_set_option,
282 .get_option = op_sun_get_option
285 const char * const op_pcm_options[] = {
286 "device",
287 NULL
290 const int op_priority = 0;