chiark / gitweb /
Add 'adopt' command. This adopts a randomly picked track by changing
[disorder] / lib / mixer-oss.c
CommitLineData
bd8895a8 1/*
2 * This file is part of DisOrder
a35bb7cc 3 * Copyright (C) 2004, 2007, 2008 Richard Kettlewell
bd8895a8 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
bd8895a8 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
bd8895a8 8 * (at your option) any later version.
e7eb3a27
RK
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
bd8895a8 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
bd8895a8 17 */
b25aac59 18/** @file lib/mixer-oss.c
19 * @brief OSS mixer support
20 *
21 * Mono output devices aren't explicitly supported (but may work
22 * nonetheless).
23 */
bd8895a8 24
05b75f8d 25#include "common.h"
b25aac59 26
27#if HAVE_SYS_SOUNDCARD_H
28
bd8895a8 29#include <fcntl.h>
30#include <unistd.h>
bd8895a8 31#include <errno.h>
32#include <stddef.h>
33#include <sys/ioctl.h>
b25aac59 34#include <sys/soundcard.h>
bd8895a8 35
36#include "configuration.h"
37#include "mixer.h"
38#include "log.h"
39#include "syscalls.h"
a35bb7cc 40#include "mem.h"
bd8895a8 41
bd8895a8 42/* documentation does not match implementation! */
43#ifndef SOUND_MIXER_READ
44# define SOUND_MIXER_READ(x) MIXER_READ(x)
45#endif
46#ifndef SOUND_MIXER_WRITE
47# define SOUND_MIXER_WRITE(x) MIXER_WRITE(x)
48#endif
49
b25aac59 50/** @brief Channel names */
bd8895a8 51static const char *channels[] = SOUND_DEVICE_NAMES;
52
b25aac59 53/** @brief Convert channel name to number */
bd8895a8 54static int mixer_channel(const char *c) {
55 unsigned n;
56
57 if(!c[strspn(c, "0123456789")])
58 return atoi(c);
59 else {
60 for(n = 0; n < sizeof channels / sizeof *channels; ++n)
61 if(!strcmp(channels[n], c))
62 return n;
63 return -1;
64 }
65}
66
b25aac59 67/** @brief Open the OSS mixer device and return its fd */
bd8895a8 68static int oss_do_open(void) {
69 int fd;
70
a35bb7cc
RK
71 if((fd = open(config->mixer, O_RDWR, 0)) < 0) {
72 static char *reported;
73
74 if(!reported || strcmp(reported, config->mixer)) {
75 if(reported)
76 xfree(reported);
77 reported = xstrdup(config->mixer);
78 error(errno, "error opening %s", config->mixer);
79 }
80 }
bd8895a8 81 return fd;
82}
83
b25aac59 84/** @brief Get the OSS mixer setting */
bd8895a8 85static int oss_do_get(int *left, int *right, int fd, int ch) {
86 int r;
87
88 if(ioctl(fd, SOUND_MIXER_READ(ch), &r) == -1) {
89 error(errno, "error reading %s channel %s",
90 config->mixer, config->channel);
91 return -1;
92 }
93 *left = r & 0xff;
94 *right = (r >> 8) & 0xff;
95 return 0;
96}
97
b25aac59 98/** @brief Get OSS volume */
bd8895a8 99static int oss_get(int *left, int *right) {
100 int ch, fd;
101
102 if(config->mixer
103 && config->channel
104 && (ch = mixer_channel(config->channel)) != -1) {
105 if((fd = oss_do_open()) < 0)
106 return -1;
107 if(oss_do_get(left, right, fd, ch) < 0) {
108 xclose(fd);
109 return -1;
110 }
111 xclose(fd);
112 return 0;
113 } else
114 return -1;
115}
116
b25aac59 117/** @brief Set OSS volume */
bd8895a8 118static int oss_set(int *left, int *right) {
119 int ch, fd, r;
120
121 if(config->mixer
122 && config->channel
123 && (ch = mixer_channel(config->channel)) != -1) {
124 if((fd = oss_do_open()) < 0)
125 return -1;
126 r = (*left & 0xff) + (*right & 0xff) * 256;
127 if(ioctl(fd, SOUND_MIXER_WRITE(ch), &r) == -1) {
128 error(errno, "error changing %s channel %s",
129 config->mixer, config->channel);
130 xclose(fd);
131 return -1;
132 }
133 if(oss_do_get(left, right, fd, ch) < 0) {
134 xclose(fd);
135 return -1;
136 }
137 xclose(fd);
138 return 0;
139 } else
140 return -1;
141}
142
b25aac59 143/** @brief OSS mixer vtable */
bd8895a8 144const struct mixer mixer_oss = {
145 BACKEND_OSS,
bd8895a8 146 oss_get,
147 oss_set,
148 "/dev/mixer",
149 "pcm"
150};
151#endif
152
153/*
154Local Variables:
155c-basic-offset:2
156comment-column:40
157End:
158*/